1 /* 2 * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz 3 * Copyright (c) 1980, 1989, 1993 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgment: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors, as well as Christoph 21 * Herrmann and Thomas-Henning von Kamptz. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $ 39 * 40 */ 41 42 #ifndef lint 43 static const char copyright[] = 44 "@(#) Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz\n\ 45 Copyright (c) 1980, 1989, 1993 The Regents of the University of California.\n\ 46 All rights reserved.\n"; 47 #endif /* not lint */ 48 49 #include <sys/cdefs.h> 50 __FBSDID("$FreeBSD$"); 51 52 /* ********************************************************** INCLUDES ***** */ 53 #include <sys/param.h> 54 #include <sys/disklabel.h> 55 #include <sys/ioctl.h> 56 #include <sys/stat.h> 57 #include <sys/disk.h> 58 59 #include <stdio.h> 60 #include <paths.h> 61 #include <ctype.h> 62 #include <err.h> 63 #include <fcntl.h> 64 #include <limits.h> 65 #include <stdlib.h> 66 #include <stdint.h> 67 #include <string.h> 68 #include <time.h> 69 #include <unistd.h> 70 #include <ufs/ufs/dinode.h> 71 #include <ufs/ffs/fs.h> 72 73 #include "debug.h" 74 75 /* *************************************************** GLOBALS & TYPES ***** */ 76 #ifdef FS_DEBUG 77 int _dbg_lvl_ = (DL_INFO); /* DL_TRC */ 78 #endif /* FS_DEBUG */ 79 80 static union { 81 struct fs fs; 82 char pad[SBLOCKSIZE]; 83 } fsun1, fsun2; 84 #define sblock fsun1.fs /* the new superblock */ 85 #define osblock fsun2.fs /* the old superblock */ 86 87 /* 88 * Possible superblock locations ordered from most to least likely. 89 */ 90 static int sblock_try[] = SBLOCKSEARCH; 91 static ufs2_daddr_t sblockloc; 92 93 static union { 94 struct cg cg; 95 char pad[MAXBSIZE]; 96 } cgun1, cgun2; 97 #define acg cgun1.cg /* a cylinder cgroup (new) */ 98 #define aocg cgun2.cg /* an old cylinder group */ 99 100 static char ablk[MAXBSIZE]; /* a block */ 101 102 static struct csum *fscs; /* cylinder summary */ 103 104 union dinode { 105 struct ufs1_dinode dp1; 106 struct ufs2_dinode dp2; 107 }; 108 #define DIP(dp, field) \ 109 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 110 (uint32_t)(dp)->dp1.field : (dp)->dp2.field) 111 #define DIP_SET(dp, field, val) do { \ 112 if (sblock.fs_magic == FS_UFS1_MAGIC) \ 113 (dp)->dp1.field = (val); \ 114 else \ 115 (dp)->dp2.field = (val); \ 116 } while (0) 117 static ufs2_daddr_t inoblk; /* inode block address */ 118 static char inobuf[MAXBSIZE]; /* inode block */ 119 ino_t maxino; /* last valid inode */ 120 static int unlabeled; /* unlabeled partition, e.g. vinum volume etc. */ 121 122 /* 123 * An array of elements of type struct gfs_bpp describes all blocks to 124 * be relocated in order to free the space needed for the cylinder group 125 * summary for all cylinder groups located in the first cylinder group. 126 */ 127 struct gfs_bpp { 128 ufs2_daddr_t old; /* old block number */ 129 ufs2_daddr_t new; /* new block number */ 130 #define GFS_FL_FIRST 1 131 #define GFS_FL_LAST 2 132 unsigned int flags; /* special handling required */ 133 int found; /* how many references were updated */ 134 }; 135 136 /* ******************************************************** PROTOTYPES ***** */ 137 static void growfs(int, int, unsigned int); 138 static void rdfs(ufs2_daddr_t, size_t, void *, int); 139 static void wtfs(ufs2_daddr_t, size_t, void *, int, unsigned int); 140 static ufs2_daddr_t alloc(void); 141 static int charsperline(void); 142 static void usage(void); 143 static int isblock(struct fs *, unsigned char *, int); 144 static void clrblock(struct fs *, unsigned char *, int); 145 static void setblock(struct fs *, unsigned char *, int); 146 static void initcg(int, time_t, int, unsigned int); 147 static void updjcg(int, time_t, int, int, unsigned int); 148 static void updcsloc(time_t, int, int, unsigned int); 149 static struct disklabel *get_disklabel(int); 150 static void return_disklabel(int, struct disklabel *, unsigned int); 151 static union dinode *ginode(ino_t, int, int); 152 static void frag_adjust(ufs2_daddr_t, int); 153 static int cond_bl_upd(ufs2_daddr_t *, struct gfs_bpp *, int, int, 154 unsigned int); 155 static void updclst(int); 156 static void updrefs(int, ino_t, struct gfs_bpp *, int, int, unsigned int); 157 static void indirchk(ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t, ufs_lbn_t, 158 struct gfs_bpp *, int, int, unsigned int); 159 static void get_dev_size(int, int *); 160 161 /* ************************************************************ growfs ***** */ 162 /* 163 * Here we actually start growing the file system. We basically read the 164 * cylinder summary from the first cylinder group as we want to update 165 * this on the fly during our various operations. First we handle the 166 * changes in the former last cylinder group. Afterwards we create all new 167 * cylinder groups. Now we handle the cylinder group containing the 168 * cylinder summary which might result in a relocation of the whole 169 * structure. In the end we write back the updated cylinder summary, the 170 * new superblock, and slightly patched versions of the super block 171 * copies. 172 */ 173 static void 174 growfs(int fsi, int fso, unsigned int Nflag) 175 { 176 DBG_FUNC("growfs") 177 int i; 178 int cylno, j; 179 time_t utime; 180 int width; 181 char tmpbuf[100]; 182 #ifdef FSIRAND 183 static int randinit=0; 184 185 DBG_ENTER; 186 187 if (!randinit) { 188 randinit = 1; 189 srandomdev(); 190 } 191 #else /* not FSIRAND */ 192 193 DBG_ENTER; 194 195 #endif /* FSIRAND */ 196 time(&utime); 197 198 /* 199 * Get the cylinder summary into the memory. 200 */ 201 fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize); 202 if(fscs == NULL) { 203 errx(1, "calloc failed"); 204 } 205 for (i = 0; i < osblock.fs_cssize; i += osblock.fs_bsize) { 206 rdfs(fsbtodb(&osblock, osblock.fs_csaddr + 207 numfrags(&osblock, i)), (size_t)MIN(osblock.fs_cssize - i, 208 osblock.fs_bsize), (void *)(((char *)fscs)+i), fsi); 209 } 210 211 #ifdef FS_DEBUG 212 { 213 struct csum *dbg_csp; 214 int dbg_csc; 215 char dbg_line[80]; 216 217 dbg_csp=fscs; 218 for(dbg_csc=0; dbg_csc<osblock.fs_ncg; dbg_csc++) { 219 snprintf(dbg_line, sizeof(dbg_line), 220 "%d. old csum in old location", dbg_csc); 221 DBG_DUMP_CSUM(&osblock, 222 dbg_line, 223 dbg_csp++); 224 } 225 } 226 #endif /* FS_DEBUG */ 227 DBG_PRINT0("fscs read\n"); 228 229 /* 230 * Do all needed changes in the former last cylinder group. 231 */ 232 updjcg(osblock.fs_ncg-1, utime, fsi, fso, Nflag); 233 234 /* 235 * Dump out summary information about file system. 236 */ 237 # define B2MBFACTOR (1 / (1024.0 * 1024.0)) 238 printf("growfs: %.1fMB (%jd sectors) block size %d, fragment size %d\n", 239 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 240 (intmax_t)fsbtodb(&sblock, sblock.fs_size), sblock.fs_bsize, 241 sblock.fs_fsize); 242 printf("\tusing %d cylinder groups of %.2fMB, %d blks, %d inodes.\n", 243 sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 244 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg); 245 if (sblock.fs_flags & FS_DOSOFTDEP) 246 printf("\twith soft updates\n"); 247 # undef B2MBFACTOR 248 249 /* 250 * Now build the cylinders group blocks and 251 * then print out indices of cylinder groups. 252 */ 253 printf("super-block backups (for fsck -b #) at:\n"); 254 i = 0; 255 width = charsperline(); 256 257 /* 258 * Iterate for only the new cylinder groups. 259 */ 260 for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) { 261 initcg(cylno, utime, fso, Nflag); 262 j = sprintf(tmpbuf, " %jd%s", 263 (intmax_t)fsbtodb(&sblock, cgsblock(&sblock, cylno)), 264 cylno < (sblock.fs_ncg-1) ? "," : "" ); 265 if (i + j >= width) { 266 printf("\n"); 267 i = 0; 268 } 269 i += j; 270 printf("%s", tmpbuf); 271 fflush(stdout); 272 } 273 printf("\n"); 274 275 /* 276 * Do all needed changes in the first cylinder group. 277 * allocate blocks in new location 278 */ 279 updcsloc(utime, fsi, fso, Nflag); 280 281 /* 282 * Now write the cylinder summary back to disk. 283 */ 284 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) { 285 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), 286 (size_t)MIN(sblock.fs_cssize - i, sblock.fs_bsize), 287 (void *)(((char *)fscs) + i), fso, Nflag); 288 } 289 DBG_PRINT0("fscs written\n"); 290 291 #ifdef FS_DEBUG 292 { 293 struct csum *dbg_csp; 294 int dbg_csc; 295 char dbg_line[80]; 296 297 dbg_csp=fscs; 298 for(dbg_csc=0; dbg_csc<sblock.fs_ncg; dbg_csc++) { 299 snprintf(dbg_line, sizeof(dbg_line), 300 "%d. new csum in new location", dbg_csc); 301 DBG_DUMP_CSUM(&sblock, 302 dbg_line, 303 dbg_csp++); 304 } 305 } 306 #endif /* FS_DEBUG */ 307 308 /* 309 * Now write the new superblock back to disk. 310 */ 311 sblock.fs_time = utime; 312 wtfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag); 313 DBG_PRINT0("sblock written\n"); 314 DBG_DUMP_FS(&sblock, 315 "new initial sblock"); 316 317 /* 318 * Clean up the dynamic fields in our superblock copies. 319 */ 320 sblock.fs_fmod = 0; 321 sblock.fs_clean = 1; 322 sblock.fs_ronly = 0; 323 sblock.fs_cgrotor = 0; 324 sblock.fs_state = 0; 325 memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt)); 326 sblock.fs_flags &= FS_DOSOFTDEP; 327 328 /* 329 * XXX 330 * The following fields are currently distributed from the superblock 331 * to the copies: 332 * fs_minfree 333 * fs_rotdelay 334 * fs_maxcontig 335 * fs_maxbpg 336 * fs_minfree, 337 * fs_optim 338 * fs_flags regarding SOFTPDATES 339 * 340 * We probably should rather change the summary for the cylinder group 341 * statistics here to the value of what would be in there, if the file 342 * system were created initially with the new size. Therefor we still 343 * need to find an easy way of calculating that. 344 * Possibly we can try to read the first superblock copy and apply the 345 * "diffed" stats between the old and new superblock by still copying 346 * certain parameters onto that. 347 */ 348 349 /* 350 * Write out the duplicate super blocks. 351 */ 352 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 353 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), 354 (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag); 355 } 356 DBG_PRINT0("sblock copies written\n"); 357 DBG_DUMP_FS(&sblock, 358 "new other sblocks"); 359 360 DBG_LEAVE; 361 return; 362 } 363 364 /* ************************************************************ initcg ***** */ 365 /* 366 * This creates a new cylinder group structure, for more details please see 367 * the source of newfs(8), as this function is taken over almost unchanged. 368 * As this is never called for the first cylinder group, the special 369 * provisions for that case are removed here. 370 */ 371 static void 372 initcg(int cylno, time_t utime, int fso, unsigned int Nflag) 373 { 374 DBG_FUNC("initcg") 375 static void *iobuf; 376 long d, dlower, dupper, blkno, start; 377 ufs2_daddr_t i, cbase, dmax; 378 struct ufs1_dinode *dp1; 379 struct csum *cs; 380 381 if (iobuf == NULL && (iobuf = malloc(sblock.fs_bsize)) == NULL) { 382 errx(37, "panic: cannot allocate I/O buffer"); 383 } 384 /* 385 * Determine block bounds for cylinder group. 386 * Allow space for super block summary information in first 387 * cylinder group. 388 */ 389 cbase = cgbase(&sblock, cylno); 390 dmax = cbase + sblock.fs_fpg; 391 if (dmax > sblock.fs_size) 392 dmax = sblock.fs_size; 393 dlower = cgsblock(&sblock, cylno) - cbase; 394 dupper = cgdmin(&sblock, cylno) - cbase; 395 if (cylno == 0) /* XXX fscs may be relocated */ 396 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 397 cs = &fscs[cylno]; 398 memset(&acg, 0, sblock.fs_cgsize); 399 /* 400 * Note that we do not set cg_initediblk at all. 401 * In this extension of a previous filesystem 402 * we have no inodes initialized for the cylinder 403 * group at all. The first access to that cylinder 404 * group will do the correct initialization. 405 */ 406 acg.cg_time = utime; 407 acg.cg_magic = CG_MAGIC; 408 acg.cg_cgx = cylno; 409 acg.cg_niblk = sblock.fs_ipg; 410 acg.cg_ndblk = dmax - cbase; 411 if (sblock.fs_contigsumsize > 0) 412 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag; 413 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); 414 if (sblock.fs_magic == FS_UFS2_MAGIC) { 415 acg.cg_iusedoff = start; 416 } else { 417 acg.cg_old_ncyl = sblock.fs_old_cpg; 418 acg.cg_old_time = acg.cg_time; 419 acg.cg_time = 0; 420 acg.cg_old_niblk = acg.cg_niblk; 421 acg.cg_niblk = 0; 422 acg.cg_old_btotoff = start; 423 acg.cg_old_boff = acg.cg_old_btotoff + 424 sblock.fs_old_cpg * sizeof(int32_t); 425 acg.cg_iusedoff = acg.cg_old_boff + 426 sblock.fs_old_cpg * sizeof(u_int16_t); 427 } 428 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); 429 acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT); 430 if (sblock.fs_contigsumsize > 0) { 431 acg.cg_clustersumoff = 432 roundup(acg.cg_nextfreeoff, sizeof(u_int32_t)); 433 acg.cg_clustersumoff -= sizeof(u_int32_t); 434 acg.cg_clusteroff = acg.cg_clustersumoff + 435 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t); 436 acg.cg_nextfreeoff = acg.cg_clusteroff + 437 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT); 438 } 439 if (acg.cg_nextfreeoff > sblock.fs_cgsize) { 440 /* 441 * This should never happen as we would have had that panic 442 * already on file system creation 443 */ 444 errx(37, "panic: cylinder group too big"); 445 } 446 acg.cg_cs.cs_nifree += sblock.fs_ipg; 447 if (cylno == 0) 448 for (i = 0; i < ROOTINO; i++) { 449 setbit(cg_inosused(&acg), i); 450 acg.cg_cs.cs_nifree--; 451 } 452 /* 453 * XXX Newfs writes out two blocks of initialized inodes 454 * unconditionally. Should we check here to make sure that they 455 * were actually written? 456 */ 457 if (sblock.fs_magic == FS_UFS1_MAGIC) { 458 bzero(iobuf, sblock.fs_bsize); 459 for (i = 2 * sblock.fs_frag; i < sblock.fs_ipg / INOPF(&sblock); 460 i += sblock.fs_frag) { 461 dp1 = (struct ufs1_dinode *)iobuf; 462 #ifdef FSIRAND 463 for (j = 0; j < INOPB(&sblock); j++) { 464 dp1->di_gen = random(); 465 dp1++; 466 } 467 #endif 468 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), 469 sblock.fs_bsize, iobuf, fso, Nflag); 470 } 471 } 472 if (cylno > 0) { 473 /* 474 * In cylno 0, beginning space is reserved 475 * for boot and super blocks. 476 */ 477 for (d = 0; d < dlower; d += sblock.fs_frag) { 478 blkno = d / sblock.fs_frag; 479 setblock(&sblock, cg_blksfree(&acg), blkno); 480 if (sblock.fs_contigsumsize > 0) 481 setbit(cg_clustersfree(&acg), blkno); 482 acg.cg_cs.cs_nbfree++; 483 } 484 sblock.fs_dsize += dlower; 485 } 486 sblock.fs_dsize += acg.cg_ndblk - dupper; 487 if ((i = dupper % sblock.fs_frag)) { 488 acg.cg_frsum[sblock.fs_frag - i]++; 489 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 490 setbit(cg_blksfree(&acg), dupper); 491 acg.cg_cs.cs_nffree++; 492 } 493 } 494 for (d = dupper; d + sblock.fs_frag <= acg.cg_ndblk; 495 d += sblock.fs_frag) { 496 blkno = d / sblock.fs_frag; 497 setblock(&sblock, cg_blksfree(&acg), blkno); 498 if (sblock.fs_contigsumsize > 0) 499 setbit(cg_clustersfree(&acg), blkno); 500 acg.cg_cs.cs_nbfree++; 501 } 502 if (d < acg.cg_ndblk) { 503 acg.cg_frsum[acg.cg_ndblk - d]++; 504 for (; d < acg.cg_ndblk; d++) { 505 setbit(cg_blksfree(&acg), d); 506 acg.cg_cs.cs_nffree++; 507 } 508 } 509 if (sblock.fs_contigsumsize > 0) { 510 int32_t *sump = cg_clustersum(&acg); 511 u_char *mapp = cg_clustersfree(&acg); 512 int map = *mapp++; 513 int bit = 1; 514 int run = 0; 515 516 for (i = 0; i < acg.cg_nclusterblks; i++) { 517 if ((map & bit) != 0) 518 run++; 519 else if (run != 0) { 520 if (run > sblock.fs_contigsumsize) 521 run = sblock.fs_contigsumsize; 522 sump[run]++; 523 run = 0; 524 } 525 if ((i & (CHAR_BIT - 1)) != CHAR_BIT - 1) 526 bit <<= 1; 527 else { 528 map = *mapp++; 529 bit = 1; 530 } 531 } 532 if (run != 0) { 533 if (run > sblock.fs_contigsumsize) 534 run = sblock.fs_contigsumsize; 535 sump[run]++; 536 } 537 } 538 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir; 539 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree; 540 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree; 541 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree; 542 *cs = acg.cg_cs; 543 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), 544 sblock.fs_bsize, (char *)&acg, fso, Nflag); 545 DBG_DUMP_CG(&sblock, 546 "new cg", 547 &acg); 548 549 DBG_LEAVE; 550 return; 551 } 552 553 /* ******************************************************* frag_adjust ***** */ 554 /* 555 * Here we add or subtract (sign +1/-1) the available fragments in a given 556 * block to or from the fragment statistics. By subtracting before and adding 557 * after an operation on the free frag map we can easy update the fragment 558 * statistic, which seems to be otherwise a rather complex operation. 559 */ 560 static void 561 frag_adjust(ufs2_daddr_t frag, int sign) 562 { 563 DBG_FUNC("frag_adjust") 564 int fragsize; 565 int f; 566 567 DBG_ENTER; 568 569 fragsize=0; 570 /* 571 * Here frag only needs to point to any fragment in the block we want 572 * to examine. 573 */ 574 for(f=rounddown(frag, sblock.fs_frag); 575 f<roundup(frag+1, sblock.fs_frag); 576 f++) { 577 /* 578 * Count contiguous free fragments. 579 */ 580 if(isset(cg_blksfree(&acg), f)) { 581 fragsize++; 582 } else { 583 if(fragsize && fragsize<sblock.fs_frag) { 584 /* 585 * We found something in between. 586 */ 587 acg.cg_frsum[fragsize]+=sign; 588 DBG_PRINT2("frag_adjust [%d]+=%d\n", 589 fragsize, 590 sign); 591 } 592 fragsize=0; 593 } 594 } 595 if(fragsize && fragsize<sblock.fs_frag) { 596 /* 597 * We found something. 598 */ 599 acg.cg_frsum[fragsize]+=sign; 600 DBG_PRINT2("frag_adjust [%d]+=%d\n", 601 fragsize, 602 sign); 603 } 604 DBG_PRINT2("frag_adjust [[%d]]+=%d\n", 605 fragsize, 606 sign); 607 608 DBG_LEAVE; 609 return; 610 } 611 612 /* ******************************************************* cond_bl_upd ***** */ 613 /* 614 * Here we conditionally update a pointer to a fragment. We check for all 615 * relocated blocks if any of its fragments is referenced by the current 616 * field, and update the pointer to the respective fragment in our new 617 * block. If we find a reference we write back the block immediately, 618 * as there is no easy way for our general block reading engine to figure 619 * out if a write back operation is needed. 620 */ 621 static int 622 cond_bl_upd(ufs2_daddr_t *block, struct gfs_bpp *field, int fsi, int fso, 623 unsigned int Nflag) 624 { 625 DBG_FUNC("cond_bl_upd") 626 struct gfs_bpp *f; 627 ufs2_daddr_t src, dst; 628 int fragnum; 629 void *ibuf; 630 631 DBG_ENTER; 632 633 for (f = field; f->old != 0; f++) { 634 src = *block; 635 if (fragstoblks(&sblock, src) != f->old) 636 continue; 637 /* 638 * The fragment is part of the block, so update. 639 */ 640 dst = blkstofrags(&sblock, f->new); 641 fragnum = fragnum(&sblock, src); 642 *block = dst + fragnum; 643 f->found++; 644 DBG_PRINT3("scg (%jd->%jd)[%d] reference updated\n", 645 (intmax_t)f->old, 646 (intmax_t)f->new, 647 fragnum); 648 649 /* 650 * Copy the block back immediately. 651 * 652 * XXX If src is is from an indirect block we have 653 * to implement copy on write here in case of 654 * active snapshots. 655 */ 656 ibuf = malloc(sblock.fs_bsize); 657 if (!ibuf) 658 errx(1, "malloc failed"); 659 src -= fragnum; 660 rdfs(fsbtodb(&sblock, src), (size_t)sblock.fs_bsize, ibuf, fsi); 661 wtfs(dst, (size_t)sblock.fs_bsize, ibuf, fso, Nflag); 662 free(ibuf); 663 /* 664 * The same block can't be found again in this loop. 665 */ 666 return (1); 667 } 668 669 DBG_LEAVE; 670 return (0); 671 } 672 673 /* ************************************************************ updjcg ***** */ 674 /* 675 * Here we do all needed work for the former last cylinder group. It has to be 676 * changed in any case, even if the file system ended exactly on the end of 677 * this group, as there is some slightly inconsistent handling of the number 678 * of cylinders in the cylinder group. We start again by reading the cylinder 679 * group from disk. If the last block was not fully available, we first handle 680 * the missing fragments, then we handle all new full blocks in that file 681 * system and finally we handle the new last fragmented block in the file 682 * system. We again have to handle the fragment statistics rotational layout 683 * tables and cluster summary during all those operations. 684 */ 685 static void 686 updjcg(int cylno, time_t utime, int fsi, int fso, unsigned int Nflag) 687 { 688 DBG_FUNC("updjcg") 689 ufs2_daddr_t cbase, dmax, dupper; 690 struct csum *cs; 691 int i,k; 692 int j=0; 693 694 DBG_ENTER; 695 696 /* 697 * Read the former last (joining) cylinder group from disk, and make 698 * a copy. 699 */ 700 rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)), 701 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi); 702 DBG_PRINT0("jcg read\n"); 703 DBG_DUMP_CG(&sblock, 704 "old joining cg", 705 &aocg); 706 707 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2)); 708 709 /* 710 * If the cylinder group had already its new final size almost 711 * nothing is to be done ... except: 712 * For some reason the value of cg_ncyl in the last cylinder group has 713 * to be zero instead of fs_cpg. As this is now no longer the last 714 * cylinder group we have to change that value now to fs_cpg. 715 */ 716 717 if(cgbase(&osblock, cylno+1) == osblock.fs_size) { 718 if (sblock.fs_magic == FS_UFS1_MAGIC) 719 acg.cg_old_ncyl=sblock.fs_old_cpg; 720 721 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), 722 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag); 723 DBG_PRINT0("jcg written\n"); 724 DBG_DUMP_CG(&sblock, 725 "new joining cg", 726 &acg); 727 728 DBG_LEAVE; 729 return; 730 } 731 732 /* 733 * Set up some variables needed later. 734 */ 735 cbase = cgbase(&sblock, cylno); 736 dmax = cbase + sblock.fs_fpg; 737 if (dmax > sblock.fs_size) 738 dmax = sblock.fs_size; 739 dupper = cgdmin(&sblock, cylno) - cbase; 740 if (cylno == 0) { /* XXX fscs may be relocated */ 741 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 742 } 743 744 /* 745 * Set pointer to the cylinder summary for our cylinder group. 746 */ 747 cs = fscs + cylno; 748 749 /* 750 * Touch the cylinder group, update all fields in the cylinder group as 751 * needed, update the free space in the superblock. 752 */ 753 acg.cg_time = utime; 754 if (cylno == sblock.fs_ncg - 1) { 755 /* 756 * This is still the last cylinder group. 757 */ 758 if (sblock.fs_magic == FS_UFS1_MAGIC) 759 acg.cg_old_ncyl = 760 sblock.fs_old_ncyl % sblock.fs_old_cpg; 761 } else { 762 acg.cg_old_ncyl = sblock.fs_old_cpg; 763 } 764 DBG_PRINT2("jcg dbg: %d %u", 765 cylno, 766 sblock.fs_ncg); 767 #ifdef FS_DEBUG 768 if (sblock.fs_magic == FS_UFS1_MAGIC) 769 DBG_PRINT2("%d %u", 770 acg.cg_old_ncyl, 771 sblock.fs_old_cpg); 772 #endif 773 DBG_PRINT0("\n"); 774 acg.cg_ndblk = dmax - cbase; 775 sblock.fs_dsize += acg.cg_ndblk-aocg.cg_ndblk; 776 if (sblock.fs_contigsumsize > 0) { 777 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag; 778 } 779 780 /* 781 * Now we have to update the free fragment bitmap for our new free 782 * space. There again we have to handle the fragmentation and also 783 * the rotational layout tables and the cluster summary. This is 784 * also done per fragment for the first new block if the old file 785 * system end was not on a block boundary, per fragment for the new 786 * last block if the new file system end is not on a block boundary, 787 * and per block for all space in between. 788 * 789 * Handle the first new block here if it was partially available 790 * before. 791 */ 792 if(osblock.fs_size % sblock.fs_frag) { 793 if(roundup(osblock.fs_size, sblock.fs_frag)<=sblock.fs_size) { 794 /* 795 * The new space is enough to fill at least this 796 * block 797 */ 798 j=0; 799 for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag)-1; 800 i>=osblock.fs_size-cbase; 801 i--) { 802 setbit(cg_blksfree(&acg), i); 803 acg.cg_cs.cs_nffree++; 804 j++; 805 } 806 807 /* 808 * Check if the fragment just created could join an 809 * already existing fragment at the former end of the 810 * file system. 811 */ 812 if(isblock(&sblock, cg_blksfree(&acg), 813 ((osblock.fs_size - cgbase(&sblock, cylno))/ 814 sblock.fs_frag))) { 815 /* 816 * The block is now completely available. 817 */ 818 DBG_PRINT0("block was\n"); 819 acg.cg_frsum[osblock.fs_size%sblock.fs_frag]--; 820 acg.cg_cs.cs_nbfree++; 821 acg.cg_cs.cs_nffree-=sblock.fs_frag; 822 k=rounddown(osblock.fs_size-cbase, 823 sblock.fs_frag); 824 updclst((osblock.fs_size-cbase)/sblock.fs_frag); 825 } else { 826 /* 827 * Lets rejoin a possible partially growed 828 * fragment. 829 */ 830 k=0; 831 while(isset(cg_blksfree(&acg), i) && 832 (i>=rounddown(osblock.fs_size-cbase, 833 sblock.fs_frag))) { 834 i--; 835 k++; 836 } 837 if(k) { 838 acg.cg_frsum[k]--; 839 } 840 acg.cg_frsum[k+j]++; 841 } 842 } else { 843 /* 844 * We only grow by some fragments within this last 845 * block. 846 */ 847 for(i=sblock.fs_size-cbase-1; 848 i>=osblock.fs_size-cbase; 849 i--) { 850 setbit(cg_blksfree(&acg), i); 851 acg.cg_cs.cs_nffree++; 852 j++; 853 } 854 /* 855 * Lets rejoin a possible partially growed fragment. 856 */ 857 k=0; 858 while(isset(cg_blksfree(&acg), i) && 859 (i>=rounddown(osblock.fs_size-cbase, 860 sblock.fs_frag))) { 861 i--; 862 k++; 863 } 864 if(k) { 865 acg.cg_frsum[k]--; 866 } 867 acg.cg_frsum[k+j]++; 868 } 869 } 870 871 /* 872 * Handle all new complete blocks here. 873 */ 874 for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag); 875 i+sblock.fs_frag<=dmax-cbase; /* XXX <= or only < ? */ 876 i+=sblock.fs_frag) { 877 j = i / sblock.fs_frag; 878 setblock(&sblock, cg_blksfree(&acg), j); 879 updclst(j); 880 acg.cg_cs.cs_nbfree++; 881 } 882 883 /* 884 * Handle the last new block if there are stll some new fragments left. 885 * Here we don't have to bother about the cluster summary or the even 886 * the rotational layout table. 887 */ 888 if (i < (dmax - cbase)) { 889 acg.cg_frsum[dmax - cbase - i]++; 890 for (; i < dmax - cbase; i++) { 891 setbit(cg_blksfree(&acg), i); 892 acg.cg_cs.cs_nffree++; 893 } 894 } 895 896 sblock.fs_cstotal.cs_nffree += 897 (acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree); 898 sblock.fs_cstotal.cs_nbfree += 899 (acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree); 900 /* 901 * The following statistics are not changed here: 902 * sblock.fs_cstotal.cs_ndir 903 * sblock.fs_cstotal.cs_nifree 904 * As the statistics for this cylinder group are ready, copy it to 905 * the summary information array. 906 */ 907 *cs = acg.cg_cs; 908 909 /* 910 * Write the updated "joining" cylinder group back to disk. 911 */ 912 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize, 913 (void *)&acg, fso, Nflag); 914 DBG_PRINT0("jcg written\n"); 915 DBG_DUMP_CG(&sblock, 916 "new joining cg", 917 &acg); 918 919 DBG_LEAVE; 920 return; 921 } 922 923 /* ********************************************************** updcsloc ***** */ 924 /* 925 * Here we update the location of the cylinder summary. We have two possible 926 * ways of growing the cylinder summary. 927 * (1) We can try to grow the summary in the current location, and relocate 928 * possibly used blocks within the current cylinder group. 929 * (2) Alternatively we can relocate the whole cylinder summary to the first 930 * new completely empty cylinder group. Once the cylinder summary is no 931 * longer in the beginning of the first cylinder group you should never 932 * use a version of fsck which is not aware of the possibility to have 933 * this structure in a non standard place. 934 * Option (1) is considered to be less intrusive to the structure of the file- 935 * system. So we try to stick to that whenever possible. If there is not enough 936 * space in the cylinder group containing the cylinder summary we have to use 937 * method (2). In case of active snapshots in the file system we probably can 938 * completely avoid implementing copy on write if we stick to method (2) only. 939 */ 940 static void 941 updcsloc(time_t utime, int fsi, int fso, unsigned int Nflag) 942 { 943 DBG_FUNC("updcsloc") 944 struct csum *cs; 945 int ocscg, ncscg; 946 int blocks; 947 ufs2_daddr_t cbase, dupper, odupper, d, f, g; 948 int ind; 949 int cylno, inc; 950 struct gfs_bpp *bp; 951 int i, l; 952 int lcs=0; 953 int block; 954 955 DBG_ENTER; 956 957 if(howmany(sblock.fs_cssize, sblock.fs_fsize) == 958 howmany(osblock.fs_cssize, osblock.fs_fsize)) { 959 /* 960 * No new fragment needed. 961 */ 962 DBG_LEAVE; 963 return; 964 } 965 ocscg=dtog(&osblock, osblock.fs_csaddr); 966 cs=fscs+ocscg; 967 blocks = 1+howmany(sblock.fs_cssize, sblock.fs_bsize)- 968 howmany(osblock.fs_cssize, osblock.fs_bsize); 969 970 /* 971 * Read original cylinder group from disk, and make a copy. 972 * XXX If Nflag is set in some very rare cases we now miss 973 * some changes done in updjcg by reading the unmodified 974 * block from disk. 975 */ 976 rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)), 977 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi); 978 DBG_PRINT0("oscg read\n"); 979 DBG_DUMP_CG(&sblock, 980 "old summary cg", 981 &aocg); 982 983 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2)); 984 985 /* 986 * Touch the cylinder group, set up local variables needed later 987 * and update the superblock. 988 */ 989 acg.cg_time = utime; 990 991 /* 992 * XXX In the case of having active snapshots we may need much more 993 * blocks for the copy on write. We need each block twice, and 994 * also up to 8*3 blocks for indirect blocks for all possible 995 * references. 996 */ 997 if(/*((int)sblock.fs_time&0x3)>0||*/ cs->cs_nbfree < blocks) { 998 /* 999 * There is not enough space in the old cylinder group to 1000 * relocate all blocks as needed, so we relocate the whole 1001 * cylinder group summary to a new group. We try to use the 1002 * first complete new cylinder group just created. Within the 1003 * cylinder group we align the area immediately after the 1004 * cylinder group information location in order to be as 1005 * close as possible to the original implementation of ffs. 1006 * 1007 * First we have to make sure we'll find enough space in the 1008 * new cylinder group. If not, then we currently give up. 1009 * We start with freeing everything which was used by the 1010 * fragments of the old cylinder summary in the current group. 1011 * Now we write back the group meta data, read in the needed 1012 * meta data from the new cylinder group, and start allocating 1013 * within that group. Here we can assume, the group to be 1014 * completely empty. Which makes the handling of fragments and 1015 * clusters a lot easier. 1016 */ 1017 DBG_TRC; 1018 if(sblock.fs_ncg-osblock.fs_ncg < 2) { 1019 errx(2, "panic: not enough space"); 1020 } 1021 1022 /* 1023 * Point "d" to the first fragment not used by the cylinder 1024 * summary. 1025 */ 1026 d=osblock.fs_csaddr+(osblock.fs_cssize/osblock.fs_fsize); 1027 1028 /* 1029 * Set up last cluster size ("lcs") already here. Calculate 1030 * the size for the trailing cluster just behind where "d" 1031 * points to. 1032 */ 1033 if(sblock.fs_contigsumsize > 0) { 1034 for(block=howmany(d%sblock.fs_fpg, sblock.fs_frag), 1035 lcs=0; lcs<sblock.fs_contigsumsize; 1036 block++, lcs++) { 1037 if(isclr(cg_clustersfree(&acg), block)){ 1038 break; 1039 } 1040 } 1041 } 1042 1043 /* 1044 * Point "d" to the last frag used by the cylinder summary. 1045 */ 1046 d--; 1047 1048 DBG_PRINT1("d=%jd\n", 1049 (intmax_t)d); 1050 if((d+1)%sblock.fs_frag) { 1051 /* 1052 * The end of the cylinder summary is not a complete 1053 * block. 1054 */ 1055 DBG_TRC; 1056 frag_adjust(d%sblock.fs_fpg, -1); 1057 for(; (d+1)%sblock.fs_frag; d--) { 1058 DBG_PRINT1("d=%jd\n", 1059 (intmax_t)d); 1060 setbit(cg_blksfree(&acg), d%sblock.fs_fpg); 1061 acg.cg_cs.cs_nffree++; 1062 sblock.fs_cstotal.cs_nffree++; 1063 } 1064 /* 1065 * Point "d" to the last fragment of the last 1066 * (incomplete) block of the cylinder summary. 1067 */ 1068 d++; 1069 frag_adjust(d%sblock.fs_fpg, 1); 1070 1071 if(isblock(&sblock, cg_blksfree(&acg), 1072 (d%sblock.fs_fpg)/sblock.fs_frag)) { 1073 DBG_PRINT1("d=%jd\n", (intmax_t)d); 1074 acg.cg_cs.cs_nffree-=sblock.fs_frag; 1075 acg.cg_cs.cs_nbfree++; 1076 sblock.fs_cstotal.cs_nffree-=sblock.fs_frag; 1077 sblock.fs_cstotal.cs_nbfree++; 1078 if(sblock.fs_contigsumsize > 0) { 1079 setbit(cg_clustersfree(&acg), 1080 (d%sblock.fs_fpg)/sblock.fs_frag); 1081 if(lcs < sblock.fs_contigsumsize) { 1082 if(lcs) { 1083 cg_clustersum(&acg) 1084 [lcs]--; 1085 } 1086 lcs++; 1087 cg_clustersum(&acg)[lcs]++; 1088 } 1089 } 1090 } 1091 /* 1092 * Point "d" to the first fragment of the block before 1093 * the last incomplete block. 1094 */ 1095 d--; 1096 } 1097 1098 DBG_PRINT1("d=%jd\n", (intmax_t)d); 1099 for(d=rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr; 1100 d-=sblock.fs_frag) { 1101 DBG_TRC; 1102 DBG_PRINT1("d=%jd\n", (intmax_t)d); 1103 setblock(&sblock, cg_blksfree(&acg), 1104 (d%sblock.fs_fpg)/sblock.fs_frag); 1105 acg.cg_cs.cs_nbfree++; 1106 sblock.fs_cstotal.cs_nbfree++; 1107 if(sblock.fs_contigsumsize > 0) { 1108 setbit(cg_clustersfree(&acg), 1109 (d%sblock.fs_fpg)/sblock.fs_frag); 1110 /* 1111 * The last cluster size is already set up. 1112 */ 1113 if(lcs < sblock.fs_contigsumsize) { 1114 if(lcs) { 1115 cg_clustersum(&acg)[lcs]--; 1116 } 1117 lcs++; 1118 cg_clustersum(&acg)[lcs]++; 1119 } 1120 } 1121 } 1122 *cs = acg.cg_cs; 1123 1124 /* 1125 * Now write the former cylinder group containing the cylinder 1126 * summary back to disk. 1127 */ 1128 wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), 1129 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag); 1130 DBG_PRINT0("oscg written\n"); 1131 DBG_DUMP_CG(&sblock, 1132 "old summary cg", 1133 &acg); 1134 1135 /* 1136 * Find the beginning of the new cylinder group containing the 1137 * cylinder summary. 1138 */ 1139 sblock.fs_csaddr=cgdmin(&sblock, osblock.fs_ncg); 1140 ncscg=dtog(&sblock, sblock.fs_csaddr); 1141 cs=fscs+ncscg; 1142 1143 1144 /* 1145 * If Nflag is specified, we would now read random data instead 1146 * of an empty cg structure from disk. So we can't simulate that 1147 * part for now. 1148 */ 1149 if(Nflag) { 1150 DBG_PRINT0("nscg update skipped\n"); 1151 DBG_LEAVE; 1152 return; 1153 } 1154 1155 /* 1156 * Read the future cylinder group containing the cylinder 1157 * summary from disk, and make a copy. 1158 */ 1159 rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)), 1160 (size_t)sblock.fs_cgsize, (void *)&aocg, fsi); 1161 DBG_PRINT0("nscg read\n"); 1162 DBG_DUMP_CG(&sblock, 1163 "new summary cg", 1164 &aocg); 1165 1166 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2)); 1167 1168 /* 1169 * Allocate all complete blocks used by the new cylinder 1170 * summary. 1171 */ 1172 for(d=sblock.fs_csaddr; d+sblock.fs_frag <= 1173 sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize); 1174 d+=sblock.fs_frag) { 1175 clrblock(&sblock, cg_blksfree(&acg), 1176 (d%sblock.fs_fpg)/sblock.fs_frag); 1177 acg.cg_cs.cs_nbfree--; 1178 sblock.fs_cstotal.cs_nbfree--; 1179 if(sblock.fs_contigsumsize > 0) { 1180 clrbit(cg_clustersfree(&acg), 1181 (d%sblock.fs_fpg)/sblock.fs_frag); 1182 } 1183 } 1184 1185 /* 1186 * Allocate all fragments used by the cylinder summary in the 1187 * last block. 1188 */ 1189 if(d<sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize)) { 1190 for(; d-sblock.fs_csaddr< 1191 sblock.fs_cssize/sblock.fs_fsize; 1192 d++) { 1193 clrbit(cg_blksfree(&acg), d%sblock.fs_fpg); 1194 acg.cg_cs.cs_nffree--; 1195 sblock.fs_cstotal.cs_nffree--; 1196 } 1197 acg.cg_cs.cs_nbfree--; 1198 acg.cg_cs.cs_nffree+=sblock.fs_frag; 1199 sblock.fs_cstotal.cs_nbfree--; 1200 sblock.fs_cstotal.cs_nffree+=sblock.fs_frag; 1201 if(sblock.fs_contigsumsize > 0) { 1202 clrbit(cg_clustersfree(&acg), 1203 (d%sblock.fs_fpg)/sblock.fs_frag); 1204 } 1205 1206 frag_adjust(d%sblock.fs_fpg, +1); 1207 } 1208 /* 1209 * XXX Handle the cluster statistics here in the case this 1210 * cylinder group is now almost full, and the remaining 1211 * space is less then the maximum cluster size. This is 1212 * probably not needed, as you would hardly find a file 1213 * system which has only MAXCSBUFS+FS_MAXCONTIG of free 1214 * space right behind the cylinder group information in 1215 * any new cylinder group. 1216 */ 1217 1218 /* 1219 * Update our statistics in the cylinder summary. 1220 */ 1221 *cs = acg.cg_cs; 1222 1223 /* 1224 * Write the new cylinder group containing the cylinder summary 1225 * back to disk. 1226 */ 1227 wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)), 1228 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag); 1229 DBG_PRINT0("nscg written\n"); 1230 DBG_DUMP_CG(&sblock, 1231 "new summary cg", 1232 &acg); 1233 1234 DBG_LEAVE; 1235 return; 1236 } 1237 /* 1238 * We have got enough of space in the current cylinder group, so we 1239 * can relocate just a few blocks, and let the summary information 1240 * grow in place where it is right now. 1241 */ 1242 DBG_TRC; 1243 1244 cbase = cgbase(&osblock, ocscg); /* old and new are equal */ 1245 dupper = sblock.fs_csaddr - cbase + 1246 howmany(sblock.fs_cssize, sblock.fs_fsize); 1247 odupper = osblock.fs_csaddr - cbase + 1248 howmany(osblock.fs_cssize, osblock.fs_fsize); 1249 1250 sblock.fs_dsize -= dupper-odupper; 1251 1252 /* 1253 * Allocate the space for the array of blocks to be relocated. 1254 */ 1255 bp=(struct gfs_bpp *)malloc(((dupper-odupper)/sblock.fs_frag+2)* 1256 sizeof(struct gfs_bpp)); 1257 if(bp == NULL) { 1258 errx(1, "malloc failed"); 1259 } 1260 memset((char *)bp, 0, ((dupper-odupper)/sblock.fs_frag+2)* 1261 sizeof(struct gfs_bpp)); 1262 1263 /* 1264 * Lock all new frags needed for the cylinder group summary. This is 1265 * done per fragment in the first and last block of the new required 1266 * area, and per block for all other blocks. 1267 * 1268 * Handle the first new block here (but only if some fragments where 1269 * already used for the cylinder summary). 1270 */ 1271 ind=0; 1272 frag_adjust(odupper, -1); 1273 for(d=odupper; ((d<dupper)&&(d%sblock.fs_frag)); d++) { 1274 DBG_PRINT1("scg first frag check loop d=%jd\n", 1275 (intmax_t)d); 1276 if(isclr(cg_blksfree(&acg), d)) { 1277 if (!ind) { 1278 bp[ind].old=d/sblock.fs_frag; 1279 bp[ind].flags|=GFS_FL_FIRST; 1280 if(roundup(d, sblock.fs_frag) >= dupper) { 1281 bp[ind].flags|=GFS_FL_LAST; 1282 } 1283 ind++; 1284 } 1285 } else { 1286 clrbit(cg_blksfree(&acg), d); 1287 acg.cg_cs.cs_nffree--; 1288 sblock.fs_cstotal.cs_nffree--; 1289 } 1290 /* 1291 * No cluster handling is needed here, as there was at least 1292 * one fragment in use by the cylinder summary in the old 1293 * file system. 1294 * No block-free counter handling here as this block was not 1295 * a free block. 1296 */ 1297 } 1298 frag_adjust(odupper, 1); 1299 1300 /* 1301 * Handle all needed complete blocks here. 1302 */ 1303 for(; d+sblock.fs_frag<=dupper; d+=sblock.fs_frag) { 1304 DBG_PRINT1("scg block check loop d=%jd\n", 1305 (intmax_t)d); 1306 if(!isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) { 1307 for(f=d; f<d+sblock.fs_frag; f++) { 1308 if(isset(cg_blksfree(&aocg), f)) { 1309 acg.cg_cs.cs_nffree--; 1310 sblock.fs_cstotal.cs_nffree--; 1311 } 1312 } 1313 clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag); 1314 bp[ind].old=d/sblock.fs_frag; 1315 ind++; 1316 } else { 1317 clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag); 1318 acg.cg_cs.cs_nbfree--; 1319 sblock.fs_cstotal.cs_nbfree--; 1320 if(sblock.fs_contigsumsize > 0) { 1321 clrbit(cg_clustersfree(&acg), d/sblock.fs_frag); 1322 for(lcs=0, l=(d/sblock.fs_frag)+1; 1323 lcs<sblock.fs_contigsumsize; 1324 l++, lcs++ ) { 1325 if(isclr(cg_clustersfree(&acg),l)){ 1326 break; 1327 } 1328 } 1329 if(lcs < sblock.fs_contigsumsize) { 1330 cg_clustersum(&acg)[lcs+1]--; 1331 if(lcs) { 1332 cg_clustersum(&acg)[lcs]++; 1333 } 1334 } 1335 } 1336 } 1337 /* 1338 * No fragment counter handling is needed here, as this finally 1339 * doesn't change after the relocation. 1340 */ 1341 } 1342 1343 /* 1344 * Handle all fragments needed in the last new affected block. 1345 */ 1346 if(d<dupper) { 1347 frag_adjust(dupper-1, -1); 1348 1349 if(isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) { 1350 acg.cg_cs.cs_nbfree--; 1351 sblock.fs_cstotal.cs_nbfree--; 1352 acg.cg_cs.cs_nffree+=sblock.fs_frag; 1353 sblock.fs_cstotal.cs_nffree+=sblock.fs_frag; 1354 if(sblock.fs_contigsumsize > 0) { 1355 clrbit(cg_clustersfree(&acg), d/sblock.fs_frag); 1356 for(lcs=0, l=(d/sblock.fs_frag)+1; 1357 lcs<sblock.fs_contigsumsize; 1358 l++, lcs++ ) { 1359 if(isclr(cg_clustersfree(&acg),l)){ 1360 break; 1361 } 1362 } 1363 if(lcs < sblock.fs_contigsumsize) { 1364 cg_clustersum(&acg)[lcs+1]--; 1365 if(lcs) { 1366 cg_clustersum(&acg)[lcs]++; 1367 } 1368 } 1369 } 1370 } 1371 1372 for(; d<dupper; d++) { 1373 DBG_PRINT1("scg second frag check loop d=%jd\n", 1374 (intmax_t)d); 1375 if(isclr(cg_blksfree(&acg), d)) { 1376 bp[ind].old=d/sblock.fs_frag; 1377 bp[ind].flags|=GFS_FL_LAST; 1378 } else { 1379 clrbit(cg_blksfree(&acg), d); 1380 acg.cg_cs.cs_nffree--; 1381 sblock.fs_cstotal.cs_nffree--; 1382 } 1383 } 1384 if(bp[ind].flags & GFS_FL_LAST) { /* we have to advance here */ 1385 ind++; 1386 } 1387 frag_adjust(dupper-1, 1); 1388 } 1389 1390 /* 1391 * If we found a block to relocate just do so. 1392 */ 1393 if(ind) { 1394 for(i=0; i<ind; i++) { 1395 if(!bp[i].old) { /* no more blocks listed */ 1396 /* 1397 * XXX A relative blocknumber should not be 1398 * zero, which is not explicitly 1399 * guaranteed by our code. 1400 */ 1401 break; 1402 } 1403 /* 1404 * Allocate a complete block in the same (current) 1405 * cylinder group. 1406 */ 1407 bp[i].new=alloc()/sblock.fs_frag; 1408 1409 /* 1410 * There is no frag_adjust() needed for the new block 1411 * as it will have no fragments yet :-). 1412 */ 1413 for(f=bp[i].old*sblock.fs_frag, 1414 g=bp[i].new*sblock.fs_frag; 1415 f<(bp[i].old+1)*sblock.fs_frag; 1416 f++, g++) { 1417 if(isset(cg_blksfree(&aocg), f)) { 1418 setbit(cg_blksfree(&acg), g); 1419 acg.cg_cs.cs_nffree++; 1420 sblock.fs_cstotal.cs_nffree++; 1421 } 1422 } 1423 1424 /* 1425 * Special handling is required if this was the first 1426 * block. We have to consider the fragments which were 1427 * used by the cylinder summary in the original block 1428 * which re to be free in the copy of our block. We 1429 * have to be careful if this first block happens to 1430 * be also the last block to be relocated. 1431 */ 1432 if(bp[i].flags & GFS_FL_FIRST) { 1433 for(f=bp[i].old*sblock.fs_frag, 1434 g=bp[i].new*sblock.fs_frag; 1435 f<odupper; 1436 f++, g++) { 1437 setbit(cg_blksfree(&acg), g); 1438 acg.cg_cs.cs_nffree++; 1439 sblock.fs_cstotal.cs_nffree++; 1440 } 1441 if(!(bp[i].flags & GFS_FL_LAST)) { 1442 frag_adjust(bp[i].new*sblock.fs_frag,1); 1443 } 1444 } 1445 1446 /* 1447 * Special handling is required if this is the last 1448 * block to be relocated. 1449 */ 1450 if(bp[i].flags & GFS_FL_LAST) { 1451 frag_adjust(bp[i].new*sblock.fs_frag, 1); 1452 frag_adjust(bp[i].old*sblock.fs_frag, -1); 1453 for(f=dupper; 1454 f<roundup(dupper, sblock.fs_frag); 1455 f++) { 1456 if(isclr(cg_blksfree(&acg), f)) { 1457 setbit(cg_blksfree(&acg), f); 1458 acg.cg_cs.cs_nffree++; 1459 sblock.fs_cstotal.cs_nffree++; 1460 } 1461 } 1462 frag_adjust(bp[i].old*sblock.fs_frag, 1); 1463 } 1464 1465 /* 1466 * !!! Attach the cylindergroup offset here. 1467 */ 1468 bp[i].old+=cbase/sblock.fs_frag; 1469 bp[i].new+=cbase/sblock.fs_frag; 1470 1471 /* 1472 * Copy the content of the block. 1473 */ 1474 /* 1475 * XXX Here we will have to implement a copy on write 1476 * in the case we have any active snapshots. 1477 */ 1478 rdfs(fsbtodb(&sblock, bp[i].old*sblock.fs_frag), 1479 (size_t)sblock.fs_bsize, (void *)&ablk, fsi); 1480 wtfs(fsbtodb(&sblock, bp[i].new*sblock.fs_frag), 1481 (size_t)sblock.fs_bsize, (void *)&ablk, fso, Nflag); 1482 DBG_DUMP_HEX(&sblock, 1483 "copied full block", 1484 (unsigned char *)&ablk); 1485 1486 DBG_PRINT2("scg (%jd->%jd) block relocated\n", 1487 (intmax_t)bp[i].old, 1488 (intmax_t)bp[i].new); 1489 } 1490 1491 /* 1492 * Now we have to update all references to any fragment which 1493 * belongs to any block relocated. We iterate now over all 1494 * cylinder groups, within those over all non zero length 1495 * inodes. 1496 */ 1497 for(cylno=0; cylno<osblock.fs_ncg; cylno++) { 1498 DBG_PRINT1("scg doing cg (%d)\n", 1499 cylno); 1500 for(inc=osblock.fs_ipg-1 ; inc>0 ; inc--) { 1501 updrefs(cylno, (ino_t)inc, bp, fsi, fso, Nflag); 1502 } 1503 } 1504 1505 /* 1506 * All inodes are checked, now make sure the number of 1507 * references found make sense. 1508 */ 1509 for(i=0; i<ind; i++) { 1510 if(!bp[i].found || (bp[i].found>sblock.fs_frag)) { 1511 warnx("error: %jd refs found for block %jd.", 1512 (intmax_t)bp[i].found, (intmax_t)bp[i].old); 1513 } 1514 1515 } 1516 } 1517 /* 1518 * The following statistics are not changed here: 1519 * sblock.fs_cstotal.cs_ndir 1520 * sblock.fs_cstotal.cs_nifree 1521 * The following statistics were already updated on the fly: 1522 * sblock.fs_cstotal.cs_nffree 1523 * sblock.fs_cstotal.cs_nbfree 1524 * As the statistics for this cylinder group are ready, copy it to 1525 * the summary information array. 1526 */ 1527 1528 *cs = acg.cg_cs; 1529 1530 /* 1531 * Write summary cylinder group back to disk. 1532 */ 1533 wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), (size_t)sblock.fs_cgsize, 1534 (void *)&acg, fso, Nflag); 1535 DBG_PRINT0("scg written\n"); 1536 DBG_DUMP_CG(&sblock, 1537 "new summary cg", 1538 &acg); 1539 1540 DBG_LEAVE; 1541 return; 1542 } 1543 1544 /* ************************************************************** rdfs ***** */ 1545 /* 1546 * Here we read some block(s) from disk. 1547 */ 1548 static void 1549 rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi) 1550 { 1551 DBG_FUNC("rdfs") 1552 ssize_t n; 1553 1554 DBG_ENTER; 1555 1556 if (bno < 0) { 1557 err(32, "rdfs: attempting to read negative block number"); 1558 } 1559 if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) { 1560 err(33, "rdfs: seek error: %jd", (intmax_t)bno); 1561 } 1562 n = read(fsi, bf, size); 1563 if (n != (ssize_t)size) { 1564 err(34, "rdfs: read error: %jd", (intmax_t)bno); 1565 } 1566 1567 DBG_LEAVE; 1568 return; 1569 } 1570 1571 /* ************************************************************** wtfs ***** */ 1572 /* 1573 * Here we write some block(s) to disk. 1574 */ 1575 static void 1576 wtfs(ufs2_daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag) 1577 { 1578 DBG_FUNC("wtfs") 1579 ssize_t n; 1580 1581 DBG_ENTER; 1582 1583 if (Nflag) { 1584 DBG_LEAVE; 1585 return; 1586 } 1587 if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) { 1588 err(35, "wtfs: seek error: %ld", (long)bno); 1589 } 1590 n = write(fso, bf, size); 1591 if (n != (ssize_t)size) { 1592 err(36, "wtfs: write error: %ld", (long)bno); 1593 } 1594 1595 DBG_LEAVE; 1596 return; 1597 } 1598 1599 /* ************************************************************* alloc ***** */ 1600 /* 1601 * Here we allocate a free block in the current cylinder group. It is assumed, 1602 * that acg contains the current cylinder group. As we may take a block from 1603 * somewhere in the file system we have to handle cluster summary here. 1604 */ 1605 static ufs2_daddr_t 1606 alloc(void) 1607 { 1608 DBG_FUNC("alloc") 1609 ufs2_daddr_t d, blkno; 1610 int lcs1, lcs2; 1611 int l; 1612 int csmin, csmax; 1613 int dlower, dupper, dmax; 1614 1615 DBG_ENTER; 1616 1617 if (acg.cg_magic != CG_MAGIC) { 1618 warnx("acg: bad magic number"); 1619 DBG_LEAVE; 1620 return (0); 1621 } 1622 if (acg.cg_cs.cs_nbfree == 0) { 1623 warnx("error: cylinder group ran out of space"); 1624 DBG_LEAVE; 1625 return (0); 1626 } 1627 /* 1628 * We start seeking for free blocks only from the space available after 1629 * the end of the new grown cylinder summary. Otherwise we allocate a 1630 * block here which we have to relocate a couple of seconds later again 1631 * again, and we are not prepared to to this anyway. 1632 */ 1633 blkno=-1; 1634 dlower=cgsblock(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx); 1635 dupper=cgdmin(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx); 1636 dmax=cgbase(&sblock, acg.cg_cgx)+sblock.fs_fpg; 1637 if (dmax > sblock.fs_size) { 1638 dmax = sblock.fs_size; 1639 } 1640 dmax-=cgbase(&sblock, acg.cg_cgx); /* retransform into cg */ 1641 csmin=sblock.fs_csaddr-cgbase(&sblock, acg.cg_cgx); 1642 csmax=csmin+howmany(sblock.fs_cssize, sblock.fs_fsize); 1643 DBG_PRINT3("seek range: dl=%d, du=%d, dm=%d\n", 1644 dlower, 1645 dupper, 1646 dmax); 1647 DBG_PRINT2("range cont: csmin=%d, csmax=%d\n", 1648 csmin, 1649 csmax); 1650 1651 for(d=0; (d<dlower && blkno==-1); d+=sblock.fs_frag) { 1652 if(d>=csmin && d<=csmax) { 1653 continue; 1654 } 1655 if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock, 1656 d))) { 1657 blkno = fragstoblks(&sblock, d);/* Yeah found a block */ 1658 break; 1659 } 1660 } 1661 for(d=dupper; (d<dmax && blkno==-1); d+=sblock.fs_frag) { 1662 if(d>=csmin && d<=csmax) { 1663 continue; 1664 } 1665 if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock, 1666 d))) { 1667 blkno = fragstoblks(&sblock, d);/* Yeah found a block */ 1668 break; 1669 } 1670 } 1671 if(blkno==-1) { 1672 warnx("internal error: couldn't find promised block in cg"); 1673 DBG_LEAVE; 1674 return (0); 1675 } 1676 1677 /* 1678 * This is needed if the block was found already in the first loop. 1679 */ 1680 d=blkstofrags(&sblock, blkno); 1681 1682 clrblock(&sblock, cg_blksfree(&acg), blkno); 1683 if (sblock.fs_contigsumsize > 0) { 1684 /* 1685 * Handle the cluster allocation bitmap. 1686 */ 1687 clrbit(cg_clustersfree(&acg), blkno); 1688 /* 1689 * We possibly have split a cluster here, so we have to do 1690 * recalculate the sizes of the remaining cluster halves now, 1691 * and use them for updating the cluster summary information. 1692 * 1693 * Lets start with the blocks before our allocated block ... 1694 */ 1695 for(lcs1=0, l=blkno-1; lcs1<sblock.fs_contigsumsize; 1696 l--, lcs1++ ) { 1697 if(isclr(cg_clustersfree(&acg),l)){ 1698 break; 1699 } 1700 } 1701 /* 1702 * ... and continue with the blocks right after our allocated 1703 * block. 1704 */ 1705 for(lcs2=0, l=blkno+1; lcs2<sblock.fs_contigsumsize; 1706 l++, lcs2++ ) { 1707 if(isclr(cg_clustersfree(&acg),l)){ 1708 break; 1709 } 1710 } 1711 1712 /* 1713 * Now update all counters. 1714 */ 1715 cg_clustersum(&acg)[MIN(lcs1+lcs2+1,sblock.fs_contigsumsize)]--; 1716 if(lcs1) { 1717 cg_clustersum(&acg)[lcs1]++; 1718 } 1719 if(lcs2) { 1720 cg_clustersum(&acg)[lcs2]++; 1721 } 1722 } 1723 /* 1724 * Update all statistics based on blocks. 1725 */ 1726 acg.cg_cs.cs_nbfree--; 1727 sblock.fs_cstotal.cs_nbfree--; 1728 1729 DBG_LEAVE; 1730 return (d); 1731 } 1732 1733 /* *********************************************************** isblock ***** */ 1734 /* 1735 * Here we check if all frags of a block are free. For more details again 1736 * please see the source of newfs(8), as this function is taken over almost 1737 * unchanged. 1738 */ 1739 static int 1740 isblock(struct fs *fs, unsigned char *cp, int h) 1741 { 1742 DBG_FUNC("isblock") 1743 unsigned char mask; 1744 1745 DBG_ENTER; 1746 1747 switch (fs->fs_frag) { 1748 case 8: 1749 DBG_LEAVE; 1750 return (cp[h] == 0xff); 1751 case 4: 1752 mask = 0x0f << ((h & 0x1) << 2); 1753 DBG_LEAVE; 1754 return ((cp[h >> 1] & mask) == mask); 1755 case 2: 1756 mask = 0x03 << ((h & 0x3) << 1); 1757 DBG_LEAVE; 1758 return ((cp[h >> 2] & mask) == mask); 1759 case 1: 1760 mask = 0x01 << (h & 0x7); 1761 DBG_LEAVE; 1762 return ((cp[h >> 3] & mask) == mask); 1763 default: 1764 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); 1765 DBG_LEAVE; 1766 return (0); 1767 } 1768 } 1769 1770 /* ********************************************************** clrblock ***** */ 1771 /* 1772 * Here we allocate a complete block in the block map. For more details again 1773 * please see the source of newfs(8), as this function is taken over almost 1774 * unchanged. 1775 */ 1776 static void 1777 clrblock(struct fs *fs, unsigned char *cp, int h) 1778 { 1779 DBG_FUNC("clrblock") 1780 1781 DBG_ENTER; 1782 1783 switch ((fs)->fs_frag) { 1784 case 8: 1785 cp[h] = 0; 1786 break; 1787 case 4: 1788 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1789 break; 1790 case 2: 1791 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1792 break; 1793 case 1: 1794 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1795 break; 1796 default: 1797 warnx("clrblock bad fs_frag %d", fs->fs_frag); 1798 break; 1799 } 1800 1801 DBG_LEAVE; 1802 return; 1803 } 1804 1805 /* ********************************************************** setblock ***** */ 1806 /* 1807 * Here we free a complete block in the free block map. For more details again 1808 * please see the source of newfs(8), as this function is taken over almost 1809 * unchanged. 1810 */ 1811 static void 1812 setblock(struct fs *fs, unsigned char *cp, int h) 1813 { 1814 DBG_FUNC("setblock") 1815 1816 DBG_ENTER; 1817 1818 switch (fs->fs_frag) { 1819 case 8: 1820 cp[h] = 0xff; 1821 break; 1822 case 4: 1823 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1824 break; 1825 case 2: 1826 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1827 break; 1828 case 1: 1829 cp[h >> 3] |= (0x01 << (h & 0x7)); 1830 break; 1831 default: 1832 warnx("setblock bad fs_frag %d", fs->fs_frag); 1833 break; 1834 } 1835 1836 DBG_LEAVE; 1837 return; 1838 } 1839 1840 /* ************************************************************ ginode ***** */ 1841 /* 1842 * This function provides access to an individual inode. We find out in which 1843 * block the requested inode is located, read it from disk if needed, and 1844 * return the pointer into that block. We maintain a cache of one block to 1845 * not read the same block again and again if we iterate linearly over all 1846 * inodes. 1847 */ 1848 static union dinode * 1849 ginode(ino_t inumber, int fsi, int cg) 1850 { 1851 DBG_FUNC("ginode") 1852 static ino_t startinum = 0; /* first inode in cached block */ 1853 1854 DBG_ENTER; 1855 1856 /* 1857 * The inumber passed in is relative to the cg, so use it here to see 1858 * if the inode has been allocated yet. 1859 */ 1860 if (isclr(cg_inosused(&aocg), inumber)) { 1861 DBG_LEAVE; 1862 return NULL; 1863 } 1864 /* 1865 * Now make the inumber relative to the entire inode space so it can 1866 * be sanity checked. 1867 */ 1868 inumber += (cg * sblock.fs_ipg); 1869 if (inumber < ROOTINO) { 1870 DBG_LEAVE; 1871 return NULL; 1872 } 1873 if (inumber > maxino) 1874 errx(8, "bad inode number %d to ginode", inumber); 1875 if (startinum == 0 || 1876 inumber < startinum || inumber >= startinum + INOPB(&sblock)) { 1877 inoblk = fsbtodb(&sblock, ino_to_fsba(&sblock, inumber)); 1878 rdfs(inoblk, (size_t)sblock.fs_bsize, inobuf, fsi); 1879 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock); 1880 } 1881 DBG_LEAVE; 1882 if (sblock.fs_magic == FS_UFS1_MAGIC) 1883 return (union dinode *)((uintptr_t)inobuf + 1884 (inumber % INOPB(&sblock)) * sizeof(struct ufs1_dinode)); 1885 return (union dinode *)((uintptr_t)inobuf + 1886 (inumber % INOPB(&sblock)) * sizeof(struct ufs2_dinode)); 1887 } 1888 1889 /* ****************************************************** charsperline ***** */ 1890 /* 1891 * Figure out how many lines our current terminal has. For more details again 1892 * please see the source of newfs(8), as this function is taken over almost 1893 * unchanged. 1894 */ 1895 static int 1896 charsperline(void) 1897 { 1898 DBG_FUNC("charsperline") 1899 int columns; 1900 char *cp; 1901 struct winsize ws; 1902 1903 DBG_ENTER; 1904 1905 columns = 0; 1906 if (ioctl(0, TIOCGWINSZ, &ws) != -1) { 1907 columns = ws.ws_col; 1908 } 1909 if (columns == 0 && (cp = getenv("COLUMNS"))) { 1910 columns = atoi(cp); 1911 } 1912 if (columns == 0) { 1913 columns = 80; /* last resort */ 1914 } 1915 1916 DBG_LEAVE; 1917 return columns; 1918 } 1919 1920 /* ****************************************************** get_dev_size ***** */ 1921 /* 1922 * Get the size of the partition if we can't figure it out from the disklabel, 1923 * e.g. from vinum volumes. 1924 */ 1925 static void 1926 get_dev_size(int fd, int *size) 1927 { 1928 int sectorsize; 1929 off_t mediasize; 1930 1931 if (ioctl(fd, DIOCGSECTORSIZE, §orsize) == -1) 1932 err(1,"DIOCGSECTORSIZE"); 1933 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == -1) 1934 err(1,"DIOCGMEDIASIZE"); 1935 1936 if (sectorsize <= 0) 1937 errx(1, "bogus sectorsize: %d", sectorsize); 1938 1939 *size = mediasize / sectorsize; 1940 } 1941 1942 /* ************************************************************** main ***** */ 1943 /* 1944 * growfs(8) is a utility which allows to increase the size of an existing 1945 * ufs file system. Currently this can only be done on unmounted file system. 1946 * It recognizes some command line options to specify the new desired size, 1947 * and it does some basic checkings. The old file system size is determined 1948 * and after some more checks like we can really access the new last block 1949 * on the disk etc. we calculate the new parameters for the superblock. After 1950 * having done this we just call growfs() which will do the work. Before 1951 * we finish the only thing left is to update the disklabel. 1952 * We still have to provide support for snapshots. Therefore we first have to 1953 * understand what data structures are always replicated in the snapshot on 1954 * creation, for all other blocks we touch during our procedure, we have to 1955 * keep the old blocks unchanged somewhere available for the snapshots. If we 1956 * are lucky, then we only have to handle our blocks to be relocated in that 1957 * way. 1958 * Also we have to consider in what order we actually update the critical 1959 * data structures of the file system to make sure, that in case of a disaster 1960 * fsck(8) is still able to restore any lost data. 1961 * The foreseen last step then will be to provide for growing even mounted 1962 * file systems. There we have to extend the mount() system call to provide 1963 * userland access to the file system locking facility. 1964 */ 1965 int 1966 main(int argc, char **argv) 1967 { 1968 DBG_FUNC("main") 1969 char *device, *special, *cp; 1970 int ch; 1971 unsigned int size=0; 1972 size_t len; 1973 unsigned int Nflag=0; 1974 int ExpertFlag=0; 1975 struct stat st; 1976 struct disklabel *lp; 1977 struct partition *pp; 1978 int i,fsi,fso; 1979 u_int32_t p_size; 1980 char reply[5]; 1981 #ifdef FSMAXSNAP 1982 int j; 1983 #endif /* FSMAXSNAP */ 1984 1985 DBG_ENTER; 1986 1987 while((ch=getopt(argc, argv, "Ns:vy")) != -1) { 1988 switch(ch) { 1989 case 'N': 1990 Nflag=1; 1991 break; 1992 case 's': 1993 size=(size_t)atol(optarg); 1994 if(size<1) { 1995 usage(); 1996 } 1997 break; 1998 case 'v': /* for compatibility to newfs */ 1999 break; 2000 case 'y': 2001 ExpertFlag=1; 2002 break; 2003 case '?': 2004 /* FALLTHROUGH */ 2005 default: 2006 usage(); 2007 } 2008 } 2009 argc -= optind; 2010 argv += optind; 2011 2012 if(argc != 1) { 2013 usage(); 2014 } 2015 device=*argv; 2016 2017 /* 2018 * Now try to guess the (raw)device name. 2019 */ 2020 if (0 == strrchr(device, '/')) { 2021 /* 2022 * No path prefix was given, so try in that order: 2023 * /dev/r%s 2024 * /dev/%s 2025 * /dev/vinum/r%s 2026 * /dev/vinum/%s. 2027 * 2028 * FreeBSD now doesn't distinguish between raw and block 2029 * devices any longer, but it should still work this way. 2030 */ 2031 len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/"); 2032 special=(char *)malloc(len); 2033 if(special == NULL) { 2034 errx(1, "malloc failed"); 2035 } 2036 snprintf(special, len, "%sr%s", _PATH_DEV, device); 2037 if (stat(special, &st) == -1) { 2038 snprintf(special, len, "%s%s", _PATH_DEV, device); 2039 if (stat(special, &st) == -1) { 2040 snprintf(special, len, "%svinum/r%s", 2041 _PATH_DEV, device); 2042 if (stat(special, &st) == -1) { 2043 /* For now this is the 'last resort' */ 2044 snprintf(special, len, "%svinum/%s", 2045 _PATH_DEV, device); 2046 } 2047 } 2048 } 2049 device = special; 2050 } 2051 2052 /* 2053 * Try to access our devices for writing ... 2054 */ 2055 if (Nflag) { 2056 fso = -1; 2057 } else { 2058 fso = open(device, O_WRONLY); 2059 if (fso < 0) { 2060 err(1, "%s", device); 2061 } 2062 } 2063 2064 /* 2065 * ... and reading. 2066 */ 2067 fsi = open(device, O_RDONLY); 2068 if (fsi < 0) { 2069 err(1, "%s", device); 2070 } 2071 2072 /* 2073 * Try to read a label and guess the slice if not specified. This 2074 * code should guess the right thing and avoid to bother the user 2075 * with the task of specifying the option -v on vinum volumes. 2076 */ 2077 cp=device+strlen(device)-1; 2078 lp = get_disklabel(fsi); 2079 pp = NULL; 2080 if (lp != NULL) { 2081 if (isdigit(*cp)) { 2082 pp = &lp->d_partitions[2]; 2083 } else if (*cp>='a' && *cp<='h') { 2084 pp = &lp->d_partitions[*cp - 'a']; 2085 } else { 2086 errx(1, "unknown device"); 2087 } 2088 p_size = pp->p_size; 2089 } else { 2090 get_dev_size(fsi, &p_size); 2091 } 2092 2093 /* 2094 * Check if that partition is suitable for growing a file system. 2095 */ 2096 if (p_size < 1) { 2097 errx(1, "partition is unavailable"); 2098 } 2099 2100 /* 2101 * Read the current superblock, and take a backup. 2102 */ 2103 for (i = 0; sblock_try[i] != -1; i++) { 2104 sblockloc = sblock_try[i] / DEV_BSIZE; 2105 rdfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&(osblock), fsi); 2106 if ((osblock.fs_magic == FS_UFS1_MAGIC || 2107 (osblock.fs_magic == FS_UFS2_MAGIC && 2108 osblock.fs_sblockloc == sblock_try[i])) && 2109 osblock.fs_bsize <= MAXBSIZE && 2110 osblock.fs_bsize >= (int32_t) sizeof(struct fs)) 2111 break; 2112 } 2113 if (sblock_try[i] == -1) { 2114 errx(1, "superblock not recognized"); 2115 } 2116 memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2)); 2117 maxino = sblock.fs_ncg * sblock.fs_ipg; 2118 2119 DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */ 2120 DBG_DUMP_FS(&sblock, 2121 "old sblock"); 2122 2123 /* 2124 * Determine size to grow to. Default to the full size specified in 2125 * the disk label. 2126 */ 2127 sblock.fs_size = dbtofsb(&osblock, p_size); 2128 if (size != 0) { 2129 if (size > p_size){ 2130 errx(1, "there is not enough space (%d < %d)", 2131 p_size, size); 2132 } 2133 sblock.fs_size = dbtofsb(&osblock, size); 2134 } 2135 2136 /* 2137 * Are we really growing ? 2138 */ 2139 if(osblock.fs_size >= sblock.fs_size) { 2140 errx(1, "we are not growing (%jd->%jd)", 2141 (intmax_t)osblock.fs_size, (intmax_t)sblock.fs_size); 2142 } 2143 2144 2145 #ifdef FSMAXSNAP 2146 /* 2147 * Check if we find an active snapshot. 2148 */ 2149 if(ExpertFlag == 0) { 2150 for(j=0; j<FSMAXSNAP; j++) { 2151 if(sblock.fs_snapinum[j]) { 2152 errx(1, "active snapshot found in file system\n" 2153 " please remove all snapshots before " 2154 "using growfs"); 2155 } 2156 if(!sblock.fs_snapinum[j]) { /* list is dense */ 2157 break; 2158 } 2159 } 2160 } 2161 #endif 2162 2163 if (ExpertFlag == 0 && Nflag == 0) { 2164 printf("We strongly recommend you to make a backup " 2165 "before growing the Filesystem\n\n" 2166 " Did you backup your data (Yes/No) ? "); 2167 fgets(reply, (int)sizeof(reply), stdin); 2168 if (strcmp(reply, "Yes\n")){ 2169 printf("\n Nothing done \n"); 2170 exit (0); 2171 } 2172 } 2173 2174 printf("new file systemsize is: %jd frags\n", (intmax_t)sblock.fs_size); 2175 2176 /* 2177 * Try to access our new last block in the file system. Even if we 2178 * later on realize we have to abort our operation, on that block 2179 * there should be no data, so we can't destroy something yet. 2180 */ 2181 wtfs((ufs2_daddr_t)p_size-1, (size_t)DEV_BSIZE, (void *)&sblock, 2182 fso, Nflag); 2183 2184 /* 2185 * Now calculate new superblock values and check for reasonable 2186 * bound for new file system size: 2187 * fs_size: is derived from label or user input 2188 * fs_dsize: should get updated in the routines creating or 2189 * updating the cylinder groups on the fly 2190 * fs_cstotal: should get updated in the routines creating or 2191 * updating the cylinder groups 2192 */ 2193 2194 /* 2195 * Update the number of cylinders and cylinder groups in the file system. 2196 */ 2197 if (sblock.fs_magic == FS_UFS1_MAGIC) { 2198 sblock.fs_old_ncyl = 2199 sblock.fs_size * sblock.fs_old_nspf / sblock.fs_old_spc; 2200 if (sblock.fs_size * sblock.fs_old_nspf > 2201 sblock.fs_old_ncyl * sblock.fs_old_spc) 2202 sblock.fs_old_ncyl++; 2203 } 2204 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg); 2205 maxino = sblock.fs_ncg * sblock.fs_ipg; 2206 2207 if (sblock.fs_size % sblock.fs_fpg != 0 && 2208 sblock.fs_size % sblock.fs_fpg < cgdmin(&sblock, sblock.fs_ncg)) { 2209 /* 2210 * The space in the new last cylinder group is too small, 2211 * so revert back. 2212 */ 2213 sblock.fs_ncg--; 2214 if (sblock.fs_magic == FS_UFS1_MAGIC) 2215 sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg; 2216 printf("Warning: %jd sector(s) cannot be allocated.\n", 2217 (intmax_t)fsbtodb(&sblock, sblock.fs_size % sblock.fs_fpg)); 2218 sblock.fs_size = sblock.fs_ncg * sblock.fs_fpg; 2219 maxino -= sblock.fs_ipg; 2220 } 2221 2222 /* 2223 * Update the space for the cylinder group summary information in the 2224 * respective cylinder group data area. 2225 */ 2226 sblock.fs_cssize = 2227 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 2228 2229 if(osblock.fs_size >= sblock.fs_size) { 2230 errx(1, "not enough new space"); 2231 } 2232 2233 DBG_PRINT0("sblock calculated\n"); 2234 2235 /* 2236 * Ok, everything prepared, so now let's do the tricks. 2237 */ 2238 growfs(fsi, fso, Nflag); 2239 2240 /* 2241 * Update the disk label. 2242 */ 2243 if (!unlabeled) { 2244 pp->p_fsize = sblock.fs_fsize; 2245 pp->p_frag = sblock.fs_frag; 2246 pp->p_cpg = sblock.fs_fpg; 2247 2248 return_disklabel(fso, lp, Nflag); 2249 DBG_PRINT0("label rewritten\n"); 2250 } 2251 2252 close(fsi); 2253 if(fso>-1) close(fso); 2254 2255 DBG_CLOSE; 2256 2257 DBG_LEAVE; 2258 return 0; 2259 } 2260 2261 /* ************************************************** return_disklabel ***** */ 2262 /* 2263 * Write the updated disklabel back to disk. 2264 */ 2265 static void 2266 return_disklabel(int fd, struct disklabel *lp, unsigned int Nflag) 2267 { 2268 DBG_FUNC("return_disklabel") 2269 u_short sum; 2270 u_short *ptr; 2271 2272 DBG_ENTER; 2273 2274 if(!lp) { 2275 DBG_LEAVE; 2276 return; 2277 } 2278 if(!Nflag) { 2279 lp->d_checksum=0; 2280 sum = 0; 2281 ptr=(u_short *)lp; 2282 2283 /* 2284 * recalculate checksum 2285 */ 2286 while(ptr < (u_short *)&lp->d_partitions[lp->d_npartitions]) { 2287 sum ^= *ptr++; 2288 } 2289 lp->d_checksum=sum; 2290 2291 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) { 2292 errx(1, "DIOCWDINFO failed"); 2293 } 2294 } 2295 free(lp); 2296 2297 DBG_LEAVE; 2298 return ; 2299 } 2300 2301 /* ***************************************************** get_disklabel ***** */ 2302 /* 2303 * Read the disklabel from disk. 2304 */ 2305 static struct disklabel * 2306 get_disklabel(int fd) 2307 { 2308 DBG_FUNC("get_disklabel") 2309 static struct disklabel *lab; 2310 2311 DBG_ENTER; 2312 2313 lab=(struct disklabel *)malloc(sizeof(struct disklabel)); 2314 if (!lab) 2315 errx(1, "malloc failed"); 2316 2317 if (!ioctl(fd, DIOCGDINFO, (char *)lab)) 2318 return (lab); 2319 2320 unlabeled++; 2321 2322 DBG_LEAVE; 2323 return (NULL); 2324 } 2325 2326 2327 /* ************************************************************* usage ***** */ 2328 /* 2329 * Dump a line of usage. 2330 */ 2331 static void 2332 usage(void) 2333 { 2334 DBG_FUNC("usage") 2335 2336 DBG_ENTER; 2337 2338 fprintf(stderr, "usage: growfs [-Ny] [-s size] special\n"); 2339 2340 DBG_LEAVE; 2341 exit(1); 2342 } 2343 2344 /* *********************************************************** updclst ***** */ 2345 /* 2346 * This updates most parameters and the bitmap related to cluster. We have to 2347 * assume that sblock, osblock, acg are set up. 2348 */ 2349 static void 2350 updclst(int block) 2351 { 2352 DBG_FUNC("updclst") 2353 static int lcs=0; 2354 2355 DBG_ENTER; 2356 2357 if(sblock.fs_contigsumsize < 1) { /* no clustering */ 2358 return; 2359 } 2360 /* 2361 * update cluster allocation map 2362 */ 2363 setbit(cg_clustersfree(&acg), block); 2364 2365 /* 2366 * update cluster summary table 2367 */ 2368 if(!lcs) { 2369 /* 2370 * calculate size for the trailing cluster 2371 */ 2372 for(block--; lcs<sblock.fs_contigsumsize; block--, lcs++ ) { 2373 if(isclr(cg_clustersfree(&acg), block)){ 2374 break; 2375 } 2376 } 2377 } 2378 if(lcs < sblock.fs_contigsumsize) { 2379 if(lcs) { 2380 cg_clustersum(&acg)[lcs]--; 2381 } 2382 lcs++; 2383 cg_clustersum(&acg)[lcs]++; 2384 } 2385 2386 DBG_LEAVE; 2387 return; 2388 } 2389 2390 /* *********************************************************** updrefs ***** */ 2391 /* 2392 * This updates all references to relocated blocks for the given inode. The 2393 * inode is given as number within the cylinder group, and the number of the 2394 * cylinder group. 2395 */ 2396 static void 2397 updrefs(int cg, ino_t in, struct gfs_bpp *bp, int fsi, int fso, unsigned int 2398 Nflag) 2399 { 2400 DBG_FUNC("updrefs") 2401 ufs_lbn_t len, lbn, numblks; 2402 ufs2_daddr_t iptr, blksperindir; 2403 union dinode *ino; 2404 int i, mode, inodeupdated; 2405 2406 DBG_ENTER; 2407 2408 ino = ginode(in, fsi, cg); 2409 if (ino == NULL) { 2410 DBG_LEAVE; 2411 return; 2412 } 2413 mode = DIP(ino, di_mode) & IFMT; 2414 if (mode != IFDIR && mode != IFREG && mode != IFLNK) { 2415 DBG_LEAVE; 2416 return; /* only check DIR, FILE, LINK */ 2417 } 2418 if (mode == IFLNK && 2419 DIP(ino, di_size) < (u_int64_t) sblock.fs_maxsymlinklen) { 2420 DBG_LEAVE; 2421 return; /* skip short symlinks */ 2422 } 2423 numblks = howmany(DIP(ino, di_size), sblock.fs_bsize); 2424 if (numblks == 0) { 2425 DBG_LEAVE; 2426 return; /* skip empty file */ 2427 } 2428 if (DIP(ino, di_blocks) == 0) { 2429 DBG_LEAVE; 2430 return; /* skip empty swiss cheesy file or old fastlink */ 2431 } 2432 DBG_PRINT2("scg checking inode (%d in %d)\n", 2433 in, 2434 cg); 2435 2436 /* 2437 * Check all the blocks. 2438 */ 2439 inodeupdated = 0; 2440 len = numblks < NDADDR ? numblks : NDADDR; 2441 for (i = 0; i < len; i++) { 2442 iptr = DIP(ino, di_db[i]); 2443 if (iptr == 0) 2444 continue; 2445 if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) { 2446 DIP_SET(ino, di_db[i], iptr); 2447 inodeupdated++; 2448 } 2449 } 2450 DBG_PRINT0("~~scg direct blocks checked\n"); 2451 2452 blksperindir = 1; 2453 len = numblks - NDADDR; 2454 lbn = NDADDR; 2455 for (i = 0; len > 0 && i < NIADDR; i++) { 2456 iptr = DIP(ino, di_ib[i]); 2457 if (iptr == 0) 2458 continue; 2459 if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) { 2460 DIP_SET(ino, di_ib[i], iptr); 2461 inodeupdated++; 2462 } 2463 indirchk(blksperindir, lbn, iptr, numblks, bp, fsi, fso, Nflag); 2464 blksperindir *= NINDIR(&sblock); 2465 lbn += blksperindir; 2466 len -= blksperindir; 2467 DBG_PRINT1("scg indirect_%d blocks checked\n", i + 1); 2468 } 2469 if (inodeupdated) 2470 wtfs(inoblk, sblock.fs_bsize, inobuf, fso, Nflag); 2471 2472 DBG_LEAVE; 2473 return; 2474 } 2475 2476 /* 2477 * Recursively check all the indirect blocks. 2478 */ 2479 static void 2480 indirchk(ufs_lbn_t blksperindir, ufs_lbn_t lbn, ufs2_daddr_t blkno, 2481 ufs_lbn_t lastlbn, struct gfs_bpp *bp, int fsi, int fso, unsigned int Nflag) 2482 { 2483 DBG_FUNC("indirchk") 2484 void *ibuf; 2485 int i, last; 2486 ufs2_daddr_t iptr; 2487 2488 DBG_ENTER; 2489 2490 /* read in the indirect block. */ 2491 ibuf = malloc(sblock.fs_bsize); 2492 if (!ibuf) 2493 errx(1, "malloc failed"); 2494 rdfs(fsbtodb(&sblock, blkno), (size_t)sblock.fs_bsize, ibuf, fsi); 2495 last = howmany(lastlbn - lbn, blksperindir) < NINDIR(&sblock) ? 2496 howmany(lastlbn - lbn, blksperindir) : NINDIR(&sblock); 2497 for (i = 0; i < last; i++) { 2498 if (sblock.fs_magic == FS_UFS1_MAGIC) 2499 iptr = ((ufs1_daddr_t *)ibuf)[i]; 2500 else 2501 iptr = ((ufs2_daddr_t *)ibuf)[i]; 2502 if (iptr == 0) 2503 continue; 2504 if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) { 2505 if (sblock.fs_magic == FS_UFS1_MAGIC) 2506 ((ufs1_daddr_t *)ibuf)[i] = iptr; 2507 else 2508 ((ufs2_daddr_t *)ibuf)[i] = iptr; 2509 } 2510 if (blksperindir == 1) 2511 continue; 2512 indirchk(blksperindir / NINDIR(&sblock), lbn + blksperindir * i, 2513 iptr, lastlbn, bp, fsi, fso, Nflag); 2514 } 2515 free(ibuf); 2516 2517 DBG_LEAVE; 2518 return; 2519 } 2520