151139Sbostic /*- 263375Sbostic * Copyright (c) 1991, 1993 363375Sbostic * The Regents of the University of California. All rights reserved. 451139Sbostic * 551139Sbostic * %sccs.include.redist.c% 651139Sbostic * 7*69284Smckusick * @(#)lfs.h 8.9 (Berkeley) 05/08/95 851139Sbostic */ 951139Sbostic 1051140Sbostic #define LFS_LABELPAD 8192 /* LFS label size */ 1151140Sbostic #define LFS_SBPAD 8192 /* LFS superblock size */ 1251139Sbostic 1355546Sbostic /* 1455546Sbostic * XXX 1555546Sbostic * This is a kluge and NEEDS to go away. 1655546Sbostic * 1755546Sbostic * Right now, ufs code handles most of the calls for directory operations 1855546Sbostic * such as create, mkdir, link, etc. As a result VOP_UPDATE is being 1955546Sbostic * called with waitfor set (since ffs does these things synchronously). 2055546Sbostic * Since LFS does not want to do these synchronously, we treat the last 2155546Sbostic * argument to lfs_update as a set of flags. If LFS_SYNC is set, then 2255546Sbostic * the update should be synchronous, if not, do it asynchronously. 2355546Sbostic * Unfortunately, this means that LFS won't work with NFS yet because 2455546Sbostic * NFS goes through paths that will make normal calls to ufs which will 2555546Sbostic * call lfs with a last argument of 1. 2655546Sbostic */ 2755546Sbostic #define LFS_SYNC 0x02 2855546Sbostic 2951155Sbostic /* On-disk and in-memory checkpoint segment usage structure. */ 3051183Sbostic typedef struct segusage SEGUSE; 3151183Sbostic struct segusage { 3267503Smckusick u_int32_t su_nbytes; /* number of live bytes */ 3367503Smckusick u_int32_t su_lastmod; /* SEGUSE last modified timestamp */ 3467503Smckusick u_int16_t su_nsums; /* number of summaries in segment */ 3567503Smckusick u_int16_t su_ninos; /* number of inode blocks in seg */ 3667503Smckusick 3767503Smckusick #define SEGUSE_ACTIVE 0x01 /* segment is currently being written */ 3867503Smckusick #define SEGUSE_DIRTY 0x02 /* segment has data in it */ 3967503Smckusick #define SEGUSE_SUPERBLOCK 0x04 /* segment contains a superblock */ 4067503Smckusick u_int32_t su_flags; 4151183Sbostic }; 4251155Sbostic 4355793Sbostic #define SEGUPB(fs) (1 << (fs)->lfs_sushift) 4464607Sbostic #define SEGTABSIZE_SU(fs) \ 4555793Sbostic (((fs)->lfs_nseg + SEGUPB(fs) - 1) >> (fs)->lfs_sushift) 4651850Sbostic 4751183Sbostic /* On-disk file information. One per file with data blocks in the segment. */ 4851183Sbostic typedef struct finfo FINFO; 4951183Sbostic struct finfo { 5067503Smckusick u_int32_t fi_nblocks; /* number of blocks */ 5167503Smckusick u_int32_t fi_version; /* version number */ 5267503Smckusick u_int32_t fi_ino; /* inode number */ 53*69284Smckusick u_int32_t fi_lastlength; /* length of last block in array */ 5468550Smckusick ufs_daddr_t fi_blocks[1]; /* array of logical block numbers */ 5551183Sbostic }; 5651183Sbostic 5751155Sbostic /* On-disk and in-memory super block. */ 5851183Sbostic struct lfs { 5952149Sbostic #define LFS_MAGIC 0x070162 6067503Smckusick u_int32_t lfs_magic; /* magic number */ 6151140Sbostic #define LFS_VERSION 1 6267503Smckusick u_int32_t lfs_version; /* version number */ 6351139Sbostic 6467503Smckusick u_int32_t lfs_size; /* number of blocks in fs */ 6567503Smckusick u_int32_t lfs_ssize; /* number of blocks per segment */ 6667503Smckusick u_int32_t lfs_dsize; /* number of disk blocks in fs */ 6767503Smckusick u_int32_t lfs_bsize; /* file system block size */ 6867503Smckusick u_int32_t lfs_fsize; /* size of frag blocks in fs */ 6967503Smckusick u_int32_t lfs_frag; /* number of frags in a block in fs */ 7051139Sbostic 7151139Sbostic /* Checkpoint region. */ 7267503Smckusick ino_t lfs_free; /* start of the free list */ 7367503Smckusick u_int32_t lfs_bfree; /* number of free disk blocks */ 7467503Smckusick u_int32_t lfs_nfiles; /* number of allocated inodes */ 7567503Smckusick int32_t lfs_avail; /* blocks available for writing */ 7667503Smckusick u_int32_t lfs_uinodes; /* inodes in cache not yet on disk */ 7768550Smckusick ufs_daddr_t lfs_idaddr; /* inode file disk address */ 7867503Smckusick ino_t lfs_ifile; /* inode file inode number */ 7968550Smckusick ufs_daddr_t lfs_lastseg; /* address of last segment written */ 8068550Smckusick ufs_daddr_t lfs_nextseg; /* address of next segment to write */ 8168550Smckusick ufs_daddr_t lfs_curseg; /* current segment being written */ 8268550Smckusick ufs_daddr_t lfs_offset; /* offset in curseg for next partial */ 8368550Smckusick ufs_daddr_t lfs_lastpseg; /* address of last partial written */ 8467503Smckusick u_int32_t lfs_tstamp; /* time stamp */ 8551139Sbostic 8651139Sbostic /* These are configuration parameters. */ 8767503Smckusick u_int32_t lfs_minfree; /* minimum percentage of free blocks */ 8851139Sbostic 8951139Sbostic /* These fields can be computed from the others. */ 9068123Smckusick u_int64_t lfs_maxfilesize; /* maximum representable file size */ 9167503Smckusick u_int32_t lfs_dbpseg; /* disk blocks per segment */ 9267503Smckusick u_int32_t lfs_inopb; /* inodes per block */ 9367503Smckusick u_int32_t lfs_ifpb; /* IFILE entries per block */ 9467503Smckusick u_int32_t lfs_sepb; /* SEGUSE entries per block */ 9567503Smckusick u_int32_t lfs_nindir; /* indirect pointers per block */ 9667503Smckusick u_int32_t lfs_nseg; /* number of segments */ 9767503Smckusick u_int32_t lfs_nspf; /* number of sectors per fragment */ 9867503Smckusick u_int32_t lfs_cleansz; /* cleaner info size in blocks */ 9967503Smckusick u_int32_t lfs_segtabsz; /* segment table size in blocks */ 10051139Sbostic 10167503Smckusick u_int32_t lfs_segmask; /* calculate offset within a segment */ 10267503Smckusick u_int32_t lfs_segshift; /* fast mult/div for segments */ 103*69284Smckusick u_int64_t lfs_bmask; /* calc block offset from file offset */ 10467503Smckusick u_int32_t lfs_bshift; /* calc block number from file offset */ 105*69284Smckusick u_int64_t lfs_ffmask; /* calc frag offset from file offset */ 10667503Smckusick u_int32_t lfs_ffshift; /* fast mult/div for frag from file */ 107*69284Smckusick u_int64_t lfs_fbmask; /* calc frag offset from block offset */ 10867503Smckusick u_int32_t lfs_fbshift; /* fast mult/div for frag from block */ 10967503Smckusick u_int32_t lfs_fsbtodb; /* fsbtodb and dbtofsb shift constant */ 11067503Smckusick u_int32_t lfs_sushift; /* fast mult/div for segusage table */ 11151139Sbostic 11267503Smckusick int32_t lfs_maxsymlinklen; /* max length of an internal symlink */ 11367503Smckusick 11451155Sbostic #define LFS_MIN_SBINTERVAL 5 /* minimum superblock segment spacing */ 11551155Sbostic #define LFS_MAXNUMSB 10 /* superblock disk offsets */ 11668550Smckusick ufs_daddr_t lfs_sboffs[LFS_MAXNUMSB]; 11751139Sbostic 11867503Smckusick /* Checksum -- last valid disk field. */ 11967503Smckusick u_int32_t lfs_cksum; /* checksum for superblock checking */ 12067503Smckusick 12151155Sbostic /* These fields are set at mount time and are meaningless on disk. */ 12267503Smckusick struct segment *lfs_sp; /* current segment being written */ 12367503Smckusick struct vnode *lfs_ivnode; /* vnode for the ifile */ 12467503Smckusick u_long lfs_seglock; /* single-thread the segment writer */ 12567503Smckusick pid_t lfs_lockpid; /* pid of lock holder */ 12667503Smckusick u_long lfs_iocount; /* number of ios pending */ 12767503Smckusick u_long lfs_writer; /* don't allow any dirops to start */ 12867503Smckusick u_long lfs_dirops; /* count of active directory ops */ 12967503Smckusick u_long lfs_doifile; /* Write ifile blocks on next write */ 13067503Smckusick u_long lfs_nactive; /* Number of segments since last ckp */ 13167503Smckusick int8_t lfs_fmod; /* super block modified flag */ 13267503Smckusick int8_t lfs_clean; /* file system is clean flag */ 13367503Smckusick int8_t lfs_ronly; /* mounted read-only flag */ 13467503Smckusick int8_t lfs_flags; /* currently unused flag */ 13567503Smckusick u_char lfs_fsmnt[MNAMELEN]; /* name mounted on */ 13668118Smckusick 13768118Smckusick int32_t lfs_pad[40]; /* round to 512 bytes */ 13851183Sbostic }; 13951139Sbostic 14051155Sbostic /* 14167503Smckusick * Inode 0: out-of-band inode number 14267503Smckusick * Inode 1: IFILE inode number 14367503Smckusick * Inode 2: root inode 14467503Smckusick * Inode 3: lost+found inode number 14551155Sbostic */ 14651303Sbostic #define LFS_UNUSED_INUM 0 /* out of band inode number */ 14752079Sbostic #define LFS_IFILE_INUM 1 /* IFILE inode number */ 14852079Sbostic #define LOSTFOUNDINO 3 /* lost+found inode number */ 14952079Sbostic #define LFS_FIRST_INUM 4 /* first free inode number */ 15051139Sbostic 15151850Sbostic /* Address calculations for metadata located in the inode */ 15251850Sbostic #define S_INDIR(fs) -NDADDR 15352997Sstaelin #define D_INDIR(fs) (S_INDIR(fs) - NINDIR(fs) - 1) 15452997Sstaelin #define T_INDIR(fs) (D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1) 15551183Sbostic 15651350Sbostic /* Unassigned disk address. */ 15751350Sbostic #define UNASSIGNED -1 15851350Sbostic 15955934Sbostic /* Unused logical block number */ 16055934Sbostic #define LFS_UNUSED_LBN -1 16155934Sbostic 16251183Sbostic typedef struct ifile IFILE; 16351183Sbostic struct ifile { 16467503Smckusick u_int32_t if_version; /* inode version number */ 16551155Sbostic #define LFS_UNUSED_DADDR 0 /* out-of-band daddr */ 16668550Smckusick ufs_daddr_t if_daddr; /* inode disk address */ 16767503Smckusick ino_t if_nextfree; /* next-unallocated inode */ 16851183Sbostic }; 16951139Sbostic 17051850Sbostic /* 17151850Sbostic * Cleaner information structure. This resides in the ifile and is used 17251850Sbostic * to pass information between the cleaner and the kernel. 17351850Sbostic */ 17451850Sbostic typedef struct _cleanerinfo { 17567503Smckusick u_int32_t clean; /* K: number of clean segments */ 17667503Smckusick u_int32_t dirty; /* K: number of dirty segments */ 17751850Sbostic } CLEANERINFO; 17851139Sbostic 17964607Sbostic #define CLEANSIZE_SU(fs) \ 18051850Sbostic ((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift) 18151140Sbostic 18251140Sbostic /* 18351140Sbostic * All summary blocks are the same size, so we can always read a summary 18451303Sbostic * block easily from a segment. 18551140Sbostic */ 18651140Sbostic #define LFS_SUMMARY_SIZE 512 18751140Sbostic 18851139Sbostic /* On-disk segment summary information */ 18951183Sbostic typedef struct segsum SEGSUM; 19051183Sbostic struct segsum { 19167503Smckusick u_int32_t ss_sumsum; /* check sum of summary block */ 19267503Smckusick u_int32_t ss_datasum; /* check sum of data */ 193*69284Smckusick u_int32_t ss_magic; /* segment summary magic number */ 194*69284Smckusick #define SS_MAGIC 0x061561 19568550Smckusick ufs_daddr_t ss_next; /* next segment */ 19667503Smckusick u_int32_t ss_create; /* creation time stamp */ 19767503Smckusick u_int16_t ss_nfinfo; /* number of file info structures */ 19867503Smckusick u_int16_t ss_ninos; /* number of inodes in summary */ 19967503Smckusick 20054264Sbostic #define SS_DIROP 0x01 /* segment begins a dirop */ 20154264Sbostic #define SS_CONT 0x02 /* more partials to finish this write*/ 20267503Smckusick u_int16_t ss_flags; /* used for directory operations */ 20367503Smckusick u_int16_t ss_pad; /* extra space */ 20452993Sbostic /* FINFO's and inode daddr's... */ 20551183Sbostic }; 20651139Sbostic 20751155Sbostic /* NINDIR is the number of indirects in a file system block. */ 20851155Sbostic #define NINDIR(fs) ((fs)->lfs_nindir) 20951155Sbostic 21051155Sbostic /* INOPB is the number of inodes in a secondary storage block. */ 21151155Sbostic #define INOPB(fs) ((fs)->lfs_inopb) 21251155Sbostic 213*69284Smckusick #define blksize(fs, ip, lbn) \ 214*69284Smckusick (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->lfs_bshift) \ 215*69284Smckusick ? (fs)->lfs_bsize \ 216*69284Smckusick : (fragroundup(fs, blkoff(fs, (ip)->i_size)))) 217*69284Smckusick #define blkoff(fs, loc) ((int)((loc) & (fs)->lfs_bmask)) 218*69284Smckusick #define fragoff(fs, loc) /* calculates (loc % fs->lfs_fsize) */ \ 219*69284Smckusick ((int)((loc) & (fs)->lfs_ffmask)) 22051155Sbostic #define fsbtodb(fs, b) ((b) << (fs)->lfs_fsbtodb) 22155587Sbostic #define dbtofsb(fs, b) ((b) >> (fs)->lfs_fsbtodb) 222*69284Smckusick #define fragstodb(fs, b) ((b) << (fs)->lfs_fsbtodb - (fs)->lfs_fbshift) 223*69284Smckusick #define dbtofrags(fs, b) ((b) >> (fs)->lfs_fsbtodb - (fs)->lfs_fbshift) 22451155Sbostic #define lblkno(fs, loc) ((loc) >> (fs)->lfs_bshift) 22551155Sbostic #define lblktosize(fs, blk) ((blk) << (fs)->lfs_bshift) 226*69284Smckusick #define numfrags(fs, loc) /* calculates (loc / fs->lfs_fsize) */ \ 227*69284Smckusick ((loc) >> (fs)->lfs_ffshift) 228*69284Smckusick #define blkroundup(fs, size) /* calculates roundup(size, fs->lfs_bsize) */ \ 229*69284Smckusick ((int)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask))) 230*69284Smckusick #define fragroundup(fs, size) /* calculates roundup(size, fs->lfs_fsize) */ \ 231*69284Smckusick ((int)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask))) 232*69284Smckusick #define fragstoblks(fs, frags) /* calculates (frags / fs->lfs_frag) */ \ 233*69284Smckusick ((frags) >> (fs)->lfs_fbshift) 234*69284Smckusick #define blkstofrags(fs, blks) /* calculates (blks * fs->lfs_frag) */ \ 235*69284Smckusick ((blks) << (fs)->lfs_fbshift) 236*69284Smckusick #define fragnum(fs, fsb) /* calculates (fsb % fs->lfs_frag) */ \ 237*69284Smckusick ((fsb) & ((fs)->lfs_frag - 1)) 238*69284Smckusick #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->lfs_frag) */ \ 239*69284Smckusick ((fsb) &~ ((fs)->lfs_frag - 1)) 240*69284Smckusick #define dblksize(fs, dip, lbn) \ 241*69284Smckusick (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\ 242*69284Smckusick ? (fs)->lfs_bsize \ 243*69284Smckusick : (fragroundup(fs, blkoff(fs, (dip)->di_size)))) 24464607Sbostic #define datosn(fs, daddr) /* disk address to segment number */ \ 24552079Sbostic (((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize)) 24664607Sbostic #define sntoda(fs, sn) /* segment number to disk address */ \ 24768550Smckusick ((ufs_daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) + \ 24852079Sbostic (fs)->lfs_sboffs[0])) 24952079Sbostic 25051925Sbostic /* Read in the block with the cleaner info from the ifile. */ 25164607Sbostic #define LFS_CLEANERINFO(CP, F, BP) { \ 25264607Sbostic VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \ 25364607Sbostic if (bread((F)->lfs_ivnode, \ 25468550Smckusick (ufs_daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP))) \ 25564607Sbostic panic("lfs: ifile read"); \ 25664607Sbostic (CP) = (CLEANERINFO *)(BP)->b_data; \ 25751925Sbostic } 25851925Sbostic 25951850Sbostic /* Read in the block with a specific inode from the ifile. */ 26064607Sbostic #define LFS_IENTRY(IP, F, IN, BP) { \ 26164607Sbostic int _e; \ 26264607Sbostic VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \ 26364607Sbostic if (_e = bread((F)->lfs_ivnode, \ 26464607Sbostic (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\ 26564607Sbostic (F)->lfs_bsize, NOCRED, &(BP))) \ 26664607Sbostic panic("lfs: ifile read %d", _e); \ 26764607Sbostic (IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb; \ 26851481Sbostic } 26951481Sbostic 27051850Sbostic /* Read in the block with a specific segment usage entry from the ifile. */ 27164607Sbostic #define LFS_SEGENTRY(SP, F, IN, BP) { \ 27264607Sbostic int _e; \ 27364607Sbostic VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \ 27464607Sbostic if (_e = bread((F)->lfs_ivnode, \ 27564607Sbostic ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz, \ 27664607Sbostic (F)->lfs_bsize, NOCRED, &(BP))) \ 27764607Sbostic panic("lfs: ifile read: %d", _e); \ 27864607Sbostic (SP) = (SEGUSE *)(BP)->b_data + ((IN) & (F)->lfs_sepb - 1); \ 27951850Sbostic } 28051850Sbostic 28155934Sbostic /* 28255934Sbostic * Determine if there is enough room currently available to write db 28355934Sbostic * disk blocks. We need enough blocks for the new blocks, the current, 28455934Sbostic * inode blocks, a summary block, plus potentially the ifile inode and 28555934Sbostic * the segment usage table, plus an ifile page. 28655934Sbostic */ 28755934Sbostic #define LFS_FITS(fs, db) \ 28867503Smckusick ((int32_t)((db + ((fs)->lfs_uinodes + INOPB((fs))) / \ 28967503Smckusick INOPB((fs)) + fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE + \ 29064607Sbostic (fs)->lfs_segtabsz)) < (fs)->lfs_avail) 29151936Sbostic 29255934Sbostic /* Determine if a buffer belongs to the ifile */ 29355934Sbostic #define IS_IFILE(bp) (VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM) 29464607Sbostic 29551850Sbostic /* 29651850Sbostic * Structures used by lfs_bmapv and lfs_markv to communicate information 29751850Sbostic * about inodes and data blocks. 29851850Sbostic */ 29951850Sbostic typedef struct block_info { 30051850Sbostic ino_t bi_inode; /* inode # */ 30168550Smckusick ufs_daddr_t bi_lbn; /* logical block w/in file */ 30268550Smckusick ufs_daddr_t bi_daddr; /* disk address of block */ 30351850Sbostic time_t bi_segcreate; /* origin segment create time */ 30456189Smargo int bi_version; /* file version number */ 30551850Sbostic void *bi_bp; /* data buffer */ 306*69284Smckusick int bi_size; /* size of the block (if fragment) */ 30751850Sbostic } BLOCK_INFO; 30851850Sbostic 30955934Sbostic /* In-memory description of a segment about to be written. */ 31055934Sbostic struct segment { 31167503Smckusick struct lfs *fs; /* file system pointer */ 31255934Sbostic struct buf **bpp; /* pointer to buffer array */ 31355934Sbostic struct buf **cbpp; /* pointer to next available bp */ 31455934Sbostic struct buf **start_bpp; /* pointer to first bp in this set */ 31567503Smckusick struct buf *ibp; /* buffer pointer to inode page */ 31667503Smckusick struct finfo *fip; /* current fileinfo pointer */ 31767503Smckusick struct vnode *vp; /* vnode being gathered */ 31867503Smckusick void *segsum; /* segment summary info */ 31967503Smckusick u_int32_t ninodes; /* number of inodes in this segment */ 32067503Smckusick u_int32_t seg_bytes_left; /* bytes left in segment */ 32167503Smckusick u_int32_t sum_bytes_left; /* bytes left in summary block */ 32267503Smckusick u_int32_t seg_number; /* number of this segment */ 32368550Smckusick ufs_daddr_t *start_lbp; /* beginning lbn for this set */ 32467503Smckusick 32555934Sbostic #define SEGM_CKP 0x01 /* doing a checkpoint */ 32655934Sbostic #define SEGM_CLEAN 0x02 /* cleaner call; don't sort */ 32757065Smargo #define SEGM_SYNC 0x04 /* wait for segment */ 32867503Smckusick u_int16_t seg_flags; /* run-time flags for this segment */ 32955934Sbostic }; 33056154Smargo 33164607Sbostic #define ISSPACE(F, BB, C) \ 33264607Sbostic (((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) || \ 33356154Smargo ((C)->cr_uid != 0 && IS_FREESPACE(F, BB))) 33456154Smargo 33564607Sbostic #define IS_FREESPACE(F, BB) \ 33656154Smargo ((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB))) 33756154Smargo 33864607Sbostic #define ISSPACE_XXX(F, BB) \ 33956154Smargo ((F)->lfs_bfree >= (BB)) 34056154Smargo 34157065Smargo #define DOSTATS 34257065Smargo #ifdef DOSTATS 34357065Smargo /* Statistics Counters */ 34457065Smargo struct lfs_stats { 34567503Smckusick u_int segsused; 34667503Smckusick u_int psegwrites; 34767503Smckusick u_int psyncwrites; 34867503Smckusick u_int pcleanwrites; 34967503Smckusick u_int blocktot; 35067503Smckusick u_int cleanblocks; 35167503Smckusick u_int ncheckpoints; 35267503Smckusick u_int nwrites; 35367503Smckusick u_int nsync_writes; 35467503Smckusick u_int wait_exceeded; 35567503Smckusick u_int write_exceeded; 35667503Smckusick u_int flush_invoked; 35757065Smargo }; 35857065Smargo extern struct lfs_stats lfs_stats; 35957065Smargo #endif 360