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