151149Sbostic /*- 2*61524Sbostic * Copyright (c) 1991, 1993 3*61524Sbostic * The Regents of the University of California. All rights reserved. 451149Sbostic * 551149Sbostic * %sccs.include.redist.c% 651149Sbostic */ 751149Sbostic 851149Sbostic #ifndef lint 9*61524Sbostic static char sccsid[] = "@(#)lfs.c 8.1 (Berkeley) 06/05/93"; 1051149Sbostic #endif /* not lint */ 1151149Sbostic 1251149Sbostic #include <sys/param.h> 1351149Sbostic #include <sys/disklabel.h> 1451864Sbostic #include <sys/time.h> 1552205Sbostic #include <sys/mount.h> 1651866Sbostic 1751866Sbostic #include <ufs/ufs/dir.h> 1851866Sbostic #include <ufs/ufs/quota.h> 1951866Sbostic #include <ufs/ufs/dinode.h> 2051866Sbostic #include <ufs/lfs/lfs.h> 2151866Sbostic 2251149Sbostic #include <unistd.h> 2351149Sbostic #include <errno.h> 2451149Sbostic #include <stdlib.h> 2551149Sbostic #include <string.h> 2651149Sbostic #include "config.h" 2751149Sbostic #include "extern.h" 2851149Sbostic 2955389Sbostic /* 3055389Sbostic * This table is indexed by the log base 2 of the block size. 3155389Sbostic * It returns the maximum file size allowed in a file system 3255389Sbostic * with the specified block size. For block sizes smaller than 3355389Sbostic * 8K, the size is limited by tha maximum number of blocks that 3455389Sbostic * can be reached by triply indirect blocks: 3555389Sbostic * NDADDR + INOPB(bsize) + INOPB(bsize)^2 + INOPB(bsize)^3 3655389Sbostic * For block size of 8K or larger, the file size is limited by the 3755389Sbostic * number of blocks that can be represented in the file system. Since 3855389Sbostic * we use negative block numbers to represent indirect blocks, we can 3955389Sbostic * have a maximum of 2^31 blocks. 4055389Sbostic */ 4155389Sbostic 4255389Sbostic u_quad_t maxtable[] = { 4355389Sbostic /* 1 */ -1, 4455389Sbostic /* 2 */ -1, 4555389Sbostic /* 4 */ -1, 4655389Sbostic /* 8 */ -1, 4755389Sbostic /* 16 */ -1, 4855389Sbostic /* 32 */ -1, 4955389Sbostic /* 64 */ -1, 5055389Sbostic /* 128 */ -1, 5155389Sbostic /* 256 */ -1, 5255389Sbostic /* 512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128, 5355389Sbostic /* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256, 5455389Sbostic /* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512, 5555389Sbostic /* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024, 5656891Sbostic /* 8192 */ 1 << 31, 5756891Sbostic /* 16 K */ 1 << 31, 5856891Sbostic /* 32 K */ 1 << 31, 5955389Sbostic }; 6055389Sbostic 6151866Sbostic static struct lfs lfs_default = { 6251149Sbostic /* lfs_magic */ LFS_MAGIC, 6351149Sbostic /* lfs_version */ LFS_VERSION, 6451149Sbostic /* lfs_size */ 0, 6551866Sbostic /* lfs_ssize */ DFL_LFSSEG/DFL_LFSBLOCK, 6651149Sbostic /* lfs_dsize */ 0, 6751149Sbostic /* lfs_bsize */ DFL_LFSBLOCK, 6851864Sbostic /* lfs_fsize */ DFL_LFSBLOCK, 6951864Sbostic /* lfs_frag */ 1, 7051149Sbostic /* lfs_free */ LFS_FIRST_INUM, 7151864Sbostic /* lfs_bfree */ 0, 7251864Sbostic /* lfs_nfiles */ 0, 7356031Sbostic /* lfs_avail */ 0, 7456031Sbostic /* lfs_uinodes */ 0, 7551149Sbostic /* lfs_idaddr */ 0, 7651149Sbostic /* lfs_ifile */ LFS_IFILE_INUM, 7751149Sbostic /* lfs_lastseg */ 0, 7851864Sbostic /* lfs_nextseg */ 0, 7951866Sbostic /* lfs_curseg */ 0, 8051866Sbostic /* lfs_offset */ 0, 8154263Sbostic /* lfs_lastpseg */ 0, 8251149Sbostic /* lfs_tstamp */ 0, 8351149Sbostic /* lfs_minfree */ MINFREE, 8455389Sbostic /* lfs_maxfilesize */ 0, 8551866Sbostic /* lfs_dbpseg */ DFL_LFSSEG/DEV_BSIZE, 8651149Sbostic /* lfs_inopb */ DFL_LFSBLOCK/sizeof(struct dinode), 8751149Sbostic /* lfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 8851866Sbostic /* lfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 8951149Sbostic /* lfs_nindir */ DFL_LFSBLOCK/sizeof(daddr_t), 9051149Sbostic /* lfs_nseg */ 0, 9151149Sbostic /* lfs_nspf */ 0, 9251866Sbostic /* lfs_cleansz */ 0, 9351149Sbostic /* lfs_segtabsz */ 0, 9451149Sbostic /* lfs_segmask */ DFL_LFSSEG_MASK, 9551149Sbostic /* lfs_segshift */ DFL_LFSSEG_SHIFT, 9651149Sbostic /* lfs_bmask */ DFL_LFSBLOCK_MASK, 9751149Sbostic /* lfs_bshift */ DFL_LFSBLOCK_SHIFT, 9851149Sbostic /* lfs_ffmask */ 0, 9951149Sbostic /* lfs_ffshift */ 0, 10051149Sbostic /* lfs_fbmask */ 0, 10151149Sbostic /* lfs_fbshift */ 0, 10251149Sbostic /* lfs_fsbtodb */ 0, 10355596Sbostic /* lfs_sushift */ 0, 10451149Sbostic /* lfs_sboffs */ { 0 }, 10556861Smargo /* lfs_sp */ NULL, 10651864Sbostic /* lfs_ivnode */ NULL, 10754710Sbostic /* lfs_seglock */ 0, 10856863Smargo /* lfs_lockpid */ 0, 10951864Sbostic /* lfs_iocount */ 0, 11054263Sbostic /* lfs_writer */ 0, 11154263Sbostic /* lfs_dirops */ 0, 11254263Sbostic /* lfs_doifile */ 0, 11356031Sbostic /* lfs_nactive */ 0, 11451149Sbostic /* lfs_fmod */ 0, 11551149Sbostic /* lfs_clean */ 0, 11651149Sbostic /* lfs_ronly */ 0, 11751149Sbostic /* lfs_flags */ 0, 11851149Sbostic /* lfs_fsmnt */ { 0 }, 11951864Sbostic /* lfs_pad */ { 0 }, 12051149Sbostic /* lfs_cksum */ 0 12151149Sbostic }; 12251149Sbostic 12351864Sbostic 12451864Sbostic struct direct lfs_root_dir[] = { 12554715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 1, "."}, 12654715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."}, 12754715Sbostic { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"}, 12854715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"}, 12951864Sbostic }; 13051864Sbostic 13151864Sbostic struct direct lfs_lf_dir[] = { 13254715Sbostic { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 13354715Sbostic { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 13451864Sbostic }; 13551864Sbostic 13651864Sbostic static daddr_t make_dinode 13751866Sbostic __P((ino_t, struct dinode *, int, daddr_t, struct lfs *)); 13851864Sbostic static void make_dir __P(( void *, struct direct *, int)); 13951149Sbostic static void put __P((int, off_t, void *, size_t)); 14051149Sbostic 14151149Sbostic int 14251149Sbostic make_lfs(fd, lp, partp, minfree, block_size, seg_size) 14351149Sbostic int fd; 14451149Sbostic struct disklabel *lp; 14551149Sbostic struct partition *partp; 14651149Sbostic int minfree; 14751149Sbostic int block_size; 14851149Sbostic int seg_size; 14951149Sbostic { 15051149Sbostic struct dinode *dip; /* Pointer to a disk inode */ 15151864Sbostic struct dinode *dpagep; /* Pointer to page of disk inodes */ 15251866Sbostic CLEANERINFO *cleaninfo; /* Segment cleaner information table */ 15351149Sbostic FINFO file_info; /* File info structure in summary blocks */ 15451149Sbostic IFILE *ifile; /* Pointer to array of ifile structures */ 15551149Sbostic IFILE *ip; /* Pointer to array of ifile structures */ 15651866Sbostic struct lfs *lfsp; /* Superblock */ 15751864Sbostic SEGUSE *segp; /* Segment usage table */ 15851864Sbostic SEGUSE *segtable; /* Segment usage table */ 15951149Sbostic SEGSUM summary; /* Segment summary structure */ 16051866Sbostic SEGSUM *sp; /* Segment summary pointer */ 16151149Sbostic daddr_t last_sb_addr; /* Address of superblocks */ 16251864Sbostic daddr_t last_addr; /* Previous segment address */ 16351149Sbostic daddr_t sb_addr; /* Address of superblocks */ 16451864Sbostic daddr_t seg_addr; /* Address of current segment */ 16551866Sbostic void *ipagep; /* Pointer to the page we use to write stuff */ 16651149Sbostic void *sump; /* Used to copy stuff into segment buffer */ 16751149Sbostic u_long *block_array; /* Array of logical block nos to put in sum */ 16851866Sbostic u_long blocks_used; /* Number of blocks in first segment */ 16951866Sbostic u_long *dp; /* Used to computed checksum on data */ 17051866Sbostic u_long *datasump; /* Used to computed checksum on data */ 17151149Sbostic int block_array_size; /* How many entries in block array */ 17251149Sbostic int bsize; /* Block size */ 17351149Sbostic int db_per_fb; /* Disk blocks per file block */ 17451864Sbostic int i, j; 17551866Sbostic int off; /* Offset at which to write */ 17651149Sbostic int sb_interval; /* number of segs between super blocks */ 17751149Sbostic int seg_seek; /* Seek offset for a segment */ 17851149Sbostic int ssize; /* Segment size */ 17951149Sbostic int sum_size; /* Size of the summary block */ 18051149Sbostic 18151149Sbostic lfsp = &lfs_default; 18251149Sbostic 18351149Sbostic if (!(bsize = block_size)) 18451149Sbostic bsize = DFL_LFSBLOCK; 18551149Sbostic if (!(ssize = seg_size)) 18651149Sbostic ssize = DFL_LFSSEG; 18751149Sbostic 18851149Sbostic /* Modify parts of superblock overridden by command line arguments */ 18951149Sbostic if (bsize != DFL_LFSBLOCK) { 19051149Sbostic lfsp->lfs_bshift = log2(bsize); 19151149Sbostic if (1 << lfsp->lfs_bshift != bsize) 19251149Sbostic fatal("%d: block size not a power of 2", bsize); 19351149Sbostic lfsp->lfs_bsize = bsize; 19451864Sbostic lfsp->lfs_fsize = bsize; 19551149Sbostic lfsp->lfs_bmask = bsize - 1; 19651149Sbostic lfsp->lfs_inopb = bsize / sizeof(struct dinode); 19751149Sbostic /* MIS -- should I round to power of 2 */ 19851149Sbostic lfsp->lfs_ifpb = bsize / sizeof(IFILE); 19951866Sbostic lfsp->lfs_sepb = bsize / sizeof(SEGUSE); 20051149Sbostic lfsp->lfs_nindir = bsize / sizeof(daddr_t); 20151149Sbostic } 20251149Sbostic 20351149Sbostic if (ssize != DFL_LFSSEG) { 20451149Sbostic lfsp->lfs_segshift = log2(ssize); 20551149Sbostic if (1 << lfsp->lfs_segshift != ssize) 20651149Sbostic fatal("%d: segment size not power of 2", ssize); 20751149Sbostic lfsp->lfs_ssize = ssize; 20851149Sbostic lfsp->lfs_segmask = ssize - 1; 20956861Smargo lfsp->lfs_dbpseg = ssize / DEV_BSIZE; 21051149Sbostic } 21151149Sbostic lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift; 21251149Sbostic 21351149Sbostic if (minfree) 21451149Sbostic lfsp->lfs_minfree = minfree; 21551149Sbostic 21651149Sbostic /* 21751149Sbostic * Fill in parts of superblock that can be computed from file system 21851149Sbostic * size, disk geometry and current time. 21951149Sbostic */ 22051149Sbostic db_per_fb = bsize/lp->d_secsize; 22151149Sbostic lfsp->lfs_fsbtodb = log2(db_per_fb); 22255596Sbostic lfsp->lfs_sushift = log2(lfsp->lfs_sepb); 22351149Sbostic lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb; 22451149Sbostic lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift); 22551149Sbostic lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize; 22655389Sbostic lfsp->lfs_maxfilesize = maxtable[lfsp->lfs_bshift] << lfsp->lfs_bshift; 22751864Sbostic 22851864Sbostic /* 22956031Sbostic * The number of free blocks is set from the number of segments times 23056192Smargo * the segment size - 2 (that we never write because we need to make 23156192Smargo * sure the cleaner can run). Then we'll subtract off the room for the 23256031Sbostic * superblocks ifile entries and segment usage table. 23351864Sbostic */ 23456192Smargo lfsp->lfs_dsize = fsbtodb(lfsp, (lfsp->lfs_nseg - 2) * lfsp->lfs_ssize); 23556192Smargo lfsp->lfs_bfree = lfsp->lfs_dsize; 23651149Sbostic lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp); 23751866Sbostic lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp); 23851149Sbostic if ((lfsp->lfs_tstamp = time(NULL)) == -1) 23951149Sbostic fatal("time: %s", strerror(errno)); 24051149Sbostic if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 24151149Sbostic sb_interval = LFS_MIN_SBINTERVAL; 24251149Sbostic 24351149Sbostic /* 24451149Sbostic * Now, lay out the file system. We need to figure out where 24551149Sbostic * the superblocks go, initialize the checkpoint information 24651149Sbostic * for the first two superblocks, initialize the segment usage 24751149Sbostic * information, put the segusage information in the ifile, create 24851149Sbostic * the first block of IFILE structures, and link all the IFILE 24951149Sbostic * structures into a free list. 25051149Sbostic */ 25151149Sbostic 25251149Sbostic /* Figure out where the superblocks are going to live */ 25351149Sbostic lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize; 25451149Sbostic for (i = 1; i < LFS_MAXNUMSB; i++) { 25551149Sbostic sb_addr = ((i * sb_interval) << 25651149Sbostic (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb)) 25751149Sbostic + lfsp->lfs_sboffs[0]; 25851149Sbostic if (sb_addr > partp->p_size) 25951149Sbostic break; 26051149Sbostic lfsp->lfs_sboffs[i] = sb_addr; 26151149Sbostic } 26251149Sbostic last_sb_addr = lfsp->lfs_sboffs[i - 1]; 26351149Sbostic lfsp->lfs_lastseg = lfsp->lfs_sboffs[0]; 26451864Sbostic lfsp->lfs_nextseg = 26551864Sbostic lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0]; 26651866Sbostic lfsp->lfs_curseg = lfsp->lfs_lastseg; 26751149Sbostic 26851149Sbostic /* 26951149Sbostic * Initialize the segment usage table. The first segment will 27051866Sbostic * contain the superblock, the cleanerinfo (cleansz), the segusage 27151866Sbostic * table * (segtabsz), 1 block's worth of IFILE entries, the root 27251866Sbostic * directory, the lost+found directory and one block's worth of 27351866Sbostic * inodes (containing the ifile, root, and l+f inodes). 27451149Sbostic */ 27551866Sbostic if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift))) 27651866Sbostic fatal("%s", strerror(errno)); 27751930Sbostic cleaninfo->clean = lfsp->lfs_nseg - 1; 27851930Sbostic cleaninfo->dirty = 1; 27951930Sbostic 28051149Sbostic if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift))) 28151149Sbostic fatal("%s", strerror(errno)); 28251149Sbostic segp = segtable; 28351866Sbostic blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4; 28456031Sbostic segp->su_nbytes = ((blocks_used - 1) << lfsp->lfs_bshift) + 28556031Sbostic 3 * sizeof(struct dinode) + LFS_SUMMARY_SIZE; 28651149Sbostic segp->su_lastmod = lfsp->lfs_tstamp; 28755596Sbostic segp->su_nsums = 1; /* 1 summary blocks */ 28855596Sbostic segp->su_ninos = 1; /* 1 inode block */ 28951930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY; 29056031Sbostic lfsp->lfs_bfree -= LFS_SUMMARY_SIZE / lp->d_secsize; 29156031Sbostic lfsp->lfs_bfree -= 29256031Sbostic fsbtodb(lfsp, lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4); 29351149Sbostic 29451866Sbostic /* 29551866Sbostic * Now figure out the address of the ifile inode. The inode block 29651866Sbostic * appears immediately after the segment summary. 29751866Sbostic */ 29851866Sbostic lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) / 29951866Sbostic lp->d_secsize; 30051149Sbostic 30151149Sbostic for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) { 30251864Sbostic if ((i % sb_interval) == 0) { 30351930Sbostic segp->su_flags = SEGUSE_SUPERBLOCK; 30455596Sbostic lfsp->lfs_bfree -= (LFS_SBPAD / lp->d_secsize); 30552088Sbostic } else 30651930Sbostic segp->su_flags = 0; 30751149Sbostic segp->su_lastmod = 0; 30852088Sbostic segp->su_nbytes = 0; 30955596Sbostic segp->su_ninos = 0; 31055713Sbostic segp->su_nsums = 0; 31151149Sbostic } 31251149Sbostic 31356031Sbostic /* 31456031Sbostic * Initialize dynamic accounting. The blocks available for 31556031Sbostic * writing are the bfree blocks minus 1 segment summary for 31656031Sbostic * each segment since you can't write any new data without 31756031Sbostic * creating a segment summary - 2 segments that the cleaner 31856031Sbostic * needs. 31956031Sbostic */ 32056031Sbostic lfsp->lfs_avail = lfsp->lfs_bfree - lfsp->lfs_nseg - 32156031Sbostic fsbtodb(lfsp, 2 * lfsp->lfs_ssize); 32256031Sbostic lfsp->lfs_uinodes = 0; 32351149Sbostic /* 32451149Sbostic * Ready to start writing segments. The first segment is different 32551149Sbostic * because it contains the segment usage table and the ifile inode 32651864Sbostic * as well as a superblock. For the rest of the segments, set the 32751864Sbostic * time stamp to be 0 so that the first segment is the most recent. 32851864Sbostic * For each segment that is supposed to contain a copy of the super 32951864Sbostic * block, initialize its first few blocks and its segment summary 33051864Sbostic * to indicate this. 33151149Sbostic */ 33251864Sbostic lfsp->lfs_nfiles = LFS_FIRST_INUM - 1; 33351149Sbostic lfsp->lfs_cksum = 33451866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 33551149Sbostic 33651864Sbostic /* Now create a block of disk inodes */ 33751864Sbostic if (!(dpagep = malloc(lfsp->lfs_bsize))) 33851864Sbostic fatal("%s", strerror(errno)); 33951864Sbostic dip = (struct dinode *)dpagep; 34051864Sbostic bzero(dip, lfsp->lfs_bsize); 34151864Sbostic 34251866Sbostic /* Create a block of IFILE structures. */ 34351866Sbostic if (!(ipagep = malloc(lfsp->lfs_bsize))) 34451149Sbostic fatal("%s", strerror(errno)); 34551866Sbostic ifile = (IFILE *)ipagep; 34651864Sbostic 34751866Sbostic /* 34851866Sbostic * Initialize IFILE. It is the next block following the 34951866Sbostic * block of inodes (whose address has been calculated in 35051866Sbostic * lfsp->lfs_idaddr; 35151866Sbostic */ 35251866Sbostic sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize; 35351866Sbostic sb_addr = make_dinode(LFS_IFILE_INUM, dip, 35451866Sbostic lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp); 35551864Sbostic dip->di_mode = IFREG|IREAD|IWRITE; 35651864Sbostic ip = &ifile[LFS_IFILE_INUM]; 35751864Sbostic ip->if_version = 1; 35851864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 35951864Sbostic 36051864Sbostic /* Initialize the ROOT Directory */ 36151864Sbostic sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp); 36251864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 36351864Sbostic dip->di_size = DIRBLKSIZ; 36451864Sbostic dip->di_nlink = 3; 36551864Sbostic ip = &ifile[ROOTINO]; 36651864Sbostic ip->if_version = 1; 36751864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 36851864Sbostic 36951864Sbostic /* Initialize the lost+found Directory */ 37051864Sbostic sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp); 37151864Sbostic dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 37251864Sbostic dip->di_size = DIRBLKSIZ; 37351864Sbostic dip->di_nlink = 2; 37451864Sbostic ip = &ifile[LOSTFOUNDINO]; 37551864Sbostic ip->if_version = 1; 37651864Sbostic ip->if_daddr = lfsp->lfs_idaddr; 37751864Sbostic 37851864Sbostic /* Make all the other dinodes invalid */ 37951864Sbostic for (i = INOPB(lfsp)-3, dip++; i; i--, dip++) 38056861Smargo dip->di_inumber = LFS_UNUSED_INUM; 38151864Sbostic 38251864Sbostic 38351864Sbostic /* Link remaining IFILE entries in free list */ 38451864Sbostic for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM; 38551864Sbostic i < lfsp->lfs_ifpb; ++ip) { 38651149Sbostic ip->if_version = 1; 38751149Sbostic ip->if_daddr = LFS_UNUSED_DADDR; 38851149Sbostic ip->if_nextfree = ++i; 38951149Sbostic } 39051149Sbostic ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM; 39151149Sbostic 39251866Sbostic /* Now, write the segment */ 39351149Sbostic 39451866Sbostic /* Compute a checksum across all the data you're writing */ 39551866Sbostic dp = datasump = malloc (blocks_used * sizeof(u_long)); 39655654Sbostic *dp++ = ((u_long *)dpagep)[0]; /* inode block */ 39751866Sbostic for (i = 0; i < lfsp->lfs_cleansz; i++) 39851866Sbostic *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) / 39951866Sbostic sizeof(u_long)]; /* Cleaner info */ 40051866Sbostic for (i = 0; i < lfsp->lfs_segtabsz; i++) 40151866Sbostic *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) / 40251866Sbostic sizeof(u_long)]; /* Segusage table */ 40351866Sbostic *dp++ = ((u_long *)ifile)[0]; /* Ifile */ 40451866Sbostic 40551866Sbostic /* Still need the root and l+f bytes; get them later */ 40651866Sbostic 40751866Sbostic /* Write out the inode block */ 40851866Sbostic off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE; 40951866Sbostic put(fd, off, dpagep, lfsp->lfs_bsize); 41051866Sbostic free(dpagep); 41151866Sbostic off += lfsp->lfs_bsize; 41251866Sbostic 41351866Sbostic /* Write out the ifile */ 41451866Sbostic 41551866Sbostic put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift); 41651866Sbostic off += (lfsp->lfs_cleansz << lfsp->lfs_bshift); 41751866Sbostic (void)free(cleaninfo); 41851866Sbostic 41951866Sbostic put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift); 42051866Sbostic off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift); 42151866Sbostic (void)free(segtable); 42251866Sbostic 42351866Sbostic put(fd, off, ifile, lfsp->lfs_bsize); 42451866Sbostic off += lfsp->lfs_bsize; 42551866Sbostic 42651866Sbostic /* 42751866Sbostic * use ipagep for space for writing out other stuff. It used to 42851866Sbostic * contain the ifile, but we're done with it. 42951866Sbostic */ 43051866Sbostic 43151864Sbostic /* Write out the root and lost and found directories */ 43251866Sbostic bzero(ipagep, lfsp->lfs_bsize); 43351866Sbostic make_dir(ipagep, lfs_root_dir, 43451864Sbostic sizeof(lfs_root_dir) / sizeof(struct direct)); 43551866Sbostic *dp++ = ((u_long *)ipagep)[0]; 43651866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 43751866Sbostic off += lfsp->lfs_bsize; 43851149Sbostic 43951866Sbostic bzero(ipagep, lfsp->lfs_bsize); 44051866Sbostic make_dir(ipagep, lfs_lf_dir, 44151864Sbostic sizeof(lfs_lf_dir) / sizeof(struct direct)); 44251866Sbostic *dp++ = ((u_long *)ipagep)[0]; 44351866Sbostic put(fd, off, ipagep, lfsp->lfs_bsize); 44451149Sbostic 44551866Sbostic /* Write Supberblock */ 44651866Sbostic lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize; 44751866Sbostic put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs)); 44851149Sbostic 44951866Sbostic /* 45051866Sbostic * Finally, calculate all the fields for the summary structure 45151866Sbostic * and write it. 45251866Sbostic */ 45351866Sbostic 45451864Sbostic summary.ss_next = lfsp->lfs_nextseg; 45551149Sbostic summary.ss_create = lfsp->lfs_tstamp; 45651866Sbostic summary.ss_nfinfo = 3; 45751866Sbostic summary.ss_ninos = 3; 45851866Sbostic summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used); 45951149Sbostic 46051864Sbostic /* 46151864Sbostic * Make sure that we don't overflow a summary block. We have to 46251866Sbostic * record: FINFO structures for ifile, root, and l+f. The number 46351866Sbostic * of blocks recorded for the ifile is determined by the size of 46451866Sbostic * the cleaner info and the segments usage table. There is room 46551866Sbostic * for one block included in sizeof(FINFO) so we don't need to add 46651866Sbostic * any extra space for the ROOT and L+F, and one block of the ifile 46751866Sbostic * is already counted. Finally, we leave room for 1 inode block 46851866Sbostic * address. 46951864Sbostic */ 47053248Sbostic sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) + 47151866Sbostic (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long); 47251149Sbostic #define SUMERR \ 47351149Sbostic "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes." 47451149Sbostic if (sum_size > LFS_SUMMARY_SIZE) 47551149Sbostic fatal(SUMERR, sum_size); 47651149Sbostic 47751866Sbostic block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1; 47851149Sbostic 47951149Sbostic if (!(block_array = malloc(block_array_size *sizeof(int)))) 48051149Sbostic fatal("%s: %s", special, strerror(errno)); 48151149Sbostic 48251149Sbostic /* fill in the array */ 48351866Sbostic for (i = 0; i < block_array_size; i++) 48451149Sbostic block_array[i] = i; 48551149Sbostic 48651149Sbostic /* copy into segment */ 48751866Sbostic sump = ipagep; 48851149Sbostic bcopy(&summary, sump, sizeof(SEGSUM)); 48951149Sbostic sump += sizeof(SEGSUM); 49051149Sbostic 49151149Sbostic /* Now, add the ifile */ 49251866Sbostic file_info.fi_nblocks = block_array_size; 49351149Sbostic file_info.fi_version = 1; 49451149Sbostic file_info.fi_ino = LFS_IFILE_INUM; 49551149Sbostic 49651149Sbostic bcopy(&file_info, sump, sizeof(FINFO) - sizeof(u_long)); 49751149Sbostic sump += sizeof(FINFO) - sizeof(u_long); 49851149Sbostic bcopy(block_array, sump, sizeof(u_long) * file_info.fi_nblocks); 49951864Sbostic sump += sizeof(u_long) * file_info.fi_nblocks; 50051149Sbostic 50151864Sbostic /* Now, add the root directory */ 50251864Sbostic file_info.fi_nblocks = 1; 50351864Sbostic file_info.fi_version = 1; 50451864Sbostic file_info.fi_ino = ROOTINO; 50551864Sbostic file_info.fi_blocks[0] = 0; 50651864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 50751864Sbostic sump += sizeof(FINFO); 50851864Sbostic 50951864Sbostic /* Now, add the lost and found */ 51051864Sbostic file_info.fi_ino = LOSTFOUNDINO; 51151864Sbostic bcopy(&file_info, sump, sizeof(FINFO)); 51251864Sbostic 51351866Sbostic ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] = 51451866Sbostic lfsp->lfs_idaddr; 51551866Sbostic ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum), 51651866Sbostic LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum)); 51751866Sbostic put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE); 51851864Sbostic 51951866Sbostic sp = (SEGSUM *)ipagep; 52051866Sbostic sp->ss_create = 0; 52151866Sbostic sp->ss_nfinfo = 0; 52251866Sbostic sp->ss_ninos = 0; 52351866Sbostic sp->ss_datasum = 0; 52451149Sbostic 52555480Sbostic /* Now write the summary block for the next partial so it's invalid */ 52655480Sbostic lfsp->lfs_tstamp = 0; 52755480Sbostic off += lfsp->lfs_bsize; 52855480Sbostic sp->ss_sumsum = 52955480Sbostic cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 53055480Sbostic put(fd, off, sp, LFS_SUMMARY_SIZE); 53155480Sbostic 53251149Sbostic /* Now, write rest of segments containing superblocks */ 53351864Sbostic lfsp->lfs_cksum = 53451866Sbostic cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 53551864Sbostic for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1; 53651864Sbostic i < lfsp->lfs_nseg; i++) { 53751149Sbostic 53851864Sbostic seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb; 53951866Sbostic sp->ss_next = last_addr; 54051866Sbostic last_addr = seg_addr; 54151866Sbostic seg_seek = seg_addr * lp->d_secsize; 54251866Sbostic 54351864Sbostic if (seg_addr == lfsp->lfs_sboffs[j]) { 54451864Sbostic if (j < (LFS_MAXNUMSB - 2)) 54551864Sbostic j++; 54651866Sbostic put(fd, seg_seek, lfsp, sizeof(struct lfs)); 54751866Sbostic seg_seek += LFS_SBPAD; 54851866Sbostic } 54951149Sbostic 55051149Sbostic /* Summary */ 55151866Sbostic sp->ss_sumsum = cksum(&sp->ss_datasum, 55251866Sbostic LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 55351866Sbostic put(fd, seg_seek, sp, LFS_SUMMARY_SIZE); 55451149Sbostic } 55551866Sbostic free(ipagep); 55651864Sbostic close(fd); 55751149Sbostic return (0); 55851149Sbostic } 55951149Sbostic 56051149Sbostic static void 56151149Sbostic put(fd, off, p, len) 56251149Sbostic int fd; 56351149Sbostic off_t off; 56451149Sbostic void *p; 56551149Sbostic size_t len; 56651149Sbostic { 56751149Sbostic int wbytes; 56851149Sbostic 56951149Sbostic if (lseek(fd, off, SEEK_SET) < 0) 57051149Sbostic fatal("%s: %s", special, strerror(errno)); 57151149Sbostic if ((wbytes = write(fd, p, len)) < 0) 57251149Sbostic fatal("%s: %s", special, strerror(errno)); 57351149Sbostic if (wbytes != len) 57451149Sbostic fatal("%s: short write (%d, not %d)", special, wbytes, len); 57551149Sbostic } 57651864Sbostic 57751864Sbostic /* 57851864Sbostic * Create the root directory for this file system and the lost+found 57951864Sbostic * directory. 58051864Sbostic */ 58151864Sbostic 58251864Sbostic u_long d_ino; /* inode number of entry */ 58351864Sbostic u_short d_reclen; /* length of this record */ 58451864Sbostic u_short d_namlen; /* length of string in d_name */ 58551864Sbostic char d_name[MAXNAMLEN + 1]; /* name with length <= MAXNAMLEN */ 58655389Sbostic void 58751864Sbostic lfsinit() 58855389Sbostic {} 58951864Sbostic 59051864Sbostic static daddr_t 59151864Sbostic make_dinode(ino, dip, nblocks, saddr, lfsp) 59251864Sbostic ino_t ino; /* inode we're creating */ 59351864Sbostic struct dinode *dip; /* disk inode */ 59451864Sbostic int nblocks; /* number of blocks in file */ 59551864Sbostic daddr_t saddr; /* starting block address */ 59651866Sbostic struct lfs *lfsp; /* superblock */ 59751864Sbostic { 59851864Sbostic int db_per_fb, i; 59951864Sbostic 60051864Sbostic dip->di_nlink = 1; 60155440Sbostic dip->di_blocks = nblocks << lfsp->lfs_fsbtodb; 60251864Sbostic 60355480Sbostic dip->di_size = (nblocks << lfsp->lfs_bshift); 60454715Sbostic dip->di_atime.ts_sec = dip->di_mtime.ts_sec = 60554715Sbostic dip->di_ctime.ts_sec = lfsp->lfs_tstamp; 60654715Sbostic dip->di_atime.ts_nsec = dip->di_mtime.ts_nsec = 60754715Sbostic dip->di_ctime.ts_nsec = 0; 60856861Smargo dip->di_inumber = ino; 60951864Sbostic 61051864Sbostic #define SEGERR \ 61151864Sbostic "File requires more than the number of direct blocks; increase block or segment size." 61251864Sbostic if (NDADDR < nblocks) 61351864Sbostic fatal("%s", SEGERR); 61451864Sbostic 61551864Sbostic /* Assign the block addresses for the ifile */ 61651864Sbostic db_per_fb = 1 << lfsp->lfs_fsbtodb; 61755440Sbostic for (i = 0; i < nblocks; i++, saddr += db_per_fb) 61851864Sbostic dip->di_db[i] = saddr; 61951864Sbostic 62051864Sbostic return (saddr); 62151864Sbostic } 62251864Sbostic 62351864Sbostic 62451864Sbostic /* 62551864Sbostic * Construct a set of directory entries in "bufp". We assume that all the 62651864Sbostic * entries in protodir fir in the first DIRBLKSIZ. 62751864Sbostic */ 62851864Sbostic static void 62951864Sbostic make_dir(bufp, protodir, entries) 63051864Sbostic void *bufp; 63151864Sbostic register struct direct *protodir; 63251864Sbostic int entries; 63351864Sbostic { 63451864Sbostic char *cp; 63551864Sbostic int i, spcleft; 63651864Sbostic 63751864Sbostic spcleft = DIRBLKSIZ; 63851864Sbostic for (cp = bufp, i = 0; i < entries - 1; i++) { 63954715Sbostic protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i]); 64051864Sbostic bcopy(&protodir[i], cp, protodir[i].d_reclen); 64151864Sbostic cp += protodir[i].d_reclen; 64251864Sbostic if ((spcleft -= protodir[i].d_reclen) < 0) 64351864Sbostic fatal("%s: %s", special, "directory too big"); 64451864Sbostic } 64551864Sbostic protodir[i].d_reclen = spcleft; 64654715Sbostic bcopy(&protodir[i], cp, DIRSIZ(NEWDIRFMT, &protodir[i])); 64751864Sbostic } 648