xref: /haiku/headers/os/drivers/fs_interface.h (revision a26c24392042ca7bfad2e78f73a0b75c463c0455)
1 /*
2  * Copyright 2004-2008, Haiku Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _FS_INTERFACE_H
6 #define _FS_INTERFACE_H
7 
8 /*! File System Interface Layer Definition */
9 
10 
11 #include <OS.h>
12 #include <Select.h>
13 #include <module.h>
14 #include <disk_device_manager.h>
15 
16 #include <sys/uio.h>
17 
18 
19 struct dirent;
20 struct stat;
21 struct fs_info;
22 struct select_sync;
23 
24 typedef struct IORequest io_request;
25 
26 
27 /* additional flags passed to write_stat() (see NodeMonitor.h for the others) */
28 // NOTE: Changing the constants here or in NodeMonitor.h will break
29 // src/kits/storage/LibBeAdapter.cpp:_kern_write_stat().
30 #define B_STAT_SIZE_INSECURE	0x2000
31 	// TODO: this should be faded out once BFS supports sparse files
32 
33 /* passed to write_fs_info() */
34 #define	FS_WRITE_FSINFO_NAME	0x0001
35 
36 struct file_io_vec {
37 	off_t	offset;
38 	off_t	length;
39 };
40 
41 #define	B_CURRENT_FS_API_VERSION "/v1"
42 
43 // flags for publish_vnode() and fs_volume_ops::get_vnode()
44 #define B_VNODE_PUBLISH_REMOVED					0x01
45 #define B_VNODE_DONT_CREATE_SPECIAL_SUB_NODE	0x02
46 
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 typedef struct fs_volume fs_volume;
53 typedef struct fs_volume_ops fs_volume_ops;
54 typedef struct fs_vnode fs_vnode;
55 typedef struct fs_vnode_ops fs_vnode_ops;
56 typedef struct file_system_module_info file_system_module_info;
57 
58 struct fs_volume {
59 	dev_t						id;
60 	partition_id				partition;
61 	int32						layer;
62 	void*						private_volume;
63 	fs_volume_ops*				ops;
64 	fs_volume*					sub_volume;
65 	fs_volume*					super_volume;
66 	file_system_module_info*	file_system;
67 	char*						file_system_name;
68 };
69 
70 struct fs_vnode {
71 	void*			private_node;
72 	fs_vnode_ops*	ops;
73 };
74 
75 struct fs_volume_ops {
76 	status_t (*unmount)(fs_volume *volume);
77 
78 	status_t (*read_fs_info)(fs_volume *volume, struct fs_info *info);
79 	status_t (*write_fs_info)(fs_volume *volume, const struct fs_info *info,
80 				uint32 mask);
81 	status_t (*sync)(fs_volume *volume);
82 
83 	status_t (*get_vnode)(fs_volume *volume, ino_t id, fs_vnode *vnode,
84 				int *_type, uint32 *_flags, bool reenter);
85 
86 	/* index directory & index operations */
87 	status_t (*open_index_dir)(fs_volume *volume, void **cookie);
88 	status_t (*close_index_dir)(fs_volume *volume, void *cookie);
89 	status_t (*free_index_dir_cookie)(fs_volume *volume, void *cookie);
90 	status_t (*read_index_dir)(fs_volume *volume, void *cookie,
91 				struct dirent *buffer, size_t bufferSize, uint32 *_num);
92 	status_t (*rewind_index_dir)(fs_volume *volume, void *cookie);
93 
94 	status_t (*create_index)(fs_volume *volume, const char *name, uint32 type,
95 				uint32 flags);
96 	status_t (*remove_index)(fs_volume *volume, const char *name);
97 	status_t (*read_index_stat)(fs_volume *volume, const char *name,
98 				struct stat *stat);
99 
100 	/* query operations */
101 	status_t (*open_query)(fs_volume *volume, const char *query, uint32 flags,
102 				port_id port, uint32 token, void **_cookie);
103 	status_t (*close_query)(fs_volume *volume, void *cookie);
104 	status_t (*free_query_cookie)(fs_volume *volume, void *cookie);
105 	status_t (*read_query)(fs_volume *volume, void *cookie,
106 				struct dirent *buffer, size_t bufferSize, uint32 *_num);
107 	status_t (*rewind_query)(fs_volume *volume, void *cookie);
108 
109 	/* support for FS layers */
110 	status_t (*create_sub_vnode)(fs_volume *volume, ino_t id, fs_vnode *vnode);
111 	status_t (*delete_sub_vnode)(fs_volume *volume, fs_vnode *vnode);
112 };
113 
114 struct fs_vnode_ops {
115 	/* vnode operations */
116 	status_t (*lookup)(fs_volume *volume, fs_vnode *dir, const char *name,
117 				ino_t *_id);
118 	status_t (*get_vnode_name)(fs_volume *volume, fs_vnode *vnode, char *buffer,
119 				size_t bufferSize);
120 
121 	status_t (*put_vnode)(fs_volume *volume, fs_vnode *vnode, bool reenter);
122 	status_t (*remove_vnode)(fs_volume *volume, fs_vnode *vnode, bool reenter);
123 
124 	/* VM file access */
125 	bool (*can_page)(fs_volume *volume, fs_vnode *vnode, void *cookie);
126 	status_t (*read_pages)(fs_volume *volume, fs_vnode *vnode, void *cookie,
127 				off_t pos, const iovec *vecs, size_t count, size_t *_numBytes);
128 	status_t (*write_pages)(fs_volume *volume, fs_vnode *vnode,
129 				void *cookie, off_t pos, const iovec *vecs, size_t count,
130 				size_t *_numBytes);
131 
132 	/* asynchronous I/O */
133 	status_t (*io)(fs_volume *volume, fs_vnode *vnode, void *cookie,
134 				io_request *request);
135 	status_t (*cancel_io)(fs_volume *volume, fs_vnode *vnode, void *cookie,
136 				io_request *request);
137 
138 	/* cache file access */
139 	status_t (*get_file_map)(fs_volume *volume, fs_vnode *vnode, off_t offset,
140 				size_t size, struct file_io_vec *vecs, size_t *_count);
141 
142 	/* common operations */
143 	status_t (*ioctl)(fs_volume *volume, fs_vnode *vnode, void *cookie,
144 				ulong op, void *buffer, size_t length);
145 	status_t (*set_flags)(fs_volume *volume, fs_vnode *vnode, void *cookie,
146 				int flags);
147 	status_t (*select)(fs_volume *volume, fs_vnode *vnode, void *cookie,
148 				uint8 event, selectsync *sync);
149 	status_t (*deselect)(fs_volume *volume, fs_vnode *vnode, void *cookie,
150 				uint8 event, selectsync *sync);
151 	status_t (*fsync)(fs_volume *volume, fs_vnode *vnode);
152 
153 	status_t (*read_symlink)(fs_volume *volume, fs_vnode *link, char *buffer,
154 				size_t *_bufferSize);
155 	status_t (*create_symlink)(fs_volume *volume, fs_vnode *dir,
156 				const char *name, const char *path, int mode);
157 
158 	status_t (*link)(fs_volume *volume, fs_vnode *dir, const char *name,
159 				fs_vnode *vnode);
160 	status_t (*unlink)(fs_volume *volume, fs_vnode *dir, const char *name);
161 	status_t (*rename)(fs_volume *volume, fs_vnode *fromDir,
162 				const char *fromName, fs_vnode *toDir, const char *toName);
163 
164 	status_t (*access)(fs_volume *volume, fs_vnode *vnode, int mode);
165 	status_t (*read_stat)(fs_volume *volume, fs_vnode *vnode,
166 				struct stat *stat);
167 	status_t (*write_stat)(fs_volume *volume, fs_vnode *vnode,
168 				const struct stat *stat, uint32 statMask);
169 
170 	/* file operations */
171 	status_t (*create)(fs_volume *volume, fs_vnode *dir, const char *name,
172 				int openMode, int perms, void **_cookie,
173 				ino_t *_newVnodeID);
174 	status_t (*open)(fs_volume *volume, fs_vnode *vnode, int openMode,
175 				void **_cookie);
176 	status_t (*close)(fs_volume *volume, fs_vnode *vnode, void *cookie);
177 	status_t (*free_cookie)(fs_volume *volume, fs_vnode *vnode,
178 				void *cookie);
179 	status_t (*read)(fs_volume *volume, fs_vnode *vnode, void *cookie,
180 				off_t pos, void *buffer, size_t *length);
181 	status_t (*write)(fs_volume *volume, fs_vnode *vnode, void *cookie,
182 				off_t pos, const void *buffer, size_t *length);
183 
184 	/* directory operations */
185 	status_t (*create_dir)(fs_volume *volume, fs_vnode *parent,
186 				const char *name, int perms, ino_t *_newVnodeID);
187 	status_t (*remove_dir)(fs_volume *volume, fs_vnode *parent,
188 				const char *name);
189 	status_t (*open_dir)(fs_volume *volume, fs_vnode *vnode,
190 				void **_cookie);
191 	status_t (*close_dir)(fs_volume *volume, fs_vnode *vnode, void *cookie);
192 	status_t (*free_dir_cookie)(fs_volume *volume, fs_vnode *vnode,
193 				void *cookie);
194 	status_t (*read_dir)(fs_volume *volume, fs_vnode *vnode, void *cookie,
195 				struct dirent *buffer, size_t bufferSize, uint32 *_num);
196 	status_t (*rewind_dir)(fs_volume *volume, fs_vnode *vnode,
197 				void *cookie);
198 
199 	/* attribute directory operations */
200 	status_t (*open_attr_dir)(fs_volume *volume, fs_vnode *vnode,
201 				void **_cookie);
202 	status_t (*close_attr_dir)(fs_volume *volume, fs_vnode *vnode,
203 				void *cookie);
204 	status_t (*free_attr_dir_cookie)(fs_volume *volume, fs_vnode *vnode,
205 				void *cookie);
206 	status_t (*read_attr_dir)(fs_volume *volume, fs_vnode *vnode,
207 				void *cookie, struct dirent *buffer, size_t bufferSize,
208 				uint32 *_num);
209 	status_t (*rewind_attr_dir)(fs_volume *volume, fs_vnode *vnode,
210 				void *cookie);
211 
212 	/* attribute operations */
213 	status_t (*create_attr)(fs_volume *volume, fs_vnode *vnode,
214 				const char *name, uint32 type, int openMode,
215 				void **_cookie);
216 	status_t (*open_attr)(fs_volume *volume, fs_vnode *vnode, const char *name,
217 				int openMode, void **_cookie);
218 	status_t (*close_attr)(fs_volume *volume, fs_vnode *vnode,
219 				void *cookie);
220 	status_t (*free_attr_cookie)(fs_volume *volume, fs_vnode *vnode,
221 				void *cookie);
222 	status_t (*read_attr)(fs_volume *volume, fs_vnode *vnode, void *cookie,
223 				off_t pos, void *buffer, size_t *length);
224 	status_t (*write_attr)(fs_volume *volume, fs_vnode *vnode, void *cookie,
225 				off_t pos, const void *buffer, size_t *length);
226 
227 	status_t (*read_attr_stat)(fs_volume *volume, fs_vnode *vnode,
228 				void *cookie, struct stat *stat);
229 	status_t (*write_attr_stat)(fs_volume *volume, fs_vnode *vnode,
230 				void *cookie, const struct stat *stat, int statMask);
231 	status_t (*rename_attr)(fs_volume *volume, fs_vnode *fromVnode,
232 				const char *fromName, fs_vnode *toVnode, const char *toName);
233 	status_t (*remove_attr)(fs_volume *volume, fs_vnode *vnode,
234 				const char *name);
235 
236 	/* support for node and FS layers */
237 	status_t (*create_special_node)(fs_volume *volume, fs_vnode *dir,
238 				const char *name, fs_vnode *subVnode, mode_t mode, uint32 flags,
239 				fs_vnode *_superVnode, ino_t *_nodeID);
240 	status_t (*get_super_vnode)(fs_volume *volume, fs_vnode *vnode,
241 				fs_volume *superVolume, fs_vnode *superVnode);
242 };
243 
244 struct file_system_module_info {
245 	struct module_info	info;
246 	const char*			short_name;
247 	const char*			pretty_name;
248 	uint32				flags;	// DDM flags
249 
250 	/* scanning (the device is write locked) */
251 	float (*identify_partition)(int fd, partition_data *partition,
252 				void **cookie);
253 	status_t (*scan_partition)(int fd, partition_data *partition,
254 				void *cookie);
255 	void (*free_identify_partition_cookie)(partition_data *partition,
256 				void *cookie);
257 	void (*free_partition_content_cookie)(partition_data *partition);
258 
259 	/* general operations */
260 	status_t (*mount)(fs_volume *volume, const char *device, uint32 flags,
261 				const char *args, ino_t *_rootVnodeID);
262 
263 	/* capability querying (the device is read locked) */
264 	uint32 (*get_supported_operations)(partition_data* partition, uint32 mask);
265 
266 	bool (*validate_resize)(partition_data *partition, off_t *size);
267 	bool (*validate_move)(partition_data *partition, off_t *start);
268 	bool (*validate_set_content_name)(partition_data *partition,
269 				char *name);
270 	bool (*validate_set_content_parameters)(partition_data *partition,
271 				const char *parameters);
272 	bool (*validate_initialize)(partition_data *partition, char *name,
273 				const char *parameters);
274 
275 	/* shadow partition modification (device is write locked) */
276 	status_t (*shadow_changed)(partition_data *partition,
277 				partition_data *child, uint32 operation);
278 
279 	/* writing (the device is NOT locked) */
280 	status_t (*defragment)(int fd, partition_id partition,
281 				disk_job_id job);
282 	status_t (*repair)(int fd, partition_id partition, bool checkOnly,
283 				disk_job_id job);
284 	status_t (*resize)(int fd, partition_id partition, off_t size,
285 				disk_job_id job);
286 	status_t (*move)(int fd, partition_id partition, off_t offset,
287 				disk_job_id job);
288 	status_t (*set_content_name)(int fd, partition_id partition,
289 				const char *name, disk_job_id job);
290 	status_t (*set_content_parameters)(int fd, partition_id partition,
291 				const char *parameters, disk_job_id job);
292 	status_t (*initialize)(int fd, partition_id partition, const char *name,
293 				const char *parameters, off_t partitionSize, disk_job_id job);
294 };
295 
296 
297 /* file system add-ons only prototypes */
298 
299 // callbacks for do_iterative_fd_io()
300 typedef status_t (*iterative_io_get_vecs)(void *cookie, io_request* request,
301 				off_t offset, size_t size, struct file_io_vec *vecs,
302 				size_t *_count);
303 typedef status_t (*iterative_io_finished)(void* cookie, io_request* request,
304 				status_t status, bool partialTransfer, size_t bytesTransferred);
305 
306 extern status_t new_vnode(fs_volume *volume, ino_t vnodeID, void *privateNode,
307 					fs_vnode_ops *ops);
308 extern status_t publish_vnode(fs_volume *volume, ino_t vnodeID,
309 					void *privateNode, fs_vnode_ops *ops, int type,
310 					uint32 flags);
311 extern status_t get_vnode(fs_volume *volume, ino_t vnodeID,
312 					void **_privateNode, fs_vnode_ops **_vnodeOps);
313 extern status_t put_vnode(fs_volume *volume, ino_t vnodeID);
314 extern status_t acquire_vnode(fs_volume *volume, ino_t vnodeID);
315 extern status_t remove_vnode(fs_volume *volume, ino_t vnodeID);
316 extern status_t unremove_vnode(fs_volume *volume, ino_t vnodeID);
317 extern status_t get_vnode_removed(fs_volume *volume, ino_t vnodeID,
318 					bool *removed);
319 extern fs_volume* volume_for_vnode(fs_vnode *vnode);
320 
321 extern status_t read_pages(int fd, off_t pos, const struct iovec *vecs,
322 					size_t count, size_t *_numBytes);
323 extern status_t write_pages(int fd, off_t pos, const struct iovec *vecs,
324 					size_t count, size_t *_numBytes);
325 extern status_t read_file_io_vec_pages(int fd,
326 					const struct file_io_vec *fileVecs, size_t fileVecCount,
327 					const struct iovec *vecs, size_t vecCount,
328 					uint32 *_vecIndex, size_t *_vecOffset, size_t *_bytes);
329 extern status_t write_file_io_vec_pages(int fd,
330 					const struct file_io_vec *fileVecs, size_t fileVecCount,
331 					const struct iovec *vecs, size_t vecCount,
332 					uint32 *_vecIndex, size_t *_vecOffset, size_t *_bytes);
333 extern status_t do_fd_io(int fd, io_request *request);
334 extern status_t do_iterative_fd_io(int fd, io_request *request,
335 					iterative_io_get_vecs getVecs,
336 					iterative_io_finished finished, void *cookie);
337 
338 extern status_t notify_entry_created(dev_t device, ino_t directory,
339 					const char *name, ino_t node);
340 extern status_t notify_entry_removed(dev_t device, ino_t directory,
341 					const char *name, ino_t node);
342 extern status_t notify_entry_moved(dev_t device, ino_t fromDirectory,
343 					const char *fromName, ino_t toDirectory,
344 					const char *toName, ino_t node);
345 extern status_t notify_stat_changed(dev_t device, ino_t node,
346 					uint32 statFields);
347 extern status_t notify_attribute_changed(dev_t device, ino_t node,
348 					const char *attribute, int32 cause);
349 
350 extern status_t notify_query_entry_created(port_id port, int32 token,
351 					dev_t device, ino_t directory, const char *name,
352 					ino_t node);
353 extern status_t notify_query_entry_removed(port_id port, int32 token,
354 					dev_t device, ino_t directory, const char *name,
355 					ino_t node);
356 
357 #ifdef __cplusplus
358 }
359 #endif
360 
361 #endif	/* _FS_INTERFACE_H */
362