Home | History | Annotate | Line # | Download | only in lfs
lfs.h revision 1.9
      1 /*	$NetBSD: lfs.h,v 1.9 1998/03/01 02:23:23 fvdl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)lfs.h	8.9 (Berkeley) 5/8/95
     36  */
     37 
     38 #define	LFS_LABELPAD	8192		/* LFS label size */
     39 #define	LFS_SBPAD	8192		/* LFS superblock size */
     40 
     41 /*
     42  * XXX
     43  * This is a kluge and NEEDS to go away.
     44  *
     45  * Right now, ufs code handles most of the calls for directory operations
     46  * such as create, mkdir, link, etc.  As a result VOP_UPDATE is being
     47  * called with waitfor set (since ffs does these things synchronously).
     48  * Since LFS does not want to do these synchronously, we treat the last
     49  * argument to lfs_update as a set of flags.  If LFS_SYNC is set, then
     50  * the update should be synchronous, if not, do it asynchronously.
     51  * Unfortunately, this means that LFS won't work with NFS yet because
     52  * NFS goes through paths that will make normal calls to ufs which will
     53  * call lfs with a last argument of 1.
     54  */
     55 #define	LFS_SYNC	0x02
     56 
     57 /* On-disk and in-memory checkpoint segment usage structure. */
     58 typedef struct segusage SEGUSE;
     59 struct segusage {
     60 	u_int32_t su_nbytes;		/* number of live bytes */
     61 	u_int32_t su_lastmod;		/* SEGUSE last modified timestamp */
     62 	u_int16_t su_nsums;		/* number of summaries in segment */
     63 	u_int16_t su_ninos;		/* number of inode blocks in seg */
     64 
     65 #define	SEGUSE_ACTIVE		0x01	/* segment is currently being written */
     66 #define	SEGUSE_DIRTY		0x02	/* segment has data in it */
     67 #define	SEGUSE_SUPERBLOCK	0x04	/* segment contains a superblock */
     68 	u_int32_t su_flags;
     69 };
     70 
     71 #define	SEGUPB(fs)	(1 << (fs)->lfs_sushift)
     72 #define	SEGTABSIZE_SU(fs)						\
     73 	(((fs)->lfs_nseg + SEGUPB(fs) - 1) >> (fs)->lfs_sushift)
     74 
     75 /* On-disk file information.  One per file with data blocks in the segment. */
     76 typedef struct finfo FINFO;
     77 struct finfo {
     78 	u_int32_t fi_nblocks;		/* number of blocks */
     79 	u_int32_t fi_version;		/* version number */
     80 	u_int32_t fi_ino;		/* inode number */
     81 	u_int32_t fi_lastlength;	/* length of last block in array */
     82 	ufs_daddr_t	  fi_blocks[1];		/* array of logical block numbers */
     83 };
     84 
     85 /* On-disk and in-memory super block. */
     86 struct lfs {
     87 #define	LFS_MAGIC	0x070162
     88 	u_int32_t lfs_magic;		/* magic number */
     89 #define	LFS_VERSION	1
     90 	u_int32_t lfs_version;		/* version number */
     91 
     92 	u_int32_t lfs_size;		/* number of blocks in fs */
     93 	u_int32_t lfs_ssize;		/* number of blocks per segment */
     94 	u_int32_t lfs_dsize;		/* number of disk blocks in fs */
     95 	u_int32_t lfs_bsize;		/* file system block size */
     96 	u_int32_t lfs_fsize;		/* size of frag blocks in fs */
     97 	u_int32_t lfs_frag;		/* number of frags in a block in fs */
     98 
     99 /* Checkpoint region. */
    100 	ino_t	  lfs_free;		/* start of the free list */
    101 	u_int32_t lfs_bfree;		/* number of free disk blocks */
    102 	u_int32_t lfs_nfiles;		/* number of allocated inodes */
    103 	int32_t	  lfs_avail;		/* blocks available for writing */
    104 	u_int32_t lfs_uinodes;		/* inodes in cache not yet on disk */
    105 	ufs_daddr_t  lfs_idaddr;	/* inode file disk address */
    106 	ino_t	  lfs_ifile;		/* inode file inode number */
    107 	ufs_daddr_t  lfs_lastseg;	/* address of last segment written */
    108 	ufs_daddr_t  lfs_nextseg;	/* address of next segment to write */
    109 	ufs_daddr_t  lfs_curseg;	/* current segment being written */
    110 	ufs_daddr_t  lfs_offset;	/* offset in curseg for next partial */
    111 	ufs_daddr_t  lfs_lastpseg;	/* address of last partial written */
    112 	u_int32_t lfs_tstamp;		/* time stamp */
    113 
    114 /* These are configuration parameters. */
    115 	u_int32_t lfs_minfree;		/* minimum percentage of free blocks */
    116 
    117 /* These fields can be computed from the others. */
    118 	u_int64_t lfs_maxfilesize;	/* maximum representable file size */
    119 	u_int32_t lfs_dbpseg;		/* disk blocks per segment */
    120 	u_int32_t lfs_inopb;		/* inodes per block */
    121 	u_int32_t lfs_ifpb;		/* IFILE entries per block */
    122 	u_int32_t lfs_sepb;		/* SEGUSE entries per block */
    123 	u_int32_t lfs_nindir;		/* indirect pointers per block */
    124 	u_int32_t lfs_nseg;		/* number of segments */
    125 	u_int32_t lfs_nspf;		/* number of sectors per fragment */
    126 	u_int32_t lfs_cleansz;		/* cleaner info size in blocks */
    127 	u_int32_t lfs_segtabsz;		/* segment table size in blocks */
    128 
    129 	u_int32_t lfs_segmask;		/* calculate offset within a segment */
    130 	u_int32_t lfs_segshift;		/* fast mult/div for segments */
    131 	u_int64_t lfs_bmask;		/* calc block offset from file offset */
    132 	u_int32_t lfs_bshift;		/* calc block number from file offset */
    133 	u_int64_t lfs_ffmask;		/* calc frag offset from file offset */
    134 	u_int32_t lfs_ffshift;		/* fast mult/div for frag from file */
    135 	u_int64_t lfs_fbmask;		/* calc frag offset from block offset */
    136 	u_int32_t lfs_fbshift;		/* fast mult/div for frag from block */
    137 	u_int32_t lfs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
    138 	u_int32_t lfs_sushift;		/* fast mult/div for segusage table */
    139 
    140 	int32_t	  lfs_maxsymlinklen;	/* max length of an internal symlink */
    141 
    142 #define	LFS_MIN_SBINTERVAL	5	/* minimum superblock segment spacing */
    143 #define	LFS_MAXNUMSB		10	/* superblock disk offsets */
    144 	ufs_daddr_t	  lfs_sboffs[LFS_MAXNUMSB];
    145 
    146 /* Checksum -- last valid disk field. */
    147 	u_int32_t lfs_cksum;		/* checksum for superblock checking */
    148 
    149 /* These fields are set at mount time and are meaningless on disk. */
    150 	struct segment *lfs_sp;		/* current segment being written */
    151 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
    152 	u_int32_t  lfs_seglock;		/* single-thread the segment writer */
    153 	pid_t	  lfs_lockpid;		/* pid of lock holder */
    154 	u_int32_t lfs_iocount;		/* number of ios pending */
    155 	u_int32_t lfs_writer;		/* don't allow any dirops to start */
    156 	u_int32_t lfs_dirops;		/* count of active directory ops */
    157 	u_int32_t lfs_doifile;		/* Write ifile blocks on next write */
    158 	u_int32_t lfs_nactive;		/* Number of segments since last ckp */
    159 	int8_t	  lfs_fmod;		/* super block modified flag */
    160 	int8_t	  lfs_clean;		/* file system is clean flag */
    161 	int8_t	  lfs_ronly;		/* mounted read-only flag */
    162 	int8_t	  lfs_flags;		/* currently unused flag */
    163 	u_char	  lfs_fsmnt[MNAMELEN];	/* name mounted on */
    164 
    165 	int32_t	  lfs_pad[40];		/* round to 512 bytes */
    166 };
    167 
    168 /*
    169  * Inode 0:	out-of-band inode number
    170  * Inode 1:	IFILE inode number
    171  * Inode 2:	root inode
    172  * Inode 3:	lost+found inode number
    173  */
    174 #define	LFS_UNUSED_INUM	0		/* out of band inode number */
    175 #define	LFS_IFILE_INUM	1		/* IFILE inode number */
    176 #define	LOSTFOUNDINO	3		/* lost+found inode number */
    177 #define	LFS_FIRST_INUM	4		/* first free inode number */
    178 
    179 /* Address calculations for metadata located in the inode */
    180 #define	S_INDIR(fs)	-NDADDR
    181 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
    182 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
    183 
    184 /* Unassigned disk address. */
    185 #define	UNASSIGNED	-1
    186 
    187 /* Unused logical block number */
    188 #define LFS_UNUSED_LBN	-1
    189 
    190 typedef struct ifile IFILE;
    191 struct ifile {
    192 	u_int32_t if_version;		/* inode version number */
    193 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
    194 	ufs_daddr_t if_daddr;		/* inode disk address */
    195 	ino_t	  if_nextfree;		/* next-unallocated inode */
    196 };
    197 
    198 /*
    199  * Cleaner information structure.  This resides in the ifile and is used
    200  * to pass information between the cleaner and the kernel.
    201  */
    202 typedef struct _cleanerinfo {
    203 	u_int32_t clean;		/* K: number of clean segments */
    204 	u_int32_t dirty;		/* K: number of dirty segments */
    205 } CLEANERINFO;
    206 
    207 #define	CLEANSIZE_SU(fs)						\
    208 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
    209 
    210 /*
    211  * All summary blocks are the same size, so we can always read a summary
    212  * block easily from a segment.
    213  */
    214 #define	LFS_SUMMARY_SIZE	512
    215 
    216 /* On-disk segment summary information */
    217 typedef struct segsum SEGSUM;
    218 struct segsum {
    219 	u_int32_t ss_sumsum;		/* check sum of summary block */
    220 	u_int32_t ss_datasum;		/* check sum of data */
    221 	u_int32_t ss_magic;		/* segment summary magic number */
    222 #define SS_MAGIC	0x061561
    223 	ufs_daddr_t ss_next;		/* next segment */
    224 	u_int32_t ss_create;		/* creation time stamp */
    225 	u_int16_t ss_nfinfo;		/* number of file info structures */
    226 	u_int16_t ss_ninos;		/* number of inodes in summary */
    227 
    228 #define	SS_DIROP	0x01		/* segment begins a dirop */
    229 #define	SS_CONT		0x02		/* more partials to finish this write*/
    230 	u_int16_t ss_flags;		/* used for directory operations */
    231 	u_int16_t ss_pad;		/* extra space */
    232 	/* FINFO's and inode daddr's... */
    233 };
    234 
    235 /* NINDIR is the number of indirects in a file system block. */
    236 #define	NINDIR(fs)	((fs)->lfs_nindir)
    237 
    238 /* INOPB is the number of inodes in a secondary storage block. */
    239 #define	INOPB(fs)	((fs)->lfs_inopb)
    240 
    241 #define	blksize(fs, ip, lbn) \
    242 	(((lbn) >= NDADDR || (ip)->i_ffs_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
    243 	    ? (fs)->lfs_bsize \
    244 	    : (fragroundup(fs, blkoff(fs, (ip)->i_ffs_size))))
    245 #define	blkoff(fs, loc)		((int)(loc) & (fs)->lfs_bmask)
    246 #define fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
    247     ((int)((loc) & (fs)->lfs_ffmask))
    248 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
    249 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
    250 #define fragstodb(fs, b)	((b) << ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
    251 #define dbtofrags(fs, b)	((b) >> ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
    252 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
    253 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
    254 #define numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
    255 	((loc) >> (fs)->lfs_ffshift)
    256 #define blkroundup(fs, size)	/* calculates roundup(size, fs->lfs_bsize) */ \
    257 	((int)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
    258 #define fragroundup(fs, size)	/* calculates roundup(size, fs->lfs_fsize) */ \
    259 	((int)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
    260 #define fragstoblks(fs, frags)	/* calculates (frags / fs->lfs_frag) */ \
    261 	((frags) >> (fs)->lfs_fbshift)
    262 #define blkstofrags(fs, blks)	/* calculates (blks * fs->lfs_frag) */ \
    263 	((blks) << (fs)->lfs_fbshift)
    264 #define fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */ \
    265 	((fsb) & ((fs)->lfs_frag - 1))
    266 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->lfs_frag) */ \
    267 	((fsb) &~ ((fs)->lfs_frag - 1))
    268 #define dblksize(fs, dip, lbn) \
    269 	(((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
    270 	    ? (fs)->lfs_bsize \
    271 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
    272 #define	datosn(fs, daddr)	/* disk address to segment number */	\
    273 	(((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
    274 #define sntoda(fs, sn) 		/* segment number to disk address */	\
    275 	((ufs_daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) +	\
    276 	    (fs)->lfs_sboffs[0]))
    277 
    278 /* Read in the block with the cleaner info from the ifile. */
    279 #define LFS_CLEANERINFO(CP, F, BP) {					\
    280 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    281 	if (bread((F)->lfs_ivnode,					\
    282 	    (ufs_daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP)))		\
    283 		panic("lfs: ifile read");				\
    284 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    285 }
    286 
    287 /* Read in the block with a specific inode from the ifile. */
    288 #define	LFS_IENTRY(IP, F, IN, BP) {					\
    289 	int _e;								\
    290 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    291 	if ((_e = bread((F)->lfs_ivnode,				\
    292 	    (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\
    293 	    (F)->lfs_bsize, NOCRED, &(BP))) != 0)			\
    294 		panic("lfs: ifile read %d", _e);			\
    295 	(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;		\
    296 }
    297 
    298 /* Read in the block with a specific segment usage entry from the ifile. */
    299 #define	LFS_SEGENTRY(SP, F, IN, BP) {					\
    300 	int _e;								\
    301 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    302 	if ((_e = bread((F)->lfs_ivnode,				\
    303 	    ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz,		\
    304 	    (F)->lfs_bsize, NOCRED, &(BP))) != 0)			\
    305 		panic("lfs: ifile read: %d", _e);			\
    306 	(SP) = (SEGUSE *)(BP)->b_data + ((IN) & ((F)->lfs_sepb - 1));	\
    307 }
    308 
    309 /*
    310  * Determine if there is enough room currently available to write db
    311  * disk blocks.  We need enough blocks for the new blocks, the current,
    312  * inode blocks, a summary block, plus potentially the ifile inode and
    313  * the segment usage table, plus an ifile page.
    314  */
    315 #define LFS_FITS(fs, db)						\
    316 	((int32_t)((db + ((fs)->lfs_uinodes + INOPB((fs))) / 		\
    317 	INOPB((fs)) + fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE +	\
    318 	(fs)->lfs_segtabsz)) < (fs)->lfs_avail)
    319 
    320 /* Determine if a buffer belongs to the ifile */
    321 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
    322 
    323 /*
    324  * Structures used by lfs_bmapv and lfs_markv to communicate information
    325  * about inodes and data blocks.
    326  */
    327 typedef struct block_info {
    328 	ino_t	bi_inode;		/* inode # */
    329 	ufs_daddr_t bi_lbn;		/* logical block w/in file */
    330 	ufs_daddr_t bi_daddr;		/* disk address of block */
    331 	time_t	bi_segcreate;		/* origin segment create time */
    332 	int	bi_version;		/* file version number */
    333 	void	*bi_bp;			/* data buffer */
    334 	int     bi_size;		/* size of the block (if fragment) */
    335 } BLOCK_INFO;
    336 
    337 /* In-memory description of a segment about to be written. */
    338 struct segment {
    339 	struct lfs	 *fs;		/* file system pointer */
    340 	struct buf	**bpp;		/* pointer to buffer array */
    341 	struct buf	**cbpp;		/* pointer to next available bp */
    342 	struct buf	**start_bpp;	/* pointer to first bp in this set */
    343 	struct buf	 *ibp;		/* buffer pointer to inode page */
    344 	struct finfo	 *fip;		/* current fileinfo pointer */
    345 	struct vnode	 *vp;		/* vnode being gathered */
    346 	void	 *segsum;		/* segment summary info */
    347 	u_int32_t ninodes;		/* number of inodes in this segment */
    348 	u_int32_t seg_bytes_left;	/* bytes left in segment */
    349 	u_int32_t sum_bytes_left;	/* bytes left in summary block */
    350 	u_int32_t seg_number;		/* number of this segment */
    351 	ufs_daddr_t *start_lbp;		/* beginning lbn for this set */
    352 
    353 #define	SEGM_CKP	0x01		/* doing a checkpoint */
    354 #define	SEGM_CLEAN	0x02		/* cleaner call; don't sort */
    355 #define	SEGM_SYNC	0x04		/* wait for segment */
    356 	u_int16_t seg_flags;		/* run-time flags for this segment */
    357 };
    358 
    359 #define ISSPACE(F, BB, C)						\
    360 	(((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) ||		\
    361 	((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
    362 
    363 #define IS_FREESPACE(F, BB)						\
    364 	((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB)))
    365 
    366 #define ISSPACE_XXX(F, BB)						\
    367 	((F)->lfs_bfree >= (BB))
    368 
    369 #define DOSTATS
    370 #ifdef DOSTATS
    371 /* Statistics Counters */
    372 struct lfs_stats {
    373 	u_int	segsused;
    374 	u_int	psegwrites;
    375 	u_int	psyncwrites;
    376 	u_int	pcleanwrites;
    377 	u_int	blocktot;
    378 	u_int	cleanblocks;
    379 	u_int	ncheckpoints;
    380 	u_int	nwrites;
    381 	u_int	nsync_writes;
    382 	u_int	wait_exceeded;
    383 	u_int	write_exceeded;
    384 	u_int	flush_invoked;
    385 };
    386 extern struct lfs_stats lfs_stats;
    387 #endif
    388