xref: /haiku/headers/private/kernel/fs/fd.h (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
1 /*
2  * Copyright 2002-2018, 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 selectsync;
21 struct select_info;
22 
23 struct fd_ops {
24 	status_t	(*fd_close)(struct file_descriptor *);
25 	void		(*fd_free)(struct file_descriptor *);
26 
27 	status_t	(*fd_read)(struct file_descriptor *, off_t pos, void *buffer,
28 						size_t *length);
29 	status_t	(*fd_write)(struct file_descriptor *, off_t pos,
30 						const void *buffer, size_t *length);
31 	ssize_t		(*fd_readv)(struct file_descriptor *, off_t pos,
32 						const struct iovec *vecs, int count);
33 	ssize_t		(*fd_writev)(struct file_descriptor *, off_t pos,
34 						const struct iovec *vecs, int count);
35 	off_t		(*fd_seek)(struct file_descriptor *, off_t pos, int seekType);
36 
37 	status_t	(*fd_ioctl)(struct file_descriptor *, ulong op, void *buffer,
38 						size_t length);
39 	status_t	(*fd_set_flags)(struct file_descriptor *, int flags);
40 
41 	status_t	(*fd_select)(struct file_descriptor *, uint8 event,
42 						struct selectsync *sync);
43 	status_t	(*fd_deselect)(struct file_descriptor *, uint8 event,
44 						struct selectsync *sync);
45 
46 	status_t	(*fd_read_dir)(struct io_context* ioContext,
47 						struct file_descriptor *, struct dirent *buffer,
48 						size_t bufferSize, uint32 *_count);
49 	status_t	(*fd_rewind_dir)(struct file_descriptor *);
50 
51 	status_t	(*fd_read_stat)(struct file_descriptor *, struct stat *);
52 	status_t	(*fd_write_stat)(struct file_descriptor *, const struct stat *,
53 		int statMask);
54 };
55 
56 struct file_descriptor {
57 	int32	ref_count;
58 	int32	open_count;
59 	struct fd_ops *ops;
60 	union {
61 		struct vnode *vnode;
62 		struct fs_mount *mount;
63 	} u;
64 	void	*cookie;
65 	int32	open_mode;
66 	off_t	pos;
67 };
68 
69 
70 // additional open mode - kernel special
71 #define O_DISCONNECTED 0x80000000
72 
73 /* Prototypes */
74 
75 extern struct file_descriptor *alloc_fd(void);
76 extern int new_fd_etc(struct io_context *, struct file_descriptor *,
77 	int firstIndex);
78 extern int new_fd(struct io_context *, struct file_descriptor *);
79 extern struct file_descriptor *get_fd(struct io_context *, int);
80 extern struct file_descriptor *get_open_fd(struct io_context *, int);
81 extern void close_fd(struct io_context *context,
82 	struct file_descriptor *descriptor);
83 extern status_t close_fd_index(struct io_context *context, int fd);
84 extern void put_fd(struct file_descriptor *descriptor);
85 extern void disconnect_fd(struct file_descriptor *descriptor);
86 extern void inc_fd_ref_count(struct file_descriptor *descriptor);
87 extern int dup_foreign_fd(team_id fromTeam, int fd, bool kernel);
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 extern bool fd_is_file(struct file_descriptor* descriptor);
93 
94 extern bool fd_close_on_exec(struct io_context *context, int fd);
95 extern void fd_set_close_on_exec(struct io_context *context, int fd,
96 	bool closeFD);
97 
98 static struct io_context *get_current_io_context(bool kernel);
99 
100 extern status_t user_fd_kernel_ioctl(int fd, ulong op, void *buffer,
101 	size_t length);
102 
103 /* The prototypes of the (sys|user)_ functions are currently defined in vfs.h */
104 
105 
106 /* Inlines */
107 
108 static inline struct io_context *
109 get_current_io_context(bool kernel)
110 {
111 	if (kernel)
112 		return (struct io_context *)team_get_kernel_team()->io_context;
113 
114 	return (struct io_context *)thread_get_current_thread()->team->io_context;
115 }
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* _FD_H */
122