1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)lfs.h 8.3 (Berkeley) 09/23/93 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 segment *lfs_sp; /* current segment being written */ 116 struct vnode *lfs_ivnode; /* vnode for the ifile */ 117 u_long lfs_seglock; /* single-thread the segment writer */ 118 pid_t lfs_lockpid; /* pid of lock holder */ 119 u_long lfs_iocount; /* number of ios pending */ 120 u_long lfs_writer; /* don't allow any dirops to start */ 121 u_long lfs_dirops; /* count of active directory ops */ 122 u_long lfs_doifile; /* Write ifile blocks on next write */ 123 u_long lfs_nactive; /* Number of segments since last ckp */ 124 u_char lfs_fmod; /* super block modified flag */ 125 u_char lfs_clean; /* file system is clean flag */ 126 u_char lfs_ronly; /* mounted read-only flag */ 127 u_char lfs_flags; /* currently unused flag */ 128 u_char lfs_fsmnt[MNAMELEN]; /* name mounted on */ 129 u_char pad[3]; /* long-align */ 130 131 /* Checksum; valid on disk. */ 132 u_long lfs_cksum; /* checksum for superblock checking */ 133 }; 134 135 /* 136 * Inode 0 is the out-of-band inode number, inode 1 is the inode number for 137 * the IFILE, the root inode is 2 and the lost+found inode is 3. 138 */ 139 140 /* Fixed inode numbers. */ 141 #define LFS_UNUSED_INUM 0 /* out of band inode number */ 142 #define LFS_IFILE_INUM 1 /* IFILE inode number */ 143 #define LOSTFOUNDINO 3 /* lost+found inode number */ 144 #define LFS_FIRST_INUM 4 /* first free inode number */ 145 146 /* Address calculations for metadata located in the inode */ 147 #define S_INDIR(fs) -NDADDR 148 #define D_INDIR(fs) (S_INDIR(fs) - NINDIR(fs) - 1) 149 #define T_INDIR(fs) (D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1) 150 151 /* Unassigned disk address. */ 152 #define UNASSIGNED -1 153 154 /* Unused logical block number */ 155 #define LFS_UNUSED_LBN -1 156 157 typedef struct ifile IFILE; 158 struct ifile { 159 u_long if_version; /* inode version number */ 160 #define LFS_UNUSED_DADDR 0 /* out-of-band daddr */ 161 daddr_t if_daddr; /* inode disk address */ 162 ino_t if_nextfree; /* next-unallocated inode */ 163 }; 164 165 /* 166 * Cleaner information structure. This resides in the ifile and is used 167 * to pass information between the cleaner and the kernel. 168 */ 169 typedef struct _cleanerinfo { 170 u_long clean; /* K: number of clean segments */ 171 u_long dirty; /* K: number of dirty segments */ 172 } CLEANERINFO; 173 174 #define CLEANSIZE_SU(fs) \ 175 ((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift) 176 177 /* 178 * All summary blocks are the same size, so we can always read a summary 179 * block easily from a segment. 180 */ 181 #define LFS_SUMMARY_SIZE 512 182 183 /* On-disk segment summary information */ 184 typedef struct segsum SEGSUM; 185 struct segsum { 186 u_long ss_sumsum; /* check sum of summary block */ 187 u_long ss_datasum; /* check sum of data */ 188 daddr_t ss_next; /* next segment */ 189 u_long ss_create; /* creation time stamp */ 190 u_short ss_nfinfo; /* number of file info structures */ 191 u_short ss_ninos; /* number of inodes in summary */ 192 #define SS_DIROP 0x01 /* segment begins a dirop */ 193 #define SS_CONT 0x02 /* more partials to finish this write*/ 194 u_short ss_flags; /* used for directory operations */ 195 u_short ss_pad; /* extra space */ 196 /* FINFO's and inode daddr's... */ 197 }; 198 199 /* NINDIR is the number of indirects in a file system block. */ 200 #define NINDIR(fs) ((fs)->lfs_nindir) 201 202 /* INOPB is the number of inodes in a secondary storage block. */ 203 #define INOPB(fs) ((fs)->lfs_inopb) 204 205 #define blksize(fs) ((fs)->lfs_bsize) 206 #define blkoff(fs, loc) ((loc) & (fs)->lfs_bmask) 207 #define fsbtodb(fs, b) ((b) << (fs)->lfs_fsbtodb) 208 #define dbtofsb(fs, b) ((b) >> (fs)->lfs_fsbtodb) 209 #define lblkno(fs, loc) ((loc) >> (fs)->lfs_bshift) 210 #define lblktosize(fs, blk) ((blk) << (fs)->lfs_bshift) 211 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \ 212 ((loc) >> (fs)->lfs_bshift) 213 214 #define datosn(fs, daddr) /* disk address to segment number */ \ 215 (((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize)) 216 #define sntoda(fs, sn) /* segment number to disk address */ \ 217 ((daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) + \ 218 (fs)->lfs_sboffs[0])) 219 220 /* Read in the block with the cleaner info from the ifile. */ 221 #define LFS_CLEANERINFO(CP, F, BP) { \ 222 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \ 223 if (bread((F)->lfs_ivnode, \ 224 (daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP))) \ 225 panic("lfs: ifile read"); \ 226 (CP) = (CLEANERINFO *)(BP)->b_data; \ 227 } 228 229 /* Read in the block with a specific inode from the ifile. */ 230 #define LFS_IENTRY(IP, F, IN, BP) { \ 231 int _e; \ 232 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \ 233 if (_e = bread((F)->lfs_ivnode, \ 234 (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\ 235 (F)->lfs_bsize, NOCRED, &(BP))) \ 236 panic("lfs: ifile read %d", _e); \ 237 (IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb; \ 238 } 239 240 /* Read in the block with a specific segment usage entry from the ifile. */ 241 #define LFS_SEGENTRY(SP, F, IN, BP) { \ 242 int _e; \ 243 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \ 244 if (_e = bread((F)->lfs_ivnode, \ 245 ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz, \ 246 (F)->lfs_bsize, NOCRED, &(BP))) \ 247 panic("lfs: ifile read: %d", _e); \ 248 (SP) = (SEGUSE *)(BP)->b_data + ((IN) & (F)->lfs_sepb - 1); \ 249 } 250 251 /* 252 * Determine if there is enough room currently available to write db 253 * disk blocks. We need enough blocks for the new blocks, the current, 254 * inode blocks, a summary block, plus potentially the ifile inode and 255 * the segment usage table, plus an ifile page. 256 */ 257 #define LFS_FITS(fs, db) \ 258 ((long)((db + ((fs)->lfs_uinodes + INOPB((fs))) / INOPB((fs)) + \ 259 fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE + \ 260 (fs)->lfs_segtabsz)) < (fs)->lfs_avail) 261 262 /* Determine if a buffer belongs to the ifile */ 263 #define IS_IFILE(bp) (VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM) 264 265 /* 266 * Structures used by lfs_bmapv and lfs_markv to communicate information 267 * about inodes and data blocks. 268 */ 269 typedef struct block_info { 270 ino_t bi_inode; /* inode # */ 271 daddr_t bi_lbn; /* logical block w/in file */ 272 daddr_t bi_daddr; /* disk address of block */ 273 time_t bi_segcreate; /* origin segment create time */ 274 int bi_version; /* file version number */ 275 void *bi_bp; /* data buffer */ 276 } BLOCK_INFO; 277 278 /* In-memory description of a segment about to be written. */ 279 struct segment { 280 struct lfs *fs; /* file system pointer */ 281 struct buf **bpp; /* pointer to buffer array */ 282 struct buf **cbpp; /* pointer to next available bp */ 283 struct buf **start_bpp; /* pointer to first bp in this set */ 284 struct buf *ibp; /* buffer pointer to inode page */ 285 struct finfo *fip; /* current fileinfo pointer */ 286 struct vnode *vp; /* vnode being gathered */ 287 void *segsum; /* segment summary info */ 288 u_long ninodes; /* number of inodes in this segment */ 289 u_long seg_bytes_left; /* bytes left in segment */ 290 u_long sum_bytes_left; /* bytes left in summary block */ 291 u_long seg_number; /* number of this segment */ 292 daddr_t *start_lbp; /* beginning lbn for this set */ 293 #define SEGM_CKP 0x01 /* doing a checkpoint */ 294 #define SEGM_CLEAN 0x02 /* cleaner call; don't sort */ 295 #define SEGM_SYNC 0x04 /* wait for segment */ 296 u_long seg_flags; /* run-time flags for this segment */ 297 }; 298 299 #define ISSPACE(F, BB, C) \ 300 (((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) || \ 301 ((C)->cr_uid != 0 && IS_FREESPACE(F, BB))) 302 303 #define IS_FREESPACE(F, BB) \ 304 ((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB))) 305 306 #define ISSPACE_XXX(F, BB) \ 307 ((F)->lfs_bfree >= (BB)) 308 309 #define DOSTATS 310 #ifdef DOSTATS 311 /* Statistics Counters */ 312 struct lfs_stats { 313 int segsused; 314 int psegwrites; 315 int psyncwrites; 316 int pcleanwrites; 317 int blocktot; 318 int cleanblocks; 319 int ncheckpoints; 320 int nwrites; 321 int nsync_writes; 322 int wait_exceeded; 323 int write_exceeded; 324 int flush_invoked; 325 }; 326 extern struct lfs_stats lfs_stats; 327 #endif 328