Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/resize_ffs Clean up this file:



details:   https://anonhg.NetBSD.org/src/rev/9a496130be3e
branches:  trunk
changeset: 759201:9a496130be3e
user:      riz <riz%NetBSD.org@localhost>
date:      Wed Dec 01 17:33:45 2010 +0000

description:
Clean up this file:

- sync usage comment with current reality
- sort includes
- wrap lines
- use EXIT_FAILURE consistently
- make error messages consistent:  Cannot->Can't
- Remove "Old FFSv1 macros" in favor of system macros in ufs/ffs/fs.h .
  Leave dblksize() because it uses the on-disk dinode structure.

More cleanup is needed.
No functional changes intended.

diffstat:

 sbin/resize_ffs/resize_ffs.c |  147 ++++++++++++++++++++++--------------------
 1 files changed, 76 insertions(+), 71 deletions(-)

diffs (truncated from 397 to 300 lines):

diff -r 610fdaa0d727 -r 9a496130be3e sbin/resize_ffs/resize_ffs.c
--- a/sbin/resize_ffs/resize_ffs.c      Wed Dec 01 17:22:51 2010 +0000
+++ b/sbin/resize_ffs/resize_ffs.c      Wed Dec 01 17:33:45 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: resize_ffs.c,v 1.14 2010/11/29 19:54:10 riz Exp $      */
+/*     $NetBSD: resize_ffs.c,v 1.15 2010/12/01 17:33:45 riz Exp $      */
 /* From sources sent on February 17, 2003 */
 /*-
  * As its sole author, I explicitly place this code in the public
@@ -15,9 +15,9 @@
  *
  * Resize a filesystem.  Is capable of both growing and shrinking.
  *
- * Usage: resize_ffs filesystem newsize
+ * Usage: resize_ffs [-s newsize] [-y] filesystem
  *
- * Example: resize_ffs /dev/rsd1e 29574
+ * Example: resize_ffs -s 29574 /dev/rsd1e
  *
  * newsize is in DEV_BSIZE units (ie, disk sectors, usually 512 bytes
  *  each).
@@ -41,13 +41,6 @@
  */
 
 #include <sys/cdefs.h>
-#include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <strings.h>
-#include <err.h>
 #include <sys/disk.h>
 #include <sys/disklabel.h>
 #include <sys/dkio.h>
@@ -60,6 +53,14 @@
 #include <ufs/ufs/dinode.h>
 #include <ufs/ufs/ufs_bswap.h> /* ufs_rw32 */
 
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+
 /* new size of filesystem, in sectors */
 static uint32_t newsize;
 
@@ -81,7 +82,8 @@
 static struct fs *oldsb;       /* before we started */
 static struct fs *newsb;       /* copy to work with */
 /* Buffer to hold the above.  Make sure it's aligned correctly. */
-static char sbbuf[2 * SBLOCKSIZE] __attribute__((__aligned__(__alignof__(struct fs))));
+static char sbbuf[2 * SBLOCKSIZE]
+       __attribute__((__aligned__(__alignof__(struct fs))));
 
 /* a cg's worth of brand new squeaky-clean inodes */
 static struct ufs1_dinode *zinodes;
@@ -115,25 +117,7 @@
                                 * block of inodes, and applies to the whole
                                 * block. */
 
