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