ufs.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Copyright (c) 1982, 1986 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms are permitted
  6. * provided that the above copyright notice and this paragraph are
  7. * duplicated in all such forms and that any documentation,
  8. * advertising materials, and other materials related to such
  9. * distribution and use acknowledge that the software was developed
  10. * by the University of California, Berkeley. The name of the
  11. * University may not be used to endorse or promote products derived
  12. * from this software without specific prior written permission.
  13. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16. */
  17. /*
  18. * Mach Operating System
  19. * Copyright (c) 1993 Carnegie Mellon University
  20. * All Rights Reserved.
  21. *
  22. * Permission to use, copy, modify and distribute this software and its
  23. * documentation is hereby granted, provided that both the copyright
  24. * notice and this permission notice appear in all copies of the
  25. * software, derivative works or modified versions, and any portions
  26. * thereof, and that both notices appear in supporting documentation.
  27. *
  28. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  29. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  30. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  31. *
  32. * Carnegie Mellon requests users of this software to return to
  33. *
  34. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  35. * School of Computer Science
  36. * Carnegie Mellon University
  37. * Pittsburgh PA 15213-3890
  38. *
  39. * any improvements or extensions that they make and grant Carnegie Mellon
  40. * the rights to redistribute these changes.
  41. */
  42. /*
  43. * HISTORY
  44. * $Log: ufs.h,v $
  45. * Revision 1.1.1.1 2004/04/25 20:38:21 vorlon
  46. * Initial import of upstream source
  47. *
  48. * Revision 1.1.1.1 2001/10/08 23:03:52 wgwoods
  49. * initial import of CVS source from alphalinux.org, plus a couple bugfixes
  50. *
  51. * Revision 1.1.1.1 2000/05/03 03:58:23 dhd
  52. * Initial import (from 0.7 release)
  53. *
  54. * Revision 2.3 93/03/09 10:49:48 danner
  55. * Make damn sure we get sys/types.h from the right place.
  56. * [93/03/05 af]
  57. *
  58. * Revision 2.2 93/02/05 08:01:43 danner
  59. * Adapted for alpha.
  60. * [93/02/04 af]
  61. *
  62. * Revision 2.2 90/08/27 21:45:05 dbg
  63. * Created.
  64. * [90/07/16 dbg]
  65. *
  66. */
  67. /*
  68. * Common definitions for Berkeley Fast File System.
  69. */
  70. #include <linux/types.h>
  71. #define DEV_BSIZE 512
  72. #ifndef NBBY
  73. #define NBBY 8
  74. #endif
  75. /*
  76. * The file system is made out of blocks of at most MAXBSIZE units,
  77. * with smaller units (fragments) only in the last direct block.
  78. * MAXBSIZE primarily determines the size of buffers in the buffer
  79. * pool. It may be made larger without any effect on existing
  80. * file systems; however, making it smaller may make some file
  81. * systems unmountable.
  82. *
  83. * Note that the disk devices are assumed to have DEV_BSIZE "sectors"
  84. * and that fragments must be some multiple of this size.
  85. */
  86. #define MAXBSIZE 8192
  87. #define MAXFRAG 8
  88. /*
  89. * MAXPATHLEN defines the longest permissible path length
  90. * after expanding symbolic links.
  91. *
  92. * MAXSYMLINKS defines the maximum number of symbolic links
  93. * that may be expanded in a path name. It should be set
  94. * high enough to allow all legitimate uses, but halt infinite
  95. * loops reasonably quickly.
  96. */
  97. #define MAXPATHLEN 1024
  98. #define MAXSYMLINKS 8
  99. /*
  100. * Error codes for file system errors.
  101. */
  102. #define FS_NOT_DIRECTORY 5000 /* not a directory */
  103. #define FS_NO_ENTRY 5001 /* name not found */
  104. #define FS_NAME_TOO_LONG 5002 /* name too long */
  105. #define FS_SYMLINK_LOOP 5003 /* symbolic link loop */
  106. #define FS_INVALID_FS 5004 /* bad file system */
  107. #define FS_NOT_IN_FILE 5005 /* offset not in file */
  108. #define FS_INVALID_PARAMETER 5006 /* bad parameter to
  109. a routine */
  110. /*
  111. * Each disk drive contains some number of file systems.
  112. * A file system consists of a number of cylinder groups.
  113. * Each cylinder group has inodes and data.
  114. *
  115. * A file system is described by its super-block, which in turn
  116. * describes the cylinder groups. The super-block is critical
  117. * data and is replicated in each cylinder group to protect against
  118. * catastrophic loss. This is done at `newfs' time and the critical
  119. * super-block data does not change, so the copies need not be
  120. * referenced further unless disaster strikes.
  121. *
  122. * For file system fs, the offsets of the various blocks of interest
  123. * are given in the super block as:
  124. * [fs->fs_sblkno] Super-block
  125. * [fs->fs_cblkno] Cylinder group block
  126. * [fs->fs_iblkno] Inode blocks
  127. * [fs->fs_dblkno] Data blocks
  128. * The beginning of cylinder group cg in fs, is given by
  129. * the ``cgbase(fs, cg)'' macro.
  130. *
  131. * The first boot and super blocks are given in absolute disk addresses.
  132. * The byte-offset forms are preferred, as they don't imply a sector size.
  133. */
  134. #define BBSIZE 8192
  135. #define SBSIZE 8192
  136. #define BBOFF ((off_t)(0))
  137. #define SBOFF ((off_t)(BBOFF + BBSIZE))
  138. #define BBLOCK ((__kernel_daddr_t)(0))
  139. #define SBLOCK ((__kernel_daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
  140. /*
  141. * Addresses stored in inodes are capable of addressing fragments
  142. * of `blocks'. File system blocks of at most size MAXBSIZE can
  143. * be optionally broken into 2, 4, or 8 pieces, each of which is
  144. * addressable; these pieces may be DEV_BSIZE, or some multiple of
  145. * a DEV_BSIZE unit.
  146. *
  147. * Large files consist of exclusively large data blocks. To avoid
  148. * undue wasted disk space, the last data block of a small file may be
  149. * allocated as only as many fragments of a large block as are
  150. * necessary. The file system format retains only a single pointer
  151. * to such a fragment, which is a piece of a single large block that
  152. * has been divided. The size of such a fragment is determinable from
  153. * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
  154. *
  155. * The file system records space availability at the fragment level;
  156. * to determine block availability, aligned fragments are examined.
  157. *
  158. * The root inode is the root of the file system.
  159. * Inode 0 can't be used for normal purposes and
  160. * historically bad blocks were linked to inode 1,
  161. * thus the root inode is 2. (inode 1 is no longer used for
  162. * this purpose, however numerous dump tapes make this
  163. * assumption, so we are stuck with it)
  164. */
  165. #define ROOTINO ((__kernel_ino_t)2) /* i number of all roots */
  166. /*
  167. * MINBSIZE is the smallest allowable block size.
  168. * In order to insure that it is possible to create files of size
  169. * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
  170. * MINBSIZE must be big enough to hold a cylinder group block,
  171. * thus changes to (struct cg) must keep its size within MINBSIZE.
  172. * Note that super blocks are always of size SBSIZE,
  173. * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
  174. */
  175. #define MINBSIZE 4096
  176. /*
  177. * The path name on which the file system is mounted is maintained
  178. * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
  179. * the super block for this name.
  180. * The limit on the amount of summary information per file system
  181. * is defined by MAXCSBUFS. It is currently parameterized for a
  182. * maximum of two million cylinders.
  183. */
  184. #define MAXMNTLEN 512
  185. #define MAXCSBUFS 32
  186. /*
  187. * Per cylinder group information; summarized in blocks allocated
  188. * from first cylinder group data blocks. These blocks have to be
  189. * read in from fs_csaddr (size fs_cssize) in addition to the
  190. * super block.
  191. *
  192. * N.B. sizeof(struct csum) must be a power of two in order for
  193. * the ``fs_cs'' macro to work (see below).
  194. */
  195. struct csum {
  196. int cs_ndir; /* number of directories */
  197. int cs_nbfree; /* number of free blocks */
  198. int cs_nifree; /* number of free inodes */
  199. int cs_nffree; /* number of free frags */
  200. };
  201. typedef int ext_time_t;
  202. typedef struct {
  203. unsigned int val[2];
  204. } quad;
  205. /*
  206. * Super block for a file system.
  207. */
  208. #define FS_MAGIC 0x011954
  209. struct fs
  210. {
  211. int xxx1; /* struct fs *fs_link;*/
  212. int xxx2; /* struct fs *fs_rlink;*/
  213. __kernel_daddr_t fs_sblkno; /* addr of super-block in filesys */
  214. __kernel_daddr_t fs_cblkno; /* offset of cyl-block in filesys */
  215. __kernel_daddr_t fs_iblkno; /* offset of inode-blocks in filesys */
  216. __kernel_daddr_t fs_dblkno; /* offset of first data after cg */
  217. int fs_cgoffset; /* cylinder group offset in cylinder */
  218. int fs_cgmask; /* used to calc mod fs_ntrak */
  219. ext_time_t fs_time; /* last time written */
  220. int fs_size; /* number of blocks in fs */
  221. int fs_dsize; /* number of data blocks in fs */
  222. int fs_ncg; /* number of cylinder groups */
  223. int fs_bsize; /* size of basic blocks in fs */
  224. int fs_fsize; /* size of frag blocks in fs */
  225. int fs_frag; /* number of frags in a block in fs */
  226. /* these are configuration parameters */
  227. int fs_minfree; /* minimum percentage of free blocks */
  228. int fs_rotdelay; /* num of ms for optimal next block */
  229. int fs_rps; /* disk revolutions per second */
  230. /* these fields can be computed from the others */
  231. int fs_bmask; /* ``blkoff'' calc of blk offsets */
  232. int fs_fmask; /* ``fragoff'' calc of frag offsets */
  233. int fs_bshift; /* ``lblkno'' calc of logical blkno */
  234. int fs_fshift; /* ``numfrags'' calc number of frags */
  235. /* these are configuration parameters */
  236. int fs_maxcontig; /* max number of contiguous blks */
  237. int fs_maxbpg; /* max number of blks per cyl group */
  238. /* these fields can be computed from the others */
  239. int fs_fragshift; /* block to frag shift */
  240. int fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
  241. int fs_sbsize; /* actual size of super block */
  242. int fs_csmask; /* csum block offset */
  243. int fs_csshift; /* csum block number */
  244. int fs_nindir; /* value of NINDIR */
  245. int fs_inopb; /* value of INOPB */
  246. int fs_nspf; /* value of NSPF */
  247. /* yet another configuration parameter */
  248. int fs_optim; /* optimization preference, see below */
  249. /* these fields are derived from the hardware */
  250. int fs_npsect; /* # sectors/track including spares */
  251. int fs_interleave; /* hardware sector interleave */
  252. int fs_trackskew; /* sector 0 skew, per track */
  253. int fs_headswitch; /* head switch time, usec */
  254. int fs_trkseek; /* track-to-track seek, usec */
  255. /* sizes determined by number of cylinder groups and their sizes */
  256. __kernel_daddr_t fs_csaddr; /* blk addr of cyl grp summary area */
  257. int fs_cssize; /* size of cyl grp summary area */
  258. int fs_cgsize; /* cylinder group size */
  259. /* these fields are derived from the hardware */
  260. int fs_ntrak; /* tracks per cylinder */
  261. int fs_nsect; /* sectors per track */
  262. int fs_spc; /* sectors per cylinder */
  263. /* this comes from the disk driver partitioning */
  264. int fs_ncyl; /* cylinders in file system */
  265. /* these fields can be computed from the others */
  266. int fs_cpg; /* cylinders per group */
  267. int fs_ipg; /* inodes per group */
  268. int fs_fpg; /* blocks per group * fs_frag */
  269. /* this data must be re-computed after crashes */
  270. struct csum fs_cstotal; /* cylinder summary information */
  271. /* these fields are cleared at mount time */
  272. char fs_fmod; /* super block modified flag */
  273. char fs_clean; /* file system is clean flag */
  274. char fs_ronly; /* mounted read-only flag */
  275. char fs_flags; /* currently unused flag */
  276. char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
  277. /* these fields retain the current block allocation info */
  278. int fs_cgrotor; /* last cg searched */
  279. #ifdef __alpha__
  280. int was_fs_csp[MAXCSBUFS]; /* unused on Alpha */
  281. #else
  282. struct csum *fs_csp[MAXCSBUFS];/* list of fs_cs info buffers */
  283. #endif
  284. int fs_cpc; /* cyl per cycle in postbl */
  285. short fs_opostbl[16][8]; /* old rotation block list head */
  286. int fs_sparecon[56]; /* reserved for future constants */
  287. quad fs_qbmask; /* ~fs_bmask - for use with quad size */
  288. quad fs_qfmask; /* ~fs_fmask - for use with quad size */
  289. int fs_postblformat; /* format of positional layout tables */
  290. int fs_nrpos; /* number of rotaional positions */
  291. int fs_postbloff; /* (short) rotation block list head */
  292. int fs_rotbloff; /* (u_char) blocks for each rotation */
  293. int fs_magic; /* magic number */
  294. unsigned char fs_space[1]; /* list of blocks for each rotation */
  295. /* actually longer */
  296. };
  297. /*
  298. * Preference for optimization.
  299. */
  300. #define FS_OPTTIME 0 /* minimize allocation time */
  301. #define FS_OPTSPACE 1 /* minimize disk fragmentation */
  302. /*
  303. * Rotational layout table format types
  304. */
  305. #define FS_42POSTBLFMT -1 /* 4.2BSD rotational table format */
  306. #define FS_DYNAMICPOSTBLFMT 1 /* dynamic rotational table format */
  307. /*
  308. * Macros for access to superblock array structures
  309. */
  310. #define fs_postbl(fs, cylno) \
  311. (((fs)->fs_postblformat == FS_42POSTBLFMT) \
  312. ? ((fs)->fs_opostbl[cylno]) \
  313. : ((short *)((char *)(fs) + (fs)->fs_postbloff) + (cylno) * (fs)->fs_nrpos))
  314. #define fs_rotbl(fs) \
  315. (((fs)->fs_postblformat == FS_42POSTBLFMT) \
  316. ? ((fs)->fs_space) \
  317. : ((unsigned char *)((char *)(fs) + (fs)->fs_rotbloff)))
  318. /*
  319. * Convert cylinder group to base address of its global summary info.
  320. *
  321. * N.B. This macro assumes that sizeof(struct csum) is a power of two.
  322. */
  323. #define fs_cs(fs, indx) \
  324. fs_csp[(indx) >> (fs)->fs_csshift][(indx) & ~(fs)->fs_csmask]
  325. /*
  326. * Cylinder group block for a file system.
  327. */
  328. #define CG_MAGIC 0x090255
  329. struct cg {
  330. int xxx1; /* struct cg *cg_link;*/
  331. int cg_magic; /* magic number */
  332. ext_time_t cg_time; /* time last written */
  333. int cg_cgx; /* we are the cgx'th cylinder group */
  334. short cg_ncyl; /* number of cyl's this cg */
  335. short cg_niblk; /* number of inode blocks this cg */
  336. int cg_ndblk; /* number of data blocks this cg */
  337. struct csum cg_cs; /* cylinder summary information */
  338. int cg_rotor; /* position of last used block */
  339. int cg_frotor; /* position of last used frag */
  340. int cg_irotor; /* position of last used inode */
  341. int cg_frsum[MAXFRAG]; /* counts of available frags */
  342. int cg_btotoff; /* (long) block totals per cylinder */
  343. int cg_boff; /* (short) free block positions */
  344. int cg_iusedoff; /* (char) used inode map */
  345. int cg_freeoff; /* (u_char) free block map */
  346. int cg_nextfreeoff; /* (u_char) next available space */
  347. int cg_sparecon[16]; /* reserved for future use */
  348. unsigned char cg_space[1]; /* space for cylinder group maps */
  349. /* actually longer */
  350. };
  351. /*
  352. * Macros for access to cylinder group array structures
  353. */
  354. #define cg_blktot(cgp) \
  355. (((cgp)->cg_magic != CG_MAGIC) \
  356. ? (((struct ocg *)(cgp))->cg_btot) \
  357. : ((int *)((char *)(cgp) + (cgp)->cg_btotoff)))
  358. #define cg_blks(fs, cgp, cylno) \
  359. (((cgp)->cg_magic != CG_MAGIC) \
  360. ? (((struct ocg *)(cgp))->cg_b[cylno]) \
  361. : ((short *)((char *)(cgp) + (cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos))
  362. #define cg_inosused(cgp) \
  363. (((cgp)->cg_magic != CG_MAGIC) \
  364. ? (((struct ocg *)(cgp))->cg_iused) \
  365. : ((char *)((char *)(cgp) + (cgp)->cg_iusedoff)))
  366. #define cg_blksfree(cgp) \
  367. (((cgp)->cg_magic != CG_MAGIC) \
  368. ? (((struct ocg *)(cgp))->cg_free) \
  369. : ((unsigned char *)((char *)(cgp) + (cgp)->cg_freeoff)))
  370. #define cg_chkmagic(cgp) \
  371. ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
  372. /*
  373. * The following structure is defined
  374. * for compatibility with old file systems.
  375. */
  376. struct ocg {
  377. int xxx1; /* struct ocg *cg_link;*/
  378. int xxx2; /* struct ocg *cg_rlink;*/
  379. ext_time_t cg_time; /* time last written */
  380. int cg_cgx; /* we are the cgx'th cylinder group */
  381. short cg_ncyl; /* number of cyl's this cg */
  382. short cg_niblk; /* number of inode blocks this cg */
  383. int cg_ndblk; /* number of data blocks this cg */
  384. struct csum cg_cs; /* cylinder summary information */
  385. int cg_rotor; /* position of last used block */
  386. int cg_frotor; /* position of last used frag */
  387. int cg_irotor; /* position of last used inode */
  388. int cg_frsum[8]; /* counts of available frags */
  389. int cg_btot[32]; /* block totals per cylinder */
  390. short cg_b[32][8]; /* positions of free blocks */
  391. char cg_iused[256]; /* used inode map */
  392. int cg_magic; /* magic number */
  393. unsigned char cg_free[1]; /* free block map */
  394. /* actually longer */
  395. };
  396. /*
  397. * Turn file system block numbers into disk block addresses.
  398. * This maps file system blocks to device size blocks.
  399. */
  400. #define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb)
  401. #define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
  402. /*
  403. * Cylinder group macros to locate things in cylinder groups.
  404. * They calc file system addresses of cylinder group data structures.
  405. */
  406. #define cgbase(fs, c) ((__kernel_daddr_t)((fs)->fs_fpg * (c)))
  407. #define cgstart(fs, c) \
  408. (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
  409. #define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
  410. #define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
  411. #define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
  412. #define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
  413. /*
  414. * Macros for handling inode numbers:
  415. * inode number to file system block offset.
  416. * inode number to cylinder group number.
  417. * inode number to file system block address.
  418. */
  419. #define itoo(fs, x) ((x) % INOPB(fs))
  420. #define itog(fs, x) ((x) / (fs)->fs_ipg)
  421. #define itod(fs, x) \
  422. ((__kernel_daddr_t)(cgimin(fs, itog(fs, x)) + \
  423. (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
  424. /*
  425. * Give cylinder group number for a file system block.
  426. * Give cylinder group block number for a file system block.
  427. */
  428. #define dtog(fs, d) ((d) / (fs)->fs_fpg)
  429. #define dtogd(fs, d) ((d) % (fs)->fs_fpg)
  430. /*
  431. * Extract the bits for a block from a map.
  432. * Compute the cylinder and rotational position of a cyl block addr.
  433. */
  434. #define blkmap(fs, map, loc) \
  435. (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
  436. #define cbtocylno(fs, bno) \
  437. ((bno) * NSPF(fs) / (fs)->fs_spc)
  438. #define cbtorpos(fs, bno) \
  439. (((bno) * NSPF(fs) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \
  440. (bno) * NSPF(fs) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \
  441. (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect)
  442. /*
  443. * The following macros optimize certain frequently calculated
  444. * quantities by using shifts and masks in place of divisions
  445. * modulos and multiplications.
  446. */
  447. #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
  448. ((loc) & ~(fs)->fs_bmask)
  449. #define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
  450. ((loc) & ~(fs)->fs_fmask)
  451. #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
  452. ((loc) >> (fs)->fs_bshift)
  453. #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
  454. ((loc) >> (fs)->fs_fshift)
  455. #define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
  456. (((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask)
  457. #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
  458. (((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask)
  459. #define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \
  460. ((frags) >> (fs)->fs_fragshift)
  461. #define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \
  462. ((blks) << (fs)->fs_fragshift)
  463. #define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \
  464. ((fsb) & ((fs)->fs_frag - 1))
  465. #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \
  466. ((fsb) &~ ((fs)->fs_frag - 1))
  467. /*
  468. * Determine the number of available frags given a
  469. * percentage to hold in reserve
  470. */
  471. #define freespace(fs, percentreserved) \
  472. (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
  473. (fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100))
  474. /*
  475. * Determining the size of a file block in the file system.
  476. */
  477. #define blksize(fs, ip, lbn) \
  478. (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->fs_bshift) \
  479. ? (fs)->fs_bsize \
  480. : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
  481. #define dblksize(fs, dip, lbn) \
  482. (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
  483. ? (fs)->fs_bsize \
  484. : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
  485. /*
  486. * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
  487. */
  488. #define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift)
  489. #define NSPF(fs) ((fs)->fs_nspf)
  490. /*
  491. * INOPB is the number of inodes in a secondary storage block.
  492. */
  493. #define INOPB(fs) ((fs)->fs_inopb)
  494. #define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
  495. /*
  496. * NINDIR is the number of indirects in a file system block.
  497. */
  498. #define NINDIR(fs) ((fs)->fs_nindir)
  499. /*
  500. * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
  501. * All rights reserved.
  502. *
  503. * Redistribution and use in source and binary forms are permitted
  504. * provided that the above copyright notice and this paragraph are
  505. * duplicated in all such forms and that any documentation,
  506. * advertising materials, and other materials related to such
  507. * distribution and use acknowledge that the software was developed
  508. * by the University of California, Berkeley. The name of the
  509. * University may not be used to endorse or promote products derived
  510. * from this software without specific prior written permission.
  511. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  512. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  513. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  514. *
  515. * @(#)dir.h 7.6 (Berkeley) 5/9/89
  516. */
  517. /*
  518. * A directory consists of some number of blocks of DIRBLKSIZ
  519. * bytes, where DIRBLKSIZ is chosen such that it can be transferred
  520. * to disk in a single atomic operation (e.g. 512 bytes on most machines).
  521. *
  522. * Each DIRBLKSIZ byte block contains some number of directory entry
  523. * structures, which are of variable length. Each directory entry has
  524. * a struct direct at the front of it, containing its inode number,
  525. * the length of the entry, and the length of the name contained in
  526. * the entry. These are followed by the name padded to a 4 byte boundary
  527. * with null bytes. All names are guaranteed null terminated.
  528. * The maximum length of a name in a directory is MAXNAMLEN.
  529. *
  530. * The macro DIRSIZ(dp) gives the amount of space required to represent
  531. * a directory entry. Free space in a directory is represented by
  532. * entries which have dp->d_reclen > DIRSIZ(dp). All DIRBLKSIZ bytes
  533. * in a directory block are claimed by the directory entries. This
  534. * usually results in the last entry in a directory having a large
  535. * dp->d_reclen. When entries are deleted from a directory, the
  536. * space is returned to the previous entry in the same directory
  537. * block by increasing its dp->d_reclen. If the first entry of
  538. * a directory block is free, then its dp->d_ino is set to 0.
  539. * Entries other than the first in a directory do not normally have
  540. * dp->d_ino set to 0.
  541. */
  542. #define DIRBLKSIZ DEV_BSIZE
  543. #define MAXNAMLEN 255
  544. struct direct {
  545. unsigned int d_ino; /* inode number of entry */
  546. unsigned short d_reclen; /* length of this record */
  547. unsigned short d_namlen; /* length of string in d_name */
  548. char d_name[MAXNAMLEN + 1]; /* name with length <= MAXNAMLEN */
  549. };
  550. /*
  551. * The DIRSIZ macro gives the minimum record length which will hold
  552. * the directory entry. This requires the amount of space in struct direct
  553. * without the d_name field, plus enough space for the name with a terminating
  554. * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
  555. */
  556. #undef DIRSIZ
  557. #define DIRSIZ(dp) \
  558. ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
  559. /*
  560. * The I node is the focus of all file activity in the BSD Fast File System.
  561. * There is a unique inode allocated for each active file,
  562. * each current directory, each mounted-on file, text file, and the root.
  563. * An inode is 'named' by its dev/inumber pair. (iget/iget.c)
  564. * Data in icommon is read in from permanent inode on volume.
  565. */
  566. #define NDADDR 12 /* direct addresses in inode */
  567. #define NIADDR 3 /* indirect addresses in inode */
  568. #define MAX_FASTLINK_SIZE ((NDADDR + NIADDR) * sizeof(__kernel_daddr_t))
  569. struct icommon {
  570. unsigned short ic_mode; /* 0: mode and type of file */
  571. short ic_nlink; /* 2: number of links to file */
  572. unsigned short ic_uid; /* 4: owner's user id */
  573. unsigned short ic_gid; /* 6: owner's group id */
  574. long ic_size; /* 8: number of bytes in file */
  575. ext_time_t ic_atime; /* 16: time last accessed */
  576. int ic_atspare;
  577. ext_time_t ic_mtime; /* 24: time last modified */
  578. int ic_mtspare;
  579. ext_time_t ic_ctime; /* 32: last time inode changed */
  580. int ic_ctspare;
  581. union {
  582. struct {
  583. __kernel_daddr_t Mb_db[NDADDR]; /* 40: disk block addresses */
  584. __kernel_daddr_t Mb_ib[NIADDR]; /* 88: indirect blocks */
  585. } ic_Mb;
  586. char ic_Msymlink[MAX_FASTLINK_SIZE];
  587. /* 40: symbolic link name */
  588. } ic_Mun;
  589. #define ic_db ic_Mun.ic_Mb.Mb_db
  590. #define ic_ib ic_Mun.ic_Mb.Mb_ib
  591. #define ic_symlink ic_Mun.ic_Msymlink
  592. int ic_flags; /* 100: status, currently unused */
  593. #define IC_FASTLINK 0x0001 /* Symbolic link in inode */
  594. int ic_blocks; /* 104: blocks actually held */
  595. int ic_gen; /* 108: generation number */
  596. int ic_spare[4]; /* 112: reserved, currently unused */
  597. };
  598. #define i_mode i_ic.ic_mode
  599. #define i_nlink i_ic.ic_nlink
  600. #define i_uid i_ic.ic_uid
  601. #define i_gid i_ic.ic_gid
  602. #if BYTE_MSF
  603. #define i_size i_ic.ic_size
  604. #else /* BYTE_LSF */
  605. #define i_size i_ic.ic_size
  606. #endif
  607. #define i_db i_ic.ic_db
  608. #define i_ib i_ic.ic_ib
  609. #define i_atime i_ic.ic_atime
  610. #define i_mtime i_ic.ic_mtime
  611. #define i_ctime i_ic.ic_ctime
  612. #define i_blocks i_ic.ic_blocks
  613. #define i_rdev i_ic.ic_db[0]
  614. #define i_symlink i_ic.ic_symlink
  615. #define i_flags i_ic.ic_flags
  616. #define i_gen i_ic.ic_gen
  617. /* modes */
  618. #define IFMT 0170000 /* type of file */
  619. #define IFCHR 0020000 /* character special */
  620. #define IFDIR 0040000 /* directory */
  621. #define IFBLK 0060000 /* block special */
  622. #define IFREG 0100000 /* regular */
  623. #define IFLNK 0120000 /* symbolic link */
  624. #define IFSOCK 0140000 /* socket */
  625. #define ISUID 04000 /* set user id on execution */
  626. #define ISGID 02000 /* set group id on execution */
  627. #define ISVTX 01000 /* save swapped text even after use */
  628. #define IREAD 0400 /* read, write, execute permissions */
  629. #define IWRITE 0200
  630. #define IEXEC 0100
  631. /*
  632. * Same structure, but on disk.
  633. */
  634. struct dinode {
  635. union {
  636. struct icommon di_com;
  637. char di_char[128];
  638. } di_un;
  639. };
  640. #define di_ic di_un.di_com
  641. struct dev_t {
  642. int handle;
  643. unsigned int first_block;
  644. unsigned int last_block;
  645. };
  646. typedef unsigned int recnum_t;