blob: 98cbb31ec5a840338b5f4913789507f964120cad [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 Sterba093258e2018-02-26 16:15:17 +0100225 /* atomic_dec_and_test implies a barrier */
226 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500227 }
Chris Masone02119d2008-09-05 16:13:11 -0400228}
229
230
231/*
232 * the walk control struct is used to pass state down the chain when
233 * processing the log tree. The stage field tells us which part
234 * of the log tree processing we are currently doing. The others
235 * are state fields used for that specific part
236 */
237struct walk_control {
238 /* should we free the extent on disk when done? This is used
239 * at transaction commit time while freeing a log tree
240 */
241 int free;
242
243 /* should we write out the extent buffer? This is used
244 * while flushing the log tree to disk during a sync
245 */
246 int write;
247
248 /* should we wait for the extent buffer io to finish? Also used
249 * while flushing the log tree to disk for a sync
250 */
251 int wait;
252
253 /* pin only walk, we record which extents on disk belong to the
254 * log trees
255 */
256 int pin;
257
258 /* what stage of the replay code we're currently in */
259 int stage;
260
261 /* the root we are currently replaying */
262 struct btrfs_root *replay_dest;
263
264 /* the trans handle for the current replay */
265 struct btrfs_trans_handle *trans;
266
267 /* the function that gets used to process blocks we find in the
268 * tree. Note the extent_buffer might not be up to date when it is
269 * passed in, and it must be checked or read if you need the data
270 * inside it
271 */
272 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800273 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400274};
275
276/*
277 * process_func used to pin down extents, write them or wait on them
278 */
279static int process_one_buffer(struct btrfs_root *log,
280 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800281 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400282{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400283 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400284 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400285
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400286 /*
287 * If this fs is mixed then we need to be able to process the leaves to
288 * pin down any logged extents, so we have to read the block.
289 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800291 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400292 if (ret)
293 return ret;
294 }
295
Josef Bacikb50c6e22013-04-25 15:55:30 -0400296 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400297 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
298 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400299
300 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400301 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400302 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400303 if (wc->write)
304 btrfs_write_tree_block(eb);
305 if (wc->wait)
306 btrfs_wait_tree_block_writeback(eb);
307 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400308 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400309}
310
311/*
312 * Item overwrite used by replay and tree logging. eb, slot and key all refer
313 * to the src data we are copying out.
314 *
315 * root is the tree we are copying into, and path is a scratch
316 * path for use in this function (it should be released on entry and
317 * will be released on exit).
318 *
319 * If the key is already in the destination tree the existing item is
320 * overwritten. If the existing item isn't big enough, it is extended.
321 * If it is too large, it is truncated.
322 *
323 * If the key isn't in the destination yet, a new item is inserted.
324 */
325static noinline int overwrite_item(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
327 struct btrfs_path *path,
328 struct extent_buffer *eb, int slot,
329 struct btrfs_key *key)
330{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400331 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400332 int ret;
333 u32 item_size;
334 u64 saved_i_size = 0;
335 int save_old_i_size = 0;
336 unsigned long src_ptr;
337 unsigned long dst_ptr;
338 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000339 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400340
341 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
342 overwrite_root = 1;
343
344 item_size = btrfs_item_size_nr(eb, slot);
345 src_ptr = btrfs_item_ptr_offset(eb, slot);
346
347 /* look for the key in the destination tree */
348 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000349 if (ret < 0)
350 return ret;
351
Chris Masone02119d2008-09-05 16:13:11 -0400352 if (ret == 0) {
353 char *src_copy;
354 char *dst_copy;
355 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
356 path->slots[0]);
357 if (dst_size != item_size)
358 goto insert;
359
360 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200361 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400362 return 0;
363 }
364 dst_copy = kmalloc(item_size, GFP_NOFS);
365 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000366 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200367 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000368 kfree(dst_copy);
369 kfree(src_copy);
370 return -ENOMEM;
371 }
Chris Masone02119d2008-09-05 16:13:11 -0400372
373 read_extent_buffer(eb, src_copy, src_ptr, item_size);
374
375 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
376 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
377 item_size);
378 ret = memcmp(dst_copy, src_copy, item_size);
379
380 kfree(dst_copy);
381 kfree(src_copy);
382 /*
383 * they have the same contents, just return, this saves
384 * us from cowing blocks in the destination tree and doing
385 * extra writes that may not have been done by a previous
386 * sync
387 */
388 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200389 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400390 return 0;
391 }
392
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000393 /*
394 * We need to load the old nbytes into the inode so when we
395 * replay the extents we've logged we get the right nbytes.
396 */
397 if (inode_item) {
398 struct btrfs_inode_item *item;
399 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400400 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000401
402 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
403 struct btrfs_inode_item);
404 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
405 item = btrfs_item_ptr(eb, slot,
406 struct btrfs_inode_item);
407 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400408
409 /*
410 * If this is a directory we need to reset the i_size to
411 * 0 so that we can set it up properly when replaying
412 * the rest of the items in this log.
413 */
414 mode = btrfs_inode_mode(eb, item);
415 if (S_ISDIR(mode))
416 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000417 }
418 } else if (inode_item) {
419 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400420 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000421
422 /*
423 * New inode, set nbytes to 0 so that the nbytes comes out
424 * properly when we replay the extents.
425 */
426 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
427 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400428
429 /*
430 * If this is a directory we need to reset the i_size to 0 so
431 * that we can set it up properly when replaying the rest of
432 * the items in this log.
433 */
434 mode = btrfs_inode_mode(eb, item);
435 if (S_ISDIR(mode))
436 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400437 }
438insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200439 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400440 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000441 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400442 ret = btrfs_insert_empty_item(trans, root, path,
443 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000444 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400445
446 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000447 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400448 u32 found_size;
449 found_size = btrfs_item_size_nr(path->nodes[0],
450 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100451 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400452 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100453 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400454 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100455 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400456 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400457 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400458 }
459 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
460 path->slots[0]);
461
462 /* don't overwrite an existing inode if the generation number
463 * was logged as zero. This is done when the tree logging code
464 * is just logging an inode to make sure it exists after recovery.
465 *
466 * Also, don't overwrite i_size on directories during replay.
467 * log replay inserts and removes directory items based on the
468 * state of the tree found in the subvolume, and i_size is modified
469 * as it goes
470 */
471 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
472 struct btrfs_inode_item *src_item;
473 struct btrfs_inode_item *dst_item;
474
475 src_item = (struct btrfs_inode_item *)src_ptr;
476 dst_item = (struct btrfs_inode_item *)dst_ptr;
477
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000478 if (btrfs_inode_generation(eb, src_item) == 0) {
479 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000480 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000481
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000482 /*
483 * For regular files an ino_size == 0 is used only when
484 * logging that an inode exists, as part of a directory
485 * fsync, and the inode wasn't fsynced before. In this
486 * case don't set the size of the inode in the fs/subvol
487 * tree, otherwise we would be throwing valid data away.
488 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000489 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000490 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
491 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000492 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000493
494 btrfs_init_map_token(&token);
495 btrfs_set_token_inode_size(dst_eb, dst_item,
496 ino_size, &token);
497 }
Chris Masone02119d2008-09-05 16:13:11 -0400498 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000499 }
Chris Masone02119d2008-09-05 16:13:11 -0400500
501 if (overwrite_root &&
502 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
503 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
504 save_old_i_size = 1;
505 saved_i_size = btrfs_inode_size(path->nodes[0],
506 dst_item);
507 }
508 }
509
510 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
511 src_ptr, item_size);
512
513 if (save_old_i_size) {
514 struct btrfs_inode_item *dst_item;
515 dst_item = (struct btrfs_inode_item *)dst_ptr;
516 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
517 }
518
519 /* make sure the generation is filled in */
520 if (key->type == BTRFS_INODE_ITEM_KEY) {
521 struct btrfs_inode_item *dst_item;
522 dst_item = (struct btrfs_inode_item *)dst_ptr;
523 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
524 btrfs_set_inode_generation(path->nodes[0], dst_item,
525 trans->transid);
526 }
527 }
528no_copy:
529 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200530 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400531 return 0;
532}
533
534/*
535 * simple helper to read an inode off the disk from a given root
536 * This can only be called for subvolume roots and not for the log
537 */
538static noinline struct inode *read_one_inode(struct btrfs_root *root,
539 u64 objectid)
540{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400541 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400542 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400543
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400544 key.objectid = objectid;
545 key.type = BTRFS_INODE_ITEM_KEY;
546 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000547 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 if (IS_ERR(inode)) {
549 inode = NULL;
550 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400551 iput(inode);
552 inode = NULL;
553 }
554 return inode;
555}
556
557/* replays a single extent in 'eb' at 'slot' with 'key' into the
558 * subvolume 'root'. path is released on entry and should be released
559 * on exit.
560 *
561 * extents in the log tree have not been allocated out of the extent
562 * tree yet. So, this completes the allocation, taking a reference
563 * as required if the extent already exists or creating a new extent
564 * if it isn't in the extent allocation tree yet.
565 *
566 * The extent is inserted into the file, dropping any existing extents
567 * from the file that overlap the new one.
568 */
569static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 struct btrfs_path *path,
572 struct extent_buffer *eb, int slot,
573 struct btrfs_key *key)
574{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400575 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400576 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400577 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400578 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000579 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400580 struct btrfs_file_extent_item *item;
581 struct inode *inode = NULL;
582 unsigned long size;
583 int ret = 0;
584
585 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
586 found_type = btrfs_file_extent_type(eb, item);
587
Yan Zhengd899e052008-10-30 14:25:28 -0400588 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000589 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
590 nbytes = btrfs_file_extent_num_bytes(eb, item);
591 extent_end = start + nbytes;
592
593 /*
594 * We don't add to the inodes nbytes if we are prealloc or a
595 * hole.
596 */
597 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
598 nbytes = 0;
599 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800600 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000601 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400602 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400603 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400604 } else {
605 ret = 0;
606 goto out;
607 }
608
609 inode = read_one_inode(root, key->objectid);
610 if (!inode) {
611 ret = -EIO;
612 goto out;
613 }
614
615 /*
616 * first check to see if we already have this extent in the
617 * file. This must be done before the btrfs_drop_extents run
618 * so we don't try to drop this extent.
619 */
David Sterbaf85b7372017-01-20 14:54:07 +0100620 ret = btrfs_lookup_file_extent(trans, root, path,
621 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400622
Yan Zhengd899e052008-10-30 14:25:28 -0400623 if (ret == 0 &&
624 (found_type == BTRFS_FILE_EXTENT_REG ||
625 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400626 struct btrfs_file_extent_item cmp1;
627 struct btrfs_file_extent_item cmp2;
628 struct btrfs_file_extent_item *existing;
629 struct extent_buffer *leaf;
630
631 leaf = path->nodes[0];
632 existing = btrfs_item_ptr(leaf, path->slots[0],
633 struct btrfs_file_extent_item);
634
635 read_extent_buffer(eb, &cmp1, (unsigned long)item,
636 sizeof(cmp1));
637 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
638 sizeof(cmp2));
639
640 /*
641 * we already have a pointer to this exact extent,
642 * we don't have to do anything
643 */
644 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200645 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400646 goto out;
647 }
648 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200649 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400650
651 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400652 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400653 if (ret)
654 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400655
Yan Zheng07d400a2009-01-06 11:42:00 -0500656 if (found_type == BTRFS_FILE_EXTENT_REG ||
657 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500659 unsigned long dest_offset;
660 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400661
Filipe Manana3168021c2017-02-01 14:58:02 +0000662 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
663 btrfs_fs_incompat(fs_info, NO_HOLES))
664 goto update_inode;
665
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 ret = btrfs_insert_empty_item(trans, root, path, key,
667 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400668 if (ret)
669 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
671 path->slots[0]);
672 copy_extent_buffer(path->nodes[0], eb, dest_offset,
673 (unsigned long)item, sizeof(*item));
674
675 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
676 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
677 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400678 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500679
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800680 /*
681 * Manually record dirty extent, as here we did a shallow
682 * file extent item copy and skip normal backref update,
683 * but modifying extent tree all by ourselves.
684 * So need to manually record dirty extent for qgroup,
685 * as the owner of the file extent changed from log tree
686 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
687 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400688 ret = btrfs_qgroup_trace_extent(trans, fs_info,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800689 btrfs_file_extent_disk_bytenr(eb, item),
690 btrfs_file_extent_disk_num_bytes(eb, item),
691 GFP_NOFS);
692 if (ret < 0)
693 goto out;
694
Yan Zheng07d400a2009-01-06 11:42:00 -0500695 if (ins.objectid > 0) {
696 u64 csum_start;
697 u64 csum_end;
698 LIST_HEAD(ordered_sums);
699 /*
700 * is this extent already allocated in the extent
701 * allocation tree? If so, just add a reference
702 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400703 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500704 ins.offset);
705 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400706 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500707 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400708 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100709 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400710 if (ret)
711 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500712 } else {
713 /*
714 * insert the extent pointer in the extent
715 * allocation tree
716 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400718 fs_info,
719 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400720 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400721 if (ret)
722 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500723 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200724 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500725
726 if (btrfs_file_extent_compression(eb, item)) {
727 csum_start = ins.objectid;
728 csum_end = csum_start + ins.offset;
729 } else {
730 csum_start = ins.objectid +
731 btrfs_file_extent_offset(eb, item);
732 csum_end = csum_start +
733 btrfs_file_extent_num_bytes(eb, item);
734 }
735
736 ret = btrfs_lookup_csums_range(root->log_root,
737 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100738 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400739 if (ret)
740 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100741 /*
742 * Now delete all existing cums in the csum root that
743 * cover our range. We do this because we can have an
744 * extent that is completely referenced by one file
745 * extent item and partially referenced by another
746 * file extent item (like after using the clone or
747 * extent_same ioctls). In this case if we end up doing
748 * the replay of the one that partially references the
749 * extent first, and we do not do the csum deletion
750 * below, we can get 2 csum items in the csum tree that
751 * overlap each other. For example, imagine our log has
752 * the two following file extent items:
753 *
754 * key (257 EXTENT_DATA 409600)
755 * extent data disk byte 12845056 nr 102400
756 * extent data offset 20480 nr 20480 ram 102400
757 *
758 * key (257 EXTENT_DATA 819200)
759 * extent data disk byte 12845056 nr 102400
760 * extent data offset 0 nr 102400 ram 102400
761 *
762 * Where the second one fully references the 100K extent
763 * that starts at disk byte 12845056, and the log tree
764 * has a single csum item that covers the entire range
765 * of the extent:
766 *
767 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
768 *
769 * After the first file extent item is replayed, the
770 * csum tree gets the following csum item:
771 *
772 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
773 *
774 * Which covers the 20K sub-range starting at offset 20K
775 * of our extent. Now when we replay the second file
776 * extent item, if we do not delete existing csum items
777 * that cover any of its blocks, we end up getting two
778 * csum items in our csum tree that overlap each other:
779 *
780 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
781 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
782 *
783 * Which is a problem, because after this anyone trying
784 * to lookup up for the checksum of any block of our
785 * extent starting at an offset of 40K or higher, will
786 * end up looking at the second csum item only, which
787 * does not contain the checksum for any block starting
788 * at offset 40K or higher of our extent.
789 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500790 while (!list_empty(&ordered_sums)) {
791 struct btrfs_ordered_sum *sums;
792 sums = list_entry(ordered_sums.next,
793 struct btrfs_ordered_sum,
794 list);
Josef Bacik36508602013-04-25 16:23:32 -0400795 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400796 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400797 sums->bytenr,
798 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100799 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400800 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400801 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500802 list_del(&sums->list);
803 kfree(sums);
804 }
Josef Bacik36508602013-04-25 16:23:32 -0400805 if (ret)
806 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500807 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200808 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500809 }
810 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
811 /* inline extents are easy, we just overwrite them */
812 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400813 if (ret)
814 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500815 }
816
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000817 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000818update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600819 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400820out:
821 if (inode)
822 iput(inode);
823 return ret;
824}
825
826/*
827 * when cleaning up conflicts between the directory names in the
828 * subvolume, directory names in the log and directory names in the
829 * inode back references, we may have to unlink inodes from directories.
830 *
831 * This is a helper function to do the unlink of a specific directory
832 * item
833 */
834static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
835 struct btrfs_root *root,
836 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200837 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400838 struct btrfs_dir_item *di)
839{
840 struct inode *inode;
841 char *name;
842 int name_len;
843 struct extent_buffer *leaf;
844 struct btrfs_key location;
845 int ret;
846
847 leaf = path->nodes[0];
848
849 btrfs_dir_item_key_to_cpu(leaf, di, &location);
850 name_len = btrfs_dir_name_len(leaf, di);
851 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000852 if (!name)
853 return -ENOMEM;
854
Chris Masone02119d2008-09-05 16:13:11 -0400855 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200856 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400857
858 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000859 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400860 ret = -EIO;
861 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000862 }
Chris Masone02119d2008-09-05 16:13:11 -0400863
Yan Zhengec051c02009-01-05 15:43:42 -0500864 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400865 if (ret)
866 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400867
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200868 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
869 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400870 if (ret)
871 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100872 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200873 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400874out:
875 kfree(name);
876 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400877 return ret;
878}
879
880/*
881 * helper function to see if a given name and sequence number found
882 * in an inode back reference are already in a directory and correctly
883 * point to this inode
884 */
885static noinline int inode_in_dir(struct btrfs_root *root,
886 struct btrfs_path *path,
887 u64 dirid, u64 objectid, u64 index,
888 const char *name, int name_len)
889{
890 struct btrfs_dir_item *di;
891 struct btrfs_key location;
892 int match = 0;
893
894 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
895 index, name, name_len, 0);
896 if (di && !IS_ERR(di)) {
897 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
898 if (location.objectid != objectid)
899 goto out;
900 } else
901 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200902 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400903
904 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
905 if (di && !IS_ERR(di)) {
906 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
907 if (location.objectid != objectid)
908 goto out;
909 } else
910 goto out;
911 match = 1;
912out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200913 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400914 return match;
915}
916
917/*
918 * helper function to check a log tree for a named back reference in
919 * an inode. This is used to decide if a back reference that is
920 * found in the subvolume conflicts with what we find in the log.
921 *
922 * inode backreferences may have multiple refs in a single item,
923 * during replay we process one reference at a time, and we don't
924 * want to delete valid links to a file from the subvolume if that
925 * link is also in the log.
926 */
927static noinline int backref_in_log(struct btrfs_root *log,
928 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700929 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000930 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400931{
932 struct btrfs_path *path;
933 struct btrfs_inode_ref *ref;
934 unsigned long ptr;
935 unsigned long ptr_end;
936 unsigned long name_ptr;
937 int found_name_len;
938 int item_size;
939 int ret;
940 int match = 0;
941
942 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000943 if (!path)
944 return -ENOMEM;
945
Chris Masone02119d2008-09-05 16:13:11 -0400946 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
947 if (ret != 0)
948 goto out;
949
Chris Masone02119d2008-09-05 16:13:11 -0400950 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700951
952 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000953 if (btrfs_find_name_in_ext_backref(path->nodes[0],
954 path->slots[0],
955 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700956 name, namelen, NULL))
957 match = 1;
958
959 goto out;
960 }
961
962 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400963 ptr_end = ptr + item_size;
964 while (ptr < ptr_end) {
965 ref = (struct btrfs_inode_ref *)ptr;
966 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
967 if (found_name_len == namelen) {
968 name_ptr = (unsigned long)(ref + 1);
969 ret = memcmp_extent_buffer(path->nodes[0], name,
970 name_ptr, namelen);
971 if (ret == 0) {
972 match = 1;
973 goto out;
974 }
975 }
976 ptr = (unsigned long)(ref + 1) + found_name_len;
977 }
978out:
979 btrfs_free_path(path);
980 return match;
981}
982
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700983static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
984 struct btrfs_root *root,
985 struct btrfs_path *path,
986 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200987 struct btrfs_inode *dir,
988 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -0700989 u64 inode_objectid, u64 parent_objectid,
990 u64 ref_index, char *name, int namelen,
991 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700992{
993 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700994 char *victim_name;
995 int victim_name_len;
996 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700997 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700998 struct btrfs_key search_key;
999 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001000
Mark Fashehf1863732012-08-08 11:32:27 -07001001again:
1002 /* Search old style refs */
1003 search_key.objectid = inode_objectid;
1004 search_key.type = BTRFS_INODE_REF_KEY;
1005 search_key.offset = parent_objectid;
1006 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001007 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001008 struct btrfs_inode_ref *victim_ref;
1009 unsigned long ptr;
1010 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001011
1012 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001013
1014 /* are we trying to overwrite a back ref for the root directory
1015 * if so, just jump out, we're done
1016 */
Mark Fashehf1863732012-08-08 11:32:27 -07001017 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001018 return 1;
1019
1020 /* check all the names in this back reference to see
1021 * if they are in the log. if so, we allow them to stay
1022 * otherwise they must be unlinked as a conflict
1023 */
1024 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1025 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1026 while (ptr < ptr_end) {
1027 victim_ref = (struct btrfs_inode_ref *)ptr;
1028 victim_name_len = btrfs_inode_ref_name_len(leaf,
1029 victim_ref);
1030 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001031 if (!victim_name)
1032 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001033
1034 read_extent_buffer(leaf, victim_name,
1035 (unsigned long)(victim_ref + 1),
1036 victim_name_len);
1037
Mark Fashehf1863732012-08-08 11:32:27 -07001038 if (!backref_in_log(log_root, &search_key,
1039 parent_objectid,
1040 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001041 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001042 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001043 btrfs_release_path(path);
1044
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001045 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001046 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001047 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001048 if (ret)
1049 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001050 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001051 if (ret)
1052 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001053 *search_done = 1;
1054 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055 }
1056 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001057
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1059 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001060
1061 /*
1062 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001063 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001064 */
1065 *search_done = 1;
1066 }
1067 btrfs_release_path(path);
1068
Mark Fashehf1863732012-08-08 11:32:27 -07001069 /* Same search but for extended refs */
1070 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1071 inode_objectid, parent_objectid, 0,
1072 0);
1073 if (!IS_ERR_OR_NULL(extref)) {
1074 u32 item_size;
1075 u32 cur_offset = 0;
1076 unsigned long base;
1077 struct inode *victim_parent;
1078
1079 leaf = path->nodes[0];
1080
1081 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1082 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1083
1084 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001085 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001086
1087 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1088
1089 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1090 goto next;
1091
1092 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001093 if (!victim_name)
1094 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001095 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1096 victim_name_len);
1097
1098 search_key.objectid = inode_objectid;
1099 search_key.type = BTRFS_INODE_EXTREF_KEY;
1100 search_key.offset = btrfs_extref_hash(parent_objectid,
1101 victim_name,
1102 victim_name_len);
1103 ret = 0;
1104 if (!backref_in_log(log_root, &search_key,
1105 parent_objectid, victim_name,
1106 victim_name_len)) {
1107 ret = -ENOENT;
1108 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001109 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001110 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001111 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001112 btrfs_release_path(path);
1113
1114 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001115 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001116 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001117 victim_name,
1118 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001119 if (!ret)
1120 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001121 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001122 }
Mark Fashehf1863732012-08-08 11:32:27 -07001123 iput(victim_parent);
1124 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001125 if (ret)
1126 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001127 *search_done = 1;
1128 goto again;
1129 }
1130 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001131next:
1132 cur_offset += victim_name_len + sizeof(*extref);
1133 }
1134 *search_done = 1;
1135 }
1136 btrfs_release_path(path);
1137
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001138 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001139 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001140 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001141 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001142 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001143 if (ret)
1144 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001145 }
1146 btrfs_release_path(path);
1147
1148 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001149 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001150 name, namelen, 0);
1151 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001152 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001153 if (ret)
1154 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001155 }
1156 btrfs_release_path(path);
1157
1158 return 0;
1159}
Chris Masone02119d2008-09-05 16:13:11 -04001160
Qu Wenruobae15d92017-11-08 08:54:26 +08001161static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1162 u32 *namelen, char **name, u64 *index,
1163 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001164{
1165 struct btrfs_inode_extref *extref;
1166
1167 extref = (struct btrfs_inode_extref *)ref_ptr;
1168
1169 *namelen = btrfs_inode_extref_name_len(eb, extref);
1170 *name = kmalloc(*namelen, GFP_NOFS);
1171 if (*name == NULL)
1172 return -ENOMEM;
1173
1174 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1175 *namelen);
1176
Filipe Manana1f250e92018-02-28 15:56:10 +00001177 if (index)
1178 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001179 if (parent_objectid)
1180 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1181
1182 return 0;
1183}
1184
Qu Wenruobae15d92017-11-08 08:54:26 +08001185static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1186 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001187{
1188 struct btrfs_inode_ref *ref;
1189
1190 ref = (struct btrfs_inode_ref *)ref_ptr;
1191
1192 *namelen = btrfs_inode_ref_name_len(eb, ref);
1193 *name = kmalloc(*namelen, GFP_NOFS);
1194 if (*name == NULL)
1195 return -ENOMEM;
1196
1197 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1198
Filipe Manana1f250e92018-02-28 15:56:10 +00001199 if (index)
1200 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001201
1202 return 0;
1203}
1204
Chris Masone02119d2008-09-05 16:13:11 -04001205/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001206 * Take an inode reference item from the log tree and iterate all names from the
1207 * inode reference item in the subvolume tree with the same key (if it exists).
1208 * For any name that is not in the inode reference item from the log tree, do a
1209 * proper unlink of that name (that is, remove its entry from the inode
1210 * reference item and both dir index keys).
1211 */
1212static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1213 struct btrfs_root *root,
1214 struct btrfs_path *path,
1215 struct btrfs_inode *inode,
1216 struct extent_buffer *log_eb,
1217 int log_slot,
1218 struct btrfs_key *key)
1219{
1220 int ret;
1221 unsigned long ref_ptr;
1222 unsigned long ref_end;
1223 struct extent_buffer *eb;
1224
1225again:
1226 btrfs_release_path(path);
1227 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1228 if (ret > 0) {
1229 ret = 0;
1230 goto out;
1231 }
1232 if (ret < 0)
1233 goto out;
1234
1235 eb = path->nodes[0];
1236 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1237 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1238 while (ref_ptr < ref_end) {
1239 char *name = NULL;
1240 int namelen;
1241 u64 parent_id;
1242
1243 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1244 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1245 NULL, &parent_id);
1246 } else {
1247 parent_id = key->offset;
1248 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1249 NULL);
1250 }
1251 if (ret)
1252 goto out;
1253
1254 if (key->type == BTRFS_INODE_EXTREF_KEY)
1255 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1256 parent_id, name,
1257 namelen, NULL);
1258 else
1259 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1260 namelen, NULL);
1261
1262 if (!ret) {
1263 struct inode *dir;
1264
1265 btrfs_release_path(path);
1266 dir = read_one_inode(root, parent_id);
1267 if (!dir) {
1268 ret = -ENOENT;
1269 kfree(name);
1270 goto out;
1271 }
1272 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1273 inode, name, namelen);
1274 kfree(name);
1275 iput(dir);
1276 if (ret)
1277 goto out;
1278 goto again;
1279 }
1280
1281 kfree(name);
1282 ref_ptr += namelen;
1283 if (key->type == BTRFS_INODE_EXTREF_KEY)
1284 ref_ptr += sizeof(struct btrfs_inode_extref);
1285 else
1286 ref_ptr += sizeof(struct btrfs_inode_ref);
1287 }
1288 ret = 0;
1289 out:
1290 btrfs_release_path(path);
1291 return ret;
1292}
1293
1294/*
Chris Masone02119d2008-09-05 16:13:11 -04001295 * replay one inode back reference item found in the log tree.
1296 * eb, slot and key refer to the buffer and key found in the log tree.
1297 * root is the destination we are replaying into, and path is for temp
1298 * use by this function. (it should be released on return).
1299 */
1300static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1301 struct btrfs_root *root,
1302 struct btrfs_root *log,
1303 struct btrfs_path *path,
1304 struct extent_buffer *eb, int slot,
1305 struct btrfs_key *key)
1306{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001307 struct inode *dir = NULL;
1308 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001309 unsigned long ref_ptr;
1310 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001311 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001312 int namelen;
1313 int ret;
liuboc622ae62011-03-26 08:01:12 -04001314 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001315 int log_ref_ver = 0;
1316 u64 parent_objectid;
1317 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001318 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001319 int ref_struct_size;
1320
1321 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1322 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1323
1324 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1325 struct btrfs_inode_extref *r;
1326
1327 ref_struct_size = sizeof(struct btrfs_inode_extref);
1328 log_ref_ver = 1;
1329 r = (struct btrfs_inode_extref *)ref_ptr;
1330 parent_objectid = btrfs_inode_extref_parent(eb, r);
1331 } else {
1332 ref_struct_size = sizeof(struct btrfs_inode_ref);
1333 parent_objectid = key->offset;
1334 }
1335 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001336
Chris Masone02119d2008-09-05 16:13:11 -04001337 /*
1338 * it is possible that we didn't log all the parent directories
1339 * for a given inode. If we don't find the dir, just don't
1340 * copy the back ref in. The link count fixup code will take
1341 * care of the rest
1342 */
Mark Fashehf1863732012-08-08 11:32:27 -07001343 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001344 if (!dir) {
1345 ret = -ENOENT;
1346 goto out;
1347 }
Chris Masone02119d2008-09-05 16:13:11 -04001348
Mark Fashehf1863732012-08-08 11:32:27 -07001349 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001350 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001351 ret = -EIO;
1352 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001353 }
Chris Masone02119d2008-09-05 16:13:11 -04001354
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001355 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001356 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001357 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1358 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001359 /*
1360 * parent object can change from one array
1361 * item to another.
1362 */
1363 if (!dir)
1364 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001365 if (!dir) {
1366 ret = -ENOENT;
1367 goto out;
1368 }
Mark Fashehf1863732012-08-08 11:32:27 -07001369 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001370 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1371 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001372 }
1373 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001374 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001375
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001376 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001377 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1378 btrfs_ino(BTRFS_I(inode)), ref_index,
1379 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001380 /*
1381 * look for a conflicting back reference in the
1382 * metadata. if we find one we have to unlink that name
1383 * of the file before we add our new link. Later on, we
1384 * overwrite any existing back reference, and we don't
1385 * want to create dangling pointers in the directory.
1386 */
Chris Masone02119d2008-09-05 16:13:11 -04001387
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001388 if (!search_done) {
1389 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001390 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001391 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001392 inode_objectid,
1393 parent_objectid,
1394 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001395 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001396 if (ret) {
1397 if (ret == 1)
1398 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001399 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001400 }
Chris Masone02119d2008-09-05 16:13:11 -04001401 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001402
1403 /* insert our name */
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001404 ret = btrfs_add_link(trans, BTRFS_I(dir),
1405 BTRFS_I(inode),
1406 name, namelen, 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001407 if (ret)
1408 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001409
1410 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001411 }
liuboc622ae62011-03-26 08:01:12 -04001412
Mark Fashehf1863732012-08-08 11:32:27 -07001413 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001414 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001415 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001416 if (log_ref_ver) {
1417 iput(dir);
1418 dir = NULL;
1419 }
Chris Masone02119d2008-09-05 16:13:11 -04001420 }
Chris Masone02119d2008-09-05 16:13:11 -04001421
Filipe Manana1f250e92018-02-28 15:56:10 +00001422 /*
1423 * Before we overwrite the inode reference item in the subvolume tree
1424 * with the item from the log tree, we must unlink all names from the
1425 * parent directory that are in the subvolume's tree inode reference
1426 * item, otherwise we end up with an inconsistent subvolume tree where
1427 * dir index entries exist for a name but there is no inode reference
1428 * item with the same name.
1429 */
1430 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1431 key);
1432 if (ret)
1433 goto out;
1434
Chris Masone02119d2008-09-05 16:13:11 -04001435 /* finally write the back reference in the inode */
1436 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001437out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001438 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001439 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001440 iput(dir);
1441 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001442 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001443}
1444
Yan, Zhengc71bf092009-11-12 09:34:40 +00001445static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001446 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001447{
1448 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001449
David Sterba9c4f61f2015-01-02 19:12:57 +01001450 ret = btrfs_insert_orphan_item(trans, root, ino);
1451 if (ret == -EEXIST)
1452 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001453
Yan, Zhengc71bf092009-11-12 09:34:40 +00001454 return ret;
1455}
1456
Mark Fashehf1863732012-08-08 11:32:27 -07001457static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001458 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001459{
Mark Fashehf1863732012-08-08 11:32:27 -07001460 int ret = 0;
1461 int name_len;
1462 unsigned int nlink = 0;
1463 u32 item_size;
1464 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001465 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001466 u64 offset = 0;
1467 unsigned long ptr;
1468 struct btrfs_inode_extref *extref;
1469 struct extent_buffer *leaf;
1470
1471 while (1) {
1472 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1473 &extref, &offset);
1474 if (ret)
1475 break;
1476
1477 leaf = path->nodes[0];
1478 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1479 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001480 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001481
1482 while (cur_offset < item_size) {
1483 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1484 name_len = btrfs_inode_extref_name_len(leaf, extref);
1485
1486 nlink++;
1487
1488 cur_offset += name_len + sizeof(*extref);
1489 }
1490
1491 offset++;
1492 btrfs_release_path(path);
1493 }
1494 btrfs_release_path(path);
1495
Filipe Manana2c2c4522015-01-13 16:40:04 +00001496 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001497 return ret;
1498 return nlink;
1499}
1500
1501static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001502 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001503{
Chris Masone02119d2008-09-05 16:13:11 -04001504 int ret;
1505 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001506 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001507 unsigned long ptr;
1508 unsigned long ptr_end;
1509 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001510 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001511
Li Zefan33345d012011-04-20 10:31:50 +08001512 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001513 key.type = BTRFS_INODE_REF_KEY;
1514 key.offset = (u64)-1;
1515
Chris Masond3977122009-01-05 21:25:51 -05001516 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001517 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1518 if (ret < 0)
1519 break;
1520 if (ret > 0) {
1521 if (path->slots[0] == 0)
1522 break;
1523 path->slots[0]--;
1524 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001525process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001526 btrfs_item_key_to_cpu(path->nodes[0], &key,
1527 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001528 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001529 key.type != BTRFS_INODE_REF_KEY)
1530 break;
1531 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1532 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1533 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001534 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001535 struct btrfs_inode_ref *ref;
1536
1537 ref = (struct btrfs_inode_ref *)ptr;
1538 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1539 ref);
1540 ptr = (unsigned long)(ref + 1) + name_len;
1541 nlink++;
1542 }
1543
1544 if (key.offset == 0)
1545 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001546 if (path->slots[0] > 0) {
1547 path->slots[0]--;
1548 goto process_slot;
1549 }
Chris Masone02119d2008-09-05 16:13:11 -04001550 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001551 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001552 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001553 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001554
1555 return nlink;
1556}
1557
1558/*
1559 * There are a few corners where the link count of the file can't
1560 * be properly maintained during replay. So, instead of adding
1561 * lots of complexity to the log code, we just scan the backrefs
1562 * for any file that has been through replay.
1563 *
1564 * The scan will update the link count on the inode to reflect the
1565 * number of back refs found. If it goes down to zero, the iput
1566 * will free the inode.
1567 */
1568static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1569 struct btrfs_root *root,
1570 struct inode *inode)
1571{
1572 struct btrfs_path *path;
1573 int ret;
1574 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001575 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001576
1577 path = btrfs_alloc_path();
1578 if (!path)
1579 return -ENOMEM;
1580
Nikolay Borisovf329e312017-01-18 00:31:50 +02001581 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001582 if (ret < 0)
1583 goto out;
1584
1585 nlink = ret;
1586
Nikolay Borisov36283652017-01-18 00:31:49 +02001587 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001588 if (ret < 0)
1589 goto out;
1590
1591 nlink += ret;
1592
1593 ret = 0;
1594
Chris Masone02119d2008-09-05 16:13:11 -04001595 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001596 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001597 btrfs_update_inode(trans, root, inode);
1598 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001599 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001600
Yan, Zhengc71bf092009-11-12 09:34:40 +00001601 if (inode->i_nlink == 0) {
1602 if (S_ISDIR(inode->i_mode)) {
1603 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001604 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001605 if (ret)
1606 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001607 }
Li Zefan33345d012011-04-20 10:31:50 +08001608 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001609 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001610
Mark Fashehf1863732012-08-08 11:32:27 -07001611out:
1612 btrfs_free_path(path);
1613 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001614}
1615
1616static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1617 struct btrfs_root *root,
1618 struct btrfs_path *path)
1619{
1620 int ret;
1621 struct btrfs_key key;
1622 struct inode *inode;
1623
1624 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1625 key.type = BTRFS_ORPHAN_ITEM_KEY;
1626 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001627 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001628 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1629 if (ret < 0)
1630 break;
1631
1632 if (ret == 1) {
1633 if (path->slots[0] == 0)
1634 break;
1635 path->slots[0]--;
1636 }
1637
1638 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1639 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1640 key.type != BTRFS_ORPHAN_ITEM_KEY)
1641 break;
1642
1643 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001644 if (ret)
1645 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001646
David Sterbab3b4aa72011-04-21 01:20:15 +02001647 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001648 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001649 if (!inode)
1650 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001651
1652 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001653 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001654 if (ret)
1655 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001656
Chris Mason12fcfd22009-03-24 10:24:20 -04001657 /*
1658 * fixup on a directory may create new entries,
1659 * make sure we always look for the highset possible
1660 * offset
1661 */
1662 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001663 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001664 ret = 0;
1665out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001666 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001667 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001668}
1669
1670
1671/*
1672 * record a given inode in the fixup dir so we can check its link
1673 * count when replay is done. The link count is incremented here
1674 * so the inode won't go away until we check it
1675 */
1676static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1677 struct btrfs_root *root,
1678 struct btrfs_path *path,
1679 u64 objectid)
1680{
1681 struct btrfs_key key;
1682 int ret = 0;
1683 struct inode *inode;
1684
1685 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001686 if (!inode)
1687 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001688
1689 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001690 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001691 key.offset = objectid;
1692
1693 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1694
David Sterbab3b4aa72011-04-21 01:20:15 +02001695 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001696 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001697 if (!inode->i_nlink)
1698 set_nlink(inode, 1);
1699 else
Zach Brown8b558c52013-10-16 12:10:34 -07001700 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001701 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001702 } else if (ret == -EEXIST) {
1703 ret = 0;
1704 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001705 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001706 }
1707 iput(inode);
1708
1709 return ret;
1710}
1711
1712/*
1713 * when replaying the log for a directory, we only insert names
1714 * for inodes that actually exist. This means an fsync on a directory
1715 * does not implicitly fsync all the new files in it
1716 */
1717static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1718 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001719 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001720 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001721 struct btrfs_key *location)
1722{
1723 struct inode *inode;
1724 struct inode *dir;
1725 int ret;
1726
1727 inode = read_one_inode(root, location->objectid);
1728 if (!inode)
1729 return -ENOENT;
1730
1731 dir = read_one_inode(root, dirid);
1732 if (!dir) {
1733 iput(inode);
1734 return -EIO;
1735 }
Josef Bacikd5554382013-09-11 14:17:00 -04001736
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001737 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1738 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001739
1740 /* FIXME, put inode into FIXUP list */
1741
1742 iput(inode);
1743 iput(dir);
1744 return ret;
1745}
1746
1747/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001748 * Return true if an inode reference exists in the log for the given name,
1749 * inode and parent inode.
1750 */
1751static bool name_in_log_ref(struct btrfs_root *log_root,
1752 const char *name, const int name_len,
1753 const u64 dirid, const u64 ino)
1754{
1755 struct btrfs_key search_key;
1756
1757 search_key.objectid = ino;
1758 search_key.type = BTRFS_INODE_REF_KEY;
1759 search_key.offset = dirid;
1760 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1761 return true;
1762
1763 search_key.type = BTRFS_INODE_EXTREF_KEY;
1764 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1765 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1766 return true;
1767
1768 return false;
1769}
1770
1771/*
Chris Masone02119d2008-09-05 16:13:11 -04001772 * take a single entry in a log directory item and replay it into
1773 * the subvolume.
1774 *
1775 * if a conflicting item exists in the subdirectory already,
1776 * the inode it points to is unlinked and put into the link count
1777 * fix up tree.
1778 *
1779 * If a name from the log points to a file or directory that does
1780 * not exist in the FS, it is skipped. fsyncs on directories
1781 * do not force down inodes inside that directory, just changes to the
1782 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001783 *
1784 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1785 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001786 */
1787static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1788 struct btrfs_root *root,
1789 struct btrfs_path *path,
1790 struct extent_buffer *eb,
1791 struct btrfs_dir_item *di,
1792 struct btrfs_key *key)
1793{
1794 char *name;
1795 int name_len;
1796 struct btrfs_dir_item *dst_di;
1797 struct btrfs_key found_key;
1798 struct btrfs_key log_key;
1799 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001800 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001801 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001802 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001803 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001804 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001805
1806 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001807 if (!dir)
1808 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001809
1810 name_len = btrfs_dir_name_len(eb, di);
1811 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001812 if (!name) {
1813 ret = -ENOMEM;
1814 goto out;
1815 }
liubo2a29edc2011-01-26 06:22:08 +00001816
Chris Masone02119d2008-09-05 16:13:11 -04001817 log_type = btrfs_dir_type(eb, di);
1818 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1819 name_len);
1820
1821 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001822 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1823 if (exists == 0)
1824 exists = 1;
1825 else
1826 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001827 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001828
Chris Masone02119d2008-09-05 16:13:11 -04001829 if (key->type == BTRFS_DIR_ITEM_KEY) {
1830 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1831 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001832 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001833 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1834 key->objectid,
1835 key->offset, name,
1836 name_len, 1);
1837 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001838 /* Corruption */
1839 ret = -EINVAL;
1840 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001841 }
David Sterbac7040052011-04-19 18:00:01 +02001842 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001843 /* we need a sequence number to insert, so we only
1844 * do inserts for the BTRFS_DIR_INDEX_KEY types
1845 */
1846 if (key->type != BTRFS_DIR_INDEX_KEY)
1847 goto out;
1848 goto insert;
1849 }
1850
1851 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1852 /* the existing item matches the logged item */
1853 if (found_key.objectid == log_key.objectid &&
1854 found_key.type == log_key.type &&
1855 found_key.offset == log_key.offset &&
1856 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001857 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001858 goto out;
1859 }
1860
1861 /*
1862 * don't drop the conflicting directory entry if the inode
1863 * for the new entry doesn't exist
1864 */
Chris Mason4bef0842008-09-08 11:18:08 -04001865 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001866 goto out;
1867
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001868 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001869 if (ret)
1870 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001871
1872 if (key->type == BTRFS_DIR_INDEX_KEY)
1873 goto insert;
1874out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001875 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001876 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001877 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001878 ret = btrfs_update_inode(trans, root, dir);
1879 }
Chris Masone02119d2008-09-05 16:13:11 -04001880 kfree(name);
1881 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001882 if (!ret && name_added)
1883 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001884 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001885
1886insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001887 if (name_in_log_ref(root->log_root, name, name_len,
1888 key->objectid, log_key.objectid)) {
1889 /* The dentry will be added later. */
1890 ret = 0;
1891 update_size = false;
1892 goto out;
1893 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001894 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001895 ret = insert_one_name(trans, root, key->objectid, key->offset,
1896 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001897 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001898 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001899 if (!ret)
1900 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001901 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001902 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001903 goto out;
1904}
1905
1906/*
1907 * find all the names in a directory item and reconcile them into
1908 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1909 * one name in a directory item, but the same code gets used for
1910 * both directory index types
1911 */
1912static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1913 struct btrfs_root *root,
1914 struct btrfs_path *path,
1915 struct extent_buffer *eb, int slot,
1916 struct btrfs_key *key)
1917{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001918 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001919 u32 item_size = btrfs_item_size_nr(eb, slot);
1920 struct btrfs_dir_item *di;
1921 int name_len;
1922 unsigned long ptr;
1923 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001924 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001925
1926 ptr = btrfs_item_ptr_offset(eb, slot);
1927 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001928 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001929 di = (struct btrfs_dir_item *)ptr;
1930 name_len = btrfs_dir_name_len(eb, di);
1931 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001932 if (ret < 0)
1933 break;
Chris Masone02119d2008-09-05 16:13:11 -04001934 ptr = (unsigned long)(di + 1);
1935 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001936
1937 /*
1938 * If this entry refers to a non-directory (directories can not
1939 * have a link count > 1) and it was added in the transaction
1940 * that was not committed, make sure we fixup the link count of
1941 * the inode it the entry points to. Otherwise something like
1942 * the following would result in a directory pointing to an
1943 * inode with a wrong link that does not account for this dir
1944 * entry:
1945 *
1946 * mkdir testdir
1947 * touch testdir/foo
1948 * touch testdir/bar
1949 * sync
1950 *
1951 * ln testdir/bar testdir/bar_link
1952 * ln testdir/foo testdir/foo_link
1953 * xfs_io -c "fsync" testdir/bar
1954 *
1955 * <power failure>
1956 *
1957 * mount fs, log replay happens
1958 *
1959 * File foo would remain with a link count of 1 when it has two
1960 * entries pointing to it in the directory testdir. This would
1961 * make it impossible to ever delete the parent directory has
1962 * it would result in stale dentries that can never be deleted.
1963 */
1964 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1965 struct btrfs_key di_key;
1966
1967 if (!fixup_path) {
1968 fixup_path = btrfs_alloc_path();
1969 if (!fixup_path) {
1970 ret = -ENOMEM;
1971 break;
1972 }
1973 }
1974
1975 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1976 ret = link_to_fixup_dir(trans, root, fixup_path,
1977 di_key.objectid);
1978 if (ret)
1979 break;
1980 }
1981 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001982 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001983 btrfs_free_path(fixup_path);
1984 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001985}
1986
1987/*
1988 * directory replay has two parts. There are the standard directory
1989 * items in the log copied from the subvolume, and range items
1990 * created in the log while the subvolume was logged.
1991 *
1992 * The range items tell us which parts of the key space the log
1993 * is authoritative for. During replay, if a key in the subvolume
1994 * directory is in a logged range item, but not actually in the log
1995 * that means it was deleted from the directory before the fsync
1996 * and should be removed.
1997 */
1998static noinline int find_dir_range(struct btrfs_root *root,
1999 struct btrfs_path *path,
2000 u64 dirid, int key_type,
2001 u64 *start_ret, u64 *end_ret)
2002{
2003 struct btrfs_key key;
2004 u64 found_end;
2005 struct btrfs_dir_log_item *item;
2006 int ret;
2007 int nritems;
2008
2009 if (*start_ret == (u64)-1)
2010 return 1;
2011
2012 key.objectid = dirid;
2013 key.type = key_type;
2014 key.offset = *start_ret;
2015
2016 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2017 if (ret < 0)
2018 goto out;
2019 if (ret > 0) {
2020 if (path->slots[0] == 0)
2021 goto out;
2022 path->slots[0]--;
2023 }
2024 if (ret != 0)
2025 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2026
2027 if (key.type != key_type || key.objectid != dirid) {
2028 ret = 1;
2029 goto next;
2030 }
2031 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2032 struct btrfs_dir_log_item);
2033 found_end = btrfs_dir_log_end(path->nodes[0], item);
2034
2035 if (*start_ret >= key.offset && *start_ret <= found_end) {
2036 ret = 0;
2037 *start_ret = key.offset;
2038 *end_ret = found_end;
2039 goto out;
2040 }
2041 ret = 1;
2042next:
2043 /* check the next slot in the tree to see if it is a valid item */
2044 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002045 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002046 if (path->slots[0] >= nritems) {
2047 ret = btrfs_next_leaf(root, path);
2048 if (ret)
2049 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002050 }
2051
2052 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2053
2054 if (key.type != key_type || key.objectid != dirid) {
2055 ret = 1;
2056 goto out;
2057 }
2058 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2059 struct btrfs_dir_log_item);
2060 found_end = btrfs_dir_log_end(path->nodes[0], item);
2061 *start_ret = key.offset;
2062 *end_ret = found_end;
2063 ret = 0;
2064out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002065 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002066 return ret;
2067}
2068
2069/*
2070 * this looks for a given directory item in the log. If the directory
2071 * item is not in the log, the item is removed and the inode it points
2072 * to is unlinked
2073 */
2074static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2075 struct btrfs_root *root,
2076 struct btrfs_root *log,
2077 struct btrfs_path *path,
2078 struct btrfs_path *log_path,
2079 struct inode *dir,
2080 struct btrfs_key *dir_key)
2081{
2082 int ret;
2083 struct extent_buffer *eb;
2084 int slot;
2085 u32 item_size;
2086 struct btrfs_dir_item *di;
2087 struct btrfs_dir_item *log_di;
2088 int name_len;
2089 unsigned long ptr;
2090 unsigned long ptr_end;
2091 char *name;
2092 struct inode *inode;
2093 struct btrfs_key location;
2094
2095again:
2096 eb = path->nodes[0];
2097 slot = path->slots[0];
2098 item_size = btrfs_item_size_nr(eb, slot);
2099 ptr = btrfs_item_ptr_offset(eb, slot);
2100 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002101 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002102 di = (struct btrfs_dir_item *)ptr;
2103 name_len = btrfs_dir_name_len(eb, di);
2104 name = kmalloc(name_len, GFP_NOFS);
2105 if (!name) {
2106 ret = -ENOMEM;
2107 goto out;
2108 }
2109 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2110 name_len);
2111 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002112 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002113 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2114 dir_key->objectid,
2115 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002116 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002117 log_di = btrfs_lookup_dir_index_item(trans, log,
2118 log_path,
2119 dir_key->objectid,
2120 dir_key->offset,
2121 name, name_len, 0);
2122 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002123 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002124 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002125 btrfs_release_path(path);
2126 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002127 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002128 if (!inode) {
2129 kfree(name);
2130 return -EIO;
2131 }
Chris Masone02119d2008-09-05 16:13:11 -04002132
2133 ret = link_to_fixup_dir(trans, root,
2134 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002135 if (ret) {
2136 kfree(name);
2137 iput(inode);
2138 goto out;
2139 }
2140
Zach Brown8b558c52013-10-16 12:10:34 -07002141 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002142 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2143 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002144 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002145 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002146 kfree(name);
2147 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002148 if (ret)
2149 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002150
2151 /* there might still be more names under this key
2152 * check and repeat if required
2153 */
2154 ret = btrfs_search_slot(NULL, root, dir_key, path,
2155 0, 0);
2156 if (ret == 0)
2157 goto again;
2158 ret = 0;
2159 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002160 } else if (IS_ERR(log_di)) {
2161 kfree(name);
2162 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002163 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002164 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002165 kfree(name);
2166
2167 ptr = (unsigned long)(di + 1);
2168 ptr += name_len;
2169 }
2170 ret = 0;
2171out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002172 btrfs_release_path(path);
2173 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002174 return ret;
2175}
2176
Filipe Manana4f764e52015-02-23 19:53:35 +00002177static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *root,
2179 struct btrfs_root *log,
2180 struct btrfs_path *path,
2181 const u64 ino)
2182{
2183 struct btrfs_key search_key;
2184 struct btrfs_path *log_path;
2185 int i;
2186 int nritems;
2187 int ret;
2188
2189 log_path = btrfs_alloc_path();
2190 if (!log_path)
2191 return -ENOMEM;
2192
2193 search_key.objectid = ino;
2194 search_key.type = BTRFS_XATTR_ITEM_KEY;
2195 search_key.offset = 0;
2196again:
2197 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2198 if (ret < 0)
2199 goto out;
2200process_leaf:
2201 nritems = btrfs_header_nritems(path->nodes[0]);
2202 for (i = path->slots[0]; i < nritems; i++) {
2203 struct btrfs_key key;
2204 struct btrfs_dir_item *di;
2205 struct btrfs_dir_item *log_di;
2206 u32 total_size;
2207 u32 cur;
2208
2209 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2210 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2211 ret = 0;
2212 goto out;
2213 }
2214
2215 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2216 total_size = btrfs_item_size_nr(path->nodes[0], i);
2217 cur = 0;
2218 while (cur < total_size) {
2219 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2220 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2221 u32 this_len = sizeof(*di) + name_len + data_len;
2222 char *name;
2223
2224 name = kmalloc(name_len, GFP_NOFS);
2225 if (!name) {
2226 ret = -ENOMEM;
2227 goto out;
2228 }
2229 read_extent_buffer(path->nodes[0], name,
2230 (unsigned long)(di + 1), name_len);
2231
2232 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2233 name, name_len, 0);
2234 btrfs_release_path(log_path);
2235 if (!log_di) {
2236 /* Doesn't exist in log tree, so delete it. */
2237 btrfs_release_path(path);
2238 di = btrfs_lookup_xattr(trans, root, path, ino,
2239 name, name_len, -1);
2240 kfree(name);
2241 if (IS_ERR(di)) {
2242 ret = PTR_ERR(di);
2243 goto out;
2244 }
2245 ASSERT(di);
2246 ret = btrfs_delete_one_dir_name(trans, root,
2247 path, di);
2248 if (ret)
2249 goto out;
2250 btrfs_release_path(path);
2251 search_key = key;
2252 goto again;
2253 }
2254 kfree(name);
2255 if (IS_ERR(log_di)) {
2256 ret = PTR_ERR(log_di);
2257 goto out;
2258 }
2259 cur += this_len;
2260 di = (struct btrfs_dir_item *)((char *)di + this_len);
2261 }
2262 }
2263 ret = btrfs_next_leaf(root, path);
2264 if (ret > 0)
2265 ret = 0;
2266 else if (ret == 0)
2267 goto process_leaf;
2268out:
2269 btrfs_free_path(log_path);
2270 btrfs_release_path(path);
2271 return ret;
2272}
2273
2274
Chris Masone02119d2008-09-05 16:13:11 -04002275/*
2276 * deletion replay happens before we copy any new directory items
2277 * out of the log or out of backreferences from inodes. It
2278 * scans the log to find ranges of keys that log is authoritative for,
2279 * and then scans the directory to find items in those ranges that are
2280 * not present in the log.
2281 *
2282 * Anything we don't find in the log is unlinked and removed from the
2283 * directory.
2284 */
2285static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2286 struct btrfs_root *root,
2287 struct btrfs_root *log,
2288 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002289 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002290{
2291 u64 range_start;
2292 u64 range_end;
2293 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2294 int ret = 0;
2295 struct btrfs_key dir_key;
2296 struct btrfs_key found_key;
2297 struct btrfs_path *log_path;
2298 struct inode *dir;
2299
2300 dir_key.objectid = dirid;
2301 dir_key.type = BTRFS_DIR_ITEM_KEY;
2302 log_path = btrfs_alloc_path();
2303 if (!log_path)
2304 return -ENOMEM;
2305
2306 dir = read_one_inode(root, dirid);
2307 /* it isn't an error if the inode isn't there, that can happen
2308 * because we replay the deletes before we copy in the inode item
2309 * from the log
2310 */
2311 if (!dir) {
2312 btrfs_free_path(log_path);
2313 return 0;
2314 }
2315again:
2316 range_start = 0;
2317 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002318 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002319 if (del_all)
2320 range_end = (u64)-1;
2321 else {
2322 ret = find_dir_range(log, path, dirid, key_type,
2323 &range_start, &range_end);
2324 if (ret != 0)
2325 break;
2326 }
Chris Masone02119d2008-09-05 16:13:11 -04002327
2328 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002329 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002330 int nritems;
2331 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2332 0, 0);
2333 if (ret < 0)
2334 goto out;
2335
2336 nritems = btrfs_header_nritems(path->nodes[0]);
2337 if (path->slots[0] >= nritems) {
2338 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002339 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002340 break;
Liu Bob98def72018-04-03 01:59:48 +08002341 else if (ret < 0)
2342 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002343 }
2344 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2345 path->slots[0]);
2346 if (found_key.objectid != dirid ||
2347 found_key.type != dir_key.type)
2348 goto next_type;
2349
2350 if (found_key.offset > range_end)
2351 break;
2352
2353 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002354 log_path, dir,
2355 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002356 if (ret)
2357 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002358 if (found_key.offset == (u64)-1)
2359 break;
2360 dir_key.offset = found_key.offset + 1;
2361 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002362 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002363 if (range_end == (u64)-1)
2364 break;
2365 range_start = range_end + 1;
2366 }
2367
2368next_type:
2369 ret = 0;
2370 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2371 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2372 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002373 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002374 goto again;
2375 }
2376out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002377 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002378 btrfs_free_path(log_path);
2379 iput(dir);
2380 return ret;
2381}
2382
2383/*
2384 * the process_func used to replay items from the log tree. This
2385 * gets called in two different stages. The first stage just looks
2386 * for inodes and makes sure they are all copied into the subvolume.
2387 *
2388 * The second stage copies all the other item types from the log into
2389 * the subvolume. The two stage approach is slower, but gets rid of
2390 * lots of complexity around inodes referencing other inodes that exist
2391 * only in the log (references come from either directory items or inode
2392 * back refs).
2393 */
2394static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002395 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002396{
2397 int nritems;
2398 struct btrfs_path *path;
2399 struct btrfs_root *root = wc->replay_dest;
2400 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002401 int i;
2402 int ret;
2403
Qu Wenruo581c1762018-03-29 09:08:11 +08002404 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002405 if (ret)
2406 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002407
2408 level = btrfs_header_level(eb);
2409
2410 if (level != 0)
2411 return 0;
2412
2413 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002414 if (!path)
2415 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002416
2417 nritems = btrfs_header_nritems(eb);
2418 for (i = 0; i < nritems; i++) {
2419 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002420
2421 /* inode keys are done during the first stage */
2422 if (key.type == BTRFS_INODE_ITEM_KEY &&
2423 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002424 struct btrfs_inode_item *inode_item;
2425 u32 mode;
2426
2427 inode_item = btrfs_item_ptr(eb, i,
2428 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002429 ret = replay_xattr_deletes(wc->trans, root, log,
2430 path, key.objectid);
2431 if (ret)
2432 break;
Chris Masone02119d2008-09-05 16:13:11 -04002433 mode = btrfs_inode_mode(eb, inode_item);
2434 if (S_ISDIR(mode)) {
2435 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002436 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002437 if (ret)
2438 break;
Chris Masone02119d2008-09-05 16:13:11 -04002439 }
2440 ret = overwrite_item(wc->trans, root, path,
2441 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002442 if (ret)
2443 break;
Chris Masone02119d2008-09-05 16:13:11 -04002444
Filipe Manana471d5572018-04-05 22:55:12 +01002445 /*
2446 * Before replaying extents, truncate the inode to its
2447 * size. We need to do it now and not after log replay
2448 * because before an fsync we can have prealloc extents
2449 * added beyond the inode's i_size. If we did it after,
2450 * through orphan cleanup for example, we would drop
2451 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002452 */
2453 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002454 struct inode *inode;
2455 u64 from;
2456
2457 inode = read_one_inode(root, key.objectid);
2458 if (!inode) {
2459 ret = -EIO;
2460 break;
2461 }
2462 from = ALIGN(i_size_read(inode),
2463 root->fs_info->sectorsize);
2464 ret = btrfs_drop_extents(wc->trans, root, inode,
2465 from, (u64)-1, 1);
2466 /*
2467 * If the nlink count is zero here, the iput
2468 * will free the inode. We bump it to make
2469 * sure it doesn't get freed until the link
2470 * count fixup is done.
2471 */
2472 if (!ret) {
2473 if (inode->i_nlink == 0)
2474 inc_nlink(inode);
2475 /* Update link count and nbytes. */
2476 ret = btrfs_update_inode(wc->trans,
2477 root, inode);
2478 }
2479 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002480 if (ret)
2481 break;
Chris Masone02119d2008-09-05 16:13:11 -04002482 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002483
Chris Masone02119d2008-09-05 16:13:11 -04002484 ret = link_to_fixup_dir(wc->trans, root,
2485 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002486 if (ret)
2487 break;
Chris Masone02119d2008-09-05 16:13:11 -04002488 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002489
2490 if (key.type == BTRFS_DIR_INDEX_KEY &&
2491 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2492 ret = replay_one_dir_item(wc->trans, root, path,
2493 eb, i, &key);
2494 if (ret)
2495 break;
2496 }
2497
Chris Masone02119d2008-09-05 16:13:11 -04002498 if (wc->stage < LOG_WALK_REPLAY_ALL)
2499 continue;
2500
2501 /* these keys are simply copied */
2502 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2503 ret = overwrite_item(wc->trans, root, path,
2504 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002505 if (ret)
2506 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002507 } else if (key.type == BTRFS_INODE_REF_KEY ||
2508 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002509 ret = add_inode_ref(wc->trans, root, log, path,
2510 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002511 if (ret && ret != -ENOENT)
2512 break;
2513 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002514 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2515 ret = replay_one_extent(wc->trans, root, path,
2516 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002517 if (ret)
2518 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002519 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002520 ret = replay_one_dir_item(wc->trans, root, path,
2521 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002522 if (ret)
2523 break;
Chris Masone02119d2008-09-05 16:13:11 -04002524 }
2525 }
2526 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002527 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002528}
2529
Chris Masond3977122009-01-05 21:25:51 -05002530static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002531 struct btrfs_root *root,
2532 struct btrfs_path *path, int *level,
2533 struct walk_control *wc)
2534{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002535 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002536 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002537 u64 bytenr;
2538 u64 ptr_gen;
2539 struct extent_buffer *next;
2540 struct extent_buffer *cur;
2541 struct extent_buffer *parent;
2542 u32 blocksize;
2543 int ret = 0;
2544
2545 WARN_ON(*level < 0);
2546 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2547
Chris Masond3977122009-01-05 21:25:51 -05002548 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002549 struct btrfs_key first_key;
2550
Chris Masone02119d2008-09-05 16:13:11 -04002551 WARN_ON(*level < 0);
2552 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2553 cur = path->nodes[*level];
2554
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302555 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002556
2557 if (path->slots[*level] >=
2558 btrfs_header_nritems(cur))
2559 break;
2560
2561 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2562 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002563 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002564 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002565
2566 parent = path->nodes[*level];
2567 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002569 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002570 if (IS_ERR(next))
2571 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002572
Chris Masone02119d2008-09-05 16:13:11 -04002573 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002574 ret = wc->process_func(root, next, wc, ptr_gen,
2575 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002576 if (ret) {
2577 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002578 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002579 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002580
Chris Masone02119d2008-09-05 16:13:11 -04002581 path->slots[*level]++;
2582 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002583 ret = btrfs_read_buffer(next, ptr_gen,
2584 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002585 if (ret) {
2586 free_extent_buffer(next);
2587 return ret;
2588 }
Chris Masone02119d2008-09-05 16:13:11 -04002589
Josef Bacik681ae502013-10-07 15:11:00 -04002590 if (trans) {
2591 btrfs_tree_lock(next);
2592 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002593 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002594 btrfs_wait_tree_block_writeback(next);
2595 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002596 } else {
2597 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2598 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002599 }
Chris Masone02119d2008-09-05 16:13:11 -04002600
Chris Masone02119d2008-09-05 16:13:11 -04002601 WARN_ON(root_owner !=
2602 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002603 ret = btrfs_free_and_pin_reserved_extent(
2604 fs_info, bytenr,
2605 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002606 if (ret) {
2607 free_extent_buffer(next);
2608 return ret;
2609 }
Chris Masone02119d2008-09-05 16:13:11 -04002610 }
2611 free_extent_buffer(next);
2612 continue;
2613 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002614 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002615 if (ret) {
2616 free_extent_buffer(next);
2617 return ret;
2618 }
Chris Masone02119d2008-09-05 16:13:11 -04002619
2620 WARN_ON(*level <= 0);
2621 if (path->nodes[*level-1])
2622 free_extent_buffer(path->nodes[*level-1]);
2623 path->nodes[*level-1] = next;
2624 *level = btrfs_header_level(next);
2625 path->slots[*level] = 0;
2626 cond_resched();
2627 }
2628 WARN_ON(*level < 0);
2629 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2630
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002631 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002632
2633 cond_resched();
2634 return 0;
2635}
2636
Chris Masond3977122009-01-05 21:25:51 -05002637static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002638 struct btrfs_root *root,
2639 struct btrfs_path *path, int *level,
2640 struct walk_control *wc)
2641{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002642 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002643 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002644 int i;
2645 int slot;
2646 int ret;
2647
Chris Masond3977122009-01-05 21:25:51 -05002648 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002649 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002650 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002651 path->slots[i]++;
2652 *level = i;
2653 WARN_ON(*level == 0);
2654 return 0;
2655 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002656 struct extent_buffer *parent;
2657 if (path->nodes[*level] == root->node)
2658 parent = path->nodes[*level];
2659 else
2660 parent = path->nodes[*level + 1];
2661
2662 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002663 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002664 btrfs_header_generation(path->nodes[*level]),
2665 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002666 if (ret)
2667 return ret;
2668
Chris Masone02119d2008-09-05 16:13:11 -04002669 if (wc->free) {
2670 struct extent_buffer *next;
2671
2672 next = path->nodes[*level];
2673
Josef Bacik681ae502013-10-07 15:11:00 -04002674 if (trans) {
2675 btrfs_tree_lock(next);
2676 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002677 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002678 btrfs_wait_tree_block_writeback(next);
2679 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002680 } else {
2681 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2682 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002683 }
Chris Masone02119d2008-09-05 16:13:11 -04002684
Chris Masone02119d2008-09-05 16:13:11 -04002685 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002686 ret = btrfs_free_and_pin_reserved_extent(
2687 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002688 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002689 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002690 if (ret)
2691 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002692 }
2693 free_extent_buffer(path->nodes[*level]);
2694 path->nodes[*level] = NULL;
2695 *level = i + 1;
2696 }
2697 }
2698 return 1;
2699}
2700
2701/*
2702 * drop the reference count on the tree rooted at 'snap'. This traverses
2703 * the tree freeing any blocks that have a ref count of zero after being
2704 * decremented.
2705 */
2706static int walk_log_tree(struct btrfs_trans_handle *trans,
2707 struct btrfs_root *log, struct walk_control *wc)
2708{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002709 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002710 int ret = 0;
2711 int wret;
2712 int level;
2713 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002714 int orig_level;
2715
2716 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002717 if (!path)
2718 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002719
2720 level = btrfs_header_level(log->node);
2721 orig_level = level;
2722 path->nodes[level] = log->node;
2723 extent_buffer_get(log->node);
2724 path->slots[level] = 0;
2725
Chris Masond3977122009-01-05 21:25:51 -05002726 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002727 wret = walk_down_log_tree(trans, log, path, &level, wc);
2728 if (wret > 0)
2729 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002730 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002731 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002732 goto out;
2733 }
Chris Masone02119d2008-09-05 16:13:11 -04002734
2735 wret = walk_up_log_tree(trans, log, path, &level, wc);
2736 if (wret > 0)
2737 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002738 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002739 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002740 goto out;
2741 }
Chris Masone02119d2008-09-05 16:13:11 -04002742 }
2743
2744 /* was the root node processed? if not, catch it here */
2745 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002746 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002747 btrfs_header_generation(path->nodes[orig_level]),
2748 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002749 if (ret)
2750 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002751 if (wc->free) {
2752 struct extent_buffer *next;
2753
2754 next = path->nodes[orig_level];
2755
Josef Bacik681ae502013-10-07 15:11:00 -04002756 if (trans) {
2757 btrfs_tree_lock(next);
2758 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002759 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002760 btrfs_wait_tree_block_writeback(next);
2761 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002762 } else {
2763 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2764 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002765 }
Chris Masone02119d2008-09-05 16:13:11 -04002766
Chris Masone02119d2008-09-05 16:13:11 -04002767 WARN_ON(log->root_key.objectid !=
2768 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002769 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2770 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002771 if (ret)
2772 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002773 }
2774 }
2775
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002776out:
Chris Masone02119d2008-09-05 16:13:11 -04002777 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002778 return ret;
2779}
2780
Yan Zheng7237f182009-01-21 12:54:03 -05002781/*
2782 * helper function to update the item for a given subvolumes log root
2783 * in the tree of log roots
2784 */
2785static int update_log_root(struct btrfs_trans_handle *trans,
2786 struct btrfs_root *log)
2787{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002789 int ret;
2790
2791 if (log->log_transid == 1) {
2792 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002793 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002794 &log->root_key, &log->root_item);
2795 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002796 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002797 &log->root_key, &log->root_item);
2798 }
2799 return ret;
2800}
2801
Zhaolei60d53eb2015-08-17 18:44:46 +08002802static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002803{
2804 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002805 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002806
Yan Zheng7237f182009-01-21 12:54:03 -05002807 /*
2808 * we only allow two pending log transactions at a time,
2809 * so we know that if ours is more than 2 older than the
2810 * current transaction, we're done
2811 */
Liu Bo49e83f52017-09-01 16:14:30 -06002812 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002813 prepare_to_wait(&root->log_commit_wait[index],
2814 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002815
2816 if (!(root->log_transid_committed < transid &&
2817 atomic_read(&root->log_commit[index])))
2818 break;
2819
Yan Zheng7237f182009-01-21 12:54:03 -05002820 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002821 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002822 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002823 }
2824 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002825}
2826
Zhaolei60d53eb2015-08-17 18:44:46 +08002827static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002828{
2829 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002830
Liu Bo49e83f52017-09-01 16:14:30 -06002831 for (;;) {
2832 prepare_to_wait(&root->log_writer_wait, &wait,
2833 TASK_UNINTERRUPTIBLE);
2834 if (!atomic_read(&root->log_writers))
2835 break;
2836
Yan Zheng7237f182009-01-21 12:54:03 -05002837 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002838 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002839 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002840 }
Liu Bo49e83f52017-09-01 16:14:30 -06002841 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002842}
2843
Miao Xie8b050d32014-02-20 18:08:58 +08002844static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2845 struct btrfs_log_ctx *ctx)
2846{
2847 if (!ctx)
2848 return;
2849
2850 mutex_lock(&root->log_mutex);
2851 list_del_init(&ctx->list);
2852 mutex_unlock(&root->log_mutex);
2853}
2854
2855/*
2856 * Invoked in log mutex context, or be sure there is no other task which
2857 * can access the list.
2858 */
2859static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2860 int index, int error)
2861{
2862 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002863 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002864
Chris Mason570dd452016-10-27 10:42:20 -07002865 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2866 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002867 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002868 }
Miao Xie8b050d32014-02-20 18:08:58 +08002869
2870 INIT_LIST_HEAD(&root->log_ctxs[index]);
2871}
2872
Chris Masone02119d2008-09-05 16:13:11 -04002873/*
2874 * btrfs_sync_log does sends a given tree log down to the disk and
2875 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002876 * you know that any inodes previously logged are safely on disk only
2877 * if it returns 0.
2878 *
2879 * Any other return value means you need to call btrfs_commit_transaction.
2880 * Some of the edge cases for fsyncing directories that have had unlinks
2881 * or renames done in the past mean that sometimes the only safe
2882 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2883 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002884 */
2885int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002886 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002887{
Yan Zheng7237f182009-01-21 12:54:03 -05002888 int index1;
2889 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002890 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002891 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002892 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002893 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002894 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002895 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002896 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002897 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002898
Yan Zheng7237f182009-01-21 12:54:03 -05002899 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002900 log_transid = ctx->log_transid;
2901 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002902 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002903 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002904 }
Miao Xied1433de2014-02-20 18:08:59 +08002905
2906 index1 = log_transid % 2;
2907 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002908 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002909 mutex_unlock(&root->log_mutex);
2910 return ctx->log_ret;
2911 }
2912 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002913 atomic_set(&root->log_commit[index1], 1);
2914
2915 /* wait for previous tree log sync to complete */
2916 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002917 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002918
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002919 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002920 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002921 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002923 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002924 mutex_unlock(&root->log_mutex);
2925 schedule_timeout_uninterruptible(1);
2926 mutex_lock(&root->log_mutex);
2927 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002928 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002929 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002930 break;
2931 }
Chris Masond0c803c2008-09-11 16:17:57 -04002932
Chris Mason12fcfd22009-03-24 10:24:20 -04002933 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002935 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002936 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002937 mutex_unlock(&root->log_mutex);
2938 goto out;
2939 }
2940
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002941 if (log_transid % 2 == 0)
2942 mark = EXTENT_DIRTY;
2943 else
2944 mark = EXTENT_NEW;
2945
Chris Mason690587d2009-10-13 13:29:19 -04002946 /* we start IO on all the marked extents here, but we don't actually
2947 * wait for them until later.
2948 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002949 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002950 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002951 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002952 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002953 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002954 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002956 mutex_unlock(&root->log_mutex);
2957 goto out;
2958 }
Yan Zheng7237f182009-01-21 12:54:03 -05002959
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002960 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002961
Yan Zheng7237f182009-01-21 12:54:03 -05002962 root->log_transid++;
2963 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002964 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002965 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002966 * IO has been started, blocks of the log tree have WRITTEN flag set
2967 * in their headers. new modifications of the log will be written to
2968 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002969 */
2970 mutex_unlock(&root->log_mutex);
2971
Filipe Manana28a23592016-08-23 21:13:51 +01002972 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002973
Yan Zheng7237f182009-01-21 12:54:03 -05002974 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002975 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002976 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002977
2978 index2 = log_root_tree->log_transid % 2;
2979 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2980 root_log_ctx.log_transid = log_root_tree->log_transid;
2981
Yan Zheng7237f182009-01-21 12:54:03 -05002982 mutex_unlock(&log_root_tree->log_mutex);
2983
2984 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002985
2986 mutex_lock(&log_root_tree->log_mutex);
2987 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +01002988 /* atomic_dec_and_test implies a barrier */
2989 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002990 }
2991
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002992 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002993 if (!list_empty(&root_log_ctx.list))
2994 list_del_init(&root_log_ctx.list);
2995
Miao Xiec6adc9c2013-05-28 10:05:39 +00002996 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08002998
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002999 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003000 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003001 mutex_unlock(&log_root_tree->log_mutex);
3002 goto out;
3003 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003004 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003005 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003006 mutex_unlock(&log_root_tree->log_mutex);
3007 ret = -EAGAIN;
3008 goto out;
3009 }
3010
Miao Xied1433de2014-02-20 18:08:59 +08003011 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003012 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003013 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003014 mutex_unlock(&log_root_tree->log_mutex);
3015 ret = root_log_ctx.log_ret;
3016 goto out;
3017 }
Miao Xie8b050d32014-02-20 18:08:58 +08003018
Miao Xied1433de2014-02-20 18:08:59 +08003019 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003020 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003021 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003022 ret = btrfs_wait_tree_log_extents(log, mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05003023 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08003024 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003025 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003026 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003027 if (!ret)
3028 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003029 goto out;
3030 }
Miao Xied1433de2014-02-20 18:08:59 +08003031 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003032 atomic_set(&log_root_tree->log_commit[index2], 1);
3033
Chris Mason12fcfd22009-03-24 10:24:20 -04003034 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003035 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003036 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003037 }
Yan Zheng7237f182009-01-21 12:54:03 -05003038
Zhaolei60d53eb2015-08-17 18:44:46 +08003039 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003040
3041 /*
3042 * now that we've moved on to the tree of log tree roots,
3043 * check the full commit flag again
3044 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003045 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003046 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003047 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003048 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04003049 mutex_unlock(&log_root_tree->log_mutex);
3050 ret = -EAGAIN;
3051 goto out_wake_log_root;
3052 }
Yan Zheng7237f182009-01-21 12:54:03 -05003053
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003054 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003055 &log_root_tree->dirty_log_pages,
3056 EXTENT_DIRTY | EXTENT_NEW);
3057 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003059 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003060 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003061 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003062 mutex_unlock(&log_root_tree->log_mutex);
3063 goto out_wake_log_root;
3064 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003065 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003066 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003067 ret = btrfs_wait_tree_log_extents(log_root_tree,
3068 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003069 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003070 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003071 btrfs_free_logged_extents(log, log_transid);
3072 mutex_unlock(&log_root_tree->log_mutex);
3073 goto out_wake_log_root;
3074 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05003075 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003076
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003077 btrfs_set_super_log_root(fs_info->super_for_commit,
3078 log_root_tree->node->start);
3079 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3080 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003081
Yan Zheng7237f182009-01-21 12:54:03 -05003082 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003083 mutex_unlock(&log_root_tree->log_mutex);
3084
3085 /*
3086 * nobody else is going to jump in and write the the ctree
3087 * super here because the log_commit atomic below is protecting
3088 * us. We must be called with a transaction handle pinning
3089 * the running transaction open, so a full commit can't hop
3090 * in and cause problems either.
3091 */
David Sterbaeece6a92017-02-10 19:04:32 +01003092 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003093 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003094 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003095 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003096 goto out_wake_log_root;
3097 }
Yan Zheng7237f182009-01-21 12:54:03 -05003098
Chris Mason257c62e2009-10-13 13:21:08 -04003099 mutex_lock(&root->log_mutex);
3100 if (root->last_log_commit < log_transid)
3101 root->last_log_commit = log_transid;
3102 mutex_unlock(&root->log_mutex);
3103
Chris Mason12fcfd22009-03-24 10:24:20 -04003104out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003105 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003106 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3107
Miao Xied1433de2014-02-20 18:08:59 +08003108 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003109 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003110 mutex_unlock(&log_root_tree->log_mutex);
3111
David Sterba33a9eca2015-10-10 18:35:10 +02003112 /*
David Sterba093258e2018-02-26 16:15:17 +01003113 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3114 * all the updates above are seen by the woken threads. It might not be
3115 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003116 */
David Sterba093258e2018-02-26 16:15:17 +01003117 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003118out:
Miao Xied1433de2014-02-20 18:08:59 +08003119 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003120 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003121 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003122 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003123 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003124
David Sterba33a9eca2015-10-10 18:35:10 +02003125 /*
David Sterba093258e2018-02-26 16:15:17 +01003126 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3127 * all the updates above are seen by the woken threads. It might not be
3128 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003129 */
David Sterba093258e2018-02-26 16:15:17 +01003130 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003131 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003132}
3133
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003134static void free_log_tree(struct btrfs_trans_handle *trans,
3135 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003136{
3137 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003138 u64 start;
3139 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003140 struct walk_control wc = {
3141 .free = 1,
3142 .process_func = process_one_buffer
3143 };
3144
Josef Bacik681ae502013-10-07 15:11:00 -04003145 ret = walk_log_tree(trans, log, &wc);
3146 /* I don't think this can happen but just in case */
3147 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003148 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003149
Chris Masond3977122009-01-05 21:25:51 -05003150 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003151 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bo55237a52018-01-25 11:02:52 -07003152 0, &start, &end,
3153 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003154 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003155 if (ret)
3156 break;
3157
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003158 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bo55237a52018-01-25 11:02:52 -07003159 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003160 }
3161
Josef Bacik2ab28f32012-10-12 15:27:49 -04003162 /*
3163 * We may have short-circuited the log tree with the full commit logic
3164 * and left ordered extents on our list, so clear these out to keep us
3165 * from leaking inodes and memory.
3166 */
3167 btrfs_free_logged_extents(log, 0);
3168 btrfs_free_logged_extents(log, 1);
3169
Yan Zheng7237f182009-01-21 12:54:03 -05003170 free_extent_buffer(log->node);
3171 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003172}
3173
3174/*
3175 * free all the extents used by the tree log. This should be called
3176 * at commit time of the full transaction
3177 */
3178int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3179{
3180 if (root->log_root) {
3181 free_log_tree(trans, root->log_root);
3182 root->log_root = NULL;
3183 }
3184 return 0;
3185}
3186
3187int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3188 struct btrfs_fs_info *fs_info)
3189{
3190 if (fs_info->log_root_tree) {
3191 free_log_tree(trans, fs_info->log_root_tree);
3192 fs_info->log_root_tree = NULL;
3193 }
Chris Masone02119d2008-09-05 16:13:11 -04003194 return 0;
3195}
3196
3197/*
Chris Masone02119d2008-09-05 16:13:11 -04003198 * If both a file and directory are logged, and unlinks or renames are
3199 * mixed in, we have a few interesting corners:
3200 *
3201 * create file X in dir Y
3202 * link file X to X.link in dir Y
3203 * fsync file X
3204 * unlink file X but leave X.link
3205 * fsync dir Y
3206 *
3207 * After a crash we would expect only X.link to exist. But file X
3208 * didn't get fsync'd again so the log has back refs for X and X.link.
3209 *
3210 * We solve this by removing directory entries and inode backrefs from the
3211 * log when a file that was logged in the current transaction is
3212 * unlinked. Any later fsync will include the updated log entries, and
3213 * we'll be able to reconstruct the proper directory items from backrefs.
3214 *
3215 * This optimizations allows us to avoid relogging the entire inode
3216 * or the entire directory.
3217 */
3218int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3219 struct btrfs_root *root,
3220 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003221 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003222{
3223 struct btrfs_root *log;
3224 struct btrfs_dir_item *di;
3225 struct btrfs_path *path;
3226 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003227 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003228 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003229 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003230
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003231 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003232 return 0;
3233
Chris Masone02119d2008-09-05 16:13:11 -04003234 ret = join_running_log_trans(root);
3235 if (ret)
3236 return 0;
3237
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003238 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003239
3240 log = root->log_root;
3241 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003242 if (!path) {
3243 err = -ENOMEM;
3244 goto out_unlock;
3245 }
liubo2a29edc2011-01-26 06:22:08 +00003246
Li Zefan33345d012011-04-20 10:31:50 +08003247 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003248 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003249 if (IS_ERR(di)) {
3250 err = PTR_ERR(di);
3251 goto fail;
3252 }
3253 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003254 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3255 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003256 if (ret) {
3257 err = ret;
3258 goto fail;
3259 }
Chris Masone02119d2008-09-05 16:13:11 -04003260 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003261 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003262 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003263 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003264 if (IS_ERR(di)) {
3265 err = PTR_ERR(di);
3266 goto fail;
3267 }
3268 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003269 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3270 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003271 if (ret) {
3272 err = ret;
3273 goto fail;
3274 }
Chris Masone02119d2008-09-05 16:13:11 -04003275 }
3276
3277 /* update the directory size in the log to reflect the names
3278 * we have removed
3279 */
3280 if (bytes_del) {
3281 struct btrfs_key key;
3282
Li Zefan33345d012011-04-20 10:31:50 +08003283 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003284 key.offset = 0;
3285 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003286 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003287
3288 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003289 if (ret < 0) {
3290 err = ret;
3291 goto fail;
3292 }
Chris Masone02119d2008-09-05 16:13:11 -04003293 if (ret == 0) {
3294 struct btrfs_inode_item *item;
3295 u64 i_size;
3296
3297 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3298 struct btrfs_inode_item);
3299 i_size = btrfs_inode_size(path->nodes[0], item);
3300 if (i_size > bytes_del)
3301 i_size -= bytes_del;
3302 else
3303 i_size = 0;
3304 btrfs_set_inode_size(path->nodes[0], item, i_size);
3305 btrfs_mark_buffer_dirty(path->nodes[0]);
3306 } else
3307 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003308 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003309 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003310fail:
Chris Masone02119d2008-09-05 16:13:11 -04003311 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003312out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003313 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003314 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003315 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003316 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003317 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003318 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003319
Chris Mason12fcfd22009-03-24 10:24:20 -04003320 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003321
Andi Kleen411fc6b2010-10-29 15:14:31 -04003322 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003323}
3324
3325/* see comments for btrfs_del_dir_entries_in_log */
3326int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3327 struct btrfs_root *root,
3328 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003329 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003330{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003331 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003332 struct btrfs_root *log;
3333 u64 index;
3334 int ret;
3335
Nikolay Borisova491abb2017-01-18 00:31:33 +02003336 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003337 return 0;
3338
Chris Masone02119d2008-09-05 16:13:11 -04003339 ret = join_running_log_trans(root);
3340 if (ret)
3341 return 0;
3342 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003343 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003344
Nikolay Borisova491abb2017-01-18 00:31:33 +02003345 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003346 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003347 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003348 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003349 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003350 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003351 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003352 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003353 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003354
Chris Masone02119d2008-09-05 16:13:11 -04003355 return ret;
3356}
3357
3358/*
3359 * creates a range item in the log for 'dirid'. first_offset and
3360 * last_offset tell us which parts of the key space the log should
3361 * be considered authoritative for.
3362 */
3363static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3364 struct btrfs_root *log,
3365 struct btrfs_path *path,
3366 int key_type, u64 dirid,
3367 u64 first_offset, u64 last_offset)
3368{
3369 int ret;
3370 struct btrfs_key key;
3371 struct btrfs_dir_log_item *item;
3372
3373 key.objectid = dirid;
3374 key.offset = first_offset;
3375 if (key_type == BTRFS_DIR_ITEM_KEY)
3376 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3377 else
3378 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3379 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003380 if (ret)
3381 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003382
3383 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3384 struct btrfs_dir_log_item);
3385 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3386 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003387 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003388 return 0;
3389}
3390
3391/*
3392 * log all the items included in the current transaction for a given
3393 * directory. This also creates the range items in the log tree required
3394 * to replay anything deleted before the fsync
3395 */
3396static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003397 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003398 struct btrfs_path *path,
3399 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003400 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003401 u64 min_offset, u64 *last_offset_ret)
3402{
3403 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003404 struct btrfs_root *log = root->log_root;
3405 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003406 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003407 int ret;
3408 int i;
3409 int nritems;
3410 u64 first_offset = min_offset;
3411 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003412 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003413
3414 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003415
Li Zefan33345d012011-04-20 10:31:50 +08003416 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003417 min_key.type = key_type;
3418 min_key.offset = min_offset;
3419
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003420 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003421
3422 /*
3423 * we didn't find anything from this transaction, see if there
3424 * is anything at all
3425 */
Li Zefan33345d012011-04-20 10:31:50 +08003426 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3427 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003428 min_key.type = key_type;
3429 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003430 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003431 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3432 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003433 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003434 return ret;
3435 }
Li Zefan33345d012011-04-20 10:31:50 +08003436 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003437
3438 /* if ret == 0 there are items for this type,
3439 * create a range to tell us the last key of this type.
3440 * otherwise, there are no items in this directory after
3441 * *min_offset, and we create a range to indicate that.
3442 */
3443 if (ret == 0) {
3444 struct btrfs_key tmp;
3445 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3446 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003447 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003448 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003449 }
3450 goto done;
3451 }
3452
3453 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003454 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003455 if (ret == 0) {
3456 struct btrfs_key tmp;
3457 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3458 if (key_type == tmp.type) {
3459 first_offset = tmp.offset;
3460 ret = overwrite_item(trans, log, dst_path,
3461 path->nodes[0], path->slots[0],
3462 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003463 if (ret) {
3464 err = ret;
3465 goto done;
3466 }
Chris Masone02119d2008-09-05 16:13:11 -04003467 }
3468 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003469 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003470
3471 /* find the first key from this transaction again */
3472 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303473 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003474 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003475
3476 /*
3477 * we have a block from this transaction, log every item in it
3478 * from our directory
3479 */
Chris Masond3977122009-01-05 21:25:51 -05003480 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003481 struct btrfs_key tmp;
3482 src = path->nodes[0];
3483 nritems = btrfs_header_nritems(src);
3484 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003485 struct btrfs_dir_item *di;
3486
Chris Masone02119d2008-09-05 16:13:11 -04003487 btrfs_item_key_to_cpu(src, &min_key, i);
3488
Li Zefan33345d012011-04-20 10:31:50 +08003489 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003490 goto done;
3491 ret = overwrite_item(trans, log, dst_path, src, i,
3492 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003493 if (ret) {
3494 err = ret;
3495 goto done;
3496 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003497
3498 /*
3499 * We must make sure that when we log a directory entry,
3500 * the corresponding inode, after log replay, has a
3501 * matching link count. For example:
3502 *
3503 * touch foo
3504 * mkdir mydir
3505 * sync
3506 * ln foo mydir/bar
3507 * xfs_io -c "fsync" mydir
3508 * <crash>
3509 * <mount fs and log replay>
3510 *
3511 * Would result in a fsync log that when replayed, our
3512 * file inode would have a link count of 1, but we get
3513 * two directory entries pointing to the same inode.
3514 * After removing one of the names, it would not be
3515 * possible to remove the other name, which resulted
3516 * always in stale file handle errors, and would not
3517 * be possible to rmdir the parent directory, since
3518 * its i_size could never decrement to the value
3519 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3520 */
3521 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3522 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3523 if (ctx &&
3524 (btrfs_dir_transid(src, di) == trans->transid ||
3525 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3526 tmp.type != BTRFS_ROOT_ITEM_KEY)
3527 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003528 }
3529 path->slots[0] = nritems;
3530
3531 /*
3532 * look ahead to the next item and see if it is also
3533 * from this directory and from this transaction
3534 */
3535 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003536 if (ret) {
3537 if (ret == 1)
3538 last_offset = (u64)-1;
3539 else
3540 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003541 goto done;
3542 }
3543 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003544 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003545 last_offset = (u64)-1;
3546 goto done;
3547 }
3548 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3549 ret = overwrite_item(trans, log, dst_path,
3550 path->nodes[0], path->slots[0],
3551 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003552 if (ret)
3553 err = ret;
3554 else
3555 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003556 goto done;
3557 }
3558 }
3559done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003560 btrfs_release_path(path);
3561 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003562
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003563 if (err == 0) {
3564 *last_offset_ret = last_offset;
3565 /*
3566 * insert the log range keys to indicate where the log
3567 * is valid
3568 */
3569 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003570 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003571 if (ret)
3572 err = ret;
3573 }
3574 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003575}
3576
3577/*
3578 * logging directories is very similar to logging inodes, We find all the items
3579 * from the current transaction and write them to the log.
3580 *
3581 * The recovery code scans the directory in the subvolume, and if it finds a
3582 * key in the range logged that is not present in the log tree, then it means
3583 * that dir entry was unlinked during the transaction.
3584 *
3585 * In order for that scan to work, we must include one key smaller than
3586 * the smallest logged by this transaction and one key larger than the largest
3587 * key logged by this transaction.
3588 */
3589static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003590 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003591 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003592 struct btrfs_path *dst_path,
3593 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003594{
3595 u64 min_key;
3596 u64 max_key;
3597 int ret;
3598 int key_type = BTRFS_DIR_ITEM_KEY;
3599
3600again:
3601 min_key = 0;
3602 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003603 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003604 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3605 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003606 if (ret)
3607 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003608 if (max_key == (u64)-1)
3609 break;
3610 min_key = max_key + 1;
3611 }
3612
3613 if (key_type == BTRFS_DIR_ITEM_KEY) {
3614 key_type = BTRFS_DIR_INDEX_KEY;
3615 goto again;
3616 }
3617 return 0;
3618}
3619
3620/*
3621 * a helper function to drop items from the log before we relog an
3622 * inode. max_key_type indicates the highest item type to remove.
3623 * This cannot be run for file data extents because it does not
3624 * free the extents they point to.
3625 */
3626static int drop_objectid_items(struct btrfs_trans_handle *trans,
3627 struct btrfs_root *log,
3628 struct btrfs_path *path,
3629 u64 objectid, int max_key_type)
3630{
3631 int ret;
3632 struct btrfs_key key;
3633 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003634 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003635
3636 key.objectid = objectid;
3637 key.type = max_key_type;
3638 key.offset = (u64)-1;
3639
Chris Masond3977122009-01-05 21:25:51 -05003640 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003641 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003642 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003643 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003644 break;
3645
3646 if (path->slots[0] == 0)
3647 break;
3648
3649 path->slots[0]--;
3650 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3651 path->slots[0]);
3652
3653 if (found_key.objectid != objectid)
3654 break;
3655
Josef Bacik18ec90d2012-09-28 11:56:28 -04003656 found_key.offset = 0;
3657 found_key.type = 0;
3658 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3659 &start_slot);
3660
3661 ret = btrfs_del_items(trans, log, path, start_slot,
3662 path->slots[0] - start_slot + 1);
3663 /*
3664 * If start slot isn't 0 then we don't need to re-search, we've
3665 * found the last guy with the objectid in this tree.
3666 */
3667 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003668 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003669 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003670 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003671 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003672 if (ret > 0)
3673 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003674 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003675}
3676
Josef Bacik94edf4a2012-09-25 14:56:25 -04003677static void fill_inode_item(struct btrfs_trans_handle *trans,
3678 struct extent_buffer *leaf,
3679 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003680 struct inode *inode, int log_inode_only,
3681 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003682{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003683 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003684
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003685 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003686
3687 if (log_inode_only) {
3688 /* set the generation to zero so the recover code
3689 * can tell the difference between an logging
3690 * just to say 'this inode exists' and a logging
3691 * to say 'update this inode with these values'
3692 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003693 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003694 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003695 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003696 btrfs_set_token_inode_generation(leaf, item,
3697 BTRFS_I(inode)->generation,
3698 &token);
3699 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003700 }
3701
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003702 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3703 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3704 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3705 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3706
David Sterbaa937b972014-12-12 17:39:12 +01003707 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003708 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003709 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003710 inode->i_atime.tv_nsec, &token);
3711
David Sterbaa937b972014-12-12 17:39:12 +01003712 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003713 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003714 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003715 inode->i_mtime.tv_nsec, &token);
3716
David Sterbaa937b972014-12-12 17:39:12 +01003717 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003718 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003719 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003720 inode->i_ctime.tv_nsec, &token);
3721
3722 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3723 &token);
3724
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003725 btrfs_set_token_inode_sequence(leaf, item,
3726 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003727 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3728 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3729 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3730 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003731}
3732
Josef Bacika95249b2012-10-11 16:17:34 -04003733static int log_inode_item(struct btrfs_trans_handle *trans,
3734 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003735 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003736{
3737 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003738 int ret;
3739
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003740 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003741 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003742 if (ret && ret != -EEXIST)
3743 return ret;
3744 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3745 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003746 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3747 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003748 btrfs_release_path(path);
3749 return 0;
3750}
3751
Chris Mason31ff1cd2008-09-11 16:17:57 -04003752static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003753 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003754 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003755 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003756 int start_slot, int nr, int inode_only,
3757 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003758{
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003760 unsigned long src_offset;
3761 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003762 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003763 struct btrfs_file_extent_item *extent;
3764 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003765 struct extent_buffer *src = src_path->nodes[0];
3766 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003767 int ret;
3768 struct btrfs_key *ins_keys;
3769 u32 *ins_sizes;
3770 char *ins_data;
3771 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003772 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003773 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003774 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003775 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003776 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003777
3778 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003779
3780 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3781 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003782 if (!ins_data)
3783 return -ENOMEM;
3784
Josef Bacik16e75492013-10-22 12:18:51 -04003785 first_key.objectid = (u64)-1;
3786
Chris Mason31ff1cd2008-09-11 16:17:57 -04003787 ins_sizes = (u32 *)ins_data;
3788 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3789
3790 for (i = 0; i < nr; i++) {
3791 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3792 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3793 }
3794 ret = btrfs_insert_empty_items(trans, log, dst_path,
3795 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003796 if (ret) {
3797 kfree(ins_data);
3798 return ret;
3799 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003800
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003801 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003802 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3803 dst_path->slots[0]);
3804
3805 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3806
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003807 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003808 last_key = ins_keys[i];
3809
Josef Bacik94edf4a2012-09-25 14:56:25 -04003810 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003811 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3812 dst_path->slots[0],
3813 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003814 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003815 &inode->vfs_inode,
3816 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003817 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003818 } else {
3819 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3820 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003821 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003822
Josef Bacik16e75492013-10-22 12:18:51 -04003823 /*
3824 * We set need_find_last_extent here in case we know we were
3825 * processing other items and then walk into the first extent in
3826 * the inode. If we don't hit an extent then nothing changes,
3827 * we'll do the last search the next time around.
3828 */
3829 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3830 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003831 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003832 first_key = ins_keys[i];
3833 } else {
3834 need_find_last_extent = false;
3835 }
3836
Chris Mason31ff1cd2008-09-11 16:17:57 -04003837 /* take a reference on file data extents so that truncates
3838 * or deletes of this inode don't have to relog the inode
3839 * again
3840 */
David Sterba962a2982014-06-04 18:41:45 +02003841 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003842 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003843 int found_type;
3844 extent = btrfs_item_ptr(src, start_slot + i,
3845 struct btrfs_file_extent_item);
3846
liubo8e531cd2011-05-06 10:36:09 +08003847 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3848 continue;
3849
Chris Mason31ff1cd2008-09-11 16:17:57 -04003850 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003851 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003852 u64 ds, dl, cs, cl;
3853 ds = btrfs_file_extent_disk_bytenr(src,
3854 extent);
3855 /* ds == 0 is a hole */
3856 if (ds == 0)
3857 continue;
3858
3859 dl = btrfs_file_extent_disk_num_bytes(src,
3860 extent);
3861 cs = btrfs_file_extent_offset(src, extent);
3862 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003863 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003864 if (btrfs_file_extent_compression(src,
3865 extent)) {
3866 cs = 0;
3867 cl = dl;
3868 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003869
3870 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003871 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003872 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003873 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003874 if (ret) {
3875 btrfs_release_path(dst_path);
3876 kfree(ins_data);
3877 return ret;
3878 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003879 }
3880 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003881 }
3882
3883 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003884 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003885 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003886
3887 /*
3888 * we have to do this after the loop above to avoid changing the
3889 * log tree while trying to change the log tree.
3890 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003891 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003892 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003893 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3894 struct btrfs_ordered_sum,
3895 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003896 if (!ret)
3897 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003898 list_del(&sums->list);
3899 kfree(sums);
3900 }
Josef Bacik16e75492013-10-22 12:18:51 -04003901
3902 if (!has_extents)
3903 return ret;
3904
Filipe Manana74121f7c2014-08-07 12:00:44 +01003905 if (need_find_last_extent && *last_extent == first_key.offset) {
3906 /*
3907 * We don't have any leafs between our current one and the one
3908 * we processed before that can have file extent items for our
3909 * inode (and have a generation number smaller than our current
3910 * transaction id).
3911 */
3912 need_find_last_extent = false;
3913 }
3914
Josef Bacik16e75492013-10-22 12:18:51 -04003915 /*
3916 * Because we use btrfs_search_forward we could skip leaves that were
3917 * not modified and then assume *last_extent is valid when it really
3918 * isn't. So back up to the previous leaf and read the end of the last
3919 * extent before we go and fill in holes.
3920 */
3921 if (need_find_last_extent) {
3922 u64 len;
3923
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003924 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003925 if (ret < 0)
3926 return ret;
3927 if (ret)
3928 goto fill_holes;
3929 if (src_path->slots[0])
3930 src_path->slots[0]--;
3931 src = src_path->nodes[0];
3932 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003933 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003934 key.type != BTRFS_EXTENT_DATA_KEY)
3935 goto fill_holes;
3936 extent = btrfs_item_ptr(src, src_path->slots[0],
3937 struct btrfs_file_extent_item);
3938 if (btrfs_file_extent_type(src, extent) ==
3939 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003940 len = btrfs_file_extent_inline_len(src,
3941 src_path->slots[0],
3942 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003943 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003944 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003945 } else {
3946 len = btrfs_file_extent_num_bytes(src, extent);
3947 *last_extent = key.offset + len;
3948 }
3949 }
3950fill_holes:
3951 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3952 * things could have happened
3953 *
3954 * 1) A merge could have happened, so we could currently be on a leaf
3955 * that holds what we were copying in the first place.
3956 * 2) A split could have happened, and now not all of the items we want
3957 * are on the same leaf.
3958 *
3959 * So we need to adjust how we search for holes, we need to drop the
3960 * path and re-search for the first extent key we found, and then walk
3961 * forward until we hit the last one we copied.
3962 */
3963 if (need_find_last_extent) {
3964 /* btrfs_prev_leaf could return 1 without releasing the path */
3965 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01003966 ret = btrfs_search_slot(NULL, inode->root, &first_key,
3967 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04003968 if (ret < 0)
3969 return ret;
3970 ASSERT(ret == 0);
3971 src = src_path->nodes[0];
3972 i = src_path->slots[0];
3973 } else {
3974 i = start_slot;
3975 }
3976
3977 /*
3978 * Ok so here we need to go through and fill in any holes we may have
3979 * to make sure that holes are punched for those areas in case they had
3980 * extents previously.
3981 */
3982 while (!done) {
3983 u64 offset, len;
3984 u64 extent_end;
3985
3986 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003987 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003988 if (ret < 0)
3989 return ret;
3990 ASSERT(ret == 0);
3991 src = src_path->nodes[0];
3992 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01003993 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003994 }
3995
3996 btrfs_item_key_to_cpu(src, &key, i);
3997 if (!btrfs_comp_cpu_keys(&key, &last_key))
3998 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003999 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004000 key.type != BTRFS_EXTENT_DATA_KEY) {
4001 i++;
4002 continue;
4003 }
4004 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4005 if (btrfs_file_extent_type(src, extent) ==
4006 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004007 len = btrfs_file_extent_inline_len(src, i, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004008 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004009 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004010 } else {
4011 len = btrfs_file_extent_num_bytes(src, extent);
4012 extent_end = key.offset + len;
4013 }
4014 i++;
4015
4016 if (*last_extent == key.offset) {
4017 *last_extent = extent_end;
4018 continue;
4019 }
4020 offset = *last_extent;
4021 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004022 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004023 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004024 if (ret)
4025 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004026 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004027 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004028
4029 /*
4030 * Check if there is a hole between the last extent found in our leaf
4031 * and the first extent in the next leaf. If there is one, we need to
4032 * log an explicit hole so that at replay time we can punch the hole.
4033 */
4034 if (ret == 0 &&
4035 key.objectid == btrfs_ino(inode) &&
4036 key.type == BTRFS_EXTENT_DATA_KEY &&
4037 i == btrfs_header_nritems(src_path->nodes[0])) {
4038 ret = btrfs_next_leaf(inode->root, src_path);
4039 need_find_last_extent = true;
4040 if (ret > 0) {
4041 ret = 0;
4042 } else if (ret == 0) {
4043 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4044 src_path->slots[0]);
4045 if (key.objectid == btrfs_ino(inode) &&
4046 key.type == BTRFS_EXTENT_DATA_KEY &&
4047 *last_extent < key.offset) {
4048 const u64 len = key.offset - *last_extent;
4049
4050 ret = btrfs_insert_file_extent(trans, log,
4051 btrfs_ino(inode),
4052 *last_extent, 0,
4053 0, len, 0, len,
4054 0, 0, 0);
4055 }
4056 }
4057 }
Josef Bacik16e75492013-10-22 12:18:51 -04004058 /*
4059 * Need to let the callers know we dropped the path so they should
4060 * re-search.
4061 */
4062 if (!ret && need_find_last_extent)
4063 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004064 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004065}
4066
Josef Bacik5dc562c2012-08-17 13:14:17 -04004067static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4068{
4069 struct extent_map *em1, *em2;
4070
4071 em1 = list_entry(a, struct extent_map, list);
4072 em2 = list_entry(b, struct extent_map, list);
4073
4074 if (em1->start < em2->start)
4075 return -1;
4076 else if (em1->start > em2->start)
4077 return 1;
4078 return 0;
4079}
4080
Josef Bacike7175a62018-05-23 11:58:34 -04004081static int log_extent_csums(struct btrfs_trans_handle *trans,
4082 struct btrfs_inode *inode,
4083 struct btrfs_root *root,
4084 const struct extent_map *em)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004085{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004086 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana8407f552014-09-05 15:14:39 +01004087 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004088 u64 csum_offset;
4089 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004090 LIST_HEAD(ordered_sums);
4091 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004092
Josef Bacike7175a62018-05-23 11:58:34 -04004093 if (inode->flags & BTRFS_INODE_NODATASUM ||
4094 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004095 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004096 return 0;
4097
Josef Bacike7175a62018-05-23 11:58:34 -04004098 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004099 if (em->compress_type) {
4100 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004101 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004102 } else {
Josef Bacike7175a62018-05-23 11:58:34 -04004103 csum_offset = em->mod_start - em->start;
4104 csum_len = em->mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004105 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004106
Josef Bacik70c8a912012-10-11 16:54:30 -04004107 /* block start is already adjusted for the file extent offset. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004108 ret = btrfs_lookup_csums_range(fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004109 em->block_start + csum_offset,
4110 em->block_start + csum_offset +
4111 csum_len - 1, &ordered_sums, 0);
4112 if (ret)
4113 return ret;
4114
4115 while (!list_empty(&ordered_sums)) {
4116 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4117 struct btrfs_ordered_sum,
4118 list);
4119 if (!ret)
4120 ret = btrfs_csum_file_blocks(trans, log, sums);
4121 list_del(&sums->list);
4122 kfree(sums);
4123 }
4124
4125 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126}
4127
Filipe Manana8407f552014-09-05 15:14:39 +01004128static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004129 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004130 const struct extent_map *em,
4131 struct btrfs_path *path,
4132 const struct list_head *logged_list,
4133 struct btrfs_log_ctx *ctx)
4134{
4135 struct btrfs_root *log = root->log_root;
4136 struct btrfs_file_extent_item *fi;
4137 struct extent_buffer *leaf;
4138 struct btrfs_map_token token;
4139 struct btrfs_key key;
4140 u64 extent_offset = em->start - em->orig_start;
4141 u64 block_len;
4142 int ret;
4143 int extent_inserted = 0;
4144 bool ordered_io_err = false;
4145
Josef Bacike7175a62018-05-23 11:58:34 -04004146 ret = log_extent_csums(trans, inode, root, em);
Filipe Manana8407f552014-09-05 15:14:39 +01004147 if (ret)
4148 return ret;
4149
4150 if (ordered_io_err) {
4151 ctx->io_err = -EIO;
Liu Boebb70442017-11-21 14:35:40 -07004152 return ctx->io_err;
Filipe Manana8407f552014-09-05 15:14:39 +01004153 }
4154
4155 btrfs_init_map_token(&token);
4156
Nikolay Borisov9d122622017-01-18 00:31:40 +02004157 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004158 em->start + em->len, NULL, 0, 1,
4159 sizeof(*fi), &extent_inserted);
4160 if (ret)
4161 return ret;
4162
4163 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004164 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004165 key.type = BTRFS_EXTENT_DATA_KEY;
4166 key.offset = em->start;
4167
4168 ret = btrfs_insert_empty_item(trans, log, path, &key,
4169 sizeof(*fi));
4170 if (ret)
4171 return ret;
4172 }
4173 leaf = path->nodes[0];
4174 fi = btrfs_item_ptr(leaf, path->slots[0],
4175 struct btrfs_file_extent_item);
4176
Josef Bacik50d9aa92014-11-21 14:52:38 -05004177 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004178 &token);
4179 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4180 btrfs_set_token_file_extent_type(leaf, fi,
4181 BTRFS_FILE_EXTENT_PREALLOC,
4182 &token);
4183 else
4184 btrfs_set_token_file_extent_type(leaf, fi,
4185 BTRFS_FILE_EXTENT_REG,
4186 &token);
4187
4188 block_len = max(em->block_len, em->orig_block_len);
4189 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4190 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4191 em->block_start,
4192 &token);
4193 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4194 &token);
4195 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4196 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4197 em->block_start -
4198 extent_offset, &token);
4199 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4200 &token);
4201 } else {
4202 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4203 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4204 &token);
4205 }
4206
4207 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4208 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4209 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4210 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4211 &token);
4212 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4213 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4214 btrfs_mark_buffer_dirty(leaf);
4215
4216 btrfs_release_path(path);
4217
4218 return ret;
4219}
4220
Filipe Manana31d11b82018-05-09 16:01:46 +01004221/*
4222 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4223 * lose them after doing a fast fsync and replaying the log. We scan the
4224 * subvolume's root instead of iterating the inode's extent map tree because
4225 * otherwise we can log incorrect extent items based on extent map conversion.
4226 * That can happen due to the fact that extent maps are merged when they
4227 * are not in the extent map tree's list of modified extents.
4228 */
4229static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4230 struct btrfs_inode *inode,
4231 struct btrfs_path *path)
4232{
4233 struct btrfs_root *root = inode->root;
4234 struct btrfs_key key;
4235 const u64 i_size = i_size_read(&inode->vfs_inode);
4236 const u64 ino = btrfs_ino(inode);
4237 struct btrfs_path *dst_path = NULL;
4238 u64 last_extent = (u64)-1;
4239 int ins_nr = 0;
4240 int start_slot;
4241 int ret;
4242
4243 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4244 return 0;
4245
4246 key.objectid = ino;
4247 key.type = BTRFS_EXTENT_DATA_KEY;
4248 key.offset = i_size;
4249 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4250 if (ret < 0)
4251 goto out;
4252
4253 while (true) {
4254 struct extent_buffer *leaf = path->nodes[0];
4255 int slot = path->slots[0];
4256
4257 if (slot >= btrfs_header_nritems(leaf)) {
4258 if (ins_nr > 0) {
4259 ret = copy_items(trans, inode, dst_path, path,
4260 &last_extent, start_slot,
4261 ins_nr, 1, 0);
4262 if (ret < 0)
4263 goto out;
4264 ins_nr = 0;
4265 }
4266 ret = btrfs_next_leaf(root, path);
4267 if (ret < 0)
4268 goto out;
4269 if (ret > 0) {
4270 ret = 0;
4271 break;
4272 }
4273 continue;
4274 }
4275
4276 btrfs_item_key_to_cpu(leaf, &key, slot);
4277 if (key.objectid > ino)
4278 break;
4279 if (WARN_ON_ONCE(key.objectid < ino) ||
4280 key.type < BTRFS_EXTENT_DATA_KEY ||
4281 key.offset < i_size) {
4282 path->slots[0]++;
4283 continue;
4284 }
4285 if (last_extent == (u64)-1) {
4286 last_extent = key.offset;
4287 /*
4288 * Avoid logging extent items logged in past fsync calls
4289 * and leading to duplicate keys in the log tree.
4290 */
4291 do {
4292 ret = btrfs_truncate_inode_items(trans,
4293 root->log_root,
4294 &inode->vfs_inode,
4295 i_size,
4296 BTRFS_EXTENT_DATA_KEY);
4297 } while (ret == -EAGAIN);
4298 if (ret)
4299 goto out;
4300 }
4301 if (ins_nr == 0)
4302 start_slot = slot;
4303 ins_nr++;
4304 path->slots[0]++;
4305 if (!dst_path) {
4306 dst_path = btrfs_alloc_path();
4307 if (!dst_path) {
4308 ret = -ENOMEM;
4309 goto out;
4310 }
4311 }
4312 }
4313 if (ins_nr > 0) {
4314 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4315 start_slot, ins_nr, 1, 0);
4316 if (ret > 0)
4317 ret = 0;
4318 }
4319out:
4320 btrfs_release_path(path);
4321 btrfs_free_path(dst_path);
4322 return ret;
4323}
4324
Josef Bacik5dc562c2012-08-17 13:14:17 -04004325static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4326 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004327 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004328 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004329 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004330 struct btrfs_log_ctx *ctx,
4331 const u64 start,
4332 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004333{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004334 struct extent_map *em, *n;
4335 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004336 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004337 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004338 u64 test_gen;
4339 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004340 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004341
4342 INIT_LIST_HEAD(&extents);
4343
Nikolay Borisov9d122622017-01-18 00:31:40 +02004344 down_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004345 write_lock(&tree->lock);
4346 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004347 logged_start = start;
4348 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004349
4350 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4351 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004352 /*
4353 * Just an arbitrary number, this can be really CPU intensive
4354 * once we start getting a lot of extents, and really once we
4355 * have a bunch of extents we just want to commit since it will
4356 * be faster.
4357 */
4358 if (++num > 32768) {
4359 list_del_init(&tree->modified_extents);
4360 ret = -EFBIG;
4361 goto process;
4362 }
4363
Josef Bacik5dc562c2012-08-17 13:14:17 -04004364 if (em->generation <= test_gen)
4365 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004366
Filipe Manana31d11b82018-05-09 16:01:46 +01004367 /* We log prealloc extents beyond eof later. */
4368 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4369 em->start >= i_size_read(&inode->vfs_inode))
4370 continue;
4371
Josef Bacik8c6c5922017-08-29 10:11:39 -04004372 if (em->start < logged_start)
4373 logged_start = em->start;
4374 if ((em->start + em->len - 1) > logged_end)
4375 logged_end = em->start + em->len - 1;
4376
Josef Bacikff44c6e2012-09-14 12:59:20 -04004377 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004378 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004379 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004380 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004381 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004382 }
4383
4384 list_sort(NULL, &extents, extent_cmp);
Josef Bacik8c6c5922017-08-29 10:11:39 -04004385 btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004386 /*
4387 * Some ordered extents started by fsync might have completed
4388 * before we could collect them into the list logged_list, which
4389 * means they're gone, not in our logged_list nor in the inode's
4390 * ordered tree. We want the application/user space to know an
4391 * error happened while attempting to persist file data so that
4392 * it can take proper action. If such error happened, we leave
4393 * without writing to the log tree and the fsync must report the
4394 * file data write error and not commit the current transaction.
4395 */
Nikolay Borisov9d122622017-01-18 00:31:40 +02004396 ret = filemap_check_errors(inode->vfs_inode.i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004397 if (ret)
4398 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004399process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004400 while (!list_empty(&extents)) {
4401 em = list_entry(extents.next, struct extent_map, list);
4402
4403 list_del_init(&em->list);
4404
4405 /*
4406 * If we had an error we just need to delete everybody from our
4407 * private list.
4408 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004409 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004410 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004411 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004412 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004413 }
4414
4415 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004416
Filipe Manana8407f552014-09-05 15:14:39 +01004417 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4418 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004419 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004420 clear_em_logging(tree, em);
4421 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004422 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004423 WARN_ON(!list_empty(&extents));
4424 write_unlock(&tree->lock);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004425 up_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004426
Josef Bacik5dc562c2012-08-17 13:14:17 -04004427 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004428 if (!ret)
4429 ret = btrfs_log_prealloc_extents(trans, inode, path);
4430
Josef Bacik5dc562c2012-08-17 13:14:17 -04004431 return ret;
4432}
4433
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004434static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004435 struct btrfs_path *path, u64 *size_ret)
4436{
4437 struct btrfs_key key;
4438 int ret;
4439
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004440 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004441 key.type = BTRFS_INODE_ITEM_KEY;
4442 key.offset = 0;
4443
4444 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4445 if (ret < 0) {
4446 return ret;
4447 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004448 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004449 } else {
4450 struct btrfs_inode_item *item;
4451
4452 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4453 struct btrfs_inode_item);
4454 *size_ret = btrfs_inode_size(path->nodes[0], item);
4455 }
4456
4457 btrfs_release_path(path);
4458 return 0;
4459}
4460
Filipe Manana36283bf2015-06-20 00:44:51 +01004461/*
4462 * At the moment we always log all xattrs. This is to figure out at log replay
4463 * time which xattrs must have their deletion replayed. If a xattr is missing
4464 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4465 * because if a xattr is deleted, the inode is fsynced and a power failure
4466 * happens, causing the log to be replayed the next time the fs is mounted,
4467 * we want the xattr to not exist anymore (same behaviour as other filesystems
4468 * with a journal, ext3/4, xfs, f2fs, etc).
4469 */
4470static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4471 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004472 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004473 struct btrfs_path *path,
4474 struct btrfs_path *dst_path)
4475{
4476 int ret;
4477 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004478 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004479 int ins_nr = 0;
4480 int start_slot = 0;
4481
4482 key.objectid = ino;
4483 key.type = BTRFS_XATTR_ITEM_KEY;
4484 key.offset = 0;
4485
4486 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4487 if (ret < 0)
4488 return ret;
4489
4490 while (true) {
4491 int slot = path->slots[0];
4492 struct extent_buffer *leaf = path->nodes[0];
4493 int nritems = btrfs_header_nritems(leaf);
4494
4495 if (slot >= nritems) {
4496 if (ins_nr > 0) {
4497 u64 last_extent = 0;
4498
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004499 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004500 &last_extent, start_slot,
4501 ins_nr, 1, 0);
4502 /* can't be 1, extent items aren't processed */
4503 ASSERT(ret <= 0);
4504 if (ret < 0)
4505 return ret;
4506 ins_nr = 0;
4507 }
4508 ret = btrfs_next_leaf(root, path);
4509 if (ret < 0)
4510 return ret;
4511 else if (ret > 0)
4512 break;
4513 continue;
4514 }
4515
4516 btrfs_item_key_to_cpu(leaf, &key, slot);
4517 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4518 break;
4519
4520 if (ins_nr == 0)
4521 start_slot = slot;
4522 ins_nr++;
4523 path->slots[0]++;
4524 cond_resched();
4525 }
4526 if (ins_nr > 0) {
4527 u64 last_extent = 0;
4528
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004529 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004530 &last_extent, start_slot,
4531 ins_nr, 1, 0);
4532 /* can't be 1, extent items aren't processed */
4533 ASSERT(ret <= 0);
4534 if (ret < 0)
4535 return ret;
4536 }
4537
4538 return 0;
4539}
4540
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004541/*
4542 * If the no holes feature is enabled we need to make sure any hole between the
4543 * last extent and the i_size of our inode is explicitly marked in the log. This
4544 * is to make sure that doing something like:
4545 *
4546 * 1) create file with 128Kb of data
4547 * 2) truncate file to 64Kb
4548 * 3) truncate file to 256Kb
4549 * 4) fsync file
4550 * 5) <crash/power failure>
4551 * 6) mount fs and trigger log replay
4552 *
4553 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4554 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4555 * file correspond to a hole. The presence of explicit holes in a log tree is
4556 * what guarantees that log replay will remove/adjust file extent items in the
4557 * fs/subvol tree.
4558 *
4559 * Here we do not need to care about holes between extents, that is already done
4560 * by copy_items(). We also only need to do this in the full sync path, where we
4561 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4562 * lookup the list of modified extent maps and if any represents a hole, we
4563 * insert a corresponding extent representing a hole in the log tree.
4564 */
4565static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4566 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004567 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004568 struct btrfs_path *path)
4569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004570 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004571 int ret;
4572 struct btrfs_key key;
4573 u64 hole_start;
4574 u64 hole_size;
4575 struct extent_buffer *leaf;
4576 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004577 const u64 ino = btrfs_ino(inode);
4578 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004579
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004580 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004581 return 0;
4582
4583 key.objectid = ino;
4584 key.type = BTRFS_EXTENT_DATA_KEY;
4585 key.offset = (u64)-1;
4586
4587 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4588 ASSERT(ret != 0);
4589 if (ret < 0)
4590 return ret;
4591
4592 ASSERT(path->slots[0] > 0);
4593 path->slots[0]--;
4594 leaf = path->nodes[0];
4595 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4596
4597 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4598 /* inode does not have any extents */
4599 hole_start = 0;
4600 hole_size = i_size;
4601 } else {
4602 struct btrfs_file_extent_item *extent;
4603 u64 len;
4604
4605 /*
4606 * If there's an extent beyond i_size, an explicit hole was
4607 * already inserted by copy_items().
4608 */
4609 if (key.offset >= i_size)
4610 return 0;
4611
4612 extent = btrfs_item_ptr(leaf, path->slots[0],
4613 struct btrfs_file_extent_item);
4614
4615 if (btrfs_file_extent_type(leaf, extent) ==
4616 BTRFS_FILE_EXTENT_INLINE) {
4617 len = btrfs_file_extent_inline_len(leaf,
4618 path->slots[0],
4619 extent);
Filipe Manana6399fb52017-07-28 15:22:36 +01004620 ASSERT(len == i_size ||
4621 (len == fs_info->sectorsize &&
4622 btrfs_file_extent_compression(leaf, extent) !=
4623 BTRFS_COMPRESS_NONE));
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004624 return 0;
4625 }
4626
4627 len = btrfs_file_extent_num_bytes(leaf, extent);
4628 /* Last extent goes beyond i_size, no need to log a hole. */
4629 if (key.offset + len > i_size)
4630 return 0;
4631 hole_start = key.offset + len;
4632 hole_size = i_size - hole_start;
4633 }
4634 btrfs_release_path(path);
4635
4636 /* Last extent ends at i_size. */
4637 if (hole_size == 0)
4638 return 0;
4639
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004640 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004641 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4642 hole_size, 0, hole_size, 0, 0, 0);
4643 return ret;
4644}
4645
Filipe Manana56f23fd2016-03-30 23:37:21 +01004646/*
4647 * When we are logging a new inode X, check if it doesn't have a reference that
4648 * matches the reference from some other inode Y created in a past transaction
4649 * and that was renamed in the current transaction. If we don't do this, then at
4650 * log replay time we can lose inode Y (and all its files if it's a directory):
4651 *
4652 * mkdir /mnt/x
4653 * echo "hello world" > /mnt/x/foobar
4654 * sync
4655 * mv /mnt/x /mnt/y
4656 * mkdir /mnt/x # or touch /mnt/x
4657 * xfs_io -c fsync /mnt/x
4658 * <power fail>
4659 * mount fs, trigger log replay
4660 *
4661 * After the log replay procedure, we would lose the first directory and all its
4662 * files (file foobar).
4663 * For the case where inode Y is not a directory we simply end up losing it:
4664 *
4665 * echo "123" > /mnt/foo
4666 * sync
4667 * mv /mnt/foo /mnt/bar
4668 * echo "abc" > /mnt/foo
4669 * xfs_io -c fsync /mnt/foo
4670 * <power fail>
4671 *
4672 * We also need this for cases where a snapshot entry is replaced by some other
4673 * entry (file or directory) otherwise we end up with an unreplayable log due to
4674 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4675 * if it were a regular entry:
4676 *
4677 * mkdir /mnt/x
4678 * btrfs subvolume snapshot /mnt /mnt/x/snap
4679 * btrfs subvolume delete /mnt/x/snap
4680 * rmdir /mnt/x
4681 * mkdir /mnt/x
4682 * fsync /mnt/x or fsync some new file inside it
4683 * <power fail>
4684 *
4685 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4686 * the same transaction.
4687 */
4688static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4689 const int slot,
4690 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004691 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004692 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004693{
4694 int ret;
4695 struct btrfs_path *search_path;
4696 char *name = NULL;
4697 u32 name_len = 0;
4698 u32 item_size = btrfs_item_size_nr(eb, slot);
4699 u32 cur_offset = 0;
4700 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4701
4702 search_path = btrfs_alloc_path();
4703 if (!search_path)
4704 return -ENOMEM;
4705 search_path->search_commit_root = 1;
4706 search_path->skip_locking = 1;
4707
4708 while (cur_offset < item_size) {
4709 u64 parent;
4710 u32 this_name_len;
4711 u32 this_len;
4712 unsigned long name_ptr;
4713 struct btrfs_dir_item *di;
4714
4715 if (key->type == BTRFS_INODE_REF_KEY) {
4716 struct btrfs_inode_ref *iref;
4717
4718 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4719 parent = key->offset;
4720 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4721 name_ptr = (unsigned long)(iref + 1);
4722 this_len = sizeof(*iref) + this_name_len;
4723 } else {
4724 struct btrfs_inode_extref *extref;
4725
4726 extref = (struct btrfs_inode_extref *)(ptr +
4727 cur_offset);
4728 parent = btrfs_inode_extref_parent(eb, extref);
4729 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4730 name_ptr = (unsigned long)&extref->name;
4731 this_len = sizeof(*extref) + this_name_len;
4732 }
4733
4734 if (this_name_len > name_len) {
4735 char *new_name;
4736
4737 new_name = krealloc(name, this_name_len, GFP_NOFS);
4738 if (!new_name) {
4739 ret = -ENOMEM;
4740 goto out;
4741 }
4742 name_len = this_name_len;
4743 name = new_name;
4744 }
4745
4746 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004747 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4748 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004749 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004750 struct btrfs_key di_key;
4751
4752 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4753 di, &di_key);
4754 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4755 ret = 1;
4756 *other_ino = di_key.objectid;
4757 } else {
4758 ret = -EAGAIN;
4759 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004760 goto out;
4761 } else if (IS_ERR(di)) {
4762 ret = PTR_ERR(di);
4763 goto out;
4764 }
4765 btrfs_release_path(search_path);
4766
4767 cur_offset += this_len;
4768 }
4769 ret = 0;
4770out:
4771 btrfs_free_path(search_path);
4772 kfree(name);
4773 return ret;
4774}
4775
Chris Masone02119d2008-09-05 16:13:11 -04004776/* log a single inode in the tree log.
4777 * At least one parent directory for this inode must exist in the tree
4778 * or be logged already.
4779 *
4780 * Any items from this inode changed by the current transaction are copied
4781 * to the log tree. An extra reference is taken on any extents in this
4782 * file, allowing us to avoid a whole pile of corner cases around logging
4783 * blocks that have been removed from the tree.
4784 *
4785 * See LOG_INODE_ALL and related defines for a description of what inode_only
4786 * does.
4787 *
4788 * This handles both files and directories.
4789 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004790static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004791 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004792 int inode_only,
4793 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004794 const loff_t end,
4795 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004796{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004797 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004798 struct btrfs_path *path;
4799 struct btrfs_path *dst_path;
4800 struct btrfs_key min_key;
4801 struct btrfs_key max_key;
4802 struct btrfs_root *log = root->log_root;
Miao Xie827463c2014-01-14 20:31:51 +08004803 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004804 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004805 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004806 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004807 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004808 int ins_start_slot = 0;
4809 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004810 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004811 u64 ino = btrfs_ino(inode);
4812 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004813 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004814 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01004815 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004816
Chris Masone02119d2008-09-05 16:13:11 -04004817 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004818 if (!path)
4819 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004820 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004821 if (!dst_path) {
4822 btrfs_free_path(path);
4823 return -ENOMEM;
4824 }
Chris Masone02119d2008-09-05 16:13:11 -04004825
Li Zefan33345d012011-04-20 10:31:50 +08004826 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004827 min_key.type = BTRFS_INODE_ITEM_KEY;
4828 min_key.offset = 0;
4829
Li Zefan33345d012011-04-20 10:31:50 +08004830 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004831
Chris Mason12fcfd22009-03-24 10:24:20 -04004832
Josef Bacik5dc562c2012-08-17 13:14:17 -04004833 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004834 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004835 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004836 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004837 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004838 max_key.type = BTRFS_XATTR_ITEM_KEY;
4839 else
4840 max_key.type = (u8)-1;
4841 max_key.offset = (u64)-1;
4842
Filipe Manana2c2c4522015-01-13 16:40:04 +00004843 /*
4844 * Only run delayed items if we are a dir or a new file.
4845 * Otherwise commit the delayed inode only, which is needed in
4846 * order for the log replay code to mark inodes for link count
4847 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4848 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004849 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4850 inode->generation > fs_info->last_trans_committed)
4851 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004852 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004853 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004854
4855 if (ret) {
4856 btrfs_free_path(path);
4857 btrfs_free_path(dst_path);
4858 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004859 }
4860
Liu Bo781feef2016-11-30 16:20:25 -08004861 if (inode_only == LOG_OTHER_INODE) {
4862 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004863 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004864 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004865 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004866 }
Chris Masone02119d2008-09-05 16:13:11 -04004867
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004868 /*
Chris Masone02119d2008-09-05 16:13:11 -04004869 * a brute force approach to making sure we get the most uptodate
4870 * copies of everything.
4871 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004872 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004873 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4874
Filipe Manana4f764e52015-02-23 19:53:35 +00004875 if (inode_only == LOG_INODE_EXISTS)
4876 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004877 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004878 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004879 if (inode_only == LOG_INODE_EXISTS) {
4880 /*
4881 * Make sure the new inode item we write to the log has
4882 * the same isize as the current one (if it exists).
4883 * This is necessary to prevent data loss after log
4884 * replay, and also to prevent doing a wrong expanding
4885 * truncate - for e.g. create file, write 4K into offset
4886 * 0, fsync, write 4K into offset 4096, add hard link,
4887 * fsync some other file (to sync log), power fail - if
4888 * we use the inode's current i_size, after log replay
4889 * we get a 8Kb file, with the last 4Kb extent as a hole
4890 * (zeroes), as if an expanding truncate happened,
4891 * instead of getting a file of 4Kb only.
4892 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004893 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004894 if (err)
4895 goto out_unlock;
4896 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004897 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004898 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004899 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004900 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004901 ret = drop_objectid_items(trans, log, path, ino,
4902 max_key.type);
4903 } else {
4904 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004905 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00004906 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004907 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004908 while(1) {
4909 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004910 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08004911 if (ret != -EAGAIN)
4912 break;
4913 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004914 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004915 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004916 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004917 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004918 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004919 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004920 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004921 ret = drop_objectid_items(trans, log, path, ino,
4922 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004923 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004924 if (inode_only == LOG_INODE_ALL)
4925 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004926 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004927 }
Josef Bacika95249b2012-10-11 16:17:34 -04004928
Chris Masone02119d2008-09-05 16:13:11 -04004929 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004930 if (ret) {
4931 err = ret;
4932 goto out_unlock;
4933 }
Chris Masone02119d2008-09-05 16:13:11 -04004934
Chris Masond3977122009-01-05 21:25:51 -05004935 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004936 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004937 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004938 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004939 if (ret < 0) {
4940 err = ret;
4941 goto out_unlock;
4942 }
Chris Masone02119d2008-09-05 16:13:11 -04004943 if (ret != 0)
4944 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004945again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004946 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004947 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004948 break;
4949 if (min_key.type > max_key.type)
4950 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004951
Filipe Mananae4545de2015-06-17 12:49:23 +01004952 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4953 need_log_inode_item = false;
4954
Filipe Manana56f23fd2016-03-30 23:37:21 +01004955 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4956 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02004957 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004958 u64 other_ino = 0;
4959
Filipe Manana56f23fd2016-03-30 23:37:21 +01004960 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02004961 path->slots[0], &min_key, inode,
4962 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004963 if (ret < 0) {
4964 err = ret;
4965 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004966 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004967 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004968 struct btrfs_key inode_key;
4969 struct inode *other_inode;
4970
4971 if (ins_nr > 0) {
4972 ins_nr++;
4973 } else {
4974 ins_nr = 1;
4975 ins_start_slot = path->slots[0];
4976 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02004977 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01004978 &last_extent, ins_start_slot,
4979 ins_nr, inode_only,
4980 logged_isize);
4981 if (ret < 0) {
4982 err = ret;
4983 goto out_unlock;
4984 }
4985 ins_nr = 0;
4986 btrfs_release_path(path);
4987 inode_key.objectid = other_ino;
4988 inode_key.type = BTRFS_INODE_ITEM_KEY;
4989 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004990 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01004991 &inode_key, root,
4992 NULL);
4993 /*
4994 * If the other inode that had a conflicting dir
4995 * entry was deleted in the current transaction,
4996 * we don't need to do more work nor fallback to
4997 * a transaction commit.
4998 */
4999 if (IS_ERR(other_inode) &&
5000 PTR_ERR(other_inode) == -ENOENT) {
5001 goto next_key;
5002 } else if (IS_ERR(other_inode)) {
5003 err = PTR_ERR(other_inode);
5004 goto out_unlock;
5005 }
5006 /*
5007 * We are safe logging the other inode without
5008 * acquiring its i_mutex as long as we log with
5009 * the LOG_INODE_EXISTS mode. We're safe against
5010 * concurrent renames of the other inode as well
5011 * because during a rename we pin the log and
5012 * update the log with the new name before we
5013 * unpin it.
5014 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005015 err = btrfs_log_inode(trans, root,
5016 BTRFS_I(other_inode),
5017 LOG_OTHER_INODE, 0, LLONG_MAX,
5018 ctx);
Filipe Manana44f714d2016-06-06 16:11:13 +01005019 iput(other_inode);
5020 if (err)
5021 goto out_unlock;
5022 else
5023 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005024 }
5025 }
5026
Filipe Manana36283bf2015-06-20 00:44:51 +01005027 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5028 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5029 if (ins_nr == 0)
5030 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005031 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005032 &last_extent, ins_start_slot,
5033 ins_nr, inode_only, logged_isize);
5034 if (ret < 0) {
5035 err = ret;
5036 goto out_unlock;
5037 }
5038 ins_nr = 0;
5039 if (ret) {
5040 btrfs_release_path(path);
5041 continue;
5042 }
5043 goto next_slot;
5044 }
5045
Chris Mason31ff1cd2008-09-11 16:17:57 -04005046 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5047 ins_nr++;
5048 goto next_slot;
5049 } else if (!ins_nr) {
5050 ins_start_slot = path->slots[0];
5051 ins_nr = 1;
5052 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005053 }
5054
Nikolay Borisova59108a2017-01-18 00:31:48 +02005055 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005056 ins_start_slot, ins_nr, inode_only,
5057 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005058 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005059 err = ret;
5060 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005061 }
5062 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005063 ins_nr = 0;
5064 btrfs_release_path(path);
5065 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005066 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005067 ins_nr = 1;
5068 ins_start_slot = path->slots[0];
5069next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005070
Chris Mason3a5f1d42008-09-11 15:53:37 -04005071 nritems = btrfs_header_nritems(path->nodes[0]);
5072 path->slots[0]++;
5073 if (path->slots[0] < nritems) {
5074 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5075 path->slots[0]);
5076 goto again;
5077 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005078 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005079 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005080 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005081 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005082 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005083 err = ret;
5084 goto out_unlock;
5085 }
Josef Bacik16e75492013-10-22 12:18:51 -04005086 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005087 ins_nr = 0;
5088 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005089 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005090next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005091 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005092 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005093 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005094 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005095 min_key.offset = 0;
5096 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005097 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005098 }
Chris Masone02119d2008-09-05 16:13:11 -04005099 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005100 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005101 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005102 ins_start_slot, ins_nr, inode_only,
5103 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005104 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005105 err = ret;
5106 goto out_unlock;
5107 }
Josef Bacik16e75492013-10-22 12:18:51 -04005108 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005109 ins_nr = 0;
5110 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005111
Filipe Manana36283bf2015-06-20 00:44:51 +01005112 btrfs_release_path(path);
5113 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005114 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005115 if (err)
5116 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005117 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005118 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5119 btrfs_release_path(path);
5120 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005121 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005122 if (err)
5123 goto out_unlock;
5124 }
Josef Bacika95249b2012-10-11 16:17:34 -04005125log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005126 btrfs_release_path(path);
5127 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005128 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005129 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005130 if (!err && !xattrs_logged) {
5131 err = btrfs_log_all_xattrs(trans, root, inode, path,
5132 dst_path);
5133 btrfs_release_path(path);
5134 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005135 if (err)
5136 goto out_unlock;
5137 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005138 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005139 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00005140 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005141 if (ret) {
5142 err = ret;
5143 goto out_unlock;
5144 }
Josef Bacikd006a042013-11-12 20:54:09 -05005145 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005146 struct extent_map *em, *n;
5147
Filipe Manana49dae1b2014-09-06 22:34:39 +01005148 write_lock(&em_tree->lock);
5149 /*
5150 * We can't just remove every em if we're called for a ranged
5151 * fsync - that is, one that doesn't cover the whole possible
5152 * file range (0 to LLONG_MAX). This is because we can have
5153 * em's that fall outside the range we're logging and therefore
5154 * their ordered operations haven't completed yet
5155 * (btrfs_finish_ordered_io() not invoked yet). This means we
5156 * didn't get their respective file extent item in the fs/subvol
5157 * tree yet, and need to let the next fast fsync (one which
5158 * consults the list of modified extent maps) find the em so
5159 * that it logs a matching file extent item and waits for the
5160 * respective ordered operation to complete (if it's still
5161 * running).
5162 *
5163 * Removing every em outside the range we're logging would make
5164 * the next fast fsync not log their matching file extent items,
5165 * therefore making us lose data after a log replay.
5166 */
5167 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5168 list) {
5169 const u64 mod_end = em->mod_start + em->mod_len - 1;
5170
5171 if (em->mod_start >= start && mod_end <= end)
5172 list_del_init(&em->list);
5173 }
5174 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005175 }
5176
Nikolay Borisova59108a2017-01-18 00:31:48 +02005177 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5178 ret = log_directory_changes(trans, root, inode, path, dst_path,
5179 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005180 if (ret) {
5181 err = ret;
5182 goto out_unlock;
5183 }
Chris Masone02119d2008-09-05 16:13:11 -04005184 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005185
Nikolay Borisova59108a2017-01-18 00:31:48 +02005186 spin_lock(&inode->lock);
5187 inode->logged_trans = trans->transid;
5188 inode->last_log_commit = inode->last_sub_trans;
5189 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005190out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005191 if (unlikely(err))
5192 btrfs_put_logged_extents(&logged_list);
5193 else
5194 btrfs_submit_logged_extents(&logged_list, log);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005195 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005196
5197 btrfs_free_path(path);
5198 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005199 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005200}
5201
Chris Mason12fcfd22009-03-24 10:24:20 -04005202/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005203 * Check if we must fallback to a transaction commit when logging an inode.
5204 * This must be called after logging the inode and is used only in the context
5205 * when fsyncing an inode requires the need to log some other inode - in which
5206 * case we can't lock the i_mutex of each other inode we need to log as that
5207 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5208 * log inodes up or down in the hierarchy) or rename operations for example. So
5209 * we take the log_mutex of the inode after we have logged it and then check for
5210 * its last_unlink_trans value - this is safe because any task setting
5211 * last_unlink_trans must take the log_mutex and it must do this before it does
5212 * the actual unlink operation, so if we do this check before a concurrent task
5213 * sets last_unlink_trans it means we've logged a consistent version/state of
5214 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005215 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005216 * we logged the inode or it might have also done the unlink).
5217 */
5218static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005219 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005220{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005221 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005222 bool ret = false;
5223
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005224 mutex_lock(&inode->log_mutex);
5225 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005226 /*
5227 * Make sure any commits to the log are forced to be full
5228 * commits.
5229 */
5230 btrfs_set_log_full_commit(fs_info, trans);
5231 ret = true;
5232 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005233 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005234
5235 return ret;
5236}
5237
5238/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005239 * follow the dentry parent pointers up the chain and see if any
5240 * of the directories in it require a full commit before they can
5241 * be logged. Returns zero if nothing special needs to be done or 1 if
5242 * a full commit is required.
5243 */
5244static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005245 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005246 struct dentry *parent,
5247 struct super_block *sb,
5248 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005249{
Chris Mason12fcfd22009-03-24 10:24:20 -04005250 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005251 struct dentry *old_parent = NULL;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005252 struct btrfs_inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005253
Chris Masonaf4176b2009-03-24 10:24:31 -04005254 /*
5255 * for regular files, if its inode is already on disk, we don't
5256 * have to worry about the parents at all. This is because
5257 * we can use the last_unlink_trans field to record renames
5258 * and other fun in this file.
5259 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005260 if (S_ISREG(inode->vfs_inode.i_mode) &&
5261 inode->generation <= last_committed &&
5262 inode->last_unlink_trans <= last_committed)
5263 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005264
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005265 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005266 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005267 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005268 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005269 }
5270
5271 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005272 /*
5273 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005274 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005275 * logged_trans so that further down in the log code we don't
5276 * think this inode has already been logged.
5277 */
5278 if (inode != orig_inode)
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005279 inode->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005280 smp_mb();
5281
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005282 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005283 ret = 1;
5284 break;
5285 }
5286
Al Virofc640052016-04-10 01:33:30 -04005287 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005288 break;
5289
Filipe Manana44f714d2016-06-06 16:11:13 +01005290 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005291 inode = BTRFS_I(d_inode(parent));
5292 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005293 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005294 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005295 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005296
Josef Bacik6a912212010-11-20 09:48:00 +00005297 parent = dget_parent(parent);
5298 dput(old_parent);
5299 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005300 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005301
5302 }
Josef Bacik6a912212010-11-20 09:48:00 +00005303 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005304out:
Chris Masone02119d2008-09-05 16:13:11 -04005305 return ret;
5306}
5307
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005308struct btrfs_dir_list {
5309 u64 ino;
5310 struct list_head list;
5311};
5312
5313/*
5314 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5315 * details about the why it is needed.
5316 * This is a recursive operation - if an existing dentry corresponds to a
5317 * directory, that directory's new entries are logged too (same behaviour as
5318 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5319 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5320 * complains about the following circular lock dependency / possible deadlock:
5321 *
5322 * CPU0 CPU1
5323 * ---- ----
5324 * lock(&type->i_mutex_dir_key#3/2);
5325 * lock(sb_internal#2);
5326 * lock(&type->i_mutex_dir_key#3/2);
5327 * lock(&sb->s_type->i_mutex_key#14);
5328 *
5329 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5330 * sb_start_intwrite() in btrfs_start_transaction().
5331 * Not locking i_mutex of the inodes is still safe because:
5332 *
5333 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5334 * that while logging the inode new references (names) are added or removed
5335 * from the inode, leaving the logged inode item with a link count that does
5336 * not match the number of logged inode reference items. This is fine because
5337 * at log replay time we compute the real number of links and correct the
5338 * link count in the inode item (see replay_one_buffer() and
5339 * link_to_fixup_dir());
5340 *
5341 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5342 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5343 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5344 * has a size that doesn't match the sum of the lengths of all the logged
5345 * names. This does not result in a problem because if a dir_item key is
5346 * logged but its matching dir_index key is not logged, at log replay time we
5347 * don't use it to replay the respective name (see replay_one_name()). On the
5348 * other hand if only the dir_index key ends up being logged, the respective
5349 * name is added to the fs/subvol tree with both the dir_item and dir_index
5350 * keys created (see replay_one_name()).
5351 * The directory's inode item with a wrong i_size is not a problem as well,
5352 * since we don't use it at log replay time to set the i_size in the inode
5353 * item of the fs/subvol tree (see overwrite_item()).
5354 */
5355static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5356 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005357 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005358 struct btrfs_log_ctx *ctx)
5359{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005360 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005361 struct btrfs_root *log = root->log_root;
5362 struct btrfs_path *path;
5363 LIST_HEAD(dir_list);
5364 struct btrfs_dir_list *dir_elem;
5365 int ret = 0;
5366
5367 path = btrfs_alloc_path();
5368 if (!path)
5369 return -ENOMEM;
5370
5371 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5372 if (!dir_elem) {
5373 btrfs_free_path(path);
5374 return -ENOMEM;
5375 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005376 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005377 list_add_tail(&dir_elem->list, &dir_list);
5378
5379 while (!list_empty(&dir_list)) {
5380 struct extent_buffer *leaf;
5381 struct btrfs_key min_key;
5382 int nritems;
5383 int i;
5384
5385 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5386 list);
5387 if (ret)
5388 goto next_dir_inode;
5389
5390 min_key.objectid = dir_elem->ino;
5391 min_key.type = BTRFS_DIR_ITEM_KEY;
5392 min_key.offset = 0;
5393again:
5394 btrfs_release_path(path);
5395 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5396 if (ret < 0) {
5397 goto next_dir_inode;
5398 } else if (ret > 0) {
5399 ret = 0;
5400 goto next_dir_inode;
5401 }
5402
5403process_leaf:
5404 leaf = path->nodes[0];
5405 nritems = btrfs_header_nritems(leaf);
5406 for (i = path->slots[0]; i < nritems; i++) {
5407 struct btrfs_dir_item *di;
5408 struct btrfs_key di_key;
5409 struct inode *di_inode;
5410 struct btrfs_dir_list *new_dir_elem;
5411 int log_mode = LOG_INODE_EXISTS;
5412 int type;
5413
5414 btrfs_item_key_to_cpu(leaf, &min_key, i);
5415 if (min_key.objectid != dir_elem->ino ||
5416 min_key.type != BTRFS_DIR_ITEM_KEY)
5417 goto next_dir_inode;
5418
5419 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5420 type = btrfs_dir_type(leaf, di);
5421 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5422 type != BTRFS_FT_DIR)
5423 continue;
5424 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5425 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5426 continue;
5427
Robbie Koec125cf2016-10-28 10:48:26 +08005428 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005429 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005430 if (IS_ERR(di_inode)) {
5431 ret = PTR_ERR(di_inode);
5432 goto next_dir_inode;
5433 }
5434
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005435 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005436 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005437 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005438 }
5439
5440 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005441 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005442 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005443 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005444 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005445 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005446 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005447 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005448 iput(di_inode);
5449 if (ret)
5450 goto next_dir_inode;
5451 if (ctx->log_new_dentries) {
5452 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5453 GFP_NOFS);
5454 if (!new_dir_elem) {
5455 ret = -ENOMEM;
5456 goto next_dir_inode;
5457 }
5458 new_dir_elem->ino = di_key.objectid;
5459 list_add_tail(&new_dir_elem->list, &dir_list);
5460 }
5461 break;
5462 }
5463 if (i == nritems) {
5464 ret = btrfs_next_leaf(log, path);
5465 if (ret < 0) {
5466 goto next_dir_inode;
5467 } else if (ret > 0) {
5468 ret = 0;
5469 goto next_dir_inode;
5470 }
5471 goto process_leaf;
5472 }
5473 if (min_key.offset < (u64)-1) {
5474 min_key.offset++;
5475 goto again;
5476 }
5477next_dir_inode:
5478 list_del(&dir_elem->list);
5479 kfree(dir_elem);
5480 }
5481
5482 btrfs_free_path(path);
5483 return ret;
5484}
5485
Filipe Manana18aa0922015-08-05 16:49:08 +01005486static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005487 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005488 struct btrfs_log_ctx *ctx)
5489{
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005490 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Filipe Manana18aa0922015-08-05 16:49:08 +01005491 int ret;
5492 struct btrfs_path *path;
5493 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005494 struct btrfs_root *root = inode->root;
5495 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005496
5497 path = btrfs_alloc_path();
5498 if (!path)
5499 return -ENOMEM;
5500 path->skip_locking = 1;
5501 path->search_commit_root = 1;
5502
5503 key.objectid = ino;
5504 key.type = BTRFS_INODE_REF_KEY;
5505 key.offset = 0;
5506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5507 if (ret < 0)
5508 goto out;
5509
5510 while (true) {
5511 struct extent_buffer *leaf = path->nodes[0];
5512 int slot = path->slots[0];
5513 u32 cur_offset = 0;
5514 u32 item_size;
5515 unsigned long ptr;
5516
5517 if (slot >= btrfs_header_nritems(leaf)) {
5518 ret = btrfs_next_leaf(root, path);
5519 if (ret < 0)
5520 goto out;
5521 else if (ret > 0)
5522 break;
5523 continue;
5524 }
5525
5526 btrfs_item_key_to_cpu(leaf, &key, slot);
5527 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5528 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5529 break;
5530
5531 item_size = btrfs_item_size_nr(leaf, slot);
5532 ptr = btrfs_item_ptr_offset(leaf, slot);
5533 while (cur_offset < item_size) {
5534 struct btrfs_key inode_key;
5535 struct inode *dir_inode;
5536
5537 inode_key.type = BTRFS_INODE_ITEM_KEY;
5538 inode_key.offset = 0;
5539
5540 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5541 struct btrfs_inode_extref *extref;
5542
5543 extref = (struct btrfs_inode_extref *)
5544 (ptr + cur_offset);
5545 inode_key.objectid = btrfs_inode_extref_parent(
5546 leaf, extref);
5547 cur_offset += sizeof(*extref);
5548 cur_offset += btrfs_inode_extref_name_len(leaf,
5549 extref);
5550 } else {
5551 inode_key.objectid = key.offset;
5552 cur_offset = item_size;
5553 }
5554
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005555 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005556 root, NULL);
5557 /* If parent inode was deleted, skip it. */
5558 if (IS_ERR(dir_inode))
5559 continue;
5560
Filipe Manana657ed1a2016-04-06 17:11:56 +01005561 if (ctx)
5562 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005563 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005564 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005565 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005566 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005567 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005568 if (!ret && ctx && ctx->log_new_dentries)
5569 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005570 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005571 iput(dir_inode);
5572 if (ret)
5573 goto out;
5574 }
5575 path->slots[0]++;
5576 }
5577 ret = 0;
5578out:
5579 btrfs_free_path(path);
5580 return ret;
5581}
5582
Chris Masone02119d2008-09-05 16:13:11 -04005583/*
5584 * helper function around btrfs_log_inode to make sure newly created
5585 * parent directories also end up in the log. A minimal inode and backref
5586 * only logging is done of any parent directories that are older than
5587 * the last committed transaction
5588 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005589static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005590 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005591 struct dentry *parent,
5592 const loff_t start,
5593 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005594 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005595 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005596{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005597 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005598 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005599 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005600 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005601 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005602 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005603 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005604 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005605
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005606 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005607
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005608 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005609 ret = 1;
5610 goto end_no_trans;
5611 }
5612
Miao Xie995946d2014-04-02 19:51:06 +08005613 /*
5614 * The prev transaction commit doesn't complete, we need do
5615 * full commit by ourselves.
5616 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005617 if (fs_info->last_trans_log_full_commit >
5618 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005619 ret = 1;
5620 goto end_no_trans;
5621 }
5622
Nikolay Borisovf8822742018-02-27 17:37:17 +02005623 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005624 ret = 1;
5625 goto end_no_trans;
5626 }
5627
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005628 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5629 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005630 if (ret)
5631 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005632
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005633 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005634 ret = BTRFS_NO_LOG_SYNC;
5635 goto end_no_trans;
5636 }
5637
Miao Xie8b050d32014-02-20 18:08:58 +08005638 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005639 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005640 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005641
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005642 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005643 if (ret)
5644 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005645
Chris Masonaf4176b2009-03-24 10:24:31 -04005646 /*
5647 * for regular files, if its inode is already on disk, we don't
5648 * have to worry about the parents at all. This is because
5649 * we can use the last_unlink_trans field to record renames
5650 * and other fun in this file.
5651 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005652 if (S_ISREG(inode->vfs_inode.i_mode) &&
5653 inode->generation <= last_committed &&
5654 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005655 ret = 0;
5656 goto end_trans;
5657 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005658
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005659 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005660 log_dentries = true;
5661
Filipe Manana18aa0922015-08-05 16:49:08 +01005662 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005663 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005664 * inodes are fully logged. This is to prevent leaving dangling
5665 * directory index entries in directories that were our parents but are
5666 * not anymore. Not doing this results in old parent directory being
5667 * impossible to delete after log replay (rmdir will always fail with
5668 * error -ENOTEMPTY).
5669 *
5670 * Example 1:
5671 *
5672 * mkdir testdir
5673 * touch testdir/foo
5674 * ln testdir/foo testdir/bar
5675 * sync
5676 * unlink testdir/bar
5677 * xfs_io -c fsync testdir/foo
5678 * <power failure>
5679 * mount fs, triggers log replay
5680 *
5681 * If we don't log the parent directory (testdir), after log replay the
5682 * directory still has an entry pointing to the file inode using the bar
5683 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5684 * the file inode has a link count of 1.
5685 *
5686 * Example 2:
5687 *
5688 * mkdir testdir
5689 * touch foo
5690 * ln foo testdir/foo2
5691 * ln foo testdir/foo3
5692 * sync
5693 * unlink testdir/foo3
5694 * xfs_io -c fsync foo
5695 * <power failure>
5696 * mount fs, triggers log replay
5697 *
5698 * Similar as the first example, after log replay the parent directory
5699 * testdir still has an entry pointing to the inode file with name foo3
5700 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5701 * and has a link count of 2.
5702 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005703 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005704 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5705 if (ret)
5706 goto end_trans;
5707 }
5708
Chris Masond3977122009-01-05 21:25:51 -05005709 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005710 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005711 break;
5712
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005713 inode = BTRFS_I(d_inode(parent));
5714 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005715 break;
5716
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005717 if (inode->generation > last_committed) {
5718 ret = btrfs_log_inode(trans, root, inode,
5719 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005720 if (ret)
5721 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005722 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005723 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005724 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005725
Josef Bacik6a912212010-11-20 09:48:00 +00005726 parent = dget_parent(parent);
5727 dput(old_parent);
5728 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005729 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005730 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005731 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005732 else
5733 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005734end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005735 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005736 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005737 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005738 ret = 1;
5739 }
Miao Xie8b050d32014-02-20 18:08:58 +08005740
5741 if (ret)
5742 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005743 btrfs_end_log_trans(root);
5744end_no_trans:
5745 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005746}
5747
5748/*
5749 * it is not safe to log dentry if the chunk root has added new
5750 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5751 * If this returns 1, you must commit the transaction to safely get your
5752 * data on disk.
5753 */
5754int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02005755 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005756 const loff_t start,
5757 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005758 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005759{
Josef Bacik6a912212010-11-20 09:48:00 +00005760 struct dentry *parent = dget_parent(dentry);
5761 int ret;
5762
Nikolay Borisovf8822742018-02-27 17:37:17 +02005763 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5764 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005765 dput(parent);
5766
5767 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005768}
5769
5770/*
5771 * should be called during mount to recover any replay any log trees
5772 * from the FS
5773 */
5774int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5775{
5776 int ret;
5777 struct btrfs_path *path;
5778 struct btrfs_trans_handle *trans;
5779 struct btrfs_key key;
5780 struct btrfs_key found_key;
5781 struct btrfs_key tmp_key;
5782 struct btrfs_root *log;
5783 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5784 struct walk_control wc = {
5785 .process_func = process_one_buffer,
5786 .stage = 0,
5787 };
5788
Chris Masone02119d2008-09-05 16:13:11 -04005789 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005790 if (!path)
5791 return -ENOMEM;
5792
Josef Bacikafcdd122016-09-02 15:40:02 -04005793 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005794
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005795 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005796 if (IS_ERR(trans)) {
5797 ret = PTR_ERR(trans);
5798 goto error;
5799 }
Chris Masone02119d2008-09-05 16:13:11 -04005800
5801 wc.trans = trans;
5802 wc.pin = 1;
5803
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005804 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005805 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005806 btrfs_handle_fs_error(fs_info, ret,
5807 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005808 goto error;
5809 }
Chris Masone02119d2008-09-05 16:13:11 -04005810
5811again:
5812 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5813 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005814 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005815
Chris Masond3977122009-01-05 21:25:51 -05005816 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005817 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005818
5819 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005820 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005821 "Couldn't find tree log root.");
5822 goto error;
5823 }
Chris Masone02119d2008-09-05 16:13:11 -04005824 if (ret > 0) {
5825 if (path->slots[0] == 0)
5826 break;
5827 path->slots[0]--;
5828 }
5829 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5830 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005831 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005832 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5833 break;
5834
Miao Xiecb517ea2013-05-15 07:48:19 +00005835 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005836 if (IS_ERR(log)) {
5837 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005838 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005839 "Couldn't read tree log root.");
5840 goto error;
5841 }
Chris Masone02119d2008-09-05 16:13:11 -04005842
5843 tmp_key.objectid = found_key.offset;
5844 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5845 tmp_key.offset = (u64)-1;
5846
5847 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005848 if (IS_ERR(wc.replay_dest)) {
5849 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005850 free_extent_buffer(log->node);
5851 free_extent_buffer(log->commit_root);
5852 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005853 btrfs_handle_fs_error(fs_info, ret,
5854 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005855 goto error;
5856 }
Chris Masone02119d2008-09-05 16:13:11 -04005857
Yan Zheng07d400a2009-01-06 11:42:00 -05005858 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005859 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005860 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005861
Josef Bacikb50c6e22013-04-25 15:55:30 -04005862 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005863 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5864 path);
Chris Masone02119d2008-09-05 16:13:11 -04005865 }
Chris Masone02119d2008-09-05 16:13:11 -04005866
Liu Bo900c9982018-01-25 11:02:56 -07005867 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5868 struct btrfs_root *root = wc.replay_dest;
5869
5870 btrfs_release_path(path);
5871
5872 /*
5873 * We have just replayed everything, and the highest
5874 * objectid of fs roots probably has changed in case
5875 * some inode_item's got replayed.
5876 *
5877 * root->objectid_mutex is not acquired as log replay
5878 * could only happen during mount.
5879 */
5880 ret = btrfs_find_highest_objectid(root,
5881 &root->highest_objectid);
5882 }
5883
Chris Masone02119d2008-09-05 16:13:11 -04005884 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005885 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005886 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005887 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005888 kfree(log);
5889
Josef Bacikb50c6e22013-04-25 15:55:30 -04005890 if (ret)
5891 goto error;
5892
Chris Masone02119d2008-09-05 16:13:11 -04005893 if (found_key.offset == 0)
5894 break;
5895 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005896 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005897
5898 /* step one is to pin it all, step two is to replay just inodes */
5899 if (wc.pin) {
5900 wc.pin = 0;
5901 wc.process_func = replay_one_buffer;
5902 wc.stage = LOG_WALK_REPLAY_INODES;
5903 goto again;
5904 }
5905 /* step three is to replay everything */
5906 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5907 wc.stage++;
5908 goto again;
5909 }
5910
5911 btrfs_free_path(path);
5912
Josef Bacikabefa552013-04-24 16:40:05 -04005913 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005914 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04005915 if (ret)
5916 return ret;
5917
Chris Masone02119d2008-09-05 16:13:11 -04005918 free_extent_buffer(log_root_tree->node);
5919 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005920 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005921 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005922
Josef Bacikabefa552013-04-24 16:40:05 -04005923 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005924error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005925 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005926 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005927 btrfs_free_path(path);
5928 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005929}
Chris Mason12fcfd22009-03-24 10:24:20 -04005930
5931/*
5932 * there are some corner cases where we want to force a full
5933 * commit instead of allowing a directory to be logged.
5934 *
5935 * They revolve around files there were unlinked from the directory, and
5936 * this function updates the parent directory so that a full commit is
5937 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005938 *
5939 * Must be called before the unlink operations (updates to the subvolume tree,
5940 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005941 */
5942void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005943 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005944 int for_rename)
5945{
5946 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005947 * when we're logging a file, if it hasn't been renamed
5948 * or unlinked, and its inode is fully committed on disk,
5949 * we don't have to worry about walking up the directory chain
5950 * to log its parents.
5951 *
5952 * So, we use the last_unlink_trans field to put this transid
5953 * into the file. When the file is logged we check it and
5954 * don't log the parents if the file is fully on disk.
5955 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005956 mutex_lock(&inode->log_mutex);
5957 inode->last_unlink_trans = trans->transid;
5958 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005959
5960 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005961 * if this directory was already logged any new
5962 * names for this file/dir will get recorded
5963 */
5964 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005965 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005966 return;
5967
5968 /*
5969 * if the inode we're about to unlink was logged,
5970 * the log will be properly updated for any new names
5971 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005972 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005973 return;
5974
5975 /*
5976 * when renaming files across directories, if the directory
5977 * there we're unlinking from gets fsync'd later on, there's
5978 * no way to find the destination directory later and fsync it
5979 * properly. So, we have to be conservative and force commits
5980 * so the new name gets discovered.
5981 */
5982 if (for_rename)
5983 goto record;
5984
5985 /* we can safely do the unlink without any special recording */
5986 return;
5987
5988record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005989 mutex_lock(&dir->log_mutex);
5990 dir->last_unlink_trans = trans->transid;
5991 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005992}
5993
5994/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005995 * Make sure that if someone attempts to fsync the parent directory of a deleted
5996 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5997 * that after replaying the log tree of the parent directory's root we will not
5998 * see the snapshot anymore and at log replay time we will not see any log tree
5999 * corresponding to the deleted snapshot's root, which could lead to replaying
6000 * it after replaying the log tree of the parent directory (which would replay
6001 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006002 *
6003 * Must be called before the actual snapshot destroy operation (updates to the
6004 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006005 */
6006void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006007 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006008{
Nikolay Borisov43663552017-01-18 00:31:29 +02006009 mutex_lock(&dir->log_mutex);
6010 dir->last_unlink_trans = trans->transid;
6011 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006012}
6013
6014/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006015 * Call this after adding a new name for a file and it will properly
6016 * update the log to reflect the new name.
6017 *
6018 * It will return zero if all goes well, and it will return 1 if a
6019 * full transaction commit is required.
6020 */
6021int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006022 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Chris Mason12fcfd22009-03-24 10:24:20 -04006023 struct dentry *parent)
6024{
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006025 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Mason12fcfd22009-03-24 10:24:20 -04006026
6027 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006028 * this will force the logging code to walk the dentry chain
6029 * up for the file
6030 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006031 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006032 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006033
6034 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006035 * if this inode hasn't been logged and directory we're renaming it
6036 * from hasn't been logged, we don't need to log it
6037 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006038 if (inode->logged_trans <= fs_info->last_trans_committed &&
6039 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Chris Mason12fcfd22009-03-24 10:24:20 -04006040 return 0;
6041
Nikolay Borisovf8822742018-02-27 17:37:17 +02006042 return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6043 LOG_INODE_EXISTS, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04006044}
6045