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