151149Sbostic /*- 251149Sbostic * Copyright (c) 1991 The Regents of the University of California. 351149Sbostic * All rights reserved. 451149Sbostic * 551149Sbostic * %sccs.include.redist.c% 651149Sbostic */ 751149Sbostic 851149Sbostic #ifndef lint 9*55654Sbostic static char sccsid[] = "@(#)lfs.c 5.18 (Berkeley) 07/24/92"; 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, 5655389Sbostic /* 8192 */ 2 ^ 31, 5755389Sbostic /* 16 K */ 2 ^ 31, 5855389Sbostic /* 32 K */ 2 ^ 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, 6851864Sbostic /* lfs_fsize */ DFL_LFSBLOCK, 6951864Sbostic /* lfs_frag */ 1, 7051149Sbostic /* lfs_free */ LFS_FIRST_INUM, 7151864Sbostic /* lfs_bfree */ 0, 7251864Sbostic /* lfs_nfiles */ 0, 7351149Sbostic /* lfs_idaddr */ 0, 7451149Sbostic /* lfs_ifile */ LFS_IFILE_INUM, 7551149Sbostic /* lfs_lastseg */ 0, 7651864Sbostic /* lfs_nextseg */ 0, 7751866Sbostic /* lfs_curseg */ 0, 7851866Sbostic /* lfs_offset */ 0, 7954263Sbostic /* lfs_lastpseg */ 0, 8051149Sbostic /* lfs_tstamp */ 0, 8151149Sbostic /* lfs_minfree */ MINFREE, 8255389Sbostic /* lfs_maxfilesize */ 0, 8351866Sbostic /* lfs_dbpseg */ DFL_LFSSEG/DEV_BSIZE, 8451149Sbostic /* lfs_inopb */ DFL_LFSBLOCK/sizeof(struct dinode), 8551149Sbostic /* lfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 8651866Sbostic /* lfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 8751149Sbostic /* lfs_nindir */ DFL_LFSBLOCK/sizeof(daddr_t), 8851149Sbostic /* lfs_nseg */ 0, 8951149Sbostic /* lfs_nspf */ 0, 9051866Sbostic /* lfs_cleansz */ 0, 9151149Sbostic /* lfs_segtabsz */ 0, 9251149Sbostic /* lfs_segmask */ DFL_LFSSEG_MASK, 9351149Sbostic /* lfs_segshift */ DFL_LFSSEG_SHIFT, 9451149Sbostic /* lfs_bmask */ DFL_LFSBLOCK_MASK, 9551149Sbostic /* lfs_bshift */ DFL_LFSBLOCK_SHIFT, 9651149Sbostic /* lfs_ffmask */ 0, 9751149Sbostic /* lfs_ffshift */ 0, 9851149Sbostic /* lfs_fbmask */ 0, 9951149Sbostic /* lfs_fbshift */ 0, 10051149Sbostic /* lfs_fsbtodb */ 0, 10155596Sbostic /* lfs_sushift */ 0, 10251149Sbostic /* lfs_sboffs */ { 0 }, 10351864Sbostic /* lfs_ivnode */ NULL, 10454710Sbostic /* lfs_seglock */ 0, 10551864Sbostic /* lfs_iocount */ 0, 10654263Sbostic /* lfs_writer */ 0, 10754263Sbostic /* lfs_dirops */ 0, 10854263Sbostic /* lfs_doifile */ 0, 10951149Sbostic /* lfs_fmod */ 0, 11051149Sbostic /* lfs_clean */ 0, 11151149Sbostic /* lfs_ronly */ 0, 11251149Sbostic /* lfs_flags */ 0, 11351149Sbostic /* lfs_fsmnt */ { 0 }, 11451864Sbostic /* lfs_pad */ { 0 }, 11551149Sbostic /* lfs_cksum */ 0 11651149Sbostic }; 11751149Sbostic 11851864Sbostic 11951864Sbostic struct direct lfs_root_dir[] = { 12054715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 1, "."}, 12154715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."}, 12254715Sbostic { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"}, 12354715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"}, 12451864Sbostic }; 12551864Sbostic 12651864Sbostic struct direct lfs_lf_dir[] = { 12754715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 12854715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 12951864Sbostic }; 13051864Sbostic 13151864Sbostic static daddr_t make_dinode 13251866Sbostic __P((ino_t, struct dinode *, int, daddr_t, struct lfs *)); 13351864Sbostic static void make_dir __P(( void *, struct direct *, int)); 13451149Sbostic static void put __P((int, off_t, void *, size_t)); 13551149Sbostic 13651149Sbostic int 13751149Sbostic make_lfs(fd, lp, partp, minfree, block_size, seg_size) 13851149Sbostic int fd; 13951149Sbostic struct disklabel *lp; 14051149Sbostic struct partition *partp; 14151149Sbostic int minfree; 14251149Sbostic int block_size; 14351149Sbostic int seg_size; 14451149Sbostic { 14551149Sbostic struct dinode *dip; /* Pointer to a disk inode */ 14651864Sbostic struct dinode *dpagep; /* Pointer to page of disk inodes */ 14751866Sbostic CLEANERINFO *cleaninfo; /* Segment cleaner information table */ 14851149Sbostic FINFO file_info; /* File info structure in summary blocks */ 14951149Sbostic IFILE *ifile; /* Pointer to array of ifile structures */ 15051149Sbostic IFILE *ip; /* Pointer to array of ifile structures */ 15151866Sbostic struct lfs *lfsp; /* Superblock */ 15251864Sbostic SEGUSE *segp; /* Segment usage table */ 15351864Sbostic SEGUSE *segtable; /* Segment usage table */ 15451149Sbostic SEGSUM summary; /* Segment summary structure */ 15551866Sbostic SEGSUM *sp; /* Segment summary pointer */ 15651149Sbostic daddr_t last_sb_addr; /* Address of superblocks */ 15751864Sbostic daddr_t last_addr; /* Previous segment address */ 15851149Sbostic daddr_t sb_addr; /* Address of superblocks */ 15951864Sbostic daddr_t seg_addr; /* Address of current segment */ 16051866Sbostic void *ipagep; /* Pointer to the page we use to write stuff */ 16151149Sbostic void *sump; /* Used to copy stuff into segment buffer */ 16251149Sbostic u_long *block_array; /* Array of logical block nos to put in sum */ 16351866Sbostic u_long blocks_used; /* Number of blocks in first segment */ 16451866Sbostic u_long *dp; /* Used to computed checksum on data */ 16551866Sbostic u_long *datasump; /* Used to computed checksum on data */ 16651149Sbostic int block_array_size; /* How many entries in block array */ 16751149Sbostic int bsize; /* Block size */ 16851149Sbostic int db_per_fb; /* Disk blocks per file block */ 16951864Sbostic int i, j; 17051866Sbostic int off; /* Offset at which to write */ 17151149Sbostic int sb_interval; /* number of segs between super blocks */ 17251149Sbostic int seg_seek; /* Seek offset for a segment */ 17351149Sbostic int ssize; /* Segment size */ 17451149Sbostic int sum_size; /* Size of the summary block */ 17551149Sbostic 17651149Sbostic lfsp = &lfs_default; 17751149Sbostic 17851149Sbostic if (!(bsize = block_size)) 17951149Sbostic bsize = DFL_LFSBLOCK; 18051149Sbostic if (!(ssize = seg_size)) 18151149Sbostic ssize = DFL_LFSSEG; 18251149Sbostic 18351149Sbostic /* Modify parts of superblock overridden by command line arguments */ 18451149Sbostic if (bsize != DFL_LFSBLOCK) { 18551149Sbostic lfsp->lfs_bshift = log2(bsize); 18651149Sbostic if (1 << lfsp->lfs_bshift != bsize) 18751149Sbostic fatal("%d: block size not a power of 2", bsize); 18851149Sbostic lfsp->lfs_bsize = bsize; 18951864Sbostic lfsp->lfs_fsize = bsize; 19051149Sbostic lfsp->lfs_bmask = bsize - 1; 19151149Sbostic lfsp->lfs_inopb = bsize / sizeof(struct dinode); 19251149Sbostic /* MIS -- should I round to power of 2 */ 19351149Sbostic lfsp->lfs_ifpb = bsize / sizeof(IFILE); 19451866Sbostic lfsp->lfs_sepb = bsize / sizeof(SEGUSE); 19551149Sbostic lfsp->lfs_nindir = bsize / sizeof(daddr_t); 19651149Sbostic } 19751149Sbostic 19851149Sbostic if (ssize != DFL_LFSSEG) { 19951149Sbostic lfsp->lfs_segshift = log2(ssize); 20051149Sbostic if (1 << lfsp->lfs_segshift != ssize) 20151149Sbostic fatal("%d: segment size not power of 2", ssize); 20251149Sbostic lfsp->lfs_ssize = ssize; 20351149Sbostic lfsp->lfs_segmask = ssize - 1; 20451149Sbostic } 20551149Sbostic lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift; 20651149Sbostic 20751149Sbostic if (minfree) 20851149Sbostic lfsp->lfs_minfree = minfree; 20951149Sbostic 21051149Sbostic /* 21151149Sbostic * Fill in parts of superblock that can be computed from file system 21251149Sbostic * size, disk geometry and current time. 21351149Sbostic */ 21451149Sbostic db_per_fb = bsize/lp->d_secsize; 21551149Sbostic lfsp->lfs_fsbtodb = log2(db_per_fb); 21655596Sbostic lfsp->lfs_sushift = log2(lfsp->lfs_sepb); 21751149Sbostic lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb; 21851149Sbostic lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift); 21951149Sbostic lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize; 22055389Sbostic lfsp->lfs_maxfilesize = maxtable[lfsp->lfs_bshift] << lfsp->lfs_bshift; 22151864Sbostic 22251864Sbostic /* 22351864Sbostic * The number of free blocks is set from the total data size (lfs_dsize) 22455596Sbostic * minus one sector for each segment (for the segment summary). Then 22551866Sbostic * we'll subtract off the room for the superblocks, ifile entries and 22651866Sbostic * segment usage table. 22751864Sbostic */ 22855596Sbostic lfsp->lfs_bfree = lfsp->lfs_dsize * db_per_fb - lfsp->lfs_nseg; 22951149Sbostic lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp); 23051866Sbostic lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp); 23151149Sbostic if ((lfsp->lfs_tstamp = time(NULL)) == -1) 23251149Sbostic fatal("time: %s", strerror(errno)); 23351149Sbostic if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 23451149Sbostic sb_interval = LFS_MIN_SBINTERVAL; 23551149Sbostic 23651149Sbostic /* 23751149Sbostic * Now, lay out the file system. We need to figure out where 23851149Sbostic * the superblocks go, initialize the checkpoint information 23951149Sbostic * for the first two superblocks, initialize the segment usage 24051149Sbostic * information, put the segusage information in the ifile, create 24151149Sbostic * the first block of IFILE structures, and link all the IFILE 24251149Sbostic * structures into a free list. 24351149Sbostic */ 24451149Sbostic 24551149Sbostic /* Figure out where the superblocks are going to live */ 24651149Sbostic lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize; 24751149Sbostic for (i = 1; i < LFS_MAXNUMSB; i++) { 24851149Sbostic sb_addr = ((i * sb_interval) << 24951149Sbostic (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb)) 25051149Sbostic + lfsp->lfs_sboffs[0]; 25151149Sbostic if (sb_addr > partp->p_size) 25251149Sbostic break; 25351149Sbostic lfsp->lfs_sboffs[i] = sb_addr; 25451149Sbostic } 25551149Sbostic last_sb_addr = lfsp->lfs_sboffs[i - 1]; 25651149Sbostic lfsp->lfs_lastseg = lfsp->lfs_sboffs[0]; 25751864Sbostic lfsp->lfs_nextseg = 25851864Sbostic lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0]; 25951866Sbostic lfsp->lfs_curseg = lfsp->lfs_lastseg; 26051149Sbostic 26151149Sbostic /* 26251149Sbostic * Initialize the segment usage table. The first segment will 26351866Sbostic * contain the superblock, the cleanerinfo (cleansz), the segusage 26451866Sbostic * table * (segtabsz), 1 block's worth of IFILE entries, the root 26551866Sbostic * directory, the lost+found directory and one block's worth of 26651866Sbostic * inodes (containing the ifile, root, and l+f inodes). 26751149Sbostic */ 26851866Sbostic if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift))) 26951866Sbostic fatal("%s", strerror(errno)); 27051930Sbostic cleaninfo->clean = lfsp->lfs_nseg - 1; 27151930Sbostic cleaninfo->dirty = 1; 27251930Sbostic 27351149Sbostic if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift))) 27451149Sbostic fatal("%s", strerror(errno)); 27551149Sbostic segp = segtable; 27651866Sbostic blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4; 27752088Sbostic segp->su_nbytes = blocks_used << lfsp->lfs_bshift; 27851149Sbostic segp->su_lastmod = lfsp->lfs_tstamp; 27955596Sbostic segp->su_nsums = 1; /* 1 summary blocks */ 28055596Sbostic segp->su_ninos = 1; /* 1 inode block */ 28151930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY; 28255596Sbostic lfsp->lfs_bfree -= db_per_fb * 28355596Sbostic (lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4); 28451149Sbostic 28551866Sbostic /* 28651866Sbostic * Now figure out the address of the ifile inode. The inode block 28751866Sbostic * appears immediately after the segment summary. 28851866Sbostic */ 28951866Sbostic lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) / 29051866Sbostic lp->d_secsize; 29151149Sbostic 29251149Sbostic for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) { 29351864Sbostic if ((i % sb_interval) == 0) { 29451930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK; 29555596Sbostic lfsp->lfs_bfree -= (LFS_SBPAD / lp->d_secsize); 29652088Sbostic } else 29751930Sbostic segp->su_flags = 0; 29851149Sbostic segp->su_lastmod = 0; 29952088Sbostic segp->su_nbytes = 0; 30055596Sbostic segp->su_ninos = 0; 30155596Sbostic segp->su_nsums = 1; 30251149Sbostic } 30351149Sbostic 30451149Sbostic /* 30551149Sbostic * Ready to start writing segments. The first segment is different 30651149Sbostic * because it contains the segment usage table and the ifile inode 30751864Sbostic * as well as a superblock. For the rest of the segments, set the 30851864Sbostic * time stamp to be 0 so that the first segment is the most recent. 30951864Sbostic * For each segment that is supposed to contain a copy of the super 31051864Sbostic * block, initialize its first few blocks and its segment summary 31151864Sbostic * to indicate this. 31251149Sbostic */ 31351864Sbostic lfsp->lfs_nfiles = LFS_FIRST_INUM - 1; 31451149Sbostic lfsp->lfs_cksum = 31551866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 31651149Sbostic 31751864Sbostic /* Now create a block of disk inodes */ 31851864Sbostic if (!(dpagep = malloc(lfsp->lfs_bsize))) 31951864Sbostic fatal("%s", strerror(errno)); 32051864Sbostic dip = (struct dinode *)dpagep; 32151864Sbostic bzero(dip, lfsp->lfs_bsize); 32251864Sbostic 32351866Sbostic /* Create a block of IFILE structures. */ 32451866Sbostic if (!(ipagep = malloc(lfsp->lfs_bsize))) 32551149Sbostic fatal("%s", strerror(errno)); 32651866Sbostic ifile = (IFILE *)ipagep; 32751864Sbostic 32851866Sbostic /* 32951866Sbostic * Initialize IFILE. It is the next block following the 33051866Sbostic * block of inodes (whose address has been calculated in 33151866Sbostic * lfsp->lfs_idaddr; 33251866Sbostic */ 33351866Sbostic sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize; 33451866Sbostic sb_addr = make_dinode(LFS_IFILE_INUM, dip, 33551866Sbostic lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp); 33651864Sbostic dip->di_mode = IFREG|IREAD|IWRITE; 33751864Sbostic ip = &ifile[LFS_IFILE_INUM]; 33851864Sbostic ip->if_version = 1; 33951864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 34051864Sbostic 34151864Sbostic /* Initialize the ROOT Directory */ 34251864Sbostic sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp); 34351864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 34451864Sbostic dip->di_size = DIRBLKSIZ; 34551864Sbostic dip->di_nlink = 3; 34651864Sbostic ip = &ifile[ROOTINO]; 34751864Sbostic ip->if_version = 1; 34851864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 34951864Sbostic 35051864Sbostic /* Initialize the lost+found Directory */ 35151864Sbostic sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp); 35251864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 35351864Sbostic dip->di_size = DIRBLKSIZ; 35451864Sbostic dip->di_nlink = 2; 35551864Sbostic ip = &ifile[LOSTFOUNDINO]; 35651864Sbostic ip->if_version = 1; 35751864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 35851864Sbostic 35951864Sbostic /* Make all the other dinodes invalid */ 36051864Sbostic for (i = INOPB(lfsp)-3, dip++; i; i--, dip++) 36151864Sbostic dip->di_inum = LFS_UNUSED_INUM; 36251864Sbostic 36351864Sbostic 36451864Sbostic /* Link remaining IFILE entries in free list */ 36551864Sbostic for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM; 36651864Sbostic i < lfsp->lfs_ifpb; ++ip) { 36751149Sbostic ip->if_version = 1; 36851149Sbostic ip->if_daddr = LFS_UNUSED_DADDR; 36951149Sbostic ip->if_nextfree = ++i; 37051149Sbostic } 37151149Sbostic ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM; 37251149Sbostic 37351866Sbostic /* Now, write the segment */ 37451149Sbostic 37551866Sbostic /* Compute a checksum across all the data you're writing */ 37651866Sbostic dp = datasump = malloc (blocks_used * sizeof(u_long)); 377*55654Sbostic *dp++ = ((u_long *)dpagep)[0]; /* inode block */ 37851866Sbostic for (i = 0; i < lfsp->lfs_cleansz; i++) 37951866Sbostic *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) / 38051866Sbostic sizeof(u_long)]; /* Cleaner info */ 38151866Sbostic for (i = 0; i < lfsp->lfs_segtabsz; i++) 38251866Sbostic *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) / 38351866Sbostic sizeof(u_long)]; /* Segusage table */ 38451866Sbostic *dp++ = ((u_long *)ifile)[0]; /* Ifile */ 38551866Sbostic 38651866Sbostic /* Still need the root and l+f bytes; get them later */ 38751866Sbostic 38851866Sbostic /* Write out the inode block */ 38951866Sbostic off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE; 39051866Sbostic put(fd, off, dpagep, lfsp->lfs_bsize); 39151866Sbostic free(dpagep); 39251866Sbostic off += lfsp->lfs_bsize; 39351866Sbostic 39451866Sbostic /* Write out the ifile */ 39551866Sbostic 39651866Sbostic put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift); 39751866Sbostic off += (lfsp->lfs_cleansz << lfsp->lfs_bshift); 39851866Sbostic (void)free(cleaninfo); 39951866Sbostic 40051866Sbostic put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift); 40151866Sbostic off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift); 40251866Sbostic (void)free(segtable); 40351866Sbostic 40451866Sbostic put(fd, off, ifile, lfsp->lfs_bsize); 40551866Sbostic off += lfsp->lfs_bsize; 40651866Sbostic 40751866Sbostic /* 40851866Sbostic * use ipagep for space for writing out other stuff. It used to 40951866Sbostic * contain the ifile, but we're done with it. 41051866Sbostic */ 41151866Sbostic 41251864Sbostic /* Write out the root and lost and found directories */ 41351866Sbostic bzero(ipagep, lfsp->lfs_bsize); 41451866Sbostic make_dir(ipagep, lfs_root_dir, 41551864Sbostic sizeof(lfs_root_dir) / sizeof(struct direct)); 41651866Sbostic *dp++ = ((u_long *)ipagep)[0]; 41751866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 41851866Sbostic off += lfsp->lfs_bsize; 41951149Sbostic 42051866Sbostic bzero(ipagep, lfsp->lfs_bsize); 42151866Sbostic make_dir(ipagep, lfs_lf_dir, 42251864Sbostic sizeof(lfs_lf_dir) / sizeof(struct direct)); 42351866Sbostic *dp++ = ((u_long *)ipagep)[0]; 42451866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 42551149Sbostic 42651866Sbostic /* Write Supberblock */ 42751866Sbostic lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize; 42851866Sbostic put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs)); 42951149Sbostic 43051866Sbostic /* 43151866Sbostic * Finally, calculate all the fields for the summary structure 43251866Sbostic * and write it. 43351866Sbostic */ 43451866Sbostic 43551864Sbostic summary.ss_next = lfsp->lfs_nextseg; 43651149Sbostic summary.ss_create = lfsp->lfs_tstamp; 43751866Sbostic summary.ss_nfinfo = 3; 43851866Sbostic summary.ss_ninos = 3; 43951866Sbostic summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used); 44051149Sbostic 44151864Sbostic /* 44251864Sbostic * Make sure that we don't overflow a summary block. We have to 44351866Sbostic * record: FINFO structures for ifile, root, and l+f. The number 44451866Sbostic * of blocks recorded for the ifile is determined by the size of 44551866Sbostic * the cleaner info and the segments usage table. There is room 44651866Sbostic * for one block included in sizeof(FINFO) so we don't need to add 44751866Sbostic * any extra space for the ROOT and L+F, and one block of the ifile 44851866Sbostic * is already counted. Finally, we leave room for 1 inode block 44951866Sbostic * address. 45051864Sbostic */ 45153248Sbostic sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) + 45251866Sbostic (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long); 45351149Sbostic #define SUMERR \ 45451149Sbostic "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes." 45551149Sbostic if (sum_size > LFS_SUMMARY_SIZE) 45651149Sbostic fatal(SUMERR, sum_size); 45751149Sbostic 45851866Sbostic block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1; 45951149Sbostic 46051149Sbostic if (!(block_array = malloc(block_array_size *sizeof(int)))) 46151149Sbostic fatal("%s: %s", special, strerror(errno)); 46251149Sbostic 46351149Sbostic /* fill in the array */ 46451866Sbostic for (i = 0; i < block_array_size; i++) 46551149Sbostic block_array[i] = i; 46651149Sbostic 46751149Sbostic /* copy into segment */ 46851866Sbostic sump = ipagep; 46951149Sbostic bcopy(&summary, sump, sizeof(SEGSUM)); 47051149Sbostic sump += sizeof(SEGSUM); 47151149Sbostic 47251149Sbostic /* Now, add the ifile */ 47351866Sbostic file_info.fi_nblocks = block_array_size; 47451149Sbostic file_info.fi_version = 1; 47551149Sbostic file_info.fi_ino = LFS_IFILE_INUM; 47651149Sbostic 47751149Sbostic bcopy(&file_info, sump, sizeof(FINFO) - sizeof(u_long)); 47851149Sbostic sump += sizeof(FINFO) - sizeof(u_long); 47951149Sbostic bcopy(block_array, sump, sizeof(u_long) * file_info.fi_nblocks); 48051864Sbostic sump += sizeof(u_long) * file_info.fi_nblocks; 48151149Sbostic 48251864Sbostic /* Now, add the root directory */ 48351864Sbostic file_info.fi_nblocks = 1; 48451864Sbostic file_info.fi_version = 1; 48551864Sbostic file_info.fi_ino = ROOTINO; 48651864Sbostic file_info.fi_blocks[0] = 0; 48751864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 48851864Sbostic sump += sizeof(FINFO); 48951864Sbostic 49051864Sbostic /* Now, add the lost and found */ 49151864Sbostic file_info.fi_ino = LOSTFOUNDINO; 49251864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 49351864Sbostic 49451866Sbostic ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] = 49551866Sbostic lfsp->lfs_idaddr; 49651866Sbostic ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum), 49751866Sbostic LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum)); 49851866Sbostic put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE); 49951864Sbostic 50051866Sbostic sp = (SEGSUM *)ipagep; 50151866Sbostic sp->ss_create = 0; 50251866Sbostic sp->ss_nfinfo = 0; 50351866Sbostic sp->ss_ninos = 0; 50451866Sbostic sp->ss_datasum = 0; 50551149Sbostic 50655480Sbostic /* Now write the summary block for the next partial so it's invalid */ 50755480Sbostic lfsp->lfs_tstamp = 0; 50855480Sbostic off += lfsp->lfs_bsize; 50955480Sbostic sp->ss_sumsum = 51055480Sbostic cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 51155480Sbostic put(fd, off, sp, LFS_SUMMARY_SIZE); 51255480Sbostic 51351149Sbostic /* Now, write rest of segments containing superblocks */ 51451864Sbostic lfsp->lfs_cksum = 51551866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 51651864Sbostic for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1; 51751864Sbostic i < lfsp->lfs_nseg; i++) { 51851149Sbostic 51951864Sbostic seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb; 52051866Sbostic sp->ss_next = last_addr; 52151866Sbostic last_addr = seg_addr; 52251866Sbostic seg_seek = seg_addr * lp->d_secsize; 52351866Sbostic 52451864Sbostic if (seg_addr == lfsp->lfs_sboffs[j]) { 52551864Sbostic if (j < (LFS_MAXNUMSB - 2)) 52651864Sbostic j++; 52751866Sbostic put(fd, seg_seek, lfsp, sizeof(struct lfs)); 52851866Sbostic seg_seek += LFS_SBPAD; 52951866Sbostic } 53051149Sbostic 53151149Sbostic /* Summary */ 53251866Sbostic sp->ss_sumsum = cksum(&sp->ss_datasum, 53351866Sbostic LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 53451866Sbostic put(fd, seg_seek, sp, LFS_SUMMARY_SIZE); 53551149Sbostic } 53651866Sbostic free(ipagep); 53751864Sbostic close(fd); 53851149Sbostic return (0); 53951149Sbostic } 54051149Sbostic 54151149Sbostic static void 54251149Sbostic put(fd, off, p, len) 54351149Sbostic int fd; 54451149Sbostic off_t off; 54551149Sbostic void *p; 54651149Sbostic size_t len; 54751149Sbostic { 54851149Sbostic int wbytes; 54951149Sbostic 55051149Sbostic if (lseek(fd, off, SEEK_SET) < 0) 55151149Sbostic fatal("%s: %s", special, strerror(errno)); 55251149Sbostic if ((wbytes = write(fd, p, len)) < 0) 55351149Sbostic fatal("%s: %s", special, strerror(errno)); 55451149Sbostic if (wbytes != len) 55551149Sbostic fatal("%s: short write (%d, not %d)", special, wbytes, len); 55651149Sbostic } 55751864Sbostic 55851864Sbostic /* 55951864Sbostic * Create the root directory for this file system and the lost+found 56051864Sbostic * directory. 56151864Sbostic */ 56251864Sbostic 56351864Sbostic u_long d_ino; /* inode number of entry */ 56451864Sbostic u_short d_reclen; /* length of this record */ 56551864Sbostic u_short d_namlen; /* length of string in d_name */ 56651864Sbostic char d_name[MAXNAMLEN + 1]; /* name with length <= MAXNAMLEN */ 56755389Sbostic void 56851864Sbostic lfsinit() 56955389Sbostic {} 57051864Sbostic 57151864Sbostic static daddr_t 57251864Sbostic make_dinode(ino, dip, nblocks, saddr, lfsp) 57351864Sbostic ino_t ino; /* inode we're creating */ 57451864Sbostic struct dinode *dip; /* disk inode */ 57551864Sbostic int nblocks; /* number of blocks in file */ 57651864Sbostic daddr_t saddr; /* starting block address */ 57751866Sbostic struct lfs *lfsp; /* superblock */ 57851864Sbostic { 57951864Sbostic int db_per_fb, i; 58051864Sbostic 58151864Sbostic dip->di_nlink = 1; 58255440Sbostic dip->di_blocks = nblocks << lfsp->lfs_fsbtodb; 58351864Sbostic 58455480Sbostic dip->di_size = (nblocks << lfsp->lfs_bshift); 58554715Sbostic dip->di_atime.ts_sec = dip->di_mtime.ts_sec = 58654715Sbostic dip->di_ctime.ts_sec = lfsp->lfs_tstamp; 58754715Sbostic dip->di_atime.ts_nsec = dip->di_mtime.ts_nsec = 58854715Sbostic dip->di_ctime.ts_nsec = 0; 58951864Sbostic dip->di_inum = ino; 59051864Sbostic 59151864Sbostic #define SEGERR \ 59251864Sbostic "File requires more than the number of direct blocks; increase block or segment size." 59351864Sbostic if (NDADDR < nblocks) 59451864Sbostic fatal("%s", SEGERR); 59551864Sbostic 59651864Sbostic /* Assign the block addresses for the ifile */ 59751864Sbostic db_per_fb = 1 << lfsp->lfs_fsbtodb; 59855440Sbostic for (i = 0; i < nblocks; i++, saddr += db_per_fb) 59951864Sbostic dip->di_db[i] = saddr; 60051864Sbostic 60151864Sbostic return (saddr); 60251864Sbostic } 60351864Sbostic 60451864Sbostic 60551864Sbostic /* 60651864Sbostic * Construct a set of directory entries in "bufp". We assume that all the 60751864Sbostic * entries in protodir fir in the first DIRBLKSIZ. 60851864Sbostic */ 60951864Sbostic static void 61051864Sbostic make_dir(bufp, protodir, entries) 61151864Sbostic void *bufp; 61251864Sbostic register struct direct *protodir; 61351864Sbostic int entries; 61451864Sbostic { 61551864Sbostic char *cp; 61651864Sbostic int i, spcleft; 61751864Sbostic 61851864Sbostic spcleft = DIRBLKSIZ; 61951864Sbostic for (cp = bufp, i = 0; i < entries - 1; i++) { 62054715Sbostic protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i]); 62151864Sbostic bcopy(&protodir[i], cp, protodir[i].d_reclen); 62251864Sbostic cp += protodir[i].d_reclen; 62351864Sbostic if ((spcleft -= protodir[i].d_reclen) < 0) 62451864Sbostic fatal("%s: %s", special, "directory too big"); 62551864Sbostic } 62651864Sbostic protodir[i].d_reclen = spcleft; 62754715Sbostic bcopy(&protodir[i], cp, DIRSIZ(NEWDIRFMT, &protodir[i])); 62851864Sbostic } 629