151149Sbostic /*-
261524Sbostic * Copyright (c) 1991, 1993
361524Sbostic * The Regents of the University of California. All rights reserved.
451149Sbostic *
551149Sbostic * %sccs.include.redist.c%
651149Sbostic */
751149Sbostic
851149Sbostic #ifndef lint
9*69673Smargo static char sccsid[] = "@(#)lfs.c 8.5 (Berkeley) 05/24/95";
1051149Sbostic #endif /* not lint */
1151149Sbostic
1251149Sbostic #include <sys/param.h>
1351149Sbostic #include <sys/disklabel.h>
1451864Sbostic #include <sys/time.h>
1552205Sbostic #include <sys/mount.h>
1651866Sbostic
1751866Sbostic #include <ufs/ufs/dir.h>
1851866Sbostic #include <ufs/ufs/quota.h>
1951866Sbostic #include <ufs/ufs/dinode.h>
2051866Sbostic #include <ufs/lfs/lfs.h>
2151866Sbostic
2251149Sbostic #include <unistd.h>
2351149Sbostic #include <errno.h>
2451149Sbostic #include <stdlib.h>
2551149Sbostic #include <string.h>
2651149Sbostic #include "config.h"
2751149Sbostic #include "extern.h"
2851149Sbostic
2955389Sbostic /*
3055389Sbostic * This table is indexed by the log base 2 of the block size.
3155389Sbostic * It returns the maximum file size allowed in a file system
3255389Sbostic * with the specified block size. For block sizes smaller than
3355389Sbostic * 8K, the size is limited by tha maximum number of blocks that
3455389Sbostic * can be reached by triply indirect blocks:
3555389Sbostic * NDADDR + INOPB(bsize) + INOPB(bsize)^2 + INOPB(bsize)^3
3655389Sbostic * For block size of 8K or larger, the file size is limited by the
3755389Sbostic * number of blocks that can be represented in the file system. Since
3855389Sbostic * we use negative block numbers to represent indirect blocks, we can
3955389Sbostic * have a maximum of 2^31 blocks.
4055389Sbostic */
4155389Sbostic
4255389Sbostic u_quad_t maxtable[] = {
4355389Sbostic /* 1 */ -1,
4455389Sbostic /* 2 */ -1,
4555389Sbostic /* 4 */ -1,
4655389Sbostic /* 8 */ -1,
4755389Sbostic /* 16 */ -1,
4855389Sbostic /* 32 */ -1,
4955389Sbostic /* 64 */ -1,
5055389Sbostic /* 128 */ -1,
5155389Sbostic /* 256 */ -1,
5255389Sbostic /* 512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128,
5355389Sbostic /* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256,
5455389Sbostic /* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512,
5555389Sbostic /* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024,
5656891Sbostic /* 8192 */ 1 << 31,
5756891Sbostic /* 16 K */ 1 << 31,
5856891Sbostic /* 32 K */ 1 << 31,
5955389Sbostic };
6055389Sbostic
6151866Sbostic static struct lfs lfs_default = {
6251149Sbostic /* lfs_magic */ LFS_MAGIC,
6351149Sbostic /* lfs_version */ LFS_VERSION,
6451149Sbostic /* lfs_size */ 0,
6551866Sbostic /* lfs_ssize */ DFL_LFSSEG/DFL_LFSBLOCK,
6651149Sbostic /* lfs_dsize */ 0,
6751149Sbostic /* lfs_bsize */ DFL_LFSBLOCK,
68*69673Smargo /* lfs_fsize */ DFL_LFSFRAG,
6951864Sbostic /* lfs_frag */ 1,
7051149Sbostic /* lfs_free */ LFS_FIRST_INUM,
7151864Sbostic /* lfs_bfree */ 0,
7251864Sbostic /* lfs_nfiles */ 0,
7356031Sbostic /* lfs_avail */ 0,
7456031Sbostic /* lfs_uinodes */ 0,
7551149Sbostic /* lfs_idaddr */ 0,
7651149Sbostic /* lfs_ifile */ LFS_IFILE_INUM,
7751149Sbostic /* lfs_lastseg */ 0,
7851864Sbostic /* lfs_nextseg */ 0,
7951866Sbostic /* lfs_curseg */ 0,
8051866Sbostic /* lfs_offset */ 0,
8154263Sbostic /* lfs_lastpseg */ 0,
8251149Sbostic /* lfs_tstamp */ 0,
8351149Sbostic /* lfs_minfree */ MINFREE,
8455389Sbostic /* lfs_maxfilesize */ 0,
8551866Sbostic /* lfs_dbpseg */ DFL_LFSSEG/DEV_BSIZE,
8651149Sbostic /* lfs_inopb */ DFL_LFSBLOCK/sizeof(struct dinode),
8751149Sbostic /* lfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE),
8851866Sbostic /* lfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE),
8951149Sbostic /* lfs_nindir */ DFL_LFSBLOCK/sizeof(daddr_t),
9051149Sbostic /* lfs_nseg */ 0,
9151149Sbostic /* lfs_nspf */ 0,
9251866Sbostic /* lfs_cleansz */ 0,
9351149Sbostic /* lfs_segtabsz */ 0,
9451149Sbostic /* lfs_segmask */ DFL_LFSSEG_MASK,
9551149Sbostic /* lfs_segshift */ DFL_LFSSEG_SHIFT,
9651149Sbostic /* lfs_bmask */ DFL_LFSBLOCK_MASK,
9751149Sbostic /* lfs_bshift */ DFL_LFSBLOCK_SHIFT,
98*69673Smargo /* lfs_ffmask */ DFL_LFS_FFMASK,
99*69673Smargo /* lfs_ffshift */ DFL_LFS_FFSHIFT,
100*69673Smargo /* lfs_fbmask */ DFL_LFS_FBMASK,
101*69673Smargo /* lfs_fbshift */ DFL_LFS_FBSHIFT,
10251149Sbostic /* lfs_fsbtodb */ 0,
10355596Sbostic /* lfs_sushift */ 0,
10451149Sbostic /* lfs_sboffs */ { 0 },
10556861Smargo /* lfs_sp */ NULL,
10651864Sbostic /* lfs_ivnode */ NULL,
10754710Sbostic /* lfs_seglock */ 0,
10856863Smargo /* lfs_lockpid */ 0,
10951864Sbostic /* lfs_iocount */ 0,
11054263Sbostic /* lfs_writer */ 0,
11154263Sbostic /* lfs_dirops */ 0,
11254263Sbostic /* lfs_doifile */ 0,
11356031Sbostic /* lfs_nactive */ 0,
11451149Sbostic /* lfs_fmod */ 0,
11551149Sbostic /* lfs_clean */ 0,
11651149Sbostic /* lfs_ronly */ 0,
11751149Sbostic /* lfs_flags */ 0,
11851149Sbostic /* lfs_fsmnt */ { 0 },
11951864Sbostic /* lfs_pad */ { 0 },
12067384Smkm /* lfs_cksum */ 0,
12167384Smkm /* lfs_maxsymlinklen */ MAXSYMLINKLEN
12251149Sbostic };
12351149Sbostic
12451864Sbostic
12551864Sbostic struct direct lfs_root_dir[] = {
12654715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 1, "."},
12754715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."},
12854715Sbostic { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"},
12954715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"},
13051864Sbostic };
13151864Sbostic
13251864Sbostic struct direct lfs_lf_dir[] = {
13354715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
13454715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
13551864Sbostic };
13651864Sbostic
13751864Sbostic static daddr_t make_dinode
13851866Sbostic __P((ino_t, struct dinode *, int, daddr_t, struct lfs *));
13951864Sbostic static void make_dir __P(( void *, struct direct *, int));
14051149Sbostic static void put __P((int, off_t, void *, size_t));
14151149Sbostic
14251149Sbostic int
make_lfs(fd,lp,partp,minfree,block_size,frag_size,seg_size)143*69673Smargo make_lfs(fd, lp, partp, minfree, block_size, frag_size, seg_size)
14451149Sbostic int fd;
14551149Sbostic struct disklabel *lp;
14651149Sbostic struct partition *partp;
14751149Sbostic int minfree;
14851149Sbostic int block_size;
149*69673Smargo int frag_size;
15051149Sbostic int seg_size;
15151149Sbostic {
15251149Sbostic struct dinode *dip; /* Pointer to a disk inode */
15351864Sbostic struct dinode *dpagep; /* Pointer to page of disk inodes */
15451866Sbostic CLEANERINFO *cleaninfo; /* Segment cleaner information table */
15551149Sbostic FINFO file_info; /* File info structure in summary blocks */
15651149Sbostic IFILE *ifile; /* Pointer to array of ifile structures */
15751149Sbostic IFILE *ip; /* Pointer to array of ifile structures */
15851866Sbostic struct lfs *lfsp; /* Superblock */
15951864Sbostic SEGUSE *segp; /* Segment usage table */
16051864Sbostic SEGUSE *segtable; /* Segment usage table */
16151149Sbostic SEGSUM summary; /* Segment summary structure */
16251866Sbostic SEGSUM *sp; /* Segment summary pointer */
16351149Sbostic daddr_t last_sb_addr; /* Address of superblocks */
16451864Sbostic daddr_t last_addr; /* Previous segment address */
16551149Sbostic daddr_t sb_addr; /* Address of superblocks */
16651864Sbostic daddr_t seg_addr; /* Address of current segment */
16751866Sbostic void *ipagep; /* Pointer to the page we use to write stuff */
16851149Sbostic void *sump; /* Used to copy stuff into segment buffer */
16951149Sbostic u_long *block_array; /* Array of logical block nos to put in sum */
17051866Sbostic u_long blocks_used; /* Number of blocks in first segment */
17151866Sbostic u_long *dp; /* Used to computed checksum on data */
17251866Sbostic u_long *datasump; /* Used to computed checksum on data */
17351149Sbostic int block_array_size; /* How many entries in block array */
17451149Sbostic int bsize; /* Block size */
175*69673Smargo int fsize; /* Fragment size */
17651149Sbostic int db_per_fb; /* Disk blocks per file block */
17751864Sbostic int i, j;
17851866Sbostic int off; /* Offset at which to write */
17951149Sbostic int sb_interval; /* number of segs between super blocks */
18051149Sbostic int seg_seek; /* Seek offset for a segment */
18151149Sbostic int ssize; /* Segment size */
18251149Sbostic int sum_size; /* Size of the summary block */
18351149Sbostic
18451149Sbostic lfsp = &lfs_default;
18551149Sbostic
18651149Sbostic if (!(bsize = block_size))
18751149Sbostic bsize = DFL_LFSBLOCK;
188*69673Smargo if (!(fsize = frag_size))
189*69673Smargo fsize = DFL_LFSFRAG;
19051149Sbostic if (!(ssize = seg_size))
19151149Sbostic ssize = DFL_LFSSEG;
19251149Sbostic
19351149Sbostic /* Modify parts of superblock overridden by command line arguments */
194*69673Smargo if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) {
19551149Sbostic lfsp->lfs_bshift = log2(bsize);
19651149Sbostic if (1 << lfsp->lfs_bshift != bsize)
19751149Sbostic fatal("%d: block size not a power of 2", bsize);
19851149Sbostic lfsp->lfs_bsize = bsize;
199*69673Smargo lfsp->lfs_fsize = fsize;
20051149Sbostic lfsp->lfs_bmask = bsize - 1;
20151149Sbostic lfsp->lfs_inopb = bsize / sizeof(struct dinode);
202*69673Smargo lfsp->lfs_ffmask = fsize - 1;
203*69673Smargo lfsp->lfs_ffshift = log2(fsize);
204*69673Smargo if (1 << lfsp->lfs_ffshift != fsize)
205*69673Smargo fatal("%d: frag size not a power of 2", fsize);
206*69673Smargo lfsp->lfs_frag = numfrags(lfsp, bsize);
207*69673Smargo lfsp->lfs_fbmask = lfsp->lfs_frag - 1;
208*69673Smargo lfsp->lfs_fbshift = log2(lfsp->lfs_frag);
20951149Sbostic /* MIS -- should I round to power of 2 */
21051149Sbostic lfsp->lfs_ifpb = bsize / sizeof(IFILE);
21151866Sbostic lfsp->lfs_sepb = bsize / sizeof(SEGUSE);
21251149Sbostic lfsp->lfs_nindir = bsize / sizeof(daddr_t);
21351149Sbostic }
21451149Sbostic
21551149Sbostic if (ssize != DFL_LFSSEG) {
21651149Sbostic lfsp->lfs_segshift = log2(ssize);
21751149Sbostic if (1 << lfsp->lfs_segshift != ssize)
21851149Sbostic fatal("%d: segment size not power of 2", ssize);
21951149Sbostic lfsp->lfs_ssize = ssize;
22051149Sbostic lfsp->lfs_segmask = ssize - 1;
22156861Smargo lfsp->lfs_dbpseg = ssize / DEV_BSIZE;
22251149Sbostic }
22351149Sbostic lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift;
22451149Sbostic
22551149Sbostic if (minfree)
22651149Sbostic lfsp->lfs_minfree = minfree;
22751149Sbostic
22851149Sbostic /*
22951149Sbostic * Fill in parts of superblock that can be computed from file system
23051149Sbostic * size, disk geometry and current time.
23151149Sbostic */
23251149Sbostic db_per_fb = bsize/lp->d_secsize;
23351149Sbostic lfsp->lfs_fsbtodb = log2(db_per_fb);
23455596Sbostic lfsp->lfs_sushift = log2(lfsp->lfs_sepb);
23551149Sbostic lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb;
23651149Sbostic lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift);
23751149Sbostic lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize;
23855389Sbostic lfsp->lfs_maxfilesize = maxtable[lfsp->lfs_bshift] << lfsp->lfs_bshift;
23951864Sbostic
24051864Sbostic /*
24156031Sbostic * The number of free blocks is set from the number of segments times
24256192Smargo * the segment size - 2 (that we never write because we need to make
24356192Smargo * sure the cleaner can run). Then we'll subtract off the room for the
24456031Sbostic * superblocks ifile entries and segment usage table.
24551864Sbostic */
24656192Smargo lfsp->lfs_dsize = fsbtodb(lfsp, (lfsp->lfs_nseg - 2) * lfsp->lfs_ssize);
24756192Smargo lfsp->lfs_bfree = lfsp->lfs_dsize;
24851149Sbostic lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp);
24951866Sbostic lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp);
25051149Sbostic if ((lfsp->lfs_tstamp = time(NULL)) == -1)
25151149Sbostic fatal("time: %s", strerror(errno));
25251149Sbostic if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
25351149Sbostic sb_interval = LFS_MIN_SBINTERVAL;
25451149Sbostic
25551149Sbostic /*
25651149Sbostic * Now, lay out the file system. We need to figure out where
25751149Sbostic * the superblocks go, initialize the checkpoint information
25851149Sbostic * for the first two superblocks, initialize the segment usage
25951149Sbostic * information, put the segusage information in the ifile, create
26051149Sbostic * the first block of IFILE structures, and link all the IFILE
26151149Sbostic * structures into a free list.
26251149Sbostic */
26351149Sbostic
26451149Sbostic /* Figure out where the superblocks are going to live */
26551149Sbostic lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize;
26651149Sbostic for (i = 1; i < LFS_MAXNUMSB; i++) {
26751149Sbostic sb_addr = ((i * sb_interval) <<
26851149Sbostic (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb))
26951149Sbostic + lfsp->lfs_sboffs[0];
27051149Sbostic if (sb_addr > partp->p_size)
27151149Sbostic break;
27251149Sbostic lfsp->lfs_sboffs[i] = sb_addr;
27351149Sbostic }
27451149Sbostic last_sb_addr = lfsp->lfs_sboffs[i - 1];
27551149Sbostic lfsp->lfs_lastseg = lfsp->lfs_sboffs[0];
27651864Sbostic lfsp->lfs_nextseg =
27751864Sbostic lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0];
27851866Sbostic lfsp->lfs_curseg = lfsp->lfs_lastseg;
27951149Sbostic
28051149Sbostic /*
28151149Sbostic * Initialize the segment usage table. The first segment will
28251866Sbostic * contain the superblock, the cleanerinfo (cleansz), the segusage
28351866Sbostic * table * (segtabsz), 1 block's worth of IFILE entries, the root
28451866Sbostic * directory, the lost+found directory and one block's worth of
28551866Sbostic * inodes (containing the ifile, root, and l+f inodes).
28651149Sbostic */
28751866Sbostic if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift)))
28851866Sbostic fatal("%s", strerror(errno));
28951930Sbostic cleaninfo->clean = lfsp->lfs_nseg - 1;
29051930Sbostic cleaninfo->dirty = 1;
29151930Sbostic
29251149Sbostic if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift)))
29351149Sbostic fatal("%s", strerror(errno));
29451149Sbostic segp = segtable;
29551866Sbostic blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4;
29656031Sbostic segp->su_nbytes = ((blocks_used - 1) << lfsp->lfs_bshift) +
29756031Sbostic 3 * sizeof(struct dinode) + LFS_SUMMARY_SIZE;
29851149Sbostic segp->su_lastmod = lfsp->lfs_tstamp;
29955596Sbostic segp->su_nsums = 1; /* 1 summary blocks */
30055596Sbostic segp->su_ninos = 1; /* 1 inode block */
30151930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY;
30256031Sbostic lfsp->lfs_bfree -= LFS_SUMMARY_SIZE / lp->d_secsize;
30356031Sbostic lfsp->lfs_bfree -=
30456031Sbostic fsbtodb(lfsp, lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4);
30551149Sbostic
30651866Sbostic /*
30751866Sbostic * Now figure out the address of the ifile inode. The inode block
30851866Sbostic * appears immediately after the segment summary.
30951866Sbostic */
31051866Sbostic lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) /
31151866Sbostic lp->d_secsize;
31251149Sbostic
31351149Sbostic for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) {
31451864Sbostic if ((i % sb_interval) == 0) {
31551930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK;
31655596Sbostic lfsp->lfs_bfree -= (LFS_SBPAD / lp->d_secsize);
31752088Sbostic } else
31851930Sbostic segp->su_flags = 0;
31951149Sbostic segp->su_lastmod = 0;
32052088Sbostic segp->su_nbytes = 0;
32155596Sbostic segp->su_ninos = 0;
32255713Sbostic segp->su_nsums = 0;
32351149Sbostic }
32451149Sbostic
32556031Sbostic /*
32656031Sbostic * Initialize dynamic accounting. The blocks available for
32756031Sbostic * writing are the bfree blocks minus 1 segment summary for
32856031Sbostic * each segment since you can't write any new data without
32956031Sbostic * creating a segment summary - 2 segments that the cleaner
33056031Sbostic * needs.
33156031Sbostic */
33256031Sbostic lfsp->lfs_avail = lfsp->lfs_bfree - lfsp->lfs_nseg -
33356031Sbostic fsbtodb(lfsp, 2 * lfsp->lfs_ssize);
33456031Sbostic lfsp->lfs_uinodes = 0;
33551149Sbostic /*
33651149Sbostic * Ready to start writing segments. The first segment is different
33751149Sbostic * because it contains the segment usage table and the ifile inode
33851864Sbostic * as well as a superblock. For the rest of the segments, set the
33951864Sbostic * time stamp to be 0 so that the first segment is the most recent.
34051864Sbostic * For each segment that is supposed to contain a copy of the super
34151864Sbostic * block, initialize its first few blocks and its segment summary
34251864Sbostic * to indicate this.
34351149Sbostic */
34451864Sbostic lfsp->lfs_nfiles = LFS_FIRST_INUM - 1;
34551149Sbostic lfsp->lfs_cksum =
34651866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum));
34751149Sbostic
34851864Sbostic /* Now create a block of disk inodes */
34951864Sbostic if (!(dpagep = malloc(lfsp->lfs_bsize)))
35051864Sbostic fatal("%s", strerror(errno));
35151864Sbostic dip = (struct dinode *)dpagep;
35268998Sbostic memset(dip, 0, lfsp->lfs_bsize);
35351864Sbostic
35451866Sbostic /* Create a block of IFILE structures. */
35551866Sbostic if (!(ipagep = malloc(lfsp->lfs_bsize)))
35651149Sbostic fatal("%s", strerror(errno));
35751866Sbostic ifile = (IFILE *)ipagep;
35851864Sbostic
35951866Sbostic /*
36051866Sbostic * Initialize IFILE. It is the next block following the
36151866Sbostic * block of inodes (whose address has been calculated in
36251866Sbostic * lfsp->lfs_idaddr;
36351866Sbostic */
36451866Sbostic sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize;
36551866Sbostic sb_addr = make_dinode(LFS_IFILE_INUM, dip,
36651866Sbostic lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp);
36751864Sbostic dip->di_mode = IFREG|IREAD|IWRITE;
36851864Sbostic ip = &ifile[LFS_IFILE_INUM];
36951864Sbostic ip->if_version = 1;
37051864Sbostic ip->if_daddr = lfsp->lfs_idaddr;
37151864Sbostic
37251864Sbostic /* Initialize the ROOT Directory */
37351864Sbostic sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp);
37451864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC;
37551864Sbostic dip->di_size = DIRBLKSIZ;
37651864Sbostic dip->di_nlink = 3;
37751864Sbostic ip = &ifile[ROOTINO];
37851864Sbostic ip->if_version = 1;
37951864Sbostic ip->if_daddr = lfsp->lfs_idaddr;
38051864Sbostic
38151864Sbostic /* Initialize the lost+found Directory */
38251864Sbostic sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp);
38351864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC;
38451864Sbostic dip->di_size = DIRBLKSIZ;
38551864Sbostic dip->di_nlink = 2;
38651864Sbostic ip = &ifile[LOSTFOUNDINO];
38751864Sbostic ip->if_version = 1;
38851864Sbostic ip->if_daddr = lfsp->lfs_idaddr;
38951864Sbostic
39051864Sbostic /* Make all the other dinodes invalid */
39151864Sbostic for (i = INOPB(lfsp)-3, dip++; i; i--, dip++)
39256861Smargo dip->di_inumber = LFS_UNUSED_INUM;
39351864Sbostic
39451864Sbostic
39551864Sbostic /* Link remaining IFILE entries in free list */
39651864Sbostic for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM;
39751864Sbostic i < lfsp->lfs_ifpb; ++ip) {
39851149Sbostic ip->if_version = 1;
39951149Sbostic ip->if_daddr = LFS_UNUSED_DADDR;
40051149Sbostic ip->if_nextfree = ++i;
40151149Sbostic }
40251149Sbostic ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM;
40351149Sbostic
40451866Sbostic /* Now, write the segment */
40551149Sbostic
40651866Sbostic /* Compute a checksum across all the data you're writing */
40751866Sbostic dp = datasump = malloc (blocks_used * sizeof(u_long));
40855654Sbostic *dp++ = ((u_long *)dpagep)[0]; /* inode block */
40951866Sbostic for (i = 0; i < lfsp->lfs_cleansz; i++)
41051866Sbostic *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) /
41151866Sbostic sizeof(u_long)]; /* Cleaner info */
41251866Sbostic for (i = 0; i < lfsp->lfs_segtabsz; i++)
41351866Sbostic *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) /
41451866Sbostic sizeof(u_long)]; /* Segusage table */
41551866Sbostic *dp++ = ((u_long *)ifile)[0]; /* Ifile */
41651866Sbostic
41751866Sbostic /* Still need the root and l+f bytes; get them later */
41851866Sbostic
41951866Sbostic /* Write out the inode block */
42051866Sbostic off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE;
42151866Sbostic put(fd, off, dpagep, lfsp->lfs_bsize);
42251866Sbostic free(dpagep);
42351866Sbostic off += lfsp->lfs_bsize;
42451866Sbostic
42551866Sbostic /* Write out the ifile */
42651866Sbostic
42751866Sbostic put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift);
42851866Sbostic off += (lfsp->lfs_cleansz << lfsp->lfs_bshift);
42951866Sbostic (void)free(cleaninfo);
43051866Sbostic
43151866Sbostic put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift);
43251866Sbostic off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift);
43351866Sbostic (void)free(segtable);
43451866Sbostic
43551866Sbostic put(fd, off, ifile, lfsp->lfs_bsize);
43651866Sbostic off += lfsp->lfs_bsize;
43751866Sbostic
43851866Sbostic /*
43951866Sbostic * use ipagep for space for writing out other stuff. It used to
44051866Sbostic * contain the ifile, but we're done with it.
44151866Sbostic */
44251866Sbostic
44351864Sbostic /* Write out the root and lost and found directories */
44468998Sbostic memset(ipagep, 0, lfsp->lfs_bsize);
44551866Sbostic make_dir(ipagep, lfs_root_dir,
44651864Sbostic sizeof(lfs_root_dir) / sizeof(struct direct));
44751866Sbostic *dp++ = ((u_long *)ipagep)[0];
44851866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize);
44951866Sbostic off += lfsp->lfs_bsize;
45051149Sbostic
45168998Sbostic memset(ipagep, 0, lfsp->lfs_bsize);
45251866Sbostic make_dir(ipagep, lfs_lf_dir,
45351864Sbostic sizeof(lfs_lf_dir) / sizeof(struct direct));
45451866Sbostic *dp++ = ((u_long *)ipagep)[0];
45551866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize);
45651149Sbostic
45751866Sbostic /* Write Supberblock */
45851866Sbostic lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize;
45951866Sbostic put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs));
46051149Sbostic
46151866Sbostic /*
46251866Sbostic * Finally, calculate all the fields for the summary structure
46351866Sbostic * and write it.
46451866Sbostic */
46551866Sbostic
46651864Sbostic summary.ss_next = lfsp->lfs_nextseg;
46751149Sbostic summary.ss_create = lfsp->lfs_tstamp;
46851866Sbostic summary.ss_nfinfo = 3;
46951866Sbostic summary.ss_ninos = 3;
470*69673Smargo summary.ss_magic = SS_MAGIC;
47151866Sbostic summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used);
47251149Sbostic
47351864Sbostic /*
47451864Sbostic * Make sure that we don't overflow a summary block. We have to
47551866Sbostic * record: FINFO structures for ifile, root, and l+f. The number
47651866Sbostic * of blocks recorded for the ifile is determined by the size of
47751866Sbostic * the cleaner info and the segments usage table. There is room
47851866Sbostic * for one block included in sizeof(FINFO) so we don't need to add
47951866Sbostic * any extra space for the ROOT and L+F, and one block of the ifile
48051866Sbostic * is already counted. Finally, we leave room for 1 inode block
48151866Sbostic * address.
48251864Sbostic */
48353248Sbostic sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) +
48451866Sbostic (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long);
48551149Sbostic #define SUMERR \
48651149Sbostic "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes."
48751149Sbostic if (sum_size > LFS_SUMMARY_SIZE)
48851149Sbostic fatal(SUMERR, sum_size);
48951149Sbostic
49051866Sbostic block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1;
49151149Sbostic
49251149Sbostic if (!(block_array = malloc(block_array_size *sizeof(int))))
49351149Sbostic fatal("%s: %s", special, strerror(errno));
49451149Sbostic
49551149Sbostic /* fill in the array */
49651866Sbostic for (i = 0; i < block_array_size; i++)
49751149Sbostic block_array[i] = i;
49851149Sbostic
49951149Sbostic /* copy into segment */
50051866Sbostic sump = ipagep;
50168998Sbostic memmove(sump, &summary, sizeof(SEGSUM));
50251149Sbostic sump += sizeof(SEGSUM);
50351149Sbostic
50451149Sbostic /* Now, add the ifile */
50551866Sbostic file_info.fi_nblocks = block_array_size;
50651149Sbostic file_info.fi_version = 1;
507*69673Smargo file_info.fi_lastlength = lfsp->lfs_bsize;
50851149Sbostic file_info.fi_ino = LFS_IFILE_INUM;
50951149Sbostic
51068998Sbostic memmove(sump, &file_info, sizeof(FINFO) - sizeof(u_long));
51151149Sbostic sump += sizeof(FINFO) - sizeof(u_long);
51268998Sbostic memmove(sump, block_array, sizeof(u_long) * file_info.fi_nblocks);
51351864Sbostic sump += sizeof(u_long) * file_info.fi_nblocks;
51451149Sbostic
51551864Sbostic /* Now, add the root directory */
51651864Sbostic file_info.fi_nblocks = 1;
51751864Sbostic file_info.fi_version = 1;
518*69673Smargo file_info.fi_lastlength = lfsp->lfs_bsize;
51951864Sbostic file_info.fi_ino = ROOTINO;
52051864Sbostic file_info.fi_blocks[0] = 0;
52168998Sbostic memmove(sump, &file_info, sizeof(FINFO));
52251864Sbostic sump += sizeof(FINFO);
52351864Sbostic
52451864Sbostic /* Now, add the lost and found */
52551864Sbostic file_info.fi_ino = LOSTFOUNDINO;
52668998Sbostic memmove(sump, &file_info, sizeof(FINFO));
52751864Sbostic
52851866Sbostic ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] =
52951866Sbostic lfsp->lfs_idaddr;
53051866Sbostic ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum),
53151866Sbostic LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum));
53251866Sbostic put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE);
53351864Sbostic
53451866Sbostic sp = (SEGSUM *)ipagep;
53551866Sbostic sp->ss_create = 0;
53651866Sbostic sp->ss_nfinfo = 0;
53751866Sbostic sp->ss_ninos = 0;
53851866Sbostic sp->ss_datasum = 0;
539*69673Smargo sp->ss_magic = SS_MAGIC;
54051149Sbostic
54155480Sbostic /* Now write the summary block for the next partial so it's invalid */
54255480Sbostic lfsp->lfs_tstamp = 0;
54355480Sbostic off += lfsp->lfs_bsize;
54455480Sbostic sp->ss_sumsum =
54555480Sbostic cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum));
54655480Sbostic put(fd, off, sp, LFS_SUMMARY_SIZE);
54755480Sbostic
54851149Sbostic /* Now, write rest of segments containing superblocks */
54951864Sbostic lfsp->lfs_cksum =
55051866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum));
55151864Sbostic for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1;
55251864Sbostic i < lfsp->lfs_nseg; i++) {
55351149Sbostic
55451864Sbostic seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb;
55551866Sbostic sp->ss_next = last_addr;
55651866Sbostic last_addr = seg_addr;
55751866Sbostic seg_seek = seg_addr * lp->d_secsize;
55851866Sbostic
55951864Sbostic if (seg_addr == lfsp->lfs_sboffs[j]) {
56051864Sbostic if (j < (LFS_MAXNUMSB - 2))
56151864Sbostic j++;
56251866Sbostic put(fd, seg_seek, lfsp, sizeof(struct lfs));
56351866Sbostic seg_seek += LFS_SBPAD;
56451866Sbostic }
56551149Sbostic
56651149Sbostic /* Summary */
56751866Sbostic sp->ss_sumsum = cksum(&sp->ss_datasum,
56851866Sbostic LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum));
56951866Sbostic put(fd, seg_seek, sp, LFS_SUMMARY_SIZE);
57051149Sbostic }
57151866Sbostic free(ipagep);
57251864Sbostic close(fd);
57351149Sbostic return (0);
57451149Sbostic }
57551149Sbostic
57651149Sbostic static void
put(fd,off,p,len)57751149Sbostic put(fd, off, p, len)
57851149Sbostic int fd;
57951149Sbostic off_t off;
58051149Sbostic void *p;
58151149Sbostic size_t len;
58251149Sbostic {
58351149Sbostic int wbytes;
58451149Sbostic
58551149Sbostic if (lseek(fd, off, SEEK_SET) < 0)
58651149Sbostic fatal("%s: %s", special, strerror(errno));
58751149Sbostic if ((wbytes = write(fd, p, len)) < 0)
58851149Sbostic fatal("%s: %s", special, strerror(errno));
58951149Sbostic if (wbytes != len)
59051149Sbostic fatal("%s: short write (%d, not %d)", special, wbytes, len);
59151149Sbostic }
59251864Sbostic
59351864Sbostic /*
59451864Sbostic * Create the root directory for this file system and the lost+found
59551864Sbostic * directory.
59651864Sbostic */
59751864Sbostic
59855389Sbostic void
lfsinit()59951864Sbostic lfsinit()
60055389Sbostic {}
60151864Sbostic
60251864Sbostic static daddr_t
make_dinode(ino,dip,nblocks,saddr,lfsp)60351864Sbostic make_dinode(ino, dip, nblocks, saddr, lfsp)
60451864Sbostic ino_t ino; /* inode we're creating */
60551864Sbostic struct dinode *dip; /* disk inode */
60651864Sbostic int nblocks; /* number of blocks in file */
60751864Sbostic daddr_t saddr; /* starting block address */
60851866Sbostic struct lfs *lfsp; /* superblock */
60951864Sbostic {
61051864Sbostic int db_per_fb, i;
61151864Sbostic
61251864Sbostic dip->di_nlink = 1;
61355440Sbostic dip->di_blocks = nblocks << lfsp->lfs_fsbtodb;
61451864Sbostic
61555480Sbostic dip->di_size = (nblocks << lfsp->lfs_bshift);
61669161Smckusick dip->di_atime = dip->di_mtime = dip->di_ctime = lfsp->lfs_tstamp;
61769161Smckusick dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0;
61856861Smargo dip->di_inumber = ino;
61951864Sbostic
62051864Sbostic #define SEGERR \
62151864Sbostic "File requires more than the number of direct blocks; increase block or segment size."
62251864Sbostic if (NDADDR < nblocks)
62351864Sbostic fatal("%s", SEGERR);
62451864Sbostic
62551864Sbostic /* Assign the block addresses for the ifile */
62651864Sbostic db_per_fb = 1 << lfsp->lfs_fsbtodb;
62755440Sbostic for (i = 0; i < nblocks; i++, saddr += db_per_fb)
62851864Sbostic dip->di_db[i] = saddr;
62951864Sbostic
63051864Sbostic return (saddr);
63151864Sbostic }
63251864Sbostic
63351864Sbostic
63451864Sbostic /*
63551864Sbostic * Construct a set of directory entries in "bufp". We assume that all the
63651864Sbostic * entries in protodir fir in the first DIRBLKSIZ.
63751864Sbostic */
63851864Sbostic static void
make_dir(bufp,protodir,entries)63951864Sbostic make_dir(bufp, protodir, entries)
64051864Sbostic void *bufp;
64151864Sbostic register struct direct *protodir;
64251864Sbostic int entries;
64351864Sbostic {
64451864Sbostic char *cp;
64551864Sbostic int i, spcleft;
64651864Sbostic
64751864Sbostic spcleft = DIRBLKSIZ;
64851864Sbostic for (cp = bufp, i = 0; i < entries - 1; i++) {
64954715Sbostic protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i]);
65068998Sbostic memmove(cp, &protodir[i], protodir[i].d_reclen);
65151864Sbostic cp += protodir[i].d_reclen;
65251864Sbostic if ((spcleft -= protodir[i].d_reclen) < 0)
65351864Sbostic fatal("%s: %s", special, "directory too big");
65451864Sbostic }
65551864Sbostic protodir[i].d_reclen = spcleft;
65668998Sbostic memmove(cp, &protodir[i], DIRSIZ(NEWDIRFMT, &protodir[i]));
65751864Sbostic }
658