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*53248Sbostic static char sccsid[] = "@(#)lfs.c 5.8 (Berkeley) 04/22/92"; 1051149Sbostic #endif /* not lint */ 1151149Sbostic 1251149Sbostic #include <sys/param.h> 1351864Sbostic #include <sys/uio.h> 1451149Sbostic #include <sys/disklabel.h> 1551864Sbostic #include <sys/time.h> 1651864Sbostic #include <sys/resource.h> 1751864Sbostic #include <sys/proc.h> 1851864Sbostic #include <sys/vnode.h> 1952205Sbostic #include <sys/mount.h> 2051866Sbostic 2151866Sbostic #include <ufs/ufs/dir.h> 2251866Sbostic #include <ufs/ufs/quota.h> 2351866Sbostic #include <ufs/ufs/dinode.h> 2451866Sbostic #include <ufs/ufs/ufsmount.h> 2551866Sbostic #include <ufs/lfs/lfs.h> 2651866Sbostic 2751149Sbostic #include <unistd.h> 2851149Sbostic #include <errno.h> 2951149Sbostic #include <stdlib.h> 3051149Sbostic #include <string.h> 3151149Sbostic #include "config.h" 3251149Sbostic #include "extern.h" 3351149Sbostic 3451866Sbostic static struct lfs lfs_default = { 3551149Sbostic /* lfs_magic */ LFS_MAGIC, 3651149Sbostic /* lfs_version */ LFS_VERSION, 3751149Sbostic /* lfs_size */ 0, 3851866Sbostic /* lfs_ssize */ DFL_LFSSEG/DFL_LFSBLOCK, 3951149Sbostic /* lfs_dsize */ 0, 4051149Sbostic /* lfs_bsize */ DFL_LFSBLOCK, 4151864Sbostic /* lfs_fsize */ DFL_LFSBLOCK, 4251864Sbostic /* lfs_frag */ 1, 4351149Sbostic /* lfs_free */ LFS_FIRST_INUM, 4451864Sbostic /* lfs_bfree */ 0, 4551864Sbostic /* lfs_nfiles */ 0, 4651149Sbostic /* lfs_idaddr */ 0, 4751149Sbostic /* lfs_ifile */ LFS_IFILE_INUM, 4851149Sbostic /* lfs_lastseg */ 0, 4951864Sbostic /* lfs_nextseg */ 0, 5051866Sbostic /* lfs_curseg */ 0, 5151866Sbostic /* lfs_offset */ 0, 5251149Sbostic /* lfs_tstamp */ 0, 5351149Sbostic /* lfs_minfree */ MINFREE, 5451866Sbostic /* lfs_dbpseg */ DFL_LFSSEG/DEV_BSIZE, 5551149Sbostic /* lfs_inopb */ DFL_LFSBLOCK/sizeof(struct dinode), 5651149Sbostic /* lfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 5751866Sbostic /* lfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 5851149Sbostic /* lfs_nindir */ DFL_LFSBLOCK/sizeof(daddr_t), 5951149Sbostic /* lfs_nseg */ 0, 6051149Sbostic /* lfs_nspf */ 0, 6151866Sbostic /* lfs_cleansz */ 0, 6251149Sbostic /* lfs_segtabsz */ 0, 6351149Sbostic /* lfs_segmask */ DFL_LFSSEG_MASK, 6451149Sbostic /* lfs_segshift */ DFL_LFSSEG_SHIFT, 6551149Sbostic /* lfs_bmask */ DFL_LFSBLOCK_MASK, 6651149Sbostic /* lfs_bshift */ DFL_LFSBLOCK_SHIFT, 6751149Sbostic /* lfs_ffmask */ 0, 6851149Sbostic /* lfs_ffshift */ 0, 6951149Sbostic /* lfs_fbmask */ 0, 7051149Sbostic /* lfs_fbshift */ 0, 7151149Sbostic /* lfs_fsbtodb */ 0, 7251149Sbostic /* lfs_sboffs */ { 0 }, 7351864Sbostic /* lfs_ivnode */ NULL, 7451864Sbostic /* lfs_segtab */ NULL, 7551864Sbostic /* lfs_seglist */ NULL, 7651864Sbostic /* lfs_iocount */ 0, 7751149Sbostic /* lfs_fmod */ 0, 7851149Sbostic /* lfs_clean */ 0, 7951149Sbostic /* lfs_ronly */ 0, 8051149Sbostic /* lfs_flags */ 0, 8151149Sbostic /* lfs_fsmnt */ { 0 }, 8251864Sbostic /* lfs_pad */ { 0 }, 8351149Sbostic /* lfs_cksum */ 0 8451149Sbostic }; 8551149Sbostic 8651864Sbostic 8751864Sbostic struct direct lfs_root_dir[] = { 8851864Sbostic { ROOTINO, sizeof(struct direct), 1, "."}, 8951864Sbostic { ROOTINO, sizeof(struct direct), 2, ".."}, 9051866Sbostic { LFS_IFILE_INUM, sizeof(struct direct), 5, "ifile"}, 9151864Sbostic { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found"}, 9251864Sbostic }; 9351864Sbostic 9451864Sbostic struct direct lfs_lf_dir[] = { 9551864Sbostic { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, 9651864Sbostic { ROOTINO, sizeof(struct direct), 2, ".." }, 9751864Sbostic }; 9851864Sbostic 9951864Sbostic static daddr_t make_dinode 10051866Sbostic __P((ino_t, struct dinode *, int, daddr_t, struct lfs *)); 10151864Sbostic static void make_dir __P(( void *, struct direct *, int)); 10251149Sbostic static void put __P((int, off_t, void *, size_t)); 10351149Sbostic 10451149Sbostic int 10551149Sbostic make_lfs(fd, lp, partp, minfree, block_size, seg_size) 10651149Sbostic int fd; 10751149Sbostic struct disklabel *lp; 10851149Sbostic struct partition *partp; 10951149Sbostic int minfree; 11051149Sbostic int block_size; 11151149Sbostic int seg_size; 11251149Sbostic { 11351149Sbostic struct dinode *dip; /* Pointer to a disk inode */ 11451864Sbostic struct dinode *dpagep; /* Pointer to page of disk inodes */ 11551866Sbostic CLEANERINFO *cleaninfo; /* Segment cleaner information table */ 11651149Sbostic FINFO file_info; /* File info structure in summary blocks */ 11751149Sbostic IFILE *ifile; /* Pointer to array of ifile structures */ 11851149Sbostic IFILE *ip; /* Pointer to array of ifile structures */ 11951866Sbostic struct lfs *lfsp; /* Superblock */ 12051864Sbostic SEGUSE *segp; /* Segment usage table */ 12151864Sbostic SEGUSE *segtable; /* Segment usage table */ 12251149Sbostic SEGSUM summary; /* Segment summary structure */ 12351866Sbostic SEGSUM *sp; /* Segment summary pointer */ 12451149Sbostic daddr_t last_sb_addr; /* Address of superblocks */ 12551864Sbostic daddr_t last_addr; /* Previous segment address */ 12651149Sbostic daddr_t sb_addr; /* Address of superblocks */ 12751864Sbostic daddr_t seg_addr; /* Address of current segment */ 12851866Sbostic void *ipagep; /* Pointer to the page we use to write stuff */ 12951149Sbostic void *sump; /* Used to copy stuff into segment buffer */ 13051149Sbostic u_long *block_array; /* Array of logical block nos to put in sum */ 13151866Sbostic u_long blocks_used; /* Number of blocks in first segment */ 13251866Sbostic u_long *dp; /* Used to computed checksum on data */ 13351866Sbostic u_long *datasump; /* Used to computed checksum on data */ 13451149Sbostic int block_array_size; /* How many entries in block array */ 13551149Sbostic int bsize; /* Block size */ 13651149Sbostic int db_per_fb; /* Disk blocks per file block */ 13751864Sbostic int i, j; 13851866Sbostic int off; /* Offset at which to write */ 13951149Sbostic int sb_to_sum; /* offset between superblock and summary */ 14051149Sbostic int sb_interval; /* number of segs between super blocks */ 14151149Sbostic int seg_seek; /* Seek offset for a segment */ 14251149Sbostic int ssize; /* Segment size */ 14351149Sbostic int sum_size; /* Size of the summary block */ 14451149Sbostic int wbytes; /* Number of bytes returned by write */ 14551149Sbostic 14651149Sbostic lfsp = &lfs_default; 14751149Sbostic 14851149Sbostic if (!(bsize = block_size)) 14951149Sbostic bsize = DFL_LFSBLOCK; 15051149Sbostic if (!(ssize = seg_size)) 15151149Sbostic ssize = DFL_LFSSEG; 15251149Sbostic 15351149Sbostic /* Modify parts of superblock overridden by command line arguments */ 15451149Sbostic if (bsize != DFL_LFSBLOCK) { 15551149Sbostic lfsp->lfs_bshift = log2(bsize); 15651149Sbostic if (1 << lfsp->lfs_bshift != bsize) 15751149Sbostic fatal("%d: block size not a power of 2", bsize); 15851149Sbostic lfsp->lfs_bsize = bsize; 15951864Sbostic lfsp->lfs_fsize = bsize; 16051149Sbostic lfsp->lfs_bmask = bsize - 1; 16151149Sbostic lfsp->lfs_inopb = bsize / sizeof(struct dinode); 16251149Sbostic /* MIS -- should I round to power of 2 */ 16351149Sbostic lfsp->lfs_ifpb = bsize / sizeof(IFILE); 16451866Sbostic lfsp->lfs_sepb = bsize / sizeof(SEGUSE); 16551149Sbostic lfsp->lfs_nindir = bsize / sizeof(daddr_t); 16651149Sbostic } 16751149Sbostic 16851149Sbostic if (ssize != DFL_LFSSEG) { 16951149Sbostic lfsp->lfs_segshift = log2(ssize); 17051149Sbostic if (1 << lfsp->lfs_segshift != ssize) 17151149Sbostic fatal("%d: segment size not power of 2", ssize); 17251149Sbostic lfsp->lfs_ssize = ssize; 17351149Sbostic lfsp->lfs_segmask = ssize - 1; 17451149Sbostic } 17551149Sbostic lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift; 17651149Sbostic 17751149Sbostic if (minfree) 17851149Sbostic lfsp->lfs_minfree = minfree; 17951149Sbostic 18051149Sbostic /* 18151149Sbostic * Fill in parts of superblock that can be computed from file system 18251149Sbostic * size, disk geometry and current time. 18351149Sbostic */ 18451149Sbostic db_per_fb = bsize/lp->d_secsize; 18551149Sbostic lfsp->lfs_fsbtodb = log2(db_per_fb); 18651149Sbostic lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb; 18751149Sbostic lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift); 18851149Sbostic lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize; 18951864Sbostic 19051864Sbostic /* 19151864Sbostic * The number of free blocks is set from the total data size (lfs_dsize) 19251864Sbostic * minus one block for each segment (for the segment summary). Then 19351866Sbostic * we'll subtract off the room for the superblocks, ifile entries and 19451866Sbostic * segment usage table. 19551864Sbostic */ 19651864Sbostic lfsp->lfs_bfree = lfsp->lfs_dsize - lfsp->lfs_nseg; 19751149Sbostic lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp); 19851866Sbostic lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp); 19951149Sbostic if ((lfsp->lfs_tstamp = time(NULL)) == -1) 20051149Sbostic fatal("time: %s", strerror(errno)); 20151149Sbostic if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 20251149Sbostic sb_interval = LFS_MIN_SBINTERVAL; 20351149Sbostic 20451149Sbostic /* 20551149Sbostic * Now, lay out the file system. We need to figure out where 20651149Sbostic * the superblocks go, initialize the checkpoint information 20751149Sbostic * for the first two superblocks, initialize the segment usage 20851149Sbostic * information, put the segusage information in the ifile, create 20951149Sbostic * the first block of IFILE structures, and link all the IFILE 21051149Sbostic * structures into a free list. 21151149Sbostic */ 21251149Sbostic 21351149Sbostic /* Figure out where the superblocks are going to live */ 21451149Sbostic lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize; 21551149Sbostic for (i = 1; i < LFS_MAXNUMSB; i++) { 21651149Sbostic sb_addr = ((i * sb_interval) << 21751149Sbostic (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb)) 21851149Sbostic + lfsp->lfs_sboffs[0]; 21951149Sbostic if (sb_addr > partp->p_size) 22051149Sbostic break; 22151149Sbostic lfsp->lfs_sboffs[i] = sb_addr; 22251149Sbostic } 22351149Sbostic last_sb_addr = lfsp->lfs_sboffs[i - 1]; 22451149Sbostic lfsp->lfs_lastseg = lfsp->lfs_sboffs[0]; 22551864Sbostic lfsp->lfs_nextseg = 22651864Sbostic lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0]; 22751866Sbostic lfsp->lfs_curseg = lfsp->lfs_lastseg; 22851149Sbostic 22951149Sbostic /* 23051149Sbostic * Initialize the segment usage table. The first segment will 23151866Sbostic * contain the superblock, the cleanerinfo (cleansz), the segusage 23251866Sbostic * table * (segtabsz), 1 block's worth of IFILE entries, the root 23351866Sbostic * directory, the lost+found directory and one block's worth of 23451866Sbostic * inodes (containing the ifile, root, and l+f inodes). 23551149Sbostic */ 23651866Sbostic if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift))) 23751866Sbostic fatal("%s", strerror(errno)); 23851930Sbostic cleaninfo->clean = lfsp->lfs_nseg - 1; 23951930Sbostic cleaninfo->dirty = 1; 24051930Sbostic 24151149Sbostic if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift))) 24251149Sbostic fatal("%s", strerror(errno)); 24351149Sbostic segp = segtable; 24451866Sbostic blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4; 24552088Sbostic segp->su_nbytes = blocks_used << lfsp->lfs_bshift; 24651149Sbostic segp->su_lastmod = lfsp->lfs_tstamp; 24751930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY; 24851930Sbostic lfsp->lfs_bfree -= lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4; 24951149Sbostic 25051866Sbostic /* 25151866Sbostic * Now figure out the address of the ifile inode. The inode block 25251866Sbostic * appears immediately after the segment summary. 25351866Sbostic */ 25451866Sbostic lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) / 25551866Sbostic lp->d_secsize; 25651149Sbostic 25751149Sbostic for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) { 25851864Sbostic if ((i % sb_interval) == 0) { 25951930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK; 26051864Sbostic lfsp->lfs_bfree -= (LFS_SBPAD >> lfsp->lfs_bshift); 26152088Sbostic } else 26251930Sbostic segp->su_flags = 0; 26351149Sbostic segp->su_lastmod = 0; 26452088Sbostic segp->su_nbytes = 0; 26551149Sbostic } 26651149Sbostic 26751149Sbostic /* 26851149Sbostic * Ready to start writing segments. The first segment is different 26951149Sbostic * because it contains the segment usage table and the ifile inode 27051864Sbostic * as well as a superblock. For the rest of the segments, set the 27151864Sbostic * time stamp to be 0 so that the first segment is the most recent. 27251864Sbostic * For each segment that is supposed to contain a copy of the super 27351864Sbostic * block, initialize its first few blocks and its segment summary 27451864Sbostic * to indicate this. 27551149Sbostic */ 27651864Sbostic lfsp->lfs_nfiles = LFS_FIRST_INUM - 1; 27751149Sbostic lfsp->lfs_cksum = 27851866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 27951149Sbostic 28051864Sbostic /* Now create a block of disk inodes */ 28151864Sbostic if (!(dpagep = malloc(lfsp->lfs_bsize))) 28251864Sbostic fatal("%s", strerror(errno)); 28351864Sbostic dip = (struct dinode *)dpagep; 28451864Sbostic bzero(dip, lfsp->lfs_bsize); 28551864Sbostic 28651866Sbostic /* Create a block of IFILE structures. */ 28751866Sbostic if (!(ipagep = malloc(lfsp->lfs_bsize))) 28851149Sbostic fatal("%s", strerror(errno)); 28951866Sbostic ifile = (IFILE *)ipagep; 29051864Sbostic 29151866Sbostic /* 29251866Sbostic * Initialize IFILE. It is the next block following the 29351866Sbostic * block of inodes (whose address has been calculated in 29451866Sbostic * lfsp->lfs_idaddr; 29551866Sbostic */ 29651866Sbostic sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize; 29751866Sbostic sb_addr = make_dinode(LFS_IFILE_INUM, dip, 29851866Sbostic lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp); 29951864Sbostic dip->di_mode = IFREG|IREAD|IWRITE; 30051864Sbostic ip = &ifile[LFS_IFILE_INUM]; 30151864Sbostic ip->if_version = 1; 30251864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 30351864Sbostic 30451864Sbostic /* Initialize the ROOT Directory */ 30551864Sbostic sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp); 30651864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 30751864Sbostic dip->di_size = DIRBLKSIZ; 30851864Sbostic dip->di_nlink = 3; 30951864Sbostic ip = &ifile[ROOTINO]; 31051864Sbostic ip->if_version = 1; 31151864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 31251864Sbostic 31351864Sbostic /* Initialize the lost+found Directory */ 31451864Sbostic sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp); 31551864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 31651864Sbostic dip->di_size = DIRBLKSIZ; 31751864Sbostic dip->di_nlink = 2; 31851864Sbostic ip = &ifile[LOSTFOUNDINO]; 31951864Sbostic ip->if_version = 1; 32051864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 32151864Sbostic 32251864Sbostic /* Make all the other dinodes invalid */ 32351864Sbostic for (i = INOPB(lfsp)-3, dip++; i; i--, dip++) 32451864Sbostic dip->di_inum = LFS_UNUSED_INUM; 32551864Sbostic 32651864Sbostic 32751864Sbostic /* Link remaining IFILE entries in free list */ 32851864Sbostic for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM; 32951864Sbostic i < lfsp->lfs_ifpb; ++ip) { 33051149Sbostic ip->if_version = 1; 33151149Sbostic ip->if_daddr = LFS_UNUSED_DADDR; 33251149Sbostic ip->if_nextfree = ++i; 33351149Sbostic } 33451149Sbostic ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM; 33551149Sbostic 33651866Sbostic /* Now, write the segment */ 33751149Sbostic 33851866Sbostic /* Compute a checksum across all the data you're writing */ 33951866Sbostic dp = datasump = malloc (blocks_used * sizeof(u_long)); 34051866Sbostic *dp++ = ((u_long *)dip)[0]; /* inode block */ 34151866Sbostic for (i = 0; i < lfsp->lfs_cleansz; i++) 34251866Sbostic *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) / 34351866Sbostic sizeof(u_long)]; /* Cleaner info */ 34451866Sbostic for (i = 0; i < lfsp->lfs_segtabsz; i++) 34551866Sbostic *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) / 34651866Sbostic sizeof(u_long)]; /* Segusage table */ 34751866Sbostic *dp++ = ((u_long *)ifile)[0]; /* Ifile */ 34851866Sbostic 34951866Sbostic /* Still need the root and l+f bytes; get them later */ 35051866Sbostic 35151866Sbostic /* Write out the inode block */ 35251866Sbostic off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE; 35351866Sbostic put(fd, off, dpagep, lfsp->lfs_bsize); 35451866Sbostic free(dpagep); 35551866Sbostic off += lfsp->lfs_bsize; 35651866Sbostic 35751866Sbostic /* Write out the ifile */ 35851866Sbostic 35951866Sbostic put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift); 36051866Sbostic off += (lfsp->lfs_cleansz << lfsp->lfs_bshift); 36151866Sbostic (void)free(cleaninfo); 36251866Sbostic 36351866Sbostic put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift); 36451866Sbostic off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift); 36551866Sbostic (void)free(segtable); 36651866Sbostic 36751866Sbostic put(fd, off, ifile, lfsp->lfs_bsize); 36851866Sbostic off += lfsp->lfs_bsize; 36951866Sbostic 37051866Sbostic /* 37151866Sbostic * use ipagep for space for writing out other stuff. It used to 37251866Sbostic * contain the ifile, but we're done with it. 37351866Sbostic */ 37451866Sbostic 37551864Sbostic /* Write out the root and lost and found directories */ 37651866Sbostic bzero(ipagep, lfsp->lfs_bsize); 37751866Sbostic make_dir(ipagep, lfs_root_dir, 37851864Sbostic sizeof(lfs_root_dir) / sizeof(struct direct)); 37951866Sbostic *dp++ = ((u_long *)ipagep)[0]; 38051866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 38151866Sbostic off += lfsp->lfs_bsize; 38251149Sbostic 38351866Sbostic bzero(ipagep, lfsp->lfs_bsize); 38451866Sbostic make_dir(ipagep, lfs_lf_dir, 38551864Sbostic sizeof(lfs_lf_dir) / sizeof(struct direct)); 38651866Sbostic *dp++ = ((u_long *)ipagep)[0]; 38751866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 38851149Sbostic 38951866Sbostic /* Write Supberblock */ 39051866Sbostic lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize; 39151866Sbostic put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs)); 39251149Sbostic 39351866Sbostic /* 39451866Sbostic * Finally, calculate all the fields for the summary structure 39551866Sbostic * and write it. 39651866Sbostic */ 39751866Sbostic 39851864Sbostic summary.ss_next = lfsp->lfs_nextseg; 39951149Sbostic summary.ss_create = lfsp->lfs_tstamp; 40051866Sbostic summary.ss_nfinfo = 3; 40151866Sbostic summary.ss_ninos = 3; 40251866Sbostic summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used); 40351149Sbostic 40451864Sbostic /* 40551864Sbostic * Make sure that we don't overflow a summary block. We have to 40651866Sbostic * record: FINFO structures for ifile, root, and l+f. The number 40751866Sbostic * of blocks recorded for the ifile is determined by the size of 40851866Sbostic * the cleaner info and the segments usage table. There is room 40951866Sbostic * for one block included in sizeof(FINFO) so we don't need to add 41051866Sbostic * any extra space for the ROOT and L+F, and one block of the ifile 41151866Sbostic * is already counted. Finally, we leave room for 1 inode block 41251866Sbostic * address. 41351864Sbostic */ 414*53248Sbostic sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) + 41551866Sbostic (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long); 41651149Sbostic #define SUMERR \ 41751149Sbostic "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes." 41851149Sbostic if (sum_size > LFS_SUMMARY_SIZE) 41951149Sbostic fatal(SUMERR, sum_size); 42051149Sbostic 42151866Sbostic block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1; 42251149Sbostic 42351149Sbostic if (!(block_array = malloc(block_array_size *sizeof(int)))) 42451149Sbostic fatal("%s: %s", special, strerror(errno)); 42551149Sbostic 42651149Sbostic /* fill in the array */ 42751866Sbostic for (i = 0; i < block_array_size; i++) 42851149Sbostic block_array[i] = i; 42951149Sbostic 43051149Sbostic /* copy into segment */ 43151866Sbostic sump = ipagep; 43251149Sbostic bcopy(&summary, sump, sizeof(SEGSUM)); 43351149Sbostic sump += sizeof(SEGSUM); 43451149Sbostic 43551149Sbostic /* Now, add the ifile */ 43651866Sbostic file_info.fi_nblocks = block_array_size; 43751149Sbostic file_info.fi_version = 1; 43851149Sbostic file_info.fi_ino = LFS_IFILE_INUM; 43951149Sbostic 44051149Sbostic bcopy(&file_info, sump, sizeof(FINFO) - sizeof(u_long)); 44151149Sbostic sump += sizeof(FINFO) - sizeof(u_long); 44251149Sbostic bcopy(block_array, sump, sizeof(u_long) * file_info.fi_nblocks); 44351864Sbostic sump += sizeof(u_long) * file_info.fi_nblocks; 44451149Sbostic 44551864Sbostic /* Now, add the root directory */ 44651864Sbostic file_info.fi_nblocks = 1; 44751864Sbostic file_info.fi_version = 1; 44851864Sbostic file_info.fi_ino = ROOTINO; 44951864Sbostic file_info.fi_blocks[0] = 0; 45051864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 45151864Sbostic sump += sizeof(FINFO); 45251864Sbostic 45351864Sbostic /* Now, add the lost and found */ 45451864Sbostic file_info.fi_ino = LOSTFOUNDINO; 45551864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 45651864Sbostic 45751866Sbostic ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] = 45851866Sbostic lfsp->lfs_idaddr; 45951866Sbostic ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum), 46051866Sbostic LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum)); 46151866Sbostic put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE); 46251864Sbostic 46351866Sbostic sp = (SEGSUM *)ipagep; 46451866Sbostic sp->ss_create = 0; 46551866Sbostic sp->ss_nfinfo = 0; 46651866Sbostic sp->ss_ninos = 0; 46751866Sbostic sp->ss_datasum = 0; 46851149Sbostic 46951149Sbostic /* Now, write rest of segments containing superblocks */ 47051149Sbostic lfsp->lfs_tstamp = 0; 47151864Sbostic lfsp->lfs_cksum = 47251866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 47351864Sbostic for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1; 47451864Sbostic i < lfsp->lfs_nseg; i++) { 47551149Sbostic 47651864Sbostic seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb; 47751866Sbostic sp->ss_next = last_addr; 47851866Sbostic last_addr = seg_addr; 47951866Sbostic seg_seek = seg_addr * lp->d_secsize; 48051866Sbostic 48151864Sbostic if (seg_addr == lfsp->lfs_sboffs[j]) { 48251864Sbostic if (j < (LFS_MAXNUMSB - 2)) 48351864Sbostic j++; 48451866Sbostic put(fd, seg_seek, lfsp, sizeof(struct lfs)); 48551866Sbostic seg_seek += LFS_SBPAD; 48651866Sbostic } 48751149Sbostic 48851149Sbostic /* Summary */ 48951866Sbostic sp->ss_sumsum = cksum(&sp->ss_datasum, 49051866Sbostic LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 49151866Sbostic put(fd, seg_seek, sp, LFS_SUMMARY_SIZE); 49251149Sbostic } 49351866Sbostic free(ipagep); 49451864Sbostic close(fd); 49551149Sbostic return (0); 49651149Sbostic } 49751149Sbostic 49851149Sbostic static void 49951149Sbostic put(fd, off, p, len) 50051149Sbostic int fd; 50151149Sbostic off_t off; 50251149Sbostic void *p; 50351149Sbostic size_t len; 50451149Sbostic { 50551149Sbostic int wbytes; 50651149Sbostic 50751149Sbostic if (lseek(fd, off, SEEK_SET) < 0) 50851149Sbostic fatal("%s: %s", special, strerror(errno)); 50951149Sbostic if ((wbytes = write(fd, p, len)) < 0) 51051149Sbostic fatal("%s: %s", special, strerror(errno)); 51151149Sbostic if (wbytes != len) 51251149Sbostic fatal("%s: short write (%d, not %d)", special, wbytes, len); 51351149Sbostic } 51451864Sbostic 51551864Sbostic /* 51651864Sbostic * Create the root directory for this file system and the lost+found 51751864Sbostic * directory. 51851864Sbostic */ 51951864Sbostic 52051864Sbostic u_long d_ino; /* inode number of entry */ 52151864Sbostic u_short d_reclen; /* length of this record */ 52251864Sbostic u_short d_namlen; /* length of string in d_name */ 52351864Sbostic char d_name[MAXNAMLEN + 1]; /* name with length <= MAXNAMLEN */ 52451864Sbostic lfsinit() 52551864Sbostic { 52651864Sbostic } 52751864Sbostic 52851864Sbostic static daddr_t 52951864Sbostic make_dinode(ino, dip, nblocks, saddr, lfsp) 53051864Sbostic ino_t ino; /* inode we're creating */ 53151864Sbostic struct dinode *dip; /* disk inode */ 53251864Sbostic int nblocks; /* number of blocks in file */ 53351864Sbostic daddr_t saddr; /* starting block address */ 53451866Sbostic struct lfs *lfsp; /* superblock */ 53551864Sbostic { 53651864Sbostic int db_per_fb, i; 53751864Sbostic 53851864Sbostic dip->di_nlink = 1; 53951864Sbostic dip->di_blocks = nblocks; 54051864Sbostic 54151864Sbostic /* If we ever need something longer than 32 bits, this changes */ 54251864Sbostic dip->di_size = (dip->di_blocks << lfsp->lfs_bshift); 54351864Sbostic dip->di_atime = dip->di_mtime = dip->di_ctime = lfsp->lfs_tstamp; 54451864Sbostic dip->di_inum = ino; 54551864Sbostic 54651864Sbostic #define SEGERR \ 54751864Sbostic "File requires more than the number of direct blocks; increase block or segment size." 54851864Sbostic if (NDADDR < nblocks) 54951864Sbostic fatal("%s", SEGERR); 55051864Sbostic 55151864Sbostic /* Assign the block addresses for the ifile */ 55251864Sbostic db_per_fb = 1 << lfsp->lfs_fsbtodb; 55351864Sbostic for (i = 0; i < dip->di_blocks; i++, saddr += db_per_fb) 55451864Sbostic dip->di_db[i] = saddr; 55551864Sbostic 55651864Sbostic return (saddr); 55751864Sbostic } 55851864Sbostic 55951864Sbostic 56051864Sbostic /* 56151864Sbostic * Construct a set of directory entries in "bufp". We assume that all the 56251864Sbostic * entries in protodir fir in the first DIRBLKSIZ. 56351864Sbostic */ 56451864Sbostic static void 56551864Sbostic make_dir(bufp, protodir, entries) 56651864Sbostic void *bufp; 56751864Sbostic register struct direct *protodir; 56851864Sbostic int entries; 56951864Sbostic { 57051864Sbostic char *cp; 57151864Sbostic int i, spcleft; 57251864Sbostic 57351864Sbostic spcleft = DIRBLKSIZ; 57451864Sbostic for (cp = bufp, i = 0; i < entries - 1; i++) { 57551864Sbostic protodir[i].d_reclen = DIRSIZ(&protodir[i]); 57651864Sbostic bcopy(&protodir[i], cp, protodir[i].d_reclen); 57751864Sbostic cp += protodir[i].d_reclen; 57851864Sbostic if ((spcleft -= protodir[i].d_reclen) < 0) 57951864Sbostic fatal("%s: %s", special, "directory too big"); 58051864Sbostic } 58151864Sbostic protodir[i].d_reclen = spcleft; 58251864Sbostic bcopy(&protodir[i], cp, DIRSIZ(&protodir[i])); 58351864Sbostic } 584