]> code.delx.au - refind/blob - filesystems/fsw_core.h
Fix drivers to work on Macs.
[refind] / filesystems / fsw_core.h
1 /* $Id: fsw_core.h 33540 2010-10-28 09:27:05Z vboxsync $ */
2 /** @file
3 * fsw_core.h - Core file system wrapper abstraction layer header.
4 */
5
6 /*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18 /*-
19 * This code is based on:
20 *
21 * Copyright (c) 2006 Christoph Pfisterer
22 * Portions Copyright (c) The Regents of the University of California.
23 * Portions Copyright (c) UNIX System Laboratories, Inc.
24 *
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions are
27 * met:
28 *
29 * * Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 *
32 * * Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the
35 * distribution.
36 *
37 * * Neither the name of Christoph Pfisterer nor the names of the
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
45 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 #ifndef _FSW_CORE_H_
55 #define _FSW_CORE_H_
56
57 #include "fsw_base.h"
58
59
60 /** Maximum size for a path, specifically symlink target paths. */
61 #ifndef VBOX
62 #define FSW_PATH_MAX (4096)
63 #else
64 /* Too big allocations are handled with alloca() */
65 #define FSW_PATH_MAX (2048)
66 #endif
67
68 /** Helper macro for token concatenation. */
69 #define FSW_CONCAT3(a,b,c) a##b##c
70 /** Expands to the name of a fstype dispatch table (fsw_fstype_table) for a named file system type. */
71 #define FSW_FSTYPE_TABLE_NAME(t) FSW_CONCAT3(fsw_,t,_table)
72
73 /** Indicates that the block cache entry is empty. */
74 #define FSW_INVALID_BNO (~0UL)
75
76
77 //
78 // Byte-swapping macros
79 //
80
81
82 /**
83 * \name Byte Order Macros
84 * Implements big endian vs. little endian awareness and conversion.
85 */
86 /*@{*/
87
88 typedef fsw_u16 fsw_u16_le;
89 typedef fsw_u16 fsw_u16_be;
90 typedef fsw_u32 fsw_u32_le;
91 typedef fsw_u32 fsw_u32_be;
92 typedef fsw_u64 fsw_u64_le;
93 typedef fsw_u64 fsw_u64_be;
94
95 #define FSW_SWAPVALUE_U16(v) ((((fsw_u16)(v) & 0xff00) >> 8) | \
96 (((fsw_u16)(v) & 0x00ff) << 8))
97 #define FSW_SWAPVALUE_U32(v) ((((fsw_u32)(v) & 0xff000000UL) >> 24) | \
98 (((fsw_u32)(v) & 0x00ff0000UL) >> 8) | \
99 (((fsw_u32)(v) & 0x0000ff00UL) << 8) | \
100 (((fsw_u32)(v) & 0x000000ffUL) << 24))
101 #define FSW_SWAPVALUE_U64(v) ((((fsw_u64)(v) & 0xff00000000000000ULL) >> 56) | \
102 (((fsw_u64)(v) & 0x00ff000000000000ULL) >> 40) | \
103 (((fsw_u64)(v) & 0x0000ff0000000000ULL) >> 24) | \
104 (((fsw_u64)(v) & 0x000000ff00000000ULL) >> 8) | \
105 (((fsw_u64)(v) & 0x00000000ff000000ULL) << 8) | \
106 (((fsw_u64)(v) & 0x0000000000ff0000ULL) << 24) | \
107 (((fsw_u64)(v) & 0x000000000000ff00ULL) << 40) | \
108 (((fsw_u64)(v) & 0x00000000000000ffULL) << 56))
109
110 #ifdef FSW_LITTLE_ENDIAN
111
112 #define fsw_u16_le_swap(v) (v)
113 #define fsw_u16_be_swap(v) FSW_SWAPVALUE_U16(v)
114 #define fsw_u32_le_swap(v) (v)
115 #define fsw_u32_be_swap(v) FSW_SWAPVALUE_U32(v)
116 #define fsw_u64_le_swap(v) (v)
117 #define fsw_u64_be_swap(v) FSW_SWAPVALUE_U64(v)
118
119 #define fsw_u16_le_sip(var)
120 #define fsw_u16_be_sip(var) (var = FSW_SWAPVALUE_U16(var))
121 #define fsw_u32_le_sip(var)
122 #define fsw_u32_be_sip(var) (var = FSW_SWAPVALUE_U32(var))
123 #define fsw_u64_le_sip(var)
124 #define fsw_u64_be_sip(var) (var = FSW_SWAPVALUE_U64(var))
125
126 #else
127 #ifdef FSW_BIG_ENDIAN
128
129 #define fsw_u16_le_swap(v) FSW_SWAPVALUE_U16(v)
130 #define fsw_u16_be_swap(v) (v)
131 #define fsw_u32_le_swap(v) FSW_SWAPVALUE_U32(v)
132 #define fsw_u32_be_swap(v) (v)
133 #define fsw_u64_le_swap(v) FSW_SWAPVALUE_U64(v)
134 #define fsw_u64_be_swap(v) (v)
135
136 #define fsw_u16_le_sip(var) (var = FSW_SWAPVALUE_U16(var))
137 #define fsw_u16_be_sip(var)
138 #define fsw_u32_le_sip(var) (var = FSW_SWAPVALUE_U32(var))
139 #define fsw_u32_be_sip(var)
140 #define fsw_u64_le_sip(var) (var = FSW_SWAPVALUE_U64(var))
141 #define fsw_u64_be_sip(var)
142
143 #else
144 #fail Neither FSW_BIG_ENDIAN nor FSW_LITTLE_ENDIAN are defined
145 #endif
146 #endif
147
148 /*@}*/
149
150
151 //
152 // The following evil hack avoids a lot of casts between generic and fstype-specific
153 // structures.
154 //
155
156 #ifndef VOLSTRUCTNAME
157 #define VOLSTRUCTNAME fsw_volume
158 #else
159 struct VOLSTRUCTNAME;
160 #endif
161 #ifndef DNODESTRUCTNAME
162 #define DNODESTRUCTNAME fsw_dnode
163 #else
164 struct DNODESTRUCTNAME;
165 #endif
166
167
168 /**
169 * Status code type, returned from all functions that can fail.
170 */
171 typedef int fsw_status_t;
172
173 /**
174 * Possible status codes.
175 */
176 enum {
177 FSW_SUCCESS,
178 FSW_OUT_OF_MEMORY,
179 FSW_IO_ERROR,
180 FSW_UNSUPPORTED,
181 FSW_NOT_FOUND,
182 FSW_VOLUME_CORRUPTED,
183 FSW_UNKNOWN_ERROR
184 };
185
186
187 /**
188 * Core: A string with explicit length and encoding information.
189 */
190
191 struct fsw_string {
192 int type; //!< Encoding of the string - empty, ISO-8859-1, UTF8, UTF16
193 int len; //!< Length in characters
194 int size; //!< Total data size in bytes
195 void *data; //!< Data pointer (may be NULL if type is EMPTY or len is zero)
196 };
197
198 /**
199 * Possible string types / encodings. In the case of FSW_STRING_TYPE_EMPTY,
200 * all other members of the fsw_string structure may be invalid.
201 */
202 enum {
203 FSW_STRING_TYPE_EMPTY,
204 FSW_STRING_TYPE_ISO88591,
205 FSW_STRING_TYPE_UTF8,
206 FSW_STRING_TYPE_UTF16,
207 FSW_STRING_TYPE_UTF16_SWAPPED
208 };
209
210 #ifdef FSW_LITTLE_ENDIAN
211 #define FSW_STRING_TYPE_UTF16_LE FSW_STRING_TYPE_UTF16
212 #define FSW_STRING_TYPE_UTF16_BE FSW_STRING_TYPE_UTF16_SWAPPED
213 #else
214 #define FSW_STRING_TYPE_UTF16_LE FSW_STRING_TYPE_UTF16_SWAPPED
215 #define FSW_STRING_TYPE_UTF16_BE FSW_STRING_TYPE_UTF16
216 #endif
217
218 /** Static initializer for an empty string. */
219 #define FSW_STRING_INIT { FSW_STRING_TYPE_EMPTY, 0, 0, NULL }
220
221
222 /* forward declarations */
223
224 struct fsw_dnode;
225 struct fsw_host_table;
226 struct fsw_fstype_table;
227
228 struct fsw_blockcache {
229 fsw_u32 refcount; //!< Reference count
230 fsw_u32 cache_level; //!< Level of importance of this block
231 fsw_u32 phys_bno; //!< Physical block number
232 void *data; //!< Block data buffer
233 };
234
235 /**
236 * Core: Represents a mounted volume.
237 */
238
239 struct fsw_volume {
240 fsw_u32 phys_blocksize; //!< Block size for disk access / file system structures
241 fsw_u32 log_blocksize; //!< Block size for logical file data
242
243 struct DNODESTRUCTNAME *root; //!< Root directory dnode
244 struct fsw_string label; //!< Volume label
245
246 struct fsw_dnode *dnode_head; //!< List of all dnodes allocated for this volume
247
248 struct fsw_blockcache *bcache; //!< Array of block cache entries
249 fsw_u32 bcache_size; //!< Number of entries in the block cache array
250
251 void *host_data; //!< Hook for a host-specific data structure
252 struct fsw_host_table *host_table; //!< Dispatch table for host-specific functions
253 struct fsw_fstype_table *fstype_table; //!< Dispatch table for file system specific functions
254 int host_string_type; //!< String type used by the host environment
255 };
256
257 /**
258 * Core: Represents a "directory node" - a file, directory, symlink, whatever.
259 */
260
261 struct fsw_dnode {
262 fsw_u32 refcount; //!< Reference count
263
264 struct VOLSTRUCTNAME *vol; //!< The volume this dnode belongs to
265 struct DNODESTRUCTNAME *parent; //!< Parent directory dnode
266 struct fsw_string name; //!< Name of this item in the parent directory
267
268 fsw_u32 dnode_id; //!< Unique id number (usually the inode number)
269 int type; //!< Type of the dnode - file, dir, symlink, special
270 fsw_u64 size; //!< Data size in bytes
271
272 struct fsw_dnode *next; //!< Doubly-linked list of all dnodes: previous dnode
273 struct fsw_dnode *prev; //!< Doubly-linked list of all dnodes: next dnode
274 };
275
276 /**
277 * Possible dnode types. FSW_DNODE_TYPE_UNKNOWN may only be used before
278 * fsw_dnode_fill has been called on the dnode.
279 */
280 enum {
281 FSW_DNODE_TYPE_UNKNOWN,
282 FSW_DNODE_TYPE_FILE,
283 FSW_DNODE_TYPE_DIR,
284 FSW_DNODE_TYPE_SYMLINK,
285 FSW_DNODE_TYPE_SPECIAL
286 };
287
288 /**
289 * Core: Stores the mapping of a region of a file to the data on disk.
290 */
291
292 struct fsw_extent {
293 fsw_u32 type; //!< Type of extent specification
294 fsw_u32 log_start; //!< Starting logical block number
295 fsw_u32 log_count; //!< Logical block count
296 fsw_u32 phys_start; //!< Starting physical block number (for FSW_EXTENT_TYPE_PHYSBLOCK only)
297 void *buffer; //!< Allocated buffer pointer (for FSW_EXTENT_TYPE_BUFFER only)
298 };
299
300 /**
301 * Possible extent representation types. FSW_EXTENT_TYPE_INVALID is for shandle's
302 * internal use only, it must not be returned from a get_extent function.
303 */
304 enum {
305 FSW_EXTENT_TYPE_INVALID,
306 FSW_EXTENT_TYPE_SPARSE,
307 FSW_EXTENT_TYPE_PHYSBLOCK,
308 FSW_EXTENT_TYPE_BUFFER
309 };
310
311 /**
312 * Core: An access structure to a dnode's raw data. There can be multiple
313 * shandles per dnode, each of them has its own position pointer.
314 */
315
316 struct fsw_shandle {
317 struct fsw_dnode *dnode; //!< The dnode this handle reads data from
318
319 fsw_u64 pos; //!< Current file pointer in bytes
320 struct fsw_extent extent; //!< Current extent
321 };
322
323 /**
324 * Core: Used in gathering detailed information on a volume.
325 */
326
327 struct fsw_volume_stat {
328 fsw_u64 total_bytes; //!< Total size of data area size in bytes
329 fsw_u64 free_bytes; //!< Bytes still available for storing file data
330 };
331
332 /**
333 * Core: Used in gathering detailed information on a dnode.
334 */
335
336 struct fsw_dnode_stat {
337 fsw_u64 used_bytes; //!< Bytes actually used by the file on disk
338 void (*store_time_posix)(struct fsw_dnode_stat *sb, int which, fsw_u32 posix_time); //!< Callback for storing a Posix-style timestamp
339 void (*store_attr_posix)(struct fsw_dnode_stat *sb, fsw_u16 posix_mode); //!< Callback for storing a Posix-style file mode
340 void *host_data; //!< Hook for a host-specific data structure
341 };
342
343 /**
344 * Type of the timestamp passed into store_time_posix.
345 */
346 enum {
347 FSW_DNODE_STAT_CTIME,
348 FSW_DNODE_STAT_MTIME,
349 FSW_DNODE_STAT_ATIME
350 };
351
352 /**
353 * Core: Function table for a host environment.
354 */
355
356 struct fsw_host_table
357 {
358 int native_string_type; //!< String type used by the host environment
359
360 void (*change_blocksize)(struct fsw_volume *vol,
361 fsw_u32 old_phys_blocksize, fsw_u32 old_log_blocksize,
362 fsw_u32 new_phys_blocksize, fsw_u32 new_log_blocksize);
363 fsw_status_t (*read_block)(struct fsw_volume *vol, fsw_u32 phys_bno, void *buffer);
364 };
365
366 /**
367 * Core: Function table for a file system driver.
368 */
369
370 struct fsw_fstype_table
371 {
372 struct fsw_string name; //!< String giving the name of the file system
373 fsw_u32 volume_struct_size; //!< Size for allocating the fsw_volume structure
374 fsw_u32 dnode_struct_size; //!< Size for allocating the fsw_dnode structure
375
376 fsw_status_t (*volume_mount)(struct VOLSTRUCTNAME *vol);
377 void (*volume_free)(struct VOLSTRUCTNAME *vol);
378 fsw_status_t (*volume_stat)(struct VOLSTRUCTNAME *vol, struct fsw_volume_stat *sb);
379
380 fsw_status_t (*dnode_fill)(struct VOLSTRUCTNAME *vol, struct DNODESTRUCTNAME *dno);
381 void (*dnode_free)(struct VOLSTRUCTNAME *vol, struct DNODESTRUCTNAME *dno);
382 fsw_status_t (*dnode_stat)(struct VOLSTRUCTNAME *vol, struct DNODESTRUCTNAME *dno,
383 struct fsw_dnode_stat *sb);
384 fsw_status_t (*get_extent)(struct VOLSTRUCTNAME *vol, struct DNODESTRUCTNAME *dno,
385 struct fsw_extent *extent);
386
387 fsw_status_t (*dir_lookup)(struct VOLSTRUCTNAME *vol, struct DNODESTRUCTNAME *dno,
388 struct fsw_string *lookup_name, struct DNODESTRUCTNAME **child_dno);
389 fsw_status_t (*dir_read)(struct VOLSTRUCTNAME *vol, struct DNODESTRUCTNAME *dno,
390 struct fsw_shandle *shand, struct DNODESTRUCTNAME **child_dno);
391 fsw_status_t (*readlink)(struct VOLSTRUCTNAME *vol, struct DNODESTRUCTNAME *dno,
392 struct fsw_string *link_target);
393 };
394
395
396 /**
397 * \name Volume Functions
398 */
399 /*@{*/
400
401 fsw_status_t fsw_mount(void *host_data,
402 struct fsw_host_table *host_table,
403 struct fsw_fstype_table *fstype_table,
404 struct fsw_volume **vol_out);
405 void fsw_unmount(struct fsw_volume *vol);
406 fsw_status_t fsw_volume_stat(struct fsw_volume *vol, struct fsw_volume_stat *sb);
407
408 void fsw_set_blocksize(struct VOLSTRUCTNAME *vol, fsw_u32 phys_blocksize, fsw_u32 log_blocksize);
409 fsw_status_t fsw_block_get(struct VOLSTRUCTNAME *vol, fsw_u32 phys_bno, fsw_u32 cache_level, void **buffer_out);
410 void fsw_block_release(struct VOLSTRUCTNAME *vol, fsw_u32 phys_bno, void *buffer);
411
412 /*@}*/
413
414
415 /**
416 * \name dnode Functions
417 */
418 /*@{*/
419
420 fsw_status_t fsw_dnode_create_root(struct VOLSTRUCTNAME *vol, fsw_u32 dnode_id, struct DNODESTRUCTNAME **dno_out);
421 fsw_status_t fsw_dnode_create(struct DNODESTRUCTNAME *parent_dno, fsw_u32 dnode_id, int type,
422 struct fsw_string *name, struct DNODESTRUCTNAME **dno_out);
423 void fsw_dnode_retain(struct fsw_dnode *dno);
424 void fsw_dnode_release(struct fsw_dnode *dno);
425
426 fsw_status_t fsw_dnode_fill(struct fsw_dnode *dno);
427 fsw_status_t fsw_dnode_stat(struct fsw_dnode *dno, struct fsw_dnode_stat *sb);
428
429 fsw_status_t fsw_dnode_lookup(struct fsw_dnode *dno,
430 struct fsw_string *lookup_name, struct fsw_dnode **child_dno_out);
431 fsw_status_t fsw_dnode_lookup_path(struct fsw_dnode *dno,
432 struct fsw_string *lookup_path, char separator,
433 struct fsw_dnode **child_dno_out);
434 fsw_status_t fsw_dnode_dir_read(struct fsw_shandle *shand, struct fsw_dnode **child_dno_out);
435 fsw_status_t fsw_dnode_readlink(struct fsw_dnode *dno, struct fsw_string *link_target);
436 fsw_status_t fsw_dnode_readlink_data(struct DNODESTRUCTNAME *dno, struct fsw_string *link_target);
437 fsw_status_t fsw_dnode_resolve(struct fsw_dnode *dno, struct fsw_dnode **target_dno_out);
438
439 /*@}*/
440
441
442 /**
443 * \name shandle Functions
444 */
445 /*@{*/
446
447 fsw_status_t fsw_shandle_open(struct DNODESTRUCTNAME *dno, struct fsw_shandle *shand);
448 void fsw_shandle_close(struct fsw_shandle *shand);
449 fsw_status_t fsw_shandle_read(struct fsw_shandle *shand, fsw_u32 *buffer_size_inout, void *buffer);
450
451 /*@}*/
452
453
454 /**
455 * \name Memory Functions
456 */
457 /*@{*/
458
459 fsw_status_t fsw_alloc_zero(int len, void **ptr_out);
460 fsw_status_t fsw_memdup(void **dest_out, void *src, int len);
461
462 /*@}*/
463
464
465 /**
466 * \name String Functions
467 */
468 /*@{*/
469
470 int fsw_strlen(struct fsw_string *s);
471 int fsw_streq(struct fsw_string *s1, struct fsw_string *s2);
472 int fsw_streq_cstr(struct fsw_string *s1, const char *s2);
473 fsw_status_t fsw_strdup_coerce(struct fsw_string *dest, int type, struct fsw_string *src);
474 void fsw_strsplit(struct fsw_string *lookup_name, struct fsw_string *buffer, char separator);
475
476 void fsw_strfree(struct fsw_string *s);
477 fsw_u16 fsw_to_lower(fsw_u16 ch);
478
479 /*@}*/
480
481 /**
482 * \name Posix Mode Macros
483 * These macros can be used globally to test fields and bits in
484 * Posix-style modes.
485 *
486 * Taken from FreeBSD sys/stat.h.
487 */
488 /*@{*/
489 #ifndef S_IRWXU
490
491 #define S_ISUID 0004000 /* set user id on execution */
492 #define S_ISGID 0002000 /* set group id on execution */
493 #define S_ISTXT 0001000 /* sticky bit */
494
495 #define S_IRWXU 0000700 /* RWX mask for owner */
496 #define S_IRUSR 0000400 /* R for owner */
497 #define S_IWUSR 0000200 /* W for owner */
498 #define S_IXUSR 0000100 /* X for owner */
499
500 #define S_IRWXG 0000070 /* RWX mask for group */
501 #define S_IRGRP 0000040 /* R for group */
502 #define S_IWGRP 0000020 /* W for group */
503 #define S_IXGRP 0000010 /* X for group */
504
505 #define S_IRWXO 0000007 /* RWX mask for other */
506 #define S_IROTH 0000004 /* R for other */
507 #define S_IWOTH 0000002 /* W for other */
508 #define S_IXOTH 0000001 /* X for other */
509
510 #define S_IFMT 0170000 /* type of file mask */
511 #define S_IFIFO 0010000 /* named pipe (fifo) */
512 #define S_IFCHR 0020000 /* character special */
513 #define S_IFDIR 0040000 /* directory */
514 #define S_IFBLK 0060000 /* block special */
515 #define S_IFREG 0100000 /* regular */
516 #define S_IFLNK 0120000 /* symbolic link */
517 #define S_IFSOCK 0140000 /* socket */
518 #define S_ISVTX 0001000 /* save swapped text even after use */
519 #define S_IFWHT 0160000 /* whiteout */
520
521 #define S_ISDIR(m) (((m) & 0170000) == 0040000) /* directory */
522 #define S_ISCHR(m) (((m) & 0170000) == 0020000) /* char special */
523 #define S_ISBLK(m) (((m) & 0170000) == 0060000) /* block special */
524 #define S_ISREG(m) (((m) & 0170000) == 0100000) /* regular file */
525 #define S_ISFIFO(m) (((m) & 0170000) == 0010000) /* fifo or socket */
526 #define S_ISLNK(m) (((m) & 0170000) == 0120000) /* symbolic link */
527 #define S_ISSOCK(m) (((m) & 0170000) == 0140000) /* socket */
528 #define S_ISWHT(m) (((m) & 0170000) == 0160000) /* whiteout */
529
530 #define S_BLKSIZE 512 /* block size used in the stat struct */
531
532 #endif
533 /*@}*/
534
535
536 #endif