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