diff options
author | Chris Mason <chris.mason@oracle.com> | 2008-09-05 16:13:11 -0400 |
---|---|---|
committer | Chris Mason <chris.mason@oracle.com> | 2008-09-25 11:04:07 -0400 |
commit | e02119d5a7b4396c5a872582fddc8bd6d305a70a (patch) | |
tree | 825efe2a79dbca8d61256183f3526a5b5dc40dc6 /fs | |
parent | a1b32a5932cfac7c38b442582285f3da2a09dfd8 (diff) |
Btrfs: Add a write ahead tree log to optimize synchronous operations
File syncs and directory syncs are optimized by copying their
items into a special (copy-on-write) log tree. There is one log tree per
subvolume and the btrfs super block points to a tree of log tree roots.
After a crash, items are copied out of the log tree and back into the
subvolume. See tree-log.c for all the details.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/btrfs/Makefile | 3 | ||||
-rw-r--r-- | fs/btrfs/btrfs_inode.h | 8 | ||||
-rw-r--r-- | fs/btrfs/compat.h | 15 | ||||
-rw-r--r-- | fs/btrfs/ctree.c | 73 | ||||
-rw-r--r-- | fs/btrfs/ctree.h | 72 | ||||
-rw-r--r-- | fs/btrfs/dir-item.c | 3 | ||||
-rw-r--r-- | fs/btrfs/disk-io.c | 138 | ||||
-rw-r--r-- | fs/btrfs/disk-io.h | 8 | ||||
-rw-r--r-- | fs/btrfs/extent-tree.c | 93 | ||||
-rw-r--r-- | fs/btrfs/file.c | 39 | ||||
-rw-r--r-- | fs/btrfs/inode.c | 261 | ||||
-rw-r--r-- | fs/btrfs/root-tree.c | 5 | ||||
-rw-r--r-- | fs/btrfs/transaction.c | 45 | ||||
-rw-r--r-- | fs/btrfs/transaction.h | 1 | ||||
-rw-r--r-- | fs/btrfs/tree-defrag.c | 4 | ||||
-rw-r--r-- | fs/btrfs/tree-log.c | 2804 | ||||
-rw-r--r-- | fs/btrfs/tree-log.h | 41 |
17 files changed, 3408 insertions, 205 deletions
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile index 3a01065d4ef1..b7addbfd8c22 100644 --- a/fs/btrfs/Makefile +++ b/fs/btrfs/Makefile | |||
@@ -7,8 +7,7 @@ btrfs-y := super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ | |||
7 | transaction.o bit-radix.o inode.o file.o tree-defrag.o \ | 7 | transaction.o bit-radix.o inode.o file.o tree-defrag.o \ |
8 | extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \ | 8 | extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \ |
9 | extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ | 9 | extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ |
10 | ref-cache.o acl.o export.o | 10 | ref-cache.o export.o tree-log.o acl.o |
11 | |||
12 | else | 11 | else |
13 | 12 | ||
14 | # Normal Makefile | 13 | # Normal Makefile |
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h index 111f90524ae1..fcc8cf27e906 100644 --- a/fs/btrfs/btrfs_inode.h +++ b/fs/btrfs/btrfs_inode.h | |||
@@ -33,6 +33,7 @@ struct btrfs_inode { | |||
33 | struct extent_io_tree io_failure_tree; | 33 | struct extent_io_tree io_failure_tree; |
34 | struct mutex csum_mutex; | 34 | struct mutex csum_mutex; |
35 | struct mutex extent_mutex; | 35 | struct mutex extent_mutex; |
36 | struct mutex log_mutex; | ||
36 | struct inode vfs_inode; | 37 | struct inode vfs_inode; |
37 | struct btrfs_ordered_inode_tree ordered_tree; | 38 | struct btrfs_ordered_inode_tree ordered_tree; |
38 | 39 | ||
@@ -44,10 +45,17 @@ struct btrfs_inode { | |||
44 | 45 | ||
45 | struct list_head delalloc_inodes; | 46 | struct list_head delalloc_inodes; |
46 | 47 | ||
48 | /* full 64 bit generation number */ | ||
49 | u64 generation; | ||
50 | |||
47 | /* | 51 | /* |
48 | * transid of the trans_handle that last modified this inode | 52 | * transid of the trans_handle that last modified this inode |
49 | */ | 53 | */ |
50 | u64 last_trans; | 54 | u64 last_trans; |
55 | /* | ||
56 | * transid that last logged this inode | ||
57 | */ | ||
58 | u64 logged_trans; | ||
51 | u64 delalloc_bytes; | 59 | u64 delalloc_bytes; |
52 | u64 disk_i_size; | 60 | u64 disk_i_size; |
53 | u32 flags; | 61 | u32 flags; |
diff --git a/fs/btrfs/compat.h b/fs/btrfs/compat.h index d45fb37887bc..b0ed1887d9b1 100644 --- a/fs/btrfs/compat.h +++ b/fs/btrfs/compat.h | |||
@@ -22,6 +22,21 @@ static inline struct dentry *d_obtain_alias(struct inode *inode) | |||
22 | } | 22 | } |
23 | #endif | 23 | #endif |
24 | 24 | ||
25 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) | ||
26 | static inline void btrfs_drop_nlink(struct inode *inode) | ||
27 | { | ||
28 | inode->i_nlink--; | ||
29 | } | ||
30 | |||
31 | static inline void btrfs_inc_nlink(struct inode *inode) | ||
32 | { | ||
33 | inode->i_nlink++; | ||
34 | } | ||
35 | #else | ||
36 | # define btrfs_drop_nlink(inode) drop_nlink(inode) | ||
37 | # define btrfs_inc_nlink(inode) inc_nlink(inode) | ||
38 | #endif | ||
39 | |||
25 | /* | 40 | /* |
26 | * Even if AppArmor isn't enabled, it still has different prototypes. | 41 | * Even if AppArmor isn't enabled, it still has different prototypes. |
27 | * Add more distro/version pairs here to declare which has AppArmor applied. | 42 | * Add more distro/version pairs here to declare which has AppArmor applied. |
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index 7114faafa9d4..579124043d9b 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c | |||
@@ -60,7 +60,7 @@ void btrfs_free_path(struct btrfs_path *p) | |||
60 | kmem_cache_free(btrfs_path_cachep, p); | 60 | kmem_cache_free(btrfs_path_cachep, p); |
61 | } | 61 | } |
62 | 62 | ||
63 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) | 63 | void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) |
64 | { | 64 | { |
65 | int i; | 65 | int i; |
66 | 66 | ||
@@ -176,7 +176,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, | |||
176 | return 0; | 176 | return 0; |
177 | } | 177 | } |
178 | 178 | ||
179 | int __btrfs_cow_block(struct btrfs_trans_handle *trans, | 179 | int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans, |
180 | struct btrfs_root *root, | 180 | struct btrfs_root *root, |
181 | struct extent_buffer *buf, | 181 | struct extent_buffer *buf, |
182 | struct extent_buffer *parent, int parent_slot, | 182 | struct extent_buffer *parent, int parent_slot, |
@@ -294,7 +294,7 @@ int __btrfs_cow_block(struct btrfs_trans_handle *trans, | |||
294 | return 0; | 294 | return 0; |
295 | } | 295 | } |
296 | 296 | ||
297 | int btrfs_cow_block(struct btrfs_trans_handle *trans, | 297 | int noinline btrfs_cow_block(struct btrfs_trans_handle *trans, |
298 | struct btrfs_root *root, struct extent_buffer *buf, | 298 | struct btrfs_root *root, struct extent_buffer *buf, |
299 | struct extent_buffer *parent, int parent_slot, | 299 | struct extent_buffer *parent, int parent_slot, |
300 | struct extent_buffer **cow_ret, u64 prealloc_dest) | 300 | struct extent_buffer **cow_ret, u64 prealloc_dest) |
@@ -677,9 +677,10 @@ static int noinline check_block(struct btrfs_root *root, | |||
677 | * | 677 | * |
678 | * slot may point to max if the key is bigger than all of the keys | 678 | * slot may point to max if the key is bigger than all of the keys |
679 | */ | 679 | */ |
680 | static int generic_bin_search(struct extent_buffer *eb, unsigned long p, | 680 | static noinline int generic_bin_search(struct extent_buffer *eb, |
681 | int item_size, struct btrfs_key *key, | 681 | unsigned long p, |
682 | int max, int *slot) | 682 | int item_size, struct btrfs_key *key, |
683 | int max, int *slot) | ||
683 | { | 684 | { |
684 | int low = 0; | 685 | int low = 0; |
685 | int high = max; | 686 | int high = max; |
@@ -765,7 +766,7 @@ static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, | |||
765 | return -1; | 766 | return -1; |
766 | } | 767 | } |
767 | 768 | ||
768 | static struct extent_buffer *read_node_slot(struct btrfs_root *root, | 769 | static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, |
769 | struct extent_buffer *parent, int slot) | 770 | struct extent_buffer *parent, int slot) |
770 | { | 771 | { |
771 | int level = btrfs_header_level(parent); | 772 | int level = btrfs_header_level(parent); |
@@ -781,7 +782,7 @@ static struct extent_buffer *read_node_slot(struct btrfs_root *root, | |||
781 | btrfs_node_ptr_generation(parent, slot)); | 782 | btrfs_node_ptr_generation(parent, slot)); |
782 | } | 783 | } |
783 | 784 | ||
784 | static int balance_level(struct btrfs_trans_handle *trans, | 785 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
785 | struct btrfs_root *root, | 786 | struct btrfs_root *root, |
786 | struct btrfs_path *path, int level) | 787 | struct btrfs_path *path, int level) |
787 | { | 788 | { |
@@ -1128,8 +1129,9 @@ static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans, | |||
1128 | /* | 1129 | /* |
1129 | * readahead one full node of leaves | 1130 | * readahead one full node of leaves |
1130 | */ | 1131 | */ |
1131 | static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, | 1132 | static noinline void reada_for_search(struct btrfs_root *root, |
1132 | int level, int slot, u64 objectid) | 1133 | struct btrfs_path *path, |
1134 | int level, int slot, u64 objectid) | ||
1133 | { | 1135 | { |
1134 | struct extent_buffer *node; | 1136 | struct extent_buffer *node; |
1135 | struct btrfs_disk_key disk_key; | 1137 | struct btrfs_disk_key disk_key; |
@@ -1201,7 +1203,8 @@ static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, | |||
1201 | } | 1203 | } |
1202 | } | 1204 | } |
1203 | 1205 | ||
1204 | static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock) | 1206 | static noinline void unlock_up(struct btrfs_path *path, int level, |
1207 | int lowest_unlock) | ||
1205 | { | 1208 | { |
1206 | int i; | 1209 | int i; |
1207 | int skip_level = level; | 1210 | int skip_level = level; |
@@ -1759,8 +1762,9 @@ static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root | |||
1759 | * | 1762 | * |
1760 | * returns 0 on success and < 0 on failure | 1763 | * returns 0 on success and < 0 on failure |
1761 | */ | 1764 | */ |
1762 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root | 1765 | static noinline int split_node(struct btrfs_trans_handle *trans, |
1763 | *root, struct btrfs_path *path, int level) | 1766 | struct btrfs_root *root, |
1767 | struct btrfs_path *path, int level) | ||
1764 | { | 1768 | { |
1765 | u64 root_gen; | 1769 | u64 root_gen; |
1766 | struct extent_buffer *c; | 1770 | struct extent_buffer *c; |
@@ -1874,7 +1878,8 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr) | |||
1874 | * the start of the leaf data. IOW, how much room | 1878 | * the start of the leaf data. IOW, how much room |
1875 | * the leaf has left for both items and data | 1879 | * the leaf has left for both items and data |
1876 | */ | 1880 | */ |
1877 | int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf) | 1881 | int noinline btrfs_leaf_free_space(struct btrfs_root *root, |
1882 | struct extent_buffer *leaf) | ||
1878 | { | 1883 | { |
1879 | int nritems = btrfs_header_nritems(leaf); | 1884 | int nritems = btrfs_header_nritems(leaf); |
1880 | int ret; | 1885 | int ret; |
@@ -2283,9 +2288,11 @@ out: | |||
2283 | * | 2288 | * |
2284 | * returns 0 if all went well and < 0 on failure. | 2289 | * returns 0 if all went well and < 0 on failure. |
2285 | */ | 2290 | */ |
2286 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root | 2291 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
2287 | *root, struct btrfs_key *ins_key, | 2292 | struct btrfs_root *root, |
2288 | struct btrfs_path *path, int data_size, int extend) | 2293 | struct btrfs_key *ins_key, |
2294 | struct btrfs_path *path, int data_size, | ||
2295 | int extend) | ||
2289 | { | 2296 | { |
2290 | u64 root_gen; | 2297 | u64 root_gen; |
2291 | struct extent_buffer *l; | 2298 | struct extent_buffer *l; |
@@ -3079,6 +3086,7 @@ int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) | |||
3079 | * was nothing in the tree that matched the search criteria. | 3086 | * was nothing in the tree that matched the search criteria. |
3080 | */ | 3087 | */ |
3081 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, | 3088 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, |
3089 | struct btrfs_key *max_key, | ||
3082 | struct btrfs_path *path, int cache_only, | 3090 | struct btrfs_path *path, int cache_only, |
3083 | u64 min_trans) | 3091 | u64 min_trans) |
3084 | { | 3092 | { |
@@ -3093,6 +3101,7 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, | |||
3093 | again: | 3101 | again: |
3094 | cur = btrfs_lock_root_node(root); | 3102 | cur = btrfs_lock_root_node(root); |
3095 | level = btrfs_header_level(cur); | 3103 | level = btrfs_header_level(cur); |
3104 | WARN_ON(path->nodes[level]); | ||
3096 | path->nodes[level] = cur; | 3105 | path->nodes[level] = cur; |
3097 | path->locks[level] = 1; | 3106 | path->locks[level] = 1; |
3098 | 3107 | ||
@@ -3107,6 +3116,8 @@ again: | |||
3107 | 3116 | ||
3108 | /* at level = 0, we're done, setup the path and exit */ | 3117 | /* at level = 0, we're done, setup the path and exit */ |
3109 | if (level == 0) { | 3118 | if (level == 0) { |
3119 | if (slot >= nritems) | ||
3120 | goto find_next_key; | ||
3110 | ret = 0; | 3121 | ret = 0; |
3111 | path->slots[level] = slot; | 3122 | path->slots[level] = slot; |
3112 | btrfs_item_key_to_cpu(cur, &found_key, slot); | 3123 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
@@ -3123,6 +3134,8 @@ again: | |||
3123 | u64 blockptr; | 3134 | u64 blockptr; |
3124 | u64 gen; | 3135 | u64 gen; |
3125 | struct extent_buffer *tmp; | 3136 | struct extent_buffer *tmp; |
3137 | struct btrfs_disk_key disk_key; | ||
3138 | |||
3126 | blockptr = btrfs_node_blockptr(cur, slot); | 3139 | blockptr = btrfs_node_blockptr(cur, slot); |
3127 | gen = btrfs_node_ptr_generation(cur, slot); | 3140 | gen = btrfs_node_ptr_generation(cur, slot); |
3128 | if (gen < min_trans) { | 3141 | if (gen < min_trans) { |
@@ -3132,6 +3145,14 @@ again: | |||
3132 | if (!cache_only) | 3145 | if (!cache_only) |
3133 | break; | 3146 | break; |
3134 | 3147 | ||
3148 | if (max_key) { | ||
3149 | btrfs_node_key(cur, &disk_key, slot); | ||
3150 | if (comp_keys(&disk_key, max_key) >= 0) { | ||
3151 | ret = 1; | ||
3152 | goto out; | ||
3153 | } | ||
3154 | } | ||
3155 | |||
3135 | tmp = btrfs_find_tree_block(root, blockptr, | 3156 | tmp = btrfs_find_tree_block(root, blockptr, |
3136 | btrfs_level_size(root, level - 1)); | 3157 | btrfs_level_size(root, level - 1)); |
3137 | 3158 | ||
@@ -3143,14 +3164,16 @@ again: | |||
3143 | free_extent_buffer(tmp); | 3164 | free_extent_buffer(tmp); |
3144 | slot++; | 3165 | slot++; |
3145 | } | 3166 | } |
3167 | find_next_key: | ||
3146 | /* | 3168 | /* |
3147 | * we didn't find a candidate key in this node, walk forward | 3169 | * we didn't find a candidate key in this node, walk forward |
3148 | * and find another one | 3170 | * and find another one |
3149 | */ | 3171 | */ |
3150 | if (slot >= nritems) { | 3172 | if (slot >= nritems) { |
3151 | ret = btrfs_find_next_key(root, path, min_key, level, | 3173 | path->slots[level] = slot; |
3174 | sret = btrfs_find_next_key(root, path, min_key, level, | ||
3152 | cache_only, min_trans); | 3175 | cache_only, min_trans); |
3153 | if (ret == 0) { | 3176 | if (sret == 0) { |
3154 | btrfs_release_path(root, path); | 3177 | btrfs_release_path(root, path); |
3155 | goto again; | 3178 | goto again; |
3156 | } else { | 3179 | } else { |
@@ -3351,6 +3374,7 @@ int btrfs_previous_item(struct btrfs_root *root, | |||
3351 | { | 3374 | { |
3352 | struct btrfs_key found_key; | 3375 | struct btrfs_key found_key; |
3353 | struct extent_buffer *leaf; | 3376 | struct extent_buffer *leaf; |
3377 | u32 nritems; | ||
3354 | int ret; | 3378 | int ret; |
3355 | 3379 | ||
3356 | while(1) { | 3380 | while(1) { |
@@ -3362,9 +3386,20 @@ int btrfs_previous_item(struct btrfs_root *root, | |||
3362 | path->slots[0]--; | 3386 | path->slots[0]--; |
3363 | } | 3387 | } |
3364 | leaf = path->nodes[0]; | 3388 | leaf = path->nodes[0]; |
3389 | nritems = btrfs_header_nritems(leaf); | ||
3390 | if (nritems == 0) | ||
3391 | return 1; | ||
3392 | if (path->slots[0] == nritems) | ||
3393 | path->slots[0]--; | ||
3394 | |||
3365 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | 3395 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3366 | if (found_key.type == type) | 3396 | if (found_key.type == type) |
3367 | return 0; | 3397 | return 0; |
3398 | if (found_key.objectid < min_objectid) | ||
3399 | break; | ||
3400 | if (found_key.objectid == min_objectid && | ||
3401 | found_key.type < type) | ||
3402 | break; | ||
3368 | } | 3403 | } |
3369 | return 1; | 3404 | return 1; |
3370 | } | 3405 | } |
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index b305ae7e10b0..6532b60683ef 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h | |||
@@ -77,6 +77,10 @@ struct btrfs_ordered_sum; | |||
77 | /* orhpan objectid for tracking unlinked/truncated files */ | 77 | /* orhpan objectid for tracking unlinked/truncated files */ |
78 | #define BTRFS_ORPHAN_OBJECTID -5ULL | 78 | #define BTRFS_ORPHAN_OBJECTID -5ULL |
79 | 79 | ||
80 | /* does write ahead logging to speed up fsyncs */ | ||
81 | #define BTRFS_TREE_LOG_OBJECTID -6ULL | ||
82 | #define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL | ||
83 | |||
80 | /* | 84 | /* |
81 | * All files have objectids higher than this. | 85 | * All files have objectids higher than this. |
82 | */ | 86 | */ |
@@ -276,6 +280,7 @@ struct btrfs_super_block { | |||
276 | __le64 generation; | 280 | __le64 generation; |
277 | __le64 root; | 281 | __le64 root; |
278 | __le64 chunk_root; | 282 | __le64 chunk_root; |
283 | __le64 log_root; | ||
279 | __le64 total_bytes; | 284 | __le64 total_bytes; |
280 | __le64 bytes_used; | 285 | __le64 bytes_used; |
281 | __le64 root_dir_objectid; | 286 | __le64 root_dir_objectid; |
@@ -287,6 +292,7 @@ struct btrfs_super_block { | |||
287 | __le32 sys_chunk_array_size; | 292 | __le32 sys_chunk_array_size; |
288 | u8 root_level; | 293 | u8 root_level; |
289 | u8 chunk_root_level; | 294 | u8 chunk_root_level; |
295 | u8 log_root_level; | ||
290 | struct btrfs_dev_item dev_item; | 296 | struct btrfs_dev_item dev_item; |
291 | char label[BTRFS_LABEL_SIZE]; | 297 | char label[BTRFS_LABEL_SIZE]; |
292 | u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE]; | 298 | u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE]; |
@@ -392,7 +398,10 @@ struct btrfs_timespec { | |||
392 | * make a new item type | 398 | * make a new item type |
393 | */ | 399 | */ |
394 | struct btrfs_inode_item { | 400 | struct btrfs_inode_item { |
401 | /* nfs style generation number */ | ||
395 | __le64 generation; | 402 | __le64 generation; |
403 | /* transid that last touched this inode */ | ||
404 | __le64 transid; | ||
396 | __le64 size; | 405 | __le64 size; |
397 | __le64 nblocks; | 406 | __le64 nblocks; |
398 | __le64 block_group; | 407 | __le64 block_group; |
@@ -409,8 +418,13 @@ struct btrfs_inode_item { | |||
409 | struct btrfs_timespec otime; | 418 | struct btrfs_timespec otime; |
410 | } __attribute__ ((__packed__)); | 419 | } __attribute__ ((__packed__)); |
411 | 420 | ||
421 | struct btrfs_dir_log_item { | ||
422 | __le64 end; | ||
423 | } __attribute__ ((__packed__)); | ||
424 | |||
412 | struct btrfs_dir_item { | 425 | struct btrfs_dir_item { |
413 | struct btrfs_disk_key location; | 426 | struct btrfs_disk_key location; |
427 | __le64 transid; | ||
414 | __le16 data_len; | 428 | __le16 data_len; |
415 | __le16 name_len; | 429 | __le16 name_len; |
416 | u8 type; | 430 | u8 type; |
@@ -505,6 +519,9 @@ struct btrfs_fs_info { | |||
505 | struct btrfs_root *tree_root; | 519 | struct btrfs_root *tree_root; |
506 | struct btrfs_root *chunk_root; | 520 | struct btrfs_root *chunk_root; |
507 | struct btrfs_root *dev_root; | 521 | struct btrfs_root *dev_root; |
522 | |||
523 | /* the log root tree is a directory of all the other log roots */ | ||
524 | struct btrfs_root *log_root_tree; | ||
508 | struct radix_tree_root fs_roots_radix; | 525 | struct radix_tree_root fs_roots_radix; |
509 | 526 | ||
510 | struct extent_io_tree free_space_cache; | 527 | struct extent_io_tree free_space_cache; |
@@ -518,6 +535,7 @@ struct btrfs_fs_info { | |||
518 | 535 | ||
519 | u64 generation; | 536 | u64 generation; |
520 | u64 last_trans_committed; | 537 | u64 last_trans_committed; |
538 | u64 last_trans_new_blockgroup; | ||
521 | u64 open_ioctl_trans; | 539 | u64 open_ioctl_trans; |
522 | unsigned long mount_opt; | 540 | unsigned long mount_opt; |
523 | u64 max_extent; | 541 | u64 max_extent; |
@@ -527,6 +545,9 @@ struct btrfs_fs_info { | |||
527 | wait_queue_head_t transaction_throttle; | 545 | wait_queue_head_t transaction_throttle; |
528 | wait_queue_head_t transaction_wait; | 546 | wait_queue_head_t transaction_wait; |
529 | wait_queue_head_t async_submit_wait; | 547 | wait_queue_head_t async_submit_wait; |
548 | |||
549 | wait_queue_head_t tree_log_wait; | ||
550 | |||
530 | struct btrfs_super_block super_copy; | 551 | struct btrfs_super_block super_copy; |
531 | struct btrfs_super_block super_for_commit; | 552 | struct btrfs_super_block super_for_commit; |
532 | struct block_device *__bdev; | 553 | struct block_device *__bdev; |
@@ -535,6 +556,7 @@ struct btrfs_fs_info { | |||
535 | struct backing_dev_info bdi; | 556 | struct backing_dev_info bdi; |
536 | spinlock_t hash_lock; | 557 | spinlock_t hash_lock; |
537 | struct mutex trans_mutex; | 558 | struct mutex trans_mutex; |
559 | struct mutex tree_log_mutex; | ||
538 | struct mutex transaction_kthread_mutex; | 560 | struct mutex transaction_kthread_mutex; |
539 | struct mutex cleaner_mutex; | 561 | struct mutex cleaner_mutex; |
540 | struct mutex alloc_mutex; | 562 | struct mutex alloc_mutex; |
@@ -544,8 +566,13 @@ struct btrfs_fs_info { | |||
544 | struct list_head trans_list; | 566 | struct list_head trans_list; |
545 | struct list_head hashers; | 567 | struct list_head hashers; |
546 | struct list_head dead_roots; | 568 | struct list_head dead_roots; |
569 | |||
547 | atomic_t nr_async_submits; | 570 | atomic_t nr_async_submits; |
548 | atomic_t nr_async_bios; | 571 | atomic_t nr_async_bios; |
572 | atomic_t tree_log_writers; | ||
573 | atomic_t tree_log_commit; | ||
574 | unsigned long tree_log_batch; | ||
575 | u64 tree_log_transid; | ||
549 | 576 | ||
550 | /* | 577 | /* |
551 | * this is used by the balancing code to wait for all the pending | 578 | * this is used by the balancing code to wait for all the pending |
@@ -583,6 +610,7 @@ struct btrfs_fs_info { | |||
583 | struct completion kobj_unregister; | 610 | struct completion kobj_unregister; |
584 | int do_barriers; | 611 | int do_barriers; |
585 | int closing; | 612 | int closing; |
613 | int log_root_recovering; | ||
586 | atomic_t throttles; | 614 | atomic_t throttles; |
587 | atomic_t throttle_gen; | 615 | atomic_t throttle_gen; |
588 | 616 | ||
@@ -596,6 +624,7 @@ struct btrfs_fs_info { | |||
596 | u64 delalloc_bytes; | 624 | u64 delalloc_bytes; |
597 | u64 last_alloc; | 625 | u64 last_alloc; |
598 | u64 last_data_alloc; | 626 | u64 last_data_alloc; |
627 | u64 last_log_alloc; | ||
599 | 628 | ||
600 | spinlock_t ref_cache_lock; | 629 | spinlock_t ref_cache_lock; |
601 | u64 total_ref_cache_size; | 630 | u64 total_ref_cache_size; |
@@ -632,6 +661,7 @@ struct btrfs_root { | |||
632 | struct btrfs_leaf_ref_tree *ref_tree; | 661 | struct btrfs_leaf_ref_tree *ref_tree; |
633 | struct btrfs_leaf_ref_tree ref_tree_struct; | 662 | struct btrfs_leaf_ref_tree ref_tree_struct; |
634 | struct btrfs_dirty_root *dirty_root; | 663 | struct btrfs_dirty_root *dirty_root; |
664 | struct btrfs_root *log_root; | ||
635 | 665 | ||
636 | struct btrfs_root_item root_item; | 666 | struct btrfs_root_item root_item; |
637 | struct btrfs_key root_key; | 667 | struct btrfs_key root_key; |
@@ -640,6 +670,7 @@ struct btrfs_root { | |||
640 | struct kobject root_kobj; | 670 | struct kobject root_kobj; |
641 | struct completion kobj_unregister; | 671 | struct completion kobj_unregister; |
642 | struct mutex objectid_mutex; | 672 | struct mutex objectid_mutex; |
673 | struct mutex log_mutex; | ||
643 | 674 | ||
644 | u64 objectid; | 675 | u64 objectid; |
645 | u64 last_trans; | 676 | u64 last_trans; |
@@ -692,6 +723,8 @@ struct btrfs_root { | |||
692 | * dir items are the name -> inode pointers in a directory. There is one | 723 | * dir items are the name -> inode pointers in a directory. There is one |
693 | * for every name in a directory. | 724 | * for every name in a directory. |
694 | */ | 725 | */ |
726 | #define BTRFS_DIR_LOG_ITEM_KEY 14 | ||
727 | #define BTRFS_DIR_LOG_INDEX_KEY 15 | ||
695 | #define BTRFS_DIR_ITEM_KEY 16 | 728 | #define BTRFS_DIR_ITEM_KEY 16 |
696 | #define BTRFS_DIR_INDEX_KEY 17 | 729 | #define BTRFS_DIR_INDEX_KEY 17 |
697 | /* | 730 | /* |
@@ -703,7 +736,8 @@ struct btrfs_root { | |||
703 | */ | 736 | */ |
704 | #define BTRFS_CSUM_ITEM_KEY 19 | 737 | #define BTRFS_CSUM_ITEM_KEY 19 |
705 | 738 | ||
706 | /* reserve 20-31 for other file stuff */ | 739 | |
740 | /* reserve 21-31 for other file/dir stuff */ | ||
707 | 741 | ||
708 | /* | 742 | /* |
709 | * root items point to tree roots. There are typically in the root | 743 | * root items point to tree roots. There are typically in the root |
@@ -938,6 +972,7 @@ BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64); | |||
938 | 972 | ||
939 | /* struct btrfs_inode_item */ | 973 | /* struct btrfs_inode_item */ |
940 | BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64); | 974 | BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64); |
975 | BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64); | ||
941 | BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64); | 976 | BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64); |
942 | BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64); | 977 | BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64); |
943 | BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64); | 978 | BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64); |
@@ -1126,10 +1161,13 @@ static inline void btrfs_set_item_key(struct extent_buffer *eb, | |||
1126 | write_eb_member(eb, item, struct btrfs_item, key, disk_key); | 1161 | write_eb_member(eb, item, struct btrfs_item, key, disk_key); |
1127 | } | 1162 | } |
1128 | 1163 | ||
1164 | BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64); | ||
1165 | |||
1129 | /* struct btrfs_dir_item */ | 1166 | /* struct btrfs_dir_item */ |
1130 | BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16); | 1167 | BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16); |
1131 | BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8); | 1168 | BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8); |
1132 | BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16); | 1169 | BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16); |
1170 | BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64); | ||
1133 | 1171 | ||
1134 | static inline void btrfs_dir_item_key(struct extent_buffer *eb, | 1172 | static inline void btrfs_dir_item_key(struct extent_buffer *eb, |
1135 | struct btrfs_dir_item *item, | 1173 | struct btrfs_dir_item *item, |
@@ -1301,7 +1339,11 @@ BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block, | |||
1301 | BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block, | 1339 | BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block, |
1302 | chunk_root, 64); | 1340 | chunk_root, 64); |
1303 | BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block, | 1341 | BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block, |
1304 | chunk_root_level, 64); | 1342 | chunk_root_level, 8); |
1343 | BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block, | ||
1344 | log_root, 64); | ||
1345 | BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block, | ||
1346 | log_root_level, 8); | ||
1305 | BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block, | 1347 | BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block, |
1306 | total_bytes, 64); | 1348 | total_bytes, 64); |
1307 | BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block, | 1349 | BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block, |
@@ -1405,6 +1447,12 @@ static inline struct dentry *fdentry(struct file *file) { | |||
1405 | } | 1447 | } |
1406 | 1448 | ||
1407 | /* extent-tree.c */ | 1449 | /* extent-tree.c */ |
1450 | int btrfs_lookup_extent(struct btrfs_root *root, struct btrfs_path *path, | ||
1451 | u64 start, u64 len); | ||
1452 | int btrfs_update_pinned_extents(struct btrfs_root *root, | ||
1453 | u64 bytenr, u64 num, int pin); | ||
1454 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, | ||
1455 | struct btrfs_root *root, struct extent_buffer *leaf); | ||
1408 | int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans, | 1456 | int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans, |
1409 | struct btrfs_root *root, | 1457 | struct btrfs_root *root, |
1410 | struct btrfs_key *key, u64 bytenr); | 1458 | struct btrfs_key *key, u64 bytenr); |
@@ -1448,6 +1496,11 @@ int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |||
1448 | u64 root_objectid, u64 ref_generation, | 1496 | u64 root_objectid, u64 ref_generation, |
1449 | u64 owner, u64 owner_offset, | 1497 | u64 owner, u64 owner_offset, |
1450 | struct btrfs_key *ins); | 1498 | struct btrfs_key *ins); |
1499 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | ||
1500 | struct btrfs_root *root, | ||
1501 | u64 root_objectid, u64 ref_generation, | ||
1502 | u64 owner, u64 owner_offset, | ||
1503 | struct btrfs_key *ins); | ||
1451 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, | 1504 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
1452 | struct btrfs_root *root, | 1505 | struct btrfs_root *root, |
1453 | u64 num_bytes, u64 min_alloc_size, | 1506 | u64 num_bytes, u64 min_alloc_size, |
@@ -1488,9 +1541,9 @@ int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, | |||
1488 | struct btrfs_key *key, int lowest_level, | 1541 | struct btrfs_key *key, int lowest_level, |
1489 | int cache_only, u64 min_trans); | 1542 | int cache_only, u64 min_trans); |
1490 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, | 1543 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, |
1544 | struct btrfs_key *max_key, | ||
1491 | struct btrfs_path *path, int cache_only, | 1545 | struct btrfs_path *path, int cache_only, |
1492 | u64 min_trans); | 1546 | u64 min_trans); |
1493 | |||
1494 | int btrfs_cow_block(struct btrfs_trans_handle *trans, | 1547 | int btrfs_cow_block(struct btrfs_trans_handle *trans, |
1495 | struct btrfs_root *root, struct extent_buffer *buf, | 1548 | struct btrfs_root *root, struct extent_buffer *buf, |
1496 | struct extent_buffer *parent, int parent_slot, | 1549 | struct extent_buffer *parent, int parent_slot, |
@@ -1656,6 +1709,18 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans, | |||
1656 | #define PageChecked PageFsMisc | 1709 | #define PageChecked PageFsMisc |
1657 | #endif | 1710 | #endif |
1658 | 1711 | ||
1712 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, | ||
1713 | struct btrfs_root *root, | ||
1714 | struct inode *dir, struct inode *inode, | ||
1715 | const char *name, int name_len); | ||
1716 | int btrfs_add_link(struct btrfs_trans_handle *trans, | ||
1717 | struct inode *parent_inode, struct inode *inode, | ||
1718 | const char *name, int name_len, int add_backref, u64 index); | ||
1719 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, | ||
1720 | struct btrfs_root *root, | ||
1721 | struct inode *inode, u64 new_size, | ||
1722 | u32 min_type); | ||
1723 | |||
1659 | int btrfs_start_delalloc_inodes(struct btrfs_root *root); | 1724 | int btrfs_start_delalloc_inodes(struct btrfs_root *root); |
1660 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end); | 1725 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end); |
1661 | int btrfs_writepages(struct address_space *mapping, | 1726 | int btrfs_writepages(struct address_space *mapping, |
@@ -1715,6 +1780,7 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans, | |||
1715 | long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg); | 1780 | long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg); |
1716 | 1781 | ||
1717 | /* file.c */ | 1782 | /* file.c */ |
1783 | int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync); | ||
1718 | int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end); | 1784 | int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end); |
1719 | int btrfs_check_file(struct btrfs_root *root, struct inode *inode); | 1785 | int btrfs_check_file(struct btrfs_root *root, struct inode *inode); |
1720 | extern struct file_operations btrfs_file_operations; | 1786 | extern struct file_operations btrfs_file_operations; |
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c index 125094617fe8..e4f30090d640 100644 --- a/fs/btrfs/dir-item.c +++ b/fs/btrfs/dir-item.c | |||
@@ -96,6 +96,7 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, | |||
96 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); | 96 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
97 | btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR); | 97 | btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR); |
98 | btrfs_set_dir_name_len(leaf, dir_item, name_len); | 98 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
99 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); | ||
99 | btrfs_set_dir_data_len(leaf, dir_item, data_len); | 100 | btrfs_set_dir_data_len(leaf, dir_item, data_len); |
100 | name_ptr = (unsigned long)(dir_item + 1); | 101 | name_ptr = (unsigned long)(dir_item + 1); |
101 | data_ptr = (unsigned long)((char *)name_ptr + name_len); | 102 | data_ptr = (unsigned long)((char *)name_ptr + name_len); |
@@ -142,6 +143,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root | |||
142 | btrfs_set_dir_type(leaf, dir_item, type); | 143 | btrfs_set_dir_type(leaf, dir_item, type); |
143 | btrfs_set_dir_data_len(leaf, dir_item, 0); | 144 | btrfs_set_dir_data_len(leaf, dir_item, 0); |
144 | btrfs_set_dir_name_len(leaf, dir_item, name_len); | 145 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
146 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); | ||
145 | name_ptr = (unsigned long)(dir_item + 1); | 147 | name_ptr = (unsigned long)(dir_item + 1); |
146 | 148 | ||
147 | write_extent_buffer(leaf, name, name_ptr, name_len); | 149 | write_extent_buffer(leaf, name, name_ptr, name_len); |
@@ -169,6 +171,7 @@ second_insert: | |||
169 | btrfs_set_dir_type(leaf, dir_item, type); | 171 | btrfs_set_dir_type(leaf, dir_item, type); |
170 | btrfs_set_dir_data_len(leaf, dir_item, 0); | 172 | btrfs_set_dir_data_len(leaf, dir_item, 0); |
171 | btrfs_set_dir_name_len(leaf, dir_item, name_len); | 173 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
174 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); | ||
172 | name_ptr = (unsigned long)(dir_item + 1); | 175 | name_ptr = (unsigned long)(dir_item + 1); |
173 | write_extent_buffer(leaf, name, name_ptr, name_len); | 176 | write_extent_buffer(leaf, name, name_ptr, name_len); |
174 | btrfs_mark_buffer_dirty(leaf); | 177 | btrfs_mark_buffer_dirty(leaf); |
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 8e7a938bfbc7..a4373db5967a 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c | |||
@@ -41,6 +41,7 @@ | |||
41 | #include "async-thread.h" | 41 | #include "async-thread.h" |
42 | #include "locking.h" | 42 | #include "locking.h" |
43 | #include "ref-cache.h" | 43 | #include "ref-cache.h" |
44 | #include "tree-log.h" | ||
44 | 45 | ||
45 | #if 0 | 46 | #if 0 |
46 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) | 47 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) |
@@ -694,6 +695,18 @@ struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, | |||
694 | } | 695 | } |
695 | 696 | ||
696 | 697 | ||
698 | int btrfs_write_tree_block(struct extent_buffer *buf) | ||
699 | { | ||
700 | return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start, | ||
701 | buf->start + buf->len - 1, WB_SYNC_NONE); | ||
702 | } | ||
703 | |||
704 | int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) | ||
705 | { | ||
706 | return btrfs_wait_on_page_writeback_range(buf->first_page->mapping, | ||
707 | buf->start, buf->start + buf->len -1); | ||
708 | } | ||
709 | |||
697 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, | 710 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, |
698 | u32 blocksize, u64 parent_transid) | 711 | u32 blocksize, u64 parent_transid) |
699 | { | 712 | { |
@@ -732,15 +745,6 @@ int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |||
732 | return 0; | 745 | return 0; |
733 | } | 746 | } |
734 | 747 | ||
735 | int wait_on_tree_block_writeback(struct btrfs_root *root, | ||
736 | struct extent_buffer *buf) | ||
737 | { | ||
738 | struct inode *btree_inode = root->fs_info->btree_inode; | ||
739 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree, | ||
740 | buf); | ||
741 | return 0; | ||
742 | } | ||
743 | |||
744 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, | 748 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
745 | u32 stripesize, struct btrfs_root *root, | 749 | u32 stripesize, struct btrfs_root *root, |
746 | struct btrfs_fs_info *fs_info, | 750 | struct btrfs_fs_info *fs_info, |
@@ -771,6 +775,7 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, | |||
771 | spin_lock_init(&root->node_lock); | 775 | spin_lock_init(&root->node_lock); |
772 | spin_lock_init(&root->list_lock); | 776 | spin_lock_init(&root->list_lock); |
773 | mutex_init(&root->objectid_mutex); | 777 | mutex_init(&root->objectid_mutex); |
778 | mutex_init(&root->log_mutex); | ||
774 | 779 | ||
775 | btrfs_leaf_ref_tree_init(&root->ref_tree_struct); | 780 | btrfs_leaf_ref_tree_init(&root->ref_tree_struct); |
776 | root->ref_tree = &root->ref_tree_struct; | 781 | root->ref_tree = &root->ref_tree_struct; |
@@ -809,11 +814,74 @@ static int find_and_setup_root(struct btrfs_root *tree_root, | |||
809 | return 0; | 814 | return 0; |
810 | } | 815 | } |
811 | 816 | ||
812 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, | 817 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, |
813 | struct btrfs_key *location) | 818 | struct btrfs_fs_info *fs_info) |
819 | { | ||
820 | struct extent_buffer *eb; | ||
821 | int ret; | ||
822 | |||
823 | if (!fs_info->log_root_tree) | ||
824 | return 0; | ||
825 | |||
826 | eb = fs_info->log_root_tree->node; | ||
827 | |||
828 | WARN_ON(btrfs_header_level(eb) != 0); | ||
829 | WARN_ON(btrfs_header_nritems(eb) != 0); | ||
830 | |||
831 | ret = btrfs_free_extent(trans, fs_info->tree_root, | ||
832 | eb->start, eb->len, | ||
833 | BTRFS_TREE_LOG_OBJECTID, 0, 0, 0, 1); | ||
834 | BUG_ON(ret); | ||
835 | |||
836 | free_extent_buffer(eb); | ||
837 | kfree(fs_info->log_root_tree); | ||
838 | fs_info->log_root_tree = NULL; | ||
839 | return 0; | ||
840 | } | ||
841 | |||
842 | int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, | ||
843 | struct btrfs_fs_info *fs_info) | ||
814 | { | 844 | { |
815 | struct btrfs_root *root; | 845 | struct btrfs_root *root; |
816 | struct btrfs_root *tree_root = fs_info->tree_root; | 846 | struct btrfs_root *tree_root = fs_info->tree_root; |
847 | |||
848 | root = kzalloc(sizeof(*root), GFP_NOFS); | ||
849 | if (!root) | ||
850 | return -ENOMEM; | ||
851 | |||
852 | __setup_root(tree_root->nodesize, tree_root->leafsize, | ||
853 | tree_root->sectorsize, tree_root->stripesize, | ||
854 | root, fs_info, BTRFS_TREE_LOG_OBJECTID); | ||
855 | |||
856 | root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; | ||
857 | root->root_key.type = BTRFS_ROOT_ITEM_KEY; | ||
858 | root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; | ||
859 | root->ref_cows = 0; | ||
860 | |||
861 | root->node = btrfs_alloc_free_block(trans, root, root->leafsize, | ||
862 | BTRFS_TREE_LOG_OBJECTID, | ||
863 | 0, 0, 0, 0, 0); | ||
864 | |||
865 | btrfs_set_header_nritems(root->node, 0); | ||
866 | btrfs_set_header_level(root->node, 0); | ||
867 | btrfs_set_header_bytenr(root->node, root->node->start); | ||
868 | btrfs_set_header_generation(root->node, trans->transid); | ||
869 | btrfs_set_header_owner(root->node, BTRFS_TREE_LOG_OBJECTID); | ||
870 | |||
871 | write_extent_buffer(root->node, root->fs_info->fsid, | ||
872 | (unsigned long)btrfs_header_fsid(root->node), | ||
873 | BTRFS_FSID_SIZE); | ||
874 | btrfs_mark_buffer_dirty(root->node); | ||
875 | btrfs_tree_unlock(root->node); | ||
876 | fs_info->log_root_tree = root; | ||
877 | return 0; | ||
878 | } | ||
879 | |||
880 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root, | ||
881 | struct btrfs_key *location) | ||
882 | { | ||
883 | struct btrfs_root *root; | ||
884 | struct btrfs_fs_info *fs_info = tree_root->fs_info; | ||
817 | struct btrfs_path *path; | 885 | struct btrfs_path *path; |
818 | struct extent_buffer *l; | 886 | struct extent_buffer *l; |
819 | u64 highest_inode; | 887 | u64 highest_inode; |
@@ -863,11 +931,13 @@ out: | |||
863 | blocksize, 0); | 931 | blocksize, 0); |
864 | BUG_ON(!root->node); | 932 | BUG_ON(!root->node); |
865 | insert: | 933 | insert: |
866 | root->ref_cows = 1; | 934 | if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { |
867 | ret = btrfs_find_highest_inode(root, &highest_inode); | 935 | root->ref_cows = 1; |
868 | if (ret == 0) { | 936 | ret = btrfs_find_highest_inode(root, &highest_inode); |
869 | root->highest_inode = highest_inode; | 937 | if (ret == 0) { |
870 | root->last_inode_alloc = highest_inode; | 938 | root->highest_inode = highest_inode; |
939 | root->last_inode_alloc = highest_inode; | ||
940 | } | ||
871 | } | 941 | } |
872 | return root; | 942 | return root; |
873 | } | 943 | } |
@@ -907,7 +977,7 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, | |||
907 | if (root) | 977 | if (root) |
908 | return root; | 978 | return root; |
909 | 979 | ||
910 | root = btrfs_read_fs_root_no_radix(fs_info, location); | 980 | root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); |
911 | if (IS_ERR(root)) | 981 | if (IS_ERR(root)) |
912 | return root; | 982 | return root; |
913 | ret = radix_tree_insert(&fs_info->fs_roots_radix, | 983 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
@@ -1250,16 +1320,18 @@ struct btrfs_root *open_ctree(struct super_block *sb, | |||
1250 | u32 blocksize; | 1320 | u32 blocksize; |
1251 | u32 stripesize; | 1321 | u32 stripesize; |
1252 | struct buffer_head *bh; | 1322 | struct buffer_head *bh; |
1253 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), | 1323 | struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root), |
1254 | GFP_NOFS); | 1324 | GFP_NOFS); |
1255 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | 1325 | struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root), |
1256 | GFP_NOFS); | 1326 | GFP_NOFS); |
1257 | struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info), | 1327 | struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info), |
1258 | GFP_NOFS); | 1328 | GFP_NOFS); |
1259 | struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root), | 1329 | struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root), |
1260 | GFP_NOFS); | 1330 | GFP_NOFS); |
1261 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), | 1331 | struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root), |
1262 | GFP_NOFS); | 1332 | GFP_NOFS); |
1333 | struct btrfs_root *log_tree_root; | ||
1334 | |||
1263 | int ret; | 1335 | int ret; |
1264 | int err = -EINVAL; | 1336 | int err = -EINVAL; |
1265 | 1337 | ||
@@ -1343,6 +1415,7 @@ struct btrfs_root *open_ctree(struct super_block *sb, | |||
1343 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); | 1415 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
1344 | 1416 | ||
1345 | mutex_init(&fs_info->trans_mutex); | 1417 | mutex_init(&fs_info->trans_mutex); |
1418 | mutex_init(&fs_info->tree_log_mutex); | ||
1346 | mutex_init(&fs_info->drop_mutex); | 1419 | mutex_init(&fs_info->drop_mutex); |
1347 | mutex_init(&fs_info->alloc_mutex); | 1420 | mutex_init(&fs_info->alloc_mutex); |
1348 | mutex_init(&fs_info->chunk_mutex); | 1421 | mutex_init(&fs_info->chunk_mutex); |
@@ -1352,6 +1425,10 @@ struct btrfs_root *open_ctree(struct super_block *sb, | |||
1352 | init_waitqueue_head(&fs_info->transaction_throttle); | 1425 | init_waitqueue_head(&fs_info->transaction_throttle); |
1353 | init_waitqueue_head(&fs_info->transaction_wait); | 1426 | init_waitqueue_head(&fs_info->transaction_wait); |
1354 | init_waitqueue_head(&fs_info->async_submit_wait); | 1427 | init_waitqueue_head(&fs_info->async_submit_wait); |
1428 | init_waitqueue_head(&fs_info->tree_log_wait); | ||
1429 | atomic_set(&fs_info->tree_log_commit, 0); | ||
1430 | atomic_set(&fs_info->tree_log_writers, 0); | ||
1431 | fs_info->tree_log_transid = 0; | ||
1355 | 1432 | ||
1356 | #if 0 | 1433 | #if 0 |
1357 | ret = add_hasher(fs_info, "crc32c"); | 1434 | ret = add_hasher(fs_info, "crc32c"); |
@@ -1532,7 +1609,26 @@ struct btrfs_root *open_ctree(struct super_block *sb, | |||
1532 | if (!fs_info->transaction_kthread) | 1609 | if (!fs_info->transaction_kthread) |
1533 | goto fail_cleaner; | 1610 | goto fail_cleaner; |
1534 | 1611 | ||
1612 | if (btrfs_super_log_root(disk_super) != 0) { | ||
1613 | u32 blocksize; | ||
1614 | u64 bytenr = btrfs_super_log_root(disk_super); | ||
1615 | |||
1616 | blocksize = | ||
1617 | btrfs_level_size(tree_root, | ||
1618 | btrfs_super_log_root_level(disk_super)); | ||
1535 | 1619 | ||
1620 | log_tree_root = kzalloc(sizeof(struct btrfs_root), | ||
1621 | GFP_NOFS); | ||
1622 | |||
1623 | __setup_root(nodesize, leafsize, sectorsize, stripesize, | ||
1624 | log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); | ||
1625 | |||
1626 | log_tree_root->node = read_tree_block(tree_root, bytenr, | ||
1627 | blocksize, 0); | ||
1628 | ret = btrfs_recover_log_trees(log_tree_root); | ||
1629 | BUG_ON(ret); | ||
1630 | } | ||
1631 | fs_info->last_trans_committed = btrfs_super_generation(disk_super); | ||
1536 | return tree_root; | 1632 | return tree_root; |
1537 | 1633 | ||
1538 | fail_cleaner: | 1634 | fail_cleaner: |
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h index 2562a273ae18..6b6fdc697f31 100644 --- a/fs/btrfs/disk-io.h +++ b/fs/btrfs/disk-io.h | |||
@@ -45,7 +45,7 @@ struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, | |||
45 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, | 45 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
46 | struct btrfs_key *location, | 46 | struct btrfs_key *location, |
47 | const char *name, int namelen); | 47 | const char *name, int namelen); |
48 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, | 48 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root, |
49 | struct btrfs_key *location); | 49 | struct btrfs_key *location); |
50 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, | 50 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
51 | struct btrfs_key *location); | 51 | struct btrfs_key *location); |
@@ -74,4 +74,10 @@ int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, | |||
74 | extent_submit_bio_hook_t *submit_bio_hook); | 74 | extent_submit_bio_hook_t *submit_bio_hook); |
75 | int btrfs_congested_async(struct btrfs_fs_info *info, int iodone); | 75 | int btrfs_congested_async(struct btrfs_fs_info *info, int iodone); |
76 | unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info); | 76 | unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info); |
77 | int btrfs_write_tree_block(struct extent_buffer *buf); | ||
78 | int btrfs_wait_tree_block_writeback(struct extent_buffer *buf); | ||
79 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, | ||
80 | struct btrfs_fs_info *fs_info); | ||
81 | int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, | ||
82 | struct btrfs_fs_info *fs_info); | ||
77 | #endif | 83 | #endif |
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index e63b3b4bed7c..646b9148ca21 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -496,6 +496,23 @@ static int match_extent_ref(struct extent_buffer *leaf, | |||
496 | return ret == 0; | 496 | return ret == 0; |
497 | } | 497 | } |
498 | 498 | ||
499 | /* simple helper to search for an existing extent at a given offset */ | ||
500 | int btrfs_lookup_extent(struct btrfs_root *root, struct btrfs_path *path, | ||
501 | u64 start, u64 len) | ||
502 | { | ||
503 | int ret; | ||
504 | struct btrfs_key key; | ||
505 | |||
506 | maybe_lock_mutex(root); | ||
507 | key.objectid = start; | ||
508 | key.offset = len; | ||
509 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | ||
510 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, | ||
511 | 0, 0); | ||
512 | maybe_unlock_mutex(root); | ||
513 | return ret; | ||
514 | } | ||
515 | |||
499 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, | 516 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
500 | struct btrfs_root *root, | 517 | struct btrfs_root *root, |
501 | struct btrfs_path *path, u64 bytenr, | 518 | struct btrfs_path *path, u64 bytenr, |
@@ -1409,7 +1426,7 @@ static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) | |||
1409 | } | 1426 | } |
1410 | 1427 | ||
1411 | 1428 | ||
1412 | static int update_pinned_extents(struct btrfs_root *root, | 1429 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
1413 | u64 bytenr, u64 num, int pin) | 1430 | u64 bytenr, u64 num, int pin) |
1414 | { | 1431 | { |
1415 | u64 len; | 1432 | u64 len; |
@@ -1492,7 +1509,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |||
1492 | EXTENT_DIRTY); | 1509 | EXTENT_DIRTY); |
1493 | if (ret) | 1510 | if (ret) |
1494 | break; | 1511 | break; |
1495 | update_pinned_extents(root, start, end + 1 - start, 0); | 1512 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
1496 | clear_extent_dirty(unpin, start, end, GFP_NOFS); | 1513 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
1497 | set_extent_dirty(free_space_cache, start, end, GFP_NOFS); | 1514 | set_extent_dirty(free_space_cache, start, end, GFP_NOFS); |
1498 | if (need_resched()) { | 1515 | if (need_resched()) { |
@@ -1538,14 +1555,11 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, | |||
1538 | clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, | 1555 | clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, |
1539 | GFP_NOFS); | 1556 | GFP_NOFS); |
1540 | 1557 | ||
1541 | eb = btrfs_find_tree_block(extent_root, ins.objectid, | 1558 | eb = btrfs_find_create_tree_block(extent_root, ins.objectid, |
1542 | ins.offset); | 1559 | ins.offset); |
1543 | 1560 | ||
1544 | if (!btrfs_buffer_uptodate(eb, trans->transid)) { | 1561 | if (!btrfs_buffer_uptodate(eb, trans->transid)) |
1545 | mutex_unlock(&extent_root->fs_info->alloc_mutex); | ||
1546 | btrfs_read_buffer(eb, trans->transid); | 1562 | btrfs_read_buffer(eb, trans->transid); |
1547 | mutex_lock(&extent_root->fs_info->alloc_mutex); | ||
1548 | } | ||
1549 | 1563 | ||
1550 | btrfs_tree_lock(eb); | 1564 | btrfs_tree_lock(eb); |
1551 | level = btrfs_header_level(eb); | 1565 | level = btrfs_header_level(eb); |
@@ -1585,13 +1599,20 @@ static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, | |||
1585 | struct extent_buffer *buf; | 1599 | struct extent_buffer *buf; |
1586 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); | 1600 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
1587 | if (buf) { | 1601 | if (buf) { |
1602 | /* we can reuse a block if it hasn't been written | ||
1603 | * and it is from this transaction. We can't | ||
1604 | * reuse anything from the tree log root because | ||
1605 | * it has tiny sub-transactions. | ||
1606 | */ | ||
1588 | if (btrfs_buffer_uptodate(buf, 0) && | 1607 | if (btrfs_buffer_uptodate(buf, 0) && |
1589 | btrfs_try_tree_lock(buf)) { | 1608 | btrfs_try_tree_lock(buf)) { |
1590 | u64 transid = | 1609 | u64 transid = |
1591 | root->fs_info->running_transaction->transid; | 1610 | root->fs_info->running_transaction->transid; |
1592 | u64 header_transid = | 1611 | u64 header_transid = |
1593 | btrfs_header_generation(buf); | 1612 | btrfs_header_generation(buf); |
1594 | if (header_transid == transid && | 1613 | if (btrfs_header_owner(buf) != |
1614 | BTRFS_TREE_LOG_OBJECTID && | ||
1615 | header_transid == transid && | ||
1595 | !btrfs_header_flag(buf, | 1616 | !btrfs_header_flag(buf, |
1596 | BTRFS_HEADER_FLAG_WRITTEN)) { | 1617 | BTRFS_HEADER_FLAG_WRITTEN)) { |
1597 | clean_tree_block(NULL, root, buf); | 1618 | clean_tree_block(NULL, root, buf); |
@@ -1603,7 +1624,7 @@ static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, | |||
1603 | } | 1624 | } |
1604 | free_extent_buffer(buf); | 1625 | free_extent_buffer(buf); |
1605 | } | 1626 | } |
1606 | update_pinned_extents(root, bytenr, num_bytes, 1); | 1627 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
1607 | } else { | 1628 | } else { |
1608 | set_extent_bits(&root->fs_info->pending_del, | 1629 | set_extent_bits(&root->fs_info->pending_del, |
1609 | bytenr, bytenr + num_bytes - 1, | 1630 | bytenr, bytenr + num_bytes - 1, |
@@ -1801,7 +1822,7 @@ static int del_pending_extents(struct btrfs_trans_handle *trans, struct | |||
1801 | GFP_NOFS); | 1822 | GFP_NOFS); |
1802 | if (!test_range_bit(&extent_root->fs_info->extent_ins, | 1823 | if (!test_range_bit(&extent_root->fs_info->extent_ins, |
1803 | start, end, EXTENT_LOCKED, 0)) { | 1824 | start, end, EXTENT_LOCKED, 0)) { |
1804 | update_pinned_extents(extent_root, start, | 1825 | btrfs_update_pinned_extents(extent_root, start, |
1805 | end + 1 - start, 1); | 1826 | end + 1 - start, 1); |
1806 | ret = __free_extent(trans, extent_root, | 1827 | ret = __free_extent(trans, extent_root, |
1807 | start, end + 1 - start, | 1828 | start, end + 1 - start, |
@@ -1919,6 +1940,12 @@ static int noinline find_free_extent(struct btrfs_trans_handle *trans, | |||
1919 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { | 1940 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
1920 | last_ptr = &root->fs_info->last_data_alloc; | 1941 | last_ptr = &root->fs_info->last_data_alloc; |
1921 | } | 1942 | } |
1943 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { | ||
1944 | last_ptr = &root->fs_info->last_log_alloc; | ||
1945 | if (!last_ptr == 0 && root->fs_info->last_alloc) { | ||
1946 | *last_ptr = root->fs_info->last_alloc + empty_cluster; | ||
1947 | } | ||
1948 | } | ||
1922 | 1949 | ||
1923 | if (last_ptr) { | 1950 | if (last_ptr) { |
1924 | if (*last_ptr) | 1951 | if (*last_ptr) |
@@ -2268,6 +2295,35 @@ int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |||
2268 | maybe_unlock_mutex(root); | 2295 | maybe_unlock_mutex(root); |
2269 | return ret; | 2296 | return ret; |
2270 | } | 2297 | } |
2298 | |||
2299 | /* | ||
2300 | * this is used by the tree logging recovery code. It records that | ||
2301 | * an extent has been allocated and makes sure to clear the free | ||
2302 | * space cache bits as well | ||
2303 | */ | ||
2304 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | ||
2305 | struct btrfs_root *root, | ||
2306 | u64 root_objectid, u64 ref_generation, | ||
2307 | u64 owner, u64 owner_offset, | ||
2308 | struct btrfs_key *ins) | ||
2309 | { | ||
2310 | int ret; | ||
2311 | struct btrfs_block_group_cache *block_group; | ||
2312 | |||
2313 | maybe_lock_mutex(root); | ||
2314 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); | ||
2315 | cache_block_group(root, block_group); | ||
2316 | |||
2317 | clear_extent_dirty(&root->fs_info->free_space_cache, | ||
2318 | ins->objectid, ins->objectid + ins->offset - 1, | ||
2319 | GFP_NOFS); | ||
2320 | ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid, | ||
2321 | ref_generation, owner, | ||
2322 | owner_offset, ins); | ||
2323 | maybe_unlock_mutex(root); | ||
2324 | return ret; | ||
2325 | } | ||
2326 | |||
2271 | /* | 2327 | /* |
2272 | * finds a free extent and does all the dirty work required for allocation | 2328 | * finds a free extent and does all the dirty work required for allocation |
2273 | * returns the key for the extent through ins, and a tree buffer for | 2329 | * returns the key for the extent through ins, and a tree buffer for |
@@ -2350,9 +2406,8 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, | |||
2350 | return buf; | 2406 | return buf; |
2351 | } | 2407 | } |
2352 | 2408 | ||
2353 | static int noinline drop_leaf_ref_no_cache(struct btrfs_trans_handle *trans, | 2409 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
2354 | struct btrfs_root *root, | 2410 | struct btrfs_root *root, struct extent_buffer *leaf) |
2355 | struct extent_buffer *leaf) | ||
2356 | { | 2411 | { |
2357 | u64 leaf_owner; | 2412 | u64 leaf_owner; |
2358 | u64 leaf_generation; | 2413 | u64 leaf_generation; |
@@ -2402,9 +2457,9 @@ static int noinline drop_leaf_ref_no_cache(struct btrfs_trans_handle *trans, | |||
2402 | return 0; | 2457 | return 0; |
2403 | } | 2458 | } |
2404 | 2459 | ||
2405 | static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans, | 2460 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
2406 | struct btrfs_root *root, | 2461 | struct btrfs_root *root, |
2407 | struct btrfs_leaf_ref *ref) | 2462 | struct btrfs_leaf_ref *ref) |
2408 | { | 2463 | { |
2409 | int i; | 2464 | int i; |
2410 | int ret; | 2465 | int ret; |
@@ -2512,7 +2567,7 @@ static int noinline walk_down_tree(struct btrfs_trans_handle *trans, | |||
2512 | btrfs_header_nritems(cur)) | 2567 | btrfs_header_nritems(cur)) |
2513 | break; | 2568 | break; |
2514 | if (*level == 0) { | 2569 | if (*level == 0) { |
2515 | ret = drop_leaf_ref_no_cache(trans, root, cur); | 2570 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
2516 | BUG_ON(ret); | 2571 | BUG_ON(ret); |
2517 | break; | 2572 | break; |
2518 | } | 2573 | } |
@@ -2552,7 +2607,7 @@ static int noinline walk_down_tree(struct btrfs_trans_handle *trans, | |||
2552 | btrfs_node_key_to_cpu(cur, &key, path->slots[*level]); | 2607 | btrfs_node_key_to_cpu(cur, &key, path->slots[*level]); |
2553 | ref = btrfs_lookup_leaf_ref(root, bytenr); | 2608 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
2554 | if (ref) { | 2609 | if (ref) { |
2555 | ret = drop_leaf_ref(trans, root, ref); | 2610 | ret = cache_drop_leaf_ref(trans, root, ref); |
2556 | BUG_ON(ret); | 2611 | BUG_ON(ret); |
2557 | btrfs_remove_leaf_ref(root, ref); | 2612 | btrfs_remove_leaf_ref(root, ref); |
2558 | btrfs_free_leaf_ref(root, ref); | 2613 | btrfs_free_leaf_ref(root, ref); |
@@ -3628,6 +3683,8 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |||
3628 | extent_root = root->fs_info->extent_root; | 3683 | extent_root = root->fs_info->extent_root; |
3629 | block_group_cache = &root->fs_info->block_group_cache; | 3684 | block_group_cache = &root->fs_info->block_group_cache; |
3630 | 3685 | ||
3686 | root->fs_info->last_trans_new_blockgroup = trans->transid; | ||
3687 | |||
3631 | cache = kzalloc(sizeof(*cache), GFP_NOFS); | 3688 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
3632 | BUG_ON(!cache); | 3689 | BUG_ON(!cache); |
3633 | cache->key.objectid = chunk_offset; | 3690 | cache->key.objectid = chunk_offset; |
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index e9e86fbaa243..84ecf3ab8511 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c | |||
@@ -36,6 +36,8 @@ | |||
36 | #include "btrfs_inode.h" | 36 | #include "btrfs_inode.h" |
37 | #include "ioctl.h" | 37 | #include "ioctl.h" |
38 | #include "print-tree.h" | 38 | #include "print-tree.h" |
39 | #include "tree-log.h" | ||
40 | #include "locking.h" | ||
39 | #include "compat.h" | 41 | #include "compat.h" |
40 | 42 | ||
41 | 43 | ||
@@ -988,10 +990,27 @@ out_nolock: | |||
988 | *ppos = pos; | 990 | *ppos = pos; |
989 | 991 | ||
990 | if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { | 992 | if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
991 | err = sync_page_range(inode, inode->i_mapping, | 993 | struct btrfs_trans_handle *trans; |
992 | start_pos, num_written); | 994 | |
995 | err = btrfs_fdatawrite_range(inode->i_mapping, start_pos, | ||
996 | start_pos + num_written -1, | ||
997 | WB_SYNC_NONE); | ||
998 | if (err < 0) | ||
999 | num_written = err; | ||
1000 | |||
1001 | err = btrfs_wait_on_page_writeback_range(inode->i_mapping, | ||
1002 | start_pos, start_pos + num_written - 1); | ||
993 | if (err < 0) | 1003 | if (err < 0) |
994 | num_written = err; | 1004 | num_written = err; |
1005 | |||
1006 | trans = btrfs_start_transaction(root, 1); | ||
1007 | ret = btrfs_log_dentry_safe(trans, root, file->f_dentry); | ||
1008 | if (ret == 0) { | ||
1009 | btrfs_sync_log(trans, root); | ||
1010 | btrfs_end_transaction(trans, root); | ||
1011 | } else { | ||
1012 | btrfs_commit_transaction(trans, root); | ||
1013 | } | ||
995 | } else if (num_written > 0 && (file->f_flags & O_DIRECT)) { | 1014 | } else if (num_written > 0 && (file->f_flags & O_DIRECT)) { |
996 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) | 1015 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) |
997 | do_sync_file_range(file, start_pos, | 1016 | do_sync_file_range(file, start_pos, |
@@ -1019,8 +1038,7 @@ int btrfs_release_file(struct inode * inode, struct file * filp) | |||
1019 | return 0; | 1038 | return 0; |
1020 | } | 1039 | } |
1021 | 1040 | ||
1022 | static int btrfs_sync_file(struct file *file, | 1041 | int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync) |
1023 | struct dentry *dentry, int datasync) | ||
1024 | { | 1042 | { |
1025 | struct inode *inode = dentry->d_inode; | 1043 | struct inode *inode = dentry->d_inode; |
1026 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1044 | struct btrfs_root *root = BTRFS_I(inode)->root; |
@@ -1043,6 +1061,8 @@ static int btrfs_sync_file(struct file *file, | |||
1043 | } | 1061 | } |
1044 | mutex_unlock(&root->fs_info->trans_mutex); | 1062 | mutex_unlock(&root->fs_info->trans_mutex); |
1045 | 1063 | ||
1064 | filemap_fdatawait(inode->i_mapping); | ||
1065 | |||
1046 | /* | 1066 | /* |
1047 | * ok we haven't committed the transaction yet, lets do a commit | 1067 | * ok we haven't committed the transaction yet, lets do a commit |
1048 | */ | 1068 | */ |
@@ -1054,7 +1074,16 @@ static int btrfs_sync_file(struct file *file, | |||
1054 | ret = -ENOMEM; | 1074 | ret = -ENOMEM; |
1055 | goto out; | 1075 | goto out; |
1056 | } | 1076 | } |
1057 | ret = btrfs_commit_transaction(trans, root); | 1077 | |
1078 | ret = btrfs_log_dentry_safe(trans, root, file->f_dentry); | ||
1079 | if (ret < 0) | ||
1080 | goto out; | ||
1081 | if (ret > 0) { | ||
1082 | ret = btrfs_commit_transaction(trans, root); | ||
1083 | } else { | ||
1084 | btrfs_sync_log(trans, root); | ||
1085 | ret = btrfs_end_transaction(trans, root); | ||
1086 | } | ||
1058 | out: | 1087 | out: |
1059 | return ret > 0 ? EIO : ret; | 1088 | return ret > 0 ? EIO : ret; |
1060 | } | 1089 | } |
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 43d3f2649ca3..65df9d830230 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c | |||
@@ -46,6 +46,8 @@ | |||
46 | #include "volumes.h" | 46 | #include "volumes.h" |
47 | #include "ordered-data.h" | 47 | #include "ordered-data.h" |
48 | #include "xattr.h" | 48 | #include "xattr.h" |
49 | #include "compat.h" | ||
50 | #include "tree-log.h" | ||
49 | 51 | ||
50 | struct btrfs_iget_args { | 52 | struct btrfs_iget_args { |
51 | u64 ino; | 53 | u64 ino; |
@@ -586,6 +588,7 @@ nocow: | |||
586 | &ordered_extent->list); | 588 | &ordered_extent->list); |
587 | 589 | ||
588 | btrfs_ordered_update_i_size(inode, ordered_extent); | 590 | btrfs_ordered_update_i_size(inode, ordered_extent); |
591 | btrfs_update_inode(trans, root, inode); | ||
589 | btrfs_remove_ordered_extent(inode, ordered_extent); | 592 | btrfs_remove_ordered_extent(inode, ordered_extent); |
590 | 593 | ||
591 | /* once for us */ | 594 | /* once for us */ |
@@ -593,7 +596,6 @@ nocow: | |||
593 | /* once for the tree */ | 596 | /* once for the tree */ |
594 | btrfs_put_ordered_extent(ordered_extent); | 597 | btrfs_put_ordered_extent(ordered_extent); |
595 | 598 | ||
596 | btrfs_update_inode(trans, root, inode); | ||
597 | btrfs_end_transaction(trans, root); | 599 | btrfs_end_transaction(trans, root); |
598 | return 0; | 600 | return 0; |
599 | } | 601 | } |
@@ -1007,7 +1009,8 @@ void btrfs_read_locked_inode(struct inode *inode) | |||
1007 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | 1009 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
1008 | 1010 | ||
1009 | inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); | 1011 | inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); |
1010 | inode->i_generation = btrfs_inode_generation(leaf, inode_item); | 1012 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
1013 | inode->i_generation = BTRFS_I(inode)->generation; | ||
1011 | inode->i_rdev = 0; | 1014 | inode->i_rdev = 0; |
1012 | rdev = btrfs_inode_rdev(leaf, inode_item); | 1015 | rdev = btrfs_inode_rdev(leaf, inode_item); |
1013 | 1016 | ||
@@ -1056,7 +1059,8 @@ make_bad: | |||
1056 | make_bad_inode(inode); | 1059 | make_bad_inode(inode); |
1057 | } | 1060 | } |
1058 | 1061 | ||
1059 | static void fill_inode_item(struct extent_buffer *leaf, | 1062 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
1063 | struct extent_buffer *leaf, | ||
1060 | struct btrfs_inode_item *item, | 1064 | struct btrfs_inode_item *item, |
1061 | struct inode *inode) | 1065 | struct inode *inode) |
1062 | { | 1066 | { |
@@ -1082,7 +1086,8 @@ static void fill_inode_item(struct extent_buffer *leaf, | |||
1082 | inode->i_ctime.tv_nsec); | 1086 | inode->i_ctime.tv_nsec); |
1083 | 1087 | ||
1084 | btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); | 1088 | btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); |
1085 | btrfs_set_inode_generation(leaf, item, inode->i_generation); | 1089 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
1090 | btrfs_set_inode_transid(leaf, item, trans->transid); | ||
1086 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); | 1091 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
1087 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); | 1092 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
1088 | btrfs_set_inode_block_group(leaf, item, | 1093 | btrfs_set_inode_block_group(leaf, item, |
@@ -1112,7 +1117,7 @@ int noinline btrfs_update_inode(struct btrfs_trans_handle *trans, | |||
1112 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | 1117 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
1113 | struct btrfs_inode_item); | 1118 | struct btrfs_inode_item); |
1114 | 1119 | ||
1115 | fill_inode_item(leaf, inode_item, inode); | 1120 | fill_inode_item(trans, leaf, inode_item, inode); |
1116 | btrfs_mark_buffer_dirty(leaf); | 1121 | btrfs_mark_buffer_dirty(leaf); |
1117 | btrfs_set_inode_last_trans(trans, inode); | 1122 | btrfs_set_inode_last_trans(trans, inode); |
1118 | ret = 0; | 1123 | ret = 0; |
@@ -1122,14 +1127,12 @@ failed: | |||
1122 | } | 1127 | } |
1123 | 1128 | ||
1124 | 1129 | ||
1125 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, | 1130 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
1126 | struct btrfs_root *root, | 1131 | struct btrfs_root *root, |
1127 | struct inode *dir, | 1132 | struct inode *dir, struct inode *inode, |
1128 | struct dentry *dentry) | 1133 | const char *name, int name_len) |
1129 | { | 1134 | { |
1130 | struct btrfs_path *path; | 1135 | struct btrfs_path *path; |
1131 | const char *name = dentry->d_name.name; | ||
1132 | int name_len = dentry->d_name.len; | ||
1133 | int ret = 0; | 1136 | int ret = 0; |
1134 | struct extent_buffer *leaf; | 1137 | struct extent_buffer *leaf; |
1135 | struct btrfs_dir_item *di; | 1138 | struct btrfs_dir_item *di; |
@@ -1160,13 +1163,12 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, | |||
1160 | btrfs_release_path(root, path); | 1163 | btrfs_release_path(root, path); |
1161 | 1164 | ||
1162 | ret = btrfs_del_inode_ref(trans, root, name, name_len, | 1165 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
1163 | dentry->d_inode->i_ino, | 1166 | inode->i_ino, |
1164 | dentry->d_parent->d_inode->i_ino, &index); | 1167 | dir->i_ino, &index); |
1165 | if (ret) { | 1168 | if (ret) { |
1166 | printk("failed to delete reference to %.*s, " | 1169 | printk("failed to delete reference to %.*s, " |
1167 | "inode %lu parent %lu\n", name_len, name, | 1170 | "inode %lu parent %lu\n", name_len, name, |
1168 | dentry->d_inode->i_ino, | 1171 | inode->i_ino, dir->i_ino); |
1169 | dentry->d_parent->d_inode->i_ino); | ||
1170 | goto err; | 1172 | goto err; |
1171 | } | 1173 | } |
1172 | 1174 | ||
@@ -1183,21 +1185,25 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, | |||
1183 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | 1185 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
1184 | btrfs_release_path(root, path); | 1186 | btrfs_release_path(root, path); |
1185 | 1187 | ||
1186 | dentry->d_inode->i_ctime = dir->i_ctime; | 1188 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
1189 | inode, dir->i_ino); | ||
1190 | BUG_ON(ret); | ||
1191 | |||
1192 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | ||
1193 | dir, index); | ||
1194 | BUG_ON(ret); | ||
1187 | err: | 1195 | err: |
1188 | btrfs_free_path(path); | 1196 | btrfs_free_path(path); |
1189 | if (!ret) { | 1197 | if (ret) |
1190 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | 1198 | goto out; |
1191 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; | 1199 | |
1192 | btrfs_update_inode(trans, root, dir); | 1200 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
1193 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) | 1201 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
1194 | dentry->d_inode->i_nlink--; | 1202 | btrfs_update_inode(trans, root, dir); |
1195 | #else | 1203 | btrfs_drop_nlink(inode); |
1196 | drop_nlink(dentry->d_inode); | 1204 | ret = btrfs_update_inode(trans, root, inode); |
1197 | #endif | 1205 | dir->i_sb->s_dirt = 1; |
1198 | ret = btrfs_update_inode(trans, root, dentry->d_inode); | 1206 | out: |
1199 | dir->i_sb->s_dirt = 1; | ||
1200 | } | ||
1201 | return ret; | 1207 | return ret; |
1202 | } | 1208 | } |
1203 | 1209 | ||
@@ -1218,7 +1224,8 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |||
1218 | trans = btrfs_start_transaction(root, 1); | 1224 | trans = btrfs_start_transaction(root, 1); |
1219 | 1225 | ||
1220 | btrfs_set_trans_block_group(trans, dir); | 1226 | btrfs_set_trans_block_group(trans, dir); |
1221 | ret = btrfs_unlink_trans(trans, root, dir, dentry); | 1227 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
1228 | dentry->d_name.name, dentry->d_name.len); | ||
1222 | 1229 | ||
1223 | if (inode->i_nlink == 0) | 1230 | if (inode->i_nlink == 0) |
1224 | ret = btrfs_orphan_add(trans, inode); | 1231 | ret = btrfs_orphan_add(trans, inode); |
@@ -1256,7 +1263,8 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |||
1256 | goto fail_trans; | 1263 | goto fail_trans; |
1257 | 1264 | ||
1258 | /* now the directory is empty */ | 1265 | /* now the directory is empty */ |
1259 | err = btrfs_unlink_trans(trans, root, dir, dentry); | 1266 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
1267 | dentry->d_name.name, dentry->d_name.len); | ||
1260 | if (!err) { | 1268 | if (!err) { |
1261 | btrfs_i_size_write(inode, 0); | 1269 | btrfs_i_size_write(inode, 0); |
1262 | } | 1270 | } |
@@ -1283,10 +1291,10 @@ fail: | |||
1283 | * min_type is the minimum key type to truncate down to. If set to 0, this | 1291 | * min_type is the minimum key type to truncate down to. If set to 0, this |
1284 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | 1292 | * will kill all the items on this inode, including the INODE_ITEM_KEY. |
1285 | */ | 1293 | */ |
1286 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, | 1294 | noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
1287 | struct btrfs_root *root, | 1295 | struct btrfs_root *root, |
1288 | struct inode *inode, | 1296 | struct inode *inode, |
1289 | u32 min_type) | 1297 | u64 new_size, u32 min_type) |
1290 | { | 1298 | { |
1291 | int ret; | 1299 | int ret; |
1292 | struct btrfs_path *path; | 1300 | struct btrfs_path *path; |
@@ -1307,7 +1315,9 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, | |||
1307 | int extent_type = -1; | 1315 | int extent_type = -1; |
1308 | u64 mask = root->sectorsize - 1; | 1316 | u64 mask = root->sectorsize - 1; |
1309 | 1317 | ||
1310 | btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1); | 1318 | if (root->ref_cows) |
1319 | btrfs_drop_extent_cache(inode, | ||
1320 | new_size & (~mask), (u64)-1); | ||
1311 | path = btrfs_alloc_path(); | 1321 | path = btrfs_alloc_path(); |
1312 | path->reada = -1; | 1322 | path->reada = -1; |
1313 | BUG_ON(!path); | 1323 | BUG_ON(!path); |
@@ -1324,7 +1334,13 @@ search_again: | |||
1324 | goto error; | 1334 | goto error; |
1325 | } | 1335 | } |
1326 | if (ret > 0) { | 1336 | if (ret > 0) { |
1327 | BUG_ON(path->slots[0] == 0); | 1337 | /* there are no items in the tree for us to truncate, we're |
1338 | * done | ||
1339 | */ | ||
1340 | if (path->slots[0] == 0) { | ||
1341 | ret = 0; | ||
1342 | goto error; | ||
1343 | } | ||
1328 | path->slots[0]--; | 1344 | path->slots[0]--; |
1329 | } | 1345 | } |
1330 | 1346 | ||
@@ -1358,10 +1374,10 @@ search_again: | |||
1358 | } | 1374 | } |
1359 | if (found_type == BTRFS_CSUM_ITEM_KEY) { | 1375 | if (found_type == BTRFS_CSUM_ITEM_KEY) { |
1360 | ret = btrfs_csum_truncate(trans, root, path, | 1376 | ret = btrfs_csum_truncate(trans, root, path, |
1361 | inode->i_size); | 1377 | new_size); |
1362 | BUG_ON(ret); | 1378 | BUG_ON(ret); |
1363 | } | 1379 | } |
1364 | if (item_end < inode->i_size) { | 1380 | if (item_end < new_size) { |
1365 | if (found_type == BTRFS_DIR_ITEM_KEY) { | 1381 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
1366 | found_type = BTRFS_INODE_ITEM_KEY; | 1382 | found_type = BTRFS_INODE_ITEM_KEY; |
1367 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { | 1383 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { |
@@ -1378,7 +1394,7 @@ search_again: | |||
1378 | btrfs_set_key_type(&key, found_type); | 1394 | btrfs_set_key_type(&key, found_type); |
1379 | goto next; | 1395 | goto next; |
1380 | } | 1396 | } |
1381 | if (found_key.offset >= inode->i_size) | 1397 | if (found_key.offset >= new_size) |
1382 | del_item = 1; | 1398 | del_item = 1; |
1383 | else | 1399 | else |
1384 | del_item = 0; | 1400 | del_item = 0; |
@@ -1394,7 +1410,7 @@ search_again: | |||
1394 | if (!del_item) { | 1410 | if (!del_item) { |
1395 | u64 orig_num_bytes = | 1411 | u64 orig_num_bytes = |
1396 | btrfs_file_extent_num_bytes(leaf, fi); | 1412 | btrfs_file_extent_num_bytes(leaf, fi); |
1397 | extent_num_bytes = inode->i_size - | 1413 | extent_num_bytes = new_size - |
1398 | found_key.offset + root->sectorsize - 1; | 1414 | found_key.offset + root->sectorsize - 1; |
1399 | extent_num_bytes = extent_num_bytes & | 1415 | extent_num_bytes = extent_num_bytes & |
1400 | ~((u64)root->sectorsize - 1); | 1416 | ~((u64)root->sectorsize - 1); |
@@ -1402,7 +1418,7 @@ search_again: | |||
1402 | extent_num_bytes); | 1418 | extent_num_bytes); |
1403 | num_dec = (orig_num_bytes - | 1419 | num_dec = (orig_num_bytes - |
1404 | extent_num_bytes); | 1420 | extent_num_bytes); |
1405 | if (extent_start != 0) | 1421 | if (root->ref_cows && extent_start != 0) |
1406 | dec_i_blocks(inode, num_dec); | 1422 | dec_i_blocks(inode, num_dec); |
1407 | btrfs_mark_buffer_dirty(leaf); | 1423 | btrfs_mark_buffer_dirty(leaf); |
1408 | } else { | 1424 | } else { |
@@ -1413,22 +1429,29 @@ search_again: | |||
1413 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); | 1429 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
1414 | if (extent_start != 0) { | 1430 | if (extent_start != 0) { |
1415 | found_extent = 1; | 1431 | found_extent = 1; |
1416 | dec_i_blocks(inode, num_dec); | 1432 | if (root->ref_cows) |
1433 | dec_i_blocks(inode, num_dec); | ||
1434 | } | ||
1435 | if (root->ref_cows) { | ||
1436 | root_gen = | ||
1437 | btrfs_header_generation(leaf); | ||
1417 | } | 1438 | } |
1418 | root_gen = btrfs_header_generation(leaf); | ||
1419 | root_owner = btrfs_header_owner(leaf); | 1439 | root_owner = btrfs_header_owner(leaf); |
1420 | } | 1440 | } |
1421 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | 1441 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
1422 | if (!del_item) { | 1442 | if (!del_item) { |
1423 | u32 newsize = inode->i_size - found_key.offset; | 1443 | u32 size = new_size - found_key.offset; |
1424 | dec_i_blocks(inode, item_end + 1 - | 1444 | |
1425 | found_key.offset - newsize); | 1445 | if (root->ref_cows) { |
1426 | newsize = | 1446 | dec_i_blocks(inode, item_end + 1 - |
1427 | btrfs_file_extent_calc_inline_size(newsize); | 1447 | found_key.offset - size); |
1448 | } | ||
1449 | size = | ||
1450 | btrfs_file_extent_calc_inline_size(size); | ||
1428 | ret = btrfs_truncate_item(trans, root, path, | 1451 | ret = btrfs_truncate_item(trans, root, path, |
1429 | newsize, 1); | 1452 | size, 1); |
1430 | BUG_ON(ret); | 1453 | BUG_ON(ret); |
1431 | } else { | 1454 | } else if (root->ref_cows) { |
1432 | dec_i_blocks(inode, item_end + 1 - | 1455 | dec_i_blocks(inode, item_end + 1 - |
1433 | found_key.offset); | 1456 | found_key.offset); |
1434 | } | 1457 | } |
@@ -1666,7 +1689,7 @@ void btrfs_delete_inode(struct inode *inode) | |||
1666 | trans = btrfs_start_transaction(root, 1); | 1689 | trans = btrfs_start_transaction(root, 1); |
1667 | 1690 | ||
1668 | btrfs_set_trans_block_group(trans, inode); | 1691 | btrfs_set_trans_block_group(trans, inode); |
1669 | ret = btrfs_truncate_in_trans(trans, root, inode, 0); | 1692 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0); |
1670 | if (ret) { | 1693 | if (ret) { |
1671 | btrfs_orphan_del(NULL, inode); | 1694 | btrfs_orphan_del(NULL, inode); |
1672 | goto no_delete_lock; | 1695 | goto no_delete_lock; |
@@ -1753,15 +1776,20 @@ static int fixup_tree_root_location(struct btrfs_root *root, | |||
1753 | return 0; | 1776 | return 0; |
1754 | } | 1777 | } |
1755 | 1778 | ||
1756 | static int btrfs_init_locked_inode(struct inode *inode, void *p) | 1779 | static noinline void init_btrfs_i(struct inode *inode) |
1757 | { | 1780 | { |
1758 | struct btrfs_iget_args *args = p; | 1781 | struct btrfs_inode *bi = BTRFS_I(inode); |
1759 | inode->i_ino = args->ino; | 1782 | |
1760 | BTRFS_I(inode)->root = args->root; | 1783 | bi->i_acl = NULL; |
1761 | BTRFS_I(inode)->delalloc_bytes = 0; | 1784 | bi->i_default_acl = NULL; |
1762 | inode->i_mapping->writeback_index = 0; | 1785 | |
1763 | BTRFS_I(inode)->disk_i_size = 0; | 1786 | bi->generation = 0; |
1764 | BTRFS_I(inode)->index_cnt = (u64)-1; | 1787 | bi->last_trans = 0; |
1788 | bi->logged_trans = 0; | ||
1789 | bi->delalloc_bytes = 0; | ||
1790 | bi->disk_i_size = 0; | ||
1791 | bi->flags = 0; | ||
1792 | bi->index_cnt = (u64)-1; | ||
1765 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); | 1793 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
1766 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | 1794 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
1767 | inode->i_mapping, GFP_NOFS); | 1795 | inode->i_mapping, GFP_NOFS); |
@@ -1771,6 +1799,15 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p) | |||
1771 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); | 1799 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
1772 | mutex_init(&BTRFS_I(inode)->csum_mutex); | 1800 | mutex_init(&BTRFS_I(inode)->csum_mutex); |
1773 | mutex_init(&BTRFS_I(inode)->extent_mutex); | 1801 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
1802 | mutex_init(&BTRFS_I(inode)->log_mutex); | ||
1803 | } | ||
1804 | |||
1805 | static int btrfs_init_locked_inode(struct inode *inode, void *p) | ||
1806 | { | ||
1807 | struct btrfs_iget_args *args = p; | ||
1808 | inode->i_ino = args->ino; | ||
1809 | init_btrfs_i(inode); | ||
1810 | BTRFS_I(inode)->root = args->root; | ||
1774 | return 0; | 1811 | return 0; |
1775 | } | 1812 | } |
1776 | 1813 | ||
@@ -2263,21 +2300,10 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |||
2263 | * btrfs_get_inode_index_count has an explanation for the magic | 2300 | * btrfs_get_inode_index_count has an explanation for the magic |
2264 | * number | 2301 | * number |
2265 | */ | 2302 | */ |
2303 | init_btrfs_i(inode); | ||
2266 | BTRFS_I(inode)->index_cnt = 2; | 2304 | BTRFS_I(inode)->index_cnt = 2; |
2267 | |||
2268 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); | ||
2269 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | ||
2270 | inode->i_mapping, GFP_NOFS); | ||
2271 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, | ||
2272 | inode->i_mapping, GFP_NOFS); | ||
2273 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); | ||
2274 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); | ||
2275 | mutex_init(&BTRFS_I(inode)->csum_mutex); | ||
2276 | mutex_init(&BTRFS_I(inode)->extent_mutex); | ||
2277 | BTRFS_I(inode)->delalloc_bytes = 0; | ||
2278 | inode->i_mapping->writeback_index = 0; | ||
2279 | BTRFS_I(inode)->disk_i_size = 0; | ||
2280 | BTRFS_I(inode)->root = root; | 2305 | BTRFS_I(inode)->root = root; |
2306 | BTRFS_I(inode)->generation = trans->transid; | ||
2281 | 2307 | ||
2282 | if (mode & S_IFDIR) | 2308 | if (mode & S_IFDIR) |
2283 | owner = 0; | 2309 | owner = 0; |
@@ -2290,7 +2316,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |||
2290 | new_inode_group = group; | 2316 | new_inode_group = group; |
2291 | } | 2317 | } |
2292 | BTRFS_I(inode)->block_group = new_inode_group; | 2318 | BTRFS_I(inode)->block_group = new_inode_group; |
2293 | BTRFS_I(inode)->flags = 0; | ||
2294 | 2319 | ||
2295 | key[0].objectid = objectid; | 2320 | key[0].objectid = objectid; |
2296 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); | 2321 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
@@ -2318,7 +2343,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |||
2318 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | 2343 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
2319 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 2344 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
2320 | struct btrfs_inode_item); | 2345 | struct btrfs_inode_item); |
2321 | fill_inode_item(path->nodes[0], inode_item, inode); | 2346 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
2322 | 2347 | ||
2323 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | 2348 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
2324 | struct btrfs_inode_ref); | 2349 | struct btrfs_inode_ref); |
@@ -2349,38 +2374,34 @@ static inline u8 btrfs_inode_type(struct inode *inode) | |||
2349 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | 2374 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
2350 | } | 2375 | } |
2351 | 2376 | ||
2352 | static int btrfs_add_link(struct btrfs_trans_handle *trans, | 2377 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
2353 | struct dentry *dentry, struct inode *inode, | 2378 | struct inode *parent_inode, struct inode *inode, |
2354 | int add_backref, u64 index) | 2379 | const char *name, int name_len, int add_backref, u64 index) |
2355 | { | 2380 | { |
2356 | int ret; | 2381 | int ret; |
2357 | struct btrfs_key key; | 2382 | struct btrfs_key key; |
2358 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; | 2383 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
2359 | struct inode *parent_inode = dentry->d_parent->d_inode; | ||
2360 | 2384 | ||
2361 | key.objectid = inode->i_ino; | 2385 | key.objectid = inode->i_ino; |
2362 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | 2386 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
2363 | key.offset = 0; | 2387 | key.offset = 0; |
2364 | 2388 | ||
2365 | ret = btrfs_insert_dir_item(trans, root, | 2389 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
2366 | dentry->d_name.name, dentry->d_name.len, | 2390 | parent_inode->i_ino, |
2367 | dentry->d_parent->d_inode->i_ino, | ||
2368 | &key, btrfs_inode_type(inode), | 2391 | &key, btrfs_inode_type(inode), |
2369 | index); | 2392 | index); |
2370 | if (ret == 0) { | 2393 | if (ret == 0) { |
2371 | if (add_backref) { | 2394 | if (add_backref) { |
2372 | ret = btrfs_insert_inode_ref(trans, root, | 2395 | ret = btrfs_insert_inode_ref(trans, root, |
2373 | dentry->d_name.name, | 2396 | name, name_len, |
2374 | dentry->d_name.len, | 2397 | inode->i_ino, |
2375 | inode->i_ino, | 2398 | parent_inode->i_ino, |
2376 | parent_inode->i_ino, | 2399 | index); |
2377 | index); | ||
2378 | } | 2400 | } |
2379 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | 2401 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
2380 | dentry->d_name.len * 2); | 2402 | name_len * 2); |
2381 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; | 2403 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
2382 | ret = btrfs_update_inode(trans, root, | 2404 | ret = btrfs_update_inode(trans, root, parent_inode); |
2383 | dentry->d_parent->d_inode); | ||
2384 | } | 2405 | } |
2385 | return ret; | 2406 | return ret; |
2386 | } | 2407 | } |
@@ -2389,7 +2410,9 @@ static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |||
2389 | struct dentry *dentry, struct inode *inode, | 2410 | struct dentry *dentry, struct inode *inode, |
2390 | int backref, u64 index) | 2411 | int backref, u64 index) |
2391 | { | 2412 | { |
2392 | int err = btrfs_add_link(trans, dentry, inode, backref, index); | 2413 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
2414 | inode, dentry->d_name.name, | ||
2415 | dentry->d_name.len, backref, index); | ||
2393 | if (!err) { | 2416 | if (!err) { |
2394 | d_instantiate(dentry, inode); | 2417 | d_instantiate(dentry, inode); |
2395 | return 0; | 2418 | return 0; |
@@ -2513,19 +2536,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry, | |||
2513 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; | 2536 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
2514 | inode->i_fop = &btrfs_file_operations; | 2537 | inode->i_fop = &btrfs_file_operations; |
2515 | inode->i_op = &btrfs_file_inode_operations; | 2538 | inode->i_op = &btrfs_file_inode_operations; |
2516 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); | ||
2517 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | ||
2518 | inode->i_mapping, GFP_NOFS); | ||
2519 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, | ||
2520 | inode->i_mapping, GFP_NOFS); | ||
2521 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); | ||
2522 | mutex_init(&BTRFS_I(inode)->csum_mutex); | ||
2523 | mutex_init(&BTRFS_I(inode)->extent_mutex); | ||
2524 | BTRFS_I(inode)->delalloc_bytes = 0; | ||
2525 | BTRFS_I(inode)->disk_i_size = 0; | ||
2526 | inode->i_mapping->writeback_index = 0; | ||
2527 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | 2539 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
2528 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); | ||
2529 | } | 2540 | } |
2530 | dir->i_sb->s_dirt = 1; | 2541 | dir->i_sb->s_dirt = 1; |
2531 | btrfs_update_inode_block_group(trans, inode); | 2542 | btrfs_update_inode_block_group(trans, inode); |
@@ -2556,11 +2567,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |||
2556 | if (inode->i_nlink == 0) | 2567 | if (inode->i_nlink == 0) |
2557 | return -ENOENT; | 2568 | return -ENOENT; |
2558 | 2569 | ||
2559 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) | 2570 | btrfs_inc_nlink(inode); |
2560 | inode->i_nlink++; | ||
2561 | #else | ||
2562 | inc_nlink(inode); | ||
2563 | #endif | ||
2564 | err = btrfs_check_free_space(root, 1, 0); | 2571 | err = btrfs_check_free_space(root, 1, 0); |
2565 | if (err) | 2572 | if (err) |
2566 | goto fail; | 2573 | goto fail; |
@@ -2650,7 +2657,9 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |||
2650 | if (err) | 2657 | if (err) |
2651 | goto out_fail; | 2658 | goto out_fail; |
2652 | 2659 | ||
2653 | err = btrfs_add_link(trans, dentry, inode, 0, index); | 2660 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
2661 | inode, dentry->d_name.name, | ||
2662 | dentry->d_name.len, 0, index); | ||
2654 | if (err) | 2663 | if (err) |
2655 | goto out_fail; | 2664 | goto out_fail; |
2656 | 2665 | ||
@@ -3221,7 +3230,7 @@ static void btrfs_truncate(struct inode *inode) | |||
3221 | if (ret) | 3230 | if (ret) |
3222 | goto out; | 3231 | goto out; |
3223 | /* FIXME, add redo link to tree so we don't leak on crash */ | 3232 | /* FIXME, add redo link to tree so we don't leak on crash */ |
3224 | ret = btrfs_truncate_in_trans(trans, root, inode, | 3233 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, |
3225 | BTRFS_EXTENT_DATA_KEY); | 3234 | BTRFS_EXTENT_DATA_KEY); |
3226 | btrfs_update_inode(trans, root, inode); | 3235 | btrfs_update_inode(trans, root, inode); |
3227 | 3236 | ||
@@ -3304,6 +3313,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) | |||
3304 | if (!ei) | 3313 | if (!ei) |
3305 | return NULL; | 3314 | return NULL; |
3306 | ei->last_trans = 0; | 3315 | ei->last_trans = 0; |
3316 | ei->logged_trans = 0; | ||
3307 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); | 3317 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
3308 | ei->i_acl = BTRFS_ACL_NOT_CACHED; | 3318 | ei->i_acl = BTRFS_ACL_NOT_CACHED; |
3309 | ei->i_default_acl = BTRFS_ACL_NOT_CACHED; | 3319 | ei->i_default_acl = BTRFS_ACL_NOT_CACHED; |
@@ -3463,31 +3473,39 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, | |||
3463 | 3473 | ||
3464 | btrfs_set_trans_block_group(trans, new_dir); | 3474 | btrfs_set_trans_block_group(trans, new_dir); |
3465 | 3475 | ||
3466 | old_dentry->d_inode->i_nlink++; | 3476 | btrfs_inc_nlink(old_dentry->d_inode); |
3467 | old_dir->i_ctime = old_dir->i_mtime = ctime; | 3477 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
3468 | new_dir->i_ctime = new_dir->i_mtime = ctime; | 3478 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
3469 | old_inode->i_ctime = ctime; | 3479 | old_inode->i_ctime = ctime; |
3470 | 3480 | ||
3471 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); | 3481 | ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode, |
3482 | old_dentry->d_name.name, | ||
3483 | old_dentry->d_name.len); | ||
3472 | if (ret) | 3484 | if (ret) |
3473 | goto out_fail; | 3485 | goto out_fail; |
3474 | 3486 | ||
3475 | if (new_inode) { | 3487 | if (new_inode) { |
3476 | new_inode->i_ctime = CURRENT_TIME; | 3488 | new_inode->i_ctime = CURRENT_TIME; |
3477 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); | 3489 | ret = btrfs_unlink_inode(trans, root, new_dir, |
3490 | new_dentry->d_inode, | ||
3491 | new_dentry->d_name.name, | ||
3492 | new_dentry->d_name.len); | ||
3478 | if (ret) | 3493 | if (ret) |
3479 | goto out_fail; | 3494 | goto out_fail; |
3480 | if (new_inode->i_nlink == 0) { | 3495 | if (new_inode->i_nlink == 0) { |
3481 | ret = btrfs_orphan_add(trans, new_inode); | 3496 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
3482 | if (ret) | 3497 | if (ret) |
3483 | goto out_fail; | 3498 | goto out_fail; |
3484 | } | 3499 | } |
3500 | |||
3485 | } | 3501 | } |
3486 | ret = btrfs_set_inode_index(new_dir, old_inode, &index); | 3502 | ret = btrfs_set_inode_index(new_dir, old_inode, &index); |
3487 | if (ret) | 3503 | if (ret) |
3488 | goto out_fail; | 3504 | goto out_fail; |
3489 | 3505 | ||
3490 | ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index); | 3506 | ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode, |
3507 | old_inode, new_dentry->d_name.name, | ||
3508 | new_dentry->d_name.len, 1, index); | ||
3491 | if (ret) | 3509 | if (ret) |
3492 | goto out_fail; | 3510 | goto out_fail; |
3493 | 3511 | ||
@@ -3577,19 +3595,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, | |||
3577 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; | 3595 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
3578 | inode->i_fop = &btrfs_file_operations; | 3596 | inode->i_fop = &btrfs_file_operations; |
3579 | inode->i_op = &btrfs_file_inode_operations; | 3597 | inode->i_op = &btrfs_file_inode_operations; |
3580 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); | ||
3581 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | ||
3582 | inode->i_mapping, GFP_NOFS); | ||
3583 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, | ||
3584 | inode->i_mapping, GFP_NOFS); | ||
3585 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); | ||
3586 | mutex_init(&BTRFS_I(inode)->csum_mutex); | ||
3587 | mutex_init(&BTRFS_I(inode)->extent_mutex); | ||
3588 | BTRFS_I(inode)->delalloc_bytes = 0; | ||
3589 | BTRFS_I(inode)->disk_i_size = 0; | ||
3590 | inode->i_mapping->writeback_index = 0; | ||
3591 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | 3598 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
3592 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); | ||
3593 | } | 3599 | } |
3594 | dir->i_sb->s_dirt = 1; | 3600 | dir->i_sb->s_dirt = 1; |
3595 | btrfs_update_inode_block_group(trans, inode); | 3601 | btrfs_update_inode_block_group(trans, inode); |
@@ -3691,6 +3697,7 @@ static struct file_operations btrfs_dir_file_operations = { | |||
3691 | .compat_ioctl = btrfs_ioctl, | 3697 | .compat_ioctl = btrfs_ioctl, |
3692 | #endif | 3698 | #endif |
3693 | .release = btrfs_release_file, | 3699 | .release = btrfs_release_file, |
3700 | .fsync = btrfs_sync_file, | ||
3694 | }; | 3701 | }; |
3695 | 3702 | ||
3696 | static struct extent_io_ops btrfs_extent_io_ops = { | 3703 | static struct extent_io_ops btrfs_extent_io_ops = { |
diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c index 36726696e58b..e3984f902e71 100644 --- a/fs/btrfs/root-tree.c +++ b/fs/btrfs/root-tree.c | |||
@@ -202,8 +202,9 @@ again: | |||
202 | memcpy(&found_key, &key, sizeof(key)); | 202 | memcpy(&found_key, &key, sizeof(key)); |
203 | key.offset++; | 203 | key.offset++; |
204 | btrfs_release_path(root, path); | 204 | btrfs_release_path(root, path); |
205 | dead_root = btrfs_read_fs_root_no_radix(root->fs_info, | 205 | dead_root = |
206 | &found_key); | 206 | btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
207 | &found_key); | ||
207 | if (IS_ERR(dead_root)) { | 208 | if (IS_ERR(dead_root)) { |
208 | ret = PTR_ERR(dead_root); | 209 | ret = PTR_ERR(dead_root); |
209 | goto err; | 210 | goto err; |
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index eff3ad72991b..49c4f5b40ed6 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include "transaction.h" | 25 | #include "transaction.h" |
26 | #include "locking.h" | 26 | #include "locking.h" |
27 | #include "ref-cache.h" | 27 | #include "ref-cache.h" |
28 | #include "tree-log.h" | ||
28 | 29 | ||
29 | static int total_trans = 0; | 30 | static int total_trans = 0; |
30 | extern struct kmem_cache *btrfs_trans_handle_cachep; | 31 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
@@ -57,6 +58,7 @@ static noinline int join_transaction(struct btrfs_root *root) | |||
57 | root->fs_info->generation++; | 58 | root->fs_info->generation++; |
58 | root->fs_info->last_alloc = 0; | 59 | root->fs_info->last_alloc = 0; |
59 | root->fs_info->last_data_alloc = 0; | 60 | root->fs_info->last_data_alloc = 0; |
61 | root->fs_info->last_log_alloc = 0; | ||
60 | cur_trans->num_writers = 1; | 62 | cur_trans->num_writers = 1; |
61 | cur_trans->num_joined = 0; | 63 | cur_trans->num_joined = 0; |
62 | cur_trans->transid = root->fs_info->generation; | 64 | cur_trans->transid = root->fs_info->generation; |
@@ -83,7 +85,7 @@ static noinline int join_transaction(struct btrfs_root *root) | |||
83 | return 0; | 85 | return 0; |
84 | } | 86 | } |
85 | 87 | ||
86 | static noinline int record_root_in_trans(struct btrfs_root *root) | 88 | noinline int btrfs_record_root_in_trans(struct btrfs_root *root) |
87 | { | 89 | { |
88 | struct btrfs_dirty_root *dirty; | 90 | struct btrfs_dirty_root *dirty; |
89 | u64 running_trans_id = root->fs_info->running_transaction->transid; | 91 | u64 running_trans_id = root->fs_info->running_transaction->transid; |
@@ -151,7 +153,7 @@ static void wait_current_trans(struct btrfs_root *root) | |||
151 | } | 153 | } |
152 | } | 154 | } |
153 | 155 | ||
154 | struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, | 156 | static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, |
155 | int num_blocks, int wait) | 157 | int num_blocks, int wait) |
156 | { | 158 | { |
157 | struct btrfs_trans_handle *h = | 159 | struct btrfs_trans_handle *h = |
@@ -164,7 +166,7 @@ struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, | |||
164 | ret = join_transaction(root); | 166 | ret = join_transaction(root); |
165 | BUG_ON(ret); | 167 | BUG_ON(ret); |
166 | 168 | ||
167 | record_root_in_trans(root); | 169 | btrfs_record_root_in_trans(root); |
168 | h->transid = root->fs_info->running_transaction->transid; | 170 | h->transid = root->fs_info->running_transaction->transid; |
169 | h->transaction = root->fs_info->running_transaction; | 171 | h->transaction = root->fs_info->running_transaction; |
170 | h->blocks_reserved = num_blocks; | 172 | h->blocks_reserved = num_blocks; |
@@ -456,6 +458,8 @@ static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, | |||
456 | BUG_ON(!root->ref_tree); | 458 | BUG_ON(!root->ref_tree); |
457 | dirty = root->dirty_root; | 459 | dirty = root->dirty_root; |
458 | 460 | ||
461 | btrfs_free_log(trans, root); | ||
462 | |||
459 | if (root->commit_root == root->node) { | 463 | if (root->commit_root == root->node) { |
460 | WARN_ON(root->node->start != | 464 | WARN_ON(root->node->start != |
461 | btrfs_root_bytenr(&root->root_item)); | 465 | btrfs_root_bytenr(&root->root_item)); |
@@ -600,7 +604,7 @@ static noinline int drop_dirty_roots(struct btrfs_root *tree_root, | |||
600 | num_bytes -= btrfs_root_used(&dirty->root->root_item); | 604 | num_bytes -= btrfs_root_used(&dirty->root->root_item); |
601 | bytes_used = btrfs_root_used(&root->root_item); | 605 | bytes_used = btrfs_root_used(&root->root_item); |
602 | if (num_bytes) { | 606 | if (num_bytes) { |
603 | record_root_in_trans(root); | 607 | btrfs_record_root_in_trans(root); |
604 | btrfs_set_root_used(&root->root_item, | 608 | btrfs_set_root_used(&root->root_item, |
605 | bytes_used - num_bytes); | 609 | bytes_used - num_bytes); |
606 | } | 610 | } |
@@ -745,7 +749,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
745 | int ret; | 749 | int ret; |
746 | 750 | ||
747 | INIT_LIST_HEAD(&dirty_fs_roots); | 751 | INIT_LIST_HEAD(&dirty_fs_roots); |
748 | |||
749 | mutex_lock(&root->fs_info->trans_mutex); | 752 | mutex_lock(&root->fs_info->trans_mutex); |
750 | if (trans->transaction->in_commit) { | 753 | if (trans->transaction->in_commit) { |
751 | cur_trans = trans->transaction; | 754 | cur_trans = trans->transaction; |
@@ -821,10 +824,30 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
821 | 824 | ||
822 | WARN_ON(cur_trans != trans->transaction); | 825 | WARN_ON(cur_trans != trans->transaction); |
823 | 826 | ||
827 | /* btrfs_commit_tree_roots is responsible for getting the | ||
828 | * various roots consistent with each other. Every pointer | ||
829 | * in the tree of tree roots has to point to the most up to date | ||
830 | * root for every subvolume and other tree. So, we have to keep | ||
831 | * the tree logging code from jumping in and changing any | ||
832 | * of the trees. | ||
833 | * | ||
834 | * At this point in the commit, there can't be any tree-log | ||
835 | * writers, but a little lower down we drop the trans mutex | ||
836 | * and let new people in. By holding the tree_log_mutex | ||
837 | * from now until after the super is written, we avoid races | ||
838 | * with the tree-log code. | ||
839 | */ | ||
840 | mutex_lock(&root->fs_info->tree_log_mutex); | ||
841 | |||
824 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, | 842 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
825 | &dirty_fs_roots); | 843 | &dirty_fs_roots); |
826 | BUG_ON(ret); | 844 | BUG_ON(ret); |
827 | 845 | ||
846 | /* add_dirty_roots gets rid of all the tree log roots, it is now | ||
847 | * safe to free the root of tree log roots | ||
848 | */ | ||
849 | btrfs_free_log_root_tree(trans, root->fs_info); | ||
850 | |||
828 | ret = btrfs_commit_tree_roots(trans, root); | 851 | ret = btrfs_commit_tree_roots(trans, root); |
829 | BUG_ON(ret); | 852 | BUG_ON(ret); |
830 | 853 | ||
@@ -843,6 +866,12 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
843 | chunk_root->node->start); | 866 | chunk_root->node->start); |
844 | btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, | 867 | btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, |
845 | btrfs_header_level(chunk_root->node)); | 868 | btrfs_header_level(chunk_root->node)); |
869 | |||
870 | if (!root->fs_info->log_root_recovering) { | ||
871 | btrfs_set_super_log_root(&root->fs_info->super_copy, 0); | ||
872 | btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); | ||
873 | } | ||
874 | |||
846 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, | 875 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
847 | sizeof(root->fs_info->super_copy)); | 876 | sizeof(root->fs_info->super_copy)); |
848 | 877 | ||
@@ -857,6 +886,12 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
857 | BUG_ON(ret); | 886 | BUG_ON(ret); |
858 | write_ctree_super(trans, root); | 887 | write_ctree_super(trans, root); |
859 | 888 | ||
889 | /* | ||
890 | * the super is written, we can safely allow the tree-loggers | ||
891 | * to go about their business | ||
892 | */ | ||
893 | mutex_unlock(&root->fs_info->tree_log_mutex); | ||
894 | |||
860 | btrfs_finish_extent_commit(trans, root, pinned_copy); | 895 | btrfs_finish_extent_commit(trans, root, pinned_copy); |
861 | mutex_lock(&root->fs_info->trans_mutex); | 896 | mutex_lock(&root->fs_info->trans_mutex); |
862 | 897 | ||
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h index 598baa312417..cc63650d60d6 100644 --- a/fs/btrfs/transaction.h +++ b/fs/btrfs/transaction.h | |||
@@ -98,4 +98,5 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
98 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, | 98 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, |
99 | struct btrfs_root *root); | 99 | struct btrfs_root *root); |
100 | void btrfs_throttle(struct btrfs_root *root); | 100 | void btrfs_throttle(struct btrfs_root *root); |
101 | int btrfs_record_root_in_trans(struct btrfs_root *root); | ||
101 | #endif | 102 | #endif |
diff --git a/fs/btrfs/tree-defrag.c b/fs/btrfs/tree-defrag.c index cc2650b06952..b3bb5bbad76e 100644 --- a/fs/btrfs/tree-defrag.c +++ b/fs/btrfs/tree-defrag.c | |||
@@ -81,12 +81,12 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, | |||
81 | memcpy(&key, &root->defrag_progress, sizeof(key)); | 81 | memcpy(&key, &root->defrag_progress, sizeof(key)); |
82 | } | 82 | } |
83 | 83 | ||
84 | path->lowest_level = 1; | ||
85 | path->keep_locks = 1; | 84 | path->keep_locks = 1; |
86 | if (cache_only) | 85 | if (cache_only) |
87 | min_trans = root->defrag_trans_start; | 86 | min_trans = root->defrag_trans_start; |
88 | 87 | ||
89 | ret = btrfs_search_forward(root, &key, path, cache_only, min_trans); | 88 | ret = btrfs_search_forward(root, &key, NULL, path, |
89 | cache_only, min_trans); | ||
90 | if (ret < 0) | 90 | if (ret < 0) |
91 | goto out; | 91 | goto out; |
92 | if (ret > 0) { | 92 | if (ret > 0) { |
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c new file mode 100644 index 000000000000..d1ce8314b948 --- /dev/null +++ b/fs/btrfs/tree-log.c | |||
@@ -0,0 +1,2804 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2008 Oracle. All rights reserved. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU General Public | ||
6 | * License v2 as published by the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
11 | * General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public | ||
14 | * License along with this program; if not, write to the | ||
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
16 | * Boston, MA 021110-1307, USA. | ||
17 | */ | ||
18 | |||
19 | #include <linux/sched.h> | ||
20 | #include "ctree.h" | ||
21 | #include "transaction.h" | ||
22 | #include "disk-io.h" | ||
23 | #include "locking.h" | ||
24 | #include "print-tree.h" | ||
25 | #include "compat.h" | ||
26 | |||
27 | /* magic values for the inode_only field in btrfs_log_inode: | ||
28 | * | ||
29 | * LOG_INODE_ALL means to log everything | ||
30 | * LOG_INODE_EXISTS means to log just enough to recreate the inode | ||
31 | * during log replay | ||
32 | */ | ||
33 | #define LOG_INODE_ALL 0 | ||
34 | #define LOG_INODE_EXISTS 1 | ||
35 | |||
36 | /* | ||
37 | * stages for the tree walking. The first | ||
38 | * stage (0) is to only pin down the blocks we find | ||
39 | * the second stage (1) is to make sure that all the inodes | ||
40 | * we find in the log are created in the subvolume. | ||
41 | * | ||
42 | * The last stage is to deal with directories and links and extents | ||
43 | * and all the other fun semantics | ||
44 | */ | ||
45 | #define LOG_WALK_PIN_ONLY 0 | ||
46 | #define LOG_WALK_REPLAY_INODES 1 | ||
47 | #define LOG_WALK_REPLAY_ALL 2 | ||
48 | |||
49 | static int __btrfs_log_inode(struct btrfs_trans_handle *trans, | ||
50 | struct btrfs_root *root, struct inode *inode, | ||
51 | int inode_only); | ||
52 | |||
53 | /* | ||
54 | * tree logging is a special write ahead log used to make sure that | ||
55 | * fsyncs and O_SYNCs can happen without doing full tree commits. | ||
56 | * | ||
57 | * Full tree commits are expensive because they require commonly | ||
58 | * modified blocks to be recowed, creating many dirty pages in the | ||
59 | * extent tree an 4x-6x higher write load than ext3. | ||
60 | * | ||
61 | * Instead of doing a tree commit on every fsync, we use the | ||
62 | * key ranges and transaction ids to find items for a given file or directory | ||
63 | * that have changed in this transaction. Those items are copied into | ||
64 | * a special tree (one per subvolume root), that tree is written to disk | ||
65 | * and then the fsync is considered complete. | ||
66 | * | ||
67 | * After a crash, items are copied out of the log-tree back into the | ||
68 | * subvolume tree. Any file data extents found are recorded in the extent | ||
69 | * allocation tree, and the log-tree freed. | ||
70 | * | ||
71 | * The log tree is read three times, once to pin down all the extents it is | ||
72 | * using in ram and once, once to create all the inodes logged in the tree | ||
73 | * and once to do all the other items. | ||
74 | */ | ||
75 | |||
76 | /* | ||
77 | * btrfs_add_log_tree adds a new per-subvolume log tree into the | ||
78 | * tree of log tree roots. This must be called with a tree log transaction | ||
79 | * running (see start_log_trans). | ||
80 | */ | ||
81 | int btrfs_add_log_tree(struct btrfs_trans_handle *trans, | ||
82 | struct btrfs_root *root) | ||
83 | { | ||
84 | struct btrfs_key key; | ||
85 | struct btrfs_root_item root_item; | ||
86 | struct btrfs_inode_item *inode_item; | ||
87 | struct extent_buffer *leaf; | ||
88 | struct btrfs_root *new_root = root; | ||
89 | int ret; | ||
90 | u64 objectid = root->root_key.objectid; | ||
91 | |||
92 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, | ||
93 | BTRFS_TREE_LOG_OBJECTID, | ||
94 | 0, 0, 0, 0, 0); | ||
95 | if (IS_ERR(leaf)) { | ||
96 | ret = PTR_ERR(leaf); | ||
97 | return ret; | ||
98 | } | ||
99 | |||
100 | btrfs_set_header_nritems(leaf, 0); | ||
101 | btrfs_set_header_level(leaf, 0); | ||
102 | btrfs_set_header_bytenr(leaf, leaf->start); | ||
103 | btrfs_set_header_generation(leaf, trans->transid); | ||
104 | btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); | ||
105 | |||
106 | write_extent_buffer(leaf, root->fs_info->fsid, | ||
107 | (unsigned long)btrfs_header_fsid(leaf), | ||
108 | BTRFS_FSID_SIZE); | ||
109 | btrfs_mark_buffer_dirty(leaf); | ||
110 | |||
111 | inode_item = &root_item.inode; | ||
112 | memset(inode_item, 0, sizeof(*inode_item)); | ||
113 | inode_item->generation = cpu_to_le64(1); | ||
114 | inode_item->size = cpu_to_le64(3); | ||
115 | inode_item->nlink = cpu_to_le32(1); | ||
116 | inode_item->nblocks = cpu_to_le64(1); | ||
117 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); | ||
118 | |||
119 | btrfs_set_root_bytenr(&root_item, leaf->start); | ||
120 | btrfs_set_root_level(&root_item, 0); | ||
121 | btrfs_set_root_refs(&root_item, 0); | ||
122 | btrfs_set_root_used(&root_item, 0); | ||
123 | |||
124 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); | ||
125 | root_item.drop_level = 0; | ||
126 | |||
127 | btrfs_tree_unlock(leaf); | ||
128 | free_extent_buffer(leaf); | ||
129 | leaf = NULL; | ||
130 | |||
131 | btrfs_set_root_dirid(&root_item, 0); | ||
132 | |||
133 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | ||
134 | key.offset = objectid; | ||
135 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | ||
136 | ret = btrfs_insert_root(trans, root->fs_info->log_root_tree, &key, | ||
137 | &root_item); | ||
138 | if (ret) | ||
139 | goto fail; | ||
140 | |||
141 | new_root = btrfs_read_fs_root_no_radix(root->fs_info->log_root_tree, | ||
142 | &key); | ||
143 | BUG_ON(!new_root); | ||
144 | |||
145 | WARN_ON(root->log_root); | ||
146 | root->log_root = new_root; | ||
147 | |||
148 | /* | ||
149 | * log trees do not get reference counted because they go away | ||
150 | * before a real commit is actually done. They do store pointers | ||
151 | * to file data extents, and those reference counts still get | ||
152 | * updated (along with back refs to the log tree). | ||
153 | */ | ||
154 | new_root->ref_cows = 0; | ||
155 | new_root->last_trans = trans->transid; | ||
156 | fail: | ||
157 | return ret; | ||
158 | } | ||
159 | |||
160 | /* | ||
161 | * start a sub transaction and setup the log tree | ||
162 | * this increments the log tree writer count to make the people | ||
163 | * syncing the tree wait for us to finish | ||
164 | */ | ||
165 | static int start_log_trans(struct btrfs_trans_handle *trans, | ||
166 | struct btrfs_root *root) | ||
167 | { | ||
168 | int ret; | ||
169 | mutex_lock(&root->fs_info->tree_log_mutex); | ||
170 | if (!root->fs_info->log_root_tree) { | ||
171 | ret = btrfs_init_log_root_tree(trans, root->fs_info); | ||
172 | BUG_ON(ret); | ||
173 | } | ||
174 | if (!root->log_root) { | ||
175 | ret = btrfs_add_log_tree(trans, root); | ||
176 | BUG_ON(ret); | ||
177 | } | ||
178 | atomic_inc(&root->fs_info->tree_log_writers); | ||
179 | root->fs_info->tree_log_batch++; | ||
180 | mutex_unlock(&root->fs_info->tree_log_mutex); | ||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | /* | ||
185 | * returns 0 if there was a log transaction running and we were able | ||
186 | * to join, or returns -ENOENT if there were not transactions | ||
187 | * in progress | ||
188 | */ | ||
189 | static int join_running_log_trans(struct btrfs_root *root) | ||
190 | { | ||
191 | int ret = -ENOENT; | ||
192 | |||
193 | smp_mb(); | ||
194 | if (!root->log_root) | ||
195 | return -ENOENT; | ||
196 | |||
197 | mutex_lock(&root->fs_info->tree_log_mutex); | ||
198 | if (root->log_root) { | ||
199 | ret = 0; | ||
200 | atomic_inc(&root->fs_info->tree_log_writers); | ||
201 | root->fs_info->tree_log_batch++; | ||
202 | } | ||
203 | mutex_unlock(&root->fs_info->tree_log_mutex); | ||
204 | return ret; | ||
205 | } | ||
206 | |||
207 | /* | ||
208 | * indicate we're done making changes to the log tree | ||
209 | * and wake up anyone waiting to do a sync | ||
210 | */ | ||
211 | static int end_log_trans(struct btrfs_root *root) | ||
212 | { | ||
213 | atomic_dec(&root->fs_info->tree_log_writers); | ||
214 | smp_mb(); | ||
215 | if (waitqueue_active(&root->fs_info->tree_log_wait)) | ||
216 | wake_up(&root->fs_info->tree_log_wait); | ||
217 | return 0; | ||
218 | } | ||
219 | |||
220 | |||
221 | /* | ||
222 | * the walk control struct is used to pass state down the chain when | ||
223 | * processing the log tree. The stage field tells us which part | ||
224 | * of the log tree processing we are currently doing. The others | ||
225 | * are state fields used for that specific part | ||
226 | */ | ||
227 | struct walk_control { | ||
228 | /* should we free the extent on disk when done? This is used | ||
229 | * at transaction commit time while freeing a log tree | ||
230 | */ | ||
231 | int free; | ||
232 | |||
233 | /* should we write out the extent buffer? This is used | ||
234 | * while flushing the log tree to disk during a sync | ||
235 | */ | ||
236 | int write; | ||
237 | |||
238 | /* should we wait for the extent buffer io to finish? Also used | ||
239 | * while flushing the log tree to disk for a sync | ||
240 | */ | ||
241 | int wait; | ||
242 | |||
243 | /* pin only walk, we record which extents on disk belong to the | ||
244 | * log trees | ||
245 | */ | ||
246 | int pin; | ||
247 | |||
248 | /* what stage of the replay code we're currently in */ | ||
249 | int stage; | ||
250 | |||
251 | /* the root we are currently replaying */ | ||
252 | struct btrfs_root *replay_dest; | ||
253 | |||
254 | /* the trans handle for the current replay */ | ||
255 | struct btrfs_trans_handle *trans; | ||
256 | |||
257 | /* the function that gets used to process blocks we find in the | ||
258 | * tree. Note the extent_buffer might not be up to date when it is | ||
259 | * passed in, and it must be checked or read if you need the data | ||
260 | * inside it | ||
261 | */ | ||
262 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, | ||
263 | struct walk_control *wc, u64 gen); | ||
264 | }; | ||
265 | |||
266 | /* | ||
267 | * process_func used to pin down extents, write them or wait on them | ||
268 | */ | ||
269 | static int process_one_buffer(struct btrfs_root *log, | ||
270 | struct extent_buffer *eb, | ||
271 | struct walk_control *wc, u64 gen) | ||
272 | { | ||
273 | if (wc->pin) { | ||
274 | mutex_lock(&log->fs_info->alloc_mutex); | ||
275 | btrfs_update_pinned_extents(log->fs_info->extent_root, | ||
276 | eb->start, eb->len, 1); | ||
277 | mutex_unlock(&log->fs_info->alloc_mutex); | ||
278 | } | ||
279 | |||
280 | if (btrfs_buffer_uptodate(eb, gen)) { | ||
281 | if (wc->write) | ||
282 | btrfs_write_tree_block(eb); | ||
283 | if (wc->wait) | ||
284 | btrfs_wait_tree_block_writeback(eb); | ||
285 | } | ||
286 | return 0; | ||
287 | } | ||
288 | |||
289 | /* | ||
290 | * Item overwrite used by replay and tree logging. eb, slot and key all refer | ||
291 | * to the src data we are copying out. | ||
292 | * | ||
293 | * root is the tree we are copying into, and path is a scratch | ||
294 | * path for use in this function (it should be released on entry and | ||
295 | * will be released on exit). | ||
296 | * | ||
297 | * If the key is already in the destination tree the existing item is | ||
298 | * overwritten. If the existing item isn't big enough, it is extended. | ||
299 | * If it is too large, it is truncated. | ||
300 | * | ||
301 | * If the key isn't in the destination yet, a new item is inserted. | ||
302 | */ | ||
303 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, | ||
304 | struct btrfs_root *root, | ||
305 | struct btrfs_path *path, | ||
306 | struct extent_buffer *eb, int slot, | ||
307 | struct btrfs_key *key) | ||
308 | { | ||
309 | int ret; | ||
310 | u32 item_size; | ||
311 | u64 saved_i_size = 0; | ||
312 | int save_old_i_size = 0; | ||
313 | unsigned long src_ptr; | ||
314 | unsigned long dst_ptr; | ||
315 | int overwrite_root = 0; | ||
316 | |||
317 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | ||
318 | overwrite_root = 1; | ||
319 | |||
320 | item_size = btrfs_item_size_nr(eb, slot); | ||
321 | src_ptr = btrfs_item_ptr_offset(eb, slot); | ||
322 | |||
323 | /* look for the key in the destination tree */ | ||
324 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | ||
325 | if (ret == 0) { | ||
326 | char *src_copy; | ||
327 | char *dst_copy; | ||
328 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], | ||
329 | path->slots[0]); | ||
330 | if (dst_size != item_size) | ||
331 | goto insert; | ||
332 | |||
333 | if (item_size == 0) { | ||
334 | btrfs_release_path(root, path); | ||
335 | return 0; | ||
336 | } | ||
337 | dst_copy = kmalloc(item_size, GFP_NOFS); | ||
338 | src_copy = kmalloc(item_size, GFP_NOFS); | ||
339 | |||
340 | read_extent_buffer(eb, src_copy, src_ptr, item_size); | ||
341 | |||
342 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | ||
343 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, | ||
344 | item_size); | ||
345 | ret = memcmp(dst_copy, src_copy, item_size); | ||
346 | |||
347 | kfree(dst_copy); | ||
348 | kfree(src_copy); | ||
349 | /* | ||
350 | * they have the same contents, just return, this saves | ||
351 | * us from cowing blocks in the destination tree and doing | ||
352 | * extra writes that may not have been done by a previous | ||
353 | * sync | ||
354 | */ | ||
355 | if (ret == 0) { | ||
356 | btrfs_release_path(root, path); | ||
357 | return 0; | ||
358 | } | ||
359 | |||
360 | } | ||
361 | insert: | ||
362 | btrfs_release_path(root, path); | ||
363 | /* try to insert the key into the destination tree */ | ||
364 | ret = btrfs_insert_empty_item(trans, root, path, | ||
365 | key, item_size); | ||
366 | |||
367 | /* make sure any existing item is the correct size */ | ||
368 | if (ret == -EEXIST) { | ||
369 | u32 found_size; | ||
370 | found_size = btrfs_item_size_nr(path->nodes[0], | ||
371 | path->slots[0]); | ||
372 | if (found_size > item_size) { | ||
373 | btrfs_truncate_item(trans, root, path, item_size, 1); | ||
374 | } else if (found_size < item_size) { | ||
375 | ret = btrfs_del_item(trans, root, | ||
376 | path); | ||
377 | BUG_ON(ret); | ||
378 | |||
379 | btrfs_release_path(root, path); | ||
380 | ret = btrfs_insert_empty_item(trans, | ||
381 | root, path, key, item_size); | ||
382 | BUG_ON(ret); | ||
383 | } | ||
384 | } else if (ret) { | ||
385 | BUG(); | ||
386 | } | ||
387 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], | ||
388 | path->slots[0]); | ||
389 | |||
390 | /* don't overwrite an existing inode if the generation number | ||
391 | * was logged as zero. This is done when the tree logging code | ||
392 | * is just logging an inode to make sure it exists after recovery. | ||
393 | * | ||
394 | * Also, don't overwrite i_size on directories during replay. | ||
395 | * log replay inserts and removes directory items based on the | ||
396 | * state of the tree found in the subvolume, and i_size is modified | ||
397 | * as it goes | ||
398 | */ | ||
399 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { | ||
400 | struct btrfs_inode_item *src_item; | ||
401 | struct btrfs_inode_item *dst_item; | ||
402 | |||
403 | src_item = (struct btrfs_inode_item *)src_ptr; | ||
404 | dst_item = (struct btrfs_inode_item *)dst_ptr; | ||
405 | |||
406 | if (btrfs_inode_generation(eb, src_item) == 0) | ||
407 | goto no_copy; | ||
408 | |||
409 | if (overwrite_root && | ||
410 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && | ||
411 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { | ||
412 | save_old_i_size = 1; | ||
413 | saved_i_size = btrfs_inode_size(path->nodes[0], | ||
414 | dst_item); | ||
415 | } | ||
416 | } | ||
417 | |||
418 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, | ||
419 | src_ptr, item_size); | ||
420 | |||
421 | if (save_old_i_size) { | ||
422 | struct btrfs_inode_item *dst_item; | ||
423 | dst_item = (struct btrfs_inode_item *)dst_ptr; | ||
424 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); | ||
425 | } | ||
426 | |||
427 | /* make sure the generation is filled in */ | ||
428 | if (key->type == BTRFS_INODE_ITEM_KEY) { | ||
429 | struct btrfs_inode_item *dst_item; | ||
430 | dst_item = (struct btrfs_inode_item *)dst_ptr; | ||
431 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { | ||
432 | btrfs_set_inode_generation(path->nodes[0], dst_item, | ||
433 | trans->transid); | ||
434 | } | ||
435 | } | ||
436 | no_copy: | ||
437 | btrfs_mark_buffer_dirty(path->nodes[0]); | ||
438 | btrfs_release_path(root, path); | ||
439 | return 0; | ||
440 | } | ||
441 | |||
442 | /* | ||
443 | * simple helper to read an inode off the disk from a given root | ||
444 | * This can only be called for subvolume roots and not for the log | ||
445 | */ | ||
446 | static noinline struct inode *read_one_inode(struct btrfs_root *root, | ||
447 | u64 objectid) | ||
448 | { | ||
449 | struct inode *inode; | ||
450 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); | ||
451 | if (inode->i_state & I_NEW) { | ||
452 | BTRFS_I(inode)->root = root; | ||
453 | BTRFS_I(inode)->location.objectid = objectid; | ||
454 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | ||
455 | BTRFS_I(inode)->location.offset = 0; | ||
456 | btrfs_read_locked_inode(inode); | ||
457 | unlock_new_inode(inode); | ||
458 | |||
459 | } | ||
460 | if (is_bad_inode(inode)) { | ||
461 | iput(inode); | ||
462 | inode = NULL; | ||
463 | } | ||
464 | return inode; | ||
465 | } | ||
466 | |||
467 | /* replays a single extent in 'eb' at 'slot' with 'key' into the | ||
468 | * subvolume 'root'. path is released on entry and should be released | ||
469 | * on exit. | ||
470 | * | ||
471 | * extents in the log tree have not been allocated out of the extent | ||
472 | * tree yet. So, this completes the allocation, taking a reference | ||
473 | * as required if the extent already exists or creating a new extent | ||
474 | * if it isn't in the extent allocation tree yet. | ||
475 | * | ||
476 | * The extent is inserted into the file, dropping any existing extents | ||
477 | * from the file that overlap the new one. | ||
478 | */ | ||
479 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | ||
480 | struct btrfs_root *root, | ||
481 | struct btrfs_path *path, | ||
482 | struct extent_buffer *eb, int slot, | ||
483 | struct btrfs_key *key) | ||
484 | { | ||
485 | int found_type; | ||
486 | u64 mask = root->sectorsize - 1; | ||
487 | u64 extent_end; | ||
488 | u64 alloc_hint; | ||
489 | u64 start = key->offset; | ||
490 | struct btrfs_file_extent_item *item; | ||
491 | struct inode *inode = NULL; | ||
492 | unsigned long size; | ||
493 | int ret = 0; | ||
494 | |||
495 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | ||
496 | found_type = btrfs_file_extent_type(eb, item); | ||
497 | |||
498 | if (found_type == BTRFS_FILE_EXTENT_REG) | ||
499 | extent_end = start + btrfs_file_extent_num_bytes(eb, item); | ||
500 | else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | ||
501 | size = btrfs_file_extent_inline_len(eb, | ||
502 | btrfs_item_nr(eb, slot)); | ||
503 | extent_end = (start + size + mask) & ~mask; | ||
504 | } else { | ||
505 | ret = 0; | ||
506 | goto out; | ||
507 | } | ||
508 | |||
509 | inode = read_one_inode(root, key->objectid); | ||
510 | if (!inode) { | ||
511 | ret = -EIO; | ||
512 | goto out; | ||
513 | } | ||
514 | |||
515 | /* | ||
516 | * first check to see if we already have this extent in the | ||
517 | * file. This must be done before the btrfs_drop_extents run | ||
518 | * so we don't try to drop this extent. | ||
519 | */ | ||
520 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, | ||
521 | start, 0); | ||
522 | |||
523 | if (ret == 0 && found_type == BTRFS_FILE_EXTENT_REG) { | ||
524 | struct btrfs_file_extent_item cmp1; | ||
525 | struct btrfs_file_extent_item cmp2; | ||
526 | struct btrfs_file_extent_item *existing; | ||
527 | struct extent_buffer *leaf; | ||
528 | |||
529 | leaf = path->nodes[0]; | ||
530 | existing = btrfs_item_ptr(leaf, path->slots[0], | ||
531 | struct btrfs_file_extent_item); | ||
532 | |||
533 | read_extent_buffer(eb, &cmp1, (unsigned long)item, | ||
534 | sizeof(cmp1)); | ||
535 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, | ||
536 | sizeof(cmp2)); | ||
537 | |||
538 | /* | ||
539 | * we already have a pointer to this exact extent, | ||
540 | * we don't have to do anything | ||
541 | */ | ||
542 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { | ||
543 | btrfs_release_path(root, path); | ||
544 | goto out; | ||
545 | } | ||
546 | } | ||
547 | btrfs_release_path(root, path); | ||
548 | |||
549 | /* drop any overlapping extents */ | ||
550 | ret = btrfs_drop_extents(trans, root, inode, | ||
551 | start, extent_end, start, &alloc_hint); | ||
552 | BUG_ON(ret); | ||
553 | |||
554 | BUG_ON(ret); | ||
555 | if (found_type == BTRFS_FILE_EXTENT_REG) { | ||
556 | struct btrfs_key ins; | ||
557 | |||
558 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); | ||
559 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); | ||
560 | ins.type = BTRFS_EXTENT_ITEM_KEY; | ||
561 | |||
562 | /* insert the extent pointer in the file */ | ||
563 | ret = overwrite_item(trans, root, path, eb, slot, key); | ||
564 | BUG_ON(ret); | ||
565 | |||
566 | /* | ||
567 | * is this extent already allocated in the extent | ||
568 | * allocation tree? If so, just add a reference | ||
569 | */ | ||
570 | ret = btrfs_lookup_extent(root, path, ins.objectid, ins.offset); | ||
571 | btrfs_release_path(root, path); | ||
572 | if (ret == 0) { | ||
573 | ret = btrfs_inc_extent_ref(trans, root, | ||
574 | ins.objectid, ins.offset, | ||
575 | root->root_key.objectid, | ||
576 | trans->transid, key->objectid, start); | ||
577 | } else { | ||
578 | /* | ||
579 | * insert the extent pointer in the extent | ||
580 | * allocation tree | ||
581 | */ | ||
582 | ret = btrfs_alloc_logged_extent(trans, root, | ||
583 | root->root_key.objectid, | ||
584 | trans->transid, key->objectid, | ||
585 | start, &ins); | ||
586 | BUG_ON(ret); | ||
587 | } | ||
588 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | ||
589 | /* inline extents are easy, we just overwrite them */ | ||
590 | ret = overwrite_item(trans, root, path, eb, slot, key); | ||
591 | BUG_ON(ret); | ||
592 | } | ||
593 | /* btrfs_drop_extents changes i_blocks, update it here */ | ||
594 | inode->i_blocks += (extent_end - start) >> 9; | ||
595 | btrfs_update_inode(trans, root, inode); | ||
596 | out: | ||
597 | if (inode) | ||
598 | iput(inode); | ||
599 | return ret; | ||
600 | } | ||
601 | |||
602 | /* | ||
603 | * when cleaning up conflicts between the directory names in the | ||
604 | * subvolume, directory names in the log and directory names in the | ||
605 | * inode back references, we may have to unlink inodes from directories. | ||
606 | * | ||
607 | * This is a helper function to do the unlink of a specific directory | ||
608 | * item | ||
609 | */ | ||
610 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, | ||
611 | struct btrfs_root *root, | ||
612 | struct btrfs_path *path, | ||
613 | struct inode *dir, | ||
614 | struct btrfs_dir_item *di) | ||
615 | { | ||
616 | struct inode *inode; | ||
617 | char *name; | ||
618 | int name_len; | ||
619 | struct extent_buffer *leaf; | ||
620 | struct btrfs_key location; | ||
621 | int ret; | ||
622 | |||
623 | leaf = path->nodes[0]; | ||
624 | |||
625 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | ||
626 | name_len = btrfs_dir_name_len(leaf, di); | ||
627 | name = kmalloc(name_len, GFP_NOFS); | ||
628 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); | ||
629 | btrfs_release_path(root, path); | ||
630 | |||
631 | inode = read_one_inode(root, location.objectid); | ||
632 | BUG_ON(!inode); | ||
633 | |||
634 | btrfs_inc_nlink(inode); | ||
635 | ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | ||
636 | kfree(name); | ||
637 | |||
638 | iput(inode); | ||
639 | return ret; | ||
640 | } | ||
641 | |||
642 | /* | ||
643 | * helper function to see if a given name and sequence number found | ||
644 | * in an inode back reference are already in a directory and correctly | ||
645 | * point to this inode | ||
646 | */ | ||
647 | static noinline int inode_in_dir(struct btrfs_root *root, | ||
648 | struct btrfs_path *path, | ||
649 | u64 dirid, u64 objectid, u64 index, | ||
650 | const char *name, int name_len) | ||
651 | { | ||
652 | struct btrfs_dir_item *di; | ||
653 | struct btrfs_key location; | ||
654 | int match = 0; | ||
655 | |||
656 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, | ||
657 | index, name, name_len, 0); | ||
658 | if (di && !IS_ERR(di)) { | ||
659 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | ||
660 | if (location.objectid != objectid) | ||
661 | goto out; | ||
662 | } else | ||
663 | goto out; | ||
664 | btrfs_release_path(root, path); | ||
665 | |||
666 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); | ||
667 | if (di && !IS_ERR(di)) { | ||
668 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | ||
669 | if (location.objectid != objectid) | ||
670 | goto out; | ||
671 | } else | ||
672 | goto out; | ||
673 | match = 1; | ||
674 | out: | ||
675 | btrfs_release_path(root, path); | ||
676 | return match; | ||
677 | } | ||
678 | |||
679 | /* | ||
680 | * helper function to check a log tree for a named back reference in | ||
681 | * an inode. This is used to decide if a back reference that is | ||
682 | * found in the subvolume conflicts with what we find in the log. | ||
683 | * | ||
684 | * inode backreferences may have multiple refs in a single item, | ||
685 | * during replay we process one reference at a time, and we don't | ||
686 | * want to delete valid links to a file from the subvolume if that | ||
687 | * link is also in the log. | ||
688 | */ | ||
689 | static noinline int backref_in_log(struct btrfs_root *log, | ||
690 | struct btrfs_key *key, | ||
691 | char *name, int namelen) | ||
692 | { | ||
693 | struct btrfs_path *path; | ||
694 | struct btrfs_inode_ref *ref; | ||
695 | unsigned long ptr; | ||
696 | unsigned long ptr_end; | ||
697 | unsigned long name_ptr; | ||
698 | int found_name_len; | ||
699 | int item_size; | ||
700 | int ret; | ||
701 | int match = 0; | ||
702 | |||
703 | path = btrfs_alloc_path(); | ||
704 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); | ||
705 | if (ret != 0) | ||
706 | goto out; | ||
707 | |||
708 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); | ||
709 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | ||
710 | ptr_end = ptr + item_size; | ||
711 | while (ptr < ptr_end) { | ||
712 | ref = (struct btrfs_inode_ref *)ptr; | ||
713 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); | ||
714 | if (found_name_len == namelen) { | ||
715 | name_ptr = (unsigned long)(ref + 1); | ||
716 | ret = memcmp_extent_buffer(path->nodes[0], name, | ||
717 | name_ptr, namelen); | ||
718 | if (ret == 0) { | ||
719 | match = 1; | ||
720 | goto out; | ||
721 | } | ||
722 | } | ||
723 | ptr = (unsigned long)(ref + 1) + found_name_len; | ||
724 | } | ||
725 | out: | ||
726 | btrfs_free_path(path); | ||
727 | return match; | ||
728 | } | ||
729 | |||
730 | |||
731 | /* | ||
732 | * replay one inode back reference item found in the log tree. | ||
733 | * eb, slot and key refer to the buffer and key found in the log tree. | ||
734 | * root is the destination we are replaying into, and path is for temp | ||
735 | * use by this function. (it should be released on return). | ||
736 | */ | ||
737 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | ||
738 | struct btrfs_root *root, | ||
739 | struct btrfs_root *log, | ||
740 | struct btrfs_path *path, | ||
741 | struct extent_buffer *eb, int slot, | ||
742 | struct btrfs_key *key) | ||
743 | { | ||
744 | struct inode *dir; | ||
745 | int ret; | ||
746 | struct btrfs_key location; | ||
747 | struct btrfs_inode_ref *ref; | ||
748 | struct btrfs_dir_item *di; | ||
749 | struct inode *inode; | ||
750 | char *name; | ||
751 | int namelen; | ||
752 | unsigned long ref_ptr; | ||
753 | unsigned long ref_end; | ||
754 | |||
755 | location.objectid = key->objectid; | ||
756 | location.type = BTRFS_INODE_ITEM_KEY; | ||
757 | location.offset = 0; | ||
758 | |||
759 | /* | ||
760 | * it is possible that we didn't log all the parent directories | ||
761 | * for a given inode. If we don't find the dir, just don't | ||
762 | * copy the back ref in. The link count fixup code will take | ||
763 | * care of the rest | ||
764 | */ | ||
765 | dir = read_one_inode(root, key->offset); | ||
766 | if (!dir) | ||
767 | return -ENOENT; | ||
768 | |||
769 | inode = read_one_inode(root, key->objectid); | ||
770 | BUG_ON(!dir); | ||
771 | |||
772 | ref_ptr = btrfs_item_ptr_offset(eb, slot); | ||
773 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); | ||
774 | |||
775 | again: | ||
776 | ref = (struct btrfs_inode_ref *)ref_ptr; | ||
777 | |||
778 | namelen = btrfs_inode_ref_name_len(eb, ref); | ||
779 | name = kmalloc(namelen, GFP_NOFS); | ||
780 | BUG_ON(!name); | ||
781 | |||
782 | read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen); | ||
783 | |||
784 | /* if we already have a perfect match, we're done */ | ||
785 | if (inode_in_dir(root, path, dir->i_ino, inode->i_ino, | ||
786 | btrfs_inode_ref_index(eb, ref), | ||
787 | name, namelen)) { | ||
788 | goto out; | ||
789 | } | ||
790 | |||
791 | /* | ||
792 | * look for a conflicting back reference in the metadata. | ||
793 | * if we find one we have to unlink that name of the file | ||
794 | * before we add our new link. Later on, we overwrite any | ||
795 | * existing back reference, and we don't want to create | ||
796 | * dangling pointers in the directory. | ||
797 | */ | ||
798 | conflict_again: | ||
799 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | ||
800 | if (ret == 0) { | ||
801 | char *victim_name; | ||
802 | int victim_name_len; | ||
803 | struct btrfs_inode_ref *victim_ref; | ||
804 | unsigned long ptr; | ||
805 | unsigned long ptr_end; | ||
806 | struct extent_buffer *leaf = path->nodes[0]; | ||
807 | |||
808 | /* are we trying to overwrite a back ref for the root directory | ||
809 | * if so, just jump out, we're done | ||
810 | */ | ||
811 | if (key->objectid == key->offset) | ||
812 | goto out_nowrite; | ||
813 | |||
814 | /* check all the names in this back reference to see | ||
815 | * if they are in the log. if so, we allow them to stay | ||
816 | * otherwise they must be unlinked as a conflict | ||
817 | */ | ||
818 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | ||
819 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); | ||
820 | while(ptr < ptr_end) { | ||
821 | victim_ref = (struct btrfs_inode_ref *)ptr; | ||
822 | victim_name_len = btrfs_inode_ref_name_len(leaf, | ||
823 | victim_ref); | ||
824 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | ||
825 | BUG_ON(!victim_name); | ||
826 | |||
827 | read_extent_buffer(leaf, victim_name, | ||
828 | (unsigned long)(victim_ref + 1), | ||
829 | victim_name_len); | ||
830 | |||
831 | if (!backref_in_log(log, key, victim_name, | ||
832 | victim_name_len)) { | ||
833 | btrfs_inc_nlink(inode); | ||
834 | btrfs_release_path(root, path); | ||
835 | ret = btrfs_unlink_inode(trans, root, dir, | ||
836 | inode, victim_name, | ||
837 | victim_name_len); | ||
838 | kfree(victim_name); | ||
839 | btrfs_release_path(root, path); | ||
840 | goto conflict_again; | ||
841 | } | ||
842 | kfree(victim_name); | ||
843 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; | ||
844 | } | ||
845 | BUG_ON(ret); | ||
846 | } | ||
847 | btrfs_release_path(root, path); | ||
848 | |||
849 | /* look for a conflicting sequence number */ | ||
850 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, | ||
851 | btrfs_inode_ref_index(eb, ref), | ||
852 | name, namelen, 0); | ||
853 | if (di && !IS_ERR(di)) { | ||
854 | ret = drop_one_dir_item(trans, root, path, dir, di); | ||
855 | BUG_ON(ret); | ||
856 | } | ||
857 | btrfs_release_path(root, path); | ||
858 | |||
859 | |||
860 | /* look for a conflicting name */ | ||
861 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, | ||
862 | name, namelen, 0); | ||
863 | if (di && !IS_ERR(di)) { | ||
864 | ret = drop_one_dir_item(trans, root, path, dir, di); | ||
865 | BUG_ON(ret); | ||
866 | } | ||
867 | btrfs_release_path(root, path); | ||
868 | |||
869 | /* insert our name */ | ||
870 | ret = btrfs_add_link(trans, dir, inode, name, namelen, 0, | ||
871 | btrfs_inode_ref_index(eb, ref)); | ||
872 | BUG_ON(ret); | ||
873 | |||
874 | btrfs_update_inode(trans, root, inode); | ||
875 | |||
876 | out: | ||
877 | ref_ptr = (unsigned long)(ref + 1) + namelen; | ||
878 | kfree(name); | ||
879 | if (ref_ptr < ref_end) | ||
880 | goto again; | ||
881 | |||
882 | /* finally write the back reference in the inode */ | ||
883 | ret = overwrite_item(trans, root, path, eb, slot, key); | ||
884 | BUG_ON(ret); | ||
885 | |||
886 | out_nowrite: | ||
887 | btrfs_release_path(root, path); | ||
888 | iput(dir); | ||
889 | iput(inode); | ||
890 | return 0; | ||
891 | } | ||
892 | |||
893 | /* | ||
894 | * replay one csum item from the log tree into the subvolume 'root' | ||
895 | * eb, slot and key all refer to the log tree | ||
896 | * path is for temp use by this function and should be released on return | ||
897 | * | ||
898 | * This copies the checksums out of the log tree and inserts them into | ||
899 | * the subvolume. Any existing checksums for this range in the file | ||
900 | * are overwritten, and new items are added where required. | ||
901 | * | ||
902 | * We keep this simple by reusing the btrfs_ordered_sum code from | ||
903 | * the data=ordered mode. This basically means making a copy | ||
904 | * of all the checksums in ram, which we have to do anyway for kmap | ||
905 | * rules. | ||
906 | * | ||
907 | * The copy is then sent down to btrfs_csum_file_blocks, which | ||
908 | * does all the hard work of finding existing items in the file | ||
909 | * or adding new ones. | ||
910 | */ | ||
911 | static noinline int replay_one_csum(struct btrfs_trans_handle *trans, | ||
912 | struct btrfs_root *root, | ||
913 | struct btrfs_path *path, | ||
914 | struct extent_buffer *eb, int slot, | ||
915 | struct btrfs_key *key) | ||
916 | { | ||
917 | int ret; | ||
918 | u32 item_size = btrfs_item_size_nr(eb, slot); | ||
919 | u64 cur_offset; | ||
920 | unsigned long file_bytes; | ||
921 | struct btrfs_ordered_sum *sums; | ||
922 | struct btrfs_sector_sum *sector_sum; | ||
923 | struct inode *inode; | ||
924 | unsigned long ptr; | ||
925 | |||
926 | file_bytes = (item_size / BTRFS_CRC32_SIZE) * root->sectorsize; | ||
927 | inode = read_one_inode(root, key->objectid); | ||
928 | if (!inode) { | ||
929 | return -EIO; | ||
930 | } | ||
931 | |||
932 | sums = kzalloc(btrfs_ordered_sum_size(root, file_bytes), GFP_NOFS); | ||
933 | if (!sums) { | ||
934 | iput(inode); | ||
935 | return -ENOMEM; | ||
936 | } | ||
937 | |||
938 | INIT_LIST_HEAD(&sums->list); | ||
939 | sums->len = file_bytes; | ||
940 | sums->file_offset = key->offset; | ||
941 | |||
942 | /* | ||
943 | * copy all the sums into the ordered sum struct | ||
944 | */ | ||
945 | sector_sum = sums->sums; | ||
946 | cur_offset = key->offset; | ||
947 | ptr = btrfs_item_ptr_offset(eb, slot); | ||
948 | while(item_size > 0) { | ||
949 | sector_sum->offset = cur_offset; | ||
950 | read_extent_buffer(eb, §or_sum->sum, ptr, BTRFS_CRC32_SIZE); | ||
951 | sector_sum++; | ||
952 | item_size -= BTRFS_CRC32_SIZE; | ||
953 | ptr += BTRFS_CRC32_SIZE; | ||
954 | cur_offset += root->sectorsize; | ||
955 | } | ||
956 | |||
957 | /* let btrfs_csum_file_blocks add them into the file */ | ||
958 | ret = btrfs_csum_file_blocks(trans, root, inode, sums); | ||
959 | BUG_ON(ret); | ||
960 | kfree(sums); | ||
961 | iput(inode); | ||
962 | |||
963 | return 0; | ||
964 | } | ||
965 | /* | ||
966 | * There are a few corners where the link count of the file can't | ||
967 | * be properly maintained during replay. So, instead of adding | ||
968 | * lots of complexity to the log code, we just scan the backrefs | ||
969 | * for any file that has been through replay. | ||
970 | * | ||
971 | * The scan will update the link count on the inode to reflect the | ||
972 | * number of back refs found. If it goes down to zero, the iput | ||
973 | * will free the inode. | ||
974 | */ | ||
975 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, | ||
976 | struct btrfs_root *root, | ||
977 | struct inode *inode) | ||
978 | { | ||
979 | struct btrfs_path *path; | ||
980 | int ret; | ||
981 | struct btrfs_key key; | ||
982 | u64 nlink = 0; | ||
983 | unsigned long ptr; | ||
984 | unsigned long ptr_end; | ||
985 | int name_len; | ||
986 | |||
987 | key.objectid = inode->i_ino; | ||
988 | key.type = BTRFS_INODE_REF_KEY; | ||
989 | key.offset = (u64)-1; | ||
990 | |||
991 | path = btrfs_alloc_path(); | ||
992 | |||
993 | while(1) { | ||
994 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
995 | if (ret < 0) | ||
996 | break; | ||
997 | if (ret > 0) { | ||
998 | if (path->slots[0] == 0) | ||
999 | break; | ||
1000 | path->slots[0]--; | ||
1001 | } | ||
1002 | btrfs_item_key_to_cpu(path->nodes[0], &key, | ||
1003 | path->slots[0]); | ||
1004 | if (key.objectid != inode->i_ino || | ||
1005 | key.type != BTRFS_INODE_REF_KEY) | ||
1006 | break; | ||
1007 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | ||
1008 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], | ||
1009 | path->slots[0]); | ||
1010 | while(ptr < ptr_end) { | ||
1011 | struct btrfs_inode_ref *ref; | ||
1012 | |||
1013 | ref = (struct btrfs_inode_ref *)ptr; | ||
1014 | name_len = btrfs_inode_ref_name_len(path->nodes[0], | ||
1015 | ref); | ||
1016 | ptr = (unsigned long)(ref + 1) + name_len; | ||
1017 | nlink++; | ||
1018 | } | ||
1019 | |||
1020 | if (key.offset == 0) | ||
1021 | break; | ||
1022 | key.offset--; | ||
1023 | btrfs_release_path(root, path); | ||
1024 | } | ||
1025 | btrfs_free_path(path); | ||
1026 | if (nlink != inode->i_nlink) { | ||
1027 | inode->i_nlink = nlink; | ||
1028 | btrfs_update_inode(trans, root, inode); | ||
1029 | } | ||
1030 | |||
1031 | return 0; | ||
1032 | } | ||
1033 | |||
1034 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, | ||
1035 | struct btrfs_root *root, | ||
1036 | struct btrfs_path *path) | ||
1037 | { | ||
1038 | int ret; | ||
1039 | struct btrfs_key key; | ||
1040 | struct inode *inode; | ||
1041 | |||
1042 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | ||
1043 | key.type = BTRFS_ORPHAN_ITEM_KEY; | ||
1044 | key.offset = (u64)-1; | ||
1045 | while(1) { | ||
1046 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | ||
1047 | if (ret < 0) | ||
1048 | break; | ||
1049 | |||
1050 | if (ret == 1) { | ||
1051 | if (path->slots[0] == 0) | ||
1052 | break; | ||
1053 | path->slots[0]--; | ||
1054 | } | ||
1055 | |||
1056 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | ||
1057 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || | ||
1058 | key.type != BTRFS_ORPHAN_ITEM_KEY) | ||
1059 | break; | ||
1060 | |||
1061 | ret = btrfs_del_item(trans, root, path); | ||
1062 | BUG_ON(ret); | ||
1063 | |||
1064 | btrfs_release_path(root, path); | ||
1065 | inode = read_one_inode(root, key.offset); | ||
1066 | BUG_ON(!inode); | ||
1067 | |||
1068 | ret = fixup_inode_link_count(trans, root, inode); | ||
1069 | BUG_ON(ret); | ||
1070 | |||
1071 | iput(inode); | ||
1072 | |||
1073 | if (key.offset == 0) | ||
1074 | break; | ||
1075 | key.offset--; | ||
1076 | } | ||
1077 | btrfs_release_path(root, path); | ||
1078 | return 0; | ||
1079 | } | ||
1080 | |||
1081 | |||
1082 | /* | ||
1083 | * record a given inode in the fixup dir so we can check its link | ||
1084 | * count when replay is done. The link count is incremented here | ||
1085 | * so the inode won't go away until we check it | ||
1086 | */ | ||
1087 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, | ||
1088 | struct btrfs_root *root, | ||
1089 | struct btrfs_path *path, | ||
1090 | u64 objectid) | ||
1091 | { | ||
1092 | struct btrfs_key key; | ||
1093 | int ret = 0; | ||
1094 | struct inode *inode; | ||
1095 | |||
1096 | inode = read_one_inode(root, objectid); | ||
1097 | BUG_ON(!inode); | ||
1098 | |||
1099 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | ||
1100 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); | ||
1101 | key.offset = objectid; | ||
1102 | |||
1103 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | ||
1104 | |||
1105 | btrfs_release_path(root, path); | ||
1106 | if (ret == 0) { | ||
1107 | btrfs_inc_nlink(inode); | ||
1108 | btrfs_update_inode(trans, root, inode); | ||
1109 | } else if (ret == -EEXIST) { | ||
1110 | ret = 0; | ||
1111 | } else { | ||
1112 | BUG(); | ||
1113 | } | ||
1114 | iput(inode); | ||
1115 | |||
1116 | return ret; | ||
1117 | } | ||
1118 | |||
1119 | /* | ||
1120 | * when replaying the log for a directory, we only insert names | ||
1121 | * for inodes that actually exist. This means an fsync on a directory | ||
1122 | * does not implicitly fsync all the new files in it | ||
1123 | */ | ||
1124 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, | ||
1125 | struct btrfs_root *root, | ||
1126 | struct btrfs_path *path, | ||
1127 | u64 dirid, u64 index, | ||
1128 | char *name, int name_len, u8 type, | ||
1129 | struct btrfs_key *location) | ||
1130 | { | ||
1131 | struct inode *inode; | ||
1132 | struct inode *dir; | ||
1133 | int ret; | ||
1134 | |||
1135 | inode = read_one_inode(root, location->objectid); | ||
1136 | if (!inode) | ||
1137 | return -ENOENT; | ||
1138 | |||
1139 | dir = read_one_inode(root, dirid); | ||
1140 | if (!dir) { | ||
1141 | iput(inode); | ||
1142 | return -EIO; | ||
1143 | } | ||
1144 | ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index); | ||
1145 | |||
1146 | /* FIXME, put inode into FIXUP list */ | ||
1147 | |||
1148 | iput(inode); | ||
1149 | iput(dir); | ||
1150 | return ret; | ||
1151 | } | ||
1152 | |||
1153 | /* | ||
1154 | * take a single entry in a log directory item and replay it into | ||
1155 | * the subvolume. | ||
1156 | * | ||
1157 | * if a conflicting item exists in the subdirectory already, | ||
1158 | * the inode it points to is unlinked and put into the link count | ||
1159 | * fix up tree. | ||
1160 | * | ||
1161 | * If a name from the log points to a file or directory that does | ||
1162 | * not exist in the FS, it is skipped. fsyncs on directories | ||
1163 | * do not force down inodes inside that directory, just changes to the | ||
1164 | * names or unlinks in a directory. | ||
1165 | */ | ||
1166 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, | ||
1167 | struct btrfs_root *root, | ||
1168 | struct btrfs_path *path, | ||
1169 | struct extent_buffer *eb, | ||
1170 | struct btrfs_dir_item *di, | ||
1171 | struct btrfs_key *key) | ||
1172 | { | ||
1173 | char *name; | ||
1174 | int name_len; | ||
1175 | struct btrfs_dir_item *dst_di; | ||
1176 | struct btrfs_key found_key; | ||
1177 | struct btrfs_key log_key; | ||
1178 | struct inode *dir; | ||
1179 | struct inode *inode; | ||
1180 | u8 log_type; | ||
1181 | int ret; | ||
1182 | |||
1183 | dir = read_one_inode(root, key->objectid); | ||
1184 | BUG_ON(!dir); | ||
1185 | |||
1186 | name_len = btrfs_dir_name_len(eb, di); | ||
1187 | name = kmalloc(name_len, GFP_NOFS); | ||
1188 | log_type = btrfs_dir_type(eb, di); | ||
1189 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | ||
1190 | name_len); | ||
1191 | |||
1192 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); | ||
1193 | if (key->type == BTRFS_DIR_ITEM_KEY) { | ||
1194 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, | ||
1195 | name, name_len, 1); | ||
1196 | } | ||
1197 | else if (key->type == BTRFS_DIR_INDEX_KEY) { | ||
1198 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, | ||
1199 | key->objectid, | ||
1200 | key->offset, name, | ||
1201 | name_len, 1); | ||
1202 | } else { | ||
1203 | BUG(); | ||
1204 | } | ||
1205 | if (!dst_di || IS_ERR(dst_di)) { | ||
1206 | /* we need a sequence number to insert, so we only | ||
1207 | * do inserts for the BTRFS_DIR_INDEX_KEY types | ||
1208 | */ | ||
1209 | if (key->type != BTRFS_DIR_INDEX_KEY) | ||
1210 | goto out; | ||
1211 | goto insert; | ||
1212 | } | ||
1213 | |||
1214 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); | ||
1215 | /* the existing item matches the logged item */ | ||
1216 | if (found_key.objectid == log_key.objectid && | ||
1217 | found_key.type == log_key.type && | ||
1218 | found_key.offset == log_key.offset && | ||
1219 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { | ||
1220 | goto out; | ||
1221 | } | ||
1222 | |||
1223 | /* | ||
1224 | * don't drop the conflicting directory entry if the inode | ||
1225 | * for the new entry doesn't exist | ||
1226 | */ | ||
1227 | inode = read_one_inode(root, log_key.objectid); | ||
1228 | if (!inode) | ||
1229 | goto out; | ||
1230 | |||
1231 | iput(inode); | ||
1232 | ret = drop_one_dir_item(trans, root, path, dir, dst_di); | ||
1233 | BUG_ON(ret); | ||
1234 | |||
1235 | if (key->type == BTRFS_DIR_INDEX_KEY) | ||
1236 | goto insert; | ||
1237 | out: | ||
1238 | btrfs_release_path(root, path); | ||
1239 | kfree(name); | ||
1240 | iput(dir); | ||
1241 | return 0; | ||
1242 | |||
1243 | insert: | ||
1244 | btrfs_release_path(root, path); | ||
1245 | ret = insert_one_name(trans, root, path, key->objectid, key->offset, | ||
1246 | name, name_len, log_type, &log_key); | ||
1247 | |||
1248 | if (ret && ret != -ENOENT) | ||
1249 | BUG(); | ||
1250 | goto out; | ||
1251 | } | ||
1252 | |||
1253 | /* | ||
1254 | * find all the names in a directory item and reconcile them into | ||
1255 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than | ||
1256 | * one name in a directory item, but the same code gets used for | ||
1257 | * both directory index types | ||
1258 | */ | ||
1259 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, | ||
1260 | struct btrfs_root *root, | ||
1261 | struct btrfs_path *path, | ||
1262 | struct extent_buffer *eb, int slot, | ||
1263 | struct btrfs_key *key) | ||
1264 | { | ||
1265 | int ret; | ||
1266 | u32 item_size = btrfs_item_size_nr(eb, slot); | ||
1267 | struct btrfs_dir_item *di; | ||
1268 | int name_len; | ||
1269 | unsigned long ptr; | ||
1270 | unsigned long ptr_end; | ||
1271 | |||
1272 | ptr = btrfs_item_ptr_offset(eb, slot); | ||
1273 | ptr_end = ptr + item_size; | ||
1274 | while(ptr < ptr_end) { | ||
1275 | di = (struct btrfs_dir_item *)ptr; | ||
1276 | name_len = btrfs_dir_name_len(eb, di); | ||
1277 | ret = replay_one_name(trans, root, path, eb, di, key); | ||
1278 | BUG_ON(ret); | ||
1279 | ptr = (unsigned long)(di + 1); | ||
1280 | ptr += name_len; | ||
1281 | } | ||
1282 | return 0; | ||
1283 | } | ||
1284 | |||
1285 | /* | ||
1286 | * directory replay has two parts. There are the standard directory | ||
1287 | * items in the log copied from the subvolume, and range items | ||
1288 | * created in the log while the subvolume was logged. | ||
1289 | * | ||
1290 | * The range items tell us which parts of the key space the log | ||
1291 | * is authoritative for. During replay, if a key in the subvolume | ||
1292 | * directory is in a logged range item, but not actually in the log | ||
1293 | * that means it was deleted from the directory before the fsync | ||
1294 | * and should be removed. | ||
1295 | */ | ||
1296 | static noinline int find_dir_range(struct btrfs_root *root, | ||
1297 | struct btrfs_path *path, | ||
1298 | u64 dirid, int key_type, | ||
1299 | u64 *start_ret, u64 *end_ret) | ||
1300 | { | ||
1301 | struct btrfs_key key; | ||
1302 | u64 found_end; | ||
1303 | struct btrfs_dir_log_item *item; | ||
1304 | int ret; | ||
1305 | int nritems; | ||
1306 | |||
1307 | if (*start_ret == (u64)-1) | ||
1308 | return 1; | ||
1309 | |||
1310 | key.objectid = dirid; | ||
1311 | key.type = key_type; | ||
1312 | key.offset = *start_ret; | ||
1313 | |||
1314 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
1315 | if (ret < 0) | ||
1316 | goto out; | ||
1317 | if (ret > 0) { | ||
1318 | if (path->slots[0] == 0) | ||
1319 | goto out; | ||
1320 | path->slots[0]--; | ||
1321 | } | ||
1322 | if (ret != 0) | ||
1323 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | ||
1324 | |||
1325 | if (key.type != key_type || key.objectid != dirid) { | ||
1326 | ret = 1; | ||
1327 | goto next; | ||
1328 | } | ||
1329 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | ||
1330 | struct btrfs_dir_log_item); | ||
1331 | found_end = btrfs_dir_log_end(path->nodes[0], item); | ||
1332 | |||
1333 | if (*start_ret >= key.offset && *start_ret <= found_end) { | ||
1334 | ret = 0; | ||
1335 | *start_ret = key.offset; | ||
1336 | *end_ret = found_end; | ||
1337 | goto out; | ||
1338 | } | ||
1339 | ret = 1; | ||
1340 | next: | ||
1341 | /* check the next slot in the tree to see if it is a valid item */ | ||
1342 | nritems = btrfs_header_nritems(path->nodes[0]); | ||
1343 | if (path->slots[0] >= nritems) { | ||
1344 | ret = btrfs_next_leaf(root, path); | ||
1345 | if (ret) | ||
1346 | goto out; | ||
1347 | } else { | ||
1348 | path->slots[0]++; | ||
1349 | } | ||
1350 | |||
1351 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | ||
1352 | |||
1353 | if (key.type != key_type || key.objectid != dirid) { | ||
1354 | ret = 1; | ||
1355 | goto out; | ||
1356 | } | ||
1357 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | ||
1358 | struct btrfs_dir_log_item); | ||
1359 | found_end = btrfs_dir_log_end(path->nodes[0], item); | ||
1360 | *start_ret = key.offset; | ||
1361 | *end_ret = found_end; | ||
1362 | ret = 0; | ||
1363 | out: | ||
1364 | btrfs_release_path(root, path); | ||
1365 | return ret; | ||
1366 | } | ||
1367 | |||
1368 | /* | ||
1369 | * this looks for a given directory item in the log. If the directory | ||
1370 | * item is not in the log, the item is removed and the inode it points | ||
1371 | * to is unlinked | ||
1372 | */ | ||
1373 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, | ||
1374 | struct btrfs_root *root, | ||
1375 | struct btrfs_root *log, | ||
1376 | struct btrfs_path *path, | ||
1377 | struct btrfs_path *log_path, | ||
1378 | struct inode *dir, | ||
1379 | struct btrfs_key *dir_key) | ||
1380 | { | ||
1381 | int ret; | ||
1382 | struct extent_buffer *eb; | ||
1383 | int slot; | ||
1384 | u32 item_size; | ||
1385 | struct btrfs_dir_item *di; | ||
1386 | struct btrfs_dir_item *log_di; | ||
1387 | int name_len; | ||
1388 | unsigned long ptr; | ||
1389 | unsigned long ptr_end; | ||
1390 | char *name; | ||
1391 | struct inode *inode; | ||
1392 | struct btrfs_key location; | ||
1393 | |||
1394 | again: | ||
1395 | eb = path->nodes[0]; | ||
1396 | slot = path->slots[0]; | ||
1397 | item_size = btrfs_item_size_nr(eb, slot); | ||
1398 | ptr = btrfs_item_ptr_offset(eb, slot); | ||
1399 | ptr_end = ptr + item_size; | ||
1400 | while(ptr < ptr_end) { | ||
1401 | di = (struct btrfs_dir_item *)ptr; | ||
1402 | name_len = btrfs_dir_name_len(eb, di); | ||
1403 | name = kmalloc(name_len, GFP_NOFS); | ||
1404 | if (!name) { | ||
1405 | ret = -ENOMEM; | ||
1406 | goto out; | ||
1407 | } | ||
1408 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | ||
1409 | name_len); | ||
1410 | log_di = NULL; | ||
1411 | if (dir_key->type == BTRFS_DIR_ITEM_KEY) { | ||
1412 | log_di = btrfs_lookup_dir_item(trans, log, log_path, | ||
1413 | dir_key->objectid, | ||
1414 | name, name_len, 0); | ||
1415 | } else if (dir_key->type == BTRFS_DIR_INDEX_KEY) { | ||
1416 | log_di = btrfs_lookup_dir_index_item(trans, log, | ||
1417 | log_path, | ||
1418 | dir_key->objectid, | ||
1419 | dir_key->offset, | ||
1420 | name, name_len, 0); | ||
1421 | } | ||
1422 | if (!log_di || IS_ERR(log_di)) { | ||
1423 | btrfs_dir_item_key_to_cpu(eb, di, &location); | ||
1424 | btrfs_release_path(root, path); | ||
1425 | btrfs_release_path(log, log_path); | ||
1426 | inode = read_one_inode(root, location.objectid); | ||
1427 | BUG_ON(!inode); | ||
1428 | |||
1429 | ret = link_to_fixup_dir(trans, root, | ||
1430 | path, location.objectid); | ||
1431 | BUG_ON(ret); | ||
1432 | btrfs_inc_nlink(inode); | ||
1433 | ret = btrfs_unlink_inode(trans, root, dir, inode, | ||
1434 | name, name_len); | ||
1435 | BUG_ON(ret); | ||
1436 | kfree(name); | ||
1437 | iput(inode); | ||
1438 | |||
1439 | /* there might still be more names under this key | ||
1440 | * check and repeat if required | ||
1441 | */ | ||
1442 | ret = btrfs_search_slot(NULL, root, dir_key, path, | ||
1443 | 0, 0); | ||
1444 | if (ret == 0) | ||
1445 | goto again; | ||
1446 | ret = 0; | ||
1447 | goto out; | ||
1448 | } | ||
1449 | btrfs_release_path(log, log_path); | ||
1450 | kfree(name); | ||
1451 | |||
1452 | ptr = (unsigned long)(di + 1); | ||
1453 | ptr += name_len; | ||
1454 | } | ||
1455 | ret = 0; | ||
1456 | out: | ||
1457 | btrfs_release_path(root, path); | ||
1458 | btrfs_release_path(log, log_path); | ||
1459 | return ret; | ||
1460 | } | ||
1461 | |||
1462 | /* | ||
1463 | * deletion replay happens before we copy any new directory items | ||
1464 | * out of the log or out of backreferences from inodes. It | ||
1465 | * scans the log to find ranges of keys that log is authoritative for, | ||
1466 | * and then scans the directory to find items in those ranges that are | ||
1467 | * not present in the log. | ||
1468 | * | ||
1469 | * Anything we don't find in the log is unlinked and removed from the | ||
1470 | * directory. | ||
1471 | */ | ||
1472 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, | ||
1473 | struct btrfs_root *root, | ||
1474 | struct btrfs_root *log, | ||
1475 | struct btrfs_path *path, | ||
1476 | u64 dirid) | ||
1477 | { | ||
1478 | u64 range_start; | ||
1479 | u64 range_end; | ||
1480 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; | ||
1481 | int ret = 0; | ||
1482 | struct btrfs_key dir_key; | ||
1483 | struct btrfs_key found_key; | ||
1484 | struct btrfs_path *log_path; | ||
1485 | struct inode *dir; | ||
1486 | |||
1487 | dir_key.objectid = dirid; | ||
1488 | dir_key.type = BTRFS_DIR_ITEM_KEY; | ||
1489 | log_path = btrfs_alloc_path(); | ||
1490 | if (!log_path) | ||
1491 | return -ENOMEM; | ||
1492 | |||
1493 | dir = read_one_inode(root, dirid); | ||
1494 | /* it isn't an error if the inode isn't there, that can happen | ||
1495 | * because we replay the deletes before we copy in the inode item | ||
1496 | * from the log | ||
1497 | */ | ||
1498 | if (!dir) { | ||
1499 | btrfs_free_path(log_path); | ||
1500 | return 0; | ||
1501 | } | ||
1502 | again: | ||
1503 | range_start = 0; | ||
1504 | range_end = 0; | ||
1505 | while(1) { | ||
1506 | ret = find_dir_range(log, path, dirid, key_type, | ||
1507 | &range_start, &range_end); | ||
1508 | if (ret != 0) | ||
1509 | break; | ||
1510 | |||
1511 | dir_key.offset = range_start; | ||
1512 | while(1) { | ||
1513 | int nritems; | ||
1514 | ret = btrfs_search_slot(NULL, root, &dir_key, path, | ||
1515 | 0, 0); | ||
1516 | if (ret < 0) | ||
1517 | goto out; | ||
1518 | |||
1519 | nritems = btrfs_header_nritems(path->nodes[0]); | ||
1520 | if (path->slots[0] >= nritems) { | ||
1521 | ret = btrfs_next_leaf(root, path); | ||
1522 | if (ret) | ||
1523 | break; | ||
1524 | } | ||
1525 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | ||
1526 | path->slots[0]); | ||
1527 | if (found_key.objectid != dirid || | ||
1528 | found_key.type != dir_key.type) | ||
1529 | goto next_type; | ||
1530 | |||
1531 | if (found_key.offset > range_end) | ||
1532 | break; | ||
1533 | |||
1534 | ret = check_item_in_log(trans, root, log, path, | ||
1535 | log_path, dir, &found_key); | ||
1536 | BUG_ON(ret); | ||
1537 | if (found_key.offset == (u64)-1) | ||
1538 | break; | ||
1539 | dir_key.offset = found_key.offset + 1; | ||
1540 | } | ||
1541 | btrfs_release_path(root, path); | ||
1542 | if (range_end == (u64)-1) | ||
1543 | break; | ||
1544 | range_start = range_end + 1; | ||
1545 | } | ||
1546 | |||
1547 | next_type: | ||
1548 | ret = 0; | ||
1549 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { | ||
1550 | key_type = BTRFS_DIR_LOG_INDEX_KEY; | ||
1551 | dir_key.type = BTRFS_DIR_INDEX_KEY; | ||
1552 | btrfs_release_path(root, path); | ||
1553 | goto again; | ||
1554 | } | ||
1555 | out: | ||
1556 | btrfs_release_path(root, path); | ||
1557 | btrfs_free_path(log_path); | ||
1558 | iput(dir); | ||
1559 | return ret; | ||
1560 | } | ||
1561 | |||
1562 | /* | ||
1563 | * the process_func used to replay items from the log tree. This | ||
1564 | * gets called in two different stages. The first stage just looks | ||
1565 | * for inodes and makes sure they are all copied into the subvolume. | ||
1566 | * | ||
1567 | * The second stage copies all the other item types from the log into | ||
1568 | * the subvolume. The two stage approach is slower, but gets rid of | ||
1569 | * lots of complexity around inodes referencing other inodes that exist | ||
1570 | * only in the log (references come from either directory items or inode | ||
1571 | * back refs). | ||
1572 | */ | ||
1573 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | ||
1574 | struct walk_control *wc, u64 gen) | ||
1575 | { | ||
1576 | int nritems; | ||
1577 | struct btrfs_path *path; | ||
1578 | struct btrfs_root *root = wc->replay_dest; | ||
1579 | struct btrfs_key key; | ||
1580 | u32 item_size; | ||
1581 | int level; | ||
1582 | int i; | ||
1583 | int ret; | ||
1584 | |||
1585 | btrfs_read_buffer(eb, gen); | ||
1586 | |||
1587 | level = btrfs_header_level(eb); | ||
1588 | |||
1589 | if (level != 0) | ||
1590 | return 0; | ||
1591 | |||
1592 | path = btrfs_alloc_path(); | ||
1593 | BUG_ON(!path); | ||
1594 | |||
1595 | nritems = btrfs_header_nritems(eb); | ||
1596 | for (i = 0; i < nritems; i++) { | ||
1597 | btrfs_item_key_to_cpu(eb, &key, i); | ||
1598 | item_size = btrfs_item_size_nr(eb, i); | ||
1599 | |||
1600 | /* inode keys are done during the first stage */ | ||
1601 | if (key.type == BTRFS_INODE_ITEM_KEY && | ||
1602 | wc->stage == LOG_WALK_REPLAY_INODES) { | ||
1603 | struct inode *inode; | ||
1604 | struct btrfs_inode_item *inode_item; | ||
1605 | u32 mode; | ||
1606 | |||
1607 | inode_item = btrfs_item_ptr(eb, i, | ||
1608 | struct btrfs_inode_item); | ||
1609 | mode = btrfs_inode_mode(eb, inode_item); | ||
1610 | if (S_ISDIR(mode)) { | ||
1611 | ret = replay_dir_deletes(wc->trans, | ||
1612 | root, log, path, key.objectid); | ||
1613 | BUG_ON(ret); | ||
1614 | } | ||
1615 | ret = overwrite_item(wc->trans, root, path, | ||
1616 | eb, i, &key); | ||
1617 | BUG_ON(ret); | ||
1618 | |||
1619 | /* for regular files, truncate away | ||
1620 | * extents past the new EOF | ||
1621 | */ | ||
1622 | if (S_ISREG(mode)) { | ||
1623 | inode = read_one_inode(root, | ||
1624 | key.objectid); | ||
1625 | BUG_ON(!inode); | ||
1626 | |||
1627 | ret = btrfs_truncate_inode_items(wc->trans, | ||
1628 | root, inode, inode->i_size, | ||
1629 | BTRFS_EXTENT_DATA_KEY); | ||
1630 | BUG_ON(ret); | ||
1631 | iput(inode); | ||
1632 | } | ||
1633 | ret = link_to_fixup_dir(wc->trans, root, | ||
1634 | path, key.objectid); | ||
1635 | BUG_ON(ret); | ||
1636 | } | ||
1637 | if (wc->stage < LOG_WALK_REPLAY_ALL) | ||
1638 | continue; | ||
1639 | |||
1640 | /* these keys are simply copied */ | ||
1641 | if (key.type == BTRFS_XATTR_ITEM_KEY) { | ||
1642 | ret = overwrite_item(wc->trans, root, path, | ||
1643 | eb, i, &key); | ||
1644 | BUG_ON(ret); | ||
1645 | } else if (key.type == BTRFS_INODE_REF_KEY) { | ||
1646 | ret = add_inode_ref(wc->trans, root, log, path, | ||
1647 | eb, i, &key); | ||
1648 | BUG_ON(ret && ret != -ENOENT); | ||
1649 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { | ||
1650 | ret = replay_one_extent(wc->trans, root, path, | ||
1651 | eb, i, &key); | ||
1652 | BUG_ON(ret); | ||
1653 | } else if (key.type == BTRFS_CSUM_ITEM_KEY) { | ||
1654 | ret = replay_one_csum(wc->trans, root, path, | ||
1655 | eb, i, &key); | ||
1656 | BUG_ON(ret); | ||
1657 | } else if (key.type == BTRFS_DIR_ITEM_KEY || | ||
1658 | key.type == BTRFS_DIR_INDEX_KEY) { | ||
1659 | ret = replay_one_dir_item(wc->trans, root, path, | ||
1660 | eb, i, &key); | ||
1661 | BUG_ON(ret); | ||
1662 | } | ||
1663 | } | ||
1664 | btrfs_free_path(path); | ||
1665 | return 0; | ||
1666 | } | ||
1667 | |||
1668 | static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans, | ||
1669 | struct btrfs_root *root, | ||
1670 | struct btrfs_path *path, int *level, | ||
1671 | struct walk_control *wc) | ||
1672 | { | ||
1673 | u64 root_owner; | ||
1674 | u64 root_gen; | ||
1675 | u64 bytenr; | ||
1676 | u64 ptr_gen; | ||
1677 | struct extent_buffer *next; | ||
1678 | struct extent_buffer *cur; | ||
1679 | struct extent_buffer *parent; | ||
1680 | u32 blocksize; | ||
1681 | int ret = 0; | ||
1682 | |||
1683 | WARN_ON(*level < 0); | ||
1684 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | ||
1685 | |||
1686 | while(*level > 0) { | ||
1687 | WARN_ON(*level < 0); | ||
1688 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | ||
1689 | cur = path->nodes[*level]; | ||
1690 | |||
1691 | if (btrfs_header_level(cur) != *level) | ||
1692 | WARN_ON(1); | ||
1693 | |||
1694 | if (path->slots[*level] >= | ||
1695 | btrfs_header_nritems(cur)) | ||
1696 | break; | ||
1697 | |||
1698 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | ||
1699 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | ||
1700 | blocksize = btrfs_level_size(root, *level - 1); | ||
1701 | |||
1702 | parent = path->nodes[*level]; | ||
1703 | root_owner = btrfs_header_owner(parent); | ||
1704 | root_gen = btrfs_header_generation(parent); | ||
1705 | |||
1706 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); | ||
1707 | |||
1708 | wc->process_func(root, next, wc, ptr_gen); | ||
1709 | |||
1710 | if (*level == 1) { | ||
1711 | path->slots[*level]++; | ||
1712 | if (wc->free) { | ||
1713 | btrfs_read_buffer(next, ptr_gen); | ||
1714 | |||
1715 | btrfs_tree_lock(next); | ||
1716 | clean_tree_block(trans, root, next); | ||
1717 | btrfs_wait_tree_block_writeback(next); | ||
1718 | btrfs_tree_unlock(next); | ||
1719 | |||
1720 | ret = btrfs_drop_leaf_ref(trans, root, next); | ||
1721 | BUG_ON(ret); | ||
1722 | |||
1723 | WARN_ON(root_owner != | ||
1724 | BTRFS_TREE_LOG_OBJECTID); | ||
1725 | ret = btrfs_free_extent(trans, root, bytenr, | ||
1726 | blocksize, root_owner, | ||
1727 | root_gen, 0, 0, 1); | ||
1728 | BUG_ON(ret); | ||
1729 | } | ||
1730 | free_extent_buffer(next); | ||
1731 | continue; | ||
1732 | } | ||
1733 | btrfs_read_buffer(next, ptr_gen); | ||
1734 | |||
1735 | WARN_ON(*level <= 0); | ||
1736 | if (path->nodes[*level-1]) | ||
1737 | free_extent_buffer(path->nodes[*level-1]); | ||
1738 | path->nodes[*level-1] = next; | ||
1739 | *level = btrfs_header_level(next); | ||
1740 | path->slots[*level] = 0; | ||
1741 | cond_resched(); | ||
1742 | } | ||
1743 | WARN_ON(*level < 0); | ||
1744 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | ||
1745 | |||
1746 | if (path->nodes[*level] == root->node) { | ||
1747 | parent = path->nodes[*level]; | ||
1748 | } else { | ||
1749 | parent = path->nodes[*level + 1]; | ||
1750 | } | ||
1751 | bytenr = path->nodes[*level]->start; | ||
1752 | |||
1753 | blocksize = btrfs_level_size(root, *level); | ||
1754 | root_owner = btrfs_header_owner(parent); | ||
1755 | root_gen = btrfs_header_generation(parent); | ||
1756 | |||
1757 | wc->process_func(root, path->nodes[*level], wc, | ||
1758 | btrfs_header_generation(path->nodes[*level])); | ||
1759 | |||
1760 | if (wc->free) { | ||
1761 | next = path->nodes[*level]; | ||
1762 | btrfs_tree_lock(next); | ||
1763 | clean_tree_block(trans, root, next); | ||
1764 | btrfs_wait_tree_block_writeback(next); | ||
1765 | btrfs_tree_unlock(next); | ||
1766 | |||
1767 | if (*level == 0) { | ||
1768 | ret = btrfs_drop_leaf_ref(trans, root, next); | ||
1769 | BUG_ON(ret); | ||
1770 | } | ||
1771 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); | ||
1772 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, | ||
1773 | root_owner, root_gen, 0, 0, 1); | ||
1774 | BUG_ON(ret); | ||
1775 | } | ||
1776 | free_extent_buffer(path->nodes[*level]); | ||
1777 | path->nodes[*level] = NULL; | ||
1778 | *level += 1; | ||
1779 | |||
1780 | cond_resched(); | ||
1781 | return 0; | ||
1782 | } | ||
1783 | |||
1784 | static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans, | ||
1785 | struct btrfs_root *root, | ||
1786 | struct btrfs_path *path, int *level, | ||
1787 | struct walk_control *wc) | ||
1788 | { | ||
1789 | u64 root_owner; | ||
1790 | u64 root_gen; | ||
1791 | int i; | ||
1792 | int slot; | ||
1793 | int ret; | ||
1794 | |||
1795 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { | ||
1796 | slot = path->slots[i]; | ||
1797 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { | ||
1798 | struct extent_buffer *node; | ||
1799 | node = path->nodes[i]; | ||
1800 | path->slots[i]++; | ||
1801 | *level = i; | ||
1802 | WARN_ON(*level == 0); | ||
1803 | return 0; | ||
1804 | } else { | ||
1805 | if (path->nodes[*level] == root->node) { | ||
1806 | root_owner = root->root_key.objectid; | ||
1807 | root_gen = | ||
1808 | btrfs_header_generation(path->nodes[*level]); | ||
1809 | } else { | ||
1810 | struct extent_buffer *node; | ||
1811 | node = path->nodes[*level + 1]; | ||
1812 | root_owner = btrfs_header_owner(node); | ||
1813 | root_gen = btrfs_header_generation(node); | ||
1814 | } | ||
1815 | wc->process_func(root, path->nodes[*level], wc, | ||
1816 | btrfs_header_generation(path->nodes[*level])); | ||
1817 | if (wc->free) { | ||
1818 | struct extent_buffer *next; | ||
1819 | |||
1820 | next = path->nodes[*level]; | ||
1821 | |||
1822 | btrfs_tree_lock(next); | ||
1823 | clean_tree_block(trans, root, next); | ||
1824 | btrfs_wait_tree_block_writeback(next); | ||
1825 | btrfs_tree_unlock(next); | ||
1826 | |||
1827 | if (*level == 0) { | ||
1828 | ret = btrfs_drop_leaf_ref(trans, root, | ||
1829 | next); | ||
1830 | BUG_ON(ret); | ||
1831 | } | ||
1832 | |||
1833 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); | ||
1834 | ret = btrfs_free_extent(trans, root, | ||
1835 | path->nodes[*level]->start, | ||
1836 | path->nodes[*level]->len, | ||
1837 | root_owner, root_gen, 0, 0, 1); | ||
1838 | BUG_ON(ret); | ||
1839 | } | ||
1840 | free_extent_buffer(path->nodes[*level]); | ||
1841 | path->nodes[*level] = NULL; | ||
1842 | *level = i + 1; | ||
1843 | } | ||
1844 | } | ||
1845 | return 1; | ||
1846 | } | ||
1847 | |||
1848 | /* | ||
1849 | * drop the reference count on the tree rooted at 'snap'. This traverses | ||
1850 | * the tree freeing any blocks that have a ref count of zero after being | ||
1851 | * decremented. | ||
1852 | */ | ||
1853 | static int walk_log_tree(struct btrfs_trans_handle *trans, | ||
1854 | struct btrfs_root *log, struct walk_control *wc) | ||
1855 | { | ||
1856 | int ret = 0; | ||
1857 | int wret; | ||
1858 | int level; | ||
1859 | struct btrfs_path *path; | ||
1860 | int i; | ||
1861 | int orig_level; | ||
1862 | |||
1863 | path = btrfs_alloc_path(); | ||
1864 | BUG_ON(!path); | ||
1865 | |||
1866 | level = btrfs_header_level(log->node); | ||
1867 | orig_level = level; | ||
1868 | path->nodes[level] = log->node; | ||
1869 | extent_buffer_get(log->node); | ||
1870 | path->slots[level] = 0; | ||
1871 | |||
1872 | while(1) { | ||
1873 | wret = walk_down_log_tree(trans, log, path, &level, wc); | ||
1874 | if (wret > 0) | ||
1875 | break; | ||
1876 | if (wret < 0) | ||
1877 | ret = wret; | ||
1878 | |||
1879 | wret = walk_up_log_tree(trans, log, path, &level, wc); | ||
1880 | if (wret > 0) | ||
1881 | break; | ||
1882 | if (wret < 0) | ||
1883 | ret = wret; | ||
1884 | } | ||
1885 | |||
1886 | /* was the root node processed? if not, catch it here */ | ||
1887 | if (path->nodes[orig_level]) { | ||
1888 | wc->process_func(log, path->nodes[orig_level], wc, | ||
1889 | btrfs_header_generation(path->nodes[orig_level])); | ||
1890 | if (wc->free) { | ||
1891 | struct extent_buffer *next; | ||
1892 | |||
1893 | next = path->nodes[orig_level]; | ||
1894 | |||
1895 | btrfs_tree_lock(next); | ||
1896 | clean_tree_block(trans, log, next); | ||
1897 | btrfs_wait_tree_block_writeback(next); | ||
1898 | btrfs_tree_unlock(next); | ||
1899 | |||
1900 | if (orig_level == 0) { | ||
1901 | ret = btrfs_drop_leaf_ref(trans, log, | ||
1902 | next); | ||
1903 | BUG_ON(ret); | ||
1904 | } | ||
1905 | WARN_ON(log->root_key.objectid != | ||
1906 | BTRFS_TREE_LOG_OBJECTID); | ||
1907 | ret = btrfs_free_extent(trans, log, | ||
1908 | next->start, next->len, | ||
1909 | log->root_key.objectid, | ||
1910 | btrfs_header_generation(next), | ||
1911 | 0, 0, 1); | ||
1912 | BUG_ON(ret); | ||
1913 | } | ||
1914 | } | ||
1915 | |||
1916 | for (i = 0; i <= orig_level; i++) { | ||
1917 | if (path->nodes[i]) { | ||
1918 | free_extent_buffer(path->nodes[i]); | ||
1919 | path->nodes[i] = NULL; | ||
1920 | } | ||
1921 | } | ||
1922 | btrfs_free_path(path); | ||
1923 | if (wc->free) | ||
1924 | free_extent_buffer(log->node); | ||
1925 | return ret; | ||
1926 | } | ||
1927 | |||
1928 | int wait_log_commit(struct btrfs_root *log) | ||
1929 | { | ||
1930 | DEFINE_WAIT(wait); | ||
1931 | u64 transid = log->fs_info->tree_log_transid; | ||
1932 | |||
1933 | do { | ||
1934 | prepare_to_wait(&log->fs_info->tree_log_wait, &wait, | ||
1935 | TASK_UNINTERRUPTIBLE); | ||
1936 | mutex_unlock(&log->fs_info->tree_log_mutex); | ||
1937 | if (atomic_read(&log->fs_info->tree_log_commit)) | ||
1938 | schedule(); | ||
1939 | finish_wait(&log->fs_info->tree_log_wait, &wait); | ||
1940 | mutex_lock(&log->fs_info->tree_log_mutex); | ||
1941 | } while(transid == log->fs_info->tree_log_transid && | ||
1942 | atomic_read(&log->fs_info->tree_log_commit)); | ||
1943 | return 0; | ||
1944 | } | ||
1945 | |||
1946 | /* | ||
1947 | * btrfs_sync_log does sends a given tree log down to the disk and | ||
1948 | * updates the super blocks to record it. When this call is done, | ||
1949 | * you know that any inodes previously logged are safely on disk | ||
1950 | */ | ||
1951 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | ||
1952 | struct btrfs_root *root) | ||
1953 | { | ||
1954 | int ret; | ||
1955 | unsigned long batch; | ||
1956 | struct btrfs_root *log = root->log_root; | ||
1957 | struct walk_control wc = { | ||
1958 | .write = 1, | ||
1959 | .process_func = process_one_buffer | ||
1960 | }; | ||
1961 | |||
1962 | mutex_lock(&log->fs_info->tree_log_mutex); | ||
1963 | if (atomic_read(&log->fs_info->tree_log_commit)) { | ||
1964 | wait_log_commit(log); | ||
1965 | goto out; | ||
1966 | } | ||
1967 | atomic_set(&log->fs_info->tree_log_commit, 1); | ||
1968 | |||
1969 | while(1) { | ||
1970 | mutex_unlock(&log->fs_info->tree_log_mutex); | ||
1971 | schedule_timeout_uninterruptible(1); | ||
1972 | mutex_lock(&log->fs_info->tree_log_mutex); | ||
1973 | batch = log->fs_info->tree_log_batch; | ||
1974 | |||
1975 | while(atomic_read(&log->fs_info->tree_log_writers)) { | ||
1976 | DEFINE_WAIT(wait); | ||
1977 | prepare_to_wait(&log->fs_info->tree_log_wait, &wait, | ||
1978 | TASK_UNINTERRUPTIBLE); | ||
1979 | batch = log->fs_info->tree_log_batch; | ||
1980 | mutex_unlock(&log->fs_info->tree_log_mutex); | ||
1981 | if (atomic_read(&log->fs_info->tree_log_writers)) | ||
1982 | schedule(); | ||
1983 | mutex_lock(&log->fs_info->tree_log_mutex); | ||
1984 | finish_wait(&log->fs_info->tree_log_wait, &wait); | ||
1985 | } | ||
1986 | if (batch == log->fs_info->tree_log_batch) | ||
1987 | break; | ||
1988 | } | ||
1989 | ret = walk_log_tree(trans, log, &wc); | ||
1990 | BUG_ON(ret); | ||
1991 | |||
1992 | ret = walk_log_tree(trans, log->fs_info->log_root_tree, &wc); | ||
1993 | BUG_ON(ret); | ||
1994 | |||
1995 | wc.wait = 1; | ||
1996 | |||
1997 | ret = walk_log_tree(trans, log, &wc); | ||
1998 | BUG_ON(ret); | ||
1999 | |||
2000 | ret = walk_log_tree(trans, log->fs_info->log_root_tree, &wc); | ||
2001 | BUG_ON(ret); | ||
2002 | |||
2003 | btrfs_set_super_log_root(&root->fs_info->super_for_commit, | ||
2004 | log->fs_info->log_root_tree->node->start); | ||
2005 | btrfs_set_super_log_root_level(&root->fs_info->super_for_commit, | ||
2006 | btrfs_header_level(log->fs_info->log_root_tree->node)); | ||
2007 | |||
2008 | write_ctree_super(trans, log->fs_info->tree_root); | ||
2009 | log->fs_info->tree_log_transid++; | ||
2010 | log->fs_info->tree_log_batch = 0; | ||
2011 | atomic_set(&log->fs_info->tree_log_commit, 0); | ||
2012 | smp_mb(); | ||
2013 | if (waitqueue_active(&log->fs_info->tree_log_wait)) | ||
2014 | wake_up(&log->fs_info->tree_log_wait); | ||
2015 | out: | ||
2016 | mutex_unlock(&log->fs_info->tree_log_mutex); | ||
2017 | return 0; | ||
2018 | |||
2019 | } | ||
2020 | |||
2021 | /* | ||
2022 | * free all the extents used by the tree log. This should be called | ||
2023 | * at commit time of the full transaction | ||
2024 | */ | ||
2025 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) | ||
2026 | { | ||
2027 | int ret; | ||
2028 | struct btrfs_root *log; | ||
2029 | struct key; | ||
2030 | struct walk_control wc = { | ||
2031 | .free = 1, | ||
2032 | .process_func = process_one_buffer | ||
2033 | }; | ||
2034 | |||
2035 | if (!root->log_root) | ||
2036 | return 0; | ||
2037 | |||
2038 | log = root->log_root; | ||
2039 | ret = walk_log_tree(trans, log, &wc); | ||
2040 | BUG_ON(ret); | ||
2041 | |||
2042 | log = root->log_root; | ||
2043 | ret = btrfs_del_root(trans, root->fs_info->log_root_tree, | ||
2044 | &log->root_key); | ||
2045 | BUG_ON(ret); | ||
2046 | root->log_root = NULL; | ||
2047 | kfree(root->log_root); | ||
2048 | return 0; | ||
2049 | } | ||
2050 | |||
2051 | /* | ||
2052 | * helper function to update the item for a given subvolumes log root | ||
2053 | * in the tree of log roots | ||
2054 | */ | ||
2055 | static int update_log_root(struct btrfs_trans_handle *trans, | ||
2056 | struct btrfs_root *log) | ||
2057 | { | ||
2058 | u64 bytenr = btrfs_root_bytenr(&log->root_item); | ||
2059 | int ret; | ||
2060 | |||
2061 | if (log->node->start == bytenr) | ||
2062 | return 0; | ||
2063 | |||
2064 | btrfs_set_root_bytenr(&log->root_item, log->node->start); | ||
2065 | btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node)); | ||
2066 | ret = btrfs_update_root(trans, log->fs_info->log_root_tree, | ||
2067 | &log->root_key, &log->root_item); | ||
2068 | BUG_ON(ret); | ||
2069 | return ret; | ||
2070 | } | ||
2071 | |||
2072 | /* | ||
2073 | * If both a file and directory are logged, and unlinks or renames are | ||
2074 | * mixed in, we have a few interesting corners: | ||
2075 | * | ||
2076 | * create file X in dir Y | ||
2077 | * link file X to X.link in dir Y | ||
2078 | * fsync file X | ||
2079 | * unlink file X but leave X.link | ||
2080 | * fsync dir Y | ||
2081 | * | ||
2082 | * After a crash we would expect only X.link to exist. But file X | ||
2083 | * didn't get fsync'd again so the log has back refs for X and X.link. | ||
2084 | * | ||
2085 | * We solve this by removing directory entries and inode backrefs from the | ||
2086 | * log when a file that was logged in the current transaction is | ||
2087 | * unlinked. Any later fsync will include the updated log entries, and | ||
2088 | * we'll be able to reconstruct the proper directory items from backrefs. | ||
2089 | * | ||
2090 | * This optimizations allows us to avoid relogging the entire inode | ||
2091 | * or the entire directory. | ||
2092 | */ | ||
2093 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | ||
2094 | struct btrfs_root *root, | ||
2095 | const char *name, int name_len, | ||
2096 | struct inode *dir, u64 index) | ||
2097 | { | ||
2098 | struct btrfs_root *log; | ||
2099 | struct btrfs_dir_item *di; | ||
2100 | struct btrfs_path *path; | ||
2101 | int ret; | ||
2102 | int bytes_del = 0; | ||
2103 | |||
2104 | ret = join_running_log_trans(root); | ||
2105 | if (ret) | ||
2106 | return 0; | ||
2107 | |||
2108 | mutex_lock(&BTRFS_I(dir)->log_mutex); | ||
2109 | |||
2110 | log = root->log_root; | ||
2111 | path = btrfs_alloc_path(); | ||
2112 | di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino, | ||
2113 | name, name_len, -1); | ||
2114 | if (di && !IS_ERR(di)) { | ||
2115 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | ||
2116 | bytes_del += name_len; | ||
2117 | BUG_ON(ret); | ||
2118 | } | ||
2119 | btrfs_release_path(log, path); | ||
2120 | di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino, | ||
2121 | index, name, name_len, -1); | ||
2122 | if (di && !IS_ERR(di)) { | ||
2123 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | ||
2124 | bytes_del += name_len; | ||
2125 | BUG_ON(ret); | ||
2126 | } | ||
2127 | |||
2128 | /* update the directory size in the log to reflect the names | ||
2129 | * we have removed | ||
2130 | */ | ||
2131 | if (bytes_del) { | ||
2132 | struct btrfs_key key; | ||
2133 | |||
2134 | key.objectid = dir->i_ino; | ||
2135 | key.offset = 0; | ||
2136 | key.type = BTRFS_INODE_ITEM_KEY; | ||
2137 | btrfs_release_path(log, path); | ||
2138 | |||
2139 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); | ||
2140 | if (ret == 0) { | ||
2141 | struct btrfs_inode_item *item; | ||
2142 | u64 i_size; | ||
2143 | |||
2144 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | ||
2145 | struct btrfs_inode_item); | ||
2146 | i_size = btrfs_inode_size(path->nodes[0], item); | ||
2147 | if (i_size > bytes_del) | ||
2148 | i_size -= bytes_del; | ||
2149 | else | ||
2150 | i_size = 0; | ||
2151 | btrfs_set_inode_size(path->nodes[0], item, i_size); | ||
2152 | btrfs_mark_buffer_dirty(path->nodes[0]); | ||
2153 | } else | ||
2154 | ret = 0; | ||
2155 | btrfs_release_path(log, path); | ||
2156 | } | ||
2157 | |||
2158 | btrfs_free_path(path); | ||
2159 | mutex_unlock(&BTRFS_I(dir)->log_mutex); | ||
2160 | end_log_trans(root); | ||
2161 | |||
2162 | return 0; | ||
2163 | } | ||
2164 | |||
2165 | /* see comments for btrfs_del_dir_entries_in_log */ | ||
2166 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, | ||
2167 | struct btrfs_root *root, | ||
2168 | const char *name, int name_len, | ||
2169 | struct inode *inode, u64 dirid) | ||
2170 | { | ||
2171 | struct btrfs_root *log; | ||
2172 | u64 index; | ||
2173 | int ret; | ||
2174 | |||
2175 | ret = join_running_log_trans(root); | ||
2176 | if (ret) | ||
2177 | return 0; | ||
2178 | log = root->log_root; | ||
2179 | mutex_lock(&BTRFS_I(inode)->log_mutex); | ||
2180 | |||
2181 | ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino, | ||
2182 | dirid, &index); | ||
2183 | mutex_unlock(&BTRFS_I(inode)->log_mutex); | ||
2184 | end_log_trans(root); | ||
2185 | |||
2186 | if (ret == 0 || ret == -ENOENT) | ||
2187 | return 0; | ||
2188 | return ret; | ||
2189 | } | ||
2190 | |||
2191 | /* | ||
2192 | * creates a range item in the log for 'dirid'. first_offset and | ||
2193 | * last_offset tell us which parts of the key space the log should | ||
2194 | * be considered authoritative for. | ||
2195 | */ | ||
2196 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, | ||
2197 | struct btrfs_root *log, | ||
2198 | struct btrfs_path *path, | ||
2199 | int key_type, u64 dirid, | ||
2200 | u64 first_offset, u64 last_offset) | ||
2201 | { | ||
2202 | int ret; | ||
2203 | struct btrfs_key key; | ||
2204 | struct btrfs_dir_log_item *item; | ||
2205 | |||
2206 | key.objectid = dirid; | ||
2207 | key.offset = first_offset; | ||
2208 | if (key_type == BTRFS_DIR_ITEM_KEY) | ||
2209 | key.type = BTRFS_DIR_LOG_ITEM_KEY; | ||
2210 | else | ||
2211 | key.type = BTRFS_DIR_LOG_INDEX_KEY; | ||
2212 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); | ||
2213 | BUG_ON(ret); | ||
2214 | |||
2215 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | ||
2216 | struct btrfs_dir_log_item); | ||
2217 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); | ||
2218 | btrfs_mark_buffer_dirty(path->nodes[0]); | ||
2219 | btrfs_release_path(log, path); | ||
2220 | return 0; | ||
2221 | } | ||
2222 | |||
2223 | /* | ||
2224 | * log all the items included in the current transaction for a given | ||
2225 | * directory. This also creates the range items in the log tree required | ||
2226 | * to replay anything deleted before the fsync | ||
2227 | */ | ||
2228 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, | ||
2229 | struct btrfs_root *root, struct inode *inode, | ||
2230 | struct btrfs_path *path, | ||
2231 | struct btrfs_path *dst_path, int key_type, | ||
2232 | u64 min_offset, u64 *last_offset_ret) | ||
2233 | { | ||
2234 | struct btrfs_key min_key; | ||
2235 | struct btrfs_key max_key; | ||
2236 | struct btrfs_root *log = root->log_root; | ||
2237 | struct extent_buffer *src; | ||
2238 | int ret; | ||
2239 | int i; | ||
2240 | int nritems; | ||
2241 | u64 first_offset = min_offset; | ||
2242 | u64 last_offset = (u64)-1; | ||
2243 | |||
2244 | log = root->log_root; | ||
2245 | max_key.objectid = inode->i_ino; | ||
2246 | max_key.offset = (u64)-1; | ||
2247 | max_key.type = key_type; | ||
2248 | |||
2249 | min_key.objectid = inode->i_ino; | ||
2250 | min_key.type = key_type; | ||
2251 | min_key.offset = min_offset; | ||
2252 | |||
2253 | path->keep_locks = 1; | ||
2254 | |||
2255 | ret = btrfs_search_forward(root, &min_key, &max_key, | ||
2256 | path, 0, trans->transid); | ||
2257 | |||
2258 | /* | ||
2259 | * we didn't find anything from this transaction, see if there | ||
2260 | * is anything at all | ||
2261 | */ | ||
2262 | if (ret != 0 || min_key.objectid != inode->i_ino || | ||
2263 | min_key.type != key_type) { | ||
2264 | min_key.objectid = inode->i_ino; | ||
2265 | min_key.type = key_type; | ||
2266 | min_key.offset = (u64)-1; | ||
2267 | btrfs_release_path(root, path); | ||
2268 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | ||
2269 | if (ret < 0) { | ||
2270 | btrfs_release_path(root, path); | ||
2271 | return ret; | ||
2272 | } | ||
2273 | ret = btrfs_previous_item(root, path, inode->i_ino, key_type); | ||
2274 | |||
2275 | /* if ret == 0 there are items for this type, | ||
2276 | * create a range to tell us the last key of this type. | ||
2277 | * otherwise, there are no items in this directory after | ||
2278 | * *min_offset, and we create a range to indicate that. | ||
2279 | */ | ||
2280 | if (ret == 0) { | ||
2281 | struct btrfs_key tmp; | ||
2282 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, | ||
2283 | path->slots[0]); | ||
2284 | if (key_type == tmp.type) { | ||
2285 | first_offset = max(min_offset, tmp.offset) + 1; | ||
2286 | } | ||
2287 | } | ||
2288 | goto done; | ||
2289 | } | ||
2290 | |||
2291 | /* go backward to find any previous key */ | ||
2292 | ret = btrfs_previous_item(root, path, inode->i_ino, key_type); | ||
2293 | if (ret == 0) { | ||
2294 | struct btrfs_key tmp; | ||
2295 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | ||
2296 | if (key_type == tmp.type) { | ||
2297 | first_offset = tmp.offset; | ||
2298 | ret = overwrite_item(trans, log, dst_path, | ||
2299 | path->nodes[0], path->slots[0], | ||
2300 | &tmp); | ||
2301 | } | ||
2302 | } | ||
2303 | btrfs_release_path(root, path); | ||
2304 | |||
2305 | /* find the first key from this transaction again */ | ||
2306 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | ||
2307 | if (ret != 0) { | ||
2308 | WARN_ON(1); | ||
2309 | goto done; | ||
2310 | } | ||
2311 | |||
2312 | /* | ||
2313 | * we have a block from this transaction, log every item in it | ||
2314 | * from our directory | ||
2315 | */ | ||
2316 | while(1) { | ||
2317 | struct btrfs_key tmp; | ||
2318 | src = path->nodes[0]; | ||
2319 | nritems = btrfs_header_nritems(src); | ||
2320 | for (i = path->slots[0]; i < nritems; i++) { | ||
2321 | btrfs_item_key_to_cpu(src, &min_key, i); | ||
2322 | |||
2323 | if (min_key.objectid != inode->i_ino || | ||
2324 | min_key.type != key_type) | ||
2325 | goto done; | ||
2326 | ret = overwrite_item(trans, log, dst_path, src, i, | ||
2327 | &min_key); | ||
2328 | BUG_ON(ret); | ||
2329 | } | ||
2330 | path->slots[0] = nritems; | ||
2331 | |||
2332 | /* | ||
2333 | * look ahead to the next item and see if it is also | ||
2334 | * from this directory and from this transaction | ||
2335 | */ | ||
2336 | ret = btrfs_next_leaf(root, path); | ||
2337 | if (ret == 1) { | ||
2338 | last_offset = (u64)-1; | ||
2339 | goto done; | ||
2340 | } | ||
2341 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | ||
2342 | if (tmp.objectid != inode->i_ino || tmp.type != key_type) { | ||
2343 | last_offset = (u64)-1; | ||
2344 | goto done; | ||
2345 | } | ||
2346 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { | ||
2347 | ret = overwrite_item(trans, log, dst_path, | ||
2348 | path->nodes[0], path->slots[0], | ||
2349 | &tmp); | ||
2350 | |||
2351 | BUG_ON(ret); | ||
2352 | last_offset = tmp.offset; | ||
2353 | goto done; | ||
2354 | } | ||
2355 | } | ||
2356 | done: | ||
2357 | *last_offset_ret = last_offset; | ||
2358 | btrfs_release_path(root, path); | ||
2359 | btrfs_release_path(log, dst_path); | ||
2360 | |||
2361 | /* insert the log range keys to indicate where the log is valid */ | ||
2362 | ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino, | ||
2363 | first_offset, last_offset); | ||
2364 | BUG_ON(ret); | ||
2365 | return 0; | ||
2366 | } | ||
2367 | |||
2368 | /* | ||
2369 | * logging directories is very similar to logging inodes, We find all the items | ||
2370 | * from the current transaction and write them to the log. | ||
2371 | * | ||
2372 | * The recovery code scans the directory in the subvolume, and if it finds a | ||
2373 | * key in the range logged that is not present in the log tree, then it means | ||
2374 | * that dir entry was unlinked during the transaction. | ||
2375 | * | ||
2376 | * In order for that scan to work, we must include one key smaller than | ||
2377 | * the smallest logged by this transaction and one key larger than the largest | ||
2378 | * key logged by this transaction. | ||
2379 | */ | ||
2380 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, | ||
2381 | struct btrfs_root *root, struct inode *inode, | ||
2382 | struct btrfs_path *path, | ||
2383 | struct btrfs_path *dst_path) | ||
2384 | { | ||
2385 | u64 min_key; | ||
2386 | u64 max_key; | ||
2387 | int ret; | ||
2388 | int key_type = BTRFS_DIR_ITEM_KEY; | ||
2389 | |||
2390 | again: | ||
2391 | min_key = 0; | ||
2392 | max_key = 0; | ||
2393 | while(1) { | ||
2394 | ret = log_dir_items(trans, root, inode, path, | ||
2395 | dst_path, key_type, min_key, | ||
2396 | &max_key); | ||
2397 | BUG_ON(ret); | ||
2398 | if (max_key == (u64)-1) | ||
2399 | break; | ||
2400 | min_key = max_key + 1; | ||
2401 | } | ||
2402 | |||
2403 | if (key_type == BTRFS_DIR_ITEM_KEY) { | ||
2404 | key_type = BTRFS_DIR_INDEX_KEY; | ||
2405 | goto again; | ||
2406 | } | ||
2407 | return 0; | ||
2408 | } | ||
2409 | |||
2410 | /* | ||
2411 | * a helper function to drop items from the log before we relog an | ||
2412 | * inode. max_key_type indicates the highest item type to remove. | ||
2413 | * This cannot be run for file data extents because it does not | ||
2414 | * free the extents they point to. | ||
2415 | */ | ||
2416 | static int drop_objectid_items(struct btrfs_trans_handle *trans, | ||
2417 | struct btrfs_root *log, | ||
2418 | struct btrfs_path *path, | ||
2419 | u64 objectid, int max_key_type) | ||
2420 | { | ||
2421 | int ret; | ||
2422 | struct btrfs_key key; | ||
2423 | struct btrfs_key found_key; | ||
2424 | |||
2425 | key.objectid = objectid; | ||
2426 | key.type = max_key_type; | ||
2427 | key.offset = (u64)-1; | ||
2428 | |||
2429 | while(1) { | ||
2430 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); | ||
2431 | |||
2432 | if (ret != 1) | ||
2433 | break; | ||
2434 | |||
2435 | if (path->slots[0] == 0) | ||
2436 | break; | ||
2437 | |||
2438 | path->slots[0]--; | ||
2439 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | ||
2440 | path->slots[0]); | ||
2441 | |||
2442 | if (found_key.objectid != objectid) | ||
2443 | break; | ||
2444 | |||
2445 | ret = btrfs_del_item(trans, log, path); | ||
2446 | BUG_ON(ret); | ||
2447 | btrfs_release_path(log, path); | ||
2448 | } | ||
2449 | btrfs_release_path(log, path); | ||
2450 | return 0; | ||
2451 | } | ||
2452 | |||
2453 | /* log a single inode in the tree log. | ||
2454 | * At least one parent directory for this inode must exist in the tree | ||
2455 | * or be logged already. | ||
2456 | * | ||
2457 | * Any items from this inode changed by the current transaction are copied | ||
2458 | * to the log tree. An extra reference is taken on any extents in this | ||
2459 | * file, allowing us to avoid a whole pile of corner cases around logging | ||
2460 | * blocks that have been removed from the tree. | ||
2461 | * | ||
2462 | * See LOG_INODE_ALL and related defines for a description of what inode_only | ||
2463 | * does. | ||
2464 | * | ||
2465 | * This handles both files and directories. | ||
2466 | */ | ||
2467 | static int __btrfs_log_inode(struct btrfs_trans_handle *trans, | ||
2468 | struct btrfs_root *root, struct inode *inode, | ||
2469 | int inode_only) | ||
2470 | { | ||
2471 | struct btrfs_path *path; | ||
2472 | struct btrfs_path *dst_path; | ||
2473 | struct btrfs_key min_key; | ||
2474 | struct btrfs_key max_key; | ||
2475 | struct btrfs_root *log = root->log_root; | ||
2476 | unsigned long src_offset; | ||
2477 | unsigned long dst_offset; | ||
2478 | struct extent_buffer *src; | ||
2479 | struct btrfs_file_extent_item *extent; | ||
2480 | struct btrfs_inode_item *inode_item; | ||
2481 | u32 size; | ||
2482 | int ret; | ||
2483 | |||
2484 | log = root->log_root; | ||
2485 | |||
2486 | path = btrfs_alloc_path(); | ||
2487 | dst_path = btrfs_alloc_path(); | ||
2488 | |||
2489 | min_key.objectid = inode->i_ino; | ||
2490 | min_key.type = BTRFS_INODE_ITEM_KEY; | ||
2491 | min_key.offset = 0; | ||
2492 | |||
2493 | max_key.objectid = inode->i_ino; | ||
2494 | if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode)) | ||
2495 | max_key.type = BTRFS_XATTR_ITEM_KEY; | ||
2496 | else | ||
2497 | max_key.type = (u8)-1; | ||
2498 | max_key.offset = (u64)-1; | ||
2499 | |||
2500 | /* | ||
2501 | * if this inode has already been logged and we're in inode_only | ||
2502 | * mode, we don't want to delete the things that have already | ||
2503 | * been written to the log. | ||
2504 | * | ||
2505 | * But, if the inode has been through an inode_only log, | ||
2506 | * the logged_trans field is not set. This allows us to catch | ||
2507 | * any new names for this inode in the backrefs by logging it | ||
2508 | * again | ||
2509 | */ | ||
2510 | if (inode_only == LOG_INODE_EXISTS && | ||
2511 | BTRFS_I(inode)->logged_trans == trans->transid) { | ||
2512 | btrfs_free_path(path); | ||
2513 | btrfs_free_path(dst_path); | ||
2514 | goto out; | ||
2515 | } | ||
2516 | mutex_lock(&BTRFS_I(inode)->log_mutex); | ||
2517 | |||
2518 | /* | ||
2519 | * a brute force approach to making sure we get the most uptodate | ||
2520 | * copies of everything. | ||
2521 | */ | ||
2522 | if (S_ISDIR(inode->i_mode)) { | ||
2523 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; | ||
2524 | |||
2525 | if (inode_only == LOG_INODE_EXISTS) | ||
2526 | max_key_type = BTRFS_XATTR_ITEM_KEY; | ||
2527 | ret = drop_objectid_items(trans, log, path, | ||
2528 | inode->i_ino, max_key_type); | ||
2529 | } else { | ||
2530 | ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0); | ||
2531 | } | ||
2532 | BUG_ON(ret); | ||
2533 | path->keep_locks = 1; | ||
2534 | |||
2535 | while(1) { | ||
2536 | ret = btrfs_search_forward(root, &min_key, &max_key, | ||
2537 | path, 0, trans->transid); | ||
2538 | if (ret != 0) | ||
2539 | break; | ||
2540 | |||
2541 | if (min_key.objectid != inode->i_ino) | ||
2542 | break; | ||
2543 | if (min_key.type > max_key.type) | ||
2544 | break; | ||
2545 | |||
2546 | src = path->nodes[0]; | ||
2547 | size = btrfs_item_size_nr(src, path->slots[0]); | ||
2548 | ret = btrfs_insert_empty_item(trans, log, dst_path, &min_key, | ||
2549 | size); | ||
2550 | if (ret) | ||
2551 | BUG(); | ||
2552 | |||
2553 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], | ||
2554 | dst_path->slots[0]); | ||
2555 | |||
2556 | src_offset = btrfs_item_ptr_offset(src, path->slots[0]); | ||
2557 | |||
2558 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, | ||
2559 | src_offset, size); | ||
2560 | |||
2561 | if (inode_only == LOG_INODE_EXISTS && | ||
2562 | min_key.type == BTRFS_INODE_ITEM_KEY) { | ||
2563 | inode_item = btrfs_item_ptr(dst_path->nodes[0], | ||
2564 | dst_path->slots[0], | ||
2565 | struct btrfs_inode_item); | ||
2566 | btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0); | ||
2567 | |||
2568 | /* set the generation to zero so the recover code | ||
2569 | * can tell the difference between an logging | ||
2570 | * just to say 'this inode exists' and a logging | ||
2571 | * to say 'update this inode with these values' | ||
2572 | */ | ||
2573 | btrfs_set_inode_generation(dst_path->nodes[0], | ||
2574 | inode_item, 0); | ||
2575 | } | ||
2576 | /* take a reference on file data extents so that truncates | ||
2577 | * or deletes of this inode don't have to relog the inode | ||
2578 | * again | ||
2579 | */ | ||
2580 | if (btrfs_key_type(&min_key) == BTRFS_EXTENT_DATA_KEY) { | ||
2581 | int found_type; | ||
2582 | extent = btrfs_item_ptr(src, path->slots[0], | ||
2583 | struct btrfs_file_extent_item); | ||
2584 | |||
2585 | found_type = btrfs_file_extent_type(src, extent); | ||
2586 | if (found_type == BTRFS_FILE_EXTENT_REG) { | ||
2587 | u64 ds = btrfs_file_extent_disk_bytenr(src, | ||
2588 | extent); | ||
2589 | u64 dl = btrfs_file_extent_disk_num_bytes(src, | ||
2590 | extent); | ||
2591 | /* ds == 0 is a hole */ | ||
2592 | if (ds != 0) { | ||
2593 | ret = btrfs_inc_extent_ref(trans, log, | ||
2594 | ds, dl, | ||
2595 | log->root_key.objectid, | ||
2596 | 0, | ||
2597 | inode->i_ino, | ||
2598 | min_key.offset); | ||
2599 | BUG_ON(ret); | ||
2600 | } | ||
2601 | } | ||
2602 | } | ||
2603 | |||
2604 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); | ||
2605 | btrfs_release_path(root, path); | ||
2606 | btrfs_release_path(log, dst_path); | ||
2607 | |||
2608 | if (min_key.offset < (u64)-1) | ||
2609 | min_key.offset++; | ||
2610 | else if (min_key.type < (u8)-1) | ||
2611 | min_key.type++; | ||
2612 | else if (min_key.objectid < (u64)-1) | ||
2613 | min_key.objectid++; | ||
2614 | else | ||
2615 | break; | ||
2616 | } | ||
2617 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) { | ||
2618 | btrfs_release_path(root, path); | ||
2619 | btrfs_release_path(log, dst_path); | ||
2620 | ret = log_directory_changes(trans, root, inode, path, dst_path); | ||
2621 | BUG_ON(ret); | ||
2622 | } | ||
2623 | mutex_unlock(&BTRFS_I(inode)->log_mutex); | ||
2624 | |||
2625 | btrfs_free_path(path); | ||
2626 | btrfs_free_path(dst_path); | ||
2627 | |||
2628 | mutex_lock(&root->fs_info->tree_log_mutex); | ||
2629 | ret = update_log_root(trans, log); | ||
2630 | BUG_ON(ret); | ||
2631 | mutex_unlock(&root->fs_info->tree_log_mutex); | ||
2632 | out: | ||
2633 | return 0; | ||
2634 | } | ||
2635 | |||
2636 | int btrfs_log_inode(struct btrfs_trans_handle *trans, | ||
2637 | struct btrfs_root *root, struct inode *inode, | ||
2638 | int inode_only) | ||
2639 | { | ||
2640 | int ret; | ||
2641 | |||
2642 | start_log_trans(trans, root); | ||
2643 | ret = __btrfs_log_inode(trans, root, inode, inode_only); | ||
2644 | end_log_trans(root); | ||
2645 | return ret; | ||
2646 | } | ||
2647 | |||
2648 | /* | ||
2649 | * helper function around btrfs_log_inode to make sure newly created | ||
2650 | * parent directories also end up in the log. A minimal inode and backref | ||
2651 | * only logging is done of any parent directories that are older than | ||
2652 | * the last committed transaction | ||
2653 | */ | ||
2654 | int btrfs_log_dentry(struct btrfs_trans_handle *trans, | ||
2655 | struct btrfs_root *root, struct dentry *dentry) | ||
2656 | { | ||
2657 | int inode_only = LOG_INODE_ALL; | ||
2658 | struct super_block *sb; | ||
2659 | int ret; | ||
2660 | |||
2661 | start_log_trans(trans, root); | ||
2662 | sb = dentry->d_inode->i_sb; | ||
2663 | while(1) { | ||
2664 | ret = __btrfs_log_inode(trans, root, dentry->d_inode, | ||
2665 | inode_only); | ||
2666 | BUG_ON(ret); | ||
2667 | inode_only = LOG_INODE_EXISTS; | ||
2668 | |||
2669 | dentry = dentry->d_parent; | ||
2670 | if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb) | ||
2671 | break; | ||
2672 | |||
2673 | if (BTRFS_I(dentry->d_inode)->generation <= | ||
2674 | root->fs_info->last_trans_committed) | ||
2675 | break; | ||
2676 | } | ||
2677 | end_log_trans(root); | ||
2678 | return 0; | ||
2679 | } | ||
2680 | |||
2681 | /* | ||
2682 | * it is not safe to log dentry if the chunk root has added new | ||
2683 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. | ||
2684 | * If this returns 1, you must commit the transaction to safely get your | ||
2685 | * data on disk. | ||
2686 | */ | ||
2687 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | ||
2688 | struct btrfs_root *root, struct dentry *dentry) | ||
2689 | { | ||
2690 | u64 gen; | ||
2691 | gen = root->fs_info->last_trans_new_blockgroup; | ||
2692 | if (gen > root->fs_info->last_trans_committed) | ||
2693 | return 1; | ||
2694 | else | ||
2695 | return btrfs_log_dentry(trans, root, dentry); | ||
2696 | } | ||
2697 | |||
2698 | /* | ||
2699 | * should be called during mount to recover any replay any log trees | ||
2700 | * from the FS | ||
2701 | */ | ||
2702 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) | ||
2703 | { | ||
2704 | int ret; | ||
2705 | struct btrfs_path *path; | ||
2706 | struct btrfs_trans_handle *trans; | ||
2707 | struct btrfs_key key; | ||
2708 | struct btrfs_key found_key; | ||
2709 | struct btrfs_key tmp_key; | ||
2710 | struct btrfs_root *log; | ||
2711 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; | ||
2712 | struct walk_control wc = { | ||
2713 | .process_func = process_one_buffer, | ||
2714 | .stage = 0, | ||
2715 | }; | ||
2716 | |||
2717 | fs_info->log_root_recovering = 1; | ||
2718 | path = btrfs_alloc_path(); | ||
2719 | BUG_ON(!path); | ||
2720 | |||
2721 | trans = btrfs_start_transaction(fs_info->tree_root, 1); | ||
2722 | |||
2723 | wc.trans = trans; | ||
2724 | wc.pin = 1; | ||
2725 | |||
2726 | walk_log_tree(trans, log_root_tree, &wc); | ||
2727 | |||
2728 | again: | ||
2729 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | ||
2730 | key.offset = (u64)-1; | ||
2731 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | ||
2732 | |||
2733 | while(1) { | ||
2734 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); | ||
2735 | if (ret < 0) | ||
2736 | break; | ||
2737 | if (ret > 0) { | ||
2738 | if (path->slots[0] == 0) | ||
2739 | break; | ||
2740 | path->slots[0]--; | ||
2741 | } | ||
2742 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | ||
2743 | path->slots[0]); | ||
2744 | btrfs_release_path(log_root_tree, path); | ||
2745 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) | ||
2746 | break; | ||
2747 | |||
2748 | log = btrfs_read_fs_root_no_radix(log_root_tree, | ||
2749 | &found_key); | ||
2750 | BUG_ON(!log); | ||
2751 | |||
2752 | |||
2753 | tmp_key.objectid = found_key.offset; | ||
2754 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; | ||
2755 | tmp_key.offset = (u64)-1; | ||
2756 | |||
2757 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); | ||
2758 | |||
2759 | BUG_ON(!wc.replay_dest); | ||
2760 | |||
2761 | btrfs_record_root_in_trans(wc.replay_dest); | ||
2762 | ret = walk_log_tree(trans, log, &wc); | ||
2763 | BUG_ON(ret); | ||
2764 | |||
2765 | if (wc.stage == LOG_WALK_REPLAY_ALL) { | ||
2766 | ret = fixup_inode_link_counts(trans, wc.replay_dest, | ||
2767 | path); | ||
2768 | BUG_ON(ret); | ||
2769 | } | ||
2770 | |||
2771 | key.offset = found_key.offset - 1; | ||
2772 | free_extent_buffer(log->node); | ||
2773 | kfree(log); | ||
2774 | |||
2775 | if (found_key.offset == 0) | ||
2776 | break; | ||
2777 | } | ||
2778 | btrfs_release_path(log_root_tree, path); | ||
2779 | |||
2780 | /* step one is to pin it all, step two is to replay just inodes */ | ||
2781 | if (wc.pin) { | ||
2782 | wc.pin = 0; | ||
2783 | wc.process_func = replay_one_buffer; | ||
2784 | wc.stage = LOG_WALK_REPLAY_INODES; | ||
2785 | goto again; | ||
2786 | } | ||
2787 | /* step three is to replay everything */ | ||
2788 | if (wc.stage < LOG_WALK_REPLAY_ALL) { | ||
2789 | wc.stage++; | ||
2790 | goto again; | ||
2791 | } | ||
2792 | |||
2793 | btrfs_free_path(path); | ||
2794 | |||
2795 | free_extent_buffer(log_root_tree->node); | ||
2796 | log_root_tree->log_root = NULL; | ||
2797 | fs_info->log_root_recovering = 0; | ||
2798 | |||
2799 | /* step 4: commit the transaction, which also unpins the blocks */ | ||
2800 | btrfs_commit_transaction(trans, fs_info->tree_root); | ||
2801 | |||
2802 | kfree(log_root_tree); | ||
2803 | return 0; | ||
2804 | } | ||
diff --git a/fs/btrfs/tree-log.h b/fs/btrfs/tree-log.h new file mode 100644 index 000000000000..b9409b32ed02 --- /dev/null +++ b/fs/btrfs/tree-log.h | |||
@@ -0,0 +1,41 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2008 Oracle. All rights reserved. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU General Public | ||
6 | * License v2 as published by the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
11 | * General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public | ||
14 | * License along with this program; if not, write to the | ||
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
16 | * Boston, MA 021110-1307, USA. | ||
17 | */ | ||
18 | |||
19 | #ifndef __TREE_LOG_ | ||
20 | #define __TREE_LOG_ | ||
21 | |||
22 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | ||
23 | struct btrfs_root *root); | ||
24 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root); | ||
25 | int btrfs_log_dentry(struct btrfs_trans_handle *trans, | ||
26 | struct btrfs_root *root, struct dentry *dentry); | ||
27 | int btrfs_recover_log_trees(struct btrfs_root *tree_root); | ||
28 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | ||
29 | struct btrfs_root *root, struct dentry *dentry); | ||
30 | int btrfs_log_inode(struct btrfs_trans_handle *trans, | ||
31 | struct btrfs_root *root, struct inode *inode, | ||
32 | int inode_only); | ||
33 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | ||
34 | struct btrfs_root *root, | ||
35 | const char *name, int name_len, | ||
36 | struct inode *dir, u64 index); | ||
37 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, | ||
38 | struct btrfs_root *root, | ||
39 | const char *name, int name_len, | ||
40 | struct inode *inode, u64 dirid); | ||
41 | #endif | ||