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 $
40 * @(#) Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz Copyright (c) 1980, 1989, 1993 The Regents of the University of California. All rights reserved.
41 * $FreeBSD: src/sbin/growfs/growfs.c,v 1.4.2.2 2001/08/14 12:45:11 chm Exp $
42 * $DragonFly: src/sbin/growfs/growfs.c,v 1.4 2005/11/06 12:16:44 swildner Exp $
45 /* ********************************************************** INCLUDES ***** */
46 #include <sys/param.h>
47 #include <sys/disklabel.h>
48 #include <sys/ioctl.h>
59 #include <vfs/ufs/dinode.h>
60 #include <vfs/ufs/fs.h>
64 /* *************************************************** GLOBALS & TYPES ***** */
66 int _dbg_lvl_
= (DL_INFO
); /* DL_TRC */
73 #define sblock fsun1.fs /* the new superblock */
74 #define osblock fsun2.fs /* the old superblock */
80 #define acg cgun1.cg /* a cylinder cgroup (new) */
81 #define aocg cgun2.cg /* an old cylinder group */
83 static char ablk
[MAXBSIZE
]; /* a block */
84 static char i1blk
[MAXBSIZE
]; /* some indirect blocks */
85 static char i2blk
[MAXBSIZE
];
86 static char i3blk
[MAXBSIZE
];
88 /* where to write back updated blocks */
89 static daddr_t in_src
, i1_src
, i2_src
, i3_src
;
91 /* what object contains the reference */
99 static struct csum
*fscs
; /* cylinder summary */
101 static struct dinode zino
[MAXBSIZE
/sizeof(struct dinode
)]; /* some inodes */
104 * An array of elements of type struct gfs_bpp describes all blocks to
105 * be relocated in order to free the space needed for the cylinder group
106 * summary for all cylinder groups located in the first cylinder group.
109 daddr_t old
; /* old block number */
110 daddr_t
new; /* new block number */
111 #define GFS_FL_FIRST 1
112 #define GFS_FL_LAST 2
113 unsigned int flags
; /* special handling required */
114 int found
; /* how many references were updated */
117 /* ******************************************************** PROTOTYPES ***** */
118 static void growfs(int, int, unsigned int);
119 static void rdfs(daddr_t
, size_t, void *, int);
120 static void wtfs(daddr_t
, size_t, void *, int, unsigned int);
121 static daddr_t
alloc(void);
122 static int charsperline(void);
123 static void usage(void);
124 static int isblock(struct fs
*, unsigned char *, int);
125 static void clrblock(struct fs
*, unsigned char *, int);
126 static void setblock(struct fs
*, unsigned char *, int);
127 static void initcg(int, time_t, int, unsigned int);
128 static void updjcg(int, time_t, int, int, unsigned int);
129 static void updcsloc(time_t, int, int, unsigned int);
130 static struct disklabel
*get_disklabel(int);
131 static void return_disklabel(int, struct disklabel
*, unsigned int);
132 static struct dinode
*ginode(ino_t
, int, int);
133 static void frag_adjust(daddr_t
, int);
134 static void cond_bl_upd(ufs_daddr_t
*, struct gfs_bpp
*,
135 enum pointer_source
, int, unsigned int);
136 static void updclst(int);
137 static void updrefs(int, ino_t
, struct gfs_bpp
*, int, int, unsigned int);
139 /* ************************************************************ growfs ***** */
141 * Here we actually start growing the filesystem. We basically read the
142 * cylinder summary from the first cylinder group as we want to update
143 * this on the fly during our various operations. First we handle the
144 * changes in the former last cylinder group. Afterwards we create all new
145 * cylinder groups. Now we handle the cylinder group containing the
146 * cylinder summary which might result in a relocation of the whole
147 * structure. In the end we write back the updated cylinder summary, the
148 * new superblock, and slightly patched versions of the super block
152 growfs(int fsi
, int fso
, unsigned int Nflag
)
161 static int randinit
=0;
169 #else /* not FSIRAND */
177 * Get the cylinder summary into the memory.
179 fscs
= (struct csum
*)calloc((size_t)1, (size_t)sblock
.fs_cssize
);
181 errx(1, "calloc failed");
183 for (i
= 0; i
< osblock
.fs_cssize
; i
+= osblock
.fs_bsize
) {
184 rdfs(fsbtodb(&osblock
, osblock
.fs_csaddr
+
185 numfrags(&osblock
, i
)), (size_t)MIN(osblock
.fs_cssize
- i
,
186 osblock
.fs_bsize
), (void *)(((char *)fscs
)+i
), fsi
);
191 struct csum
*dbg_csp
;
196 for(dbg_csc
=0; dbg_csc
<osblock
.fs_ncg
; dbg_csc
++) {
197 snprintf(dbg_line
, sizeof(dbg_line
),
198 "%d. old csum in old location", dbg_csc
);
199 DBG_DUMP_CSUM(&osblock
,
204 #endif /* FS_DEBUG */
205 DBG_PRINT0("fscs read\n");
208 * Do all needed changes in the former last cylinder group.
210 updjcg(osblock
.fs_ncg
-1, utime
, fsi
, fso
, Nflag
);
213 * Dump out summary information about file system.
215 printf("growfs:\t%d sectors in %d %s of %d tracks, %d sectors\n",
216 sblock
.fs_size
* NSPF(&sblock
), sblock
.fs_ncyl
,
217 "cylinders", sblock
.fs_ntrak
, sblock
.fs_nsect
);
218 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
219 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
220 (float)sblock
.fs_size
* sblock
.fs_fsize
* B2MBFACTOR
,
221 sblock
.fs_ncg
, sblock
.fs_cpg
,
222 (float)sblock
.fs_fpg
* sblock
.fs_fsize
* B2MBFACTOR
,
227 * Now build the cylinders group blocks and
228 * then print out indices of cylinder groups.
230 printf("super-block backups (for fsck -b #) at:\n");
232 width
= charsperline();
235 * Iterate for only the new cylinder groups.
237 for (cylno
= osblock
.fs_ncg
; cylno
< sblock
.fs_ncg
; cylno
++) {
238 initcg(cylno
, utime
, fso
, Nflag
);
239 j
= sprintf(tmpbuf
, " %d%s",
240 (int)fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)),
241 cylno
< (sblock
.fs_ncg
-1) ? "," : "" );
242 if (i
+ j
>= width
) {
247 printf("%s", tmpbuf
);
253 * Do all needed changes in the first cylinder group.
254 * allocate blocks in new location
256 updcsloc(utime
, fsi
, fso
, Nflag
);
259 * Now write the cylinder summary back to disk.
261 for (i
= 0; i
< sblock
.fs_cssize
; i
+= sblock
.fs_bsize
) {
262 wtfs(fsbtodb(&sblock
, sblock
.fs_csaddr
+ numfrags(&sblock
, i
)),
263 (size_t)MIN(sblock
.fs_cssize
- i
, sblock
.fs_bsize
),
264 (void *)(((char *)fscs
) + i
), fso
, Nflag
);
266 DBG_PRINT0("fscs written\n");
270 struct csum
*dbg_csp
;
275 for(dbg_csc
=0; dbg_csc
<sblock
.fs_ncg
; dbg_csc
++) {
276 snprintf(dbg_line
, sizeof(dbg_line
),
277 "%d. new csum in new location", dbg_csc
);
278 DBG_DUMP_CSUM(&sblock
,
283 #endif /* FS_DEBUG */
286 * Now write the new superblock back to disk.
288 sblock
.fs_time
= utime
;
289 wtfs((daddr_t
)(SBOFF
/ DEV_BSIZE
), (size_t)SBSIZE
, (void *)&sblock
,
291 DBG_PRINT0("sblock written\n");
293 "new initial sblock");
296 * Clean up the dynamic fields in our superblock copies.
301 sblock
.fs_cgrotor
= 0;
303 memset((void *)&sblock
.fs_fsmnt
, 0, sizeof(sblock
.fs_fsmnt
));
304 sblock
.fs_flags
&= FS_DOSOFTDEP
;
308 * The following fields are currently distributed from the superblock
316 * fs_flags regarding SOFTPDATES
318 * We probably should rather change the summary for the cylinder group
319 * statistics here to the value of what would be in there, if the file
320 * system were created initially with the new size. Therefor we still
321 * need to find an easy way of calculating that.
322 * Possibly we can try to read the first superblock copy and apply the
323 * "diffed" stats between the old and new superblock by still copying
324 * certain parameters onto that.
328 * Write out the duplicate super blocks.
330 for (cylno
= 0; cylno
< sblock
.fs_ncg
; cylno
++) {
331 wtfs(fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)),
332 (size_t)SBSIZE
, (void *)&sblock
, fso
, Nflag
);
334 DBG_PRINT0("sblock copies written\n");
336 "new other sblocks");
342 /* ************************************************************ initcg ***** */
344 * This creates a new cylinder group structure, for more details please see
345 * the source of newfs(8), as this function is taken over almost unchanged.
346 * As this is never called for the first cylinder group, the special
347 * provisions for that case are removed here.
350 initcg(int cylno
, time_t utime
, int fso
, unsigned int Nflag
)
353 daddr_t cbase
, d
, dlower
, dupper
, dmax
, blkno
;
363 * Determine block bounds for cylinder group.
365 cbase
= cgbase(&sblock
, cylno
);
366 dmax
= cbase
+ sblock
.fs_fpg
;
367 if (dmax
> sblock
.fs_size
) {
368 dmax
= sblock
.fs_size
;
370 dlower
= cgsblock(&sblock
, cylno
) - cbase
;
371 dupper
= cgdmin(&sblock
, cylno
) - cbase
;
372 if (cylno
== 0) { /* XXX fscs may be relocated */
373 dupper
+= howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
376 memset(&acg
, 0, (size_t)sblock
.fs_cgsize
);
378 acg
.cg_magic
= CG_MAGIC
;
380 if (cylno
== sblock
.fs_ncg
- 1) {
381 acg
.cg_ncyl
= sblock
.fs_ncyl
% sblock
.fs_cpg
;
383 acg
.cg_ncyl
= sblock
.fs_cpg
;
385 acg
.cg_niblk
= sblock
.fs_ipg
;
386 acg
.cg_ndblk
= dmax
- cbase
;
387 if (sblock
.fs_contigsumsize
> 0) {
388 acg
.cg_nclusterblks
= acg
.cg_ndblk
/ sblock
.fs_frag
;
390 acg
.cg_btotoff
= &acg
.cg_space
[0] - (u_char
*)(&acg
.cg_firstfield
);
391 acg
.cg_boff
= acg
.cg_btotoff
+ sblock
.fs_cpg
* sizeof(int32_t);
392 acg
.cg_iusedoff
= acg
.cg_boff
+
393 sblock
.fs_cpg
* sblock
.fs_nrpos
* sizeof(u_int16_t
);
394 acg
.cg_freeoff
= acg
.cg_iusedoff
+ howmany(sblock
.fs_ipg
, NBBY
);
395 if (sblock
.fs_contigsumsize
<= 0) {
396 acg
.cg_nextfreeoff
= acg
.cg_freeoff
+
397 howmany(sblock
.fs_cpg
* sblock
.fs_spc
/ NSPF(&sblock
), NBBY
);
399 acg
.cg_clustersumoff
= acg
.cg_freeoff
+ howmany
400 (sblock
.fs_cpg
* sblock
.fs_spc
/ NSPF(&sblock
), NBBY
) -
402 acg
.cg_clustersumoff
=
403 roundup(acg
.cg_clustersumoff
, sizeof(u_int32_t
));
404 acg
.cg_clusteroff
= acg
.cg_clustersumoff
+
405 (sblock
.fs_contigsumsize
+ 1) * sizeof(u_int32_t
);
406 acg
.cg_nextfreeoff
= acg
.cg_clusteroff
+ howmany
407 (sblock
.fs_cpg
* sblock
.fs_spc
/ NSPB(&sblock
), NBBY
);
409 if (acg
.cg_nextfreeoff
-(int)(&acg
.cg_firstfield
) > sblock
.fs_cgsize
) {
411 * XXX This should never happen as we would have had that panic
412 * already on filesystem creation
414 errx(37, "panic: cylinder group too big");
416 acg
.cg_cs
.cs_nifree
+= sblock
.fs_ipg
;
418 for (i
= 0; (size_t)i
< ROOTINO
; i
++) {
419 setbit(cg_inosused(&acg
), i
);
420 acg
.cg_cs
.cs_nifree
--;
422 for (i
= 0; i
< sblock
.fs_ipg
/ INOPF(&sblock
); i
+= sblock
.fs_frag
) {
424 for (j
= 0; j
< sblock
.fs_bsize
/ sizeof(struct dinode
); j
++) {
425 zino
[j
].di_gen
= random();
428 wtfs(fsbtodb(&sblock
, cgimin(&sblock
, cylno
) + i
),
429 (size_t)sblock
.fs_bsize
, (void *)zino
, fso
, Nflag
);
431 for (d
= 0; d
< dlower
; d
+= sblock
.fs_frag
) {
432 blkno
= d
/ sblock
.fs_frag
;
433 setblock(&sblock
, cg_blksfree(&acg
), blkno
);
434 if (sblock
.fs_contigsumsize
> 0) {
435 setbit(cg_clustersfree(&acg
), blkno
);
437 acg
.cg_cs
.cs_nbfree
++;
438 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]++;
439 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
440 [cbtorpos(&sblock
, d
)]++;
442 sblock
.fs_dsize
+= dlower
;
443 sblock
.fs_dsize
+= acg
.cg_ndblk
- dupper
;
444 if ((i
= dupper
% sblock
.fs_frag
)) {
445 acg
.cg_frsum
[sblock
.fs_frag
- i
]++;
446 for (d
= dupper
+ sblock
.fs_frag
- i
; dupper
< d
; dupper
++) {
447 setbit(cg_blksfree(&acg
), dupper
);
448 acg
.cg_cs
.cs_nffree
++;
451 for (d
= dupper
; d
+ sblock
.fs_frag
<= dmax
- cbase
; ) {
452 blkno
= d
/ sblock
.fs_frag
;
453 setblock(&sblock
, cg_blksfree(&acg
), blkno
);
454 if (sblock
.fs_contigsumsize
> 0) {
455 setbit(cg_clustersfree(&acg
), blkno
);
457 acg
.cg_cs
.cs_nbfree
++;
458 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]++;
459 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
460 [cbtorpos(&sblock
, d
)]++;
463 if (d
< dmax
- cbase
) {
464 acg
.cg_frsum
[dmax
- cbase
- d
]++;
465 for (; d
< dmax
- cbase
; d
++) {
466 setbit(cg_blksfree(&acg
), d
);
467 acg
.cg_cs
.cs_nffree
++;
470 if (sblock
.fs_contigsumsize
> 0) {
471 int32_t *sump
= cg_clustersum(&acg
);
472 u_char
*mapp
= cg_clustersfree(&acg
);
477 for (i
= 0; i
< acg
.cg_nclusterblks
; i
++) {
478 if ((map
& bit
) != 0) {
480 } else if (run
!= 0) {
481 if (run
> sblock
.fs_contigsumsize
) {
482 run
= sblock
.fs_contigsumsize
;
487 if ((i
& (NBBY
- 1)) != (NBBY
- 1)) {
495 if (run
> sblock
.fs_contigsumsize
) {
496 run
= sblock
.fs_contigsumsize
;
501 sblock
.fs_cstotal
.cs_ndir
+= acg
.cg_cs
.cs_ndir
;
502 sblock
.fs_cstotal
.cs_nffree
+= acg
.cg_cs
.cs_nffree
;
503 sblock
.fs_cstotal
.cs_nbfree
+= acg
.cg_cs
.cs_nbfree
;
504 sblock
.fs_cstotal
.cs_nifree
+= acg
.cg_cs
.cs_nifree
;
506 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, cylno
)),
507 (size_t)sblock
.fs_bsize
, (void *)&acg
, fso
, Nflag
);
516 /* ******************************************************* frag_adjust ***** */
518 * Here we add or subtract (sign +1/-1) the available fragments in a given
519 * block to or from the fragment statistics. By subtracting before and adding
520 * after an operation on the free frag map we can easy update the fragment
521 * statistic, which seems to be otherwise an rather complex operation.
524 frag_adjust(daddr_t frag
, int sign
)
526 DBG_FUNC("frag_adjust")
534 * Here frag only needs to point to any fragment in the block we want
537 for(f
=rounddown(frag
, sblock
.fs_frag
);
538 f
<roundup(frag
+1, sblock
.fs_frag
);
541 * Count contiguos free fragments.
543 if(isset(cg_blksfree(&acg
), f
)) {
546 if(fragsize
&& fragsize
<sblock
.fs_frag
) {
548 * We found something in between.
550 acg
.cg_frsum
[fragsize
]+=sign
;
551 DBG_PRINT2("frag_adjust [%d]+=%d\n",
558 if(fragsize
&& fragsize
<sblock
.fs_frag
) {
560 * We found something.
562 acg
.cg_frsum
[fragsize
]+=sign
;
563 DBG_PRINT2("frag_adjust [%d]+=%d\n",
567 DBG_PRINT2("frag_adjust [[%d]]+=%d\n",
575 /* ******************************************************* cond_bl_upd ***** */
577 * Here we conditionally update a pointer to a fragment. We check for all
578 * relocated blocks if any of it's fragments is referenced by the current
579 * field, and update the pointer to the respective fragment in our new
580 * block. If we find a reference we write back the block immediately,
581 * as there is no easy way for our general block reading engine to figure
582 * out if a write back operation is needed.
585 cond_bl_upd(ufs_daddr_t
*block
, struct gfs_bpp
*field
,
586 enum pointer_source source
, int fso
, unsigned int Nflag
)
588 DBG_FUNC("cond_bl_upd")
596 while(f
->old
) { /* for all old blocks */
597 if(*block
/sblock
.fs_frag
== f
->old
) {
599 * The fragment is part of the block, so update.
601 *block
=(f
->new*sblock
.fs_frag
+(*block
%sblock
.fs_frag
));
603 DBG_PRINT3("scg (%d->%d)[%d] reference updated\n",
606 *block
%sblock
.fs_frag
);
608 /* Write the block back to disk immediately */
614 case GFS_PS_IND_BLK_LVL1
:
618 case GFS_PS_IND_BLK_LVL2
:
622 case GFS_PS_IND_BLK_LVL3
:
632 * XXX If src is not of type inode we have to
633 * implement copy on write here in case
634 * of active snapshots.
636 wtfs(dst
, (size_t)sblock
.fs_bsize
, (void *)src
,
641 * The same block can't be found again in this loop.
652 /* ************************************************************ updjcg ***** */
654 * Here we do all needed work for the former last cylinder group. It has to be
655 * changed in any case, even if the filesystem ended exactly on the end of
656 * this group, as there is some slightly inconsistent handling of the number
657 * of cylinders in the cylinder group. We start again by reading the cylinder
658 * group from disk. If the last block was not fully available, we first handle
659 * the missing fragments, then we handle all new full blocks in that file
660 * system and finally we handle the new last fragmented block in the file
661 * system. We again have to handle the fragment statistics rotational layout
662 * tables and cluster summary during all those operations.
665 updjcg(int cylno
, time_t utime
, int fsi
, int fso
, unsigned int Nflag
)
668 daddr_t cbase
, dmax
, dupper
;
676 * Read the former last (joining) cylinder group from disk, and make
679 rdfs(fsbtodb(&osblock
, cgtod(&osblock
, cylno
)),
680 (size_t)osblock
.fs_cgsize
, (void *)&aocg
, fsi
);
681 DBG_PRINT0("jcg read\n");
686 memcpy((void *)&cgun1
, (void *)&cgun2
, sizeof(cgun2
));
689 * If the cylinder group had already it's new final size almost
690 * nothing is to be done ... except:
691 * For some reason the value of cg_ncyl in the last cylinder group has
692 * to be zero instead of fs_cpg. As this is now no longer the last
693 * cylinder group we have to change that value now to fs_cpg.
696 if(cgbase(&osblock
, cylno
+1) == osblock
.fs_size
) {
697 acg
.cg_ncyl
=sblock
.fs_cpg
;
699 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, cylno
)),
700 (size_t)sblock
.fs_cgsize
, (void *)&acg
, fso
, Nflag
);
701 DBG_PRINT0("jcg written\n");
711 * Set up some variables needed later.
713 cbase
= cgbase(&sblock
, cylno
);
714 dmax
= cbase
+ sblock
.fs_fpg
;
715 if (dmax
> sblock
.fs_size
)
716 dmax
= sblock
.fs_size
;
717 dupper
= cgdmin(&sblock
, cylno
) - cbase
;
718 if (cylno
== 0) { /* XXX fscs may be relocated */
719 dupper
+= howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
723 * Set pointer to the cylinder summary for our cylinder group.
728 * Touch the cylinder group, update all fields in the cylinder group as
729 * needed, update the free space in the superblock.
732 if (cylno
== sblock
.fs_ncg
- 1) {
734 * This is still the last cylinder group.
736 acg
.cg_ncyl
= sblock
.fs_ncyl
% sblock
.fs_cpg
;
738 acg
.cg_ncyl
= sblock
.fs_cpg
;
740 DBG_PRINT4("jcg dbg: %d %u %d %u\n",
745 acg
.cg_ndblk
= dmax
- cbase
;
746 sblock
.fs_dsize
+= acg
.cg_ndblk
-aocg
.cg_ndblk
;
747 if (sblock
.fs_contigsumsize
> 0) {
748 acg
.cg_nclusterblks
= acg
.cg_ndblk
/ sblock
.fs_frag
;
752 * Now we have to update the free fragment bitmap for our new free
753 * space. There again we have to handle the fragmentation and also
754 * the rotational layout tables and the cluster summary. This is
755 * also done per fragment for the first new block if the old file
756 * system end was not on a block boundary, per fragment for the new
757 * last block if the new file system end is not on a block boundary,
758 * and per block for all space in between.
760 * Handle the first new block here if it was partially available
763 if(osblock
.fs_size
% sblock
.fs_frag
) {
764 if(roundup(osblock
.fs_size
, sblock
.fs_frag
)<=sblock
.fs_size
) {
766 * The new space is enough to fill at least this
770 for(i
=roundup(osblock
.fs_size
-cbase
, sblock
.fs_frag
)-1;
771 i
>=osblock
.fs_size
-cbase
;
773 setbit(cg_blksfree(&acg
), i
);
774 acg
.cg_cs
.cs_nffree
++;
779 * Check if the fragment just created could join an
780 * already existing fragment at the former end of the
783 if(isblock(&sblock
, cg_blksfree(&acg
),
784 ((osblock
.fs_size
- cgbase(&sblock
, cylno
))/
787 * The block is now completely available
789 DBG_PRINT0("block was\n");
790 acg
.cg_frsum
[osblock
.fs_size
%sblock
.fs_frag
]--;
791 acg
.cg_cs
.cs_nbfree
++;
792 acg
.cg_cs
.cs_nffree
-=sblock
.fs_frag
;
793 k
=rounddown(osblock
.fs_size
-cbase
,
795 cg_blktot(&acg
)[cbtocylno(&sblock
, k
)]++;
796 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, k
))
797 [cbtorpos(&sblock
, k
)]++;
798 updclst((osblock
.fs_size
-cbase
)/sblock
.fs_frag
);
801 * Lets rejoin a possible partially growed
805 while(isset(cg_blksfree(&acg
), i
) &&
806 (i
>=rounddown(osblock
.fs_size
-cbase
,
818 * We only grow by some fragments within this last
821 for(i
=sblock
.fs_size
-cbase
-1;
822 i
>=osblock
.fs_size
-cbase
;
824 setbit(cg_blksfree(&acg
), i
);
825 acg
.cg_cs
.cs_nffree
++;
829 * Lets rejoin a possible partially growed fragment.
832 while(isset(cg_blksfree(&acg
), i
) &&
833 (i
>=rounddown(osblock
.fs_size
-cbase
,
846 * Handle all new complete blocks here.
848 for(i
=roundup(osblock
.fs_size
-cbase
, sblock
.fs_frag
);
849 i
+sblock
.fs_frag
<=dmax
-cbase
; /* XXX <= or only < ? */
851 j
= i
/ sblock
.fs_frag
;
852 setblock(&sblock
, cg_blksfree(&acg
), j
);
854 acg
.cg_cs
.cs_nbfree
++;
855 cg_blktot(&acg
)[cbtocylno(&sblock
, i
)]++;
856 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, i
))
857 [cbtorpos(&sblock
, i
)]++;
861 * Handle the last new block if there are stll some new fragments left.
862 * Here we don't have to bother about the cluster summary or the even
863 * the rotational layout table.
865 if (i
< (dmax
- cbase
)) {
866 acg
.cg_frsum
[dmax
- cbase
- i
]++;
867 for (; i
< dmax
- cbase
; i
++) {
868 setbit(cg_blksfree(&acg
), i
);
869 acg
.cg_cs
.cs_nffree
++;
873 sblock
.fs_cstotal
.cs_nffree
+=
874 (acg
.cg_cs
.cs_nffree
- aocg
.cg_cs
.cs_nffree
);
875 sblock
.fs_cstotal
.cs_nbfree
+=
876 (acg
.cg_cs
.cs_nbfree
- aocg
.cg_cs
.cs_nbfree
);
878 * The following statistics are not changed here:
879 * sblock.fs_cstotal.cs_ndir
880 * sblock.fs_cstotal.cs_nifree
881 * As the statistics for this cylinder group are ready, copy it to
882 * the summary information array.
887 * Write the updated "joining" cylinder group back to disk.
889 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, cylno
)), (size_t)sblock
.fs_cgsize
,
890 (void *)&acg
, fso
, Nflag
);
891 DBG_PRINT0("jcg written\n");
900 /* ********************************************************** updcsloc ***** */
902 * Here we update the location of the cylinder summary. We have two possible
903 * ways of growing the cylinder summary.
904 * (1) We can try to grow the summary in the current location, and relocate
905 * possibly used blocks within the current cylinder group.
906 * (2) Alternatively we can relocate the whole cylinder summary to the first
907 * new completely empty cylinder group. Once the cylinder summary is no
908 * longer in the beginning of the first cylinder group you should never
909 * use a version of fsck which is not aware of the possibility to have
910 * this structure in a non standard place.
911 * Option (1) is considered to be less intrusive to the structure of the file-
912 * system. So we try to stick to that whenever possible. If there is not enough
913 * space in the cylinder group containing the cylinder summary we have to use
914 * method (2). In case of active snapshots in the filesystem we probably can
915 * completely avoid implementing copy on write if we stick to method (2) only.
918 updcsloc(time_t utime
, int fsi
, int fso
, unsigned int Nflag
)
924 daddr_t cbase
, dupper
, odupper
, d
, f
, g
;
934 if(howmany(sblock
.fs_cssize
, sblock
.fs_fsize
) ==
935 howmany(osblock
.fs_cssize
, osblock
.fs_fsize
)) {
937 * No new fragment needed.
942 ocscg
=dtog(&osblock
, osblock
.fs_csaddr
);
944 blocks
= 1+howmany(sblock
.fs_cssize
, sblock
.fs_bsize
)-
945 howmany(osblock
.fs_cssize
, osblock
.fs_bsize
);
948 * Read original cylinder group from disk, and make a copy.
949 * XXX If Nflag is set in some very rare cases we now miss
950 * some changes done in updjcg by reading the unmodified
953 rdfs(fsbtodb(&osblock
, cgtod(&osblock
, ocscg
)),
954 (size_t)osblock
.fs_cgsize
, (void *)&aocg
, fsi
);
955 DBG_PRINT0("oscg read\n");
960 memcpy((void *)&cgun1
, (void *)&cgun2
, sizeof(cgun2
));
963 * Touch the cylinder group, set up local variables needed later
964 * and update the superblock.
969 * XXX In the case of having active snapshots we may need much more
970 * blocks for the copy on write. We need each block twice, and
971 * also up to 8*3 blocks for indirect blocks for all possible
974 if(/*((int)sblock.fs_time&0x3)>0||*/ cs
->cs_nbfree
< blocks
) {
976 * There is not enough space in the old cylinder group to
977 * relocate all blocks as needed, so we relocate the whole
978 * cylinder group summary to a new group. We try to use the
979 * first complete new cylinder group just created. Within the
980 * cylinder group we allign the area immediately after the
981 * cylinder group information location in order to be as
982 * close as possible to the original implementation of ffs.
984 * First we have to make sure we'll find enough space in the
985 * new cylinder group. If not, then we currently give up.
986 * We start with freeing everything which was used by the
987 * fragments of the old cylinder summary in the current group.
988 * Now we write back the group meta data, read in the needed
989 * meta data from the new cylinder group, and start allocating
990 * within that group. Here we can assume, the group to be
991 * completely empty. Which makes the handling of fragments and
992 * clusters a lot easier.
995 if(sblock
.fs_ncg
-osblock
.fs_ncg
< 2) {
996 errx(2, "panic: not enough space");
1000 * Point "d" to the first fragment not used by the cylinder
1003 d
=osblock
.fs_csaddr
+(osblock
.fs_cssize
/osblock
.fs_fsize
);
1006 * Set up last cluster size ("lcs") already here. Calculate
1007 * the size for the trailing cluster just behind where "d"
1010 if(sblock
.fs_contigsumsize
> 0) {
1011 for(block
=howmany(d
%sblock
.fs_fpg
, sblock
.fs_frag
),
1012 lcs
=0; lcs
<sblock
.fs_contigsumsize
;
1014 if(isclr(cg_clustersfree(&acg
), block
)){
1021 * Point "d" to the last frag used by the cylinder summary.
1025 DBG_PRINT1("d=%d\n",
1027 if((d
+1)%sblock
.fs_frag
) {
1029 * The end of the cylinder summary is not a complete
1033 frag_adjust(d
%sblock
.fs_fpg
, -1);
1034 for(; (d
+1)%sblock
.fs_frag
; d
--) {
1035 DBG_PRINT1("d=%d\n",
1037 setbit(cg_blksfree(&acg
), d
%sblock
.fs_fpg
);
1038 acg
.cg_cs
.cs_nffree
++;
1039 sblock
.fs_cstotal
.cs_nffree
++;
1042 * Point "d" to the last fragment of the last
1043 * (incomplete) block of the clinder summary.
1046 frag_adjust(d
%sblock
.fs_fpg
, 1);
1048 if(isblock(&sblock
, cg_blksfree(&acg
),
1049 (d
%sblock
.fs_fpg
)/sblock
.fs_frag
)) {
1050 DBG_PRINT1("d=%d\n",
1052 acg
.cg_cs
.cs_nffree
-=sblock
.fs_frag
;
1053 acg
.cg_cs
.cs_nbfree
++;
1054 sblock
.fs_cstotal
.cs_nffree
-=sblock
.fs_frag
;
1055 sblock
.fs_cstotal
.cs_nbfree
++;
1056 cg_blktot(&acg
)[cbtocylno(&sblock
,
1057 d
%sblock
.fs_fpg
)]++;
1058 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
,
1059 d
%sblock
.fs_fpg
))[cbtorpos(&sblock
,
1060 d
%sblock
.fs_fpg
)]++;
1061 if(sblock
.fs_contigsumsize
> 0) {
1062 setbit(cg_clustersfree(&acg
),
1063 (d
%sblock
.fs_fpg
)/sblock
.fs_frag
);
1064 if(lcs
< sblock
.fs_contigsumsize
) {
1070 cg_clustersum(&acg
)[lcs
]++;
1075 * Point "d" to the first fragment of the block before
1076 * the last incomplete block.
1081 DBG_PRINT1("d=%d\n",
1083 for(d
=rounddown(d
, sblock
.fs_frag
); d
>= osblock
.fs_csaddr
;
1084 d
-=sblock
.fs_frag
) {
1086 DBG_PRINT1("d=%d\n",
1088 setblock(&sblock
, cg_blksfree(&acg
),
1089 (d
%sblock
.fs_fpg
)/sblock
.fs_frag
);
1090 acg
.cg_cs
.cs_nbfree
++;
1091 sblock
.fs_cstotal
.cs_nbfree
++;
1092 cg_blktot(&acg
)[cbtocylno(&sblock
, d
%sblock
.fs_fpg
)]++;
1093 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
,
1094 d
%sblock
.fs_fpg
))[cbtorpos(&sblock
,
1095 d
%sblock
.fs_fpg
)]++;
1096 if(sblock
.fs_contigsumsize
> 0) {
1097 setbit(cg_clustersfree(&acg
),
1098 (d
%sblock
.fs_fpg
)/sblock
.fs_frag
);
1100 * The last cluster size is already set up.
1102 if(lcs
< sblock
.fs_contigsumsize
) {
1104 cg_clustersum(&acg
)[lcs
]--;
1107 cg_clustersum(&acg
)[lcs
]++;
1114 * Now write the former cylinder group containing the cylinder
1115 * summary back to disk.
1117 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, ocscg
)),
1118 (size_t)sblock
.fs_cgsize
, (void *)&acg
, fso
, Nflag
);
1119 DBG_PRINT0("oscg written\n");
1120 DBG_DUMP_CG(&sblock
,
1125 * Find the beginning of the new cylinder group containing the
1128 sblock
.fs_csaddr
=cgdmin(&sblock
, osblock
.fs_ncg
);
1129 ncscg
=dtog(&sblock
, sblock
.fs_csaddr
);
1134 * If Nflag is specified, we would now read random data instead
1135 * of an empty cg structure from disk. So we can't simulate that
1139 DBG_PRINT0("nscg update skipped\n");
1145 * Read the future cylinder group containing the cylinder
1146 * summary from disk, and make a copy.
1148 rdfs(fsbtodb(&sblock
, cgtod(&sblock
, ncscg
)),
1149 (size_t)sblock
.fs_cgsize
, (void *)&aocg
, fsi
);
1150 DBG_PRINT0("nscg read\n");
1151 DBG_DUMP_CG(&sblock
,
1155 memcpy((void *)&cgun1
, (void *)&cgun2
, sizeof(cgun2
));
1158 * Allocate all complete blocks used by the new cylinder
1161 for(d
=sblock
.fs_csaddr
; d
+sblock
.fs_frag
<=
1162 sblock
.fs_csaddr
+(sblock
.fs_cssize
/sblock
.fs_fsize
);
1163 d
+=sblock
.fs_frag
) {
1164 clrblock(&sblock
, cg_blksfree(&acg
),
1165 (d
%sblock
.fs_fpg
)/sblock
.fs_frag
);
1166 acg
.cg_cs
.cs_nbfree
--;
1167 sblock
.fs_cstotal
.cs_nbfree
--;
1168 cg_blktot(&acg
)[cbtocylno(&sblock
, d
%sblock
.fs_fpg
)]--;
1169 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
,
1170 d
%sblock
.fs_fpg
))[cbtorpos(&sblock
,
1171 d
%sblock
.fs_fpg
)]--;
1172 if(sblock
.fs_contigsumsize
> 0) {
1173 clrbit(cg_clustersfree(&acg
),
1174 (d
%sblock
.fs_fpg
)/sblock
.fs_frag
);
1179 * Allocate all fragments used by the cylinder summary in the
1182 if(d
<sblock
.fs_csaddr
+(sblock
.fs_cssize
/sblock
.fs_fsize
)) {
1183 for(; d
-sblock
.fs_csaddr
<
1184 sblock
.fs_cssize
/sblock
.fs_fsize
;
1186 clrbit(cg_blksfree(&acg
), d
%sblock
.fs_fpg
);
1187 acg
.cg_cs
.cs_nffree
--;
1188 sblock
.fs_cstotal
.cs_nffree
--;
1190 acg
.cg_cs
.cs_nbfree
--;
1191 acg
.cg_cs
.cs_nffree
+=sblock
.fs_frag
;
1192 sblock
.fs_cstotal
.cs_nbfree
--;
1193 sblock
.fs_cstotal
.cs_nffree
+=sblock
.fs_frag
;
1194 cg_blktot(&acg
)[cbtocylno(&sblock
, d
%sblock
.fs_fpg
)]--;
1195 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
,
1196 d
%sblock
.fs_fpg
))[cbtorpos(&sblock
,
1197 d
%sblock
.fs_fpg
)]--;
1198 if(sblock
.fs_contigsumsize
> 0) {
1199 clrbit(cg_clustersfree(&acg
),
1200 (d
%sblock
.fs_fpg
)/sblock
.fs_frag
);
1203 frag_adjust(d
%sblock
.fs_fpg
, +1);
1206 * XXX Handle the cluster statistics here in the case this
1207 * cylinder group is now almost full, and the remaining
1208 * space is less then the maximum cluster size. This is
1209 * probably not needed, as you would hardly find a file
1210 * system which has only MAXCSBUFS+FS_MAXCONTIG of free
1211 * space right behind the cylinder group information in
1212 * any new cylinder group.
1216 * Update our statistics in the cylinder summary.
1221 * Write the new cylinder group containing the cylinder summary
1224 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, ncscg
)),
1225 (size_t)sblock
.fs_cgsize
, (void *)&acg
, fso
, Nflag
);
1226 DBG_PRINT0("nscg written\n");
1227 DBG_DUMP_CG(&sblock
,
1235 * We have got enough of space in the current cylinder group, so we
1236 * can relocate just a few blocks, and let the summary information
1237 * grow in place where it is right now.
1241 cbase
= cgbase(&osblock
, ocscg
); /* old and new are equal */
1242 dupper
= sblock
.fs_csaddr
- cbase
+
1243 howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
1244 odupper
= osblock
.fs_csaddr
- cbase
+
1245 howmany(osblock
.fs_cssize
, osblock
.fs_fsize
);
1247 sblock
.fs_dsize
-= dupper
-odupper
;
1250 * Allocate the space for the array of blocks to be relocated.
1252 bp
=(struct gfs_bpp
*)malloc(((dupper
-odupper
)/sblock
.fs_frag
+2)*
1253 sizeof(struct gfs_bpp
));
1255 errx(1, "malloc failed");
1257 memset((char *)bp
, 0, ((dupper
-odupper
)/sblock
.fs_frag
+2)*
1258 sizeof(struct gfs_bpp
));
1261 * Lock all new frags needed for the cylinder group summary. This is
1262 * done per fragment in the first and last block of the new required
1263 * area, and per block for all other blocks.
1265 * Handle the first new block here (but only if some fragments where
1266 * already used for the cylinder summary).
1269 frag_adjust(odupper
, -1);
1270 for(d
=odupper
; ((d
<dupper
)&&(d
%sblock
.fs_frag
)); d
++) {
1271 DBG_PRINT1("scg first frag check loop d=%d\n",
1273 if(isclr(cg_blksfree(&acg
), d
)) {
1275 bp
[ind
].old
=d
/sblock
.fs_frag
;
1276 bp
[ind
].flags
|=GFS_FL_FIRST
;
1277 if(roundup(d
, sblock
.fs_frag
) >= dupper
) {
1278 bp
[ind
].flags
|=GFS_FL_LAST
;
1283 clrbit(cg_blksfree(&acg
), d
);
1284 acg
.cg_cs
.cs_nffree
--;
1285 sblock
.fs_cstotal
.cs_nffree
--;
1288 * No cluster handling is needed here, as there was at least
1289 * one fragment in use by the cylinder summary in the old
1291 * No block-free counter handling here as this block was not
1295 frag_adjust(odupper
, 1);
1298 * Handle all needed complete blocks here.
1300 for(; d
+sblock
.fs_frag
<=dupper
; d
+=sblock
.fs_frag
) {
1301 DBG_PRINT1("scg block check loop d=%d\n",
1303 if(!isblock(&sblock
, cg_blksfree(&acg
), d
/sblock
.fs_frag
)) {
1304 for(f
=d
; f
<d
+sblock
.fs_frag
; f
++) {
1305 if(isset(cg_blksfree(&aocg
), f
)) {
1306 acg
.cg_cs
.cs_nffree
--;
1307 sblock
.fs_cstotal
.cs_nffree
--;
1310 clrblock(&sblock
, cg_blksfree(&acg
), d
/sblock
.fs_frag
);
1311 bp
[ind
].old
=d
/sblock
.fs_frag
;
1314 clrblock(&sblock
, cg_blksfree(&acg
), d
/sblock
.fs_frag
);
1315 acg
.cg_cs
.cs_nbfree
--;
1316 sblock
.fs_cstotal
.cs_nbfree
--;
1317 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]--;
1318 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
1319 [cbtorpos(&sblock
, d
)]--;
1320 if(sblock
.fs_contigsumsize
> 0) {
1321 clrbit(cg_clustersfree(&acg
), d
/sblock
.fs_frag
);
1322 for(lcs
=0, l
=(d
/sblock
.fs_frag
)+1;
1323 lcs
<sblock
.fs_contigsumsize
;
1325 if(isclr(cg_clustersfree(&acg
),l
)){
1329 if(lcs
< sblock
.fs_contigsumsize
) {
1330 cg_clustersum(&acg
)[lcs
+1]--;
1332 cg_clustersum(&acg
)[lcs
]++;
1338 * No fragment counter handling is needed here, as this finally
1339 * doesn't change after the relocation.
1344 * Handle all fragments needed in the last new affected block.
1347 frag_adjust(dupper
-1, -1);
1349 if(isblock(&sblock
, cg_blksfree(&acg
), d
/sblock
.fs_frag
)) {
1350 acg
.cg_cs
.cs_nbfree
--;
1351 sblock
.fs_cstotal
.cs_nbfree
--;
1352 acg
.cg_cs
.cs_nffree
+=sblock
.fs_frag
;
1353 sblock
.fs_cstotal
.cs_nffree
+=sblock
.fs_frag
;
1354 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]--;
1355 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
1356 [cbtorpos(&sblock
, d
)]--;
1357 if(sblock
.fs_contigsumsize
> 0) {
1358 clrbit(cg_clustersfree(&acg
), d
/sblock
.fs_frag
);
1359 for(lcs
=0, l
=(d
/sblock
.fs_frag
)+1;
1360 lcs
<sblock
.fs_contigsumsize
;
1362 if(isclr(cg_clustersfree(&acg
),l
)){
1366 if(lcs
< sblock
.fs_contigsumsize
) {
1367 cg_clustersum(&acg
)[lcs
+1]--;
1369 cg_clustersum(&acg
)[lcs
]++;
1375 for(; d
<dupper
; d
++) {
1376 DBG_PRINT1("scg second frag check loop d=%d\n",
1378 if(isclr(cg_blksfree(&acg
), d
)) {
1379 bp
[ind
].old
=d
/sblock
.fs_frag
;
1380 bp
[ind
].flags
|=GFS_FL_LAST
;
1382 clrbit(cg_blksfree(&acg
), d
);
1383 acg
.cg_cs
.cs_nffree
--;
1384 sblock
.fs_cstotal
.cs_nffree
--;
1387 if(bp
[ind
].flags
& GFS_FL_LAST
) { /* we have to advance here */
1390 frag_adjust(dupper
-1, 1);
1394 * If we found a block to relocate just do so.
1397 for(i
=0; i
<ind
; i
++) {
1398 if(!bp
[i
].old
) { /* no more blocks listed */
1400 * XXX A relative blocknumber should not be
1401 * zero, which is not explicitly
1402 * guaranteed by our code.
1407 * Allocate a complete block in the same (current)
1410 bp
[i
].new=alloc()/sblock
.fs_frag
;
1413 * There is no frag_adjust() needed for the new block
1414 * as it will have no fragments yet :-).
1416 for(f
=bp
[i
].old
*sblock
.fs_frag
,
1417 g
=bp
[i
].new*sblock
.fs_frag
;
1418 f
<(bp
[i
].old
+1)*sblock
.fs_frag
;
1420 if(isset(cg_blksfree(&aocg
), f
)) {
1421 setbit(cg_blksfree(&acg
), g
);
1422 acg
.cg_cs
.cs_nffree
++;
1423 sblock
.fs_cstotal
.cs_nffree
++;
1428 * Special handling is required if this was the first
1429 * block. We have to consider the fragments which were
1430 * used by the cylinder summary in the original block
1431 * which re to be free in the copy of our block. We
1432 * have to be careful if this first block happens to
1433 * be also the last block to be relocated.
1435 if(bp
[i
].flags
& GFS_FL_FIRST
) {
1436 for(f
=bp
[i
].old
*sblock
.fs_frag
,
1437 g
=bp
[i
].new*sblock
.fs_frag
;
1440 setbit(cg_blksfree(&acg
), g
);
1441 acg
.cg_cs
.cs_nffree
++;
1442 sblock
.fs_cstotal
.cs_nffree
++;
1444 if(!(bp
[i
].flags
& GFS_FL_LAST
)) {
1445 frag_adjust(bp
[i
].new*sblock
.fs_frag
,1);
1451 * Special handling is required if this is the last
1452 * block to be relocated.
1454 if(bp
[i
].flags
& GFS_FL_LAST
) {
1455 frag_adjust(bp
[i
].new*sblock
.fs_frag
, 1);
1456 frag_adjust(bp
[i
].old
*sblock
.fs_frag
, -1);
1458 f
<roundup(dupper
, sblock
.fs_frag
);
1460 if(isclr(cg_blksfree(&acg
), f
)) {
1461 setbit(cg_blksfree(&acg
), f
);
1462 acg
.cg_cs
.cs_nffree
++;
1463 sblock
.fs_cstotal
.cs_nffree
++;
1466 frag_adjust(bp
[i
].old
*sblock
.fs_frag
, 1);
1470 * !!! Attach the cylindergroup offset here.
1472 bp
[i
].old
+=cbase
/sblock
.fs_frag
;
1473 bp
[i
].new+=cbase
/sblock
.fs_frag
;
1476 * Copy the content of the block.
1479 * XXX Here we will have to implement a copy on write
1480 * in the case we have any active snapshots.
1482 rdfs(fsbtodb(&sblock
, bp
[i
].old
*sblock
.fs_frag
),
1483 (size_t)sblock
.fs_bsize
, (void *)&ablk
, fsi
);
1484 wtfs(fsbtodb(&sblock
, bp
[i
].new*sblock
.fs_frag
),
1485 (size_t)sblock
.fs_bsize
, (void *)&ablk
, fso
, Nflag
);
1486 DBG_DUMP_HEX(&sblock
,
1487 "copied full block",
1488 (unsigned char *)&ablk
);
1490 DBG_PRINT2("scg (%d->%d) block relocated\n",
1496 * Now we have to update all references to any fragment which
1497 * belongs to any block relocated. We iterate now over all
1498 * cylinder groups, within those over all non zero length
1501 for(cylno
=0; cylno
<osblock
.fs_ncg
; cylno
++) {
1502 DBG_PRINT1("scg doing cg (%d)\n",
1504 for(inc
=osblock
.fs_ipg
-1 ; inc
>=0 ; inc
--) {
1505 updrefs(cylno
, (ino_t
)inc
, bp
, fsi
, fso
, Nflag
);
1510 * All inodes are checked, now make sure the number of
1511 * references found make sense.
1513 for(i
=0; i
<ind
; i
++) {
1514 if(!bp
[i
].found
|| (bp
[i
].found
>sblock
.fs_frag
)) {
1515 warnx("error: %d refs found for block %d.",
1516 bp
[i
].found
, bp
[i
].old
);
1522 * The following statistics are not changed here:
1523 * sblock.fs_cstotal.cs_ndir
1524 * sblock.fs_cstotal.cs_nifree
1525 * The following statistics were already updated on the fly:
1526 * sblock.fs_cstotal.cs_nffree
1527 * sblock.fs_cstotal.cs_nbfree
1528 * As the statistics for this cylinder group are ready, copy it to
1529 * the summary information array.
1535 * Write summary cylinder group back to disk.
1537 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, ocscg
)), (size_t)sblock
.fs_cgsize
,
1538 (void *)&acg
, fso
, Nflag
);
1539 DBG_PRINT0("scg written\n");
1540 DBG_DUMP_CG(&sblock
,
1548 /* ************************************************************** rdfs ***** */
1550 * Here we read some block(s) from disk.
1553 rdfs(daddr_t bno
, size_t size
, void *bf
, int fsi
)
1560 if (lseek(fsi
, (off_t
)bno
* DEV_BSIZE
, 0) < 0) {
1561 err(33, "rdfs: seek error: %ld", (long)bno
);
1563 n
= read(fsi
, bf
, size
);
1564 if (n
!= (ssize_t
)size
) {
1565 err(34, "rdfs: read error: %ld", (long)bno
);
1572 /* ************************************************************** wtfs ***** */
1574 * Here we write some block(s) to disk.
1577 wtfs(daddr_t bno
, size_t size
, void *bf
, int fso
, unsigned int Nflag
)
1588 if (lseek(fso
, (off_t
)bno
* DEV_BSIZE
, SEEK_SET
) < 0) {
1589 err(35, "wtfs: seek error: %ld", (long)bno
);
1591 n
= write(fso
, bf
, size
);
1592 if (n
!= (ssize_t
)size
) {
1593 err(36, "wtfs: write error: %ld", (long)bno
);
1600 /* ************************************************************* alloc ***** */
1602 * Here we allocate a free block in the current cylinder group. It is assumed,
1603 * that acg contains the current cylinder group. As we may take a block from
1604 * somewhere in the filesystem we have to handle cluster summary here.
1614 int dlower
, dupper
, dmax
;
1618 if (acg
.cg_magic
!= CG_MAGIC
) {
1619 warnx("acg: bad magic number");
1623 if (acg
.cg_cs
.cs_nbfree
== 0) {
1624 warnx("error: cylinder group ran out of space");
1629 * We start seeking for free blocks only from the space available after
1630 * the end of the new grown cylinder summary. Otherwise we allocate a
1631 * block here which we have to relocate a couple of seconds later again
1632 * again, and we are not prepared to to this anyway.
1635 dlower
=cgsblock(&sblock
, acg
.cg_cgx
)-cgbase(&sblock
, acg
.cg_cgx
);
1636 dupper
=cgdmin(&sblock
, acg
.cg_cgx
)-cgbase(&sblock
, acg
.cg_cgx
);
1637 dmax
=cgbase(&sblock
, acg
.cg_cgx
)+sblock
.fs_fpg
;
1638 if (dmax
> sblock
.fs_size
) {
1639 dmax
= sblock
.fs_size
;
1641 dmax
-=cgbase(&sblock
, acg
.cg_cgx
); /* retransform into cg */
1642 csmin
=sblock
.fs_csaddr
-cgbase(&sblock
, acg
.cg_cgx
);
1643 csmax
=csmin
+howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
1644 DBG_PRINT3("seek range: dl=%d, du=%d, dm=%d\n",
1648 DBG_PRINT2("range cont: csmin=%d, csmax=%d\n",
1652 for(d
=0; (d
<dlower
&& blkno
==-1); d
+=sblock
.fs_frag
) {
1653 if(d
>=csmin
&& d
<=csmax
) {
1656 if(isblock(&sblock
, cg_blksfree(&acg
), fragstoblks(&sblock
,
1658 blkno
= fragstoblks(&sblock
, d
);/* Yeah found a block */
1662 for(d
=dupper
; (d
<dmax
&& blkno
==-1); d
+=sblock
.fs_frag
) {
1663 if(d
>=csmin
&& d
<=csmax
) {
1666 if(isblock(&sblock
, cg_blksfree(&acg
), fragstoblks(&sblock
,
1668 blkno
= fragstoblks(&sblock
, d
);/* Yeah found a block */
1673 warnx("internal error: couldn't find promised block in cg");
1679 * This is needed if the block was found already in the first loop.
1681 d
=blkstofrags(&sblock
, blkno
);
1683 clrblock(&sblock
, cg_blksfree(&acg
), blkno
);
1684 if (sblock
.fs_contigsumsize
> 0) {
1686 * Handle the cluster allocation bitmap.
1688 clrbit(cg_clustersfree(&acg
), blkno
);
1690 * We possibly have split a cluster here, so we have to do
1691 * recalculate the sizes of the remaining cluster halves now,
1692 * and use them for updating the cluster summary information.
1694 * Lets start with the blocks before our allocated block ...
1696 for(lcs1
=0, l
=blkno
-1; lcs1
<sblock
.fs_contigsumsize
;
1698 if(isclr(cg_clustersfree(&acg
),l
)){
1703 * ... and continue with the blocks right after our allocated
1706 for(lcs2
=0, l
=blkno
+1; lcs2
<sblock
.fs_contigsumsize
;
1708 if(isclr(cg_clustersfree(&acg
),l
)){
1714 * Now update all counters.
1716 cg_clustersum(&acg
)[MIN(lcs1
+lcs2
+1,sblock
.fs_contigsumsize
)]--;
1718 cg_clustersum(&acg
)[lcs1
]++;
1721 cg_clustersum(&acg
)[lcs2
]++;
1725 * Update all statistics based on blocks.
1727 acg
.cg_cs
.cs_nbfree
--;
1728 sblock
.fs_cstotal
.cs_nbfree
--;
1729 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]--;
1730 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))[cbtorpos(&sblock
, d
)]--;
1736 /* *********************************************************** isblock ***** */
1738 * Here we check if all frags of a block are free. For more details again
1739 * please see the source of newfs(8), as this function is taken over almost
1743 isblock(struct fs
*fs
, unsigned char *cp
, int h
)
1750 switch (fs
->fs_frag
) {
1753 return (cp
[h
] == 0xff);
1755 mask
= 0x0f << ((h
& 0x1) << 2);
1757 return ((cp
[h
>> 1] & mask
) == mask
);
1759 mask
= 0x03 << ((h
& 0x3) << 1);
1761 return ((cp
[h
>> 2] & mask
) == mask
);
1763 mask
= 0x01 << (h
& 0x7);
1765 return ((cp
[h
>> 3] & mask
) == mask
);
1767 fprintf(stderr
, "isblock bad fs_frag %d\n", fs
->fs_frag
);
1773 /* ********************************************************** clrblock ***** */
1775 * Here we allocate a complete block in the block map. For more details again
1776 * please see the source of newfs(8), as this function is taken over almost
1780 clrblock(struct fs
*fs
, unsigned char *cp
, int h
)
1782 DBG_FUNC("clrblock")
1786 switch ((fs
)->fs_frag
) {
1791 cp
[h
>> 1] &= ~(0x0f << ((h
& 0x1) << 2));
1794 cp
[h
>> 2] &= ~(0x03 << ((h
& 0x3) << 1));
1797 cp
[h
>> 3] &= ~(0x01 << (h
& 0x7));
1800 warnx("clrblock bad fs_frag %d", fs
->fs_frag
);
1808 /* ********************************************************** setblock ***** */
1810 * Here we free a complete block in the free block map. For more details again
1811 * please see the source of newfs(8), as this function is taken over almost
1815 setblock(struct fs
*fs
, unsigned char *cp
, int h
)
1817 DBG_FUNC("setblock")
1821 switch (fs
->fs_frag
) {
1826 cp
[h
>> 1] |= (0x0f << ((h
& 0x1) << 2));
1829 cp
[h
>> 2] |= (0x03 << ((h
& 0x3) << 1));
1832 cp
[h
>> 3] |= (0x01 << (h
& 0x7));
1835 warnx("setblock bad fs_frag %d", fs
->fs_frag
);
1843 /* ************************************************************ ginode ***** */
1845 * This function provides access to an individual inode. We find out in which
1846 * block the requested inode is located, read it from disk if needed, and
1847 * return the pointer into that block. We maintain a cache of one block to
1848 * not read the same block again and again if we iterate linearly over all
1851 static struct dinode
*
1852 ginode(ino_t inumber
, int fsi
, int cg
)
1856 static ino_t startinum
=0; /* first inode in cached block */
1861 pi
=(struct dinode
*)(void *)ablk
;
1862 inumber
+=(cg
* sblock
.fs_ipg
);
1863 if (startinum
== 0 || inumber
< startinum
||
1864 inumber
>= startinum
+ INOPB(&sblock
)) {
1866 * The block needed is not cached, so we have to read it from
1869 iblk
= ino_to_fsba(&sblock
, inumber
);
1870 in_src
=fsbtodb(&sblock
, iblk
);
1871 rdfs(in_src
, (size_t)sblock
.fs_bsize
, (void *)&ablk
, fsi
);
1872 startinum
= (inumber
/ INOPB(&sblock
)) * INOPB(&sblock
);
1876 return (&(pi
[inumber
% INOPB(&sblock
)]));
1879 /* ****************************************************** charsperline ***** */
1881 * Figure out how many lines our current terminal has. For more details again
1882 * please see the source of newfs(8), as this function is taken over almost
1888 DBG_FUNC("charsperline")
1896 if (ioctl(0, TIOCGWINSZ
, &ws
) != -1) {
1897 columns
= ws
.ws_col
;
1899 if (columns
== 0 && (cp
= getenv("COLUMNS"))) {
1903 columns
= 80; /* last resort */
1910 /* ************************************************************** main ***** */
1912 * growfs(8) is a utility which allows to increase the size of an existing
1913 * ufs filesystem. Currently this can only be done on unmounted file system.
1914 * It recognizes some command line options to specify the new desired size,
1915 * and it does some basic checkings. The old file system size is determined
1916 * and after some more checks like we can really access the new last block
1917 * on the disk etc. we calculate the new parameters for the superblock. After
1918 * having done this we just call growfs() which will do the work. Before
1919 * we finish the only thing left is to update the disklabel.
1920 * We still have to provide support for snapshots. Therefore we first have to
1921 * understand what data structures are always replicated in the snapshot on
1922 * creation, for all other blocks we touch during our procedure, we have to
1923 * keep the old blocks unchanged somewhere available for the snapshots. If we
1924 * are lucky, then we only have to handle our blocks to be relocated in that
1926 * Also we have to consider in what order we actually update the critical
1927 * data structures of the filesystem to make sure, that in case of a disaster
1928 * fsck(8) is still able to restore any lost data.
1929 * The foreseen last step then will be to provide for growing even mounted
1930 * file systems. There we have to extend the mount() system call to provide
1931 * userland access to the file system locking facility.
1934 main(int argc
, char **argv
)
1937 char *device
, *special
, *cp
;
1939 unsigned int size
=0;
1941 unsigned int Nflag
=0;
1944 struct disklabel
*lp
;
1945 struct partition
*pp
;
1950 #endif /* FSMAXSNAP */
1954 while((ch
=getopt(argc
, argv
, "Ns:vy")) != -1) {
1960 size
=(size_t)atol(optarg
);
1965 case 'v': /* for compatibility to newfs */
1985 * Now try to guess the (raw)device name.
1987 if (0 == strrchr(device
, '/')) {
1989 * No path prefix was given, so try in that order:
1995 * FreeBSD now doesn't distinguish between raw and block
1996 * devices any longer, but it should still work this way.
1998 len
=strlen(device
)+strlen(_PATH_DEV
)+2+strlen("vinum/");
1999 special
=(char *)malloc(len
);
2000 if(special
== NULL
) {
2001 errx(1, "malloc failed");
2003 snprintf(special
, len
, "%sr%s", _PATH_DEV
, device
);
2004 if (stat(special
, &st
) == -1) {
2005 snprintf(special
, len
, "%s%s", _PATH_DEV
, device
);
2006 if (stat(special
, &st
) == -1) {
2007 snprintf(special
, len
, "%svinum/r%s",
2009 if (stat(special
, &st
) == -1) {
2010 /* For now this is the 'last resort' */
2011 snprintf(special
, len
, "%svinum/%s",
2020 * Try to access our devices for writing ...
2025 fso
= open(device
, O_WRONLY
);
2027 err(1, "%s", device
);
2034 fsi
= open(device
, O_RDONLY
);
2036 err(1, "%s", device
);
2040 * Try to read a label and gess the slice if not specified. This
2041 * code should guess the right thing and avaid to bother the user
2042 * user with the task of specifying the option -v on vinum volumes.
2044 cp
=device
+strlen(device
)-1;
2045 lp
= get_disklabel(fsi
);
2046 if(lp
->d_type
== DTYPE_VINUM
) {
2047 pp
= &lp
->d_partitions
[0];
2048 } else if (isdigit(*cp
)) {
2049 pp
= &lp
->d_partitions
[2];
2050 } else if (*cp
>='a' && *cp
<='h') {
2051 pp
= &lp
->d_partitions
[*cp
- 'a'];
2053 errx(1, "unknown device");
2057 * Check if that partition looks suited for growing a file system.
2059 if (pp
->p_size
< 1) {
2060 errx(1, "partition is unavailable");
2062 if (pp
->p_fstype
!= FS_BSDFFS
) {
2063 errx(1, "partition not 4.2BSD");
2067 * Read the current superblock, and take a backup.
2069 rdfs((daddr_t
)(SBOFF
/DEV_BSIZE
), (size_t)SBSIZE
, (void *)&(osblock
),
2071 if (osblock
.fs_magic
!= FS_MAGIC
) {
2072 errx(1, "superblock not recognized");
2074 memcpy((void *)&fsun1
, (void *)&fsun2
, sizeof(fsun2
));
2076 DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
2077 DBG_DUMP_FS(&sblock
,
2081 * Determine size to grow to. Default to the full size specified in
2084 sblock
.fs_size
= dbtofsb(&osblock
, pp
->p_size
);
2086 if (size
> pp
->p_size
){
2087 errx(1, "There is not enough space (%d < %d)",
2090 sblock
.fs_size
= dbtofsb(&osblock
, size
);
2094 * Are we really growing ?
2096 if(osblock
.fs_size
>= sblock
.fs_size
) {
2097 errx(1, "we are not growing (%d->%d)", osblock
.fs_size
,
2104 * Check if we find an active snapshot.
2106 if(ExpertFlag
== 0) {
2107 for(j
=0; j
<FSMAXSNAP
; j
++) {
2108 if(sblock
.fs_snapinum
[j
]) {
2109 errx(1, "active snapshot found in filesystem\n"
2110 " please remove all snapshots before "
2113 if(!sblock
.fs_snapinum
[j
]) { /* list is dense */
2120 if (ExpertFlag
== 0 && Nflag
== 0) {
2121 printf("We strongly recommend you to make a backup "
2122 "before growing the Filesystem\n\n"
2123 " Did you backup your data (Yes/No) ? ");
2124 fgets(reply
, (int)sizeof(reply
), stdin
);
2125 if (strcmp(reply
, "Yes\n")){
2126 printf("\n Nothing done \n");
2131 printf("new filesystemsize is: %d frags\n", sblock
.fs_size
);
2134 * Try to access our new last block in the filesystem. Even if we
2135 * later on realize we have to abort our operation, on that block
2136 * there should be no data, so we can't destroy something yet.
2138 wtfs((daddr_t
)pp
->p_size
-1, (size_t)DEV_BSIZE
, (void *)&sblock
, fso
,
2142 * Now calculate new superblock values and check for reasonable
2143 * bound for new file system size:
2144 * fs_size: is derived from label or user input
2145 * fs_dsize: should get updated in the routines creating or
2146 * updating the cylinder groups on the fly
2147 * fs_cstotal: should get updated in the routines creating or
2148 * updating the cylinder groups
2152 * Update the number of cylinders in the filesystem.
2154 sblock
.fs_ncyl
= sblock
.fs_size
* NSPF(&sblock
) / sblock
.fs_spc
;
2155 if (sblock
.fs_size
* NSPF(&sblock
) > sblock
.fs_ncyl
* sblock
.fs_spc
) {
2160 * Update the number of cylinder groups in the filesystem.
2162 sblock
.fs_ncg
= sblock
.fs_ncyl
/ sblock
.fs_cpg
;
2163 if (sblock
.fs_ncyl
% sblock
.fs_cpg
) {
2167 if ((sblock
.fs_size
- (sblock
.fs_ncg
-1) * sblock
.fs_fpg
) <
2168 sblock
.fs_fpg
&& cgdmin(&sblock
, (sblock
.fs_ncg
-1))-
2169 cgbase(&sblock
, (sblock
.fs_ncg
-1)) > (sblock
.fs_size
-
2170 (sblock
.fs_ncg
-1) * sblock
.fs_fpg
)) {
2172 * The space in the new last cylinder group is too small,
2176 #if 1 /* this is a bit more safe */
2177 sblock
.fs_ncyl
= sblock
.fs_ncg
* sblock
.fs_cpg
;
2179 sblock
.fs_ncyl
-= sblock
.fs_ncyl
% sblock
.fs_cpg
;
2181 sblock
.fs_ncyl
-= sblock
.fs_ncyl
% sblock
.fs_cpg
;
2182 printf( "Warning: %d sector(s) cannot be allocated.\n",
2183 (sblock
.fs_size
-(sblock
.fs_ncg
)*sblock
.fs_fpg
) *
2185 sblock
.fs_size
= sblock
.fs_ncyl
* sblock
.fs_spc
/ NSPF(&sblock
);
2189 * Update the space for the cylinder group summary information in the
2190 * respective cylinder group data area.
2193 fragroundup(&sblock
, sblock
.fs_ncg
* sizeof(struct csum
));
2195 if(osblock
.fs_size
>= sblock
.fs_size
) {
2196 errx(1, "not enough new space");
2199 DBG_PRINT0("sblock calculated\n");
2202 * Ok, everything prepared, so now let's do the tricks.
2204 growfs(fsi
, fso
, Nflag
);
2207 * Update the disk label.
2209 pp
->p_fsize
= sblock
.fs_fsize
;
2210 pp
->p_frag
= sblock
.fs_frag
;
2211 pp
->p_cpg
= sblock
.fs_cpg
;
2213 return_disklabel(fso
, lp
, Nflag
);
2214 DBG_PRINT0("label rewritten\n");
2217 if(fso
>-1) close(fso
);
2225 /* ************************************************** return_disklabel ***** */
2227 * Write the updated disklabel back to disk.
2230 return_disklabel(int fd
, struct disklabel
*lp
, unsigned int Nflag
)
2232 DBG_FUNC("return_disklabel")
2248 * recalculate checksum
2250 while(ptr
< (u_short
*)&lp
->d_partitions
[lp
->d_npartitions
]) {
2255 if (ioctl(fd
, DIOCWDINFO
, (char *)lp
) < 0) {
2256 errx(1, "DIOCWDINFO failed");
2265 /* ***************************************************** get_disklabel ***** */
2267 * Read the disklabel from disk.
2269 static struct disklabel
*
2270 get_disklabel(int fd
)
2272 DBG_FUNC("get_disklabel")
2273 static struct disklabel
*lab
;
2277 lab
=(struct disklabel
*)malloc(sizeof(struct disklabel
));
2279 errx(1, "malloc failed");
2281 if (ioctl(fd
, DIOCGDINFO
, (char *)lab
) < 0) {
2282 errx(1, "DIOCGDINFO failed");
2290 /* ************************************************************* usage ***** */
2292 * Dump a line of usage.
2301 fprintf(stderr
, "usage: growfs [-Ny] [-s size] special\n");
2307 /* *********************************************************** updclst ***** */
2309 * This updates most paramters and the bitmap related to cluster. We have to
2310 * assume, that sblock, osblock, acg are set up.
2320 if(sblock
.fs_contigsumsize
< 1) { /* no clustering */
2324 * update cluster allocation map
2326 setbit(cg_clustersfree(&acg
), block
);
2329 * update cluster summary table
2333 * calculate size for the trailing cluster
2335 for(block
--; lcs
<sblock
.fs_contigsumsize
; block
--, lcs
++ ) {
2336 if(isclr(cg_clustersfree(&acg
), block
)){
2341 if(lcs
< sblock
.fs_contigsumsize
) {
2343 cg_clustersum(&acg
)[lcs
]--;
2346 cg_clustersum(&acg
)[lcs
]++;
2353 /* *********************************************************** updrefs ***** */
2355 * This updates all references to relocated blocks for the given inode. The
2356 * inode is given as number within the cylinder group, and the number of the
2360 updrefs(int cg
, ino_t in
, struct gfs_bpp
*bp
, int fsi
, int fso
, unsigned int
2364 unsigned int ictr
, ind2ctr
, ind3ctr
;
2365 ufs_daddr_t
*iptr
, *ind2ptr
, *ind3ptr
;
2367 int remaining_blocks
;
2372 * XXX We should skip unused inodes even from beeing read from disk
2373 * here by using the bitmap.
2375 ino
=ginode(in
, fsi
, cg
);
2376 if(!((ino
->di_mode
& IFMT
)==IFDIR
|| (ino
->di_mode
& IFMT
)==IFREG
||
2377 (ino
->di_mode
& IFMT
)==IFLNK
)) {
2379 return; /* only check DIR, FILE, LINK */
2381 if(((ino
->di_mode
& IFMT
)==IFLNK
) && (ino
->di_size
<MAXSYMLINKLEN
)) {
2383 return; /* skip short symlinks */
2387 return; /* skip empty file */
2389 if(!ino
->di_blocks
) {
2391 return; /* skip empty swiss cheesy file or old fastlink */
2393 DBG_PRINT2("scg checking inode (%d in %d)\n",
2398 * Start checking all direct blocks.
2400 remaining_blocks
=howmany(ino
->di_size
, sblock
.fs_bsize
);
2401 for(ictr
=0; ictr
< MIN(NDADDR
, (unsigned int)remaining_blocks
);
2403 iptr
=&(ino
->di_db
[ictr
]);
2405 cond_bl_upd(iptr
, bp
, GFS_PS_INODE
, fso
, Nflag
);
2408 DBG_PRINT0("~~scg direct blocks checked\n");
2410 remaining_blocks
-=NDADDR
;
2411 if(remaining_blocks
<0) {
2417 * Start checking first indirect block
2419 cond_bl_upd(&(ino
->di_ib
[0]), bp
, GFS_PS_INODE
, fso
, Nflag
);
2420 i1_src
=fsbtodb(&sblock
, ino
->di_ib
[0]);
2421 rdfs(i1_src
, (size_t)sblock
.fs_bsize
, (void *)&i1blk
, fsi
);
2422 for(ictr
=0; ictr
< MIN(howmany(sblock
.fs_bsize
,
2423 sizeof(ufs_daddr_t
)), (unsigned int)remaining_blocks
);
2425 iptr
=&((ufs_daddr_t
*)(void *)&i1blk
)[ictr
];
2427 cond_bl_upd(iptr
, bp
, GFS_PS_IND_BLK_LVL1
,
2432 DBG_PRINT0("scg indirect_1 blocks checked\n");
2434 remaining_blocks
-= howmany(sblock
.fs_bsize
, sizeof(ufs_daddr_t
));
2435 if(remaining_blocks
<0) {
2441 * Start checking second indirect block
2443 cond_bl_upd(&(ino
->di_ib
[1]), bp
, GFS_PS_INODE
, fso
, Nflag
);
2444 i2_src
=fsbtodb(&sblock
, ino
->di_ib
[1]);
2445 rdfs(i2_src
, (size_t)sblock
.fs_bsize
, (void *)&i2blk
, fsi
);
2446 for(ind2ctr
=0; ind2ctr
< howmany(sblock
.fs_bsize
,
2447 sizeof(ufs_daddr_t
)); ind2ctr
++) {
2448 ind2ptr
=&((ufs_daddr_t
*)(void *)&i2blk
)[ind2ctr
];
2452 cond_bl_upd(ind2ptr
, bp
, GFS_PS_IND_BLK_LVL2
, fso
,
2454 i1_src
=fsbtodb(&sblock
, *ind2ptr
);
2455 rdfs(i1_src
, (size_t)sblock
.fs_bsize
, (void *)&i1blk
,
2457 for(ictr
=0; ictr
<MIN(howmany((unsigned int)
2458 sblock
.fs_bsize
, sizeof(ufs_daddr_t
)),
2459 (unsigned int)remaining_blocks
); ictr
++) {
2460 iptr
=&((ufs_daddr_t
*)(void *)&i1blk
)[ictr
];
2462 cond_bl_upd(iptr
, bp
,
2463 GFS_PS_IND_BLK_LVL1
, fso
, Nflag
);
2468 DBG_PRINT0("scg indirect_2 blocks checked\n");
2470 #define SQUARE(a) ((a)*(a))
2471 remaining_blocks
-=SQUARE(howmany(sblock
.fs_bsize
, sizeof(ufs_daddr_t
)));
2473 if(remaining_blocks
<0) {
2480 * Start checking third indirect block
2482 cond_bl_upd(&(ino
->di_ib
[2]), bp
, GFS_PS_INODE
, fso
, Nflag
);
2483 i3_src
=fsbtodb(&sblock
, ino
->di_ib
[2]);
2484 rdfs(i3_src
, (size_t)sblock
.fs_bsize
, (void *)&i3blk
, fsi
);
2485 for(ind3ctr
=0; ind3ctr
< howmany(sblock
.fs_bsize
,
2486 sizeof(ufs_daddr_t
)); ind3ctr
++) {
2487 ind3ptr
=&((ufs_daddr_t
*)(void *)&i3blk
)[ind3ctr
];
2491 cond_bl_upd(ind3ptr
, bp
, GFS_PS_IND_BLK_LVL3
, fso
,
2493 i2_src
=fsbtodb(&sblock
, *ind3ptr
);
2494 rdfs(i2_src
, (size_t)sblock
.fs_bsize
, (void *)&i2blk
,
2496 for(ind2ctr
=0; ind2ctr
< howmany(sblock
.fs_bsize
,
2497 sizeof(ufs_daddr_t
)); ind2ctr
++) {
2498 ind2ptr
=&((ufs_daddr_t
*)(void *)&i2blk
)
2503 cond_bl_upd(ind2ptr
, bp
, GFS_PS_IND_BLK_LVL2
,
2505 i1_src
=fsbtodb(&sblock
, *ind2ptr
);
2506 rdfs(i1_src
, (size_t)sblock
.fs_bsize
,
2507 (void *)&i1blk
, fsi
);
2508 for(ictr
=0; ictr
< MIN(howmany(sblock
.fs_bsize
,
2509 sizeof(ufs_daddr_t
)),
2510 (unsigned int)remaining_blocks
); ictr
++) {
2511 iptr
=&((ufs_daddr_t
*)(void *)&i1blk
)
2514 cond_bl_upd(iptr
, bp
,
2515 GFS_PS_IND_BLK_LVL1
, fso
,
2523 DBG_PRINT0("scg indirect_3 blocks checked\n");