1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2e02119d5SChris Mason /*
3e02119d5SChris Mason * Copyright (C) 2008 Oracle. All rights reserved.
4e02119d5SChris Mason */
5e02119d5SChris Mason
6e02119d5SChris Mason #include <linux/sched.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
8c6adc9ccSMiao Xie #include <linux/blkdev.h>
95dc562c5SJosef Bacik #include <linux/list_sort.h>
10c7f88c4eSJeff Layton #include <linux/iversion.h>
11602cbe91SDavid Sterba #include "misc.h"
129678c543SNikolay Borisov #include "ctree.h"
13995946ddSMiao Xie #include "tree-log.h"
14e02119d5SChris Mason #include "disk-io.h"
15e02119d5SChris Mason #include "locking.h"
16e02119d5SChris Mason #include "print-tree.h"
17f186373fSMark Fasheh #include "backref.h"
18ebb8765bSAnand Jain #include "compression.h"
19df2c95f3SQu Wenruo #include "qgroup.h"
206787bb9fSNikolay Borisov #include "block-group.h"
216787bb9fSNikolay Borisov #include "space-info.h"
22d3575156SNaohiro Aota #include "zoned.h"
2326c2c454SJosef Bacik #include "inode-item.h"
24c7f13d42SJosef Bacik #include "fs.h"
25ad1ac501SJosef Bacik #include "accessors.h"
26a0231804SJosef Bacik #include "extent-tree.h"
2745c40c8fSJosef Bacik #include "root-tree.h"
28f2b39277SJosef Bacik #include "dir-item.h"
297c8ede16SJosef Bacik #include "file-item.h"
30af142b6fSJosef Bacik #include "file.h"
31aa5d3003SJosef Bacik #include "orphan.h"
32103c1972SChristoph Hellwig #include "tree-checker.h"
33e02119d5SChris Mason
34e09d94c9SFilipe Manana #define MAX_CONFLICT_INODES 10
35e09d94c9SFilipe Manana
36e02119d5SChris Mason /* magic values for the inode_only field in btrfs_log_inode:
37e02119d5SChris Mason *
38e02119d5SChris Mason * LOG_INODE_ALL means to log everything
39e02119d5SChris Mason * LOG_INODE_EXISTS means to log just enough to recreate the inode
40e02119d5SChris Mason * during log replay
41e02119d5SChris Mason */
42e13976cfSDavid Sterba enum {
43e13976cfSDavid Sterba LOG_INODE_ALL,
44e13976cfSDavid Sterba LOG_INODE_EXISTS,
45e13976cfSDavid Sterba };
46e02119d5SChris Mason
47e02119d5SChris Mason /*
4812fcfd22SChris Mason * directory trouble cases
4912fcfd22SChris Mason *
5012fcfd22SChris Mason * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
5112fcfd22SChris Mason * log, we must force a full commit before doing an fsync of the directory
5212fcfd22SChris Mason * where the unlink was done.
5312fcfd22SChris Mason * ---> record transid of last unlink/rename per directory
5412fcfd22SChris Mason *
5512fcfd22SChris Mason * mkdir foo/some_dir
5612fcfd22SChris Mason * normal commit
5712fcfd22SChris Mason * rename foo/some_dir foo2/some_dir
5812fcfd22SChris Mason * mkdir foo/some_dir
5912fcfd22SChris Mason * fsync foo/some_dir/some_file
6012fcfd22SChris Mason *
6112fcfd22SChris Mason * The fsync above will unlink the original some_dir without recording
6212fcfd22SChris Mason * it in its new location (foo2). After a crash, some_dir will be gone
6312fcfd22SChris Mason * unless the fsync of some_file forces a full commit
6412fcfd22SChris Mason *
6512fcfd22SChris Mason * 2) we must log any new names for any file or dir that is in the fsync
6612fcfd22SChris Mason * log. ---> check inode while renaming/linking.
6712fcfd22SChris Mason *
6812fcfd22SChris Mason * 2a) we must log any new names for any file or dir during rename
6912fcfd22SChris Mason * when the directory they are being removed from was logged.
7012fcfd22SChris Mason * ---> check inode and old parent dir during rename
7112fcfd22SChris Mason *
7212fcfd22SChris Mason * 2a is actually the more important variant. With the extra logging
7312fcfd22SChris Mason * a crash might unlink the old name without recreating the new one
7412fcfd22SChris Mason *
7512fcfd22SChris Mason * 3) after a crash, we must go through any directories with a link count
7612fcfd22SChris Mason * of zero and redo the rm -rf
7712fcfd22SChris Mason *
7812fcfd22SChris Mason * mkdir f1/foo
7912fcfd22SChris Mason * normal commit
8012fcfd22SChris Mason * rm -rf f1/foo
8112fcfd22SChris Mason * fsync(f1)
8212fcfd22SChris Mason *
8312fcfd22SChris Mason * The directory f1 was fully removed from the FS, but fsync was never
8412fcfd22SChris Mason * called on f1, only its parent dir. After a crash the rm -rf must
8512fcfd22SChris Mason * be replayed. This must be able to recurse down the entire
8612fcfd22SChris Mason * directory tree. The inode link count fixup code takes care of the
8712fcfd22SChris Mason * ugly details.
8812fcfd22SChris Mason */
8912fcfd22SChris Mason
9012fcfd22SChris Mason /*
91e02119d5SChris Mason * stages for the tree walking. The first
92e02119d5SChris Mason * stage (0) is to only pin down the blocks we find
93e02119d5SChris Mason * the second stage (1) is to make sure that all the inodes
94e02119d5SChris Mason * we find in the log are created in the subvolume.
95e02119d5SChris Mason *
96e02119d5SChris Mason * The last stage is to deal with directories and links and extents
97e02119d5SChris Mason * and all the other fun semantics
98e02119d5SChris Mason */
99e13976cfSDavid Sterba enum {
100e13976cfSDavid Sterba LOG_WALK_PIN_ONLY,
101e13976cfSDavid Sterba LOG_WALK_REPLAY_INODES,
102e13976cfSDavid Sterba LOG_WALK_REPLAY_DIR_INDEX,
103e13976cfSDavid Sterba LOG_WALK_REPLAY_ALL,
104e13976cfSDavid Sterba };
105e02119d5SChris Mason
10612fcfd22SChris Mason static int btrfs_log_inode(struct btrfs_trans_handle *trans,
10790d04510SFilipe Manana struct btrfs_inode *inode,
10849dae1bcSFilipe Manana int inode_only,
1098407f553SFilipe Manana struct btrfs_log_ctx *ctx);
110ec051c0fSYan Zheng static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
111ec051c0fSYan Zheng struct btrfs_root *root,
112ec051c0fSYan Zheng struct btrfs_path *path, u64 objectid);
11312fcfd22SChris Mason static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
11412fcfd22SChris Mason struct btrfs_root *root,
11512fcfd22SChris Mason struct btrfs_root *log,
11612fcfd22SChris Mason struct btrfs_path *path,
11712fcfd22SChris Mason u64 dirid, int del_all);
118fa1a0f42SNaohiro Aota static void wait_log_commit(struct btrfs_root *root, int transid);
119e02119d5SChris Mason
120e02119d5SChris Mason /*
121e02119d5SChris Mason * tree logging is a special write ahead log used to make sure that
122e02119d5SChris Mason * fsyncs and O_SYNCs can happen without doing full tree commits.
123e02119d5SChris Mason *
124e02119d5SChris Mason * Full tree commits are expensive because they require commonly
125e02119d5SChris Mason * modified blocks to be recowed, creating many dirty pages in the
126e02119d5SChris Mason * extent tree an 4x-6x higher write load than ext3.
127e02119d5SChris Mason *
128e02119d5SChris Mason * Instead of doing a tree commit on every fsync, we use the
129e02119d5SChris Mason * key ranges and transaction ids to find items for a given file or directory
130e02119d5SChris Mason * that have changed in this transaction. Those items are copied into
131e02119d5SChris Mason * a special tree (one per subvolume root), that tree is written to disk
132e02119d5SChris Mason * and then the fsync is considered complete.
133e02119d5SChris Mason *
134e02119d5SChris Mason * After a crash, items are copied out of the log-tree back into the
135e02119d5SChris Mason * subvolume tree. Any file data extents found are recorded in the extent
136e02119d5SChris Mason * allocation tree, and the log-tree freed.
137e02119d5SChris Mason *
138e02119d5SChris Mason * The log tree is read three times, once to pin down all the extents it is
139e02119d5SChris Mason * using in ram and once, once to create all the inodes logged in the tree
140e02119d5SChris Mason * and once to do all the other items.
141e02119d5SChris Mason */
142e02119d5SChris Mason
btrfs_iget_logging(u64 objectid,struct btrfs_root * root)143cf0291a6SFilipe Manana static struct inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root)
144cf0291a6SFilipe Manana {
145cf0291a6SFilipe Manana unsigned int nofs_flag;
146cf0291a6SFilipe Manana struct inode *inode;
147cf0291a6SFilipe Manana
148cf0291a6SFilipe Manana /*
149cf0291a6SFilipe Manana * We're holding a transaction handle whether we are logging or
150cf0291a6SFilipe Manana * replaying a log tree, so we must make sure NOFS semantics apply
151cf0291a6SFilipe Manana * because btrfs_alloc_inode() may be triggered and it uses GFP_KERNEL
152cf0291a6SFilipe Manana * to allocate an inode, which can recurse back into the filesystem and
153cf0291a6SFilipe Manana * attempt a transaction commit, resulting in a deadlock.
154cf0291a6SFilipe Manana */
155cf0291a6SFilipe Manana nofs_flag = memalloc_nofs_save();
156cf0291a6SFilipe Manana inode = btrfs_iget(root->fs_info->sb, objectid, root);
157cf0291a6SFilipe Manana memalloc_nofs_restore(nofs_flag);
158cf0291a6SFilipe Manana
159cf0291a6SFilipe Manana return inode;
160cf0291a6SFilipe Manana }
161cf0291a6SFilipe Manana
162e02119d5SChris Mason /*
163e02119d5SChris Mason * start a sub transaction and setup the log tree
164e02119d5SChris Mason * this increments the log tree writer count to make the people
165e02119d5SChris Mason * syncing the tree wait for us to finish
166e02119d5SChris Mason */
start_log_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_log_ctx * ctx)167e02119d5SChris Mason static int start_log_trans(struct btrfs_trans_handle *trans,
1688b050d35SMiao Xie struct btrfs_root *root,
1698b050d35SMiao Xie struct btrfs_log_ctx *ctx)
170e02119d5SChris Mason {
1710b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
17247876f7cSFilipe Manana struct btrfs_root *tree_root = fs_info->tree_root;
173fa1a0f42SNaohiro Aota const bool zoned = btrfs_is_zoned(fs_info);
17434eb2a52SZhaolei int ret = 0;
175fa1a0f42SNaohiro Aota bool created = false;
1767237f183SYan Zheng
17747876f7cSFilipe Manana /*
17847876f7cSFilipe Manana * First check if the log root tree was already created. If not, create
17947876f7cSFilipe Manana * it before locking the root's log_mutex, just to keep lockdep happy.
18047876f7cSFilipe Manana */
18147876f7cSFilipe Manana if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) {
18247876f7cSFilipe Manana mutex_lock(&tree_root->log_mutex);
18347876f7cSFilipe Manana if (!fs_info->log_root_tree) {
18447876f7cSFilipe Manana ret = btrfs_init_log_root_tree(trans, fs_info);
185fa1a0f42SNaohiro Aota if (!ret) {
18647876f7cSFilipe Manana set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state);
187fa1a0f42SNaohiro Aota created = true;
188fa1a0f42SNaohiro Aota }
18947876f7cSFilipe Manana }
19047876f7cSFilipe Manana mutex_unlock(&tree_root->log_mutex);
19147876f7cSFilipe Manana if (ret)
19247876f7cSFilipe Manana return ret;
19347876f7cSFilipe Manana }
19447876f7cSFilipe Manana
1957237f183SYan Zheng mutex_lock(&root->log_mutex);
19634eb2a52SZhaolei
197fa1a0f42SNaohiro Aota again:
1987237f183SYan Zheng if (root->log_root) {
199fa1a0f42SNaohiro Aota int index = (root->log_transid + 1) % 2;
200fa1a0f42SNaohiro Aota
2014884b8e8SDavid Sterba if (btrfs_need_log_full_commit(trans)) {
202f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
20350471a38SMiao Xie goto out;
20450471a38SMiao Xie }
20534eb2a52SZhaolei
206fa1a0f42SNaohiro Aota if (zoned && atomic_read(&root->log_commit[index])) {
207fa1a0f42SNaohiro Aota wait_log_commit(root, root->log_transid - 1);
208fa1a0f42SNaohiro Aota goto again;
209fa1a0f42SNaohiro Aota }
210fa1a0f42SNaohiro Aota
211ff782e0aSJosef Bacik if (!root->log_start_pid) {
21227cdeb70SMiao Xie clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
21334eb2a52SZhaolei root->log_start_pid = current->pid;
214ff782e0aSJosef Bacik } else if (root->log_start_pid != current->pid) {
21527cdeb70SMiao Xie set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
216ff782e0aSJosef Bacik }
21734eb2a52SZhaolei } else {
218fa1a0f42SNaohiro Aota /*
219fa1a0f42SNaohiro Aota * This means fs_info->log_root_tree was already created
220fa1a0f42SNaohiro Aota * for some other FS trees. Do the full commit not to mix
221fa1a0f42SNaohiro Aota * nodes from multiple log transactions to do sequential
222fa1a0f42SNaohiro Aota * writing.
223fa1a0f42SNaohiro Aota */
224fa1a0f42SNaohiro Aota if (zoned && !created) {
225f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
226fa1a0f42SNaohiro Aota goto out;
227fa1a0f42SNaohiro Aota }
228fa1a0f42SNaohiro Aota
229e02119d5SChris Mason ret = btrfs_add_log_tree(trans, root);
2304a500fd1SYan, Zheng if (ret)
231e87ac136SMiao Xie goto out;
23234eb2a52SZhaolei
233e7a79811SFilipe Manana set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
23427cdeb70SMiao Xie clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
235e87ac136SMiao Xie root->log_start_pid = current->pid;
23634eb2a52SZhaolei }
23734eb2a52SZhaolei
2387237f183SYan Zheng atomic_inc(&root->log_writers);
239289cffcbSFilipe Manana if (!ctx->logging_new_name) {
24034eb2a52SZhaolei int index = root->log_transid % 2;
2418b050d35SMiao Xie list_add_tail(&ctx->list, &root->log_ctxs[index]);
242d1433debSMiao Xie ctx->log_transid = root->log_transid;
2438b050d35SMiao Xie }
24434eb2a52SZhaolei
245e87ac136SMiao Xie out:
2467237f183SYan Zheng mutex_unlock(&root->log_mutex);
247e87ac136SMiao Xie return ret;
248e02119d5SChris Mason }
249e02119d5SChris Mason
250e02119d5SChris Mason /*
251e02119d5SChris Mason * returns 0 if there was a log transaction running and we were able
252e02119d5SChris Mason * to join, or returns -ENOENT if there were not transactions
253e02119d5SChris Mason * in progress
254e02119d5SChris Mason */
join_running_log_trans(struct btrfs_root * root)255e02119d5SChris Mason static int join_running_log_trans(struct btrfs_root *root)
256e02119d5SChris Mason {
257fa1a0f42SNaohiro Aota const bool zoned = btrfs_is_zoned(root->fs_info);
258e02119d5SChris Mason int ret = -ENOENT;
259e02119d5SChris Mason
260e7a79811SFilipe Manana if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
261e7a79811SFilipe Manana return ret;
262e7a79811SFilipe Manana
2637237f183SYan Zheng mutex_lock(&root->log_mutex);
264fa1a0f42SNaohiro Aota again:
265e02119d5SChris Mason if (root->log_root) {
266fa1a0f42SNaohiro Aota int index = (root->log_transid + 1) % 2;
267fa1a0f42SNaohiro Aota
268e02119d5SChris Mason ret = 0;
269fa1a0f42SNaohiro Aota if (zoned && atomic_read(&root->log_commit[index])) {
270fa1a0f42SNaohiro Aota wait_log_commit(root, root->log_transid - 1);
271fa1a0f42SNaohiro Aota goto again;
272fa1a0f42SNaohiro Aota }
2737237f183SYan Zheng atomic_inc(&root->log_writers);
274e02119d5SChris Mason }
2757237f183SYan Zheng mutex_unlock(&root->log_mutex);
276e02119d5SChris Mason return ret;
277e02119d5SChris Mason }
278e02119d5SChris Mason
279e02119d5SChris Mason /*
28012fcfd22SChris Mason * This either makes the current running log transaction wait
28112fcfd22SChris Mason * until you call btrfs_end_log_trans() or it makes any future
28212fcfd22SChris Mason * log transactions wait until you call btrfs_end_log_trans()
28312fcfd22SChris Mason */
btrfs_pin_log_trans(struct btrfs_root * root)28445128b08Szhong jiang void btrfs_pin_log_trans(struct btrfs_root *root)
28512fcfd22SChris Mason {
28612fcfd22SChris Mason atomic_inc(&root->log_writers);
28712fcfd22SChris Mason }
28812fcfd22SChris Mason
28912fcfd22SChris Mason /*
290e02119d5SChris Mason * indicate we're done making changes to the log tree
291e02119d5SChris Mason * and wake up anyone waiting to do a sync
292e02119d5SChris Mason */
btrfs_end_log_trans(struct btrfs_root * root)293143bede5SJeff Mahoney void btrfs_end_log_trans(struct btrfs_root *root)
294e02119d5SChris Mason {
2957237f183SYan Zheng if (atomic_dec_and_test(&root->log_writers)) {
296093258e6SDavid Sterba /* atomic_dec_and_test implies a barrier */
297093258e6SDavid Sterba cond_wake_up_nomb(&root->log_writer_wait);
2987237f183SYan Zheng }
299e02119d5SChris Mason }
300e02119d5SChris Mason
301e02119d5SChris Mason /*
302e02119d5SChris Mason * the walk control struct is used to pass state down the chain when
303e02119d5SChris Mason * processing the log tree. The stage field tells us which part
304e02119d5SChris Mason * of the log tree processing we are currently doing. The others
305e02119d5SChris Mason * are state fields used for that specific part
306e02119d5SChris Mason */
307e02119d5SChris Mason struct walk_control {
308e02119d5SChris Mason /* should we free the extent on disk when done? This is used
309e02119d5SChris Mason * at transaction commit time while freeing a log tree
310e02119d5SChris Mason */
311e02119d5SChris Mason int free;
312e02119d5SChris Mason
313e02119d5SChris Mason /* pin only walk, we record which extents on disk belong to the
314e02119d5SChris Mason * log trees
315e02119d5SChris Mason */
316e02119d5SChris Mason int pin;
317e02119d5SChris Mason
318e02119d5SChris Mason /* what stage of the replay code we're currently in */
319e02119d5SChris Mason int stage;
320e02119d5SChris Mason
321f2d72f42SFilipe Manana /*
322f2d72f42SFilipe Manana * Ignore any items from the inode currently being processed. Needs
323f2d72f42SFilipe Manana * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
324f2d72f42SFilipe Manana * the LOG_WALK_REPLAY_INODES stage.
325f2d72f42SFilipe Manana */
326f2d72f42SFilipe Manana bool ignore_cur_inode;
327f2d72f42SFilipe Manana
328e02119d5SChris Mason /* the root we are currently replaying */
329e02119d5SChris Mason struct btrfs_root *replay_dest;
330e02119d5SChris Mason
331e02119d5SChris Mason /* the trans handle for the current replay */
332e02119d5SChris Mason struct btrfs_trans_handle *trans;
333e02119d5SChris Mason
334e02119d5SChris Mason /* the function that gets used to process blocks we find in the
335e02119d5SChris Mason * tree. Note the extent_buffer might not be up to date when it is
336e02119d5SChris Mason * passed in, and it must be checked or read if you need the data
337e02119d5SChris Mason * inside it
338e02119d5SChris Mason */
339e02119d5SChris Mason int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
340581c1760SQu Wenruo struct walk_control *wc, u64 gen, int level);
341e02119d5SChris Mason };
342e02119d5SChris Mason
343e02119d5SChris Mason /*
344e02119d5SChris Mason * process_func used to pin down extents, write them or wait on them
345e02119d5SChris Mason */
process_one_buffer(struct btrfs_root * log,struct extent_buffer * eb,struct walk_control * wc,u64 gen,int level)346e02119d5SChris Mason static int process_one_buffer(struct btrfs_root *log,
347e02119d5SChris Mason struct extent_buffer *eb,
348581c1760SQu Wenruo struct walk_control *wc, u64 gen, int level)
349e02119d5SChris Mason {
3500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = log->fs_info;
351b50c6e25SJosef Bacik int ret = 0;
352b50c6e25SJosef Bacik
3538c2a1a30SJosef Bacik /*
3548c2a1a30SJosef Bacik * If this fs is mixed then we need to be able to process the leaves to
3558c2a1a30SJosef Bacik * pin down any logged extents, so we have to read the block.
3568c2a1a30SJosef Bacik */
3570b246afaSJeff Mahoney if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
358789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = {
359789d6a3aSQu Wenruo .level = level,
360789d6a3aSQu Wenruo .transid = gen
361789d6a3aSQu Wenruo };
362789d6a3aSQu Wenruo
363789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(eb, &check);
3648c2a1a30SJosef Bacik if (ret)
3658c2a1a30SJosef Bacik return ret;
3668c2a1a30SJosef Bacik }
3678c2a1a30SJosef Bacik
368c816d705SFilipe Manana if (wc->pin) {
3699fce5704SNikolay Borisov ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
3702ff7e61eSJeff Mahoney eb->len);
371c816d705SFilipe Manana if (ret)
372c816d705SFilipe Manana return ret;
373e02119d5SChris Mason
374c816d705SFilipe Manana if (btrfs_buffer_uptodate(eb, gen, 0) &&
375c816d705SFilipe Manana btrfs_header_level(eb) == 0)
376bcdc428cSDavid Sterba ret = btrfs_exclude_logged_extents(eb);
377e02119d5SChris Mason }
378b50c6e25SJosef Bacik return ret;
379e02119d5SChris Mason }
380e02119d5SChris Mason
3813a8d1db3SFilipe Manana /*
3823a8d1db3SFilipe Manana * Item overwrite used by replay and tree logging. eb, slot and key all refer
3833a8d1db3SFilipe Manana * to the src data we are copying out.
3843a8d1db3SFilipe Manana *
3853a8d1db3SFilipe Manana * root is the tree we are copying into, and path is a scratch
3863a8d1db3SFilipe Manana * path for use in this function (it should be released on entry and
3873a8d1db3SFilipe Manana * will be released on exit).
3883a8d1db3SFilipe Manana *
3893a8d1db3SFilipe Manana * If the key is already in the destination tree the existing item is
3903a8d1db3SFilipe Manana * overwritten. If the existing item isn't big enough, it is extended.
3913a8d1db3SFilipe Manana * If it is too large, it is truncated.
3923a8d1db3SFilipe Manana *
3933a8d1db3SFilipe Manana * If the key isn't in the destination yet, a new item is inserted.
3943a8d1db3SFilipe Manana */
overwrite_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)3953a8d1db3SFilipe Manana static int overwrite_item(struct btrfs_trans_handle *trans,
396e02119d5SChris Mason struct btrfs_root *root,
397e02119d5SChris Mason struct btrfs_path *path,
398e02119d5SChris Mason struct extent_buffer *eb, int slot,
399e02119d5SChris Mason struct btrfs_key *key)
400e02119d5SChris Mason {
401e02119d5SChris Mason int ret;
402e02119d5SChris Mason u32 item_size;
403e02119d5SChris Mason u64 saved_i_size = 0;
404e02119d5SChris Mason int save_old_i_size = 0;
405e02119d5SChris Mason unsigned long src_ptr;
406e02119d5SChris Mason unsigned long dst_ptr;
4074bc4bee4SJosef Bacik bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
408e02119d5SChris Mason
4093eb42344SFilipe Manana /*
4103eb42344SFilipe Manana * This is only used during log replay, so the root is always from a
4113eb42344SFilipe Manana * fs/subvolume tree. In case we ever need to support a log root, then
4123eb42344SFilipe Manana * we'll have to clone the leaf in the path, release the path and use
4133eb42344SFilipe Manana * the leaf before writing into the log tree. See the comments at
4143eb42344SFilipe Manana * copy_items() for more details.
4153eb42344SFilipe Manana */
4163eb42344SFilipe Manana ASSERT(root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID);
417e02119d5SChris Mason
4183212fa14SJosef Bacik item_size = btrfs_item_size(eb, slot);
419e02119d5SChris Mason src_ptr = btrfs_item_ptr_offset(eb, slot);
420e02119d5SChris Mason
4213a8d1db3SFilipe Manana /* Look for the key in the destination tree. */
4223a8d1db3SFilipe Manana ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
4233a8d1db3SFilipe Manana if (ret < 0)
4243a8d1db3SFilipe Manana return ret;
4254bc4bee4SJosef Bacik
426e02119d5SChris Mason if (ret == 0) {
427e02119d5SChris Mason char *src_copy;
428e02119d5SChris Mason char *dst_copy;
4293212fa14SJosef Bacik u32 dst_size = btrfs_item_size(path->nodes[0],
430e02119d5SChris Mason path->slots[0]);
431e02119d5SChris Mason if (dst_size != item_size)
432e02119d5SChris Mason goto insert;
433e02119d5SChris Mason
434e02119d5SChris Mason if (item_size == 0) {
435b3b4aa74SDavid Sterba btrfs_release_path(path);
436e02119d5SChris Mason return 0;
437e02119d5SChris Mason }
438e02119d5SChris Mason dst_copy = kmalloc(item_size, GFP_NOFS);
439e02119d5SChris Mason src_copy = kmalloc(item_size, GFP_NOFS);
4402a29edc6Sliubo if (!dst_copy || !src_copy) {
441b3b4aa74SDavid Sterba btrfs_release_path(path);
4422a29edc6Sliubo kfree(dst_copy);
4432a29edc6Sliubo kfree(src_copy);
4442a29edc6Sliubo return -ENOMEM;
4452a29edc6Sliubo }
446e02119d5SChris Mason
447e02119d5SChris Mason read_extent_buffer(eb, src_copy, src_ptr, item_size);
448e02119d5SChris Mason
449e02119d5SChris Mason dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
450e02119d5SChris Mason read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
451e02119d5SChris Mason item_size);
452e02119d5SChris Mason ret = memcmp(dst_copy, src_copy, item_size);
453e02119d5SChris Mason
454e02119d5SChris Mason kfree(dst_copy);
455e02119d5SChris Mason kfree(src_copy);
456e02119d5SChris Mason /*
457e02119d5SChris Mason * they have the same contents, just return, this saves
458e02119d5SChris Mason * us from cowing blocks in the destination tree and doing
459e02119d5SChris Mason * extra writes that may not have been done by a previous
460e02119d5SChris Mason * sync
461e02119d5SChris Mason */
462e02119d5SChris Mason if (ret == 0) {
463b3b4aa74SDavid Sterba btrfs_release_path(path);
464e02119d5SChris Mason return 0;
465e02119d5SChris Mason }
466e02119d5SChris Mason
4674bc4bee4SJosef Bacik /*
4684bc4bee4SJosef Bacik * We need to load the old nbytes into the inode so when we
4694bc4bee4SJosef Bacik * replay the extents we've logged we get the right nbytes.
4704bc4bee4SJosef Bacik */
4714bc4bee4SJosef Bacik if (inode_item) {
4724bc4bee4SJosef Bacik struct btrfs_inode_item *item;
4734bc4bee4SJosef Bacik u64 nbytes;
474d555438bSJosef Bacik u32 mode;
4754bc4bee4SJosef Bacik
4764bc4bee4SJosef Bacik item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4774bc4bee4SJosef Bacik struct btrfs_inode_item);
4784bc4bee4SJosef Bacik nbytes = btrfs_inode_nbytes(path->nodes[0], item);
4794bc4bee4SJosef Bacik item = btrfs_item_ptr(eb, slot,
4804bc4bee4SJosef Bacik struct btrfs_inode_item);
4814bc4bee4SJosef Bacik btrfs_set_inode_nbytes(eb, item, nbytes);
482d555438bSJosef Bacik
483d555438bSJosef Bacik /*
484d555438bSJosef Bacik * If this is a directory we need to reset the i_size to
485d555438bSJosef Bacik * 0 so that we can set it up properly when replaying
486d555438bSJosef Bacik * the rest of the items in this log.
487d555438bSJosef Bacik */
488d555438bSJosef Bacik mode = btrfs_inode_mode(eb, item);
489d555438bSJosef Bacik if (S_ISDIR(mode))
490d555438bSJosef Bacik btrfs_set_inode_size(eb, item, 0);
4914bc4bee4SJosef Bacik }
4924bc4bee4SJosef Bacik } else if (inode_item) {
4934bc4bee4SJosef Bacik struct btrfs_inode_item *item;
494d555438bSJosef Bacik u32 mode;
4954bc4bee4SJosef Bacik
4964bc4bee4SJosef Bacik /*
4974bc4bee4SJosef Bacik * New inode, set nbytes to 0 so that the nbytes comes out
4984bc4bee4SJosef Bacik * properly when we replay the extents.
4994bc4bee4SJosef Bacik */
5004bc4bee4SJosef Bacik item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
5014bc4bee4SJosef Bacik btrfs_set_inode_nbytes(eb, item, 0);
502d555438bSJosef Bacik
503d555438bSJosef Bacik /*
504d555438bSJosef Bacik * If this is a directory we need to reset the i_size to 0 so
505d555438bSJosef Bacik * that we can set it up properly when replaying the rest of
506d555438bSJosef Bacik * the items in this log.
507d555438bSJosef Bacik */
508d555438bSJosef Bacik mode = btrfs_inode_mode(eb, item);
509d555438bSJosef Bacik if (S_ISDIR(mode))
510d555438bSJosef Bacik btrfs_set_inode_size(eb, item, 0);
511e02119d5SChris Mason }
512e02119d5SChris Mason insert:
513b3b4aa74SDavid Sterba btrfs_release_path(path);
514e02119d5SChris Mason /* try to insert the key into the destination tree */
515df8d116fSFilipe Manana path->skip_release_on_error = 1;
516e02119d5SChris Mason ret = btrfs_insert_empty_item(trans, root, path,
517e02119d5SChris Mason key, item_size);
518df8d116fSFilipe Manana path->skip_release_on_error = 0;
519e02119d5SChris Mason
520e02119d5SChris Mason /* make sure any existing item is the correct size */
521df8d116fSFilipe Manana if (ret == -EEXIST || ret == -EOVERFLOW) {
522e02119d5SChris Mason u32 found_size;
5233212fa14SJosef Bacik found_size = btrfs_item_size(path->nodes[0],
524e02119d5SChris Mason path->slots[0]);
525143bede5SJeff Mahoney if (found_size > item_size)
526d5e09e38SFilipe Manana btrfs_truncate_item(trans, path, item_size, 1);
527143bede5SJeff Mahoney else if (found_size < item_size)
528d5e09e38SFilipe Manana btrfs_extend_item(trans, path, item_size - found_size);
529e02119d5SChris Mason } else if (ret) {
5304a500fd1SYan, Zheng return ret;
531e02119d5SChris Mason }
532e02119d5SChris Mason dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
533e02119d5SChris Mason path->slots[0]);
534e02119d5SChris Mason
535e02119d5SChris Mason /* don't overwrite an existing inode if the generation number
536e02119d5SChris Mason * was logged as zero. This is done when the tree logging code
537e02119d5SChris Mason * is just logging an inode to make sure it exists after recovery.
538e02119d5SChris Mason *
539e02119d5SChris Mason * Also, don't overwrite i_size on directories during replay.
540e02119d5SChris Mason * log replay inserts and removes directory items based on the
541e02119d5SChris Mason * state of the tree found in the subvolume, and i_size is modified
542e02119d5SChris Mason * as it goes
543e02119d5SChris Mason */
544e02119d5SChris Mason if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
545e02119d5SChris Mason struct btrfs_inode_item *src_item;
546e02119d5SChris Mason struct btrfs_inode_item *dst_item;
547e02119d5SChris Mason
548e02119d5SChris Mason src_item = (struct btrfs_inode_item *)src_ptr;
549e02119d5SChris Mason dst_item = (struct btrfs_inode_item *)dst_ptr;
550e02119d5SChris Mason
5511a4bcf47SFilipe Manana if (btrfs_inode_generation(eb, src_item) == 0) {
5521a4bcf47SFilipe Manana struct extent_buffer *dst_eb = path->nodes[0];
5532f2ff0eeSFilipe Manana const u64 ino_size = btrfs_inode_size(eb, src_item);
5541a4bcf47SFilipe Manana
5552f2ff0eeSFilipe Manana /*
5562f2ff0eeSFilipe Manana * For regular files an ino_size == 0 is used only when
5572f2ff0eeSFilipe Manana * logging that an inode exists, as part of a directory
5582f2ff0eeSFilipe Manana * fsync, and the inode wasn't fsynced before. In this
5592f2ff0eeSFilipe Manana * case don't set the size of the inode in the fs/subvol
5602f2ff0eeSFilipe Manana * tree, otherwise we would be throwing valid data away.
5612f2ff0eeSFilipe Manana */
5621a4bcf47SFilipe Manana if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
5632f2ff0eeSFilipe Manana S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
56460d48e2eSDavid Sterba ino_size != 0)
56560d48e2eSDavid Sterba btrfs_set_inode_size(dst_eb, dst_item, ino_size);
566e02119d5SChris Mason goto no_copy;
5671a4bcf47SFilipe Manana }
568e02119d5SChris Mason
5693eb42344SFilipe Manana if (S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
570e02119d5SChris Mason S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
571e02119d5SChris Mason save_old_i_size = 1;
572e02119d5SChris Mason saved_i_size = btrfs_inode_size(path->nodes[0],
573e02119d5SChris Mason dst_item);
574e02119d5SChris Mason }
575e02119d5SChris Mason }
576e02119d5SChris Mason
577e02119d5SChris Mason copy_extent_buffer(path->nodes[0], eb, dst_ptr,
578e02119d5SChris Mason src_ptr, item_size);
579e02119d5SChris Mason
580e02119d5SChris Mason if (save_old_i_size) {
581e02119d5SChris Mason struct btrfs_inode_item *dst_item;
582e02119d5SChris Mason dst_item = (struct btrfs_inode_item *)dst_ptr;
583e02119d5SChris Mason btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
584e02119d5SChris Mason }
585e02119d5SChris Mason
586e02119d5SChris Mason /* make sure the generation is filled in */
587e02119d5SChris Mason if (key->type == BTRFS_INODE_ITEM_KEY) {
588e02119d5SChris Mason struct btrfs_inode_item *dst_item;
589e02119d5SChris Mason dst_item = (struct btrfs_inode_item *)dst_ptr;
590e02119d5SChris Mason if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
591e02119d5SChris Mason btrfs_set_inode_generation(path->nodes[0], dst_item,
592e02119d5SChris Mason trans->transid);
593e02119d5SChris Mason }
594e02119d5SChris Mason }
595e02119d5SChris Mason no_copy:
596d5e09e38SFilipe Manana btrfs_mark_buffer_dirty(trans, path->nodes[0]);
597b3b4aa74SDavid Sterba btrfs_release_path(path);
598e02119d5SChris Mason return 0;
599e02119d5SChris Mason }
600e02119d5SChris Mason
read_alloc_one_name(struct extent_buffer * eb,void * start,int len,struct fscrypt_str * name)601e43eec81SSweet Tea Dorminy static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len,
6026db75318SSweet Tea Dorminy struct fscrypt_str *name)
603e43eec81SSweet Tea Dorminy {
604e43eec81SSweet Tea Dorminy char *buf;
605e43eec81SSweet Tea Dorminy
606e43eec81SSweet Tea Dorminy buf = kmalloc(len, GFP_NOFS);
607e43eec81SSweet Tea Dorminy if (!buf)
608e43eec81SSweet Tea Dorminy return -ENOMEM;
609e43eec81SSweet Tea Dorminy
610e43eec81SSweet Tea Dorminy read_extent_buffer(eb, buf, (unsigned long)start, len);
611e43eec81SSweet Tea Dorminy name->name = buf;
612e43eec81SSweet Tea Dorminy name->len = len;
613e43eec81SSweet Tea Dorminy return 0;
614e43eec81SSweet Tea Dorminy }
615e43eec81SSweet Tea Dorminy
616086dcbfaSFilipe Manana /*
617e02119d5SChris Mason * simple helper to read an inode off the disk from a given root
618e02119d5SChris Mason * This can only be called for subvolume roots and not for the log
619e02119d5SChris Mason */
read_one_inode(struct btrfs_root * root,u64 objectid)620e02119d5SChris Mason static noinline struct inode *read_one_inode(struct btrfs_root *root,
621e02119d5SChris Mason u64 objectid)
622e02119d5SChris Mason {
623e02119d5SChris Mason struct inode *inode;
624e02119d5SChris Mason
625cf0291a6SFilipe Manana inode = btrfs_iget_logging(objectid, root);
6262e19f1f9SAl Viro if (IS_ERR(inode))
6275d4f98a2SYan Zheng inode = NULL;
628e02119d5SChris Mason return inode;
629e02119d5SChris Mason }
630e02119d5SChris Mason
631e02119d5SChris Mason /* replays a single extent in 'eb' at 'slot' with 'key' into the
632e02119d5SChris Mason * subvolume 'root'. path is released on entry and should be released
633e02119d5SChris Mason * on exit.
634e02119d5SChris Mason *
635e02119d5SChris Mason * extents in the log tree have not been allocated out of the extent
636e02119d5SChris Mason * tree yet. So, this completes the allocation, taking a reference
637e02119d5SChris Mason * as required if the extent already exists or creating a new extent
638e02119d5SChris Mason * if it isn't in the extent allocation tree yet.
639e02119d5SChris Mason *
640e02119d5SChris Mason * The extent is inserted into the file, dropping any existing extents
641e02119d5SChris Mason * from the file that overlap the new one.
642e02119d5SChris Mason */
replay_one_extent(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)643e02119d5SChris Mason static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
644e02119d5SChris Mason struct btrfs_root *root,
645e02119d5SChris Mason struct btrfs_path *path,
646e02119d5SChris Mason struct extent_buffer *eb, int slot,
647e02119d5SChris Mason struct btrfs_key *key)
648e02119d5SChris Mason {
6495893dfb9SFilipe Manana struct btrfs_drop_extents_args drop_args = { 0 };
6500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
651e02119d5SChris Mason int found_type;
652e02119d5SChris Mason u64 extent_end;
653e02119d5SChris Mason u64 start = key->offset;
6544bc4bee4SJosef Bacik u64 nbytes = 0;
655e02119d5SChris Mason struct btrfs_file_extent_item *item;
656e02119d5SChris Mason struct inode *inode = NULL;
657e02119d5SChris Mason unsigned long size;
658e02119d5SChris Mason int ret = 0;
659e02119d5SChris Mason
660e02119d5SChris Mason item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
661e02119d5SChris Mason found_type = btrfs_file_extent_type(eb, item);
662e02119d5SChris Mason
663d899e052SYan Zheng if (found_type == BTRFS_FILE_EXTENT_REG ||
6644bc4bee4SJosef Bacik found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6654bc4bee4SJosef Bacik nbytes = btrfs_file_extent_num_bytes(eb, item);
6664bc4bee4SJosef Bacik extent_end = start + nbytes;
6674bc4bee4SJosef Bacik
6684bc4bee4SJosef Bacik /*
6694bc4bee4SJosef Bacik * We don't add to the inodes nbytes if we are prealloc or a
6704bc4bee4SJosef Bacik * hole.
6714bc4bee4SJosef Bacik */
6724bc4bee4SJosef Bacik if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6734bc4bee4SJosef Bacik nbytes = 0;
6744bc4bee4SJosef Bacik } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
675e41ca589SQu Wenruo size = btrfs_file_extent_ram_bytes(eb, item);
6764bc4bee4SJosef Bacik nbytes = btrfs_file_extent_ram_bytes(eb, item);
677da17066cSJeff Mahoney extent_end = ALIGN(start + size,
6780b246afaSJeff Mahoney fs_info->sectorsize);
679e02119d5SChris Mason } else {
680e02119d5SChris Mason ret = 0;
681e02119d5SChris Mason goto out;
682e02119d5SChris Mason }
683e02119d5SChris Mason
684e02119d5SChris Mason inode = read_one_inode(root, key->objectid);
685e02119d5SChris Mason if (!inode) {
686e02119d5SChris Mason ret = -EIO;
687e02119d5SChris Mason goto out;
688e02119d5SChris Mason }
689e02119d5SChris Mason
690e02119d5SChris Mason /*
691e02119d5SChris Mason * first check to see if we already have this extent in the
692e02119d5SChris Mason * file. This must be done before the btrfs_drop_extents run
693e02119d5SChris Mason * so we don't try to drop this extent.
694e02119d5SChris Mason */
695f85b7379SDavid Sterba ret = btrfs_lookup_file_extent(trans, root, path,
696f85b7379SDavid Sterba btrfs_ino(BTRFS_I(inode)), start, 0);
697e02119d5SChris Mason
698d899e052SYan Zheng if (ret == 0 &&
699d899e052SYan Zheng (found_type == BTRFS_FILE_EXTENT_REG ||
700d899e052SYan Zheng found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
701e02119d5SChris Mason struct btrfs_file_extent_item cmp1;
702e02119d5SChris Mason struct btrfs_file_extent_item cmp2;
703e02119d5SChris Mason struct btrfs_file_extent_item *existing;
704e02119d5SChris Mason struct extent_buffer *leaf;
705e02119d5SChris Mason
706e02119d5SChris Mason leaf = path->nodes[0];
707e02119d5SChris Mason existing = btrfs_item_ptr(leaf, path->slots[0],
708e02119d5SChris Mason struct btrfs_file_extent_item);
709e02119d5SChris Mason
710e02119d5SChris Mason read_extent_buffer(eb, &cmp1, (unsigned long)item,
711e02119d5SChris Mason sizeof(cmp1));
712e02119d5SChris Mason read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
713e02119d5SChris Mason sizeof(cmp2));
714e02119d5SChris Mason
715e02119d5SChris Mason /*
716e02119d5SChris Mason * we already have a pointer to this exact extent,
717e02119d5SChris Mason * we don't have to do anything
718e02119d5SChris Mason */
719e02119d5SChris Mason if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
720b3b4aa74SDavid Sterba btrfs_release_path(path);
721e02119d5SChris Mason goto out;
722e02119d5SChris Mason }
723e02119d5SChris Mason }
724b3b4aa74SDavid Sterba btrfs_release_path(path);
725e02119d5SChris Mason
726e02119d5SChris Mason /* drop any overlapping extents */
7275893dfb9SFilipe Manana drop_args.start = start;
7285893dfb9SFilipe Manana drop_args.end = extent_end;
7295893dfb9SFilipe Manana drop_args.drop_cache = true;
7305893dfb9SFilipe Manana ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args);
7313650860bSJosef Bacik if (ret)
7323650860bSJosef Bacik goto out;
733e02119d5SChris Mason
73407d400a6SYan Zheng if (found_type == BTRFS_FILE_EXTENT_REG ||
73507d400a6SYan Zheng found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7365d4f98a2SYan Zheng u64 offset;
73707d400a6SYan Zheng unsigned long dest_offset;
73807d400a6SYan Zheng struct btrfs_key ins;
73907d400a6SYan Zheng
7403168021cSFilipe Manana if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
7413168021cSFilipe Manana btrfs_fs_incompat(fs_info, NO_HOLES))
7423168021cSFilipe Manana goto update_inode;
7433168021cSFilipe Manana
74407d400a6SYan Zheng ret = btrfs_insert_empty_item(trans, root, path, key,
74507d400a6SYan Zheng sizeof(*item));
7463650860bSJosef Bacik if (ret)
7473650860bSJosef Bacik goto out;
74807d400a6SYan Zheng dest_offset = btrfs_item_ptr_offset(path->nodes[0],
74907d400a6SYan Zheng path->slots[0]);
75007d400a6SYan Zheng copy_extent_buffer(path->nodes[0], eb, dest_offset,
75107d400a6SYan Zheng (unsigned long)item, sizeof(*item));
75207d400a6SYan Zheng
75307d400a6SYan Zheng ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
75407d400a6SYan Zheng ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
75507d400a6SYan Zheng ins.type = BTRFS_EXTENT_ITEM_KEY;
7565d4f98a2SYan Zheng offset = key->offset - btrfs_file_extent_offset(eb, item);
75707d400a6SYan Zheng
758df2c95f3SQu Wenruo /*
759df2c95f3SQu Wenruo * Manually record dirty extent, as here we did a shallow
760df2c95f3SQu Wenruo * file extent item copy and skip normal backref update,
761df2c95f3SQu Wenruo * but modifying extent tree all by ourselves.
762df2c95f3SQu Wenruo * So need to manually record dirty extent for qgroup,
763df2c95f3SQu Wenruo * as the owner of the file extent changed from log tree
764df2c95f3SQu Wenruo * (doesn't affect qgroup) to fs/file tree(affects qgroup)
765df2c95f3SQu Wenruo */
766a95f3aafSLu Fengqi ret = btrfs_qgroup_trace_extent(trans,
767df2c95f3SQu Wenruo btrfs_file_extent_disk_bytenr(eb, item),
768e2896e79SDavid Sterba btrfs_file_extent_disk_num_bytes(eb, item));
769df2c95f3SQu Wenruo if (ret < 0)
770df2c95f3SQu Wenruo goto out;
771df2c95f3SQu Wenruo
77207d400a6SYan Zheng if (ins.objectid > 0) {
77382fa113fSQu Wenruo struct btrfs_ref ref = { 0 };
77407d400a6SYan Zheng u64 csum_start;
77507d400a6SYan Zheng u64 csum_end;
77607d400a6SYan Zheng LIST_HEAD(ordered_sums);
77782fa113fSQu Wenruo
77807d400a6SYan Zheng /*
77907d400a6SYan Zheng * is this extent already allocated in the extent
78007d400a6SYan Zheng * allocation tree? If so, just add a reference
78107d400a6SYan Zheng */
7822ff7e61eSJeff Mahoney ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
78307d400a6SYan Zheng ins.offset);
7843736127aSMarcos Paulo de Souza if (ret < 0) {
7853736127aSMarcos Paulo de Souza goto out;
7863736127aSMarcos Paulo de Souza } else if (ret == 0) {
78782fa113fSQu Wenruo btrfs_init_generic_ref(&ref,
78882fa113fSQu Wenruo BTRFS_ADD_DELAYED_REF,
78982fa113fSQu Wenruo ins.objectid, ins.offset, 0);
79082fa113fSQu Wenruo btrfs_init_data_ref(&ref,
79182fa113fSQu Wenruo root->root_key.objectid,
792f42c5da6SNikolay Borisov key->objectid, offset, 0, false);
79382fa113fSQu Wenruo ret = btrfs_inc_extent_ref(trans, &ref);
794b50c6e25SJosef Bacik if (ret)
795b50c6e25SJosef Bacik goto out;
79607d400a6SYan Zheng } else {
79707d400a6SYan Zheng /*
79807d400a6SYan Zheng * insert the extent pointer in the extent
79907d400a6SYan Zheng * allocation tree
80007d400a6SYan Zheng */
8015d4f98a2SYan Zheng ret = btrfs_alloc_logged_file_extent(trans,
8022ff7e61eSJeff Mahoney root->root_key.objectid,
8035d4f98a2SYan Zheng key->objectid, offset, &ins);
804b50c6e25SJosef Bacik if (ret)
805b50c6e25SJosef Bacik goto out;
80607d400a6SYan Zheng }
807b3b4aa74SDavid Sterba btrfs_release_path(path);
80807d400a6SYan Zheng
80907d400a6SYan Zheng if (btrfs_file_extent_compression(eb, item)) {
81007d400a6SYan Zheng csum_start = ins.objectid;
81107d400a6SYan Zheng csum_end = csum_start + ins.offset;
81207d400a6SYan Zheng } else {
81307d400a6SYan Zheng csum_start = ins.objectid +
81407d400a6SYan Zheng btrfs_file_extent_offset(eb, item);
81507d400a6SYan Zheng csum_end = csum_start +
81607d400a6SYan Zheng btrfs_file_extent_num_bytes(eb, item);
81707d400a6SYan Zheng }
81807d400a6SYan Zheng
81997e38239SQu Wenruo ret = btrfs_lookup_csums_list(root->log_root,
82007d400a6SYan Zheng csum_start, csum_end - 1,
82126ce9114SJosef Bacik &ordered_sums, 0, false);
8223650860bSJosef Bacik if (ret)
8233650860bSJosef Bacik goto out;
824b84b8390SFilipe Manana /*
825b84b8390SFilipe Manana * Now delete all existing cums in the csum root that
826b84b8390SFilipe Manana * cover our range. We do this because we can have an
827b84b8390SFilipe Manana * extent that is completely referenced by one file
828b84b8390SFilipe Manana * extent item and partially referenced by another
829b84b8390SFilipe Manana * file extent item (like after using the clone or
830b84b8390SFilipe Manana * extent_same ioctls). In this case if we end up doing
831b84b8390SFilipe Manana * the replay of the one that partially references the
832b84b8390SFilipe Manana * extent first, and we do not do the csum deletion
833b84b8390SFilipe Manana * below, we can get 2 csum items in the csum tree that
834b84b8390SFilipe Manana * overlap each other. For example, imagine our log has
835b84b8390SFilipe Manana * the two following file extent items:
836b84b8390SFilipe Manana *
837b84b8390SFilipe Manana * key (257 EXTENT_DATA 409600)
838b84b8390SFilipe Manana * extent data disk byte 12845056 nr 102400
839b84b8390SFilipe Manana * extent data offset 20480 nr 20480 ram 102400
840b84b8390SFilipe Manana *
841b84b8390SFilipe Manana * key (257 EXTENT_DATA 819200)
842b84b8390SFilipe Manana * extent data disk byte 12845056 nr 102400
843b84b8390SFilipe Manana * extent data offset 0 nr 102400 ram 102400
844b84b8390SFilipe Manana *
845b84b8390SFilipe Manana * Where the second one fully references the 100K extent
846b84b8390SFilipe Manana * that starts at disk byte 12845056, and the log tree
847b84b8390SFilipe Manana * has a single csum item that covers the entire range
848b84b8390SFilipe Manana * of the extent:
849b84b8390SFilipe Manana *
850b84b8390SFilipe Manana * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
851b84b8390SFilipe Manana *
852b84b8390SFilipe Manana * After the first file extent item is replayed, the
853b84b8390SFilipe Manana * csum tree gets the following csum item:
854b84b8390SFilipe Manana *
855b84b8390SFilipe Manana * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
856b84b8390SFilipe Manana *
857b84b8390SFilipe Manana * Which covers the 20K sub-range starting at offset 20K
858b84b8390SFilipe Manana * of our extent. Now when we replay the second file
859b84b8390SFilipe Manana * extent item, if we do not delete existing csum items
860b84b8390SFilipe Manana * that cover any of its blocks, we end up getting two
861b84b8390SFilipe Manana * csum items in our csum tree that overlap each other:
862b84b8390SFilipe Manana *
863b84b8390SFilipe Manana * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
864b84b8390SFilipe Manana * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
865b84b8390SFilipe Manana *
866b84b8390SFilipe Manana * Which is a problem, because after this anyone trying
867b84b8390SFilipe Manana * to lookup up for the checksum of any block of our
868b84b8390SFilipe Manana * extent starting at an offset of 40K or higher, will
869b84b8390SFilipe Manana * end up looking at the second csum item only, which
870b84b8390SFilipe Manana * does not contain the checksum for any block starting
871b84b8390SFilipe Manana * at offset 40K or higher of our extent.
872b84b8390SFilipe Manana */
87307d400a6SYan Zheng while (!list_empty(&ordered_sums)) {
87407d400a6SYan Zheng struct btrfs_ordered_sum *sums;
875fc28b25eSJosef Bacik struct btrfs_root *csum_root;
876fc28b25eSJosef Bacik
87707d400a6SYan Zheng sums = list_entry(ordered_sums.next,
87807d400a6SYan Zheng struct btrfs_ordered_sum,
87907d400a6SYan Zheng list);
880fc28b25eSJosef Bacik csum_root = btrfs_csum_root(fs_info,
8815cfe76f8SChristoph Hellwig sums->logical);
8823650860bSJosef Bacik if (!ret)
883fc28b25eSJosef Bacik ret = btrfs_del_csums(trans, csum_root,
8845cfe76f8SChristoph Hellwig sums->logical,
885b84b8390SFilipe Manana sums->len);
886b84b8390SFilipe Manana if (!ret)
88707d400a6SYan Zheng ret = btrfs_csum_file_blocks(trans,
888fc28b25eSJosef Bacik csum_root,
889fc28b25eSJosef Bacik sums);
89007d400a6SYan Zheng list_del(&sums->list);
89107d400a6SYan Zheng kfree(sums);
89207d400a6SYan Zheng }
8933650860bSJosef Bacik if (ret)
8943650860bSJosef Bacik goto out;
89507d400a6SYan Zheng } else {
896b3b4aa74SDavid Sterba btrfs_release_path(path);
89707d400a6SYan Zheng }
89807d400a6SYan Zheng } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
89907d400a6SYan Zheng /* inline extents are easy, we just overwrite them */
900e02119d5SChris Mason ret = overwrite_item(trans, root, path, eb, slot, key);
9013650860bSJosef Bacik if (ret)
9023650860bSJosef Bacik goto out;
90307d400a6SYan Zheng }
904e02119d5SChris Mason
9059ddc959eSJosef Bacik ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start,
9069ddc959eSJosef Bacik extent_end - start);
9079ddc959eSJosef Bacik if (ret)
9089ddc959eSJosef Bacik goto out;
9099ddc959eSJosef Bacik
9103168021cSFilipe Manana update_inode:
9112766ff61SFilipe Manana btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found);
9129a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
913e02119d5SChris Mason out:
914e02119d5SChris Mason iput(inode);
915e02119d5SChris Mason return ret;
916e02119d5SChris Mason }
917e02119d5SChris Mason
unlink_inode_for_log_replay(struct btrfs_trans_handle * trans,struct btrfs_inode * dir,struct btrfs_inode * inode,const struct fscrypt_str * name)918313ab753SFilipe Manana static int unlink_inode_for_log_replay(struct btrfs_trans_handle *trans,
919313ab753SFilipe Manana struct btrfs_inode *dir,
920313ab753SFilipe Manana struct btrfs_inode *inode,
9216db75318SSweet Tea Dorminy const struct fscrypt_str *name)
922313ab753SFilipe Manana {
923313ab753SFilipe Manana int ret;
924313ab753SFilipe Manana
925e43eec81SSweet Tea Dorminy ret = btrfs_unlink_inode(trans, dir, inode, name);
926313ab753SFilipe Manana if (ret)
927313ab753SFilipe Manana return ret;
928313ab753SFilipe Manana /*
929313ab753SFilipe Manana * Whenever we need to check if a name exists or not, we check the
930313ab753SFilipe Manana * fs/subvolume tree. So after an unlink we must run delayed items, so
931313ab753SFilipe Manana * that future checks for a name during log replay see that the name
932313ab753SFilipe Manana * does not exists anymore.
933313ab753SFilipe Manana */
934313ab753SFilipe Manana return btrfs_run_delayed_items(trans);
935313ab753SFilipe Manana }
936313ab753SFilipe Manana
937e02119d5SChris Mason /*
938e02119d5SChris Mason * when cleaning up conflicts between the directory names in the
939e02119d5SChris Mason * subvolume, directory names in the log and directory names in the
940e02119d5SChris Mason * inode back references, we may have to unlink inodes from directories.
941e02119d5SChris Mason *
942e02119d5SChris Mason * This is a helper function to do the unlink of a specific directory
943e02119d5SChris Mason * item
944e02119d5SChris Mason */
drop_one_dir_item(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_inode * dir,struct btrfs_dir_item * di)945e02119d5SChris Mason static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
946e02119d5SChris Mason struct btrfs_path *path,
947207e7d92SNikolay Borisov struct btrfs_inode *dir,
948e02119d5SChris Mason struct btrfs_dir_item *di)
949e02119d5SChris Mason {
9509798ba24SFilipe Manana struct btrfs_root *root = dir->root;
951e02119d5SChris Mason struct inode *inode;
9526db75318SSweet Tea Dorminy struct fscrypt_str name;
953e02119d5SChris Mason struct extent_buffer *leaf;
954e02119d5SChris Mason struct btrfs_key location;
955e02119d5SChris Mason int ret;
956e02119d5SChris Mason
957e02119d5SChris Mason leaf = path->nodes[0];
958e02119d5SChris Mason
959e02119d5SChris Mason btrfs_dir_item_key_to_cpu(leaf, di, &location);
960e43eec81SSweet Tea Dorminy ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name);
961e43eec81SSweet Tea Dorminy if (ret)
9622a29edc6Sliubo return -ENOMEM;
9632a29edc6Sliubo
964b3b4aa74SDavid Sterba btrfs_release_path(path);
965e02119d5SChris Mason
966e02119d5SChris Mason inode = read_one_inode(root, location.objectid);
967c00e9493STsutomu Itoh if (!inode) {
9683650860bSJosef Bacik ret = -EIO;
9693650860bSJosef Bacik goto out;
970c00e9493STsutomu Itoh }
971e02119d5SChris Mason
972ec051c0fSYan Zheng ret = link_to_fixup_dir(trans, root, path, location.objectid);
9733650860bSJosef Bacik if (ret)
9743650860bSJosef Bacik goto out;
97512fcfd22SChris Mason
976e43eec81SSweet Tea Dorminy ret = unlink_inode_for_log_replay(trans, dir, BTRFS_I(inode), &name);
9773650860bSJosef Bacik out:
978e43eec81SSweet Tea Dorminy kfree(name.name);
9793650860bSJosef Bacik iput(inode);
980e02119d5SChris Mason return ret;
981e02119d5SChris Mason }
982e02119d5SChris Mason
983e02119d5SChris Mason /*
98477a5b9e3SFilipe Manana * See if a given name and sequence number found in an inode back reference are
98577a5b9e3SFilipe Manana * already in a directory and correctly point to this inode.
98677a5b9e3SFilipe Manana *
98777a5b9e3SFilipe Manana * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
98877a5b9e3SFilipe Manana * exists.
989e02119d5SChris Mason */
inode_in_dir(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,u64 objectid,u64 index,struct fscrypt_str * name)990e02119d5SChris Mason static noinline int inode_in_dir(struct btrfs_root *root,
991e02119d5SChris Mason struct btrfs_path *path,
992e02119d5SChris Mason u64 dirid, u64 objectid, u64 index,
9936db75318SSweet Tea Dorminy struct fscrypt_str *name)
994e02119d5SChris Mason {
995e02119d5SChris Mason struct btrfs_dir_item *di;
996e02119d5SChris Mason struct btrfs_key location;
99777a5b9e3SFilipe Manana int ret = 0;
998e02119d5SChris Mason
999e02119d5SChris Mason di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
1000e43eec81SSweet Tea Dorminy index, name, 0);
100177a5b9e3SFilipe Manana if (IS_ERR(di)) {
100277a5b9e3SFilipe Manana ret = PTR_ERR(di);
100377a5b9e3SFilipe Manana goto out;
100477a5b9e3SFilipe Manana } else if (di) {
1005e02119d5SChris Mason btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1006e02119d5SChris Mason if (location.objectid != objectid)
1007e02119d5SChris Mason goto out;
100877a5b9e3SFilipe Manana } else {
1009e02119d5SChris Mason goto out;
101077a5b9e3SFilipe Manana }
1011e02119d5SChris Mason
101277a5b9e3SFilipe Manana btrfs_release_path(path);
1013e43eec81SSweet Tea Dorminy di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0);
101477a5b9e3SFilipe Manana if (IS_ERR(di)) {
101577a5b9e3SFilipe Manana ret = PTR_ERR(di);
101677a5b9e3SFilipe Manana goto out;
101777a5b9e3SFilipe Manana } else if (di) {
1018e02119d5SChris Mason btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
101977a5b9e3SFilipe Manana if (location.objectid == objectid)
102077a5b9e3SFilipe Manana ret = 1;
102177a5b9e3SFilipe Manana }
1022e02119d5SChris Mason out:
1023b3b4aa74SDavid Sterba btrfs_release_path(path);
102477a5b9e3SFilipe Manana return ret;
1025e02119d5SChris Mason }
1026e02119d5SChris Mason
1027e02119d5SChris Mason /*
1028e02119d5SChris Mason * helper function to check a log tree for a named back reference in
1029e02119d5SChris Mason * an inode. This is used to decide if a back reference that is
1030e02119d5SChris Mason * found in the subvolume conflicts with what we find in the log.
1031e02119d5SChris Mason *
1032e02119d5SChris Mason * inode backreferences may have multiple refs in a single item,
1033e02119d5SChris Mason * during replay we process one reference at a time, and we don't
1034e02119d5SChris Mason * want to delete valid links to a file from the subvolume if that
1035e02119d5SChris Mason * link is also in the log.
1036e02119d5SChris Mason */
backref_in_log(struct btrfs_root * log,struct btrfs_key * key,u64 ref_objectid,const struct fscrypt_str * name)1037e02119d5SChris Mason static noinline int backref_in_log(struct btrfs_root *log,
1038e02119d5SChris Mason struct btrfs_key *key,
1039f186373fSMark Fasheh u64 ref_objectid,
10406db75318SSweet Tea Dorminy const struct fscrypt_str *name)
1041e02119d5SChris Mason {
1042e02119d5SChris Mason struct btrfs_path *path;
1043e02119d5SChris Mason int ret;
1044e02119d5SChris Mason
1045e02119d5SChris Mason path = btrfs_alloc_path();
10462a29edc6Sliubo if (!path)
10472a29edc6Sliubo return -ENOMEM;
10482a29edc6Sliubo
1049e02119d5SChris Mason ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
1050d3316c82SNikolay Borisov if (ret < 0) {
1051d3316c82SNikolay Borisov goto out;
1052d3316c82SNikolay Borisov } else if (ret == 1) {
105389cbf5f6SNikolay Borisov ret = 0;
1054e02119d5SChris Mason goto out;
105589cbf5f6SNikolay Borisov }
1056e02119d5SChris Mason
105789cbf5f6SNikolay Borisov if (key->type == BTRFS_INODE_EXTREF_KEY)
105889cbf5f6SNikolay Borisov ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
10591f250e92SFilipe Manana path->slots[0],
1060e43eec81SSweet Tea Dorminy ref_objectid, name);
106189cbf5f6SNikolay Borisov else
106289cbf5f6SNikolay Borisov ret = !!btrfs_find_name_in_backref(path->nodes[0],
1063e43eec81SSweet Tea Dorminy path->slots[0], name);
1064e02119d5SChris Mason out:
1065e02119d5SChris Mason btrfs_free_path(path);
106689cbf5f6SNikolay Borisov return ret;
1067e02119d5SChris Mason }
1068e02119d5SChris Mason
__add_inode_ref(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_root * log_root,struct btrfs_inode * dir,struct btrfs_inode * inode,u64 inode_objectid,u64 parent_objectid,u64 ref_index,struct fscrypt_str * name)10695a1d7843SJan Schmidt static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
10705a1d7843SJan Schmidt struct btrfs_root *root,
10715a1d7843SJan Schmidt struct btrfs_path *path,
10725a1d7843SJan Schmidt struct btrfs_root *log_root,
107394c91a1fSNikolay Borisov struct btrfs_inode *dir,
107494c91a1fSNikolay Borisov struct btrfs_inode *inode,
1075f186373fSMark Fasheh u64 inode_objectid, u64 parent_objectid,
10766db75318SSweet Tea Dorminy u64 ref_index, struct fscrypt_str *name)
10775a1d7843SJan Schmidt {
10785a1d7843SJan Schmidt int ret;
1079f186373fSMark Fasheh struct extent_buffer *leaf;
1080f186373fSMark Fasheh struct btrfs_dir_item *di;
1081f186373fSMark Fasheh struct btrfs_key search_key;
1082f186373fSMark Fasheh struct btrfs_inode_extref *extref;
1083f186373fSMark Fasheh
1084f186373fSMark Fasheh again:
1085f186373fSMark Fasheh /* Search old style refs */
1086f186373fSMark Fasheh search_key.objectid = inode_objectid;
1087f186373fSMark Fasheh search_key.type = BTRFS_INODE_REF_KEY;
1088f186373fSMark Fasheh search_key.offset = parent_objectid;
1089f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1090f186373fSMark Fasheh if (ret == 0) {
10915a1d7843SJan Schmidt struct btrfs_inode_ref *victim_ref;
10925a1d7843SJan Schmidt unsigned long ptr;
10935a1d7843SJan Schmidt unsigned long ptr_end;
1094f186373fSMark Fasheh
1095f186373fSMark Fasheh leaf = path->nodes[0];
10965a1d7843SJan Schmidt
10975a1d7843SJan Schmidt /* are we trying to overwrite a back ref for the root directory
10985a1d7843SJan Schmidt * if so, just jump out, we're done
10995a1d7843SJan Schmidt */
1100f186373fSMark Fasheh if (search_key.objectid == search_key.offset)
11015a1d7843SJan Schmidt return 1;
11025a1d7843SJan Schmidt
11035a1d7843SJan Schmidt /* check all the names in this back reference to see
11045a1d7843SJan Schmidt * if they are in the log. if so, we allow them to stay
11055a1d7843SJan Schmidt * otherwise they must be unlinked as a conflict
11065a1d7843SJan Schmidt */
11075a1d7843SJan Schmidt ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
11083212fa14SJosef Bacik ptr_end = ptr + btrfs_item_size(leaf, path->slots[0]);
11095a1d7843SJan Schmidt while (ptr < ptr_end) {
11106db75318SSweet Tea Dorminy struct fscrypt_str victim_name;
11115a1d7843SJan Schmidt
1112e43eec81SSweet Tea Dorminy victim_ref = (struct btrfs_inode_ref *)ptr;
1113e43eec81SSweet Tea Dorminy ret = read_alloc_one_name(leaf, (victim_ref + 1),
1114e43eec81SSweet Tea Dorminy btrfs_inode_ref_name_len(leaf, victim_ref),
1115e43eec81SSweet Tea Dorminy &victim_name);
1116e43eec81SSweet Tea Dorminy if (ret)
1117e43eec81SSweet Tea Dorminy return ret;
11185a1d7843SJan Schmidt
1119d3316c82SNikolay Borisov ret = backref_in_log(log_root, &search_key,
1120e43eec81SSweet Tea Dorminy parent_objectid, &victim_name);
1121d3316c82SNikolay Borisov if (ret < 0) {
1122e43eec81SSweet Tea Dorminy kfree(victim_name.name);
1123d3316c82SNikolay Borisov return ret;
1124d3316c82SNikolay Borisov } else if (!ret) {
112594c91a1fSNikolay Borisov inc_nlink(&inode->vfs_inode);
11265a1d7843SJan Schmidt btrfs_release_path(path);
11275a1d7843SJan Schmidt
1128313ab753SFilipe Manana ret = unlink_inode_for_log_replay(trans, dir, inode,
1129e43eec81SSweet Tea Dorminy &victim_name);
1130e43eec81SSweet Tea Dorminy kfree(victim_name.name);
11313650860bSJosef Bacik if (ret)
11323650860bSJosef Bacik return ret;
1133f186373fSMark Fasheh goto again;
11345a1d7843SJan Schmidt }
1135e43eec81SSweet Tea Dorminy kfree(victim_name.name);
1136f186373fSMark Fasheh
1137e43eec81SSweet Tea Dorminy ptr = (unsigned long)(victim_ref + 1) + victim_name.len;
11385a1d7843SJan Schmidt }
11395a1d7843SJan Schmidt }
11405a1d7843SJan Schmidt btrfs_release_path(path);
11415a1d7843SJan Schmidt
1142f186373fSMark Fasheh /* Same search but for extended refs */
1143e43eec81SSweet Tea Dorminy extref = btrfs_lookup_inode_extref(NULL, root, path, name,
1144f186373fSMark Fasheh inode_objectid, parent_objectid, 0,
1145f186373fSMark Fasheh 0);
11467a6b75b7SFilipe Manana if (IS_ERR(extref)) {
11477a6b75b7SFilipe Manana return PTR_ERR(extref);
11487a6b75b7SFilipe Manana } else if (extref) {
1149f186373fSMark Fasheh u32 item_size;
1150f186373fSMark Fasheh u32 cur_offset = 0;
1151f186373fSMark Fasheh unsigned long base;
1152f186373fSMark Fasheh struct inode *victim_parent;
1153f186373fSMark Fasheh
1154f186373fSMark Fasheh leaf = path->nodes[0];
1155f186373fSMark Fasheh
11563212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]);
1157f186373fSMark Fasheh base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1158f186373fSMark Fasheh
1159f186373fSMark Fasheh while (cur_offset < item_size) {
11606db75318SSweet Tea Dorminy struct fscrypt_str victim_name;
1161f186373fSMark Fasheh
1162e43eec81SSweet Tea Dorminy extref = (struct btrfs_inode_extref *)(base + cur_offset);
1163f186373fSMark Fasheh
1164f186373fSMark Fasheh if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1165f186373fSMark Fasheh goto next;
1166f186373fSMark Fasheh
1167e43eec81SSweet Tea Dorminy ret = read_alloc_one_name(leaf, &extref->name,
1168e43eec81SSweet Tea Dorminy btrfs_inode_extref_name_len(leaf, extref),
1169e43eec81SSweet Tea Dorminy &victim_name);
1170e43eec81SSweet Tea Dorminy if (ret)
1171e43eec81SSweet Tea Dorminy return ret;
1172f186373fSMark Fasheh
1173f186373fSMark Fasheh search_key.objectid = inode_objectid;
1174f186373fSMark Fasheh search_key.type = BTRFS_INODE_EXTREF_KEY;
1175f186373fSMark Fasheh search_key.offset = btrfs_extref_hash(parent_objectid,
1176e43eec81SSweet Tea Dorminy victim_name.name,
1177e43eec81SSweet Tea Dorminy victim_name.len);
1178d3316c82SNikolay Borisov ret = backref_in_log(log_root, &search_key,
1179e43eec81SSweet Tea Dorminy parent_objectid, &victim_name);
1180d3316c82SNikolay Borisov if (ret < 0) {
1181e43eec81SSweet Tea Dorminy kfree(victim_name.name);
1182d3316c82SNikolay Borisov return ret;
1183d3316c82SNikolay Borisov } else if (!ret) {
1184f186373fSMark Fasheh ret = -ENOENT;
1185f186373fSMark Fasheh victim_parent = read_one_inode(root,
1186f186373fSMark Fasheh parent_objectid);
1187f186373fSMark Fasheh if (victim_parent) {
118894c91a1fSNikolay Borisov inc_nlink(&inode->vfs_inode);
1189f186373fSMark Fasheh btrfs_release_path(path);
1190f186373fSMark Fasheh
1191313ab753SFilipe Manana ret = unlink_inode_for_log_replay(trans,
11924ec5934eSNikolay Borisov BTRFS_I(victim_parent),
1193e43eec81SSweet Tea Dorminy inode, &victim_name);
1194f186373fSMark Fasheh }
1195f186373fSMark Fasheh iput(victim_parent);
1196e43eec81SSweet Tea Dorminy kfree(victim_name.name);
11973650860bSJosef Bacik if (ret)
11983650860bSJosef Bacik return ret;
1199f186373fSMark Fasheh goto again;
1200f186373fSMark Fasheh }
1201e43eec81SSweet Tea Dorminy kfree(victim_name.name);
1202f186373fSMark Fasheh next:
1203e43eec81SSweet Tea Dorminy cur_offset += victim_name.len + sizeof(*extref);
1204f186373fSMark Fasheh }
1205f186373fSMark Fasheh }
1206f186373fSMark Fasheh btrfs_release_path(path);
1207f186373fSMark Fasheh
12085a1d7843SJan Schmidt /* look for a conflicting sequence number */
120994c91a1fSNikolay Borisov di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1210e43eec81SSweet Tea Dorminy ref_index, name, 0);
121152db7779SFilipe Manana if (IS_ERR(di)) {
121252db7779SFilipe Manana return PTR_ERR(di);
121352db7779SFilipe Manana } else if (di) {
12149798ba24SFilipe Manana ret = drop_one_dir_item(trans, path, dir, di);
12153650860bSJosef Bacik if (ret)
12163650860bSJosef Bacik return ret;
12175a1d7843SJan Schmidt }
12185a1d7843SJan Schmidt btrfs_release_path(path);
12195a1d7843SJan Schmidt
122052042d8eSAndrea Gelmini /* look for a conflicting name */
1221e43eec81SSweet Tea Dorminy di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), name, 0);
122252db7779SFilipe Manana if (IS_ERR(di)) {
122352db7779SFilipe Manana return PTR_ERR(di);
122452db7779SFilipe Manana } else if (di) {
12259798ba24SFilipe Manana ret = drop_one_dir_item(trans, path, dir, di);
12263650860bSJosef Bacik if (ret)
12273650860bSJosef Bacik return ret;
12285a1d7843SJan Schmidt }
12295a1d7843SJan Schmidt btrfs_release_path(path);
12305a1d7843SJan Schmidt
12315a1d7843SJan Schmidt return 0;
12325a1d7843SJan Schmidt }
1233e02119d5SChris Mason
extref_get_fields(struct extent_buffer * eb,unsigned long ref_ptr,struct fscrypt_str * name,u64 * index,u64 * parent_objectid)1234bae15d95SQu Wenruo static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
12356db75318SSweet Tea Dorminy struct fscrypt_str *name, u64 *index,
1236bae15d95SQu Wenruo u64 *parent_objectid)
1237f186373fSMark Fasheh {
1238f186373fSMark Fasheh struct btrfs_inode_extref *extref;
1239e43eec81SSweet Tea Dorminy int ret;
1240f186373fSMark Fasheh
1241f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ref_ptr;
1242f186373fSMark Fasheh
1243e43eec81SSweet Tea Dorminy ret = read_alloc_one_name(eb, &extref->name,
1244e43eec81SSweet Tea Dorminy btrfs_inode_extref_name_len(eb, extref), name);
1245e43eec81SSweet Tea Dorminy if (ret)
1246e43eec81SSweet Tea Dorminy return ret;
1247f186373fSMark Fasheh
12481f250e92SFilipe Manana if (index)
1249f186373fSMark Fasheh *index = btrfs_inode_extref_index(eb, extref);
1250f186373fSMark Fasheh if (parent_objectid)
1251f186373fSMark Fasheh *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1252f186373fSMark Fasheh
1253f186373fSMark Fasheh return 0;
1254f186373fSMark Fasheh }
1255f186373fSMark Fasheh
ref_get_fields(struct extent_buffer * eb,unsigned long ref_ptr,struct fscrypt_str * name,u64 * index)1256bae15d95SQu Wenruo static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
12576db75318SSweet Tea Dorminy struct fscrypt_str *name, u64 *index)
1258f186373fSMark Fasheh {
1259f186373fSMark Fasheh struct btrfs_inode_ref *ref;
1260e43eec81SSweet Tea Dorminy int ret;
1261f186373fSMark Fasheh
1262f186373fSMark Fasheh ref = (struct btrfs_inode_ref *)ref_ptr;
1263f186373fSMark Fasheh
1264e43eec81SSweet Tea Dorminy ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref),
1265e43eec81SSweet Tea Dorminy name);
1266e43eec81SSweet Tea Dorminy if (ret)
1267e43eec81SSweet Tea Dorminy return ret;
1268f186373fSMark Fasheh
12691f250e92SFilipe Manana if (index)
1270f186373fSMark Fasheh *index = btrfs_inode_ref_index(eb, ref);
1271f186373fSMark Fasheh
1272f186373fSMark Fasheh return 0;
1273f186373fSMark Fasheh }
1274f186373fSMark Fasheh
1275e02119d5SChris Mason /*
12761f250e92SFilipe Manana * Take an inode reference item from the log tree and iterate all names from the
12771f250e92SFilipe Manana * inode reference item in the subvolume tree with the same key (if it exists).
12781f250e92SFilipe Manana * For any name that is not in the inode reference item from the log tree, do a
12791f250e92SFilipe Manana * proper unlink of that name (that is, remove its entry from the inode
12801f250e92SFilipe Manana * reference item and both dir index keys).
12811f250e92SFilipe Manana */
unlink_old_inode_refs(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_inode * inode,struct extent_buffer * log_eb,int log_slot,struct btrfs_key * key)12821f250e92SFilipe Manana static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
12831f250e92SFilipe Manana struct btrfs_root *root,
12841f250e92SFilipe Manana struct btrfs_path *path,
12851f250e92SFilipe Manana struct btrfs_inode *inode,
12861f250e92SFilipe Manana struct extent_buffer *log_eb,
12871f250e92SFilipe Manana int log_slot,
12881f250e92SFilipe Manana struct btrfs_key *key)
12891f250e92SFilipe Manana {
12901f250e92SFilipe Manana int ret;
12911f250e92SFilipe Manana unsigned long ref_ptr;
12921f250e92SFilipe Manana unsigned long ref_end;
12931f250e92SFilipe Manana struct extent_buffer *eb;
12941f250e92SFilipe Manana
12951f250e92SFilipe Manana again:
12961f250e92SFilipe Manana btrfs_release_path(path);
12971f250e92SFilipe Manana ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
12981f250e92SFilipe Manana if (ret > 0) {
12991f250e92SFilipe Manana ret = 0;
13001f250e92SFilipe Manana goto out;
13011f250e92SFilipe Manana }
13021f250e92SFilipe Manana if (ret < 0)
13031f250e92SFilipe Manana goto out;
13041f250e92SFilipe Manana
13051f250e92SFilipe Manana eb = path->nodes[0];
13061f250e92SFilipe Manana ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
13073212fa14SJosef Bacik ref_end = ref_ptr + btrfs_item_size(eb, path->slots[0]);
13081f250e92SFilipe Manana while (ref_ptr < ref_end) {
13096db75318SSweet Tea Dorminy struct fscrypt_str name;
13101f250e92SFilipe Manana u64 parent_id;
13111f250e92SFilipe Manana
13121f250e92SFilipe Manana if (key->type == BTRFS_INODE_EXTREF_KEY) {
1313e43eec81SSweet Tea Dorminy ret = extref_get_fields(eb, ref_ptr, &name,
13141f250e92SFilipe Manana NULL, &parent_id);
13151f250e92SFilipe Manana } else {
13161f250e92SFilipe Manana parent_id = key->offset;
1317e43eec81SSweet Tea Dorminy ret = ref_get_fields(eb, ref_ptr, &name, NULL);
13181f250e92SFilipe Manana }
13191f250e92SFilipe Manana if (ret)
13201f250e92SFilipe Manana goto out;
13211f250e92SFilipe Manana
13221f250e92SFilipe Manana if (key->type == BTRFS_INODE_EXTREF_KEY)
13236ff49c6aSNikolay Borisov ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
1324e43eec81SSweet Tea Dorminy parent_id, &name);
13251f250e92SFilipe Manana else
1326e43eec81SSweet Tea Dorminy ret = !!btrfs_find_name_in_backref(log_eb, log_slot, &name);
13271f250e92SFilipe Manana
13281f250e92SFilipe Manana if (!ret) {
13291f250e92SFilipe Manana struct inode *dir;
13301f250e92SFilipe Manana
13311f250e92SFilipe Manana btrfs_release_path(path);
13321f250e92SFilipe Manana dir = read_one_inode(root, parent_id);
13331f250e92SFilipe Manana if (!dir) {
13341f250e92SFilipe Manana ret = -ENOENT;
1335e43eec81SSweet Tea Dorminy kfree(name.name);
13361f250e92SFilipe Manana goto out;
13371f250e92SFilipe Manana }
1338313ab753SFilipe Manana ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir),
1339e43eec81SSweet Tea Dorminy inode, &name);
1340e43eec81SSweet Tea Dorminy kfree(name.name);
13411f250e92SFilipe Manana iput(dir);
13421f250e92SFilipe Manana if (ret)
13431f250e92SFilipe Manana goto out;
13441f250e92SFilipe Manana goto again;
13451f250e92SFilipe Manana }
13461f250e92SFilipe Manana
1347e43eec81SSweet Tea Dorminy kfree(name.name);
1348e43eec81SSweet Tea Dorminy ref_ptr += name.len;
13491f250e92SFilipe Manana if (key->type == BTRFS_INODE_EXTREF_KEY)
13501f250e92SFilipe Manana ref_ptr += sizeof(struct btrfs_inode_extref);
13511f250e92SFilipe Manana else
13521f250e92SFilipe Manana ref_ptr += sizeof(struct btrfs_inode_ref);
13531f250e92SFilipe Manana }
13541f250e92SFilipe Manana ret = 0;
13551f250e92SFilipe Manana out:
13561f250e92SFilipe Manana btrfs_release_path(path);
13571f250e92SFilipe Manana return ret;
13581f250e92SFilipe Manana }
13591f250e92SFilipe Manana
13601f250e92SFilipe Manana /*
1361e02119d5SChris Mason * replay one inode back reference item found in the log tree.
1362e02119d5SChris Mason * eb, slot and key refer to the buffer and key found in the log tree.
1363e02119d5SChris Mason * root is the destination we are replaying into, and path is for temp
1364e02119d5SChris Mason * use by this function. (it should be released on return).
1365e02119d5SChris Mason */
add_inode_ref(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)1366e02119d5SChris Mason static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1367e02119d5SChris Mason struct btrfs_root *root,
1368e02119d5SChris Mason struct btrfs_root *log,
1369e02119d5SChris Mason struct btrfs_path *path,
1370e02119d5SChris Mason struct extent_buffer *eb, int slot,
1371e02119d5SChris Mason struct btrfs_key *key)
1372e02119d5SChris Mason {
137303b2f08bSGeyslan G. Bem struct inode *dir = NULL;
137403b2f08bSGeyslan G. Bem struct inode *inode = NULL;
1375e02119d5SChris Mason unsigned long ref_ptr;
1376e02119d5SChris Mason unsigned long ref_end;
1377e11ce03bSRoi Martin struct fscrypt_str name = { 0 };
137834f3e4f2Sliubo int ret;
1379f186373fSMark Fasheh int log_ref_ver = 0;
1380f186373fSMark Fasheh u64 parent_objectid;
1381f186373fSMark Fasheh u64 inode_objectid;
1382f46dbe3dSChris Mason u64 ref_index = 0;
1383f186373fSMark Fasheh int ref_struct_size;
1384f186373fSMark Fasheh
1385f186373fSMark Fasheh ref_ptr = btrfs_item_ptr_offset(eb, slot);
13863212fa14SJosef Bacik ref_end = ref_ptr + btrfs_item_size(eb, slot);
1387f186373fSMark Fasheh
1388f186373fSMark Fasheh if (key->type == BTRFS_INODE_EXTREF_KEY) {
1389f186373fSMark Fasheh struct btrfs_inode_extref *r;
1390f186373fSMark Fasheh
1391f186373fSMark Fasheh ref_struct_size = sizeof(struct btrfs_inode_extref);
1392f186373fSMark Fasheh log_ref_ver = 1;
1393f186373fSMark Fasheh r = (struct btrfs_inode_extref *)ref_ptr;
1394f186373fSMark Fasheh parent_objectid = btrfs_inode_extref_parent(eb, r);
1395f186373fSMark Fasheh } else {
1396f186373fSMark Fasheh ref_struct_size = sizeof(struct btrfs_inode_ref);
1397f186373fSMark Fasheh parent_objectid = key->offset;
1398f186373fSMark Fasheh }
1399f186373fSMark Fasheh inode_objectid = key->objectid;
1400e02119d5SChris Mason
1401e02119d5SChris Mason /*
1402e02119d5SChris Mason * it is possible that we didn't log all the parent directories
1403e02119d5SChris Mason * for a given inode. If we don't find the dir, just don't
1404e02119d5SChris Mason * copy the back ref in. The link count fixup code will take
1405e02119d5SChris Mason * care of the rest
1406e02119d5SChris Mason */
1407f186373fSMark Fasheh dir = read_one_inode(root, parent_objectid);
140803b2f08bSGeyslan G. Bem if (!dir) {
140903b2f08bSGeyslan G. Bem ret = -ENOENT;
141003b2f08bSGeyslan G. Bem goto out;
141103b2f08bSGeyslan G. Bem }
1412e02119d5SChris Mason
1413f186373fSMark Fasheh inode = read_one_inode(root, inode_objectid);
1414c00e9493STsutomu Itoh if (!inode) {
141503b2f08bSGeyslan G. Bem ret = -EIO;
141603b2f08bSGeyslan G. Bem goto out;
1417c00e9493STsutomu Itoh }
1418e02119d5SChris Mason
14195a1d7843SJan Schmidt while (ref_ptr < ref_end) {
1420f186373fSMark Fasheh if (log_ref_ver) {
1421e43eec81SSweet Tea Dorminy ret = extref_get_fields(eb, ref_ptr, &name,
1422bae15d95SQu Wenruo &ref_index, &parent_objectid);
1423f186373fSMark Fasheh /*
1424f186373fSMark Fasheh * parent object can change from one array
1425f186373fSMark Fasheh * item to another.
1426f186373fSMark Fasheh */
1427f186373fSMark Fasheh if (!dir)
1428f186373fSMark Fasheh dir = read_one_inode(root, parent_objectid);
142903b2f08bSGeyslan G. Bem if (!dir) {
143003b2f08bSGeyslan G. Bem ret = -ENOENT;
143103b2f08bSGeyslan G. Bem goto out;
143203b2f08bSGeyslan G. Bem }
1433f186373fSMark Fasheh } else {
1434e43eec81SSweet Tea Dorminy ret = ref_get_fields(eb, ref_ptr, &name, &ref_index);
1435f186373fSMark Fasheh }
1436f186373fSMark Fasheh if (ret)
143703b2f08bSGeyslan G. Bem goto out;
1438e02119d5SChris Mason
143977a5b9e3SFilipe Manana ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1440e43eec81SSweet Tea Dorminy btrfs_ino(BTRFS_I(inode)), ref_index, &name);
144177a5b9e3SFilipe Manana if (ret < 0) {
144277a5b9e3SFilipe Manana goto out;
144377a5b9e3SFilipe Manana } else if (ret == 0) {
14445a1d7843SJan Schmidt /*
14455a1d7843SJan Schmidt * look for a conflicting back reference in the
14465a1d7843SJan Schmidt * metadata. if we find one we have to unlink that name
14475a1d7843SJan Schmidt * of the file before we add our new link. Later on, we
14485a1d7843SJan Schmidt * overwrite any existing back reference, and we don't
14495a1d7843SJan Schmidt * want to create dangling pointers in the directory.
14505a1d7843SJan Schmidt */
14515a1d7843SJan Schmidt ret = __add_inode_ref(trans, root, path, log,
14527059c658SFilipe Manana BTRFS_I(dir), BTRFS_I(inode),
14537059c658SFilipe Manana inode_objectid, parent_objectid,
1454e43eec81SSweet Tea Dorminy ref_index, &name);
145503b2f08bSGeyslan G. Bem if (ret) {
145603b2f08bSGeyslan G. Bem if (ret == 1)
14573650860bSJosef Bacik ret = 0;
1458e02119d5SChris Mason goto out;
14593650860bSJosef Bacik }
14600d836392SFilipe Manana
1461e02119d5SChris Mason /* insert our name */
14627059c658SFilipe Manana ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1463e43eec81SSweet Tea Dorminy &name, 0, ref_index);
14643650860bSJosef Bacik if (ret)
14653650860bSJosef Bacik goto out;
1466e02119d5SChris Mason
1467f96d4474SJosef Bacik ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1468f96d4474SJosef Bacik if (ret)
1469f96d4474SJosef Bacik goto out;
14705a1d7843SJan Schmidt }
147177a5b9e3SFilipe Manana /* Else, ret == 1, we already have a perfect match, we're done. */
1472e02119d5SChris Mason
1473e43eec81SSweet Tea Dorminy ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len;
1474e43eec81SSweet Tea Dorminy kfree(name.name);
1475e43eec81SSweet Tea Dorminy name.name = NULL;
1476f186373fSMark Fasheh if (log_ref_ver) {
1477f186373fSMark Fasheh iput(dir);
1478f186373fSMark Fasheh dir = NULL;
1479f186373fSMark Fasheh }
14805a1d7843SJan Schmidt }
1481e02119d5SChris Mason
14821f250e92SFilipe Manana /*
14831f250e92SFilipe Manana * Before we overwrite the inode reference item in the subvolume tree
14841f250e92SFilipe Manana * with the item from the log tree, we must unlink all names from the
14851f250e92SFilipe Manana * parent directory that are in the subvolume's tree inode reference
14861f250e92SFilipe Manana * item, otherwise we end up with an inconsistent subvolume tree where
14871f250e92SFilipe Manana * dir index entries exist for a name but there is no inode reference
14881f250e92SFilipe Manana * item with the same name.
14891f250e92SFilipe Manana */
14901f250e92SFilipe Manana ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
14911f250e92SFilipe Manana key);
14921f250e92SFilipe Manana if (ret)
14931f250e92SFilipe Manana goto out;
14941f250e92SFilipe Manana
1495e02119d5SChris Mason /* finally write the back reference in the inode */
1496e02119d5SChris Mason ret = overwrite_item(trans, root, path, eb, slot, key);
14975a1d7843SJan Schmidt out:
1498b3b4aa74SDavid Sterba btrfs_release_path(path);
1499e43eec81SSweet Tea Dorminy kfree(name.name);
1500e02119d5SChris Mason iput(dir);
1501e02119d5SChris Mason iput(inode);
15023650860bSJosef Bacik return ret;
1503e02119d5SChris Mason }
1504e02119d5SChris Mason
count_inode_extrefs(struct btrfs_root * root,struct btrfs_inode * inode,struct btrfs_path * path)1505f186373fSMark Fasheh static int count_inode_extrefs(struct btrfs_root *root,
150636283658SNikolay Borisov struct btrfs_inode *inode, struct btrfs_path *path)
1507e02119d5SChris Mason {
1508f186373fSMark Fasheh int ret = 0;
1509f186373fSMark Fasheh int name_len;
1510f186373fSMark Fasheh unsigned int nlink = 0;
1511f186373fSMark Fasheh u32 item_size;
1512f186373fSMark Fasheh u32 cur_offset = 0;
151336283658SNikolay Borisov u64 inode_objectid = btrfs_ino(inode);
1514f186373fSMark Fasheh u64 offset = 0;
1515f186373fSMark Fasheh unsigned long ptr;
1516f186373fSMark Fasheh struct btrfs_inode_extref *extref;
1517f186373fSMark Fasheh struct extent_buffer *leaf;
1518f186373fSMark Fasheh
1519f186373fSMark Fasheh while (1) {
1520f186373fSMark Fasheh ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1521f186373fSMark Fasheh &extref, &offset);
1522f186373fSMark Fasheh if (ret)
1523f186373fSMark Fasheh break;
1524f186373fSMark Fasheh
1525f186373fSMark Fasheh leaf = path->nodes[0];
15263212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]);
1527f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
15282c2c452bSFilipe Manana cur_offset = 0;
1529f186373fSMark Fasheh
1530f186373fSMark Fasheh while (cur_offset < item_size) {
1531f186373fSMark Fasheh extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1532f186373fSMark Fasheh name_len = btrfs_inode_extref_name_len(leaf, extref);
1533f186373fSMark Fasheh
1534f186373fSMark Fasheh nlink++;
1535f186373fSMark Fasheh
1536f186373fSMark Fasheh cur_offset += name_len + sizeof(*extref);
1537f186373fSMark Fasheh }
1538f186373fSMark Fasheh
1539f186373fSMark Fasheh offset++;
1540f186373fSMark Fasheh btrfs_release_path(path);
1541f186373fSMark Fasheh }
1542f186373fSMark Fasheh btrfs_release_path(path);
1543f186373fSMark Fasheh
15442c2c452bSFilipe Manana if (ret < 0 && ret != -ENOENT)
1545f186373fSMark Fasheh return ret;
1546f186373fSMark Fasheh return nlink;
1547f186373fSMark Fasheh }
1548f186373fSMark Fasheh
count_inode_refs(struct btrfs_root * root,struct btrfs_inode * inode,struct btrfs_path * path)1549f186373fSMark Fasheh static int count_inode_refs(struct btrfs_root *root,
1550f329e319SNikolay Borisov struct btrfs_inode *inode, struct btrfs_path *path)
1551f186373fSMark Fasheh {
1552e02119d5SChris Mason int ret;
1553e02119d5SChris Mason struct btrfs_key key;
1554f186373fSMark Fasheh unsigned int nlink = 0;
1555e02119d5SChris Mason unsigned long ptr;
1556e02119d5SChris Mason unsigned long ptr_end;
1557e02119d5SChris Mason int name_len;
1558f329e319SNikolay Borisov u64 ino = btrfs_ino(inode);
1559e02119d5SChris Mason
156033345d01SLi Zefan key.objectid = ino;
1561e02119d5SChris Mason key.type = BTRFS_INODE_REF_KEY;
1562e02119d5SChris Mason key.offset = (u64)-1;
1563e02119d5SChris Mason
1564e02119d5SChris Mason while (1) {
1565e02119d5SChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1566e02119d5SChris Mason if (ret < 0)
1567e02119d5SChris Mason break;
1568e02119d5SChris Mason if (ret > 0) {
1569e02119d5SChris Mason if (path->slots[0] == 0)
1570e02119d5SChris Mason break;
1571e02119d5SChris Mason path->slots[0]--;
1572e02119d5SChris Mason }
1573e93ae26fSFilipe David Borba Manana process_slot:
1574e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key,
1575e02119d5SChris Mason path->slots[0]);
157633345d01SLi Zefan if (key.objectid != ino ||
1577e02119d5SChris Mason key.type != BTRFS_INODE_REF_KEY)
1578e02119d5SChris Mason break;
1579e02119d5SChris Mason ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
15803212fa14SJosef Bacik ptr_end = ptr + btrfs_item_size(path->nodes[0],
1581e02119d5SChris Mason path->slots[0]);
1582e02119d5SChris Mason while (ptr < ptr_end) {
1583e02119d5SChris Mason struct btrfs_inode_ref *ref;
1584e02119d5SChris Mason
1585e02119d5SChris Mason ref = (struct btrfs_inode_ref *)ptr;
1586e02119d5SChris Mason name_len = btrfs_inode_ref_name_len(path->nodes[0],
1587e02119d5SChris Mason ref);
1588e02119d5SChris Mason ptr = (unsigned long)(ref + 1) + name_len;
1589e02119d5SChris Mason nlink++;
1590e02119d5SChris Mason }
1591e02119d5SChris Mason
1592e02119d5SChris Mason if (key.offset == 0)
1593e02119d5SChris Mason break;
1594e93ae26fSFilipe David Borba Manana if (path->slots[0] > 0) {
1595e93ae26fSFilipe David Borba Manana path->slots[0]--;
1596e93ae26fSFilipe David Borba Manana goto process_slot;
1597e93ae26fSFilipe David Borba Manana }
1598e02119d5SChris Mason key.offset--;
1599b3b4aa74SDavid Sterba btrfs_release_path(path);
1600e02119d5SChris Mason }
1601b3b4aa74SDavid Sterba btrfs_release_path(path);
1602f186373fSMark Fasheh
1603f186373fSMark Fasheh return nlink;
1604f186373fSMark Fasheh }
1605f186373fSMark Fasheh
1606f186373fSMark Fasheh /*
1607f186373fSMark Fasheh * There are a few corners where the link count of the file can't
1608f186373fSMark Fasheh * be properly maintained during replay. So, instead of adding
1609f186373fSMark Fasheh * lots of complexity to the log code, we just scan the backrefs
1610f186373fSMark Fasheh * for any file that has been through replay.
1611f186373fSMark Fasheh *
1612f186373fSMark Fasheh * The scan will update the link count on the inode to reflect the
1613f186373fSMark Fasheh * number of back refs found. If it goes down to zero, the iput
1614f186373fSMark Fasheh * will free the inode.
1615f186373fSMark Fasheh */
fixup_inode_link_count(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct inode * inode)1616f186373fSMark Fasheh static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1617f186373fSMark Fasheh struct btrfs_root *root,
1618f186373fSMark Fasheh struct inode *inode)
1619f186373fSMark Fasheh {
1620f186373fSMark Fasheh struct btrfs_path *path;
1621f186373fSMark Fasheh int ret;
1622f186373fSMark Fasheh u64 nlink = 0;
16234a0cc7caSNikolay Borisov u64 ino = btrfs_ino(BTRFS_I(inode));
1624f186373fSMark Fasheh
1625f186373fSMark Fasheh path = btrfs_alloc_path();
1626f186373fSMark Fasheh if (!path)
1627f186373fSMark Fasheh return -ENOMEM;
1628f186373fSMark Fasheh
1629f329e319SNikolay Borisov ret = count_inode_refs(root, BTRFS_I(inode), path);
1630f186373fSMark Fasheh if (ret < 0)
1631f186373fSMark Fasheh goto out;
1632f186373fSMark Fasheh
1633f186373fSMark Fasheh nlink = ret;
1634f186373fSMark Fasheh
163536283658SNikolay Borisov ret = count_inode_extrefs(root, BTRFS_I(inode), path);
1636f186373fSMark Fasheh if (ret < 0)
1637f186373fSMark Fasheh goto out;
1638f186373fSMark Fasheh
1639f186373fSMark Fasheh nlink += ret;
1640f186373fSMark Fasheh
1641f186373fSMark Fasheh ret = 0;
1642f186373fSMark Fasheh
1643e02119d5SChris Mason if (nlink != inode->i_nlink) {
1644bfe86848SMiklos Szeredi set_nlink(inode, nlink);
1645f96d4474SJosef Bacik ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1646f96d4474SJosef Bacik if (ret)
1647f96d4474SJosef Bacik goto out;
1648e02119d5SChris Mason }
16498d5bf1cbSChris Mason BTRFS_I(inode)->index_cnt = (u64)-1;
1650e02119d5SChris Mason
1651c71bf099SYan, Zheng if (inode->i_nlink == 0) {
1652c71bf099SYan, Zheng if (S_ISDIR(inode->i_mode)) {
165312fcfd22SChris Mason ret = replay_dir_deletes(trans, root, NULL, path,
165433345d01SLi Zefan ino, 1);
16553650860bSJosef Bacik if (ret)
16563650860bSJosef Bacik goto out;
165712fcfd22SChris Mason }
1658ecdcf3c2SNikolay Borisov ret = btrfs_insert_orphan_item(trans, root, ino);
1659ecdcf3c2SNikolay Borisov if (ret == -EEXIST)
1660ecdcf3c2SNikolay Borisov ret = 0;
1661c71bf099SYan, Zheng }
166212fcfd22SChris Mason
1663f186373fSMark Fasheh out:
1664f186373fSMark Fasheh btrfs_free_path(path);
1665f186373fSMark Fasheh return ret;
1666e02119d5SChris Mason }
1667e02119d5SChris Mason
fixup_inode_link_counts(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path)1668e02119d5SChris Mason static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1669e02119d5SChris Mason struct btrfs_root *root,
1670e02119d5SChris Mason struct btrfs_path *path)
1671e02119d5SChris Mason {
1672e02119d5SChris Mason int ret;
1673e02119d5SChris Mason struct btrfs_key key;
1674e02119d5SChris Mason struct inode *inode;
1675e02119d5SChris Mason
1676e02119d5SChris Mason key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1677e02119d5SChris Mason key.type = BTRFS_ORPHAN_ITEM_KEY;
1678e02119d5SChris Mason key.offset = (u64)-1;
1679e02119d5SChris Mason while (1) {
1680e02119d5SChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1681e02119d5SChris Mason if (ret < 0)
1682e02119d5SChris Mason break;
1683e02119d5SChris Mason
1684e02119d5SChris Mason if (ret == 1) {
1685011b28acSJosef Bacik ret = 0;
1686e02119d5SChris Mason if (path->slots[0] == 0)
1687e02119d5SChris Mason break;
1688e02119d5SChris Mason path->slots[0]--;
1689e02119d5SChris Mason }
1690e02119d5SChris Mason
1691e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1692e02119d5SChris Mason if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1693e02119d5SChris Mason key.type != BTRFS_ORPHAN_ITEM_KEY)
1694e02119d5SChris Mason break;
1695e02119d5SChris Mason
1696e02119d5SChris Mason ret = btrfs_del_item(trans, root, path);
169765a246c5STsutomu Itoh if (ret)
1698011b28acSJosef Bacik break;
1699e02119d5SChris Mason
1700b3b4aa74SDavid Sterba btrfs_release_path(path);
1701e02119d5SChris Mason inode = read_one_inode(root, key.offset);
1702011b28acSJosef Bacik if (!inode) {
1703011b28acSJosef Bacik ret = -EIO;
1704011b28acSJosef Bacik break;
1705011b28acSJosef Bacik }
1706e02119d5SChris Mason
1707e02119d5SChris Mason ret = fixup_inode_link_count(trans, root, inode);
1708e02119d5SChris Mason iput(inode);
17093650860bSJosef Bacik if (ret)
1710011b28acSJosef Bacik break;
1711e02119d5SChris Mason
171212fcfd22SChris Mason /*
171312fcfd22SChris Mason * fixup on a directory may create new entries,
171412fcfd22SChris Mason * make sure we always look for the highset possible
171512fcfd22SChris Mason * offset
171612fcfd22SChris Mason */
171712fcfd22SChris Mason key.offset = (u64)-1;
1718e02119d5SChris Mason }
1719b3b4aa74SDavid Sterba btrfs_release_path(path);
172065a246c5STsutomu Itoh return ret;
1721e02119d5SChris Mason }
1722e02119d5SChris Mason
1723e02119d5SChris Mason
1724e02119d5SChris Mason /*
1725e02119d5SChris Mason * record a given inode in the fixup dir so we can check its link
1726e02119d5SChris Mason * count when replay is done. The link count is incremented here
1727e02119d5SChris Mason * so the inode won't go away until we check it
1728e02119d5SChris Mason */
link_to_fixup_dir(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 objectid)1729e02119d5SChris Mason static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1730e02119d5SChris Mason struct btrfs_root *root,
1731e02119d5SChris Mason struct btrfs_path *path,
1732e02119d5SChris Mason u64 objectid)
1733e02119d5SChris Mason {
1734e02119d5SChris Mason struct btrfs_key key;
1735e02119d5SChris Mason int ret = 0;
1736e02119d5SChris Mason struct inode *inode;
1737e02119d5SChris Mason
1738e02119d5SChris Mason inode = read_one_inode(root, objectid);
1739c00e9493STsutomu Itoh if (!inode)
1740c00e9493STsutomu Itoh return -EIO;
1741e02119d5SChris Mason
1742e02119d5SChris Mason key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1743962a298fSDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY;
1744e02119d5SChris Mason key.offset = objectid;
1745e02119d5SChris Mason
1746e02119d5SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1747e02119d5SChris Mason
1748b3b4aa74SDavid Sterba btrfs_release_path(path);
1749e02119d5SChris Mason if (ret == 0) {
17509bf7a489SJosef Bacik if (!inode->i_nlink)
17519bf7a489SJosef Bacik set_nlink(inode, 1);
17529bf7a489SJosef Bacik else
17538b558c5fSZach Brown inc_nlink(inode);
17549a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1755e02119d5SChris Mason } else if (ret == -EEXIST) {
1756e02119d5SChris Mason ret = 0;
1757e02119d5SChris Mason }
1758e02119d5SChris Mason iput(inode);
1759e02119d5SChris Mason
1760e02119d5SChris Mason return ret;
1761e02119d5SChris Mason }
1762e02119d5SChris Mason
1763e02119d5SChris Mason /*
1764e02119d5SChris Mason * when replaying the log for a directory, we only insert names
1765e02119d5SChris Mason * for inodes that actually exist. This means an fsync on a directory
1766e02119d5SChris Mason * does not implicitly fsync all the new files in it
1767e02119d5SChris Mason */
insert_one_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 dirid,u64 index,const struct fscrypt_str * name,struct btrfs_key * location)1768e02119d5SChris Mason static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1769e02119d5SChris Mason struct btrfs_root *root,
1770e02119d5SChris Mason u64 dirid, u64 index,
17716db75318SSweet Tea Dorminy const struct fscrypt_str *name,
1772e02119d5SChris Mason struct btrfs_key *location)
1773e02119d5SChris Mason {
1774e02119d5SChris Mason struct inode *inode;
1775e02119d5SChris Mason struct inode *dir;
1776e02119d5SChris Mason int ret;
1777e02119d5SChris Mason
1778e02119d5SChris Mason inode = read_one_inode(root, location->objectid);
1779e02119d5SChris Mason if (!inode)
1780e02119d5SChris Mason return -ENOENT;
1781e02119d5SChris Mason
1782e02119d5SChris Mason dir = read_one_inode(root, dirid);
1783e02119d5SChris Mason if (!dir) {
1784e02119d5SChris Mason iput(inode);
1785e02119d5SChris Mason return -EIO;
1786e02119d5SChris Mason }
1787d555438bSJosef Bacik
1788db0a669fSNikolay Borisov ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1789e43eec81SSweet Tea Dorminy 1, index);
1790e02119d5SChris Mason
1791e02119d5SChris Mason /* FIXME, put inode into FIXUP list */
1792e02119d5SChris Mason
1793e02119d5SChris Mason iput(inode);
1794e02119d5SChris Mason iput(dir);
1795e02119d5SChris Mason return ret;
1796e02119d5SChris Mason }
1797e02119d5SChris Mason
delete_conflicting_dir_entry(struct btrfs_trans_handle * trans,struct btrfs_inode * dir,struct btrfs_path * path,struct btrfs_dir_item * dst_di,const struct btrfs_key * log_key,u8 log_flags,bool exists)1798339d0354SFilipe Manana static int delete_conflicting_dir_entry(struct btrfs_trans_handle *trans,
1799339d0354SFilipe Manana struct btrfs_inode *dir,
1800339d0354SFilipe Manana struct btrfs_path *path,
1801339d0354SFilipe Manana struct btrfs_dir_item *dst_di,
1802339d0354SFilipe Manana const struct btrfs_key *log_key,
180394a48aefSOmar Sandoval u8 log_flags,
1804339d0354SFilipe Manana bool exists)
1805339d0354SFilipe Manana {
1806339d0354SFilipe Manana struct btrfs_key found_key;
1807339d0354SFilipe Manana
1808339d0354SFilipe Manana btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1809339d0354SFilipe Manana /* The existing dentry points to the same inode, don't delete it. */
1810339d0354SFilipe Manana if (found_key.objectid == log_key->objectid &&
1811339d0354SFilipe Manana found_key.type == log_key->type &&
1812339d0354SFilipe Manana found_key.offset == log_key->offset &&
181394a48aefSOmar Sandoval btrfs_dir_flags(path->nodes[0], dst_di) == log_flags)
1814339d0354SFilipe Manana return 1;
1815339d0354SFilipe Manana
1816339d0354SFilipe Manana /*
1817339d0354SFilipe Manana * Don't drop the conflicting directory entry if the inode for the new
1818339d0354SFilipe Manana * entry doesn't exist.
1819339d0354SFilipe Manana */
1820339d0354SFilipe Manana if (!exists)
1821339d0354SFilipe Manana return 0;
1822339d0354SFilipe Manana
1823339d0354SFilipe Manana return drop_one_dir_item(trans, path, dir, dst_di);
1824339d0354SFilipe Manana }
1825339d0354SFilipe Manana
1826e02119d5SChris Mason /*
1827e02119d5SChris Mason * take a single entry in a log directory item and replay it into
1828e02119d5SChris Mason * the subvolume.
1829e02119d5SChris Mason *
1830e02119d5SChris Mason * if a conflicting item exists in the subdirectory already,
1831e02119d5SChris Mason * the inode it points to is unlinked and put into the link count
1832e02119d5SChris Mason * fix up tree.
1833e02119d5SChris Mason *
1834e02119d5SChris Mason * If a name from the log points to a file or directory that does
1835e02119d5SChris Mason * not exist in the FS, it is skipped. fsyncs on directories
1836e02119d5SChris Mason * do not force down inodes inside that directory, just changes to the
1837e02119d5SChris Mason * names or unlinks in a directory.
1838bb53eda9SFilipe Manana *
1839bb53eda9SFilipe Manana * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1840bb53eda9SFilipe Manana * non-existing inode) and 1 if the name was replayed.
1841e02119d5SChris Mason */
replay_one_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,struct btrfs_dir_item * di,struct btrfs_key * key)1842e02119d5SChris Mason static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1843e02119d5SChris Mason struct btrfs_root *root,
1844e02119d5SChris Mason struct btrfs_path *path,
1845e02119d5SChris Mason struct extent_buffer *eb,
1846e02119d5SChris Mason struct btrfs_dir_item *di,
1847e02119d5SChris Mason struct btrfs_key *key)
1848e02119d5SChris Mason {
1849*7fc7c47bSRoi Martin struct fscrypt_str name = { 0 };
1850339d0354SFilipe Manana struct btrfs_dir_item *dir_dst_di;
1851339d0354SFilipe Manana struct btrfs_dir_item *index_dst_di;
1852339d0354SFilipe Manana bool dir_dst_matches = false;
1853339d0354SFilipe Manana bool index_dst_matches = false;
1854e02119d5SChris Mason struct btrfs_key log_key;
1855339d0354SFilipe Manana struct btrfs_key search_key;
1856e02119d5SChris Mason struct inode *dir;
185794a48aefSOmar Sandoval u8 log_flags;
1858cfd31269SFilipe Manana bool exists;
1859cfd31269SFilipe Manana int ret;
1860339d0354SFilipe Manana bool update_size = true;
1861bb53eda9SFilipe Manana bool name_added = false;
1862e02119d5SChris Mason
1863e02119d5SChris Mason dir = read_one_inode(root, key->objectid);
1864c00e9493STsutomu Itoh if (!dir)
1865c00e9493STsutomu Itoh return -EIO;
1866e02119d5SChris Mason
1867e43eec81SSweet Tea Dorminy ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
1868e43eec81SSweet Tea Dorminy if (ret)
18692bac325eSFilipe David Borba Manana goto out;
18702a29edc6Sliubo
187194a48aefSOmar Sandoval log_flags = btrfs_dir_flags(eb, di);
1872e02119d5SChris Mason btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1873cfd31269SFilipe Manana ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1874b3b4aa74SDavid Sterba btrfs_release_path(path);
1875cfd31269SFilipe Manana if (ret < 0)
1876cfd31269SFilipe Manana goto out;
1877cfd31269SFilipe Manana exists = (ret == 0);
1878cfd31269SFilipe Manana ret = 0;
18794bef0848SChris Mason
1880339d0354SFilipe Manana dir_dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1881e43eec81SSweet Tea Dorminy &name, 1);
1882339d0354SFilipe Manana if (IS_ERR(dir_dst_di)) {
1883339d0354SFilipe Manana ret = PTR_ERR(dir_dst_di);
18843650860bSJosef Bacik goto out;
1885339d0354SFilipe Manana } else if (dir_dst_di) {
1886339d0354SFilipe Manana ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path,
188794a48aefSOmar Sandoval dir_dst_di, &log_key,
188894a48aefSOmar Sandoval log_flags, exists);
1889339d0354SFilipe Manana if (ret < 0)
1890339d0354SFilipe Manana goto out;
1891339d0354SFilipe Manana dir_dst_matches = (ret == 1);
1892e02119d5SChris Mason }
1893e15ac641SFilipe Manana
1894339d0354SFilipe Manana btrfs_release_path(path);
1895339d0354SFilipe Manana
1896339d0354SFilipe Manana index_dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1897339d0354SFilipe Manana key->objectid, key->offset,
1898e43eec81SSweet Tea Dorminy &name, 1);
1899339d0354SFilipe Manana if (IS_ERR(index_dst_di)) {
1900339d0354SFilipe Manana ret = PTR_ERR(index_dst_di);
1901e15ac641SFilipe Manana goto out;
1902339d0354SFilipe Manana } else if (index_dst_di) {
1903339d0354SFilipe Manana ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path,
1904339d0354SFilipe Manana index_dst_di, &log_key,
190594a48aefSOmar Sandoval log_flags, exists);
1906339d0354SFilipe Manana if (ret < 0)
1907e02119d5SChris Mason goto out;
1908339d0354SFilipe Manana index_dst_matches = (ret == 1);
1909e02119d5SChris Mason }
1910e02119d5SChris Mason
1911339d0354SFilipe Manana btrfs_release_path(path);
1912339d0354SFilipe Manana
1913339d0354SFilipe Manana if (dir_dst_matches && index_dst_matches) {
1914339d0354SFilipe Manana ret = 0;
1915a2cc11dbSFilipe Manana update_size = false;
1916e02119d5SChris Mason goto out;
1917e02119d5SChris Mason }
1918e02119d5SChris Mason
1919e02119d5SChris Mason /*
1920725af92aSNikolay Borisov * Check if the inode reference exists in the log for the given name,
1921725af92aSNikolay Borisov * inode and parent inode
1922725af92aSNikolay Borisov */
1923339d0354SFilipe Manana search_key.objectid = log_key.objectid;
1924339d0354SFilipe Manana search_key.type = BTRFS_INODE_REF_KEY;
1925339d0354SFilipe Manana search_key.offset = key->objectid;
1926e43eec81SSweet Tea Dorminy ret = backref_in_log(root->log_root, &search_key, 0, &name);
1927725af92aSNikolay Borisov if (ret < 0) {
1928725af92aSNikolay Borisov goto out;
1929725af92aSNikolay Borisov } else if (ret) {
1930725af92aSNikolay Borisov /* The dentry will be added later. */
1931725af92aSNikolay Borisov ret = 0;
1932725af92aSNikolay Borisov update_size = false;
1933725af92aSNikolay Borisov goto out;
1934725af92aSNikolay Borisov }
1935725af92aSNikolay Borisov
1936339d0354SFilipe Manana search_key.objectid = log_key.objectid;
1937339d0354SFilipe Manana search_key.type = BTRFS_INODE_EXTREF_KEY;
1938339d0354SFilipe Manana search_key.offset = key->objectid;
1939e43eec81SSweet Tea Dorminy ret = backref_in_log(root->log_root, &search_key, key->objectid, &name);
1940725af92aSNikolay Borisov if (ret < 0) {
1941725af92aSNikolay Borisov goto out;
1942725af92aSNikolay Borisov } else if (ret) {
1943df8d116fSFilipe Manana /* The dentry will be added later. */
1944df8d116fSFilipe Manana ret = 0;
1945df8d116fSFilipe Manana update_size = false;
1946df8d116fSFilipe Manana goto out;
1947df8d116fSFilipe Manana }
1948b3b4aa74SDavid Sterba btrfs_release_path(path);
194960d53eb3SZhaolei ret = insert_one_name(trans, root, key->objectid, key->offset,
1950e43eec81SSweet Tea Dorminy &name, &log_key);
1951df8d116fSFilipe Manana if (ret && ret != -ENOENT && ret != -EEXIST)
19523650860bSJosef Bacik goto out;
1953bb53eda9SFilipe Manana if (!ret)
1954bb53eda9SFilipe Manana name_added = true;
1955d555438bSJosef Bacik update_size = false;
19563650860bSJosef Bacik ret = 0;
1957339d0354SFilipe Manana
1958339d0354SFilipe Manana out:
1959339d0354SFilipe Manana if (!ret && update_size) {
1960e43eec81SSweet Tea Dorminy btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name.len * 2);
1961339d0354SFilipe Manana ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
1962339d0354SFilipe Manana }
1963e43eec81SSweet Tea Dorminy kfree(name.name);
1964339d0354SFilipe Manana iput(dir);
1965339d0354SFilipe Manana if (!ret && name_added)
1966339d0354SFilipe Manana ret = 1;
1967339d0354SFilipe Manana return ret;
1968e02119d5SChris Mason }
1969e02119d5SChris Mason
1970339d0354SFilipe Manana /* Replay one dir item from a BTRFS_DIR_INDEX_KEY key. */
replay_one_dir_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)1971e02119d5SChris Mason static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1972e02119d5SChris Mason struct btrfs_root *root,
1973e02119d5SChris Mason struct btrfs_path *path,
1974e02119d5SChris Mason struct extent_buffer *eb, int slot,
1975e02119d5SChris Mason struct btrfs_key *key)
1976e02119d5SChris Mason {
1977339d0354SFilipe Manana int ret;
1978e02119d5SChris Mason struct btrfs_dir_item *di;
1979e02119d5SChris Mason
1980339d0354SFilipe Manana /* We only log dir index keys, which only contain a single dir item. */
1981339d0354SFilipe Manana ASSERT(key->type == BTRFS_DIR_INDEX_KEY);
1982339d0354SFilipe Manana
1983339d0354SFilipe Manana di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1984e02119d5SChris Mason ret = replay_one_name(trans, root, path, eb, di, key);
1985bb53eda9SFilipe Manana if (ret < 0)
1986339d0354SFilipe Manana return ret;
1987bb53eda9SFilipe Manana
1988bb53eda9SFilipe Manana /*
1989339d0354SFilipe Manana * If this entry refers to a non-directory (directories can not have a
1990339d0354SFilipe Manana * link count > 1) and it was added in the transaction that was not
1991339d0354SFilipe Manana * committed, make sure we fixup the link count of the inode the entry
1992339d0354SFilipe Manana * points to. Otherwise something like the following would result in a
1993339d0354SFilipe Manana * directory pointing to an inode with a wrong link that does not account
1994339d0354SFilipe Manana * for this dir entry:
1995bb53eda9SFilipe Manana *
1996bb53eda9SFilipe Manana * mkdir testdir
1997bb53eda9SFilipe Manana * touch testdir/foo
1998bb53eda9SFilipe Manana * touch testdir/bar
1999bb53eda9SFilipe Manana * sync
2000bb53eda9SFilipe Manana *
2001bb53eda9SFilipe Manana * ln testdir/bar testdir/bar_link
2002bb53eda9SFilipe Manana * ln testdir/foo testdir/foo_link
2003bb53eda9SFilipe Manana * xfs_io -c "fsync" testdir/bar
2004bb53eda9SFilipe Manana *
2005bb53eda9SFilipe Manana * <power failure>
2006bb53eda9SFilipe Manana *
2007bb53eda9SFilipe Manana * mount fs, log replay happens
2008bb53eda9SFilipe Manana *
2009339d0354SFilipe Manana * File foo would remain with a link count of 1 when it has two entries
2010339d0354SFilipe Manana * pointing to it in the directory testdir. This would make it impossible
2011339d0354SFilipe Manana * to ever delete the parent directory has it would result in stale
2012339d0354SFilipe Manana * dentries that can never be deleted.
2013bb53eda9SFilipe Manana */
201494a48aefSOmar Sandoval if (ret == 1 && btrfs_dir_ftype(eb, di) != BTRFS_FT_DIR) {
2015339d0354SFilipe Manana struct btrfs_path *fixup_path;
2016bb53eda9SFilipe Manana struct btrfs_key di_key;
2017bb53eda9SFilipe Manana
2018bb53eda9SFilipe Manana fixup_path = btrfs_alloc_path();
2019339d0354SFilipe Manana if (!fixup_path)
2020339d0354SFilipe Manana return -ENOMEM;
2021bb53eda9SFilipe Manana
2022bb53eda9SFilipe Manana btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2023339d0354SFilipe Manana ret = link_to_fixup_dir(trans, root, fixup_path, di_key.objectid);
2024bb53eda9SFilipe Manana btrfs_free_path(fixup_path);
2025339d0354SFilipe Manana }
2026339d0354SFilipe Manana
2027bb53eda9SFilipe Manana return ret;
2028e02119d5SChris Mason }
2029e02119d5SChris Mason
2030e02119d5SChris Mason /*
2031e02119d5SChris Mason * directory replay has two parts. There are the standard directory
2032e02119d5SChris Mason * items in the log copied from the subvolume, and range items
2033e02119d5SChris Mason * created in the log while the subvolume was logged.
2034e02119d5SChris Mason *
2035e02119d5SChris Mason * The range items tell us which parts of the key space the log
2036e02119d5SChris Mason * is authoritative for. During replay, if a key in the subvolume
2037e02119d5SChris Mason * directory is in a logged range item, but not actually in the log
2038e02119d5SChris Mason * that means it was deleted from the directory before the fsync
2039e02119d5SChris Mason * and should be removed.
2040e02119d5SChris Mason */
find_dir_range(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,u64 * start_ret,u64 * end_ret)2041e02119d5SChris Mason static noinline int find_dir_range(struct btrfs_root *root,
2042e02119d5SChris Mason struct btrfs_path *path,
2043ccae4a19SFilipe Manana u64 dirid,
2044e02119d5SChris Mason u64 *start_ret, u64 *end_ret)
2045e02119d5SChris Mason {
2046e02119d5SChris Mason struct btrfs_key key;
2047e02119d5SChris Mason u64 found_end;
2048e02119d5SChris Mason struct btrfs_dir_log_item *item;
2049e02119d5SChris Mason int ret;
2050e02119d5SChris Mason int nritems;
2051e02119d5SChris Mason
2052e02119d5SChris Mason if (*start_ret == (u64)-1)
2053e02119d5SChris Mason return 1;
2054e02119d5SChris Mason
2055e02119d5SChris Mason key.objectid = dirid;
2056ccae4a19SFilipe Manana key.type = BTRFS_DIR_LOG_INDEX_KEY;
2057e02119d5SChris Mason key.offset = *start_ret;
2058e02119d5SChris Mason
2059e02119d5SChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2060e02119d5SChris Mason if (ret < 0)
2061e02119d5SChris Mason goto out;
2062e02119d5SChris Mason if (ret > 0) {
2063e02119d5SChris Mason if (path->slots[0] == 0)
2064e02119d5SChris Mason goto out;
2065e02119d5SChris Mason path->slots[0]--;
2066e02119d5SChris Mason }
2067e02119d5SChris Mason if (ret != 0)
2068e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2069e02119d5SChris Mason
2070ccae4a19SFilipe Manana if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
2071e02119d5SChris Mason ret = 1;
2072e02119d5SChris Mason goto next;
2073e02119d5SChris Mason }
2074e02119d5SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2075e02119d5SChris Mason struct btrfs_dir_log_item);
2076e02119d5SChris Mason found_end = btrfs_dir_log_end(path->nodes[0], item);
2077e02119d5SChris Mason
2078e02119d5SChris Mason if (*start_ret >= key.offset && *start_ret <= found_end) {
2079e02119d5SChris Mason ret = 0;
2080e02119d5SChris Mason *start_ret = key.offset;
2081e02119d5SChris Mason *end_ret = found_end;
2082e02119d5SChris Mason goto out;
2083e02119d5SChris Mason }
2084e02119d5SChris Mason ret = 1;
2085e02119d5SChris Mason next:
2086e02119d5SChris Mason /* check the next slot in the tree to see if it is a valid item */
2087e02119d5SChris Mason nritems = btrfs_header_nritems(path->nodes[0]);
20882a7bf53fSRobbie Ko path->slots[0]++;
2089e02119d5SChris Mason if (path->slots[0] >= nritems) {
2090e02119d5SChris Mason ret = btrfs_next_leaf(root, path);
2091e02119d5SChris Mason if (ret)
2092e02119d5SChris Mason goto out;
2093e02119d5SChris Mason }
2094e02119d5SChris Mason
2095e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2096e02119d5SChris Mason
2097ccae4a19SFilipe Manana if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
2098e02119d5SChris Mason ret = 1;
2099e02119d5SChris Mason goto out;
2100e02119d5SChris Mason }
2101e02119d5SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2102e02119d5SChris Mason struct btrfs_dir_log_item);
2103e02119d5SChris Mason found_end = btrfs_dir_log_end(path->nodes[0], item);
2104e02119d5SChris Mason *start_ret = key.offset;
2105e02119d5SChris Mason *end_ret = found_end;
2106e02119d5SChris Mason ret = 0;
2107e02119d5SChris Mason out:
2108b3b4aa74SDavid Sterba btrfs_release_path(path);
2109e02119d5SChris Mason return ret;
2110e02119d5SChris Mason }
2111e02119d5SChris Mason
2112e02119d5SChris Mason /*
2113e02119d5SChris Mason * this looks for a given directory item in the log. If the directory
2114e02119d5SChris Mason * item is not in the log, the item is removed and the inode it points
2115e02119d5SChris Mason * to is unlinked
2116e02119d5SChris Mason */
check_item_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,struct btrfs_path * log_path,struct inode * dir,struct btrfs_key * dir_key)2117e02119d5SChris Mason static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2118e02119d5SChris Mason struct btrfs_root *log,
2119e02119d5SChris Mason struct btrfs_path *path,
2120e02119d5SChris Mason struct btrfs_path *log_path,
2121e02119d5SChris Mason struct inode *dir,
2122e02119d5SChris Mason struct btrfs_key *dir_key)
2123e02119d5SChris Mason {
2124d1ed82f3SFilipe Manana struct btrfs_root *root = BTRFS_I(dir)->root;
2125e02119d5SChris Mason int ret;
2126e02119d5SChris Mason struct extent_buffer *eb;
2127e02119d5SChris Mason int slot;
2128e02119d5SChris Mason struct btrfs_dir_item *di;
2129*7fc7c47bSRoi Martin struct fscrypt_str name = { 0 };
2130ccae4a19SFilipe Manana struct inode *inode = NULL;
2131e02119d5SChris Mason struct btrfs_key location;
2132e02119d5SChris Mason
2133ccae4a19SFilipe Manana /*
2134143823cfSDavid Sterba * Currently we only log dir index keys. Even if we replay a log created
2135ccae4a19SFilipe Manana * by an older kernel that logged both dir index and dir item keys, all
2136ccae4a19SFilipe Manana * we need to do is process the dir index keys, we (and our caller) can
2137ccae4a19SFilipe Manana * safely ignore dir item keys (key type BTRFS_DIR_ITEM_KEY).
2138ccae4a19SFilipe Manana */
2139ccae4a19SFilipe Manana ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY);
2140ccae4a19SFilipe Manana
2141e02119d5SChris Mason eb = path->nodes[0];
2142e02119d5SChris Mason slot = path->slots[0];
2143ccae4a19SFilipe Manana di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2144e43eec81SSweet Tea Dorminy ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
2145e43eec81SSweet Tea Dorminy if (ret)
2146e02119d5SChris Mason goto out;
2147ccae4a19SFilipe Manana
2148ccae4a19SFilipe Manana if (log) {
2149ccae4a19SFilipe Manana struct btrfs_dir_item *log_di;
2150ccae4a19SFilipe Manana
2151ccae4a19SFilipe Manana log_di = btrfs_lookup_dir_index_item(trans, log, log_path,
2152e02119d5SChris Mason dir_key->objectid,
2153e43eec81SSweet Tea Dorminy dir_key->offset, &name, 0);
2154ccae4a19SFilipe Manana if (IS_ERR(log_di)) {
2155ccae4a19SFilipe Manana ret = PTR_ERR(log_di);
2156ccae4a19SFilipe Manana goto out;
2157ccae4a19SFilipe Manana } else if (log_di) {
2158ccae4a19SFilipe Manana /* The dentry exists in the log, we have nothing to do. */
2159ccae4a19SFilipe Manana ret = 0;
2160ccae4a19SFilipe Manana goto out;
2161e02119d5SChris Mason }
2162ccae4a19SFilipe Manana }
2163ccae4a19SFilipe Manana
2164e02119d5SChris Mason btrfs_dir_item_key_to_cpu(eb, di, &location);
2165b3b4aa74SDavid Sterba btrfs_release_path(path);
2166b3b4aa74SDavid Sterba btrfs_release_path(log_path);
2167e02119d5SChris Mason inode = read_one_inode(root, location.objectid);
2168c00e9493STsutomu Itoh if (!inode) {
2169ccae4a19SFilipe Manana ret = -EIO;
21703650860bSJosef Bacik goto out;
21713650860bSJosef Bacik }
21723650860bSJosef Bacik
2173ccae4a19SFilipe Manana ret = link_to_fixup_dir(trans, root, path, location.objectid);
21743650860bSJosef Bacik if (ret)
21753650860bSJosef Bacik goto out;
2176e02119d5SChris Mason
2177ccae4a19SFilipe Manana inc_nlink(inode);
2178313ab753SFilipe Manana ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir), BTRFS_I(inode),
2179e43eec81SSweet Tea Dorminy &name);
2180ccae4a19SFilipe Manana /*
2181ccae4a19SFilipe Manana * Unlike dir item keys, dir index keys can only have one name (entry) in
2182ccae4a19SFilipe Manana * them, as there are no key collisions since each key has a unique offset
2183ccae4a19SFilipe Manana * (an index number), so we're done.
2184ccae4a19SFilipe Manana */
2185e02119d5SChris Mason out:
2186b3b4aa74SDavid Sterba btrfs_release_path(path);
2187b3b4aa74SDavid Sterba btrfs_release_path(log_path);
2188e43eec81SSweet Tea Dorminy kfree(name.name);
2189ccae4a19SFilipe Manana iput(inode);
2190e02119d5SChris Mason return ret;
2191e02119d5SChris Mason }
2192e02119d5SChris Mason
replay_xattr_deletes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,const u64 ino)21934f764e51SFilipe Manana static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
21944f764e51SFilipe Manana struct btrfs_root *root,
21954f764e51SFilipe Manana struct btrfs_root *log,
21964f764e51SFilipe Manana struct btrfs_path *path,
21974f764e51SFilipe Manana const u64 ino)
21984f764e51SFilipe Manana {
21994f764e51SFilipe Manana struct btrfs_key search_key;
22004f764e51SFilipe Manana struct btrfs_path *log_path;
22014f764e51SFilipe Manana int i;
22024f764e51SFilipe Manana int nritems;
22034f764e51SFilipe Manana int ret;
22044f764e51SFilipe Manana
22054f764e51SFilipe Manana log_path = btrfs_alloc_path();
22064f764e51SFilipe Manana if (!log_path)
22074f764e51SFilipe Manana return -ENOMEM;
22084f764e51SFilipe Manana
22094f764e51SFilipe Manana search_key.objectid = ino;
22104f764e51SFilipe Manana search_key.type = BTRFS_XATTR_ITEM_KEY;
22114f764e51SFilipe Manana search_key.offset = 0;
22124f764e51SFilipe Manana again:
22134f764e51SFilipe Manana ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
22144f764e51SFilipe Manana if (ret < 0)
22154f764e51SFilipe Manana goto out;
22164f764e51SFilipe Manana process_leaf:
22174f764e51SFilipe Manana nritems = btrfs_header_nritems(path->nodes[0]);
22184f764e51SFilipe Manana for (i = path->slots[0]; i < nritems; i++) {
22194f764e51SFilipe Manana struct btrfs_key key;
22204f764e51SFilipe Manana struct btrfs_dir_item *di;
22214f764e51SFilipe Manana struct btrfs_dir_item *log_di;
22224f764e51SFilipe Manana u32 total_size;
22234f764e51SFilipe Manana u32 cur;
22244f764e51SFilipe Manana
22254f764e51SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, i);
22264f764e51SFilipe Manana if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
22274f764e51SFilipe Manana ret = 0;
22284f764e51SFilipe Manana goto out;
22294f764e51SFilipe Manana }
22304f764e51SFilipe Manana
22314f764e51SFilipe Manana di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
22323212fa14SJosef Bacik total_size = btrfs_item_size(path->nodes[0], i);
22334f764e51SFilipe Manana cur = 0;
22344f764e51SFilipe Manana while (cur < total_size) {
22354f764e51SFilipe Manana u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
22364f764e51SFilipe Manana u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
22374f764e51SFilipe Manana u32 this_len = sizeof(*di) + name_len + data_len;
22384f764e51SFilipe Manana char *name;
22394f764e51SFilipe Manana
22404f764e51SFilipe Manana name = kmalloc(name_len, GFP_NOFS);
22414f764e51SFilipe Manana if (!name) {
22424f764e51SFilipe Manana ret = -ENOMEM;
22434f764e51SFilipe Manana goto out;
22444f764e51SFilipe Manana }
22454f764e51SFilipe Manana read_extent_buffer(path->nodes[0], name,
22464f764e51SFilipe Manana (unsigned long)(di + 1), name_len);
22474f764e51SFilipe Manana
22484f764e51SFilipe Manana log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
22494f764e51SFilipe Manana name, name_len, 0);
22504f764e51SFilipe Manana btrfs_release_path(log_path);
22514f764e51SFilipe Manana if (!log_di) {
22524f764e51SFilipe Manana /* Doesn't exist in log tree, so delete it. */
22534f764e51SFilipe Manana btrfs_release_path(path);
22544f764e51SFilipe Manana di = btrfs_lookup_xattr(trans, root, path, ino,
22554f764e51SFilipe Manana name, name_len, -1);
22564f764e51SFilipe Manana kfree(name);
22574f764e51SFilipe Manana if (IS_ERR(di)) {
22584f764e51SFilipe Manana ret = PTR_ERR(di);
22594f764e51SFilipe Manana goto out;
22604f764e51SFilipe Manana }
22614f764e51SFilipe Manana ASSERT(di);
22624f764e51SFilipe Manana ret = btrfs_delete_one_dir_name(trans, root,
22634f764e51SFilipe Manana path, di);
22644f764e51SFilipe Manana if (ret)
22654f764e51SFilipe Manana goto out;
22664f764e51SFilipe Manana btrfs_release_path(path);
22674f764e51SFilipe Manana search_key = key;
22684f764e51SFilipe Manana goto again;
22694f764e51SFilipe Manana }
22704f764e51SFilipe Manana kfree(name);
22714f764e51SFilipe Manana if (IS_ERR(log_di)) {
22724f764e51SFilipe Manana ret = PTR_ERR(log_di);
22734f764e51SFilipe Manana goto out;
22744f764e51SFilipe Manana }
22754f764e51SFilipe Manana cur += this_len;
22764f764e51SFilipe Manana di = (struct btrfs_dir_item *)((char *)di + this_len);
22774f764e51SFilipe Manana }
22784f764e51SFilipe Manana }
22794f764e51SFilipe Manana ret = btrfs_next_leaf(root, path);
22804f764e51SFilipe Manana if (ret > 0)
22814f764e51SFilipe Manana ret = 0;
22824f764e51SFilipe Manana else if (ret == 0)
22834f764e51SFilipe Manana goto process_leaf;
22844f764e51SFilipe Manana out:
22854f764e51SFilipe Manana btrfs_free_path(log_path);
22864f764e51SFilipe Manana btrfs_release_path(path);
22874f764e51SFilipe Manana return ret;
22884f764e51SFilipe Manana }
22894f764e51SFilipe Manana
22904f764e51SFilipe Manana
2291e02119d5SChris Mason /*
2292e02119d5SChris Mason * deletion replay happens before we copy any new directory items
2293e02119d5SChris Mason * out of the log or out of backreferences from inodes. It
2294e02119d5SChris Mason * scans the log to find ranges of keys that log is authoritative for,
2295e02119d5SChris Mason * and then scans the directory to find items in those ranges that are
2296e02119d5SChris Mason * not present in the log.
2297e02119d5SChris Mason *
2298e02119d5SChris Mason * Anything we don't find in the log is unlinked and removed from the
2299e02119d5SChris Mason * directory.
2300e02119d5SChris Mason */
replay_dir_deletes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,u64 dirid,int del_all)2301e02119d5SChris Mason static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2302e02119d5SChris Mason struct btrfs_root *root,
2303e02119d5SChris Mason struct btrfs_root *log,
2304e02119d5SChris Mason struct btrfs_path *path,
230512fcfd22SChris Mason u64 dirid, int del_all)
2306e02119d5SChris Mason {
2307e02119d5SChris Mason u64 range_start;
2308e02119d5SChris Mason u64 range_end;
2309e02119d5SChris Mason int ret = 0;
2310e02119d5SChris Mason struct btrfs_key dir_key;
2311e02119d5SChris Mason struct btrfs_key found_key;
2312e02119d5SChris Mason struct btrfs_path *log_path;
2313e02119d5SChris Mason struct inode *dir;
2314e02119d5SChris Mason
2315e02119d5SChris Mason dir_key.objectid = dirid;
2316ccae4a19SFilipe Manana dir_key.type = BTRFS_DIR_INDEX_KEY;
2317e02119d5SChris Mason log_path = btrfs_alloc_path();
2318e02119d5SChris Mason if (!log_path)
2319e02119d5SChris Mason return -ENOMEM;
2320e02119d5SChris Mason
2321e02119d5SChris Mason dir = read_one_inode(root, dirid);
2322e02119d5SChris Mason /* it isn't an error if the inode isn't there, that can happen
2323e02119d5SChris Mason * because we replay the deletes before we copy in the inode item
2324e02119d5SChris Mason * from the log
2325e02119d5SChris Mason */
2326e02119d5SChris Mason if (!dir) {
2327e02119d5SChris Mason btrfs_free_path(log_path);
2328e02119d5SChris Mason return 0;
2329e02119d5SChris Mason }
2330ccae4a19SFilipe Manana
2331e02119d5SChris Mason range_start = 0;
2332e02119d5SChris Mason range_end = 0;
2333e02119d5SChris Mason while (1) {
233412fcfd22SChris Mason if (del_all)
233512fcfd22SChris Mason range_end = (u64)-1;
233612fcfd22SChris Mason else {
2337ccae4a19SFilipe Manana ret = find_dir_range(log, path, dirid,
2338e02119d5SChris Mason &range_start, &range_end);
233910adb115SFilipe Manana if (ret < 0)
234010adb115SFilipe Manana goto out;
234110adb115SFilipe Manana else if (ret > 0)
2342e02119d5SChris Mason break;
234312fcfd22SChris Mason }
2344e02119d5SChris Mason
2345e02119d5SChris Mason dir_key.offset = range_start;
2346e02119d5SChris Mason while (1) {
2347e02119d5SChris Mason int nritems;
2348e02119d5SChris Mason ret = btrfs_search_slot(NULL, root, &dir_key, path,
2349e02119d5SChris Mason 0, 0);
2350e02119d5SChris Mason if (ret < 0)
2351e02119d5SChris Mason goto out;
2352e02119d5SChris Mason
2353e02119d5SChris Mason nritems = btrfs_header_nritems(path->nodes[0]);
2354e02119d5SChris Mason if (path->slots[0] >= nritems) {
2355e02119d5SChris Mason ret = btrfs_next_leaf(root, path);
2356b98def7cSLiu Bo if (ret == 1)
2357e02119d5SChris Mason break;
2358b98def7cSLiu Bo else if (ret < 0)
2359b98def7cSLiu Bo goto out;
2360e02119d5SChris Mason }
2361e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2362e02119d5SChris Mason path->slots[0]);
2363e02119d5SChris Mason if (found_key.objectid != dirid ||
2364ccae4a19SFilipe Manana found_key.type != dir_key.type) {
2365ccae4a19SFilipe Manana ret = 0;
2366ccae4a19SFilipe Manana goto out;
2367ccae4a19SFilipe Manana }
2368e02119d5SChris Mason
2369e02119d5SChris Mason if (found_key.offset > range_end)
2370e02119d5SChris Mason break;
2371e02119d5SChris Mason
2372d1ed82f3SFilipe Manana ret = check_item_in_log(trans, log, path,
237312fcfd22SChris Mason log_path, dir,
237412fcfd22SChris Mason &found_key);
23753650860bSJosef Bacik if (ret)
23763650860bSJosef Bacik goto out;
2377e02119d5SChris Mason if (found_key.offset == (u64)-1)
2378e02119d5SChris Mason break;
2379e02119d5SChris Mason dir_key.offset = found_key.offset + 1;
2380e02119d5SChris Mason }
2381b3b4aa74SDavid Sterba btrfs_release_path(path);
2382e02119d5SChris Mason if (range_end == (u64)-1)
2383e02119d5SChris Mason break;
2384e02119d5SChris Mason range_start = range_end + 1;
2385e02119d5SChris Mason }
2386e02119d5SChris Mason ret = 0;
2387e02119d5SChris Mason out:
2388b3b4aa74SDavid Sterba btrfs_release_path(path);
2389e02119d5SChris Mason btrfs_free_path(log_path);
2390e02119d5SChris Mason iput(dir);
2391e02119d5SChris Mason return ret;
2392e02119d5SChris Mason }
2393e02119d5SChris Mason
2394e02119d5SChris Mason /*
2395e02119d5SChris Mason * the process_func used to replay items from the log tree. This
2396e02119d5SChris Mason * gets called in two different stages. The first stage just looks
2397e02119d5SChris Mason * for inodes and makes sure they are all copied into the subvolume.
2398e02119d5SChris Mason *
2399e02119d5SChris Mason * The second stage copies all the other item types from the log into
2400e02119d5SChris Mason * the subvolume. The two stage approach is slower, but gets rid of
2401e02119d5SChris Mason * lots of complexity around inodes referencing other inodes that exist
2402e02119d5SChris Mason * only in the log (references come from either directory items or inode
2403e02119d5SChris Mason * back refs).
2404e02119d5SChris Mason */
replay_one_buffer(struct btrfs_root * log,struct extent_buffer * eb,struct walk_control * wc,u64 gen,int level)2405e02119d5SChris Mason static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2406581c1760SQu Wenruo struct walk_control *wc, u64 gen, int level)
2407e02119d5SChris Mason {
2408e02119d5SChris Mason int nritems;
2409789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = {
2410789d6a3aSQu Wenruo .transid = gen,
2411789d6a3aSQu Wenruo .level = level
2412789d6a3aSQu Wenruo };
2413e02119d5SChris Mason struct btrfs_path *path;
2414e02119d5SChris Mason struct btrfs_root *root = wc->replay_dest;
2415e02119d5SChris Mason struct btrfs_key key;
2416e02119d5SChris Mason int i;
2417e02119d5SChris Mason int ret;
2418e02119d5SChris Mason
2419789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(eb, &check);
2420018642a1STsutomu Itoh if (ret)
2421018642a1STsutomu Itoh return ret;
2422e02119d5SChris Mason
2423e02119d5SChris Mason level = btrfs_header_level(eb);
2424e02119d5SChris Mason
2425e02119d5SChris Mason if (level != 0)
2426e02119d5SChris Mason return 0;
2427e02119d5SChris Mason
2428e02119d5SChris Mason path = btrfs_alloc_path();
24291e5063d0SMark Fasheh if (!path)
24301e5063d0SMark Fasheh return -ENOMEM;
2431e02119d5SChris Mason
2432e02119d5SChris Mason nritems = btrfs_header_nritems(eb);
2433e02119d5SChris Mason for (i = 0; i < nritems; i++) {
2434e02119d5SChris Mason btrfs_item_key_to_cpu(eb, &key, i);
2435e02119d5SChris Mason
2436e02119d5SChris Mason /* inode keys are done during the first stage */
2437e02119d5SChris Mason if (key.type == BTRFS_INODE_ITEM_KEY &&
2438e02119d5SChris Mason wc->stage == LOG_WALK_REPLAY_INODES) {
2439e02119d5SChris Mason struct btrfs_inode_item *inode_item;
2440e02119d5SChris Mason u32 mode;
2441e02119d5SChris Mason
2442e02119d5SChris Mason inode_item = btrfs_item_ptr(eb, i,
2443e02119d5SChris Mason struct btrfs_inode_item);
2444f2d72f42SFilipe Manana /*
2445f2d72f42SFilipe Manana * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2446f2d72f42SFilipe Manana * and never got linked before the fsync, skip it, as
2447f2d72f42SFilipe Manana * replaying it is pointless since it would be deleted
2448f2d72f42SFilipe Manana * later. We skip logging tmpfiles, but it's always
2449f2d72f42SFilipe Manana * possible we are replaying a log created with a kernel
2450f2d72f42SFilipe Manana * that used to log tmpfiles.
2451f2d72f42SFilipe Manana */
2452f2d72f42SFilipe Manana if (btrfs_inode_nlink(eb, inode_item) == 0) {
2453f2d72f42SFilipe Manana wc->ignore_cur_inode = true;
2454f2d72f42SFilipe Manana continue;
2455f2d72f42SFilipe Manana } else {
2456f2d72f42SFilipe Manana wc->ignore_cur_inode = false;
2457f2d72f42SFilipe Manana }
24584f764e51SFilipe Manana ret = replay_xattr_deletes(wc->trans, root, log,
24594f764e51SFilipe Manana path, key.objectid);
24604f764e51SFilipe Manana if (ret)
24614f764e51SFilipe Manana break;
2462e02119d5SChris Mason mode = btrfs_inode_mode(eb, inode_item);
2463e02119d5SChris Mason if (S_ISDIR(mode)) {
2464e02119d5SChris Mason ret = replay_dir_deletes(wc->trans,
246512fcfd22SChris Mason root, log, path, key.objectid, 0);
2466b50c6e25SJosef Bacik if (ret)
2467b50c6e25SJosef Bacik break;
2468e02119d5SChris Mason }
2469e02119d5SChris Mason ret = overwrite_item(wc->trans, root, path,
2470e02119d5SChris Mason eb, i, &key);
2471b50c6e25SJosef Bacik if (ret)
2472b50c6e25SJosef Bacik break;
2473e02119d5SChris Mason
2474471d557aSFilipe Manana /*
2475471d557aSFilipe Manana * Before replaying extents, truncate the inode to its
2476471d557aSFilipe Manana * size. We need to do it now and not after log replay
2477471d557aSFilipe Manana * because before an fsync we can have prealloc extents
2478471d557aSFilipe Manana * added beyond the inode's i_size. If we did it after,
2479471d557aSFilipe Manana * through orphan cleanup for example, we would drop
2480471d557aSFilipe Manana * those prealloc extents just after replaying them.
2481e02119d5SChris Mason */
2482e02119d5SChris Mason if (S_ISREG(mode)) {
24835893dfb9SFilipe Manana struct btrfs_drop_extents_args drop_args = { 0 };
2484471d557aSFilipe Manana struct inode *inode;
2485471d557aSFilipe Manana u64 from;
2486471d557aSFilipe Manana
2487471d557aSFilipe Manana inode = read_one_inode(root, key.objectid);
2488471d557aSFilipe Manana if (!inode) {
2489471d557aSFilipe Manana ret = -EIO;
2490471d557aSFilipe Manana break;
2491471d557aSFilipe Manana }
2492471d557aSFilipe Manana from = ALIGN(i_size_read(inode),
2493471d557aSFilipe Manana root->fs_info->sectorsize);
24945893dfb9SFilipe Manana drop_args.start = from;
24955893dfb9SFilipe Manana drop_args.end = (u64)-1;
24965893dfb9SFilipe Manana drop_args.drop_cache = true;
24975893dfb9SFilipe Manana ret = btrfs_drop_extents(wc->trans, root,
24985893dfb9SFilipe Manana BTRFS_I(inode),
24995893dfb9SFilipe Manana &drop_args);
2500471d557aSFilipe Manana if (!ret) {
25012766ff61SFilipe Manana inode_sub_bytes(inode,
25022766ff61SFilipe Manana drop_args.bytes_found);
2503f2d72f42SFilipe Manana /* Update the inode's nbytes. */
2504471d557aSFilipe Manana ret = btrfs_update_inode(wc->trans,
25059a56fcd1SNikolay Borisov root, BTRFS_I(inode));
2506471d557aSFilipe Manana }
2507471d557aSFilipe Manana iput(inode);
2508b50c6e25SJosef Bacik if (ret)
2509b50c6e25SJosef Bacik break;
2510c71bf099SYan, Zheng }
2511a74ac322SChris Mason
2512e02119d5SChris Mason ret = link_to_fixup_dir(wc->trans, root,
2513e02119d5SChris Mason path, key.objectid);
2514b50c6e25SJosef Bacik if (ret)
2515b50c6e25SJosef Bacik break;
2516e02119d5SChris Mason }
2517dd8e7217SJosef Bacik
2518f2d72f42SFilipe Manana if (wc->ignore_cur_inode)
2519f2d72f42SFilipe Manana continue;
2520f2d72f42SFilipe Manana
2521dd8e7217SJosef Bacik if (key.type == BTRFS_DIR_INDEX_KEY &&
2522dd8e7217SJosef Bacik wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2523dd8e7217SJosef Bacik ret = replay_one_dir_item(wc->trans, root, path,
2524dd8e7217SJosef Bacik eb, i, &key);
2525dd8e7217SJosef Bacik if (ret)
2526dd8e7217SJosef Bacik break;
2527dd8e7217SJosef Bacik }
2528dd8e7217SJosef Bacik
2529e02119d5SChris Mason if (wc->stage < LOG_WALK_REPLAY_ALL)
2530e02119d5SChris Mason continue;
2531e02119d5SChris Mason
2532e02119d5SChris Mason /* these keys are simply copied */
2533e02119d5SChris Mason if (key.type == BTRFS_XATTR_ITEM_KEY) {
2534e02119d5SChris Mason ret = overwrite_item(wc->trans, root, path,
2535e02119d5SChris Mason eb, i, &key);
2536b50c6e25SJosef Bacik if (ret)
2537b50c6e25SJosef Bacik break;
25382da1c669SLiu Bo } else if (key.type == BTRFS_INODE_REF_KEY ||
25392da1c669SLiu Bo key.type == BTRFS_INODE_EXTREF_KEY) {
2540f186373fSMark Fasheh ret = add_inode_ref(wc->trans, root, log, path,
2541f186373fSMark Fasheh eb, i, &key);
2542b50c6e25SJosef Bacik if (ret && ret != -ENOENT)
2543b50c6e25SJosef Bacik break;
2544b50c6e25SJosef Bacik ret = 0;
2545e02119d5SChris Mason } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2546e02119d5SChris Mason ret = replay_one_extent(wc->trans, root, path,
2547e02119d5SChris Mason eb, i, &key);
2548b50c6e25SJosef Bacik if (ret)
2549b50c6e25SJosef Bacik break;
2550e02119d5SChris Mason }
2551339d0354SFilipe Manana /*
2552339d0354SFilipe Manana * We don't log BTRFS_DIR_ITEM_KEY keys anymore, only the
2553339d0354SFilipe Manana * BTRFS_DIR_INDEX_KEY items which we use to derive the
2554339d0354SFilipe Manana * BTRFS_DIR_ITEM_KEY items. If we are replaying a log from an
2555339d0354SFilipe Manana * older kernel with such keys, ignore them.
2556339d0354SFilipe Manana */
2557e02119d5SChris Mason }
2558e02119d5SChris Mason btrfs_free_path(path);
2559b50c6e25SJosef Bacik return ret;
2560e02119d5SChris Mason }
2561e02119d5SChris Mason
25626787bb9fSNikolay Borisov /*
25636787bb9fSNikolay Borisov * Correctly adjust the reserved bytes occupied by a log tree extent buffer
25646787bb9fSNikolay Borisov */
unaccount_log_buffer(struct btrfs_fs_info * fs_info,u64 start)25656787bb9fSNikolay Borisov static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
25666787bb9fSNikolay Borisov {
25676787bb9fSNikolay Borisov struct btrfs_block_group *cache;
25686787bb9fSNikolay Borisov
25696787bb9fSNikolay Borisov cache = btrfs_lookup_block_group(fs_info, start);
25706787bb9fSNikolay Borisov if (!cache) {
25716787bb9fSNikolay Borisov btrfs_err(fs_info, "unable to find block group for %llu", start);
25726787bb9fSNikolay Borisov return;
25736787bb9fSNikolay Borisov }
25746787bb9fSNikolay Borisov
25756787bb9fSNikolay Borisov spin_lock(&cache->space_info->lock);
25766787bb9fSNikolay Borisov spin_lock(&cache->lock);
25776787bb9fSNikolay Borisov cache->reserved -= fs_info->nodesize;
25786787bb9fSNikolay Borisov cache->space_info->bytes_reserved -= fs_info->nodesize;
25796787bb9fSNikolay Borisov spin_unlock(&cache->lock);
25806787bb9fSNikolay Borisov spin_unlock(&cache->space_info->lock);
25816787bb9fSNikolay Borisov
25826787bb9fSNikolay Borisov btrfs_put_block_group(cache);
25836787bb9fSNikolay Borisov }
25846787bb9fSNikolay Borisov
clean_log_buffer(struct btrfs_trans_handle * trans,struct extent_buffer * eb)2585e6b430f8SChristoph Hellwig static int clean_log_buffer(struct btrfs_trans_handle *trans,
2586e6b430f8SChristoph Hellwig struct extent_buffer *eb)
2587e6b430f8SChristoph Hellwig {
2588e6b430f8SChristoph Hellwig int ret;
2589e6b430f8SChristoph Hellwig
2590e6b430f8SChristoph Hellwig btrfs_tree_lock(eb);
2591e6b430f8SChristoph Hellwig btrfs_clear_buffer_dirty(trans, eb);
2592e6b430f8SChristoph Hellwig wait_on_extent_buffer_writeback(eb);
2593e6b430f8SChristoph Hellwig btrfs_tree_unlock(eb);
2594e6b430f8SChristoph Hellwig
2595e6b430f8SChristoph Hellwig if (trans) {
2596e6b430f8SChristoph Hellwig ret = btrfs_pin_reserved_extent(trans, eb->start, eb->len);
2597e6b430f8SChristoph Hellwig if (ret)
2598e6b430f8SChristoph Hellwig return ret;
2599e6b430f8SChristoph Hellwig btrfs_redirty_list_add(trans->transaction, eb);
2600e6b430f8SChristoph Hellwig } else {
2601e6b430f8SChristoph Hellwig unaccount_log_buffer(eb->fs_info, eb->start);
2602e6b430f8SChristoph Hellwig }
2603e6b430f8SChristoph Hellwig
2604e6b430f8SChristoph Hellwig return 0;
2605e6b430f8SChristoph Hellwig }
2606e6b430f8SChristoph Hellwig
walk_down_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int * level,struct walk_control * wc)2607d397712bSChris Mason static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2608e02119d5SChris Mason struct btrfs_root *root,
2609e02119d5SChris Mason struct btrfs_path *path, int *level,
2610e02119d5SChris Mason struct walk_control *wc)
2611e02119d5SChris Mason {
26120b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
2613e02119d5SChris Mason u64 bytenr;
2614e02119d5SChris Mason u64 ptr_gen;
2615e02119d5SChris Mason struct extent_buffer *next;
2616e02119d5SChris Mason struct extent_buffer *cur;
2617e02119d5SChris Mason int ret = 0;
2618e02119d5SChris Mason
2619e02119d5SChris Mason while (*level > 0) {
2620789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 };
2621581c1760SQu Wenruo
2622e02119d5SChris Mason cur = path->nodes[*level];
2623e02119d5SChris Mason
2624fae7f21cSDulshani Gunawardhana WARN_ON(btrfs_header_level(cur) != *level);
2625e02119d5SChris Mason
2626e02119d5SChris Mason if (path->slots[*level] >=
2627e02119d5SChris Mason btrfs_header_nritems(cur))
2628e02119d5SChris Mason break;
2629e02119d5SChris Mason
2630e02119d5SChris Mason bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2631e02119d5SChris Mason ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2632789d6a3aSQu Wenruo check.transid = ptr_gen;
2633789d6a3aSQu Wenruo check.level = *level - 1;
2634789d6a3aSQu Wenruo check.has_first_key = true;
2635789d6a3aSQu Wenruo btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]);
2636e02119d5SChris Mason
26373fbaf258SJosef Bacik next = btrfs_find_create_tree_block(fs_info, bytenr,
26383fbaf258SJosef Bacik btrfs_header_owner(cur),
26393fbaf258SJosef Bacik *level - 1);
2640c871b0f2SLiu Bo if (IS_ERR(next))
2641c871b0f2SLiu Bo return PTR_ERR(next);
2642e02119d5SChris Mason
26434a500fd1SYan, Zheng if (*level == 1) {
2644581c1760SQu Wenruo ret = wc->process_func(root, next, wc, ptr_gen,
2645581c1760SQu Wenruo *level - 1);
2646b50c6e25SJosef Bacik if (ret) {
2647b50c6e25SJosef Bacik free_extent_buffer(next);
26481e5063d0SMark Fasheh return ret;
2649b50c6e25SJosef Bacik }
2650e02119d5SChris Mason
2651e02119d5SChris Mason path->slots[*level]++;
2652e02119d5SChris Mason if (wc->free) {
2653789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(next, &check);
2654018642a1STsutomu Itoh if (ret) {
2655018642a1STsutomu Itoh free_extent_buffer(next);
2656018642a1STsutomu Itoh return ret;
2657018642a1STsutomu Itoh }
2658e02119d5SChris Mason
2659e6b430f8SChristoph Hellwig ret = clean_log_buffer(trans, next);
26603650860bSJosef Bacik if (ret) {
26613650860bSJosef Bacik free_extent_buffer(next);
26623650860bSJosef Bacik return ret;
26633650860bSJosef Bacik }
2664e02119d5SChris Mason }
2665e02119d5SChris Mason free_extent_buffer(next);
2666e02119d5SChris Mason continue;
2667e02119d5SChris Mason }
2668789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(next, &check);
2669018642a1STsutomu Itoh if (ret) {
2670018642a1STsutomu Itoh free_extent_buffer(next);
2671018642a1STsutomu Itoh return ret;
2672018642a1STsutomu Itoh }
2673e02119d5SChris Mason
2674e02119d5SChris Mason if (path->nodes[*level-1])
2675e02119d5SChris Mason free_extent_buffer(path->nodes[*level-1]);
2676e02119d5SChris Mason path->nodes[*level-1] = next;
2677e02119d5SChris Mason *level = btrfs_header_level(next);
2678e02119d5SChris Mason path->slots[*level] = 0;
2679e02119d5SChris Mason cond_resched();
2680e02119d5SChris Mason }
26814a500fd1SYan, Zheng path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2682e02119d5SChris Mason
2683e02119d5SChris Mason cond_resched();
2684e02119d5SChris Mason return 0;
2685e02119d5SChris Mason }
2686e02119d5SChris Mason
walk_up_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int * level,struct walk_control * wc)2687d397712bSChris Mason static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2688e02119d5SChris Mason struct btrfs_root *root,
2689e02119d5SChris Mason struct btrfs_path *path, int *level,
2690e02119d5SChris Mason struct walk_control *wc)
2691e02119d5SChris Mason {
2692e02119d5SChris Mason int i;
2693e02119d5SChris Mason int slot;
2694e02119d5SChris Mason int ret;
2695e02119d5SChris Mason
2696e02119d5SChris Mason for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2697e02119d5SChris Mason slot = path->slots[i];
26984a500fd1SYan, Zheng if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2699e02119d5SChris Mason path->slots[i]++;
2700e02119d5SChris Mason *level = i;
2701e02119d5SChris Mason WARN_ON(*level == 0);
2702e02119d5SChris Mason return 0;
2703e02119d5SChris Mason } else {
27041e5063d0SMark Fasheh ret = wc->process_func(root, path->nodes[*level], wc,
2705581c1760SQu Wenruo btrfs_header_generation(path->nodes[*level]),
2706581c1760SQu Wenruo *level);
27071e5063d0SMark Fasheh if (ret)
27081e5063d0SMark Fasheh return ret;
27091e5063d0SMark Fasheh
2710e02119d5SChris Mason if (wc->free) {
2711e6b430f8SChristoph Hellwig ret = clean_log_buffer(trans, path->nodes[*level]);
27123650860bSJosef Bacik if (ret)
27133650860bSJosef Bacik return ret;
2714e02119d5SChris Mason }
2715e02119d5SChris Mason free_extent_buffer(path->nodes[*level]);
2716e02119d5SChris Mason path->nodes[*level] = NULL;
2717e02119d5SChris Mason *level = i + 1;
2718e02119d5SChris Mason }
2719e02119d5SChris Mason }
2720e02119d5SChris Mason return 1;
2721e02119d5SChris Mason }
2722e02119d5SChris Mason
2723e02119d5SChris Mason /*
2724e02119d5SChris Mason * drop the reference count on the tree rooted at 'snap'. This traverses
2725e02119d5SChris Mason * the tree freeing any blocks that have a ref count of zero after being
2726e02119d5SChris Mason * decremented.
2727e02119d5SChris Mason */
walk_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct walk_control * wc)2728e02119d5SChris Mason static int walk_log_tree(struct btrfs_trans_handle *trans,
2729e02119d5SChris Mason struct btrfs_root *log, struct walk_control *wc)
2730e02119d5SChris Mason {
2731e02119d5SChris Mason int ret = 0;
2732e02119d5SChris Mason int wret;
2733e02119d5SChris Mason int level;
2734e02119d5SChris Mason struct btrfs_path *path;
2735e02119d5SChris Mason int orig_level;
2736e02119d5SChris Mason
2737e02119d5SChris Mason path = btrfs_alloc_path();
2738db5b493aSTsutomu Itoh if (!path)
2739db5b493aSTsutomu Itoh return -ENOMEM;
2740e02119d5SChris Mason
2741e02119d5SChris Mason level = btrfs_header_level(log->node);
2742e02119d5SChris Mason orig_level = level;
2743e02119d5SChris Mason path->nodes[level] = log->node;
274467439dadSDavid Sterba atomic_inc(&log->node->refs);
2745e02119d5SChris Mason path->slots[level] = 0;
2746e02119d5SChris Mason
2747e02119d5SChris Mason while (1) {
2748e02119d5SChris Mason wret = walk_down_log_tree(trans, log, path, &level, wc);
2749e02119d5SChris Mason if (wret > 0)
2750e02119d5SChris Mason break;
275179787eaaSJeff Mahoney if (wret < 0) {
2752e02119d5SChris Mason ret = wret;
275379787eaaSJeff Mahoney goto out;
275479787eaaSJeff Mahoney }
2755e02119d5SChris Mason
2756e02119d5SChris Mason wret = walk_up_log_tree(trans, log, path, &level, wc);
2757e02119d5SChris Mason if (wret > 0)
2758e02119d5SChris Mason break;
275979787eaaSJeff Mahoney if (wret < 0) {
2760e02119d5SChris Mason ret = wret;
276179787eaaSJeff Mahoney goto out;
276279787eaaSJeff Mahoney }
2763e02119d5SChris Mason }
2764e02119d5SChris Mason
2765e02119d5SChris Mason /* was the root node processed? if not, catch it here */
2766e02119d5SChris Mason if (path->nodes[orig_level]) {
276779787eaaSJeff Mahoney ret = wc->process_func(log, path->nodes[orig_level], wc,
2768581c1760SQu Wenruo btrfs_header_generation(path->nodes[orig_level]),
2769581c1760SQu Wenruo orig_level);
277079787eaaSJeff Mahoney if (ret)
277179787eaaSJeff Mahoney goto out;
2772e6b430f8SChristoph Hellwig if (wc->free)
2773e6b430f8SChristoph Hellwig ret = clean_log_buffer(trans, path->nodes[orig_level]);
2774e02119d5SChris Mason }
2775e02119d5SChris Mason
277679787eaaSJeff Mahoney out:
2777e02119d5SChris Mason btrfs_free_path(path);
2778e02119d5SChris Mason return ret;
2779e02119d5SChris Mason }
2780e02119d5SChris Mason
27817237f183SYan Zheng /*
27827237f183SYan Zheng * helper function to update the item for a given subvolumes log root
27837237f183SYan Zheng * in the tree of log roots
27847237f183SYan Zheng */
update_log_root(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_root_item * root_item)27857237f183SYan Zheng static int update_log_root(struct btrfs_trans_handle *trans,
27864203e968SJosef Bacik struct btrfs_root *log,
27874203e968SJosef Bacik struct btrfs_root_item *root_item)
27887237f183SYan Zheng {
27890b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = log->fs_info;
27907237f183SYan Zheng int ret;
27917237f183SYan Zheng
27927237f183SYan Zheng if (log->log_transid == 1) {
27937237f183SYan Zheng /* insert root item on the first sync */
27940b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->log_root_tree,
27954203e968SJosef Bacik &log->root_key, root_item);
27967237f183SYan Zheng } else {
27970b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->log_root_tree,
27984203e968SJosef Bacik &log->root_key, root_item);
27997237f183SYan Zheng }
28007237f183SYan Zheng return ret;
28017237f183SYan Zheng }
28027237f183SYan Zheng
wait_log_commit(struct btrfs_root * root,int transid)280360d53eb3SZhaolei static void wait_log_commit(struct btrfs_root *root, int transid)
2804e02119d5SChris Mason {
2805e02119d5SChris Mason DEFINE_WAIT(wait);
28067237f183SYan Zheng int index = transid % 2;
2807e02119d5SChris Mason
28087237f183SYan Zheng /*
28097237f183SYan Zheng * we only allow two pending log transactions at a time,
28107237f183SYan Zheng * so we know that if ours is more than 2 older than the
28117237f183SYan Zheng * current transaction, we're done
28127237f183SYan Zheng */
281349e83f57SLiu Bo for (;;) {
28147237f183SYan Zheng prepare_to_wait(&root->log_commit_wait[index],
28157237f183SYan Zheng &wait, TASK_UNINTERRUPTIBLE);
281649e83f57SLiu Bo
281749e83f57SLiu Bo if (!(root->log_transid_committed < transid &&
281849e83f57SLiu Bo atomic_read(&root->log_commit[index])))
281949e83f57SLiu Bo break;
282049e83f57SLiu Bo
28217237f183SYan Zheng mutex_unlock(&root->log_mutex);
2822e02119d5SChris Mason schedule();
28237237f183SYan Zheng mutex_lock(&root->log_mutex);
282449e83f57SLiu Bo }
282549e83f57SLiu Bo finish_wait(&root->log_commit_wait[index], &wait);
28267237f183SYan Zheng }
28277237f183SYan Zheng
wait_for_writer(struct btrfs_root * root)282860d53eb3SZhaolei static void wait_for_writer(struct btrfs_root *root)
28297237f183SYan Zheng {
28307237f183SYan Zheng DEFINE_WAIT(wait);
28318b050d35SMiao Xie
283249e83f57SLiu Bo for (;;) {
283349e83f57SLiu Bo prepare_to_wait(&root->log_writer_wait, &wait,
283449e83f57SLiu Bo TASK_UNINTERRUPTIBLE);
283549e83f57SLiu Bo if (!atomic_read(&root->log_writers))
283649e83f57SLiu Bo break;
283749e83f57SLiu Bo
28387237f183SYan Zheng mutex_unlock(&root->log_mutex);
28397237f183SYan Zheng schedule();
2840575849ecSFilipe Manana mutex_lock(&root->log_mutex);
28417237f183SYan Zheng }
284249e83f57SLiu Bo finish_wait(&root->log_writer_wait, &wait);
2843e02119d5SChris Mason }
2844e02119d5SChris Mason
btrfs_remove_log_ctx(struct btrfs_root * root,struct btrfs_log_ctx * ctx)28458b050d35SMiao Xie static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
28468b050d35SMiao Xie struct btrfs_log_ctx *ctx)
28478b050d35SMiao Xie {
28488b050d35SMiao Xie mutex_lock(&root->log_mutex);
28498b050d35SMiao Xie list_del_init(&ctx->list);
28508b050d35SMiao Xie mutex_unlock(&root->log_mutex);
28518b050d35SMiao Xie }
28528b050d35SMiao Xie
28538b050d35SMiao Xie /*
28548b050d35SMiao Xie * Invoked in log mutex context, or be sure there is no other task which
28558b050d35SMiao Xie * can access the list.
28568b050d35SMiao Xie */
btrfs_remove_all_log_ctxs(struct btrfs_root * root,int index,int error)28578b050d35SMiao Xie static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
28588b050d35SMiao Xie int index, int error)
28598b050d35SMiao Xie {
28608b050d35SMiao Xie struct btrfs_log_ctx *ctx;
2861570dd450SChris Mason struct btrfs_log_ctx *safe;
28628b050d35SMiao Xie
2863570dd450SChris Mason list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2864570dd450SChris Mason list_del_init(&ctx->list);
28658b050d35SMiao Xie ctx->log_ret = error;
2866570dd450SChris Mason }
28678b050d35SMiao Xie }
28688b050d35SMiao Xie
2869e02119d5SChris Mason /*
2870e02119d5SChris Mason * btrfs_sync_log does sends a given tree log down to the disk and
2871e02119d5SChris Mason * updates the super blocks to record it. When this call is done,
287212fcfd22SChris Mason * you know that any inodes previously logged are safely on disk only
287312fcfd22SChris Mason * if it returns 0.
287412fcfd22SChris Mason *
287512fcfd22SChris Mason * Any other return value means you need to call btrfs_commit_transaction.
287612fcfd22SChris Mason * Some of the edge cases for fsyncing directories that have had unlinks
287712fcfd22SChris Mason * or renames done in the past mean that sometimes the only safe
287812fcfd22SChris Mason * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
287912fcfd22SChris Mason * that has happened.
2880e02119d5SChris Mason */
btrfs_sync_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_log_ctx * ctx)2881e02119d5SChris Mason int btrfs_sync_log(struct btrfs_trans_handle *trans,
28828b050d35SMiao Xie struct btrfs_root *root, struct btrfs_log_ctx *ctx)
2883e02119d5SChris Mason {
28847237f183SYan Zheng int index1;
28857237f183SYan Zheng int index2;
28868cef4e16SYan, Zheng int mark;
2887e02119d5SChris Mason int ret;
28880b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
2889e02119d5SChris Mason struct btrfs_root *log = root->log_root;
28900b246afaSJeff Mahoney struct btrfs_root *log_root_tree = fs_info->log_root_tree;
28914203e968SJosef Bacik struct btrfs_root_item new_root_item;
2892bb14a59bSMiao Xie int log_transid = 0;
28938b050d35SMiao Xie struct btrfs_log_ctx root_log_ctx;
2894c6adc9ccSMiao Xie struct blk_plug plug;
289547876f7cSFilipe Manana u64 log_root_start;
289647876f7cSFilipe Manana u64 log_root_level;
2897e02119d5SChris Mason
28987237f183SYan Zheng mutex_lock(&root->log_mutex);
2899d1433debSMiao Xie log_transid = ctx->log_transid;
2900d1433debSMiao Xie if (root->log_transid_committed >= log_transid) {
29017237f183SYan Zheng mutex_unlock(&root->log_mutex);
29028b050d35SMiao Xie return ctx->log_ret;
2903e02119d5SChris Mason }
2904d1433debSMiao Xie
2905d1433debSMiao Xie index1 = log_transid % 2;
2906d1433debSMiao Xie if (atomic_read(&root->log_commit[index1])) {
290760d53eb3SZhaolei wait_log_commit(root, log_transid);
2908d1433debSMiao Xie mutex_unlock(&root->log_mutex);
2909d1433debSMiao Xie return ctx->log_ret;
2910d1433debSMiao Xie }
2911d1433debSMiao Xie ASSERT(log_transid == root->log_transid);
29127237f183SYan Zheng atomic_set(&root->log_commit[index1], 1);
29137237f183SYan Zheng
29147237f183SYan Zheng /* wait for previous tree log sync to complete */
29157237f183SYan Zheng if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
291660d53eb3SZhaolei wait_log_commit(root, log_transid - 1);
291748cab2e0SMiao Xie
291886df7eb9SYan, Zheng while (1) {
29192ecb7923SMiao Xie int batch = atomic_read(&root->log_batch);
2920cd354ad6SChris Mason /* when we're on an ssd, just kick the log commit out */
29210b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, SSD) &&
292227cdeb70SMiao Xie test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
29237237f183SYan Zheng mutex_unlock(&root->log_mutex);
2924e02119d5SChris Mason schedule_timeout_uninterruptible(1);
29257237f183SYan Zheng mutex_lock(&root->log_mutex);
292686df7eb9SYan, Zheng }
292760d53eb3SZhaolei wait_for_writer(root);
29282ecb7923SMiao Xie if (batch == atomic_read(&root->log_batch))
2929e02119d5SChris Mason break;
2930e02119d5SChris Mason }
2931d0c803c4SChris Mason
293212fcfd22SChris Mason /* bail out if we need to do a full commit */
29334884b8e8SDavid Sterba if (btrfs_need_log_full_commit(trans)) {
2934f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
293512fcfd22SChris Mason mutex_unlock(&root->log_mutex);
293612fcfd22SChris Mason goto out;
293712fcfd22SChris Mason }
293812fcfd22SChris Mason
29398cef4e16SYan, Zheng if (log_transid % 2 == 0)
29408cef4e16SYan, Zheng mark = EXTENT_DIRTY;
29418cef4e16SYan, Zheng else
29428cef4e16SYan, Zheng mark = EXTENT_NEW;
29438cef4e16SYan, Zheng
2944690587d1SChris Mason /* we start IO on all the marked extents here, but we don't actually
2945690587d1SChris Mason * wait for them until later.
2946690587d1SChris Mason */
2947c6adc9ccSMiao Xie blk_start_plug(&plug);
29482ff7e61eSJeff Mahoney ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
2949b528f467SNaohiro Aota /*
2950b528f467SNaohiro Aota * -EAGAIN happens when someone, e.g., a concurrent transaction
2951b528f467SNaohiro Aota * commit, writes a dirty extent in this tree-log commit. This
2952b528f467SNaohiro Aota * concurrent write will create a hole writing out the extents,
2953b528f467SNaohiro Aota * and we cannot proceed on a zoned filesystem, requiring
2954b528f467SNaohiro Aota * sequential writing. While we can bail out to a full commit
2955b528f467SNaohiro Aota * here, but we can continue hoping the concurrent writing fills
2956b528f467SNaohiro Aota * the hole.
2957b528f467SNaohiro Aota */
2958b528f467SNaohiro Aota if (ret == -EAGAIN && btrfs_is_zoned(fs_info))
2959b528f467SNaohiro Aota ret = 0;
296079787eaaSJeff Mahoney if (ret) {
2961c6adc9ccSMiao Xie blk_finish_plug(&plug);
296290787766SDavid Sterba btrfs_set_log_full_commit(trans);
296379787eaaSJeff Mahoney mutex_unlock(&root->log_mutex);
296479787eaaSJeff Mahoney goto out;
296579787eaaSJeff Mahoney }
29667237f183SYan Zheng
29674203e968SJosef Bacik /*
29684203e968SJosef Bacik * We _must_ update under the root->log_mutex in order to make sure we
29694203e968SJosef Bacik * have a consistent view of the log root we are trying to commit at
29704203e968SJosef Bacik * this moment.
29714203e968SJosef Bacik *
29724203e968SJosef Bacik * We _must_ copy this into a local copy, because we are not holding the
29734203e968SJosef Bacik * log_root_tree->log_mutex yet. This is important because when we
29744203e968SJosef Bacik * commit the log_root_tree we must have a consistent view of the
29754203e968SJosef Bacik * log_root_tree when we update the super block to point at the
29764203e968SJosef Bacik * log_root_tree bytenr. If we update the log_root_tree here we'll race
29774203e968SJosef Bacik * with the commit and possibly point at the new block which we may not
29784203e968SJosef Bacik * have written out.
29794203e968SJosef Bacik */
29805d4f98a2SYan Zheng btrfs_set_root_node(&log->root_item, log->node);
29814203e968SJosef Bacik memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
29827237f183SYan Zheng
29837237f183SYan Zheng root->log_transid++;
29847237f183SYan Zheng log->log_transid = root->log_transid;
2985ff782e0aSJosef Bacik root->log_start_pid = 0;
29867237f183SYan Zheng /*
29878cef4e16SYan, Zheng * IO has been started, blocks of the log tree have WRITTEN flag set
29888cef4e16SYan, Zheng * in their headers. new modifications of the log will be written to
29898cef4e16SYan, Zheng * new positions. so it's safe to allow log writers to go in.
29907237f183SYan Zheng */
29917237f183SYan Zheng mutex_unlock(&root->log_mutex);
29927237f183SYan Zheng
29933ddebf27SNaohiro Aota if (btrfs_is_zoned(fs_info)) {
2994e75f9fd1SNaohiro Aota mutex_lock(&fs_info->tree_root->log_mutex);
29953ddebf27SNaohiro Aota if (!log_root_tree->node) {
29963ddebf27SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
29973ddebf27SNaohiro Aota if (ret) {
2998ea32af47SFilipe Manana mutex_unlock(&fs_info->tree_root->log_mutex);
299950ff5788SFilipe Manana blk_finish_plug(&plug);
30003ddebf27SNaohiro Aota goto out;
30013ddebf27SNaohiro Aota }
30023ddebf27SNaohiro Aota }
3003e75f9fd1SNaohiro Aota mutex_unlock(&fs_info->tree_root->log_mutex);
30043ddebf27SNaohiro Aota }
30053ddebf27SNaohiro Aota
3006e75f9fd1SNaohiro Aota btrfs_init_log_ctx(&root_log_ctx, NULL);
3007e75f9fd1SNaohiro Aota
3008e75f9fd1SNaohiro Aota mutex_lock(&log_root_tree->log_mutex);
3009e75f9fd1SNaohiro Aota
3010e3d3b415SFilipe Manana index2 = log_root_tree->log_transid % 2;
3011e3d3b415SFilipe Manana list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3012e3d3b415SFilipe Manana root_log_ctx.log_transid = log_root_tree->log_transid;
3013e3d3b415SFilipe Manana
30144203e968SJosef Bacik /*
30154203e968SJosef Bacik * Now we are safe to update the log_root_tree because we're under the
30164203e968SJosef Bacik * log_mutex, and we're a current writer so we're holding the commit
30174203e968SJosef Bacik * open until we drop the log_mutex.
30184203e968SJosef Bacik */
30194203e968SJosef Bacik ret = update_log_root(trans, log, &new_root_item);
30204a500fd1SYan, Zheng if (ret) {
3021d1433debSMiao Xie if (!list_empty(&root_log_ctx.list))
3022d1433debSMiao Xie list_del_init(&root_log_ctx.list);
3023d1433debSMiao Xie
3024c6adc9ccSMiao Xie blk_finish_plug(&plug);
302590787766SDavid Sterba btrfs_set_log_full_commit(trans);
302609e44868SFilipe Manana if (ret != -ENOSPC)
302709e44868SFilipe Manana btrfs_err(fs_info,
302809e44868SFilipe Manana "failed to update log for root %llu ret %d",
302909e44868SFilipe Manana root->root_key.objectid, ret);
3030bf89d38fSJeff Mahoney btrfs_wait_tree_log_extents(log, mark);
30314a500fd1SYan, Zheng mutex_unlock(&log_root_tree->log_mutex);
30324a500fd1SYan, Zheng goto out;
30334a500fd1SYan, Zheng }
30344a500fd1SYan, Zheng
3035d1433debSMiao Xie if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
30363da5ab56SForrest Liu blk_finish_plug(&plug);
3037cbd60aa7SChris Mason list_del_init(&root_log_ctx.list);
3038d1433debSMiao Xie mutex_unlock(&log_root_tree->log_mutex);
3039d1433debSMiao Xie ret = root_log_ctx.log_ret;
3040d1433debSMiao Xie goto out;
3041d1433debSMiao Xie }
30428b050d35SMiao Xie
3043d1433debSMiao Xie index2 = root_log_ctx.log_transid % 2;
30447237f183SYan Zheng if (atomic_read(&log_root_tree->log_commit[index2])) {
3045c6adc9ccSMiao Xie blk_finish_plug(&plug);
3046bf89d38fSJeff Mahoney ret = btrfs_wait_tree_log_extents(log, mark);
304760d53eb3SZhaolei wait_log_commit(log_root_tree,
3048d1433debSMiao Xie root_log_ctx.log_transid);
30497237f183SYan Zheng mutex_unlock(&log_root_tree->log_mutex);
30505ab5e44aSFilipe Manana if (!ret)
30518b050d35SMiao Xie ret = root_log_ctx.log_ret;
30527237f183SYan Zheng goto out;
30537237f183SYan Zheng }
3054d1433debSMiao Xie ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
30557237f183SYan Zheng atomic_set(&log_root_tree->log_commit[index2], 1);
30567237f183SYan Zheng
305712fcfd22SChris Mason if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
305860d53eb3SZhaolei wait_log_commit(log_root_tree,
3059d1433debSMiao Xie root_log_ctx.log_transid - 1);
306012fcfd22SChris Mason }
30617237f183SYan Zheng
306212fcfd22SChris Mason /*
306312fcfd22SChris Mason * now that we've moved on to the tree of log tree roots,
306412fcfd22SChris Mason * check the full commit flag again
306512fcfd22SChris Mason */
30664884b8e8SDavid Sterba if (btrfs_need_log_full_commit(trans)) {
3067c6adc9ccSMiao Xie blk_finish_plug(&plug);
3068bf89d38fSJeff Mahoney btrfs_wait_tree_log_extents(log, mark);
306912fcfd22SChris Mason mutex_unlock(&log_root_tree->log_mutex);
3070f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
307112fcfd22SChris Mason goto out_wake_log_root;
307212fcfd22SChris Mason }
30737237f183SYan Zheng
30742ff7e61eSJeff Mahoney ret = btrfs_write_marked_extents(fs_info,
30758cef4e16SYan, Zheng &log_root_tree->dirty_log_pages,
30768cef4e16SYan, Zheng EXTENT_DIRTY | EXTENT_NEW);
3077c6adc9ccSMiao Xie blk_finish_plug(&plug);
3078b528f467SNaohiro Aota /*
3079b528f467SNaohiro Aota * As described above, -EAGAIN indicates a hole in the extents. We
3080b528f467SNaohiro Aota * cannot wait for these write outs since the waiting cause a
3081b528f467SNaohiro Aota * deadlock. Bail out to the full commit instead.
3082b528f467SNaohiro Aota */
3083b528f467SNaohiro Aota if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) {
3084b528f467SNaohiro Aota btrfs_set_log_full_commit(trans);
3085b528f467SNaohiro Aota btrfs_wait_tree_log_extents(log, mark);
3086b528f467SNaohiro Aota mutex_unlock(&log_root_tree->log_mutex);
3087b528f467SNaohiro Aota goto out_wake_log_root;
3088b528f467SNaohiro Aota } else if (ret) {
308990787766SDavid Sterba btrfs_set_log_full_commit(trans);
309079787eaaSJeff Mahoney mutex_unlock(&log_root_tree->log_mutex);
309179787eaaSJeff Mahoney goto out_wake_log_root;
309279787eaaSJeff Mahoney }
3093bf89d38fSJeff Mahoney ret = btrfs_wait_tree_log_extents(log, mark);
30945ab5e44aSFilipe Manana if (!ret)
3095bf89d38fSJeff Mahoney ret = btrfs_wait_tree_log_extents(log_root_tree,
3096c6adc9ccSMiao Xie EXTENT_NEW | EXTENT_DIRTY);
30975ab5e44aSFilipe Manana if (ret) {
309890787766SDavid Sterba btrfs_set_log_full_commit(trans);
30995ab5e44aSFilipe Manana mutex_unlock(&log_root_tree->log_mutex);
31005ab5e44aSFilipe Manana goto out_wake_log_root;
31015ab5e44aSFilipe Manana }
3102e02119d5SChris Mason
310347876f7cSFilipe Manana log_root_start = log_root_tree->node->start;
310447876f7cSFilipe Manana log_root_level = btrfs_header_level(log_root_tree->node);
31057237f183SYan Zheng log_root_tree->log_transid++;
31067237f183SYan Zheng mutex_unlock(&log_root_tree->log_mutex);
31077237f183SYan Zheng
31087237f183SYan Zheng /*
310947876f7cSFilipe Manana * Here we are guaranteed that nobody is going to write the superblock
311047876f7cSFilipe Manana * for the current transaction before us and that neither we do write
311147876f7cSFilipe Manana * our superblock before the previous transaction finishes its commit
311247876f7cSFilipe Manana * and writes its superblock, because:
311347876f7cSFilipe Manana *
311447876f7cSFilipe Manana * 1) We are holding a handle on the current transaction, so no body
311547876f7cSFilipe Manana * can commit it until we release the handle;
311647876f7cSFilipe Manana *
311747876f7cSFilipe Manana * 2) Before writing our superblock we acquire the tree_log_mutex, so
311847876f7cSFilipe Manana * if the previous transaction is still committing, and hasn't yet
311947876f7cSFilipe Manana * written its superblock, we wait for it to do it, because a
312047876f7cSFilipe Manana * transaction commit acquires the tree_log_mutex when the commit
312147876f7cSFilipe Manana * begins and releases it only after writing its superblock.
31227237f183SYan Zheng */
312347876f7cSFilipe Manana mutex_lock(&fs_info->tree_log_mutex);
3124165ea85fSJosef Bacik
3125165ea85fSJosef Bacik /*
3126165ea85fSJosef Bacik * The previous transaction writeout phase could have failed, and thus
3127165ea85fSJosef Bacik * marked the fs in an error state. We must not commit here, as we
3128165ea85fSJosef Bacik * could have updated our generation in the super_for_commit and
3129165ea85fSJosef Bacik * writing the super here would result in transid mismatches. If there
3130165ea85fSJosef Bacik * is an error here just bail.
3131165ea85fSJosef Bacik */
313284961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) {
3133165ea85fSJosef Bacik ret = -EIO;
3134165ea85fSJosef Bacik btrfs_set_log_full_commit(trans);
3135165ea85fSJosef Bacik btrfs_abort_transaction(trans, ret);
3136165ea85fSJosef Bacik mutex_unlock(&fs_info->tree_log_mutex);
3137165ea85fSJosef Bacik goto out_wake_log_root;
3138165ea85fSJosef Bacik }
3139165ea85fSJosef Bacik
314047876f7cSFilipe Manana btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
314147876f7cSFilipe Manana btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
3142eece6a9cSDavid Sterba ret = write_all_supers(fs_info, 1);
314347876f7cSFilipe Manana mutex_unlock(&fs_info->tree_log_mutex);
31445af3e8ccSStefan Behrens if (ret) {
314590787766SDavid Sterba btrfs_set_log_full_commit(trans);
314666642832SJeff Mahoney btrfs_abort_transaction(trans, ret);
31475af3e8ccSStefan Behrens goto out_wake_log_root;
31485af3e8ccSStefan Behrens }
31497237f183SYan Zheng
3150e1a6d264SFilipe Manana /*
3151e1a6d264SFilipe Manana * We know there can only be one task here, since we have not yet set
3152e1a6d264SFilipe Manana * root->log_commit[index1] to 0 and any task attempting to sync the
3153e1a6d264SFilipe Manana * log must wait for the previous log transaction to commit if it's
3154e1a6d264SFilipe Manana * still in progress or wait for the current log transaction commit if
3155e1a6d264SFilipe Manana * someone else already started it. We use <= and not < because the
3156e1a6d264SFilipe Manana * first log transaction has an ID of 0.
3157e1a6d264SFilipe Manana */
3158e1a6d264SFilipe Manana ASSERT(root->last_log_commit <= log_transid);
3159257c62e1SChris Mason root->last_log_commit = log_transid;
3160257c62e1SChris Mason
316112fcfd22SChris Mason out_wake_log_root:
3162570dd450SChris Mason mutex_lock(&log_root_tree->log_mutex);
31638b050d35SMiao Xie btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
31648b050d35SMiao Xie
3165d1433debSMiao Xie log_root_tree->log_transid_committed++;
31667237f183SYan Zheng atomic_set(&log_root_tree->log_commit[index2], 0);
3167d1433debSMiao Xie mutex_unlock(&log_root_tree->log_mutex);
3168d1433debSMiao Xie
316933a9eca7SDavid Sterba /*
3170093258e6SDavid Sterba * The barrier before waitqueue_active (in cond_wake_up) is needed so
3171093258e6SDavid Sterba * all the updates above are seen by the woken threads. It might not be
3172093258e6SDavid Sterba * necessary, but proving that seems to be hard.
317333a9eca7SDavid Sterba */
3174093258e6SDavid Sterba cond_wake_up(&log_root_tree->log_commit_wait[index2]);
3175e02119d5SChris Mason out:
3176d1433debSMiao Xie mutex_lock(&root->log_mutex);
3177570dd450SChris Mason btrfs_remove_all_log_ctxs(root, index1, ret);
3178d1433debSMiao Xie root->log_transid_committed++;
31797237f183SYan Zheng atomic_set(&root->log_commit[index1], 0);
3180d1433debSMiao Xie mutex_unlock(&root->log_mutex);
31818b050d35SMiao Xie
318233a9eca7SDavid Sterba /*
3183093258e6SDavid Sterba * The barrier before waitqueue_active (in cond_wake_up) is needed so
3184093258e6SDavid Sterba * all the updates above are seen by the woken threads. It might not be
3185093258e6SDavid Sterba * necessary, but proving that seems to be hard.
318633a9eca7SDavid Sterba */
3187093258e6SDavid Sterba cond_wake_up(&root->log_commit_wait[index1]);
3188b31eabd8SChris Mason return ret;
3189e02119d5SChris Mason }
3190e02119d5SChris Mason
free_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * log)31914a500fd1SYan, Zheng static void free_log_tree(struct btrfs_trans_handle *trans,
31924a500fd1SYan, Zheng struct btrfs_root *log)
3193e02119d5SChris Mason {
3194e02119d5SChris Mason int ret;
3195e02119d5SChris Mason struct walk_control wc = {
3196e02119d5SChris Mason .free = 1,
3197e02119d5SChris Mason .process_func = process_one_buffer
3198e02119d5SChris Mason };
3199e02119d5SChris Mason
32003ddebf27SNaohiro Aota if (log->node) {
3201e02119d5SChris Mason ret = walk_log_tree(trans, log, &wc);
3202374b0e2dSJeff Mahoney if (ret) {
320340cdc509SFilipe Manana /*
320440cdc509SFilipe Manana * We weren't able to traverse the entire log tree, the
320540cdc509SFilipe Manana * typical scenario is getting an -EIO when reading an
320640cdc509SFilipe Manana * extent buffer of the tree, due to a previous writeback
320740cdc509SFilipe Manana * failure of it.
320840cdc509SFilipe Manana */
320940cdc509SFilipe Manana set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
321040cdc509SFilipe Manana &log->fs_info->fs_state);
321140cdc509SFilipe Manana
321240cdc509SFilipe Manana /*
321340cdc509SFilipe Manana * Some extent buffers of the log tree may still be dirty
321440cdc509SFilipe Manana * and not yet written back to storage, because we may
321540cdc509SFilipe Manana * have updates to a log tree without syncing a log tree,
321640cdc509SFilipe Manana * such as during rename and link operations. So flush
321740cdc509SFilipe Manana * them out and wait for their writeback to complete, so
321840cdc509SFilipe Manana * that we properly cleanup their state and pages.
321940cdc509SFilipe Manana */
322040cdc509SFilipe Manana btrfs_write_marked_extents(log->fs_info,
322140cdc509SFilipe Manana &log->dirty_log_pages,
322240cdc509SFilipe Manana EXTENT_DIRTY | EXTENT_NEW);
322340cdc509SFilipe Manana btrfs_wait_tree_log_extents(log,
322440cdc509SFilipe Manana EXTENT_DIRTY | EXTENT_NEW);
322540cdc509SFilipe Manana
3226374b0e2dSJeff Mahoney if (trans)
322766642832SJeff Mahoney btrfs_abort_transaction(trans, ret);
3228374b0e2dSJeff Mahoney else
3229374b0e2dSJeff Mahoney btrfs_handle_fs_error(log->fs_info, ret, NULL);
3230374b0e2dSJeff Mahoney }
32313ddebf27SNaohiro Aota }
3232e02119d5SChris Mason
323359b0713aSFilipe Manana clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
323455237a5fSLiu Bo EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3235e289f03eSFilipe Manana extent_io_tree_release(&log->log_csum_range);
3236d3575156SNaohiro Aota
323700246528SJosef Bacik btrfs_put_root(log);
32384a500fd1SYan, Zheng }
32394a500fd1SYan, Zheng
32404a500fd1SYan, Zheng /*
32414a500fd1SYan, Zheng * free all the extents used by the tree log. This should be called
32424a500fd1SYan, Zheng * at commit time of the full transaction
32434a500fd1SYan, Zheng */
btrfs_free_log(struct btrfs_trans_handle * trans,struct btrfs_root * root)32444a500fd1SYan, Zheng int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
32454a500fd1SYan, Zheng {
32464a500fd1SYan, Zheng if (root->log_root) {
32474a500fd1SYan, Zheng free_log_tree(trans, root->log_root);
32484a500fd1SYan, Zheng root->log_root = NULL;
3249e7a79811SFilipe Manana clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
32504a500fd1SYan, Zheng }
32514a500fd1SYan, Zheng return 0;
32524a500fd1SYan, Zheng }
32534a500fd1SYan, Zheng
btrfs_free_log_root_tree(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)32544a500fd1SYan, Zheng int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
32554a500fd1SYan, Zheng struct btrfs_fs_info *fs_info)
32564a500fd1SYan, Zheng {
32574a500fd1SYan, Zheng if (fs_info->log_root_tree) {
32584a500fd1SYan, Zheng free_log_tree(trans, fs_info->log_root_tree);
32594a500fd1SYan, Zheng fs_info->log_root_tree = NULL;
326047876f7cSFilipe Manana clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state);
32614a500fd1SYan, Zheng }
3262e02119d5SChris Mason return 0;
3263e02119d5SChris Mason }
3264e02119d5SChris Mason
3265e02119d5SChris Mason /*
32660f8ce498SFilipe Manana * Check if an inode was logged in the current transaction. This correctly deals
32670f8ce498SFilipe Manana * with the case where the inode was logged but has a logged_trans of 0, which
32680f8ce498SFilipe Manana * happens if the inode is evicted and loaded again, as logged_trans is an in
32690f8ce498SFilipe Manana * memory only field (not persisted).
32700f8ce498SFilipe Manana *
32710f8ce498SFilipe Manana * Returns 1 if the inode was logged before in the transaction, 0 if it was not,
32720f8ce498SFilipe Manana * and < 0 on error.
3273803f0f64SFilipe Manana */
inode_logged(const struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path_in)3274bf1f4fd3SFilipe Manana static int inode_logged(const struct btrfs_trans_handle *trans,
32750f8ce498SFilipe Manana struct btrfs_inode *inode,
32760f8ce498SFilipe Manana struct btrfs_path *path_in)
3277803f0f64SFilipe Manana {
32780f8ce498SFilipe Manana struct btrfs_path *path = path_in;
32790f8ce498SFilipe Manana struct btrfs_key key;
32800f8ce498SFilipe Manana int ret;
3281803f0f64SFilipe Manana
32820f8ce498SFilipe Manana if (inode->logged_trans == trans->transid)
32830f8ce498SFilipe Manana return 1;
32841e0860f3SFilipe Manana
32856e8e777dSFilipe Manana /*
32860f8ce498SFilipe Manana * If logged_trans is not 0, then we know the inode logged was not logged
32870f8ce498SFilipe Manana * in this transaction, so we can return false right away.
32886e8e777dSFilipe Manana */
32890f8ce498SFilipe Manana if (inode->logged_trans > 0)
32900f8ce498SFilipe Manana return 0;
3291803f0f64SFilipe Manana
32920f8ce498SFilipe Manana /*
32930f8ce498SFilipe Manana * If no log tree was created for this root in this transaction, then
32940f8ce498SFilipe Manana * the inode can not have been logged in this transaction. In that case
32950f8ce498SFilipe Manana * set logged_trans to anything greater than 0 and less than the current
32960f8ce498SFilipe Manana * transaction's ID, to avoid the search below in a future call in case
32970f8ce498SFilipe Manana * a log tree gets created after this.
32980f8ce498SFilipe Manana */
32990f8ce498SFilipe Manana if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state)) {
33000f8ce498SFilipe Manana inode->logged_trans = trans->transid - 1;
33010f8ce498SFilipe Manana return 0;
33020f8ce498SFilipe Manana }
33030f8ce498SFilipe Manana
33040f8ce498SFilipe Manana /*
33050f8ce498SFilipe Manana * We have a log tree and the inode's logged_trans is 0. We can't tell
33060f8ce498SFilipe Manana * for sure if the inode was logged before in this transaction by looking
33070f8ce498SFilipe Manana * only at logged_trans. We could be pessimistic and assume it was, but
33080f8ce498SFilipe Manana * that can lead to unnecessarily logging an inode during rename and link
33090f8ce498SFilipe Manana * operations, and then further updating the log in followup rename and
33100f8ce498SFilipe Manana * link operations, specially if it's a directory, which adds latency
33110f8ce498SFilipe Manana * visible to applications doing a series of rename or link operations.
33120f8ce498SFilipe Manana *
33130f8ce498SFilipe Manana * A logged_trans of 0 here can mean several things:
33140f8ce498SFilipe Manana *
33150f8ce498SFilipe Manana * 1) The inode was never logged since the filesystem was mounted, and may
33160f8ce498SFilipe Manana * or may have not been evicted and loaded again;
33170f8ce498SFilipe Manana *
33180f8ce498SFilipe Manana * 2) The inode was logged in a previous transaction, then evicted and
33190f8ce498SFilipe Manana * then loaded again;
33200f8ce498SFilipe Manana *
33210f8ce498SFilipe Manana * 3) The inode was logged in the current transaction, then evicted and
33220f8ce498SFilipe Manana * then loaded again.
33230f8ce498SFilipe Manana *
33240f8ce498SFilipe Manana * For cases 1) and 2) we don't want to return true, but we need to detect
33250f8ce498SFilipe Manana * case 3) and return true. So we do a search in the log root for the inode
33260f8ce498SFilipe Manana * item.
33270f8ce498SFilipe Manana */
33280f8ce498SFilipe Manana key.objectid = btrfs_ino(inode);
33290f8ce498SFilipe Manana key.type = BTRFS_INODE_ITEM_KEY;
33300f8ce498SFilipe Manana key.offset = 0;
33310f8ce498SFilipe Manana
33320f8ce498SFilipe Manana if (!path) {
33330f8ce498SFilipe Manana path = btrfs_alloc_path();
33340f8ce498SFilipe Manana if (!path)
33350f8ce498SFilipe Manana return -ENOMEM;
33360f8ce498SFilipe Manana }
33370f8ce498SFilipe Manana
33380f8ce498SFilipe Manana ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
33390f8ce498SFilipe Manana
33400f8ce498SFilipe Manana if (path_in)
33410f8ce498SFilipe Manana btrfs_release_path(path);
33420f8ce498SFilipe Manana else
33430f8ce498SFilipe Manana btrfs_free_path(path);
33440f8ce498SFilipe Manana
33450f8ce498SFilipe Manana /*
33460f8ce498SFilipe Manana * Logging an inode always results in logging its inode item. So if we
33470f8ce498SFilipe Manana * did not find the item we know the inode was not logged for sure.
33480f8ce498SFilipe Manana */
33490f8ce498SFilipe Manana if (ret < 0) {
33500f8ce498SFilipe Manana return ret;
33510f8ce498SFilipe Manana } else if (ret > 0) {
33520f8ce498SFilipe Manana /*
33530f8ce498SFilipe Manana * Set logged_trans to a value greater than 0 and less then the
33540f8ce498SFilipe Manana * current transaction to avoid doing the search in future calls.
33550f8ce498SFilipe Manana */
33560f8ce498SFilipe Manana inode->logged_trans = trans->transid - 1;
33570f8ce498SFilipe Manana return 0;
33580f8ce498SFilipe Manana }
33590f8ce498SFilipe Manana
33600f8ce498SFilipe Manana /*
33610f8ce498SFilipe Manana * The inode was previously logged and then evicted, set logged_trans to
33620f8ce498SFilipe Manana * the current transacion's ID, to avoid future tree searches as long as
33630f8ce498SFilipe Manana * the inode is not evicted again.
33640f8ce498SFilipe Manana */
33650f8ce498SFilipe Manana inode->logged_trans = trans->transid;
33660f8ce498SFilipe Manana
33670f8ce498SFilipe Manana /*
33680f8ce498SFilipe Manana * If it's a directory, then we must set last_dir_index_offset to the
33690f8ce498SFilipe Manana * maximum possible value, so that the next attempt to log the inode does
33700f8ce498SFilipe Manana * not skip checking if dir index keys found in modified subvolume tree
33710f8ce498SFilipe Manana * leaves have been logged before, otherwise it would result in attempts
33720f8ce498SFilipe Manana * to insert duplicate dir index keys in the log tree. This must be done
33730f8ce498SFilipe Manana * because last_dir_index_offset is an in-memory only field, not persisted
33740f8ce498SFilipe Manana * in the inode item or any other on-disk structure, so its value is lost
33750f8ce498SFilipe Manana * once the inode is evicted.
33760f8ce498SFilipe Manana */
33770f8ce498SFilipe Manana if (S_ISDIR(inode->vfs_inode.i_mode))
33780f8ce498SFilipe Manana inode->last_dir_index_offset = (u64)-1;
33790f8ce498SFilipe Manana
33800f8ce498SFilipe Manana return 1;
3381803f0f64SFilipe Manana }
3382803f0f64SFilipe Manana
3383803f0f64SFilipe Manana /*
3384839061feSFilipe Manana * Delete a directory entry from the log if it exists.
3385839061feSFilipe Manana *
3386839061feSFilipe Manana * Returns < 0 on error
3387839061feSFilipe Manana * 1 if the entry does not exists
3388839061feSFilipe Manana * 0 if the entry existed and was successfully deleted
3389839061feSFilipe Manana */
del_logged_dentry(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,u64 dir_ino,const struct fscrypt_str * name,u64 index)3390839061feSFilipe Manana static int del_logged_dentry(struct btrfs_trans_handle *trans,
3391839061feSFilipe Manana struct btrfs_root *log,
3392839061feSFilipe Manana struct btrfs_path *path,
3393839061feSFilipe Manana u64 dir_ino,
33946db75318SSweet Tea Dorminy const struct fscrypt_str *name,
3395839061feSFilipe Manana u64 index)
3396839061feSFilipe Manana {
3397839061feSFilipe Manana struct btrfs_dir_item *di;
3398839061feSFilipe Manana
3399839061feSFilipe Manana /*
3400839061feSFilipe Manana * We only log dir index items of a directory, so we don't need to look
3401839061feSFilipe Manana * for dir item keys.
3402839061feSFilipe Manana */
3403839061feSFilipe Manana di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3404e43eec81SSweet Tea Dorminy index, name, -1);
3405839061feSFilipe Manana if (IS_ERR(di))
3406839061feSFilipe Manana return PTR_ERR(di);
3407839061feSFilipe Manana else if (!di)
3408839061feSFilipe Manana return 1;
3409839061feSFilipe Manana
3410839061feSFilipe Manana /*
3411839061feSFilipe Manana * We do not need to update the size field of the directory's
3412839061feSFilipe Manana * inode item because on log replay we update the field to reflect
3413839061feSFilipe Manana * all existing entries in the directory (see overwrite_item()).
3414839061feSFilipe Manana */
3415839061feSFilipe Manana return btrfs_delete_one_dir_name(trans, log, path, di);
3416839061feSFilipe Manana }
3417839061feSFilipe Manana
3418839061feSFilipe Manana /*
3419e02119d5SChris Mason * If both a file and directory are logged, and unlinks or renames are
3420e02119d5SChris Mason * mixed in, we have a few interesting corners:
3421e02119d5SChris Mason *
3422e02119d5SChris Mason * create file X in dir Y
3423e02119d5SChris Mason * link file X to X.link in dir Y
3424e02119d5SChris Mason * fsync file X
3425e02119d5SChris Mason * unlink file X but leave X.link
3426e02119d5SChris Mason * fsync dir Y
3427e02119d5SChris Mason *
3428e02119d5SChris Mason * After a crash we would expect only X.link to exist. But file X
3429e02119d5SChris Mason * didn't get fsync'd again so the log has back refs for X and X.link.
3430e02119d5SChris Mason *
3431e02119d5SChris Mason * We solve this by removing directory entries and inode backrefs from the
3432e02119d5SChris Mason * log when a file that was logged in the current transaction is
3433e02119d5SChris Mason * unlinked. Any later fsync will include the updated log entries, and
3434e02119d5SChris Mason * we'll be able to reconstruct the proper directory items from backrefs.
3435e02119d5SChris Mason *
3436e02119d5SChris Mason * This optimizations allows us to avoid relogging the entire inode
3437e02119d5SChris Mason * or the entire directory.
3438e02119d5SChris Mason */
btrfs_del_dir_entries_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct fscrypt_str * name,struct btrfs_inode * dir,u64 index)34399a35fc95SJosef Bacik void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3440e02119d5SChris Mason struct btrfs_root *root,
34416db75318SSweet Tea Dorminy const struct fscrypt_str *name,
344249f34d1fSNikolay Borisov struct btrfs_inode *dir, u64 index)
3443e02119d5SChris Mason {
3444e02119d5SChris Mason struct btrfs_path *path;
3445e02119d5SChris Mason int ret;
3446e02119d5SChris Mason
34470f8ce498SFilipe Manana ret = inode_logged(trans, dir, NULL);
34480f8ce498SFilipe Manana if (ret == 0)
34499a35fc95SJosef Bacik return;
34500f8ce498SFilipe Manana else if (ret < 0) {
34510f8ce498SFilipe Manana btrfs_set_log_full_commit(trans);
34520f8ce498SFilipe Manana return;
34530f8ce498SFilipe Manana }
34543a5f1d45SChris Mason
3455e02119d5SChris Mason ret = join_running_log_trans(root);
3456e02119d5SChris Mason if (ret)
34579a35fc95SJosef Bacik return;
3458e02119d5SChris Mason
345949f34d1fSNikolay Borisov mutex_lock(&dir->log_mutex);
3460e02119d5SChris Mason
3461e02119d5SChris Mason path = btrfs_alloc_path();
3462a62f44a5STsutomu Itoh if (!path) {
3463839061feSFilipe Manana ret = -ENOMEM;
3464a62f44a5STsutomu Itoh goto out_unlock;
3465a62f44a5STsutomu Itoh }
34662a29edc6Sliubo
3467839061feSFilipe Manana ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir),
3468e43eec81SSweet Tea Dorminy name, index);
3469e02119d5SChris Mason btrfs_free_path(path);
3470a62f44a5STsutomu Itoh out_unlock:
347149f34d1fSNikolay Borisov mutex_unlock(&dir->log_mutex);
3472839061feSFilipe Manana if (ret < 0)
347390787766SDavid Sterba btrfs_set_log_full_commit(trans);
347412fcfd22SChris Mason btrfs_end_log_trans(root);
3475e02119d5SChris Mason }
3476e02119d5SChris Mason
3477e02119d5SChris Mason /* see comments for btrfs_del_dir_entries_in_log */
btrfs_del_inode_ref_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct fscrypt_str * name,struct btrfs_inode * inode,u64 dirid)34789a35fc95SJosef Bacik void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3479e02119d5SChris Mason struct btrfs_root *root,
34806db75318SSweet Tea Dorminy const struct fscrypt_str *name,
3481a491abb2SNikolay Borisov struct btrfs_inode *inode, u64 dirid)
3482e02119d5SChris Mason {
3483e02119d5SChris Mason struct btrfs_root *log;
3484e02119d5SChris Mason u64 index;
3485e02119d5SChris Mason int ret;
3486e02119d5SChris Mason
34870f8ce498SFilipe Manana ret = inode_logged(trans, inode, NULL);
34880f8ce498SFilipe Manana if (ret == 0)
34899a35fc95SJosef Bacik return;
34900f8ce498SFilipe Manana else if (ret < 0) {
34910f8ce498SFilipe Manana btrfs_set_log_full_commit(trans);
34920f8ce498SFilipe Manana return;
34930f8ce498SFilipe Manana }
34943a5f1d45SChris Mason
3495e02119d5SChris Mason ret = join_running_log_trans(root);
3496e02119d5SChris Mason if (ret)
34979a35fc95SJosef Bacik return;
3498e02119d5SChris Mason log = root->log_root;
3499a491abb2SNikolay Borisov mutex_lock(&inode->log_mutex);
3500e02119d5SChris Mason
3501e43eec81SSweet Tea Dorminy ret = btrfs_del_inode_ref(trans, log, name, btrfs_ino(inode),
3502e02119d5SChris Mason dirid, &index);
3503a491abb2SNikolay Borisov mutex_unlock(&inode->log_mutex);
35049a35fc95SJosef Bacik if (ret < 0 && ret != -ENOENT)
350590787766SDavid Sterba btrfs_set_log_full_commit(trans);
350612fcfd22SChris Mason btrfs_end_log_trans(root);
3507e02119d5SChris Mason }
3508e02119d5SChris Mason
3509e02119d5SChris Mason /*
3510e02119d5SChris Mason * creates a range item in the log for 'dirid'. first_offset and
3511e02119d5SChris Mason * last_offset tell us which parts of the key space the log should
3512e02119d5SChris Mason * be considered authoritative for.
3513e02119d5SChris Mason */
insert_dir_log_key(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,u64 dirid,u64 first_offset,u64 last_offset)3514e02119d5SChris Mason static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3515e02119d5SChris Mason struct btrfs_root *log,
3516e02119d5SChris Mason struct btrfs_path *path,
3517339d0354SFilipe Manana u64 dirid,
3518e02119d5SChris Mason u64 first_offset, u64 last_offset)
3519e02119d5SChris Mason {
3520e02119d5SChris Mason int ret;
3521e02119d5SChris Mason struct btrfs_key key;
3522e02119d5SChris Mason struct btrfs_dir_log_item *item;
3523e02119d5SChris Mason
3524e02119d5SChris Mason key.objectid = dirid;
3525e02119d5SChris Mason key.offset = first_offset;
3526e02119d5SChris Mason key.type = BTRFS_DIR_LOG_INDEX_KEY;
3527e02119d5SChris Mason ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3528750ee454SFilipe Manana /*
3529750ee454SFilipe Manana * -EEXIST is fine and can happen sporadically when we are logging a
3530750ee454SFilipe Manana * directory and have concurrent insertions in the subvolume's tree for
3531750ee454SFilipe Manana * items from other inodes and that result in pushing off some dir items
3532750ee454SFilipe Manana * from one leaf to another in order to accommodate for the new items.
3533750ee454SFilipe Manana * This results in logging the same dir index range key.
3534750ee454SFilipe Manana */
3535750ee454SFilipe Manana if (ret && ret != -EEXIST)
35364a500fd1SYan, Zheng return ret;
3537e02119d5SChris Mason
3538e02119d5SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3539e02119d5SChris Mason struct btrfs_dir_log_item);
3540750ee454SFilipe Manana if (ret == -EEXIST) {
3541750ee454SFilipe Manana const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item);
3542750ee454SFilipe Manana
3543750ee454SFilipe Manana /*
3544750ee454SFilipe Manana * btrfs_del_dir_entries_in_log() might have been called during
3545750ee454SFilipe Manana * an unlink between the initial insertion of this key and the
3546750ee454SFilipe Manana * current update, or we might be logging a single entry deletion
3547750ee454SFilipe Manana * during a rename, so set the new last_offset to the max value.
3548750ee454SFilipe Manana */
3549750ee454SFilipe Manana last_offset = max(last_offset, curr_end);
3550750ee454SFilipe Manana }
3551e02119d5SChris Mason btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3552d5e09e38SFilipe Manana btrfs_mark_buffer_dirty(trans, path->nodes[0]);
3553b3b4aa74SDavid Sterba btrfs_release_path(path);
3554e02119d5SChris Mason return 0;
3555e02119d5SChris Mason }
3556e02119d5SChris Mason
flush_dir_items_batch(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct extent_buffer * src,struct btrfs_path * dst_path,int start_slot,int count)3557086dcbfaSFilipe Manana static int flush_dir_items_batch(struct btrfs_trans_handle *trans,
35586afaed53SFilipe Manana struct btrfs_inode *inode,
3559086dcbfaSFilipe Manana struct extent_buffer *src,
3560086dcbfaSFilipe Manana struct btrfs_path *dst_path,
3561086dcbfaSFilipe Manana int start_slot,
3562086dcbfaSFilipe Manana int count)
3563086dcbfaSFilipe Manana {
35646afaed53SFilipe Manana struct btrfs_root *log = inode->root->log_root;
3565086dcbfaSFilipe Manana char *ins_data = NULL;
3566b7ef5f3aSFilipe Manana struct btrfs_item_batch batch;
3567086dcbfaSFilipe Manana struct extent_buffer *dst;
3568da1b811fSFilipe Manana unsigned long src_offset;
3569da1b811fSFilipe Manana unsigned long dst_offset;
35706afaed53SFilipe Manana u64 last_index;
3571086dcbfaSFilipe Manana struct btrfs_key key;
3572086dcbfaSFilipe Manana u32 item_size;
3573086dcbfaSFilipe Manana int ret;
3574086dcbfaSFilipe Manana int i;
3575086dcbfaSFilipe Manana
3576086dcbfaSFilipe Manana ASSERT(count > 0);
3577b7ef5f3aSFilipe Manana batch.nr = count;
3578086dcbfaSFilipe Manana
3579086dcbfaSFilipe Manana if (count == 1) {
3580086dcbfaSFilipe Manana btrfs_item_key_to_cpu(src, &key, start_slot);
35813212fa14SJosef Bacik item_size = btrfs_item_size(src, start_slot);
3582b7ef5f3aSFilipe Manana batch.keys = &key;
3583b7ef5f3aSFilipe Manana batch.data_sizes = &item_size;
3584b7ef5f3aSFilipe Manana batch.total_data_size = item_size;
3585086dcbfaSFilipe Manana } else {
3586b7ef5f3aSFilipe Manana struct btrfs_key *ins_keys;
3587b7ef5f3aSFilipe Manana u32 *ins_sizes;
3588b7ef5f3aSFilipe Manana
3589086dcbfaSFilipe Manana ins_data = kmalloc(count * sizeof(u32) +
3590086dcbfaSFilipe Manana count * sizeof(struct btrfs_key), GFP_NOFS);
3591086dcbfaSFilipe Manana if (!ins_data)
3592086dcbfaSFilipe Manana return -ENOMEM;
3593086dcbfaSFilipe Manana
3594086dcbfaSFilipe Manana ins_sizes = (u32 *)ins_data;
3595086dcbfaSFilipe Manana ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32));
3596b7ef5f3aSFilipe Manana batch.keys = ins_keys;
3597b7ef5f3aSFilipe Manana batch.data_sizes = ins_sizes;
3598b7ef5f3aSFilipe Manana batch.total_data_size = 0;
3599086dcbfaSFilipe Manana
3600086dcbfaSFilipe Manana for (i = 0; i < count; i++) {
3601086dcbfaSFilipe Manana const int slot = start_slot + i;
3602086dcbfaSFilipe Manana
3603086dcbfaSFilipe Manana btrfs_item_key_to_cpu(src, &ins_keys[i], slot);
36043212fa14SJosef Bacik ins_sizes[i] = btrfs_item_size(src, slot);
3605b7ef5f3aSFilipe Manana batch.total_data_size += ins_sizes[i];
3606086dcbfaSFilipe Manana }
3607086dcbfaSFilipe Manana }
3608086dcbfaSFilipe Manana
3609b7ef5f3aSFilipe Manana ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
3610086dcbfaSFilipe Manana if (ret)
3611086dcbfaSFilipe Manana goto out;
3612086dcbfaSFilipe Manana
3613086dcbfaSFilipe Manana dst = dst_path->nodes[0];
3614da1b811fSFilipe Manana /*
3615da1b811fSFilipe Manana * Copy all the items in bulk, in a single copy operation. Item data is
3616da1b811fSFilipe Manana * organized such that it's placed at the end of a leaf and from right
3617da1b811fSFilipe Manana * to left. For example, the data for the second item ends at an offset
3618da1b811fSFilipe Manana * that matches the offset where the data for the first item starts, the
3619da1b811fSFilipe Manana * data for the third item ends at an offset that matches the offset
3620da1b811fSFilipe Manana * where the data of the second items starts, and so on.
3621da1b811fSFilipe Manana * Therefore our source and destination start offsets for copy match the
3622da1b811fSFilipe Manana * offsets of the last items (highest slots).
3623da1b811fSFilipe Manana */
3624da1b811fSFilipe Manana dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1);
3625da1b811fSFilipe Manana src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1);
3626da1b811fSFilipe Manana copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size);
3627086dcbfaSFilipe Manana btrfs_release_path(dst_path);
36286afaed53SFilipe Manana
36296afaed53SFilipe Manana last_index = batch.keys[count - 1].offset;
36306afaed53SFilipe Manana ASSERT(last_index > inode->last_dir_index_offset);
36316afaed53SFilipe Manana
36326afaed53SFilipe Manana /*
36336afaed53SFilipe Manana * If for some unexpected reason the last item's index is not greater
36345cce1780SFilipe Manana * than the last index we logged, warn and force a transaction commit.
36356afaed53SFilipe Manana */
36366afaed53SFilipe Manana if (WARN_ON(last_index <= inode->last_dir_index_offset))
36375cce1780SFilipe Manana ret = BTRFS_LOG_FORCE_COMMIT;
36386afaed53SFilipe Manana else
36396afaed53SFilipe Manana inode->last_dir_index_offset = last_index;
3640fa4b8cb1SFilipe Manana
3641fa4b8cb1SFilipe Manana if (btrfs_get_first_dir_index_to_log(inode) == 0)
3642fa4b8cb1SFilipe Manana btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset);
3643086dcbfaSFilipe Manana out:
3644086dcbfaSFilipe Manana kfree(ins_data);
3645086dcbfaSFilipe Manana
3646086dcbfaSFilipe Manana return ret;
3647086dcbfaSFilipe Manana }
3648086dcbfaSFilipe Manana
process_dir_items_leaf(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,struct btrfs_log_ctx * ctx,u64 * last_old_dentry_offset)3649eb10d85eSFilipe Manana static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
3650eb10d85eSFilipe Manana struct btrfs_inode *inode,
3651eb10d85eSFilipe Manana struct btrfs_path *path,
3652eb10d85eSFilipe Manana struct btrfs_path *dst_path,
3653732d591aSFilipe Manana struct btrfs_log_ctx *ctx,
3654732d591aSFilipe Manana u64 *last_old_dentry_offset)
3655eb10d85eSFilipe Manana {
3656eb10d85eSFilipe Manana struct btrfs_root *log = inode->root->log_root;
3657796787c9SFilipe Manana struct extent_buffer *src;
3658796787c9SFilipe Manana const int nritems = btrfs_header_nritems(path->nodes[0]);
3659eb10d85eSFilipe Manana const u64 ino = btrfs_ino(inode);
3660086dcbfaSFilipe Manana bool last_found = false;
3661086dcbfaSFilipe Manana int batch_start = 0;
3662086dcbfaSFilipe Manana int batch_size = 0;
3663eb10d85eSFilipe Manana int i;
3664eb10d85eSFilipe Manana
3665796787c9SFilipe Manana /*
3666796787c9SFilipe Manana * We need to clone the leaf, release the read lock on it, and use the
3667796787c9SFilipe Manana * clone before modifying the log tree. See the comment at copy_items()
3668796787c9SFilipe Manana * about why we need to do this.
3669796787c9SFilipe Manana */
3670796787c9SFilipe Manana src = btrfs_clone_extent_buffer(path->nodes[0]);
3671796787c9SFilipe Manana if (!src)
3672796787c9SFilipe Manana return -ENOMEM;
3673796787c9SFilipe Manana
3674796787c9SFilipe Manana i = path->slots[0];
3675796787c9SFilipe Manana btrfs_release_path(path);
3676796787c9SFilipe Manana path->nodes[0] = src;
3677796787c9SFilipe Manana path->slots[0] = i;
3678796787c9SFilipe Manana
3679796787c9SFilipe Manana for (; i < nritems; i++) {
3680732d591aSFilipe Manana struct btrfs_dir_item *di;
3681eb10d85eSFilipe Manana struct btrfs_key key;
3682eb10d85eSFilipe Manana int ret;
3683eb10d85eSFilipe Manana
3684eb10d85eSFilipe Manana btrfs_item_key_to_cpu(src, &key, i);
3685eb10d85eSFilipe Manana
3686339d0354SFilipe Manana if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) {
3687086dcbfaSFilipe Manana last_found = true;
3688086dcbfaSFilipe Manana break;
3689086dcbfaSFilipe Manana }
3690eb10d85eSFilipe Manana
3691732d591aSFilipe Manana di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3692732d591aSFilipe Manana
3693732d591aSFilipe Manana /*
3694732d591aSFilipe Manana * Skip ranges of items that consist only of dir item keys created
3695732d591aSFilipe Manana * in past transactions. However if we find a gap, we must log a
3696732d591aSFilipe Manana * dir index range item for that gap, so that index keys in that
3697732d591aSFilipe Manana * gap are deleted during log replay.
3698732d591aSFilipe Manana */
3699732d591aSFilipe Manana if (btrfs_dir_transid(src, di) < trans->transid) {
3700732d591aSFilipe Manana if (key.offset > *last_old_dentry_offset + 1) {
3701732d591aSFilipe Manana ret = insert_dir_log_key(trans, log, dst_path,
3702732d591aSFilipe Manana ino, *last_old_dentry_offset + 1,
3703732d591aSFilipe Manana key.offset - 1);
3704732d591aSFilipe Manana if (ret < 0)
3705732d591aSFilipe Manana return ret;
3706732d591aSFilipe Manana }
3707732d591aSFilipe Manana
3708732d591aSFilipe Manana *last_old_dentry_offset = key.offset;
3709732d591aSFilipe Manana continue;
3710732d591aSFilipe Manana }
3711193df624SFilipe Manana
3712193df624SFilipe Manana /* If we logged this dir index item before, we can skip it. */
3713193df624SFilipe Manana if (key.offset <= inode->last_dir_index_offset)
3714193df624SFilipe Manana continue;
3715193df624SFilipe Manana
3716eb10d85eSFilipe Manana /*
3717eb10d85eSFilipe Manana * We must make sure that when we log a directory entry, the
3718eb10d85eSFilipe Manana * corresponding inode, after log replay, has a matching link
3719eb10d85eSFilipe Manana * count. For example:
3720eb10d85eSFilipe Manana *
3721eb10d85eSFilipe Manana * touch foo
3722eb10d85eSFilipe Manana * mkdir mydir
3723eb10d85eSFilipe Manana * sync
3724eb10d85eSFilipe Manana * ln foo mydir/bar
3725eb10d85eSFilipe Manana * xfs_io -c "fsync" mydir
3726eb10d85eSFilipe Manana * <crash>
3727eb10d85eSFilipe Manana * <mount fs and log replay>
3728eb10d85eSFilipe Manana *
3729eb10d85eSFilipe Manana * Would result in a fsync log that when replayed, our file inode
3730eb10d85eSFilipe Manana * would have a link count of 1, but we get two directory entries
3731eb10d85eSFilipe Manana * pointing to the same inode. After removing one of the names,
3732eb10d85eSFilipe Manana * it would not be possible to remove the other name, which
3733eb10d85eSFilipe Manana * resulted always in stale file handle errors, and would not be
3734eb10d85eSFilipe Manana * possible to rmdir the parent directory, since its i_size could
3735eb10d85eSFilipe Manana * never be decremented to the value BTRFS_EMPTY_DIR_SIZE,
3736eb10d85eSFilipe Manana * resulting in -ENOTEMPTY errors.
3737eb10d85eSFilipe Manana */
3738086dcbfaSFilipe Manana if (!ctx->log_new_dentries) {
3739086dcbfaSFilipe Manana struct btrfs_key di_key;
3740086dcbfaSFilipe Manana
3741086dcbfaSFilipe Manana btrfs_dir_item_key_to_cpu(src, di, &di_key);
3742732d591aSFilipe Manana if (di_key.type != BTRFS_ROOT_ITEM_KEY)
3743eb10d85eSFilipe Manana ctx->log_new_dentries = true;
3744eb10d85eSFilipe Manana }
3745eb10d85eSFilipe Manana
3746086dcbfaSFilipe Manana if (batch_size == 0)
3747086dcbfaSFilipe Manana batch_start = i;
3748086dcbfaSFilipe Manana batch_size++;
3749086dcbfaSFilipe Manana }
3750086dcbfaSFilipe Manana
3751086dcbfaSFilipe Manana if (batch_size > 0) {
3752086dcbfaSFilipe Manana int ret;
3753086dcbfaSFilipe Manana
37546afaed53SFilipe Manana ret = flush_dir_items_batch(trans, inode, src, dst_path,
3755086dcbfaSFilipe Manana batch_start, batch_size);
3756086dcbfaSFilipe Manana if (ret < 0)
3757086dcbfaSFilipe Manana return ret;
3758086dcbfaSFilipe Manana }
3759086dcbfaSFilipe Manana
3760086dcbfaSFilipe Manana return last_found ? 1 : 0;
3761eb10d85eSFilipe Manana }
3762eb10d85eSFilipe Manana
3763e02119d5SChris Mason /*
3764e02119d5SChris Mason * log all the items included in the current transaction for a given
3765e02119d5SChris Mason * directory. This also creates the range items in the log tree required
3766e02119d5SChris Mason * to replay anything deleted before the fsync
3767e02119d5SChris Mason */
log_dir_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,struct btrfs_log_ctx * ctx,u64 min_offset,u64 * last_offset_ret)3768e02119d5SChris Mason static noinline int log_dir_items(struct btrfs_trans_handle *trans,
376990d04510SFilipe Manana struct btrfs_inode *inode,
3770e02119d5SChris Mason struct btrfs_path *path,
3771339d0354SFilipe Manana struct btrfs_path *dst_path,
37722f2ff0eeSFilipe Manana struct btrfs_log_ctx *ctx,
3773e02119d5SChris Mason u64 min_offset, u64 *last_offset_ret)
3774e02119d5SChris Mason {
3775e02119d5SChris Mason struct btrfs_key min_key;
377690d04510SFilipe Manana struct btrfs_root *root = inode->root;
3777e02119d5SChris Mason struct btrfs_root *log = root->log_root;
3778e02119d5SChris Mason int ret;
3779732d591aSFilipe Manana u64 last_old_dentry_offset = min_offset - 1;
3780e02119d5SChris Mason u64 last_offset = (u64)-1;
3781684a5773SNikolay Borisov u64 ino = btrfs_ino(inode);
3782e02119d5SChris Mason
378333345d01SLi Zefan min_key.objectid = ino;
3784339d0354SFilipe Manana min_key.type = BTRFS_DIR_INDEX_KEY;
3785e02119d5SChris Mason min_key.offset = min_offset;
3786e02119d5SChris Mason
37876174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3788e02119d5SChris Mason
3789e02119d5SChris Mason /*
3790e02119d5SChris Mason * we didn't find anything from this transaction, see if there
3791e02119d5SChris Mason * is anything at all
3792e02119d5SChris Mason */
3793339d0354SFilipe Manana if (ret != 0 || min_key.objectid != ino ||
3794339d0354SFilipe Manana min_key.type != BTRFS_DIR_INDEX_KEY) {
379533345d01SLi Zefan min_key.objectid = ino;
3796339d0354SFilipe Manana min_key.type = BTRFS_DIR_INDEX_KEY;
3797e02119d5SChris Mason min_key.offset = (u64)-1;
3798b3b4aa74SDavid Sterba btrfs_release_path(path);
3799e02119d5SChris Mason ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3800e02119d5SChris Mason if (ret < 0) {
3801b3b4aa74SDavid Sterba btrfs_release_path(path);
3802e02119d5SChris Mason return ret;
3803e02119d5SChris Mason }
3804339d0354SFilipe Manana ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
3805e02119d5SChris Mason
3806e02119d5SChris Mason /* if ret == 0 there are items for this type,
3807e02119d5SChris Mason * create a range to tell us the last key of this type.
3808e02119d5SChris Mason * otherwise, there are no items in this directory after
3809e02119d5SChris Mason * *min_offset, and we create a range to indicate that.
3810e02119d5SChris Mason */
3811e02119d5SChris Mason if (ret == 0) {
3812e02119d5SChris Mason struct btrfs_key tmp;
3813732d591aSFilipe Manana
3814e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3815e02119d5SChris Mason path->slots[0]);
3816339d0354SFilipe Manana if (tmp.type == BTRFS_DIR_INDEX_KEY)
3817732d591aSFilipe Manana last_old_dentry_offset = tmp.offset;
3818235e1c7bSFilipe Manana } else if (ret > 0) {
3819235e1c7bSFilipe Manana ret = 0;
3820e02119d5SChris Mason }
38216d3d970bSFilipe Manana
3822e02119d5SChris Mason goto done;
3823e02119d5SChris Mason }
3824e02119d5SChris Mason
3825e02119d5SChris Mason /* go backward to find any previous key */
3826339d0354SFilipe Manana ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
3827e02119d5SChris Mason if (ret == 0) {
3828e02119d5SChris Mason struct btrfs_key tmp;
3829a450a4afSFilipe Manana
3830e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3831a450a4afSFilipe Manana /*
3832a450a4afSFilipe Manana * The dir index key before the first one we found that needs to
3833a450a4afSFilipe Manana * be logged might be in a previous leaf, and there might be a
3834a450a4afSFilipe Manana * gap between these keys, meaning that we had deletions that
3835a450a4afSFilipe Manana * happened. So the key range item we log (key type
3836a450a4afSFilipe Manana * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the
3837a450a4afSFilipe Manana * previous key's offset plus 1, so that those deletes are replayed.
3838a450a4afSFilipe Manana */
3839a450a4afSFilipe Manana if (tmp.type == BTRFS_DIR_INDEX_KEY)
3840732d591aSFilipe Manana last_old_dentry_offset = tmp.offset;
38416d3d970bSFilipe Manana } else if (ret < 0) {
38426d3d970bSFilipe Manana goto done;
3843e02119d5SChris Mason }
38446d3d970bSFilipe Manana
3845b3b4aa74SDavid Sterba btrfs_release_path(path);
3846e02119d5SChris Mason
38472cc83342SJosef Bacik /*
38488bb6898dSFilipe Manana * Find the first key from this transaction again or the one we were at
38498bb6898dSFilipe Manana * in the loop below in case we had to reschedule. We may be logging the
38508bb6898dSFilipe Manana * directory without holding its VFS lock, which happen when logging new
38518bb6898dSFilipe Manana * dentries (through log_new_dir_dentries()) or in some cases when we
38528bb6898dSFilipe Manana * need to log the parent directory of an inode. This means a dir index
38538bb6898dSFilipe Manana * key might be deleted from the inode's root, and therefore we may not
38548bb6898dSFilipe Manana * find it anymore. If we can't find it, just move to the next key. We
38558bb6898dSFilipe Manana * can not bail out and ignore, because if we do that we will simply
38568bb6898dSFilipe Manana * not log dir index keys that come after the one that was just deleted
38578bb6898dSFilipe Manana * and we can end up logging a dir index range that ends at (u64)-1
38588bb6898dSFilipe Manana * (@last_offset is initialized to that), resulting in removing dir
38598bb6898dSFilipe Manana * entries we should not remove at log replay time.
38602cc83342SJosef Bacik */
3861bb56f02fSFilipe Manana search:
3862e02119d5SChris Mason ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3863235e1c7bSFilipe Manana if (ret > 0) {
38648bb6898dSFilipe Manana ret = btrfs_next_item(root, path);
3865235e1c7bSFilipe Manana if (ret > 0) {
3866235e1c7bSFilipe Manana /* There are no more keys in the inode's root. */
3867235e1c7bSFilipe Manana ret = 0;
3868235e1c7bSFilipe Manana goto done;
3869235e1c7bSFilipe Manana }
3870235e1c7bSFilipe Manana }
38716d3d970bSFilipe Manana if (ret < 0)
3872e02119d5SChris Mason goto done;
3873e02119d5SChris Mason
3874e02119d5SChris Mason /*
3875e02119d5SChris Mason * we have a block from this transaction, log every item in it
3876e02119d5SChris Mason * from our directory
3877e02119d5SChris Mason */
3878e02119d5SChris Mason while (1) {
3879732d591aSFilipe Manana ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx,
3880732d591aSFilipe Manana &last_old_dentry_offset);
3881eb10d85eSFilipe Manana if (ret != 0) {
3882235e1c7bSFilipe Manana if (ret > 0)
3883235e1c7bSFilipe Manana ret = 0;
38844a500fd1SYan, Zheng goto done;
38854a500fd1SYan, Zheng }
3886eb10d85eSFilipe Manana path->slots[0] = btrfs_header_nritems(path->nodes[0]);
3887e02119d5SChris Mason
3888e02119d5SChris Mason /*
3889e02119d5SChris Mason * look ahead to the next item and see if it is also
3890e02119d5SChris Mason * from this directory and from this transaction
3891e02119d5SChris Mason */
3892e02119d5SChris Mason ret = btrfs_next_leaf(root, path);
389380c0b421SLiu Bo if (ret) {
3894235e1c7bSFilipe Manana if (ret == 1) {
3895e02119d5SChris Mason last_offset = (u64)-1;
3896235e1c7bSFilipe Manana ret = 0;
3897235e1c7bSFilipe Manana }
3898e02119d5SChris Mason goto done;
3899e02119d5SChris Mason }
3900eb10d85eSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]);
3901339d0354SFilipe Manana if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) {
3902e02119d5SChris Mason last_offset = (u64)-1;
3903e02119d5SChris Mason goto done;
3904e02119d5SChris Mason }
3905e02119d5SChris Mason if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3906a450a4afSFilipe Manana /*
3907a450a4afSFilipe Manana * The next leaf was not changed in the current transaction
3908a450a4afSFilipe Manana * and has at least one dir index key.
3909a450a4afSFilipe Manana * We check for the next key because there might have been
3910a450a4afSFilipe Manana * one or more deletions between the last key we logged and
3911a450a4afSFilipe Manana * that next key. So the key range item we log (key type
3912a450a4afSFilipe Manana * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's
3913a450a4afSFilipe Manana * offset minus 1, so that those deletes are replayed.
3914a450a4afSFilipe Manana */
3915a450a4afSFilipe Manana last_offset = min_key.offset - 1;
3916e02119d5SChris Mason goto done;
3917e02119d5SChris Mason }
3918eb10d85eSFilipe Manana if (need_resched()) {
3919eb10d85eSFilipe Manana btrfs_release_path(path);
3920eb10d85eSFilipe Manana cond_resched();
3921eb10d85eSFilipe Manana goto search;
3922eb10d85eSFilipe Manana }
3923e02119d5SChris Mason }
3924e02119d5SChris Mason done:
3925b3b4aa74SDavid Sterba btrfs_release_path(path);
3926b3b4aa74SDavid Sterba btrfs_release_path(dst_path);
3927e02119d5SChris Mason
3928235e1c7bSFilipe Manana if (ret == 0) {
39294a500fd1SYan, Zheng *last_offset_ret = last_offset;
39304a500fd1SYan, Zheng /*
3931732d591aSFilipe Manana * In case the leaf was changed in the current transaction but
3932732d591aSFilipe Manana * all its dir items are from a past transaction, the last item
3933732d591aSFilipe Manana * in the leaf is a dir item and there's no gap between that last
3934732d591aSFilipe Manana * dir item and the first one on the next leaf (which did not
3935732d591aSFilipe Manana * change in the current transaction), then we don't need to log
3936732d591aSFilipe Manana * a range, last_old_dentry_offset is == to last_offset.
39374a500fd1SYan, Zheng */
3938732d591aSFilipe Manana ASSERT(last_old_dentry_offset <= last_offset);
3939235e1c7bSFilipe Manana if (last_old_dentry_offset < last_offset)
3940732d591aSFilipe Manana ret = insert_dir_log_key(trans, log, path, ino,
3941732d591aSFilipe Manana last_old_dentry_offset + 1,
3942339d0354SFilipe Manana last_offset);
39434a500fd1SYan, Zheng }
3944235e1c7bSFilipe Manana
3945235e1c7bSFilipe Manana return ret;
3946e02119d5SChris Mason }
3947e02119d5SChris Mason
3948e02119d5SChris Mason /*
3949193df624SFilipe Manana * If the inode was logged before and it was evicted, then its
3950193df624SFilipe Manana * last_dir_index_offset is (u64)-1, so we don't the value of the last index
3951193df624SFilipe Manana * key offset. If that's the case, search for it and update the inode. This
3952193df624SFilipe Manana * is to avoid lookups in the log tree every time we try to insert a dir index
3953193df624SFilipe Manana * key from a leaf changed in the current transaction, and to allow us to always
3954193df624SFilipe Manana * do batch insertions of dir index keys.
3955193df624SFilipe Manana */
update_last_dir_index_offset(struct btrfs_inode * inode,struct btrfs_path * path,const struct btrfs_log_ctx * ctx)3956193df624SFilipe Manana static int update_last_dir_index_offset(struct btrfs_inode *inode,
3957193df624SFilipe Manana struct btrfs_path *path,
3958193df624SFilipe Manana const struct btrfs_log_ctx *ctx)
3959193df624SFilipe Manana {
3960193df624SFilipe Manana const u64 ino = btrfs_ino(inode);
3961193df624SFilipe Manana struct btrfs_key key;
3962193df624SFilipe Manana int ret;
3963193df624SFilipe Manana
3964193df624SFilipe Manana lockdep_assert_held(&inode->log_mutex);
3965193df624SFilipe Manana
3966193df624SFilipe Manana if (inode->last_dir_index_offset != (u64)-1)
3967193df624SFilipe Manana return 0;
3968193df624SFilipe Manana
3969193df624SFilipe Manana if (!ctx->logged_before) {
3970193df624SFilipe Manana inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
3971193df624SFilipe Manana return 0;
3972193df624SFilipe Manana }
3973193df624SFilipe Manana
3974193df624SFilipe Manana key.objectid = ino;
3975193df624SFilipe Manana key.type = BTRFS_DIR_INDEX_KEY;
3976193df624SFilipe Manana key.offset = (u64)-1;
3977193df624SFilipe Manana
3978193df624SFilipe Manana ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
3979193df624SFilipe Manana /*
3980193df624SFilipe Manana * An error happened or we actually have an index key with an offset
3981193df624SFilipe Manana * value of (u64)-1. Bail out, we're done.
3982193df624SFilipe Manana */
3983193df624SFilipe Manana if (ret <= 0)
3984193df624SFilipe Manana goto out;
3985193df624SFilipe Manana
3986193df624SFilipe Manana ret = 0;
3987193df624SFilipe Manana inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
3988193df624SFilipe Manana
3989193df624SFilipe Manana /*
3990193df624SFilipe Manana * No dir index items, bail out and leave last_dir_index_offset with
3991193df624SFilipe Manana * the value right before the first valid index value.
3992193df624SFilipe Manana */
3993193df624SFilipe Manana if (path->slots[0] == 0)
3994193df624SFilipe Manana goto out;
3995193df624SFilipe Manana
3996193df624SFilipe Manana /*
3997193df624SFilipe Manana * btrfs_search_slot() left us at one slot beyond the slot with the last
3998193df624SFilipe Manana * index key, or beyond the last key of the directory that is not an
3999193df624SFilipe Manana * index key. If we have an index key before, set last_dir_index_offset
4000193df624SFilipe Manana * to its offset value, otherwise leave it with a value right before the
4001193df624SFilipe Manana * first valid index value, as it means we have an empty directory.
4002193df624SFilipe Manana */
4003193df624SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
4004193df624SFilipe Manana if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY)
4005193df624SFilipe Manana inode->last_dir_index_offset = key.offset;
4006193df624SFilipe Manana
4007193df624SFilipe Manana out:
4008193df624SFilipe Manana btrfs_release_path(path);
4009193df624SFilipe Manana
4010193df624SFilipe Manana return ret;
4011193df624SFilipe Manana }
4012193df624SFilipe Manana
4013193df624SFilipe Manana /*
4014e02119d5SChris Mason * logging directories is very similar to logging inodes, We find all the items
4015e02119d5SChris Mason * from the current transaction and write them to the log.
4016e02119d5SChris Mason *
4017e02119d5SChris Mason * The recovery code scans the directory in the subvolume, and if it finds a
4018e02119d5SChris Mason * key in the range logged that is not present in the log tree, then it means
4019e02119d5SChris Mason * that dir entry was unlinked during the transaction.
4020e02119d5SChris Mason *
4021e02119d5SChris Mason * In order for that scan to work, we must include one key smaller than
4022e02119d5SChris Mason * the smallest logged by this transaction and one key larger than the largest
4023e02119d5SChris Mason * key logged by this transaction.
4024e02119d5SChris Mason */
log_directory_changes(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,struct btrfs_log_ctx * ctx)4025e02119d5SChris Mason static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
402690d04510SFilipe Manana struct btrfs_inode *inode,
4027e02119d5SChris Mason struct btrfs_path *path,
40282f2ff0eeSFilipe Manana struct btrfs_path *dst_path,
40292f2ff0eeSFilipe Manana struct btrfs_log_ctx *ctx)
4030e02119d5SChris Mason {
4031e02119d5SChris Mason u64 min_key;
4032e02119d5SChris Mason u64 max_key;
4033e02119d5SChris Mason int ret;
4034e02119d5SChris Mason
4035193df624SFilipe Manana ret = update_last_dir_index_offset(inode, path, ctx);
4036193df624SFilipe Manana if (ret)
4037193df624SFilipe Manana return ret;
4038193df624SFilipe Manana
4039732d591aSFilipe Manana min_key = BTRFS_DIR_START_INDEX;
4040e02119d5SChris Mason max_key = 0;
4041dc287224SFilipe Manana
4042e02119d5SChris Mason while (1) {
4043339d0354SFilipe Manana ret = log_dir_items(trans, inode, path, dst_path,
4044dbf39ea4SNikolay Borisov ctx, min_key, &max_key);
40454a500fd1SYan, Zheng if (ret)
40464a500fd1SYan, Zheng return ret;
4047e02119d5SChris Mason if (max_key == (u64)-1)
4048e02119d5SChris Mason break;
4049e02119d5SChris Mason min_key = max_key + 1;
4050e02119d5SChris Mason }
4051e02119d5SChris Mason
4052e02119d5SChris Mason return 0;
4053e02119d5SChris Mason }
4054e02119d5SChris Mason
4055e02119d5SChris Mason /*
4056e02119d5SChris Mason * a helper function to drop items from the log before we relog an
4057e02119d5SChris Mason * inode. max_key_type indicates the highest item type to remove.
4058e02119d5SChris Mason * This cannot be run for file data extents because it does not
4059e02119d5SChris Mason * free the extents they point to.
4060e02119d5SChris Mason */
drop_inode_items(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,struct btrfs_inode * inode,int max_key_type)406188e221cdSFilipe Manana static int drop_inode_items(struct btrfs_trans_handle *trans,
4062e02119d5SChris Mason struct btrfs_root *log,
4063e02119d5SChris Mason struct btrfs_path *path,
406488e221cdSFilipe Manana struct btrfs_inode *inode,
406588e221cdSFilipe Manana int max_key_type)
4066e02119d5SChris Mason {
4067e02119d5SChris Mason int ret;
4068e02119d5SChris Mason struct btrfs_key key;
4069e02119d5SChris Mason struct btrfs_key found_key;
407018ec90d6SJosef Bacik int start_slot;
4071e02119d5SChris Mason
407288e221cdSFilipe Manana key.objectid = btrfs_ino(inode);
4073e02119d5SChris Mason key.type = max_key_type;
4074e02119d5SChris Mason key.offset = (u64)-1;
4075e02119d5SChris Mason
4076e02119d5SChris Mason while (1) {
4077e02119d5SChris Mason ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
4078fc4026e2SFilipe Manana if (ret < 0) {
4079e02119d5SChris Mason break;
4080fc4026e2SFilipe Manana } else if (ret > 0) {
4081e02119d5SChris Mason if (path->slots[0] == 0)
4082e02119d5SChris Mason break;
4083e02119d5SChris Mason path->slots[0]--;
4084fc4026e2SFilipe Manana }
4085fc4026e2SFilipe Manana
4086e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4087e02119d5SChris Mason path->slots[0]);
4088e02119d5SChris Mason
408988e221cdSFilipe Manana if (found_key.objectid != key.objectid)
4090e02119d5SChris Mason break;
4091e02119d5SChris Mason
409218ec90d6SJosef Bacik found_key.offset = 0;
409318ec90d6SJosef Bacik found_key.type = 0;
4094fdf8d595SAnand Jain ret = btrfs_bin_search(path->nodes[0], 0, &found_key, &start_slot);
4095cbca7d59SFilipe Manana if (ret < 0)
4096cbca7d59SFilipe Manana break;
409718ec90d6SJosef Bacik
409818ec90d6SJosef Bacik ret = btrfs_del_items(trans, log, path, start_slot,
409918ec90d6SJosef Bacik path->slots[0] - start_slot + 1);
410018ec90d6SJosef Bacik /*
410118ec90d6SJosef Bacik * If start slot isn't 0 then we don't need to re-search, we've
410218ec90d6SJosef Bacik * found the last guy with the objectid in this tree.
410318ec90d6SJosef Bacik */
410418ec90d6SJosef Bacik if (ret || start_slot != 0)
410565a246c5STsutomu Itoh break;
4106b3b4aa74SDavid Sterba btrfs_release_path(path);
4107e02119d5SChris Mason }
4108b3b4aa74SDavid Sterba btrfs_release_path(path);
41095bdbeb21SJosef Bacik if (ret > 0)
41105bdbeb21SJosef Bacik ret = 0;
41114a500fd1SYan, Zheng return ret;
4112e02119d5SChris Mason }
4113e02119d5SChris Mason
truncate_inode_items(struct btrfs_trans_handle * trans,struct btrfs_root * log_root,struct btrfs_inode * inode,u64 new_size,u32 min_type)41148a2b3da1SFilipe Manana static int truncate_inode_items(struct btrfs_trans_handle *trans,
41158a2b3da1SFilipe Manana struct btrfs_root *log_root,
41168a2b3da1SFilipe Manana struct btrfs_inode *inode,
41178a2b3da1SFilipe Manana u64 new_size, u32 min_type)
41188a2b3da1SFilipe Manana {
4119d9ac19c3SJosef Bacik struct btrfs_truncate_control control = {
4120d9ac19c3SJosef Bacik .new_size = new_size,
4121487e81d2SJosef Bacik .ino = btrfs_ino(inode),
4122d9ac19c3SJosef Bacik .min_type = min_type,
41235caa490eSJosef Bacik .skip_ref_updates = true,
4124d9ac19c3SJosef Bacik };
41258a2b3da1SFilipe Manana
41268697b8f8SJosef Bacik return btrfs_truncate_inode_items(trans, log_root, &control);
41278a2b3da1SFilipe Manana }
41288a2b3da1SFilipe Manana
fill_inode_item(struct btrfs_trans_handle * trans,struct extent_buffer * leaf,struct btrfs_inode_item * item,struct inode * inode,int log_inode_only,u64 logged_isize)412994edf4aeSJosef Bacik static void fill_inode_item(struct btrfs_trans_handle *trans,
413094edf4aeSJosef Bacik struct extent_buffer *leaf,
413194edf4aeSJosef Bacik struct btrfs_inode_item *item,
41321a4bcf47SFilipe Manana struct inode *inode, int log_inode_only,
41331a4bcf47SFilipe Manana u64 logged_isize)
413494edf4aeSJosef Bacik {
41350b1c6ccaSJosef Bacik struct btrfs_map_token token;
413677eea05eSBoris Burkov u64 flags;
413794edf4aeSJosef Bacik
4138c82f823cSDavid Sterba btrfs_init_map_token(&token, leaf);
413994edf4aeSJosef Bacik
414094edf4aeSJosef Bacik if (log_inode_only) {
414194edf4aeSJosef Bacik /* set the generation to zero so the recover code
414294edf4aeSJosef Bacik * can tell the difference between an logging
414394edf4aeSJosef Bacik * just to say 'this inode exists' and a logging
414494edf4aeSJosef Bacik * to say 'update this inode with these values'
414594edf4aeSJosef Bacik */
4146cc4c13d5SDavid Sterba btrfs_set_token_inode_generation(&token, item, 0);
4147cc4c13d5SDavid Sterba btrfs_set_token_inode_size(&token, item, logged_isize);
414894edf4aeSJosef Bacik } else {
4149cc4c13d5SDavid Sterba btrfs_set_token_inode_generation(&token, item,
4150cc4c13d5SDavid Sterba BTRFS_I(inode)->generation);
4151cc4c13d5SDavid Sterba btrfs_set_token_inode_size(&token, item, inode->i_size);
415294edf4aeSJosef Bacik }
415394edf4aeSJosef Bacik
4154cc4c13d5SDavid Sterba btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
4155cc4c13d5SDavid Sterba btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
4156cc4c13d5SDavid Sterba btrfs_set_token_inode_mode(&token, item, inode->i_mode);
4157cc4c13d5SDavid Sterba btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
41580b1c6ccaSJosef Bacik
4159cc4c13d5SDavid Sterba btrfs_set_token_timespec_sec(&token, &item->atime,
4160cc4c13d5SDavid Sterba inode->i_atime.tv_sec);
4161cc4c13d5SDavid Sterba btrfs_set_token_timespec_nsec(&token, &item->atime,
4162cc4c13d5SDavid Sterba inode->i_atime.tv_nsec);
41630b1c6ccaSJosef Bacik
4164cc4c13d5SDavid Sterba btrfs_set_token_timespec_sec(&token, &item->mtime,
4165cc4c13d5SDavid Sterba inode->i_mtime.tv_sec);
4166cc4c13d5SDavid Sterba btrfs_set_token_timespec_nsec(&token, &item->mtime,
4167cc4c13d5SDavid Sterba inode->i_mtime.tv_nsec);
41680b1c6ccaSJosef Bacik
4169cc4c13d5SDavid Sterba btrfs_set_token_timespec_sec(&token, &item->ctime,
41702a9462deSJeff Layton inode_get_ctime(inode).tv_sec);
4171cc4c13d5SDavid Sterba btrfs_set_token_timespec_nsec(&token, &item->ctime,
41722a9462deSJeff Layton inode_get_ctime(inode).tv_nsec);
41730b1c6ccaSJosef Bacik
4174e593e54eSFilipe Manana /*
4175e593e54eSFilipe Manana * We do not need to set the nbytes field, in fact during a fast fsync
4176e593e54eSFilipe Manana * its value may not even be correct, since a fast fsync does not wait
4177e593e54eSFilipe Manana * for ordered extent completion, which is where we update nbytes, it
4178e593e54eSFilipe Manana * only waits for writeback to complete. During log replay as we find
4179e593e54eSFilipe Manana * file extent items and replay them, we adjust the nbytes field of the
4180e593e54eSFilipe Manana * inode item in subvolume tree as needed (see overwrite_item()).
4181e593e54eSFilipe Manana */
41820b1c6ccaSJosef Bacik
4183cc4c13d5SDavid Sterba btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
4184cc4c13d5SDavid Sterba btrfs_set_token_inode_transid(&token, item, trans->transid);
4185cc4c13d5SDavid Sterba btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
418677eea05eSBoris Burkov flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
418777eea05eSBoris Burkov BTRFS_I(inode)->ro_flags);
418877eea05eSBoris Burkov btrfs_set_token_inode_flags(&token, item, flags);
4189cc4c13d5SDavid Sterba btrfs_set_token_inode_block_group(&token, item, 0);
419094edf4aeSJosef Bacik }
419194edf4aeSJosef Bacik
log_inode_item(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,struct btrfs_inode * inode,bool inode_item_dropped)4192a95249b3SJosef Bacik static int log_inode_item(struct btrfs_trans_handle *trans,
4193a95249b3SJosef Bacik struct btrfs_root *log, struct btrfs_path *path,
41942ac691d8SFilipe Manana struct btrfs_inode *inode, bool inode_item_dropped)
4195a95249b3SJosef Bacik {
4196a95249b3SJosef Bacik struct btrfs_inode_item *inode_item;
4197a95249b3SJosef Bacik int ret;
4198a95249b3SJosef Bacik
41992ac691d8SFilipe Manana /*
42002ac691d8SFilipe Manana * If we are doing a fast fsync and the inode was logged before in the
42012ac691d8SFilipe Manana * current transaction, then we know the inode was previously logged and
42022ac691d8SFilipe Manana * it exists in the log tree. For performance reasons, in this case use
42032ac691d8SFilipe Manana * btrfs_search_slot() directly with ins_len set to 0 so that we never
42042ac691d8SFilipe Manana * attempt a write lock on the leaf's parent, which adds unnecessary lock
42052ac691d8SFilipe Manana * contention in case there are concurrent fsyncs for other inodes of the
42062ac691d8SFilipe Manana * same subvolume. Using btrfs_insert_empty_item() when the inode item
42072ac691d8SFilipe Manana * already exists can also result in unnecessarily splitting a leaf.
42082ac691d8SFilipe Manana */
42092ac691d8SFilipe Manana if (!inode_item_dropped && inode->logged_trans == trans->transid) {
42102ac691d8SFilipe Manana ret = btrfs_search_slot(trans, log, &inode->location, path, 0, 1);
42112ac691d8SFilipe Manana ASSERT(ret <= 0);
42122ac691d8SFilipe Manana if (ret > 0)
42132ac691d8SFilipe Manana ret = -ENOENT;
42142ac691d8SFilipe Manana } else {
42152ac691d8SFilipe Manana /*
42162ac691d8SFilipe Manana * This means it is the first fsync in the current transaction,
42172ac691d8SFilipe Manana * so the inode item is not in the log and we need to insert it.
42182ac691d8SFilipe Manana * We can never get -EEXIST because we are only called for a fast
42192ac691d8SFilipe Manana * fsync and in case an inode eviction happens after the inode was
42202ac691d8SFilipe Manana * logged before in the current transaction, when we load again
42212ac691d8SFilipe Manana * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime
42222ac691d8SFilipe Manana * flags and set ->logged_trans to 0.
42232ac691d8SFilipe Manana */
42242ac691d8SFilipe Manana ret = btrfs_insert_empty_item(trans, log, path, &inode->location,
42252ac691d8SFilipe Manana sizeof(*inode_item));
42262ac691d8SFilipe Manana ASSERT(ret != -EEXIST);
42272ac691d8SFilipe Manana }
42282ac691d8SFilipe Manana if (ret)
4229a95249b3SJosef Bacik return ret;
4230a95249b3SJosef Bacik inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4231a95249b3SJosef Bacik struct btrfs_inode_item);
42326d889a3bSNikolay Borisov fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
42336d889a3bSNikolay Borisov 0, 0);
4234a95249b3SJosef Bacik btrfs_release_path(path);
4235a95249b3SJosef Bacik return 0;
4236a95249b3SJosef Bacik }
4237a95249b3SJosef Bacik
log_csums(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_root * log_root,struct btrfs_ordered_sum * sums)423840e046acSFilipe Manana static int log_csums(struct btrfs_trans_handle *trans,
42393ebac17cSFilipe Manana struct btrfs_inode *inode,
424040e046acSFilipe Manana struct btrfs_root *log_root,
424140e046acSFilipe Manana struct btrfs_ordered_sum *sums)
424240e046acSFilipe Manana {
42435cfe76f8SChristoph Hellwig const u64 lock_end = sums->logical + sums->len - 1;
4244e289f03eSFilipe Manana struct extent_state *cached_state = NULL;
424540e046acSFilipe Manana int ret;
424640e046acSFilipe Manana
424740e046acSFilipe Manana /*
42483ebac17cSFilipe Manana * If this inode was not used for reflink operations in the current
42493ebac17cSFilipe Manana * transaction with new extents, then do the fast path, no need to
42503ebac17cSFilipe Manana * worry about logging checksum items with overlapping ranges.
42513ebac17cSFilipe Manana */
42523ebac17cSFilipe Manana if (inode->last_reflink_trans < trans->transid)
42533ebac17cSFilipe Manana return btrfs_csum_file_blocks(trans, log_root, sums);
42543ebac17cSFilipe Manana
42553ebac17cSFilipe Manana /*
4256e289f03eSFilipe Manana * Serialize logging for checksums. This is to avoid racing with the
4257e289f03eSFilipe Manana * same checksum being logged by another task that is logging another
4258e289f03eSFilipe Manana * file which happens to refer to the same extent as well. Such races
4259e289f03eSFilipe Manana * can leave checksum items in the log with overlapping ranges.
4260e289f03eSFilipe Manana */
42615cfe76f8SChristoph Hellwig ret = lock_extent(&log_root->log_csum_range, sums->logical, lock_end,
4262570eb97bSJosef Bacik &cached_state);
4263e289f03eSFilipe Manana if (ret)
4264e289f03eSFilipe Manana return ret;
4265e289f03eSFilipe Manana /*
426640e046acSFilipe Manana * Due to extent cloning, we might have logged a csum item that covers a
426740e046acSFilipe Manana * subrange of a cloned extent, and later we can end up logging a csum
426840e046acSFilipe Manana * item for a larger subrange of the same extent or the entire range.
426940e046acSFilipe Manana * This would leave csum items in the log tree that cover the same range
427040e046acSFilipe Manana * and break the searches for checksums in the log tree, resulting in
427140e046acSFilipe Manana * some checksums missing in the fs/subvolume tree. So just delete (or
427240e046acSFilipe Manana * trim and adjust) any existing csum items in the log for this range.
427340e046acSFilipe Manana */
42745cfe76f8SChristoph Hellwig ret = btrfs_del_csums(trans, log_root, sums->logical, sums->len);
4275e289f03eSFilipe Manana if (!ret)
4276e289f03eSFilipe Manana ret = btrfs_csum_file_blocks(trans, log_root, sums);
427740e046acSFilipe Manana
42785cfe76f8SChristoph Hellwig unlock_extent(&log_root->log_csum_range, sums->logical, lock_end,
4279e289f03eSFilipe Manana &cached_state);
4280e289f03eSFilipe Manana
4281e289f03eSFilipe Manana return ret;
428240e046acSFilipe Manana }
428340e046acSFilipe Manana
copy_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * dst_path,struct btrfs_path * src_path,int start_slot,int nr,int inode_only,u64 logged_isize)428431ff1cd2SChris Mason static noinline int copy_items(struct btrfs_trans_handle *trans,
428544d70e19SNikolay Borisov struct btrfs_inode *inode,
428631ff1cd2SChris Mason struct btrfs_path *dst_path,
42870e56315cSFilipe Manana struct btrfs_path *src_path,
42881a4bcf47SFilipe Manana int start_slot, int nr, int inode_only,
42891a4bcf47SFilipe Manana u64 logged_isize)
429031ff1cd2SChris Mason {
429144d70e19SNikolay Borisov struct btrfs_root *log = inode->root->log_root;
429231ff1cd2SChris Mason struct btrfs_file_extent_item *extent;
4293796787c9SFilipe Manana struct extent_buffer *src;
42947f30c072SFilipe Manana int ret = 0;
429531ff1cd2SChris Mason struct btrfs_key *ins_keys;
429631ff1cd2SChris Mason u32 *ins_sizes;
4297b7ef5f3aSFilipe Manana struct btrfs_item_batch batch;
429831ff1cd2SChris Mason char *ins_data;
429931ff1cd2SChris Mason int i;
43007f30c072SFilipe Manana int dst_index;
43017f30c072SFilipe Manana const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM);
43027f30c072SFilipe Manana const u64 i_size = i_size_read(&inode->vfs_inode);
4303d20f7043SChris Mason
4304796787c9SFilipe Manana /*
4305796787c9SFilipe Manana * To keep lockdep happy and avoid deadlocks, clone the source leaf and
4306796787c9SFilipe Manana * use the clone. This is because otherwise we would be changing the log
4307796787c9SFilipe Manana * tree, to insert items from the subvolume tree or insert csum items,
4308796787c9SFilipe Manana * while holding a read lock on a leaf from the subvolume tree, which
4309796787c9SFilipe Manana * creates a nasty lock dependency when COWing log tree nodes/leaves:
4310796787c9SFilipe Manana *
4311796787c9SFilipe Manana * 1) Modifying the log tree triggers an extent buffer allocation while
4312796787c9SFilipe Manana * holding a write lock on a parent extent buffer from the log tree.
4313796787c9SFilipe Manana * Allocating the pages for an extent buffer, or the extent buffer
4314796787c9SFilipe Manana * struct, can trigger inode eviction and finally the inode eviction
4315796787c9SFilipe Manana * will trigger a release/remove of a delayed node, which requires
4316796787c9SFilipe Manana * taking the delayed node's mutex;
4317796787c9SFilipe Manana *
4318796787c9SFilipe Manana * 2) Allocating a metadata extent for a log tree can trigger the async
4319796787c9SFilipe Manana * reclaim thread and make us wait for it to release enough space and
4320796787c9SFilipe Manana * unblock our reservation ticket. The reclaim thread can start
4321796787c9SFilipe Manana * flushing delayed items, and that in turn results in the need to
4322796787c9SFilipe Manana * lock delayed node mutexes and in the need to write lock extent
4323796787c9SFilipe Manana * buffers of a subvolume tree - all this while holding a write lock
4324796787c9SFilipe Manana * on the parent extent buffer in the log tree.
4325796787c9SFilipe Manana *
4326796787c9SFilipe Manana * So one task in scenario 1) running in parallel with another task in
4327796787c9SFilipe Manana * scenario 2) could lead to a deadlock, one wanting to lock a delayed
4328796787c9SFilipe Manana * node mutex while having a read lock on a leaf from the subvolume,
4329796787c9SFilipe Manana * while the other is holding the delayed node's mutex and wants to
4330796787c9SFilipe Manana * write lock the same subvolume leaf for flushing delayed items.
4331796787c9SFilipe Manana */
4332796787c9SFilipe Manana src = btrfs_clone_extent_buffer(src_path->nodes[0]);
4333796787c9SFilipe Manana if (!src)
4334796787c9SFilipe Manana return -ENOMEM;
4335796787c9SFilipe Manana
4336796787c9SFilipe Manana i = src_path->slots[0];
4337796787c9SFilipe Manana btrfs_release_path(src_path);
4338796787c9SFilipe Manana src_path->nodes[0] = src;
4339796787c9SFilipe Manana src_path->slots[0] = i;
4340796787c9SFilipe Manana
434131ff1cd2SChris Mason ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
434231ff1cd2SChris Mason nr * sizeof(u32), GFP_NOFS);
43432a29edc6Sliubo if (!ins_data)
43442a29edc6Sliubo return -ENOMEM;
43452a29edc6Sliubo
434631ff1cd2SChris Mason ins_sizes = (u32 *)ins_data;
434731ff1cd2SChris Mason ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
4348b7ef5f3aSFilipe Manana batch.keys = ins_keys;
4349b7ef5f3aSFilipe Manana batch.data_sizes = ins_sizes;
4350b7ef5f3aSFilipe Manana batch.total_data_size = 0;
43517f30c072SFilipe Manana batch.nr = 0;
435231ff1cd2SChris Mason
43537f30c072SFilipe Manana dst_index = 0;
435431ff1cd2SChris Mason for (i = 0; i < nr; i++) {
43557f30c072SFilipe Manana const int src_slot = start_slot + i;
43567f30c072SFilipe Manana struct btrfs_root *csum_root;
43575b7ce5e2SFilipe Manana struct btrfs_ordered_sum *sums;
43585b7ce5e2SFilipe Manana struct btrfs_ordered_sum *sums_next;
43595b7ce5e2SFilipe Manana LIST_HEAD(ordered_sums);
43607f30c072SFilipe Manana u64 disk_bytenr;
43617f30c072SFilipe Manana u64 disk_num_bytes;
43627f30c072SFilipe Manana u64 extent_offset;
43637f30c072SFilipe Manana u64 extent_num_bytes;
43647f30c072SFilipe Manana bool is_old_extent;
43657f30c072SFilipe Manana
43667f30c072SFilipe Manana btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot);
43677f30c072SFilipe Manana
43687f30c072SFilipe Manana if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY)
43697f30c072SFilipe Manana goto add_to_batch;
43707f30c072SFilipe Manana
43717f30c072SFilipe Manana extent = btrfs_item_ptr(src, src_slot,
43727f30c072SFilipe Manana struct btrfs_file_extent_item);
43737f30c072SFilipe Manana
43747f30c072SFilipe Manana is_old_extent = (btrfs_file_extent_generation(src, extent) <
43757f30c072SFilipe Manana trans->transid);
43767f30c072SFilipe Manana
43777f30c072SFilipe Manana /*
43787f30c072SFilipe Manana * Don't copy extents from past generations. That would make us
43797f30c072SFilipe Manana * log a lot more metadata for common cases like doing only a
43807f30c072SFilipe Manana * few random writes into a file and then fsync it for the first
43817f30c072SFilipe Manana * time or after the full sync flag is set on the inode. We can
43827f30c072SFilipe Manana * get leaves full of extent items, most of which are from past
43837f30c072SFilipe Manana * generations, so we can skip them - as long as the inode has
43847f30c072SFilipe Manana * not been the target of a reflink operation in this transaction,
43857f30c072SFilipe Manana * as in that case it might have had file extent items with old
43867f30c072SFilipe Manana * generations copied into it. We also must always log prealloc
43877f30c072SFilipe Manana * extents that start at or beyond eof, otherwise we would lose
43887f30c072SFilipe Manana * them on log replay.
43897f30c072SFilipe Manana */
43907f30c072SFilipe Manana if (is_old_extent &&
43917f30c072SFilipe Manana ins_keys[dst_index].offset < i_size &&
43927f30c072SFilipe Manana inode->last_reflink_trans < trans->transid)
43937f30c072SFilipe Manana continue;
43947f30c072SFilipe Manana
43957f30c072SFilipe Manana if (skip_csum)
43967f30c072SFilipe Manana goto add_to_batch;
43977f30c072SFilipe Manana
43987f30c072SFilipe Manana /* Only regular extents have checksums. */
43997f30c072SFilipe Manana if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG)
44007f30c072SFilipe Manana goto add_to_batch;
44017f30c072SFilipe Manana
44027f30c072SFilipe Manana /*
44037f30c072SFilipe Manana * If it's an extent created in a past transaction, then its
44047f30c072SFilipe Manana * checksums are already accessible from the committed csum tree,
44057f30c072SFilipe Manana * no need to log them.
44067f30c072SFilipe Manana */
44077f30c072SFilipe Manana if (is_old_extent)
44087f30c072SFilipe Manana goto add_to_batch;
44097f30c072SFilipe Manana
44107f30c072SFilipe Manana disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent);
44117f30c072SFilipe Manana /* If it's an explicit hole, there are no checksums. */
44127f30c072SFilipe Manana if (disk_bytenr == 0)
44137f30c072SFilipe Manana goto add_to_batch;
44147f30c072SFilipe Manana
44157f30c072SFilipe Manana disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent);
44167f30c072SFilipe Manana
44177f30c072SFilipe Manana if (btrfs_file_extent_compression(src, extent)) {
44187f30c072SFilipe Manana extent_offset = 0;
44197f30c072SFilipe Manana extent_num_bytes = disk_num_bytes;
44207f30c072SFilipe Manana } else {
44217f30c072SFilipe Manana extent_offset = btrfs_file_extent_offset(src, extent);
44227f30c072SFilipe Manana extent_num_bytes = btrfs_file_extent_num_bytes(src, extent);
442331ff1cd2SChris Mason }
44247f30c072SFilipe Manana
44257f30c072SFilipe Manana csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr);
44267f30c072SFilipe Manana disk_bytenr += extent_offset;
442797e38239SQu Wenruo ret = btrfs_lookup_csums_list(csum_root, disk_bytenr,
44287f30c072SFilipe Manana disk_bytenr + extent_num_bytes - 1,
442926ce9114SJosef Bacik &ordered_sums, 0, false);
44307f30c072SFilipe Manana if (ret)
44317f30c072SFilipe Manana goto out;
44327f30c072SFilipe Manana
44335b7ce5e2SFilipe Manana list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) {
44345b7ce5e2SFilipe Manana if (!ret)
44355b7ce5e2SFilipe Manana ret = log_csums(trans, inode, log, sums);
44365b7ce5e2SFilipe Manana list_del(&sums->list);
44375b7ce5e2SFilipe Manana kfree(sums);
44385b7ce5e2SFilipe Manana }
44395b7ce5e2SFilipe Manana if (ret)
44405b7ce5e2SFilipe Manana goto out;
44415b7ce5e2SFilipe Manana
44427f30c072SFilipe Manana add_to_batch:
44437f30c072SFilipe Manana ins_sizes[dst_index] = btrfs_item_size(src, src_slot);
44447f30c072SFilipe Manana batch.total_data_size += ins_sizes[dst_index];
44457f30c072SFilipe Manana batch.nr++;
44467f30c072SFilipe Manana dst_index++;
44477f30c072SFilipe Manana }
44487f30c072SFilipe Manana
44497f30c072SFilipe Manana /*
44507f30c072SFilipe Manana * We have a leaf full of old extent items that don't need to be logged,
44517f30c072SFilipe Manana * so we don't need to do anything.
44527f30c072SFilipe Manana */
44537f30c072SFilipe Manana if (batch.nr == 0)
44547f30c072SFilipe Manana goto out;
44557f30c072SFilipe Manana
4456b7ef5f3aSFilipe Manana ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
44577f30c072SFilipe Manana if (ret)
44587f30c072SFilipe Manana goto out;
445931ff1cd2SChris Mason
44607f30c072SFilipe Manana dst_index = 0;
44617f30c072SFilipe Manana for (i = 0; i < nr; i++) {
44627f30c072SFilipe Manana const int src_slot = start_slot + i;
44637f30c072SFilipe Manana const int dst_slot = dst_path->slots[0] + dst_index;
44647f30c072SFilipe Manana struct btrfs_key key;
44657f30c072SFilipe Manana unsigned long src_offset;
44667f30c072SFilipe Manana unsigned long dst_offset;
446731ff1cd2SChris Mason
44687f30c072SFilipe Manana /*
44697f30c072SFilipe Manana * We're done, all the remaining items in the source leaf
44707f30c072SFilipe Manana * correspond to old file extent items.
44717f30c072SFilipe Manana */
44727f30c072SFilipe Manana if (dst_index >= batch.nr)
44737f30c072SFilipe Manana break;
447431ff1cd2SChris Mason
44757f30c072SFilipe Manana btrfs_item_key_to_cpu(src, &key, src_slot);
44767f30c072SFilipe Manana
44777f30c072SFilipe Manana if (key.type != BTRFS_EXTENT_DATA_KEY)
44787f30c072SFilipe Manana goto copy_item;
44797f30c072SFilipe Manana
44807f30c072SFilipe Manana extent = btrfs_item_ptr(src, src_slot,
44817f30c072SFilipe Manana struct btrfs_file_extent_item);
44827f30c072SFilipe Manana
44837f30c072SFilipe Manana /* See the comment in the previous loop, same logic. */
44847f30c072SFilipe Manana if (btrfs_file_extent_generation(src, extent) < trans->transid &&
44857f30c072SFilipe Manana key.offset < i_size &&
44867f30c072SFilipe Manana inode->last_reflink_trans < trans->transid)
44877f30c072SFilipe Manana continue;
44887f30c072SFilipe Manana
44897f30c072SFilipe Manana copy_item:
44907f30c072SFilipe Manana dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot);
44917f30c072SFilipe Manana src_offset = btrfs_item_ptr_offset(src, src_slot);
44927f30c072SFilipe Manana
44937f30c072SFilipe Manana if (key.type == BTRFS_INODE_ITEM_KEY) {
44947f30c072SFilipe Manana struct btrfs_inode_item *inode_item;
44957f30c072SFilipe Manana
44967f30c072SFilipe Manana inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot,
449731ff1cd2SChris Mason struct btrfs_inode_item);
449894edf4aeSJosef Bacik fill_inode_item(trans, dst_path->nodes[0], inode_item,
4499f85b7379SDavid Sterba &inode->vfs_inode,
4500f85b7379SDavid Sterba inode_only == LOG_INODE_EXISTS,
45011a4bcf47SFilipe Manana logged_isize);
450294edf4aeSJosef Bacik } else {
450394edf4aeSJosef Bacik copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
45047f30c072SFilipe Manana src_offset, ins_sizes[dst_index]);
450531ff1cd2SChris Mason }
450694edf4aeSJosef Bacik
45077f30c072SFilipe Manana dst_index++;
450831ff1cd2SChris Mason }
450931ff1cd2SChris Mason
4510d5e09e38SFilipe Manana btrfs_mark_buffer_dirty(trans, dst_path->nodes[0]);
4511b3b4aa74SDavid Sterba btrfs_release_path(dst_path);
45127f30c072SFilipe Manana out:
451331ff1cd2SChris Mason kfree(ins_data);
4514d20f7043SChris Mason
45154a500fd1SYan, Zheng return ret;
451631ff1cd2SChris Mason }
451731ff1cd2SChris Mason
extent_cmp(void * priv,const struct list_head * a,const struct list_head * b)45184f0f586bSSami Tolvanen static int extent_cmp(void *priv, const struct list_head *a,
45194f0f586bSSami Tolvanen const struct list_head *b)
45205dc562c5SJosef Bacik {
4521214cc184SDavid Sterba const struct extent_map *em1, *em2;
45225dc562c5SJosef Bacik
45235dc562c5SJosef Bacik em1 = list_entry(a, struct extent_map, list);
45245dc562c5SJosef Bacik em2 = list_entry(b, struct extent_map, list);
45255dc562c5SJosef Bacik
45265dc562c5SJosef Bacik if (em1->start < em2->start)
45275dc562c5SJosef Bacik return -1;
45285dc562c5SJosef Bacik else if (em1->start > em2->start)
45295dc562c5SJosef Bacik return 1;
45305dc562c5SJosef Bacik return 0;
45315dc562c5SJosef Bacik }
45325dc562c5SJosef Bacik
log_extent_csums(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_root * log_root,const struct extent_map * em,struct btrfs_log_ctx * ctx)4533e7175a69SJosef Bacik static int log_extent_csums(struct btrfs_trans_handle *trans,
4534e7175a69SJosef Bacik struct btrfs_inode *inode,
4535a9ecb653SNikolay Borisov struct btrfs_root *log_root,
453648778179SFilipe Manana const struct extent_map *em,
453748778179SFilipe Manana struct btrfs_log_ctx *ctx)
45385dc562c5SJosef Bacik {
453948778179SFilipe Manana struct btrfs_ordered_extent *ordered;
4540fc28b25eSJosef Bacik struct btrfs_root *csum_root;
45412ab28f32SJosef Bacik u64 csum_offset;
45422ab28f32SJosef Bacik u64 csum_len;
454348778179SFilipe Manana u64 mod_start = em->mod_start;
454448778179SFilipe Manana u64 mod_len = em->mod_len;
45458407f553SFilipe Manana LIST_HEAD(ordered_sums);
45468407f553SFilipe Manana int ret = 0;
454709a2a8f9SJosef Bacik
4548e7175a69SJosef Bacik if (inode->flags & BTRFS_INODE_NODATASUM ||
4549e7175a69SJosef Bacik test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
45508407f553SFilipe Manana em->block_start == EXTENT_MAP_HOLE)
455170c8a91cSJosef Bacik return 0;
455270c8a91cSJosef Bacik
455348778179SFilipe Manana list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
455448778179SFilipe Manana const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
455548778179SFilipe Manana const u64 mod_end = mod_start + mod_len;
455648778179SFilipe Manana struct btrfs_ordered_sum *sums;
455748778179SFilipe Manana
455848778179SFilipe Manana if (mod_len == 0)
455948778179SFilipe Manana break;
456048778179SFilipe Manana
456148778179SFilipe Manana if (ordered_end <= mod_start)
456248778179SFilipe Manana continue;
456348778179SFilipe Manana if (mod_end <= ordered->file_offset)
456448778179SFilipe Manana break;
456548778179SFilipe Manana
456648778179SFilipe Manana /*
456748778179SFilipe Manana * We are going to copy all the csums on this ordered extent, so
456848778179SFilipe Manana * go ahead and adjust mod_start and mod_len in case this ordered
456948778179SFilipe Manana * extent has already been logged.
457048778179SFilipe Manana */
457148778179SFilipe Manana if (ordered->file_offset > mod_start) {
457248778179SFilipe Manana if (ordered_end >= mod_end)
457348778179SFilipe Manana mod_len = ordered->file_offset - mod_start;
457448778179SFilipe Manana /*
457548778179SFilipe Manana * If we have this case
457648778179SFilipe Manana *
457748778179SFilipe Manana * |--------- logged extent ---------|
457848778179SFilipe Manana * |----- ordered extent ----|
457948778179SFilipe Manana *
458048778179SFilipe Manana * Just don't mess with mod_start and mod_len, we'll
458148778179SFilipe Manana * just end up logging more csums than we need and it
458248778179SFilipe Manana * will be ok.
458348778179SFilipe Manana */
458448778179SFilipe Manana } else {
458548778179SFilipe Manana if (ordered_end < mod_end) {
458648778179SFilipe Manana mod_len = mod_end - ordered_end;
458748778179SFilipe Manana mod_start = ordered_end;
458848778179SFilipe Manana } else {
458948778179SFilipe Manana mod_len = 0;
459048778179SFilipe Manana }
459148778179SFilipe Manana }
459248778179SFilipe Manana
459348778179SFilipe Manana /*
459448778179SFilipe Manana * To keep us from looping for the above case of an ordered
459548778179SFilipe Manana * extent that falls inside of the logged extent.
459648778179SFilipe Manana */
459748778179SFilipe Manana if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
459848778179SFilipe Manana continue;
459948778179SFilipe Manana
460048778179SFilipe Manana list_for_each_entry(sums, &ordered->list, list) {
460148778179SFilipe Manana ret = log_csums(trans, inode, log_root, sums);
460248778179SFilipe Manana if (ret)
460348778179SFilipe Manana return ret;
460448778179SFilipe Manana }
460548778179SFilipe Manana }
460648778179SFilipe Manana
460748778179SFilipe Manana /* We're done, found all csums in the ordered extents. */
460848778179SFilipe Manana if (mod_len == 0)
460948778179SFilipe Manana return 0;
461048778179SFilipe Manana
4611e7175a69SJosef Bacik /* If we're compressed we have to save the entire range of csums. */
4612488111aaSFilipe David Borba Manana if (em->compress_type) {
4613488111aaSFilipe David Borba Manana csum_offset = 0;
46148407f553SFilipe Manana csum_len = max(em->block_len, em->orig_block_len);
4615488111aaSFilipe David Borba Manana } else {
461648778179SFilipe Manana csum_offset = mod_start - em->start;
461748778179SFilipe Manana csum_len = mod_len;
4618488111aaSFilipe David Borba Manana }
46192ab28f32SJosef Bacik
462070c8a91cSJosef Bacik /* block start is already adjusted for the file extent offset. */
4621fc28b25eSJosef Bacik csum_root = btrfs_csum_root(trans->fs_info, em->block_start);
462297e38239SQu Wenruo ret = btrfs_lookup_csums_list(csum_root, em->block_start + csum_offset,
462370c8a91cSJosef Bacik em->block_start + csum_offset +
462426ce9114SJosef Bacik csum_len - 1, &ordered_sums, 0, false);
46255dc562c5SJosef Bacik if (ret)
46265dc562c5SJosef Bacik return ret;
462770c8a91cSJosef Bacik
462870c8a91cSJosef Bacik while (!list_empty(&ordered_sums)) {
462970c8a91cSJosef Bacik struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
463070c8a91cSJosef Bacik struct btrfs_ordered_sum,
463170c8a91cSJosef Bacik list);
463270c8a91cSJosef Bacik if (!ret)
46333ebac17cSFilipe Manana ret = log_csums(trans, inode, log_root, sums);
463470c8a91cSJosef Bacik list_del(&sums->list);
463570c8a91cSJosef Bacik kfree(sums);
46365dc562c5SJosef Bacik }
46375dc562c5SJosef Bacik
463870c8a91cSJosef Bacik return ret;
46395dc562c5SJosef Bacik }
46405dc562c5SJosef Bacik
log_one_extent(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,const struct extent_map * em,struct btrfs_path * path,struct btrfs_log_ctx * ctx)46418407f553SFilipe Manana static int log_one_extent(struct btrfs_trans_handle *trans,
464290d04510SFilipe Manana struct btrfs_inode *inode,
46438407f553SFilipe Manana const struct extent_map *em,
46448407f553SFilipe Manana struct btrfs_path *path,
46458407f553SFilipe Manana struct btrfs_log_ctx *ctx)
46468407f553SFilipe Manana {
46475893dfb9SFilipe Manana struct btrfs_drop_extents_args drop_args = { 0 };
464890d04510SFilipe Manana struct btrfs_root *log = inode->root->log_root;
4649e1f53ed8SFilipe Manana struct btrfs_file_extent_item fi = { 0 };
46508407f553SFilipe Manana struct extent_buffer *leaf;
46518407f553SFilipe Manana struct btrfs_key key;
46528407f553SFilipe Manana u64 extent_offset = em->start - em->orig_start;
46538407f553SFilipe Manana u64 block_len;
46548407f553SFilipe Manana int ret;
46558407f553SFilipe Manana
4656e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_generation(&fi, trans->transid);
4657e1f53ed8SFilipe Manana if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4658e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);
4659e1f53ed8SFilipe Manana else
4660e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);
4661e1f53ed8SFilipe Manana
4662e1f53ed8SFilipe Manana block_len = max(em->block_len, em->orig_block_len);
4663e1f53ed8SFilipe Manana if (em->compress_type != BTRFS_COMPRESS_NONE) {
4664e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start);
4665e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
4666e1f53ed8SFilipe Manana } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4667e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start -
4668e1f53ed8SFilipe Manana extent_offset);
4669e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
4670e1f53ed8SFilipe Manana }
4671e1f53ed8SFilipe Manana
4672e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_offset(&fi, extent_offset);
4673e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_num_bytes(&fi, em->len);
4674e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes);
4675e1f53ed8SFilipe Manana btrfs_set_stack_file_extent_compression(&fi, em->compress_type);
4676e1f53ed8SFilipe Manana
467748778179SFilipe Manana ret = log_extent_csums(trans, inode, log, em, ctx);
46788407f553SFilipe Manana if (ret)
46798407f553SFilipe Manana return ret;
46808407f553SFilipe Manana
46815328b2a7SFilipe Manana /*
46825328b2a7SFilipe Manana * If this is the first time we are logging the inode in the current
46835328b2a7SFilipe Manana * transaction, we can avoid btrfs_drop_extents(), which is expensive
46845328b2a7SFilipe Manana * because it does a deletion search, which always acquires write locks
46855328b2a7SFilipe Manana * for extent buffers at levels 2, 1 and 0. This not only wastes time
46865328b2a7SFilipe Manana * but also adds significant contention in a log tree, since log trees
46875328b2a7SFilipe Manana * are small, with a root at level 2 or 3 at most, due to their short
46885328b2a7SFilipe Manana * life span.
46895328b2a7SFilipe Manana */
46900f8ce498SFilipe Manana if (ctx->logged_before) {
46915893dfb9SFilipe Manana drop_args.path = path;
46925893dfb9SFilipe Manana drop_args.start = em->start;
46935893dfb9SFilipe Manana drop_args.end = em->start + em->len;
46945893dfb9SFilipe Manana drop_args.replace_extent = true;
4695e1f53ed8SFilipe Manana drop_args.extent_item_size = sizeof(fi);
46965893dfb9SFilipe Manana ret = btrfs_drop_extents(trans, log, inode, &drop_args);
46978407f553SFilipe Manana if (ret)
46988407f553SFilipe Manana return ret;
46995328b2a7SFilipe Manana }
47008407f553SFilipe Manana
47015893dfb9SFilipe Manana if (!drop_args.extent_inserted) {
47029d122629SNikolay Borisov key.objectid = btrfs_ino(inode);
47038407f553SFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY;
47048407f553SFilipe Manana key.offset = em->start;
47058407f553SFilipe Manana
47068407f553SFilipe Manana ret = btrfs_insert_empty_item(trans, log, path, &key,
4707e1f53ed8SFilipe Manana sizeof(fi));
47088407f553SFilipe Manana if (ret)
47098407f553SFilipe Manana return ret;
47108407f553SFilipe Manana }
47118407f553SFilipe Manana leaf = path->nodes[0];
4712e1f53ed8SFilipe Manana write_extent_buffer(leaf, &fi,
4713e1f53ed8SFilipe Manana btrfs_item_ptr_offset(leaf, path->slots[0]),
4714e1f53ed8SFilipe Manana sizeof(fi));
4715d5e09e38SFilipe Manana btrfs_mark_buffer_dirty(trans, leaf);
47168407f553SFilipe Manana
47178407f553SFilipe Manana btrfs_release_path(path);
47188407f553SFilipe Manana
47198407f553SFilipe Manana return ret;
47208407f553SFilipe Manana }
47218407f553SFilipe Manana
472231d11b83SFilipe Manana /*
472331d11b83SFilipe Manana * Log all prealloc extents beyond the inode's i_size to make sure we do not
4724d9947887SFilipe Manana * lose them after doing a full/fast fsync and replaying the log. We scan the
472531d11b83SFilipe Manana * subvolume's root instead of iterating the inode's extent map tree because
472631d11b83SFilipe Manana * otherwise we can log incorrect extent items based on extent map conversion.
472731d11b83SFilipe Manana * That can happen due to the fact that extent maps are merged when they
472831d11b83SFilipe Manana * are not in the extent map tree's list of modified extents.
472931d11b83SFilipe Manana */
btrfs_log_prealloc_extents(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path)473031d11b83SFilipe Manana static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
473131d11b83SFilipe Manana struct btrfs_inode *inode,
473231d11b83SFilipe Manana struct btrfs_path *path)
473331d11b83SFilipe Manana {
473431d11b83SFilipe Manana struct btrfs_root *root = inode->root;
473531d11b83SFilipe Manana struct btrfs_key key;
473631d11b83SFilipe Manana const u64 i_size = i_size_read(&inode->vfs_inode);
473731d11b83SFilipe Manana const u64 ino = btrfs_ino(inode);
473831d11b83SFilipe Manana struct btrfs_path *dst_path = NULL;
47390e56315cSFilipe Manana bool dropped_extents = false;
4740f135cea3SFilipe Manana u64 truncate_offset = i_size;
4741f135cea3SFilipe Manana struct extent_buffer *leaf;
4742f135cea3SFilipe Manana int slot;
474331d11b83SFilipe Manana int ins_nr = 0;
4744b4c639f6SJosef Bacik int start_slot = 0;
474531d11b83SFilipe Manana int ret;
474631d11b83SFilipe Manana
474731d11b83SFilipe Manana if (!(inode->flags & BTRFS_INODE_PREALLOC))
474831d11b83SFilipe Manana return 0;
474931d11b83SFilipe Manana
475031d11b83SFilipe Manana key.objectid = ino;
475131d11b83SFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY;
475231d11b83SFilipe Manana key.offset = i_size;
475331d11b83SFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
475431d11b83SFilipe Manana if (ret < 0)
475531d11b83SFilipe Manana goto out;
475631d11b83SFilipe Manana
4757f135cea3SFilipe Manana /*
4758f135cea3SFilipe Manana * We must check if there is a prealloc extent that starts before the
4759f135cea3SFilipe Manana * i_size and crosses the i_size boundary. This is to ensure later we
4760f135cea3SFilipe Manana * truncate down to the end of that extent and not to the i_size, as
4761f135cea3SFilipe Manana * otherwise we end up losing part of the prealloc extent after a log
4762f135cea3SFilipe Manana * replay and with an implicit hole if there is another prealloc extent
4763f135cea3SFilipe Manana * that starts at an offset beyond i_size.
4764f135cea3SFilipe Manana */
4765f135cea3SFilipe Manana ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4766f135cea3SFilipe Manana if (ret < 0)
4767f135cea3SFilipe Manana goto out;
4768f135cea3SFilipe Manana
4769f135cea3SFilipe Manana if (ret == 0) {
4770f135cea3SFilipe Manana struct btrfs_file_extent_item *ei;
4771f135cea3SFilipe Manana
4772f135cea3SFilipe Manana leaf = path->nodes[0];
4773f135cea3SFilipe Manana slot = path->slots[0];
4774f135cea3SFilipe Manana ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4775f135cea3SFilipe Manana
4776f135cea3SFilipe Manana if (btrfs_file_extent_type(leaf, ei) ==
4777f135cea3SFilipe Manana BTRFS_FILE_EXTENT_PREALLOC) {
4778f135cea3SFilipe Manana u64 extent_end;
4779f135cea3SFilipe Manana
4780f135cea3SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot);
4781f135cea3SFilipe Manana extent_end = key.offset +
4782f135cea3SFilipe Manana btrfs_file_extent_num_bytes(leaf, ei);
4783f135cea3SFilipe Manana
4784f135cea3SFilipe Manana if (extent_end > i_size)
4785f135cea3SFilipe Manana truncate_offset = extent_end;
4786f135cea3SFilipe Manana }
4787f135cea3SFilipe Manana } else {
4788f135cea3SFilipe Manana ret = 0;
4789f135cea3SFilipe Manana }
4790f135cea3SFilipe Manana
479131d11b83SFilipe Manana while (true) {
4792f135cea3SFilipe Manana leaf = path->nodes[0];
4793f135cea3SFilipe Manana slot = path->slots[0];
479431d11b83SFilipe Manana
479531d11b83SFilipe Manana if (slot >= btrfs_header_nritems(leaf)) {
479631d11b83SFilipe Manana if (ins_nr > 0) {
479731d11b83SFilipe Manana ret = copy_items(trans, inode, dst_path, path,
47980e56315cSFilipe Manana start_slot, ins_nr, 1, 0);
479931d11b83SFilipe Manana if (ret < 0)
480031d11b83SFilipe Manana goto out;
480131d11b83SFilipe Manana ins_nr = 0;
480231d11b83SFilipe Manana }
480331d11b83SFilipe Manana ret = btrfs_next_leaf(root, path);
480431d11b83SFilipe Manana if (ret < 0)
480531d11b83SFilipe Manana goto out;
480631d11b83SFilipe Manana if (ret > 0) {
480731d11b83SFilipe Manana ret = 0;
480831d11b83SFilipe Manana break;
480931d11b83SFilipe Manana }
481031d11b83SFilipe Manana continue;
481131d11b83SFilipe Manana }
481231d11b83SFilipe Manana
481331d11b83SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot);
481431d11b83SFilipe Manana if (key.objectid > ino)
481531d11b83SFilipe Manana break;
481631d11b83SFilipe Manana if (WARN_ON_ONCE(key.objectid < ino) ||
481731d11b83SFilipe Manana key.type < BTRFS_EXTENT_DATA_KEY ||
481831d11b83SFilipe Manana key.offset < i_size) {
481931d11b83SFilipe Manana path->slots[0]++;
482031d11b83SFilipe Manana continue;
482131d11b83SFilipe Manana }
482231d11b83SFilipe Manana /*
48233d08c52bSOmar Sandoval * Avoid overlapping items in the log tree. The first time we
48243d08c52bSOmar Sandoval * get here, get rid of everything from a past fsync. After
48253d08c52bSOmar Sandoval * that, if the current extent starts before the end of the last
48263d08c52bSOmar Sandoval * extent we copied, truncate the last one. This can happen if
48273d08c52bSOmar Sandoval * an ordered extent completion modifies the subvolume tree
48283d08c52bSOmar Sandoval * while btrfs_next_leaf() has the tree unlocked.
482931d11b83SFilipe Manana */
48303d08c52bSOmar Sandoval if (!dropped_extents || key.offset < truncate_offset) {
48318a2b3da1SFilipe Manana ret = truncate_inode_items(trans, root->log_root, inode,
48323d08c52bSOmar Sandoval min(key.offset, truncate_offset),
48338a2b3da1SFilipe Manana BTRFS_EXTENT_DATA_KEY);
483431d11b83SFilipe Manana if (ret)
483531d11b83SFilipe Manana goto out;
48360e56315cSFilipe Manana dropped_extents = true;
483731d11b83SFilipe Manana }
48383d08c52bSOmar Sandoval truncate_offset = btrfs_file_extent_end(path);
483931d11b83SFilipe Manana if (ins_nr == 0)
484031d11b83SFilipe Manana start_slot = slot;
484131d11b83SFilipe Manana ins_nr++;
484231d11b83SFilipe Manana path->slots[0]++;
484331d11b83SFilipe Manana if (!dst_path) {
484431d11b83SFilipe Manana dst_path = btrfs_alloc_path();
484531d11b83SFilipe Manana if (!dst_path) {
484631d11b83SFilipe Manana ret = -ENOMEM;
484731d11b83SFilipe Manana goto out;
484831d11b83SFilipe Manana }
484931d11b83SFilipe Manana }
485031d11b83SFilipe Manana }
48510bc2d3c0SFilipe Manana if (ins_nr > 0)
48520e56315cSFilipe Manana ret = copy_items(trans, inode, dst_path, path,
485331d11b83SFilipe Manana start_slot, ins_nr, 1, 0);
485431d11b83SFilipe Manana out:
485531d11b83SFilipe Manana btrfs_release_path(path);
485631d11b83SFilipe Manana btrfs_free_path(dst_path);
485731d11b83SFilipe Manana return ret;
485831d11b83SFilipe Manana }
485931d11b83SFilipe Manana
btrfs_log_changed_extents(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_log_ctx * ctx)48605dc562c5SJosef Bacik static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
48619d122629SNikolay Borisov struct btrfs_inode *inode,
4862827463c4SMiao Xie struct btrfs_path *path,
486348778179SFilipe Manana struct btrfs_log_ctx *ctx)
48645dc562c5SJosef Bacik {
486548778179SFilipe Manana struct btrfs_ordered_extent *ordered;
486648778179SFilipe Manana struct btrfs_ordered_extent *tmp;
48675dc562c5SJosef Bacik struct extent_map *em, *n;
486884af994bSRuan Jinjie LIST_HEAD(extents);
48699d122629SNikolay Borisov struct extent_map_tree *tree = &inode->extent_tree;
48705dc562c5SJosef Bacik int ret = 0;
48712ab28f32SJosef Bacik int num = 0;
48725dc562c5SJosef Bacik
48735dc562c5SJosef Bacik write_lock(&tree->lock);
48745dc562c5SJosef Bacik
48755dc562c5SJosef Bacik list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
48765dc562c5SJosef Bacik list_del_init(&em->list);
48772ab28f32SJosef Bacik /*
48782ab28f32SJosef Bacik * Just an arbitrary number, this can be really CPU intensive
48792ab28f32SJosef Bacik * once we start getting a lot of extents, and really once we
48802ab28f32SJosef Bacik * have a bunch of extents we just want to commit since it will
48812ab28f32SJosef Bacik * be faster.
48822ab28f32SJosef Bacik */
48832ab28f32SJosef Bacik if (++num > 32768) {
48842ab28f32SJosef Bacik list_del_init(&tree->modified_extents);
48852ab28f32SJosef Bacik ret = -EFBIG;
48862ab28f32SJosef Bacik goto process;
48872ab28f32SJosef Bacik }
48882ab28f32SJosef Bacik
48895f96bfb7SFilipe Manana if (em->generation < trans->transid)
48905dc562c5SJosef Bacik continue;
48918c6c5928SJosef Bacik
489231d11b83SFilipe Manana /* We log prealloc extents beyond eof later. */
489331d11b83SFilipe Manana if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
489431d11b83SFilipe Manana em->start >= i_size_read(&inode->vfs_inode))
489531d11b83SFilipe Manana continue;
489631d11b83SFilipe Manana
4897ff44c6e3SJosef Bacik /* Need a ref to keep it from getting evicted from cache */
4898490b54d6SElena Reshetova refcount_inc(&em->refs);
4899ff44c6e3SJosef Bacik set_bit(EXTENT_FLAG_LOGGING, &em->flags);
49005dc562c5SJosef Bacik list_add_tail(&em->list, &extents);
49012ab28f32SJosef Bacik num++;
49025dc562c5SJosef Bacik }
49035dc562c5SJosef Bacik
49045dc562c5SJosef Bacik list_sort(NULL, &extents, extent_cmp);
49052ab28f32SJosef Bacik process:
49065dc562c5SJosef Bacik while (!list_empty(&extents)) {
49075dc562c5SJosef Bacik em = list_entry(extents.next, struct extent_map, list);
49085dc562c5SJosef Bacik
49095dc562c5SJosef Bacik list_del_init(&em->list);
49105dc562c5SJosef Bacik
49115dc562c5SJosef Bacik /*
49125dc562c5SJosef Bacik * If we had an error we just need to delete everybody from our
49135dc562c5SJosef Bacik * private list.
49145dc562c5SJosef Bacik */
4915ff44c6e3SJosef Bacik if (ret) {
4916201a9038SJosef Bacik clear_em_logging(tree, em);
4917ff44c6e3SJosef Bacik free_extent_map(em);
49185dc562c5SJosef Bacik continue;
4919ff44c6e3SJosef Bacik }
4920ff44c6e3SJosef Bacik
4921ff44c6e3SJosef Bacik write_unlock(&tree->lock);
49225dc562c5SJosef Bacik
492390d04510SFilipe Manana ret = log_one_extent(trans, inode, em, path, ctx);
4924ff44c6e3SJosef Bacik write_lock(&tree->lock);
4925201a9038SJosef Bacik clear_em_logging(tree, em);
4926201a9038SJosef Bacik free_extent_map(em);
49275dc562c5SJosef Bacik }
4928ff44c6e3SJosef Bacik WARN_ON(!list_empty(&extents));
4929ff44c6e3SJosef Bacik write_unlock(&tree->lock);
49305dc562c5SJosef Bacik
493131d11b83SFilipe Manana if (!ret)
493231d11b83SFilipe Manana ret = btrfs_log_prealloc_extents(trans, inode, path);
493348778179SFilipe Manana if (ret)
49345dc562c5SJosef Bacik return ret;
493548778179SFilipe Manana
493648778179SFilipe Manana /*
493748778179SFilipe Manana * We have logged all extents successfully, now make sure the commit of
493848778179SFilipe Manana * the current transaction waits for the ordered extents to complete
493948778179SFilipe Manana * before it commits and wipes out the log trees, otherwise we would
494048778179SFilipe Manana * lose data if an ordered extents completes after the transaction
494148778179SFilipe Manana * commits and a power failure happens after the transaction commit.
494248778179SFilipe Manana */
494348778179SFilipe Manana list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
494448778179SFilipe Manana list_del_init(&ordered->log_list);
494548778179SFilipe Manana set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
494648778179SFilipe Manana
494748778179SFilipe Manana if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
494848778179SFilipe Manana spin_lock_irq(&inode->ordered_tree.lock);
494948778179SFilipe Manana if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
495048778179SFilipe Manana set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
495148778179SFilipe Manana atomic_inc(&trans->transaction->pending_ordered);
495248778179SFilipe Manana }
495348778179SFilipe Manana spin_unlock_irq(&inode->ordered_tree.lock);
495448778179SFilipe Manana }
495548778179SFilipe Manana btrfs_put_ordered_extent(ordered);
495648778179SFilipe Manana }
495748778179SFilipe Manana
495848778179SFilipe Manana return 0;
49595dc562c5SJosef Bacik }
49605dc562c5SJosef Bacik
logged_inode_size(struct btrfs_root * log,struct btrfs_inode * inode,struct btrfs_path * path,u64 * size_ret)4961481b01c0SNikolay Borisov static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
49621a4bcf47SFilipe Manana struct btrfs_path *path, u64 *size_ret)
49631a4bcf47SFilipe Manana {
49641a4bcf47SFilipe Manana struct btrfs_key key;
49651a4bcf47SFilipe Manana int ret;
49661a4bcf47SFilipe Manana
4967481b01c0SNikolay Borisov key.objectid = btrfs_ino(inode);
49681a4bcf47SFilipe Manana key.type = BTRFS_INODE_ITEM_KEY;
49691a4bcf47SFilipe Manana key.offset = 0;
49701a4bcf47SFilipe Manana
49711a4bcf47SFilipe Manana ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
49721a4bcf47SFilipe Manana if (ret < 0) {
49731a4bcf47SFilipe Manana return ret;
49741a4bcf47SFilipe Manana } else if (ret > 0) {
49752f2ff0eeSFilipe Manana *size_ret = 0;
49761a4bcf47SFilipe Manana } else {
49771a4bcf47SFilipe Manana struct btrfs_inode_item *item;
49781a4bcf47SFilipe Manana
49791a4bcf47SFilipe Manana item = btrfs_item_ptr(path->nodes[0], path->slots[0],
49801a4bcf47SFilipe Manana struct btrfs_inode_item);
49811a4bcf47SFilipe Manana *size_ret = btrfs_inode_size(path->nodes[0], item);
4982bf504110SFilipe Manana /*
4983bf504110SFilipe Manana * If the in-memory inode's i_size is smaller then the inode
4984bf504110SFilipe Manana * size stored in the btree, return the inode's i_size, so
4985bf504110SFilipe Manana * that we get a correct inode size after replaying the log
4986bf504110SFilipe Manana * when before a power failure we had a shrinking truncate
4987bf504110SFilipe Manana * followed by addition of a new name (rename / new hard link).
4988bf504110SFilipe Manana * Otherwise return the inode size from the btree, to avoid
4989bf504110SFilipe Manana * data loss when replaying a log due to previously doing a
4990bf504110SFilipe Manana * write that expands the inode's size and logging a new name
4991bf504110SFilipe Manana * immediately after.
4992bf504110SFilipe Manana */
4993bf504110SFilipe Manana if (*size_ret > inode->vfs_inode.i_size)
4994bf504110SFilipe Manana *size_ret = inode->vfs_inode.i_size;
49951a4bcf47SFilipe Manana }
49961a4bcf47SFilipe Manana
49971a4bcf47SFilipe Manana btrfs_release_path(path);
49981a4bcf47SFilipe Manana return 0;
49991a4bcf47SFilipe Manana }
50001a4bcf47SFilipe Manana
500136283bf7SFilipe Manana /*
500236283bf7SFilipe Manana * At the moment we always log all xattrs. This is to figure out at log replay
500336283bf7SFilipe Manana * time which xattrs must have their deletion replayed. If a xattr is missing
500436283bf7SFilipe Manana * in the log tree and exists in the fs/subvol tree, we delete it. This is
500536283bf7SFilipe Manana * because if a xattr is deleted, the inode is fsynced and a power failure
500636283bf7SFilipe Manana * happens, causing the log to be replayed the next time the fs is mounted,
500736283bf7SFilipe Manana * we want the xattr to not exist anymore (same behaviour as other filesystems
500836283bf7SFilipe Manana * with a journal, ext3/4, xfs, f2fs, etc).
500936283bf7SFilipe Manana */
btrfs_log_all_xattrs(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path)501036283bf7SFilipe Manana static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
50111a93c36aSNikolay Borisov struct btrfs_inode *inode,
501236283bf7SFilipe Manana struct btrfs_path *path,
501336283bf7SFilipe Manana struct btrfs_path *dst_path)
501436283bf7SFilipe Manana {
501590d04510SFilipe Manana struct btrfs_root *root = inode->root;
501636283bf7SFilipe Manana int ret;
501736283bf7SFilipe Manana struct btrfs_key key;
50181a93c36aSNikolay Borisov const u64 ino = btrfs_ino(inode);
501936283bf7SFilipe Manana int ins_nr = 0;
502036283bf7SFilipe Manana int start_slot = 0;
5021f2f121abSFilipe Manana bool found_xattrs = false;
5022f2f121abSFilipe Manana
5023f2f121abSFilipe Manana if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
5024f2f121abSFilipe Manana return 0;
502536283bf7SFilipe Manana
502636283bf7SFilipe Manana key.objectid = ino;
502736283bf7SFilipe Manana key.type = BTRFS_XATTR_ITEM_KEY;
502836283bf7SFilipe Manana key.offset = 0;
502936283bf7SFilipe Manana
503036283bf7SFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
503136283bf7SFilipe Manana if (ret < 0)
503236283bf7SFilipe Manana return ret;
503336283bf7SFilipe Manana
503436283bf7SFilipe Manana while (true) {
503536283bf7SFilipe Manana int slot = path->slots[0];
503636283bf7SFilipe Manana struct extent_buffer *leaf = path->nodes[0];
503736283bf7SFilipe Manana int nritems = btrfs_header_nritems(leaf);
503836283bf7SFilipe Manana
503936283bf7SFilipe Manana if (slot >= nritems) {
504036283bf7SFilipe Manana if (ins_nr > 0) {
50411a93c36aSNikolay Borisov ret = copy_items(trans, inode, dst_path, path,
50420e56315cSFilipe Manana start_slot, ins_nr, 1, 0);
504336283bf7SFilipe Manana if (ret < 0)
504436283bf7SFilipe Manana return ret;
504536283bf7SFilipe Manana ins_nr = 0;
504636283bf7SFilipe Manana }
504736283bf7SFilipe Manana ret = btrfs_next_leaf(root, path);
504836283bf7SFilipe Manana if (ret < 0)
504936283bf7SFilipe Manana return ret;
505036283bf7SFilipe Manana else if (ret > 0)
505136283bf7SFilipe Manana break;
505236283bf7SFilipe Manana continue;
505336283bf7SFilipe Manana }
505436283bf7SFilipe Manana
505536283bf7SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot);
505636283bf7SFilipe Manana if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
505736283bf7SFilipe Manana break;
505836283bf7SFilipe Manana
505936283bf7SFilipe Manana if (ins_nr == 0)
506036283bf7SFilipe Manana start_slot = slot;
506136283bf7SFilipe Manana ins_nr++;
506236283bf7SFilipe Manana path->slots[0]++;
5063f2f121abSFilipe Manana found_xattrs = true;
506436283bf7SFilipe Manana cond_resched();
506536283bf7SFilipe Manana }
506636283bf7SFilipe Manana if (ins_nr > 0) {
50671a93c36aSNikolay Borisov ret = copy_items(trans, inode, dst_path, path,
50680e56315cSFilipe Manana start_slot, ins_nr, 1, 0);
506936283bf7SFilipe Manana if (ret < 0)
507036283bf7SFilipe Manana return ret;
507136283bf7SFilipe Manana }
507236283bf7SFilipe Manana
5073f2f121abSFilipe Manana if (!found_xattrs)
5074f2f121abSFilipe Manana set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
5075f2f121abSFilipe Manana
507636283bf7SFilipe Manana return 0;
507736283bf7SFilipe Manana }
507836283bf7SFilipe Manana
5079a89ca6f2SFilipe Manana /*
50800e56315cSFilipe Manana * When using the NO_HOLES feature if we punched a hole that causes the
50810e56315cSFilipe Manana * deletion of entire leafs or all the extent items of the first leaf (the one
50820e56315cSFilipe Manana * that contains the inode item and references) we may end up not processing
50830e56315cSFilipe Manana * any extents, because there are no leafs with a generation matching the
50840e56315cSFilipe Manana * current transaction that have extent items for our inode. So we need to find
50850e56315cSFilipe Manana * if any holes exist and then log them. We also need to log holes after any
50860e56315cSFilipe Manana * truncate operation that changes the inode's size.
5087a89ca6f2SFilipe Manana */
btrfs_log_holes(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path)50880e56315cSFilipe Manana static int btrfs_log_holes(struct btrfs_trans_handle *trans,
5089a0308dd7SNikolay Borisov struct btrfs_inode *inode,
50907af59743SFilipe Manana struct btrfs_path *path)
5091a89ca6f2SFilipe Manana {
509290d04510SFilipe Manana struct btrfs_root *root = inode->root;
50930b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
5094a89ca6f2SFilipe Manana struct btrfs_key key;
5095a0308dd7SNikolay Borisov const u64 ino = btrfs_ino(inode);
5096a0308dd7SNikolay Borisov const u64 i_size = i_size_read(&inode->vfs_inode);
50977af59743SFilipe Manana u64 prev_extent_end = 0;
50980e56315cSFilipe Manana int ret;
5099a89ca6f2SFilipe Manana
51000e56315cSFilipe Manana if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
5101a89ca6f2SFilipe Manana return 0;
5102a89ca6f2SFilipe Manana
5103a89ca6f2SFilipe Manana key.objectid = ino;
5104a89ca6f2SFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY;
51057af59743SFilipe Manana key.offset = 0;
5106a89ca6f2SFilipe Manana
5107a89ca6f2SFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5108a89ca6f2SFilipe Manana if (ret < 0)
5109a89ca6f2SFilipe Manana return ret;
5110a89ca6f2SFilipe Manana
51110e56315cSFilipe Manana while (true) {
51120e56315cSFilipe Manana struct extent_buffer *leaf = path->nodes[0];
5113a89ca6f2SFilipe Manana
51140e56315cSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
51150e56315cSFilipe Manana ret = btrfs_next_leaf(root, path);
51160e56315cSFilipe Manana if (ret < 0)
51170e56315cSFilipe Manana return ret;
51180e56315cSFilipe Manana if (ret > 0) {
51190e56315cSFilipe Manana ret = 0;
51200e56315cSFilipe Manana break;
51210e56315cSFilipe Manana }
51220e56315cSFilipe Manana leaf = path->nodes[0];
51230e56315cSFilipe Manana }
51240e56315cSFilipe Manana
51250e56315cSFilipe Manana btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
51260e56315cSFilipe Manana if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
51270e56315cSFilipe Manana break;
51280e56315cSFilipe Manana
51290e56315cSFilipe Manana /* We have a hole, log it. */
51300e56315cSFilipe Manana if (prev_extent_end < key.offset) {
51317af59743SFilipe Manana const u64 hole_len = key.offset - prev_extent_end;
51320e56315cSFilipe Manana
5133a89ca6f2SFilipe Manana /*
51340e56315cSFilipe Manana * Release the path to avoid deadlocks with other code
51350e56315cSFilipe Manana * paths that search the root while holding locks on
51360e56315cSFilipe Manana * leafs from the log root.
5137a89ca6f2SFilipe Manana */
51380e56315cSFilipe Manana btrfs_release_path(path);
5139d1f68ba0SOmar Sandoval ret = btrfs_insert_hole_extent(trans, root->log_root,
5140d1f68ba0SOmar Sandoval ino, prev_extent_end,
5141d1f68ba0SOmar Sandoval hole_len);
51420e56315cSFilipe Manana if (ret < 0)
51430e56315cSFilipe Manana return ret;
51440e56315cSFilipe Manana
51450e56315cSFilipe Manana /*
51460e56315cSFilipe Manana * Search for the same key again in the root. Since it's
51470e56315cSFilipe Manana * an extent item and we are holding the inode lock, the
51480e56315cSFilipe Manana * key must still exist. If it doesn't just emit warning
51490e56315cSFilipe Manana * and return an error to fall back to a transaction
51500e56315cSFilipe Manana * commit.
51510e56315cSFilipe Manana */
51520e56315cSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
51530e56315cSFilipe Manana if (ret < 0)
51540e56315cSFilipe Manana return ret;
51550e56315cSFilipe Manana if (WARN_ON(ret > 0))
51560e56315cSFilipe Manana return -ENOENT;
51570e56315cSFilipe Manana leaf = path->nodes[0];
51580e56315cSFilipe Manana }
5159a89ca6f2SFilipe Manana
51607af59743SFilipe Manana prev_extent_end = btrfs_file_extent_end(path);
51610e56315cSFilipe Manana path->slots[0]++;
51620e56315cSFilipe Manana cond_resched();
51630e56315cSFilipe Manana }
51640e56315cSFilipe Manana
51657af59743SFilipe Manana if (prev_extent_end < i_size) {
51660e56315cSFilipe Manana u64 hole_len;
51670e56315cSFilipe Manana
5168a89ca6f2SFilipe Manana btrfs_release_path(path);
51697af59743SFilipe Manana hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
5170d1f68ba0SOmar Sandoval ret = btrfs_insert_hole_extent(trans, root->log_root, ino,
5171d1f68ba0SOmar Sandoval prev_extent_end, hole_len);
51720e56315cSFilipe Manana if (ret < 0)
5173a89ca6f2SFilipe Manana return ret;
5174a89ca6f2SFilipe Manana }
5175a89ca6f2SFilipe Manana
51760e56315cSFilipe Manana return 0;
51770e56315cSFilipe Manana }
51780e56315cSFilipe Manana
517956f23fdbSFilipe Manana /*
518056f23fdbSFilipe Manana * When we are logging a new inode X, check if it doesn't have a reference that
518156f23fdbSFilipe Manana * matches the reference from some other inode Y created in a past transaction
518256f23fdbSFilipe Manana * and that was renamed in the current transaction. If we don't do this, then at
518356f23fdbSFilipe Manana * log replay time we can lose inode Y (and all its files if it's a directory):
518456f23fdbSFilipe Manana *
518556f23fdbSFilipe Manana * mkdir /mnt/x
518656f23fdbSFilipe Manana * echo "hello world" > /mnt/x/foobar
518756f23fdbSFilipe Manana * sync
518856f23fdbSFilipe Manana * mv /mnt/x /mnt/y
518956f23fdbSFilipe Manana * mkdir /mnt/x # or touch /mnt/x
519056f23fdbSFilipe Manana * xfs_io -c fsync /mnt/x
519156f23fdbSFilipe Manana * <power fail>
519256f23fdbSFilipe Manana * mount fs, trigger log replay
519356f23fdbSFilipe Manana *
519456f23fdbSFilipe Manana * After the log replay procedure, we would lose the first directory and all its
519556f23fdbSFilipe Manana * files (file foobar).
519656f23fdbSFilipe Manana * For the case where inode Y is not a directory we simply end up losing it:
519756f23fdbSFilipe Manana *
519856f23fdbSFilipe Manana * echo "123" > /mnt/foo
519956f23fdbSFilipe Manana * sync
520056f23fdbSFilipe Manana * mv /mnt/foo /mnt/bar
520156f23fdbSFilipe Manana * echo "abc" > /mnt/foo
520256f23fdbSFilipe Manana * xfs_io -c fsync /mnt/foo
520356f23fdbSFilipe Manana * <power fail>
520456f23fdbSFilipe Manana *
520556f23fdbSFilipe Manana * We also need this for cases where a snapshot entry is replaced by some other
520656f23fdbSFilipe Manana * entry (file or directory) otherwise we end up with an unreplayable log due to
520756f23fdbSFilipe Manana * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
520856f23fdbSFilipe Manana * if it were a regular entry:
520956f23fdbSFilipe Manana *
521056f23fdbSFilipe Manana * mkdir /mnt/x
521156f23fdbSFilipe Manana * btrfs subvolume snapshot /mnt /mnt/x/snap
521256f23fdbSFilipe Manana * btrfs subvolume delete /mnt/x/snap
521356f23fdbSFilipe Manana * rmdir /mnt/x
521456f23fdbSFilipe Manana * mkdir /mnt/x
521556f23fdbSFilipe Manana * fsync /mnt/x or fsync some new file inside it
521656f23fdbSFilipe Manana * <power fail>
521756f23fdbSFilipe Manana *
521856f23fdbSFilipe Manana * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
521956f23fdbSFilipe Manana * the same transaction.
522056f23fdbSFilipe Manana */
btrfs_check_ref_name_override(struct extent_buffer * eb,const int slot,const struct btrfs_key * key,struct btrfs_inode * inode,u64 * other_ino,u64 * other_parent)522156f23fdbSFilipe Manana static int btrfs_check_ref_name_override(struct extent_buffer *eb,
522256f23fdbSFilipe Manana const int slot,
522356f23fdbSFilipe Manana const struct btrfs_key *key,
52244791c8f1SNikolay Borisov struct btrfs_inode *inode,
5225a3baaf0dSFilipe Manana u64 *other_ino, u64 *other_parent)
522656f23fdbSFilipe Manana {
522756f23fdbSFilipe Manana int ret;
522856f23fdbSFilipe Manana struct btrfs_path *search_path;
522956f23fdbSFilipe Manana char *name = NULL;
523056f23fdbSFilipe Manana u32 name_len = 0;
52313212fa14SJosef Bacik u32 item_size = btrfs_item_size(eb, slot);
523256f23fdbSFilipe Manana u32 cur_offset = 0;
523356f23fdbSFilipe Manana unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
523456f23fdbSFilipe Manana
523556f23fdbSFilipe Manana search_path = btrfs_alloc_path();
523656f23fdbSFilipe Manana if (!search_path)
523756f23fdbSFilipe Manana return -ENOMEM;
523856f23fdbSFilipe Manana search_path->search_commit_root = 1;
523956f23fdbSFilipe Manana search_path->skip_locking = 1;
524056f23fdbSFilipe Manana
524156f23fdbSFilipe Manana while (cur_offset < item_size) {
524256f23fdbSFilipe Manana u64 parent;
524356f23fdbSFilipe Manana u32 this_name_len;
524456f23fdbSFilipe Manana u32 this_len;
524556f23fdbSFilipe Manana unsigned long name_ptr;
524656f23fdbSFilipe Manana struct btrfs_dir_item *di;
52476db75318SSweet Tea Dorminy struct fscrypt_str name_str;
524856f23fdbSFilipe Manana
524956f23fdbSFilipe Manana if (key->type == BTRFS_INODE_REF_KEY) {
525056f23fdbSFilipe Manana struct btrfs_inode_ref *iref;
525156f23fdbSFilipe Manana
525256f23fdbSFilipe Manana iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
525356f23fdbSFilipe Manana parent = key->offset;
525456f23fdbSFilipe Manana this_name_len = btrfs_inode_ref_name_len(eb, iref);
525556f23fdbSFilipe Manana name_ptr = (unsigned long)(iref + 1);
525656f23fdbSFilipe Manana this_len = sizeof(*iref) + this_name_len;
525756f23fdbSFilipe Manana } else {
525856f23fdbSFilipe Manana struct btrfs_inode_extref *extref;
525956f23fdbSFilipe Manana
526056f23fdbSFilipe Manana extref = (struct btrfs_inode_extref *)(ptr +
526156f23fdbSFilipe Manana cur_offset);
526256f23fdbSFilipe Manana parent = btrfs_inode_extref_parent(eb, extref);
526356f23fdbSFilipe Manana this_name_len = btrfs_inode_extref_name_len(eb, extref);
526456f23fdbSFilipe Manana name_ptr = (unsigned long)&extref->name;
526556f23fdbSFilipe Manana this_len = sizeof(*extref) + this_name_len;
526656f23fdbSFilipe Manana }
526756f23fdbSFilipe Manana
526856f23fdbSFilipe Manana if (this_name_len > name_len) {
526956f23fdbSFilipe Manana char *new_name;
527056f23fdbSFilipe Manana
527156f23fdbSFilipe Manana new_name = krealloc(name, this_name_len, GFP_NOFS);
527256f23fdbSFilipe Manana if (!new_name) {
527356f23fdbSFilipe Manana ret = -ENOMEM;
527456f23fdbSFilipe Manana goto out;
527556f23fdbSFilipe Manana }
527656f23fdbSFilipe Manana name_len = this_name_len;
527756f23fdbSFilipe Manana name = new_name;
527856f23fdbSFilipe Manana }
527956f23fdbSFilipe Manana
528056f23fdbSFilipe Manana read_extent_buffer(eb, name, name_ptr, this_name_len);
5281e43eec81SSweet Tea Dorminy
5282e43eec81SSweet Tea Dorminy name_str.name = name;
5283e43eec81SSweet Tea Dorminy name_str.len = this_name_len;
52844791c8f1SNikolay Borisov di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
5285e43eec81SSweet Tea Dorminy parent, &name_str, 0);
528656f23fdbSFilipe Manana if (di && !IS_ERR(di)) {
528744f714daSFilipe Manana struct btrfs_key di_key;
528844f714daSFilipe Manana
528944f714daSFilipe Manana btrfs_dir_item_key_to_cpu(search_path->nodes[0],
529044f714daSFilipe Manana di, &di_key);
529144f714daSFilipe Manana if (di_key.type == BTRFS_INODE_ITEM_KEY) {
52926b5fc433SFilipe Manana if (di_key.objectid != key->objectid) {
529356f23fdbSFilipe Manana ret = 1;
529444f714daSFilipe Manana *other_ino = di_key.objectid;
5295a3baaf0dSFilipe Manana *other_parent = parent;
529644f714daSFilipe Manana } else {
52976b5fc433SFilipe Manana ret = 0;
52986b5fc433SFilipe Manana }
52996b5fc433SFilipe Manana } else {
530044f714daSFilipe Manana ret = -EAGAIN;
530144f714daSFilipe Manana }
530256f23fdbSFilipe Manana goto out;
530356f23fdbSFilipe Manana } else if (IS_ERR(di)) {
530456f23fdbSFilipe Manana ret = PTR_ERR(di);
530556f23fdbSFilipe Manana goto out;
530656f23fdbSFilipe Manana }
530756f23fdbSFilipe Manana btrfs_release_path(search_path);
530856f23fdbSFilipe Manana
530956f23fdbSFilipe Manana cur_offset += this_len;
531056f23fdbSFilipe Manana }
531156f23fdbSFilipe Manana ret = 0;
531256f23fdbSFilipe Manana out:
531356f23fdbSFilipe Manana btrfs_free_path(search_path);
531456f23fdbSFilipe Manana kfree(name);
531556f23fdbSFilipe Manana return ret;
531656f23fdbSFilipe Manana }
531756f23fdbSFilipe Manana
5318a3751024SFilipe Manana /*
5319a3751024SFilipe Manana * Check if we need to log an inode. This is used in contexts where while
5320a3751024SFilipe Manana * logging an inode we need to log another inode (either that it exists or in
5321a3751024SFilipe Manana * full mode). This is used instead of btrfs_inode_in_log() because the later
5322a3751024SFilipe Manana * requires the inode to be in the log and have the log transaction committed,
5323a3751024SFilipe Manana * while here we do not care if the log transaction was already committed - our
5324a3751024SFilipe Manana * caller will commit the log later - and we want to avoid logging an inode
5325a3751024SFilipe Manana * multiple times when multiple tasks have joined the same log transaction.
5326a3751024SFilipe Manana */
need_log_inode(const struct btrfs_trans_handle * trans,struct btrfs_inode * inode)5327a3751024SFilipe Manana static bool need_log_inode(const struct btrfs_trans_handle *trans,
5328bf1f4fd3SFilipe Manana struct btrfs_inode *inode)
5329a3751024SFilipe Manana {
5330a3751024SFilipe Manana /*
5331a3751024SFilipe Manana * If a directory was not modified, no dentries added or removed, we can
5332a3751024SFilipe Manana * and should avoid logging it.
5333a3751024SFilipe Manana */
5334a3751024SFilipe Manana if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid)
5335a3751024SFilipe Manana return false;
5336a3751024SFilipe Manana
5337a3751024SFilipe Manana /*
5338a3751024SFilipe Manana * If this inode does not have new/updated/deleted xattrs since the last
5339a3751024SFilipe Manana * time it was logged and is flagged as logged in the current transaction,
5340a3751024SFilipe Manana * we can skip logging it. As for new/deleted names, those are updated in
5341a3751024SFilipe Manana * the log by link/unlink/rename operations.
5342a3751024SFilipe Manana * In case the inode was logged and then evicted and reloaded, its
5343a3751024SFilipe Manana * logged_trans will be 0, in which case we have to fully log it since
5344a3751024SFilipe Manana * logged_trans is a transient field, not persisted.
5345a3751024SFilipe Manana */
5346bf1f4fd3SFilipe Manana if (inode_logged(trans, inode, NULL) == 1 &&
5347a3751024SFilipe Manana !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
5348a3751024SFilipe Manana return false;
5349a3751024SFilipe Manana
5350a3751024SFilipe Manana return true;
5351a3751024SFilipe Manana }
5352a3751024SFilipe Manana
5353f6d86dbeSFilipe Manana struct btrfs_dir_list {
5354f6d86dbeSFilipe Manana u64 ino;
5355f6d86dbeSFilipe Manana struct list_head list;
5356f6d86dbeSFilipe Manana };
5357f6d86dbeSFilipe Manana
5358f6d86dbeSFilipe Manana /*
5359f6d86dbeSFilipe Manana * Log the inodes of the new dentries of a directory.
5360f6d86dbeSFilipe Manana * See process_dir_items_leaf() for details about why it is needed.
5361f6d86dbeSFilipe Manana * This is a recursive operation - if an existing dentry corresponds to a
5362f6d86dbeSFilipe Manana * directory, that directory's new entries are logged too (same behaviour as
5363f6d86dbeSFilipe Manana * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5364f6d86dbeSFilipe Manana * the dentries point to we do not acquire their VFS lock, otherwise lockdep
5365f6d86dbeSFilipe Manana * complains about the following circular lock dependency / possible deadlock:
5366f6d86dbeSFilipe Manana *
5367f6d86dbeSFilipe Manana * CPU0 CPU1
5368f6d86dbeSFilipe Manana * ---- ----
5369f6d86dbeSFilipe Manana * lock(&type->i_mutex_dir_key#3/2);
5370f6d86dbeSFilipe Manana * lock(sb_internal#2);
5371f6d86dbeSFilipe Manana * lock(&type->i_mutex_dir_key#3/2);
5372f6d86dbeSFilipe Manana * lock(&sb->s_type->i_mutex_key#14);
5373f6d86dbeSFilipe Manana *
5374f6d86dbeSFilipe Manana * Where sb_internal is the lock (a counter that works as a lock) acquired by
5375f6d86dbeSFilipe Manana * sb_start_intwrite() in btrfs_start_transaction().
5376f6d86dbeSFilipe Manana * Not acquiring the VFS lock of the inodes is still safe because:
5377f6d86dbeSFilipe Manana *
5378f6d86dbeSFilipe Manana * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5379f6d86dbeSFilipe Manana * that while logging the inode new references (names) are added or removed
5380f6d86dbeSFilipe Manana * from the inode, leaving the logged inode item with a link count that does
5381f6d86dbeSFilipe Manana * not match the number of logged inode reference items. This is fine because
5382f6d86dbeSFilipe Manana * at log replay time we compute the real number of links and correct the
5383f6d86dbeSFilipe Manana * link count in the inode item (see replay_one_buffer() and
5384f6d86dbeSFilipe Manana * link_to_fixup_dir());
5385f6d86dbeSFilipe Manana *
5386f6d86dbeSFilipe Manana * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5387f6d86dbeSFilipe Manana * while logging the inode's items new index items (key type
5388f6d86dbeSFilipe Manana * BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item
5389f6d86dbeSFilipe Manana * has a size that doesn't match the sum of the lengths of all the logged
5390f6d86dbeSFilipe Manana * names - this is ok, not a problem, because at log replay time we set the
5391f6d86dbeSFilipe Manana * directory's i_size to the correct value (see replay_one_name() and
53923a8d1db3SFilipe Manana * overwrite_item()).
5393f6d86dbeSFilipe Manana */
log_new_dir_dentries(struct btrfs_trans_handle * trans,struct btrfs_inode * start_inode,struct btrfs_log_ctx * ctx)5394f6d86dbeSFilipe Manana static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5395f6d86dbeSFilipe Manana struct btrfs_inode *start_inode,
5396f6d86dbeSFilipe Manana struct btrfs_log_ctx *ctx)
5397f6d86dbeSFilipe Manana {
5398f6d86dbeSFilipe Manana struct btrfs_root *root = start_inode->root;
5399f6d86dbeSFilipe Manana struct btrfs_path *path;
5400f6d86dbeSFilipe Manana LIST_HEAD(dir_list);
5401f6d86dbeSFilipe Manana struct btrfs_dir_list *dir_elem;
5402f6d86dbeSFilipe Manana u64 ino = btrfs_ino(start_inode);
5403fa4b8cb1SFilipe Manana struct btrfs_inode *curr_inode = start_inode;
5404f6d86dbeSFilipe Manana int ret = 0;
5405f6d86dbeSFilipe Manana
5406f6d86dbeSFilipe Manana /*
5407f6d86dbeSFilipe Manana * If we are logging a new name, as part of a link or rename operation,
5408f6d86dbeSFilipe Manana * don't bother logging new dentries, as we just want to log the names
5409f6d86dbeSFilipe Manana * of an inode and that any new parents exist.
5410f6d86dbeSFilipe Manana */
5411f6d86dbeSFilipe Manana if (ctx->logging_new_name)
5412f6d86dbeSFilipe Manana return 0;
5413f6d86dbeSFilipe Manana
5414f6d86dbeSFilipe Manana path = btrfs_alloc_path();
5415f6d86dbeSFilipe Manana if (!path)
5416f6d86dbeSFilipe Manana return -ENOMEM;
5417f6d86dbeSFilipe Manana
5418fa4b8cb1SFilipe Manana /* Pairs with btrfs_add_delayed_iput below. */
5419fa4b8cb1SFilipe Manana ihold(&curr_inode->vfs_inode);
5420fa4b8cb1SFilipe Manana
5421f6d86dbeSFilipe Manana while (true) {
5422fa4b8cb1SFilipe Manana struct inode *vfs_inode;
54235d3e4f1dSFilipe Manana struct btrfs_key key;
54245d3e4f1dSFilipe Manana struct btrfs_key found_key;
5425fa4b8cb1SFilipe Manana u64 next_index;
5426f6d86dbeSFilipe Manana bool continue_curr_inode = true;
54275d3e4f1dSFilipe Manana int iter_ret;
5428f6d86dbeSFilipe Manana
54295d3e4f1dSFilipe Manana key.objectid = ino;
54305d3e4f1dSFilipe Manana key.type = BTRFS_DIR_INDEX_KEY;
54315d3e4f1dSFilipe Manana key.offset = btrfs_get_first_dir_index_to_log(curr_inode);
54325d3e4f1dSFilipe Manana next_index = key.offset;
5433f6d86dbeSFilipe Manana again:
54345d3e4f1dSFilipe Manana btrfs_for_each_slot(root->log_root, &key, &found_key, path, iter_ret) {
54355d3e4f1dSFilipe Manana struct extent_buffer *leaf = path->nodes[0];
5436f6d86dbeSFilipe Manana struct btrfs_dir_item *di;
5437f6d86dbeSFilipe Manana struct btrfs_key di_key;
5438f6d86dbeSFilipe Manana struct inode *di_inode;
5439f6d86dbeSFilipe Manana int log_mode = LOG_INODE_EXISTS;
5440f6d86dbeSFilipe Manana int type;
5441f6d86dbeSFilipe Manana
54425d3e4f1dSFilipe Manana if (found_key.objectid != ino ||
54435d3e4f1dSFilipe Manana found_key.type != BTRFS_DIR_INDEX_KEY) {
5444f6d86dbeSFilipe Manana continue_curr_inode = false;
5445f6d86dbeSFilipe Manana break;
5446f6d86dbeSFilipe Manana }
5447f6d86dbeSFilipe Manana
54485d3e4f1dSFilipe Manana next_index = found_key.offset + 1;
5449fa4b8cb1SFilipe Manana
54505d3e4f1dSFilipe Manana di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
545194a48aefSOmar Sandoval type = btrfs_dir_ftype(leaf, di);
5452f6d86dbeSFilipe Manana if (btrfs_dir_transid(leaf, di) < trans->transid)
5453f6d86dbeSFilipe Manana continue;
5454f6d86dbeSFilipe Manana btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5455f6d86dbeSFilipe Manana if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5456f6d86dbeSFilipe Manana continue;
5457f6d86dbeSFilipe Manana
5458f6d86dbeSFilipe Manana btrfs_release_path(path);
5459cf0291a6SFilipe Manana di_inode = btrfs_iget_logging(di_key.objectid, root);
5460f6d86dbeSFilipe Manana if (IS_ERR(di_inode)) {
5461f6d86dbeSFilipe Manana ret = PTR_ERR(di_inode);
5462f6d86dbeSFilipe Manana goto out;
5463f6d86dbeSFilipe Manana }
5464f6d86dbeSFilipe Manana
5465f6d86dbeSFilipe Manana if (!need_log_inode(trans, BTRFS_I(di_inode))) {
5466e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(di_inode));
5467f6d86dbeSFilipe Manana break;
5468f6d86dbeSFilipe Manana }
5469f6d86dbeSFilipe Manana
5470f6d86dbeSFilipe Manana ctx->log_new_dentries = false;
5471f6d86dbeSFilipe Manana if (type == BTRFS_FT_DIR)
5472f6d86dbeSFilipe Manana log_mode = LOG_INODE_ALL;
5473f6d86dbeSFilipe Manana ret = btrfs_log_inode(trans, BTRFS_I(di_inode),
5474f6d86dbeSFilipe Manana log_mode, ctx);
5475e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(di_inode));
5476f6d86dbeSFilipe Manana if (ret)
5477f6d86dbeSFilipe Manana goto out;
5478f6d86dbeSFilipe Manana if (ctx->log_new_dentries) {
5479f6d86dbeSFilipe Manana dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5480f6d86dbeSFilipe Manana if (!dir_elem) {
5481f6d86dbeSFilipe Manana ret = -ENOMEM;
5482f6d86dbeSFilipe Manana goto out;
5483f6d86dbeSFilipe Manana }
5484f6d86dbeSFilipe Manana dir_elem->ino = di_key.objectid;
5485f6d86dbeSFilipe Manana list_add_tail(&dir_elem->list, &dir_list);
5486f6d86dbeSFilipe Manana }
5487f6d86dbeSFilipe Manana break;
5488f6d86dbeSFilipe Manana }
5489f6d86dbeSFilipe Manana
5490fa4b8cb1SFilipe Manana btrfs_release_path(path);
5491fa4b8cb1SFilipe Manana
54925d3e4f1dSFilipe Manana if (iter_ret < 0) {
54935d3e4f1dSFilipe Manana ret = iter_ret;
54945d3e4f1dSFilipe Manana goto out;
54955d3e4f1dSFilipe Manana } else if (iter_ret > 0) {
54965d3e4f1dSFilipe Manana continue_curr_inode = false;
54975d3e4f1dSFilipe Manana } else {
54985d3e4f1dSFilipe Manana key = found_key;
54995d3e4f1dSFilipe Manana }
55005d3e4f1dSFilipe Manana
55015d3e4f1dSFilipe Manana if (continue_curr_inode && key.offset < (u64)-1) {
55025d3e4f1dSFilipe Manana key.offset++;
5503f6d86dbeSFilipe Manana goto again;
5504f6d86dbeSFilipe Manana }
5505f6d86dbeSFilipe Manana
5506fa4b8cb1SFilipe Manana btrfs_set_first_dir_index_to_log(curr_inode, next_index);
5507fa4b8cb1SFilipe Manana
5508f6d86dbeSFilipe Manana if (list_empty(&dir_list))
5509f6d86dbeSFilipe Manana break;
5510f6d86dbeSFilipe Manana
5511f6d86dbeSFilipe Manana dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list);
5512f6d86dbeSFilipe Manana ino = dir_elem->ino;
5513f6d86dbeSFilipe Manana list_del(&dir_elem->list);
5514f6d86dbeSFilipe Manana kfree(dir_elem);
5515fa4b8cb1SFilipe Manana
5516fa4b8cb1SFilipe Manana btrfs_add_delayed_iput(curr_inode);
5517fa4b8cb1SFilipe Manana curr_inode = NULL;
5518fa4b8cb1SFilipe Manana
5519cf0291a6SFilipe Manana vfs_inode = btrfs_iget_logging(ino, root);
5520fa4b8cb1SFilipe Manana if (IS_ERR(vfs_inode)) {
5521fa4b8cb1SFilipe Manana ret = PTR_ERR(vfs_inode);
5522fa4b8cb1SFilipe Manana break;
5523fa4b8cb1SFilipe Manana }
5524fa4b8cb1SFilipe Manana curr_inode = BTRFS_I(vfs_inode);
5525f6d86dbeSFilipe Manana }
5526f6d86dbeSFilipe Manana out:
5527f6d86dbeSFilipe Manana btrfs_free_path(path);
5528fa4b8cb1SFilipe Manana if (curr_inode)
5529fa4b8cb1SFilipe Manana btrfs_add_delayed_iput(curr_inode);
5530fa4b8cb1SFilipe Manana
5531f6d86dbeSFilipe Manana if (ret) {
5532f6d86dbeSFilipe Manana struct btrfs_dir_list *next;
5533f6d86dbeSFilipe Manana
5534f6d86dbeSFilipe Manana list_for_each_entry_safe(dir_elem, next, &dir_list, list)
5535f6d86dbeSFilipe Manana kfree(dir_elem);
5536f6d86dbeSFilipe Manana }
5537f6d86dbeSFilipe Manana
5538f6d86dbeSFilipe Manana return ret;
5539f6d86dbeSFilipe Manana }
5540f6d86dbeSFilipe Manana
55416b5fc433SFilipe Manana struct btrfs_ino_list {
55426b5fc433SFilipe Manana u64 ino;
5543a3baaf0dSFilipe Manana u64 parent;
55446b5fc433SFilipe Manana struct list_head list;
55456b5fc433SFilipe Manana };
55466b5fc433SFilipe Manana
free_conflicting_inodes(struct btrfs_log_ctx * ctx)5547e09d94c9SFilipe Manana static void free_conflicting_inodes(struct btrfs_log_ctx *ctx)
5548e09d94c9SFilipe Manana {
5549e09d94c9SFilipe Manana struct btrfs_ino_list *curr;
5550e09d94c9SFilipe Manana struct btrfs_ino_list *next;
5551e09d94c9SFilipe Manana
5552e09d94c9SFilipe Manana list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) {
5553e09d94c9SFilipe Manana list_del(&curr->list);
5554e09d94c9SFilipe Manana kfree(curr);
5555e09d94c9SFilipe Manana }
5556e09d94c9SFilipe Manana }
5557e09d94c9SFilipe Manana
conflicting_inode_is_dir(struct btrfs_root * root,u64 ino,struct btrfs_path * path)55585557a069SFilipe Manana static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino,
55595557a069SFilipe Manana struct btrfs_path *path)
55605557a069SFilipe Manana {
55615557a069SFilipe Manana struct btrfs_key key;
55625557a069SFilipe Manana int ret;
55635557a069SFilipe Manana
55645557a069SFilipe Manana key.objectid = ino;
55655557a069SFilipe Manana key.type = BTRFS_INODE_ITEM_KEY;
55665557a069SFilipe Manana key.offset = 0;
55675557a069SFilipe Manana
55685557a069SFilipe Manana path->search_commit_root = 1;
55695557a069SFilipe Manana path->skip_locking = 1;
55705557a069SFilipe Manana
55715557a069SFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
55725557a069SFilipe Manana if (WARN_ON_ONCE(ret > 0)) {
55735557a069SFilipe Manana /*
55745557a069SFilipe Manana * We have previously found the inode through the commit root
55755557a069SFilipe Manana * so this should not happen. If it does, just error out and
55765557a069SFilipe Manana * fallback to a transaction commit.
55775557a069SFilipe Manana */
55785557a069SFilipe Manana ret = -ENOENT;
55795557a069SFilipe Manana } else if (ret == 0) {
55805557a069SFilipe Manana struct btrfs_inode_item *item;
55815557a069SFilipe Manana
55825557a069SFilipe Manana item = btrfs_item_ptr(path->nodes[0], path->slots[0],
55835557a069SFilipe Manana struct btrfs_inode_item);
55845557a069SFilipe Manana if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item)))
55855557a069SFilipe Manana ret = 1;
55865557a069SFilipe Manana }
55875557a069SFilipe Manana
55885557a069SFilipe Manana btrfs_release_path(path);
55895557a069SFilipe Manana path->search_commit_root = 0;
55905557a069SFilipe Manana path->skip_locking = 0;
55915557a069SFilipe Manana
55925557a069SFilipe Manana return ret;
55935557a069SFilipe Manana }
55945557a069SFilipe Manana
add_conflicting_inode(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 ino,u64 parent,struct btrfs_log_ctx * ctx)5595e09d94c9SFilipe Manana static int add_conflicting_inode(struct btrfs_trans_handle *trans,
55966b5fc433SFilipe Manana struct btrfs_root *root,
55975557a069SFilipe Manana struct btrfs_path *path,
5598e09d94c9SFilipe Manana u64 ino, u64 parent,
5599e09d94c9SFilipe Manana struct btrfs_log_ctx *ctx)
56006b5fc433SFilipe Manana {
56016b5fc433SFilipe Manana struct btrfs_ino_list *ino_elem;
5602e09d94c9SFilipe Manana struct inode *inode;
5603e09d94c9SFilipe Manana
5604e09d94c9SFilipe Manana /*
5605e09d94c9SFilipe Manana * It's rare to have a lot of conflicting inodes, in practice it is not
5606e09d94c9SFilipe Manana * common to have more than 1 or 2. We don't want to collect too many,
5607e09d94c9SFilipe Manana * as we could end up logging too many inodes (even if only in
5608e09d94c9SFilipe Manana * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction
5609e09d94c9SFilipe Manana * commits.
5610e09d94c9SFilipe Manana */
56115cce1780SFilipe Manana if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES)
5612e09d94c9SFilipe Manana return BTRFS_LOG_FORCE_COMMIT;
5613e09d94c9SFilipe Manana
5614cf0291a6SFilipe Manana inode = btrfs_iget_logging(ino, root);
5615e09d94c9SFilipe Manana /*
5616e09d94c9SFilipe Manana * If the other inode that had a conflicting dir entry was deleted in
56175557a069SFilipe Manana * the current transaction then we either:
56185557a069SFilipe Manana *
56195557a069SFilipe Manana * 1) Log the parent directory (later after adding it to the list) if
56205557a069SFilipe Manana * the inode is a directory. This is because it may be a deleted
56215557a069SFilipe Manana * subvolume/snapshot or it may be a regular directory that had
56225557a069SFilipe Manana * deleted subvolumes/snapshots (or subdirectories that had them),
56235557a069SFilipe Manana * and at the moment we can't deal with dropping subvolumes/snapshots
56245557a069SFilipe Manana * during log replay. So we just log the parent, which will result in
56255557a069SFilipe Manana * a fallback to a transaction commit if we are dealing with those
56265557a069SFilipe Manana * cases (last_unlink_trans will match the current transaction);
56275557a069SFilipe Manana *
56285557a069SFilipe Manana * 2) Do nothing if it's not a directory. During log replay we simply
56295557a069SFilipe Manana * unlink the conflicting dentry from the parent directory and then
56305557a069SFilipe Manana * add the dentry for our inode. Like this we can avoid logging the
56315557a069SFilipe Manana * parent directory (and maybe fallback to a transaction commit in
56325557a069SFilipe Manana * case it has a last_unlink_trans == trans->transid, due to moving
56335557a069SFilipe Manana * some inode from it to some other directory).
5634e09d94c9SFilipe Manana */
5635e09d94c9SFilipe Manana if (IS_ERR(inode)) {
5636e09d94c9SFilipe Manana int ret = PTR_ERR(inode);
5637e09d94c9SFilipe Manana
5638e09d94c9SFilipe Manana if (ret != -ENOENT)
5639e09d94c9SFilipe Manana return ret;
56406b5fc433SFilipe Manana
56415557a069SFilipe Manana ret = conflicting_inode_is_dir(root, ino, path);
56425557a069SFilipe Manana /* Not a directory or we got an error. */
56435557a069SFilipe Manana if (ret <= 0)
56445557a069SFilipe Manana return ret;
56455557a069SFilipe Manana
56465557a069SFilipe Manana /* Conflicting inode is a directory, so we'll log its parent. */
56476b5fc433SFilipe Manana ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
56486b5fc433SFilipe Manana if (!ino_elem)
56496b5fc433SFilipe Manana return -ENOMEM;
56506b5fc433SFilipe Manana ino_elem->ino = ino;
5651a3baaf0dSFilipe Manana ino_elem->parent = parent;
5652e09d94c9SFilipe Manana list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
5653e09d94c9SFilipe Manana ctx->num_conflict_inodes++;
56546b5fc433SFilipe Manana
5655e09d94c9SFilipe Manana return 0;
5656e09d94c9SFilipe Manana }
56576b5fc433SFilipe Manana
56586b5fc433SFilipe Manana /*
5659e09d94c9SFilipe Manana * If the inode was already logged skip it - otherwise we can hit an
5660e09d94c9SFilipe Manana * infinite loop. Example:
5661b5e4ff9dSFilipe Manana *
5662e09d94c9SFilipe Manana * From the commit root (previous transaction) we have the following
5663e09d94c9SFilipe Manana * inodes:
5664b5e4ff9dSFilipe Manana *
5665b5e4ff9dSFilipe Manana * inode 257 a directory
5666b5e4ff9dSFilipe Manana * inode 258 with references "zz" and "zz_link" on inode 257
5667b5e4ff9dSFilipe Manana * inode 259 with reference "a" on inode 257
5668b5e4ff9dSFilipe Manana *
5669b5e4ff9dSFilipe Manana * And in the current (uncommitted) transaction we have:
5670b5e4ff9dSFilipe Manana *
5671b5e4ff9dSFilipe Manana * inode 257 a directory, unchanged
5672b5e4ff9dSFilipe Manana * inode 258 with references "a" and "a2" on inode 257
5673b5e4ff9dSFilipe Manana * inode 259 with reference "zz_link" on inode 257
5674b5e4ff9dSFilipe Manana * inode 261 with reference "zz" on inode 257
5675b5e4ff9dSFilipe Manana *
5676b5e4ff9dSFilipe Manana * When logging inode 261 the following infinite loop could
5677b5e4ff9dSFilipe Manana * happen if we don't skip already logged inodes:
5678b5e4ff9dSFilipe Manana *
5679b5e4ff9dSFilipe Manana * - we detect inode 258 as a conflicting inode, with inode 261
5680b5e4ff9dSFilipe Manana * on reference "zz", and log it;
5681b5e4ff9dSFilipe Manana *
5682b5e4ff9dSFilipe Manana * - we detect inode 259 as a conflicting inode, with inode 258
5683b5e4ff9dSFilipe Manana * on reference "a", and log it;
5684b5e4ff9dSFilipe Manana *
5685b5e4ff9dSFilipe Manana * - we detect inode 258 as a conflicting inode, with inode 259
5686b5e4ff9dSFilipe Manana * on reference "zz_link", and log it - again! After this we
5687b5e4ff9dSFilipe Manana * repeat the above steps forever.
5688e09d94c9SFilipe Manana *
5689e09d94c9SFilipe Manana * Here we can use need_log_inode() because we only need to log the
5690e09d94c9SFilipe Manana * inode in LOG_INODE_EXISTS mode and rename operations update the log,
5691e09d94c9SFilipe Manana * so that the log ends up with the new name and without the old name.
5692b5e4ff9dSFilipe Manana */
5693e09d94c9SFilipe Manana if (!need_log_inode(trans, BTRFS_I(inode))) {
5694e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(inode));
5695e09d94c9SFilipe Manana return 0;
5696e09d94c9SFilipe Manana }
5697e09d94c9SFilipe Manana
5698e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(inode));
5699e09d94c9SFilipe Manana
5700e09d94c9SFilipe Manana ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5701e09d94c9SFilipe Manana if (!ino_elem)
5702e09d94c9SFilipe Manana return -ENOMEM;
5703e09d94c9SFilipe Manana ino_elem->ino = ino;
5704e09d94c9SFilipe Manana ino_elem->parent = parent;
5705e09d94c9SFilipe Manana list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
5706e09d94c9SFilipe Manana ctx->num_conflict_inodes++;
5707e09d94c9SFilipe Manana
5708e09d94c9SFilipe Manana return 0;
5709e09d94c9SFilipe Manana }
5710e09d94c9SFilipe Manana
log_conflicting_inodes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_log_ctx * ctx)5711e09d94c9SFilipe Manana static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
5712e09d94c9SFilipe Manana struct btrfs_root *root,
5713e09d94c9SFilipe Manana struct btrfs_log_ctx *ctx)
5714e09d94c9SFilipe Manana {
5715e09d94c9SFilipe Manana int ret = 0;
5716e09d94c9SFilipe Manana
5717b5e4ff9dSFilipe Manana /*
5718e09d94c9SFilipe Manana * Conflicting inodes are logged by the first call to btrfs_log_inode(),
5719e09d94c9SFilipe Manana * otherwise we could have unbounded recursion of btrfs_log_inode()
5720e09d94c9SFilipe Manana * calls. This check guarantees we can have only 1 level of recursion.
5721b5e4ff9dSFilipe Manana */
5722e09d94c9SFilipe Manana if (ctx->logging_conflict_inodes)
5723e09d94c9SFilipe Manana return 0;
5724e09d94c9SFilipe Manana
5725e09d94c9SFilipe Manana ctx->logging_conflict_inodes = true;
5726e09d94c9SFilipe Manana
5727e09d94c9SFilipe Manana /*
5728e09d94c9SFilipe Manana * New conflicting inodes may be found and added to the list while we
5729e09d94c9SFilipe Manana * are logging a conflicting inode, so keep iterating while the list is
5730e09d94c9SFilipe Manana * not empty.
5731e09d94c9SFilipe Manana */
5732e09d94c9SFilipe Manana while (!list_empty(&ctx->conflict_inodes)) {
5733e09d94c9SFilipe Manana struct btrfs_ino_list *curr;
5734e09d94c9SFilipe Manana struct inode *inode;
5735e09d94c9SFilipe Manana u64 ino;
5736e09d94c9SFilipe Manana u64 parent;
5737e09d94c9SFilipe Manana
5738e09d94c9SFilipe Manana curr = list_first_entry(&ctx->conflict_inodes,
5739e09d94c9SFilipe Manana struct btrfs_ino_list, list);
5740e09d94c9SFilipe Manana ino = curr->ino;
5741e09d94c9SFilipe Manana parent = curr->parent;
5742e09d94c9SFilipe Manana list_del(&curr->list);
5743e09d94c9SFilipe Manana kfree(curr);
5744e09d94c9SFilipe Manana
5745cf0291a6SFilipe Manana inode = btrfs_iget_logging(ino, root);
5746e09d94c9SFilipe Manana /*
5747e09d94c9SFilipe Manana * If the other inode that had a conflicting dir entry was
5748e09d94c9SFilipe Manana * deleted in the current transaction, we need to log its parent
5749e09d94c9SFilipe Manana * directory. See the comment at add_conflicting_inode().
5750e09d94c9SFilipe Manana */
5751e09d94c9SFilipe Manana if (IS_ERR(inode)) {
5752e09d94c9SFilipe Manana ret = PTR_ERR(inode);
5753e09d94c9SFilipe Manana if (ret != -ENOENT)
5754e09d94c9SFilipe Manana break;
5755e09d94c9SFilipe Manana
5756cf0291a6SFilipe Manana inode = btrfs_iget_logging(parent, root);
5757e09d94c9SFilipe Manana if (IS_ERR(inode)) {
5758e09d94c9SFilipe Manana ret = PTR_ERR(inode);
5759e09d94c9SFilipe Manana break;
5760e09d94c9SFilipe Manana }
5761e09d94c9SFilipe Manana
5762e09d94c9SFilipe Manana /*
5763e09d94c9SFilipe Manana * Always log the directory, we cannot make this
5764e09d94c9SFilipe Manana * conditional on need_log_inode() because the directory
5765e09d94c9SFilipe Manana * might have been logged in LOG_INODE_EXISTS mode or
5766e09d94c9SFilipe Manana * the dir index of the conflicting inode is not in a
5767e09d94c9SFilipe Manana * dir index key range logged for the directory. So we
5768e09d94c9SFilipe Manana * must make sure the deletion is recorded.
5769e09d94c9SFilipe Manana */
5770e09d94c9SFilipe Manana ret = btrfs_log_inode(trans, BTRFS_I(inode),
5771e09d94c9SFilipe Manana LOG_INODE_ALL, ctx);
5772e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(inode));
5773e09d94c9SFilipe Manana if (ret)
5774e09d94c9SFilipe Manana break;
5775e09d94c9SFilipe Manana continue;
5776e09d94c9SFilipe Manana }
5777e09d94c9SFilipe Manana
5778e09d94c9SFilipe Manana /*
5779e09d94c9SFilipe Manana * Here we can use need_log_inode() because we only need to log
5780e09d94c9SFilipe Manana * the inode in LOG_INODE_EXISTS mode and rename operations
5781e09d94c9SFilipe Manana * update the log, so that the log ends up with the new name and
5782e09d94c9SFilipe Manana * without the old name.
5783e09d94c9SFilipe Manana *
5784e09d94c9SFilipe Manana * We did this check at add_conflicting_inode(), but here we do
5785e09d94c9SFilipe Manana * it again because if some other task logged the inode after
5786e09d94c9SFilipe Manana * that, we can avoid doing it again.
5787e09d94c9SFilipe Manana */
5788e09d94c9SFilipe Manana if (!need_log_inode(trans, BTRFS_I(inode))) {
5789e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(inode));
5790b5e4ff9dSFilipe Manana continue;
5791b5e4ff9dSFilipe Manana }
5792e09d94c9SFilipe Manana
5793b5e4ff9dSFilipe Manana /*
57946b5fc433SFilipe Manana * We are safe logging the other inode without acquiring its
57956b5fc433SFilipe Manana * lock as long as we log with the LOG_INODE_EXISTS mode. We
57966b5fc433SFilipe Manana * are safe against concurrent renames of the other inode as
57976b5fc433SFilipe Manana * well because during a rename we pin the log and update the
57986b5fc433SFilipe Manana * log with the new name before we unpin it.
57996b5fc433SFilipe Manana */
5800e09d94c9SFilipe Manana ret = btrfs_log_inode(trans, BTRFS_I(inode), LOG_INODE_EXISTS, ctx);
5801e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(inode));
5802e09d94c9SFilipe Manana if (ret)
58036b5fc433SFilipe Manana break;
58046b5fc433SFilipe Manana }
58056b5fc433SFilipe Manana
5806e09d94c9SFilipe Manana ctx->logging_conflict_inodes = false;
5807e09d94c9SFilipe Manana if (ret)
5808e09d94c9SFilipe Manana free_conflicting_inodes(ctx);
58096b5fc433SFilipe Manana
58106b5fc433SFilipe Manana return ret;
58116b5fc433SFilipe Manana }
58126b5fc433SFilipe Manana
copy_inode_items_to_log(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_key * min_key,const struct btrfs_key * max_key,struct btrfs_path * path,struct btrfs_path * dst_path,const u64 logged_isize,const int inode_only,struct btrfs_log_ctx * ctx,bool * need_log_inode_item)5813da447009SFilipe Manana static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
5814da447009SFilipe Manana struct btrfs_inode *inode,
5815da447009SFilipe Manana struct btrfs_key *min_key,
5816da447009SFilipe Manana const struct btrfs_key *max_key,
5817da447009SFilipe Manana struct btrfs_path *path,
5818da447009SFilipe Manana struct btrfs_path *dst_path,
5819da447009SFilipe Manana const u64 logged_isize,
5820da447009SFilipe Manana const int inode_only,
5821da447009SFilipe Manana struct btrfs_log_ctx *ctx,
5822da447009SFilipe Manana bool *need_log_inode_item)
5823da447009SFilipe Manana {
5824d9947887SFilipe Manana const u64 i_size = i_size_read(&inode->vfs_inode);
5825da447009SFilipe Manana struct btrfs_root *root = inode->root;
5826da447009SFilipe Manana int ins_start_slot = 0;
5827da447009SFilipe Manana int ins_nr = 0;
5828da447009SFilipe Manana int ret;
5829da447009SFilipe Manana
5830da447009SFilipe Manana while (1) {
5831da447009SFilipe Manana ret = btrfs_search_forward(root, min_key, path, trans->transid);
5832da447009SFilipe Manana if (ret < 0)
5833da447009SFilipe Manana return ret;
5834da447009SFilipe Manana if (ret > 0) {
5835da447009SFilipe Manana ret = 0;
5836da447009SFilipe Manana break;
5837da447009SFilipe Manana }
5838da447009SFilipe Manana again:
5839da447009SFilipe Manana /* Note, ins_nr might be > 0 here, cleanup outside the loop */
5840da447009SFilipe Manana if (min_key->objectid != max_key->objectid)
5841da447009SFilipe Manana break;
5842da447009SFilipe Manana if (min_key->type > max_key->type)
5843da447009SFilipe Manana break;
5844da447009SFilipe Manana
5845d9947887SFilipe Manana if (min_key->type == BTRFS_INODE_ITEM_KEY) {
5846da447009SFilipe Manana *need_log_inode_item = false;
5847d9947887SFilipe Manana } else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
5848d9947887SFilipe Manana min_key->offset >= i_size) {
5849d9947887SFilipe Manana /*
5850d9947887SFilipe Manana * Extents at and beyond eof are logged with
5851d9947887SFilipe Manana * btrfs_log_prealloc_extents().
5852d9947887SFilipe Manana * Only regular files have BTRFS_EXTENT_DATA_KEY keys,
5853d9947887SFilipe Manana * and no keys greater than that, so bail out.
5854d9947887SFilipe Manana */
5855d9947887SFilipe Manana break;
5856d9947887SFilipe Manana } else if ((min_key->type == BTRFS_INODE_REF_KEY ||
5857da447009SFilipe Manana min_key->type == BTRFS_INODE_EXTREF_KEY) &&
5858e09d94c9SFilipe Manana (inode->generation == trans->transid ||
5859e09d94c9SFilipe Manana ctx->logging_conflict_inodes)) {
5860da447009SFilipe Manana u64 other_ino = 0;
5861da447009SFilipe Manana u64 other_parent = 0;
5862da447009SFilipe Manana
5863da447009SFilipe Manana ret = btrfs_check_ref_name_override(path->nodes[0],
5864da447009SFilipe Manana path->slots[0], min_key, inode,
5865da447009SFilipe Manana &other_ino, &other_parent);
5866da447009SFilipe Manana if (ret < 0) {
5867da447009SFilipe Manana return ret;
5868289cffcbSFilipe Manana } else if (ret > 0 &&
5869da447009SFilipe Manana other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5870da447009SFilipe Manana if (ins_nr > 0) {
5871da447009SFilipe Manana ins_nr++;
5872da447009SFilipe Manana } else {
5873da447009SFilipe Manana ins_nr = 1;
5874da447009SFilipe Manana ins_start_slot = path->slots[0];
5875da447009SFilipe Manana }
5876da447009SFilipe Manana ret = copy_items(trans, inode, dst_path, path,
5877da447009SFilipe Manana ins_start_slot, ins_nr,
5878da447009SFilipe Manana inode_only, logged_isize);
5879da447009SFilipe Manana if (ret < 0)
5880da447009SFilipe Manana return ret;
5881da447009SFilipe Manana ins_nr = 0;
5882da447009SFilipe Manana
5883e09d94c9SFilipe Manana btrfs_release_path(path);
58845557a069SFilipe Manana ret = add_conflicting_inode(trans, root, path,
5885e09d94c9SFilipe Manana other_ino,
5886e09d94c9SFilipe Manana other_parent, ctx);
5887da447009SFilipe Manana if (ret)
5888da447009SFilipe Manana return ret;
5889da447009SFilipe Manana goto next_key;
5890da447009SFilipe Manana }
5891d9947887SFilipe Manana } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
5892d9947887SFilipe Manana /* Skip xattrs, logged later with btrfs_log_all_xattrs() */
5893da447009SFilipe Manana if (ins_nr == 0)
5894da447009SFilipe Manana goto next_slot;
5895da447009SFilipe Manana ret = copy_items(trans, inode, dst_path, path,
5896da447009SFilipe Manana ins_start_slot,
5897da447009SFilipe Manana ins_nr, inode_only, logged_isize);
5898da447009SFilipe Manana if (ret < 0)
5899da447009SFilipe Manana return ret;
5900da447009SFilipe Manana ins_nr = 0;
5901da447009SFilipe Manana goto next_slot;
5902da447009SFilipe Manana }
5903da447009SFilipe Manana
5904da447009SFilipe Manana if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5905da447009SFilipe Manana ins_nr++;
5906da447009SFilipe Manana goto next_slot;
5907da447009SFilipe Manana } else if (!ins_nr) {
5908da447009SFilipe Manana ins_start_slot = path->slots[0];
5909da447009SFilipe Manana ins_nr = 1;
5910da447009SFilipe Manana goto next_slot;
5911da447009SFilipe Manana }
5912da447009SFilipe Manana
5913da447009SFilipe Manana ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5914da447009SFilipe Manana ins_nr, inode_only, logged_isize);
5915da447009SFilipe Manana if (ret < 0)
5916da447009SFilipe Manana return ret;
5917da447009SFilipe Manana ins_nr = 1;
5918da447009SFilipe Manana ins_start_slot = path->slots[0];
5919da447009SFilipe Manana next_slot:
5920da447009SFilipe Manana path->slots[0]++;
5921da447009SFilipe Manana if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5922da447009SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], min_key,
5923da447009SFilipe Manana path->slots[0]);
5924da447009SFilipe Manana goto again;
5925da447009SFilipe Manana }
5926da447009SFilipe Manana if (ins_nr) {
5927da447009SFilipe Manana ret = copy_items(trans, inode, dst_path, path,
5928da447009SFilipe Manana ins_start_slot, ins_nr, inode_only,
5929da447009SFilipe Manana logged_isize);
5930da447009SFilipe Manana if (ret < 0)
5931da447009SFilipe Manana return ret;
5932da447009SFilipe Manana ins_nr = 0;
5933da447009SFilipe Manana }
5934da447009SFilipe Manana btrfs_release_path(path);
5935da447009SFilipe Manana next_key:
5936da447009SFilipe Manana if (min_key->offset < (u64)-1) {
5937da447009SFilipe Manana min_key->offset++;
5938da447009SFilipe Manana } else if (min_key->type < max_key->type) {
5939da447009SFilipe Manana min_key->type++;
5940da447009SFilipe Manana min_key->offset = 0;
5941da447009SFilipe Manana } else {
5942da447009SFilipe Manana break;
5943da447009SFilipe Manana }
594496acb375SFilipe Manana
594596acb375SFilipe Manana /*
594696acb375SFilipe Manana * We may process many leaves full of items for our inode, so
594796acb375SFilipe Manana * avoid monopolizing a cpu for too long by rescheduling while
594896acb375SFilipe Manana * not holding locks on any tree.
594996acb375SFilipe Manana */
595096acb375SFilipe Manana cond_resched();
5951da447009SFilipe Manana }
5952d9947887SFilipe Manana if (ins_nr) {
5953da447009SFilipe Manana ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5954da447009SFilipe Manana ins_nr, inode_only, logged_isize);
5955d9947887SFilipe Manana if (ret)
5956d9947887SFilipe Manana return ret;
5957d9947887SFilipe Manana }
5958d9947887SFilipe Manana
5959d9947887SFilipe Manana if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
5960d9947887SFilipe Manana /*
5961d9947887SFilipe Manana * Release the path because otherwise we might attempt to double
5962d9947887SFilipe Manana * lock the same leaf with btrfs_log_prealloc_extents() below.
5963d9947887SFilipe Manana */
5964d9947887SFilipe Manana btrfs_release_path(path);
5965d9947887SFilipe Manana ret = btrfs_log_prealloc_extents(trans, inode, dst_path);
5966d9947887SFilipe Manana }
5967da447009SFilipe Manana
5968da447009SFilipe Manana return ret;
5969da447009SFilipe Manana }
5970da447009SFilipe Manana
insert_delayed_items_batch(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,const struct btrfs_item_batch * batch,const struct btrfs_delayed_item * first_item)597130b80f3cSFilipe Manana static int insert_delayed_items_batch(struct btrfs_trans_handle *trans,
597230b80f3cSFilipe Manana struct btrfs_root *log,
597330b80f3cSFilipe Manana struct btrfs_path *path,
597430b80f3cSFilipe Manana const struct btrfs_item_batch *batch,
597530b80f3cSFilipe Manana const struct btrfs_delayed_item *first_item)
597630b80f3cSFilipe Manana {
597730b80f3cSFilipe Manana const struct btrfs_delayed_item *curr = first_item;
597830b80f3cSFilipe Manana int ret;
597930b80f3cSFilipe Manana
598030b80f3cSFilipe Manana ret = btrfs_insert_empty_items(trans, log, path, batch);
598130b80f3cSFilipe Manana if (ret)
598230b80f3cSFilipe Manana return ret;
598330b80f3cSFilipe Manana
598430b80f3cSFilipe Manana for (int i = 0; i < batch->nr; i++) {
598530b80f3cSFilipe Manana char *data_ptr;
598630b80f3cSFilipe Manana
598730b80f3cSFilipe Manana data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
598830b80f3cSFilipe Manana write_extent_buffer(path->nodes[0], &curr->data,
598930b80f3cSFilipe Manana (unsigned long)data_ptr, curr->data_len);
599030b80f3cSFilipe Manana curr = list_next_entry(curr, log_list);
599130b80f3cSFilipe Manana path->slots[0]++;
599230b80f3cSFilipe Manana }
599330b80f3cSFilipe Manana
599430b80f3cSFilipe Manana btrfs_release_path(path);
599530b80f3cSFilipe Manana
599630b80f3cSFilipe Manana return 0;
599730b80f3cSFilipe Manana }
599830b80f3cSFilipe Manana
log_delayed_insertion_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_ins_list,struct btrfs_log_ctx * ctx)599930b80f3cSFilipe Manana static int log_delayed_insertion_items(struct btrfs_trans_handle *trans,
600030b80f3cSFilipe Manana struct btrfs_inode *inode,
600130b80f3cSFilipe Manana struct btrfs_path *path,
600230b80f3cSFilipe Manana const struct list_head *delayed_ins_list,
600330b80f3cSFilipe Manana struct btrfs_log_ctx *ctx)
600430b80f3cSFilipe Manana {
600530b80f3cSFilipe Manana /* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */
600630b80f3cSFilipe Manana const int max_batch_size = 195;
600730b80f3cSFilipe Manana const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info);
600830b80f3cSFilipe Manana const u64 ino = btrfs_ino(inode);
600930b80f3cSFilipe Manana struct btrfs_root *log = inode->root->log_root;
601030b80f3cSFilipe Manana struct btrfs_item_batch batch = {
601130b80f3cSFilipe Manana .nr = 0,
601230b80f3cSFilipe Manana .total_data_size = 0,
601330b80f3cSFilipe Manana };
601430b80f3cSFilipe Manana const struct btrfs_delayed_item *first = NULL;
601530b80f3cSFilipe Manana const struct btrfs_delayed_item *curr;
601630b80f3cSFilipe Manana char *ins_data;
601730b80f3cSFilipe Manana struct btrfs_key *ins_keys;
601830b80f3cSFilipe Manana u32 *ins_sizes;
601930b80f3cSFilipe Manana u64 curr_batch_size = 0;
602030b80f3cSFilipe Manana int batch_idx = 0;
602130b80f3cSFilipe Manana int ret;
602230b80f3cSFilipe Manana
602330b80f3cSFilipe Manana /* We are adding dir index items to the log tree. */
602430b80f3cSFilipe Manana lockdep_assert_held(&inode->log_mutex);
602530b80f3cSFilipe Manana
602630b80f3cSFilipe Manana /*
602730b80f3cSFilipe Manana * We collect delayed items before copying index keys from the subvolume
602830b80f3cSFilipe Manana * to the log tree. However just after we collected them, they may have
602930b80f3cSFilipe Manana * been flushed (all of them or just some of them), and therefore we
603030b80f3cSFilipe Manana * could have copied them from the subvolume tree to the log tree.
603130b80f3cSFilipe Manana * So find the first delayed item that was not yet logged (they are
603230b80f3cSFilipe Manana * sorted by index number).
603330b80f3cSFilipe Manana */
603430b80f3cSFilipe Manana list_for_each_entry(curr, delayed_ins_list, log_list) {
603530b80f3cSFilipe Manana if (curr->index > inode->last_dir_index_offset) {
603630b80f3cSFilipe Manana first = curr;
603730b80f3cSFilipe Manana break;
603830b80f3cSFilipe Manana }
603930b80f3cSFilipe Manana }
604030b80f3cSFilipe Manana
604130b80f3cSFilipe Manana /* Empty list or all delayed items were already logged. */
604230b80f3cSFilipe Manana if (!first)
604330b80f3cSFilipe Manana return 0;
604430b80f3cSFilipe Manana
604530b80f3cSFilipe Manana ins_data = kmalloc(max_batch_size * sizeof(u32) +
604630b80f3cSFilipe Manana max_batch_size * sizeof(struct btrfs_key), GFP_NOFS);
604730b80f3cSFilipe Manana if (!ins_data)
604830b80f3cSFilipe Manana return -ENOMEM;
604930b80f3cSFilipe Manana ins_sizes = (u32 *)ins_data;
605030b80f3cSFilipe Manana batch.data_sizes = ins_sizes;
605130b80f3cSFilipe Manana ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32));
605230b80f3cSFilipe Manana batch.keys = ins_keys;
605330b80f3cSFilipe Manana
605430b80f3cSFilipe Manana curr = first;
605530b80f3cSFilipe Manana while (!list_entry_is_head(curr, delayed_ins_list, log_list)) {
605630b80f3cSFilipe Manana const u32 curr_size = curr->data_len + sizeof(struct btrfs_item);
605730b80f3cSFilipe Manana
605830b80f3cSFilipe Manana if (curr_batch_size + curr_size > leaf_data_size ||
605930b80f3cSFilipe Manana batch.nr == max_batch_size) {
606030b80f3cSFilipe Manana ret = insert_delayed_items_batch(trans, log, path,
606130b80f3cSFilipe Manana &batch, first);
606230b80f3cSFilipe Manana if (ret)
606330b80f3cSFilipe Manana goto out;
606430b80f3cSFilipe Manana batch_idx = 0;
606530b80f3cSFilipe Manana batch.nr = 0;
606630b80f3cSFilipe Manana batch.total_data_size = 0;
606730b80f3cSFilipe Manana curr_batch_size = 0;
606830b80f3cSFilipe Manana first = curr;
606930b80f3cSFilipe Manana }
607030b80f3cSFilipe Manana
607130b80f3cSFilipe Manana ins_sizes[batch_idx] = curr->data_len;
607230b80f3cSFilipe Manana ins_keys[batch_idx].objectid = ino;
607330b80f3cSFilipe Manana ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY;
607430b80f3cSFilipe Manana ins_keys[batch_idx].offset = curr->index;
607530b80f3cSFilipe Manana curr_batch_size += curr_size;
607630b80f3cSFilipe Manana batch.total_data_size += curr->data_len;
607730b80f3cSFilipe Manana batch.nr++;
607830b80f3cSFilipe Manana batch_idx++;
607930b80f3cSFilipe Manana curr = list_next_entry(curr, log_list);
608030b80f3cSFilipe Manana }
608130b80f3cSFilipe Manana
608230b80f3cSFilipe Manana ASSERT(batch.nr >= 1);
608330b80f3cSFilipe Manana ret = insert_delayed_items_batch(trans, log, path, &batch, first);
608430b80f3cSFilipe Manana
608530b80f3cSFilipe Manana curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item,
608630b80f3cSFilipe Manana log_list);
608730b80f3cSFilipe Manana inode->last_dir_index_offset = curr->index;
608830b80f3cSFilipe Manana out:
608930b80f3cSFilipe Manana kfree(ins_data);
609030b80f3cSFilipe Manana
609130b80f3cSFilipe Manana return ret;
609230b80f3cSFilipe Manana }
609330b80f3cSFilipe Manana
log_delayed_deletions_full(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_del_list,struct btrfs_log_ctx * ctx)609430b80f3cSFilipe Manana static int log_delayed_deletions_full(struct btrfs_trans_handle *trans,
609530b80f3cSFilipe Manana struct btrfs_inode *inode,
609630b80f3cSFilipe Manana struct btrfs_path *path,
609730b80f3cSFilipe Manana const struct list_head *delayed_del_list,
609830b80f3cSFilipe Manana struct btrfs_log_ctx *ctx)
609930b80f3cSFilipe Manana {
610030b80f3cSFilipe Manana const u64 ino = btrfs_ino(inode);
610130b80f3cSFilipe Manana const struct btrfs_delayed_item *curr;
610230b80f3cSFilipe Manana
610330b80f3cSFilipe Manana curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
610430b80f3cSFilipe Manana log_list);
610530b80f3cSFilipe Manana
610630b80f3cSFilipe Manana while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
610730b80f3cSFilipe Manana u64 first_dir_index = curr->index;
610830b80f3cSFilipe Manana u64 last_dir_index;
610930b80f3cSFilipe Manana const struct btrfs_delayed_item *next;
611030b80f3cSFilipe Manana int ret;
611130b80f3cSFilipe Manana
611230b80f3cSFilipe Manana /*
611330b80f3cSFilipe Manana * Find a range of consecutive dir index items to delete. Like
611430b80f3cSFilipe Manana * this we log a single dir range item spanning several contiguous
611530b80f3cSFilipe Manana * dir items instead of logging one range item per dir index item.
611630b80f3cSFilipe Manana */
611730b80f3cSFilipe Manana next = list_next_entry(curr, log_list);
611830b80f3cSFilipe Manana while (!list_entry_is_head(next, delayed_del_list, log_list)) {
611930b80f3cSFilipe Manana if (next->index != curr->index + 1)
612030b80f3cSFilipe Manana break;
612130b80f3cSFilipe Manana curr = next;
612230b80f3cSFilipe Manana next = list_next_entry(next, log_list);
612330b80f3cSFilipe Manana }
612430b80f3cSFilipe Manana
612530b80f3cSFilipe Manana last_dir_index = curr->index;
612630b80f3cSFilipe Manana ASSERT(last_dir_index >= first_dir_index);
612730b80f3cSFilipe Manana
612830b80f3cSFilipe Manana ret = insert_dir_log_key(trans, inode->root->log_root, path,
612930b80f3cSFilipe Manana ino, first_dir_index, last_dir_index);
613030b80f3cSFilipe Manana if (ret)
613130b80f3cSFilipe Manana return ret;
613230b80f3cSFilipe Manana curr = list_next_entry(curr, log_list);
613330b80f3cSFilipe Manana }
613430b80f3cSFilipe Manana
613530b80f3cSFilipe Manana return 0;
613630b80f3cSFilipe Manana }
613730b80f3cSFilipe Manana
batch_delete_dir_index_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_log_ctx * ctx,const struct list_head * delayed_del_list,const struct btrfs_delayed_item * first,const struct btrfs_delayed_item ** last_ret)613830b80f3cSFilipe Manana static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans,
613930b80f3cSFilipe Manana struct btrfs_inode *inode,
614030b80f3cSFilipe Manana struct btrfs_path *path,
614130b80f3cSFilipe Manana struct btrfs_log_ctx *ctx,
614230b80f3cSFilipe Manana const struct list_head *delayed_del_list,
614330b80f3cSFilipe Manana const struct btrfs_delayed_item *first,
614430b80f3cSFilipe Manana const struct btrfs_delayed_item **last_ret)
614530b80f3cSFilipe Manana {
614630b80f3cSFilipe Manana const struct btrfs_delayed_item *next;
614730b80f3cSFilipe Manana struct extent_buffer *leaf = path->nodes[0];
614830b80f3cSFilipe Manana const int last_slot = btrfs_header_nritems(leaf) - 1;
614930b80f3cSFilipe Manana int slot = path->slots[0] + 1;
615030b80f3cSFilipe Manana const u64 ino = btrfs_ino(inode);
615130b80f3cSFilipe Manana
615230b80f3cSFilipe Manana next = list_next_entry(first, log_list);
615330b80f3cSFilipe Manana
615430b80f3cSFilipe Manana while (slot < last_slot &&
615530b80f3cSFilipe Manana !list_entry_is_head(next, delayed_del_list, log_list)) {
615630b80f3cSFilipe Manana struct btrfs_key key;
615730b80f3cSFilipe Manana
615830b80f3cSFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot);
615930b80f3cSFilipe Manana if (key.objectid != ino ||
616030b80f3cSFilipe Manana key.type != BTRFS_DIR_INDEX_KEY ||
616130b80f3cSFilipe Manana key.offset != next->index)
616230b80f3cSFilipe Manana break;
616330b80f3cSFilipe Manana
616430b80f3cSFilipe Manana slot++;
616530b80f3cSFilipe Manana *last_ret = next;
616630b80f3cSFilipe Manana next = list_next_entry(next, log_list);
616730b80f3cSFilipe Manana }
616830b80f3cSFilipe Manana
616930b80f3cSFilipe Manana return btrfs_del_items(trans, inode->root->log_root, path,
617030b80f3cSFilipe Manana path->slots[0], slot - path->slots[0]);
617130b80f3cSFilipe Manana }
617230b80f3cSFilipe Manana
log_delayed_deletions_incremental(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_del_list,struct btrfs_log_ctx * ctx)617330b80f3cSFilipe Manana static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans,
617430b80f3cSFilipe Manana struct btrfs_inode *inode,
617530b80f3cSFilipe Manana struct btrfs_path *path,
617630b80f3cSFilipe Manana const struct list_head *delayed_del_list,
617730b80f3cSFilipe Manana struct btrfs_log_ctx *ctx)
617830b80f3cSFilipe Manana {
617930b80f3cSFilipe Manana struct btrfs_root *log = inode->root->log_root;
618030b80f3cSFilipe Manana const struct btrfs_delayed_item *curr;
61818fd9f423SShida Zhang u64 last_range_start = 0;
618230b80f3cSFilipe Manana u64 last_range_end = 0;
618330b80f3cSFilipe Manana struct btrfs_key key;
618430b80f3cSFilipe Manana
618530b80f3cSFilipe Manana key.objectid = btrfs_ino(inode);
618630b80f3cSFilipe Manana key.type = BTRFS_DIR_INDEX_KEY;
618730b80f3cSFilipe Manana curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
618830b80f3cSFilipe Manana log_list);
618930b80f3cSFilipe Manana
619030b80f3cSFilipe Manana while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
619130b80f3cSFilipe Manana const struct btrfs_delayed_item *last = curr;
619230b80f3cSFilipe Manana u64 first_dir_index = curr->index;
619330b80f3cSFilipe Manana u64 last_dir_index;
619430b80f3cSFilipe Manana bool deleted_items = false;
619530b80f3cSFilipe Manana int ret;
619630b80f3cSFilipe Manana
619730b80f3cSFilipe Manana key.offset = curr->index;
619830b80f3cSFilipe Manana ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
619930b80f3cSFilipe Manana if (ret < 0) {
620030b80f3cSFilipe Manana return ret;
620130b80f3cSFilipe Manana } else if (ret == 0) {
620230b80f3cSFilipe Manana ret = batch_delete_dir_index_items(trans, inode, path, ctx,
620330b80f3cSFilipe Manana delayed_del_list, curr,
620430b80f3cSFilipe Manana &last);
620530b80f3cSFilipe Manana if (ret)
620630b80f3cSFilipe Manana return ret;
620730b80f3cSFilipe Manana deleted_items = true;
620830b80f3cSFilipe Manana }
620930b80f3cSFilipe Manana
621030b80f3cSFilipe Manana btrfs_release_path(path);
621130b80f3cSFilipe Manana
621230b80f3cSFilipe Manana /*
621330b80f3cSFilipe Manana * If we deleted items from the leaf, it means we have a range
621430b80f3cSFilipe Manana * item logging their range, so no need to add one or update an
621530b80f3cSFilipe Manana * existing one. Otherwise we have to log a dir range item.
621630b80f3cSFilipe Manana */
621730b80f3cSFilipe Manana if (deleted_items)
621830b80f3cSFilipe Manana goto next_batch;
621930b80f3cSFilipe Manana
622030b80f3cSFilipe Manana last_dir_index = last->index;
622130b80f3cSFilipe Manana ASSERT(last_dir_index >= first_dir_index);
622230b80f3cSFilipe Manana /*
622330b80f3cSFilipe Manana * If this range starts right after where the previous one ends,
622430b80f3cSFilipe Manana * then we want to reuse the previous range item and change its
622530b80f3cSFilipe Manana * end offset to the end of this range. This is just to minimize
622630b80f3cSFilipe Manana * leaf space usage, by avoiding adding a new range item.
622730b80f3cSFilipe Manana */
622830b80f3cSFilipe Manana if (last_range_end != 0 && first_dir_index == last_range_end + 1)
622930b80f3cSFilipe Manana first_dir_index = last_range_start;
623030b80f3cSFilipe Manana
623130b80f3cSFilipe Manana ret = insert_dir_log_key(trans, log, path, key.objectid,
623230b80f3cSFilipe Manana first_dir_index, last_dir_index);
623330b80f3cSFilipe Manana if (ret)
623430b80f3cSFilipe Manana return ret;
623530b80f3cSFilipe Manana
623630b80f3cSFilipe Manana last_range_start = first_dir_index;
623730b80f3cSFilipe Manana last_range_end = last_dir_index;
623830b80f3cSFilipe Manana next_batch:
623930b80f3cSFilipe Manana curr = list_next_entry(last, log_list);
624030b80f3cSFilipe Manana }
624130b80f3cSFilipe Manana
624230b80f3cSFilipe Manana return 0;
624330b80f3cSFilipe Manana }
624430b80f3cSFilipe Manana
log_delayed_deletion_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_del_list,struct btrfs_log_ctx * ctx)624530b80f3cSFilipe Manana static int log_delayed_deletion_items(struct btrfs_trans_handle *trans,
624630b80f3cSFilipe Manana struct btrfs_inode *inode,
624730b80f3cSFilipe Manana struct btrfs_path *path,
624830b80f3cSFilipe Manana const struct list_head *delayed_del_list,
624930b80f3cSFilipe Manana struct btrfs_log_ctx *ctx)
625030b80f3cSFilipe Manana {
625130b80f3cSFilipe Manana /*
625230b80f3cSFilipe Manana * We are deleting dir index items from the log tree or adding range
625330b80f3cSFilipe Manana * items to it.
625430b80f3cSFilipe Manana */
625530b80f3cSFilipe Manana lockdep_assert_held(&inode->log_mutex);
625630b80f3cSFilipe Manana
625730b80f3cSFilipe Manana if (list_empty(delayed_del_list))
625830b80f3cSFilipe Manana return 0;
625930b80f3cSFilipe Manana
626030b80f3cSFilipe Manana if (ctx->logged_before)
626130b80f3cSFilipe Manana return log_delayed_deletions_incremental(trans, inode, path,
626230b80f3cSFilipe Manana delayed_del_list, ctx);
626330b80f3cSFilipe Manana
626430b80f3cSFilipe Manana return log_delayed_deletions_full(trans, inode, path, delayed_del_list,
626530b80f3cSFilipe Manana ctx);
626630b80f3cSFilipe Manana }
626730b80f3cSFilipe Manana
626830b80f3cSFilipe Manana /*
626930b80f3cSFilipe Manana * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed
627030b80f3cSFilipe Manana * items instead of the subvolume tree.
627130b80f3cSFilipe Manana */
log_new_delayed_dentries(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,const struct list_head * delayed_ins_list,struct btrfs_log_ctx * ctx)627230b80f3cSFilipe Manana static int log_new_delayed_dentries(struct btrfs_trans_handle *trans,
627330b80f3cSFilipe Manana struct btrfs_inode *inode,
627430b80f3cSFilipe Manana const struct list_head *delayed_ins_list,
627530b80f3cSFilipe Manana struct btrfs_log_ctx *ctx)
627630b80f3cSFilipe Manana {
627730b80f3cSFilipe Manana const bool orig_log_new_dentries = ctx->log_new_dentries;
627830b80f3cSFilipe Manana struct btrfs_delayed_item *item;
627930b80f3cSFilipe Manana int ret = 0;
628030b80f3cSFilipe Manana
628130b80f3cSFilipe Manana /*
628230b80f3cSFilipe Manana * No need for the log mutex, plus to avoid potential deadlocks or
628330b80f3cSFilipe Manana * lockdep annotations due to nesting of delayed inode mutexes and log
628430b80f3cSFilipe Manana * mutexes.
628530b80f3cSFilipe Manana */
628630b80f3cSFilipe Manana lockdep_assert_not_held(&inode->log_mutex);
628730b80f3cSFilipe Manana
628830b80f3cSFilipe Manana ASSERT(!ctx->logging_new_delayed_dentries);
628930b80f3cSFilipe Manana ctx->logging_new_delayed_dentries = true;
629030b80f3cSFilipe Manana
629130b80f3cSFilipe Manana list_for_each_entry(item, delayed_ins_list, log_list) {
629230b80f3cSFilipe Manana struct btrfs_dir_item *dir_item;
629330b80f3cSFilipe Manana struct inode *di_inode;
629430b80f3cSFilipe Manana struct btrfs_key key;
629530b80f3cSFilipe Manana int log_mode = LOG_INODE_EXISTS;
629630b80f3cSFilipe Manana
629730b80f3cSFilipe Manana dir_item = (struct btrfs_dir_item *)item->data;
629830b80f3cSFilipe Manana btrfs_disk_key_to_cpu(&key, &dir_item->location);
629930b80f3cSFilipe Manana
630030b80f3cSFilipe Manana if (key.type == BTRFS_ROOT_ITEM_KEY)
630130b80f3cSFilipe Manana continue;
630230b80f3cSFilipe Manana
6303cf0291a6SFilipe Manana di_inode = btrfs_iget_logging(key.objectid, inode->root);
630430b80f3cSFilipe Manana if (IS_ERR(di_inode)) {
630530b80f3cSFilipe Manana ret = PTR_ERR(di_inode);
630630b80f3cSFilipe Manana break;
630730b80f3cSFilipe Manana }
630830b80f3cSFilipe Manana
630930b80f3cSFilipe Manana if (!need_log_inode(trans, BTRFS_I(di_inode))) {
6310e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(di_inode));
631130b80f3cSFilipe Manana continue;
631230b80f3cSFilipe Manana }
631330b80f3cSFilipe Manana
631494a48aefSOmar Sandoval if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR)
631530b80f3cSFilipe Manana log_mode = LOG_INODE_ALL;
631630b80f3cSFilipe Manana
631730b80f3cSFilipe Manana ctx->log_new_dentries = false;
631830b80f3cSFilipe Manana ret = btrfs_log_inode(trans, BTRFS_I(di_inode), log_mode, ctx);
631930b80f3cSFilipe Manana
632030b80f3cSFilipe Manana if (!ret && ctx->log_new_dentries)
632130b80f3cSFilipe Manana ret = log_new_dir_dentries(trans, BTRFS_I(di_inode), ctx);
632230b80f3cSFilipe Manana
6323e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(di_inode));
632430b80f3cSFilipe Manana
632530b80f3cSFilipe Manana if (ret)
632630b80f3cSFilipe Manana break;
632730b80f3cSFilipe Manana }
632830b80f3cSFilipe Manana
632930b80f3cSFilipe Manana ctx->log_new_dentries = orig_log_new_dentries;
633030b80f3cSFilipe Manana ctx->logging_new_delayed_dentries = false;
633130b80f3cSFilipe Manana
633230b80f3cSFilipe Manana return ret;
633330b80f3cSFilipe Manana }
633430b80f3cSFilipe Manana
6335e02119d5SChris Mason /* log a single inode in the tree log.
6336e02119d5SChris Mason * At least one parent directory for this inode must exist in the tree
6337e02119d5SChris Mason * or be logged already.
6338e02119d5SChris Mason *
6339e02119d5SChris Mason * Any items from this inode changed by the current transaction are copied
6340e02119d5SChris Mason * to the log tree. An extra reference is taken on any extents in this
6341e02119d5SChris Mason * file, allowing us to avoid a whole pile of corner cases around logging
6342e02119d5SChris Mason * blocks that have been removed from the tree.
6343e02119d5SChris Mason *
6344e02119d5SChris Mason * See LOG_INODE_ALL and related defines for a description of what inode_only
6345e02119d5SChris Mason * does.
6346e02119d5SChris Mason *
6347e02119d5SChris Mason * This handles both files and directories.
6348e02119d5SChris Mason */
btrfs_log_inode(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,int inode_only,struct btrfs_log_ctx * ctx)634912fcfd22SChris Mason static int btrfs_log_inode(struct btrfs_trans_handle *trans,
635090d04510SFilipe Manana struct btrfs_inode *inode,
635149dae1bcSFilipe Manana int inode_only,
63528407f553SFilipe Manana struct btrfs_log_ctx *ctx)
6353e02119d5SChris Mason {
6354e02119d5SChris Mason struct btrfs_path *path;
6355e02119d5SChris Mason struct btrfs_path *dst_path;
6356e02119d5SChris Mason struct btrfs_key min_key;
6357e02119d5SChris Mason struct btrfs_key max_key;
635890d04510SFilipe Manana struct btrfs_root *log = inode->root->log_root;
635965faced5SFilipe Manana int ret;
63605dc562c5SJosef Bacik bool fast_search = false;
6361a59108a7SNikolay Borisov u64 ino = btrfs_ino(inode);
6362a59108a7SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree;
63631a4bcf47SFilipe Manana u64 logged_isize = 0;
6364e4545de5SFilipe Manana bool need_log_inode_item = true;
63659a8fca62SFilipe Manana bool xattrs_logged = false;
63662ac691d8SFilipe Manana bool inode_item_dropped = true;
636730b80f3cSFilipe Manana bool full_dir_logging = false;
636830b80f3cSFilipe Manana LIST_HEAD(delayed_ins_list);
636930b80f3cSFilipe Manana LIST_HEAD(delayed_del_list);
6370e02119d5SChris Mason
6371e02119d5SChris Mason path = btrfs_alloc_path();
63725df67083STsutomu Itoh if (!path)
63735df67083STsutomu Itoh return -ENOMEM;
6374e02119d5SChris Mason dst_path = btrfs_alloc_path();
63755df67083STsutomu Itoh if (!dst_path) {
63765df67083STsutomu Itoh btrfs_free_path(path);
63775df67083STsutomu Itoh return -ENOMEM;
63785df67083STsutomu Itoh }
6379e02119d5SChris Mason
638033345d01SLi Zefan min_key.objectid = ino;
6381e02119d5SChris Mason min_key.type = BTRFS_INODE_ITEM_KEY;
6382e02119d5SChris Mason min_key.offset = 0;
6383e02119d5SChris Mason
638433345d01SLi Zefan max_key.objectid = ino;
638512fcfd22SChris Mason
638612fcfd22SChris Mason
63875dc562c5SJosef Bacik /* today the code can only do partial logging of directories */
6388a59108a7SNikolay Borisov if (S_ISDIR(inode->vfs_inode.i_mode) ||
63895269b67eSMiao Xie (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
6390a59108a7SNikolay Borisov &inode->runtime_flags) &&
6391781feef7SLiu Bo inode_only >= LOG_INODE_EXISTS))
6392e02119d5SChris Mason max_key.type = BTRFS_XATTR_ITEM_KEY;
6393e02119d5SChris Mason else
6394e02119d5SChris Mason max_key.type = (u8)-1;
6395e02119d5SChris Mason max_key.offset = (u64)-1;
6396e02119d5SChris Mason
639730b80f3cSFilipe Manana if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL)
639830b80f3cSFilipe Manana full_dir_logging = true;
639930b80f3cSFilipe Manana
64002c2c452bSFilipe Manana /*
640130b80f3cSFilipe Manana * If we are logging a directory while we are logging dentries of the
640230b80f3cSFilipe Manana * delayed items of some other inode, then we need to flush the delayed
640330b80f3cSFilipe Manana * items of this directory and not log the delayed items directly. This
640430b80f3cSFilipe Manana * is to prevent more than one level of recursion into btrfs_log_inode()
640530b80f3cSFilipe Manana * by having something like this:
640630b80f3cSFilipe Manana *
640730b80f3cSFilipe Manana * $ mkdir -p a/b/c/d/e/f/g/h/...
640830b80f3cSFilipe Manana * $ xfs_io -c "fsync" a
640930b80f3cSFilipe Manana *
641030b80f3cSFilipe Manana * Where all directories in the path did not exist before and are
641130b80f3cSFilipe Manana * created in the current transaction.
641230b80f3cSFilipe Manana * So in such a case we directly log the delayed items of the main
641330b80f3cSFilipe Manana * directory ("a") without flushing them first, while for each of its
641430b80f3cSFilipe Manana * subdirectories we flush their delayed items before logging them.
641530b80f3cSFilipe Manana * This prevents a potential unbounded recursion like this:
641630b80f3cSFilipe Manana *
641730b80f3cSFilipe Manana * btrfs_log_inode()
641830b80f3cSFilipe Manana * log_new_delayed_dentries()
641930b80f3cSFilipe Manana * btrfs_log_inode()
642030b80f3cSFilipe Manana * log_new_delayed_dentries()
642130b80f3cSFilipe Manana * btrfs_log_inode()
642230b80f3cSFilipe Manana * log_new_delayed_dentries()
642330b80f3cSFilipe Manana * (...)
642430b80f3cSFilipe Manana *
642530b80f3cSFilipe Manana * We have thresholds for the maximum number of delayed items to have in
642630b80f3cSFilipe Manana * memory, and once they are hit, the items are flushed asynchronously.
642730b80f3cSFilipe Manana * However the limit is quite high, so lets prevent deep levels of
642830b80f3cSFilipe Manana * recursion to happen by limiting the maximum depth to be 1.
64292c2c452bSFilipe Manana */
643030b80f3cSFilipe Manana if (full_dir_logging && ctx->logging_new_delayed_dentries) {
643165faced5SFilipe Manana ret = btrfs_commit_inode_delayed_items(trans, inode);
643265faced5SFilipe Manana if (ret)
6433f6df27ddSFilipe Manana goto out;
643416cdcec7SMiao Xie }
643516cdcec7SMiao Xie
6436a59108a7SNikolay Borisov mutex_lock(&inode->log_mutex);
6437e02119d5SChris Mason
64385e33a2bdSFilipe Manana /*
6439d0e64a98SFilipe Manana * For symlinks, we must always log their content, which is stored in an
6440d0e64a98SFilipe Manana * inline extent, otherwise we could end up with an empty symlink after
6441d0e64a98SFilipe Manana * log replay, which is invalid on linux (symlink(2) returns -ENOENT if
6442d0e64a98SFilipe Manana * one attempts to create an empty symlink).
6443d0e64a98SFilipe Manana * We don't need to worry about flushing delalloc, because when we create
6444d0e64a98SFilipe Manana * the inline extent when the symlink is created (we never have delalloc
6445d0e64a98SFilipe Manana * for symlinks).
6446d0e64a98SFilipe Manana */
6447d0e64a98SFilipe Manana if (S_ISLNK(inode->vfs_inode.i_mode))
6448d0e64a98SFilipe Manana inode_only = LOG_INODE_ALL;
6449d0e64a98SFilipe Manana
6450d0e64a98SFilipe Manana /*
64510f8ce498SFilipe Manana * Before logging the inode item, cache the value returned by
64520f8ce498SFilipe Manana * inode_logged(), because after that we have the need to figure out if
64530f8ce498SFilipe Manana * the inode was previously logged in this transaction.
64540f8ce498SFilipe Manana */
64550f8ce498SFilipe Manana ret = inode_logged(trans, inode, path);
645665faced5SFilipe Manana if (ret < 0)
64570f8ce498SFilipe Manana goto out_unlock;
64580f8ce498SFilipe Manana ctx->logged_before = (ret == 1);
645965faced5SFilipe Manana ret = 0;
64600f8ce498SFilipe Manana
64610f8ce498SFilipe Manana /*
646264d6b281SFilipe Manana * This is for cases where logging a directory could result in losing a
646364d6b281SFilipe Manana * a file after replaying the log. For example, if we move a file from a
646464d6b281SFilipe Manana * directory A to a directory B, then fsync directory A, we have no way
646564d6b281SFilipe Manana * to known the file was moved from A to B, so logging just A would
646664d6b281SFilipe Manana * result in losing the file after a log replay.
646764d6b281SFilipe Manana */
646830b80f3cSFilipe Manana if (full_dir_logging && inode->last_unlink_trans >= trans->transid) {
6469f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
647064d6b281SFilipe Manana goto out_unlock;
647164d6b281SFilipe Manana }
647264d6b281SFilipe Manana
647364d6b281SFilipe Manana /*
6474e02119d5SChris Mason * a brute force approach to making sure we get the most uptodate
6475e02119d5SChris Mason * copies of everything.
6476e02119d5SChris Mason */
6477a59108a7SNikolay Borisov if (S_ISDIR(inode->vfs_inode.i_mode)) {
6478ab12313aSFilipe Manana clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
64790f8ce498SFilipe Manana if (ctx->logged_before)
64800f8ce498SFilipe Manana ret = drop_inode_items(trans, log, path, inode,
648104fc7d51SFilipe Manana BTRFS_XATTR_ITEM_KEY);
6482e02119d5SChris Mason } else {
64830f8ce498SFilipe Manana if (inode_only == LOG_INODE_EXISTS && ctx->logged_before) {
64841a4bcf47SFilipe Manana /*
64851a4bcf47SFilipe Manana * Make sure the new inode item we write to the log has
64861a4bcf47SFilipe Manana * the same isize as the current one (if it exists).
64871a4bcf47SFilipe Manana * This is necessary to prevent data loss after log
64881a4bcf47SFilipe Manana * replay, and also to prevent doing a wrong expanding
64891a4bcf47SFilipe Manana * truncate - for e.g. create file, write 4K into offset
64901a4bcf47SFilipe Manana * 0, fsync, write 4K into offset 4096, add hard link,
64911a4bcf47SFilipe Manana * fsync some other file (to sync log), power fail - if
64921a4bcf47SFilipe Manana * we use the inode's current i_size, after log replay
64931a4bcf47SFilipe Manana * we get a 8Kb file, with the last 4Kb extent as a hole
64941a4bcf47SFilipe Manana * (zeroes), as if an expanding truncate happened,
64951a4bcf47SFilipe Manana * instead of getting a file of 4Kb only.
64961a4bcf47SFilipe Manana */
649765faced5SFilipe Manana ret = logged_inode_size(log, inode, path, &logged_isize);
649865faced5SFilipe Manana if (ret)
64991a4bcf47SFilipe Manana goto out_unlock;
65001a4bcf47SFilipe Manana }
6501a742994aSFilipe Manana if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
6502a59108a7SNikolay Borisov &inode->runtime_flags)) {
6503a742994aSFilipe Manana if (inode_only == LOG_INODE_EXISTS) {
65044f764e51SFilipe Manana max_key.type = BTRFS_XATTR_ITEM_KEY;
65050f8ce498SFilipe Manana if (ctx->logged_before)
65060f8ce498SFilipe Manana ret = drop_inode_items(trans, log, path,
65070f8ce498SFilipe Manana inode, max_key.type);
6508a742994aSFilipe Manana } else {
6509a742994aSFilipe Manana clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
6510a59108a7SNikolay Borisov &inode->runtime_flags);
6511e9976151SJosef Bacik clear_bit(BTRFS_INODE_COPY_EVERYTHING,
6512a59108a7SNikolay Borisov &inode->runtime_flags);
65130f8ce498SFilipe Manana if (ctx->logged_before)
65144934a815SFilipe Manana ret = truncate_inode_items(trans, log,
65154934a815SFilipe Manana inode, 0, 0);
6516a742994aSFilipe Manana }
65174f764e51SFilipe Manana } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
6518a59108a7SNikolay Borisov &inode->runtime_flags) ||
65196cfab851SJosef Bacik inode_only == LOG_INODE_EXISTS) {
65204f764e51SFilipe Manana if (inode_only == LOG_INODE_ALL)
6521a95249b3SJosef Bacik fast_search = true;
6522a95249b3SJosef Bacik max_key.type = BTRFS_XATTR_ITEM_KEY;
65230f8ce498SFilipe Manana if (ctx->logged_before)
652488e221cdSFilipe Manana ret = drop_inode_items(trans, log, path, inode,
6525a95249b3SJosef Bacik max_key.type);
65265dc562c5SJosef Bacik } else {
6527183f37faSLiu Bo if (inode_only == LOG_INODE_ALL)
65285dc562c5SJosef Bacik fast_search = true;
65292ac691d8SFilipe Manana inode_item_dropped = false;
6530a95249b3SJosef Bacik goto log_extents;
6531a95249b3SJosef Bacik }
6532a95249b3SJosef Bacik
6533e02119d5SChris Mason }
653465faced5SFilipe Manana if (ret)
65354a500fd1SYan, Zheng goto out_unlock;
6536e02119d5SChris Mason
653730b80f3cSFilipe Manana /*
653830b80f3cSFilipe Manana * If we are logging a directory in full mode, collect the delayed items
653930b80f3cSFilipe Manana * before iterating the subvolume tree, so that we don't miss any new
654030b80f3cSFilipe Manana * dir index items in case they get flushed while or right after we are
654130b80f3cSFilipe Manana * iterating the subvolume tree.
654230b80f3cSFilipe Manana */
654330b80f3cSFilipe Manana if (full_dir_logging && !ctx->logging_new_delayed_dentries)
654430b80f3cSFilipe Manana btrfs_log_get_delayed_items(inode, &delayed_ins_list,
654530b80f3cSFilipe Manana &delayed_del_list);
654630b80f3cSFilipe Manana
654765faced5SFilipe Manana ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
6548da447009SFilipe Manana path, dst_path, logged_isize,
6549e09d94c9SFilipe Manana inode_only, ctx,
65507af59743SFilipe Manana &need_log_inode_item);
655165faced5SFilipe Manana if (ret)
655244f714daSFilipe Manana goto out_unlock;
65535dc562c5SJosef Bacik
655436283bf7SFilipe Manana btrfs_release_path(path);
655536283bf7SFilipe Manana btrfs_release_path(dst_path);
655665faced5SFilipe Manana ret = btrfs_log_all_xattrs(trans, inode, path, dst_path);
655765faced5SFilipe Manana if (ret)
655836283bf7SFilipe Manana goto out_unlock;
65599a8fca62SFilipe Manana xattrs_logged = true;
6560a89ca6f2SFilipe Manana if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
6561a89ca6f2SFilipe Manana btrfs_release_path(path);
6562a89ca6f2SFilipe Manana btrfs_release_path(dst_path);
656365faced5SFilipe Manana ret = btrfs_log_holes(trans, inode, path);
656465faced5SFilipe Manana if (ret)
6565a89ca6f2SFilipe Manana goto out_unlock;
6566a89ca6f2SFilipe Manana }
6567a95249b3SJosef Bacik log_extents:
6568f3b15ccdSJosef Bacik btrfs_release_path(path);
65695dc562c5SJosef Bacik btrfs_release_path(dst_path);
6570e4545de5SFilipe Manana if (need_log_inode_item) {
657165faced5SFilipe Manana ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped);
657265faced5SFilipe Manana if (ret)
6573e4545de5SFilipe Manana goto out_unlock;
6574b590b839SFilipe Manana /*
6575b590b839SFilipe Manana * If we are doing a fast fsync and the inode was logged before
6576b590b839SFilipe Manana * in this transaction, we don't need to log the xattrs because
6577b590b839SFilipe Manana * they were logged before. If xattrs were added, changed or
6578b590b839SFilipe Manana * deleted since the last time we logged the inode, then we have
6579b590b839SFilipe Manana * already logged them because the inode had the runtime flag
6580b590b839SFilipe Manana * BTRFS_INODE_COPY_EVERYTHING set.
6581b590b839SFilipe Manana */
6582b590b839SFilipe Manana if (!xattrs_logged && inode->logged_trans < trans->transid) {
658365faced5SFilipe Manana ret = btrfs_log_all_xattrs(trans, inode, path, dst_path);
658465faced5SFilipe Manana if (ret)
6585b590b839SFilipe Manana goto out_unlock;
6586b590b839SFilipe Manana btrfs_release_path(path);
6587b590b839SFilipe Manana }
6588e4545de5SFilipe Manana }
6589f3b15ccdSJosef Bacik if (fast_search) {
659090d04510SFilipe Manana ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx);
659165faced5SFilipe Manana if (ret)
65925dc562c5SJosef Bacik goto out_unlock;
6593d006a048SJosef Bacik } else if (inode_only == LOG_INODE_ALL) {
659406d3d22bSLiu Bo struct extent_map *em, *n;
659506d3d22bSLiu Bo
659649dae1bcSFilipe Manana write_lock(&em_tree->lock);
659748778179SFilipe Manana list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
659806d3d22bSLiu Bo list_del_init(&em->list);
659949dae1bcSFilipe Manana write_unlock(&em_tree->lock);
66005dc562c5SJosef Bacik }
66015dc562c5SJosef Bacik
660230b80f3cSFilipe Manana if (full_dir_logging) {
660390d04510SFilipe Manana ret = log_directory_changes(trans, inode, path, dst_path, ctx);
660465faced5SFilipe Manana if (ret)
66054a500fd1SYan, Zheng goto out_unlock;
660630b80f3cSFilipe Manana ret = log_delayed_insertion_items(trans, inode, path,
660730b80f3cSFilipe Manana &delayed_ins_list, ctx);
660830b80f3cSFilipe Manana if (ret)
660930b80f3cSFilipe Manana goto out_unlock;
661030b80f3cSFilipe Manana ret = log_delayed_deletion_items(trans, inode, path,
661130b80f3cSFilipe Manana &delayed_del_list, ctx);
661230b80f3cSFilipe Manana if (ret)
661330b80f3cSFilipe Manana goto out_unlock;
66144a500fd1SYan, Zheng }
661549dae1bcSFilipe Manana
6616a59108a7SNikolay Borisov spin_lock(&inode->lock);
6617a59108a7SNikolay Borisov inode->logged_trans = trans->transid;
661875b463d2SFilipe Manana /*
66199acc8103SFilipe Manana * Don't update last_log_commit if we logged that an inode exists.
6620130341beSFilipe Manana * We do this for three reasons:
66219acc8103SFilipe Manana *
66229acc8103SFilipe Manana * 1) We might have had buffered writes to this inode that were
66239acc8103SFilipe Manana * flushed and had their ordered extents completed in this
66249acc8103SFilipe Manana * transaction, but we did not previously log the inode with
66259acc8103SFilipe Manana * LOG_INODE_ALL. Later the inode was evicted and after that
66269acc8103SFilipe Manana * it was loaded again and this LOG_INODE_EXISTS log operation
66279acc8103SFilipe Manana * happened. We must make sure that if an explicit fsync against
66289acc8103SFilipe Manana * the inode is performed later, it logs the new extents, an
66299acc8103SFilipe Manana * updated inode item, etc, and syncs the log. The same logic
66309acc8103SFilipe Manana * applies to direct IO writes instead of buffered writes.
66319acc8103SFilipe Manana *
66329acc8103SFilipe Manana * 2) When we log the inode with LOG_INODE_EXISTS, its inode item
66339acc8103SFilipe Manana * is logged with an i_size of 0 or whatever value was logged
66349acc8103SFilipe Manana * before. If later the i_size of the inode is increased by a
66359acc8103SFilipe Manana * truncate operation, the log is synced through an fsync of
66369acc8103SFilipe Manana * some other inode and then finally an explicit fsync against
66379acc8103SFilipe Manana * this inode is made, we must make sure this fsync logs the
66389acc8103SFilipe Manana * inode with the new i_size, the hole between old i_size and
66399acc8103SFilipe Manana * the new i_size, and syncs the log.
6640130341beSFilipe Manana *
6641130341beSFilipe Manana * 3) If we are logging that an ancestor inode exists as part of
6642130341beSFilipe Manana * logging a new name from a link or rename operation, don't update
6643130341beSFilipe Manana * its last_log_commit - otherwise if an explicit fsync is made
6644130341beSFilipe Manana * against an ancestor, the fsync considers the inode in the log
6645130341beSFilipe Manana * and doesn't sync the log, resulting in the ancestor missing after
6646130341beSFilipe Manana * a power failure unless the log was synced as part of an fsync
6647130341beSFilipe Manana * against any other unrelated inode.
664875b463d2SFilipe Manana */
66499acc8103SFilipe Manana if (inode_only != LOG_INODE_EXISTS)
6650a59108a7SNikolay Borisov inode->last_log_commit = inode->last_sub_trans;
6651a59108a7SNikolay Borisov spin_unlock(&inode->lock);
665223e3337fSFilipe Manana
665323e3337fSFilipe Manana /*
665423e3337fSFilipe Manana * Reset the last_reflink_trans so that the next fsync does not need to
665523e3337fSFilipe Manana * go through the slower path when logging extents and their checksums.
665623e3337fSFilipe Manana */
665723e3337fSFilipe Manana if (inode_only == LOG_INODE_ALL)
665823e3337fSFilipe Manana inode->last_reflink_trans = 0;
665923e3337fSFilipe Manana
66604a500fd1SYan, Zheng out_unlock:
6661a59108a7SNikolay Borisov mutex_unlock(&inode->log_mutex);
6662f6df27ddSFilipe Manana out:
6663e02119d5SChris Mason btrfs_free_path(path);
6664e02119d5SChris Mason btrfs_free_path(dst_path);
66650f8ce498SFilipe Manana
6666e09d94c9SFilipe Manana if (ret)
6667e09d94c9SFilipe Manana free_conflicting_inodes(ctx);
6668e09d94c9SFilipe Manana else
6669e09d94c9SFilipe Manana ret = log_conflicting_inodes(trans, inode->root, ctx);
66700f8ce498SFilipe Manana
667130b80f3cSFilipe Manana if (full_dir_logging && !ctx->logging_new_delayed_dentries) {
667230b80f3cSFilipe Manana if (!ret)
667330b80f3cSFilipe Manana ret = log_new_delayed_dentries(trans, inode,
667430b80f3cSFilipe Manana &delayed_ins_list, ctx);
667530b80f3cSFilipe Manana
667630b80f3cSFilipe Manana btrfs_log_put_delayed_items(inode, &delayed_ins_list,
667730b80f3cSFilipe Manana &delayed_del_list);
667830b80f3cSFilipe Manana }
667930b80f3cSFilipe Manana
668065faced5SFilipe Manana return ret;
6681e02119d5SChris Mason }
6682e02119d5SChris Mason
btrfs_log_all_parents(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_log_ctx * ctx)668318aa0922SFilipe Manana static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
6684d0a0b78dSNikolay Borisov struct btrfs_inode *inode,
668518aa0922SFilipe Manana struct btrfs_log_ctx *ctx)
668618aa0922SFilipe Manana {
668718aa0922SFilipe Manana int ret;
668818aa0922SFilipe Manana struct btrfs_path *path;
668918aa0922SFilipe Manana struct btrfs_key key;
6690d0a0b78dSNikolay Borisov struct btrfs_root *root = inode->root;
6691d0a0b78dSNikolay Borisov const u64 ino = btrfs_ino(inode);
669218aa0922SFilipe Manana
669318aa0922SFilipe Manana path = btrfs_alloc_path();
669418aa0922SFilipe Manana if (!path)
669518aa0922SFilipe Manana return -ENOMEM;
669618aa0922SFilipe Manana path->skip_locking = 1;
669718aa0922SFilipe Manana path->search_commit_root = 1;
669818aa0922SFilipe Manana
669918aa0922SFilipe Manana key.objectid = ino;
670018aa0922SFilipe Manana key.type = BTRFS_INODE_REF_KEY;
670118aa0922SFilipe Manana key.offset = 0;
670218aa0922SFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
670318aa0922SFilipe Manana if (ret < 0)
670418aa0922SFilipe Manana goto out;
670518aa0922SFilipe Manana
670618aa0922SFilipe Manana while (true) {
670718aa0922SFilipe Manana struct extent_buffer *leaf = path->nodes[0];
670818aa0922SFilipe Manana int slot = path->slots[0];
670918aa0922SFilipe Manana u32 cur_offset = 0;
671018aa0922SFilipe Manana u32 item_size;
671118aa0922SFilipe Manana unsigned long ptr;
671218aa0922SFilipe Manana
671318aa0922SFilipe Manana if (slot >= btrfs_header_nritems(leaf)) {
671418aa0922SFilipe Manana ret = btrfs_next_leaf(root, path);
671518aa0922SFilipe Manana if (ret < 0)
671618aa0922SFilipe Manana goto out;
671718aa0922SFilipe Manana else if (ret > 0)
671818aa0922SFilipe Manana break;
671918aa0922SFilipe Manana continue;
672018aa0922SFilipe Manana }
672118aa0922SFilipe Manana
672218aa0922SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot);
672318aa0922SFilipe Manana /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
672418aa0922SFilipe Manana if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
672518aa0922SFilipe Manana break;
672618aa0922SFilipe Manana
67273212fa14SJosef Bacik item_size = btrfs_item_size(leaf, slot);
672818aa0922SFilipe Manana ptr = btrfs_item_ptr_offset(leaf, slot);
672918aa0922SFilipe Manana while (cur_offset < item_size) {
673018aa0922SFilipe Manana struct btrfs_key inode_key;
673118aa0922SFilipe Manana struct inode *dir_inode;
673218aa0922SFilipe Manana
673318aa0922SFilipe Manana inode_key.type = BTRFS_INODE_ITEM_KEY;
673418aa0922SFilipe Manana inode_key.offset = 0;
673518aa0922SFilipe Manana
673618aa0922SFilipe Manana if (key.type == BTRFS_INODE_EXTREF_KEY) {
673718aa0922SFilipe Manana struct btrfs_inode_extref *extref;
673818aa0922SFilipe Manana
673918aa0922SFilipe Manana extref = (struct btrfs_inode_extref *)
674018aa0922SFilipe Manana (ptr + cur_offset);
674118aa0922SFilipe Manana inode_key.objectid = btrfs_inode_extref_parent(
674218aa0922SFilipe Manana leaf, extref);
674318aa0922SFilipe Manana cur_offset += sizeof(*extref);
674418aa0922SFilipe Manana cur_offset += btrfs_inode_extref_name_len(leaf,
674518aa0922SFilipe Manana extref);
674618aa0922SFilipe Manana } else {
674718aa0922SFilipe Manana inode_key.objectid = key.offset;
674818aa0922SFilipe Manana cur_offset = item_size;
674918aa0922SFilipe Manana }
675018aa0922SFilipe Manana
6751cf0291a6SFilipe Manana dir_inode = btrfs_iget_logging(inode_key.objectid, root);
67520f375eedSFilipe Manana /*
67530f375eedSFilipe Manana * If the parent inode was deleted, return an error to
67540f375eedSFilipe Manana * fallback to a transaction commit. This is to prevent
67550f375eedSFilipe Manana * getting an inode that was moved from one parent A to
67560f375eedSFilipe Manana * a parent B, got its former parent A deleted and then
67570f375eedSFilipe Manana * it got fsync'ed, from existing at both parents after
67580f375eedSFilipe Manana * a log replay (and the old parent still existing).
67590f375eedSFilipe Manana * Example:
67600f375eedSFilipe Manana *
67610f375eedSFilipe Manana * mkdir /mnt/A
67620f375eedSFilipe Manana * mkdir /mnt/B
67630f375eedSFilipe Manana * touch /mnt/B/bar
67640f375eedSFilipe Manana * sync
67650f375eedSFilipe Manana * mv /mnt/B/bar /mnt/A/bar
67660f375eedSFilipe Manana * mv -T /mnt/A /mnt/B
67670f375eedSFilipe Manana * fsync /mnt/B/bar
67680f375eedSFilipe Manana * <power fail>
67690f375eedSFilipe Manana *
67700f375eedSFilipe Manana * If we ignore the old parent B which got deleted,
67710f375eedSFilipe Manana * after a log replay we would have file bar linked
67720f375eedSFilipe Manana * at both parents and the old parent B would still
67730f375eedSFilipe Manana * exist.
67740f375eedSFilipe Manana */
67750f375eedSFilipe Manana if (IS_ERR(dir_inode)) {
67760f375eedSFilipe Manana ret = PTR_ERR(dir_inode);
67770f375eedSFilipe Manana goto out;
67780f375eedSFilipe Manana }
677918aa0922SFilipe Manana
67803e6a86a1SFilipe Manana if (!need_log_inode(trans, BTRFS_I(dir_inode))) {
6781e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(dir_inode));
67823e6a86a1SFilipe Manana continue;
67833e6a86a1SFilipe Manana }
67843e6a86a1SFilipe Manana
6785657ed1aaSFilipe Manana ctx->log_new_dentries = false;
678690d04510SFilipe Manana ret = btrfs_log_inode(trans, BTRFS_I(dir_inode),
678748778179SFilipe Manana LOG_INODE_ALL, ctx);
6788289cffcbSFilipe Manana if (!ret && ctx->log_new_dentries)
67898786a6d7SFilipe Manana ret = log_new_dir_dentries(trans,
679051cc0d32SNikolay Borisov BTRFS_I(dir_inode), ctx);
6791e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(dir_inode));
679218aa0922SFilipe Manana if (ret)
679318aa0922SFilipe Manana goto out;
679418aa0922SFilipe Manana }
679518aa0922SFilipe Manana path->slots[0]++;
679618aa0922SFilipe Manana }
679718aa0922SFilipe Manana ret = 0;
679818aa0922SFilipe Manana out:
679918aa0922SFilipe Manana btrfs_free_path(path);
680018aa0922SFilipe Manana return ret;
680118aa0922SFilipe Manana }
680218aa0922SFilipe Manana
log_new_ancestors(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_log_ctx * ctx)6803b8aa330dSFilipe Manana static int log_new_ancestors(struct btrfs_trans_handle *trans,
6804b8aa330dSFilipe Manana struct btrfs_root *root,
6805b8aa330dSFilipe Manana struct btrfs_path *path,
6806b8aa330dSFilipe Manana struct btrfs_log_ctx *ctx)
6807b8aa330dSFilipe Manana {
6808b8aa330dSFilipe Manana struct btrfs_key found_key;
6809b8aa330dSFilipe Manana
6810b8aa330dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
6811b8aa330dSFilipe Manana
6812b8aa330dSFilipe Manana while (true) {
6813966de47fSColin Ian King struct extent_buffer *leaf;
6814966de47fSColin Ian King int slot;
6815b8aa330dSFilipe Manana struct btrfs_key search_key;
6816b8aa330dSFilipe Manana struct inode *inode;
68170202e83fSDavid Sterba u64 ino;
6818b8aa330dSFilipe Manana int ret = 0;
6819b8aa330dSFilipe Manana
6820b8aa330dSFilipe Manana btrfs_release_path(path);
6821b8aa330dSFilipe Manana
68220202e83fSDavid Sterba ino = found_key.offset;
68230202e83fSDavid Sterba
6824b8aa330dSFilipe Manana search_key.objectid = found_key.offset;
6825b8aa330dSFilipe Manana search_key.type = BTRFS_INODE_ITEM_KEY;
6826b8aa330dSFilipe Manana search_key.offset = 0;
6827cf0291a6SFilipe Manana inode = btrfs_iget_logging(ino, root);
6828b8aa330dSFilipe Manana if (IS_ERR(inode))
6829b8aa330dSFilipe Manana return PTR_ERR(inode);
6830b8aa330dSFilipe Manana
6831ab12313aSFilipe Manana if (BTRFS_I(inode)->generation >= trans->transid &&
6832ab12313aSFilipe Manana need_log_inode(trans, BTRFS_I(inode)))
683390d04510SFilipe Manana ret = btrfs_log_inode(trans, BTRFS_I(inode),
683448778179SFilipe Manana LOG_INODE_EXISTS, ctx);
6835e55cf7caSDavid Sterba btrfs_add_delayed_iput(BTRFS_I(inode));
6836b8aa330dSFilipe Manana if (ret)
6837b8aa330dSFilipe Manana return ret;
6838b8aa330dSFilipe Manana
6839b8aa330dSFilipe Manana if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
6840b8aa330dSFilipe Manana break;
6841b8aa330dSFilipe Manana
6842b8aa330dSFilipe Manana search_key.type = BTRFS_INODE_REF_KEY;
6843b8aa330dSFilipe Manana ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6844b8aa330dSFilipe Manana if (ret < 0)
6845b8aa330dSFilipe Manana return ret;
6846b8aa330dSFilipe Manana
6847b8aa330dSFilipe Manana leaf = path->nodes[0];
6848b8aa330dSFilipe Manana slot = path->slots[0];
6849b8aa330dSFilipe Manana if (slot >= btrfs_header_nritems(leaf)) {
6850b8aa330dSFilipe Manana ret = btrfs_next_leaf(root, path);
6851b8aa330dSFilipe Manana if (ret < 0)
6852b8aa330dSFilipe Manana return ret;
6853b8aa330dSFilipe Manana else if (ret > 0)
6854b8aa330dSFilipe Manana return -ENOENT;
6855b8aa330dSFilipe Manana leaf = path->nodes[0];
6856b8aa330dSFilipe Manana slot = path->slots[0];
6857b8aa330dSFilipe Manana }
6858b8aa330dSFilipe Manana
6859b8aa330dSFilipe Manana btrfs_item_key_to_cpu(leaf, &found_key, slot);
6860b8aa330dSFilipe Manana if (found_key.objectid != search_key.objectid ||
6861b8aa330dSFilipe Manana found_key.type != BTRFS_INODE_REF_KEY)
6862b8aa330dSFilipe Manana return -ENOENT;
6863b8aa330dSFilipe Manana }
6864b8aa330dSFilipe Manana return 0;
6865b8aa330dSFilipe Manana }
6866b8aa330dSFilipe Manana
log_new_ancestors_fast(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct dentry * parent,struct btrfs_log_ctx * ctx)6867b8aa330dSFilipe Manana static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
6868b8aa330dSFilipe Manana struct btrfs_inode *inode,
6869b8aa330dSFilipe Manana struct dentry *parent,
6870b8aa330dSFilipe Manana struct btrfs_log_ctx *ctx)
6871b8aa330dSFilipe Manana {
6872b8aa330dSFilipe Manana struct btrfs_root *root = inode->root;
6873b8aa330dSFilipe Manana struct dentry *old_parent = NULL;
6874b8aa330dSFilipe Manana struct super_block *sb = inode->vfs_inode.i_sb;
6875b8aa330dSFilipe Manana int ret = 0;
6876b8aa330dSFilipe Manana
6877b8aa330dSFilipe Manana while (true) {
6878b8aa330dSFilipe Manana if (!parent || d_really_is_negative(parent) ||
6879b8aa330dSFilipe Manana sb != parent->d_sb)
6880b8aa330dSFilipe Manana break;
6881b8aa330dSFilipe Manana
6882b8aa330dSFilipe Manana inode = BTRFS_I(d_inode(parent));
6883b8aa330dSFilipe Manana if (root != inode->root)
6884b8aa330dSFilipe Manana break;
6885b8aa330dSFilipe Manana
6886ab12313aSFilipe Manana if (inode->generation >= trans->transid &&
6887ab12313aSFilipe Manana need_log_inode(trans, inode)) {
688890d04510SFilipe Manana ret = btrfs_log_inode(trans, inode,
688948778179SFilipe Manana LOG_INODE_EXISTS, ctx);
6890b8aa330dSFilipe Manana if (ret)
6891b8aa330dSFilipe Manana break;
6892b8aa330dSFilipe Manana }
6893b8aa330dSFilipe Manana if (IS_ROOT(parent))
6894b8aa330dSFilipe Manana break;
6895b8aa330dSFilipe Manana
6896b8aa330dSFilipe Manana parent = dget_parent(parent);
6897b8aa330dSFilipe Manana dput(old_parent);
6898b8aa330dSFilipe Manana old_parent = parent;
6899b8aa330dSFilipe Manana }
6900b8aa330dSFilipe Manana dput(old_parent);
6901b8aa330dSFilipe Manana
6902b8aa330dSFilipe Manana return ret;
6903b8aa330dSFilipe Manana }
6904b8aa330dSFilipe Manana
log_all_new_ancestors(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct dentry * parent,struct btrfs_log_ctx * ctx)6905b8aa330dSFilipe Manana static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
6906b8aa330dSFilipe Manana struct btrfs_inode *inode,
6907b8aa330dSFilipe Manana struct dentry *parent,
6908b8aa330dSFilipe Manana struct btrfs_log_ctx *ctx)
6909b8aa330dSFilipe Manana {
6910b8aa330dSFilipe Manana struct btrfs_root *root = inode->root;
6911b8aa330dSFilipe Manana const u64 ino = btrfs_ino(inode);
6912b8aa330dSFilipe Manana struct btrfs_path *path;
6913b8aa330dSFilipe Manana struct btrfs_key search_key;
6914b8aa330dSFilipe Manana int ret;
6915b8aa330dSFilipe Manana
6916b8aa330dSFilipe Manana /*
6917b8aa330dSFilipe Manana * For a single hard link case, go through a fast path that does not
6918b8aa330dSFilipe Manana * need to iterate the fs/subvolume tree.
6919b8aa330dSFilipe Manana */
6920b8aa330dSFilipe Manana if (inode->vfs_inode.i_nlink < 2)
6921b8aa330dSFilipe Manana return log_new_ancestors_fast(trans, inode, parent, ctx);
6922b8aa330dSFilipe Manana
6923b8aa330dSFilipe Manana path = btrfs_alloc_path();
6924b8aa330dSFilipe Manana if (!path)
6925b8aa330dSFilipe Manana return -ENOMEM;
6926b8aa330dSFilipe Manana
6927b8aa330dSFilipe Manana search_key.objectid = ino;
6928b8aa330dSFilipe Manana search_key.type = BTRFS_INODE_REF_KEY;
6929b8aa330dSFilipe Manana search_key.offset = 0;
6930b8aa330dSFilipe Manana again:
6931b8aa330dSFilipe Manana ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6932b8aa330dSFilipe Manana if (ret < 0)
6933b8aa330dSFilipe Manana goto out;
6934b8aa330dSFilipe Manana if (ret == 0)
6935b8aa330dSFilipe Manana path->slots[0]++;
6936b8aa330dSFilipe Manana
6937b8aa330dSFilipe Manana while (true) {
6938b8aa330dSFilipe Manana struct extent_buffer *leaf = path->nodes[0];
6939b8aa330dSFilipe Manana int slot = path->slots[0];
6940b8aa330dSFilipe Manana struct btrfs_key found_key;
6941b8aa330dSFilipe Manana
6942b8aa330dSFilipe Manana if (slot >= btrfs_header_nritems(leaf)) {
6943b8aa330dSFilipe Manana ret = btrfs_next_leaf(root, path);
6944b8aa330dSFilipe Manana if (ret < 0)
6945b8aa330dSFilipe Manana goto out;
6946b8aa330dSFilipe Manana else if (ret > 0)
6947b8aa330dSFilipe Manana break;
6948b8aa330dSFilipe Manana continue;
6949b8aa330dSFilipe Manana }
6950b8aa330dSFilipe Manana
6951b8aa330dSFilipe Manana btrfs_item_key_to_cpu(leaf, &found_key, slot);
6952b8aa330dSFilipe Manana if (found_key.objectid != ino ||
6953b8aa330dSFilipe Manana found_key.type > BTRFS_INODE_EXTREF_KEY)
6954b8aa330dSFilipe Manana break;
6955b8aa330dSFilipe Manana
6956b8aa330dSFilipe Manana /*
6957b8aa330dSFilipe Manana * Don't deal with extended references because they are rare
6958b8aa330dSFilipe Manana * cases and too complex to deal with (we would need to keep
6959b8aa330dSFilipe Manana * track of which subitem we are processing for each item in
6960b8aa330dSFilipe Manana * this loop, etc). So just return some error to fallback to
6961b8aa330dSFilipe Manana * a transaction commit.
6962b8aa330dSFilipe Manana */
6963b8aa330dSFilipe Manana if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
6964b8aa330dSFilipe Manana ret = -EMLINK;
6965b8aa330dSFilipe Manana goto out;
6966b8aa330dSFilipe Manana }
6967b8aa330dSFilipe Manana
6968b8aa330dSFilipe Manana /*
6969b8aa330dSFilipe Manana * Logging ancestors needs to do more searches on the fs/subvol
6970b8aa330dSFilipe Manana * tree, so it releases the path as needed to avoid deadlocks.
6971b8aa330dSFilipe Manana * Keep track of the last inode ref key and resume from that key
6972b8aa330dSFilipe Manana * after logging all new ancestors for the current hard link.
6973b8aa330dSFilipe Manana */
6974b8aa330dSFilipe Manana memcpy(&search_key, &found_key, sizeof(search_key));
6975b8aa330dSFilipe Manana
6976b8aa330dSFilipe Manana ret = log_new_ancestors(trans, root, path, ctx);
6977b8aa330dSFilipe Manana if (ret)
6978b8aa330dSFilipe Manana goto out;
6979b8aa330dSFilipe Manana btrfs_release_path(path);
6980b8aa330dSFilipe Manana goto again;
6981b8aa330dSFilipe Manana }
6982b8aa330dSFilipe Manana ret = 0;
6983b8aa330dSFilipe Manana out:
6984b8aa330dSFilipe Manana btrfs_free_path(path);
6985b8aa330dSFilipe Manana return ret;
6986b8aa330dSFilipe Manana }
6987b8aa330dSFilipe Manana
6988e02119d5SChris Mason /*
6989e02119d5SChris Mason * helper function around btrfs_log_inode to make sure newly created
6990e02119d5SChris Mason * parent directories also end up in the log. A minimal inode and backref
6991e02119d5SChris Mason * only logging is done of any parent directories that are older than
6992e02119d5SChris Mason * the last committed transaction
6993e02119d5SChris Mason */
btrfs_log_inode_parent(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct dentry * parent,int inode_only,struct btrfs_log_ctx * ctx)699448a3b636SEric Sandeen static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
699519df27a9SNikolay Borisov struct btrfs_inode *inode,
699649dae1bcSFilipe Manana struct dentry *parent,
699741a1eadaSEdmund Nadolski int inode_only,
69988b050d35SMiao Xie struct btrfs_log_ctx *ctx)
6999e02119d5SChris Mason {
7000f882274bSNikolay Borisov struct btrfs_root *root = inode->root;
70010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
700212fcfd22SChris Mason int ret = 0;
70032f2ff0eeSFilipe Manana bool log_dentries = false;
700412fcfd22SChris Mason
70050b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, NOTREELOG)) {
7006f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
70073a5e1404SSage Weil goto end_no_trans;
70083a5e1404SSage Weil }
70093a5e1404SSage Weil
7010f882274bSNikolay Borisov if (btrfs_root_refs(&root->root_item) == 0) {
7011f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
701276dda93cSYan, Zheng goto end_no_trans;
701376dda93cSYan, Zheng }
701476dda93cSYan, Zheng
7015f2d72f42SFilipe Manana /*
7016f2d72f42SFilipe Manana * Skip already logged inodes or inodes corresponding to tmpfiles
7017f2d72f42SFilipe Manana * (since logging them is pointless, a link count of 0 means they
7018f2d72f42SFilipe Manana * will never be accessible).
7019f2d72f42SFilipe Manana */
7020626e9f41SFilipe Manana if ((btrfs_inode_in_log(inode, trans->transid) &&
7021626e9f41SFilipe Manana list_empty(&ctx->ordered_extents)) ||
7022f2d72f42SFilipe Manana inode->vfs_inode.i_nlink == 0) {
7023257c62e1SChris Mason ret = BTRFS_NO_LOG_SYNC;
7024257c62e1SChris Mason goto end_no_trans;
7025257c62e1SChris Mason }
7026257c62e1SChris Mason
70278b050d35SMiao Xie ret = start_log_trans(trans, root, ctx);
70284a500fd1SYan, Zheng if (ret)
7029e87ac136SMiao Xie goto end_no_trans;
703012fcfd22SChris Mason
703190d04510SFilipe Manana ret = btrfs_log_inode(trans, inode, inode_only, ctx);
70324a500fd1SYan, Zheng if (ret)
70334a500fd1SYan, Zheng goto end_trans;
7034e02119d5SChris Mason
7035af4176b4SChris Mason /*
7036af4176b4SChris Mason * for regular files, if its inode is already on disk, we don't
7037af4176b4SChris Mason * have to worry about the parents at all. This is because
7038af4176b4SChris Mason * we can use the last_unlink_trans field to record renames
7039af4176b4SChris Mason * and other fun in this file.
7040af4176b4SChris Mason */
704119df27a9SNikolay Borisov if (S_ISREG(inode->vfs_inode.i_mode) &&
704247d3db41SFilipe Manana inode->generation < trans->transid &&
704347d3db41SFilipe Manana inode->last_unlink_trans < trans->transid) {
70444a500fd1SYan, Zheng ret = 0;
70454a500fd1SYan, Zheng goto end_trans;
70464a500fd1SYan, Zheng }
7047af4176b4SChris Mason
7048289cffcbSFilipe Manana if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries)
70492f2ff0eeSFilipe Manana log_dentries = true;
70502f2ff0eeSFilipe Manana
705118aa0922SFilipe Manana /*
705201327610SNicholas D Steeves * On unlink we must make sure all our current and old parent directory
705318aa0922SFilipe Manana * inodes are fully logged. This is to prevent leaving dangling
705418aa0922SFilipe Manana * directory index entries in directories that were our parents but are
705518aa0922SFilipe Manana * not anymore. Not doing this results in old parent directory being
705618aa0922SFilipe Manana * impossible to delete after log replay (rmdir will always fail with
705718aa0922SFilipe Manana * error -ENOTEMPTY).
705818aa0922SFilipe Manana *
705918aa0922SFilipe Manana * Example 1:
706018aa0922SFilipe Manana *
706118aa0922SFilipe Manana * mkdir testdir
706218aa0922SFilipe Manana * touch testdir/foo
706318aa0922SFilipe Manana * ln testdir/foo testdir/bar
706418aa0922SFilipe Manana * sync
706518aa0922SFilipe Manana * unlink testdir/bar
706618aa0922SFilipe Manana * xfs_io -c fsync testdir/foo
706718aa0922SFilipe Manana * <power failure>
706818aa0922SFilipe Manana * mount fs, triggers log replay
706918aa0922SFilipe Manana *
707018aa0922SFilipe Manana * If we don't log the parent directory (testdir), after log replay the
707118aa0922SFilipe Manana * directory still has an entry pointing to the file inode using the bar
707218aa0922SFilipe Manana * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
707318aa0922SFilipe Manana * the file inode has a link count of 1.
707418aa0922SFilipe Manana *
707518aa0922SFilipe Manana * Example 2:
707618aa0922SFilipe Manana *
707718aa0922SFilipe Manana * mkdir testdir
707818aa0922SFilipe Manana * touch foo
707918aa0922SFilipe Manana * ln foo testdir/foo2
708018aa0922SFilipe Manana * ln foo testdir/foo3
708118aa0922SFilipe Manana * sync
708218aa0922SFilipe Manana * unlink testdir/foo3
708318aa0922SFilipe Manana * xfs_io -c fsync foo
708418aa0922SFilipe Manana * <power failure>
708518aa0922SFilipe Manana * mount fs, triggers log replay
708618aa0922SFilipe Manana *
708718aa0922SFilipe Manana * Similar as the first example, after log replay the parent directory
708818aa0922SFilipe Manana * testdir still has an entry pointing to the inode file with name foo3
708918aa0922SFilipe Manana * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
709018aa0922SFilipe Manana * and has a link count of 2.
709118aa0922SFilipe Manana */
709247d3db41SFilipe Manana if (inode->last_unlink_trans >= trans->transid) {
7093b8aa330dSFilipe Manana ret = btrfs_log_all_parents(trans, inode, ctx);
709418aa0922SFilipe Manana if (ret)
709518aa0922SFilipe Manana goto end_trans;
709618aa0922SFilipe Manana }
709718aa0922SFilipe Manana
7098b8aa330dSFilipe Manana ret = log_all_new_ancestors(trans, inode, parent, ctx);
70994a500fd1SYan, Zheng if (ret)
71004a500fd1SYan, Zheng goto end_trans;
710112fcfd22SChris Mason
71022f2ff0eeSFilipe Manana if (log_dentries)
71038786a6d7SFilipe Manana ret = log_new_dir_dentries(trans, inode, ctx);
71042f2ff0eeSFilipe Manana else
710512fcfd22SChris Mason ret = 0;
71064a500fd1SYan, Zheng end_trans:
71074a500fd1SYan, Zheng if (ret < 0) {
710890787766SDavid Sterba btrfs_set_log_full_commit(trans);
7109f31f09f6SJosef Bacik ret = BTRFS_LOG_FORCE_COMMIT;
71104a500fd1SYan, Zheng }
71118b050d35SMiao Xie
71128b050d35SMiao Xie if (ret)
71138b050d35SMiao Xie btrfs_remove_log_ctx(root, ctx);
711412fcfd22SChris Mason btrfs_end_log_trans(root);
711512fcfd22SChris Mason end_no_trans:
711612fcfd22SChris Mason return ret;
7117e02119d5SChris Mason }
7118e02119d5SChris Mason
7119e02119d5SChris Mason /*
7120e02119d5SChris Mason * it is not safe to log dentry if the chunk root has added new
7121e02119d5SChris Mason * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
7122e02119d5SChris Mason * If this returns 1, you must commit the transaction to safely get your
7123e02119d5SChris Mason * data on disk.
7124e02119d5SChris Mason */
btrfs_log_dentry_safe(struct btrfs_trans_handle * trans,struct dentry * dentry,struct btrfs_log_ctx * ctx)7125e02119d5SChris Mason int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
7126e5b84f7aSNikolay Borisov struct dentry *dentry,
71278b050d35SMiao Xie struct btrfs_log_ctx *ctx)
7128e02119d5SChris Mason {
71296a912213SJosef Bacik struct dentry *parent = dget_parent(dentry);
71306a912213SJosef Bacik int ret;
71316a912213SJosef Bacik
7132f882274bSNikolay Borisov ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
713348778179SFilipe Manana LOG_INODE_ALL, ctx);
71346a912213SJosef Bacik dput(parent);
71356a912213SJosef Bacik
71366a912213SJosef Bacik return ret;
7137e02119d5SChris Mason }
7138e02119d5SChris Mason
7139e02119d5SChris Mason /*
7140e02119d5SChris Mason * should be called during mount to recover any replay any log trees
7141e02119d5SChris Mason * from the FS
7142e02119d5SChris Mason */
btrfs_recover_log_trees(struct btrfs_root * log_root_tree)7143e02119d5SChris Mason int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
7144e02119d5SChris Mason {
7145e02119d5SChris Mason int ret;
7146e02119d5SChris Mason struct btrfs_path *path;
7147e02119d5SChris Mason struct btrfs_trans_handle *trans;
7148e02119d5SChris Mason struct btrfs_key key;
7149e02119d5SChris Mason struct btrfs_key found_key;
7150e02119d5SChris Mason struct btrfs_root *log;
7151e02119d5SChris Mason struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
7152e02119d5SChris Mason struct walk_control wc = {
7153e02119d5SChris Mason .process_func = process_one_buffer,
7154430a6626SDavid Sterba .stage = LOG_WALK_PIN_ONLY,
7155e02119d5SChris Mason };
7156e02119d5SChris Mason
7157e02119d5SChris Mason path = btrfs_alloc_path();
7158db5b493aSTsutomu Itoh if (!path)
7159db5b493aSTsutomu Itoh return -ENOMEM;
7160db5b493aSTsutomu Itoh
7161afcdd129SJosef Bacik set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
7162e02119d5SChris Mason
71634a500fd1SYan, Zheng trans = btrfs_start_transaction(fs_info->tree_root, 0);
716479787eaaSJeff Mahoney if (IS_ERR(trans)) {
716579787eaaSJeff Mahoney ret = PTR_ERR(trans);
716679787eaaSJeff Mahoney goto error;
716779787eaaSJeff Mahoney }
7168e02119d5SChris Mason
7169e02119d5SChris Mason wc.trans = trans;
7170e02119d5SChris Mason wc.pin = 1;
7171e02119d5SChris Mason
7172db5b493aSTsutomu Itoh ret = walk_log_tree(trans, log_root_tree, &wc);
717379787eaaSJeff Mahoney if (ret) {
7174ba51e2a1SJosef Bacik btrfs_abort_transaction(trans, ret);
717579787eaaSJeff Mahoney goto error;
717679787eaaSJeff Mahoney }
7177e02119d5SChris Mason
7178e02119d5SChris Mason again:
7179e02119d5SChris Mason key.objectid = BTRFS_TREE_LOG_OBJECTID;
7180e02119d5SChris Mason key.offset = (u64)-1;
7181962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY;
7182e02119d5SChris Mason
7183e02119d5SChris Mason while (1) {
7184e02119d5SChris Mason ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
718579787eaaSJeff Mahoney
718679787eaaSJeff Mahoney if (ret < 0) {
7187ba51e2a1SJosef Bacik btrfs_abort_transaction(trans, ret);
718879787eaaSJeff Mahoney goto error;
718979787eaaSJeff Mahoney }
7190e02119d5SChris Mason if (ret > 0) {
7191e02119d5SChris Mason if (path->slots[0] == 0)
7192e02119d5SChris Mason break;
7193e02119d5SChris Mason path->slots[0]--;
7194e02119d5SChris Mason }
7195e02119d5SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key,
7196e02119d5SChris Mason path->slots[0]);
7197b3b4aa74SDavid Sterba btrfs_release_path(path);
7198e02119d5SChris Mason if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
7199e02119d5SChris Mason break;
7200e02119d5SChris Mason
720162a2c73eSJosef Bacik log = btrfs_read_tree_root(log_root_tree, &found_key);
720279787eaaSJeff Mahoney if (IS_ERR(log)) {
720379787eaaSJeff Mahoney ret = PTR_ERR(log);
7204ba51e2a1SJosef Bacik btrfs_abort_transaction(trans, ret);
720579787eaaSJeff Mahoney goto error;
720679787eaaSJeff Mahoney }
7207e02119d5SChris Mason
720856e9357aSDavid Sterba wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
720956e9357aSDavid Sterba true);
721079787eaaSJeff Mahoney if (IS_ERR(wc.replay_dest)) {
721179787eaaSJeff Mahoney ret = PTR_ERR(wc.replay_dest);
72129bc574deSJosef Bacik
72139bc574deSJosef Bacik /*
72149bc574deSJosef Bacik * We didn't find the subvol, likely because it was
72159bc574deSJosef Bacik * deleted. This is ok, simply skip this log and go to
72169bc574deSJosef Bacik * the next one.
72179bc574deSJosef Bacik *
72189bc574deSJosef Bacik * We need to exclude the root because we can't have
72199bc574deSJosef Bacik * other log replays overwriting this log as we'll read
72209bc574deSJosef Bacik * it back in a few more times. This will keep our
72219bc574deSJosef Bacik * block from being modified, and we'll just bail for
72229bc574deSJosef Bacik * each subsequent pass.
72239bc574deSJosef Bacik */
72249bc574deSJosef Bacik if (ret == -ENOENT)
72259fce5704SNikolay Borisov ret = btrfs_pin_extent_for_log_replay(trans,
72269bc574deSJosef Bacik log->node->start,
72279bc574deSJosef Bacik log->node->len);
722800246528SJosef Bacik btrfs_put_root(log);
72299bc574deSJosef Bacik
72309bc574deSJosef Bacik if (!ret)
72319bc574deSJosef Bacik goto next;
7232ba51e2a1SJosef Bacik btrfs_abort_transaction(trans, ret);
723379787eaaSJeff Mahoney goto error;
723479787eaaSJeff Mahoney }
7235e02119d5SChris Mason
723607d400a6SYan Zheng wc.replay_dest->log_root = log;
72372002ae11SJosef Bacik ret = btrfs_record_root_in_trans(trans, wc.replay_dest);
72382002ae11SJosef Bacik if (ret)
72392002ae11SJosef Bacik /* The loop needs to continue due to the root refs */
7240ba51e2a1SJosef Bacik btrfs_abort_transaction(trans, ret);
72412002ae11SJosef Bacik else
7242e02119d5SChris Mason ret = walk_log_tree(trans, log, &wc);
7243e02119d5SChris Mason
7244b50c6e25SJosef Bacik if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
7245e02119d5SChris Mason ret = fixup_inode_link_counts(trans, wc.replay_dest,
7246e02119d5SChris Mason path);
7247ba51e2a1SJosef Bacik if (ret)
7248ba51e2a1SJosef Bacik btrfs_abort_transaction(trans, ret);
7249e02119d5SChris Mason }
7250e02119d5SChris Mason
7251900c9981SLiu Bo if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
7252900c9981SLiu Bo struct btrfs_root *root = wc.replay_dest;
7253900c9981SLiu Bo
7254900c9981SLiu Bo btrfs_release_path(path);
7255900c9981SLiu Bo
7256900c9981SLiu Bo /*
7257900c9981SLiu Bo * We have just replayed everything, and the highest
7258900c9981SLiu Bo * objectid of fs roots probably has changed in case
7259900c9981SLiu Bo * some inode_item's got replayed.
7260900c9981SLiu Bo *
7261900c9981SLiu Bo * root->objectid_mutex is not acquired as log replay
7262900c9981SLiu Bo * could only happen during mount.
7263900c9981SLiu Bo */
7264453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root);
7265ba51e2a1SJosef Bacik if (ret)
7266ba51e2a1SJosef Bacik btrfs_abort_transaction(trans, ret);
7267900c9981SLiu Bo }
7268900c9981SLiu Bo
726907d400a6SYan Zheng wc.replay_dest->log_root = NULL;
727000246528SJosef Bacik btrfs_put_root(wc.replay_dest);
727100246528SJosef Bacik btrfs_put_root(log);
7272e02119d5SChris Mason
7273b50c6e25SJosef Bacik if (ret)
7274b50c6e25SJosef Bacik goto error;
72759bc574deSJosef Bacik next:
7276e02119d5SChris Mason if (found_key.offset == 0)
7277e02119d5SChris Mason break;
72789bc574deSJosef Bacik key.offset = found_key.offset - 1;
7279e02119d5SChris Mason }
7280b3b4aa74SDavid Sterba btrfs_release_path(path);
7281e02119d5SChris Mason
7282e02119d5SChris Mason /* step one is to pin it all, step two is to replay just inodes */
7283e02119d5SChris Mason if (wc.pin) {
7284e02119d5SChris Mason wc.pin = 0;
7285e02119d5SChris Mason wc.process_func = replay_one_buffer;
7286e02119d5SChris Mason wc.stage = LOG_WALK_REPLAY_INODES;
7287e02119d5SChris Mason goto again;
7288e02119d5SChris Mason }
7289e02119d5SChris Mason /* step three is to replay everything */
7290e02119d5SChris Mason if (wc.stage < LOG_WALK_REPLAY_ALL) {
7291e02119d5SChris Mason wc.stage++;
7292e02119d5SChris Mason goto again;
7293e02119d5SChris Mason }
7294e02119d5SChris Mason
7295e02119d5SChris Mason btrfs_free_path(path);
7296e02119d5SChris Mason
7297abefa55aSJosef Bacik /* step 4: commit the transaction, which also unpins the blocks */
72983a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans);
7299abefa55aSJosef Bacik if (ret)
7300abefa55aSJosef Bacik return ret;
7301abefa55aSJosef Bacik
7302e02119d5SChris Mason log_root_tree->log_root = NULL;
7303afcdd129SJosef Bacik clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
730400246528SJosef Bacik btrfs_put_root(log_root_tree);
730579787eaaSJeff Mahoney
7306abefa55aSJosef Bacik return 0;
730779787eaaSJeff Mahoney error:
7308b50c6e25SJosef Bacik if (wc.trans)
73093a45bb20SJeff Mahoney btrfs_end_transaction(wc.trans);
73101aeb6b56SDavid Sterba clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
731179787eaaSJeff Mahoney btrfs_free_path(path);
731279787eaaSJeff Mahoney return ret;
7313e02119d5SChris Mason }
731412fcfd22SChris Mason
731512fcfd22SChris Mason /*
731612fcfd22SChris Mason * there are some corner cases where we want to force a full
731712fcfd22SChris Mason * commit instead of allowing a directory to be logged.
731812fcfd22SChris Mason *
731912fcfd22SChris Mason * They revolve around files there were unlinked from the directory, and
732012fcfd22SChris Mason * this function updates the parent directory so that a full commit is
732112fcfd22SChris Mason * properly done if it is fsync'd later after the unlinks are done.
73222be63d5cSFilipe Manana *
73232be63d5cSFilipe Manana * Must be called before the unlink operations (updates to the subvolume tree,
73242be63d5cSFilipe Manana * inodes, etc) are done.
732512fcfd22SChris Mason */
btrfs_record_unlink_dir(struct btrfs_trans_handle * trans,struct btrfs_inode * dir,struct btrfs_inode * inode,bool for_rename)732612fcfd22SChris Mason void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
73274176bdbfSNikolay Borisov struct btrfs_inode *dir, struct btrfs_inode *inode,
732859fcf388SFilipe Manana bool for_rename)
732912fcfd22SChris Mason {
733012fcfd22SChris Mason /*
7331af4176b4SChris Mason * when we're logging a file, if it hasn't been renamed
7332af4176b4SChris Mason * or unlinked, and its inode is fully committed on disk,
7333af4176b4SChris Mason * we don't have to worry about walking up the directory chain
7334af4176b4SChris Mason * to log its parents.
7335af4176b4SChris Mason *
7336af4176b4SChris Mason * So, we use the last_unlink_trans field to put this transid
7337af4176b4SChris Mason * into the file. When the file is logged we check it and
7338af4176b4SChris Mason * don't log the parents if the file is fully on disk.
7339af4176b4SChris Mason */
73404176bdbfSNikolay Borisov mutex_lock(&inode->log_mutex);
73414176bdbfSNikolay Borisov inode->last_unlink_trans = trans->transid;
73424176bdbfSNikolay Borisov mutex_unlock(&inode->log_mutex);
7343af4176b4SChris Mason
7344acfb5a4fSFilipe Manana if (!for_rename)
7345acfb5a4fSFilipe Manana return;
7346acfb5a4fSFilipe Manana
7347af4176b4SChris Mason /*
73481e75ef03SFilipe Manana * If this directory was already logged, any new names will be logged
73491e75ef03SFilipe Manana * with btrfs_log_new_name() and old names will be deleted from the log
73501e75ef03SFilipe Manana * tree with btrfs_del_dir_entries_in_log() or with
73511e75ef03SFilipe Manana * btrfs_del_inode_ref_in_log().
735212fcfd22SChris Mason */
7353d67ba263SFilipe Manana if (inode_logged(trans, dir, NULL) == 1)
735412fcfd22SChris Mason return;
735512fcfd22SChris Mason
735612fcfd22SChris Mason /*
73571e75ef03SFilipe Manana * If the inode we're about to unlink was logged before, the log will be
73581e75ef03SFilipe Manana * properly updated with the new name with btrfs_log_new_name() and the
73591e75ef03SFilipe Manana * old name removed with btrfs_del_dir_entries_in_log() or with
73601e75ef03SFilipe Manana * btrfs_del_inode_ref_in_log().
736112fcfd22SChris Mason */
7362d67ba263SFilipe Manana if (inode_logged(trans, inode, NULL) == 1)
736312fcfd22SChris Mason return;
736412fcfd22SChris Mason
736512fcfd22SChris Mason /*
736612fcfd22SChris Mason * when renaming files across directories, if the directory
736712fcfd22SChris Mason * there we're unlinking from gets fsync'd later on, there's
736812fcfd22SChris Mason * no way to find the destination directory later and fsync it
736912fcfd22SChris Mason * properly. So, we have to be conservative and force commits
737012fcfd22SChris Mason * so the new name gets discovered.
737112fcfd22SChris Mason */
73724176bdbfSNikolay Borisov mutex_lock(&dir->log_mutex);
73734176bdbfSNikolay Borisov dir->last_unlink_trans = trans->transid;
73744176bdbfSNikolay Borisov mutex_unlock(&dir->log_mutex);
737512fcfd22SChris Mason }
737612fcfd22SChris Mason
737712fcfd22SChris Mason /*
73781ec9a1aeSFilipe Manana * Make sure that if someone attempts to fsync the parent directory of a deleted
73791ec9a1aeSFilipe Manana * snapshot, it ends up triggering a transaction commit. This is to guarantee
73801ec9a1aeSFilipe Manana * that after replaying the log tree of the parent directory's root we will not
73811ec9a1aeSFilipe Manana * see the snapshot anymore and at log replay time we will not see any log tree
73821ec9a1aeSFilipe Manana * corresponding to the deleted snapshot's root, which could lead to replaying
73831ec9a1aeSFilipe Manana * it after replaying the log tree of the parent directory (which would replay
73841ec9a1aeSFilipe Manana * the snapshot delete operation).
73852be63d5cSFilipe Manana *
73862be63d5cSFilipe Manana * Must be called before the actual snapshot destroy operation (updates to the
73872be63d5cSFilipe Manana * parent root and tree of tree roots trees, etc) are done.
73881ec9a1aeSFilipe Manana */
btrfs_record_snapshot_destroy(struct btrfs_trans_handle * trans,struct btrfs_inode * dir)73891ec9a1aeSFilipe Manana void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
739043663557SNikolay Borisov struct btrfs_inode *dir)
73911ec9a1aeSFilipe Manana {
739243663557SNikolay Borisov mutex_lock(&dir->log_mutex);
739343663557SNikolay Borisov dir->last_unlink_trans = trans->transid;
739443663557SNikolay Borisov mutex_unlock(&dir->log_mutex);
73951ec9a1aeSFilipe Manana }
73961ec9a1aeSFilipe Manana
739743dd529aSDavid Sterba /*
7398d5f5bd54SFilipe Manana * Update the log after adding a new name for an inode.
7399d5f5bd54SFilipe Manana *
7400d5f5bd54SFilipe Manana * @trans: Transaction handle.
7401d5f5bd54SFilipe Manana * @old_dentry: The dentry associated with the old name and the old
7402d5f5bd54SFilipe Manana * parent directory.
7403d5f5bd54SFilipe Manana * @old_dir: The inode of the previous parent directory for the case
7404d5f5bd54SFilipe Manana * of a rename. For a link operation, it must be NULL.
740588d2beecSFilipe Manana * @old_dir_index: The index number associated with the old name, meaningful
740688d2beecSFilipe Manana * only for rename operations (when @old_dir is not NULL).
740788d2beecSFilipe Manana * Ignored for link operations.
7408d5f5bd54SFilipe Manana * @parent: The dentry associated with the directory under which the
7409d5f5bd54SFilipe Manana * new name is located.
7410d5f5bd54SFilipe Manana *
7411d5f5bd54SFilipe Manana * Call this after adding a new name for an inode, as a result of a link or
7412d5f5bd54SFilipe Manana * rename operation, and it will properly update the log to reflect the new name.
741312fcfd22SChris Mason */
btrfs_log_new_name(struct btrfs_trans_handle * trans,struct dentry * old_dentry,struct btrfs_inode * old_dir,u64 old_dir_index,struct dentry * parent)741475b463d2SFilipe Manana void btrfs_log_new_name(struct btrfs_trans_handle *trans,
7415d5f5bd54SFilipe Manana struct dentry *old_dentry, struct btrfs_inode *old_dir,
741688d2beecSFilipe Manana u64 old_dir_index, struct dentry *parent)
741712fcfd22SChris Mason {
7418d5f5bd54SFilipe Manana struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry));
7419259c4b96SFilipe Manana struct btrfs_root *root = inode->root;
742075b463d2SFilipe Manana struct btrfs_log_ctx ctx;
7421259c4b96SFilipe Manana bool log_pinned = false;
74220f8ce498SFilipe Manana int ret;
742312fcfd22SChris Mason
742412fcfd22SChris Mason /*
7425af4176b4SChris Mason * this will force the logging code to walk the dentry chain
7426af4176b4SChris Mason * up for the file
7427af4176b4SChris Mason */
74289a6509c4SFilipe Manana if (!S_ISDIR(inode->vfs_inode.i_mode))
74299ca5fbfbSNikolay Borisov inode->last_unlink_trans = trans->transid;
7430af4176b4SChris Mason
7431af4176b4SChris Mason /*
743212fcfd22SChris Mason * if this inode hasn't been logged and directory we're renaming it
743312fcfd22SChris Mason * from hasn't been logged, we don't need to log it
743412fcfd22SChris Mason */
74350f8ce498SFilipe Manana ret = inode_logged(trans, inode, NULL);
74360f8ce498SFilipe Manana if (ret < 0) {
74370f8ce498SFilipe Manana goto out;
74380f8ce498SFilipe Manana } else if (ret == 0) {
74390f8ce498SFilipe Manana if (!old_dir)
744075b463d2SFilipe Manana return;
74410f8ce498SFilipe Manana /*
74420f8ce498SFilipe Manana * If the inode was not logged and we are doing a rename (old_dir is not
74430f8ce498SFilipe Manana * NULL), check if old_dir was logged - if it was not we can return and
74440f8ce498SFilipe Manana * do nothing.
74450f8ce498SFilipe Manana */
74460f8ce498SFilipe Manana ret = inode_logged(trans, old_dir, NULL);
74470f8ce498SFilipe Manana if (ret < 0)
74480f8ce498SFilipe Manana goto out;
74490f8ce498SFilipe Manana else if (ret == 0)
74500f8ce498SFilipe Manana return;
74510f8ce498SFilipe Manana }
74520f8ce498SFilipe Manana ret = 0;
745312fcfd22SChris Mason
745454a40fc3SFilipe Manana /*
745554a40fc3SFilipe Manana * If we are doing a rename (old_dir is not NULL) from a directory that
745688d2beecSFilipe Manana * was previously logged, make sure that on log replay we get the old
745788d2beecSFilipe Manana * dir entry deleted. This is needed because we will also log the new
745888d2beecSFilipe Manana * name of the renamed inode, so we need to make sure that after log
745988d2beecSFilipe Manana * replay we don't end up with both the new and old dir entries existing.
746054a40fc3SFilipe Manana */
746188d2beecSFilipe Manana if (old_dir && old_dir->logged_trans == trans->transid) {
746288d2beecSFilipe Manana struct btrfs_root *log = old_dir->root->log_root;
746388d2beecSFilipe Manana struct btrfs_path *path;
7464ab3c5c18SSweet Tea Dorminy struct fscrypt_name fname;
746588d2beecSFilipe Manana
746688d2beecSFilipe Manana ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX);
746788d2beecSFilipe Manana
7468ab3c5c18SSweet Tea Dorminy ret = fscrypt_setup_filename(&old_dir->vfs_inode,
7469ab3c5c18SSweet Tea Dorminy &old_dentry->d_name, 0, &fname);
7470ab3c5c18SSweet Tea Dorminy if (ret)
7471ab3c5c18SSweet Tea Dorminy goto out;
7472259c4b96SFilipe Manana /*
7473259c4b96SFilipe Manana * We have two inodes to update in the log, the old directory and
7474259c4b96SFilipe Manana * the inode that got renamed, so we must pin the log to prevent
7475259c4b96SFilipe Manana * anyone from syncing the log until we have updated both inodes
7476259c4b96SFilipe Manana * in the log.
7477259c4b96SFilipe Manana */
7478723df2bcSFilipe Manana ret = join_running_log_trans(root);
7479723df2bcSFilipe Manana /*
7480723df2bcSFilipe Manana * At least one of the inodes was logged before, so this should
7481723df2bcSFilipe Manana * not fail, but if it does, it's not serious, just bail out and
7482723df2bcSFilipe Manana * mark the log for a full commit.
7483723df2bcSFilipe Manana */
7484fee4c199SFilipe Manana if (WARN_ON_ONCE(ret < 0)) {
7485fee4c199SFilipe Manana fscrypt_free_filename(&fname);
7486723df2bcSFilipe Manana goto out;
7487fee4c199SFilipe Manana }
7488fee4c199SFilipe Manana
7489259c4b96SFilipe Manana log_pinned = true;
7490259c4b96SFilipe Manana
749188d2beecSFilipe Manana path = btrfs_alloc_path();
749288d2beecSFilipe Manana if (!path) {
7493259c4b96SFilipe Manana ret = -ENOMEM;
7494ab3c5c18SSweet Tea Dorminy fscrypt_free_filename(&fname);
7495259c4b96SFilipe Manana goto out;
749688d2beecSFilipe Manana }
749788d2beecSFilipe Manana
749888d2beecSFilipe Manana /*
749988d2beecSFilipe Manana * Other concurrent task might be logging the old directory,
750088d2beecSFilipe Manana * as it can be triggered when logging other inode that had or
7501750ee454SFilipe Manana * still has a dentry in the old directory. We lock the old
7502750ee454SFilipe Manana * directory's log_mutex to ensure the deletion of the old
7503750ee454SFilipe Manana * name is persisted, because during directory logging we
7504750ee454SFilipe Manana * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of
7505750ee454SFilipe Manana * the old name's dir index item is in the delayed items, so
7506750ee454SFilipe Manana * it could be missed by an in progress directory logging.
750788d2beecSFilipe Manana */
750888d2beecSFilipe Manana mutex_lock(&old_dir->log_mutex);
750988d2beecSFilipe Manana ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir),
75106db75318SSweet Tea Dorminy &fname.disk_name, old_dir_index);
751188d2beecSFilipe Manana if (ret > 0) {
751288d2beecSFilipe Manana /*
751388d2beecSFilipe Manana * The dentry does not exist in the log, so record its
751488d2beecSFilipe Manana * deletion.
751588d2beecSFilipe Manana */
751688d2beecSFilipe Manana btrfs_release_path(path);
751788d2beecSFilipe Manana ret = insert_dir_log_key(trans, log, path,
751888d2beecSFilipe Manana btrfs_ino(old_dir),
751988d2beecSFilipe Manana old_dir_index, old_dir_index);
752088d2beecSFilipe Manana }
752188d2beecSFilipe Manana mutex_unlock(&old_dir->log_mutex);
752288d2beecSFilipe Manana
752388d2beecSFilipe Manana btrfs_free_path(path);
7524ab3c5c18SSweet Tea Dorminy fscrypt_free_filename(&fname);
7525259c4b96SFilipe Manana if (ret < 0)
7526259c4b96SFilipe Manana goto out;
752788d2beecSFilipe Manana }
752854a40fc3SFilipe Manana
752975b463d2SFilipe Manana btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
753075b463d2SFilipe Manana ctx.logging_new_name = true;
753175b463d2SFilipe Manana /*
753275b463d2SFilipe Manana * We don't care about the return value. If we fail to log the new name
753375b463d2SFilipe Manana * then we know the next attempt to sync the log will fallback to a full
753475b463d2SFilipe Manana * transaction commit (due to a call to btrfs_set_log_full_commit()), so
753575b463d2SFilipe Manana * we don't need to worry about getting a log committed that has an
753675b463d2SFilipe Manana * inconsistent state after a rename operation.
753775b463d2SFilipe Manana */
753848778179SFilipe Manana btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
7539e09d94c9SFilipe Manana ASSERT(list_empty(&ctx.conflict_inodes));
7540259c4b96SFilipe Manana out:
7541259c4b96SFilipe Manana /*
75420f8ce498SFilipe Manana * If an error happened mark the log for a full commit because it's not
75430f8ce498SFilipe Manana * consistent and up to date or we couldn't find out if one of the
75440f8ce498SFilipe Manana * inodes was logged before in this transaction. Do it before unpinning
75450f8ce498SFilipe Manana * the log, to avoid any races with someone else trying to commit it.
7546259c4b96SFilipe Manana */
7547259c4b96SFilipe Manana if (ret < 0)
7548259c4b96SFilipe Manana btrfs_set_log_full_commit(trans);
75490f8ce498SFilipe Manana if (log_pinned)
7550259c4b96SFilipe Manana btrfs_end_log_trans(root);
7551259c4b96SFilipe Manana }
755212fcfd22SChris Mason
7553