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