1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)lfs.h 7.25 (Berkeley) 10/01/92 8 */ 9 10 #define LFS_LABELPAD 8192 /* LFS label size */ 11 #define LFS_SBPAD 8192 /* LFS superblock size */ 12 13 /* 14 * XXX 15 * This is a kluge and NEEDS to go away. 16 * 17 * Right now, ufs code handles most of the calls for directory operations 18 * such as create, mkdir, link, etc. As a result VOP_UPDATE is being 19 * called with waitfor set (since ffs does these things synchronously). 20 * Since LFS does not want to do these synchronously, we treat the last 21 * argument to lfs_update as a set of flags. If LFS_SYNC is set, then 22 * the update should be synchronous, if not, do it asynchronously. 23 * Unfortunately, this means that LFS won't work with NFS yet because 24 * NFS goes through paths that will make normal calls to ufs which will 25 * call lfs with a last argument of 1. 26 */ 27 #define LFS_SYNC 0x02 28 29 /* On-disk and in-memory checkpoint segment usage structure. */ 30 typedef struct segusage SEGUSE; 31 struct segusage { 32 u_long su_nbytes; /* number of live bytes */ 33 u_long su_lastmod; /* SEGUSE last modified timestamp */ 34 u_short su_nsums; /* number of summaries in segment */ 35 u_short su_ninos; /* number of inode blocks in seg */ 36 #define SEGUSE_ACTIVE 0x1 /* segment is currently being written */ 37 #define SEGUSE_DIRTY 0x2 /* segment has data in it */ 38 #define SEGUSE_SUPERBLOCK 0x4 /* segment contains a superblock */ 39 u_long su_flags; 40 }; 41 42 #define SEGUPB(fs) (1 << (fs)->lfs_sushift) 43 #define SEGTABSIZE_SU(fs) \ 44 (((fs)->lfs_nseg + SEGUPB(fs) - 1) >> (fs)->lfs_sushift) 45 46 /* On-disk file information. One per file with data blocks in the segment. */ 47 typedef struct finfo FINFO; 48 struct finfo { 49 u_long fi_nblocks; /* number of blocks */ 50 u_long fi_version; /* version number */ 51 u_long fi_ino; /* inode number */ 52 long fi_blocks[1]; /* array of logical block numbers */ 53 }; 54 55 /* On-disk and in-memory super block. */ 56 struct lfs { 57 #define LFS_MAGIC 0x070162 58 u_long lfs_magic; /* magic number */ 59 #define LFS_VERSION 1 60 u_long lfs_version; /* version number */ 61 62 u_long lfs_size; /* number of blocks in fs */ 63 u_long lfs_ssize; /* number of blocks per segment */ 64 u_long lfs_dsize; /* number of disk blocks in fs */ 65 u_long lfs_bsize; /* file system block size */ 66 u_long lfs_fsize; /* size of frag blocks in fs */ 67 u_long lfs_frag; /* number of frags in a block in fs */ 68 69 /* Checkpoint region. */ 70 ino_t lfs_free; /* start of the free list */ 71 u_long lfs_bfree; /* number of free disk blocks */ 72 u_long lfs_nfiles; /* number of allocated inodes */ 73 long lfs_avail; /* blocks available for writing */ 74 u_long lfs_uinodes; /* inodes in cache not yet on disk */ 75 daddr_t lfs_idaddr; /* inode file disk address */ 76 ino_t lfs_ifile; /* inode file inode number */ 77 daddr_t lfs_lastseg; /* address of last segment written */ 78 daddr_t lfs_nextseg; /* address of next segment to write */ 79 daddr_t lfs_curseg; /* current segment being written */ 80 daddr_t lfs_offset; /* offset in curseg for next partial */ 81 daddr_t lfs_lastpseg; /* address of last partial written */ 82 u_long lfs_tstamp; /* time stamp */ 83 84 /* These are configuration parameters. */ 85 u_long lfs_minfree; /* minimum percentage of free blocks */ 86 87 /* These fields can be computed from the others. */ 88 u_quad_t lfs_maxfilesize; /* maximum representable file size */ 89 u_long lfs_dbpseg; /* disk blocks per segment */ 90 u_long lfs_inopb; /* inodes per block */ 91 u_long lfs_ifpb; /* IFILE entries per block */ 92 u_long lfs_sepb; /* SEGUSE entries per block */ 93 u_long lfs_nindir; /* indirect pointers per block */ 94 u_long lfs_nseg; /* number of segments */ 95 u_long lfs_nspf; /* number of sectors per fragment */ 96 u_long lfs_cleansz; /* cleaner info size in blocks */ 97 u_long lfs_segtabsz; /* segment table size in blocks */ 98 99 u_long lfs_segmask; /* calculate offset within a segment */ 100 u_long lfs_segshift; /* fast mult/div for segments */ 101 u_long lfs_bmask; /* calc block offset from file offset */ 102 u_long lfs_bshift; /* calc block number from file offset */ 103 u_long lfs_ffmask; /* calc frag offset from file offset */ 104 u_long lfs_ffshift; /* fast mult/div for frag from file */ 105 u_long lfs_fbmask; /* calc frag offset from block offset */ 106 u_long lfs_fbshift; /* fast mult/div for frag from block */ 107 u_long lfs_fsbtodb; /* fsbtodb and dbtofsb shift constant */ 108 u_long lfs_sushift; /* fast mult/div for segusage table */ 109 110 #define LFS_MIN_SBINTERVAL 5 /* minimum superblock segment spacing */ 111 #define LFS_MAXNUMSB 10 /* superblock disk offsets */ 112 daddr_t lfs_sboffs[LFS_MAXNUMSB]; 113 114 /* These fields are set at mount time and are meaningless on disk. */ 115 struct vnode *lfs_ivnode; /* vnode for the ifile */ 116 u_long lfs_seglock; /* single-thread the segment writer */ 117 u_long lfs_iocount; /* number of ios pending */ 118 u_long lfs_writer; /* don't allow any dirops to start */ 119 u_long lfs_dirops; /* count of active directory ops */ 120 u_long lfs_doifile; /* Write ifile blocks on next write */ 121 u_long lfs_nactive; /* Number of segments since last ckp */ 122 u_char lfs_fmod; /* super block modified flag */ 123 u_char lfs_clean; /* file system is clean flag */ 124 u_char lfs_ronly; /* mounted read-only flag */ 125 u_char lfs_flags; /* currently unused flag */ 126 u_char lfs_fsmnt[MNAMELEN]; /* name mounted on */ 127 u_char pad[3]; /* long-align */ 128 129 /* Checksum; valid on disk. */ 130 u_long lfs_cksum; /* checksum for superblock checking */ 131 }; 132 133 /* 134 * Inode 0 is the out-of-band inode number, inode 1 is the inode number for 135 * the IFILE, the root inode is 2 and the lost+found inode is 3. 136 */ 137 138 /* Fixed inode numbers. */ 139 #define LFS_UNUSED_INUM 0 /* out of band inode number */ 140 #define LFS_IFILE_INUM 1 /* IFILE inode number */ 141 #define LOSTFOUNDINO 3 /* lost+found inode number */ 142 #define LFS_FIRST_INUM 4 /* first free inode number */ 143 144 /* 145 * Used to access the first spare of the dinode which we use to store 146 * the ifile number so we can identify them 147 */ 148 #define di_inum di_spare[0] 149 150 /* Address calculations for metadata located in the inode */ 151 #define S_INDIR(fs) -NDADDR 152 #define D_INDIR(fs) (S_INDIR(fs) - NINDIR(fs) - 1) 153 #define T_INDIR(fs) (D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1) 154 155 /* Structure used to pass around logical block paths. */ 156 typedef struct _indir { 157 long in_lbn; /* logical block number */ 158 int in_off; /* offset in buffer */ 159 } INDIR; 160 161 /* Unassigned disk address. */ 162 #define UNASSIGNED -1 163 164 /* Unused logical block number */ 165 #define LFS_UNUSED_LBN -1 166 167 typedef struct ifile IFILE; 168 struct ifile { 169 u_long if_version; /* inode version number */ 170 #define LFS_UNUSED_DADDR 0 /* out-of-band daddr */ 171 daddr_t if_daddr; /* inode disk address */ 172 ino_t if_nextfree; /* next-unallocated inode */ 173 }; 174 175 /* 176 * Cleaner information structure. This resides in the ifile and is used 177 * to pass information between the cleaner and the kernel. 178 */ 179 typedef struct _cleanerinfo { 180 u_long clean; /* K: number of clean segments */ 181 u_long dirty; /* K: number of dirty segments */ 182 } CLEANERINFO; 183 184 #define CLEANSIZE_SU(fs) \ 185 ((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift) 186 187 /* 188 * All summary blocks are the same size, so we can always read a summary 189 * block easily from a segment. 190 */ 191 #define LFS_SUMMARY_SIZE 512 192 193 /* On-disk segment summary information */ 194 typedef struct segsum SEGSUM; 195 struct segsum { 196 u_long ss_sumsum; /* check sum of summary block */ 197 u_long ss_datasum; /* check sum of data */ 198 daddr_t ss_next; /* next segment */ 199 u_long ss_create; /* creation time stamp */ 200 u_short ss_nfinfo; /* number of file info structures */ 201 u_short ss_ninos; /* number of inodes in summary */ 202 #define SS_DIROP 0x01 /* segment begins a dirop */ 203 #define SS_CONT 0x02 /* more partials to finish this write*/ 204 u_short ss_flags; /* used for directory operations */ 205 u_short ss_pad; /* extra space */ 206 /* FINFO's and inode daddr's... */ 207 }; 208 209 /* NINDIR is the number of indirects in a file system block. */ 210 #define NINDIR(fs) ((fs)->lfs_nindir) 211 212 /* INOPB is the number of inodes in a secondary storage block. */ 213 #define INOPB(fs) ((fs)->lfs_inopb) 214 215 #define blksize(fs) ((fs)->lfs_bsize) 216 #define blkoff(fs, loc) ((loc) & (fs)->lfs_bmask) 217 #define fsbtodb(fs, b) ((b) << (fs)->lfs_fsbtodb) 218 #define dbtofsb(fs, b) ((b) >> (fs)->lfs_fsbtodb) 219 #define lblkno(fs, loc) ((loc) >> (fs)->lfs_bshift) 220 #define lblktosize(fs, blk) ((blk) << (fs)->lfs_bshift) 221 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \ 222 ((loc) >> (fs)->lfs_bshift) 223 224 #define datosn(fs, daddr) /* disk address to segment number */ \ 225 (((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize)) 226 #define sntoda(fs, sn) /* segment number to disk address */ \ 227 ((daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) + \ 228 (fs)->lfs_sboffs[0])) 229 230 /* Read in the block with the cleaner info from the ifile. */ 231 #define LFS_CLEANERINFO(CP, F, BP) { \ 232 VTOI((F)->lfs_ivnode)->i_flag |= IACC; \ 233 if (bread((F)->lfs_ivnode, (daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP))) \ 234 panic("lfs: ifile read"); \ 235 (CP) = (CLEANERINFO *)(BP)->b_un.b_addr; \ 236 } 237 238 /* Read in the block with a specific inode from the ifile. */ 239 #define LFS_IENTRY(IP, F, IN, BP) { \ 240 int _e; \ 241 VTOI((F)->lfs_ivnode)->i_flag |= IACC; \ 242 if (_e = bread((F)->lfs_ivnode, \ 243 (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz, \ 244 (F)->lfs_bsize, NOCRED, &(BP))) \ 245 panic("lfs: ifile read %d", _e); \ 246 (IP) = (IFILE *)(BP)->b_un.b_addr + (IN) % (F)->lfs_ifpb; \ 247 } 248 249 /* Read in the block with a specific segment usage entry from the ifile. */ 250 #define LFS_SEGENTRY(SP, F, IN, BP) { \ 251 int _e; \ 252 VTOI((F)->lfs_ivnode)->i_flag |= IACC; \ 253 if (_e = bread((F)->lfs_ivnode, \ 254 ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz, \ 255 (F)->lfs_bsize, NOCRED, &(BP))) \ 256 panic("lfs: ifile read: %d", _e); \ 257 (SP) = (SEGUSE *)(BP)->b_un.b_addr + ((IN) & (F)->lfs_sepb - 1); \ 258 } 259 260 /* 261 * Determine if there is enough room currently available to write db 262 * disk blocks. We need enough blocks for the new blocks, the current, 263 * inode blocks, a summary block, plus potentially the ifile inode and 264 * the segment usage table, plus an ifile page. 265 */ 266 #define LFS_FITS(fs, db) \ 267 ((long)((db + ((fs)->lfs_uinodes + INOPB((fs))) / INOPB((fs)) + \ 268 fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE + (fs)->lfs_segtabsz))\ 269 < (fs)->lfs_avail) 270 271 /* Determine if a buffer belongs to the ifile */ 272 #define IS_IFILE(bp) (VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM) 273 /* 274 * Structures used by lfs_bmapv and lfs_markv to communicate information 275 * about inodes and data blocks. 276 */ 277 typedef struct block_info { 278 ino_t bi_inode; /* inode # */ 279 daddr_t bi_lbn; /* logical block w/in file */ 280 daddr_t bi_daddr; /* disk address of block */ 281 time_t bi_segcreate; /* origin segment create time */ 282 int bi_version; /* file version number */ 283 void *bi_bp; /* data buffer */ 284 } BLOCK_INFO; 285 286 /* In-memory description of a segment about to be written. */ 287 struct segment { 288 struct lfs *fs; /* file system pointer */ 289 struct buf **bpp; /* pointer to buffer array */ 290 struct buf **cbpp; /* pointer to next available bp */ 291 struct buf **start_bpp; /* pointer to first bp in this set */ 292 struct buf *ibp; /* buffer pointer to inode page */ 293 struct finfo *fip; /* current fileinfo pointer */ 294 struct vnode *vp; /* vnode being gathered */ 295 void *segsum; /* segment summary info */ 296 u_long ninodes; /* number of inodes in this segment */ 297 u_long seg_bytes_left; /* bytes left in segment */ 298 u_long sum_bytes_left; /* bytes left in summary block */ 299 u_long seg_number; /* number of this segment */ 300 daddr_t *start_lbp; /* beginning lbn for this set */ 301 #define SEGM_CKP 0x01 /* doing a checkpoint */ 302 #define SEGM_CLEAN 0x02 /* cleaner call; don't sort */ 303 u_long seg_flags; /* run-time flags for this segment */ 304 }; 305 306 #define ISSPACE(F, BB, C) \ 307 (((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) || \ 308 ((C)->cr_uid != 0 && IS_FREESPACE(F, BB))) 309 310 #define IS_FREESPACE(F, BB) \ 311 ((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB))) 312 313 #define ISSPACE_XXX(F, BB) \ 314 ((F)->lfs_bfree >= (BB)) 315 316