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