Btrfs-progs: don't close the file descriptor 0 when closing a device

As we know, the file descriptor 0 is a special number, so we shouldn't
use it to initialize the file descriptor of the devices, or we might
close this special file descriptor by mistake when we close the devices.
"-1" is a better choice.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
master
Miao Xie 2013-07-03 21:25:10 +08:00 committed by Chris Mason
parent 32a8c1dd90
commit 0daa219ee1
3 changed files with 12 additions and 5 deletions

View File

@ -76,7 +76,10 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)
list = &fs_info->fs_devices->devices;
list_for_each(next, list) {
device = list_entry(next, struct btrfs_device, dev_list);
close(device->fd);
if (device->fd != -1) {
close(device->fd);
device->fd = -1;
}
}
return 0;
}

View File

@ -1270,12 +1270,13 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)
while (!list_empty(list)) {
device = list_entry(list->next, struct btrfs_device, dev_list);
list_del_init(&device->dev_list);
if (device->fd) {
if (device->fd != -1) {
fsync(device->fd);
if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
fprintf(stderr, "Warning, could not drop caches\n");
close(device->fd);
device->fd = -1;
}
close(device->fd);
kfree(device->name);
kfree(device->label);
kfree(device);

View File

@ -116,6 +116,7 @@ static int device_list_add(const char *path,
/* we can safely leave the fs_devices entry around */
return -ENOMEM;
}
device->fd = -1;
device->devid = devid;
memcpy(device->uuid, disk_super->dev_item.uuid,
BTRFS_UUID_SIZE);
@ -161,8 +162,10 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
again:
list_for_each(cur, &fs_devices->devices) {
device = list_entry(cur, struct btrfs_device, dev_list);
close(device->fd);
device->fd = -1;
if (device->fd != -1) {
close(device->fd);
device->fd = -1;
}
device->writeable = 0;
}