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