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
12 namespace FSShell {
13
14
15 struct file_descriptor;
16 struct select_sync;
17 struct vnode;
18
19 extern io_context* gKernelIOContext;
20
21
22 struct 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
47 struct 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
64 enum 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
80 extern file_descriptor* alloc_fd(void);
81 extern int new_fd_etc(struct io_context *,
82 struct file_descriptor *, int firstIndex);
83 extern int new_fd(struct io_context *, struct file_descriptor *);
84 extern file_descriptor* get_fd(struct io_context *, int);
85 extern void close_fd(struct file_descriptor *descriptor);
86 extern void put_fd(struct file_descriptor *descriptor);
87 extern void disconnect_fd(struct file_descriptor *descriptor);
88 extern void inc_fd_ref_count(struct file_descriptor *descriptor);
89 extern fssh_status_t select_fd(int fd, uint8_t event, uint32_t ref,
90 struct select_sync *sync, bool kernel);
91 extern fssh_status_t deselect_fd(int fd, uint8_t event,
92 struct select_sync *sync, bool kernel);
93 extern bool fd_is_valid(int fd, bool kernel);
94 extern vnode* fd_vnode(struct file_descriptor *descriptor);
95
96 extern bool fd_close_on_exec(struct io_context *context, int fd);
97 extern void fd_set_close_on_exec(struct io_context *context, int fd,
98 bool closeFD);
99
100 static 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
107 static inline struct io_context *
get_current_io_context(bool)108 get_current_io_context(bool /*kernel*/)
109 {
110 return gKernelIOContext;
111 }
112
113
114 } // namespace FSShell
115
116
117 #endif /* _FSSH_FD_H */
118