xref: /dragonfly/sbin/growfs/growfs.c (revision 49781055)
1 /*
2  * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
3  * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgment:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors, as well as Christoph
21  *      Herrmann and Thomas-Henning von Kamptz.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $
39  *
40  * @(#) 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 $
43  */
44 
45 /* ********************************************************** INCLUDES ***** */
46 #include <sys/param.h>
47 #include <sys/disklabel.h>
48 #include <sys/ioctl.h>
49 #include <sys/stat.h>
50 
51 #include <stdio.h>
52 #include <paths.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <fcntl.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <vfs/ufs/dinode.h>
60 #include <vfs/ufs/fs.h>
61 
62 #include "debug.h"
63 
64 /* *************************************************** GLOBALS & TYPES ***** */
65 #ifdef FS_DEBUG
66 int	_dbg_lvl_ = (DL_INFO);	/* DL_TRC */
67 #endif /* FS_DEBUG */
68 
69 static union {
70 	struct fs	fs;
71 	char	pad[SBSIZE];
72 } fsun1, fsun2;
73 #define	sblock	fsun1.fs	/* the new superblock */
74 #define	osblock	fsun2.fs	/* the old superblock */
75 
76 static union {
77 	struct cg	cg;
78 	char	pad[MAXBSIZE];
79 } cgun1, cgun2;
80 #define	acg	cgun1.cg	/* a cylinder cgroup (new) */
81 #define	aocg	cgun2.cg	/* an old cylinder group */
82 
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];
87 
88 	/* where to write back updated blocks */
89 static daddr_t	in_src, i1_src, i2_src, i3_src;
90 
91 	/* what object contains the reference */
92 enum pointer_source {
93 	GFS_PS_INODE,
94 	GFS_PS_IND_BLK_LVL1,
95 	GFS_PS_IND_BLK_LVL2,
96 	GFS_PS_IND_BLK_LVL3
97 };
98 
99 static struct csum	*fscs;		/* cylinder summary */
100 
101 static struct dinode	zino[MAXBSIZE/sizeof(struct dinode)]; /* some inodes */
102 
103 /*
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.
107  */
108 struct gfs_bpp {
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 */
115 };
116 
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);
138 
139 /* ************************************************************ growfs ***** */
140 /*
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
149  * copies.
150  */
151 static void
152 growfs(int fsi, int fso, unsigned int Nflag)
153 {
154 	DBG_FUNC("growfs")
155 	int	i;
156 	int	cylno, j;
157 	time_t	utime;
158 	int	width;
159 	char	tmpbuf[100];
160 #ifdef FSIRAND
161 	static int	randinit=0;
162 
163 	DBG_ENTER;
164 
165 	if (!randinit) {
166 		randinit = 1;
167 		srandomdev();
168 	}
169 #else /* not FSIRAND */
170 
171 	DBG_ENTER;
172 
173 #endif /* FSIRAND */
174 	time(&utime);
175 
176 	/*
177 	 * Get the cylinder summary into the memory.
178 	 */
179 	fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize);
180 	if(fscs == NULL) {
181 		errx(1, "calloc failed");
182 	}
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);
187 	}
188 
189 #ifdef FS_DEBUG
190 {
191 	struct csum	*dbg_csp;
192 	int	dbg_csc;
193 	char	dbg_line[80];
194 
195 	dbg_csp=fscs;
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,
200 		    dbg_line,
201 		    dbg_csp++);
202 	}
203 }
204 #endif /* FS_DEBUG */
205 	DBG_PRINT0("fscs read\n");
206 
207 	/*
208 	 * Do all needed changes in the former last cylinder group.
209 	 */
210 	updjcg(osblock.fs_ncg-1, utime, fsi, fso, Nflag);
211 
212 	/*
213 	 * Dump out summary information about file system.
214 	 */
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,
223 	    sblock.fs_ipg);
224 #undef B2MBFACTOR
225 
226 	/*
227 	 * Now build the cylinders group blocks and
228 	 * then print out indices of cylinder groups.
229 	 */
230 	printf("super-block backups (for fsck -b #) at:\n");
231 	i = 0;
232 	width = charsperline();
233 
234 	/*
235 	 * Iterate for only the new cylinder groups.
236 	 */
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) {
243 			printf("\n");
244 			i = 0;
245 		}
246 		i += j;
247 		printf("%s", tmpbuf);
248 		fflush(stdout);
249 	}
250 	printf("\n");
251 
252 	/*
253 	 * Do all needed changes in the first cylinder group.
254 	 * allocate blocks in new location
255 	 */
256 	updcsloc(utime, fsi, fso, Nflag);
257 
258 	/*
259 	 * Now write the cylinder summary back to disk.
260 	 */
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);
265 	}
266 	DBG_PRINT0("fscs written\n");
267 
268 #ifdef FS_DEBUG
269 {
270 	struct csum	*dbg_csp;
271 	int	dbg_csc;
272 	char	dbg_line[80];
273 
274 	dbg_csp=fscs;
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,
279 		    dbg_line,
280 		    dbg_csp++);
281 	}
282 }
283 #endif /* FS_DEBUG */
284 
285 	/*
286 	 * Now write the new superblock back to disk.
287 	 */
288 	sblock.fs_time = utime;
289 	wtfs((daddr_t)(SBOFF / DEV_BSIZE), (size_t)SBSIZE, (void *)&sblock,
290 	    fso, Nflag);
291 	DBG_PRINT0("sblock written\n");
292 	DBG_DUMP_FS(&sblock,
293 	    "new initial sblock");
294 
295 	/*
296 	 * Clean up the dynamic fields in our superblock copies.
297 	 */
298 	sblock.fs_fmod = 0;
299 	sblock.fs_clean = 1;
300 	sblock.fs_ronly = 0;
301 	sblock.fs_cgrotor = 0;
302 	sblock.fs_state = 0;
303 	memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
304 	sblock.fs_flags &= FS_DOSOFTDEP;
305 
306 	/*
307 	 * XXX
308 	 * The following fields are currently distributed from the  superblock
309 	 * to the copies:
310 	 *     fs_minfree
311 	 *     fs_rotdelay
312 	 *     fs_maxcontig
313 	 *     fs_maxbpg
314 	 *     fs_minfree,
315 	 *     fs_optim
316 	 *     fs_flags regarding SOFTPDATES
317 	 *
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.
325 	 */
326 
327 	/*
328 	 * Write out the duplicate super blocks.
329 	 */
330 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
331 		wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
332 		    (size_t)SBSIZE, (void *)&sblock, fso, Nflag);
333 	}
334 	DBG_PRINT0("sblock copies written\n");
335 	DBG_DUMP_FS(&sblock,
336 	    "new other sblocks");
337 
338 	DBG_LEAVE;
339 	return;
340 }
341 
342 /* ************************************************************ initcg ***** */
343 /*
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.
348  */
349 static void
350 initcg(int cylno, time_t utime, int fso, unsigned int Nflag)
351 {
352 	DBG_FUNC("initcg")
353 	daddr_t cbase, d, dlower, dupper, dmax, blkno;
354 	int i;
355 	struct csum *cs;
356 #ifdef FSIRAND
357 	int j;
358 #endif
359 
360 	DBG_ENTER;
361 
362 	/*
363 	 * Determine block bounds for cylinder group.
364 	 */
365 	cbase = cgbase(&sblock, cylno);
366 	dmax = cbase + sblock.fs_fpg;
367 	if (dmax > sblock.fs_size) {
368 		dmax = sblock.fs_size;
369 	}
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);
374 	}
375 	cs = fscs + cylno;
376 	memset(&acg, 0, (size_t)sblock.fs_cgsize);
377 	acg.cg_time = utime;
378 	acg.cg_magic = CG_MAGIC;
379 	acg.cg_cgx = cylno;
380 	if (cylno == sblock.fs_ncg - 1) {
381 		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
382 	} else {
383 		acg.cg_ncyl = sblock.fs_cpg;
384 	}
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;
389 	}
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);
398 	} else {
399 		acg.cg_clustersumoff = acg.cg_freeoff + howmany
400 		    (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
401 		    sizeof(u_int32_t);
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);
408 	}
409 	if (acg.cg_nextfreeoff-(int)(&acg.cg_firstfield) > sblock.fs_cgsize) {
410 		/*
411 		 * XXX This should never happen as we would have had that panic
412 		 *     already on filesystem creation
413 		 */
414 		errx(37, "panic: cylinder group too big");
415 	}
416 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
417 	if (cylno == 0)
418 		for (i = 0; (size_t)i < ROOTINO; i++) {
419 			setbit(cg_inosused(&acg), i);
420 			acg.cg_cs.cs_nifree--;
421 		}
422 	for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) {
423 #ifdef FSIRAND
424 		for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++) {
425 			zino[j].di_gen = random();
426 		}
427 #endif
428 		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
429 		    (size_t)sblock.fs_bsize, (void *)zino, fso, Nflag);
430 	}
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);
436 		}
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)]++;
441 	}
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++;
449 		}
450 	}
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);
456 		}
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)]++;
461 		d += sblock.fs_frag;
462 	}
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++;
468 		}
469 	}
470 	if (sblock.fs_contigsumsize > 0) {
471 		int32_t	*sump = cg_clustersum(&acg);
472 		u_char	*mapp = cg_clustersfree(&acg);
473 		int	map = *mapp++;
474 		int	bit = 1;
475 		int	run = 0;
476 
477 		for (i = 0; i < acg.cg_nclusterblks; i++) {
478 			if ((map & bit) != 0) {
479 				run++;
480 			} else if (run != 0) {
481 				if (run > sblock.fs_contigsumsize) {
482 					run = sblock.fs_contigsumsize;
483 				}
484 				sump[run]++;
485 				run = 0;
486 			}
487 			if ((i & (NBBY - 1)) != (NBBY - 1)) {
488 				bit <<= 1;
489 			} else {
490 				map = *mapp++;
491 				bit = 1;
492 			}
493 		}
494 		if (run != 0) {
495 			if (run > sblock.fs_contigsumsize) {
496 				run = sblock.fs_contigsumsize;
497 			}
498 			sump[run]++;
499 		}
500 	}
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;
505 	*cs = acg.cg_cs;
506 	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
507 	    (size_t)sblock.fs_bsize, (void *)&acg, fso, Nflag);
508 	DBG_DUMP_CG(&sblock,
509 	    "new cg",
510 	    &acg);
511 
512 	DBG_LEAVE;
513 	return;
514 }
515 
516 /* ******************************************************* frag_adjust ***** */
517 /*
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.
522  */
523 static void
524 frag_adjust(daddr_t frag, int sign)
525 {
526 	DBG_FUNC("frag_adjust")
527 	int fragsize;
528 	int f;
529 
530 	DBG_ENTER;
531 
532 	fragsize=0;
533 	/*
534 	 * Here frag only needs to point to any fragment in the block we want
535 	 * to examine.
536 	 */
537 	for(f=rounddown(frag, sblock.fs_frag);
538 	    f<roundup(frag+1, sblock.fs_frag);
539 	    f++) {
540 		/*
541 		 * Count contiguos free fragments.
542 		 */
543 		if(isset(cg_blksfree(&acg), f)) {
544 			fragsize++;
545 		} else {
546 			if(fragsize && fragsize<sblock.fs_frag) {
547 				/*
548 				 * We found something in between.
549 				 */
550 				acg.cg_frsum[fragsize]+=sign;
551 				DBG_PRINT2("frag_adjust [%d]+=%d\n",
552 				    fragsize,
553 				    sign);
554 			}
555 			fragsize=0;
556 		}
557 	}
558 	if(fragsize && fragsize<sblock.fs_frag) {
559 		/*
560 		 * We found something.
561 		 */
562 		acg.cg_frsum[fragsize]+=sign;
563 		DBG_PRINT2("frag_adjust [%d]+=%d\n",
564 		    fragsize,
565 		    sign);
566 	}
567 	DBG_PRINT2("frag_adjust [[%d]]+=%d\n",
568 	    fragsize,
569 	    sign);
570 
571 	DBG_LEAVE;
572 	return;
573 }
574 
575 /* ******************************************************* cond_bl_upd ***** */
576 /*
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.
583  */
584 static void
585 cond_bl_upd(ufs_daddr_t *block, struct gfs_bpp *field,
586     enum pointer_source source, int fso, unsigned int Nflag)
587 {
588 	DBG_FUNC("cond_bl_upd")
589 	struct gfs_bpp	*f;
590 	char *src;
591 	daddr_t dst=0;
592 
593 	DBG_ENTER;
594 
595 	f=field;
596 	while(f->old) { /* for all old blocks */
597 		if(*block/sblock.fs_frag == f->old) {
598 			/*
599 			 * The fragment is part of the block, so update.
600 			 */
601 			*block=(f->new*sblock.fs_frag+(*block%sblock.fs_frag));
602 			f->found++;
603 			DBG_PRINT3("scg (%d->%d)[%d] reference updated\n",
604 			    f->old,
605 			    f->new,
606 			    *block%sblock.fs_frag);
607 
608 			/* Write the block back to disk immediately */
609 			switch (source) {
610 			case GFS_PS_INODE:
611 				src=ablk;
612 				dst=in_src;
613 				break;
614 			case GFS_PS_IND_BLK_LVL1:
615 				src=i1blk;
616 				dst=i1_src;
617 				break;
618 			case GFS_PS_IND_BLK_LVL2:
619 				src=i2blk;
620 				dst=i2_src;
621 				break;
622 			case GFS_PS_IND_BLK_LVL3:
623 				src=i3blk;
624 				dst=i3_src;
625 				break;
626 			default:	/* error */
627 				src=NULL;
628 				break;
629 			}
630 			if(src) {
631 				/*
632 				 * XXX	If src is not of type inode we have to
633 				 *	implement  copy on write here in  case
634 				 *	of active snapshots.
635 				 */
636 				wtfs(dst, (size_t)sblock.fs_bsize, (void *)src,
637 				    fso, Nflag);
638 			}
639 
640 			/*
641 			 * The same block can't be found again in this loop.
642 			 */
643 			break;
644 		}
645 		f++;
646 	}
647 
648 	DBG_LEAVE;
649 	return;
650 }
651 
652 /* ************************************************************ updjcg ***** */
653 /*
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.
663  */
664 static void
665 updjcg(int cylno, time_t utime, int fsi, int fso, unsigned int Nflag)
666 {
667 	DBG_FUNC("updjcg")
668 	daddr_t	cbase, dmax, dupper;
669 	struct csum	*cs;
670 	int	i,k;
671 	int	j=0;
672 
673 	DBG_ENTER;
674 
675 	/*
676 	 * Read the former last (joining) cylinder group from disk, and make
677 	 * a copy.
678 	 */
679 	rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
680 	    (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
681 	DBG_PRINT0("jcg read\n");
682 	DBG_DUMP_CG(&sblock,
683 	    "old joining cg",
684 	    &aocg);
685 
686 	memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
687 
688 	/*
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.
694 	 */
695 
696 	if(cgbase(&osblock, cylno+1) == osblock.fs_size) {
697 		acg.cg_ncyl=sblock.fs_cpg;
698 
699 		wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
700 		    (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
701 		DBG_PRINT0("jcg written\n");
702 		DBG_DUMP_CG(&sblock,
703 		    "new joining cg",
704 		    &acg);
705 
706 		DBG_LEAVE;
707 		return;
708 	}
709 
710 	/*
711 	 * Set up some variables needed later.
712 	 */
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);
720 	}
721 
722 	/*
723 	 * Set pointer to the cylinder summary for our cylinder group.
724 	 */
725 	cs = fscs + cylno;
726 
727 	/*
728 	 * Touch the cylinder group, update all fields in the cylinder group as
729 	 * needed, update the free space in the superblock.
730 	 */
731 	acg.cg_time = utime;
732 	if (cylno == sblock.fs_ncg - 1) {
733 		/*
734 		 * This is still the last cylinder group.
735 		 */
736 		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
737 	} else {
738 		acg.cg_ncyl = sblock.fs_cpg;
739 	}
740 	DBG_PRINT4("jcg dbg: %d %u %d %u\n",
741 	    cylno,
742 	    sblock.fs_ncg,
743 	    acg.cg_ncyl,
744 	    sblock.fs_cpg);
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;
749 	}
750 
751 	/*
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.
759 	 *
760 	 * Handle the first new block here if it was partially available
761 	 * before.
762 	 */
763 	if(osblock.fs_size % sblock.fs_frag) {
764 		if(roundup(osblock.fs_size, sblock.fs_frag)<=sblock.fs_size) {
765 			/*
766 			 * The new space is enough to fill at least this
767 			 * block
768 			 */
769 			j=0;
770 			for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag)-1;
771 			    i>=osblock.fs_size-cbase;
772 			    i--) {
773 				setbit(cg_blksfree(&acg), i);
774 				acg.cg_cs.cs_nffree++;
775 				j++;
776 			}
777 
778 			/*
779 			 * Check  if the fragment just created could join  an
780 			 * already existing fragment at the former end of the
781 			 * file system.
782 			 */
783 			if(isblock(&sblock, cg_blksfree(&acg),
784 			    ((osblock.fs_size - cgbase(&sblock, cylno))/
785 			    sblock.fs_frag))) {
786 				/*
787 				 * The block is now completely available
788 				 */
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,
794 				    sblock.fs_frag);
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);
799 			} else {
800 				/*
801 				 * Lets rejoin a possible partially growed
802 				 * fragment.
803 				 */
804 				k=0;
805 				while(isset(cg_blksfree(&acg), i) &&
806 				    (i>=rounddown(osblock.fs_size-cbase,
807 				    sblock.fs_frag))) {
808 					i--;
809 					k++;
810 				}
811 				if(k) {
812 					acg.cg_frsum[k]--;
813 				}
814 				acg.cg_frsum[k+j]++;
815 			}
816 		} else {
817 			/*
818 			 * We only grow by some fragments within this last
819 			 * block.
820 			 */
821 			for(i=sblock.fs_size-cbase-1;
822 				i>=osblock.fs_size-cbase;
823 				i--) {
824 				setbit(cg_blksfree(&acg), i);
825 				acg.cg_cs.cs_nffree++;
826 				j++;
827 			}
828 			/*
829 			 * Lets rejoin a possible partially growed fragment.
830 			 */
831 			k=0;
832 			while(isset(cg_blksfree(&acg), i) &&
833 			    (i>=rounddown(osblock.fs_size-cbase,
834 			    sblock.fs_frag))) {
835 				i--;
836 				k++;
837 			}
838 			if(k) {
839 				acg.cg_frsum[k]--;
840 			}
841 			acg.cg_frsum[k+j]++;
842 		}
843 	}
844 
845 	/*
846 	 * Handle all new complete blocks here.
847 	 */
848 	for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag);
849 	    i+sblock.fs_frag<=dmax-cbase;	/* XXX <= or only < ? */
850 	    i+=sblock.fs_frag) {
851 		j = i / sblock.fs_frag;
852 		setblock(&sblock, cg_blksfree(&acg), j);
853 		updclst(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)]++;
858 	}
859 
860 	/*
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.
864 	 */
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++;
870 		}
871 	}
872 
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);
877 	/*
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.
883 	 */
884 	*cs = acg.cg_cs;
885 
886 	/*
887 	 * Write the updated "joining" cylinder group back to disk.
888 	 */
889 	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
890 	    (void *)&acg, fso, Nflag);
891 	DBG_PRINT0("jcg written\n");
892 	DBG_DUMP_CG(&sblock,
893 	    "new joining cg",
894 	    &acg);
895 
896 	DBG_LEAVE;
897 	return;
898 }
899 
900 /* ********************************************************** updcsloc ***** */
901 /*
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.
916  */
917 static void
918 updcsloc(time_t utime, int fsi, int fso, unsigned int Nflag)
919 {
920 	DBG_FUNC("updcsloc")
921 	struct csum	*cs;
922 	int	ocscg, ncscg;
923 	int	blocks;
924 	daddr_t	cbase, dupper, odupper, d, f, g;
925 	int	ind;
926 	int	cylno, inc;
927 	struct gfs_bpp	*bp;
928 	int	i, l;
929 	int	lcs=0;
930 	int	block;
931 
932 	DBG_ENTER;
933 
934 	if(howmany(sblock.fs_cssize, sblock.fs_fsize) ==
935 	    howmany(osblock.fs_cssize, osblock.fs_fsize)) {
936 		/*
937 		 * No new fragment needed.
938 		 */
939 		DBG_LEAVE;
940 		return;
941 	}
942 	ocscg=dtog(&osblock, osblock.fs_csaddr);
943 	cs=fscs+ocscg;
944 	blocks = 1+howmany(sblock.fs_cssize, sblock.fs_bsize)-
945 	    howmany(osblock.fs_cssize, osblock.fs_bsize);
946 
947 	/*
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
951 	 *	block from disk.
952 	 */
953 	rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
954 	    (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
955 	DBG_PRINT0("oscg read\n");
956 	DBG_DUMP_CG(&sblock,
957 	    "old summary cg",
958 	    &aocg);
959 
960 	memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
961 
962 	/*
963 	 * Touch the cylinder group, set up local variables needed later
964 	 * and update the superblock.
965 	 */
966 	acg.cg_time = utime;
967 
968 	/*
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
972 	 *	references.
973 	 */
974 	if(/*((int)sblock.fs_time&0x3)>0||*/ cs->cs_nbfree < blocks) {
975 		/*
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.
983 		 *
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.
993 		 */
994 		DBG_TRC;
995 		if(sblock.fs_ncg-osblock.fs_ncg < 2) {
996 			errx(2, "panic: not enough space");
997 		}
998 
999 		/*
1000 		 * Point "d" to the first fragment not used by the cylinder
1001 		 * summary.
1002 		 */
1003 		d=osblock.fs_csaddr+(osblock.fs_cssize/osblock.fs_fsize);
1004 
1005 		/*
1006 		 * Set up last cluster size ("lcs") already here. Calculate
1007 		 * the size for the trailing cluster just behind where  "d"
1008 		 * points to.
1009 		 */
1010 		if(sblock.fs_contigsumsize > 0) {
1011 			for(block=howmany(d%sblock.fs_fpg, sblock.fs_frag),
1012 			    lcs=0; lcs<sblock.fs_contigsumsize;
1013 			    block++, lcs++) {
1014 				if(isclr(cg_clustersfree(&acg), block)){
1015 					break;
1016 				}
1017 			}
1018 		}
1019 
1020 		/*
1021 		 * Point "d" to the last frag used by the cylinder summary.
1022 		 */
1023 		d--;
1024 
1025 		DBG_PRINT1("d=%d\n",
1026 		    d);
1027 		if((d+1)%sblock.fs_frag) {
1028 			/*
1029 			 * The end of the cylinder summary is not a complete
1030 			 * block.
1031 			 */
1032 			DBG_TRC;
1033 			frag_adjust(d%sblock.fs_fpg, -1);
1034 			for(; (d+1)%sblock.fs_frag; d--) {
1035 				DBG_PRINT1("d=%d\n",
1036 				    d);
1037 				setbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1038 				acg.cg_cs.cs_nffree++;
1039 				sblock.fs_cstotal.cs_nffree++;
1040 			}
1041 			/*
1042 			 * Point  "d" to the last fragment of the  last
1043 			 * (incomplete) block of the clinder summary.
1044 			 */
1045 			d++;
1046 			frag_adjust(d%sblock.fs_fpg, 1);
1047 
1048 			if(isblock(&sblock, cg_blksfree(&acg),
1049 			    (d%sblock.fs_fpg)/sblock.fs_frag)) {
1050 				DBG_PRINT1("d=%d\n",
1051 				    d);
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) {
1065 						if(lcs) {
1066 							cg_clustersum(&acg)
1067 							    [lcs]--;
1068 						}
1069 						lcs++;
1070 						cg_clustersum(&acg)[lcs]++;
1071 					}
1072 				}
1073 			}
1074 			/*
1075 			 * Point "d" to the first fragment of the block before
1076 			 * the last incomplete block.
1077 			 */
1078 			d--;
1079 		}
1080 
1081 		DBG_PRINT1("d=%d\n",
1082 		    d);
1083 		for(d=rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
1084 		    d-=sblock.fs_frag) {
1085 			DBG_TRC;
1086 			DBG_PRINT1("d=%d\n",
1087 			    d);
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);
1099 				/*
1100 				 * The last cluster size is already set up.
1101 				 */
1102 				if(lcs < sblock.fs_contigsumsize) {
1103 					if(lcs) {
1104 						cg_clustersum(&acg)[lcs]--;
1105 					}
1106 					lcs++;
1107 					cg_clustersum(&acg)[lcs]++;
1108 				}
1109 			}
1110 		}
1111 		*cs = acg.cg_cs;
1112 
1113 		/*
1114 		 * Now write the former cylinder group containing the cylinder
1115 		 * summary back to disk.
1116 		 */
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,
1121 		    "old summary cg",
1122 		    &acg);
1123 
1124 		/*
1125 		 * Find the beginning of the new cylinder group containing the
1126 		 * cylinder summary.
1127 		 */
1128 		sblock.fs_csaddr=cgdmin(&sblock, osblock.fs_ncg);
1129 		ncscg=dtog(&sblock, sblock.fs_csaddr);
1130 		cs=fscs+ncscg;
1131 
1132 
1133 		/*
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
1136 		 * part for now.
1137 		 */
1138 		if(Nflag) {
1139 			DBG_PRINT0("nscg update skipped\n");
1140 			DBG_LEAVE;
1141 			return;
1142 		}
1143 
1144 		/*
1145 		 * Read the future cylinder group containing the cylinder
1146 		 * summary from disk, and make a copy.
1147 		 */
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,
1152 		    "new summary cg",
1153 		    &aocg);
1154 
1155 		memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
1156 
1157 		/*
1158 		 * Allocate all complete blocks used by the new cylinder
1159 		 * summary.
1160 		 */
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);
1175 			}
1176 		}
1177 
1178 		/*
1179 		 * Allocate all fragments used by the cylinder summary in the
1180 		 * last block.
1181 		 */
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;
1185 			    d++) {
1186 				clrbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1187 				acg.cg_cs.cs_nffree--;
1188 				sblock.fs_cstotal.cs_nffree--;
1189 			}
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);
1201 			}
1202 
1203 			frag_adjust(d%sblock.fs_fpg, +1);
1204 		}
1205 		/*
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.
1213 		 */
1214 
1215 		/*
1216 		 * Update our statistics in the cylinder summary.
1217 		 */
1218 		*cs = acg.cg_cs;
1219 
1220 		/*
1221 		 * Write the new cylinder group containing the cylinder summary
1222 		 * back to disk.
1223 		 */
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,
1228 		    "new summary cg",
1229 		    &acg);
1230 
1231 		DBG_LEAVE;
1232 		return;
1233 	}
1234 	/*
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.
1238 	 */
1239 	DBG_TRC;
1240 
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);
1246 
1247 	sblock.fs_dsize -= dupper-odupper;
1248 
1249 	/*
1250 	 * Allocate the space for the array of blocks to be relocated.
1251 	 */
1252  	bp=(struct gfs_bpp *)malloc(((dupper-odupper)/sblock.fs_frag+2)*
1253 	    sizeof(struct gfs_bpp));
1254 	if(bp == NULL) {
1255 		errx(1, "malloc failed");
1256 	}
1257 	memset((char *)bp, 0, ((dupper-odupper)/sblock.fs_frag+2)*
1258 	    sizeof(struct gfs_bpp));
1259 
1260 	/*
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.
1264 	 *
1265 	 * Handle the first new  block here (but only if some fragments where
1266 	 * already used for the cylinder summary).
1267 	 */
1268 	ind=0;
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",
1272 		    d);
1273 		if(isclr(cg_blksfree(&acg), d)) {
1274 			if (!ind) {
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;
1279 				}
1280 				ind++;
1281 			}
1282 		} else {
1283 			clrbit(cg_blksfree(&acg), d);
1284 			acg.cg_cs.cs_nffree--;
1285 			sblock.fs_cstotal.cs_nffree--;
1286 		}
1287 		/*
1288 		 * No cluster handling is needed here, as there was at least
1289 		 * one  fragment in use by the cylinder summary in  the  old
1290 		 * file system.
1291 		 * No block-free counter handling here as this block was not
1292 		 * a free block.
1293 		 */
1294 	}
1295 	frag_adjust(odupper, 1);
1296 
1297 	/*
1298 	 * Handle all needed complete blocks here.
1299 	 */
1300 	for(; d+sblock.fs_frag<=dupper; d+=sblock.fs_frag) {
1301 		DBG_PRINT1("scg block check loop d=%d\n",
1302 		    d);
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--;
1308 				}
1309 			}
1310 			clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
1311 			bp[ind].old=d/sblock.fs_frag;
1312 			ind++;
1313 		} else {
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;
1324 				    l++, lcs++ ) {
1325 					if(isclr(cg_clustersfree(&acg),l)){
1326 						break;
1327 					}
1328 				}
1329 				if(lcs < sblock.fs_contigsumsize) {
1330 					cg_clustersum(&acg)[lcs+1]--;
1331 					if(lcs) {
1332 						cg_clustersum(&acg)[lcs]++;
1333 					}
1334 				}
1335 			}
1336 		}
1337 		/*
1338 		 * No fragment counter handling is needed here, as this finally
1339 		 * doesn't change after the relocation.
1340 		 */
1341 	}
1342 
1343 	/*
1344 	 * Handle all fragments needed in the last new affected block.
1345 	 */
1346 	if(d<dupper) {
1347 		frag_adjust(dupper-1, -1);
1348 
1349 		if(isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) {
1350 			acg.cg_cs.cs_nbfree--;
1351 			sblock.fs_cstotal.cs_nbfree--;
1352 			acg.cg_cs.cs_nffree+=sblock.fs_frag;
1353 			sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1354 			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;
1361 				    l++, lcs++ ) {
1362 					if(isclr(cg_clustersfree(&acg),l)){
1363 						break;
1364 					}
1365 				}
1366 				if(lcs < sblock.fs_contigsumsize) {
1367 					cg_clustersum(&acg)[lcs+1]--;
1368 					if(lcs) {
1369 						cg_clustersum(&acg)[lcs]++;
1370 					}
1371 				}
1372 			}
1373 		}
1374 
1375 		for(; d<dupper; d++) {
1376 			DBG_PRINT1("scg second frag check loop d=%d\n",
1377 			    d);
1378 			if(isclr(cg_blksfree(&acg), d)) {
1379 				bp[ind].old=d/sblock.fs_frag;
1380 				bp[ind].flags|=GFS_FL_LAST;
1381 			} else {
1382 				clrbit(cg_blksfree(&acg), d);
1383 				acg.cg_cs.cs_nffree--;
1384 				sblock.fs_cstotal.cs_nffree--;
1385 			}
1386 		}
1387 		if(bp[ind].flags & GFS_FL_LAST) { /* we have to advance here */
1388 			ind++;
1389 		}
1390 		frag_adjust(dupper-1, 1);
1391 	}
1392 
1393 	/*
1394 	 * If we found a block to relocate just do so.
1395 	 */
1396 	if(ind) {
1397 		for(i=0; i<ind; i++) {
1398 			if(!bp[i].old) { /* no more blocks listed */
1399 				/*
1400 				 * XXX	A relative blocknumber should not be
1401 				 *	zero,   which  is   not   explicitly
1402 				 *	guaranteed by our code.
1403 				 */
1404 				break;
1405 			}
1406 			/*
1407 			 * Allocate a complete block in the same (current)
1408 			 * cylinder group.
1409 			 */
1410 			bp[i].new=alloc()/sblock.fs_frag;
1411 
1412 			/*
1413 			 * There is no frag_adjust() needed for the new block
1414 			 * as it will have no fragments yet :-).
1415 			 */
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;
1419 			    f++, g++) {
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++;
1424 				}
1425 			}
1426 
1427 			/*
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.
1434 			 */
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;
1438 				    f<odupper;
1439 				    f++, g++) {
1440 					setbit(cg_blksfree(&acg), g);
1441 					acg.cg_cs.cs_nffree++;
1442 					sblock.fs_cstotal.cs_nffree++;
1443 				}
1444 				if(!(bp[i].flags & GFS_FL_LAST)) {
1445 					frag_adjust(bp[i].new*sblock.fs_frag,1);
1446 				}
1447 
1448 			}
1449 
1450 			/*
1451 			 * Special handling is required if this is the last
1452 			 * block to be relocated.
1453 			 */
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);
1457 				for(f=dupper;
1458 				    f<roundup(dupper, sblock.fs_frag);
1459 				    f++) {
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++;
1464 					}
1465 				}
1466 				frag_adjust(bp[i].old*sblock.fs_frag, 1);
1467 			}
1468 
1469 			/*
1470 			 * !!! Attach the cylindergroup offset here.
1471 			 */
1472 			bp[i].old+=cbase/sblock.fs_frag;
1473 			bp[i].new+=cbase/sblock.fs_frag;
1474 
1475 			/*
1476 			 * Copy the content of the block.
1477 			 */
1478 			/*
1479 			 * XXX	Here we will have to implement a copy on write
1480 			 *	in the case we have any active snapshots.
1481 			 */
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);
1489 
1490 			DBG_PRINT2("scg (%d->%d) block relocated\n",
1491 			    bp[i].old,
1492 			    bp[i].new);
1493 		}
1494 
1495 		/*
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
1499 		 * inodes.
1500 		 */
1501 		for(cylno=0; cylno<osblock.fs_ncg; cylno++) {
1502 			DBG_PRINT1("scg doing cg (%d)\n",
1503 			    cylno);
1504 			for(inc=osblock.fs_ipg-1 ; inc>=0 ; inc--) {
1505 				updrefs(cylno, (ino_t)inc, bp, fsi, fso, Nflag);
1506 			}
1507 		}
1508 
1509 		/*
1510 		 * All inodes are checked, now make sure the number of
1511 		 * references found make sense.
1512 		 */
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);
1517 			}
1518 
1519 		}
1520 	}
1521 	/*
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.
1530 	 */
1531 
1532 	*cs = acg.cg_cs;
1533 
1534 	/*
1535 	 * Write summary cylinder group back to disk.
1536 	 */
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,
1541 	    "new summary cg",
1542 	    &acg);
1543 
1544 	DBG_LEAVE;
1545 	return;
1546 }
1547 
1548 /* ************************************************************** rdfs ***** */
1549 /*
1550  * Here we read some block(s) from disk.
1551  */
1552 static void
1553 rdfs(daddr_t bno, size_t size, void *bf, int fsi)
1554 {
1555 	DBG_FUNC("rdfs")
1556 	ssize_t	n;
1557 
1558 	DBG_ENTER;
1559 
1560 	if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
1561 		err(33, "rdfs: seek error: %ld", (long)bno);
1562 	}
1563 	n = read(fsi, bf, size);
1564 	if (n != (ssize_t)size) {
1565 		err(34, "rdfs: read error: %ld", (long)bno);
1566 	}
1567 
1568 	DBG_LEAVE;
1569 	return;
1570 }
1571 
1572 /* ************************************************************** wtfs ***** */
1573 /*
1574  * Here we write some block(s) to disk.
1575  */
1576 static void
1577 wtfs(daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1578 {
1579 	DBG_FUNC("wtfs")
1580 	ssize_t	n;
1581 
1582 	DBG_ENTER;
1583 
1584 	if (Nflag) {
1585 		DBG_LEAVE;
1586 		return;
1587 	}
1588 	if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) {
1589 		err(35, "wtfs: seek error: %ld", (long)bno);
1590 	}
1591 	n = write(fso, bf, size);
1592 	if (n != (ssize_t)size) {
1593 		err(36, "wtfs: write error: %ld", (long)bno);
1594 	}
1595 
1596 	DBG_LEAVE;
1597 	return;
1598 }
1599 
1600 /* ************************************************************* alloc ***** */
1601 /*
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.
1605  */
1606 static daddr_t
1607 alloc(void)
1608 {
1609 	DBG_FUNC("alloc")
1610 	daddr_t	d, blkno;
1611 	int	lcs1, lcs2;
1612 	int	l;
1613 	int	csmin, csmax;
1614 	int	dlower, dupper, dmax;
1615 
1616 	DBG_ENTER;
1617 
1618 	if (acg.cg_magic != CG_MAGIC) {
1619 		warnx("acg: bad magic number");
1620 		DBG_LEAVE;
1621 		return (0);
1622 	}
1623 	if (acg.cg_cs.cs_nbfree == 0) {
1624 		warnx("error: cylinder group ran out of space");
1625 		DBG_LEAVE;
1626 		return (0);
1627 	}
1628 	/*
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.
1633 	 */
1634 	blkno=-1;
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;
1640 	}
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",
1645 	    dlower,
1646 	    dupper,
1647 	    dmax);
1648 	DBG_PRINT2("range cont: csmin=%d, csmax=%d\n",
1649 	    csmin,
1650 	    csmax);
1651 
1652 	for(d=0; (d<dlower && blkno==-1); d+=sblock.fs_frag) {
1653 		if(d>=csmin && d<=csmax) {
1654 			continue;
1655 		}
1656 		if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1657 		    d))) {
1658 			blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1659 			break;
1660 		}
1661 	}
1662 	for(d=dupper; (d<dmax && blkno==-1); d+=sblock.fs_frag) {
1663 		if(d>=csmin && d<=csmax) {
1664 			continue;
1665 		}
1666 		if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1667 		    d))) {
1668 			blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1669 			break;
1670 		}
1671 	}
1672 	if(blkno==-1) {
1673 		warnx("internal error: couldn't find promised block in cg");
1674 		DBG_LEAVE;
1675 		return (0);
1676 	}
1677 
1678 	/*
1679 	 * This is needed if the block was found already in the first loop.
1680 	 */
1681 	d=blkstofrags(&sblock, blkno);
1682 
1683 	clrblock(&sblock, cg_blksfree(&acg), blkno);
1684 	if (sblock.fs_contigsumsize > 0) {
1685 		/*
1686 		 * Handle the cluster allocation bitmap.
1687 		 */
1688 		clrbit(cg_clustersfree(&acg), blkno);
1689 		/*
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.
1693 		 *
1694 		 * Lets start with the blocks before our allocated block ...
1695 		 */
1696 		for(lcs1=0, l=blkno-1; lcs1<sblock.fs_contigsumsize;
1697 		    l--, lcs1++ ) {
1698 			if(isclr(cg_clustersfree(&acg),l)){
1699 				break;
1700 			}
1701 		}
1702 		/*
1703 		 * ... and continue with the blocks right after our allocated
1704 		 * block.
1705 		 */
1706 		for(lcs2=0, l=blkno+1; lcs2<sblock.fs_contigsumsize;
1707 		    l++, lcs2++ ) {
1708 			if(isclr(cg_clustersfree(&acg),l)){
1709 				break;
1710 			}
1711 		}
1712 
1713 		/*
1714 		 * Now update all counters.
1715 		 */
1716 		cg_clustersum(&acg)[MIN(lcs1+lcs2+1,sblock.fs_contigsumsize)]--;
1717 		if(lcs1) {
1718 			cg_clustersum(&acg)[lcs1]++;
1719 		}
1720 		if(lcs2) {
1721 			cg_clustersum(&acg)[lcs2]++;
1722 		}
1723 	}
1724 	/*
1725 	 * Update all statistics based on blocks.
1726 	 */
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)]--;
1731 
1732 	DBG_LEAVE;
1733 	return (d);
1734 }
1735 
1736 /* *********************************************************** isblock ***** */
1737 /*
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
1740  * unchanged.
1741  */
1742 static int
1743 isblock(struct fs *fs, unsigned char *cp, int h)
1744 {
1745 	DBG_FUNC("isblock")
1746 	unsigned char	mask;
1747 
1748 	DBG_ENTER;
1749 
1750 	switch (fs->fs_frag) {
1751 	case 8:
1752 		DBG_LEAVE;
1753 		return (cp[h] == 0xff);
1754 	case 4:
1755 		mask = 0x0f << ((h & 0x1) << 2);
1756 		DBG_LEAVE;
1757 		return ((cp[h >> 1] & mask) == mask);
1758 	case 2:
1759 		mask = 0x03 << ((h & 0x3) << 1);
1760 		DBG_LEAVE;
1761 		return ((cp[h >> 2] & mask) == mask);
1762 	case 1:
1763 		mask = 0x01 << (h & 0x7);
1764 		DBG_LEAVE;
1765 		return ((cp[h >> 3] & mask) == mask);
1766 	default:
1767 		fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1768 		DBG_LEAVE;
1769 		return (0);
1770 	}
1771 }
1772 
1773 /* ********************************************************** clrblock ***** */
1774 /*
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
1777  * unchanged.
1778  */
1779 static void
1780 clrblock(struct fs *fs, unsigned char *cp, int h)
1781 {
1782 	DBG_FUNC("clrblock")
1783 
1784 	DBG_ENTER;
1785 
1786 	switch ((fs)->fs_frag) {
1787 	case 8:
1788 		cp[h] = 0;
1789 		break;
1790 	case 4:
1791 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1792 		break;
1793 	case 2:
1794 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1795 		break;
1796 	case 1:
1797 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
1798 		break;
1799 	default:
1800 		warnx("clrblock bad fs_frag %d", fs->fs_frag);
1801 		break;
1802 	}
1803 
1804 	DBG_LEAVE;
1805 	return;
1806 }
1807 
1808 /* ********************************************************** setblock ***** */
1809 /*
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
1812  * unchanged.
1813  */
1814 static void
1815 setblock(struct fs *fs, unsigned char *cp, int h)
1816 {
1817 	DBG_FUNC("setblock")
1818 
1819 	DBG_ENTER;
1820 
1821 	switch (fs->fs_frag) {
1822 	case 8:
1823 		cp[h] = 0xff;
1824 		break;
1825 	case 4:
1826 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1827 		break;
1828 	case 2:
1829 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1830 		break;
1831 	case 1:
1832 		cp[h >> 3] |= (0x01 << (h & 0x7));
1833 		break;
1834 	default:
1835 		warnx("setblock bad fs_frag %d", fs->fs_frag);
1836 		break;
1837 	}
1838 
1839 	DBG_LEAVE;
1840 	return;
1841 }
1842 
1843 /* ************************************************************ ginode ***** */
1844 /*
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
1849  * inodes.
1850  */
1851 static struct dinode *
1852 ginode(ino_t inumber, int fsi, int cg)
1853 {
1854 	DBG_FUNC("ginode")
1855 	ufs_daddr_t	iblk;
1856 	static ino_t	startinum=0;	/* first inode in cached block */
1857 	struct dinode	*pi;
1858 
1859 	DBG_ENTER;
1860 
1861 	pi=(struct dinode *)(void *)ablk;
1862 	inumber+=(cg * sblock.fs_ipg);
1863 	if (startinum == 0 || inumber < startinum ||
1864 	    inumber >= startinum + INOPB(&sblock)) {
1865 		/*
1866 		 * The block needed is not cached, so we have to read it from
1867 		 * disk now.
1868 		 */
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);
1873 	}
1874 
1875 	DBG_LEAVE;
1876 	return (&(pi[inumber % INOPB(&sblock)]));
1877 }
1878 
1879 /* ****************************************************** charsperline ***** */
1880 /*
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
1883  * unchanged.
1884  */
1885 static int
1886 charsperline(void)
1887 {
1888 	DBG_FUNC("charsperline")
1889 	int	columns;
1890 	char	*cp;
1891 	struct winsize	ws;
1892 
1893 	DBG_ENTER;
1894 
1895 	columns = 0;
1896 	if (ioctl(0, TIOCGWINSZ, &ws) != -1) {
1897 		columns = ws.ws_col;
1898 	}
1899 	if (columns == 0 && (cp = getenv("COLUMNS"))) {
1900 		columns = atoi(cp);
1901 	}
1902 	if (columns == 0) {
1903 		columns = 80;	/* last resort */
1904 	}
1905 
1906 	DBG_LEAVE;
1907 	return columns;
1908 }
1909 
1910 /* ************************************************************** main ***** */
1911 /*
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
1925  * way.
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.
1932  */
1933 int
1934 main(int argc, char **argv)
1935 {
1936 	DBG_FUNC("main")
1937 	char	*device, *special, *cp;
1938 	char	ch;
1939 	unsigned int	size=0;
1940 	size_t	len;
1941 	unsigned int	Nflag=0;
1942 	int	ExpertFlag=0;
1943 	struct stat	st;
1944 	struct disklabel	*lp;
1945 	struct partition	*pp;
1946 	int	fsi,fso;
1947 	char	reply[5];
1948 #ifdef FSMAXSNAP
1949 	int	j;
1950 #endif /* FSMAXSNAP */
1951 
1952 	DBG_ENTER;
1953 
1954 	while((ch=getopt(argc, argv, "Ns:vy")) != -1) {
1955 		switch(ch) {
1956 		case 'N':
1957 			Nflag=1;
1958 			break;
1959 		case 's':
1960 			size=(size_t)atol(optarg);
1961 			if(size<1) {
1962 				usage();
1963 			}
1964 			break;
1965 		case 'v': /* for compatibility to newfs */
1966 			break;
1967 		case 'y':
1968 			ExpertFlag=1;
1969 			break;
1970 		case '?':
1971 			/* FALLTHROUGH */
1972 		default:
1973 			usage();
1974 		}
1975 	}
1976 	argc -= optind;
1977 	argv += optind;
1978 
1979 	if(argc != 1) {
1980 		usage();
1981 	}
1982 	device=*argv;
1983 
1984 	/*
1985 	 * Now try to guess the (raw)device name.
1986 	 */
1987 	if (0 == strrchr(device, '/')) {
1988 		/*
1989 		 * No path prefix was given, so try in that order:
1990 		 *     /dev/r%s
1991 		 *     /dev/%s
1992 		 *     /dev/vinum/r%s
1993 		 *     /dev/vinum/%s.
1994 		 *
1995 		 * FreeBSD now doesn't distinguish between raw and  block
1996 		 * devices any longer, but it should still work this way.
1997 		 */
1998 		len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/");
1999 		special=(char *)malloc(len);
2000 		if(special == NULL) {
2001 			errx(1, "malloc failed");
2002 		}
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",
2008 				    _PATH_DEV, device);
2009 				if (stat(special, &st) == -1) {
2010 					/* For now this is the 'last resort' */
2011 					snprintf(special, len, "%svinum/%s",
2012 					    _PATH_DEV, device);
2013 				}
2014 			}
2015 		}
2016 		device = special;
2017 	}
2018 
2019 	/*
2020 	 * Try to access our devices for writing ...
2021 	 */
2022 	if (Nflag) {
2023 		fso = -1;
2024 	} else {
2025 		fso = open(device, O_WRONLY);
2026 		if (fso < 0) {
2027 			err(1, "%s", device);
2028 		}
2029 	}
2030 
2031 	/*
2032 	 * ... and reading.
2033 	 */
2034 	fsi = open(device, O_RDONLY);
2035 	if (fsi < 0) {
2036 		err(1, "%s", device);
2037 	}
2038 
2039 	/*
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.
2043 	 */
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'];
2052 	} else {
2053 		errx(1, "unknown device");
2054 	}
2055 
2056 	/*
2057 	 * Check if that partition looks suited for growing a file system.
2058 	 */
2059 	if (pp->p_size < 1) {
2060 		errx(1, "partition is unavailable");
2061 	}
2062 	if (pp->p_fstype != FS_BSDFFS) {
2063 		errx(1, "partition not 4.2BSD");
2064 	}
2065 
2066 	/*
2067 	 * Read the current superblock, and take a backup.
2068 	 */
2069 	rdfs((daddr_t)(SBOFF/DEV_BSIZE), (size_t)SBSIZE, (void *)&(osblock),
2070 	    fsi);
2071 	if (osblock.fs_magic != FS_MAGIC) {
2072 		errx(1, "superblock not recognized");
2073 	}
2074 	memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2));
2075 
2076 	DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
2077 	DBG_DUMP_FS(&sblock,
2078 	    "old sblock");
2079 
2080 	/*
2081 	 * Determine size to grow to. Default to the full size specified in
2082 	 * the disk label.
2083 	 */
2084 	sblock.fs_size = dbtofsb(&osblock, pp->p_size);
2085 	if (size != 0) {
2086 		if (size > pp->p_size){
2087 			errx(1, "There is not enough space (%d < %d)",
2088 			    pp->p_size, size);
2089 		}
2090 		sblock.fs_size = dbtofsb(&osblock, size);
2091 	}
2092 
2093 	/*
2094 	 * Are we really growing ?
2095 	 */
2096 	if(osblock.fs_size >= sblock.fs_size) {
2097 		errx(1, "we are not growing (%d->%d)", osblock.fs_size,
2098 		    sblock.fs_size);
2099 	}
2100 
2101 
2102 #ifdef FSMAXSNAP
2103 	/*
2104 	 * Check if we find an active snapshot.
2105 	 */
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 "
2111 				    "using growfs\n");
2112 			}
2113 			if(!sblock.fs_snapinum[j]) { /* list is dense */
2114 				break;
2115 			}
2116 		}
2117 	}
2118 #endif
2119 
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");
2127 			exit (0);
2128 		}
2129 	}
2130 
2131 	printf("new filesystemsize is: %d frags\n", sblock.fs_size);
2132 
2133 	/*
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.
2137 	 */
2138 	wtfs((daddr_t)pp->p_size-1, (size_t)DEV_BSIZE, (void *)&sblock, fso,
2139 	    Nflag);
2140 
2141 	/*
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
2149 	 */
2150 
2151 	/*
2152 	 * Update the number of cylinders in the filesystem.
2153 	 */
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) {
2156 		sblock.fs_ncyl++;
2157 	}
2158 
2159 	/*
2160 	 * Update the number of cylinder groups in the filesystem.
2161 	 */
2162 	sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
2163 	if (sblock.fs_ncyl % sblock.fs_cpg) {
2164 		sblock.fs_ncg++;
2165 	}
2166 
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 )) {
2171 		/*
2172 		 * The space in the new last cylinder group is too small,
2173 		 * so revert back.
2174 		 */
2175 		sblock.fs_ncg--;
2176 #if 1 /* this is a bit more safe */
2177 		sblock.fs_ncyl = sblock.fs_ncg * sblock.fs_cpg;
2178 #else
2179 		sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
2180 #endif
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) *
2184 		    NSPF(&sblock));
2185 		sblock.fs_size = sblock.fs_ncyl * sblock.fs_spc / NSPF(&sblock);
2186 	}
2187 
2188 	/*
2189 	 * Update the space for the cylinder group summary information in the
2190 	 * respective cylinder group data area.
2191 	 */
2192 	sblock.fs_cssize =
2193 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
2194 
2195 	if(osblock.fs_size >= sblock.fs_size) {
2196 		errx(1, "not enough new space");
2197 	}
2198 
2199 	DBG_PRINT0("sblock calculated\n");
2200 
2201 	/*
2202 	 * Ok, everything prepared, so now let's do the tricks.
2203 	 */
2204 	growfs(fsi, fso, Nflag);
2205 
2206 	/*
2207 	 * Update the disk label.
2208 	 */
2209 	pp->p_fsize = sblock.fs_fsize;
2210 	pp->p_frag = sblock.fs_frag;
2211 	pp->p_cpg = sblock.fs_cpg;
2212 
2213 	return_disklabel(fso, lp, Nflag);
2214 	DBG_PRINT0("label rewritten\n");
2215 
2216 	close(fsi);
2217 	if(fso>-1) close(fso);
2218 
2219 	DBG_CLOSE;
2220 
2221 	DBG_LEAVE;
2222 	return 0;
2223 }
2224 
2225 /* ************************************************** return_disklabel ***** */
2226 /*
2227  * Write the updated disklabel back to disk.
2228  */
2229 static void
2230 return_disklabel(int fd, struct disklabel *lp, unsigned int Nflag)
2231 {
2232 	DBG_FUNC("return_disklabel")
2233 	u_short	sum;
2234 	u_short	*ptr;
2235 
2236 	DBG_ENTER;
2237 
2238 	if(!lp) {
2239 		DBG_LEAVE;
2240 		return;
2241 	}
2242 	if(!Nflag) {
2243 		lp->d_checksum=0;
2244 		sum = 0;
2245 		ptr=(u_short *)lp;
2246 
2247 		/*
2248 		 * recalculate checksum
2249 		 */
2250 		while(ptr < (u_short *)&lp->d_partitions[lp->d_npartitions]) {
2251 			sum ^= *ptr++;
2252 		}
2253 		lp->d_checksum=sum;
2254 
2255 		if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
2256 			errx(1, "DIOCWDINFO failed");
2257 		}
2258 	}
2259 	free(lp);
2260 
2261 	DBG_LEAVE;
2262 	return ;
2263 }
2264 
2265 /* ***************************************************** get_disklabel ***** */
2266 /*
2267  * Read the disklabel from disk.
2268  */
2269 static struct disklabel *
2270 get_disklabel(int fd)
2271 {
2272 	DBG_FUNC("get_disklabel")
2273 	static struct	disklabel *lab;
2274 
2275 	DBG_ENTER;
2276 
2277 	lab=(struct disklabel *)malloc(sizeof(struct disklabel));
2278 	if (!lab) {
2279 		errx(1, "malloc failed");
2280 	}
2281 	if (ioctl(fd, DIOCGDINFO, (char *)lab) < 0) {
2282 		errx(1, "DIOCGDINFO failed");
2283 	}
2284 
2285 	DBG_LEAVE;
2286 	return (lab);
2287 }
2288 
2289 
2290 /* ************************************************************* usage ***** */
2291 /*
2292  * Dump a line of usage.
2293  */
2294 static void
2295 usage(void)
2296 {
2297 	DBG_FUNC("usage")
2298 
2299 	DBG_ENTER;
2300 
2301 	fprintf(stderr, "usage: growfs [-Ny] [-s size] special\n");
2302 
2303 	DBG_LEAVE;
2304 	exit(1);
2305 }
2306 
2307 /* *********************************************************** updclst ***** */
2308 /*
2309  * This updates most paramters and the bitmap related to cluster. We have to
2310  * assume, that sblock, osblock, acg are set up.
2311  */
2312 static void
2313 updclst(int block)
2314 {
2315 	DBG_FUNC("updclst")
2316 	static int	lcs=0;
2317 
2318 	DBG_ENTER;
2319 
2320 	if(sblock.fs_contigsumsize < 1) { /* no clustering */
2321 		return;
2322 	}
2323 	/*
2324 	 * update cluster allocation map
2325 	 */
2326 	setbit(cg_clustersfree(&acg), block);
2327 
2328 	/*
2329 	 * update cluster summary table
2330 	 */
2331 	if(!lcs) {
2332 		/*
2333 		 * calculate size for the trailing cluster
2334 		 */
2335 		for(block--; lcs<sblock.fs_contigsumsize; block--, lcs++ ) {
2336 			if(isclr(cg_clustersfree(&acg), block)){
2337 				break;
2338 			}
2339 		}
2340 	}
2341 	if(lcs < sblock.fs_contigsumsize) {
2342 		if(lcs) {
2343 			cg_clustersum(&acg)[lcs]--;
2344 		}
2345 		lcs++;
2346 		cg_clustersum(&acg)[lcs]++;
2347 	}
2348 
2349 	DBG_LEAVE;
2350 	return;
2351 }
2352 
2353 /* *********************************************************** updrefs ***** */
2354 /*
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
2357  * cylinder group.
2358  */
2359 static void
2360 updrefs(int cg, ino_t in, struct gfs_bpp *bp, int fsi, int fso, unsigned int
2361     Nflag)
2362 {
2363 	DBG_FUNC("updrefs")
2364 	unsigned int	ictr, ind2ctr, ind3ctr;
2365 	ufs_daddr_t	*iptr, *ind2ptr, *ind3ptr;
2366 	struct dinode	*ino;
2367 	int	remaining_blocks;
2368 
2369 	DBG_ENTER;
2370 
2371 	/*
2372 	 * XXX We should skip unused inodes even from beeing read from disk
2373 	 *     here by using the bitmap.
2374 	 */
2375 	ino=ginode(in, fsi, cg);
2376 	if(!((ino->di_mode & IFMT)==IFDIR || (ino->di_mode & IFMT)==IFREG ||
2377 	    (ino->di_mode & IFMT)==IFLNK)) {
2378 		DBG_LEAVE;
2379 		return; /* only check DIR, FILE, LINK */
2380 	}
2381 	if(((ino->di_mode & IFMT)==IFLNK) && (ino->di_size<MAXSYMLINKLEN)) {
2382 		DBG_LEAVE;
2383 		return;	/* skip short symlinks */
2384 	}
2385 	if(!ino->di_size) {
2386 		DBG_LEAVE;
2387 		return;	/* skip empty file */
2388 	}
2389 	if(!ino->di_blocks) {
2390 		DBG_LEAVE;
2391 		return;	/* skip empty swiss cheesy file or old fastlink */
2392 	}
2393 	DBG_PRINT2("scg checking inode (%d in %d)\n",
2394 	    in,
2395 	    cg);
2396 
2397 	/*
2398 	 * Start checking all direct blocks.
2399 	 */
2400 	remaining_blocks=howmany(ino->di_size, sblock.fs_bsize);
2401 	for(ictr=0; ictr < MIN(NDADDR, (unsigned int)remaining_blocks);
2402 	    ictr++) {
2403 		iptr=&(ino->di_db[ictr]);
2404 		if(*iptr) {
2405 			cond_bl_upd(iptr, bp, GFS_PS_INODE, fso, Nflag);
2406 		}
2407 	}
2408 	DBG_PRINT0("~~scg direct blocks checked\n");
2409 
2410 	remaining_blocks-=NDADDR;
2411 	if(remaining_blocks<0) {
2412 		DBG_LEAVE;
2413 		return;
2414 	}
2415 	if(ino->di_ib[0]) {
2416 		/*
2417 		 * Start checking first indirect block
2418 		 */
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);
2424 		    ictr++) {
2425 			iptr=&((ufs_daddr_t *)(void *)&i1blk)[ictr];
2426 			if(*iptr) {
2427 				cond_bl_upd(iptr, bp, GFS_PS_IND_BLK_LVL1,
2428 				    fso, Nflag);
2429 			}
2430 		}
2431 	}
2432 	DBG_PRINT0("scg indirect_1 blocks checked\n");
2433 
2434 	remaining_blocks-= howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
2435 	if(remaining_blocks<0) {
2436 		DBG_LEAVE;
2437 		return;
2438 	}
2439 	if(ino->di_ib[1]) {
2440 		/*
2441 		 * Start checking second indirect block
2442 		 */
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];
2449 			if(!*ind2ptr) {
2450 				continue;
2451 			}
2452 			cond_bl_upd(ind2ptr, bp, GFS_PS_IND_BLK_LVL2, fso,
2453 			    Nflag);
2454 			i1_src=fsbtodb(&sblock, *ind2ptr);
2455 			rdfs(i1_src, (size_t)sblock.fs_bsize, (void *)&i1blk,
2456 			    fsi);
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];
2461 				if(*iptr) {
2462 					cond_bl_upd(iptr, bp,
2463 					    GFS_PS_IND_BLK_LVL1, fso, Nflag);
2464 				}
2465 			}
2466 		}
2467 	}
2468 	DBG_PRINT0("scg indirect_2 blocks checked\n");
2469 
2470 #define SQUARE(a) ((a)*(a))
2471 	remaining_blocks-=SQUARE(howmany(sblock.fs_bsize, sizeof(ufs_daddr_t)));
2472 #undef SQUARE
2473 	if(remaining_blocks<0) {
2474 		DBG_LEAVE;
2475 		return;
2476 	}
2477 
2478 	if(ino->di_ib[2]) {
2479 		/*
2480 		 * Start checking third indirect block
2481 		 */
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];
2488 			if(!*ind3ptr) {
2489 				continue;
2490 			}
2491 			cond_bl_upd(ind3ptr, bp, GFS_PS_IND_BLK_LVL3, fso,
2492 			    Nflag);
2493 			i2_src=fsbtodb(&sblock, *ind3ptr);
2494 			rdfs(i2_src, (size_t)sblock.fs_bsize, (void *)&i2blk,
2495 			    fsi);
2496 			for(ind2ctr=0; ind2ctr < howmany(sblock.fs_bsize,
2497 			    sizeof(ufs_daddr_t)); ind2ctr ++) {
2498 				ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)
2499 				    [ind2ctr];
2500 				if(!*ind2ptr) {
2501 					continue;
2502 				}
2503 				cond_bl_upd(ind2ptr, bp, GFS_PS_IND_BLK_LVL2,
2504 				    fso, Nflag);
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)
2512 					    [ictr];
2513 					if(*iptr) {
2514 						cond_bl_upd(iptr, bp,
2515 						    GFS_PS_IND_BLK_LVL1, fso,
2516 						    Nflag);
2517 					}
2518 				}
2519 			}
2520 		}
2521 	}
2522 
2523 	DBG_PRINT0("scg indirect_3 blocks checked\n");
2524 
2525 	DBG_LEAVE;
2526 	return;
2527 }
2528 
2529