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