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*54715Sbostic static char sccsid[] = "@(#)lfs.c 5.12 (Berkeley) 07/06/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, 5254263Sbostic /* 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, 7554710Sbostic /* lfs_seglock */ 0, 7651864Sbostic /* lfs_iocount */ 0, 7754263Sbostic /* lfs_writer */ 0, 7854263Sbostic /* lfs_dirops */ 0, 7954263Sbostic /* lfs_doifile */ 0, 8051149Sbostic /* lfs_fmod */ 0, 8151149Sbostic /* lfs_clean */ 0, 8251149Sbostic /* lfs_ronly */ 0, 8351149Sbostic /* lfs_flags */ 0, 8451149Sbostic /* lfs_fsmnt */ { 0 }, 8551864Sbostic /* lfs_pad */ { 0 }, 8651149Sbostic /* lfs_cksum */ 0 8751149Sbostic }; 8851149Sbostic 8951864Sbostic 9051864Sbostic struct direct lfs_root_dir[] = { 91*54715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 1, "."}, 92*54715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."}, 93*54715Sbostic { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"}, 94*54715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"}, 9551864Sbostic }; 9651864Sbostic 9751864Sbostic struct direct lfs_lf_dir[] = { 98*54715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 99*54715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 10051864Sbostic }; 10151864Sbostic 10251864Sbostic static daddr_t make_dinode 10351866Sbostic __P((ino_t, struct dinode *, int, daddr_t, struct lfs *)); 10451864Sbostic static void make_dir __P(( void *, struct direct *, int)); 10551149Sbostic static void put __P((int, off_t, void *, size_t)); 10651149Sbostic 10751149Sbostic int 10851149Sbostic make_lfs(fd, lp, partp, minfree, block_size, seg_size) 10951149Sbostic int fd; 11051149Sbostic struct disklabel *lp; 11151149Sbostic struct partition *partp; 11251149Sbostic int minfree; 11351149Sbostic int block_size; 11451149Sbostic int seg_size; 11551149Sbostic { 11651149Sbostic struct dinode *dip; /* Pointer to a disk inode */ 11751864Sbostic struct dinode *dpagep; /* Pointer to page of disk inodes */ 11851866Sbostic CLEANERINFO *cleaninfo; /* Segment cleaner information table */ 11951149Sbostic FINFO file_info; /* File info structure in summary blocks */ 12051149Sbostic IFILE *ifile; /* Pointer to array of ifile structures */ 12151149Sbostic IFILE *ip; /* Pointer to array of ifile structures */ 12251866Sbostic struct lfs *lfsp; /* Superblock */ 12351864Sbostic SEGUSE *segp; /* Segment usage table */ 12451864Sbostic SEGUSE *segtable; /* Segment usage table */ 12551149Sbostic SEGSUM summary; /* Segment summary structure */ 12651866Sbostic SEGSUM *sp; /* Segment summary pointer */ 12751149Sbostic daddr_t last_sb_addr; /* Address of superblocks */ 12851864Sbostic daddr_t last_addr; /* Previous segment address */ 12951149Sbostic daddr_t sb_addr; /* Address of superblocks */ 13051864Sbostic daddr_t seg_addr; /* Address of current segment */ 13151866Sbostic void *ipagep; /* Pointer to the page we use to write stuff */ 13251149Sbostic void *sump; /* Used to copy stuff into segment buffer */ 13351149Sbostic u_long *block_array; /* Array of logical block nos to put in sum */ 13451866Sbostic u_long blocks_used; /* Number of blocks in first segment */ 13551866Sbostic u_long *dp; /* Used to computed checksum on data */ 13651866Sbostic u_long *datasump; /* Used to computed checksum on data */ 13751149Sbostic int block_array_size; /* How many entries in block array */ 13851149Sbostic int bsize; /* Block size */ 13951149Sbostic int db_per_fb; /* Disk blocks per file block */ 14051864Sbostic int i, j; 14151866Sbostic int off; /* Offset at which to write */ 14251149Sbostic int sb_to_sum; /* offset between superblock and summary */ 14351149Sbostic int sb_interval; /* number of segs between super blocks */ 14451149Sbostic int seg_seek; /* Seek offset for a segment */ 14551149Sbostic int ssize; /* Segment size */ 14651149Sbostic int sum_size; /* Size of the summary block */ 14751149Sbostic int wbytes; /* Number of bytes returned by write */ 14851149Sbostic 14951149Sbostic lfsp = &lfs_default; 15051149Sbostic 15151149Sbostic if (!(bsize = block_size)) 15251149Sbostic bsize = DFL_LFSBLOCK; 15351149Sbostic if (!(ssize = seg_size)) 15451149Sbostic ssize = DFL_LFSSEG; 15551149Sbostic 15651149Sbostic /* Modify parts of superblock overridden by command line arguments */ 15751149Sbostic if (bsize != DFL_LFSBLOCK) { 15851149Sbostic lfsp->lfs_bshift = log2(bsize); 15951149Sbostic if (1 << lfsp->lfs_bshift != bsize) 16051149Sbostic fatal("%d: block size not a power of 2", bsize); 16151149Sbostic lfsp->lfs_bsize = bsize; 16251864Sbostic lfsp->lfs_fsize = bsize; 16351149Sbostic lfsp->lfs_bmask = bsize - 1; 16451149Sbostic lfsp->lfs_inopb = bsize / sizeof(struct dinode); 16551149Sbostic /* MIS -- should I round to power of 2 */ 16651149Sbostic lfsp->lfs_ifpb = bsize / sizeof(IFILE); 16751866Sbostic lfsp->lfs_sepb = bsize / sizeof(SEGUSE); 16851149Sbostic lfsp->lfs_nindir = bsize / sizeof(daddr_t); 16951149Sbostic } 17051149Sbostic 17151149Sbostic if (ssize != DFL_LFSSEG) { 17251149Sbostic lfsp->lfs_segshift = log2(ssize); 17351149Sbostic if (1 << lfsp->lfs_segshift != ssize) 17451149Sbostic fatal("%d: segment size not power of 2", ssize); 17551149Sbostic lfsp->lfs_ssize = ssize; 17651149Sbostic lfsp->lfs_segmask = ssize - 1; 17751149Sbostic } 17851149Sbostic lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift; 17951149Sbostic 18051149Sbostic if (minfree) 18151149Sbostic lfsp->lfs_minfree = minfree; 18251149Sbostic 18351149Sbostic /* 18451149Sbostic * Fill in parts of superblock that can be computed from file system 18551149Sbostic * size, disk geometry and current time. 18651149Sbostic */ 18751149Sbostic db_per_fb = bsize/lp->d_secsize; 18851149Sbostic lfsp->lfs_fsbtodb = log2(db_per_fb); 18951149Sbostic lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb; 19051149Sbostic lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift); 19151149Sbostic lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize; 19251864Sbostic 19351864Sbostic /* 19451864Sbostic * The number of free blocks is set from the total data size (lfs_dsize) 19551864Sbostic * minus one block for each segment (for the segment summary). Then 19651866Sbostic * we'll subtract off the room for the superblocks, ifile entries and 19751866Sbostic * segment usage table. 19851864Sbostic */ 19951864Sbostic lfsp->lfs_bfree = lfsp->lfs_dsize - lfsp->lfs_nseg; 20051149Sbostic lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp); 20151866Sbostic lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp); 20251149Sbostic if ((lfsp->lfs_tstamp = time(NULL)) == -1) 20351149Sbostic fatal("time: %s", strerror(errno)); 20451149Sbostic if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 20551149Sbostic sb_interval = LFS_MIN_SBINTERVAL; 20651149Sbostic 20751149Sbostic /* 20851149Sbostic * Now, lay out the file system. We need to figure out where 20951149Sbostic * the superblocks go, initialize the checkpoint information 21051149Sbostic * for the first two superblocks, initialize the segment usage 21151149Sbostic * information, put the segusage information in the ifile, create 21251149Sbostic * the first block of IFILE structures, and link all the IFILE 21351149Sbostic * structures into a free list. 21451149Sbostic */ 21551149Sbostic 21651149Sbostic /* Figure out where the superblocks are going to live */ 21751149Sbostic lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize; 21851149Sbostic for (i = 1; i < LFS_MAXNUMSB; i++) { 21951149Sbostic sb_addr = ((i * sb_interval) << 22051149Sbostic (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb)) 22151149Sbostic + lfsp->lfs_sboffs[0]; 22251149Sbostic if (sb_addr > partp->p_size) 22351149Sbostic break; 22451149Sbostic lfsp->lfs_sboffs[i] = sb_addr; 22551149Sbostic } 22651149Sbostic last_sb_addr = lfsp->lfs_sboffs[i - 1]; 22751149Sbostic lfsp->lfs_lastseg = lfsp->lfs_sboffs[0]; 22851864Sbostic lfsp->lfs_nextseg = 22951864Sbostic lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0]; 23051866Sbostic lfsp->lfs_curseg = lfsp->lfs_lastseg; 23151149Sbostic 23251149Sbostic /* 23351149Sbostic * Initialize the segment usage table. The first segment will 23451866Sbostic * contain the superblock, the cleanerinfo (cleansz), the segusage 23551866Sbostic * table * (segtabsz), 1 block's worth of IFILE entries, the root 23651866Sbostic * directory, the lost+found directory and one block's worth of 23751866Sbostic * inodes (containing the ifile, root, and l+f inodes). 23851149Sbostic */ 23951866Sbostic if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift))) 24051866Sbostic fatal("%s", strerror(errno)); 24151930Sbostic cleaninfo->clean = lfsp->lfs_nseg - 1; 24251930Sbostic cleaninfo->dirty = 1; 24351930Sbostic 24451149Sbostic if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift))) 24551149Sbostic fatal("%s", strerror(errno)); 24651149Sbostic segp = segtable; 24751866Sbostic blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4; 24852088Sbostic segp->su_nbytes = blocks_used << lfsp->lfs_bshift; 24951149Sbostic segp->su_lastmod = lfsp->lfs_tstamp; 25051930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY; 25151930Sbostic lfsp->lfs_bfree -= lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4; 25251149Sbostic 25351866Sbostic /* 25451866Sbostic * Now figure out the address of the ifile inode. The inode block 25551866Sbostic * appears immediately after the segment summary. 25651866Sbostic */ 25751866Sbostic lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) / 25851866Sbostic lp->d_secsize; 25951149Sbostic 26051149Sbostic for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) { 26151864Sbostic if ((i % sb_interval) == 0) { 26251930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK; 26351864Sbostic lfsp->lfs_bfree -= (LFS_SBPAD >> lfsp->lfs_bshift); 26452088Sbostic } else 26551930Sbostic segp->su_flags = 0; 26651149Sbostic segp->su_lastmod = 0; 26752088Sbostic segp->su_nbytes = 0; 26851149Sbostic } 26951149Sbostic 27051149Sbostic /* 27151149Sbostic * Ready to start writing segments. The first segment is different 27251149Sbostic * because it contains the segment usage table and the ifile inode 27351864Sbostic * as well as a superblock. For the rest of the segments, set the 27451864Sbostic * time stamp to be 0 so that the first segment is the most recent. 27551864Sbostic * For each segment that is supposed to contain a copy of the super 27651864Sbostic * block, initialize its first few blocks and its segment summary 27751864Sbostic * to indicate this. 27851149Sbostic */ 27951864Sbostic lfsp->lfs_nfiles = LFS_FIRST_INUM - 1; 28051149Sbostic lfsp->lfs_cksum = 28151866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 28251149Sbostic 28351864Sbostic /* Now create a block of disk inodes */ 28451864Sbostic if (!(dpagep = malloc(lfsp->lfs_bsize))) 28551864Sbostic fatal("%s", strerror(errno)); 28651864Sbostic dip = (struct dinode *)dpagep; 28751864Sbostic bzero(dip, lfsp->lfs_bsize); 28851864Sbostic 28951866Sbostic /* Create a block of IFILE structures. */ 29051866Sbostic if (!(ipagep = malloc(lfsp->lfs_bsize))) 29151149Sbostic fatal("%s", strerror(errno)); 29251866Sbostic ifile = (IFILE *)ipagep; 29351864Sbostic 29451866Sbostic /* 29551866Sbostic * Initialize IFILE. It is the next block following the 29651866Sbostic * block of inodes (whose address has been calculated in 29751866Sbostic * lfsp->lfs_idaddr; 29851866Sbostic */ 29951866Sbostic sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize; 30051866Sbostic sb_addr = make_dinode(LFS_IFILE_INUM, dip, 30151866Sbostic lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp); 30251864Sbostic dip->di_mode = IFREG|IREAD|IWRITE; 30351864Sbostic ip = &ifile[LFS_IFILE_INUM]; 30451864Sbostic ip->if_version = 1; 30551864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 30651864Sbostic 30751864Sbostic /* Initialize the ROOT Directory */ 30851864Sbostic sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp); 30951864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 31051864Sbostic dip->di_size = DIRBLKSIZ; 31151864Sbostic dip->di_nlink = 3; 31251864Sbostic ip = &ifile[ROOTINO]; 31351864Sbostic ip->if_version = 1; 31451864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 31551864Sbostic 31651864Sbostic /* Initialize the lost+found Directory */ 31751864Sbostic sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp); 31851864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 31951864Sbostic dip->di_size = DIRBLKSIZ; 32051864Sbostic dip->di_nlink = 2; 32151864Sbostic ip = &ifile[LOSTFOUNDINO]; 32251864Sbostic ip->if_version = 1; 32351864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 32451864Sbostic 32551864Sbostic /* Make all the other dinodes invalid */ 32651864Sbostic for (i = INOPB(lfsp)-3, dip++; i; i--, dip++) 32751864Sbostic dip->di_inum = LFS_UNUSED_INUM; 32851864Sbostic 32951864Sbostic 33051864Sbostic /* Link remaining IFILE entries in free list */ 33151864Sbostic for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM; 33251864Sbostic i < lfsp->lfs_ifpb; ++ip) { 33351149Sbostic ip->if_version = 1; 33451149Sbostic ip->if_daddr = LFS_UNUSED_DADDR; 33551149Sbostic ip->if_nextfree = ++i; 33651149Sbostic } 33751149Sbostic ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM; 33851149Sbostic 33951866Sbostic /* Now, write the segment */ 34051149Sbostic 34151866Sbostic /* Compute a checksum across all the data you're writing */ 34251866Sbostic dp = datasump = malloc (blocks_used * sizeof(u_long)); 34351866Sbostic *dp++ = ((u_long *)dip)[0]; /* inode block */ 34451866Sbostic for (i = 0; i < lfsp->lfs_cleansz; i++) 34551866Sbostic *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) / 34651866Sbostic sizeof(u_long)]; /* Cleaner info */ 34751866Sbostic for (i = 0; i < lfsp->lfs_segtabsz; i++) 34851866Sbostic *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) / 34951866Sbostic sizeof(u_long)]; /* Segusage table */ 35051866Sbostic *dp++ = ((u_long *)ifile)[0]; /* Ifile */ 35151866Sbostic 35251866Sbostic /* Still need the root and l+f bytes; get them later */ 35351866Sbostic 35451866Sbostic /* Write out the inode block */ 35551866Sbostic off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE; 35651866Sbostic put(fd, off, dpagep, lfsp->lfs_bsize); 35751866Sbostic free(dpagep); 35851866Sbostic off += lfsp->lfs_bsize; 35951866Sbostic 36051866Sbostic /* Write out the ifile */ 36151866Sbostic 36251866Sbostic put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift); 36351866Sbostic off += (lfsp->lfs_cleansz << lfsp->lfs_bshift); 36451866Sbostic (void)free(cleaninfo); 36551866Sbostic 36651866Sbostic put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift); 36751866Sbostic off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift); 36851866Sbostic (void)free(segtable); 36951866Sbostic 37051866Sbostic put(fd, off, ifile, lfsp->lfs_bsize); 37151866Sbostic off += lfsp->lfs_bsize; 37251866Sbostic 37351866Sbostic /* 37451866Sbostic * use ipagep for space for writing out other stuff. It used to 37551866Sbostic * contain the ifile, but we're done with it. 37651866Sbostic */ 37751866Sbostic 37851864Sbostic /* Write out the root and lost and found directories */ 37951866Sbostic bzero(ipagep, lfsp->lfs_bsize); 38051866Sbostic make_dir(ipagep, lfs_root_dir, 38151864Sbostic sizeof(lfs_root_dir) / sizeof(struct direct)); 38251866Sbostic *dp++ = ((u_long *)ipagep)[0]; 38351866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 38451866Sbostic off += lfsp->lfs_bsize; 38551149Sbostic 38651866Sbostic bzero(ipagep, lfsp->lfs_bsize); 38751866Sbostic make_dir(ipagep, lfs_lf_dir, 38851864Sbostic sizeof(lfs_lf_dir) / sizeof(struct direct)); 38951866Sbostic *dp++ = ((u_long *)ipagep)[0]; 39051866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 39151149Sbostic 39251866Sbostic /* Write Supberblock */ 39351866Sbostic lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize; 39451866Sbostic put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs)); 39551149Sbostic 39651866Sbostic /* 39751866Sbostic * Finally, calculate all the fields for the summary structure 39851866Sbostic * and write it. 39951866Sbostic */ 40051866Sbostic 40151864Sbostic summary.ss_next = lfsp->lfs_nextseg; 40251149Sbostic summary.ss_create = lfsp->lfs_tstamp; 40351866Sbostic summary.ss_nfinfo = 3; 40451866Sbostic summary.ss_ninos = 3; 40551866Sbostic summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used); 40651149Sbostic 40751864Sbostic /* 40851864Sbostic * Make sure that we don't overflow a summary block. We have to 40951866Sbostic * record: FINFO structures for ifile, root, and l+f. The number 41051866Sbostic * of blocks recorded for the ifile is determined by the size of 41151866Sbostic * the cleaner info and the segments usage table. There is room 41251866Sbostic * for one block included in sizeof(FINFO) so we don't need to add 41351866Sbostic * any extra space for the ROOT and L+F, and one block of the ifile 41451866Sbostic * is already counted. Finally, we leave room for 1 inode block 41551866Sbostic * address. 41651864Sbostic */ 41753248Sbostic sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) + 41851866Sbostic (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long); 41951149Sbostic #define SUMERR \ 42051149Sbostic "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes." 42151149Sbostic if (sum_size > LFS_SUMMARY_SIZE) 42251149Sbostic fatal(SUMERR, sum_size); 42351149Sbostic 42451866Sbostic block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1; 42551149Sbostic 42651149Sbostic if (!(block_array = malloc(block_array_size *sizeof(int)))) 42751149Sbostic fatal("%s: %s", special, strerror(errno)); 42851149Sbostic 42951149Sbostic /* fill in the array */ 43051866Sbostic for (i = 0; i < block_array_size; i++) 43151149Sbostic block_array[i] = i; 43251149Sbostic 43351149Sbostic /* copy into segment */ 43451866Sbostic sump = ipagep; 43551149Sbostic bcopy(&summary, sump, sizeof(SEGSUM)); 43651149Sbostic sump += sizeof(SEGSUM); 43751149Sbostic 43851149Sbostic /* Now, add the ifile */ 43951866Sbostic file_info.fi_nblocks = block_array_size; 44051149Sbostic file_info.fi_version = 1; 44151149Sbostic file_info.fi_ino = LFS_IFILE_INUM; 44251149Sbostic 44351149Sbostic bcopy(&file_info, sump, sizeof(FINFO) - sizeof(u_long)); 44451149Sbostic sump += sizeof(FINFO) - sizeof(u_long); 44551149Sbostic bcopy(block_array, sump, sizeof(u_long) * file_info.fi_nblocks); 44651864Sbostic sump += sizeof(u_long) * file_info.fi_nblocks; 44751149Sbostic 44851864Sbostic /* Now, add the root directory */ 44951864Sbostic file_info.fi_nblocks = 1; 45051864Sbostic file_info.fi_version = 1; 45151864Sbostic file_info.fi_ino = ROOTINO; 45251864Sbostic file_info.fi_blocks[0] = 0; 45351864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 45451864Sbostic sump += sizeof(FINFO); 45551864Sbostic 45651864Sbostic /* Now, add the lost and found */ 45751864Sbostic file_info.fi_ino = LOSTFOUNDINO; 45851864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 45951864Sbostic 46051866Sbostic ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] = 46151866Sbostic lfsp->lfs_idaddr; 46251866Sbostic ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum), 46351866Sbostic LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum)); 46451866Sbostic put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE); 46551864Sbostic 46651866Sbostic sp = (SEGSUM *)ipagep; 46751866Sbostic sp->ss_create = 0; 46851866Sbostic sp->ss_nfinfo = 0; 46951866Sbostic sp->ss_ninos = 0; 47051866Sbostic sp->ss_datasum = 0; 47151149Sbostic 47251149Sbostic /* Now, write rest of segments containing superblocks */ 47351149Sbostic lfsp->lfs_tstamp = 0; 47451864Sbostic lfsp->lfs_cksum = 47551866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 47651864Sbostic for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1; 47751864Sbostic i < lfsp->lfs_nseg; i++) { 47851149Sbostic 47951864Sbostic seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb; 48051866Sbostic sp->ss_next = last_addr; 48151866Sbostic last_addr = seg_addr; 48251866Sbostic seg_seek = seg_addr * lp->d_secsize; 48351866Sbostic 48451864Sbostic if (seg_addr == lfsp->lfs_sboffs[j]) { 48551864Sbostic if (j < (LFS_MAXNUMSB - 2)) 48651864Sbostic j++; 48751866Sbostic put(fd, seg_seek, lfsp, sizeof(struct lfs)); 48851866Sbostic seg_seek += LFS_SBPAD; 48951866Sbostic } 49051149Sbostic 49151149Sbostic /* Summary */ 49251866Sbostic sp->ss_sumsum = cksum(&sp->ss_datasum, 49351866Sbostic LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 49451866Sbostic put(fd, seg_seek, sp, LFS_SUMMARY_SIZE); 49551149Sbostic } 49651866Sbostic free(ipagep); 49751864Sbostic close(fd); 49851149Sbostic return (0); 49951149Sbostic } 50051149Sbostic 50151149Sbostic static void 50251149Sbostic put(fd, off, p, len) 50351149Sbostic int fd; 50451149Sbostic off_t off; 50551149Sbostic void *p; 50651149Sbostic size_t len; 50751149Sbostic { 50851149Sbostic int wbytes; 50951149Sbostic 51051149Sbostic if (lseek(fd, off, SEEK_SET) < 0) 51151149Sbostic fatal("%s: %s", special, strerror(errno)); 51251149Sbostic if ((wbytes = write(fd, p, len)) < 0) 51351149Sbostic fatal("%s: %s", special, strerror(errno)); 51451149Sbostic if (wbytes != len) 51551149Sbostic fatal("%s: short write (%d, not %d)", special, wbytes, len); 51651149Sbostic } 51751864Sbostic 51851864Sbostic /* 51951864Sbostic * Create the root directory for this file system and the lost+found 52051864Sbostic * directory. 52151864Sbostic */ 52251864Sbostic 52351864Sbostic u_long d_ino; /* inode number of entry */ 52451864Sbostic u_short d_reclen; /* length of this record */ 52551864Sbostic u_short d_namlen; /* length of string in d_name */ 52651864Sbostic char d_name[MAXNAMLEN + 1]; /* name with length <= MAXNAMLEN */ 52751864Sbostic lfsinit() 52851864Sbostic { 52951864Sbostic } 53051864Sbostic 53151864Sbostic static daddr_t 53251864Sbostic make_dinode(ino, dip, nblocks, saddr, lfsp) 53351864Sbostic ino_t ino; /* inode we're creating */ 53451864Sbostic struct dinode *dip; /* disk inode */ 53551864Sbostic int nblocks; /* number of blocks in file */ 53651864Sbostic daddr_t saddr; /* starting block address */ 53751866Sbostic struct lfs *lfsp; /* superblock */ 53851864Sbostic { 53951864Sbostic int db_per_fb, i; 54051864Sbostic 54151864Sbostic dip->di_nlink = 1; 54251864Sbostic dip->di_blocks = nblocks; 54351864Sbostic 54451864Sbostic /* If we ever need something longer than 32 bits, this changes */ 54551864Sbostic dip->di_size = (dip->di_blocks << lfsp->lfs_bshift); 546*54715Sbostic dip->di_atime.ts_sec = dip->di_mtime.ts_sec = 547*54715Sbostic dip->di_ctime.ts_sec = lfsp->lfs_tstamp; 548*54715Sbostic dip->di_atime.ts_nsec = dip->di_mtime.ts_nsec = 549*54715Sbostic dip->di_ctime.ts_nsec = 0; 55051864Sbostic dip->di_inum = ino; 55151864Sbostic 55251864Sbostic #define SEGERR \ 55351864Sbostic "File requires more than the number of direct blocks; increase block or segment size." 55451864Sbostic if (NDADDR < nblocks) 55551864Sbostic fatal("%s", SEGERR); 55651864Sbostic 55751864Sbostic /* Assign the block addresses for the ifile */ 55851864Sbostic db_per_fb = 1 << lfsp->lfs_fsbtodb; 55951864Sbostic for (i = 0; i < dip->di_blocks; i++, saddr += db_per_fb) 56051864Sbostic dip->di_db[i] = saddr; 56151864Sbostic 56251864Sbostic return (saddr); 56351864Sbostic } 56451864Sbostic 56551864Sbostic 56651864Sbostic /* 56751864Sbostic * Construct a set of directory entries in "bufp". We assume that all the 56851864Sbostic * entries in protodir fir in the first DIRBLKSIZ. 56951864Sbostic */ 57051864Sbostic static void 57151864Sbostic make_dir(bufp, protodir, entries) 57251864Sbostic void *bufp; 57351864Sbostic register struct direct *protodir; 57451864Sbostic int entries; 57551864Sbostic { 57651864Sbostic char *cp; 57751864Sbostic int i, spcleft; 57851864Sbostic 57951864Sbostic spcleft = DIRBLKSIZ; 58051864Sbostic for (cp = bufp, i = 0; i < entries - 1; i++) { 581*54715Sbostic protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i]); 58251864Sbostic bcopy(&protodir[i], cp, protodir[i].d_reclen); 58351864Sbostic cp += protodir[i].d_reclen; 58451864Sbostic if ((spcleft -= protodir[i].d_reclen) < 0) 58551864Sbostic fatal("%s: %s", special, "directory too big"); 58651864Sbostic } 58751864Sbostic protodir[i].d_reclen = spcleft; 588*54715Sbostic bcopy(&protodir[i], cp, DIRSIZ(NEWDIRFMT, &protodir[i])); 58951864Sbostic } 590