1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)lfs.c 8.4 (Berkeley) 05/01/95"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/disklabel.h> 14 #include <sys/time.h> 15 #include <sys/mount.h> 16 17 #include <ufs/ufs/dir.h> 18 #include <ufs/ufs/quota.h> 19 #include <ufs/ufs/dinode.h> 20 #include <ufs/lfs/lfs.h> 21 22 #include <unistd.h> 23 #include <errno.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include "config.h" 27 #include "extern.h" 28 29 /* 30 * This table is indexed by the log base 2 of the block size. 31 * It returns the maximum file size allowed in a file system 32 * with the specified block size. For block sizes smaller than 33 * 8K, the size is limited by tha maximum number of blocks that 34 * can be reached by triply indirect blocks: 35 * NDADDR + INOPB(bsize) + INOPB(bsize)^2 + INOPB(bsize)^3 36 * For block size of 8K or larger, the file size is limited by the 37 * number of blocks that can be represented in the file system. Since 38 * we use negative block numbers to represent indirect blocks, we can 39 * have a maximum of 2^31 blocks. 40 */ 41 42 u_quad_t maxtable[] = { 43 /* 1 */ -1, 44 /* 2 */ -1, 45 /* 4 */ -1, 46 /* 8 */ -1, 47 /* 16 */ -1, 48 /* 32 */ -1, 49 /* 64 */ -1, 50 /* 128 */ -1, 51 /* 256 */ -1, 52 /* 512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128, 53 /* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256, 54 /* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512, 55 /* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024, 56 /* 8192 */ 1 << 31, 57 /* 16 K */ 1 << 31, 58 /* 32 K */ 1 << 31, 59 }; 60 61 static struct lfs lfs_default = { 62 /* lfs_magic */ LFS_MAGIC, 63 /* lfs_version */ LFS_VERSION, 64 /* lfs_size */ 0, 65 /* lfs_ssize */ DFL_LFSSEG/DFL_LFSBLOCK, 66 /* lfs_dsize */ 0, 67 /* lfs_bsize */ DFL_LFSBLOCK, 68 /* lfs_fsize */ DFL_LFSBLOCK, 69 /* lfs_frag */ 1, 70 /* lfs_free */ LFS_FIRST_INUM, 71 /* lfs_bfree */ 0, 72 /* lfs_nfiles */ 0, 73 /* lfs_avail */ 0, 74 /* lfs_uinodes */ 0, 75 /* lfs_idaddr */ 0, 76 /* lfs_ifile */ LFS_IFILE_INUM, 77 /* lfs_lastseg */ 0, 78 /* lfs_nextseg */ 0, 79 /* lfs_curseg */ 0, 80 /* lfs_offset */ 0, 81 /* lfs_lastpseg */ 0, 82 /* lfs_tstamp */ 0, 83 /* lfs_minfree */ MINFREE, 84 /* lfs_maxfilesize */ 0, 85 /* lfs_dbpseg */ DFL_LFSSEG/DEV_BSIZE, 86 /* lfs_inopb */ DFL_LFSBLOCK/sizeof(struct dinode), 87 /* lfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 88 /* lfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 89 /* lfs_nindir */ DFL_LFSBLOCK/sizeof(daddr_t), 90 /* lfs_nseg */ 0, 91 /* lfs_nspf */ 0, 92 /* lfs_cleansz */ 0, 93 /* lfs_segtabsz */ 0, 94 /* lfs_segmask */ DFL_LFSSEG_MASK, 95 /* lfs_segshift */ DFL_LFSSEG_SHIFT, 96 /* lfs_bmask */ DFL_LFSBLOCK_MASK, 97 /* lfs_bshift */ DFL_LFSBLOCK_SHIFT, 98 /* lfs_ffmask */ 0, 99 /* lfs_ffshift */ 0, 100 /* lfs_fbmask */ 0, 101 /* lfs_fbshift */ 0, 102 /* lfs_fsbtodb */ 0, 103 /* lfs_sushift */ 0, 104 /* lfs_sboffs */ { 0 }, 105 /* lfs_sp */ NULL, 106 /* lfs_ivnode */ NULL, 107 /* lfs_seglock */ 0, 108 /* lfs_lockpid */ 0, 109 /* lfs_iocount */ 0, 110 /* lfs_writer */ 0, 111 /* lfs_dirops */ 0, 112 /* lfs_doifile */ 0, 113 /* lfs_nactive */ 0, 114 /* lfs_fmod */ 0, 115 /* lfs_clean */ 0, 116 /* lfs_ronly */ 0, 117 /* lfs_flags */ 0, 118 /* lfs_fsmnt */ { 0 }, 119 /* lfs_pad */ { 0 }, 120 /* lfs_cksum */ 0, 121 /* lfs_maxsymlinklen */ MAXSYMLINKLEN 122 }; 123 124 125 struct direct lfs_root_dir[] = { 126 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "."}, 127 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."}, 128 { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"}, 129 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"}, 130 }; 131 132 struct direct lfs_lf_dir[] = { 133 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 134 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 135 }; 136 137 static daddr_t make_dinode 138 __P((ino_t, struct dinode *, int, daddr_t, struct lfs *)); 139 static void make_dir __P(( void *, struct direct *, int)); 140 static void put __P((int, off_t, void *, size_t)); 141 142 int 143 make_lfs(fd, lp, partp, minfree, block_size, seg_size) 144 int fd; 145 struct disklabel *lp; 146 struct partition *partp; 147 int minfree; 148 int block_size; 149 int seg_size; 150 { 151 struct dinode *dip; /* Pointer to a disk inode */ 152 struct dinode *dpagep; /* Pointer to page of disk inodes */ 153 CLEANERINFO *cleaninfo; /* Segment cleaner information table */ 154 FINFO file_info; /* File info structure in summary blocks */ 155 IFILE *ifile; /* Pointer to array of ifile structures */ 156 IFILE *ip; /* Pointer to array of ifile structures */ 157 struct lfs *lfsp; /* Superblock */ 158 SEGUSE *segp; /* Segment usage table */ 159 SEGUSE *segtable; /* Segment usage table */ 160 SEGSUM summary; /* Segment summary structure */ 161 SEGSUM *sp; /* Segment summary pointer */ 162 daddr_t last_sb_addr; /* Address of superblocks */ 163 daddr_t last_addr; /* Previous segment address */ 164 daddr_t sb_addr; /* Address of superblocks */ 165 daddr_t seg_addr; /* Address of current segment */ 166 void *ipagep; /* Pointer to the page we use to write stuff */ 167 void *sump; /* Used to copy stuff into segment buffer */ 168 u_long *block_array; /* Array of logical block nos to put in sum */ 169 u_long blocks_used; /* Number of blocks in first segment */ 170 u_long *dp; /* Used to computed checksum on data */ 171 u_long *datasump; /* Used to computed checksum on data */ 172 int block_array_size; /* How many entries in block array */ 173 int bsize; /* Block size */ 174 int db_per_fb; /* Disk blocks per file block */ 175 int i, j; 176 int off; /* Offset at which to write */ 177 int sb_interval; /* number of segs between super blocks */ 178 int seg_seek; /* Seek offset for a segment */ 179 int ssize; /* Segment size */ 180 int sum_size; /* Size of the summary block */ 181 182 lfsp = &lfs_default; 183 184 if (!(bsize = block_size)) 185 bsize = DFL_LFSBLOCK; 186 if (!(ssize = seg_size)) 187 ssize = DFL_LFSSEG; 188 189 /* Modify parts of superblock overridden by command line arguments */ 190 if (bsize != DFL_LFSBLOCK) { 191 lfsp->lfs_bshift = log2(bsize); 192 if (1 << lfsp->lfs_bshift != bsize) 193 fatal("%d: block size not a power of 2", bsize); 194 lfsp->lfs_bsize = bsize; 195 lfsp->lfs_fsize = bsize; 196 lfsp->lfs_bmask = bsize - 1; 197 lfsp->lfs_inopb = bsize / sizeof(struct dinode); 198 /* MIS -- should I round to power of 2 */ 199 lfsp->lfs_ifpb = bsize / sizeof(IFILE); 200 lfsp->lfs_sepb = bsize / sizeof(SEGUSE); 201 lfsp->lfs_nindir = bsize / sizeof(daddr_t); 202 } 203 204 if (ssize != DFL_LFSSEG) { 205 lfsp->lfs_segshift = log2(ssize); 206 if (1 << lfsp->lfs_segshift != ssize) 207 fatal("%d: segment size not power of 2", ssize); 208 lfsp->lfs_ssize = ssize; 209 lfsp->lfs_segmask = ssize - 1; 210 lfsp->lfs_dbpseg = ssize / DEV_BSIZE; 211 } 212 lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift; 213 214 if (minfree) 215 lfsp->lfs_minfree = minfree; 216 217 /* 218 * Fill in parts of superblock that can be computed from file system 219 * size, disk geometry and current time. 220 */ 221 db_per_fb = bsize/lp->d_secsize; 222 lfsp->lfs_fsbtodb = log2(db_per_fb); 223 lfsp->lfs_sushift = log2(lfsp->lfs_sepb); 224 lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb; 225 lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift); 226 lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize; 227 lfsp->lfs_maxfilesize = maxtable[lfsp->lfs_bshift] << lfsp->lfs_bshift; 228 229 /* 230 * The number of free blocks is set from the number of segments times 231 * the segment size - 2 (that we never write because we need to make 232 * sure the cleaner can run). Then we'll subtract off the room for the 233 * superblocks ifile entries and segment usage table. 234 */ 235 lfsp->lfs_dsize = fsbtodb(lfsp, (lfsp->lfs_nseg - 2) * lfsp->lfs_ssize); 236 lfsp->lfs_bfree = lfsp->lfs_dsize; 237 lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp); 238 lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp); 239 if ((lfsp->lfs_tstamp = time(NULL)) == -1) 240 fatal("time: %s", strerror(errno)); 241 if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 242 sb_interval = LFS_MIN_SBINTERVAL; 243 244 /* 245 * Now, lay out the file system. We need to figure out where 246 * the superblocks go, initialize the checkpoint information 247 * for the first two superblocks, initialize the segment usage 248 * information, put the segusage information in the ifile, create 249 * the first block of IFILE structures, and link all the IFILE 250 * structures into a free list. 251 */ 252 253 /* Figure out where the superblocks are going to live */ 254 lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize; 255 for (i = 1; i < LFS_MAXNUMSB; i++) { 256 sb_addr = ((i * sb_interval) << 257 (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb)) 258 + lfsp->lfs_sboffs[0]; 259 if (sb_addr > partp->p_size) 260 break; 261 lfsp->lfs_sboffs[i] = sb_addr; 262 } 263 last_sb_addr = lfsp->lfs_sboffs[i - 1]; 264 lfsp->lfs_lastseg = lfsp->lfs_sboffs[0]; 265 lfsp->lfs_nextseg = 266 lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0]; 267 lfsp->lfs_curseg = lfsp->lfs_lastseg; 268 269 /* 270 * Initialize the segment usage table. The first segment will 271 * contain the superblock, the cleanerinfo (cleansz), the segusage 272 * table * (segtabsz), 1 block's worth of IFILE entries, the root 273 * directory, the lost+found directory and one block's worth of 274 * inodes (containing the ifile, root, and l+f inodes). 275 */ 276 if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift))) 277 fatal("%s", strerror(errno)); 278 cleaninfo->clean = lfsp->lfs_nseg - 1; 279 cleaninfo->dirty = 1; 280 281 if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift))) 282 fatal("%s", strerror(errno)); 283 segp = segtable; 284 blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4; 285 segp->su_nbytes = ((blocks_used - 1) << lfsp->lfs_bshift) + 286 3 * sizeof(struct dinode) + LFS_SUMMARY_SIZE; 287 segp->su_lastmod = lfsp->lfs_tstamp; 288 segp->su_nsums = 1; /* 1 summary blocks */ 289 segp->su_ninos = 1; /* 1 inode block */ 290 segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY; 291 lfsp->lfs_bfree -= LFS_SUMMARY_SIZE / lp->d_secsize; 292 lfsp->lfs_bfree -= 293 fsbtodb(lfsp, lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4); 294 295 /* 296 * Now figure out the address of the ifile inode. The inode block 297 * appears immediately after the segment summary. 298 */ 299 lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) / 300 lp->d_secsize; 301 302 for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) { 303 if ((i % sb_interval) == 0) { 304 segp->su_flags = SEGUSE_SUPERBLOCK; 305 lfsp->lfs_bfree -= (LFS_SBPAD / lp->d_secsize); 306 } else 307 segp->su_flags = 0; 308 segp->su_lastmod = 0; 309 segp->su_nbytes = 0; 310 segp->su_ninos = 0; 311 segp->su_nsums = 0; 312 } 313 314 /* 315 * Initialize dynamic accounting. The blocks available for 316 * writing are the bfree blocks minus 1 segment summary for 317 * each segment since you can't write any new data without 318 * creating a segment summary - 2 segments that the cleaner 319 * needs. 320 */ 321 lfsp->lfs_avail = lfsp->lfs_bfree - lfsp->lfs_nseg - 322 fsbtodb(lfsp, 2 * lfsp->lfs_ssize); 323 lfsp->lfs_uinodes = 0; 324 /* 325 * Ready to start writing segments. The first segment is different 326 * because it contains the segment usage table and the ifile inode 327 * as well as a superblock. For the rest of the segments, set the 328 * time stamp to be 0 so that the first segment is the most recent. 329 * For each segment that is supposed to contain a copy of the super 330 * block, initialize its first few blocks and its segment summary 331 * to indicate this. 332 */ 333 lfsp->lfs_nfiles = LFS_FIRST_INUM - 1; 334 lfsp->lfs_cksum = 335 cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 336 337 /* Now create a block of disk inodes */ 338 if (!(dpagep = malloc(lfsp->lfs_bsize))) 339 fatal("%s", strerror(errno)); 340 dip = (struct dinode *)dpagep; 341 memset(dip, 0, lfsp->lfs_bsize); 342 343 /* Create a block of IFILE structures. */ 344 if (!(ipagep = malloc(lfsp->lfs_bsize))) 345 fatal("%s", strerror(errno)); 346 ifile = (IFILE *)ipagep; 347 348 /* 349 * Initialize IFILE. It is the next block following the 350 * block of inodes (whose address has been calculated in 351 * lfsp->lfs_idaddr; 352 */ 353 sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize; 354 sb_addr = make_dinode(LFS_IFILE_INUM, dip, 355 lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp); 356 dip->di_mode = IFREG|IREAD|IWRITE; 357 ip = &ifile[LFS_IFILE_INUM]; 358 ip->if_version = 1; 359 ip->if_daddr = lfsp->lfs_idaddr; 360 361 /* Initialize the ROOT Directory */ 362 sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp); 363 dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 364 dip->di_size = DIRBLKSIZ; 365 dip->di_nlink = 3; 366 ip = &ifile[ROOTINO]; 367 ip->if_version = 1; 368 ip->if_daddr = lfsp->lfs_idaddr; 369 370 /* Initialize the lost+found Directory */ 371 sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp); 372 dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC; 373 dip->di_size = DIRBLKSIZ; 374 dip->di_nlink = 2; 375 ip = &ifile[LOSTFOUNDINO]; 376 ip->if_version = 1; 377 ip->if_daddr = lfsp->lfs_idaddr; 378 379 /* Make all the other dinodes invalid */ 380 for (i = INOPB(lfsp)-3, dip++; i; i--, dip++) 381 dip->di_inumber = LFS_UNUSED_INUM; 382 383 384 /* Link remaining IFILE entries in free list */ 385 for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM; 386 i < lfsp->lfs_ifpb; ++ip) { 387 ip->if_version = 1; 388 ip->if_daddr = LFS_UNUSED_DADDR; 389 ip->if_nextfree = ++i; 390 } 391 ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM; 392 393 /* Now, write the segment */ 394 395 /* Compute a checksum across all the data you're writing */ 396 dp = datasump = malloc (blocks_used * sizeof(u_long)); 397 *dp++ = ((u_long *)dpagep)[0]; /* inode block */ 398 for (i = 0; i < lfsp->lfs_cleansz; i++) 399 *dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) / 400 sizeof(u_long)]; /* Cleaner info */ 401 for (i = 0; i < lfsp->lfs_segtabsz; i++) 402 *dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) / 403 sizeof(u_long)]; /* Segusage table */ 404 *dp++ = ((u_long *)ifile)[0]; /* Ifile */ 405 406 /* Still need the root and l+f bytes; get them later */ 407 408 /* Write out the inode block */ 409 off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE; 410 put(fd, off, dpagep, lfsp->lfs_bsize); 411 free(dpagep); 412 off += lfsp->lfs_bsize; 413 414 /* Write out the ifile */ 415 416 put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift); 417 off += (lfsp->lfs_cleansz << lfsp->lfs_bshift); 418 (void)free(cleaninfo); 419 420 put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift); 421 off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift); 422 (void)free(segtable); 423 424 put(fd, off, ifile, lfsp->lfs_bsize); 425 off += lfsp->lfs_bsize; 426 427 /* 428 * use ipagep for space for writing out other stuff. It used to 429 * contain the ifile, but we're done with it. 430 */ 431 432 /* Write out the root and lost and found directories */ 433 memset(ipagep, 0, lfsp->lfs_bsize); 434 make_dir(ipagep, lfs_root_dir, 435 sizeof(lfs_root_dir) / sizeof(struct direct)); 436 *dp++ = ((u_long *)ipagep)[0]; 437 put(fd, off, ipagep, lfsp->lfs_bsize); 438 off += lfsp->lfs_bsize; 439 440 memset(ipagep, 0, lfsp->lfs_bsize); 441 make_dir(ipagep, lfs_lf_dir, 442 sizeof(lfs_lf_dir) / sizeof(struct direct)); 443 *dp++ = ((u_long *)ipagep)[0]; 444 put(fd, off, ipagep, lfsp->lfs_bsize); 445 446 /* Write Supberblock */ 447 lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize; 448 put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs)); 449 450 /* 451 * Finally, calculate all the fields for the summary structure 452 * and write it. 453 */ 454 455 summary.ss_next = lfsp->lfs_nextseg; 456 summary.ss_create = lfsp->lfs_tstamp; 457 summary.ss_nfinfo = 3; 458 summary.ss_ninos = 3; 459 summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used); 460 461 /* 462 * Make sure that we don't overflow a summary block. We have to 463 * record: FINFO structures for ifile, root, and l+f. The number 464 * of blocks recorded for the ifile is determined by the size of 465 * the cleaner info and the segments usage table. There is room 466 * for one block included in sizeof(FINFO) so we don't need to add 467 * any extra space for the ROOT and L+F, and one block of the ifile 468 * is already counted. Finally, we leave room for 1 inode block 469 * address. 470 */ 471 sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) + 472 (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long); 473 #define SUMERR \ 474 "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes." 475 if (sum_size > LFS_SUMMARY_SIZE) 476 fatal(SUMERR, sum_size); 477 478 block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1; 479 480 if (!(block_array = malloc(block_array_size *sizeof(int)))) 481 fatal("%s: %s", special, strerror(errno)); 482 483 /* fill in the array */ 484 for (i = 0; i < block_array_size; i++) 485 block_array[i] = i; 486 487 /* copy into segment */ 488 sump = ipagep; 489 memmove(sump, &summary, sizeof(SEGSUM)); 490 sump += sizeof(SEGSUM); 491 492 /* Now, add the ifile */ 493 file_info.fi_nblocks = block_array_size; 494 file_info.fi_version = 1; 495 file_info.fi_ino = LFS_IFILE_INUM; 496 497 memmove(sump, &file_info, sizeof(FINFO) - sizeof(u_long)); 498 sump += sizeof(FINFO) - sizeof(u_long); 499 memmove(sump, block_array, sizeof(u_long) * file_info.fi_nblocks); 500 sump += sizeof(u_long) * file_info.fi_nblocks; 501 502 /* Now, add the root directory */ 503 file_info.fi_nblocks = 1; 504 file_info.fi_version = 1; 505 file_info.fi_ino = ROOTINO; 506 file_info.fi_blocks[0] = 0; 507 memmove(sump, &file_info, sizeof(FINFO)); 508 sump += sizeof(FINFO); 509 510 /* Now, add the lost and found */ 511 file_info.fi_ino = LOSTFOUNDINO; 512 memmove(sump, &file_info, sizeof(FINFO)); 513 514 ((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] = 515 lfsp->lfs_idaddr; 516 ((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum), 517 LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum)); 518 put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE); 519 520 sp = (SEGSUM *)ipagep; 521 sp->ss_create = 0; 522 sp->ss_nfinfo = 0; 523 sp->ss_ninos = 0; 524 sp->ss_datasum = 0; 525 526 /* Now write the summary block for the next partial so it's invalid */ 527 lfsp->lfs_tstamp = 0; 528 off += lfsp->lfs_bsize; 529 sp->ss_sumsum = 530 cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 531 put(fd, off, sp, LFS_SUMMARY_SIZE); 532 533 /* Now, write rest of segments containing superblocks */ 534 lfsp->lfs_cksum = 535 cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum)); 536 for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1; 537 i < lfsp->lfs_nseg; i++) { 538 539 seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb; 540 sp->ss_next = last_addr; 541 last_addr = seg_addr; 542 seg_seek = seg_addr * lp->d_secsize; 543 544 if (seg_addr == lfsp->lfs_sboffs[j]) { 545 if (j < (LFS_MAXNUMSB - 2)) 546 j++; 547 put(fd, seg_seek, lfsp, sizeof(struct lfs)); 548 seg_seek += LFS_SBPAD; 549 } 550 551 /* Summary */ 552 sp->ss_sumsum = cksum(&sp->ss_datasum, 553 LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum)); 554 put(fd, seg_seek, sp, LFS_SUMMARY_SIZE); 555 } 556 free(ipagep); 557 close(fd); 558 return (0); 559 } 560 561 static void 562 put(fd, off, p, len) 563 int fd; 564 off_t off; 565 void *p; 566 size_t len; 567 { 568 int wbytes; 569 570 if (lseek(fd, off, SEEK_SET) < 0) 571 fatal("%s: %s", special, strerror(errno)); 572 if ((wbytes = write(fd, p, len)) < 0) 573 fatal("%s: %s", special, strerror(errno)); 574 if (wbytes != len) 575 fatal("%s: short write (%d, not %d)", special, wbytes, len); 576 } 577 578 /* 579 * Create the root directory for this file system and the lost+found 580 * directory. 581 */ 582 583 void 584 lfsinit() 585 {} 586 587 static daddr_t 588 make_dinode(ino, dip, nblocks, saddr, lfsp) 589 ino_t ino; /* inode we're creating */ 590 struct dinode *dip; /* disk inode */ 591 int nblocks; /* number of blocks in file */ 592 daddr_t saddr; /* starting block address */ 593 struct lfs *lfsp; /* superblock */ 594 { 595 int db_per_fb, i; 596 597 dip->di_nlink = 1; 598 dip->di_blocks = nblocks << lfsp->lfs_fsbtodb; 599 600 dip->di_size = (nblocks << lfsp->lfs_bshift); 601 dip->di_atime = dip->di_mtime = dip->di_ctime = lfsp->lfs_tstamp; 602 dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0; 603 dip->di_inumber = ino; 604 605 #define SEGERR \ 606 "File requires more than the number of direct blocks; increase block or segment size." 607 if (NDADDR < nblocks) 608 fatal("%s", SEGERR); 609 610 /* Assign the block addresses for the ifile */ 611 db_per_fb = 1 << lfsp->lfs_fsbtodb; 612 for (i = 0; i < nblocks; i++, saddr += db_per_fb) 613 dip->di_db[i] = saddr; 614 615 return (saddr); 616 } 617 618 619 /* 620 * Construct a set of directory entries in "bufp". We assume that all the 621 * entries in protodir fir in the first DIRBLKSIZ. 622 */ 623 static void 624 make_dir(bufp, protodir, entries) 625 void *bufp; 626 register struct direct *protodir; 627 int entries; 628 { 629 char *cp; 630 int i, spcleft; 631 632 spcleft = DIRBLKSIZ; 633 for (cp = bufp, i = 0; i < entries - 1; i++) { 634 protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i]); 635 memmove(cp, &protodir[i], protodir[i].d_reclen); 636 cp += protodir[i].d_reclen; 637 if ((spcleft -= protodir[i].d_reclen) < 0) 638 fatal("%s: %s", special, "directory too big"); 639 } 640 protodir[i].d_reclen = spcleft; 641 memmove(cp, &protodir[i], DIRSIZ(NEWDIRFMT, &protodir[i])); 642 } 643