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