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*54263Sbostic static char sccsid[] = "@(#)lfs.c 5.9 (Berkeley) 06/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, 52*54263Sbostic /* lfs_lastpseg */ 0, 5351149Sbostic /* lfs_tstamp */ 0, 5451149Sbostic /* lfs_minfree */ MINFREE, 5551866Sbostic /* lfs_dbpseg */ DFL_LFSSEG/DEV_BSIZE, 5651149Sbostic /* lfs_inopb */ DFL_LFSBLOCK/sizeof(struct dinode), 5751149Sbostic /* lfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 5851866Sbostic /* lfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 5951149Sbostic /* lfs_nindir */ DFL_LFSBLOCK/sizeof(daddr_t), 6051149Sbostic /* lfs_nseg */ 0, 6151149Sbostic /* lfs_nspf */ 0, 6251866Sbostic /* lfs_cleansz */ 0, 6351149Sbostic /* lfs_segtabsz */ 0, 6451149Sbostic /* lfs_segmask */ DFL_LFSSEG_MASK, 6551149Sbostic /* lfs_segshift */ DFL_LFSSEG_SHIFT, 6651149Sbostic /* lfs_bmask */ DFL_LFSBLOCK_MASK, 6751149Sbostic /* lfs_bshift */ DFL_LFSBLOCK_SHIFT, 6851149Sbostic /* lfs_ffmask */ 0, 6951149Sbostic /* lfs_ffshift */ 0, 7051149Sbostic /* lfs_fbmask */ 0, 7151149Sbostic /* lfs_fbshift */ 0, 7251149Sbostic /* lfs_fsbtodb */ 0, 7351149Sbostic /* lfs_sboffs */ { 0 }, 7451864Sbostic /* lfs_ivnode */ NULL, 7551864Sbostic /* lfs_segtab */ NULL, 7651864Sbostic /* lfs_seglist */ NULL, 7751864Sbostic /* lfs_iocount */ 0, 78*54263Sbostic /* lfs_writer */ 0, 79*54263Sbostic /* lfs_dirops */ 0, 80*54263Sbostic /* lfs_doifile */ 0, 8151149Sbostic /* lfs_fmod */ 0, 8251149Sbostic /* lfs_clean */ 0, 8351149Sbostic /* lfs_ronly */ 0, 8451149Sbostic /* lfs_flags */ 0, 8551149Sbostic /* lfs_fsmnt */ { 0 }, 8651864Sbostic /* lfs_pad */ { 0 }, 8751149Sbostic /* lfs_cksum */ 0 8851149Sbostic }; 8951149Sbostic 9051864Sbostic 9151864Sbostic struct direct lfs_root_dir[] = { 9251864Sbostic { ROOTINO, sizeof(struct direct), 1, "."}, 9351864Sbostic { ROOTINO, sizeof(struct direct), 2, ".."}, 9451866Sbostic { LFS_IFILE_INUM, sizeof(struct direct), 5, "ifile"}, 9551864Sbostic { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found"}, 9651864Sbostic }; 9751864Sbostic 9851864Sbostic struct direct lfs_lf_dir[] = { 9951864Sbostic { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, 10051864Sbostic { ROOTINO, sizeof(struct direct), 2, ".." }, 10151864Sbostic }; 10251864Sbostic 10351864Sbostic static daddr_t make_dinode 10451866Sbostic __P((ino_t, struct dinode *, int, daddr_t, struct lfs *)); 10551864Sbostic static void make_dir __P(( void *, struct direct *, int)); 10651149Sbostic static void put __P((int, off_t, void *, size_t)); 10751149Sbostic 10851149Sbostic int 10951149Sbostic make_lfs(fd, lp, partp, minfree, block_size, seg_size) 11051149Sbostic int fd; 11151149Sbostic struct disklabel *lp; 11251149Sbostic struct partition *partp; 11351149Sbostic int minfree; 11451149Sbostic int block_size; 11551149Sbostic int seg_size; 11651149Sbostic { 11751149Sbostic struct dinode *dip; /* Pointer to a disk inode */ 11851864Sbostic struct dinode *dpagep; /* Pointer to page of disk inodes */ 11951866Sbostic CLEANERINFO *cleaninfo; /* Segment cleaner information table */ 12051149Sbostic FINFO file_info; /* File info structure in summary blocks */ 12151149Sbostic IFILE *ifile; /* Pointer to array of ifile structures */ 12251149Sbostic IFILE *ip; /* Pointer to array of ifile structures */ 12351866Sbostic struct lfs *lfsp; /* Superblock */ 12451864Sbostic SEGUSE *segp; /* Segment usage table */ 12551864Sbostic SEGUSE *segtable; /* Segment usage table */ 12651149Sbostic SEGSUM summary; /* Segment summary structure */ 12751866Sbostic SEGSUM *sp; /* Segment summary pointer */ 12851149Sbostic daddr_t last_sb_addr; /* Address of superblocks */ 12951864Sbostic daddr_t last_addr; /* Previous segment address */ 13051149Sbostic daddr_t sb_addr; /* Address of superblocks */ 13151864Sbostic daddr_t seg_addr; /* Address of current segment */ 13251866Sbostic void *ipagep; /* Pointer to the page we use to write stuff */ 13351149Sbostic void *sump; /* Used to copy stuff into segment buffer */ 13451149Sbostic u_long *block_array; /* Array of logical block nos to put in sum */ 13551866Sbostic u_long blocks_used; /* Number of blocks in first segment */ 13651866Sbostic u_long *dp; /* Used to computed checksum on data */ 13751866Sbostic u_long *datasump; /* Used to computed checksum on data */ 13851149Sbostic int block_array_size; /* How many entries in block array */ 13951149Sbostic int bsize; /* Block size */ 14051149Sbostic int db_per_fb; /* Disk blocks per file block */ 14151864Sbostic int i, j; 14251866Sbostic int off; /* Offset at which to write */ 14351149Sbostic int sb_to_sum; /* offset between superblock and summary */ 14451149Sbostic int sb_interval; /* number of segs between super blocks */ 14551149Sbostic int seg_seek; /* Seek offset for a segment */ 14651149Sbostic int ssize; /* Segment size */ 14751149Sbostic int sum_size; /* Size of the summary block */ 14851149Sbostic int wbytes; /* Number of bytes returned by write */ 14951149Sbostic 15051149Sbostic lfsp = &lfs_default; 15151149Sbostic 15251149Sbostic if (!(bsize = block_size)) 15351149Sbostic bsize = DFL_LFSBLOCK; 15451149Sbostic if (!(ssize = seg_size)) 15551149Sbostic ssize = DFL_LFSSEG; 15651149Sbostic 15751149Sbostic /* Modify parts of superblock overridden by command line arguments */ 15851149Sbostic if (bsize != DFL_LFSBLOCK) { 15951149Sbostic lfsp->lfs_bshift = log2(bsize); 16051149Sbostic if (1 << lfsp->lfs_bshift != bsize) 16151149Sbostic fatal("%d: block size not a power of 2", bsize); 16251149Sbostic lfsp->lfs_bsize = bsize; 16351864Sbostic lfsp->lfs_fsize = bsize; 16451149Sbostic lfsp->lfs_bmask = bsize - 1; 16551149Sbostic lfsp->lfs_inopb = bsize / sizeof(struct dinode); 16651149Sbostic /* MIS -- should I round to power of 2 */ 16751149Sbostic lfsp->lfs_ifpb = bsize / sizeof(IFILE); 16851866Sbostic lfsp->lfs_sepb = bsize / sizeof(SEGUSE); 16951149Sbostic lfsp->lfs_nindir = bsize / sizeof(daddr_t); 17051149Sbostic } 17151149Sbostic 17251149Sbostic if (ssize != DFL_LFSSEG) { 17351149Sbostic lfsp->lfs_segshift = log2(ssize); 17451149Sbostic if (1 << lfsp->lfs_segshift != ssize) 17551149Sbostic fatal("%d: segment size not power of 2", ssize); 17651149Sbostic lfsp->lfs_ssize = ssize; 17751149Sbostic lfsp->lfs_segmask = ssize - 1; 17851149Sbostic } 17951149Sbostic lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift; 18051149Sbostic 18151149Sbostic if (minfree) 18251149Sbostic lfsp->lfs_minfree = minfree; 18351149Sbostic 18451149Sbostic /* 18551149Sbostic * Fill in parts of superblock that can be computed from file system 18651149Sbostic * size, disk geometry and current time. 18751149Sbostic */ 18851149Sbostic db_per_fb = bsize/lp->d_secsize; 18951149Sbostic lfsp->lfs_fsbtodb = log2(db_per_fb); 19051149Sbostic lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb; 19151149Sbostic lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift); 19251149Sbostic lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize; 19351864Sbostic 19451864Sbostic /* 19551864Sbostic * The number of free blocks is set from the total data size (lfs_dsize) 19651864Sbostic * minus one block for each segment (for the segment summary). Then 19751866Sbostic * we'll subtract off the room for the superblocks, ifile entries and 19851866Sbostic * segment usage table. 19951864Sbostic */ 20051864Sbostic lfsp->lfs_bfree = lfsp->lfs_dsize - lfsp->lfs_nseg; 20151149Sbostic lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp); 20251866Sbostic lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp); 20351149Sbostic if ((lfsp->lfs_tstamp = time(NULL)) == -1) 20451149Sbostic fatal("time: %s", strerror(errno)); 20551149Sbostic if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 20651149Sbostic sb_interval = LFS_MIN_SBINTERVAL; 20751149Sbostic 20851149Sbostic /* 20951149Sbostic * Now, lay out the file system. We need to figure out where 21051149Sbostic * the superblocks go, initialize the checkpoint information 21151149Sbostic * for the first two superblocks, initialize the segment usage 21251149Sbostic * information, put the segusage information in the ifile, create 21351149Sbostic * the first block of IFILE structures, and link all the IFILE 21451149Sbostic * structures into a free list. 21551149Sbostic */ 21651149Sbostic 21751149Sbostic /* Figure out where the superblocks are going to live */ 21851149Sbostic lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize; 21951149Sbostic for (i = 1; i < LFS_MAXNUMSB; i++) { 22051149Sbostic sb_addr = ((i * sb_interval) << 22151149Sbostic (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb)) 22251149Sbostic + lfsp->lfs_sboffs[0]; 22351149Sbostic if (sb_addr > partp->p_size) 22451149Sbostic break; 22551149Sbostic lfsp->lfs_sboffs[i] = sb_addr; 22651149Sbostic } 22751149Sbostic last_sb_addr = lfsp->lfs_sboffs[i - 1]; 22851149Sbostic lfsp->lfs_lastseg = lfsp->lfs_sboffs[0]; 22951864Sbostic lfsp->lfs_nextseg = 23051864Sbostic lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0]; 23151866Sbostic lfsp->lfs_curseg = lfsp->lfs_lastseg; 23251149Sbostic 23351149Sbostic /* 23451149Sbostic * Initialize the segment usage table. The first segment will 23551866Sbostic * contain the superblock, the cleanerinfo (cleansz), the segusage 23651866Sbostic * table * (segtabsz), 1 block's worth of IFILE entries, the root 23751866Sbostic * directory, the lost+found directory and one block's worth of 23851866Sbostic * inodes (containing the ifile, root, and l+f inodes). 23951149Sbostic */ 24051866Sbostic if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift))) 24151866Sbostic fatal("%s", strerror(errno)); 24251930Sbostic cleaninfo->clean = lfsp->lfs_nseg - 1; 24351930Sbostic cleaninfo->dirty = 1; 24451930Sbostic 24551149Sbostic if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift))) 24651149Sbostic fatal("%s", strerror(errno)); 24751149Sbostic segp = segtable; 24851866Sbostic blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4; 24952088Sbostic segp->su_nbytes = blocks_used << lfsp->lfs_bshift; 25051149Sbostic segp->su_lastmod = lfsp->lfs_tstamp; 25151930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY; 25251930Sbostic lfsp->lfs_bfree -= lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4; 25351149Sbostic 25451866Sbostic /* 25551866Sbostic * Now figure out the address of the ifile inode. The inode block 25651866Sbostic * appears immediately after the segment summary. 25751866Sbostic */ 25851866Sbostic lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) / 25951866Sbostic lp->d_secsize; 26051149Sbostic 26151149Sbostic for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) { 26251864Sbostic if ((i % sb_interval) == 0) { 26351930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK; 26451864Sbostic lfsp->lfs_bfree -= (LFS_SBPAD >> lfsp->lfs_bshift); 26552088Sbostic } else 26651930Sbostic segp->su_flags = 0; 26751149Sbostic segp->su_lastmod = 0; 26852088Sbostic segp->su_nbytes = 0; 26951149Sbostic } 27051149Sbostic 27151149Sbostic /* 27251149Sbostic * Ready to start writing segments. The first segment is different 27351149Sbostic * because it contains the segment usage table and the ifile inode 27451864Sbostic * as well as a superblock. For the rest of the segments, set the 27551864Sbostic * time stamp to be 0 so that the first segment is the most recent. 27651864Sbostic * For each segment that is supposed to contain a copy of the super 27751864Sbostic * block, initialize its first few blocks and its segment summary 27851864Sbostic * to indicate this. 27951149Sbostic */ 28051864Sbostic lfsp->lfs_nfiles = LFS_FIRST_INUM - 1; 28151149Sbostic lfsp->lfs_cksum = 28251866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 28351149Sbostic 28451864Sbostic /* Now create a block of disk inodes */ 28551864Sbostic if (!(dpagep = malloc(lfsp->lfs_bsize))) 28651864Sbostic fatal("%s", strerror(errno)); 28751864Sbostic dip = (struct dinode *)dpagep; 28851864Sbostic bzero(dip, lfsp->lfs_bsize); 28951864Sbostic 29051866Sbostic /* Create a block of IFILE structures. */ 29151866Sbostic if (!(ipagep = malloc(lfsp->lfs_bsize))) 29251149Sbostic fatal("%s", strerror(errno)); 29351866Sbostic ifile = (IFILE *)ipagep; 29451864Sbostic 29551866Sbostic /* 29651866Sbostic * Initialize IFILE. It is the next block following the 29751866Sbostic * block of inodes (whose address has been calculated in 29851866Sbostic * lfsp->lfs_idaddr; 29951866Sbostic */ 30051866Sbostic sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize; 30151866Sbostic sb_addr = make_dinode(LFS_IFILE_INUM, dip, 30251866Sbostic lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp); 30351864Sbostic dip->di_mode = IFREG|IREAD|IWRITE; 30451864Sbostic ip = &ifile[LFS_IFILE_INUM]; 30551864Sbostic ip->if_version = 1; 30651864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 30751864Sbostic 30851864Sbostic /* Initialize the ROOT Directory */ 30951864Sbostic sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp); 31051864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 31151864Sbostic dip->di_size = DIRBLKSIZ; 31251864Sbostic dip->di_nlink = 3; 31351864Sbostic ip = &ifile[ROOTINO]; 31451864Sbostic ip->if_version = 1; 31551864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 31651864Sbostic 31751864Sbostic /* Initialize the lost+found Directory */ 31851864Sbostic sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp); 31951864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 32051864Sbostic dip->di_size = DIRBLKSIZ; 32151864Sbostic dip->di_nlink = 2; 32251864Sbostic ip = &ifile[LOSTFOUNDINO]; 32351864Sbostic ip->if_version = 1; 32451864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 32551864Sbostic 32651864Sbostic /* Make all the other dinodes invalid */ 32751864Sbostic for (i = INOPB(lfsp)-3, dip++; i; i--, dip++) 32851864Sbostic dip->di_inum = LFS_UNUSED_INUM; 32951864Sbostic 33051864Sbostic 33151864Sbostic /* Link remaining IFILE entries in free list */ 33251864Sbostic for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM; 33351864Sbostic i < lfsp->lfs_ifpb; ++ip) { 33451149Sbostic ip->if_version = 1; 33551149Sbostic ip->if_daddr = LFS_UNUSED_DADDR; 33651149Sbostic ip->if_nextfree = ++i; 33751149Sbostic } 33851149Sbostic ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM; 33951149Sbostic 34051866Sbostic /* Now, write the segment */ 34151149Sbostic 34251866Sbostic /* Compute a checksum across all the data you're writing */ 34351866Sbostic dp = datasump = malloc (blocks_used * sizeof(u_long)); 34451866Sbostic *dp++ = ((u_long *)dip)[0]; /* inode block */ 34551866Sbostic for (i = 0; i < lfsp->lfs_cleansz; i++) 34651866Sbostic *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) / 34751866Sbostic sizeof(u_long)]; /* Cleaner info */ 34851866Sbostic for (i = 0; i < lfsp->lfs_segtabsz; i++) 34951866Sbostic *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) / 35051866Sbostic sizeof(u_long)]; /* Segusage table */ 35151866Sbostic *dp++ = ((u_long *)ifile)[0]; /* Ifile */ 35251866Sbostic 35351866Sbostic /* Still need the root and l+f bytes; get them later */ 35451866Sbostic 35551866Sbostic /* Write out the inode block */ 35651866Sbostic off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE; 35751866Sbostic put(fd, off, dpagep, lfsp->lfs_bsize); 35851866Sbostic free(dpagep); 35951866Sbostic off += lfsp->lfs_bsize; 36051866Sbostic 36151866Sbostic /* Write out the ifile */ 36251866Sbostic 36351866Sbostic put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift); 36451866Sbostic off += (lfsp->lfs_cleansz << lfsp->lfs_bshift); 36551866Sbostic (void)free(cleaninfo); 36651866Sbostic 36751866Sbostic put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift); 36851866Sbostic off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift); 36951866Sbostic (void)free(segtable); 37051866Sbostic 37151866Sbostic put(fd, off, ifile, lfsp->lfs_bsize); 37251866Sbostic off += lfsp->lfs_bsize; 37351866Sbostic 37451866Sbostic /* 37551866Sbostic * use ipagep for space for writing out other stuff. It used to 37651866Sbostic * contain the ifile, but we're done with it. 37751866Sbostic */ 37851866Sbostic 37951864Sbostic /* Write out the root and lost and found directories */ 38051866Sbostic bzero(ipagep, lfsp->lfs_bsize); 38151866Sbostic make_dir(ipagep, lfs_root_dir, 38251864Sbostic sizeof(lfs_root_dir) / sizeof(struct direct)); 38351866Sbostic *dp++ = ((u_long *)ipagep)[0]; 38451866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 38551866Sbostic off += lfsp->lfs_bsize; 38651149Sbostic 38751866Sbostic bzero(ipagep, lfsp->lfs_bsize); 38851866Sbostic make_dir(ipagep, lfs_lf_dir, 38951864Sbostic sizeof(lfs_lf_dir) / sizeof(struct direct)); 39051866Sbostic *dp++ = ((u_long *)ipagep)[0]; 39151866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 39251149Sbostic 39351866Sbostic /* Write Supberblock */ 39451866Sbostic lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize; 39551866Sbostic put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs)); 39651149Sbostic 39751866Sbostic /* 39851866Sbostic * Finally, calculate all the fields for the summary structure 39951866Sbostic * and write it. 40051866Sbostic */ 40151866Sbostic 40251864Sbostic summary.ss_next = lfsp->lfs_nextseg; 40351149Sbostic summary.ss_create = lfsp->lfs_tstamp; 40451866Sbostic summary.ss_nfinfo = 3; 40551866Sbostic summary.ss_ninos = 3; 40651866Sbostic summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used); 40751149Sbostic 40851864Sbostic /* 40951864Sbostic * Make sure that we don't overflow a summary block. We have to 41051866Sbostic * record: FINFO structures for ifile, root, and l+f. The number 41151866Sbostic * of blocks recorded for the ifile is determined by the size of 41251866Sbostic * the cleaner info and the segments usage table. There is room 41351866Sbostic * for one block included in sizeof(FINFO) so we don't need to add 41451866Sbostic * any extra space for the ROOT and L+F, and one block of the ifile 41551866Sbostic * is already counted. Finally, we leave room for 1 inode block 41651866Sbostic * address. 41751864Sbostic */ 41853248Sbostic sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) + 41951866Sbostic (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long); 42051149Sbostic #define SUMERR \ 42151149Sbostic "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes." 42251149Sbostic if (sum_size > LFS_SUMMARY_SIZE) 42351149Sbostic fatal(SUMERR, sum_size); 42451149Sbostic 42551866Sbostic block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1; 42651149Sbostic 42751149Sbostic if (!(block_array = malloc(block_array_size *sizeof(int)))) 42851149Sbostic fatal("%s: %s", special, strerror(errno)); 42951149Sbostic 43051149Sbostic /* fill in the array */ 43151866Sbostic for (i = 0; i < block_array_size; i++) 43251149Sbostic block_array[i] = i; 43351149Sbostic 43451149Sbostic /* copy into segment */ 43551866Sbostic sump = ipagep; 43651149Sbostic bcopy(&summary, sump, sizeof(SEGSUM)); 43751149Sbostic sump += sizeof(SEGSUM); 43851149Sbostic 43951149Sbostic /* Now, add the ifile */ 44051866Sbostic file_info.fi_nblocks = block_array_size; 44151149Sbostic file_info.fi_version = 1; 44251149Sbostic file_info.fi_ino = LFS_IFILE_INUM; 44351149Sbostic 44451149Sbostic bcopy(&file_info, sump, sizeof(FINFO) - sizeof(u_long)); 44551149Sbostic sump += sizeof(FINFO) - sizeof(u_long); 44651149Sbostic bcopy(block_array, sump, sizeof(u_long) * file_info.fi_nblocks); 44751864Sbostic sump += sizeof(u_long) * file_info.fi_nblocks; 44851149Sbostic 44951864Sbostic /* Now, add the root directory */ 45051864Sbostic file_info.fi_nblocks = 1; 45151864Sbostic file_info.fi_version = 1; 45251864Sbostic file_info.fi_ino = ROOTINO; 45351864Sbostic file_info.fi_blocks[0] = 0; 45451864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 45551864Sbostic sump += sizeof(FINFO); 45651864Sbostic 45751864Sbostic /* Now, add the lost and found */ 45851864Sbostic file_info.fi_ino = LOSTFOUNDINO; 45951864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 46051864Sbostic 46151866Sbostic ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] = 46251866Sbostic lfsp->lfs_idaddr; 46351866Sbostic ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum), 46451866Sbostic LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum)); 46551866Sbostic put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE); 46651864Sbostic 46751866Sbostic sp = (SEGSUM *)ipagep; 46851866Sbostic sp->ss_create = 0; 46951866Sbostic sp->ss_nfinfo = 0; 47051866Sbostic sp->ss_ninos = 0; 47151866Sbostic sp->ss_datasum = 0; 47251149Sbostic 47351149Sbostic /* Now, write rest of segments containing superblocks */ 47451149Sbostic lfsp->lfs_tstamp = 0; 47551864Sbostic lfsp->lfs_cksum = 47651866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 47751864Sbostic for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1; 47851864Sbostic i < lfsp->lfs_nseg; i++) { 47951149Sbostic 48051864Sbostic seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb; 48151866Sbostic sp->ss_next = last_addr; 48251866Sbostic last_addr = seg_addr; 48351866Sbostic seg_seek = seg_addr * lp->d_secsize; 48451866Sbostic 48551864Sbostic if (seg_addr == lfsp->lfs_sboffs[j]) { 48651864Sbostic if (j < (LFS_MAXNUMSB - 2)) 48751864Sbostic j++; 48851866Sbostic put(fd, seg_seek, lfsp, sizeof(struct lfs)); 48951866Sbostic seg_seek += LFS_SBPAD; 49051866Sbostic } 49151149Sbostic 49251149Sbostic /* Summary */ 49351866Sbostic sp->ss_sumsum = cksum(&sp->ss_datasum, 49451866Sbostic LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 49551866Sbostic put(fd, seg_seek, sp, LFS_SUMMARY_SIZE); 49651149Sbostic } 49751866Sbostic free(ipagep); 49851864Sbostic close(fd); 49951149Sbostic return (0); 50051149Sbostic } 50151149Sbostic 50251149Sbostic static void 50351149Sbostic put(fd, off, p, len) 50451149Sbostic int fd; 50551149Sbostic off_t off; 50651149Sbostic void *p; 50751149Sbostic size_t len; 50851149Sbostic { 50951149Sbostic int wbytes; 51051149Sbostic 51151149Sbostic if (lseek(fd, off, SEEK_SET) < 0) 51251149Sbostic fatal("%s: %s", special, strerror(errno)); 51351149Sbostic if ((wbytes = write(fd, p, len)) < 0) 51451149Sbostic fatal("%s: %s", special, strerror(errno)); 51551149Sbostic if (wbytes != len) 51651149Sbostic fatal("%s: short write (%d, not %d)", special, wbytes, len); 51751149Sbostic } 51851864Sbostic 51951864Sbostic /* 52051864Sbostic * Create the root directory for this file system and the lost+found 52151864Sbostic * directory. 52251864Sbostic */ 52351864Sbostic 52451864Sbostic u_long d_ino; /* inode number of entry */ 52551864Sbostic u_short d_reclen; /* length of this record */ 52651864Sbostic u_short d_namlen; /* length of string in d_name */ 52751864Sbostic char d_name[MAXNAMLEN + 1]; /* name with length <= MAXNAMLEN */ 52851864Sbostic lfsinit() 52951864Sbostic { 53051864Sbostic } 53151864Sbostic 53251864Sbostic static daddr_t 53351864Sbostic make_dinode(ino, dip, nblocks, saddr, lfsp) 53451864Sbostic ino_t ino; /* inode we're creating */ 53551864Sbostic struct dinode *dip; /* disk inode */ 53651864Sbostic int nblocks; /* number of blocks in file */ 53751864Sbostic daddr_t saddr; /* starting block address */ 53851866Sbostic struct lfs *lfsp; /* superblock */ 53951864Sbostic { 54051864Sbostic int db_per_fb, i; 54151864Sbostic 54251864Sbostic dip->di_nlink = 1; 54351864Sbostic dip->di_blocks = nblocks; 54451864Sbostic 54551864Sbostic /* If we ever need something longer than 32 bits, this changes */ 54651864Sbostic dip->di_size = (dip->di_blocks << lfsp->lfs_bshift); 54751864Sbostic dip->di_atime = dip->di_mtime = dip->di_ctime = lfsp->lfs_tstamp; 54851864Sbostic dip->di_inum = ino; 54951864Sbostic 55051864Sbostic #define SEGERR \ 55151864Sbostic "File requires more than the number of direct blocks; increase block or segment size." 55251864Sbostic if (NDADDR < nblocks) 55351864Sbostic fatal("%s", SEGERR); 55451864Sbostic 55551864Sbostic /* Assign the block addresses for the ifile */ 55651864Sbostic db_per_fb = 1 << lfsp->lfs_fsbtodb; 55751864Sbostic for (i = 0; i < dip->di_blocks; i++, saddr += db_per_fb) 55851864Sbostic dip->di_db[i] = saddr; 55951864Sbostic 56051864Sbostic return (saddr); 56151864Sbostic } 56251864Sbostic 56351864Sbostic 56451864Sbostic /* 56551864Sbostic * Construct a set of directory entries in "bufp". We assume that all the 56651864Sbostic * entries in protodir fir in the first DIRBLKSIZ. 56751864Sbostic */ 56851864Sbostic static void 56951864Sbostic make_dir(bufp, protodir, entries) 57051864Sbostic void *bufp; 57151864Sbostic register struct direct *protodir; 57251864Sbostic int entries; 57351864Sbostic { 57451864Sbostic char *cp; 57551864Sbostic int i, spcleft; 57651864Sbostic 57751864Sbostic spcleft = DIRBLKSIZ; 57851864Sbostic for (cp = bufp, i = 0; i < entries - 1; i++) { 57951864Sbostic protodir[i].d_reclen = DIRSIZ(&protodir[i]); 58051864Sbostic bcopy(&protodir[i], cp, protodir[i].d_reclen); 58151864Sbostic cp += protodir[i].d_reclen; 58251864Sbostic if ((spcleft -= protodir[i].d_reclen) < 0) 58351864Sbostic fatal("%s: %s", special, "directory too big"); 58451864Sbostic } 58551864Sbostic protodir[i].d_reclen = spcleft; 58651864Sbostic bcopy(&protodir[i], cp, DIRSIZ(&protodir[i])); 58751864Sbostic } 588