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