2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
5 * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
6 * Copyright (c) 2012 The FreeBSD Foundation
9 * This code is derived from software contributed to Berkeley by
10 * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
12 * Portions of this software were developed by Edward Tomasz Napierala
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgment:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors, as well as Christoph
27 * Herrmann and Thomas-Henning von Kamptz.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $
48 #include <sys/param.h>
49 #include <sys/ioctl.h>
52 #include <sys/ucred.h>
53 #include <sys/mount.h>
71 #include <ufs/ufs/dinode.h>
72 #include <ufs/ffs/fs.h>
79 int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
86 #define sblock fsun1.fs /* the new superblock */
87 #define osblock fsun2.fs /* the old superblock */
93 #define acg cgun1.cg /* a cylinder cgroup (new) */
94 #define aocg cgun2.cg /* an old cylinder group */
96 static struct csum *fscs; /* cylinder summary */
98 static void growfs(int, int, unsigned int);
99 static void rdfs(ufs2_daddr_t, size_t, void *, int);
100 static void wtfs(ufs2_daddr_t, size_t, void *, int, unsigned int);
101 static int charsperline(void);
102 static void usage(void);
103 static int isblock(struct fs *, unsigned char *, int);
104 static void clrblock(struct fs *, unsigned char *, int);
105 static void setblock(struct fs *, unsigned char *, int);
106 static void initcg(int, time_t, int, unsigned int);
107 static void updjcg(int, time_t, int, int, unsigned int);
108 static void updcsloc(time_t, int, int, unsigned int);
109 static void frag_adjust(ufs2_daddr_t, int);
110 static void updclst(int);
111 static void cgckhash(struct cg *);
114 * Here we actually start growing the file system. We basically read the
115 * cylinder summary from the first cylinder group as we want to update
116 * this on the fly during our various operations. First we handle the
117 * changes in the former last cylinder group. Afterwards we create all new
118 * cylinder groups. Now we handle the cylinder group containing the
119 * cylinder summary which might result in a relocation of the whole
120 * structure. In the end we write back the updated cylinder summary, the
121 * new superblock, and slightly patched versions of the super block
125 growfs(int fsi, int fso, unsigned int Nflag)
138 * Get the cylinder summary into the memory.
140 fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize);
142 errx(3, "calloc failed");
143 memcpy(fscs, osblock.fs_csp, osblock.fs_cssize);
144 free(osblock.fs_csp);
145 osblock.fs_csp = NULL;
146 sblock.fs_csp = fscs;
150 struct csum *dbg_csp;
156 for (dbg_csc = 0; dbg_csc < osblock.fs_ncg; dbg_csc++) {
157 snprintf(dbg_line, sizeof(dbg_line),
158 "%d. old csum in old location", dbg_csc);
159 DBG_DUMP_CSUM(&osblock, dbg_line, dbg_csp++);
162 #endif /* FS_DEBUG */
163 DBG_PRINT0("fscs read\n");
166 * Do all needed changes in the former last cylinder group.
168 updjcg(osblock.fs_ncg - 1, modtime, fsi, fso, Nflag);
171 * Dump out summary information about file system.
174 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
175 printf("growfs: %.1fMB (%jd sectors) block size %d, fragment size %d\n",
176 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
177 (intmax_t)fsbtodb(&sblock, sblock.fs_size), sblock.fs_bsize,
179 printf("\tusing %d cylinder groups of %.2fMB, %d blks, %d inodes.\n",
180 sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
181 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
182 if (sblock.fs_flags & FS_DOSOFTDEP)
183 printf("\twith soft updates\n");
185 #endif /* FS_DEBUG */
188 * Now build the cylinders group blocks and
189 * then print out indices of cylinder groups.
191 printf("super-block backups (for fsck_ffs -b #) at:\n");
193 width = charsperline();
196 * Iterate for only the new cylinder groups.
198 for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
199 initcg(cylno, modtime, fso, Nflag);
200 j = sprintf(tmpbuf, " %jd%s",
201 (intmax_t)fsbtodb(&sblock, cgsblock(&sblock, cylno)),
202 cylno < (sblock.fs_ncg - 1) ? "," : "" );
203 if (i + j >= width) {
208 printf("%s", tmpbuf);
214 * Do all needed changes in the first cylinder group.
215 * allocate blocks in new location
217 updcsloc(modtime, fsi, fso, Nflag);
220 * Clean up the dynamic fields in our superblock.
223 * The following fields are currently distributed from the superblock
233 * We probably should rather change the summary for the cylinder group
234 * statistics here to the value of what would be in there, if the file
235 * system were created initially with the new size. Therefore we still
236 * need to find an easy way of calculating that.
237 * Possibly we can try to read the first superblock copy and apply the
238 * "diffed" stats between the old and new superblock by still copying
239 * certain parameters onto that.
241 sblock.fs_time = modtime;
245 sblock.fs_cgrotor = 0;
247 memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
250 * Now write the new superblock, its summary information,
251 * and all the alternates back to disk.
253 if (!Nflag && sbput(fso, &sblock, sblock.fs_ncg) != 0)
254 errc(3, EIO, "could not write updated superblock");
255 DBG_PRINT0("fscs written\n");
259 struct csum *dbg_csp;
264 for (dbg_csc = 0; dbg_csc < sblock.fs_ncg; dbg_csc++) {
265 snprintf(dbg_line, sizeof(dbg_line),
266 "%d. new csum in new location", dbg_csc);
267 DBG_DUMP_CSUM(&sblock, dbg_line, dbg_csp++);
270 #endif /* FS_DEBUG */
272 DBG_PRINT0("sblock written\n");
273 DBG_DUMP_FS(&sblock, "new initial sblock");
275 DBG_PRINT0("sblock copies written\n");
276 DBG_DUMP_FS(&sblock, "new other sblocks");
283 * This creates a new cylinder group structure, for more details please see
284 * the source of newfs(8), as this function is taken over almost unchanged.
285 * As this is never called for the first cylinder group, the special
286 * provisions for that case are removed here.
289 initcg(int cylno, time_t modtime, int fso, unsigned int Nflag)
292 static caddr_t iobuf;
293 static long iobufsize;
296 ufs2_daddr_t i, cbase, dmax;
297 struct ufs1_dinode *dp1;
298 struct ufs2_dinode *dp2;
300 uint j, d, dupper, dlower;
303 iobufsize = 2 * sblock.fs_bsize;
304 if ((iobuf = malloc(iobufsize)) == NULL)
305 errx(37, "panic: cannot allocate I/O buffer");
306 memset(iobuf, '\0', iobufsize);
309 * Determine block bounds for cylinder group.
310 * Allow space for super block summary information in first
313 cbase = cgbase(&sblock, cylno);
314 dmax = cbase + sblock.fs_fpg;
315 if (dmax > sblock.fs_size)
316 dmax = sblock.fs_size;
317 dlower = cgsblock(&sblock, cylno) - cbase;
318 dupper = cgdmin(&sblock, cylno) - cbase;
319 if (cylno == 0) /* XXX fscs may be relocated */
320 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
322 memset(&acg, 0, sblock.fs_cgsize);
323 acg.cg_time = modtime;
324 acg.cg_magic = CG_MAGIC;
326 acg.cg_niblk = sblock.fs_ipg;
327 acg.cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
328 acg.cg_ndblk = dmax - cbase;
329 if (sblock.fs_contigsumsize > 0)
330 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
332 if (sblock.fs_magic == FS_UFS2_MAGIC) {
333 acg.cg_iusedoff = start;
335 acg.cg_old_ncyl = sblock.fs_old_cpg;
336 acg.cg_old_time = acg.cg_time;
338 acg.cg_old_niblk = acg.cg_niblk;
340 acg.cg_initediblk = 0;
341 acg.cg_old_btotoff = start;
342 acg.cg_old_boff = acg.cg_old_btotoff +
343 sblock.fs_old_cpg * sizeof(int32_t);
344 acg.cg_iusedoff = acg.cg_old_boff +
345 sblock.fs_old_cpg * sizeof(u_int16_t);
347 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
348 acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT);
349 if (sblock.fs_contigsumsize > 0) {
350 acg.cg_clustersumoff =
351 roundup(acg.cg_nextfreeoff, sizeof(u_int32_t));
352 acg.cg_clustersumoff -= sizeof(u_int32_t);
353 acg.cg_clusteroff = acg.cg_clustersumoff +
354 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
355 acg.cg_nextfreeoff = acg.cg_clusteroff +
356 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
358 if (acg.cg_nextfreeoff > (unsigned)sblock.fs_cgsize) {
360 * This should never happen as we would have had that panic
361 * already on file system creation
363 errx(37, "panic: cylinder group too big");
365 acg.cg_cs.cs_nifree += sblock.fs_ipg;
367 for (ino = 0; ino < UFS_ROOTINO; ino++) {
368 setbit(cg_inosused(&acg), ino);
369 acg.cg_cs.cs_nifree--;
372 * Initialize the initial inode blocks.
374 dp1 = (struct ufs1_dinode *)(void *)iobuf;
375 dp2 = (struct ufs2_dinode *)(void *)iobuf;
376 for (i = 0; i < acg.cg_initediblk; i++) {
377 if (sblock.fs_magic == FS_UFS1_MAGIC) {
378 dp1->di_gen = arc4random();
381 dp2->di_gen = arc4random();
385 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno)), iobufsize, iobuf,
388 * For the old file system, we have to initialize all the inodes.
390 if (sblock.fs_magic == FS_UFS1_MAGIC &&
391 sblock.fs_ipg > 2 * INOPB(&sblock)) {
392 for (i = 2 * sblock.fs_frag;
393 i < sblock.fs_ipg / INOPF(&sblock);
394 i += sblock.fs_frag) {
395 dp1 = (struct ufs1_dinode *)(void *)iobuf;
396 for (j = 0; j < INOPB(&sblock); j++) {
397 dp1->di_gen = arc4random();
400 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
401 sblock.fs_bsize, iobuf, fso, Nflag);
406 * In cylno 0, beginning space is reserved
407 * for boot and super blocks.
409 for (d = 0; d < dlower; d += sblock.fs_frag) {
410 blkno = d / sblock.fs_frag;
411 setblock(&sblock, cg_blksfree(&acg), blkno);
412 if (sblock.fs_contigsumsize > 0)
413 setbit(cg_clustersfree(&acg), blkno);
414 acg.cg_cs.cs_nbfree++;
416 sblock.fs_dsize += dlower;
418 sblock.fs_dsize += acg.cg_ndblk - dupper;
419 sblock.fs_old_dsize = sblock.fs_dsize;
420 if ((i = dupper % sblock.fs_frag)) {
421 acg.cg_frsum[sblock.fs_frag - i]++;
422 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
423 setbit(cg_blksfree(&acg), dupper);
424 acg.cg_cs.cs_nffree++;
427 for (d = dupper; d + sblock.fs_frag <= acg.cg_ndblk;
428 d += sblock.fs_frag) {
429 blkno = d / sblock.fs_frag;
430 setblock(&sblock, cg_blksfree(&acg), blkno);
431 if (sblock.fs_contigsumsize > 0)
432 setbit(cg_clustersfree(&acg), blkno);
433 acg.cg_cs.cs_nbfree++;
435 if (d < acg.cg_ndblk) {
436 acg.cg_frsum[acg.cg_ndblk - d]++;
437 for (; d < acg.cg_ndblk; d++) {
438 setbit(cg_blksfree(&acg), d);
439 acg.cg_cs.cs_nffree++;
442 if (sblock.fs_contigsumsize > 0) {
443 int32_t *sump = cg_clustersum(&acg);
444 u_char *mapp = cg_clustersfree(&acg);
449 for (i = 0; i < acg.cg_nclusterblks; i++) {
450 if ((map & bit) != 0)
453 if (run > sblock.fs_contigsumsize)
454 run = sblock.fs_contigsumsize;
458 if ((i & (CHAR_BIT - 1)) != CHAR_BIT - 1)
466 if (run > sblock.fs_contigsumsize)
467 run = sblock.fs_contigsumsize;
471 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
472 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
473 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
474 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
478 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), sblock.fs_cgsize, &acg,
480 DBG_DUMP_CG(&sblock, "new cg", &acg);
487 * Here we add or subtract (sign +1/-1) the available fragments in a given
488 * block to or from the fragment statistics. By subtracting before and adding
489 * after an operation on the free frag map we can easy update the fragment
490 * statistic, which seems to be otherwise a rather complex operation.
493 frag_adjust(ufs2_daddr_t frag, int sign)
495 DBG_FUNC("frag_adjust")
503 * Here frag only needs to point to any fragment in the block we want
506 for (f = rounddown(frag, sblock.fs_frag);
507 f < roundup(frag + 1, sblock.fs_frag); f++) {
509 * Count contiguous free fragments.
511 if (isset(cg_blksfree(&acg), f)) {
514 if (fragsize && fragsize < sblock.fs_frag) {
516 * We found something in between.
518 acg.cg_frsum[fragsize] += sign;
519 DBG_PRINT2("frag_adjust [%d]+=%d\n",
525 if (fragsize && fragsize < sblock.fs_frag) {
527 * We found something.
529 acg.cg_frsum[fragsize] += sign;
530 DBG_PRINT2("frag_adjust [%d]+=%d\n", fragsize, sign);
532 DBG_PRINT2("frag_adjust [[%d]]+=%d\n", fragsize, sign);
539 * Here we do all needed work for the former last cylinder group. It has to be
540 * changed in any case, even if the file system ended exactly on the end of
541 * this group, as there is some slightly inconsistent handling of the number
542 * of cylinders in the cylinder group. We start again by reading the cylinder
543 * group from disk. If the last block was not fully available, we first handle
544 * the missing fragments, then we handle all new full blocks in that file
545 * system and finally we handle the new last fragmented block in the file
546 * system. We again have to handle the fragment statistics rotational layout
547 * tables and cluster summary during all those operations.
550 updjcg(int cylno, time_t modtime, int fsi, int fso, unsigned int Nflag)
553 ufs2_daddr_t cbase, dmax;
561 * Read the former last (joining) cylinder group from disk, and make
564 rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
565 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
566 DBG_PRINT0("jcg read\n");
567 DBG_DUMP_CG(&sblock, "old joining cg", &aocg);
569 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
572 * If the cylinder group had already its new final size almost
573 * nothing is to be done ... except:
574 * For some reason the value of cg_ncyl in the last cylinder group has
575 * to be zero instead of fs_cpg. As this is now no longer the last
576 * cylinder group we have to change that value now to fs_cpg.
579 if (cgbase(&osblock, cylno + 1) == osblock.fs_size) {
580 if (sblock.fs_magic == FS_UFS1_MAGIC)
581 acg.cg_old_ncyl = sblock.fs_old_cpg;
584 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
585 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
586 DBG_PRINT0("jcg written\n");
587 DBG_DUMP_CG(&sblock, "new joining cg", &acg);
594 * Set up some variables needed later.
596 cbase = cgbase(&sblock, cylno);
597 dmax = cbase + sblock.fs_fpg;
598 if (dmax > sblock.fs_size)
599 dmax = sblock.fs_size;
602 * Set pointer to the cylinder summary for our cylinder group.
607 * Touch the cylinder group, update all fields in the cylinder group as
608 * needed, update the free space in the superblock.
610 acg.cg_time = modtime;
611 if ((unsigned)cylno == sblock.fs_ncg - 1) {
613 * This is still the last cylinder group.
615 if (sblock.fs_magic == FS_UFS1_MAGIC)
617 sblock.fs_old_ncyl % sblock.fs_old_cpg;
619 acg.cg_old_ncyl = sblock.fs_old_cpg;
621 DBG_PRINT2("jcg dbg: %d %u", cylno, sblock.fs_ncg);
623 if (sblock.fs_magic == FS_UFS1_MAGIC)
624 DBG_PRINT2("%d %u", acg.cg_old_ncyl, sblock.fs_old_cpg);
627 acg.cg_ndblk = dmax - cbase;
628 sblock.fs_dsize += acg.cg_ndblk - aocg.cg_ndblk;
629 sblock.fs_old_dsize = sblock.fs_dsize;
630 if (sblock.fs_contigsumsize > 0)
631 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
634 * Now we have to update the free fragment bitmap for our new free
635 * space. There again we have to handle the fragmentation and also
636 * the rotational layout tables and the cluster summary. This is
637 * also done per fragment for the first new block if the old file
638 * system end was not on a block boundary, per fragment for the new
639 * last block if the new file system end is not on a block boundary,
640 * and per block for all space in between.
642 * Handle the first new block here if it was partially available
645 if (osblock.fs_size % sblock.fs_frag) {
646 if (roundup(osblock.fs_size, sblock.fs_frag) <=
649 * The new space is enough to fill at least this
653 for (i = roundup(osblock.fs_size - cbase,
654 sblock.fs_frag) - 1; i >= osblock.fs_size - cbase;
656 setbit(cg_blksfree(&acg), i);
657 acg.cg_cs.cs_nffree++;
662 * Check if the fragment just created could join an
663 * already existing fragment at the former end of the
666 if (isblock(&sblock, cg_blksfree(&acg),
667 ((osblock.fs_size - cgbase(&sblock, cylno)) /
670 * The block is now completely available.
672 DBG_PRINT0("block was\n");
673 acg.cg_frsum[osblock.fs_size % sblock.fs_frag]--;
674 acg.cg_cs.cs_nbfree++;
675 acg.cg_cs.cs_nffree -= sblock.fs_frag;
676 k = rounddown(osblock.fs_size - cbase,
678 updclst((osblock.fs_size - cbase) /
682 * Lets rejoin a possible partially grown
686 while (isset(cg_blksfree(&acg), i) &&
687 (i >= rounddown(osblock.fs_size - cbase,
694 acg.cg_frsum[k + j]++;
698 * We only grow by some fragments within this last
701 for (i = sblock.fs_size - cbase - 1;
702 i >= osblock.fs_size - cbase; i--) {
703 setbit(cg_blksfree(&acg), i);
704 acg.cg_cs.cs_nffree++;
708 * Lets rejoin a possible partially grown fragment.
711 while (isset(cg_blksfree(&acg), i) &&
712 (i >= rounddown(osblock.fs_size - cbase,
719 acg.cg_frsum[k + j]++;
724 * Handle all new complete blocks here.
726 for (i = roundup(osblock.fs_size - cbase, sblock.fs_frag);
727 i + sblock.fs_frag <= dmax - cbase; /* XXX <= or only < ? */
728 i += sblock.fs_frag) {
729 j = i / sblock.fs_frag;
730 setblock(&sblock, cg_blksfree(&acg), j);
732 acg.cg_cs.cs_nbfree++;
736 * Handle the last new block if there are still some new fragments left.
737 * Here we don't have to bother about the cluster summary or the even
738 * the rotational layout table.
740 if (i < (dmax - cbase)) {
741 acg.cg_frsum[dmax - cbase - i]++;
742 for (; i < dmax - cbase; i++) {
743 setbit(cg_blksfree(&acg), i);
744 acg.cg_cs.cs_nffree++;
748 sblock.fs_cstotal.cs_nffree +=
749 (acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree);
750 sblock.fs_cstotal.cs_nbfree +=
751 (acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree);
753 * The following statistics are not changed here:
754 * sblock.fs_cstotal.cs_ndir
755 * sblock.fs_cstotal.cs_nifree
756 * As the statistics for this cylinder group are ready, copy it to
757 * the summary information array.
762 * Write the updated "joining" cylinder group back to disk.
765 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
766 (void *)&acg, fso, Nflag);
767 DBG_PRINT0("jcg written\n");
768 DBG_DUMP_CG(&sblock, "new joining cg", &acg);
775 * Here we update the location of the cylinder summary. We have two possible
776 * ways of growing the cylinder summary:
777 * (1) We can try to grow the summary in the current location, and relocate
778 * possibly used blocks within the current cylinder group.
779 * (2) Alternatively we can relocate the whole cylinder summary to the first
780 * new completely empty cylinder group. Once the cylinder summary is no
781 * longer in the beginning of the first cylinder group you should never
782 * use a version of fsck which is not aware of the possibility to have
783 * this structure in a non standard place.
784 * Option (2) is considered to be less intrusive to the structure of the file-
785 * system, so that's the one being used.
788 updcsloc(time_t modtime, int fsi, int fso, unsigned int Nflag)
799 if (howmany(sblock.fs_cssize, sblock.fs_fsize) ==
800 howmany(osblock.fs_cssize, osblock.fs_fsize)) {
802 * No new fragment needed.
807 /* Adjust fs_dsize by added summary blocks */
808 sblock.fs_dsize -= howmany(sblock.fs_cssize, sblock.fs_fsize) -
809 howmany(osblock.fs_cssize, osblock.fs_fsize);
810 sblock.fs_old_dsize = sblock.fs_dsize;
811 ocscg = dtog(&osblock, osblock.fs_csaddr);
815 * Read original cylinder group from disk, and make a copy.
816 * XXX If Nflag is set in some very rare cases we now miss
817 * some changes done in updjcg by reading the unmodified
820 rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
821 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
822 DBG_PRINT0("oscg read\n");
823 DBG_DUMP_CG(&sblock, "old summary cg", &aocg);
825 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
828 * Touch the cylinder group, set up local variables needed later
829 * and update the superblock.
831 acg.cg_time = modtime;
834 * XXX In the case of having active snapshots we may need much more
835 * blocks for the copy on write. We need each block twice, and
836 * also up to 8*3 blocks for indirect blocks for all possible
840 * There is not enough space in the old cylinder group to
841 * relocate all blocks as needed, so we relocate the whole
842 * cylinder group summary to a new group. We try to use the
843 * first complete new cylinder group just created. Within the
844 * cylinder group we align the area immediately after the
845 * cylinder group information location in order to be as
846 * close as possible to the original implementation of ffs.
848 * First we have to make sure we'll find enough space in the
849 * new cylinder group. If not, then we currently give up.
850 * We start with freeing everything which was used by the
851 * fragments of the old cylinder summary in the current group.
852 * Now we write back the group meta data, read in the needed
853 * meta data from the new cylinder group, and start allocating
854 * within that group. Here we can assume, the group to be
855 * completely empty. Which makes the handling of fragments and
856 * clusters a lot easier.
859 if (sblock.fs_ncg - osblock.fs_ncg < 2)
860 errx(2, "panic: not enough space");
863 * Point "d" to the first fragment not used by the cylinder
866 d = osblock.fs_csaddr + (osblock.fs_cssize / osblock.fs_fsize);
869 * Set up last cluster size ("lcs") already here. Calculate
870 * the size for the trailing cluster just behind where "d"
873 if (sblock.fs_contigsumsize > 0) {
874 for (block = howmany(d % sblock.fs_fpg, sblock.fs_frag),
875 lcs = 0; lcs < sblock.fs_contigsumsize; block++, lcs++) {
876 if (isclr(cg_clustersfree(&acg), block))
882 * Point "d" to the last frag used by the cylinder summary.
886 DBG_PRINT1("d=%jd\n", (intmax_t)d);
887 if ((d + 1) % sblock.fs_frag) {
889 * The end of the cylinder summary is not a complete
893 frag_adjust(d % sblock.fs_fpg, -1);
894 for (; (d + 1) % sblock.fs_frag; d--) {
895 DBG_PRINT1("d=%jd\n", (intmax_t)d);
896 setbit(cg_blksfree(&acg), d % sblock.fs_fpg);
897 acg.cg_cs.cs_nffree++;
898 sblock.fs_cstotal.cs_nffree++;
901 * Point "d" to the last fragment of the last
902 * (incomplete) block of the cylinder summary.
905 frag_adjust(d % sblock.fs_fpg, 1);
907 if (isblock(&sblock, cg_blksfree(&acg),
908 (d % sblock.fs_fpg) / sblock.fs_frag)) {
909 DBG_PRINT1("d=%jd\n", (intmax_t)d);
910 acg.cg_cs.cs_nffree -= sblock.fs_frag;
911 acg.cg_cs.cs_nbfree++;
912 sblock.fs_cstotal.cs_nffree -= sblock.fs_frag;
913 sblock.fs_cstotal.cs_nbfree++;
914 if (sblock.fs_contigsumsize > 0) {
915 setbit(cg_clustersfree(&acg),
916 (d % sblock.fs_fpg) / sblock.fs_frag);
917 if (lcs < sblock.fs_contigsumsize) {
919 cg_clustersum(&acg)[lcs]--;
921 cg_clustersum(&acg)[lcs]++;
926 * Point "d" to the first fragment of the block before
927 * the last incomplete block.
932 DBG_PRINT1("d=%jd\n", (intmax_t)d);
933 for (d = rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
934 d -= sblock.fs_frag) {
936 DBG_PRINT1("d=%jd\n", (intmax_t)d);
937 setblock(&sblock, cg_blksfree(&acg),
938 (d % sblock.fs_fpg) / sblock.fs_frag);
939 acg.cg_cs.cs_nbfree++;
940 sblock.fs_cstotal.cs_nbfree++;
941 if (sblock.fs_contigsumsize > 0) {
942 setbit(cg_clustersfree(&acg),
943 (d % sblock.fs_fpg) / sblock.fs_frag);
945 * The last cluster size is already set up.
947 if (lcs < sblock.fs_contigsumsize) {
949 cg_clustersum(&acg)[lcs]--;
951 cg_clustersum(&acg)[lcs]++;
958 * Now write the former cylinder group containing the cylinder
959 * summary back to disk.
962 wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
963 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
964 DBG_PRINT0("oscg written\n");
965 DBG_DUMP_CG(&sblock, "old summary cg", &acg);
968 * Find the beginning of the new cylinder group containing the
971 sblock.fs_csaddr = cgdmin(&sblock, osblock.fs_ncg);
972 ncscg = dtog(&sblock, sblock.fs_csaddr);
976 * If Nflag is specified, we would now read random data instead
977 * of an empty cg structure from disk. So we can't simulate that
981 DBG_PRINT0("nscg update skipped\n");
987 * Read the future cylinder group containing the cylinder
988 * summary from disk, and make a copy.
990 rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
991 (size_t)sblock.fs_cgsize, (void *)&aocg, fsi);
992 DBG_PRINT0("nscg read\n");
993 DBG_DUMP_CG(&sblock, "new summary cg", &aocg);
995 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
998 * Allocate all complete blocks used by the new cylinder
1001 for (d = sblock.fs_csaddr; d + sblock.fs_frag <=
1002 sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize);
1003 d += sblock.fs_frag) {
1004 clrblock(&sblock, cg_blksfree(&acg),
1005 (d % sblock.fs_fpg) / sblock.fs_frag);
1006 acg.cg_cs.cs_nbfree--;
1007 sblock.fs_cstotal.cs_nbfree--;
1008 if (sblock.fs_contigsumsize > 0) {
1009 clrbit(cg_clustersfree(&acg),
1010 (d % sblock.fs_fpg) / sblock.fs_frag);
1015 * Allocate all fragments used by the cylinder summary in the
1018 if (d < sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize)) {
1019 for (; d - sblock.fs_csaddr <
1020 sblock.fs_cssize/sblock.fs_fsize; d++) {
1021 clrbit(cg_blksfree(&acg), d % sblock.fs_fpg);
1022 acg.cg_cs.cs_nffree--;
1023 sblock.fs_cstotal.cs_nffree--;
1025 acg.cg_cs.cs_nbfree--;
1026 acg.cg_cs.cs_nffree += sblock.fs_frag;
1027 sblock.fs_cstotal.cs_nbfree--;
1028 sblock.fs_cstotal.cs_nffree += sblock.fs_frag;
1029 if (sblock.fs_contigsumsize > 0)
1030 clrbit(cg_clustersfree(&acg),
1031 (d % sblock.fs_fpg) / sblock.fs_frag);
1033 frag_adjust(d % sblock.fs_fpg, 1);
1036 * XXX Handle the cluster statistics here in the case this
1037 * cylinder group is now almost full, and the remaining
1038 * space is less then the maximum cluster size. This is
1039 * probably not needed, as you would hardly find a file
1040 * system which has only MAXCSBUFS+FS_MAXCONTIG of free
1041 * space right behind the cylinder group information in
1042 * any new cylinder group.
1046 * Update our statistics in the cylinder summary.
1051 * Write the new cylinder group containing the cylinder summary
1055 wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1056 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1057 DBG_PRINT0("nscg written\n");
1058 DBG_DUMP_CG(&sblock, "new summary cg", &acg);
1065 * Here we read some block(s) from disk.
1068 rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi)
1076 err(32, "rdfs: attempting to read negative block number");
1077 if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0)
1078 err(33, "rdfs: seek error: %jd", (intmax_t)bno);
1079 n = read(fsi, bf, size);
1080 if (n != (ssize_t)size)
1081 err(34, "rdfs: read error: %jd", (intmax_t)bno);
1088 * Here we write some block(s) to disk.
1091 wtfs(ufs2_daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1102 if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0)
1103 err(35, "wtfs: seek error: %ld", (long)bno);
1104 n = write(fso, bf, size);
1105 if (n != (ssize_t)size)
1106 err(36, "wtfs: write error: %ld", (long)bno);
1113 * Here we check if all frags of a block are free. For more details again
1114 * please see the source of newfs(8), as this function is taken over almost
1118 isblock(struct fs *fs, unsigned char *cp, int h)
1125 switch (fs->fs_frag) {
1128 return (cp[h] == 0xff);
1130 mask = 0x0f << ((h & 0x1) << 2);
1132 return ((cp[h >> 1] & mask) == mask);
1134 mask = 0x03 << ((h & 0x3) << 1);
1136 return ((cp[h >> 2] & mask) == mask);
1138 mask = 0x01 << (h & 0x7);
1140 return ((cp[h >> 3] & mask) == mask);
1142 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1149 * Here we allocate a complete block in the block map. For more details again
1150 * please see the source of newfs(8), as this function is taken over almost
1154 clrblock(struct fs *fs, unsigned char *cp, int h)
1156 DBG_FUNC("clrblock")
1160 switch ((fs)->fs_frag) {
1165 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1168 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1171 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1174 warnx("clrblock bad fs_frag %d", fs->fs_frag);
1183 * Here we free a complete block in the free block map. For more details again
1184 * please see the source of newfs(8), as this function is taken over almost
1188 setblock(struct fs *fs, unsigned char *cp, int h)
1190 DBG_FUNC("setblock")
1194 switch (fs->fs_frag) {
1199 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1202 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1205 cp[h >> 3] |= (0x01 << (h & 0x7));
1208 warnx("setblock bad fs_frag %d", fs->fs_frag);
1217 * Figure out how many lines our current terminal has. For more details again
1218 * please see the source of newfs(8), as this function is taken over almost
1224 DBG_FUNC("charsperline")
1232 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1233 columns = ws.ws_col;
1234 if (columns == 0 && (cp = getenv("COLUMNS")))
1237 columns = 80; /* last resort */
1244 is_dev(const char *name)
1246 struct stat devstat;
1248 if (stat(name, &devstat) != 0)
1250 if (!S_ISCHR(devstat.st_mode))
1256 getdev(const char *name, struct statfs *statfsp)
1258 static char device[MAXPATHLEN];
1264 cp = strrchr(name, '/');
1266 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, name);
1271 if (statfsp != NULL)
1272 return (statfsp->f_mntfromname);
1278 * growfs(8) is a utility which allows to increase the size of an existing
1279 * ufs file system. Currently this can only be done on unmounted file system.
1280 * It recognizes some command line options to specify the new desired size,
1281 * and it does some basic checkings. The old file system size is determined
1282 * and after some more checks like we can really access the new last block
1283 * on the disk etc. we calculate the new parameters for the superblock. After
1284 * having done this we just call growfs() which will do the work.
1285 * We still have to provide support for snapshots. Therefore we first have to
1286 * understand what data structures are always replicated in the snapshot on
1287 * creation, for all other blocks we touch during our procedure, we have to
1288 * keep the old blocks unchanged somewhere available for the snapshots. If we
1289 * are lucky, then we only have to handle our blocks to be relocated in that
1291 * Also we have to consider in what order we actually update the critical
1292 * data structures of the file system to make sure, that in case of a disaster
1293 * fsck(8) is still able to restore any lost data.
1294 * The foreseen last step then will be to provide for growing even mounted
1295 * file systems. There we have to extend the mount() system call to provide
1296 * userland access to the file system locking facility.
1299 main(int argc, char **argv)
1304 struct statfs *statfsp;
1307 int error, j, fsi, fso, ch, ret, Nflag = 0, yflag = 0;
1308 char *p, reply[5], oldsizebuf[6], newsizebuf[6];
1313 while ((ch = getopt(argc, argv, "Ns:vy")) != -1) {
1319 size = (off_t)strtoumax(optarg, &p, 0);
1320 if (p == NULL || *p == '\0')
1322 else if (*p == 'b' || *p == 'B')
1324 else if (*p == 'k' || *p == 'K')
1326 else if (*p == 'm' || *p == 'M')
1328 else if (*p == 'g' || *p == 'G')
1330 else if (*p == 't' || *p == 'T') {
1334 errx(2, "unknown suffix on -s argument");
1336 case 'v': /* for compatibility to newfs */
1354 * Now try to guess the device name.
1356 statfsp = getmntpoint(*argv);
1357 device = getdev(*argv, statfsp);
1359 errx(2, "cannot find special device for %s", *argv);
1361 fsi = open(device, O_RDONLY);
1363 err(3, "%s", device);
1366 * Try to guess the slice size if not specified.
1368 if (ioctl(fsi, DIOCGMEDIASIZE, &mediasize) == -1)
1369 err(3,"DIOCGMEDIASIZE");
1372 * Check if that partition is suitable for growing a file system.
1375 errx(2, "partition is unavailable");
1378 * Read the current superblock, and take a backup.
1380 if ((ret = sbget(fsi, &fs, UFS_STDSB, 0)) != 0) {
1383 errx(2, "superblock not recognized");
1385 errc(3, ret, "unable to read superblock");
1389 * Check for filesystem that was unclean at mount time.
1391 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0)
1392 errx(2, "%s is not clean - run fsck.\n", *argv);
1393 memcpy(&osblock, fs, fs->fs_sbsize);
1395 memcpy((void *)&fsun1, (void *)&fsun2, osblock.fs_sbsize);
1397 DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
1398 DBG_DUMP_FS(&sblock, "old sblock");
1401 * Determine size to grow to. Default to the device size.
1406 if (size > (uint64_t)mediasize) {
1407 humanize_number(oldsizebuf, sizeof(oldsizebuf), size,
1408 "B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1409 humanize_number(newsizebuf, sizeof(newsizebuf),
1411 "B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1413 errx(2, "requested size %s is larger "
1414 "than the available %s", oldsizebuf, newsizebuf);
1419 * Make sure the new size is a multiple of fs_fsize; /dev/ufssuspend
1420 * only supports fragment-aligned IO requests.
1422 size -= size % osblock.fs_fsize;
1424 if (size <= (uint64_t)(osblock.fs_size * osblock.fs_fsize)) {
1425 humanize_number(oldsizebuf, sizeof(oldsizebuf),
1426 osblock.fs_size * osblock.fs_fsize,
1427 "B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1428 humanize_number(newsizebuf, sizeof(newsizebuf), size,
1429 "B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1431 if (size == (uint64_t)(osblock.fs_size * osblock.fs_fsize))
1432 errx(0, "requested size %s is equal to the current "
1433 "filesystem size %s", newsizebuf, oldsizebuf);
1434 errx(2, "requested size %s is smaller than the current "
1435 "filesystem size %s", newsizebuf, oldsizebuf);
1438 sblock.fs_old_size = sblock.fs_size =
1439 dbtofsb(&osblock, size / DEV_BSIZE);
1440 sblock.fs_providersize = dbtofsb(&osblock, mediasize / DEV_BSIZE);
1443 * Are we really growing?
1445 if (osblock.fs_size >= sblock.fs_size) {
1446 errx(3, "we are not growing (%jd->%jd)",
1447 (intmax_t)osblock.fs_size, (intmax_t)sblock.fs_size);
1451 * Check if we find an active snapshot.
1454 for (j = 0; j < FSMAXSNAP; j++) {
1455 if (sblock.fs_snapinum[j]) {
1456 errx(2, "active snapshot found in file system; "
1457 "please remove all snapshots before "
1460 if (!sblock.fs_snapinum[j]) /* list is dense */
1465 if (yflag == 0 && Nflag == 0) {
1466 if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) == 0)
1467 printf("Device is mounted read-write; resizing will "
1468 "result in temporary write suspension for %s.\n",
1469 statfsp->f_mntonname);
1470 printf("It's strongly recommended to make a backup "
1471 "before growing the file system.\n"
1472 "OK to grow filesystem on %s", device);
1473 if (statfsp != NULL)
1474 printf(", mounted on %s,", statfsp->f_mntonname);
1475 humanize_number(oldsizebuf, sizeof(oldsizebuf),
1476 osblock.fs_size * osblock.fs_fsize,
1477 "B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1478 humanize_number(newsizebuf, sizeof(newsizebuf),
1479 sblock.fs_size * sblock.fs_fsize,
1480 "B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1481 printf(" from %s to %s? [yes/no] ", oldsizebuf, newsizebuf);
1483 fgets(reply, (int)sizeof(reply), stdin);
1484 if (strcasecmp(reply, "yes\n")){
1485 printf("Response other than \"yes\"; aborting\n");
1491 * Try to access our device for writing. If it's not mounted,
1492 * or mounted read-only, simply open it; otherwise, use UFS
1493 * suspension mechanism.
1498 if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) == 0) {
1499 fso = open(_PATH_UFSSUSPEND, O_RDWR);
1501 err(3, "unable to open %s", _PATH_UFSSUSPEND);
1502 error = ioctl(fso, UFSSUSPEND, &statfsp->f_fsid);
1504 err(3, "UFSSUSPEND");
1506 fso = open(device, O_WRONLY);
1508 err(3, "%s", device);
1513 * Try to access our new last block in the file system.
1515 testbuf = malloc(sblock.fs_fsize);
1516 if (testbuf == NULL)
1518 rdfs((ufs2_daddr_t)((size - sblock.fs_fsize) / DEV_BSIZE),
1519 sblock.fs_fsize, testbuf, fsi);
1520 wtfs((ufs2_daddr_t)((size - sblock.fs_fsize) / DEV_BSIZE),
1521 sblock.fs_fsize, testbuf, fso, Nflag);
1525 * Now calculate new superblock values and check for reasonable
1526 * bound for new file system size:
1527 * fs_size: is derived from user input
1528 * fs_dsize: should get updated in the routines creating or
1529 * updating the cylinder groups on the fly
1530 * fs_cstotal: should get updated in the routines creating or
1531 * updating the cylinder groups
1535 * Update the number of cylinders and cylinder groups in the file system.
1537 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1538 sblock.fs_old_ncyl =
1539 sblock.fs_size * sblock.fs_old_nspf / sblock.fs_old_spc;
1540 if (sblock.fs_size * sblock.fs_old_nspf >
1541 sblock.fs_old_ncyl * sblock.fs_old_spc)
1542 sblock.fs_old_ncyl++;
1544 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
1547 * Allocate last cylinder group only if there is enough room
1548 * for at least one data block.
1550 if (sblock.fs_size % sblock.fs_fpg != 0 &&
1551 sblock.fs_size <= cgdmin(&sblock, sblock.fs_ncg - 1)) {
1552 humanize_number(oldsizebuf, sizeof(oldsizebuf),
1553 (sblock.fs_size % sblock.fs_fpg) * sblock.fs_fsize,
1554 "B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1555 warnx("no room to allocate last cylinder group; "
1556 "leaving %s unused", oldsizebuf);
1558 if (sblock.fs_magic == FS_UFS1_MAGIC)
1559 sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg;
1560 sblock.fs_old_size = sblock.fs_size =
1561 sblock.fs_ncg * sblock.fs_fpg;
1565 * Update the space for the cylinder group summary information in the
1566 * respective cylinder group data area.
1569 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
1571 if (osblock.fs_size >= sblock.fs_size)
1572 errx(3, "not enough new space");
1574 DBG_PRINT0("sblock calculated\n");
1577 * Ok, everything prepared, so now let's do the tricks.
1579 growfs(fsi, fso, Nflag);
1583 if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) == 0) {
1584 error = ioctl(fso, UFSRESUME);
1586 err(3, "UFSRESUME");
1591 if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) != 0 &&
1592 chkdoreload(statfsp, warn) != 0)
1603 * Dump a line of usage.
1612 fprintf(stderr, "usage: growfs [-Ny] [-s size] special | filesystem\n");
1619 * This updates most parameters and the bitmap related to cluster. We have to
1620 * assume that sblock, osblock, acg are set up.
1630 if (sblock.fs_contigsumsize < 1) /* no clustering */
1633 * update cluster allocation map
1635 setbit(cg_clustersfree(&acg), block);
1638 * update cluster summary table
1642 * calculate size for the trailing cluster
1644 for (block--; lcs < sblock.fs_contigsumsize; block--, lcs++ ) {
1645 if (isclr(cg_clustersfree(&acg), block))
1649 if (lcs < sblock.fs_contigsumsize) {
1651 cg_clustersum(&acg)[lcs]--;
1653 cg_clustersum(&acg)[lcs]++;
1661 * Calculate the check-hash of the cylinder group.
1664 cgckhash(struct cg *cgp)
1667 if ((sblock.fs_metackhash & CK_CYLGRP) == 0)
1670 cgp->cg_ckhash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);