1/*
2 * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _FSSH_FD_H
6#define _FSSH_FD_H
7
8
9#include "vfs.h"
10
11
12namespace FSShell {
13
14
15struct file_descriptor;
16struct select_sync;
17struct vnode;
18
19extern io_context* gKernelIOContext;
20
21
22struct fd_ops {
23	fssh_status_t	(*fd_read)(struct file_descriptor *, fssh_off_t pos,
24							void *buffer, fssh_size_t *length);
25	fssh_status_t	(*fd_write)(struct file_descriptor *, fssh_off_t pos,
26							const void *buffer, fssh_size_t *length);
27	fssh_off_t		(*fd_seek)(struct file_descriptor *, fssh_off_t pos,
28							int seekType);
29	fssh_status_t	(*fd_ioctl)(struct file_descriptor *, uint32_t op,
30							void *buffer, fssh_size_t length);
31	fssh_status_t	(*fd_select)(struct file_descriptor *, uint8_t event,
32							uint32_t ref, struct select_sync *sync);
33	fssh_status_t	(*fd_deselect)(struct file_descriptor *, uint8_t event,
34							struct select_sync *sync);
35	fssh_status_t	(*fd_read_dir)(struct file_descriptor *,
36							struct fssh_dirent *buffer, fssh_size_t bufferSize,
37							uint32_t *_count);
38	fssh_status_t	(*fd_rewind_dir)(struct file_descriptor *);
39	fssh_status_t	(*fd_read_stat)(struct file_descriptor *,
40							struct fssh_stat *);
41	fssh_status_t	(*fd_write_stat)(struct file_descriptor *,
42							const struct fssh_stat *, int statMask);
43	fssh_status_t	(*fd_close)(struct file_descriptor *);
44	void			(*fd_free)(struct file_descriptor *);
45};
46
47struct file_descriptor {
48	int32_t			type;               /* descriptor type */
49	int32_t			ref_count;
50	int32_t			open_count;
51	struct fd_ops*	ops;
52	union {
53		struct vnode*		vnode;
54		struct fs_mount*	mount;
55	} u;
56	void*			cookie;
57	int32_t			open_mode;
58	fssh_off_t		pos;
59};
60
61
62/* Types of file descriptors we can create */
63
64enum fd_types {
65	FDTYPE_FILE	= 1,
66	FDTYPE_ATTR,
67	FDTYPE_DIR,
68	FDTYPE_ATTR_DIR,
69	FDTYPE_INDEX,
70	FDTYPE_INDEX_DIR,
71	FDTYPE_QUERY,
72	FDTYPE_SOCKET
73};
74
75// additional open mode - kernel special
76#define FSSH_O_DISCONNECTED 0x80000000
77
78/* Prototypes */
79
80extern file_descriptor*	alloc_fd(void);
81extern int				new_fd_etc(struct io_context *,
82							struct file_descriptor *, int firstIndex);
83extern int				new_fd(struct io_context *, struct file_descriptor *);
84extern file_descriptor*	get_fd(struct io_context *, int);
85extern void				close_fd(struct file_descriptor *descriptor);
86extern void				put_fd(struct file_descriptor *descriptor);
87extern void				disconnect_fd(struct file_descriptor *descriptor);
88extern void				inc_fd_ref_count(struct file_descriptor *descriptor);
89extern fssh_status_t	select_fd(int fd, uint8_t event, uint32_t ref,
90							struct select_sync *sync, bool kernel);
91extern fssh_status_t	deselect_fd(int fd, uint8_t event,
92							struct select_sync *sync, bool kernel);
93extern bool				fd_is_valid(int fd, bool kernel);
94extern vnode*			fd_vnode(struct file_descriptor *descriptor);
95
96extern bool				fd_close_on_exec(struct io_context *context, int fd);
97extern void				fd_set_close_on_exec(struct io_context *context, int fd,
98							bool closeFD);
99
100static io_context*		get_current_io_context(bool kernel);
101
102/* The prototypes of the (sys|user)_ functions are currently defined in vfs.h */
103
104
105/* Inlines */
106
107static inline struct io_context *
108get_current_io_context(bool /*kernel*/)
109{
110	return gKernelIOContext;
111}
112
113
114}	// namespace FSShell
115
116
117#endif /* _FSSH_FD_H */
118