xref: /haiku/headers/private/kernel/fs/fd.h (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
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 selectsync;
21 struct select_info;
22 
23 struct fd_ops {
24 	status_t	(*fd_read)(struct file_descriptor *, off_t pos, void *buffer, size_t *length);
25 	status_t	(*fd_write)(struct file_descriptor *, off_t pos, const void *buffer, size_t *length);
26 	off_t		(*fd_seek)(struct file_descriptor *, off_t pos, int seekType);
27 	status_t	(*fd_ioctl)(struct file_descriptor *, ulong op, void *buffer, size_t length);
28 	status_t	(*fd_select)(struct file_descriptor *, uint8 event,
29 						struct selectsync *sync);
30 	status_t	(*fd_deselect)(struct file_descriptor *, uint8 event,
31 						struct selectsync *sync);
32 	status_t	(*fd_read_dir)(struct io_context* ioContext,
33 						struct file_descriptor *, struct dirent *buffer,
34 						size_t bufferSize, uint32 *_count);
35 	status_t	(*fd_rewind_dir)(struct file_descriptor *);
36 	status_t	(*fd_read_stat)(struct file_descriptor *, struct stat *);
37 	status_t	(*fd_write_stat)(struct file_descriptor *, const struct stat *, int statMask);
38 	status_t	(*fd_close)(struct file_descriptor *);
39 	void		(*fd_free)(struct file_descriptor *);
40 };
41 
42 struct file_descriptor {
43 	int32	type;               /* descriptor type */
44 	int32	ref_count;
45 	int32	open_count;
46 	struct fd_ops *ops;
47 	union {
48 		struct vnode *vnode;
49 		struct fs_mount *mount;
50 	} u;
51 	void	*cookie;
52 	int32	open_mode;
53 	off_t	pos;
54 };
55 
56 
57 /* Types of file descriptors we can create */
58 
59 enum fd_types {
60 	FDTYPE_FILE	= 1,
61 	FDTYPE_ATTR,
62 	FDTYPE_DIR,
63 	FDTYPE_ATTR_DIR,
64 	FDTYPE_INDEX,
65 	FDTYPE_INDEX_DIR,
66 	FDTYPE_QUERY,
67 	FDTYPE_SOCKET
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 *, int firstIndex);
77 extern int new_fd(struct io_context *, struct file_descriptor *);
78 extern struct file_descriptor *get_fd(struct io_context *, int);
79 extern void close_fd(struct file_descriptor *descriptor);
80 extern void put_fd(struct file_descriptor *descriptor);
81 extern void disconnect_fd(struct file_descriptor *descriptor);
82 extern void inc_fd_ref_count(struct file_descriptor *descriptor);
83 extern status_t select_fd(int32 fd, struct select_info *info, bool kernel);
84 extern status_t deselect_fd(int32 fd, struct select_info *info, bool kernel);
85 extern bool fd_is_valid(int fd, bool kernel);
86 extern struct vnode *fd_vnode(struct file_descriptor *descriptor);
87 
88 extern bool fd_close_on_exec(struct io_context *context, int fd);
89 extern void fd_set_close_on_exec(struct io_context *context, int fd, bool closeFD);
90 
91 static struct io_context *get_current_io_context(bool kernel);
92 
93 extern status_t user_fd_kernel_ioctl(int fd, ulong op, void *buffer, size_t length);
94 
95 /* The prototypes of the (sys|user)_ functions are currently defined in vfs.h */
96 
97 
98 /* Inlines */
99 
100 static inline struct io_context *
101 get_current_io_context(bool kernel)
102 {
103 	if (kernel)
104 		return (struct io_context *)team_get_kernel_team()->io_context;
105 
106 	return (struct io_context *)thread_get_current_thread()->team->io_context;
107 }
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* _FD_H */
114