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