blob: ee1aaed1330e85e6dc5a6d95caa716894e8afe71 [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +000021#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -040022#include <linux/list_sort.h>
Miao Xie995946d2014-04-02 19:51:06 +080023#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040024#include "disk-io.h"
25#include "locking.h"
26#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070027#include "backref.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "hash.h"
Anand Jainebb87652016-03-10 17:26:59 +080029#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080030#include "qgroup.h"
Chris Masone02119d2008-09-05 16:13:11 -040031
32/* magic values for the inode_only field in btrfs_log_inode:
33 *
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
36 * during log replay
37 */
38#define LOG_INODE_ALL 0
39#define LOG_INODE_EXISTS 1
Liu Bo781feef2016-11-30 16:20:25 -080040#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040041
42/*
Chris Mason12fcfd22009-03-24 10:24:20 -040043 * directory trouble cases
44 *
45 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
46 * log, we must force a full commit before doing an fsync of the directory
47 * where the unlink was done.
48 * ---> record transid of last unlink/rename per directory
49 *
50 * mkdir foo/some_dir
51 * normal commit
52 * rename foo/some_dir foo2/some_dir
53 * mkdir foo/some_dir
54 * fsync foo/some_dir/some_file
55 *
56 * The fsync above will unlink the original some_dir without recording
57 * it in its new location (foo2). After a crash, some_dir will be gone
58 * unless the fsync of some_file forces a full commit
59 *
60 * 2) we must log any new names for any file or dir that is in the fsync
61 * log. ---> check inode while renaming/linking.
62 *
63 * 2a) we must log any new names for any file or dir during rename
64 * when the directory they are being removed from was logged.
65 * ---> check inode and old parent dir during rename
66 *
67 * 2a is actually the more important variant. With the extra logging
68 * a crash might unlink the old name without recreating the new one
69 *
70 * 3) after a crash, we must go through any directories with a link count
71 * of zero and redo the rm -rf
72 *
73 * mkdir f1/foo
74 * normal commit
75 * rm -rf f1/foo
76 * fsync(f1)
77 *
78 * The directory f1 was fully removed from the FS, but fsync was never
79 * called on f1, only its parent dir. After a crash the rm -rf must
80 * be replayed. This must be able to recurse down the entire
81 * directory tree. The inode link count fixup code takes care of the
82 * ugly details.
83 */
84
85/*
Chris Masone02119d2008-09-05 16:13:11 -040086 * stages for the tree walking. The first
87 * stage (0) is to only pin down the blocks we find
88 * the second stage (1) is to make sure that all the inodes
89 * we find in the log are created in the subvolume.
90 *
91 * The last stage is to deal with directories and links and extents
92 * and all the other fun semantics
93 */
94#define LOG_WALK_PIN_ONLY 0
95#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040096#define LOG_WALK_REPLAY_DIR_INDEX 2
97#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040098
Chris Mason12fcfd22009-03-24 10:24:20 -040099static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +0200100 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +0100101 int inode_only,
102 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100103 const loff_t end,
104 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500105static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
107 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400108static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
109 struct btrfs_root *root,
110 struct btrfs_root *log,
111 struct btrfs_path *path,
112 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400113
114/*
115 * tree logging is a special write ahead log used to make sure that
116 * fsyncs and O_SYNCs can happen without doing full tree commits.
117 *
118 * Full tree commits are expensive because they require commonly
119 * modified blocks to be recowed, creating many dirty pages in the
120 * extent tree an 4x-6x higher write load than ext3.
121 *
122 * Instead of doing a tree commit on every fsync, we use the
123 * key ranges and transaction ids to find items for a given file or directory
124 * that have changed in this transaction. Those items are copied into
125 * a special tree (one per subvolume root), that tree is written to disk
126 * and then the fsync is considered complete.
127 *
128 * After a crash, items are copied out of the log-tree back into the
129 * subvolume tree. Any file data extents found are recorded in the extent
130 * allocation tree, and the log-tree freed.
131 *
132 * The log tree is read three times, once to pin down all the extents it is
133 * using in ram and once, once to create all the inodes logged in the tree
134 * and once to do all the other items.
135 */
136
137/*
Chris Masone02119d2008-09-05 16:13:11 -0400138 * start a sub transaction and setup the log tree
139 * this increments the log tree writer count to make the people
140 * syncing the tree wait for us to finish
141 */
142static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800143 struct btrfs_root *root,
144 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400146 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800147 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500148
149 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800150
Yan Zheng7237f182009-01-21 12:54:03 -0500151 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400152 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800153 ret = -EAGAIN;
154 goto out;
155 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800156
Josef Bacikff782e02009-10-08 15:30:04 -0400157 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800158 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800159 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400160 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800161 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400162 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800163 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400164 mutex_lock(&fs_info->tree_log_mutex);
165 if (!fs_info->log_root_tree)
166 ret = btrfs_init_log_root_tree(trans, fs_info);
167 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800168 if (ret)
169 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400170
Chris Masone02119d2008-09-05 16:13:11 -0400171 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400172 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800173 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800174
175 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
176 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400177 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800178
Miao Xie2ecb7922012-09-06 04:04:27 -0600179 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500180 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800181 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800182 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800183 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800184 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800185 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800186
Miao Xiee87ac132014-02-20 18:08:53 +0800187out:
Yan Zheng7237f182009-01-21 12:54:03 -0500188 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800189 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400190}
191
192/*
193 * returns 0 if there was a log transaction running and we were able
194 * to join, or returns -ENOENT if there were not transactions
195 * in progress
196 */
197static int join_running_log_trans(struct btrfs_root *root)
198{
199 int ret = -ENOENT;
200
201 smp_mb();
202 if (!root->log_root)
203 return -ENOENT;
204
Yan Zheng7237f182009-01-21 12:54:03 -0500205 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400206 if (root->log_root) {
207 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500208 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400209 }
Yan Zheng7237f182009-01-21 12:54:03 -0500210 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400211 return ret;
212}
213
214/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400215 * This either makes the current running log transaction wait
216 * until you call btrfs_end_log_trans() or it makes any future
217 * log transactions wait until you call btrfs_end_log_trans()
218 */
219int btrfs_pin_log_trans(struct btrfs_root *root)
220{
221 int ret = -ENOENT;
222
223 mutex_lock(&root->log_mutex);
224 atomic_inc(&root->log_writers);
225 mutex_unlock(&root->log_mutex);
226 return ret;
227}
228
229/*
Chris Masone02119d2008-09-05 16:13:11 -0400230 * indicate we're done making changes to the log tree
231 * and wake up anyone waiting to do a sync
232 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100233void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400234{
Yan Zheng7237f182009-01-21 12:54:03 -0500235 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100236 /*
237 * Implicit memory barrier after atomic_dec_and_test
238 */
Yan Zheng7237f182009-01-21 12:54:03 -0500239 if (waitqueue_active(&root->log_writer_wait))
240 wake_up(&root->log_writer_wait);
241 }
Chris Masone02119d2008-09-05 16:13:11 -0400242}
243
244
245/*
246 * the walk control struct is used to pass state down the chain when
247 * processing the log tree. The stage field tells us which part
248 * of the log tree processing we are currently doing. The others
249 * are state fields used for that specific part
250 */
251struct walk_control {
252 /* should we free the extent on disk when done? This is used
253 * at transaction commit time while freeing a log tree
254 */
255 int free;
256
257 /* should we write out the extent buffer? This is used
258 * while flushing the log tree to disk during a sync
259 */
260 int write;
261
262 /* should we wait for the extent buffer io to finish? Also used
263 * while flushing the log tree to disk for a sync
264 */
265 int wait;
266
267 /* pin only walk, we record which extents on disk belong to the
268 * log trees
269 */
270 int pin;
271
272 /* what stage of the replay code we're currently in */
273 int stage;
274
275 /* the root we are currently replaying */
276 struct btrfs_root *replay_dest;
277
278 /* the trans handle for the current replay */
279 struct btrfs_trans_handle *trans;
280
281 /* the function that gets used to process blocks we find in the
282 * tree. Note the extent_buffer might not be up to date when it is
283 * passed in, and it must be checked or read if you need the data
284 * inside it
285 */
286 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
287 struct walk_control *wc, u64 gen);
288};
289
290/*
291 * process_func used to pin down extents, write them or wait on them
292 */
293static int process_one_buffer(struct btrfs_root *log,
294 struct extent_buffer *eb,
295 struct walk_control *wc, u64 gen)
296{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400298 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400299
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400300 /*
301 * If this fs is mixed then we need to be able to process the leaves to
302 * pin down any logged extents, so we have to read the block.
303 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400305 ret = btrfs_read_buffer(eb, gen);
306 if (ret)
307 return ret;
308 }
309
Josef Bacikb50c6e22013-04-25 15:55:30 -0400310 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400311 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
312 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400313
314 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400315 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400316 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400317 if (wc->write)
318 btrfs_write_tree_block(eb);
319 if (wc->wait)
320 btrfs_wait_tree_block_writeback(eb);
321 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400322 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400323}
324
325/*
326 * Item overwrite used by replay and tree logging. eb, slot and key all refer
327 * to the src data we are copying out.
328 *
329 * root is the tree we are copying into, and path is a scratch
330 * path for use in this function (it should be released on entry and
331 * will be released on exit).
332 *
333 * If the key is already in the destination tree the existing item is
334 * overwritten. If the existing item isn't big enough, it is extended.
335 * If it is too large, it is truncated.
336 *
337 * If the key isn't in the destination yet, a new item is inserted.
338 */
339static noinline int overwrite_item(struct btrfs_trans_handle *trans,
340 struct btrfs_root *root,
341 struct btrfs_path *path,
342 struct extent_buffer *eb, int slot,
343 struct btrfs_key *key)
344{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400345 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400346 int ret;
347 u32 item_size;
348 u64 saved_i_size = 0;
349 int save_old_i_size = 0;
350 unsigned long src_ptr;
351 unsigned long dst_ptr;
352 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000353 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400354
355 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
356 overwrite_root = 1;
357
358 item_size = btrfs_item_size_nr(eb, slot);
359 src_ptr = btrfs_item_ptr_offset(eb, slot);
360
361 /* look for the key in the destination tree */
362 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000363 if (ret < 0)
364 return ret;
365
Chris Masone02119d2008-09-05 16:13:11 -0400366 if (ret == 0) {
367 char *src_copy;
368 char *dst_copy;
369 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
370 path->slots[0]);
371 if (dst_size != item_size)
372 goto insert;
373
374 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200375 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400376 return 0;
377 }
378 dst_copy = kmalloc(item_size, GFP_NOFS);
379 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000380 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200381 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000382 kfree(dst_copy);
383 kfree(src_copy);
384 return -ENOMEM;
385 }
Chris Masone02119d2008-09-05 16:13:11 -0400386
387 read_extent_buffer(eb, src_copy, src_ptr, item_size);
388
389 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
390 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
391 item_size);
392 ret = memcmp(dst_copy, src_copy, item_size);
393
394 kfree(dst_copy);
395 kfree(src_copy);
396 /*
397 * they have the same contents, just return, this saves
398 * us from cowing blocks in the destination tree and doing
399 * extra writes that may not have been done by a previous
400 * sync
401 */
402 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200403 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400404 return 0;
405 }
406
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000407 /*
408 * We need to load the old nbytes into the inode so when we
409 * replay the extents we've logged we get the right nbytes.
410 */
411 if (inode_item) {
412 struct btrfs_inode_item *item;
413 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400414 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000415
416 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
417 struct btrfs_inode_item);
418 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
419 item = btrfs_item_ptr(eb, slot,
420 struct btrfs_inode_item);
421 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400422
423 /*
424 * If this is a directory we need to reset the i_size to
425 * 0 so that we can set it up properly when replaying
426 * the rest of the items in this log.
427 */
428 mode = btrfs_inode_mode(eb, item);
429 if (S_ISDIR(mode))
430 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000431 }
432 } else if (inode_item) {
433 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400434 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000435
436 /*
437 * New inode, set nbytes to 0 so that the nbytes comes out
438 * properly when we replay the extents.
439 */
440 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
441 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400442
443 /*
444 * If this is a directory we need to reset the i_size to 0 so
445 * that we can set it up properly when replaying the rest of
446 * the items in this log.
447 */
448 mode = btrfs_inode_mode(eb, item);
449 if (S_ISDIR(mode))
450 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400451 }
452insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200453 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400454 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000455 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400456 ret = btrfs_insert_empty_item(trans, root, path,
457 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000458 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400459
460 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000461 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400462 u32 found_size;
463 found_size = btrfs_item_size_nr(path->nodes[0],
464 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100465 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400466 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100467 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400468 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100469 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400470 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400471 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400472 }
473 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
474 path->slots[0]);
475
476 /* don't overwrite an existing inode if the generation number
477 * was logged as zero. This is done when the tree logging code
478 * is just logging an inode to make sure it exists after recovery.
479 *
480 * Also, don't overwrite i_size on directories during replay.
481 * log replay inserts and removes directory items based on the
482 * state of the tree found in the subvolume, and i_size is modified
483 * as it goes
484 */
485 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
486 struct btrfs_inode_item *src_item;
487 struct btrfs_inode_item *dst_item;
488
489 src_item = (struct btrfs_inode_item *)src_ptr;
490 dst_item = (struct btrfs_inode_item *)dst_ptr;
491
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000492 if (btrfs_inode_generation(eb, src_item) == 0) {
493 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000494 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000495
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000496 /*
497 * For regular files an ino_size == 0 is used only when
498 * logging that an inode exists, as part of a directory
499 * fsync, and the inode wasn't fsynced before. In this
500 * case don't set the size of the inode in the fs/subvol
501 * tree, otherwise we would be throwing valid data away.
502 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000503 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000504 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
505 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000506 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000507
508 btrfs_init_map_token(&token);
509 btrfs_set_token_inode_size(dst_eb, dst_item,
510 ino_size, &token);
511 }
Chris Masone02119d2008-09-05 16:13:11 -0400512 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000513 }
Chris Masone02119d2008-09-05 16:13:11 -0400514
515 if (overwrite_root &&
516 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
517 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
518 save_old_i_size = 1;
519 saved_i_size = btrfs_inode_size(path->nodes[0],
520 dst_item);
521 }
522 }
523
524 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
525 src_ptr, item_size);
526
527 if (save_old_i_size) {
528 struct btrfs_inode_item *dst_item;
529 dst_item = (struct btrfs_inode_item *)dst_ptr;
530 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
531 }
532
533 /* make sure the generation is filled in */
534 if (key->type == BTRFS_INODE_ITEM_KEY) {
535 struct btrfs_inode_item *dst_item;
536 dst_item = (struct btrfs_inode_item *)dst_ptr;
537 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
538 btrfs_set_inode_generation(path->nodes[0], dst_item,
539 trans->transid);
540 }
541 }
542no_copy:
543 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200544 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400545 return 0;
546}
547
548/*
549 * simple helper to read an inode off the disk from a given root
550 * This can only be called for subvolume roots and not for the log
551 */
552static noinline struct inode *read_one_inode(struct btrfs_root *root,
553 u64 objectid)
554{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400555 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400556 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400557
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400558 key.objectid = objectid;
559 key.type = BTRFS_INODE_ITEM_KEY;
560 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000561 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400562 if (IS_ERR(inode)) {
563 inode = NULL;
564 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400565 iput(inode);
566 inode = NULL;
567 }
568 return inode;
569}
570
571/* replays a single extent in 'eb' at 'slot' with 'key' into the
572 * subvolume 'root'. path is released on entry and should be released
573 * on exit.
574 *
575 * extents in the log tree have not been allocated out of the extent
576 * tree yet. So, this completes the allocation, taking a reference
577 * as required if the extent already exists or creating a new extent
578 * if it isn't in the extent allocation tree yet.
579 *
580 * The extent is inserted into the file, dropping any existing extents
581 * from the file that overlap the new one.
582 */
583static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
584 struct btrfs_root *root,
585 struct btrfs_path *path,
586 struct extent_buffer *eb, int slot,
587 struct btrfs_key *key)
588{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400589 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400590 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400591 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400592 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000593 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400594 struct btrfs_file_extent_item *item;
595 struct inode *inode = NULL;
596 unsigned long size;
597 int ret = 0;
598
599 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
600 found_type = btrfs_file_extent_type(eb, item);
601
Yan Zhengd899e052008-10-30 14:25:28 -0400602 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000603 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
604 nbytes = btrfs_file_extent_num_bytes(eb, item);
605 extent_end = start + nbytes;
606
607 /*
608 * We don't add to the inodes nbytes if we are prealloc or a
609 * hole.
610 */
611 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
612 nbytes = 0;
613 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800614 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000615 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400616 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400617 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400618 } else {
619 ret = 0;
620 goto out;
621 }
622
623 inode = read_one_inode(root, key->objectid);
624 if (!inode) {
625 ret = -EIO;
626 goto out;
627 }
628
629 /*
630 * first check to see if we already have this extent in the
631 * file. This must be done before the btrfs_drop_extents run
632 * so we don't try to drop this extent.
633 */
David Sterbaf85b7372017-01-20 14:54:07 +0100634 ret = btrfs_lookup_file_extent(trans, root, path,
635 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400636
Yan Zhengd899e052008-10-30 14:25:28 -0400637 if (ret == 0 &&
638 (found_type == BTRFS_FILE_EXTENT_REG ||
639 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400640 struct btrfs_file_extent_item cmp1;
641 struct btrfs_file_extent_item cmp2;
642 struct btrfs_file_extent_item *existing;
643 struct extent_buffer *leaf;
644
645 leaf = path->nodes[0];
646 existing = btrfs_item_ptr(leaf, path->slots[0],
647 struct btrfs_file_extent_item);
648
649 read_extent_buffer(eb, &cmp1, (unsigned long)item,
650 sizeof(cmp1));
651 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
652 sizeof(cmp2));
653
654 /*
655 * we already have a pointer to this exact extent,
656 * we don't have to do anything
657 */
658 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200659 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400660 goto out;
661 }
662 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200663 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400664
665 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400666 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400667 if (ret)
668 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400669
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 if (found_type == BTRFS_FILE_EXTENT_REG ||
671 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500673 unsigned long dest_offset;
674 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400675
Filipe Manana3168021c2017-02-01 14:58:02 +0000676 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
677 btrfs_fs_incompat(fs_info, NO_HOLES))
678 goto update_inode;
679
Yan Zheng07d400a2009-01-06 11:42:00 -0500680 ret = btrfs_insert_empty_item(trans, root, path, key,
681 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400682 if (ret)
683 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500684 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
685 path->slots[0]);
686 copy_extent_buffer(path->nodes[0], eb, dest_offset,
687 (unsigned long)item, sizeof(*item));
688
689 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
690 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
691 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400692 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500693
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800694 /*
695 * Manually record dirty extent, as here we did a shallow
696 * file extent item copy and skip normal backref update,
697 * but modifying extent tree all by ourselves.
698 * So need to manually record dirty extent for qgroup,
699 * as the owner of the file extent changed from log tree
700 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
701 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400702 ret = btrfs_qgroup_trace_extent(trans, fs_info,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800703 btrfs_file_extent_disk_bytenr(eb, item),
704 btrfs_file_extent_disk_num_bytes(eb, item),
705 GFP_NOFS);
706 if (ret < 0)
707 goto out;
708
Yan Zheng07d400a2009-01-06 11:42:00 -0500709 if (ins.objectid > 0) {
710 u64 csum_start;
711 u64 csum_end;
712 LIST_HEAD(ordered_sums);
713 /*
714 * is this extent already allocated in the extent
715 * allocation tree? If so, just add a reference
716 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400717 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500718 ins.offset);
719 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400720 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500721 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100723 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400724 if (ret)
725 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500726 } else {
727 /*
728 * insert the extent pointer in the extent
729 * allocation tree
730 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400731 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400732 fs_info,
733 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400734 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400735 if (ret)
736 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500737 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200738 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500739
740 if (btrfs_file_extent_compression(eb, item)) {
741 csum_start = ins.objectid;
742 csum_end = csum_start + ins.offset;
743 } else {
744 csum_start = ins.objectid +
745 btrfs_file_extent_offset(eb, item);
746 csum_end = csum_start +
747 btrfs_file_extent_num_bytes(eb, item);
748 }
749
750 ret = btrfs_lookup_csums_range(root->log_root,
751 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100752 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400753 if (ret)
754 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100755 /*
756 * Now delete all existing cums in the csum root that
757 * cover our range. We do this because we can have an
758 * extent that is completely referenced by one file
759 * extent item and partially referenced by another
760 * file extent item (like after using the clone or
761 * extent_same ioctls). In this case if we end up doing
762 * the replay of the one that partially references the
763 * extent first, and we do not do the csum deletion
764 * below, we can get 2 csum items in the csum tree that
765 * overlap each other. For example, imagine our log has
766 * the two following file extent items:
767 *
768 * key (257 EXTENT_DATA 409600)
769 * extent data disk byte 12845056 nr 102400
770 * extent data offset 20480 nr 20480 ram 102400
771 *
772 * key (257 EXTENT_DATA 819200)
773 * extent data disk byte 12845056 nr 102400
774 * extent data offset 0 nr 102400 ram 102400
775 *
776 * Where the second one fully references the 100K extent
777 * that starts at disk byte 12845056, and the log tree
778 * has a single csum item that covers the entire range
779 * of the extent:
780 *
781 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
782 *
783 * After the first file extent item is replayed, the
784 * csum tree gets the following csum item:
785 *
786 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
787 *
788 * Which covers the 20K sub-range starting at offset 20K
789 * of our extent. Now when we replay the second file
790 * extent item, if we do not delete existing csum items
791 * that cover any of its blocks, we end up getting two
792 * csum items in our csum tree that overlap each other:
793 *
794 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
795 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
796 *
797 * Which is a problem, because after this anyone trying
798 * to lookup up for the checksum of any block of our
799 * extent starting at an offset of 40K or higher, will
800 * end up looking at the second csum item only, which
801 * does not contain the checksum for any block starting
802 * at offset 40K or higher of our extent.
803 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500804 while (!list_empty(&ordered_sums)) {
805 struct btrfs_ordered_sum *sums;
806 sums = list_entry(ordered_sums.next,
807 struct btrfs_ordered_sum,
808 list);
Josef Bacik36508602013-04-25 16:23:32 -0400809 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400810 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400811 sums->bytenr,
812 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100813 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400814 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400815 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500816 list_del(&sums->list);
817 kfree(sums);
818 }
Josef Bacik36508602013-04-25 16:23:32 -0400819 if (ret)
820 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500821 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200822 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500823 }
824 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
825 /* inline extents are easy, we just overwrite them */
826 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400827 if (ret)
828 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500829 }
830
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000831 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000832update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600833 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400834out:
835 if (inode)
836 iput(inode);
837 return ret;
838}
839
840/*
841 * when cleaning up conflicts between the directory names in the
842 * subvolume, directory names in the log and directory names in the
843 * inode back references, we may have to unlink inodes from directories.
844 *
845 * This is a helper function to do the unlink of a specific directory
846 * item
847 */
848static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
849 struct btrfs_root *root,
850 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200851 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400852 struct btrfs_dir_item *di)
853{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400854 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400855 struct inode *inode;
856 char *name;
857 int name_len;
858 struct extent_buffer *leaf;
859 struct btrfs_key location;
860 int ret;
861
862 leaf = path->nodes[0];
863
864 btrfs_dir_item_key_to_cpu(leaf, di, &location);
865 name_len = btrfs_dir_name_len(leaf, di);
866 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000867 if (!name)
868 return -ENOMEM;
869
Chris Masone02119d2008-09-05 16:13:11 -0400870 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200871 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400872
873 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000874 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400875 ret = -EIO;
876 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000877 }
Chris Masone02119d2008-09-05 16:13:11 -0400878
Yan Zhengec051c02009-01-05 15:43:42 -0500879 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400880 if (ret)
881 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400882
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200883 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
884 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400885 if (ret)
886 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100887 else
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400888 ret = btrfs_run_delayed_items(trans, fs_info);
Josef Bacik36508602013-04-25 16:23:32 -0400889out:
890 kfree(name);
891 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400892 return ret;
893}
894
895/*
896 * helper function to see if a given name and sequence number found
897 * in an inode back reference are already in a directory and correctly
898 * point to this inode
899 */
900static noinline int inode_in_dir(struct btrfs_root *root,
901 struct btrfs_path *path,
902 u64 dirid, u64 objectid, u64 index,
903 const char *name, int name_len)
904{
905 struct btrfs_dir_item *di;
906 struct btrfs_key location;
907 int match = 0;
908
909 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
910 index, name, name_len, 0);
911 if (di && !IS_ERR(di)) {
912 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
913 if (location.objectid != objectid)
914 goto out;
915 } else
916 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200917 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400918
919 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
920 if (di && !IS_ERR(di)) {
921 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
922 if (location.objectid != objectid)
923 goto out;
924 } else
925 goto out;
926 match = 1;
927out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200928 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400929 return match;
930}
931
932/*
933 * helper function to check a log tree for a named back reference in
934 * an inode. This is used to decide if a back reference that is
935 * found in the subvolume conflicts with what we find in the log.
936 *
937 * inode backreferences may have multiple refs in a single item,
938 * during replay we process one reference at a time, and we don't
939 * want to delete valid links to a file from the subvolume if that
940 * link is also in the log.
941 */
942static noinline int backref_in_log(struct btrfs_root *log,
943 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700944 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000945 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400946{
947 struct btrfs_path *path;
948 struct btrfs_inode_ref *ref;
949 unsigned long ptr;
950 unsigned long ptr_end;
951 unsigned long name_ptr;
952 int found_name_len;
953 int item_size;
954 int ret;
955 int match = 0;
956
957 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000958 if (!path)
959 return -ENOMEM;
960
Chris Masone02119d2008-09-05 16:13:11 -0400961 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
962 if (ret != 0)
963 goto out;
964
Chris Masone02119d2008-09-05 16:13:11 -0400965 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700966
967 if (key->type == BTRFS_INODE_EXTREF_KEY) {
968 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
969 name, namelen, NULL))
970 match = 1;
971
972 goto out;
973 }
974
975 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400976 ptr_end = ptr + item_size;
977 while (ptr < ptr_end) {
978 ref = (struct btrfs_inode_ref *)ptr;
979 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
980 if (found_name_len == namelen) {
981 name_ptr = (unsigned long)(ref + 1);
982 ret = memcmp_extent_buffer(path->nodes[0], name,
983 name_ptr, namelen);
984 if (ret == 0) {
985 match = 1;
986 goto out;
987 }
988 }
989 ptr = (unsigned long)(ref + 1) + found_name_len;
990 }
991out:
992 btrfs_free_path(path);
993 return match;
994}
995
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700996static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
997 struct btrfs_root *root,
998 struct btrfs_path *path,
999 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001000 struct btrfs_inode *dir,
1001 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -07001002 u64 inode_objectid, u64 parent_objectid,
1003 u64 ref_index, char *name, int namelen,
1004 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001005{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001006 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001007 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001008 char *victim_name;
1009 int victim_name_len;
1010 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001011 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001012 struct btrfs_key search_key;
1013 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001014
Mark Fashehf1863732012-08-08 11:32:27 -07001015again:
1016 /* Search old style refs */
1017 search_key.objectid = inode_objectid;
1018 search_key.type = BTRFS_INODE_REF_KEY;
1019 search_key.offset = parent_objectid;
1020 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001021 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001022 struct btrfs_inode_ref *victim_ref;
1023 unsigned long ptr;
1024 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001025
1026 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001027
1028 /* are we trying to overwrite a back ref for the root directory
1029 * if so, just jump out, we're done
1030 */
Mark Fashehf1863732012-08-08 11:32:27 -07001031 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001032 return 1;
1033
1034 /* check all the names in this back reference to see
1035 * if they are in the log. if so, we allow them to stay
1036 * otherwise they must be unlinked as a conflict
1037 */
1038 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1039 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1040 while (ptr < ptr_end) {
1041 victim_ref = (struct btrfs_inode_ref *)ptr;
1042 victim_name_len = btrfs_inode_ref_name_len(leaf,
1043 victim_ref);
1044 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001045 if (!victim_name)
1046 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001047
1048 read_extent_buffer(leaf, victim_name,
1049 (unsigned long)(victim_ref + 1),
1050 victim_name_len);
1051
Mark Fashehf1863732012-08-08 11:32:27 -07001052 if (!backref_in_log(log_root, &search_key,
1053 parent_objectid,
1054 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001056 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001057 btrfs_release_path(path);
1058
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001059 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001060 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001061 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001062 if (ret)
1063 return ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001064 ret = btrfs_run_delayed_items(trans, fs_info);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001065 if (ret)
1066 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001067 *search_done = 1;
1068 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001069 }
1070 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001071
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001072 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1073 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001074
1075 /*
1076 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001077 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001078 */
1079 *search_done = 1;
1080 }
1081 btrfs_release_path(path);
1082
Mark Fashehf1863732012-08-08 11:32:27 -07001083 /* Same search but for extended refs */
1084 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1085 inode_objectid, parent_objectid, 0,
1086 0);
1087 if (!IS_ERR_OR_NULL(extref)) {
1088 u32 item_size;
1089 u32 cur_offset = 0;
1090 unsigned long base;
1091 struct inode *victim_parent;
1092
1093 leaf = path->nodes[0];
1094
1095 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1096 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1097
1098 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001099 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001100
1101 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1102
1103 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1104 goto next;
1105
1106 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001107 if (!victim_name)
1108 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001109 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1110 victim_name_len);
1111
1112 search_key.objectid = inode_objectid;
1113 search_key.type = BTRFS_INODE_EXTREF_KEY;
1114 search_key.offset = btrfs_extref_hash(parent_objectid,
1115 victim_name,
1116 victim_name_len);
1117 ret = 0;
1118 if (!backref_in_log(log_root, &search_key,
1119 parent_objectid, victim_name,
1120 victim_name_len)) {
1121 ret = -ENOENT;
1122 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001123 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001124 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001125 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001126 btrfs_release_path(path);
1127
1128 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001129 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001130 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001131 victim_name,
1132 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001133 if (!ret)
1134 ret = btrfs_run_delayed_items(
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001135 trans,
1136 fs_info);
Mark Fashehf1863732012-08-08 11:32:27 -07001137 }
Mark Fashehf1863732012-08-08 11:32:27 -07001138 iput(victim_parent);
1139 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001140 if (ret)
1141 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001142 *search_done = 1;
1143 goto again;
1144 }
1145 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001146next:
1147 cur_offset += victim_name_len + sizeof(*extref);
1148 }
1149 *search_done = 1;
1150 }
1151 btrfs_release_path(path);
1152
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001153 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001154 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001155 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001156 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001157 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001158 if (ret)
1159 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001160 }
1161 btrfs_release_path(path);
1162
1163 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001164 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001165 name, namelen, 0);
1166 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001167 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001168 if (ret)
1169 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001170 }
1171 btrfs_release_path(path);
1172
1173 return 0;
1174}
Chris Masone02119d2008-09-05 16:13:11 -04001175
Qu Wenruobae15d92017-11-08 08:54:26 +08001176static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1177 u32 *namelen, char **name, u64 *index,
1178 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001179{
1180 struct btrfs_inode_extref *extref;
1181
1182 extref = (struct btrfs_inode_extref *)ref_ptr;
1183
1184 *namelen = btrfs_inode_extref_name_len(eb, extref);
1185 *name = kmalloc(*namelen, GFP_NOFS);
1186 if (*name == NULL)
1187 return -ENOMEM;
1188
1189 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1190 *namelen);
1191
1192 *index = btrfs_inode_extref_index(eb, extref);
1193 if (parent_objectid)
1194 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1195
1196 return 0;
1197}
1198
Qu Wenruobae15d92017-11-08 08:54:26 +08001199static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1200 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001201{
1202 struct btrfs_inode_ref *ref;
1203
1204 ref = (struct btrfs_inode_ref *)ref_ptr;
1205
1206 *namelen = btrfs_inode_ref_name_len(eb, ref);
1207 *name = kmalloc(*namelen, GFP_NOFS);
1208 if (*name == NULL)
1209 return -ENOMEM;
1210
1211 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1212
1213 *index = btrfs_inode_ref_index(eb, ref);
1214
1215 return 0;
1216}
1217
Chris Masone02119d2008-09-05 16:13:11 -04001218/*
1219 * replay one inode back reference item found in the log tree.
1220 * eb, slot and key refer to the buffer and key found in the log tree.
1221 * root is the destination we are replaying into, and path is for temp
1222 * use by this function. (it should be released on return).
1223 */
1224static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1225 struct btrfs_root *root,
1226 struct btrfs_root *log,
1227 struct btrfs_path *path,
1228 struct extent_buffer *eb, int slot,
1229 struct btrfs_key *key)
1230{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001231 struct inode *dir = NULL;
1232 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001233 unsigned long ref_ptr;
1234 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001235 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001236 int namelen;
1237 int ret;
liuboc622ae62011-03-26 08:01:12 -04001238 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001239 int log_ref_ver = 0;
1240 u64 parent_objectid;
1241 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001242 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001243 int ref_struct_size;
1244
1245 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1246 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1247
1248 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1249 struct btrfs_inode_extref *r;
1250
1251 ref_struct_size = sizeof(struct btrfs_inode_extref);
1252 log_ref_ver = 1;
1253 r = (struct btrfs_inode_extref *)ref_ptr;
1254 parent_objectid = btrfs_inode_extref_parent(eb, r);
1255 } else {
1256 ref_struct_size = sizeof(struct btrfs_inode_ref);
1257 parent_objectid = key->offset;
1258 }
1259 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001260
Chris Masone02119d2008-09-05 16:13:11 -04001261 /*
1262 * it is possible that we didn't log all the parent directories
1263 * for a given inode. If we don't find the dir, just don't
1264 * copy the back ref in. The link count fixup code will take
1265 * care of the rest
1266 */
Mark Fashehf1863732012-08-08 11:32:27 -07001267 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001268 if (!dir) {
1269 ret = -ENOENT;
1270 goto out;
1271 }
Chris Masone02119d2008-09-05 16:13:11 -04001272
Mark Fashehf1863732012-08-08 11:32:27 -07001273 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001274 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001275 ret = -EIO;
1276 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001277 }
Chris Masone02119d2008-09-05 16:13:11 -04001278
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001279 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001280 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001281 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1282 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001283 /*
1284 * parent object can change from one array
1285 * item to another.
1286 */
1287 if (!dir)
1288 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001289 if (!dir) {
1290 ret = -ENOENT;
1291 goto out;
1292 }
Mark Fashehf1863732012-08-08 11:32:27 -07001293 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001294 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1295 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001296 }
1297 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001298 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001299
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001300 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001301 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1302 btrfs_ino(BTRFS_I(inode)), ref_index,
1303 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001304 /*
1305 * look for a conflicting back reference in the
1306 * metadata. if we find one we have to unlink that name
1307 * of the file before we add our new link. Later on, we
1308 * overwrite any existing back reference, and we don't
1309 * want to create dangling pointers in the directory.
1310 */
Chris Masone02119d2008-09-05 16:13:11 -04001311
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001312 if (!search_done) {
1313 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001314 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001315 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001316 inode_objectid,
1317 parent_objectid,
1318 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001319 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001320 if (ret) {
1321 if (ret == 1)
1322 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001323 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001324 }
Chris Masone02119d2008-09-05 16:13:11 -04001325 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001326
1327 /* insert our name */
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001328 ret = btrfs_add_link(trans, BTRFS_I(dir),
1329 BTRFS_I(inode),
1330 name, namelen, 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001331 if (ret)
1332 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001333
1334 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001335 }
liuboc622ae62011-03-26 08:01:12 -04001336
Mark Fashehf1863732012-08-08 11:32:27 -07001337 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001338 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001339 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001340 if (log_ref_ver) {
1341 iput(dir);
1342 dir = NULL;
1343 }
Chris Masone02119d2008-09-05 16:13:11 -04001344 }
Chris Masone02119d2008-09-05 16:13:11 -04001345
1346 /* finally write the back reference in the inode */
1347 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001348out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001349 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001350 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001351 iput(dir);
1352 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001353 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001354}
1355
Yan, Zhengc71bf092009-11-12 09:34:40 +00001356static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001357 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001358{
1359 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001360
David Sterba9c4f61f2015-01-02 19:12:57 +01001361 ret = btrfs_insert_orphan_item(trans, root, ino);
1362 if (ret == -EEXIST)
1363 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001364
Yan, Zhengc71bf092009-11-12 09:34:40 +00001365 return ret;
1366}
1367
Mark Fashehf1863732012-08-08 11:32:27 -07001368static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001369 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001370{
Mark Fashehf1863732012-08-08 11:32:27 -07001371 int ret = 0;
1372 int name_len;
1373 unsigned int nlink = 0;
1374 u32 item_size;
1375 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001376 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001377 u64 offset = 0;
1378 unsigned long ptr;
1379 struct btrfs_inode_extref *extref;
1380 struct extent_buffer *leaf;
1381
1382 while (1) {
1383 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1384 &extref, &offset);
1385 if (ret)
1386 break;
1387
1388 leaf = path->nodes[0];
1389 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1390 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001391 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001392
1393 while (cur_offset < item_size) {
1394 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1395 name_len = btrfs_inode_extref_name_len(leaf, extref);
1396
1397 nlink++;
1398
1399 cur_offset += name_len + sizeof(*extref);
1400 }
1401
1402 offset++;
1403 btrfs_release_path(path);
1404 }
1405 btrfs_release_path(path);
1406
Filipe Manana2c2c4522015-01-13 16:40:04 +00001407 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001408 return ret;
1409 return nlink;
1410}
1411
1412static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001413 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001414{
Chris Masone02119d2008-09-05 16:13:11 -04001415 int ret;
1416 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001417 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001418 unsigned long ptr;
1419 unsigned long ptr_end;
1420 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001421 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001422
Li Zefan33345d012011-04-20 10:31:50 +08001423 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001424 key.type = BTRFS_INODE_REF_KEY;
1425 key.offset = (u64)-1;
1426
Chris Masond3977122009-01-05 21:25:51 -05001427 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001428 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1429 if (ret < 0)
1430 break;
1431 if (ret > 0) {
1432 if (path->slots[0] == 0)
1433 break;
1434 path->slots[0]--;
1435 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001436process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001437 btrfs_item_key_to_cpu(path->nodes[0], &key,
1438 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001439 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001440 key.type != BTRFS_INODE_REF_KEY)
1441 break;
1442 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1443 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1444 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001445 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001446 struct btrfs_inode_ref *ref;
1447
1448 ref = (struct btrfs_inode_ref *)ptr;
1449 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1450 ref);
1451 ptr = (unsigned long)(ref + 1) + name_len;
1452 nlink++;
1453 }
1454
1455 if (key.offset == 0)
1456 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001457 if (path->slots[0] > 0) {
1458 path->slots[0]--;
1459 goto process_slot;
1460 }
Chris Masone02119d2008-09-05 16:13:11 -04001461 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001462 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001463 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001464 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001465
1466 return nlink;
1467}
1468
1469/*
1470 * There are a few corners where the link count of the file can't
1471 * be properly maintained during replay. So, instead of adding
1472 * lots of complexity to the log code, we just scan the backrefs
1473 * for any file that has been through replay.
1474 *
1475 * The scan will update the link count on the inode to reflect the
1476 * number of back refs found. If it goes down to zero, the iput
1477 * will free the inode.
1478 */
1479static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1480 struct btrfs_root *root,
1481 struct inode *inode)
1482{
1483 struct btrfs_path *path;
1484 int ret;
1485 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001486 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001487
1488 path = btrfs_alloc_path();
1489 if (!path)
1490 return -ENOMEM;
1491
Nikolay Borisovf329e312017-01-18 00:31:50 +02001492 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001493 if (ret < 0)
1494 goto out;
1495
1496 nlink = ret;
1497
Nikolay Borisov36283652017-01-18 00:31:49 +02001498 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001499 if (ret < 0)
1500 goto out;
1501
1502 nlink += ret;
1503
1504 ret = 0;
1505
Chris Masone02119d2008-09-05 16:13:11 -04001506 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001507 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001508 btrfs_update_inode(trans, root, inode);
1509 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001510 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001511
Yan, Zhengc71bf092009-11-12 09:34:40 +00001512 if (inode->i_nlink == 0) {
1513 if (S_ISDIR(inode->i_mode)) {
1514 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001515 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001516 if (ret)
1517 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001518 }
Li Zefan33345d012011-04-20 10:31:50 +08001519 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001520 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001521
Mark Fashehf1863732012-08-08 11:32:27 -07001522out:
1523 btrfs_free_path(path);
1524 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001525}
1526
1527static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1528 struct btrfs_root *root,
1529 struct btrfs_path *path)
1530{
1531 int ret;
1532 struct btrfs_key key;
1533 struct inode *inode;
1534
1535 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1536 key.type = BTRFS_ORPHAN_ITEM_KEY;
1537 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001538 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001539 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1540 if (ret < 0)
1541 break;
1542
1543 if (ret == 1) {
1544 if (path->slots[0] == 0)
1545 break;
1546 path->slots[0]--;
1547 }
1548
1549 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1550 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1551 key.type != BTRFS_ORPHAN_ITEM_KEY)
1552 break;
1553
1554 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001555 if (ret)
1556 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001557
David Sterbab3b4aa72011-04-21 01:20:15 +02001558 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001559 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001560 if (!inode)
1561 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001562
1563 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001564 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001565 if (ret)
1566 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001567
Chris Mason12fcfd22009-03-24 10:24:20 -04001568 /*
1569 * fixup on a directory may create new entries,
1570 * make sure we always look for the highset possible
1571 * offset
1572 */
1573 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001574 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001575 ret = 0;
1576out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001577 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001578 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001579}
1580
1581
1582/*
1583 * record a given inode in the fixup dir so we can check its link
1584 * count when replay is done. The link count is incremented here
1585 * so the inode won't go away until we check it
1586 */
1587static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1588 struct btrfs_root *root,
1589 struct btrfs_path *path,
1590 u64 objectid)
1591{
1592 struct btrfs_key key;
1593 int ret = 0;
1594 struct inode *inode;
1595
1596 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001597 if (!inode)
1598 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001599
1600 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001601 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001602 key.offset = objectid;
1603
1604 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1605
David Sterbab3b4aa72011-04-21 01:20:15 +02001606 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001607 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001608 if (!inode->i_nlink)
1609 set_nlink(inode, 1);
1610 else
Zach Brown8b558c52013-10-16 12:10:34 -07001611 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001612 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001613 } else if (ret == -EEXIST) {
1614 ret = 0;
1615 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001616 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001617 }
1618 iput(inode);
1619
1620 return ret;
1621}
1622
1623/*
1624 * when replaying the log for a directory, we only insert names
1625 * for inodes that actually exist. This means an fsync on a directory
1626 * does not implicitly fsync all the new files in it
1627 */
1628static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1629 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001630 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001631 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001632 struct btrfs_key *location)
1633{
1634 struct inode *inode;
1635 struct inode *dir;
1636 int ret;
1637
1638 inode = read_one_inode(root, location->objectid);
1639 if (!inode)
1640 return -ENOENT;
1641
1642 dir = read_one_inode(root, dirid);
1643 if (!dir) {
1644 iput(inode);
1645 return -EIO;
1646 }
Josef Bacikd5554382013-09-11 14:17:00 -04001647
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001648 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1649 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001650
1651 /* FIXME, put inode into FIXUP list */
1652
1653 iput(inode);
1654 iput(dir);
1655 return ret;
1656}
1657
1658/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001659 * Return true if an inode reference exists in the log for the given name,
1660 * inode and parent inode.
1661 */
1662static bool name_in_log_ref(struct btrfs_root *log_root,
1663 const char *name, const int name_len,
1664 const u64 dirid, const u64 ino)
1665{
1666 struct btrfs_key search_key;
1667
1668 search_key.objectid = ino;
1669 search_key.type = BTRFS_INODE_REF_KEY;
1670 search_key.offset = dirid;
1671 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1672 return true;
1673
1674 search_key.type = BTRFS_INODE_EXTREF_KEY;
1675 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1676 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1677 return true;
1678
1679 return false;
1680}
1681
1682/*
Chris Masone02119d2008-09-05 16:13:11 -04001683 * take a single entry in a log directory item and replay it into
1684 * the subvolume.
1685 *
1686 * if a conflicting item exists in the subdirectory already,
1687 * the inode it points to is unlinked and put into the link count
1688 * fix up tree.
1689 *
1690 * If a name from the log points to a file or directory that does
1691 * not exist in the FS, it is skipped. fsyncs on directories
1692 * do not force down inodes inside that directory, just changes to the
1693 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001694 *
1695 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1696 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001697 */
1698static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1699 struct btrfs_root *root,
1700 struct btrfs_path *path,
1701 struct extent_buffer *eb,
1702 struct btrfs_dir_item *di,
1703 struct btrfs_key *key)
1704{
1705 char *name;
1706 int name_len;
1707 struct btrfs_dir_item *dst_di;
1708 struct btrfs_key found_key;
1709 struct btrfs_key log_key;
1710 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001711 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001712 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001713 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001714 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001715 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001716
1717 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001718 if (!dir)
1719 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001720
1721 name_len = btrfs_dir_name_len(eb, di);
1722 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001723 if (!name) {
1724 ret = -ENOMEM;
1725 goto out;
1726 }
liubo2a29edc2011-01-26 06:22:08 +00001727
Chris Masone02119d2008-09-05 16:13:11 -04001728 log_type = btrfs_dir_type(eb, di);
1729 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1730 name_len);
1731
1732 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001733 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1734 if (exists == 0)
1735 exists = 1;
1736 else
1737 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001738 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001739
Chris Masone02119d2008-09-05 16:13:11 -04001740 if (key->type == BTRFS_DIR_ITEM_KEY) {
1741 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1742 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001743 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001744 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1745 key->objectid,
1746 key->offset, name,
1747 name_len, 1);
1748 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001749 /* Corruption */
1750 ret = -EINVAL;
1751 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001752 }
David Sterbac7040052011-04-19 18:00:01 +02001753 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001754 /* we need a sequence number to insert, so we only
1755 * do inserts for the BTRFS_DIR_INDEX_KEY types
1756 */
1757 if (key->type != BTRFS_DIR_INDEX_KEY)
1758 goto out;
1759 goto insert;
1760 }
1761
1762 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1763 /* the existing item matches the logged item */
1764 if (found_key.objectid == log_key.objectid &&
1765 found_key.type == log_key.type &&
1766 found_key.offset == log_key.offset &&
1767 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001768 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001769 goto out;
1770 }
1771
1772 /*
1773 * don't drop the conflicting directory entry if the inode
1774 * for the new entry doesn't exist
1775 */
Chris Mason4bef0842008-09-08 11:18:08 -04001776 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001777 goto out;
1778
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001779 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001780 if (ret)
1781 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001782
1783 if (key->type == BTRFS_DIR_INDEX_KEY)
1784 goto insert;
1785out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001786 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001787 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001788 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001789 ret = btrfs_update_inode(trans, root, dir);
1790 }
Chris Masone02119d2008-09-05 16:13:11 -04001791 kfree(name);
1792 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001793 if (!ret && name_added)
1794 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001795 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001796
1797insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001798 if (name_in_log_ref(root->log_root, name, name_len,
1799 key->objectid, log_key.objectid)) {
1800 /* The dentry will be added later. */
1801 ret = 0;
1802 update_size = false;
1803 goto out;
1804 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001805 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001806 ret = insert_one_name(trans, root, key->objectid, key->offset,
1807 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001808 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001809 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001810 if (!ret)
1811 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001812 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001813 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001814 goto out;
1815}
1816
1817/*
1818 * find all the names in a directory item and reconcile them into
1819 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1820 * one name in a directory item, but the same code gets used for
1821 * both directory index types
1822 */
1823static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1824 struct btrfs_root *root,
1825 struct btrfs_path *path,
1826 struct extent_buffer *eb, int slot,
1827 struct btrfs_key *key)
1828{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001829 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001830 u32 item_size = btrfs_item_size_nr(eb, slot);
1831 struct btrfs_dir_item *di;
1832 int name_len;
1833 unsigned long ptr;
1834 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001835 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001836
1837 ptr = btrfs_item_ptr_offset(eb, slot);
1838 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001839 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001840 di = (struct btrfs_dir_item *)ptr;
1841 name_len = btrfs_dir_name_len(eb, di);
1842 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001843 if (ret < 0)
1844 break;
Chris Masone02119d2008-09-05 16:13:11 -04001845 ptr = (unsigned long)(di + 1);
1846 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001847
1848 /*
1849 * If this entry refers to a non-directory (directories can not
1850 * have a link count > 1) and it was added in the transaction
1851 * that was not committed, make sure we fixup the link count of
1852 * the inode it the entry points to. Otherwise something like
1853 * the following would result in a directory pointing to an
1854 * inode with a wrong link that does not account for this dir
1855 * entry:
1856 *
1857 * mkdir testdir
1858 * touch testdir/foo
1859 * touch testdir/bar
1860 * sync
1861 *
1862 * ln testdir/bar testdir/bar_link
1863 * ln testdir/foo testdir/foo_link
1864 * xfs_io -c "fsync" testdir/bar
1865 *
1866 * <power failure>
1867 *
1868 * mount fs, log replay happens
1869 *
1870 * File foo would remain with a link count of 1 when it has two
1871 * entries pointing to it in the directory testdir. This would
1872 * make it impossible to ever delete the parent directory has
1873 * it would result in stale dentries that can never be deleted.
1874 */
1875 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1876 struct btrfs_key di_key;
1877
1878 if (!fixup_path) {
1879 fixup_path = btrfs_alloc_path();
1880 if (!fixup_path) {
1881 ret = -ENOMEM;
1882 break;
1883 }
1884 }
1885
1886 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1887 ret = link_to_fixup_dir(trans, root, fixup_path,
1888 di_key.objectid);
1889 if (ret)
1890 break;
1891 }
1892 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001893 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001894 btrfs_free_path(fixup_path);
1895 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001896}
1897
1898/*
1899 * directory replay has two parts. There are the standard directory
1900 * items in the log copied from the subvolume, and range items
1901 * created in the log while the subvolume was logged.
1902 *
1903 * The range items tell us which parts of the key space the log
1904 * is authoritative for. During replay, if a key in the subvolume
1905 * directory is in a logged range item, but not actually in the log
1906 * that means it was deleted from the directory before the fsync
1907 * and should be removed.
1908 */
1909static noinline int find_dir_range(struct btrfs_root *root,
1910 struct btrfs_path *path,
1911 u64 dirid, int key_type,
1912 u64 *start_ret, u64 *end_ret)
1913{
1914 struct btrfs_key key;
1915 u64 found_end;
1916 struct btrfs_dir_log_item *item;
1917 int ret;
1918 int nritems;
1919
1920 if (*start_ret == (u64)-1)
1921 return 1;
1922
1923 key.objectid = dirid;
1924 key.type = key_type;
1925 key.offset = *start_ret;
1926
1927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1928 if (ret < 0)
1929 goto out;
1930 if (ret > 0) {
1931 if (path->slots[0] == 0)
1932 goto out;
1933 path->slots[0]--;
1934 }
1935 if (ret != 0)
1936 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1937
1938 if (key.type != key_type || key.objectid != dirid) {
1939 ret = 1;
1940 goto next;
1941 }
1942 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1943 struct btrfs_dir_log_item);
1944 found_end = btrfs_dir_log_end(path->nodes[0], item);
1945
1946 if (*start_ret >= key.offset && *start_ret <= found_end) {
1947 ret = 0;
1948 *start_ret = key.offset;
1949 *end_ret = found_end;
1950 goto out;
1951 }
1952 ret = 1;
1953next:
1954 /* check the next slot in the tree to see if it is a valid item */
1955 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08001956 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04001957 if (path->slots[0] >= nritems) {
1958 ret = btrfs_next_leaf(root, path);
1959 if (ret)
1960 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001961 }
1962
1963 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1964
1965 if (key.type != key_type || key.objectid != dirid) {
1966 ret = 1;
1967 goto out;
1968 }
1969 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1970 struct btrfs_dir_log_item);
1971 found_end = btrfs_dir_log_end(path->nodes[0], item);
1972 *start_ret = key.offset;
1973 *end_ret = found_end;
1974 ret = 0;
1975out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001976 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001977 return ret;
1978}
1979
1980/*
1981 * this looks for a given directory item in the log. If the directory
1982 * item is not in the log, the item is removed and the inode it points
1983 * to is unlinked
1984 */
1985static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1986 struct btrfs_root *root,
1987 struct btrfs_root *log,
1988 struct btrfs_path *path,
1989 struct btrfs_path *log_path,
1990 struct inode *dir,
1991 struct btrfs_key *dir_key)
1992{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001993 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04001994 int ret;
1995 struct extent_buffer *eb;
1996 int slot;
1997 u32 item_size;
1998 struct btrfs_dir_item *di;
1999 struct btrfs_dir_item *log_di;
2000 int name_len;
2001 unsigned long ptr;
2002 unsigned long ptr_end;
2003 char *name;
2004 struct inode *inode;
2005 struct btrfs_key location;
2006
2007again:
2008 eb = path->nodes[0];
2009 slot = path->slots[0];
2010 item_size = btrfs_item_size_nr(eb, slot);
2011 ptr = btrfs_item_ptr_offset(eb, slot);
2012 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002013 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002014 di = (struct btrfs_dir_item *)ptr;
2015 name_len = btrfs_dir_name_len(eb, di);
2016 name = kmalloc(name_len, GFP_NOFS);
2017 if (!name) {
2018 ret = -ENOMEM;
2019 goto out;
2020 }
2021 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2022 name_len);
2023 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002024 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002025 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2026 dir_key->objectid,
2027 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002028 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002029 log_di = btrfs_lookup_dir_index_item(trans, log,
2030 log_path,
2031 dir_key->objectid,
2032 dir_key->offset,
2033 name, name_len, 0);
2034 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002035 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002036 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002037 btrfs_release_path(path);
2038 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002039 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002040 if (!inode) {
2041 kfree(name);
2042 return -EIO;
2043 }
Chris Masone02119d2008-09-05 16:13:11 -04002044
2045 ret = link_to_fixup_dir(trans, root,
2046 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002047 if (ret) {
2048 kfree(name);
2049 iput(inode);
2050 goto out;
2051 }
2052
Zach Brown8b558c52013-10-16 12:10:34 -07002053 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002054 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2055 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002056 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002057 ret = btrfs_run_delayed_items(trans, fs_info);
Chris Masone02119d2008-09-05 16:13:11 -04002058 kfree(name);
2059 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002060 if (ret)
2061 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002062
2063 /* there might still be more names under this key
2064 * check and repeat if required
2065 */
2066 ret = btrfs_search_slot(NULL, root, dir_key, path,
2067 0, 0);
2068 if (ret == 0)
2069 goto again;
2070 ret = 0;
2071 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002072 } else if (IS_ERR(log_di)) {
2073 kfree(name);
2074 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002075 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002076 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002077 kfree(name);
2078
2079 ptr = (unsigned long)(di + 1);
2080 ptr += name_len;
2081 }
2082 ret = 0;
2083out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002084 btrfs_release_path(path);
2085 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002086 return ret;
2087}
2088
Filipe Manana4f764e52015-02-23 19:53:35 +00002089static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2090 struct btrfs_root *root,
2091 struct btrfs_root *log,
2092 struct btrfs_path *path,
2093 const u64 ino)
2094{
2095 struct btrfs_key search_key;
2096 struct btrfs_path *log_path;
2097 int i;
2098 int nritems;
2099 int ret;
2100
2101 log_path = btrfs_alloc_path();
2102 if (!log_path)
2103 return -ENOMEM;
2104
2105 search_key.objectid = ino;
2106 search_key.type = BTRFS_XATTR_ITEM_KEY;
2107 search_key.offset = 0;
2108again:
2109 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2110 if (ret < 0)
2111 goto out;
2112process_leaf:
2113 nritems = btrfs_header_nritems(path->nodes[0]);
2114 for (i = path->slots[0]; i < nritems; i++) {
2115 struct btrfs_key key;
2116 struct btrfs_dir_item *di;
2117 struct btrfs_dir_item *log_di;
2118 u32 total_size;
2119 u32 cur;
2120
2121 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2122 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2123 ret = 0;
2124 goto out;
2125 }
2126
2127 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2128 total_size = btrfs_item_size_nr(path->nodes[0], i);
2129 cur = 0;
2130 while (cur < total_size) {
2131 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2132 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2133 u32 this_len = sizeof(*di) + name_len + data_len;
2134 char *name;
2135
2136 name = kmalloc(name_len, GFP_NOFS);
2137 if (!name) {
2138 ret = -ENOMEM;
2139 goto out;
2140 }
2141 read_extent_buffer(path->nodes[0], name,
2142 (unsigned long)(di + 1), name_len);
2143
2144 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2145 name, name_len, 0);
2146 btrfs_release_path(log_path);
2147 if (!log_di) {
2148 /* Doesn't exist in log tree, so delete it. */
2149 btrfs_release_path(path);
2150 di = btrfs_lookup_xattr(trans, root, path, ino,
2151 name, name_len, -1);
2152 kfree(name);
2153 if (IS_ERR(di)) {
2154 ret = PTR_ERR(di);
2155 goto out;
2156 }
2157 ASSERT(di);
2158 ret = btrfs_delete_one_dir_name(trans, root,
2159 path, di);
2160 if (ret)
2161 goto out;
2162 btrfs_release_path(path);
2163 search_key = key;
2164 goto again;
2165 }
2166 kfree(name);
2167 if (IS_ERR(log_di)) {
2168 ret = PTR_ERR(log_di);
2169 goto out;
2170 }
2171 cur += this_len;
2172 di = (struct btrfs_dir_item *)((char *)di + this_len);
2173 }
2174 }
2175 ret = btrfs_next_leaf(root, path);
2176 if (ret > 0)
2177 ret = 0;
2178 else if (ret == 0)
2179 goto process_leaf;
2180out:
2181 btrfs_free_path(log_path);
2182 btrfs_release_path(path);
2183 return ret;
2184}
2185
2186
Chris Masone02119d2008-09-05 16:13:11 -04002187/*
2188 * deletion replay happens before we copy any new directory items
2189 * out of the log or out of backreferences from inodes. It
2190 * scans the log to find ranges of keys that log is authoritative for,
2191 * and then scans the directory to find items in those ranges that are
2192 * not present in the log.
2193 *
2194 * Anything we don't find in the log is unlinked and removed from the
2195 * directory.
2196 */
2197static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2198 struct btrfs_root *root,
2199 struct btrfs_root *log,
2200 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002201 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002202{
2203 u64 range_start;
2204 u64 range_end;
2205 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2206 int ret = 0;
2207 struct btrfs_key dir_key;
2208 struct btrfs_key found_key;
2209 struct btrfs_path *log_path;
2210 struct inode *dir;
2211
2212 dir_key.objectid = dirid;
2213 dir_key.type = BTRFS_DIR_ITEM_KEY;
2214 log_path = btrfs_alloc_path();
2215 if (!log_path)
2216 return -ENOMEM;
2217
2218 dir = read_one_inode(root, dirid);
2219 /* it isn't an error if the inode isn't there, that can happen
2220 * because we replay the deletes before we copy in the inode item
2221 * from the log
2222 */
2223 if (!dir) {
2224 btrfs_free_path(log_path);
2225 return 0;
2226 }
2227again:
2228 range_start = 0;
2229 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002230 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002231 if (del_all)
2232 range_end = (u64)-1;
2233 else {
2234 ret = find_dir_range(log, path, dirid, key_type,
2235 &range_start, &range_end);
2236 if (ret != 0)
2237 break;
2238 }
Chris Masone02119d2008-09-05 16:13:11 -04002239
2240 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002241 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002242 int nritems;
2243 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2244 0, 0);
2245 if (ret < 0)
2246 goto out;
2247
2248 nritems = btrfs_header_nritems(path->nodes[0]);
2249 if (path->slots[0] >= nritems) {
2250 ret = btrfs_next_leaf(root, path);
2251 if (ret)
2252 break;
2253 }
2254 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2255 path->slots[0]);
2256 if (found_key.objectid != dirid ||
2257 found_key.type != dir_key.type)
2258 goto next_type;
2259
2260 if (found_key.offset > range_end)
2261 break;
2262
2263 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002264 log_path, dir,
2265 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002266 if (ret)
2267 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002268 if (found_key.offset == (u64)-1)
2269 break;
2270 dir_key.offset = found_key.offset + 1;
2271 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002272 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002273 if (range_end == (u64)-1)
2274 break;
2275 range_start = range_end + 1;
2276 }
2277
2278next_type:
2279 ret = 0;
2280 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2281 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2282 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002283 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002284 goto again;
2285 }
2286out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002287 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002288 btrfs_free_path(log_path);
2289 iput(dir);
2290 return ret;
2291}
2292
2293/*
2294 * the process_func used to replay items from the log tree. This
2295 * gets called in two different stages. The first stage just looks
2296 * for inodes and makes sure they are all copied into the subvolume.
2297 *
2298 * The second stage copies all the other item types from the log into
2299 * the subvolume. The two stage approach is slower, but gets rid of
2300 * lots of complexity around inodes referencing other inodes that exist
2301 * only in the log (references come from either directory items or inode
2302 * back refs).
2303 */
2304static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2305 struct walk_control *wc, u64 gen)
2306{
2307 int nritems;
2308 struct btrfs_path *path;
2309 struct btrfs_root *root = wc->replay_dest;
2310 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002311 int level;
2312 int i;
2313 int ret;
2314
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002315 ret = btrfs_read_buffer(eb, gen);
2316 if (ret)
2317 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002318
2319 level = btrfs_header_level(eb);
2320
2321 if (level != 0)
2322 return 0;
2323
2324 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002325 if (!path)
2326 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002327
2328 nritems = btrfs_header_nritems(eb);
2329 for (i = 0; i < nritems; i++) {
2330 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002331
2332 /* inode keys are done during the first stage */
2333 if (key.type == BTRFS_INODE_ITEM_KEY &&
2334 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002335 struct btrfs_inode_item *inode_item;
2336 u32 mode;
2337
2338 inode_item = btrfs_item_ptr(eb, i,
2339 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002340 ret = replay_xattr_deletes(wc->trans, root, log,
2341 path, key.objectid);
2342 if (ret)
2343 break;
Chris Masone02119d2008-09-05 16:13:11 -04002344 mode = btrfs_inode_mode(eb, inode_item);
2345 if (S_ISDIR(mode)) {
2346 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002347 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002348 if (ret)
2349 break;
Chris Masone02119d2008-09-05 16:13:11 -04002350 }
2351 ret = overwrite_item(wc->trans, root, path,
2352 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002353 if (ret)
2354 break;
Chris Masone02119d2008-09-05 16:13:11 -04002355
Yan, Zhengc71bf092009-11-12 09:34:40 +00002356 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002357 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002358 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002359 */
2360 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002361 ret = insert_orphan_item(wc->trans, root,
2362 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002363 if (ret)
2364 break;
Chris Masone02119d2008-09-05 16:13:11 -04002365 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002366
Chris Masone02119d2008-09-05 16:13:11 -04002367 ret = link_to_fixup_dir(wc->trans, root,
2368 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002369 if (ret)
2370 break;
Chris Masone02119d2008-09-05 16:13:11 -04002371 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002372
2373 if (key.type == BTRFS_DIR_INDEX_KEY &&
2374 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2375 ret = replay_one_dir_item(wc->trans, root, path,
2376 eb, i, &key);
2377 if (ret)
2378 break;
2379 }
2380
Chris Masone02119d2008-09-05 16:13:11 -04002381 if (wc->stage < LOG_WALK_REPLAY_ALL)
2382 continue;
2383
2384 /* these keys are simply copied */
2385 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2386 ret = overwrite_item(wc->trans, root, path,
2387 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002388 if (ret)
2389 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002390 } else if (key.type == BTRFS_INODE_REF_KEY ||
2391 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002392 ret = add_inode_ref(wc->trans, root, log, path,
2393 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002394 if (ret && ret != -ENOENT)
2395 break;
2396 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002397 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2398 ret = replay_one_extent(wc->trans, root, path,
2399 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002400 if (ret)
2401 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002402 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002403 ret = replay_one_dir_item(wc->trans, root, path,
2404 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002405 if (ret)
2406 break;
Chris Masone02119d2008-09-05 16:13:11 -04002407 }
2408 }
2409 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002410 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002411}
2412
Chris Masond3977122009-01-05 21:25:51 -05002413static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002414 struct btrfs_root *root,
2415 struct btrfs_path *path, int *level,
2416 struct walk_control *wc)
2417{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002418 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002419 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002420 u64 bytenr;
2421 u64 ptr_gen;
2422 struct extent_buffer *next;
2423 struct extent_buffer *cur;
2424 struct extent_buffer *parent;
2425 u32 blocksize;
2426 int ret = 0;
2427
2428 WARN_ON(*level < 0);
2429 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2430
Chris Masond3977122009-01-05 21:25:51 -05002431 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002432 WARN_ON(*level < 0);
2433 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2434 cur = path->nodes[*level];
2435
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302436 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002437
2438 if (path->slots[*level] >=
2439 btrfs_header_nritems(cur))
2440 break;
2441
2442 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2443 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002444 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002445
2446 parent = path->nodes[*level];
2447 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002448
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002449 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002450 if (IS_ERR(next))
2451 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002452
Chris Masone02119d2008-09-05 16:13:11 -04002453 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002454 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002455 if (ret) {
2456 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002457 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002458 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002459
Chris Masone02119d2008-09-05 16:13:11 -04002460 path->slots[*level]++;
2461 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002462 ret = btrfs_read_buffer(next, ptr_gen);
2463 if (ret) {
2464 free_extent_buffer(next);
2465 return ret;
2466 }
Chris Masone02119d2008-09-05 16:13:11 -04002467
Josef Bacik681ae502013-10-07 15:11:00 -04002468 if (trans) {
2469 btrfs_tree_lock(next);
2470 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002471 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002472 btrfs_wait_tree_block_writeback(next);
2473 btrfs_tree_unlock(next);
2474 }
Chris Masone02119d2008-09-05 16:13:11 -04002475
Chris Masone02119d2008-09-05 16:13:11 -04002476 WARN_ON(root_owner !=
2477 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002478 ret = btrfs_free_and_pin_reserved_extent(
2479 fs_info, bytenr,
2480 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002481 if (ret) {
2482 free_extent_buffer(next);
2483 return ret;
2484 }
Chris Masone02119d2008-09-05 16:13:11 -04002485 }
2486 free_extent_buffer(next);
2487 continue;
2488 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002489 ret = btrfs_read_buffer(next, ptr_gen);
2490 if (ret) {
2491 free_extent_buffer(next);
2492 return ret;
2493 }
Chris Masone02119d2008-09-05 16:13:11 -04002494
2495 WARN_ON(*level <= 0);
2496 if (path->nodes[*level-1])
2497 free_extent_buffer(path->nodes[*level-1]);
2498 path->nodes[*level-1] = next;
2499 *level = btrfs_header_level(next);
2500 path->slots[*level] = 0;
2501 cond_resched();
2502 }
2503 WARN_ON(*level < 0);
2504 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2505
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002506 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002507
2508 cond_resched();
2509 return 0;
2510}
2511
Chris Masond3977122009-01-05 21:25:51 -05002512static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002513 struct btrfs_root *root,
2514 struct btrfs_path *path, int *level,
2515 struct walk_control *wc)
2516{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002517 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002518 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002519 int i;
2520 int slot;
2521 int ret;
2522
Chris Masond3977122009-01-05 21:25:51 -05002523 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002524 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002525 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002526 path->slots[i]++;
2527 *level = i;
2528 WARN_ON(*level == 0);
2529 return 0;
2530 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002531 struct extent_buffer *parent;
2532 if (path->nodes[*level] == root->node)
2533 parent = path->nodes[*level];
2534 else
2535 parent = path->nodes[*level + 1];
2536
2537 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002538 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002539 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002540 if (ret)
2541 return ret;
2542
Chris Masone02119d2008-09-05 16:13:11 -04002543 if (wc->free) {
2544 struct extent_buffer *next;
2545
2546 next = path->nodes[*level];
2547
Josef Bacik681ae502013-10-07 15:11:00 -04002548 if (trans) {
2549 btrfs_tree_lock(next);
2550 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002551 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002552 btrfs_wait_tree_block_writeback(next);
2553 btrfs_tree_unlock(next);
2554 }
Chris Masone02119d2008-09-05 16:13:11 -04002555
Chris Masone02119d2008-09-05 16:13:11 -04002556 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002557 ret = btrfs_free_and_pin_reserved_extent(
2558 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002559 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002560 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002561 if (ret)
2562 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002563 }
2564 free_extent_buffer(path->nodes[*level]);
2565 path->nodes[*level] = NULL;
2566 *level = i + 1;
2567 }
2568 }
2569 return 1;
2570}
2571
2572/*
2573 * drop the reference count on the tree rooted at 'snap'. This traverses
2574 * the tree freeing any blocks that have a ref count of zero after being
2575 * decremented.
2576 */
2577static int walk_log_tree(struct btrfs_trans_handle *trans,
2578 struct btrfs_root *log, struct walk_control *wc)
2579{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002580 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002581 int ret = 0;
2582 int wret;
2583 int level;
2584 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002585 int orig_level;
2586
2587 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002588 if (!path)
2589 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002590
2591 level = btrfs_header_level(log->node);
2592 orig_level = level;
2593 path->nodes[level] = log->node;
2594 extent_buffer_get(log->node);
2595 path->slots[level] = 0;
2596
Chris Masond3977122009-01-05 21:25:51 -05002597 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002598 wret = walk_down_log_tree(trans, log, path, &level, wc);
2599 if (wret > 0)
2600 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002601 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002602 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002603 goto out;
2604 }
Chris Masone02119d2008-09-05 16:13:11 -04002605
2606 wret = walk_up_log_tree(trans, log, path, &level, wc);
2607 if (wret > 0)
2608 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002609 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002610 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002611 goto out;
2612 }
Chris Masone02119d2008-09-05 16:13:11 -04002613 }
2614
2615 /* was the root node processed? if not, catch it here */
2616 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002617 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002618 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002619 if (ret)
2620 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002621 if (wc->free) {
2622 struct extent_buffer *next;
2623
2624 next = path->nodes[orig_level];
2625
Josef Bacik681ae502013-10-07 15:11:00 -04002626 if (trans) {
2627 btrfs_tree_lock(next);
2628 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002629 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002630 btrfs_wait_tree_block_writeback(next);
2631 btrfs_tree_unlock(next);
2632 }
Chris Masone02119d2008-09-05 16:13:11 -04002633
Chris Masone02119d2008-09-05 16:13:11 -04002634 WARN_ON(log->root_key.objectid !=
2635 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002636 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2637 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002638 if (ret)
2639 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002640 }
2641 }
2642
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002643out:
Chris Masone02119d2008-09-05 16:13:11 -04002644 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002645 return ret;
2646}
2647
Yan Zheng7237f182009-01-21 12:54:03 -05002648/*
2649 * helper function to update the item for a given subvolumes log root
2650 * in the tree of log roots
2651 */
2652static int update_log_root(struct btrfs_trans_handle *trans,
2653 struct btrfs_root *log)
2654{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002655 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002656 int ret;
2657
2658 if (log->log_transid == 1) {
2659 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002660 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002661 &log->root_key, &log->root_item);
2662 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002663 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002664 &log->root_key, &log->root_item);
2665 }
2666 return ret;
2667}
2668
Zhaolei60d53eb2015-08-17 18:44:46 +08002669static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002670{
2671 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002672 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002673
Yan Zheng7237f182009-01-21 12:54:03 -05002674 /*
2675 * we only allow two pending log transactions at a time,
2676 * so we know that if ours is more than 2 older than the
2677 * current transaction, we're done
2678 */
Liu Bo49e83f52017-09-01 16:14:30 -06002679 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002680 prepare_to_wait(&root->log_commit_wait[index],
2681 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002682
2683 if (!(root->log_transid_committed < transid &&
2684 atomic_read(&root->log_commit[index])))
2685 break;
2686
Yan Zheng7237f182009-01-21 12:54:03 -05002687 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002688 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002689 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002690 }
2691 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002692}
2693
Zhaolei60d53eb2015-08-17 18:44:46 +08002694static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002695{
2696 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002697
Liu Bo49e83f52017-09-01 16:14:30 -06002698 for (;;) {
2699 prepare_to_wait(&root->log_writer_wait, &wait,
2700 TASK_UNINTERRUPTIBLE);
2701 if (!atomic_read(&root->log_writers))
2702 break;
2703
Yan Zheng7237f182009-01-21 12:54:03 -05002704 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002705 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002706 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002707 }
Liu Bo49e83f52017-09-01 16:14:30 -06002708 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002709}
2710
Miao Xie8b050d32014-02-20 18:08:58 +08002711static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2712 struct btrfs_log_ctx *ctx)
2713{
2714 if (!ctx)
2715 return;
2716
2717 mutex_lock(&root->log_mutex);
2718 list_del_init(&ctx->list);
2719 mutex_unlock(&root->log_mutex);
2720}
2721
2722/*
2723 * Invoked in log mutex context, or be sure there is no other task which
2724 * can access the list.
2725 */
2726static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2727 int index, int error)
2728{
2729 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002730 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002731
Chris Mason570dd452016-10-27 10:42:20 -07002732 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2733 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002734 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002735 }
Miao Xie8b050d32014-02-20 18:08:58 +08002736
2737 INIT_LIST_HEAD(&root->log_ctxs[index]);
2738}
2739
Chris Masone02119d2008-09-05 16:13:11 -04002740/*
2741 * btrfs_sync_log does sends a given tree log down to the disk and
2742 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002743 * you know that any inodes previously logged are safely on disk only
2744 * if it returns 0.
2745 *
2746 * Any other return value means you need to call btrfs_commit_transaction.
2747 * Some of the edge cases for fsyncing directories that have had unlinks
2748 * or renames done in the past mean that sometimes the only safe
2749 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2750 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002751 */
2752int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002753 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002754{
Yan Zheng7237f182009-01-21 12:54:03 -05002755 int index1;
2756 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002757 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002758 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002759 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002760 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002761 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002762 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002763 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002764 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002765
Yan Zheng7237f182009-01-21 12:54:03 -05002766 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002767 log_transid = ctx->log_transid;
2768 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002769 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002770 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002771 }
Miao Xied1433de2014-02-20 18:08:59 +08002772
2773 index1 = log_transid % 2;
2774 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002775 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002776 mutex_unlock(&root->log_mutex);
2777 return ctx->log_ret;
2778 }
2779 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002780 atomic_set(&root->log_commit[index1], 1);
2781
2782 /* wait for previous tree log sync to complete */
2783 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002784 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002785
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002786 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002787 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002788 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002789 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002790 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002791 mutex_unlock(&root->log_mutex);
2792 schedule_timeout_uninterruptible(1);
2793 mutex_lock(&root->log_mutex);
2794 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002795 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002796 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002797 break;
2798 }
Chris Masond0c803c2008-09-11 16:17:57 -04002799
Chris Mason12fcfd22009-03-24 10:24:20 -04002800 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002801 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002802 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002803 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002804 mutex_unlock(&root->log_mutex);
2805 goto out;
2806 }
2807
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002808 if (log_transid % 2 == 0)
2809 mark = EXTENT_DIRTY;
2810 else
2811 mark = EXTENT_NEW;
2812
Chris Mason690587d2009-10-13 13:29:19 -04002813 /* we start IO on all the marked extents here, but we don't actually
2814 * wait for them until later.
2815 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002816 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002817 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002818 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002819 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002820 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002821 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002822 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002823 mutex_unlock(&root->log_mutex);
2824 goto out;
2825 }
Yan Zheng7237f182009-01-21 12:54:03 -05002826
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002827 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002828
Yan Zheng7237f182009-01-21 12:54:03 -05002829 root->log_transid++;
2830 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002831 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002832 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002833 * IO has been started, blocks of the log tree have WRITTEN flag set
2834 * in their headers. new modifications of the log will be written to
2835 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002836 */
2837 mutex_unlock(&root->log_mutex);
2838
Filipe Manana28a23592016-08-23 21:13:51 +01002839 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002840
Yan Zheng7237f182009-01-21 12:54:03 -05002841 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002842 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002843 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002844
2845 index2 = log_root_tree->log_transid % 2;
2846 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2847 root_log_ctx.log_transid = log_root_tree->log_transid;
2848
Yan Zheng7237f182009-01-21 12:54:03 -05002849 mutex_unlock(&log_root_tree->log_mutex);
2850
2851 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002852
2853 mutex_lock(&log_root_tree->log_mutex);
2854 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002855 /*
2856 * Implicit memory barrier after atomic_dec_and_test
2857 */
Yan Zheng7237f182009-01-21 12:54:03 -05002858 if (waitqueue_active(&log_root_tree->log_writer_wait))
2859 wake_up(&log_root_tree->log_writer_wait);
2860 }
2861
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002862 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002863 if (!list_empty(&root_log_ctx.list))
2864 list_del_init(&root_log_ctx.list);
2865
Miao Xiec6adc9c2013-05-28 10:05:39 +00002866 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08002868
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002869 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002870 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002871 mutex_unlock(&log_root_tree->log_mutex);
2872 goto out;
2873 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002874 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002875 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002876 mutex_unlock(&log_root_tree->log_mutex);
2877 ret = -EAGAIN;
2878 goto out;
2879 }
2880
Miao Xied1433de2014-02-20 18:08:59 +08002881 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002882 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002883 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002884 mutex_unlock(&log_root_tree->log_mutex);
2885 ret = root_log_ctx.log_ret;
2886 goto out;
2887 }
Miao Xie8b050d32014-02-20 18:08:58 +08002888
Miao Xied1433de2014-02-20 18:08:59 +08002889 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002890 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002891 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002892 ret = btrfs_wait_tree_log_extents(log, mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002893 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002894 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002895 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002896 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002897 if (!ret)
2898 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002899 goto out;
2900 }
Miao Xied1433de2014-02-20 18:08:59 +08002901 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002902 atomic_set(&log_root_tree->log_commit[index2], 1);
2903
Chris Mason12fcfd22009-03-24 10:24:20 -04002904 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002905 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002906 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002907 }
Yan Zheng7237f182009-01-21 12:54:03 -05002908
Zhaolei60d53eb2015-08-17 18:44:46 +08002909 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002910
2911 /*
2912 * now that we've moved on to the tree of log tree roots,
2913 * check the full commit flag again
2914 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002915 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002916 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002917 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002918 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002919 mutex_unlock(&log_root_tree->log_mutex);
2920 ret = -EAGAIN;
2921 goto out_wake_log_root;
2922 }
Yan Zheng7237f182009-01-21 12:54:03 -05002923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002924 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00002925 &log_root_tree->dirty_log_pages,
2926 EXTENT_DIRTY | EXTENT_NEW);
2927 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002928 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002929 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002930 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002931 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002932 mutex_unlock(&log_root_tree->log_mutex);
2933 goto out_wake_log_root;
2934 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002935 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002936 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002937 ret = btrfs_wait_tree_log_extents(log_root_tree,
2938 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002939 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002940 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002941 btrfs_free_logged_extents(log, log_transid);
2942 mutex_unlock(&log_root_tree->log_mutex);
2943 goto out_wake_log_root;
2944 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002945 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002946
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002947 btrfs_set_super_log_root(fs_info->super_for_commit,
2948 log_root_tree->node->start);
2949 btrfs_set_super_log_root_level(fs_info->super_for_commit,
2950 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002951
Yan Zheng7237f182009-01-21 12:54:03 -05002952 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002953 mutex_unlock(&log_root_tree->log_mutex);
2954
2955 /*
2956 * nobody else is going to jump in and write the the ctree
2957 * super here because the log_commit atomic below is protecting
2958 * us. We must be called with a transaction handle pinning
2959 * the running transaction open, so a full commit can't hop
2960 * in and cause problems either.
2961 */
David Sterbaeece6a92017-02-10 19:04:32 +01002962 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002963 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002964 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002965 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002966 goto out_wake_log_root;
2967 }
Yan Zheng7237f182009-01-21 12:54:03 -05002968
Chris Mason257c62e2009-10-13 13:21:08 -04002969 mutex_lock(&root->log_mutex);
2970 if (root->last_log_commit < log_transid)
2971 root->last_log_commit = log_transid;
2972 mutex_unlock(&root->log_mutex);
2973
Chris Mason12fcfd22009-03-24 10:24:20 -04002974out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002975 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002976 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2977
Miao Xied1433de2014-02-20 18:08:59 +08002978 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002979 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002980 mutex_unlock(&log_root_tree->log_mutex);
2981
David Sterba33a9eca2015-10-10 18:35:10 +02002982 /*
2983 * The barrier before waitqueue_active is implied by mutex_unlock
2984 */
Yan Zheng7237f182009-01-21 12:54:03 -05002985 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2986 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002987out:
Miao Xied1433de2014-02-20 18:08:59 +08002988 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002989 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002990 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002991 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002992 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002993
David Sterba33a9eca2015-10-10 18:35:10 +02002994 /*
2995 * The barrier before waitqueue_active is implied by mutex_unlock
2996 */
Yan Zheng7237f182009-01-21 12:54:03 -05002997 if (waitqueue_active(&root->log_commit_wait[index1]))
2998 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002999 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003000}
3001
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003002static void free_log_tree(struct btrfs_trans_handle *trans,
3003 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003004{
3005 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003006 u64 start;
3007 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003008 struct walk_control wc = {
3009 .free = 1,
3010 .process_func = process_one_buffer
3011 };
3012
Josef Bacik681ae502013-10-07 15:11:00 -04003013 ret = walk_log_tree(trans, log, &wc);
3014 /* I don't think this can happen but just in case */
3015 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003016 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003017
Chris Masond3977122009-01-05 21:25:51 -05003018 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003019 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04003020 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
3021 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003022 if (ret)
3023 break;
3024
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003025 clear_extent_bits(&log->dirty_log_pages, start, end,
David Sterba91166212016-04-26 23:54:39 +02003026 EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04003027 }
3028
Josef Bacik2ab28f32012-10-12 15:27:49 -04003029 /*
3030 * We may have short-circuited the log tree with the full commit logic
3031 * and left ordered extents on our list, so clear these out to keep us
3032 * from leaking inodes and memory.
3033 */
3034 btrfs_free_logged_extents(log, 0);
3035 btrfs_free_logged_extents(log, 1);
3036
Yan Zheng7237f182009-01-21 12:54:03 -05003037 free_extent_buffer(log->node);
3038 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003039}
3040
3041/*
3042 * free all the extents used by the tree log. This should be called
3043 * at commit time of the full transaction
3044 */
3045int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3046{
3047 if (root->log_root) {
3048 free_log_tree(trans, root->log_root);
3049 root->log_root = NULL;
3050 }
3051 return 0;
3052}
3053
3054int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3055 struct btrfs_fs_info *fs_info)
3056{
3057 if (fs_info->log_root_tree) {
3058 free_log_tree(trans, fs_info->log_root_tree);
3059 fs_info->log_root_tree = NULL;
3060 }
Chris Masone02119d2008-09-05 16:13:11 -04003061 return 0;
3062}
3063
3064/*
Chris Masone02119d2008-09-05 16:13:11 -04003065 * If both a file and directory are logged, and unlinks or renames are
3066 * mixed in, we have a few interesting corners:
3067 *
3068 * create file X in dir Y
3069 * link file X to X.link in dir Y
3070 * fsync file X
3071 * unlink file X but leave X.link
3072 * fsync dir Y
3073 *
3074 * After a crash we would expect only X.link to exist. But file X
3075 * didn't get fsync'd again so the log has back refs for X and X.link.
3076 *
3077 * We solve this by removing directory entries and inode backrefs from the
3078 * log when a file that was logged in the current transaction is
3079 * unlinked. Any later fsync will include the updated log entries, and
3080 * we'll be able to reconstruct the proper directory items from backrefs.
3081 *
3082 * This optimizations allows us to avoid relogging the entire inode
3083 * or the entire directory.
3084 */
3085int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3086 struct btrfs_root *root,
3087 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003088 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003089{
3090 struct btrfs_root *log;
3091 struct btrfs_dir_item *di;
3092 struct btrfs_path *path;
3093 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003094 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003095 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003096 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003097
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003098 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003099 return 0;
3100
Chris Masone02119d2008-09-05 16:13:11 -04003101 ret = join_running_log_trans(root);
3102 if (ret)
3103 return 0;
3104
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003105 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003106
3107 log = root->log_root;
3108 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003109 if (!path) {
3110 err = -ENOMEM;
3111 goto out_unlock;
3112 }
liubo2a29edc2011-01-26 06:22:08 +00003113
Li Zefan33345d012011-04-20 10:31:50 +08003114 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003115 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003116 if (IS_ERR(di)) {
3117 err = PTR_ERR(di);
3118 goto fail;
3119 }
3120 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003121 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3122 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003123 if (ret) {
3124 err = ret;
3125 goto fail;
3126 }
Chris Masone02119d2008-09-05 16:13:11 -04003127 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003128 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003129 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003130 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003131 if (IS_ERR(di)) {
3132 err = PTR_ERR(di);
3133 goto fail;
3134 }
3135 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003136 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3137 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003138 if (ret) {
3139 err = ret;
3140 goto fail;
3141 }
Chris Masone02119d2008-09-05 16:13:11 -04003142 }
3143
3144 /* update the directory size in the log to reflect the names
3145 * we have removed
3146 */
3147 if (bytes_del) {
3148 struct btrfs_key key;
3149
Li Zefan33345d012011-04-20 10:31:50 +08003150 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003151 key.offset = 0;
3152 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003153 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003154
3155 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003156 if (ret < 0) {
3157 err = ret;
3158 goto fail;
3159 }
Chris Masone02119d2008-09-05 16:13:11 -04003160 if (ret == 0) {
3161 struct btrfs_inode_item *item;
3162 u64 i_size;
3163
3164 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3165 struct btrfs_inode_item);
3166 i_size = btrfs_inode_size(path->nodes[0], item);
3167 if (i_size > bytes_del)
3168 i_size -= bytes_del;
3169 else
3170 i_size = 0;
3171 btrfs_set_inode_size(path->nodes[0], item, i_size);
3172 btrfs_mark_buffer_dirty(path->nodes[0]);
3173 } else
3174 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003175 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003176 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003177fail:
Chris Masone02119d2008-09-05 16:13:11 -04003178 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003179out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003180 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003181 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003182 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003183 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003185 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003186
Chris Mason12fcfd22009-03-24 10:24:20 -04003187 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003188
Andi Kleen411fc6b2010-10-29 15:14:31 -04003189 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003190}
3191
3192/* see comments for btrfs_del_dir_entries_in_log */
3193int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3194 struct btrfs_root *root,
3195 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003196 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003198 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003199 struct btrfs_root *log;
3200 u64 index;
3201 int ret;
3202
Nikolay Borisova491abb2017-01-18 00:31:33 +02003203 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003204 return 0;
3205
Chris Masone02119d2008-09-05 16:13:11 -04003206 ret = join_running_log_trans(root);
3207 if (ret)
3208 return 0;
3209 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003210 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003211
Nikolay Borisova491abb2017-01-18 00:31:33 +02003212 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003213 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003214 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003215 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003216 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003217 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003218 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003219 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003220 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003221
Chris Masone02119d2008-09-05 16:13:11 -04003222 return ret;
3223}
3224
3225/*
3226 * creates a range item in the log for 'dirid'. first_offset and
3227 * last_offset tell us which parts of the key space the log should
3228 * be considered authoritative for.
3229 */
3230static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3231 struct btrfs_root *log,
3232 struct btrfs_path *path,
3233 int key_type, u64 dirid,
3234 u64 first_offset, u64 last_offset)
3235{
3236 int ret;
3237 struct btrfs_key key;
3238 struct btrfs_dir_log_item *item;
3239
3240 key.objectid = dirid;
3241 key.offset = first_offset;
3242 if (key_type == BTRFS_DIR_ITEM_KEY)
3243 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3244 else
3245 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3246 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003247 if (ret)
3248 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003249
3250 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3251 struct btrfs_dir_log_item);
3252 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3253 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003254 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003255 return 0;
3256}
3257
3258/*
3259 * log all the items included in the current transaction for a given
3260 * directory. This also creates the range items in the log tree required
3261 * to replay anything deleted before the fsync
3262 */
3263static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003264 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003265 struct btrfs_path *path,
3266 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003267 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003268 u64 min_offset, u64 *last_offset_ret)
3269{
3270 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003271 struct btrfs_root *log = root->log_root;
3272 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003273 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003274 int ret;
3275 int i;
3276 int nritems;
3277 u64 first_offset = min_offset;
3278 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003279 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003280
3281 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003282
Li Zefan33345d012011-04-20 10:31:50 +08003283 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003284 min_key.type = key_type;
3285 min_key.offset = min_offset;
3286
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003287 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003288
3289 /*
3290 * we didn't find anything from this transaction, see if there
3291 * is anything at all
3292 */
Li Zefan33345d012011-04-20 10:31:50 +08003293 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3294 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003295 min_key.type = key_type;
3296 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003297 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003298 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3299 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003300 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003301 return ret;
3302 }
Li Zefan33345d012011-04-20 10:31:50 +08003303 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003304
3305 /* if ret == 0 there are items for this type,
3306 * create a range to tell us the last key of this type.
3307 * otherwise, there are no items in this directory after
3308 * *min_offset, and we create a range to indicate that.
3309 */
3310 if (ret == 0) {
3311 struct btrfs_key tmp;
3312 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3313 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003314 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003315 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003316 }
3317 goto done;
3318 }
3319
3320 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003321 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003322 if (ret == 0) {
3323 struct btrfs_key tmp;
3324 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3325 if (key_type == tmp.type) {
3326 first_offset = tmp.offset;
3327 ret = overwrite_item(trans, log, dst_path,
3328 path->nodes[0], path->slots[0],
3329 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003330 if (ret) {
3331 err = ret;
3332 goto done;
3333 }
Chris Masone02119d2008-09-05 16:13:11 -04003334 }
3335 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003336 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003337
3338 /* find the first key from this transaction again */
3339 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303340 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003341 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003342
3343 /*
3344 * we have a block from this transaction, log every item in it
3345 * from our directory
3346 */
Chris Masond3977122009-01-05 21:25:51 -05003347 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003348 struct btrfs_key tmp;
3349 src = path->nodes[0];
3350 nritems = btrfs_header_nritems(src);
3351 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003352 struct btrfs_dir_item *di;
3353
Chris Masone02119d2008-09-05 16:13:11 -04003354 btrfs_item_key_to_cpu(src, &min_key, i);
3355
Li Zefan33345d012011-04-20 10:31:50 +08003356 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003357 goto done;
3358 ret = overwrite_item(trans, log, dst_path, src, i,
3359 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003360 if (ret) {
3361 err = ret;
3362 goto done;
3363 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003364
3365 /*
3366 * We must make sure that when we log a directory entry,
3367 * the corresponding inode, after log replay, has a
3368 * matching link count. For example:
3369 *
3370 * touch foo
3371 * mkdir mydir
3372 * sync
3373 * ln foo mydir/bar
3374 * xfs_io -c "fsync" mydir
3375 * <crash>
3376 * <mount fs and log replay>
3377 *
3378 * Would result in a fsync log that when replayed, our
3379 * file inode would have a link count of 1, but we get
3380 * two directory entries pointing to the same inode.
3381 * After removing one of the names, it would not be
3382 * possible to remove the other name, which resulted
3383 * always in stale file handle errors, and would not
3384 * be possible to rmdir the parent directory, since
3385 * its i_size could never decrement to the value
3386 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3387 */
3388 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3389 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3390 if (ctx &&
3391 (btrfs_dir_transid(src, di) == trans->transid ||
3392 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3393 tmp.type != BTRFS_ROOT_ITEM_KEY)
3394 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003395 }
3396 path->slots[0] = nritems;
3397
3398 /*
3399 * look ahead to the next item and see if it is also
3400 * from this directory and from this transaction
3401 */
3402 ret = btrfs_next_leaf(root, path);
3403 if (ret == 1) {
3404 last_offset = (u64)-1;
3405 goto done;
3406 }
3407 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003408 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003409 last_offset = (u64)-1;
3410 goto done;
3411 }
3412 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3413 ret = overwrite_item(trans, log, dst_path,
3414 path->nodes[0], path->slots[0],
3415 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003416 if (ret)
3417 err = ret;
3418 else
3419 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003420 goto done;
3421 }
3422 }
3423done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003424 btrfs_release_path(path);
3425 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003426
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003427 if (err == 0) {
3428 *last_offset_ret = last_offset;
3429 /*
3430 * insert the log range keys to indicate where the log
3431 * is valid
3432 */
3433 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003434 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003435 if (ret)
3436 err = ret;
3437 }
3438 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003439}
3440
3441/*
3442 * logging directories is very similar to logging inodes, We find all the items
3443 * from the current transaction and write them to the log.
3444 *
3445 * The recovery code scans the directory in the subvolume, and if it finds a
3446 * key in the range logged that is not present in the log tree, then it means
3447 * that dir entry was unlinked during the transaction.
3448 *
3449 * In order for that scan to work, we must include one key smaller than
3450 * the smallest logged by this transaction and one key larger than the largest
3451 * key logged by this transaction.
3452 */
3453static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003454 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003455 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003456 struct btrfs_path *dst_path,
3457 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003458{
3459 u64 min_key;
3460 u64 max_key;
3461 int ret;
3462 int key_type = BTRFS_DIR_ITEM_KEY;
3463
3464again:
3465 min_key = 0;
3466 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003467 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003468 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3469 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003470 if (ret)
3471 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003472 if (max_key == (u64)-1)
3473 break;
3474 min_key = max_key + 1;
3475 }
3476
3477 if (key_type == BTRFS_DIR_ITEM_KEY) {
3478 key_type = BTRFS_DIR_INDEX_KEY;
3479 goto again;
3480 }
3481 return 0;
3482}
3483
3484/*
3485 * a helper function to drop items from the log before we relog an
3486 * inode. max_key_type indicates the highest item type to remove.
3487 * This cannot be run for file data extents because it does not
3488 * free the extents they point to.
3489 */
3490static int drop_objectid_items(struct btrfs_trans_handle *trans,
3491 struct btrfs_root *log,
3492 struct btrfs_path *path,
3493 u64 objectid, int max_key_type)
3494{
3495 int ret;
3496 struct btrfs_key key;
3497 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003498 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003499
3500 key.objectid = objectid;
3501 key.type = max_key_type;
3502 key.offset = (u64)-1;
3503
Chris Masond3977122009-01-05 21:25:51 -05003504 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003505 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003506 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003507 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003508 break;
3509
3510 if (path->slots[0] == 0)
3511 break;
3512
3513 path->slots[0]--;
3514 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3515 path->slots[0]);
3516
3517 if (found_key.objectid != objectid)
3518 break;
3519
Josef Bacik18ec90d2012-09-28 11:56:28 -04003520 found_key.offset = 0;
3521 found_key.type = 0;
3522 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3523 &start_slot);
3524
3525 ret = btrfs_del_items(trans, log, path, start_slot,
3526 path->slots[0] - start_slot + 1);
3527 /*
3528 * If start slot isn't 0 then we don't need to re-search, we've
3529 * found the last guy with the objectid in this tree.
3530 */
3531 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003532 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003533 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003534 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003535 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003536 if (ret > 0)
3537 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003538 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003539}
3540
Josef Bacik94edf4a2012-09-25 14:56:25 -04003541static void fill_inode_item(struct btrfs_trans_handle *trans,
3542 struct extent_buffer *leaf,
3543 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003544 struct inode *inode, int log_inode_only,
3545 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003546{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003547 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003548
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003549 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003550
3551 if (log_inode_only) {
3552 /* set the generation to zero so the recover code
3553 * can tell the difference between an logging
3554 * just to say 'this inode exists' and a logging
3555 * to say 'update this inode with these values'
3556 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003557 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003558 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003559 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003560 btrfs_set_token_inode_generation(leaf, item,
3561 BTRFS_I(inode)->generation,
3562 &token);
3563 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003564 }
3565
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003566 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3567 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3568 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3569 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3570
David Sterbaa937b972014-12-12 17:39:12 +01003571 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003572 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003573 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003574 inode->i_atime.tv_nsec, &token);
3575
David Sterbaa937b972014-12-12 17:39:12 +01003576 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003577 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003578 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003579 inode->i_mtime.tv_nsec, &token);
3580
David Sterbaa937b972014-12-12 17:39:12 +01003581 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003582 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003583 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003584 inode->i_ctime.tv_nsec, &token);
3585
3586 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3587 &token);
3588
3589 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3590 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3591 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3592 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3593 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003594}
3595
Josef Bacika95249b2012-10-11 16:17:34 -04003596static int log_inode_item(struct btrfs_trans_handle *trans,
3597 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003598 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003599{
3600 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003601 int ret;
3602
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003603 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003604 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003605 if (ret && ret != -EEXIST)
3606 return ret;
3607 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3608 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003609 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3610 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003611 btrfs_release_path(path);
3612 return 0;
3613}
3614
Chris Mason31ff1cd2008-09-11 16:17:57 -04003615static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003616 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003617 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003618 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003619 int start_slot, int nr, int inode_only,
3620 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003621{
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003622 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003623 unsigned long src_offset;
3624 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003625 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003626 struct btrfs_file_extent_item *extent;
3627 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003628 struct extent_buffer *src = src_path->nodes[0];
3629 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003630 int ret;
3631 struct btrfs_key *ins_keys;
3632 u32 *ins_sizes;
3633 char *ins_data;
3634 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003635 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003636 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003637 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003638 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003639 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003640
3641 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003642
3643 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3644 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003645 if (!ins_data)
3646 return -ENOMEM;
3647
Josef Bacik16e75492013-10-22 12:18:51 -04003648 first_key.objectid = (u64)-1;
3649
Chris Mason31ff1cd2008-09-11 16:17:57 -04003650 ins_sizes = (u32 *)ins_data;
3651 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3652
3653 for (i = 0; i < nr; i++) {
3654 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3655 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3656 }
3657 ret = btrfs_insert_empty_items(trans, log, dst_path,
3658 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003659 if (ret) {
3660 kfree(ins_data);
3661 return ret;
3662 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003663
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003664 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003665 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3666 dst_path->slots[0]);
3667
3668 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3669
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003670 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003671 last_key = ins_keys[i];
3672
Josef Bacik94edf4a2012-09-25 14:56:25 -04003673 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003674 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3675 dst_path->slots[0],
3676 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003677 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003678 &inode->vfs_inode,
3679 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003680 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003681 } else {
3682 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3683 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003684 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003685
Josef Bacik16e75492013-10-22 12:18:51 -04003686 /*
3687 * We set need_find_last_extent here in case we know we were
3688 * processing other items and then walk into the first extent in
3689 * the inode. If we don't hit an extent then nothing changes,
3690 * we'll do the last search the next time around.
3691 */
3692 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3693 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003694 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003695 first_key = ins_keys[i];
3696 } else {
3697 need_find_last_extent = false;
3698 }
3699
Chris Mason31ff1cd2008-09-11 16:17:57 -04003700 /* take a reference on file data extents so that truncates
3701 * or deletes of this inode don't have to relog the inode
3702 * again
3703 */
David Sterba962a2982014-06-04 18:41:45 +02003704 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003705 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003706 int found_type;
3707 extent = btrfs_item_ptr(src, start_slot + i,
3708 struct btrfs_file_extent_item);
3709
liubo8e531cd2011-05-06 10:36:09 +08003710 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3711 continue;
3712
Chris Mason31ff1cd2008-09-11 16:17:57 -04003713 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003714 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003715 u64 ds, dl, cs, cl;
3716 ds = btrfs_file_extent_disk_bytenr(src,
3717 extent);
3718 /* ds == 0 is a hole */
3719 if (ds == 0)
3720 continue;
3721
3722 dl = btrfs_file_extent_disk_num_bytes(src,
3723 extent);
3724 cs = btrfs_file_extent_offset(src, extent);
3725 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003726 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003727 if (btrfs_file_extent_compression(src,
3728 extent)) {
3729 cs = 0;
3730 cl = dl;
3731 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003732
3733 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003734 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003735 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003736 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003737 if (ret) {
3738 btrfs_release_path(dst_path);
3739 kfree(ins_data);
3740 return ret;
3741 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003742 }
3743 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003744 }
3745
3746 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003747 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003748 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003749
3750 /*
3751 * we have to do this after the loop above to avoid changing the
3752 * log tree while trying to change the log tree.
3753 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003754 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003755 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003756 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3757 struct btrfs_ordered_sum,
3758 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003759 if (!ret)
3760 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003761 list_del(&sums->list);
3762 kfree(sums);
3763 }
Josef Bacik16e75492013-10-22 12:18:51 -04003764
3765 if (!has_extents)
3766 return ret;
3767
Filipe Manana74121f7c2014-08-07 12:00:44 +01003768 if (need_find_last_extent && *last_extent == first_key.offset) {
3769 /*
3770 * We don't have any leafs between our current one and the one
3771 * we processed before that can have file extent items for our
3772 * inode (and have a generation number smaller than our current
3773 * transaction id).
3774 */
3775 need_find_last_extent = false;
3776 }
3777
Josef Bacik16e75492013-10-22 12:18:51 -04003778 /*
3779 * Because we use btrfs_search_forward we could skip leaves that were
3780 * not modified and then assume *last_extent is valid when it really
3781 * isn't. So back up to the previous leaf and read the end of the last
3782 * extent before we go and fill in holes.
3783 */
3784 if (need_find_last_extent) {
3785 u64 len;
3786
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003787 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003788 if (ret < 0)
3789 return ret;
3790 if (ret)
3791 goto fill_holes;
3792 if (src_path->slots[0])
3793 src_path->slots[0]--;
3794 src = src_path->nodes[0];
3795 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003796 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003797 key.type != BTRFS_EXTENT_DATA_KEY)
3798 goto fill_holes;
3799 extent = btrfs_item_ptr(src, src_path->slots[0],
3800 struct btrfs_file_extent_item);
3801 if (btrfs_file_extent_type(src, extent) ==
3802 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003803 len = btrfs_file_extent_inline_len(src,
3804 src_path->slots[0],
3805 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003806 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003807 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003808 } else {
3809 len = btrfs_file_extent_num_bytes(src, extent);
3810 *last_extent = key.offset + len;
3811 }
3812 }
3813fill_holes:
3814 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3815 * things could have happened
3816 *
3817 * 1) A merge could have happened, so we could currently be on a leaf
3818 * that holds what we were copying in the first place.
3819 * 2) A split could have happened, and now not all of the items we want
3820 * are on the same leaf.
3821 *
3822 * So we need to adjust how we search for holes, we need to drop the
3823 * path and re-search for the first extent key we found, and then walk
3824 * forward until we hit the last one we copied.
3825 */
3826 if (need_find_last_extent) {
3827 /* btrfs_prev_leaf could return 1 without releasing the path */
3828 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01003829 ret = btrfs_search_slot(NULL, inode->root, &first_key,
3830 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04003831 if (ret < 0)
3832 return ret;
3833 ASSERT(ret == 0);
3834 src = src_path->nodes[0];
3835 i = src_path->slots[0];
3836 } else {
3837 i = start_slot;
3838 }
3839
3840 /*
3841 * Ok so here we need to go through and fill in any holes we may have
3842 * to make sure that holes are punched for those areas in case they had
3843 * extents previously.
3844 */
3845 while (!done) {
3846 u64 offset, len;
3847 u64 extent_end;
3848
3849 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003850 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003851 if (ret < 0)
3852 return ret;
3853 ASSERT(ret == 0);
3854 src = src_path->nodes[0];
3855 i = 0;
3856 }
3857
3858 btrfs_item_key_to_cpu(src, &key, i);
3859 if (!btrfs_comp_cpu_keys(&key, &last_key))
3860 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003861 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003862 key.type != BTRFS_EXTENT_DATA_KEY) {
3863 i++;
3864 continue;
3865 }
3866 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3867 if (btrfs_file_extent_type(src, extent) ==
3868 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003869 len = btrfs_file_extent_inline_len(src, i, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04003870 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003871 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003872 } else {
3873 len = btrfs_file_extent_num_bytes(src, extent);
3874 extent_end = key.offset + len;
3875 }
3876 i++;
3877
3878 if (*last_extent == key.offset) {
3879 *last_extent = extent_end;
3880 continue;
3881 }
3882 offset = *last_extent;
3883 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003884 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01003885 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04003886 if (ret)
3887 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003888 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003889 }
3890 /*
3891 * Need to let the callers know we dropped the path so they should
3892 * re-search.
3893 */
3894 if (!ret && need_find_last_extent)
3895 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003896 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003897}
3898
Josef Bacik5dc562c2012-08-17 13:14:17 -04003899static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3900{
3901 struct extent_map *em1, *em2;
3902
3903 em1 = list_entry(a, struct extent_map, list);
3904 em2 = list_entry(b, struct extent_map, list);
3905
3906 if (em1->start < em2->start)
3907 return -1;
3908 else if (em1->start > em2->start)
3909 return 1;
3910 return 0;
3911}
3912
Filipe Manana8407f552014-09-05 15:14:39 +01003913static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3914 struct inode *inode,
3915 struct btrfs_root *root,
3916 const struct extent_map *em,
3917 const struct list_head *logged_list,
3918 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003919{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003920 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003921 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003922 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003923 u64 mod_start = em->mod_start;
3924 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003925 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003926 u64 csum_offset;
3927 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003928 LIST_HEAD(ordered_sums);
3929 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003930
Filipe Manana8407f552014-09-05 15:14:39 +01003931 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003932
Filipe Manana8407f552014-09-05 15:14:39 +01003933 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3934 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003935 return 0;
3936
Josef Bacik2ab28f32012-10-12 15:27:49 -04003937 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003938 * Wait far any ordered extent that covers our extent map. If it
3939 * finishes without an error, first check and see if our csums are on
3940 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003941 */
Miao Xie827463c2014-01-14 20:31:51 +08003942 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003943 struct btrfs_ordered_sum *sum;
3944
3945 if (!mod_len)
3946 break;
3947
Josef Bacik2ab28f32012-10-12 15:27:49 -04003948 if (ordered->file_offset + ordered->len <= mod_start ||
3949 mod_start + mod_len <= ordered->file_offset)
3950 continue;
3951
Filipe Manana8407f552014-09-05 15:14:39 +01003952 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3953 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3954 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3955 const u64 start = ordered->file_offset;
3956 const u64 end = ordered->file_offset + ordered->len - 1;
3957
3958 WARN_ON(ordered->inode != inode);
3959 filemap_fdatawrite_range(inode->i_mapping, start, end);
3960 }
3961
3962 wait_event(ordered->wait,
3963 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3964 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3965
3966 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003967 /*
3968 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3969 * i_mapping flags, so that the next fsync won't get
3970 * an outdated io error too.
3971 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003972 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003973 *ordered_io_error = true;
3974 break;
3975 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003976 /*
3977 * We are going to copy all the csums on this ordered extent, so
3978 * go ahead and adjust mod_start and mod_len in case this
3979 * ordered extent has already been logged.
3980 */
3981 if (ordered->file_offset > mod_start) {
3982 if (ordered->file_offset + ordered->len >=
3983 mod_start + mod_len)
3984 mod_len = ordered->file_offset - mod_start;
3985 /*
3986 * If we have this case
3987 *
3988 * |--------- logged extent ---------|
3989 * |----- ordered extent ----|
3990 *
3991 * Just don't mess with mod_start and mod_len, we'll
3992 * just end up logging more csums than we need and it
3993 * will be ok.
3994 */
3995 } else {
3996 if (ordered->file_offset + ordered->len <
3997 mod_start + mod_len) {
3998 mod_len = (mod_start + mod_len) -
3999 (ordered->file_offset + ordered->len);
4000 mod_start = ordered->file_offset +
4001 ordered->len;
4002 } else {
4003 mod_len = 0;
4004 }
4005 }
4006
Filipe Manana8407f552014-09-05 15:14:39 +01004007 if (skip_csum)
4008 continue;
4009
Josef Bacik2ab28f32012-10-12 15:27:49 -04004010 /*
4011 * To keep us from looping for the above case of an ordered
4012 * extent that falls inside of the logged extent.
4013 */
4014 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4015 &ordered->flags))
4016 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004017
Josef Bacik2ab28f32012-10-12 15:27:49 -04004018 list_for_each_entry(sum, &ordered->list, list) {
4019 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004020 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004021 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004022 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004023 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004024
Filipe Manana8407f552014-09-05 15:14:39 +01004025 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004026 return ret;
4027
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004028 if (em->compress_type) {
4029 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004030 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004031 } else {
4032 csum_offset = mod_start - em->start;
4033 csum_len = mod_len;
4034 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004035
Josef Bacik70c8a912012-10-11 16:54:30 -04004036 /* block start is already adjusted for the file extent offset. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004037 ret = btrfs_lookup_csums_range(fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004038 em->block_start + csum_offset,
4039 em->block_start + csum_offset +
4040 csum_len - 1, &ordered_sums, 0);
4041 if (ret)
4042 return ret;
4043
4044 while (!list_empty(&ordered_sums)) {
4045 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4046 struct btrfs_ordered_sum,
4047 list);
4048 if (!ret)
4049 ret = btrfs_csum_file_blocks(trans, log, sums);
4050 list_del(&sums->list);
4051 kfree(sums);
4052 }
4053
4054 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004055}
4056
Filipe Manana8407f552014-09-05 15:14:39 +01004057static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004058 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004059 const struct extent_map *em,
4060 struct btrfs_path *path,
4061 const struct list_head *logged_list,
4062 struct btrfs_log_ctx *ctx)
4063{
4064 struct btrfs_root *log = root->log_root;
4065 struct btrfs_file_extent_item *fi;
4066 struct extent_buffer *leaf;
4067 struct btrfs_map_token token;
4068 struct btrfs_key key;
4069 u64 extent_offset = em->start - em->orig_start;
4070 u64 block_len;
4071 int ret;
4072 int extent_inserted = 0;
4073 bool ordered_io_err = false;
4074
David Sterbaf85b7372017-01-20 14:54:07 +01004075 ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
4076 logged_list, &ordered_io_err);
Filipe Manana8407f552014-09-05 15:14:39 +01004077 if (ret)
4078 return ret;
4079
4080 if (ordered_io_err) {
4081 ctx->io_err = -EIO;
Liu Boebb70442017-11-21 14:35:40 -07004082 return ctx->io_err;
Filipe Manana8407f552014-09-05 15:14:39 +01004083 }
4084
4085 btrfs_init_map_token(&token);
4086
Nikolay Borisov9d122622017-01-18 00:31:40 +02004087 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004088 em->start + em->len, NULL, 0, 1,
4089 sizeof(*fi), &extent_inserted);
4090 if (ret)
4091 return ret;
4092
4093 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004094 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004095 key.type = BTRFS_EXTENT_DATA_KEY;
4096 key.offset = em->start;
4097
4098 ret = btrfs_insert_empty_item(trans, log, path, &key,
4099 sizeof(*fi));
4100 if (ret)
4101 return ret;
4102 }
4103 leaf = path->nodes[0];
4104 fi = btrfs_item_ptr(leaf, path->slots[0],
4105 struct btrfs_file_extent_item);
4106
Josef Bacik50d9aa92014-11-21 14:52:38 -05004107 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004108 &token);
4109 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4110 btrfs_set_token_file_extent_type(leaf, fi,
4111 BTRFS_FILE_EXTENT_PREALLOC,
4112 &token);
4113 else
4114 btrfs_set_token_file_extent_type(leaf, fi,
4115 BTRFS_FILE_EXTENT_REG,
4116 &token);
4117
4118 block_len = max(em->block_len, em->orig_block_len);
4119 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4120 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4121 em->block_start,
4122 &token);
4123 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4124 &token);
4125 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4126 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4127 em->block_start -
4128 extent_offset, &token);
4129 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4130 &token);
4131 } else {
4132 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4133 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4134 &token);
4135 }
4136
4137 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4138 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4139 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4140 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4141 &token);
4142 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4143 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4144 btrfs_mark_buffer_dirty(leaf);
4145
4146 btrfs_release_path(path);
4147
4148 return ret;
4149}
4150
Josef Bacik5dc562c2012-08-17 13:14:17 -04004151static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4152 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004153 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004154 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004155 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004156 struct btrfs_log_ctx *ctx,
4157 const u64 start,
4158 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004159{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004160 struct extent_map *em, *n;
4161 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004162 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004163 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004164 u64 test_gen;
4165 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004166 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004167
4168 INIT_LIST_HEAD(&extents);
4169
Nikolay Borisov9d122622017-01-18 00:31:40 +02004170 down_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004171 write_lock(&tree->lock);
4172 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004173 logged_start = start;
4174 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004175
4176 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4177 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004178 /*
4179 * Just an arbitrary number, this can be really CPU intensive
4180 * once we start getting a lot of extents, and really once we
4181 * have a bunch of extents we just want to commit since it will
4182 * be faster.
4183 */
4184 if (++num > 32768) {
4185 list_del_init(&tree->modified_extents);
4186 ret = -EFBIG;
4187 goto process;
4188 }
4189
Josef Bacik5dc562c2012-08-17 13:14:17 -04004190 if (em->generation <= test_gen)
4191 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004192
4193 if (em->start < logged_start)
4194 logged_start = em->start;
4195 if ((em->start + em->len - 1) > logged_end)
4196 logged_end = em->start + em->len - 1;
4197
Josef Bacikff44c6e2012-09-14 12:59:20 -04004198 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004199 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004200 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004201 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004202 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004203 }
4204
4205 list_sort(NULL, &extents, extent_cmp);
Josef Bacik8c6c5922017-08-29 10:11:39 -04004206 btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004207 /*
4208 * Some ordered extents started by fsync might have completed
4209 * before we could collect them into the list logged_list, which
4210 * means they're gone, not in our logged_list nor in the inode's
4211 * ordered tree. We want the application/user space to know an
4212 * error happened while attempting to persist file data so that
4213 * it can take proper action. If such error happened, we leave
4214 * without writing to the log tree and the fsync must report the
4215 * file data write error and not commit the current transaction.
4216 */
Nikolay Borisov9d122622017-01-18 00:31:40 +02004217 ret = filemap_check_errors(inode->vfs_inode.i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004218 if (ret)
4219 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004220process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004221 while (!list_empty(&extents)) {
4222 em = list_entry(extents.next, struct extent_map, list);
4223
4224 list_del_init(&em->list);
4225
4226 /*
4227 * If we had an error we just need to delete everybody from our
4228 * private list.
4229 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004230 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004231 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004232 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004233 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004234 }
4235
4236 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004237
Filipe Manana8407f552014-09-05 15:14:39 +01004238 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4239 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004240 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004241 clear_em_logging(tree, em);
4242 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004243 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004244 WARN_ON(!list_empty(&extents));
4245 write_unlock(&tree->lock);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004246 up_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004247
Josef Bacik5dc562c2012-08-17 13:14:17 -04004248 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004249 return ret;
4250}
4251
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004252static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004253 struct btrfs_path *path, u64 *size_ret)
4254{
4255 struct btrfs_key key;
4256 int ret;
4257
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004258 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004259 key.type = BTRFS_INODE_ITEM_KEY;
4260 key.offset = 0;
4261
4262 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4263 if (ret < 0) {
4264 return ret;
4265 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004266 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004267 } else {
4268 struct btrfs_inode_item *item;
4269
4270 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4271 struct btrfs_inode_item);
4272 *size_ret = btrfs_inode_size(path->nodes[0], item);
4273 }
4274
4275 btrfs_release_path(path);
4276 return 0;
4277}
4278
Filipe Manana36283bf2015-06-20 00:44:51 +01004279/*
4280 * At the moment we always log all xattrs. This is to figure out at log replay
4281 * time which xattrs must have their deletion replayed. If a xattr is missing
4282 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4283 * because if a xattr is deleted, the inode is fsynced and a power failure
4284 * happens, causing the log to be replayed the next time the fs is mounted,
4285 * we want the xattr to not exist anymore (same behaviour as other filesystems
4286 * with a journal, ext3/4, xfs, f2fs, etc).
4287 */
4288static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4289 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004290 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004291 struct btrfs_path *path,
4292 struct btrfs_path *dst_path)
4293{
4294 int ret;
4295 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004296 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004297 int ins_nr = 0;
4298 int start_slot = 0;
4299
4300 key.objectid = ino;
4301 key.type = BTRFS_XATTR_ITEM_KEY;
4302 key.offset = 0;
4303
4304 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4305 if (ret < 0)
4306 return ret;
4307
4308 while (true) {
4309 int slot = path->slots[0];
4310 struct extent_buffer *leaf = path->nodes[0];
4311 int nritems = btrfs_header_nritems(leaf);
4312
4313 if (slot >= nritems) {
4314 if (ins_nr > 0) {
4315 u64 last_extent = 0;
4316
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004317 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004318 &last_extent, start_slot,
4319 ins_nr, 1, 0);
4320 /* can't be 1, extent items aren't processed */
4321 ASSERT(ret <= 0);
4322 if (ret < 0)
4323 return ret;
4324 ins_nr = 0;
4325 }
4326 ret = btrfs_next_leaf(root, path);
4327 if (ret < 0)
4328 return ret;
4329 else if (ret > 0)
4330 break;
4331 continue;
4332 }
4333
4334 btrfs_item_key_to_cpu(leaf, &key, slot);
4335 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4336 break;
4337
4338 if (ins_nr == 0)
4339 start_slot = slot;
4340 ins_nr++;
4341 path->slots[0]++;
4342 cond_resched();
4343 }
4344 if (ins_nr > 0) {
4345 u64 last_extent = 0;
4346
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004347 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004348 &last_extent, start_slot,
4349 ins_nr, 1, 0);
4350 /* can't be 1, extent items aren't processed */
4351 ASSERT(ret <= 0);
4352 if (ret < 0)
4353 return ret;
4354 }
4355
4356 return 0;
4357}
4358
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004359/*
4360 * If the no holes feature is enabled we need to make sure any hole between the
4361 * last extent and the i_size of our inode is explicitly marked in the log. This
4362 * is to make sure that doing something like:
4363 *
4364 * 1) create file with 128Kb of data
4365 * 2) truncate file to 64Kb
4366 * 3) truncate file to 256Kb
4367 * 4) fsync file
4368 * 5) <crash/power failure>
4369 * 6) mount fs and trigger log replay
4370 *
4371 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4372 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4373 * file correspond to a hole. The presence of explicit holes in a log tree is
4374 * what guarantees that log replay will remove/adjust file extent items in the
4375 * fs/subvol tree.
4376 *
4377 * Here we do not need to care about holes between extents, that is already done
4378 * by copy_items(). We also only need to do this in the full sync path, where we
4379 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4380 * lookup the list of modified extent maps and if any represents a hole, we
4381 * insert a corresponding extent representing a hole in the log tree.
4382 */
4383static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4384 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004385 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004386 struct btrfs_path *path)
4387{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004388 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004389 int ret;
4390 struct btrfs_key key;
4391 u64 hole_start;
4392 u64 hole_size;
4393 struct extent_buffer *leaf;
4394 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004395 const u64 ino = btrfs_ino(inode);
4396 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004397
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004398 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004399 return 0;
4400
4401 key.objectid = ino;
4402 key.type = BTRFS_EXTENT_DATA_KEY;
4403 key.offset = (u64)-1;
4404
4405 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4406 ASSERT(ret != 0);
4407 if (ret < 0)
4408 return ret;
4409
4410 ASSERT(path->slots[0] > 0);
4411 path->slots[0]--;
4412 leaf = path->nodes[0];
4413 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4414
4415 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4416 /* inode does not have any extents */
4417 hole_start = 0;
4418 hole_size = i_size;
4419 } else {
4420 struct btrfs_file_extent_item *extent;
4421 u64 len;
4422
4423 /*
4424 * If there's an extent beyond i_size, an explicit hole was
4425 * already inserted by copy_items().
4426 */
4427 if (key.offset >= i_size)
4428 return 0;
4429
4430 extent = btrfs_item_ptr(leaf, path->slots[0],
4431 struct btrfs_file_extent_item);
4432
4433 if (btrfs_file_extent_type(leaf, extent) ==
4434 BTRFS_FILE_EXTENT_INLINE) {
4435 len = btrfs_file_extent_inline_len(leaf,
4436 path->slots[0],
4437 extent);
Filipe Manana6399fb52017-07-28 15:22:36 +01004438 ASSERT(len == i_size ||
4439 (len == fs_info->sectorsize &&
4440 btrfs_file_extent_compression(leaf, extent) !=
4441 BTRFS_COMPRESS_NONE));
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004442 return 0;
4443 }
4444
4445 len = btrfs_file_extent_num_bytes(leaf, extent);
4446 /* Last extent goes beyond i_size, no need to log a hole. */
4447 if (key.offset + len > i_size)
4448 return 0;
4449 hole_start = key.offset + len;
4450 hole_size = i_size - hole_start;
4451 }
4452 btrfs_release_path(path);
4453
4454 /* Last extent ends at i_size. */
4455 if (hole_size == 0)
4456 return 0;
4457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004458 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004459 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4460 hole_size, 0, hole_size, 0, 0, 0);
4461 return ret;
4462}
4463
Filipe Manana56f23fd2016-03-30 23:37:21 +01004464/*
4465 * When we are logging a new inode X, check if it doesn't have a reference that
4466 * matches the reference from some other inode Y created in a past transaction
4467 * and that was renamed in the current transaction. If we don't do this, then at
4468 * log replay time we can lose inode Y (and all its files if it's a directory):
4469 *
4470 * mkdir /mnt/x
4471 * echo "hello world" > /mnt/x/foobar
4472 * sync
4473 * mv /mnt/x /mnt/y
4474 * mkdir /mnt/x # or touch /mnt/x
4475 * xfs_io -c fsync /mnt/x
4476 * <power fail>
4477 * mount fs, trigger log replay
4478 *
4479 * After the log replay procedure, we would lose the first directory and all its
4480 * files (file foobar).
4481 * For the case where inode Y is not a directory we simply end up losing it:
4482 *
4483 * echo "123" > /mnt/foo
4484 * sync
4485 * mv /mnt/foo /mnt/bar
4486 * echo "abc" > /mnt/foo
4487 * xfs_io -c fsync /mnt/foo
4488 * <power fail>
4489 *
4490 * We also need this for cases where a snapshot entry is replaced by some other
4491 * entry (file or directory) otherwise we end up with an unreplayable log due to
4492 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4493 * if it were a regular entry:
4494 *
4495 * mkdir /mnt/x
4496 * btrfs subvolume snapshot /mnt /mnt/x/snap
4497 * btrfs subvolume delete /mnt/x/snap
4498 * rmdir /mnt/x
4499 * mkdir /mnt/x
4500 * fsync /mnt/x or fsync some new file inside it
4501 * <power fail>
4502 *
4503 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4504 * the same transaction.
4505 */
4506static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4507 const int slot,
4508 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004509 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004510 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004511{
4512 int ret;
4513 struct btrfs_path *search_path;
4514 char *name = NULL;
4515 u32 name_len = 0;
4516 u32 item_size = btrfs_item_size_nr(eb, slot);
4517 u32 cur_offset = 0;
4518 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4519
4520 search_path = btrfs_alloc_path();
4521 if (!search_path)
4522 return -ENOMEM;
4523 search_path->search_commit_root = 1;
4524 search_path->skip_locking = 1;
4525
4526 while (cur_offset < item_size) {
4527 u64 parent;
4528 u32 this_name_len;
4529 u32 this_len;
4530 unsigned long name_ptr;
4531 struct btrfs_dir_item *di;
4532
4533 if (key->type == BTRFS_INODE_REF_KEY) {
4534 struct btrfs_inode_ref *iref;
4535
4536 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4537 parent = key->offset;
4538 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4539 name_ptr = (unsigned long)(iref + 1);
4540 this_len = sizeof(*iref) + this_name_len;
4541 } else {
4542 struct btrfs_inode_extref *extref;
4543
4544 extref = (struct btrfs_inode_extref *)(ptr +
4545 cur_offset);
4546 parent = btrfs_inode_extref_parent(eb, extref);
4547 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4548 name_ptr = (unsigned long)&extref->name;
4549 this_len = sizeof(*extref) + this_name_len;
4550 }
4551
4552 if (this_name_len > name_len) {
4553 char *new_name;
4554
4555 new_name = krealloc(name, this_name_len, GFP_NOFS);
4556 if (!new_name) {
4557 ret = -ENOMEM;
4558 goto out;
4559 }
4560 name_len = this_name_len;
4561 name = new_name;
4562 }
4563
4564 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004565 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4566 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004567 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004568 struct btrfs_key di_key;
4569
4570 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4571 di, &di_key);
4572 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4573 ret = 1;
4574 *other_ino = di_key.objectid;
4575 } else {
4576 ret = -EAGAIN;
4577 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004578 goto out;
4579 } else if (IS_ERR(di)) {
4580 ret = PTR_ERR(di);
4581 goto out;
4582 }
4583 btrfs_release_path(search_path);
4584
4585 cur_offset += this_len;
4586 }
4587 ret = 0;
4588out:
4589 btrfs_free_path(search_path);
4590 kfree(name);
4591 return ret;
4592}
4593
Chris Masone02119d2008-09-05 16:13:11 -04004594/* log a single inode in the tree log.
4595 * At least one parent directory for this inode must exist in the tree
4596 * or be logged already.
4597 *
4598 * Any items from this inode changed by the current transaction are copied
4599 * to the log tree. An extra reference is taken on any extents in this
4600 * file, allowing us to avoid a whole pile of corner cases around logging
4601 * blocks that have been removed from the tree.
4602 *
4603 * See LOG_INODE_ALL and related defines for a description of what inode_only
4604 * does.
4605 *
4606 * This handles both files and directories.
4607 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004608static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004609 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004610 int inode_only,
4611 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004612 const loff_t end,
4613 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004614{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004615 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004616 struct btrfs_path *path;
4617 struct btrfs_path *dst_path;
4618 struct btrfs_key min_key;
4619 struct btrfs_key max_key;
4620 struct btrfs_root *log = root->log_root;
Miao Xie827463c2014-01-14 20:31:51 +08004621 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004622 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004623 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004624 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004625 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004626 int ins_start_slot = 0;
4627 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004628 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004629 u64 ino = btrfs_ino(inode);
4630 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004631 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004632 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004633
Chris Masone02119d2008-09-05 16:13:11 -04004634 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004635 if (!path)
4636 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004637 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004638 if (!dst_path) {
4639 btrfs_free_path(path);
4640 return -ENOMEM;
4641 }
Chris Masone02119d2008-09-05 16:13:11 -04004642
Li Zefan33345d012011-04-20 10:31:50 +08004643 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004644 min_key.type = BTRFS_INODE_ITEM_KEY;
4645 min_key.offset = 0;
4646
Li Zefan33345d012011-04-20 10:31:50 +08004647 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004648
Chris Mason12fcfd22009-03-24 10:24:20 -04004649
Josef Bacik5dc562c2012-08-17 13:14:17 -04004650 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004651 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004652 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004653 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004654 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004655 max_key.type = BTRFS_XATTR_ITEM_KEY;
4656 else
4657 max_key.type = (u8)-1;
4658 max_key.offset = (u64)-1;
4659
Filipe Manana2c2c4522015-01-13 16:40:04 +00004660 /*
4661 * Only run delayed items if we are a dir or a new file.
4662 * Otherwise commit the delayed inode only, which is needed in
4663 * order for the log replay code to mark inodes for link count
4664 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4665 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004666 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4667 inode->generation > fs_info->last_trans_committed)
4668 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004669 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004670 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004671
4672 if (ret) {
4673 btrfs_free_path(path);
4674 btrfs_free_path(dst_path);
4675 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004676 }
4677
Liu Bo781feef2016-11-30 16:20:25 -08004678 if (inode_only == LOG_OTHER_INODE) {
4679 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004680 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004681 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004682 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004683 }
Chris Masone02119d2008-09-05 16:13:11 -04004684
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004685 /*
Chris Masone02119d2008-09-05 16:13:11 -04004686 * a brute force approach to making sure we get the most uptodate
4687 * copies of everything.
4688 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004689 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004690 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4691
Filipe Manana4f764e52015-02-23 19:53:35 +00004692 if (inode_only == LOG_INODE_EXISTS)
4693 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004694 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004695 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004696 if (inode_only == LOG_INODE_EXISTS) {
4697 /*
4698 * Make sure the new inode item we write to the log has
4699 * the same isize as the current one (if it exists).
4700 * This is necessary to prevent data loss after log
4701 * replay, and also to prevent doing a wrong expanding
4702 * truncate - for e.g. create file, write 4K into offset
4703 * 0, fsync, write 4K into offset 4096, add hard link,
4704 * fsync some other file (to sync log), power fail - if
4705 * we use the inode's current i_size, after log replay
4706 * we get a 8Kb file, with the last 4Kb extent as a hole
4707 * (zeroes), as if an expanding truncate happened,
4708 * instead of getting a file of 4Kb only.
4709 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004710 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004711 if (err)
4712 goto out_unlock;
4713 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004714 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004715 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004716 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004717 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004718 ret = drop_objectid_items(trans, log, path, ino,
4719 max_key.type);
4720 } else {
4721 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004722 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00004723 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004724 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004725 while(1) {
4726 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004727 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08004728 if (ret != -EAGAIN)
4729 break;
4730 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004731 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004732 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004733 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004734 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004735 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004736 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004737 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004738 ret = drop_objectid_items(trans, log, path, ino,
4739 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004740 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004741 if (inode_only == LOG_INODE_ALL)
4742 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004743 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004744 }
Josef Bacika95249b2012-10-11 16:17:34 -04004745
Chris Masone02119d2008-09-05 16:13:11 -04004746 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004747 if (ret) {
4748 err = ret;
4749 goto out_unlock;
4750 }
Chris Masone02119d2008-09-05 16:13:11 -04004751
Chris Masond3977122009-01-05 21:25:51 -05004752 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004753 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004754 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004755 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004756 if (ret < 0) {
4757 err = ret;
4758 goto out_unlock;
4759 }
Chris Masone02119d2008-09-05 16:13:11 -04004760 if (ret != 0)
4761 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004762again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004763 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004764 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004765 break;
4766 if (min_key.type > max_key.type)
4767 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004768
Filipe Mananae4545de2015-06-17 12:49:23 +01004769 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4770 need_log_inode_item = false;
4771
Filipe Manana56f23fd2016-03-30 23:37:21 +01004772 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4773 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02004774 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004775 u64 other_ino = 0;
4776
Filipe Manana56f23fd2016-03-30 23:37:21 +01004777 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02004778 path->slots[0], &min_key, inode,
4779 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004780 if (ret < 0) {
4781 err = ret;
4782 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004783 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004784 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004785 struct btrfs_key inode_key;
4786 struct inode *other_inode;
4787
4788 if (ins_nr > 0) {
4789 ins_nr++;
4790 } else {
4791 ins_nr = 1;
4792 ins_start_slot = path->slots[0];
4793 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02004794 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01004795 &last_extent, ins_start_slot,
4796 ins_nr, inode_only,
4797 logged_isize);
4798 if (ret < 0) {
4799 err = ret;
4800 goto out_unlock;
4801 }
4802 ins_nr = 0;
4803 btrfs_release_path(path);
4804 inode_key.objectid = other_ino;
4805 inode_key.type = BTRFS_INODE_ITEM_KEY;
4806 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01004808 &inode_key, root,
4809 NULL);
4810 /*
4811 * If the other inode that had a conflicting dir
4812 * entry was deleted in the current transaction,
4813 * we don't need to do more work nor fallback to
4814 * a transaction commit.
4815 */
4816 if (IS_ERR(other_inode) &&
4817 PTR_ERR(other_inode) == -ENOENT) {
4818 goto next_key;
4819 } else if (IS_ERR(other_inode)) {
4820 err = PTR_ERR(other_inode);
4821 goto out_unlock;
4822 }
4823 /*
4824 * We are safe logging the other inode without
4825 * acquiring its i_mutex as long as we log with
4826 * the LOG_INODE_EXISTS mode. We're safe against
4827 * concurrent renames of the other inode as well
4828 * because during a rename we pin the log and
4829 * update the log with the new name before we
4830 * unpin it.
4831 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004832 err = btrfs_log_inode(trans, root,
4833 BTRFS_I(other_inode),
4834 LOG_OTHER_INODE, 0, LLONG_MAX,
4835 ctx);
Filipe Manana44f714d2016-06-06 16:11:13 +01004836 iput(other_inode);
4837 if (err)
4838 goto out_unlock;
4839 else
4840 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004841 }
4842 }
4843
Filipe Manana36283bf2015-06-20 00:44:51 +01004844 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4845 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4846 if (ins_nr == 0)
4847 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004848 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004849 &last_extent, ins_start_slot,
4850 ins_nr, inode_only, logged_isize);
4851 if (ret < 0) {
4852 err = ret;
4853 goto out_unlock;
4854 }
4855 ins_nr = 0;
4856 if (ret) {
4857 btrfs_release_path(path);
4858 continue;
4859 }
4860 goto next_slot;
4861 }
4862
Chris Mason31ff1cd2008-09-11 16:17:57 -04004863 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4864 ins_nr++;
4865 goto next_slot;
4866 } else if (!ins_nr) {
4867 ins_start_slot = path->slots[0];
4868 ins_nr = 1;
4869 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004870 }
4871
Nikolay Borisova59108a2017-01-18 00:31:48 +02004872 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004873 ins_start_slot, ins_nr, inode_only,
4874 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004875 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004876 err = ret;
4877 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004878 }
4879 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004880 ins_nr = 0;
4881 btrfs_release_path(path);
4882 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004883 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004884 ins_nr = 1;
4885 ins_start_slot = path->slots[0];
4886next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004887
Chris Mason3a5f1d42008-09-11 15:53:37 -04004888 nritems = btrfs_header_nritems(path->nodes[0]);
4889 path->slots[0]++;
4890 if (path->slots[0] < nritems) {
4891 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4892 path->slots[0]);
4893 goto again;
4894 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004895 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004896 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04004897 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004898 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004899 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004900 err = ret;
4901 goto out_unlock;
4902 }
Josef Bacik16e75492013-10-22 12:18:51 -04004903 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004904 ins_nr = 0;
4905 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004906 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004907next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004908 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004909 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004910 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004911 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004912 min_key.offset = 0;
4913 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004914 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004915 }
Chris Masone02119d2008-09-05 16:13:11 -04004916 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004917 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004918 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004919 ins_start_slot, ins_nr, inode_only,
4920 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004921 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004922 err = ret;
4923 goto out_unlock;
4924 }
Josef Bacik16e75492013-10-22 12:18:51 -04004925 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004926 ins_nr = 0;
4927 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004928
Filipe Manana36283bf2015-06-20 00:44:51 +01004929 btrfs_release_path(path);
4930 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02004931 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01004932 if (err)
4933 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004934 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4935 btrfs_release_path(path);
4936 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02004937 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004938 if (err)
4939 goto out_unlock;
4940 }
Josef Bacika95249b2012-10-11 16:17:34 -04004941log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004942 btrfs_release_path(path);
4943 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004944 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004945 err = log_inode_item(trans, log, dst_path, inode);
Filipe Mananae4545de2015-06-17 12:49:23 +01004946 if (err)
4947 goto out_unlock;
4948 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004949 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004950 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004951 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004952 if (ret) {
4953 err = ret;
4954 goto out_unlock;
4955 }
Josef Bacikd006a042013-11-12 20:54:09 -05004956 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004957 struct extent_map *em, *n;
4958
Filipe Manana49dae1b2014-09-06 22:34:39 +01004959 write_lock(&em_tree->lock);
4960 /*
4961 * We can't just remove every em if we're called for a ranged
4962 * fsync - that is, one that doesn't cover the whole possible
4963 * file range (0 to LLONG_MAX). This is because we can have
4964 * em's that fall outside the range we're logging and therefore
4965 * their ordered operations haven't completed yet
4966 * (btrfs_finish_ordered_io() not invoked yet). This means we
4967 * didn't get their respective file extent item in the fs/subvol
4968 * tree yet, and need to let the next fast fsync (one which
4969 * consults the list of modified extent maps) find the em so
4970 * that it logs a matching file extent item and waits for the
4971 * respective ordered operation to complete (if it's still
4972 * running).
4973 *
4974 * Removing every em outside the range we're logging would make
4975 * the next fast fsync not log their matching file extent items,
4976 * therefore making us lose data after a log replay.
4977 */
4978 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4979 list) {
4980 const u64 mod_end = em->mod_start + em->mod_len - 1;
4981
4982 if (em->mod_start >= start && mod_end <= end)
4983 list_del_init(&em->list);
4984 }
4985 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004986 }
4987
Nikolay Borisova59108a2017-01-18 00:31:48 +02004988 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
4989 ret = log_directory_changes(trans, root, inode, path, dst_path,
4990 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004991 if (ret) {
4992 err = ret;
4993 goto out_unlock;
4994 }
Chris Masone02119d2008-09-05 16:13:11 -04004995 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004996
Nikolay Borisova59108a2017-01-18 00:31:48 +02004997 spin_lock(&inode->lock);
4998 inode->logged_trans = trans->transid;
4999 inode->last_log_commit = inode->last_sub_trans;
5000 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005001out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005002 if (unlikely(err))
5003 btrfs_put_logged_extents(&logged_list);
5004 else
5005 btrfs_submit_logged_extents(&logged_list, log);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005006 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005007
5008 btrfs_free_path(path);
5009 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005010 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005011}
5012
Chris Mason12fcfd22009-03-24 10:24:20 -04005013/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005014 * Check if we must fallback to a transaction commit when logging an inode.
5015 * This must be called after logging the inode and is used only in the context
5016 * when fsyncing an inode requires the need to log some other inode - in which
5017 * case we can't lock the i_mutex of each other inode we need to log as that
5018 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5019 * log inodes up or down in the hierarchy) or rename operations for example. So
5020 * we take the log_mutex of the inode after we have logged it and then check for
5021 * its last_unlink_trans value - this is safe because any task setting
5022 * last_unlink_trans must take the log_mutex and it must do this before it does
5023 * the actual unlink operation, so if we do this check before a concurrent task
5024 * sets last_unlink_trans it means we've logged a consistent version/state of
5025 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005026 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005027 * we logged the inode or it might have also done the unlink).
5028 */
5029static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005030 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005031{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005032 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005033 bool ret = false;
5034
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005035 mutex_lock(&inode->log_mutex);
5036 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005037 /*
5038 * Make sure any commits to the log are forced to be full
5039 * commits.
5040 */
5041 btrfs_set_log_full_commit(fs_info, trans);
5042 ret = true;
5043 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005044 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005045
5046 return ret;
5047}
5048
5049/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005050 * follow the dentry parent pointers up the chain and see if any
5051 * of the directories in it require a full commit before they can
5052 * be logged. Returns zero if nothing special needs to be done or 1 if
5053 * a full commit is required.
5054 */
5055static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005056 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005057 struct dentry *parent,
5058 struct super_block *sb,
5059 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005060{
Chris Mason12fcfd22009-03-24 10:24:20 -04005061 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005062 struct dentry *old_parent = NULL;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005063 struct btrfs_inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005064
Chris Masonaf4176b2009-03-24 10:24:31 -04005065 /*
5066 * for regular files, if its inode is already on disk, we don't
5067 * have to worry about the parents at all. This is because
5068 * we can use the last_unlink_trans field to record renames
5069 * and other fun in this file.
5070 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005071 if (S_ISREG(inode->vfs_inode.i_mode) &&
5072 inode->generation <= last_committed &&
5073 inode->last_unlink_trans <= last_committed)
5074 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005075
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005076 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005077 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005078 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005079 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005080 }
5081
5082 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005083 /*
5084 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005085 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005086 * logged_trans so that further down in the log code we don't
5087 * think this inode has already been logged.
5088 */
5089 if (inode != orig_inode)
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005090 inode->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005091 smp_mb();
5092
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005093 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005094 ret = 1;
5095 break;
5096 }
5097
Al Virofc640052016-04-10 01:33:30 -04005098 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005099 break;
5100
Filipe Manana44f714d2016-06-06 16:11:13 +01005101 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005102 inode = BTRFS_I(d_inode(parent));
5103 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005104 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005105 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005106 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005107
Josef Bacik6a912212010-11-20 09:48:00 +00005108 parent = dget_parent(parent);
5109 dput(old_parent);
5110 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005111 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005112
5113 }
Josef Bacik6a912212010-11-20 09:48:00 +00005114 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005115out:
Chris Masone02119d2008-09-05 16:13:11 -04005116 return ret;
5117}
5118
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005119struct btrfs_dir_list {
5120 u64 ino;
5121 struct list_head list;
5122};
5123
5124/*
5125 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5126 * details about the why it is needed.
5127 * This is a recursive operation - if an existing dentry corresponds to a
5128 * directory, that directory's new entries are logged too (same behaviour as
5129 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5130 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5131 * complains about the following circular lock dependency / possible deadlock:
5132 *
5133 * CPU0 CPU1
5134 * ---- ----
5135 * lock(&type->i_mutex_dir_key#3/2);
5136 * lock(sb_internal#2);
5137 * lock(&type->i_mutex_dir_key#3/2);
5138 * lock(&sb->s_type->i_mutex_key#14);
5139 *
5140 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5141 * sb_start_intwrite() in btrfs_start_transaction().
5142 * Not locking i_mutex of the inodes is still safe because:
5143 *
5144 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5145 * that while logging the inode new references (names) are added or removed
5146 * from the inode, leaving the logged inode item with a link count that does
5147 * not match the number of logged inode reference items. This is fine because
5148 * at log replay time we compute the real number of links and correct the
5149 * link count in the inode item (see replay_one_buffer() and
5150 * link_to_fixup_dir());
5151 *
5152 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5153 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5154 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5155 * has a size that doesn't match the sum of the lengths of all the logged
5156 * names. This does not result in a problem because if a dir_item key is
5157 * logged but its matching dir_index key is not logged, at log replay time we
5158 * don't use it to replay the respective name (see replay_one_name()). On the
5159 * other hand if only the dir_index key ends up being logged, the respective
5160 * name is added to the fs/subvol tree with both the dir_item and dir_index
5161 * keys created (see replay_one_name()).
5162 * The directory's inode item with a wrong i_size is not a problem as well,
5163 * since we don't use it at log replay time to set the i_size in the inode
5164 * item of the fs/subvol tree (see overwrite_item()).
5165 */
5166static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5167 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005168 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005169 struct btrfs_log_ctx *ctx)
5170{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005171 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005172 struct btrfs_root *log = root->log_root;
5173 struct btrfs_path *path;
5174 LIST_HEAD(dir_list);
5175 struct btrfs_dir_list *dir_elem;
5176 int ret = 0;
5177
5178 path = btrfs_alloc_path();
5179 if (!path)
5180 return -ENOMEM;
5181
5182 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5183 if (!dir_elem) {
5184 btrfs_free_path(path);
5185 return -ENOMEM;
5186 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005187 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005188 list_add_tail(&dir_elem->list, &dir_list);
5189
5190 while (!list_empty(&dir_list)) {
5191 struct extent_buffer *leaf;
5192 struct btrfs_key min_key;
5193 int nritems;
5194 int i;
5195
5196 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5197 list);
5198 if (ret)
5199 goto next_dir_inode;
5200
5201 min_key.objectid = dir_elem->ino;
5202 min_key.type = BTRFS_DIR_ITEM_KEY;
5203 min_key.offset = 0;
5204again:
5205 btrfs_release_path(path);
5206 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5207 if (ret < 0) {
5208 goto next_dir_inode;
5209 } else if (ret > 0) {
5210 ret = 0;
5211 goto next_dir_inode;
5212 }
5213
5214process_leaf:
5215 leaf = path->nodes[0];
5216 nritems = btrfs_header_nritems(leaf);
5217 for (i = path->slots[0]; i < nritems; i++) {
5218 struct btrfs_dir_item *di;
5219 struct btrfs_key di_key;
5220 struct inode *di_inode;
5221 struct btrfs_dir_list *new_dir_elem;
5222 int log_mode = LOG_INODE_EXISTS;
5223 int type;
5224
5225 btrfs_item_key_to_cpu(leaf, &min_key, i);
5226 if (min_key.objectid != dir_elem->ino ||
5227 min_key.type != BTRFS_DIR_ITEM_KEY)
5228 goto next_dir_inode;
5229
5230 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5231 type = btrfs_dir_type(leaf, di);
5232 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5233 type != BTRFS_FT_DIR)
5234 continue;
5235 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5236 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5237 continue;
5238
Robbie Koec125cf2016-10-28 10:48:26 +08005239 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005240 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005241 if (IS_ERR(di_inode)) {
5242 ret = PTR_ERR(di_inode);
5243 goto next_dir_inode;
5244 }
5245
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005246 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005247 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005248 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005249 }
5250
5251 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005252 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005253 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005254 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005255 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005256 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005257 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005258 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005259 iput(di_inode);
5260 if (ret)
5261 goto next_dir_inode;
5262 if (ctx->log_new_dentries) {
5263 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5264 GFP_NOFS);
5265 if (!new_dir_elem) {
5266 ret = -ENOMEM;
5267 goto next_dir_inode;
5268 }
5269 new_dir_elem->ino = di_key.objectid;
5270 list_add_tail(&new_dir_elem->list, &dir_list);
5271 }
5272 break;
5273 }
5274 if (i == nritems) {
5275 ret = btrfs_next_leaf(log, path);
5276 if (ret < 0) {
5277 goto next_dir_inode;
5278 } else if (ret > 0) {
5279 ret = 0;
5280 goto next_dir_inode;
5281 }
5282 goto process_leaf;
5283 }
5284 if (min_key.offset < (u64)-1) {
5285 min_key.offset++;
5286 goto again;
5287 }
5288next_dir_inode:
5289 list_del(&dir_elem->list);
5290 kfree(dir_elem);
5291 }
5292
5293 btrfs_free_path(path);
5294 return ret;
5295}
5296
Filipe Manana18aa0922015-08-05 16:49:08 +01005297static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005298 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005299 struct btrfs_log_ctx *ctx)
5300{
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005301 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Filipe Manana18aa0922015-08-05 16:49:08 +01005302 int ret;
5303 struct btrfs_path *path;
5304 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005305 struct btrfs_root *root = inode->root;
5306 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005307
5308 path = btrfs_alloc_path();
5309 if (!path)
5310 return -ENOMEM;
5311 path->skip_locking = 1;
5312 path->search_commit_root = 1;
5313
5314 key.objectid = ino;
5315 key.type = BTRFS_INODE_REF_KEY;
5316 key.offset = 0;
5317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5318 if (ret < 0)
5319 goto out;
5320
5321 while (true) {
5322 struct extent_buffer *leaf = path->nodes[0];
5323 int slot = path->slots[0];
5324 u32 cur_offset = 0;
5325 u32 item_size;
5326 unsigned long ptr;
5327
5328 if (slot >= btrfs_header_nritems(leaf)) {
5329 ret = btrfs_next_leaf(root, path);
5330 if (ret < 0)
5331 goto out;
5332 else if (ret > 0)
5333 break;
5334 continue;
5335 }
5336
5337 btrfs_item_key_to_cpu(leaf, &key, slot);
5338 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5339 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5340 break;
5341
5342 item_size = btrfs_item_size_nr(leaf, slot);
5343 ptr = btrfs_item_ptr_offset(leaf, slot);
5344 while (cur_offset < item_size) {
5345 struct btrfs_key inode_key;
5346 struct inode *dir_inode;
5347
5348 inode_key.type = BTRFS_INODE_ITEM_KEY;
5349 inode_key.offset = 0;
5350
5351 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5352 struct btrfs_inode_extref *extref;
5353
5354 extref = (struct btrfs_inode_extref *)
5355 (ptr + cur_offset);
5356 inode_key.objectid = btrfs_inode_extref_parent(
5357 leaf, extref);
5358 cur_offset += sizeof(*extref);
5359 cur_offset += btrfs_inode_extref_name_len(leaf,
5360 extref);
5361 } else {
5362 inode_key.objectid = key.offset;
5363 cur_offset = item_size;
5364 }
5365
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005366 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005367 root, NULL);
5368 /* If parent inode was deleted, skip it. */
5369 if (IS_ERR(dir_inode))
5370 continue;
5371
Filipe Manana657ed1a2016-04-06 17:11:56 +01005372 if (ctx)
5373 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005374 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005375 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005376 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005377 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005378 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005379 if (!ret && ctx && ctx->log_new_dentries)
5380 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005381 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005382 iput(dir_inode);
5383 if (ret)
5384 goto out;
5385 }
5386 path->slots[0]++;
5387 }
5388 ret = 0;
5389out:
5390 btrfs_free_path(path);
5391 return ret;
5392}
5393
Chris Masone02119d2008-09-05 16:13:11 -04005394/*
5395 * helper function around btrfs_log_inode to make sure newly created
5396 * parent directories also end up in the log. A minimal inode and backref
5397 * only logging is done of any parent directories that are older than
5398 * the last committed transaction
5399 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005400static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005401 struct btrfs_root *root,
5402 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005403 struct dentry *parent,
5404 const loff_t start,
5405 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005406 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005407 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005410 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005411 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005412 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005413 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005414 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005415 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005416
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005417 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005418
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005419 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005420 ret = 1;
5421 goto end_no_trans;
5422 }
5423
Miao Xie995946d2014-04-02 19:51:06 +08005424 /*
5425 * The prev transaction commit doesn't complete, we need do
5426 * full commit by ourselves.
5427 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005428 if (fs_info->last_trans_log_full_commit >
5429 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005430 ret = 1;
5431 goto end_no_trans;
5432 }
5433
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005434 if (root != inode->root || btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005435 ret = 1;
5436 goto end_no_trans;
5437 }
5438
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005439 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5440 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005441 if (ret)
5442 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005443
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005444 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005445 ret = BTRFS_NO_LOG_SYNC;
5446 goto end_no_trans;
5447 }
5448
Miao Xie8b050d32014-02-20 18:08:58 +08005449 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005450 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005451 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005452
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005453 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005454 if (ret)
5455 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005456
Chris Masonaf4176b2009-03-24 10:24:31 -04005457 /*
5458 * for regular files, if its inode is already on disk, we don't
5459 * have to worry about the parents at all. This is because
5460 * we can use the last_unlink_trans field to record renames
5461 * and other fun in this file.
5462 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005463 if (S_ISREG(inode->vfs_inode.i_mode) &&
5464 inode->generation <= last_committed &&
5465 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005466 ret = 0;
5467 goto end_trans;
5468 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005469
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005470 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005471 log_dentries = true;
5472
Filipe Manana18aa0922015-08-05 16:49:08 +01005473 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005474 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005475 * inodes are fully logged. This is to prevent leaving dangling
5476 * directory index entries in directories that were our parents but are
5477 * not anymore. Not doing this results in old parent directory being
5478 * impossible to delete after log replay (rmdir will always fail with
5479 * error -ENOTEMPTY).
5480 *
5481 * Example 1:
5482 *
5483 * mkdir testdir
5484 * touch testdir/foo
5485 * ln testdir/foo testdir/bar
5486 * sync
5487 * unlink testdir/bar
5488 * xfs_io -c fsync testdir/foo
5489 * <power failure>
5490 * mount fs, triggers log replay
5491 *
5492 * If we don't log the parent directory (testdir), after log replay the
5493 * directory still has an entry pointing to the file inode using the bar
5494 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5495 * the file inode has a link count of 1.
5496 *
5497 * Example 2:
5498 *
5499 * mkdir testdir
5500 * touch foo
5501 * ln foo testdir/foo2
5502 * ln foo testdir/foo3
5503 * sync
5504 * unlink testdir/foo3
5505 * xfs_io -c fsync foo
5506 * <power failure>
5507 * mount fs, triggers log replay
5508 *
5509 * Similar as the first example, after log replay the parent directory
5510 * testdir still has an entry pointing to the inode file with name foo3
5511 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5512 * and has a link count of 2.
5513 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005514 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005515 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5516 if (ret)
5517 goto end_trans;
5518 }
5519
Chris Masond3977122009-01-05 21:25:51 -05005520 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005521 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005522 break;
5523
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005524 inode = BTRFS_I(d_inode(parent));
5525 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005526 break;
5527
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005528 if (inode->generation > last_committed) {
5529 ret = btrfs_log_inode(trans, root, inode,
5530 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005531 if (ret)
5532 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005533 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005534 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005535 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005536
Josef Bacik6a912212010-11-20 09:48:00 +00005537 parent = dget_parent(parent);
5538 dput(old_parent);
5539 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005540 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005541 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005542 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005543 else
5544 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005545end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005546 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005547 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005548 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005549 ret = 1;
5550 }
Miao Xie8b050d32014-02-20 18:08:58 +08005551
5552 if (ret)
5553 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005554 btrfs_end_log_trans(root);
5555end_no_trans:
5556 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005557}
5558
5559/*
5560 * it is not safe to log dentry if the chunk root has added new
5561 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5562 * If this returns 1, you must commit the transaction to safely get your
5563 * data on disk.
5564 */
5565int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005566 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005567 const loff_t start,
5568 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005569 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005570{
Josef Bacik6a912212010-11-20 09:48:00 +00005571 struct dentry *parent = dget_parent(dentry);
5572 int ret;
5573
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005574 ret = btrfs_log_inode_parent(trans, root, BTRFS_I(d_inode(dentry)),
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005575 parent, start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005576 dput(parent);
5577
5578 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005579}
5580
5581/*
5582 * should be called during mount to recover any replay any log trees
5583 * from the FS
5584 */
5585int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5586{
5587 int ret;
5588 struct btrfs_path *path;
5589 struct btrfs_trans_handle *trans;
5590 struct btrfs_key key;
5591 struct btrfs_key found_key;
5592 struct btrfs_key tmp_key;
5593 struct btrfs_root *log;
5594 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5595 struct walk_control wc = {
5596 .process_func = process_one_buffer,
5597 .stage = 0,
5598 };
5599
Chris Masone02119d2008-09-05 16:13:11 -04005600 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005601 if (!path)
5602 return -ENOMEM;
5603
Josef Bacikafcdd122016-09-02 15:40:02 -04005604 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005605
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005606 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005607 if (IS_ERR(trans)) {
5608 ret = PTR_ERR(trans);
5609 goto error;
5610 }
Chris Masone02119d2008-09-05 16:13:11 -04005611
5612 wc.trans = trans;
5613 wc.pin = 1;
5614
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005615 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005616 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005617 btrfs_handle_fs_error(fs_info, ret,
5618 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005619 goto error;
5620 }
Chris Masone02119d2008-09-05 16:13:11 -04005621
5622again:
5623 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5624 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005625 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005626
Chris Masond3977122009-01-05 21:25:51 -05005627 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005628 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005629
5630 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005631 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005632 "Couldn't find tree log root.");
5633 goto error;
5634 }
Chris Masone02119d2008-09-05 16:13:11 -04005635 if (ret > 0) {
5636 if (path->slots[0] == 0)
5637 break;
5638 path->slots[0]--;
5639 }
5640 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5641 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005642 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005643 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5644 break;
5645
Miao Xiecb517ea2013-05-15 07:48:19 +00005646 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005647 if (IS_ERR(log)) {
5648 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005649 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005650 "Couldn't read tree log root.");
5651 goto error;
5652 }
Chris Masone02119d2008-09-05 16:13:11 -04005653
5654 tmp_key.objectid = found_key.offset;
5655 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5656 tmp_key.offset = (u64)-1;
5657
5658 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005659 if (IS_ERR(wc.replay_dest)) {
5660 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005661 free_extent_buffer(log->node);
5662 free_extent_buffer(log->commit_root);
5663 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005664 btrfs_handle_fs_error(fs_info, ret,
5665 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005666 goto error;
5667 }
Chris Masone02119d2008-09-05 16:13:11 -04005668
Yan Zheng07d400a2009-01-06 11:42:00 -05005669 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005670 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005671 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005672
Josef Bacikb50c6e22013-04-25 15:55:30 -04005673 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005674 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5675 path);
Chris Masone02119d2008-09-05 16:13:11 -04005676 }
Chris Masone02119d2008-09-05 16:13:11 -04005677
5678 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005679 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005680 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005681 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005682 kfree(log);
5683
Josef Bacikb50c6e22013-04-25 15:55:30 -04005684 if (ret)
5685 goto error;
5686
Chris Masone02119d2008-09-05 16:13:11 -04005687 if (found_key.offset == 0)
5688 break;
5689 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005690 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005691
5692 /* step one is to pin it all, step two is to replay just inodes */
5693 if (wc.pin) {
5694 wc.pin = 0;
5695 wc.process_func = replay_one_buffer;
5696 wc.stage = LOG_WALK_REPLAY_INODES;
5697 goto again;
5698 }
5699 /* step three is to replay everything */
5700 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5701 wc.stage++;
5702 goto again;
5703 }
5704
5705 btrfs_free_path(path);
5706
Josef Bacikabefa552013-04-24 16:40:05 -04005707 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005708 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04005709 if (ret)
5710 return ret;
5711
Chris Masone02119d2008-09-05 16:13:11 -04005712 free_extent_buffer(log_root_tree->node);
5713 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005714 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005715 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005716
Josef Bacikabefa552013-04-24 16:40:05 -04005717 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005718error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005719 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005720 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005721 btrfs_free_path(path);
5722 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005723}
Chris Mason12fcfd22009-03-24 10:24:20 -04005724
5725/*
5726 * there are some corner cases where we want to force a full
5727 * commit instead of allowing a directory to be logged.
5728 *
5729 * They revolve around files there were unlinked from the directory, and
5730 * this function updates the parent directory so that a full commit is
5731 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005732 *
5733 * Must be called before the unlink operations (updates to the subvolume tree,
5734 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005735 */
5736void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005737 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005738 int for_rename)
5739{
5740 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005741 * when we're logging a file, if it hasn't been renamed
5742 * or unlinked, and its inode is fully committed on disk,
5743 * we don't have to worry about walking up the directory chain
5744 * to log its parents.
5745 *
5746 * So, we use the last_unlink_trans field to put this transid
5747 * into the file. When the file is logged we check it and
5748 * don't log the parents if the file is fully on disk.
5749 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005750 mutex_lock(&inode->log_mutex);
5751 inode->last_unlink_trans = trans->transid;
5752 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005753
5754 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005755 * if this directory was already logged any new
5756 * names for this file/dir will get recorded
5757 */
5758 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005759 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005760 return;
5761
5762 /*
5763 * if the inode we're about to unlink was logged,
5764 * the log will be properly updated for any new names
5765 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005766 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005767 return;
5768
5769 /*
5770 * when renaming files across directories, if the directory
5771 * there we're unlinking from gets fsync'd later on, there's
5772 * no way to find the destination directory later and fsync it
5773 * properly. So, we have to be conservative and force commits
5774 * so the new name gets discovered.
5775 */
5776 if (for_rename)
5777 goto record;
5778
5779 /* we can safely do the unlink without any special recording */
5780 return;
5781
5782record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005783 mutex_lock(&dir->log_mutex);
5784 dir->last_unlink_trans = trans->transid;
5785 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005786}
5787
5788/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005789 * Make sure that if someone attempts to fsync the parent directory of a deleted
5790 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5791 * that after replaying the log tree of the parent directory's root we will not
5792 * see the snapshot anymore and at log replay time we will not see any log tree
5793 * corresponding to the deleted snapshot's root, which could lead to replaying
5794 * it after replaying the log tree of the parent directory (which would replay
5795 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005796 *
5797 * Must be called before the actual snapshot destroy operation (updates to the
5798 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005799 */
5800void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02005801 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005802{
Nikolay Borisov43663552017-01-18 00:31:29 +02005803 mutex_lock(&dir->log_mutex);
5804 dir->last_unlink_trans = trans->transid;
5805 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005806}
5807
5808/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005809 * Call this after adding a new name for a file and it will properly
5810 * update the log to reflect the new name.
5811 *
5812 * It will return zero if all goes well, and it will return 1 if a
5813 * full transaction commit is required.
5814 */
5815int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005816 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Chris Mason12fcfd22009-03-24 10:24:20 -04005817 struct dentry *parent)
5818{
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005819 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
David Sterbaf85b7372017-01-20 14:54:07 +01005820 struct btrfs_root *root = inode->root;
Chris Mason12fcfd22009-03-24 10:24:20 -04005821
5822 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005823 * this will force the logging code to walk the dentry chain
5824 * up for the file
5825 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005826 if (S_ISREG(inode->vfs_inode.i_mode))
5827 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04005828
5829 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005830 * if this inode hasn't been logged and directory we're renaming it
5831 * from hasn't been logged, we don't need to log it
5832 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005833 if (inode->logged_trans <= fs_info->last_trans_committed &&
5834 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Chris Mason12fcfd22009-03-24 10:24:20 -04005835 return 0;
5836
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005837 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005838 LLONG_MAX, LOG_INODE_EXISTS, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005839}
5840