Skip to content

Commit a184151

Browse files
committed
mkfs.nilfs2: use libmountchk to check the mount status of target device
The current mkfs.nilfs2 device mount status check does not support files mounted via loop devices. Use libmountchk (check_mount) for stricter checking. Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
1 parent bc4885c commit a184151

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

sbin/mkfs.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
#include "crc32.h"
7171

7272

73+
extern int check_mount(const char *device);
74+
7375
typedef uint64_t blocknr_t;
7476

7577
#define nilfs_crc32(seed, data, length) crc32_le(seed, data, length)
@@ -329,7 +331,6 @@ static int nilfs_mkfs_discard_zeroes_data(int fd)
329331
#endif
330332

331333
static void disk_scan(const char *device);
332-
static void check_mount(int fd, const char *device);
333334

334335
#if HAVE_LIBBLKID
335336
static void check_safety_of_device_overwrite(int fd, const char *device);
@@ -585,7 +586,7 @@ int main(int argc, char *argv[])
585586
struct nilfs_segment_info *si;
586587
struct stat statbuf;
587588
const char *device;
588-
int fd;
589+
int fd, ret;
589590

590591
parse_options(argc, argv);
591592
device = argv[optind];
@@ -598,10 +599,18 @@ int main(int argc, char *argv[])
598599
if (cflag)
599600
disk_scan(device); /* check the block device */
600601

602+
ret = check_mount(device);
603+
if (ret < 0)
604+
perr("Error checking mount status of %s: %s", device,
605+
strerror(errno));
606+
if (ret > 0)
607+
perr("Error: %s is currently mounted. You cannot make a filesystem on this device.",
608+
device);
609+
601610
fd = open(device, O_RDWR);
602611
if (fd < 0)
603612
perr("Error: cannot open device: %s", device);
604-
check_mount(fd, device);
613+
605614
check_safety_of_device_overwrite(fd, device);
606615

607616
init_disk_layout(di, fd, device);
@@ -696,28 +705,6 @@ static void disk_scan(const char *device)
696705
}
697706
}
698707

699-
static void check_mount(int fd, const char *device)
700-
{
701-
FILE *fp;
702-
char line[LINE_BUFFER_SIZE];
703-
704-
fp = fopen(_PATH_MOUNTED, "r");
705-
if (fp == NULL) {
706-
close(fd);
707-
perr("Error: cannot open %s!", _PATH_MOUNTED);
708-
}
709-
710-
while (fgets(line, LINE_BUFFER_SIZE, fp) != NULL) {
711-
if (strncmp(strtok(line, " "), device, strlen(device)) == 0) {
712-
fclose(fp);
713-
close(fd);
714-
perr("Error: %s is currently mounted. You cannot make a filesystem on this device.",
715-
device);
716-
}
717-
}
718-
fclose(fp);
719-
}
720-
721708
#if HAVE_LIBBLKID
722709
static void check_safety_of_device_overwrite(int fd, const char *device)
723710
{

0 commit comments

Comments
 (0)