2 * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
3 * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
7 * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
38 * $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $
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";
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
53 #include <sys/ioctl.h>
68 #include <ufs/ufs/dinode.h>
69 #include <ufs/ffs/fs.h>
74 int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
81 #define sblock fsun1.fs /* the new superblock */
82 #define osblock fsun2.fs /* the old superblock */
85 * Possible superblock locations ordered from most to least likely.
87 static int sblock_try[] = SBLOCKSEARCH;
88 static ufs2_daddr_t sblockloc;
94 #define acg cgun1.cg /* a cylinder cgroup (new) */
95 #define aocg cgun2.cg /* an old cylinder group */
97 static struct csum *fscs; /* cylinder summary */
99 static void growfs(int, int, unsigned int);
100 static void rdfs(ufs2_daddr_t, size_t, void *, int);
101 static void wtfs(ufs2_daddr_t, size_t, void *, int, unsigned int);
102 static int charsperline(void);
103 static void usage(void);
104 static int isblock(struct fs *, unsigned char *, int);
105 static void clrblock(struct fs *, unsigned char *, int);
106 static void setblock(struct fs *, unsigned char *, int);
107 static void initcg(int, time_t, int, unsigned int);
108 static void updjcg(int, time_t, int, int, unsigned int);
109 static void updcsloc(time_t, int, int, unsigned int);
110 static void frag_adjust(ufs2_daddr_t, int);
111 static void updclst(int);
112 static void get_dev_size(int, int *);
115 * Here we actually start growing the file system. We basically read the
116 * cylinder summary from the first cylinder group as we want to update
117 * this on the fly during our various operations. First we handle the
118 * changes in the former last cylinder group. Afterwards we create all new
119 * cylinder groups. Now we handle the cylinder group containing the
120 * cylinder summary which might result in a relocation of the whole
121 * structure. In the end we write back the updated cylinder summary, the
122 * new superblock, and slightly patched versions of the super block
126 growfs(int fsi, int fso, unsigned int Nflag)
134 static int randinit=0;
142 #else /* not FSIRAND */
150 * Get the cylinder summary into the memory.
152 fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize);
154 errx(1, "calloc failed");
155 for (i = 0; i < osblock.fs_cssize; i += osblock.fs_bsize) {
156 rdfs(fsbtodb(&osblock, osblock.fs_csaddr +
157 numfrags(&osblock, i)), (size_t)MIN(osblock.fs_cssize - i,
158 osblock.fs_bsize), (void *)(((char *)fscs) + i), fsi);
163 struct csum *dbg_csp;
169 for (dbg_csc = 0; dbg_csc < osblock.fs_ncg; dbg_csc++) {
170 snprintf(dbg_line, sizeof(dbg_line),
171 "%d. old csum in old location", dbg_csc);
172 DBG_DUMP_CSUM(&osblock, dbg_line, dbg_csp++);
175 #endif /* FS_DEBUG */
176 DBG_PRINT0("fscs read\n");
179 * Do all needed changes in the former last cylinder group.
181 updjcg(osblock.fs_ncg - 1, modtime, fsi, fso, Nflag);
184 * Dump out summary information about file system.
186 # define B2MBFACTOR (1 / (1024.0 * 1024.0))
187 printf("growfs: %.1fMB (%jd sectors) block size %d, fragment size %d\n",
188 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
189 (intmax_t)fsbtodb(&sblock, sblock.fs_size), sblock.fs_bsize,
191 printf("\tusing %d cylinder groups of %.2fMB, %d blks, %d inodes.\n",
192 sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
193 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
194 if (sblock.fs_flags & FS_DOSOFTDEP)
195 printf("\twith soft updates\n");
199 * Now build the cylinders group blocks and
200 * then print out indices of cylinder groups.
202 printf("super-block backups (for fsck -b #) at:\n");
204 width = charsperline();
207 * Iterate for only the new cylinder groups.
209 for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
210 initcg(cylno, modtime, fso, Nflag);
211 j = sprintf(tmpbuf, " %jd%s",
212 (intmax_t)fsbtodb(&sblock, cgsblock(&sblock, cylno)),
213 cylno < (sblock.fs_ncg - 1) ? "," : "" );
214 if (i + j >= width) {
219 printf("%s", tmpbuf);
225 * Do all needed changes in the first cylinder group.
226 * allocate blocks in new location
228 updcsloc(modtime, fsi, fso, Nflag);
231 * Now write the cylinder summary back to disk.
233 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
234 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
235 (size_t)MIN(sblock.fs_cssize - i, sblock.fs_bsize),
236 (void *)(((char *)fscs) + i), fso, Nflag);
238 DBG_PRINT0("fscs written\n");
242 struct csum *dbg_csp;
247 for (dbg_csc = 0; dbg_csc < sblock.fs_ncg; dbg_csc++) {
248 snprintf(dbg_line, sizeof(dbg_line),
249 "%d. new csum in new location", dbg_csc);
250 DBG_DUMP_CSUM(&sblock, dbg_line, dbg_csp++);
253 #endif /* FS_DEBUG */
256 * Now write the new superblock back to disk.
258 sblock.fs_time = modtime;
259 wtfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
260 DBG_PRINT0("sblock written\n");
261 DBG_DUMP_FS(&sblock, "new initial sblock");
264 * Clean up the dynamic fields in our superblock copies.
269 sblock.fs_cgrotor = 0;
271 memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
272 sblock.fs_flags &= FS_DOSOFTDEP;
276 * The following fields are currently distributed from the superblock
284 * fs_flags regarding SOFTPDATES
286 * We probably should rather change the summary for the cylinder group
287 * statistics here to the value of what would be in there, if the file
288 * system were created initially with the new size. Therefor we still
289 * need to find an easy way of calculating that.
290 * Possibly we can try to read the first superblock copy and apply the
291 * "diffed" stats between the old and new superblock by still copying
292 * certain parameters onto that.
296 * Write out the duplicate super blocks.
298 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
299 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
300 (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
302 DBG_PRINT0("sblock copies written\n");
303 DBG_DUMP_FS(&sblock, "new other sblocks");
310 * This creates a new cylinder group structure, for more details please see
311 * the source of newfs(8), as this function is taken over almost unchanged.
312 * As this is never called for the first cylinder group, the special
313 * provisions for that case are removed here.
316 initcg(int cylno, time_t modtime, int fso, unsigned int Nflag)
319 static caddr_t iobuf;
321 ufs2_daddr_t i, cbase, dmax;
323 struct ufs1_dinode *dp1;
326 uint d, dupper, dlower;
328 if (iobuf == NULL && (iobuf = malloc(sblock.fs_bsize * 3)) == NULL)
329 errx(37, "panic: cannot allocate I/O buffer");
332 * Determine block bounds for cylinder group.
333 * Allow space for super block summary information in first
336 cbase = cgbase(&sblock, cylno);
337 dmax = cbase + sblock.fs_fpg;
338 if (dmax > sblock.fs_size)
339 dmax = sblock.fs_size;
340 dlower = cgsblock(&sblock, cylno) - cbase;
341 dupper = cgdmin(&sblock, cylno) - cbase;
342 if (cylno == 0) /* XXX fscs may be relocated */
343 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
345 memset(&acg, 0, sblock.fs_cgsize);
346 acg.cg_time = modtime;
347 acg.cg_magic = CG_MAGIC;
349 acg.cg_niblk = sblock.fs_ipg;
350 acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
351 sblock.fs_ipg : 2 * INOPB(&sblock);
352 acg.cg_ndblk = dmax - cbase;
353 if (sblock.fs_contigsumsize > 0)
354 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
355 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
356 if (sblock.fs_magic == FS_UFS2_MAGIC) {
357 acg.cg_iusedoff = start;
359 acg.cg_old_ncyl = sblock.fs_old_cpg;
360 acg.cg_old_time = acg.cg_time;
362 acg.cg_old_niblk = acg.cg_niblk;
364 acg.cg_initediblk = 0;
365 acg.cg_old_btotoff = start;
366 acg.cg_old_boff = acg.cg_old_btotoff +
367 sblock.fs_old_cpg * sizeof(int32_t);
368 acg.cg_iusedoff = acg.cg_old_boff +
369 sblock.fs_old_cpg * sizeof(u_int16_t);
371 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
372 acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT);
373 if (sblock.fs_contigsumsize > 0) {
374 acg.cg_clustersumoff =
375 roundup(acg.cg_nextfreeoff, sizeof(u_int32_t));
376 acg.cg_clustersumoff -= sizeof(u_int32_t);
377 acg.cg_clusteroff = acg.cg_clustersumoff +
378 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
379 acg.cg_nextfreeoff = acg.cg_clusteroff +
380 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
382 if (acg.cg_nextfreeoff > (unsigned)sblock.fs_cgsize) {
384 * This should never happen as we would have had that panic
385 * already on file system creation
387 errx(37, "panic: cylinder group too big");
389 acg.cg_cs.cs_nifree += sblock.fs_ipg;
391 for (i = 0; i < ROOTINO; i++) {
392 setbit(cg_inosused(&acg), i);
393 acg.cg_cs.cs_nifree--;
396 * For the old file system, we have to initialize all the inodes.
398 if (sblock.fs_magic == FS_UFS1_MAGIC) {
399 bzero(iobuf, sblock.fs_bsize);
400 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock);
401 i += sblock.fs_frag) {
403 dp1 = (struct ufs1_dinode *)(void *)iobuf;
404 for (j = 0; j < INOPB(&sblock); j++) {
405 dp1->di_gen = random();
409 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
410 sblock.fs_bsize, iobuf, fso, Nflag);
415 * In cylno 0, beginning space is reserved
416 * for boot and super blocks.
418 for (d = 0; d < dlower; d += sblock.fs_frag) {
419 blkno = d / sblock.fs_frag;
420 setblock(&sblock, cg_blksfree(&acg), blkno);
421 if (sblock.fs_contigsumsize > 0)
422 setbit(cg_clustersfree(&acg), blkno);
423 acg.cg_cs.cs_nbfree++;
425 sblock.fs_dsize += dlower;
427 sblock.fs_dsize += acg.cg_ndblk - dupper;
428 if ((i = dupper % sblock.fs_frag)) {
429 acg.cg_frsum[sblock.fs_frag - i]++;
430 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
431 setbit(cg_blksfree(&acg), dupper);
432 acg.cg_cs.cs_nffree++;
435 for (d = dupper; d + sblock.fs_frag <= acg.cg_ndblk;
436 d += sblock.fs_frag) {
437 blkno = d / sblock.fs_frag;
438 setblock(&sblock, cg_blksfree(&acg), blkno);
439 if (sblock.fs_contigsumsize > 0)
440 setbit(cg_clustersfree(&acg), blkno);
441 acg.cg_cs.cs_nbfree++;
443 if (d < acg.cg_ndblk) {
444 acg.cg_frsum[acg.cg_ndblk - d]++;
445 for (; d < acg.cg_ndblk; d++) {
446 setbit(cg_blksfree(&acg), d);
447 acg.cg_cs.cs_nffree++;
450 if (sblock.fs_contigsumsize > 0) {
451 int32_t *sump = cg_clustersum(&acg);
452 u_char *mapp = cg_clustersfree(&acg);
457 for (i = 0; i < acg.cg_nclusterblks; i++) {
458 if ((map & bit) != 0)
461 if (run > sblock.fs_contigsumsize)
462 run = sblock.fs_contigsumsize;
466 if ((i & (CHAR_BIT - 1)) != CHAR_BIT - 1)
474 if (run > sblock.fs_contigsumsize)
475 run = sblock.fs_contigsumsize;
479 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
480 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
481 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
482 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
485 memcpy(iobuf, &acg, sblock.fs_cgsize);
486 memset(iobuf + sblock.fs_cgsize, '\0',
487 sblock.fs_bsize * 3 - sblock.fs_cgsize);
489 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
490 sblock.fs_bsize * 3, iobuf, fso, Nflag);
491 DBG_DUMP_CG(&sblock, "new cg", &acg);
498 * Here we add or subtract (sign +1/-1) the available fragments in a given
499 * block to or from the fragment statistics. By subtracting before and adding
500 * after an operation on the free frag map we can easy update the fragment
501 * statistic, which seems to be otherwise a rather complex operation.
504 frag_adjust(ufs2_daddr_t frag, int sign)
506 DBG_FUNC("frag_adjust")
514 * Here frag only needs to point to any fragment in the block we want
517 for (f = rounddown(frag, sblock.fs_frag);
518 f < roundup(frag + 1, sblock.fs_frag); f++) {
520 * Count contiguous free fragments.
522 if (isset(cg_blksfree(&acg), f)) {
525 if (fragsize && fragsize < sblock.fs_frag) {
527 * We found something in between.
529 acg.cg_frsum[fragsize]+=sign;
530 DBG_PRINT2("frag_adjust [%d]+=%d\n",
536 if (fragsize && fragsize < sblock.fs_frag) {
538 * We found something.
540 acg.cg_frsum[fragsize] += sign;
541 DBG_PRINT2("frag_adjust [%d]+=%d\n", fragsize, sign);
543 DBG_PRINT2("frag_adjust [[%d]]+=%d\n", fragsize, sign);
550 * Here we do all needed work for the former last cylinder group. It has to be
551 * changed in any case, even if the file system ended exactly on the end of
552 * this group, as there is some slightly inconsistent handling of the number
553 * of cylinders in the cylinder group. We start again by reading the cylinder
554 * group from disk. If the last block was not fully available, we first handle
555 * the missing fragments, then we handle all new full blocks in that file
556 * system and finally we handle the new last fragmented block in the file
557 * system. We again have to handle the fragment statistics rotational layout
558 * tables and cluster summary during all those operations.
561 updjcg(int cylno, time_t modtime, int fsi, int fso, unsigned int Nflag)
564 ufs2_daddr_t cbase, dmax, dupper;
572 * Read the former last (joining) cylinder group from disk, and make
575 rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
576 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
577 DBG_PRINT0("jcg read\n");
578 DBG_DUMP_CG(&sblock, "old joining cg", &aocg);
580 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
583 * If the cylinder group had already its new final size almost
584 * nothing is to be done ... except:
585 * For some reason the value of cg_ncyl in the last cylinder group has
586 * to be zero instead of fs_cpg. As this is now no longer the last
587 * cylinder group we have to change that value now to fs_cpg.
590 if (cgbase(&osblock, cylno + 1) == osblock.fs_size) {
591 if (sblock.fs_magic == FS_UFS1_MAGIC)
592 acg.cg_old_ncyl=sblock.fs_old_cpg;
594 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
595 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
596 DBG_PRINT0("jcg written\n");
597 DBG_DUMP_CG(&sblock, "new joining cg", &acg);
604 * Set up some variables needed later.
606 cbase = cgbase(&sblock, cylno);
607 dmax = cbase + sblock.fs_fpg;
608 if (dmax > sblock.fs_size)
609 dmax = sblock.fs_size;
610 dupper = cgdmin(&sblock, cylno) - cbase;
611 if (cylno == 0) /* XXX fscs may be relocated */
612 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
615 * Set pointer to the cylinder summary for our cylinder group.
620 * Touch the cylinder group, update all fields in the cylinder group as
621 * needed, update the free space in the superblock.
623 acg.cg_time = modtime;
624 if ((unsigned)cylno == sblock.fs_ncg - 1) {
626 * This is still the last cylinder group.
628 if (sblock.fs_magic == FS_UFS1_MAGIC)
630 sblock.fs_old_ncyl % sblock.fs_old_cpg;
632 acg.cg_old_ncyl = sblock.fs_old_cpg;
634 DBG_PRINT2("jcg dbg: %d %u", cylno, sblock.fs_ncg);
636 if (sblock.fs_magic == FS_UFS1_MAGIC)
637 DBG_PRINT2("%d %u", acg.cg_old_ncyl, sblock.fs_old_cpg);
640 acg.cg_ndblk = dmax - cbase;
641 sblock.fs_dsize += acg.cg_ndblk - aocg.cg_ndblk;
642 if (sblock.fs_contigsumsize > 0)
643 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
646 * Now we have to update the free fragment bitmap for our new free
647 * space. There again we have to handle the fragmentation and also
648 * the rotational layout tables and the cluster summary. This is
649 * also done per fragment for the first new block if the old file
650 * system end was not on a block boundary, per fragment for the new
651 * last block if the new file system end is not on a block boundary,
652 * and per block for all space in between.
654 * Handle the first new block here if it was partially available
657 if (osblock.fs_size % sblock.fs_frag) {
658 if (roundup(osblock.fs_size, sblock.fs_frag) <=
661 * The new space is enough to fill at least this
665 for (i = roundup(osblock.fs_size - cbase,
666 sblock.fs_frag) - 1; i >= osblock.fs_size - cbase;
668 setbit(cg_blksfree(&acg), i);
669 acg.cg_cs.cs_nffree++;
674 * Check if the fragment just created could join an
675 * already existing fragment at the former end of the
678 if (isblock(&sblock, cg_blksfree(&acg),
679 ((osblock.fs_size - cgbase(&sblock, cylno)) /
682 * The block is now completely available.
684 DBG_PRINT0("block was\n");
685 acg.cg_frsum[osblock.fs_size % sblock.fs_frag]--;
686 acg.cg_cs.cs_nbfree++;
687 acg.cg_cs.cs_nffree -= sblock.fs_frag;
688 k = rounddown(osblock.fs_size - cbase,
690 updclst((osblock.fs_size - cbase) /
694 * Lets rejoin a possible partially growed
698 while (isset(cg_blksfree(&acg), i) &&
699 (i >= rounddown(osblock.fs_size - cbase,
706 acg.cg_frsum[k + j]++;
710 * We only grow by some fragments within this last
713 for (i = sblock.fs_size - cbase - 1;
714 i >= osblock.fs_size - cbase; i--) {
715 setbit(cg_blksfree(&acg), i);
716 acg.cg_cs.cs_nffree++;
720 * Lets rejoin a possible partially growed fragment.
723 while (isset(cg_blksfree(&acg), i) &&
724 (i >= rounddown(osblock.fs_size - cbase,
731 acg.cg_frsum[k + j]++;
736 * Handle all new complete blocks here.
738 for (i = roundup(osblock.fs_size - cbase, sblock.fs_frag);
739 i + sblock.fs_frag <= dmax - cbase; /* XXX <= or only < ? */
740 i += sblock.fs_frag) {
741 j = i / sblock.fs_frag;
742 setblock(&sblock, cg_blksfree(&acg), j);
744 acg.cg_cs.cs_nbfree++;
748 * Handle the last new block if there are stll some new fragments left.
749 * Here we don't have to bother about the cluster summary or the even
750 * the rotational layout table.
752 if (i < (dmax - cbase)) {
753 acg.cg_frsum[dmax - cbase - i]++;
754 for (; i < dmax - cbase; i++) {
755 setbit(cg_blksfree(&acg), i);
756 acg.cg_cs.cs_nffree++;
760 sblock.fs_cstotal.cs_nffree +=
761 (acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree);
762 sblock.fs_cstotal.cs_nbfree +=
763 (acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree);
765 * The following statistics are not changed here:
766 * sblock.fs_cstotal.cs_ndir
767 * sblock.fs_cstotal.cs_nifree
768 * As the statistics for this cylinder group are ready, copy it to
769 * the summary information array.
774 * Write the updated "joining" cylinder group back to disk.
776 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
777 (void *)&acg, fso, Nflag);
778 DBG_PRINT0("jcg written\n");
779 DBG_DUMP_CG(&sblock, "new joining cg", &acg);
786 * Here we update the location of the cylinder summary. We have two possible
787 * ways of growing the cylinder summary.
788 * (1) We can try to grow the summary in the current location, and relocate
789 * possibly used blocks within the current cylinder group.
790 * (2) Alternatively we can relocate the whole cylinder summary to the first
791 * new completely empty cylinder group. Once the cylinder summary is no
792 * longer in the beginning of the first cylinder group you should never
793 * use a version of fsck which is not aware of the possibility to have
794 * this structure in a non standard place.
795 * Option (2) is considered to be less intrusive to the structure of the file-
796 * system, so that's the one being used.
799 updcsloc(time_t modtime, int fsi, int fso, unsigned int Nflag)
811 if (howmany(sblock.fs_cssize, sblock.fs_fsize) ==
812 howmany(osblock.fs_cssize, osblock.fs_fsize)) {
814 * No new fragment needed.
819 ocscg = dtog(&osblock, osblock.fs_csaddr);
821 blocks = 1 + howmany(sblock.fs_cssize, sblock.fs_bsize) -
822 howmany(osblock.fs_cssize, osblock.fs_bsize);
825 * Read original cylinder group from disk, and make a copy.
826 * XXX If Nflag is set in some very rare cases we now miss
827 * some changes done in updjcg by reading the unmodified
830 rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
831 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
832 DBG_PRINT0("oscg read\n");
833 DBG_DUMP_CG(&sblock, "old summary cg", &aocg);
835 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
838 * Touch the cylinder group, set up local variables needed later
839 * and update the superblock.
841 acg.cg_time = modtime;
844 * XXX In the case of having active snapshots we may need much more
845 * blocks for the copy on write. We need each block twice, and
846 * also up to 8*3 blocks for indirect blocks for all possible
850 * There is not enough space in the old cylinder group to
851 * relocate all blocks as needed, so we relocate the whole
852 * cylinder group summary to a new group. We try to use the
853 * first complete new cylinder group just created. Within the
854 * cylinder group we align the area immediately after the
855 * cylinder group information location in order to be as
856 * close as possible to the original implementation of ffs.
858 * First we have to make sure we'll find enough space in the
859 * new cylinder group. If not, then we currently give up.
860 * We start with freeing everything which was used by the
861 * fragments of the old cylinder summary in the current group.
862 * Now we write back the group meta data, read in the needed
863 * meta data from the new cylinder group, and start allocating
864 * within that group. Here we can assume, the group to be
865 * completely empty. Which makes the handling of fragments and
866 * clusters a lot easier.
869 if (sblock.fs_ncg - osblock.fs_ncg < 2)
870 errx(2, "panic: not enough space");
873 * Point "d" to the first fragment not used by the cylinder
876 d = osblock.fs_csaddr + (osblock.fs_cssize / osblock.fs_fsize);
879 * Set up last cluster size ("lcs") already here. Calculate
880 * the size for the trailing cluster just behind where "d"
883 if (sblock.fs_contigsumsize > 0) {
884 for (block = howmany(d % sblock.fs_fpg, sblock.fs_frag),
885 lcs = 0; lcs < sblock.fs_contigsumsize;
887 if (isclr(cg_clustersfree(&acg), block))
893 * Point "d" to the last frag used by the cylinder summary.
897 DBG_PRINT1("d=%jd\n", (intmax_t)d);
898 if ((d + 1) % sblock.fs_frag) {
900 * The end of the cylinder summary is not a complete
904 frag_adjust(d % sblock.fs_fpg, -1);
905 for (; (d + 1) % sblock.fs_frag; d--) {
906 DBG_PRINT1("d=%jd\n", (intmax_t)d);
907 setbit(cg_blksfree(&acg), d % sblock.fs_fpg);
908 acg.cg_cs.cs_nffree++;
909 sblock.fs_cstotal.cs_nffree++;
912 * Point "d" to the last fragment of the last
913 * (incomplete) block of the cylinder summary.
916 frag_adjust(d%sblock.fs_fpg, 1);
918 if (isblock(&sblock, cg_blksfree(&acg),
919 (d % sblock.fs_fpg) / sblock.fs_frag)) {
920 DBG_PRINT1("d=%jd\n", (intmax_t)d);
921 acg.cg_cs.cs_nffree -= sblock.fs_frag;
922 acg.cg_cs.cs_nbfree++;
923 sblock.fs_cstotal.cs_nffree -= sblock.fs_frag;
924 sblock.fs_cstotal.cs_nbfree++;
925 if (sblock.fs_contigsumsize > 0) {
926 setbit(cg_clustersfree(&acg),
927 (d % sblock.fs_fpg) /
929 if (lcs < sblock.fs_contigsumsize) {
931 cg_clustersum(&acg)[lcs]--;
933 cg_clustersum(&acg)[lcs]++;
938 * Point "d" to the first fragment of the block before
939 * the last incomplete block.
944 DBG_PRINT1("d=%jd\n", (intmax_t)d);
945 for (d = rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
946 d -= sblock.fs_frag) {
948 DBG_PRINT1("d=%jd\n", (intmax_t)d);
949 setblock(&sblock, cg_blksfree(&acg),
950 (d % sblock.fs_fpg) / sblock.fs_frag);
951 acg.cg_cs.cs_nbfree++;
952 sblock.fs_cstotal.cs_nbfree++;
953 if (sblock.fs_contigsumsize > 0) {
954 setbit(cg_clustersfree(&acg),
955 (d % sblock.fs_fpg) / sblock.fs_frag);
957 * The last cluster size is already set up.
959 if (lcs < sblock.fs_contigsumsize) {
961 cg_clustersum(&acg)[lcs]--;
963 cg_clustersum(&acg)[lcs]++;
970 * Now write the former cylinder group containing the cylinder
971 * summary back to disk.
973 wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
974 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
975 DBG_PRINT0("oscg written\n");
976 DBG_DUMP_CG(&sblock, "old summary cg", &acg);
979 * Find the beginning of the new cylinder group containing the
982 sblock.fs_csaddr = cgdmin(&sblock, osblock.fs_ncg);
983 ncscg = dtog(&sblock, sblock.fs_csaddr);
987 * If Nflag is specified, we would now read random data instead
988 * of an empty cg structure from disk. So we can't simulate that
992 DBG_PRINT0("nscg update skipped\n");
998 * Read the future cylinder group containing the cylinder
999 * summary from disk, and make a copy.
1001 rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1002 (size_t)sblock.fs_cgsize, (void *)&aocg, fsi);
1003 DBG_PRINT0("nscg read\n");
1004 DBG_DUMP_CG(&sblock, "new summary cg", &aocg);
1006 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
1009 * Allocate all complete blocks used by the new cylinder
1012 for (d = sblock.fs_csaddr; d + sblock.fs_frag <=
1013 sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize);
1014 d += sblock.fs_frag) {
1015 clrblock(&sblock, cg_blksfree(&acg),
1016 (d % sblock.fs_fpg) / sblock.fs_frag);
1017 acg.cg_cs.cs_nbfree--;
1018 sblock.fs_cstotal.cs_nbfree--;
1019 if (sblock.fs_contigsumsize > 0) {
1020 clrbit(cg_clustersfree(&acg),
1021 (d % sblock.fs_fpg) / sblock.fs_frag);
1026 * Allocate all fragments used by the cylinder summary in the
1030 sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize)) {
1031 for (; d - sblock.fs_csaddr <
1032 sblock.fs_cssize/sblock.fs_fsize; d++) {
1033 clrbit(cg_blksfree(&acg), d % sblock.fs_fpg);
1034 acg.cg_cs.cs_nffree--;
1035 sblock.fs_cstotal.cs_nffree--;
1037 acg.cg_cs.cs_nbfree--;
1038 acg.cg_cs.cs_nffree += sblock.fs_frag;
1039 sblock.fs_cstotal.cs_nbfree--;
1040 sblock.fs_cstotal.cs_nffree += sblock.fs_frag;
1041 if (sblock.fs_contigsumsize > 0)
1042 clrbit(cg_clustersfree(&acg),
1043 (d % sblock.fs_fpg) / sblock.fs_frag);
1045 frag_adjust(d % sblock.fs_fpg, 1);
1048 * XXX Handle the cluster statistics here in the case this
1049 * cylinder group is now almost full, and the remaining
1050 * space is less then the maximum cluster size. This is
1051 * probably not needed, as you would hardly find a file
1052 * system which has only MAXCSBUFS+FS_MAXCONTIG of free
1053 * space right behind the cylinder group information in
1054 * any new cylinder group.
1058 * Update our statistics in the cylinder summary.
1063 * Write the new cylinder group containing the cylinder summary
1066 wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1067 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1068 DBG_PRINT0("nscg written\n");
1069 DBG_DUMP_CG(&sblock, "new summary cg", &acg);
1076 * Here we read some block(s) from disk.
1079 rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi)
1087 err(32, "rdfs: attempting to read negative block number");
1088 if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0)
1089 err(33, "rdfs: seek error: %jd", (intmax_t)bno);
1090 n = read(fsi, bf, size);
1091 if (n != (ssize_t)size)
1092 err(34, "rdfs: read error: %jd", (intmax_t)bno);
1099 * Here we write some block(s) to disk.
1102 wtfs(ufs2_daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1113 if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0)
1114 err(35, "wtfs: seek error: %ld", (long)bno);
1115 n = write(fso, bf, size);
1116 if (n != (ssize_t)size)
1117 err(36, "wtfs: write error: %ld", (long)bno);
1124 * Here we check if all frags of a block are free. For more details again
1125 * please see the source of newfs(8), as this function is taken over almost
1129 isblock(struct fs *fs, unsigned char *cp, int h)
1136 switch (fs->fs_frag) {
1139 return (cp[h] == 0xff);
1141 mask = 0x0f << ((h & 0x1) << 2);
1143 return ((cp[h >> 1] & mask) == mask);
1145 mask = 0x03 << ((h & 0x3) << 1);
1147 return ((cp[h >> 2] & mask) == mask);
1149 mask = 0x01 << (h & 0x7);
1151 return ((cp[h >> 3] & mask) == mask);
1153 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1160 * Here we allocate a complete block in the block map. For more details again
1161 * please see the source of newfs(8), as this function is taken over almost
1165 clrblock(struct fs *fs, unsigned char *cp, int h)
1167 DBG_FUNC("clrblock")
1171 switch ((fs)->fs_frag) {
1176 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1179 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1182 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1185 warnx("clrblock bad fs_frag %d", fs->fs_frag);
1194 * Here we free a complete block in the free block map. For more details again
1195 * please see the source of newfs(8), as this function is taken over almost
1199 setblock(struct fs *fs, unsigned char *cp, int h)
1201 DBG_FUNC("setblock")
1205 switch (fs->fs_frag) {
1210 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1213 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1216 cp[h >> 3] |= (0x01 << (h & 0x7));
1219 warnx("setblock bad fs_frag %d", fs->fs_frag);
1228 * Figure out how many lines our current terminal has. For more details again
1229 * please see the source of newfs(8), as this function is taken over almost
1235 DBG_FUNC("charsperline")
1243 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1244 columns = ws.ws_col;
1245 if (columns == 0 && (cp = getenv("COLUMNS")))
1248 columns = 80; /* last resort */
1255 * Get the size of the partition.
1258 get_dev_size(int fd, int *size)
1263 if (ioctl(fd, DIOCGSECTORSIZE, §orsize) == -1)
1264 err(1,"DIOCGSECTORSIZE");
1265 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == -1)
1266 err(1,"DIOCGMEDIASIZE");
1268 if (sectorsize <= 0)
1269 errx(1, "bogus sectorsize: %d", sectorsize);
1271 *size = mediasize / sectorsize;
1275 * growfs(8) is a utility which allows to increase the size of an existing
1276 * ufs file system. Currently this can only be done on unmounted file system.
1277 * It recognizes some command line options to specify the new desired size,
1278 * and it does some basic checkings. The old file system size is determined
1279 * and after some more checks like we can really access the new last block
1280 * on the disk etc. we calculate the new parameters for the superblock. After
1281 * having done this we just call growfs() which will do the work.
1282 * We still have to provide support for snapshots. Therefore we first have to
1283 * understand what data structures are always replicated in the snapshot on
1284 * creation, for all other blocks we touch during our procedure, we have to
1285 * keep the old blocks unchanged somewhere available for the snapshots. If we
1286 * are lucky, then we only have to handle our blocks to be relocated in that
1288 * Also we have to consider in what order we actually update the critical
1289 * data structures of the file system to make sure, that in case of a disaster
1290 * fsck(8) is still able to restore any lost data.
1291 * The foreseen last step then will be to provide for growing even mounted
1292 * file systems. There we have to extend the mount() system call to provide
1293 * userland access to the file system locking facility.
1296 main(int argc, char **argv)
1299 char *device, *special;
1301 unsigned int size = 0;
1303 unsigned int Nflag = 0;
1311 #endif /* FSMAXSNAP */
1315 while ((ch = getopt(argc, argv, "Ns:vy")) != -1) {
1321 size = (size_t)atol(optarg);
1325 case 'v': /* for compatibility to newfs */
1345 * Now try to guess the (raw)device name.
1347 if (0 == strrchr(device, '/')) {
1349 * No path prefix was given, so try in that order:
1355 * FreeBSD now doesn't distinguish between raw and block
1356 * devices any longer, but it should still work this way.
1358 len = strlen(device) + strlen(_PATH_DEV) + 2 + strlen("vinum/");
1359 special = (char *)malloc(len);
1360 if (special == NULL)
1361 errx(1, "malloc failed");
1362 snprintf(special, len, "%sr%s", _PATH_DEV, device);
1363 if (stat(special, &st) == -1) {
1364 snprintf(special, len, "%s%s", _PATH_DEV, device);
1365 if (stat(special, &st) == -1) {
1366 snprintf(special, len, "%svinum/r%s",
1368 if (stat(special, &st) == -1) {
1369 /* For now this is the 'last resort' */
1370 snprintf(special, len, "%svinum/%s",
1379 * Try to access our devices for writing ...
1384 fso = open(device, O_WRONLY);
1386 err(1, "%s", device);
1392 fsi = open(device, O_RDONLY);
1394 err(1, "%s", device);
1397 * Try to guess the slice if not specified. This code should guess
1398 * the right thing and avoid to bother the user with the task
1399 * of specifying the option -v on vinum volumes.
1401 get_dev_size(fsi, &p_size);
1404 * Check if that partition is suitable for growing a file system.
1407 errx(1, "partition is unavailable");
1410 * Read the current superblock, and take a backup.
1412 for (i = 0; sblock_try[i] != -1; i++) {
1413 sblockloc = sblock_try[i] / DEV_BSIZE;
1414 rdfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&(osblock), fsi);
1415 if ((osblock.fs_magic == FS_UFS1_MAGIC ||
1416 (osblock.fs_magic == FS_UFS2_MAGIC &&
1417 osblock.fs_sblockloc == sblock_try[i])) &&
1418 osblock.fs_bsize <= MAXBSIZE &&
1419 osblock.fs_bsize >= (int32_t) sizeof(struct fs))
1422 if (sblock_try[i] == -1)
1423 errx(1, "superblock not recognized");
1424 memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2));
1426 DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
1427 DBG_DUMP_FS(&sblock, "old sblock");
1430 * Determine size to grow to. Default to the device size.
1432 sblock.fs_size = dbtofsb(&osblock, p_size);
1435 errx(1, "there is not enough space (%d < %d)",
1437 sblock.fs_size = dbtofsb(&osblock, size);
1441 * Are we really growing ?
1443 if (osblock.fs_size >= sblock.fs_size) {
1444 errx(1, "we are not growing (%jd->%jd)",
1445 (intmax_t)osblock.fs_size, (intmax_t)sblock.fs_size);
1451 * Check if we find an active snapshot.
1453 if (ExpertFlag == 0) {
1454 for (j = 0; j < FSMAXSNAP; j++) {
1455 if (sblock.fs_snapinum[j]) {
1456 errx(1, "active snapshot found in file system; "
1457 "please remove all snapshots before "
1460 if (!sblock.fs_snapinum[j]) /* list is dense */
1466 if (ExpertFlag == 0 && Nflag == 0) {
1467 printf("We strongly recommend you to make a backup "
1468 "before growing the file system.\n"
1469 "Did you backup your data (Yes/No)? ");
1470 fgets(reply, (int)sizeof(reply), stdin);
1471 if (strcmp(reply, "Yes\n")){
1472 printf("\nNothing done\n");
1477 printf("New file system size is %jd frags\n", (intmax_t)sblock.fs_size);
1480 * Try to access our new last block in the file system. Even if we
1481 * later on realize we have to abort our operation, on that block
1482 * there should be no data, so we can't destroy something yet.
1484 wtfs((ufs2_daddr_t)p_size - 1, (size_t)DEV_BSIZE, (void *)&sblock,
1488 * Now calculate new superblock values and check for reasonable
1489 * bound for new file system size:
1490 * fs_size: is derived from user input
1491 * fs_dsize: should get updated in the routines creating or
1492 * updating the cylinder groups on the fly
1493 * fs_cstotal: should get updated in the routines creating or
1494 * updating the cylinder groups
1498 * Update the number of cylinders and cylinder groups in the file system.
1500 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1501 sblock.fs_old_ncyl =
1502 sblock.fs_size * sblock.fs_old_nspf / sblock.fs_old_spc;
1503 if (sblock.fs_size * sblock.fs_old_nspf >
1504 sblock.fs_old_ncyl * sblock.fs_old_spc)
1505 sblock.fs_old_ncyl++;
1507 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
1509 if (sblock.fs_size % sblock.fs_fpg != 0 &&
1510 sblock.fs_size % sblock.fs_fpg < cgdmin(&sblock, sblock.fs_ncg)) {
1512 * The space in the new last cylinder group is too small,
1516 if (sblock.fs_magic == FS_UFS1_MAGIC)
1517 sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg;
1518 printf("Warning: %jd sector(s) cannot be allocated.\n",
1519 (intmax_t)fsbtodb(&sblock, sblock.fs_size % sblock.fs_fpg));
1520 sblock.fs_size = sblock.fs_ncg * sblock.fs_fpg;
1524 * Update the space for the cylinder group summary information in the
1525 * respective cylinder group data area.
1528 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
1530 if (osblock.fs_size >= sblock.fs_size)
1531 errx(1, "not enough new space");
1533 DBG_PRINT0("sblock calculated\n");
1536 * Ok, everything prepared, so now let's do the tricks.
1538 growfs(fsi, fso, Nflag);
1551 * Dump a line of usage.
1560 fprintf(stderr, "usage: growfs [-Ny] [-s size] special\n");
1567 * This updates most parameters and the bitmap related to cluster. We have to
1568 * assume that sblock, osblock, acg are set up.
1578 if (sblock.fs_contigsumsize < 1) /* no clustering */
1581 * update cluster allocation map
1583 setbit(cg_clustersfree(&acg), block);
1586 * update cluster summary table
1590 * calculate size for the trailing cluster
1592 for (block--; lcs < sblock.fs_contigsumsize; block--, lcs++ ) {
1593 if (isclr(cg_clustersfree(&acg), block))
1597 if (lcs < sblock.fs_contigsumsize) {
1599 cg_clustersum(&acg)[lcs]--;
1601 cg_clustersum(&acg)[lcs]++;