HAMMER 27/many: Major surgery - change allocation model
[dragonfly.git] / sys / vfs / hammer / hammer_disk.h
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_disk.h,v 1.22 2008/02/08 08:30:59 dillon Exp $
35  */
36
37 #ifndef VFS_HAMMER_DISK_H_
38 #define VFS_HAMMER_DISK_H_
39
40 #ifndef _SYS_UUID_H_
41 #include <sys/uuid.h>
42 #endif
43
44 /*
45  * The structures below represent the on-disk format for a HAMMER
46  * filesystem.  Note that all fields for on-disk structures are naturally
47  * aligned.  The host endian format is used - compatibility is possible
48  * if the implementation detects reversed endian and adjusts data accordingly.
49  *
50  * Most of HAMMER revolves around the concept of an object identifier.  An
51  * obj_id is a 64 bit quantity which uniquely identifies a filesystem object
52  * FOR THE ENTIRE LIFE OF THE FILESYSTEM.  This uniqueness allows backups
53  * and mirrors to retain varying amounts of filesystem history by removing
54  * any possibility of conflict through identifier reuse.
55  *
56  * A HAMMER filesystem may spam multiple volumes.
57  *
58  * A HAMMER filesystem uses a 16K filesystem buffer size.  All filesystem
59  * I/O is done in multiples of 16K.  Most buffer-sized headers such as those
60  * used by volumes, super-clusters, clusters, and basic filesystem buffers
61  * use fixed-sized A-lists which are heavily dependant on HAMMER_BUFSIZE.
62  */
63 #define HAMMER_BUFSIZE  16384
64 #define HAMMER_BUFMASK  (HAMMER_BUFSIZE - 1)
65 #define HAMMER_MAXDATA  (256*1024)
66
67 #define HAMMER_BUFSIZE64        ((u_int64_t)HAMMER_BUFSIZE)
68 #define HAMMER_BUFMASK64        ((u_int64_t)HAMMER_BUFMASK)
69
70 #define HAMMER_OFF_ZONE_MASK    0xF000000000000000ULL /* zone portion */
71 #define HAMMER_OFF_VOL_MASK     0x0FF0000000000000ULL /* volume portion */
72 #define HAMMER_OFF_SHORT_MASK   0x000FFFFFFFFFFFFFULL /* offset portion */
73 #define HAMMER_OFF_LONG_MASK    0x0FFFFFFFFFFFFFFFULL /* offset portion */
74 #define HAMMER_OFF_SHORT_REC_MASK 0x000FFFFFFF000000ULL /* recovery boundary */
75 #define HAMMER_OFF_LONG_REC_MASK 0x0FFFFFFFFF000000ULL /* recovery boundary */
76 #define HAMMER_RECOVERY_BND     0x0000000001000000ULL
77
78 /*
79  * Hammer transction ids are 64 bit unsigned integers and are usually
80  * synchronized with the time of day in nanoseconds.
81  *
82  * Hammer offsets are used for FIFO indexing and embed a cycle counter
83  * and volume number in addition to the offset.  Most offsets are required
84  * to be 64-byte aligned.
85  */
86 typedef u_int64_t hammer_tid_t;
87 typedef u_int64_t hammer_off_t;
88
89 #define HAMMER_MIN_TID          0ULL                    /* unsigned */
90 #define HAMMER_MAX_TID          0xFFFFFFFFFFFFFFFFULL   /* unsigned */
91 #define HAMMER_MIN_KEY          -0x8000000000000000LL   /* signed */
92 #define HAMMER_MAX_KEY          0x7FFFFFFFFFFFFFFFLL    /* signed */
93 #define HAMMER_MIN_OBJID        HAMMER_MIN_KEY          /* signed */
94 #define HAMMER_MAX_OBJID        HAMMER_MAX_KEY          /* signed */
95 #define HAMMER_MIN_RECTYPE      0x0U                    /* unsigned */
96 #define HAMMER_MAX_RECTYPE      0xFFFFU                 /* unsigned */
97 #define HAMMER_MIN_OFFSET       0ULL                    /* unsigned */
98 #define HAMMER_MAX_OFFSET       0xFFFFFFFFFFFFFFFFULL   /* unsigned */
99
100 /*
101  * hammer_off_t has several different encodings.  Note that not all zones
102  * encode a vol_no.
103  *
104  * zone 0 (z,v,o):      reserved (for sanity)
105  * zone 1 (z,v,o):      raw volume relative (offset 0 is the volume header)
106  * zone 2 (z,v,o):      raw buffer relative (offset 0 is the first buffer)
107  * zone 3-15     :      reserved
108  */
109
110 #define HAMMER_ZONE_RAW_VOLUME          0x1000000000000000ULL
111 #define HAMMER_ZONE_RAW_BUFFER          0x2000000000000000ULL
112
113 #define HAMMER_VOL_ENCODE(vol_no)                       \
114         ((hammer_off_t)((vol_no) & 255) << 52)
115 #define HAMMER_VOL_DECODE(ham_off)                      \
116         (int32_t)(((hammer_off_t)(ham_off) >> 52) & 255)
117 #define HAMMER_SHORT_OFF_ENCODE(offset)                 \
118         ((hammer_off_t)(offset) & HAMMER_OFF_SHORT_MASK)
119 #define HAMMER_LONG_OFF_ENCODE(offset)                  \
120         ((hammer_off_t)(offset) & HAMMER_OFF_LONG_MASK)
121
122 #define HAMMER_ENCODE_RAW_VOLUME(vol_no, offset)        \
123         (HAMMER_ZONE_RAW_VOLUME |                       \
124         HAMMER_VOL_ENCODE(vol_no) |                     \
125         HAMMER_SHORT_OFF_ENCODE(offset))
126
127 #define HAMMER_ENCODE_RAW_BUFFER(vol_no, offset)        \
128         (HAMMER_ZONE_RAW_BUFFER |                       \
129         HAMMER_VOL_ENCODE(vol_no) |                     \
130         HAMMER_SHORT_OFF_ENCODE(offset))
131
132
133 /*
134  * All on-disk HAMMER structures which make up elements of the FIFO contain
135  * a hammer_fifo_head structure.  This structure contains all the information
136  * required to validate the fifo element and to scan the fifo in either
137  * direction.
138  *
139  * Nearly all such structures are guaranteed to not cross a 16K filesystem
140  * buffer boundary.  The one exception is a record, whos related data may
141  * cross a buffer boundary.
142  *
143  * HAMMER guarantees alignment with a fifo head structure at 16MB intervals
144  * (i.e. the base of the buffer will not be in the middle of a data record).
145  * This is used to allow the recovery code to re-sync after hitting corrupted
146  * data.
147  */
148 #define HAMMER_HEAD_ONDISK_SIZE         32
149 #define HAMMER_HEAD_RECOVERY_ALIGNMENT  (16 * 1024 * 1024)
150 #define HAMMER_HEAD_ALIGN               32
151 #define HAMMER_HEAD_ALIGN_MASK          (HAMMER_HEAD_ALIGN - 1)
152
153 struct hammer_fifo_head {
154         u_int16_t hdr_signature;
155         u_int16_t hdr_type;
156         u_int32_t hdr_fwd_link;
157         u_int32_t hdr_rev_link;
158         u_int32_t hdr_crc;
159         hammer_tid_t hdr_seq;
160         hammer_tid_t hdr_tid;
161 };
162
163 typedef struct hammer_fifo_head *hammer_fifo_head_t;
164
165 /*
166  * Fifo header types.
167  */
168 #define HAMMER_HEAD_TYPE_PAD    0xF000U         /* FIFO pad (also FREED) */
169 #define HAMMER_HEAD_TYPE_VOL    0x7001U         /* Volume (dummy header) */
170 #define HAMMER_HEAD_TYPE_BTREE  0x7002U         /* B-Tree node */
171 #define HAMMER_HEAD_TYPE_UNDO   0x7003U         /* random UNDO information */
172 #define HAMMER_HEAD_TYPE_DELETE 0x7004U         /* record deletion */
173 #define HAMMER_HEAD_TYPE_RECORD 0x7005U         /* Filesystem record */
174 #define HAMMER_HEAD_TYPE_TERM   0x7009U         /* Dummy Terminator */
175
176 #define HAMMER_HEAD_TYPEF_FREED 0x8000U         /* Indicates object freed */
177
178 #define HAMMER_HEAD_SIGNATURE   0xC84EU
179
180 /*
181  * Misc FIFO structures (except for the B-Tree node and hammer record)
182  */
183 struct hammer_fifo_undo {
184         struct hammer_fifo_head head;
185         hammer_off_t            undo_offset;
186         /* followed by data */
187 };
188
189 typedef struct hammer_fifo_undo *hammer_fifo_undo_t;
190
191 /*
192  * Volume header types
193  */
194 #define HAMMER_FSBUF_VOLUME     0xC8414D4DC5523031ULL   /* HAMMER01 */
195 #define HAMMER_FSBUF_VOLUME_REV 0x313052C54D4D41C8ULL   /* (reverse endian) */
196
197 /*
198  * The B-Tree structures need hammer_fsbuf_head.
199  */
200 #include "hammer_btree.h"
201
202 /*
203  * HAMMER Volume header
204  *
205  * A HAMMER filesystem is built from any number of block devices,  Each block
206  * device contains a volume header followed by however many buffers fit
207  * into the volume.
208  *
209  * One of the volumes making up a HAMMER filesystem is the master, the
210  * rest are slaves.  It does not have to be volume #0.
211  *
212  * The volume header takes up an entire 16K filesystem buffer and may
213  * represent up to 64KTB (65536 TB) of space.
214  *
215  * Special field notes:
216  *
217  *      vol_bot_beg - offset of boot area (mem_beg - bot_beg bytes)
218  *      vol_mem_beg - offset of memory log (clu_beg - mem_beg bytes)
219  *      vol_buf_beg - offset of the first buffer.
220  *
221  *      The memory log area allows a kernel to cache new records and data
222  *      in memory without allocating space in the actual filesystem to hold
223  *      the records and data.  In the event that a filesystem becomes full,
224  *      any records remaining in memory can be flushed to the memory log
225  *      area.  This allows the kernel to immediately return success.
226  */
227 #define HAMMER_VOL_MAXCLUSTERS          32768   /* 1-layer */
228 #define HAMMER_VOL_MAXSUPERCLUSTERS     4096    /* 2-layer */
229 #define HAMMER_VOL_SUPERCLUSTER_GROUP   16
230 #define HAMMER_VOL_METAELMS_1LYR        HAMMER_ALIST_METAELMS_32K_1LYR
231 #define HAMMER_VOL_METAELMS_2LYR        HAMMER_ALIST_METAELMS_16K_2LYR
232
233 #define HAMMER_BOOT_MINBYTES            (32*1024)
234 #define HAMMER_BOOT_NOMBYTES            (64LL*1024*1024)
235 #define HAMMER_BOOT_MAXBYTES            (256LL*1024*1024)
236
237 #define HAMMER_MEM_MINBYTES             (256*1024)
238 #define HAMMER_MEM_NOMBYTES             (1LL*1024*1024*1024)
239 #define HAMMER_MEM_MAXBYTES             (64LL*1024*1024*1024)
240
241 struct hammer_volume_ondisk {
242         struct hammer_fifo_head head;
243         int64_t vol_bot_beg;    /* byte offset of boot area or 0 */
244         int64_t vol_mem_beg;    /* byte offset of memory log or 0 */
245         int64_t vol_buf_beg;    /* byte offset of first buffer in volume */
246         int64_t vol_buf_end;    /* byte offset of volume EOF (on buf bndry) */
247         int64_t vol_locked;     /* reserved clusters are >= this offset */
248
249         uuid_t    vol_fsid;     /* identify filesystem */
250         uuid_t    vol_fstype;   /* identify filesystem type */
251         char      vol_name[64]; /* Name of volume */
252
253         u_int64_t vol_signature;/* Signature #2 */
254         int32_t vol_no;         /* volume number within filesystem */
255         int32_t vol_count;      /* number of volumes making up FS */
256
257         u_int32_t vol_version;  /* version control information */
258         u_int32_t vol_reserved01;
259         u_int32_t vol_flags;    /* volume flags */
260         u_int32_t vol_rootvol;  /* which volume is the root volume? */
261
262         int32_t vol_reserved04; /* cluster size (same for all volumes) */
263         int32_t vol_reserved05;
264         u_int32_t vol_reserved06;
265         u_int32_t vol_reserved07;
266
267         int32_t vol_blocksize;          /* for statfs only */
268         int32_t vol_reserved08;
269         int64_t vol_nblocks;            /* total allocatable hammer bufs */
270
271         /*
272          * These fields are initialized and space is reserved in every
273          * volume making up a HAMMER filesytem, but only the master volume
274          * contains valid data.
275          */
276         int64_t vol0_stat_bytes;        /* for statfs only */
277         int64_t vol0_stat_inodes;       /* for statfs only */
278         int64_t vol0_stat_records;      /* total records in filesystem */
279         hammer_off_t vol0_fifo_beg;     /* CIRCULAR FIFO START */
280         hammer_off_t vol0_fifo_end;     /* CIRCULAR FIFO END */
281         hammer_off_t vol0_btree_root;   /* B-Tree root */
282         hammer_tid_t vol0_next_tid;     /* highest synchronized TID */
283         hammer_tid_t vol0_next_seq;     /* next SEQ no */
284 };
285
286 typedef struct hammer_volume_ondisk *hammer_volume_ondisk_t;
287
288 #define HAMMER_VOLF_VALID               0x0001  /* valid entry */
289 #define HAMMER_VOLF_OPEN                0x0002  /* volume is open */
290
291 /*
292  * All HAMMER records have a common 72-byte base and a variable-length
293  * extension, plus a possible data reference.  The data portion of the
294  * HAMMER record can cross a filesystem buffer boundary (but not the primary
295  * record portion).
296  *
297  * Current only relative in-band data offsets are supported, but the field
298  * is large enough for future out-of-band references.
299  */
300 struct hammer_base_record {
301         struct hammer_fifo_head head;   /* 16 byte fifo header */
302         struct hammer_base_elm base;    /* 40 byte base element */
303         hammer_off_t    data_off;       /* in-band or out-of-band */
304         int32_t         data_len;       /* size of data in bytes */
305         u_int32_t       reserved03;
306 };
307
308 /*
309  * Record types are fairly straightforward.  The B-Tree includes the record
310  * type in its index sort.
311  *
312  * In particular please note that it is possible to create a pseudo-
313  * filesystem within a HAMMER filesystem by creating a special object
314  * type within a directory.  Pseudo-filesystems are used as replication
315  * targets and even though they are built within a HAMMER filesystem they
316  * get their own obj_id space (and thus can serve as a replication target)
317  * and look like a mount point to the system.
318  *
319  * Inter-cluster records are special-cased in the B-Tree.  These records
320  * are referenced from a B-Tree INTERNAL node, NOT A LEAF.  This means
321  * that the element in the B-Tree node is actually a boundary element whos
322  * base element fields, including rec_type, reflect the boundary, NOT
323  * the inter-cluster record type.
324  *
325  * HAMMER_RECTYPE_CLUSTER - only set in the actual inter-cluster record,
326  * not set in the left or right boundary elements around the inter-cluster
327  * reference of an internal node in the B-Tree (because doing so would
328  * interfere with the boundary tests).
329  *
330  * NOTE: hammer_ip_delete_range_all() deletes all record types greater
331  * then HAMMER_RECTYPE_INODE.
332  */
333 #define HAMMER_RECTYPE_UNKNOWN          0
334 #define HAMMER_RECTYPE_LOWEST           1       /* lowest record type avail */
335 #define HAMMER_RECTYPE_INODE            1       /* inode in obj_id space */
336 #define HAMMER_RECTYPE_PSEUDO_INODE     2       /* pseudo filesysem */
337 #define HAMMER_RECTYPE_CLUSTER          3       /* inter-cluster reference */
338 #define HAMMER_RECTYPE_DATA             0x10
339 #define HAMMER_RECTYPE_DIRENTRY         0x11
340 #define HAMMER_RECTYPE_DB               0x12
341 #define HAMMER_RECTYPE_EXT              0x13    /* ext attributes */
342 #define HAMMER_RECTYPE_FIX              0x14    /* fixed attribute */
343
344 #define HAMMER_FIXKEY_SYMLINK           1
345
346 #define HAMMER_OBJTYPE_UNKNOWN          0       /* (never exists on-disk) */
347 #define HAMMER_OBJTYPE_DIRECTORY        1
348 #define HAMMER_OBJTYPE_REGFILE          2
349 #define HAMMER_OBJTYPE_DBFILE           3
350 #define HAMMER_OBJTYPE_FIFO             4
351 #define HAMMER_OBJTYPE_CDEV             5
352 #define HAMMER_OBJTYPE_BDEV             6
353 #define HAMMER_OBJTYPE_SOFTLINK         7
354 #define HAMMER_OBJTYPE_PSEUDOFS         8       /* pseudo filesystem obj */
355
356 /*
357  * A HAMMER inode record.
358  *
359  * This forms the basis for a filesystem object.  obj_id is the inode number,
360  * key1 represents the pseudo filesystem id for security partitioning
361  * (preventing cross-links and/or restricting a NFS export and specifying the
362  * security policy), and key2 represents the data retention policy id.
363  *
364  * Inode numbers are 64 bit quantities which uniquely identify a filesystem
365  * object for the ENTIRE life of the filesystem, even after the object has
366  * been deleted.  For all intents and purposes inode numbers are simply 
367  * allocated by incrementing a sequence space.
368  *
369  * There is an important distinction between the data stored in the inode
370  * record and the record's data reference.  The record references a
371  * hammer_inode_data structure but the filesystem object size and hard link
372  * count is stored in the inode record itself.  This allows multiple inodes
373  * to share the same hammer_inode_data structure.  This is possible because
374  * any modifications will lay out new data.  The HAMMER implementation need
375  * not use the data-sharing ability when laying down new records.
376  *
377  * A HAMMER inode is subject to the same historical storage requirements
378  * as any other record.  In particular any change in filesystem or hard link
379  * count will lay down a new inode record when the filesystem is synced to
380  * disk.  This can lead to a lot of junk records which get cleaned up by
381  * the data retention policy.
382  *
383  * The ino_atime and ino_mtime fields are a special case.  Modifications to
384  * these fields do NOT lay down a new record by default, though the values
385  * are effectively frozen for snapshots which access historical versions
386  * of the inode record due to other operations.  This means that atime will
387  * not necessarily be accurate in snapshots, backups, or mirrors.  mtime
388  * will be accurate in backups and mirrors since it can be regenerated from
389  * the mirroring stream.
390  *
391  * Because nlinks is historically retained the hardlink count will be
392  * accurate when accessing a HAMMER filesystem snapshot.
393  */
394 struct hammer_inode_record {
395         struct hammer_base_record base;
396         u_int64_t ino_atime;    /* last access time (not historical) */
397         u_int64_t ino_mtime;    /* last modified time (not historical) */
398         u_int64_t ino_size;     /* filesystem object size */
399         u_int64_t ino_nlinks;   /* hard links */
400 };
401
402 /*
403  * Data records specify the entire contents of a regular file object,
404  * including attributes.  Small amounts of data can theoretically be
405  * embedded in the record itself but the use of this ability verses using
406  * an out-of-band data reference depends on the implementation.
407  */
408 struct hammer_data_record {
409         struct hammer_base_record base;
410 };
411
412 /*
413  * A directory entry specifies the HAMMER filesystem object id, a copy of
414  * the file type, and file name (either embedded or as out-of-band data).
415  * If the file name is short enough to fit into den_name[] (including a
416  * terminating nul) then it will be embedded in the record, otherwise it
417  * is stored out-of-band.  The base record's data reference always points
418  * to the nul-terminated filename regardless.
419  *
420  * Directory entries are indexed with a 128 bit namekey rather then an
421  * offset.  A portion of the namekey is an iterator or randomizer to deal
422  * with collisions.
423  *
424  * NOTE: base.base.obj_type holds the filesystem object type of obj_id,
425  *       e.g. a den_type equivalent.
426  *
427  * NOTE: den_name / the filename data reference is NOT terminated with \0.
428  *
429  */
430 struct hammer_entry_record {
431         struct hammer_base_record base;
432         u_int64_t obj_id;               /* object being referenced */
433         u_int64_t reserved01;
434 };
435
436 /*
437  * Hammer rollup record
438  */
439 union hammer_record_ondisk {
440         struct hammer_base_record       base;
441         struct hammer_inode_record      inode;
442         struct hammer_data_record       data;
443         struct hammer_entry_record      entry;
444 };
445
446 typedef union hammer_record_ondisk *hammer_record_ondisk_t;
447
448 /*
449  * HAMMER UNIX Attribute data
450  *
451  * The data reference in a HAMMER inode record points to this structure.  Any
452  * modifications to the contents of this structure will result in a record
453  * replacement operation.
454  *
455  * short_data_off allows a small amount of data to be embedded in the
456  * hammer_inode_data structure.  HAMMER typically uses this to represent
457  * up to 64 bytes of data, or to hold symlinks.  Remember that allocations
458  * are in powers of 2 so 64, 192, 448, or 960 bytes of embedded data is
459  * support (64+64, 64+192, 64+448 64+960).
460  *
461  * parent_obj_id is only valid for directories (which cannot be hard-linked),
462  * and specifies the parent directory obj_id.  This field will also be set
463  * for non-directory inodes as a recovery aid, but can wind up specifying
464  * stale information.  However, since object id's are not reused, the worse
465  * that happens is that the recovery code is unable to use it.
466  */
467 struct hammer_inode_data {
468         u_int16_t version;      /* inode data version */
469         u_int16_t mode;         /* basic unix permissions */
470         u_int32_t uflags;       /* chflags */
471         u_int32_t rmajor;       /* used by device nodes */
472         u_int32_t rminor;       /* used by device nodes */
473         u_int64_t ctime;
474         u_int64_t parent_obj_id;/* parent directory obj_id */
475         uuid_t  uid;
476         uuid_t  gid;
477         /* XXX device, softlink extension */
478 };
479
480 #define HAMMER_INODE_DATA_VERSION       1
481
482 #define HAMMER_OBJID_ROOT               1
483
484 /*
485  * Rollup various structures embedded as record data
486  */
487 union hammer_data_ondisk {
488         struct hammer_inode_data inode;
489 };
490
491 #endif