2 * Copyright (c) 2010-2012 Semihalf
3 * Copyright (c) 2008, 2009 Reinoud Zandijk
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Original definitions written by Koji Sato <koji@osrg.net>
27 * and Ryusuke Konishi <ryusuke@osrg.net>
28 * From: NetBSD: nandfs_fs.h,v 1.1 2009/07/18 16:31:42 reinoud
38 #define MNINDIR(fsdev) ((fsdev)->nd_blocksize / sizeof(nandfs_daddr_t))
41 * Inode structure. There are a few dedicated inode numbers that are
44 #define NANDFS_WHT_INO 1 /* Whiteout ino */
45 #define NANDFS_ROOT_INO 2 /* Root file inode */
46 #define NANDFS_DAT_INO 3 /* DAT file */
47 #define NANDFS_CPFILE_INO 4 /* checkpoint file */
48 #define NANDFS_SUFILE_INO 5 /* segment usage file */
49 #define NANDFS_IFILE_INO 6 /* ifile */
50 #define NANDFS_GC_INO 7 /* Cleanerd node */
51 #define NANDFS_ATIME_INO 8 /* Atime file (reserved) */
52 #define NANDFS_XATTR_INO 9 /* Xattribute file (reserved) */
53 #define NANDFS_SKETCH_INO 10 /* Sketch file (obsolete) */
54 #define NANDFS_USER_INO 11 /* First user's file inode number */
56 #define NANDFS_SYS_NODE(ino) \
57 (((ino) >= NANDFS_DAT_INO) && ((ino) <= NANDFS_GC_INO))
59 #define NANDFS_NDADDR 12 /* Direct addresses in inode. */
60 #define NANDFS_NIADDR 3 /* Indirect addresses in inode. */
62 typedef int64_t nandfs_daddr_t;
63 typedef int64_t nandfs_lbn_t;
66 uint64_t i_blocks; /* 0: size in device blocks */
67 uint64_t i_size; /* 8: size in bytes */
68 uint64_t i_ctime; /* 16: creation time in seconds */
69 uint64_t i_mtime; /* 24: modification time in seconds part*/
70 uint32_t i_ctime_nsec; /* 32: creation time nanoseconds part */
71 uint32_t i_mtime_nsec; /* 36: modification time in nanoseconds */
72 uint32_t i_uid; /* 40: user id */
73 uint32_t i_gid; /* 44: group id */
74 uint16_t i_mode; /* 48: file mode */
75 uint16_t i_links_count; /* 50: number of references to the inode*/
76 uint32_t i_flags; /* 52: NANDFS_*_FL flags */
77 nandfs_daddr_t i_special; /* 56: special */
78 nandfs_daddr_t i_db[NANDFS_NDADDR]; /* 64: Direct disk blocks. */
79 nandfs_daddr_t i_ib[NANDFS_NIADDR]; /* 160: Indirect disk blocks. */
80 uint64_t i_xattr; /* 184: reserved for extended attributes*/
81 uint32_t i_generation; /* 192: file generation for NFS */
82 uint32_t i_pad[15]; /* 196: make it 64 bits aligned */
86 CTASSERT(sizeof(struct nandfs_inode) == 256);
90 * Each checkpoint/snapshot has a super root.
92 * The super root holds the inodes of the three system files: `dat', `cp' and
93 * 'su' files. All other FS state is defined by those.
95 * It is CRC checksum'ed and time stamped.
98 struct nandfs_super_root {
99 uint32_t sr_sum; /* check-sum */
100 uint16_t sr_bytes; /* byte count of this structure */
101 uint16_t sr_flags; /* reserved for flags */
102 uint64_t sr_nongc_ctime; /* timestamp, not for cleaner(?) */
103 struct nandfs_inode sr_dat; /* DAT, virt->phys translation inode */
104 struct nandfs_inode sr_cpfile; /* CP, checkpoints inode */
105 struct nandfs_inode sr_sufile; /* SU, segment usage inode */
108 #define NANDFS_SR_MDT_OFFSET(inode_size, i) \
109 ((uint32_t)&((struct nandfs_super_root *)0)->sr_dat + \
112 #define NANDFS_SR_DAT_OFFSET(inode_size) NANDFS_SR_MDT_OFFSET(inode_size, 0)
113 #define NANDFS_SR_CPFILE_OFFSET(inode_size) NANDFS_SR_MDT_OFFSET(inode_size, 1)
114 #define NANDFS_SR_SUFILE_OFFSET(inode_size) NANDFS_SR_MDT_OFFSET(inode_size, 2)
115 #define NANDFS_SR_BYTES (sizeof(struct nandfs_super_root))
118 * The superblock describes the basic structure and mount history. It also
119 * records some sizes of structures found on the disc for sanity checks.
121 * The superblock is stored at two places: NANDFS_SB_OFFSET_BYTES and
122 * NANDFS_SB2_OFFSET_BYTES.
125 /* File system states stored on media in superblock's sbp->s_state */
126 #define NANDFS_VALID_FS 0x0001 /* cleanly unmounted and all is ok */
127 #define NANDFS_ERROR_FS 0x0002 /* there were errors detected, fsck */
128 #define NANDFS_RESIZE_FS 0x0004 /* resize required, XXX unknown flag*/
129 #define NANDFS_MOUNT_STATE_BITS "\20\1VALID_FS\2ERROR_FS\3RESIZE_FS"
132 * Brief description of control structures:
134 * NANDFS_NFSAREAS first blocks contain fsdata and some amount of super blocks.
135 * Simple round-robin policy is used in order to choose which block will
136 * contain new super block.
138 * Simple case with 2 blocks:
139 * 1: fsdata sblock1 [sblock3 [sblock5 ..]]
140 * 2: fsdata sblock2 [sblock4 [sblock6 ..]]
142 struct nandfs_fsdata {
146 uint32_t f_sum; /* checksum of fsdata */
147 uint32_t f_rev_level; /* major disk format revision */
149 uint64_t f_ctime; /* creation time (execution time
151 /* Block size represented as: blocksize = 1 << (f_log_block_size + 10) */
152 uint32_t f_log_block_size;
154 uint16_t f_inode_size; /* size of an inode */
155 uint16_t f_dat_entry_size; /* size of a dat entry */
156 uint16_t f_checkpoint_size; /* size of a checkpoint */
157 uint16_t f_segment_usage_size; /* size of a segment usage */
159 uint16_t f_sbbytes; /* byte count of CRC calculation
160 for super blocks. s_reserved
163 uint16_t f_errors; /* behaviour on detecting errors */
165 uint32_t f_erasesize;
166 uint64_t f_nsegments; /* number of segm. in filesystem */
167 nandfs_daddr_t f_first_data_block; /* 1st seg disk block number */
168 uint32_t f_blocks_per_segment; /* number of blocks per segment */
169 uint32_t f_r_segments_percentage; /* reserved segments percentage */
171 struct uuid f_uuid; /* 128-bit uuid for volume */
172 char f_volume_name[16]; /* volume name */
177 CTASSERT(sizeof(struct nandfs_fsdata) == 512);
180 struct nandfs_super_block {
181 uint16_t s_magic; /* magic value for identification */
183 uint32_t s_sum; /* check sum of super block */
185 uint64_t s_last_cno; /* last checkpoint number */
186 uint64_t s_last_pseg; /* addr part. segm. written last */
187 uint64_t s_last_seq; /* seq.number of seg written last */
188 uint64_t s_free_blocks_count; /* free blocks count */
190 uint64_t s_mtime; /* mount time */
191 uint64_t s_wtime; /* write time */
192 uint16_t s_state; /* file system state */
194 char s_last_mounted[64]; /* directory where last mounted */
196 uint32_t s_c_interval; /* commit interval of segment */
197 uint32_t s_c_block_max; /* threshold of data amount for
198 the segment construction */
199 uint32_t s_reserved[32]; /* padding to end of the block */
203 CTASSERT(sizeof(struct nandfs_super_block) == 256);
206 #define NANDFS_FSDATA_MAGIC 0xf8da
207 #define NANDFS_SUPER_MAGIC 0x8008
209 #define NANDFS_NFSAREAS 4
210 #define NANDFS_DATA_OFFSET_BYTES(esize) (NANDFS_NFSAREAS * (esize))
212 #define NANDFS_SBLOCK_OFFSET_BYTES (sizeof(struct nandfs_fsdata))
214 #define NANDFS_DEF_BLOCKSIZE 4096
215 #define NANDFS_MIN_BLOCKSIZE 512
217 #define NANDFS_DEF_ERASESIZE (2 << 16)
219 #define NANDFS_MIN_SEGSIZE NANDFS_DEF_ERASESIZE
221 #define NANDFS_CURRENT_REV 9 /* current major revision */
223 #define NANDFS_FSDATA_CRC_BYTES offsetof(struct nandfs_fsdata, f_pad)
224 /* Bytes count of super_block for CRC-calculation */
225 #define NANDFS_SB_BYTES offsetof(struct nandfs_super_block, s_reserved)
227 /* Maximal count of links to a file */
228 #define NANDFS_LINK_MAX 32000
231 * Structure of a directory entry.
233 * Note that they can't span blocks; the rec_len fills out.
236 #define NANDFS_NAME_LEN 255
237 struct nandfs_dir_entry {
238 uint64_t inode; /* inode number */
239 uint16_t rec_len; /* directory entry length */
240 uint8_t name_len; /* name length */
242 char name[NANDFS_NAME_LEN]; /* file name */
247 * NANDFS_DIR_PAD defines the directory entries boundaries
249 * NOTE: It must be a multiple of 8
251 #define NANDFS_DIR_PAD 8
252 #define NANDFS_DIR_ROUND (NANDFS_DIR_PAD - 1)
253 #define NANDFS_DIR_NAME_OFFSET (offsetof(struct nandfs_dir_entry, name))
254 #define NANDFS_DIR_REC_LEN(name_len) \
255 (((name_len) + NANDFS_DIR_NAME_OFFSET + NANDFS_DIR_ROUND) \
257 #define NANDFS_DIR_NAME_LEN(name_len) \
258 (NANDFS_DIR_REC_LEN(name_len) - NANDFS_DIR_NAME_OFFSET)
261 * NiLFS/NANDFS devides the disc into fixed length segments. Each segment is
262 * filled with one or more partial segments of variable lengths.
264 * Each partial segment has a segment summary header followed by updates of
265 * files and optionally a super root.
269 * Virtual to physical block translation information. For data blocks it maps
270 * logical block number bi_blkoff to virtual block nr bi_vblocknr. For non
271 * datablocks it is the virtual block number assigned to an indirect block
272 * and has no bi_blkoff. The physical block number is the next
273 * available data block in the partial segment after all the binfo's.
275 struct nandfs_binfo_v {
276 uint64_t bi_ino; /* file's inode */
277 uint64_t bi_vblocknr; /* assigned virtual block number */
278 uint64_t bi_blkoff; /* for file's logical block number */
282 * DAT allocation. For data blocks just the logical block number that maps on
283 * the next available data block in the partial segment after the binfo's.
285 struct nandfs_binfo_dat {
287 uint64_t bi_blkoff; /* DAT file's logical block number */
288 uint8_t bi_level; /* whether this is meta block */
293 CTASSERT(sizeof(struct nandfs_binfo_v) == sizeof(struct nandfs_binfo_dat));
296 /* Convenience union for both types of binfo's */
298 struct nandfs_binfo_v bi_v;
299 struct nandfs_binfo_dat bi_dat;
302 /* Indirect buffers path */
303 struct nandfs_indir {
304 nandfs_daddr_t in_lbn;
308 /* The (partial) segment summary */
309 struct nandfs_segment_summary {
310 uint32_t ss_datasum; /* CRC of complete data block */
311 uint32_t ss_sumsum; /* CRC of segment summary only */
312 uint32_t ss_magic; /* magic to identify segment summary */
313 uint16_t ss_bytes; /* size of segment summary structure */
314 uint16_t ss_flags; /* NANDFS_SS_* flags */
315 uint64_t ss_seq; /* sequence number of this segm. sum */
316 uint64_t ss_create; /* creation timestamp in seconds */
317 uint64_t ss_next; /* blocknumber of next segment */
318 uint32_t ss_nblocks; /* number of blocks used by summary */
319 uint32_t ss_nbinfos; /* number of binfo structures */
320 uint32_t ss_sumbytes; /* total size of segment summary */
322 /* stream of binfo structures */
325 #define NANDFS_SEGSUM_MAGIC 0x8e680011 /* segment summary magic number */
327 /* Segment summary flags */
328 #define NANDFS_SS_LOGBGN 0x0001 /* begins a logical segment */
329 #define NANDFS_SS_LOGEND 0x0002 /* ends a logical segment */
330 #define NANDFS_SS_SR 0x0004 /* has super root */
331 #define NANDFS_SS_SYNDT 0x0008 /* includes data only updates */
332 #define NANDFS_SS_GC 0x0010 /* segment written for cleaner operation */
333 #define NANDFS_SS_FLAG_BITS "\20\1LOGBGN\2LOGEND\3SR\4SYNDT\5GC"
335 /* Segment summary constrains */
336 #define NANDFS_SEG_MIN_BLOCKS 16 /* minimum number of blocks in a
338 #define NANDFS_PSEG_MIN_BLOCKS 2 /* minimum number of blocks in a
340 #define NANDFS_MIN_NRSVSEGS 8 /* minimum number of reserved
344 * Structure of DAT/inode file.
346 * A DAT file is divided into groups. The maximum number of groups is the
347 * number of block group descriptors that fit into one block; this descriptor
348 * only gives the number of free entries in the associated group.
350 * Each group has a block sized bitmap indicating if an entry is taken or
351 * empty. Each bit stands for a DAT entry.
353 * The inode file has exactly the same format only the entries are inode
357 struct nandfs_block_group_desc {
358 uint32_t bg_nfrees; /* num. free entries in block group */
361 /* DAT entry in a super root's DAT file */
362 struct nandfs_dat_entry {
363 uint64_t de_blocknr; /* block number */
364 uint64_t de_start; /* valid from checkpoint */
365 uint64_t de_end; /* valid till checkpoint */
366 uint64_t de_rsv; /* reserved for future use */
370 * Structure of CP file.
372 * A snapshot is just a checkpoint only it's protected against removal by the
373 * cleaner. The snapshots are kept on a double linked list of checkpoints.
375 struct nandfs_snapshot_list {
376 uint64_t ssl_next; /* checkpoint nr. forward */
377 uint64_t ssl_prev; /* checkpoint nr. back */
380 /* Checkpoint entry structure */
381 struct nandfs_checkpoint {
382 uint32_t cp_flags; /* NANDFS_CHECKPOINT_* flags */
383 uint32_t cp_checkpoints_count; /* ZERO, not used anymore? */
384 struct nandfs_snapshot_list cp_snapshot_list; /* list of snapshots */
385 uint64_t cp_cno; /* checkpoint number */
386 uint64_t cp_create; /* creation timestamp */
387 uint64_t cp_nblk_inc; /* number of blocks incremented */
388 uint64_t cp_blocks_count; /* reserved (might be deleted) */
389 struct nandfs_inode cp_ifile_inode; /* inode file inode */
392 /* Checkpoint flags */
393 #define NANDFS_CHECKPOINT_SNAPSHOT 1
394 #define NANDFS_CHECKPOINT_INVALID 2
395 #define NANDFS_CHECKPOINT_SKETCH 4
396 #define NANDFS_CHECKPOINT_MINOR 8
397 #define NANDFS_CHECKPOINT_BITS "\20\1SNAPSHOT\2INVALID\3SKETCH\4MINOR"
399 /* Header of the checkpoint file */
400 struct nandfs_cpfile_header {
401 uint64_t ch_ncheckpoints; /* number of checkpoints */
402 uint64_t ch_nsnapshots; /* number of snapshots */
403 struct nandfs_snapshot_list ch_snapshot_list; /* snapshot list */
406 #define NANDFS_CPFILE_FIRST_CHECKPOINT_OFFSET \
407 ((sizeof(struct nandfs_cpfile_header) + \
408 sizeof(struct nandfs_checkpoint) - 1) / \
409 sizeof(struct nandfs_checkpoint))
412 #define NANDFS_NOSEGMENT 0xffffffff
415 * Structure of SU file.
417 * The segment usage file sums up how each of the segments are used. They are
418 * indexed by their segment number.
421 /* Segment usage entry */
422 struct nandfs_segment_usage {
423 uint64_t su_lastmod; /* last modified timestamp */
424 uint32_t su_nblocks; /* number of blocks in segment */
425 uint32_t su_flags; /* NANDFS_SEGMENT_USAGE_* flags */
428 /* Segment usage flag */
429 #define NANDFS_SEGMENT_USAGE_ACTIVE 1
430 #define NANDFS_SEGMENT_USAGE_DIRTY 2
431 #define NANDFS_SEGMENT_USAGE_ERROR 4
432 #define NANDFS_SEGMENT_USAGE_GC 8
433 #define NANDFS_SEGMENT_USAGE_BITS "\20\1ACTIVE\2DIRTY\3ERROR"
435 /* Header of the segment usage file */
436 struct nandfs_sufile_header {
437 uint64_t sh_ncleansegs; /* number of segments marked clean */
438 uint64_t sh_ndirtysegs; /* number of segments marked dirty */
439 uint64_t sh_last_alloc; /* last allocated segment number */
442 #define NANDFS_SUFILE_FIRST_SEGMENT_USAGE_OFFSET \
443 ((sizeof(struct nandfs_sufile_header) + \
444 sizeof(struct nandfs_segment_usage) - 1) / \
445 sizeof(struct nandfs_segment_usage))
447 struct nandfs_seg_stat {
449 uint64_t nss_ncleansegs;
450 uint64_t nss_ndirtysegs;
452 uint64_t nss_nongc_ctime;
453 uint64_t nss_prot_seq;
461 #define NANDFS_CPINFO_MAX 512
463 struct nandfs_cpinfo {
468 uint64_t nci_nblk_inc;
469 uint64_t nci_blocks_count;
473 #define NANDFS_SEGMENTS_MAX 512
475 struct nandfs_suinfo {
477 uint64_t nsi_lastmod;
482 #define NANDFS_VINFO_MAX 512
484 struct nandfs_vinfo {
486 uint64_t nvi_vblocknr;
489 uint64_t nvi_blocknr;
493 struct nandfs_cpmode {
507 struct nandfs_cpstat {
513 struct nandfs_period {
518 struct nandfs_vdesc {
521 uint64_t vd_vblocknr;
522 struct nandfs_period vd_period;
529 struct nandfs_bdesc {
531 uint64_t bd_oblocknr;
540 #define MNAMELEN 1024
544 struct nandfs_fsinfo {
545 struct nandfs_fsdata fs_fsdata;
546 struct nandfs_super_block fs_super;
547 char fs_dev[MNAMELEN];
550 #define NANDFS_MAX_MOUNTS 65535
552 #define NANDFS_IOCTL_GET_SUSTAT _IOR('N', 100, struct nandfs_seg_stat)
553 #define NANDFS_IOCTL_CHANGE_CPMODE _IOWR('N', 101, struct nandfs_cpmode)
554 #define NANDFS_IOCTL_GET_CPINFO _IOWR('N', 102, struct nandfs_argv)
555 #define NANDFS_IOCTL_DELETE_CP _IOWR('N', 103, uint64_t[2])
556 #define NANDFS_IOCTL_GET_CPSTAT _IOR('N', 104, struct nandfs_cpstat)
557 #define NANDFS_IOCTL_GET_SUINFO _IOWR('N', 105, struct nandfs_argv)
558 #define NANDFS_IOCTL_GET_VINFO _IOWR('N', 106, struct nandfs_argv)
559 #define NANDFS_IOCTL_GET_BDESCS _IOWR('N', 107, struct nandfs_argv)
560 #define NANDFS_IOCTL_GET_FSINFO _IOR('N', 108, struct nandfs_fsinfo)
561 #define NANDFS_IOCTL_MAKE_SNAP _IOWR('N', 109, uint64_t)
562 #define NANDFS_IOCTL_DELETE_SNAP _IOWR('N', 110, uint64_t)
563 #define NANDFS_IOCTL_SYNC _IOWR('N', 111, uint64_t)
565 #endif /* _NANDFS_FS_H */