]> git.neil.brown.name Git - lafs-utils.git/commitdiff
mkfs: allow creation of a filesystem on a regular file.
authorNeilBrown <neilb@suse.de>
Wed, 2 Mar 2011 23:39:26 +0000 (10:39 +1100)
committerNeilBrown <neilb@suse.de>
Wed, 2 Mar 2011 23:39:26 +0000 (10:39 +1100)
Primarily for ease of testing.

Signed-off-by: NeilBrown <neilb@suse.de>
lib/lafs_add_device.c
tools/mkfs.lafs.c

index 6e8a683b428b3a3b26c3d7aa962ec81aa1f018f8..3f5e702529eb1c65b2f7de9692fc8195244e8ba4 100644 (file)
@@ -1,3 +1,5 @@
+#define _GNU_SOURCE
+#define _FILE_OFFSET_BITS 64
 
 #include <unistd.h>
 #include <lafs/lafs.h>
@@ -28,6 +30,8 @@ static int get_logical_block_size(int fd)
        char path[60];
        char buf[20];
        fstat(fd, &stb);
+       if ((stb.st_mode & S_IFMT) != S_IFBLK)
+               return 512;
        sprintf(path, "/sys/dev/block/%d:%d/queue/logical_block_size",
                major(stb.st_rdev), minor(stb.st_rdev));
        sfd = open(path, O_RDONLY);
@@ -89,7 +93,8 @@ struct lafs_device *lafs_add_device(struct lafs *fs, char *devname, int fd,
         * Device block needs 1K and goes once at start and once at end.
         * Start location is 1K in unless basic block size is larger
         */
-       ioctl(fd, BLKGETSIZE64, &size);
+       if (ioctl(fd, BLKGETSIZE64, &size) < 0)
+               size = lseek64(fd, 0, SEEK_END);
        devblk = get_logical_block_size(fd);
        if (devblk < LAFS_DEVBLK_SIZE)
                devblk = LAFS_DEVBLK_SIZE;
index 40565f06d53f97bd8c63f38b0bbc3d0d11ec9bfc..d24ee442745eb0e0335effc0b9399a98963d3d00 100644 (file)
@@ -62,6 +62,7 @@ enum {
        opt_width,
        opt_stride,
        opt_noatime,
+       opt_regular,
 };
 
 char short_options[] = "-b:Vvh";
@@ -74,6 +75,7 @@ struct option long_options[] = {
        {"verbose",      0, 0, 'v'},
        {"help",         0, 0, 'h'},
        {"no-atime-file",0, 0, opt_noatime},
+       {"regular-file", 0, 0, opt_regular},
        {0, 0, 0, 0}
 };
 
@@ -255,27 +257,39 @@ void validate_parameters(long *block_bytes, long *segment_bytes, long *stride_by
        }
 }
 
-int open_device(char *devname, long long *device_bytes)
+int open_device(char *devname, long long *device_bytes, int regular_file)
 {
        /* must be able to get an exclusive open on the device and its size
         * must be non-trivial
-        *
+        * If 'regular_file', then expect a regular file to be used instead.
         */
        int fd;
        struct stat stb;
-       unsigned long long size;
+       unsigned long long size = 0;
 
        fd = open(devname, O_RDWR|O_EXCL);
-       if (fd < 0)
+       if (fd < 0 || fstat(fd, &stb) < 0) {
                fprintf(stderr, "mkfs.lafs: cannot open device %s: %s\n",
                        devname, strerror(errno));
-       else if (fstat(fd, &stb) < 0 ||
-                (stb.st_mode & S_IFMT) != S_IFBLK)
-               fprintf(stderr, "mkfs.lafs: %s is not a block device\n",
-                       devname);
-       else if (ioctl(fd, BLKGETSIZE64, &size) != 0)
-               fprintf(stderr, "mkfs.lafs: Cannot get size of %s\n",
-                       devname);
+               exit(2);
+       }
+
+       if (regular_file) {
+               if ((stb.st_mode & S_IFMT) != S_IFREG)
+                       fprintf(stderr, "mkfs.lafs: %s is not a regular file\n",
+                               devname);
+               else
+                       size = stb.st_size;
+       } else {
+               if ((stb.st_mode & S_IFMT) != S_IFBLK)
+                       fprintf(stderr, "mkfs.lafs: %s is not a block device\n",
+                               devname);
+               else if (ioctl(fd, BLKGETSIZE64, &size) != 0)
+                       fprintf(stderr, "mkfs.lafs: Cannot get size of %s\n",
+                               devname);
+       }
+       if (size == 0)
+               ;
        else if (size < 64*1024)
                fprintf(stderr, "mkfs.lafs: %s is too small for a LAFS filesystem\n",
                        devname);
@@ -296,6 +310,7 @@ int main(int argc, char *argv[])
        long long device_bytes;
        char *devname = NULL;
        int create_atime = 1;
+       int regular_file = 0;
        int opt;
        int dev_fd;
        struct lafs *lafs;
@@ -332,6 +347,9 @@ int main(int argc, char *argv[])
                case opt_noatime:
                        create_atime = 0;
                        break;
+               case opt_regular:
+                       regular_file = 1;
+                       break;
 
                case 1:
                        if (devname == NULL) {
@@ -356,7 +374,7 @@ int main(int argc, char *argv[])
        }
 
        /* Validate device */
-       dev_fd = open_device(devname, &device_bytes);
+       dev_fd = open_device(devname, &device_bytes, regular_file);
 
        /* Validate parameters */
        validate_parameters(&block_bytes, &segment_bytes, &stride_bytes, &width,