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