-/* Old FFS1 macros */
-#define cg_blktot(cgp, ns) \
-    (cg_chkmagic(cgp, ns) ? \
-    ((int32_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_old_btotoff, (ns)))) \
-    : (((struct ocg *)(cgp))->cg_btot))
-#define cg_blks(fs, cgp, cylno, ns) \
-    (cg_chkmagic(cgp, ns) ? \
-    ((int16_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_old_boff, (ns))) + \
-       (cylno) * (fs)->fs_old_nrpos) \
-    : (((struct ocg *)(cgp))->cg_b[cylno]))
-#define cbtocylno(fs, bno) \
-   (fsbtodb(fs, bno) / (fs)->fs_old_spc) 
-#define cbtorpos(fs, bno) \
-    ((fs)->fs_old_nrpos <= 1 ? 0 : \
-     (fsbtodb(fs, bno) % (fs)->fs_old_spc / \
-      (fs)->fs_old_nsect * (fs)->fs_old_trackskew + \
-      fsbtodb(fs, bno) % (fs)->fs_old_spc % \
-      (fs)->fs_old_nsect * (fs)->fs_old_interleave) %\
-    (fs)->fs_old_nsect * (fs)->fs_old_nrpos / (fs)->fs_old_npsect)
+/* resize_ffs works directly on dinodes, adapt blksize() */
 #define dblksize(fs, dip, lbn) \
     (((lbn) >= NDADDR || (dip)->di_size >= lblktosize(fs, (lbn) + 1)) \
     ? (fs)->fs_bsize \
@@ -174,7 +158,7 @@
 {
        /* Seek to the correct place. */
        if (lseek(fd, blkno * DEV_BSIZE, L_SET) < 0)
-               err(1, "lseek failed");
+               err(EXIT_FAILURE, "lseek failed");
 
        /* See if we have to break up the transfer... */
        if (smallio) {
@@ -190,7 +174,8 @@
                        if (rv < 0)
                                err(EXIT_FAILURE, "read failed");
                        if (rv != n)
-                               errx(1, "read: wanted %d, got %d", n, rv);
+                               errx(EXIT_FAILURE,
+                                   "read: wanted %d, got %d", n, rv);
                        bp += n;
                        left -= n;
                }
@@ -200,7 +185,7 @@
                if (rv < 0)
                        err(EXIT_FAILURE, "read failed");
                if (rv != size)
-                       errx(1, "read: wanted %d, got %d", size, rv);
+                       errx(EXIT_FAILURE, "read: wanted %d, got %d", size, rv);
        }
 }
 /*
@@ -227,7 +212,8 @@
                        if (rv < 0)
                                err(EXIT_FAILURE, "write failed");
                        if (rv != n)
-                               errx(1, "write: wanted %d, got %d", n, rv);
+                               errx(EXIT_FAILURE,
+                                   "write: wanted %d, got %d", n, rv);
                        bp += n;
                        left -= n;
                }
@@ -237,7 +223,8 @@
                if (rv < 0)
                        err(EXIT_FAILURE, "write failed");
                if (rv != size)
-                       errx(1, "write: wanted %d, got %d", size, rv);
+                       errx(EXIT_FAILURE,
+                           "write: wanted %d, got %d", size, rv);
        }
 }
 /*
@@ -458,7 +445,8 @@
        cg->cg_cgx = cgn;
        cg->cg_old_ncyl = newsb->fs_old_cpg;
        /* Update the cg_old_ncyl value for the last cylinder. */
