xref: /netbsd-src/sbin/resize_ffs/resize_ffs.c (revision 4817a0b0b8fe9612e8ebe21a9bf2d97b95038a97)
1 /*	$NetBSD: resize_ffs.c,v 1.24 2010/12/14 21:49:21 wiz Exp $	*/
2 /* From sources sent on February 17, 2003 */
3 /*-
4  * As its sole author, I explicitly place this code in the public
5  *  domain.  Anyone may use it for any purpose (though I would
6  *  appreciate credit where it is due).
7  *
8  *					der Mouse
9  *
10  *			       mouse@rodents.montreal.qc.ca
11  *		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B
12  */
13 /*
14  * resize_ffs:
15  *
16  * Resize a file system.  Is capable of both growing and shrinking.
17  *
18  * Usage: resize_ffs [-s newsize] [-y] file_system
19  *
20  * Example: resize_ffs -s 29574 /dev/rsd1e
21  *
22  * newsize is in DEV_BSIZE units (ie, disk sectors, usually 512 bytes
23  *  each).
24  *
25  * Note: this currently requires gcc to build, since it is written
26  *  depending on gcc-specific features, notably nested function
27  *  definitions (which in at least a few cases depend on the lexical
28  *  scoping gcc provides, so they can't be trivially moved outside).
29  *
30  * It will not do anything useful with file systems in other than
31  *  host-native byte order.  This really should be fixed (it's largely
32  *  a historical accident; the original version of this program is
33  *  older than bi-endian support in FFS).
34  *
35  * Many thanks go to John Kohl <jtk@NetBSD.org> for finding bugs: the
36  *  one responsible for the "realloccgblk: can't find blk in cyl"
37  *  problem and a more minor one which left fs_dsize wrong when
38  *  shrinking.  (These actually indicate bugs in fsck too - it should
39  *  have caught and fixed them.)
40  *
41  */
42 
43 #include <sys/cdefs.h>
44 #include <sys/disk.h>
45 #include <sys/disklabel.h>
46 #include <sys/dkio.h>
47 #include <sys/ioctl.h>
48 #include <sys/stat.h>
49 #include <sys/mman.h>
50 #include <sys/param.h>		/* MAXFRAG */
51 #include <ufs/ffs/fs.h>
52 #include <ufs/ufs/dir.h>
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ufs/ufs_bswap.h>	/* ufs_rw32 */
55 
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <strings.h>
62 #include <unistd.h>
63 
64 /* new size of file system, in sectors */
65 static uint32_t newsize;
66 
67 /* fd open onto disk device or file */
68 static int fd;
69 
70 /* must we break up big I/O operations - see checksmallio() */
71 static int smallio;
72 
73 /* size of a cg, in bytes, rounded up to a frag boundary */
74 static int cgblksz;
75 
76 /* possible superblock localtions */
77 static int search[] = SBLOCKSEARCH;
78 /* location of the superblock */
79 static off_t where;
80 
81 /* Superblocks. */
82 static struct fs *oldsb;	/* before we started */
83 static struct fs *newsb;	/* copy to work with */
84 /* Buffer to hold the above.  Make sure it's aligned correctly. */
85 static char sbbuf[2 * SBLOCKSIZE]
86 	__attribute__((__aligned__(__alignof__(struct fs))));
87 
88 /* a cg's worth of brand new squeaky-clean inodes */
89 static struct ufs1_dinode *zinodes;
90 
91 /* pointers to the in-core cgs, read off disk and possibly modified */
92 static struct cg **cgs;
93 
94 /* pointer to csum array - the stuff pointed to on-disk by fs_csaddr */
95 static struct csum *csums;
96 
97 /* per-cg flags, indexed by cg number */
98 static unsigned char *cgflags;
99 #define CGF_DIRTY   0x01	/* needs to be written to disk */
100 #define CGF_BLKMAPS 0x02	/* block bitmaps need rebuilding */
101 #define CGF_INOMAPS 0x04	/* inode bitmaps need rebuilding */
102 
103 /* when shrinking, these two arrays record how we want blocks to move.	 */
104 /*  if blkmove[i] is j, the frag that started out as frag #i should end	 */
105 /*  up as frag #j.  inomove[i]=j means, similarly, that the inode that	 */
106 /*  started out as inode i should end up as inode j.			 */
107 static unsigned int *blkmove;
108 static unsigned int *inomove;
109 
110 /* in-core copies of all inodes in the fs, indexed by inumber */
111 static struct ufs1_dinode *inodes;
112 
113 /* per-inode flags, indexed by inumber */
114 static unsigned char *iflags;
115 #define IF_DIRTY  0x01		/* needs to be written to disk */
116 #define IF_BDIRTY 0x02		/* like DIRTY, but is set on first inode in a
117 				 * block of inodes, and applies to the whole
118 				 * block. */
119 
120 /* resize_ffs works directly on dinodes, adapt blksize() */
121 #define dblksize(fs, dip, lbn) \
122     (((lbn) >= NDADDR || (dip)->di_size >= lblktosize(fs, (lbn) + 1)) \
123     ? (fs)->fs_bsize \
124     : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
125 
126 
127 /*
128  * Number of disk sectors per block/fragment; assumes DEV_BSIZE byte
129  * sector size.
130  */
131 #define NSPB(fs)	((fs)->fs_old_nspf << (fs)->fs_fragshift)
132 #define NSPF(fs)	((fs)->fs_old_nspf)
133 
134 /* global flags */
135 int is_ufs2 = 0;
136 int needswap = 0;
137 
138 static void usage(void) __dead;
139 
140 /*
141  * See if we need to break up large I/O operations.  This should never
142  *  be needed, but under at least one <version,platform> combination,
143  *  large enough disk transfers to the raw device hang.  So if we're
144  *  talking to a character special device, play it safe; in this case,
145  *  readat() and writeat() break everything up into pieces no larger
146  *  than 8K, doing multiple syscalls for larger operations.
147  */
148 static void
149 checksmallio(void)
150 {
151 	struct stat stb;
152 
153 	fstat(fd, &stb);
154 	smallio = ((stb.st_mode & S_IFMT) == S_IFCHR);
155 }
156 
157 static int
158 isplainfile(void)
159 {
160 	struct stat stb;
161 
162 	fstat(fd, &stb);
163 	return S_ISREG(stb.st_mode);
164 }
165 /*
166  * Read size bytes starting at blkno into buf.  blkno is in DEV_BSIZE
167  *  units, ie, after fsbtodb(); size is in bytes.
168  */
169 static void
170 readat(off_t blkno, void *buf, int size)
171 {
172 	/* Seek to the correct place. */
173 	if (lseek(fd, blkno * DEV_BSIZE, L_SET) < 0)
174 		err(EXIT_FAILURE, "lseek failed");
175 
176 	/* See if we have to break up the transfer... */
177 	if (smallio) {
178 		char *bp;	/* pointer into buf */
179 		int left;	/* bytes left to go */
180 		int n;		/* number to do this time around */
181 		int rv;		/* syscall return value */
182 		bp = buf;
183 		left = size;
184 		while (left > 0) {
185 			n = (left > 8192) ? 8192 : left;
186 			rv = read(fd, bp, n);
187 			if (rv < 0)
188 				err(EXIT_FAILURE, "read failed");
189 			if (rv != n)
190 				errx(EXIT_FAILURE,
191 				    "read: wanted %d, got %d", n, rv);
192 			bp += n;
193 			left -= n;
194 		}
195 	} else {
196 		int rv;
197 		rv = read(fd, buf, size);
198 		if (rv < 0)
199 			err(EXIT_FAILURE, "read failed");
200 		if (rv != size)
201 			errx(EXIT_FAILURE, "read: wanted %d, got %d", size, rv);
202 	}
203 }
204 /*
205  * Write size bytes from buf starting at blkno.  blkno is in DEV_BSIZE
206  *  units, ie, after fsbtodb(); size is in bytes.
207  */
208 static void
209 writeat(off_t blkno, const void *buf, int size)
210 {
211 	/* Seek to the correct place. */
212 	if (lseek(fd, blkno * DEV_BSIZE, L_SET) < 0)
213 		err(EXIT_FAILURE, "lseek failed");
214 	/* See if we have to break up the transfer... */
215 	if (smallio) {
216 		const char *bp;	/* pointer into buf */
217 		int left;	/* bytes left to go */
218 		int n;		/* number to do this time around */
219 		int rv;		/* syscall return value */
220 		bp = buf;
221 		left = size;
222 		while (left > 0) {
223 			n = (left > 8192) ? 8192 : left;
224 			rv = write(fd, bp, n);
225 			if (rv < 0)
226 				err(EXIT_FAILURE, "write failed");
227 			if (rv != n)
228 				errx(EXIT_FAILURE,
229 				    "write: wanted %d, got %d", n, rv);
230 			bp += n;
231 			left -= n;
232 		}
233 	} else {
234 		int rv;
235 		rv = write(fd, buf, size);
236 		if (rv < 0)
237 			err(EXIT_FAILURE, "write failed");
238 		if (rv != size)
239 			errx(EXIT_FAILURE,
240 			    "write: wanted %d, got %d", size, rv);
241 	}
242 }
243 /*
244  * Never-fail versions of malloc() and realloc(), and an allocation
245  *  routine (which also never fails) for allocating memory that will
246  *  never be freed until exit.
247  */
248 
249 /*
250  * Never-fail malloc.
251  */
252 static void *
253 nfmalloc(size_t nb, const char *tag)
254 {
255 	void *rv;
256 
257 	rv = malloc(nb);
258 	if (rv)
259 		return (rv);
260 	err(EXIT_FAILURE, "Can't allocate %lu bytes for %s",
261 	    (unsigned long int) nb, tag);
262 }
263 /*
264  * Never-fail realloc.
265  */
266 static void *
267 nfrealloc(void *blk, size_t nb, const char *tag)
268 {
269 	void *rv;
270 
271 	rv = realloc(blk, nb);
272 	if (rv)
273 		return (rv);
274 	err(EXIT_FAILURE, "Can't re-allocate %lu bytes for %s",
275 	    (unsigned long int) nb, tag);
276 }
277 /*
278  * Allocate memory that will never be freed or reallocated.  Arguably
279  *  this routine should handle small allocations by chopping up pages,
280  *  but that's not worth the bother; it's not called more than a
281  *  handful of times per run, and if the allocations are that small the
282  *  waste in giving each one its own page is ignorable.
283  */
284 static void *
285 alloconce(size_t nb, const char *tag)
286 {
287 	void *rv;
288 
289 	rv = mmap(0, nb, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
290 	if (rv != MAP_FAILED)
291 		return (rv);
292 	err(EXIT_FAILURE, "Can't map %lu bytes for %s",
293 	    (unsigned long int) nb, tag);
294 }
295 /*
296  * Load the cgs and csums off disk.  Also allocates the space to load
297  *  them into and initializes the per-cg flags.
298  */
299 static void
300 loadcgs(void)
301 {
302 	int cg;
303 	char *cgp;
304 
305 	cgblksz = roundup(oldsb->fs_cgsize, oldsb->fs_fsize);
306 	cgs = nfmalloc(oldsb->fs_ncg * sizeof(struct cg *), "cg pointers");
307 	cgp = alloconce(oldsb->fs_ncg * cgblksz, "cgs");
308 	cgflags = nfmalloc(oldsb->fs_ncg, "cg flags");
309 	csums = nfmalloc(oldsb->fs_cssize, "cg summary");
310 	for (cg = 0; cg < oldsb->fs_ncg; cg++) {
311 		cgs[cg] = (struct cg *) cgp;
312 		readat(fsbtodb(oldsb, cgtod(oldsb, cg)), cgp, cgblksz);
313 		cgflags[cg] = 0;
314 		cgp += cgblksz;
315 	}
316 	readat(fsbtodb(oldsb, oldsb->fs_csaddr), csums, oldsb->fs_cssize);
317 }
318 /*
319  * Set n bits, starting with bit #base, in the bitmap pointed to by
320  *  bitvec (which is assumed to be large enough to include bits base
321  *  through base+n-1).
322  */
323 static void
324 set_bits(unsigned char *bitvec, unsigned int base, unsigned int n)
325 {
326 	if (n < 1)
327 		return;		/* nothing to do */
328 	if (base & 7) {		/* partial byte at beginning */
329 		if (n <= 8 - (base & 7)) {	/* entirely within one byte */
330 			bitvec[base >> 3] |= (~((~0U) << n)) << (base & 7);
331 			return;
332 		}
333 		bitvec[base >> 3] |= (~0U) << (base & 7);
334 		n -= 8 - (base & 7);
335 		base = (base & ~7) + 8;
336 	}
337 	if (n >= 8) {		/* do full bytes */
338 		memset(bitvec + (base >> 3), 0xff, n >> 3);
339 		base += n & ~7;
340 		n &= 7;
341 	}
342 	if (n) {		/* partial byte at end */
343 		bitvec[base >> 3] |= ~((~0U) << n);
344 	}
345 }
346 /*
347  * Clear n bits, starting with bit #base, in the bitmap pointed to by
348  *  bitvec (which is assumed to be large enough to include bits base
349  *  through base+n-1).  Code parallels set_bits().
350  */
351 static void
352 clr_bits(unsigned char *bitvec, int base, int n)
353 {
354 	if (n < 1)
355 		return;
356 	if (base & 7) {
357 		if (n <= 8 - (base & 7)) {
358 			bitvec[base >> 3] &= ~((~((~0U) << n)) << (base & 7));
359 			return;
360 		}
361 		bitvec[base >> 3] &= ~((~0U) << (base & 7));
362 		n -= 8 - (base & 7);
363 		base = (base & ~7) + 8;
364 	}
365 	if (n >= 8) {
366 		bzero(bitvec + (base >> 3), n >> 3);
367 		base += n & ~7;
368 		n &= 7;
369 	}
370 	if (n) {
371 		bitvec[base >> 3] &= (~0U) << n;
372 	}
373 }
374 /*
375  * Test whether bit #bit is set in the bitmap pointed to by bitvec.
376  */
377 static int
378 bit_is_set(unsigned char *bitvec, int bit)
379 {
380 	return (bitvec[bit >> 3] & (1 << (bit & 7)));
381 }
382 /*
383  * Test whether bit #bit is clear in the bitmap pointed to by bitvec.
384  */
385 static int
386 bit_is_clr(unsigned char *bitvec, int bit)
387 {
388 	return (!bit_is_set(bitvec, bit));
389 }
390 /*
391  * Test whether a whole block of bits is set in a bitmap.  This is
392  *  designed for testing (aligned) disk blocks in a bit-per-frag
393  *  bitmap; it has assumptions wired into it based on that, essentially
394  *  that the entire block fits into a single byte.  This returns true
395  *  iff _all_ the bits are set; it is not just the complement of
396  *  blk_is_clr on the same arguments (unless blkfrags==1).
397  */
398 static int
399 blk_is_set(unsigned char *bitvec, int blkbase, int blkfrags)
400 {
401 	unsigned int mask;
402 
403 	mask = (~((~0U) << blkfrags)) << (blkbase & 7);
404 	return ((bitvec[blkbase >> 3] & mask) == mask);
405 }
406 /*
407  * Test whether a whole block of bits is clear in a bitmap.  See
408  *  blk_is_set (above) for assumptions.  This returns true iff _all_
409  *  the bits are clear; it is not just the complement of blk_is_set on
410  *  the same arguments (unless blkfrags==1).
411  */
412 static int
413 blk_is_clr(unsigned char *bitvec, int blkbase, int blkfrags)
414 {
415 	unsigned int mask;
416 
417 	mask = (~((~0U) << blkfrags)) << (blkbase & 7);
418 	return ((bitvec[blkbase >> 3] & mask) == 0);
419 }
420 /*
421  * Initialize a new cg.  Called when growing.  Assumes memory has been
422  *  allocated but not otherwise set up.  This code sets the fields of
423  *  the cg, initializes the bitmaps (and cluster summaries, if
424  *  applicable), updates both per-cylinder summary info and the global
425  *  summary info in newsb; it also writes out new inodes for the cg.
426  *
427  * This code knows it can never be called for cg 0, which makes it a
428  *  bit simpler than it would otherwise be.
429  */
430 static void
431 initcg(int cgn)
432 {
433 	struct cg *cg;		/* The in-core cg, of course */
434 	int base;		/* Disk address of cg base */
435 	int dlow;		/* Size of pre-cg data area */
436 	int dhigh;		/* Offset of post-inode data area, from base */
437 	int dmax;		/* Offset of end of post-inode data area */
438 	int i;			/* Generic loop index */
439 	int n;			/* Generic count */
440 
441 	cg = cgs[cgn];
442 	/* Place the data areas */
443 	base = cgbase(newsb, cgn);
444 	dlow = cgsblock(newsb, cgn) - base;
445 	dhigh = cgdmin(newsb, cgn) - base;
446 	dmax = newsb->fs_size - base;
447 	if (dmax > newsb->fs_fpg)
448 		dmax = newsb->fs_fpg;
449 	/*
450          * Clear out the cg - assumes all-0-bytes is the correct way
451          * to initialize fields we don't otherwise touch, which is
452          * perhaps not the right thing to do, but it's what fsck and
453          * mkfs do.
454          */
455 	bzero(cg, newsb->fs_cgsize);
456 	cg->cg_old_time = newsb->fs_time;
457 	if (newsb->fs_old_flags & FS_FLAGS_UPDATED)
458 		cg->cg_time = newsb->fs_time;
459 	cg->cg_magic = CG_MAGIC;
460 	cg->cg_cgx = cgn;
461 	cg->cg_old_ncyl = newsb->fs_old_cpg;
462 	/* Update the cg_old_ncyl value for the last cylinder. */
463 	if (cgn == newsb->fs_ncg - 1) {
464 		if ((newsb->fs_old_flags & FS_FLAGS_UPDATED) == 0)
465 			cg->cg_old_ncyl = newsb->fs_old_ncyl % newsb->fs_old_cpg;
466 	}
467 	cg->cg_old_niblk = newsb->fs_ipg;
468 	cg->cg_ndblk = dmax;
469 	/* Set up the bitmap pointers.  We have to be careful to lay out the
470 	 * cg _exactly_ the way mkfs and fsck do it, since fsck compares the
471 	 * _entire_ cg against a recomputed cg, and whines if there is any
472 	 * mismatch, including the bitmap offsets. */
473 	/* XXX update this comment when fsck is fixed */
474 	cg->cg_old_btotoff = &cg->cg_space[0] - (unsigned char *) cg;
475 	cg->cg_old_boff = cg->cg_old_btotoff
476 	    + (newsb->fs_old_cpg * sizeof(int32_t));
477 	cg->cg_iusedoff = cg->cg_old_boff +
478 	    (newsb->fs_old_cpg * newsb->fs_old_nrpos * sizeof(int16_t));
479 	cg->cg_freeoff = cg->cg_iusedoff + howmany(newsb->fs_ipg, NBBY);
480 	if (newsb->fs_contigsumsize > 0) {
481 		cg->cg_nclusterblks = cg->cg_ndblk / newsb->fs_frag;
482 		cg->cg_clustersumoff = cg->cg_freeoff +
483 		    howmany(newsb->fs_old_cpg * newsb->fs_old_spc / NSPF(newsb),
484 		    NBBY) - sizeof(int32_t);
485 		cg->cg_clustersumoff =
486 		    roundup(cg->cg_clustersumoff, sizeof(int32_t));
487 		cg->cg_clusteroff = cg->cg_clustersumoff +
488 		    ((newsb->fs_contigsumsize + 1) * sizeof(int32_t));
489 		cg->cg_nextfreeoff = cg->cg_clusteroff +
490 		    howmany(newsb->fs_old_cpg * newsb->fs_old_spc / NSPB(newsb),
491 		    NBBY);
492 		n = dlow / newsb->fs_frag;
493 		if (n > 0) {
494 			set_bits(cg_clustersfree(cg, 0), 0, n);
495 			cg_clustersum(cg, 0)[(n > newsb->fs_contigsumsize) ?
496 			    newsb->fs_contigsumsize : n]++;
497 		}
498 	} else {
499 		cg->cg_nextfreeoff = cg->cg_freeoff +
500 		    howmany(newsb->fs_old_cpg * newsb->fs_old_spc / NSPF(newsb),
501 		    NBBY);
502 	}
503 	/* Mark the data areas as free; everything else is marked busy by the
504 	 * bzero up at the top. */
505 	set_bits(cg_blksfree(cg, 0), 0, dlow);
506 	set_bits(cg_blksfree(cg, 0), dhigh, dmax - dhigh);
507 	/* Initialize summary info */
508 	cg->cg_cs.cs_ndir = 0;
509 	cg->cg_cs.cs_nifree = newsb->fs_ipg;
510 	cg->cg_cs.cs_nbfree = dlow / newsb->fs_frag;
511 	cg->cg_cs.cs_nffree = 0;
512 
513 	/* This is the simplest way of doing this; we perhaps could compute
514 	 * the correct cg_blktot()[] and cg_blks()[] values other ways, but it
515 	 * would be complicated and hardly seems worth the effort.  (The
516 	 * reason there isn't frag-at-beginning and frag-at-end code here,
517 	 * like the code below for the post-inode data area, is that the
518 	 * pre-sb data area always starts at 0, and thus is block-aligned, and
519 	 * always ends at the sb, which is block-aligned.) */
520 	for (i = 0; i < dlow; i += newsb->fs_frag) {
521 		old_cg_blktot(cg, 0)[old_cbtocylno(newsb, i)]++;
522 		old_cg_blks(newsb, cg,
523 		    old_cbtocylno(newsb, i), 0)[old_cbtorpos(newsb, i)]++;
524 	}
525 	/* Deal with a partial block at the beginning of the post-inode area.
526 	 * I'm not convinced this can happen - I think the inodes are always
527 	 * block-aligned and always an integral number of blocks - but it's
528 	 * cheap to do the right thing just in case. */
529 	if (dhigh % newsb->fs_frag) {
530 		n = newsb->fs_frag - (dhigh % newsb->fs_frag);
531 		cg->cg_frsum[n]++;
532 		cg->cg_cs.cs_nffree += n;
533 		dhigh += n;
534 	}
535 	n = (dmax - dhigh) / newsb->fs_frag;
536 	/* We have n full-size blocks in the post-inode data area. */
537 	if (n > 0) {
538 		cg->cg_cs.cs_nbfree += n;
539 		if (newsb->fs_contigsumsize > 0) {
540 			i = dhigh / newsb->fs_frag;
541 			set_bits(cg_clustersfree(cg, 0), i, n);
542 			cg_clustersum(cg, 0)[(n > newsb->fs_contigsumsize) ?
543 			    newsb->fs_contigsumsize : n]++;
544 		}
545 		for (i = n; i > 0; i--) {
546 			old_cg_blktot(cg, 0)[old_cbtocylno(newsb, dhigh)]++;
547 			old_cg_blks(newsb, cg,
548 			    old_cbtocylno(newsb, dhigh), 0)[old_cbtorpos(newsb,
549 				dhigh)]++;
550 			dhigh += newsb->fs_frag;
551 		}
552 	}
553 	/* Deal with any leftover frag at the end of the cg. */
554 	i = dmax - dhigh;
555 	if (i) {
556 		cg->cg_frsum[i]++;
557 		cg->cg_cs.cs_nffree += i;
558 	}
559 	/* Update the csum info. */
560 	csums[cgn] = cg->cg_cs;
561 	newsb->fs_cstotal.cs_nffree += cg->cg_cs.cs_nffree;
562 	newsb->fs_cstotal.cs_nbfree += cg->cg_cs.cs_nbfree;
563 	newsb->fs_cstotal.cs_nifree += cg->cg_cs.cs_nifree;
564 	/* Write out the cleared inodes. */
565 	writeat(fsbtodb(newsb, cgimin(newsb, cgn)), zinodes,
566 	    newsb->fs_ipg * sizeof(struct ufs1_dinode));
567 	/* Dirty the cg. */
568 	cgflags[cgn] |= CGF_DIRTY;
569 }
570 /*
571  * Find free space, at least nfrags consecutive frags of it.  Pays no
572  *  attention to block boundaries, but refuses to straddle cg
573  *  boundaries, even if the disk blocks involved are in fact
574  *  consecutive.  Return value is the frag number of the first frag of
575  *  the block, or -1 if no space was found.  Uses newsb for sb values,
576  *  and assumes the cgs[] structures correctly describe the area to be
577  *  searched.
578  *
579  * XXX is there a bug lurking in the ignoring of block boundaries by
580  *  the routine used by fragmove() in evict_data()?  Can an end-of-file
581  *  frag legally straddle a block boundary?  If not, this should be
582  *  cloned and fixed to stop at block boundaries for that use.  The
583  *  current one may still be needed for csum info motion, in case that
584  *  takes up more than a whole block (is the csum info allowed to begin
585  *  partway through a block and continue into the following block?).
586  *
587  * If we wrap off the end of the file system back to the beginning, we
588  *  can end up searching the end of the file system twice.  I ignore
589  *  this inefficiency, since if that happens we're going to croak with
590  *  a no-space error anyway, so it happens at most once.
591  */
592 static int
593 find_freespace(unsigned int nfrags)
594 {
595 	static int hand = 0;	/* hand rotates through all frags in the fs */
596 	int cgsize;		/* size of the cg hand currently points into */
597 	int cgn;		/* number of cg hand currently points into */
598 	int fwc;		/* frag-within-cg number of frag hand points
599 				 * to */
600 	int run;		/* length of run of free frags seen so far */
601 	int secondpass;		/* have we wrapped from end of fs to
602 				 * beginning? */
603 	unsigned char *bits;	/* cg_blksfree()[] for cg hand points into */
604 
605 	cgn = dtog(newsb, hand);
606 	fwc = dtogd(newsb, hand);
607 	secondpass = (hand == 0);
608 	run = 0;
609 	bits = cg_blksfree(cgs[cgn], 0);
610 	cgsize = cgs[cgn]->cg_ndblk;
611 	while (1) {
612 		if (bit_is_set(bits, fwc)) {
613 			run++;
614 			if (run >= nfrags)
615 				return (hand + 1 - run);
616 		} else {
617 			run = 0;
618 		}
619 		hand++;
620 		fwc++;
621 		if (fwc >= cgsize) {
622 			fwc = 0;
623 			cgn++;
624 			if (cgn >= newsb->fs_ncg) {
625 				hand = 0;
626 				if (secondpass)
627 					return (-1);
628 				secondpass = 1;
629 				cgn = 0;
630 			}
631 			bits = cg_blksfree(cgs[cgn], 0);
632 			cgsize = cgs[cgn]->cg_ndblk;
633 			run = 0;
634 		}
635 	}
636 }
637 /*
638  * Find a free block of disk space.  Finds an entire block of frags,
639  *  all of which are free.  Return value is the frag number of the
640  *  first frag of the block, or -1 if no space was found.  Uses newsb
641  *  for sb values, and assumes the cgs[] structures correctly describe
642  *  the area to be searched.
643  *
644  * See find_freespace(), above, for remarks about hand wrapping around.
645  */
646 static int
647 find_freeblock(void)
648 {
649 	static int hand = 0;	/* hand rotates through all frags in fs */
650 	int cgn;		/* cg number of cg hand points into */
651 	int fwc;		/* frag-within-cg number of frag hand points
652 				 * to */
653 	int cgsize;		/* size of cg hand points into */
654 	int secondpass;		/* have we wrapped from end to beginning? */
655 	unsigned char *bits;	/* cg_blksfree()[] for cg hand points into */
656 
657 	cgn = dtog(newsb, hand);
658 	fwc = dtogd(newsb, hand);
659 	secondpass = (hand == 0);
660 	bits = cg_blksfree(cgs[cgn], 0);
661 	cgsize = blknum(newsb, cgs[cgn]->cg_ndblk);
662 	while (1) {
663 		if (blk_is_set(bits, fwc, newsb->fs_frag))
664 			return (hand);
665 		fwc += newsb->fs_frag;
666 		hand += newsb->fs_frag;
667 		if (fwc >= cgsize) {
668 			fwc = 0;
669 			cgn++;
670 			if (cgn >= newsb->fs_ncg) {
671 				hand = 0;
672 				if (secondpass)
673 					return (-1);
674 				secondpass = 1;
675 				cgn = 0;
676 			}
677 			bits = cg_blksfree(cgs[cgn], 0);
678 			cgsize = blknum(newsb, cgs[cgn]->cg_ndblk);
679 		}
680 	}
681 }
682 /*
683  * Find a free inode, returning its inumber or -1 if none was found.
684  *  Uses newsb for sb values, and assumes the cgs[] structures
685  *  correctly describe the area to be searched.
686  *
687  * See find_freespace(), above, for remarks about hand wrapping around.
688  */
689 static int
690 find_freeinode(void)
691 {
692 	static int hand = 0;	/* hand rotates through all inodes in fs */
693 	int cgn;		/* cg number of cg hand points into */
694 	int iwc;		/* inode-within-cg number of inode hand points
695 				 * to */
696 	int secondpass;		/* have we wrapped from end to beginning? */
697 	unsigned char *bits;	/* cg_inosused()[] for cg hand points into */
698 
699 	cgn = hand / newsb->fs_ipg;
700 	iwc = hand % newsb->fs_ipg;
701 	secondpass = (hand == 0);
702 	bits = cg_inosused(cgs[cgn], 0);
703 	while (1) {
704 		if (bit_is_clr(bits, iwc))
705 			return (hand);
706 		hand++;
707 		iwc++;
708 		if (iwc >= newsb->fs_ipg) {
709 			iwc = 0;
710 			cgn++;
711 			if (cgn >= newsb->fs_ncg) {
712 				hand = 0;
713 				if (secondpass)
714 					return (-1);
715 				secondpass = 1;
716 				cgn = 0;
717 			}
718 			bits = cg_inosused(cgs[cgn], 0);
719 		}
720 	}
721 }
722 /*
723  * Mark a frag as free.  Sets the frag's bit in the cg_blksfree bitmap
724  *  for the appropriate cg, and marks the cg as dirty.
725  */
726 static void
727 free_frag(int fno)
728 {
729 	int cgn;
730 
731 	cgn = dtog(newsb, fno);
732 	set_bits(cg_blksfree(cgs[cgn], 0), dtogd(newsb, fno), 1);
733 	cgflags[cgn] |= CGF_DIRTY | CGF_BLKMAPS;
734 }
735 /*
736  * Allocate a frag.  Clears the frag's bit in the cg_blksfree bitmap
737  *  for the appropriate cg, and marks the cg as dirty.
738  */
739 static void
740 alloc_frag(int fno)
741 {
742 	int cgn;
743 
744 	cgn = dtog(newsb, fno);
745 	clr_bits(cg_blksfree(cgs[cgn], 0), dtogd(newsb, fno), 1);
746 	cgflags[cgn] |= CGF_DIRTY | CGF_BLKMAPS;
747 }
748 /*
749  * Fix up the csum array.  If shrinking, this involves freeing zero or
750  *  more frags; if growing, it involves allocating them, or if the
751  *  frags being grown into aren't free, finding space elsewhere for the
752  *  csum info.  (If the number of occupied frags doesn't change,
753  *  nothing happens here.)
754  */
755 static void
756 csum_fixup(void)
757 {
758 	int nold;		/* # frags in old csum info */
759 	int ntot;		/* # frags in new csum info */
760 	int nnew;		/* ntot-nold */
761 	int newloc;		/* new location for csum info, if necessary */
762 	int i;			/* generic loop index */
763 	int j;			/* generic loop index */
764 	int f;			/* "from" frag number, if moving */
765 	int t;			/* "to" frag number, if moving */
766 	int cgn;		/* cg number, used when shrinking */
767 
768 	ntot = howmany(newsb->fs_cssize, newsb->fs_fsize);
769 	nold = howmany(oldsb->fs_cssize, newsb->fs_fsize);
770 	nnew = ntot - nold;
771 	/* First, if there's no change in frag counts, it's easy. */
772 	if (nnew == 0)
773 		return;
774 	/* Next, if we're shrinking, it's almost as easy.  Just free up any
775 	 * frags in the old area we no longer need. */
776 	if (nnew < 0) {
777 		for ((i = newsb->fs_csaddr + ntot - 1), (j = nnew);
778 		    j < 0;
779 		    i--, j++) {
780 			free_frag(i);
781 		}
782 		return;
783 	}
784 	/* We must be growing.  Check to see that the new csum area fits
785 	 * within the file system.  I think this can never happen, since for
786 	 * the csum area to grow, we must be adding at least one cg, so the
787 	 * old csum area can't be this close to the end of the new file system.
788 	 * But it's a cheap check. */
789 	/* XXX what if csum info is at end of cg and grows into next cg, what
790 	 * if it spills over onto the next cg's backup superblock?  Can this
791 	 * happen? */
792 	if (newsb->fs_csaddr + ntot <= newsb->fs_size) {
793 		/* Okay, it fits - now,  see if the space we want is free. */
794 		for ((i = newsb->fs_csaddr + nold), (j = nnew);
795 		    j > 0;
796 		    i++, j--) {
797 			cgn = dtog(newsb, i);
798 			if (bit_is_clr(cg_blksfree(cgs[cgn], 0),
799 				dtogd(newsb, i)))
800 				break;
801 		}
802 		if (j <= 0) {
803 			/* Win win - all the frags we want are free. Allocate
804 			 * 'em and we're all done.  */
805 			for ((i = newsb->fs_csaddr + ntot - nnew), (j = nnew); j > 0; i++, j--) {
806 				alloc_frag(i);
807 			}
808 			return;
809 		}
810 	}
811 	/* We have to move the csum info, sigh.  Look for new space, free old
812 	 * space, and allocate new.  Update fs_csaddr.  We don't copy anything
813 	 * on disk at this point; the csum info will be written to the
814 	 * then-current fs_csaddr as part of the final flush. */
815 	newloc = find_freespace(ntot);
816 	if (newloc < 0) {
817 		printf("Sorry, no space available for new csums\n");
818 		exit(EXIT_FAILURE);
819 	}
820 	for (i = 0, f = newsb->fs_csaddr, t = newloc; i < ntot; i++, f++, t++) {
821 		if (i < nold) {
822 			free_frag(f);
823 		}
824 		alloc_frag(t);
825 	}
826 	newsb->fs_csaddr = newloc;
827 }
828 /*
829  * Recompute newsb->fs_dsize.  Just scans all cgs, adding the number of
830  *  data blocks in that cg to the total.
831  */
832 static void
833 recompute_fs_dsize(void)
834 {
835 	int i;
836 
837 	newsb->fs_dsize = 0;
838 	for (i = 0; i < newsb->fs_ncg; i++) {
839 		int dlow;	/* size of before-sb data area */
840 		int dhigh;	/* offset of post-inode data area */
841 		int dmax;	/* total size of cg */
842 		int base;	/* base of cg, since cgsblock() etc add it in */
843 		base = cgbase(newsb, i);
844 		dlow = cgsblock(newsb, i) - base;
845 		dhigh = cgdmin(newsb, i) - base;
846 		dmax = newsb->fs_size - base;
847 		if (dmax > newsb->fs_fpg)
848 			dmax = newsb->fs_fpg;
849 		newsb->fs_dsize += dlow + dmax - dhigh;
850 	}
851 	/* Space in cg 0 before cgsblock is boot area, not free space! */
852 	newsb->fs_dsize -= cgsblock(newsb, 0) - cgbase(newsb, 0);
853 	/* And of course the csum info takes up space. */
854 	newsb->fs_dsize -= howmany(newsb->fs_cssize, newsb->fs_fsize);
855 }
856 /*
857  * Return the current time.  We call this and assign, rather than
858  *  calling time() directly, as insulation against OSes where fs_time
859  *  is not a time_t.
860  */
861 static time_t
862 timestamp(void)
863 {
864 	time_t t;
865 
866 	time(&t);
867 	return (t);
868 }
869 /*
870  * Grow the file system.
871  */
872 static void
873 grow(void)
874 {
875 	int i;
876 
877 	/* Update the timestamp. */
878 	newsb->fs_time = timestamp();
879 	/* Allocate and clear the new-inode area, in case we add any cgs. */
880 	zinodes = alloconce(newsb->fs_ipg * sizeof(struct ufs1_dinode),
881                             "zeroed inodes");
882 	bzero(zinodes, newsb->fs_ipg * sizeof(struct ufs1_dinode));
883 	/* Update the size. */
884 	newsb->fs_size = dbtofsb(newsb, newsize);
885 	/* Did we actually not grow?  (This can happen if newsize is less than
886 	 * a frag larger than the old size - unlikely, but no excuse to
887 	 * misbehave if it happens.) */
888 	if (newsb->fs_size == oldsb->fs_size) {
889 		printf("New fs size %"PRIu64" = odl fs size %"PRIu64
890 		    ", not growing.\n", newsb->fs_size, oldsb->fs_size);
891 		return;
892 	}
893 	/* Check that the new last sector (frag, actually) is writable.  Since
894 	 * it's at least one frag larger than it used to be, we know we aren't
895 	 * overwriting anything important by this.  (The choice of sbbuf as
896 	 * what to write is irrelevant; it's just something handy that's known
897 	 * to be at least one frag in size.) */
898 	writeat(fsbtodb(newsb,newsb->fs_size - 1), &sbbuf, newsb->fs_fsize);
899 	/* Update fs_old_ncyl and fs_ncg. */
900 	newsb->fs_old_ncyl = howmany(newsb->fs_size * NSPF(newsb),
901 	    newsb->fs_old_spc);
902 	newsb->fs_ncg = howmany(newsb->fs_old_ncyl, newsb->fs_old_cpg);
903 	/* Does the last cg end before the end of its inode area? There is no
904 	 * reason why this couldn't be handled, but it would complicate a lot
905 	 * of code (in all file system code - fsck, kernel, etc) because of the
906 	 * potential partial inode area, and the gain in space would be
907 	 * minimal, at most the pre-sb data area. */
908 	if (cgdmin(newsb, newsb->fs_ncg - 1) > newsb->fs_size) {
909 		newsb->fs_ncg--;
910 		newsb->fs_old_ncyl = newsb->fs_ncg * newsb->fs_old_cpg;
911 		newsb->fs_size = (newsb->fs_old_ncyl * newsb->fs_old_spc)
912 		    / NSPF(newsb);
913 		printf("Warning: last cylinder group is too small;\n");
914 		printf("    dropping it.  New size = %lu.\n",
915 		    (unsigned long int) fsbtodb(newsb, newsb->fs_size));
916 	}
917 	/* Find out how big the csum area is, and realloc csums if bigger. */
918 	newsb->fs_cssize = fragroundup(newsb,
919 	    newsb->fs_ncg * sizeof(struct csum));
920 	if (newsb->fs_cssize > oldsb->fs_cssize)
921 		csums = nfrealloc(csums, newsb->fs_cssize, "new cg summary");
922 	/* If we're adding any cgs, realloc structures and set up the new cgs. */
923 	if (newsb->fs_ncg > oldsb->fs_ncg) {
924 		char *cgp;
925 		cgs = nfrealloc(cgs, newsb->fs_ncg * sizeof(struct cg *),
926                                 "cg pointers");
927 		cgflags = nfrealloc(cgflags, newsb->fs_ncg, "cg flags");
928 		bzero(cgflags + oldsb->fs_ncg, newsb->fs_ncg - oldsb->fs_ncg);
929 		cgp = alloconce((newsb->fs_ncg - oldsb->fs_ncg) * cgblksz,
930                                 "cgs");
931 		for (i = oldsb->fs_ncg; i < newsb->fs_ncg; i++) {
932 			cgs[i] = (struct cg *) cgp;
933 			initcg(i);
934 			cgp += cgblksz;
935 		}
936 		cgs[oldsb->fs_ncg - 1]->cg_old_ncyl = oldsb->fs_old_cpg;
937 		cgflags[oldsb->fs_ncg - 1] |= CGF_DIRTY;
938 	}
939 	/* If the old fs ended partway through a cg, we have to update the old
940 	 * last cg (though possibly not to a full cg!). */
941 	if (oldsb->fs_size % oldsb->fs_fpg) {
942 		struct cg *cg;
943 		int newcgsize;
944 		int prevcgtop;
945 		int oldcgsize;
946 		cg = cgs[oldsb->fs_ncg - 1];
947 		cgflags[oldsb->fs_ncg - 1] |= CGF_DIRTY | CGF_BLKMAPS;
948 		prevcgtop = oldsb->fs_fpg * (oldsb->fs_ncg - 1);
949 		newcgsize = newsb->fs_size - prevcgtop;
950 		if (newcgsize > newsb->fs_fpg)
951 			newcgsize = newsb->fs_fpg;
952 		oldcgsize = oldsb->fs_size % oldsb->fs_fpg;
953 		set_bits(cg_blksfree(cg, 0), oldcgsize, newcgsize - oldcgsize);
954 		cg->cg_old_ncyl = oldsb->fs_old_cpg;
955 		cg->cg_ndblk = newcgsize;
956 	}
957 	/* Fix up the csum info, if necessary. */
958 	csum_fixup();
959 	/* Make fs_dsize match the new reality. */
960 	recompute_fs_dsize();
961 }
962 /*
963  * Call (*fn)() for each inode, passing the inode and its inumber.  The
964  *  number of cylinder groups is pased in, so this can be used to map
965  *  over either the old or the new file system's set of inodes.
966  */
967 static void
968 map_inodes(void (*fn) (struct ufs1_dinode * di, unsigned int, void *arg),
969 	   int ncg, void *cbarg) {
970 	int i;
971 	int ni;
972 
973 	ni = oldsb->fs_ipg * ncg;
974 	for (i = 0; i < ni; i++)
975 		(*fn) (inodes + i, i, cbarg);
976 }
977 /* Values for the third argument to the map function for
978  * map_inode_data_blocks.  MDB_DATA indicates the block is contains
979  * file data; MDB_INDIR_PRE and MDB_INDIR_POST indicate that it's an
980  * indirect block.  The MDB_INDIR_PRE call is made before the indirect
981  * block pointers are followed and the pointed-to blocks scanned,
982  * MDB_INDIR_POST after.
983  */
984 #define MDB_DATA       1
985 #define MDB_INDIR_PRE  2
986 #define MDB_INDIR_POST 3
987 
988 typedef void (*mark_callback_t) (unsigned int blocknum, unsigned int nfrags,
989 				 unsigned int blksize, int opcode);
990 
991 /* Helper function - handles a data block.  Calls the callback
992  * function and returns number of bytes occupied in file (actually,
993  * rounded up to a frag boundary).  The name is historical.  */
994 static int
995 markblk(mark_callback_t fn, struct ufs1_dinode * di, int bn, off_t o)
996 {
997 	int sz;
998 	int nb;
999 	if (o >= di->di_size)
1000 		return (0);
1001 	sz = dblksize(newsb, di, lblkno(newsb, o));
1002 	nb = (sz > di->di_size - o) ? di->di_size - o : sz;
1003 	if (bn)
1004 		(*fn) (bn, numfrags(newsb, sz), nb, MDB_DATA);
1005 	return (sz);
1006 }
1007 /* Helper function - handles an indirect block.  Makes the
1008  * MDB_INDIR_PRE callback for the indirect block, loops over the
1009  * pointers and recurses, and makes the MDB_INDIR_POST callback.
1010  * Returns the number of bytes occupied in file, as does markblk().
1011  * For the sake of update_for_data_move(), we read the indirect block
1012  * _after_ making the _PRE callback.  The name is historical.  */
1013 static int
1014 markiblk(mark_callback_t fn, struct ufs1_dinode * di, int bn, off_t o, int lev)
1015 {
1016 	int i;
1017 	int j;
1018 	int tot;
1019 	static int32_t indirblk1[howmany(MAXBSIZE, sizeof(int32_t))];
1020 	static int32_t indirblk2[howmany(MAXBSIZE, sizeof(int32_t))];
1021 	static int32_t indirblk3[howmany(MAXBSIZE, sizeof(int32_t))];
1022 	static int32_t *indirblks[3] = {
1023 		&indirblk1[0], &indirblk2[0], &indirblk3[0]
1024 	};
1025 	if (lev < 0)
1026 		return (markblk(fn, di, bn, o));
1027 	if (bn == 0) {
1028 		for (i = newsb->fs_bsize;
1029 		    lev >= 0;
1030 		    i *= NINDIR(newsb), lev--);
1031 		return (i);
1032 	}
1033 	(*fn) (bn, newsb->fs_frag, newsb->fs_bsize, MDB_INDIR_PRE);
1034 	readat(fsbtodb(newsb, bn), indirblks[lev], newsb->fs_bsize);
1035 	tot = 0;
1036 	for (i = 0; i < NINDIR(newsb); i++) {
1037 		j = markiblk(fn, di, indirblks[lev][i], o, lev - 1);
1038 		if (j == 0)
1039 			break;
1040 		o += j;
1041 		tot += j;
1042 	}
1043 	(*fn) (bn, newsb->fs_frag, newsb->fs_bsize, MDB_INDIR_POST);
1044 	return (tot);
1045 }
1046 
1047 
1048 /*
1049  * Call (*fn)() for each data block for an inode.  This routine assumes
1050  *  the inode is known to be of a type that has data blocks (file,
1051  *  directory, or non-fast symlink).  The called function is:
1052  *
1053  * (*fn)(unsigned int blkno, unsigned int nf, unsigned int nb, int op)
1054  *
1055  *  where blkno is the frag number, nf is the number of frags starting
1056  *  at blkno (always <= fs_frag), nb is the number of bytes that belong
1057  *  to the file (usually nf*fs_frag, often less for the last block/frag
1058  *  of a file).
1059  */
1060 static void
1061 map_inode_data_blocks(struct ufs1_dinode * di, mark_callback_t fn)
1062 {
1063 	off_t o;		/* offset within  inode */
1064 	int inc;		/* increment for o - maybe should be off_t? */
1065 	int b;			/* index within di_db[] and di_ib[] arrays */
1066 
1067 	/* Scan the direct blocks... */
1068 	o = 0;
1069 	for (b = 0; b < NDADDR; b++) {
1070 		inc = markblk(fn, di, di->di_db[b], o);
1071 		if (inc == 0)
1072 			break;
1073 		o += inc;
1074 	}
1075 	/* ...and the indirect blocks. */
1076 	if (inc) {
1077 		for (b = 0; b < NIADDR; b++) {
1078 			inc = markiblk(fn, di, di->di_ib[b], o, b);
1079 			if (inc == 0)
1080 				return;
1081 			o += inc;
1082 		}
1083 	}
1084 }
1085 
1086 static void
1087 dblk_callback(struct ufs1_dinode * di, unsigned int inum, void *arg)
1088 {
1089 	mark_callback_t fn;
1090 	fn = (mark_callback_t) arg;
1091 	switch (di->di_mode & IFMT) {
1092 	case IFLNK:
1093 		if (di->di_size > newsb->fs_maxsymlinklen) {
1094 	case IFDIR:
1095 	case IFREG:
1096 			map_inode_data_blocks(di, fn);
1097 		}
1098 		break;
1099 	}
1100 }
1101 /*
1102  * Make a callback call, a la map_inode_data_blocks, for all data
1103  *  blocks in the entire fs.  This is used only once, in
1104  *  update_for_data_move, but it's out at top level because the complex
1105  *  downward-funarg nesting that would otherwise result seems to give
1106  *  gcc gastric distress.
1107  */
1108 static void
1109 map_data_blocks(mark_callback_t fn, int ncg)
1110 {
1111 	map_inodes(&dblk_callback, ncg, (void *) fn);
1112 }
1113 /*
1114  * Initialize the blkmove array.
1115  */
1116 static void
1117 blkmove_init(void)
1118 {
1119 	int i;
1120 
1121 	blkmove = alloconce(oldsb->fs_size * sizeof(*blkmove), "blkmove");
1122 	for (i = 0; i < oldsb->fs_size; i++)
1123 		blkmove[i] = i;
1124 }
1125 /*
1126  * Load the inodes off disk.  Allocates the structures and initializes
1127  *  them - the inodes from disk, the flags to zero.
1128  */
1129 static void
1130 loadinodes(void)
1131 {
1132 	int cg;
1133 	struct ufs1_dinode *iptr;
1134 
1135 	inodes = alloconce(oldsb->fs_ncg * oldsb->fs_ipg *
1136 	    sizeof(struct ufs1_dinode), "inodes");
1137 	iflags = alloconce(oldsb->fs_ncg * oldsb->fs_ipg, "inode flags");
1138 	bzero(iflags, oldsb->fs_ncg * oldsb->fs_ipg);
1139 	iptr = inodes;
1140 	for (cg = 0; cg < oldsb->fs_ncg; cg++) {
1141 		readat(fsbtodb(oldsb, cgimin(oldsb, cg)), iptr,
1142 		    oldsb->fs_ipg * sizeof(struct ufs1_dinode));
1143 		iptr += oldsb->fs_ipg;
1144 	}
1145 }
1146 /*
1147  * Report a file-system-too-full problem.
1148  */
1149 static void
1150 toofull(void)
1151 {
1152 	printf("Sorry, would run out of data blocks\n");
1153 	exit(EXIT_FAILURE);
1154 }
1155 /*
1156  * Record a desire to move "n" frags from "from" to "to".
1157  */
1158 static void
1159 mark_move(unsigned int from, unsigned int to, unsigned int n)
1160 {
1161 	for (; n > 0; n--)
1162 		blkmove[from++] = to++;
1163 }
1164 /* Helper function - evict n frags, starting with start (cg-relative).
1165  * The free bitmap is scanned, unallocated frags are ignored, and
1166  * each block of consecutive allocated frags is moved as a unit.
1167  */
1168 static void
1169 fragmove(struct cg * cg, int base, unsigned int start, unsigned int n)
1170 {
1171 	int i;
1172 	int run;
1173 	run = 0;
1174 	for (i = 0; i <= n; i++) {
1175 		if ((i < n) && bit_is_clr(cg_blksfree(cg, 0), start + i)) {
1176 			run++;
1177 		} else {
1178 			if (run > 0) {
1179 				int off;
1180 				off = find_freespace(run);
1181 				if (off < 0)
1182 					toofull();
1183 				mark_move(base + start + i - run, off, run);
1184 				set_bits(cg_blksfree(cg, 0), start + i - run,
1185 				    run);
1186 				clr_bits(cg_blksfree(cgs[dtog(oldsb, off)], 0),
1187 				    dtogd(oldsb, off), run);
1188 			}
1189 			run = 0;
1190 		}
1191 	}
1192 }
1193 /*
1194  * Evict all data blocks from the given cg, starting at minfrag (based
1195  *  at the beginning of the cg), for length nfrag.  The eviction is
1196  *  assumed to be entirely data-area; this should not be called with a
1197  *  range overlapping the metadata structures in the cg.  It also
1198  *  assumes minfrag points into the given cg; it will misbehave if this
1199  *  is not true.
1200  *
1201  * See the comment header on find_freespace() for one possible bug
1202  *  lurking here.
1203  */
1204 static void
1205 evict_data(struct cg * cg, unsigned int minfrag, unsigned int nfrag)
1206 {
1207 	int base;		/* base of cg (in frags from beginning of fs) */
1208 
1209 
1210 	base = cgbase(oldsb, cg->cg_cgx);
1211 	/* Does the boundary fall in the middle of a block?  To avoid breaking
1212 	 * between frags allocated as consecutive, we always evict the whole
1213 	 * block in this case, though one could argue we should check to see
1214 	 * if the frag before or after the break is unallocated. */
1215 	if (minfrag % oldsb->fs_frag) {
1216 		int n;
1217 		n = minfrag % oldsb->fs_frag;
1218 		minfrag -= n;
1219 		nfrag += n;
1220 	}
1221 	/* Do whole blocks.  If a block is wholly free, skip it; if wholly
1222 	 * allocated, move it in toto.  If neither, call fragmove() to move
1223 	 * the frags to new locations. */
1224 	while (nfrag >= oldsb->fs_frag) {
1225 		if (!blk_is_set(cg_blksfree(cg, 0), minfrag, oldsb->fs_frag)) {
1226 			if (blk_is_clr(cg_blksfree(cg, 0), minfrag,
1227 				oldsb->fs_frag)) {
1228 				int off;
1229 				off = find_freeblock();
1230 				if (off < 0)
1231 					toofull();
1232 				mark_move(base + minfrag, off, oldsb->fs_frag);
1233 				set_bits(cg_blksfree(cg, 0), minfrag,
1234 				    oldsb->fs_frag);
1235 				clr_bits(cg_blksfree(cgs[dtog(oldsb, off)], 0),
1236 				    dtogd(oldsb, off), oldsb->fs_frag);
1237 			} else {
1238 				fragmove(cg, base, minfrag, oldsb->fs_frag);
1239 			}
1240 		}
1241 		minfrag += oldsb->fs_frag;
1242 		nfrag -= oldsb->fs_frag;
1243 	}
1244 	/* Clean up any sub-block amount left over. */
1245 	if (nfrag) {
1246 		fragmove(cg, base, minfrag, nfrag);
1247 	}
1248 }
1249 /*
1250  * Move all data blocks according to blkmove.  We have to be careful,
1251  *  because we may be updating indirect blocks that will themselves be
1252  *  getting moved, or inode int32_t arrays that point to indirect
1253  *  blocks that will be moved.  We call this before
1254  *  update_for_data_move, and update_for_data_move does inodes first,
1255  *  then indirect blocks in preorder, so as to make sure that the
1256  *  file system is self-consistent at all points, for better crash
1257  *  tolerance.  (We can get away with this only because all the writes
1258  *  done by perform_data_move() are writing into space that's not used
1259  *  by the old file system.)  If we crash, some things may point to the
1260  *  old data and some to the new, but both copies are the same.  The
1261  *  only wrong things should be csum info and free bitmaps, which fsck
1262  *  is entirely capable of cleaning up.
1263  *
1264  * Since blkmove_init() initializes all blocks to move to their current
1265  *  locations, we can have two blocks marked as wanting to move to the
1266  *  same location, but only two and only when one of them is the one
1267  *  that was already there.  So if blkmove[i]==i, we ignore that entry
1268  *  entirely - for unallocated blocks, we don't want it (and may be
1269  *  putting something else there), and for allocated blocks, we don't
1270  *  want to copy it anywhere.
1271  */
1272 static void
1273 perform_data_move(void)
1274 {
1275 	int i;
1276 	int run;
1277 	int maxrun;
1278 	char buf[65536];
1279 
1280 	maxrun = sizeof(buf) / newsb->fs_fsize;
1281 	run = 0;
1282 	for (i = 0; i < oldsb->fs_size; i++) {
1283 		if ((blkmove[i] == i) ||
1284 		    (run >= maxrun) ||
1285 		    ((run > 0) &&
1286 			(blkmove[i] != blkmove[i - 1] + 1))) {
1287 			if (run > 0) {
1288 				readat(fsbtodb(oldsb, i - run), &buf[0],
1289 				    run << oldsb->fs_fshift);
1290 				writeat(fsbtodb(oldsb, blkmove[i - run]),
1291 				    &buf[0], run << oldsb->fs_fshift);
1292 			}
1293 			run = 0;
1294 		}
1295 		if (blkmove[i] != i)
1296 			run++;
1297 	}
1298 	if (run > 0) {
1299 		readat(fsbtodb(oldsb, i - run), &buf[0],
1300 		    run << oldsb->fs_fshift);
1301 		writeat(fsbtodb(oldsb, blkmove[i - run]), &buf[0],
1302 		    run << oldsb->fs_fshift);
1303 	}
1304 }
1305 /*
1306  * This modifies an array of int32_t, according to blkmove.  This is
1307  *  used to update inode block arrays and indirect blocks to point to
1308  *  the new locations of data blocks.
1309  *
1310  * Return value is the number of int32_ts that needed updating; in
1311  *  particular, the return value is zero iff nothing was modified.
1312  */
1313 static int
1314 movemap_blocks(int32_t * vec, int n)
1315 {
1316 	int rv;
1317 
1318 	rv = 0;
1319 	for (; n > 0; n--, vec++) {
1320 		if (blkmove[*vec] != *vec) {
1321 			*vec = blkmove[*vec];
1322 			rv++;
1323 		}
1324 	}
1325 	return (rv);
1326 }
1327 static void
1328 moveblocks_callback(struct ufs1_dinode * di, unsigned int inum, void *arg)
1329 {
1330 	switch (di->di_mode & IFMT) {
1331 	case IFLNK:
1332 		if (di->di_size > oldsb->fs_maxsymlinklen) {
1333 	case IFDIR:
1334 	case IFREG:
1335 			/* don't || these two calls; we need their
1336 			 * side-effects */
1337 			if (movemap_blocks(&di->di_db[0], NDADDR)) {
1338 				iflags[inum] |= IF_DIRTY;
1339 			}
1340 			if (movemap_blocks(&di->di_ib[0], NIADDR)) {
1341 				iflags[inum] |= IF_DIRTY;
1342 			}
1343 		}
1344 		break;
1345 	}
1346 }
1347 
1348 static void
1349 moveindir_callback(unsigned int off, unsigned int nfrag, unsigned int nbytes,
1350 		   int kind)
1351 {
1352 	if (kind == MDB_INDIR_PRE) {
1353 		int32_t blk[howmany(MAXBSIZE, sizeof(int32_t))];
1354 		readat(fsbtodb(oldsb, off), &blk[0], oldsb->fs_bsize);
1355 		if (movemap_blocks(&blk[0], NINDIR(oldsb))) {
1356 			writeat(fsbtodb(oldsb, off), &blk[0], oldsb->fs_bsize);
1357 		}
1358 	}
1359 }
1360 /*
1361  * Update all inode data arrays and indirect blocks to point to the new
1362  *  locations of data blocks.  See the comment header on
1363  *  perform_data_move for some ordering considerations.
1364  */
1365 static void
1366 update_for_data_move(void)
1367 {
1368 	map_inodes(&moveblocks_callback, oldsb->fs_ncg, NULL);
1369 	map_data_blocks(&moveindir_callback, oldsb->fs_ncg);
1370 }
1371 /*
1372  * Initialize the inomove array.
1373  */
1374 static void
1375 inomove_init(void)
1376 {
1377 	int i;
1378 
1379 	inomove = alloconce(oldsb->fs_ipg * oldsb->fs_ncg * sizeof(*inomove),
1380                             "inomove");
1381 	for (i = (oldsb->fs_ipg * oldsb->fs_ncg) - 1; i >= 0; i--)
1382 		inomove[i] = i;
1383 }
1384 /*
1385  * Flush all dirtied inodes to disk.  Scans the inode flags array; for
1386  *  each dirty inode, it sets the BDIRTY bit on the first inode in the
1387  *  block containing the dirty inode.  Then it scans by blocks, and for
1388  *  each marked block, writes it.
1389  */
1390 static void
1391 flush_inodes(void)
1392 {
1393 	int i;
1394 	int ni;
1395 	int m;
1396 
1397 	ni = newsb->fs_ipg * newsb->fs_ncg;
1398 	m = INOPB(newsb) - 1;
1399 	for (i = 0; i < ni; i++) {
1400 		if (iflags[i] & IF_DIRTY) {
1401 			iflags[i & ~m] |= IF_BDIRTY;
1402 		}
1403 	}
1404 	m++;
1405 	for (i = 0; i < ni; i += m) {
1406 		if (iflags[i] & IF_BDIRTY) {
1407 			writeat(fsbtodb(newsb, ino_to_fsba(newsb, i)),
1408 			    inodes + i, newsb->fs_bsize);
1409 		}
1410 	}
1411 }
1412 /*
1413  * Evict all inodes from the specified cg.  shrink() already checked
1414  *  that there were enough free inodes, so the no-free-inodes check is
1415  *  a can't-happen.  If it does trip, the file system should be in good
1416  *  enough shape for fsck to fix; see the comment on perform_data_move
1417  *  for the considerations in question.
1418  */
1419 static void
1420 evict_inodes(struct cg * cg)
1421 {
1422 	int inum;
1423 	int i;
1424 	int fi;
1425 
1426 	inum = newsb->fs_ipg * cg->cg_cgx;
1427 	for (i = 0; i < newsb->fs_ipg; i++, inum++) {
1428 		if (inodes[inum].di_mode != 0) {
1429 			fi = find_freeinode();
1430 			if (fi < 0) {
1431 				printf("Sorry, inodes evaporated - "
1432 				    "file system probably needs fsck\n");
1433 				exit(EXIT_FAILURE);
1434 			}
1435 			inomove[inum] = fi;
1436 			clr_bits(cg_inosused(cg, 0), i, 1);
1437 			set_bits(cg_inosused(cgs[ino_to_cg(newsb, fi)], 0),
1438 			    fi % newsb->fs_ipg, 1);
1439 		}
1440 	}
1441 }
1442 /*
1443  * Move inodes from old locations to new.  Does not actually write
1444  *  anything to disk; just copies in-core and sets dirty bits.
1445  *
1446  * We have to be careful here for reasons similar to those mentioned in
1447  *  the comment header on perform_data_move, above: for the sake of
1448  *  crash tolerance, we want to make sure everything is present at both
1449  *  old and new locations before we update pointers.  So we call this
1450  *  first, then flush_inodes() to get them out on disk, then update
1451  *  directories to match.
1452  */
1453 static void
1454 perform_inode_move(void)
1455 {
1456 	int i;
1457 	int ni;
1458 
1459 	ni = oldsb->fs_ipg * oldsb->fs_ncg;
1460 	for (i = 0; i < ni; i++) {
1461 		if (inomove[i] != i) {
1462 			inodes[inomove[i]] = inodes[i];
1463 			iflags[inomove[i]] = iflags[i] | IF_DIRTY;
1464 		}
1465 	}
1466 }
1467 /*
1468  * Update the directory contained in the nb bytes at buf, to point to
1469  *  inodes' new locations.
1470  */
1471 static int
1472 update_dirents(char *buf, int nb)
1473 {
1474 	int rv;
1475 #define d ((struct direct *)buf)
1476 
1477 	rv = 0;
1478 	while (nb > 0) {
1479 		if (inomove[d->d_ino] != d->d_ino) {
1480 			rv++;
1481 			d->d_ino = inomove[d->d_ino];
1482 		}
1483 		nb -= d->d_reclen;
1484 		buf += d->d_reclen;
1485 	}
1486 	return (rv);
1487 #undef d
1488 }
1489 /*
1490  * Callback function for map_inode_data_blocks, for updating a
1491  *  directory to point to new inode locations.
1492  */
1493 static void
1494 update_dir_data(unsigned int bn, unsigned int size, unsigned int nb, int kind)
1495 {
1496 	if (kind == MDB_DATA) {
1497 		union {
1498 			struct direct d;
1499 			char ch[MAXBSIZE];
1500 		}     buf;
1501 		readat(fsbtodb(oldsb, bn), &buf, size << oldsb->fs_fshift);
1502 		if (update_dirents((char *) &buf, nb)) {
1503 			writeat(fsbtodb(oldsb, bn), &buf,
1504 			    size << oldsb->fs_fshift);
1505 		}
1506 	}
1507 }
1508 static void
1509 dirmove_callback(struct ufs1_dinode * di, unsigned int inum, void *arg)
1510 {
1511 	switch (di->di_mode & IFMT) {
1512 	case IFDIR:
1513 		map_inode_data_blocks(di, &update_dir_data);
1514 		break;
1515 	}
1516 }
1517 /*
1518  * Update directory entries to point to new inode locations.
1519  */
1520 static void
1521 update_for_inode_move(void)
1522 {
1523 	map_inodes(&dirmove_callback, newsb->fs_ncg, NULL);
1524 }
1525 /*
1526  * Shrink the file system.
1527  */
1528 static void
1529 shrink(void)
1530 {
1531 	int i;
1532 
1533 	/* Load the inodes off disk - we'll need 'em. */
1534 	loadinodes();
1535 	/* Update the timestamp. */
1536 	newsb->fs_time = timestamp();
1537 	/* Update the size figures. */
1538 	newsb->fs_size = dbtofsb(newsb, newsize);
1539 	newsb->fs_old_ncyl = howmany(newsb->fs_size * NSPF(newsb),
1540 	    newsb->fs_old_spc);
1541 	newsb->fs_ncg = howmany(newsb->fs_old_ncyl, newsb->fs_old_cpg);
1542 	/* Does the (new) last cg end before the end of its inode area?  See
1543 	 * the similar code in grow() for more on this. */
1544 	if (cgdmin(newsb, newsb->fs_ncg - 1) > newsb->fs_size) {
1545 		newsb->fs_ncg--;
1546 		newsb->fs_old_ncyl = newsb->fs_ncg * newsb->fs_old_cpg;
1547 		newsb->fs_size = (newsb->fs_old_ncyl * newsb->fs_old_spc) /
1548 		    NSPF(newsb);
1549 		printf("Warning: last cylinder group is too small;\n");
1550 		printf("    dropping it.  New size = %lu.\n",
1551 		    (unsigned long int) fsbtodb(newsb, newsb->fs_size));
1552 	}
1553 	/* Let's make sure we're not being shrunk into oblivion. */
1554 	if (newsb->fs_ncg < 1) {
1555 		printf("Size too small - file system would have no cylinders\n");
1556 		exit(EXIT_FAILURE);
1557 	}
1558 	/* Initialize for block motion. */
1559 	blkmove_init();
1560 	/* Update csum size, then fix up for the new size */
1561 	newsb->fs_cssize = fragroundup(newsb,
1562 	    newsb->fs_ncg * sizeof(struct csum));
1563 	csum_fixup();
1564 	/* Evict data from any cgs being wholly eliminated */
1565 	for (i = newsb->fs_ncg; i < oldsb->fs_ncg; i++) {
1566 		int base;
1567 		int dlow;
1568 		int dhigh;
1569 		int dmax;
1570 		base = cgbase(oldsb, i);
1571 		dlow = cgsblock(oldsb, i) - base;
1572 		dhigh = cgdmin(oldsb, i) - base;
1573 		dmax = oldsb->fs_size - base;
1574 		if (dmax > cgs[i]->cg_ndblk)
1575 			dmax = cgs[i]->cg_ndblk;
1576 		evict_data(cgs[i], 0, dlow);
1577 		evict_data(cgs[i], dhigh, dmax - dhigh);
1578 		newsb->fs_cstotal.cs_ndir -= cgs[i]->cg_cs.cs_ndir;
1579 		newsb->fs_cstotal.cs_nifree -= cgs[i]->cg_cs.cs_nifree;
1580 		newsb->fs_cstotal.cs_nffree -= cgs[i]->cg_cs.cs_nffree;
1581 		newsb->fs_cstotal.cs_nbfree -= cgs[i]->cg_cs.cs_nbfree;
1582 	}
1583 	/* Update the new last cg. */
1584 	cgs[newsb->fs_ncg - 1]->cg_ndblk = newsb->fs_size -
1585 	    ((newsb->fs_ncg - 1) * newsb->fs_fpg);
1586 	/* Is the new last cg partial?  If so, evict any data from the part
1587 	 * being shrunken away. */
1588 	if (newsb->fs_size % newsb->fs_fpg) {
1589 		struct cg *cg;
1590 		int oldcgsize;
1591 		int newcgsize;
1592 		cg = cgs[newsb->fs_ncg - 1];
1593 		newcgsize = newsb->fs_size % newsb->fs_fpg;
1594 		oldcgsize = oldsb->fs_size - ((newsb->fs_ncg - 1) &
1595 		    oldsb->fs_fpg);
1596 		if (oldcgsize > oldsb->fs_fpg)
1597 			oldcgsize = oldsb->fs_fpg;
1598 		evict_data(cg, newcgsize, oldcgsize - newcgsize);
1599 		clr_bits(cg_blksfree(cg, 0), newcgsize, oldcgsize - newcgsize);
1600 	}
1601 	/* Find out whether we would run out of inodes.  (Note we haven't
1602 	 * actually done anything to the file system yet; all those evict_data
1603 	 * calls just update blkmove.) */
1604 	{
1605 		int slop;
1606 		slop = 0;
1607 		for (i = 0; i < newsb->fs_ncg; i++)
1608 			slop += cgs[i]->cg_cs.cs_nifree;
1609 		for (; i < oldsb->fs_ncg; i++)
1610 			slop -= oldsb->fs_ipg - cgs[i]->cg_cs.cs_nifree;
1611 		if (slop < 0) {
1612 			printf("Sorry, would run out of inodes\n");
1613 			exit(EXIT_FAILURE);
1614 		}
1615 	}
1616 	/* Copy data, then update pointers to data.  See the comment header on
1617 	 * perform_data_move for ordering considerations. */
1618 	perform_data_move();
1619 	update_for_data_move();
1620 	/* Now do inodes.  Initialize, evict, move, update - see the comment
1621 	 * header on perform_inode_move. */
1622 	inomove_init();
1623 	for (i = newsb->fs_ncg; i < oldsb->fs_ncg; i++)
1624 		evict_inodes(cgs[i]);
1625 	perform_inode_move();
1626 	flush_inodes();
1627 	update_for_inode_move();
1628 	/* Recompute all the bitmaps; most of them probably need it anyway,
1629 	 * the rest are just paranoia and not wanting to have to bother
1630 	 * keeping track of exactly which ones require it. */
1631 	for (i = 0; i < newsb->fs_ncg; i++)
1632 		cgflags[i] |= CGF_DIRTY | CGF_BLKMAPS | CGF_INOMAPS;
1633 	/* Update the cg_old_ncyl value for the last cylinder. */
1634 	if ((newsb->fs_old_flags & FS_FLAGS_UPDATED) == 0)
1635 		cgs[newsb->fs_ncg - 1]->cg_old_ncyl =
1636 		    newsb->fs_old_ncyl % newsb->fs_old_cpg;
1637 	/* Make fs_dsize match the new reality. */
1638 	recompute_fs_dsize();
1639 }
1640 /*
1641  * Recompute the block totals, block cluster summaries, and rotational
1642  *  position summaries, for a given cg (specified by number), based on
1643  *  its free-frag bitmap (cg_blksfree()[]).
1644  */
1645 static void
1646 rescan_blkmaps(int cgn)
1647 {
1648 	struct cg *cg;
1649 	int f;
1650 	int b;
1651 	int blkfree;
1652 	int blkrun;
1653 	int fragrun;
1654 	int fwb;
1655 
1656 	cg = cgs[cgn];
1657 	/* Subtract off the current totals from the sb's summary info */
1658 	newsb->fs_cstotal.cs_nffree -= cg->cg_cs.cs_nffree;
1659 	newsb->fs_cstotal.cs_nbfree -= cg->cg_cs.cs_nbfree;
1660 	/* Clear counters and bitmaps. */
1661 	cg->cg_cs.cs_nffree = 0;
1662 	cg->cg_cs.cs_nbfree = 0;
1663 	bzero(&cg->cg_frsum[0], MAXFRAG * sizeof(cg->cg_frsum[0]));
1664 	bzero(&old_cg_blktot(cg, 0)[0],
1665 	    newsb->fs_old_cpg * sizeof(old_cg_blktot(cg, 0)[0]));
1666 	bzero(&old_cg_blks(newsb, cg, 0, 0)[0],
1667 	    newsb->fs_old_cpg * newsb->fs_old_nrpos *
1668 	    sizeof(old_cg_blks(newsb, cg, 0, 0)[0]));
1669 	if (newsb->fs_contigsumsize > 0) {
1670 		cg->cg_nclusterblks = cg->cg_ndblk / newsb->fs_frag;
1671 		bzero(&cg_clustersum(cg, 0)[1],
1672 		    newsb->fs_contigsumsize *
1673 		    sizeof(cg_clustersum(cg, 0)[1]));
1674 		bzero(&cg_clustersfree(cg, 0)[0],
1675 		    howmany((newsb->fs_old_cpg * newsb->fs_old_spc) /
1676 		    NSPB(newsb), NBBY));
1677 	}
1678 	/* Scan the free-frag bitmap.  Runs of free frags are kept track of
1679 	 * with fragrun, and recorded into cg_frsum[] and cg_cs.cs_nffree; on
1680 	 * each block boundary, entire free blocks are recorded as well. */
1681 	blkfree = 1;
1682 	blkrun = 0;
1683 	fragrun = 0;
1684 	f = 0;
1685 	b = 0;
1686 	fwb = 0;
1687 	while (f < cg->cg_ndblk) {
1688 		if (bit_is_set(cg_blksfree(cg, 0), f)) {
1689 			fragrun++;
1690 		} else {
1691 			blkfree = 0;
1692 			if (fragrun > 0) {
1693 				cg->cg_frsum[fragrun]++;
1694 				cg->cg_cs.cs_nffree += fragrun;
1695 			}
1696 			fragrun = 0;
1697 		}
1698 		f++;
1699 		fwb++;
1700 		if (fwb >= newsb->fs_frag) {
1701 			if (blkfree) {
1702 				cg->cg_cs.cs_nbfree++;
1703 				if (newsb->fs_contigsumsize > 0)
1704 					set_bits(cg_clustersfree(cg, 0), b, 1);
1705 				old_cg_blktot(cg, 0)[old_cbtocylno(newsb,
1706 				    f - newsb->fs_frag)]++;
1707 				old_cg_blks(newsb, cg,
1708 				    old_cbtocylno(newsb, f - newsb->fs_frag),
1709 				    0)[old_cbtorpos(newsb,
1710 				    f - newsb->fs_frag)]++;
1711 				blkrun++;
1712 			} else {
1713 				if (fragrun > 0) {
1714 					cg->cg_frsum[fragrun]++;
1715 					cg->cg_cs.cs_nffree += fragrun;
1716 				}
1717 				if (newsb->fs_contigsumsize > 0) {
1718 					if (blkrun > 0) {
1719 						cg_clustersum(cg, 0)[(blkrun
1720 						    > newsb->fs_contigsumsize)
1721 						    ? newsb->fs_contigsumsize
1722 						    : blkrun]++;
1723 					}
1724 				}
1725 				blkrun = 0;
1726 			}
1727 			fwb = 0;
1728 			b++;
1729 			blkfree = 1;
1730 			fragrun = 0;
1731 		}
1732 	}
1733 	if (fragrun > 0) {
1734 		cg->cg_frsum[fragrun]++;
1735 		cg->cg_cs.cs_nffree += fragrun;
1736 	}
1737 	if ((blkrun > 0) && (newsb->fs_contigsumsize > 0)) {
1738 		cg_clustersum(cg, 0)[(blkrun > newsb->fs_contigsumsize) ?
1739 		    newsb->fs_contigsumsize : blkrun]++;
1740 	}
1741 	/*
1742          * Put the updated summary info back into csums, and add it
1743          * back into the sb's summary info.  Then mark the cg dirty.
1744          */
1745 	csums[cgn] = cg->cg_cs;
1746 	newsb->fs_cstotal.cs_nffree += cg->cg_cs.cs_nffree;
1747 	newsb->fs_cstotal.cs_nbfree += cg->cg_cs.cs_nbfree;
1748 	cgflags[cgn] |= CGF_DIRTY;
1749 }
1750 /*
1751  * Recompute the cg_inosused()[] bitmap, and the cs_nifree and cs_ndir
1752  *  values, for a cg, based on the in-core inodes for that cg.
1753  */
1754 static void
1755 rescan_inomaps(int cgn)
1756 {
1757 	struct cg *cg;
1758 	int inum;
1759 	int iwc;
1760 
1761 	cg = cgs[cgn];
1762 	newsb->fs_cstotal.cs_ndir -= cg->cg_cs.cs_ndir;
1763 	newsb->fs_cstotal.cs_nifree -= cg->cg_cs.cs_nifree;
1764 	cg->cg_cs.cs_ndir = 0;
1765 	cg->cg_cs.cs_nifree = 0;
1766 	bzero(&cg_inosused(cg, 0)[0], howmany(newsb->fs_ipg, NBBY));
1767 	inum = cgn * newsb->fs_ipg;
1768 	if (cgn == 0) {
1769 		set_bits(cg_inosused(cg, 0), 0, 2);
1770 		iwc = 2;
1771 		inum += 2;
1772 	} else {
1773 		iwc = 0;
1774 	}
1775 	for (; iwc < newsb->fs_ipg; iwc++, inum++) {
1776 		switch (inodes[inum].di_mode & IFMT) {
1777 		case 0:
1778 			cg->cg_cs.cs_nifree++;
1779 			break;
1780 		case IFDIR:
1781 			cg->cg_cs.cs_ndir++;
1782 			/* fall through */
1783 		default:
1784 			set_bits(cg_inosused(cg, 0), iwc, 1);
1785 			break;
1786 		}
1787 	}
1788 	csums[cgn] = cg->cg_cs;
1789 	newsb->fs_cstotal.cs_ndir += cg->cg_cs.cs_ndir;
1790 	newsb->fs_cstotal.cs_nifree += cg->cg_cs.cs_nifree;
1791 	cgflags[cgn] |= CGF_DIRTY;
1792 }
1793 /*
1794  * Flush cgs to disk, recomputing anything they're marked as needing.
1795  */
1796 static void
1797 flush_cgs(void)
1798 {
1799 	int i;
1800 
1801 	for (i = 0; i < newsb->fs_ncg; i++) {
1802 		if (cgflags[i] & CGF_BLKMAPS) {
1803 			rescan_blkmaps(i);
1804 		}
1805 		if (cgflags[i] & CGF_INOMAPS) {
1806 			rescan_inomaps(i);
1807 		}
1808 		if (cgflags[i] & CGF_DIRTY) {
1809 			cgs[i]->cg_rotor = 0;
1810 			cgs[i]->cg_frotor = 0;
1811 			cgs[i]->cg_irotor = 0;
1812 			writeat(fsbtodb(newsb, cgtod(newsb, i)), cgs[i],
1813 			    cgblksz);
1814 		}
1815 	}
1816 	writeat(fsbtodb(newsb, newsb->fs_csaddr), csums, newsb->fs_cssize);
1817 }
1818 /*
1819  * Write the superblock, both to the main superblock and to each cg's
1820  *  alternative superblock.
1821  */
1822 static void
1823 write_sbs(void)
1824 {
1825 	int i;
1826 
1827 	if (newsb->fs_magic == FS_UFS1_MAGIC &&
1828 	    (newsb->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1829 		newsb->fs_old_time = newsb->fs_time;
1830 	    	newsb->fs_old_size = newsb->fs_size;
1831 	    	/* we don't update fs_csaddr */
1832 	    	newsb->fs_old_dsize = newsb->fs_dsize;
1833 		newsb->fs_old_cstotal.cs_ndir = newsb->fs_cstotal.cs_ndir;
1834 		newsb->fs_old_cstotal.cs_nbfree = newsb->fs_cstotal.cs_nbfree;
1835 		newsb->fs_old_cstotal.cs_nifree = newsb->fs_cstotal.cs_nifree;
1836 		newsb->fs_old_cstotal.cs_nffree = newsb->fs_cstotal.cs_nffree;
1837 		/* fill fs_old_postbl_start with 256 bytes of 0xff? */
1838 	}
1839 	writeat(where /  DEV_BSIZE, newsb, SBLOCKSIZE);
1840 	for (i = 0; i < newsb->fs_ncg; i++) {
1841 		writeat(fsbtodb(newsb, cgsblock(newsb, i)), newsb, SBLOCKSIZE);
1842 	}
1843 }
1844 
1845 static uint32_t
1846 get_dev_size(char *dev_name)
1847 {
1848 	struct dkwedge_info dkw;
1849 	struct partition *pp;
1850 	struct disklabel lp;
1851 	size_t ptn;
1852 
1853 	/* Get info about partition/wedge */
1854 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) {
1855 		if (ioctl(fd, DIOCGDINFO, &lp) == -1)
1856 			return 0;
1857 
1858 		ptn = strchr(dev_name, '\0')[-1] - 'a';
1859 		if (ptn >= lp.d_npartitions)
1860 			return 0;
1861 
1862 		pp = &lp.d_partitions[ptn];
1863 		return pp->p_size;
1864 	}
1865 
1866 	return dkw.dkw_size;
1867 }
1868 
1869 /*
1870  * main().
1871  */
1872 int
1873 main(int argc, char **argv)
1874 {
1875 	int ch;
1876 	int ExpertFlag;
1877 	int SFlag;
1878 	size_t i;
1879 
1880 	char *special;
1881 	char reply[5];
1882 
1883 	newsize = 0;
1884 	ExpertFlag = 0;
1885 	SFlag = 0;
1886 
1887 	while ((ch = getopt(argc, argv, "s:y")) != -1) {
1888 		switch (ch) {
1889 		case 's':
1890 			SFlag = 1;
1891 			newsize = (size_t)strtoul(optarg, NULL, 10);
1892 			if(newsize < 1) {
1893 				usage();
1894 			}
1895 			break;
1896 		case 'y':
1897 			ExpertFlag = 1;
1898 			break;
1899 		case '?':
1900 			/* FALLTHROUGH */
1901 		default:
1902 			usage();
1903 		}
1904 	}
1905 	argc -= optind;
1906 	argv += optind;
1907 
1908 	if (argc != 1) {
1909 		usage();
1910 	}
1911 
1912 	special = *argv;
1913 
1914 	if (ExpertFlag == 0) {
1915 		printf("It's required to manually run fsck on file system "
1916 		    "before you can resize it\n\n"
1917 		    " Did you run fsck on your disk (Yes/No) ? ");
1918 		fgets(reply, (int)sizeof(reply), stdin);
1919 		if (strcasecmp(reply, "Yes\n")) {
1920 			printf("\n Nothing done \n");
1921 			exit(EXIT_SUCCESS);
1922 		}
1923 	}
1924 
1925 	fd = open(special, O_RDWR, 0);
1926 	if (fd < 0)
1927 		err(EXIT_FAILURE, "Can't open `%s'", special);
1928 	checksmallio();
1929 
1930 	if (SFlag == 0) {
1931 		newsize = get_dev_size(special);
1932 		if (newsize == 0)
1933 			err(EXIT_FAILURE,
1934 			    "Can't resize file system, newsize not known.");
1935 	}
1936 
1937 	oldsb = (struct fs *) & sbbuf;
1938 	newsb = (struct fs *) (SBLOCKSIZE + (char *) &sbbuf);
1939 	for (where = search[i = 0]; search[i] != -1; where = search[++i]) {
1940 		readat(where / DEV_BSIZE, oldsb, SBLOCKSIZE);
1941 		switch (oldsb->fs_magic) {
1942 		case FS_UFS2_MAGIC:
1943 			/* FALLTHROUGH */
1944 			is_ufs2 = 1;
1945 		case FS_UFS1_MAGIC:
1946 			needswap = 0;
1947 			break;
1948 		case FS_UFS2_MAGIC_SWAPPED:
1949  			is_ufs2 = 1;
1950 			/* FALLTHROUGH */
1951 		case FS_UFS1_MAGIC_SWAPPED:
1952 			needswap = 1;
1953 			break;
1954 		default:
1955 			continue;
1956 		}
1957 		if (!is_ufs2 && where == SBLOCK_UFS2)
1958 			continue;
1959 		break;
1960 	}
1961 	if (where == (off_t)-1)
1962 		errx(EXIT_FAILURE, "Bad magic number");
1963 	if (oldsb->fs_magic == FS_UFS1_MAGIC &&
1964 	    (oldsb->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1965 		oldsb->fs_csaddr = oldsb->fs_old_csaddr;
1966 		oldsb->fs_size = oldsb->fs_old_size;
1967 		oldsb->fs_dsize = oldsb->fs_old_dsize;
1968 		oldsb->fs_cstotal.cs_ndir = oldsb->fs_old_cstotal.cs_ndir;
1969 		oldsb->fs_cstotal.cs_nbfree = oldsb->fs_old_cstotal.cs_nbfree;
1970 		oldsb->fs_cstotal.cs_nifree = oldsb->fs_old_cstotal.cs_nifree;
1971 		oldsb->fs_cstotal.cs_nffree = oldsb->fs_old_cstotal.cs_nffree;
1972 		/* any others? */
1973 		printf("Resizing with ffsv1 superblock\n");
1974 	}
1975 	if (is_ufs2)
1976 		errx(EXIT_FAILURE, "ffsv2 file systems currently unsupported.");
1977 	if (needswap)
1978 		errx(EXIT_FAILURE, "Swapped byte order file system detected"
1979 		    " - currently unsupported.");
1980 	oldsb->fs_qbmask = ~(int64_t) oldsb->fs_bmask;
1981 	oldsb->fs_qfmask = ~(int64_t) oldsb->fs_fmask;
1982 	if (oldsb->fs_ipg % INOPB(oldsb)) {
1983 		(void)fprintf(stderr, "ipg[%d] %% INOPB[%d] != 0\n",
1984 		    (int) oldsb->fs_ipg, (int) INOPB(oldsb));
1985 		exit(EXIT_FAILURE);
1986 	}
1987 	/* The superblock is bigger than struct fs (there are trailing tables,
1988 	 * of non-fixed size); make sure we copy the whole thing.  SBLOCKSIZE may
1989 	 * be an over-estimate, but we do this just once, so being generous is
1990 	 * cheap. */
1991 	bcopy(oldsb, newsb, SBLOCKSIZE);
1992 	loadcgs();
1993 	if (newsize > fsbtodb(oldsb, oldsb->fs_size)) {
1994 		grow();
1995 	} else if (newsize < fsbtodb(oldsb, oldsb->fs_size)) {
1996 		shrink();
1997 	}
1998 	flush_cgs();
1999 	write_sbs();
2000 	if (isplainfile())
2001 		ftruncate(fd,newsize * DEV_BSIZE);
2002 	return 0;
2003 }
2004 
2005 static void
2006 usage(void)
2007 {
2008 
2009 	(void)fprintf(stderr, "usage: %s [-y] [-s size] special\n", getprogname());
2010 	exit(EXIT_FAILURE);
2011 }
2012