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