blob: 2009cea65d89de1f539133150a5384ee9c202709 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Masone02119d2008-09-05 16:13:11 -04002/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
Chris Masone02119d2008-09-05 16:13:11 -04004 */
5
6#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +00008#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -04009#include <linux/list_sort.h>
Jeff Laytonc7f88c42017-12-11 06:35:12 -050010#include <linux/iversion.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020011#include "ctree.h"
Miao Xie995946d2014-04-02 19:51:06 +080012#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040013#include "disk-io.h"
14#include "locking.h"
15#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070016#include "backref.h"
Anand Jainebb87652016-03-10 17:26:59 +080017#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080018#include "qgroup.h"
Liu Bo900c9982018-01-25 11:02:56 -070019#include "inode-map.h"
Chris Masone02119d2008-09-05 16:13:11 -040020
21/* magic values for the inode_only field in btrfs_log_inode:
22 *
23 * LOG_INODE_ALL means to log everything
24 * LOG_INODE_EXISTS means to log just enough to recreate the inode
25 * during log replay
26 */
27#define LOG_INODE_ALL 0
28#define LOG_INODE_EXISTS 1
Liu Bo781feef2016-11-30 16:20:25 -080029#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040030
31/*
Chris Mason12fcfd22009-03-24 10:24:20 -040032 * directory trouble cases
33 *
34 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35 * log, we must force a full commit before doing an fsync of the directory
36 * where the unlink was done.
37 * ---> record transid of last unlink/rename per directory
38 *
39 * mkdir foo/some_dir
40 * normal commit
41 * rename foo/some_dir foo2/some_dir
42 * mkdir foo/some_dir
43 * fsync foo/some_dir/some_file
44 *
45 * The fsync above will unlink the original some_dir without recording
46 * it in its new location (foo2). After a crash, some_dir will be gone
47 * unless the fsync of some_file forces a full commit
48 *
49 * 2) we must log any new names for any file or dir that is in the fsync
50 * log. ---> check inode while renaming/linking.
51 *
52 * 2a) we must log any new names for any file or dir during rename
53 * when the directory they are being removed from was logged.
54 * ---> check inode and old parent dir during rename
55 *
56 * 2a is actually the more important variant. With the extra logging
57 * a crash might unlink the old name without recreating the new one
58 *
59 * 3) after a crash, we must go through any directories with a link count
60 * of zero and redo the rm -rf
61 *
62 * mkdir f1/foo
63 * normal commit
64 * rm -rf f1/foo
65 * fsync(f1)
66 *
67 * The directory f1 was fully removed from the FS, but fsync was never
68 * called on f1, only its parent dir. After a crash the rm -rf must
69 * be replayed. This must be able to recurse down the entire
70 * directory tree. The inode link count fixup code takes care of the
71 * ugly details.
72 */
73
74/*
Chris Masone02119d2008-09-05 16:13:11 -040075 * stages for the tree walking. The first
76 * stage (0) is to only pin down the blocks we find
77 * the second stage (1) is to make sure that all the inodes
78 * we find in the log are created in the subvolume.
79 *
80 * The last stage is to deal with directories and links and extents
81 * and all the other fun semantics
82 */
83#define LOG_WALK_PIN_ONLY 0
84#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040085#define LOG_WALK_REPLAY_DIR_INDEX 2
86#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040087
Chris Mason12fcfd22009-03-24 10:24:20 -040088static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +020089 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +010090 int inode_only,
91 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +010092 const loff_t end,
93 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -050094static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -040097static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_root *log,
100 struct btrfs_path *path,
101 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400102
103/*
104 * tree logging is a special write ahead log used to make sure that
105 * fsyncs and O_SYNCs can happen without doing full tree commits.
106 *
107 * Full tree commits are expensive because they require commonly
108 * modified blocks to be recowed, creating many dirty pages in the
109 * extent tree an 4x-6x higher write load than ext3.
110 *
111 * Instead of doing a tree commit on every fsync, we use the
112 * key ranges and transaction ids to find items for a given file or directory
113 * that have changed in this transaction. Those items are copied into
114 * a special tree (one per subvolume root), that tree is written to disk
115 * and then the fsync is considered complete.
116 *
117 * After a crash, items are copied out of the log-tree back into the
118 * subvolume tree. Any file data extents found are recorded in the extent
119 * allocation tree, and the log-tree freed.
120 *
121 * The log tree is read three times, once to pin down all the extents it is
122 * using in ram and once, once to create all the inodes logged in the tree
123 * and once to do all the other items.
124 */
125
126/*
Chris Masone02119d2008-09-05 16:13:11 -0400127 * start a sub transaction and setup the log tree
128 * this increments the log tree writer count to make the people
129 * syncing the tree wait for us to finish
130 */
131static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800132 struct btrfs_root *root,
133 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400134{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400135 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800136 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500137
138 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800139
Yan Zheng7237f182009-01-21 12:54:03 -0500140 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400141 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800142 ret = -EAGAIN;
143 goto out;
144 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800145
Josef Bacikff782e02009-10-08 15:30:04 -0400146 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800147 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800148 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400149 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800150 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400151 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800152 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400153 mutex_lock(&fs_info->tree_log_mutex);
154 if (!fs_info->log_root_tree)
155 ret = btrfs_init_log_root_tree(trans, fs_info);
156 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800157 if (ret)
158 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400159
Chris Masone02119d2008-09-05 16:13:11 -0400160 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400161 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800162 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800163
164 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
165 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800167
Miao Xie2ecb7922012-09-06 04:04:27 -0600168 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500169 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800170 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800171 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800172 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800173 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800174 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800175
Miao Xiee87ac132014-02-20 18:08:53 +0800176out:
Yan Zheng7237f182009-01-21 12:54:03 -0500177 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800178 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400179}
180
181/*
182 * returns 0 if there was a log transaction running and we were able
183 * to join, or returns -ENOENT if there were not transactions
184 * in progress
185 */
186static int join_running_log_trans(struct btrfs_root *root)
187{
188 int ret = -ENOENT;
189
190 smp_mb();
191 if (!root->log_root)
192 return -ENOENT;
193
Yan Zheng7237f182009-01-21 12:54:03 -0500194 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400195 if (root->log_root) {
196 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500197 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400198 }
Yan Zheng7237f182009-01-21 12:54:03 -0500199 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400200 return ret;
201}
202
203/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400204 * This either makes the current running log transaction wait
205 * until you call btrfs_end_log_trans() or it makes any future
206 * log transactions wait until you call btrfs_end_log_trans()
207 */
208int btrfs_pin_log_trans(struct btrfs_root *root)
209{
210 int ret = -ENOENT;
211
212 mutex_lock(&root->log_mutex);
213 atomic_inc(&root->log_writers);
214 mutex_unlock(&root->log_mutex);
215 return ret;
216}
217
218/*
Chris Masone02119d2008-09-05 16:13:11 -0400219 * indicate we're done making changes to the log tree
220 * and wake up anyone waiting to do a sync
221 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100222void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400223{
Yan Zheng7237f182009-01-21 12:54:03 -0500224 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100225 /*
226 * Implicit memory barrier after atomic_dec_and_test
227 */
Yan Zheng7237f182009-01-21 12:54:03 -0500228 if (waitqueue_active(&root->log_writer_wait))
229 wake_up(&root->log_writer_wait);
230 }
Chris Masone02119d2008-09-05 16:13:11 -0400231}
232
233
234/*
235 * the walk control struct is used to pass state down the chain when
236 * processing the log tree. The stage field tells us which part
237 * of the log tree processing we are currently doing. The others
238 * are state fields used for that specific part
239 */
240struct walk_control {
241 /* should we free the extent on disk when done? This is used
242 * at transaction commit time while freeing a log tree
243 */
244 int free;
245
246 /* should we write out the extent buffer? This is used
247 * while flushing the log tree to disk during a sync
248 */
249 int write;
250
251 /* should we wait for the extent buffer io to finish? Also used
252 * while flushing the log tree to disk for a sync
253 */
254 int wait;
255
256 /* pin only walk, we record which extents on disk belong to the
257 * log trees
258 */
259 int pin;
260
261 /* what stage of the replay code we're currently in */
262 int stage;
263
264 /* the root we are currently replaying */
265 struct btrfs_root *replay_dest;
266
267 /* the trans handle for the current replay */
268 struct btrfs_trans_handle *trans;
269
270 /* the function that gets used to process blocks we find in the
271 * tree. Note the extent_buffer might not be up to date when it is
272 * passed in, and it must be checked or read if you need the data
273 * inside it
274 */
275 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800276 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400277};
278
279/*
280 * process_func used to pin down extents, write them or wait on them
281 */
282static int process_one_buffer(struct btrfs_root *log,
283 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800284 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400286 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400287 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400288
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400289 /*
290 * If this fs is mixed then we need to be able to process the leaves to
291 * pin down any logged extents, so we have to read the block.
292 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800294 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400295 if (ret)
296 return ret;
297 }
298
Josef Bacikb50c6e22013-04-25 15:55:30 -0400299 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400300 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
301 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400302
303 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400304 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400305 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400306 if (wc->write)
307 btrfs_write_tree_block(eb);
308 if (wc->wait)
309 btrfs_wait_tree_block_writeback(eb);
310 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400311 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400312}
313
314/*
315 * Item overwrite used by replay and tree logging. eb, slot and key all refer
316 * to the src data we are copying out.
317 *
318 * root is the tree we are copying into, and path is a scratch
319 * path for use in this function (it should be released on entry and
320 * will be released on exit).
321 *
322 * If the key is already in the destination tree the existing item is
323 * overwritten. If the existing item isn't big enough, it is extended.
324 * If it is too large, it is truncated.
325 *
326 * If the key isn't in the destination yet, a new item is inserted.
327 */
328static noinline int overwrite_item(struct btrfs_trans_handle *trans,
329 struct btrfs_root *root,
330 struct btrfs_path *path,
331 struct extent_buffer *eb, int slot,
332 struct btrfs_key *key)
333{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400334 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400335 int ret;
336 u32 item_size;
337 u64 saved_i_size = 0;
338 int save_old_i_size = 0;
339 unsigned long src_ptr;
340 unsigned long dst_ptr;
341 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000342 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400343
344 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
345 overwrite_root = 1;
346
347 item_size = btrfs_item_size_nr(eb, slot);
348 src_ptr = btrfs_item_ptr_offset(eb, slot);
349
350 /* look for the key in the destination tree */
351 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000352 if (ret < 0)
353 return ret;
354
Chris Masone02119d2008-09-05 16:13:11 -0400355 if (ret == 0) {
356 char *src_copy;
357 char *dst_copy;
358 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
359 path->slots[0]);
360 if (dst_size != item_size)
361 goto insert;
362
363 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200364 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400365 return 0;
366 }
367 dst_copy = kmalloc(item_size, GFP_NOFS);
368 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000369 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200370 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000371 kfree(dst_copy);
372 kfree(src_copy);
373 return -ENOMEM;
374 }
Chris Masone02119d2008-09-05 16:13:11 -0400375
376 read_extent_buffer(eb, src_copy, src_ptr, item_size);
377
378 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
379 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
380 item_size);
381 ret = memcmp(dst_copy, src_copy, item_size);
382
383 kfree(dst_copy);
384 kfree(src_copy);
385 /*
386 * they have the same contents, just return, this saves
387 * us from cowing blocks in the destination tree and doing
388 * extra writes that may not have been done by a previous
389 * sync
390 */
391 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200392 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400393 return 0;
394 }
395
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000396 /*
397 * We need to load the old nbytes into the inode so when we
398 * replay the extents we've logged we get the right nbytes.
399 */
400 if (inode_item) {
401 struct btrfs_inode_item *item;
402 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400403 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000404
405 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
406 struct btrfs_inode_item);
407 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
408 item = btrfs_item_ptr(eb, slot,
409 struct btrfs_inode_item);
410 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400411
412 /*
413 * If this is a directory we need to reset the i_size to
414 * 0 so that we can set it up properly when replaying
415 * the rest of the items in this log.
416 */
417 mode = btrfs_inode_mode(eb, item);
418 if (S_ISDIR(mode))
419 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000420 }
421 } else if (inode_item) {
422 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400423 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000424
425 /*
426 * New inode, set nbytes to 0 so that the nbytes comes out
427 * properly when we replay the extents.
428 */
429 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
430 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400431
432 /*
433 * If this is a directory we need to reset the i_size to 0 so
434 * that we can set it up properly when replaying the rest of
435 * the items in this log.
436 */
437 mode = btrfs_inode_mode(eb, item);
438 if (S_ISDIR(mode))
439 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400440 }
441insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200442 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400443 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000444 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400445 ret = btrfs_insert_empty_item(trans, root, path,
446 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000447 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400448
449 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000450 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400451 u32 found_size;
452 found_size = btrfs_item_size_nr(path->nodes[0],
453 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100454 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400455 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100456 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400457 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100458 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400459 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400460 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400461 }
462 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
463 path->slots[0]);
464
465 /* don't overwrite an existing inode if the generation number
466 * was logged as zero. This is done when the tree logging code
467 * is just logging an inode to make sure it exists after recovery.
468 *
469 * Also, don't overwrite i_size on directories during replay.
470 * log replay inserts and removes directory items based on the
471 * state of the tree found in the subvolume, and i_size is modified
472 * as it goes
473 */
474 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
475 struct btrfs_inode_item *src_item;
476 struct btrfs_inode_item *dst_item;
477
478 src_item = (struct btrfs_inode_item *)src_ptr;
479 dst_item = (struct btrfs_inode_item *)dst_ptr;
480
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000481 if (btrfs_inode_generation(eb, src_item) == 0) {
482 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000483 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000484
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000485 /*
486 * For regular files an ino_size == 0 is used only when
487 * logging that an inode exists, as part of a directory
488 * fsync, and the inode wasn't fsynced before. In this
489 * case don't set the size of the inode in the fs/subvol
490 * tree, otherwise we would be throwing valid data away.
491 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000492 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000493 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
494 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000495 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000496
497 btrfs_init_map_token(&token);
498 btrfs_set_token_inode_size(dst_eb, dst_item,
499 ino_size, &token);
500 }
Chris Masone02119d2008-09-05 16:13:11 -0400501 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000502 }
Chris Masone02119d2008-09-05 16:13:11 -0400503
504 if (overwrite_root &&
505 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
506 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
507 save_old_i_size = 1;
508 saved_i_size = btrfs_inode_size(path->nodes[0],
509 dst_item);
510 }
511 }
512
513 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
514 src_ptr, item_size);
515
516 if (save_old_i_size) {
517 struct btrfs_inode_item *dst_item;
518 dst_item = (struct btrfs_inode_item *)dst_ptr;
519 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
520 }
521
522 /* make sure the generation is filled in */
523 if (key->type == BTRFS_INODE_ITEM_KEY) {
524 struct btrfs_inode_item *dst_item;
525 dst_item = (struct btrfs_inode_item *)dst_ptr;
526 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
527 btrfs_set_inode_generation(path->nodes[0], dst_item,
528 trans->transid);
529 }
530 }
531no_copy:
532 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200533 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400534 return 0;
535}
536
537/*
538 * simple helper to read an inode off the disk from a given root
539 * This can only be called for subvolume roots and not for the log
540 */
541static noinline struct inode *read_one_inode(struct btrfs_root *root,
542 u64 objectid)
543{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400544 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400545 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400546
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400547 key.objectid = objectid;
548 key.type = BTRFS_INODE_ITEM_KEY;
549 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000550 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400551 if (IS_ERR(inode)) {
552 inode = NULL;
553 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400554 iput(inode);
555 inode = NULL;
556 }
557 return inode;
558}
559
560/* replays a single extent in 'eb' at 'slot' with 'key' into the
561 * subvolume 'root'. path is released on entry and should be released
562 * on exit.
563 *
564 * extents in the log tree have not been allocated out of the extent
565 * tree yet. So, this completes the allocation, taking a reference
566 * as required if the extent already exists or creating a new extent
567 * if it isn't in the extent allocation tree yet.
568 *
569 * The extent is inserted into the file, dropping any existing extents
570 * from the file that overlap the new one.
571 */
572static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root,
574 struct btrfs_path *path,
575 struct extent_buffer *eb, int slot,
576 struct btrfs_key *key)
577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400578 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400579 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400580 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400581 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000582 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400583 struct btrfs_file_extent_item *item;
584 struct inode *inode = NULL;
585 unsigned long size;
586 int ret = 0;
587
588 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
589 found_type = btrfs_file_extent_type(eb, item);
590
Yan Zhengd899e052008-10-30 14:25:28 -0400591 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000592 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
593 nbytes = btrfs_file_extent_num_bytes(eb, item);
594 extent_end = start + nbytes;
595
596 /*
597 * We don't add to the inodes nbytes if we are prealloc or a
598 * hole.
599 */
600 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
601 nbytes = 0;
602 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800603 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000604 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400605 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400606 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400607 } else {
608 ret = 0;
609 goto out;
610 }
611
612 inode = read_one_inode(root, key->objectid);
613 if (!inode) {
614 ret = -EIO;
615 goto out;
616 }
617
618 /*
619 * first check to see if we already have this extent in the
620 * file. This must be done before the btrfs_drop_extents run
621 * so we don't try to drop this extent.
622 */
David Sterbaf85b7372017-01-20 14:54:07 +0100623 ret = btrfs_lookup_file_extent(trans, root, path,
624 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400625
Yan Zhengd899e052008-10-30 14:25:28 -0400626 if (ret == 0 &&
627 (found_type == BTRFS_FILE_EXTENT_REG ||
628 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400629 struct btrfs_file_extent_item cmp1;
630 struct btrfs_file_extent_item cmp2;
631 struct btrfs_file_extent_item *existing;
632 struct extent_buffer *leaf;
633
634 leaf = path->nodes[0];
635 existing = btrfs_item_ptr(leaf, path->slots[0],
636 struct btrfs_file_extent_item);
637
638 read_extent_buffer(eb, &cmp1, (unsigned long)item,
639 sizeof(cmp1));
640 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
641 sizeof(cmp2));
642
643 /*
644 * we already have a pointer to this exact extent,
645 * we don't have to do anything
646 */
647 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200648 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400649 goto out;
650 }
651 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200652 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400653
654 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400655 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400656 if (ret)
657 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400658
Yan Zheng07d400a2009-01-06 11:42:00 -0500659 if (found_type == BTRFS_FILE_EXTENT_REG ||
660 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400661 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500662 unsigned long dest_offset;
663 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400664
Filipe Manana3168021c2017-02-01 14:58:02 +0000665 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
666 btrfs_fs_incompat(fs_info, NO_HOLES))
667 goto update_inode;
668
Yan Zheng07d400a2009-01-06 11:42:00 -0500669 ret = btrfs_insert_empty_item(trans, root, path, key,
670 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400671 if (ret)
672 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500673 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
674 path->slots[0]);
675 copy_extent_buffer(path->nodes[0], eb, dest_offset,
676 (unsigned long)item, sizeof(*item));
677
678 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
679 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
680 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400681 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500682
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800683 /*
684 * Manually record dirty extent, as here we did a shallow
685 * file extent item copy and skip normal backref update,
686 * but modifying extent tree all by ourselves.
687 * So need to manually record dirty extent for qgroup,
688 * as the owner of the file extent changed from log tree
689 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
690 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400691 ret = btrfs_qgroup_trace_extent(trans, fs_info,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800692 btrfs_file_extent_disk_bytenr(eb, item),
693 btrfs_file_extent_disk_num_bytes(eb, item),
694 GFP_NOFS);
695 if (ret < 0)
696 goto out;
697
Yan Zheng07d400a2009-01-06 11:42:00 -0500698 if (ins.objectid > 0) {
699 u64 csum_start;
700 u64 csum_end;
701 LIST_HEAD(ordered_sums);
702 /*
703 * is this extent already allocated in the extent
704 * allocation tree? If so, just add a reference
705 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400706 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500707 ins.offset);
708 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400709 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500710 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400711 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100712 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400713 if (ret)
714 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500715 } else {
716 /*
717 * insert the extent pointer in the extent
718 * allocation tree
719 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400720 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400721 fs_info,
722 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400723 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400724 if (ret)
725 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500726 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200727 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500728
729 if (btrfs_file_extent_compression(eb, item)) {
730 csum_start = ins.objectid;
731 csum_end = csum_start + ins.offset;
732 } else {
733 csum_start = ins.objectid +
734 btrfs_file_extent_offset(eb, item);
735 csum_end = csum_start +
736 btrfs_file_extent_num_bytes(eb, item);
737 }
738
739 ret = btrfs_lookup_csums_range(root->log_root,
740 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100741 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400742 if (ret)
743 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100744 /*
745 * Now delete all existing cums in the csum root that
746 * cover our range. We do this because we can have an
747 * extent that is completely referenced by one file
748 * extent item and partially referenced by another
749 * file extent item (like after using the clone or
750 * extent_same ioctls). In this case if we end up doing
751 * the replay of the one that partially references the
752 * extent first, and we do not do the csum deletion
753 * below, we can get 2 csum items in the csum tree that
754 * overlap each other. For example, imagine our log has
755 * the two following file extent items:
756 *
757 * key (257 EXTENT_DATA 409600)
758 * extent data disk byte 12845056 nr 102400
759 * extent data offset 20480 nr 20480 ram 102400
760 *
761 * key (257 EXTENT_DATA 819200)
762 * extent data disk byte 12845056 nr 102400
763 * extent data offset 0 nr 102400 ram 102400
764 *
765 * Where the second one fully references the 100K extent
766 * that starts at disk byte 12845056, and the log tree
767 * has a single csum item that covers the entire range
768 * of the extent:
769 *
770 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
771 *
772 * After the first file extent item is replayed, the
773 * csum tree gets the following csum item:
774 *
775 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
776 *
777 * Which covers the 20K sub-range starting at offset 20K
778 * of our extent. Now when we replay the second file
779 * extent item, if we do not delete existing csum items
780 * that cover any of its blocks, we end up getting two
781 * csum items in our csum tree that overlap each other:
782 *
783 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
784 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
785 *
786 * Which is a problem, because after this anyone trying
787 * to lookup up for the checksum of any block of our
788 * extent starting at an offset of 40K or higher, will
789 * end up looking at the second csum item only, which
790 * does not contain the checksum for any block starting
791 * at offset 40K or higher of our extent.
792 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500793 while (!list_empty(&ordered_sums)) {
794 struct btrfs_ordered_sum *sums;
795 sums = list_entry(ordered_sums.next,
796 struct btrfs_ordered_sum,
797 list);
Josef Bacik36508602013-04-25 16:23:32 -0400798 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400799 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400800 sums->bytenr,
801 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100802 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400803 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400804 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500805 list_del(&sums->list);
806 kfree(sums);
807 }
Josef Bacik36508602013-04-25 16:23:32 -0400808 if (ret)
809 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500810 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200811 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500812 }
813 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
814 /* inline extents are easy, we just overwrite them */
815 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400816 if (ret)
817 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500818 }
819
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000820 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000821update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600822 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400823out:
824 if (inode)
825 iput(inode);
826 return ret;
827}
828
829/*
830 * when cleaning up conflicts between the directory names in the
831 * subvolume, directory names in the log and directory names in the
832 * inode back references, we may have to unlink inodes from directories.
833 *
834 * This is a helper function to do the unlink of a specific directory
835 * item
836 */
837static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
838 struct btrfs_root *root,
839 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200840 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400841 struct btrfs_dir_item *di)
842{
843 struct inode *inode;
844 char *name;
845 int name_len;
846 struct extent_buffer *leaf;
847 struct btrfs_key location;
848 int ret;
849
850 leaf = path->nodes[0];
851
852 btrfs_dir_item_key_to_cpu(leaf, di, &location);
853 name_len = btrfs_dir_name_len(leaf, di);
854 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000855 if (!name)
856 return -ENOMEM;
857
Chris Masone02119d2008-09-05 16:13:11 -0400858 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200859 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400860
861 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000862 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400863 ret = -EIO;
864 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000865 }
Chris Masone02119d2008-09-05 16:13:11 -0400866
Yan Zhengec051c02009-01-05 15:43:42 -0500867 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400868 if (ret)
869 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400870
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200871 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
872 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400873 if (ret)
874 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100875 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200876 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400877out:
878 kfree(name);
879 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400880 return ret;
881}
882
883/*
884 * helper function to see if a given name and sequence number found
885 * in an inode back reference are already in a directory and correctly
886 * point to this inode
887 */
888static noinline int inode_in_dir(struct btrfs_root *root,
889 struct btrfs_path *path,
890 u64 dirid, u64 objectid, u64 index,
891 const char *name, int name_len)
892{
893 struct btrfs_dir_item *di;
894 struct btrfs_key location;
895 int match = 0;
896
897 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
898 index, name, name_len, 0);
899 if (di && !IS_ERR(di)) {
900 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
901 if (location.objectid != objectid)
902 goto out;
903 } else
904 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200905 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400906
907 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
908 if (di && !IS_ERR(di)) {
909 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
910 if (location.objectid != objectid)
911 goto out;
912 } else
913 goto out;
914 match = 1;
915out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200916 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400917 return match;
918}
919
920/*
921 * helper function to check a log tree for a named back reference in
922 * an inode. This is used to decide if a back reference that is
923 * found in the subvolume conflicts with what we find in the log.
924 *
925 * inode backreferences may have multiple refs in a single item,
926 * during replay we process one reference at a time, and we don't
927 * want to delete valid links to a file from the subvolume if that
928 * link is also in the log.
929 */
930static noinline int backref_in_log(struct btrfs_root *log,
931 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700932 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000933 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400934{
935 struct btrfs_path *path;
936 struct btrfs_inode_ref *ref;
937 unsigned long ptr;
938 unsigned long ptr_end;
939 unsigned long name_ptr;
940 int found_name_len;
941 int item_size;
942 int ret;
943 int match = 0;
944
945 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000946 if (!path)
947 return -ENOMEM;
948
Chris Masone02119d2008-09-05 16:13:11 -0400949 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
950 if (ret != 0)
951 goto out;
952
Chris Masone02119d2008-09-05 16:13:11 -0400953 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700954
955 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000956 if (btrfs_find_name_in_ext_backref(path->nodes[0],
957 path->slots[0],
958 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700959 name, namelen, NULL))
960 match = 1;
961
962 goto out;
963 }
964
965 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400966 ptr_end = ptr + item_size;
967 while (ptr < ptr_end) {
968 ref = (struct btrfs_inode_ref *)ptr;
969 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
970 if (found_name_len == namelen) {
971 name_ptr = (unsigned long)(ref + 1);
972 ret = memcmp_extent_buffer(path->nodes[0], name,
973 name_ptr, namelen);
974 if (ret == 0) {
975 match = 1;
976 goto out;
977 }
978 }
979 ptr = (unsigned long)(ref + 1) + found_name_len;
980 }
981out:
982 btrfs_free_path(path);
983 return match;
984}
985
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700986static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
988 struct btrfs_path *path,
989 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200990 struct btrfs_inode *dir,
991 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -0700992 u64 inode_objectid, u64 parent_objectid,
993 u64 ref_index, char *name, int namelen,
994 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700995{
996 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700997 char *victim_name;
998 int victim_name_len;
999 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001000 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001001 struct btrfs_key search_key;
1002 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001003
Mark Fashehf1863732012-08-08 11:32:27 -07001004again:
1005 /* Search old style refs */
1006 search_key.objectid = inode_objectid;
1007 search_key.type = BTRFS_INODE_REF_KEY;
1008 search_key.offset = parent_objectid;
1009 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001011 struct btrfs_inode_ref *victim_ref;
1012 unsigned long ptr;
1013 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001014
1015 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001016
1017 /* are we trying to overwrite a back ref for the root directory
1018 * if so, just jump out, we're done
1019 */
Mark Fashehf1863732012-08-08 11:32:27 -07001020 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001021 return 1;
1022
1023 /* check all the names in this back reference to see
1024 * if they are in the log. if so, we allow them to stay
1025 * otherwise they must be unlinked as a conflict
1026 */
1027 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1028 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1029 while (ptr < ptr_end) {
1030 victim_ref = (struct btrfs_inode_ref *)ptr;
1031 victim_name_len = btrfs_inode_ref_name_len(leaf,
1032 victim_ref);
1033 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001034 if (!victim_name)
1035 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001036
1037 read_extent_buffer(leaf, victim_name,
1038 (unsigned long)(victim_ref + 1),
1039 victim_name_len);
1040
Mark Fashehf1863732012-08-08 11:32:27 -07001041 if (!backref_in_log(log_root, &search_key,
1042 parent_objectid,
1043 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001044 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001045 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001046 btrfs_release_path(path);
1047
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001048 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001049 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001050 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001051 if (ret)
1052 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001053 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001054 if (ret)
1055 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001056 *search_done = 1;
1057 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 }
1059 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001060
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001061 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1062 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001063
1064 /*
1065 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001066 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001067 */
1068 *search_done = 1;
1069 }
1070 btrfs_release_path(path);
1071
Mark Fashehf1863732012-08-08 11:32:27 -07001072 /* Same search but for extended refs */
1073 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1074 inode_objectid, parent_objectid, 0,
1075 0);
1076 if (!IS_ERR_OR_NULL(extref)) {
1077 u32 item_size;
1078 u32 cur_offset = 0;
1079 unsigned long base;
1080 struct inode *victim_parent;
1081
1082 leaf = path->nodes[0];
1083
1084 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1085 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1086
1087 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001088 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001089
1090 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1091
1092 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1093 goto next;
1094
1095 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001096 if (!victim_name)
1097 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001098 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1099 victim_name_len);
1100
1101 search_key.objectid = inode_objectid;
1102 search_key.type = BTRFS_INODE_EXTREF_KEY;
1103 search_key.offset = btrfs_extref_hash(parent_objectid,
1104 victim_name,
1105 victim_name_len);
1106 ret = 0;
1107 if (!backref_in_log(log_root, &search_key,
1108 parent_objectid, victim_name,
1109 victim_name_len)) {
1110 ret = -ENOENT;
1111 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001112 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001113 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001114 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001115 btrfs_release_path(path);
1116
1117 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001118 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001119 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001120 victim_name,
1121 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001122 if (!ret)
1123 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001124 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001125 }
Mark Fashehf1863732012-08-08 11:32:27 -07001126 iput(victim_parent);
1127 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001128 if (ret)
1129 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001130 *search_done = 1;
1131 goto again;
1132 }
1133 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001134next:
1135 cur_offset += victim_name_len + sizeof(*extref);
1136 }
1137 *search_done = 1;
1138 }
1139 btrfs_release_path(path);
1140
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001141 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001142 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001143 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001144 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001145 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001146 if (ret)
1147 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001148 }
1149 btrfs_release_path(path);
1150
1151 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001152 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001153 name, namelen, 0);
1154 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001155 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001156 if (ret)
1157 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001158 }
1159 btrfs_release_path(path);
1160
1161 return 0;
1162}
Chris Masone02119d2008-09-05 16:13:11 -04001163
Qu Wenruobae15d92017-11-08 08:54:26 +08001164static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1165 u32 *namelen, char **name, u64 *index,
1166 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001167{
1168 struct btrfs_inode_extref *extref;
1169
1170 extref = (struct btrfs_inode_extref *)ref_ptr;
1171
1172 *namelen = btrfs_inode_extref_name_len(eb, extref);
1173 *name = kmalloc(*namelen, GFP_NOFS);
1174 if (*name == NULL)
1175 return -ENOMEM;
1176
1177 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1178 *namelen);
1179
Filipe Manana1f250e92018-02-28 15:56:10 +00001180 if (index)
1181 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001182 if (parent_objectid)
1183 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1184
1185 return 0;
1186}
1187
Qu Wenruobae15d92017-11-08 08:54:26 +08001188static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1189 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001190{
1191 struct btrfs_inode_ref *ref;
1192
1193 ref = (struct btrfs_inode_ref *)ref_ptr;
1194
1195 *namelen = btrfs_inode_ref_name_len(eb, ref);
1196 *name = kmalloc(*namelen, GFP_NOFS);
1197 if (*name == NULL)
1198 return -ENOMEM;
1199
1200 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1201
Filipe Manana1f250e92018-02-28 15:56:10 +00001202 if (index)
1203 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001204
1205 return 0;
1206}
1207
Chris Masone02119d2008-09-05 16:13:11 -04001208/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001209 * Take an inode reference item from the log tree and iterate all names from the
1210 * inode reference item in the subvolume tree with the same key (if it exists).
1211 * For any name that is not in the inode reference item from the log tree, do a
1212 * proper unlink of that name (that is, remove its entry from the inode
1213 * reference item and both dir index keys).
1214 */
1215static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_path *path,
1218 struct btrfs_inode *inode,
1219 struct extent_buffer *log_eb,
1220 int log_slot,
1221 struct btrfs_key *key)
1222{
1223 int ret;
1224 unsigned long ref_ptr;
1225 unsigned long ref_end;
1226 struct extent_buffer *eb;
1227
1228again:
1229 btrfs_release_path(path);
1230 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1231 if (ret > 0) {
1232 ret = 0;
1233 goto out;
1234 }
1235 if (ret < 0)
1236 goto out;
1237
1238 eb = path->nodes[0];
1239 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1240 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1241 while (ref_ptr < ref_end) {
1242 char *name = NULL;
1243 int namelen;
1244 u64 parent_id;
1245
1246 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1247 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1248 NULL, &parent_id);
1249 } else {
1250 parent_id = key->offset;
1251 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1252 NULL);
1253 }
1254 if (ret)
1255 goto out;
1256
1257 if (key->type == BTRFS_INODE_EXTREF_KEY)
1258 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1259 parent_id, name,
1260 namelen, NULL);
1261 else
1262 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1263 namelen, NULL);
1264
1265 if (!ret) {
1266 struct inode *dir;
1267
1268 btrfs_release_path(path);
1269 dir = read_one_inode(root, parent_id);
1270 if (!dir) {
1271 ret = -ENOENT;
1272 kfree(name);
1273 goto out;
1274 }
1275 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1276 inode, name, namelen);
1277 kfree(name);
1278 iput(dir);
1279 if (ret)
1280 goto out;
1281 goto again;
1282 }
1283
1284 kfree(name);
1285 ref_ptr += namelen;
1286 if (key->type == BTRFS_INODE_EXTREF_KEY)
1287 ref_ptr += sizeof(struct btrfs_inode_extref);
1288 else
1289 ref_ptr += sizeof(struct btrfs_inode_ref);
1290 }
1291 ret = 0;
1292 out:
1293 btrfs_release_path(path);
1294 return ret;
1295}
1296
1297/*
Chris Masone02119d2008-09-05 16:13:11 -04001298 * replay one inode back reference item found in the log tree.
1299 * eb, slot and key refer to the buffer and key found in the log tree.
1300 * root is the destination we are replaying into, and path is for temp
1301 * use by this function. (it should be released on return).
1302 */
1303static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1304 struct btrfs_root *root,
1305 struct btrfs_root *log,
1306 struct btrfs_path *path,
1307 struct extent_buffer *eb, int slot,
1308 struct btrfs_key *key)
1309{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001310 struct inode *dir = NULL;
1311 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001312 unsigned long ref_ptr;
1313 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001314 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001315 int namelen;
1316 int ret;
liuboc622ae62011-03-26 08:01:12 -04001317 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001318 int log_ref_ver = 0;
1319 u64 parent_objectid;
1320 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001321 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001322 int ref_struct_size;
1323
1324 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1325 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1326
1327 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1328 struct btrfs_inode_extref *r;
1329
1330 ref_struct_size = sizeof(struct btrfs_inode_extref);
1331 log_ref_ver = 1;
1332 r = (struct btrfs_inode_extref *)ref_ptr;
1333 parent_objectid = btrfs_inode_extref_parent(eb, r);
1334 } else {
1335 ref_struct_size = sizeof(struct btrfs_inode_ref);
1336 parent_objectid = key->offset;
1337 }
1338 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001339
Chris Masone02119d2008-09-05 16:13:11 -04001340 /*
1341 * it is possible that we didn't log all the parent directories
1342 * for a given inode. If we don't find the dir, just don't
1343 * copy the back ref in. The link count fixup code will take
1344 * care of the rest
1345 */
Mark Fashehf1863732012-08-08 11:32:27 -07001346 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001347 if (!dir) {
1348 ret = -ENOENT;
1349 goto out;
1350 }
Chris Masone02119d2008-09-05 16:13:11 -04001351
Mark Fashehf1863732012-08-08 11:32:27 -07001352 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001353 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001354 ret = -EIO;
1355 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001356 }
Chris Masone02119d2008-09-05 16:13:11 -04001357
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001358 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001359 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001360 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1361 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001362 /*
1363 * parent object can change from one array
1364 * item to another.
1365 */
1366 if (!dir)
1367 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001368 if (!dir) {
1369 ret = -ENOENT;
1370 goto out;
1371 }
Mark Fashehf1863732012-08-08 11:32:27 -07001372 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001373 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1374 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001375 }
1376 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001377 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001378
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001379 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001380 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1381 btrfs_ino(BTRFS_I(inode)), ref_index,
1382 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001383 /*
1384 * look for a conflicting back reference in the
1385 * metadata. if we find one we have to unlink that name
1386 * of the file before we add our new link. Later on, we
1387 * overwrite any existing back reference, and we don't
1388 * want to create dangling pointers in the directory.
1389 */
Chris Masone02119d2008-09-05 16:13:11 -04001390
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001391 if (!search_done) {
1392 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001393 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001394 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001395 inode_objectid,
1396 parent_objectid,
1397 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001398 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001399 if (ret) {
1400 if (ret == 1)
1401 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001402 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001403 }
Chris Masone02119d2008-09-05 16:13:11 -04001404 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001405
1406 /* insert our name */
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001407 ret = btrfs_add_link(trans, BTRFS_I(dir),
1408 BTRFS_I(inode),
1409 name, namelen, 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001410 if (ret)
1411 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001412
1413 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001414 }
liuboc622ae62011-03-26 08:01:12 -04001415
Mark Fashehf1863732012-08-08 11:32:27 -07001416 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001417 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001418 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001419 if (log_ref_ver) {
1420 iput(dir);
1421 dir = NULL;
1422 }
Chris Masone02119d2008-09-05 16:13:11 -04001423 }
Chris Masone02119d2008-09-05 16:13:11 -04001424
Filipe Manana1f250e92018-02-28 15:56:10 +00001425 /*
1426 * Before we overwrite the inode reference item in the subvolume tree
1427 * with the item from the log tree, we must unlink all names from the
1428 * parent directory that are in the subvolume's tree inode reference
1429 * item, otherwise we end up with an inconsistent subvolume tree where
1430 * dir index entries exist for a name but there is no inode reference
1431 * item with the same name.
1432 */
1433 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1434 key);
1435 if (ret)
1436 goto out;
1437
Chris Masone02119d2008-09-05 16:13:11 -04001438 /* finally write the back reference in the inode */
1439 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001440out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001441 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001442 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001443 iput(dir);
1444 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001445 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001446}
1447
Yan, Zhengc71bf092009-11-12 09:34:40 +00001448static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001449 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001450{
1451 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001452
David Sterba9c4f61f2015-01-02 19:12:57 +01001453 ret = btrfs_insert_orphan_item(trans, root, ino);
1454 if (ret == -EEXIST)
1455 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001456
Yan, Zhengc71bf092009-11-12 09:34:40 +00001457 return ret;
1458}
1459
Mark Fashehf1863732012-08-08 11:32:27 -07001460static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001461 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001462{
Mark Fashehf1863732012-08-08 11:32:27 -07001463 int ret = 0;
1464 int name_len;
1465 unsigned int nlink = 0;
1466 u32 item_size;
1467 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001468 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001469 u64 offset = 0;
1470 unsigned long ptr;
1471 struct btrfs_inode_extref *extref;
1472 struct extent_buffer *leaf;
1473
1474 while (1) {
1475 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1476 &extref, &offset);
1477 if (ret)
1478 break;
1479
1480 leaf = path->nodes[0];
1481 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1482 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001483 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001484
1485 while (cur_offset < item_size) {
1486 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1487 name_len = btrfs_inode_extref_name_len(leaf, extref);
1488
1489 nlink++;
1490
1491 cur_offset += name_len + sizeof(*extref);
1492 }
1493
1494 offset++;
1495 btrfs_release_path(path);
1496 }
1497 btrfs_release_path(path);
1498
Filipe Manana2c2c4522015-01-13 16:40:04 +00001499 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001500 return ret;
1501 return nlink;
1502}
1503
1504static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001505 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001506{
Chris Masone02119d2008-09-05 16:13:11 -04001507 int ret;
1508 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001509 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001510 unsigned long ptr;
1511 unsigned long ptr_end;
1512 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001513 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001514
Li Zefan33345d012011-04-20 10:31:50 +08001515 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001516 key.type = BTRFS_INODE_REF_KEY;
1517 key.offset = (u64)-1;
1518
Chris Masond3977122009-01-05 21:25:51 -05001519 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1521 if (ret < 0)
1522 break;
1523 if (ret > 0) {
1524 if (path->slots[0] == 0)
1525 break;
1526 path->slots[0]--;
1527 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001528process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001529 btrfs_item_key_to_cpu(path->nodes[0], &key,
1530 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001531 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001532 key.type != BTRFS_INODE_REF_KEY)
1533 break;
1534 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1535 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1536 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001537 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001538 struct btrfs_inode_ref *ref;
1539
1540 ref = (struct btrfs_inode_ref *)ptr;
1541 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1542 ref);
1543 ptr = (unsigned long)(ref + 1) + name_len;
1544 nlink++;
1545 }
1546
1547 if (key.offset == 0)
1548 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001549 if (path->slots[0] > 0) {
1550 path->slots[0]--;
1551 goto process_slot;
1552 }
Chris Masone02119d2008-09-05 16:13:11 -04001553 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001554 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001555 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001556 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001557
1558 return nlink;
1559}
1560
1561/*
1562 * There are a few corners where the link count of the file can't
1563 * be properly maintained during replay. So, instead of adding
1564 * lots of complexity to the log code, we just scan the backrefs
1565 * for any file that has been through replay.
1566 *
1567 * The scan will update the link count on the inode to reflect the
1568 * number of back refs found. If it goes down to zero, the iput
1569 * will free the inode.
1570 */
1571static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1572 struct btrfs_root *root,
1573 struct inode *inode)
1574{
1575 struct btrfs_path *path;
1576 int ret;
1577 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001578 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001579
1580 path = btrfs_alloc_path();
1581 if (!path)
1582 return -ENOMEM;
1583
Nikolay Borisovf329e312017-01-18 00:31:50 +02001584 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001585 if (ret < 0)
1586 goto out;
1587
1588 nlink = ret;
1589
Nikolay Borisov36283652017-01-18 00:31:49 +02001590 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001591 if (ret < 0)
1592 goto out;
1593
1594 nlink += ret;
1595
1596 ret = 0;
1597
Chris Masone02119d2008-09-05 16:13:11 -04001598 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001599 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001600 btrfs_update_inode(trans, root, inode);
1601 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001602 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001603
Yan, Zhengc71bf092009-11-12 09:34:40 +00001604 if (inode->i_nlink == 0) {
1605 if (S_ISDIR(inode->i_mode)) {
1606 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001607 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001608 if (ret)
1609 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001610 }
Li Zefan33345d012011-04-20 10:31:50 +08001611 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001612 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001613
Mark Fashehf1863732012-08-08 11:32:27 -07001614out:
1615 btrfs_free_path(path);
1616 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001617}
1618
1619static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1620 struct btrfs_root *root,
1621 struct btrfs_path *path)
1622{
1623 int ret;
1624 struct btrfs_key key;
1625 struct inode *inode;
1626
1627 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1628 key.type = BTRFS_ORPHAN_ITEM_KEY;
1629 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001630 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001631 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1632 if (ret < 0)
1633 break;
1634
1635 if (ret == 1) {
1636 if (path->slots[0] == 0)
1637 break;
1638 path->slots[0]--;
1639 }
1640
1641 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1642 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1643 key.type != BTRFS_ORPHAN_ITEM_KEY)
1644 break;
1645
1646 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001647 if (ret)
1648 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001649
David Sterbab3b4aa72011-04-21 01:20:15 +02001650 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001651 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001652 if (!inode)
1653 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001654
1655 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001656 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001657 if (ret)
1658 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001659
Chris Mason12fcfd22009-03-24 10:24:20 -04001660 /*
1661 * fixup on a directory may create new entries,
1662 * make sure we always look for the highset possible
1663 * offset
1664 */
1665 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001666 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001667 ret = 0;
1668out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001669 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001670 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001671}
1672
1673
1674/*
1675 * record a given inode in the fixup dir so we can check its link
1676 * count when replay is done. The link count is incremented here
1677 * so the inode won't go away until we check it
1678 */
1679static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1680 struct btrfs_root *root,
1681 struct btrfs_path *path,
1682 u64 objectid)
1683{
1684 struct btrfs_key key;
1685 int ret = 0;
1686 struct inode *inode;
1687
1688 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001689 if (!inode)
1690 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001691
1692 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001693 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001694 key.offset = objectid;
1695
1696 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1697
David Sterbab3b4aa72011-04-21 01:20:15 +02001698 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001699 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001700 if (!inode->i_nlink)
1701 set_nlink(inode, 1);
1702 else
Zach Brown8b558c52013-10-16 12:10:34 -07001703 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001704 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001705 } else if (ret == -EEXIST) {
1706 ret = 0;
1707 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001708 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001709 }
1710 iput(inode);
1711
1712 return ret;
1713}
1714
1715/*
1716 * when replaying the log for a directory, we only insert names
1717 * for inodes that actually exist. This means an fsync on a directory
1718 * does not implicitly fsync all the new files in it
1719 */
1720static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1721 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001722 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001723 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001724 struct btrfs_key *location)
1725{
1726 struct inode *inode;
1727 struct inode *dir;
1728 int ret;
1729
1730 inode = read_one_inode(root, location->objectid);
1731 if (!inode)
1732 return -ENOENT;
1733
1734 dir = read_one_inode(root, dirid);
1735 if (!dir) {
1736 iput(inode);
1737 return -EIO;
1738 }
Josef Bacikd5554382013-09-11 14:17:00 -04001739
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001740 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1741 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001742
1743 /* FIXME, put inode into FIXUP list */
1744
1745 iput(inode);
1746 iput(dir);
1747 return ret;
1748}
1749
1750/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001751 * Return true if an inode reference exists in the log for the given name,
1752 * inode and parent inode.
1753 */
1754static bool name_in_log_ref(struct btrfs_root *log_root,
1755 const char *name, const int name_len,
1756 const u64 dirid, const u64 ino)
1757{
1758 struct btrfs_key search_key;
1759
1760 search_key.objectid = ino;
1761 search_key.type = BTRFS_INODE_REF_KEY;
1762 search_key.offset = dirid;
1763 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1764 return true;
1765
1766 search_key.type = BTRFS_INODE_EXTREF_KEY;
1767 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1768 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1769 return true;
1770
1771 return false;
1772}
1773
1774/*
Chris Masone02119d2008-09-05 16:13:11 -04001775 * take a single entry in a log directory item and replay it into
1776 * the subvolume.
1777 *
1778 * if a conflicting item exists in the subdirectory already,
1779 * the inode it points to is unlinked and put into the link count
1780 * fix up tree.
1781 *
1782 * If a name from the log points to a file or directory that does
1783 * not exist in the FS, it is skipped. fsyncs on directories
1784 * do not force down inodes inside that directory, just changes to the
1785 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001786 *
1787 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1788 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001789 */
1790static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1791 struct btrfs_root *root,
1792 struct btrfs_path *path,
1793 struct extent_buffer *eb,
1794 struct btrfs_dir_item *di,
1795 struct btrfs_key *key)
1796{
1797 char *name;
1798 int name_len;
1799 struct btrfs_dir_item *dst_di;
1800 struct btrfs_key found_key;
1801 struct btrfs_key log_key;
1802 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001803 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001804 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001805 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001806 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001807 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001808
1809 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001810 if (!dir)
1811 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001812
1813 name_len = btrfs_dir_name_len(eb, di);
1814 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001815 if (!name) {
1816 ret = -ENOMEM;
1817 goto out;
1818 }
liubo2a29edc2011-01-26 06:22:08 +00001819
Chris Masone02119d2008-09-05 16:13:11 -04001820 log_type = btrfs_dir_type(eb, di);
1821 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1822 name_len);
1823
1824 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001825 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1826 if (exists == 0)
1827 exists = 1;
1828 else
1829 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001830 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001831
Chris Masone02119d2008-09-05 16:13:11 -04001832 if (key->type == BTRFS_DIR_ITEM_KEY) {
1833 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1834 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001835 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001836 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1837 key->objectid,
1838 key->offset, name,
1839 name_len, 1);
1840 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001841 /* Corruption */
1842 ret = -EINVAL;
1843 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001844 }
David Sterbac7040052011-04-19 18:00:01 +02001845 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001846 /* we need a sequence number to insert, so we only
1847 * do inserts for the BTRFS_DIR_INDEX_KEY types
1848 */
1849 if (key->type != BTRFS_DIR_INDEX_KEY)
1850 goto out;
1851 goto insert;
1852 }
1853
1854 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1855 /* the existing item matches the logged item */
1856 if (found_key.objectid == log_key.objectid &&
1857 found_key.type == log_key.type &&
1858 found_key.offset == log_key.offset &&
1859 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001860 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001861 goto out;
1862 }
1863
1864 /*
1865 * don't drop the conflicting directory entry if the inode
1866 * for the new entry doesn't exist
1867 */
Chris Mason4bef0842008-09-08 11:18:08 -04001868 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001869 goto out;
1870
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001871 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001872 if (ret)
1873 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001874
1875 if (key->type == BTRFS_DIR_INDEX_KEY)
1876 goto insert;
1877out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001878 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001879 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001880 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001881 ret = btrfs_update_inode(trans, root, dir);
1882 }
Chris Masone02119d2008-09-05 16:13:11 -04001883 kfree(name);
1884 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001885 if (!ret && name_added)
1886 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001887 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001888
1889insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001890 if (name_in_log_ref(root->log_root, name, name_len,
1891 key->objectid, log_key.objectid)) {
1892 /* The dentry will be added later. */
1893 ret = 0;
1894 update_size = false;
1895 goto out;
1896 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001897 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001898 ret = insert_one_name(trans, root, key->objectid, key->offset,
1899 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001900 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001901 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001902 if (!ret)
1903 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001904 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001905 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001906 goto out;
1907}
1908
1909/*
1910 * find all the names in a directory item and reconcile them into
1911 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1912 * one name in a directory item, but the same code gets used for
1913 * both directory index types
1914 */
1915static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1916 struct btrfs_root *root,
1917 struct btrfs_path *path,
1918 struct extent_buffer *eb, int slot,
1919 struct btrfs_key *key)
1920{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001921 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001922 u32 item_size = btrfs_item_size_nr(eb, slot);
1923 struct btrfs_dir_item *di;
1924 int name_len;
1925 unsigned long ptr;
1926 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001927 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001928
1929 ptr = btrfs_item_ptr_offset(eb, slot);
1930 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001931 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001932 di = (struct btrfs_dir_item *)ptr;
1933 name_len = btrfs_dir_name_len(eb, di);
1934 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001935 if (ret < 0)
1936 break;
Chris Masone02119d2008-09-05 16:13:11 -04001937 ptr = (unsigned long)(di + 1);
1938 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001939
1940 /*
1941 * If this entry refers to a non-directory (directories can not
1942 * have a link count > 1) and it was added in the transaction
1943 * that was not committed, make sure we fixup the link count of
1944 * the inode it the entry points to. Otherwise something like
1945 * the following would result in a directory pointing to an
1946 * inode with a wrong link that does not account for this dir
1947 * entry:
1948 *
1949 * mkdir testdir
1950 * touch testdir/foo
1951 * touch testdir/bar
1952 * sync
1953 *
1954 * ln testdir/bar testdir/bar_link
1955 * ln testdir/foo testdir/foo_link
1956 * xfs_io -c "fsync" testdir/bar
1957 *
1958 * <power failure>
1959 *
1960 * mount fs, log replay happens
1961 *
1962 * File foo would remain with a link count of 1 when it has two
1963 * entries pointing to it in the directory testdir. This would
1964 * make it impossible to ever delete the parent directory has
1965 * it would result in stale dentries that can never be deleted.
1966 */
1967 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1968 struct btrfs_key di_key;
1969
1970 if (!fixup_path) {
1971 fixup_path = btrfs_alloc_path();
1972 if (!fixup_path) {
1973 ret = -ENOMEM;
1974 break;
1975 }
1976 }
1977
1978 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1979 ret = link_to_fixup_dir(trans, root, fixup_path,
1980 di_key.objectid);
1981 if (ret)
1982 break;
1983 }
1984 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001985 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001986 btrfs_free_path(fixup_path);
1987 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001988}
1989
1990/*
1991 * directory replay has two parts. There are the standard directory
1992 * items in the log copied from the subvolume, and range items
1993 * created in the log while the subvolume was logged.
1994 *
1995 * The range items tell us which parts of the key space the log
1996 * is authoritative for. During replay, if a key in the subvolume
1997 * directory is in a logged range item, but not actually in the log
1998 * that means it was deleted from the directory before the fsync
1999 * and should be removed.
2000 */
2001static noinline int find_dir_range(struct btrfs_root *root,
2002 struct btrfs_path *path,
2003 u64 dirid, int key_type,
2004 u64 *start_ret, u64 *end_ret)
2005{
2006 struct btrfs_key key;
2007 u64 found_end;
2008 struct btrfs_dir_log_item *item;
2009 int ret;
2010 int nritems;
2011
2012 if (*start_ret == (u64)-1)
2013 return 1;
2014
2015 key.objectid = dirid;
2016 key.type = key_type;
2017 key.offset = *start_ret;
2018
2019 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2020 if (ret < 0)
2021 goto out;
2022 if (ret > 0) {
2023 if (path->slots[0] == 0)
2024 goto out;
2025 path->slots[0]--;
2026 }
2027 if (ret != 0)
2028 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2029
2030 if (key.type != key_type || key.objectid != dirid) {
2031 ret = 1;
2032 goto next;
2033 }
2034 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2035 struct btrfs_dir_log_item);
2036 found_end = btrfs_dir_log_end(path->nodes[0], item);
2037
2038 if (*start_ret >= key.offset && *start_ret <= found_end) {
2039 ret = 0;
2040 *start_ret = key.offset;
2041 *end_ret = found_end;
2042 goto out;
2043 }
2044 ret = 1;
2045next:
2046 /* check the next slot in the tree to see if it is a valid item */
2047 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002048 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002049 if (path->slots[0] >= nritems) {
2050 ret = btrfs_next_leaf(root, path);
2051 if (ret)
2052 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002053 }
2054
2055 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2056
2057 if (key.type != key_type || key.objectid != dirid) {
2058 ret = 1;
2059 goto out;
2060 }
2061 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2062 struct btrfs_dir_log_item);
2063 found_end = btrfs_dir_log_end(path->nodes[0], item);
2064 *start_ret = key.offset;
2065 *end_ret = found_end;
2066 ret = 0;
2067out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002068 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002069 return ret;
2070}
2071
2072/*
2073 * this looks for a given directory item in the log. If the directory
2074 * item is not in the log, the item is removed and the inode it points
2075 * to is unlinked
2076 */
2077static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2078 struct btrfs_root *root,
2079 struct btrfs_root *log,
2080 struct btrfs_path *path,
2081 struct btrfs_path *log_path,
2082 struct inode *dir,
2083 struct btrfs_key *dir_key)
2084{
2085 int ret;
2086 struct extent_buffer *eb;
2087 int slot;
2088 u32 item_size;
2089 struct btrfs_dir_item *di;
2090 struct btrfs_dir_item *log_di;
2091 int name_len;
2092 unsigned long ptr;
2093 unsigned long ptr_end;
2094 char *name;
2095 struct inode *inode;
2096 struct btrfs_key location;
2097
2098again:
2099 eb = path->nodes[0];
2100 slot = path->slots[0];
2101 item_size = btrfs_item_size_nr(eb, slot);
2102 ptr = btrfs_item_ptr_offset(eb, slot);
2103 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002104 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002105 di = (struct btrfs_dir_item *)ptr;
2106 name_len = btrfs_dir_name_len(eb, di);
2107 name = kmalloc(name_len, GFP_NOFS);
2108 if (!name) {
2109 ret = -ENOMEM;
2110 goto out;
2111 }
2112 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2113 name_len);
2114 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002115 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002116 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2117 dir_key->objectid,
2118 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002119 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002120 log_di = btrfs_lookup_dir_index_item(trans, log,
2121 log_path,
2122 dir_key->objectid,
2123 dir_key->offset,
2124 name, name_len, 0);
2125 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002126 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002127 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002128 btrfs_release_path(path);
2129 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002130 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002131 if (!inode) {
2132 kfree(name);
2133 return -EIO;
2134 }
Chris Masone02119d2008-09-05 16:13:11 -04002135
2136 ret = link_to_fixup_dir(trans, root,
2137 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002138 if (ret) {
2139 kfree(name);
2140 iput(inode);
2141 goto out;
2142 }
2143
Zach Brown8b558c52013-10-16 12:10:34 -07002144 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002145 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2146 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002147 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002148 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002149 kfree(name);
2150 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002151 if (ret)
2152 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002153
2154 /* there might still be more names under this key
2155 * check and repeat if required
2156 */
2157 ret = btrfs_search_slot(NULL, root, dir_key, path,
2158 0, 0);
2159 if (ret == 0)
2160 goto again;
2161 ret = 0;
2162 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002163 } else if (IS_ERR(log_di)) {
2164 kfree(name);
2165 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002166 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002167 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002168 kfree(name);
2169
2170 ptr = (unsigned long)(di + 1);
2171 ptr += name_len;
2172 }
2173 ret = 0;
2174out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002175 btrfs_release_path(path);
2176 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002177 return ret;
2178}
2179
Filipe Manana4f764e52015-02-23 19:53:35 +00002180static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2181 struct btrfs_root *root,
2182 struct btrfs_root *log,
2183 struct btrfs_path *path,
2184 const u64 ino)
2185{
2186 struct btrfs_key search_key;
2187 struct btrfs_path *log_path;
2188 int i;
2189 int nritems;
2190 int ret;
2191
2192 log_path = btrfs_alloc_path();
2193 if (!log_path)
2194 return -ENOMEM;
2195
2196 search_key.objectid = ino;
2197 search_key.type = BTRFS_XATTR_ITEM_KEY;
2198 search_key.offset = 0;
2199again:
2200 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2201 if (ret < 0)
2202 goto out;
2203process_leaf:
2204 nritems = btrfs_header_nritems(path->nodes[0]);
2205 for (i = path->slots[0]; i < nritems; i++) {
2206 struct btrfs_key key;
2207 struct btrfs_dir_item *di;
2208 struct btrfs_dir_item *log_di;
2209 u32 total_size;
2210 u32 cur;
2211
2212 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2213 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2214 ret = 0;
2215 goto out;
2216 }
2217
2218 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2219 total_size = btrfs_item_size_nr(path->nodes[0], i);
2220 cur = 0;
2221 while (cur < total_size) {
2222 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2223 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2224 u32 this_len = sizeof(*di) + name_len + data_len;
2225 char *name;
2226
2227 name = kmalloc(name_len, GFP_NOFS);
2228 if (!name) {
2229 ret = -ENOMEM;
2230 goto out;
2231 }
2232 read_extent_buffer(path->nodes[0], name,
2233 (unsigned long)(di + 1), name_len);
2234
2235 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2236 name, name_len, 0);
2237 btrfs_release_path(log_path);
2238 if (!log_di) {
2239 /* Doesn't exist in log tree, so delete it. */
2240 btrfs_release_path(path);
2241 di = btrfs_lookup_xattr(trans, root, path, ino,
2242 name, name_len, -1);
2243 kfree(name);
2244 if (IS_ERR(di)) {
2245 ret = PTR_ERR(di);
2246 goto out;
2247 }
2248 ASSERT(di);
2249 ret = btrfs_delete_one_dir_name(trans, root,
2250 path, di);
2251 if (ret)
2252 goto out;
2253 btrfs_release_path(path);
2254 search_key = key;
2255 goto again;
2256 }
2257 kfree(name);
2258 if (IS_ERR(log_di)) {
2259 ret = PTR_ERR(log_di);
2260 goto out;
2261 }
2262 cur += this_len;
2263 di = (struct btrfs_dir_item *)((char *)di + this_len);
2264 }
2265 }
2266 ret = btrfs_next_leaf(root, path);
2267 if (ret > 0)
2268 ret = 0;
2269 else if (ret == 0)
2270 goto process_leaf;
2271out:
2272 btrfs_free_path(log_path);
2273 btrfs_release_path(path);
2274 return ret;
2275}
2276
2277
Chris Masone02119d2008-09-05 16:13:11 -04002278/*
2279 * deletion replay happens before we copy any new directory items
2280 * out of the log or out of backreferences from inodes. It
2281 * scans the log to find ranges of keys that log is authoritative for,
2282 * and then scans the directory to find items in those ranges that are
2283 * not present in the log.
2284 *
2285 * Anything we don't find in the log is unlinked and removed from the
2286 * directory.
2287 */
2288static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2289 struct btrfs_root *root,
2290 struct btrfs_root *log,
2291 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002292 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002293{
2294 u64 range_start;
2295 u64 range_end;
2296 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2297 int ret = 0;
2298 struct btrfs_key dir_key;
2299 struct btrfs_key found_key;
2300 struct btrfs_path *log_path;
2301 struct inode *dir;
2302
2303 dir_key.objectid = dirid;
2304 dir_key.type = BTRFS_DIR_ITEM_KEY;
2305 log_path = btrfs_alloc_path();
2306 if (!log_path)
2307 return -ENOMEM;
2308
2309 dir = read_one_inode(root, dirid);
2310 /* it isn't an error if the inode isn't there, that can happen
2311 * because we replay the deletes before we copy in the inode item
2312 * from the log
2313 */
2314 if (!dir) {
2315 btrfs_free_path(log_path);
2316 return 0;
2317 }
2318again:
2319 range_start = 0;
2320 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002321 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002322 if (del_all)
2323 range_end = (u64)-1;
2324 else {
2325 ret = find_dir_range(log, path, dirid, key_type,
2326 &range_start, &range_end);
2327 if (ret != 0)
2328 break;
2329 }
Chris Masone02119d2008-09-05 16:13:11 -04002330
2331 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002332 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002333 int nritems;
2334 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2335 0, 0);
2336 if (ret < 0)
2337 goto out;
2338
2339 nritems = btrfs_header_nritems(path->nodes[0]);
2340 if (path->slots[0] >= nritems) {
2341 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002342 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002343 break;
Liu Bob98def72018-04-03 01:59:48 +08002344 else if (ret < 0)
2345 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002346 }
2347 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2348 path->slots[0]);
2349 if (found_key.objectid != dirid ||
2350 found_key.type != dir_key.type)
2351 goto next_type;
2352
2353 if (found_key.offset > range_end)
2354 break;
2355
2356 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002357 log_path, dir,
2358 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002359 if (ret)
2360 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002361 if (found_key.offset == (u64)-1)
2362 break;
2363 dir_key.offset = found_key.offset + 1;
2364 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002365 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002366 if (range_end == (u64)-1)
2367 break;
2368 range_start = range_end + 1;
2369 }
2370
2371next_type:
2372 ret = 0;
2373 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2374 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2375 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002376 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002377 goto again;
2378 }
2379out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002380 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002381 btrfs_free_path(log_path);
2382 iput(dir);
2383 return ret;
2384}
2385
2386/*
2387 * the process_func used to replay items from the log tree. This
2388 * gets called in two different stages. The first stage just looks
2389 * for inodes and makes sure they are all copied into the subvolume.
2390 *
2391 * The second stage copies all the other item types from the log into
2392 * the subvolume. The two stage approach is slower, but gets rid of
2393 * lots of complexity around inodes referencing other inodes that exist
2394 * only in the log (references come from either directory items or inode
2395 * back refs).
2396 */
2397static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002398 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002399{
2400 int nritems;
2401 struct btrfs_path *path;
2402 struct btrfs_root *root = wc->replay_dest;
2403 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002404 int i;
2405 int ret;
2406
Qu Wenruo581c1762018-03-29 09:08:11 +08002407 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002408 if (ret)
2409 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002410
2411 level = btrfs_header_level(eb);
2412
2413 if (level != 0)
2414 return 0;
2415
2416 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002417 if (!path)
2418 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002419
2420 nritems = btrfs_header_nritems(eb);
2421 for (i = 0; i < nritems; i++) {
2422 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002423
2424 /* inode keys are done during the first stage */
2425 if (key.type == BTRFS_INODE_ITEM_KEY &&
2426 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002427 struct btrfs_inode_item *inode_item;
2428 u32 mode;
2429
2430 inode_item = btrfs_item_ptr(eb, i,
2431 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002432 ret = replay_xattr_deletes(wc->trans, root, log,
2433 path, key.objectid);
2434 if (ret)
2435 break;
Chris Masone02119d2008-09-05 16:13:11 -04002436 mode = btrfs_inode_mode(eb, inode_item);
2437 if (S_ISDIR(mode)) {
2438 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002439 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002440 if (ret)
2441 break;
Chris Masone02119d2008-09-05 16:13:11 -04002442 }
2443 ret = overwrite_item(wc->trans, root, path,
2444 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002445 if (ret)
2446 break;
Chris Masone02119d2008-09-05 16:13:11 -04002447
Filipe Manana471d5572018-04-05 22:55:12 +01002448 /*
2449 * Before replaying extents, truncate the inode to its
2450 * size. We need to do it now and not after log replay
2451 * because before an fsync we can have prealloc extents
2452 * added beyond the inode's i_size. If we did it after,
2453 * through orphan cleanup for example, we would drop
2454 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002455 */
2456 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002457 struct inode *inode;
2458 u64 from;
2459
2460 inode = read_one_inode(root, key.objectid);
2461 if (!inode) {
2462 ret = -EIO;
2463 break;
2464 }
2465 from = ALIGN(i_size_read(inode),
2466 root->fs_info->sectorsize);
2467 ret = btrfs_drop_extents(wc->trans, root, inode,
2468 from, (u64)-1, 1);
2469 /*
2470 * If the nlink count is zero here, the iput
2471 * will free the inode. We bump it to make
2472 * sure it doesn't get freed until the link
2473 * count fixup is done.
2474 */
2475 if (!ret) {
2476 if (inode->i_nlink == 0)
2477 inc_nlink(inode);
2478 /* Update link count and nbytes. */
2479 ret = btrfs_update_inode(wc->trans,
2480 root, inode);
2481 }
2482 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002483 if (ret)
2484 break;
Chris Masone02119d2008-09-05 16:13:11 -04002485 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002486
Chris Masone02119d2008-09-05 16:13:11 -04002487 ret = link_to_fixup_dir(wc->trans, root,
2488 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002489 if (ret)
2490 break;
Chris Masone02119d2008-09-05 16:13:11 -04002491 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002492
2493 if (key.type == BTRFS_DIR_INDEX_KEY &&
2494 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2495 ret = replay_one_dir_item(wc->trans, root, path,
2496 eb, i, &key);
2497 if (ret)
2498 break;
2499 }
2500
Chris Masone02119d2008-09-05 16:13:11 -04002501 if (wc->stage < LOG_WALK_REPLAY_ALL)
2502 continue;
2503
2504 /* these keys are simply copied */
2505 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2506 ret = overwrite_item(wc->trans, root, path,
2507 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002508 if (ret)
2509 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002510 } else if (key.type == BTRFS_INODE_REF_KEY ||
2511 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002512 ret = add_inode_ref(wc->trans, root, log, path,
2513 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002514 if (ret && ret != -ENOENT)
2515 break;
2516 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002517 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2518 ret = replay_one_extent(wc->trans, root, path,
2519 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002520 if (ret)
2521 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002522 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002523 ret = replay_one_dir_item(wc->trans, root, path,
2524 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002525 if (ret)
2526 break;
Chris Masone02119d2008-09-05 16:13:11 -04002527 }
2528 }
2529 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002530 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002531}
2532
Chris Masond3977122009-01-05 21:25:51 -05002533static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002534 struct btrfs_root *root,
2535 struct btrfs_path *path, int *level,
2536 struct walk_control *wc)
2537{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002538 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002539 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002540 u64 bytenr;
2541 u64 ptr_gen;
2542 struct extent_buffer *next;
2543 struct extent_buffer *cur;
2544 struct extent_buffer *parent;
2545 u32 blocksize;
2546 int ret = 0;
2547
2548 WARN_ON(*level < 0);
2549 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2550
Chris Masond3977122009-01-05 21:25:51 -05002551 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002552 struct btrfs_key first_key;
2553
Chris Masone02119d2008-09-05 16:13:11 -04002554 WARN_ON(*level < 0);
2555 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2556 cur = path->nodes[*level];
2557
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302558 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002559
2560 if (path->slots[*level] >=
2561 btrfs_header_nritems(cur))
2562 break;
2563
2564 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2565 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002566 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002567 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002568
2569 parent = path->nodes[*level];
2570 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002571
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002572 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002573 if (IS_ERR(next))
2574 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002575
Chris Masone02119d2008-09-05 16:13:11 -04002576 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002577 ret = wc->process_func(root, next, wc, ptr_gen,
2578 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002579 if (ret) {
2580 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002581 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002582 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002583
Chris Masone02119d2008-09-05 16:13:11 -04002584 path->slots[*level]++;
2585 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002586 ret = btrfs_read_buffer(next, ptr_gen,
2587 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002588 if (ret) {
2589 free_extent_buffer(next);
2590 return ret;
2591 }
Chris Masone02119d2008-09-05 16:13:11 -04002592
Josef Bacik681ae502013-10-07 15:11:00 -04002593 if (trans) {
2594 btrfs_tree_lock(next);
2595 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002596 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002597 btrfs_wait_tree_block_writeback(next);
2598 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002599 } else {
2600 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2601 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002602 }
Chris Masone02119d2008-09-05 16:13:11 -04002603
Chris Masone02119d2008-09-05 16:13:11 -04002604 WARN_ON(root_owner !=
2605 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002606 ret = btrfs_free_and_pin_reserved_extent(
2607 fs_info, bytenr,
2608 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002609 if (ret) {
2610 free_extent_buffer(next);
2611 return ret;
2612 }
Chris Masone02119d2008-09-05 16:13:11 -04002613 }
2614 free_extent_buffer(next);
2615 continue;
2616 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002617 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002618 if (ret) {
2619 free_extent_buffer(next);
2620 return ret;
2621 }
Chris Masone02119d2008-09-05 16:13:11 -04002622
2623 WARN_ON(*level <= 0);
2624 if (path->nodes[*level-1])
2625 free_extent_buffer(path->nodes[*level-1]);
2626 path->nodes[*level-1] = next;
2627 *level = btrfs_header_level(next);
2628 path->slots[*level] = 0;
2629 cond_resched();
2630 }
2631 WARN_ON(*level < 0);
2632 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2633
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002634 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002635
2636 cond_resched();
2637 return 0;
2638}
2639
Chris Masond3977122009-01-05 21:25:51 -05002640static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002641 struct btrfs_root *root,
2642 struct btrfs_path *path, int *level,
2643 struct walk_control *wc)
2644{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002645 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002646 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002647 int i;
2648 int slot;
2649 int ret;
2650
Chris Masond3977122009-01-05 21:25:51 -05002651 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002652 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002653 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002654 path->slots[i]++;
2655 *level = i;
2656 WARN_ON(*level == 0);
2657 return 0;
2658 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002659 struct extent_buffer *parent;
2660 if (path->nodes[*level] == root->node)
2661 parent = path->nodes[*level];
2662 else
2663 parent = path->nodes[*level + 1];
2664
2665 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002666 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002667 btrfs_header_generation(path->nodes[*level]),
2668 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002669 if (ret)
2670 return ret;
2671
Chris Masone02119d2008-09-05 16:13:11 -04002672 if (wc->free) {
2673 struct extent_buffer *next;
2674
2675 next = path->nodes[*level];
2676
Josef Bacik681ae502013-10-07 15:11:00 -04002677 if (trans) {
2678 btrfs_tree_lock(next);
2679 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002680 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002681 btrfs_wait_tree_block_writeback(next);
2682 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002683 } else {
2684 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2685 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002686 }
Chris Masone02119d2008-09-05 16:13:11 -04002687
Chris Masone02119d2008-09-05 16:13:11 -04002688 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002689 ret = btrfs_free_and_pin_reserved_extent(
2690 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002691 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002692 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002693 if (ret)
2694 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002695 }
2696 free_extent_buffer(path->nodes[*level]);
2697 path->nodes[*level] = NULL;
2698 *level = i + 1;
2699 }
2700 }
2701 return 1;
2702}
2703
2704/*
2705 * drop the reference count on the tree rooted at 'snap'. This traverses
2706 * the tree freeing any blocks that have a ref count of zero after being
2707 * decremented.
2708 */
2709static int walk_log_tree(struct btrfs_trans_handle *trans,
2710 struct btrfs_root *log, struct walk_control *wc)
2711{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002712 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002713 int ret = 0;
2714 int wret;
2715 int level;
2716 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002717 int orig_level;
2718
2719 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002720 if (!path)
2721 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002722
2723 level = btrfs_header_level(log->node);
2724 orig_level = level;
2725 path->nodes[level] = log->node;
2726 extent_buffer_get(log->node);
2727 path->slots[level] = 0;
2728
Chris Masond3977122009-01-05 21:25:51 -05002729 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002730 wret = walk_down_log_tree(trans, log, path, &level, wc);
2731 if (wret > 0)
2732 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002733 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002734 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002735 goto out;
2736 }
Chris Masone02119d2008-09-05 16:13:11 -04002737
2738 wret = walk_up_log_tree(trans, log, path, &level, wc);
2739 if (wret > 0)
2740 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002741 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002742 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002743 goto out;
2744 }
Chris Masone02119d2008-09-05 16:13:11 -04002745 }
2746
2747 /* was the root node processed? if not, catch it here */
2748 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002749 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002750 btrfs_header_generation(path->nodes[orig_level]),
2751 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002752 if (ret)
2753 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002754 if (wc->free) {
2755 struct extent_buffer *next;
2756
2757 next = path->nodes[orig_level];
2758
Josef Bacik681ae502013-10-07 15:11:00 -04002759 if (trans) {
2760 btrfs_tree_lock(next);
2761 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002762 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002763 btrfs_wait_tree_block_writeback(next);
2764 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002765 } else {
2766 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2767 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002768 }
Chris Masone02119d2008-09-05 16:13:11 -04002769
Chris Masone02119d2008-09-05 16:13:11 -04002770 WARN_ON(log->root_key.objectid !=
2771 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002772 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2773 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002774 if (ret)
2775 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002776 }
2777 }
2778
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002779out:
Chris Masone02119d2008-09-05 16:13:11 -04002780 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002781 return ret;
2782}
2783
Yan Zheng7237f182009-01-21 12:54:03 -05002784/*
2785 * helper function to update the item for a given subvolumes log root
2786 * in the tree of log roots
2787 */
2788static int update_log_root(struct btrfs_trans_handle *trans,
2789 struct btrfs_root *log)
2790{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002791 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002792 int ret;
2793
2794 if (log->log_transid == 1) {
2795 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002796 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002797 &log->root_key, &log->root_item);
2798 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002799 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002800 &log->root_key, &log->root_item);
2801 }
2802 return ret;
2803}
2804
Zhaolei60d53eb2015-08-17 18:44:46 +08002805static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002806{
2807 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002808 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002809
Yan Zheng7237f182009-01-21 12:54:03 -05002810 /*
2811 * we only allow two pending log transactions at a time,
2812 * so we know that if ours is more than 2 older than the
2813 * current transaction, we're done
2814 */
Liu Bo49e83f52017-09-01 16:14:30 -06002815 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002816 prepare_to_wait(&root->log_commit_wait[index],
2817 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002818
2819 if (!(root->log_transid_committed < transid &&
2820 atomic_read(&root->log_commit[index])))
2821 break;
2822
Yan Zheng7237f182009-01-21 12:54:03 -05002823 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002824 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002825 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002826 }
2827 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002828}
2829
Zhaolei60d53eb2015-08-17 18:44:46 +08002830static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002831{
2832 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002833
Liu Bo49e83f52017-09-01 16:14:30 -06002834 for (;;) {
2835 prepare_to_wait(&root->log_writer_wait, &wait,
2836 TASK_UNINTERRUPTIBLE);
2837 if (!atomic_read(&root->log_writers))
2838 break;
2839
Yan Zheng7237f182009-01-21 12:54:03 -05002840 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002841 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002842 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002843 }
Liu Bo49e83f52017-09-01 16:14:30 -06002844 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002845}
2846
Miao Xie8b050d32014-02-20 18:08:58 +08002847static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2848 struct btrfs_log_ctx *ctx)
2849{
2850 if (!ctx)
2851 return;
2852
2853 mutex_lock(&root->log_mutex);
2854 list_del_init(&ctx->list);
2855 mutex_unlock(&root->log_mutex);
2856}
2857
2858/*
2859 * Invoked in log mutex context, or be sure there is no other task which
2860 * can access the list.
2861 */
2862static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2863 int index, int error)
2864{
2865 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002866 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002867
Chris Mason570dd452016-10-27 10:42:20 -07002868 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2869 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002870 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002871 }
Miao Xie8b050d32014-02-20 18:08:58 +08002872
2873 INIT_LIST_HEAD(&root->log_ctxs[index]);
2874}
2875
Chris Masone02119d2008-09-05 16:13:11 -04002876/*
2877 * btrfs_sync_log does sends a given tree log down to the disk and
2878 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002879 * you know that any inodes previously logged are safely on disk only
2880 * if it returns 0.
2881 *
2882 * Any other return value means you need to call btrfs_commit_transaction.
2883 * Some of the edge cases for fsyncing directories that have had unlinks
2884 * or renames done in the past mean that sometimes the only safe
2885 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2886 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002887 */
2888int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002889 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002890{
Yan Zheng7237f182009-01-21 12:54:03 -05002891 int index1;
2892 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002893 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002894 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002895 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002896 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002897 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002898 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002899 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002900 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002901
Yan Zheng7237f182009-01-21 12:54:03 -05002902 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002903 log_transid = ctx->log_transid;
2904 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002905 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002906 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002907 }
Miao Xied1433de2014-02-20 18:08:59 +08002908
2909 index1 = log_transid % 2;
2910 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002911 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002912 mutex_unlock(&root->log_mutex);
2913 return ctx->log_ret;
2914 }
2915 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002916 atomic_set(&root->log_commit[index1], 1);
2917
2918 /* wait for previous tree log sync to complete */
2919 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002920 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002921
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002922 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002923 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002924 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002926 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002927 mutex_unlock(&root->log_mutex);
2928 schedule_timeout_uninterruptible(1);
2929 mutex_lock(&root->log_mutex);
2930 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002931 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002932 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002933 break;
2934 }
Chris Masond0c803c2008-09-11 16:17:57 -04002935
Chris Mason12fcfd22009-03-24 10:24:20 -04002936 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002937 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002938 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002939 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002940 mutex_unlock(&root->log_mutex);
2941 goto out;
2942 }
2943
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002944 if (log_transid % 2 == 0)
2945 mark = EXTENT_DIRTY;
2946 else
2947 mark = EXTENT_NEW;
2948
Chris Mason690587d2009-10-13 13:29:19 -04002949 /* we start IO on all the marked extents here, but we don't actually
2950 * wait for them until later.
2951 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002952 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002953 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002954 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002955 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002956 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002957 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002958 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002959 mutex_unlock(&root->log_mutex);
2960 goto out;
2961 }
Yan Zheng7237f182009-01-21 12:54:03 -05002962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002963 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002964
Yan Zheng7237f182009-01-21 12:54:03 -05002965 root->log_transid++;
2966 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002967 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002968 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002969 * IO has been started, blocks of the log tree have WRITTEN flag set
2970 * in their headers. new modifications of the log will be written to
2971 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002972 */
2973 mutex_unlock(&root->log_mutex);
2974
Filipe Manana28a23592016-08-23 21:13:51 +01002975 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002976
Yan Zheng7237f182009-01-21 12:54:03 -05002977 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002978 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002979 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002980
2981 index2 = log_root_tree->log_transid % 2;
2982 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2983 root_log_ctx.log_transid = log_root_tree->log_transid;
2984
Yan Zheng7237f182009-01-21 12:54:03 -05002985 mutex_unlock(&log_root_tree->log_mutex);
2986
2987 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002988
2989 mutex_lock(&log_root_tree->log_mutex);
2990 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002991 /*
2992 * Implicit memory barrier after atomic_dec_and_test
2993 */
Yan Zheng7237f182009-01-21 12:54:03 -05002994 if (waitqueue_active(&log_root_tree->log_writer_wait))
2995 wake_up(&log_root_tree->log_writer_wait);
2996 }
2997
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002998 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002999 if (!list_empty(&root_log_ctx.list))
3000 list_del_init(&root_log_ctx.list);
3001
Miao Xiec6adc9c2013-05-28 10:05:39 +00003002 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08003004
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003005 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003006 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003007 mutex_unlock(&log_root_tree->log_mutex);
3008 goto out;
3009 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003010 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003011 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003012 mutex_unlock(&log_root_tree->log_mutex);
3013 ret = -EAGAIN;
3014 goto out;
3015 }
3016
Miao Xied1433de2014-02-20 18:08:59 +08003017 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003018 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003019 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003020 mutex_unlock(&log_root_tree->log_mutex);
3021 ret = root_log_ctx.log_ret;
3022 goto out;
3023 }
Miao Xie8b050d32014-02-20 18:08:58 +08003024
Miao Xied1433de2014-02-20 18:08:59 +08003025 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003026 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003027 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003028 ret = btrfs_wait_tree_log_extents(log, mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05003029 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08003030 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003031 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003032 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003033 if (!ret)
3034 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003035 goto out;
3036 }
Miao Xied1433de2014-02-20 18:08:59 +08003037 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003038 atomic_set(&log_root_tree->log_commit[index2], 1);
3039
Chris Mason12fcfd22009-03-24 10:24:20 -04003040 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003041 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003042 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003043 }
Yan Zheng7237f182009-01-21 12:54:03 -05003044
Zhaolei60d53eb2015-08-17 18:44:46 +08003045 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003046
3047 /*
3048 * now that we've moved on to the tree of log tree roots,
3049 * check the full commit flag again
3050 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003051 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003052 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003053 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003054 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04003055 mutex_unlock(&log_root_tree->log_mutex);
3056 ret = -EAGAIN;
3057 goto out_wake_log_root;
3058 }
Yan Zheng7237f182009-01-21 12:54:03 -05003059
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003060 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003061 &log_root_tree->dirty_log_pages,
3062 EXTENT_DIRTY | EXTENT_NEW);
3063 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003064 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003065 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003066 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003067 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003068 mutex_unlock(&log_root_tree->log_mutex);
3069 goto out_wake_log_root;
3070 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003071 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003072 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003073 ret = btrfs_wait_tree_log_extents(log_root_tree,
3074 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003075 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003077 btrfs_free_logged_extents(log, log_transid);
3078 mutex_unlock(&log_root_tree->log_mutex);
3079 goto out_wake_log_root;
3080 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05003081 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003082
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003083 btrfs_set_super_log_root(fs_info->super_for_commit,
3084 log_root_tree->node->start);
3085 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3086 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003087
Yan Zheng7237f182009-01-21 12:54:03 -05003088 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003089 mutex_unlock(&log_root_tree->log_mutex);
3090
3091 /*
3092 * nobody else is going to jump in and write the the ctree
3093 * super here because the log_commit atomic below is protecting
3094 * us. We must be called with a transaction handle pinning
3095 * the running transaction open, so a full commit can't hop
3096 * in and cause problems either.
3097 */
David Sterbaeece6a92017-02-10 19:04:32 +01003098 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003099 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003100 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003101 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003102 goto out_wake_log_root;
3103 }
Yan Zheng7237f182009-01-21 12:54:03 -05003104
Chris Mason257c62e2009-10-13 13:21:08 -04003105 mutex_lock(&root->log_mutex);
3106 if (root->last_log_commit < log_transid)
3107 root->last_log_commit = log_transid;
3108 mutex_unlock(&root->log_mutex);
3109
Chris Mason12fcfd22009-03-24 10:24:20 -04003110out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003111 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003112 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3113
Miao Xied1433de2014-02-20 18:08:59 +08003114 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003115 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003116 mutex_unlock(&log_root_tree->log_mutex);
3117
David Sterba33a9eca2015-10-10 18:35:10 +02003118 /*
David Sterba3d3a2e62018-04-24 14:53:56 +02003119 * The barrier before waitqueue_active is needed so all the updates
3120 * above are seen by the woken threads. It might not be necessary, but
3121 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003122 */
David Sterba3d3a2e62018-04-24 14:53:56 +02003123 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05003124 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
3125 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003126out:
Miao Xied1433de2014-02-20 18:08:59 +08003127 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003128 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003129 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003130 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003131 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003132
David Sterba33a9eca2015-10-10 18:35:10 +02003133 /*
David Sterba3d3a2e62018-04-24 14:53:56 +02003134 * The barrier before waitqueue_active is needed so all the updates
3135 * above are seen by the woken threads. It might not be necessary, but
3136 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003137 */
David Sterba3d3a2e62018-04-24 14:53:56 +02003138 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05003139 if (waitqueue_active(&root->log_commit_wait[index1]))
3140 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003141 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003142}
3143
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003144static void free_log_tree(struct btrfs_trans_handle *trans,
3145 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003146{
3147 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003148 u64 start;
3149 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003150 struct walk_control wc = {
3151 .free = 1,
3152 .process_func = process_one_buffer
3153 };
3154
Josef Bacik681ae502013-10-07 15:11:00 -04003155 ret = walk_log_tree(trans, log, &wc);
3156 /* I don't think this can happen but just in case */
3157 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003158 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003159
Chris Masond3977122009-01-05 21:25:51 -05003160 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003161 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bo55237a52018-01-25 11:02:52 -07003162 0, &start, &end,
3163 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003164 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003165 if (ret)
3166 break;
3167
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003168 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bo55237a52018-01-25 11:02:52 -07003169 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003170 }
3171
Josef Bacik2ab28f32012-10-12 15:27:49 -04003172 /*
3173 * We may have short-circuited the log tree with the full commit logic
3174 * and left ordered extents on our list, so clear these out to keep us
3175 * from leaking inodes and memory.
3176 */
3177 btrfs_free_logged_extents(log, 0);
3178 btrfs_free_logged_extents(log, 1);
3179
Yan Zheng7237f182009-01-21 12:54:03 -05003180 free_extent_buffer(log->node);
3181 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003182}
3183
3184/*
3185 * free all the extents used by the tree log. This should be called
3186 * at commit time of the full transaction
3187 */
3188int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3189{
3190 if (root->log_root) {
3191 free_log_tree(trans, root->log_root);
3192 root->log_root = NULL;
3193 }
3194 return 0;
3195}
3196
3197int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3198 struct btrfs_fs_info *fs_info)
3199{
3200 if (fs_info->log_root_tree) {
3201 free_log_tree(trans, fs_info->log_root_tree);
3202 fs_info->log_root_tree = NULL;
3203 }
Chris Masone02119d2008-09-05 16:13:11 -04003204 return 0;
3205}
3206
3207/*
Chris Masone02119d2008-09-05 16:13:11 -04003208 * If both a file and directory are logged, and unlinks or renames are
3209 * mixed in, we have a few interesting corners:
3210 *
3211 * create file X in dir Y
3212 * link file X to X.link in dir Y
3213 * fsync file X
3214 * unlink file X but leave X.link
3215 * fsync dir Y
3216 *
3217 * After a crash we would expect only X.link to exist. But file X
3218 * didn't get fsync'd again so the log has back refs for X and X.link.
3219 *
3220 * We solve this by removing directory entries and inode backrefs from the
3221 * log when a file that was logged in the current transaction is
3222 * unlinked. Any later fsync will include the updated log entries, and
3223 * we'll be able to reconstruct the proper directory items from backrefs.
3224 *
3225 * This optimizations allows us to avoid relogging the entire inode
3226 * or the entire directory.
3227 */
3228int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3229 struct btrfs_root *root,
3230 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003231 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003232{
3233 struct btrfs_root *log;
3234 struct btrfs_dir_item *di;
3235 struct btrfs_path *path;
3236 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003237 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003238 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003239 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003240
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003241 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003242 return 0;
3243
Chris Masone02119d2008-09-05 16:13:11 -04003244 ret = join_running_log_trans(root);
3245 if (ret)
3246 return 0;
3247
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003248 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003249
3250 log = root->log_root;
3251 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003252 if (!path) {
3253 err = -ENOMEM;
3254 goto out_unlock;
3255 }
liubo2a29edc2011-01-26 06:22:08 +00003256
Li Zefan33345d012011-04-20 10:31:50 +08003257 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003258 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003259 if (IS_ERR(di)) {
3260 err = PTR_ERR(di);
3261 goto fail;
3262 }
3263 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003264 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3265 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003266 if (ret) {
3267 err = ret;
3268 goto fail;
3269 }
Chris Masone02119d2008-09-05 16:13:11 -04003270 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003271 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003272 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003273 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003274 if (IS_ERR(di)) {
3275 err = PTR_ERR(di);
3276 goto fail;
3277 }
3278 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003279 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3280 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003281 if (ret) {
3282 err = ret;
3283 goto fail;
3284 }
Chris Masone02119d2008-09-05 16:13:11 -04003285 }
3286
3287 /* update the directory size in the log to reflect the names
3288 * we have removed
3289 */
3290 if (bytes_del) {
3291 struct btrfs_key key;
3292
Li Zefan33345d012011-04-20 10:31:50 +08003293 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003294 key.offset = 0;
3295 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003296 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003297
3298 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003299 if (ret < 0) {
3300 err = ret;
3301 goto fail;
3302 }
Chris Masone02119d2008-09-05 16:13:11 -04003303 if (ret == 0) {
3304 struct btrfs_inode_item *item;
3305 u64 i_size;
3306
3307 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3308 struct btrfs_inode_item);
3309 i_size = btrfs_inode_size(path->nodes[0], item);
3310 if (i_size > bytes_del)
3311 i_size -= bytes_del;
3312 else
3313 i_size = 0;
3314 btrfs_set_inode_size(path->nodes[0], item, i_size);
3315 btrfs_mark_buffer_dirty(path->nodes[0]);
3316 } else
3317 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003318 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003319 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003320fail:
Chris Masone02119d2008-09-05 16:13:11 -04003321 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003322out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003323 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003324 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003325 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003326 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003327 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003328 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003329
Chris Mason12fcfd22009-03-24 10:24:20 -04003330 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003331
Andi Kleen411fc6b2010-10-29 15:14:31 -04003332 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003333}
3334
3335/* see comments for btrfs_del_dir_entries_in_log */
3336int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3337 struct btrfs_root *root,
3338 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003339 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003340{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003341 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003342 struct btrfs_root *log;
3343 u64 index;
3344 int ret;
3345
Nikolay Borisova491abb2017-01-18 00:31:33 +02003346 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003347 return 0;
3348
Chris Masone02119d2008-09-05 16:13:11 -04003349 ret = join_running_log_trans(root);
3350 if (ret)
3351 return 0;
3352 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003353 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003354
Nikolay Borisova491abb2017-01-18 00:31:33 +02003355 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003356 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003357 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003358 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003359 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003360 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003361 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003362 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003363 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003364
Chris Masone02119d2008-09-05 16:13:11 -04003365 return ret;
3366}
3367
3368/*
3369 * creates a range item in the log for 'dirid'. first_offset and
3370 * last_offset tell us which parts of the key space the log should
3371 * be considered authoritative for.
3372 */
3373static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3374 struct btrfs_root *log,
3375 struct btrfs_path *path,
3376 int key_type, u64 dirid,
3377 u64 first_offset, u64 last_offset)
3378{
3379 int ret;
3380 struct btrfs_key key;
3381 struct btrfs_dir_log_item *item;
3382
3383 key.objectid = dirid;
3384 key.offset = first_offset;
3385 if (key_type == BTRFS_DIR_ITEM_KEY)
3386 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3387 else
3388 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3389 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003390 if (ret)
3391 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003392
3393 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3394 struct btrfs_dir_log_item);
3395 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3396 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003397 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003398 return 0;
3399}
3400
3401/*
3402 * log all the items included in the current transaction for a given
3403 * directory. This also creates the range items in the log tree required
3404 * to replay anything deleted before the fsync
3405 */
3406static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003407 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003408 struct btrfs_path *path,
3409 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003410 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003411 u64 min_offset, u64 *last_offset_ret)
3412{
3413 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003414 struct btrfs_root *log = root->log_root;
3415 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003416 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003417 int ret;
3418 int i;
3419 int nritems;
3420 u64 first_offset = min_offset;
3421 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003422 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003423
3424 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003425
Li Zefan33345d012011-04-20 10:31:50 +08003426 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003427 min_key.type = key_type;
3428 min_key.offset = min_offset;
3429
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003430 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003431
3432 /*
3433 * we didn't find anything from this transaction, see if there
3434 * is anything at all
3435 */
Li Zefan33345d012011-04-20 10:31:50 +08003436 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3437 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003438 min_key.type = key_type;
3439 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003440 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003441 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3442 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003443 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003444 return ret;
3445 }
Li Zefan33345d012011-04-20 10:31:50 +08003446 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003447
3448 /* if ret == 0 there are items for this type,
3449 * create a range to tell us the last key of this type.
3450 * otherwise, there are no items in this directory after
3451 * *min_offset, and we create a range to indicate that.
3452 */
3453 if (ret == 0) {
3454 struct btrfs_key tmp;
3455 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3456 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003457 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003458 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003459 }
3460 goto done;
3461 }
3462
3463 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003464 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003465 if (ret == 0) {
3466 struct btrfs_key tmp;
3467 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3468 if (key_type == tmp.type) {
3469 first_offset = tmp.offset;
3470 ret = overwrite_item(trans, log, dst_path,
3471 path->nodes[0], path->slots[0],
3472 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003473 if (ret) {
3474 err = ret;
3475 goto done;
3476 }
Chris Masone02119d2008-09-05 16:13:11 -04003477 }
3478 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003479 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003480
3481 /* find the first key from this transaction again */
3482 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303483 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003484 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003485
3486 /*
3487 * we have a block from this transaction, log every item in it
3488 * from our directory
3489 */
Chris Masond3977122009-01-05 21:25:51 -05003490 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003491 struct btrfs_key tmp;
3492 src = path->nodes[0];
3493 nritems = btrfs_header_nritems(src);
3494 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003495 struct btrfs_dir_item *di;
3496
Chris Masone02119d2008-09-05 16:13:11 -04003497 btrfs_item_key_to_cpu(src, &min_key, i);
3498
Li Zefan33345d012011-04-20 10:31:50 +08003499 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003500 goto done;
3501 ret = overwrite_item(trans, log, dst_path, src, i,
3502 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003503 if (ret) {
3504 err = ret;
3505 goto done;
3506 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003507
3508 /*
3509 * We must make sure that when we log a directory entry,
3510 * the corresponding inode, after log replay, has a
3511 * matching link count. For example:
3512 *
3513 * touch foo
3514 * mkdir mydir
3515 * sync
3516 * ln foo mydir/bar
3517 * xfs_io -c "fsync" mydir
3518 * <crash>
3519 * <mount fs and log replay>
3520 *
3521 * Would result in a fsync log that when replayed, our
3522 * file inode would have a link count of 1, but we get
3523 * two directory entries pointing to the same inode.
3524 * After removing one of the names, it would not be
3525 * possible to remove the other name, which resulted
3526 * always in stale file handle errors, and would not
3527 * be possible to rmdir the parent directory, since
3528 * its i_size could never decrement to the value
3529 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3530 */
3531 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3532 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3533 if (ctx &&
3534 (btrfs_dir_transid(src, di) == trans->transid ||
3535 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3536 tmp.type != BTRFS_ROOT_ITEM_KEY)
3537 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003538 }
3539 path->slots[0] = nritems;
3540
3541 /*
3542 * look ahead to the next item and see if it is also
3543 * from this directory and from this transaction
3544 */
3545 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003546 if (ret) {
3547 if (ret == 1)
3548 last_offset = (u64)-1;
3549 else
3550 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003551 goto done;
3552 }
3553 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003554 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003555 last_offset = (u64)-1;
3556 goto done;
3557 }
3558 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3559 ret = overwrite_item(trans, log, dst_path,
3560 path->nodes[0], path->slots[0],
3561 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003562 if (ret)
3563 err = ret;
3564 else
3565 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003566 goto done;
3567 }
3568 }
3569done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003570 btrfs_release_path(path);
3571 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003572
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003573 if (err == 0) {
3574 *last_offset_ret = last_offset;
3575 /*
3576 * insert the log range keys to indicate where the log
3577 * is valid
3578 */
3579 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003580 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003581 if (ret)
3582 err = ret;
3583 }
3584 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003585}
3586
3587/*
3588 * logging directories is very similar to logging inodes, We find all the items
3589 * from the current transaction and write them to the log.
3590 *
3591 * The recovery code scans the directory in the subvolume, and if it finds a
3592 * key in the range logged that is not present in the log tree, then it means
3593 * that dir entry was unlinked during the transaction.
3594 *
3595 * In order for that scan to work, we must include one key smaller than
3596 * the smallest logged by this transaction and one key larger than the largest
3597 * key logged by this transaction.
3598 */
3599static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003600 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003601 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003602 struct btrfs_path *dst_path,
3603 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003604{
3605 u64 min_key;
3606 u64 max_key;
3607 int ret;
3608 int key_type = BTRFS_DIR_ITEM_KEY;
3609
3610again:
3611 min_key = 0;
3612 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003613 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003614 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3615 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003616 if (ret)
3617 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003618 if (max_key == (u64)-1)
3619 break;
3620 min_key = max_key + 1;
3621 }
3622
3623 if (key_type == BTRFS_DIR_ITEM_KEY) {
3624 key_type = BTRFS_DIR_INDEX_KEY;
3625 goto again;
3626 }
3627 return 0;
3628}
3629
3630/*
3631 * a helper function to drop items from the log before we relog an
3632 * inode. max_key_type indicates the highest item type to remove.
3633 * This cannot be run for file data extents because it does not
3634 * free the extents they point to.
3635 */
3636static int drop_objectid_items(struct btrfs_trans_handle *trans,
3637 struct btrfs_root *log,
3638 struct btrfs_path *path,
3639 u64 objectid, int max_key_type)
3640{
3641 int ret;
3642 struct btrfs_key key;
3643 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003644 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003645
3646 key.objectid = objectid;
3647 key.type = max_key_type;
3648 key.offset = (u64)-1;
3649
Chris Masond3977122009-01-05 21:25:51 -05003650 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003651 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003652 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003653 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003654 break;
3655
3656 if (path->slots[0] == 0)
3657 break;
3658
3659 path->slots[0]--;
3660 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3661 path->slots[0]);
3662
3663 if (found_key.objectid != objectid)
3664 break;
3665
Josef Bacik18ec90d2012-09-28 11:56:28 -04003666 found_key.offset = 0;
3667 found_key.type = 0;
3668 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3669 &start_slot);
3670
3671 ret = btrfs_del_items(trans, log, path, start_slot,
3672 path->slots[0] - start_slot + 1);
3673 /*
3674 * If start slot isn't 0 then we don't need to re-search, we've
3675 * found the last guy with the objectid in this tree.
3676 */
3677 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003678 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003679 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003680 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003681 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003682 if (ret > 0)
3683 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003684 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003685}
3686
Josef Bacik94edf4a2012-09-25 14:56:25 -04003687static void fill_inode_item(struct btrfs_trans_handle *trans,
3688 struct extent_buffer *leaf,
3689 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003690 struct inode *inode, int log_inode_only,
3691 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003692{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003693 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003694
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003695 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003696
3697 if (log_inode_only) {
3698 /* set the generation to zero so the recover code
3699 * can tell the difference between an logging
3700 * just to say 'this inode exists' and a logging
3701 * to say 'update this inode with these values'
3702 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003703 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003704 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003705 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003706 btrfs_set_token_inode_generation(leaf, item,
3707 BTRFS_I(inode)->generation,
3708 &token);
3709 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003710 }
3711
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003712 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3713 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3714 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3715 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3716
David Sterbaa937b972014-12-12 17:39:12 +01003717 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003718 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003719 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003720 inode->i_atime.tv_nsec, &token);
3721
David Sterbaa937b972014-12-12 17:39:12 +01003722 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003723 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003724 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003725 inode->i_mtime.tv_nsec, &token);
3726
David Sterbaa937b972014-12-12 17:39:12 +01003727 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003728 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003729 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003730 inode->i_ctime.tv_nsec, &token);
3731
3732 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3733 &token);
3734
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003735 btrfs_set_token_inode_sequence(leaf, item,
3736 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003737 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3738 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3739 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3740 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003741}
3742
Josef Bacika95249b2012-10-11 16:17:34 -04003743static int log_inode_item(struct btrfs_trans_handle *trans,
3744 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003745 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003746{
3747 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003748 int ret;
3749
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003750 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003751 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003752 if (ret && ret != -EEXIST)
3753 return ret;
3754 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3755 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003756 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3757 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003758 btrfs_release_path(path);
3759 return 0;
3760}
3761
Chris Mason31ff1cd2008-09-11 16:17:57 -04003762static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003763 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003764 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003765 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003766 int start_slot, int nr, int inode_only,
3767 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003768{
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003769 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003770 unsigned long src_offset;
3771 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003772 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003773 struct btrfs_file_extent_item *extent;
3774 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003775 struct extent_buffer *src = src_path->nodes[0];
3776 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003777 int ret;
3778 struct btrfs_key *ins_keys;
3779 u32 *ins_sizes;
3780 char *ins_data;
3781 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003782 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003783 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003784 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003785 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003786 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003787
3788 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003789
3790 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3791 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003792 if (!ins_data)
3793 return -ENOMEM;
3794
Josef Bacik16e75492013-10-22 12:18:51 -04003795 first_key.objectid = (u64)-1;
3796
Chris Mason31ff1cd2008-09-11 16:17:57 -04003797 ins_sizes = (u32 *)ins_data;
3798 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3799
3800 for (i = 0; i < nr; i++) {
3801 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3802 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3803 }
3804 ret = btrfs_insert_empty_items(trans, log, dst_path,
3805 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003806 if (ret) {
3807 kfree(ins_data);
3808 return ret;
3809 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003810
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003811 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003812 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3813 dst_path->slots[0]);
3814
3815 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3816
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003817 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003818 last_key = ins_keys[i];
3819
Josef Bacik94edf4a2012-09-25 14:56:25 -04003820 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003821 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3822 dst_path->slots[0],
3823 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003824 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003825 &inode->vfs_inode,
3826 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003827 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003828 } else {
3829 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3830 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003831 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003832
Josef Bacik16e75492013-10-22 12:18:51 -04003833 /*
3834 * We set need_find_last_extent here in case we know we were
3835 * processing other items and then walk into the first extent in
3836 * the inode. If we don't hit an extent then nothing changes,
3837 * we'll do the last search the next time around.
3838 */
3839 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3840 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003841 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003842 first_key = ins_keys[i];
3843 } else {
3844 need_find_last_extent = false;
3845 }
3846
Chris Mason31ff1cd2008-09-11 16:17:57 -04003847 /* take a reference on file data extents so that truncates
3848 * or deletes of this inode don't have to relog the inode
3849 * again
3850 */
David Sterba962a2982014-06-04 18:41:45 +02003851 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003852 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003853 int found_type;
3854 extent = btrfs_item_ptr(src, start_slot + i,
3855 struct btrfs_file_extent_item);
3856
liubo8e531cd2011-05-06 10:36:09 +08003857 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3858 continue;
3859
Chris Mason31ff1cd2008-09-11 16:17:57 -04003860 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003861 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003862 u64 ds, dl, cs, cl;
3863 ds = btrfs_file_extent_disk_bytenr(src,
3864 extent);
3865 /* ds == 0 is a hole */
3866 if (ds == 0)
3867 continue;
3868
3869 dl = btrfs_file_extent_disk_num_bytes(src,
3870 extent);
3871 cs = btrfs_file_extent_offset(src, extent);
3872 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003873 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003874 if (btrfs_file_extent_compression(src,
3875 extent)) {
3876 cs = 0;
3877 cl = dl;
3878 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003879
3880 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003881 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003882 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003883 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003884 if (ret) {
3885 btrfs_release_path(dst_path);
3886 kfree(ins_data);
3887 return ret;
3888 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003889 }
3890 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003891 }
3892
3893 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003894 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003895 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003896
3897 /*
3898 * we have to do this after the loop above to avoid changing the
3899 * log tree while trying to change the log tree.
3900 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003901 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003902 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003903 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3904 struct btrfs_ordered_sum,
3905 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003906 if (!ret)
3907 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003908 list_del(&sums->list);
3909 kfree(sums);
3910 }
Josef Bacik16e75492013-10-22 12:18:51 -04003911
3912 if (!has_extents)
3913 return ret;
3914
Filipe Manana74121f7c2014-08-07 12:00:44 +01003915 if (need_find_last_extent && *last_extent == first_key.offset) {
3916 /*
3917 * We don't have any leafs between our current one and the one
3918 * we processed before that can have file extent items for our
3919 * inode (and have a generation number smaller than our current
3920 * transaction id).
3921 */
3922 need_find_last_extent = false;
3923 }
3924
Josef Bacik16e75492013-10-22 12:18:51 -04003925 /*
3926 * Because we use btrfs_search_forward we could skip leaves that were
3927 * not modified and then assume *last_extent is valid when it really
3928 * isn't. So back up to the previous leaf and read the end of the last
3929 * extent before we go and fill in holes.
3930 */
3931 if (need_find_last_extent) {
3932 u64 len;
3933
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003934 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003935 if (ret < 0)
3936 return ret;
3937 if (ret)
3938 goto fill_holes;
3939 if (src_path->slots[0])
3940 src_path->slots[0]--;
3941 src = src_path->nodes[0];
3942 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003943 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003944 key.type != BTRFS_EXTENT_DATA_KEY)
3945 goto fill_holes;
3946 extent = btrfs_item_ptr(src, src_path->slots[0],
3947 struct btrfs_file_extent_item);
3948 if (btrfs_file_extent_type(src, extent) ==
3949 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003950 len = btrfs_file_extent_inline_len(src,
3951 src_path->slots[0],
3952 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003953 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003954 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003955 } else {
3956 len = btrfs_file_extent_num_bytes(src, extent);
3957 *last_extent = key.offset + len;
3958 }
3959 }
3960fill_holes:
3961 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3962 * things could have happened
3963 *
3964 * 1) A merge could have happened, so we could currently be on a leaf
3965 * that holds what we were copying in the first place.
3966 * 2) A split could have happened, and now not all of the items we want
3967 * are on the same leaf.
3968 *
3969 * So we need to adjust how we search for holes, we need to drop the
3970 * path and re-search for the first extent key we found, and then walk
3971 * forward until we hit the last one we copied.
3972 */
3973 if (need_find_last_extent) {
3974 /* btrfs_prev_leaf could return 1 without releasing the path */
3975 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01003976 ret = btrfs_search_slot(NULL, inode->root, &first_key,
3977 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04003978 if (ret < 0)
3979 return ret;
3980 ASSERT(ret == 0);
3981 src = src_path->nodes[0];
3982 i = src_path->slots[0];
3983 } else {
3984 i = start_slot;
3985 }
3986
3987 /*
3988 * Ok so here we need to go through and fill in any holes we may have
3989 * to make sure that holes are punched for those areas in case they had
3990 * extents previously.
3991 */
3992 while (!done) {
3993 u64 offset, len;
3994 u64 extent_end;
3995
3996 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003997 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003998 if (ret < 0)
3999 return ret;
4000 ASSERT(ret == 0);
4001 src = src_path->nodes[0];
4002 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01004003 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04004004 }
4005
4006 btrfs_item_key_to_cpu(src, &key, i);
4007 if (!btrfs_comp_cpu_keys(&key, &last_key))
4008 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004009 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004010 key.type != BTRFS_EXTENT_DATA_KEY) {
4011 i++;
4012 continue;
4013 }
4014 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4015 if (btrfs_file_extent_type(src, extent) ==
4016 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004017 len = btrfs_file_extent_inline_len(src, i, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004018 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004019 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004020 } else {
4021 len = btrfs_file_extent_num_bytes(src, extent);
4022 extent_end = key.offset + len;
4023 }
4024 i++;
4025
4026 if (*last_extent == key.offset) {
4027 *last_extent = extent_end;
4028 continue;
4029 }
4030 offset = *last_extent;
4031 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004032 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004033 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004034 if (ret)
4035 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004036 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004037 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004038
4039 /*
4040 * Check if there is a hole between the last extent found in our leaf
4041 * and the first extent in the next leaf. If there is one, we need to
4042 * log an explicit hole so that at replay time we can punch the hole.
4043 */
4044 if (ret == 0 &&
4045 key.objectid == btrfs_ino(inode) &&
4046 key.type == BTRFS_EXTENT_DATA_KEY &&
4047 i == btrfs_header_nritems(src_path->nodes[0])) {
4048 ret = btrfs_next_leaf(inode->root, src_path);
4049 need_find_last_extent = true;
4050 if (ret > 0) {
4051 ret = 0;
4052 } else if (ret == 0) {
4053 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4054 src_path->slots[0]);
4055 if (key.objectid == btrfs_ino(inode) &&
4056 key.type == BTRFS_EXTENT_DATA_KEY &&
4057 *last_extent < key.offset) {
4058 const u64 len = key.offset - *last_extent;
4059
4060 ret = btrfs_insert_file_extent(trans, log,
4061 btrfs_ino(inode),
4062 *last_extent, 0,
4063 0, len, 0, len,
4064 0, 0, 0);
4065 }
4066 }
4067 }
Josef Bacik16e75492013-10-22 12:18:51 -04004068 /*
4069 * Need to let the callers know we dropped the path so they should
4070 * re-search.
4071 */
4072 if (!ret && need_find_last_extent)
4073 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004074 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004075}
4076
Josef Bacik5dc562c2012-08-17 13:14:17 -04004077static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4078{
4079 struct extent_map *em1, *em2;
4080
4081 em1 = list_entry(a, struct extent_map, list);
4082 em2 = list_entry(b, struct extent_map, list);
4083
4084 if (em1->start < em2->start)
4085 return -1;
4086 else if (em1->start > em2->start)
4087 return 1;
4088 return 0;
4089}
4090
Filipe Manana8407f552014-09-05 15:14:39 +01004091static int wait_ordered_extents(struct btrfs_trans_handle *trans,
4092 struct inode *inode,
4093 struct btrfs_root *root,
4094 const struct extent_map *em,
4095 const struct list_head *logged_list,
4096 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004097{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004098 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004099 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01004100 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004101 u64 mod_start = em->mod_start;
4102 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004103 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004104 u64 csum_offset;
4105 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004106 LIST_HEAD(ordered_sums);
4107 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004108
Filipe Manana8407f552014-09-05 15:14:39 +01004109 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04004110
Filipe Manana8407f552014-09-05 15:14:39 +01004111 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4112 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004113 return 0;
4114
Josef Bacik2ab28f32012-10-12 15:27:49 -04004115 /*
Filipe Manana8407f552014-09-05 15:14:39 +01004116 * Wait far any ordered extent that covers our extent map. If it
4117 * finishes without an error, first check and see if our csums are on
4118 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04004119 */
Miao Xie827463c2014-01-14 20:31:51 +08004120 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04004121 struct btrfs_ordered_sum *sum;
4122
4123 if (!mod_len)
4124 break;
4125
Josef Bacik2ab28f32012-10-12 15:27:49 -04004126 if (ordered->file_offset + ordered->len <= mod_start ||
4127 mod_start + mod_len <= ordered->file_offset)
4128 continue;
4129
Filipe Manana8407f552014-09-05 15:14:39 +01004130 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
4131 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
4132 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
4133 const u64 start = ordered->file_offset;
4134 const u64 end = ordered->file_offset + ordered->len - 1;
4135
4136 WARN_ON(ordered->inode != inode);
4137 filemap_fdatawrite_range(inode->i_mapping, start, end);
4138 }
4139
4140 wait_event(ordered->wait,
4141 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
4142 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
4143
4144 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00004145 /*
4146 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
4147 * i_mapping flags, so that the next fsync won't get
4148 * an outdated io error too.
4149 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004150 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01004151 *ordered_io_error = true;
4152 break;
4153 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004154 /*
4155 * We are going to copy all the csums on this ordered extent, so
4156 * go ahead and adjust mod_start and mod_len in case this
4157 * ordered extent has already been logged.
4158 */
4159 if (ordered->file_offset > mod_start) {
4160 if (ordered->file_offset + ordered->len >=
4161 mod_start + mod_len)
4162 mod_len = ordered->file_offset - mod_start;
4163 /*
4164 * If we have this case
4165 *
4166 * |--------- logged extent ---------|
4167 * |----- ordered extent ----|
4168 *
4169 * Just don't mess with mod_start and mod_len, we'll
4170 * just end up logging more csums than we need and it
4171 * will be ok.
4172 */
4173 } else {
4174 if (ordered->file_offset + ordered->len <
4175 mod_start + mod_len) {
4176 mod_len = (mod_start + mod_len) -
4177 (ordered->file_offset + ordered->len);
4178 mod_start = ordered->file_offset +
4179 ordered->len;
4180 } else {
4181 mod_len = 0;
4182 }
4183 }
4184
Filipe Manana8407f552014-09-05 15:14:39 +01004185 if (skip_csum)
4186 continue;
4187
Josef Bacik2ab28f32012-10-12 15:27:49 -04004188 /*
4189 * To keep us from looping for the above case of an ordered
4190 * extent that falls inside of the logged extent.
4191 */
4192 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4193 &ordered->flags))
4194 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004195
Josef Bacik2ab28f32012-10-12 15:27:49 -04004196 list_for_each_entry(sum, &ordered->list, list) {
4197 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004198 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004199 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004200 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004201 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004202
Filipe Manana8407f552014-09-05 15:14:39 +01004203 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004204 return ret;
4205
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004206 if (em->compress_type) {
4207 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004208 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004209 } else {
4210 csum_offset = mod_start - em->start;
4211 csum_len = mod_len;
4212 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004213
Josef Bacik70c8a912012-10-11 16:54:30 -04004214 /* block start is already adjusted for the file extent offset. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004215 ret = btrfs_lookup_csums_range(fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004216 em->block_start + csum_offset,
4217 em->block_start + csum_offset +
4218 csum_len - 1, &ordered_sums, 0);
4219 if (ret)
4220 return ret;
4221
4222 while (!list_empty(&ordered_sums)) {
4223 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4224 struct btrfs_ordered_sum,
4225 list);
4226 if (!ret)
4227 ret = btrfs_csum_file_blocks(trans, log, sums);
4228 list_del(&sums->list);
4229 kfree(sums);
4230 }
4231
4232 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004233}
4234
Filipe Manana8407f552014-09-05 15:14:39 +01004235static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004236 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004237 const struct extent_map *em,
4238 struct btrfs_path *path,
4239 const struct list_head *logged_list,
4240 struct btrfs_log_ctx *ctx)
4241{
4242 struct btrfs_root *log = root->log_root;
4243 struct btrfs_file_extent_item *fi;
4244 struct extent_buffer *leaf;
4245 struct btrfs_map_token token;
4246 struct btrfs_key key;
4247 u64 extent_offset = em->start - em->orig_start;
4248 u64 block_len;
4249 int ret;
4250 int extent_inserted = 0;
4251 bool ordered_io_err = false;
4252
David Sterbaf85b7372017-01-20 14:54:07 +01004253 ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
4254 logged_list, &ordered_io_err);
Filipe Manana8407f552014-09-05 15:14:39 +01004255 if (ret)
4256 return ret;
4257
4258 if (ordered_io_err) {
4259 ctx->io_err = -EIO;
Liu Boebb70442017-11-21 14:35:40 -07004260 return ctx->io_err;
Filipe Manana8407f552014-09-05 15:14:39 +01004261 }
4262
4263 btrfs_init_map_token(&token);
4264
Nikolay Borisov9d122622017-01-18 00:31:40 +02004265 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004266 em->start + em->len, NULL, 0, 1,
4267 sizeof(*fi), &extent_inserted);
4268 if (ret)
4269 return ret;
4270
4271 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004272 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004273 key.type = BTRFS_EXTENT_DATA_KEY;
4274 key.offset = em->start;
4275
4276 ret = btrfs_insert_empty_item(trans, log, path, &key,
4277 sizeof(*fi));
4278 if (ret)
4279 return ret;
4280 }
4281 leaf = path->nodes[0];
4282 fi = btrfs_item_ptr(leaf, path->slots[0],
4283 struct btrfs_file_extent_item);
4284
Josef Bacik50d9aa92014-11-21 14:52:38 -05004285 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004286 &token);
4287 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4288 btrfs_set_token_file_extent_type(leaf, fi,
4289 BTRFS_FILE_EXTENT_PREALLOC,
4290 &token);
4291 else
4292 btrfs_set_token_file_extent_type(leaf, fi,
4293 BTRFS_FILE_EXTENT_REG,
4294 &token);
4295
4296 block_len = max(em->block_len, em->orig_block_len);
4297 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4298 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4299 em->block_start,
4300 &token);
4301 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4302 &token);
4303 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4304 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4305 em->block_start -
4306 extent_offset, &token);
4307 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4308 &token);
4309 } else {
4310 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4311 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4312 &token);
4313 }
4314
4315 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4316 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4317 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4318 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4319 &token);
4320 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4321 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4322 btrfs_mark_buffer_dirty(leaf);
4323
4324 btrfs_release_path(path);
4325
4326 return ret;
4327}
4328
Filipe Manana31d11b82018-05-09 16:01:46 +01004329/*
4330 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4331 * lose them after doing a fast fsync and replaying the log. We scan the
4332 * subvolume's root instead of iterating the inode's extent map tree because
4333 * otherwise we can log incorrect extent items based on extent map conversion.
4334 * That can happen due to the fact that extent maps are merged when they
4335 * are not in the extent map tree's list of modified extents.
4336 */
4337static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4338 struct btrfs_inode *inode,
4339 struct btrfs_path *path)
4340{
4341 struct btrfs_root *root = inode->root;
4342 struct btrfs_key key;
4343 const u64 i_size = i_size_read(&inode->vfs_inode);
4344 const u64 ino = btrfs_ino(inode);
4345 struct btrfs_path *dst_path = NULL;
4346 u64 last_extent = (u64)-1;
4347 int ins_nr = 0;
4348 int start_slot;
4349 int ret;
4350
4351 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4352 return 0;
4353
4354 key.objectid = ino;
4355 key.type = BTRFS_EXTENT_DATA_KEY;
4356 key.offset = i_size;
4357 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4358 if (ret < 0)
4359 goto out;
4360
4361 while (true) {
4362 struct extent_buffer *leaf = path->nodes[0];
4363 int slot = path->slots[0];
4364
4365 if (slot >= btrfs_header_nritems(leaf)) {
4366 if (ins_nr > 0) {
4367 ret = copy_items(trans, inode, dst_path, path,
4368 &last_extent, start_slot,
4369 ins_nr, 1, 0);
4370 if (ret < 0)
4371 goto out;
4372 ins_nr = 0;
4373 }
4374 ret = btrfs_next_leaf(root, path);
4375 if (ret < 0)
4376 goto out;
4377 if (ret > 0) {
4378 ret = 0;
4379 break;
4380 }
4381 continue;
4382 }
4383
4384 btrfs_item_key_to_cpu(leaf, &key, slot);
4385 if (key.objectid > ino)
4386 break;
4387 if (WARN_ON_ONCE(key.objectid < ino) ||
4388 key.type < BTRFS_EXTENT_DATA_KEY ||
4389 key.offset < i_size) {
4390 path->slots[0]++;
4391 continue;
4392 }
4393 if (last_extent == (u64)-1) {
4394 last_extent = key.offset;
4395 /*
4396 * Avoid logging extent items logged in past fsync calls
4397 * and leading to duplicate keys in the log tree.
4398 */
4399 do {
4400 ret = btrfs_truncate_inode_items(trans,
4401 root->log_root,
4402 &inode->vfs_inode,
4403 i_size,
4404 BTRFS_EXTENT_DATA_KEY);
4405 } while (ret == -EAGAIN);
4406 if (ret)
4407 goto out;
4408 }
4409 if (ins_nr == 0)
4410 start_slot = slot;
4411 ins_nr++;
4412 path->slots[0]++;
4413 if (!dst_path) {
4414 dst_path = btrfs_alloc_path();
4415 if (!dst_path) {
4416 ret = -ENOMEM;
4417 goto out;
4418 }
4419 }
4420 }
4421 if (ins_nr > 0) {
4422 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4423 start_slot, ins_nr, 1, 0);
4424 if (ret > 0)
4425 ret = 0;
4426 }
4427out:
4428 btrfs_release_path(path);
4429 btrfs_free_path(dst_path);
4430 return ret;
4431}
4432
Josef Bacik5dc562c2012-08-17 13:14:17 -04004433static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4434 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004435 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004436 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004437 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004438 struct btrfs_log_ctx *ctx,
4439 const u64 start,
4440 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004441{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004442 struct extent_map *em, *n;
4443 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004444 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004445 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004446 u64 test_gen;
4447 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004448 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004449
4450 INIT_LIST_HEAD(&extents);
4451
Nikolay Borisov9d122622017-01-18 00:31:40 +02004452 down_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004453 write_lock(&tree->lock);
4454 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004455 logged_start = start;
4456 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004457
4458 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4459 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004460 /*
4461 * Just an arbitrary number, this can be really CPU intensive
4462 * once we start getting a lot of extents, and really once we
4463 * have a bunch of extents we just want to commit since it will
4464 * be faster.
4465 */
4466 if (++num > 32768) {
4467 list_del_init(&tree->modified_extents);
4468 ret = -EFBIG;
4469 goto process;
4470 }
4471
Josef Bacik5dc562c2012-08-17 13:14:17 -04004472 if (em->generation <= test_gen)
4473 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004474
Filipe Manana31d11b82018-05-09 16:01:46 +01004475 /* We log prealloc extents beyond eof later. */
4476 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4477 em->start >= i_size_read(&inode->vfs_inode))
4478 continue;
4479
Josef Bacik8c6c5922017-08-29 10:11:39 -04004480 if (em->start < logged_start)
4481 logged_start = em->start;
4482 if ((em->start + em->len - 1) > logged_end)
4483 logged_end = em->start + em->len - 1;
4484
Josef Bacikff44c6e2012-09-14 12:59:20 -04004485 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004486 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004487 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004488 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004489 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004490 }
4491
4492 list_sort(NULL, &extents, extent_cmp);
Josef Bacik8c6c5922017-08-29 10:11:39 -04004493 btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004494 /*
4495 * Some ordered extents started by fsync might have completed
4496 * before we could collect them into the list logged_list, which
4497 * means they're gone, not in our logged_list nor in the inode's
4498 * ordered tree. We want the application/user space to know an
4499 * error happened while attempting to persist file data so that
4500 * it can take proper action. If such error happened, we leave
4501 * without writing to the log tree and the fsync must report the
4502 * file data write error and not commit the current transaction.
4503 */
Nikolay Borisov9d122622017-01-18 00:31:40 +02004504 ret = filemap_check_errors(inode->vfs_inode.i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004505 if (ret)
4506 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004507process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004508 while (!list_empty(&extents)) {
4509 em = list_entry(extents.next, struct extent_map, list);
4510
4511 list_del_init(&em->list);
4512
4513 /*
4514 * If we had an error we just need to delete everybody from our
4515 * private list.
4516 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004517 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004518 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004519 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004520 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004521 }
4522
4523 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004524
Filipe Manana8407f552014-09-05 15:14:39 +01004525 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4526 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004527 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004528 clear_em_logging(tree, em);
4529 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004530 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004531 WARN_ON(!list_empty(&extents));
4532 write_unlock(&tree->lock);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004533 up_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004534
Josef Bacik5dc562c2012-08-17 13:14:17 -04004535 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004536 if (!ret)
4537 ret = btrfs_log_prealloc_extents(trans, inode, path);
4538
Josef Bacik5dc562c2012-08-17 13:14:17 -04004539 return ret;
4540}
4541
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004542static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004543 struct btrfs_path *path, u64 *size_ret)
4544{
4545 struct btrfs_key key;
4546 int ret;
4547
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004548 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004549 key.type = BTRFS_INODE_ITEM_KEY;
4550 key.offset = 0;
4551
4552 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4553 if (ret < 0) {
4554 return ret;
4555 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004556 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004557 } else {
4558 struct btrfs_inode_item *item;
4559
4560 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4561 struct btrfs_inode_item);
4562 *size_ret = btrfs_inode_size(path->nodes[0], item);
4563 }
4564
4565 btrfs_release_path(path);
4566 return 0;
4567}
4568
Filipe Manana36283bf2015-06-20 00:44:51 +01004569/*
4570 * At the moment we always log all xattrs. This is to figure out at log replay
4571 * time which xattrs must have their deletion replayed. If a xattr is missing
4572 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4573 * because if a xattr is deleted, the inode is fsynced and a power failure
4574 * happens, causing the log to be replayed the next time the fs is mounted,
4575 * we want the xattr to not exist anymore (same behaviour as other filesystems
4576 * with a journal, ext3/4, xfs, f2fs, etc).
4577 */
4578static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4579 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004580 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004581 struct btrfs_path *path,
4582 struct btrfs_path *dst_path)
4583{
4584 int ret;
4585 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004586 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004587 int ins_nr = 0;
4588 int start_slot = 0;
4589
4590 key.objectid = ino;
4591 key.type = BTRFS_XATTR_ITEM_KEY;
4592 key.offset = 0;
4593
4594 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4595 if (ret < 0)
4596 return ret;
4597
4598 while (true) {
4599 int slot = path->slots[0];
4600 struct extent_buffer *leaf = path->nodes[0];
4601 int nritems = btrfs_header_nritems(leaf);
4602
4603 if (slot >= nritems) {
4604 if (ins_nr > 0) {
4605 u64 last_extent = 0;
4606
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004607 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004608 &last_extent, start_slot,
4609 ins_nr, 1, 0);
4610 /* can't be 1, extent items aren't processed */
4611 ASSERT(ret <= 0);
4612 if (ret < 0)
4613 return ret;
4614 ins_nr = 0;
4615 }
4616 ret = btrfs_next_leaf(root, path);
4617 if (ret < 0)
4618 return ret;
4619 else if (ret > 0)
4620 break;
4621 continue;
4622 }
4623
4624 btrfs_item_key_to_cpu(leaf, &key, slot);
4625 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4626 break;
4627
4628 if (ins_nr == 0)
4629 start_slot = slot;
4630 ins_nr++;
4631 path->slots[0]++;
4632 cond_resched();
4633 }
4634 if (ins_nr > 0) {
4635 u64 last_extent = 0;
4636
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004637 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004638 &last_extent, start_slot,
4639 ins_nr, 1, 0);
4640 /* can't be 1, extent items aren't processed */
4641 ASSERT(ret <= 0);
4642 if (ret < 0)
4643 return ret;
4644 }
4645
4646 return 0;
4647}
4648
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004649/*
4650 * If the no holes feature is enabled we need to make sure any hole between the
4651 * last extent and the i_size of our inode is explicitly marked in the log. This
4652 * is to make sure that doing something like:
4653 *
4654 * 1) create file with 128Kb of data
4655 * 2) truncate file to 64Kb
4656 * 3) truncate file to 256Kb
4657 * 4) fsync file
4658 * 5) <crash/power failure>
4659 * 6) mount fs and trigger log replay
4660 *
4661 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4662 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4663 * file correspond to a hole. The presence of explicit holes in a log tree is
4664 * what guarantees that log replay will remove/adjust file extent items in the
4665 * fs/subvol tree.
4666 *
4667 * Here we do not need to care about holes between extents, that is already done
4668 * by copy_items(). We also only need to do this in the full sync path, where we
4669 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4670 * lookup the list of modified extent maps and if any represents a hole, we
4671 * insert a corresponding extent representing a hole in the log tree.
4672 */
4673static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4674 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004675 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004676 struct btrfs_path *path)
4677{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004678 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004679 int ret;
4680 struct btrfs_key key;
4681 u64 hole_start;
4682 u64 hole_size;
4683 struct extent_buffer *leaf;
4684 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004685 const u64 ino = btrfs_ino(inode);
4686 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004687
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004688 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004689 return 0;
4690
4691 key.objectid = ino;
4692 key.type = BTRFS_EXTENT_DATA_KEY;
4693 key.offset = (u64)-1;
4694
4695 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4696 ASSERT(ret != 0);
4697 if (ret < 0)
4698 return ret;
4699
4700 ASSERT(path->slots[0] > 0);
4701 path->slots[0]--;
4702 leaf = path->nodes[0];
4703 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4704
4705 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4706 /* inode does not have any extents */
4707 hole_start = 0;
4708 hole_size = i_size;
4709 } else {
4710 struct btrfs_file_extent_item *extent;
4711 u64 len;
4712
4713 /*
4714 * If there's an extent beyond i_size, an explicit hole was
4715 * already inserted by copy_items().
4716 */
4717 if (key.offset >= i_size)
4718 return 0;
4719
4720 extent = btrfs_item_ptr(leaf, path->slots[0],
4721 struct btrfs_file_extent_item);
4722
4723 if (btrfs_file_extent_type(leaf, extent) ==
4724 BTRFS_FILE_EXTENT_INLINE) {
4725 len = btrfs_file_extent_inline_len(leaf,
4726 path->slots[0],
4727 extent);
Filipe Manana6399fb52017-07-28 15:22:36 +01004728 ASSERT(len == i_size ||
4729 (len == fs_info->sectorsize &&
4730 btrfs_file_extent_compression(leaf, extent) !=
4731 BTRFS_COMPRESS_NONE));
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004732 return 0;
4733 }
4734
4735 len = btrfs_file_extent_num_bytes(leaf, extent);
4736 /* Last extent goes beyond i_size, no need to log a hole. */
4737 if (key.offset + len > i_size)
4738 return 0;
4739 hole_start = key.offset + len;
4740 hole_size = i_size - hole_start;
4741 }
4742 btrfs_release_path(path);
4743
4744 /* Last extent ends at i_size. */
4745 if (hole_size == 0)
4746 return 0;
4747
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004748 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004749 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4750 hole_size, 0, hole_size, 0, 0, 0);
4751 return ret;
4752}
4753
Filipe Manana56f23fd2016-03-30 23:37:21 +01004754/*
4755 * When we are logging a new inode X, check if it doesn't have a reference that
4756 * matches the reference from some other inode Y created in a past transaction
4757 * and that was renamed in the current transaction. If we don't do this, then at
4758 * log replay time we can lose inode Y (and all its files if it's a directory):
4759 *
4760 * mkdir /mnt/x
4761 * echo "hello world" > /mnt/x/foobar
4762 * sync
4763 * mv /mnt/x /mnt/y
4764 * mkdir /mnt/x # or touch /mnt/x
4765 * xfs_io -c fsync /mnt/x
4766 * <power fail>
4767 * mount fs, trigger log replay
4768 *
4769 * After the log replay procedure, we would lose the first directory and all its
4770 * files (file foobar).
4771 * For the case where inode Y is not a directory we simply end up losing it:
4772 *
4773 * echo "123" > /mnt/foo
4774 * sync
4775 * mv /mnt/foo /mnt/bar
4776 * echo "abc" > /mnt/foo
4777 * xfs_io -c fsync /mnt/foo
4778 * <power fail>
4779 *
4780 * We also need this for cases where a snapshot entry is replaced by some other
4781 * entry (file or directory) otherwise we end up with an unreplayable log due to
4782 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4783 * if it were a regular entry:
4784 *
4785 * mkdir /mnt/x
4786 * btrfs subvolume snapshot /mnt /mnt/x/snap
4787 * btrfs subvolume delete /mnt/x/snap
4788 * rmdir /mnt/x
4789 * mkdir /mnt/x
4790 * fsync /mnt/x or fsync some new file inside it
4791 * <power fail>
4792 *
4793 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4794 * the same transaction.
4795 */
4796static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4797 const int slot,
4798 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004799 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004800 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004801{
4802 int ret;
4803 struct btrfs_path *search_path;
4804 char *name = NULL;
4805 u32 name_len = 0;
4806 u32 item_size = btrfs_item_size_nr(eb, slot);
4807 u32 cur_offset = 0;
4808 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4809
4810 search_path = btrfs_alloc_path();
4811 if (!search_path)
4812 return -ENOMEM;
4813 search_path->search_commit_root = 1;
4814 search_path->skip_locking = 1;
4815
4816 while (cur_offset < item_size) {
4817 u64 parent;
4818 u32 this_name_len;
4819 u32 this_len;
4820 unsigned long name_ptr;
4821 struct btrfs_dir_item *di;
4822
4823 if (key->type == BTRFS_INODE_REF_KEY) {
4824 struct btrfs_inode_ref *iref;
4825
4826 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4827 parent = key->offset;
4828 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4829 name_ptr = (unsigned long)(iref + 1);
4830 this_len = sizeof(*iref) + this_name_len;
4831 } else {
4832 struct btrfs_inode_extref *extref;
4833
4834 extref = (struct btrfs_inode_extref *)(ptr +
4835 cur_offset);
4836 parent = btrfs_inode_extref_parent(eb, extref);
4837 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4838 name_ptr = (unsigned long)&extref->name;
4839 this_len = sizeof(*extref) + this_name_len;
4840 }
4841
4842 if (this_name_len > name_len) {
4843 char *new_name;
4844
4845 new_name = krealloc(name, this_name_len, GFP_NOFS);
4846 if (!new_name) {
4847 ret = -ENOMEM;
4848 goto out;
4849 }
4850 name_len = this_name_len;
4851 name = new_name;
4852 }
4853
4854 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004855 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4856 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004857 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004858 struct btrfs_key di_key;
4859
4860 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4861 di, &di_key);
4862 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4863 ret = 1;
4864 *other_ino = di_key.objectid;
4865 } else {
4866 ret = -EAGAIN;
4867 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004868 goto out;
4869 } else if (IS_ERR(di)) {
4870 ret = PTR_ERR(di);
4871 goto out;
4872 }
4873 btrfs_release_path(search_path);
4874
4875 cur_offset += this_len;
4876 }
4877 ret = 0;
4878out:
4879 btrfs_free_path(search_path);
4880 kfree(name);
4881 return ret;
4882}
4883
Chris Masone02119d2008-09-05 16:13:11 -04004884/* log a single inode in the tree log.
4885 * At least one parent directory for this inode must exist in the tree
4886 * or be logged already.
4887 *
4888 * Any items from this inode changed by the current transaction are copied
4889 * to the log tree. An extra reference is taken on any extents in this
4890 * file, allowing us to avoid a whole pile of corner cases around logging
4891 * blocks that have been removed from the tree.
4892 *
4893 * See LOG_INODE_ALL and related defines for a description of what inode_only
4894 * does.
4895 *
4896 * This handles both files and directories.
4897 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004898static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004899 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004900 int inode_only,
4901 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004902 const loff_t end,
4903 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004904{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004905 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004906 struct btrfs_path *path;
4907 struct btrfs_path *dst_path;
4908 struct btrfs_key min_key;
4909 struct btrfs_key max_key;
4910 struct btrfs_root *log = root->log_root;
Miao Xie827463c2014-01-14 20:31:51 +08004911 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004912 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004913 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004914 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004915 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004916 int ins_start_slot = 0;
4917 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004918 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004919 u64 ino = btrfs_ino(inode);
4920 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004921 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004922 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01004923 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004924
Chris Masone02119d2008-09-05 16:13:11 -04004925 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004926 if (!path)
4927 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004928 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004929 if (!dst_path) {
4930 btrfs_free_path(path);
4931 return -ENOMEM;
4932 }
Chris Masone02119d2008-09-05 16:13:11 -04004933
Li Zefan33345d012011-04-20 10:31:50 +08004934 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004935 min_key.type = BTRFS_INODE_ITEM_KEY;
4936 min_key.offset = 0;
4937
Li Zefan33345d012011-04-20 10:31:50 +08004938 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004939
Chris Mason12fcfd22009-03-24 10:24:20 -04004940
Josef Bacik5dc562c2012-08-17 13:14:17 -04004941 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004942 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004943 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004944 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004945 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004946 max_key.type = BTRFS_XATTR_ITEM_KEY;
4947 else
4948 max_key.type = (u8)-1;
4949 max_key.offset = (u64)-1;
4950
Filipe Manana2c2c4522015-01-13 16:40:04 +00004951 /*
4952 * Only run delayed items if we are a dir or a new file.
4953 * Otherwise commit the delayed inode only, which is needed in
4954 * order for the log replay code to mark inodes for link count
4955 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4956 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004957 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4958 inode->generation > fs_info->last_trans_committed)
4959 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004960 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004961 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004962
4963 if (ret) {
4964 btrfs_free_path(path);
4965 btrfs_free_path(dst_path);
4966 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004967 }
4968
Liu Bo781feef2016-11-30 16:20:25 -08004969 if (inode_only == LOG_OTHER_INODE) {
4970 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004971 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004972 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004973 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004974 }
Chris Masone02119d2008-09-05 16:13:11 -04004975
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004976 /*
Chris Masone02119d2008-09-05 16:13:11 -04004977 * a brute force approach to making sure we get the most uptodate
4978 * copies of everything.
4979 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004980 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004981 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4982
Filipe Manana4f764e52015-02-23 19:53:35 +00004983 if (inode_only == LOG_INODE_EXISTS)
4984 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004985 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004986 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004987 if (inode_only == LOG_INODE_EXISTS) {
4988 /*
4989 * Make sure the new inode item we write to the log has
4990 * the same isize as the current one (if it exists).
4991 * This is necessary to prevent data loss after log
4992 * replay, and also to prevent doing a wrong expanding
4993 * truncate - for e.g. create file, write 4K into offset
4994 * 0, fsync, write 4K into offset 4096, add hard link,
4995 * fsync some other file (to sync log), power fail - if
4996 * we use the inode's current i_size, after log replay
4997 * we get a 8Kb file, with the last 4Kb extent as a hole
4998 * (zeroes), as if an expanding truncate happened,
4999 * instead of getting a file of 4Kb only.
5000 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005001 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005002 if (err)
5003 goto out_unlock;
5004 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005005 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005006 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00005007 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005008 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00005009 ret = drop_objectid_items(trans, log, path, ino,
5010 max_key.type);
5011 } else {
5012 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005013 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00005014 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005015 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08005016 while(1) {
5017 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005018 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005019 if (ret != -EAGAIN)
5020 break;
5021 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005022 }
Filipe Manana4f764e52015-02-23 19:53:35 +00005023 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005024 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05005025 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005026 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04005027 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00005028 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04005029 ret = drop_objectid_items(trans, log, path, ino,
5030 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005031 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00005032 if (inode_only == LOG_INODE_ALL)
5033 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04005034 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005035 }
Josef Bacika95249b2012-10-11 16:17:34 -04005036
Chris Masone02119d2008-09-05 16:13:11 -04005037 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005038 if (ret) {
5039 err = ret;
5040 goto out_unlock;
5041 }
Chris Masone02119d2008-09-05 16:13:11 -04005042
Chris Masond3977122009-01-05 21:25:51 -05005043 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04005044 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01005045 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005046 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07005047 if (ret < 0) {
5048 err = ret;
5049 goto out_unlock;
5050 }
Chris Masone02119d2008-09-05 16:13:11 -04005051 if (ret != 0)
5052 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04005053again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04005054 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08005055 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04005056 break;
5057 if (min_key.type > max_key.type)
5058 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005059
Filipe Mananae4545de2015-06-17 12:49:23 +01005060 if (min_key.type == BTRFS_INODE_ITEM_KEY)
5061 need_log_inode_item = false;
5062
Filipe Manana56f23fd2016-03-30 23:37:21 +01005063 if ((min_key.type == BTRFS_INODE_REF_KEY ||
5064 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02005065 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005066 u64 other_ino = 0;
5067
Filipe Manana56f23fd2016-03-30 23:37:21 +01005068 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02005069 path->slots[0], &min_key, inode,
5070 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01005071 if (ret < 0) {
5072 err = ret;
5073 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01005074 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005075 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005076 struct btrfs_key inode_key;
5077 struct inode *other_inode;
5078
5079 if (ins_nr > 0) {
5080 ins_nr++;
5081 } else {
5082 ins_nr = 1;
5083 ins_start_slot = path->slots[0];
5084 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02005085 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01005086 &last_extent, ins_start_slot,
5087 ins_nr, inode_only,
5088 logged_isize);
5089 if (ret < 0) {
5090 err = ret;
5091 goto out_unlock;
5092 }
5093 ins_nr = 0;
5094 btrfs_release_path(path);
5095 inode_key.objectid = other_ino;
5096 inode_key.type = BTRFS_INODE_ITEM_KEY;
5097 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005098 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01005099 &inode_key, root,
5100 NULL);
5101 /*
5102 * If the other inode that had a conflicting dir
5103 * entry was deleted in the current transaction,
5104 * we don't need to do more work nor fallback to
5105 * a transaction commit.
5106 */
5107 if (IS_ERR(other_inode) &&
5108 PTR_ERR(other_inode) == -ENOENT) {
5109 goto next_key;
5110 } else if (IS_ERR(other_inode)) {
5111 err = PTR_ERR(other_inode);
5112 goto out_unlock;
5113 }
5114 /*
5115 * We are safe logging the other inode without
5116 * acquiring its i_mutex as long as we log with
5117 * the LOG_INODE_EXISTS mode. We're safe against
5118 * concurrent renames of the other inode as well
5119 * because during a rename we pin the log and
5120 * update the log with the new name before we
5121 * unpin it.
5122 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005123 err = btrfs_log_inode(trans, root,
5124 BTRFS_I(other_inode),
5125 LOG_OTHER_INODE, 0, LLONG_MAX,
5126 ctx);
Filipe Manana44f714d2016-06-06 16:11:13 +01005127 iput(other_inode);
5128 if (err)
5129 goto out_unlock;
5130 else
5131 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005132 }
5133 }
5134
Filipe Manana36283bf2015-06-20 00:44:51 +01005135 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5136 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5137 if (ins_nr == 0)
5138 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005139 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005140 &last_extent, ins_start_slot,
5141 ins_nr, inode_only, logged_isize);
5142 if (ret < 0) {
5143 err = ret;
5144 goto out_unlock;
5145 }
5146 ins_nr = 0;
5147 if (ret) {
5148 btrfs_release_path(path);
5149 continue;
5150 }
5151 goto next_slot;
5152 }
5153
Chris Mason31ff1cd2008-09-11 16:17:57 -04005154 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5155 ins_nr++;
5156 goto next_slot;
5157 } else if (!ins_nr) {
5158 ins_start_slot = path->slots[0];
5159 ins_nr = 1;
5160 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005161 }
5162
Nikolay Borisova59108a2017-01-18 00:31:48 +02005163 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005164 ins_start_slot, ins_nr, inode_only,
5165 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005166 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005167 err = ret;
5168 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005169 }
5170 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005171 ins_nr = 0;
5172 btrfs_release_path(path);
5173 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005174 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005175 ins_nr = 1;
5176 ins_start_slot = path->slots[0];
5177next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005178
Chris Mason3a5f1d42008-09-11 15:53:37 -04005179 nritems = btrfs_header_nritems(path->nodes[0]);
5180 path->slots[0]++;
5181 if (path->slots[0] < nritems) {
5182 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5183 path->slots[0]);
5184 goto again;
5185 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005186 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005187 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005188 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005189 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005190 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005191 err = ret;
5192 goto out_unlock;
5193 }
Josef Bacik16e75492013-10-22 12:18:51 -04005194 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005195 ins_nr = 0;
5196 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005197 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005198next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005199 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005200 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005201 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005202 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005203 min_key.offset = 0;
5204 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005205 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005206 }
Chris Masone02119d2008-09-05 16:13:11 -04005207 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005208 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005209 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005210 ins_start_slot, ins_nr, inode_only,
5211 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005212 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005213 err = ret;
5214 goto out_unlock;
5215 }
Josef Bacik16e75492013-10-22 12:18:51 -04005216 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005217 ins_nr = 0;
5218 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005219
Filipe Manana36283bf2015-06-20 00:44:51 +01005220 btrfs_release_path(path);
5221 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005222 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005223 if (err)
5224 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005225 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005226 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5227 btrfs_release_path(path);
5228 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005229 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005230 if (err)
5231 goto out_unlock;
5232 }
Josef Bacika95249b2012-10-11 16:17:34 -04005233log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005234 btrfs_release_path(path);
5235 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005236 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005237 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005238 if (!err && !xattrs_logged) {
5239 err = btrfs_log_all_xattrs(trans, root, inode, path,
5240 dst_path);
5241 btrfs_release_path(path);
5242 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005243 if (err)
5244 goto out_unlock;
5245 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005246 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005247 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00005248 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005249 if (ret) {
5250 err = ret;
5251 goto out_unlock;
5252 }
Josef Bacikd006a042013-11-12 20:54:09 -05005253 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005254 struct extent_map *em, *n;
5255
Filipe Manana49dae1b2014-09-06 22:34:39 +01005256 write_lock(&em_tree->lock);
5257 /*
5258 * We can't just remove every em if we're called for a ranged
5259 * fsync - that is, one that doesn't cover the whole possible
5260 * file range (0 to LLONG_MAX). This is because we can have
5261 * em's that fall outside the range we're logging and therefore
5262 * their ordered operations haven't completed yet
5263 * (btrfs_finish_ordered_io() not invoked yet). This means we
5264 * didn't get their respective file extent item in the fs/subvol
5265 * tree yet, and need to let the next fast fsync (one which
5266 * consults the list of modified extent maps) find the em so
5267 * that it logs a matching file extent item and waits for the
5268 * respective ordered operation to complete (if it's still
5269 * running).
5270 *
5271 * Removing every em outside the range we're logging would make
5272 * the next fast fsync not log their matching file extent items,
5273 * therefore making us lose data after a log replay.
5274 */
5275 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5276 list) {
5277 const u64 mod_end = em->mod_start + em->mod_len - 1;
5278
5279 if (em->mod_start >= start && mod_end <= end)
5280 list_del_init(&em->list);
5281 }
5282 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005283 }
5284
Nikolay Borisova59108a2017-01-18 00:31:48 +02005285 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5286 ret = log_directory_changes(trans, root, inode, path, dst_path,
5287 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005288 if (ret) {
5289 err = ret;
5290 goto out_unlock;
5291 }
Chris Masone02119d2008-09-05 16:13:11 -04005292 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005293
Nikolay Borisova59108a2017-01-18 00:31:48 +02005294 spin_lock(&inode->lock);
5295 inode->logged_trans = trans->transid;
5296 inode->last_log_commit = inode->last_sub_trans;
5297 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005298out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005299 if (unlikely(err))
5300 btrfs_put_logged_extents(&logged_list);
5301 else
5302 btrfs_submit_logged_extents(&logged_list, log);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005303 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005304
5305 btrfs_free_path(path);
5306 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005307 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005308}
5309
Chris Mason12fcfd22009-03-24 10:24:20 -04005310/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005311 * Check if we must fallback to a transaction commit when logging an inode.
5312 * This must be called after logging the inode and is used only in the context
5313 * when fsyncing an inode requires the need to log some other inode - in which
5314 * case we can't lock the i_mutex of each other inode we need to log as that
5315 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5316 * log inodes up or down in the hierarchy) or rename operations for example. So
5317 * we take the log_mutex of the inode after we have logged it and then check for
5318 * its last_unlink_trans value - this is safe because any task setting
5319 * last_unlink_trans must take the log_mutex and it must do this before it does
5320 * the actual unlink operation, so if we do this check before a concurrent task
5321 * sets last_unlink_trans it means we've logged a consistent version/state of
5322 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005323 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005324 * we logged the inode or it might have also done the unlink).
5325 */
5326static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005327 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005328{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005329 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005330 bool ret = false;
5331
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005332 mutex_lock(&inode->log_mutex);
5333 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005334 /*
5335 * Make sure any commits to the log are forced to be full
5336 * commits.
5337 */
5338 btrfs_set_log_full_commit(fs_info, trans);
5339 ret = true;
5340 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005341 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005342
5343 return ret;
5344}
5345
5346/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005347 * follow the dentry parent pointers up the chain and see if any
5348 * of the directories in it require a full commit before they can
5349 * be logged. Returns zero if nothing special needs to be done or 1 if
5350 * a full commit is required.
5351 */
5352static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005353 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005354 struct dentry *parent,
5355 struct super_block *sb,
5356 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005357{
Chris Mason12fcfd22009-03-24 10:24:20 -04005358 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005359 struct dentry *old_parent = NULL;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005360 struct btrfs_inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005361
Chris Masonaf4176b2009-03-24 10:24:31 -04005362 /*
5363 * for regular files, if its inode is already on disk, we don't
5364 * have to worry about the parents at all. This is because
5365 * we can use the last_unlink_trans field to record renames
5366 * and other fun in this file.
5367 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005368 if (S_ISREG(inode->vfs_inode.i_mode) &&
5369 inode->generation <= last_committed &&
5370 inode->last_unlink_trans <= last_committed)
5371 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005372
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005373 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005374 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005375 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005376 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005377 }
5378
5379 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005380 /*
5381 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005382 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005383 * logged_trans so that further down in the log code we don't
5384 * think this inode has already been logged.
5385 */
5386 if (inode != orig_inode)
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005387 inode->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005388 smp_mb();
5389
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005390 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005391 ret = 1;
5392 break;
5393 }
5394
Al Virofc640052016-04-10 01:33:30 -04005395 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005396 break;
5397
Filipe Manana44f714d2016-06-06 16:11:13 +01005398 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005399 inode = BTRFS_I(d_inode(parent));
5400 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005401 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005402 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005403 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005404
Josef Bacik6a912212010-11-20 09:48:00 +00005405 parent = dget_parent(parent);
5406 dput(old_parent);
5407 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005408 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005409
5410 }
Josef Bacik6a912212010-11-20 09:48:00 +00005411 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005412out:
Chris Masone02119d2008-09-05 16:13:11 -04005413 return ret;
5414}
5415
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005416struct btrfs_dir_list {
5417 u64 ino;
5418 struct list_head list;
5419};
5420
5421/*
5422 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5423 * details about the why it is needed.
5424 * This is a recursive operation - if an existing dentry corresponds to a
5425 * directory, that directory's new entries are logged too (same behaviour as
5426 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5427 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5428 * complains about the following circular lock dependency / possible deadlock:
5429 *
5430 * CPU0 CPU1
5431 * ---- ----
5432 * lock(&type->i_mutex_dir_key#3/2);
5433 * lock(sb_internal#2);
5434 * lock(&type->i_mutex_dir_key#3/2);
5435 * lock(&sb->s_type->i_mutex_key#14);
5436 *
5437 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5438 * sb_start_intwrite() in btrfs_start_transaction().
5439 * Not locking i_mutex of the inodes is still safe because:
5440 *
5441 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5442 * that while logging the inode new references (names) are added or removed
5443 * from the inode, leaving the logged inode item with a link count that does
5444 * not match the number of logged inode reference items. This is fine because
5445 * at log replay time we compute the real number of links and correct the
5446 * link count in the inode item (see replay_one_buffer() and
5447 * link_to_fixup_dir());
5448 *
5449 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5450 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5451 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5452 * has a size that doesn't match the sum of the lengths of all the logged
5453 * names. This does not result in a problem because if a dir_item key is
5454 * logged but its matching dir_index key is not logged, at log replay time we
5455 * don't use it to replay the respective name (see replay_one_name()). On the
5456 * other hand if only the dir_index key ends up being logged, the respective
5457 * name is added to the fs/subvol tree with both the dir_item and dir_index
5458 * keys created (see replay_one_name()).
5459 * The directory's inode item with a wrong i_size is not a problem as well,
5460 * since we don't use it at log replay time to set the i_size in the inode
5461 * item of the fs/subvol tree (see overwrite_item()).
5462 */
5463static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5464 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005465 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005466 struct btrfs_log_ctx *ctx)
5467{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005468 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005469 struct btrfs_root *log = root->log_root;
5470 struct btrfs_path *path;
5471 LIST_HEAD(dir_list);
5472 struct btrfs_dir_list *dir_elem;
5473 int ret = 0;
5474
5475 path = btrfs_alloc_path();
5476 if (!path)
5477 return -ENOMEM;
5478
5479 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5480 if (!dir_elem) {
5481 btrfs_free_path(path);
5482 return -ENOMEM;
5483 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005484 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005485 list_add_tail(&dir_elem->list, &dir_list);
5486
5487 while (!list_empty(&dir_list)) {
5488 struct extent_buffer *leaf;
5489 struct btrfs_key min_key;
5490 int nritems;
5491 int i;
5492
5493 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5494 list);
5495 if (ret)
5496 goto next_dir_inode;
5497
5498 min_key.objectid = dir_elem->ino;
5499 min_key.type = BTRFS_DIR_ITEM_KEY;
5500 min_key.offset = 0;
5501again:
5502 btrfs_release_path(path);
5503 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5504 if (ret < 0) {
5505 goto next_dir_inode;
5506 } else if (ret > 0) {
5507 ret = 0;
5508 goto next_dir_inode;
5509 }
5510
5511process_leaf:
5512 leaf = path->nodes[0];
5513 nritems = btrfs_header_nritems(leaf);
5514 for (i = path->slots[0]; i < nritems; i++) {
5515 struct btrfs_dir_item *di;
5516 struct btrfs_key di_key;
5517 struct inode *di_inode;
5518 struct btrfs_dir_list *new_dir_elem;
5519 int log_mode = LOG_INODE_EXISTS;
5520 int type;
5521
5522 btrfs_item_key_to_cpu(leaf, &min_key, i);
5523 if (min_key.objectid != dir_elem->ino ||
5524 min_key.type != BTRFS_DIR_ITEM_KEY)
5525 goto next_dir_inode;
5526
5527 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5528 type = btrfs_dir_type(leaf, di);
5529 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5530 type != BTRFS_FT_DIR)
5531 continue;
5532 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5533 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5534 continue;
5535
Robbie Koec125cf2016-10-28 10:48:26 +08005536 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005538 if (IS_ERR(di_inode)) {
5539 ret = PTR_ERR(di_inode);
5540 goto next_dir_inode;
5541 }
5542
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005543 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005544 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005545 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005546 }
5547
5548 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005549 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005550 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005551 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005552 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005553 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005554 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005555 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005556 iput(di_inode);
5557 if (ret)
5558 goto next_dir_inode;
5559 if (ctx->log_new_dentries) {
5560 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5561 GFP_NOFS);
5562 if (!new_dir_elem) {
5563 ret = -ENOMEM;
5564 goto next_dir_inode;
5565 }
5566 new_dir_elem->ino = di_key.objectid;
5567 list_add_tail(&new_dir_elem->list, &dir_list);
5568 }
5569 break;
5570 }
5571 if (i == nritems) {
5572 ret = btrfs_next_leaf(log, path);
5573 if (ret < 0) {
5574 goto next_dir_inode;
5575 } else if (ret > 0) {
5576 ret = 0;
5577 goto next_dir_inode;
5578 }
5579 goto process_leaf;
5580 }
5581 if (min_key.offset < (u64)-1) {
5582 min_key.offset++;
5583 goto again;
5584 }
5585next_dir_inode:
5586 list_del(&dir_elem->list);
5587 kfree(dir_elem);
5588 }
5589
5590 btrfs_free_path(path);
5591 return ret;
5592}
5593
Filipe Manana18aa0922015-08-05 16:49:08 +01005594static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005595 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005596 struct btrfs_log_ctx *ctx)
5597{
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005598 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Filipe Manana18aa0922015-08-05 16:49:08 +01005599 int ret;
5600 struct btrfs_path *path;
5601 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005602 struct btrfs_root *root = inode->root;
5603 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005604
5605 path = btrfs_alloc_path();
5606 if (!path)
5607 return -ENOMEM;
5608 path->skip_locking = 1;
5609 path->search_commit_root = 1;
5610
5611 key.objectid = ino;
5612 key.type = BTRFS_INODE_REF_KEY;
5613 key.offset = 0;
5614 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5615 if (ret < 0)
5616 goto out;
5617
5618 while (true) {
5619 struct extent_buffer *leaf = path->nodes[0];
5620 int slot = path->slots[0];
5621 u32 cur_offset = 0;
5622 u32 item_size;
5623 unsigned long ptr;
5624
5625 if (slot >= btrfs_header_nritems(leaf)) {
5626 ret = btrfs_next_leaf(root, path);
5627 if (ret < 0)
5628 goto out;
5629 else if (ret > 0)
5630 break;
5631 continue;
5632 }
5633
5634 btrfs_item_key_to_cpu(leaf, &key, slot);
5635 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5636 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5637 break;
5638
5639 item_size = btrfs_item_size_nr(leaf, slot);
5640 ptr = btrfs_item_ptr_offset(leaf, slot);
5641 while (cur_offset < item_size) {
5642 struct btrfs_key inode_key;
5643 struct inode *dir_inode;
5644
5645 inode_key.type = BTRFS_INODE_ITEM_KEY;
5646 inode_key.offset = 0;
5647
5648 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5649 struct btrfs_inode_extref *extref;
5650
5651 extref = (struct btrfs_inode_extref *)
5652 (ptr + cur_offset);
5653 inode_key.objectid = btrfs_inode_extref_parent(
5654 leaf, extref);
5655 cur_offset += sizeof(*extref);
5656 cur_offset += btrfs_inode_extref_name_len(leaf,
5657 extref);
5658 } else {
5659 inode_key.objectid = key.offset;
5660 cur_offset = item_size;
5661 }
5662
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005663 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005664 root, NULL);
5665 /* If parent inode was deleted, skip it. */
5666 if (IS_ERR(dir_inode))
5667 continue;
5668
Filipe Manana657ed1a2016-04-06 17:11:56 +01005669 if (ctx)
5670 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005671 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005672 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005673 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005674 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005675 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005676 if (!ret && ctx && ctx->log_new_dentries)
5677 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005678 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005679 iput(dir_inode);
5680 if (ret)
5681 goto out;
5682 }
5683 path->slots[0]++;
5684 }
5685 ret = 0;
5686out:
5687 btrfs_free_path(path);
5688 return ret;
5689}
5690
Chris Masone02119d2008-09-05 16:13:11 -04005691/*
5692 * helper function around btrfs_log_inode to make sure newly created
5693 * parent directories also end up in the log. A minimal inode and backref
5694 * only logging is done of any parent directories that are older than
5695 * the last committed transaction
5696 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005697static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005698 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005699 struct dentry *parent,
5700 const loff_t start,
5701 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005702 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005703 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005704{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005705 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005706 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005707 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005708 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005709 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005710 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005711 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005712 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005713
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005714 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005715
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005716 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005717 ret = 1;
5718 goto end_no_trans;
5719 }
5720
Miao Xie995946d2014-04-02 19:51:06 +08005721 /*
5722 * The prev transaction commit doesn't complete, we need do
5723 * full commit by ourselves.
5724 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005725 if (fs_info->last_trans_log_full_commit >
5726 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005727 ret = 1;
5728 goto end_no_trans;
5729 }
5730
Nikolay Borisovf8822742018-02-27 17:37:17 +02005731 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005732 ret = 1;
5733 goto end_no_trans;
5734 }
5735
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005736 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5737 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005738 if (ret)
5739 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005740
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005741 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005742 ret = BTRFS_NO_LOG_SYNC;
5743 goto end_no_trans;
5744 }
5745
Miao Xie8b050d32014-02-20 18:08:58 +08005746 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005747 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005748 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005749
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005750 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005751 if (ret)
5752 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005753
Chris Masonaf4176b2009-03-24 10:24:31 -04005754 /*
5755 * for regular files, if its inode is already on disk, we don't
5756 * have to worry about the parents at all. This is because
5757 * we can use the last_unlink_trans field to record renames
5758 * and other fun in this file.
5759 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005760 if (S_ISREG(inode->vfs_inode.i_mode) &&
5761 inode->generation <= last_committed &&
5762 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005763 ret = 0;
5764 goto end_trans;
5765 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005766
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005767 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005768 log_dentries = true;
5769
Filipe Manana18aa0922015-08-05 16:49:08 +01005770 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005771 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005772 * inodes are fully logged. This is to prevent leaving dangling
5773 * directory index entries in directories that were our parents but are
5774 * not anymore. Not doing this results in old parent directory being
5775 * impossible to delete after log replay (rmdir will always fail with
5776 * error -ENOTEMPTY).
5777 *
5778 * Example 1:
5779 *
5780 * mkdir testdir
5781 * touch testdir/foo
5782 * ln testdir/foo testdir/bar
5783 * sync
5784 * unlink testdir/bar
5785 * xfs_io -c fsync testdir/foo
5786 * <power failure>
5787 * mount fs, triggers log replay
5788 *
5789 * If we don't log the parent directory (testdir), after log replay the
5790 * directory still has an entry pointing to the file inode using the bar
5791 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5792 * the file inode has a link count of 1.
5793 *
5794 * Example 2:
5795 *
5796 * mkdir testdir
5797 * touch foo
5798 * ln foo testdir/foo2
5799 * ln foo testdir/foo3
5800 * sync
5801 * unlink testdir/foo3
5802 * xfs_io -c fsync foo
5803 * <power failure>
5804 * mount fs, triggers log replay
5805 *
5806 * Similar as the first example, after log replay the parent directory
5807 * testdir still has an entry pointing to the inode file with name foo3
5808 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5809 * and has a link count of 2.
5810 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005811 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005812 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5813 if (ret)
5814 goto end_trans;
5815 }
5816
Chris Masond3977122009-01-05 21:25:51 -05005817 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005818 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005819 break;
5820
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005821 inode = BTRFS_I(d_inode(parent));
5822 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005823 break;
5824
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005825 if (inode->generation > last_committed) {
5826 ret = btrfs_log_inode(trans, root, inode,
5827 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005828 if (ret)
5829 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005830 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005831 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005832 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005833
Josef Bacik6a912212010-11-20 09:48:00 +00005834 parent = dget_parent(parent);
5835 dput(old_parent);
5836 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005837 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005838 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005839 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005840 else
5841 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005842end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005843 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005844 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005845 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005846 ret = 1;
5847 }
Miao Xie8b050d32014-02-20 18:08:58 +08005848
5849 if (ret)
5850 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005851 btrfs_end_log_trans(root);
5852end_no_trans:
5853 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005854}
5855
5856/*
5857 * it is not safe to log dentry if the chunk root has added new
5858 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5859 * If this returns 1, you must commit the transaction to safely get your
5860 * data on disk.
5861 */
5862int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02005863 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005864 const loff_t start,
5865 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005866 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005867{
Josef Bacik6a912212010-11-20 09:48:00 +00005868 struct dentry *parent = dget_parent(dentry);
5869 int ret;
5870
Nikolay Borisovf8822742018-02-27 17:37:17 +02005871 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5872 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005873 dput(parent);
5874
5875 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005876}
5877
5878/*
5879 * should be called during mount to recover any replay any log trees
5880 * from the FS
5881 */
5882int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5883{
5884 int ret;
5885 struct btrfs_path *path;
5886 struct btrfs_trans_handle *trans;
5887 struct btrfs_key key;
5888 struct btrfs_key found_key;
5889 struct btrfs_key tmp_key;
5890 struct btrfs_root *log;
5891 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5892 struct walk_control wc = {
5893 .process_func = process_one_buffer,
5894 .stage = 0,
5895 };
5896
Chris Masone02119d2008-09-05 16:13:11 -04005897 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005898 if (!path)
5899 return -ENOMEM;
5900
Josef Bacikafcdd122016-09-02 15:40:02 -04005901 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005902
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005903 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005904 if (IS_ERR(trans)) {
5905 ret = PTR_ERR(trans);
5906 goto error;
5907 }
Chris Masone02119d2008-09-05 16:13:11 -04005908
5909 wc.trans = trans;
5910 wc.pin = 1;
5911
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005912 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005913 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005914 btrfs_handle_fs_error(fs_info, ret,
5915 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005916 goto error;
5917 }
Chris Masone02119d2008-09-05 16:13:11 -04005918
5919again:
5920 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5921 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005922 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005923
Chris Masond3977122009-01-05 21:25:51 -05005924 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005925 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005926
5927 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005928 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005929 "Couldn't find tree log root.");
5930 goto error;
5931 }
Chris Masone02119d2008-09-05 16:13:11 -04005932 if (ret > 0) {
5933 if (path->slots[0] == 0)
5934 break;
5935 path->slots[0]--;
5936 }
5937 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5938 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005939 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005940 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5941 break;
5942
Miao Xiecb517ea2013-05-15 07:48:19 +00005943 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005944 if (IS_ERR(log)) {
5945 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005946 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005947 "Couldn't read tree log root.");
5948 goto error;
5949 }
Chris Masone02119d2008-09-05 16:13:11 -04005950
5951 tmp_key.objectid = found_key.offset;
5952 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5953 tmp_key.offset = (u64)-1;
5954
5955 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005956 if (IS_ERR(wc.replay_dest)) {
5957 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005958 free_extent_buffer(log->node);
5959 free_extent_buffer(log->commit_root);
5960 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005961 btrfs_handle_fs_error(fs_info, ret,
5962 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005963 goto error;
5964 }
Chris Masone02119d2008-09-05 16:13:11 -04005965
Yan Zheng07d400a2009-01-06 11:42:00 -05005966 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005967 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005968 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005969
Josef Bacikb50c6e22013-04-25 15:55:30 -04005970 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005971 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5972 path);
Chris Masone02119d2008-09-05 16:13:11 -04005973 }
Chris Masone02119d2008-09-05 16:13:11 -04005974
Liu Bo900c9982018-01-25 11:02:56 -07005975 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5976 struct btrfs_root *root = wc.replay_dest;
5977
5978 btrfs_release_path(path);
5979
5980 /*
5981 * We have just replayed everything, and the highest
5982 * objectid of fs roots probably has changed in case
5983 * some inode_item's got replayed.
5984 *
5985 * root->objectid_mutex is not acquired as log replay
5986 * could only happen during mount.
5987 */
5988 ret = btrfs_find_highest_objectid(root,
5989 &root->highest_objectid);
5990 }
5991
Chris Masone02119d2008-09-05 16:13:11 -04005992 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005993 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005994 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005995 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005996 kfree(log);
5997
Josef Bacikb50c6e22013-04-25 15:55:30 -04005998 if (ret)
5999 goto error;
6000
Chris Masone02119d2008-09-05 16:13:11 -04006001 if (found_key.offset == 0)
6002 break;
6003 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006004 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006005
6006 /* step one is to pin it all, step two is to replay just inodes */
6007 if (wc.pin) {
6008 wc.pin = 0;
6009 wc.process_func = replay_one_buffer;
6010 wc.stage = LOG_WALK_REPLAY_INODES;
6011 goto again;
6012 }
6013 /* step three is to replay everything */
6014 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6015 wc.stage++;
6016 goto again;
6017 }
6018
6019 btrfs_free_path(path);
6020
Josef Bacikabefa552013-04-24 16:40:05 -04006021 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006022 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04006023 if (ret)
6024 return ret;
6025
Chris Masone02119d2008-09-05 16:13:11 -04006026 free_extent_buffer(log_root_tree->node);
6027 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04006028 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006029 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006030
Josef Bacikabefa552013-04-24 16:40:05 -04006031 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006032error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04006033 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006034 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006035 btrfs_free_path(path);
6036 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006037}
Chris Mason12fcfd22009-03-24 10:24:20 -04006038
6039/*
6040 * there are some corner cases where we want to force a full
6041 * commit instead of allowing a directory to be logged.
6042 *
6043 * They revolve around files there were unlinked from the directory, and
6044 * this function updates the parent directory so that a full commit is
6045 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00006046 *
6047 * Must be called before the unlink operations (updates to the subvolume tree,
6048 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04006049 */
6050void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006051 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006052 int for_rename)
6053{
6054 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006055 * when we're logging a file, if it hasn't been renamed
6056 * or unlinked, and its inode is fully committed on disk,
6057 * we don't have to worry about walking up the directory chain
6058 * to log its parents.
6059 *
6060 * So, we use the last_unlink_trans field to put this transid
6061 * into the file. When the file is logged we check it and
6062 * don't log the parents if the file is fully on disk.
6063 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006064 mutex_lock(&inode->log_mutex);
6065 inode->last_unlink_trans = trans->transid;
6066 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006067
6068 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006069 * if this directory was already logged any new
6070 * names for this file/dir will get recorded
6071 */
6072 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006073 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006074 return;
6075
6076 /*
6077 * if the inode we're about to unlink was logged,
6078 * the log will be properly updated for any new names
6079 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006080 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006081 return;
6082
6083 /*
6084 * when renaming files across directories, if the directory
6085 * there we're unlinking from gets fsync'd later on, there's
6086 * no way to find the destination directory later and fsync it
6087 * properly. So, we have to be conservative and force commits
6088 * so the new name gets discovered.
6089 */
6090 if (for_rename)
6091 goto record;
6092
6093 /* we can safely do the unlink without any special recording */
6094 return;
6095
6096record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006097 mutex_lock(&dir->log_mutex);
6098 dir->last_unlink_trans = trans->transid;
6099 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006100}
6101
6102/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006103 * Make sure that if someone attempts to fsync the parent directory of a deleted
6104 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6105 * that after replaying the log tree of the parent directory's root we will not
6106 * see the snapshot anymore and at log replay time we will not see any log tree
6107 * corresponding to the deleted snapshot's root, which could lead to replaying
6108 * it after replaying the log tree of the parent directory (which would replay
6109 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006110 *
6111 * Must be called before the actual snapshot destroy operation (updates to the
6112 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006113 */
6114void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006115 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006116{
Nikolay Borisov43663552017-01-18 00:31:29 +02006117 mutex_lock(&dir->log_mutex);
6118 dir->last_unlink_trans = trans->transid;
6119 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006120}
6121
6122/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006123 * Call this after adding a new name for a file and it will properly
6124 * update the log to reflect the new name.
6125 *
6126 * It will return zero if all goes well, and it will return 1 if a
6127 * full transaction commit is required.
6128 */
6129int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006130 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Chris Mason12fcfd22009-03-24 10:24:20 -04006131 struct dentry *parent)
6132{
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006133 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Mason12fcfd22009-03-24 10:24:20 -04006134
6135 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006136 * this will force the logging code to walk the dentry chain
6137 * up for the file
6138 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006139 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006140 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006141
6142 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006143 * if this inode hasn't been logged and directory we're renaming it
6144 * from hasn't been logged, we don't need to log it
6145 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006146 if (inode->logged_trans <= fs_info->last_trans_committed &&
6147 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Chris Mason12fcfd22009-03-24 10:24:20 -04006148 return 0;
6149
Nikolay Borisovf8822742018-02-27 17:37:17 +02006150 return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6151 LOG_INODE_EXISTS, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04006152}
6153