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