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
15extern "C" {
16#endif
17
18struct file_descriptor;
19struct io_context;
20struct net_socket;
21struct selectsync;
22struct select_info;
23
24struct 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
44struct 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
62enum 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
78extern struct file_descriptor *alloc_fd(void);
79extern int new_fd_etc(struct io_context *, struct file_descriptor *, int firstIndex);
80extern int new_fd(struct io_context *, struct file_descriptor *);
81extern struct file_descriptor *get_fd(struct io_context *, int);
82extern struct file_descriptor *get_open_fd(struct io_context *, int);
83extern void close_fd(struct file_descriptor *descriptor);
84extern status_t close_fd_index(struct io_context *context, int fd);
85extern void put_fd(struct file_descriptor *descriptor);
86extern void disconnect_fd(struct file_descriptor *descriptor);
87extern void inc_fd_ref_count(struct file_descriptor *descriptor);
88extern int dup_foreign_fd(team_id fromTeam, int fd, bool kernel);
89extern status_t select_fd(int32 fd, struct select_info *info, bool kernel);
90extern status_t deselect_fd(int32 fd, struct select_info *info, bool kernel);
91extern bool fd_is_valid(int fd, bool kernel);
92extern struct vnode *fd_vnode(struct file_descriptor *descriptor);
93
94extern bool fd_close_on_exec(struct io_context *context, int fd);
95extern void fd_set_close_on_exec(struct io_context *context, int fd, bool closeFD);
96
97static struct io_context *get_current_io_context(bool kernel);
98
99extern status_t user_fd_kernel_ioctl(int fd, ulong op, void *buffer, size_t length);
100
101/* The prototypes of the (sys|user)_ functions are currently defined in vfs.h */
102
103
104/* Inlines */
105
106static inline struct io_context *
107get_current_io_context(bool kernel)
108{
109	if (kernel)
110		return (struct io_context *)team_get_kernel_team()->io_context;
111
112	return (struct io_context *)thread_get_current_thread()->team->io_context;
113}
114
115#ifdef __cplusplus
116}
117#endif
118
119#endif /* _FD_H */
120