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