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