Whamcloud - gitweb
resize2fs: add debugging code to test the old online resizing interfaces
[tools/e2fsprogs.git] / resize / online.c
index 1a77839..2d34640 100644 (file)
@@ -21,6 +21,33 @@ extern char *program_name;
 
 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
 
+#ifdef __linux__
+static int parse_version_number(const char *s)
+{
+       int     major, minor, rev;
+       char    *endptr;
+       const char *cp = s;
+
+       if (!s)
+               return 0;
+       major = strtol(cp, &endptr, 10);
+       if (cp == endptr || *endptr != '.')
+               return 0;
+       cp = endptr + 1;
+       minor = strtol(cp, &endptr, 10);
+       if (cp == endptr || *endptr != '.')
+               return 0;
+       cp = endptr + 1;
+       rev = strtol(cp, &endptr, 10);
+       if (cp == endptr)
+               return 0;
+       return ((((major * 256) + minor) * 256) + rev);
+}
+
+#define VERSION_CODE(a,b,c) (((a) << 16) + ((b) << 8) + (c))
+
+#endif
+
 errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
                           blk64_t *new_size, int flags EXT2FS_ATTR((unused)))
 {
@@ -36,6 +63,18 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
        blk_t                   size;
        int                     fd, overhead;
        int                     use_old_ioctl = 1;
+       int                     no_meta_bg_resize = 0;
+       int                     no_resize_ioctl = 0;
+
+       if (getenv("RESIZE2FS_KERNEL_VERSION")) {
+               char *version_to_emulate = getenv("RESIZE2FS_KERNEL_VERSION");
+               int kvers = parse_version_number(version_to_emulate);
+
+               if (kvers < VERSION_CODE(3, 7, 0))
+                       no_meta_bg_resize = 1;
+               if (kvers < VERSION_CODE(3, 3, 0))
+                       no_resize_ioctl = 1;
+       }
 
        printf(_("Filesystem at %s is mounted on %s; "
                 "on-line resizing required\n"), fs->device_name, mtpt);
@@ -56,12 +95,35 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
                EXT2_DESC_PER_BLOCK(fs->super));
        printf("old_desc_blocks = %lu, new_desc_blocks = %lu\n",
               fs->desc_blocks, new_desc_blocks);
-       if (!(fs->super->s_feature_compat &
-             EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
-           new_desc_blocks != fs->desc_blocks) {
-               com_err(program_name, 0,
-                       _("Filesystem does not support online resizing"));
-               exit(1);
+
+       /*
+        * Do error checking to make sure the resize will be successful.
+        */
+       if ((access("/sys/fs/ext4/features/meta_bg_resize", R_OK) != 0) ||
+           no_meta_bg_resize) {
+               if (!EXT2_HAS_COMPAT_FEATURE(fs->super,
+                                       EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
+                   (new_desc_blocks != fs->desc_blocks)) {
+                       com_err(program_name, 0,
+                               _("Filesystem does not support online resizing"));
+                       exit(1);
+               }
+
+               if (EXT2_HAS_COMPAT_FEATURE(fs->super,
+                                       EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
+                   new_desc_blocks > (fs->desc_blocks +
+                                      fs->super->s_reserved_gdt_blocks)) {
+                       com_err(program_name, 0,
+                               _("Not enough reserved gdt blocks for resizing"));
+                       exit(1);
+               }
+
+               if ((ext2fs_blocks_count(sb) > MAX_32_NUM) ||
+                   (*new_size > MAX_32_NUM)) {
+                       com_err(program_name, 0,
+                               _("Kernel does not support resizing a file system this large"));
+                       exit(1);
+               }
        }
 
        fd = open(mtpt, O_RDONLY);
@@ -71,13 +133,24 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
                exit(1);
        }
 
-       if (ioctl(fd, EXT4_IOC_RESIZE_FS, new_size)) {
+       if (no_resize_ioctl) {
+               printf(_("Old resize interface requested.\n"));
+       } else if (ioctl(fd, EXT4_IOC_RESIZE_FS, new_size)) {
                /*
                 * If kernel does not support EXT4_IOC_RESIZE_FS, use the
                 * old online resize. Note that the old approach does not
                 * handle >32 bit file systems
+                *
+                * Sigh, if we are running a 32-bit binary on a 64-bit
+                * kernel (which happens all the time on the MIPS
+                * architecture in Debian, but can happen on other CPU
+                * architectures as well) we will get EINVAL returned
+                * when an ioctl doesn't exist, at least up to Linux
+                * 3.1.  See compat_sys_ioctl() in fs/compat_ioctl.c
+                * in the kernel sources.  This is probably a kernel
+                * bug, but work around it here.
                 */
-               if (errno != ENOTTY) {
+               if ((errno != ENOTTY) && (errno != EINVAL)) {
                        if (errno == EPERM)
                                com_err(program_name, 0,
                                _("Permission denied to resize filesystem"));
@@ -92,13 +165,6 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
                return 0;
        }
 
-       if ((ext2fs_blocks_count(sb) > MAX_32_NUM) ||
-           (*new_size > MAX_32_NUM)) {
-               com_err(program_name, 0,
-                       _("Kernel does not support resizing a file system "
-                         "this large"));
-               exit(1);
-       }
        size = ext2fs_blocks_count(sb);
 
        if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {