xref: /haiku/headers/os/drivers/fs_interface.h (revision 98a5231fe5497c526849f2d84b1a9bbcbdfd2dbc)
1 /*
2  * Copyright 2004-2010, 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 enum BVnodeOperation {
49 	B_VNODE_OPERATION_IO	= 0
50 };
51 
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 typedef struct fs_volume fs_volume;
58 typedef struct fs_volume_ops fs_volume_ops;
59 typedef struct fs_vnode fs_vnode;
60 typedef struct fs_vnode_ops fs_vnode_ops;
61 typedef struct file_system_module_info file_system_module_info;
62 
63 struct fs_volume {
64 	dev_t						id;
65 	partition_id				partition;
66 	int32						layer;
67 	void*						private_volume;
68 	fs_volume_ops*				ops;
69 	fs_volume*					sub_volume;
70 	fs_volume*					super_volume;
71 	file_system_module_info*	file_system;
72 	char*						file_system_name;
73 };
74 
75 struct fs_vnode {
76 	void*			private_node;
77 	fs_vnode_ops*	ops;
78 };
79 
80 struct fs_volume_ops {
81 	status_t (*unmount)(fs_volume* volume);
82 
83 	status_t (*read_fs_info)(fs_volume* volume, struct fs_info* info);
84 	status_t (*write_fs_info)(fs_volume* volume, const struct fs_info* info,
85 				uint32 mask);
86 	status_t (*sync)(fs_volume* volume);
87 
88 	status_t (*get_vnode)(fs_volume* volume, ino_t id, fs_vnode* vnode,
89 				int* _type, uint32* _flags, bool reenter);
90 
91 	/* index directory & index operations */
92 	status_t (*open_index_dir)(fs_volume* volume, void** _cookie);
93 	status_t (*close_index_dir)(fs_volume* volume, void* cookie);
94 	status_t (*free_index_dir_cookie)(fs_volume* volume, void* cookie);
95 	status_t (*read_index_dir)(fs_volume* volume, void* cookie,
96 				struct dirent* buffer, size_t bufferSize, uint32* _num);
97 	status_t (*rewind_index_dir)(fs_volume* volume, void* cookie);
98 
99 	status_t (*create_index)(fs_volume* volume, const char* name, uint32 type,
100 				uint32 flags);
101 	status_t (*remove_index)(fs_volume* volume, const char* name);
102 	status_t (*read_index_stat)(fs_volume* volume, const char* name,
103 				struct stat* stat);
104 
105 	/* query operations */
106 	status_t (*open_query)(fs_volume* volume, const char* query, uint32 flags,
107 				port_id port, uint32 token, void** _cookie);
108 	status_t (*close_query)(fs_volume* volume, void* cookie);
109 	status_t (*free_query_cookie)(fs_volume* volume, void* cookie);
110 	status_t (*read_query)(fs_volume* volume, void* cookie,
111 				struct dirent* buffer, size_t bufferSize, uint32* _num);
112 	status_t (*rewind_query)(fs_volume* volume, void* cookie);
113 
114 	/* support for FS layers */
115 	status_t (*all_layers_mounted)(fs_volume* volume);
116 	status_t (*create_sub_vnode)(fs_volume* volume, ino_t id, fs_vnode* vnode);
117 	status_t (*delete_sub_vnode)(fs_volume* volume, fs_vnode* vnode);
118 };
119 
120 struct fs_vnode_ops {
121 	/* vnode operations */
122 	status_t (*lookup)(fs_volume* volume, fs_vnode* dir, const char* name,
123 				ino_t* _id);
124 	status_t (*get_vnode_name)(fs_volume* volume, fs_vnode* vnode, char* buffer,
125 				size_t bufferSize);
126 
127 	status_t (*put_vnode)(fs_volume* volume, fs_vnode* vnode, bool reenter);
128 	status_t (*remove_vnode)(fs_volume* volume, fs_vnode* vnode, bool reenter);
129 
130 	/* VM file access */
131 	bool (*can_page)(fs_volume* volume, fs_vnode* vnode, void* cookie);
132 	status_t (*read_pages)(fs_volume* volume, fs_vnode* vnode, void* cookie,
133 				off_t pos, const iovec* vecs, size_t count, size_t* _numBytes);
134 	status_t (*write_pages)(fs_volume* volume, fs_vnode* vnode,
135 				void* cookie, off_t pos, const iovec* vecs, size_t count,
136 				size_t* _numBytes);
137 
138 	/* asynchronous I/O */
139 	status_t (*io)(fs_volume* volume, fs_vnode* vnode, void* cookie,
140 				io_request* request);
141 	status_t (*cancel_io)(fs_volume* volume, fs_vnode* vnode, void* cookie,
142 				io_request* request);
143 
144 	/* cache file access */
145 	status_t (*get_file_map)(fs_volume* volume, fs_vnode* vnode, off_t offset,
146 				size_t size, struct file_io_vec* vecs, size_t* _count);
147 
148 	/* common operations */
149 	status_t (*ioctl)(fs_volume* volume, fs_vnode* vnode, void* cookie,
150 				uint32 op, void* buffer, size_t length);
151 	status_t (*set_flags)(fs_volume* volume, fs_vnode* vnode, void* cookie,
152 				int flags);
153 	status_t (*select)(fs_volume* volume, fs_vnode* vnode, void* cookie,
154 				uint8 event, selectsync* sync);
155 	status_t (*deselect)(fs_volume* volume, fs_vnode* vnode, void* cookie,
156 				uint8 event, selectsync* sync);
157 	status_t (*fsync)(fs_volume* volume, fs_vnode* vnode);
158 
159 	status_t (*read_symlink)(fs_volume* volume, fs_vnode* link, char* buffer,
160 				size_t* _bufferSize);
161 	status_t (*create_symlink)(fs_volume* volume, fs_vnode* dir,
162 				const char* name, const char* path, int mode);
163 
164 	status_t (*link)(fs_volume* volume, fs_vnode* dir, const char* name,
165 				fs_vnode* vnode);
166 	status_t (*unlink)(fs_volume* volume, fs_vnode* dir, const char* name);
167 	status_t (*rename)(fs_volume* volume, fs_vnode* fromDir,
168 				const char* fromName, fs_vnode* toDir, const char* toName);
169 
170 	status_t (*access)(fs_volume* volume, fs_vnode* vnode, int mode);
171 	status_t (*read_stat)(fs_volume* volume, fs_vnode* vnode,
172 				struct stat* stat);
173 	status_t (*write_stat)(fs_volume* volume, fs_vnode* vnode,
174 				const struct stat* stat, uint32 statMask);
175 	status_t (*preallocate)(fs_volume* volume, fs_vnode* vnode,
176 				off_t pos, off_t length);
177 
178 	/* file operations */
179 	status_t (*create)(fs_volume* volume, fs_vnode* dir, const char* name,
180 				int openMode, int perms, void** _cookie,
181 				ino_t* _newVnodeID);
182 	status_t (*open)(fs_volume* volume, fs_vnode* vnode, int openMode,
183 				void** _cookie);
184 	status_t (*close)(fs_volume* volume, fs_vnode* vnode, void* cookie);
185 	status_t (*free_cookie)(fs_volume* volume, fs_vnode* vnode,
186 				void* cookie);
187 	status_t (*read)(fs_volume* volume, fs_vnode* vnode, void* cookie,
188 				off_t pos, void* buffer, size_t* length);
189 	status_t (*write)(fs_volume* volume, fs_vnode* vnode, void* cookie,
190 				off_t pos, const void* buffer, size_t* length);
191 
192 	/* directory operations */
193 	status_t (*create_dir)(fs_volume* volume, fs_vnode* parent,
194 				const char* name, int perms);
195 	status_t (*remove_dir)(fs_volume* volume, fs_vnode* parent,
196 				const char* name);
197 	status_t (*open_dir)(fs_volume* volume, fs_vnode* vnode,
198 				void** _cookie);
199 	status_t (*close_dir)(fs_volume* volume, fs_vnode* vnode, void* cookie);
200 	status_t (*free_dir_cookie)(fs_volume* volume, fs_vnode* vnode,
201 				void* cookie);
202 	status_t (*read_dir)(fs_volume* volume, fs_vnode* vnode, void* cookie,
203 				struct dirent* buffer, size_t bufferSize, uint32* _num);
204 	status_t (*rewind_dir)(fs_volume* volume, fs_vnode* vnode,
205 				void* cookie);
206 
207 	/* attribute directory operations */
208 	status_t (*open_attr_dir)(fs_volume* volume, fs_vnode* vnode,
209 				void** _cookie);
210 	status_t (*close_attr_dir)(fs_volume* volume, fs_vnode* vnode,
211 				void* cookie);
212 	status_t (*free_attr_dir_cookie)(fs_volume* volume, fs_vnode* vnode,
213 				void* cookie);
214 	status_t (*read_attr_dir)(fs_volume* volume, fs_vnode* vnode,
215 				void* cookie, struct dirent* buffer, size_t bufferSize,
216 				uint32* _num);
217 	status_t (*rewind_attr_dir)(fs_volume* volume, fs_vnode* vnode,
218 				void* cookie);
219 
220 	/* attribute operations */
221 	status_t (*create_attr)(fs_volume* volume, fs_vnode* vnode,
222 				const char* name, uint32 type, int openMode,
223 				void** _cookie);
224 	status_t (*open_attr)(fs_volume* volume, fs_vnode* vnode, const char* name,
225 				int openMode, void** _cookie);
226 	status_t (*close_attr)(fs_volume* volume, fs_vnode* vnode,
227 				void* cookie);
228 	status_t (*free_attr_cookie)(fs_volume* volume, fs_vnode* vnode,
229 				void* cookie);
230 	status_t (*read_attr)(fs_volume* volume, fs_vnode* vnode, void* cookie,
231 				off_t pos, void* buffer, size_t* length);
232 	status_t (*write_attr)(fs_volume* volume, fs_vnode* vnode, void* cookie,
233 				off_t pos, const void* buffer, size_t* length);
234 
235 	status_t (*read_attr_stat)(fs_volume* volume, fs_vnode* vnode,
236 				void* cookie, struct stat* stat);
237 	status_t (*write_attr_stat)(fs_volume* volume, fs_vnode* vnode,
238 				void* cookie, const struct stat* stat, int statMask);
239 	status_t (*rename_attr)(fs_volume* volume, fs_vnode* fromVnode,
240 				const char* fromName, fs_vnode* toVnode, const char* toName);
241 	status_t (*remove_attr)(fs_volume* volume, fs_vnode* vnode,
242 				const char* name);
243 
244 	/* support for node and FS layers */
245 	status_t (*create_special_node)(fs_volume* volume, fs_vnode* dir,
246 				const char* name, fs_vnode* subVnode, mode_t mode, uint32 flags,
247 				fs_vnode* _superVnode, ino_t* _nodeID);
248 	status_t (*get_super_vnode)(fs_volume* volume, fs_vnode* vnode,
249 				fs_volume* superVolume, fs_vnode* superVnode);
250 
251 	/* lock operations */
252 	status_t (*test_lock)(fs_volume* volume, fs_vnode* vnode, void* cookie,
253 				struct flock* lock);
254 	status_t (*acquire_lock)(fs_volume* volume, fs_vnode* vnode, void* cookie,
255 				const struct flock* lock, bool wait);
256 	status_t (*release_lock)(fs_volume* volume, fs_vnode* vnode, void* cookie,
257 				const struct flock* lock);
258 
259 	/* supported vnode operations info */
260 	bool (*supports_operation)(fs_volume* volume, fs_vnode* vnode,
261 				enum BVnodeOperation operation);
262 };
263 
264 struct file_system_module_info {
265 	struct module_info	info;
266 	const char*			short_name;
267 	const char*			pretty_name;
268 	uint32				flags;	// DDM flags
269 
270 	/* scanning (the device is write locked) */
271 	float (*identify_partition)(int fd, partition_data* partition,
272 				void** _cookie);
273 	status_t (*scan_partition)(int fd, partition_data* partition,
274 				void* cookie);
275 	void (*free_identify_partition_cookie)(partition_data* partition,
276 				void* cookie);
277 	void (*free_partition_content_cookie)(partition_data* partition);
278 
279 	/* general operations */
280 	status_t (*mount)(fs_volume* volume, const char* device, uint32 flags,
281 				const char* args, ino_t* _rootVnodeID);
282 
283 	/* capability querying (the device is read locked) */
284 	uint32 (*get_supported_operations)(partition_data* partition, uint32 mask);
285 
286 	bool (*validate_resize)(partition_data* partition, off_t* size);
287 	bool (*validate_move)(partition_data* partition, off_t* start);
288 	bool (*validate_set_content_name)(partition_data* partition,
289 				char* name);
290 	bool (*validate_set_content_parameters)(partition_data* partition,
291 				const char* parameters);
292 	bool (*validate_initialize)(partition_data* partition, char* name,
293 				const char* parameters);
294 
295 	/* shadow partition modification (device is write locked) */
296 	status_t (*shadow_changed)(partition_data* partition,
297 				partition_data* child, uint32 operation);
298 
299 	/* writing (the device is NOT locked) */
300 	status_t (*defragment)(int fd, partition_id partition,
301 				disk_job_id job);
302 	status_t (*repair)(int fd, partition_id partition, bool checkOnly,
303 				disk_job_id job);
304 	status_t (*resize)(int fd, partition_id partition, off_t size,
305 				disk_job_id job);
306 	status_t (*move)(int fd, partition_id partition, off_t offset,
307 				disk_job_id job);
308 	status_t (*set_content_name)(int fd, partition_id partition,
309 				const char* name, disk_job_id job);
310 	status_t (*set_content_parameters)(int fd, partition_id partition,
311 				const char* parameters, disk_job_id job);
312 	status_t (*initialize)(int fd, partition_id partition, const char* name,
313 				const char* parameters, off_t partitionSize, disk_job_id job);
314 	status_t (*uninitialize)(int fd, partition_id partition,
315 				off_t partitionSize, uint32 blockSize, disk_job_id job);
316 };
317 
318 
319 /* file system add-ons only prototypes */
320 
321 // callbacks for do_iterative_fd_io()
322 typedef status_t (*iterative_io_get_vecs)(void* cookie, io_request* request,
323 				off_t offset, size_t size, struct file_io_vec* vecs,
324 				size_t* _count);
325 typedef status_t (*iterative_io_finished)(void* cookie, io_request* request,
326 				status_t status, bool partialTransfer, size_t bytesTransferred);
327 
328 extern status_t new_vnode(fs_volume* volume, ino_t vnodeID, void* privateNode,
329 					fs_vnode_ops* ops);
330 extern status_t publish_vnode(fs_volume* volume, ino_t vnodeID,
331 					void* privateNode, fs_vnode_ops* ops, int type,
332 					uint32 flags);
333 extern status_t get_vnode(fs_volume* volume, ino_t vnodeID,
334 					void** _privateNode);
335 extern status_t put_vnode(fs_volume* volume, ino_t vnodeID);
336 extern status_t acquire_vnode(fs_volume* volume, ino_t vnodeID);
337 extern status_t remove_vnode(fs_volume* volume, ino_t vnodeID);
338 extern status_t unremove_vnode(fs_volume* volume, ino_t vnodeID);
339 extern status_t get_vnode_removed(fs_volume* volume, ino_t vnodeID,
340 					bool* _removed);
341 extern fs_volume* volume_for_vnode(fs_vnode* vnode);
342 
343 extern status_t read_pages(int fd, off_t pos, const struct iovec* vecs,
344 					size_t count, size_t* _numBytes);
345 extern status_t write_pages(int fd, off_t pos, const struct iovec* vecs,
346 					size_t count, size_t* _numBytes);
347 extern status_t read_file_io_vec_pages(int fd,
348 					const struct file_io_vec* fileVecs, size_t fileVecCount,
349 					const struct iovec* vecs, size_t vecCount,
350 					uint32* _vecIndex, size_t* _vecOffset, size_t* _bytes);
351 extern status_t write_file_io_vec_pages(int fd,
352 					const struct file_io_vec* fileVecs, size_t fileVecCount,
353 					const struct iovec* vecs, size_t vecCount,
354 					uint32* _vecIndex, size_t* _vecOffset, size_t* _bytes);
355 extern status_t do_fd_io(int fd, io_request* request);
356 extern status_t do_iterative_fd_io(int fd, io_request* request,
357 					iterative_io_get_vecs getVecs,
358 					iterative_io_finished finished, void* cookie);
359 
360 extern status_t notify_entry_created(dev_t device, ino_t directory,
361 					const char* name, ino_t node);
362 extern status_t notify_entry_removed(dev_t device, ino_t directory,
363 					const char* name, ino_t node);
364 extern status_t notify_entry_moved(dev_t device, ino_t fromDirectory,
365 					const char* fromName, ino_t toDirectory,
366 					const char* toName, ino_t node);
367 extern status_t notify_stat_changed(dev_t device, ino_t node,
368 					uint32 statFields);
369 extern status_t notify_attribute_changed(dev_t device, ino_t node,
370 					const char* attribute, int32 cause);
371 
372 extern status_t notify_query_entry_created(port_id port, int32 token,
373 					dev_t device, ino_t directory, const char* name,
374 					ino_t node);
375 extern status_t notify_query_entry_removed(port_id port, int32 token,
376 					dev_t device, ino_t directory, const char* name,
377 					ino_t node);
378 extern status_t notify_query_attr_changed(port_id port, int32 token,
379 					dev_t device, ino_t directory, const char* name,
380 					ino_t node);
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 
386 #endif	/* _FS_INTERFACE_H */
387