Blame clear-caches-when-opening-and-closing-devices.patch

a9cea1
diff -ur btrfs-progs-0.20.rc1.20121017git91d9eec.old/disk-io.c btrfs-progs-0.20.rc1.20121017git91d9eec/disk-io.c
a9cea1
--- btrfs-progs-0.20.rc1.20121017git91d9eec.old/disk-io.c	2013-02-13 11:04:05.361458311 +0000
a9cea1
+++ btrfs-progs-0.20.rc1.20121017git91d9eec/disk-io.c	2013-02-13 11:04:22.513377018 +0000
a9cea1
@@ -634,6 +634,9 @@
a9cea1
 	if (sb_bytenr == 0)
a9cea1
 		sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
a9cea1
 
a9cea1
+	/* try to drop all the caches */
a9cea1
+	posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED);
a9cea1
+
a9cea1
 	ret = btrfs_scan_one_device(fp, path, &fs_devices,
a9cea1
 				    &total_devs, sb_bytenr);
a9cea1
 
a9cea1
@@ -1083,6 +1086,10 @@
a9cea1
 	list = &fs_info->fs_devices->devices;
a9cea1
 	list_for_each_safe(next, tmp, list) {
a9cea1
 		device = list_entry(next, struct btrfs_device, dev_list);
a9cea1
+		if (device->fd) {
a9cea1
+			fsync(device->fd);
a9cea1
+			posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED);
a9cea1
+		}
a9cea1
 		close(device->fd);
a9cea1
 		list_del(&device->dev_list);
a9cea1
 		free(device->name);
a9cea1
diff -ur btrfs-progs-0.20.rc1.20121017git91d9eec.old/disk-io.c.orig btrfs-progs-0.20.rc1.20121017git91d9eec/disk-io.c.orig
a9cea1
--- btrfs-progs-0.20.rc1.20121017git91d9eec.old/disk-io.c.orig	2012-10-05 01:35:31.000000000 +0100
a9cea1
+++ btrfs-progs-0.20.rc1.20121017git91d9eec/disk-io.c.orig	2013-02-13 11:04:05.361458311 +0000
a9cea1
@@ -458,8 +458,10 @@
a9cea1
 	u64 blocknr = btrfs_super_log_root(disk_super);
a9cea1
 	struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
a9cea1
 
a9cea1
-	if (blocknr == 0)
a9cea1
+	if (blocknr == 0) {
a9cea1
+		free(log_root);
a9cea1
 		return 0;
a9cea1
+	}
a9cea1
 
a9cea1
 	blocksize = btrfs_level_size(tree_root,
a9cea1
 			     btrfs_super_log_root_level(disk_super));
a9cea1
@@ -622,7 +624,7 @@
a9cea1
 	struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
a9cea1
 	struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
a9cea1
 	struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
a9cea1
-	struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
a9cea1
+	struct btrfs_fs_info *fs_info = malloc(sizeof(struct btrfs_fs_info));
a9cea1
 	int ret;
a9cea1
 	struct btrfs_super_block *disk_super;
a9cea1
 	struct btrfs_fs_devices *fs_devices = NULL;
a9cea1
@@ -646,7 +648,7 @@
a9cea1
 			goto out;
a9cea1
 	}
a9cea1
 
a9cea1
-	memset(fs_info, 0, sizeof(*fs_info));
a9cea1
+	memset(fs_info, 0, sizeof(struct btrfs_fs_info));
a9cea1
 	fs_info->tree_root = tree_root;
a9cea1
 	fs_info->extent_root = extent_root;
a9cea1
 	fs_info->chunk_root = chunk_root;
a9cea1
@@ -1075,15 +1077,19 @@
a9cea1
 {
a9cea1
 	struct list_head *list;
a9cea1
 	struct list_head *next;
a9cea1
+	struct list_head *tmp;
a9cea1
 	struct btrfs_device *device;
a9cea1
 
a9cea1
-	return 0;
a9cea1
-
a9cea1
 	list = &fs_info->fs_devices->devices;
a9cea1
-	list_for_each(next, list) {
a9cea1
+	list_for_each_safe(next, tmp, list) {
a9cea1
 		device = list_entry(next, struct btrfs_device, dev_list);
a9cea1
 		close(device->fd);
a9cea1
+		list_del(&device->dev_list);
a9cea1
+		free(device->name);
a9cea1
+		free(device->label);
a9cea1
+		free(device);
a9cea1
 	}
a9cea1
+	free(fs_info->fs_devices);
a9cea1
 	return 0;
a9cea1
 }
a9cea1
 
a9cea1
@@ -1133,12 +1139,14 @@
a9cea1
 	extent_io_tree_cleanup(&fs_info->pinned_extents);
a9cea1
 	extent_io_tree_cleanup(&fs_info->pending_del);
a9cea1
 	extent_io_tree_cleanup(&fs_info->extent_ins);
a9cea1
+	btrfs_mapping_tree_free(&fs_info->mapping_tree);
a9cea1
 
a9cea1
 	free(fs_info->tree_root);
a9cea1
 	free(fs_info->extent_root);
a9cea1
 	free(fs_info->chunk_root);
a9cea1
 	free(fs_info->dev_root);
a9cea1
 	free(fs_info->csum_root);
a9cea1
+	free(fs_info->log_root_tree);
a9cea1
 	free(fs_info);
a9cea1
 
a9cea1
 	return 0;
a9cea1
diff -ur btrfs-progs-0.20.rc1.20121017git91d9eec.old/volumes.c btrfs-progs-0.20.rc1.20121017git91d9eec/volumes.c
a9cea1
--- btrfs-progs-0.20.rc1.20121017git91d9eec.old/volumes.c	2013-02-13 11:04:05.411458074 +0000
a9cea1
+++ btrfs-progs-0.20.rc1.20121017git91d9eec/volumes.c	2013-02-13 11:04:22.514377013 +0000
a9cea1
@@ -176,6 +176,8 @@
a9cea1
 			goto fail;
a9cea1
 		}
a9cea1
 
a9cea1
+		posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
a9cea1
+
a9cea1
 		if (device->devid == fs_devices->latest_devid)
a9cea1
 			fs_devices->latest_bdev = fd;
a9cea1
 		if (device->devid == fs_devices->lowest_devid)