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