summaryrefslogtreecommitdiff
path: root/mkfs.c
diff options
context:
space:
mode:
authorTsutomu Itoh <t-itoh@jp.fujitsu.com>2013-02-14 16:53:04 +0900
committerDavid Sterba <dsterba@suse.cz>2013-02-19 11:15:30 +0100
commitd065d63057c249e109159d0620ce461954b1bc73 (patch)
treead8acea1e80f7dd9335608884b942b9d229f9c5c /mkfs.c
parent0c38ff3ca4f481bbae74283fd00a56fbd938e4b2 (diff)
Btrfs-progs: check out if the swap device
Currently, the following commands succeed. # cat /proc/swaps Filename Type Size Used Priority /dev/sda3 partition 8388604 0 -1 /dev/sdc8 partition 9765884 0 -2 # mkfs.btrfs /dev/sdc8 WARNING! - Btrfs v0.20-rc1-165-g82ac345 IS EXPERIMENTAL WARNING! - see http://btrfs.wiki.kernel.org before using fs created label (null) on /dev/sdc8 nodesize 4096 leafsize 4096 sectorsize 4096 size 9.31GB Btrfs v0.20-rc1-165-g82ac345 # btrfs fi sh /dev/sdc8 Label: none uuid: fc0bdbd0-7eed-460f-b4e9-131273b66df2 Total devices 1 FS bytes used 28.00KB devid 1 size 9.31GB used 989.62MB path /dev/sdc8 Btrfs v0.20-rc1-165-g82ac345 # But we should check out the swap device. Fixed it. Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com> Tested-by: David Sterba <dsterba@suse.cz>
Diffstat (limited to 'mkfs.c')
-rw-r--r--mkfs.c52
1 files changed, 50 insertions, 2 deletions
diff --git a/mkfs.c b/mkfs.c
index 2d3c2af8..2210312c 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -1366,6 +1366,16 @@ int main(int ac, char **av)
if (source_dir == 0) {
file = av[optind++];
+ ret = is_swap_device(file);
+ if (ret < 0) {
+ fprintf(stderr, "error checking %s status: %s\n", file,
+ strerror(-ret));
+ exit(1);
+ }
+ if (ret == 1) {
+ fprintf(stderr, "%s is a swap device\n", file);
+ exit(1);
+ }
ret = check_mounted(file);
if (ret < 0) {
fprintf(stderr, "error checking %s mount status\n", file);
@@ -1376,9 +1386,23 @@ int main(int ac, char **av)
exit(1);
}
ac--;
+ /* check if the device is busy */
+ fd = open(file, O_RDWR|O_EXCL);
+ if (fd < 0) {
+ fprintf(stderr, "unable to open %s: %s\n", file,
+ strerror(errno));
+ exit(1);
+ }
+ close(fd);
+ /*
+ * open again without O_EXCL so that the problem should not
+ * occur by the following processing.
+ * (btrfs_register_one_device() fails if O_EXCL is on)
+ */
fd = open(file, O_RDWR);
if (fd < 0) {
- fprintf(stderr, "unable to open %s\n", file);
+ fprintf(stderr, "unable to open %s: %s\n", file,
+ strerror(errno));
exit(1);
}
first_file = file;
@@ -1461,6 +1485,16 @@ int main(int ac, char **av)
int old_mixed = mixed;
file = av[optind++];
+ ret = is_swap_device(file);
+ if (ret < 0) {
+ fprintf(stderr, "error checking %s status: %s\n", file,
+ strerror(-ret));
+ exit(1);
+ }
+ if (ret == 1) {
+ fprintf(stderr, "%s is a swap device\n", file);
+ exit(1);
+ }
ret = check_mounted(file);
if (ret < 0) {
fprintf(stderr, "error checking %s mount status\n",
@@ -1471,9 +1505,23 @@ int main(int ac, char **av)
fprintf(stderr, "%s is mounted\n", file);
exit(1);
}
+ /* check if the device is busy */
+ fd = open(file, O_RDWR|O_EXCL);
+ if (fd < 0) {
+ fprintf(stderr, "unable to open %s: %s\n", file,
+ strerror(errno));
+ exit(1);
+ }
+ close(fd);
+ /*
+ * open again without O_EXCL so that the problem should not
+ * occur by the following processing.
+ * (btrfs_register_one_device() fails if O_EXCL is on)
+ */
fd = open(file, O_RDWR);
if (fd < 0) {
- fprintf(stderr, "unable to open %s\n", file);
+ fprintf(stderr, "unable to open %s: %s\n", file,
+ strerror(errno));
exit(1);
}
ret = btrfs_device_already_in_root(root, fd,