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