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