Btrfs-progs: check out if the swap device
PATCH v3 1/1 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.
---
(this patch is based on Chris's raid56-experimental branch) v2 -> v3:
- Open again without O_EXCL so that the problem should not occur (btrfs_register_one_device() fails if O_EXCL is on)
v1 -> v2:
- O_EXCL is specified at open(), as a last safety check
- Check when the swap device is a file is added
- Translate '\nnn'(octal) to character
Thank you Eric, David, Zach and others.
--- mkfs.c | 52 +++++++++++++++++++++++++++++++++++++++++++-- utils.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 1 + 3 files changed, 126 insertions(+), 2 deletions(-)
mkfs.c +50 -2
@@ -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,
utils.c +75 -0
78 lines not on this page · raw message
utils.h +1 -0
@@ -55,6 +55,7 @@ int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args, struct btrfs_ioctl_dev_info_args **di_ret);char *__strncpy__null (char *dest,const char *src,size_t n); +int is_swap_device (const char *file);/* Helper to always get proper size of the destination string */ #define strncpy_null (dest, src) __strncpy__null(dest, src, sizeof(dest))
Quoted 1 line · Tsutomu Itoh
Signed-off-by Tsutomu Itoh <[email protected]>
Worked for me here in these situations (ie. mkfs failed, filenames specified given as simple path or with mixture of ./ and ../ leading to the device):
* mounted /dev/sdx
* /dev/sdx part of a multi-device mounted fs
* /dev/sdx swap device
* file as a swap device
* swap files with funny names: \ \040 \134 \888 <space>
Quoted 5 lines · Tsutomu Itoh
mkfs.c +5
-0+if (ret <0 ) { +fprintf (stderr,"error checking %s status: %s\n" , file, +strerror (-ret)); +exit (1 ); + }
mkfs.c int main(int ac, char **av) · line 1378
Quoted 10 lines
mkfs.c +7
-0+/* 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 ); + }
This is fine and works (as tested by David) - but I'm not sure if the below suggestions from Zach were taken into account.
1. If the check with "open(file, O_RDWR|O_EXCL)" shows that the device is available, there's no point in checking if it is mounted as a swap device. A preliminary check using this could precede all other checks which should be skipped if it shows success.
2. If there's an error checking the status (for example lets say /proc/swaps is deprecated), we should print the informational message but not error out.
Quoted 10 lines · Zach Brown
- First always open with
O_EXCL. If it succeeds then there's no reason to check/proc/swapsat all. (Maybe it doesn't need to trycheck_mounted()there either? Not sure if it's protecting against accidentally mounting mounted shared storage or not.)...
- At no point is failure of any of the
/proc/swapsparsing fatal. It'd carry on ignoring errors until it doesnt have work to do. It'd only ever print the nice message when it finds a match.
Signature · 4 lines
__________ Brendan Hide http://swiftspirit.co.za/ http://www.webafrica.co.za/?AFF1E97
Quoted 9 lines · Brendan Hide
Quoted 8 lines · Tsutomu Itoh
mkfs.c +5
-0+if (ret <0 ) { +fprintf (stderr,"error checking %s status: %s\n" , file, +strerror (-ret)); +exit (1 ); + }
Quoted 8 lines
mkfs.c int main(int ac, char **av) · line 1395
Quoted 7 lines
mkfs.c +7
-0+/* 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 ); + }This is fine and works (as tested by David) - but I'm not sure if the below
suggestions from Zach were taken into account.
Quoted 8 lines
1. If the check with "
open(file, O_RDWR|O_EXCL)" shows that the device is available, there's no point in checking if it is mounted as a swap device. A preliminary check using this could precede all other checks which should be skipped if it shows success.2. If there's an error checking the status (for example lets say
/proc/swapsis deprecated), we should print the informational message but not error out.
If "open(file, O_RDWR|O_EXCL)" failed, we should output an appropriate message why it failed. So, I'm testing is_swap_device() and check_mounted() first. But is_swap_device() is not perfect, so I'm trying to open O_EXCL after all other tests, as a last safety check.
Thanks,
Tsutomu
Quoted 14 lines · Zach Brown
Quoted 10 lines
- First always open with
O_EXCL. If it succeeds then there's no reason to check/proc/swapsat all. (Maybe it doesn't need to trycheck_mounted()there either? Not sure if it's protecting against accidentally mounting mounted shared storage or not.)...
- At no point is failure of any of the
/proc/swapsparsing fatal. It'd carry on ignoring errors until it doesnt have work to do. It'd only ever print the nice message when it finds a match.