xref: /csrg-svn/sys/ufs/lfs/lfs.h (revision 68118)
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*68118Smckusick  *	@(#)lfs.h	8.6 (Berkeley) 01/02/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 */
5367503Smckusick 	daddr_t	  fi_blocks[1];		/* array of logical block numbers */
5451183Sbostic };
5551183Sbostic 
5651155Sbostic /* On-disk and in-memory super block. */
5751183Sbostic struct lfs {
5852149Sbostic #define	LFS_MAGIC	0x070162
5967503Smckusick 	u_int32_t lfs_magic;		/* magic number */
6051140Sbostic #define	LFS_VERSION	1
6167503Smckusick 	u_int32_t lfs_version;		/* version number */
6251139Sbostic 
6367503Smckusick 	u_int32_t lfs_size;		/* number of blocks in fs */
6467503Smckusick 	u_int32_t lfs_ssize;		/* number of blocks per segment */
6567503Smckusick 	u_int32_t lfs_dsize;		/* number of disk blocks in fs */
6667503Smckusick 	u_int32_t lfs_bsize;		/* file system block size */
6767503Smckusick 	u_int32_t lfs_fsize;		/* size of frag blocks in fs */
6867503Smckusick 	u_int32_t lfs_frag;		/* number of frags in a block in fs */
6951139Sbostic 
7051139Sbostic /* Checkpoint region. */
7167503Smckusick 	ino_t	  lfs_free;		/* start of the free list */
7267503Smckusick 	u_int32_t lfs_bfree;		/* number of free disk blocks */
7367503Smckusick 	u_int32_t lfs_nfiles;		/* number of allocated inodes */
7467503Smckusick 	int32_t	  lfs_avail;		/* blocks available for writing */
7567503Smckusick 	u_int32_t lfs_uinodes;		/* inodes in cache not yet on disk */
7667503Smckusick 	daddr_t	  lfs_idaddr;		/* inode file disk address */
7767503Smckusick 	ino_t	  lfs_ifile;		/* inode file inode number */
7867503Smckusick 	daddr_t	  lfs_lastseg;		/* address of last segment written */
7967503Smckusick 	daddr_t	  lfs_nextseg;		/* address of next segment to write */
8067503Smckusick 	daddr_t	  lfs_curseg;		/* current segment being written */
8167503Smckusick 	daddr_t	  lfs_offset;		/* offset in curseg for next partial */
8267503Smckusick 	daddr_t	  lfs_lastpseg;		/* address of last partial written */
8367503Smckusick 	u_int32_t lfs_tstamp;		/* time stamp */
8451139Sbostic 
8551139Sbostic /* These are configuration parameters. */
8667503Smckusick 	u_int32_t lfs_minfree;		/* minimum percentage of free blocks */
8751139Sbostic 
8851139Sbostic /* These fields can be computed from the others. */
8967503Smckusick 	u_quad_t  lfs_maxfilesize;	/* maximum representable file size */
9067503Smckusick 	u_int32_t lfs_dbpseg;		/* disk blocks per segment */
9167503Smckusick 	u_int32_t lfs_inopb;		/* inodes per block */
9267503Smckusick 	u_int32_t lfs_ifpb;		/* IFILE entries per block */
9367503Smckusick 	u_int32_t lfs_sepb;		/* SEGUSE entries per block */
9467503Smckusick 	u_int32_t lfs_nindir;		/* indirect pointers per block */
9567503Smckusick 	u_int32_t lfs_nseg;		/* number of segments */
9667503Smckusick 	u_int32_t lfs_nspf;		/* number of sectors per fragment */
9767503Smckusick 	u_int32_t lfs_cleansz;		/* cleaner info size in blocks */
9867503Smckusick 	u_int32_t lfs_segtabsz;		/* segment table size in blocks */
9951139Sbostic 
10067503Smckusick 	u_int32_t lfs_segmask;		/* calculate offset within a segment */
10167503Smckusick 	u_int32_t lfs_segshift;		/* fast mult/div for segments */
10267503Smckusick 	u_int32_t lfs_bmask;		/* calc block offset from file offset */
10367503Smckusick 	u_int32_t lfs_bshift;		/* calc block number from file offset */
10467503Smckusick 	u_int32_t lfs_ffmask;		/* calc frag offset from file offset */
10567503Smckusick 	u_int32_t lfs_ffshift;		/* fast mult/div for frag from file */
10667503Smckusick 	u_int32_t lfs_fbmask;		/* calc frag offset from block offset */
10767503Smckusick 	u_int32_t lfs_fbshift;		/* fast mult/div for frag from block */
10867503Smckusick 	u_int32_t lfs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
10967503Smckusick 	u_int32_t lfs_sushift;		/* fast mult/div for segusage table */
11051139Sbostic 
11167503Smckusick 	int32_t	  lfs_maxsymlinklen;	/* max length of an internal symlink */
11267503Smckusick 
11351155Sbostic #define	LFS_MIN_SBINTERVAL	5	/* minimum superblock segment spacing */
11451155Sbostic #define	LFS_MAXNUMSB		10	/* superblock disk offsets */
11567503Smckusick 	daddr_t	  lfs_sboffs[LFS_MAXNUMSB];
11651139Sbostic 
11767503Smckusick /* Checksum -- last valid disk field. */
11867503Smckusick 	u_int32_t lfs_cksum;		/* checksum for superblock checking */
11967503Smckusick 
12051155Sbostic /* These fields are set at mount time and are meaningless on disk. */
12167503Smckusick 	struct segment *lfs_sp;		/* current segment being written */
12267503Smckusick 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
12367503Smckusick 	u_long	  lfs_seglock;		/* single-thread the segment writer */
12467503Smckusick 	pid_t	  lfs_lockpid;		/* pid of lock holder */
12567503Smckusick 	u_long	  lfs_iocount;		/* number of ios pending */
12667503Smckusick 	u_long	  lfs_writer;		/* don't allow any dirops to start */
12767503Smckusick 	u_long	  lfs_dirops;		/* count of active directory ops */
12867503Smckusick 	u_long	  lfs_doifile;		/* Write ifile blocks on next write */
12967503Smckusick 	u_long	  lfs_nactive;		/* Number of segments since last ckp */
13067503Smckusick 	int8_t	  lfs_fmod;		/* super block modified flag */
13167503Smckusick 	int8_t	  lfs_clean;		/* file system is clean flag */
13267503Smckusick 	int8_t	  lfs_ronly;		/* mounted read-only flag */
13367503Smckusick 	int8_t	  lfs_flags;		/* currently unused flag */
13467503Smckusick 	u_char	  lfs_fsmnt[MNAMELEN];	/* name mounted on */
135*68118Smckusick 
136*68118Smckusick 	int32_t	  lfs_pad[40];		/* round to 512 bytes */
13751183Sbostic };
13851139Sbostic 
13951155Sbostic /*
14067503Smckusick  * Inode 0:	out-of-band inode number
14167503Smckusick  * Inode 1:	IFILE inode number
14267503Smckusick  * Inode 2:	root inode
14367503Smckusick  * Inode 3:	lost+found inode number
14451155Sbostic  */
14551303Sbostic #define	LFS_UNUSED_INUM	0		/* out of band inode number */
14652079Sbostic #define	LFS_IFILE_INUM	1		/* IFILE inode number */
14752079Sbostic #define	LOSTFOUNDINO	3		/* lost+found inode number */
14852079Sbostic #define	LFS_FIRST_INUM	4		/* first free inode number */
14951139Sbostic 
15051850Sbostic /* Address calculations for metadata located in the inode */
15151850Sbostic #define	S_INDIR(fs)	-NDADDR
15252997Sstaelin #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
15352997Sstaelin #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
15451183Sbostic 
15551350Sbostic /* Unassigned disk address. */
15651350Sbostic #define	UNASSIGNED	-1
15751350Sbostic 
15855934Sbostic /* Unused logical block number */
15955934Sbostic #define LFS_UNUSED_LBN	-1
16055934Sbostic 
16151183Sbostic typedef struct ifile IFILE;
16251183Sbostic struct ifile {
16367503Smckusick 	u_int32_t if_version;		/* inode version number */
16451155Sbostic #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
16567503Smckusick 	daddr_t	  if_daddr;		/* inode disk address */
16667503Smckusick 	ino_t	  if_nextfree;		/* next-unallocated inode */
16751183Sbostic };
16851139Sbostic 
16951850Sbostic /*
17051850Sbostic  * Cleaner information structure.  This resides in the ifile and is used
17151850Sbostic  * to pass information between the cleaner and the kernel.
17251850Sbostic  */
17351850Sbostic typedef struct _cleanerinfo {
17467503Smckusick 	u_int32_t clean;		/* K: number of clean segments */
17567503Smckusick 	u_int32_t dirty;		/* K: number of dirty segments */
17651850Sbostic } CLEANERINFO;
17751139Sbostic 
17864607Sbostic #define	CLEANSIZE_SU(fs)						\
17951850Sbostic 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
18051140Sbostic 
18151140Sbostic /*
18251140Sbostic  * All summary blocks are the same size, so we can always read a summary
18351303Sbostic  * block easily from a segment.
18451140Sbostic  */
18551140Sbostic #define	LFS_SUMMARY_SIZE	512
18651140Sbostic 
18751139Sbostic /* On-disk segment summary information */
18851183Sbostic typedef struct segsum SEGSUM;
18951183Sbostic struct segsum {
19067503Smckusick 	u_int32_t ss_sumsum;		/* check sum of summary block */
19167503Smckusick 	u_int32_t ss_datasum;		/* check sum of data */
19267503Smckusick 	daddr_t	  ss_next;		/* next segment */
19367503Smckusick 	u_int32_t ss_create;		/* creation time stamp */
19467503Smckusick 	u_int16_t ss_nfinfo;		/* number of file info structures */
19567503Smckusick 	u_int16_t ss_ninos;		/* number of inodes in summary */
19667503Smckusick 
19754264Sbostic #define	SS_DIROP	0x01		/* segment begins a dirop */
19854264Sbostic #define	SS_CONT		0x02		/* more partials to finish this write*/
19967503Smckusick 	u_int16_t ss_flags;		/* used for directory operations */
20067503Smckusick 	u_int16_t ss_pad;		/* extra space */
20152993Sbostic 	/* FINFO's and inode daddr's... */
20251183Sbostic };
20351139Sbostic 
20451155Sbostic /* NINDIR is the number of indirects in a file system block. */
20551155Sbostic #define	NINDIR(fs)	((fs)->lfs_nindir)
20651155Sbostic 
20751155Sbostic /* INOPB is the number of inodes in a secondary storage block. */
20851155Sbostic #define	INOPB(fs)	((fs)->lfs_inopb)
20951155Sbostic 
21051155Sbostic #define	blksize(fs)		((fs)->lfs_bsize)
21151155Sbostic #define	blkoff(fs, loc)		((loc) & (fs)->lfs_bmask)
21251155Sbostic #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
21355587Sbostic #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
21451155Sbostic #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
21551155Sbostic #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
21664607Sbostic #define numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */	\
21751183Sbostic 	((loc) >> (fs)->lfs_bshift)
21851350Sbostic 
21964607Sbostic #define	datosn(fs, daddr)	/* disk address to segment number */	\
22052079Sbostic 	(((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
22164607Sbostic #define sntoda(fs, sn) 		/* segment number to disk address */	\
22264607Sbostic 	((daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) +	\
22352079Sbostic 	    (fs)->lfs_sboffs[0]))
22452079Sbostic 
22551925Sbostic /* Read in the block with the cleaner info from the ifile. */
22664607Sbostic #define LFS_CLEANERINFO(CP, F, BP) {					\
22764607Sbostic 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
22864607Sbostic 	if (bread((F)->lfs_ivnode,					\
22964607Sbostic 	    (daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP)))			\
23064607Sbostic 		panic("lfs: ifile read");				\
23164607Sbostic 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
23251925Sbostic }
23351925Sbostic 
23451850Sbostic /* Read in the block with a specific inode from the ifile. */
23564607Sbostic #define	LFS_IENTRY(IP, F, IN, BP) {					\
23664607Sbostic 	int _e;								\
23764607Sbostic 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
23864607Sbostic 	if (_e = bread((F)->lfs_ivnode,					\
23964607Sbostic 	    (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\
24064607Sbostic 	    (F)->lfs_bsize, NOCRED, &(BP)))				\
24164607Sbostic 		panic("lfs: ifile read %d", _e);			\
24264607Sbostic 	(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;		\
24351481Sbostic }
24451481Sbostic 
24551850Sbostic /* Read in the block with a specific segment usage entry from the ifile. */
24664607Sbostic #define	LFS_SEGENTRY(SP, F, IN, BP) {					\
24764607Sbostic 	int _e;								\
24864607Sbostic 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
24964607Sbostic 	if (_e = bread((F)->lfs_ivnode,					\
25064607Sbostic 	    ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz,		\
25164607Sbostic 	    (F)->lfs_bsize, NOCRED, &(BP)))				\
25264607Sbostic 		panic("lfs: ifile read: %d", _e);			\
25364607Sbostic 	(SP) = (SEGUSE *)(BP)->b_data + ((IN) & (F)->lfs_sepb - 1);	\
25451850Sbostic }
25551850Sbostic 
25655934Sbostic /*
25755934Sbostic  * Determine if there is enough room currently available to write db
25855934Sbostic  * disk blocks.  We need enough blocks for the new blocks, the current,
25955934Sbostic  * inode blocks, a summary block, plus potentially the ifile inode and
26055934Sbostic  * the segment usage table, plus an ifile page.
26155934Sbostic  */
26255934Sbostic #define LFS_FITS(fs, db)						\
26367503Smckusick 	((int32_t)((db + ((fs)->lfs_uinodes + INOPB((fs))) / 		\
26467503Smckusick 	INOPB((fs)) + fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE +	\
26564607Sbostic 	(fs)->lfs_segtabsz)) < (fs)->lfs_avail)
26651936Sbostic 
26755934Sbostic /* Determine if a buffer belongs to the ifile */
26855934Sbostic #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
26964607Sbostic 
27051850Sbostic /*
27151850Sbostic  * Structures used by lfs_bmapv and lfs_markv to communicate information
27251850Sbostic  * about inodes and data blocks.
27351850Sbostic  */
27451850Sbostic typedef struct block_info {
27551850Sbostic 	ino_t	bi_inode;		/* inode # */
27655667Sbostic 	daddr_t	bi_lbn;			/* logical block w/in file */
27751850Sbostic 	daddr_t	bi_daddr;		/* disk address of block */
27851850Sbostic 	time_t	bi_segcreate;		/* origin segment create time */
27956189Smargo 	int	bi_version;		/* file version number */
28051850Sbostic 	void	*bi_bp;			/* data buffer */
28151850Sbostic } BLOCK_INFO;
28251850Sbostic 
28355934Sbostic /* In-memory description of a segment about to be written. */
28455934Sbostic struct segment {
28567503Smckusick 	struct lfs	 *fs;		/* file system pointer */
28655934Sbostic 	struct buf	**bpp;		/* pointer to buffer array */
28755934Sbostic 	struct buf	**cbpp;		/* pointer to next available bp */
28855934Sbostic 	struct buf	**start_bpp;	/* pointer to first bp in this set */
28967503Smckusick 	struct buf	 *ibp;		/* buffer pointer to inode page */
29067503Smckusick 	struct finfo	 *fip;		/* current fileinfo pointer */
29167503Smckusick 	struct vnode	 *vp;		/* vnode being gathered */
29267503Smckusick 	void	 *segsum;		/* segment summary info */
29367503Smckusick 	u_int32_t ninodes;		/* number of inodes in this segment */
29467503Smckusick 	u_int32_t seg_bytes_left;	/* bytes left in segment */
29567503Smckusick 	u_int32_t sum_bytes_left;	/* bytes left in summary block */
29667503Smckusick 	u_int32_t seg_number;		/* number of this segment */
29767503Smckusick 	daddr_t  *start_lbp;		/* beginning lbn for this set */
29867503Smckusick 
29955934Sbostic #define	SEGM_CKP	0x01		/* doing a checkpoint */
30055934Sbostic #define	SEGM_CLEAN	0x02		/* cleaner call; don't sort */
30157065Smargo #define	SEGM_SYNC	0x04		/* wait for segment */
30267503Smckusick 	u_int16_t seg_flags;		/* run-time flags for this segment */
30355934Sbostic };
30456154Smargo 
30564607Sbostic #define ISSPACE(F, BB, C)						\
30664607Sbostic 	(((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) ||		\
30756154Smargo 	((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
30856154Smargo 
30964607Sbostic #define IS_FREESPACE(F, BB)						\
31056154Smargo 	((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB)))
31156154Smargo 
31264607Sbostic #define ISSPACE_XXX(F, BB)						\
31356154Smargo 	((F)->lfs_bfree >= (BB))
31456154Smargo 
31557065Smargo #define DOSTATS
31657065Smargo #ifdef DOSTATS
31757065Smargo /* Statistics Counters */
31857065Smargo struct lfs_stats {
31967503Smckusick 	u_int	segsused;
32067503Smckusick 	u_int	psegwrites;
32167503Smckusick 	u_int	psyncwrites;
32267503Smckusick 	u_int	pcleanwrites;
32367503Smckusick 	u_int	blocktot;
32467503Smckusick 	u_int	cleanblocks;
32567503Smckusick 	u_int	ncheckpoints;
32667503Smckusick 	u_int	nwrites;
32767503Smckusick 	u_int	nsync_writes;
32867503Smckusick 	u_int	wait_exceeded;
32967503Smckusick 	u_int	write_exceeded;
33067503Smckusick 	u_int	flush_invoked;
33157065Smargo };
33257065Smargo extern struct lfs_stats lfs_stats;
33357065Smargo #endif
334