loopdev: fix losetup failure with a lost device file
Subsystems (2)
PATCH 1/1 loopdev: fix losetup failure with a lost device file
If a /dev/loopX is lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"
Users can resurrect the loop device by recreating the device using mknod, but regular users might not be aware. Since /sysfs still have the loop device intact, this commit will detect that case by scanning sysfs and recreate the lost device file.
--- lib/loopdev.c | 100 ++++++++++++++++++++++++++++++++++++++++---- sys-utils/losetup.c | 14 +++---- 2 files changed, 97 insertions(+), 17 deletions(-)
lib/loopdev.c +92 -8
@@ -279,6 +279,87 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; } +static int loopcxt_get_devid (struct loopdev_cxt *lc) +{ +int devid =-1 ; +const char *p, *dev =loopcxt_get_device (lc); + +if (!dev) +return -EINVAL ; + + p =strrchr (dev,'/' ); +if (!p || (sscanf (p,"/loop%d" , &devid) !=1 + &&sscanf (p,"/%d" , &devid) !=1 )) +return -EINVAL ; + +return devid; +} + +/* scan sysfs entry for loop device. */ +static int loopcxt_scan_sysfs (struct loopdev_cxt *lc) +{ +struct dirent *d; +DIR *sysblock; +int nr; +char name[256 ]; + +if (!loopcxt_sysfs_available (lc)) +return 0 ; + + sysblock =opendir (_PATH_SYS_BLOCK); +if (!sysblock) +return 0 ; + + nr =loopcxt_get_devid (lc); +sprintf (name,"loop%d" , nr); + +while ((d =readdir (sysblock))) { +if (!strcmp (d->d_name, name)) +return 1 ; + } + +return 0 ; +} + +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will try to detect whether that is case, +* if so, it will recreate the device file and open it. +* Return fd if succeed, otherwise negative error code. +*/ +static int loopcxt_fix_lost_device_file (struct loopdev_cxt *lc) +{ +char path[PATH_MAX ]; +int nr, ret; +FILE *fp; +unsigned int major, minor; + +if (!loopcxt_scan_sysfs (lc)) +return -1 ; + + nr =loopcxt_get_devid (lc); +if (nr <0 ) +return -1 ; + ret =snprintf (path,PATH_MAX ,"%s/loop%d/dev" , + _PATH_SYS_BLOCK, nr); +if (ret <=0 || ret >=PATH_MAX ) +return -1 ; + fp =fopen (path,"r" ); +if (!fp) +return -1 ; + ret =fscanf (fp,"%d:%d" , &major, &minor); +fclose (fp); +if (ret !=2 ) +return -1 ; +sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ; +return open (path, lc->mode |O_CLOEXEC ); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode) {int old =-1 ;
@@ -303,7 +384,17 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode)DBG (CXT ,ul_debugobj (lc,"open %s [%s]: %m" , lc->device, mode ==O_RDONLY ?"ro" : mode ==O_RDWR ?"rw" :"??" )); - +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { + lc->fd =loopcxt_fix_lost_device_file (lc); +DBG (CXT ,ul_debugobj (lc,"recreate %s fd %d" , + lc->device, lc->fd)); +/* loop file is not lost but doesn't exist, +* reset errno to have user know. +*/ +if (lc->fd <0 ) + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lc->fd = old;
@@ -416,13 +507,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ -if (!is_loopdev (lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;if ((lc->iter.flags &LOOPITER_FL_USED ) && used)
sys-utils/losetup.c +5 -9
@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;
@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;
@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;
@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;
@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;
Signature · 1 line
2.39.3 (Apple Git-145)
Anybody can help take a look at this patch?
Quoted 195 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"Users can resurrect the loop device by recreating the device using mknod, but regular users might not be aware. Since /sysfs still have the loop device intact, this commit will detect that case by scanning sysfs and recreate the lost device file.
Signed-off-by Junxiao Bi <[email protected]>--- lib/loopdev.c | 100 ++++++++++++++++++++++++++++++++++++++++---- sys-utils/losetup.c | 14 +++---- 2 files changed, 97 insertions(+), 17 deletions(-)lib/loopdev.c +92
-8@@ -279,6 +279,87 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; } +static int loopcxt_get_devid (struct loopdev_cxt *lc) +{ +int devid =-1 ; +const char *p, *dev =loopcxt_get_device (lc); + +if (!dev) +return -EINVAL ; + + p =strrchr (dev,'/' ); +if (!p || (sscanf (p,"/loop%d" , &devid) !=1 + &&sscanf (p,"/%d" , &devid) !=1 )) +return -EINVAL ; + +return devid; +} + +/* scan sysfs entry for loop device. */ +static int loopcxt_scan_sysfs (struct loopdev_cxt *lc) +{ +struct dirent *d; +DIR *sysblock; +int nr; +char name[256 ]; + +if (!loopcxt_sysfs_available (lc)) +return 0 ; + + sysblock =opendir (_PATH_SYS_BLOCK); +if (!sysblock) +return 0 ; + + nr =loopcxt_get_devid (lc); +sprintf (name,"loop%d" , nr); + +while ((d =readdir (sysblock))) { +if (!strcmp (d->d_name, name)) +return 1 ; + } + +return 0 ; +} + +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will try to detect whether that is case, +* if so, it will recreate the device file and open it. +* Return fd if succeed, otherwise negative error code. +*/ +static int loopcxt_fix_lost_device_file (struct loopdev_cxt *lc) +{ +char path[PATH_MAX ]; +int nr, ret; +FILE *fp; +unsigned int major, minor; + +if (!loopcxt_scan_sysfs (lc)) +return -1 ; + + nr =loopcxt_get_devid (lc); +if (nr <0 ) +return -1 ; + ret =snprintf (path,PATH_MAX ,"%s/loop%d/dev" , + _PATH_SYS_BLOCK, nr); +if (ret <=0 || ret >=PATH_MAX ) +return -1 ; + fp =fopen (path,"r" ); +if (!fp) +return -1 ; + ret =fscanf (fp,"%d:%d" , &major, &minor); +fclose (fp); +if (ret !=2 ) +return -1 ; +sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ; +return open (path, lc->mode |O_CLOEXEC ); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode) {int old =-1 ;@@ -303,7 +384,17 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode)DBG (CXT ,ul_debugobj (lc,"open %s [%s]: %m" , lc->device, mode ==O_RDONLY ?"ro" : mode ==O_RDWR ?"rw" :"??" )); - +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { + lc->fd =loopcxt_fix_lost_device_file (lc); +DBG (CXT ,ul_debugobj (lc,"recreate %s fd %d" , + lc->device, lc->fd)); +/* loop file is not lost but doesn't exist, +* reset errno to have user know. +*/ +if (lc->fd <0 ) + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lc->fd = old;@@ -416,13 +507,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ -if (!is_loopdev (lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;if ((lc->iter.flags &LOOPITER_FL_USED ) && used)sys-utils/losetup.c +5
-9@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;
Quoted 1 line · On 2023-10-18 01:06, [email protected] wrote:
Anybody can help take a look at this patch?
As a note, AFAIK it usually takes 7 to 10 days for the submitted patches to be reviewed.
Quoted 195 lines · Junxiao Bi
Quoted 194 lines
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"Users can resurrect the loop device by recreating the device using mknod, but regular users might not be aware. Since /sysfs still have the loop device intact, this commit will detect that case by scanning sysfs and recreate the lost device file.
Signed-off-by Junxiao Bi <[email protected]>--- lib/loopdev.c | 100 ++++++++++++++++++++++++++++++++++++++++---- sys-utils/losetup.c | 14 +++---- 2 files changed, 97 insertions(+), 17 deletions(-)lib/loopdev.c +91
-1@@ -279,6 +279,87 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; } +static int loopcxt_get_devid(struct loopdev_cxt *lc) +{ +int devid =-1 ; +const char *p, *dev =loopcxt_get_device (lc); + +if (!dev) +return -EINVAL ; + + p =strrchr (dev,'/' ); +if (!p || (sscanf (p,"/loop%d" , &devid) !=1 + &&sscanf (p,"/%d" , &devid) !=1 )) +return -EINVAL ; + +return devid; +} + +/* scan sysfs entry for loop device. */ +static int loopcxt_scan_sysfs (struct loopdev_cxt *lc) +{ +struct dirent *d; +DIR *sysblock; +int nr; +char name[256 ]; + +if (!loopcxt_sysfs_available (lc)) +return 0 ; + + sysblock =opendir (_PATH_SYS_BLOCK); +if (!sysblock) +return 0 ; + + nr =loopcxt_get_devid (lc); +sprintf (name,"loop%d" , nr); + +while ((d =readdir (sysblock))) { +if (!strcmp (d->d_name, name)) +return 1 ; + } + +return 0 ; +} + +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will try to detect whether that is case, +* if so, it will recreate the device file and open it. +* Return fd if succeed, otherwise negative error code. +*/ +static int loopcxt_fix_lost_device_file (struct loopdev_cxt *lc) +{ +char path[PATH_MAX ]; +int nr, ret; +FILE *fp; +unsigned int major, minor; + +if (!loopcxt_scan_sysfs (lc)) +return -1 ; + + nr =loopcxt_get_devid (lc); +if (nr <0 ) +return -1 ; + ret =snprintf (path,PATH_MAX ,"%s/loop%d/dev" , + _PATH_SYS_BLOCK, nr); +if (ret <=0 || ret >=PATH_MAX ) +return -1 ; + fp =fopen (path,"r" ); +if (!fp) +return -1 ; + ret =fscanf (fp,"%d:%d" , &major, &minor); +fclose (fp); +if (ret !=2 ) +return -1 ; +sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ; +return open (path, lc->mode |O_CLOEXEC ); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode) {int old =-1 ;@@ -303,7 +384,17 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode)DBG (CXT ,ul_debugobj (lc,"open %s [%s]: %m" , lc->device, mode ==O_RDONLY ?"ro" : mode ==O_RDWR ?"rw" :"??" )); - +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { + lc->fd =loopcxt_fix_lost_device_file (lc); +DBG (CXT ,ul_debugobj (lc,"recreate %s fd %d" , + lc->device, lc->fd)); +/* loop file is not lost but doesn't exist, +* reset errno to have user know. +*/ +if (lc->fd <0 ) + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lc->fd = old;lib/loopdev.c +0
-6@@ -416,13 +507,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ - if (!is_loopdev(lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;if ((lc->iter.flags &LOOPITER_FL_USED ) && used)sys-utils/losetup.c +5
-8@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; - if (!is_loopdev(argv[optind]) || - loopcxt_set_device(&lc, argv[optind])) + if (loopcxt_set_device(&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;
Quoted 8 lines · Dragan Simic
Quoted 1 line · On 2023-10-18 01:06, [email protected] wrote:
Anybody can help take a look at this patch?
As a note, AFAIK it usually takes 7 to 10 days for the submitted patches to be reviewed.
Ah, I see. Thank you.
Quoted 190 lines
Quoted 190 lines
Quoted 190 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"
Users can resurrect the loop device by recreating the device using mknod, but regular users might not be aware. Since /sysfs still have the loop device intact, this commit will detect that case by scanning sysfs and recreate the lost device file.Signed-off-by Junxiao Bi <[email protected]>--- lib/loopdev.c | 100 ++++++++++++++++++++++++++++++++++++++++---- sys-utils/losetup.c | 14 +++---- 2 files changed, 97 insertions(+), 17 deletions(-)lib/loopdev.c +91
-7@@ -279,6 +279,87 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; } +static int loopcxt_get_devid(struct loopdev_cxt *lc) +{ +int devid =-1 ; +const char *p, *dev =loopcxt_get_device (lc); + +if (!dev) +return -EINVAL ; + + p =strrchr (dev,'/' ); +if (!p || (sscanf (p,"/loop%d" , &devid) !=1 + &&sscanf (p,"/%d" , &devid) !=1 )) +return -EINVAL ; + +return devid; +} + +/* scan sysfs entry for loop device. */ +static int loopcxt_scan_sysfs (struct loopdev_cxt *lc) +{ +struct dirent *d; +DIR *sysblock; +int nr; +char name[256 ]; + +if (!loopcxt_sysfs_available (lc)) +return 0 ; + + sysblock =opendir (_PATH_SYS_BLOCK); +if (!sysblock) +return 0 ; + + nr =loopcxt_get_devid (lc); +sprintf (name,"loop%d" , nr); + +while ((d =readdir (sysblock))) { +if (!strcmp (d->d_name, name)) +return 1 ; + } + +return 0 ; +} + +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will try to detect whether that is case, +* if so, it will recreate the device file and open it. +* Return fd if succeed, otherwise negative error code. +*/ +static int loopcxt_fix_lost_device_file (struct loopdev_cxt *lc) +{ +char path[PATH_MAX ]; +int nr, ret; +FILE *fp; +unsigned int major, minor; + +if (!loopcxt_scan_sysfs (lc)) +return -1 ; + + nr =loopcxt_get_devid (lc); +if (nr <0 ) +return -1 ; + ret =snprintf (path,PATH_MAX ,"%s/loop%d/dev" , + _PATH_SYS_BLOCK, nr); +if (ret <=0 || ret >=PATH_MAX ) +return -1 ; + fp =fopen (path,"r" ); +if (!fp) +return -1 ; + ret =fscanf (fp,"%d:%d" , &major, &minor); +fclose (fp); +if (ret !=2 ) +return -1 ; +sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ; +return open (path, lc->mode |O_CLOEXEC ); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode) {int old =-1 ;@@ -303,7 +384,17 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode)DBG (CXT ,ul_debugobj (lc,"open %s [%s]: %m" , lc->device, mode ==O_RDONLY ?"ro" : mode ==O_RDWR ?"rw" :"??" )); - +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { + lc->fd =loopcxt_fix_lost_device_file (lc); +DBG (CXT ,ul_debugobj (lc,"recreate %s fd %d" , + lc->device, lc->fd)); +/* loop file is not lost but doesn't exist, +* reset errno to have user know. +*/ +if (lc->fd <0 ) + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lc->fd = old;@@ -416,13 +507,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ - if (!is_loopdev(lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;if ((lc->iter.flags &LOOPITER_FL_USED ) && used)sys-utils/losetup.c +5
-8@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; - if (!is_loopdev(argv[optind]) || - loopcxt_set_device(&lc, argv[optind])) + if (loopcxt_set_device(&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;
Hi Junxiao,
Quoted 5 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"
What makes /dev/loopX more prone to accidental deletion than say /dev/loop-control or /dev/sda?
Quoted 16 lines
Users can resurrect the loop device by recreating the device using mknod, but regular users might not be aware. Since /sysfs still have the loop device intact, this commit will detect that case by scanning sysfs and recreate the lost device file.
Signed-off-by Junxiao Bi <[email protected]>--- lib/loopdev.c | 100 ++++++++++++++++++++++++++++++++++++++++---- sys-utils/losetup.c | 14 +++---- 2 files changed, 97 insertions(+), 17 deletions(-)lib/loopdev.c +0
-0
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 283
Quoted 5 lines
lib/loopdev.c +2
-0@@ -279,6 +279,87 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; } +static int loopcxt_get_devid (struct loopdev_cxt *lc) +{
The only use of this function is to directly format the numer extracted from loopX back into loopX.
Why not use the loopX string directly?
Quoted 28 lines
lib/loopdev.c +28
-0+int devid =-1 ; +const char *p, *dev =loopcxt_get_device (lc); + +if (!dev) +return -EINVAL ; + + p =strrchr (dev,'/' ); +if (!p || (sscanf (p,"/loop%d" , &devid) !=1 + &&sscanf (p,"/%d" , &devid) !=1 )) +return -EINVAL ; + +return devid; +} + +/* scan sysfs entry for loop device. */ +static int loopcxt_scan_sysfs (struct loopdev_cxt *lc) +{ +struct dirent *d; +DIR *sysblock; +int nr; +char name[256 ]; + +if (!loopcxt_sysfs_available (lc)) +return 0 ; + + sysblock =opendir (_PATH_SYS_BLOCK); +if (!sysblock) +return 0 ;
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 321
Quoted 10 lines
lib/loopdev.c +10
-0+ + nr =loopcxt_get_devid (lc); +sprintf (name,"loop%d" , nr); + +while ((d =readdir (sysblock))) { +if (!strcmp (d->d_name, name)) +return 1 ; + } + +return 0 ;
All of this could be replaced by ul_path_accessf().
Quoted 19 lines
lib/loopdev.c +19
-0+} + +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will try to detect whether that is case, +* if so, it will recreate the device file and open it. +* Return fd if succeed, otherwise negative error code. +*/ +static int loopcxt_fix_lost_device_file (struct loopdev_cxt *lc) +{ +char path[PATH_MAX ]; +int nr, ret; +FILE *fp; +unsigned int major, minor; + +if (!loopcxt_scan_sysfs (lc)) +return -1 ; + + nr =loopcxt_get_devid (lc);
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 350
Quoted 10 lines
lib/loopdev.c +10
-0+if (nr <0 ) +return -1 ; + ret =snprintf (path,PATH_MAX ,"%s/loop%d/dev" , + _PATH_SYS_BLOCK, nr); +if (ret <=0 || ret >=PATH_MAX ) +return -1 ; + fp =fopen (path,"r" ); +if (!fp) +return -1 ; + ret =fscanf (fp,"%d:%d" , &major, &minor);
sysfs_devname_to_devno()
Quoted 104 lines
lib/loopdev.c +23
-8+fclose (fp); +if (ret !=2 ) +return -1 ; +sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ; +return open (path, lc->mode |O_CLOEXEC ); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode) {int old =-1 ;@@ -303,7 +384,17 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode)DBG (CXT ,ul_debugobj (lc,"open %s [%s]: %m" , lc->device, mode ==O_RDONLY ?"ro" : mode ==O_RDWR ?"rw" :"??" )); - +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { + lc->fd =loopcxt_fix_lost_device_file (lc); +DBG (CXT ,ul_debugobj (lc,"recreate %s fd %d" , + lc->device, lc->fd)); +/* loop file is not lost but doesn't exist, +* reset errno to have user know. +*/ +if (lc->fd <0 ) + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lc->fd = old;@@ -416,13 +507,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ -if (!is_loopdev (lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;if ((lc->iter.flags &LOOPITER_FL_USED ) && used)sys-utils/losetup.c +5
-9@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;Signature · 1 line
2.39.3 (Apple Git-145)
Hi Thomas,
Thank you for the review.
Quoted 10 lines · Thomas Weißschuh
Hi Junxiao,
Quoted 5 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"What makes
/dev/loopXmore prone to accidental deletion than say/dev/loop-controlor/dev/sda?
I can't see /dev/loopX is more prone to be deleted than other files. The issue was triggered by one customer, somehow the lost loop device file caused their automation script failure, they don't know how that loop device file get lost. I think it would be good to support this to make losetup more resilient.
Quoted 20 lines
Quoted 20 lines
Users can resurrect the loop device by recreating the device using mknod, but regular users might not be aware. Since /sysfs still have the loop device intact, this commit will detect that case by scanning sysfs and recreate the lost device file.
Signed-off-by Junxiao Bi <[email protected]>--- lib/loopdev.c | 100 ++++++++++++++++++++++++++++++++++++++++---- sys-utils/losetup.c | 14 +++---- 2 files changed, 97 insertions(+), 17 deletions(-)lib/loopdev.c +0
-0@@ -279,6 +279,87 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; }
Quoted 5 lines
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 283
Quoted 2 lines
lib/loopdev.c +2
-0+static int loopcxt_get_devid (struct loopdev_cxt *lc) +{The only use of this function is to directly format the numer extracted from loopX back into loopX.
Why not use the loopX string directly?
This function can be used in other function to remove duplicated code, for example loopcxt_add_device(), i can add a new patch to do that in V2.
Quoted 32 lines
Quoted 32 lines
lib/loopdev.c +31
-0+int devid =-1 ; +const char *p, *dev =loopcxt_get_device (lc); + +if (!dev) +return -EINVAL ; + + p =strrchr (dev,'/' ); +if (!p || (sscanf (p,"/loop%d" , &devid) !=1 + &&sscanf (p,"/%d" , &devid) !=1 )) +return -EINVAL ; + +return devid; +} + +/* scan sysfs entry for loop device. */ +static int loopcxt_scan_sysfs (struct loopdev_cxt *lc) +{ +struct dirent *d; +DIR *sysblock; +int nr; +char name[256 ]; + +if (!loopcxt_sysfs_available (lc)) +return 0 ; + + sysblock =opendir (_PATH_SYS_BLOCK); +if (!sysblock) +return 0 ; + + nr =loopcxt_get_devid (lc); +sprintf (name,"loop%d" , nr);
Quoted 8 lines
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 321
Quoted 7 lines
lib/loopdev.c +7
-0+ +while ((d =readdir (sysblock))) { +if (!strcmp (d->d_name, name)) +return 1 ; + } + +return 0 ;All of this could be replaced by
ul_path_accessf().
I will see how to do it in v2.
Quoted 23 lines
Quoted 23 lines
lib/loopdev.c +22
-0+} + +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will try to detect whether that is case, +* if so, it will recreate the device file and open it.:q +* Return fd if succeed, otherwise negative error code. +*/ +static int loopcxt_fix_lost_device_file (struct loopdev_cxt *lc) +{ +char path[PATH_MAX ]; +int nr, ret; +FILE *fp; +unsigned int major, minor; + +if (!loopcxt_scan_sysfs (lc)) +return -1 ; + + nr =loopcxt_get_devid (lc); +if (nr <0 ) +return -1 ; + ret = snprintf(path, PATH_MAX, "%s/loop%d/dev",
Quoted 8 lines
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 350
Quoted 7 lines
lib/loopdev.c +7
-0+ _PATH_SYS_BLOCK, nr); +if (ret <=0 || ret >=PATH_MAX ) +return -1 ; + fp =fopen (path,"r" ); +if (!fp) +return -1 ; + ret =fscanf (fp,"%d:%d" , &major, &minor);
sysfs_devname_to_devno()
This function only works when /dev/loopX exist. Anyway this part of code will be removed in V2, as Karel suggested we should not do mknod in losetup.
Thanks,
Junxiao.
Quoted 105 lines
Quoted 105 lines
lib/loopdev.c +23
-8+fclose (fp); +if (ret !=2 ) +return -1 ; +sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ; +return open (path, lc->mode |O_CLOEXEC ); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode) {int old =-1 ;@@ -303,7 +384,17 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode)DBG (CXT ,ul_debugobj (lc,"open %s [%s]: %m" , lc->device, mode ==O_RDONLY ?"ro" : mode ==O_RDWR ?"rw" :"??" )); - +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { + lc->fd =loopcxt_fix_lost_device_file (lc); +DBG (CXT ,ul_debugobj (lc,"recreate %s fd %d" , + lc->device, lc->fd)); +/* loop file is not lost but doesn't exist, +* reset errno to have user know. +*/ +if (lc->fd <0 ) + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lc->fd = old;@@ -416,13 +507,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ -if (!is_loopdev (lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;if ((lc->iter.flags &LOOPITER_FL_USED ) && used)sys-utils/losetup.c +5
-9@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;Signature · 1 line
2.39.3 (Apple Git-145)
Karel Zak
maintainerHi Junxiao,
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 359
Quoted 6 lines · Junxiao Bi
lib/loopdev.c +6
-0+sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ;
frankly, it will be challenging to convince me that using mknode in a tool like losetup is a good idea. After all these decades, it's still not a common practice (although I have see many attempts). Nodes are typically created by udevd by default, following udev rules.
IMHO, it's better to report the problem (perhaps with a helpful hint) as error message and assume that the administrator will address it.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 14 lines · Karel Zak
Hi Junxiao,
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 359
Quoted 6 lines · Junxiao Bi
lib/loopdev.c +6
-0+sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ;frankly, it will be challenging to convince me that using mknode in a tool like losetup is a good idea. After all these decades, it's still not a common practice (although I have see many attempts). Nodes are typically created by udevd by default, following udev rules.
Quoted 2 lines
IMHO, it's better to report the problem (perhaps with a helpful hint) as error message and assume that the administrator will address it.
Fair enough. I will make a V2 with only detailed error logging.
Thanks,
Junxiao.
Quoted 3 lines
Karel
Quoted 13 lines · Karel Zak
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 359
Quoted 6 lines · Junxiao Bi
lib/loopdev.c +6
-0+sprintf (path,"/dev/loop%d" , nr); +/* default loop device permission is "brw-rw----" */ +umask (0003 ); + ret =mknod (path,S_IFBLK |0660 ,makedev (major, minor)); +if (ret) +return -1 ;frankly, it will be challenging to convince me that using mknode in a tool like losetup is a good idea. After all these decades, it's still not a common practice (although I have see many attempts). Nodes are typically created by udevd by default, following udev rules.
Quoted 2 lines
IMHO, it's better to report the problem (perhaps with a helpful hint) as error message and assume that the administrator will address it.
Perhaps a separate exit status could also be returned? By the way, when does actually losetup(8) return 2, as described in the man page? I'm having troubles finding the exact place in the source code where that happens.