2
* Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
3
* Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
6
* This code is derived from software contributed to Berkeley by
7
* Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
* 3. All advertising materials mentioning features or use of this software
18
* must display the following acknowledgment:
19
* This product includes software developed by the University of
20
* California, Berkeley and its contributors, as well as Christoph
21
* Herrmann and Thomas-Henning von Kamptz.
22
* 4. Neither the name of the University nor the names of its contributors
23
* may be used to endorse or promote products derived from this software
24
* without specific prior written permission.
26
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
* $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $
43
static const char copyright[] =
44
"@(#) Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz\n\
45
Copyright (c) 1980, 1989, 1993 The Regents of the University of California.\n\
46
All rights reserved.\n";
49
#include <sys/cdefs.h>
50
__FBSDID("$FreeBSD: src/sbin/growfs/growfs.c,v 1.25.2.1.6.1 2010/02/10 00:26:20 kensmith Exp $");
52
/* ********************************************************** INCLUDES ***** */
53
#include <sys/param.h>
54
#include <sys/disklabel.h>
55
#include <sys/ioctl.h>
70
#include <ufs/ufs/dinode.h>
71
#include <ufs/ffs/fs.h>
75
/* *************************************************** GLOBALS & TYPES ***** */
77
int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
84
#define sblock fsun1.fs /* the new superblock */
85
#define osblock fsun2.fs /* the old superblock */
88
* Possible superblock locations ordered from most to least likely.
90
static int sblock_try[] = SBLOCKSEARCH;
91
static ufs2_daddr_t sblockloc;
97
#define acg cgun1.cg /* a cylinder cgroup (new) */
98
#define aocg cgun2.cg /* an old cylinder group */
100
static char ablk[MAXBSIZE]; /* a block */
102
static struct csum *fscs; /* cylinder summary */
105
struct ufs1_dinode dp1;
106
struct ufs2_dinode dp2;
108
#define DIP(dp, field) \
109
((sblock.fs_magic == FS_UFS1_MAGIC) ? \
110
(uint32_t)(dp)->dp1.field : (dp)->dp2.field)
111
#define DIP_SET(dp, field, val) do { \
112
if (sblock.fs_magic == FS_UFS1_MAGIC) \
113
(dp)->dp1.field = (val); \
115
(dp)->dp2.field = (val); \
117
static ufs2_daddr_t inoblk; /* inode block address */
118
static char inobuf[MAXBSIZE]; /* inode block */
119
ino_t maxino; /* last valid inode */
120
static int unlabeled; /* unlabeled partition, e.g. vinum volume etc. */
123
* An array of elements of type struct gfs_bpp describes all blocks to
124
* be relocated in order to free the space needed for the cylinder group
125
* summary for all cylinder groups located in the first cylinder group.
128
ufs2_daddr_t old; /* old block number */
129
ufs2_daddr_t new; /* new block number */
130
#define GFS_FL_FIRST 1
131
#define GFS_FL_LAST 2
132
unsigned int flags; /* special handling required */
133
int found; /* how many references were updated */
136
/* ******************************************************** PROTOTYPES ***** */
137
static void growfs(int, int, unsigned int);
138
static void rdfs(ufs2_daddr_t, size_t, void *, int);
139
static void wtfs(ufs2_daddr_t, size_t, void *, int, unsigned int);
140
static ufs2_daddr_t alloc(void);
141
static int charsperline(void);
142
static void usage(void);
143
static int isblock(struct fs *, unsigned char *, int);
144
static void clrblock(struct fs *, unsigned char *, int);
145
static void setblock(struct fs *, unsigned char *, int);
146
static void initcg(int, time_t, int, unsigned int);
147
static void updjcg(int, time_t, int, int, unsigned int);
148
static void updcsloc(time_t, int, int, unsigned int);
149
static struct disklabel *get_disklabel(int);
150
static void return_disklabel(int, struct disklabel *, unsigned int);
151
static union dinode *ginode(ino_t, int, int);
152
static void frag_adjust(ufs2_daddr_t, int);
153
static int cond_bl_upd(ufs2_daddr_t *, struct gfs_bpp *, int, int,
155
static void updclst(int);
156
static void updrefs(int, ino_t, struct gfs_bpp *, int, int, unsigned int);
157
static void indirchk(ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t, ufs_lbn_t,
158
struct gfs_bpp *, int, int, unsigned int);
159
static void get_dev_size(int, int *);
161
/* ************************************************************ growfs ***** */
163
* Here we actually start growing the file system. We basically read the
164
* cylinder summary from the first cylinder group as we want to update
165
* this on the fly during our various operations. First we handle the
166
* changes in the former last cylinder group. Afterwards we create all new
167
* cylinder groups. Now we handle the cylinder group containing the
168
* cylinder summary which might result in a relocation of the whole
169
* structure. In the end we write back the updated cylinder summary, the
170
* new superblock, and slightly patched versions of the super block
174
growfs(int fsi, int fso, unsigned int Nflag)
183
static int randinit=0;
191
#else /* not FSIRAND */
199
* Get the cylinder summary into the memory.
201
fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize);
203
errx(1, "calloc failed");
205
for (i = 0; i < osblock.fs_cssize; i += osblock.fs_bsize) {
206
rdfs(fsbtodb(&osblock, osblock.fs_csaddr +
207
numfrags(&osblock, i)), (size_t)MIN(osblock.fs_cssize - i,
208
osblock.fs_bsize), (void *)(((char *)fscs)+i), fsi);
213
struct csum *dbg_csp;
218
for(dbg_csc=0; dbg_csc<osblock.fs_ncg; dbg_csc++) {
219
snprintf(dbg_line, sizeof(dbg_line),
220
"%d. old csum in old location", dbg_csc);
221
DBG_DUMP_CSUM(&osblock,
226
#endif /* FS_DEBUG */
227
DBG_PRINT0("fscs read\n");
230
* Do all needed changes in the former last cylinder group.
232
updjcg(osblock.fs_ncg-1, utime, fsi, fso, Nflag);
235
* Dump out summary information about file system.
237
# define B2MBFACTOR (1 / (1024.0 * 1024.0))
238
printf("growfs: %.1fMB (%jd sectors) block size %d, fragment size %d\n",
239
(float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
240
(intmax_t)fsbtodb(&sblock, sblock.fs_size), sblock.fs_bsize,
242
printf("\tusing %d cylinder groups of %.2fMB, %d blks, %d inodes.\n",
243
sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
244
sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
245
if (sblock.fs_flags & FS_DOSOFTDEP)
246
printf("\twith soft updates\n");
250
* Now build the cylinders group blocks and
251
* then print out indices of cylinder groups.
253
printf("super-block backups (for fsck -b #) at:\n");
255
width = charsperline();
258
* Iterate for only the new cylinder groups.
260
for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
261
initcg(cylno, utime, fso, Nflag);
262
j = sprintf(tmpbuf, " %jd%s",
263
(intmax_t)fsbtodb(&sblock, cgsblock(&sblock, cylno)),
264
cylno < (sblock.fs_ncg-1) ? "," : "" );
265
if (i + j >= width) {
270
printf("%s", tmpbuf);
276
* Do all needed changes in the first cylinder group.
277
* allocate blocks in new location
279
updcsloc(utime, fsi, fso, Nflag);
282
* Now write the cylinder summary back to disk.
284
for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
285
wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
286
(size_t)MIN(sblock.fs_cssize - i, sblock.fs_bsize),
287
(void *)(((char *)fscs) + i), fso, Nflag);
289
DBG_PRINT0("fscs written\n");
293
struct csum *dbg_csp;
298
for(dbg_csc=0; dbg_csc<sblock.fs_ncg; dbg_csc++) {
299
snprintf(dbg_line, sizeof(dbg_line),
300
"%d. new csum in new location", dbg_csc);
301
DBG_DUMP_CSUM(&sblock,
306
#endif /* FS_DEBUG */
309
* Now write the new superblock back to disk.
311
sblock.fs_time = utime;
312
wtfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
313
DBG_PRINT0("sblock written\n");
315
"new initial sblock");
318
* Clean up the dynamic fields in our superblock copies.
323
sblock.fs_cgrotor = 0;
325
memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
326
sblock.fs_flags &= FS_DOSOFTDEP;
330
* The following fields are currently distributed from the superblock
338
* fs_flags regarding SOFTPDATES
340
* We probably should rather change the summary for the cylinder group
341
* statistics here to the value of what would be in there, if the file
342
* system were created initially with the new size. Therefor we still
343
* need to find an easy way of calculating that.
344
* Possibly we can try to read the first superblock copy and apply the
345
* "diffed" stats between the old and new superblock by still copying
346
* certain parameters onto that.
350
* Write out the duplicate super blocks.
352
for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
353
wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
354
(size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
356
DBG_PRINT0("sblock copies written\n");
358
"new other sblocks");
364
/* ************************************************************ initcg ***** */
366
* This creates a new cylinder group structure, for more details please see
367
* the source of newfs(8), as this function is taken over almost unchanged.
368
* As this is never called for the first cylinder group, the special
369
* provisions for that case are removed here.
372
initcg(int cylno, time_t utime, int fso, unsigned int Nflag)
376
long d, dlower, dupper, blkno, start;
377
ufs2_daddr_t i, cbase, dmax;
378
struct ufs1_dinode *dp1;
379
struct ufs2_dinode *dp2;
382
if (iobuf == NULL && (iobuf = malloc(sblock.fs_bsize)) == NULL) {
383
errx(37, "panic: cannot allocate I/O buffer");
386
* Determine block bounds for cylinder group.
387
* Allow space for super block summary information in first
390
cbase = cgbase(&sblock, cylno);
391
dmax = cbase + sblock.fs_fpg;
392
if (dmax > sblock.fs_size)
393
dmax = sblock.fs_size;
394
dlower = cgsblock(&sblock, cylno) - cbase;
395
dupper = cgdmin(&sblock, cylno) - cbase;
396
if (cylno == 0) /* XXX fscs may be relocated */
397
dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
399
memset(&acg, 0, sblock.fs_cgsize);
401
acg.cg_magic = CG_MAGIC;
403
acg.cg_niblk = sblock.fs_ipg;
404
acg.cg_initediblk = sblock.fs_ipg;
405
acg.cg_ndblk = dmax - cbase;
406
if (sblock.fs_contigsumsize > 0)
407
acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
408
start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
409
if (sblock.fs_magic == FS_UFS2_MAGIC) {
410
acg.cg_iusedoff = start;
412
acg.cg_old_ncyl = sblock.fs_old_cpg;
413
acg.cg_old_time = acg.cg_time;
415
acg.cg_old_niblk = acg.cg_niblk;
417
acg.cg_initediblk = 0;
418
acg.cg_old_btotoff = start;
419
acg.cg_old_boff = acg.cg_old_btotoff +
420
sblock.fs_old_cpg * sizeof(int32_t);
421
acg.cg_iusedoff = acg.cg_old_boff +
422
sblock.fs_old_cpg * sizeof(u_int16_t);
424
acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
425
acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT);
426
if (sblock.fs_contigsumsize > 0) {
427
acg.cg_clustersumoff =
428
roundup(acg.cg_nextfreeoff, sizeof(u_int32_t));
429
acg.cg_clustersumoff -= sizeof(u_int32_t);
430
acg.cg_clusteroff = acg.cg_clustersumoff +
431
(sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
432
acg.cg_nextfreeoff = acg.cg_clusteroff +
433
howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
435
if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
437
* This should never happen as we would have had that panic
438
* already on file system creation
440
errx(37, "panic: cylinder group too big");
442
acg.cg_cs.cs_nifree += sblock.fs_ipg;
444
for (i = 0; i < ROOTINO; i++) {
445
setbit(cg_inosused(&acg), i);
446
acg.cg_cs.cs_nifree--;
449
* XXX Newfs writes out two blocks of initialized inodes
450
* unconditionally. Should we check here to make sure that they
451
* were actually written?
453
if (sblock.fs_magic == FS_UFS1_MAGIC) {
454
bzero(iobuf, sblock.fs_bsize);
455
for (i = 2 * sblock.fs_frag; i < sblock.fs_ipg / INOPF(&sblock);
456
i += sblock.fs_frag) {
457
dp1 = (struct ufs1_dinode *)iobuf;
458
dp2 = (struct ufs2_dinode *)iobuf;
460
for (j = 0; j < INOPB(&sblock); j++)
461
if (sblock.fs_magic == FS_UFS1_MAGIC) {
462
dp1->di_gen = random();
465
dp2->di_gen = random();
469
wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
470
sblock.fs_bsize, iobuf, fso, Nflag);
475
* In cylno 0, beginning space is reserved
476
* for boot and super blocks.
478
for (d = 0; d < dlower; d += sblock.fs_frag) {
479
blkno = d / sblock.fs_frag;
480
setblock(&sblock, cg_blksfree(&acg), blkno);
481
if (sblock.fs_contigsumsize > 0)
482
setbit(cg_clustersfree(&acg), blkno);
483
acg.cg_cs.cs_nbfree++;
485
sblock.fs_dsize += dlower;
487
sblock.fs_dsize += acg.cg_ndblk - dupper;
488
if ((i = dupper % sblock.fs_frag)) {
489
acg.cg_frsum[sblock.fs_frag - i]++;
490
for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
491
setbit(cg_blksfree(&acg), dupper);
492
acg.cg_cs.cs_nffree++;
495
for (d = dupper; d + sblock.fs_frag <= acg.cg_ndblk;
496
d += sblock.fs_frag) {
497
blkno = d / sblock.fs_frag;
498
setblock(&sblock, cg_blksfree(&acg), blkno);
499
if (sblock.fs_contigsumsize > 0)
500
setbit(cg_clustersfree(&acg), blkno);
501
acg.cg_cs.cs_nbfree++;
503
if (d < acg.cg_ndblk) {
504
acg.cg_frsum[acg.cg_ndblk - d]++;
505
for (; d < acg.cg_ndblk; d++) {
506
setbit(cg_blksfree(&acg), d);
507
acg.cg_cs.cs_nffree++;
510
if (sblock.fs_contigsumsize > 0) {
511
int32_t *sump = cg_clustersum(&acg);
512
u_char *mapp = cg_clustersfree(&acg);
517
for (i = 0; i < acg.cg_nclusterblks; i++) {
518
if ((map & bit) != 0)
521
if (run > sblock.fs_contigsumsize)
522
run = sblock.fs_contigsumsize;
526
if ((i & (CHAR_BIT - 1)) != CHAR_BIT - 1)
534
if (run > sblock.fs_contigsumsize)
535
run = sblock.fs_contigsumsize;
539
sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
540
sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
541
sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
542
sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
544
wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
545
sblock.fs_bsize, (char *)&acg, fso, Nflag);
554
/* ******************************************************* frag_adjust ***** */
556
* Here we add or subtract (sign +1/-1) the available fragments in a given
557
* block to or from the fragment statistics. By subtracting before and adding
558
* after an operation on the free frag map we can easy update the fragment
559
* statistic, which seems to be otherwise a rather complex operation.
562
frag_adjust(ufs2_daddr_t frag, int sign)
564
DBG_FUNC("frag_adjust")
572
* Here frag only needs to point to any fragment in the block we want
575
for(f=rounddown(frag, sblock.fs_frag);
576
f<roundup(frag+1, sblock.fs_frag);
579
* Count contiguous free fragments.
581
if(isset(cg_blksfree(&acg), f)) {
584
if(fragsize && fragsize<sblock.fs_frag) {
586
* We found something in between.
588
acg.cg_frsum[fragsize]+=sign;
589
DBG_PRINT2("frag_adjust [%d]+=%d\n",
596
if(fragsize && fragsize<sblock.fs_frag) {
598
* We found something.
600
acg.cg_frsum[fragsize]+=sign;
601
DBG_PRINT2("frag_adjust [%d]+=%d\n",
605
DBG_PRINT2("frag_adjust [[%d]]+=%d\n",
613
/* ******************************************************* cond_bl_upd ***** */
615
* Here we conditionally update a pointer to a fragment. We check for all
616
* relocated blocks if any of its fragments is referenced by the current
617
* field, and update the pointer to the respective fragment in our new
618
* block. If we find a reference we write back the block immediately,
619
* as there is no easy way for our general block reading engine to figure
620
* out if a write back operation is needed.
623
cond_bl_upd(ufs2_daddr_t *block, struct gfs_bpp *field, int fsi, int fso,
626
DBG_FUNC("cond_bl_upd")
628
ufs2_daddr_t src, dst;
634
for (f = field; f->old != 0; f++) {
636
if (fragstoblks(&sblock, src) != f->old)
639
* The fragment is part of the block, so update.
641
dst = blkstofrags(&sblock, f->new);
642
fragnum = fragnum(&sblock, src);
643
*block = dst + fragnum;
645
DBG_PRINT3("scg (%jd->%jd)[%d] reference updated\n",
651
* Copy the block back immediately.
653
* XXX If src is is from an indirect block we have
654
* to implement copy on write here in case of
657
ibuf = malloc(sblock.fs_bsize);
659
errx(1, "malloc failed");
661
rdfs(fsbtodb(&sblock, src), (size_t)sblock.fs_bsize, ibuf, fsi);
662
wtfs(dst, (size_t)sblock.fs_bsize, ibuf, fso, Nflag);
665
* The same block can't be found again in this loop.
674
/* ************************************************************ updjcg ***** */
676
* Here we do all needed work for the former last cylinder group. It has to be
677
* changed in any case, even if the file system ended exactly on the end of
678
* this group, as there is some slightly inconsistent handling of the number
679
* of cylinders in the cylinder group. We start again by reading the cylinder
680
* group from disk. If the last block was not fully available, we first handle
681
* the missing fragments, then we handle all new full blocks in that file
682
* system and finally we handle the new last fragmented block in the file
683
* system. We again have to handle the fragment statistics rotational layout
684
* tables and cluster summary during all those operations.
687
updjcg(int cylno, time_t utime, int fsi, int fso, unsigned int Nflag)
690
ufs2_daddr_t cbase, dmax, dupper;
698
* Read the former last (joining) cylinder group from disk, and make
701
rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
702
(size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
703
DBG_PRINT0("jcg read\n");
708
memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
711
* If the cylinder group had already its new final size almost
712
* nothing is to be done ... except:
713
* For some reason the value of cg_ncyl in the last cylinder group has
714
* to be zero instead of fs_cpg. As this is now no longer the last
715
* cylinder group we have to change that value now to fs_cpg.
718
if(cgbase(&osblock, cylno+1) == osblock.fs_size) {
719
if (sblock.fs_magic == FS_UFS1_MAGIC)
720
acg.cg_old_ncyl=sblock.fs_old_cpg;
722
wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
723
(size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
724
DBG_PRINT0("jcg written\n");
734
* Set up some variables needed later.
736
cbase = cgbase(&sblock, cylno);
737
dmax = cbase + sblock.fs_fpg;
738
if (dmax > sblock.fs_size)
739
dmax = sblock.fs_size;
740
dupper = cgdmin(&sblock, cylno) - cbase;
741
if (cylno == 0) { /* XXX fscs may be relocated */
742
dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
746
* Set pointer to the cylinder summary for our cylinder group.
751
* Touch the cylinder group, update all fields in the cylinder group as
752
* needed, update the free space in the superblock.
755
if (cylno == sblock.fs_ncg - 1) {
757
* This is still the last cylinder group.
759
if (sblock.fs_magic == FS_UFS1_MAGIC)
761
sblock.fs_old_ncyl % sblock.fs_old_cpg;
763
acg.cg_old_ncyl = sblock.fs_old_cpg;
765
DBG_PRINT2("jcg dbg: %d %u",
769
if (sblock.fs_magic == FS_UFS1_MAGIC)
775
acg.cg_ndblk = dmax - cbase;
776
sblock.fs_dsize += acg.cg_ndblk-aocg.cg_ndblk;
777
if (sblock.fs_contigsumsize > 0) {
778
acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
782
* Now we have to update the free fragment bitmap for our new free
783
* space. There again we have to handle the fragmentation and also
784
* the rotational layout tables and the cluster summary. This is
785
* also done per fragment for the first new block if the old file
786
* system end was not on a block boundary, per fragment for the new
787
* last block if the new file system end is not on a block boundary,
788
* and per block for all space in between.
790
* Handle the first new block here if it was partially available
793
if(osblock.fs_size % sblock.fs_frag) {
794
if(roundup(osblock.fs_size, sblock.fs_frag)<=sblock.fs_size) {
796
* The new space is enough to fill at least this
800
for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag)-1;
801
i>=osblock.fs_size-cbase;
803
setbit(cg_blksfree(&acg), i);
804
acg.cg_cs.cs_nffree++;
809
* Check if the fragment just created could join an
810
* already existing fragment at the former end of the
813
if(isblock(&sblock, cg_blksfree(&acg),
814
((osblock.fs_size - cgbase(&sblock, cylno))/
817
* The block is now completely available.
819
DBG_PRINT0("block was\n");
820
acg.cg_frsum[osblock.fs_size%sblock.fs_frag]--;
821
acg.cg_cs.cs_nbfree++;
822
acg.cg_cs.cs_nffree-=sblock.fs_frag;
823
k=rounddown(osblock.fs_size-cbase,
825
updclst((osblock.fs_size-cbase)/sblock.fs_frag);
828
* Lets rejoin a possible partially growed
832
while(isset(cg_blksfree(&acg), i) &&
833
(i>=rounddown(osblock.fs_size-cbase,
845
* We only grow by some fragments within this last
848
for(i=sblock.fs_size-cbase-1;
849
i>=osblock.fs_size-cbase;
851
setbit(cg_blksfree(&acg), i);
852
acg.cg_cs.cs_nffree++;
856
* Lets rejoin a possible partially growed fragment.
859
while(isset(cg_blksfree(&acg), i) &&
860
(i>=rounddown(osblock.fs_size-cbase,
873
* Handle all new complete blocks here.
875
for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag);
876
i+sblock.fs_frag<=dmax-cbase; /* XXX <= or only < ? */
878
j = i / sblock.fs_frag;
879
setblock(&sblock, cg_blksfree(&acg), j);
881
acg.cg_cs.cs_nbfree++;
885
* Handle the last new block if there are stll some new fragments left.
886
* Here we don't have to bother about the cluster summary or the even
887
* the rotational layout table.
889
if (i < (dmax - cbase)) {
890
acg.cg_frsum[dmax - cbase - i]++;
891
for (; i < dmax - cbase; i++) {
892
setbit(cg_blksfree(&acg), i);
893
acg.cg_cs.cs_nffree++;
897
sblock.fs_cstotal.cs_nffree +=
898
(acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree);
899
sblock.fs_cstotal.cs_nbfree +=
900
(acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree);
902
* The following statistics are not changed here:
903
* sblock.fs_cstotal.cs_ndir
904
* sblock.fs_cstotal.cs_nifree
905
* As the statistics for this cylinder group are ready, copy it to
906
* the summary information array.
911
* Write the updated "joining" cylinder group back to disk.
913
wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
914
(void *)&acg, fso, Nflag);
915
DBG_PRINT0("jcg written\n");
924
/* ********************************************************** updcsloc ***** */
926
* Here we update the location of the cylinder summary. We have two possible
927
* ways of growing the cylinder summary.
928
* (1) We can try to grow the summary in the current location, and relocate
929
* possibly used blocks within the current cylinder group.
930
* (2) Alternatively we can relocate the whole cylinder summary to the first
931
* new completely empty cylinder group. Once the cylinder summary is no
932
* longer in the beginning of the first cylinder group you should never
933
* use a version of fsck which is not aware of the possibility to have
934
* this structure in a non standard place.
935
* Option (1) is considered to be less intrusive to the structure of the file-
936
* system. So we try to stick to that whenever possible. If there is not enough
937
* space in the cylinder group containing the cylinder summary we have to use
938
* method (2). In case of active snapshots in the file system we probably can
939
* completely avoid implementing copy on write if we stick to method (2) only.
942
updcsloc(time_t utime, int fsi, int fso, unsigned int Nflag)
948
ufs2_daddr_t cbase, dupper, odupper, d, f, g;
958
if(howmany(sblock.fs_cssize, sblock.fs_fsize) ==
959
howmany(osblock.fs_cssize, osblock.fs_fsize)) {
961
* No new fragment needed.
966
ocscg=dtog(&osblock, osblock.fs_csaddr);
968
blocks = 1+howmany(sblock.fs_cssize, sblock.fs_bsize)-
969
howmany(osblock.fs_cssize, osblock.fs_bsize);
972
* Read original cylinder group from disk, and make a copy.
973
* XXX If Nflag is set in some very rare cases we now miss
974
* some changes done in updjcg by reading the unmodified
977
rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
978
(size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
979
DBG_PRINT0("oscg read\n");
984
memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
987
* Touch the cylinder group, set up local variables needed later
988
* and update the superblock.
993
* XXX In the case of having active snapshots we may need much more
994
* blocks for the copy on write. We need each block twice, and
995
* also up to 8*3 blocks for indirect blocks for all possible
998
if(/*((int)sblock.fs_time&0x3)>0||*/ cs->cs_nbfree < blocks) {
1000
* There is not enough space in the old cylinder group to
1001
* relocate all blocks as needed, so we relocate the whole
1002
* cylinder group summary to a new group. We try to use the
1003
* first complete new cylinder group just created. Within the
1004
* cylinder group we align the area immediately after the
1005
* cylinder group information location in order to be as
1006
* close as possible to the original implementation of ffs.
1008
* First we have to make sure we'll find enough space in the
1009
* new cylinder group. If not, then we currently give up.
1010
* We start with freeing everything which was used by the
1011
* fragments of the old cylinder summary in the current group.
1012
* Now we write back the group meta data, read in the needed
1013
* meta data from the new cylinder group, and start allocating
1014
* within that group. Here we can assume, the group to be
1015
* completely empty. Which makes the handling of fragments and
1016
* clusters a lot easier.
1019
if(sblock.fs_ncg-osblock.fs_ncg < 2) {
1020
errx(2, "panic: not enough space");
1024
* Point "d" to the first fragment not used by the cylinder
1027
d=osblock.fs_csaddr+(osblock.fs_cssize/osblock.fs_fsize);
1030
* Set up last cluster size ("lcs") already here. Calculate
1031
* the size for the trailing cluster just behind where "d"
1034
if(sblock.fs_contigsumsize > 0) {
1035
for(block=howmany(d%sblock.fs_fpg, sblock.fs_frag),
1036
lcs=0; lcs<sblock.fs_contigsumsize;
1038
if(isclr(cg_clustersfree(&acg), block)){
1045
* Point "d" to the last frag used by the cylinder summary.
1049
DBG_PRINT1("d=%jd\n",
1051
if((d+1)%sblock.fs_frag) {
1053
* The end of the cylinder summary is not a complete
1057
frag_adjust(d%sblock.fs_fpg, -1);
1058
for(; (d+1)%sblock.fs_frag; d--) {
1059
DBG_PRINT1("d=%jd\n",
1061
setbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1062
acg.cg_cs.cs_nffree++;
1063
sblock.fs_cstotal.cs_nffree++;
1066
* Point "d" to the last fragment of the last
1067
* (incomplete) block of the cylinder summary.
1070
frag_adjust(d%sblock.fs_fpg, 1);
1072
if(isblock(&sblock, cg_blksfree(&acg),
1073
(d%sblock.fs_fpg)/sblock.fs_frag)) {
1074
DBG_PRINT1("d=%jd\n", (intmax_t)d);
1075
acg.cg_cs.cs_nffree-=sblock.fs_frag;
1076
acg.cg_cs.cs_nbfree++;
1077
sblock.fs_cstotal.cs_nffree-=sblock.fs_frag;
1078
sblock.fs_cstotal.cs_nbfree++;
1079
if(sblock.fs_contigsumsize > 0) {
1080
setbit(cg_clustersfree(&acg),
1081
(d%sblock.fs_fpg)/sblock.fs_frag);
1082
if(lcs < sblock.fs_contigsumsize) {
1088
cg_clustersum(&acg)[lcs]++;
1093
* Point "d" to the first fragment of the block before
1094
* the last incomplete block.
1099
DBG_PRINT1("d=%jd\n", (intmax_t)d);
1100
for(d=rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
1101
d-=sblock.fs_frag) {
1103
DBG_PRINT1("d=%jd\n", (intmax_t)d);
1104
setblock(&sblock, cg_blksfree(&acg),
1105
(d%sblock.fs_fpg)/sblock.fs_frag);
1106
acg.cg_cs.cs_nbfree++;
1107
sblock.fs_cstotal.cs_nbfree++;
1108
if(sblock.fs_contigsumsize > 0) {
1109
setbit(cg_clustersfree(&acg),
1110
(d%sblock.fs_fpg)/sblock.fs_frag);
1112
* The last cluster size is already set up.
1114
if(lcs < sblock.fs_contigsumsize) {
1116
cg_clustersum(&acg)[lcs]--;
1119
cg_clustersum(&acg)[lcs]++;
1126
* Now write the former cylinder group containing the cylinder
1127
* summary back to disk.
1129
wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
1130
(size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1131
DBG_PRINT0("oscg written\n");
1132
DBG_DUMP_CG(&sblock,
1137
* Find the beginning of the new cylinder group containing the
1140
sblock.fs_csaddr=cgdmin(&sblock, osblock.fs_ncg);
1141
ncscg=dtog(&sblock, sblock.fs_csaddr);
1146
* If Nflag is specified, we would now read random data instead
1147
* of an empty cg structure from disk. So we can't simulate that
1151
DBG_PRINT0("nscg update skipped\n");
1157
* Read the future cylinder group containing the cylinder
1158
* summary from disk, and make a copy.
1160
rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1161
(size_t)sblock.fs_cgsize, (void *)&aocg, fsi);
1162
DBG_PRINT0("nscg read\n");
1163
DBG_DUMP_CG(&sblock,
1167
memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
1170
* Allocate all complete blocks used by the new cylinder
1173
for(d=sblock.fs_csaddr; d+sblock.fs_frag <=
1174
sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize);
1175
d+=sblock.fs_frag) {
1176
clrblock(&sblock, cg_blksfree(&acg),
1177
(d%sblock.fs_fpg)/sblock.fs_frag);
1178
acg.cg_cs.cs_nbfree--;
1179
sblock.fs_cstotal.cs_nbfree--;
1180
if(sblock.fs_contigsumsize > 0) {
1181
clrbit(cg_clustersfree(&acg),
1182
(d%sblock.fs_fpg)/sblock.fs_frag);
1187
* Allocate all fragments used by the cylinder summary in the
1190
if(d<sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize)) {
1191
for(; d-sblock.fs_csaddr<
1192
sblock.fs_cssize/sblock.fs_fsize;
1194
clrbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1195
acg.cg_cs.cs_nffree--;
1196
sblock.fs_cstotal.cs_nffree--;
1198
acg.cg_cs.cs_nbfree--;
1199
acg.cg_cs.cs_nffree+=sblock.fs_frag;
1200
sblock.fs_cstotal.cs_nbfree--;
1201
sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1202
if(sblock.fs_contigsumsize > 0) {
1203
clrbit(cg_clustersfree(&acg),
1204
(d%sblock.fs_fpg)/sblock.fs_frag);
1207
frag_adjust(d%sblock.fs_fpg, +1);
1210
* XXX Handle the cluster statistics here in the case this
1211
* cylinder group is now almost full, and the remaining
1212
* space is less then the maximum cluster size. This is
1213
* probably not needed, as you would hardly find a file
1214
* system which has only MAXCSBUFS+FS_MAXCONTIG of free
1215
* space right behind the cylinder group information in
1216
* any new cylinder group.
1220
* Update our statistics in the cylinder summary.
1225
* Write the new cylinder group containing the cylinder summary
1228
wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1229
(size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1230
DBG_PRINT0("nscg written\n");
1231
DBG_DUMP_CG(&sblock,
1239
* We have got enough of space in the current cylinder group, so we
1240
* can relocate just a few blocks, and let the summary information
1241
* grow in place where it is right now.
1245
cbase = cgbase(&osblock, ocscg); /* old and new are equal */
1246
dupper = sblock.fs_csaddr - cbase +
1247
howmany(sblock.fs_cssize, sblock.fs_fsize);
1248
odupper = osblock.fs_csaddr - cbase +
1249
howmany(osblock.fs_cssize, osblock.fs_fsize);
1251
sblock.fs_dsize -= dupper-odupper;
1254
* Allocate the space for the array of blocks to be relocated.
1256
bp=(struct gfs_bpp *)malloc(((dupper-odupper)/sblock.fs_frag+2)*
1257
sizeof(struct gfs_bpp));
1259
errx(1, "malloc failed");
1261
memset((char *)bp, 0, ((dupper-odupper)/sblock.fs_frag+2)*
1262
sizeof(struct gfs_bpp));
1265
* Lock all new frags needed for the cylinder group summary. This is
1266
* done per fragment in the first and last block of the new required
1267
* area, and per block for all other blocks.
1269
* Handle the first new block here (but only if some fragments where
1270
* already used for the cylinder summary).
1273
frag_adjust(odupper, -1);
1274
for(d=odupper; ((d<dupper)&&(d%sblock.fs_frag)); d++) {
1275
DBG_PRINT1("scg first frag check loop d=%jd\n",
1277
if(isclr(cg_blksfree(&acg), d)) {
1279
bp[ind].old=d/sblock.fs_frag;
1280
bp[ind].flags|=GFS_FL_FIRST;
1281
if(roundup(d, sblock.fs_frag) >= dupper) {
1282
bp[ind].flags|=GFS_FL_LAST;
1287
clrbit(cg_blksfree(&acg), d);
1288
acg.cg_cs.cs_nffree--;
1289
sblock.fs_cstotal.cs_nffree--;
1292
* No cluster handling is needed here, as there was at least
1293
* one fragment in use by the cylinder summary in the old
1295
* No block-free counter handling here as this block was not
1299
frag_adjust(odupper, 1);
1302
* Handle all needed complete blocks here.
1304
for(; d+sblock.fs_frag<=dupper; d+=sblock.fs_frag) {
1305
DBG_PRINT1("scg block check loop d=%jd\n",
1307
if(!isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) {
1308
for(f=d; f<d+sblock.fs_frag; f++) {
1309
if(isset(cg_blksfree(&aocg), f)) {
1310
acg.cg_cs.cs_nffree--;
1311
sblock.fs_cstotal.cs_nffree--;
1314
clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
1315
bp[ind].old=d/sblock.fs_frag;
1318
clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
1319
acg.cg_cs.cs_nbfree--;
1320
sblock.fs_cstotal.cs_nbfree--;
1321
if(sblock.fs_contigsumsize > 0) {
1322
clrbit(cg_clustersfree(&acg), d/sblock.fs_frag);
1323
for(lcs=0, l=(d/sblock.fs_frag)+1;
1324
lcs<sblock.fs_contigsumsize;
1326
if(isclr(cg_clustersfree(&acg),l)){
1330
if(lcs < sblock.fs_contigsumsize) {
1331
cg_clustersum(&acg)[lcs+1]--;
1333
cg_clustersum(&acg)[lcs]++;
1339
* No fragment counter handling is needed here, as this finally
1340
* doesn't change after the relocation.
1345
* Handle all fragments needed in the last new affected block.
1348
frag_adjust(dupper-1, -1);
1350
if(isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) {
1351
acg.cg_cs.cs_nbfree--;
1352
sblock.fs_cstotal.cs_nbfree--;
1353
acg.cg_cs.cs_nffree+=sblock.fs_frag;
1354
sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1355
if(sblock.fs_contigsumsize > 0) {
1356
clrbit(cg_clustersfree(&acg), d/sblock.fs_frag);
1357
for(lcs=0, l=(d/sblock.fs_frag)+1;
1358
lcs<sblock.fs_contigsumsize;
1360
if(isclr(cg_clustersfree(&acg),l)){
1364
if(lcs < sblock.fs_contigsumsize) {
1365
cg_clustersum(&acg)[lcs+1]--;
1367
cg_clustersum(&acg)[lcs]++;
1373
for(; d<dupper; d++) {
1374
DBG_PRINT1("scg second frag check loop d=%jd\n",
1376
if(isclr(cg_blksfree(&acg), d)) {
1377
bp[ind].old=d/sblock.fs_frag;
1378
bp[ind].flags|=GFS_FL_LAST;
1380
clrbit(cg_blksfree(&acg), d);
1381
acg.cg_cs.cs_nffree--;
1382
sblock.fs_cstotal.cs_nffree--;
1385
if(bp[ind].flags & GFS_FL_LAST) { /* we have to advance here */
1388
frag_adjust(dupper-1, 1);
1392
* If we found a block to relocate just do so.
1395
for(i=0; i<ind; i++) {
1396
if(!bp[i].old) { /* no more blocks listed */
1398
* XXX A relative blocknumber should not be
1399
* zero, which is not explicitly
1400
* guaranteed by our code.
1405
* Allocate a complete block in the same (current)
1408
bp[i].new=alloc()/sblock.fs_frag;
1411
* There is no frag_adjust() needed for the new block
1412
* as it will have no fragments yet :-).
1414
for(f=bp[i].old*sblock.fs_frag,
1415
g=bp[i].new*sblock.fs_frag;
1416
f<(bp[i].old+1)*sblock.fs_frag;
1418
if(isset(cg_blksfree(&aocg), f)) {
1419
setbit(cg_blksfree(&acg), g);
1420
acg.cg_cs.cs_nffree++;
1421
sblock.fs_cstotal.cs_nffree++;
1426
* Special handling is required if this was the first
1427
* block. We have to consider the fragments which were
1428
* used by the cylinder summary in the original block
1429
* which re to be free in the copy of our block. We
1430
* have to be careful if this first block happens to
1431
* be also the last block to be relocated.
1433
if(bp[i].flags & GFS_FL_FIRST) {
1434
for(f=bp[i].old*sblock.fs_frag,
1435
g=bp[i].new*sblock.fs_frag;
1438
setbit(cg_blksfree(&acg), g);
1439
acg.cg_cs.cs_nffree++;
1440
sblock.fs_cstotal.cs_nffree++;
1442
if(!(bp[i].flags & GFS_FL_LAST)) {
1443
frag_adjust(bp[i].new*sblock.fs_frag,1);
1448
* Special handling is required if this is the last
1449
* block to be relocated.
1451
if(bp[i].flags & GFS_FL_LAST) {
1452
frag_adjust(bp[i].new*sblock.fs_frag, 1);
1453
frag_adjust(bp[i].old*sblock.fs_frag, -1);
1455
f<roundup(dupper, sblock.fs_frag);
1457
if(isclr(cg_blksfree(&acg), f)) {
1458
setbit(cg_blksfree(&acg), f);
1459
acg.cg_cs.cs_nffree++;
1460
sblock.fs_cstotal.cs_nffree++;
1463
frag_adjust(bp[i].old*sblock.fs_frag, 1);
1467
* !!! Attach the cylindergroup offset here.
1469
bp[i].old+=cbase/sblock.fs_frag;
1470
bp[i].new+=cbase/sblock.fs_frag;
1473
* Copy the content of the block.
1476
* XXX Here we will have to implement a copy on write
1477
* in the case we have any active snapshots.
1479
rdfs(fsbtodb(&sblock, bp[i].old*sblock.fs_frag),
1480
(size_t)sblock.fs_bsize, (void *)&ablk, fsi);
1481
wtfs(fsbtodb(&sblock, bp[i].new*sblock.fs_frag),
1482
(size_t)sblock.fs_bsize, (void *)&ablk, fso, Nflag);
1483
DBG_DUMP_HEX(&sblock,
1484
"copied full block",
1485
(unsigned char *)&ablk);
1487
DBG_PRINT2("scg (%jd->%jd) block relocated\n",
1488
(intmax_t)bp[i].old,
1489
(intmax_t)bp[i].new);
1493
* Now we have to update all references to any fragment which
1494
* belongs to any block relocated. We iterate now over all
1495
* cylinder groups, within those over all non zero length
1498
for(cylno=0; cylno<osblock.fs_ncg; cylno++) {
1499
DBG_PRINT1("scg doing cg (%d)\n",
1501
for(inc=osblock.fs_ipg-1 ; inc>0 ; inc--) {
1502
updrefs(cylno, (ino_t)inc, bp, fsi, fso, Nflag);
1507
* All inodes are checked, now make sure the number of
1508
* references found make sense.
1510
for(i=0; i<ind; i++) {
1511
if(!bp[i].found || (bp[i].found>sblock.fs_frag)) {
1512
warnx("error: %jd refs found for block %jd.",
1513
(intmax_t)bp[i].found, (intmax_t)bp[i].old);
1519
* The following statistics are not changed here:
1520
* sblock.fs_cstotal.cs_ndir
1521
* sblock.fs_cstotal.cs_nifree
1522
* The following statistics were already updated on the fly:
1523
* sblock.fs_cstotal.cs_nffree
1524
* sblock.fs_cstotal.cs_nbfree
1525
* As the statistics for this cylinder group are ready, copy it to
1526
* the summary information array.
1532
* Write summary cylinder group back to disk.
1534
wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), (size_t)sblock.fs_cgsize,
1535
(void *)&acg, fso, Nflag);
1536
DBG_PRINT0("scg written\n");
1537
DBG_DUMP_CG(&sblock,
1545
/* ************************************************************** rdfs ***** */
1547
* Here we read some block(s) from disk.
1550
rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi)
1558
err(32, "rdfs: attempting to read negative block number");
1560
if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
1561
err(33, "rdfs: seek error: %jd", (intmax_t)bno);
1563
n = read(fsi, bf, size);
1564
if (n != (ssize_t)size) {
1565
err(34, "rdfs: read error: %jd", (intmax_t)bno);
1572
/* ************************************************************** wtfs ***** */
1574
* Here we write some block(s) to disk.
1577
wtfs(ufs2_daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1588
if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) {
1589
err(35, "wtfs: seek error: %ld", (long)bno);
1591
n = write(fso, bf, size);
1592
if (n != (ssize_t)size) {
1593
err(36, "wtfs: write error: %ld", (long)bno);
1600
/* ************************************************************* alloc ***** */
1602
* Here we allocate a free block in the current cylinder group. It is assumed,
1603
* that acg contains the current cylinder group. As we may take a block from
1604
* somewhere in the file system we have to handle cluster summary here.
1610
ufs2_daddr_t d, blkno;
1614
int dlower, dupper, dmax;
1618
if (acg.cg_magic != CG_MAGIC) {
1619
warnx("acg: bad magic number");
1623
if (acg.cg_cs.cs_nbfree == 0) {
1624
warnx("error: cylinder group ran out of space");
1629
* We start seeking for free blocks only from the space available after
1630
* the end of the new grown cylinder summary. Otherwise we allocate a
1631
* block here which we have to relocate a couple of seconds later again
1632
* again, and we are not prepared to to this anyway.
1635
dlower=cgsblock(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx);
1636
dupper=cgdmin(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx);
1637
dmax=cgbase(&sblock, acg.cg_cgx)+sblock.fs_fpg;
1638
if (dmax > sblock.fs_size) {
1639
dmax = sblock.fs_size;
1641
dmax-=cgbase(&sblock, acg.cg_cgx); /* retransform into cg */
1642
csmin=sblock.fs_csaddr-cgbase(&sblock, acg.cg_cgx);
1643
csmax=csmin+howmany(sblock.fs_cssize, sblock.fs_fsize);
1644
DBG_PRINT3("seek range: dl=%d, du=%d, dm=%d\n",
1648
DBG_PRINT2("range cont: csmin=%d, csmax=%d\n",
1652
for(d=0; (d<dlower && blkno==-1); d+=sblock.fs_frag) {
1653
if(d>=csmin && d<=csmax) {
1656
if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1658
blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1662
for(d=dupper; (d<dmax && blkno==-1); d+=sblock.fs_frag) {
1663
if(d>=csmin && d<=csmax) {
1666
if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1668
blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1673
warnx("internal error: couldn't find promised block in cg");
1679
* This is needed if the block was found already in the first loop.
1681
d=blkstofrags(&sblock, blkno);
1683
clrblock(&sblock, cg_blksfree(&acg), blkno);
1684
if (sblock.fs_contigsumsize > 0) {
1686
* Handle the cluster allocation bitmap.
1688
clrbit(cg_clustersfree(&acg), blkno);
1690
* We possibly have split a cluster here, so we have to do
1691
* recalculate the sizes of the remaining cluster halves now,
1692
* and use them for updating the cluster summary information.
1694
* Lets start with the blocks before our allocated block ...
1696
for(lcs1=0, l=blkno-1; lcs1<sblock.fs_contigsumsize;
1698
if(isclr(cg_clustersfree(&acg),l)){
1703
* ... and continue with the blocks right after our allocated
1706
for(lcs2=0, l=blkno+1; lcs2<sblock.fs_contigsumsize;
1708
if(isclr(cg_clustersfree(&acg),l)){
1714
* Now update all counters.
1716
cg_clustersum(&acg)[MIN(lcs1+lcs2+1,sblock.fs_contigsumsize)]--;
1718
cg_clustersum(&acg)[lcs1]++;
1721
cg_clustersum(&acg)[lcs2]++;
1725
* Update all statistics based on blocks.
1727
acg.cg_cs.cs_nbfree--;
1728
sblock.fs_cstotal.cs_nbfree--;
1734
/* *********************************************************** isblock ***** */
1736
* Here we check if all frags of a block are free. For more details again
1737
* please see the source of newfs(8), as this function is taken over almost
1741
isblock(struct fs *fs, unsigned char *cp, int h)
1748
switch (fs->fs_frag) {
1751
return (cp[h] == 0xff);
1753
mask = 0x0f << ((h & 0x1) << 2);
1755
return ((cp[h >> 1] & mask) == mask);
1757
mask = 0x03 << ((h & 0x3) << 1);
1759
return ((cp[h >> 2] & mask) == mask);
1761
mask = 0x01 << (h & 0x7);
1763
return ((cp[h >> 3] & mask) == mask);
1765
fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1771
/* ********************************************************** clrblock ***** */
1773
* Here we allocate a complete block in the block map. For more details again
1774
* please see the source of newfs(8), as this function is taken over almost
1778
clrblock(struct fs *fs, unsigned char *cp, int h)
1780
DBG_FUNC("clrblock")
1784
switch ((fs)->fs_frag) {
1789
cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1792
cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1795
cp[h >> 3] &= ~(0x01 << (h & 0x7));
1798
warnx("clrblock bad fs_frag %d", fs->fs_frag);
1806
/* ********************************************************** setblock ***** */
1808
* Here we free a complete block in the free block map. For more details again
1809
* please see the source of newfs(8), as this function is taken over almost
1813
setblock(struct fs *fs, unsigned char *cp, int h)
1815
DBG_FUNC("setblock")
1819
switch (fs->fs_frag) {
1824
cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1827
cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1830
cp[h >> 3] |= (0x01 << (h & 0x7));
1833
warnx("setblock bad fs_frag %d", fs->fs_frag);
1841
/* ************************************************************ ginode ***** */
1843
* This function provides access to an individual inode. We find out in which
1844
* block the requested inode is located, read it from disk if needed, and
1845
* return the pointer into that block. We maintain a cache of one block to
1846
* not read the same block again and again if we iterate linearly over all
1849
static union dinode *
1850
ginode(ino_t inumber, int fsi, int cg)
1853
static ino_t startinum = 0; /* first inode in cached block */
1858
* The inumber passed in is relative to the cg, so use it here to see
1859
* if the inode has been allocated yet.
1861
if (isclr(cg_inosused(&aocg), inumber)) {
1866
* Now make the inumber relative to the entire inode space so it can
1867
* be sanity checked.
1869
inumber += (cg * sblock.fs_ipg);
1870
if (inumber < ROOTINO) {
1874
if (inumber > maxino)
1875
errx(8, "bad inode number %d to ginode", inumber);
1876
if (startinum == 0 ||
1877
inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
1878
inoblk = fsbtodb(&sblock, ino_to_fsba(&sblock, inumber));
1879
rdfs(inoblk, (size_t)sblock.fs_bsize, inobuf, fsi);
1880
startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
1883
if (sblock.fs_magic == FS_UFS1_MAGIC)
1884
return (union dinode *)((uintptr_t)inobuf +
1885
(inumber % INOPB(&sblock)) * sizeof(struct ufs1_dinode));
1886
return (union dinode *)((uintptr_t)inobuf +
1887
(inumber % INOPB(&sblock)) * sizeof(struct ufs2_dinode));
1890
/* ****************************************************** charsperline ***** */
1892
* Figure out how many lines our current terminal has. For more details again
1893
* please see the source of newfs(8), as this function is taken over almost
1899
DBG_FUNC("charsperline")
1907
if (ioctl(0, TIOCGWINSZ, &ws) != -1) {
1908
columns = ws.ws_col;
1910
if (columns == 0 && (cp = getenv("COLUMNS"))) {
1914
columns = 80; /* last resort */
1921
/* ****************************************************** get_dev_size ***** */
1923
* Get the size of the partition if we can't figure it out from the disklabel,
1924
* e.g. from vinum volumes.
1927
get_dev_size(int fd, int *size)
1932
if (ioctl(fd, DIOCGSECTORSIZE, §orsize) == -1)
1933
err(1,"DIOCGSECTORSIZE");
1934
if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == -1)
1935
err(1,"DIOCGMEDIASIZE");
1937
if (sectorsize <= 0)
1938
errx(1, "bogus sectorsize: %d", sectorsize);
1940
*size = mediasize / sectorsize;
1943
/* ************************************************************** main ***** */
1945
* growfs(8) is a utility which allows to increase the size of an existing
1946
* ufs file system. Currently this can only be done on unmounted file system.
1947
* It recognizes some command line options to specify the new desired size,
1948
* and it does some basic checkings. The old file system size is determined
1949
* and after some more checks like we can really access the new last block
1950
* on the disk etc. we calculate the new parameters for the superblock. After
1951
* having done this we just call growfs() which will do the work. Before
1952
* we finish the only thing left is to update the disklabel.
1953
* We still have to provide support for snapshots. Therefore we first have to
1954
* understand what data structures are always replicated in the snapshot on
1955
* creation, for all other blocks we touch during our procedure, we have to
1956
* keep the old blocks unchanged somewhere available for the snapshots. If we
1957
* are lucky, then we only have to handle our blocks to be relocated in that
1959
* Also we have to consider in what order we actually update the critical
1960
* data structures of the file system to make sure, that in case of a disaster
1961
* fsck(8) is still able to restore any lost data.
1962
* The foreseen last step then will be to provide for growing even mounted
1963
* file systems. There we have to extend the mount() system call to provide
1964
* userland access to the file system locking facility.
1967
main(int argc, char **argv)
1970
char *device, *special, *cp;
1972
unsigned int size=0;
1974
unsigned int Nflag=0;
1977
struct disklabel *lp;
1978
struct partition *pp;
1984
#endif /* FSMAXSNAP */
1988
while((ch=getopt(argc, argv, "Ns:vy")) != -1) {
1994
size=(size_t)atol(optarg);
1999
case 'v': /* for compatibility to newfs */
2019
* Now try to guess the (raw)device name.
2021
if (0 == strrchr(device, '/')) {
2023
* No path prefix was given, so try in that order:
2029
* FreeBSD now doesn't distinguish between raw and block
2030
* devices any longer, but it should still work this way.
2032
len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/");
2033
special=(char *)malloc(len);
2034
if(special == NULL) {
2035
errx(1, "malloc failed");
2037
snprintf(special, len, "%sr%s", _PATH_DEV, device);
2038
if (stat(special, &st) == -1) {
2039
snprintf(special, len, "%s%s", _PATH_DEV, device);
2040
if (stat(special, &st) == -1) {
2041
snprintf(special, len, "%svinum/r%s",
2043
if (stat(special, &st) == -1) {
2044
/* For now this is the 'last resort' */
2045
snprintf(special, len, "%svinum/%s",
2054
* Try to access our devices for writing ...
2059
fso = open(device, O_WRONLY);
2061
err(1, "%s", device);
2068
fsi = open(device, O_RDONLY);
2070
err(1, "%s", device);
2074
* Try to read a label and guess the slice if not specified. This
2075
* code should guess the right thing and avoid to bother the user
2076
* with the task of specifying the option -v on vinum volumes.
2078
cp=device+strlen(device)-1;
2079
lp = get_disklabel(fsi);
2083
pp = &lp->d_partitions[2];
2084
} else if (*cp>='a' && *cp<='h') {
2085
pp = &lp->d_partitions[*cp - 'a'];
2087
errx(1, "unknown device");
2089
p_size = pp->p_size;
2091
get_dev_size(fsi, &p_size);
2095
* Check if that partition is suitable for growing a file system.
2098
errx(1, "partition is unavailable");
2102
* Read the current superblock, and take a backup.
2104
for (i = 0; sblock_try[i] != -1; i++) {
2105
sblockloc = sblock_try[i] / DEV_BSIZE;
2106
rdfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&(osblock), fsi);
2107
if ((osblock.fs_magic == FS_UFS1_MAGIC ||
2108
(osblock.fs_magic == FS_UFS2_MAGIC &&
2109
osblock.fs_sblockloc == sblock_try[i])) &&
2110
osblock.fs_bsize <= MAXBSIZE &&
2111
osblock.fs_bsize >= (int32_t) sizeof(struct fs))
2114
if (sblock_try[i] == -1) {
2115
errx(1, "superblock not recognized");
2117
memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2));
2118
maxino = sblock.fs_ncg * sblock.fs_ipg;
2120
DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
2121
DBG_DUMP_FS(&sblock,
2125
* Determine size to grow to. Default to the full size specified in
2128
sblock.fs_size = dbtofsb(&osblock, p_size);
2131
errx(1, "there is not enough space (%d < %d)",
2134
sblock.fs_size = dbtofsb(&osblock, size);
2138
* Are we really growing ?
2140
if(osblock.fs_size >= sblock.fs_size) {
2141
errx(1, "we are not growing (%jd->%jd)",
2142
(intmax_t)osblock.fs_size, (intmax_t)sblock.fs_size);
2148
* Check if we find an active snapshot.
2150
if(ExpertFlag == 0) {
2151
for(j=0; j<FSMAXSNAP; j++) {
2152
if(sblock.fs_snapinum[j]) {
2153
errx(1, "active snapshot found in file system\n"
2154
" please remove all snapshots before "
2157
if(!sblock.fs_snapinum[j]) { /* list is dense */
2164
if (ExpertFlag == 0 && Nflag == 0) {
2165
printf("We strongly recommend you to make a backup "
2166
"before growing the Filesystem\n\n"
2167
" Did you backup your data (Yes/No) ? ");
2168
fgets(reply, (int)sizeof(reply), stdin);
2169
if (strcmp(reply, "Yes\n")){
2170
printf("\n Nothing done \n");
2175
printf("new file systemsize is: %jd frags\n", (intmax_t)sblock.fs_size);
2178
* Try to access our new last block in the file system. Even if we
2179
* later on realize we have to abort our operation, on that block
2180
* there should be no data, so we can't destroy something yet.
2182
wtfs((ufs2_daddr_t)p_size-1, (size_t)DEV_BSIZE, (void *)&sblock,
2186
* Now calculate new superblock values and check for reasonable
2187
* bound for new file system size:
2188
* fs_size: is derived from label or user input
2189
* fs_dsize: should get updated in the routines creating or
2190
* updating the cylinder groups on the fly
2191
* fs_cstotal: should get updated in the routines creating or
2192
* updating the cylinder groups
2196
* Update the number of cylinders and cylinder groups in the file system.
2198
if (sblock.fs_magic == FS_UFS1_MAGIC) {
2199
sblock.fs_old_ncyl =
2200
sblock.fs_size * sblock.fs_old_nspf / sblock.fs_old_spc;
2201
if (sblock.fs_size * sblock.fs_old_nspf >
2202
sblock.fs_old_ncyl * sblock.fs_old_spc)
2203
sblock.fs_old_ncyl++;
2205
sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
2206
maxino = sblock.fs_ncg * sblock.fs_ipg;
2208
if (sblock.fs_size % sblock.fs_fpg != 0 &&
2209
sblock.fs_size % sblock.fs_fpg < cgdmin(&sblock, sblock.fs_ncg)) {
2211
* The space in the new last cylinder group is too small,
2215
if (sblock.fs_magic == FS_UFS1_MAGIC)
2216
sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg;
2217
printf("Warning: %jd sector(s) cannot be allocated.\n",
2218
(intmax_t)fsbtodb(&sblock, sblock.fs_size % sblock.fs_fpg));
2219
sblock.fs_size = sblock.fs_ncg * sblock.fs_fpg;
2223
* Update the space for the cylinder group summary information in the
2224
* respective cylinder group data area.
2227
fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
2229
if(osblock.fs_size >= sblock.fs_size) {
2230
errx(1, "not enough new space");
2233
DBG_PRINT0("sblock calculated\n");
2236
* Ok, everything prepared, so now let's do the tricks.
2238
growfs(fsi, fso, Nflag);
2241
* Update the disk label.
2244
pp->p_fsize = sblock.fs_fsize;
2245
pp->p_frag = sblock.fs_frag;
2246
pp->p_cpg = sblock.fs_fpg;
2248
return_disklabel(fso, lp, Nflag);
2249
DBG_PRINT0("label rewritten\n");
2253
if(fso>-1) close(fso);
2261
/* ************************************************** return_disklabel ***** */
2263
* Write the updated disklabel back to disk.
2266
return_disklabel(int fd, struct disklabel *lp, unsigned int Nflag)
2268
DBG_FUNC("return_disklabel")
2284
* recalculate checksum
2286
while(ptr < (u_short *)&lp->d_partitions[lp->d_npartitions]) {
2291
if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
2292
errx(1, "DIOCWDINFO failed");
2301
/* ***************************************************** get_disklabel ***** */
2303
* Read the disklabel from disk.
2305
static struct disklabel *
2306
get_disklabel(int fd)
2308
DBG_FUNC("get_disklabel")
2309
static struct disklabel *lab;
2313
lab=(struct disklabel *)malloc(sizeof(struct disklabel));
2315
errx(1, "malloc failed");
2317
if (!ioctl(fd, DIOCGDINFO, (char *)lab))
2327
/* ************************************************************* usage ***** */
2329
* Dump a line of usage.
2338
fprintf(stderr, "usage: growfs [-Ny] [-s size] special\n");
2344
/* *********************************************************** updclst ***** */
2346
* This updates most parameters and the bitmap related to cluster. We have to
2347
* assume that sblock, osblock, acg are set up.
2357
if(sblock.fs_contigsumsize < 1) { /* no clustering */
2361
* update cluster allocation map
2363
setbit(cg_clustersfree(&acg), block);
2366
* update cluster summary table
2370
* calculate size for the trailing cluster
2372
for(block--; lcs<sblock.fs_contigsumsize; block--, lcs++ ) {
2373
if(isclr(cg_clustersfree(&acg), block)){
2378
if(lcs < sblock.fs_contigsumsize) {
2380
cg_clustersum(&acg)[lcs]--;
2383
cg_clustersum(&acg)[lcs]++;
2390
/* *********************************************************** updrefs ***** */
2392
* This updates all references to relocated blocks for the given inode. The
2393
* inode is given as number within the cylinder group, and the number of the
2397
updrefs(int cg, ino_t in, struct gfs_bpp *bp, int fsi, int fso, unsigned int
2401
ufs_lbn_t len, lbn, numblks;
2402
ufs2_daddr_t iptr, blksperindir;
2404
int i, mode, inodeupdated;
2408
ino = ginode(in, fsi, cg);
2413
mode = DIP(ino, di_mode) & IFMT;
2414
if (mode != IFDIR && mode != IFREG && mode != IFLNK) {
2416
return; /* only check DIR, FILE, LINK */
2418
if (mode == IFLNK &&
2419
DIP(ino, di_size) < (u_int64_t) sblock.fs_maxsymlinklen) {
2421
return; /* skip short symlinks */
2423
numblks = howmany(DIP(ino, di_size), sblock.fs_bsize);
2426
return; /* skip empty file */
2428
if (DIP(ino, di_blocks) == 0) {
2430
return; /* skip empty swiss cheesy file or old fastlink */
2432
DBG_PRINT2("scg checking inode (%d in %d)\n",
2437
* Check all the blocks.
2440
len = numblks < NDADDR ? numblks : NDADDR;
2441
for (i = 0; i < len; i++) {
2442
iptr = DIP(ino, di_db[i]);
2445
if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2446
DIP_SET(ino, di_db[i], iptr);
2450
DBG_PRINT0("~~scg direct blocks checked\n");
2453
len = numblks - NDADDR;
2455
for (i = 0; len > 0 && i < NIADDR; i++) {
2456
iptr = DIP(ino, di_ib[i]);
2459
if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2460
DIP_SET(ino, di_ib[i], iptr);
2463
indirchk(blksperindir, lbn, iptr, numblks, bp, fsi, fso, Nflag);
2464
blksperindir *= NINDIR(&sblock);
2465
lbn += blksperindir;
2466
len -= blksperindir;
2467
DBG_PRINT1("scg indirect_%d blocks checked\n", i + 1);
2470
wtfs(inoblk, sblock.fs_bsize, inobuf, fso, Nflag);
2477
* Recursively check all the indirect blocks.
2480
indirchk(ufs_lbn_t blksperindir, ufs_lbn_t lbn, ufs2_daddr_t blkno,
2481
ufs_lbn_t lastlbn, struct gfs_bpp *bp, int fsi, int fso, unsigned int Nflag)
2483
DBG_FUNC("indirchk")
2490
/* read in the indirect block. */
2491
ibuf = malloc(sblock.fs_bsize);
2493
errx(1, "malloc failed");
2494
rdfs(fsbtodb(&sblock, blkno), (size_t)sblock.fs_bsize, ibuf, fsi);
2495
last = howmany(lastlbn - lbn, blksperindir) < NINDIR(&sblock) ?
2496
howmany(lastlbn - lbn, blksperindir) : NINDIR(&sblock);
2497
for (i = 0; i < last; i++) {
2498
if (sblock.fs_magic == FS_UFS1_MAGIC)
2499
iptr = ((ufs1_daddr_t *)ibuf)[i];
2501
iptr = ((ufs2_daddr_t *)ibuf)[i];
2504
if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2505
if (sblock.fs_magic == FS_UFS1_MAGIC)
2506
((ufs1_daddr_t *)ibuf)[i] = iptr;
2508
((ufs2_daddr_t *)ibuf)[i] = iptr;
2510
if (blksperindir == 1)
2512
indirchk(blksperindir / NINDIR(&sblock), lbn + blksperindir * i,
2513
iptr, lastlbn, bp, fsi, fso, Nflag);