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*51866Sbostic static char sccsid[] = "@(#)lfs.c 5.3 (Berkeley) 12/06/91"; 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> 19*51866Sbostic 20*51866Sbostic #include <ufs/ufs/dir.h> 21*51866Sbostic #include <ufs/ufs/quota.h> 22*51866Sbostic #include <ufs/ufs/dinode.h> 23*51866Sbostic #include <ufs/ufs/ufsmount.h> 24*51866Sbostic #include <ufs/lfs/lfs.h> 25*51866Sbostic 2651149Sbostic #include <unistd.h> 2751149Sbostic #include <errno.h> 2851149Sbostic #include <stdlib.h> 2951149Sbostic #include <string.h> 3051149Sbostic #include "config.h" 3151149Sbostic #include "extern.h" 3251149Sbostic 33*51866Sbostic static struct lfs lfs_default = { 3451149Sbostic /* lfs_magic */ LFS_MAGIC, 3551149Sbostic /* lfs_version */ LFS_VERSION, 3651149Sbostic /* lfs_size */ 0, 37*51866Sbostic /* lfs_ssize */ DFL_LFSSEG/DFL_LFSBLOCK, 3851149Sbostic /* lfs_dsize */ 0, 3951149Sbostic /* lfs_bsize */ DFL_LFSBLOCK, 4051864Sbostic /* lfs_fsize */ DFL_LFSBLOCK, 4151864Sbostic /* lfs_frag */ 1, 4251149Sbostic /* lfs_free */ LFS_FIRST_INUM, 4351864Sbostic /* lfs_bfree */ 0, 4451864Sbostic /* lfs_nfiles */ 0, 4551149Sbostic /* lfs_idaddr */ 0, 4651149Sbostic /* lfs_ifile */ LFS_IFILE_INUM, 4751149Sbostic /* lfs_lastseg */ 0, 4851864Sbostic /* lfs_nextseg */ 0, 49*51866Sbostic /* lfs_curseg */ 0, 50*51866Sbostic /* lfs_offset */ 0, 5151149Sbostic /* lfs_tstamp */ 0, 5251149Sbostic /* lfs_minfree */ MINFREE, 53*51866Sbostic /* lfs_dbpseg */ DFL_LFSSEG/DEV_BSIZE, 5451149Sbostic /* lfs_inopb */ DFL_LFSBLOCK/sizeof(struct dinode), 5551149Sbostic /* lfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 56*51866Sbostic /* lfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 5751149Sbostic /* lfs_nindir */ DFL_LFSBLOCK/sizeof(daddr_t), 5851149Sbostic /* lfs_nseg */ 0, 5951149Sbostic /* lfs_nspf */ 0, 60*51866Sbostic /* lfs_cleansz */ 0, 6151149Sbostic /* lfs_segtabsz */ 0, 6251149Sbostic /* lfs_segmask */ DFL_LFSSEG_MASK, 6351149Sbostic /* lfs_segshift */ DFL_LFSSEG_SHIFT, 6451149Sbostic /* lfs_bmask */ DFL_LFSBLOCK_MASK, 6551149Sbostic /* lfs_bshift */ DFL_LFSBLOCK_SHIFT, 6651149Sbostic /* lfs_ffmask */ 0, 6751149Sbostic /* lfs_ffshift */ 0, 6851149Sbostic /* lfs_fbmask */ 0, 6951149Sbostic /* lfs_fbshift */ 0, 7051149Sbostic /* lfs_fsbtodb */ 0, 7151149Sbostic /* lfs_sboffs */ { 0 }, 7251864Sbostic /* lfs_ivnode */ NULL, 7351864Sbostic /* lfs_segtab */ NULL, 7451864Sbostic /* lfs_seglist */ NULL, 7551864Sbostic /* lfs_iocount */ 0, 7651149Sbostic /* lfs_fmod */ 0, 7751149Sbostic /* lfs_clean */ 0, 7851149Sbostic /* lfs_ronly */ 0, 7951149Sbostic /* lfs_flags */ 0, 8051149Sbostic /* lfs_fsmnt */ { 0 }, 8151864Sbostic /* lfs_pad */ { 0 }, 8251149Sbostic /* lfs_cksum */ 0 8351149Sbostic }; 8451149Sbostic 8551864Sbostic 8651864Sbostic struct direct lfs_root_dir[] = { 8751864Sbostic { ROOTINO, sizeof(struct direct), 1, "."}, 8851864Sbostic { ROOTINO, sizeof(struct direct), 2, ".."}, 89*51866Sbostic { LFS_IFILE_INUM, sizeof(struct direct), 5, "ifile"}, 9051864Sbostic { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found"}, 9151864Sbostic }; 9251864Sbostic 9351864Sbostic struct direct lfs_lf_dir[] = { 9451864Sbostic { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, 9551864Sbostic { ROOTINO, sizeof(struct direct), 2, ".." }, 9651864Sbostic }; 9751864Sbostic 9851864Sbostic static daddr_t make_dinode 99*51866Sbostic __P((ino_t, struct dinode *, int, daddr_t, struct lfs *)); 10051864Sbostic static void make_dir __P(( void *, struct direct *, int)); 10151149Sbostic static void put __P((int, off_t, void *, size_t)); 10251149Sbostic 10351149Sbostic int 10451149Sbostic make_lfs(fd, lp, partp, minfree, block_size, seg_size) 10551149Sbostic int fd; 10651149Sbostic struct disklabel *lp; 10751149Sbostic struct partition *partp; 10851149Sbostic int minfree; 10951149Sbostic int block_size; 11051149Sbostic int seg_size; 11151149Sbostic { 11251149Sbostic struct dinode *dip; /* Pointer to a disk inode */ 11351864Sbostic struct dinode *dpagep; /* Pointer to page of disk inodes */ 114*51866Sbostic CLEANERINFO *cleaninfo; /* Segment cleaner information table */ 11551149Sbostic FINFO file_info; /* File info structure in summary blocks */ 11651149Sbostic IFILE *ifile; /* Pointer to array of ifile structures */ 11751149Sbostic IFILE *ip; /* Pointer to array of ifile structures */ 118*51866Sbostic struct lfs *lfsp; /* Superblock */ 11951864Sbostic SEGUSE *segp; /* Segment usage table */ 12051864Sbostic SEGUSE *segtable; /* Segment usage table */ 12151149Sbostic SEGSUM summary; /* Segment summary structure */ 122*51866Sbostic SEGSUM *sp; /* Segment summary pointer */ 12351149Sbostic daddr_t last_sb_addr; /* Address of superblocks */ 12451864Sbostic daddr_t last_addr; /* Previous segment address */ 12551149Sbostic daddr_t sb_addr; /* Address of superblocks */ 12651864Sbostic daddr_t seg_addr; /* Address of current segment */ 127*51866Sbostic void *ipagep; /* Pointer to the page we use to write stuff */ 12851149Sbostic void *sump; /* Used to copy stuff into segment buffer */ 12951149Sbostic u_long *block_array; /* Array of logical block nos to put in sum */ 130*51866Sbostic u_long blocks_used; /* Number of blocks in first segment */ 131*51866Sbostic u_long *dp; /* Used to computed checksum on data */ 132*51866Sbostic u_long *datasump; /* Used to computed checksum on data */ 13351149Sbostic int block_array_size; /* How many entries in block array */ 13451149Sbostic int bsize; /* Block size */ 13551149Sbostic int db_per_fb; /* Disk blocks per file block */ 13651864Sbostic int i, j; 137*51866Sbostic int off; /* Offset at which to write */ 13851149Sbostic int sb_to_sum; /* offset between superblock and summary */ 13951149Sbostic int sb_interval; /* number of segs between super blocks */ 14051149Sbostic int seg_seek; /* Seek offset for a segment */ 14151149Sbostic int ssize; /* Segment size */ 14251149Sbostic int sum_size; /* Size of the summary block */ 14351149Sbostic int wbytes; /* Number of bytes returned by write */ 14451149Sbostic 14551149Sbostic lfsp = &lfs_default; 14651149Sbostic 14751149Sbostic if (!(bsize = block_size)) 14851149Sbostic bsize = DFL_LFSBLOCK; 14951149Sbostic if (!(ssize = seg_size)) 15051149Sbostic ssize = DFL_LFSSEG; 15151149Sbostic 15251149Sbostic /* Modify parts of superblock overridden by command line arguments */ 15351149Sbostic if (bsize != DFL_LFSBLOCK) { 15451149Sbostic lfsp->lfs_bshift = log2(bsize); 15551149Sbostic if (1 << lfsp->lfs_bshift != bsize) 15651149Sbostic fatal("%d: block size not a power of 2", bsize); 15751149Sbostic lfsp->lfs_bsize = bsize; 15851864Sbostic lfsp->lfs_fsize = bsize; 15951149Sbostic lfsp->lfs_bmask = bsize - 1; 16051149Sbostic lfsp->lfs_inopb = bsize / sizeof(struct dinode); 16151149Sbostic /* MIS -- should I round to power of 2 */ 16251149Sbostic lfsp->lfs_ifpb = bsize / sizeof(IFILE); 163*51866Sbostic lfsp->lfs_sepb = bsize / sizeof(SEGUSE); 16451149Sbostic lfsp->lfs_nindir = bsize / sizeof(daddr_t); 16551149Sbostic } 16651149Sbostic 16751149Sbostic if (ssize != DFL_LFSSEG) { 16851149Sbostic lfsp->lfs_segshift = log2(ssize); 16951149Sbostic if (1 << lfsp->lfs_segshift != ssize) 17051149Sbostic fatal("%d: segment size not power of 2", ssize); 17151149Sbostic lfsp->lfs_ssize = ssize; 17251149Sbostic lfsp->lfs_segmask = ssize - 1; 17351149Sbostic } 17451149Sbostic lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift; 17551149Sbostic 17651149Sbostic if (minfree) 17751149Sbostic lfsp->lfs_minfree = minfree; 17851149Sbostic 17951149Sbostic /* 18051149Sbostic * Fill in parts of superblock that can be computed from file system 18151149Sbostic * size, disk geometry and current time. 18251149Sbostic */ 18351149Sbostic db_per_fb = bsize/lp->d_secsize; 18451149Sbostic lfsp->lfs_fsbtodb = log2(db_per_fb); 18551149Sbostic lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb; 18651149Sbostic lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift); 18751149Sbostic lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize; 18851864Sbostic 18951864Sbostic /* 19051864Sbostic * The number of free blocks is set from the total data size (lfs_dsize) 19151864Sbostic * minus one block for each segment (for the segment summary). Then 192*51866Sbostic * we'll subtract off the room for the superblocks, ifile entries and 193*51866Sbostic * segment usage table. 19451864Sbostic */ 19551864Sbostic lfsp->lfs_bfree = lfsp->lfs_dsize - lfsp->lfs_nseg; 19651149Sbostic lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp); 197*51866Sbostic lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp); 19851149Sbostic if ((lfsp->lfs_tstamp = time(NULL)) == -1) 19951149Sbostic fatal("time: %s", strerror(errno)); 20051149Sbostic if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 20151149Sbostic sb_interval = LFS_MIN_SBINTERVAL; 20251149Sbostic 20351149Sbostic /* 20451149Sbostic * Now, lay out the file system. We need to figure out where 20551149Sbostic * the superblocks go, initialize the checkpoint information 20651149Sbostic * for the first two superblocks, initialize the segment usage 20751149Sbostic * information, put the segusage information in the ifile, create 20851149Sbostic * the first block of IFILE structures, and link all the IFILE 20951149Sbostic * structures into a free list. 21051149Sbostic */ 21151149Sbostic 21251149Sbostic /* Figure out where the superblocks are going to live */ 21351149Sbostic lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize; 21451149Sbostic for (i = 1; i < LFS_MAXNUMSB; i++) { 21551149Sbostic sb_addr = ((i * sb_interval) << 21651149Sbostic (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb)) 21751149Sbostic + lfsp->lfs_sboffs[0]; 21851149Sbostic if (sb_addr > partp->p_size) 21951149Sbostic break; 22051149Sbostic lfsp->lfs_sboffs[i] = sb_addr; 22151149Sbostic } 22251149Sbostic last_sb_addr = lfsp->lfs_sboffs[i - 1]; 22351149Sbostic lfsp->lfs_lastseg = lfsp->lfs_sboffs[0]; 22451864Sbostic lfsp->lfs_nextseg = 22551864Sbostic lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0]; 226*51866Sbostic lfsp->lfs_curseg = lfsp->lfs_lastseg; 22751149Sbostic 22851149Sbostic /* 22951149Sbostic * Initialize the segment usage table. The first segment will 230*51866Sbostic * contain the superblock, the cleanerinfo (cleansz), the segusage 231*51866Sbostic * table * (segtabsz), 1 block's worth of IFILE entries, the root 232*51866Sbostic * directory, the lost+found directory and one block's worth of 233*51866Sbostic * inodes (containing the ifile, root, and l+f inodes). 23451149Sbostic */ 235*51866Sbostic if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift))) 236*51866Sbostic fatal("%s", strerror(errno)); 237*51866Sbostic /* XXX When Carl has defined the cleanerinfo, initialize it here */ 23851149Sbostic if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift))) 23951149Sbostic fatal("%s", strerror(errno)); 24051149Sbostic segp = segtable; 241*51866Sbostic blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4; 242*51866Sbostic segp->su_nbytes = LFS_SBPAD + (blocks_used << lfsp->lfs_bshift); 24351149Sbostic segp->su_lastmod = lfsp->lfs_tstamp; 24451864Sbostic segp->su_flags = SEGUSE_DIRTY; 245*51866Sbostic lfsp->lfs_bfree -= (lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4); 24651149Sbostic 247*51866Sbostic /* 248*51866Sbostic * Now figure out the address of the ifile inode. The inode block 249*51866Sbostic * appears immediately after the segment summary. 250*51866Sbostic */ 251*51866Sbostic lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) / 252*51866Sbostic lp->d_secsize; 25351149Sbostic 25451149Sbostic for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) { 25551864Sbostic if ((i % sb_interval) == 0) { 25651149Sbostic segp->su_nbytes = LFS_SBPAD; 25751864Sbostic lfsp->lfs_bfree -= (LFS_SBPAD >> lfsp->lfs_bshift); 25851864Sbostic } else 25951149Sbostic segp->su_nbytes = 0; 26051149Sbostic segp->su_lastmod = 0; 26151149Sbostic segp->su_flags = 0; 26251149Sbostic } 26351149Sbostic 26451149Sbostic /* 26551149Sbostic * Ready to start writing segments. The first segment is different 26651149Sbostic * because it contains the segment usage table and the ifile inode 26751864Sbostic * as well as a superblock. For the rest of the segments, set the 26851864Sbostic * time stamp to be 0 so that the first segment is the most recent. 26951864Sbostic * For each segment that is supposed to contain a copy of the super 27051864Sbostic * block, initialize its first few blocks and its segment summary 27151864Sbostic * to indicate this. 27251149Sbostic */ 27351864Sbostic lfsp->lfs_nfiles = LFS_FIRST_INUM - 1; 27451149Sbostic lfsp->lfs_cksum = 275*51866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 27651149Sbostic 27751864Sbostic /* Now create a block of disk inodes */ 27851864Sbostic if (!(dpagep = malloc(lfsp->lfs_bsize))) 27951864Sbostic fatal("%s", strerror(errno)); 28051864Sbostic dip = (struct dinode *)dpagep; 28151864Sbostic bzero(dip, lfsp->lfs_bsize); 28251864Sbostic 283*51866Sbostic /* Create a block of IFILE structures. */ 284*51866Sbostic if (!(ipagep = malloc(lfsp->lfs_bsize))) 28551149Sbostic fatal("%s", strerror(errno)); 286*51866Sbostic ifile = (IFILE *)ipagep; 28751864Sbostic 288*51866Sbostic /* 289*51866Sbostic * Initialize IFILE. It is the next block following the 290*51866Sbostic * block of inodes (whose address has been calculated in 291*51866Sbostic * lfsp->lfs_idaddr; 292*51866Sbostic */ 293*51866Sbostic sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize; 294*51866Sbostic sb_addr = make_dinode(LFS_IFILE_INUM, dip, 295*51866Sbostic lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp); 29651864Sbostic dip->di_mode = IFREG|IREAD|IWRITE; 29751864Sbostic ip = &ifile[LFS_IFILE_INUM]; 29851864Sbostic ip->if_version = 1; 29951864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 30051864Sbostic ip->if_st_atime = lfsp->lfs_tstamp; 30151864Sbostic 30251864Sbostic /* Initialize the ROOT Directory */ 30351864Sbostic sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp); 30451864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 30551864Sbostic dip->di_size = DIRBLKSIZ; 30651864Sbostic dip->di_nlink = 3; 30751864Sbostic ip = &ifile[ROOTINO]; 30851864Sbostic ip->if_version = 1; 30951864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 31051864Sbostic ip->if_st_atime = lfsp->lfs_tstamp; 31151864Sbostic 31251864Sbostic /* Initialize the lost+found Directory */ 31351864Sbostic sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp); 31451864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 31551864Sbostic dip->di_size = DIRBLKSIZ; 31651864Sbostic dip->di_nlink = 2; 31751864Sbostic ip = &ifile[LOSTFOUNDINO]; 31851864Sbostic ip->if_version = 1; 31951864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 32051864Sbostic ip->if_st_atime = lfsp->lfs_tstamp; 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 336*51866Sbostic /* Now, write the segment */ 33751149Sbostic 338*51866Sbostic /* Compute a checksum across all the data you're writing */ 339*51866Sbostic dp = datasump = malloc (blocks_used * sizeof(u_long)); 340*51866Sbostic *dp++ = ((u_long *)dip)[0]; /* inode block */ 341*51866Sbostic for (i = 0; i < lfsp->lfs_cleansz; i++) 342*51866Sbostic *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) / 343*51866Sbostic sizeof(u_long)]; /* Cleaner info */ 344*51866Sbostic for (i = 0; i < lfsp->lfs_segtabsz; i++) 345*51866Sbostic *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) / 346*51866Sbostic sizeof(u_long)]; /* Segusage table */ 347*51866Sbostic *dp++ = ((u_long *)ifile)[0]; /* Ifile */ 348*51866Sbostic 349*51866Sbostic /* Still need the root and l+f bytes; get them later */ 350*51866Sbostic 351*51866Sbostic /* Write out the inode block */ 352*51866Sbostic off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE; 353*51866Sbostic put(fd, off, dpagep, lfsp->lfs_bsize); 354*51866Sbostic free(dpagep); 355*51866Sbostic off += lfsp->lfs_bsize; 356*51866Sbostic 357*51866Sbostic /* Write out the ifile */ 358*51866Sbostic 359*51866Sbostic put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift); 360*51866Sbostic off += (lfsp->lfs_cleansz << lfsp->lfs_bshift); 361*51866Sbostic (void)free(cleaninfo); 362*51866Sbostic 363*51866Sbostic put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift); 364*51866Sbostic off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift); 365*51866Sbostic (void)free(segtable); 366*51866Sbostic 367*51866Sbostic put(fd, off, ifile, lfsp->lfs_bsize); 368*51866Sbostic off += lfsp->lfs_bsize; 369*51866Sbostic 370*51866Sbostic /* 371*51866Sbostic * use ipagep for space for writing out other stuff. It used to 372*51866Sbostic * contain the ifile, but we're done with it. 373*51866Sbostic */ 374*51866Sbostic 37551864Sbostic /* Write out the root and lost and found directories */ 376*51866Sbostic bzero(ipagep, lfsp->lfs_bsize); 377*51866Sbostic make_dir(ipagep, lfs_root_dir, 37851864Sbostic sizeof(lfs_root_dir) / sizeof(struct direct)); 379*51866Sbostic *dp++ = ((u_long *)ipagep)[0]; 380*51866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 381*51866Sbostic off += lfsp->lfs_bsize; 38251149Sbostic 383*51866Sbostic bzero(ipagep, lfsp->lfs_bsize); 384*51866Sbostic make_dir(ipagep, lfs_lf_dir, 38551864Sbostic sizeof(lfs_lf_dir) / sizeof(struct direct)); 386*51866Sbostic *dp++ = ((u_long *)ipagep)[0]; 387*51866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 38851149Sbostic 389*51866Sbostic /* Write Supberblock */ 390*51866Sbostic lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize; 391*51866Sbostic put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs)); 39251149Sbostic 393*51866Sbostic /* 394*51866Sbostic * Finally, calculate all the fields for the summary structure 395*51866Sbostic * and write it. 396*51866Sbostic */ 397*51866Sbostic 39851864Sbostic summary.ss_next = lfsp->lfs_nextseg; 39951149Sbostic summary.ss_create = lfsp->lfs_tstamp; 400*51866Sbostic summary.ss_nfinfo = 3; 401*51866Sbostic summary.ss_ninos = 3; 402*51866Sbostic 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 406*51866Sbostic * record: FINFO structures for ifile, root, and l+f. The number 407*51866Sbostic * of blocks recorded for the ifile is determined by the size of 408*51866Sbostic * the cleaner info and the segments usage table. There is room 409*51866Sbostic * for one block included in sizeof(FINFO) so we don't need to add 410*51866Sbostic * any extra space for the ROOT and L+F, and one block of the ifile 411*51866Sbostic * is already counted. Finally, we leave room for 1 inode block 412*51866Sbostic * address. 41351864Sbostic */ 414*51866Sbostic sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t); 415*51866Sbostic (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 421*51866Sbostic 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 */ 427*51866Sbostic for (i = 0; i < block_array_size; i++) 42851149Sbostic block_array[i] = i; 42951149Sbostic 43051149Sbostic /* copy into segment */ 431*51866Sbostic sump = ipagep; 43251149Sbostic bcopy(&summary, sump, sizeof(SEGSUM)); 43351149Sbostic sump += sizeof(SEGSUM); 43451149Sbostic 43551149Sbostic /* Now, add the ifile */ 436*51866Sbostic 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 457*51866Sbostic ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] = 458*51866Sbostic lfsp->lfs_idaddr; 459*51866Sbostic ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum), 460*51866Sbostic LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum)); 461*51866Sbostic put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE); 46251864Sbostic 463*51866Sbostic sp = (SEGSUM *)ipagep; 464*51866Sbostic sp->ss_create = 0; 465*51866Sbostic sp->ss_nfinfo = 0; 466*51866Sbostic sp->ss_ninos = 0; 467*51866Sbostic sp->ss_datasum = 0; 46851149Sbostic 46951149Sbostic /* Now, write rest of segments containing superblocks */ 47051149Sbostic lfsp->lfs_tstamp = 0; 47151864Sbostic lfsp->lfs_cksum = 472*51866Sbostic 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; 477*51866Sbostic sp->ss_next = last_addr; 478*51866Sbostic last_addr = seg_addr; 479*51866Sbostic seg_seek = seg_addr * lp->d_secsize; 480*51866Sbostic 48151864Sbostic if (seg_addr == lfsp->lfs_sboffs[j]) { 48251864Sbostic if (j < (LFS_MAXNUMSB - 2)) 48351864Sbostic j++; 484*51866Sbostic put(fd, seg_seek, lfsp, sizeof(struct lfs)); 485*51866Sbostic seg_seek += LFS_SBPAD; 486*51866Sbostic } 48751149Sbostic 48851149Sbostic /* Summary */ 489*51866Sbostic sp->ss_sumsum = cksum(&sp->ss_datasum, 490*51866Sbostic LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 491*51866Sbostic put(fd, seg_seek, sp, LFS_SUMMARY_SIZE); 49251149Sbostic } 493*51866Sbostic 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 */ 534*51866Sbostic 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