1/* $NetBSD$ */ 2 3/*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Konrad E. Schroder <perseant@hhhh.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31/*- 32 * Copyright (c) 1991, 1993 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 */ 59 60#include <sys/cdefs.h> 61#ifndef lint 62#if 0 63static char sccsid[] = "@(#)lfs.c 8.5 (Berkeley) 5/24/95"; 64#else 65__RCSID("$NetBSD$"); 66#endif 67#endif /* not lint */ 68 69#include <sys/param.h> 70#include <sys/disk.h> 71#include <sys/time.h> 72#include <sys/mount.h> 73#include <sys/stat.h> 74 75#include <ufs/ufs/dir.h> 76#include <ufs/ufs/quota.h> 77#include <ufs/ufs/inode.h> 78 79/* Override certain things to make <ufs/lfs/lfs.h> work */ 80# undef simple_lock 81# define simple_lock(x) 82# undef simple_unlock 83# define simple_unlock(x) 84# define vnode uvnode 85# define buf ubuf 86# define panic call_panic 87#include <ufs/lfs/lfs.h> 88 89#include <err.h> 90#include <errno.h> 91#include <stdio.h> 92#include <stdlib.h> 93#include <string.h> 94#include <time.h> 95#include <unistd.h> 96 97#include "config.h" 98#include "extern.h" 99 100#include "bufcache.h" 101#include "vnode.h" 102#include "lfs_user.h" 103#include "segwrite.h" 104 105extern int Nflag; /* Don't write anything */ 106ufs_daddr_t ifibc; /* How many indirect blocks */ 107 108#ifdef MAKE_LF_DIR 109# define HIGHEST_USED_INO LOSTFOUNDINO 110#else 111# define HIGHEST_USED_INO ROOTINO 112#endif 113 114static struct lfs lfs_default = { 115 .lfs_dlfs = { /* lfs_dlfs */ 116 /* dlfs_magic */ LFS_MAGIC, 117 /* dlfs_version */ LFS_VERSION, 118 /* dlfs_size */ 0, 119 /* dlfs_ssize */ DFL_LFSSEG, 120 /* dlfs_dsize */ 0, 121 /* dlfs_bsize */ DFL_LFSBLOCK, 122 /* dlfs_fsize */ DFL_LFSFRAG, 123 /* dlfs_frag */ DFL_LFSBLOCK/DFL_LFSFRAG, 124 /* dlfs_freehd */ HIGHEST_USED_INO + 1, 125 /* dlfs_bfree */ 0, 126 /* dlfs_nfiles */ 0, 127 /* dlfs_avail */ 0, 128 /* dlfs_uinodes */ 0, 129 /* dlfs_idaddr */ 0, 130 /* dlfs_ifile */ LFS_IFILE_INUM, 131 /* dlfs_lastseg */ 0, 132 /* dlfs_nextseg */ 0, 133 /* dlfs_curseg */ 0, 134 /* dlfs_offset */ 0, 135 /* dlfs_lastpseg */ 0, 136 /* dlfs_inopf */ 0, 137 /* dlfs_minfree */ MINFREE, 138 /* dlfs_maxfilesize */ 0, 139 /* dlfs_fsbpseg */ 0, 140 /* dlfs_inopb */ DFL_LFSBLOCK/sizeof(struct ufs1_dinode), 141 /* dlfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 142 /* dlfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 143 /* XXX ondisk32 */ 144 /* dlfs_nindir */ DFL_LFSBLOCK/sizeof(int32_t), 145 /* dlfs_nseg */ 0, 146 /* dlfs_nspf */ 0, 147 /* dlfs_cleansz */ 0, 148 /* dlfs_segtabsz */ 0, 149 /* dlfs_segmask */ DFL_LFSSEG_MASK, 150 /* dlfs_segshift */ DFL_LFSSEG_SHIFT, 151 /* dlfs_bshift */ DFL_LFSBLOCK_SHIFT, 152 /* dlfs_ffshift */ DFL_LFS_FFSHIFT, 153 /* dlfs_fbshift */ DFL_LFS_FBSHIFT, 154 /* dlfs_bmask */ DFL_LFSBLOCK_MASK, 155 /* dlfs_ffmask */ DFL_LFS_FFMASK, 156 /* dlfs_fbmask */ DFL_LFS_FBMASK, 157 /* dlfs_blktodb */ 0, 158 /* dlfs_sushift */ 0, 159 /* dlfs_maxsymlinklen */ MAXSYMLINKLEN_UFS1, 160 /* dlfs_sboffs */ { 0 }, 161 /* dlfs_nclean */ 0, 162 /* dlfs_fsmnt */ { 0 }, 163 /* dlfs_pflags */ LFS_PF_CLEAN, 164 /* dlfs_dmeta */ 0, 165 /* dlfs_minfreeseg */ 0, 166 /* dlfs_sumsize */ 0, 167 /* dlfs_serial */ 0, 168 /* dlfs_ibsize */ DFL_LFSFRAG, 169 /* dlfs_start */ 0, 170 /* dlfs_tstamp */ 0, 171 /* dlfs_inodefmt */ LFS_44INODEFMT, 172 /* dlfs_interleave */ 0, 173 /* dlfs_ident */ 0, 174 /* dlfs_fsbtodb */ 0, 175 /* dlfs_resvseg */ 0, 176 177 /* dlfs_pad */ { 0 }, 178 /* dlfs_cksum */ 0 179 }, 180}; 181 182#define UMASK 0755 183 184struct direct lfs_root_dir[] = { 185 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "."}, 186 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."}, 187 /* { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"}, */ 188#ifdef MAKE_LF_DIR 189 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"}, 190#endif 191}; 192 193#ifdef MAKE_LF_DIR 194struct direct lfs_lf_dir[] = { 195 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 196 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 197}; 198#endif 199 200void pwarn(const char *, ...); 201static void make_dinode(ino_t, struct ufs1_dinode *, int, struct lfs *); 202static void make_dir( void *, struct direct *, int); 203static uint64_t maxfilesize(int); 204 205/* 206 * calculate the maximum file size allowed with the specified block shift. 207 */ 208static uint64_t 209maxfilesize(int bshift) 210{ 211 uint64_t nptr; /* number of block pointers per block */ 212 uint64_t maxblock; 213 214 nptr = (1 << bshift) / sizeof(uint32_t); 215 maxblock = NDADDR + nptr + nptr * nptr + nptr * nptr * nptr; 216 217 return maxblock << bshift; 218} 219 220/* 221 * Create the root directory for this file system and the lost+found 222 * directory. 223 */ 224static void 225make_dinode(ino_t ino, struct ufs1_dinode *dip, int nfrags, struct lfs *fs) 226{ 227 int fsb_per_blk, i; 228 int nblocks, bb, base, factor, lvl; 229 230 nblocks = howmany(nfrags, fs->lfs_frag); 231 if(nblocks >= NDADDR) 232 nfrags = roundup(nfrags, fs->lfs_frag); 233 234 dip->di_nlink = 1; 235 dip->di_blocks = nfrags; 236 237 dip->di_size = (nfrags << fs->lfs_ffshift); 238 dip->di_atime = dip->di_mtime = dip->di_ctime = fs->lfs_tstamp; 239 dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0; 240 dip->di_inumber = ino; 241 dip->di_gen = 1; 242 243 fsb_per_blk = blkstofrags(fs, 1); 244 245 if (NDADDR < nblocks) { 246 /* Count up how many indirect blocks we need, recursively */ 247 /* XXX We are only called with nblocks > 1 for Ifile */ 248 bb = nblocks - NDADDR; 249 while (bb > 0) { 250 bb = howmany(bb, NINDIR(fs)); 251 ifibc += bb; 252 --bb; 253 } 254 dip->di_blocks += blkstofrags(fs, ifibc); 255 } 256 257 /* Assign the block addresses for the ifile */ 258 for (i = 0; i < MIN(nblocks,NDADDR); i++) { 259 dip->di_db[i] = 0x0; 260 } 261 if(nblocks > NDADDR) { 262 dip->di_ib[0] = 0x0; 263 bb = howmany(nblocks - NDADDR, NINDIR(fs)) - 1; 264 factor = NINDIR(fs); 265 base = -NDADDR - factor; 266 lvl = 1; 267 while (bb > 0) { 268 dip->di_ib[lvl] = 0x0; 269 bb = howmany(bb, NINDIR(fs)); 270 --bb; 271 factor *= NINDIR(fs); 272 base -= factor; 273 ++lvl; 274 } 275 } 276} 277 278/* 279 * Construct a set of directory entries in "bufp". We assume that all the 280 * entries in protodir fir in the first DIRBLKSIZ. 281 */ 282static void 283make_dir(void *bufp, struct direct *protodir, int entries) 284{ 285 char *cp; 286 int i, spcleft; 287 288 spcleft = DIRBLKSIZ; 289 for (cp = bufp, i = 0; i < entries - 1; i++) { 290 protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i], 0); 291 memmove(cp, &protodir[i], protodir[i].d_reclen); 292 cp += protodir[i].d_reclen; 293 if ((spcleft -= protodir[i].d_reclen) < 0) 294 fatal("%s: %s", special, "directory too big"); 295 } 296 protodir[i].d_reclen = spcleft; 297 memmove(cp, &protodir[i], DIRSIZ(NEWDIRFMT, &protodir[i], 0)); 298} 299 300int 301make_lfs(int devfd, uint secsize, struct dkwedge_info *dkw, int minfree, 302 int block_size, int frag_size, int seg_size, int minfreeseg, 303 int resvseg, int version, daddr_t start, int ibsize, int interleave, 304 u_int32_t roll_id) 305{ 306 struct ufs1_dinode *dip; /* Pointer to a disk inode */ 307 CLEANERINFO *cip; /* Segment cleaner information table */ 308 IFILE *ip; /* Pointer to array of ifile structures */ 309 IFILE_V1 *ip_v1 = NULL; 310 struct lfs *fs; /* Superblock */ 311 SEGUSE *segp; /* Segment usage table */ 312 daddr_t sb_addr; /* Address of superblocks */ 313 daddr_t seg_addr; /* Address of current segment */ 314 int bsize; /* Block size */ 315 int fsize; /* Fragment size */ 316 int db_per_blk; /* Disk blocks per file block */ 317 int i, j; 318 int sb_interval; /* number of segs between super blocks */ 319 int ssize; /* Segment size */ 320 double fssize; 321 int warned_segtoobig=0; 322 int label_fsb, sb_fsb; 323 int curw, ww; 324 char tbuf[BUFSIZ]; 325 struct ubuf *bp; 326 struct uvnode *vp, *save_devvp; 327 int bb, ubb, dmeta, labelskew; 328 u_int64_t tsepb, tnseg; 329 330 /* 331 * Initialize buffer cache. Use a ballpark guess of the length of 332 * the segment table for the number of hash chains. 333 */ 334 tnseg = dkw->dkw_size / ((seg_size ? seg_size : DFL_LFSSEG) / secsize); 335 tsepb = (block_size ? block_size : DFL_LFSBLOCK) / sizeof(SEGSUM); 336 if (tnseg == 0) 337 fatal("zero size partition"); 338 bufinit(tnseg / tsepb); 339 340 /* Initialize LFS subsystem with blank superblock and ifile. */ 341 fs = lfs_init(devfd, start, (ufs_daddr_t)0, 1, 1/* XXX debug*/); 342 save_devvp = fs->lfs_devvp; 343 vp = fs->lfs_ivnode; 344 *fs = lfs_default; 345 fs->lfs_ivnode = vp; 346 fs->lfs_devvp = save_devvp; 347 348 349 /* Set version first of all since it is used to compute other fields */ 350 fs->lfs_version = version; 351 352 /* If partition is not an LFS partition, warn that that is the case */ 353 if (strcmp(dkw->dkw_ptype, DKW_PTYPE_LFS) != 0) { 354 fatal("partition label indicated fs type \"%s\", " 355 "expected \"%s\"", dkw->dkw_ptype, DKW_PTYPE_LFS); 356 } 357 358 if (!(bsize = block_size)) { 359 bsize = DFL_LFSBLOCK; 360 if (dkw->dkw_size <= SMALL_FSSIZE) 361 bsize = SMALL_LFSBLOCK; 362 } 363 if (!(fsize = frag_size)) { 364 fsize = DFL_LFSFRAG; 365 if (dkw->dkw_size <= SMALL_FSSIZE) 366 fsize = SMALL_LFSFRAG; 367 } 368 if (!(ssize = seg_size)) { 369 ssize = DFL_LFSSEG; 370 if (dkw->dkw_size <= SMALL_FSSIZE) 371 ssize = SMALL_LFSSEG; 372 } 373 if (version > 1) { 374 if (ibsize == 0) 375 ibsize = fsize; 376 if (ibsize <= 0 || ibsize % fsize) 377 fatal("illegal inode block size: %d\n", ibsize); 378 } else if (ibsize && ibsize != bsize) 379 fatal("cannot specify inode block size when version == 1\n"); 380 381 /* Sanity check: fsize<=bsize<ssize */ 382 if (fsize > bsize) { 383 /* Only complain if fsize was explicitly set */ 384 if(frag_size) 385 fatal("fragment size must be <= block size %d", bsize); 386 fsize = bsize; 387 } 388 if (bsize >= ssize) { 389 /* Only fatal if ssize was explicitly set */ 390 if(seg_size) 391 fatal("block size must be < segment size"); 392 warnx("%s: disklabel segment size (%d) too small, using default (%d)", 393 progname, ssize, DFL_LFSSEG); 394 ssize = DFL_LFSSEG; 395 } 396 if (start < 0 || start >= dkw->dkw_size) 397 fatal("filesystem offset %ld out of range", (long)start); 398 if (version == 1) { 399 if (start) 400 warnx("filesystem offset ignored for version 1 filesystem"); 401 start = LFS_LABELPAD / secsize; 402 } 403 404 tryagain: 405 /* Modify parts of superblock overridden by command line arguments */ 406 if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) { 407 fs->lfs_bshift = lfs_log2(bsize); 408 if (1 << fs->lfs_bshift != bsize) 409 fatal("%d: block size not a power of 2", bsize); 410 fs->lfs_bsize = bsize; 411 fs->lfs_fsize = fsize; 412 fs->lfs_bmask = bsize - 1; 413 fs->lfs_ffmask = fsize - 1; 414 fs->lfs_ffshift = lfs_log2(fsize); 415 if (1 << fs->lfs_ffshift != fsize) 416 fatal("%d: frag size not a power of 2", fsize); 417 fs->lfs_frag = numfrags(fs, bsize); 418 fs->lfs_fbmask = fs->lfs_frag - 1; 419 fs->lfs_fbshift = lfs_log2(fs->lfs_frag); 420 fs->lfs_ifpb = bsize / sizeof(IFILE); 421 /* XXX ondisk32 */ 422 fs->lfs_nindir = bsize / sizeof(int32_t); 423 } 424 425 if (fs->lfs_version == 1) { 426 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE; 427 fs->lfs_segshift = lfs_log2(ssize); 428 if (1 << fs->lfs_segshift != ssize) 429 fatal("%d: segment size not power of 2", ssize); 430 fs->lfs_segmask = ssize - 1; 431 fs->lfs_ifpb = fs->lfs_bsize / sizeof(IFILE_V1); 432 fs->lfs_ibsize = fs->lfs_bsize; 433 fs->lfs_sepb = bsize / sizeof(SEGUSE_V1); 434 fs->lfs_ssize = ssize >> fs->lfs_bshift; 435 } else { 436 if (ssize % fsize) { 437 fprintf(stderr, 438 "Segment size %d is not a multiple of frag size; ", 439 ssize); 440 ssize = roundup(ssize, fsize); 441 fprintf(stderr, "trying size %d.\n", ssize); 442 goto tryagain; 443 } 444 fs->lfs_sumsize = fsize; 445 fs->lfs_segshift = 0; 446 fs->lfs_segmask = 0; 447 fs->lfs_sepb = bsize / sizeof(SEGUSE); 448 fs->lfs_ssize = ssize; 449 fs->lfs_ibsize = ibsize; 450 } 451 fs->lfs_inopb = fs->lfs_ibsize / sizeof(struct ufs1_dinode); 452 fs->lfs_minfree = minfree; 453 454 if (version > 1) { 455 fs->lfs_inopf = secsize/DINODE1_SIZE; 456 fs->lfs_interleave = interleave; 457 if (roll_id == 0) 458 roll_id = arc4random(); 459 fs->lfs_ident = roll_id; 460 } 461 462 /* 463 * Fill in parts of superblock that can be computed from file system 464 * size, disk geometry and current time. 465 */ 466 db_per_blk = bsize/secsize; 467 fs->lfs_blktodb = lfs_log2(db_per_blk); 468 fs->lfs_fsbtodb = lfs_log2(fsize / secsize); 469 if (version == 1) { 470 fs->lfs_sushift = lfs_log2(fs->lfs_sepb); 471 fs->lfs_fsbtodb = 0; 472 fs->lfs_size = dkw->dkw_size >> fs->lfs_blktodb; 473 } 474 label_fsb = btofsb(fs, roundup(LFS_LABELPAD, fsize)); 475 sb_fsb = btofsb(fs, roundup(LFS_SBPAD, fsize)); 476 fs->lfs_fsbpseg = dbtofsb(fs, ssize / secsize); 477 fs->lfs_size = dkw->dkw_size >> fs->lfs_fsbtodb; 478 fs->lfs_dsize = dbtofsb(fs, dkw->dkw_size) - 479 MAX(label_fsb, dbtofsb(fs, start)); 480 fs->lfs_nseg = fs->lfs_dsize / segtod(fs, 1); 481 482 fs->lfs_nclean = fs->lfs_nseg - 1; 483 fs->lfs_maxfilesize = maxfilesize(fs->lfs_bshift); 484 485 if (minfreeseg == 0) 486 fs->lfs_minfreeseg = fs->lfs_nseg / DFL_MIN_FREE_SEGS; 487 else 488 fs->lfs_minfreeseg = minfreeseg; 489 if (fs->lfs_minfreeseg < MIN_FREE_SEGS) 490 fs->lfs_minfreeseg = MIN_FREE_SEGS; 491 492 if (resvseg == 0) 493 fs->lfs_resvseg = fs->lfs_minfreeseg / 2 + 1; 494 else 495 fs->lfs_resvseg = resvseg; 496 if (fs->lfs_resvseg < MIN_RESV_SEGS) 497 fs->lfs_resvseg = MIN_RESV_SEGS; 498 499 if(fs->lfs_nseg < (4 * fs->lfs_minfreeseg) 500 || fs->lfs_nseg < LFS_MIN_SBINTERVAL + 1) 501 { 502 if(seg_size == 0 && ssize > (bsize<<1)) { 503 if(!warned_segtoobig) { 504 fprintf(stderr,"Segment size %d is too large; " 505 "trying smaller sizes.\n", ssize); 506 if (ssize == (bsize << 16)) { 507 fprintf(stderr, "(Did you perhaps " 508 "accidentally leave \"16\" " 509 "in the disklabel's sgs " 510 "field?)\n"); 511 } 512 } 513 ++warned_segtoobig; 514 ssize >>= 1; 515 goto tryagain; 516 } 517 fatal("Could not allocate enough segments with segment " 518 "size %d and block size %d;\nplease decrease the " 519 "segment size.\n", ssize, fs->lfs_bsize); 520 } 521 if(warned_segtoobig) 522 fprintf(stderr,"Using segment size %d, block size %d, frag size %d.\n", ssize, bsize, fsize); 523 524 /* 525 * Now that we've determined what we're going to do, announce it 526 * to the user. 527 */ 528 printf("Creating a version %d LFS", fs->lfs_version); 529 if (fs->lfs_version > 1) 530 printf(" with roll-forward ident 0x%x", fs->lfs_ident); 531 printf("\n"); 532 fssize = (double)fs->lfs_nseg; 533 fssize *= (double)ssize; 534 fssize /= 1048576.0; 535 printf("%.1fMB in %d segments of size %d\n", fssize, 536 fs->lfs_nseg, ssize); 537 538 /* 539 * The number of free blocks is set from the number of segments 540 * times the segment size - lfs_minfreesegs (that we never write 541 * because we need to make sure the cleaner can run). Then 542 * we'll subtract off the room for the superblocks ifile entries 543 * and segment usage table, and half a block per segment that can't 544 * be written due to fragmentation. 545 */ 546 fs->lfs_dsize = (fs->lfs_nseg - fs->lfs_minfreeseg) * 547 segtod(fs, 1); 548 fs->lfs_bfree = fs->lfs_dsize; 549 fs->lfs_bfree -= dbtofsb(fs, ((fs->lfs_nseg / 2) << 550 fs->lfs_blktodb)); 551 552 fs->lfs_segtabsz = SEGTABSIZE_SU(fs); 553 fs->lfs_cleansz = CLEANSIZE_SU(fs); 554 if ((fs->lfs_tstamp = time(NULL)) == -1) 555 fatal("time: %s", strerror(errno)); 556 if (version == 1) 557 fs->lfs_otstamp = fs->lfs_tstamp; 558 559 if ((sb_interval = fs->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 560 sb_interval = LFS_MIN_SBINTERVAL; 561 562 /* 563 * Figure out where the superblocks are going to live. 564 * 565 * Make segment 0 start at either zero, or LFS_LABELPAD, or 566 * >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0 567 * from having half a superblock in it. 568 */ 569 if (fsbtodb(fs, dbtofsb(fs, start)) != start) 570 fatal("Segment 0 offset is not multiple of frag size\n"); 571 if (start != 0 && dbtob(start) != LFS_LABELPAD && 572 dbtob(start) < LFS_SBPAD + LFS_LABELPAD) { 573 fatal("Using flags \"-O %" PRId64 "\" would result in the " 574 "first segment containing only\npart of a superblock. " 575 "Please choose an offset of 0, %d, or %d or more,\n", 576 start, btodb(LFS_LABELPAD), 577 btodb(LFS_LABELPAD + LFS_SBPAD)); 578 } 579 fs->lfs_sboffs[0] = label_fsb; 580 if (version == 1) 581 fs->lfs_start = fs->lfs_sboffs[0]; 582 else 583 fs->lfs_start = dbtofsb(fs, start); 584 fs->lfs_dsize -= sb_fsb; 585 for (i = 1; i < LFS_MAXNUMSB; i++) { 586 sb_addr = ((i * sb_interval) * segtod(fs, 1)) 587 + fs->lfs_sboffs[0]; 588 /* Segment 0 eats the label, except for version 1 */ 589 if (fs->lfs_version > 1 && fs->lfs_start < label_fsb) 590 sb_addr -= label_fsb - start; 591 if (sb_addr + sizeof(struct dlfs) 592 >= dbtofsb(fs, dkw->dkw_size)) 593 break; 594 fs->lfs_sboffs[i] = sb_addr; 595 fs->lfs_dsize -= sb_fsb; 596 } 597 598 /* We need >= 2 superblocks */ 599 if(fs->lfs_sboffs[1] == 0x0) { 600 fatal("Could not assign a disk address for the second " 601 "superblock.\nPlease decrease the segment size.\n"); 602 } 603 604 fs->lfs_lastseg = sntod(fs, fs->lfs_nseg - 2); 605 fs->lfs_curseg = sntod(fs, fs->lfs_nseg - 1); 606 fs->lfs_offset = sntod(fs, fs->lfs_nseg); 607 fs->lfs_nextseg = sntod(fs, 0); 608 609 /* 610 * Initialize the Ifile inode. Do this before we do anything 611 * with the Ifile or segment tables. 612 */ 613 dip = VTOI(fs->lfs_ivnode)->i_din.ffs1_din = (struct ufs1_dinode *) 614 malloc(sizeof(*dip)); 615 if (dip == NULL) 616 err(1, NULL); 617 memset(dip, 0, sizeof(*dip)); 618 dip->di_mode = IFREG|IREAD|IWRITE; 619 dip->di_flags = SF_IMMUTABLE; 620 make_dinode(LFS_IFILE_INUM, dip, 621 blkstofrags(fs, fs->lfs_cleansz + fs->lfs_segtabsz + 1), fs); 622 dip->di_size = (fs->lfs_cleansz + fs->lfs_segtabsz + 1) << fs->lfs_bshift; 623 for (i = 0; i < NDADDR && i < (dip->di_size >> fs->lfs_bshift); i++) 624 VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = fs->lfs_bsize; 625 626 /* 627 * Set up in-superblock segment usage cache 628 */ 629 fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *)); 630 if (fs->lfs_suflags == NULL) 631 err(1, NULL); 632 fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t)); 633 if (fs->lfs_suflags[0] == NULL) 634 err(1, NULL); 635 fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t)); 636 if (fs->lfs_suflags[1] == NULL) 637 err(1, NULL); 638 639 /* 640 * Initialize the cleanerinfo block 641 */ 642 LFS_CLEANERINFO(cip, fs, bp); 643 cip->clean = fs->lfs_nseg; 644 cip->dirty = 0; 645 if (version > 1) { 646 cip->free_head = HIGHEST_USED_INO + 1; 647 cip->free_tail = fs->lfs_ifpb - 1; 648 } 649 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1); 650 651 /* 652 * Run through segment table and initialize that 653 */ 654 for (i = j = 0; i < fs->lfs_nseg; i++) { 655 LFS_SEGENTRY(segp, fs, i, bp); 656 657 if (i == 0 && 658 fs->lfs_start < btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) { 659 segp->su_flags = SEGUSE_SUPERBLOCK; 660 fs->lfs_bfree -= sb_fsb; 661 ++j; 662 } 663 if (i > 0) { 664 if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) { 665 segp->su_flags = SEGUSE_SUPERBLOCK; 666 fs->lfs_bfree -= sb_fsb; 667 ++j; 668 } else 669 segp->su_flags = 0; 670 } 671 segp->su_lastmod = 0; 672 segp->su_nbytes = 0; 673 segp->su_ninos = 0; 674 segp->su_nsums = 0; 675 676 LFS_WRITESEGENTRY(segp, fs, i, bp); 677 } 678 679 /* Initialize root directory */ 680 vp = lfs_raw_vget(fs, ROOTINO, devfd, 0x0); 681 dip = VTOI(vp)->i_din.ffs1_din; 682 make_dinode(ROOTINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs); 683 dip->di_mode = IFDIR | UMASK; 684 VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ; 685#ifdef MAKE_LF_DIR 686 VTOI(vp)->i_nlink = dip->di_nlink = 3; 687#else 688 VTOI(vp)->i_nlink = dip->di_nlink = 2; 689#endif 690 VTOI(vp)->i_lfs_effnblks = dip->di_blocks = 691 btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize)); 692 for (i = 0; i < NDADDR && i < howmany(DIRBLKSIZ, fs->lfs_bsize); i++) 693 VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize; 694 if (DIRBLKSIZ < fs->lfs_bsize) 695 VTOI(vp)->i_lfs_fragsize[i - 1] = 696 roundup(DIRBLKSIZ,fs->lfs_fsize); 697 bread(vp, 0, fs->lfs_fsize, NOCRED, 0, &bp); 698 make_dir(bp->b_data, lfs_root_dir, 699 sizeof(lfs_root_dir) / sizeof(struct direct)); 700 VOP_BWRITE(bp); 701 702#ifdef MAKE_LF_DIR 703 /* Initialize lost+found directory */ 704 vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0); 705 dip = VTOI(vp)->i_din.ffs1_din; 706 make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs); 707 dip->di_mode = IFDIR | UMASK; 708 VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ; 709 VTOI(vp)->i_nlink = dip->di_nlink = 2; 710 VTOI(vp)->i_lfs_effnblks = dip->di_blocks = 711 btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize)); 712 for (i = 0; i < NDADDR && i < howmany(DIRBLKSIZ, fs->lfs_bsize); i++) 713 VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize; 714 if (DIRBLKSIZ < fs->lfs_bsize) 715 VTOI(vp)->i_lfs_fragsize[i - 1] = 716 roundup(DIRBLKSIZ,fs->lfs_fsize); 717 bread(vp, 0, fs->lfs_fsize, NOCRED, 0, &bp); 718 make_dir(bp->b_data, lfs_lf_dir, 719 sizeof(lfs_lf_dir) / sizeof(struct direct)); 720 VOP_BWRITE(bp); 721#endif /* MAKE_LF_DIR */ 722 723 /* Set their IFILE entry version numbers to 1 */ 724 LFS_IENTRY(ip, fs, 1, bp); 725 if (version == 1) { 726 ip_v1 = (IFILE_V1 *)ip; 727 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) { 728 ip_v1->if_version = 1; 729 ip_v1->if_daddr = 0x0; 730 ip_v1->if_nextfree = 0; 731 ++ip_v1; 732 } 733 } else { 734 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) { 735 ip->if_version = 1; 736 ip->if_daddr = 0x0; 737 ip->if_nextfree = 0; 738 ++ip; 739 } 740 } 741 /* Link remaining IFILE entries in free list */ 742 if (version == 1) { 743 for (; 744 i < fs->lfs_ifpb; ++ip_v1) { 745 ip_v1->if_version = 1; 746 ip_v1->if_daddr = LFS_UNUSED_DADDR; 747 ip_v1->if_nextfree = ++i; 748 } 749 --ip_v1; 750 ip_v1->if_nextfree = LFS_UNUSED_INUM; 751 } else { 752 for (; 753 i < fs->lfs_ifpb; ++ip) { 754 ip->if_version = 1; 755 ip->if_daddr = LFS_UNUSED_DADDR; 756 ip->if_nextfree = ++i; 757 } 758 --ip; 759 ip->if_nextfree = LFS_UNUSED_INUM; 760 } 761 VOP_BWRITE(bp); 762 763 /* Write it all to disk. */ 764 if (!Nflag) 765 lfs_segwrite(fs, SEGM_CKP); 766 767 /* 768 * Now that we've written everything, look to see what's available 769 * for writing. 770 */ 771 fs->lfs_avail = 0; 772 bb = ubb = dmeta = 0; 773 for (i = 0; i < fs->lfs_nseg; i++) { 774 LFS_SEGENTRY(segp, fs, i, bp); 775 if (segp->su_flags & SEGUSE_DIRTY) { 776 bb += btofsb(fs, segp->su_nbytes + 777 segp->su_nsums * fs->lfs_sumsize); 778 ubb += btofsb(fs, segp->su_nbytes + 779 segp->su_nsums * fs->lfs_sumsize + 780 segp->su_ninos * fs->lfs_ibsize); 781 dmeta += btofsb(fs, 782 fs->lfs_sumsize * segp->su_nsums); 783 dmeta += btofsb(fs, 784 fs->lfs_ibsize * segp->su_ninos); 785 } else { 786 fs->lfs_avail += segtod(fs, 1); 787 if (segp->su_flags & SEGUSE_SUPERBLOCK) 788 fs->lfs_avail -= btofsb(fs, LFS_SBPAD); 789 if (i == 0 && fs->lfs_version > 1 && 790 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) 791 fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) - 792 fs->lfs_start; 793 } 794 brelse(bp, 0); 795 } 796 /* Also may be available bytes in current seg */ 797 i = dtosn(fs, fs->lfs_offset); 798 fs->lfs_avail += sntod(fs, i + 1) - fs->lfs_offset; 799 /* But do not count minfreesegs */ 800 fs->lfs_avail -= segtod(fs, (fs->lfs_minfreeseg - (fs->lfs_minfreeseg / 2))); 801 802 labelskew = 0; 803 if (fs->lfs_version > 1 && fs->lfs_start < btofsb(fs, LFS_LABELPAD)) 804 labelskew = btofsb(fs, LFS_LABELPAD); 805 fs->lfs_bfree = fs->lfs_dsize - labelskew - (ubb + bb) / 2; 806 807 /* Put that in the Ifile version too, and write it */ 808 LFS_CLEANERINFO(cip, fs, bp); 809 cip->bfree = fs->lfs_bfree; 810 cip->avail = fs->lfs_avail; 811 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1); 812 if (!Nflag) 813 lfs_segwrite(fs, SEGM_CKP); 814 815 /* 816 * Finally write out superblocks. 817 */ 818 printf("super-block backups (for fsck -b #) at:\n"); 819 curw = 0; 820 for (i = 0; i < LFS_MAXNUMSB; i++) { 821 seg_addr = fs->lfs_sboffs[i]; 822 if (seg_addr == 0) 823 break; 824 825 if (i != 0) 826 curw += printf(", "); 827 ww = snprintf(tbuf, sizeof(tbuf), "%lld", 828 (long long)fsbtodb(fs, seg_addr)); 829 curw += ww; 830 if (curw >= 78) { 831 printf("\n%s", tbuf); 832 curw = ww; 833 } else 834 printf("%s", tbuf); 835 fflush(stdout); 836 837 /* Leave the time stamp on the alt sb, zero the rest */ 838 if (i == 2) { 839 fs->lfs_tstamp = 0; 840 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs)); 841 } 842 if (!Nflag) 843 lfs_writesuper(fs, seg_addr); 844 } 845 printf(".\n"); 846 847 return 0; 848} 849 850/* 851 * Compatibility with fsck_lfs, since the "generic" LFS userland code uses it. 852 */ 853void 854pwarn(const char *fmt, ...) 855{ 856 va_list ap; 857 858 va_start(ap, fmt); 859 vfprintf(stderr, fmt, ap); 860 va_end(ap); 861} 862