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";
50 static const char rcsid[] =
51 "$FreeBSD: src/sbin/growfs/growfs.c,v 1.4.2.2 2001/08/14 12:45:11 chm Exp $";
54 /* ********************************************************** INCLUDES ***** */
55 #include <sys/param.h>
56 #include <sys/disklabel.h>
57 #include <sys/ioctl.h>
68 #include <ufs/ufs/dinode.h>
69 #include <ufs/ffs/fs.h>
73 /* *************************************************** GLOBALS & TYPES ***** */
75 int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
82 #define sblock fsun1.fs /* the new superblock */
83 #define osblock fsun2.fs /* the old superblock */
89 #define acg cgun1.cg /* a cylinder cgroup (new) */
90 #define aocg cgun2.cg /* an old cylinder group */
92 static char ablk[MAXBSIZE]; /* a block */
93 static char i1blk[MAXBSIZE]; /* some indirect blocks */
94 static char i2blk[MAXBSIZE];
95 static char i3blk[MAXBSIZE];
97 /* where to write back updated blocks */
98 static daddr_t in_src, i1_src, i2_src, i3_src;
100 /* what object contains the reference */
101 enum pointer_source {
108 static struct csum *fscs; /* cylinder summary */
110 static struct dinode zino[MAXBSIZE/sizeof(struct dinode)]; /* some inodes */
113 * An array of elements of type struct gfs_bpp describes all blocks to
114 * be relocated in order to free the space needed for the cylinder group
115 * summary for all cylinder groups located in the first cylinder group.
118 daddr_t old; /* old block number */
119 daddr_t new; /* new block number */
120 #define GFS_FL_FIRST 1
121 #define GFS_FL_LAST 2
122 unsigned int flags; /* special handling required */
123 int found; /* how many references were updated */
126 /* ******************************************************** PROTOTYPES ***** */
127 static void growfs(int, int, unsigned int);
128 static void rdfs(daddr_t, size_t, void *, int);
129 static void wtfs(daddr_t, size_t, void *, int, unsigned int);
130 static daddr_t alloc(void);
131 static int charsperline(void);
132 static void usage(void);
133 static int isblock(struct fs *, unsigned char *, int);
134 static void clrblock(struct fs *, unsigned char *, int);
135 static void setblock(struct fs *, unsigned char *, int);
136 static void initcg(int, time_t, int, unsigned int);
137 static void updjcg(int, time_t, int, int, unsigned int);
138 static void updcsloc(time_t, int, int, unsigned int);
139 static struct disklabel *get_disklabel(int);
140 static void return_disklabel(int, struct disklabel *, unsigned int);
141 static struct dinode *ginode(ino_t, int, int);
142 static void frag_adjust(daddr_t, int);
143 static void cond_bl_upd(ufs_daddr_t *, struct gfs_bpp *,
144 enum pointer_source, int, unsigned int);
145 static void updclst(int);
146 static void updrefs(int, ino_t, struct gfs_bpp *, int, int, unsigned int);
148 /* ************************************************************ growfs ***** */
150 * Here we actually start growing the filesystem. We basically read the
151 * cylinder summary from the first cylinder group as we want to update
152 * this on the fly during our various operations. First we handle the
153 * changes in the former last cylinder group. Afterwards we create all new
154 * cylinder groups. Now we handle the cylinder group containing the
155 * cylinder summary which might result in a relocation of the whole
156 * structure. In the end we write back the updated cylinder summary, the
157 * new superblock, and slightly patched versions of the super block
161 growfs(int fsi, int fso, unsigned int Nflag)
170 static int randinit=0;
178 #else /* not FSIRAND */
186 * Get the cylinder summary into the memory.
188 fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize);
190 errx(1, "calloc failed");
192 for (i = 0; i < osblock.fs_cssize; i += osblock.fs_bsize) {
193 rdfs(fsbtodb(&osblock, osblock.fs_csaddr +
194 numfrags(&osblock, i)), (size_t)MIN(osblock.fs_cssize - i,
195 osblock.fs_bsize), (void *)(((char *)fscs)+i), fsi);
200 struct csum *dbg_csp;
205 for(dbg_csc=0; dbg_csc<osblock.fs_ncg; dbg_csc++) {
206 snprintf(dbg_line, sizeof(dbg_line),
207 "%d. old csum in old location", dbg_csc);
208 DBG_DUMP_CSUM(&osblock,
213 #endif /* FS_DEBUG */
214 DBG_PRINT0("fscs read\n");
217 * Do all needed changes in the former last cylinder group.
219 updjcg(osblock.fs_ncg-1, utime, fsi, fso, Nflag);
222 * Dump out summary information about file system.
224 printf("growfs:\t%d sectors in %d %s of %d tracks, %d sectors\n",
225 sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
226 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
227 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
228 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
229 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
230 sblock.fs_ncg, sblock.fs_cpg,
231 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
236 * Now build the cylinders group blocks and
237 * then print out indices of cylinder groups.
239 printf("super-block backups (for fsck -b #) at:\n");
241 width = charsperline();
244 * Iterate for only the new cylinder groups.
246 for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
247 initcg(cylno, utime, fso, Nflag);
248 j = sprintf(tmpbuf, " %d%s",
249 (int)fsbtodb(&sblock, cgsblock(&sblock, cylno)),
250 cylno < (sblock.fs_ncg-1) ? "," : "" );
251 if (i + j >= width) {
256 printf("%s", tmpbuf);
262 * Do all needed changes in the first cylinder group.
263 * allocate blocks in new location
265 updcsloc(utime, fsi, fso, Nflag);
268 * Now write the cylinder summary back to disk.
270 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
271 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
272 (size_t)MIN(sblock.fs_cssize - i, sblock.fs_bsize),
273 (void *)(((char *)fscs) + i), fso, Nflag);
275 DBG_PRINT0("fscs written\n");
279 struct csum *dbg_csp;
284 for(dbg_csc=0; dbg_csc<sblock.fs_ncg; dbg_csc++) {
285 snprintf(dbg_line, sizeof(dbg_line),
286 "%d. new csum in new location", dbg_csc);
287 DBG_DUMP_CSUM(&sblock,
292 #endif /* FS_DEBUG */
295 * Now write the new superblock back to disk.
297 sblock.fs_time = utime;
298 wtfs((daddr_t)(SBOFF / DEV_BSIZE), (size_t)SBSIZE, (void *)&sblock,
300 DBG_PRINT0("sblock written\n");
302 "new initial sblock");
305 * Clean up the dynamic fields in our superblock copies.
310 sblock.fs_cgrotor = 0;
312 memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
313 sblock.fs_flags &= FS_DOSOFTDEP;
317 * The following fields are currently distributed from the superblock
325 * fs_flags regarding SOFTPDATES
327 * We probably should rather change the summary for the cylinder group
328 * statistics here to the value of what would be in there, if the file
329 * system were created initially with the new size. Therefor we still
330 * need to find an easy way of calculating that.
331 * Possibly we can try to read the first superblock copy and apply the
332 * "diffed" stats between the old and new superblock by still copying
333 * certain parameters onto that.
337 * Write out the duplicate super blocks.
339 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
340 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
341 (size_t)SBSIZE, (void *)&sblock, fso, Nflag);
343 DBG_PRINT0("sblock copies written\n");
345 "new other sblocks");
351 /* ************************************************************ initcg ***** */
353 * This creates a new cylinder group structure, for more details please see
354 * the source of newfs(8), as this function is taken over almost unchanged.
355 * As this is never called for the first cylinder group, the special
356 * provisions for that case are removed here.
359 initcg(int cylno, time_t utime, int fso, unsigned int Nflag)
362 daddr_t cbase, d, dlower, dupper, dmax, blkno;
364 register struct csum *cs;
372 * Determine block bounds for cylinder group.
374 cbase = cgbase(&sblock, cylno);
375 dmax = cbase + sblock.fs_fpg;
376 if (dmax > sblock.fs_size) {
377 dmax = sblock.fs_size;
379 dlower = cgsblock(&sblock, cylno) - cbase;
380 dupper = cgdmin(&sblock, cylno) - cbase;
381 if (cylno == 0) { /* XXX fscs may be relocated */
382 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
385 memset(&acg, 0, (size_t)sblock.fs_cgsize);
387 acg.cg_magic = CG_MAGIC;
389 if (cylno == sblock.fs_ncg - 1) {
390 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
392 acg.cg_ncyl = sblock.fs_cpg;
394 acg.cg_niblk = sblock.fs_ipg;
395 acg.cg_ndblk = dmax - cbase;
396 if (sblock.fs_contigsumsize > 0) {
397 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
399 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
400 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
401 acg.cg_iusedoff = acg.cg_boff +
402 sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t);
403 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
404 if (sblock.fs_contigsumsize <= 0) {
405 acg.cg_nextfreeoff = acg.cg_freeoff +
406 howmany(sblock.fs_cpg* sblock.fs_spc/ NSPF(&sblock), NBBY);
408 acg.cg_clustersumoff = acg.cg_freeoff + howmany
409 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
411 acg.cg_clustersumoff =
412 roundup(acg.cg_clustersumoff, sizeof(u_int32_t));
413 acg.cg_clusteroff = acg.cg_clustersumoff +
414 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
415 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
416 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
418 if (acg.cg_nextfreeoff-(int)(&acg.cg_firstfield) > sblock.fs_cgsize) {
420 * XXX This should never happen as we would have had that panic
421 * already on filesystem creation
423 errx(37, "panic: cylinder group too big");
425 acg.cg_cs.cs_nifree += sblock.fs_ipg;
427 for (i = 0; (size_t)i < ROOTINO; i++) {
428 setbit(cg_inosused(&acg), i);
429 acg.cg_cs.cs_nifree--;
431 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
433 for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++) {
434 zino[j].di_gen = random();
437 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
438 (size_t)sblock.fs_bsize, (void *)zino, fso, Nflag);
440 for (d = 0; d < dlower; d += sblock.fs_frag) {
441 blkno = d / sblock.fs_frag;
442 setblock(&sblock, cg_blksfree(&acg), blkno);
443 if (sblock.fs_contigsumsize > 0) {
444 setbit(cg_clustersfree(&acg), blkno);
446 acg.cg_cs.cs_nbfree++;
447 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
448 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
449 [cbtorpos(&sblock, d)]++;
451 sblock.fs_dsize += dlower;
452 sblock.fs_dsize += acg.cg_ndblk - dupper;
453 if ((i = dupper % sblock.fs_frag)) {
454 acg.cg_frsum[sblock.fs_frag - i]++;
455 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
456 setbit(cg_blksfree(&acg), dupper);
457 acg.cg_cs.cs_nffree++;
460 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
461 blkno = d / sblock.fs_frag;
462 setblock(&sblock, cg_blksfree(&acg), blkno);
463 if (sblock.fs_contigsumsize > 0) {
464 setbit(cg_clustersfree(&acg), blkno);
466 acg.cg_cs.cs_nbfree++;
467 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
468 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
469 [cbtorpos(&sblock, d)]++;
472 if (d < dmax - cbase) {
473 acg.cg_frsum[dmax - cbase - d]++;
474 for (; d < dmax - cbase; d++) {
475 setbit(cg_blksfree(&acg), d);
476 acg.cg_cs.cs_nffree++;
479 if (sblock.fs_contigsumsize > 0) {
480 int32_t *sump = cg_clustersum(&acg);
481 u_char *mapp = cg_clustersfree(&acg);
486 for (i = 0; i < acg.cg_nclusterblks; i++) {
487 if ((map & bit) != 0) {
489 } else if (run != 0) {
490 if (run > sblock.fs_contigsumsize) {
491 run = sblock.fs_contigsumsize;
496 if ((i & (NBBY - 1)) != (NBBY - 1)) {
504 if (run > sblock.fs_contigsumsize) {
505 run = sblock.fs_contigsumsize;
510 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
511 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
512 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
513 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
515 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
516 (size_t)sblock.fs_bsize, (void *)&acg, fso, Nflag);
525 /* ******************************************************* frag_adjust ***** */
527 * Here we add or subtract (sign +1/-1) the available fragments in a given
528 * block to or from the fragment statistics. By subtracting before and adding
529 * after an operation on the free frag map we can easy update the fragment
530 * statistic, which seems to be otherwise an rather complex operation.
533 frag_adjust(daddr_t frag, int sign)
535 DBG_FUNC("frag_adjust")
543 * Here frag only needs to point to any fragment in the block we want
546 for(f=rounddown(frag, sblock.fs_frag);
547 f<roundup(frag+1, sblock.fs_frag);
550 * Count contiguos free fragments.
552 if(isset(cg_blksfree(&acg), f)) {
555 if(fragsize && fragsize<sblock.fs_frag) {
557 * We found something in between.
559 acg.cg_frsum[fragsize]+=sign;
560 DBG_PRINT2("frag_adjust [%d]+=%d\n",
567 if(fragsize && fragsize<sblock.fs_frag) {
569 * We found something.
571 acg.cg_frsum[fragsize]+=sign;
572 DBG_PRINT2("frag_adjust [%d]+=%d\n",
576 DBG_PRINT2("frag_adjust [[%d]]+=%d\n",
584 /* ******************************************************* cond_bl_upd ***** */
586 * Here we conditionally update a pointer to a fragment. We check for all
587 * relocated blocks if any of it's fragments is referenced by the current
588 * field, and update the pointer to the respective fragment in our new
589 * block. If we find a reference we write back the block immediately,
590 * as there is no easy way for our general block reading engine to figure
591 * out if a write back operation is needed.
594 cond_bl_upd(ufs_daddr_t *block, struct gfs_bpp *field,
595 enum pointer_source source, int fso, unsigned int Nflag)
597 DBG_FUNC("cond_bl_upd")
605 while(f->old) { /* for all old blocks */
606 if(*block/sblock.fs_frag == f->old) {
608 * The fragment is part of the block, so update.
610 *block=(f->new*sblock.fs_frag+(*block%sblock.fs_frag));
612 DBG_PRINT3("scg (%d->%d)[%d] reference updated\n",
615 *block%sblock.fs_frag);
617 /* Write the block back to disk immediately */
623 case GFS_PS_IND_BLK_LVL1:
627 case GFS_PS_IND_BLK_LVL2:
631 case GFS_PS_IND_BLK_LVL3:
641 * XXX If src is not of type inode we have to
642 * implement copy on write here in case
643 * of active snapshots.
645 wtfs(dst, (size_t)sblock.fs_bsize, (void *)src,
650 * The same block can't be found again in this loop.
661 /* ************************************************************ updjcg ***** */
663 * Here we do all needed work for the former last cylinder group. It has to be
664 * changed in any case, even if the filesystem ended exactly on the end of
665 * this group, as there is some slightly inconsistent handling of the number
666 * of cylinders in the cylinder group. We start again by reading the cylinder
667 * group from disk. If the last block was not fully available, we first handle
668 * the missing fragments, then we handle all new full blocks in that file
669 * system and finally we handle the new last fragmented block in the file
670 * system. We again have to handle the fragment statistics rotational layout
671 * tables and cluster summary during all those operations.
674 updjcg(int cylno, time_t utime, int fsi, int fso, unsigned int Nflag)
677 daddr_t cbase, dmax, dupper;
685 * Read the former last (joining) cylinder group from disk, and make
688 rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
689 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
690 DBG_PRINT0("jcg read\n");
695 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
698 * If the cylinder group had already it's new final size almost
699 * nothing is to be done ... except:
700 * For some reason the value of cg_ncyl in the last cylinder group has
701 * to be zero instead of fs_cpg. As this is now no longer the last
702 * cylinder group we have to change that value now to fs_cpg.
705 if(cgbase(&osblock, cylno+1) == osblock.fs_size) {
706 acg.cg_ncyl=sblock.fs_cpg;
708 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
709 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
710 DBG_PRINT0("jcg written\n");
720 * Set up some variables needed later.
722 cbase = cgbase(&sblock, cylno);
723 dmax = cbase + sblock.fs_fpg;
724 if (dmax > sblock.fs_size)
725 dmax = sblock.fs_size;
726 dupper = cgdmin(&sblock, cylno) - cbase;
727 if (cylno == 0) { /* XXX fscs may be relocated */
728 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
732 * Set pointer to the cylinder summary for our cylinder group.
737 * Touch the cylinder group, update all fields in the cylinder group as
738 * needed, update the free space in the superblock.
741 if (cylno == sblock.fs_ncg - 1) {
743 * This is still the last cylinder group.
745 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
747 acg.cg_ncyl = sblock.fs_cpg;
749 DBG_PRINT4("jcg dbg: %d %u %d %u\n",
754 acg.cg_ndblk = dmax - cbase;
755 sblock.fs_dsize += acg.cg_ndblk-aocg.cg_ndblk;
756 if (sblock.fs_contigsumsize > 0) {
757 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
761 * Now we have to update the free fragment bitmap for our new free
762 * space. There again we have to handle the fragmentation and also
763 * the rotational layout tables and the cluster summary. This is
764 * also done per fragment for the first new block if the old file
765 * system end was not on a block boundary, per fragment for the new
766 * last block if the new file system end is not on a block boundary,
767 * and per block for all space in between.
769 * Handle the first new block here if it was partially available
772 if(osblock.fs_size % sblock.fs_frag) {
773 if(roundup(osblock.fs_size, sblock.fs_frag)<=sblock.fs_size) {
775 * The new space is enough to fill at least this
779 for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag)-1;
780 i>=osblock.fs_size-cbase;
782 setbit(cg_blksfree(&acg), i);
783 acg.cg_cs.cs_nffree++;
788 * Check if the fragment just created could join an
789 * already existing fragment at the former end of the
792 if(isblock(&sblock, cg_blksfree(&acg),
793 ((osblock.fs_size - cgbase(&sblock, cylno))/
796 * The block is now completely available
798 DBG_PRINT0("block was\n");
799 acg.cg_frsum[osblock.fs_size%sblock.fs_frag]--;
800 acg.cg_cs.cs_nbfree++;
801 acg.cg_cs.cs_nffree-=sblock.fs_frag;
802 k=rounddown(osblock.fs_size-cbase,
804 cg_blktot(&acg)[cbtocylno(&sblock, k)]++;
805 cg_blks(&sblock, &acg, cbtocylno(&sblock, k))
806 [cbtorpos(&sblock, k)]++;
807 updclst((osblock.fs_size-cbase)/sblock.fs_frag);
810 * Lets rejoin a possible partially growed
814 while(isset(cg_blksfree(&acg), i) &&
815 (i>=rounddown(osblock.fs_size-cbase,
827 * We only grow by some fragments within this last
830 for(i=sblock.fs_size-cbase-1;
831 i>=osblock.fs_size-cbase;
833 setbit(cg_blksfree(&acg), i);
834 acg.cg_cs.cs_nffree++;
838 * Lets rejoin a possible partially growed fragment.
841 while(isset(cg_blksfree(&acg), i) &&
842 (i>=rounddown(osblock.fs_size-cbase,
855 * Handle all new complete blocks here.
857 for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag);
858 i+sblock.fs_frag<=dmax-cbase; /* XXX <= or only < ? */
860 j = i / sblock.fs_frag;
861 setblock(&sblock, cg_blksfree(&acg), j);
863 acg.cg_cs.cs_nbfree++;
864 cg_blktot(&acg)[cbtocylno(&sblock, i)]++;
865 cg_blks(&sblock, &acg, cbtocylno(&sblock, i))
866 [cbtorpos(&sblock, i)]++;
870 * Handle the last new block if there are stll some new fragments left.
871 * Here we don't have to bother about the cluster summary or the even
872 * the rotational layout table.
874 if (i < (dmax - cbase)) {
875 acg.cg_frsum[dmax - cbase - i]++;
876 for (; i < dmax - cbase; i++) {
877 setbit(cg_blksfree(&acg), i);
878 acg.cg_cs.cs_nffree++;
882 sblock.fs_cstotal.cs_nffree +=
883 (acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree);
884 sblock.fs_cstotal.cs_nbfree +=
885 (acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree);
887 * The following statistics are not changed here:
888 * sblock.fs_cstotal.cs_ndir
889 * sblock.fs_cstotal.cs_nifree
890 * As the statistics for this cylinder group are ready, copy it to
891 * the summary information array.
896 * Write the updated "joining" cylinder group back to disk.
898 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
899 (void *)&acg, fso, Nflag);
900 DBG_PRINT0("jcg written\n");
909 /* ********************************************************** updcsloc ***** */
911 * Here we update the location of the cylinder summary. We have two possible
912 * ways of growing the cylinder summary.
913 * (1) We can try to grow the summary in the current location, and relocate
914 * possibly used blocks within the current cylinder group.
915 * (2) Alternatively we can relocate the whole cylinder summary to the first
916 * new completely empty cylinder group. Once the cylinder summary is no
917 * longer in the beginning of the first cylinder group you should never
918 * use a version of fsck which is not aware of the possibility to have
919 * this structure in a non standard place.
920 * Option (1) is considered to be less intrusive to the structure of the file-
921 * system. So we try to stick to that whenever possible. If there is not enough
922 * space in the cylinder group containing the cylinder summary we have to use
923 * method (2). In case of active snapshots in the filesystem we probably can
924 * completely avoid implementing copy on write if we stick to method (2) only.
927 updcsloc(time_t utime, int fsi, int fso, unsigned int Nflag)
933 daddr_t cbase, dupper, odupper, d, f, g;
943 if(howmany(sblock.fs_cssize, sblock.fs_fsize) ==
944 howmany(osblock.fs_cssize, osblock.fs_fsize)) {
946 * No new fragment needed.
951 ocscg=dtog(&osblock, osblock.fs_csaddr);
953 blocks = 1+howmany(sblock.fs_cssize, sblock.fs_bsize)-
954 howmany(osblock.fs_cssize, osblock.fs_bsize);
957 * Read original cylinder group from disk, and make a copy.
958 * XXX If Nflag is set in some very rare cases we now miss
959 * some changes done in updjcg by reading the unmodified
962 rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
963 (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
964 DBG_PRINT0("oscg read\n");
969 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
972 * Touch the cylinder group, set up local variables needed later
973 * and update the superblock.
978 * XXX In the case of having active snapshots we may need much more
979 * blocks for the copy on write. We need each block twice, and
980 * also up to 8*3 blocks for indirect blocks for all possible
983 if(/*((int)sblock.fs_time&0x3)>0||*/ cs->cs_nbfree < blocks) {
985 * There is not enough space in the old cylinder group to
986 * relocate all blocks as needed, so we relocate the whole
987 * cylinder group summary to a new group. We try to use the
988 * first complete new cylinder group just created. Within the
989 * cylinder group we allign the area immediately after the
990 * cylinder group information location in order to be as
991 * close as possible to the original implementation of ffs.
993 * First we have to make sure we'll find enough space in the
994 * new cylinder group. If not, then we currently give up.
995 * We start with freeing everything which was used by the
996 * fragments of the old cylinder summary in the current group.
997 * Now we write back the group meta data, read in the needed
998 * meta data from the new cylinder group, and start allocating
999 * within that group. Here we can assume, the group to be
1000 * completely empty. Which makes the handling of fragments and
1001 * clusters a lot easier.
1004 if(sblock.fs_ncg-osblock.fs_ncg < 2) {
1005 errx(2, "panic: not enough space");
1009 * Point "d" to the first fragment not used by the cylinder
1012 d=osblock.fs_csaddr+(osblock.fs_cssize/osblock.fs_fsize);
1015 * Set up last cluster size ("lcs") already here. Calculate
1016 * the size for the trailing cluster just behind where "d"
1019 if(sblock.fs_contigsumsize > 0) {
1020 for(block=howmany(d%sblock.fs_fpg, sblock.fs_frag),
1021 lcs=0; lcs<sblock.fs_contigsumsize;
1023 if(isclr(cg_clustersfree(&acg), block)){
1030 * Point "d" to the last frag used by the cylinder summary.
1034 DBG_PRINT1("d=%d\n",
1036 if((d+1)%sblock.fs_frag) {
1038 * The end of the cylinder summary is not a complete
1042 frag_adjust(d%sblock.fs_fpg, -1);
1043 for(; (d+1)%sblock.fs_frag; d--) {
1044 DBG_PRINT1("d=%d\n",
1046 setbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1047 acg.cg_cs.cs_nffree++;
1048 sblock.fs_cstotal.cs_nffree++;
1051 * Point "d" to the last fragment of the last
1052 * (incomplete) block of the clinder summary.
1055 frag_adjust(d%sblock.fs_fpg, 1);
1057 if(isblock(&sblock, cg_blksfree(&acg),
1058 (d%sblock.fs_fpg)/sblock.fs_frag)) {
1059 DBG_PRINT1("d=%d\n",
1061 acg.cg_cs.cs_nffree-=sblock.fs_frag;
1062 acg.cg_cs.cs_nbfree++;
1063 sblock.fs_cstotal.cs_nffree-=sblock.fs_frag;
1064 sblock.fs_cstotal.cs_nbfree++;
1065 cg_blktot(&acg)[cbtocylno(&sblock,
1066 d%sblock.fs_fpg)]++;
1067 cg_blks(&sblock, &acg, cbtocylno(&sblock,
1068 d%sblock.fs_fpg))[cbtorpos(&sblock,
1069 d%sblock.fs_fpg)]++;
1070 if(sblock.fs_contigsumsize > 0) {
1071 setbit(cg_clustersfree(&acg),
1072 (d%sblock.fs_fpg)/sblock.fs_frag);
1073 if(lcs < sblock.fs_contigsumsize) {
1079 cg_clustersum(&acg)[lcs]++;
1084 * Point "d" to the first fragment of the block before
1085 * the last incomplete block.
1090 DBG_PRINT1("d=%d\n",
1092 for(d=rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
1093 d-=sblock.fs_frag) {
1095 DBG_PRINT1("d=%d\n",
1097 setblock(&sblock, cg_blksfree(&acg),
1098 (d%sblock.fs_fpg)/sblock.fs_frag);
1099 acg.cg_cs.cs_nbfree++;
1100 sblock.fs_cstotal.cs_nbfree++;
1101 cg_blktot(&acg)[cbtocylno(&sblock, d%sblock.fs_fpg)]++;
1102 cg_blks(&sblock, &acg, cbtocylno(&sblock,
1103 d%sblock.fs_fpg))[cbtorpos(&sblock,
1104 d%sblock.fs_fpg)]++;
1105 if(sblock.fs_contigsumsize > 0) {
1106 setbit(cg_clustersfree(&acg),
1107 (d%sblock.fs_fpg)/sblock.fs_frag);
1109 * The last cluster size is already set up.
1111 if(lcs < sblock.fs_contigsumsize) {
1113 cg_clustersum(&acg)[lcs]--;
1116 cg_clustersum(&acg)[lcs]++;
1123 * Now write the former cylinder group containing the cylinder
1124 * summary back to disk.
1126 wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
1127 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1128 DBG_PRINT0("oscg written\n");
1129 DBG_DUMP_CG(&sblock,
1134 * Find the beginning of the new cylinder group containing the
1137 sblock.fs_csaddr=cgdmin(&sblock, osblock.fs_ncg);
1138 ncscg=dtog(&sblock, sblock.fs_csaddr);
1143 * If Nflag is specified, we would now read random data instead
1144 * of an empty cg structure from disk. So we can't simulate that
1148 DBG_PRINT0("nscg update skipped\n");
1154 * Read the future cylinder group containing the cylinder
1155 * summary from disk, and make a copy.
1157 rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1158 (size_t)sblock.fs_cgsize, (void *)&aocg, fsi);
1159 DBG_PRINT0("nscg read\n");
1160 DBG_DUMP_CG(&sblock,
1164 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
1167 * Allocate all complete blocks used by the new cylinder
1170 for(d=sblock.fs_csaddr; d+sblock.fs_frag <=
1171 sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize);
1172 d+=sblock.fs_frag) {
1173 clrblock(&sblock, cg_blksfree(&acg),
1174 (d%sblock.fs_fpg)/sblock.fs_frag);
1175 acg.cg_cs.cs_nbfree--;
1176 sblock.fs_cstotal.cs_nbfree--;
1177 cg_blktot(&acg)[cbtocylno(&sblock, d%sblock.fs_fpg)]--;
1178 cg_blks(&sblock, &acg, cbtocylno(&sblock,
1179 d%sblock.fs_fpg))[cbtorpos(&sblock,
1180 d%sblock.fs_fpg)]--;
1181 if(sblock.fs_contigsumsize > 0) {
1182 clrbit(cg_clustersfree(&acg),
1183 (d%sblock.fs_fpg)/sblock.fs_frag);
1188 * Allocate all fragments used by the cylinder summary in the
1191 if(d<sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize)) {
1192 for(; d-sblock.fs_csaddr<
1193 sblock.fs_cssize/sblock.fs_fsize;
1195 clrbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1196 acg.cg_cs.cs_nffree--;
1197 sblock.fs_cstotal.cs_nffree--;
1199 acg.cg_cs.cs_nbfree--;
1200 acg.cg_cs.cs_nffree+=sblock.fs_frag;
1201 sblock.fs_cstotal.cs_nbfree--;
1202 sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1203 cg_blktot(&acg)[cbtocylno(&sblock, d%sblock.fs_fpg)]--;
1204 cg_blks(&sblock, &acg, cbtocylno(&sblock,
1205 d%sblock.fs_fpg))[cbtorpos(&sblock,
1206 d%sblock.fs_fpg)]--;
1207 if(sblock.fs_contigsumsize > 0) {
1208 clrbit(cg_clustersfree(&acg),
1209 (d%sblock.fs_fpg)/sblock.fs_frag);
1212 frag_adjust(d%sblock.fs_fpg, +1);
1215 * XXX Handle the cluster statistics here in the case this
1216 * cylinder group is now almost full, and the remaining
1217 * space is less then the maximum cluster size. This is
1218 * probably not needed, as you would hardly find a file
1219 * system which has only MAXCSBUFS+FS_MAXCONTIG of free
1220 * space right behind the cylinder group information in
1221 * any new cylinder group.
1225 * Update our statistics in the cylinder summary.
1230 * Write the new cylinder group containing the cylinder summary
1233 wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1234 (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1235 DBG_PRINT0("nscg written\n");
1236 DBG_DUMP_CG(&sblock,
1244 * We have got enough of space in the current cylinder group, so we
1245 * can relocate just a few blocks, and let the summary information
1246 * grow in place where it is right now.
1250 cbase = cgbase(&osblock, ocscg); /* old and new are equal */
1251 dupper = sblock.fs_csaddr - cbase +
1252 howmany(sblock.fs_cssize, sblock.fs_fsize);
1253 odupper = osblock.fs_csaddr - cbase +
1254 howmany(osblock.fs_cssize, osblock.fs_fsize);
1256 sblock.fs_dsize -= dupper-odupper;
1259 * Allocate the space for the array of blocks to be relocated.
1261 bp=(struct gfs_bpp *)malloc(((dupper-odupper)/sblock.fs_frag+2)*
1262 sizeof(struct gfs_bpp));
1264 errx(1, "malloc failed");
1266 memset((char *)bp, 0, ((dupper-odupper)/sblock.fs_frag+2)*
1267 sizeof(struct gfs_bpp));
1270 * Lock all new frags needed for the cylinder group summary. This is
1271 * done per fragment in the first and last block of the new required
1272 * area, and per block for all other blocks.
1274 * Handle the first new block here (but only if some fragments where
1275 * already used for the cylinder summary).
1278 frag_adjust(odupper, -1);
1279 for(d=odupper; ((d<dupper)&&(d%sblock.fs_frag)); d++) {
1280 DBG_PRINT1("scg first frag check loop d=%d\n",
1282 if(isclr(cg_blksfree(&acg), d)) {
1284 bp[ind].old=d/sblock.fs_frag;
1285 bp[ind].flags|=GFS_FL_FIRST;
1286 if(roundup(d, sblock.fs_frag) >= dupper) {
1287 bp[ind].flags|=GFS_FL_LAST;
1292 clrbit(cg_blksfree(&acg), d);
1293 acg.cg_cs.cs_nffree--;
1294 sblock.fs_cstotal.cs_nffree--;
1297 * No cluster handling is needed here, as there was at least
1298 * one fragment in use by the cylinder summary in the old
1300 * No block-free counter handling here as this block was not
1304 frag_adjust(odupper, 1);
1307 * Handle all needed complete blocks here.
1309 for(; d+sblock.fs_frag<=dupper; d+=sblock.fs_frag) {
1310 DBG_PRINT1("scg block check loop d=%d\n",
1312 if(!isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) {
1313 for(f=d; f<d+sblock.fs_frag; f++) {
1314 if(isset(cg_blksfree(&aocg), f)) {
1315 acg.cg_cs.cs_nffree--;
1316 sblock.fs_cstotal.cs_nffree--;
1319 clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
1320 bp[ind].old=d/sblock.fs_frag;
1323 clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
1324 acg.cg_cs.cs_nbfree--;
1325 sblock.fs_cstotal.cs_nbfree--;
1326 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1327 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
1328 [cbtorpos(&sblock, d)]--;
1329 if(sblock.fs_contigsumsize > 0) {
1330 clrbit(cg_clustersfree(&acg), d/sblock.fs_frag);
1331 for(lcs=0, l=(d/sblock.fs_frag)+1;
1332 lcs<sblock.fs_contigsumsize;
1334 if(isclr(cg_clustersfree(&acg),l)){
1338 if(lcs < sblock.fs_contigsumsize) {
1339 cg_clustersum(&acg)[lcs+1]--;
1341 cg_clustersum(&acg)[lcs]++;
1347 * No fragment counter handling is needed here, as this finally
1348 * doesn't change after the relocation.
1353 * Handle all fragments needed in the last new affected block.
1356 frag_adjust(dupper-1, -1);
1358 if(isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) {
1359 acg.cg_cs.cs_nbfree--;
1360 sblock.fs_cstotal.cs_nbfree--;
1361 acg.cg_cs.cs_nffree+=sblock.fs_frag;
1362 sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1363 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1364 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
1365 [cbtorpos(&sblock, d)]--;
1366 if(sblock.fs_contigsumsize > 0) {
1367 clrbit(cg_clustersfree(&acg), d/sblock.fs_frag);
1368 for(lcs=0, l=(d/sblock.fs_frag)+1;
1369 lcs<sblock.fs_contigsumsize;
1371 if(isclr(cg_clustersfree(&acg),l)){
1375 if(lcs < sblock.fs_contigsumsize) {
1376 cg_clustersum(&acg)[lcs+1]--;
1378 cg_clustersum(&acg)[lcs]++;
1384 for(; d<dupper; d++) {
1385 DBG_PRINT1("scg second frag check loop d=%d\n",
1387 if(isclr(cg_blksfree(&acg), d)) {
1388 bp[ind].old=d/sblock.fs_frag;
1389 bp[ind].flags|=GFS_FL_LAST;
1391 clrbit(cg_blksfree(&acg), d);
1392 acg.cg_cs.cs_nffree--;
1393 sblock.fs_cstotal.cs_nffree--;
1396 if(bp[ind].flags & GFS_FL_LAST) { /* we have to advance here */
1399 frag_adjust(dupper-1, 1);
1403 * If we found a block to relocate just do so.
1406 for(i=0; i<ind; i++) {
1407 if(!bp[i].old) { /* no more blocks listed */
1409 * XXX A relative blocknumber should not be
1410 * zero, which is not explicitly
1411 * guaranteed by our code.
1416 * Allocate a complete block in the same (current)
1419 bp[i].new=alloc()/sblock.fs_frag;
1422 * There is no frag_adjust() needed for the new block
1423 * as it will have no fragments yet :-).
1425 for(f=bp[i].old*sblock.fs_frag,
1426 g=bp[i].new*sblock.fs_frag;
1427 f<(bp[i].old+1)*sblock.fs_frag;
1429 if(isset(cg_blksfree(&aocg), f)) {
1430 setbit(cg_blksfree(&acg), g);
1431 acg.cg_cs.cs_nffree++;
1432 sblock.fs_cstotal.cs_nffree++;
1437 * Special handling is required if this was the first
1438 * block. We have to consider the fragments which were
1439 * used by the cylinder summary in the original block
1440 * which re to be free in the copy of our block. We
1441 * have to be careful if this first block happens to
1442 * be also the last block to be relocated.
1444 if(bp[i].flags & GFS_FL_FIRST) {
1445 for(f=bp[i].old*sblock.fs_frag,
1446 g=bp[i].new*sblock.fs_frag;
1449 setbit(cg_blksfree(&acg), g);
1450 acg.cg_cs.cs_nffree++;
1451 sblock.fs_cstotal.cs_nffree++;
1453 if(!(bp[i].flags & GFS_FL_LAST)) {
1454 frag_adjust(bp[i].new*sblock.fs_frag,1);
1460 * Special handling is required if this is the last
1461 * block to be relocated.
1463 if(bp[i].flags & GFS_FL_LAST) {
1464 frag_adjust(bp[i].new*sblock.fs_frag, 1);
1465 frag_adjust(bp[i].old*sblock.fs_frag, -1);
1467 f<roundup(dupper, sblock.fs_frag);
1469 if(isclr(cg_blksfree(&acg), f)) {
1470 setbit(cg_blksfree(&acg), f);
1471 acg.cg_cs.cs_nffree++;
1472 sblock.fs_cstotal.cs_nffree++;
1475 frag_adjust(bp[i].old*sblock.fs_frag, 1);
1479 * !!! Attach the cylindergroup offset here.
1481 bp[i].old+=cbase/sblock.fs_frag;
1482 bp[i].new+=cbase/sblock.fs_frag;
1485 * Copy the content of the block.
1488 * XXX Here we will have to implement a copy on write
1489 * in the case we have any active snapshots.
1491 rdfs(fsbtodb(&sblock, bp[i].old*sblock.fs_frag),
1492 (size_t)sblock.fs_bsize, (void *)&ablk, fsi);
1493 wtfs(fsbtodb(&sblock, bp[i].new*sblock.fs_frag),
1494 (size_t)sblock.fs_bsize, (void *)&ablk, fso, Nflag);
1495 DBG_DUMP_HEX(&sblock,
1496 "copied full block",
1497 (unsigned char *)&ablk);
1499 DBG_PRINT2("scg (%d->%d) block relocated\n",
1505 * Now we have to update all references to any fragment which
1506 * belongs to any block relocated. We iterate now over all
1507 * cylinder groups, within those over all non zero length
1510 for(cylno=0; cylno<osblock.fs_ncg; cylno++) {
1511 DBG_PRINT1("scg doing cg (%d)\n",
1513 for(inc=osblock.fs_ipg-1 ; inc>=0 ; inc--) {
1514 updrefs(cylno, (ino_t)inc, bp, fsi, fso, Nflag);
1519 * All inodes are checked, now make sure the number of
1520 * references found make sense.
1522 for(i=0; i<ind; i++) {
1523 if(!bp[i].found || (bp[i].found>sblock.fs_frag)) {
1524 warnx("error: %d refs found for block %d.",
1525 bp[i].found, bp[i].old);
1531 * The following statistics are not changed here:
1532 * sblock.fs_cstotal.cs_ndir
1533 * sblock.fs_cstotal.cs_nifree
1534 * The following statistics were already updated on the fly:
1535 * sblock.fs_cstotal.cs_nffree
1536 * sblock.fs_cstotal.cs_nbfree
1537 * As the statistics for this cylinder group are ready, copy it to
1538 * the summary information array.
1544 * Write summary cylinder group back to disk.
1546 wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), (size_t)sblock.fs_cgsize,
1547 (void *)&acg, fso, Nflag);
1548 DBG_PRINT0("scg written\n");
1549 DBG_DUMP_CG(&sblock,
1557 /* ************************************************************** rdfs ***** */
1559 * Here we read some block(s) from disk.
1562 rdfs(daddr_t bno, size_t size, void *bf, int fsi)
1569 if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
1570 err(33, "rdfs: seek error: %ld", (long)bno);
1572 n = read(fsi, bf, size);
1573 if (n != (ssize_t)size) {
1574 err(34, "rdfs: read error: %ld", (long)bno);
1581 /* ************************************************************** wtfs ***** */
1583 * Here we write some block(s) to disk.
1586 wtfs(daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1597 if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) {
1598 err(35, "wtfs: seek error: %ld", (long)bno);
1600 n = write(fso, bf, size);
1601 if (n != (ssize_t)size) {
1602 err(36, "wtfs: write error: %ld", (long)bno);
1609 /* ************************************************************* alloc ***** */
1611 * Here we allocate a free block in the current cylinder group. It is assumed,
1612 * that acg contains the current cylinder group. As we may take a block from
1613 * somewhere in the filesystem we have to handle cluster summary here.
1623 int dlower, dupper, dmax;
1627 if (acg.cg_magic != CG_MAGIC) {
1628 warnx("acg: bad magic number");
1632 if (acg.cg_cs.cs_nbfree == 0) {
1633 warnx("error: cylinder group ran out of space");
1638 * We start seeking for free blocks only from the space available after
1639 * the end of the new grown cylinder summary. Otherwise we allocate a
1640 * block here which we have to relocate a couple of seconds later again
1641 * again, and we are not prepared to to this anyway.
1644 dlower=cgsblock(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx);
1645 dupper=cgdmin(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx);
1646 dmax=cgbase(&sblock, acg.cg_cgx)+sblock.fs_fpg;
1647 if (dmax > sblock.fs_size) {
1648 dmax = sblock.fs_size;
1650 dmax-=cgbase(&sblock, acg.cg_cgx); /* retransform into cg */
1651 csmin=sblock.fs_csaddr-cgbase(&sblock, acg.cg_cgx);
1652 csmax=csmin+howmany(sblock.fs_cssize, sblock.fs_fsize);
1653 DBG_PRINT3("seek range: dl=%d, du=%d, dm=%d\n",
1657 DBG_PRINT2("range cont: csmin=%d, csmax=%d\n",
1661 for(d=0; (d<dlower && blkno==-1); d+=sblock.fs_frag) {
1662 if(d>=csmin && d<=csmax) {
1665 if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1667 blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1671 for(d=dupper; (d<dmax && blkno==-1); d+=sblock.fs_frag) {
1672 if(d>=csmin && d<=csmax) {
1675 if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1677 blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1682 warnx("internal error: couldn't find promised block in cg");
1688 * This is needed if the block was found already in the first loop.
1690 d=blkstofrags(&sblock, blkno);
1692 clrblock(&sblock, cg_blksfree(&acg), blkno);
1693 if (sblock.fs_contigsumsize > 0) {
1695 * Handle the cluster allocation bitmap.
1697 clrbit(cg_clustersfree(&acg), blkno);
1699 * We possibly have split a cluster here, so we have to do
1700 * recalculate the sizes of the remaining cluster halves now,
1701 * and use them for updating the cluster summary information.
1703 * Lets start with the blocks before our allocated block ...
1705 for(lcs1=0, l=blkno-1; lcs1<sblock.fs_contigsumsize;
1707 if(isclr(cg_clustersfree(&acg),l)){
1712 * ... and continue with the blocks right after our allocated
1715 for(lcs2=0, l=blkno+1; lcs2<sblock.fs_contigsumsize;
1717 if(isclr(cg_clustersfree(&acg),l)){
1723 * Now update all counters.
1725 cg_clustersum(&acg)[MIN(lcs1+lcs2+1,sblock.fs_contigsumsize)]--;
1727 cg_clustersum(&acg)[lcs1]++;
1730 cg_clustersum(&acg)[lcs2]++;
1734 * Update all statistics based on blocks.
1736 acg.cg_cs.cs_nbfree--;
1737 sblock.fs_cstotal.cs_nbfree--;
1738 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
1739 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
1745 /* *********************************************************** isblock ***** */
1747 * Here we check if all frags of a block are free. For more details again
1748 * please see the source of newfs(8), as this function is taken over almost
1752 isblock(struct fs *fs, unsigned char *cp, int h)
1759 switch (fs->fs_frag) {
1762 return (cp[h] == 0xff);
1764 mask = 0x0f << ((h & 0x1) << 2);
1766 return ((cp[h >> 1] & mask) == mask);
1768 mask = 0x03 << ((h & 0x3) << 1);
1770 return ((cp[h >> 2] & mask) == mask);
1772 mask = 0x01 << (h & 0x7);
1774 return ((cp[h >> 3] & mask) == mask);
1776 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1782 /* ********************************************************** clrblock ***** */
1784 * Here we allocate a complete block in the block map. For more details again
1785 * please see the source of newfs(8), as this function is taken over almost
1789 clrblock(struct fs *fs, unsigned char *cp, int h)
1791 DBG_FUNC("clrblock")
1795 switch ((fs)->fs_frag) {
1800 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1803 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1806 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1809 warnx("clrblock bad fs_frag %d", fs->fs_frag);
1817 /* ********************************************************** setblock ***** */
1819 * Here we free a complete block in the free block map. For more details again
1820 * please see the source of newfs(8), as this function is taken over almost
1824 setblock(struct fs *fs, unsigned char *cp, int h)
1826 DBG_FUNC("setblock")
1830 switch (fs->fs_frag) {
1835 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1838 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1841 cp[h >> 3] |= (0x01 << (h & 0x7));
1844 warnx("setblock bad fs_frag %d", fs->fs_frag);
1852 /* ************************************************************ ginode ***** */
1854 * This function provides access to an individual inode. We find out in which
1855 * block the requested inode is located, read it from disk if needed, and
1856 * return the pointer into that block. We maintain a cache of one block to
1857 * not read the same block again and again if we iterate linearly over all
1860 static struct dinode *
1861 ginode(ino_t inumber, int fsi, int cg)
1865 static ino_t startinum=0; /* first inode in cached block */
1870 pi=(struct dinode *)(void *)ablk;
1871 inumber+=(cg * sblock.fs_ipg);
1872 if (startinum == 0 || inumber < startinum ||
1873 inumber >= startinum + INOPB(&sblock)) {
1875 * The block needed is not cached, so we have to read it from
1878 iblk = ino_to_fsba(&sblock, inumber);
1879 in_src=fsbtodb(&sblock, iblk);
1880 rdfs(in_src, (size_t)sblock.fs_bsize, (void *)&ablk, fsi);
1881 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
1885 return (&(pi[inumber % INOPB(&sblock)]));
1888 /* ****************************************************** charsperline ***** */
1890 * Figure out how many lines our current terminal has. For more details again
1891 * please see the source of newfs(8), as this function is taken over almost
1897 DBG_FUNC("charsperline")
1905 if (ioctl(0, TIOCGWINSZ, &ws) != -1) {
1906 columns = ws.ws_col;
1908 if (columns == 0 && (cp = getenv("COLUMNS"))) {
1912 columns = 80; /* last resort */
1919 /* ************************************************************** main ***** */
1921 * growfs(8) is a utility which allows to increase the size of an existing
1922 * ufs filesystem. Currently this can only be done on unmounted file system.
1923 * It recognizes some command line options to specify the new desired size,
1924 * and it does some basic checkings. The old file system size is determined
1925 * and after some more checks like we can really access the new last block
1926 * on the disk etc. we calculate the new parameters for the superblock. After
1927 * having done this we just call growfs() which will do the work. Before
1928 * we finish the only thing left is to update the disklabel.
1929 * We still have to provide support for snapshots. Therefore we first have to
1930 * understand what data structures are always replicated in the snapshot on
1931 * creation, for all other blocks we touch during our procedure, we have to
1932 * keep the old blocks unchanged somewhere available for the snapshots. If we
1933 * are lucky, then we only have to handle our blocks to be relocated in that
1935 * Also we have to consider in what order we actually update the critical
1936 * data structures of the filesystem to make sure, that in case of a disaster
1937 * fsck(8) is still able to restore any lost data.
1938 * The foreseen last step then will be to provide for growing even mounted
1939 * file systems. There we have to extend the mount() system call to provide
1940 * userland access to the file system locking facility.
1943 main(int argc, char **argv)
1946 char *device, *special, *cp;
1948 unsigned int size=0;
1950 unsigned int Nflag=0;
1953 struct disklabel *lp;
1954 struct partition *pp;
1959 #endif /* FSMAXSNAP */
1963 while((ch=getopt(argc, argv, "Ns:vy")) != -1) {
1969 size=(size_t)atol(optarg);
1974 case 'v': /* for compatibility to newfs */
1994 * Now try to guess the (raw)device name.
1996 if (0 == strrchr(device, '/')) {
1998 * No path prefix was given, so try in that order:
2004 * FreeBSD now doesn't distinguish between raw and block
2005 * devices any longer, but it should still work this way.
2007 len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/");
2008 special=(char *)malloc(len);
2009 if(special == NULL) {
2010 errx(1, "malloc failed");
2012 snprintf(special, len, "%sr%s", _PATH_DEV, device);
2013 if (stat(special, &st) == -1) {
2014 snprintf(special, len, "%s%s", _PATH_DEV, device);
2015 if (stat(special, &st) == -1) {
2016 snprintf(special, len, "%svinum/r%s",
2018 if (stat(special, &st) == -1) {
2019 /* For now this is the 'last resort' */
2020 snprintf(special, len, "%svinum/%s",
2029 * Try to access our devices for writing ...
2034 fso = open(device, O_WRONLY);
2036 err(1, "%s", device);
2043 fsi = open(device, O_RDONLY);
2045 err(1, "%s", device);
2049 * Try to read a label and gess the slice if not specified. This
2050 * code should guess the right thing and avaid to bother the user
2051 * user with the task of specifying the option -v on vinum volumes.
2053 cp=device+strlen(device)-1;
2054 lp = get_disklabel(fsi);
2055 if(lp->d_type == DTYPE_VINUM) {
2056 pp = &lp->d_partitions[0];
2057 } else if (isdigit(*cp)) {
2058 pp = &lp->d_partitions[2];
2059 } else if (*cp>='a' && *cp<='h') {
2060 pp = &lp->d_partitions[*cp - 'a'];
2062 errx(1, "unknown device");
2066 * Check if that partition looks suited for growing a file system.
2068 if (pp->p_size < 1) {
2069 errx(1, "partition is unavailable");
2071 if (pp->p_fstype != FS_BSDFFS) {
2072 errx(1, "partition not 4.2BSD");
2076 * Read the current superblock, and take a backup.
2078 rdfs((daddr_t)(SBOFF/DEV_BSIZE), (size_t)SBSIZE, (void *)&(osblock),
2080 if (osblock.fs_magic != FS_MAGIC) {
2081 errx(1, "superblock not recognized");
2083 memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2));
2085 DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
2086 DBG_DUMP_FS(&sblock,
2090 * Determine size to grow to. Default to the full size specified in
2093 sblock.fs_size = dbtofsb(&osblock, pp->p_size);
2095 if (size > pp->p_size){
2096 errx(1, "There is not enough space (%d < %d)",
2099 sblock.fs_size = dbtofsb(&osblock, size);
2103 * Are we really growing ?
2105 if(osblock.fs_size >= sblock.fs_size) {
2106 errx(1, "we are not growing (%d->%d)", osblock.fs_size,
2113 * Check if we find an active snapshot.
2115 if(ExpertFlag == 0) {
2116 for(j=0; j<FSMAXSNAP; j++) {
2117 if(sblock.fs_snapinum[j]) {
2118 errx(1, "active snapshot found in filesystem\n"
2119 " please remove all snapshots before "
2122 if(!sblock.fs_snapinum[j]) { /* list is dense */
2129 if (ExpertFlag == 0 && Nflag == 0) {
2130 printf("We strongly recommend you to make a backup "
2131 "before growing the Filesystem\n\n"
2132 " Did you backup your data (Yes/No) ? ");
2133 fgets(reply, (int)sizeof(reply), stdin);
2134 if (strcmp(reply, "Yes\n")){
2135 printf("\n Nothing done \n");
2140 printf("new filesystemsize is: %d frags\n", sblock.fs_size);
2143 * Try to access our new last block in the filesystem. Even if we
2144 * later on realize we have to abort our operation, on that block
2145 * there should be no data, so we can't destroy something yet.
2147 wtfs((daddr_t)pp->p_size-1, (size_t)DEV_BSIZE, (void *)&sblock, fso,
2151 * Now calculate new superblock values and check for reasonable
2152 * bound for new file system size:
2153 * fs_size: is derived from label or user input
2154 * fs_dsize: should get updated in the routines creating or
2155 * updating the cylinder groups on the fly
2156 * fs_cstotal: should get updated in the routines creating or
2157 * updating the cylinder groups
2161 * Update the number of cylinders in the filesystem.
2163 sblock.fs_ncyl = sblock.fs_size * NSPF(&sblock) / sblock.fs_spc;
2164 if (sblock.fs_size * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
2169 * Update the number of cylinder groups in the filesystem.
2171 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
2172 if (sblock.fs_ncyl % sblock.fs_cpg) {
2176 if ((sblock.fs_size - (sblock.fs_ncg-1) * sblock.fs_fpg) <
2177 sblock.fs_fpg && cgdmin(&sblock, (sblock.fs_ncg-1))-
2178 cgbase(&sblock, (sblock.fs_ncg-1)) > (sblock.fs_size -
2179 (sblock.fs_ncg-1) * sblock.fs_fpg )) {
2181 * The space in the new last cylinder group is too small,
2185 #if 1 /* this is a bit more safe */
2186 sblock.fs_ncyl = sblock.fs_ncg * sblock.fs_cpg;
2188 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
2190 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
2191 printf( "Warning: %d sector(s) cannot be allocated.\n",
2192 (sblock.fs_size-(sblock.fs_ncg)*sblock.fs_fpg) *
2194 sblock.fs_size = sblock.fs_ncyl * sblock.fs_spc / NSPF(&sblock);
2198 * Update the space for the cylinder group summary information in the
2199 * respective cylinder group data area.
2202 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
2204 if(osblock.fs_size >= sblock.fs_size) {
2205 errx(1, "not enough new space");
2208 DBG_PRINT0("sblock calculated\n");
2211 * Ok, everything prepared, so now let's do the tricks.
2213 growfs(fsi, fso, Nflag);
2216 * Update the disk label.
2218 pp->p_fsize = sblock.fs_fsize;
2219 pp->p_frag = sblock.fs_frag;
2220 pp->p_cpg = sblock.fs_cpg;
2222 return_disklabel(fso, lp, Nflag);
2223 DBG_PRINT0("label rewritten\n");
2226 if(fso>-1) close(fso);
2234 /* ************************************************** return_disklabel ***** */
2236 * Write the updated disklabel back to disk.
2239 return_disklabel(int fd, struct disklabel *lp, unsigned int Nflag)
2241 DBG_FUNC("return_disklabel")
2257 * recalculate checksum
2259 while(ptr < (u_short *)&lp->d_partitions[lp->d_npartitions]) {
2264 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
2265 errx(1, "DIOCWDINFO failed");
2274 /* ***************************************************** get_disklabel ***** */
2276 * Read the disklabel from disk.
2278 static struct disklabel *
2279 get_disklabel(int fd)
2281 DBG_FUNC("get_disklabel")
2282 static struct disklabel *lab;
2286 lab=(struct disklabel *)malloc(sizeof(struct disklabel));
2288 errx(1, "malloc failed");
2290 if (ioctl(fd, DIOCGDINFO, (char *)lab) < 0) {
2291 errx(1, "DIOCGDINFO failed");
2299 /* ************************************************************* usage ***** */
2301 * Dump a line of usage.
2310 fprintf(stderr, "usage: growfs [-Ny] [-s size] special\n");
2316 /* *********************************************************** updclst ***** */
2318 * This updates most paramters and the bitmap related to cluster. We have to
2319 * assume, that sblock, osblock, acg are set up.
2329 if(sblock.fs_contigsumsize < 1) { /* no clustering */
2333 * update cluster allocation map
2335 setbit(cg_clustersfree(&acg), block);
2338 * update cluster summary table
2342 * calculate size for the trailing cluster
2344 for(block--; lcs<sblock.fs_contigsumsize; block--, lcs++ ) {
2345 if(isclr(cg_clustersfree(&acg), block)){
2350 if(lcs < sblock.fs_contigsumsize) {
2352 cg_clustersum(&acg)[lcs]--;
2355 cg_clustersum(&acg)[lcs]++;
2362 /* *********************************************************** updrefs ***** */
2364 * This updates all references to relocated blocks for the given inode. The
2365 * inode is given as number within the cylinder group, and the number of the
2369 updrefs(int cg, ino_t in, struct gfs_bpp *bp, int fsi, int fso, unsigned int
2373 unsigned int ictr, ind2ctr, ind3ctr;
2374 ufs_daddr_t *iptr, *ind2ptr, *ind3ptr;
2376 int remaining_blocks;
2381 * XXX We should skip unused inodes even from beeing read from disk
2382 * here by using the bitmap.
2384 ino=ginode(in, fsi, cg);
2385 if(!((ino->di_mode & IFMT)==IFDIR || (ino->di_mode & IFMT)==IFREG ||
2386 (ino->di_mode & IFMT)==IFLNK)) {
2388 return; /* only check DIR, FILE, LINK */
2390 if(((ino->di_mode & IFMT)==IFLNK) && (ino->di_size<MAXSYMLINKLEN)) {
2392 return; /* skip short symlinks */
2396 return; /* skip empty file */
2398 if(!ino->di_blocks) {
2400 return; /* skip empty swiss cheesy file or old fastlink */
2402 DBG_PRINT2("scg checking inode (%d in %d)\n",
2407 * Start checking all direct blocks.
2409 remaining_blocks=howmany(ino->di_size, sblock.fs_bsize);
2410 for(ictr=0; ictr < MIN(NDADDR, (unsigned int)remaining_blocks);
2412 iptr=&(ino->di_db[ictr]);
2414 cond_bl_upd(iptr, bp, GFS_PS_INODE, fso, Nflag);
2417 DBG_PRINT0("~~scg direct blocks checked\n");
2419 remaining_blocks-=NDADDR;
2420 if(remaining_blocks<0) {
2426 * Start checking first indirect block
2428 cond_bl_upd(&(ino->di_ib[0]), bp, GFS_PS_INODE, fso, Nflag);
2429 i1_src=fsbtodb(&sblock, ino->di_ib[0]);
2430 rdfs(i1_src, (size_t)sblock.fs_bsize, (void *)&i1blk, fsi);
2431 for(ictr=0; ictr < MIN(howmany(sblock.fs_bsize,
2432 sizeof(ufs_daddr_t)), (unsigned int)remaining_blocks);
2434 iptr=&((ufs_daddr_t *)(void *)&i1blk)[ictr];
2436 cond_bl_upd(iptr, bp, GFS_PS_IND_BLK_LVL1,
2441 DBG_PRINT0("scg indirect_1 blocks checked\n");
2443 remaining_blocks-= howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
2444 if(remaining_blocks<0) {
2450 * Start checking second indirect block
2452 cond_bl_upd(&(ino->di_ib[1]), bp, GFS_PS_INODE, fso, Nflag);
2453 i2_src=fsbtodb(&sblock, ino->di_ib[1]);
2454 rdfs(i2_src, (size_t)sblock.fs_bsize, (void *)&i2blk, fsi);
2455 for(ind2ctr=0; ind2ctr < howmany(sblock.fs_bsize,
2456 sizeof(ufs_daddr_t)); ind2ctr++) {
2457 ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)[ind2ctr];
2461 cond_bl_upd(ind2ptr, bp, GFS_PS_IND_BLK_LVL2, fso,
2463 i1_src=fsbtodb(&sblock, *ind2ptr);
2464 rdfs(i1_src, (size_t)sblock.fs_bsize, (void *)&i1blk,
2466 for(ictr=0; ictr<MIN(howmany((unsigned int)
2467 sblock.fs_bsize, sizeof(ufs_daddr_t)),
2468 (unsigned int)remaining_blocks); ictr++) {
2469 iptr=&((ufs_daddr_t *)(void *)&i1blk)[ictr];
2471 cond_bl_upd(iptr, bp,
2472 GFS_PS_IND_BLK_LVL1, fso, Nflag);
2477 DBG_PRINT0("scg indirect_2 blocks checked\n");
2479 #define SQUARE(a) ((a)*(a))
2480 remaining_blocks-=SQUARE(howmany(sblock.fs_bsize, sizeof(ufs_daddr_t)));
2482 if(remaining_blocks<0) {
2489 * Start checking third indirect block
2491 cond_bl_upd(&(ino->di_ib[2]), bp, GFS_PS_INODE, fso, Nflag);
2492 i3_src=fsbtodb(&sblock, ino->di_ib[2]);
2493 rdfs(i3_src, (size_t)sblock.fs_bsize, (void *)&i3blk, fsi);
2494 for(ind3ctr=0; ind3ctr < howmany(sblock.fs_bsize,
2495 sizeof(ufs_daddr_t)); ind3ctr ++) {
2496 ind3ptr=&((ufs_daddr_t *)(void *)&i3blk)[ind3ctr];
2500 cond_bl_upd(ind3ptr, bp, GFS_PS_IND_BLK_LVL3, fso,
2502 i2_src=fsbtodb(&sblock, *ind3ptr);
2503 rdfs(i2_src, (size_t)sblock.fs_bsize, (void *)&i2blk,
2505 for(ind2ctr=0; ind2ctr < howmany(sblock.fs_bsize,
2506 sizeof(ufs_daddr_t)); ind2ctr ++) {
2507 ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)
2512 cond_bl_upd(ind2ptr, bp, GFS_PS_IND_BLK_LVL2,
2514 i1_src=fsbtodb(&sblock, *ind2ptr);
2515 rdfs(i1_src, (size_t)sblock.fs_bsize,
2516 (void *)&i1blk, fsi);
2517 for(ictr=0; ictr < MIN(howmany(sblock.fs_bsize,
2518 sizeof(ufs_daddr_t)),
2519 (unsigned int)remaining_blocks); ictr++) {
2520 iptr=&((ufs_daddr_t *)(void *)&i1blk)
2523 cond_bl_upd(iptr, bp,
2524 GFS_PS_IND_BLK_LVL1, fso,
2532 DBG_PRINT0("scg indirect_3 blocks checked\n");