-       if ((cgn == newsb->fs_ncg - 1) && (newsb->fs_old_ncyl % newsb->fs_old_cpg) ) {
+       if ((cgn == newsb->fs_ncg - 1) && 
+           (newsb->fs_old_ncyl % newsb->fs_old_cpg) ) {
                cg->cg_old_ncyl = newsb->fs_old_ncyl % newsb->fs_old_cpg;
        }
        cg->cg_old_niblk = newsb->fs_ipg;
@@ -515,8 +503,9 @@
         * pre-sb data area always starts at 0, and thus is block-aligned, and
         * always ends at the sb, which is block-aligned.) */
        for (i = 0; i < dlow; i += newsb->fs_frag) {
-               cg_blktot(cg, 0)[cbtocylno(newsb, i)]++;
-               cg_blks(newsb, cg, cbtocylno(newsb, i), 0)[cbtorpos(newsb, i)]++;
+               old_cg_blktot(cg, 0)[old_cbtocylno(newsb, i)]++;
+               old_cg_blks(newsb, cg,
+                   old_cbtocylno(newsb, i), 0)[old_cbtorpos(newsb, i)]++;
        }
        /* Deal with a partial block at the beginning of the post-inode area.
         * I'm not convinced this can happen - I think the inodes are always
@@ -539,9 +528,9 @@
                            newsb->fs_contigsumsize : n]++;
                }
                for (i = n; i > 0; i--) {
-                       cg_blktot(cg, 0)[cbtocylno(newsb, dhigh)]++;
-                       cg_blks(newsb, cg,
-                           cbtocylno(newsb, dhigh), 0)[cbtorpos(newsb,
+                       old_cg_blktot(cg, 0)[old_cbtocylno(newsb, dhigh)]++;
+                       old_cg_blks(newsb, cg,
+                           old_cbtocylno(newsb, dhigh), 0)[old_cbtorpos(newsb,
                                dhigh)]++;
                        dhigh += newsb->fs_frag;
                }
@@ -811,7 +800,7 @@
        newloc = find_freespace(ntot);
        if (newloc < 0) {
                printf("Sorry, no space available for new csums\n");
-               exit(1);
+               exit(EXIT_FAILURE);
        }
        for (i = 0, f = newsb->fs_csaddr, t = newloc; i < ntot; i++, f++, t++) {
                if (i < nold) {
@@ -882,7 +871,8 @@
         * a frag larger than the old size - unlikely, but no excuse to
         * misbehave if it happens.) */
        if (newsb->fs_size == oldsb->fs_size) {
-               printf("New fs size %"PRIu64" = odl fs size %"PRIu64", not growing.\n", newsb->fs_size, oldsb->fs_size);
+               printf("New fs size %"PRIu64" = odl fs size %"PRIu64
+                   ", not growing.\n", newsb->fs_size, oldsb->fs_size);
                return;
        }
        /* Check that the new last sector (frag, actually) is writable.  Since
@@ -902,7 +892,8 @@
        if (cgdmin(newsb, newsb->fs_ncg - 1) > newsb->fs_size) {
                newsb->fs_ncg--;
                newsb->fs_old_ncyl = newsb->fs_ncg * newsb->fs_old_cpg;
-               newsb->fs_size = (newsb->fs_old_ncyl * newsb->fs_old_spc) / NSPF(newsb);
+               newsb->fs_size = (newsb->fs_old_ncyl * newsb->fs_old_spc)
+                   / NSPF(newsb);
                printf("Warning: last cylinder group is too small;\n");
                printf("    dropping it.  New size = %lu.\n",
                    (unsigned long int) fsbtodb(newsb, newsb->fs_size));
@@ -944,7 +935,8 @@
                        newcgsize = newsb->fs_fpg;
                oldcgsize = oldsb->fs_size % oldsb->fs_fpg;
                set_bits(cg_blksfree(cg, 0), oldcgsize, newcgsize - oldcgsize);
-               cg->cg_old_ncyl = howmany(newcgsize * NSPF(newsb), newsb->fs_old_spc);
+               cg->cg_old_ncyl = howmany(newcgsize * NSPF(newsb),
+                   newsb->fs_old_spc);
                cg->cg_ndblk = newcgsize;
        }
        /* Fix up the csum info, if necessary. */
@@ -958,7 +950,8 @@
  *  over either the old or the new filesystem's set of inodes.
  */
 static void
