]>
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" | |
f186373f | 16 | #include "backref.h" |
ebb8765b | 17 | #include "compression.h" |
df2c95f3 | 18 | #include "qgroup.h" |
6787bb9f NB |
19 | #include "block-group.h" |
20 | #include "space-info.h" | |
26c2c454 | 21 | #include "inode-item.h" |
c7f13d42 | 22 | #include "fs.h" |
ad1ac501 | 23 | #include "accessors.h" |
a0231804 | 24 | #include "extent-tree.h" |
45c40c8f | 25 | #include "root-tree.h" |
f2b39277 | 26 | #include "dir-item.h" |
7c8ede16 | 27 | #include "file-item.h" |
af142b6f | 28 | #include "file.h" |
aa5d3003 | 29 | #include "orphan.h" |
103c1972 | 30 | #include "tree-checker.h" |
e02119d5 | 31 | |
e09d94c9 FM |
32 | #define MAX_CONFLICT_INODES 10 |
33 | ||
e02119d5 CM |
34 | /* magic values for the inode_only field in btrfs_log_inode: |
35 | * | |
36 | * LOG_INODE_ALL means to log everything | |
37 | * LOG_INODE_EXISTS means to log just enough to recreate the inode | |
38 | * during log replay | |
39 | */ | |
e13976cf DS |
40 | enum { |
41 | LOG_INODE_ALL, | |
42 | LOG_INODE_EXISTS, | |
e13976cf | 43 | }; |
e02119d5 | 44 | |
12fcfd22 CM |
45 | /* |
46 | * directory trouble cases | |
47 | * | |
48 | * 1) on rename or unlink, if the inode being unlinked isn't in the fsync | |
49 | * log, we must force a full commit before doing an fsync of the directory | |
50 | * where the unlink was done. | |
51 | * ---> record transid of last unlink/rename per directory | |
52 | * | |
53 | * mkdir foo/some_dir | |
54 | * normal commit | |
55 | * rename foo/some_dir foo2/some_dir | |
56 | * mkdir foo/some_dir | |
57 | * fsync foo/some_dir/some_file | |
58 | * | |
59 | * The fsync above will unlink the original some_dir without recording | |
60 | * it in its new location (foo2). After a crash, some_dir will be gone | |
61 | * unless the fsync of some_file forces a full commit | |
62 | * | |
63 | * 2) we must log any new names for any file or dir that is in the fsync | |
64 | * log. ---> check inode while renaming/linking. | |
65 | * | |
66 | * 2a) we must log any new names for any file or dir during rename | |
67 | * when the directory they are being removed from was logged. | |
68 | * ---> check inode and old parent dir during rename | |
69 | * | |
70 | * 2a is actually the more important variant. With the extra logging | |
71 | * a crash might unlink the old name without recreating the new one | |
72 | * | |
73 | * 3) after a crash, we must go through any directories with a link count | |
74 | * of zero and redo the rm -rf | |
75 | * | |
76 | * mkdir f1/foo | |
77 | * normal commit | |
78 | * rm -rf f1/foo | |
79 | * fsync(f1) | |
80 | * | |
81 | * The directory f1 was fully removed from the FS, but fsync was never | |
82 | * called on f1, only its parent dir. After a crash the rm -rf must | |
83 | * be replayed. This must be able to recurse down the entire | |
84 | * directory tree. The inode link count fixup code takes care of the | |
85 | * ugly details. | |
86 | */ | |
87 | ||
e02119d5 CM |
88 | /* |
89 | * stages for the tree walking. The first | |
90 | * stage (0) is to only pin down the blocks we find | |
91 | * the second stage (1) is to make sure that all the inodes | |
92 | * we find in the log are created in the subvolume. | |
93 | * | |
94 | * The last stage is to deal with directories and links and extents | |
95 | * and all the other fun semantics | |
96 | */ | |
e13976cf DS |
97 | enum { |
98 | LOG_WALK_PIN_ONLY, | |
99 | LOG_WALK_REPLAY_INODES, | |
100 | LOG_WALK_REPLAY_DIR_INDEX, | |
101 | LOG_WALK_REPLAY_ALL, | |
102 | }; | |
e02119d5 | 103 | |
12fcfd22 | 104 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
90d04510 | 105 | struct btrfs_inode *inode, |
49dae1bc | 106 | int inode_only, |
8407f553 | 107 | struct btrfs_log_ctx *ctx); |
ec051c0f YZ |
108 | static int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
109 | struct btrfs_root *root, | |
110 | struct btrfs_path *path, u64 objectid); | |
12fcfd22 CM |
111 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
112 | struct btrfs_root *root, | |
113 | struct btrfs_root *log, | |
114 | struct btrfs_path *path, | |
115 | u64 dirid, int del_all); | |
fa1a0f42 | 116 | static void wait_log_commit(struct btrfs_root *root, int transid); |
e02119d5 CM |
117 | |
118 | /* | |
119 | * tree logging is a special write ahead log used to make sure that | |
120 | * fsyncs and O_SYNCs can happen without doing full tree commits. | |
121 | * | |
122 | * Full tree commits are expensive because they require commonly | |
123 | * modified blocks to be recowed, creating many dirty pages in the | |
124 | * extent tree an 4x-6x higher write load than ext3. | |
125 | * | |
126 | * Instead of doing a tree commit on every fsync, we use the | |
127 | * key ranges and transaction ids to find items for a given file or directory | |
128 | * that have changed in this transaction. Those items are copied into | |
129 | * a special tree (one per subvolume root), that tree is written to disk | |
130 | * and then the fsync is considered complete. | |
131 | * | |
132 | * After a crash, items are copied out of the log-tree back into the | |
133 | * subvolume tree. Any file data extents found are recorded in the extent | |
134 | * allocation tree, and the log-tree freed. | |
135 | * | |
136 | * The log tree is read three times, once to pin down all the extents it is | |
137 | * using in ram and once, once to create all the inodes logged in the tree | |
138 | * and once to do all the other items. | |
139 | */ | |
140 | ||
e02119d5 CM |
141 | /* |
142 | * start a sub transaction and setup the log tree | |
143 | * this increments the log tree writer count to make the people | |
144 | * syncing the tree wait for us to finish | |
145 | */ | |
146 | static int start_log_trans(struct btrfs_trans_handle *trans, | |
8b050d35 MX |
147 | struct btrfs_root *root, |
148 | struct btrfs_log_ctx *ctx) | |
e02119d5 | 149 | { |
0b246afa | 150 | struct btrfs_fs_info *fs_info = root->fs_info; |
47876f7c | 151 | struct btrfs_root *tree_root = fs_info->tree_root; |
fa1a0f42 | 152 | const bool zoned = btrfs_is_zoned(fs_info); |
34eb2a52 | 153 | int ret = 0; |
fa1a0f42 | 154 | bool created = false; |
7237f183 | 155 | |
47876f7c FM |
156 | /* |
157 | * First check if the log root tree was already created. If not, create | |
158 | * it before locking the root's log_mutex, just to keep lockdep happy. | |
159 | */ | |
160 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) { | |
161 | mutex_lock(&tree_root->log_mutex); | |
162 | if (!fs_info->log_root_tree) { | |
163 | ret = btrfs_init_log_root_tree(trans, fs_info); | |
fa1a0f42 | 164 | if (!ret) { |
47876f7c | 165 | set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state); |
fa1a0f42 NA |
166 | created = true; |
167 | } | |
47876f7c FM |
168 | } |
169 | mutex_unlock(&tree_root->log_mutex); | |
170 | if (ret) | |
171 | return ret; | |
172 | } | |
173 | ||
7237f183 | 174 | mutex_lock(&root->log_mutex); |
34eb2a52 | 175 | |
fa1a0f42 | 176 | again: |
7237f183 | 177 | if (root->log_root) { |
fa1a0f42 NA |
178 | int index = (root->log_transid + 1) % 2; |
179 | ||
4884b8e8 | 180 | if (btrfs_need_log_full_commit(trans)) { |
f31f09f6 | 181 | ret = BTRFS_LOG_FORCE_COMMIT; |
50471a38 MX |
182 | goto out; |
183 | } | |
34eb2a52 | 184 | |
fa1a0f42 NA |
185 | if (zoned && atomic_read(&root->log_commit[index])) { |
186 | wait_log_commit(root, root->log_transid - 1); | |
187 | goto again; | |
188 | } | |
189 | ||
ff782e0a | 190 | if (!root->log_start_pid) { |
27cdeb70 | 191 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
34eb2a52 | 192 | root->log_start_pid = current->pid; |
ff782e0a | 193 | } else if (root->log_start_pid != current->pid) { |
27cdeb70 | 194 | set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
ff782e0a | 195 | } |
34eb2a52 | 196 | } else { |
fa1a0f42 NA |
197 | /* |
198 | * This means fs_info->log_root_tree was already created | |
199 | * for some other FS trees. Do the full commit not to mix | |
200 | * nodes from multiple log transactions to do sequential | |
201 | * writing. | |
202 | */ | |
203 | if (zoned && !created) { | |
f31f09f6 | 204 | ret = BTRFS_LOG_FORCE_COMMIT; |
fa1a0f42 NA |
205 | goto out; |
206 | } | |
207 | ||
e02119d5 | 208 | ret = btrfs_add_log_tree(trans, root); |
4a500fd1 | 209 | if (ret) |
e87ac136 | 210 | goto out; |
34eb2a52 | 211 | |
e7a79811 | 212 | set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); |
34eb2a52 Z |
213 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
214 | root->log_start_pid = current->pid; | |
e02119d5 | 215 | } |
34eb2a52 | 216 | |
7237f183 | 217 | atomic_inc(&root->log_writers); |
289cffcb | 218 | if (!ctx->logging_new_name) { |
34eb2a52 | 219 | int index = root->log_transid % 2; |
8b050d35 | 220 | list_add_tail(&ctx->list, &root->log_ctxs[index]); |
d1433deb | 221 | ctx->log_transid = root->log_transid; |
8b050d35 | 222 | } |
34eb2a52 | 223 | |
e87ac136 | 224 | out: |
7237f183 | 225 | mutex_unlock(&root->log_mutex); |
e87ac136 | 226 | return ret; |
e02119d5 CM |
227 | } |
228 | ||
229 | /* | |
230 | * returns 0 if there was a log transaction running and we were able | |
231 | * to join, or returns -ENOENT if there were not transactions | |
232 | * in progress | |
233 | */ | |
234 | static int join_running_log_trans(struct btrfs_root *root) | |
235 | { | |
fa1a0f42 | 236 | const bool zoned = btrfs_is_zoned(root->fs_info); |
e02119d5 CM |
237 | int ret = -ENOENT; |
238 | ||
e7a79811 FM |
239 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state)) |
240 | return ret; | |
241 | ||
7237f183 | 242 | mutex_lock(&root->log_mutex); |
fa1a0f42 | 243 | again: |
e02119d5 | 244 | if (root->log_root) { |
fa1a0f42 NA |
245 | int index = (root->log_transid + 1) % 2; |
246 | ||
e02119d5 | 247 | ret = 0; |
fa1a0f42 NA |
248 | if (zoned && atomic_read(&root->log_commit[index])) { |
249 | wait_log_commit(root, root->log_transid - 1); | |
250 | goto again; | |
251 | } | |
7237f183 | 252 | atomic_inc(&root->log_writers); |
e02119d5 | 253 | } |
7237f183 | 254 | mutex_unlock(&root->log_mutex); |
e02119d5 CM |
255 | return ret; |
256 | } | |
257 | ||
12fcfd22 CM |
258 | /* |
259 | * This either makes the current running log transaction wait | |
260 | * until you call btrfs_end_log_trans() or it makes any future | |
261 | * log transactions wait until you call btrfs_end_log_trans() | |
262 | */ | |
45128b08 | 263 | void btrfs_pin_log_trans(struct btrfs_root *root) |
12fcfd22 | 264 | { |
12fcfd22 | 265 | atomic_inc(&root->log_writers); |
12fcfd22 CM |
266 | } |
267 | ||
e02119d5 CM |
268 | /* |
269 | * indicate we're done making changes to the log tree | |
270 | * and wake up anyone waiting to do a sync | |
271 | */ | |
143bede5 | 272 | void btrfs_end_log_trans(struct btrfs_root *root) |
e02119d5 | 273 | { |
7237f183 | 274 | if (atomic_dec_and_test(&root->log_writers)) { |
093258e6 DS |
275 | /* atomic_dec_and_test implies a barrier */ |
276 | cond_wake_up_nomb(&root->log_writer_wait); | |
7237f183 | 277 | } |
e02119d5 CM |
278 | } |
279 | ||
e02119d5 CM |
280 | /* |
281 | * the walk control struct is used to pass state down the chain when | |
282 | * processing the log tree. The stage field tells us which part | |
283 | * of the log tree processing we are currently doing. The others | |
284 | * are state fields used for that specific part | |
285 | */ | |
286 | struct walk_control { | |
287 | /* should we free the extent on disk when done? This is used | |
288 | * at transaction commit time while freeing a log tree | |
289 | */ | |
290 | int free; | |
291 | ||
e02119d5 CM |
292 | /* pin only walk, we record which extents on disk belong to the |
293 | * log trees | |
294 | */ | |
295 | int pin; | |
296 | ||
297 | /* what stage of the replay code we're currently in */ | |
298 | int stage; | |
299 | ||
f2d72f42 FM |
300 | /* |
301 | * Ignore any items from the inode currently being processed. Needs | |
302 | * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in | |
303 | * the LOG_WALK_REPLAY_INODES stage. | |
304 | */ | |
305 | bool ignore_cur_inode; | |
306 | ||
e02119d5 CM |
307 | /* the root we are currently replaying */ |
308 | struct btrfs_root *replay_dest; | |
309 | ||
310 | /* the trans handle for the current replay */ | |
311 | struct btrfs_trans_handle *trans; | |
312 | ||
313 | /* the function that gets used to process blocks we find in the | |
314 | * tree. Note the extent_buffer might not be up to date when it is | |
315 | * passed in, and it must be checked or read if you need the data | |
316 | * inside it | |
317 | */ | |
318 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, | |
581c1760 | 319 | struct walk_control *wc, u64 gen, int level); |
e02119d5 CM |
320 | }; |
321 | ||
322 | /* | |
323 | * process_func used to pin down extents, write them or wait on them | |
324 | */ | |
325 | static int process_one_buffer(struct btrfs_root *log, | |
326 | struct extent_buffer *eb, | |
581c1760 | 327 | struct walk_control *wc, u64 gen, int level) |
e02119d5 | 328 | { |
0b246afa | 329 | struct btrfs_fs_info *fs_info = log->fs_info; |
b50c6e25 JB |
330 | int ret = 0; |
331 | ||
8c2a1a30 JB |
332 | /* |
333 | * If this fs is mixed then we need to be able to process the leaves to | |
334 | * pin down any logged extents, so we have to read the block. | |
335 | */ | |
0b246afa | 336 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
789d6a3a QW |
337 | struct btrfs_tree_parent_check check = { |
338 | .level = level, | |
339 | .transid = gen | |
340 | }; | |
341 | ||
342 | ret = btrfs_read_extent_buffer(eb, &check); | |
8c2a1a30 JB |
343 | if (ret) |
344 | return ret; | |
345 | } | |
346 | ||
c816d705 | 347 | if (wc->pin) { |
007dec8c | 348 | ret = btrfs_pin_extent_for_log_replay(wc->trans, eb); |
c816d705 FM |
349 | if (ret) |
350 | return ret; | |
e02119d5 | 351 | |
c816d705 FM |
352 | if (btrfs_buffer_uptodate(eb, gen, 0) && |
353 | btrfs_header_level(eb) == 0) | |
bcdc428c | 354 | ret = btrfs_exclude_logged_extents(eb); |
e02119d5 | 355 | } |
b50c6e25 | 356 | return ret; |
e02119d5 CM |
357 | } |
358 | ||
3a8d1db3 FM |
359 | /* |
360 | * Item overwrite used by replay and tree logging. eb, slot and key all refer | |
361 | * to the src data we are copying out. | |
362 | * | |
363 | * root is the tree we are copying into, and path is a scratch | |
364 | * path for use in this function (it should be released on entry and | |
365 | * will be released on exit). | |
366 | * | |
367 | * If the key is already in the destination tree the existing item is | |
368 | * overwritten. If the existing item isn't big enough, it is extended. | |
369 | * If it is too large, it is truncated. | |
370 | * | |
371 | * If the key isn't in the destination yet, a new item is inserted. | |
372 | */ | |
373 | static int overwrite_item(struct btrfs_trans_handle *trans, | |
374 | struct btrfs_root *root, | |
375 | struct btrfs_path *path, | |
376 | struct extent_buffer *eb, int slot, | |
377 | struct btrfs_key *key) | |
e02119d5 CM |
378 | { |
379 | int ret; | |
380 | u32 item_size; | |
381 | u64 saved_i_size = 0; | |
382 | int save_old_i_size = 0; | |
383 | unsigned long src_ptr; | |
384 | unsigned long dst_ptr; | |
4bc4bee4 | 385 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; |
e02119d5 | 386 | |
3eb42344 FM |
387 | /* |
388 | * This is only used during log replay, so the root is always from a | |
389 | * fs/subvolume tree. In case we ever need to support a log root, then | |
390 | * we'll have to clone the leaf in the path, release the path and use | |
391 | * the leaf before writing into the log tree. See the comments at | |
392 | * copy_items() for more details. | |
393 | */ | |
394 | ASSERT(root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID); | |
e02119d5 | 395 | |
3212fa14 | 396 | item_size = btrfs_item_size(eb, slot); |
e02119d5 CM |
397 | src_ptr = btrfs_item_ptr_offset(eb, slot); |
398 | ||
3a8d1db3 FM |
399 | /* Look for the key in the destination tree. */ |
400 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
401 | if (ret < 0) | |
402 | return ret; | |
4bc4bee4 | 403 | |
e02119d5 CM |
404 | if (ret == 0) { |
405 | char *src_copy; | |
406 | char *dst_copy; | |
3212fa14 | 407 | u32 dst_size = btrfs_item_size(path->nodes[0], |
e02119d5 CM |
408 | path->slots[0]); |
409 | if (dst_size != item_size) | |
410 | goto insert; | |
411 | ||
412 | if (item_size == 0) { | |
b3b4aa74 | 413 | btrfs_release_path(path); |
e02119d5 CM |
414 | return 0; |
415 | } | |
416 | dst_copy = kmalloc(item_size, GFP_NOFS); | |
417 | src_copy = kmalloc(item_size, GFP_NOFS); | |
2a29edc6 | 418 | if (!dst_copy || !src_copy) { |
b3b4aa74 | 419 | btrfs_release_path(path); |
2a29edc6 | 420 | kfree(dst_copy); |
421 | kfree(src_copy); | |
422 | return -ENOMEM; | |
423 | } | |
e02119d5 CM |
424 | |
425 | read_extent_buffer(eb, src_copy, src_ptr, item_size); | |
426 | ||
427 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
428 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, | |
429 | item_size); | |
430 | ret = memcmp(dst_copy, src_copy, item_size); | |
431 | ||
432 | kfree(dst_copy); | |
433 | kfree(src_copy); | |
434 | /* | |
435 | * they have the same contents, just return, this saves | |
436 | * us from cowing blocks in the destination tree and doing | |
437 | * extra writes that may not have been done by a previous | |
438 | * sync | |
439 | */ | |
440 | if (ret == 0) { | |
b3b4aa74 | 441 | btrfs_release_path(path); |
e02119d5 CM |
442 | return 0; |
443 | } | |
444 | ||
4bc4bee4 JB |
445 | /* |
446 | * We need to load the old nbytes into the inode so when we | |
447 | * replay the extents we've logged we get the right nbytes. | |
448 | */ | |
449 | if (inode_item) { | |
450 | struct btrfs_inode_item *item; | |
451 | u64 nbytes; | |
d555438b | 452 | u32 mode; |
4bc4bee4 JB |
453 | |
454 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
455 | struct btrfs_inode_item); | |
456 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); | |
457 | item = btrfs_item_ptr(eb, slot, | |
458 | struct btrfs_inode_item); | |
459 | btrfs_set_inode_nbytes(eb, item, nbytes); | |
d555438b JB |
460 | |
461 | /* | |
462 | * If this is a directory we need to reset the i_size to | |
463 | * 0 so that we can set it up properly when replaying | |
464 | * the rest of the items in this log. | |
465 | */ | |
466 | mode = btrfs_inode_mode(eb, item); | |
467 | if (S_ISDIR(mode)) | |
468 | btrfs_set_inode_size(eb, item, 0); | |
4bc4bee4 JB |
469 | } |
470 | } else if (inode_item) { | |
471 | struct btrfs_inode_item *item; | |
d555438b | 472 | u32 mode; |
4bc4bee4 JB |
473 | |
474 | /* | |
475 | * New inode, set nbytes to 0 so that the nbytes comes out | |
476 | * properly when we replay the extents. | |
477 | */ | |
478 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | |
479 | btrfs_set_inode_nbytes(eb, item, 0); | |
d555438b JB |
480 | |
481 | /* | |
482 | * If this is a directory we need to reset the i_size to 0 so | |
483 | * that we can set it up properly when replaying the rest of | |
484 | * the items in this log. | |
485 | */ | |
486 | mode = btrfs_inode_mode(eb, item); | |
487 | if (S_ISDIR(mode)) | |
488 | btrfs_set_inode_size(eb, item, 0); | |
e02119d5 CM |
489 | } |
490 | insert: | |
b3b4aa74 | 491 | btrfs_release_path(path); |
e02119d5 | 492 | /* try to insert the key into the destination tree */ |
df8d116f | 493 | path->skip_release_on_error = 1; |
e02119d5 CM |
494 | ret = btrfs_insert_empty_item(trans, root, path, |
495 | key, item_size); | |
df8d116f | 496 | path->skip_release_on_error = 0; |
e02119d5 CM |
497 | |
498 | /* make sure any existing item is the correct size */ | |
df8d116f | 499 | if (ret == -EEXIST || ret == -EOVERFLOW) { |
e02119d5 | 500 | u32 found_size; |
3212fa14 | 501 | found_size = btrfs_item_size(path->nodes[0], |
e02119d5 | 502 | path->slots[0]); |
143bede5 | 503 | if (found_size > item_size) |
50564b65 | 504 | btrfs_truncate_item(trans, path, item_size, 1); |
143bede5 | 505 | else if (found_size < item_size) |
50564b65 | 506 | btrfs_extend_item(trans, path, item_size - found_size); |
e02119d5 | 507 | } else if (ret) { |
4a500fd1 | 508 | return ret; |
e02119d5 CM |
509 | } |
510 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], | |
511 | path->slots[0]); | |
512 | ||
513 | /* don't overwrite an existing inode if the generation number | |
514 | * was logged as zero. This is done when the tree logging code | |
515 | * is just logging an inode to make sure it exists after recovery. | |
516 | * | |
517 | * Also, don't overwrite i_size on directories during replay. | |
518 | * log replay inserts and removes directory items based on the | |
519 | * state of the tree found in the subvolume, and i_size is modified | |
520 | * as it goes | |
521 | */ | |
522 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { | |
523 | struct btrfs_inode_item *src_item; | |
524 | struct btrfs_inode_item *dst_item; | |
525 | ||
526 | src_item = (struct btrfs_inode_item *)src_ptr; | |
527 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
528 | ||
1a4bcf47 FM |
529 | if (btrfs_inode_generation(eb, src_item) == 0) { |
530 | struct extent_buffer *dst_eb = path->nodes[0]; | |
2f2ff0ee | 531 | const u64 ino_size = btrfs_inode_size(eb, src_item); |
1a4bcf47 | 532 | |
2f2ff0ee FM |
533 | /* |
534 | * For regular files an ino_size == 0 is used only when | |
535 | * logging that an inode exists, as part of a directory | |
536 | * fsync, and the inode wasn't fsynced before. In this | |
537 | * case don't set the size of the inode in the fs/subvol | |
538 | * tree, otherwise we would be throwing valid data away. | |
539 | */ | |
1a4bcf47 | 540 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && |
2f2ff0ee | 541 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && |
60d48e2e DS |
542 | ino_size != 0) |
543 | btrfs_set_inode_size(dst_eb, dst_item, ino_size); | |
e02119d5 | 544 | goto no_copy; |
1a4bcf47 | 545 | } |
e02119d5 | 546 | |
3eb42344 | 547 | if (S_ISDIR(btrfs_inode_mode(eb, src_item)) && |
e02119d5 CM |
548 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { |
549 | save_old_i_size = 1; | |
550 | saved_i_size = btrfs_inode_size(path->nodes[0], | |
551 | dst_item); | |
552 | } | |
553 | } | |
554 | ||
555 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, | |
556 | src_ptr, item_size); | |
557 | ||
558 | if (save_old_i_size) { | |
559 | struct btrfs_inode_item *dst_item; | |
560 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
561 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); | |
562 | } | |
563 | ||
564 | /* make sure the generation is filled in */ | |
565 | if (key->type == BTRFS_INODE_ITEM_KEY) { | |
566 | struct btrfs_inode_item *dst_item; | |
567 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
568 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { | |
569 | btrfs_set_inode_generation(path->nodes[0], dst_item, | |
570 | trans->transid); | |
571 | } | |
572 | } | |
573 | no_copy: | |
50564b65 | 574 | btrfs_mark_buffer_dirty(trans, path->nodes[0]); |
b3b4aa74 | 575 | btrfs_release_path(path); |
e02119d5 CM |
576 | return 0; |
577 | } | |
578 | ||
e43eec81 | 579 | static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len, |
6db75318 | 580 | struct fscrypt_str *name) |
e43eec81 STD |
581 | { |
582 | char *buf; | |
583 | ||
584 | buf = kmalloc(len, GFP_NOFS); | |
585 | if (!buf) | |
586 | return -ENOMEM; | |
587 | ||
588 | read_extent_buffer(eb, buf, (unsigned long)start, len); | |
589 | name->name = buf; | |
590 | name->len = len; | |
591 | return 0; | |
592 | } | |
593 | ||
e02119d5 CM |
594 | /* |
595 | * simple helper to read an inode off the disk from a given root | |
596 | * This can only be called for subvolume roots and not for the log | |
597 | */ | |
598 | static noinline struct inode *read_one_inode(struct btrfs_root *root, | |
599 | u64 objectid) | |
600 | { | |
601 | struct inode *inode; | |
e02119d5 | 602 | |
0202e83f | 603 | inode = btrfs_iget(root->fs_info->sb, objectid, root); |
2e19f1f9 | 604 | if (IS_ERR(inode)) |
5d4f98a2 | 605 | inode = NULL; |
e02119d5 CM |
606 | return inode; |
607 | } | |
608 | ||
609 | /* replays a single extent in 'eb' at 'slot' with 'key' into the | |
610 | * subvolume 'root'. path is released on entry and should be released | |
611 | * on exit. | |
612 | * | |
613 | * extents in the log tree have not been allocated out of the extent | |
614 | * tree yet. So, this completes the allocation, taking a reference | |
615 | * as required if the extent already exists or creating a new extent | |
616 | * if it isn't in the extent allocation tree yet. | |
617 | * | |
618 | * The extent is inserted into the file, dropping any existing extents | |
619 | * from the file that overlap the new one. | |
620 | */ | |
621 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |
622 | struct btrfs_root *root, | |
623 | struct btrfs_path *path, | |
624 | struct extent_buffer *eb, int slot, | |
625 | struct btrfs_key *key) | |
626 | { | |
5893dfb9 | 627 | struct btrfs_drop_extents_args drop_args = { 0 }; |
0b246afa | 628 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 | 629 | int found_type; |
e02119d5 | 630 | u64 extent_end; |
e02119d5 | 631 | u64 start = key->offset; |
4bc4bee4 | 632 | u64 nbytes = 0; |
e02119d5 CM |
633 | struct btrfs_file_extent_item *item; |
634 | struct inode *inode = NULL; | |
635 | unsigned long size; | |
636 | int ret = 0; | |
637 | ||
638 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
639 | found_type = btrfs_file_extent_type(eb, item); | |
640 | ||
d899e052 | 641 | if (found_type == BTRFS_FILE_EXTENT_REG || |
4bc4bee4 JB |
642 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
643 | nbytes = btrfs_file_extent_num_bytes(eb, item); | |
644 | extent_end = start + nbytes; | |
645 | ||
646 | /* | |
647 | * We don't add to the inodes nbytes if we are prealloc or a | |
648 | * hole. | |
649 | */ | |
650 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) | |
651 | nbytes = 0; | |
652 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
e41ca589 | 653 | size = btrfs_file_extent_ram_bytes(eb, item); |
4bc4bee4 | 654 | nbytes = btrfs_file_extent_ram_bytes(eb, item); |
da17066c | 655 | extent_end = ALIGN(start + size, |
0b246afa | 656 | fs_info->sectorsize); |
e02119d5 CM |
657 | } else { |
658 | ret = 0; | |
659 | goto out; | |
660 | } | |
661 | ||
662 | inode = read_one_inode(root, key->objectid); | |
663 | if (!inode) { | |
664 | ret = -EIO; | |
665 | goto out; | |
666 | } | |
667 | ||
668 | /* | |
669 | * first check to see if we already have this extent in the | |
670 | * file. This must be done before the btrfs_drop_extents run | |
671 | * so we don't try to drop this extent. | |
672 | */ | |
f85b7379 DS |
673 | ret = btrfs_lookup_file_extent(trans, root, path, |
674 | btrfs_ino(BTRFS_I(inode)), start, 0); | |
e02119d5 | 675 | |
d899e052 YZ |
676 | if (ret == 0 && |
677 | (found_type == BTRFS_FILE_EXTENT_REG || | |
678 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { | |
e02119d5 CM |
679 | struct btrfs_file_extent_item cmp1; |
680 | struct btrfs_file_extent_item cmp2; | |
681 | struct btrfs_file_extent_item *existing; | |
682 | struct extent_buffer *leaf; | |
683 | ||
684 | leaf = path->nodes[0]; | |
685 | existing = btrfs_item_ptr(leaf, path->slots[0], | |
686 | struct btrfs_file_extent_item); | |
687 | ||
688 | read_extent_buffer(eb, &cmp1, (unsigned long)item, | |
689 | sizeof(cmp1)); | |
690 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, | |
691 | sizeof(cmp2)); | |
692 | ||
693 | /* | |
694 | * we already have a pointer to this exact extent, | |
695 | * we don't have to do anything | |
696 | */ | |
697 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { | |
b3b4aa74 | 698 | btrfs_release_path(path); |
e02119d5 CM |
699 | goto out; |
700 | } | |
701 | } | |
b3b4aa74 | 702 | btrfs_release_path(path); |
e02119d5 CM |
703 | |
704 | /* drop any overlapping extents */ | |
5893dfb9 FM |
705 | drop_args.start = start; |
706 | drop_args.end = extent_end; | |
707 | drop_args.drop_cache = true; | |
708 | ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args); | |
3650860b JB |
709 | if (ret) |
710 | goto out; | |
e02119d5 | 711 | |
07d400a6 YZ |
712 | if (found_type == BTRFS_FILE_EXTENT_REG || |
713 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
5d4f98a2 | 714 | u64 offset; |
07d400a6 YZ |
715 | unsigned long dest_offset; |
716 | struct btrfs_key ins; | |
717 | ||
3168021c FM |
718 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && |
719 | btrfs_fs_incompat(fs_info, NO_HOLES)) | |
720 | goto update_inode; | |
721 | ||
07d400a6 YZ |
722 | ret = btrfs_insert_empty_item(trans, root, path, key, |
723 | sizeof(*item)); | |
3650860b JB |
724 | if (ret) |
725 | goto out; | |
07d400a6 YZ |
726 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
727 | path->slots[0]); | |
728 | copy_extent_buffer(path->nodes[0], eb, dest_offset, | |
729 | (unsigned long)item, sizeof(*item)); | |
730 | ||
731 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); | |
732 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); | |
733 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 | 734 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
07d400a6 | 735 | |
df2c95f3 QW |
736 | /* |
737 | * Manually record dirty extent, as here we did a shallow | |
738 | * file extent item copy and skip normal backref update, | |
739 | * but modifying extent tree all by ourselves. | |
740 | * So need to manually record dirty extent for qgroup, | |
741 | * as the owner of the file extent changed from log tree | |
742 | * (doesn't affect qgroup) to fs/file tree(affects qgroup) | |
743 | */ | |
a95f3aaf | 744 | ret = btrfs_qgroup_trace_extent(trans, |
df2c95f3 | 745 | btrfs_file_extent_disk_bytenr(eb, item), |
e2896e79 | 746 | btrfs_file_extent_disk_num_bytes(eb, item)); |
df2c95f3 QW |
747 | if (ret < 0) |
748 | goto out; | |
749 | ||
07d400a6 YZ |
750 | if (ins.objectid > 0) { |
751 | u64 csum_start; | |
752 | u64 csum_end; | |
753 | LIST_HEAD(ordered_sums); | |
82fa113f | 754 | |
07d400a6 YZ |
755 | /* |
756 | * is this extent already allocated in the extent | |
757 | * allocation tree? If so, just add a reference | |
758 | */ | |
2ff7e61e | 759 | ret = btrfs_lookup_data_extent(fs_info, ins.objectid, |
07d400a6 | 760 | ins.offset); |
3736127a MPS |
761 | if (ret < 0) { |
762 | goto out; | |
763 | } else if (ret == 0) { | |
4d09b4e9 JB |
764 | struct btrfs_ref ref = { |
765 | .action = BTRFS_ADD_DELAYED_REF, | |
766 | .bytenr = ins.objectid, | |
12390e42 | 767 | .num_bytes = ins.offset, |
4d09b4e9 | 768 | .owning_root = root->root_key.objectid, |
f2e69a77 | 769 | .ref_root = root->root_key.objectid, |
4d09b4e9 | 770 | }; |
f2e69a77 JB |
771 | btrfs_init_data_ref(&ref, key->objectid, offset, |
772 | 0, false); | |
82fa113f | 773 | ret = btrfs_inc_extent_ref(trans, &ref); |
b50c6e25 JB |
774 | if (ret) |
775 | goto out; | |
07d400a6 YZ |
776 | } else { |
777 | /* | |
778 | * insert the extent pointer in the extent | |
779 | * allocation tree | |
780 | */ | |
5d4f98a2 | 781 | ret = btrfs_alloc_logged_file_extent(trans, |
2ff7e61e | 782 | root->root_key.objectid, |
5d4f98a2 | 783 | key->objectid, offset, &ins); |
b50c6e25 JB |
784 | if (ret) |
785 | goto out; | |
07d400a6 | 786 | } |
b3b4aa74 | 787 | btrfs_release_path(path); |
07d400a6 YZ |
788 | |
789 | if (btrfs_file_extent_compression(eb, item)) { | |
790 | csum_start = ins.objectid; | |
791 | csum_end = csum_start + ins.offset; | |
792 | } else { | |
793 | csum_start = ins.objectid + | |
794 | btrfs_file_extent_offset(eb, item); | |
795 | csum_end = csum_start + | |
796 | btrfs_file_extent_num_bytes(eb, item); | |
797 | } | |
798 | ||
97e38239 | 799 | ret = btrfs_lookup_csums_list(root->log_root, |
07d400a6 | 800 | csum_start, csum_end - 1, |
afcb8062 | 801 | &ordered_sums, false); |
8d2a83a9 | 802 | if (ret < 0) |
3650860b | 803 | goto out; |
8d2a83a9 | 804 | ret = 0; |
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; | |
e6b430f8 CH |
2578 | } else { |
2579 | unaccount_log_buffer(eb->fs_info, eb->start); | |
2580 | } | |
2581 | ||
2582 | return 0; | |
2583 | } | |
2584 | ||
d397712b | 2585 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
2586 | struct btrfs_root *root, |
2587 | struct btrfs_path *path, int *level, | |
2588 | struct walk_control *wc) | |
2589 | { | |
0b246afa | 2590 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 CM |
2591 | u64 bytenr; |
2592 | u64 ptr_gen; | |
2593 | struct extent_buffer *next; | |
2594 | struct extent_buffer *cur; | |
e02119d5 CM |
2595 | int ret = 0; |
2596 | ||
d397712b | 2597 | while (*level > 0) { |
789d6a3a | 2598 | struct btrfs_tree_parent_check check = { 0 }; |
581c1760 | 2599 | |
e02119d5 CM |
2600 | cur = path->nodes[*level]; |
2601 | ||
fae7f21c | 2602 | WARN_ON(btrfs_header_level(cur) != *level); |
e02119d5 CM |
2603 | |
2604 | if (path->slots[*level] >= | |
2605 | btrfs_header_nritems(cur)) | |
2606 | break; | |
2607 | ||
2608 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | |
2609 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | |
789d6a3a QW |
2610 | check.transid = ptr_gen; |
2611 | check.level = *level - 1; | |
2612 | check.has_first_key = true; | |
2613 | btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]); | |
e02119d5 | 2614 | |
3fbaf258 JB |
2615 | next = btrfs_find_create_tree_block(fs_info, bytenr, |
2616 | btrfs_header_owner(cur), | |
2617 | *level - 1); | |
c871b0f2 LB |
2618 | if (IS_ERR(next)) |
2619 | return PTR_ERR(next); | |
e02119d5 | 2620 | |
e02119d5 | 2621 | if (*level == 1) { |
581c1760 QW |
2622 | ret = wc->process_func(root, next, wc, ptr_gen, |
2623 | *level - 1); | |
b50c6e25 JB |
2624 | if (ret) { |
2625 | free_extent_buffer(next); | |
1e5063d0 | 2626 | return ret; |
b50c6e25 | 2627 | } |
4a500fd1 | 2628 | |
e02119d5 CM |
2629 | path->slots[*level]++; |
2630 | if (wc->free) { | |
789d6a3a | 2631 | ret = btrfs_read_extent_buffer(next, &check); |
018642a1 TI |
2632 | if (ret) { |
2633 | free_extent_buffer(next); | |
2634 | return ret; | |
2635 | } | |
e02119d5 | 2636 | |
e6b430f8 CH |
2637 | ret = clean_log_buffer(trans, next); |
2638 | if (ret) { | |
2639 | free_extent_buffer(next); | |
2640 | return ret; | |
3650860b | 2641 | } |
e02119d5 CM |
2642 | } |
2643 | free_extent_buffer(next); | |
2644 | continue; | |
2645 | } | |
789d6a3a | 2646 | ret = btrfs_read_extent_buffer(next, &check); |
018642a1 TI |
2647 | if (ret) { |
2648 | free_extent_buffer(next); | |
2649 | return ret; | |
2650 | } | |
e02119d5 | 2651 | |
e02119d5 CM |
2652 | if (path->nodes[*level-1]) |
2653 | free_extent_buffer(path->nodes[*level-1]); | |
2654 | path->nodes[*level-1] = next; | |
2655 | *level = btrfs_header_level(next); | |
2656 | path->slots[*level] = 0; | |
2657 | cond_resched(); | |
2658 | } | |
4a500fd1 | 2659 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
e02119d5 CM |
2660 | |
2661 | cond_resched(); | |
2662 | return 0; | |
2663 | } | |
2664 | ||
d397712b | 2665 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
2666 | struct btrfs_root *root, |
2667 | struct btrfs_path *path, int *level, | |
2668 | struct walk_control *wc) | |
2669 | { | |
e02119d5 CM |
2670 | int i; |
2671 | int slot; | |
2672 | int ret; | |
2673 | ||
d397712b | 2674 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
e02119d5 | 2675 | slot = path->slots[i]; |
4a500fd1 | 2676 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
e02119d5 CM |
2677 | path->slots[i]++; |
2678 | *level = i; | |
2679 | WARN_ON(*level == 0); | |
2680 | return 0; | |
2681 | } else { | |
1e5063d0 | 2682 | ret = wc->process_func(root, path->nodes[*level], wc, |
581c1760 QW |
2683 | btrfs_header_generation(path->nodes[*level]), |
2684 | *level); | |
1e5063d0 MF |
2685 | if (ret) |
2686 | return ret; | |
2687 | ||
e02119d5 | 2688 | if (wc->free) { |
e6b430f8 CH |
2689 | ret = clean_log_buffer(trans, path->nodes[*level]); |
2690 | if (ret) | |
2691 | return ret; | |
e02119d5 CM |
2692 | } |
2693 | free_extent_buffer(path->nodes[*level]); | |
2694 | path->nodes[*level] = NULL; | |
2695 | *level = i + 1; | |
2696 | } | |
2697 | } | |
2698 | return 1; | |
2699 | } | |
2700 | ||
2701 | /* | |
2702 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
2703 | * the tree freeing any blocks that have a ref count of zero after being | |
2704 | * decremented. | |
2705 | */ | |
2706 | static int walk_log_tree(struct btrfs_trans_handle *trans, | |
2707 | struct btrfs_root *log, struct walk_control *wc) | |
2708 | { | |
2709 | int ret = 0; | |
2710 | int wret; | |
2711 | int level; | |
2712 | struct btrfs_path *path; | |
e02119d5 CM |
2713 | int orig_level; |
2714 | ||
2715 | path = btrfs_alloc_path(); | |
db5b493a TI |
2716 | if (!path) |
2717 | return -ENOMEM; | |
e02119d5 CM |
2718 | |
2719 | level = btrfs_header_level(log->node); | |
2720 | orig_level = level; | |
2721 | path->nodes[level] = log->node; | |
67439dad | 2722 | atomic_inc(&log->node->refs); |
e02119d5 CM |
2723 | path->slots[level] = 0; |
2724 | ||
d397712b | 2725 | while (1) { |
e02119d5 CM |
2726 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
2727 | if (wret > 0) | |
2728 | break; | |
79787eaa | 2729 | if (wret < 0) { |
e02119d5 | 2730 | ret = wret; |
79787eaa JM |
2731 | goto out; |
2732 | } | |
e02119d5 CM |
2733 | |
2734 | wret = walk_up_log_tree(trans, log, path, &level, wc); | |
2735 | if (wret > 0) | |
2736 | break; | |
79787eaa | 2737 | if (wret < 0) { |
e02119d5 | 2738 | ret = wret; |
79787eaa JM |
2739 | goto out; |
2740 | } | |
e02119d5 CM |
2741 | } |
2742 | ||
2743 | /* was the root node processed? if not, catch it here */ | |
2744 | if (path->nodes[orig_level]) { | |
79787eaa | 2745 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
581c1760 QW |
2746 | btrfs_header_generation(path->nodes[orig_level]), |
2747 | orig_level); | |
79787eaa JM |
2748 | if (ret) |
2749 | goto out; | |
e6b430f8 CH |
2750 | if (wc->free) |
2751 | ret = clean_log_buffer(trans, path->nodes[orig_level]); | |
e02119d5 CM |
2752 | } |
2753 | ||
79787eaa | 2754 | out: |
e02119d5 | 2755 | btrfs_free_path(path); |
e02119d5 CM |
2756 | return ret; |
2757 | } | |
2758 | ||
7237f183 YZ |
2759 | /* |
2760 | * helper function to update the item for a given subvolumes log root | |
2761 | * in the tree of log roots | |
2762 | */ | |
2763 | static int update_log_root(struct btrfs_trans_handle *trans, | |
4203e968 JB |
2764 | struct btrfs_root *log, |
2765 | struct btrfs_root_item *root_item) | |
7237f183 | 2766 | { |
0b246afa | 2767 | struct btrfs_fs_info *fs_info = log->fs_info; |
7237f183 YZ |
2768 | int ret; |
2769 | ||
2770 | if (log->log_transid == 1) { | |
2771 | /* insert root item on the first sync */ | |
0b246afa | 2772 | ret = btrfs_insert_root(trans, fs_info->log_root_tree, |
4203e968 | 2773 | &log->root_key, root_item); |
7237f183 | 2774 | } else { |
0b246afa | 2775 | ret = btrfs_update_root(trans, fs_info->log_root_tree, |
4203e968 | 2776 | &log->root_key, root_item); |
7237f183 YZ |
2777 | } |
2778 | return ret; | |
2779 | } | |
2780 | ||
60d53eb3 | 2781 | static void wait_log_commit(struct btrfs_root *root, int transid) |
e02119d5 CM |
2782 | { |
2783 | DEFINE_WAIT(wait); | |
7237f183 | 2784 | int index = transid % 2; |
e02119d5 | 2785 | |
7237f183 YZ |
2786 | /* |
2787 | * we only allow two pending log transactions at a time, | |
2788 | * so we know that if ours is more than 2 older than the | |
2789 | * current transaction, we're done | |
2790 | */ | |
49e83f57 | 2791 | for (;;) { |
7237f183 YZ |
2792 | prepare_to_wait(&root->log_commit_wait[index], |
2793 | &wait, TASK_UNINTERRUPTIBLE); | |
12fcfd22 | 2794 | |
49e83f57 LB |
2795 | if (!(root->log_transid_committed < transid && |
2796 | atomic_read(&root->log_commit[index]))) | |
2797 | break; | |
12fcfd22 | 2798 | |
49e83f57 LB |
2799 | mutex_unlock(&root->log_mutex); |
2800 | schedule(); | |
7237f183 | 2801 | mutex_lock(&root->log_mutex); |
49e83f57 LB |
2802 | } |
2803 | finish_wait(&root->log_commit_wait[index], &wait); | |
7237f183 YZ |
2804 | } |
2805 | ||
60d53eb3 | 2806 | static void wait_for_writer(struct btrfs_root *root) |
7237f183 YZ |
2807 | { |
2808 | DEFINE_WAIT(wait); | |
8b050d35 | 2809 | |
49e83f57 LB |
2810 | for (;;) { |
2811 | prepare_to_wait(&root->log_writer_wait, &wait, | |
2812 | TASK_UNINTERRUPTIBLE); | |
2813 | if (!atomic_read(&root->log_writers)) | |
2814 | break; | |
2815 | ||
7237f183 | 2816 | mutex_unlock(&root->log_mutex); |
49e83f57 | 2817 | schedule(); |
575849ec | 2818 | mutex_lock(&root->log_mutex); |
7237f183 | 2819 | } |
49e83f57 | 2820 | finish_wait(&root->log_writer_wait, &wait); |
e02119d5 CM |
2821 | } |
2822 | ||
c207adc1 DS |
2823 | void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx, struct inode *inode) |
2824 | { | |
2825 | ctx->log_ret = 0; | |
2826 | ctx->log_transid = 0; | |
2827 | ctx->log_new_dentries = false; | |
2828 | ctx->logging_new_name = false; | |
2829 | ctx->logging_new_delayed_dentries = false; | |
2830 | ctx->logged_before = false; | |
2831 | ctx->inode = inode; | |
2832 | INIT_LIST_HEAD(&ctx->list); | |
2833 | INIT_LIST_HEAD(&ctx->ordered_extents); | |
2834 | INIT_LIST_HEAD(&ctx->conflict_inodes); | |
2835 | ctx->num_conflict_inodes = 0; | |
2836 | ctx->logging_conflict_inodes = false; | |
2837 | ctx->scratch_eb = NULL; | |
2838 | } | |
2839 | ||
2840 | void btrfs_init_log_ctx_scratch_eb(struct btrfs_log_ctx *ctx) | |
2841 | { | |
2842 | struct btrfs_inode *inode = BTRFS_I(ctx->inode); | |
2843 | ||
2844 | if (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) && | |
2845 | !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags)) | |
2846 | return; | |
2847 | ||
2848 | /* | |
2849 | * Don't care about allocation failure. This is just for optimization, | |
2850 | * if we fail to allocate here, we will try again later if needed. | |
2851 | */ | |
2852 | ctx->scratch_eb = alloc_dummy_extent_buffer(inode->root->fs_info, 0); | |
2853 | } | |
2854 | ||
2855 | void btrfs_release_log_ctx_extents(struct btrfs_log_ctx *ctx) | |
2856 | { | |
2857 | struct btrfs_ordered_extent *ordered; | |
2858 | struct btrfs_ordered_extent *tmp; | |
2859 | ||
2860 | ASSERT(inode_is_locked(ctx->inode)); | |
2861 | ||
2862 | list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { | |
2863 | list_del_init(&ordered->log_list); | |
2864 | btrfs_put_ordered_extent(ordered); | |
2865 | } | |
2866 | } | |
2867 | ||
2868 | ||
8b050d35 MX |
2869 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
2870 | struct btrfs_log_ctx *ctx) | |
2871 | { | |
8b050d35 MX |
2872 | mutex_lock(&root->log_mutex); |
2873 | list_del_init(&ctx->list); | |
2874 | mutex_unlock(&root->log_mutex); | |
2875 | } | |
2876 | ||
2877 | /* | |
2878 | * Invoked in log mutex context, or be sure there is no other task which | |
2879 | * can access the list. | |
2880 | */ | |
2881 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, | |
2882 | int index, int error) | |
2883 | { | |
2884 | struct btrfs_log_ctx *ctx; | |
570dd450 | 2885 | struct btrfs_log_ctx *safe; |
8b050d35 | 2886 | |
570dd450 CM |
2887 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
2888 | list_del_init(&ctx->list); | |
8b050d35 | 2889 | ctx->log_ret = error; |
570dd450 | 2890 | } |
8b050d35 MX |
2891 | } |
2892 | ||
e02119d5 | 2893 | /* |
9580503b DS |
2894 | * Sends a given tree log down to the disk and updates the super blocks to |
2895 | * record it. When this call is done, you know that any inodes previously | |
2896 | * logged are safely on disk only if it returns 0. | |
12fcfd22 CM |
2897 | * |
2898 | * Any other return value means you need to call btrfs_commit_transaction. | |
2899 | * Some of the edge cases for fsyncing directories that have had unlinks | |
2900 | * or renames done in the past mean that sometimes the only safe | |
2901 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, | |
2902 | * that has happened. | |
e02119d5 CM |
2903 | */ |
2904 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | |
8b050d35 | 2905 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
e02119d5 | 2906 | { |
7237f183 YZ |
2907 | int index1; |
2908 | int index2; | |
8cef4e16 | 2909 | int mark; |
e02119d5 | 2910 | int ret; |
0b246afa | 2911 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 | 2912 | struct btrfs_root *log = root->log_root; |
0b246afa | 2913 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; |
4203e968 | 2914 | struct btrfs_root_item new_root_item; |
bb14a59b | 2915 | int log_transid = 0; |
8b050d35 | 2916 | struct btrfs_log_ctx root_log_ctx; |
c6adc9cc | 2917 | struct blk_plug plug; |
47876f7c FM |
2918 | u64 log_root_start; |
2919 | u64 log_root_level; | |
e02119d5 | 2920 | |
7237f183 | 2921 | mutex_lock(&root->log_mutex); |
d1433deb MX |
2922 | log_transid = ctx->log_transid; |
2923 | if (root->log_transid_committed >= log_transid) { | |
2924 | mutex_unlock(&root->log_mutex); | |
2925 | return ctx->log_ret; | |
2926 | } | |
2927 | ||
2928 | index1 = log_transid % 2; | |
7237f183 | 2929 | if (atomic_read(&root->log_commit[index1])) { |
60d53eb3 | 2930 | wait_log_commit(root, log_transid); |
7237f183 | 2931 | mutex_unlock(&root->log_mutex); |
8b050d35 | 2932 | return ctx->log_ret; |
e02119d5 | 2933 | } |
d1433deb | 2934 | ASSERT(log_transid == root->log_transid); |
7237f183 YZ |
2935 | atomic_set(&root->log_commit[index1], 1); |
2936 | ||
2937 | /* wait for previous tree log sync to complete */ | |
2938 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) | |
60d53eb3 | 2939 | wait_log_commit(root, log_transid - 1); |
48cab2e0 | 2940 | |
86df7eb9 | 2941 | while (1) { |
2ecb7923 | 2942 | int batch = atomic_read(&root->log_batch); |
cd354ad6 | 2943 | /* when we're on an ssd, just kick the log commit out */ |
0b246afa | 2944 | if (!btrfs_test_opt(fs_info, SSD) && |
27cdeb70 | 2945 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { |
86df7eb9 YZ |
2946 | mutex_unlock(&root->log_mutex); |
2947 | schedule_timeout_uninterruptible(1); | |
2948 | mutex_lock(&root->log_mutex); | |
2949 | } | |
60d53eb3 | 2950 | wait_for_writer(root); |
2ecb7923 | 2951 | if (batch == atomic_read(&root->log_batch)) |
e02119d5 CM |
2952 | break; |
2953 | } | |
e02119d5 | 2954 | |
12fcfd22 | 2955 | /* bail out if we need to do a full commit */ |
4884b8e8 | 2956 | if (btrfs_need_log_full_commit(trans)) { |
f31f09f6 | 2957 | ret = BTRFS_LOG_FORCE_COMMIT; |
12fcfd22 CM |
2958 | mutex_unlock(&root->log_mutex); |
2959 | goto out; | |
2960 | } | |
2961 | ||
8cef4e16 YZ |
2962 | if (log_transid % 2 == 0) |
2963 | mark = EXTENT_DIRTY; | |
2964 | else | |
2965 | mark = EXTENT_NEW; | |
2966 | ||
690587d1 CM |
2967 | /* we start IO on all the marked extents here, but we don't actually |
2968 | * wait for them until later. | |
2969 | */ | |
c6adc9cc | 2970 | blk_start_plug(&plug); |
2ff7e61e | 2971 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); |
b528f467 NA |
2972 | /* |
2973 | * -EAGAIN happens when someone, e.g., a concurrent transaction | |
2974 | * commit, writes a dirty extent in this tree-log commit. This | |
2975 | * concurrent write will create a hole writing out the extents, | |
2976 | * and we cannot proceed on a zoned filesystem, requiring | |
2977 | * sequential writing. While we can bail out to a full commit | |
2978 | * here, but we can continue hoping the concurrent writing fills | |
2979 | * the hole. | |
2980 | */ | |
2981 | if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) | |
2982 | ret = 0; | |
79787eaa | 2983 | if (ret) { |
c6adc9cc | 2984 | blk_finish_plug(&plug); |
90787766 | 2985 | btrfs_set_log_full_commit(trans); |
79787eaa JM |
2986 | mutex_unlock(&root->log_mutex); |
2987 | goto out; | |
2988 | } | |
7237f183 | 2989 | |
4203e968 JB |
2990 | /* |
2991 | * We _must_ update under the root->log_mutex in order to make sure we | |
2992 | * have a consistent view of the log root we are trying to commit at | |
2993 | * this moment. | |
2994 | * | |
2995 | * We _must_ copy this into a local copy, because we are not holding the | |
2996 | * log_root_tree->log_mutex yet. This is important because when we | |
2997 | * commit the log_root_tree we must have a consistent view of the | |
2998 | * log_root_tree when we update the super block to point at the | |
2999 | * log_root_tree bytenr. If we update the log_root_tree here we'll race | |
3000 | * with the commit and possibly point at the new block which we may not | |
3001 | * have written out. | |
3002 | */ | |
5d4f98a2 | 3003 | btrfs_set_root_node(&log->root_item, log->node); |
4203e968 | 3004 | memcpy(&new_root_item, &log->root_item, sizeof(new_root_item)); |
7237f183 | 3005 | |
6008859b | 3006 | btrfs_set_root_log_transid(root, root->log_transid + 1); |
7237f183 | 3007 | log->log_transid = root->log_transid; |
ff782e0a | 3008 | root->log_start_pid = 0; |
7237f183 | 3009 | /* |
8cef4e16 YZ |
3010 | * IO has been started, blocks of the log tree have WRITTEN flag set |
3011 | * in their headers. new modifications of the log will be written to | |
3012 | * new positions. so it's safe to allow log writers to go in. | |
7237f183 YZ |
3013 | */ |
3014 | mutex_unlock(&root->log_mutex); | |
3015 | ||
3ddebf27 | 3016 | if (btrfs_is_zoned(fs_info)) { |
e75f9fd1 | 3017 | mutex_lock(&fs_info->tree_root->log_mutex); |
3ddebf27 NA |
3018 | if (!log_root_tree->node) { |
3019 | ret = btrfs_alloc_log_tree_node(trans, log_root_tree); | |
3020 | if (ret) { | |
ea32af47 | 3021 | mutex_unlock(&fs_info->tree_root->log_mutex); |
50ff5788 | 3022 | blk_finish_plug(&plug); |
3ddebf27 NA |
3023 | goto out; |
3024 | } | |
3025 | } | |
e75f9fd1 | 3026 | mutex_unlock(&fs_info->tree_root->log_mutex); |
3ddebf27 NA |
3027 | } |
3028 | ||
e75f9fd1 NA |
3029 | btrfs_init_log_ctx(&root_log_ctx, NULL); |
3030 | ||
3031 | mutex_lock(&log_root_tree->log_mutex); | |
3032 | ||
e3d3b415 FM |
3033 | index2 = log_root_tree->log_transid % 2; |
3034 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); | |
3035 | root_log_ctx.log_transid = log_root_tree->log_transid; | |
3036 | ||
4203e968 JB |
3037 | /* |
3038 | * Now we are safe to update the log_root_tree because we're under the | |
3039 | * log_mutex, and we're a current writer so we're holding the commit | |
3040 | * open until we drop the log_mutex. | |
3041 | */ | |
3042 | ret = update_log_root(trans, log, &new_root_item); | |
4a500fd1 | 3043 | if (ret) { |
3cf63ddf | 3044 | list_del_init(&root_log_ctx.list); |
c6adc9cc | 3045 | blk_finish_plug(&plug); |
90787766 | 3046 | btrfs_set_log_full_commit(trans); |
09e44868 FM |
3047 | if (ret != -ENOSPC) |
3048 | btrfs_err(fs_info, | |
3049 | "failed to update log for root %llu ret %d", | |
3050 | root->root_key.objectid, ret); | |
bf89d38f | 3051 | btrfs_wait_tree_log_extents(log, mark); |
4a500fd1 | 3052 | mutex_unlock(&log_root_tree->log_mutex); |
4a500fd1 YZ |
3053 | goto out; |
3054 | } | |
3055 | ||
d1433deb | 3056 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
3da5ab56 | 3057 | blk_finish_plug(&plug); |
cbd60aa7 | 3058 | list_del_init(&root_log_ctx.list); |
d1433deb MX |
3059 | mutex_unlock(&log_root_tree->log_mutex); |
3060 | ret = root_log_ctx.log_ret; | |
3061 | goto out; | |
3062 | } | |
8b050d35 | 3063 | |
7237f183 | 3064 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
c6adc9cc | 3065 | blk_finish_plug(&plug); |
bf89d38f | 3066 | ret = btrfs_wait_tree_log_extents(log, mark); |
60d53eb3 | 3067 | wait_log_commit(log_root_tree, |
d1433deb | 3068 | root_log_ctx.log_transid); |
7237f183 | 3069 | mutex_unlock(&log_root_tree->log_mutex); |
5ab5e44a FM |
3070 | if (!ret) |
3071 | ret = root_log_ctx.log_ret; | |
7237f183 YZ |
3072 | goto out; |
3073 | } | |
d1433deb | 3074 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
7237f183 YZ |
3075 | atomic_set(&log_root_tree->log_commit[index2], 1); |
3076 | ||
12fcfd22 | 3077 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
60d53eb3 | 3078 | wait_log_commit(log_root_tree, |
d1433deb | 3079 | root_log_ctx.log_transid - 1); |
12fcfd22 CM |
3080 | } |
3081 | ||
12fcfd22 CM |
3082 | /* |
3083 | * now that we've moved on to the tree of log tree roots, | |
3084 | * check the full commit flag again | |
3085 | */ | |
4884b8e8 | 3086 | if (btrfs_need_log_full_commit(trans)) { |
c6adc9cc | 3087 | blk_finish_plug(&plug); |
bf89d38f | 3088 | btrfs_wait_tree_log_extents(log, mark); |
12fcfd22 | 3089 | mutex_unlock(&log_root_tree->log_mutex); |
f31f09f6 | 3090 | ret = BTRFS_LOG_FORCE_COMMIT; |
12fcfd22 CM |
3091 | goto out_wake_log_root; |
3092 | } | |
7237f183 | 3093 | |
2ff7e61e | 3094 | ret = btrfs_write_marked_extents(fs_info, |
c6adc9cc MX |
3095 | &log_root_tree->dirty_log_pages, |
3096 | EXTENT_DIRTY | EXTENT_NEW); | |
3097 | blk_finish_plug(&plug); | |
b528f467 NA |
3098 | /* |
3099 | * As described above, -EAGAIN indicates a hole in the extents. We | |
3100 | * cannot wait for these write outs since the waiting cause a | |
3101 | * deadlock. Bail out to the full commit instead. | |
3102 | */ | |
3103 | if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) { | |
3104 | btrfs_set_log_full_commit(trans); | |
3105 | btrfs_wait_tree_log_extents(log, mark); | |
3106 | mutex_unlock(&log_root_tree->log_mutex); | |
3107 | goto out_wake_log_root; | |
3108 | } else if (ret) { | |
90787766 | 3109 | btrfs_set_log_full_commit(trans); |
79787eaa JM |
3110 | mutex_unlock(&log_root_tree->log_mutex); |
3111 | goto out_wake_log_root; | |
3112 | } | |
bf89d38f | 3113 | ret = btrfs_wait_tree_log_extents(log, mark); |
5ab5e44a | 3114 | if (!ret) |
bf89d38f JM |
3115 | ret = btrfs_wait_tree_log_extents(log_root_tree, |
3116 | EXTENT_NEW | EXTENT_DIRTY); | |
5ab5e44a | 3117 | if (ret) { |
90787766 | 3118 | btrfs_set_log_full_commit(trans); |
5ab5e44a FM |
3119 | mutex_unlock(&log_root_tree->log_mutex); |
3120 | goto out_wake_log_root; | |
3121 | } | |
e02119d5 | 3122 | |
47876f7c FM |
3123 | log_root_start = log_root_tree->node->start; |
3124 | log_root_level = btrfs_header_level(log_root_tree->node); | |
7237f183 | 3125 | log_root_tree->log_transid++; |
7237f183 YZ |
3126 | mutex_unlock(&log_root_tree->log_mutex); |
3127 | ||
3128 | /* | |
47876f7c FM |
3129 | * Here we are guaranteed that nobody is going to write the superblock |
3130 | * for the current transaction before us and that neither we do write | |
3131 | * our superblock before the previous transaction finishes its commit | |
3132 | * and writes its superblock, because: | |
3133 | * | |
3134 | * 1) We are holding a handle on the current transaction, so no body | |
3135 | * can commit it until we release the handle; | |
3136 | * | |
3137 | * 2) Before writing our superblock we acquire the tree_log_mutex, so | |
3138 | * if the previous transaction is still committing, and hasn't yet | |
3139 | * written its superblock, we wait for it to do it, because a | |
3140 | * transaction commit acquires the tree_log_mutex when the commit | |
3141 | * begins and releases it only after writing its superblock. | |
7237f183 | 3142 | */ |
47876f7c | 3143 | mutex_lock(&fs_info->tree_log_mutex); |
165ea85f JB |
3144 | |
3145 | /* | |
3146 | * The previous transaction writeout phase could have failed, and thus | |
3147 | * marked the fs in an error state. We must not commit here, as we | |
3148 | * could have updated our generation in the super_for_commit and | |
3149 | * writing the super here would result in transid mismatches. If there | |
3150 | * is an error here just bail. | |
3151 | */ | |
84961539 | 3152 | if (BTRFS_FS_ERROR(fs_info)) { |
165ea85f JB |
3153 | ret = -EIO; |
3154 | btrfs_set_log_full_commit(trans); | |
3155 | btrfs_abort_transaction(trans, ret); | |
3156 | mutex_unlock(&fs_info->tree_log_mutex); | |
3157 | goto out_wake_log_root; | |
3158 | } | |
3159 | ||
47876f7c FM |
3160 | btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start); |
3161 | btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level); | |
eece6a9c | 3162 | ret = write_all_supers(fs_info, 1); |
47876f7c | 3163 | mutex_unlock(&fs_info->tree_log_mutex); |
5af3e8cc | 3164 | if (ret) { |
90787766 | 3165 | btrfs_set_log_full_commit(trans); |
66642832 | 3166 | btrfs_abort_transaction(trans, ret); |
5af3e8cc SB |
3167 | goto out_wake_log_root; |
3168 | } | |
7237f183 | 3169 | |
e1a6d264 FM |
3170 | /* |
3171 | * We know there can only be one task here, since we have not yet set | |
3172 | * root->log_commit[index1] to 0 and any task attempting to sync the | |
3173 | * log must wait for the previous log transaction to commit if it's | |
3174 | * still in progress or wait for the current log transaction commit if | |
3175 | * someone else already started it. We use <= and not < because the | |
3176 | * first log transaction has an ID of 0. | |
3177 | */ | |
f9850787 FM |
3178 | ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid); |
3179 | btrfs_set_root_last_log_commit(root, log_transid); | |
257c62e1 | 3180 | |
12fcfd22 | 3181 | out_wake_log_root: |
570dd450 | 3182 | mutex_lock(&log_root_tree->log_mutex); |
8b050d35 MX |
3183 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
3184 | ||
d1433deb | 3185 | log_root_tree->log_transid_committed++; |
7237f183 | 3186 | atomic_set(&log_root_tree->log_commit[index2], 0); |
d1433deb MX |
3187 | mutex_unlock(&log_root_tree->log_mutex); |
3188 | ||
33a9eca7 | 3189 | /* |
093258e6 DS |
3190 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
3191 | * all the updates above are seen by the woken threads. It might not be | |
3192 | * necessary, but proving that seems to be hard. | |
33a9eca7 | 3193 | */ |
093258e6 | 3194 | cond_wake_up(&log_root_tree->log_commit_wait[index2]); |
e02119d5 | 3195 | out: |
d1433deb | 3196 | mutex_lock(&root->log_mutex); |
570dd450 | 3197 | btrfs_remove_all_log_ctxs(root, index1, ret); |
d1433deb | 3198 | root->log_transid_committed++; |
7237f183 | 3199 | atomic_set(&root->log_commit[index1], 0); |
d1433deb | 3200 | mutex_unlock(&root->log_mutex); |
8b050d35 | 3201 | |
33a9eca7 | 3202 | /* |
093258e6 DS |
3203 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
3204 | * all the updates above are seen by the woken threads. It might not be | |
3205 | * necessary, but proving that seems to be hard. | |
33a9eca7 | 3206 | */ |
093258e6 | 3207 | cond_wake_up(&root->log_commit_wait[index1]); |
b31eabd8 | 3208 | return ret; |
e02119d5 CM |
3209 | } |
3210 | ||
4a500fd1 YZ |
3211 | static void free_log_tree(struct btrfs_trans_handle *trans, |
3212 | struct btrfs_root *log) | |
e02119d5 CM |
3213 | { |
3214 | int ret; | |
e02119d5 CM |
3215 | struct walk_control wc = { |
3216 | .free = 1, | |
3217 | .process_func = process_one_buffer | |
3218 | }; | |
3219 | ||
3ddebf27 NA |
3220 | if (log->node) { |
3221 | ret = walk_log_tree(trans, log, &wc); | |
3222 | if (ret) { | |
40cdc509 FM |
3223 | /* |
3224 | * We weren't able to traverse the entire log tree, the | |
3225 | * typical scenario is getting an -EIO when reading an | |
3226 | * extent buffer of the tree, due to a previous writeback | |
3227 | * failure of it. | |
3228 | */ | |
3229 | set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, | |
3230 | &log->fs_info->fs_state); | |
3231 | ||
3232 | /* | |
3233 | * Some extent buffers of the log tree may still be dirty | |
3234 | * and not yet written back to storage, because we may | |
3235 | * have updates to a log tree without syncing a log tree, | |
3236 | * such as during rename and link operations. So flush | |
3237 | * them out and wait for their writeback to complete, so | |
3238 | * that we properly cleanup their state and pages. | |
3239 | */ | |
3240 | btrfs_write_marked_extents(log->fs_info, | |
3241 | &log->dirty_log_pages, | |
3242 | EXTENT_DIRTY | EXTENT_NEW); | |
3243 | btrfs_wait_tree_log_extents(log, | |
3244 | EXTENT_DIRTY | EXTENT_NEW); | |
3245 | ||
3ddebf27 NA |
3246 | if (trans) |
3247 | btrfs_abort_transaction(trans, ret); | |
3248 | else | |
3249 | btrfs_handle_fs_error(log->fs_info, ret, NULL); | |
3250 | } | |
374b0e2d | 3251 | } |
e02119d5 | 3252 | |
0f8ac74d | 3253 | extent_io_tree_release(&log->dirty_log_pages); |
e289f03e | 3254 | extent_io_tree_release(&log->log_csum_range); |
d3575156 | 3255 | |
00246528 | 3256 | btrfs_put_root(log); |
4a500fd1 YZ |
3257 | } |
3258 | ||
3259 | /* | |
3260 | * free all the extents used by the tree log. This should be called | |
3261 | * at commit time of the full transaction | |
3262 | */ | |
3263 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) | |
3264 | { | |
3265 | if (root->log_root) { | |
3266 | free_log_tree(trans, root->log_root); | |
3267 | root->log_root = NULL; | |
e7a79811 | 3268 | clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); |
4a500fd1 YZ |
3269 | } |
3270 | return 0; | |
3271 | } | |
3272 | ||
3273 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, | |
3274 | struct btrfs_fs_info *fs_info) | |
3275 | { | |
3276 | if (fs_info->log_root_tree) { | |
3277 | free_log_tree(trans, fs_info->log_root_tree); | |
3278 | fs_info->log_root_tree = NULL; | |
47876f7c | 3279 | clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state); |
4a500fd1 | 3280 | } |
e02119d5 CM |
3281 | return 0; |
3282 | } | |
3283 | ||
803f0f64 | 3284 | /* |
0f8ce498 FM |
3285 | * Check if an inode was logged in the current transaction. This correctly deals |
3286 | * with the case where the inode was logged but has a logged_trans of 0, which | |
3287 | * happens if the inode is evicted and loaded again, as logged_trans is an in | |
3288 | * memory only field (not persisted). | |
3289 | * | |
3290 | * Returns 1 if the inode was logged before in the transaction, 0 if it was not, | |
3291 | * and < 0 on error. | |
803f0f64 | 3292 | */ |
bf1f4fd3 | 3293 | static int inode_logged(const struct btrfs_trans_handle *trans, |
0f8ce498 FM |
3294 | struct btrfs_inode *inode, |
3295 | struct btrfs_path *path_in) | |
803f0f64 | 3296 | { |
0f8ce498 FM |
3297 | struct btrfs_path *path = path_in; |
3298 | struct btrfs_key key; | |
3299 | int ret; | |
3300 | ||
803f0f64 | 3301 | if (inode->logged_trans == trans->transid) |
0f8ce498 | 3302 | return 1; |
803f0f64 | 3303 | |
0f8ce498 FM |
3304 | /* |
3305 | * If logged_trans is not 0, then we know the inode logged was not logged | |
3306 | * in this transaction, so we can return false right away. | |
3307 | */ | |
3308 | if (inode->logged_trans > 0) | |
3309 | return 0; | |
3310 | ||
3311 | /* | |
3312 | * If no log tree was created for this root in this transaction, then | |
3313 | * the inode can not have been logged in this transaction. In that case | |
3314 | * set logged_trans to anything greater than 0 and less than the current | |
3315 | * transaction's ID, to avoid the search below in a future call in case | |
3316 | * a log tree gets created after this. | |
3317 | */ | |
3318 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state)) { | |
3319 | inode->logged_trans = trans->transid - 1; | |
3320 | return 0; | |
3321 | } | |
3322 | ||
3323 | /* | |
3324 | * We have a log tree and the inode's logged_trans is 0. We can't tell | |
3325 | * for sure if the inode was logged before in this transaction by looking | |
3326 | * only at logged_trans. We could be pessimistic and assume it was, but | |
3327 | * that can lead to unnecessarily logging an inode during rename and link | |
3328 | * operations, and then further updating the log in followup rename and | |
3329 | * link operations, specially if it's a directory, which adds latency | |
3330 | * visible to applications doing a series of rename or link operations. | |
3331 | * | |
3332 | * A logged_trans of 0 here can mean several things: | |
3333 | * | |
3334 | * 1) The inode was never logged since the filesystem was mounted, and may | |
3335 | * or may have not been evicted and loaded again; | |
3336 | * | |
3337 | * 2) The inode was logged in a previous transaction, then evicted and | |
3338 | * then loaded again; | |
3339 | * | |
3340 | * 3) The inode was logged in the current transaction, then evicted and | |
3341 | * then loaded again. | |
3342 | * | |
3343 | * For cases 1) and 2) we don't want to return true, but we need to detect | |
3344 | * case 3) and return true. So we do a search in the log root for the inode | |
3345 | * item. | |
3346 | */ | |
3347 | key.objectid = btrfs_ino(inode); | |
3348 | key.type = BTRFS_INODE_ITEM_KEY; | |
3349 | key.offset = 0; | |
3350 | ||
3351 | if (!path) { | |
3352 | path = btrfs_alloc_path(); | |
3353 | if (!path) | |
3354 | return -ENOMEM; | |
3355 | } | |
3356 | ||
3357 | ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); | |
3358 | ||
3359 | if (path_in) | |
3360 | btrfs_release_path(path); | |
3361 | else | |
3362 | btrfs_free_path(path); | |
1e0860f3 | 3363 | |
6e8e777d | 3364 | /* |
0f8ce498 FM |
3365 | * Logging an inode always results in logging its inode item. So if we |
3366 | * did not find the item we know the inode was not logged for sure. | |
6e8e777d | 3367 | */ |
0f8ce498 FM |
3368 | if (ret < 0) { |
3369 | return ret; | |
3370 | } else if (ret > 0) { | |
3371 | /* | |
3372 | * Set logged_trans to a value greater than 0 and less then the | |
3373 | * current transaction to avoid doing the search in future calls. | |
3374 | */ | |
3375 | inode->logged_trans = trans->transid - 1; | |
3376 | return 0; | |
3377 | } | |
3378 | ||
3379 | /* | |
3380 | * The inode was previously logged and then evicted, set logged_trans to | |
3381 | * the current transacion's ID, to avoid future tree searches as long as | |
3382 | * the inode is not evicted again. | |
3383 | */ | |
3384 | inode->logged_trans = trans->transid; | |
3385 | ||
3386 | /* | |
3387 | * If it's a directory, then we must set last_dir_index_offset to the | |
3388 | * maximum possible value, so that the next attempt to log the inode does | |
3389 | * not skip checking if dir index keys found in modified subvolume tree | |
3390 | * leaves have been logged before, otherwise it would result in attempts | |
3391 | * to insert duplicate dir index keys in the log tree. This must be done | |
3392 | * because last_dir_index_offset is an in-memory only field, not persisted | |
3393 | * in the inode item or any other on-disk structure, so its value is lost | |
3394 | * once the inode is evicted. | |
3395 | */ | |
3396 | if (S_ISDIR(inode->vfs_inode.i_mode)) | |
3397 | inode->last_dir_index_offset = (u64)-1; | |
803f0f64 | 3398 | |
0f8ce498 | 3399 | return 1; |
803f0f64 FM |
3400 | } |
3401 | ||
839061fe FM |
3402 | /* |
3403 | * Delete a directory entry from the log if it exists. | |
3404 | * | |
3405 | * Returns < 0 on error | |
3406 | * 1 if the entry does not exists | |
3407 | * 0 if the entry existed and was successfully deleted | |
3408 | */ | |
3409 | static int del_logged_dentry(struct btrfs_trans_handle *trans, | |
3410 | struct btrfs_root *log, | |
3411 | struct btrfs_path *path, | |
3412 | u64 dir_ino, | |
6db75318 | 3413 | const struct fscrypt_str *name, |
839061fe FM |
3414 | u64 index) |
3415 | { | |
3416 | struct btrfs_dir_item *di; | |
3417 | ||
3418 | /* | |
3419 | * We only log dir index items of a directory, so we don't need to look | |
3420 | * for dir item keys. | |
3421 | */ | |
3422 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, | |
e43eec81 | 3423 | index, name, -1); |
839061fe FM |
3424 | if (IS_ERR(di)) |
3425 | return PTR_ERR(di); | |
3426 | else if (!di) | |
3427 | return 1; | |
3428 | ||
3429 | /* | |
3430 | * We do not need to update the size field of the directory's | |
3431 | * inode item because on log replay we update the field to reflect | |
3432 | * all existing entries in the directory (see overwrite_item()). | |
3433 | */ | |
3434 | return btrfs_delete_one_dir_name(trans, log, path, di); | |
3435 | } | |
3436 | ||
e02119d5 CM |
3437 | /* |
3438 | * If both a file and directory are logged, and unlinks or renames are | |
3439 | * mixed in, we have a few interesting corners: | |
3440 | * | |
3441 | * create file X in dir Y | |
3442 | * link file X to X.link in dir Y | |
3443 | * fsync file X | |
3444 | * unlink file X but leave X.link | |
3445 | * fsync dir Y | |
3446 | * | |
3447 | * After a crash we would expect only X.link to exist. But file X | |
3448 | * didn't get fsync'd again so the log has back refs for X and X.link. | |
3449 | * | |
3450 | * We solve this by removing directory entries and inode backrefs from the | |
3451 | * log when a file that was logged in the current transaction is | |
3452 | * unlinked. Any later fsync will include the updated log entries, and | |
3453 | * we'll be able to reconstruct the proper directory items from backrefs. | |
3454 | * | |
3455 | * This optimizations allows us to avoid relogging the entire inode | |
3456 | * or the entire directory. | |
3457 | */ | |
9a35fc95 JB |
3458 | void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
3459 | struct btrfs_root *root, | |
6db75318 | 3460 | const struct fscrypt_str *name, |
9a35fc95 | 3461 | struct btrfs_inode *dir, u64 index) |
e02119d5 | 3462 | { |
e02119d5 CM |
3463 | struct btrfs_path *path; |
3464 | int ret; | |
e02119d5 | 3465 | |
0f8ce498 FM |
3466 | ret = inode_logged(trans, dir, NULL); |
3467 | if (ret == 0) | |
3468 | return; | |
3469 | else if (ret < 0) { | |
3470 | btrfs_set_log_full_commit(trans); | |
9a35fc95 | 3471 | return; |
0f8ce498 | 3472 | } |
3a5f1d45 | 3473 | |
e02119d5 CM |
3474 | ret = join_running_log_trans(root); |
3475 | if (ret) | |
9a35fc95 | 3476 | return; |
e02119d5 | 3477 | |
49f34d1f | 3478 | mutex_lock(&dir->log_mutex); |
e02119d5 | 3479 | |
e02119d5 | 3480 | path = btrfs_alloc_path(); |
a62f44a5 | 3481 | if (!path) { |
839061fe | 3482 | ret = -ENOMEM; |
a62f44a5 TI |
3483 | goto out_unlock; |
3484 | } | |
2a29edc6 | 3485 | |
839061fe | 3486 | ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir), |
e43eec81 | 3487 | name, index); |
e02119d5 | 3488 | btrfs_free_path(path); |
a62f44a5 | 3489 | out_unlock: |
49f34d1f | 3490 | mutex_unlock(&dir->log_mutex); |
839061fe | 3491 | if (ret < 0) |
90787766 | 3492 | btrfs_set_log_full_commit(trans); |
12fcfd22 | 3493 | btrfs_end_log_trans(root); |
e02119d5 CM |
3494 | } |
3495 | ||
3496 | /* see comments for btrfs_del_dir_entries_in_log */ | |
9a35fc95 JB |
3497 | void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
3498 | struct btrfs_root *root, | |
6db75318 | 3499 | const struct fscrypt_str *name, |
9a35fc95 | 3500 | struct btrfs_inode *inode, u64 dirid) |
e02119d5 CM |
3501 | { |
3502 | struct btrfs_root *log; | |
3503 | u64 index; | |
3504 | int ret; | |
3505 | ||
0f8ce498 FM |
3506 | ret = inode_logged(trans, inode, NULL); |
3507 | if (ret == 0) | |
9a35fc95 | 3508 | return; |
0f8ce498 FM |
3509 | else if (ret < 0) { |
3510 | btrfs_set_log_full_commit(trans); | |
3511 | return; | |
3512 | } | |
3a5f1d45 | 3513 | |
e02119d5 CM |
3514 | ret = join_running_log_trans(root); |
3515 | if (ret) | |
9a35fc95 | 3516 | return; |
e02119d5 | 3517 | log = root->log_root; |
a491abb2 | 3518 | mutex_lock(&inode->log_mutex); |
e02119d5 | 3519 | |
e43eec81 | 3520 | ret = btrfs_del_inode_ref(trans, log, name, btrfs_ino(inode), |
e02119d5 | 3521 | dirid, &index); |
a491abb2 | 3522 | mutex_unlock(&inode->log_mutex); |
9a35fc95 | 3523 | if (ret < 0 && ret != -ENOENT) |
90787766 | 3524 | btrfs_set_log_full_commit(trans); |
12fcfd22 | 3525 | btrfs_end_log_trans(root); |
e02119d5 CM |
3526 | } |
3527 | ||
3528 | /* | |
3529 | * creates a range item in the log for 'dirid'. first_offset and | |
3530 | * last_offset tell us which parts of the key space the log should | |
3531 | * be considered authoritative for. | |
3532 | */ | |
3533 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, | |
3534 | struct btrfs_root *log, | |
3535 | struct btrfs_path *path, | |
339d0354 | 3536 | u64 dirid, |
e02119d5 CM |
3537 | u64 first_offset, u64 last_offset) |
3538 | { | |
3539 | int ret; | |
3540 | struct btrfs_key key; | |
3541 | struct btrfs_dir_log_item *item; | |
3542 | ||
3543 | key.objectid = dirid; | |
3544 | key.offset = first_offset; | |
339d0354 | 3545 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
e02119d5 | 3546 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
750ee454 FM |
3547 | /* |
3548 | * -EEXIST is fine and can happen sporadically when we are logging a | |
3549 | * directory and have concurrent insertions in the subvolume's tree for | |
3550 | * items from other inodes and that result in pushing off some dir items | |
3551 | * from one leaf to another in order to accommodate for the new items. | |
3552 | * This results in logging the same dir index range key. | |
3553 | */ | |
3554 | if (ret && ret != -EEXIST) | |
4a500fd1 | 3555 | return ret; |
e02119d5 CM |
3556 | |
3557 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3558 | struct btrfs_dir_log_item); | |
750ee454 FM |
3559 | if (ret == -EEXIST) { |
3560 | const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item); | |
3561 | ||
3562 | /* | |
3563 | * btrfs_del_dir_entries_in_log() might have been called during | |
3564 | * an unlink between the initial insertion of this key and the | |
3565 | * current update, or we might be logging a single entry deletion | |
3566 | * during a rename, so set the new last_offset to the max value. | |
3567 | */ | |
3568 | last_offset = max(last_offset, curr_end); | |
3569 | } | |
e02119d5 | 3570 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
50564b65 | 3571 | btrfs_mark_buffer_dirty(trans, path->nodes[0]); |
b3b4aa74 | 3572 | btrfs_release_path(path); |
e02119d5 CM |
3573 | return 0; |
3574 | } | |
3575 | ||
086dcbfa | 3576 | static int flush_dir_items_batch(struct btrfs_trans_handle *trans, |
6afaed53 | 3577 | struct btrfs_inode *inode, |
086dcbfa FM |
3578 | struct extent_buffer *src, |
3579 | struct btrfs_path *dst_path, | |
3580 | int start_slot, | |
3581 | int count) | |
3582 | { | |
6afaed53 | 3583 | struct btrfs_root *log = inode->root->log_root; |
086dcbfa | 3584 | char *ins_data = NULL; |
b7ef5f3a | 3585 | struct btrfs_item_batch batch; |
086dcbfa | 3586 | struct extent_buffer *dst; |
da1b811f FM |
3587 | unsigned long src_offset; |
3588 | unsigned long dst_offset; | |
6afaed53 | 3589 | u64 last_index; |
086dcbfa FM |
3590 | struct btrfs_key key; |
3591 | u32 item_size; | |
3592 | int ret; | |
3593 | int i; | |
3594 | ||
3595 | ASSERT(count > 0); | |
b7ef5f3a | 3596 | batch.nr = count; |
086dcbfa FM |
3597 | |
3598 | if (count == 1) { | |
3599 | btrfs_item_key_to_cpu(src, &key, start_slot); | |
3212fa14 | 3600 | item_size = btrfs_item_size(src, start_slot); |
b7ef5f3a FM |
3601 | batch.keys = &key; |
3602 | batch.data_sizes = &item_size; | |
3603 | batch.total_data_size = item_size; | |
086dcbfa | 3604 | } else { |
b7ef5f3a FM |
3605 | struct btrfs_key *ins_keys; |
3606 | u32 *ins_sizes; | |
3607 | ||
086dcbfa FM |
3608 | ins_data = kmalloc(count * sizeof(u32) + |
3609 | count * sizeof(struct btrfs_key), GFP_NOFS); | |
3610 | if (!ins_data) | |
3611 | return -ENOMEM; | |
3612 | ||
3613 | ins_sizes = (u32 *)ins_data; | |
3614 | ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32)); | |
b7ef5f3a FM |
3615 | batch.keys = ins_keys; |
3616 | batch.data_sizes = ins_sizes; | |
3617 | batch.total_data_size = 0; | |
086dcbfa FM |
3618 | |
3619 | for (i = 0; i < count; i++) { | |
3620 | const int slot = start_slot + i; | |
3621 | ||
3622 | btrfs_item_key_to_cpu(src, &ins_keys[i], slot); | |
3212fa14 | 3623 | ins_sizes[i] = btrfs_item_size(src, slot); |
b7ef5f3a | 3624 | batch.total_data_size += ins_sizes[i]; |
086dcbfa FM |
3625 | } |
3626 | } | |
3627 | ||
b7ef5f3a | 3628 | ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); |
086dcbfa FM |
3629 | if (ret) |
3630 | goto out; | |
3631 | ||
3632 | dst = dst_path->nodes[0]; | |
da1b811f FM |
3633 | /* |
3634 | * Copy all the items in bulk, in a single copy operation. Item data is | |
3635 | * organized such that it's placed at the end of a leaf and from right | |
3636 | * to left. For example, the data for the second item ends at an offset | |
3637 | * that matches the offset where the data for the first item starts, the | |
3638 | * data for the third item ends at an offset that matches the offset | |
3639 | * where the data of the second items starts, and so on. | |
3640 | * Therefore our source and destination start offsets for copy match the | |
3641 | * offsets of the last items (highest slots). | |
3642 | */ | |
3643 | dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1); | |
3644 | src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1); | |
3645 | copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size); | |
086dcbfa | 3646 | btrfs_release_path(dst_path); |
6afaed53 FM |
3647 | |
3648 | last_index = batch.keys[count - 1].offset; | |
3649 | ASSERT(last_index > inode->last_dir_index_offset); | |
3650 | ||
3651 | /* | |
3652 | * If for some unexpected reason the last item's index is not greater | |
5cce1780 | 3653 | * than the last index we logged, warn and force a transaction commit. |
6afaed53 FM |
3654 | */ |
3655 | if (WARN_ON(last_index <= inode->last_dir_index_offset)) | |
5cce1780 | 3656 | ret = BTRFS_LOG_FORCE_COMMIT; |
6afaed53 FM |
3657 | else |
3658 | inode->last_dir_index_offset = last_index; | |
fa4b8cb1 FM |
3659 | |
3660 | if (btrfs_get_first_dir_index_to_log(inode) == 0) | |
3661 | btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset); | |
086dcbfa FM |
3662 | out: |
3663 | kfree(ins_data); | |
3664 | ||
3665 | return ret; | |
3666 | } | |
3667 | ||
e383e158 FM |
3668 | static int clone_leaf(struct btrfs_path *path, struct btrfs_log_ctx *ctx) |
3669 | { | |
3670 | const int slot = path->slots[0]; | |
3671 | ||
3672 | if (ctx->scratch_eb) { | |
3673 | copy_extent_buffer_full(ctx->scratch_eb, path->nodes[0]); | |
3674 | } else { | |
3675 | ctx->scratch_eb = btrfs_clone_extent_buffer(path->nodes[0]); | |
3676 | if (!ctx->scratch_eb) | |
3677 | return -ENOMEM; | |
3678 | } | |
3679 | ||
3680 | btrfs_release_path(path); | |
3681 | path->nodes[0] = ctx->scratch_eb; | |
3682 | path->slots[0] = slot; | |
3683 | /* | |
3684 | * Add extra ref to scratch eb so that it is not freed when callers | |
3685 | * release the path, so we can reuse it later if needed. | |
3686 | */ | |
3687 | atomic_inc(&ctx->scratch_eb->refs); | |
3688 | ||
3689 | return 0; | |
3690 | } | |
3691 | ||
eb10d85e FM |
3692 | static int process_dir_items_leaf(struct btrfs_trans_handle *trans, |
3693 | struct btrfs_inode *inode, | |
3694 | struct btrfs_path *path, | |
3695 | struct btrfs_path *dst_path, | |
732d591a FM |
3696 | struct btrfs_log_ctx *ctx, |
3697 | u64 *last_old_dentry_offset) | |
eb10d85e FM |
3698 | { |
3699 | struct btrfs_root *log = inode->root->log_root; | |
796787c9 FM |
3700 | struct extent_buffer *src; |
3701 | const int nritems = btrfs_header_nritems(path->nodes[0]); | |
eb10d85e | 3702 | const u64 ino = btrfs_ino(inode); |
086dcbfa FM |
3703 | bool last_found = false; |
3704 | int batch_start = 0; | |
3705 | int batch_size = 0; | |
e383e158 | 3706 | int ret; |
eb10d85e | 3707 | |
796787c9 FM |
3708 | /* |
3709 | * We need to clone the leaf, release the read lock on it, and use the | |
3710 | * clone before modifying the log tree. See the comment at copy_items() | |
3711 | * about why we need to do this. | |
3712 | */ | |
e383e158 FM |
3713 | ret = clone_leaf(path, ctx); |
3714 | if (ret < 0) | |
3715 | return ret; | |
796787c9 | 3716 | |
e383e158 | 3717 | src = path->nodes[0]; |
796787c9 | 3718 | |
e383e158 | 3719 | for (int i = path->slots[0]; i < nritems; i++) { |
732d591a | 3720 | struct btrfs_dir_item *di; |
eb10d85e | 3721 | struct btrfs_key key; |
eb10d85e FM |
3722 | int ret; |
3723 | ||
3724 | btrfs_item_key_to_cpu(src, &key, i); | |
3725 | ||
339d0354 | 3726 | if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) { |
086dcbfa FM |
3727 | last_found = true; |
3728 | break; | |
3729 | } | |
eb10d85e | 3730 | |
732d591a | 3731 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); |
732d591a FM |
3732 | |
3733 | /* | |
3734 | * Skip ranges of items that consist only of dir item keys created | |
3735 | * in past transactions. However if we find a gap, we must log a | |
3736 | * dir index range item for that gap, so that index keys in that | |
3737 | * gap are deleted during log replay. | |
3738 | */ | |
3739 | if (btrfs_dir_transid(src, di) < trans->transid) { | |
3740 | if (key.offset > *last_old_dentry_offset + 1) { | |
3741 | ret = insert_dir_log_key(trans, log, dst_path, | |
3742 | ino, *last_old_dentry_offset + 1, | |
3743 | key.offset - 1); | |
732d591a FM |
3744 | if (ret < 0) |
3745 | return ret; | |
3746 | } | |
3747 | ||
3748 | *last_old_dentry_offset = key.offset; | |
3749 | continue; | |
3750 | } | |
193df624 FM |
3751 | |
3752 | /* If we logged this dir index item before, we can skip it. */ | |
3753 | if (key.offset <= inode->last_dir_index_offset) | |
3754 | continue; | |
3755 | ||
eb10d85e FM |
3756 | /* |
3757 | * We must make sure that when we log a directory entry, the | |
3758 | * corresponding inode, after log replay, has a matching link | |
3759 | * count. For example: | |
3760 | * | |
3761 | * touch foo | |
3762 | * mkdir mydir | |
3763 | * sync | |
3764 | * ln foo mydir/bar | |
3765 | * xfs_io -c "fsync" mydir | |
3766 | * <crash> | |
3767 | * <mount fs and log replay> | |
3768 | * | |
3769 | * Would result in a fsync log that when replayed, our file inode | |
3770 | * would have a link count of 1, but we get two directory entries | |
3771 | * pointing to the same inode. After removing one of the names, | |
3772 | * it would not be possible to remove the other name, which | |
3773 | * resulted always in stale file handle errors, and would not be | |
3774 | * possible to rmdir the parent directory, since its i_size could | |
3775 | * never be decremented to the value BTRFS_EMPTY_DIR_SIZE, | |
3776 | * resulting in -ENOTEMPTY errors. | |
3777 | */ | |
086dcbfa | 3778 | if (!ctx->log_new_dentries) { |
086dcbfa FM |
3779 | struct btrfs_key di_key; |
3780 | ||
086dcbfa | 3781 | btrfs_dir_item_key_to_cpu(src, di, &di_key); |
732d591a | 3782 | if (di_key.type != BTRFS_ROOT_ITEM_KEY) |
086dcbfa FM |
3783 | ctx->log_new_dentries = true; |
3784 | } | |
3785 | ||
086dcbfa FM |
3786 | if (batch_size == 0) |
3787 | batch_start = i; | |
3788 | batch_size++; | |
eb10d85e FM |
3789 | } |
3790 | ||
086dcbfa FM |
3791 | if (batch_size > 0) { |
3792 | int ret; | |
3793 | ||
6afaed53 | 3794 | ret = flush_dir_items_batch(trans, inode, src, dst_path, |
086dcbfa FM |
3795 | batch_start, batch_size); |
3796 | if (ret < 0) | |
3797 | return ret; | |
3798 | } | |
3799 | ||
3800 | return last_found ? 1 : 0; | |
eb10d85e FM |
3801 | } |
3802 | ||
e02119d5 CM |
3803 | /* |
3804 | * log all the items included in the current transaction for a given | |
3805 | * directory. This also creates the range items in the log tree required | |
3806 | * to replay anything deleted before the fsync | |
3807 | */ | |
3808 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, | |
90d04510 | 3809 | struct btrfs_inode *inode, |
e02119d5 | 3810 | struct btrfs_path *path, |
339d0354 | 3811 | struct btrfs_path *dst_path, |
2f2ff0ee | 3812 | struct btrfs_log_ctx *ctx, |
e02119d5 CM |
3813 | u64 min_offset, u64 *last_offset_ret) |
3814 | { | |
3815 | struct btrfs_key min_key; | |
90d04510 | 3816 | struct btrfs_root *root = inode->root; |
e02119d5 | 3817 | struct btrfs_root *log = root->log_root; |
e02119d5 | 3818 | int ret; |
732d591a | 3819 | u64 last_old_dentry_offset = min_offset - 1; |
e02119d5 | 3820 | u64 last_offset = (u64)-1; |
684a5773 | 3821 | u64 ino = btrfs_ino(inode); |
e02119d5 | 3822 | |
33345d01 | 3823 | min_key.objectid = ino; |
339d0354 | 3824 | min_key.type = BTRFS_DIR_INDEX_KEY; |
e02119d5 CM |
3825 | min_key.offset = min_offset; |
3826 | ||
6174d3cb | 3827 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
e02119d5 CM |
3828 | |
3829 | /* | |
3830 | * we didn't find anything from this transaction, see if there | |
3831 | * is anything at all | |
3832 | */ | |
339d0354 FM |
3833 | if (ret != 0 || min_key.objectid != ino || |
3834 | min_key.type != BTRFS_DIR_INDEX_KEY) { | |
33345d01 | 3835 | min_key.objectid = ino; |
339d0354 | 3836 | min_key.type = BTRFS_DIR_INDEX_KEY; |
e02119d5 | 3837 | min_key.offset = (u64)-1; |
b3b4aa74 | 3838 | btrfs_release_path(path); |
e02119d5 CM |
3839 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
3840 | if (ret < 0) { | |
b3b4aa74 | 3841 | btrfs_release_path(path); |
e02119d5 CM |
3842 | return ret; |
3843 | } | |
339d0354 | 3844 | ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); |
e02119d5 CM |
3845 | |
3846 | /* if ret == 0 there are items for this type, | |
3847 | * create a range to tell us the last key of this type. | |
3848 | * otherwise, there are no items in this directory after | |
3849 | * *min_offset, and we create a range to indicate that. | |
3850 | */ | |
3851 | if (ret == 0) { | |
3852 | struct btrfs_key tmp; | |
732d591a | 3853 | |
e02119d5 CM |
3854 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
3855 | path->slots[0]); | |
339d0354 | 3856 | if (tmp.type == BTRFS_DIR_INDEX_KEY) |
732d591a | 3857 | last_old_dentry_offset = tmp.offset; |
235e1c7b FM |
3858 | } else if (ret > 0) { |
3859 | ret = 0; | |
e02119d5 | 3860 | } |
6d3d970b | 3861 | |
e02119d5 CM |
3862 | goto done; |
3863 | } | |
3864 | ||
3865 | /* go backward to find any previous key */ | |
339d0354 | 3866 | ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); |
e02119d5 CM |
3867 | if (ret == 0) { |
3868 | struct btrfs_key tmp; | |
a450a4af | 3869 | |
e02119d5 | 3870 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
a450a4af FM |
3871 | /* |
3872 | * The dir index key before the first one we found that needs to | |
3873 | * be logged might be in a previous leaf, and there might be a | |
3874 | * gap between these keys, meaning that we had deletions that | |
3875 | * happened. So the key range item we log (key type | |
3876 | * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the | |
3877 | * previous key's offset plus 1, so that those deletes are replayed. | |
3878 | */ | |
3879 | if (tmp.type == BTRFS_DIR_INDEX_KEY) | |
732d591a | 3880 | last_old_dentry_offset = tmp.offset; |
6d3d970b | 3881 | } else if (ret < 0) { |
6d3d970b | 3882 | goto done; |
e02119d5 | 3883 | } |
6d3d970b | 3884 | |
b3b4aa74 | 3885 | btrfs_release_path(path); |
e02119d5 | 3886 | |
2cc83342 | 3887 | /* |
8bb6898d FM |
3888 | * Find the first key from this transaction again or the one we were at |
3889 | * in the loop below in case we had to reschedule. We may be logging the | |
3890 | * directory without holding its VFS lock, which happen when logging new | |
3891 | * dentries (through log_new_dir_dentries()) or in some cases when we | |
3892 | * need to log the parent directory of an inode. This means a dir index | |
3893 | * key might be deleted from the inode's root, and therefore we may not | |
3894 | * find it anymore. If we can't find it, just move to the next key. We | |
3895 | * can not bail out and ignore, because if we do that we will simply | |
3896 | * not log dir index keys that come after the one that was just deleted | |
3897 | * and we can end up logging a dir index range that ends at (u64)-1 | |
3898 | * (@last_offset is initialized to that), resulting in removing dir | |
3899 | * entries we should not remove at log replay time. | |
2cc83342 | 3900 | */ |
bb56f02f | 3901 | search: |
e02119d5 | 3902 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
235e1c7b | 3903 | if (ret > 0) { |
8bb6898d | 3904 | ret = btrfs_next_item(root, path); |
235e1c7b FM |
3905 | if (ret > 0) { |
3906 | /* There are no more keys in the inode's root. */ | |
3907 | ret = 0; | |
3908 | goto done; | |
3909 | } | |
3910 | } | |
6d3d970b | 3911 | if (ret < 0) |
e02119d5 | 3912 | goto done; |
e02119d5 CM |
3913 | |
3914 | /* | |
3915 | * we have a block from this transaction, log every item in it | |
3916 | * from our directory | |
3917 | */ | |
d397712b | 3918 | while (1) { |
732d591a FM |
3919 | ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx, |
3920 | &last_old_dentry_offset); | |
eb10d85e | 3921 | if (ret != 0) { |
235e1c7b FM |
3922 | if (ret > 0) |
3923 | ret = 0; | |
eb10d85e | 3924 | goto done; |
e02119d5 | 3925 | } |
eb10d85e | 3926 | path->slots[0] = btrfs_header_nritems(path->nodes[0]); |
e02119d5 CM |
3927 | |
3928 | /* | |
3929 | * look ahead to the next item and see if it is also | |
3930 | * from this directory and from this transaction | |
3931 | */ | |
3932 | ret = btrfs_next_leaf(root, path); | |
80c0b421 | 3933 | if (ret) { |
235e1c7b | 3934 | if (ret == 1) { |
80c0b421 | 3935 | last_offset = (u64)-1; |
235e1c7b FM |
3936 | ret = 0; |
3937 | } | |
e02119d5 CM |
3938 | goto done; |
3939 | } | |
eb10d85e | 3940 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]); |
339d0354 | 3941 | if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) { |
e02119d5 CM |
3942 | last_offset = (u64)-1; |
3943 | goto done; | |
3944 | } | |
3945 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { | |
a450a4af FM |
3946 | /* |
3947 | * The next leaf was not changed in the current transaction | |
3948 | * and has at least one dir index key. | |
3949 | * We check for the next key because there might have been | |
3950 | * one or more deletions between the last key we logged and | |
3951 | * that next key. So the key range item we log (key type | |
3952 | * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's | |
3953 | * offset minus 1, so that those deletes are replayed. | |
3954 | */ | |
3955 | last_offset = min_key.offset - 1; | |
e02119d5 CM |
3956 | goto done; |
3957 | } | |
eb10d85e FM |
3958 | if (need_resched()) { |
3959 | btrfs_release_path(path); | |
3960 | cond_resched(); | |
3961 | goto search; | |
3962 | } | |
e02119d5 CM |
3963 | } |
3964 | done: | |
b3b4aa74 DS |
3965 | btrfs_release_path(path); |
3966 | btrfs_release_path(dst_path); | |
e02119d5 | 3967 | |
235e1c7b | 3968 | if (ret == 0) { |
4a500fd1 YZ |
3969 | *last_offset_ret = last_offset; |
3970 | /* | |
732d591a FM |
3971 | * In case the leaf was changed in the current transaction but |
3972 | * all its dir items are from a past transaction, the last item | |
3973 | * in the leaf is a dir item and there's no gap between that last | |
3974 | * dir item and the first one on the next leaf (which did not | |
3975 | * change in the current transaction), then we don't need to log | |
3976 | * a range, last_old_dentry_offset is == to last_offset. | |
4a500fd1 | 3977 | */ |
732d591a | 3978 | ASSERT(last_old_dentry_offset <= last_offset); |
235e1c7b | 3979 | if (last_old_dentry_offset < last_offset) |
732d591a FM |
3980 | ret = insert_dir_log_key(trans, log, path, ino, |
3981 | last_old_dentry_offset + 1, | |
3982 | last_offset); | |
4a500fd1 | 3983 | } |
235e1c7b FM |
3984 | |
3985 | return ret; | |
e02119d5 CM |
3986 | } |
3987 | ||
193df624 FM |
3988 | /* |
3989 | * If the inode was logged before and it was evicted, then its | |
3990 | * last_dir_index_offset is (u64)-1, so we don't the value of the last index | |
3991 | * key offset. If that's the case, search for it and update the inode. This | |
3992 | * is to avoid lookups in the log tree every time we try to insert a dir index | |
3993 | * key from a leaf changed in the current transaction, and to allow us to always | |
3994 | * do batch insertions of dir index keys. | |
3995 | */ | |
3996 | static int update_last_dir_index_offset(struct btrfs_inode *inode, | |
3997 | struct btrfs_path *path, | |
3998 | const struct btrfs_log_ctx *ctx) | |
3999 | { | |
4000 | const u64 ino = btrfs_ino(inode); | |
4001 | struct btrfs_key key; | |
4002 | int ret; | |
4003 | ||
4004 | lockdep_assert_held(&inode->log_mutex); | |
4005 | ||
4006 | if (inode->last_dir_index_offset != (u64)-1) | |
4007 | return 0; | |
4008 | ||
4009 | if (!ctx->logged_before) { | |
4010 | inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; | |
4011 | return 0; | |
4012 | } | |
4013 | ||
4014 | key.objectid = ino; | |
4015 | key.type = BTRFS_DIR_INDEX_KEY; | |
4016 | key.offset = (u64)-1; | |
4017 | ||
4018 | ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); | |
4019 | /* | |
4020 | * An error happened or we actually have an index key with an offset | |
4021 | * value of (u64)-1. Bail out, we're done. | |
4022 | */ | |
4023 | if (ret <= 0) | |
4024 | goto out; | |
4025 | ||
4026 | ret = 0; | |
4027 | inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; | |
4028 | ||
4029 | /* | |
4030 | * No dir index items, bail out and leave last_dir_index_offset with | |
4031 | * the value right before the first valid index value. | |
4032 | */ | |
4033 | if (path->slots[0] == 0) | |
4034 | goto out; | |
4035 | ||
4036 | /* | |
4037 | * btrfs_search_slot() left us at one slot beyond the slot with the last | |
4038 | * index key, or beyond the last key of the directory that is not an | |
4039 | * index key. If we have an index key before, set last_dir_index_offset | |
4040 | * to its offset value, otherwise leave it with a value right before the | |
4041 | * first valid index value, as it means we have an empty directory. | |
4042 | */ | |
4043 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); | |
4044 | if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY) | |
4045 | inode->last_dir_index_offset = key.offset; | |
4046 | ||
4047 | out: | |
4048 | btrfs_release_path(path); | |
4049 | ||
4050 | return ret; | |
4051 | } | |
4052 | ||
e02119d5 CM |
4053 | /* |
4054 | * logging directories is very similar to logging inodes, We find all the items | |
4055 | * from the current transaction and write them to the log. | |
4056 | * | |
4057 | * The recovery code scans the directory in the subvolume, and if it finds a | |
4058 | * key in the range logged that is not present in the log tree, then it means | |
4059 | * that dir entry was unlinked during the transaction. | |
4060 | * | |
4061 | * In order for that scan to work, we must include one key smaller than | |
4062 | * the smallest logged by this transaction and one key larger than the largest | |
4063 | * key logged by this transaction. | |
4064 | */ | |
4065 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, | |
90d04510 | 4066 | struct btrfs_inode *inode, |
e02119d5 | 4067 | struct btrfs_path *path, |
2f2ff0ee FM |
4068 | struct btrfs_path *dst_path, |
4069 | struct btrfs_log_ctx *ctx) | |
e02119d5 CM |
4070 | { |
4071 | u64 min_key; | |
4072 | u64 max_key; | |
4073 | int ret; | |
e02119d5 | 4074 | |
193df624 FM |
4075 | ret = update_last_dir_index_offset(inode, path, ctx); |
4076 | if (ret) | |
4077 | return ret; | |
4078 | ||
732d591a | 4079 | min_key = BTRFS_DIR_START_INDEX; |
e02119d5 | 4080 | max_key = 0; |
dc287224 | 4081 | |
d397712b | 4082 | while (1) { |
339d0354 | 4083 | ret = log_dir_items(trans, inode, path, dst_path, |
dbf39ea4 | 4084 | ctx, min_key, &max_key); |
4a500fd1 YZ |
4085 | if (ret) |
4086 | return ret; | |
e02119d5 CM |
4087 | if (max_key == (u64)-1) |
4088 | break; | |
4089 | min_key = max_key + 1; | |
4090 | } | |
4091 | ||
e02119d5 CM |
4092 | return 0; |
4093 | } | |
4094 | ||
4095 | /* | |
4096 | * a helper function to drop items from the log before we relog an | |
4097 | * inode. max_key_type indicates the highest item type to remove. | |
4098 | * This cannot be run for file data extents because it does not | |
4099 | * free the extents they point to. | |
4100 | */ | |
88e221cd | 4101 | static int drop_inode_items(struct btrfs_trans_handle *trans, |
e02119d5 CM |
4102 | struct btrfs_root *log, |
4103 | struct btrfs_path *path, | |
88e221cd FM |
4104 | struct btrfs_inode *inode, |
4105 | int max_key_type) | |
e02119d5 CM |
4106 | { |
4107 | int ret; | |
4108 | struct btrfs_key key; | |
4109 | struct btrfs_key found_key; | |
18ec90d6 | 4110 | int start_slot; |
e02119d5 | 4111 | |
88e221cd | 4112 | key.objectid = btrfs_ino(inode); |
e02119d5 CM |
4113 | key.type = max_key_type; |
4114 | key.offset = (u64)-1; | |
4115 | ||
d397712b | 4116 | while (1) { |
e02119d5 | 4117 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
fc4026e2 | 4118 | if (ret < 0) { |
e02119d5 | 4119 | break; |
fc4026e2 FM |
4120 | } else if (ret > 0) { |
4121 | if (path->slots[0] == 0) | |
4122 | break; | |
4123 | path->slots[0]--; | |
4124 | } | |
e02119d5 | 4125 | |
e02119d5 CM |
4126 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
4127 | path->slots[0]); | |
4128 | ||
88e221cd | 4129 | if (found_key.objectid != key.objectid) |
e02119d5 CM |
4130 | break; |
4131 | ||
18ec90d6 JB |
4132 | found_key.offset = 0; |
4133 | found_key.type = 0; | |
fdf8d595 | 4134 | ret = btrfs_bin_search(path->nodes[0], 0, &found_key, &start_slot); |
cbca7d59 FM |
4135 | if (ret < 0) |
4136 | break; | |
18ec90d6 JB |
4137 | |
4138 | ret = btrfs_del_items(trans, log, path, start_slot, | |
4139 | path->slots[0] - start_slot + 1); | |
4140 | /* | |
4141 | * If start slot isn't 0 then we don't need to re-search, we've | |
4142 | * found the last guy with the objectid in this tree. | |
4143 | */ | |
4144 | if (ret || start_slot != 0) | |
65a246c5 | 4145 | break; |
b3b4aa74 | 4146 | btrfs_release_path(path); |
e02119d5 | 4147 | } |
b3b4aa74 | 4148 | btrfs_release_path(path); |
5bdbeb21 JB |
4149 | if (ret > 0) |
4150 | ret = 0; | |
4a500fd1 | 4151 | return ret; |
e02119d5 CM |
4152 | } |
4153 | ||
8a2b3da1 FM |
4154 | static int truncate_inode_items(struct btrfs_trans_handle *trans, |
4155 | struct btrfs_root *log_root, | |
4156 | struct btrfs_inode *inode, | |
4157 | u64 new_size, u32 min_type) | |
4158 | { | |
d9ac19c3 JB |
4159 | struct btrfs_truncate_control control = { |
4160 | .new_size = new_size, | |
487e81d2 | 4161 | .ino = btrfs_ino(inode), |
d9ac19c3 | 4162 | .min_type = min_type, |
5caa490e | 4163 | .skip_ref_updates = true, |
d9ac19c3 | 4164 | }; |
8a2b3da1 | 4165 | |
8697b8f8 | 4166 | return btrfs_truncate_inode_items(trans, log_root, &control); |
8a2b3da1 FM |
4167 | } |
4168 | ||
94edf4ae JB |
4169 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
4170 | struct extent_buffer *leaf, | |
4171 | struct btrfs_inode_item *item, | |
1a4bcf47 FM |
4172 | struct inode *inode, int log_inode_only, |
4173 | u64 logged_isize) | |
94edf4ae | 4174 | { |
0b1c6cca | 4175 | struct btrfs_map_token token; |
77eea05e | 4176 | u64 flags; |
0b1c6cca | 4177 | |
c82f823c | 4178 | btrfs_init_map_token(&token, leaf); |
94edf4ae JB |
4179 | |
4180 | if (log_inode_only) { | |
4181 | /* set the generation to zero so the recover code | |
4182 | * can tell the difference between an logging | |
4183 | * just to say 'this inode exists' and a logging | |
4184 | * to say 'update this inode with these values' | |
4185 | */ | |
cc4c13d5 DS |
4186 | btrfs_set_token_inode_generation(&token, item, 0); |
4187 | btrfs_set_token_inode_size(&token, item, logged_isize); | |
94edf4ae | 4188 | } else { |
cc4c13d5 DS |
4189 | btrfs_set_token_inode_generation(&token, item, |
4190 | BTRFS_I(inode)->generation); | |
4191 | btrfs_set_token_inode_size(&token, item, inode->i_size); | |
0b1c6cca JB |
4192 | } |
4193 | ||
cc4c13d5 DS |
4194 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
4195 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
4196 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
4197 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
4198 | ||
4199 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
b1c38a13 | 4200 | inode_get_atime_sec(inode)); |
cc4c13d5 | 4201 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
b1c38a13 | 4202 | inode_get_atime_nsec(inode)); |
cc4c13d5 DS |
4203 | |
4204 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
b1c38a13 | 4205 | inode_get_mtime_sec(inode)); |
cc4c13d5 | 4206 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
b1c38a13 | 4207 | inode_get_mtime_nsec(inode)); |
cc4c13d5 DS |
4208 | |
4209 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
b1c38a13 | 4210 | inode_get_ctime_sec(inode)); |
cc4c13d5 | 4211 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
b1c38a13 | 4212 | inode_get_ctime_nsec(inode)); |
cc4c13d5 | 4213 | |
e593e54e FM |
4214 | /* |
4215 | * We do not need to set the nbytes field, in fact during a fast fsync | |
4216 | * its value may not even be correct, since a fast fsync does not wait | |
4217 | * for ordered extent completion, which is where we update nbytes, it | |
4218 | * only waits for writeback to complete. During log replay as we find | |
4219 | * file extent items and replay them, we adjust the nbytes field of the | |
4220 | * inode item in subvolume tree as needed (see overwrite_item()). | |
4221 | */ | |
cc4c13d5 DS |
4222 | |
4223 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
4224 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
4225 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
77eea05e BB |
4226 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
4227 | BTRFS_I(inode)->ro_flags); | |
4228 | btrfs_set_token_inode_flags(&token, item, flags); | |
cc4c13d5 | 4229 | btrfs_set_token_inode_block_group(&token, item, 0); |
94edf4ae JB |
4230 | } |
4231 | ||
a95249b3 JB |
4232 | static int log_inode_item(struct btrfs_trans_handle *trans, |
4233 | struct btrfs_root *log, struct btrfs_path *path, | |
2ac691d8 | 4234 | struct btrfs_inode *inode, bool inode_item_dropped) |
a95249b3 JB |
4235 | { |
4236 | struct btrfs_inode_item *inode_item; | |
a95249b3 JB |
4237 | int ret; |
4238 | ||
2ac691d8 FM |
4239 | /* |
4240 | * If we are doing a fast fsync and the inode was logged before in the | |
4241 | * current transaction, then we know the inode was previously logged and | |
4242 | * it exists in the log tree. For performance reasons, in this case use | |
4243 | * btrfs_search_slot() directly with ins_len set to 0 so that we never | |
4244 | * attempt a write lock on the leaf's parent, which adds unnecessary lock | |
4245 | * contention in case there are concurrent fsyncs for other inodes of the | |
4246 | * same subvolume. Using btrfs_insert_empty_item() when the inode item | |
4247 | * already exists can also result in unnecessarily splitting a leaf. | |
4248 | */ | |
4249 | if (!inode_item_dropped && inode->logged_trans == trans->transid) { | |
4250 | ret = btrfs_search_slot(trans, log, &inode->location, path, 0, 1); | |
4251 | ASSERT(ret <= 0); | |
4252 | if (ret > 0) | |
4253 | ret = -ENOENT; | |
4254 | } else { | |
4255 | /* | |
4256 | * This means it is the first fsync in the current transaction, | |
4257 | * so the inode item is not in the log and we need to insert it. | |
4258 | * We can never get -EEXIST because we are only called for a fast | |
4259 | * fsync and in case an inode eviction happens after the inode was | |
4260 | * logged before in the current transaction, when we load again | |
4261 | * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime | |
4262 | * flags and set ->logged_trans to 0. | |
4263 | */ | |
4264 | ret = btrfs_insert_empty_item(trans, log, path, &inode->location, | |
4265 | sizeof(*inode_item)); | |
4266 | ASSERT(ret != -EEXIST); | |
4267 | } | |
4268 | if (ret) | |
a95249b3 JB |
4269 | return ret; |
4270 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4271 | struct btrfs_inode_item); | |
6d889a3b NB |
4272 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, |
4273 | 0, 0); | |
a95249b3 JB |
4274 | btrfs_release_path(path); |
4275 | return 0; | |
4276 | } | |
4277 | ||
40e046ac | 4278 | static int log_csums(struct btrfs_trans_handle *trans, |
3ebac17c | 4279 | struct btrfs_inode *inode, |
40e046ac FM |
4280 | struct btrfs_root *log_root, |
4281 | struct btrfs_ordered_sum *sums) | |
4282 | { | |
5cfe76f8 | 4283 | const u64 lock_end = sums->logical + sums->len - 1; |
e289f03e | 4284 | struct extent_state *cached_state = NULL; |
40e046ac FM |
4285 | int ret; |
4286 | ||
3ebac17c FM |
4287 | /* |
4288 | * If this inode was not used for reflink operations in the current | |
4289 | * transaction with new extents, then do the fast path, no need to | |
4290 | * worry about logging checksum items with overlapping ranges. | |
4291 | */ | |
4292 | if (inode->last_reflink_trans < trans->transid) | |
4293 | return btrfs_csum_file_blocks(trans, log_root, sums); | |
4294 | ||
e289f03e FM |
4295 | /* |
4296 | * Serialize logging for checksums. This is to avoid racing with the | |
4297 | * same checksum being logged by another task that is logging another | |
4298 | * file which happens to refer to the same extent as well. Such races | |
4299 | * can leave checksum items in the log with overlapping ranges. | |
4300 | */ | |
5cfe76f8 | 4301 | ret = lock_extent(&log_root->log_csum_range, sums->logical, lock_end, |
570eb97b | 4302 | &cached_state); |
e289f03e FM |
4303 | if (ret) |
4304 | return ret; | |
40e046ac FM |
4305 | /* |
4306 | * Due to extent cloning, we might have logged a csum item that covers a | |
4307 | * subrange of a cloned extent, and later we can end up logging a csum | |
4308 | * item for a larger subrange of the same extent or the entire range. | |
4309 | * This would leave csum items in the log tree that cover the same range | |
4310 | * and break the searches for checksums in the log tree, resulting in | |
4311 | * some checksums missing in the fs/subvolume tree. So just delete (or | |
4312 | * trim and adjust) any existing csum items in the log for this range. | |
4313 | */ | |
5cfe76f8 | 4314 | ret = btrfs_del_csums(trans, log_root, sums->logical, sums->len); |
e289f03e FM |
4315 | if (!ret) |
4316 | ret = btrfs_csum_file_blocks(trans, log_root, sums); | |
40e046ac | 4317 | |
5cfe76f8 | 4318 | unlock_extent(&log_root->log_csum_range, sums->logical, lock_end, |
570eb97b | 4319 | &cached_state); |
e289f03e FM |
4320 | |
4321 | return ret; | |
40e046ac FM |
4322 | } |
4323 | ||
31ff1cd2 | 4324 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
44d70e19 | 4325 | struct btrfs_inode *inode, |
31ff1cd2 | 4326 | struct btrfs_path *dst_path, |
0e56315c | 4327 | struct btrfs_path *src_path, |
1a4bcf47 | 4328 | int start_slot, int nr, int inode_only, |
e383e158 | 4329 | u64 logged_isize, struct btrfs_log_ctx *ctx) |
31ff1cd2 | 4330 | { |
44d70e19 | 4331 | struct btrfs_root *log = inode->root->log_root; |
31ff1cd2 | 4332 | struct btrfs_file_extent_item *extent; |
796787c9 | 4333 | struct extent_buffer *src; |
e383e158 | 4334 | int ret; |
31ff1cd2 CM |
4335 | struct btrfs_key *ins_keys; |
4336 | u32 *ins_sizes; | |
b7ef5f3a | 4337 | struct btrfs_item_batch batch; |
31ff1cd2 | 4338 | char *ins_data; |
7f30c072 | 4339 | int dst_index; |
7f30c072 FM |
4340 | const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM); |
4341 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
d20f7043 | 4342 | |
796787c9 FM |
4343 | /* |
4344 | * To keep lockdep happy and avoid deadlocks, clone the source leaf and | |
4345 | * use the clone. This is because otherwise we would be changing the log | |
4346 | * tree, to insert items from the subvolume tree or insert csum items, | |
4347 | * while holding a read lock on a leaf from the subvolume tree, which | |
4348 | * creates a nasty lock dependency when COWing log tree nodes/leaves: | |
4349 | * | |
4350 | * 1) Modifying the log tree triggers an extent buffer allocation while | |
4351 | * holding a write lock on a parent extent buffer from the log tree. | |
4352 | * Allocating the pages for an extent buffer, or the extent buffer | |
4353 | * struct, can trigger inode eviction and finally the inode eviction | |
4354 | * will trigger a release/remove of a delayed node, which requires | |
4355 | * taking the delayed node's mutex; | |
4356 | * | |
4357 | * 2) Allocating a metadata extent for a log tree can trigger the async | |
4358 | * reclaim thread and make us wait for it to release enough space and | |
4359 | * unblock our reservation ticket. The reclaim thread can start | |
4360 | * flushing delayed items, and that in turn results in the need to | |
4361 | * lock delayed node mutexes and in the need to write lock extent | |
4362 | * buffers of a subvolume tree - all this while holding a write lock | |
4363 | * on the parent extent buffer in the log tree. | |
4364 | * | |
4365 | * So one task in scenario 1) running in parallel with another task in | |
4366 | * scenario 2) could lead to a deadlock, one wanting to lock a delayed | |
4367 | * node mutex while having a read lock on a leaf from the subvolume, | |
4368 | * while the other is holding the delayed node's mutex and wants to | |
4369 | * write lock the same subvolume leaf for flushing delayed items. | |
4370 | */ | |
e383e158 FM |
4371 | ret = clone_leaf(src_path, ctx); |
4372 | if (ret < 0) | |
4373 | return ret; | |
796787c9 | 4374 | |
e383e158 | 4375 | src = src_path->nodes[0]; |
796787c9 | 4376 | |
31ff1cd2 CM |
4377 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
4378 | nr * sizeof(u32), GFP_NOFS); | |
2a29edc6 | 4379 | if (!ins_data) |
4380 | return -ENOMEM; | |
4381 | ||
31ff1cd2 CM |
4382 | ins_sizes = (u32 *)ins_data; |
4383 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); | |
b7ef5f3a FM |
4384 | batch.keys = ins_keys; |
4385 | batch.data_sizes = ins_sizes; | |
4386 | batch.total_data_size = 0; | |
7f30c072 | 4387 | batch.nr = 0; |
31ff1cd2 | 4388 | |
7f30c072 | 4389 | dst_index = 0; |
e383e158 | 4390 | for (int i = 0; i < nr; i++) { |
7f30c072 FM |
4391 | const int src_slot = start_slot + i; |
4392 | struct btrfs_root *csum_root; | |
5b7ce5e2 FM |
4393 | struct btrfs_ordered_sum *sums; |
4394 | struct btrfs_ordered_sum *sums_next; | |
4395 | LIST_HEAD(ordered_sums); | |
7f30c072 FM |
4396 | u64 disk_bytenr; |
4397 | u64 disk_num_bytes; | |
4398 | u64 extent_offset; | |
4399 | u64 extent_num_bytes; | |
4400 | bool is_old_extent; | |
4401 | ||
4402 | btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot); | |
4403 | ||
4404 | if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY) | |
4405 | goto add_to_batch; | |
4406 | ||
4407 | extent = btrfs_item_ptr(src, src_slot, | |
4408 | struct btrfs_file_extent_item); | |
4409 | ||
4410 | is_old_extent = (btrfs_file_extent_generation(src, extent) < | |
4411 | trans->transid); | |
4412 | ||
4413 | /* | |
4414 | * Don't copy extents from past generations. That would make us | |
4415 | * log a lot more metadata for common cases like doing only a | |
4416 | * few random writes into a file and then fsync it for the first | |
4417 | * time or after the full sync flag is set on the inode. We can | |
4418 | * get leaves full of extent items, most of which are from past | |
4419 | * generations, so we can skip them - as long as the inode has | |
4420 | * not been the target of a reflink operation in this transaction, | |
4421 | * as in that case it might have had file extent items with old | |
4422 | * generations copied into it. We also must always log prealloc | |
4423 | * extents that start at or beyond eof, otherwise we would lose | |
4424 | * them on log replay. | |
4425 | */ | |
4426 | if (is_old_extent && | |
4427 | ins_keys[dst_index].offset < i_size && | |
4428 | inode->last_reflink_trans < trans->transid) | |
4429 | continue; | |
4430 | ||
4431 | if (skip_csum) | |
4432 | goto add_to_batch; | |
4433 | ||
4434 | /* Only regular extents have checksums. */ | |
4435 | if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG) | |
4436 | goto add_to_batch; | |
4437 | ||
4438 | /* | |
4439 | * If it's an extent created in a past transaction, then its | |
4440 | * checksums are already accessible from the committed csum tree, | |
4441 | * no need to log them. | |
4442 | */ | |
4443 | if (is_old_extent) | |
4444 | goto add_to_batch; | |
4445 | ||
4446 | disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent); | |
4447 | /* If it's an explicit hole, there are no checksums. */ | |
4448 | if (disk_bytenr == 0) | |
4449 | goto add_to_batch; | |
4450 | ||
4451 | disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent); | |
4452 | ||
4453 | if (btrfs_file_extent_compression(src, extent)) { | |
4454 | extent_offset = 0; | |
4455 | extent_num_bytes = disk_num_bytes; | |
4456 | } else { | |
4457 | extent_offset = btrfs_file_extent_offset(src, extent); | |
4458 | extent_num_bytes = btrfs_file_extent_num_bytes(src, extent); | |
4459 | } | |
4460 | ||
4461 | csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr); | |
4462 | disk_bytenr += extent_offset; | |
97e38239 QW |
4463 | ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, |
4464 | disk_bytenr + extent_num_bytes - 1, | |
afcb8062 | 4465 | &ordered_sums, false); |
8d2a83a9 | 4466 | if (ret < 0) |
7f30c072 | 4467 | goto out; |
8d2a83a9 | 4468 | ret = 0; |
7f30c072 | 4469 | |
5b7ce5e2 FM |
4470 | list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) { |
4471 | if (!ret) | |
4472 | ret = log_csums(trans, inode, log, sums); | |
4473 | list_del(&sums->list); | |
4474 | kfree(sums); | |
4475 | } | |
4476 | if (ret) | |
4477 | goto out; | |
4478 | ||
7f30c072 FM |
4479 | add_to_batch: |
4480 | ins_sizes[dst_index] = btrfs_item_size(src, src_slot); | |
4481 | batch.total_data_size += ins_sizes[dst_index]; | |
4482 | batch.nr++; | |
4483 | dst_index++; | |
31ff1cd2 | 4484 | } |
7f30c072 FM |
4485 | |
4486 | /* | |
4487 | * We have a leaf full of old extent items that don't need to be logged, | |
4488 | * so we don't need to do anything. | |
4489 | */ | |
4490 | if (batch.nr == 0) | |
4491 | goto out; | |
4492 | ||
b7ef5f3a | 4493 | ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); |
7f30c072 FM |
4494 | if (ret) |
4495 | goto out; | |
4496 | ||
4497 | dst_index = 0; | |
e383e158 | 4498 | for (int i = 0; i < nr; i++) { |
7f30c072 FM |
4499 | const int src_slot = start_slot + i; |
4500 | const int dst_slot = dst_path->slots[0] + dst_index; | |
4501 | struct btrfs_key key; | |
4502 | unsigned long src_offset; | |
4503 | unsigned long dst_offset; | |
4504 | ||
4505 | /* | |
4506 | * We're done, all the remaining items in the source leaf | |
4507 | * correspond to old file extent items. | |
4508 | */ | |
4509 | if (dst_index >= batch.nr) | |
4510 | break; | |
4511 | ||
4512 | btrfs_item_key_to_cpu(src, &key, src_slot); | |
4513 | ||
4514 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
4515 | goto copy_item; | |
31ff1cd2 | 4516 | |
7f30c072 FM |
4517 | extent = btrfs_item_ptr(src, src_slot, |
4518 | struct btrfs_file_extent_item); | |
31ff1cd2 | 4519 | |
7f30c072 FM |
4520 | /* See the comment in the previous loop, same logic. */ |
4521 | if (btrfs_file_extent_generation(src, extent) < trans->transid && | |
4522 | key.offset < i_size && | |
4523 | inode->last_reflink_trans < trans->transid) | |
4524 | continue; | |
4525 | ||
4526 | copy_item: | |
4527 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot); | |
4528 | src_offset = btrfs_item_ptr_offset(src, src_slot); | |
31ff1cd2 | 4529 | |
7f30c072 FM |
4530 | if (key.type == BTRFS_INODE_ITEM_KEY) { |
4531 | struct btrfs_inode_item *inode_item; | |
4532 | ||
4533 | inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot, | |
31ff1cd2 | 4534 | struct btrfs_inode_item); |
94edf4ae | 4535 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
f85b7379 DS |
4536 | &inode->vfs_inode, |
4537 | inode_only == LOG_INODE_EXISTS, | |
1a4bcf47 | 4538 | logged_isize); |
94edf4ae JB |
4539 | } else { |
4540 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, | |
7f30c072 | 4541 | src_offset, ins_sizes[dst_index]); |
31ff1cd2 | 4542 | } |
94edf4ae | 4543 | |
7f30c072 | 4544 | dst_index++; |
31ff1cd2 CM |
4545 | } |
4546 | ||
50564b65 | 4547 | btrfs_mark_buffer_dirty(trans, dst_path->nodes[0]); |
b3b4aa74 | 4548 | btrfs_release_path(dst_path); |
7f30c072 | 4549 | out: |
31ff1cd2 | 4550 | kfree(ins_data); |
d20f7043 | 4551 | |
4a500fd1 | 4552 | return ret; |
31ff1cd2 CM |
4553 | } |
4554 | ||
4f0f586b ST |
4555 | static int extent_cmp(void *priv, const struct list_head *a, |
4556 | const struct list_head *b) | |
5dc562c5 | 4557 | { |
214cc184 | 4558 | const struct extent_map *em1, *em2; |
5dc562c5 JB |
4559 | |
4560 | em1 = list_entry(a, struct extent_map, list); | |
4561 | em2 = list_entry(b, struct extent_map, list); | |
4562 | ||
4563 | if (em1->start < em2->start) | |
4564 | return -1; | |
4565 | else if (em1->start > em2->start) | |
4566 | return 1; | |
4567 | return 0; | |
4568 | } | |
4569 | ||
e7175a69 JB |
4570 | static int log_extent_csums(struct btrfs_trans_handle *trans, |
4571 | struct btrfs_inode *inode, | |
a9ecb653 | 4572 | struct btrfs_root *log_root, |
48778179 FM |
4573 | const struct extent_map *em, |
4574 | struct btrfs_log_ctx *ctx) | |
5dc562c5 | 4575 | { |
48778179 | 4576 | struct btrfs_ordered_extent *ordered; |
fc28b25e | 4577 | struct btrfs_root *csum_root; |
2ab28f32 JB |
4578 | u64 csum_offset; |
4579 | u64 csum_len; | |
2e438442 FM |
4580 | u64 mod_start = em->start; |
4581 | u64 mod_len = em->len; | |
8407f553 FM |
4582 | LIST_HEAD(ordered_sums); |
4583 | int ret = 0; | |
0aa4a17d | 4584 | |
e7175a69 | 4585 | if (inode->flags & BTRFS_INODE_NODATASUM || |
f86f7a75 | 4586 | (em->flags & EXTENT_FLAG_PREALLOC) || |
8407f553 | 4587 | em->block_start == EXTENT_MAP_HOLE) |
70c8a91c | 4588 | return 0; |
5dc562c5 | 4589 | |
48778179 FM |
4590 | list_for_each_entry(ordered, &ctx->ordered_extents, log_list) { |
4591 | const u64 ordered_end = ordered->file_offset + ordered->num_bytes; | |
4592 | const u64 mod_end = mod_start + mod_len; | |
4593 | struct btrfs_ordered_sum *sums; | |
4594 | ||
4595 | if (mod_len == 0) | |
4596 | break; | |
4597 | ||
4598 | if (ordered_end <= mod_start) | |
4599 | continue; | |
4600 | if (mod_end <= ordered->file_offset) | |
4601 | break; | |
4602 | ||
4603 | /* | |
4604 | * We are going to copy all the csums on this ordered extent, so | |
4605 | * go ahead and adjust mod_start and mod_len in case this ordered | |
4606 | * extent has already been logged. | |
4607 | */ | |
4608 | if (ordered->file_offset > mod_start) { | |
4609 | if (ordered_end >= mod_end) | |
4610 | mod_len = ordered->file_offset - mod_start; | |
4611 | /* | |
4612 | * If we have this case | |
4613 | * | |
4614 | * |--------- logged extent ---------| | |
4615 | * |----- ordered extent ----| | |
4616 | * | |
4617 | * Just don't mess with mod_start and mod_len, we'll | |
4618 | * just end up logging more csums than we need and it | |
4619 | * will be ok. | |
4620 | */ | |
4621 | } else { | |
4622 | if (ordered_end < mod_end) { | |
4623 | mod_len = mod_end - ordered_end; | |
4624 | mod_start = ordered_end; | |
4625 | } else { | |
4626 | mod_len = 0; | |
4627 | } | |
4628 | } | |
4629 | ||
4630 | /* | |
4631 | * To keep us from looping for the above case of an ordered | |
4632 | * extent that falls inside of the logged extent. | |
4633 | */ | |
4634 | if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags)) | |
4635 | continue; | |
4636 | ||
4637 | list_for_each_entry(sums, &ordered->list, list) { | |
4638 | ret = log_csums(trans, inode, log_root, sums); | |
4639 | if (ret) | |
4640 | return ret; | |
4641 | } | |
4642 | } | |
4643 | ||
4644 | /* We're done, found all csums in the ordered extents. */ | |
4645 | if (mod_len == 0) | |
4646 | return 0; | |
4647 | ||
e7175a69 | 4648 | /* If we're compressed we have to save the entire range of csums. */ |
f86f7a75 | 4649 | if (extent_map_is_compressed(em)) { |
488111aa | 4650 | csum_offset = 0; |
8407f553 | 4651 | csum_len = max(em->block_len, em->orig_block_len); |
488111aa | 4652 | } else { |
48778179 FM |
4653 | csum_offset = mod_start - em->start; |
4654 | csum_len = mod_len; | |
488111aa | 4655 | } |
2ab28f32 | 4656 | |
70c8a91c | 4657 | /* block start is already adjusted for the file extent offset. */ |
fc28b25e | 4658 | csum_root = btrfs_csum_root(trans->fs_info, em->block_start); |
97e38239 QW |
4659 | ret = btrfs_lookup_csums_list(csum_root, em->block_start + csum_offset, |
4660 | em->block_start + csum_offset + | |
afcb8062 | 4661 | csum_len - 1, &ordered_sums, false); |
8d2a83a9 | 4662 | if (ret < 0) |
70c8a91c | 4663 | return ret; |
8d2a83a9 | 4664 | ret = 0; |
5dc562c5 | 4665 | |
70c8a91c JB |
4666 | while (!list_empty(&ordered_sums)) { |
4667 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, | |
4668 | struct btrfs_ordered_sum, | |
4669 | list); | |
4670 | if (!ret) | |
3ebac17c | 4671 | ret = log_csums(trans, inode, log_root, sums); |
70c8a91c JB |
4672 | list_del(&sums->list); |
4673 | kfree(sums); | |
5dc562c5 JB |
4674 | } |
4675 | ||
70c8a91c | 4676 | return ret; |
5dc562c5 JB |
4677 | } |
4678 | ||
8407f553 | 4679 | static int log_one_extent(struct btrfs_trans_handle *trans, |
90d04510 | 4680 | struct btrfs_inode *inode, |
8407f553 FM |
4681 | const struct extent_map *em, |
4682 | struct btrfs_path *path, | |
8407f553 FM |
4683 | struct btrfs_log_ctx *ctx) |
4684 | { | |
5893dfb9 | 4685 | struct btrfs_drop_extents_args drop_args = { 0 }; |
90d04510 | 4686 | struct btrfs_root *log = inode->root->log_root; |
e1f53ed8 | 4687 | struct btrfs_file_extent_item fi = { 0 }; |
8407f553 | 4688 | struct extent_buffer *leaf; |
8407f553 | 4689 | struct btrfs_key key; |
f86f7a75 | 4690 | enum btrfs_compression_type compress_type; |
8407f553 FM |
4691 | u64 extent_offset = em->start - em->orig_start; |
4692 | u64 block_len; | |
4693 | int ret; | |
8407f553 | 4694 | |
e1f53ed8 | 4695 | btrfs_set_stack_file_extent_generation(&fi, trans->transid); |
f86f7a75 | 4696 | if (em->flags & EXTENT_FLAG_PREALLOC) |
e1f53ed8 FM |
4697 | btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC); |
4698 | else | |
4699 | btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG); | |
4700 | ||
4701 | block_len = max(em->block_len, em->orig_block_len); | |
f86f7a75 FM |
4702 | compress_type = extent_map_compression(em); |
4703 | if (compress_type != BTRFS_COMPRESS_NONE) { | |
e1f53ed8 FM |
4704 | btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start); |
4705 | btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); | |
4706 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { | |
4707 | btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start - | |
4708 | extent_offset); | |
4709 | btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); | |
4710 | } | |
4711 | ||
4712 | btrfs_set_stack_file_extent_offset(&fi, extent_offset); | |
4713 | btrfs_set_stack_file_extent_num_bytes(&fi, em->len); | |
4714 | btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes); | |
f86f7a75 | 4715 | btrfs_set_stack_file_extent_compression(&fi, compress_type); |
e1f53ed8 | 4716 | |
48778179 | 4717 | ret = log_extent_csums(trans, inode, log, em, ctx); |
8407f553 FM |
4718 | if (ret) |
4719 | return ret; | |
4720 | ||
5328b2a7 FM |
4721 | /* |
4722 | * If this is the first time we are logging the inode in the current | |
4723 | * transaction, we can avoid btrfs_drop_extents(), which is expensive | |
4724 | * because it does a deletion search, which always acquires write locks | |
4725 | * for extent buffers at levels 2, 1 and 0. This not only wastes time | |
4726 | * but also adds significant contention in a log tree, since log trees | |
4727 | * are small, with a root at level 2 or 3 at most, due to their short | |
4728 | * life span. | |
4729 | */ | |
0f8ce498 | 4730 | if (ctx->logged_before) { |
5328b2a7 FM |
4731 | drop_args.path = path; |
4732 | drop_args.start = em->start; | |
4733 | drop_args.end = em->start + em->len; | |
4734 | drop_args.replace_extent = true; | |
e1f53ed8 | 4735 | drop_args.extent_item_size = sizeof(fi); |
5328b2a7 FM |
4736 | ret = btrfs_drop_extents(trans, log, inode, &drop_args); |
4737 | if (ret) | |
4738 | return ret; | |
4739 | } | |
8407f553 | 4740 | |
5893dfb9 | 4741 | if (!drop_args.extent_inserted) { |
9d122629 | 4742 | key.objectid = btrfs_ino(inode); |
8407f553 FM |
4743 | key.type = BTRFS_EXTENT_DATA_KEY; |
4744 | key.offset = em->start; | |
4745 | ||
4746 | ret = btrfs_insert_empty_item(trans, log, path, &key, | |
e1f53ed8 | 4747 | sizeof(fi)); |
8407f553 FM |
4748 | if (ret) |
4749 | return ret; | |
4750 | } | |
4751 | leaf = path->nodes[0]; | |
e1f53ed8 FM |
4752 | write_extent_buffer(leaf, &fi, |
4753 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
4754 | sizeof(fi)); | |
50564b65 | 4755 | btrfs_mark_buffer_dirty(trans, leaf); |
8407f553 FM |
4756 | |
4757 | btrfs_release_path(path); | |
4758 | ||
4759 | return ret; | |
4760 | } | |
4761 | ||
31d11b83 FM |
4762 | /* |
4763 | * Log all prealloc extents beyond the inode's i_size to make sure we do not | |
d9947887 | 4764 | * lose them after doing a full/fast fsync and replaying the log. We scan the |
31d11b83 FM |
4765 | * subvolume's root instead of iterating the inode's extent map tree because |
4766 | * otherwise we can log incorrect extent items based on extent map conversion. | |
4767 | * That can happen due to the fact that extent maps are merged when they | |
4768 | * are not in the extent map tree's list of modified extents. | |
4769 | */ | |
4770 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, | |
4771 | struct btrfs_inode *inode, | |
e383e158 FM |
4772 | struct btrfs_path *path, |
4773 | struct btrfs_log_ctx *ctx) | |
31d11b83 FM |
4774 | { |
4775 | struct btrfs_root *root = inode->root; | |
4776 | struct btrfs_key key; | |
4777 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
4778 | const u64 ino = btrfs_ino(inode); | |
4779 | struct btrfs_path *dst_path = NULL; | |
0e56315c | 4780 | bool dropped_extents = false; |
f135cea3 FM |
4781 | u64 truncate_offset = i_size; |
4782 | struct extent_buffer *leaf; | |
4783 | int slot; | |
31d11b83 | 4784 | int ins_nr = 0; |
b4c639f6 | 4785 | int start_slot = 0; |
31d11b83 FM |
4786 | int ret; |
4787 | ||
4788 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) | |
4789 | return 0; | |
4790 | ||
4791 | key.objectid = ino; | |
4792 | key.type = BTRFS_EXTENT_DATA_KEY; | |
4793 | key.offset = i_size; | |
4794 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
4795 | if (ret < 0) | |
4796 | goto out; | |
4797 | ||
f135cea3 FM |
4798 | /* |
4799 | * We must check if there is a prealloc extent that starts before the | |
4800 | * i_size and crosses the i_size boundary. This is to ensure later we | |
4801 | * truncate down to the end of that extent and not to the i_size, as | |
4802 | * otherwise we end up losing part of the prealloc extent after a log | |
4803 | * replay and with an implicit hole if there is another prealloc extent | |
4804 | * that starts at an offset beyond i_size. | |
4805 | */ | |
4806 | ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); | |
4807 | if (ret < 0) | |
4808 | goto out; | |
4809 | ||
4810 | if (ret == 0) { | |
4811 | struct btrfs_file_extent_item *ei; | |
4812 | ||
4813 | leaf = path->nodes[0]; | |
4814 | slot = path->slots[0]; | |
4815 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4816 | ||
4817 | if (btrfs_file_extent_type(leaf, ei) == | |
4818 | BTRFS_FILE_EXTENT_PREALLOC) { | |
4819 | u64 extent_end; | |
4820 | ||
4821 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4822 | extent_end = key.offset + | |
4823 | btrfs_file_extent_num_bytes(leaf, ei); | |
4824 | ||
4825 | if (extent_end > i_size) | |
4826 | truncate_offset = extent_end; | |
4827 | } | |
4828 | } else { | |
4829 | ret = 0; | |
4830 | } | |
4831 | ||
31d11b83 | 4832 | while (true) { |
f135cea3 FM |
4833 | leaf = path->nodes[0]; |
4834 | slot = path->slots[0]; | |
31d11b83 FM |
4835 | |
4836 | if (slot >= btrfs_header_nritems(leaf)) { | |
4837 | if (ins_nr > 0) { | |
4838 | ret = copy_items(trans, inode, dst_path, path, | |
e383e158 | 4839 | start_slot, ins_nr, 1, 0, ctx); |
31d11b83 FM |
4840 | if (ret < 0) |
4841 | goto out; | |
4842 | ins_nr = 0; | |
4843 | } | |
4844 | ret = btrfs_next_leaf(root, path); | |
4845 | if (ret < 0) | |
4846 | goto out; | |
4847 | if (ret > 0) { | |
4848 | ret = 0; | |
4849 | break; | |
4850 | } | |
4851 | continue; | |
4852 | } | |
4853 | ||
4854 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4855 | if (key.objectid > ino) | |
4856 | break; | |
4857 | if (WARN_ON_ONCE(key.objectid < ino) || | |
4858 | key.type < BTRFS_EXTENT_DATA_KEY || | |
4859 | key.offset < i_size) { | |
4860 | path->slots[0]++; | |
4861 | continue; | |
4862 | } | |
0e56315c | 4863 | if (!dropped_extents) { |
31d11b83 FM |
4864 | /* |
4865 | * Avoid logging extent items logged in past fsync calls | |
4866 | * and leading to duplicate keys in the log tree. | |
4867 | */ | |
8a2b3da1 FM |
4868 | ret = truncate_inode_items(trans, root->log_root, inode, |
4869 | truncate_offset, | |
4870 | BTRFS_EXTENT_DATA_KEY); | |
31d11b83 FM |
4871 | if (ret) |
4872 | goto out; | |
0e56315c | 4873 | dropped_extents = true; |
31d11b83 FM |
4874 | } |
4875 | if (ins_nr == 0) | |
4876 | start_slot = slot; | |
4877 | ins_nr++; | |
4878 | path->slots[0]++; | |
4879 | if (!dst_path) { | |
4880 | dst_path = btrfs_alloc_path(); | |
4881 | if (!dst_path) { | |
4882 | ret = -ENOMEM; | |
4883 | goto out; | |
4884 | } | |
4885 | } | |
4886 | } | |
0bc2d3c0 | 4887 | if (ins_nr > 0) |
0e56315c | 4888 | ret = copy_items(trans, inode, dst_path, path, |
e383e158 | 4889 | start_slot, ins_nr, 1, 0, ctx); |
31d11b83 FM |
4890 | out: |
4891 | btrfs_release_path(path); | |
4892 | btrfs_free_path(dst_path); | |
4893 | return ret; | |
4894 | } | |
4895 | ||
5dc562c5 | 4896 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
9d122629 | 4897 | struct btrfs_inode *inode, |
827463c4 | 4898 | struct btrfs_path *path, |
48778179 | 4899 | struct btrfs_log_ctx *ctx) |
5dc562c5 | 4900 | { |
48778179 FM |
4901 | struct btrfs_ordered_extent *ordered; |
4902 | struct btrfs_ordered_extent *tmp; | |
5dc562c5 | 4903 | struct extent_map *em, *n; |
84af994b | 4904 | LIST_HEAD(extents); |
9d122629 | 4905 | struct extent_map_tree *tree = &inode->extent_tree; |
5dc562c5 | 4906 | int ret = 0; |
2ab28f32 | 4907 | int num = 0; |
5dc562c5 | 4908 | |
5dc562c5 | 4909 | write_lock(&tree->lock); |
5dc562c5 JB |
4910 | |
4911 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { | |
4912 | list_del_init(&em->list); | |
2ab28f32 JB |
4913 | /* |
4914 | * Just an arbitrary number, this can be really CPU intensive | |
4915 | * once we start getting a lot of extents, and really once we | |
4916 | * have a bunch of extents we just want to commit since it will | |
4917 | * be faster. | |
4918 | */ | |
4919 | if (++num > 32768) { | |
4920 | list_del_init(&tree->modified_extents); | |
4921 | ret = -EFBIG; | |
4922 | goto process; | |
4923 | } | |
4924 | ||
5f96bfb7 | 4925 | if (em->generation < trans->transid) |
5dc562c5 | 4926 | continue; |
8c6c5928 | 4927 | |
31d11b83 | 4928 | /* We log prealloc extents beyond eof later. */ |
f86f7a75 | 4929 | if ((em->flags & EXTENT_FLAG_PREALLOC) && |
31d11b83 FM |
4930 | em->start >= i_size_read(&inode->vfs_inode)) |
4931 | continue; | |
4932 | ||
ff44c6e3 | 4933 | /* Need a ref to keep it from getting evicted from cache */ |
490b54d6 | 4934 | refcount_inc(&em->refs); |
f86f7a75 | 4935 | em->flags |= EXTENT_FLAG_LOGGING; |
5dc562c5 | 4936 | list_add_tail(&em->list, &extents); |
2ab28f32 | 4937 | num++; |
5dc562c5 JB |
4938 | } |
4939 | ||
4940 | list_sort(NULL, &extents, extent_cmp); | |
2ab28f32 | 4941 | process: |
5dc562c5 JB |
4942 | while (!list_empty(&extents)) { |
4943 | em = list_entry(extents.next, struct extent_map, list); | |
4944 | ||
4945 | list_del_init(&em->list); | |
4946 | ||
4947 | /* | |
4948 | * If we had an error we just need to delete everybody from our | |
4949 | * private list. | |
4950 | */ | |
ff44c6e3 | 4951 | if (ret) { |
201a9038 | 4952 | clear_em_logging(tree, em); |
ff44c6e3 | 4953 | free_extent_map(em); |
5dc562c5 | 4954 | continue; |
ff44c6e3 JB |
4955 | } |
4956 | ||
4957 | write_unlock(&tree->lock); | |
5dc562c5 | 4958 | |
90d04510 | 4959 | ret = log_one_extent(trans, inode, em, path, ctx); |
ff44c6e3 | 4960 | write_lock(&tree->lock); |
201a9038 JB |
4961 | clear_em_logging(tree, em); |
4962 | free_extent_map(em); | |
5dc562c5 | 4963 | } |
ff44c6e3 JB |
4964 | WARN_ON(!list_empty(&extents)); |
4965 | write_unlock(&tree->lock); | |
5dc562c5 | 4966 | |
31d11b83 | 4967 | if (!ret) |
e383e158 | 4968 | ret = btrfs_log_prealloc_extents(trans, inode, path, ctx); |
48778179 FM |
4969 | if (ret) |
4970 | return ret; | |
31d11b83 | 4971 | |
48778179 FM |
4972 | /* |
4973 | * We have logged all extents successfully, now make sure the commit of | |
4974 | * the current transaction waits for the ordered extents to complete | |
4975 | * before it commits and wipes out the log trees, otherwise we would | |
4976 | * lose data if an ordered extents completes after the transaction | |
4977 | * commits and a power failure happens after the transaction commit. | |
4978 | */ | |
4979 | list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { | |
4980 | list_del_init(&ordered->log_list); | |
4981 | set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags); | |
4982 | ||
4983 | if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { | |
54c65371 | 4984 | spin_lock_irq(&inode->ordered_tree_lock); |
48778179 FM |
4985 | if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { |
4986 | set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); | |
4987 | atomic_inc(&trans->transaction->pending_ordered); | |
4988 | } | |
54c65371 | 4989 | spin_unlock_irq(&inode->ordered_tree_lock); |
48778179 FM |
4990 | } |
4991 | btrfs_put_ordered_extent(ordered); | |
4992 | } | |
4993 | ||
4994 | return 0; | |
5dc562c5 JB |
4995 | } |
4996 | ||
481b01c0 | 4997 | static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode, |
1a4bcf47 FM |
4998 | struct btrfs_path *path, u64 *size_ret) |
4999 | { | |
5000 | struct btrfs_key key; | |
5001 | int ret; | |
5002 | ||
481b01c0 | 5003 | key.objectid = btrfs_ino(inode); |
1a4bcf47 FM |
5004 | key.type = BTRFS_INODE_ITEM_KEY; |
5005 | key.offset = 0; | |
5006 | ||
5007 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); | |
5008 | if (ret < 0) { | |
5009 | return ret; | |
5010 | } else if (ret > 0) { | |
2f2ff0ee | 5011 | *size_ret = 0; |
1a4bcf47 FM |
5012 | } else { |
5013 | struct btrfs_inode_item *item; | |
5014 | ||
5015 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
5016 | struct btrfs_inode_item); | |
5017 | *size_ret = btrfs_inode_size(path->nodes[0], item); | |
bf504110 FM |
5018 | /* |
5019 | * If the in-memory inode's i_size is smaller then the inode | |
5020 | * size stored in the btree, return the inode's i_size, so | |
5021 | * that we get a correct inode size after replaying the log | |
5022 | * when before a power failure we had a shrinking truncate | |
5023 | * followed by addition of a new name (rename / new hard link). | |
5024 | * Otherwise return the inode size from the btree, to avoid | |
5025 | * data loss when replaying a log due to previously doing a | |
5026 | * write that expands the inode's size and logging a new name | |
5027 | * immediately after. | |
5028 | */ | |
5029 | if (*size_ret > inode->vfs_inode.i_size) | |
5030 | *size_ret = inode->vfs_inode.i_size; | |
1a4bcf47 FM |
5031 | } |
5032 | ||
5033 | btrfs_release_path(path); | |
5034 | return 0; | |
5035 | } | |
5036 | ||
36283bf7 FM |
5037 | /* |
5038 | * At the moment we always log all xattrs. This is to figure out at log replay | |
5039 | * time which xattrs must have their deletion replayed. If a xattr is missing | |
5040 | * in the log tree and exists in the fs/subvol tree, we delete it. This is | |
5041 | * because if a xattr is deleted, the inode is fsynced and a power failure | |
5042 | * happens, causing the log to be replayed the next time the fs is mounted, | |
5043 | * we want the xattr to not exist anymore (same behaviour as other filesystems | |
5044 | * with a journal, ext3/4, xfs, f2fs, etc). | |
5045 | */ | |
5046 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, | |
1a93c36a | 5047 | struct btrfs_inode *inode, |
36283bf7 | 5048 | struct btrfs_path *path, |
e383e158 FM |
5049 | struct btrfs_path *dst_path, |
5050 | struct btrfs_log_ctx *ctx) | |
36283bf7 | 5051 | { |
90d04510 | 5052 | struct btrfs_root *root = inode->root; |
36283bf7 FM |
5053 | int ret; |
5054 | struct btrfs_key key; | |
1a93c36a | 5055 | const u64 ino = btrfs_ino(inode); |
36283bf7 FM |
5056 | int ins_nr = 0; |
5057 | int start_slot = 0; | |
f2f121ab FM |
5058 | bool found_xattrs = false; |
5059 | ||
5060 | if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags)) | |
5061 | return 0; | |
36283bf7 FM |
5062 | |
5063 | key.objectid = ino; | |
5064 | key.type = BTRFS_XATTR_ITEM_KEY; | |
5065 | key.offset = 0; | |
5066 | ||
5067 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5068 | if (ret < 0) | |
5069 | return ret; | |
5070 | ||
5071 | while (true) { | |
5072 | int slot = path->slots[0]; | |
5073 | struct extent_buffer *leaf = path->nodes[0]; | |
5074 | int nritems = btrfs_header_nritems(leaf); | |
5075 | ||
5076 | if (slot >= nritems) { | |
5077 | if (ins_nr > 0) { | |
1a93c36a | 5078 | ret = copy_items(trans, inode, dst_path, path, |
e383e158 | 5079 | start_slot, ins_nr, 1, 0, ctx); |
36283bf7 FM |
5080 | if (ret < 0) |
5081 | return ret; | |
5082 | ins_nr = 0; | |
5083 | } | |
5084 | ret = btrfs_next_leaf(root, path); | |
5085 | if (ret < 0) | |
5086 | return ret; | |
5087 | else if (ret > 0) | |
5088 | break; | |
5089 | continue; | |
5090 | } | |
5091 | ||
5092 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
5093 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) | |
5094 | break; | |
5095 | ||
5096 | if (ins_nr == 0) | |
5097 | start_slot = slot; | |
5098 | ins_nr++; | |
5099 | path->slots[0]++; | |
f2f121ab | 5100 | found_xattrs = true; |
36283bf7 FM |
5101 | cond_resched(); |
5102 | } | |
5103 | if (ins_nr > 0) { | |
1a93c36a | 5104 | ret = copy_items(trans, inode, dst_path, path, |
e383e158 | 5105 | start_slot, ins_nr, 1, 0, ctx); |
36283bf7 FM |
5106 | if (ret < 0) |
5107 | return ret; | |
5108 | } | |
5109 | ||
f2f121ab FM |
5110 | if (!found_xattrs) |
5111 | set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags); | |
5112 | ||
36283bf7 FM |
5113 | return 0; |
5114 | } | |
5115 | ||
a89ca6f2 | 5116 | /* |
0e56315c FM |
5117 | * When using the NO_HOLES feature if we punched a hole that causes the |
5118 | * deletion of entire leafs or all the extent items of the first leaf (the one | |
5119 | * that contains the inode item and references) we may end up not processing | |
5120 | * any extents, because there are no leafs with a generation matching the | |
5121 | * current transaction that have extent items for our inode. So we need to find | |
5122 | * if any holes exist and then log them. We also need to log holes after any | |
5123 | * truncate operation that changes the inode's size. | |
a89ca6f2 | 5124 | */ |
0e56315c | 5125 | static int btrfs_log_holes(struct btrfs_trans_handle *trans, |
0e56315c | 5126 | struct btrfs_inode *inode, |
7af59743 | 5127 | struct btrfs_path *path) |
a89ca6f2 | 5128 | { |
90d04510 | 5129 | struct btrfs_root *root = inode->root; |
0b246afa | 5130 | struct btrfs_fs_info *fs_info = root->fs_info; |
a89ca6f2 | 5131 | struct btrfs_key key; |
a0308dd7 NB |
5132 | const u64 ino = btrfs_ino(inode); |
5133 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
7af59743 | 5134 | u64 prev_extent_end = 0; |
0e56315c | 5135 | int ret; |
a89ca6f2 | 5136 | |
0e56315c | 5137 | if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0) |
a89ca6f2 FM |
5138 | return 0; |
5139 | ||
5140 | key.objectid = ino; | |
5141 | key.type = BTRFS_EXTENT_DATA_KEY; | |
7af59743 | 5142 | key.offset = 0; |
a89ca6f2 FM |
5143 | |
5144 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
a89ca6f2 FM |
5145 | if (ret < 0) |
5146 | return ret; | |
5147 | ||
0e56315c | 5148 | while (true) { |
0e56315c | 5149 | struct extent_buffer *leaf = path->nodes[0]; |
a89ca6f2 | 5150 | |
0e56315c FM |
5151 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
5152 | ret = btrfs_next_leaf(root, path); | |
5153 | if (ret < 0) | |
5154 | return ret; | |
5155 | if (ret > 0) { | |
5156 | ret = 0; | |
5157 | break; | |
5158 | } | |
5159 | leaf = path->nodes[0]; | |
5160 | } | |
5161 | ||
5162 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
5163 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
5164 | break; | |
5165 | ||
5166 | /* We have a hole, log it. */ | |
5167 | if (prev_extent_end < key.offset) { | |
7af59743 | 5168 | const u64 hole_len = key.offset - prev_extent_end; |
0e56315c FM |
5169 | |
5170 | /* | |
5171 | * Release the path to avoid deadlocks with other code | |
5172 | * paths that search the root while holding locks on | |
5173 | * leafs from the log root. | |
5174 | */ | |
5175 | btrfs_release_path(path); | |
d1f68ba0 OS |
5176 | ret = btrfs_insert_hole_extent(trans, root->log_root, |
5177 | ino, prev_extent_end, | |
5178 | hole_len); | |
0e56315c FM |
5179 | if (ret < 0) |
5180 | return ret; | |
5181 | ||
5182 | /* | |
5183 | * Search for the same key again in the root. Since it's | |
5184 | * an extent item and we are holding the inode lock, the | |
5185 | * key must still exist. If it doesn't just emit warning | |
5186 | * and return an error to fall back to a transaction | |
5187 | * commit. | |
5188 | */ | |
5189 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5190 | if (ret < 0) | |
5191 | return ret; | |
5192 | if (WARN_ON(ret > 0)) | |
5193 | return -ENOENT; | |
5194 | leaf = path->nodes[0]; | |
5195 | } | |
a89ca6f2 | 5196 | |
7af59743 | 5197 | prev_extent_end = btrfs_file_extent_end(path); |
0e56315c FM |
5198 | path->slots[0]++; |
5199 | cond_resched(); | |
a89ca6f2 | 5200 | } |
a89ca6f2 | 5201 | |
7af59743 | 5202 | if (prev_extent_end < i_size) { |
0e56315c | 5203 | u64 hole_len; |
a89ca6f2 | 5204 | |
0e56315c | 5205 | btrfs_release_path(path); |
7af59743 | 5206 | hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize); |
d1f68ba0 OS |
5207 | ret = btrfs_insert_hole_extent(trans, root->log_root, ino, |
5208 | prev_extent_end, hole_len); | |
0e56315c FM |
5209 | if (ret < 0) |
5210 | return ret; | |
5211 | } | |
5212 | ||
5213 | return 0; | |
a89ca6f2 FM |
5214 | } |
5215 | ||
56f23fdb FM |
5216 | /* |
5217 | * When we are logging a new inode X, check if it doesn't have a reference that | |
5218 | * matches the reference from some other inode Y created in a past transaction | |
5219 | * and that was renamed in the current transaction. If we don't do this, then at | |
5220 | * log replay time we can lose inode Y (and all its files if it's a directory): | |
5221 | * | |
5222 | * mkdir /mnt/x | |
5223 | * echo "hello world" > /mnt/x/foobar | |
5224 | * sync | |
5225 | * mv /mnt/x /mnt/y | |
5226 | * mkdir /mnt/x # or touch /mnt/x | |
5227 | * xfs_io -c fsync /mnt/x | |
5228 | * <power fail> | |
5229 | * mount fs, trigger log replay | |
5230 | * | |
5231 | * After the log replay procedure, we would lose the first directory and all its | |
5232 | * files (file foobar). | |
5233 | * For the case where inode Y is not a directory we simply end up losing it: | |
5234 | * | |
5235 | * echo "123" > /mnt/foo | |
5236 | * sync | |
5237 | * mv /mnt/foo /mnt/bar | |
5238 | * echo "abc" > /mnt/foo | |
5239 | * xfs_io -c fsync /mnt/foo | |
5240 | * <power fail> | |
5241 | * | |
5242 | * We also need this for cases where a snapshot entry is replaced by some other | |
5243 | * entry (file or directory) otherwise we end up with an unreplayable log due to | |
5244 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as | |
5245 | * if it were a regular entry: | |
5246 | * | |
5247 | * mkdir /mnt/x | |
5248 | * btrfs subvolume snapshot /mnt /mnt/x/snap | |
5249 | * btrfs subvolume delete /mnt/x/snap | |
5250 | * rmdir /mnt/x | |
5251 | * mkdir /mnt/x | |
5252 | * fsync /mnt/x or fsync some new file inside it | |
5253 | * <power fail> | |
5254 | * | |
5255 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in | |
5256 | * the same transaction. | |
5257 | */ | |
5258 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, | |
5259 | const int slot, | |
5260 | const struct btrfs_key *key, | |
4791c8f1 | 5261 | struct btrfs_inode *inode, |
a3baaf0d | 5262 | u64 *other_ino, u64 *other_parent) |
56f23fdb FM |
5263 | { |
5264 | int ret; | |
5265 | struct btrfs_path *search_path; | |
5266 | char *name = NULL; | |
5267 | u32 name_len = 0; | |
3212fa14 | 5268 | u32 item_size = btrfs_item_size(eb, slot); |
56f23fdb FM |
5269 | u32 cur_offset = 0; |
5270 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); | |
5271 | ||
5272 | search_path = btrfs_alloc_path(); | |
5273 | if (!search_path) | |
5274 | return -ENOMEM; | |
5275 | search_path->search_commit_root = 1; | |
5276 | search_path->skip_locking = 1; | |
5277 | ||
5278 | while (cur_offset < item_size) { | |
5279 | u64 parent; | |
5280 | u32 this_name_len; | |
5281 | u32 this_len; | |
5282 | unsigned long name_ptr; | |
5283 | struct btrfs_dir_item *di; | |
6db75318 | 5284 | struct fscrypt_str name_str; |
56f23fdb FM |
5285 | |
5286 | if (key->type == BTRFS_INODE_REF_KEY) { | |
5287 | struct btrfs_inode_ref *iref; | |
5288 | ||
5289 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); | |
5290 | parent = key->offset; | |
5291 | this_name_len = btrfs_inode_ref_name_len(eb, iref); | |
5292 | name_ptr = (unsigned long)(iref + 1); | |
5293 | this_len = sizeof(*iref) + this_name_len; | |
5294 | } else { | |
5295 | struct btrfs_inode_extref *extref; | |
5296 | ||
5297 | extref = (struct btrfs_inode_extref *)(ptr + | |
5298 | cur_offset); | |
5299 | parent = btrfs_inode_extref_parent(eb, extref); | |
5300 | this_name_len = btrfs_inode_extref_name_len(eb, extref); | |
5301 | name_ptr = (unsigned long)&extref->name; | |
5302 | this_len = sizeof(*extref) + this_name_len; | |
5303 | } | |
5304 | ||
5305 | if (this_name_len > name_len) { | |
5306 | char *new_name; | |
5307 | ||
5308 | new_name = krealloc(name, this_name_len, GFP_NOFS); | |
5309 | if (!new_name) { | |
5310 | ret = -ENOMEM; | |
5311 | goto out; | |
5312 | } | |
5313 | name_len = this_name_len; | |
5314 | name = new_name; | |
5315 | } | |
5316 | ||
5317 | read_extent_buffer(eb, name, name_ptr, this_name_len); | |
e43eec81 STD |
5318 | |
5319 | name_str.name = name; | |
5320 | name_str.len = this_name_len; | |
4791c8f1 | 5321 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, |
e43eec81 | 5322 | parent, &name_str, 0); |
56f23fdb | 5323 | if (di && !IS_ERR(di)) { |
44f714da FM |
5324 | struct btrfs_key di_key; |
5325 | ||
5326 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], | |
5327 | di, &di_key); | |
5328 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { | |
6b5fc433 FM |
5329 | if (di_key.objectid != key->objectid) { |
5330 | ret = 1; | |
5331 | *other_ino = di_key.objectid; | |
a3baaf0d | 5332 | *other_parent = parent; |
6b5fc433 FM |
5333 | } else { |
5334 | ret = 0; | |
5335 | } | |
44f714da FM |
5336 | } else { |
5337 | ret = -EAGAIN; | |
5338 | } | |
56f23fdb FM |
5339 | goto out; |
5340 | } else if (IS_ERR(di)) { | |
5341 | ret = PTR_ERR(di); | |
5342 | goto out; | |
5343 | } | |
5344 | btrfs_release_path(search_path); | |
5345 | ||
5346 | cur_offset += this_len; | |
5347 | } | |
5348 | ret = 0; | |
5349 | out: | |
5350 | btrfs_free_path(search_path); | |
5351 | kfree(name); | |
5352 | return ret; | |
5353 | } | |
5354 | ||
a3751024 FM |
5355 | /* |
5356 | * Check if we need to log an inode. This is used in contexts where while | |
5357 | * logging an inode we need to log another inode (either that it exists or in | |
5358 | * full mode). This is used instead of btrfs_inode_in_log() because the later | |
5359 | * requires the inode to be in the log and have the log transaction committed, | |
5360 | * while here we do not care if the log transaction was already committed - our | |
5361 | * caller will commit the log later - and we want to avoid logging an inode | |
5362 | * multiple times when multiple tasks have joined the same log transaction. | |
5363 | */ | |
5364 | static bool need_log_inode(const struct btrfs_trans_handle *trans, | |
bf1f4fd3 | 5365 | struct btrfs_inode *inode) |
a3751024 FM |
5366 | { |
5367 | /* | |
5368 | * If a directory was not modified, no dentries added or removed, we can | |
5369 | * and should avoid logging it. | |
5370 | */ | |
5371 | if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid) | |
5372 | return false; | |
5373 | ||
5374 | /* | |
5375 | * If this inode does not have new/updated/deleted xattrs since the last | |
5376 | * time it was logged and is flagged as logged in the current transaction, | |
5377 | * we can skip logging it. As for new/deleted names, those are updated in | |
5378 | * the log by link/unlink/rename operations. | |
5379 | * In case the inode was logged and then evicted and reloaded, its | |
5380 | * logged_trans will be 0, in which case we have to fully log it since | |
5381 | * logged_trans is a transient field, not persisted. | |
5382 | */ | |
bf1f4fd3 | 5383 | if (inode_logged(trans, inode, NULL) == 1 && |
a3751024 FM |
5384 | !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags)) |
5385 | return false; | |
5386 | ||
5387 | return true; | |
5388 | } | |
5389 | ||
f6d86dbe FM |
5390 | struct btrfs_dir_list { |
5391 | u64 ino; | |
5392 | struct list_head list; | |
5393 | }; | |
5394 | ||
5395 | /* | |
5396 | * Log the inodes of the new dentries of a directory. | |
5397 | * See process_dir_items_leaf() for details about why it is needed. | |
5398 | * This is a recursive operation - if an existing dentry corresponds to a | |
5399 | * directory, that directory's new entries are logged too (same behaviour as | |
5400 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes | |
5401 | * the dentries point to we do not acquire their VFS lock, otherwise lockdep | |
5402 | * complains about the following circular lock dependency / possible deadlock: | |
5403 | * | |
5404 | * CPU0 CPU1 | |
5405 | * ---- ---- | |
5406 | * lock(&type->i_mutex_dir_key#3/2); | |
5407 | * lock(sb_internal#2); | |
5408 | * lock(&type->i_mutex_dir_key#3/2); | |
5409 | * lock(&sb->s_type->i_mutex_key#14); | |
5410 | * | |
5411 | * Where sb_internal is the lock (a counter that works as a lock) acquired by | |
5412 | * sb_start_intwrite() in btrfs_start_transaction(). | |
5413 | * Not acquiring the VFS lock of the inodes is still safe because: | |
5414 | * | |
5415 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible | |
5416 | * that while logging the inode new references (names) are added or removed | |
5417 | * from the inode, leaving the logged inode item with a link count that does | |
5418 | * not match the number of logged inode reference items. This is fine because | |
5419 | * at log replay time we compute the real number of links and correct the | |
5420 | * link count in the inode item (see replay_one_buffer() and | |
5421 | * link_to_fixup_dir()); | |
5422 | * | |
5423 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that | |
5424 | * while logging the inode's items new index items (key type | |
5425 | * BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item | |
5426 | * has a size that doesn't match the sum of the lengths of all the logged | |
5427 | * names - this is ok, not a problem, because at log replay time we set the | |
5428 | * directory's i_size to the correct value (see replay_one_name() and | |
3a8d1db3 | 5429 | * overwrite_item()). |
f6d86dbe FM |
5430 | */ |
5431 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, | |
5432 | struct btrfs_inode *start_inode, | |
5433 | struct btrfs_log_ctx *ctx) | |
5434 | { | |
5435 | struct btrfs_root *root = start_inode->root; | |
5436 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5437 | struct btrfs_path *path; | |
5438 | LIST_HEAD(dir_list); | |
5439 | struct btrfs_dir_list *dir_elem; | |
5440 | u64 ino = btrfs_ino(start_inode); | |
fa4b8cb1 | 5441 | struct btrfs_inode *curr_inode = start_inode; |
f6d86dbe FM |
5442 | int ret = 0; |
5443 | ||
5444 | /* | |
5445 | * If we are logging a new name, as part of a link or rename operation, | |
5446 | * don't bother logging new dentries, as we just want to log the names | |
5447 | * of an inode and that any new parents exist. | |
5448 | */ | |
5449 | if (ctx->logging_new_name) | |
5450 | return 0; | |
5451 | ||
5452 | path = btrfs_alloc_path(); | |
5453 | if (!path) | |
5454 | return -ENOMEM; | |
5455 | ||
fa4b8cb1 FM |
5456 | /* Pairs with btrfs_add_delayed_iput below. */ |
5457 | ihold(&curr_inode->vfs_inode); | |
5458 | ||
f6d86dbe | 5459 | while (true) { |
fa4b8cb1 | 5460 | struct inode *vfs_inode; |
5d3e4f1d FM |
5461 | struct btrfs_key key; |
5462 | struct btrfs_key found_key; | |
fa4b8cb1 | 5463 | u64 next_index; |
f6d86dbe | 5464 | bool continue_curr_inode = true; |
5d3e4f1d | 5465 | int iter_ret; |
f6d86dbe | 5466 | |
5d3e4f1d FM |
5467 | key.objectid = ino; |
5468 | key.type = BTRFS_DIR_INDEX_KEY; | |
5469 | key.offset = btrfs_get_first_dir_index_to_log(curr_inode); | |
5470 | next_index = key.offset; | |
f6d86dbe | 5471 | again: |
5d3e4f1d FM |
5472 | btrfs_for_each_slot(root->log_root, &key, &found_key, path, iter_ret) { |
5473 | struct extent_buffer *leaf = path->nodes[0]; | |
f6d86dbe FM |
5474 | struct btrfs_dir_item *di; |
5475 | struct btrfs_key di_key; | |
5476 | struct inode *di_inode; | |
5477 | int log_mode = LOG_INODE_EXISTS; | |
5478 | int type; | |
5479 | ||
5d3e4f1d FM |
5480 | if (found_key.objectid != ino || |
5481 | found_key.type != BTRFS_DIR_INDEX_KEY) { | |
f6d86dbe FM |
5482 | continue_curr_inode = false; |
5483 | break; | |
5484 | } | |
5485 | ||
5d3e4f1d | 5486 | next_index = found_key.offset + 1; |
fa4b8cb1 | 5487 | |
5d3e4f1d | 5488 | di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); |
94a48aef | 5489 | type = btrfs_dir_ftype(leaf, di); |
f6d86dbe FM |
5490 | if (btrfs_dir_transid(leaf, di) < trans->transid) |
5491 | continue; | |
5492 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); | |
5493 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) | |
5494 | continue; | |
5495 | ||
5496 | btrfs_release_path(path); | |
5497 | di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root); | |
5498 | if (IS_ERR(di_inode)) { | |
5499 | ret = PTR_ERR(di_inode); | |
5500 | goto out; | |
5501 | } | |
5502 | ||
5503 | if (!need_log_inode(trans, BTRFS_I(di_inode))) { | |
e55cf7ca | 5504 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
f6d86dbe FM |
5505 | break; |
5506 | } | |
5507 | ||
5508 | ctx->log_new_dentries = false; | |
5509 | if (type == BTRFS_FT_DIR) | |
5510 | log_mode = LOG_INODE_ALL; | |
5511 | ret = btrfs_log_inode(trans, BTRFS_I(di_inode), | |
5512 | log_mode, ctx); | |
e55cf7ca | 5513 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
f6d86dbe FM |
5514 | if (ret) |
5515 | goto out; | |
5516 | if (ctx->log_new_dentries) { | |
5517 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); | |
5518 | if (!dir_elem) { | |
5519 | ret = -ENOMEM; | |
5520 | goto out; | |
5521 | } | |
5522 | dir_elem->ino = di_key.objectid; | |
5523 | list_add_tail(&dir_elem->list, &dir_list); | |
5524 | } | |
5525 | break; | |
5526 | } | |
5527 | ||
fa4b8cb1 FM |
5528 | btrfs_release_path(path); |
5529 | ||
5d3e4f1d FM |
5530 | if (iter_ret < 0) { |
5531 | ret = iter_ret; | |
5532 | goto out; | |
5533 | } else if (iter_ret > 0) { | |
5534 | continue_curr_inode = false; | |
5535 | } else { | |
5536 | key = found_key; | |
5537 | } | |
5538 | ||
5539 | if (continue_curr_inode && key.offset < (u64)-1) { | |
5540 | key.offset++; | |
f6d86dbe FM |
5541 | goto again; |
5542 | } | |
5543 | ||
fa4b8cb1 FM |
5544 | btrfs_set_first_dir_index_to_log(curr_inode, next_index); |
5545 | ||
f6d86dbe FM |
5546 | if (list_empty(&dir_list)) |
5547 | break; | |
5548 | ||
5549 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list); | |
5550 | ino = dir_elem->ino; | |
5551 | list_del(&dir_elem->list); | |
5552 | kfree(dir_elem); | |
fa4b8cb1 FM |
5553 | |
5554 | btrfs_add_delayed_iput(curr_inode); | |
5555 | curr_inode = NULL; | |
5556 | ||
5557 | vfs_inode = btrfs_iget(fs_info->sb, ino, root); | |
5558 | if (IS_ERR(vfs_inode)) { | |
5559 | ret = PTR_ERR(vfs_inode); | |
5560 | break; | |
5561 | } | |
5562 | curr_inode = BTRFS_I(vfs_inode); | |
f6d86dbe FM |
5563 | } |
5564 | out: | |
5565 | btrfs_free_path(path); | |
fa4b8cb1 FM |
5566 | if (curr_inode) |
5567 | btrfs_add_delayed_iput(curr_inode); | |
5568 | ||
f6d86dbe FM |
5569 | if (ret) { |
5570 | struct btrfs_dir_list *next; | |
5571 | ||
5572 | list_for_each_entry_safe(dir_elem, next, &dir_list, list) | |
5573 | kfree(dir_elem); | |
5574 | } | |
5575 | ||
5576 | return ret; | |
5577 | } | |
5578 | ||
6b5fc433 FM |
5579 | struct btrfs_ino_list { |
5580 | u64 ino; | |
a3baaf0d | 5581 | u64 parent; |
6b5fc433 FM |
5582 | struct list_head list; |
5583 | }; | |
5584 | ||
e09d94c9 FM |
5585 | static void free_conflicting_inodes(struct btrfs_log_ctx *ctx) |
5586 | { | |
5587 | struct btrfs_ino_list *curr; | |
5588 | struct btrfs_ino_list *next; | |
5589 | ||
5590 | list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) { | |
5591 | list_del(&curr->list); | |
5592 | kfree(curr); | |
5593 | } | |
5594 | } | |
5595 | ||
5557a069 FM |
5596 | static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino, |
5597 | struct btrfs_path *path) | |
5598 | { | |
5599 | struct btrfs_key key; | |
5600 | int ret; | |
5601 | ||
5602 | key.objectid = ino; | |
5603 | key.type = BTRFS_INODE_ITEM_KEY; | |
5604 | key.offset = 0; | |
5605 | ||
5606 | path->search_commit_root = 1; | |
5607 | path->skip_locking = 1; | |
5608 | ||
5609 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5610 | if (WARN_ON_ONCE(ret > 0)) { | |
5611 | /* | |
5612 | * We have previously found the inode through the commit root | |
5613 | * so this should not happen. If it does, just error out and | |
5614 | * fallback to a transaction commit. | |
5615 | */ | |
5616 | ret = -ENOENT; | |
5617 | } else if (ret == 0) { | |
5618 | struct btrfs_inode_item *item; | |
5619 | ||
5620 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
5621 | struct btrfs_inode_item); | |
5622 | if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item))) | |
5623 | ret = 1; | |
5624 | } | |
5625 | ||
5626 | btrfs_release_path(path); | |
5627 | path->search_commit_root = 0; | |
5628 | path->skip_locking = 0; | |
5629 | ||
5630 | return ret; | |
5631 | } | |
5632 | ||
e09d94c9 FM |
5633 | static int add_conflicting_inode(struct btrfs_trans_handle *trans, |
5634 | struct btrfs_root *root, | |
5557a069 | 5635 | struct btrfs_path *path, |
e09d94c9 FM |
5636 | u64 ino, u64 parent, |
5637 | struct btrfs_log_ctx *ctx) | |
6b5fc433 FM |
5638 | { |
5639 | struct btrfs_ino_list *ino_elem; | |
e09d94c9 FM |
5640 | struct inode *inode; |
5641 | ||
5642 | /* | |
5643 | * It's rare to have a lot of conflicting inodes, in practice it is not | |
5644 | * common to have more than 1 or 2. We don't want to collect too many, | |
5645 | * as we could end up logging too many inodes (even if only in | |
5646 | * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction | |
5647 | * commits. | |
5648 | */ | |
5cce1780 | 5649 | if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES) |
e09d94c9 FM |
5650 | return BTRFS_LOG_FORCE_COMMIT; |
5651 | ||
5652 | inode = btrfs_iget(root->fs_info->sb, ino, root); | |
5653 | /* | |
5654 | * If the other inode that had a conflicting dir entry was deleted in | |
5557a069 FM |
5655 | * the current transaction then we either: |
5656 | * | |
5657 | * 1) Log the parent directory (later after adding it to the list) if | |
5658 | * the inode is a directory. This is because it may be a deleted | |
5659 | * subvolume/snapshot or it may be a regular directory that had | |
5660 | * deleted subvolumes/snapshots (or subdirectories that had them), | |
5661 | * and at the moment we can't deal with dropping subvolumes/snapshots | |
5662 | * during log replay. So we just log the parent, which will result in | |
5663 | * a fallback to a transaction commit if we are dealing with those | |
5664 | * cases (last_unlink_trans will match the current transaction); | |
5665 | * | |
5666 | * 2) Do nothing if it's not a directory. During log replay we simply | |
5667 | * unlink the conflicting dentry from the parent directory and then | |
5668 | * add the dentry for our inode. Like this we can avoid logging the | |
5669 | * parent directory (and maybe fallback to a transaction commit in | |
5670 | * case it has a last_unlink_trans == trans->transid, due to moving | |
5671 | * some inode from it to some other directory). | |
e09d94c9 FM |
5672 | */ |
5673 | if (IS_ERR(inode)) { | |
5674 | int ret = PTR_ERR(inode); | |
5675 | ||
5676 | if (ret != -ENOENT) | |
5677 | return ret; | |
5678 | ||
5557a069 FM |
5679 | ret = conflicting_inode_is_dir(root, ino, path); |
5680 | /* Not a directory or we got an error. */ | |
5681 | if (ret <= 0) | |
5682 | return ret; | |
5683 | ||
5684 | /* Conflicting inode is a directory, so we'll log its parent. */ | |
e09d94c9 FM |
5685 | ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); |
5686 | if (!ino_elem) | |
5687 | return -ENOMEM; | |
5688 | ino_elem->ino = ino; | |
5689 | ino_elem->parent = parent; | |
5690 | list_add_tail(&ino_elem->list, &ctx->conflict_inodes); | |
5691 | ctx->num_conflict_inodes++; | |
5692 | ||
5693 | return 0; | |
5694 | } | |
5695 | ||
5696 | /* | |
5697 | * If the inode was already logged skip it - otherwise we can hit an | |
5698 | * infinite loop. Example: | |
5699 | * | |
5700 | * From the commit root (previous transaction) we have the following | |
5701 | * inodes: | |
5702 | * | |
5703 | * inode 257 a directory | |
5704 | * inode 258 with references "zz" and "zz_link" on inode 257 | |
5705 | * inode 259 with reference "a" on inode 257 | |
5706 | * | |
5707 | * And in the current (uncommitted) transaction we have: | |
5708 | * | |
5709 | * inode 257 a directory, unchanged | |
5710 | * inode 258 with references "a" and "a2" on inode 257 | |
5711 | * inode 259 with reference "zz_link" on inode 257 | |
5712 | * inode 261 with reference "zz" on inode 257 | |
5713 | * | |
5714 | * When logging inode 261 the following infinite loop could | |
5715 | * happen if we don't skip already logged inodes: | |
5716 | * | |
5717 | * - we detect inode 258 as a conflicting inode, with inode 261 | |
5718 | * on reference "zz", and log it; | |
5719 | * | |
5720 | * - we detect inode 259 as a conflicting inode, with inode 258 | |
5721 | * on reference "a", and log it; | |
5722 | * | |
5723 | * - we detect inode 258 as a conflicting inode, with inode 259 | |
5724 | * on reference "zz_link", and log it - again! After this we | |
5725 | * repeat the above steps forever. | |
5726 | * | |
5727 | * Here we can use need_log_inode() because we only need to log the | |
5728 | * inode in LOG_INODE_EXISTS mode and rename operations update the log, | |
5729 | * so that the log ends up with the new name and without the old name. | |
5730 | */ | |
5731 | if (!need_log_inode(trans, BTRFS_I(inode))) { | |
e55cf7ca | 5732 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
e09d94c9 FM |
5733 | return 0; |
5734 | } | |
5735 | ||
e55cf7ca | 5736 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
6b5fc433 FM |
5737 | |
5738 | ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); | |
5739 | if (!ino_elem) | |
5740 | return -ENOMEM; | |
5741 | ino_elem->ino = ino; | |
a3baaf0d | 5742 | ino_elem->parent = parent; |
e09d94c9 FM |
5743 | list_add_tail(&ino_elem->list, &ctx->conflict_inodes); |
5744 | ctx->num_conflict_inodes++; | |
6b5fc433 | 5745 | |
e09d94c9 FM |
5746 | return 0; |
5747 | } | |
6b5fc433 | 5748 | |
e09d94c9 FM |
5749 | static int log_conflicting_inodes(struct btrfs_trans_handle *trans, |
5750 | struct btrfs_root *root, | |
5751 | struct btrfs_log_ctx *ctx) | |
5752 | { | |
5753 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5754 | int ret = 0; | |
6b5fc433 | 5755 | |
e09d94c9 FM |
5756 | /* |
5757 | * Conflicting inodes are logged by the first call to btrfs_log_inode(), | |
5758 | * otherwise we could have unbounded recursion of btrfs_log_inode() | |
5759 | * calls. This check guarantees we can have only 1 level of recursion. | |
5760 | */ | |
5761 | if (ctx->logging_conflict_inodes) | |
5762 | return 0; | |
5763 | ||
5764 | ctx->logging_conflict_inodes = true; | |
5765 | ||
5766 | /* | |
5767 | * New conflicting inodes may be found and added to the list while we | |
5768 | * are logging a conflicting inode, so keep iterating while the list is | |
5769 | * not empty. | |
5770 | */ | |
5771 | while (!list_empty(&ctx->conflict_inodes)) { | |
5772 | struct btrfs_ino_list *curr; | |
5773 | struct inode *inode; | |
5774 | u64 ino; | |
5775 | u64 parent; | |
5776 | ||
5777 | curr = list_first_entry(&ctx->conflict_inodes, | |
5778 | struct btrfs_ino_list, list); | |
5779 | ino = curr->ino; | |
5780 | parent = curr->parent; | |
5781 | list_del(&curr->list); | |
5782 | kfree(curr); | |
6b5fc433 | 5783 | |
0202e83f | 5784 | inode = btrfs_iget(fs_info->sb, ino, root); |
6b5fc433 FM |
5785 | /* |
5786 | * If the other inode that had a conflicting dir entry was | |
a3baaf0d | 5787 | * deleted in the current transaction, we need to log its parent |
e09d94c9 | 5788 | * directory. See the comment at add_conflicting_inode(). |
6b5fc433 FM |
5789 | */ |
5790 | if (IS_ERR(inode)) { | |
5791 | ret = PTR_ERR(inode); | |
e09d94c9 FM |
5792 | if (ret != -ENOENT) |
5793 | break; | |
5794 | ||
5795 | inode = btrfs_iget(fs_info->sb, parent, root); | |
5796 | if (IS_ERR(inode)) { | |
5797 | ret = PTR_ERR(inode); | |
5798 | break; | |
a3baaf0d | 5799 | } |
e09d94c9 FM |
5800 | |
5801 | /* | |
5802 | * Always log the directory, we cannot make this | |
5803 | * conditional on need_log_inode() because the directory | |
5804 | * might have been logged in LOG_INODE_EXISTS mode or | |
5805 | * the dir index of the conflicting inode is not in a | |
5806 | * dir index key range logged for the directory. So we | |
5807 | * must make sure the deletion is recorded. | |
5808 | */ | |
5809 | ret = btrfs_log_inode(trans, BTRFS_I(inode), | |
5810 | LOG_INODE_ALL, ctx); | |
e55cf7ca | 5811 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
e09d94c9 FM |
5812 | if (ret) |
5813 | break; | |
6b5fc433 FM |
5814 | continue; |
5815 | } | |
e09d94c9 | 5816 | |
b5e4ff9d | 5817 | /* |
e09d94c9 FM |
5818 | * Here we can use need_log_inode() because we only need to log |
5819 | * the inode in LOG_INODE_EXISTS mode and rename operations | |
5820 | * update the log, so that the log ends up with the new name and | |
5821 | * without the old name. | |
b5e4ff9d | 5822 | * |
e09d94c9 FM |
5823 | * We did this check at add_conflicting_inode(), but here we do |
5824 | * it again because if some other task logged the inode after | |
5825 | * that, we can avoid doing it again. | |
b5e4ff9d | 5826 | */ |
e09d94c9 | 5827 | if (!need_log_inode(trans, BTRFS_I(inode))) { |
e55cf7ca | 5828 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
b5e4ff9d FM |
5829 | continue; |
5830 | } | |
e09d94c9 | 5831 | |
6b5fc433 FM |
5832 | /* |
5833 | * We are safe logging the other inode without acquiring its | |
5834 | * lock as long as we log with the LOG_INODE_EXISTS mode. We | |
5835 | * are safe against concurrent renames of the other inode as | |
5836 | * well because during a rename we pin the log and update the | |
5837 | * log with the new name before we unpin it. | |
5838 | */ | |
e09d94c9 | 5839 | ret = btrfs_log_inode(trans, BTRFS_I(inode), LOG_INODE_EXISTS, ctx); |
e55cf7ca | 5840 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
e09d94c9 FM |
5841 | if (ret) |
5842 | break; | |
6b5fc433 FM |
5843 | } |
5844 | ||
e09d94c9 FM |
5845 | ctx->logging_conflict_inodes = false; |
5846 | if (ret) | |
5847 | free_conflicting_inodes(ctx); | |
5848 | ||
6b5fc433 FM |
5849 | return ret; |
5850 | } | |
5851 | ||
da447009 FM |
5852 | static int copy_inode_items_to_log(struct btrfs_trans_handle *trans, |
5853 | struct btrfs_inode *inode, | |
5854 | struct btrfs_key *min_key, | |
5855 | const struct btrfs_key *max_key, | |
5856 | struct btrfs_path *path, | |
5857 | struct btrfs_path *dst_path, | |
5858 | const u64 logged_isize, | |
da447009 FM |
5859 | const int inode_only, |
5860 | struct btrfs_log_ctx *ctx, | |
5861 | bool *need_log_inode_item) | |
5862 | { | |
d9947887 | 5863 | const u64 i_size = i_size_read(&inode->vfs_inode); |
da447009 FM |
5864 | struct btrfs_root *root = inode->root; |
5865 | int ins_start_slot = 0; | |
5866 | int ins_nr = 0; | |
5867 | int ret; | |
5868 | ||
5869 | while (1) { | |
5870 | ret = btrfs_search_forward(root, min_key, path, trans->transid); | |
5871 | if (ret < 0) | |
5872 | return ret; | |
5873 | if (ret > 0) { | |
5874 | ret = 0; | |
5875 | break; | |
5876 | } | |
5877 | again: | |
5878 | /* Note, ins_nr might be > 0 here, cleanup outside the loop */ | |
5879 | if (min_key->objectid != max_key->objectid) | |
5880 | break; | |
5881 | if (min_key->type > max_key->type) | |
5882 | break; | |
5883 | ||
d9947887 | 5884 | if (min_key->type == BTRFS_INODE_ITEM_KEY) { |
da447009 | 5885 | *need_log_inode_item = false; |
d9947887 FM |
5886 | } else if (min_key->type == BTRFS_EXTENT_DATA_KEY && |
5887 | min_key->offset >= i_size) { | |
5888 | /* | |
5889 | * Extents at and beyond eof are logged with | |
5890 | * btrfs_log_prealloc_extents(). | |
5891 | * Only regular files have BTRFS_EXTENT_DATA_KEY keys, | |
5892 | * and no keys greater than that, so bail out. | |
5893 | */ | |
5894 | break; | |
5895 | } else if ((min_key->type == BTRFS_INODE_REF_KEY || | |
5896 | min_key->type == BTRFS_INODE_EXTREF_KEY) && | |
e09d94c9 FM |
5897 | (inode->generation == trans->transid || |
5898 | ctx->logging_conflict_inodes)) { | |
da447009 FM |
5899 | u64 other_ino = 0; |
5900 | u64 other_parent = 0; | |
5901 | ||
5902 | ret = btrfs_check_ref_name_override(path->nodes[0], | |
5903 | path->slots[0], min_key, inode, | |
5904 | &other_ino, &other_parent); | |
5905 | if (ret < 0) { | |
5906 | return ret; | |
289cffcb | 5907 | } else if (ret > 0 && |
da447009 FM |
5908 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { |
5909 | if (ins_nr > 0) { | |
5910 | ins_nr++; | |
5911 | } else { | |
5912 | ins_nr = 1; | |
5913 | ins_start_slot = path->slots[0]; | |
5914 | } | |
5915 | ret = copy_items(trans, inode, dst_path, path, | |
5916 | ins_start_slot, ins_nr, | |
e383e158 | 5917 | inode_only, logged_isize, ctx); |
da447009 FM |
5918 | if (ret < 0) |
5919 | return ret; | |
5920 | ins_nr = 0; | |
5921 | ||
e09d94c9 | 5922 | btrfs_release_path(path); |
5557a069 | 5923 | ret = add_conflicting_inode(trans, root, path, |
e09d94c9 FM |
5924 | other_ino, |
5925 | other_parent, ctx); | |
da447009 FM |
5926 | if (ret) |
5927 | return ret; | |
da447009 FM |
5928 | goto next_key; |
5929 | } | |
d9947887 FM |
5930 | } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) { |
5931 | /* Skip xattrs, logged later with btrfs_log_all_xattrs() */ | |
da447009 FM |
5932 | if (ins_nr == 0) |
5933 | goto next_slot; | |
5934 | ret = copy_items(trans, inode, dst_path, path, | |
5935 | ins_start_slot, | |
e383e158 | 5936 | ins_nr, inode_only, logged_isize, ctx); |
da447009 FM |
5937 | if (ret < 0) |
5938 | return ret; | |
5939 | ins_nr = 0; | |
5940 | goto next_slot; | |
5941 | } | |
5942 | ||
5943 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { | |
5944 | ins_nr++; | |
5945 | goto next_slot; | |
5946 | } else if (!ins_nr) { | |
5947 | ins_start_slot = path->slots[0]; | |
5948 | ins_nr = 1; | |
5949 | goto next_slot; | |
5950 | } | |
5951 | ||
5952 | ret = copy_items(trans, inode, dst_path, path, ins_start_slot, | |
e383e158 | 5953 | ins_nr, inode_only, logged_isize, ctx); |
da447009 FM |
5954 | if (ret < 0) |
5955 | return ret; | |
5956 | ins_nr = 1; | |
5957 | ins_start_slot = path->slots[0]; | |
5958 | next_slot: | |
5959 | path->slots[0]++; | |
5960 | if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) { | |
5961 | btrfs_item_key_to_cpu(path->nodes[0], min_key, | |
5962 | path->slots[0]); | |
5963 | goto again; | |
5964 | } | |
5965 | if (ins_nr) { | |
5966 | ret = copy_items(trans, inode, dst_path, path, | |
5967 | ins_start_slot, ins_nr, inode_only, | |
e383e158 | 5968 | logged_isize, ctx); |
da447009 FM |
5969 | if (ret < 0) |
5970 | return ret; | |
5971 | ins_nr = 0; | |
5972 | } | |
5973 | btrfs_release_path(path); | |
5974 | next_key: | |
5975 | if (min_key->offset < (u64)-1) { | |
5976 | min_key->offset++; | |
5977 | } else if (min_key->type < max_key->type) { | |
5978 | min_key->type++; | |
5979 | min_key->offset = 0; | |
5980 | } else { | |
5981 | break; | |
5982 | } | |
96acb375 FM |
5983 | |
5984 | /* | |
5985 | * We may process many leaves full of items for our inode, so | |
5986 | * avoid monopolizing a cpu for too long by rescheduling while | |
5987 | * not holding locks on any tree. | |
5988 | */ | |
5989 | cond_resched(); | |
da447009 | 5990 | } |
d9947887 | 5991 | if (ins_nr) { |
da447009 | 5992 | ret = copy_items(trans, inode, dst_path, path, ins_start_slot, |
e383e158 | 5993 | ins_nr, inode_only, logged_isize, ctx); |
d9947887 FM |
5994 | if (ret) |
5995 | return ret; | |
5996 | } | |
5997 | ||
5998 | if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) { | |
5999 | /* | |
6000 | * Release the path because otherwise we might attempt to double | |
6001 | * lock the same leaf with btrfs_log_prealloc_extents() below. | |
6002 | */ | |
6003 | btrfs_release_path(path); | |
e383e158 | 6004 | ret = btrfs_log_prealloc_extents(trans, inode, dst_path, ctx); |
d9947887 | 6005 | } |
da447009 FM |
6006 | |
6007 | return ret; | |
6008 | } | |
6009 | ||
30b80f3c FM |
6010 | static int insert_delayed_items_batch(struct btrfs_trans_handle *trans, |
6011 | struct btrfs_root *log, | |
6012 | struct btrfs_path *path, | |
6013 | const struct btrfs_item_batch *batch, | |
6014 | const struct btrfs_delayed_item *first_item) | |
6015 | { | |
6016 | const struct btrfs_delayed_item *curr = first_item; | |
6017 | int ret; | |
6018 | ||
6019 | ret = btrfs_insert_empty_items(trans, log, path, batch); | |
6020 | if (ret) | |
6021 | return ret; | |
6022 | ||
6023 | for (int i = 0; i < batch->nr; i++) { | |
6024 | char *data_ptr; | |
6025 | ||
6026 | data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char); | |
6027 | write_extent_buffer(path->nodes[0], &curr->data, | |
6028 | (unsigned long)data_ptr, curr->data_len); | |
6029 | curr = list_next_entry(curr, log_list); | |
6030 | path->slots[0]++; | |
6031 | } | |
6032 | ||
6033 | btrfs_release_path(path); | |
6034 | ||
6035 | return 0; | |
6036 | } | |
6037 | ||
6038 | static int log_delayed_insertion_items(struct btrfs_trans_handle *trans, | |
6039 | struct btrfs_inode *inode, | |
6040 | struct btrfs_path *path, | |
6041 | const struct list_head *delayed_ins_list, | |
6042 | struct btrfs_log_ctx *ctx) | |
6043 | { | |
6044 | /* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */ | |
6045 | const int max_batch_size = 195; | |
6046 | const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info); | |
6047 | const u64 ino = btrfs_ino(inode); | |
6048 | struct btrfs_root *log = inode->root->log_root; | |
6049 | struct btrfs_item_batch batch = { | |
6050 | .nr = 0, | |
6051 | .total_data_size = 0, | |
6052 | }; | |
6053 | const struct btrfs_delayed_item *first = NULL; | |
6054 | const struct btrfs_delayed_item *curr; | |
6055 | char *ins_data; | |
6056 | struct btrfs_key *ins_keys; | |
6057 | u32 *ins_sizes; | |
6058 | u64 curr_batch_size = 0; | |
6059 | int batch_idx = 0; | |
6060 | int ret; | |
6061 | ||
6062 | /* We are adding dir index items to the log tree. */ | |
6063 | lockdep_assert_held(&inode->log_mutex); | |
6064 | ||
6065 | /* | |
6066 | * We collect delayed items before copying index keys from the subvolume | |
6067 | * to the log tree. However just after we collected them, they may have | |
6068 | * been flushed (all of them or just some of them), and therefore we | |
6069 | * could have copied them from the subvolume tree to the log tree. | |
6070 | * So find the first delayed item that was not yet logged (they are | |
6071 | * sorted by index number). | |
6072 | */ | |
6073 | list_for_each_entry(curr, delayed_ins_list, log_list) { | |
6074 | if (curr->index > inode->last_dir_index_offset) { | |
6075 | first = curr; | |
6076 | break; | |
6077 | } | |
6078 | } | |
6079 | ||
6080 | /* Empty list or all delayed items were already logged. */ | |
6081 | if (!first) | |
6082 | return 0; | |
6083 | ||
6084 | ins_data = kmalloc(max_batch_size * sizeof(u32) + | |
6085 | max_batch_size * sizeof(struct btrfs_key), GFP_NOFS); | |
6086 | if (!ins_data) | |
6087 | return -ENOMEM; | |
6088 | ins_sizes = (u32 *)ins_data; | |
6089 | batch.data_sizes = ins_sizes; | |
6090 | ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32)); | |
6091 | batch.keys = ins_keys; | |
6092 | ||
6093 | curr = first; | |
6094 | while (!list_entry_is_head(curr, delayed_ins_list, log_list)) { | |
6095 | const u32 curr_size = curr->data_len + sizeof(struct btrfs_item); | |
6096 | ||
6097 | if (curr_batch_size + curr_size > leaf_data_size || | |
6098 | batch.nr == max_batch_size) { | |
6099 | ret = insert_delayed_items_batch(trans, log, path, | |
6100 | &batch, first); | |
6101 | if (ret) | |
6102 | goto out; | |
6103 | batch_idx = 0; | |
6104 | batch.nr = 0; | |
6105 | batch.total_data_size = 0; | |
6106 | curr_batch_size = 0; | |
6107 | first = curr; | |
6108 | } | |
6109 | ||
6110 | ins_sizes[batch_idx] = curr->data_len; | |
6111 | ins_keys[batch_idx].objectid = ino; | |
6112 | ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY; | |
6113 | ins_keys[batch_idx].offset = curr->index; | |
6114 | curr_batch_size += curr_size; | |
6115 | batch.total_data_size += curr->data_len; | |
6116 | batch.nr++; | |
6117 | batch_idx++; | |
6118 | curr = list_next_entry(curr, log_list); | |
6119 | } | |
6120 | ||
6121 | ASSERT(batch.nr >= 1); | |
6122 | ret = insert_delayed_items_batch(trans, log, path, &batch, first); | |
6123 | ||
6124 | curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item, | |
6125 | log_list); | |
6126 | inode->last_dir_index_offset = curr->index; | |
6127 | out: | |
6128 | kfree(ins_data); | |
6129 | ||
6130 | return ret; | |
6131 | } | |
6132 | ||
6133 | static int log_delayed_deletions_full(struct btrfs_trans_handle *trans, | |
6134 | struct btrfs_inode *inode, | |
6135 | struct btrfs_path *path, | |
6136 | const struct list_head *delayed_del_list, | |
6137 | struct btrfs_log_ctx *ctx) | |
6138 | { | |
6139 | const u64 ino = btrfs_ino(inode); | |
6140 | const struct btrfs_delayed_item *curr; | |
6141 | ||
6142 | curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, | |
6143 | log_list); | |
6144 | ||
6145 | while (!list_entry_is_head(curr, delayed_del_list, log_list)) { | |
6146 | u64 first_dir_index = curr->index; | |
6147 | u64 last_dir_index; | |
6148 | const struct btrfs_delayed_item *next; | |
6149 | int ret; | |
6150 | ||
6151 | /* | |
6152 | * Find a range of consecutive dir index items to delete. Like | |
6153 | * this we log a single dir range item spanning several contiguous | |
6154 | * dir items instead of logging one range item per dir index item. | |
6155 | */ | |
6156 | next = list_next_entry(curr, log_list); | |
6157 | while (!list_entry_is_head(next, delayed_del_list, log_list)) { | |
6158 | if (next->index != curr->index + 1) | |
6159 | break; | |
6160 | curr = next; | |
6161 | next = list_next_entry(next, log_list); | |
6162 | } | |
6163 | ||
6164 | last_dir_index = curr->index; | |
6165 | ASSERT(last_dir_index >= first_dir_index); | |
6166 | ||
6167 | ret = insert_dir_log_key(trans, inode->root->log_root, path, | |
6168 | ino, first_dir_index, last_dir_index); | |
6169 | if (ret) | |
6170 | return ret; | |
6171 | curr = list_next_entry(curr, log_list); | |
6172 | } | |
6173 | ||
6174 | return 0; | |
6175 | } | |
6176 | ||
6177 | static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans, | |
6178 | struct btrfs_inode *inode, | |
6179 | struct btrfs_path *path, | |
6180 | struct btrfs_log_ctx *ctx, | |
6181 | const struct list_head *delayed_del_list, | |
6182 | const struct btrfs_delayed_item *first, | |
6183 | const struct btrfs_delayed_item **last_ret) | |
6184 | { | |
6185 | const struct btrfs_delayed_item *next; | |
6186 | struct extent_buffer *leaf = path->nodes[0]; | |
6187 | const int last_slot = btrfs_header_nritems(leaf) - 1; | |
6188 | int slot = path->slots[0] + 1; | |
6189 | const u64 ino = btrfs_ino(inode); | |
6190 | ||
6191 | next = list_next_entry(first, log_list); | |
6192 | ||
6193 | while (slot < last_slot && | |
6194 | !list_entry_is_head(next, delayed_del_list, log_list)) { | |
6195 | struct btrfs_key key; | |
6196 | ||
6197 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
6198 | if (key.objectid != ino || | |
6199 | key.type != BTRFS_DIR_INDEX_KEY || | |
6200 | key.offset != next->index) | |
6201 | break; | |
6202 | ||
6203 | slot++; | |
6204 | *last_ret = next; | |
6205 | next = list_next_entry(next, log_list); | |
6206 | } | |
6207 | ||
6208 | return btrfs_del_items(trans, inode->root->log_root, path, | |
6209 | path->slots[0], slot - path->slots[0]); | |
6210 | } | |
6211 | ||
6212 | static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans, | |
6213 | struct btrfs_inode *inode, | |
6214 | struct btrfs_path *path, | |
6215 | const struct list_head *delayed_del_list, | |
6216 | struct btrfs_log_ctx *ctx) | |
6217 | { | |
6218 | struct btrfs_root *log = inode->root->log_root; | |
6219 | const struct btrfs_delayed_item *curr; | |
8fd9f423 | 6220 | u64 last_range_start = 0; |
30b80f3c FM |
6221 | u64 last_range_end = 0; |
6222 | struct btrfs_key key; | |
6223 | ||
6224 | key.objectid = btrfs_ino(inode); | |
6225 | key.type = BTRFS_DIR_INDEX_KEY; | |
6226 | curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, | |
6227 | log_list); | |
6228 | ||
6229 | while (!list_entry_is_head(curr, delayed_del_list, log_list)) { | |
6230 | const struct btrfs_delayed_item *last = curr; | |
6231 | u64 first_dir_index = curr->index; | |
6232 | u64 last_dir_index; | |
6233 | bool deleted_items = false; | |
6234 | int ret; | |
6235 | ||
6236 | key.offset = curr->index; | |
6237 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); | |
6238 | if (ret < 0) { | |
6239 | return ret; | |
6240 | } else if (ret == 0) { | |
6241 | ret = batch_delete_dir_index_items(trans, inode, path, ctx, | |
6242 | delayed_del_list, curr, | |
6243 | &last); | |
6244 | if (ret) | |
6245 | return ret; | |
6246 | deleted_items = true; | |
6247 | } | |
6248 | ||
6249 | btrfs_release_path(path); | |
6250 | ||
6251 | /* | |
6252 | * If we deleted items from the leaf, it means we have a range | |
6253 | * item logging their range, so no need to add one or update an | |
6254 | * existing one. Otherwise we have to log a dir range item. | |
6255 | */ | |
6256 | if (deleted_items) | |
6257 | goto next_batch; | |
6258 | ||
6259 | last_dir_index = last->index; | |
6260 | ASSERT(last_dir_index >= first_dir_index); | |
6261 | /* | |
6262 | * If this range starts right after where the previous one ends, | |
6263 | * then we want to reuse the previous range item and change its | |
6264 | * end offset to the end of this range. This is just to minimize | |
6265 | * leaf space usage, by avoiding adding a new range item. | |
6266 | */ | |
6267 | if (last_range_end != 0 && first_dir_index == last_range_end + 1) | |
6268 | first_dir_index = last_range_start; | |
6269 | ||
6270 | ret = insert_dir_log_key(trans, log, path, key.objectid, | |
6271 | first_dir_index, last_dir_index); | |
6272 | if (ret) | |
6273 | return ret; | |
6274 | ||
6275 | last_range_start = first_dir_index; | |
6276 | last_range_end = last_dir_index; | |
6277 | next_batch: | |
6278 | curr = list_next_entry(last, log_list); | |
6279 | } | |
6280 | ||
6281 | return 0; | |
6282 | } | |
6283 | ||
6284 | static int log_delayed_deletion_items(struct btrfs_trans_handle *trans, | |
6285 | struct btrfs_inode *inode, | |
6286 | struct btrfs_path *path, | |
6287 | const struct list_head *delayed_del_list, | |
6288 | struct btrfs_log_ctx *ctx) | |
6289 | { | |
6290 | /* | |
6291 | * We are deleting dir index items from the log tree or adding range | |
6292 | * items to it. | |
6293 | */ | |
6294 | lockdep_assert_held(&inode->log_mutex); | |
6295 | ||
6296 | if (list_empty(delayed_del_list)) | |
6297 | return 0; | |
6298 | ||
6299 | if (ctx->logged_before) | |
6300 | return log_delayed_deletions_incremental(trans, inode, path, | |
6301 | delayed_del_list, ctx); | |
6302 | ||
6303 | return log_delayed_deletions_full(trans, inode, path, delayed_del_list, | |
6304 | ctx); | |
6305 | } | |
6306 | ||
6307 | /* | |
6308 | * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed | |
6309 | * items instead of the subvolume tree. | |
6310 | */ | |
6311 | static int log_new_delayed_dentries(struct btrfs_trans_handle *trans, | |
6312 | struct btrfs_inode *inode, | |
6313 | const struct list_head *delayed_ins_list, | |
6314 | struct btrfs_log_ctx *ctx) | |
6315 | { | |
6316 | const bool orig_log_new_dentries = ctx->log_new_dentries; | |
6317 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
6318 | struct btrfs_delayed_item *item; | |
6319 | int ret = 0; | |
6320 | ||
6321 | /* | |
6322 | * No need for the log mutex, plus to avoid potential deadlocks or | |
6323 | * lockdep annotations due to nesting of delayed inode mutexes and log | |
6324 | * mutexes. | |
6325 | */ | |
6326 | lockdep_assert_not_held(&inode->log_mutex); | |
6327 | ||
6328 | ASSERT(!ctx->logging_new_delayed_dentries); | |
6329 | ctx->logging_new_delayed_dentries = true; | |
6330 | ||
6331 | list_for_each_entry(item, delayed_ins_list, log_list) { | |
6332 | struct btrfs_dir_item *dir_item; | |
6333 | struct inode *di_inode; | |
6334 | struct btrfs_key key; | |
6335 | int log_mode = LOG_INODE_EXISTS; | |
6336 | ||
6337 | dir_item = (struct btrfs_dir_item *)item->data; | |
6338 | btrfs_disk_key_to_cpu(&key, &dir_item->location); | |
6339 | ||
6340 | if (key.type == BTRFS_ROOT_ITEM_KEY) | |
6341 | continue; | |
6342 | ||
6343 | di_inode = btrfs_iget(fs_info->sb, key.objectid, inode->root); | |
6344 | if (IS_ERR(di_inode)) { | |
6345 | ret = PTR_ERR(di_inode); | |
6346 | break; | |
6347 | } | |
6348 | ||
6349 | if (!need_log_inode(trans, BTRFS_I(di_inode))) { | |
e55cf7ca | 6350 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
30b80f3c FM |
6351 | continue; |
6352 | } | |
6353 | ||
94a48aef | 6354 | if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR) |
30b80f3c FM |
6355 | log_mode = LOG_INODE_ALL; |
6356 | ||
6357 | ctx->log_new_dentries = false; | |
6358 | ret = btrfs_log_inode(trans, BTRFS_I(di_inode), log_mode, ctx); | |
6359 | ||
6360 | if (!ret && ctx->log_new_dentries) | |
6361 | ret = log_new_dir_dentries(trans, BTRFS_I(di_inode), ctx); | |
6362 | ||
e55cf7ca | 6363 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
30b80f3c FM |
6364 | |
6365 | if (ret) | |
6366 | break; | |
6367 | } | |
6368 | ||
6369 | ctx->log_new_dentries = orig_log_new_dentries; | |
6370 | ctx->logging_new_delayed_dentries = false; | |
6371 | ||
6372 | return ret; | |
6373 | } | |
6374 | ||
e02119d5 CM |
6375 | /* log a single inode in the tree log. |
6376 | * At least one parent directory for this inode must exist in the tree | |
6377 | * or be logged already. | |
6378 | * | |
6379 | * Any items from this inode changed by the current transaction are copied | |
6380 | * to the log tree. An extra reference is taken on any extents in this | |
6381 | * file, allowing us to avoid a whole pile of corner cases around logging | |
6382 | * blocks that have been removed from the tree. | |
6383 | * | |
6384 | * See LOG_INODE_ALL and related defines for a description of what inode_only | |
6385 | * does. | |
6386 | * | |
6387 | * This handles both files and directories. | |
6388 | */ | |
12fcfd22 | 6389 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
90d04510 | 6390 | struct btrfs_inode *inode, |
49dae1bc | 6391 | int inode_only, |
8407f553 | 6392 | struct btrfs_log_ctx *ctx) |
e02119d5 CM |
6393 | { |
6394 | struct btrfs_path *path; | |
6395 | struct btrfs_path *dst_path; | |
6396 | struct btrfs_key min_key; | |
6397 | struct btrfs_key max_key; | |
90d04510 | 6398 | struct btrfs_root *log = inode->root->log_root; |
65faced5 | 6399 | int ret; |
5dc562c5 | 6400 | bool fast_search = false; |
a59108a7 NB |
6401 | u64 ino = btrfs_ino(inode); |
6402 | struct extent_map_tree *em_tree = &inode->extent_tree; | |
1a4bcf47 | 6403 | u64 logged_isize = 0; |
e4545de5 | 6404 | bool need_log_inode_item = true; |
9a8fca62 | 6405 | bool xattrs_logged = false; |
2ac691d8 | 6406 | bool inode_item_dropped = true; |
30b80f3c FM |
6407 | bool full_dir_logging = false; |
6408 | LIST_HEAD(delayed_ins_list); | |
6409 | LIST_HEAD(delayed_del_list); | |
e02119d5 | 6410 | |
e02119d5 | 6411 | path = btrfs_alloc_path(); |
5df67083 TI |
6412 | if (!path) |
6413 | return -ENOMEM; | |
e02119d5 | 6414 | dst_path = btrfs_alloc_path(); |
5df67083 TI |
6415 | if (!dst_path) { |
6416 | btrfs_free_path(path); | |
6417 | return -ENOMEM; | |
6418 | } | |
e02119d5 | 6419 | |
33345d01 | 6420 | min_key.objectid = ino; |
e02119d5 CM |
6421 | min_key.type = BTRFS_INODE_ITEM_KEY; |
6422 | min_key.offset = 0; | |
6423 | ||
33345d01 | 6424 | max_key.objectid = ino; |
12fcfd22 | 6425 | |
12fcfd22 | 6426 | |
5dc562c5 | 6427 | /* today the code can only do partial logging of directories */ |
a59108a7 | 6428 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
5269b67e | 6429 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
a59108a7 | 6430 | &inode->runtime_flags) && |
781feef7 | 6431 | inode_only >= LOG_INODE_EXISTS)) |
e02119d5 CM |
6432 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
6433 | else | |
6434 | max_key.type = (u8)-1; | |
6435 | max_key.offset = (u64)-1; | |
6436 | ||
30b80f3c FM |
6437 | if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL) |
6438 | full_dir_logging = true; | |
6439 | ||
2c2c452b | 6440 | /* |
30b80f3c FM |
6441 | * If we are logging a directory while we are logging dentries of the |
6442 | * delayed items of some other inode, then we need to flush the delayed | |
6443 | * items of this directory and not log the delayed items directly. This | |
6444 | * is to prevent more than one level of recursion into btrfs_log_inode() | |
6445 | * by having something like this: | |
6446 | * | |
6447 | * $ mkdir -p a/b/c/d/e/f/g/h/... | |
6448 | * $ xfs_io -c "fsync" a | |
6449 | * | |
6450 | * Where all directories in the path did not exist before and are | |
6451 | * created in the current transaction. | |
6452 | * So in such a case we directly log the delayed items of the main | |
6453 | * directory ("a") without flushing them first, while for each of its | |
6454 | * subdirectories we flush their delayed items before logging them. | |
6455 | * This prevents a potential unbounded recursion like this: | |
6456 | * | |
6457 | * btrfs_log_inode() | |
6458 | * log_new_delayed_dentries() | |
6459 | * btrfs_log_inode() | |
6460 | * log_new_delayed_dentries() | |
6461 | * btrfs_log_inode() | |
6462 | * log_new_delayed_dentries() | |
6463 | * (...) | |
6464 | * | |
6465 | * We have thresholds for the maximum number of delayed items to have in | |
6466 | * memory, and once they are hit, the items are flushed asynchronously. | |
6467 | * However the limit is quite high, so lets prevent deep levels of | |
6468 | * recursion to happen by limiting the maximum depth to be 1. | |
2c2c452b | 6469 | */ |
30b80f3c | 6470 | if (full_dir_logging && ctx->logging_new_delayed_dentries) { |
65faced5 FM |
6471 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
6472 | if (ret) | |
f6df27dd | 6473 | goto out; |
16cdcec7 MX |
6474 | } |
6475 | ||
e09d94c9 | 6476 | mutex_lock(&inode->log_mutex); |
e02119d5 | 6477 | |
d0e64a98 FM |
6478 | /* |
6479 | * For symlinks, we must always log their content, which is stored in an | |
6480 | * inline extent, otherwise we could end up with an empty symlink after | |
6481 | * log replay, which is invalid on linux (symlink(2) returns -ENOENT if | |
6482 | * one attempts to create an empty symlink). | |
6483 | * We don't need to worry about flushing delalloc, because when we create | |
6484 | * the inline extent when the symlink is created (we never have delalloc | |
6485 | * for symlinks). | |
6486 | */ | |
6487 | if (S_ISLNK(inode->vfs_inode.i_mode)) | |
6488 | inode_only = LOG_INODE_ALL; | |
6489 | ||
0f8ce498 FM |
6490 | /* |
6491 | * Before logging the inode item, cache the value returned by | |
6492 | * inode_logged(), because after that we have the need to figure out if | |
6493 | * the inode was previously logged in this transaction. | |
6494 | */ | |
6495 | ret = inode_logged(trans, inode, path); | |
65faced5 | 6496 | if (ret < 0) |
0f8ce498 | 6497 | goto out_unlock; |
0f8ce498 | 6498 | ctx->logged_before = (ret == 1); |
65faced5 | 6499 | ret = 0; |
0f8ce498 | 6500 | |
64d6b281 FM |
6501 | /* |
6502 | * This is for cases where logging a directory could result in losing a | |
6503 | * a file after replaying the log. For example, if we move a file from a | |
6504 | * directory A to a directory B, then fsync directory A, we have no way | |
6505 | * to known the file was moved from A to B, so logging just A would | |
6506 | * result in losing the file after a log replay. | |
6507 | */ | |
30b80f3c | 6508 | if (full_dir_logging && inode->last_unlink_trans >= trans->transid) { |
f31f09f6 | 6509 | ret = BTRFS_LOG_FORCE_COMMIT; |
64d6b281 FM |
6510 | goto out_unlock; |
6511 | } | |
6512 | ||
e02119d5 CM |
6513 | /* |
6514 | * a brute force approach to making sure we get the most uptodate | |
6515 | * copies of everything. | |
6516 | */ | |
a59108a7 | 6517 | if (S_ISDIR(inode->vfs_inode.i_mode)) { |
ab12313a | 6518 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags); |
0f8ce498 FM |
6519 | if (ctx->logged_before) |
6520 | ret = drop_inode_items(trans, log, path, inode, | |
04fc7d51 | 6521 | BTRFS_XATTR_ITEM_KEY); |
e02119d5 | 6522 | } else { |
0f8ce498 | 6523 | if (inode_only == LOG_INODE_EXISTS && ctx->logged_before) { |
1a4bcf47 FM |
6524 | /* |
6525 | * Make sure the new inode item we write to the log has | |
6526 | * the same isize as the current one (if it exists). | |
6527 | * This is necessary to prevent data loss after log | |
6528 | * replay, and also to prevent doing a wrong expanding | |
6529 | * truncate - for e.g. create file, write 4K into offset | |
6530 | * 0, fsync, write 4K into offset 4096, add hard link, | |
6531 | * fsync some other file (to sync log), power fail - if | |
6532 | * we use the inode's current i_size, after log replay | |
6533 | * we get a 8Kb file, with the last 4Kb extent as a hole | |
6534 | * (zeroes), as if an expanding truncate happened, | |
6535 | * instead of getting a file of 4Kb only. | |
6536 | */ | |
65faced5 FM |
6537 | ret = logged_inode_size(log, inode, path, &logged_isize); |
6538 | if (ret) | |
1a4bcf47 FM |
6539 | goto out_unlock; |
6540 | } | |
a742994a | 6541 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
a59108a7 | 6542 | &inode->runtime_flags)) { |
a742994a | 6543 | if (inode_only == LOG_INODE_EXISTS) { |
4f764e51 | 6544 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
0f8ce498 FM |
6545 | if (ctx->logged_before) |
6546 | ret = drop_inode_items(trans, log, path, | |
6547 | inode, max_key.type); | |
a742994a FM |
6548 | } else { |
6549 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
a59108a7 | 6550 | &inode->runtime_flags); |
a742994a | 6551 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
a59108a7 | 6552 | &inode->runtime_flags); |
0f8ce498 | 6553 | if (ctx->logged_before) |
4934a815 FM |
6554 | ret = truncate_inode_items(trans, log, |
6555 | inode, 0, 0); | |
a742994a | 6556 | } |
4f764e51 | 6557 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
a59108a7 | 6558 | &inode->runtime_flags) || |
6cfab851 | 6559 | inode_only == LOG_INODE_EXISTS) { |
4f764e51 | 6560 | if (inode_only == LOG_INODE_ALL) |
183f37fa | 6561 | fast_search = true; |
4f764e51 | 6562 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
0f8ce498 FM |
6563 | if (ctx->logged_before) |
6564 | ret = drop_inode_items(trans, log, path, inode, | |
6565 | max_key.type); | |
a95249b3 JB |
6566 | } else { |
6567 | if (inode_only == LOG_INODE_ALL) | |
6568 | fast_search = true; | |
2ac691d8 | 6569 | inode_item_dropped = false; |
a95249b3 | 6570 | goto log_extents; |
5dc562c5 | 6571 | } |
a95249b3 | 6572 | |
e02119d5 | 6573 | } |
65faced5 | 6574 | if (ret) |
4a500fd1 | 6575 | goto out_unlock; |
e02119d5 | 6576 | |
30b80f3c FM |
6577 | /* |
6578 | * If we are logging a directory in full mode, collect the delayed items | |
6579 | * before iterating the subvolume tree, so that we don't miss any new | |
6580 | * dir index items in case they get flushed while or right after we are | |
6581 | * iterating the subvolume tree. | |
6582 | */ | |
6583 | if (full_dir_logging && !ctx->logging_new_delayed_dentries) | |
6584 | btrfs_log_get_delayed_items(inode, &delayed_ins_list, | |
6585 | &delayed_del_list); | |
6586 | ||
65faced5 | 6587 | ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key, |
da447009 | 6588 | path, dst_path, logged_isize, |
e09d94c9 | 6589 | inode_only, ctx, |
7af59743 | 6590 | &need_log_inode_item); |
65faced5 | 6591 | if (ret) |
da447009 | 6592 | goto out_unlock; |
5dc562c5 | 6593 | |
36283bf7 FM |
6594 | btrfs_release_path(path); |
6595 | btrfs_release_path(dst_path); | |
e383e158 | 6596 | ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx); |
65faced5 | 6597 | if (ret) |
36283bf7 | 6598 | goto out_unlock; |
9a8fca62 | 6599 | xattrs_logged = true; |
a89ca6f2 FM |
6600 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { |
6601 | btrfs_release_path(path); | |
6602 | btrfs_release_path(dst_path); | |
65faced5 FM |
6603 | ret = btrfs_log_holes(trans, inode, path); |
6604 | if (ret) | |
a89ca6f2 FM |
6605 | goto out_unlock; |
6606 | } | |
a95249b3 | 6607 | log_extents: |
f3b15ccd JB |
6608 | btrfs_release_path(path); |
6609 | btrfs_release_path(dst_path); | |
e4545de5 | 6610 | if (need_log_inode_item) { |
65faced5 FM |
6611 | ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped); |
6612 | if (ret) | |
b590b839 FM |
6613 | goto out_unlock; |
6614 | /* | |
6615 | * If we are doing a fast fsync and the inode was logged before | |
6616 | * in this transaction, we don't need to log the xattrs because | |
6617 | * they were logged before. If xattrs were added, changed or | |
6618 | * deleted since the last time we logged the inode, then we have | |
6619 | * already logged them because the inode had the runtime flag | |
6620 | * BTRFS_INODE_COPY_EVERYTHING set. | |
6621 | */ | |
6622 | if (!xattrs_logged && inode->logged_trans < trans->transid) { | |
e383e158 | 6623 | ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx); |
65faced5 | 6624 | if (ret) |
b590b839 | 6625 | goto out_unlock; |
9a8fca62 FM |
6626 | btrfs_release_path(path); |
6627 | } | |
e4545de5 | 6628 | } |
5dc562c5 | 6629 | if (fast_search) { |
90d04510 | 6630 | ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx); |
65faced5 | 6631 | if (ret) |
5dc562c5 | 6632 | goto out_unlock; |
d006a048 | 6633 | } else if (inode_only == LOG_INODE_ALL) { |
06d3d22b LB |
6634 | struct extent_map *em, *n; |
6635 | ||
49dae1bc | 6636 | write_lock(&em_tree->lock); |
48778179 FM |
6637 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, list) |
6638 | list_del_init(&em->list); | |
49dae1bc | 6639 | write_unlock(&em_tree->lock); |
5dc562c5 JB |
6640 | } |
6641 | ||
30b80f3c | 6642 | if (full_dir_logging) { |
90d04510 | 6643 | ret = log_directory_changes(trans, inode, path, dst_path, ctx); |
65faced5 | 6644 | if (ret) |
4a500fd1 | 6645 | goto out_unlock; |
30b80f3c FM |
6646 | ret = log_delayed_insertion_items(trans, inode, path, |
6647 | &delayed_ins_list, ctx); | |
6648 | if (ret) | |
6649 | goto out_unlock; | |
6650 | ret = log_delayed_deletion_items(trans, inode, path, | |
6651 | &delayed_del_list, ctx); | |
6652 | if (ret) | |
6653 | goto out_unlock; | |
e02119d5 | 6654 | } |
49dae1bc | 6655 | |
130341be FM |
6656 | spin_lock(&inode->lock); |
6657 | inode->logged_trans = trans->transid; | |
d1d832a0 | 6658 | /* |
130341be FM |
6659 | * Don't update last_log_commit if we logged that an inode exists. |
6660 | * We do this for three reasons: | |
6661 | * | |
6662 | * 1) We might have had buffered writes to this inode that were | |
6663 | * flushed and had their ordered extents completed in this | |
6664 | * transaction, but we did not previously log the inode with | |
6665 | * LOG_INODE_ALL. Later the inode was evicted and after that | |
6666 | * it was loaded again and this LOG_INODE_EXISTS log operation | |
6667 | * happened. We must make sure that if an explicit fsync against | |
6668 | * the inode is performed later, it logs the new extents, an | |
6669 | * updated inode item, etc, and syncs the log. The same logic | |
6670 | * applies to direct IO writes instead of buffered writes. | |
6671 | * | |
6672 | * 2) When we log the inode with LOG_INODE_EXISTS, its inode item | |
6673 | * is logged with an i_size of 0 or whatever value was logged | |
6674 | * before. If later the i_size of the inode is increased by a | |
6675 | * truncate operation, the log is synced through an fsync of | |
6676 | * some other inode and then finally an explicit fsync against | |
6677 | * this inode is made, we must make sure this fsync logs the | |
6678 | * inode with the new i_size, the hole between old i_size and | |
6679 | * the new i_size, and syncs the log. | |
6680 | * | |
6681 | * 3) If we are logging that an ancestor inode exists as part of | |
6682 | * logging a new name from a link or rename operation, don't update | |
6683 | * its last_log_commit - otherwise if an explicit fsync is made | |
6684 | * against an ancestor, the fsync considers the inode in the log | |
6685 | * and doesn't sync the log, resulting in the ancestor missing after | |
6686 | * a power failure unless the log was synced as part of an fsync | |
6687 | * against any other unrelated inode. | |
d1d832a0 | 6688 | */ |
130341be FM |
6689 | if (inode_only != LOG_INODE_EXISTS) |
6690 | inode->last_log_commit = inode->last_sub_trans; | |
6691 | spin_unlock(&inode->lock); | |
23e3337f FM |
6692 | |
6693 | /* | |
6694 | * Reset the last_reflink_trans so that the next fsync does not need to | |
6695 | * go through the slower path when logging extents and their checksums. | |
6696 | */ | |
6697 | if (inode_only == LOG_INODE_ALL) | |
6698 | inode->last_reflink_trans = 0; | |
6699 | ||
4a500fd1 | 6700 | out_unlock: |
a59108a7 | 6701 | mutex_unlock(&inode->log_mutex); |
f6df27dd | 6702 | out: |
e02119d5 CM |
6703 | btrfs_free_path(path); |
6704 | btrfs_free_path(dst_path); | |
0f8ce498 | 6705 | |
e09d94c9 FM |
6706 | if (ret) |
6707 | free_conflicting_inodes(ctx); | |
6708 | else | |
6709 | ret = log_conflicting_inodes(trans, inode->root, ctx); | |
0f8ce498 | 6710 | |
30b80f3c FM |
6711 | if (full_dir_logging && !ctx->logging_new_delayed_dentries) { |
6712 | if (!ret) | |
6713 | ret = log_new_delayed_dentries(trans, inode, | |
6714 | &delayed_ins_list, ctx); | |
6715 | ||
6716 | btrfs_log_put_delayed_items(inode, &delayed_ins_list, | |
6717 | &delayed_del_list); | |
6718 | } | |
6719 | ||
65faced5 | 6720 | return ret; |
e02119d5 CM |
6721 | } |
6722 | ||
18aa0922 | 6723 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, |
d0a0b78d | 6724 | struct btrfs_inode *inode, |
18aa0922 FM |
6725 | struct btrfs_log_ctx *ctx) |
6726 | { | |
3ffbd68c | 6727 | struct btrfs_fs_info *fs_info = trans->fs_info; |
18aa0922 FM |
6728 | int ret; |
6729 | struct btrfs_path *path; | |
6730 | struct btrfs_key key; | |
d0a0b78d NB |
6731 | struct btrfs_root *root = inode->root; |
6732 | const u64 ino = btrfs_ino(inode); | |
18aa0922 FM |
6733 | |
6734 | path = btrfs_alloc_path(); | |
6735 | if (!path) | |
6736 | return -ENOMEM; | |
6737 | path->skip_locking = 1; | |
6738 | path->search_commit_root = 1; | |
6739 | ||
6740 | key.objectid = ino; | |
6741 | key.type = BTRFS_INODE_REF_KEY; | |
6742 | key.offset = 0; | |
6743 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6744 | if (ret < 0) | |
6745 | goto out; | |
6746 | ||
6747 | while (true) { | |
6748 | struct extent_buffer *leaf = path->nodes[0]; | |
6749 | int slot = path->slots[0]; | |
6750 | u32 cur_offset = 0; | |
6751 | u32 item_size; | |
6752 | unsigned long ptr; | |
6753 | ||
6754 | if (slot >= btrfs_header_nritems(leaf)) { | |
6755 | ret = btrfs_next_leaf(root, path); | |
6756 | if (ret < 0) | |
6757 | goto out; | |
6758 | else if (ret > 0) | |
6759 | break; | |
6760 | continue; | |
6761 | } | |
6762 | ||
6763 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
6764 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ | |
6765 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) | |
6766 | break; | |
6767 | ||
3212fa14 | 6768 | item_size = btrfs_item_size(leaf, slot); |
18aa0922 FM |
6769 | ptr = btrfs_item_ptr_offset(leaf, slot); |
6770 | while (cur_offset < item_size) { | |
6771 | struct btrfs_key inode_key; | |
6772 | struct inode *dir_inode; | |
6773 | ||
6774 | inode_key.type = BTRFS_INODE_ITEM_KEY; | |
6775 | inode_key.offset = 0; | |
6776 | ||
6777 | if (key.type == BTRFS_INODE_EXTREF_KEY) { | |
6778 | struct btrfs_inode_extref *extref; | |
6779 | ||
6780 | extref = (struct btrfs_inode_extref *) | |
6781 | (ptr + cur_offset); | |
6782 | inode_key.objectid = btrfs_inode_extref_parent( | |
6783 | leaf, extref); | |
6784 | cur_offset += sizeof(*extref); | |
6785 | cur_offset += btrfs_inode_extref_name_len(leaf, | |
6786 | extref); | |
6787 | } else { | |
6788 | inode_key.objectid = key.offset; | |
6789 | cur_offset = item_size; | |
6790 | } | |
6791 | ||
0202e83f DS |
6792 | dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid, |
6793 | root); | |
0f375eed FM |
6794 | /* |
6795 | * If the parent inode was deleted, return an error to | |
6796 | * fallback to a transaction commit. This is to prevent | |
6797 | * getting an inode that was moved from one parent A to | |
6798 | * a parent B, got its former parent A deleted and then | |
6799 | * it got fsync'ed, from existing at both parents after | |
6800 | * a log replay (and the old parent still existing). | |
6801 | * Example: | |
6802 | * | |
6803 | * mkdir /mnt/A | |
6804 | * mkdir /mnt/B | |
6805 | * touch /mnt/B/bar | |
6806 | * sync | |
6807 | * mv /mnt/B/bar /mnt/A/bar | |
6808 | * mv -T /mnt/A /mnt/B | |
6809 | * fsync /mnt/B/bar | |
6810 | * <power fail> | |
6811 | * | |
6812 | * If we ignore the old parent B which got deleted, | |
6813 | * after a log replay we would have file bar linked | |
6814 | * at both parents and the old parent B would still | |
6815 | * exist. | |
6816 | */ | |
6817 | if (IS_ERR(dir_inode)) { | |
6818 | ret = PTR_ERR(dir_inode); | |
6819 | goto out; | |
6820 | } | |
18aa0922 | 6821 | |
3e6a86a1 | 6822 | if (!need_log_inode(trans, BTRFS_I(dir_inode))) { |
e55cf7ca | 6823 | btrfs_add_delayed_iput(BTRFS_I(dir_inode)); |
3e6a86a1 FM |
6824 | continue; |
6825 | } | |
6826 | ||
289cffcb | 6827 | ctx->log_new_dentries = false; |
90d04510 | 6828 | ret = btrfs_log_inode(trans, BTRFS_I(dir_inode), |
48778179 | 6829 | LOG_INODE_ALL, ctx); |
289cffcb | 6830 | if (!ret && ctx->log_new_dentries) |
8786a6d7 | 6831 | ret = log_new_dir_dentries(trans, |
f85b7379 | 6832 | BTRFS_I(dir_inode), ctx); |
e55cf7ca | 6833 | btrfs_add_delayed_iput(BTRFS_I(dir_inode)); |
18aa0922 FM |
6834 | if (ret) |
6835 | goto out; | |
6836 | } | |
6837 | path->slots[0]++; | |
6838 | } | |
6839 | ret = 0; | |
6840 | out: | |
6841 | btrfs_free_path(path); | |
6842 | return ret; | |
6843 | } | |
6844 | ||
b8aa330d FM |
6845 | static int log_new_ancestors(struct btrfs_trans_handle *trans, |
6846 | struct btrfs_root *root, | |
6847 | struct btrfs_path *path, | |
6848 | struct btrfs_log_ctx *ctx) | |
6849 | { | |
6850 | struct btrfs_key found_key; | |
6851 | ||
6852 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); | |
6853 | ||
6854 | while (true) { | |
6855 | struct btrfs_fs_info *fs_info = root->fs_info; | |
966de47f CIK |
6856 | struct extent_buffer *leaf; |
6857 | int slot; | |
b8aa330d FM |
6858 | struct btrfs_key search_key; |
6859 | struct inode *inode; | |
0202e83f | 6860 | u64 ino; |
b8aa330d FM |
6861 | int ret = 0; |
6862 | ||
6863 | btrfs_release_path(path); | |
6864 | ||
0202e83f DS |
6865 | ino = found_key.offset; |
6866 | ||
b8aa330d FM |
6867 | search_key.objectid = found_key.offset; |
6868 | search_key.type = BTRFS_INODE_ITEM_KEY; | |
6869 | search_key.offset = 0; | |
0202e83f | 6870 | inode = btrfs_iget(fs_info->sb, ino, root); |
b8aa330d FM |
6871 | if (IS_ERR(inode)) |
6872 | return PTR_ERR(inode); | |
6873 | ||
ab12313a FM |
6874 | if (BTRFS_I(inode)->generation >= trans->transid && |
6875 | need_log_inode(trans, BTRFS_I(inode))) | |
90d04510 | 6876 | ret = btrfs_log_inode(trans, BTRFS_I(inode), |
48778179 | 6877 | LOG_INODE_EXISTS, ctx); |
e55cf7ca | 6878 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
b8aa330d FM |
6879 | if (ret) |
6880 | return ret; | |
6881 | ||
6882 | if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID) | |
6883 | break; | |
6884 | ||
6885 | search_key.type = BTRFS_INODE_REF_KEY; | |
6886 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
6887 | if (ret < 0) | |
6888 | return ret; | |
6889 | ||
6890 | leaf = path->nodes[0]; | |
6891 | slot = path->slots[0]; | |
6892 | if (slot >= btrfs_header_nritems(leaf)) { | |
6893 | ret = btrfs_next_leaf(root, path); | |
6894 | if (ret < 0) | |
6895 | return ret; | |
6896 | else if (ret > 0) | |
6897 | return -ENOENT; | |
6898 | leaf = path->nodes[0]; | |
6899 | slot = path->slots[0]; | |
6900 | } | |
6901 | ||
6902 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
6903 | if (found_key.objectid != search_key.objectid || | |
6904 | found_key.type != BTRFS_INODE_REF_KEY) | |
6905 | return -ENOENT; | |
6906 | } | |
6907 | return 0; | |
6908 | } | |
6909 | ||
6910 | static int log_new_ancestors_fast(struct btrfs_trans_handle *trans, | |
6911 | struct btrfs_inode *inode, | |
6912 | struct dentry *parent, | |
6913 | struct btrfs_log_ctx *ctx) | |
6914 | { | |
6915 | struct btrfs_root *root = inode->root; | |
b8aa330d FM |
6916 | struct dentry *old_parent = NULL; |
6917 | struct super_block *sb = inode->vfs_inode.i_sb; | |
6918 | int ret = 0; | |
6919 | ||
6920 | while (true) { | |
6921 | if (!parent || d_really_is_negative(parent) || | |
6922 | sb != parent->d_sb) | |
6923 | break; | |
6924 | ||
6925 | inode = BTRFS_I(d_inode(parent)); | |
6926 | if (root != inode->root) | |
6927 | break; | |
6928 | ||
ab12313a FM |
6929 | if (inode->generation >= trans->transid && |
6930 | need_log_inode(trans, inode)) { | |
90d04510 | 6931 | ret = btrfs_log_inode(trans, inode, |
48778179 | 6932 | LOG_INODE_EXISTS, ctx); |
b8aa330d FM |
6933 | if (ret) |
6934 | break; | |
6935 | } | |
6936 | if (IS_ROOT(parent)) | |
6937 | break; | |
6938 | ||
6939 | parent = dget_parent(parent); | |
6940 | dput(old_parent); | |
6941 | old_parent = parent; | |
6942 | } | |
6943 | dput(old_parent); | |
6944 | ||
6945 | return ret; | |
6946 | } | |
6947 | ||
6948 | static int log_all_new_ancestors(struct btrfs_trans_handle *trans, | |
6949 | struct btrfs_inode *inode, | |
6950 | struct dentry *parent, | |
6951 | struct btrfs_log_ctx *ctx) | |
6952 | { | |
6953 | struct btrfs_root *root = inode->root; | |
6954 | const u64 ino = btrfs_ino(inode); | |
6955 | struct btrfs_path *path; | |
6956 | struct btrfs_key search_key; | |
6957 | int ret; | |
6958 | ||
6959 | /* | |
6960 | * For a single hard link case, go through a fast path that does not | |
6961 | * need to iterate the fs/subvolume tree. | |
6962 | */ | |
6963 | if (inode->vfs_inode.i_nlink < 2) | |
6964 | return log_new_ancestors_fast(trans, inode, parent, ctx); | |
6965 | ||
6966 | path = btrfs_alloc_path(); | |
6967 | if (!path) | |
6968 | return -ENOMEM; | |
6969 | ||
6970 | search_key.objectid = ino; | |
6971 | search_key.type = BTRFS_INODE_REF_KEY; | |
6972 | search_key.offset = 0; | |
6973 | again: | |
6974 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
6975 | if (ret < 0) | |
6976 | goto out; | |
6977 | if (ret == 0) | |
6978 | path->slots[0]++; | |
6979 | ||
6980 | while (true) { | |
6981 | struct extent_buffer *leaf = path->nodes[0]; | |
6982 | int slot = path->slots[0]; | |
6983 | struct btrfs_key found_key; | |
6984 | ||
6985 | if (slot >= btrfs_header_nritems(leaf)) { | |
6986 | ret = btrfs_next_leaf(root, path); | |
6987 | if (ret < 0) | |
6988 | goto out; | |
6989 | else if (ret > 0) | |
6990 | break; | |
6991 | continue; | |
6992 | } | |
6993 | ||
6994 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
6995 | if (found_key.objectid != ino || | |
6996 | found_key.type > BTRFS_INODE_EXTREF_KEY) | |
6997 | break; | |
6998 | ||
6999 | /* | |
7000 | * Don't deal with extended references because they are rare | |
7001 | * cases and too complex to deal with (we would need to keep | |
7002 | * track of which subitem we are processing for each item in | |
7003 | * this loop, etc). So just return some error to fallback to | |
7004 | * a transaction commit. | |
7005 | */ | |
7006 | if (found_key.type == BTRFS_INODE_EXTREF_KEY) { | |
7007 | ret = -EMLINK; | |
7008 | goto out; | |
7009 | } | |
7010 | ||
7011 | /* | |
7012 | * Logging ancestors needs to do more searches on the fs/subvol | |
7013 | * tree, so it releases the path as needed to avoid deadlocks. | |
7014 | * Keep track of the last inode ref key and resume from that key | |
7015 | * after logging all new ancestors for the current hard link. | |
7016 | */ | |
7017 | memcpy(&search_key, &found_key, sizeof(search_key)); | |
7018 | ||
7019 | ret = log_new_ancestors(trans, root, path, ctx); | |
7020 | if (ret) | |
7021 | goto out; | |
7022 | btrfs_release_path(path); | |
7023 | goto again; | |
7024 | } | |
7025 | ret = 0; | |
7026 | out: | |
7027 | btrfs_free_path(path); | |
7028 | return ret; | |
7029 | } | |
7030 | ||
e02119d5 CM |
7031 | /* |
7032 | * helper function around btrfs_log_inode to make sure newly created | |
7033 | * parent directories also end up in the log. A minimal inode and backref | |
7034 | * only logging is done of any parent directories that are older than | |
7035 | * the last committed transaction | |
7036 | */ | |
48a3b636 | 7037 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
19df27a9 | 7038 | struct btrfs_inode *inode, |
49dae1bc | 7039 | struct dentry *parent, |
41a1eada | 7040 | int inode_only, |
8b050d35 | 7041 | struct btrfs_log_ctx *ctx) |
e02119d5 | 7042 | { |
f882274b | 7043 | struct btrfs_root *root = inode->root; |
0b246afa | 7044 | struct btrfs_fs_info *fs_info = root->fs_info; |
12fcfd22 | 7045 | int ret = 0; |
2f2ff0ee | 7046 | bool log_dentries = false; |
12fcfd22 | 7047 | |
0b246afa | 7048 | if (btrfs_test_opt(fs_info, NOTREELOG)) { |
f31f09f6 | 7049 | ret = BTRFS_LOG_FORCE_COMMIT; |
3a5e1404 SW |
7050 | goto end_no_trans; |
7051 | } | |
7052 | ||
f882274b | 7053 | if (btrfs_root_refs(&root->root_item) == 0) { |
f31f09f6 | 7054 | ret = BTRFS_LOG_FORCE_COMMIT; |
76dda93c YZ |
7055 | goto end_no_trans; |
7056 | } | |
7057 | ||
f2d72f42 FM |
7058 | /* |
7059 | * Skip already logged inodes or inodes corresponding to tmpfiles | |
7060 | * (since logging them is pointless, a link count of 0 means they | |
7061 | * will never be accessible). | |
7062 | */ | |
626e9f41 FM |
7063 | if ((btrfs_inode_in_log(inode, trans->transid) && |
7064 | list_empty(&ctx->ordered_extents)) || | |
f2d72f42 | 7065 | inode->vfs_inode.i_nlink == 0) { |
257c62e1 CM |
7066 | ret = BTRFS_NO_LOG_SYNC; |
7067 | goto end_no_trans; | |
7068 | } | |
7069 | ||
8b050d35 | 7070 | ret = start_log_trans(trans, root, ctx); |
4a500fd1 | 7071 | if (ret) |
e87ac136 | 7072 | goto end_no_trans; |
e02119d5 | 7073 | |
90d04510 | 7074 | ret = btrfs_log_inode(trans, inode, inode_only, ctx); |
4a500fd1 YZ |
7075 | if (ret) |
7076 | goto end_trans; | |
12fcfd22 | 7077 | |
af4176b4 CM |
7078 | /* |
7079 | * for regular files, if its inode is already on disk, we don't | |
7080 | * have to worry about the parents at all. This is because | |
7081 | * we can use the last_unlink_trans field to record renames | |
7082 | * and other fun in this file. | |
7083 | */ | |
19df27a9 | 7084 | if (S_ISREG(inode->vfs_inode.i_mode) && |
47d3db41 FM |
7085 | inode->generation < trans->transid && |
7086 | inode->last_unlink_trans < trans->transid) { | |
4a500fd1 YZ |
7087 | ret = 0; |
7088 | goto end_trans; | |
7089 | } | |
af4176b4 | 7090 | |
289cffcb | 7091 | if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries) |
2f2ff0ee FM |
7092 | log_dentries = true; |
7093 | ||
18aa0922 | 7094 | /* |
01327610 | 7095 | * On unlink we must make sure all our current and old parent directory |
18aa0922 FM |
7096 | * inodes are fully logged. This is to prevent leaving dangling |
7097 | * directory index entries in directories that were our parents but are | |
7098 | * not anymore. Not doing this results in old parent directory being | |
7099 | * impossible to delete after log replay (rmdir will always fail with | |
7100 | * error -ENOTEMPTY). | |
7101 | * | |
7102 | * Example 1: | |
7103 | * | |
7104 | * mkdir testdir | |
7105 | * touch testdir/foo | |
7106 | * ln testdir/foo testdir/bar | |
7107 | * sync | |
7108 | * unlink testdir/bar | |
7109 | * xfs_io -c fsync testdir/foo | |
7110 | * <power failure> | |
7111 | * mount fs, triggers log replay | |
7112 | * | |
7113 | * If we don't log the parent directory (testdir), after log replay the | |
7114 | * directory still has an entry pointing to the file inode using the bar | |
7115 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and | |
7116 | * the file inode has a link count of 1. | |
7117 | * | |
7118 | * Example 2: | |
7119 | * | |
7120 | * mkdir testdir | |
7121 | * touch foo | |
7122 | * ln foo testdir/foo2 | |
7123 | * ln foo testdir/foo3 | |
7124 | * sync | |
7125 | * unlink testdir/foo3 | |
7126 | * xfs_io -c fsync foo | |
7127 | * <power failure> | |
7128 | * mount fs, triggers log replay | |
7129 | * | |
7130 | * Similar as the first example, after log replay the parent directory | |
7131 | * testdir still has an entry pointing to the inode file with name foo3 | |
7132 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item | |
7133 | * and has a link count of 2. | |
7134 | */ | |
47d3db41 | 7135 | if (inode->last_unlink_trans >= trans->transid) { |
b8aa330d | 7136 | ret = btrfs_log_all_parents(trans, inode, ctx); |
18aa0922 FM |
7137 | if (ret) |
7138 | goto end_trans; | |
7139 | } | |
7140 | ||
b8aa330d FM |
7141 | ret = log_all_new_ancestors(trans, inode, parent, ctx); |
7142 | if (ret) | |
41bd6067 | 7143 | goto end_trans; |
76dda93c | 7144 | |
2f2ff0ee | 7145 | if (log_dentries) |
8786a6d7 | 7146 | ret = log_new_dir_dentries(trans, inode, ctx); |
2f2ff0ee FM |
7147 | else |
7148 | ret = 0; | |
4a500fd1 YZ |
7149 | end_trans: |
7150 | if (ret < 0) { | |
90787766 | 7151 | btrfs_set_log_full_commit(trans); |
f31f09f6 | 7152 | ret = BTRFS_LOG_FORCE_COMMIT; |
4a500fd1 | 7153 | } |
8b050d35 MX |
7154 | |
7155 | if (ret) | |
7156 | btrfs_remove_log_ctx(root, ctx); | |
12fcfd22 CM |
7157 | btrfs_end_log_trans(root); |
7158 | end_no_trans: | |
7159 | return ret; | |
e02119d5 CM |
7160 | } |
7161 | ||
7162 | /* | |
7163 | * it is not safe to log dentry if the chunk root has added new | |
7164 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. | |
7165 | * If this returns 1, you must commit the transaction to safely get your | |
7166 | * data on disk. | |
7167 | */ | |
7168 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | |
e5b84f7a | 7169 | struct dentry *dentry, |
8b050d35 | 7170 | struct btrfs_log_ctx *ctx) |
e02119d5 | 7171 | { |
6a912213 JB |
7172 | struct dentry *parent = dget_parent(dentry); |
7173 | int ret; | |
7174 | ||
f882274b | 7175 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, |
48778179 | 7176 | LOG_INODE_ALL, ctx); |
6a912213 JB |
7177 | dput(parent); |
7178 | ||
7179 | return ret; | |
e02119d5 CM |
7180 | } |
7181 | ||
7182 | /* | |
7183 | * should be called during mount to recover any replay any log trees | |
7184 | * from the FS | |
7185 | */ | |
7186 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) | |
7187 | { | |
7188 | int ret; | |
7189 | struct btrfs_path *path; | |
7190 | struct btrfs_trans_handle *trans; | |
7191 | struct btrfs_key key; | |
7192 | struct btrfs_key found_key; | |
e02119d5 CM |
7193 | struct btrfs_root *log; |
7194 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; | |
7195 | struct walk_control wc = { | |
7196 | .process_func = process_one_buffer, | |
430a6626 | 7197 | .stage = LOG_WALK_PIN_ONLY, |
e02119d5 CM |
7198 | }; |
7199 | ||
e02119d5 | 7200 | path = btrfs_alloc_path(); |
db5b493a TI |
7201 | if (!path) |
7202 | return -ENOMEM; | |
7203 | ||
afcdd129 | 7204 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
e02119d5 | 7205 | |
4a500fd1 | 7206 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
79787eaa JM |
7207 | if (IS_ERR(trans)) { |
7208 | ret = PTR_ERR(trans); | |
7209 | goto error; | |
7210 | } | |
e02119d5 CM |
7211 | |
7212 | wc.trans = trans; | |
7213 | wc.pin = 1; | |
7214 | ||
db5b493a | 7215 | ret = walk_log_tree(trans, log_root_tree, &wc); |
79787eaa | 7216 | if (ret) { |
ba51e2a1 | 7217 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7218 | goto error; |
7219 | } | |
e02119d5 CM |
7220 | |
7221 | again: | |
7222 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | |
7223 | key.offset = (u64)-1; | |
962a298f | 7224 | key.type = BTRFS_ROOT_ITEM_KEY; |
e02119d5 | 7225 | |
d397712b | 7226 | while (1) { |
e02119d5 | 7227 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
79787eaa JM |
7228 | |
7229 | if (ret < 0) { | |
ba51e2a1 | 7230 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7231 | goto error; |
7232 | } | |
e02119d5 CM |
7233 | if (ret > 0) { |
7234 | if (path->slots[0] == 0) | |
7235 | break; | |
7236 | path->slots[0]--; | |
7237 | } | |
7238 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
7239 | path->slots[0]); | |
b3b4aa74 | 7240 | btrfs_release_path(path); |
e02119d5 CM |
7241 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7242 | break; | |
7243 | ||
62a2c73e | 7244 | log = btrfs_read_tree_root(log_root_tree, &found_key); |
79787eaa JM |
7245 | if (IS_ERR(log)) { |
7246 | ret = PTR_ERR(log); | |
ba51e2a1 | 7247 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7248 | goto error; |
7249 | } | |
e02119d5 | 7250 | |
56e9357a DS |
7251 | wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset, |
7252 | true); | |
79787eaa JM |
7253 | if (IS_ERR(wc.replay_dest)) { |
7254 | ret = PTR_ERR(wc.replay_dest); | |
9bc574de JB |
7255 | |
7256 | /* | |
7257 | * We didn't find the subvol, likely because it was | |
7258 | * deleted. This is ok, simply skip this log and go to | |
7259 | * the next one. | |
7260 | * | |
7261 | * We need to exclude the root because we can't have | |
7262 | * other log replays overwriting this log as we'll read | |
7263 | * it back in a few more times. This will keep our | |
7264 | * block from being modified, and we'll just bail for | |
7265 | * each subsequent pass. | |
7266 | */ | |
7267 | if (ret == -ENOENT) | |
007dec8c | 7268 | ret = btrfs_pin_extent_for_log_replay(trans, log->node); |
00246528 | 7269 | btrfs_put_root(log); |
9bc574de JB |
7270 | |
7271 | if (!ret) | |
7272 | goto next; | |
ba51e2a1 | 7273 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
7274 | goto error; |
7275 | } | |
e02119d5 | 7276 | |
07d400a6 | 7277 | wc.replay_dest->log_root = log; |
2002ae11 JB |
7278 | ret = btrfs_record_root_in_trans(trans, wc.replay_dest); |
7279 | if (ret) | |
7280 | /* The loop needs to continue due to the root refs */ | |
ba51e2a1 | 7281 | btrfs_abort_transaction(trans, ret); |
2002ae11 JB |
7282 | else |
7283 | ret = walk_log_tree(trans, log, &wc); | |
e02119d5 | 7284 | |
b50c6e25 | 7285 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
e02119d5 CM |
7286 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
7287 | path); | |
ba51e2a1 JB |
7288 | if (ret) |
7289 | btrfs_abort_transaction(trans, ret); | |
e02119d5 CM |
7290 | } |
7291 | ||
900c9981 LB |
7292 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
7293 | struct btrfs_root *root = wc.replay_dest; | |
7294 | ||
7295 | btrfs_release_path(path); | |
7296 | ||
7297 | /* | |
7298 | * We have just replayed everything, and the highest | |
7299 | * objectid of fs roots probably has changed in case | |
7300 | * some inode_item's got replayed. | |
7301 | * | |
7302 | * root->objectid_mutex is not acquired as log replay | |
7303 | * could only happen during mount. | |
7304 | */ | |
453e4873 | 7305 | ret = btrfs_init_root_free_objectid(root); |
ba51e2a1 JB |
7306 | if (ret) |
7307 | btrfs_abort_transaction(trans, ret); | |
900c9981 LB |
7308 | } |
7309 | ||
07d400a6 | 7310 | wc.replay_dest->log_root = NULL; |
00246528 | 7311 | btrfs_put_root(wc.replay_dest); |
00246528 | 7312 | btrfs_put_root(log); |
e02119d5 | 7313 | |
b50c6e25 JB |
7314 | if (ret) |
7315 | goto error; | |
9bc574de | 7316 | next: |
e02119d5 CM |
7317 | if (found_key.offset == 0) |
7318 | break; | |
9bc574de | 7319 | key.offset = found_key.offset - 1; |
e02119d5 | 7320 | } |
b3b4aa74 | 7321 | btrfs_release_path(path); |
e02119d5 CM |
7322 | |
7323 | /* step one is to pin it all, step two is to replay just inodes */ | |
7324 | if (wc.pin) { | |
7325 | wc.pin = 0; | |
7326 | wc.process_func = replay_one_buffer; | |
7327 | wc.stage = LOG_WALK_REPLAY_INODES; | |
7328 | goto again; | |
7329 | } | |
7330 | /* step three is to replay everything */ | |
7331 | if (wc.stage < LOG_WALK_REPLAY_ALL) { | |
7332 | wc.stage++; | |
7333 | goto again; | |
7334 | } | |
7335 | ||
7336 | btrfs_free_path(path); | |
7337 | ||
abefa55a | 7338 | /* step 4: commit the transaction, which also unpins the blocks */ |
3a45bb20 | 7339 | ret = btrfs_commit_transaction(trans); |
abefa55a JB |
7340 | if (ret) |
7341 | return ret; | |
7342 | ||
e02119d5 | 7343 | log_root_tree->log_root = NULL; |
afcdd129 | 7344 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
00246528 | 7345 | btrfs_put_root(log_root_tree); |
79787eaa | 7346 | |
abefa55a | 7347 | return 0; |
79787eaa | 7348 | error: |
b50c6e25 | 7349 | if (wc.trans) |
3a45bb20 | 7350 | btrfs_end_transaction(wc.trans); |
1aeb6b56 | 7351 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
79787eaa JM |
7352 | btrfs_free_path(path); |
7353 | return ret; | |
e02119d5 | 7354 | } |
12fcfd22 CM |
7355 | |
7356 | /* | |
7357 | * there are some corner cases where we want to force a full | |
7358 | * commit instead of allowing a directory to be logged. | |
7359 | * | |
7360 | * They revolve around files there were unlinked from the directory, and | |
7361 | * this function updates the parent directory so that a full commit is | |
7362 | * properly done if it is fsync'd later after the unlinks are done. | |
2be63d5c FM |
7363 | * |
7364 | * Must be called before the unlink operations (updates to the subvolume tree, | |
7365 | * inodes, etc) are done. | |
12fcfd22 CM |
7366 | */ |
7367 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, | |
4176bdbf | 7368 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
59fcf388 | 7369 | bool for_rename) |
12fcfd22 | 7370 | { |
af4176b4 CM |
7371 | /* |
7372 | * when we're logging a file, if it hasn't been renamed | |
7373 | * or unlinked, and its inode is fully committed on disk, | |
7374 | * we don't have to worry about walking up the directory chain | |
7375 | * to log its parents. | |
7376 | * | |
7377 | * So, we use the last_unlink_trans field to put this transid | |
7378 | * into the file. When the file is logged we check it and | |
7379 | * don't log the parents if the file is fully on disk. | |
7380 | */ | |
4176bdbf NB |
7381 | mutex_lock(&inode->log_mutex); |
7382 | inode->last_unlink_trans = trans->transid; | |
7383 | mutex_unlock(&inode->log_mutex); | |
af4176b4 | 7384 | |
acfb5a4f FM |
7385 | if (!for_rename) |
7386 | return; | |
7387 | ||
12fcfd22 | 7388 | /* |
1e75ef03 FM |
7389 | * If this directory was already logged, any new names will be logged |
7390 | * with btrfs_log_new_name() and old names will be deleted from the log | |
7391 | * tree with btrfs_del_dir_entries_in_log() or with | |
7392 | * btrfs_del_inode_ref_in_log(). | |
12fcfd22 | 7393 | */ |
d67ba263 | 7394 | if (inode_logged(trans, dir, NULL) == 1) |
12fcfd22 CM |
7395 | return; |
7396 | ||
7397 | /* | |
1e75ef03 FM |
7398 | * If the inode we're about to unlink was logged before, the log will be |
7399 | * properly updated with the new name with btrfs_log_new_name() and the | |
7400 | * old name removed with btrfs_del_dir_entries_in_log() or with | |
7401 | * btrfs_del_inode_ref_in_log(). | |
12fcfd22 | 7402 | */ |
d67ba263 | 7403 | if (inode_logged(trans, inode, NULL) == 1) |
12fcfd22 CM |
7404 | return; |
7405 | ||
7406 | /* | |
7407 | * when renaming files across directories, if the directory | |
7408 | * there we're unlinking from gets fsync'd later on, there's | |
7409 | * no way to find the destination directory later and fsync it | |
7410 | * properly. So, we have to be conservative and force commits | |
7411 | * so the new name gets discovered. | |
7412 | */ | |
4176bdbf NB |
7413 | mutex_lock(&dir->log_mutex); |
7414 | dir->last_unlink_trans = trans->transid; | |
7415 | mutex_unlock(&dir->log_mutex); | |
1ec9a1ae FM |
7416 | } |
7417 | ||
7418 | /* | |
7419 | * Make sure that if someone attempts to fsync the parent directory of a deleted | |
7420 | * snapshot, it ends up triggering a transaction commit. This is to guarantee | |
7421 | * that after replaying the log tree of the parent directory's root we will not | |
7422 | * see the snapshot anymore and at log replay time we will not see any log tree | |
7423 | * corresponding to the deleted snapshot's root, which could lead to replaying | |
7424 | * it after replaying the log tree of the parent directory (which would replay | |
7425 | * the snapshot delete operation). | |
2be63d5c FM |
7426 | * |
7427 | * Must be called before the actual snapshot destroy operation (updates to the | |
7428 | * parent root and tree of tree roots trees, etc) are done. | |
1ec9a1ae FM |
7429 | */ |
7430 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, | |
43663557 | 7431 | struct btrfs_inode *dir) |
1ec9a1ae | 7432 | { |
43663557 NB |
7433 | mutex_lock(&dir->log_mutex); |
7434 | dir->last_unlink_trans = trans->transid; | |
7435 | mutex_unlock(&dir->log_mutex); | |
12fcfd22 CM |
7436 | } |
7437 | ||
43dd529a | 7438 | /* |
d5f5bd54 FM |
7439 | * Update the log after adding a new name for an inode. |
7440 | * | |
7441 | * @trans: Transaction handle. | |
7442 | * @old_dentry: The dentry associated with the old name and the old | |
7443 | * parent directory. | |
7444 | * @old_dir: The inode of the previous parent directory for the case | |
7445 | * of a rename. For a link operation, it must be NULL. | |
88d2beec FM |
7446 | * @old_dir_index: The index number associated with the old name, meaningful |
7447 | * only for rename operations (when @old_dir is not NULL). | |
7448 | * Ignored for link operations. | |
d5f5bd54 FM |
7449 | * @parent: The dentry associated with the directory under which the |
7450 | * new name is located. | |
7451 | * | |
7452 | * Call this after adding a new name for an inode, as a result of a link or | |
7453 | * rename operation, and it will properly update the log to reflect the new name. | |
12fcfd22 | 7454 | */ |
75b463d2 | 7455 | void btrfs_log_new_name(struct btrfs_trans_handle *trans, |
d5f5bd54 | 7456 | struct dentry *old_dentry, struct btrfs_inode *old_dir, |
88d2beec | 7457 | u64 old_dir_index, struct dentry *parent) |
12fcfd22 | 7458 | { |
d5f5bd54 | 7459 | struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry)); |
259c4b96 | 7460 | struct btrfs_root *root = inode->root; |
75b463d2 | 7461 | struct btrfs_log_ctx ctx; |
259c4b96 | 7462 | bool log_pinned = false; |
0f8ce498 | 7463 | int ret; |
12fcfd22 | 7464 | |
af4176b4 CM |
7465 | /* |
7466 | * this will force the logging code to walk the dentry chain | |
7467 | * up for the file | |
7468 | */ | |
9a6509c4 | 7469 | if (!S_ISDIR(inode->vfs_inode.i_mode)) |
9ca5fbfb | 7470 | inode->last_unlink_trans = trans->transid; |
af4176b4 | 7471 | |
12fcfd22 CM |
7472 | /* |
7473 | * if this inode hasn't been logged and directory we're renaming it | |
7474 | * from hasn't been logged, we don't need to log it | |
7475 | */ | |
0f8ce498 FM |
7476 | ret = inode_logged(trans, inode, NULL); |
7477 | if (ret < 0) { | |
7478 | goto out; | |
7479 | } else if (ret == 0) { | |
7480 | if (!old_dir) | |
7481 | return; | |
7482 | /* | |
7483 | * If the inode was not logged and we are doing a rename (old_dir is not | |
7484 | * NULL), check if old_dir was logged - if it was not we can return and | |
7485 | * do nothing. | |
7486 | */ | |
7487 | ret = inode_logged(trans, old_dir, NULL); | |
7488 | if (ret < 0) | |
7489 | goto out; | |
7490 | else if (ret == 0) | |
7491 | return; | |
7492 | } | |
7493 | ret = 0; | |
12fcfd22 | 7494 | |
54a40fc3 FM |
7495 | /* |
7496 | * If we are doing a rename (old_dir is not NULL) from a directory that | |
88d2beec FM |
7497 | * was previously logged, make sure that on log replay we get the old |
7498 | * dir entry deleted. This is needed because we will also log the new | |
7499 | * name of the renamed inode, so we need to make sure that after log | |
7500 | * replay we don't end up with both the new and old dir entries existing. | |
54a40fc3 | 7501 | */ |
88d2beec FM |
7502 | if (old_dir && old_dir->logged_trans == trans->transid) { |
7503 | struct btrfs_root *log = old_dir->root->log_root; | |
7504 | struct btrfs_path *path; | |
ab3c5c18 | 7505 | struct fscrypt_name fname; |
88d2beec FM |
7506 | |
7507 | ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX); | |
7508 | ||
ab3c5c18 STD |
7509 | ret = fscrypt_setup_filename(&old_dir->vfs_inode, |
7510 | &old_dentry->d_name, 0, &fname); | |
7511 | if (ret) | |
7512 | goto out; | |
259c4b96 FM |
7513 | /* |
7514 | * We have two inodes to update in the log, the old directory and | |
7515 | * the inode that got renamed, so we must pin the log to prevent | |
7516 | * anyone from syncing the log until we have updated both inodes | |
7517 | * in the log. | |
7518 | */ | |
723df2bc FM |
7519 | ret = join_running_log_trans(root); |
7520 | /* | |
7521 | * At least one of the inodes was logged before, so this should | |
7522 | * not fail, but if it does, it's not serious, just bail out and | |
7523 | * mark the log for a full commit. | |
7524 | */ | |
fee4c199 FM |
7525 | if (WARN_ON_ONCE(ret < 0)) { |
7526 | fscrypt_free_filename(&fname); | |
723df2bc | 7527 | goto out; |
fee4c199 FM |
7528 | } |
7529 | ||
259c4b96 | 7530 | log_pinned = true; |
259c4b96 | 7531 | |
88d2beec FM |
7532 | path = btrfs_alloc_path(); |
7533 | if (!path) { | |
259c4b96 | 7534 | ret = -ENOMEM; |
ab3c5c18 | 7535 | fscrypt_free_filename(&fname); |
259c4b96 | 7536 | goto out; |
88d2beec FM |
7537 | } |
7538 | ||
7539 | /* | |
7540 | * Other concurrent task might be logging the old directory, | |
7541 | * as it can be triggered when logging other inode that had or | |
750ee454 FM |
7542 | * still has a dentry in the old directory. We lock the old |
7543 | * directory's log_mutex to ensure the deletion of the old | |
7544 | * name is persisted, because during directory logging we | |
7545 | * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of | |
7546 | * the old name's dir index item is in the delayed items, so | |
7547 | * it could be missed by an in progress directory logging. | |
88d2beec FM |
7548 | */ |
7549 | mutex_lock(&old_dir->log_mutex); | |
7550 | ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir), | |
6db75318 | 7551 | &fname.disk_name, old_dir_index); |
88d2beec FM |
7552 | if (ret > 0) { |
7553 | /* | |
7554 | * The dentry does not exist in the log, so record its | |
7555 | * deletion. | |
7556 | */ | |
7557 | btrfs_release_path(path); | |
7558 | ret = insert_dir_log_key(trans, log, path, | |
7559 | btrfs_ino(old_dir), | |
7560 | old_dir_index, old_dir_index); | |
7561 | } | |
7562 | mutex_unlock(&old_dir->log_mutex); | |
7563 | ||
7564 | btrfs_free_path(path); | |
ab3c5c18 | 7565 | fscrypt_free_filename(&fname); |
259c4b96 FM |
7566 | if (ret < 0) |
7567 | goto out; | |
88d2beec | 7568 | } |
54a40fc3 | 7569 | |
75b463d2 FM |
7570 | btrfs_init_log_ctx(&ctx, &inode->vfs_inode); |
7571 | ctx.logging_new_name = true; | |
e383e158 | 7572 | btrfs_init_log_ctx_scratch_eb(&ctx); |
75b463d2 FM |
7573 | /* |
7574 | * We don't care about the return value. If we fail to log the new name | |
7575 | * then we know the next attempt to sync the log will fallback to a full | |
7576 | * transaction commit (due to a call to btrfs_set_log_full_commit()), so | |
7577 | * we don't need to worry about getting a log committed that has an | |
7578 | * inconsistent state after a rename operation. | |
7579 | */ | |
48778179 | 7580 | btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx); |
e383e158 | 7581 | free_extent_buffer(ctx.scratch_eb); |
e09d94c9 | 7582 | ASSERT(list_empty(&ctx.conflict_inodes)); |
259c4b96 | 7583 | out: |
0f8ce498 FM |
7584 | /* |
7585 | * If an error happened mark the log for a full commit because it's not | |
7586 | * consistent and up to date or we couldn't find out if one of the | |
7587 | * inodes was logged before in this transaction. Do it before unpinning | |
7588 | * the log, to avoid any races with someone else trying to commit it. | |
7589 | */ | |
7590 | if (ret < 0) | |
7591 | btrfs_set_log_full_commit(trans); | |
7592 | if (log_pinned) | |
259c4b96 | 7593 | btrfs_end_log_trans(root); |
12fcfd22 CM |
7594 | } |
7595 |