fs.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*-
  2. * Copyright (c) 1982, 1986, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)fs.h 8.13 (Berkeley) 3/21/95
  30. * $FreeBSD$
  31. */
  32. /*
  33. * Each disk drive contains some number of filesystems.
  34. * A filesystem consists of a number of cylinder groups.
  35. * Each cylinder group has inodes and data.
  36. *
  37. * A filesystem is described by its super-block, which in turn
  38. * describes the cylinder groups. The super-block is critical
  39. * data and is replicated in each cylinder group to protect against
  40. * catastrophic loss. This is done at `newfs' time and the critical
  41. * super-block data does not change, so the copies need not be
  42. * referenced further unless disaster strikes.
  43. *
  44. * For filesystem fs, the offsets of the various blocks of interest
  45. * are given in the super block as:
  46. * [fs->fs_sblkno] Super-block
  47. * [fs->fs_cblkno] Cylinder group block
  48. * [fs->fs_iblkno] Inode blocks
  49. * [fs->fs_dblkno] Data blocks
  50. * The beginning of cylinder group cg in fs, is given by
  51. * the ``cgbase(fs, cg)'' macro.
  52. *
  53. * Depending on the architecture and the media, the superblock may
  54. * reside in any one of four places. For tiny media where every block
  55. * counts, it is placed at the very front of the partition. Historically,
  56. * UFS1 placed it 8K from the front to leave room for the disk label and
  57. * a small bootstrap. For UFS2 it got moved to 64K from the front to leave
  58. * room for the disk label and a bigger bootstrap, and for really piggy
  59. * systems we check at 256K from the front if the first three fail. In
  60. * all cases the size of the superblock will be SBLOCKSIZE. All values are
  61. * given in byte-offset form, so they do not imply a sector size. The
  62. * SBLOCKSEARCH specifies the order in which the locations should be searched.
  63. */
  64. #define SBLOCK_FLOPPY 0
  65. #define SBLOCK_UFS1 8192
  66. #define SBLOCK_UFS2 65536
  67. #define SBLOCK_PIGGY 262144
  68. #define SBLOCKSIZE 8192
  69. #define SBLOCKSEARCH \
  70. { SBLOCK_UFS2, SBLOCK_UFS1, SBLOCK_FLOPPY, SBLOCK_PIGGY, -1 }
  71. /*
  72. * Max number of fragments per block. This value is NOT tweakable.
  73. */
  74. #define MAXFRAG 8
  75. /*
  76. * Addresses stored in inodes are capable of addressing fragments
  77. * of `blocks'. File system blocks of at most size MAXBSIZE can
  78. * be optionally broken into 2, 4, or 8 pieces, each of which is
  79. * addressable; these pieces may be DEV_BSIZE, or some multiple of
  80. * a DEV_BSIZE unit.
  81. *
  82. * Large files consist of exclusively large data blocks. To avoid
  83. * undue wasted disk space, the last data block of a small file may be
  84. * allocated as only as many fragments of a large block as are
  85. * necessary. The filesystem format retains only a single pointer
  86. * to such a fragment, which is a piece of a single large block that
  87. * has been divided. The size of such a fragment is determinable from
  88. * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
  89. *
  90. * The filesystem records space availability at the fragment level;
  91. * to determine block availability, aligned fragments are examined.
  92. */
  93. /*
  94. * MINBSIZE is the smallest allowable block size.
  95. * In order to insure that it is possible to create files of size
  96. * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
  97. * MINBSIZE must be big enough to hold a cylinder group block,
  98. * thus changes to (struct cg) must keep its size within MINBSIZE.
  99. * Note that super blocks are always of size SBLOCKSIZE,
  100. * and that both SBLOCKSIZE and MAXBSIZE must be >= MINBSIZE.
  101. */
  102. #define MINBSIZE 4096
  103. /*
  104. * The path name on which the filesystem is mounted is maintained
  105. * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
  106. * the super block for this name.
  107. */
  108. #define MAXMNTLEN 468
  109. /*
  110. * The volume name for this filesystem is maintained in fs_volname.
  111. * MAXVOLLEN defines the length of the buffer allocated.
  112. */
  113. #define MAXVOLLEN 32
  114. /*
  115. * There is a 128-byte region in the superblock reserved for in-core
  116. * pointers to summary information. Originally this included an array
  117. * of pointers to blocks of struct csum; now there are just a few
  118. * pointers and the remaining space is padded with fs_ocsp[].
  119. *
  120. * NOCSPTRS determines the size of this padding. One pointer (fs_csp)
  121. * is taken away to point to a contiguous array of struct csum for
  122. * all cylinder groups; a second (fs_maxcluster) points to an array
  123. * of cluster sizes that is computed as cylinder groups are inspected,
  124. * and the third points to an array that tracks the creation of new
  125. * directories. A fourth pointer, fs_active, is used when creating
  126. * snapshots; it points to a bitmap of cylinder groups for which the
  127. * free-block bitmap has changed since the snapshot operation began.
  128. */
  129. #define NOCSPTRS ((128 / sizeof(void *)) - 4)
  130. /*
  131. * A summary of contiguous blocks of various sizes is maintained
  132. * in each cylinder group. Normally this is set by the initial
  133. * value of fs_maxcontig. To conserve space, a maximum summary size
  134. * is set by FS_MAXCONTIG.
  135. */
  136. #define FS_MAXCONTIG 16
  137. /*
  138. * MINFREE gives the minimum acceptable percentage of filesystem
  139. * blocks which may be free. If the freelist drops below this level
  140. * only the superuser may continue to allocate blocks. This may
  141. * be set to 0 if no reserve of free blocks is deemed necessary,
  142. * however throughput drops by fifty percent if the filesystem
  143. * is run at between 95% and 100% full; thus the minimum default
  144. * value of fs_minfree is 5%. However, to get good clustering
  145. * performance, 10% is a better choice. hence we use 10% as our
  146. * default value. With 10% free space, fragmentation is not a
  147. * problem, so we choose to optimize for time.
  148. */
  149. #define MINFREE 8
  150. #define DEFAULTOPT FS_OPTTIME
  151. /*
  152. * Grigoriy Orlov <gluk@ptci.ru> has done some extensive work to fine
  153. * tune the layout preferences for directories within a filesystem.
  154. * His algorithm can be tuned by adjusting the following parameters
  155. * which tell the system the average file size and the average number
  156. * of files per directory. These defaults are well selected for typical
  157. * filesystems, but may need to be tuned for odd cases like filesystems
  158. * being used for squid caches or news spools.
  159. */
  160. #define AVFILESIZ 16384 /* expected average file size */
  161. #define AFPDIR 64 /* expected number of files per directory */
  162. /*
  163. * The maximum number of snapshot nodes that can be associated
  164. * with each filesystem. This limit affects only the number of
  165. * snapshot files that can be recorded within the superblock so
  166. * that they can be found when the filesystem is mounted. However,
  167. * maintaining too many will slow the filesystem performance, so
  168. * having this limit is a good idea.
  169. */
  170. #define FSMAXSNAP 20
  171. /*
  172. * Used to identify special blocks in snapshots:
  173. *
  174. * BLK_NOCOPY - A block that was unallocated at the time the snapshot
  175. * was taken, hence does not need to be copied when written.
  176. * BLK_SNAP - A block held by another snapshot that is not needed by this
  177. * snapshot. When the other snapshot is freed, the BLK_SNAP entries
  178. * are converted to BLK_NOCOPY. These are needed to allow fsck to
  179. * identify blocks that are in use by other snapshots (which are
  180. * expunged from this snapshot).
  181. */
  182. #define BLK_NOCOPY ((ufs2_daddr_t)(1))
  183. #define BLK_SNAP ((ufs2_daddr_t)(2))
  184. /*
  185. * Sysctl values for the fast filesystem.
  186. */
  187. #define FFS_ADJ_REFCNT 1 /* adjust inode reference count */
  188. #define FFS_ADJ_BLKCNT 2 /* adjust inode used block count */
  189. #define FFS_BLK_FREE 3 /* free range of blocks in map */
  190. #define FFS_DIR_FREE 4 /* free specified dir inodes in map */
  191. #define FFS_FILE_FREE 5 /* free specified file inodes in map */
  192. #define FFS_SET_FLAGS 6 /* set filesystem flags */
  193. #define FFS_ADJ_NDIR 7 /* adjust number of directories */
  194. #define FFS_ADJ_NBFREE 8 /* adjust number of free blocks */
  195. #define FFS_ADJ_NIFREE 9 /* adjust number of free inodes */
  196. #define FFS_ADJ_NFFREE 10 /* adjust number of free frags */
  197. #define FFS_ADJ_NUMCLUSTERS 11 /* adjust number of free clusters */
  198. #define FFS_SET_CWD 12 /* set current directory */
  199. #define FFS_SET_DOTDOT 13 /* set inode number for ".." */
  200. #define FFS_UNLINK 14 /* remove a name in the filesystem */
  201. #define FFS_SET_INODE 15 /* update an on-disk inode */
  202. #define FFS_SET_BUFOUTPUT 16 /* set buffered writing on descriptor */
  203. #define FFS_MAXID 16 /* number of valid ffs ids */
  204. /*
  205. * Command structure passed in to the filesystem to adjust filesystem values.
  206. */
  207. #define FFS_CMD_VERSION 0x19790518 /* version ID */
  208. struct fsck_cmd {
  209. int32_t version; /* version of command structure */
  210. int32_t handle; /* reference to filesystem to be changed */
  211. int64_t value; /* inode or block number to be affected */
  212. int64_t size; /* amount or range to be adjusted */
  213. int64_t spare; /* reserved for future use */
  214. };
  215. /*
  216. * Per cylinder group information; summarized in blocks allocated
  217. * from first cylinder group data blocks. These blocks have to be
  218. * read in from fs_csaddr (size fs_cssize) in addition to the
  219. * super block.
  220. */
  221. typedef struct csum {
  222. int32_t cs_ndir; /* number of directories */
  223. int32_t cs_nbfree; /* number of free blocks */
  224. int32_t cs_nifree; /* number of free inodes */
  225. int32_t cs_nffree; /* number of free frags */
  226. } csum;
  227. typedef struct csum_total {
  228. int64_t cs_ndir; /* number of directories */
  229. int64_t cs_nbfree; /* number of free blocks */
  230. int64_t cs_nifree; /* number of free inodes */
  231. int64_t cs_nffree; /* number of free frags */
  232. int64_t cs_numclusters; /* number of free clusters */
  233. int64_t cs_spare[3]; /* future expansion */
  234. } csum_total;
  235. /*
  236. * Super block for an FFS filesystem.
  237. */
  238. typedef struct Fs {
  239. int32_t fs_firstfield; /* historic filesystem linked list, */
  240. int32_t fs_unused_1; /* used for incore super blocks */
  241. int32_t fs_sblkno; /* offset of super-block in filesys */
  242. int32_t fs_cblkno; /* offset of cyl-block in filesys */
  243. int32_t fs_iblkno; /* offset of inode-blocks in filesys */
  244. int32_t fs_dblkno; /* offset of first data after cg */
  245. int32_t fs_old_cgoffset; /* cylinder group offset in cylinder */
  246. int32_t fs_old_cgmask; /* used to calc mod fs_ntrak */
  247. int32_t fs_old_time; /* last time written */
  248. int32_t fs_old_size; /* number of blocks in fs */
  249. int32_t fs_old_dsize; /* number of data blocks in fs */
  250. uint32_t fs_ncg; /* number of cylinder groups */
  251. int32_t fs_bsize; /* size of basic blocks in fs */
  252. int32_t fs_fsize; /* size of frag blocks in fs */
  253. int32_t fs_frag; /* number of frags in a block in fs */
  254. /* these are configuration parameters */
  255. int32_t fs_minfree; /* minimum percentage of free blocks */
  256. int32_t fs_old_rotdelay; /* num of ms for optimal next block */
  257. int32_t fs_old_rps; /* disk revolutions per second */
  258. /* these fields can be computed from the others */
  259. int32_t fs_bmask; /* ``blkoff'' calc of blk offsets */
  260. int32_t fs_fmask; /* ``fragoff'' calc of frag offsets */
  261. int32_t fs_bshift; /* ``lblkno'' calc of logical blkno */
  262. int32_t fs_fshift; /* ``numfrags'' calc number of frags */
  263. /* these are configuration parameters */
  264. int32_t fs_maxcontig; /* max number of contiguous blks */
  265. int32_t fs_maxbpg; /* max number of blks per cyl group */
  266. /* these fields can be computed from the others */
  267. int32_t fs_fragshift; /* block to frag shift */
  268. int32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
  269. int32_t fs_sbsize; /* actual size of super block */
  270. int32_t fs_spare1[2]; /* old fs_csmask */
  271. /* old fs_csshift */
  272. int32_t fs_nindir; /* value of NINDIR */
  273. uint32_t fs_inopb; /* value of INOPB */
  274. int32_t fs_old_nspf; /* value of NSPF */
  275. /* yet another configuration parameter */
  276. int32_t fs_optim; /* optimization preference, see below */
  277. int32_t fs_old_npsect; /* # sectors/track including spares */
  278. int32_t fs_old_interleave; /* hardware sector interleave */
  279. int32_t fs_old_trackskew; /* sector 0 skew, per track */
  280. int32_t fs_id[2]; /* unique filesystem id */
  281. /* sizes determined by number of cylinder groups and their sizes */
  282. int32_t fs_old_csaddr; /* blk addr of cyl grp summary area */
  283. int32_t fs_cssize; /* size of cyl grp summary area */
  284. int32_t fs_cgsize; /* cylinder group size */
  285. int32_t fs_spare2; /* old fs_ntrak */
  286. int32_t fs_old_nsect; /* sectors per track */
  287. int32_t fs_old_spc; /* sectors per cylinder */
  288. int32_t fs_old_ncyl; /* cylinders in filesystem */
  289. int32_t fs_old_cpg; /* cylinders per group */
  290. uint32_t fs_ipg; /* inodes per group */
  291. int32_t fs_fpg; /* blocks per group * fs_frag */
  292. /* this data must be re-computed after crashes */
  293. csum fs_old_cstotal; /* cylinder summary information */
  294. /* these fields are cleared at mount time */
  295. int8_t fs_fmod; /* super block modified flag */
  296. int8_t fs_clean; /* filesystem is clean flag */
  297. int8_t fs_ronly; /* mounted read-only flag */
  298. int8_t fs_old_flags; /* old FS_ flags */
  299. uint8_t fs_fsmnt[MAXMNTLEN]; /* name mounted on */
  300. uint8_t fs_volname[MAXVOLLEN]; /* volume name */
  301. uint64_t fs_swuid; /* system-wide uid */
  302. int32_t fs_pad; /* due to alignment of fs_swuid */
  303. /* these fields retain the current block allocation info */
  304. int32_t fs_cgrotor; /* last cg searched */
  305. void *fs_ocsp[NOCSPTRS]; /* padding; was list of fs_cs buffers */
  306. uint8_t *fs_contigdirs; /* (u) # of contig. allocated dirs */
  307. csum *fs_csp; /* (u) cg summary info buffer */
  308. int32_t *fs_maxcluster; /* (u) max cluster in each cyl group */
  309. uint *fs_active; /* (u) used by snapshots to track fs */
  310. int32_t fs_old_cpc; /* cyl per cycle in postbl */
  311. int32_t fs_maxbsize; /* maximum blocking factor permitted */
  312. int64_t fs_unrefs; /* number of unreferenced inodes */
  313. int64_t fs_providersize; /* size of underlying GEOM provider */
  314. int64_t fs_metaspace; /* size of area reserved for metadata */
  315. int64_t fs_sparecon64[14]; /* old rotation block list head */
  316. int64_t fs_sblockloc; /* byte offset of standard superblock */
  317. csum_total fs_cstotal; /* (u) cylinder summary information */
  318. ufs_time_t fs_time; /* last time written */
  319. int64_t fs_size; /* number of blocks in fs */
  320. int64_t fs_dsize; /* number of data blocks in fs */
  321. ufs2_daddr_t fs_csaddr; /* blk addr of cyl grp summary area */
  322. int64_t fs_pendingblocks; /* (u) blocks being freed */
  323. uint32_t fs_pendinginodes; /* (u) inodes being freed */
  324. uint32_t fs_snapinum[FSMAXSNAP];/* list of snapshot inode numbers */
  325. uint32_t fs_avgfilesize; /* expected average file size */
  326. uint32_t fs_avgfpdir; /* expected # of files per directory */
  327. int32_t fs_save_cgsize; /* save real cg size to use fs_bsize */
  328. ufs_time_t fs_mtime; /* Last mount or fsck time. */
  329. int32_t fs_sujfree; /* SUJ free list */
  330. int32_t fs_sparecon32[23]; /* reserved for future constants */
  331. int32_t fs_flags; /* see FS_ flags below */
  332. int32_t fs_contigsumsize; /* size of cluster summary array */
  333. int32_t fs_maxsymlinklen; /* max length of an internal symlink */
  334. int32_t fs_old_inodefmt; /* format of on-disk inodes */
  335. uint64_t fs_maxfilesize; /* maximum representable file size */
  336. int64_t fs_qbmask; /* ~fs_bmask for use with 64-bit size */
  337. int64_t fs_qfmask; /* ~fs_fmask for use with 64-bit size */
  338. int32_t fs_state; /* validate fs_clean field */
  339. int32_t fs_old_postblformat; /* format of positional layout tables */
  340. int32_t fs_old_nrpos; /* number of rotational positions */
  341. int32_t fs_spare5[2]; /* old fs_postbloff */
  342. /* old fs_rotbloff */
  343. int32_t fs_magic; /* magic number */
  344. } Fs;
  345. /* Sanity checking. */
  346. #ifdef CTASSERT
  347. CTASSERT(sizeof(Fs) == 1376);
  348. #endif
  349. /*
  350. * Filesystem identification
  351. */
  352. #define FS_UFS1_MAGIC 0x011954 /* UFS1 fast filesystem magic number */
  353. #define FS_UFS2_MAGIC 0x19540119 /* UFS2 fast filesystem magic number */
  354. #define FS_BAD_MAGIC 0x19960408 /* UFS incomplete newfs magic number */
  355. #define FS_OKAY 0x7c269d38 /* superblock checksum */
  356. #define FS_42INODEFMT -1 /* 4.2BSD inode format */
  357. #define FS_44INODEFMT 2 /* 4.4BSD inode format */
  358. /*
  359. * Preference for optimization.
  360. */
  361. #define FS_OPTTIME 0 /* minimize allocation time */
  362. #define FS_OPTSPACE 1 /* minimize disk fragmentation */
  363. /*
  364. * Filesystem flags.
  365. *
  366. * The FS_UNCLEAN flag is set by the kernel when the filesystem was
  367. * mounted with fs_clean set to zero. The FS_DOSOFTDEP flag indicates
  368. * that the filesystem should be managed by the soft updates code.
  369. * Note that the FS_NEEDSFSCK flag is set and cleared only by the
  370. * fsck utility. It is set when background fsck finds an unexpected
  371. * inconsistency which requires a traditional foreground fsck to be
  372. * run. Such inconsistencies should only be found after an uncorrectable
  373. * disk error. A foreground fsck will clear the FS_NEEDSFSCK flag when
  374. * it has successfully cleaned up the filesystem. The kernel uses this
  375. * flag to enforce that inconsistent filesystems be mounted read-only.
  376. * The FS_INDEXDIRS flag when set indicates that the kernel maintains
  377. * on-disk auxiliary indexes (such as B-trees) for speeding directory
  378. * accesses. Kernels that do not support auxiliary indices clear the
  379. * flag to indicate that the indices need to be rebuilt (by fsck) before
  380. * they can be used.
  381. *
  382. * FS_ACLS indicates that POSIX.1e ACLs are administratively enabled
  383. * for the file system, so they should be loaded from extended attributes,
  384. * observed for access control purposes, and be administered by object
  385. * owners. FS_NFS4ACLS indicates that NFSv4 ACLs are administratively
  386. * enabled. This flag is mutually exclusive with FS_ACLS. FS_MULTILABEL
  387. * indicates that the TrustedBSD MAC Framework should attempt to back MAC
  388. * labels into extended attributes on the file system rather than maintain
  389. * a single mount label for all objects.
  390. */
  391. #define FS_UNCLEAN 0x0001 /* filesystem not clean at mount */
  392. #define FS_DOSOFTDEP 0x0002 /* filesystem using soft dependencies */
  393. #define FS_NEEDSFSCK 0x0004 /* filesystem needs sync fsck before mount */
  394. #define FS_SUJ 0x0008 /* Filesystem using softupdate journal */
  395. #define FS_ACLS 0x0010 /* file system has POSIX.1e ACLs enabled */
  396. #define FS_MULTILABEL 0x0020 /* file system is MAC multi-label */
  397. #define FS_GJOURNAL 0x0040 /* gjournaled file system */
  398. #define FS_FLAGS_UPDATED 0x0080 /* flags have been moved to new location */
  399. #define FS_NFS4ACLS 0x0100 /* file system has NFSv4 ACLs enabled */
  400. #define FS_INDEXDIRS 0x0200 /* kernel supports indexed directories */
  401. #define FS_TRIM 0x0400 /* issue BIO_DELETE for deleted blocks */
  402. /*
  403. * Macros to access bits in the fs_active array.
  404. */
  405. #define ACTIVECGNUM(fs, cg) ((fs)->fs_active[(cg) / (NBBY * sizeof(int))])
  406. #define ACTIVECGOFF(cg) (1 << ((cg) % (NBBY * sizeof(int))))
  407. #define ACTIVESET(fs, cg) do { \
  408. if ((fs)->fs_active) \
  409. ACTIVECGNUM((fs), (cg)) |= ACTIVECGOFF((cg)); \
  410. } while (0)
  411. #define ACTIVECLEAR(fs, cg) do { \
  412. if ((fs)->fs_active) \
  413. ACTIVECGNUM((fs), (cg)) &= ~ACTIVECGOFF((cg)); \
  414. } while (0)
  415. /*
  416. * The minimal number of cylinder groups that should be created.
  417. */
  418. #define MINCYLGRPS 4
  419. /*
  420. * Convert cylinder group to base address of its global summary info.
  421. */
  422. #define fs_cs(fs, indx) fs_csp[indx]
  423. /*
  424. * Cylinder group block for a filesystem.
  425. */
  426. #define CG_MAGIC 0x090255
  427. typedef struct Cg {
  428. int32_t cg_firstfield; /* historic cyl groups linked list */
  429. int32_t cg_magic; /* magic number */
  430. int32_t cg_old_time; /* time last written */
  431. uint32_t cg_cgx; /* we are the cgx'th cylinder group */
  432. int16_t cg_old_ncyl; /* number of cyl's this cg */
  433. int16_t cg_old_niblk; /* number of inode blocks this cg */
  434. uint32_t cg_ndblk; /* number of data blocks this cg */
  435. struct csum cg_cs; /* cylinder summary information */
  436. uint32_t cg_rotor; /* position of last used block */
  437. uint32_t cg_frotor; /* position of last used frag */
  438. uint32_t cg_irotor; /* position of last used inode */
  439. uint32_t cg_frsum[MAXFRAG]; /* counts of available frags */
  440. int32_t cg_old_btotoff; /* (int32) block totals per cylinder */
  441. int32_t cg_old_boff; /* (u_int16) free block positions */
  442. uint32_t cg_iusedoff; /* (u_int8) used inode map */
  443. uint32_t cg_freeoff; /* (u_int8) free block map */
  444. uint32_t cg_nextfreeoff; /* (u_int8) next available space */
  445. uint32_t cg_clustersumoff; /* (u_int32) counts of avail clusters */
  446. uint32_t cg_clusteroff; /* (u_int8) free cluster map */
  447. uint32_t cg_nclusterblks; /* number of clusters this cg */
  448. uint32_t cg_niblk; /* number of inode blocks this cg */
  449. uint32_t cg_initediblk; /* last initialized inode */
  450. uint32_t cg_unrefs; /* number of unreferenced inodes */
  451. int32_t cg_sparecon32[2]; /* reserved for future use */
  452. ufs_time_t cg_time; /* time last written */
  453. int64_t cg_sparecon64[3]; /* reserved for future use */
  454. uint8_t cg_space[1]; /* space for cylinder group maps */
  455. /* actually longer */
  456. } Cg;
  457. /*
  458. * Macros for access to cylinder group array structures
  459. */
  460. #define cg_chkmagic(cgp) ((cgp)->cg_magic == CG_MAGIC)
  461. #define cg_inosused(cgp) \
  462. ((uint8_t *)((uint8_t *)(cgp) + (cgp)->cg_iusedoff))
  463. #define cg_blksfree(cgp) \
  464. ((uint8_t *)((uint8_t *)(cgp) + (cgp)->cg_freeoff))
  465. #define cg_clustersfree(cgp) \
  466. ((uint8_t *)((uint8_t *)(cgp) + (cgp)->cg_clusteroff))
  467. #define cg_clustersum(cgp) \
  468. ((int32_t *)((uintptr_t)(cgp) + (cgp)->cg_clustersumoff))
  469. /*
  470. * Turn filesystem block numbers into disk block addresses.
  471. * This maps filesystem blocks to device size blocks.
  472. */
  473. #define fsbtodb(fs, b) ((daddr_t)(b) << (fs)->fs_fsbtodb)
  474. #define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
  475. /*
  476. * Cylinder group macros to locate things in cylinder groups.
  477. * They calc filesystem addresses of cylinder group data structures.
  478. */
  479. #define cgbase(fs, c) (((ufs2_daddr_t)(fs)->fs_fpg) * (c))
  480. #define cgdata(fs, c) (cgdmin(fs, c) + (fs)->fs_metaspace) /* data zone */
  481. #define cgmeta(fs, c) (cgdmin(fs, c)) /* meta data */
  482. #define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
  483. #define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
  484. #define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
  485. #define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
  486. #define cgstart(fs, c) (cgbase(fs, c))
  487. /*
  488. * Macros for handling inode numbers:
  489. * inode number to filesystem block offset.
  490. * inode number to cylinder group number.
  491. * inode number to filesystem block address.
  492. */
  493. #define ino_to_cg(fs, x) (((ino_t)(x)) / (fs)->fs_ipg)
  494. #define ino_to_fsba(fs, x) \
  495. ((ufs2_daddr_t)(cgimin(fs, ino_to_cg(fs, (ino_t)(x))) + \
  496. (blkstofrags((fs), ((((ino_t)(x)) % (fs)->fs_ipg) / INOPB(fs))))))
  497. #define ino_to_fsbo(fs, x) (((ino_t)(x)) % INOPB(fs))
  498. /*
  499. * Give cylinder group number for a filesystem block.
  500. * Give cylinder group block number for a filesystem block.
  501. */
  502. #define dtog(fs, d) ((d) / (fs)->fs_fpg)
  503. #define dtogd(fs, d) ((d) % (fs)->fs_fpg)
  504. /*
  505. * Extract the bits for a block from a map.
  506. * Compute the cylinder and rotational position of a cyl block addr.
  507. */
  508. #define blkmap(fs, map, loc) \
  509. (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
  510. /*
  511. * The following macros optimize certain frequently calculated
  512. * quantities by using shifts and masks in place of divisions
  513. * modulos and multiplications.
  514. */
  515. #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
  516. ((loc) & (fs)->fs_qbmask)
  517. #define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
  518. ((loc) & (fs)->fs_qfmask)
  519. #define lfragtosize(fs, frag) /* calculates ((off_t)frag * fs->fs_fsize) */ \
  520. (((off_t)(frag)) << (fs)->fs_fshift)
  521. #define lblktosize(fs, blk) /* calculates ((off_t)blk * fs->fs_bsize) */ \
  522. (((off_t)(blk)) << (fs)->fs_bshift)
  523. /* Use this only when `blk' is known to be small, e.g., < UFS_NDADDR. */
  524. #define smalllblktosize(fs, blk) /* calculates (blk * fs->fs_bsize) */ \
  525. ((blk) << (fs)->fs_bshift)
  526. #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
  527. ((loc) >> (fs)->fs_bshift)
  528. #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
  529. ((loc) >> (fs)->fs_fshift)
  530. #define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
  531. (((size) + (fs)->fs_qbmask) & (fs)->fs_bmask)
  532. #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
  533. (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask)
  534. #define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \
  535. ((frags) >> (fs)->fs_fragshift)
  536. #define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \
  537. ((blks) << (fs)->fs_fragshift)
  538. #define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \
  539. ((fsb) & ((fs)->fs_frag - 1))
  540. #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \
  541. ((fsb) &~ ((fs)->fs_frag - 1))
  542. /*
  543. * Determine the number of available frags given a
  544. * percentage to hold in reserve.
  545. */
  546. #define freespace(fs, percentreserved) \
  547. (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
  548. (fs)->fs_cstotal.cs_nffree - \
  549. (((off_t)((fs)->fs_dsize)) * (percentreserved) / 100))
  550. /*
  551. * Determining the size of a file block in the filesystem.
  552. */
  553. #define blksize(fs, ip, lbn) \
  554. (((lbn) >= UFS_NDADDR || (ip)->i_size >= \
  555. (uint64_t)smalllblktosize(fs, (lbn) + 1)) \
  556. ? (fs)->fs_bsize \
  557. : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
  558. #define sblksize(fs, size, lbn) \
  559. (((lbn) >= UFS_NDADDR || (size) >= ((lbn) + 1) << (fs)->fs_bshift) \
  560. ? (fs)->fs_bsize \
  561. : (fragroundup(fs, blkoff(fs, (size)))))
  562. /*
  563. * Number of indirects in a filesystem block.
  564. */
  565. #define NINDIR(fs) ((fs)->fs_nindir)
  566. /*
  567. * Indirect lbns are aligned on UFS_NDADDR addresses where single indirects
  568. * are the negated address of the lowest lbn reachable, double indirects
  569. * are this lbn - 1 and triple indirects are this lbn - 2. This yields
  570. * an unusual bit order to determine level.
  571. */
  572. static inline int
  573. lbn_level(ufs_lbn_t lbn)
  574. {
  575. if (lbn >= 0)
  576. return 0;
  577. switch (lbn & 0x3) {
  578. case 0:
  579. return (0);
  580. case 1:
  581. break;
  582. case 2:
  583. return (2);
  584. case 3:
  585. return (1);
  586. default:
  587. break;
  588. }
  589. return (-1);
  590. }
  591. static inline ufs_lbn_t
  592. lbn_offset(Fs *fs, int level)
  593. {
  594. ufs_lbn_t res;
  595. for (res = 1; level > 0; level--)
  596. res *= NINDIR(fs);
  597. return (res);
  598. }
  599. /*
  600. * Number of inodes in a secondary storage block/fragment.
  601. */
  602. #define INOPB(fs) ((fs)->fs_inopb)
  603. #define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
  604. /*
  605. * Softdep journal record format.
  606. */
  607. #define JOP_ADDREF 1 /* Add a reference to an inode. */
  608. #define JOP_REMREF 2 /* Remove a reference from an inode. */
  609. #define JOP_NEWBLK 3 /* Allocate a block. */
  610. #define JOP_FREEBLK 4 /* Free a block or a tree of blocks. */
  611. #define JOP_MVREF 5 /* Move a reference from one off to another. */
  612. #define JOP_TRUNC 6 /* Partial truncation record. */
  613. #define JOP_SYNC 7 /* fsync() complete record. */
  614. #define JREC_SIZE 32 /* Record and segment header size. */
  615. #define SUJ_MIN (4 * 1024 * 1024) /* Minimum journal size */
  616. #define SUJ_MAX (32 * 1024 * 1024) /* Maximum journal size */
  617. #define SUJ_FILE ".sujournal" /* Journal file name */
  618. /*
  619. * Size of the segment record header. There is at most one for each disk
  620. * block in the journal. The segment header is followed by an array of
  621. * records. fsck depends on the first element in each record being 'op'
  622. * and the second being 'ino'. Segments may span multiple disk blocks but
  623. * the header is present on each.
  624. */
  625. struct jsegrec {
  626. uint64_t jsr_seq; /* Our sequence number */
  627. uint64_t jsr_oldest; /* Oldest valid sequence number */
  628. uint16_t jsr_cnt; /* Count of valid records */
  629. uint16_t jsr_blocks; /* Count of device bsize blocks. */
  630. uint32_t jsr_crc; /* 32bit crc of the valid space */
  631. ufs_time_t jsr_time; /* timestamp for mount instance */
  632. };
  633. /*
  634. * Reference record. Records a single link count modification.
  635. */
  636. struct jrefrec {
  637. uint32_t jr_op;
  638. uint32_t jr_ino;
  639. uint32_t jr_parent;
  640. uint16_t jr_nlink;
  641. uint16_t jr_mode;
  642. int64_t jr_diroff;
  643. uint64_t jr_unused;
  644. };
  645. /*
  646. * Move record. Records a reference moving within a directory block. The
  647. * nlink is unchanged but we must search both locations.
  648. */
  649. struct jmvrec {
  650. uint32_t jm_op;
  651. uint32_t jm_ino;
  652. uint32_t jm_parent;
  653. uint16_t jm_unused;
  654. int64_t jm_oldoff;
  655. int64_t jm_newoff;
  656. };
  657. /*
  658. * Block record. A set of frags or tree of blocks starting at an indirect are
  659. * freed or a set of frags are allocated.
  660. */
  661. struct jblkrec {
  662. uint32_t jb_op;
  663. uint32_t jb_ino;
  664. ufs2_daddr_t jb_blkno;
  665. ufs_lbn_t jb_lbn;
  666. uint16_t jb_frags;
  667. uint16_t jb_oldfrags;
  668. uint32_t jb_unused;
  669. };
  670. /*
  671. * Truncation record. Records a partial truncation so that it may be
  672. * completed at check time. Also used for sync records.
  673. */
  674. struct jtrncrec {
  675. uint32_t jt_op;
  676. uint32_t jt_ino;
  677. int64_t jt_size;
  678. uint32_t jt_extsize;
  679. uint32_t jt_pad[3];
  680. };
  681. union jrec {
  682. struct jsegrec rec_jsegrec;
  683. struct jrefrec rec_jrefrec;
  684. struct jmvrec rec_jmvrec;
  685. struct jblkrec rec_jblkrec;
  686. struct jtrncrec rec_jtrncrec;
  687. };
  688. #ifdef CTASSERT
  689. CTASSERT(sizeof(struct jsegrec) == JREC_SIZE);
  690. CTASSERT(sizeof(struct jrefrec) == JREC_SIZE);
  691. CTASSERT(sizeof(struct jmvrec) == JREC_SIZE);
  692. CTASSERT(sizeof(struct jblkrec) == JREC_SIZE);
  693. CTASSERT(sizeof(struct jtrncrec) == JREC_SIZE);
  694. CTASSERT(sizeof(union jrec) == JREC_SIZE);
  695. #endif
  696. extern int inside[], around[];
  697. extern uint8_t *fragtbl[];