]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/growfs/growfs.c
Merge 4.2.4p8 into contrib (r200452 & r200454).
[FreeBSD/FreeBSD.git] / sbin / growfs / growfs.c
1 /*
2  * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
3  * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
38  * $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $
39  *
40  */
41
42 #ifndef lint
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";
47 #endif /* not lint */
48
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51
52 /* ********************************************************** INCLUDES ***** */
53 #include <sys/param.h>
54 #include <sys/disklabel.h>
55 #include <sys/ioctl.h>
56 #include <sys/stat.h>
57 #include <sys/disk.h>
58
59 #include <stdio.h>
60 #include <paths.h>
61 #include <ctype.h>
62 #include <err.h>
63 #include <fcntl.h>
64 #include <limits.h>
65 #include <stdlib.h>
66 #include <stdint.h>
67 #include <string.h>
68 #include <time.h>
69 #include <unistd.h>
70 #include <ufs/ufs/dinode.h>
71 #include <ufs/ffs/fs.h>
72
73 #include "debug.h"
74
75 /* *************************************************** GLOBALS & TYPES ***** */
76 #ifdef FS_DEBUG
77 int     _dbg_lvl_ = (DL_INFO);  /* DL_TRC */
78 #endif /* FS_DEBUG */
79
80 static union {
81         struct fs       fs;
82         char    pad[SBLOCKSIZE];
83 } fsun1, fsun2;
84 #define sblock  fsun1.fs        /* the new superblock */
85 #define osblock fsun2.fs        /* the old superblock */
86
87 /*
88  * Possible superblock locations ordered from most to least likely.
89  */
90 static int sblock_try[] = SBLOCKSEARCH;
91 static ufs2_daddr_t sblockloc;
92
93 static union {
94         struct cg       cg;
95         char    pad[MAXBSIZE];
96 } cgun1, cgun2;
97 #define acg     cgun1.cg        /* a cylinder cgroup (new) */
98 #define aocg    cgun2.cg        /* an old cylinder group */
99
100 static char     ablk[MAXBSIZE]; /* a block */
101
102 static struct csum      *fscs;  /* cylinder summary */
103
104 union dinode {
105         struct ufs1_dinode dp1;
106         struct ufs2_dinode dp2;
107 };
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); \
114         else \
115                 (dp)->dp2.field = (val); \
116         } while (0)
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. */
121
122 /*
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.
126  */
127 struct gfs_bpp {
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 */
134 };
135
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,
154                     unsigned 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 *);
160
161 /* ************************************************************ growfs ***** */
162 /*
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
171  * copies.
172  */
173 static void
174 growfs(int fsi, int fso, unsigned int Nflag)
175 {
176         DBG_FUNC("growfs")
177         int     i;
178         int     cylno, j;
179         time_t  utime;
180         int     width;
181         char    tmpbuf[100];
182 #ifdef FSIRAND
183         static int      randinit=0;
184
185         DBG_ENTER;
186
187         if (!randinit) {
188                 randinit = 1;
189                 srandomdev();
190         }
191 #else /* not FSIRAND */
192
193         DBG_ENTER;
194
195 #endif /* FSIRAND */
196         time(&utime);
197
198         /*
199          * Get the cylinder summary into the memory.
200          */
201         fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize);
202         if(fscs == NULL) {
203                 errx(1, "calloc failed");
204         }
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);
209         }
210
211 #ifdef FS_DEBUG
212 {
213         struct csum     *dbg_csp;
214         int     dbg_csc;
215         char    dbg_line[80];
216
217         dbg_csp=fscs;
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,
222                     dbg_line,
223                     dbg_csp++);
224         }
225 }
226 #endif /* FS_DEBUG */
227         DBG_PRINT0("fscs read\n");
228
229         /*
230          * Do all needed changes in the former last cylinder group.
231          */
232         updjcg(osblock.fs_ncg-1, utime, fsi, fso, Nflag);
233
234         /*
235          * Dump out summary information about file system.
236          */
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,
241             sblock.fs_fsize);
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");
247 #       undef B2MBFACTOR
248
249         /*
250          * Now build the cylinders group blocks and
251          * then print out indices of cylinder groups.
252          */
253         printf("super-block backups (for fsck -b #) at:\n");
254         i = 0;
255         width = charsperline();
256
257         /*
258          * Iterate for only the new cylinder groups.
259          */
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) {
266                         printf("\n");
267                         i = 0;
268                 }
269                 i += j;
270                 printf("%s", tmpbuf);
271                 fflush(stdout);
272         }
273         printf("\n");
274
275         /*
276          * Do all needed changes in the first cylinder group.
277          * allocate blocks in new location
278          */
279         updcsloc(utime, fsi, fso, Nflag);
280
281         /*
282          * Now write the cylinder summary back to disk.
283          */
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);
288         }
289         DBG_PRINT0("fscs written\n");
290
291 #ifdef FS_DEBUG
292 {
293         struct csum     *dbg_csp;
294         int     dbg_csc;
295         char    dbg_line[80];
296
297         dbg_csp=fscs;
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,
302                     dbg_line,
303                     dbg_csp++);
304         }
305 }
306 #endif /* FS_DEBUG */
307
308         /*
309          * Now write the new superblock back to disk.
310          */
311         sblock.fs_time = utime;
312         wtfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
313         DBG_PRINT0("sblock written\n");
314         DBG_DUMP_FS(&sblock,
315             "new initial sblock");
316
317         /*
318          * Clean up the dynamic fields in our superblock copies.
319          */
320         sblock.fs_fmod = 0;
321         sblock.fs_clean = 1;
322         sblock.fs_ronly = 0;
323         sblock.fs_cgrotor = 0;
324         sblock.fs_state = 0;
325         memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
326         sblock.fs_flags &= FS_DOSOFTDEP;
327
328         /*
329          * XXX
330          * The following fields are currently distributed from the superblock
331          * to the copies:
332          *     fs_minfree
333          *     fs_rotdelay
334          *     fs_maxcontig
335          *     fs_maxbpg
336          *     fs_minfree,
337          *     fs_optim
338          *     fs_flags regarding SOFTPDATES
339          *
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.
347          */
348
349         /*
350          * Write out the duplicate super blocks.
351          */
352         for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
353                 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
354                     (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
355         }
356         DBG_PRINT0("sblock copies written\n");
357         DBG_DUMP_FS(&sblock,
358             "new other sblocks");
359
360         DBG_LEAVE;
361         return;
362 }
363
364 /* ************************************************************ initcg ***** */
365 /*
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.
370  */
371 static void
372 initcg(int cylno, time_t utime, int fso, unsigned int Nflag)
373 {
374         DBG_FUNC("initcg")
375         static void *iobuf;
376         long d, dlower, dupper, blkno, start;
377         ufs2_daddr_t i, cbase, dmax;
378         struct ufs1_dinode *dp1;
379         struct ufs2_dinode *dp2;
380         struct csum *cs;
381
382         if (iobuf == NULL && (iobuf = malloc(sblock.fs_bsize)) == NULL) {
383                 errx(37, "panic: cannot allocate I/O buffer");
384         }
385         /*
386          * Determine block bounds for cylinder group.
387          * Allow space for super block summary information in first
388          * cylinder group.
389          */
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);
398         cs = &fscs[cylno];
399         memset(&acg, 0, sblock.fs_cgsize);
400         /*
401          * Note that we do not set cg_initediblk at all.
402          * In this extension of a previous filesystem
403          * we have no inodes initialized for the cylinder
404          * group at all. The first access to that cylinder
405          * group will do the correct initialization.
406          */
407         acg.cg_time = utime;
408         acg.cg_magic = CG_MAGIC;
409         acg.cg_cgx = cylno;
410         acg.cg_niblk = sblock.fs_ipg;
411         acg.cg_ndblk = dmax - cbase;
412         if (sblock.fs_contigsumsize > 0)
413                 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
414         start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
415         if (sblock.fs_magic == FS_UFS2_MAGIC) {
416                 acg.cg_iusedoff = start;
417         } else {
418                 acg.cg_old_ncyl = sblock.fs_old_cpg;
419                 acg.cg_old_time = acg.cg_time;
420                 acg.cg_time = 0;
421                 acg.cg_old_niblk = acg.cg_niblk;
422                 acg.cg_niblk = 0;
423                 acg.cg_old_btotoff = start;
424                 acg.cg_old_boff = acg.cg_old_btotoff +
425                     sblock.fs_old_cpg * sizeof(int32_t);
426                 acg.cg_iusedoff = acg.cg_old_boff +
427                     sblock.fs_old_cpg * sizeof(u_int16_t);
428         }
429         acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
430         acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT);
431         if (sblock.fs_contigsumsize > 0) {
432                 acg.cg_clustersumoff =
433                     roundup(acg.cg_nextfreeoff, sizeof(u_int32_t));
434                 acg.cg_clustersumoff -= sizeof(u_int32_t);
435                 acg.cg_clusteroff = acg.cg_clustersumoff +
436                     (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
437                 acg.cg_nextfreeoff = acg.cg_clusteroff +
438                     howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
439         }
440         if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
441                 /*
442                  * This should never happen as we would have had that panic
443                  * already on file system creation
444                  */
445                 errx(37, "panic: cylinder group too big");
446         }
447         acg.cg_cs.cs_nifree += sblock.fs_ipg;
448         if (cylno == 0)
449                 for (i = 0; i < ROOTINO; i++) {
450                         setbit(cg_inosused(&acg), i);
451                         acg.cg_cs.cs_nifree--;
452                 }
453         /*
454          * XXX Newfs writes out two blocks of initialized inodes
455          *     unconditionally.  Should we check here to make sure that they
456          *     were actually written?
457          */
458         if (sblock.fs_magic == FS_UFS1_MAGIC) {
459                 bzero(iobuf, sblock.fs_bsize);
460                 for (i = 2 * sblock.fs_frag; i < sblock.fs_ipg / INOPF(&sblock);
461                      i += sblock.fs_frag) {
462                         dp1 = (struct ufs1_dinode *)iobuf;
463                         dp2 = (struct ufs2_dinode *)iobuf;
464 #ifdef FSIRAND
465                         for (j = 0; j < INOPB(&sblock); j++)
466                                 if (sblock.fs_magic == FS_UFS1_MAGIC) {
467                                         dp1->di_gen = random();
468                                         dp1++;
469                                 } else {
470                                         dp2->di_gen = random();
471                                         dp2++;
472                                 }
473 #endif
474                         wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
475                             sblock.fs_bsize, iobuf, fso, Nflag);
476                 }
477         }
478         if (cylno > 0) {
479                 /*
480                  * In cylno 0, beginning space is reserved
481                  * for boot and super blocks.
482                  */
483                 for (d = 0; d < dlower; d += sblock.fs_frag) {
484                         blkno = d / sblock.fs_frag;
485                         setblock(&sblock, cg_blksfree(&acg), blkno);
486                         if (sblock.fs_contigsumsize > 0)
487                                 setbit(cg_clustersfree(&acg), blkno);
488                         acg.cg_cs.cs_nbfree++;
489                 }
490                 sblock.fs_dsize += dlower;
491         }
492         sblock.fs_dsize += acg.cg_ndblk - dupper;
493         if ((i = dupper % sblock.fs_frag)) {
494                 acg.cg_frsum[sblock.fs_frag - i]++;
495                 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
496                         setbit(cg_blksfree(&acg), dupper);
497                         acg.cg_cs.cs_nffree++;
498                 }
499         }
500         for (d = dupper; d + sblock.fs_frag <= acg.cg_ndblk;
501              d += sblock.fs_frag) {
502                 blkno = d / sblock.fs_frag;
503                 setblock(&sblock, cg_blksfree(&acg), blkno);
504                 if (sblock.fs_contigsumsize > 0)
505                         setbit(cg_clustersfree(&acg), blkno);
506                 acg.cg_cs.cs_nbfree++;
507         }
508         if (d < acg.cg_ndblk) {
509                 acg.cg_frsum[acg.cg_ndblk - d]++;
510                 for (; d < acg.cg_ndblk; d++) {
511                         setbit(cg_blksfree(&acg), d);
512                         acg.cg_cs.cs_nffree++;
513                 }
514         }
515         if (sblock.fs_contigsumsize > 0) {
516                 int32_t *sump = cg_clustersum(&acg);
517                 u_char *mapp = cg_clustersfree(&acg);
518                 int map = *mapp++;
519                 int bit = 1;
520                 int run = 0;
521
522                 for (i = 0; i < acg.cg_nclusterblks; i++) {
523                         if ((map & bit) != 0)
524                                 run++;
525                         else if (run != 0) {
526                                 if (run > sblock.fs_contigsumsize)
527                                         run = sblock.fs_contigsumsize;
528                                 sump[run]++;
529                                 run = 0;
530                         }
531                         if ((i & (CHAR_BIT - 1)) != CHAR_BIT - 1)
532                                 bit <<= 1;
533                         else {
534                                 map = *mapp++;
535                                 bit = 1;
536                         }
537                 }
538                 if (run != 0) {
539                         if (run > sblock.fs_contigsumsize)
540                                 run = sblock.fs_contigsumsize;
541                         sump[run]++;
542                 }
543         }
544         sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
545         sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
546         sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
547         sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
548         *cs = acg.cg_cs;
549         wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
550                 sblock.fs_bsize, (char *)&acg, fso, Nflag);
551         DBG_DUMP_CG(&sblock,
552             "new cg",
553             &acg);
554
555         DBG_LEAVE;
556         return;
557 }
558
559 /* ******************************************************* frag_adjust ***** */
560 /*
561  * Here we add or subtract (sign +1/-1) the available fragments in a given
562  * block to or from the fragment statistics. By subtracting before and adding
563  * after an operation on the free frag map we can easy update the fragment
564  * statistic, which seems to be otherwise a rather complex operation.
565  */
566 static void
567 frag_adjust(ufs2_daddr_t frag, int sign)
568 {
569         DBG_FUNC("frag_adjust")
570         int fragsize;
571         int f;
572
573         DBG_ENTER;
574
575         fragsize=0;
576         /*
577          * Here frag only needs to point to any fragment in the block we want
578          * to examine.
579          */
580         for(f=rounddown(frag, sblock.fs_frag);
581             f<roundup(frag+1, sblock.fs_frag);
582             f++) {
583                 /*
584                  * Count contiguous free fragments.
585                  */
586                 if(isset(cg_blksfree(&acg), f)) {
587                         fragsize++;
588                 } else {
589                         if(fragsize && fragsize<sblock.fs_frag) {
590                                 /*
591                                  * We found something in between.
592                                  */
593                                 acg.cg_frsum[fragsize]+=sign;
594                                 DBG_PRINT2("frag_adjust [%d]+=%d\n",
595                                     fragsize,
596                                     sign);
597                         }
598                         fragsize=0;
599                 }
600         }
601         if(fragsize && fragsize<sblock.fs_frag) {
602                 /*
603                  * We found something.
604                  */
605                 acg.cg_frsum[fragsize]+=sign;
606                 DBG_PRINT2("frag_adjust [%d]+=%d\n",
607                     fragsize,
608                     sign);
609         }
610         DBG_PRINT2("frag_adjust [[%d]]+=%d\n",
611             fragsize,
612             sign);
613
614         DBG_LEAVE;
615         return;
616 }
617
618 /* ******************************************************* cond_bl_upd ***** */
619 /*
620  * Here we conditionally update a pointer to a fragment. We check for all
621  * relocated blocks if any of its fragments is referenced by the current
622  * field, and update the pointer to the respective fragment in our new
623  * block.  If we find a reference we write back the block immediately,
624  * as there is no easy way for our general block reading engine to figure
625  * out if a write back operation is needed.
626  */
627 static int
628 cond_bl_upd(ufs2_daddr_t *block, struct gfs_bpp *field, int fsi, int fso,
629     unsigned int Nflag)
630 {
631         DBG_FUNC("cond_bl_upd")
632         struct gfs_bpp *f;
633         ufs2_daddr_t src, dst;
634         int fragnum;
635         void *ibuf;
636
637         DBG_ENTER;
638
639         for (f = field; f->old != 0; f++) {
640                 src = *block;
641                 if (fragstoblks(&sblock, src) != f->old)
642                         continue;
643                 /*
644                  * The fragment is part of the block, so update.
645                  */
646                 dst = blkstofrags(&sblock, f->new);
647                 fragnum = fragnum(&sblock, src);
648                 *block = dst + fragnum;
649                 f->found++;
650                 DBG_PRINT3("scg (%jd->%jd)[%d] reference updated\n",
651                     (intmax_t)f->old,
652                     (intmax_t)f->new,
653                     fragnum);
654
655                 /*
656                  * Copy the block back immediately.
657                  *
658                  * XXX  If src is is from an indirect block we have
659                  *      to implement copy on write here in case of
660                  *      active snapshots.
661                  */
662                 ibuf = malloc(sblock.fs_bsize);
663                 if (!ibuf)
664                         errx(1, "malloc failed");
665                 src -= fragnum;
666                 rdfs(fsbtodb(&sblock, src), (size_t)sblock.fs_bsize, ibuf, fsi);
667                 wtfs(dst, (size_t)sblock.fs_bsize, ibuf, fso, Nflag);
668                 free(ibuf);
669                 /*
670                  * The same block can't be found again in this loop.
671                  */
672                 return (1);
673         }
674
675         DBG_LEAVE;
676         return (0);
677 }
678
679 /* ************************************************************ updjcg ***** */
680 /*
681  * Here we do all needed work for the former last cylinder group. It has to be
682  * changed in any case, even if the file system ended exactly on the end of
683  * this group, as there is some slightly inconsistent handling of the number
684  * of cylinders in the cylinder group. We start again by reading the cylinder
685  * group from disk. If the last block was not fully available, we first handle
686  * the missing fragments, then we handle all new full blocks in that file
687  * system and finally we handle the new last fragmented block in the file
688  * system.  We again have to handle the fragment statistics rotational layout
689  * tables and cluster summary during all those operations.
690  */
691 static void
692 updjcg(int cylno, time_t utime, int fsi, int fso, unsigned int Nflag)
693 {
694         DBG_FUNC("updjcg")
695         ufs2_daddr_t    cbase, dmax, dupper;
696         struct csum     *cs;
697         int     i,k;
698         int     j=0;
699
700         DBG_ENTER;
701
702         /*
703          * Read the former last (joining) cylinder group from disk, and make
704          * a copy.
705          */
706         rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
707             (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
708         DBG_PRINT0("jcg read\n");
709         DBG_DUMP_CG(&sblock,
710             "old joining cg",
711             &aocg);
712
713         memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
714
715         /*
716          * If the cylinder group had already its new final size almost
717          * nothing is to be done ... except:
718          * For some reason the value of cg_ncyl in the last cylinder group has
719          * to be zero instead of fs_cpg. As this is now no longer the last
720          * cylinder group we have to change that value now to fs_cpg.
721          */
722
723         if(cgbase(&osblock, cylno+1) == osblock.fs_size) {
724                 if (sblock.fs_magic == FS_UFS1_MAGIC)
725                         acg.cg_old_ncyl=sblock.fs_old_cpg;
726
727                 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
728                     (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
729                 DBG_PRINT0("jcg written\n");
730                 DBG_DUMP_CG(&sblock,
731                     "new joining cg",
732                     &acg);
733
734                 DBG_LEAVE;
735                 return;
736         }
737
738         /*
739          * Set up some variables needed later.
740          */
741         cbase = cgbase(&sblock, cylno);
742         dmax = cbase + sblock.fs_fpg;
743         if (dmax > sblock.fs_size)
744                 dmax = sblock.fs_size;
745         dupper = cgdmin(&sblock, cylno) - cbase;
746         if (cylno == 0) { /* XXX fscs may be relocated */
747                 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
748         }
749
750         /*
751          * Set pointer to the cylinder summary for our cylinder group.
752          */
753         cs = fscs + cylno;
754
755         /*
756          * Touch the cylinder group, update all fields in the cylinder group as
757          * needed, update the free space in the superblock.
758          */
759         acg.cg_time = utime;
760         if (cylno == sblock.fs_ncg - 1) {
761                 /*
762                  * This is still the last cylinder group.
763                  */
764                 if (sblock.fs_magic == FS_UFS1_MAGIC)
765                         acg.cg_old_ncyl =
766                             sblock.fs_old_ncyl % sblock.fs_old_cpg;
767         } else {
768                 acg.cg_old_ncyl = sblock.fs_old_cpg;
769         }
770         DBG_PRINT2("jcg dbg: %d %u",
771             cylno,
772             sblock.fs_ncg);
773 #ifdef FS_DEBUG
774         if (sblock.fs_magic == FS_UFS1_MAGIC)
775                 DBG_PRINT2("%d %u",
776                     acg.cg_old_ncyl,
777                     sblock.fs_old_cpg);
778 #endif
779         DBG_PRINT0("\n");
780         acg.cg_ndblk = dmax - cbase;
781         sblock.fs_dsize += acg.cg_ndblk-aocg.cg_ndblk;
782         if (sblock.fs_contigsumsize > 0) {
783                 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
784         }
785
786         /*
787          * Now we have to update the free fragment bitmap for our new free
788          * space.  There again we have to handle the fragmentation and also
789          * the rotational layout tables and the cluster summary.  This is
790          * also done per fragment for the first new block if the old file
791          * system end was not on a block boundary, per fragment for the new
792          * last block if the new file system end is not on a block boundary,
793          * and per block for all space in between.
794          *
795          * Handle the first new block here if it was partially available
796          * before.
797          */
798         if(osblock.fs_size % sblock.fs_frag) {
799                 if(roundup(osblock.fs_size, sblock.fs_frag)<=sblock.fs_size) {
800                         /*
801                          * The new space is enough to fill at least this
802                          * block
803                          */
804                         j=0;
805                         for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag)-1;
806                             i>=osblock.fs_size-cbase;
807                             i--) {
808                                 setbit(cg_blksfree(&acg), i);
809                                 acg.cg_cs.cs_nffree++;
810                                 j++;
811                         }
812
813                         /*
814                          * Check if the fragment just created could join an
815                          * already existing fragment at the former end of the
816                          * file system.
817                          */
818                         if(isblock(&sblock, cg_blksfree(&acg),
819                             ((osblock.fs_size - cgbase(&sblock, cylno))/
820                             sblock.fs_frag))) {
821                                 /*
822                                  * The block is now completely available.
823                                  */
824                                 DBG_PRINT0("block was\n");
825                                 acg.cg_frsum[osblock.fs_size%sblock.fs_frag]--;
826                                 acg.cg_cs.cs_nbfree++;
827                                 acg.cg_cs.cs_nffree-=sblock.fs_frag;
828                                 k=rounddown(osblock.fs_size-cbase,
829                                     sblock.fs_frag);
830                                 updclst((osblock.fs_size-cbase)/sblock.fs_frag);
831                         } else {
832                                 /*
833                                  * Lets rejoin a possible partially growed
834                                  * fragment.
835                                  */
836                                 k=0;
837                                 while(isset(cg_blksfree(&acg), i) &&
838                                     (i>=rounddown(osblock.fs_size-cbase,
839                                     sblock.fs_frag))) {
840                                         i--;
841                                         k++;
842                                 }
843                                 if(k) {
844                                         acg.cg_frsum[k]--;
845                                 }
846                                 acg.cg_frsum[k+j]++;
847                         }
848                 } else {
849                         /*
850                          * We only grow by some fragments within this last
851                          * block.
852                          */
853                         for(i=sblock.fs_size-cbase-1;
854                                 i>=osblock.fs_size-cbase;
855                                 i--) {
856                                 setbit(cg_blksfree(&acg), i);
857                                 acg.cg_cs.cs_nffree++;
858                                 j++;
859                         }
860                         /*
861                          * Lets rejoin a possible partially growed fragment.
862                          */
863                         k=0;
864                         while(isset(cg_blksfree(&acg), i) &&
865                             (i>=rounddown(osblock.fs_size-cbase,
866                             sblock.fs_frag))) {
867                                 i--;
868                                 k++;
869                         }
870                         if(k) {
871                                 acg.cg_frsum[k]--;
872                         }
873                         acg.cg_frsum[k+j]++;
874                 }
875         }
876
877         /*
878          * Handle all new complete blocks here.
879          */
880         for(i=roundup(osblock.fs_size-cbase, sblock.fs_frag);
881             i+sblock.fs_frag<=dmax-cbase;       /* XXX <= or only < ? */
882             i+=sblock.fs_frag) {
883                 j = i / sblock.fs_frag;
884                 setblock(&sblock, cg_blksfree(&acg), j);
885                 updclst(j);
886                 acg.cg_cs.cs_nbfree++;
887         }
888
889         /*
890          * Handle the last new block if there are stll some new fragments left.
891          * Here we don't have to bother about the cluster summary or the even
892          * the rotational layout table.
893          */
894         if (i < (dmax - cbase)) {
895                 acg.cg_frsum[dmax - cbase - i]++;
896                 for (; i < dmax - cbase; i++) {
897                         setbit(cg_blksfree(&acg), i);
898                         acg.cg_cs.cs_nffree++;
899                 }
900         }
901
902         sblock.fs_cstotal.cs_nffree +=
903             (acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree);
904         sblock.fs_cstotal.cs_nbfree +=
905             (acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree);
906         /*
907          * The following statistics are not changed here:
908          *     sblock.fs_cstotal.cs_ndir
909          *     sblock.fs_cstotal.cs_nifree
910          * As the statistics for this cylinder group are ready, copy it to
911          * the summary information array.
912          */
913         *cs = acg.cg_cs;
914
915         /*
916          * Write the updated "joining" cylinder group back to disk.
917          */
918         wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
919             (void *)&acg, fso, Nflag);
920         DBG_PRINT0("jcg written\n");
921         DBG_DUMP_CG(&sblock,
922             "new joining cg",
923             &acg);
924
925         DBG_LEAVE;
926         return;
927 }
928
929 /* ********************************************************** updcsloc ***** */
930 /*
931  * Here we update the location of the cylinder summary. We have two possible
932  * ways of growing the cylinder summary.
933  * (1)  We can try to grow the summary in the current location, and relocate
934  *      possibly used blocks within the current cylinder group.
935  * (2)  Alternatively we can relocate the whole cylinder summary to the first
936  *      new completely empty cylinder group. Once the cylinder summary is no
937  *      longer in the beginning of the first cylinder group you should never
938  *      use a version of fsck which is not aware of the possibility to have
939  *      this structure in a non standard place.
940  * Option (1) is considered to be less intrusive to the structure of the file-
941  * system. So we try to stick to that whenever possible. If there is not enough
942  * space in the cylinder group containing the cylinder summary we have to use
943  * method (2). In case of active snapshots in the file system we probably can
944  * completely avoid implementing copy on write if we stick to method (2) only.
945  */
946 static void
947 updcsloc(time_t utime, int fsi, int fso, unsigned int Nflag)
948 {
949         DBG_FUNC("updcsloc")
950         struct csum     *cs;
951         int     ocscg, ncscg;
952         int     blocks;
953         ufs2_daddr_t    cbase, dupper, odupper, d, f, g;
954         int     ind;
955         int     cylno, inc;
956         struct gfs_bpp  *bp;
957         int     i, l;
958         int     lcs=0;
959         int     block;
960
961         DBG_ENTER;
962
963         if(howmany(sblock.fs_cssize, sblock.fs_fsize) ==
964             howmany(osblock.fs_cssize, osblock.fs_fsize)) {
965                 /*
966                  * No new fragment needed.
967                  */
968                 DBG_LEAVE;
969                 return;
970         }
971         ocscg=dtog(&osblock, osblock.fs_csaddr);
972         cs=fscs+ocscg;
973         blocks = 1+howmany(sblock.fs_cssize, sblock.fs_bsize)-
974             howmany(osblock.fs_cssize, osblock.fs_bsize);
975
976         /*
977          * Read original cylinder group from disk, and make a copy.
978          * XXX  If Nflag is set in some very rare cases we now miss
979          *      some changes done in updjcg by reading the unmodified
980          *      block from disk.
981          */
982         rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
983             (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
984         DBG_PRINT0("oscg read\n");
985         DBG_DUMP_CG(&sblock,
986             "old summary cg",
987             &aocg);
988
989         memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
990
991         /*
992          * Touch the cylinder group, set up local variables needed later
993          * and update the superblock.
994          */
995         acg.cg_time = utime;
996
997         /*
998          * XXX  In the case of having active snapshots we may need much more
999          *      blocks for the copy on write. We need each block twice, and
1000          *      also up to 8*3 blocks for indirect blocks for all possible
1001          *      references.
1002          */
1003         if(/*((int)sblock.fs_time&0x3)>0||*/ cs->cs_nbfree < blocks) {
1004                 /*
1005                  * There is not enough space in the old cylinder group to
1006                  * relocate all blocks as needed, so we relocate the whole
1007                  * cylinder group summary to a new group. We try to use the
1008                  * first complete new cylinder group just created. Within the
1009                  * cylinder group we align the area immediately after the
1010                  * cylinder group information location in order to be as
1011                  * close as possible to the original implementation of ffs.
1012                  *
1013                  * First we have to make sure we'll find enough space in the
1014                  * new cylinder group. If not, then we currently give up.
1015                  * We start with freeing everything which was used by the
1016                  * fragments of the old cylinder summary in the current group.
1017                  * Now we write back the group meta data, read in the needed
1018                  * meta data from the new cylinder group, and start allocating
1019                  * within that group. Here we can assume, the group to be
1020                  * completely empty. Which makes the handling of fragments and
1021                  * clusters a lot easier.
1022                  */
1023                 DBG_TRC;
1024                 if(sblock.fs_ncg-osblock.fs_ncg < 2) {
1025                         errx(2, "panic: not enough space");
1026                 }
1027
1028                 /*
1029                  * Point "d" to the first fragment not used by the cylinder
1030                  * summary.
1031                  */
1032                 d=osblock.fs_csaddr+(osblock.fs_cssize/osblock.fs_fsize);
1033
1034                 /*
1035                  * Set up last cluster size ("lcs") already here. Calculate
1036                  * the size for the trailing cluster just behind where "d"
1037                  * points to.
1038                  */
1039                 if(sblock.fs_contigsumsize > 0) {
1040                         for(block=howmany(d%sblock.fs_fpg, sblock.fs_frag),
1041                             lcs=0; lcs<sblock.fs_contigsumsize;
1042                             block++, lcs++) {
1043                                 if(isclr(cg_clustersfree(&acg), block)){
1044                                         break;
1045                                 }
1046                         }
1047                 }
1048
1049                 /*
1050                  * Point "d" to the last frag used by the cylinder summary.
1051                  */
1052                 d--;
1053
1054                 DBG_PRINT1("d=%jd\n",
1055                     (intmax_t)d);
1056                 if((d+1)%sblock.fs_frag) {
1057                         /*
1058                          * The end of the cylinder summary is not a complete
1059                          * block.
1060                          */
1061                         DBG_TRC;
1062                         frag_adjust(d%sblock.fs_fpg, -1);
1063                         for(; (d+1)%sblock.fs_frag; d--) {
1064                                 DBG_PRINT1("d=%jd\n",
1065                                     (intmax_t)d);
1066                                 setbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1067                                 acg.cg_cs.cs_nffree++;
1068                                 sblock.fs_cstotal.cs_nffree++;
1069                         }
1070                         /*
1071                          * Point "d" to the last fragment of the last
1072                          * (incomplete) block of the cylinder summary.
1073                          */
1074                         d++;
1075                         frag_adjust(d%sblock.fs_fpg, 1);
1076
1077                         if(isblock(&sblock, cg_blksfree(&acg),
1078                             (d%sblock.fs_fpg)/sblock.fs_frag)) {
1079                                 DBG_PRINT1("d=%jd\n", (intmax_t)d);
1080                                 acg.cg_cs.cs_nffree-=sblock.fs_frag;
1081                                 acg.cg_cs.cs_nbfree++;
1082                                 sblock.fs_cstotal.cs_nffree-=sblock.fs_frag;
1083                                 sblock.fs_cstotal.cs_nbfree++;
1084                                 if(sblock.fs_contigsumsize > 0) {
1085                                         setbit(cg_clustersfree(&acg),
1086                                             (d%sblock.fs_fpg)/sblock.fs_frag);
1087                                         if(lcs < sblock.fs_contigsumsize) {
1088                                                 if(lcs) {
1089                                                         cg_clustersum(&acg)
1090                                                             [lcs]--;
1091                                                 }
1092                                                 lcs++;
1093                                                 cg_clustersum(&acg)[lcs]++;
1094                                         }
1095                                 }
1096                         }
1097                         /*
1098                          * Point "d" to the first fragment of the block before
1099                          * the last incomplete block.
1100                          */
1101                         d--;
1102                 }
1103
1104                 DBG_PRINT1("d=%jd\n", (intmax_t)d);
1105                 for(d=rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
1106                     d-=sblock.fs_frag) {
1107                         DBG_TRC;
1108                         DBG_PRINT1("d=%jd\n", (intmax_t)d);
1109                         setblock(&sblock, cg_blksfree(&acg),
1110                             (d%sblock.fs_fpg)/sblock.fs_frag);
1111                         acg.cg_cs.cs_nbfree++;
1112                         sblock.fs_cstotal.cs_nbfree++;
1113                         if(sblock.fs_contigsumsize > 0) {
1114                                 setbit(cg_clustersfree(&acg),
1115                                     (d%sblock.fs_fpg)/sblock.fs_frag);
1116                                 /*
1117                                  * The last cluster size is already set up.
1118                                  */
1119                                 if(lcs < sblock.fs_contigsumsize) {
1120                                         if(lcs) {
1121                                                 cg_clustersum(&acg)[lcs]--;
1122                                         }
1123                                         lcs++;
1124                                         cg_clustersum(&acg)[lcs]++;
1125                                 }
1126                         }
1127                 }
1128                 *cs = acg.cg_cs;
1129
1130                 /*
1131                  * Now write the former cylinder group containing the cylinder
1132                  * summary back to disk.
1133                  */
1134                 wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
1135                     (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1136                 DBG_PRINT0("oscg written\n");
1137                 DBG_DUMP_CG(&sblock,
1138                     "old summary cg",
1139                     &acg);
1140
1141                 /*
1142                  * Find the beginning of the new cylinder group containing the
1143                  * cylinder summary.
1144                  */
1145                 sblock.fs_csaddr=cgdmin(&sblock, osblock.fs_ncg);
1146                 ncscg=dtog(&sblock, sblock.fs_csaddr);
1147                 cs=fscs+ncscg;
1148
1149
1150                 /*
1151                  * If Nflag is specified, we would now read random data instead
1152                  * of an empty cg structure from disk. So we can't simulate that
1153                  * part for now.
1154                  */
1155                 if(Nflag) {
1156                         DBG_PRINT0("nscg update skipped\n");
1157                         DBG_LEAVE;
1158                         return;
1159                 }
1160
1161                 /*
1162                  * Read the future cylinder group containing the cylinder
1163                  * summary from disk, and make a copy.
1164                  */
1165                 rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1166                     (size_t)sblock.fs_cgsize, (void *)&aocg, fsi);
1167                 DBG_PRINT0("nscg read\n");
1168                 DBG_DUMP_CG(&sblock,
1169                     "new summary cg",
1170                     &aocg);
1171
1172                 memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
1173
1174                 /*
1175                  * Allocate all complete blocks used by the new cylinder
1176                  * summary.
1177                  */
1178                 for(d=sblock.fs_csaddr; d+sblock.fs_frag <=
1179                     sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize);
1180                     d+=sblock.fs_frag) {
1181                         clrblock(&sblock, cg_blksfree(&acg),
1182                             (d%sblock.fs_fpg)/sblock.fs_frag);
1183                         acg.cg_cs.cs_nbfree--;
1184                         sblock.fs_cstotal.cs_nbfree--;
1185                         if(sblock.fs_contigsumsize > 0) {
1186                                 clrbit(cg_clustersfree(&acg),
1187                                     (d%sblock.fs_fpg)/sblock.fs_frag);
1188                         }
1189                 }
1190
1191                 /*
1192                  * Allocate all fragments used by the cylinder summary in the
1193                  * last block.
1194                  */
1195                 if(d<sblock.fs_csaddr+(sblock.fs_cssize/sblock.fs_fsize)) {
1196                         for(; d-sblock.fs_csaddr<
1197                             sblock.fs_cssize/sblock.fs_fsize;
1198                             d++) {
1199                                 clrbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1200                                 acg.cg_cs.cs_nffree--;
1201                                 sblock.fs_cstotal.cs_nffree--;
1202                         }
1203                         acg.cg_cs.cs_nbfree--;
1204                         acg.cg_cs.cs_nffree+=sblock.fs_frag;
1205                         sblock.fs_cstotal.cs_nbfree--;
1206                         sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1207                         if(sblock.fs_contigsumsize > 0) {
1208                                 clrbit(cg_clustersfree(&acg),
1209                                     (d%sblock.fs_fpg)/sblock.fs_frag);
1210                         }
1211
1212                         frag_adjust(d%sblock.fs_fpg, +1);
1213                 }
1214                 /*
1215                  * XXX  Handle the cluster statistics here in the case this
1216                  *      cylinder group is now almost full, and the remaining
1217                  *      space is less then the maximum cluster size. This is
1218                  *      probably not needed, as you would hardly find a file
1219                  *      system which has only MAXCSBUFS+FS_MAXCONTIG of free
1220                  *      space right behind the cylinder group information in
1221                  *      any new cylinder group.
1222                  */
1223
1224                 /*
1225                  * Update our statistics in the cylinder summary.
1226                  */
1227                 *cs = acg.cg_cs;
1228
1229                 /*
1230                  * Write the new cylinder group containing the cylinder summary
1231                  * back to disk.
1232                  */
1233                 wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1234                     (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1235                 DBG_PRINT0("nscg written\n");
1236                 DBG_DUMP_CG(&sblock,
1237                     "new summary cg",
1238                     &acg);
1239
1240                 DBG_LEAVE;
1241                 return;
1242         }
1243         /*
1244          * We have got enough of space in the current cylinder group, so we
1245          * can relocate just a few blocks, and let the summary information
1246          * grow in place where it is right now.
1247          */
1248         DBG_TRC;
1249
1250         cbase = cgbase(&osblock, ocscg);        /* old and new are equal */
1251         dupper = sblock.fs_csaddr - cbase +
1252             howmany(sblock.fs_cssize, sblock.fs_fsize);
1253         odupper = osblock.fs_csaddr - cbase +
1254             howmany(osblock.fs_cssize, osblock.fs_fsize);
1255
1256         sblock.fs_dsize -= dupper-odupper;
1257
1258         /*
1259          * Allocate the space for the array of blocks to be relocated.
1260          */
1261         bp=(struct gfs_bpp *)malloc(((dupper-odupper)/sblock.fs_frag+2)*
1262             sizeof(struct gfs_bpp));
1263         if(bp == NULL) {
1264                 errx(1, "malloc failed");
1265         }
1266         memset((char *)bp, 0, ((dupper-odupper)/sblock.fs_frag+2)*
1267             sizeof(struct gfs_bpp));
1268
1269         /*
1270          * Lock all new frags needed for the cylinder group summary. This is
1271          * done per fragment in the first and last block of the new required
1272          * area, and per block for all other blocks.
1273          *
1274          * Handle the first new block here (but only if some fragments where
1275          * already used for the cylinder summary).
1276          */
1277         ind=0;
1278         frag_adjust(odupper, -1);
1279         for(d=odupper; ((d<dupper)&&(d%sblock.fs_frag)); d++) {
1280                 DBG_PRINT1("scg first frag check loop d=%jd\n",
1281                     (intmax_t)d);
1282                 if(isclr(cg_blksfree(&acg), d)) {
1283                         if (!ind) {
1284                                 bp[ind].old=d/sblock.fs_frag;
1285                                 bp[ind].flags|=GFS_FL_FIRST;
1286                                 if(roundup(d, sblock.fs_frag) >= dupper) {
1287                                         bp[ind].flags|=GFS_FL_LAST;
1288                                 }
1289                                 ind++;
1290                         }
1291                 } else {
1292                         clrbit(cg_blksfree(&acg), d);
1293                         acg.cg_cs.cs_nffree--;
1294                         sblock.fs_cstotal.cs_nffree--;
1295                 }
1296                 /*
1297                  * No cluster handling is needed here, as there was at least
1298                  * one fragment in use by the cylinder summary in the old
1299                  * file system.
1300                  * No block-free counter handling here as this block was not
1301                  * a free block.
1302                  */
1303         }
1304         frag_adjust(odupper, 1);
1305
1306         /*
1307          * Handle all needed complete blocks here.
1308          */
1309         for(; d+sblock.fs_frag<=dupper; d+=sblock.fs_frag) {
1310                 DBG_PRINT1("scg block check loop d=%jd\n",
1311                     (intmax_t)d);
1312                 if(!isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) {
1313                         for(f=d; f<d+sblock.fs_frag; f++) {
1314                                 if(isset(cg_blksfree(&aocg), f)) {
1315                                         acg.cg_cs.cs_nffree--;
1316                                         sblock.fs_cstotal.cs_nffree--;
1317                                 }
1318                         }
1319                         clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
1320                         bp[ind].old=d/sblock.fs_frag;
1321                         ind++;
1322                 } else {
1323                         clrblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
1324                         acg.cg_cs.cs_nbfree--;
1325                         sblock.fs_cstotal.cs_nbfree--;
1326                         if(sblock.fs_contigsumsize > 0) {
1327                                 clrbit(cg_clustersfree(&acg), d/sblock.fs_frag);
1328                                 for(lcs=0, l=(d/sblock.fs_frag)+1;
1329                                     lcs<sblock.fs_contigsumsize;
1330                                     l++, lcs++ ) {
1331                                         if(isclr(cg_clustersfree(&acg),l)){
1332                                                 break;
1333                                         }
1334                                 }
1335                                 if(lcs < sblock.fs_contigsumsize) {
1336                                         cg_clustersum(&acg)[lcs+1]--;
1337                                         if(lcs) {
1338                                                 cg_clustersum(&acg)[lcs]++;
1339                                         }
1340                                 }
1341                         }
1342                 }
1343                 /*
1344                  * No fragment counter handling is needed here, as this finally
1345                  * doesn't change after the relocation.
1346                  */
1347         }
1348
1349         /*
1350          * Handle all fragments needed in the last new affected block.
1351          */
1352         if(d<dupper) {
1353                 frag_adjust(dupper-1, -1);
1354
1355                 if(isblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag)) {
1356                         acg.cg_cs.cs_nbfree--;
1357                         sblock.fs_cstotal.cs_nbfree--;
1358                         acg.cg_cs.cs_nffree+=sblock.fs_frag;
1359                         sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1360                         if(sblock.fs_contigsumsize > 0) {
1361                                 clrbit(cg_clustersfree(&acg), d/sblock.fs_frag);
1362                                 for(lcs=0, l=(d/sblock.fs_frag)+1;
1363                                     lcs<sblock.fs_contigsumsize;
1364                                     l++, lcs++ ) {
1365                                         if(isclr(cg_clustersfree(&acg),l)){
1366                                                 break;
1367                                         }
1368                                 }
1369                                 if(lcs < sblock.fs_contigsumsize) {
1370                                         cg_clustersum(&acg)[lcs+1]--;
1371                                         if(lcs) {
1372                                                 cg_clustersum(&acg)[lcs]++;
1373                                         }
1374                                 }
1375                         }
1376                 }
1377
1378                 for(; d<dupper; d++) {
1379                         DBG_PRINT1("scg second frag check loop d=%jd\n",
1380                             (intmax_t)d);
1381                         if(isclr(cg_blksfree(&acg), d)) {
1382                                 bp[ind].old=d/sblock.fs_frag;
1383                                 bp[ind].flags|=GFS_FL_LAST;
1384                         } else {
1385                                 clrbit(cg_blksfree(&acg), d);
1386                                 acg.cg_cs.cs_nffree--;
1387                                 sblock.fs_cstotal.cs_nffree--;
1388                         }
1389                 }
1390                 if(bp[ind].flags & GFS_FL_LAST) { /* we have to advance here */
1391                         ind++;
1392                 }
1393                 frag_adjust(dupper-1, 1);
1394         }
1395
1396         /*
1397          * If we found a block to relocate just do so.
1398          */
1399         if(ind) {
1400                 for(i=0; i<ind; i++) {
1401                         if(!bp[i].old) { /* no more blocks listed */
1402                                 /*
1403                                  * XXX  A relative blocknumber should not be
1404                                  *      zero, which is not explicitly
1405                                  *      guaranteed by our code.
1406                                  */
1407                                 break;
1408                         }
1409                         /*
1410                          * Allocate a complete block in the same (current)
1411                          * cylinder group.
1412                          */
1413                         bp[i].new=alloc()/sblock.fs_frag;
1414
1415                         /*
1416                          * There is no frag_adjust() needed for the new block
1417                          * as it will have no fragments yet :-).
1418                          */
1419                         for(f=bp[i].old*sblock.fs_frag,
1420                             g=bp[i].new*sblock.fs_frag;
1421                             f<(bp[i].old+1)*sblock.fs_frag;
1422                             f++, g++) {
1423                                 if(isset(cg_blksfree(&aocg), f)) {
1424                                         setbit(cg_blksfree(&acg), g);
1425                                         acg.cg_cs.cs_nffree++;
1426                                         sblock.fs_cstotal.cs_nffree++;
1427                                 }
1428                         }
1429
1430                         /*
1431                          * Special handling is required if this was the first
1432                          * block. We have to consider the fragments which were
1433                          * used by the cylinder summary in the original block
1434                          * which re to be free in the copy of our block.  We
1435                          * have to be careful if this first block happens to
1436                          * be also the last block to be relocated.
1437                          */
1438                         if(bp[i].flags & GFS_FL_FIRST) {
1439                                 for(f=bp[i].old*sblock.fs_frag,
1440                                     g=bp[i].new*sblock.fs_frag;
1441                                     f<odupper;
1442                                     f++, g++) {
1443                                         setbit(cg_blksfree(&acg), g);
1444                                         acg.cg_cs.cs_nffree++;
1445                                         sblock.fs_cstotal.cs_nffree++;
1446                                 }
1447                                 if(!(bp[i].flags & GFS_FL_LAST)) {
1448                                         frag_adjust(bp[i].new*sblock.fs_frag,1);
1449                                 }
1450                         }
1451
1452                         /*
1453                          * Special handling is required if this is the last
1454                          * block to be relocated.
1455                          */
1456                         if(bp[i].flags & GFS_FL_LAST) {
1457                                 frag_adjust(bp[i].new*sblock.fs_frag, 1);
1458                                 frag_adjust(bp[i].old*sblock.fs_frag, -1);
1459                                 for(f=dupper;
1460                                     f<roundup(dupper, sblock.fs_frag);
1461                                     f++) {
1462                                         if(isclr(cg_blksfree(&acg), f)) {
1463                                                 setbit(cg_blksfree(&acg), f);
1464                                                 acg.cg_cs.cs_nffree++;
1465                                                 sblock.fs_cstotal.cs_nffree++;
1466                                         }
1467                                 }
1468                                 frag_adjust(bp[i].old*sblock.fs_frag, 1);
1469                         }
1470
1471                         /*
1472                          * !!! Attach the cylindergroup offset here.
1473                          */
1474                         bp[i].old+=cbase/sblock.fs_frag;
1475                         bp[i].new+=cbase/sblock.fs_frag;
1476
1477                         /*
1478                          * Copy the content of the block.
1479                          */
1480                         /*
1481                          * XXX  Here we will have to implement a copy on write
1482                          *      in the case we have any active snapshots.
1483                          */
1484                         rdfs(fsbtodb(&sblock, bp[i].old*sblock.fs_frag),
1485                             (size_t)sblock.fs_bsize, (void *)&ablk, fsi);
1486                         wtfs(fsbtodb(&sblock, bp[i].new*sblock.fs_frag),
1487                             (size_t)sblock.fs_bsize, (void *)&ablk, fso, Nflag);
1488                         DBG_DUMP_HEX(&sblock,
1489                             "copied full block",
1490                             (unsigned char *)&ablk);
1491
1492                         DBG_PRINT2("scg (%jd->%jd) block relocated\n",
1493                             (intmax_t)bp[i].old,
1494                             (intmax_t)bp[i].new);
1495                 }
1496
1497                 /*
1498                  * Now we have to update all references to any fragment which
1499                  * belongs to any block relocated. We iterate now over all
1500                  * cylinder groups, within those over all non zero length
1501                  * inodes.
1502                  */
1503                 for(cylno=0; cylno<osblock.fs_ncg; cylno++) {
1504                         DBG_PRINT1("scg doing cg (%d)\n",
1505                             cylno);
1506                         for(inc=osblock.fs_ipg-1 ; inc>0 ; inc--) {
1507                                 updrefs(cylno, (ino_t)inc, bp, fsi, fso, Nflag);
1508                         }
1509                 }
1510
1511                 /*
1512                  * All inodes are checked, now make sure the number of
1513                  * references found make sense.
1514                  */
1515                 for(i=0; i<ind; i++) {
1516                         if(!bp[i].found || (bp[i].found>sblock.fs_frag)) {
1517                                 warnx("error: %jd refs found for block %jd.",
1518                                     (intmax_t)bp[i].found, (intmax_t)bp[i].old);
1519                         }
1520
1521                 }
1522         }
1523         /*
1524          * The following statistics are not changed here:
1525          *     sblock.fs_cstotal.cs_ndir
1526          *     sblock.fs_cstotal.cs_nifree
1527          * The following statistics were already updated on the fly:
1528          *     sblock.fs_cstotal.cs_nffree
1529          *     sblock.fs_cstotal.cs_nbfree
1530          * As the statistics for this cylinder group are ready, copy it to
1531          * the summary information array.
1532          */
1533
1534         *cs = acg.cg_cs;
1535
1536         /*
1537          * Write summary cylinder group back to disk.
1538          */
1539         wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), (size_t)sblock.fs_cgsize,
1540             (void *)&acg, fso, Nflag);
1541         DBG_PRINT0("scg written\n");
1542         DBG_DUMP_CG(&sblock,
1543             "new summary cg",
1544             &acg);
1545
1546         DBG_LEAVE;
1547         return;
1548 }
1549
1550 /* ************************************************************** rdfs ***** */
1551 /*
1552  * Here we read some block(s) from disk.
1553  */
1554 static void
1555 rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi)
1556 {
1557         DBG_FUNC("rdfs")
1558         ssize_t n;
1559
1560         DBG_ENTER;
1561
1562         if (bno < 0) {
1563                 err(32, "rdfs: attempting to read negative block number");
1564         }
1565         if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
1566                 err(33, "rdfs: seek error: %jd", (intmax_t)bno);
1567         }
1568         n = read(fsi, bf, size);
1569         if (n != (ssize_t)size) {
1570                 err(34, "rdfs: read error: %jd", (intmax_t)bno);
1571         }
1572
1573         DBG_LEAVE;
1574         return;
1575 }
1576
1577 /* ************************************************************** wtfs ***** */
1578 /*
1579  * Here we write some block(s) to disk.
1580  */
1581 static void
1582 wtfs(ufs2_daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1583 {
1584         DBG_FUNC("wtfs")
1585         ssize_t n;
1586
1587         DBG_ENTER;
1588
1589         if (Nflag) {
1590                 DBG_LEAVE;
1591                 return;
1592         }
1593         if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) {
1594                 err(35, "wtfs: seek error: %ld", (long)bno);
1595         }
1596         n = write(fso, bf, size);
1597         if (n != (ssize_t)size) {
1598                 err(36, "wtfs: write error: %ld", (long)bno);
1599         }
1600
1601         DBG_LEAVE;
1602         return;
1603 }
1604
1605 /* ************************************************************* alloc ***** */
1606 /*
1607  * Here we allocate a free block in the current cylinder group. It is assumed,
1608  * that acg contains the current cylinder group. As we may take a block from
1609  * somewhere in the file system we have to handle cluster summary here.
1610  */
1611 static ufs2_daddr_t
1612 alloc(void)
1613 {
1614         DBG_FUNC("alloc")
1615         ufs2_daddr_t    d, blkno;
1616         int     lcs1, lcs2;
1617         int     l;
1618         int     csmin, csmax;
1619         int     dlower, dupper, dmax;
1620
1621         DBG_ENTER;
1622
1623         if (acg.cg_magic != CG_MAGIC) {
1624                 warnx("acg: bad magic number");
1625                 DBG_LEAVE;
1626                 return (0);
1627         }
1628         if (acg.cg_cs.cs_nbfree == 0) {
1629                 warnx("error: cylinder group ran out of space");
1630                 DBG_LEAVE;
1631                 return (0);
1632         }
1633         /*
1634          * We start seeking for free blocks only from the space available after
1635          * the end of the new grown cylinder summary. Otherwise we allocate a
1636          * block here which we have to relocate a couple of seconds later again
1637          * again, and we are not prepared to to this anyway.
1638          */
1639         blkno=-1;
1640         dlower=cgsblock(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx);
1641         dupper=cgdmin(&sblock, acg.cg_cgx)-cgbase(&sblock, acg.cg_cgx);
1642         dmax=cgbase(&sblock, acg.cg_cgx)+sblock.fs_fpg;
1643         if (dmax > sblock.fs_size) {
1644                 dmax = sblock.fs_size;
1645         }
1646         dmax-=cgbase(&sblock, acg.cg_cgx); /* retransform into cg */
1647         csmin=sblock.fs_csaddr-cgbase(&sblock, acg.cg_cgx);
1648         csmax=csmin+howmany(sblock.fs_cssize, sblock.fs_fsize);
1649         DBG_PRINT3("seek range: dl=%d, du=%d, dm=%d\n",
1650             dlower,
1651             dupper,
1652             dmax);
1653         DBG_PRINT2("range cont: csmin=%d, csmax=%d\n",
1654             csmin,
1655             csmax);
1656
1657         for(d=0; (d<dlower && blkno==-1); d+=sblock.fs_frag) {
1658                 if(d>=csmin && d<=csmax) {
1659                         continue;
1660                 }
1661                 if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1662                     d))) {
1663                         blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1664                         break;
1665                 }
1666         }
1667         for(d=dupper; (d<dmax && blkno==-1); d+=sblock.fs_frag) {
1668                 if(d>=csmin && d<=csmax) {
1669                         continue;
1670                 }
1671                 if(isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1672                     d))) {
1673                         blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1674                         break;
1675                 }
1676         }
1677         if(blkno==-1) {
1678                 warnx("internal error: couldn't find promised block in cg");
1679                 DBG_LEAVE;
1680                 return (0);
1681         }
1682
1683         /*
1684          * This is needed if the block was found already in the first loop.
1685          */
1686         d=blkstofrags(&sblock, blkno);
1687
1688         clrblock(&sblock, cg_blksfree(&acg), blkno);
1689         if (sblock.fs_contigsumsize > 0) {
1690                 /*
1691                  * Handle the cluster allocation bitmap.
1692                  */
1693                 clrbit(cg_clustersfree(&acg), blkno);
1694                 /*
1695                  * We possibly have split a cluster here, so we have to do
1696                  * recalculate the sizes of the remaining cluster halves now,
1697                  * and use them for updating the cluster summary information.
1698                  *
1699                  * Lets start with the blocks before our allocated block ...
1700                  */
1701                 for(lcs1=0, l=blkno-1; lcs1<sblock.fs_contigsumsize;
1702                     l--, lcs1++ ) {
1703                         if(isclr(cg_clustersfree(&acg),l)){
1704                                 break;
1705                         }
1706                 }
1707                 /*
1708                  * ... and continue with the blocks right after our allocated
1709                  * block.
1710                  */
1711                 for(lcs2=0, l=blkno+1; lcs2<sblock.fs_contigsumsize;
1712                     l++, lcs2++ ) {
1713                         if(isclr(cg_clustersfree(&acg),l)){
1714                                 break;
1715                         }
1716                 }
1717
1718                 /*
1719                  * Now update all counters.
1720                  */
1721                 cg_clustersum(&acg)[MIN(lcs1+lcs2+1,sblock.fs_contigsumsize)]--;
1722                 if(lcs1) {
1723                         cg_clustersum(&acg)[lcs1]++;
1724                 }
1725                 if(lcs2) {
1726                         cg_clustersum(&acg)[lcs2]++;
1727                 }
1728         }
1729         /*
1730          * Update all statistics based on blocks.
1731          */
1732         acg.cg_cs.cs_nbfree--;
1733         sblock.fs_cstotal.cs_nbfree--;
1734
1735         DBG_LEAVE;
1736         return (d);
1737 }
1738
1739 /* *********************************************************** isblock ***** */
1740 /*
1741  * Here we check if all frags of a block are free. For more details again
1742  * please see the source of newfs(8), as this function is taken over almost
1743  * unchanged.
1744  */
1745 static int
1746 isblock(struct fs *fs, unsigned char *cp, int h)
1747 {
1748         DBG_FUNC("isblock")
1749         unsigned char   mask;
1750
1751         DBG_ENTER;
1752
1753         switch (fs->fs_frag) {
1754         case 8:
1755                 DBG_LEAVE;
1756                 return (cp[h] == 0xff);
1757         case 4:
1758                 mask = 0x0f << ((h & 0x1) << 2);
1759                 DBG_LEAVE;
1760                 return ((cp[h >> 1] & mask) == mask);
1761         case 2:
1762                 mask = 0x03 << ((h & 0x3) << 1);
1763                 DBG_LEAVE;
1764                 return ((cp[h >> 2] & mask) == mask);
1765         case 1:
1766                 mask = 0x01 << (h & 0x7);
1767                 DBG_LEAVE;
1768                 return ((cp[h >> 3] & mask) == mask);
1769         default:
1770                 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1771                 DBG_LEAVE;
1772                 return (0);
1773         }
1774 }
1775
1776 /* ********************************************************** clrblock ***** */
1777 /*
1778  * Here we allocate a complete block in the block map. For more details again
1779  * please see the source of newfs(8), as this function is taken over almost
1780  * unchanged.
1781  */
1782 static void
1783 clrblock(struct fs *fs, unsigned char *cp, int h)
1784 {
1785         DBG_FUNC("clrblock")
1786
1787         DBG_ENTER;
1788
1789         switch ((fs)->fs_frag) {
1790         case 8:
1791                 cp[h] = 0;
1792                 break;
1793         case 4:
1794                 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1795                 break;
1796         case 2:
1797                 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1798                 break;
1799         case 1:
1800                 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1801                 break;
1802         default:
1803                 warnx("clrblock bad fs_frag %d", fs->fs_frag);
1804                 break;
1805         }
1806
1807         DBG_LEAVE;
1808         return;
1809 }
1810
1811 /* ********************************************************** setblock ***** */
1812 /*
1813  * Here we free a complete block in the free block map. For more details again
1814  * please see the source of newfs(8), as this function is taken over almost
1815  * unchanged.
1816  */
1817 static void
1818 setblock(struct fs *fs, unsigned char *cp, int h)
1819 {
1820         DBG_FUNC("setblock")
1821
1822         DBG_ENTER;
1823
1824         switch (fs->fs_frag) {
1825         case 8:
1826                 cp[h] = 0xff;
1827                 break;
1828         case 4:
1829                 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1830                 break;
1831         case 2:
1832                 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1833                 break;
1834         case 1:
1835                 cp[h >> 3] |= (0x01 << (h & 0x7));
1836                 break;
1837         default:
1838                 warnx("setblock bad fs_frag %d", fs->fs_frag);
1839                 break;
1840         }
1841
1842         DBG_LEAVE;
1843         return;
1844 }
1845
1846 /* ************************************************************ ginode ***** */
1847 /*
1848  * This function provides access to an individual inode. We find out in which
1849  * block the requested inode is located, read it from disk if needed, and
1850  * return the pointer into that block. We maintain a cache of one block to
1851  * not read the same block again and again if we iterate linearly over all
1852  * inodes.
1853  */
1854 static union dinode *
1855 ginode(ino_t inumber, int fsi, int cg)
1856 {
1857         DBG_FUNC("ginode")
1858         static ino_t    startinum = 0;  /* first inode in cached block */
1859
1860         DBG_ENTER;
1861
1862         /*
1863          * The inumber passed in is relative to the cg, so use it here to see
1864          * if the inode has been allocated yet.
1865          */
1866         if (isclr(cg_inosused(&aocg), inumber)) {
1867                 DBG_LEAVE;
1868                 return NULL;
1869         }
1870         /*
1871          * Now make the inumber relative to the entire inode space so it can
1872          * be sanity checked.
1873          */
1874         inumber += (cg * sblock.fs_ipg);
1875         if (inumber < ROOTINO) {
1876                 DBG_LEAVE;
1877                 return NULL;
1878         }
1879         if (inumber > maxino)
1880                 errx(8, "bad inode number %d to ginode", inumber);
1881         if (startinum == 0 ||
1882             inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
1883                 inoblk = fsbtodb(&sblock, ino_to_fsba(&sblock, inumber));
1884                 rdfs(inoblk, (size_t)sblock.fs_bsize, inobuf, fsi);
1885                 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
1886         }
1887         DBG_LEAVE;
1888         if (sblock.fs_magic == FS_UFS1_MAGIC)
1889                 return (union dinode *)((uintptr_t)inobuf +
1890                     (inumber % INOPB(&sblock)) * sizeof(struct ufs1_dinode));
1891         return (union dinode *)((uintptr_t)inobuf +
1892             (inumber % INOPB(&sblock)) * sizeof(struct ufs2_dinode));
1893 }
1894
1895 /* ****************************************************** charsperline ***** */
1896 /*
1897  * Figure out how many lines our current terminal has. For more details again
1898  * please see the source of newfs(8), as this function is taken over almost
1899  * unchanged.
1900  */
1901 static int
1902 charsperline(void)
1903 {
1904         DBG_FUNC("charsperline")
1905         int     columns;
1906         char    *cp;
1907         struct winsize  ws;
1908
1909         DBG_ENTER;
1910
1911         columns = 0;
1912         if (ioctl(0, TIOCGWINSZ, &ws) != -1) {
1913                 columns = ws.ws_col;
1914         }
1915         if (columns == 0 && (cp = getenv("COLUMNS"))) {
1916                 columns = atoi(cp);
1917         }
1918         if (columns == 0) {
1919                 columns = 80;   /* last resort */
1920         }
1921
1922         DBG_LEAVE;
1923         return columns;
1924 }
1925
1926 /* ****************************************************** get_dev_size ***** */
1927 /*
1928  * Get the size of the partition if we can't figure it out from the disklabel,
1929  * e.g. from vinum volumes.
1930  */
1931 static void
1932 get_dev_size(int fd, int *size)
1933 {
1934    int sectorsize;
1935    off_t mediasize;
1936
1937    if (ioctl(fd, DIOCGSECTORSIZE, &sectorsize) == -1)
1938         err(1,"DIOCGSECTORSIZE");
1939    if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == -1)
1940         err(1,"DIOCGMEDIASIZE");
1941
1942    if (sectorsize <= 0)
1943        errx(1, "bogus sectorsize: %d", sectorsize);
1944
1945    *size = mediasize / sectorsize;
1946 }
1947
1948 /* ************************************************************** main ***** */
1949 /*
1950  * growfs(8)  is a utility which allows to increase the size of an existing
1951  * ufs file system. Currently this can only be done on unmounted file system.
1952  * It recognizes some command line options to specify the new desired size,
1953  * and it does some basic checkings. The old file system size is determined
1954  * and after some more checks like we can really access the new last block
1955  * on the disk etc. we calculate the new parameters for the superblock. After
1956  * having done this we just call growfs() which will do the work.  Before
1957  * we finish the only thing left is to update the disklabel.
1958  * We still have to provide support for snapshots. Therefore we first have to
1959  * understand what data structures are always replicated in the snapshot on
1960  * creation, for all other blocks we touch during our procedure, we have to
1961  * keep the old blocks unchanged somewhere available for the snapshots. If we
1962  * are lucky, then we only have to handle our blocks to be relocated in that
1963  * way.
1964  * Also we have to consider in what order we actually update the critical
1965  * data structures of the file system to make sure, that in case of a disaster
1966  * fsck(8) is still able to restore any lost data.
1967  * The foreseen last step then will be to provide for growing even mounted
1968  * file systems. There we have to extend the mount() system call to provide
1969  * userland access to the file system locking facility.
1970  */
1971 int
1972 main(int argc, char **argv)
1973 {
1974         DBG_FUNC("main")
1975         char    *device, *special, *cp;
1976         int     ch;
1977         unsigned int    size=0;
1978         size_t  len;
1979         unsigned int    Nflag=0;
1980         int     ExpertFlag=0;
1981         struct stat     st;
1982         struct disklabel        *lp;
1983         struct partition        *pp;
1984         int     i,fsi,fso;
1985     u_int32_t p_size;
1986         char    reply[5];
1987 #ifdef FSMAXSNAP
1988         int     j;
1989 #endif /* FSMAXSNAP */
1990
1991         DBG_ENTER;
1992
1993         while((ch=getopt(argc, argv, "Ns:vy")) != -1) {
1994                 switch(ch) {
1995                 case 'N':
1996                         Nflag=1;
1997                         break;
1998                 case 's':
1999                         size=(size_t)atol(optarg);
2000                         if(size<1) {
2001                                 usage();
2002                         }
2003                         break;
2004                 case 'v': /* for compatibility to newfs */
2005                         break;
2006                 case 'y':
2007                         ExpertFlag=1;
2008                         break;
2009                 case '?':
2010                         /* FALLTHROUGH */
2011                 default:
2012                         usage();
2013                 }
2014         }
2015         argc -= optind;
2016         argv += optind;
2017
2018         if(argc != 1) {
2019                 usage();
2020         }
2021         device=*argv;
2022
2023         /*
2024          * Now try to guess the (raw)device name.
2025          */
2026         if (0 == strrchr(device, '/')) {
2027                 /*
2028                  * No path prefix was given, so try in that order:
2029                  *     /dev/r%s
2030                  *     /dev/%s
2031                  *     /dev/vinum/r%s
2032                  *     /dev/vinum/%s.
2033                  *
2034                  * FreeBSD now doesn't distinguish between raw and block
2035                  * devices any longer, but it should still work this way.
2036                  */
2037                 len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/");
2038                 special=(char *)malloc(len);
2039                 if(special == NULL) {
2040                         errx(1, "malloc failed");
2041                 }
2042                 snprintf(special, len, "%sr%s", _PATH_DEV, device);
2043                 if (stat(special, &st) == -1) {
2044                         snprintf(special, len, "%s%s", _PATH_DEV, device);
2045                         if (stat(special, &st) == -1) {
2046                                 snprintf(special, len, "%svinum/r%s",
2047                                     _PATH_DEV, device);
2048                                 if (stat(special, &st) == -1) {
2049                                         /* For now this is the 'last resort' */
2050                                         snprintf(special, len, "%svinum/%s",
2051                                             _PATH_DEV, device);
2052                                 }
2053                         }
2054                 }
2055                 device = special;
2056         }
2057
2058         /*
2059          * Try to access our devices for writing ...
2060          */
2061         if (Nflag) {
2062                 fso = -1;
2063         } else {
2064                 fso = open(device, O_WRONLY);
2065                 if (fso < 0) {
2066                         err(1, "%s", device);
2067                 }
2068         }
2069
2070         /*
2071          * ... and reading.
2072          */
2073         fsi = open(device, O_RDONLY);
2074         if (fsi < 0) {
2075                 err(1, "%s", device);
2076         }
2077
2078         /*
2079          * Try to read a label and guess the slice if not specified. This
2080          * code should guess the right thing and avoid to bother the user
2081          * with the task of specifying the option -v on vinum volumes.
2082          */
2083         cp=device+strlen(device)-1;
2084         lp = get_disklabel(fsi);
2085         pp = NULL;
2086     if (lp != NULL) {
2087         if (isdigit(*cp)) {
2088             pp = &lp->d_partitions[2];
2089         } else if (*cp>='a' && *cp<='h') {
2090             pp = &lp->d_partitions[*cp - 'a'];
2091         } else {
2092             errx(1, "unknown device");
2093         }
2094         p_size = pp->p_size;
2095     } else {
2096         get_dev_size(fsi, &p_size);
2097     }
2098
2099         /*
2100          * Check if that partition is suitable for growing a file system.
2101          */
2102         if (p_size < 1) {
2103                 errx(1, "partition is unavailable");
2104         }
2105
2106         /*
2107          * Read the current superblock, and take a backup.
2108          */
2109         for (i = 0; sblock_try[i] != -1; i++) {
2110                 sblockloc = sblock_try[i] / DEV_BSIZE;
2111                 rdfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&(osblock), fsi);
2112                 if ((osblock.fs_magic == FS_UFS1_MAGIC ||
2113                      (osblock.fs_magic == FS_UFS2_MAGIC &&
2114                       osblock.fs_sblockloc == sblock_try[i])) &&
2115                     osblock.fs_bsize <= MAXBSIZE &&
2116                     osblock.fs_bsize >= (int32_t) sizeof(struct fs))
2117                         break;
2118         }
2119         if (sblock_try[i] == -1) {
2120                 errx(1, "superblock not recognized");
2121         }
2122         memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2));
2123         maxino = sblock.fs_ncg * sblock.fs_ipg;
2124
2125         DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
2126         DBG_DUMP_FS(&sblock,
2127             "old sblock");
2128
2129         /*
2130          * Determine size to grow to. Default to the full size specified in
2131          * the disk label.
2132          */
2133         sblock.fs_size = dbtofsb(&osblock, p_size);
2134         if (size != 0) {
2135                 if (size > p_size){
2136                         errx(1, "there is not enough space (%d < %d)",
2137                             p_size, size);
2138                 }
2139                 sblock.fs_size = dbtofsb(&osblock, size);
2140         }
2141
2142         /*
2143          * Are we really growing ?
2144          */
2145         if(osblock.fs_size >= sblock.fs_size) {
2146                 errx(1, "we are not growing (%jd->%jd)",
2147                     (intmax_t)osblock.fs_size, (intmax_t)sblock.fs_size);
2148         }
2149
2150
2151 #ifdef FSMAXSNAP
2152         /*
2153          * Check if we find an active snapshot.
2154          */
2155         if(ExpertFlag == 0) {
2156                 for(j=0; j<FSMAXSNAP; j++) {
2157                         if(sblock.fs_snapinum[j]) {
2158                                 errx(1, "active snapshot found in file system\n"
2159                                     "   please remove all snapshots before "
2160                                     "using growfs");
2161                         }
2162                         if(!sblock.fs_snapinum[j]) { /* list is dense */
2163                                 break;
2164                         }
2165                 }
2166         }
2167 #endif
2168
2169         if (ExpertFlag == 0 && Nflag == 0) {
2170                 printf("We strongly recommend you to make a backup "
2171                     "before growing the Filesystem\n\n"
2172                     " Did you backup your data (Yes/No) ? ");
2173                 fgets(reply, (int)sizeof(reply), stdin);
2174                 if (strcmp(reply, "Yes\n")){
2175                         printf("\n Nothing done \n");
2176                         exit (0);
2177                 }
2178         }
2179
2180         printf("new file systemsize is: %jd frags\n", (intmax_t)sblock.fs_size);
2181
2182         /*
2183          * Try to access our new last block in the file system. Even if we
2184          * later on realize we have to abort our operation, on that block
2185          * there should be no data, so we can't destroy something yet.
2186          */
2187         wtfs((ufs2_daddr_t)p_size-1, (size_t)DEV_BSIZE, (void *)&sblock,
2188             fso, Nflag);
2189
2190         /*
2191          * Now calculate new superblock values and check for reasonable
2192          * bound for new file system size:
2193          *     fs_size:    is derived from label or user input
2194          *     fs_dsize:   should get updated in the routines creating or
2195          *                 updating the cylinder groups on the fly
2196          *     fs_cstotal: should get updated in the routines creating or
2197          *                 updating the cylinder groups
2198          */
2199
2200         /*
2201          * Update the number of cylinders and cylinder groups in the file system.
2202          */
2203         if (sblock.fs_magic == FS_UFS1_MAGIC) {
2204                 sblock.fs_old_ncyl =
2205                     sblock.fs_size * sblock.fs_old_nspf / sblock.fs_old_spc;
2206                 if (sblock.fs_size * sblock.fs_old_nspf >
2207                     sblock.fs_old_ncyl * sblock.fs_old_spc)
2208                         sblock.fs_old_ncyl++;
2209         }
2210         sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
2211         maxino = sblock.fs_ncg * sblock.fs_ipg;
2212
2213         if (sblock.fs_size % sblock.fs_fpg != 0 &&
2214             sblock.fs_size % sblock.fs_fpg < cgdmin(&sblock, sblock.fs_ncg)) {
2215                 /*
2216                  * The space in the new last cylinder group is too small,
2217                  * so revert back.
2218                  */
2219                 sblock.fs_ncg--;
2220                 if (sblock.fs_magic == FS_UFS1_MAGIC)
2221                         sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg;
2222                 printf("Warning: %jd sector(s) cannot be allocated.\n",
2223                     (intmax_t)fsbtodb(&sblock, sblock.fs_size % sblock.fs_fpg));
2224                 sblock.fs_size = sblock.fs_ncg * sblock.fs_fpg;
2225                 maxino -= sblock.fs_ipg;
2226         }
2227
2228         /*
2229          * Update the space for the cylinder group summary information in the
2230          * respective cylinder group data area.
2231          */
2232         sblock.fs_cssize =
2233             fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
2234
2235         if(osblock.fs_size >= sblock.fs_size) {
2236                 errx(1, "not enough new space");
2237         }
2238
2239         DBG_PRINT0("sblock calculated\n");
2240
2241         /*
2242          * Ok, everything prepared, so now let's do the tricks.
2243          */
2244         growfs(fsi, fso, Nflag);
2245
2246         /*
2247          * Update the disk label.
2248          */
2249     if (!unlabeled) {
2250         pp->p_fsize = sblock.fs_fsize;
2251         pp->p_frag = sblock.fs_frag;
2252         pp->p_cpg = sblock.fs_fpg;
2253
2254         return_disklabel(fso, lp, Nflag);
2255         DBG_PRINT0("label rewritten\n");
2256     }
2257
2258         close(fsi);
2259         if(fso>-1) close(fso);
2260
2261         DBG_CLOSE;
2262
2263         DBG_LEAVE;
2264         return 0;
2265 }
2266
2267 /* ************************************************** return_disklabel ***** */
2268 /*
2269  * Write the updated disklabel back to disk.
2270  */
2271 static void
2272 return_disklabel(int fd, struct disklabel *lp, unsigned int Nflag)
2273 {
2274         DBG_FUNC("return_disklabel")
2275         u_short sum;
2276         u_short *ptr;
2277
2278         DBG_ENTER;
2279
2280         if(!lp) {
2281                 DBG_LEAVE;
2282                 return;
2283         }
2284         if(!Nflag) {
2285                 lp->d_checksum=0;
2286                 sum = 0;
2287                 ptr=(u_short *)lp;
2288
2289                 /*
2290                  * recalculate checksum
2291                  */
2292                 while(ptr < (u_short *)&lp->d_partitions[lp->d_npartitions]) {
2293                         sum ^= *ptr++;
2294                 }
2295                 lp->d_checksum=sum;
2296
2297                 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
2298                         errx(1, "DIOCWDINFO failed");
2299                 }
2300         }
2301         free(lp);
2302
2303         DBG_LEAVE;
2304         return ;
2305 }
2306
2307 /* ***************************************************** get_disklabel ***** */
2308 /*
2309  * Read the disklabel from disk.
2310  */
2311 static struct disklabel *
2312 get_disklabel(int fd)
2313 {
2314         DBG_FUNC("get_disklabel")
2315         static struct   disklabel *lab;
2316
2317         DBG_ENTER;
2318
2319         lab=(struct disklabel *)malloc(sizeof(struct disklabel));
2320         if (!lab)
2321                 errx(1, "malloc failed");
2322
2323     if (!ioctl(fd, DIOCGDINFO, (char *)lab))
2324         return (lab);
2325
2326     unlabeled++;
2327
2328         DBG_LEAVE;
2329         return (NULL);
2330 }
2331
2332
2333 /* ************************************************************* usage ***** */
2334 /*
2335  * Dump a line of usage.
2336  */
2337 static void
2338 usage(void)
2339 {
2340         DBG_FUNC("usage")
2341
2342         DBG_ENTER;
2343
2344         fprintf(stderr, "usage: growfs [-Ny] [-s size] special\n");
2345
2346         DBG_LEAVE;
2347         exit(1);
2348 }
2349
2350 /* *********************************************************** updclst ***** */
2351 /*
2352  * This updates most parameters and the bitmap related to cluster. We have to
2353  * assume that sblock, osblock, acg are set up.
2354  */
2355 static void
2356 updclst(int block)
2357 {
2358         DBG_FUNC("updclst")
2359         static int      lcs=0;
2360
2361         DBG_ENTER;
2362
2363         if(sblock.fs_contigsumsize < 1) { /* no clustering */
2364                 return;
2365         }
2366         /*
2367          * update cluster allocation map
2368          */
2369         setbit(cg_clustersfree(&acg), block);
2370
2371         /*
2372          * update cluster summary table
2373          */
2374         if(!lcs) {
2375                 /*
2376                  * calculate size for the trailing cluster
2377                  */
2378                 for(block--; lcs<sblock.fs_contigsumsize; block--, lcs++ ) {
2379                         if(isclr(cg_clustersfree(&acg), block)){
2380                                 break;
2381                         }
2382                 }
2383         }
2384         if(lcs < sblock.fs_contigsumsize) {
2385                 if(lcs) {
2386                         cg_clustersum(&acg)[lcs]--;
2387                 }
2388                 lcs++;
2389                 cg_clustersum(&acg)[lcs]++;
2390         }
2391
2392         DBG_LEAVE;
2393         return;
2394 }
2395
2396 /* *********************************************************** updrefs ***** */
2397 /*
2398  * This updates all references to relocated blocks for the given inode.  The
2399  * inode is given as number within the cylinder group, and the number of the
2400  * cylinder group.
2401  */
2402 static void
2403 updrefs(int cg, ino_t in, struct gfs_bpp *bp, int fsi, int fso, unsigned int
2404     Nflag)
2405 {
2406         DBG_FUNC("updrefs")
2407         ufs_lbn_t       len, lbn, numblks;
2408         ufs2_daddr_t    iptr, blksperindir;
2409         union dinode    *ino;
2410         int             i, mode, inodeupdated;
2411
2412         DBG_ENTER;
2413
2414         ino = ginode(in, fsi, cg);
2415         if (ino == NULL) {
2416                 DBG_LEAVE;
2417                 return;
2418         }
2419         mode = DIP(ino, di_mode) & IFMT;
2420         if (mode != IFDIR && mode != IFREG && mode != IFLNK) {
2421                 DBG_LEAVE;
2422                 return; /* only check DIR, FILE, LINK */
2423         }
2424         if (mode == IFLNK && 
2425             DIP(ino, di_size) < (u_int64_t) sblock.fs_maxsymlinklen) {
2426                 DBG_LEAVE;
2427                 return; /* skip short symlinks */
2428         }
2429         numblks = howmany(DIP(ino, di_size), sblock.fs_bsize);
2430         if (numblks == 0) {
2431                 DBG_LEAVE;
2432                 return; /* skip empty file */
2433         }
2434         if (DIP(ino, di_blocks) == 0) {
2435                 DBG_LEAVE;
2436                 return; /* skip empty swiss cheesy file or old fastlink */
2437         }
2438         DBG_PRINT2("scg checking inode (%d in %d)\n",
2439             in,
2440             cg);
2441
2442         /*
2443          * Check all the blocks.
2444          */
2445         inodeupdated = 0;
2446         len = numblks < NDADDR ? numblks : NDADDR;
2447         for (i = 0; i < len; i++) {
2448                 iptr = DIP(ino, di_db[i]);
2449                 if (iptr == 0)
2450                         continue;
2451                 if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2452                         DIP_SET(ino, di_db[i], iptr);
2453                         inodeupdated++;
2454                 }
2455         }
2456         DBG_PRINT0("~~scg direct blocks checked\n");
2457
2458         blksperindir = 1;
2459         len = numblks - NDADDR;
2460         lbn = NDADDR;
2461         for (i = 0; len > 0 && i < NIADDR; i++) {
2462                 iptr = DIP(ino, di_ib[i]);
2463                 if (iptr == 0)
2464                         continue;
2465                 if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2466                         DIP_SET(ino, di_ib[i], iptr);
2467                         inodeupdated++;
2468                 }
2469                 indirchk(blksperindir, lbn, iptr, numblks, bp, fsi, fso, Nflag);
2470                 blksperindir *= NINDIR(&sblock);
2471                 lbn += blksperindir;
2472                 len -= blksperindir;
2473                 DBG_PRINT1("scg indirect_%d blocks checked\n", i + 1);
2474         }
2475         if (inodeupdated)
2476                 wtfs(inoblk, sblock.fs_bsize, inobuf, fso, Nflag);
2477
2478         DBG_LEAVE;
2479         return;
2480 }
2481
2482 /*
2483  * Recursively check all the indirect blocks.
2484  */
2485 static void
2486 indirchk(ufs_lbn_t blksperindir, ufs_lbn_t lbn, ufs2_daddr_t blkno,
2487     ufs_lbn_t lastlbn, struct gfs_bpp *bp, int fsi, int fso, unsigned int Nflag)
2488 {
2489         DBG_FUNC("indirchk")
2490         void *ibuf;
2491         int i, last;
2492         ufs2_daddr_t iptr;
2493
2494         DBG_ENTER;
2495
2496         /* read in the indirect block. */
2497         ibuf = malloc(sblock.fs_bsize);
2498         if (!ibuf)
2499                 errx(1, "malloc failed");
2500         rdfs(fsbtodb(&sblock, blkno), (size_t)sblock.fs_bsize, ibuf, fsi);
2501         last = howmany(lastlbn - lbn, blksperindir) < NINDIR(&sblock) ?
2502             howmany(lastlbn - lbn, blksperindir) : NINDIR(&sblock);
2503         for (i = 0; i < last; i++) {
2504                 if (sblock.fs_magic == FS_UFS1_MAGIC)
2505                         iptr = ((ufs1_daddr_t *)ibuf)[i];
2506                 else
2507                         iptr = ((ufs2_daddr_t *)ibuf)[i];
2508                 if (iptr == 0)
2509                         continue;
2510                 if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2511                         if (sblock.fs_magic == FS_UFS1_MAGIC)
2512                                 ((ufs1_daddr_t *)ibuf)[i] = iptr;
2513                         else
2514                                 ((ufs2_daddr_t *)ibuf)[i] = iptr;
2515                 }
2516                 if (blksperindir == 1)
2517                         continue;
2518                 indirchk(blksperindir / NINDIR(&sblock), lbn + blksperindir * i,
2519                     iptr, lastlbn, bp, fsi, fso, Nflag);
2520         }
2521         free(ibuf);
2522
2523         DBG_LEAVE;
2524         return;
2525 }