1/*
2 * Copyright 2004-2016, Haiku Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _FSSH_FS_INTERFACE_H
6#define _FSSH_FS_INTERFACE_H
7
8/*! File System Interface Layer Definition */
9
10
11#include "fssh_disk_device_defs.h"
12#include "fssh_module.h"
13#include "fssh_os.h"
14
15
16struct fssh_dirent;
17struct fssh_fs_info;
18struct fssh_iovec;
19struct fssh_partition_data;
20struct fssh_selectsync;
21struct fssh_stat;
22
23typedef fssh_dev_t fssh_mount_id;
24typedef fssh_ino_t fssh_vnode_id;
25
26/* the file system's private data structures */
27typedef void *fssh_fs_cookie;
28
29typedef struct FSSHIORequest fssh_io_request;
30
31/* additional flags passed to write_stat() */
32#define FSSH_B_STAT_SIZE_INSECURE	0x2000
33
34/* passed to write_fs_info() */
35#define	FSSH_FS_WRITE_FSINFO_NAME	0x0001
36
37struct fssh_file_io_vec {
38	fssh_off_t	offset;
39	fssh_off_t	length;
40};
41
42#define	FSSH_B_CURRENT_FS_API_VERSION "/v1"
43
44// flags for publish_vnode() and fs_volume_ops::get_vnode()
45#define FSSH_B_VNODE_PUBLISH_REMOVED				0x01
46#define FSSH_B_VNODE_DONT_CREATE_SPECIAL_SUB_NODE	0x02
47
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53typedef struct fssh_fs_volume fssh_fs_volume;
54typedef struct fssh_fs_volume_ops fssh_fs_volume_ops;
55typedef struct fssh_fs_vnode fssh_fs_vnode;
56typedef struct fssh_fs_vnode_ops fssh_fs_vnode_ops;
57
58
59struct fssh_fs_volume {
60	fssh_dev_t			id;
61	int32_t				layer;
62	void*				private_volume;
63	fssh_fs_volume_ops*	ops;
64	fssh_fs_volume*		sub_volume;
65	fssh_fs_volume*		super_volume;
66};
67
68struct fssh_fs_vnode {
69	void*				private_node;
70	fssh_fs_vnode_ops*	ops;
71};
72
73struct fssh_fs_volume_ops {
74	fssh_status_t (*unmount)(fssh_fs_volume *volume);
75
76	fssh_status_t (*read_fs_info)(fssh_fs_volume *volume,
77				struct fssh_fs_info *info);
78	fssh_status_t (*write_fs_info)(fssh_fs_volume *volume,
79				const struct fssh_fs_info *info, uint32_t mask);
80	fssh_status_t (*sync)(fssh_fs_volume *volume);
81
82	fssh_status_t (*get_vnode)(fssh_fs_volume *volume, fssh_vnode_id id,
83				fssh_fs_vnode *_vnode, int *_type, uint32_t *_flags,
84				bool reenter);
85
86	/* index directory & index operations */
87	fssh_status_t (*open_index_dir)(fssh_fs_volume *volume,
88				fssh_fs_cookie *cookie);
89	fssh_status_t (*close_index_dir)(fssh_fs_volume *volume,
90				fssh_fs_cookie cookie);
91	fssh_status_t (*free_index_dir_cookie)(fssh_fs_volume *volume,
92				fssh_fs_cookie cookie);
93	fssh_status_t (*read_index_dir)(fssh_fs_volume *volume,
94				fssh_fs_cookie cookie, struct fssh_dirent *buffer,
95				fssh_size_t bufferSize, uint32_t *_num);
96	fssh_status_t (*rewind_index_dir)(fssh_fs_volume *volume,
97				fssh_fs_cookie cookie);
98
99	fssh_status_t (*create_index)(fssh_fs_volume *volume, const char *name,
100				uint32_t type, uint32_t flags);
101	fssh_status_t (*remove_index)(fssh_fs_volume *volume, const char *name);
102	fssh_status_t (*read_index_stat)(fssh_fs_volume *volume, const char *name,
103				struct fssh_stat *stat);
104
105	/* query operations */
106	fssh_status_t (*open_query)(fssh_fs_volume *volume, const char *query,
107				uint32_t flags, fssh_port_id port, uint32_t token,
108				fssh_fs_cookie *_cookie);
109	fssh_status_t (*close_query)(fssh_fs_volume *volume, fssh_fs_cookie cookie);
110	fssh_status_t (*free_query_cookie)(fssh_fs_volume *volume,
111				fssh_fs_cookie cookie);
112	fssh_status_t (*read_query)(fssh_fs_volume *volume, fssh_fs_cookie cookie,
113				struct fssh_dirent *buffer, fssh_size_t bufferSize,
114				uint32_t *_num);
115	fssh_status_t (*rewind_query)(fssh_fs_volume *volume,
116				fssh_fs_cookie cookie);
117
118	/* support for FS layers */
119	fssh_status_t (*create_sub_vnode)(fssh_fs_volume *volume, fssh_ino_t id,
120		fssh_fs_vnode *vnode);
121	fssh_status_t (*delete_sub_vnode)(fssh_fs_volume *volume,
122				fssh_fs_vnode *vnode);
123};
124
125struct fssh_fs_vnode_ops {
126	/* vnode operations */
127	fssh_status_t (*lookup)(fssh_fs_volume *volume, fssh_fs_vnode *dir,
128				const char *name, fssh_vnode_id *_id);
129	fssh_status_t (*get_vnode_name)(fssh_fs_volume *volume,
130				fssh_fs_vnode *vnode, char *buffer, fssh_size_t bufferSize);
131
132	fssh_status_t (*put_vnode)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
133				bool reenter);
134	fssh_status_t (*remove_vnode)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
135				bool reenter);
136
137	/* VM file access */
138	bool (*can_page)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
139				fssh_fs_cookie cookie);
140	fssh_status_t (*read_pages)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
141				fssh_fs_cookie cookie, fssh_off_t pos, const fssh_iovec *vecs,
142				fssh_size_t count, fssh_size_t *_numBytes);
143	fssh_status_t (*write_pages)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
144				fssh_fs_cookie cookie, fssh_off_t pos, const fssh_iovec *vecs,
145				fssh_size_t count, fssh_size_t *_numBytes);
146
147	/* asynchronous I/O */
148	fssh_status_t (*io)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
149				void *cookie, fssh_io_request *request);
150	fssh_status_t (*cancel_io)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
151				void *cookie, fssh_io_request *request);
152
153	/* cache file access */
154	fssh_status_t (*get_file_map)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
155				fssh_off_t offset, fssh_size_t size,
156				struct fssh_file_io_vec *vecs, fssh_size_t *_count);
157
158	/* common operations */
159	fssh_status_t (*ioctl)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
160				fssh_fs_cookie cookie, uint32_t op, void *buffer,
161				fssh_size_t length);
162	fssh_status_t (*set_flags)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
163				fssh_fs_cookie cookie, int flags);
164	fssh_status_t (*select)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
165				fssh_fs_cookie cookie, uint8_t event, fssh_selectsync *sync);
166	fssh_status_t (*deselect)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
167				fssh_fs_cookie cookie, uint8_t event, fssh_selectsync *sync);
168	fssh_status_t (*fsync)(fssh_fs_volume *volume, fssh_fs_vnode *vnode);
169
170	fssh_status_t (*read_symlink)(fssh_fs_volume *volume, fssh_fs_vnode *link,
171				char *buffer, fssh_size_t *_bufferSize);
172	fssh_status_t (*create_symlink)(fssh_fs_volume *volume, fssh_fs_vnode *dir,
173				const char *name, const char *path, int mode);
174
175	fssh_status_t (*link)(fssh_fs_volume *volume, fssh_fs_vnode *dir,
176				const char *name, fssh_fs_vnode *vnode);
177	fssh_status_t (*unlink)(fssh_fs_volume *volume, fssh_fs_vnode *dir,
178				const char *name);
179	fssh_status_t (*rename)(fssh_fs_volume *volume, fssh_fs_vnode *fromDir,
180				const char *fromName, fssh_fs_vnode *toDir, const char *toName);
181
182	fssh_status_t (*access)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
183				int mode);
184	fssh_status_t (*read_stat)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
185				struct fssh_stat *stat);
186	fssh_status_t (*write_stat)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
187				const struct fssh_stat *stat, uint32_t statMask);
188	fssh_status_t (*preallocate)(fssh_fs_volume* volume, fssh_fs_vnode* vnode,
189				fssh_off_t pos, fssh_off_t length);
190
191	/* file operations */
192	fssh_status_t (*create)(fssh_fs_volume *volume, fssh_fs_vnode *dir,
193				const char *name, int openMode, int perms,
194				fssh_fs_cookie *_cookie, fssh_vnode_id *_newVnodeID);
195	fssh_status_t (*open)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
196				int openMode, fssh_fs_cookie *_cookie);
197	fssh_status_t (*close)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
198				fssh_fs_cookie cookie);
199	fssh_status_t (*free_cookie)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
200				fssh_fs_cookie cookie);
201	fssh_status_t (*read)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
202				fssh_fs_cookie cookie, fssh_off_t pos, void *buffer,
203				fssh_size_t *length);
204	fssh_status_t (*write)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
205				fssh_fs_cookie cookie, fssh_off_t pos, const void *buffer,
206				fssh_size_t *length);
207
208	/* directory operations */
209	fssh_status_t (*create_dir)(fssh_fs_volume *volume, fssh_fs_vnode *parent,
210				const char *name, int perms);
211	fssh_status_t (*remove_dir)(fssh_fs_volume *volume, fssh_fs_vnode *parent,
212				const char *name);
213	fssh_status_t (*open_dir)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
214				fssh_fs_cookie *_cookie);
215	fssh_status_t (*close_dir)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
216				fssh_fs_cookie cookie);
217	fssh_status_t (*free_dir_cookie)(fssh_fs_volume *volume,
218				fssh_fs_vnode *vnode, fssh_fs_cookie cookie);
219	fssh_status_t (*read_dir)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
220				fssh_fs_cookie cookie, struct fssh_dirent *buffer,
221				fssh_size_t bufferSize, uint32_t *_num);
222	fssh_status_t (*rewind_dir)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
223				fssh_fs_cookie cookie);
224
225	/* attribute directory operations */
226	fssh_status_t (*open_attr_dir)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
227				fssh_fs_cookie *_cookie);
228	fssh_status_t (*close_attr_dir)(fssh_fs_volume *volume,
229				fssh_fs_vnode *vnode, fssh_fs_cookie cookie);
230	fssh_status_t (*free_attr_dir_cookie)(fssh_fs_volume *volume,
231				fssh_fs_vnode *vnode, fssh_fs_cookie cookie);
232	fssh_status_t (*read_attr_dir)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
233				fssh_fs_cookie cookie, struct fssh_dirent *buffer,
234				fssh_size_t bufferSize, uint32_t *_num);
235	fssh_status_t (*rewind_attr_dir)(fssh_fs_volume *volume,
236				fssh_fs_vnode *vnode, fssh_fs_cookie cookie);
237
238	/* attribute operations */
239	fssh_status_t (*create_attr)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
240				const char *name, uint32_t type, int openMode,
241				fssh_fs_cookie *_cookie);
242	fssh_status_t (*open_attr)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
243				const char *name, int openMode, fssh_fs_cookie *_cookie);
244	fssh_status_t (*close_attr)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
245				fssh_fs_cookie cookie);
246	fssh_status_t (*free_attr_cookie)(fssh_fs_volume *volume,
247				fssh_fs_vnode *vnode, fssh_fs_cookie cookie);
248	fssh_status_t (*read_attr)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
249				fssh_fs_cookie cookie, fssh_off_t pos, void *buffer,
250				fssh_size_t *length);
251	fssh_status_t (*write_attr)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
252				fssh_fs_cookie cookie, fssh_off_t pos, const void *buffer,
253				fssh_size_t *length);
254
255	fssh_status_t (*read_attr_stat)(fssh_fs_volume *volume,
256				fssh_fs_vnode *vnode, fssh_fs_cookie cookie,
257				struct fssh_stat *stat);
258	fssh_status_t (*write_attr_stat)(fssh_fs_volume *volume,
259				fssh_fs_vnode *vnode, fssh_fs_cookie cookie,
260				const struct fssh_stat *stat, int statMask);
261	fssh_status_t (*rename_attr)(fssh_fs_volume *volume,
262				fssh_fs_vnode *fromVnode, const char *fromName,
263				fssh_fs_vnode *toVnode, const char *toName);
264	fssh_status_t (*remove_attr)(fssh_fs_volume *volume, fssh_fs_vnode *vnode,
265				const char *name);
266
267	/* support for vnode and FS layers */
268	fssh_status_t (*create_special_node)(fssh_fs_volume *volume,
269				fssh_fs_vnode *dir, const char *name, fssh_fs_vnode *subVnode,
270				fssh_mode_t mode, uint32_t flags, fssh_fs_vnode *_superVnode,
271				fssh_ino_t *_nodeID);
272	fssh_status_t (*get_super_vnode)(fssh_fs_volume *volume,
273				fssh_fs_vnode *vnode, fssh_fs_volume *superVolume,
274				fssh_fs_vnode *superVnode);
275};
276
277typedef struct fssh_file_system_module_info {
278	struct fssh_module_info	info;
279	const char*				short_name;
280	const char*				pretty_name;
281	uint32_t				flags;	// DDM flags
282
283	/* scanning (the device is write locked) */
284	float (*identify_partition)(int fd, fssh_partition_data *partition,
285				void **cookie);
286	fssh_status_t (*scan_partition)(int fd, fssh_partition_data *partition,
287				void *cookie);
288	void (*free_identify_partition_cookie)(fssh_partition_data *partition,
289				void *cookie);
290	void (*free_partition_content_cookie)(fssh_partition_data *partition);
291
292	/* general operations */
293	fssh_status_t (*mount)(fssh_fs_volume *volume, const char *device,
294				uint32_t flags, const char *args, fssh_vnode_id *_rootVnodeID);
295
296	/* capability querying (the device is read locked) */
297	uint32_t (*get_supported_operations)(fssh_partition_data* partition,
298				uint32_t mask);
299
300	bool (*validate_resize)(fssh_partition_data *partition, fssh_off_t *size);
301	bool (*validate_move)(fssh_partition_data *partition, fssh_off_t *start);
302	bool (*validate_set_content_name)(fssh_partition_data *partition,
303				char *name);
304	bool (*validate_set_content_parameters)(fssh_partition_data *partition,
305				const char *parameters);
306	bool (*validate_initialize)(fssh_partition_data *partition, char *name,
307				const char *parameters);
308
309	/* shadow partition modification (device is write locked) */
310	fssh_status_t (*shadow_changed)(fssh_partition_data *partition,
311				fssh_partition_data *child, uint32_t operation);
312
313	/* writing (the device is NOT locked) */
314	fssh_status_t (*defragment)(int fd, fssh_partition_id partition,
315				fssh_disk_job_id job);
316	fssh_status_t (*repair)(int fd, fssh_partition_id partition, bool checkOnly,
317				fssh_disk_job_id job);
318	fssh_status_t (*resize)(int fd, fssh_partition_id partition,
319				fssh_off_t size, fssh_disk_job_id job);
320	fssh_status_t (*move)(int fd, fssh_partition_id partition,
321				fssh_off_t offset, fssh_disk_job_id job);
322	fssh_status_t (*set_content_name)(int fd, fssh_partition_id partition,
323				const char *name, fssh_disk_job_id job);
324	fssh_status_t (*set_content_parameters)(int fd, fssh_partition_id partition,
325				const char *parameters, fssh_disk_job_id job);
326	fssh_status_t (*initialize)(int fd, fssh_partition_id partition,
327				const char *name, const char *parameters,
328				fssh_off_t partitionSize, fssh_disk_job_id job);
329	fssh_status_t (*uninitialize)(int fd, fssh_partition_id partition,
330				fssh_off_t partitionSize, uint32_t blockSize,
331				fssh_disk_job_id job);
332} fssh_file_system_module_info;
333
334
335/* file system add-ons only prototypes */
336
337// callbacks for do_iterative_fd_io()
338typedef fssh_status_t (*fssh_iterative_io_get_vecs)(void *cookie,
339				fssh_io_request* request, fssh_off_t offset, fssh_size_t size,
340				struct fssh_file_io_vec *vecs, fssh_size_t *_count);
341typedef fssh_status_t (*fssh_iterative_io_finished)(void* cookie,
342				fssh_io_request* request, fssh_status_t status,
343				bool partialTransfer, fssh_size_t bytesTransferred);
344
345extern fssh_status_t fssh_new_vnode(fssh_fs_volume *volume,
346				fssh_vnode_id vnodeID, void *privateNode,
347				fssh_fs_vnode_ops *ops);
348extern fssh_status_t fssh_publish_vnode(fssh_fs_volume *volume,
349				fssh_vnode_id vnodeID, void *privateNode,
350				fssh_fs_vnode_ops *ops, int type, uint32_t flags);
351extern fssh_status_t fssh_get_vnode(fssh_fs_volume *volume,
352				fssh_vnode_id vnodeID, void **_privateNode);
353extern fssh_status_t fssh_put_vnode(fssh_fs_volume *volume,
354				fssh_vnode_id vnodeID);
355extern fssh_status_t fssh_acquire_vnode(fssh_fs_volume *volume,
356				fssh_vnode_id vnodeID);
357extern fssh_status_t fssh_remove_vnode(fssh_fs_volume *volume,
358				fssh_vnode_id vnodeID);
359extern fssh_status_t fssh_unremove_vnode(fssh_fs_volume *volume,
360				fssh_vnode_id vnodeID);
361extern fssh_status_t fssh_get_vnode_removed(fssh_fs_volume *volume,
362				fssh_vnode_id vnodeID, bool* removed);
363extern fssh_fs_volume* fssh_volume_for_vnode(fssh_fs_vnode *vnode);
364extern fssh_status_t fssh_check_access_permissions(int accessMode,
365				fssh_mode_t mode, fssh_gid_t nodeGroupID,
366				fssh_uid_t nodeUserID);
367
368extern fssh_status_t fssh_read_pages(int fd, fssh_off_t pos,
369				const struct fssh_iovec *vecs, fssh_size_t count,
370				fssh_size_t *_numBytes);
371extern fssh_status_t fssh_write_pages(int fd, fssh_off_t pos,
372				const struct fssh_iovec *vecs, fssh_size_t count,
373				fssh_size_t *_numBytes);
374extern fssh_status_t fssh_read_file_io_vec_pages(int fd,
375				const struct fssh_file_io_vec *fileVecs,
376				fssh_size_t fileVecCount, const struct fssh_iovec *vecs,
377				fssh_size_t vecCount, uint32_t *_vecIndex,
378				fssh_size_t *_vecOffset, fssh_size_t *_bytes);
379extern fssh_status_t fssh_write_file_io_vec_pages(int fd,
380				const struct fssh_file_io_vec *fileVecs,
381				fssh_size_t fileVecCount, const struct fssh_iovec *vecs,
382				fssh_size_t vecCount, uint32_t *_vecIndex,
383				fssh_size_t *_vecOffset, fssh_size_t *_bytes);
384extern fssh_status_t fssh_do_fd_io(int fd, fssh_io_request *request);
385extern fssh_status_t fssh_do_iterative_fd_io(int fd, fssh_io_request *request,
386				fssh_iterative_io_get_vecs getVecs,
387				fssh_iterative_io_finished finished, void *cookie);
388
389extern fssh_status_t fssh_notify_entry_created(fssh_mount_id device,
390				fssh_vnode_id directory, const char *name, fssh_vnode_id node);
391extern fssh_status_t fssh_notify_entry_removed(fssh_mount_id device,
392				fssh_vnode_id directory, const char *name, fssh_vnode_id node);
393extern fssh_status_t fssh_notify_entry_moved(fssh_mount_id device,
394				fssh_vnode_id fromDirectory, const char *fromName,
395				fssh_vnode_id toDirectory, const char *toName,
396				fssh_vnode_id node);
397extern fssh_status_t fssh_notify_stat_changed(fssh_mount_id device,
398				fssh_vnode_id dir, fssh_vnode_id node, uint32_t statFields);
399extern fssh_status_t fssh_notify_attribute_changed(fssh_mount_id device,
400				fssh_vnode_id dir, fssh_vnode_id node, const char *attribute,
401				int32_t cause);
402
403extern fssh_status_t fssh_notify_query_entry_created(fssh_port_id port,
404				int32_t token, fssh_mount_id device,
405				fssh_vnode_id directory, const char *name,
406				fssh_vnode_id node);
407extern fssh_status_t fssh_notify_query_entry_removed(fssh_port_id port,
408				int32_t token, fssh_mount_id device,
409				fssh_vnode_id directory, const char *name,
410				fssh_vnode_id node);
411extern fssh_status_t fssh_notify_query_attr_changed(fssh_port_id port,
412				int32_t token, fssh_mount_id device,
413				fssh_vnode_id directory, const char *name,
414				fssh_vnode_id node);
415
416#ifdef __cplusplus
417}
418#endif
419
420#endif	/* _FSSH_FS_INTERFACE_H */
421