Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
e02119d5 CM |
2 | /* |
3 | * Copyright (C) 2008 Oracle. All rights reserved. | |
e02119d5 CM |
4 | */ |
5 | ||
6 | #include <linux/sched.h> | |
5a0e3ad6 | 7 | #include <linux/slab.h> |
c6adc9cc | 8 | #include <linux/blkdev.h> |
5dc562c5 | 9 | #include <linux/list_sort.h> |
c7f88c4e | 10 | #include <linux/iversion.h> |
602cbe91 | 11 | #include "misc.h" |
9678c543 | 12 | #include "ctree.h" |
995946dd | 13 | #include "tree-log.h" |
e02119d5 CM |
14 | #include "disk-io.h" |
15 | #include "locking.h" | |
16 | #include "print-tree.h" | |
f186373f | 17 | #include "backref.h" |
ebb8765b | 18 | #include "compression.h" |
df2c95f3 | 19 | #include "qgroup.h" |
6787bb9f NB |
20 | #include "block-group.h" |
21 | #include "space-info.h" | |
d3575156 | 22 | #include "zoned.h" |
26c2c454 | 23 | #include "inode-item.h" |
c7f13d42 | 24 | #include "fs.h" |
ad1ac501 | 25 | #include "accessors.h" |
a0231804 | 26 | #include "extent-tree.h" |
45c40c8f | 27 | #include "root-tree.h" |
e02119d5 | 28 | |
e09d94c9 FM |
29 | #define MAX_CONFLICT_INODES 10 |
30 | ||
e02119d5 CM |
31 | /* magic values for the inode_only field in btrfs_log_inode: |
32 | * | |
33 | * LOG_INODE_ALL means to log everything | |
34 | * LOG_INODE_EXISTS means to log just enough to recreate the inode | |
35 | * during log replay | |
36 | */ | |
e13976cf DS |
37 | enum { |
38 | LOG_INODE_ALL, | |
39 | LOG_INODE_EXISTS, | |
e13976cf | 40 | }; |
e02119d5 | 41 | |
12fcfd22 CM |
42 | /* |
43 | * directory trouble cases | |
44 | * | |
45 | * 1) on rename or unlink, if the inode being unlinked isn't in the fsync | |
46 | * log, we must force a full commit before doing an fsync of the directory | |
47 | * where the unlink was done. | |
48 | * ---> record transid of last unlink/rename per directory | |
49 | * | |
50 | * mkdir foo/some_dir | |
51 | * normal commit | |
52 | * rename foo/some_dir foo2/some_dir | |
53 | * mkdir foo/some_dir | |
54 | * fsync foo/some_dir/some_file | |
55 | * | |
56 | * The fsync above will unlink the original some_dir without recording | |
57 | * it in its new location (foo2). After a crash, some_dir will be gone | |
58 | * unless the fsync of some_file forces a full commit | |
59 | * | |
60 | * 2) we must log any new names for any file or dir that is in the fsync | |
61 | * log. ---> check inode while renaming/linking. | |
62 | * | |
63 | * 2a) we must log any new names for any file or dir during rename | |
64 | * when the directory they are being removed from was logged. | |
65 | * ---> check inode and old parent dir during rename | |
66 | * | |
67 | * 2a is actually the more important variant. With the extra logging | |
68 | * a crash might unlink the old name without recreating the new one | |
69 | * | |
70 | * 3) after a crash, we must go through any directories with a link count | |
71 | * of zero and redo the rm -rf | |
72 | * | |
73 | * mkdir f1/foo | |
74 | * normal commit | |
75 | * rm -rf f1/foo | |
76 | * fsync(f1) | |
77 | * | |
78 | * The directory f1 was fully removed from the FS, but fsync was never | |
79 | * called on f1, only its parent dir. After a crash the rm -rf must | |
80 | * be replayed. This must be able to recurse down the entire | |
81 | * directory tree. The inode link count fixup code takes care of the | |
82 | * ugly details. | |
83 | */ | |
84 | ||
e02119d5 CM |
85 | /* |
86 | * stages for the tree walking. The first | |
87 | * stage (0) is to only pin down the blocks we find | |
88 | * the second stage (1) is to make sure that all the inodes | |
89 | * we find in the log are created in the subvolume. | |
90 | * | |
91 | * The last stage is to deal with directories and links and extents | |
92 | * and all the other fun semantics | |
93 | */ | |
e13976cf DS |
94 | enum { |
95 | LOG_WALK_PIN_ONLY, | |
96 | LOG_WALK_REPLAY_INODES, | |
97 | LOG_WALK_REPLAY_DIR_INDEX, | |
98 | LOG_WALK_REPLAY_ALL, | |
99 | }; | |
e02119d5 | 100 | |
12fcfd22 | 101 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
90d04510 | 102 | struct btrfs_inode *inode, |
49dae1bc | 103 | int inode_only, |
8407f553 | 104 | struct btrfs_log_ctx *ctx); |
ec051c0f YZ |
105 | static int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
106 | struct btrfs_root *root, | |
107 | struct btrfs_path *path, u64 objectid); | |
12fcfd22 CM |
108 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
109 | struct btrfs_root *root, | |
110 | struct btrfs_root *log, | |
111 | struct btrfs_path *path, | |
112 | u64 dirid, int del_all); | |
fa1a0f42 | 113 | static void wait_log_commit(struct btrfs_root *root, int transid); |
e02119d5 CM |
114 | |
115 | /* | |
116 | * tree logging is a special write ahead log used to make sure that | |
117 | * fsyncs and O_SYNCs can happen without doing full tree commits. | |
118 | * | |
119 | * Full tree commits are expensive because they require commonly | |
120 | * modified blocks to be recowed, creating many dirty pages in the | |
121 | * extent tree an 4x-6x higher write load than ext3. | |
122 | * | |
123 | * Instead of doing a tree commit on every fsync, we use the | |
124 | * key ranges and transaction ids to find items for a given file or directory | |
125 | * that have changed in this transaction. Those items are copied into | |
126 | * a special tree (one per subvolume root), that tree is written to disk | |
127 | * and then the fsync is considered complete. | |
128 | * | |
129 | * After a crash, items are copied out of the log-tree back into the | |
130 | * subvolume tree. Any file data extents found are recorded in the extent | |
131 | * allocation tree, and the log-tree freed. | |
132 | * | |
133 | * The log tree is read three times, once to pin down all the extents it is | |
134 | * using in ram and once, once to create all the inodes logged in the tree | |
135 | * and once to do all the other items. | |
136 | */ | |
137 | ||
e02119d5 CM |
138 | /* |
139 | * start a sub transaction and setup the log tree | |
140 | * this increments the log tree writer count to make the people | |
141 | * syncing the tree wait for us to finish | |
142 | */ | |
143 | static int start_log_trans(struct btrfs_trans_handle *trans, | |
8b050d35 MX |
144 | struct btrfs_root *root, |
145 | struct btrfs_log_ctx *ctx) | |
e02119d5 | 146 | { |
0b246afa | 147 | struct btrfs_fs_info *fs_info = root->fs_info; |
47876f7c | 148 | struct btrfs_root *tree_root = fs_info->tree_root; |
fa1a0f42 | 149 | const bool zoned = btrfs_is_zoned(fs_info); |
34eb2a52 | 150 | int ret = 0; |
fa1a0f42 | 151 | bool created = false; |
7237f183 | 152 | |
47876f7c FM |
153 | /* |
154 | * First check if the log root tree was already created. If not, create | |
155 | * it before locking the root's log_mutex, just to keep lockdep happy. | |
156 | */ | |
157 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) { | |
158 | mutex_lock(&tree_root->log_mutex); | |
159 | if (!fs_info->log_root_tree) { | |
160 | ret = btrfs_init_log_root_tree(trans, fs_info); | |
fa1a0f42 | 161 | if (!ret) { |
47876f7c | 162 | set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state); |
fa1a0f42 NA |
163 | created = true; |
164 | } | |
47876f7c FM |
165 | } |
166 | mutex_unlock(&tree_root->log_mutex); | |
167 | if (ret) | |
168 | return ret; | |
169 | } | |
170 | ||
7237f183 | 171 | mutex_lock(&root->log_mutex); |
34eb2a52 | 172 | |
fa1a0f42 | 173 | again: |
7237f183 | 174 | if (root->log_root) { |
fa1a0f42 NA |
175 | int index = (root->log_transid + 1) % 2; |
176 | ||
4884b8e8 | 177 | if (btrfs_need_log_full_commit(trans)) { |
f31f09f6 | 178 | ret = BTRFS_LOG_FORCE_COMMIT; |
50471a38 MX |
179 | goto out; |
180 | } | |
34eb2a52 | 181 | |
fa1a0f42 NA |
182 | if (zoned && atomic_read(&root->log_commit[index])) { |
183 | wait_log_commit(root, root->log_transid - 1); | |
184 | goto again; | |
185 | } | |
186 | ||
ff782e0a | 187 | if (!root->log_start_pid) { |
27cdeb70 | 188 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
34eb2a52 | 189 | root->log_start_pid = current->pid; |
ff782e0a | 190 | } else if (root->log_start_pid != current->pid) { |
27cdeb70 | 191 | set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
ff782e0a | 192 | } |
34eb2a52 | 193 | } else { |
fa1a0f42 NA |
194 | /* |
195 | * This means fs_info->log_root_tree was already created | |
196 | * for some other FS trees. Do the full commit not to mix | |
197 | * nodes from multiple log transactions to do sequential | |
198 | * writing. | |
199 | */ | |
200 | if (zoned && !created) { | |
f31f09f6 | 201 | ret = BTRFS_LOG_FORCE_COMMIT; |
fa1a0f42 NA |
202 | goto out; |
203 | } | |
204 | ||
e02119d5 | 205 | ret = btrfs_add_log_tree(trans, root); |
4a500fd1 | 206 | if (ret) |
e87ac136 | 207 | goto out; |
34eb2a52 | 208 | |
e7a79811 | 209 | set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); |
34eb2a52 Z |
210 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
211 | root->log_start_pid = current->pid; | |
e02119d5 | 212 | } |
34eb2a52 | 213 | |
7237f183 | 214 | atomic_inc(&root->log_writers); |
289cffcb | 215 | if (!ctx->logging_new_name) { |
34eb2a52 | 216 | int index = root->log_transid % 2; |
8b050d35 | 217 | list_add_tail(&ctx->list, &root->log_ctxs[index]); |
d1433deb | 218 | ctx->log_transid = root->log_transid; |
8b050d35 | 219 | } |
34eb2a52 | 220 | |
e87ac136 | 221 | out: |
7237f183 | 222 | mutex_unlock(&root->log_mutex); |
e87ac136 | 223 | return ret; |
e02119d5 CM |
224 | } |
225 | ||
226 | /* | |
227 | * returns 0 if there was a log transaction running and we were able | |
228 | * to join, or returns -ENOENT if there were not transactions | |
229 | * in progress | |
230 | */ | |
231 | static int join_running_log_trans(struct btrfs_root *root) | |
232 | { | |
fa1a0f42 | 233 | const bool zoned = btrfs_is_zoned(root->fs_info); |
e02119d5 CM |
234 | int ret = -ENOENT; |
235 | ||
e7a79811 FM |
236 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state)) |
237 | return ret; | |
238 | ||
7237f183 | 239 | mutex_lock(&root->log_mutex); |
fa1a0f42 | 240 | again: |
e02119d5 | 241 | if (root->log_root) { |
fa1a0f42 NA |
242 | int index = (root->log_transid + 1) % 2; |
243 | ||
e02119d5 | 244 | ret = 0; |
fa1a0f42 NA |
245 | if (zoned && atomic_read(&root->log_commit[index])) { |
246 | wait_log_commit(root, root->log_transid - 1); | |
247 | goto again; | |
248 | } | |
7237f183 | 249 | atomic_inc(&root->log_writers); |
e02119d5 | 250 | } |
7237f183 | 251 | mutex_unlock(&root->log_mutex); |
e02119d5 CM |
252 | return ret; |
253 | } | |
254 | ||
12fcfd22 CM |
255 | /* |
256 | * This either makes the current running log transaction wait | |
257 | * until you call btrfs_end_log_trans() or it makes any future | |
258 | * log transactions wait until you call btrfs_end_log_trans() | |
259 | */ | |
45128b08 | 260 | void btrfs_pin_log_trans(struct btrfs_root *root) |
12fcfd22 | 261 | { |
12fcfd22 | 262 | atomic_inc(&root->log_writers); |
12fcfd22 CM |
263 | } |
264 | ||
e02119d5 CM |
265 | /* |
266 | * indicate we're done making changes to the log tree | |
267 | * and wake up anyone waiting to do a sync | |
268 | */ | |
143bede5 | 269 | void btrfs_end_log_trans(struct btrfs_root *root) |
e02119d5 | 270 | { |
7237f183 | 271 | if (atomic_dec_and_test(&root->log_writers)) { |
093258e6 DS |
272 | /* atomic_dec_and_test implies a barrier */ |
273 | cond_wake_up_nomb(&root->log_writer_wait); | |
7237f183 | 274 | } |
e02119d5 CM |
275 | } |
276 | ||
247462a5 DS |
277 | static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf) |
278 | { | |
279 | filemap_fdatawait_range(buf->pages[0]->mapping, | |
280 | buf->start, buf->start + buf->len - 1); | |
281 | } | |
e02119d5 CM |
282 | |
283 | /* | |
284 | * the walk control struct is used to pass state down the chain when | |
285 | * processing the log tree. The stage field tells us which part | |
286 | * of the log tree processing we are currently doing. The others | |
287 | * are state fields used for that specific part | |
288 | */ | |
289 | struct walk_control { | |
290 | /* should we free the extent on disk when done? This is used | |
291 | * at transaction commit time while freeing a log tree | |
292 | */ | |
293 | int free; | |
294 | ||
e02119d5 CM |
295 | /* pin only walk, we record which extents on disk belong to the |
296 | * log trees | |
297 | */ | |
298 | int pin; | |
299 | ||
300 | /* what stage of the replay code we're currently in */ | |
301 | int stage; | |
302 | ||
f2d72f42 FM |
303 | /* |
304 | * Ignore any items from the inode currently being processed. Needs | |
305 | * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in | |
306 | * the LOG_WALK_REPLAY_INODES stage. | |
307 | */ | |
308 | bool ignore_cur_inode; | |
309 | ||
e02119d5 CM |
310 | /* the root we are currently replaying */ |
311 | struct btrfs_root *replay_dest; | |
312 | ||
313 | /* the trans handle for the current replay */ | |
314 | struct btrfs_trans_handle *trans; | |
315 | ||
316 | /* the function that gets used to process blocks we find in the | |
317 | * tree. Note the extent_buffer might not be up to date when it is | |
318 | * passed in, and it must be checked or read if you need the data | |
319 | * inside it | |
320 | */ | |
321 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, | |
581c1760 | 322 | struct walk_control *wc, u64 gen, int level); |
e02119d5 CM |
323 | }; |
324 | ||
325 | /* | |
326 | * process_func used to pin down extents, write them or wait on them | |
327 | */ | |
328 | static int process_one_buffer(struct btrfs_root *log, | |
329 | struct extent_buffer *eb, | |
581c1760 | 330 | struct walk_control *wc, u64 gen, int level) |
e02119d5 | 331 | { |
0b246afa | 332 | struct btrfs_fs_info *fs_info = log->fs_info; |
b50c6e25 JB |
333 | int ret = 0; |
334 | ||
8c2a1a30 JB |
335 | /* |
336 | * If this fs is mixed then we need to be able to process the leaves to | |
337 | * pin down any logged extents, so we have to read the block. | |
338 | */ | |
0b246afa | 339 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
6a2e9dc4 | 340 | ret = btrfs_read_extent_buffer(eb, gen, level, NULL); |
8c2a1a30 JB |
341 | if (ret) |
342 | return ret; | |
343 | } | |
344 | ||
c816d705 | 345 | if (wc->pin) { |
9fce5704 | 346 | ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start, |
2ff7e61e | 347 | eb->len); |
c816d705 FM |
348 | if (ret) |
349 | return ret; | |
e02119d5 | 350 | |
c816d705 FM |
351 | if (btrfs_buffer_uptodate(eb, gen, 0) && |
352 | btrfs_header_level(eb) == 0) | |
bcdc428c | 353 | ret = btrfs_exclude_logged_extents(eb); |
e02119d5 | 354 | } |
b50c6e25 | 355 | return ret; |
e02119d5 CM |
356 | } |
357 | ||
086dcbfa FM |
358 | static int do_overwrite_item(struct btrfs_trans_handle *trans, |
359 | struct btrfs_root *root, | |
360 | struct btrfs_path *path, | |
361 | struct extent_buffer *eb, int slot, | |
362 | struct btrfs_key *key) | |
e02119d5 CM |
363 | { |
364 | int ret; | |
365 | u32 item_size; | |
366 | u64 saved_i_size = 0; | |
367 | int save_old_i_size = 0; | |
368 | unsigned long src_ptr; | |
369 | unsigned long dst_ptr; | |
370 | int overwrite_root = 0; | |
4bc4bee4 | 371 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; |
e02119d5 CM |
372 | |
373 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
374 | overwrite_root = 1; | |
375 | ||
3212fa14 | 376 | item_size = btrfs_item_size(eb, slot); |
e02119d5 CM |
377 | src_ptr = btrfs_item_ptr_offset(eb, slot); |
378 | ||
086dcbfa FM |
379 | /* Our caller must have done a search for the key for us. */ |
380 | ASSERT(path->nodes[0] != NULL); | |
381 | ||
382 | /* | |
383 | * And the slot must point to the exact key or the slot where the key | |
384 | * should be at (the first item with a key greater than 'key') | |
385 | */ | |
386 | if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) { | |
387 | struct btrfs_key found_key; | |
388 | ||
389 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); | |
390 | ret = btrfs_comp_cpu_keys(&found_key, key); | |
391 | ASSERT(ret >= 0); | |
392 | } else { | |
393 | ret = 1; | |
394 | } | |
4bc4bee4 | 395 | |
e02119d5 CM |
396 | if (ret == 0) { |
397 | char *src_copy; | |
398 | char *dst_copy; | |
3212fa14 | 399 | u32 dst_size = btrfs_item_size(path->nodes[0], |
e02119d5 CM |
400 | path->slots[0]); |
401 | if (dst_size != item_size) | |
402 | goto insert; | |
403 | ||
404 | if (item_size == 0) { | |
b3b4aa74 | 405 | btrfs_release_path(path); |
e02119d5 CM |
406 | return 0; |
407 | } | |
408 | dst_copy = kmalloc(item_size, GFP_NOFS); | |
409 | src_copy = kmalloc(item_size, GFP_NOFS); | |
2a29edc6 | 410 | if (!dst_copy || !src_copy) { |
b3b4aa74 | 411 | btrfs_release_path(path); |
2a29edc6 | 412 | kfree(dst_copy); |
413 | kfree(src_copy); | |
414 | return -ENOMEM; | |
415 | } | |
e02119d5 CM |
416 | |
417 | read_extent_buffer(eb, src_copy, src_ptr, item_size); | |
418 | ||
419 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
420 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, | |
421 | item_size); | |
422 | ret = memcmp(dst_copy, src_copy, item_size); | |
423 | ||
424 | kfree(dst_copy); | |
425 | kfree(src_copy); | |
426 | /* | |
427 | * they have the same contents, just return, this saves | |
428 | * us from cowing blocks in the destination tree and doing | |
429 | * extra writes that may not have been done by a previous | |
430 | * sync | |
431 | */ | |
432 | if (ret == 0) { | |
b3b4aa74 | 433 | btrfs_release_path(path); |
e02119d5 CM |
434 | return 0; |
435 | } | |
436 | ||
4bc4bee4 JB |
437 | /* |
438 | * We need to load the old nbytes into the inode so when we | |
439 | * replay the extents we've logged we get the right nbytes. | |
440 | */ | |
441 | if (inode_item) { | |
442 | struct btrfs_inode_item *item; | |
443 | u64 nbytes; | |
d555438b | 444 | u32 mode; |
4bc4bee4 JB |
445 | |
446 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
447 | struct btrfs_inode_item); | |
448 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); | |
449 | item = btrfs_item_ptr(eb, slot, | |
450 | struct btrfs_inode_item); | |
451 | btrfs_set_inode_nbytes(eb, item, nbytes); | |
d555438b JB |
452 | |
453 | /* | |
454 | * If this is a directory we need to reset the i_size to | |
455 | * 0 so that we can set it up properly when replaying | |
456 | * the rest of the items in this log. | |
457 | */ | |
458 | mode = btrfs_inode_mode(eb, item); | |
459 | if (S_ISDIR(mode)) | |
460 | btrfs_set_inode_size(eb, item, 0); | |
4bc4bee4 JB |
461 | } |
462 | } else if (inode_item) { | |
463 | struct btrfs_inode_item *item; | |
d555438b | 464 | u32 mode; |
4bc4bee4 JB |
465 | |
466 | /* | |
467 | * New inode, set nbytes to 0 so that the nbytes comes out | |
468 | * properly when we replay the extents. | |
469 | */ | |
470 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | |
471 | btrfs_set_inode_nbytes(eb, item, 0); | |
d555438b JB |
472 | |
473 | /* | |
474 | * If this is a directory we need to reset the i_size to 0 so | |
475 | * that we can set it up properly when replaying the rest of | |
476 | * the items in this log. | |
477 | */ | |
478 | mode = btrfs_inode_mode(eb, item); | |
479 | if (S_ISDIR(mode)) | |
480 | btrfs_set_inode_size(eb, item, 0); | |
e02119d5 CM |
481 | } |
482 | insert: | |
b3b4aa74 | 483 | btrfs_release_path(path); |
e02119d5 | 484 | /* try to insert the key into the destination tree */ |
df8d116f | 485 | path->skip_release_on_error = 1; |
e02119d5 CM |
486 | ret = btrfs_insert_empty_item(trans, root, path, |
487 | key, item_size); | |
df8d116f | 488 | path->skip_release_on_error = 0; |
e02119d5 CM |
489 | |
490 | /* make sure any existing item is the correct size */ | |
df8d116f | 491 | if (ret == -EEXIST || ret == -EOVERFLOW) { |
e02119d5 | 492 | u32 found_size; |
3212fa14 | 493 | found_size = btrfs_item_size(path->nodes[0], |
e02119d5 | 494 | path->slots[0]); |
143bede5 | 495 | if (found_size > item_size) |
78ac4f9e | 496 | btrfs_truncate_item(path, item_size, 1); |
143bede5 | 497 | else if (found_size < item_size) |
c71dd880 | 498 | btrfs_extend_item(path, item_size - found_size); |
e02119d5 | 499 | } else if (ret) { |
4a500fd1 | 500 | return ret; |
e02119d5 CM |
501 | } |
502 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], | |
503 | path->slots[0]); | |
504 | ||
505 | /* don't overwrite an existing inode if the generation number | |
506 | * was logged as zero. This is done when the tree logging code | |
507 | * is just logging an inode to make sure it exists after recovery. | |
508 | * | |
509 | * Also, don't overwrite i_size on directories during replay. | |
510 | * log replay inserts and removes directory items based on the | |
511 | * state of the tree found in the subvolume, and i_size is modified | |
512 | * as it goes | |
513 | */ | |
514 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { | |
515 | struct btrfs_inode_item *src_item; | |
516 | struct btrfs_inode_item *dst_item; | |
517 | ||
518 | src_item = (struct btrfs_inode_item *)src_ptr; | |
519 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
520 | ||
1a4bcf47 FM |
521 | if (btrfs_inode_generation(eb, src_item) == 0) { |
522 | struct extent_buffer *dst_eb = path->nodes[0]; | |
2f2ff0ee | 523 | const u64 ino_size = btrfs_inode_size(eb, src_item); |
1a4bcf47 | 524 | |
2f2ff0ee FM |
525 | /* |
526 | * For regular files an ino_size == 0 is used only when | |
527 | * logging that an inode exists, as part of a directory | |
528 | * fsync, and the inode wasn't fsynced before. In this | |
529 | * case don't set the size of the inode in the fs/subvol | |
530 | * tree, otherwise we would be throwing valid data away. | |
531 | */ | |
1a4bcf47 | 532 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && |
2f2ff0ee | 533 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && |
60d48e2e DS |
534 | ino_size != 0) |
535 | btrfs_set_inode_size(dst_eb, dst_item, ino_size); | |
e02119d5 | 536 | goto no_copy; |
1a4bcf47 | 537 | } |
e02119d5 CM |
538 | |
539 | if (overwrite_root && | |
540 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && | |
541 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { | |
542 | save_old_i_size = 1; | |
543 | saved_i_size = btrfs_inode_size(path->nodes[0], | |
544 | dst_item); | |
545 | } | |
546 | } | |
547 | ||
548 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, | |
549 | src_ptr, item_size); | |
550 | ||
551 | if (save_old_i_size) { | |
552 | struct btrfs_inode_item *dst_item; | |
553 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
554 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); | |
555 | } | |
556 | ||
557 | /* make sure the generation is filled in */ | |
558 | if (key->type == BTRFS_INODE_ITEM_KEY) { | |
559 | struct btrfs_inode_item *dst_item; | |
560 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
561 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { | |
562 | btrfs_set_inode_generation(path->nodes[0], dst_item, | |
563 | trans->transid); | |
564 | } | |
565 | } | |
566 | no_copy: | |
567 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
b3b4aa74 | 568 | btrfs_release_path(path); |
e02119d5 CM |
569 | return 0; |
570 | } | |
571 | ||
086dcbfa FM |
572 | /* |
573 | * Item overwrite used by replay and tree logging. eb, slot and key all refer | |
574 | * to the src data we are copying out. | |
575 | * | |
576 | * root is the tree we are copying into, and path is a scratch | |
577 | * path for use in this function (it should be released on entry and | |
578 | * will be released on exit). | |
579 | * | |
580 | * If the key is already in the destination tree the existing item is | |
581 | * overwritten. If the existing item isn't big enough, it is extended. | |
582 | * If it is too large, it is truncated. | |
583 | * | |
584 | * If the key isn't in the destination yet, a new item is inserted. | |
585 | */ | |
586 | static int overwrite_item(struct btrfs_trans_handle *trans, | |
587 | struct btrfs_root *root, | |
588 | struct btrfs_path *path, | |
589 | struct extent_buffer *eb, int slot, | |
590 | struct btrfs_key *key) | |
591 | { | |
592 | int ret; | |
593 | ||
594 | /* Look for the key in the destination tree. */ | |
595 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
596 | if (ret < 0) | |
597 | return ret; | |
598 | ||
599 | return do_overwrite_item(trans, root, path, eb, slot, key); | |
600 | } | |
601 | ||
e43eec81 | 602 | static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len, |
6db75318 | 603 | struct fscrypt_str *name) |
e43eec81 STD |
604 | { |
605 | char *buf; | |
606 | ||
607 | buf = kmalloc(len, GFP_NOFS); | |
608 | if (!buf) | |
609 | return -ENOMEM; | |
610 | ||
611 | read_extent_buffer(eb, buf, (unsigned long)start, len); | |
612 | name->name = buf; | |
613 | name->len = len; | |
614 | return 0; | |
615 | } | |
616 | ||
e02119d5 CM |
617 | /* |
618 | * simple helper to read an inode off the disk from a given root | |
619 | * This can only be called for subvolume roots and not for the log | |
620 | */ | |
621 | static noinline struct inode *read_one_inode(struct btrfs_root *root, | |
622 | u64 objectid) | |
623 | { | |
624 | struct inode *inode; | |
e02119d5 | 625 | |
0202e83f | 626 | inode = btrfs_iget(root->fs_info->sb, objectid, root); |
2e19f1f9 | 627 | if (IS_ERR(inode)) |
5d4f98a2 | 628 | inode = NULL; |
e02119d5 CM |
629 | return inode; |
630 | } | |
631 | ||
632 | /* replays a single extent in 'eb' at 'slot' with 'key' into the | |
633 | * subvolume 'root'. path is released on entry and should be released | |
634 | * on exit. | |
635 | * | |
636 | * extents in the log tree have not been allocated out of the extent | |
637 | * tree yet. So, this completes the allocation, taking a reference | |
638 | * as required if the extent already exists or creating a new extent | |
639 | * if it isn't in the extent allocation tree yet. | |
640 | * | |
641 | * The extent is inserted into the file, dropping any existing extents | |
642 | * from the file that overlap the new one. | |
643 | */ | |
644 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |
645 | struct btrfs_root *root, | |
646 | struct btrfs_path *path, | |
647 | struct extent_buffer *eb, int slot, | |
648 | struct btrfs_key *key) | |
649 | { | |
5893dfb9 | 650 | struct btrfs_drop_extents_args drop_args = { 0 }; |
0b246afa | 651 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 | 652 | int found_type; |
e02119d5 | 653 | u64 extent_end; |
e02119d5 | 654 | u64 start = key->offset; |
4bc4bee4 | 655 | u64 nbytes = 0; |
e02119d5 CM |
656 | struct btrfs_file_extent_item *item; |
657 | struct inode *inode = NULL; | |
658 | unsigned long size; | |
659 | int ret = 0; | |
660 | ||
661 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
662 | found_type = btrfs_file_extent_type(eb, item); | |
663 | ||
d899e052 | 664 | if (found_type == BTRFS_FILE_EXTENT_REG || |
4bc4bee4 JB |
665 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
666 | nbytes = btrfs_file_extent_num_bytes(eb, item); | |
667 | extent_end = start + nbytes; | |
668 | ||
669 | /* | |
670 | * We don't add to the inodes nbytes if we are prealloc or a | |
671 | * hole. | |
672 | */ | |
673 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) | |
674 | nbytes = 0; | |
675 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
e41ca589 | 676 | size = btrfs_file_extent_ram_bytes(eb, item); |
4bc4bee4 | 677 | nbytes = btrfs_file_extent_ram_bytes(eb, item); |
da17066c | 678 | extent_end = ALIGN(start + size, |
0b246afa | 679 | fs_info->sectorsize); |
e02119d5 CM |
680 | } else { |
681 | ret = 0; | |
682 | goto out; | |
683 | } | |
684 | ||
685 | inode = read_one_inode(root, key->objectid); | |
686 | if (!inode) { | |
687 | ret = -EIO; | |
688 | goto out; | |
689 | } | |
690 | ||
691 | /* | |
692 | * first check to see if we already have this extent in the | |
693 | * file. This must be done before the btrfs_drop_extents run | |
694 | * so we don't try to drop this extent. | |
695 | */ | |
f85b7379 DS |
696 | ret = btrfs_lookup_file_extent(trans, root, path, |
697 | btrfs_ino(BTRFS_I(inode)), start, 0); | |
e02119d5 | 698 | |
d899e052 YZ |
699 | if (ret == 0 && |
700 | (found_type == BTRFS_FILE_EXTENT_REG || | |
701 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { | |
e02119d5 CM |
702 | struct btrfs_file_extent_item cmp1; |
703 | struct btrfs_file_extent_item cmp2; | |
704 | struct btrfs_file_extent_item *existing; | |
705 | struct extent_buffer *leaf; | |
706 | ||
707 | leaf = path->nodes[0]; | |
708 | existing = btrfs_item_ptr(leaf, path->slots[0], | |
709 | struct btrfs_file_extent_item); | |
710 | ||
711 | read_extent_buffer(eb, &cmp1, (unsigned long)item, | |
712 | sizeof(cmp1)); | |
713 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, | |
714 | sizeof(cmp2)); | |
715 | ||
716 | /* | |
717 | * we already have a pointer to this exact extent, | |
718 | * we don't have to do anything | |
719 | */ | |
720 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { | |
b3b4aa74 | 721 | btrfs_release_path(path); |
e02119d5 CM |
722 | goto out; |
723 | } | |
724 | } | |
b3b4aa74 | 725 | btrfs_release_path(path); |
e02119d5 CM |
726 | |
727 | /* drop any overlapping extents */ | |
5893dfb9 FM |
728 | drop_args.start = start; |
729 | drop_args.end = extent_end; | |
730 | drop_args.drop_cache = true; | |
731 | ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args); | |
3650860b JB |
732 | if (ret) |
733 | goto out; | |
e02119d5 | 734 | |
07d400a6 YZ |
735 | if (found_type == BTRFS_FILE_EXTENT_REG || |
736 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
5d4f98a2 | 737 | u64 offset; |
07d400a6 YZ |
738 | unsigned long dest_offset; |
739 | struct btrfs_key ins; | |
740 | ||
3168021c FM |
741 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && |
742 | btrfs_fs_incompat(fs_info, NO_HOLES)) | |
743 | goto update_inode; | |
744 | ||
07d400a6 YZ |
745 | ret = btrfs_insert_empty_item(trans, root, path, key, |
746 | sizeof(*item)); | |
3650860b JB |
747 | if (ret) |
748 | goto out; | |
07d400a6 YZ |
749 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
750 | path->slots[0]); | |
751 | copy_extent_buffer(path->nodes[0], eb, dest_offset, | |
752 | (unsigned long)item, sizeof(*item)); | |
753 | ||
754 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); | |
755 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); | |
756 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 | 757 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
07d400a6 | 758 | |
df2c95f3 QW |
759 | /* |
760 | * Manually record dirty extent, as here we did a shallow | |
761 | * file extent item copy and skip normal backref update, | |
762 | * but modifying extent tree all by ourselves. | |
763 | * So need to manually record dirty extent for qgroup, | |
764 | * as the owner of the file extent changed from log tree | |
765 | * (doesn't affect qgroup) to fs/file tree(affects qgroup) | |
766 | */ | |
a95f3aaf | 767 | ret = btrfs_qgroup_trace_extent(trans, |
df2c95f3 | 768 | btrfs_file_extent_disk_bytenr(eb, item), |
e2896e79 | 769 | btrfs_file_extent_disk_num_bytes(eb, item)); |
df2c95f3 QW |
770 | if (ret < 0) |
771 | goto out; | |
772 | ||
07d400a6 | 773 | if (ins.objectid > 0) { |
82fa113f | 774 | struct btrfs_ref ref = { 0 }; |
07d400a6 YZ |
775 | u64 csum_start; |
776 | u64 csum_end; | |
777 | LIST_HEAD(ordered_sums); | |
82fa113f | 778 | |
07d400a6 YZ |
779 | /* |
780 | * is this extent already allocated in the extent | |
781 | * allocation tree? If so, just add a reference | |
782 | */ | |
2ff7e61e | 783 | ret = btrfs_lookup_data_extent(fs_info, ins.objectid, |
07d400a6 | 784 | ins.offset); |
3736127a MPS |
785 | if (ret < 0) { |
786 | goto out; | |
787 | } else if (ret == 0) { | |
82fa113f QW |
788 | btrfs_init_generic_ref(&ref, |
789 | BTRFS_ADD_DELAYED_REF, | |
790 | ins.objectid, ins.offset, 0); | |
791 | btrfs_init_data_ref(&ref, | |
792 | root->root_key.objectid, | |
f42c5da6 | 793 | key->objectid, offset, 0, false); |
82fa113f | 794 | ret = btrfs_inc_extent_ref(trans, &ref); |
b50c6e25 JB |
795 | if (ret) |
796 | goto out; | |
07d400a6 YZ |
797 | } else { |
798 | /* | |
799 | * insert the extent pointer in the extent | |
800 | * allocation tree | |
801 | */ | |
5d4f98a2 | 802 | ret = btrfs_alloc_logged_file_extent(trans, |
2ff7e61e | 803 | root->root_key.objectid, |
5d4f98a2 | 804 | key->objectid, offset, &ins); |
b50c6e25 JB |
805 | if (ret) |
806 | goto out; | |
07d400a6 | 807 | } |
b3b4aa74 | 808 | btrfs_release_path(path); |
07d400a6 YZ |
809 | |
810 | if (btrfs_file_extent_compression(eb, item)) { | |
811 | csum_start = ins.objectid; | |
812 | csum_end = csum_start + ins.offset; | |
813 | } else { | |
814 | csum_start = ins.objectid + | |
815 | btrfs_file_extent_offset(eb, item); | |
816 | csum_end = csum_start + | |
817 | btrfs_file_extent_num_bytes(eb, item); | |
818 | } | |
819 | ||
820 | ret = btrfs_lookup_csums_range(root->log_root, | |
821 | csum_start, csum_end - 1, | |
26ce9114 | 822 | &ordered_sums, 0, false); |
3650860b JB |
823 | if (ret) |
824 | goto out; | |
b84b8390 FM |
825 | /* |
826 | * Now delete all existing cums in the csum root that | |
827 | * cover our range. We do this because we can have an | |
828 | * extent that is completely referenced by one file | |
829 | * extent item and partially referenced by another | |
830 | * file extent item (like after using the clone or | |
831 | * extent_same ioctls). In this case if we end up doing | |
832 | * the replay of the one that partially references the | |
833 | * extent first, and we do not do the csum deletion | |
834 | * below, we can get 2 csum items in the csum tree that | |
835 | * overlap each other. For example, imagine our log has | |
836 | * the two following file extent items: | |
837 | * | |
838 | * key (257 EXTENT_DATA 409600) | |
839 | * extent data disk byte 12845056 nr 102400 | |
840 | * extent data offset 20480 nr 20480 ram 102400 | |
841 | * | |
842 | * key (257 EXTENT_DATA 819200) | |
843 | * extent data disk byte 12845056 nr 102400 | |
844 | * extent data offset 0 nr 102400 ram 102400 | |
845 | * | |
846 | * Where the second one fully references the 100K extent | |
847 | * that starts at disk byte 12845056, and the log tree | |
848 | * has a single csum item that covers the entire range | |
849 | * of the extent: | |
850 | * | |
851 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 | |
852 | * | |
853 | * After the first file extent item is replayed, the | |
854 | * csum tree gets the following csum item: | |
855 | * | |
856 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 | |
857 | * | |
858 | * Which covers the 20K sub-range starting at offset 20K | |
859 | * of our extent. Now when we replay the second file | |
860 | * extent item, if we do not delete existing csum items | |
861 | * that cover any of its blocks, we end up getting two | |
862 | * csum items in our csum tree that overlap each other: | |
863 | * | |
864 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 | |
865 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 | |
866 | * | |
867 | * Which is a problem, because after this anyone trying | |
868 | * to lookup up for the checksum of any block of our | |
869 | * extent starting at an offset of 40K or higher, will | |
870 | * end up looking at the second csum item only, which | |
871 | * does not contain the checksum for any block starting | |
872 | * at offset 40K or higher of our extent. | |
873 | */ | |
07d400a6 YZ |
874 | while (!list_empty(&ordered_sums)) { |
875 | struct btrfs_ordered_sum *sums; | |
fc28b25e JB |
876 | struct btrfs_root *csum_root; |
877 | ||
07d400a6 YZ |
878 | sums = list_entry(ordered_sums.next, |
879 | struct btrfs_ordered_sum, | |
880 | list); | |
fc28b25e JB |
881 | csum_root = btrfs_csum_root(fs_info, |
882 | sums->bytenr); | |
b84b8390 | 883 | if (!ret) |
fc28b25e | 884 | ret = btrfs_del_csums(trans, csum_root, |
5b4aacef JM |
885 | sums->bytenr, |
886 | sums->len); | |
3650860b JB |
887 | if (!ret) |
888 | ret = btrfs_csum_file_blocks(trans, | |
fc28b25e JB |
889 | csum_root, |
890 | sums); | |
07d400a6 YZ |
891 | list_del(&sums->list); |
892 | kfree(sums); | |
893 | } | |
3650860b JB |
894 | if (ret) |
895 | goto out; | |
07d400a6 | 896 | } else { |
b3b4aa74 | 897 | btrfs_release_path(path); |
07d400a6 YZ |
898 | } |
899 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
900 | /* inline extents are easy, we just overwrite them */ | |
901 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
3650860b JB |
902 | if (ret) |
903 | goto out; | |
07d400a6 | 904 | } |
e02119d5 | 905 | |
9ddc959e JB |
906 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, |
907 | extent_end - start); | |
908 | if (ret) | |
909 | goto out; | |
910 | ||
3168021c | 911 | update_inode: |
2766ff61 | 912 | btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found); |
9a56fcd1 | 913 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
e02119d5 | 914 | out: |
8aa1e49e | 915 | iput(inode); |
e02119d5 CM |
916 | return ret; |
917 | } | |
918 | ||
313ab753 FM |
919 | static int unlink_inode_for_log_replay(struct btrfs_trans_handle *trans, |
920 | struct btrfs_inode *dir, | |
921 | struct btrfs_inode *inode, | |
6db75318 | 922 | const struct fscrypt_str *name) |
313ab753 FM |
923 | { |
924 | int ret; | |
925 | ||
e43eec81 | 926 | ret = btrfs_unlink_inode(trans, dir, inode, name); |
313ab753 FM |
927 | if (ret) |
928 | return ret; | |
929 | /* | |
930 | * Whenever we need to check if a name exists or not, we check the | |
931 | * fs/subvolume tree. So after an unlink we must run delayed items, so | |
932 | * that future checks for a name during log replay see that the name | |
933 | * does not exists anymore. | |
934 | */ | |
935 | return btrfs_run_delayed_items(trans); | |
936 | } | |
937 | ||
e02119d5 CM |
938 | /* |
939 | * when cleaning up conflicts between the directory names in the | |
940 | * subvolume, directory names in the log and directory names in the | |
941 | * inode back references, we may have to unlink inodes from directories. | |
942 | * | |
943 | * This is a helper function to do the unlink of a specific directory | |
944 | * item | |
945 | */ | |
946 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, | |
e02119d5 | 947 | struct btrfs_path *path, |
207e7d92 | 948 | struct btrfs_inode *dir, |
e02119d5 CM |
949 | struct btrfs_dir_item *di) |
950 | { | |
9798ba24 | 951 | struct btrfs_root *root = dir->root; |
e02119d5 | 952 | struct inode *inode; |
6db75318 | 953 | struct fscrypt_str name; |
e02119d5 CM |
954 | struct extent_buffer *leaf; |
955 | struct btrfs_key location; | |
956 | int ret; | |
957 | ||
958 | leaf = path->nodes[0]; | |
959 | ||
960 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
e43eec81 STD |
961 | ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name); |
962 | if (ret) | |
2a29edc6 | 963 | return -ENOMEM; |
964 | ||
b3b4aa74 | 965 | btrfs_release_path(path); |
e02119d5 CM |
966 | |
967 | inode = read_one_inode(root, location.objectid); | |
c00e9493 | 968 | if (!inode) { |
3650860b JB |
969 | ret = -EIO; |
970 | goto out; | |
c00e9493 | 971 | } |
e02119d5 | 972 | |
ec051c0f | 973 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
3650860b JB |
974 | if (ret) |
975 | goto out; | |
12fcfd22 | 976 | |
e43eec81 | 977 | ret = unlink_inode_for_log_replay(trans, dir, BTRFS_I(inode), &name); |
3650860b | 978 | out: |
e43eec81 | 979 | kfree(name.name); |
e02119d5 CM |
980 | iput(inode); |
981 | return ret; | |
982 | } | |
983 | ||
984 | /* | |
77a5b9e3 FM |
985 | * See if a given name and sequence number found in an inode back reference are |
986 | * already in a directory and correctly point to this inode. | |
987 | * | |
988 | * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it | |
989 | * exists. | |
e02119d5 CM |
990 | */ |
991 | static noinline int inode_in_dir(struct btrfs_root *root, | |
992 | struct btrfs_path *path, | |
993 | u64 dirid, u64 objectid, u64 index, | |
6db75318 | 994 | struct fscrypt_str *name) |
e02119d5 CM |
995 | { |
996 | struct btrfs_dir_item *di; | |
997 | struct btrfs_key location; | |
77a5b9e3 | 998 | int ret = 0; |
e02119d5 CM |
999 | |
1000 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, | |
e43eec81 | 1001 | index, name, 0); |
77a5b9e3 | 1002 | if (IS_ERR(di)) { |
8dcbc261 | 1003 | ret = PTR_ERR(di); |
77a5b9e3 FM |
1004 | goto out; |
1005 | } else if (di) { | |
e02119d5 CM |
1006 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
1007 | if (location.objectid != objectid) | |
1008 | goto out; | |
77a5b9e3 | 1009 | } else { |
e02119d5 | 1010 | goto out; |
77a5b9e3 | 1011 | } |
e02119d5 | 1012 | |
77a5b9e3 | 1013 | btrfs_release_path(path); |
e43eec81 | 1014 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0); |
77a5b9e3 FM |
1015 | if (IS_ERR(di)) { |
1016 | ret = PTR_ERR(di); | |
e02119d5 | 1017 | goto out; |
77a5b9e3 FM |
1018 | } else if (di) { |
1019 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
1020 | if (location.objectid == objectid) | |
1021 | ret = 1; | |
1022 | } | |
e02119d5 | 1023 | out: |
b3b4aa74 | 1024 | btrfs_release_path(path); |
77a5b9e3 | 1025 | return ret; |
e02119d5 CM |
1026 | } |
1027 | ||
1028 | /* | |
1029 | * helper function to check a log tree for a named back reference in | |
1030 | * an inode. This is used to decide if a back reference that is | |
1031 | * found in the subvolume conflicts with what we find in the log. | |
1032 | * | |
1033 | * inode backreferences may have multiple refs in a single item, | |
1034 | * during replay we process one reference at a time, and we don't | |
1035 | * want to delete valid links to a file from the subvolume if that | |
1036 | * link is also in the log. | |
1037 | */ | |
1038 | static noinline int backref_in_log(struct btrfs_root *log, | |
1039 | struct btrfs_key *key, | |
f186373f | 1040 | u64 ref_objectid, |
6db75318 | 1041 | const struct fscrypt_str *name) |
e02119d5 CM |
1042 | { |
1043 | struct btrfs_path *path; | |
e02119d5 | 1044 | int ret; |
e02119d5 CM |
1045 | |
1046 | path = btrfs_alloc_path(); | |
2a29edc6 | 1047 | if (!path) |
1048 | return -ENOMEM; | |
1049 | ||
e02119d5 | 1050 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
d3316c82 NB |
1051 | if (ret < 0) { |
1052 | goto out; | |
1053 | } else if (ret == 1) { | |
89cbf5f6 | 1054 | ret = 0; |
f186373f MF |
1055 | goto out; |
1056 | } | |
1057 | ||
89cbf5f6 NB |
1058 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
1059 | ret = !!btrfs_find_name_in_ext_backref(path->nodes[0], | |
1060 | path->slots[0], | |
e43eec81 | 1061 | ref_objectid, name); |
89cbf5f6 NB |
1062 | else |
1063 | ret = !!btrfs_find_name_in_backref(path->nodes[0], | |
e43eec81 | 1064 | path->slots[0], name); |
e02119d5 CM |
1065 | out: |
1066 | btrfs_free_path(path); | |
89cbf5f6 | 1067 | return ret; |
e02119d5 CM |
1068 | } |
1069 | ||
5a1d7843 | 1070 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
e02119d5 | 1071 | struct btrfs_root *root, |
e02119d5 | 1072 | struct btrfs_path *path, |
5a1d7843 | 1073 | struct btrfs_root *log_root, |
94c91a1f NB |
1074 | struct btrfs_inode *dir, |
1075 | struct btrfs_inode *inode, | |
f186373f | 1076 | u64 inode_objectid, u64 parent_objectid, |
6db75318 | 1077 | u64 ref_index, struct fscrypt_str *name) |
e02119d5 | 1078 | { |
34f3e4f2 | 1079 | int ret; |
f186373f | 1080 | struct extent_buffer *leaf; |
5a1d7843 | 1081 | struct btrfs_dir_item *di; |
f186373f MF |
1082 | struct btrfs_key search_key; |
1083 | struct btrfs_inode_extref *extref; | |
c622ae60 | 1084 | |
f186373f MF |
1085 | again: |
1086 | /* Search old style refs */ | |
1087 | search_key.objectid = inode_objectid; | |
1088 | search_key.type = BTRFS_INODE_REF_KEY; | |
1089 | search_key.offset = parent_objectid; | |
1090 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
e02119d5 | 1091 | if (ret == 0) { |
e02119d5 CM |
1092 | struct btrfs_inode_ref *victim_ref; |
1093 | unsigned long ptr; | |
1094 | unsigned long ptr_end; | |
f186373f MF |
1095 | |
1096 | leaf = path->nodes[0]; | |
e02119d5 CM |
1097 | |
1098 | /* are we trying to overwrite a back ref for the root directory | |
1099 | * if so, just jump out, we're done | |
1100 | */ | |
f186373f | 1101 | if (search_key.objectid == search_key.offset) |
5a1d7843 | 1102 | return 1; |
e02119d5 CM |
1103 | |
1104 | /* check all the names in this back reference to see | |
1105 | * if they are in the log. if so, we allow them to stay | |
1106 | * otherwise they must be unlinked as a conflict | |
1107 | */ | |
1108 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3212fa14 | 1109 | ptr_end = ptr + btrfs_item_size(leaf, path->slots[0]); |
d397712b | 1110 | while (ptr < ptr_end) { |
6db75318 | 1111 | struct fscrypt_str victim_name; |
e02119d5 | 1112 | |
e43eec81 STD |
1113 | victim_ref = (struct btrfs_inode_ref *)ptr; |
1114 | ret = read_alloc_one_name(leaf, (victim_ref + 1), | |
1115 | btrfs_inode_ref_name_len(leaf, victim_ref), | |
1116 | &victim_name); | |
1117 | if (ret) | |
1118 | return ret; | |
e02119d5 | 1119 | |
d3316c82 | 1120 | ret = backref_in_log(log_root, &search_key, |
e43eec81 | 1121 | parent_objectid, &victim_name); |
d3316c82 | 1122 | if (ret < 0) { |
e43eec81 | 1123 | kfree(victim_name.name); |
d3316c82 NB |
1124 | return ret; |
1125 | } else if (!ret) { | |
94c91a1f | 1126 | inc_nlink(&inode->vfs_inode); |
b3b4aa74 | 1127 | btrfs_release_path(path); |
12fcfd22 | 1128 | |
313ab753 | 1129 | ret = unlink_inode_for_log_replay(trans, dir, inode, |
e43eec81 STD |
1130 | &victim_name); |
1131 | kfree(victim_name.name); | |
ada9af21 FDBM |
1132 | if (ret) |
1133 | return ret; | |
f186373f | 1134 | goto again; |
e02119d5 | 1135 | } |
e43eec81 | 1136 | kfree(victim_name.name); |
f186373f | 1137 | |
e43eec81 | 1138 | ptr = (unsigned long)(victim_ref + 1) + victim_name.len; |
e02119d5 | 1139 | } |
e02119d5 | 1140 | } |
b3b4aa74 | 1141 | btrfs_release_path(path); |
e02119d5 | 1142 | |
f186373f | 1143 | /* Same search but for extended refs */ |
e43eec81 | 1144 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, |
f186373f MF |
1145 | inode_objectid, parent_objectid, 0, |
1146 | 0); | |
7a6b75b7 FM |
1147 | if (IS_ERR(extref)) { |
1148 | return PTR_ERR(extref); | |
1149 | } else if (extref) { | |
f186373f MF |
1150 | u32 item_size; |
1151 | u32 cur_offset = 0; | |
1152 | unsigned long base; | |
1153 | struct inode *victim_parent; | |
1154 | ||
1155 | leaf = path->nodes[0]; | |
1156 | ||
3212fa14 | 1157 | item_size = btrfs_item_size(leaf, path->slots[0]); |
f186373f MF |
1158 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); |
1159 | ||
1160 | while (cur_offset < item_size) { | |
6db75318 | 1161 | struct fscrypt_str victim_name; |
f186373f | 1162 | |
e43eec81 | 1163 | extref = (struct btrfs_inode_extref *)(base + cur_offset); |
f186373f MF |
1164 | |
1165 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) | |
1166 | goto next; | |
1167 | ||
e43eec81 STD |
1168 | ret = read_alloc_one_name(leaf, &extref->name, |
1169 | btrfs_inode_extref_name_len(leaf, extref), | |
1170 | &victim_name); | |
1171 | if (ret) | |
1172 | return ret; | |
f186373f MF |
1173 | |
1174 | search_key.objectid = inode_objectid; | |
1175 | search_key.type = BTRFS_INODE_EXTREF_KEY; | |
1176 | search_key.offset = btrfs_extref_hash(parent_objectid, | |
e43eec81 STD |
1177 | victim_name.name, |
1178 | victim_name.len); | |
d3316c82 | 1179 | ret = backref_in_log(log_root, &search_key, |
e43eec81 | 1180 | parent_objectid, &victim_name); |
d3316c82 | 1181 | if (ret < 0) { |
e43eec81 | 1182 | kfree(victim_name.name); |
d3316c82 NB |
1183 | return ret; |
1184 | } else if (!ret) { | |
f186373f MF |
1185 | ret = -ENOENT; |
1186 | victim_parent = read_one_inode(root, | |
94c91a1f | 1187 | parent_objectid); |
f186373f | 1188 | if (victim_parent) { |
94c91a1f | 1189 | inc_nlink(&inode->vfs_inode); |
f186373f MF |
1190 | btrfs_release_path(path); |
1191 | ||
313ab753 | 1192 | ret = unlink_inode_for_log_replay(trans, |
4ec5934e | 1193 | BTRFS_I(victim_parent), |
e43eec81 | 1194 | inode, &victim_name); |
f186373f | 1195 | } |
f186373f | 1196 | iput(victim_parent); |
e43eec81 | 1197 | kfree(victim_name.name); |
3650860b JB |
1198 | if (ret) |
1199 | return ret; | |
f186373f MF |
1200 | goto again; |
1201 | } | |
e43eec81 | 1202 | kfree(victim_name.name); |
f186373f | 1203 | next: |
e43eec81 | 1204 | cur_offset += victim_name.len + sizeof(*extref); |
f186373f | 1205 | } |
f186373f MF |
1206 | } |
1207 | btrfs_release_path(path); | |
1208 | ||
34f3e4f2 | 1209 | /* look for a conflicting sequence number */ |
94c91a1f | 1210 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), |
e43eec81 | 1211 | ref_index, name, 0); |
52db7779 | 1212 | if (IS_ERR(di)) { |
8dcbc261 | 1213 | return PTR_ERR(di); |
52db7779 | 1214 | } else if (di) { |
9798ba24 | 1215 | ret = drop_one_dir_item(trans, path, dir, di); |
3650860b JB |
1216 | if (ret) |
1217 | return ret; | |
34f3e4f2 | 1218 | } |
1219 | btrfs_release_path(path); | |
1220 | ||
52042d8e | 1221 | /* look for a conflicting name */ |
e43eec81 | 1222 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), name, 0); |
52db7779 FM |
1223 | if (IS_ERR(di)) { |
1224 | return PTR_ERR(di); | |
1225 | } else if (di) { | |
9798ba24 | 1226 | ret = drop_one_dir_item(trans, path, dir, di); |
3650860b JB |
1227 | if (ret) |
1228 | return ret; | |
34f3e4f2 | 1229 | } |
1230 | btrfs_release_path(path); | |
1231 | ||
5a1d7843 JS |
1232 | return 0; |
1233 | } | |
e02119d5 | 1234 | |
bae15d95 | 1235 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
6db75318 | 1236 | struct fscrypt_str *name, u64 *index, |
bae15d95 | 1237 | u64 *parent_objectid) |
f186373f MF |
1238 | { |
1239 | struct btrfs_inode_extref *extref; | |
e43eec81 | 1240 | int ret; |
f186373f MF |
1241 | |
1242 | extref = (struct btrfs_inode_extref *)ref_ptr; | |
1243 | ||
e43eec81 STD |
1244 | ret = read_alloc_one_name(eb, &extref->name, |
1245 | btrfs_inode_extref_name_len(eb, extref), name); | |
1246 | if (ret) | |
1247 | return ret; | |
f186373f | 1248 | |
1f250e92 FM |
1249 | if (index) |
1250 | *index = btrfs_inode_extref_index(eb, extref); | |
f186373f MF |
1251 | if (parent_objectid) |
1252 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); | |
1253 | ||
1254 | return 0; | |
1255 | } | |
1256 | ||
bae15d95 | 1257 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
6db75318 | 1258 | struct fscrypt_str *name, u64 *index) |
f186373f MF |
1259 | { |
1260 | struct btrfs_inode_ref *ref; | |
e43eec81 | 1261 | int ret; |
f186373f MF |
1262 | |
1263 | ref = (struct btrfs_inode_ref *)ref_ptr; | |
1264 | ||
e43eec81 STD |
1265 | ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref), |
1266 | name); | |
1267 | if (ret) | |
1268 | return ret; | |
f186373f | 1269 | |
1f250e92 FM |
1270 | if (index) |
1271 | *index = btrfs_inode_ref_index(eb, ref); | |
f186373f MF |
1272 | |
1273 | return 0; | |
1274 | } | |
1275 | ||
1f250e92 FM |
1276 | /* |
1277 | * Take an inode reference item from the log tree and iterate all names from the | |
1278 | * inode reference item in the subvolume tree with the same key (if it exists). | |
1279 | * For any name that is not in the inode reference item from the log tree, do a | |
1280 | * proper unlink of that name (that is, remove its entry from the inode | |
1281 | * reference item and both dir index keys). | |
1282 | */ | |
1283 | static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, | |
1284 | struct btrfs_root *root, | |
1285 | struct btrfs_path *path, | |
1286 | struct btrfs_inode *inode, | |
1287 | struct extent_buffer *log_eb, | |
1288 | int log_slot, | |
1289 | struct btrfs_key *key) | |
1290 | { | |
1291 | int ret; | |
1292 | unsigned long ref_ptr; | |
1293 | unsigned long ref_end; | |
1294 | struct extent_buffer *eb; | |
1295 | ||
1296 | again: | |
1297 | btrfs_release_path(path); | |
1298 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
1299 | if (ret > 0) { | |
1300 | ret = 0; | |
1301 | goto out; | |
1302 | } | |
1303 | if (ret < 0) | |
1304 | goto out; | |
1305 | ||
1306 | eb = path->nodes[0]; | |
1307 | ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); | |
3212fa14 | 1308 | ref_end = ref_ptr + btrfs_item_size(eb, path->slots[0]); |
1f250e92 | 1309 | while (ref_ptr < ref_end) { |
6db75318 | 1310 | struct fscrypt_str name; |
1f250e92 FM |
1311 | u64 parent_id; |
1312 | ||
1313 | if (key->type == BTRFS_INODE_EXTREF_KEY) { | |
e43eec81 | 1314 | ret = extref_get_fields(eb, ref_ptr, &name, |
1f250e92 FM |
1315 | NULL, &parent_id); |
1316 | } else { | |
1317 | parent_id = key->offset; | |
e43eec81 | 1318 | ret = ref_get_fields(eb, ref_ptr, &name, NULL); |
1f250e92 FM |
1319 | } |
1320 | if (ret) | |
1321 | goto out; | |
1322 | ||
1323 | if (key->type == BTRFS_INODE_EXTREF_KEY) | |
6ff49c6a | 1324 | ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot, |
e43eec81 | 1325 | parent_id, &name); |
1f250e92 | 1326 | else |
e43eec81 | 1327 | ret = !!btrfs_find_name_in_backref(log_eb, log_slot, &name); |
1f250e92 FM |
1328 | |
1329 | if (!ret) { | |
1330 | struct inode *dir; | |
1331 | ||
1332 | btrfs_release_path(path); | |
1333 | dir = read_one_inode(root, parent_id); | |
1334 | if (!dir) { | |
1335 | ret = -ENOENT; | |
e43eec81 | 1336 | kfree(name.name); |
1f250e92 FM |
1337 | goto out; |
1338 | } | |
313ab753 | 1339 | ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir), |
e43eec81 STD |
1340 | inode, &name); |
1341 | kfree(name.name); | |
1f250e92 FM |
1342 | iput(dir); |
1343 | if (ret) | |
1344 | goto out; | |
1345 | goto again; | |
1346 | } | |
1347 | ||
e43eec81 STD |
1348 | kfree(name.name); |
1349 | ref_ptr += name.len; | |
1f250e92 FM |
1350 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
1351 | ref_ptr += sizeof(struct btrfs_inode_extref); | |
1352 | else | |
1353 | ref_ptr += sizeof(struct btrfs_inode_ref); | |
1354 | } | |
1355 | ret = 0; | |
1356 | out: | |
1357 | btrfs_release_path(path); | |
1358 | return ret; | |
1359 | } | |
1360 | ||
5a1d7843 JS |
1361 | /* |
1362 | * replay one inode back reference item found in the log tree. | |
1363 | * eb, slot and key refer to the buffer and key found in the log tree. | |
1364 | * root is the destination we are replaying into, and path is for temp | |
1365 | * use by this function. (it should be released on return). | |
1366 | */ | |
1367 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | |
1368 | struct btrfs_root *root, | |
1369 | struct btrfs_root *log, | |
1370 | struct btrfs_path *path, | |
1371 | struct extent_buffer *eb, int slot, | |
1372 | struct btrfs_key *key) | |
1373 | { | |
03b2f08b GB |
1374 | struct inode *dir = NULL; |
1375 | struct inode *inode = NULL; | |
5a1d7843 JS |
1376 | unsigned long ref_ptr; |
1377 | unsigned long ref_end; | |
6db75318 | 1378 | struct fscrypt_str name; |
5a1d7843 | 1379 | int ret; |
f186373f MF |
1380 | int log_ref_ver = 0; |
1381 | u64 parent_objectid; | |
1382 | u64 inode_objectid; | |
f46dbe3d | 1383 | u64 ref_index = 0; |
f186373f MF |
1384 | int ref_struct_size; |
1385 | ||
1386 | ref_ptr = btrfs_item_ptr_offset(eb, slot); | |
3212fa14 | 1387 | ref_end = ref_ptr + btrfs_item_size(eb, slot); |
f186373f MF |
1388 | |
1389 | if (key->type == BTRFS_INODE_EXTREF_KEY) { | |
1390 | struct btrfs_inode_extref *r; | |
1391 | ||
1392 | ref_struct_size = sizeof(struct btrfs_inode_extref); | |
1393 | log_ref_ver = 1; | |
1394 | r = (struct btrfs_inode_extref *)ref_ptr; | |
1395 | parent_objectid = btrfs_inode_extref_parent(eb, r); | |
1396 | } else { | |
1397 | ref_struct_size = sizeof(struct btrfs_inode_ref); | |
1398 | parent_objectid = key->offset; | |
1399 | } | |
1400 | inode_objectid = key->objectid; | |
e02119d5 | 1401 | |
5a1d7843 JS |
1402 | /* |
1403 | * it is possible that we didn't log all the parent directories | |
1404 | * for a given inode. If we don't find the dir, just don't | |
1405 | * copy the back ref in. The link count fixup code will take | |
1406 | * care of the rest | |
1407 | */ | |
f186373f | 1408 | dir = read_one_inode(root, parent_objectid); |
03b2f08b GB |
1409 | if (!dir) { |
1410 | ret = -ENOENT; | |
1411 | goto out; | |
1412 | } | |
5a1d7843 | 1413 | |
f186373f | 1414 | inode = read_one_inode(root, inode_objectid); |
5a1d7843 | 1415 | if (!inode) { |
03b2f08b GB |
1416 | ret = -EIO; |
1417 | goto out; | |
5a1d7843 JS |
1418 | } |
1419 | ||
5a1d7843 | 1420 | while (ref_ptr < ref_end) { |
f186373f | 1421 | if (log_ref_ver) { |
e43eec81 | 1422 | ret = extref_get_fields(eb, ref_ptr, &name, |
bae15d95 | 1423 | &ref_index, &parent_objectid); |
f186373f MF |
1424 | /* |
1425 | * parent object can change from one array | |
1426 | * item to another. | |
1427 | */ | |
1428 | if (!dir) | |
1429 | dir = read_one_inode(root, parent_objectid); | |
03b2f08b GB |
1430 | if (!dir) { |
1431 | ret = -ENOENT; | |
1432 | goto out; | |
1433 | } | |
f186373f | 1434 | } else { |
e43eec81 | 1435 | ret = ref_get_fields(eb, ref_ptr, &name, &ref_index); |
f186373f MF |
1436 | } |
1437 | if (ret) | |
03b2f08b | 1438 | goto out; |
5a1d7843 | 1439 | |
77a5b9e3 | 1440 | ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), |
e43eec81 | 1441 | btrfs_ino(BTRFS_I(inode)), ref_index, &name); |
77a5b9e3 FM |
1442 | if (ret < 0) { |
1443 | goto out; | |
1444 | } else if (ret == 0) { | |
5a1d7843 JS |
1445 | /* |
1446 | * look for a conflicting back reference in the | |
1447 | * metadata. if we find one we have to unlink that name | |
1448 | * of the file before we add our new link. Later on, we | |
1449 | * overwrite any existing back reference, and we don't | |
1450 | * want to create dangling pointers in the directory. | |
1451 | */ | |
7059c658 FM |
1452 | ret = __add_inode_ref(trans, root, path, log, |
1453 | BTRFS_I(dir), BTRFS_I(inode), | |
1454 | inode_objectid, parent_objectid, | |
e43eec81 | 1455 | ref_index, &name); |
7059c658 FM |
1456 | if (ret) { |
1457 | if (ret == 1) | |
1458 | ret = 0; | |
0d836392 | 1459 | goto out; |
7059c658 | 1460 | } |
0d836392 | 1461 | |
5a1d7843 | 1462 | /* insert our name */ |
7059c658 | 1463 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
e43eec81 | 1464 | &name, 0, ref_index); |
3650860b JB |
1465 | if (ret) |
1466 | goto out; | |
5a1d7843 | 1467 | |
f96d4474 JB |
1468 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
1469 | if (ret) | |
1470 | goto out; | |
5a1d7843 | 1471 | } |
77a5b9e3 | 1472 | /* Else, ret == 1, we already have a perfect match, we're done. */ |
5a1d7843 | 1473 | |
e43eec81 STD |
1474 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len; |
1475 | kfree(name.name); | |
1476 | name.name = NULL; | |
f186373f MF |
1477 | if (log_ref_ver) { |
1478 | iput(dir); | |
1479 | dir = NULL; | |
1480 | } | |
5a1d7843 | 1481 | } |
e02119d5 | 1482 | |
1f250e92 FM |
1483 | /* |
1484 | * Before we overwrite the inode reference item in the subvolume tree | |
1485 | * with the item from the log tree, we must unlink all names from the | |
1486 | * parent directory that are in the subvolume's tree inode reference | |
1487 | * item, otherwise we end up with an inconsistent subvolume tree where | |
1488 | * dir index entries exist for a name but there is no inode reference | |
1489 | * item with the same name. | |
1490 | */ | |
1491 | ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, | |
1492 | key); | |
1493 | if (ret) | |
1494 | goto out; | |
1495 | ||
e02119d5 CM |
1496 | /* finally write the back reference in the inode */ |
1497 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
5a1d7843 | 1498 | out: |
b3b4aa74 | 1499 | btrfs_release_path(path); |
e43eec81 | 1500 | kfree(name.name); |
e02119d5 CM |
1501 | iput(dir); |
1502 | iput(inode); | |
3650860b | 1503 | return ret; |
e02119d5 CM |
1504 | } |
1505 | ||
f186373f | 1506 | static int count_inode_extrefs(struct btrfs_root *root, |
36283658 | 1507 | struct btrfs_inode *inode, struct btrfs_path *path) |
f186373f MF |
1508 | { |
1509 | int ret = 0; | |
1510 | int name_len; | |
1511 | unsigned int nlink = 0; | |
1512 | u32 item_size; | |
1513 | u32 cur_offset = 0; | |
36283658 | 1514 | u64 inode_objectid = btrfs_ino(inode); |
f186373f MF |
1515 | u64 offset = 0; |
1516 | unsigned long ptr; | |
1517 | struct btrfs_inode_extref *extref; | |
1518 | struct extent_buffer *leaf; | |
1519 | ||
1520 | while (1) { | |
1521 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, | |
1522 | &extref, &offset); | |
1523 | if (ret) | |
1524 | break; | |
c71bf099 | 1525 | |
f186373f | 1526 | leaf = path->nodes[0]; |
3212fa14 | 1527 | item_size = btrfs_item_size(leaf, path->slots[0]); |
f186373f | 1528 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
2c2c452b | 1529 | cur_offset = 0; |
f186373f MF |
1530 | |
1531 | while (cur_offset < item_size) { | |
1532 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); | |
1533 | name_len = btrfs_inode_extref_name_len(leaf, extref); | |
1534 | ||
1535 | nlink++; | |
1536 | ||
1537 | cur_offset += name_len + sizeof(*extref); | |
1538 | } | |
1539 | ||
1540 | offset++; | |
1541 | btrfs_release_path(path); | |
1542 | } | |
1543 | btrfs_release_path(path); | |
1544 | ||
2c2c452b | 1545 | if (ret < 0 && ret != -ENOENT) |
f186373f MF |
1546 | return ret; |
1547 | return nlink; | |
1548 | } | |
1549 | ||
1550 | static int count_inode_refs(struct btrfs_root *root, | |
f329e319 | 1551 | struct btrfs_inode *inode, struct btrfs_path *path) |
e02119d5 | 1552 | { |
e02119d5 CM |
1553 | int ret; |
1554 | struct btrfs_key key; | |
f186373f | 1555 | unsigned int nlink = 0; |
e02119d5 CM |
1556 | unsigned long ptr; |
1557 | unsigned long ptr_end; | |
1558 | int name_len; | |
f329e319 | 1559 | u64 ino = btrfs_ino(inode); |
e02119d5 | 1560 | |
33345d01 | 1561 | key.objectid = ino; |
e02119d5 CM |
1562 | key.type = BTRFS_INODE_REF_KEY; |
1563 | key.offset = (u64)-1; | |
1564 | ||
d397712b | 1565 | while (1) { |
e02119d5 CM |
1566 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
1567 | if (ret < 0) | |
1568 | break; | |
1569 | if (ret > 0) { | |
1570 | if (path->slots[0] == 0) | |
1571 | break; | |
1572 | path->slots[0]--; | |
1573 | } | |
e93ae26f | 1574 | process_slot: |
e02119d5 CM |
1575 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
1576 | path->slots[0]); | |
33345d01 | 1577 | if (key.objectid != ino || |
e02119d5 CM |
1578 | key.type != BTRFS_INODE_REF_KEY) |
1579 | break; | |
1580 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
3212fa14 | 1581 | ptr_end = ptr + btrfs_item_size(path->nodes[0], |
e02119d5 | 1582 | path->slots[0]); |
d397712b | 1583 | while (ptr < ptr_end) { |
e02119d5 CM |
1584 | struct btrfs_inode_ref *ref; |
1585 | ||
1586 | ref = (struct btrfs_inode_ref *)ptr; | |
1587 | name_len = btrfs_inode_ref_name_len(path->nodes[0], | |
1588 | ref); | |
1589 | ptr = (unsigned long)(ref + 1) + name_len; | |
1590 | nlink++; | |
1591 | } | |
1592 | ||
1593 | if (key.offset == 0) | |
1594 | break; | |
e93ae26f FDBM |
1595 | if (path->slots[0] > 0) { |
1596 | path->slots[0]--; | |
1597 | goto process_slot; | |
1598 | } | |
e02119d5 | 1599 | key.offset--; |
b3b4aa74 | 1600 | btrfs_release_path(path); |
e02119d5 | 1601 | } |
b3b4aa74 | 1602 | btrfs_release_path(path); |
f186373f MF |
1603 | |
1604 | return nlink; | |
1605 | } | |
1606 | ||
1607 | /* | |
1608 | * There are a few corners where the link count of the file can't | |
1609 | * be properly maintained during replay. So, instead of adding | |
1610 | * lots of complexity to the log code, we just scan the backrefs | |
1611 | * for any file that has been through replay. | |
1612 | * | |
1613 | * The scan will update the link count on the inode to reflect the | |
1614 | * number of back refs found. If it goes down to zero, the iput | |
1615 | * will free the inode. | |
1616 | */ | |
1617 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, | |
1618 | struct btrfs_root *root, | |
1619 | struct inode *inode) | |
1620 | { | |
1621 | struct btrfs_path *path; | |
1622 | int ret; | |
1623 | u64 nlink = 0; | |
4a0cc7ca | 1624 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
f186373f MF |
1625 | |
1626 | path = btrfs_alloc_path(); | |
1627 | if (!path) | |
1628 | return -ENOMEM; | |
1629 | ||
f329e319 | 1630 | ret = count_inode_refs(root, BTRFS_I(inode), path); |
f186373f MF |
1631 | if (ret < 0) |
1632 | goto out; | |
1633 | ||
1634 | nlink = ret; | |
1635 | ||
36283658 | 1636 | ret = count_inode_extrefs(root, BTRFS_I(inode), path); |
f186373f MF |
1637 | if (ret < 0) |
1638 | goto out; | |
1639 | ||
1640 | nlink += ret; | |
1641 | ||
1642 | ret = 0; | |
1643 | ||
e02119d5 | 1644 | if (nlink != inode->i_nlink) { |
bfe86848 | 1645 | set_nlink(inode, nlink); |
f96d4474 JB |
1646 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
1647 | if (ret) | |
1648 | goto out; | |
e02119d5 | 1649 | } |
8d5bf1cb | 1650 | BTRFS_I(inode)->index_cnt = (u64)-1; |
e02119d5 | 1651 | |
c71bf099 YZ |
1652 | if (inode->i_nlink == 0) { |
1653 | if (S_ISDIR(inode->i_mode)) { | |
1654 | ret = replay_dir_deletes(trans, root, NULL, path, | |
33345d01 | 1655 | ino, 1); |
3650860b JB |
1656 | if (ret) |
1657 | goto out; | |
c71bf099 | 1658 | } |
ecdcf3c2 NB |
1659 | ret = btrfs_insert_orphan_item(trans, root, ino); |
1660 | if (ret == -EEXIST) | |
1661 | ret = 0; | |
12fcfd22 | 1662 | } |
12fcfd22 | 1663 | |
f186373f MF |
1664 | out: |
1665 | btrfs_free_path(path); | |
1666 | return ret; | |
e02119d5 CM |
1667 | } |
1668 | ||
1669 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, | |
1670 | struct btrfs_root *root, | |
1671 | struct btrfs_path *path) | |
1672 | { | |
1673 | int ret; | |
1674 | struct btrfs_key key; | |
1675 | struct inode *inode; | |
1676 | ||
1677 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
1678 | key.type = BTRFS_ORPHAN_ITEM_KEY; | |
1679 | key.offset = (u64)-1; | |
d397712b | 1680 | while (1) { |
e02119d5 CM |
1681 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1682 | if (ret < 0) | |
1683 | break; | |
1684 | ||
1685 | if (ret == 1) { | |
011b28ac | 1686 | ret = 0; |
e02119d5 CM |
1687 | if (path->slots[0] == 0) |
1688 | break; | |
1689 | path->slots[0]--; | |
1690 | } | |
1691 | ||
1692 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1693 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || | |
1694 | key.type != BTRFS_ORPHAN_ITEM_KEY) | |
1695 | break; | |
1696 | ||
1697 | ret = btrfs_del_item(trans, root, path); | |
65a246c5 | 1698 | if (ret) |
011b28ac | 1699 | break; |
e02119d5 | 1700 | |
b3b4aa74 | 1701 | btrfs_release_path(path); |
e02119d5 | 1702 | inode = read_one_inode(root, key.offset); |
011b28ac JB |
1703 | if (!inode) { |
1704 | ret = -EIO; | |
1705 | break; | |
1706 | } | |
e02119d5 CM |
1707 | |
1708 | ret = fixup_inode_link_count(trans, root, inode); | |
e02119d5 | 1709 | iput(inode); |
3650860b | 1710 | if (ret) |
011b28ac | 1711 | break; |
e02119d5 | 1712 | |
12fcfd22 CM |
1713 | /* |
1714 | * fixup on a directory may create new entries, | |
1715 | * make sure we always look for the highset possible | |
1716 | * offset | |
1717 | */ | |
1718 | key.offset = (u64)-1; | |
e02119d5 | 1719 | } |
b3b4aa74 | 1720 | btrfs_release_path(path); |
65a246c5 | 1721 | return ret; |
e02119d5 CM |
1722 | } |
1723 | ||
1724 | ||
1725 | /* | |
1726 | * record a given inode in the fixup dir so we can check its link | |
1727 | * count when replay is done. The link count is incremented here | |
1728 | * so the inode won't go away until we check it | |
1729 | */ | |
1730 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, | |
1731 | struct btrfs_root *root, | |
1732 | struct btrfs_path *path, | |
1733 | u64 objectid) | |
1734 | { | |
1735 | struct btrfs_key key; | |
1736 | int ret = 0; | |
1737 | struct inode *inode; | |
1738 | ||
1739 | inode = read_one_inode(root, objectid); | |
c00e9493 TI |
1740 | if (!inode) |
1741 | return -EIO; | |
e02119d5 CM |
1742 | |
1743 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
962a298f | 1744 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
e02119d5 CM |
1745 | key.offset = objectid; |
1746 | ||
1747 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | |
1748 | ||
b3b4aa74 | 1749 | btrfs_release_path(path); |
e02119d5 | 1750 | if (ret == 0) { |
9bf7a489 JB |
1751 | if (!inode->i_nlink) |
1752 | set_nlink(inode, 1); | |
1753 | else | |
8b558c5f | 1754 | inc_nlink(inode); |
9a56fcd1 | 1755 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
e02119d5 CM |
1756 | } else if (ret == -EEXIST) { |
1757 | ret = 0; | |
e02119d5 CM |
1758 | } |
1759 | iput(inode); | |
1760 | ||
1761 | return ret; | |
1762 | } | |
1763 | ||
1764 | /* | |
1765 | * when replaying the log for a directory, we only insert names | |
1766 | * for inodes that actually exist. This means an fsync on a directory | |
1767 | * does not implicitly fsync all the new files in it | |
1768 | */ | |
1769 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, | |
1770 | struct btrfs_root *root, | |
e02119d5 | 1771 | u64 dirid, u64 index, |
6db75318 | 1772 | const struct fscrypt_str *name, |
e02119d5 CM |
1773 | struct btrfs_key *location) |
1774 | { | |
1775 | struct inode *inode; | |
1776 | struct inode *dir; | |
1777 | int ret; | |
1778 | ||
1779 | inode = read_one_inode(root, location->objectid); | |
1780 | if (!inode) | |
1781 | return -ENOENT; | |
1782 | ||
1783 | dir = read_one_inode(root, dirid); | |
1784 | if (!dir) { | |
1785 | iput(inode); | |
1786 | return -EIO; | |
1787 | } | |
d555438b | 1788 | |
db0a669f | 1789 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, |
e43eec81 | 1790 | 1, index); |
e02119d5 CM |
1791 | |
1792 | /* FIXME, put inode into FIXUP list */ | |
1793 | ||
1794 | iput(inode); | |
1795 | iput(dir); | |
1796 | return ret; | |
1797 | } | |
1798 | ||
339d0354 FM |
1799 | static int delete_conflicting_dir_entry(struct btrfs_trans_handle *trans, |
1800 | struct btrfs_inode *dir, | |
1801 | struct btrfs_path *path, | |
1802 | struct btrfs_dir_item *dst_di, | |
1803 | const struct btrfs_key *log_key, | |
94a48aef | 1804 | u8 log_flags, |
339d0354 FM |
1805 | bool exists) |
1806 | { | |
1807 | struct btrfs_key found_key; | |
1808 | ||
1809 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); | |
1810 | /* The existing dentry points to the same inode, don't delete it. */ | |
1811 | if (found_key.objectid == log_key->objectid && | |
1812 | found_key.type == log_key->type && | |
1813 | found_key.offset == log_key->offset && | |
94a48aef | 1814 | btrfs_dir_flags(path->nodes[0], dst_di) == log_flags) |
339d0354 FM |
1815 | return 1; |
1816 | ||
1817 | /* | |
1818 | * Don't drop the conflicting directory entry if the inode for the new | |
1819 | * entry doesn't exist. | |
1820 | */ | |
1821 | if (!exists) | |
1822 | return 0; | |
1823 | ||
1824 | return drop_one_dir_item(trans, path, dir, dst_di); | |
1825 | } | |
1826 | ||
e02119d5 CM |
1827 | /* |
1828 | * take a single entry in a log directory item and replay it into | |
1829 | * the subvolume. | |
1830 | * | |
1831 | * if a conflicting item exists in the subdirectory already, | |
1832 | * the inode it points to is unlinked and put into the link count | |
1833 | * fix up tree. | |
1834 | * | |
1835 | * If a name from the log points to a file or directory that does | |
1836 | * not exist in the FS, it is skipped. fsyncs on directories | |
1837 | * do not force down inodes inside that directory, just changes to the | |
1838 | * names or unlinks in a directory. | |
bb53eda9 FM |
1839 | * |
1840 | * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a | |
1841 | * non-existing inode) and 1 if the name was replayed. | |
e02119d5 CM |
1842 | */ |
1843 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, | |
1844 | struct btrfs_root *root, | |
1845 | struct btrfs_path *path, | |
1846 | struct extent_buffer *eb, | |
1847 | struct btrfs_dir_item *di, | |
1848 | struct btrfs_key *key) | |
1849 | { | |
6db75318 | 1850 | struct fscrypt_str name; |
339d0354 FM |
1851 | struct btrfs_dir_item *dir_dst_di; |
1852 | struct btrfs_dir_item *index_dst_di; | |
1853 | bool dir_dst_matches = false; | |
1854 | bool index_dst_matches = false; | |
e02119d5 | 1855 | struct btrfs_key log_key; |
339d0354 | 1856 | struct btrfs_key search_key; |
e02119d5 | 1857 | struct inode *dir; |
94a48aef | 1858 | u8 log_flags; |
cfd31269 FM |
1859 | bool exists; |
1860 | int ret; | |
339d0354 | 1861 | bool update_size = true; |
bb53eda9 | 1862 | bool name_added = false; |
e02119d5 CM |
1863 | |
1864 | dir = read_one_inode(root, key->objectid); | |
c00e9493 TI |
1865 | if (!dir) |
1866 | return -EIO; | |
e02119d5 | 1867 | |
e43eec81 STD |
1868 | ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name); |
1869 | if (ret) | |
2bac325e | 1870 | goto out; |
2a29edc6 | 1871 | |
94a48aef | 1872 | log_flags = btrfs_dir_flags(eb, di); |
e02119d5 | 1873 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); |
cfd31269 | 1874 | ret = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
b3b4aa74 | 1875 | btrfs_release_path(path); |
cfd31269 FM |
1876 | if (ret < 0) |
1877 | goto out; | |
1878 | exists = (ret == 0); | |
1879 | ret = 0; | |
4bef0848 | 1880 | |
339d0354 | 1881 | dir_dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, |
e43eec81 | 1882 | &name, 1); |
339d0354 FM |
1883 | if (IS_ERR(dir_dst_di)) { |
1884 | ret = PTR_ERR(dir_dst_di); | |
3650860b | 1885 | goto out; |
339d0354 FM |
1886 | } else if (dir_dst_di) { |
1887 | ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path, | |
94a48aef OS |
1888 | dir_dst_di, &log_key, |
1889 | log_flags, exists); | |
339d0354 FM |
1890 | if (ret < 0) |
1891 | goto out; | |
1892 | dir_dst_matches = (ret == 1); | |
e02119d5 | 1893 | } |
e15ac641 | 1894 | |
339d0354 FM |
1895 | btrfs_release_path(path); |
1896 | ||
1897 | index_dst_di = btrfs_lookup_dir_index_item(trans, root, path, | |
1898 | key->objectid, key->offset, | |
e43eec81 | 1899 | &name, 1); |
339d0354 FM |
1900 | if (IS_ERR(index_dst_di)) { |
1901 | ret = PTR_ERR(index_dst_di); | |
e15ac641 | 1902 | goto out; |
339d0354 FM |
1903 | } else if (index_dst_di) { |
1904 | ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path, | |
1905 | index_dst_di, &log_key, | |
94a48aef | 1906 | log_flags, exists); |
339d0354 | 1907 | if (ret < 0) |
e02119d5 | 1908 | goto out; |
339d0354 | 1909 | index_dst_matches = (ret == 1); |
e02119d5 CM |
1910 | } |
1911 | ||
339d0354 FM |
1912 | btrfs_release_path(path); |
1913 | ||
1914 | if (dir_dst_matches && index_dst_matches) { | |
1915 | ret = 0; | |
a2cc11db | 1916 | update_size = false; |
e02119d5 CM |
1917 | goto out; |
1918 | } | |
1919 | ||
725af92a NB |
1920 | /* |
1921 | * Check if the inode reference exists in the log for the given name, | |
1922 | * inode and parent inode | |
1923 | */ | |
339d0354 FM |
1924 | search_key.objectid = log_key.objectid; |
1925 | search_key.type = BTRFS_INODE_REF_KEY; | |
1926 | search_key.offset = key->objectid; | |
e43eec81 | 1927 | ret = backref_in_log(root->log_root, &search_key, 0, &name); |
725af92a NB |
1928 | if (ret < 0) { |
1929 | goto out; | |
1930 | } else if (ret) { | |
1931 | /* The dentry will be added later. */ | |
1932 | ret = 0; | |
1933 | update_size = false; | |
1934 | goto out; | |
1935 | } | |
1936 | ||
339d0354 FM |
1937 | search_key.objectid = log_key.objectid; |
1938 | search_key.type = BTRFS_INODE_EXTREF_KEY; | |
1939 | search_key.offset = key->objectid; | |
e43eec81 | 1940 | ret = backref_in_log(root->log_root, &search_key, key->objectid, &name); |
725af92a NB |
1941 | if (ret < 0) { |
1942 | goto out; | |
1943 | } else if (ret) { | |
df8d116f FM |
1944 | /* The dentry will be added later. */ |
1945 | ret = 0; | |
1946 | update_size = false; | |
1947 | goto out; | |
1948 | } | |
b3b4aa74 | 1949 | btrfs_release_path(path); |
60d53eb3 | 1950 | ret = insert_one_name(trans, root, key->objectid, key->offset, |
e43eec81 | 1951 | &name, &log_key); |
df8d116f | 1952 | if (ret && ret != -ENOENT && ret != -EEXIST) |
3650860b | 1953 | goto out; |
bb53eda9 FM |
1954 | if (!ret) |
1955 | name_added = true; | |
d555438b | 1956 | update_size = false; |
3650860b | 1957 | ret = 0; |
339d0354 FM |
1958 | |
1959 | out: | |
1960 | if (!ret && update_size) { | |
e43eec81 | 1961 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name.len * 2); |
339d0354 FM |
1962 | ret = btrfs_update_inode(trans, root, BTRFS_I(dir)); |
1963 | } | |
e43eec81 | 1964 | kfree(name.name); |
339d0354 FM |
1965 | iput(dir); |
1966 | if (!ret && name_added) | |
1967 | ret = 1; | |
1968 | return ret; | |
e02119d5 CM |
1969 | } |
1970 | ||
339d0354 | 1971 | /* Replay one dir item from a BTRFS_DIR_INDEX_KEY key. */ |
e02119d5 CM |
1972 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, |
1973 | struct btrfs_root *root, | |
1974 | struct btrfs_path *path, | |
1975 | struct extent_buffer *eb, int slot, | |
1976 | struct btrfs_key *key) | |
1977 | { | |
339d0354 | 1978 | int ret; |
e02119d5 | 1979 | struct btrfs_dir_item *di; |
e02119d5 | 1980 | |
339d0354 FM |
1981 | /* We only log dir index keys, which only contain a single dir item. */ |
1982 | ASSERT(key->type == BTRFS_DIR_INDEX_KEY); | |
bb53eda9 | 1983 | |
339d0354 FM |
1984 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
1985 | ret = replay_one_name(trans, root, path, eb, di, key); | |
1986 | if (ret < 0) | |
1987 | return ret; | |
bb53eda9 | 1988 | |
339d0354 FM |
1989 | /* |
1990 | * If this entry refers to a non-directory (directories can not have a | |
1991 | * link count > 1) and it was added in the transaction that was not | |
1992 | * committed, make sure we fixup the link count of the inode the entry | |
1993 | * points to. Otherwise something like the following would result in a | |
1994 | * directory pointing to an inode with a wrong link that does not account | |
1995 | * for this dir entry: | |
1996 | * | |
1997 | * mkdir testdir | |
1998 | * touch testdir/foo | |
1999 | * touch testdir/bar | |
2000 | * sync | |
2001 | * | |
2002 | * ln testdir/bar testdir/bar_link | |
2003 | * ln testdir/foo testdir/foo_link | |
2004 | * xfs_io -c "fsync" testdir/bar | |
2005 | * | |
2006 | * <power failure> | |
2007 | * | |
2008 | * mount fs, log replay happens | |
2009 | * | |
2010 | * File foo would remain with a link count of 1 when it has two entries | |
2011 | * pointing to it in the directory testdir. This would make it impossible | |
2012 | * to ever delete the parent directory has it would result in stale | |
2013 | * dentries that can never be deleted. | |
2014 | */ | |
94a48aef | 2015 | if (ret == 1 && btrfs_dir_ftype(eb, di) != BTRFS_FT_DIR) { |
339d0354 FM |
2016 | struct btrfs_path *fixup_path; |
2017 | struct btrfs_key di_key; | |
bb53eda9 | 2018 | |
339d0354 FM |
2019 | fixup_path = btrfs_alloc_path(); |
2020 | if (!fixup_path) | |
2021 | return -ENOMEM; | |
2022 | ||
2023 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); | |
2024 | ret = link_to_fixup_dir(trans, root, fixup_path, di_key.objectid); | |
2025 | btrfs_free_path(fixup_path); | |
e02119d5 | 2026 | } |
339d0354 | 2027 | |
bb53eda9 | 2028 | return ret; |
e02119d5 CM |
2029 | } |
2030 | ||
2031 | /* | |
2032 | * directory replay has two parts. There are the standard directory | |
2033 | * items in the log copied from the subvolume, and range items | |
2034 | * created in the log while the subvolume was logged. | |
2035 | * | |
2036 | * The range items tell us which parts of the key space the log | |
2037 | * is authoritative for. During replay, if a key in the subvolume | |
2038 | * directory is in a logged range item, but not actually in the log | |
2039 | * that means it was deleted from the directory before the fsync | |
2040 | * and should be removed. | |
2041 | */ | |
2042 | static noinline int find_dir_range(struct btrfs_root *root, | |
2043 | struct btrfs_path *path, | |
ccae4a19 | 2044 | u64 dirid, |
e02119d5 CM |
2045 | u64 *start_ret, u64 *end_ret) |
2046 | { | |
2047 | struct btrfs_key key; | |
2048 | u64 found_end; | |
2049 | struct btrfs_dir_log_item *item; | |
2050 | int ret; | |
2051 | int nritems; | |
2052 | ||
2053 | if (*start_ret == (u64)-1) | |
2054 | return 1; | |
2055 | ||
2056 | key.objectid = dirid; | |
ccae4a19 | 2057 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
e02119d5 CM |
2058 | key.offset = *start_ret; |
2059 | ||
2060 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2061 | if (ret < 0) | |
2062 | goto out; | |
2063 | if (ret > 0) { | |
2064 | if (path->slots[0] == 0) | |
2065 | goto out; | |
2066 | path->slots[0]--; | |
2067 | } | |
2068 | if (ret != 0) | |
2069 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
2070 | ||
ccae4a19 | 2071 | if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) { |
e02119d5 CM |
2072 | ret = 1; |
2073 | goto next; | |
2074 | } | |
2075 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2076 | struct btrfs_dir_log_item); | |
2077 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
2078 | ||
2079 | if (*start_ret >= key.offset && *start_ret <= found_end) { | |
2080 | ret = 0; | |
2081 | *start_ret = key.offset; | |
2082 | *end_ret = found_end; | |
2083 | goto out; | |
2084 | } | |
2085 | ret = 1; | |
2086 | next: | |
2087 | /* check the next slot in the tree to see if it is a valid item */ | |
2088 | nritems = btrfs_header_nritems(path->nodes[0]); | |
2a7bf53f | 2089 | path->slots[0]++; |
e02119d5 CM |
2090 | if (path->slots[0] >= nritems) { |
2091 | ret = btrfs_next_leaf(root, path); | |
2092 | if (ret) | |
2093 | goto out; | |
e02119d5 CM |
2094 | } |
2095 | ||
2096 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
2097 | ||
ccae4a19 | 2098 | if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) { |
e02119d5 CM |
2099 | ret = 1; |
2100 | goto out; | |
2101 | } | |
2102 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2103 | struct btrfs_dir_log_item); | |
2104 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
2105 | *start_ret = key.offset; | |
2106 | *end_ret = found_end; | |
2107 | ret = 0; | |
2108 | out: | |
b3b4aa74 | 2109 | btrfs_release_path(path); |
e02119d5 CM |
2110 | return ret; |
2111 | } | |
2112 | ||
2113 | /* | |
2114 | * this looks for a given directory item in the log. If the directory | |
2115 | * item is not in the log, the item is removed and the inode it points | |
2116 | * to is unlinked | |
2117 | */ | |
2118 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, | |
e02119d5 CM |
2119 | struct btrfs_root *log, |
2120 | struct btrfs_path *path, | |
2121 | struct btrfs_path *log_path, | |
2122 | struct inode *dir, | |
2123 | struct btrfs_key *dir_key) | |
2124 | { | |
d1ed82f3 | 2125 | struct btrfs_root *root = BTRFS_I(dir)->root; |
e02119d5 CM |
2126 | int ret; |
2127 | struct extent_buffer *eb; | |
2128 | int slot; | |
e02119d5 | 2129 | struct btrfs_dir_item *di; |
6db75318 | 2130 | struct fscrypt_str name; |
ccae4a19 | 2131 | struct inode *inode = NULL; |
e02119d5 CM |
2132 | struct btrfs_key location; |
2133 | ||
ccae4a19 | 2134 | /* |
143823cf | 2135 | * Currently we only log dir index keys. Even if we replay a log created |
ccae4a19 FM |
2136 | * by an older kernel that logged both dir index and dir item keys, all |
2137 | * we need to do is process the dir index keys, we (and our caller) can | |
2138 | * safely ignore dir item keys (key type BTRFS_DIR_ITEM_KEY). | |
2139 | */ | |
2140 | ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY); | |
2141 | ||
e02119d5 CM |
2142 | eb = path->nodes[0]; |
2143 | slot = path->slots[0]; | |
ccae4a19 | 2144 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
e43eec81 STD |
2145 | ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name); |
2146 | if (ret) | |
ccae4a19 | 2147 | goto out; |
3650860b | 2148 | |
ccae4a19 FM |
2149 | if (log) { |
2150 | struct btrfs_dir_item *log_di; | |
e02119d5 | 2151 | |
ccae4a19 FM |
2152 | log_di = btrfs_lookup_dir_index_item(trans, log, log_path, |
2153 | dir_key->objectid, | |
e43eec81 | 2154 | dir_key->offset, &name, 0); |
ccae4a19 FM |
2155 | if (IS_ERR(log_di)) { |
2156 | ret = PTR_ERR(log_di); | |
2157 | goto out; | |
2158 | } else if (log_di) { | |
2159 | /* The dentry exists in the log, we have nothing to do. */ | |
e02119d5 CM |
2160 | ret = 0; |
2161 | goto out; | |
2162 | } | |
ccae4a19 | 2163 | } |
e02119d5 | 2164 | |
ccae4a19 FM |
2165 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
2166 | btrfs_release_path(path); | |
2167 | btrfs_release_path(log_path); | |
2168 | inode = read_one_inode(root, location.objectid); | |
2169 | if (!inode) { | |
2170 | ret = -EIO; | |
2171 | goto out; | |
e02119d5 | 2172 | } |
ccae4a19 FM |
2173 | |
2174 | ret = link_to_fixup_dir(trans, root, path, location.objectid); | |
2175 | if (ret) | |
2176 | goto out; | |
2177 | ||
2178 | inc_nlink(inode); | |
313ab753 | 2179 | ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir), BTRFS_I(inode), |
e43eec81 | 2180 | &name); |
ccae4a19 FM |
2181 | /* |
2182 | * Unlike dir item keys, dir index keys can only have one name (entry) in | |
2183 | * them, as there are no key collisions since each key has a unique offset | |
2184 | * (an index number), so we're done. | |
2185 | */ | |
e02119d5 | 2186 | out: |
b3b4aa74 DS |
2187 | btrfs_release_path(path); |
2188 | btrfs_release_path(log_path); | |
e43eec81 | 2189 | kfree(name.name); |
ccae4a19 | 2190 | iput(inode); |
e02119d5 CM |
2191 | return ret; |
2192 | } | |
2193 | ||
4f764e51 FM |
2194 | static int replay_xattr_deletes(struct btrfs_trans_handle *trans, |
2195 | struct btrfs_root *root, | |
2196 | struct btrfs_root *log, | |
2197 | struct btrfs_path *path, | |
2198 | const u64 ino) | |
2199 | { | |
2200 | struct btrfs_key search_key; | |
2201 | struct btrfs_path *log_path; | |
2202 | int i; | |
2203 | int nritems; | |
2204 | int ret; | |
2205 | ||
2206 | log_path = btrfs_alloc_path(); | |
2207 | if (!log_path) | |
2208 | return -ENOMEM; | |
2209 | ||
2210 | search_key.objectid = ino; | |
2211 | search_key.type = BTRFS_XATTR_ITEM_KEY; | |
2212 | search_key.offset = 0; | |
2213 | again: | |
2214 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
2215 | if (ret < 0) | |
2216 | goto out; | |
2217 | process_leaf: | |
2218 | nritems = btrfs_header_nritems(path->nodes[0]); | |
2219 | for (i = path->slots[0]; i < nritems; i++) { | |
2220 | struct btrfs_key key; | |
2221 | struct btrfs_dir_item *di; | |
2222 | struct btrfs_dir_item *log_di; | |
2223 | u32 total_size; | |
2224 | u32 cur; | |
2225 | ||
2226 | btrfs_item_key_to_cpu(path->nodes[0], &key, i); | |
2227 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { | |
2228 | ret = 0; | |
2229 | goto out; | |
2230 | } | |
2231 | ||
2232 | di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); | |
3212fa14 | 2233 | total_size = btrfs_item_size(path->nodes[0], i); |
4f764e51 FM |
2234 | cur = 0; |
2235 | while (cur < total_size) { | |
2236 | u16 name_len = btrfs_dir_name_len(path->nodes[0], di); | |
2237 | u16 data_len = btrfs_dir_data_len(path->nodes[0], di); | |
2238 | u32 this_len = sizeof(*di) + name_len + data_len; | |
2239 | char *name; | |
2240 | ||
2241 | name = kmalloc(name_len, GFP_NOFS); | |
2242 | if (!name) { | |
2243 | ret = -ENOMEM; | |
2244 | goto out; | |
2245 | } | |
2246 | read_extent_buffer(path->nodes[0], name, | |
2247 | (unsigned long)(di + 1), name_len); | |
2248 | ||
2249 | log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, | |
2250 | name, name_len, 0); | |
2251 | btrfs_release_path(log_path); | |
2252 | if (!log_di) { | |
2253 | /* Doesn't exist in log tree, so delete it. */ | |
2254 | btrfs_release_path(path); | |
2255 | di = btrfs_lookup_xattr(trans, root, path, ino, | |
2256 | name, name_len, -1); | |
2257 | kfree(name); | |
2258 | if (IS_ERR(di)) { | |
2259 | ret = PTR_ERR(di); | |
2260 | goto out; | |
2261 | } | |
2262 | ASSERT(di); | |
2263 | ret = btrfs_delete_one_dir_name(trans, root, | |
2264 | path, di); | |
2265 | if (ret) | |
2266 | goto out; | |
2267 | btrfs_release_path(path); | |
2268 | search_key = key; | |
2269 | goto again; | |
2270 | } | |
2271 | kfree(name); | |
2272 | if (IS_ERR(log_di)) { | |
2273 | ret = PTR_ERR(log_di); | |
2274 | goto out; | |
2275 | } | |
2276 | cur += this_len; | |
2277 | di = (struct btrfs_dir_item *)((char *)di + this_len); | |
2278 | } | |
2279 | } | |
2280 | ret = btrfs_next_leaf(root, path); | |
2281 | if (ret > 0) | |
2282 | ret = 0; | |
2283 | else if (ret == 0) | |
2284 | goto process_leaf; | |
2285 | out: | |
2286 | btrfs_free_path(log_path); | |
2287 | btrfs_release_path(path); | |
2288 | return ret; | |
2289 | } | |
2290 | ||
2291 | ||
e02119d5 CM |
2292 | /* |
2293 | * deletion replay happens before we copy any new directory items | |
2294 | * out of the log or out of backreferences from inodes. It | |
2295 | * scans the log to find ranges of keys that log is authoritative for, | |
2296 | * and then scans the directory to find items in those ranges that are | |
2297 | * not present in the log. | |
2298 | * | |
2299 | * Anything we don't find in the log is unlinked and removed from the | |
2300 | * directory. | |
2301 | */ | |
2302 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, | |
2303 | struct btrfs_root *root, | |
2304 | struct btrfs_root *log, | |
2305 | struct btrfs_path *path, | |
12fcfd22 | 2306 | u64 dirid, int del_all) |
e02119d5 CM |
2307 | { |
2308 | u64 range_start; | |
2309 | u64 range_end; | |
e02119d5 CM |
2310 | int ret = 0; |
2311 | struct btrfs_key dir_key; | |
2312 | struct btrfs_key found_key; | |
2313 | struct btrfs_path *log_path; | |
2314 | struct inode *dir; | |
2315 | ||
2316 | dir_key.objectid = dirid; | |
ccae4a19 | 2317 | dir_key.type = BTRFS_DIR_INDEX_KEY; |
e02119d5 CM |
2318 | log_path = btrfs_alloc_path(); |
2319 | if (!log_path) | |
2320 | return -ENOMEM; | |
2321 | ||
2322 | dir = read_one_inode(root, dirid); | |
2323 | /* it isn't an error if the inode isn't there, that can happen | |
2324 | * because we replay the deletes before we copy in the inode item | |
2325 | * from the log | |
2326 | */ | |
2327 | if (!dir) { | |
2328 | btrfs_free_path(log_path); | |
2329 | return 0; | |
2330 | } | |
ccae4a19 | 2331 | |
e02119d5 CM |
2332 | range_start = 0; |
2333 | range_end = 0; | |
d397712b | 2334 | while (1) { |
12fcfd22 CM |
2335 | if (del_all) |
2336 | range_end = (u64)-1; | |
2337 | else { | |
ccae4a19 | 2338 | ret = find_dir_range(log, path, dirid, |
12fcfd22 | 2339 | &range_start, &range_end); |
10adb115 FM |
2340 | if (ret < 0) |
2341 | goto out; | |
2342 | else if (ret > 0) | |
12fcfd22 CM |
2343 | break; |
2344 | } | |
e02119d5 CM |
2345 | |
2346 | dir_key.offset = range_start; | |
d397712b | 2347 | while (1) { |
e02119d5 CM |
2348 | int nritems; |
2349 | ret = btrfs_search_slot(NULL, root, &dir_key, path, | |
2350 | 0, 0); | |
2351 | if (ret < 0) | |
2352 | goto out; | |
2353 | ||
2354 | nritems = btrfs_header_nritems(path->nodes[0]); | |
2355 | if (path->slots[0] >= nritems) { | |
2356 | ret = btrfs_next_leaf(root, path); | |
b98def7c | 2357 | if (ret == 1) |
e02119d5 | 2358 | break; |
b98def7c LB |
2359 | else if (ret < 0) |
2360 | goto out; | |
e02119d5 CM |
2361 | } |
2362 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
2363 | path->slots[0]); | |
2364 | if (found_key.objectid != dirid || | |
ccae4a19 FM |
2365 | found_key.type != dir_key.type) { |
2366 | ret = 0; | |
2367 | goto out; | |
2368 | } | |
e02119d5 CM |
2369 | |
2370 | if (found_key.offset > range_end) | |
2371 | break; | |
2372 | ||
d1ed82f3 | 2373 | ret = check_item_in_log(trans, log, path, |
12fcfd22 CM |
2374 | log_path, dir, |
2375 | &found_key); | |
3650860b JB |
2376 | if (ret) |
2377 | goto out; | |
e02119d5 CM |
2378 | if (found_key.offset == (u64)-1) |
2379 | break; | |
2380 | dir_key.offset = found_key.offset + 1; | |
2381 | } | |
b3b4aa74 | 2382 | btrfs_release_path(path); |
e02119d5 CM |
2383 | if (range_end == (u64)-1) |
2384 | break; | |
2385 | range_start = range_end + 1; | |
2386 | } | |
e02119d5 | 2387 | ret = 0; |
e02119d5 | 2388 | out: |
b3b4aa74 | 2389 | btrfs_release_path(path); |
e02119d5 CM |
2390 | btrfs_free_path(log_path); |
2391 | iput(dir); | |
2392 | return ret; | |
2393 | } | |
2394 | ||
2395 | /* | |
2396 | * the process_func used to replay items from the log tree. This | |
2397 | * gets called in two different stages. The first stage just looks | |
2398 | * for inodes and makes sure they are all copied into the subvolume. | |
2399 | * | |
2400 | * The second stage copies all the other item types from the log into | |
2401 | * the subvolume. The two stage approach is slower, but gets rid of | |
2402 | * lots of complexity around inodes referencing other inodes that exist | |
2403 | * only in the log (references come from either directory items or inode | |
2404 | * back refs). | |
2405 | */ | |
2406 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | |
581c1760 | 2407 | struct walk_control *wc, u64 gen, int level) |
e02119d5 CM |
2408 | { |
2409 | int nritems; | |
2410 | struct btrfs_path *path; | |
2411 | struct btrfs_root *root = wc->replay_dest; | |
2412 | struct btrfs_key key; | |
e02119d5 CM |
2413 | int i; |
2414 | int ret; | |
2415 | ||
6a2e9dc4 | 2416 | ret = btrfs_read_extent_buffer(eb, gen, level, NULL); |
018642a1 TI |
2417 | if (ret) |
2418 | return ret; | |
e02119d5 CM |
2419 | |
2420 | level = btrfs_header_level(eb); | |
2421 | ||
2422 | if (level != 0) | |
2423 | return 0; | |
2424 | ||
2425 | path = btrfs_alloc_path(); | |
1e5063d0 MF |
2426 | if (!path) |
2427 | return -ENOMEM; | |
e02119d5 CM |
2428 | |
2429 | nritems = btrfs_header_nritems(eb); | |
2430 | for (i = 0; i < nritems; i++) { | |
2431 | btrfs_item_key_to_cpu(eb, &key, i); | |
e02119d5 CM |
2432 | |
2433 | /* inode keys are done during the first stage */ | |
2434 | if (key.type == BTRFS_INODE_ITEM_KEY && | |
2435 | wc->stage == LOG_WALK_REPLAY_INODES) { | |
e02119d5 CM |
2436 | struct btrfs_inode_item *inode_item; |
2437 | u32 mode; | |
2438 | ||
2439 | inode_item = btrfs_item_ptr(eb, i, | |
2440 | struct btrfs_inode_item); | |
f2d72f42 FM |
2441 | /* |
2442 | * If we have a tmpfile (O_TMPFILE) that got fsync'ed | |
2443 | * and never got linked before the fsync, skip it, as | |
2444 | * replaying it is pointless since it would be deleted | |
2445 | * later. We skip logging tmpfiles, but it's always | |
2446 | * possible we are replaying a log created with a kernel | |
2447 | * that used to log tmpfiles. | |
2448 | */ | |
2449 | if (btrfs_inode_nlink(eb, inode_item) == 0) { | |
2450 | wc->ignore_cur_inode = true; | |
2451 | continue; | |
2452 | } else { | |
2453 | wc->ignore_cur_inode = false; | |
2454 | } | |
4f764e51 FM |
2455 | ret = replay_xattr_deletes(wc->trans, root, log, |
2456 | path, key.objectid); | |
2457 | if (ret) | |
2458 | break; | |
e02119d5 CM |
2459 | mode = btrfs_inode_mode(eb, inode_item); |
2460 | if (S_ISDIR(mode)) { | |
2461 | ret = replay_dir_deletes(wc->trans, | |
12fcfd22 | 2462 | root, log, path, key.objectid, 0); |
b50c6e25 JB |
2463 | if (ret) |
2464 | break; | |
e02119d5 CM |
2465 | } |
2466 | ret = overwrite_item(wc->trans, root, path, | |
2467 | eb, i, &key); | |
b50c6e25 JB |
2468 | if (ret) |
2469 | break; | |
e02119d5 | 2470 | |
471d557a FM |
2471 | /* |
2472 | * Before replaying extents, truncate the inode to its | |
2473 | * size. We need to do it now and not after log replay | |
2474 | * because before an fsync we can have prealloc extents | |
2475 | * added beyond the inode's i_size. If we did it after, | |
2476 | * through orphan cleanup for example, we would drop | |
2477 | * those prealloc extents just after replaying them. | |
e02119d5 CM |
2478 | */ |
2479 | if (S_ISREG(mode)) { | |
5893dfb9 | 2480 | struct btrfs_drop_extents_args drop_args = { 0 }; |
471d557a FM |
2481 | struct inode *inode; |
2482 | u64 from; | |
2483 | ||
2484 | inode = read_one_inode(root, key.objectid); | |
2485 | if (!inode) { | |
2486 | ret = -EIO; | |
2487 | break; | |
2488 | } | |
2489 | from = ALIGN(i_size_read(inode), | |
2490 | root->fs_info->sectorsize); | |
5893dfb9 FM |
2491 | drop_args.start = from; |
2492 | drop_args.end = (u64)-1; | |
2493 | drop_args.drop_cache = true; | |
2494 | ret = btrfs_drop_extents(wc->trans, root, | |
2495 | BTRFS_I(inode), | |
2496 | &drop_args); | |
471d557a | 2497 | if (!ret) { |
2766ff61 FM |
2498 | inode_sub_bytes(inode, |
2499 | drop_args.bytes_found); | |
f2d72f42 | 2500 | /* Update the inode's nbytes. */ |
471d557a | 2501 | ret = btrfs_update_inode(wc->trans, |
9a56fcd1 | 2502 | root, BTRFS_I(inode)); |
471d557a FM |
2503 | } |
2504 | iput(inode); | |
b50c6e25 JB |
2505 | if (ret) |
2506 | break; | |
e02119d5 | 2507 | } |
c71bf099 | 2508 | |
e02119d5 CM |
2509 | ret = link_to_fixup_dir(wc->trans, root, |
2510 | path, key.objectid); | |
b50c6e25 JB |
2511 | if (ret) |
2512 | break; | |
e02119d5 | 2513 | } |
dd8e7217 | 2514 | |
f2d72f42 FM |
2515 | if (wc->ignore_cur_inode) |
2516 | continue; | |
2517 | ||
dd8e7217 JB |
2518 | if (key.type == BTRFS_DIR_INDEX_KEY && |
2519 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { | |
2520 | ret = replay_one_dir_item(wc->trans, root, path, | |
2521 | eb, i, &key); | |
2522 | if (ret) | |
2523 | break; | |
2524 | } | |
2525 | ||
e02119d5 CM |
2526 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
2527 | continue; | |
2528 | ||
2529 | /* these keys are simply copied */ | |
2530 | if (key.type == BTRFS_XATTR_ITEM_KEY) { | |
2531 | ret = overwrite_item(wc->trans, root, path, | |
2532 | eb, i, &key); | |
b50c6e25 JB |
2533 | if (ret) |
2534 | break; | |
2da1c669 LB |
2535 | } else if (key.type == BTRFS_INODE_REF_KEY || |
2536 | key.type == BTRFS_INODE_EXTREF_KEY) { | |
f186373f MF |
2537 | ret = add_inode_ref(wc->trans, root, log, path, |
2538 | eb, i, &key); | |
b50c6e25 JB |
2539 | if (ret && ret != -ENOENT) |
2540 | break; | |
2541 | ret = 0; | |
e02119d5 CM |
2542 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
2543 | ret = replay_one_extent(wc->trans, root, path, | |
2544 | eb, i, &key); | |
b50c6e25 JB |
2545 | if (ret) |
2546 | break; | |
e02119d5 | 2547 | } |
339d0354 FM |
2548 | /* |
2549 | * We don't log BTRFS_DIR_ITEM_KEY keys anymore, only the | |
2550 | * BTRFS_DIR_INDEX_KEY items which we use to derive the | |
2551 | * BTRFS_DIR_ITEM_KEY items. If we are replaying a log from an | |
2552 | * older kernel with such keys, ignore them. | |
2553 | */ | |
e02119d5 CM |
2554 | } |
2555 | btrfs_free_path(path); | |
b50c6e25 | 2556 | return ret; |
e02119d5 CM |
2557 | } |
2558 | ||
6787bb9f NB |
2559 | /* |
2560 | * Correctly adjust the reserved bytes occupied by a log tree extent buffer | |
2561 | */ | |
2562 | static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start) | |
2563 | { | |
2564 | struct btrfs_block_group *cache; | |
2565 | ||
2566 | cache = btrfs_lookup_block_group(fs_info, start); | |
2567 | if (!cache) { | |
2568 | btrfs_err(fs_info, "unable to find block group for %llu", start); | |
2569 | return; | |
2570 | } | |
2571 | ||
2572 | spin_lock(&cache->space_info->lock); | |
2573 | spin_lock(&cache->lock); | |
2574 | cache->reserved -= fs_info->nodesize; | |
2575 | cache->space_info->bytes_reserved -= fs_info->nodesize; | |
2576 | spin_unlock(&cache->lock); | |
2577 | spin_unlock(&cache->space_info->lock); | |
2578 | ||
2579 | btrfs_put_block_group(cache); | |
2580 | } | |
2581 | ||
d397712b | 2582 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
2583 | struct btrfs_root *root, |
2584 | struct btrfs_path *path, int *level, | |
2585 | struct walk_control *wc) | |
2586 | { | |
0b246afa | 2587 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 CM |
2588 | u64 bytenr; |
2589 | u64 ptr_gen; | |
2590 | struct extent_buffer *next; | |
2591 | struct extent_buffer *cur; | |
e02119d5 CM |
2592 | u32 blocksize; |
2593 | int ret = 0; | |
2594 | ||
d397712b | 2595 | while (*level > 0) { |
581c1760 QW |
2596 | struct btrfs_key first_key; |
2597 | ||
e02119d5 CM |
2598 | cur = path->nodes[*level]; |
2599 | ||
fae7f21c | 2600 | WARN_ON(btrfs_header_level(cur) != *level); |
e02119d5 CM |
2601 | |
2602 | if (path->slots[*level] >= | |
2603 | btrfs_header_nritems(cur)) | |
2604 | break; | |
2605 | ||
2606 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | |
2607 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | |
581c1760 | 2608 | btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]); |
0b246afa | 2609 | blocksize = fs_info->nodesize; |
e02119d5 | 2610 | |
3fbaf258 JB |
2611 | next = btrfs_find_create_tree_block(fs_info, bytenr, |
2612 | btrfs_header_owner(cur), | |
2613 | *level - 1); | |
c871b0f2 LB |
2614 | if (IS_ERR(next)) |
2615 | return PTR_ERR(next); | |
e02119d5 | 2616 | |
e02119d5 | 2617 | if (*level == 1) { |
581c1760 QW |
2618 | ret = wc->process_func(root, next, wc, ptr_gen, |
2619 | *level - 1); | |
b50c6e25 JB |
2620 | if (ret) { |
2621 | free_extent_buffer(next); | |
1e5063d0 | 2622 | return ret; |
b50c6e25 | 2623 | } |
4a500fd1 | 2624 | |
e02119d5 CM |
2625 | path->slots[*level]++; |
2626 | if (wc->free) { | |
6a2e9dc4 | 2627 | ret = btrfs_read_extent_buffer(next, ptr_gen, |
581c1760 | 2628 | *level - 1, &first_key); |
018642a1 TI |
2629 | if (ret) { |
2630 | free_extent_buffer(next); | |
2631 | return ret; | |
2632 | } | |
e02119d5 | 2633 | |
681ae509 JB |
2634 | if (trans) { |
2635 | btrfs_tree_lock(next); | |
6a884d7d | 2636 | btrfs_clean_tree_block(next); |
681ae509 JB |
2637 | btrfs_wait_tree_block_writeback(next); |
2638 | btrfs_tree_unlock(next); | |
7bfc1007 | 2639 | ret = btrfs_pin_reserved_extent(trans, |
10e958d5 NB |
2640 | bytenr, blocksize); |
2641 | if (ret) { | |
2642 | free_extent_buffer(next); | |
2643 | return ret; | |
2644 | } | |
d3575156 NA |
2645 | btrfs_redirty_list_add( |
2646 | trans->transaction, next); | |
1846430c LB |
2647 | } else { |
2648 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) | |
2649 | clear_extent_buffer_dirty(next); | |
10e958d5 | 2650 | unaccount_log_buffer(fs_info, bytenr); |
3650860b | 2651 | } |
e02119d5 CM |
2652 | } |
2653 | free_extent_buffer(next); | |
2654 | continue; | |
2655 | } | |
6a2e9dc4 | 2656 | ret = btrfs_read_extent_buffer(next, ptr_gen, *level - 1, &first_key); |
018642a1 TI |
2657 | if (ret) { |
2658 | free_extent_buffer(next); | |
2659 | return ret; | |
2660 | } | |
e02119d5 | 2661 | |
e02119d5 CM |
2662 | if (path->nodes[*level-1]) |
2663 | free_extent_buffer(path->nodes[*level-1]); | |
2664 | path->nodes[*level-1] = next; | |
2665 | *level = btrfs_header_level(next); | |
2666 | path->slots[*level] = 0; | |
2667 | cond_resched(); | |
2668 | } | |
4a500fd1 | 2669 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
e02119d5 CM |
2670 | |
2671 | cond_resched(); | |
2672 | return 0; | |
2673 | } | |
2674 | ||
d397712b | 2675 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
2676 | struct btrfs_root *root, |
2677 | struct btrfs_path *path, int *level, | |
2678 | struct walk_control *wc) | |
2679 | { | |
0b246afa | 2680 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 CM |
2681 | int i; |
2682 | int slot; | |
2683 | int ret; | |
2684 | ||
d397712b | 2685 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
e02119d5 | 2686 | slot = path->slots[i]; |
4a500fd1 | 2687 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
e02119d5 CM |
2688 | path->slots[i]++; |
2689 | *level = i; | |
2690 | WARN_ON(*level == 0); | |
2691 | return 0; | |
2692 | } else { | |
1e5063d0 | 2693 | ret = wc->process_func(root, path->nodes[*level], wc, |
581c1760 QW |
2694 | btrfs_header_generation(path->nodes[*level]), |
2695 | *level); | |
1e5063d0 MF |
2696 | if (ret) |
2697 | return ret; | |
2698 | ||
e02119d5 CM |
2699 | if (wc->free) { |
2700 | struct extent_buffer *next; | |
2701 | ||
2702 | next = path->nodes[*level]; | |
2703 | ||
681ae509 JB |
2704 | if (trans) { |
2705 | btrfs_tree_lock(next); | |
6a884d7d | 2706 | btrfs_clean_tree_block(next); |
681ae509 JB |
2707 | btrfs_wait_tree_block_writeback(next); |
2708 | btrfs_tree_unlock(next); | |
7bfc1007 | 2709 | ret = btrfs_pin_reserved_extent(trans, |
10e958d5 NB |
2710 | path->nodes[*level]->start, |
2711 | path->nodes[*level]->len); | |
2712 | if (ret) | |
2713 | return ret; | |
84c25448 NA |
2714 | btrfs_redirty_list_add(trans->transaction, |
2715 | next); | |
1846430c LB |
2716 | } else { |
2717 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) | |
2718 | clear_extent_buffer_dirty(next); | |
e02119d5 | 2719 | |
10e958d5 NB |
2720 | unaccount_log_buffer(fs_info, |
2721 | path->nodes[*level]->start); | |
2722 | } | |
e02119d5 CM |
2723 | } |
2724 | free_extent_buffer(path->nodes[*level]); | |
2725 | path->nodes[*level] = NULL; | |
2726 | *level = i + 1; | |
2727 | } | |
2728 | } | |
2729 | return 1; | |
2730 | } | |
2731 | ||
2732 | /* | |
2733 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
2734 | * the tree freeing any blocks that have a ref count of zero after being | |
2735 | * decremented. | |
2736 | */ | |
2737 | static int walk_log_tree(struct btrfs_trans_handle *trans, | |
2738 | struct btrfs_root *log, struct walk_control *wc) | |
2739 | { | |
2ff7e61e | 2740 | struct btrfs_fs_info *fs_info = log->fs_info; |
e02119d5 CM |
2741 | int ret = 0; |
2742 | int wret; | |
2743 | int level; | |
2744 | struct btrfs_path *path; | |
e02119d5 CM |
2745 | int orig_level; |
2746 | ||
2747 | path = btrfs_alloc_path(); | |
db5b493a TI |
2748 | if (!path) |
2749 | return -ENOMEM; | |
e02119d5 CM |
2750 | |
2751 | level = btrfs_header_level(log->node); | |
2752 | orig_level = level; | |
2753 | path->nodes[level] = log->node; | |
67439dad | 2754 | atomic_inc(&log->node->refs); |
e02119d5 CM |
2755 | path->slots[level] = 0; |
2756 | ||
d397712b | 2757 | while (1) { |
e02119d5 CM |
2758 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
2759 | if (wret > 0) | |
2760 | break; | |
79787eaa | 2761 | if (wret < 0) { |
e02119d5 | 2762 | ret = wret; |
79787eaa JM |
2763 | goto out; |
2764 | } | |
e02119d5 CM |
2765 | |
2766 | wret = walk_up_log_tree(trans, log, path, &level, wc); | |
2767 | if (wret > 0) | |
2768 | break; | |
79787eaa | 2769 | if (wret < 0) { |
e02119d5 | 2770 | ret = wret; |
79787eaa JM |
2771 | goto out; |
2772 | } | |
e02119d5 CM |
2773 | } |
2774 | ||
2775 | /* was the root node processed? if not, catch it here */ | |
2776 | if (path->nodes[orig_level]) { | |
79787eaa | 2777 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
581c1760 QW |
2778 | btrfs_header_generation(path->nodes[orig_level]), |
2779 | orig_level); | |
79787eaa JM |
2780 | if (ret) |
2781 | goto out; | |
e02119d5 CM |
2782 | if (wc->free) { |
2783 | struct extent_buffer *next; | |
2784 | ||
2785 | next = path->nodes[orig_level]; | |
2786 | ||
681ae509 JB |
2787 | if (trans) { |
2788 | btrfs_tree_lock(next); | |
6a884d7d | 2789 | btrfs_clean_tree_block(next); |
681ae509 JB |
2790 | btrfs_wait_tree_block_writeback(next); |
2791 | btrfs_tree_unlock(next); | |
7bfc1007 | 2792 | ret = btrfs_pin_reserved_extent(trans, |
10e958d5 NB |
2793 | next->start, next->len); |
2794 | if (ret) | |
2795 | goto out; | |
84c25448 | 2796 | btrfs_redirty_list_add(trans->transaction, next); |
1846430c LB |
2797 | } else { |
2798 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) | |
2799 | clear_extent_buffer_dirty(next); | |
10e958d5 | 2800 | unaccount_log_buffer(fs_info, next->start); |
681ae509 | 2801 | } |
e02119d5 CM |
2802 | } |
2803 | } | |
2804 | ||
79787eaa | 2805 | out: |
e02119d5 | 2806 | btrfs_free_path(path); |
e02119d5 CM |
2807 | return ret; |
2808 | } | |
2809 | ||
7237f183 YZ |
2810 | /* |
2811 | * helper function to update the item for a given subvolumes log root | |
2812 | * in the tree of log roots | |
2813 | */ | |
2814 | static int update_log_root(struct btrfs_trans_handle *trans, | |
4203e968 JB |
2815 | struct btrfs_root *log, |
2816 | struct btrfs_root_item *root_item) | |
7237f183 | 2817 | { |
0b246afa | 2818 | struct btrfs_fs_info *fs_info = log->fs_info; |
7237f183 YZ |
2819 | int ret; |
2820 | ||
2821 | if (log->log_transid == 1) { | |
2822 | /* insert root item on the first sync */ | |
0b246afa | 2823 | ret = btrfs_insert_root(trans, fs_info->log_root_tree, |
4203e968 | 2824 | &log->root_key, root_item); |
7237f183 | 2825 | } else { |
0b246afa | 2826 | ret = btrfs_update_root(trans, fs_info->log_root_tree, |
4203e968 | 2827 | &log->root_key, root_item); |
7237f183 YZ |
2828 | } |
2829 | return ret; | |
2830 | } | |
2831 | ||
60d53eb3 | 2832 | static void wait_log_commit(struct btrfs_root *root, int transid) |
e02119d5 CM |
2833 | { |
2834 | DEFINE_WAIT(wait); | |
7237f183 | 2835 | int index = transid % 2; |
e02119d5 | 2836 | |
7237f183 YZ |
2837 | /* |
2838 | * we only allow two pending log transactions at a time, | |
2839 | * so we know that if ours is more than 2 older than the | |
2840 | * current transaction, we're done | |
2841 | */ | |
49e83f57 | 2842 | for (;;) { |
7237f183 YZ |
2843 | prepare_to_wait(&root->log_commit_wait[index], |
2844 | &wait, TASK_UNINTERRUPTIBLE); | |
12fcfd22 | 2845 | |
49e83f57 LB |
2846 | if (!(root->log_transid_committed < transid && |
2847 | atomic_read(&root->log_commit[index]))) | |
2848 | break; | |
12fcfd22 | 2849 | |
49e83f57 LB |
2850 | mutex_unlock(&root->log_mutex); |
2851 | schedule(); | |
7237f183 | 2852 | mutex_lock(&root->log_mutex); |
49e83f57 LB |
2853 | } |
2854 | finish_wait(&root->log_commit_wait[index], &wait); | |
7237f183 YZ |
2855 | } |
2856 | ||
60d53eb3 | 2857 | static void wait_for_writer(struct btrfs_root *root) |
7237f183 YZ |
2858 | { |
2859 | DEFINE_WAIT(wait); | |
8b050d35 | 2860 | |
49e83f57 LB |
2861 | for (;;) { |
2862 | prepare_to_wait(&root->log_writer_wait, &wait, | |
2863 | TASK_UNINTERRUPTIBLE); | |
2864 | if (!atomic_read(&root->log_writers)) | |
2865 | break; | |
2866 | ||
7237f183 | 2867 | mutex_unlock(&root->log_mutex); |
49e83f57 | 2868 | schedule(); |
575849ec | 2869 | mutex_lock(&root->log_mutex); |
7237f183 | 2870 | } |
49e83f57 | 2871 | finish_wait(&root->log_writer_wait, &wait); |
e02119d5 CM |
2872 | } |
2873 | ||
8b050d35 MX |
2874 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
2875 | struct btrfs_log_ctx *ctx) | |
2876 | { | |
8b050d35 MX |
2877 | mutex_lock(&root->log_mutex); |
2878 | list_del_init(&ctx->list); | |
2879 | mutex_unlock(&root->log_mutex); | |
2880 | } | |
2881 | ||
2882 | /* | |
2883 | * Invoked in log mutex context, or be sure there is no other task which | |
2884 | * can access the list. | |
2885 | */ | |
2886 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, | |
2887 | int index, int error) | |
2888 | { | |
2889 | struct btrfs_log_ctx *ctx; | |
570dd450 | 2890 | struct btrfs_log_ctx *safe; |
8b050d35 | 2891 | |
570dd450 CM |
2892 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
2893 | list_del_init(&ctx->list); | |
8b050d35 | 2894 | ctx->log_ret = error; |
570dd450 | 2895 | } |
8b050d35 MX |
2896 | } |
2897 | ||
e02119d5 CM |
2898 | /* |
2899 | * btrfs_sync_log does sends a given tree log down to the disk and | |
2900 | * updates the super blocks to record it. When this call is done, | |
12fcfd22 CM |
2901 | * you know that any inodes previously logged are safely on disk only |
2902 | * if it returns 0. | |
2903 | * | |
2904 | * Any other return value means you need to call btrfs_commit_transaction. | |
2905 | * Some of the edge cases for fsyncing directories that have had unlinks | |
2906 | * or renames done in the past mean that sometimes the only safe | |
2907 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, | |
2908 | * that has happened. | |
e02119d5 CM |
2909 | */ |
2910 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | |
8b050d35 | 2911 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
e02119d5 | 2912 | { |
7237f183 YZ |
2913 | int index1; |
2914 | int index2; | |
8cef4e16 | 2915 | int mark; |
e02119d5 | 2916 | int ret; |
0b246afa | 2917 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 | 2918 | struct btrfs_root *log = root->log_root; |
0b246afa | 2919 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; |
4203e968 | 2920 | struct btrfs_root_item new_root_item; |
bb14a59b | 2921 | int log_transid = 0; |
8b050d35 | 2922 | struct btrfs_log_ctx root_log_ctx; |
c6adc9cc | 2923 | struct blk_plug plug; |
47876f7c FM |
2924 | u64 log_root_start; |
2925 | u64 log_root_level; | |
e02119d5 | 2926 | |
7237f183 | 2927 | mutex_lock(&root->log_mutex); |
d1433deb MX |
2928 | log_transid = ctx->log_transid; |
2929 | if (root->log_transid_committed >= log_transid) { | |
2930 | mutex_unlock(&root->log_mutex); | |
2931 | return ctx->log_ret; | |
2932 | } | |
2933 | ||
2934 | index1 = log_transid % 2; | |
7237f183 | 2935 | if (atomic_read(&root->log_commit[index1])) { |
60d53eb3 | 2936 | wait_log_commit(root, log_transid); |
7237f183 | 2937 | mutex_unlock(&root->log_mutex); |
8b050d35 | 2938 | return ctx->log_ret; |
e02119d5 | 2939 | } |
d1433deb | 2940 | ASSERT(log_transid == root->log_transid); |
7237f183 YZ |
2941 | atomic_set(&root->log_commit[index1], 1); |
2942 | ||
2943 | /* wait for previous tree log sync to complete */ | |
2944 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) | |
60d53eb3 | 2945 | wait_log_commit(root, log_transid - 1); |
48cab2e0 | 2946 | |
86df7eb9 | 2947 | while (1) { |
2ecb7923 | 2948 | int batch = atomic_read(&root->log_batch); |
cd354ad6 | 2949 | /* when we're on an ssd, just kick the log commit out */ |
0b246afa | 2950 | if (!btrfs_test_opt(fs_info, SSD) && |
27cdeb70 | 2951 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { |
86df7eb9 YZ |
2952 | mutex_unlock(&root->log_mutex); |
2953 | schedule_timeout_uninterruptible(1); | |
2954 | mutex_lock(&root->log_mutex); | |
2955 | } | |
60d53eb3 | 2956 | wait_for_writer(root); |
2ecb7923 | 2957 | if (batch == atomic_read(&root->log_batch)) |
e02119d5 CM |
2958 | break; |
2959 | } | |
e02119d5 | 2960 | |
12fcfd22 | 2961 | /* bail out if we need to do a full commit */ |
4884b8e8 | 2962 | if (btrfs_need_log_full_commit(trans)) { |
f31f09f6 | 2963 | ret = BTRFS_LOG_FORCE_COMMIT; |
12fcfd22 CM |
2964 | mutex_unlock(&root->log_mutex); |
2965 | goto out; | |
2966 | } | |
2967 | ||
8cef4e16 YZ |
2968 | if (log_transid % 2 == 0) |
2969 | mark = EXTENT_DIRTY; | |
2970 | else | |
2971 | mark = EXTENT_NEW; | |
2972 | ||
690587d1 CM |
2973 | /* we start IO on all the marked extents here, but we don't actually |
2974 | * wait for them until later. | |
2975 | */ | |
c6adc9cc | 2976 | blk_start_plug(&plug); |
2ff7e61e | 2977 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); |
b528f467 NA |
2978 | /* |
2979 | * -EAGAIN happens when someone, e.g., a concurrent transaction | |
2980 | * commit, writes a dirty extent in this tree-log commit. This | |
2981 | * concurrent write will create a hole writing out the extents, | |
2982 | * and we cannot proceed on a zoned filesystem, requiring | |
2983 | * sequential writing. While we can bail out to a full commit | |
2984 | * here, but we can continue hoping the concurrent writing fills | |
2985 | * the hole. | |
2986 | */ | |
2987 | if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) | |
2988 | ret = 0; | |
79787eaa | 2989 | if (ret) { |
c6adc9cc | 2990 | blk_finish_plug(&plug); |
66642832 | 2991 | btrfs_abort_transaction(trans, ret); |
90787766 | 2992 | btrfs_set_log_full_commit(trans); |
79787eaa JM |
2993 | mutex_unlock(&root->log_mutex); |
2994 | goto out; | |
2995 | } | |
7237f183 | 2996 | |
4203e968 JB |
2997 | /* |
2998 | * We _must_ update under the root->log_mutex in order to make sure we | |
2999 | * have a consistent view of the log root we are trying to commit at | |
3000 | * this moment. | |
3001 | * | |
3002 | * We _must_ copy this into a local copy, because we are not holding the | |
3003 | * log_root_tree->log_mutex yet. This is important because when we | |
3004 | * commit the log_root_tree we must have a consistent view of the | |
3005 | * log_root_tree when we update the super block to point at the | |
3006 | * log_root_tree bytenr. If we update the log_root_tree here we'll race | |
3007 | * with the commit and possibly point at the new block which we may not | |
3008 | * have written out. | |
3009 | */ | |
5d4f98a2 | 3010 | btrfs_set_root_node(&log->root_item, log->node); |
4203e968 | 3011 | memcpy(&new_root_item, &log->root_item, sizeof(new_root_item)); |
7237f183 | 3012 | |
7237f183 YZ |
3013 | root->log_transid++; |
3014 | log->log_transid = root->log_transid; | |
ff782e0a | 3015 | root->log_start_pid = 0; |
7237f183 | 3016 | /* |
8cef4e16 YZ |
3017 | * IO has been started, blocks of the log tree have WRITTEN flag set |
3018 | * in their headers. new modifications of the log will be written to | |
3019 | * new positions. so it's safe to allow log writers to go in. | |
7237f183 YZ |
3020 | */ |
3021 | mutex_unlock(&root->log_mutex); | |
3022 | ||
3ddebf27 | 3023 | if (btrfs_is_zoned(fs_info)) { |
e75f9fd1 | 3024 | mutex_lock(&fs_info->tree_root->log_mutex); |
3ddebf27 NA |
3025 | if (!log_root_tree->node) { |
3026 | ret = btrfs_alloc_log_tree_node(trans, log_root_tree); | |
3027 | if (ret) { | |
ea32af47 | 3028 | mutex_unlock(&fs_info->tree_root->log_mutex); |
50ff5788 | 3029 | blk_finish_plug(&plug); |
3ddebf27 NA |
3030 | goto out; |
3031 | } | |
3032 | } | |
e75f9fd1 | 3033 | mutex_unlock(&fs_info->tree_root->log_mutex); |
3ddebf27 NA |
3034 | } |
3035 | ||
e75f9fd1 NA |
3036 | btrfs_init_log_ctx(&root_log_ctx, NULL); |
3037 | ||
3038 | mutex_lock(&log_root_tree->log_mutex); | |
3039 | ||
e3d3b415 FM |
3040 | index2 = log_root_tree->log_transid % 2; |
3041 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); | |
3042 | root_log_ctx.log_transid = log_root_tree->log_transid; | |
3043 | ||
4203e968 JB |
3044 | /* |
3045 | * Now we are safe to update the log_root_tree because we're under the | |
3046 | * log_mutex, and we're a current writer so we're holding the commit | |
3047 | * open until we drop the log_mutex. | |
3048 | */ | |
3049 | ret = update_log_root(trans, log, &new_root_item); | |
4a500fd1 | 3050 | if (ret) { |
d1433deb MX |
3051 | if (!list_empty(&root_log_ctx.list)) |
3052 | list_del_init(&root_log_ctx.list); | |
3053 | ||
c6adc9cc | 3054 | blk_finish_plug(&plug); |
90787766 | 3055 | btrfs_set_log_full_commit(trans); |
995946dd | 3056 | |
79787eaa | 3057 | if (ret != -ENOSPC) { |
66642832 | 3058 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3059 | mutex_unlock(&log_root_tree->log_mutex); |
3060 | goto out; | |
3061 | } | |
bf89d38f | 3062 | btrfs_wait_tree_log_extents(log, mark); |
4a500fd1 | 3063 | mutex_unlock(&log_root_tree->log_mutex); |
f31f09f6 | 3064 | ret = BTRFS_LOG_FORCE_COMMIT; |
4a500fd1 YZ |
3065 | goto out; |
3066 | } | |
3067 | ||
d1433deb | 3068 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
3da5ab56 | 3069 | blk_finish_plug(&plug); |
cbd60aa7 | 3070 | list_del_init(&root_log_ctx.list); |
d1433deb MX |
3071 | mutex_unlock(&log_root_tree->log_mutex); |
3072 | ret = root_log_ctx.log_ret; | |
3073 | goto out; | |
3074 | } | |
8b050d35 | 3075 | |
d1433deb | 3076 | index2 = root_log_ctx.log_transid % 2; |
7237f183 | 3077 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
c6adc9cc | 3078 | blk_finish_plug(&plug); |
bf89d38f | 3079 | ret = btrfs_wait_tree_log_extents(log, mark); |
60d53eb3 | 3080 | wait_log_commit(log_root_tree, |
d1433deb | 3081 | root_log_ctx.log_transid); |
7237f183 | 3082 | mutex_unlock(&log_root_tree->log_mutex); |
5ab5e44a FM |
3083 | if (!ret) |
3084 | ret = root_log_ctx.log_ret; | |
7237f183 YZ |
3085 | goto out; |
3086 | } | |
d1433deb | 3087 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
7237f183 YZ |
3088 | atomic_set(&log_root_tree->log_commit[index2], 1); |
3089 | ||
12fcfd22 | 3090 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
60d53eb3 | 3091 | wait_log_commit(log_root_tree, |
d1433deb | 3092 | root_log_ctx.log_transid - 1); |
12fcfd22 CM |
3093 | } |
3094 | ||
12fcfd22 CM |
3095 | /* |
3096 | * now that we've moved on to the tree of log tree roots, | |
3097 | * check the full commit flag again | |
3098 | */ | |
4884b8e8 | 3099 | if (btrfs_need_log_full_commit(trans)) { |
c6adc9cc | 3100 | blk_finish_plug(&plug); |
bf89d38f | 3101 | btrfs_wait_tree_log_extents(log, mark); |
12fcfd22 | 3102 | mutex_unlock(&log_root_tree->log_mutex); |
f31f09f6 | 3103 | ret = BTRFS_LOG_FORCE_COMMIT; |
12fcfd22 CM |
3104 | goto out_wake_log_root; |
3105 | } | |
7237f183 | 3106 | |
2ff7e61e | 3107 | ret = btrfs_write_marked_extents(fs_info, |
c6adc9cc MX |
3108 | &log_root_tree->dirty_log_pages, |
3109 | EXTENT_DIRTY | EXTENT_NEW); | |
3110 | blk_finish_plug(&plug); | |
b528f467 NA |
3111 | /* |
3112 | * As described above, -EAGAIN indicates a hole in the extents. We | |
3113 | * cannot wait for these write outs since the waiting cause a | |
3114 | * deadlock. Bail out to the full commit instead. | |
3115 | */ | |
3116 | if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) { | |
3117 | btrfs_set_log_full_commit(trans); | |
3118 | btrfs_wait_tree_log_extents(log, mark); | |
3119 | mutex_unlock(&log_root_tree->log_mutex); | |
3120 | goto out_wake_log_root; | |
3121 | } else if (ret) { | |
90787766 | 3122 | btrfs_set_log_full_commit(trans); |
66642832 | 3123 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3124 | mutex_unlock(&log_root_tree->log_mutex); |
3125 | goto out_wake_log_root; | |
3126 | } | |
bf89d38f | 3127 | ret = btrfs_wait_tree_log_extents(log, mark); |
5ab5e44a | 3128 | if (!ret) |
bf89d38f JM |
3129 | ret = btrfs_wait_tree_log_extents(log_root_tree, |
3130 | EXTENT_NEW | EXTENT_DIRTY); | |
5ab5e44a | 3131 | if (ret) { |
90787766 | 3132 | btrfs_set_log_full_commit(trans); |
5ab5e44a FM |
3133 | mutex_unlock(&log_root_tree->log_mutex); |
3134 | goto out_wake_log_root; | |
3135 | } | |
e02119d5 | 3136 | |
47876f7c FM |
3137 | log_root_start = log_root_tree->node->start; |
3138 | log_root_level = btrfs_header_level(log_root_tree->node); | |
7237f183 | 3139 | log_root_tree->log_transid++; |
7237f183 YZ |
3140 | mutex_unlock(&log_root_tree->log_mutex); |
3141 | ||
3142 | /* | |
47876f7c FM |
3143 | * Here we are guaranteed that nobody is going to write the superblock |
3144 | * for the current transaction before us and that neither we do write | |
3145 | * our superblock before the previous transaction finishes its commit | |
3146 | * and writes its superblock, because: | |
3147 | * | |
3148 | * 1) We are holding a handle on the current transaction, so no body | |
3149 | * can commit it until we release the handle; | |
3150 | * | |
3151 | * 2) Before writing our superblock we acquire the tree_log_mutex, so | |
3152 | * if the previous transaction is still committing, and hasn't yet | |
3153 | * written its superblock, we wait for it to do it, because a | |
3154 | * transaction commit acquires the tree_log_mutex when the commit | |
3155 | * begins and releases it only after writing its superblock. | |
7237f183 | 3156 | */ |
47876f7c | 3157 | mutex_lock(&fs_info->tree_log_mutex); |
165ea85f JB |
3158 | |
3159 | /* | |
3160 | * The previous transaction writeout phase could have failed, and thus | |
3161 | * marked the fs in an error state. We must not commit here, as we | |
3162 | * could have updated our generation in the super_for_commit and | |
3163 | * writing the super here would result in transid mismatches. If there | |
3164 | * is an error here just bail. | |
3165 | */ | |
84961539 | 3166 | if (BTRFS_FS_ERROR(fs_info)) { |
165ea85f JB |
3167 | ret = -EIO; |
3168 | btrfs_set_log_full_commit(trans); | |
3169 | btrfs_abort_transaction(trans, ret); | |
3170 | mutex_unlock(&fs_info->tree_log_mutex); | |
3171 | goto out_wake_log_root; | |
3172 | } | |
3173 | ||
47876f7c FM |
3174 | btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start); |
3175 | btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level); | |
eece6a9c | 3176 | ret = write_all_supers(fs_info, 1); |
47876f7c | 3177 | mutex_unlock(&fs_info->tree_log_mutex); |
5af3e8cc | 3178 | if (ret) { |
90787766 | 3179 | btrfs_set_log_full_commit(trans); |
66642832 | 3180 | btrfs_abort_transaction(trans, ret); |
5af3e8cc SB |
3181 | goto out_wake_log_root; |
3182 | } | |
7237f183 | 3183 | |
e1a6d264 FM |
3184 | /* |
3185 | * We know there can only be one task here, since we have not yet set | |
3186 | * root->log_commit[index1] to 0 and any task attempting to sync the | |
3187 | * log must wait for the previous log transaction to commit if it's | |
3188 | * still in progress or wait for the current log transaction commit if | |
3189 | * someone else already started it. We use <= and not < because the | |
3190 | * first log transaction has an ID of 0. | |
3191 | */ | |
3192 | ASSERT(root->last_log_commit <= log_transid); | |
3193 | root->last_log_commit = log_transid; | |
257c62e1 | 3194 | |
12fcfd22 | 3195 | out_wake_log_root: |
570dd450 | 3196 | mutex_lock(&log_root_tree->log_mutex); |
8b050d35 MX |
3197 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
3198 | ||
d1433deb | 3199 | log_root_tree->log_transid_committed++; |
7237f183 | 3200 | atomic_set(&log_root_tree->log_commit[index2], 0); |
d1433deb MX |
3201 | mutex_unlock(&log_root_tree->log_mutex); |
3202 | ||
33a9eca7 | 3203 | /* |
093258e6 DS |
3204 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
3205 | * all the updates above are seen by the woken threads. It might not be | |
3206 | * necessary, but proving that seems to be hard. | |
33a9eca7 | 3207 | */ |
093258e6 | 3208 | cond_wake_up(&log_root_tree->log_commit_wait[index2]); |
e02119d5 | 3209 | out: |
d1433deb | 3210 | mutex_lock(&root->log_mutex); |
570dd450 | 3211 | btrfs_remove_all_log_ctxs(root, index1, ret); |
d1433deb | 3212 | root->log_transid_committed++; |
7237f183 | 3213 | atomic_set(&root->log_commit[index1], 0); |
d1433deb | 3214 | mutex_unlock(&root->log_mutex); |
8b050d35 | 3215 | |
33a9eca7 | 3216 | /* |
093258e6 DS |
3217 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
3218 | * all the updates above are seen by the woken threads. It might not be | |
3219 | * necessary, but proving that seems to be hard. | |
33a9eca7 | 3220 | */ |
093258e6 | 3221 | cond_wake_up(&root->log_commit_wait[index1]); |
b31eabd8 | 3222 | return ret; |
e02119d5 CM |
3223 | } |
3224 | ||
4a500fd1 YZ |
3225 | static void free_log_tree(struct btrfs_trans_handle *trans, |
3226 | struct btrfs_root *log) | |
e02119d5 CM |
3227 | { |
3228 | int ret; | |
e02119d5 CM |
3229 | struct walk_control wc = { |
3230 | .free = 1, | |
3231 | .process_func = process_one_buffer | |
3232 | }; | |
3233 | ||
3ddebf27 NA |
3234 | if (log->node) { |
3235 | ret = walk_log_tree(trans, log, &wc); | |
3236 | if (ret) { | |
40cdc509 FM |
3237 | /* |
3238 | * We weren't able to traverse the entire log tree, the | |
3239 | * typical scenario is getting an -EIO when reading an | |
3240 | * extent buffer of the tree, due to a previous writeback | |
3241 | * failure of it. | |
3242 | */ | |
3243 | set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, | |
3244 | &log->fs_info->fs_state); | |
3245 | ||
3246 | /* | |
3247 | * Some extent buffers of the log tree may still be dirty | |
3248 | * and not yet written back to storage, because we may | |
3249 | * have updates to a log tree without syncing a log tree, | |
3250 | * such as during rename and link operations. So flush | |
3251 | * them out and wait for their writeback to complete, so | |
3252 | * that we properly cleanup their state and pages. | |
3253 | */ | |
3254 | btrfs_write_marked_extents(log->fs_info, | |
3255 | &log->dirty_log_pages, | |
3256 | EXTENT_DIRTY | EXTENT_NEW); | |
3257 | btrfs_wait_tree_log_extents(log, | |
3258 | EXTENT_DIRTY | EXTENT_NEW); | |
3259 | ||
3ddebf27 NA |
3260 | if (trans) |
3261 | btrfs_abort_transaction(trans, ret); | |
3262 | else | |
3263 | btrfs_handle_fs_error(log->fs_info, ret, NULL); | |
3264 | } | |
374b0e2d | 3265 | } |
e02119d5 | 3266 | |
59b0713a FM |
3267 | clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1, |
3268 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT); | |
e289f03e | 3269 | extent_io_tree_release(&log->log_csum_range); |
d3575156 | 3270 | |
00246528 | 3271 | btrfs_put_root(log); |
4a500fd1 YZ |
3272 | } |
3273 | ||
3274 | /* | |
3275 | * free all the extents used by the tree log. This should be called | |
3276 | * at commit time of the full transaction | |
3277 | */ | |
3278 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) | |
3279 | { | |
3280 | if (root->log_root) { | |
3281 | free_log_tree(trans, root->log_root); | |
3282 | root->log_root = NULL; | |
e7a79811 | 3283 | clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); |
4a500fd1 YZ |
3284 | } |
3285 | return 0; | |
3286 | } | |
3287 | ||
3288 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, | |
3289 | struct btrfs_fs_info *fs_info) | |
3290 | { | |
3291 | if (fs_info->log_root_tree) { | |
3292 | free_log_tree(trans, fs_info->log_root_tree); | |
3293 | fs_info->log_root_tree = NULL; | |
47876f7c | 3294 | clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state); |
4a500fd1 | 3295 | } |
e02119d5 CM |
3296 | return 0; |
3297 | } | |
3298 | ||
803f0f64 | 3299 | /* |
0f8ce498 FM |
3300 | * Check if an inode was logged in the current transaction. This correctly deals |
3301 | * with the case where the inode was logged but has a logged_trans of 0, which | |
3302 | * happens if the inode is evicted and loaded again, as logged_trans is an in | |
3303 | * memory only field (not persisted). | |
3304 | * | |
3305 | * Returns 1 if the inode was logged before in the transaction, 0 if it was not, | |
3306 | * and < 0 on error. | |
803f0f64 | 3307 | */ |
0f8ce498 FM |
3308 | static int inode_logged(struct btrfs_trans_handle *trans, |
3309 | struct btrfs_inode *inode, | |
3310 | struct btrfs_path *path_in) | |
803f0f64 | 3311 | { |
0f8ce498 FM |
3312 | struct btrfs_path *path = path_in; |
3313 | struct btrfs_key key; | |
3314 | int ret; | |
3315 | ||
803f0f64 | 3316 | if (inode->logged_trans == trans->transid) |
0f8ce498 | 3317 | return 1; |
803f0f64 | 3318 | |
0f8ce498 FM |
3319 | /* |
3320 | * If logged_trans is not 0, then we know the inode logged was not logged | |
3321 | * in this transaction, so we can return false right away. | |
3322 | */ | |
3323 | if (inode->logged_trans > 0) | |
3324 | return 0; | |
3325 | ||
3326 | /* | |
3327 | * If no log tree was created for this root in this transaction, then | |
3328 | * the inode can not have been logged in this transaction. In that case | |
3329 | * set logged_trans to anything greater than 0 and less than the current | |
3330 | * transaction's ID, to avoid the search below in a future call in case | |
3331 | * a log tree gets created after this. | |
3332 | */ | |
3333 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state)) { | |
3334 | inode->logged_trans = trans->transid - 1; | |
3335 | return 0; | |
3336 | } | |
3337 | ||
3338 | /* | |
3339 | * We have a log tree and the inode's logged_trans is 0. We can't tell | |
3340 | * for sure if the inode was logged before in this transaction by looking | |
3341 | * only at logged_trans. We could be pessimistic and assume it was, but | |
3342 | * that can lead to unnecessarily logging an inode during rename and link | |
3343 | * operations, and then further updating the log in followup rename and | |
3344 | * link operations, specially if it's a directory, which adds latency | |
3345 | * visible to applications doing a series of rename or link operations. | |
3346 | * | |
3347 | * A logged_trans of 0 here can mean several things: | |
3348 | * | |
3349 | * 1) The inode was never logged since the filesystem was mounted, and may | |
3350 | * or may have not been evicted and loaded again; | |
3351 | * | |
3352 | * 2) The inode was logged in a previous transaction, then evicted and | |
3353 | * then loaded again; | |
3354 | * | |
3355 | * 3) The inode was logged in the current transaction, then evicted and | |
3356 | * then loaded again. | |
3357 | * | |
3358 | * For cases 1) and 2) we don't want to return true, but we need to detect | |
3359 | * case 3) and return true. So we do a search in the log root for the inode | |
3360 | * item. | |
3361 | */ | |
3362 | key.objectid = btrfs_ino(inode); | |
3363 | key.type = BTRFS_INODE_ITEM_KEY; | |
3364 | key.offset = 0; | |
3365 | ||
3366 | if (!path) { | |
3367 | path = btrfs_alloc_path(); | |
3368 | if (!path) | |
3369 | return -ENOMEM; | |
3370 | } | |
3371 | ||
3372 | ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); | |
3373 | ||
3374 | if (path_in) | |
3375 | btrfs_release_path(path); | |
3376 | else | |
3377 | btrfs_free_path(path); | |
1e0860f3 | 3378 | |
6e8e777d | 3379 | /* |
0f8ce498 FM |
3380 | * Logging an inode always results in logging its inode item. So if we |
3381 | * did not find the item we know the inode was not logged for sure. | |
6e8e777d | 3382 | */ |
0f8ce498 FM |
3383 | if (ret < 0) { |
3384 | return ret; | |
3385 | } else if (ret > 0) { | |
3386 | /* | |
3387 | * Set logged_trans to a value greater than 0 and less then the | |
3388 | * current transaction to avoid doing the search in future calls. | |
3389 | */ | |
3390 | inode->logged_trans = trans->transid - 1; | |
3391 | return 0; | |
3392 | } | |
3393 | ||
3394 | /* | |
3395 | * The inode was previously logged and then evicted, set logged_trans to | |
3396 | * the current transacion's ID, to avoid future tree searches as long as | |
3397 | * the inode is not evicted again. | |
3398 | */ | |
3399 | inode->logged_trans = trans->transid; | |
3400 | ||
3401 | /* | |
3402 | * If it's a directory, then we must set last_dir_index_offset to the | |
3403 | * maximum possible value, so that the next attempt to log the inode does | |
3404 | * not skip checking if dir index keys found in modified subvolume tree | |
3405 | * leaves have been logged before, otherwise it would result in attempts | |
3406 | * to insert duplicate dir index keys in the log tree. This must be done | |
3407 | * because last_dir_index_offset is an in-memory only field, not persisted | |
3408 | * in the inode item or any other on-disk structure, so its value is lost | |
3409 | * once the inode is evicted. | |
3410 | */ | |
3411 | if (S_ISDIR(inode->vfs_inode.i_mode)) | |
3412 | inode->last_dir_index_offset = (u64)-1; | |
803f0f64 | 3413 | |
0f8ce498 | 3414 | return 1; |
803f0f64 FM |
3415 | } |
3416 | ||
839061fe FM |
3417 | /* |
3418 | * Delete a directory entry from the log if it exists. | |
3419 | * | |
3420 | * Returns < 0 on error | |
3421 | * 1 if the entry does not exists | |
3422 | * 0 if the entry existed and was successfully deleted | |
3423 | */ | |
3424 | static int del_logged_dentry(struct btrfs_trans_handle *trans, | |
3425 | struct btrfs_root *log, | |
3426 | struct btrfs_path *path, | |
3427 | u64 dir_ino, | |
6db75318 | 3428 | const struct fscrypt_str *name, |
839061fe FM |
3429 | u64 index) |
3430 | { | |
3431 | struct btrfs_dir_item *di; | |
3432 | ||
3433 | /* | |
3434 | * We only log dir index items of a directory, so we don't need to look | |
3435 | * for dir item keys. | |
3436 | */ | |
3437 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, | |
e43eec81 | 3438 | index, name, -1); |
839061fe FM |
3439 | if (IS_ERR(di)) |
3440 | return PTR_ERR(di); | |
3441 | else if (!di) | |
3442 | return 1; | |
3443 | ||
3444 | /* | |
3445 | * We do not need to update the size field of the directory's | |
3446 | * inode item because on log replay we update the field to reflect | |
3447 | * all existing entries in the directory (see overwrite_item()). | |
3448 | */ | |
3449 | return btrfs_delete_one_dir_name(trans, log, path, di); | |
3450 | } | |
3451 | ||
e02119d5 CM |
3452 | /* |
3453 | * If both a file and directory are logged, and unlinks or renames are | |
3454 | * mixed in, we have a few interesting corners: | |
3455 | * | |
3456 | * create file X in dir Y | |
3457 | * link file X to X.link in dir Y | |
3458 | * fsync file X | |
3459 | * unlink file X but leave X.link | |
3460 | * fsync dir Y | |
3461 | * | |
3462 | * After a crash we would expect only X.link to exist. But file X | |
3463 | * didn't get fsync'd again so the log has back refs for X and X.link. | |
3464 | * | |
3465 | * We solve this by removing directory entries and inode backrefs from the | |
3466 | * log when a file that was logged in the current transaction is | |
3467 | * unlinked. Any later fsync will include the updated log entries, and | |
3468 | * we'll be able to reconstruct the proper directory items from backrefs. | |
3469 | * | |
3470 | * This optimizations allows us to avoid relogging the entire inode | |
3471 | * or the entire directory. | |
3472 | */ | |
9a35fc95 JB |
3473 | void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
3474 | struct btrfs_root *root, | |
6db75318 | 3475 | const struct fscrypt_str *name, |
9a35fc95 | 3476 | struct btrfs_inode *dir, u64 index) |
e02119d5 | 3477 | { |
e02119d5 CM |
3478 | struct btrfs_path *path; |
3479 | int ret; | |
e02119d5 | 3480 | |
0f8ce498 FM |
3481 | ret = inode_logged(trans, dir, NULL); |
3482 | if (ret == 0) | |
3483 | return; | |
3484 | else if (ret < 0) { | |
3485 | btrfs_set_log_full_commit(trans); | |
9a35fc95 | 3486 | return; |
0f8ce498 | 3487 | } |
3a5f1d45 | 3488 | |
e02119d5 CM |
3489 | ret = join_running_log_trans(root); |
3490 | if (ret) | |
9a35fc95 | 3491 | return; |
e02119d5 | 3492 | |
49f34d1f | 3493 | mutex_lock(&dir->log_mutex); |
e02119d5 | 3494 | |
e02119d5 | 3495 | path = btrfs_alloc_path(); |
a62f44a5 | 3496 | if (!path) { |
839061fe | 3497 | ret = -ENOMEM; |
a62f44a5 TI |
3498 | goto out_unlock; |
3499 | } | |
2a29edc6 | 3500 | |
839061fe | 3501 | ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir), |
e43eec81 | 3502 | name, index); |
e02119d5 | 3503 | btrfs_free_path(path); |
a62f44a5 | 3504 | out_unlock: |
49f34d1f | 3505 | mutex_unlock(&dir->log_mutex); |
839061fe | 3506 | if (ret < 0) |
90787766 | 3507 | btrfs_set_log_full_commit(trans); |
12fcfd22 | 3508 | btrfs_end_log_trans(root); |
e02119d5 CM |
3509 | } |
3510 | ||
3511 | /* see comments for btrfs_del_dir_entries_in_log */ | |
9a35fc95 JB |
3512 | void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
3513 | struct btrfs_root *root, | |
6db75318 | 3514 | const struct fscrypt_str *name, |
9a35fc95 | 3515 | struct btrfs_inode *inode, u64 dirid) |
e02119d5 CM |
3516 | { |
3517 | struct btrfs_root *log; | |
3518 | u64 index; | |
3519 | int ret; | |
3520 | ||
0f8ce498 FM |
3521 | ret = inode_logged(trans, inode, NULL); |
3522 | if (ret == 0) | |
9a35fc95 | 3523 | return; |
0f8ce498 FM |
3524 | else if (ret < 0) { |
3525 | btrfs_set_log_full_commit(trans); | |
3526 | return; | |
3527 | } | |
3a5f1d45 | 3528 | |
e02119d5 CM |
3529 | ret = join_running_log_trans(root); |
3530 | if (ret) | |
9a35fc95 | 3531 | return; |
e02119d5 | 3532 | log = root->log_root; |
a491abb2 | 3533 | mutex_lock(&inode->log_mutex); |
e02119d5 | 3534 | |
e43eec81 | 3535 | ret = btrfs_del_inode_ref(trans, log, name, btrfs_ino(inode), |
e02119d5 | 3536 | dirid, &index); |
a491abb2 | 3537 | mutex_unlock(&inode->log_mutex); |
9a35fc95 | 3538 | if (ret < 0 && ret != -ENOENT) |
90787766 | 3539 | btrfs_set_log_full_commit(trans); |
12fcfd22 | 3540 | btrfs_end_log_trans(root); |
e02119d5 CM |
3541 | } |
3542 | ||
3543 | /* | |
3544 | * creates a range item in the log for 'dirid'. first_offset and | |
3545 | * last_offset tell us which parts of the key space the log should | |
3546 | * be considered authoritative for. | |
3547 | */ | |
3548 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, | |
3549 | struct btrfs_root *log, | |
3550 | struct btrfs_path *path, | |
339d0354 | 3551 | u64 dirid, |
e02119d5 CM |
3552 | u64 first_offset, u64 last_offset) |
3553 | { | |
3554 | int ret; | |
3555 | struct btrfs_key key; | |
3556 | struct btrfs_dir_log_item *item; | |
3557 | ||
3558 | key.objectid = dirid; | |
3559 | key.offset = first_offset; | |
339d0354 | 3560 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
e02119d5 | 3561 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
750ee454 FM |
3562 | /* |
3563 | * -EEXIST is fine and can happen sporadically when we are logging a | |
3564 | * directory and have concurrent insertions in the subvolume's tree for | |
3565 | * items from other inodes and that result in pushing off some dir items | |
3566 | * from one leaf to another in order to accommodate for the new items. | |
3567 | * This results in logging the same dir index range key. | |
3568 | */ | |
3569 | if (ret && ret != -EEXIST) | |
4a500fd1 | 3570 | return ret; |
e02119d5 CM |
3571 | |
3572 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3573 | struct btrfs_dir_log_item); | |
750ee454 FM |
3574 | if (ret == -EEXIST) { |
3575 | const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item); | |
3576 | ||
3577 | /* | |
3578 | * btrfs_del_dir_entries_in_log() might have been called during | |
3579 | * an unlink between the initial insertion of this key and the | |
3580 | * current update, or we might be logging a single entry deletion | |
3581 | * during a rename, so set the new last_offset to the max value. | |
3582 | */ | |
3583 | last_offset = max(last_offset, curr_end); | |
3584 | } | |
e02119d5 CM |
3585 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
3586 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
b3b4aa74 | 3587 | btrfs_release_path(path); |
e02119d5 CM |
3588 | return 0; |
3589 | } | |
3590 | ||
086dcbfa FM |
3591 | static int flush_dir_items_batch(struct btrfs_trans_handle *trans, |
3592 | struct btrfs_root *log, | |
3593 | struct extent_buffer *src, | |
3594 | struct btrfs_path *dst_path, | |
3595 | int start_slot, | |
3596 | int count) | |
3597 | { | |
3598 | char *ins_data = NULL; | |
b7ef5f3a | 3599 | struct btrfs_item_batch batch; |
086dcbfa | 3600 | struct extent_buffer *dst; |
da1b811f FM |
3601 | unsigned long src_offset; |
3602 | unsigned long dst_offset; | |
086dcbfa FM |
3603 | struct btrfs_key key; |
3604 | u32 item_size; | |
3605 | int ret; | |
3606 | int i; | |
3607 | ||
3608 | ASSERT(count > 0); | |
b7ef5f3a | 3609 | batch.nr = count; |
086dcbfa FM |
3610 | |
3611 | if (count == 1) { | |
3612 | btrfs_item_key_to_cpu(src, &key, start_slot); | |
3212fa14 | 3613 | item_size = btrfs_item_size(src, start_slot); |
b7ef5f3a FM |
3614 | batch.keys = &key; |
3615 | batch.data_sizes = &item_size; | |
3616 | batch.total_data_size = item_size; | |
086dcbfa | 3617 | } else { |
b7ef5f3a FM |
3618 | struct btrfs_key *ins_keys; |
3619 | u32 *ins_sizes; | |
3620 | ||
086dcbfa FM |
3621 | ins_data = kmalloc(count * sizeof(u32) + |
3622 | count * sizeof(struct btrfs_key), GFP_NOFS); | |
3623 | if (!ins_data) | |
3624 | return -ENOMEM; | |
3625 | ||
3626 | ins_sizes = (u32 *)ins_data; | |
3627 | ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32)); | |
b7ef5f3a FM |
3628 | batch.keys = ins_keys; |
3629 | batch.data_sizes = ins_sizes; | |
3630 | batch.total_data_size = 0; | |
086dcbfa FM |
3631 | |
3632 | for (i = 0; i < count; i++) { | |
3633 | const int slot = start_slot + i; | |
3634 | ||
3635 | btrfs_item_key_to_cpu(src, &ins_keys[i], slot); | |
3212fa14 | 3636 | ins_sizes[i] = btrfs_item_size(src, slot); |
b7ef5f3a | 3637 | batch.total_data_size += ins_sizes[i]; |
086dcbfa FM |
3638 | } |
3639 | } | |
3640 | ||
b7ef5f3a | 3641 | ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); |
086dcbfa FM |
3642 | if (ret) |
3643 | goto out; | |
3644 | ||
3645 | dst = dst_path->nodes[0]; | |
da1b811f FM |
3646 | /* |
3647 | * Copy all the items in bulk, in a single copy operation. Item data is | |
3648 | * organized such that it's placed at the end of a leaf and from right | |
3649 | * to left. For example, the data for the second item ends at an offset | |
3650 | * that matches the offset where the data for the first item starts, the | |
3651 | * data for the third item ends at an offset that matches the offset | |
3652 | * where the data of the second items starts, and so on. | |
3653 | * Therefore our source and destination start offsets for copy match the | |
3654 | * offsets of the last items (highest slots). | |
3655 | */ | |
3656 | dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1); | |
3657 | src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1); | |
3658 | copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size); | |
086dcbfa FM |
3659 | btrfs_release_path(dst_path); |
3660 | out: | |
3661 | kfree(ins_data); | |
3662 | ||
3663 | return ret; | |
3664 | } | |
3665 | ||
eb10d85e FM |
3666 | static int process_dir_items_leaf(struct btrfs_trans_handle *trans, |
3667 | struct btrfs_inode *inode, | |
3668 | struct btrfs_path *path, | |
3669 | struct btrfs_path *dst_path, | |
732d591a FM |
3670 | struct btrfs_log_ctx *ctx, |
3671 | u64 *last_old_dentry_offset) | |
eb10d85e FM |
3672 | { |
3673 | struct btrfs_root *log = inode->root->log_root; | |
796787c9 FM |
3674 | struct extent_buffer *src; |
3675 | const int nritems = btrfs_header_nritems(path->nodes[0]); | |
eb10d85e | 3676 | const u64 ino = btrfs_ino(inode); |
086dcbfa FM |
3677 | bool last_found = false; |
3678 | int batch_start = 0; | |
3679 | int batch_size = 0; | |
eb10d85e FM |
3680 | int i; |
3681 | ||
796787c9 FM |
3682 | /* |
3683 | * We need to clone the leaf, release the read lock on it, and use the | |
3684 | * clone before modifying the log tree. See the comment at copy_items() | |
3685 | * about why we need to do this. | |
3686 | */ | |
3687 | src = btrfs_clone_extent_buffer(path->nodes[0]); | |
3688 | if (!src) | |
3689 | return -ENOMEM; | |
3690 | ||
3691 | i = path->slots[0]; | |
3692 | btrfs_release_path(path); | |
3693 | path->nodes[0] = src; | |
3694 | path->slots[0] = i; | |
3695 | ||
3696 | for (; i < nritems; i++) { | |
732d591a | 3697 | struct btrfs_dir_item *di; |
eb10d85e | 3698 | struct btrfs_key key; |
eb10d85e FM |
3699 | int ret; |
3700 | ||
3701 | btrfs_item_key_to_cpu(src, &key, i); | |
3702 | ||
339d0354 | 3703 | if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) { |
086dcbfa FM |
3704 | last_found = true; |
3705 | break; | |
3706 | } | |
eb10d85e | 3707 | |
732d591a | 3708 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); |
dc287224 | 3709 | ctx->last_dir_item_offset = key.offset; |
732d591a FM |
3710 | |
3711 | /* | |
3712 | * Skip ranges of items that consist only of dir item keys created | |
3713 | * in past transactions. However if we find a gap, we must log a | |
3714 | * dir index range item for that gap, so that index keys in that | |
3715 | * gap are deleted during log replay. | |
3716 | */ | |
3717 | if (btrfs_dir_transid(src, di) < trans->transid) { | |
3718 | if (key.offset > *last_old_dentry_offset + 1) { | |
3719 | ret = insert_dir_log_key(trans, log, dst_path, | |
3720 | ino, *last_old_dentry_offset + 1, | |
3721 | key.offset - 1); | |
732d591a FM |
3722 | if (ret < 0) |
3723 | return ret; | |
3724 | } | |
3725 | ||
3726 | *last_old_dentry_offset = key.offset; | |
3727 | continue; | |
3728 | } | |
193df624 FM |
3729 | |
3730 | /* If we logged this dir index item before, we can skip it. */ | |
3731 | if (key.offset <= inode->last_dir_index_offset) | |
3732 | continue; | |
3733 | ||
eb10d85e FM |
3734 | /* |
3735 | * We must make sure that when we log a directory entry, the | |
3736 | * corresponding inode, after log replay, has a matching link | |
3737 | * count. For example: | |
3738 | * | |
3739 | * touch foo | |
3740 | * mkdir mydir | |
3741 | * sync | |
3742 | * ln foo mydir/bar | |
3743 | * xfs_io -c "fsync" mydir | |
3744 | * <crash> | |
3745 | * <mount fs and log replay> | |
3746 | * | |
3747 | * Would result in a fsync log that when replayed, our file inode | |
3748 | * would have a link count of 1, but we get two directory entries | |
3749 | * pointing to the same inode. After removing one of the names, | |
3750 | * it would not be possible to remove the other name, which | |
3751 | * resulted always in stale file handle errors, and would not be | |
3752 | * possible to rmdir the parent directory, since its i_size could | |
3753 | * never be decremented to the value BTRFS_EMPTY_DIR_SIZE, | |
3754 | * resulting in -ENOTEMPTY errors. | |
3755 | */ | |
086dcbfa | 3756 | if (!ctx->log_new_dentries) { |
086dcbfa FM |
3757 | struct btrfs_key di_key; |
3758 | ||
086dcbfa | 3759 | btrfs_dir_item_key_to_cpu(src, di, &di_key); |
732d591a | 3760 | if (di_key.type != BTRFS_ROOT_ITEM_KEY) |
086dcbfa FM |
3761 | ctx->log_new_dentries = true; |
3762 | } | |
3763 | ||
086dcbfa FM |
3764 | if (batch_size == 0) |
3765 | batch_start = i; | |
3766 | batch_size++; | |
eb10d85e FM |
3767 | } |
3768 | ||
086dcbfa FM |
3769 | if (batch_size > 0) { |
3770 | int ret; | |
3771 | ||
3772 | ret = flush_dir_items_batch(trans, log, src, dst_path, | |
3773 | batch_start, batch_size); | |
3774 | if (ret < 0) | |
3775 | return ret; | |
3776 | } | |
3777 | ||
3778 | return last_found ? 1 : 0; | |
eb10d85e FM |
3779 | } |
3780 | ||
e02119d5 CM |
3781 | /* |
3782 | * log all the items included in the current transaction for a given | |
3783 | * directory. This also creates the range items in the log tree required | |
3784 | * to replay anything deleted before the fsync | |
3785 | */ | |
3786 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, | |
90d04510 | 3787 | struct btrfs_inode *inode, |
e02119d5 | 3788 | struct btrfs_path *path, |
339d0354 | 3789 | struct btrfs_path *dst_path, |
2f2ff0ee | 3790 | struct btrfs_log_ctx *ctx, |
e02119d5 CM |
3791 | u64 min_offset, u64 *last_offset_ret) |
3792 | { | |
3793 | struct btrfs_key min_key; | |
90d04510 | 3794 | struct btrfs_root *root = inode->root; |
e02119d5 | 3795 | struct btrfs_root *log = root->log_root; |
4a500fd1 | 3796 | int err = 0; |
e02119d5 | 3797 | int ret; |
732d591a | 3798 | u64 last_old_dentry_offset = min_offset - 1; |
e02119d5 | 3799 | u64 last_offset = (u64)-1; |
684a5773 | 3800 | u64 ino = btrfs_ino(inode); |
e02119d5 | 3801 | |
33345d01 | 3802 | min_key.objectid = ino; |
339d0354 | 3803 | min_key.type = BTRFS_DIR_INDEX_KEY; |
e02119d5 CM |
3804 | min_key.offset = min_offset; |
3805 | ||
6174d3cb | 3806 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
e02119d5 CM |
3807 | |
3808 | /* | |
3809 | * we didn't find anything from this transaction, see if there | |
3810 | * is anything at all | |
3811 | */ | |
339d0354 FM |
3812 | if (ret != 0 || min_key.objectid != ino || |
3813 | min_key.type != BTRFS_DIR_INDEX_KEY) { | |
33345d01 | 3814 | min_key.objectid = ino; |
339d0354 | 3815 | min_key.type = BTRFS_DIR_INDEX_KEY; |
e02119d5 | 3816 | min_key.offset = (u64)-1; |
b3b4aa74 | 3817 | btrfs_release_path(path); |
e02119d5 CM |
3818 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
3819 | if (ret < 0) { | |
b3b4aa74 | 3820 | btrfs_release_path(path); |
e02119d5 CM |
3821 | return ret; |
3822 | } | |
339d0354 | 3823 | ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); |
e02119d5 CM |
3824 | |
3825 | /* if ret == 0 there are items for this type, | |
3826 | * create a range to tell us the last key of this type. | |
3827 | * otherwise, there are no items in this directory after | |
3828 | * *min_offset, and we create a range to indicate that. | |
3829 | */ | |
3830 | if (ret == 0) { | |
3831 | struct btrfs_key tmp; | |
732d591a | 3832 | |
e02119d5 CM |
3833 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
3834 | path->slots[0]); | |
339d0354 | 3835 | if (tmp.type == BTRFS_DIR_INDEX_KEY) |
732d591a | 3836 | last_old_dentry_offset = tmp.offset; |
e02119d5 CM |
3837 | } |
3838 | goto done; | |
3839 | } | |
3840 | ||
3841 | /* go backward to find any previous key */ | |
339d0354 | 3842 | ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); |
e02119d5 CM |
3843 | if (ret == 0) { |
3844 | struct btrfs_key tmp; | |
a450a4af | 3845 | |
e02119d5 | 3846 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
a450a4af FM |
3847 | /* |
3848 | * The dir index key before the first one we found that needs to | |
3849 | * be logged might be in a previous leaf, and there might be a | |
3850 | * gap between these keys, meaning that we had deletions that | |
3851 | * happened. So the key range item we log (key type | |
3852 | * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the | |
3853 | * previous key's offset plus 1, so that those deletes are replayed. | |
3854 | */ | |
3855 | if (tmp.type == BTRFS_DIR_INDEX_KEY) | |
732d591a | 3856 | last_old_dentry_offset = tmp.offset; |
e02119d5 | 3857 | } |
b3b4aa74 | 3858 | btrfs_release_path(path); |
e02119d5 | 3859 | |
2cc83342 JB |
3860 | /* |
3861 | * Find the first key from this transaction again. See the note for | |
3862 | * log_new_dir_dentries, if we're logging a directory recursively we | |
3863 | * won't be holding its i_mutex, which means we can modify the directory | |
3864 | * while we're logging it. If we remove an entry between our first | |
3865 | * search and this search we'll not find the key again and can just | |
3866 | * bail. | |
3867 | */ | |
bb56f02f | 3868 | search: |
e02119d5 | 3869 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
2cc83342 | 3870 | if (ret != 0) |
e02119d5 | 3871 | goto done; |
e02119d5 CM |
3872 | |
3873 | /* | |
3874 | * we have a block from this transaction, log every item in it | |
3875 | * from our directory | |
3876 | */ | |
d397712b | 3877 | while (1) { |
732d591a FM |
3878 | ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx, |
3879 | &last_old_dentry_offset); | |
eb10d85e FM |
3880 | if (ret != 0) { |
3881 | if (ret < 0) | |
4a500fd1 | 3882 | err = ret; |
eb10d85e | 3883 | goto done; |
e02119d5 | 3884 | } |
eb10d85e | 3885 | path->slots[0] = btrfs_header_nritems(path->nodes[0]); |
e02119d5 CM |
3886 | |
3887 | /* | |
3888 | * look ahead to the next item and see if it is also | |
3889 | * from this directory and from this transaction | |
3890 | */ | |
3891 | ret = btrfs_next_leaf(root, path); | |
80c0b421 LB |
3892 | if (ret) { |
3893 | if (ret == 1) | |
3894 | last_offset = (u64)-1; | |
3895 | else | |
3896 | err = ret; | |
e02119d5 CM |
3897 | goto done; |
3898 | } | |
eb10d85e | 3899 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]); |
339d0354 | 3900 | if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) { |
e02119d5 CM |
3901 | last_offset = (u64)-1; |
3902 | goto done; | |
3903 | } | |
3904 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { | |
a450a4af FM |
3905 | /* |
3906 | * The next leaf was not changed in the current transaction | |
3907 | * and has at least one dir index key. | |
3908 | * We check for the next key because there might have been | |
3909 | * one or more deletions between the last key we logged and | |
3910 | * that next key. So the key range item we log (key type | |
3911 | * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's | |
3912 | * offset minus 1, so that those deletes are replayed. | |
3913 | */ | |
3914 | last_offset = min_key.offset - 1; | |
e02119d5 CM |
3915 | goto done; |
3916 | } | |
eb10d85e FM |
3917 | if (need_resched()) { |
3918 | btrfs_release_path(path); | |
3919 | cond_resched(); | |
3920 | goto search; | |
3921 | } | |
e02119d5 CM |
3922 | } |
3923 | done: | |
b3b4aa74 DS |
3924 | btrfs_release_path(path); |
3925 | btrfs_release_path(dst_path); | |
e02119d5 | 3926 | |
4a500fd1 YZ |
3927 | if (err == 0) { |
3928 | *last_offset_ret = last_offset; | |
3929 | /* | |
732d591a FM |
3930 | * In case the leaf was changed in the current transaction but |
3931 | * all its dir items are from a past transaction, the last item | |
3932 | * in the leaf is a dir item and there's no gap between that last | |
3933 | * dir item and the first one on the next leaf (which did not | |
3934 | * change in the current transaction), then we don't need to log | |
3935 | * a range, last_old_dentry_offset is == to last_offset. | |
4a500fd1 | 3936 | */ |
732d591a FM |
3937 | ASSERT(last_old_dentry_offset <= last_offset); |
3938 | if (last_old_dentry_offset < last_offset) { | |
3939 | ret = insert_dir_log_key(trans, log, path, ino, | |
3940 | last_old_dentry_offset + 1, | |
3941 | last_offset); | |
3942 | if (ret) | |
3943 | err = ret; | |
3944 | } | |
4a500fd1 YZ |
3945 | } |
3946 | return err; | |
e02119d5 CM |
3947 | } |
3948 | ||
193df624 FM |
3949 | /* |
3950 | * If the inode was logged before and it was evicted, then its | |
3951 | * last_dir_index_offset is (u64)-1, so we don't the value of the last index | |
3952 | * key offset. If that's the case, search for it and update the inode. This | |
3953 | * is to avoid lookups in the log tree every time we try to insert a dir index | |
3954 | * key from a leaf changed in the current transaction, and to allow us to always | |
3955 | * do batch insertions of dir index keys. | |
3956 | */ | |
3957 | static int update_last_dir_index_offset(struct btrfs_inode *inode, | |
3958 | struct btrfs_path *path, | |
3959 | const struct btrfs_log_ctx *ctx) | |
3960 | { | |
3961 | const u64 ino = btrfs_ino(inode); | |
3962 | struct btrfs_key key; | |
3963 | int ret; | |
3964 | ||
3965 | lockdep_assert_held(&inode->log_mutex); | |
3966 | ||
3967 | if (inode->last_dir_index_offset != (u64)-1) | |
3968 | return 0; | |
3969 | ||
3970 | if (!ctx->logged_before) { | |
3971 | inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; | |
3972 | return 0; | |
3973 | } | |
3974 | ||
3975 | key.objectid = ino; | |
3976 | key.type = BTRFS_DIR_INDEX_KEY; | |
3977 | key.offset = (u64)-1; | |
3978 | ||
3979 | ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); | |
3980 | /* | |
3981 | * An error happened or we actually have an index key with an offset | |
3982 | * value of (u64)-1. Bail out, we're done. | |
3983 | */ | |
3984 | if (ret <= 0) | |
3985 | goto out; | |
3986 | ||
3987 | ret = 0; | |
3988 | inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; | |
3989 | ||
3990 | /* | |
3991 | * No dir index items, bail out and leave last_dir_index_offset with | |
3992 | * the value right before the first valid index value. | |
3993 | */ | |
3994 | if (path->slots[0] == 0) | |
3995 | goto out; | |
3996 | ||
3997 | /* | |
3998 | * btrfs_search_slot() left us at one slot beyond the slot with the last | |
3999 | * index key, or beyond the last key of the directory that is not an | |
4000 | * index key. If we have an index key before, set last_dir_index_offset | |
4001 | * to its offset value, otherwise leave it with a value right before the | |
4002 | * first valid index value, as it means we have an empty directory. | |
4003 | */ | |
4004 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); | |
4005 | if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY) | |
4006 | inode->last_dir_index_offset = key.offset; | |
4007 | ||
4008 | out: | |
4009 | btrfs_release_path(path); | |
4010 | ||
4011 | return ret; | |
4012 | } | |
4013 | ||
e02119d5 CM |
4014 | /* |
4015 | * logging directories is very similar to logging inodes, We find all the items | |
4016 | * from the current transaction and write them to the log. | |
4017 | * | |
4018 | * The recovery code scans the directory in the subvolume, and if it finds a | |
4019 | * key in the range logged that is not present in the log tree, then it means | |
4020 | * that dir entry was unlinked during the transaction. | |
4021 | * | |
4022 | * In order for that scan to work, we must include one key smaller than | |
4023 | * the smallest logged by this transaction and one key larger than the largest | |
4024 | * key logged by this transaction. | |
4025 | */ | |
4026 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, | |
90d04510 | 4027 | struct btrfs_inode *inode, |
e02119d5 | 4028 | struct btrfs_path *path, |
2f2ff0ee FM |
4029 | struct btrfs_path *dst_path, |
4030 | struct btrfs_log_ctx *ctx) | |
e02119d5 CM |
4031 | { |
4032 | u64 min_key; | |
4033 | u64 max_key; | |
4034 | int ret; | |
e02119d5 | 4035 | |
193df624 FM |
4036 | ret = update_last_dir_index_offset(inode, path, ctx); |
4037 | if (ret) | |
4038 | return ret; | |
4039 | ||
732d591a | 4040 | min_key = BTRFS_DIR_START_INDEX; |
e02119d5 | 4041 | max_key = 0; |
339d0354 | 4042 | ctx->last_dir_item_offset = inode->last_dir_index_offset; |
dc287224 | 4043 | |
d397712b | 4044 | while (1) { |
339d0354 | 4045 | ret = log_dir_items(trans, inode, path, dst_path, |
dbf39ea4 | 4046 | ctx, min_key, &max_key); |
4a500fd1 YZ |
4047 | if (ret) |
4048 | return ret; | |
e02119d5 CM |
4049 | if (max_key == (u64)-1) |
4050 | break; | |
4051 | min_key = max_key + 1; | |
4052 | } | |
4053 | ||
339d0354 FM |
4054 | inode->last_dir_index_offset = ctx->last_dir_item_offset; |
4055 | ||
e02119d5 CM |
4056 | return 0; |
4057 | } | |
4058 | ||
4059 | /* | |
4060 | * a helper function to drop items from the log before we relog an | |
4061 | * inode. max_key_type indicates the highest item type to remove. | |
4062 | * This cannot be run for file data extents because it does not | |
4063 | * free the extents they point to. | |
4064 | */ | |
88e221cd | 4065 | static int drop_inode_items(struct btrfs_trans_handle *trans, |
e02119d5 CM |
4066 | struct btrfs_root *log, |
4067 | struct btrfs_path *path, | |
88e221cd FM |
4068 | struct btrfs_inode *inode, |
4069 | int max_key_type) | |
e02119d5 CM |
4070 | { |
4071 | int ret; | |
4072 | struct btrfs_key key; | |
4073 | struct btrfs_key found_key; | |
18ec90d6 | 4074 | int start_slot; |
e02119d5 | 4075 | |
88e221cd | 4076 | key.objectid = btrfs_ino(inode); |
e02119d5 CM |
4077 | key.type = max_key_type; |
4078 | key.offset = (u64)-1; | |
4079 | ||
d397712b | 4080 | while (1) { |
e02119d5 | 4081 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
3650860b | 4082 | BUG_ON(ret == 0); /* Logic error */ |
4a500fd1 | 4083 | if (ret < 0) |
e02119d5 CM |
4084 | break; |
4085 | ||
4086 | if (path->slots[0] == 0) | |
4087 | break; | |
4088 | ||
4089 | path->slots[0]--; | |
4090 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
4091 | path->slots[0]); | |
4092 | ||
88e221cd | 4093 | if (found_key.objectid != key.objectid) |
e02119d5 CM |
4094 | break; |
4095 | ||
18ec90d6 JB |
4096 | found_key.offset = 0; |
4097 | found_key.type = 0; | |
e3b83361 | 4098 | ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot); |
cbca7d59 FM |
4099 | if (ret < 0) |
4100 | break; | |
18ec90d6 JB |
4101 | |
4102 | ret = btrfs_del_items(trans, log, path, start_slot, | |
4103 | path->slots[0] - start_slot + 1); | |
4104 | /* | |
4105 | * If start slot isn't 0 then we don't need to re-search, we've | |
4106 | * found the last guy with the objectid in this tree. | |
4107 | */ | |
4108 | if (ret || start_slot != 0) | |
65a246c5 | 4109 | break; |
b3b4aa74 | 4110 | btrfs_release_path(path); |
e02119d5 | 4111 | } |
b3b4aa74 | 4112 | btrfs_release_path(path); |
5bdbeb21 JB |
4113 | if (ret > 0) |
4114 | ret = 0; | |
4a500fd1 | 4115 | return ret; |
e02119d5 CM |
4116 | } |
4117 | ||
8a2b3da1 FM |
4118 | static int truncate_inode_items(struct btrfs_trans_handle *trans, |
4119 | struct btrfs_root *log_root, | |
4120 | struct btrfs_inode *inode, | |
4121 | u64 new_size, u32 min_type) | |
4122 | { | |
d9ac19c3 JB |
4123 | struct btrfs_truncate_control control = { |
4124 | .new_size = new_size, | |
487e81d2 | 4125 | .ino = btrfs_ino(inode), |
d9ac19c3 | 4126 | .min_type = min_type, |
5caa490e | 4127 | .skip_ref_updates = true, |
d9ac19c3 | 4128 | }; |
8a2b3da1 | 4129 | |
8697b8f8 | 4130 | return btrfs_truncate_inode_items(trans, log_root, &control); |
8a2b3da1 FM |
4131 | } |
4132 | ||
94edf4ae JB |
4133 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
4134 | struct extent_buffer *leaf, | |
4135 | struct btrfs_inode_item *item, | |
1a4bcf47 FM |
4136 | struct inode *inode, int log_inode_only, |
4137 | u64 logged_isize) | |
94edf4ae | 4138 | { |
0b1c6cca | 4139 | struct btrfs_map_token token; |
77eea05e | 4140 | u64 flags; |
0b1c6cca | 4141 | |
c82f823c | 4142 | btrfs_init_map_token(&token, leaf); |
94edf4ae JB |
4143 | |
4144 | if (log_inode_only) { | |
4145 | /* set the generation to zero so the recover code | |
4146 | * can tell the difference between an logging | |
4147 | * just to say 'this inode exists' and a logging | |
4148 | * to say 'update this inode with these values' | |
4149 | */ | |
cc4c13d5 DS |
4150 | btrfs_set_token_inode_generation(&token, item, 0); |
4151 | btrfs_set_token_inode_size(&token, item, logged_isize); | |
94edf4ae | 4152 | } else { |
cc4c13d5 DS |
4153 | btrfs_set_token_inode_generation(&token, item, |
4154 | BTRFS_I(inode)->generation); | |
4155 | btrfs_set_token_inode_size(&token, item, inode->i_size); | |
0b1c6cca JB |
4156 | } |
4157 | ||
cc4c13d5 DS |
4158 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
4159 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
4160 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
4161 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
4162 | ||
4163 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
4164 | inode->i_atime.tv_sec); | |
4165 | btrfs_set_token_timespec_nsec(&token, &item->atime, | |
4166 | inode->i_atime.tv_nsec); | |
4167 | ||
4168 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
4169 | inode->i_mtime.tv_sec); | |
4170 | btrfs_set_token_timespec_nsec(&token, &item->mtime, | |
4171 | inode->i_mtime.tv_nsec); | |
4172 | ||
4173 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
4174 | inode->i_ctime.tv_sec); | |
4175 | btrfs_set_token_timespec_nsec(&token, &item->ctime, | |
4176 | inode->i_ctime.tv_nsec); | |
4177 | ||
e593e54e FM |
4178 | /* |
4179 | * We do not need to set the nbytes field, in fact during a fast fsync | |
4180 | * its value may not even be correct, since a fast fsync does not wait | |
4181 | * for ordered extent completion, which is where we update nbytes, it | |
4182 | * only waits for writeback to complete. During log replay as we find | |
4183 | * file extent items and replay them, we adjust the nbytes field of the | |
4184 | * inode item in subvolume tree as needed (see overwrite_item()). | |
4185 | */ | |
cc4c13d5 DS |
4186 | |
4187 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
4188 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
4189 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
77eea05e BB |
4190 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
4191 | BTRFS_I(inode)->ro_flags); | |
4192 | btrfs_set_token_inode_flags(&token, item, flags); | |
cc4c13d5 | 4193 | btrfs_set_token_inode_block_group(&token, item, 0); |
94edf4ae JB |
4194 | } |
4195 | ||
a95249b3 JB |
4196 | static int log_inode_item(struct btrfs_trans_handle *trans, |
4197 | struct btrfs_root *log, struct btrfs_path *path, | |
2ac691d8 | 4198 | struct btrfs_inode *inode, bool inode_item_dropped) |
a95249b3 JB |
4199 | { |
4200 | struct btrfs_inode_item *inode_item; | |
a95249b3 JB |
4201 | int ret; |
4202 | ||
2ac691d8 FM |
4203 | /* |
4204 | * If we are doing a fast fsync and the inode was logged before in the | |
4205 | * current transaction, then we know the inode was previously logged and | |
4206 | * it exists in the log tree. For performance reasons, in this case use | |
4207 | * btrfs_search_slot() directly with ins_len set to 0 so that we never | |
4208 | * attempt a write lock on the leaf's parent, which adds unnecessary lock | |
4209 | * contention in case there are concurrent fsyncs for other inodes of the | |
4210 | * same subvolume. Using btrfs_insert_empty_item() when the inode item | |
4211 | * already exists can also result in unnecessarily splitting a leaf. | |
4212 | */ | |
4213 | if (!inode_item_dropped && inode->logged_trans == trans->transid) { | |
4214 | ret = btrfs_search_slot(trans, log, &inode->location, path, 0, 1); | |
4215 | ASSERT(ret <= 0); | |
4216 | if (ret > 0) | |
4217 | ret = -ENOENT; | |
4218 | } else { | |
4219 | /* | |
4220 | * This means it is the first fsync in the current transaction, | |
4221 | * so the inode item is not in the log and we need to insert it. | |
4222 | * We can never get -EEXIST because we are only called for a fast | |
4223 | * fsync and in case an inode eviction happens after the inode was | |
4224 | * logged before in the current transaction, when we load again | |
4225 | * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime | |
4226 | * flags and set ->logged_trans to 0. | |
4227 | */ | |
4228 | ret = btrfs_insert_empty_item(trans, log, path, &inode->location, | |
4229 | sizeof(*inode_item)); | |
4230 | ASSERT(ret != -EEXIST); | |
4231 | } | |
4232 | if (ret) | |
a95249b3 JB |
4233 | return ret; |
4234 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4235 | struct btrfs_inode_item); | |
6d889a3b NB |
4236 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, |
4237 | 0, 0); | |
a95249b3 JB |
4238 | btrfs_release_path(path); |
4239 | return 0; | |
4240 | } | |
4241 | ||
40e046ac | 4242 | static int log_csums(struct btrfs_trans_handle *trans, |
3ebac17c | 4243 | struct btrfs_inode *inode, |
40e046ac FM |
4244 | struct btrfs_root *log_root, |
4245 | struct btrfs_ordered_sum *sums) | |
4246 | { | |
e289f03e FM |
4247 | const u64 lock_end = sums->bytenr + sums->len - 1; |
4248 | struct extent_state *cached_state = NULL; | |
40e046ac FM |
4249 | int ret; |
4250 | ||
3ebac17c FM |
4251 | /* |
4252 | * If this inode was not used for reflink operations in the current | |
4253 | * transaction with new extents, then do the fast path, no need to | |
4254 | * worry about logging checksum items with overlapping ranges. | |
4255 | */ | |
4256 | if (inode->last_reflink_trans < trans->transid) | |
4257 | return btrfs_csum_file_blocks(trans, log_root, sums); | |
4258 | ||
e289f03e FM |
4259 | /* |
4260 | * Serialize logging for checksums. This is to avoid racing with the | |
4261 | * same checksum being logged by another task that is logging another | |
4262 | * file which happens to refer to the same extent as well. Such races | |
4263 | * can leave checksum items in the log with overlapping ranges. | |
4264 | */ | |
570eb97b JB |
4265 | ret = lock_extent(&log_root->log_csum_range, sums->bytenr, lock_end, |
4266 | &cached_state); | |
e289f03e FM |
4267 | if (ret) |
4268 | return ret; | |
40e046ac FM |
4269 | /* |
4270 | * Due to extent cloning, we might have logged a csum item that covers a | |
4271 | * subrange of a cloned extent, and later we can end up logging a csum | |
4272 | * item for a larger subrange of the same extent or the entire range. | |
4273 | * This would leave csum items in the log tree that cover the same range | |
4274 | * and break the searches for checksums in the log tree, resulting in | |
4275 | * some checksums missing in the fs/subvolume tree. So just delete (or | |
4276 | * trim and adjust) any existing csum items in the log for this range. | |
4277 | */ | |
4278 | ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len); | |
e289f03e FM |
4279 | if (!ret) |
4280 | ret = btrfs_csum_file_blocks(trans, log_root, sums); | |
40e046ac | 4281 | |
570eb97b JB |
4282 | unlock_extent(&log_root->log_csum_range, sums->bytenr, lock_end, |
4283 | &cached_state); | |
e289f03e FM |
4284 | |
4285 | return ret; | |
40e046ac FM |
4286 | } |
4287 | ||
31ff1cd2 | 4288 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
44d70e19 | 4289 | struct btrfs_inode *inode, |
31ff1cd2 | 4290 | struct btrfs_path *dst_path, |
0e56315c | 4291 | struct btrfs_path *src_path, |
1a4bcf47 FM |
4292 | int start_slot, int nr, int inode_only, |
4293 | u64 logged_isize) | |
31ff1cd2 | 4294 | { |
44d70e19 | 4295 | struct btrfs_root *log = inode->root->log_root; |
31ff1cd2 | 4296 | struct btrfs_file_extent_item *extent; |
796787c9 | 4297 | struct extent_buffer *src; |
7f30c072 | 4298 | int ret = 0; |
31ff1cd2 CM |
4299 | struct btrfs_key *ins_keys; |
4300 | u32 *ins_sizes; | |
b7ef5f3a | 4301 | struct btrfs_item_batch batch; |
31ff1cd2 CM |
4302 | char *ins_data; |
4303 | int i; | |
7f30c072 | 4304 | int dst_index; |
7f30c072 FM |
4305 | const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM); |
4306 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
d20f7043 | 4307 | |
796787c9 FM |
4308 | /* |
4309 | * To keep lockdep happy and avoid deadlocks, clone the source leaf and | |
4310 | * use the clone. This is because otherwise we would be changing the log | |
4311 | * tree, to insert items from the subvolume tree or insert csum items, | |
4312 | * while holding a read lock on a leaf from the subvolume tree, which | |
4313 | * creates a nasty lock dependency when COWing log tree nodes/leaves: | |
4314 | * | |
4315 | * 1) Modifying the log tree triggers an extent buffer allocation while | |
4316 | * holding a write lock on a parent extent buffer from the log tree. | |
4317 | * Allocating the pages for an extent buffer, or the extent buffer | |
4318 | * struct, can trigger inode eviction and finally the inode eviction | |
4319 | * will trigger a release/remove of a delayed node, which requires | |
4320 | * taking the delayed node's mutex; | |
4321 | * | |
4322 | * 2) Allocating a metadata extent for a log tree can trigger the async | |
4323 | * reclaim thread and make us wait for it to release enough space and | |
4324 | * unblock our reservation ticket. The reclaim thread can start | |
4325 | * flushing delayed items, and that in turn results in the need to | |
4326 | * lock delayed node mutexes and in the need to write lock extent | |
4327 | * buffers of a subvolume tree - all this while holding a write lock | |
4328 | * on the parent extent buffer in the log tree. | |
4329 | * | |
4330 | * So one task in scenario 1) running in parallel with another task in | |
4331 | * scenario 2) could lead to a deadlock, one wanting to lock a delayed | |
4332 | * node mutex while having a read lock on a leaf from the subvolume, | |
4333 | * while the other is holding the delayed node's mutex and wants to | |
4334 | * write lock the same subvolume leaf for flushing delayed items. | |
4335 | */ | |
4336 | src = btrfs_clone_extent_buffer(src_path->nodes[0]); | |
4337 | if (!src) | |
4338 | return -ENOMEM; | |
4339 | ||
4340 | i = src_path->slots[0]; | |
4341 | btrfs_release_path(src_path); | |
4342 | src_path->nodes[0] = src; | |
4343 | src_path->slots[0] = i; | |
4344 | ||
31ff1cd2 CM |
4345 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
4346 | nr * sizeof(u32), GFP_NOFS); | |
2a29edc6 | 4347 | if (!ins_data) |
4348 | return -ENOMEM; | |
4349 | ||
31ff1cd2 CM |
4350 | ins_sizes = (u32 *)ins_data; |
4351 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); | |
b7ef5f3a FM |
4352 | batch.keys = ins_keys; |
4353 | batch.data_sizes = ins_sizes; | |
4354 | batch.total_data_size = 0; | |
7f30c072 | 4355 | batch.nr = 0; |
31ff1cd2 | 4356 | |
7f30c072 | 4357 | dst_index = 0; |
31ff1cd2 | 4358 | for (i = 0; i < nr; i++) { |
7f30c072 FM |
4359 | const int src_slot = start_slot + i; |
4360 | struct btrfs_root *csum_root; | |
5b7ce5e2 FM |
4361 | struct btrfs_ordered_sum *sums; |
4362 | struct btrfs_ordered_sum *sums_next; | |
4363 | LIST_HEAD(ordered_sums); | |
7f30c072 FM |
4364 | u64 disk_bytenr; |
4365 | u64 disk_num_bytes; | |
4366 | u64 extent_offset; | |
4367 | u64 extent_num_bytes; | |
4368 | bool is_old_extent; | |
4369 | ||
4370 | btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot); | |
4371 | ||
4372 | if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY) | |
4373 | goto add_to_batch; | |
4374 | ||
4375 | extent = btrfs_item_ptr(src, src_slot, | |
4376 | struct btrfs_file_extent_item); | |
4377 | ||
4378 | is_old_extent = (btrfs_file_extent_generation(src, extent) < | |
4379 | trans->transid); | |
4380 | ||
4381 | /* | |
4382 | * Don't copy extents from past generations. That would make us | |
4383 | * log a lot more metadata for common cases like doing only a | |
4384 | * few random writes into a file and then fsync it for the first | |
4385 | * time or after the full sync flag is set on the inode. We can | |
4386 | * get leaves full of extent items, most of which are from past | |
4387 | * generations, so we can skip them - as long as the inode has | |
4388 | * not been the target of a reflink operation in this transaction, | |
4389 | * as in that case it might have had file extent items with old | |
4390 | * generations copied into it. We also must always log prealloc | |
4391 | * extents that start at or beyond eof, otherwise we would lose | |
4392 | * them on log replay. | |
4393 | */ | |
4394 | if (is_old_extent && | |
4395 | ins_keys[dst_index].offset < i_size && | |
4396 | inode->last_reflink_trans < trans->transid) | |
4397 | continue; | |
4398 | ||
4399 | if (skip_csum) | |
4400 | goto add_to_batch; | |
4401 | ||
4402 | /* Only regular extents have checksums. */ | |
4403 | if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG) | |
4404 | goto add_to_batch; | |
4405 | ||
4406 | /* | |
4407 | * If it's an extent created in a past transaction, then its | |
4408 | * checksums are already accessible from the committed csum tree, | |
4409 | * no need to log them. | |
4410 | */ | |
4411 | if (is_old_extent) | |
4412 | goto add_to_batch; | |
4413 | ||
4414 | disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent); | |
4415 | /* If it's an explicit hole, there are no checksums. */ | |
4416 | if (disk_bytenr == 0) | |
4417 | goto add_to_batch; | |
4418 | ||
4419 | disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent); | |
4420 | ||
4421 | if (btrfs_file_extent_compression(src, extent)) { | |
4422 | extent_offset = 0; | |
4423 | extent_num_bytes = disk_num_bytes; | |
4424 | } else { | |
4425 | extent_offset = btrfs_file_extent_offset(src, extent); | |
4426 | extent_num_bytes = btrfs_file_extent_num_bytes(src, extent); | |
4427 | } | |
4428 | ||
4429 | csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr); | |
4430 | disk_bytenr += extent_offset; | |
4431 | ret = btrfs_lookup_csums_range(csum_root, disk_bytenr, | |
4432 | disk_bytenr + extent_num_bytes - 1, | |
26ce9114 | 4433 | &ordered_sums, 0, false); |
7f30c072 FM |
4434 | if (ret) |
4435 | goto out; | |
4436 | ||
5b7ce5e2 FM |
4437 | list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) { |
4438 | if (!ret) | |
4439 | ret = log_csums(trans, inode, log, sums); | |
4440 | list_del(&sums->list); | |
4441 | kfree(sums); | |
4442 | } | |
4443 | if (ret) | |
4444 | goto out; | |
4445 | ||
7f30c072 FM |
4446 | add_to_batch: |
4447 | ins_sizes[dst_index] = btrfs_item_size(src, src_slot); | |
4448 | batch.total_data_size += ins_sizes[dst_index]; | |
4449 | batch.nr++; | |
4450 | dst_index++; | |
31ff1cd2 | 4451 | } |
7f30c072 FM |
4452 | |
4453 | /* | |
4454 | * We have a leaf full of old extent items that don't need to be logged, | |
4455 | * so we don't need to do anything. | |
4456 | */ | |
4457 | if (batch.nr == 0) | |
4458 | goto out; | |
4459 | ||
b7ef5f3a | 4460 | ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); |
7f30c072 FM |
4461 | if (ret) |
4462 | goto out; | |
4463 | ||
4464 | dst_index = 0; | |
4465 | for (i = 0; i < nr; i++) { | |
4466 | const int src_slot = start_slot + i; | |
4467 | const int dst_slot = dst_path->slots[0] + dst_index; | |
4468 | struct btrfs_key key; | |
4469 | unsigned long src_offset; | |
4470 | unsigned long dst_offset; | |
4471 | ||
4472 | /* | |
4473 | * We're done, all the remaining items in the source leaf | |
4474 | * correspond to old file extent items. | |
4475 | */ | |
4476 | if (dst_index >= batch.nr) | |
4477 | break; | |
4478 | ||
4479 | btrfs_item_key_to_cpu(src, &key, src_slot); | |
4480 | ||
4481 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
4482 | goto copy_item; | |
31ff1cd2 | 4483 | |
7f30c072 FM |
4484 | extent = btrfs_item_ptr(src, src_slot, |
4485 | struct btrfs_file_extent_item); | |
31ff1cd2 | 4486 | |
7f30c072 FM |
4487 | /* See the comment in the previous loop, same logic. */ |
4488 | if (btrfs_file_extent_generation(src, extent) < trans->transid && | |
4489 | key.offset < i_size && | |
4490 | inode->last_reflink_trans < trans->transid) | |
4491 | continue; | |
4492 | ||
4493 | copy_item: | |
4494 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot); | |
4495 | src_offset = btrfs_item_ptr_offset(src, src_slot); | |
31ff1cd2 | 4496 | |
7f30c072 FM |
4497 | if (key.type == BTRFS_INODE_ITEM_KEY) { |
4498 | struct btrfs_inode_item *inode_item; | |
4499 | ||
4500 | inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot, | |
31ff1cd2 | 4501 | struct btrfs_inode_item); |
94edf4ae | 4502 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
f85b7379 DS |
4503 | &inode->vfs_inode, |
4504 | inode_only == LOG_INODE_EXISTS, | |
1a4bcf47 | 4505 | logged_isize); |
94edf4ae JB |
4506 | } else { |
4507 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, | |
7f30c072 | 4508 | src_offset, ins_sizes[dst_index]); |
31ff1cd2 | 4509 | } |
94edf4ae | 4510 | |
7f30c072 | 4511 | dst_index++; |
31ff1cd2 CM |
4512 | } |
4513 | ||
4514 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); | |
b3b4aa74 | 4515 | btrfs_release_path(dst_path); |
7f30c072 | 4516 | out: |
31ff1cd2 | 4517 | kfree(ins_data); |
d20f7043 | 4518 | |
4a500fd1 | 4519 | return ret; |
31ff1cd2 CM |
4520 | } |
4521 | ||
4f0f586b ST |
4522 | static int extent_cmp(void *priv, const struct list_head *a, |
4523 | const struct list_head *b) | |
5dc562c5 | 4524 | { |
214cc184 | 4525 | const struct extent_map *em1, *em2; |
5dc562c5 JB |
4526 | |
4527 | em1 = list_entry(a, struct extent_map, list); | |
4528 | em2 = list_entry(b, struct extent_map, list); | |
4529 | ||
4530 | if (em1->start < em2->start) | |
4531 | return -1; | |
4532 | else if (em1->start > em2->start) | |
4533 | return 1; | |
4534 | return 0; | |
4535 | } | |
4536 | ||
e7175a69 JB |
4537 | static int log_extent_csums(struct btrfs_trans_handle *trans, |
4538 | struct btrfs_inode *inode, | |
a9ecb653 | 4539 | struct btrfs_root *log_root, |
48778179 FM |
4540 | const struct extent_map *em, |
4541 | struct btrfs_log_ctx *ctx) | |
5dc562c5 | 4542 | { |
48778179 | 4543 | struct btrfs_ordered_extent *ordered; |
fc28b25e | 4544 | struct btrfs_root *csum_root; |
2ab28f32 JB |
4545 | u64 csum_offset; |
4546 | u64 csum_len; | |
48778179 FM |
4547 | u64 mod_start = em->mod_start; |
4548 | u64 mod_len = em->mod_len; | |
8407f553 FM |
4549 | LIST_HEAD(ordered_sums); |
4550 | int ret = 0; | |
0aa4a17d | 4551 | |
e7175a69 JB |
4552 | if (inode->flags & BTRFS_INODE_NODATASUM || |
4553 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
8407f553 | 4554 | em->block_start == EXTENT_MAP_HOLE) |
70c8a91c | 4555 | return 0; |
5dc562c5 | 4556 | |
48778179 FM |
4557 | list_for_each_entry(ordered, &ctx->ordered_extents, log_list) { |
4558 | const u64 ordered_end = ordered->file_offset + ordered->num_bytes; | |
4559 | const u64 mod_end = mod_start + mod_len; | |
4560 | struct btrfs_ordered_sum *sums; | |
4561 | ||
4562 | if (mod_len == 0) | |
4563 | break; | |
4564 | ||
4565 | if (ordered_end <= mod_start) | |
4566 | continue; | |
4567 | if (mod_end <= ordered->file_offset) | |
4568 | break; | |
4569 | ||
4570 | /* | |
4571 | * We are going to copy all the csums on this ordered extent, so | |
4572 | * go ahead and adjust mod_start and mod_len in case this ordered | |
4573 | * extent has already been logged. | |
4574 | */ | |
4575 | if (ordered->file_offset > mod_start) { | |
4576 | if (ordered_end >= mod_end) | |
4577 | mod_len = ordered->file_offset - mod_start; | |
4578 | /* | |
4579 | * If we have this case | |
4580 | * | |
4581 | * |--------- logged extent ---------| | |
4582 | * |----- ordered extent ----| | |
4583 | * | |
4584 | * Just don't mess with mod_start and mod_len, we'll | |
4585 | * just end up logging more csums than we need and it | |
4586 | * will be ok. | |
4587 | */ | |
4588 | } else { | |
4589 | if (ordered_end < mod_end) { | |
4590 | mod_len = mod_end - ordered_end; | |
4591 | mod_start = ordered_end; | |
4592 | } else { | |
4593 | mod_len = 0; | |
4594 | } | |
4595 | } | |
4596 | ||
4597 | /* | |
4598 | * To keep us from looping for the above case of an ordered | |
4599 | * extent that falls inside of the logged extent. | |
4600 | */ | |
4601 | if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags)) | |
4602 | continue; | |
4603 | ||
4604 | list_for_each_entry(sums, &ordered->list, list) { | |
4605 | ret = log_csums(trans, inode, log_root, sums); | |
4606 | if (ret) | |
4607 | return ret; | |
4608 | } | |
4609 | } | |
4610 | ||
4611 | /* We're done, found all csums in the ordered extents. */ | |
4612 | if (mod_len == 0) | |
4613 | return 0; | |
4614 | ||
e7175a69 | 4615 | /* If we're compressed we have to save the entire range of csums. */ |
488111aa FDBM |
4616 | if (em->compress_type) { |
4617 | csum_offset = 0; | |
8407f553 | 4618 | csum_len = max(em->block_len, em->orig_block_len); |
488111aa | 4619 | } else { |
48778179 FM |
4620 | csum_offset = mod_start - em->start; |
4621 | csum_len = mod_len; | |
488111aa | 4622 | } |
2ab28f32 | 4623 | |
70c8a91c | 4624 | /* block start is already adjusted for the file extent offset. */ |
fc28b25e JB |
4625 | csum_root = btrfs_csum_root(trans->fs_info, em->block_start); |
4626 | ret = btrfs_lookup_csums_range(csum_root, | |
70c8a91c JB |
4627 | em->block_start + csum_offset, |
4628 | em->block_start + csum_offset + | |
26ce9114 | 4629 | csum_len - 1, &ordered_sums, 0, false); |
70c8a91c JB |
4630 | if (ret) |
4631 | return ret; | |
5dc562c5 | 4632 | |
70c8a91c JB |
4633 | while (!list_empty(&ordered_sums)) { |
4634 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, | |
4635 | struct btrfs_ordered_sum, | |
4636 | list); | |
4637 | if (!ret) | |
3ebac17c | 4638 | ret = log_csums(trans, inode, log_root, sums); |
70c8a91c JB |
4639 | list_del(&sums->list); |
4640 | kfree(sums); | |
5dc562c5 JB |
4641 | } |
4642 | ||
70c8a91c | 4643 | return ret; |
5dc562c5 JB |
4644 | } |
4645 | ||
8407f553 | 4646 | static int log_one_extent(struct btrfs_trans_handle *trans, |
90d04510 | 4647 | struct btrfs_inode *inode, |
8407f553 FM |
4648 | const struct extent_map *em, |
4649 | struct btrfs_path *path, | |
8407f553 FM |
4650 | struct btrfs_log_ctx *ctx) |
4651 | { | |
5893dfb9 | 4652 | struct btrfs_drop_extents_args drop_args = { 0 }; |
90d04510 | 4653 | struct btrfs_root *log = inode->root->log_root; |
e1f53ed8 | 4654 | struct btrfs_file_extent_item fi = { 0 }; |
8407f553 | 4655 | struct extent_buffer *leaf; |
8407f553 FM |
4656 | struct btrfs_key key; |
4657 | u64 extent_offset = em->start - em->orig_start; | |
4658 | u64 block_len; | |
4659 | int ret; | |
8407f553 | 4660 | |
e1f53ed8 FM |
4661 | btrfs_set_stack_file_extent_generation(&fi, trans->transid); |
4662 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
4663 | btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC); | |
4664 | else | |
4665 | btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG); | |
4666 | ||
4667 | block_len = max(em->block_len, em->orig_block_len); | |
4668 | if (em->compress_type != BTRFS_COMPRESS_NONE) { | |
4669 | btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start); | |
4670 | btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); | |
4671 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { | |
4672 | btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start - | |
4673 | extent_offset); | |
4674 | btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); | |
4675 | } | |
4676 | ||
4677 | btrfs_set_stack_file_extent_offset(&fi, extent_offset); | |
4678 | btrfs_set_stack_file_extent_num_bytes(&fi, em->len); | |
4679 | btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes); | |
4680 | btrfs_set_stack_file_extent_compression(&fi, em->compress_type); | |
4681 | ||
48778179 | 4682 | ret = log_extent_csums(trans, inode, log, em, ctx); |
8407f553 FM |
4683 | if (ret) |
4684 | return ret; | |
4685 | ||
5328b2a7 FM |
4686 | /* |
4687 | * If this is the first time we are logging the inode in the current | |
4688 | * transaction, we can avoid btrfs_drop_extents(), which is expensive | |
4689 | * because it does a deletion search, which always acquires write locks | |
4690 | * for extent buffers at levels 2, 1 and 0. This not only wastes time | |
4691 | * but also adds significant contention in a log tree, since log trees | |
4692 | * are small, with a root at level 2 or 3 at most, due to their short | |
4693 | * life span. | |
4694 | */ | |
0f8ce498 | 4695 | if (ctx->logged_before) { |
5328b2a7 FM |
4696 | drop_args.path = path; |
4697 | drop_args.start = em->start; | |
4698 | drop_args.end = em->start + em->len; | |
4699 | drop_args.replace_extent = true; | |
e1f53ed8 | 4700 | drop_args.extent_item_size = sizeof(fi); |
5328b2a7 FM |
4701 | ret = btrfs_drop_extents(trans, log, inode, &drop_args); |
4702 | if (ret) | |
4703 | return ret; | |
4704 | } | |
8407f553 | 4705 | |
5893dfb9 | 4706 | if (!drop_args.extent_inserted) { |
9d122629 | 4707 | key.objectid = btrfs_ino(inode); |
8407f553 FM |
4708 | key.type = BTRFS_EXTENT_DATA_KEY; |
4709 | key.offset = em->start; | |
4710 | ||
4711 | ret = btrfs_insert_empty_item(trans, log, path, &key, | |
e1f53ed8 | 4712 | sizeof(fi)); |
8407f553 FM |
4713 | if (ret) |
4714 | return ret; | |
4715 | } | |
4716 | leaf = path->nodes[0]; | |
e1f53ed8 FM |
4717 | write_extent_buffer(leaf, &fi, |
4718 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
4719 | sizeof(fi)); | |
8407f553 FM |
4720 | btrfs_mark_buffer_dirty(leaf); |
4721 | ||
4722 | btrfs_release_path(path); | |
4723 | ||
4724 | return ret; | |
4725 | } | |
4726 | ||
31d11b83 FM |
4727 | /* |
4728 | * Log all prealloc extents beyond the inode's i_size to make sure we do not | |
d9947887 | 4729 | * lose them after doing a full/fast fsync and replaying the log. We scan the |
31d11b83 FM |
4730 | * subvolume's root instead of iterating the inode's extent map tree because |
4731 | * otherwise we can log incorrect extent items based on extent map conversion. | |
4732 | * That can happen due to the fact that extent maps are merged when they | |
4733 | * are not in the extent map tree's list of modified extents. | |
4734 | */ | |
4735 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, | |
4736 | struct btrfs_inode *inode, | |
4737 | struct btrfs_path *path) | |
4738 | { | |
4739 | struct btrfs_root *root = inode->root; | |
4740 | struct btrfs_key key; | |
4741 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
4742 | const u64 ino = btrfs_ino(inode); | |
4743 | struct btrfs_path *dst_path = NULL; | |
0e56315c | 4744 | bool dropped_extents = false; |
f135cea3 FM |
4745 | u64 truncate_offset = i_size; |
4746 | struct extent_buffer *leaf; | |
4747 | int slot; | |
31d11b83 FM |
4748 | int ins_nr = 0; |
4749 | int start_slot; | |
4750 | int ret; | |
4751 | ||
4752 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) | |
4753 | return 0; | |
4754 | ||
4755 | key.objectid = ino; | |
4756 | key.type = BTRFS_EXTENT_DATA_KEY; | |
4757 | key.offset = i_size; | |
4758 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
4759 | if (ret < 0) | |
4760 | goto out; | |
4761 | ||
f135cea3 FM |
4762 | /* |
4763 | * We must check if there is a prealloc extent that starts before the | |
4764 | * i_size and crosses the i_size boundary. This is to ensure later we | |
4765 | * truncate down to the end of that extent and not to the i_size, as | |
4766 | * otherwise we end up losing part of the prealloc extent after a log | |
4767 | * replay and with an implicit hole if there is another prealloc extent | |
4768 | * that starts at an offset beyond i_size. | |
4769 | */ | |
4770 | ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); | |
4771 | if (ret < 0) | |
4772 | goto out; | |
4773 | ||
4774 | if (ret == 0) { | |
4775 | struct btrfs_file_extent_item *ei; | |
4776 | ||
4777 | leaf = path->nodes[0]; | |
4778 | slot = path->slots[0]; | |
4779 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4780 | ||
4781 | if (btrfs_file_extent_type(leaf, ei) == | |
4782 | BTRFS_FILE_EXTENT_PREALLOC) { | |
4783 | u64 extent_end; | |
4784 | ||
4785 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4786 | extent_end = key.offset + | |
4787 | btrfs_file_extent_num_bytes(leaf, ei); | |
4788 | ||
4789 | if (extent_end > i_size) | |
4790 | truncate_offset = extent_end; | |
4791 | } | |
4792 | } else { | |
4793 | ret = 0; | |
4794 | } | |
4795 | ||
31d11b83 | 4796 | while (true) { |
f135cea3 FM |
4797 | leaf = path->nodes[0]; |
4798 | slot = path->slots[0]; | |
31d11b83 FM |
4799 | |
4800 | if (slot >= btrfs_header_nritems(leaf)) { | |
4801 | if (ins_nr > 0) { | |
4802 | ret = copy_items(trans, inode, dst_path, path, | |
0e56315c | 4803 | start_slot, ins_nr, 1, 0); |
31d11b83 FM |
4804 | if (ret < 0) |
4805 | goto out; | |
4806 | ins_nr = 0; | |
4807 | } | |
4808 | ret = btrfs_next_leaf(root, path); | |
4809 | if (ret < 0) | |
4810 | goto out; | |
4811 | if (ret > 0) { | |
4812 | ret = 0; | |
4813 | break; | |
4814 | } | |
4815 | continue; | |
4816 | } | |
4817 | ||
4818 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4819 | if (key.objectid > ino) | |
4820 | break; | |
4821 | if (WARN_ON_ONCE(key.objectid < ino) || | |
4822 | key.type < BTRFS_EXTENT_DATA_KEY || | |
4823 | key.offset < i_size) { | |
4824 | path->slots[0]++; | |
4825 | continue; | |
4826 | } | |
0e56315c | 4827 | if (!dropped_extents) { |
31d11b83 FM |
4828 | /* |
4829 | * Avoid logging extent items logged in past fsync calls | |
4830 | * and leading to duplicate keys in the log tree. | |
4831 | */ | |
8a2b3da1 FM |
4832 | ret = truncate_inode_items(trans, root->log_root, inode, |
4833 | truncate_offset, | |
4834 | BTRFS_EXTENT_DATA_KEY); | |
31d11b83 FM |
4835 | if (ret) |
4836 | goto out; | |
0e56315c | 4837 | dropped_extents = true; |
31d11b83 FM |
4838 | } |
4839 | if (ins_nr == 0) | |
4840 | start_slot = slot; | |
4841 | ins_nr++; | |
4842 | path->slots[0]++; | |
4843 | if (!dst_path) { | |
4844 | dst_path = btrfs_alloc_path(); | |
4845 | if (!dst_path) { | |
4846 | ret = -ENOMEM; | |
4847 | goto out; | |
4848 | } | |
4849 | } | |
4850 | } | |
0bc2d3c0 | 4851 | if (ins_nr > 0) |
0e56315c | 4852 | ret = copy_items(trans, inode, dst_path, path, |
31d11b83 | 4853 | start_slot, ins_nr, 1, 0); |
31d11b83 FM |
4854 | out: |
4855 | btrfs_release_path(path); | |
4856 | btrfs_free_path(dst_path); | |
4857 | return ret; | |
4858 | } | |
4859 | ||
5dc562c5 | 4860 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
9d122629 | 4861 | struct btrfs_inode *inode, |
827463c4 | 4862 | struct btrfs_path *path, |
48778179 | 4863 | struct btrfs_log_ctx *ctx) |
5dc562c5 | 4864 | { |
48778179 FM |
4865 | struct btrfs_ordered_extent *ordered; |
4866 | struct btrfs_ordered_extent *tmp; | |
5dc562c5 JB |
4867 | struct extent_map *em, *n; |
4868 | struct list_head extents; | |
9d122629 | 4869 | struct extent_map_tree *tree = &inode->extent_tree; |
5dc562c5 | 4870 | int ret = 0; |
2ab28f32 | 4871 | int num = 0; |
5dc562c5 JB |
4872 | |
4873 | INIT_LIST_HEAD(&extents); | |
4874 | ||
5dc562c5 | 4875 | write_lock(&tree->lock); |
5dc562c5 JB |
4876 | |
4877 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { | |
4878 | list_del_init(&em->list); | |
2ab28f32 JB |
4879 | /* |
4880 | * Just an arbitrary number, this can be really CPU intensive | |
4881 | * once we start getting a lot of extents, and really once we | |
4882 | * have a bunch of extents we just want to commit since it will | |
4883 | * be faster. | |
4884 | */ | |
4885 | if (++num > 32768) { | |
4886 | list_del_init(&tree->modified_extents); | |
4887 | ret = -EFBIG; | |
4888 | goto process; | |
4889 | } | |
4890 | ||
5f96bfb7 | 4891 | if (em->generation < trans->transid) |
5dc562c5 | 4892 | continue; |
8c6c5928 | 4893 | |
31d11b83 FM |
4894 | /* We log prealloc extents beyond eof later. */ |
4895 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && | |
4896 | em->start >= i_size_read(&inode->vfs_inode)) | |
4897 | continue; | |
4898 | ||
ff44c6e3 | 4899 | /* Need a ref to keep it from getting evicted from cache */ |
490b54d6 | 4900 | refcount_inc(&em->refs); |
ff44c6e3 | 4901 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); |
5dc562c5 | 4902 | list_add_tail(&em->list, &extents); |
2ab28f32 | 4903 | num++; |
5dc562c5 JB |
4904 | } |
4905 | ||
4906 | list_sort(NULL, &extents, extent_cmp); | |
2ab28f32 | 4907 | process: |
5dc562c5 JB |
4908 | while (!list_empty(&extents)) { |
4909 | em = list_entry(extents.next, struct extent_map, list); | |
4910 | ||
4911 | list_del_init(&em->list); | |
4912 | ||
4913 | /* | |
4914 | * If we had an error we just need to delete everybody from our | |
4915 | * private list. | |
4916 | */ | |
ff44c6e3 | 4917 | if (ret) { |
201a9038 | 4918 | clear_em_logging(tree, em); |
ff44c6e3 | 4919 | free_extent_map(em); |
5dc562c5 | 4920 | continue; |
ff44c6e3 JB |
4921 | } |
4922 | ||
4923 | write_unlock(&tree->lock); | |
5dc562c5 | 4924 | |
90d04510 | 4925 | ret = log_one_extent(trans, inode, em, path, ctx); |
ff44c6e3 | 4926 | write_lock(&tree->lock); |
201a9038 JB |
4927 | clear_em_logging(tree, em); |
4928 | free_extent_map(em); | |
5dc562c5 | 4929 | } |
ff44c6e3 JB |
4930 | WARN_ON(!list_empty(&extents)); |
4931 | write_unlock(&tree->lock); | |
5dc562c5 | 4932 | |
31d11b83 FM |
4933 | if (!ret) |
4934 | ret = btrfs_log_prealloc_extents(trans, inode, path); | |
48778179 FM |
4935 | if (ret) |
4936 | return ret; | |
31d11b83 | 4937 | |
48778179 FM |
4938 | /* |
4939 | * We have logged all extents successfully, now make sure the commit of | |
4940 | * the current transaction waits for the ordered extents to complete | |
4941 | * before it commits and wipes out the log trees, otherwise we would | |
4942 | * lose data if an ordered extents completes after the transaction | |
4943 | * commits and a power failure happens after the transaction commit. | |
4944 | */ | |
4945 | list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { | |
4946 | list_del_init(&ordered->log_list); | |
4947 | set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags); | |
4948 | ||
4949 | if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { | |
4950 | spin_lock_irq(&inode->ordered_tree.lock); | |
4951 | if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { | |
4952 | set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); | |
4953 | atomic_inc(&trans->transaction->pending_ordered); | |
4954 | } | |
4955 | spin_unlock_irq(&inode->ordered_tree.lock); | |
4956 | } | |
4957 | btrfs_put_ordered_extent(ordered); | |
4958 | } | |
4959 | ||
4960 | return 0; | |
5dc562c5 JB |
4961 | } |
4962 | ||
481b01c0 | 4963 | static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode, |
1a4bcf47 FM |
4964 | struct btrfs_path *path, u64 *size_ret) |
4965 | { | |
4966 | struct btrfs_key key; | |
4967 | int ret; | |
4968 | ||
481b01c0 | 4969 | key.objectid = btrfs_ino(inode); |
1a4bcf47 FM |
4970 | key.type = BTRFS_INODE_ITEM_KEY; |
4971 | key.offset = 0; | |
4972 | ||
4973 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); | |
4974 | if (ret < 0) { | |
4975 | return ret; | |
4976 | } else if (ret > 0) { | |
2f2ff0ee | 4977 | *size_ret = 0; |
1a4bcf47 FM |
4978 | } else { |
4979 | struct btrfs_inode_item *item; | |
4980 | ||
4981 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4982 | struct btrfs_inode_item); | |
4983 | *size_ret = btrfs_inode_size(path->nodes[0], item); | |
bf504110 FM |
4984 | /* |
4985 | * If the in-memory inode's i_size is smaller then the inode | |
4986 | * size stored in the btree, return the inode's i_size, so | |
4987 | * that we get a correct inode size after replaying the log | |
4988 | * when before a power failure we had a shrinking truncate | |
4989 | * followed by addition of a new name (rename / new hard link). | |
4990 | * Otherwise return the inode size from the btree, to avoid | |
4991 | * data loss when replaying a log due to previously doing a | |
4992 | * write that expands the inode's size and logging a new name | |
4993 | * immediately after. | |
4994 | */ | |
4995 | if (*size_ret > inode->vfs_inode.i_size) | |
4996 | *size_ret = inode->vfs_inode.i_size; | |
1a4bcf47 FM |
4997 | } |
4998 | ||
4999 | btrfs_release_path(path); | |
5000 | return 0; | |
5001 | } | |
5002 | ||
36283bf7 FM |
5003 | /* |
5004 | * At the moment we always log all xattrs. This is to figure out at log replay | |
5005 | * time which xattrs must have their deletion replayed. If a xattr is missing | |
5006 | * in the log tree and exists in the fs/subvol tree, we delete it. This is | |
5007 | * because if a xattr is deleted, the inode is fsynced and a power failure | |
5008 | * happens, causing the log to be replayed the next time the fs is mounted, | |
5009 | * we want the xattr to not exist anymore (same behaviour as other filesystems | |
5010 | * with a journal, ext3/4, xfs, f2fs, etc). | |
5011 | */ | |
5012 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, | |
1a93c36a | 5013 | struct btrfs_inode *inode, |
36283bf7 FM |
5014 | struct btrfs_path *path, |
5015 | struct btrfs_path *dst_path) | |
5016 | { | |
90d04510 | 5017 | struct btrfs_root *root = inode->root; |
36283bf7 FM |
5018 | int ret; |
5019 | struct btrfs_key key; | |
1a93c36a | 5020 | const u64 ino = btrfs_ino(inode); |
36283bf7 FM |
5021 | int ins_nr = 0; |
5022 | int start_slot = 0; | |
f2f121ab FM |
5023 | bool found_xattrs = false; |
5024 | ||
5025 | if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags)) | |
5026 | return 0; | |
36283bf7 FM |
5027 | |
5028 | key.objectid = ino; | |
5029 | key.type = BTRFS_XATTR_ITEM_KEY; | |
5030 | key.offset = 0; | |
5031 | ||
5032 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5033 | if (ret < 0) | |
5034 | return ret; | |
5035 | ||
5036 | while (true) { | |
5037 | int slot = path->slots[0]; | |
5038 | struct extent_buffer *leaf = path->nodes[0]; | |
5039 | int nritems = btrfs_header_nritems(leaf); | |
5040 | ||
5041 | if (slot >= nritems) { | |
5042 | if (ins_nr > 0) { | |
1a93c36a | 5043 | ret = copy_items(trans, inode, dst_path, path, |
0e56315c | 5044 | start_slot, ins_nr, 1, 0); |
36283bf7 FM |
5045 | if (ret < 0) |
5046 | return ret; | |
5047 | ins_nr = 0; | |
5048 | } | |
5049 | ret = btrfs_next_leaf(root, path); | |
5050 | if (ret < 0) | |
5051 | return ret; | |
5052 | else if (ret > 0) | |
5053 | break; | |
5054 | continue; | |
5055 | } | |
5056 | ||
5057 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
5058 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) | |
5059 | break; | |
5060 | ||
5061 | if (ins_nr == 0) | |
5062 | start_slot = slot; | |
5063 | ins_nr++; | |
5064 | path->slots[0]++; | |
f2f121ab | 5065 | found_xattrs = true; |
36283bf7 FM |
5066 | cond_resched(); |
5067 | } | |
5068 | if (ins_nr > 0) { | |
1a93c36a | 5069 | ret = copy_items(trans, inode, dst_path, path, |
0e56315c | 5070 | start_slot, ins_nr, 1, 0); |
36283bf7 FM |
5071 | if (ret < 0) |
5072 | return ret; | |
5073 | } | |
5074 | ||
f2f121ab FM |
5075 | if (!found_xattrs) |
5076 | set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags); | |
5077 | ||
36283bf7 FM |
5078 | return 0; |
5079 | } | |
5080 | ||
a89ca6f2 | 5081 | /* |
0e56315c FM |
5082 | * When using the NO_HOLES feature if we punched a hole that causes the |
5083 | * deletion of entire leafs or all the extent items of the first leaf (the one | |
5084 | * that contains the inode item and references) we may end up not processing | |
5085 | * any extents, because there are no leafs with a generation matching the | |
5086 | * current transaction that have extent items for our inode. So we need to find | |
5087 | * if any holes exist and then log them. We also need to log holes after any | |
5088 | * truncate operation that changes the inode's size. | |
a89ca6f2 | 5089 | */ |
0e56315c | 5090 | static int btrfs_log_holes(struct btrfs_trans_handle *trans, |
0e56315c | 5091 | struct btrfs_inode *inode, |
7af59743 | 5092 | struct btrfs_path *path) |
a89ca6f2 | 5093 | { |
90d04510 | 5094 | struct btrfs_root *root = inode->root; |
0b246afa | 5095 | struct btrfs_fs_info *fs_info = root->fs_info; |
a89ca6f2 | 5096 | struct btrfs_key key; |
a0308dd7 NB |
5097 | const u64 ino = btrfs_ino(inode); |
5098 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
7af59743 | 5099 | u64 prev_extent_end = 0; |
0e56315c | 5100 | int ret; |
a89ca6f2 | 5101 | |
0e56315c | 5102 | if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0) |
a89ca6f2 FM |
5103 | return 0; |
5104 | ||
5105 | key.objectid = ino; | |
5106 | key.type = BTRFS_EXTENT_DATA_KEY; | |
7af59743 | 5107 | key.offset = 0; |
a89ca6f2 FM |
5108 | |
5109 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
a89ca6f2 FM |
5110 | if (ret < 0) |
5111 | return ret; | |
5112 | ||
0e56315c | 5113 | while (true) { |
0e56315c | 5114 | struct extent_buffer *leaf = path->nodes[0]; |
a89ca6f2 | 5115 | |
0e56315c FM |
5116 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
5117 | ret = btrfs_next_leaf(root, path); | |
5118 | if (ret < 0) | |
5119 | return ret; | |
5120 | if (ret > 0) { | |
5121 | ret = 0; | |
5122 | break; | |
5123 | } | |
5124 | leaf = path->nodes[0]; | |
5125 | } | |
5126 | ||
5127 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
5128 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
5129 | break; | |
5130 | ||
5131 | /* We have a hole, log it. */ | |
5132 | if (prev_extent_end < key.offset) { | |
7af59743 | 5133 | const u64 hole_len = key.offset - prev_extent_end; |
0e56315c FM |
5134 | |
5135 | /* | |
5136 | * Release the path to avoid deadlocks with other code | |
5137 | * paths that search the root while holding locks on | |
5138 | * leafs from the log root. | |
5139 | */ | |
5140 | btrfs_release_path(path); | |
d1f68ba0 OS |
5141 | ret = btrfs_insert_hole_extent(trans, root->log_root, |
5142 | ino, prev_extent_end, | |
5143 | hole_len); | |
0e56315c FM |
5144 | if (ret < 0) |
5145 | return ret; | |
5146 | ||
5147 | /* | |
5148 | * Search for the same key again in the root. Since it's | |
5149 | * an extent item and we are holding the inode lock, the | |
5150 | * key must still exist. If it doesn't just emit warning | |
5151 | * and return an error to fall back to a transaction | |
5152 | * commit. | |
5153 | */ | |
5154 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5155 | if (ret < 0) | |
5156 | return ret; | |
5157 | if (WARN_ON(ret > 0)) | |
5158 | return -ENOENT; | |
5159 | leaf = path->nodes[0]; | |
5160 | } | |
a89ca6f2 | 5161 | |
7af59743 | 5162 | prev_extent_end = btrfs_file_extent_end(path); |
0e56315c FM |
5163 | path->slots[0]++; |
5164 | cond_resched(); | |
a89ca6f2 | 5165 | } |
a89ca6f2 | 5166 | |
7af59743 | 5167 | if (prev_extent_end < i_size) { |
0e56315c | 5168 | u64 hole_len; |
a89ca6f2 | 5169 | |
0e56315c | 5170 | btrfs_release_path(path); |
7af59743 | 5171 | hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize); |
d1f68ba0 OS |
5172 | ret = btrfs_insert_hole_extent(trans, root->log_root, ino, |
5173 | prev_extent_end, hole_len); | |
0e56315c FM |
5174 | if (ret < 0) |
5175 | return ret; | |
5176 | } | |
5177 | ||
5178 | return 0; | |
a89ca6f2 FM |
5179 | } |
5180 | ||
56f23fdb FM |
5181 | /* |
5182 | * When we are logging a new inode X, check if it doesn't have a reference that | |
5183 | * matches the reference from some other inode Y created in a past transaction | |
5184 | * and that was renamed in the current transaction. If we don't do this, then at | |
5185 | * log replay time we can lose inode Y (and all its files if it's a directory): | |
5186 | * | |
5187 | * mkdir /mnt/x | |
5188 | * echo "hello world" > /mnt/x/foobar | |
5189 | * sync | |
5190 | * mv /mnt/x /mnt/y | |
5191 | * mkdir /mnt/x # or touch /mnt/x | |
5192 | * xfs_io -c fsync /mnt/x | |
5193 | * <power fail> | |
5194 | * mount fs, trigger log replay | |
5195 | * | |
5196 | * After the log replay procedure, we would lose the first directory and all its | |
5197 | * files (file foobar). | |
5198 | * For the case where inode Y is not a directory we simply end up losing it: | |
5199 | * | |
5200 | * echo "123" > /mnt/foo | |
5201 | * sync | |
5202 | * mv /mnt/foo /mnt/bar | |
5203 | * echo "abc" > /mnt/foo | |
5204 | * xfs_io -c fsync /mnt/foo | |
5205 | * <power fail> | |
5206 | * | |
5207 | * We also need this for cases where a snapshot entry is replaced by some other | |
5208 | * entry (file or directory) otherwise we end up with an unreplayable log due to | |
5209 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as | |
5210 | * if it were a regular entry: | |
5211 | * | |
5212 | * mkdir /mnt/x | |
5213 | * btrfs subvolume snapshot /mnt /mnt/x/snap | |
5214 | * btrfs subvolume delete /mnt/x/snap | |
5215 | * rmdir /mnt/x | |
5216 | * mkdir /mnt/x | |
5217 | * fsync /mnt/x or fsync some new file inside it | |
5218 | * <power fail> | |
5219 | * | |
5220 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in | |
5221 | * the same transaction. | |
5222 | */ | |
5223 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, | |
5224 | const int slot, | |
5225 | const struct btrfs_key *key, | |
4791c8f1 | 5226 | struct btrfs_inode *inode, |
a3baaf0d | 5227 | u64 *other_ino, u64 *other_parent) |
56f23fdb FM |
5228 | { |
5229 | int ret; | |
5230 | struct btrfs_path *search_path; | |
5231 | char *name = NULL; | |
5232 | u32 name_len = 0; | |
3212fa14 | 5233 | u32 item_size = btrfs_item_size(eb, slot); |
56f23fdb FM |
5234 | u32 cur_offset = 0; |
5235 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); | |
5236 | ||
5237 | search_path = btrfs_alloc_path(); | |
5238 | if (!search_path) | |
5239 | return -ENOMEM; | |
5240 | search_path->search_commit_root = 1; | |
5241 | search_path->skip_locking = 1; | |
5242 | ||
5243 | while (cur_offset < item_size) { | |
5244 | u64 parent; | |
5245 | u32 this_name_len; | |
5246 | u32 this_len; | |
5247 | unsigned long name_ptr; | |
5248 | struct btrfs_dir_item *di; | |
6db75318 | 5249 | struct fscrypt_str name_str; |
56f23fdb FM |
5250 | |
5251 | if (key->type == BTRFS_INODE_REF_KEY) { | |
5252 | struct btrfs_inode_ref *iref; | |
5253 | ||
5254 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); | |
5255 | parent = key->offset; | |
5256 | this_name_len = btrfs_inode_ref_name_len(eb, iref); | |
5257 | name_ptr = (unsigned long)(iref + 1); | |
5258 | this_len = sizeof(*iref) + this_name_len; | |
5259 | } else { | |
5260 | struct btrfs_inode_extref *extref; | |
5261 | ||
5262 | extref = (struct btrfs_inode_extref *)(ptr + | |
5263 | cur_offset); | |
5264 | parent = btrfs_inode_extref_parent(eb, extref); | |
5265 | this_name_len = btrfs_inode_extref_name_len(eb, extref); | |
5266 | name_ptr = (unsigned long)&extref->name; | |
5267 | this_len = sizeof(*extref) + this_name_len; | |
5268 | } | |
5269 | ||
5270 | if (this_name_len > name_len) { | |
5271 | char *new_name; | |
5272 | ||
5273 | new_name = krealloc(name, this_name_len, GFP_NOFS); | |
5274 | if (!new_name) { | |
5275 | ret = -ENOMEM; | |
5276 | goto out; | |
5277 | } | |
5278 | name_len = this_name_len; | |
5279 | name = new_name; | |
5280 | } | |
5281 | ||
5282 | read_extent_buffer(eb, name, name_ptr, this_name_len); | |
e43eec81 STD |
5283 | |
5284 | name_str.name = name; | |
5285 | name_str.len = this_name_len; | |
4791c8f1 | 5286 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, |
e43eec81 | 5287 | parent, &name_str, 0); |
56f23fdb | 5288 | if (di && !IS_ERR(di)) { |
44f714da FM |
5289 | struct btrfs_key di_key; |
5290 | ||
5291 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], | |
5292 | di, &di_key); | |
5293 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { | |
6b5fc433 FM |
5294 | if (di_key.objectid != key->objectid) { |
5295 | ret = 1; | |
5296 | *other_ino = di_key.objectid; | |
a3baaf0d | 5297 | *other_parent = parent; |
6b5fc433 FM |
5298 | } else { |
5299 | ret = 0; | |
5300 | } | |
44f714da FM |
5301 | } else { |
5302 | ret = -EAGAIN; | |
5303 | } | |
56f23fdb FM |
5304 | goto out; |
5305 | } else if (IS_ERR(di)) { | |
5306 | ret = PTR_ERR(di); | |
5307 | goto out; | |
5308 | } | |
5309 | btrfs_release_path(search_path); | |
5310 | ||
5311 | cur_offset += this_len; | |
5312 | } | |
5313 | ret = 0; | |
5314 | out: | |
5315 | btrfs_free_path(search_path); | |
5316 | kfree(name); | |
5317 | return ret; | |
5318 | } | |
5319 | ||
a3751024 FM |
5320 | /* |
5321 | * Check if we need to log an inode. This is used in contexts where while | |
5322 | * logging an inode we need to log another inode (either that it exists or in | |
5323 | * full mode). This is used instead of btrfs_inode_in_log() because the later | |
5324 | * requires the inode to be in the log and have the log transaction committed, | |
5325 | * while here we do not care if the log transaction was already committed - our | |
5326 | * caller will commit the log later - and we want to avoid logging an inode | |
5327 | * multiple times when multiple tasks have joined the same log transaction. | |
5328 | */ | |
5329 | static bool need_log_inode(const struct btrfs_trans_handle *trans, | |
5330 | const struct btrfs_inode *inode) | |
5331 | { | |
5332 | /* | |
5333 | * If a directory was not modified, no dentries added or removed, we can | |
5334 | * and should avoid logging it. | |
5335 | */ | |
5336 | if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid) | |
5337 | return false; | |
5338 | ||
5339 | /* | |
5340 | * If this inode does not have new/updated/deleted xattrs since the last | |
5341 | * time it was logged and is flagged as logged in the current transaction, | |
5342 | * we can skip logging it. As for new/deleted names, those are updated in | |
5343 | * the log by link/unlink/rename operations. | |
5344 | * In case the inode was logged and then evicted and reloaded, its | |
5345 | * logged_trans will be 0, in which case we have to fully log it since | |
5346 | * logged_trans is a transient field, not persisted. | |
5347 | */ | |
5348 | if (inode->logged_trans == trans->transid && | |
5349 | !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags)) | |
5350 | return false; | |
5351 | ||
5352 | return true; | |
5353 | } | |
5354 | ||
f6d86dbe FM |
5355 | struct btrfs_dir_list { |
5356 | u64 ino; | |
5357 | struct list_head list; | |
5358 | }; | |
5359 | ||
5360 | /* | |
5361 | * Log the inodes of the new dentries of a directory. | |
5362 | * See process_dir_items_leaf() for details about why it is needed. | |
5363 | * This is a recursive operation - if an existing dentry corresponds to a | |
5364 | * directory, that directory's new entries are logged too (same behaviour as | |
5365 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes | |
5366 | * the dentries point to we do not acquire their VFS lock, otherwise lockdep | |
5367 | * complains about the following circular lock dependency / possible deadlock: | |
5368 | * | |
5369 | * CPU0 CPU1 | |
5370 | * ---- ---- | |
5371 | * lock(&type->i_mutex_dir_key#3/2); | |
5372 | * lock(sb_internal#2); | |
5373 | * lock(&type->i_mutex_dir_key#3/2); | |
5374 | * lock(&sb->s_type->i_mutex_key#14); | |
5375 | * | |
5376 | * Where sb_internal is the lock (a counter that works as a lock) acquired by | |
5377 | * sb_start_intwrite() in btrfs_start_transaction(). | |
5378 | * Not acquiring the VFS lock of the inodes is still safe because: | |
5379 | * | |
5380 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible | |
5381 | * that while logging the inode new references (names) are added or removed | |
5382 | * from the inode, leaving the logged inode item with a link count that does | |
5383 | * not match the number of logged inode reference items. This is fine because | |
5384 | * at log replay time we compute the real number of links and correct the | |
5385 | * link count in the inode item (see replay_one_buffer() and | |
5386 | * link_to_fixup_dir()); | |
5387 | * | |
5388 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that | |
5389 | * while logging the inode's items new index items (key type | |
5390 | * BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item | |
5391 | * has a size that doesn't match the sum of the lengths of all the logged | |
5392 | * names - this is ok, not a problem, because at log replay time we set the | |
5393 | * directory's i_size to the correct value (see replay_one_name() and | |
5394 | * do_overwrite_item()). | |
5395 | */ | |
5396 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, | |
5397 | struct btrfs_inode *start_inode, | |
5398 | struct btrfs_log_ctx *ctx) | |
5399 | { | |
5400 | struct btrfs_root *root = start_inode->root; | |
5401 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5402 | struct btrfs_path *path; | |
5403 | LIST_HEAD(dir_list); | |
5404 | struct btrfs_dir_list *dir_elem; | |
5405 | u64 ino = btrfs_ino(start_inode); | |
5406 | int ret = 0; | |
5407 | ||
5408 | /* | |
5409 | * If we are logging a new name, as part of a link or rename operation, | |
5410 | * don't bother logging new dentries, as we just want to log the names | |
5411 | * of an inode and that any new parents exist. | |
5412 | */ | |
5413 | if (ctx->logging_new_name) | |
5414 | return 0; | |
5415 | ||
5416 | path = btrfs_alloc_path(); | |
5417 | if (!path) | |
5418 | return -ENOMEM; | |
5419 | ||
5420 | while (true) { | |
5421 | struct extent_buffer *leaf; | |
5422 | struct btrfs_key min_key; | |
5423 | bool continue_curr_inode = true; | |
5424 | int nritems; | |
5425 | int i; | |
5426 | ||
5427 | min_key.objectid = ino; | |
5428 | min_key.type = BTRFS_DIR_INDEX_KEY; | |
5429 | min_key.offset = 0; | |
5430 | again: | |
5431 | btrfs_release_path(path); | |
5432 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); | |
5433 | if (ret < 0) { | |
5434 | break; | |
5435 | } else if (ret > 0) { | |
5436 | ret = 0; | |
5437 | goto next; | |
5438 | } | |
5439 | ||
5440 | leaf = path->nodes[0]; | |
5441 | nritems = btrfs_header_nritems(leaf); | |
5442 | for (i = path->slots[0]; i < nritems; i++) { | |
5443 | struct btrfs_dir_item *di; | |
5444 | struct btrfs_key di_key; | |
5445 | struct inode *di_inode; | |
5446 | int log_mode = LOG_INODE_EXISTS; | |
5447 | int type; | |
5448 | ||
5449 | btrfs_item_key_to_cpu(leaf, &min_key, i); | |
5450 | if (min_key.objectid != ino || | |
5451 | min_key.type != BTRFS_DIR_INDEX_KEY) { | |
5452 | continue_curr_inode = false; | |
5453 | break; | |
5454 | } | |
5455 | ||
5456 | di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); | |
94a48aef | 5457 | type = btrfs_dir_ftype(leaf, di); |
f6d86dbe FM |
5458 | if (btrfs_dir_transid(leaf, di) < trans->transid) |
5459 | continue; | |
5460 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); | |
5461 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) | |
5462 | continue; | |
5463 | ||
5464 | btrfs_release_path(path); | |
5465 | di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root); | |
5466 | if (IS_ERR(di_inode)) { | |
5467 | ret = PTR_ERR(di_inode); | |
5468 | goto out; | |
5469 | } | |
5470 | ||
5471 | if (!need_log_inode(trans, BTRFS_I(di_inode))) { | |
5472 | btrfs_add_delayed_iput(di_inode); | |
5473 | break; | |
5474 | } | |
5475 | ||
5476 | ctx->log_new_dentries = false; | |
5477 | if (type == BTRFS_FT_DIR) | |
5478 | log_mode = LOG_INODE_ALL; | |
5479 | ret = btrfs_log_inode(trans, BTRFS_I(di_inode), | |
5480 | log_mode, ctx); | |
5481 | btrfs_add_delayed_iput(di_inode); | |
5482 | if (ret) | |
5483 | goto out; | |
5484 | if (ctx->log_new_dentries) { | |
5485 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); | |
5486 | if (!dir_elem) { | |
5487 | ret = -ENOMEM; | |
5488 | goto out; | |
5489 | } | |
5490 | dir_elem->ino = di_key.objectid; | |
5491 | list_add_tail(&dir_elem->list, &dir_list); | |
5492 | } | |
5493 | break; | |
5494 | } | |
5495 | ||
5496 | if (continue_curr_inode && min_key.offset < (u64)-1) { | |
5497 | min_key.offset++; | |
5498 | goto again; | |
5499 | } | |
5500 | ||
5501 | next: | |
5502 | if (list_empty(&dir_list)) | |
5503 | break; | |
5504 | ||
5505 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list); | |
5506 | ino = dir_elem->ino; | |
5507 | list_del(&dir_elem->list); | |
5508 | kfree(dir_elem); | |
5509 | } | |
5510 | out: | |
5511 | btrfs_free_path(path); | |
5512 | if (ret) { | |
5513 | struct btrfs_dir_list *next; | |
5514 | ||
5515 | list_for_each_entry_safe(dir_elem, next, &dir_list, list) | |
5516 | kfree(dir_elem); | |
5517 | } | |
5518 | ||
5519 | return ret; | |
5520 | } | |
5521 | ||
6b5fc433 FM |
5522 | struct btrfs_ino_list { |
5523 | u64 ino; | |
a3baaf0d | 5524 | u64 parent; |
6b5fc433 FM |
5525 | struct list_head list; |
5526 | }; | |
5527 | ||
e09d94c9 FM |
5528 | static void free_conflicting_inodes(struct btrfs_log_ctx *ctx) |
5529 | { | |
5530 | struct btrfs_ino_list *curr; | |
5531 | struct btrfs_ino_list *next; | |
5532 | ||
5533 | list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) { | |
5534 | list_del(&curr->list); | |
5535 | kfree(curr); | |
5536 | } | |
5537 | } | |
5538 | ||
5557a069 FM |
5539 | static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino, |
5540 | struct btrfs_path *path) | |
5541 | { | |
5542 | struct btrfs_key key; | |
5543 | int ret; | |
5544 | ||
5545 | key.objectid = ino; | |
5546 | key.type = BTRFS_INODE_ITEM_KEY; | |
5547 | key.offset = 0; | |
5548 | ||
5549 | path->search_commit_root = 1; | |
5550 | path->skip_locking = 1; | |
5551 | ||
5552 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5553 | if (WARN_ON_ONCE(ret > 0)) { | |
5554 | /* | |
5555 | * We have previously found the inode through the commit root | |
5556 | * so this should not happen. If it does, just error out and | |
5557 | * fallback to a transaction commit. | |
5558 | */ | |
5559 | ret = -ENOENT; | |
5560 | } else if (ret == 0) { | |
5561 | struct btrfs_inode_item *item; | |
5562 | ||
5563 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
5564 | struct btrfs_inode_item); | |
5565 | if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item))) | |
5566 | ret = 1; | |
5567 | } | |
5568 | ||
5569 | btrfs_release_path(path); | |
5570 | path->search_commit_root = 0; | |
5571 | path->skip_locking = 0; | |
5572 | ||
5573 | return ret; | |
5574 | } | |
5575 | ||
e09d94c9 FM |
5576 | static int add_conflicting_inode(struct btrfs_trans_handle *trans, |
5577 | struct btrfs_root *root, | |
5557a069 | 5578 | struct btrfs_path *path, |
e09d94c9 FM |
5579 | u64 ino, u64 parent, |
5580 | struct btrfs_log_ctx *ctx) | |
6b5fc433 FM |
5581 | { |
5582 | struct btrfs_ino_list *ino_elem; | |
e09d94c9 FM |
5583 | struct inode *inode; |
5584 | ||
5585 | /* | |
5586 | * It's rare to have a lot of conflicting inodes, in practice it is not | |
5587 | * common to have more than 1 or 2. We don't want to collect too many, | |
5588 | * as we could end up logging too many inodes (even if only in | |
5589 | * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction | |
5590 | * commits. | |
5591 | */ | |
5592 | if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES) | |
5593 | return BTRFS_LOG_FORCE_COMMIT; | |
5594 | ||
5595 | inode = btrfs_iget(root->fs_info->sb, ino, root); | |
5596 | /* | |
5597 | * If the other inode that had a conflicting dir entry was deleted in | |
5557a069 FM |
5598 | * the current transaction then we either: |
5599 | * | |
5600 | * 1) Log the parent directory (later after adding it to the list) if | |
5601 | * the inode is a directory. This is because it may be a deleted | |
5602 | * subvolume/snapshot or it may be a regular directory that had | |
5603 | * deleted subvolumes/snapshots (or subdirectories that had them), | |
5604 | * and at the moment we can't deal with dropping subvolumes/snapshots | |
5605 | * during log replay. So we just log the parent, which will result in | |
5606 | * a fallback to a transaction commit if we are dealing with those | |
5607 | * cases (last_unlink_trans will match the current transaction); | |
5608 | * | |
5609 | * 2) Do nothing if it's not a directory. During log replay we simply | |
5610 | * unlink the conflicting dentry from the parent directory and then | |
5611 | * add the dentry for our inode. Like this we can avoid logging the | |
5612 | * parent directory (and maybe fallback to a transaction commit in | |
5613 | * case it has a last_unlink_trans == trans->transid, due to moving | |
5614 | * some inode from it to some other directory). | |
e09d94c9 FM |
5615 | */ |
5616 | if (IS_ERR(inode)) { | |
5617 | int ret = PTR_ERR(inode); | |
5618 | ||
5619 | if (ret != -ENOENT) | |
5620 | return ret; | |
5621 | ||
5557a069 FM |
5622 | ret = conflicting_inode_is_dir(root, ino, path); |
5623 | /* Not a directory or we got an error. */ | |
5624 | if (ret <= 0) | |
5625 | return ret; | |
5626 | ||
5627 | /* Conflicting inode is a directory, so we'll log its parent. */ | |
e09d94c9 FM |
5628 | ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); |
5629 | if (!ino_elem) | |
5630 | return -ENOMEM; | |
5631 | ino_elem->ino = ino; | |
5632 | ino_elem->parent = parent; | |
5633 | list_add_tail(&ino_elem->list, &ctx->conflict_inodes); | |
5634 | ctx->num_conflict_inodes++; | |
5635 | ||
5636 | return 0; | |
5637 | } | |
5638 | ||
5639 | /* | |
5640 | * If the inode was already logged skip it - otherwise we can hit an | |
5641 | * infinite loop. Example: | |
5642 | * | |
5643 | * From the commit root (previous transaction) we have the following | |
5644 | * inodes: | |
5645 | * | |
5646 | * inode 257 a directory | |
5647 | * inode 258 with references "zz" and "zz_link" on inode 257 | |
5648 | * inode 259 with reference "a" on inode 257 | |
5649 | * | |
5650 | * And in the current (uncommitted) transaction we have: | |
5651 | * | |
5652 | * inode 257 a directory, unchanged | |
5653 | * inode 258 with references "a" and "a2" on inode 257 | |
5654 | * inode 259 with reference "zz_link" on inode 257 | |
5655 | * inode 261 with reference "zz" on inode 257 | |
5656 | * | |
5657 | * When logging inode 261 the following infinite loop could | |
5658 | * happen if we don't skip already logged inodes: | |
5659 | * | |
5660 | * - we detect inode 258 as a conflicting inode, with inode 261 | |
5661 | * on reference "zz", and log it; | |
5662 | * | |
5663 | * - we detect inode 259 as a conflicting inode, with inode 258 | |
5664 | * on reference "a", and log it; | |
5665 | * | |
5666 | * - we detect inode 258 as a conflicting inode, with inode 259 | |
5667 | * on reference "zz_link", and log it - again! After this we | |
5668 | * repeat the above steps forever. | |
5669 | * | |
5670 | * Here we can use need_log_inode() because we only need to log the | |
5671 | * inode in LOG_INODE_EXISTS mode and rename operations update the log, | |
5672 | * so that the log ends up with the new name and without the old name. | |
5673 | */ | |
5674 | if (!need_log_inode(trans, BTRFS_I(inode))) { | |
5675 | btrfs_add_delayed_iput(inode); | |
5676 | return 0; | |
5677 | } | |
5678 | ||
5679 | btrfs_add_delayed_iput(inode); | |
6b5fc433 FM |
5680 | |
5681 | ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); | |
5682 | if (!ino_elem) | |
5683 | return -ENOMEM; | |
5684 | ino_elem->ino = ino; | |
a3baaf0d | 5685 | ino_elem->parent = parent; |
e09d94c9 FM |
5686 | list_add_tail(&ino_elem->list, &ctx->conflict_inodes); |
5687 | ctx->num_conflict_inodes++; | |
6b5fc433 | 5688 | |
e09d94c9 FM |
5689 | return 0; |
5690 | } | |
6b5fc433 | 5691 | |
e09d94c9 FM |
5692 | static int log_conflicting_inodes(struct btrfs_trans_handle *trans, |
5693 | struct btrfs_root *root, | |
5694 | struct btrfs_log_ctx *ctx) | |
5695 | { | |
5696 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5697 | int ret = 0; | |
6b5fc433 | 5698 | |
e09d94c9 FM |
5699 | /* |
5700 | * Conflicting inodes are logged by the first call to btrfs_log_inode(), | |
5701 | * otherwise we could have unbounded recursion of btrfs_log_inode() | |
5702 | * calls. This check guarantees we can have only 1 level of recursion. | |
5703 | */ | |
5704 | if (ctx->logging_conflict_inodes) | |
5705 | return 0; | |
5706 | ||
5707 | ctx->logging_conflict_inodes = true; | |
5708 | ||
5709 | /* | |
5710 | * New conflicting inodes may be found and added to the list while we | |
5711 | * are logging a conflicting inode, so keep iterating while the list is | |
5712 | * not empty. | |
5713 | */ | |
5714 | while (!list_empty(&ctx->conflict_inodes)) { | |
5715 | struct btrfs_ino_list *curr; | |
5716 | struct inode *inode; | |
5717 | u64 ino; | |
5718 | u64 parent; | |
5719 | ||
5720 | curr = list_first_entry(&ctx->conflict_inodes, | |
5721 | struct btrfs_ino_list, list); | |
5722 | ino = curr->ino; | |
5723 | parent = curr->parent; | |
5724 | list_del(&curr->list); | |
5725 | kfree(curr); | |
6b5fc433 | 5726 | |
0202e83f | 5727 | inode = btrfs_iget(fs_info->sb, ino, root); |
6b5fc433 FM |
5728 | /* |
5729 | * If the other inode that had a conflicting dir entry was | |
a3baaf0d | 5730 | * deleted in the current transaction, we need to log its parent |
e09d94c9 | 5731 | * directory. See the comment at add_conflicting_inode(). |
6b5fc433 FM |
5732 | */ |
5733 | if (IS_ERR(inode)) { | |
5734 | ret = PTR_ERR(inode); | |
e09d94c9 FM |
5735 | if (ret != -ENOENT) |
5736 | break; | |
5737 | ||
5738 | inode = btrfs_iget(fs_info->sb, parent, root); | |
5739 | if (IS_ERR(inode)) { | |
5740 | ret = PTR_ERR(inode); | |
5741 | break; | |
a3baaf0d | 5742 | } |
e09d94c9 FM |
5743 | |
5744 | /* | |
5745 | * Always log the directory, we cannot make this | |
5746 | * conditional on need_log_inode() because the directory | |
5747 | * might have been logged in LOG_INODE_EXISTS mode or | |
5748 | * the dir index of the conflicting inode is not in a | |
5749 | * dir index key range logged for the directory. So we | |
5750 | * must make sure the deletion is recorded. | |
5751 | */ | |
5752 | ret = btrfs_log_inode(trans, BTRFS_I(inode), | |
5753 | LOG_INODE_ALL, ctx); | |
5754 | btrfs_add_delayed_iput(inode); | |
5755 | if (ret) | |
5756 | break; | |
6b5fc433 FM |
5757 | continue; |
5758 | } | |
e09d94c9 | 5759 | |
b5e4ff9d | 5760 | /* |
e09d94c9 FM |
5761 | * Here we can use need_log_inode() because we only need to log |
5762 | * the inode in LOG_INODE_EXISTS mode and rename operations | |
5763 | * update the log, so that the log ends up with the new name and | |
5764 | * without the old name. | |
b5e4ff9d | 5765 | * |
e09d94c9 FM |
5766 | * We did this check at add_conflicting_inode(), but here we do |
5767 | * it again because if some other task logged the inode after | |
5768 | * that, we can avoid doing it again. | |
b5e4ff9d | 5769 | */ |
e09d94c9 | 5770 | if (!need_log_inode(trans, BTRFS_I(inode))) { |
b5e4ff9d FM |
5771 | btrfs_add_delayed_iput(inode); |
5772 | continue; | |
5773 | } | |
e09d94c9 | 5774 | |
6b5fc433 FM |
5775 | /* |
5776 | * We are safe logging the other inode without acquiring its | |
5777 | * lock as long as we log with the LOG_INODE_EXISTS mode. We | |
5778 | * are safe against concurrent renames of the other inode as | |
5779 | * well because during a rename we pin the log and update the | |
5780 | * log with the new name before we unpin it. | |
5781 | */ | |
e09d94c9 | 5782 | ret = btrfs_log_inode(trans, BTRFS_I(inode), LOG_INODE_EXISTS, ctx); |
410f954c | 5783 | btrfs_add_delayed_iput(inode); |
e09d94c9 FM |
5784 | if (ret) |
5785 | break; | |
6b5fc433 FM |
5786 | } |
5787 | ||
e09d94c9 FM |
5788 | ctx->logging_conflict_inodes = false; |
5789 | if (ret) | |
5790 | free_conflicting_inodes(ctx); | |
5791 | ||
6b5fc433 FM |
5792 | return ret; |
5793 | } | |
5794 | ||
da447009 FM |
5795 | static int copy_inode_items_to_log(struct btrfs_trans_handle *trans, |
5796 | struct btrfs_inode *inode, | |
5797 | struct btrfs_key *min_key, | |
5798 | const struct btrfs_key *max_key, | |
5799 | struct btrfs_path *path, | |
5800 | struct btrfs_path *dst_path, | |
5801 | const u64 logged_isize, | |
da447009 FM |
5802 | const int inode_only, |
5803 | struct btrfs_log_ctx *ctx, | |
5804 | bool *need_log_inode_item) | |
5805 | { | |
d9947887 | 5806 | const u64 i_size = i_size_read(&inode->vfs_inode); |
da447009 FM |
5807 | struct btrfs_root *root = inode->root; |
5808 | int ins_start_slot = 0; | |
5809 | int ins_nr = 0; | |
5810 | int ret; | |
5811 | ||
5812 | while (1) { | |
5813 | ret = btrfs_search_forward(root, min_key, path, trans->transid); | |
5814 | if (ret < 0) | |
5815 | return ret; | |
5816 | if (ret > 0) { | |
5817 | ret = 0; | |
5818 | break; | |
5819 | } | |
5820 | again: | |
5821 | /* Note, ins_nr might be > 0 here, cleanup outside the loop */ | |
5822 | if (min_key->objectid != max_key->objectid) | |
5823 | break; | |
5824 | if (min_key->type > max_key->type) | |
5825 | break; | |
5826 | ||
d9947887 | 5827 | if (min_key->type == BTRFS_INODE_ITEM_KEY) { |
da447009 | 5828 | *need_log_inode_item = false; |
d9947887 FM |
5829 | } else if (min_key->type == BTRFS_EXTENT_DATA_KEY && |
5830 | min_key->offset >= i_size) { | |
5831 | /* | |
5832 | * Extents at and beyond eof are logged with | |
5833 | * btrfs_log_prealloc_extents(). | |
5834 | * Only regular files have BTRFS_EXTENT_DATA_KEY keys, | |
5835 | * and no keys greater than that, so bail out. | |
5836 | */ | |
5837 | break; | |
5838 | } else if ((min_key->type == BTRFS_INODE_REF_KEY || | |
5839 | min_key->type == BTRFS_INODE_EXTREF_KEY) && | |
e09d94c9 FM |
5840 | (inode->generation == trans->transid || |
5841 | ctx->logging_conflict_inodes)) { | |
da447009 FM |
5842 | u64 other_ino = 0; |
5843 | u64 other_parent = 0; | |
5844 | ||
5845 | ret = btrfs_check_ref_name_override(path->nodes[0], | |
5846 | path->slots[0], min_key, inode, | |
5847 | &other_ino, &other_parent); | |
5848 | if (ret < 0) { | |
5849 | return ret; | |
289cffcb | 5850 | } else if (ret > 0 && |
da447009 FM |
5851 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { |
5852 | if (ins_nr > 0) { | |
5853 | ins_nr++; | |
5854 | } else { | |
5855 | ins_nr = 1; | |
5856 | ins_start_slot = path->slots[0]; | |
5857 | } | |
5858 | ret = copy_items(trans, inode, dst_path, path, | |
5859 | ins_start_slot, ins_nr, | |
5860 | inode_only, logged_isize); | |
5861 | if (ret < 0) | |
5862 | return ret; | |
5863 | ins_nr = 0; | |
5864 | ||
e09d94c9 | 5865 | btrfs_release_path(path); |
5557a069 | 5866 | ret = add_conflicting_inode(trans, root, path, |
e09d94c9 FM |
5867 | other_ino, |
5868 | other_parent, ctx); | |
da447009 FM |
5869 | if (ret) |
5870 | return ret; | |
da447009 FM |
5871 | goto next_key; |
5872 | } | |
d9947887 FM |
5873 | } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) { |
5874 | /* Skip xattrs, logged later with btrfs_log_all_xattrs() */ | |
da447009 FM |
5875 | if (ins_nr == 0) |
5876 | goto next_slot; | |
5877 | ret = copy_items(trans, inode, dst_path, path, | |
5878 | ins_start_slot, | |
5879 | ins_nr, inode_only, logged_isize); | |
5880 | if (ret < 0) | |
5881 | return ret; | |
5882 | ins_nr = 0; | |
5883 | goto next_slot; | |
5884 | } | |
5885 | ||
5886 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { | |
5887 | ins_nr++; | |
5888 | goto next_slot; | |
5889 | } else if (!ins_nr) { | |
5890 | ins_start_slot = path->slots[0]; | |
5891 | ins_nr = 1; | |
5892 | goto next_slot; | |
5893 | } | |
5894 | ||
5895 | ret = copy_items(trans, inode, dst_path, path, ins_start_slot, | |
5896 | ins_nr, inode_only, logged_isize); | |
5897 | if (ret < 0) | |
5898 | return ret; | |
5899 | ins_nr = 1; | |
5900 | ins_start_slot = path->slots[0]; | |
5901 | next_slot: | |
5902 | path->slots[0]++; | |
5903 | if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) { | |
5904 | btrfs_item_key_to_cpu(path->nodes[0], min_key, | |
5905 | path->slots[0]); | |
5906 | goto again; | |
5907 | } | |
5908 | if (ins_nr) { | |
5909 | ret = copy_items(trans, inode, dst_path, path, | |
5910 | ins_start_slot, ins_nr, inode_only, | |
5911 | logged_isize); | |
5912 | if (ret < 0) | |
5913 | return ret; | |
5914 | ins_nr = 0; | |
5915 | } | |
5916 | btrfs_release_path(path); | |
5917 | next_key: | |
5918 | if (min_key->offset < (u64)-1) { | |
5919 | min_key->offset++; | |
5920 | } else if (min_key->type < max_key->type) { | |
5921 | min_key->type++; | |
5922 | min_key->offset = 0; | |
5923 | } else { | |
5924 | break; | |
5925 | } | |
96acb375 FM |
5926 | |
5927 | /* | |
5928 | * We may process many leaves full of items for our inode, so | |
5929 | * avoid monopolizing a cpu for too long by rescheduling while | |
5930 | * not holding locks on any tree. | |
5931 | */ | |
5932 | cond_resched(); | |
da447009 | 5933 | } |
d9947887 | 5934 | if (ins_nr) { |
da447009 FM |
5935 | ret = copy_items(trans, inode, dst_path, path, ins_start_slot, |
5936 | ins_nr, inode_only, logged_isize); | |
d9947887 FM |
5937 | if (ret) |
5938 | return ret; | |
5939 | } | |
5940 | ||
5941 | if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) { | |
5942 | /* | |
5943 | * Release the path because otherwise we might attempt to double | |
5944 | * lock the same leaf with btrfs_log_prealloc_extents() below. | |
5945 | */ | |
5946 | btrfs_release_path(path); | |
5947 | ret = btrfs_log_prealloc_extents(trans, inode, dst_path); | |
5948 | } | |
da447009 FM |
5949 | |
5950 | return ret; | |
5951 | } | |
5952 | ||
30b80f3c FM |
5953 | static int insert_delayed_items_batch(struct btrfs_trans_handle *trans, |
5954 | struct btrfs_root *log, | |
5955 | struct btrfs_path *path, | |
5956 | const struct btrfs_item_batch *batch, | |
5957 | const struct btrfs_delayed_item *first_item) | |
5958 | { | |
5959 | const struct btrfs_delayed_item *curr = first_item; | |
5960 | int ret; | |
5961 | ||
5962 | ret = btrfs_insert_empty_items(trans, log, path, batch); | |
5963 | if (ret) | |
5964 | return ret; | |
5965 | ||
5966 | for (int i = 0; i < batch->nr; i++) { | |
5967 | char *data_ptr; | |
5968 | ||
5969 | data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char); | |
5970 | write_extent_buffer(path->nodes[0], &curr->data, | |
5971 | (unsigned long)data_ptr, curr->data_len); | |
5972 | curr = list_next_entry(curr, log_list); | |
5973 | path->slots[0]++; | |
5974 | } | |
5975 | ||
5976 | btrfs_release_path(path); | |
5977 | ||
5978 | return 0; | |
5979 | } | |
5980 | ||
5981 | static int log_delayed_insertion_items(struct btrfs_trans_handle *trans, | |
5982 | struct btrfs_inode *inode, | |
5983 | struct btrfs_path *path, | |
5984 | const struct list_head *delayed_ins_list, | |
5985 | struct btrfs_log_ctx *ctx) | |
5986 | { | |
5987 | /* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */ | |
5988 | const int max_batch_size = 195; | |
5989 | const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info); | |
5990 | const u64 ino = btrfs_ino(inode); | |
5991 | struct btrfs_root *log = inode->root->log_root; | |
5992 | struct btrfs_item_batch batch = { | |
5993 | .nr = 0, | |
5994 | .total_data_size = 0, | |
5995 | }; | |
5996 | const struct btrfs_delayed_item *first = NULL; | |
5997 | const struct btrfs_delayed_item *curr; | |
5998 | char *ins_data; | |
5999 | struct btrfs_key *ins_keys; | |
6000 | u32 *ins_sizes; | |
6001 | u64 curr_batch_size = 0; | |
6002 | int batch_idx = 0; | |
6003 | int ret; | |
6004 | ||
6005 | /* We are adding dir index items to the log tree. */ | |
6006 | lockdep_assert_held(&inode->log_mutex); | |
6007 | ||
6008 | /* | |
6009 | * We collect delayed items before copying index keys from the subvolume | |
6010 | * to the log tree. However just after we collected them, they may have | |
6011 | * been flushed (all of them or just some of them), and therefore we | |
6012 | * could have copied them from the subvolume tree to the log tree. | |
6013 | * So find the first delayed item that was not yet logged (they are | |
6014 | * sorted by index number). | |
6015 | */ | |
6016 | list_for_each_entry(curr, delayed_ins_list, log_list) { | |
6017 | if (curr->index > inode->last_dir_index_offset) { | |
6018 | first = curr; | |
6019 | break; | |
6020 | } | |
6021 | } | |
6022 | ||
6023 | /* Empty list or all delayed items were already logged. */ | |
6024 | if (!first) | |
6025 | return 0; | |
6026 | ||
6027 | ins_data = kmalloc(max_batch_size * sizeof(u32) + | |
6028 | max_batch_size * sizeof(struct btrfs_key), GFP_NOFS); | |
6029 | if (!ins_data) | |
6030 | return -ENOMEM; | |
6031 | ins_sizes = (u32 *)ins_data; | |
6032 | batch.data_sizes = ins_sizes; | |
6033 | ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32)); | |
6034 | batch.keys = ins_keys; | |
6035 | ||
6036 | curr = first; | |
6037 | while (!list_entry_is_head(curr, delayed_ins_list, log_list)) { | |
6038 | const u32 curr_size = curr->data_len + sizeof(struct btrfs_item); | |
6039 | ||
6040 | if (curr_batch_size + curr_size > leaf_data_size || | |
6041 | batch.nr == max_batch_size) { | |
6042 | ret = insert_delayed_items_batch(trans, log, path, | |
6043 | &batch, first); | |
6044 | if (ret) | |
6045 | goto out; | |
6046 | batch_idx = 0; | |
6047 | batch.nr = 0; | |
6048 | batch.total_data_size = 0; | |
6049 | curr_batch_size = 0; | |
6050 | first = curr; | |
6051 | } | |
6052 | ||
6053 | ins_sizes[batch_idx] = curr->data_len; | |
6054 | ins_keys[batch_idx].objectid = ino; | |
6055 | ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY; | |
6056 | ins_keys[batch_idx].offset = curr->index; | |
6057 | curr_batch_size += curr_size; | |
6058 | batch.total_data_size += curr->data_len; | |
6059 | batch.nr++; | |
6060 | batch_idx++; | |
6061 | curr = list_next_entry(curr, log_list); | |
6062 | } | |
6063 | ||
6064 | ASSERT(batch.nr >= 1); | |
6065 | ret = insert_delayed_items_batch(trans, log, path, &batch, first); | |
6066 | ||
6067 | curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item, | |
6068 | log_list); | |
6069 | inode->last_dir_index_offset = curr->index; | |
6070 | out: | |
6071 | kfree(ins_data); | |
6072 | ||
6073 | return ret; | |
6074 | } | |
6075 | ||
6076 | static int log_delayed_deletions_full(struct btrfs_trans_handle *trans, | |
6077 | struct btrfs_inode *inode, | |
6078 | struct btrfs_path *path, | |
6079 | const struct list_head *delayed_del_list, | |
6080 | struct btrfs_log_ctx *ctx) | |
6081 | { | |
6082 | const u64 ino = btrfs_ino(inode); | |
6083 | const struct btrfs_delayed_item *curr; | |
6084 | ||
6085 | curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, | |
6086 | log_list); | |
6087 | ||
6088 | while (!list_entry_is_head(curr, delayed_del_list, log_list)) { | |
6089 | u64 first_dir_index = curr->index; | |
6090 | u64 last_dir_index; | |
6091 | const struct btrfs_delayed_item *next; | |
6092 | int ret; | |
6093 | ||
6094 | /* | |
6095 | * Find a range of consecutive dir index items to delete. Like | |
6096 | * this we log a single dir range item spanning several contiguous | |
6097 | * dir items instead of logging one range item per dir index item. | |
6098 | */ | |
6099 | next = list_next_entry(curr, log_list); | |
6100 | while (!list_entry_is_head(next, delayed_del_list, log_list)) { | |
6101 | if (next->index != curr->index + 1) | |
6102 | break; | |
6103 | curr = next; | |
6104 | next = list_next_entry(next, log_list); | |
6105 | } | |
6106 | ||
6107 | last_dir_index = curr->index; | |
6108 | ASSERT(last_dir_index >= first_dir_index); | |
6109 | ||
6110 | ret = insert_dir_log_key(trans, inode->root->log_root, path, | |
6111 | ino, first_dir_index, last_dir_index); | |
6112 | if (ret) | |
6113 | return ret; | |
6114 | curr = list_next_entry(curr, log_list); | |
6115 | } | |
6116 | ||
6117 | return 0; | |
6118 | } | |
6119 | ||
6120 | static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans, | |
6121 | struct btrfs_inode *inode, | |
6122 | struct btrfs_path *path, | |
6123 | struct btrfs_log_ctx *ctx, | |
6124 | const struct list_head *delayed_del_list, | |
6125 | const struct btrfs_delayed_item *first, | |
6126 | const struct btrfs_delayed_item **last_ret) | |
6127 | { | |
6128 | const struct btrfs_delayed_item *next; | |
6129 | struct extent_buffer *leaf = path->nodes[0]; | |
6130 | const int last_slot = btrfs_header_nritems(leaf) - 1; | |
6131 | int slot = path->slots[0] + 1; | |
6132 | const u64 ino = btrfs_ino(inode); | |
6133 | ||
6134 | next = list_next_entry(first, log_list); | |
6135 | ||
6136 | while (slot < last_slot && | |
6137 | !list_entry_is_head(next, delayed_del_list, log_list)) { | |
6138 | struct btrfs_key key; | |
6139 | ||
6140 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
6141 | if (key.objectid != ino || | |
6142 | key.type != BTRFS_DIR_INDEX_KEY || | |
6143 | key.offset != next->index) | |
6144 | break; | |
6145 | ||
6146 | slot++; | |
6147 | *last_ret = next; | |
6148 | next = list_next_entry(next, log_list); | |
6149 | } | |
6150 | ||
6151 | return btrfs_del_items(trans, inode->root->log_root, path, | |
6152 | path->slots[0], slot - path->slots[0]); | |
6153 | } | |
6154 | ||
6155 | static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans, | |
6156 | struct btrfs_inode *inode, | |
6157 | struct btrfs_path *path, | |
6158 | const struct list_head *delayed_del_list, | |
6159 | struct btrfs_log_ctx *ctx) | |
6160 | { | |
6161 | struct btrfs_root *log = inode->root->log_root; | |
6162 | const struct btrfs_delayed_item *curr; | |
6163 | u64 last_range_start; | |
6164 | u64 last_range_end = 0; | |
6165 | struct btrfs_key key; | |
6166 | ||
6167 | key.objectid = btrfs_ino(inode); | |
6168 | key.type = BTRFS_DIR_INDEX_KEY; | |
6169 | curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, | |
6170 | log_list); | |
6171 | ||
6172 | while (!list_entry_is_head(curr, delayed_del_list, log_list)) { | |
6173 | const struct btrfs_delayed_item *last = curr; | |
6174 | u64 first_dir_index = curr->index; | |
6175 | u64 last_dir_index; | |
6176 | bool deleted_items = false; | |
6177 | int ret; | |
6178 | ||
6179 | key.offset = curr->index; | |
6180 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); | |
6181 | if (ret < 0) { | |
6182 | return ret; | |
6183 | } else if (ret == 0) { | |
6184 | ret = batch_delete_dir_index_items(trans, inode, path, ctx, | |
6185 | delayed_del_list, curr, | |
6186 | &last); | |
6187 | if (ret) | |
6188 | return ret; | |
6189 | deleted_items = true; | |
6190 | } | |
6191 | ||
6192 | btrfs_release_path(path); | |
6193 | ||
6194 | /* | |
6195 | * If we deleted items from the leaf, it means we have a range | |
6196 | * item logging their range, so no need to add one or update an | |
6197 | * existing one. Otherwise we have to log a dir range item. | |
6198 | */ | |
6199 | if (deleted_items) | |
6200 | goto next_batch; | |
6201 | ||
6202 | last_dir_index = last->index; | |
6203 | ASSERT(last_dir_index >= first_dir_index); | |
6204 | /* | |
6205 | * If this range starts right after where the previous one ends, | |
6206 | * then we want to reuse the previous range item and change its | |
6207 | * end offset to the end of this range. This is just to minimize | |
6208 | * leaf space usage, by avoiding adding a new range item. | |
6209 | */ | |
6210 | if (last_range_end != 0 && first_dir_index == last_range_end + 1) | |
6211 | first_dir_index = last_range_start; | |
6212 | ||
6213 | ret = insert_dir_log_key(trans, log, path, key.objectid, | |
6214 | first_dir_index, last_dir_index); | |
6215 | if (ret) | |
6216 | return ret; | |
6217 | ||
6218 | last_range_start = first_dir_index; | |
6219 | last_range_end = last_dir_index; | |
6220 | next_batch: | |
6221 | curr = list_next_entry(last, log_list); | |
6222 | } | |
6223 | ||
6224 | return 0; | |
6225 | } | |
6226 | ||
6227 | static int log_delayed_deletion_items(struct btrfs_trans_handle *trans, | |
6228 | struct btrfs_inode *inode, | |
6229 | struct btrfs_path *path, | |
6230 | const struct list_head *delayed_del_list, | |
6231 | struct btrfs_log_ctx *ctx) | |
6232 | { | |
6233 | /* | |
6234 | * We are deleting dir index items from the log tree or adding range | |
6235 | * items to it. | |
6236 | */ | |
6237 | lockdep_assert_held(&inode->log_mutex); | |
6238 | ||
6239 | if (list_empty(delayed_del_list)) | |
6240 | return 0; | |
6241 | ||
6242 | if (ctx->logged_before) | |
6243 | return log_delayed_deletions_incremental(trans, inode, path, | |
6244 | delayed_del_list, ctx); | |
6245 | ||
6246 | return log_delayed_deletions_full(trans, inode, path, delayed_del_list, | |
6247 | ctx); | |
6248 | } | |
6249 | ||
6250 | /* | |
6251 | * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed | |
6252 | * items instead of the subvolume tree. | |
6253 | */ | |
6254 | static int log_new_delayed_dentries(struct btrfs_trans_handle *trans, | |
6255 | struct btrfs_inode *inode, | |
6256 | const struct list_head *delayed_ins_list, | |
6257 | struct btrfs_log_ctx *ctx) | |
6258 | { | |
6259 | const bool orig_log_new_dentries = ctx->log_new_dentries; | |
6260 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
6261 | struct btrfs_delayed_item *item; | |
6262 | int ret = 0; | |
6263 | ||
6264 | /* | |
6265 | * No need for the log mutex, plus to avoid potential deadlocks or | |
6266 | * lockdep annotations due to nesting of delayed inode mutexes and log | |
6267 | * mutexes. | |
6268 | */ | |
6269 | lockdep_assert_not_held(&inode->log_mutex); | |
6270 | ||
6271 | ASSERT(!ctx->logging_new_delayed_dentries); | |
6272 | ctx->logging_new_delayed_dentries = true; | |
6273 | ||
6274 | list_for_each_entry(item, delayed_ins_list, log_list) { | |
6275 | struct btrfs_dir_item *dir_item; | |
6276 | struct inode *di_inode; | |
6277 | struct btrfs_key key; | |
6278 | int log_mode = LOG_INODE_EXISTS; | |
6279 | ||
6280 | dir_item = (struct btrfs_dir_item *)item->data; | |
6281 | btrfs_disk_key_to_cpu(&key, &dir_item->location); | |
6282 | ||
6283 | if (key.type == BTRFS_ROOT_ITEM_KEY) | |
6284 | continue; | |
6285 | ||
6286 | di_inode = btrfs_iget(fs_info->sb, key.objectid, inode->root); | |
6287 | if (IS_ERR(di_inode)) { | |
6288 | ret = PTR_ERR(di_inode); | |
6289 | break; | |
6290 | } | |
6291 | ||
6292 | if (!need_log_inode(trans, BTRFS_I(di_inode))) { | |
6293 | btrfs_add_delayed_iput(di_inode); | |
6294 | continue; | |
6295 | } | |
6296 | ||
94a48aef | 6297 | if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR) |
30b80f3c FM |
6298 | log_mode = LOG_INODE_ALL; |
6299 | ||
6300 | ctx->log_new_dentries = false; | |
6301 | ret = btrfs_log_inode(trans, BTRFS_I(di_inode), log_mode, ctx); | |
6302 | ||
6303 | if (!ret && ctx->log_new_dentries) | |
6304 | ret = log_new_dir_dentries(trans, BTRFS_I(di_inode), ctx); | |
6305 | ||
6306 | btrfs_add_delayed_iput(di_inode); | |
6307 | ||
6308 | if (ret) | |
6309 | break; | |
6310 | } | |
6311 | ||
6312 | ctx->log_new_dentries = orig_log_new_dentries; | |
6313 | ctx->logging_new_delayed_dentries = false; | |
6314 | ||
6315 | return ret; | |
6316 | } | |
6317 | ||
e02119d5 CM |
6318 | /* log a single inode in the tree log. |
6319 | * At least one parent directory for this inode must exist in the tree | |
6320 | * or be logged already. | |
6321 | * | |
6322 | * Any items from this inode changed by the current transaction are copied | |
6323 | * to the log tree. An extra reference is taken on any extents in this | |
6324 | * file, allowing us to avoid a whole pile of corner cases around logging | |
6325 | * blocks that have been removed from the tree. | |
6326 | * | |
6327 | * See LOG_INODE_ALL and related defines for a description of what inode_only | |
6328 | * does. | |
6329 | * | |
6330 | * This handles both files and directories. | |
6331 | */ | |
12fcfd22 | 6332 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
90d04510 | 6333 | struct btrfs_inode *inode, |
49dae1bc | 6334 | int inode_only, |
8407f553 | 6335 | struct btrfs_log_ctx *ctx) |
e02119d5 CM |
6336 | { |
6337 | struct btrfs_path *path; | |
6338 | struct btrfs_path *dst_path; | |
6339 | struct btrfs_key min_key; | |
6340 | struct btrfs_key max_key; | |
90d04510 | 6341 | struct btrfs_root *log = inode->root->log_root; |
65faced5 | 6342 | int ret; |
5dc562c5 | 6343 | bool fast_search = false; |
a59108a7 NB |
6344 | u64 ino = btrfs_ino(inode); |
6345 | struct extent_map_tree *em_tree = &inode->extent_tree; | |
1a4bcf47 | 6346 | u64 logged_isize = 0; |
e4545de5 | 6347 | bool need_log_inode_item = true; |
9a8fca62 | 6348 | bool xattrs_logged = false; |
2ac691d8 | 6349 | bool inode_item_dropped = true; |
30b80f3c FM |
6350 | bool full_dir_logging = false; |
6351 | LIST_HEAD(delayed_ins_list); | |
6352 | LIST_HEAD(delayed_del_list); | |
e02119d5 | 6353 | |
e02119d5 | 6354 | path = btrfs_alloc_path(); |
5df67083 TI |
6355 | if (!path) |
6356 | return -ENOMEM; | |
e02119d5 | 6357 | dst_path = btrfs_alloc_path(); |
5df67083 TI |
6358 | if (!dst_path) { |
6359 | btrfs_free_path(path); | |
6360 | return -ENOMEM; | |
6361 | } | |
e02119d5 | 6362 | |
33345d01 | 6363 | min_key.objectid = ino; |
e02119d5 CM |
6364 | min_key.type = BTRFS_INODE_ITEM_KEY; |
6365 | min_key.offset = 0; | |
6366 | ||
33345d01 | 6367 | max_key.objectid = ino; |
12fcfd22 | 6368 | |
12fcfd22 | 6369 | |
5dc562c5 | 6370 | /* today the code can only do partial logging of directories */ |
a59108a7 | 6371 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
5269b67e | 6372 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
a59108a7 | 6373 | &inode->runtime_flags) && |
781feef7 | 6374 | inode_only >= LOG_INODE_EXISTS)) |
e02119d5 CM |
6375 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
6376 | else | |
6377 | max_key.type = (u8)-1; | |
6378 | max_key.offset = (u64)-1; | |
6379 | ||
30b80f3c FM |
6380 | if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL) |
6381 | full_dir_logging = true; | |
6382 | ||
2c2c452b | 6383 | /* |
30b80f3c FM |
6384 | * If we are logging a directory while we are logging dentries of the |
6385 | * delayed items of some other inode, then we need to flush the delayed | |
6386 | * items of this directory and not log the delayed items directly. This | |
6387 | * is to prevent more than one level of recursion into btrfs_log_inode() | |
6388 | * by having something like this: | |
6389 | * | |
6390 | * $ mkdir -p a/b/c/d/e/f/g/h/... | |
6391 | * $ xfs_io -c "fsync" a | |
6392 | * | |
6393 | * Where all directories in the path did not exist before and are | |
6394 | * created in the current transaction. | |
6395 | * So in such a case we directly log the delayed items of the main | |
6396 | * directory ("a") without flushing them first, while for each of its | |
6397 | * subdirectories we flush their delayed items before logging them. | |
6398 | * This prevents a potential unbounded recursion like this: | |
6399 | * | |
6400 | * btrfs_log_inode() | |
6401 | * log_new_delayed_dentries() | |
6402 | * btrfs_log_inode() | |
6403 | * log_new_delayed_dentries() | |
6404 | * btrfs_log_inode() | |
6405 | * log_new_delayed_dentries() | |
6406 | * (...) | |
6407 | * | |
6408 | * We have thresholds for the maximum number of delayed items to have in | |
6409 | * memory, and once they are hit, the items are flushed asynchronously. | |
6410 | * However the limit is quite high, so lets prevent deep levels of | |
6411 | * recursion to happen by limiting the maximum depth to be 1. | |
2c2c452b | 6412 | */ |
30b80f3c | 6413 | if (full_dir_logging && ctx->logging_new_delayed_dentries) { |
65faced5 FM |
6414 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
6415 | if (ret) | |
f6df27dd | 6416 | goto out; |
16cdcec7 MX |
6417 | } |
6418 | ||
e09d94c9 | 6419 | mutex_lock(&inode->log_mutex); |
e02119d5 | 6420 | |
d0e64a98 FM |
6421 | /* |
6422 | * For symlinks, we must always log their content, which is stored in an | |
6423 | * inline extent, otherwise we could end up with an empty symlink after | |
6424 | * log replay, which is invalid on linux (symlink(2) returns -ENOENT if | |
6425 | * one attempts to create an empty symlink). | |
6426 | * We don't need to worry about flushing delalloc, because when we create | |
6427 | * the inline extent when the symlink is created (we never have delalloc | |
6428 | * for symlinks). | |
6429 | */ | |
6430 | if (S_ISLNK(inode->vfs_inode.i_mode)) | |
6431 | inode_only = LOG_INODE_ALL; | |
6432 | ||
0f8ce498 FM |
6433 | /* |
6434 | * Before logging the inode item, cache the value returned by | |
6435 | * inode_logged(), because after that we have the need to figure out if | |
6436 | * the inode was previously logged in this transaction. | |
6437 | */ | |
6438 | ret = inode_logged(trans, inode, path); | |
65faced5 | 6439 | if (ret < 0) |
0f8ce498 | 6440 | goto out_unlock; |
0f8ce498 | 6441 | ctx->logged_before = (ret == 1); |
65faced5 | 6442 | ret = 0; |
0f8ce498 | 6443 | |
64d6b281 FM |
6444 | /* |
6445 | * This is for cases where logging a directory could result in losing a | |
6446 | * a file after replaying the log. For example, if we move a file from a | |
6447 | * directory A to a directory B, then fsync directory A, we have no way | |
6448 | * to known the file was moved from A to B, so logging just A would | |
6449 | * result in losing the file after a log replay. | |
6450 | */ | |
30b80f3c | 6451 | if (full_dir_logging && inode->last_unlink_trans >= trans->transid) { |
64d6b281 | 6452 | btrfs_set_log_full_commit(trans); |
f31f09f6 | 6453 | ret = BTRFS_LOG_FORCE_COMMIT; |
64d6b281 FM |
6454 | goto out_unlock; |
6455 | } | |
6456 | ||
e02119d5 CM |
6457 | /* |
6458 | * a brute force approach to making sure we get the most uptodate | |
6459 | * copies of everything. | |
6460 | */ | |
a59108a7 | 6461 | if (S_ISDIR(inode->vfs_inode.i_mode)) { |
ab12313a | 6462 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags); |
0f8ce498 FM |
6463 | if (ctx->logged_before) |
6464 | ret = drop_inode_items(trans, log, path, inode, | |
04fc7d51 | 6465 | BTRFS_XATTR_ITEM_KEY); |
e02119d5 | 6466 | } else { |
0f8ce498 | 6467 | if (inode_only == LOG_INODE_EXISTS && ctx->logged_before) { |
1a4bcf47 FM |
6468 | /* |
6469 | * Make sure the new inode item we write to the log has | |
6470 | * the same isize as the current one (if it exists). | |
6471 | * This is necessary to prevent data loss after log | |
6472 | * replay, and also to prevent doing a wrong expanding | |
6473 | * truncate - for e.g. create file, write 4K into offset | |
6474 | * 0, fsync, write 4K into offset 4096, add hard link, | |
6475 | * fsync some other file (to sync log), power fail - if | |
6476 | * we use the inode's current i_size, after log replay | |
6477 | * we get a 8Kb file, with the last 4Kb extent as a hole | |
6478 | * (zeroes), as if an expanding truncate happened, | |
6479 | * instead of getting a file of 4Kb only. | |
6480 | */ | |
65faced5 FM |
6481 | ret = logged_inode_size(log, inode, path, &logged_isize); |
6482 | if (ret) | |
1a4bcf47 FM |
6483 | goto out_unlock; |
6484 | } | |
a742994a | 6485 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
a59108a7 | 6486 | &inode->runtime_flags)) { |
a742994a | 6487 | if (inode_only == LOG_INODE_EXISTS) { |
4f764e51 | 6488 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
0f8ce498 FM |
6489 | if (ctx->logged_before) |
6490 | ret = drop_inode_items(trans, log, path, | |
6491 | inode, max_key.type); | |
a742994a FM |
6492 | } else { |
6493 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
a59108a7 | 6494 | &inode->runtime_flags); |
a742994a | 6495 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
a59108a7 | 6496 | &inode->runtime_flags); |
0f8ce498 | 6497 | if (ctx->logged_before) |
4934a815 FM |
6498 | ret = truncate_inode_items(trans, log, |
6499 | inode, 0, 0); | |
a742994a | 6500 | } |
4f764e51 | 6501 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
a59108a7 | 6502 | &inode->runtime_flags) || |
6cfab851 | 6503 | inode_only == LOG_INODE_EXISTS) { |
4f764e51 | 6504 | if (inode_only == LOG_INODE_ALL) |
183f37fa | 6505 | fast_search = true; |
4f764e51 | 6506 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
0f8ce498 FM |
6507 | if (ctx->logged_before) |
6508 | ret = drop_inode_items(trans, log, path, inode, | |
6509 | max_key.type); | |
a95249b3 JB |
6510 | } else { |
6511 | if (inode_only == LOG_INODE_ALL) | |
6512 | fast_search = true; | |
2ac691d8 | 6513 | inode_item_dropped = false; |
a95249b3 | 6514 | goto log_extents; |
5dc562c5 | 6515 | } |
a95249b3 | 6516 | |
e02119d5 | 6517 | } |
65faced5 | 6518 | if (ret) |
4a500fd1 | 6519 | goto out_unlock; |
e02119d5 | 6520 | |
30b80f3c FM |
6521 | /* |
6522 | * If we are logging a directory in full mode, collect the delayed items | |
6523 | * before iterating the subvolume tree, so that we don't miss any new | |
6524 | * dir index items in case they get flushed while or right after we are | |
6525 | * iterating the subvolume tree. | |
6526 | */ | |
6527 | if (full_dir_logging && !ctx->logging_new_delayed_dentries) | |
6528 | btrfs_log_get_delayed_items(inode, &delayed_ins_list, | |
6529 | &delayed_del_list); | |
6530 | ||
65faced5 | 6531 | ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key, |
da447009 | 6532 | path, dst_path, logged_isize, |
e09d94c9 | 6533 | inode_only, ctx, |
7af59743 | 6534 | &need_log_inode_item); |
65faced5 | 6535 | if (ret) |
da447009 | 6536 | goto out_unlock; |
5dc562c5 | 6537 | |
36283bf7 FM |
6538 | btrfs_release_path(path); |
6539 | btrfs_release_path(dst_path); | |
65faced5 FM |
6540 | ret = btrfs_log_all_xattrs(trans, inode, path, dst_path); |
6541 | if (ret) | |
36283bf7 | 6542 | goto out_unlock; |
9a8fca62 | 6543 | xattrs_logged = true; |
a89ca6f2 FM |
6544 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { |
6545 | btrfs_release_path(path); | |
6546 | btrfs_release_path(dst_path); | |
65faced5 FM |
6547 | ret = btrfs_log_holes(trans, inode, path); |
6548 | if (ret) | |
a89ca6f2 FM |
6549 | goto out_unlock; |
6550 | } | |
a95249b3 | 6551 | log_extents: |
f3b15ccd JB |
6552 | btrfs_release_path(path); |
6553 | btrfs_release_path(dst_path); | |
e4545de5 | 6554 | if (need_log_inode_item) { |
65faced5 FM |
6555 | ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped); |
6556 | if (ret) | |
b590b839 FM |
6557 | goto out_unlock; |
6558 | /* | |
6559 | * If we are doing a fast fsync and the inode was logged before | |
6560 | * in this transaction, we don't need to log the xattrs because | |
6561 | * they were logged before. If xattrs were added, changed or | |
6562 | * deleted since the last time we logged the inode, then we have | |
6563 | * already logged them because the inode had the runtime flag | |
6564 | * BTRFS_INODE_COPY_EVERYTHING set. | |
6565 | */ | |
6566 | if (!xattrs_logged && inode->logged_trans < trans->transid) { | |
65faced5 FM |
6567 | ret = btrfs_log_all_xattrs(trans, inode, path, dst_path); |
6568 | if (ret) | |
b590b839 | 6569 | goto out_unlock; |
9a8fca62 FM |
6570 | btrfs_release_path(path); |
6571 | } | |
e4545de5 | 6572 | } |
5dc562c5 | 6573 | if (fast_search) { |
90d04510 | 6574 | ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx); |
65faced5 | 6575 | if (ret) |
5dc562c5 | 6576 | goto out_unlock; |
d006a048 | 6577 | } else if (inode_only == LOG_INODE_ALL) { |
06d3d22b LB |
6578 | struct extent_map *em, *n; |
6579 | ||
49dae1bc | 6580 | write_lock(&em_tree->lock); |
48778179 FM |
6581 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, list) |
6582 | list_del_init(&em->list); | |
49dae1bc | 6583 | write_unlock(&em_tree->lock); |
5dc562c5 JB |
6584 | } |
6585 | ||
30b80f3c | 6586 | if (full_dir_logging) { |
90d04510 | 6587 | ret = log_directory_changes(trans, inode, path, dst_path, ctx); |
65faced5 | 6588 | if (ret) |
4a500fd1 | 6589 | goto out_unlock; |
30b80f3c FM |
6590 | ret = log_delayed_insertion_items(trans, inode, path, |
6591 | &delayed_ins_list, ctx); | |
6592 | if (ret) | |
6593 | goto out_unlock; | |
6594 | ret = log_delayed_deletion_items(trans, inode, path, | |
6595 | &delayed_del_list, ctx); | |
6596 | if (ret) | |
6597 | goto out_unlock; | |
e02119d5 | 6598 | } |
49dae1bc | 6599 | |
130341be FM |
6600 | spin_lock(&inode->lock); |
6601 | inode->logged_trans = trans->transid; | |
d1d832a0 | 6602 | /* |
130341be FM |
6603 | * Don't update last_log_commit if we logged that an inode exists. |
6604 | * We do this for three reasons: | |
6605 | * | |
6606 | * 1) We might have had buffered writes to this inode that were | |
6607 | * flushed and had their ordered extents completed in this | |
6608 | * transaction, but we did not previously log the inode with | |
6609 | * LOG_INODE_ALL. Later the inode was evicted and after that | |
6610 | * it was loaded again and this LOG_INODE_EXISTS log operation | |
6611 | * happened. We must make sure that if an explicit fsync against | |
6612 | * the inode is performed later, it logs the new extents, an | |
6613 | * updated inode item, etc, and syncs the log. The same logic | |
6614 | * applies to direct IO writes instead of buffered writes. | |
6615 | * | |
6616 | * 2) When we log the inode with LOG_INODE_EXISTS, its inode item | |
6617 | * is logged with an i_size of 0 or whatever value was logged | |
6618 | * before. If later the i_size of the inode is increased by a | |
6619 | * truncate operation, the log is synced through an fsync of | |
6620 | * some other inode and then finally an explicit fsync against | |
6621 | * this inode is made, we must make sure this fsync logs the | |
6622 | * inode with the new i_size, the hole between old i_size and | |
6623 | * the new i_size, and syncs the log. | |
6624 | * | |
6625 | * 3) If we are logging that an ancestor inode exists as part of | |
6626 | * logging a new name from a link or rename operation, don't update | |
6627 | * its last_log_commit - otherwise if an explicit fsync is made | |
6628 | * against an ancestor, the fsync considers the inode in the log | |
6629 | * and doesn't sync the log, resulting in the ancestor missing after | |
6630 | * a power failure unless the log was synced as part of an fsync | |
6631 | * against any other unrelated inode. | |
d1d832a0 | 6632 | */ |
130341be FM |
6633 | if (inode_only != LOG_INODE_EXISTS) |
6634 | inode->last_log_commit = inode->last_sub_trans; | |
6635 | spin_unlock(&inode->lock); | |
23e3337f FM |
6636 | |
6637 | /* | |
6638 | * Reset the last_reflink_trans so that the next fsync does not need to | |
6639 | * go through the slower path when logging extents and their checksums. | |
6640 | */ | |
6641 | if (inode_only == LOG_INODE_ALL) | |
6642 | inode->last_reflink_trans = 0; | |
6643 | ||
4a500fd1 | 6644 | out_unlock: |
a59108a7 | 6645 | mutex_unlock(&inode->log_mutex); |
f6df27dd | 6646 | out: |
e02119d5 CM |
6647 | btrfs_free_path(path); |
6648 | btrfs_free_path(dst_path); | |
0f8ce498 | 6649 | |
e09d94c9 FM |
6650 | if (ret) |
6651 | free_conflicting_inodes(ctx); | |
6652 | else | |
6653 | ret = log_conflicting_inodes(trans, inode->root, ctx); | |
0f8ce498 | 6654 | |
30b80f3c FM |
6655 | if (full_dir_logging && !ctx->logging_new_delayed_dentries) { |
6656 | if (!ret) | |
6657 | ret = log_new_delayed_dentries(trans, inode, | |
6658 | &delayed_ins_list, ctx); | |
6659 | ||
6660 | btrfs_log_put_delayed_items(inode, &delayed_ins_list, | |
6661 | &delayed_del_list); | |
6662 | } | |
6663 | ||
65faced5 | 6664 | return ret; |
e02119d5 CM |
6665 | } |
6666 | ||
18aa0922 | 6667 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, |
d0a0b78d | 6668 | struct btrfs_inode *inode, |
18aa0922 FM |
6669 | struct btrfs_log_ctx *ctx) |
6670 | { | |
3ffbd68c | 6671 | struct btrfs_fs_info *fs_info = trans->fs_info; |
18aa0922 FM |
6672 | int ret; |
6673 | struct btrfs_path *path; | |
6674 | struct btrfs_key key; | |
d0a0b78d NB |
6675 | struct btrfs_root *root = inode->root; |
6676 | const u64 ino = btrfs_ino(inode); | |
18aa0922 FM |
6677 | |
6678 | path = btrfs_alloc_path(); | |
6679 | if (!path) | |
6680 | return -ENOMEM; | |
6681 | path->skip_locking = 1; | |
6682 | path->search_commit_root = 1; | |
6683 | ||
6684 | key.objectid = ino; | |
6685 | key.type = BTRFS_INODE_REF_KEY; | |
6686 | key.offset = 0; | |
6687 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6688 | if (ret < 0) | |
6689 | goto out; | |
6690 | ||
6691 | while (true) { | |
6692 | struct extent_buffer *leaf = path->nodes[0]; | |
6693 | int slot = path->slots[0]; | |
6694 | u32 cur_offset = 0; | |
6695 | u32 item_size; | |
6696 | unsigned long ptr; | |
6697 | ||
6698 | if (slot >= btrfs_header_nritems(leaf)) { | |
6699 | ret = btrfs_next_leaf(root, path); | |
6700 | if (ret < 0) | |
6701 | goto out; | |
6702 | else if (ret > 0) | |
6703 | break; | |
6704 | continue; | |
6705 | } | |
6706 | ||
6707 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
6708 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ | |
6709 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) | |
6710 | break; | |
6711 | ||
3212fa14 | 6712 | item_size = btrfs_item_size(leaf, slot); |
18aa0922 FM |
6713 | ptr = btrfs_item_ptr_offset(leaf, slot); |
6714 | while (cur_offset < item_size) { | |
6715 | struct btrfs_key inode_key; | |
6716 | struct inode *dir_inode; | |
6717 | ||
6718 | inode_key.type = BTRFS_INODE_ITEM_KEY; | |
6719 | inode_key.offset = 0; | |
6720 | ||
6721 | if (key.type == BTRFS_INODE_EXTREF_KEY) { | |
6722 | struct btrfs_inode_extref *extref; | |
6723 | ||
6724 | extref = (struct btrfs_inode_extref *) | |
6725 | (ptr + cur_offset); | |
6726 | inode_key.objectid = btrfs_inode_extref_parent( | |
6727 | leaf, extref); | |
6728 | cur_offset += sizeof(*extref); | |
6729 | cur_offset += btrfs_inode_extref_name_len(leaf, | |
6730 | extref); | |
6731 | } else { | |
6732 | inode_key.objectid = key.offset; | |
6733 | cur_offset = item_size; | |
6734 | } | |
6735 | ||
0202e83f DS |
6736 | dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid, |
6737 | root); | |
0f375eed FM |
6738 | /* |
6739 | * If the parent inode was deleted, return an error to | |
6740 | * fallback to a transaction commit. This is to prevent | |
6741 | * getting an inode that was moved from one parent A to | |
6742 | * a parent B, got its former parent A deleted and then | |
6743 | * it got fsync'ed, from existing at both parents after | |
6744 | * a log replay (and the old parent still existing). | |
6745 | * Example: | |
6746 | * | |
6747 | * mkdir /mnt/A | |
6748 | * mkdir /mnt/B | |
6749 | * touch /mnt/B/bar | |
6750 | * sync | |
6751 | * mv /mnt/B/bar /mnt/A/bar | |
6752 | * mv -T /mnt/A /mnt/B | |
6753 | * fsync /mnt/B/bar | |
6754 | * <power fail> | |
6755 | * | |
6756 | * If we ignore the old parent B which got deleted, | |
6757 | * after a log replay we would have file bar linked | |
6758 | * at both parents and the old parent B would still | |
6759 | * exist. | |
6760 | */ | |
6761 | if (IS_ERR(dir_inode)) { | |
6762 | ret = PTR_ERR(dir_inode); | |
6763 | goto out; | |
6764 | } | |
18aa0922 | 6765 | |
3e6a86a1 FM |
6766 | if (!need_log_inode(trans, BTRFS_I(dir_inode))) { |
6767 | btrfs_add_delayed_iput(dir_inode); | |
6768 | continue; | |
6769 | } | |
6770 | ||
289cffcb | 6771 | ctx->log_new_dentries = false; |
90d04510 | 6772 | ret = btrfs_log_inode(trans, BTRFS_I(dir_inode), |
48778179 | 6773 | LOG_INODE_ALL, ctx); |
289cffcb | 6774 | if (!ret && ctx->log_new_dentries) |
8786a6d7 | 6775 | ret = log_new_dir_dentries(trans, |
f85b7379 | 6776 | BTRFS_I(dir_inode), ctx); |
410f954c | 6777 | btrfs_add_delayed_iput(dir_inode); |
18aa0922 FM |
6778 | if (ret) |
6779 | goto out; | |
6780 | } | |
6781 | path->slots[0]++; | |
6782 | } | |
6783 | ret = 0; | |
6784 | out: | |
6785 | btrfs_free_path(path); | |
6786 | return ret; | |
6787 | } | |
6788 | ||
b8aa330d FM |
6789 | static int log_new_ancestors(struct btrfs_trans_handle *trans, |
6790 | struct btrfs_root *root, | |
6791 | struct btrfs_path *path, | |
6792 | struct btrfs_log_ctx *ctx) | |
6793 | { | |
6794 | struct btrfs_key found_key; | |
6795 | ||
6796 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); | |
6797 | ||
6798 | while (true) { | |
6799 | struct btrfs_fs_info *fs_info = root->fs_info; | |
b8aa330d FM |
6800 | struct extent_buffer *leaf = path->nodes[0]; |
6801 | int slot = path->slots[0]; | |
6802 | struct btrfs_key search_key; | |
6803 | struct inode *inode; | |
0202e83f | 6804 | u64 ino; |
b8aa330d FM |
6805 | int ret = 0; |
6806 | ||
6807 | btrfs_release_path(path); | |
6808 | ||
0202e83f DS |
6809 | ino = found_key.offset; |
6810 | ||
b8aa330d FM |
6811 | search_key.objectid = found_key.offset; |
6812 | search_key.type = BTRFS_INODE_ITEM_KEY; | |
6813 | search_key.offset = 0; | |
0202e83f | 6814 | inode = btrfs_iget(fs_info->sb, ino, root); |
b8aa330d FM |
6815 | if (IS_ERR(inode)) |
6816 | return PTR_ERR(inode); | |
6817 | ||
ab12313a FM |
6818 | if (BTRFS_I(inode)->generation >= trans->transid && |
6819 | need_log_inode(trans, BTRFS_I(inode))) | |
90d04510 | 6820 | ret = btrfs_log_inode(trans, BTRFS_I(inode), |
48778179 | 6821 | LOG_INODE_EXISTS, ctx); |
410f954c | 6822 | btrfs_add_delayed_iput(inode); |
b8aa330d FM |
6823 | if (ret) |
6824 | return ret; | |
6825 | ||
6826 | if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID) | |
6827 | break; | |
6828 | ||
6829 | search_key.type = BTRFS_INODE_REF_KEY; | |
6830 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
6831 | if (ret < 0) | |
6832 | return ret; | |
6833 | ||
6834 | leaf = path->nodes[0]; | |
6835 | slot = path->slots[0]; | |
6836 | if (slot >= btrfs_header_nritems(leaf)) { | |
6837 | ret = btrfs_next_leaf(root, path); | |
6838 | if (ret < 0) | |
6839 | return ret; | |
6840 | else if (ret > 0) | |
6841 | return -ENOENT; | |
6842 | leaf = path->nodes[0]; | |
6843 | slot = path->slots[0]; | |
6844 | } | |
6845 | ||
6846 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
6847 | if (found_key.objectid != search_key.objectid || | |
6848 | found_key.type != BTRFS_INODE_REF_KEY) | |
6849 | return -ENOENT; | |
6850 | } | |
6851 | return 0; | |
6852 | } | |
6853 | ||
6854 | static int log_new_ancestors_fast(struct btrfs_trans_handle *trans, | |
6855 | struct btrfs_inode *inode, | |
6856 | struct dentry *parent, | |
6857 | struct btrfs_log_ctx *ctx) | |
6858 | { | |
6859 | struct btrfs_root *root = inode->root; | |
b8aa330d FM |
6860 | struct dentry *old_parent = NULL; |
6861 | struct super_block *sb = inode->vfs_inode.i_sb; | |
6862 | int ret = 0; | |
6863 | ||
6864 | while (true) { | |
6865 | if (!parent || d_really_is_negative(parent) || | |
6866 | sb != parent->d_sb) | |
6867 | break; | |
6868 | ||
6869 | inode = BTRFS_I(d_inode(parent)); | |
6870 | if (root != inode->root) | |
6871 | break; | |
6872 | ||
ab12313a FM |
6873 | if (inode->generation >= trans->transid && |
6874 | need_log_inode(trans, inode)) { | |
90d04510 | 6875 | ret = btrfs_log_inode(trans, inode, |
48778179 | 6876 | LOG_INODE_EXISTS, ctx); |
b8aa330d FM |
6877 | if (ret) |
6878 | break; | |
6879 | } | |
6880 | if (IS_ROOT(parent)) | |
6881 | break; | |
6882 | ||
6883 | parent = dget_parent(parent); | |
6884 | dput(old_parent); | |
6885 | old_parent = parent; | |
6886 | } | |
6887 | dput(old_parent); | |
6888 | ||
6889 | return ret; | |
6890 | } | |
6891 | ||
6892 | static int log_all_new_ancestors(struct btrfs_trans_handle *trans, | |
6893 | struct btrfs_inode *inode, | |
6894 | struct dentry *parent, | |
6895 | struct btrfs_log_ctx *ctx) | |
6896 | { | |
6897 | struct btrfs_root *root = inode->root; | |
6898 | const u64 ino = btrfs_ino(inode); | |
6899 | struct btrfs_path *path; | |
6900 | struct btrfs_key search_key; | |
6901 | int ret; | |
6902 | ||
6903 | /* | |
6904 | * For a single hard link case, go through a fast path that does not | |
6905 | * need to iterate the fs/subvolume tree. | |
6906 | */ | |
6907 | if (inode->vfs_inode.i_nlink < 2) | |
6908 | return log_new_ancestors_fast(trans, inode, parent, ctx); | |
6909 | ||
6910 | path = btrfs_alloc_path(); | |
6911 | if (!path) | |
6912 | return -ENOMEM; | |
6913 | ||
6914 | search_key.objectid = ino; | |
6915 | search_key.type = BTRFS_INODE_REF_KEY; | |
6916 | search_key.offset = 0; | |
6917 | again: | |
6918 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
6919 | if (ret < 0) | |
6920 | goto out; | |
6921 | if (ret == 0) | |
6922 | path->slots[0]++; | |
6923 | ||
6924 | while (true) { | |
6925 | struct extent_buffer *leaf = path->nodes[0]; | |
6926 | int slot = path->slots[0]; | |
6927 | struct btrfs_key found_key; | |
6928 | ||
6929 | if (slot >= btrfs_header_nritems(leaf)) { | |
6930 | ret = btrfs_next_leaf(root, path); | |
6931 | if (ret < 0) | |
6932 | goto out; | |
6933 | else if (ret > 0) | |
6934 | break; | |
6935 | continue; | |
6936 | } | |
6937 | ||
6938 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
6939 | if (found_key.objectid != ino || | |
6940 | found_key.type > BTRFS_INODE_EXTREF_KEY) | |
6941 | break; | |
6942 | ||
6943 | /* | |
6944 | * Don't deal with extended references because they are rare | |
6945 | * cases and too complex to deal with (we would need to keep | |
6946 | * track of which subitem we are processing for each item in | |
6947 | * this loop, etc). So just return some error to fallback to | |
6948 | * a transaction commit. | |
6949 | */ | |
6950 | if (found_key.type == BTRFS_INODE_EXTREF_KEY) { | |
6951 | ret = -EMLINK; | |
6952 | goto out; | |
6953 | } | |
6954 | ||
6955 | /* | |
6956 | * Logging ancestors needs to do more searches on the fs/subvol | |
6957 | * tree, so it releases the path as needed to avoid deadlocks. | |
6958 | * Keep track of the last inode ref key and resume from that key | |
6959 | * after logging all new ancestors for the current hard link. | |
6960 | */ | |
6961 | memcpy(&search_key, &found_key, sizeof(search_key)); | |
6962 | ||
6963 | ret = log_new_ancestors(trans, root, path, ctx); | |
6964 | if (ret) | |
6965 | goto out; | |
6966 | btrfs_release_path(path); | |
6967 | goto again; | |
6968 | } | |
6969 | ret = 0; | |
6970 | out: | |
6971 | btrfs_free_path(path); | |
6972 | return ret; | |
6973 | } | |
6974 | ||
e02119d5 CM |
6975 | /* |
6976 | * helper function around btrfs_log_inode to make sure newly created | |
6977 | * parent directories also end up in the log. A minimal inode and backref | |
6978 | * only logging is done of any parent directories that are older than | |
6979 | * the last committed transaction | |
6980 | */ | |
48a3b636 | 6981 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
19df27a9 | 6982 | struct btrfs_inode *inode, |
49dae1bc | 6983 | struct dentry *parent, |
41a1eada | 6984 | int inode_only, |
8b050d35 | 6985 | struct btrfs_log_ctx *ctx) |
e02119d5 | 6986 | { |
f882274b | 6987 | struct btrfs_root *root = inode->root; |
0b246afa | 6988 | struct btrfs_fs_info *fs_info = root->fs_info; |
12fcfd22 | 6989 | int ret = 0; |
2f2ff0ee | 6990 | bool log_dentries = false; |
12fcfd22 | 6991 | |
0b246afa | 6992 | if (btrfs_test_opt(fs_info, NOTREELOG)) { |
f31f09f6 | 6993 | ret = BTRFS_LOG_FORCE_COMMIT; |
3a5e1404 SW |
6994 | goto end_no_trans; |
6995 | } | |
6996 | ||
f882274b | 6997 | if (btrfs_root_refs(&root->root_item) == 0) { |
f31f09f6 | 6998 | ret = BTRFS_LOG_FORCE_COMMIT; |
76dda93c YZ |
6999 | goto end_no_trans; |
7000 | } | |
7001 | ||
f2d72f42 FM |
7002 | /* |
7003 | * Skip already logged inodes or inodes corresponding to tmpfiles | |
7004 | * (since logging them is pointless, a link count of 0 means they | |
7005 | * will never be accessible). | |
7006 | */ | |
626e9f41 FM |
7007 | if ((btrfs_inode_in_log(inode, trans->transid) && |
7008 | list_empty(&ctx->ordered_extents)) || | |
f2d72f42 | 7009 | inode->vfs_inode.i_nlink == 0) { |
257c62e1 CM |
7010 | ret = BTRFS_NO_LOG_SYNC; |
7011 | goto end_no_trans; | |
7012 | } | |
7013 | ||
8b050d35 | 7014 | ret = start_log_trans(trans, root, ctx); |
4a500fd1 | 7015 | if (ret) |
e87ac136 | 7016 | goto end_no_trans; |
e02119d5 | 7017 | |
90d04510 | 7018 | ret = btrfs_log_inode(trans, inode, inode_only, ctx); |
4a500fd1 YZ |
7019 | if (ret) |
7020 | goto end_trans; | |
12fcfd22 | 7021 | |
af4176b4 CM |
7022 | /* |
7023 | * for regular files, if its inode is already on disk, we don't | |
7024 | * have to worry about the parents at all. This is because | |
7025 | * we can use the last_unlink_trans field to record renames | |
7026 | * and other fun in this file. | |
7027 | */ | |
19df27a9 | 7028 | if (S_ISREG(inode->vfs_inode.i_mode) && |
47d3db41 FM |
7029 | inode->generation < trans->transid && |
7030 | inode->last_unlink_trans < trans->transid) { | |
4a500fd1 YZ |
7031 | ret = 0; |
7032 | goto end_trans; | |
7033 | } | |
af4176b4 | 7034 | |
289cffcb | 7035 | if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries) |
2f2ff0ee FM |
7036 | log_dentries = true; |
7037 | ||
18aa0922 | 7038 | /* |
01327610 | 7039 | * On unlink we must make sure all our current and old parent directory |
18aa0922 FM |
7040 | * inodes are fully logged. This is to prevent leaving dangling |
7041 | * directory index entries in directories that were our parents but are | |
7042 | * not anymore. Not doing this results in old parent directory being | |
7043 | * impossible to delete after log replay (rmdir will always fail with | |
7044 | * error -ENOTEMPTY). | |
7045 | * | |
7046 | * Example 1: | |
7047 | * | |
7048 | * mkdir testdir | |
7049 | * touch testdir/foo | |
7050 | * ln testdir/foo testdir/bar | |
7051 | * sync | |
7052 | * unlink testdir/bar | |
7053 | * xfs_io -c fsync testdir/foo | |
7054 | * <power failure> | |
7055 | * mount fs, triggers log replay | |
7056 | * | |
7057 | * If we don't log the parent directory (testdir), after log replay the | |
7058 | * directory still has an entry pointing to the file inode using the bar | |
7059 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and | |
7060 | * the file inode has a link count of 1. | |
7061 | * | |
7062 | * Example 2: | |
7063 | * | |
7064 | * mkdir testdir | |
7065 | * touch foo | |
7066 | * ln foo testdir/foo2 | |
7067 | * ln foo testdir/foo3 | |
7068 | * sync | |
7069 | * unlink testdir/foo3 | |
7070 | * xfs_io -c fsync foo | |
7071 | * <power failure> | |
7072 | * mount fs, triggers log replay | |
7073 | * | |
7074 | * Similar as the first example, after log replay the parent directory | |
7075 | * testdir still has an entry pointing to the inode file with name foo3 | |
7076 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item | |
7077 | * and has a link count of 2. | |
7078 | */ | |
47d3db41 | 7079 | if (inode->last_unlink_trans >= trans->transid) { |
b8aa330d | 7080 | ret = btrfs_log_all_parents(trans, inode, ctx); |
18aa0922 FM |
7081 | if (ret) |
7082 | goto end_trans; | |
7083 | } | |
7084 | ||
b8aa330d FM |
7085 | ret = log_all_new_ancestors(trans, inode, parent, ctx); |
7086 | if (ret) | |
41bd6067 | 7087 | goto end_trans; |
76dda93c | 7088 | |
2f2ff0ee | 7089 | if (log_dentries) |
8786a6d7 | 7090 | ret = log_new_dir_dentries(trans, inode, ctx); |
2f2ff0ee FM |
7091 | else |
7092 | ret = 0; | |
4a500fd1 YZ |
7093 | end_trans: |
7094 | if (ret < 0) { | |
90787766 | 7095 | btrfs_set_log_full_commit(trans); |
f31f09f6 | 7096 | ret = BTRFS_LOG_FORCE_COMMIT; |
4a500fd1 | 7097 | } |
8b050d35 MX |
7098 | |
7099 | if (ret) | |
7100 | btrfs_remove_log_ctx(root, ctx); | |
12fcfd22 CM |
7101 | btrfs_end_log_trans(root); |
7102 | end_no_trans: | |
7103 | return ret; | |
e02119d5 CM |
7104 | } |
7105 | ||
7106 | /* | |
7107 | * it is not safe to log dentry if the chunk root has added new | |
7108 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. | |
7109 | * If this returns 1, you must commit the transaction to safely get your | |
7110 | * data on disk. | |
7111 | */ | |
7112 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | |
e5b84f7a | 7113 | struct dentry *dentry, |
8b050d35 | 7114 | struct btrfs_log_ctx *ctx) |
e02119d5 | 7115 | { |
6a912213 JB |
7116 | struct dentry *parent = dget_parent(dentry); |
7117 | int ret; | |
7118 | ||
f882274b | 7119 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, |
48778179 | 7120 | LOG_INODE_ALL, ctx); |
6a912213 JB |
7121 | dput(parent); |
7122 | ||
7123 | return ret; | |
e02119d5 CM |
7124 | } |
7125 | ||
7126 | /* | |
7127 | * should be called during mount to recover any replay any log trees | |
7128 | * from the FS | |
7129 | */ | |
7130 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) | |
7131 | { | |
7132 | int ret; | |
7133 | struct btrfs_path *path; | |
7134 | struct btrfs_trans_handle *trans; | |
7135 | struct btrfs_key key; | |
7136 | struct btrfs_key found_key; | |
e02119d5 CM |
7137 | struct btrfs_root *log; |
7138 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; | |
7139 | struct walk_control wc = { | |
7140 | .process_func = process_one_buffer, | |
430a6626 | 7141 | .stage = LOG_WALK_PIN_ONLY, |
e02119d5 CM |
7142 | }; |
7143 | ||
e02119d5 | 7144 | path = btrfs_alloc_path(); |
db5b493a TI |
7145 | if (!path) |
7146 | return -ENOMEM; | |
7147 | ||
afcdd129 | 7148 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
e02119d5 | 7149 | |
4a500fd1 | 7150 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
79787eaa JM |
7151 | if (IS_ERR(trans)) { |
7152 | ret = PTR_ERR(trans); | |
7153 | goto error; | |
7154 | } | |
e02119d5 CM |
7155 | |
7156 | wc.trans = trans; | |
7157 | wc.pin = 1; | |
7158 | ||
db5b493a | 7159 | ret = walk_log_tree(trans, log_root_tree, &wc); |
79787eaa | 7160 | if (ret) { |
ba51e2a1 | 7161 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7162 | goto error; |
7163 | } | |
e02119d5 CM |
7164 | |
7165 | again: | |
7166 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | |
7167 | key.offset = (u64)-1; | |
962a298f | 7168 | key.type = BTRFS_ROOT_ITEM_KEY; |
e02119d5 | 7169 | |
d397712b | 7170 | while (1) { |
e02119d5 | 7171 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
79787eaa JM |
7172 | |
7173 | if (ret < 0) { | |
ba51e2a1 | 7174 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7175 | goto error; |
7176 | } | |
e02119d5 CM |
7177 | if (ret > 0) { |
7178 | if (path->slots[0] == 0) | |
7179 | break; | |
7180 | path->slots[0]--; | |
7181 | } | |
7182 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
7183 | path->slots[0]); | |
b3b4aa74 | 7184 | btrfs_release_path(path); |
e02119d5 CM |
7185 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7186 | break; | |
7187 | ||
62a2c73e | 7188 | log = btrfs_read_tree_root(log_root_tree, &found_key); |
79787eaa JM |
7189 | if (IS_ERR(log)) { |
7190 | ret = PTR_ERR(log); | |
ba51e2a1 | 7191 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7192 | goto error; |
7193 | } | |
e02119d5 | 7194 | |
56e9357a DS |
7195 | wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset, |
7196 | true); | |
79787eaa JM |
7197 | if (IS_ERR(wc.replay_dest)) { |
7198 | ret = PTR_ERR(wc.replay_dest); | |
9bc574de JB |
7199 | |
7200 | /* | |
7201 | * We didn't find the subvol, likely because it was | |
7202 | * deleted. This is ok, simply skip this log and go to | |
7203 | * the next one. | |
7204 | * | |
7205 | * We need to exclude the root because we can't have | |
7206 | * other log replays overwriting this log as we'll read | |
7207 | * it back in a few more times. This will keep our | |
7208 | * block from being modified, and we'll just bail for | |
7209 | * each subsequent pass. | |
7210 | */ | |
7211 | if (ret == -ENOENT) | |
9fce5704 | 7212 | ret = btrfs_pin_extent_for_log_replay(trans, |
9bc574de JB |
7213 | log->node->start, |
7214 | log->node->len); | |
00246528 | 7215 | btrfs_put_root(log); |
9bc574de JB |
7216 | |
7217 | if (!ret) | |
7218 | goto next; | |
ba51e2a1 | 7219 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7220 | goto error; |
7221 | } | |
e02119d5 | 7222 | |
07d400a6 | 7223 | wc.replay_dest->log_root = log; |
2002ae11 JB |
7224 | ret = btrfs_record_root_in_trans(trans, wc.replay_dest); |
7225 | if (ret) | |
7226 | /* The loop needs to continue due to the root refs */ | |
ba51e2a1 | 7227 | btrfs_abort_transaction(trans, ret); |
2002ae11 JB |
7228 | else |
7229 | ret = walk_log_tree(trans, log, &wc); | |
e02119d5 | 7230 | |
b50c6e25 | 7231 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
e02119d5 CM |
7232 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
7233 | path); | |
ba51e2a1 JB |
7234 | if (ret) |
7235 | btrfs_abort_transaction(trans, ret); | |
e02119d5 CM |
7236 | } |
7237 | ||
900c9981 LB |
7238 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
7239 | struct btrfs_root *root = wc.replay_dest; | |
7240 | ||
7241 | btrfs_release_path(path); | |
7242 | ||
7243 | /* | |
7244 | * We have just replayed everything, and the highest | |
7245 | * objectid of fs roots probably has changed in case | |
7246 | * some inode_item's got replayed. | |
7247 | * | |
7248 | * root->objectid_mutex is not acquired as log replay | |
7249 | * could only happen during mount. | |
7250 | */ | |
453e4873 | 7251 | ret = btrfs_init_root_free_objectid(root); |
ba51e2a1 JB |
7252 | if (ret) |
7253 | btrfs_abort_transaction(trans, ret); | |
900c9981 LB |
7254 | } |
7255 | ||
07d400a6 | 7256 | wc.replay_dest->log_root = NULL; |
00246528 | 7257 | btrfs_put_root(wc.replay_dest); |
00246528 | 7258 | btrfs_put_root(log); |
e02119d5 | 7259 | |
b50c6e25 JB |
7260 | if (ret) |
7261 | goto error; | |
9bc574de | 7262 | next: |
e02119d5 CM |
7263 | if (found_key.offset == 0) |
7264 | break; | |
9bc574de | 7265 | key.offset = found_key.offset - 1; |
e02119d5 | 7266 | } |
b3b4aa74 | 7267 | btrfs_release_path(path); |
e02119d5 CM |
7268 | |
7269 | /* step one is to pin it all, step two is to replay just inodes */ | |
7270 | if (wc.pin) { | |
7271 | wc.pin = 0; | |
7272 | wc.process_func = replay_one_buffer; | |
7273 | wc.stage = LOG_WALK_REPLAY_INODES; | |
7274 | goto again; | |
7275 | } | |
7276 | /* step three is to replay everything */ | |
7277 | if (wc.stage < LOG_WALK_REPLAY_ALL) { | |
7278 | wc.stage++; | |
7279 | goto again; | |
7280 | } | |
7281 | ||
7282 | btrfs_free_path(path); | |
7283 | ||
abefa55a | 7284 | /* step 4: commit the transaction, which also unpins the blocks */ |
3a45bb20 | 7285 | ret = btrfs_commit_transaction(trans); |
abefa55a JB |
7286 | if (ret) |
7287 | return ret; | |
7288 | ||
e02119d5 | 7289 | log_root_tree->log_root = NULL; |
afcdd129 | 7290 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
00246528 | 7291 | btrfs_put_root(log_root_tree); |
79787eaa | 7292 | |
abefa55a | 7293 | return 0; |
79787eaa | 7294 | error: |
b50c6e25 | 7295 | if (wc.trans) |
3a45bb20 | 7296 | btrfs_end_transaction(wc.trans); |
1aeb6b56 | 7297 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
79787eaa JM |
7298 | btrfs_free_path(path); |
7299 | return ret; | |
e02119d5 | 7300 | } |
12fcfd22 CM |
7301 | |
7302 | /* | |
7303 | * there are some corner cases where we want to force a full | |
7304 | * commit instead of allowing a directory to be logged. | |
7305 | * | |
7306 | * They revolve around files there were unlinked from the directory, and | |
7307 | * this function updates the parent directory so that a full commit is | |
7308 | * properly done if it is fsync'd later after the unlinks are done. | |
2be63d5c FM |
7309 | * |
7310 | * Must be called before the unlink operations (updates to the subvolume tree, | |
7311 | * inodes, etc) are done. | |
12fcfd22 CM |
7312 | */ |
7313 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, | |
4176bdbf | 7314 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
12fcfd22 CM |
7315 | int for_rename) |
7316 | { | |
af4176b4 CM |
7317 | /* |
7318 | * when we're logging a file, if it hasn't been renamed | |
7319 | * or unlinked, and its inode is fully committed on disk, | |
7320 | * we don't have to worry about walking up the directory chain | |
7321 | * to log its parents. | |
7322 | * | |
7323 | * So, we use the last_unlink_trans field to put this transid | |
7324 | * into the file. When the file is logged we check it and | |
7325 | * don't log the parents if the file is fully on disk. | |
7326 | */ | |
4176bdbf NB |
7327 | mutex_lock(&inode->log_mutex); |
7328 | inode->last_unlink_trans = trans->transid; | |
7329 | mutex_unlock(&inode->log_mutex); | |
af4176b4 | 7330 | |
12fcfd22 CM |
7331 | /* |
7332 | * if this directory was already logged any new | |
7333 | * names for this file/dir will get recorded | |
7334 | */ | |
4176bdbf | 7335 | if (dir->logged_trans == trans->transid) |
12fcfd22 CM |
7336 | return; |
7337 | ||
7338 | /* | |
7339 | * if the inode we're about to unlink was logged, | |
7340 | * the log will be properly updated for any new names | |
7341 | */ | |
4176bdbf | 7342 | if (inode->logged_trans == trans->transid) |
12fcfd22 CM |
7343 | return; |
7344 | ||
7345 | /* | |
7346 | * when renaming files across directories, if the directory | |
7347 | * there we're unlinking from gets fsync'd later on, there's | |
7348 | * no way to find the destination directory later and fsync it | |
7349 | * properly. So, we have to be conservative and force commits | |
7350 | * so the new name gets discovered. | |
7351 | */ | |
7352 | if (for_rename) | |
7353 | goto record; | |
7354 | ||
7355 | /* we can safely do the unlink without any special recording */ | |
7356 | return; | |
7357 | ||
7358 | record: | |
4176bdbf NB |
7359 | mutex_lock(&dir->log_mutex); |
7360 | dir->last_unlink_trans = trans->transid; | |
7361 | mutex_unlock(&dir->log_mutex); | |
1ec9a1ae FM |
7362 | } |
7363 | ||
7364 | /* | |
7365 | * Make sure that if someone attempts to fsync the parent directory of a deleted | |
7366 | * snapshot, it ends up triggering a transaction commit. This is to guarantee | |
7367 | * that after replaying the log tree of the parent directory's root we will not | |
7368 | * see the snapshot anymore and at log replay time we will not see any log tree | |
7369 | * corresponding to the deleted snapshot's root, which could lead to replaying | |
7370 | * it after replaying the log tree of the parent directory (which would replay | |
7371 | * the snapshot delete operation). | |
2be63d5c FM |
7372 | * |
7373 | * Must be called before the actual snapshot destroy operation (updates to the | |
7374 | * parent root and tree of tree roots trees, etc) are done. | |
1ec9a1ae FM |
7375 | */ |
7376 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, | |
43663557 | 7377 | struct btrfs_inode *dir) |
1ec9a1ae | 7378 | { |
43663557 NB |
7379 | mutex_lock(&dir->log_mutex); |
7380 | dir->last_unlink_trans = trans->transid; | |
7381 | mutex_unlock(&dir->log_mutex); | |
12fcfd22 CM |
7382 | } |
7383 | ||
43dd529a | 7384 | /* |
d5f5bd54 FM |
7385 | * Update the log after adding a new name for an inode. |
7386 | * | |
7387 | * @trans: Transaction handle. | |
7388 | * @old_dentry: The dentry associated with the old name and the old | |
7389 | * parent directory. | |
7390 | * @old_dir: The inode of the previous parent directory for the case | |
7391 | * of a rename. For a link operation, it must be NULL. | |
88d2beec FM |
7392 | * @old_dir_index: The index number associated with the old name, meaningful |
7393 | * only for rename operations (when @old_dir is not NULL). | |
7394 | * Ignored for link operations. | |
d5f5bd54 FM |
7395 | * @parent: The dentry associated with the directory under which the |
7396 | * new name is located. | |
7397 | * | |
7398 | * Call this after adding a new name for an inode, as a result of a link or | |
7399 | * rename operation, and it will properly update the log to reflect the new name. | |
12fcfd22 | 7400 | */ |
75b463d2 | 7401 | void btrfs_log_new_name(struct btrfs_trans_handle *trans, |
d5f5bd54 | 7402 | struct dentry *old_dentry, struct btrfs_inode *old_dir, |
88d2beec | 7403 | u64 old_dir_index, struct dentry *parent) |
12fcfd22 | 7404 | { |
d5f5bd54 | 7405 | struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry)); |
259c4b96 | 7406 | struct btrfs_root *root = inode->root; |
75b463d2 | 7407 | struct btrfs_log_ctx ctx; |
259c4b96 | 7408 | bool log_pinned = false; |
0f8ce498 | 7409 | int ret; |
12fcfd22 | 7410 | |
af4176b4 CM |
7411 | /* |
7412 | * this will force the logging code to walk the dentry chain | |
7413 | * up for the file | |
7414 | */ | |
9a6509c4 | 7415 | if (!S_ISDIR(inode->vfs_inode.i_mode)) |
9ca5fbfb | 7416 | inode->last_unlink_trans = trans->transid; |
af4176b4 | 7417 | |
12fcfd22 CM |
7418 | /* |
7419 | * if this inode hasn't been logged and directory we're renaming it | |
7420 | * from hasn't been logged, we don't need to log it | |
7421 | */ | |
0f8ce498 FM |
7422 | ret = inode_logged(trans, inode, NULL); |
7423 | if (ret < 0) { | |
7424 | goto out; | |
7425 | } else if (ret == 0) { | |
7426 | if (!old_dir) | |
7427 | return; | |
7428 | /* | |
7429 | * If the inode was not logged and we are doing a rename (old_dir is not | |
7430 | * NULL), check if old_dir was logged - if it was not we can return and | |
7431 | * do nothing. | |
7432 | */ | |
7433 | ret = inode_logged(trans, old_dir, NULL); | |
7434 | if (ret < 0) | |
7435 | goto out; | |
7436 | else if (ret == 0) | |
7437 | return; | |
7438 | } | |
7439 | ret = 0; | |
12fcfd22 | 7440 | |
54a40fc3 FM |
7441 | /* |
7442 | * If we are doing a rename (old_dir is not NULL) from a directory that | |
88d2beec FM |
7443 | * was previously logged, make sure that on log replay we get the old |
7444 | * dir entry deleted. This is needed because we will also log the new | |
7445 | * name of the renamed inode, so we need to make sure that after log | |
7446 | * replay we don't end up with both the new and old dir entries existing. | |
54a40fc3 | 7447 | */ |
88d2beec FM |
7448 | if (old_dir && old_dir->logged_trans == trans->transid) { |
7449 | struct btrfs_root *log = old_dir->root->log_root; | |
7450 | struct btrfs_path *path; | |
ab3c5c18 | 7451 | struct fscrypt_name fname; |
88d2beec FM |
7452 | |
7453 | ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX); | |
7454 | ||
ab3c5c18 STD |
7455 | ret = fscrypt_setup_filename(&old_dir->vfs_inode, |
7456 | &old_dentry->d_name, 0, &fname); | |
7457 | if (ret) | |
7458 | goto out; | |
259c4b96 FM |
7459 | /* |
7460 | * We have two inodes to update in the log, the old directory and | |
7461 | * the inode that got renamed, so we must pin the log to prevent | |
7462 | * anyone from syncing the log until we have updated both inodes | |
7463 | * in the log. | |
7464 | */ | |
723df2bc FM |
7465 | ret = join_running_log_trans(root); |
7466 | /* | |
7467 | * At least one of the inodes was logged before, so this should | |
7468 | * not fail, but if it does, it's not serious, just bail out and | |
7469 | * mark the log for a full commit. | |
7470 | */ | |
7471 | if (WARN_ON_ONCE(ret < 0)) | |
7472 | goto out; | |
259c4b96 | 7473 | log_pinned = true; |
259c4b96 | 7474 | |
88d2beec FM |
7475 | path = btrfs_alloc_path(); |
7476 | if (!path) { | |
259c4b96 | 7477 | ret = -ENOMEM; |
ab3c5c18 | 7478 | fscrypt_free_filename(&fname); |
259c4b96 | 7479 | goto out; |
88d2beec FM |
7480 | } |
7481 | ||
7482 | /* | |
7483 | * Other concurrent task might be logging the old directory, | |
7484 | * as it can be triggered when logging other inode that had or | |
750ee454 FM |
7485 | * still has a dentry in the old directory. We lock the old |
7486 | * directory's log_mutex to ensure the deletion of the old | |
7487 | * name is persisted, because during directory logging we | |
7488 | * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of | |
7489 | * the old name's dir index item is in the delayed items, so | |
7490 | * it could be missed by an in progress directory logging. | |
88d2beec FM |
7491 | */ |
7492 | mutex_lock(&old_dir->log_mutex); | |
7493 | ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir), | |
6db75318 | 7494 | &fname.disk_name, old_dir_index); |
88d2beec FM |
7495 | if (ret > 0) { |
7496 | /* | |
7497 | * The dentry does not exist in the log, so record its | |
7498 | * deletion. | |
7499 | */ | |
7500 | btrfs_release_path(path); | |
7501 | ret = insert_dir_log_key(trans, log, path, | |
7502 | btrfs_ino(old_dir), | |
7503 | old_dir_index, old_dir_index); | |
7504 | } | |
7505 | mutex_unlock(&old_dir->log_mutex); | |
7506 | ||
7507 | btrfs_free_path(path); | |
ab3c5c18 | 7508 | fscrypt_free_filename(&fname); |
259c4b96 FM |
7509 | if (ret < 0) |
7510 | goto out; | |
88d2beec | 7511 | } |
54a40fc3 | 7512 | |
75b463d2 FM |
7513 | btrfs_init_log_ctx(&ctx, &inode->vfs_inode); |
7514 | ctx.logging_new_name = true; | |
7515 | /* | |
7516 | * We don't care about the return value. If we fail to log the new name | |
7517 | * then we know the next attempt to sync the log will fallback to a full | |
7518 | * transaction commit (due to a call to btrfs_set_log_full_commit()), so | |
7519 | * we don't need to worry about getting a log committed that has an | |
7520 | * inconsistent state after a rename operation. | |
7521 | */ | |
48778179 | 7522 | btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx); |
e09d94c9 | 7523 | ASSERT(list_empty(&ctx.conflict_inodes)); |
259c4b96 | 7524 | out: |
0f8ce498 FM |
7525 | /* |
7526 | * If an error happened mark the log for a full commit because it's not | |
7527 | * consistent and up to date or we couldn't find out if one of the | |
7528 | * inodes was logged before in this transaction. Do it before unpinning | |
7529 | * the log, to avoid any races with someone else trying to commit it. | |
7530 | */ | |
7531 | if (ret < 0) | |
7532 | btrfs_set_log_full_commit(trans); | |
7533 | if (log_pinned) | |
259c4b96 | 7534 | btrfs_end_log_trans(root); |
12fcfd22 CM |
7535 | } |
7536 |