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