From: NeilBrown Date: Wed, 2 Mar 2011 23:39:26 +0000 (+1100) Subject: mkfs: allow creation of a filesystem on a regular file. X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=254c17e5d95a2b8d2ef9706a6024e772bd6bafdd;p=lafs-utils.git mkfs: allow creation of a filesystem on a regular file. Primarily for ease of testing. Signed-off-by: NeilBrown --- diff --git a/lib/lafs_add_device.c b/lib/lafs_add_device.c index 6e8a683..3f5e702 100644 --- a/lib/lafs_add_device.c +++ b/lib/lafs_add_device.c @@ -1,3 +1,5 @@ +#define _GNU_SOURCE +#define _FILE_OFFSET_BITS 64 #include #include @@ -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; diff --git a/tools/mkfs.lafs.c b/tools/mkfs.lafs.c index 40565f0..d24ee44 100644 --- a/tools/mkfs.lafs.c +++ b/tools/mkfs.lafs.c @@ -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,