-map_inodes(void (*fn) (struct ufs1_dinode * di, unsigned int, void *arg), int ncg, void *cbarg) {
+map_inodes(void (*fn) (struct ufs1_dinode * di, unsigned int, void *arg),
+          int ncg, void *cbarg) {
        int i;
        int ni;
 
@@ -977,7 +970,8 @@
 #define MDB_INDIR_PRE  2
 #define MDB_INDIR_POST 3
 
-typedef void (*mark_callback_t) (unsigned int blocknum, unsigned int nfrags, unsigned int blksize, int opcode);
+typedef void (*mark_callback_t) (unsigned int blocknum, unsigned int nfrags,
+                                unsigned int blksize, int opcode);
 
 /* Helper function - handles a data block.  Calls the callback
  * function and returns number of bytes occupied in file (actually,
@@ -1123,7 +1117,8 @@
        int cg;
        struct ufs1_dinode *iptr;
 
-       inodes = alloconce(oldsb->fs_ncg * oldsb->fs_ipg * sizeof(struct ufs1_dinode), "inodes");
+       inodes = alloconce(oldsb->fs_ncg * oldsb->fs_ipg *
+           sizeof(struct ufs1_dinode), "inodes");
        iflags = alloconce(oldsb->fs_ncg * oldsb->fs_ipg, "inode flags");
        bzero(iflags, oldsb->fs_ncg * oldsb->fs_ipg);
        iptr = inodes;
@@ -1140,7 +1135,7 @@
 toofull(void)
 {
        printf("Sorry, would run out of data blocks\n");
-       exit(1);
+       exit(EXIT_FAILURE);
 }
 /*
  * Record a desire to move "n" frags from "from" to "to".
@@ -1336,7 +1331,8 @@
 }
 
 static void
-moveindir_callback(unsigned int off, unsigned int nfrag, unsigned int nbytes, int kind)
+moveindir_callback(unsigned int off, unsigned int nfrag, unsigned int nbytes,
+                  int kind)
 {
        if (kind == MDB_INDIR_PRE) {
                int32_t blk[howmany(MAXBSIZE, sizeof(int32_t))];
@@ -1419,7 +1415,7 @@
                        if (fi < 0) {
                                printf("Sorry, inodes evaporated - "
                                    "filesystem probably needs fsck\n");
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        inomove[inum] = fi;
                        clr_bits(cg_inosused(cg, 0), i, 1);
@@ -1532,7 +1528,8 @@
        if (cgdmin(newsb, newsb->fs_ncg - 1) > newsb->fs_size) {
                newsb->fs_ncg--;
                newsb->fs_old_ncyl = newsb->fs_ncg * newsb->fs_old_cpg;
-               newsb->fs_size = (newsb->fs_old_ncyl * newsb->fs_old_spc) / NSPF(newsb);
+               newsb->fs_size = (newsb->fs_old_ncyl * newsb->fs_old_spc) /
+                   NSPF(newsb);
                printf("Warning: last cylinder group is too small;\n");
                printf("    dropping it.  New size = %lu.\n",
                    (unsigned long int) fsbtodb(newsb, newsb->fs_size));
@@ -1540,7 +1537,7 @@
        /* Let's make sure we're not being shrunk into oblivion. */
        if (newsb->fs_ncg < 1) {
                printf("Size too small - filesystem would have no cylinders\n");
-               exit(1);
+               exit(EXIT_FAILURE);
        }
        /* Initialize for block motion. */
        blkmove_init();
@@ -1578,7 +1575,8 @@
                int newcgsize;
                cg = cgs[newsb->fs_ncg - 1];
                newcgsize = newsb->fs_size % newsb->fs_fpg;
-               oldcgsize = oldsb->fs_size - ((newsb->fs_ncg - 1) & oldsb->fs_fpg);
+               oldcgsize = oldsb->fs_size - ((newsb->fs_ncg - 1) &
+                   oldsb->fs_fpg);
                if (oldcgsize > oldsb->fs_fpg)
                        oldcgsize = oldsb->fs_fpg;
                evict_data(cg, newcgsize, oldcgsize - newcgsize);
@@ -1596,7 +1594,7 @@
                        slop -= oldsb->fs_ipg - cgs[i]->cg_cs.cs_nifree;
                if (slop < 0) {
                        printf("Sorry, would run out of inodes\n");



Home | Main Index | Thread Index | Old Index