blob: e0478f51cf16942aa26b2b1eae03a5a6cd7ca88d [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
40
41/*
Chris Mason12fcfd22009-03-24 10:24:20 -040042 * directory trouble cases
43 *
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
48 *
49 * mkdir foo/some_dir
50 * normal commit
51 * rename foo/some_dir foo2/some_dir
52 * mkdir foo/some_dir
53 * fsync foo/some_dir/some_file
54 *
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
58 *
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
61 *
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
65 *
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
68 *
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
71 *
72 * mkdir f1/foo
73 * normal commit
74 * rm -rf f1/foo
75 * fsync(f1)
76 *
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
81 * ugly details.
82 */
83
84/*
Chris Masone02119d2008-09-05 16:13:11 -040085 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
89 *
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
92 */
93#define LOG_WALK_PIN_ONLY 0
94#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040095#define LOG_WALK_REPLAY_DIR_INDEX 2
96#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040097
Chris Mason12fcfd22009-03-24 10:24:20 -040098static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +010099 struct btrfs_root *root, struct inode *inode,
100 int inode_only,
101 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100102 const loff_t end,
103 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500104static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root,
106 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400107static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
108 struct btrfs_root *root,
109 struct btrfs_root *log,
110 struct btrfs_path *path,
111 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400112
113/*
114 * tree logging is a special write ahead log used to make sure that
115 * fsyncs and O_SYNCs can happen without doing full tree commits.
116 *
117 * Full tree commits are expensive because they require commonly
118 * modified blocks to be recowed, creating many dirty pages in the
119 * extent tree an 4x-6x higher write load than ext3.
120 *
121 * Instead of doing a tree commit on every fsync, we use the
122 * key ranges and transaction ids to find items for a given file or directory
123 * that have changed in this transaction. Those items are copied into
124 * a special tree (one per subvolume root), that tree is written to disk
125 * and then the fsync is considered complete.
126 *
127 * After a crash, items are copied out of the log-tree back into the
128 * subvolume tree. Any file data extents found are recorded in the extent
129 * allocation tree, and the log-tree freed.
130 *
131 * The log tree is read three times, once to pin down all the extents it is
132 * using in ram and once, once to create all the inodes logged in the tree
133 * and once to do all the other items.
134 */
135
136/*
Chris Masone02119d2008-09-05 16:13:11 -0400137 * start a sub transaction and setup the log tree
138 * this increments the log tree writer count to make the people
139 * syncing the tree wait for us to finish
140 */
141static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800142 struct btrfs_root *root,
143 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400144{
Zhaolei34eb2a52015-08-17 18:44:45 +0800145 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500146
147 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800148
Yan Zheng7237f182009-01-21 12:54:03 -0500149 if (root->log_root) {
Miao Xie995946d2014-04-02 19:51:06 +0800150 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800151 ret = -EAGAIN;
152 goto out;
153 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800154
Josef Bacikff782e02009-10-08 15:30:04 -0400155 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800156 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800157 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400158 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800159 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400160 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800161 } else {
162 mutex_lock(&root->fs_info->tree_log_mutex);
163 if (!root->fs_info->log_root_tree)
164 ret = btrfs_init_log_root_tree(trans, root->fs_info);
165 mutex_unlock(&root->fs_info->tree_log_mutex);
166 if (ret)
167 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400168
Chris Masone02119d2008-09-05 16:13:11 -0400169 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400170 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800171 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800172
173 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
174 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400175 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800176
Miao Xie2ecb7922012-09-06 04:04:27 -0600177 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500178 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800179 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800180 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800181 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800182 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800183 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800184
Miao Xiee87ac132014-02-20 18:08:53 +0800185out:
Yan Zheng7237f182009-01-21 12:54:03 -0500186 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800187 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400188}
189
190/*
191 * returns 0 if there was a log transaction running and we were able
192 * to join, or returns -ENOENT if there were not transactions
193 * in progress
194 */
195static int join_running_log_trans(struct btrfs_root *root)
196{
197 int ret = -ENOENT;
198
199 smp_mb();
200 if (!root->log_root)
201 return -ENOENT;
202
Yan Zheng7237f182009-01-21 12:54:03 -0500203 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400204 if (root->log_root) {
205 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500206 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400207 }
Yan Zheng7237f182009-01-21 12:54:03 -0500208 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400209 return ret;
210}
211
212/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400213 * This either makes the current running log transaction wait
214 * until you call btrfs_end_log_trans() or it makes any future
215 * log transactions wait until you call btrfs_end_log_trans()
216 */
217int btrfs_pin_log_trans(struct btrfs_root *root)
218{
219 int ret = -ENOENT;
220
221 mutex_lock(&root->log_mutex);
222 atomic_inc(&root->log_writers);
223 mutex_unlock(&root->log_mutex);
224 return ret;
225}
226
227/*
Chris Masone02119d2008-09-05 16:13:11 -0400228 * indicate we're done making changes to the log tree
229 * and wake up anyone waiting to do a sync
230 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100231void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400232{
Yan Zheng7237f182009-01-21 12:54:03 -0500233 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100234 /*
235 * Implicit memory barrier after atomic_dec_and_test
236 */
Yan Zheng7237f182009-01-21 12:54:03 -0500237 if (waitqueue_active(&root->log_writer_wait))
238 wake_up(&root->log_writer_wait);
239 }
Chris Masone02119d2008-09-05 16:13:11 -0400240}
241
242
243/*
244 * the walk control struct is used to pass state down the chain when
245 * processing the log tree. The stage field tells us which part
246 * of the log tree processing we are currently doing. The others
247 * are state fields used for that specific part
248 */
249struct walk_control {
250 /* should we free the extent on disk when done? This is used
251 * at transaction commit time while freeing a log tree
252 */
253 int free;
254
255 /* should we write out the extent buffer? This is used
256 * while flushing the log tree to disk during a sync
257 */
258 int write;
259
260 /* should we wait for the extent buffer io to finish? Also used
261 * while flushing the log tree to disk for a sync
262 */
263 int wait;
264
265 /* pin only walk, we record which extents on disk belong to the
266 * log trees
267 */
268 int pin;
269
270 /* what stage of the replay code we're currently in */
271 int stage;
272
273 /* the root we are currently replaying */
274 struct btrfs_root *replay_dest;
275
276 /* the trans handle for the current replay */
277 struct btrfs_trans_handle *trans;
278
279 /* the function that gets used to process blocks we find in the
280 * tree. Note the extent_buffer might not be up to date when it is
281 * passed in, and it must be checked or read if you need the data
282 * inside it
283 */
284 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
285 struct walk_control *wc, u64 gen);
286};
287
288/*
289 * process_func used to pin down extents, write them or wait on them
290 */
291static int process_one_buffer(struct btrfs_root *log,
292 struct extent_buffer *eb,
293 struct walk_control *wc, u64 gen)
294{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400295 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400296
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400297 /*
298 * If this fs is mixed then we need to be able to process the leaves to
299 * pin down any logged extents, so we have to read the block.
300 */
301 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
302 ret = btrfs_read_buffer(eb, gen);
303 if (ret)
304 return ret;
305 }
306
Josef Bacikb50c6e22013-04-25 15:55:30 -0400307 if (wc->pin)
308 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
309 eb->start, eb->len);
310
311 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400312 if (wc->pin && btrfs_header_level(eb) == 0)
313 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400314 if (wc->write)
315 btrfs_write_tree_block(eb);
316 if (wc->wait)
317 btrfs_wait_tree_block_writeback(eb);
318 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400319 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400320}
321
322/*
323 * Item overwrite used by replay and tree logging. eb, slot and key all refer
324 * to the src data we are copying out.
325 *
326 * root is the tree we are copying into, and path is a scratch
327 * path for use in this function (it should be released on entry and
328 * will be released on exit).
329 *
330 * If the key is already in the destination tree the existing item is
331 * overwritten. If the existing item isn't big enough, it is extended.
332 * If it is too large, it is truncated.
333 *
334 * If the key isn't in the destination yet, a new item is inserted.
335 */
336static noinline int overwrite_item(struct btrfs_trans_handle *trans,
337 struct btrfs_root *root,
338 struct btrfs_path *path,
339 struct extent_buffer *eb, int slot,
340 struct btrfs_key *key)
341{
342 int ret;
343 u32 item_size;
344 u64 saved_i_size = 0;
345 int save_old_i_size = 0;
346 unsigned long src_ptr;
347 unsigned long dst_ptr;
348 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000349 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400350
351 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
352 overwrite_root = 1;
353
354 item_size = btrfs_item_size_nr(eb, slot);
355 src_ptr = btrfs_item_ptr_offset(eb, slot);
356
357 /* look for the key in the destination tree */
358 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000359 if (ret < 0)
360 return ret;
361
Chris Masone02119d2008-09-05 16:13:11 -0400362 if (ret == 0) {
363 char *src_copy;
364 char *dst_copy;
365 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
366 path->slots[0]);
367 if (dst_size != item_size)
368 goto insert;
369
370 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200371 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400372 return 0;
373 }
374 dst_copy = kmalloc(item_size, GFP_NOFS);
375 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000376 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200377 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000378 kfree(dst_copy);
379 kfree(src_copy);
380 return -ENOMEM;
381 }
Chris Masone02119d2008-09-05 16:13:11 -0400382
383 read_extent_buffer(eb, src_copy, src_ptr, item_size);
384
385 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
386 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
387 item_size);
388 ret = memcmp(dst_copy, src_copy, item_size);
389
390 kfree(dst_copy);
391 kfree(src_copy);
392 /*
393 * they have the same contents, just return, this saves
394 * us from cowing blocks in the destination tree and doing
395 * extra writes that may not have been done by a previous
396 * sync
397 */
398 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200399 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400400 return 0;
401 }
402
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000403 /*
404 * We need to load the old nbytes into the inode so when we
405 * replay the extents we've logged we get the right nbytes.
406 */
407 if (inode_item) {
408 struct btrfs_inode_item *item;
409 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400410 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000411
412 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
413 struct btrfs_inode_item);
414 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
415 item = btrfs_item_ptr(eb, slot,
416 struct btrfs_inode_item);
417 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400418
419 /*
420 * If this is a directory we need to reset the i_size to
421 * 0 so that we can set it up properly when replaying
422 * the rest of the items in this log.
423 */
424 mode = btrfs_inode_mode(eb, item);
425 if (S_ISDIR(mode))
426 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000427 }
428 } else if (inode_item) {
429 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400430 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000431
432 /*
433 * New inode, set nbytes to 0 so that the nbytes comes out
434 * properly when we replay the extents.
435 */
436 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
437 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400438
439 /*
440 * If this is a directory we need to reset the i_size to 0 so
441 * that we can set it up properly when replaying the rest of
442 * the items in this log.
443 */
444 mode = btrfs_inode_mode(eb, item);
445 if (S_ISDIR(mode))
446 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400447 }
448insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200449 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400450 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000451 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400452 ret = btrfs_insert_empty_item(trans, root, path,
453 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000454 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400455
456 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000457 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400458 u32 found_size;
459 found_size = btrfs_item_size_nr(path->nodes[0],
460 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100461 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000462 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100463 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000464 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100465 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400466 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400467 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400468 }
469 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
470 path->slots[0]);
471
472 /* don't overwrite an existing inode if the generation number
473 * was logged as zero. This is done when the tree logging code
474 * is just logging an inode to make sure it exists after recovery.
475 *
476 * Also, don't overwrite i_size on directories during replay.
477 * log replay inserts and removes directory items based on the
478 * state of the tree found in the subvolume, and i_size is modified
479 * as it goes
480 */
481 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
482 struct btrfs_inode_item *src_item;
483 struct btrfs_inode_item *dst_item;
484
485 src_item = (struct btrfs_inode_item *)src_ptr;
486 dst_item = (struct btrfs_inode_item *)dst_ptr;
487
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000488 if (btrfs_inode_generation(eb, src_item) == 0) {
489 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000490 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000491
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000492 /*
493 * For regular files an ino_size == 0 is used only when
494 * logging that an inode exists, as part of a directory
495 * fsync, and the inode wasn't fsynced before. In this
496 * case don't set the size of the inode in the fs/subvol
497 * tree, otherwise we would be throwing valid data away.
498 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000499 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000500 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
501 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000502 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000503
504 btrfs_init_map_token(&token);
505 btrfs_set_token_inode_size(dst_eb, dst_item,
506 ino_size, &token);
507 }
Chris Masone02119d2008-09-05 16:13:11 -0400508 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000509 }
Chris Masone02119d2008-09-05 16:13:11 -0400510
511 if (overwrite_root &&
512 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
513 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
514 save_old_i_size = 1;
515 saved_i_size = btrfs_inode_size(path->nodes[0],
516 dst_item);
517 }
518 }
519
520 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
521 src_ptr, item_size);
522
523 if (save_old_i_size) {
524 struct btrfs_inode_item *dst_item;
525 dst_item = (struct btrfs_inode_item *)dst_ptr;
526 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
527 }
528
529 /* make sure the generation is filled in */
530 if (key->type == BTRFS_INODE_ITEM_KEY) {
531 struct btrfs_inode_item *dst_item;
532 dst_item = (struct btrfs_inode_item *)dst_ptr;
533 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
534 btrfs_set_inode_generation(path->nodes[0], dst_item,
535 trans->transid);
536 }
537 }
538no_copy:
539 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200540 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400541 return 0;
542}
543
544/*
545 * simple helper to read an inode off the disk from a given root
546 * This can only be called for subvolume roots and not for the log
547 */
548static noinline struct inode *read_one_inode(struct btrfs_root *root,
549 u64 objectid)
550{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400551 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400552 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400553
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 key.objectid = objectid;
555 key.type = BTRFS_INODE_ITEM_KEY;
556 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000557 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400558 if (IS_ERR(inode)) {
559 inode = NULL;
560 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400561 iput(inode);
562 inode = NULL;
563 }
564 return inode;
565}
566
567/* replays a single extent in 'eb' at 'slot' with 'key' into the
568 * subvolume 'root'. path is released on entry and should be released
569 * on exit.
570 *
571 * extents in the log tree have not been allocated out of the extent
572 * tree yet. So, this completes the allocation, taking a reference
573 * as required if the extent already exists or creating a new extent
574 * if it isn't in the extent allocation tree yet.
575 *
576 * The extent is inserted into the file, dropping any existing extents
577 * from the file that overlap the new one.
578 */
579static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
580 struct btrfs_root *root,
581 struct btrfs_path *path,
582 struct extent_buffer *eb, int slot,
583 struct btrfs_key *key)
584{
585 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400586 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400587 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000588 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400589 struct btrfs_file_extent_item *item;
590 struct inode *inode = NULL;
591 unsigned long size;
592 int ret = 0;
593
594 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
595 found_type = btrfs_file_extent_type(eb, item);
596
Yan Zhengd899e052008-10-30 14:25:28 -0400597 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000598 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
599 nbytes = btrfs_file_extent_num_bytes(eb, item);
600 extent_end = start + nbytes;
601
602 /*
603 * We don't add to the inodes nbytes if we are prealloc or a
604 * hole.
605 */
606 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
607 nbytes = 0;
608 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800609 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000610 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000611 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400612 } else {
613 ret = 0;
614 goto out;
615 }
616
617 inode = read_one_inode(root, key->objectid);
618 if (!inode) {
619 ret = -EIO;
620 goto out;
621 }
622
623 /*
624 * first check to see if we already have this extent in the
625 * file. This must be done before the btrfs_drop_extents run
626 * so we don't try to drop this extent.
627 */
Li Zefan33345d012011-04-20 10:31:50 +0800628 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400629 start, 0);
630
Yan Zhengd899e052008-10-30 14:25:28 -0400631 if (ret == 0 &&
632 (found_type == BTRFS_FILE_EXTENT_REG ||
633 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400634 struct btrfs_file_extent_item cmp1;
635 struct btrfs_file_extent_item cmp2;
636 struct btrfs_file_extent_item *existing;
637 struct extent_buffer *leaf;
638
639 leaf = path->nodes[0];
640 existing = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_file_extent_item);
642
643 read_extent_buffer(eb, &cmp1, (unsigned long)item,
644 sizeof(cmp1));
645 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
646 sizeof(cmp2));
647
648 /*
649 * we already have a pointer to this exact extent,
650 * we don't have to do anything
651 */
652 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200653 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400654 goto out;
655 }
656 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200657 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400658
659 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400660 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400661 if (ret)
662 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400663
Yan Zheng07d400a2009-01-06 11:42:00 -0500664 if (found_type == BTRFS_FILE_EXTENT_REG ||
665 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400666 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500667 unsigned long dest_offset;
668 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400669
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 ret = btrfs_insert_empty_item(trans, root, path, key,
671 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400672 if (ret)
673 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500674 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
675 path->slots[0]);
676 copy_extent_buffer(path->nodes[0], eb, dest_offset,
677 (unsigned long)item, sizeof(*item));
678
679 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
680 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
681 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500683
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800684 /*
685 * Manually record dirty extent, as here we did a shallow
686 * file extent item copy and skip normal backref update,
687 * but modifying extent tree all by ourselves.
688 * So need to manually record dirty extent for qgroup,
689 * as the owner of the file extent changed from log tree
690 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
691 */
Qu Wenruo50b3e042016-10-18 09:31:27 +0800692 ret = btrfs_qgroup_trace_extent(trans, root->fs_info,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800693 btrfs_file_extent_disk_bytenr(eb, item),
694 btrfs_file_extent_disk_num_bytes(eb, item),
695 GFP_NOFS);
696 if (ret < 0)
697 goto out;
698
Yan Zheng07d400a2009-01-06 11:42:00 -0500699 if (ins.objectid > 0) {
700 u64 csum_start;
701 u64 csum_end;
702 LIST_HEAD(ordered_sums);
703 /*
704 * is this extent already allocated in the extent
705 * allocation tree? If so, just add a reference
706 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000707 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500708 ins.offset);
709 if (ret == 0) {
710 ret = btrfs_inc_extent_ref(trans, root,
711 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400712 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100713 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400714 if (ret)
715 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500716 } else {
717 /*
718 * insert the extent pointer in the extent
719 * allocation tree
720 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400721 ret = btrfs_alloc_logged_file_extent(trans,
722 root, root->root_key.objectid,
723 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400724 if (ret)
725 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500726 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200727 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500728
729 if (btrfs_file_extent_compression(eb, item)) {
730 csum_start = ins.objectid;
731 csum_end = csum_start + ins.offset;
732 } else {
733 csum_start = ins.objectid +
734 btrfs_file_extent_offset(eb, item);
735 csum_end = csum_start +
736 btrfs_file_extent_num_bytes(eb, item);
737 }
738
739 ret = btrfs_lookup_csums_range(root->log_root,
740 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100741 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400742 if (ret)
743 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100744 /*
745 * Now delete all existing cums in the csum root that
746 * cover our range. We do this because we can have an
747 * extent that is completely referenced by one file
748 * extent item and partially referenced by another
749 * file extent item (like after using the clone or
750 * extent_same ioctls). In this case if we end up doing
751 * the replay of the one that partially references the
752 * extent first, and we do not do the csum deletion
753 * below, we can get 2 csum items in the csum tree that
754 * overlap each other. For example, imagine our log has
755 * the two following file extent items:
756 *
757 * key (257 EXTENT_DATA 409600)
758 * extent data disk byte 12845056 nr 102400
759 * extent data offset 20480 nr 20480 ram 102400
760 *
761 * key (257 EXTENT_DATA 819200)
762 * extent data disk byte 12845056 nr 102400
763 * extent data offset 0 nr 102400 ram 102400
764 *
765 * Where the second one fully references the 100K extent
766 * that starts at disk byte 12845056, and the log tree
767 * has a single csum item that covers the entire range
768 * of the extent:
769 *
770 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
771 *
772 * After the first file extent item is replayed, the
773 * csum tree gets the following csum item:
774 *
775 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
776 *
777 * Which covers the 20K sub-range starting at offset 20K
778 * of our extent. Now when we replay the second file
779 * extent item, if we do not delete existing csum items
780 * that cover any of its blocks, we end up getting two
781 * csum items in our csum tree that overlap each other:
782 *
783 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
784 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
785 *
786 * Which is a problem, because after this anyone trying
787 * to lookup up for the checksum of any block of our
788 * extent starting at an offset of 40K or higher, will
789 * end up looking at the second csum item only, which
790 * does not contain the checksum for any block starting
791 * at offset 40K or higher of our extent.
792 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500793 while (!list_empty(&ordered_sums)) {
794 struct btrfs_ordered_sum *sums;
795 sums = list_entry(ordered_sums.next,
796 struct btrfs_ordered_sum,
797 list);
Josef Bacik36508602013-04-25 16:23:32 -0400798 if (!ret)
Filipe Mananab84b8392015-08-19 11:09:40 +0100799 ret = btrfs_del_csums(trans,
800 root->fs_info->csum_root,
801 sums->bytenr,
802 sums->len);
803 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400804 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500805 root->fs_info->csum_root,
806 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500807 list_del(&sums->list);
808 kfree(sums);
809 }
Josef Bacik36508602013-04-25 16:23:32 -0400810 if (ret)
811 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500812 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200813 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500814 }
815 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
816 /* inline extents are easy, we just overwrite them */
817 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400818 if (ret)
819 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500820 }
821
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000822 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600823 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400824out:
825 if (inode)
826 iput(inode);
827 return ret;
828}
829
830/*
831 * when cleaning up conflicts between the directory names in the
832 * subvolume, directory names in the log and directory names in the
833 * inode back references, we may have to unlink inodes from directories.
834 *
835 * This is a helper function to do the unlink of a specific directory
836 * item
837 */
838static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
839 struct btrfs_root *root,
840 struct btrfs_path *path,
841 struct inode *dir,
842 struct btrfs_dir_item *di)
843{
844 struct inode *inode;
845 char *name;
846 int name_len;
847 struct extent_buffer *leaf;
848 struct btrfs_key location;
849 int ret;
850
851 leaf = path->nodes[0];
852
853 btrfs_dir_item_key_to_cpu(leaf, di, &location);
854 name_len = btrfs_dir_name_len(leaf, di);
855 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000856 if (!name)
857 return -ENOMEM;
858
Chris Masone02119d2008-09-05 16:13:11 -0400859 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200860 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400861
862 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000863 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400864 ret = -EIO;
865 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000866 }
Chris Masone02119d2008-09-05 16:13:11 -0400867
Yan Zhengec051c02009-01-05 15:43:42 -0500868 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400869 if (ret)
870 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400871
Chris Masone02119d2008-09-05 16:13:11 -0400872 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400873 if (ret)
874 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100875 else
876 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400877out:
878 kfree(name);
879 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400880 return ret;
881}
882
883/*
884 * helper function to see if a given name and sequence number found
885 * in an inode back reference are already in a directory and correctly
886 * point to this inode
887 */
888static noinline int inode_in_dir(struct btrfs_root *root,
889 struct btrfs_path *path,
890 u64 dirid, u64 objectid, u64 index,
891 const char *name, int name_len)
892{
893 struct btrfs_dir_item *di;
894 struct btrfs_key location;
895 int match = 0;
896
897 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
898 index, name, name_len, 0);
899 if (di && !IS_ERR(di)) {
900 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
901 if (location.objectid != objectid)
902 goto out;
903 } else
904 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200905 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400906
907 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
908 if (di && !IS_ERR(di)) {
909 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
910 if (location.objectid != objectid)
911 goto out;
912 } else
913 goto out;
914 match = 1;
915out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200916 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400917 return match;
918}
919
920/*
921 * helper function to check a log tree for a named back reference in
922 * an inode. This is used to decide if a back reference that is
923 * found in the subvolume conflicts with what we find in the log.
924 *
925 * inode backreferences may have multiple refs in a single item,
926 * during replay we process one reference at a time, and we don't
927 * want to delete valid links to a file from the subvolume if that
928 * link is also in the log.
929 */
930static noinline int backref_in_log(struct btrfs_root *log,
931 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700932 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000933 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400934{
935 struct btrfs_path *path;
936 struct btrfs_inode_ref *ref;
937 unsigned long ptr;
938 unsigned long ptr_end;
939 unsigned long name_ptr;
940 int found_name_len;
941 int item_size;
942 int ret;
943 int match = 0;
944
945 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000946 if (!path)
947 return -ENOMEM;
948
Chris Masone02119d2008-09-05 16:13:11 -0400949 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
950 if (ret != 0)
951 goto out;
952
Chris Masone02119d2008-09-05 16:13:11 -0400953 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700954
955 if (key->type == BTRFS_INODE_EXTREF_KEY) {
956 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
957 name, namelen, NULL))
958 match = 1;
959
960 goto out;
961 }
962
963 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400964 ptr_end = ptr + item_size;
965 while (ptr < ptr_end) {
966 ref = (struct btrfs_inode_ref *)ptr;
967 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
968 if (found_name_len == namelen) {
969 name_ptr = (unsigned long)(ref + 1);
970 ret = memcmp_extent_buffer(path->nodes[0], name,
971 name_ptr, namelen);
972 if (ret == 0) {
973 match = 1;
974 goto out;
975 }
976 }
977 ptr = (unsigned long)(ref + 1) + found_name_len;
978 }
979out:
980 btrfs_free_path(path);
981 return match;
982}
983
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700984static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
985 struct btrfs_root *root,
986 struct btrfs_path *path,
987 struct btrfs_root *log_root,
988 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700989 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700990 u64 inode_objectid, u64 parent_objectid,
991 u64 ref_index, char *name, int namelen,
992 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700993{
994 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700995 char *victim_name;
996 int victim_name_len;
997 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700998 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700999 struct btrfs_key search_key;
1000 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001001
Mark Fashehf1863732012-08-08 11:32:27 -07001002again:
1003 /* Search old style refs */
1004 search_key.objectid = inode_objectid;
1005 search_key.type = BTRFS_INODE_REF_KEY;
1006 search_key.offset = parent_objectid;
1007 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001008 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001009 struct btrfs_inode_ref *victim_ref;
1010 unsigned long ptr;
1011 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001012
1013 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001014
1015 /* are we trying to overwrite a back ref for the root directory
1016 * if so, just jump out, we're done
1017 */
Mark Fashehf1863732012-08-08 11:32:27 -07001018 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001019 return 1;
1020
1021 /* check all the names in this back reference to see
1022 * if they are in the log. if so, we allow them to stay
1023 * otherwise they must be unlinked as a conflict
1024 */
1025 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1026 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1027 while (ptr < ptr_end) {
1028 victim_ref = (struct btrfs_inode_ref *)ptr;
1029 victim_name_len = btrfs_inode_ref_name_len(leaf,
1030 victim_ref);
1031 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001032 if (!victim_name)
1033 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001034
1035 read_extent_buffer(leaf, victim_name,
1036 (unsigned long)(victim_ref + 1),
1037 victim_name_len);
1038
Mark Fashehf1863732012-08-08 11:32:27 -07001039 if (!backref_in_log(log_root, &search_key,
1040 parent_objectid,
1041 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001042 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -07001043 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001044 btrfs_release_path(path);
1045
1046 ret = btrfs_unlink_inode(trans, root, dir,
1047 inode, victim_name,
1048 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001049 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001050 if (ret)
1051 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001052 ret = btrfs_run_delayed_items(trans, root);
1053 if (ret)
1054 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001055 *search_done = 1;
1056 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001057 }
1058 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001059
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001060 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1061 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001062
1063 /*
1064 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001065 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001066 */
1067 *search_done = 1;
1068 }
1069 btrfs_release_path(path);
1070
Mark Fashehf1863732012-08-08 11:32:27 -07001071 /* Same search but for extended refs */
1072 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1073 inode_objectid, parent_objectid, 0,
1074 0);
1075 if (!IS_ERR_OR_NULL(extref)) {
1076 u32 item_size;
1077 u32 cur_offset = 0;
1078 unsigned long base;
1079 struct inode *victim_parent;
1080
1081 leaf = path->nodes[0];
1082
1083 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1084 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1085
1086 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001087 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001088
1089 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1090
1091 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1092 goto next;
1093
1094 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001095 if (!victim_name)
1096 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001097 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1098 victim_name_len);
1099
1100 search_key.objectid = inode_objectid;
1101 search_key.type = BTRFS_INODE_EXTREF_KEY;
1102 search_key.offset = btrfs_extref_hash(parent_objectid,
1103 victim_name,
1104 victim_name_len);
1105 ret = 0;
1106 if (!backref_in_log(log_root, &search_key,
1107 parent_objectid, victim_name,
1108 victim_name_len)) {
1109 ret = -ENOENT;
1110 victim_parent = read_one_inode(root,
1111 parent_objectid);
1112 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001113 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001114 btrfs_release_path(path);
1115
1116 ret = btrfs_unlink_inode(trans, root,
1117 victim_parent,
1118 inode,
1119 victim_name,
1120 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001121 if (!ret)
1122 ret = btrfs_run_delayed_items(
1123 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001124 }
Mark Fashehf1863732012-08-08 11:32:27 -07001125 iput(victim_parent);
1126 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001127 if (ret)
1128 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001129 *search_done = 1;
1130 goto again;
1131 }
1132 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001133 if (ret)
1134 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001135next:
1136 cur_offset += victim_name_len + sizeof(*extref);
1137 }
1138 *search_done = 1;
1139 }
1140 btrfs_release_path(path);
1141
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001142 /* look for a conflicting sequence number */
1143 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001144 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001145 if (di && !IS_ERR(di)) {
1146 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001147 if (ret)
1148 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001149 }
1150 btrfs_release_path(path);
1151
1152 /* look for a conflicing name */
1153 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1154 name, namelen, 0);
1155 if (di && !IS_ERR(di)) {
1156 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001157 if (ret)
1158 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001159 }
1160 btrfs_release_path(path);
1161
1162 return 0;
1163}
Chris Masone02119d2008-09-05 16:13:11 -04001164
Mark Fashehf1863732012-08-08 11:32:27 -07001165static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1166 u32 *namelen, char **name, u64 *index,
1167 u64 *parent_objectid)
1168{
1169 struct btrfs_inode_extref *extref;
1170
1171 extref = (struct btrfs_inode_extref *)ref_ptr;
1172
1173 *namelen = btrfs_inode_extref_name_len(eb, extref);
1174 *name = kmalloc(*namelen, GFP_NOFS);
1175 if (*name == NULL)
1176 return -ENOMEM;
1177
1178 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1179 *namelen);
1180
1181 *index = btrfs_inode_extref_index(eb, extref);
1182 if (parent_objectid)
1183 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1184
1185 return 0;
1186}
1187
1188static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1189 u32 *namelen, char **name, u64 *index)
1190{
1191 struct btrfs_inode_ref *ref;
1192
1193 ref = (struct btrfs_inode_ref *)ref_ptr;
1194
1195 *namelen = btrfs_inode_ref_name_len(eb, ref);
1196 *name = kmalloc(*namelen, GFP_NOFS);
1197 if (*name == NULL)
1198 return -ENOMEM;
1199
1200 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1201
1202 *index = btrfs_inode_ref_index(eb, ref);
1203
1204 return 0;
1205}
1206
Chris Masone02119d2008-09-05 16:13:11 -04001207/*
1208 * replay one inode back reference item found in the log tree.
1209 * eb, slot and key refer to the buffer and key found in the log tree.
1210 * root is the destination we are replaying into, and path is for temp
1211 * use by this function. (it should be released on return).
1212 */
1213static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1214 struct btrfs_root *root,
1215 struct btrfs_root *log,
1216 struct btrfs_path *path,
1217 struct extent_buffer *eb, int slot,
1218 struct btrfs_key *key)
1219{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001220 struct inode *dir = NULL;
1221 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001222 unsigned long ref_ptr;
1223 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001224 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001225 int namelen;
1226 int ret;
liuboc622ae62011-03-26 08:01:12 -04001227 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001228 int log_ref_ver = 0;
1229 u64 parent_objectid;
1230 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001231 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001232 int ref_struct_size;
1233
1234 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1235 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1236
1237 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1238 struct btrfs_inode_extref *r;
1239
1240 ref_struct_size = sizeof(struct btrfs_inode_extref);
1241 log_ref_ver = 1;
1242 r = (struct btrfs_inode_extref *)ref_ptr;
1243 parent_objectid = btrfs_inode_extref_parent(eb, r);
1244 } else {
1245 ref_struct_size = sizeof(struct btrfs_inode_ref);
1246 parent_objectid = key->offset;
1247 }
1248 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001249
Chris Masone02119d2008-09-05 16:13:11 -04001250 /*
1251 * it is possible that we didn't log all the parent directories
1252 * for a given inode. If we don't find the dir, just don't
1253 * copy the back ref in. The link count fixup code will take
1254 * care of the rest
1255 */
Mark Fashehf1863732012-08-08 11:32:27 -07001256 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001257 if (!dir) {
1258 ret = -ENOENT;
1259 goto out;
1260 }
Chris Masone02119d2008-09-05 16:13:11 -04001261
Mark Fashehf1863732012-08-08 11:32:27 -07001262 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001263 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001264 ret = -EIO;
1265 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001266 }
Chris Masone02119d2008-09-05 16:13:11 -04001267
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001268 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001269 if (log_ref_ver) {
1270 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1271 &ref_index, &parent_objectid);
1272 /*
1273 * parent object can change from one array
1274 * item to another.
1275 */
1276 if (!dir)
1277 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001278 if (!dir) {
1279 ret = -ENOENT;
1280 goto out;
1281 }
Mark Fashehf1863732012-08-08 11:32:27 -07001282 } else {
1283 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1284 &ref_index);
1285 }
1286 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001287 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001288
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001289 /* if we already have a perfect match, we're done */
1290 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001291 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001292 /*
1293 * look for a conflicting back reference in the
1294 * metadata. if we find one we have to unlink that name
1295 * of the file before we add our new link. Later on, we
1296 * overwrite any existing back reference, and we don't
1297 * want to create dangling pointers in the directory.
1298 */
Chris Masone02119d2008-09-05 16:13:11 -04001299
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001300 if (!search_done) {
1301 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001302 dir, inode, eb,
1303 inode_objectid,
1304 parent_objectid,
1305 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001306 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001307 if (ret) {
1308 if (ret == 1)
1309 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001310 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001311 }
Chris Masone02119d2008-09-05 16:13:11 -04001312 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001313
1314 /* insert our name */
1315 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001316 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001317 if (ret)
1318 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001319
1320 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001321 }
liuboc622ae62011-03-26 08:01:12 -04001322
Mark Fashehf1863732012-08-08 11:32:27 -07001323 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001324 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001325 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001326 if (log_ref_ver) {
1327 iput(dir);
1328 dir = NULL;
1329 }
Chris Masone02119d2008-09-05 16:13:11 -04001330 }
Chris Masone02119d2008-09-05 16:13:11 -04001331
1332 /* finally write the back reference in the inode */
1333 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001334out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001335 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001336 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001337 iput(dir);
1338 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001339 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001340}
1341
Yan, Zhengc71bf092009-11-12 09:34:40 +00001342static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001343 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001344{
1345 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001346
David Sterba9c4f61f2015-01-02 19:12:57 +01001347 ret = btrfs_insert_orphan_item(trans, root, ino);
1348 if (ret == -EEXIST)
1349 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001350
Yan, Zhengc71bf092009-11-12 09:34:40 +00001351 return ret;
1352}
1353
Mark Fashehf1863732012-08-08 11:32:27 -07001354static int count_inode_extrefs(struct btrfs_root *root,
1355 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001356{
Mark Fashehf1863732012-08-08 11:32:27 -07001357 int ret = 0;
1358 int name_len;
1359 unsigned int nlink = 0;
1360 u32 item_size;
1361 u32 cur_offset = 0;
1362 u64 inode_objectid = btrfs_ino(inode);
1363 u64 offset = 0;
1364 unsigned long ptr;
1365 struct btrfs_inode_extref *extref;
1366 struct extent_buffer *leaf;
1367
1368 while (1) {
1369 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1370 &extref, &offset);
1371 if (ret)
1372 break;
1373
1374 leaf = path->nodes[0];
1375 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1376 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001377 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001378
1379 while (cur_offset < item_size) {
1380 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1381 name_len = btrfs_inode_extref_name_len(leaf, extref);
1382
1383 nlink++;
1384
1385 cur_offset += name_len + sizeof(*extref);
1386 }
1387
1388 offset++;
1389 btrfs_release_path(path);
1390 }
1391 btrfs_release_path(path);
1392
Filipe Manana2c2c4522015-01-13 16:40:04 +00001393 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001394 return ret;
1395 return nlink;
1396}
1397
1398static int count_inode_refs(struct btrfs_root *root,
1399 struct inode *inode, struct btrfs_path *path)
1400{
Chris Masone02119d2008-09-05 16:13:11 -04001401 int ret;
1402 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001403 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001404 unsigned long ptr;
1405 unsigned long ptr_end;
1406 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001407 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001408
Li Zefan33345d012011-04-20 10:31:50 +08001409 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001410 key.type = BTRFS_INODE_REF_KEY;
1411 key.offset = (u64)-1;
1412
Chris Masond3977122009-01-05 21:25:51 -05001413 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001414 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1415 if (ret < 0)
1416 break;
1417 if (ret > 0) {
1418 if (path->slots[0] == 0)
1419 break;
1420 path->slots[0]--;
1421 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001422process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001423 btrfs_item_key_to_cpu(path->nodes[0], &key,
1424 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001425 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001426 key.type != BTRFS_INODE_REF_KEY)
1427 break;
1428 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1429 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1430 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001431 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001432 struct btrfs_inode_ref *ref;
1433
1434 ref = (struct btrfs_inode_ref *)ptr;
1435 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1436 ref);
1437 ptr = (unsigned long)(ref + 1) + name_len;
1438 nlink++;
1439 }
1440
1441 if (key.offset == 0)
1442 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001443 if (path->slots[0] > 0) {
1444 path->slots[0]--;
1445 goto process_slot;
1446 }
Chris Masone02119d2008-09-05 16:13:11 -04001447 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001448 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001449 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001450 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001451
1452 return nlink;
1453}
1454
1455/*
1456 * There are a few corners where the link count of the file can't
1457 * be properly maintained during replay. So, instead of adding
1458 * lots of complexity to the log code, we just scan the backrefs
1459 * for any file that has been through replay.
1460 *
1461 * The scan will update the link count on the inode to reflect the
1462 * number of back refs found. If it goes down to zero, the iput
1463 * will free the inode.
1464 */
1465static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1466 struct btrfs_root *root,
1467 struct inode *inode)
1468{
1469 struct btrfs_path *path;
1470 int ret;
1471 u64 nlink = 0;
1472 u64 ino = btrfs_ino(inode);
1473
1474 path = btrfs_alloc_path();
1475 if (!path)
1476 return -ENOMEM;
1477
1478 ret = count_inode_refs(root, inode, path);
1479 if (ret < 0)
1480 goto out;
1481
1482 nlink = ret;
1483
1484 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001485 if (ret < 0)
1486 goto out;
1487
1488 nlink += ret;
1489
1490 ret = 0;
1491
Chris Masone02119d2008-09-05 16:13:11 -04001492 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001493 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001494 btrfs_update_inode(trans, root, inode);
1495 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001496 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001497
Yan, Zhengc71bf092009-11-12 09:34:40 +00001498 if (inode->i_nlink == 0) {
1499 if (S_ISDIR(inode->i_mode)) {
1500 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001501 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001502 if (ret)
1503 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001504 }
Li Zefan33345d012011-04-20 10:31:50 +08001505 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001506 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001507
Mark Fashehf1863732012-08-08 11:32:27 -07001508out:
1509 btrfs_free_path(path);
1510 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001511}
1512
1513static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *root,
1515 struct btrfs_path *path)
1516{
1517 int ret;
1518 struct btrfs_key key;
1519 struct inode *inode;
1520
1521 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1522 key.type = BTRFS_ORPHAN_ITEM_KEY;
1523 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001524 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001525 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1526 if (ret < 0)
1527 break;
1528
1529 if (ret == 1) {
1530 if (path->slots[0] == 0)
1531 break;
1532 path->slots[0]--;
1533 }
1534
1535 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1536 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1537 key.type != BTRFS_ORPHAN_ITEM_KEY)
1538 break;
1539
1540 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001541 if (ret)
1542 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001543
David Sterbab3b4aa72011-04-21 01:20:15 +02001544 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001545 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001546 if (!inode)
1547 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001548
1549 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001550 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001551 if (ret)
1552 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001553
Chris Mason12fcfd22009-03-24 10:24:20 -04001554 /*
1555 * fixup on a directory may create new entries,
1556 * make sure we always look for the highset possible
1557 * offset
1558 */
1559 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001560 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001561 ret = 0;
1562out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001563 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001564 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001565}
1566
1567
1568/*
1569 * record a given inode in the fixup dir so we can check its link
1570 * count when replay is done. The link count is incremented here
1571 * so the inode won't go away until we check it
1572 */
1573static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1574 struct btrfs_root *root,
1575 struct btrfs_path *path,
1576 u64 objectid)
1577{
1578 struct btrfs_key key;
1579 int ret = 0;
1580 struct inode *inode;
1581
1582 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001583 if (!inode)
1584 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001585
1586 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001587 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001588 key.offset = objectid;
1589
1590 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1591
David Sterbab3b4aa72011-04-21 01:20:15 +02001592 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001593 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001594 if (!inode->i_nlink)
1595 set_nlink(inode, 1);
1596 else
Zach Brown8b558c52013-10-16 12:10:34 -07001597 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001598 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001599 } else if (ret == -EEXIST) {
1600 ret = 0;
1601 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001602 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001603 }
1604 iput(inode);
1605
1606 return ret;
1607}
1608
1609/*
1610 * when replaying the log for a directory, we only insert names
1611 * for inodes that actually exist. This means an fsync on a directory
1612 * does not implicitly fsync all the new files in it
1613 */
1614static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001616 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001617 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001618 struct btrfs_key *location)
1619{
1620 struct inode *inode;
1621 struct inode *dir;
1622 int ret;
1623
1624 inode = read_one_inode(root, location->objectid);
1625 if (!inode)
1626 return -ENOENT;
1627
1628 dir = read_one_inode(root, dirid);
1629 if (!dir) {
1630 iput(inode);
1631 return -EIO;
1632 }
Josef Bacikd5554382013-09-11 14:17:00 -04001633
Chris Masone02119d2008-09-05 16:13:11 -04001634 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1635
1636 /* FIXME, put inode into FIXUP list */
1637
1638 iput(inode);
1639 iput(dir);
1640 return ret;
1641}
1642
1643/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001644 * Return true if an inode reference exists in the log for the given name,
1645 * inode and parent inode.
1646 */
1647static bool name_in_log_ref(struct btrfs_root *log_root,
1648 const char *name, const int name_len,
1649 const u64 dirid, const u64 ino)
1650{
1651 struct btrfs_key search_key;
1652
1653 search_key.objectid = ino;
1654 search_key.type = BTRFS_INODE_REF_KEY;
1655 search_key.offset = dirid;
1656 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1657 return true;
1658
1659 search_key.type = BTRFS_INODE_EXTREF_KEY;
1660 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1661 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1662 return true;
1663
1664 return false;
1665}
1666
1667/*
Chris Masone02119d2008-09-05 16:13:11 -04001668 * take a single entry in a log directory item and replay it into
1669 * the subvolume.
1670 *
1671 * if a conflicting item exists in the subdirectory already,
1672 * the inode it points to is unlinked and put into the link count
1673 * fix up tree.
1674 *
1675 * If a name from the log points to a file or directory that does
1676 * not exist in the FS, it is skipped. fsyncs on directories
1677 * do not force down inodes inside that directory, just changes to the
1678 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001679 *
1680 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1681 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001682 */
1683static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root,
1685 struct btrfs_path *path,
1686 struct extent_buffer *eb,
1687 struct btrfs_dir_item *di,
1688 struct btrfs_key *key)
1689{
1690 char *name;
1691 int name_len;
1692 struct btrfs_dir_item *dst_di;
1693 struct btrfs_key found_key;
1694 struct btrfs_key log_key;
1695 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001696 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001697 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001698 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001699 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001700 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001701
1702 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001703 if (!dir)
1704 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001705
1706 name_len = btrfs_dir_name_len(eb, di);
1707 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001708 if (!name) {
1709 ret = -ENOMEM;
1710 goto out;
1711 }
liubo2a29edc2011-01-26 06:22:08 +00001712
Chris Masone02119d2008-09-05 16:13:11 -04001713 log_type = btrfs_dir_type(eb, di);
1714 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1715 name_len);
1716
1717 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001718 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1719 if (exists == 0)
1720 exists = 1;
1721 else
1722 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001723 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001724
Chris Masone02119d2008-09-05 16:13:11 -04001725 if (key->type == BTRFS_DIR_ITEM_KEY) {
1726 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1727 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001728 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001729 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1730 key->objectid,
1731 key->offset, name,
1732 name_len, 1);
1733 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001734 /* Corruption */
1735 ret = -EINVAL;
1736 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001737 }
David Sterbac7040052011-04-19 18:00:01 +02001738 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001739 /* we need a sequence number to insert, so we only
1740 * do inserts for the BTRFS_DIR_INDEX_KEY types
1741 */
1742 if (key->type != BTRFS_DIR_INDEX_KEY)
1743 goto out;
1744 goto insert;
1745 }
1746
1747 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1748 /* the existing item matches the logged item */
1749 if (found_key.objectid == log_key.objectid &&
1750 found_key.type == log_key.type &&
1751 found_key.offset == log_key.offset &&
1752 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001753 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001754 goto out;
1755 }
1756
1757 /*
1758 * don't drop the conflicting directory entry if the inode
1759 * for the new entry doesn't exist
1760 */
Chris Mason4bef0842008-09-08 11:18:08 -04001761 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001762 goto out;
1763
Chris Masone02119d2008-09-05 16:13:11 -04001764 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001765 if (ret)
1766 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001767
1768 if (key->type == BTRFS_DIR_INDEX_KEY)
1769 goto insert;
1770out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001771 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001772 if (!ret && update_size) {
1773 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1774 ret = btrfs_update_inode(trans, root, dir);
1775 }
Chris Masone02119d2008-09-05 16:13:11 -04001776 kfree(name);
1777 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001778 if (!ret && name_added)
1779 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001780 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001781
1782insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001783 if (name_in_log_ref(root->log_root, name, name_len,
1784 key->objectid, log_key.objectid)) {
1785 /* The dentry will be added later. */
1786 ret = 0;
1787 update_size = false;
1788 goto out;
1789 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001790 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001791 ret = insert_one_name(trans, root, key->objectid, key->offset,
1792 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001793 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001794 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001795 if (!ret)
1796 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001797 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001798 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001799 goto out;
1800}
1801
1802/*
1803 * find all the names in a directory item and reconcile them into
1804 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1805 * one name in a directory item, but the same code gets used for
1806 * both directory index types
1807 */
1808static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1809 struct btrfs_root *root,
1810 struct btrfs_path *path,
1811 struct extent_buffer *eb, int slot,
1812 struct btrfs_key *key)
1813{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001814 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001815 u32 item_size = btrfs_item_size_nr(eb, slot);
1816 struct btrfs_dir_item *di;
1817 int name_len;
1818 unsigned long ptr;
1819 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001820 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001821
1822 ptr = btrfs_item_ptr_offset(eb, slot);
1823 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001824 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001825 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001826 if (verify_dir_item(root, eb, di))
1827 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001828 name_len = btrfs_dir_name_len(eb, di);
1829 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001830 if (ret < 0)
1831 break;
Chris Masone02119d2008-09-05 16:13:11 -04001832 ptr = (unsigned long)(di + 1);
1833 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001834
1835 /*
1836 * If this entry refers to a non-directory (directories can not
1837 * have a link count > 1) and it was added in the transaction
1838 * that was not committed, make sure we fixup the link count of
1839 * the inode it the entry points to. Otherwise something like
1840 * the following would result in a directory pointing to an
1841 * inode with a wrong link that does not account for this dir
1842 * entry:
1843 *
1844 * mkdir testdir
1845 * touch testdir/foo
1846 * touch testdir/bar
1847 * sync
1848 *
1849 * ln testdir/bar testdir/bar_link
1850 * ln testdir/foo testdir/foo_link
1851 * xfs_io -c "fsync" testdir/bar
1852 *
1853 * <power failure>
1854 *
1855 * mount fs, log replay happens
1856 *
1857 * File foo would remain with a link count of 1 when it has two
1858 * entries pointing to it in the directory testdir. This would
1859 * make it impossible to ever delete the parent directory has
1860 * it would result in stale dentries that can never be deleted.
1861 */
1862 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1863 struct btrfs_key di_key;
1864
1865 if (!fixup_path) {
1866 fixup_path = btrfs_alloc_path();
1867 if (!fixup_path) {
1868 ret = -ENOMEM;
1869 break;
1870 }
1871 }
1872
1873 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1874 ret = link_to_fixup_dir(trans, root, fixup_path,
1875 di_key.objectid);
1876 if (ret)
1877 break;
1878 }
1879 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001880 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001881 btrfs_free_path(fixup_path);
1882 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001883}
1884
1885/*
1886 * directory replay has two parts. There are the standard directory
1887 * items in the log copied from the subvolume, and range items
1888 * created in the log while the subvolume was logged.
1889 *
1890 * The range items tell us which parts of the key space the log
1891 * is authoritative for. During replay, if a key in the subvolume
1892 * directory is in a logged range item, but not actually in the log
1893 * that means it was deleted from the directory before the fsync
1894 * and should be removed.
1895 */
1896static noinline int find_dir_range(struct btrfs_root *root,
1897 struct btrfs_path *path,
1898 u64 dirid, int key_type,
1899 u64 *start_ret, u64 *end_ret)
1900{
1901 struct btrfs_key key;
1902 u64 found_end;
1903 struct btrfs_dir_log_item *item;
1904 int ret;
1905 int nritems;
1906
1907 if (*start_ret == (u64)-1)
1908 return 1;
1909
1910 key.objectid = dirid;
1911 key.type = key_type;
1912 key.offset = *start_ret;
1913
1914 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1915 if (ret < 0)
1916 goto out;
1917 if (ret > 0) {
1918 if (path->slots[0] == 0)
1919 goto out;
1920 path->slots[0]--;
1921 }
1922 if (ret != 0)
1923 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1924
1925 if (key.type != key_type || key.objectid != dirid) {
1926 ret = 1;
1927 goto next;
1928 }
1929 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1930 struct btrfs_dir_log_item);
1931 found_end = btrfs_dir_log_end(path->nodes[0], item);
1932
1933 if (*start_ret >= key.offset && *start_ret <= found_end) {
1934 ret = 0;
1935 *start_ret = key.offset;
1936 *end_ret = found_end;
1937 goto out;
1938 }
1939 ret = 1;
1940next:
1941 /* check the next slot in the tree to see if it is a valid item */
1942 nritems = btrfs_header_nritems(path->nodes[0]);
1943 if (path->slots[0] >= nritems) {
1944 ret = btrfs_next_leaf(root, path);
1945 if (ret)
1946 goto out;
1947 } else {
1948 path->slots[0]++;
1949 }
1950
1951 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1952
1953 if (key.type != key_type || key.objectid != dirid) {
1954 ret = 1;
1955 goto out;
1956 }
1957 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1958 struct btrfs_dir_log_item);
1959 found_end = btrfs_dir_log_end(path->nodes[0], item);
1960 *start_ret = key.offset;
1961 *end_ret = found_end;
1962 ret = 0;
1963out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001964 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001965 return ret;
1966}
1967
1968/*
1969 * this looks for a given directory item in the log. If the directory
1970 * item is not in the log, the item is removed and the inode it points
1971 * to is unlinked
1972 */
1973static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1974 struct btrfs_root *root,
1975 struct btrfs_root *log,
1976 struct btrfs_path *path,
1977 struct btrfs_path *log_path,
1978 struct inode *dir,
1979 struct btrfs_key *dir_key)
1980{
1981 int ret;
1982 struct extent_buffer *eb;
1983 int slot;
1984 u32 item_size;
1985 struct btrfs_dir_item *di;
1986 struct btrfs_dir_item *log_di;
1987 int name_len;
1988 unsigned long ptr;
1989 unsigned long ptr_end;
1990 char *name;
1991 struct inode *inode;
1992 struct btrfs_key location;
1993
1994again:
1995 eb = path->nodes[0];
1996 slot = path->slots[0];
1997 item_size = btrfs_item_size_nr(eb, slot);
1998 ptr = btrfs_item_ptr_offset(eb, slot);
1999 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002000 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002001 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04002002 if (verify_dir_item(root, eb, di)) {
2003 ret = -EIO;
2004 goto out;
2005 }
2006
Chris Masone02119d2008-09-05 16:13:11 -04002007 name_len = btrfs_dir_name_len(eb, di);
2008 name = kmalloc(name_len, GFP_NOFS);
2009 if (!name) {
2010 ret = -ENOMEM;
2011 goto out;
2012 }
2013 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2014 name_len);
2015 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002016 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002017 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2018 dir_key->objectid,
2019 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002020 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002021 log_di = btrfs_lookup_dir_index_item(trans, log,
2022 log_path,
2023 dir_key->objectid,
2024 dir_key->offset,
2025 name, name_len, 0);
2026 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002027 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002028 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002029 btrfs_release_path(path);
2030 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002031 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002032 if (!inode) {
2033 kfree(name);
2034 return -EIO;
2035 }
Chris Masone02119d2008-09-05 16:13:11 -04002036
2037 ret = link_to_fixup_dir(trans, root,
2038 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002039 if (ret) {
2040 kfree(name);
2041 iput(inode);
2042 goto out;
2043 }
2044
Zach Brown8b558c52013-10-16 12:10:34 -07002045 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002046 ret = btrfs_unlink_inode(trans, root, dir, inode,
2047 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002048 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01002049 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002050 kfree(name);
2051 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002052 if (ret)
2053 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002054
2055 /* there might still be more names under this key
2056 * check and repeat if required
2057 */
2058 ret = btrfs_search_slot(NULL, root, dir_key, path,
2059 0, 0);
2060 if (ret == 0)
2061 goto again;
2062 ret = 0;
2063 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002064 } else if (IS_ERR(log_di)) {
2065 kfree(name);
2066 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002067 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002068 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002069 kfree(name);
2070
2071 ptr = (unsigned long)(di + 1);
2072 ptr += name_len;
2073 }
2074 ret = 0;
2075out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002076 btrfs_release_path(path);
2077 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002078 return ret;
2079}
2080
Filipe Manana4f764e52015-02-23 19:53:35 +00002081static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2082 struct btrfs_root *root,
2083 struct btrfs_root *log,
2084 struct btrfs_path *path,
2085 const u64 ino)
2086{
2087 struct btrfs_key search_key;
2088 struct btrfs_path *log_path;
2089 int i;
2090 int nritems;
2091 int ret;
2092
2093 log_path = btrfs_alloc_path();
2094 if (!log_path)
2095 return -ENOMEM;
2096
2097 search_key.objectid = ino;
2098 search_key.type = BTRFS_XATTR_ITEM_KEY;
2099 search_key.offset = 0;
2100again:
2101 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2102 if (ret < 0)
2103 goto out;
2104process_leaf:
2105 nritems = btrfs_header_nritems(path->nodes[0]);
2106 for (i = path->slots[0]; i < nritems; i++) {
2107 struct btrfs_key key;
2108 struct btrfs_dir_item *di;
2109 struct btrfs_dir_item *log_di;
2110 u32 total_size;
2111 u32 cur;
2112
2113 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2114 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2115 ret = 0;
2116 goto out;
2117 }
2118
2119 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2120 total_size = btrfs_item_size_nr(path->nodes[0], i);
2121 cur = 0;
2122 while (cur < total_size) {
2123 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2124 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2125 u32 this_len = sizeof(*di) + name_len + data_len;
2126 char *name;
2127
2128 name = kmalloc(name_len, GFP_NOFS);
2129 if (!name) {
2130 ret = -ENOMEM;
2131 goto out;
2132 }
2133 read_extent_buffer(path->nodes[0], name,
2134 (unsigned long)(di + 1), name_len);
2135
2136 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2137 name, name_len, 0);
2138 btrfs_release_path(log_path);
2139 if (!log_di) {
2140 /* Doesn't exist in log tree, so delete it. */
2141 btrfs_release_path(path);
2142 di = btrfs_lookup_xattr(trans, root, path, ino,
2143 name, name_len, -1);
2144 kfree(name);
2145 if (IS_ERR(di)) {
2146 ret = PTR_ERR(di);
2147 goto out;
2148 }
2149 ASSERT(di);
2150 ret = btrfs_delete_one_dir_name(trans, root,
2151 path, di);
2152 if (ret)
2153 goto out;
2154 btrfs_release_path(path);
2155 search_key = key;
2156 goto again;
2157 }
2158 kfree(name);
2159 if (IS_ERR(log_di)) {
2160 ret = PTR_ERR(log_di);
2161 goto out;
2162 }
2163 cur += this_len;
2164 di = (struct btrfs_dir_item *)((char *)di + this_len);
2165 }
2166 }
2167 ret = btrfs_next_leaf(root, path);
2168 if (ret > 0)
2169 ret = 0;
2170 else if (ret == 0)
2171 goto process_leaf;
2172out:
2173 btrfs_free_path(log_path);
2174 btrfs_release_path(path);
2175 return ret;
2176}
2177
2178
Chris Masone02119d2008-09-05 16:13:11 -04002179/*
2180 * deletion replay happens before we copy any new directory items
2181 * out of the log or out of backreferences from inodes. It
2182 * scans the log to find ranges of keys that log is authoritative for,
2183 * and then scans the directory to find items in those ranges that are
2184 * not present in the log.
2185 *
2186 * Anything we don't find in the log is unlinked and removed from the
2187 * directory.
2188 */
2189static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2190 struct btrfs_root *root,
2191 struct btrfs_root *log,
2192 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002193 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002194{
2195 u64 range_start;
2196 u64 range_end;
2197 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2198 int ret = 0;
2199 struct btrfs_key dir_key;
2200 struct btrfs_key found_key;
2201 struct btrfs_path *log_path;
2202 struct inode *dir;
2203
2204 dir_key.objectid = dirid;
2205 dir_key.type = BTRFS_DIR_ITEM_KEY;
2206 log_path = btrfs_alloc_path();
2207 if (!log_path)
2208 return -ENOMEM;
2209
2210 dir = read_one_inode(root, dirid);
2211 /* it isn't an error if the inode isn't there, that can happen
2212 * because we replay the deletes before we copy in the inode item
2213 * from the log
2214 */
2215 if (!dir) {
2216 btrfs_free_path(log_path);
2217 return 0;
2218 }
2219again:
2220 range_start = 0;
2221 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002222 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002223 if (del_all)
2224 range_end = (u64)-1;
2225 else {
2226 ret = find_dir_range(log, path, dirid, key_type,
2227 &range_start, &range_end);
2228 if (ret != 0)
2229 break;
2230 }
Chris Masone02119d2008-09-05 16:13:11 -04002231
2232 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002233 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002234 int nritems;
2235 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2236 0, 0);
2237 if (ret < 0)
2238 goto out;
2239
2240 nritems = btrfs_header_nritems(path->nodes[0]);
2241 if (path->slots[0] >= nritems) {
2242 ret = btrfs_next_leaf(root, path);
2243 if (ret)
2244 break;
2245 }
2246 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2247 path->slots[0]);
2248 if (found_key.objectid != dirid ||
2249 found_key.type != dir_key.type)
2250 goto next_type;
2251
2252 if (found_key.offset > range_end)
2253 break;
2254
2255 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002256 log_path, dir,
2257 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002258 if (ret)
2259 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002260 if (found_key.offset == (u64)-1)
2261 break;
2262 dir_key.offset = found_key.offset + 1;
2263 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002264 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002265 if (range_end == (u64)-1)
2266 break;
2267 range_start = range_end + 1;
2268 }
2269
2270next_type:
2271 ret = 0;
2272 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2273 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2274 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002275 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002276 goto again;
2277 }
2278out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002279 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002280 btrfs_free_path(log_path);
2281 iput(dir);
2282 return ret;
2283}
2284
2285/*
2286 * the process_func used to replay items from the log tree. This
2287 * gets called in two different stages. The first stage just looks
2288 * for inodes and makes sure they are all copied into the subvolume.
2289 *
2290 * The second stage copies all the other item types from the log into
2291 * the subvolume. The two stage approach is slower, but gets rid of
2292 * lots of complexity around inodes referencing other inodes that exist
2293 * only in the log (references come from either directory items or inode
2294 * back refs).
2295 */
2296static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2297 struct walk_control *wc, u64 gen)
2298{
2299 int nritems;
2300 struct btrfs_path *path;
2301 struct btrfs_root *root = wc->replay_dest;
2302 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002303 int level;
2304 int i;
2305 int ret;
2306
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002307 ret = btrfs_read_buffer(eb, gen);
2308 if (ret)
2309 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002310
2311 level = btrfs_header_level(eb);
2312
2313 if (level != 0)
2314 return 0;
2315
2316 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002317 if (!path)
2318 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002319
2320 nritems = btrfs_header_nritems(eb);
2321 for (i = 0; i < nritems; i++) {
2322 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002323
2324 /* inode keys are done during the first stage */
2325 if (key.type == BTRFS_INODE_ITEM_KEY &&
2326 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002327 struct btrfs_inode_item *inode_item;
2328 u32 mode;
2329
2330 inode_item = btrfs_item_ptr(eb, i,
2331 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002332 ret = replay_xattr_deletes(wc->trans, root, log,
2333 path, key.objectid);
2334 if (ret)
2335 break;
Chris Masone02119d2008-09-05 16:13:11 -04002336 mode = btrfs_inode_mode(eb, inode_item);
2337 if (S_ISDIR(mode)) {
2338 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002339 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002340 if (ret)
2341 break;
Chris Masone02119d2008-09-05 16:13:11 -04002342 }
2343 ret = overwrite_item(wc->trans, root, path,
2344 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002345 if (ret)
2346 break;
Chris Masone02119d2008-09-05 16:13:11 -04002347
Yan, Zhengc71bf092009-11-12 09:34:40 +00002348 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002349 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002350 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002351 */
2352 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002353 ret = insert_orphan_item(wc->trans, root,
2354 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002355 if (ret)
2356 break;
Chris Masone02119d2008-09-05 16:13:11 -04002357 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002358
Chris Masone02119d2008-09-05 16:13:11 -04002359 ret = link_to_fixup_dir(wc->trans, root,
2360 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002361 if (ret)
2362 break;
Chris Masone02119d2008-09-05 16:13:11 -04002363 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002364
2365 if (key.type == BTRFS_DIR_INDEX_KEY &&
2366 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2367 ret = replay_one_dir_item(wc->trans, root, path,
2368 eb, i, &key);
2369 if (ret)
2370 break;
2371 }
2372
Chris Masone02119d2008-09-05 16:13:11 -04002373 if (wc->stage < LOG_WALK_REPLAY_ALL)
2374 continue;
2375
2376 /* these keys are simply copied */
2377 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2378 ret = overwrite_item(wc->trans, root, path,
2379 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002380 if (ret)
2381 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002382 } else if (key.type == BTRFS_INODE_REF_KEY ||
2383 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002384 ret = add_inode_ref(wc->trans, root, log, path,
2385 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002386 if (ret && ret != -ENOENT)
2387 break;
2388 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002389 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2390 ret = replay_one_extent(wc->trans, root, path,
2391 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002392 if (ret)
2393 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002394 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002395 ret = replay_one_dir_item(wc->trans, root, path,
2396 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002397 if (ret)
2398 break;
Chris Masone02119d2008-09-05 16:13:11 -04002399 }
2400 }
2401 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002402 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002403}
2404
Chris Masond3977122009-01-05 21:25:51 -05002405static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002406 struct btrfs_root *root,
2407 struct btrfs_path *path, int *level,
2408 struct walk_control *wc)
2409{
2410 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002411 u64 bytenr;
2412 u64 ptr_gen;
2413 struct extent_buffer *next;
2414 struct extent_buffer *cur;
2415 struct extent_buffer *parent;
2416 u32 blocksize;
2417 int ret = 0;
2418
2419 WARN_ON(*level < 0);
2420 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2421
Chris Masond3977122009-01-05 21:25:51 -05002422 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002423 WARN_ON(*level < 0);
2424 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2425 cur = path->nodes[*level];
2426
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302427 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002428
2429 if (path->slots[*level] >=
2430 btrfs_header_nritems(cur))
2431 break;
2432
2433 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2434 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002435 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002436
2437 parent = path->nodes[*level];
2438 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002439
David Sterbaa83fffb2014-06-15 02:39:54 +02002440 next = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002441 if (IS_ERR(next))
2442 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002443
Chris Masone02119d2008-09-05 16:13:11 -04002444 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002445 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002446 if (ret) {
2447 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002448 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002449 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002450
Chris Masone02119d2008-09-05 16:13:11 -04002451 path->slots[*level]++;
2452 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002453 ret = btrfs_read_buffer(next, ptr_gen);
2454 if (ret) {
2455 free_extent_buffer(next);
2456 return ret;
2457 }
Chris Masone02119d2008-09-05 16:13:11 -04002458
Josef Bacik681ae502013-10-07 15:11:00 -04002459 if (trans) {
2460 btrfs_tree_lock(next);
2461 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002462 clean_tree_block(trans, root->fs_info,
2463 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002464 btrfs_wait_tree_block_writeback(next);
2465 btrfs_tree_unlock(next);
2466 }
Chris Masone02119d2008-09-05 16:13:11 -04002467
Chris Masone02119d2008-09-05 16:13:11 -04002468 WARN_ON(root_owner !=
2469 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002470 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002471 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002472 if (ret) {
2473 free_extent_buffer(next);
2474 return ret;
2475 }
Chris Masone02119d2008-09-05 16:13:11 -04002476 }
2477 free_extent_buffer(next);
2478 continue;
2479 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002480 ret = btrfs_read_buffer(next, ptr_gen);
2481 if (ret) {
2482 free_extent_buffer(next);
2483 return ret;
2484 }
Chris Masone02119d2008-09-05 16:13:11 -04002485
2486 WARN_ON(*level <= 0);
2487 if (path->nodes[*level-1])
2488 free_extent_buffer(path->nodes[*level-1]);
2489 path->nodes[*level-1] = next;
2490 *level = btrfs_header_level(next);
2491 path->slots[*level] = 0;
2492 cond_resched();
2493 }
2494 WARN_ON(*level < 0);
2495 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2496
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002497 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002498
2499 cond_resched();
2500 return 0;
2501}
2502
Chris Masond3977122009-01-05 21:25:51 -05002503static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002504 struct btrfs_root *root,
2505 struct btrfs_path *path, int *level,
2506 struct walk_control *wc)
2507{
2508 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002509 int i;
2510 int slot;
2511 int ret;
2512
Chris Masond3977122009-01-05 21:25:51 -05002513 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002514 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002515 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002516 path->slots[i]++;
2517 *level = i;
2518 WARN_ON(*level == 0);
2519 return 0;
2520 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 struct extent_buffer *parent;
2522 if (path->nodes[*level] == root->node)
2523 parent = path->nodes[*level];
2524 else
2525 parent = path->nodes[*level + 1];
2526
2527 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002528 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002529 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002530 if (ret)
2531 return ret;
2532
Chris Masone02119d2008-09-05 16:13:11 -04002533 if (wc->free) {
2534 struct extent_buffer *next;
2535
2536 next = path->nodes[*level];
2537
Josef Bacik681ae502013-10-07 15:11:00 -04002538 if (trans) {
2539 btrfs_tree_lock(next);
2540 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002541 clean_tree_block(trans, root->fs_info,
2542 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002543 btrfs_wait_tree_block_writeback(next);
2544 btrfs_tree_unlock(next);
2545 }
Chris Masone02119d2008-09-05 16:13:11 -04002546
Chris Masone02119d2008-09-05 16:13:11 -04002547 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002548 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002549 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002550 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002551 if (ret)
2552 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002553 }
2554 free_extent_buffer(path->nodes[*level]);
2555 path->nodes[*level] = NULL;
2556 *level = i + 1;
2557 }
2558 }
2559 return 1;
2560}
2561
2562/*
2563 * drop the reference count on the tree rooted at 'snap'. This traverses
2564 * the tree freeing any blocks that have a ref count of zero after being
2565 * decremented.
2566 */
2567static int walk_log_tree(struct btrfs_trans_handle *trans,
2568 struct btrfs_root *log, struct walk_control *wc)
2569{
2570 int ret = 0;
2571 int wret;
2572 int level;
2573 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002574 int orig_level;
2575
2576 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002577 if (!path)
2578 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002579
2580 level = btrfs_header_level(log->node);
2581 orig_level = level;
2582 path->nodes[level] = log->node;
2583 extent_buffer_get(log->node);
2584 path->slots[level] = 0;
2585
Chris Masond3977122009-01-05 21:25:51 -05002586 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002587 wret = walk_down_log_tree(trans, log, path, &level, wc);
2588 if (wret > 0)
2589 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002590 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002591 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002592 goto out;
2593 }
Chris Masone02119d2008-09-05 16:13:11 -04002594
2595 wret = walk_up_log_tree(trans, log, path, &level, wc);
2596 if (wret > 0)
2597 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002598 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002599 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002600 goto out;
2601 }
Chris Masone02119d2008-09-05 16:13:11 -04002602 }
2603
2604 /* was the root node processed? if not, catch it here */
2605 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002606 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002607 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002608 if (ret)
2609 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002610 if (wc->free) {
2611 struct extent_buffer *next;
2612
2613 next = path->nodes[orig_level];
2614
Josef Bacik681ae502013-10-07 15:11:00 -04002615 if (trans) {
2616 btrfs_tree_lock(next);
2617 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002618 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002619 btrfs_wait_tree_block_writeback(next);
2620 btrfs_tree_unlock(next);
2621 }
Chris Masone02119d2008-09-05 16:13:11 -04002622
Chris Masone02119d2008-09-05 16:13:11 -04002623 WARN_ON(log->root_key.objectid !=
2624 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002625 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002626 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002627 if (ret)
2628 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002629 }
2630 }
2631
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002632out:
Chris Masone02119d2008-09-05 16:13:11 -04002633 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002634 return ret;
2635}
2636
Yan Zheng7237f182009-01-21 12:54:03 -05002637/*
2638 * helper function to update the item for a given subvolumes log root
2639 * in the tree of log roots
2640 */
2641static int update_log_root(struct btrfs_trans_handle *trans,
2642 struct btrfs_root *log)
2643{
2644 int ret;
2645
2646 if (log->log_transid == 1) {
2647 /* insert root item on the first sync */
2648 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2649 &log->root_key, &log->root_item);
2650 } else {
2651 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2652 &log->root_key, &log->root_item);
2653 }
2654 return ret;
2655}
2656
Zhaolei60d53eb2015-08-17 18:44:46 +08002657static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002658{
2659 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002660 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002661
Yan Zheng7237f182009-01-21 12:54:03 -05002662 /*
2663 * we only allow two pending log transactions at a time,
2664 * so we know that if ours is more than 2 older than the
2665 * current transaction, we're done
2666 */
Chris Masone02119d2008-09-05 16:13:11 -04002667 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002668 prepare_to_wait(&root->log_commit_wait[index],
2669 &wait, TASK_UNINTERRUPTIBLE);
2670 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002671
Miao Xied1433de2014-02-20 18:08:59 +08002672 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002673 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002674 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002675
Yan Zheng7237f182009-01-21 12:54:03 -05002676 finish_wait(&root->log_commit_wait[index], &wait);
2677 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002678 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002679 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002680}
2681
Zhaolei60d53eb2015-08-17 18:44:46 +08002682static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002683{
2684 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002685
2686 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002687 prepare_to_wait(&root->log_writer_wait,
2688 &wait, TASK_UNINTERRUPTIBLE);
2689 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002690 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002691 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002692 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002693 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002694 }
Chris Masone02119d2008-09-05 16:13:11 -04002695}
2696
Miao Xie8b050d32014-02-20 18:08:58 +08002697static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2698 struct btrfs_log_ctx *ctx)
2699{
2700 if (!ctx)
2701 return;
2702
2703 mutex_lock(&root->log_mutex);
2704 list_del_init(&ctx->list);
2705 mutex_unlock(&root->log_mutex);
2706}
2707
2708/*
2709 * Invoked in log mutex context, or be sure there is no other task which
2710 * can access the list.
2711 */
2712static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2713 int index, int error)
2714{
2715 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002716 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002717
Chris Mason570dd452016-10-27 10:42:20 -07002718 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2719 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002720 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002721 }
Miao Xie8b050d32014-02-20 18:08:58 +08002722
2723 INIT_LIST_HEAD(&root->log_ctxs[index]);
2724}
2725
Chris Masone02119d2008-09-05 16:13:11 -04002726/*
2727 * btrfs_sync_log does sends a given tree log down to the disk and
2728 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002729 * you know that any inodes previously logged are safely on disk only
2730 * if it returns 0.
2731 *
2732 * Any other return value means you need to call btrfs_commit_transaction.
2733 * Some of the edge cases for fsyncing directories that have had unlinks
2734 * or renames done in the past mean that sometimes the only safe
2735 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2736 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002737 */
2738int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002739 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002740{
Yan Zheng7237f182009-01-21 12:54:03 -05002741 int index1;
2742 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002743 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002744 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002745 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002746 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002747 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002748 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002749 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002750
Yan Zheng7237f182009-01-21 12:54:03 -05002751 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002752 log_transid = ctx->log_transid;
2753 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002754 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002755 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002756 }
Miao Xied1433de2014-02-20 18:08:59 +08002757
2758 index1 = log_transid % 2;
2759 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002760 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002761 mutex_unlock(&root->log_mutex);
2762 return ctx->log_ret;
2763 }
2764 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002765 atomic_set(&root->log_commit[index1], 1);
2766
2767 /* wait for previous tree log sync to complete */
2768 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002769 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002770
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002771 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002772 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002773 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002774 if (!btrfs_test_opt(root->fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002775 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002776 mutex_unlock(&root->log_mutex);
2777 schedule_timeout_uninterruptible(1);
2778 mutex_lock(&root->log_mutex);
2779 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002780 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002781 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002782 break;
2783 }
Chris Masond0c803c2008-09-11 16:17:57 -04002784
Chris Mason12fcfd22009-03-24 10:24:20 -04002785 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002786 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002787 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002788 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002789 mutex_unlock(&root->log_mutex);
2790 goto out;
2791 }
2792
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002793 if (log_transid % 2 == 0)
2794 mark = EXTENT_DIRTY;
2795 else
2796 mark = EXTENT_NEW;
2797
Chris Mason690587d2009-10-13 13:29:19 -04002798 /* we start IO on all the marked extents here, but we don't actually
2799 * wait for them until later.
2800 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002801 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002802 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002803 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002804 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002805 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002806 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002807 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002808 mutex_unlock(&root->log_mutex);
2809 goto out;
2810 }
Yan Zheng7237f182009-01-21 12:54:03 -05002811
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002812 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002813
Yan Zheng7237f182009-01-21 12:54:03 -05002814 root->log_transid++;
2815 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002816 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002817 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002818 * IO has been started, blocks of the log tree have WRITTEN flag set
2819 * in their headers. new modifications of the log will be written to
2820 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002821 */
2822 mutex_unlock(&root->log_mutex);
2823
Filipe Manana28a23592016-08-23 21:13:51 +01002824 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002825
Yan Zheng7237f182009-01-21 12:54:03 -05002826 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002827 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002828 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002829
2830 index2 = log_root_tree->log_transid % 2;
2831 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2832 root_log_ctx.log_transid = log_root_tree->log_transid;
2833
Yan Zheng7237f182009-01-21 12:54:03 -05002834 mutex_unlock(&log_root_tree->log_mutex);
2835
2836 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002837
2838 mutex_lock(&log_root_tree->log_mutex);
2839 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002840 /*
2841 * Implicit memory barrier after atomic_dec_and_test
2842 */
Yan Zheng7237f182009-01-21 12:54:03 -05002843 if (waitqueue_active(&log_root_tree->log_writer_wait))
2844 wake_up(&log_root_tree->log_writer_wait);
2845 }
2846
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002847 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002848 if (!list_empty(&root_log_ctx.list))
2849 list_del_init(&root_log_ctx.list);
2850
Miao Xiec6adc9c2013-05-28 10:05:39 +00002851 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002852 btrfs_set_log_full_commit(root->fs_info, trans);
2853
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002854 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002855 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002856 mutex_unlock(&log_root_tree->log_mutex);
2857 goto out;
2858 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002859 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002860 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002861 mutex_unlock(&log_root_tree->log_mutex);
2862 ret = -EAGAIN;
2863 goto out;
2864 }
2865
Miao Xied1433de2014-02-20 18:08:59 +08002866 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002867 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002868 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002869 mutex_unlock(&log_root_tree->log_mutex);
2870 ret = root_log_ctx.log_ret;
2871 goto out;
2872 }
Miao Xie8b050d32014-02-20 18:08:58 +08002873
Miao Xied1433de2014-02-20 18:08:59 +08002874 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002875 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002876 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002877 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2878 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002879 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002880 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002881 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002882 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002883 if (!ret)
2884 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002885 goto out;
2886 }
Miao Xied1433de2014-02-20 18:08:59 +08002887 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002888 atomic_set(&log_root_tree->log_commit[index2], 1);
2889
Chris Mason12fcfd22009-03-24 10:24:20 -04002890 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002891 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002892 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002893 }
Yan Zheng7237f182009-01-21 12:54:03 -05002894
Zhaolei60d53eb2015-08-17 18:44:46 +08002895 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002896
2897 /*
2898 * now that we've moved on to the tree of log tree roots,
2899 * check the full commit flag again
2900 */
Miao Xie995946d2014-04-02 19:51:06 +08002901 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002902 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002903 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002904 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002905 mutex_unlock(&log_root_tree->log_mutex);
2906 ret = -EAGAIN;
2907 goto out_wake_log_root;
2908 }
Yan Zheng7237f182009-01-21 12:54:03 -05002909
Miao Xiec6adc9c2013-05-28 10:05:39 +00002910 ret = btrfs_write_marked_extents(log_root_tree,
2911 &log_root_tree->dirty_log_pages,
2912 EXTENT_DIRTY | EXTENT_NEW);
2913 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002914 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002915 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002916 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002917 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002918 mutex_unlock(&log_root_tree->log_mutex);
2919 goto out_wake_log_root;
2920 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002921 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2922 if (!ret)
2923 ret = btrfs_wait_marked_extents(log_root_tree,
2924 &log_root_tree->dirty_log_pages,
2925 EXTENT_NEW | EXTENT_DIRTY);
2926 if (ret) {
2927 btrfs_set_log_full_commit(root->fs_info, trans);
2928 btrfs_free_logged_extents(log, log_transid);
2929 mutex_unlock(&log_root_tree->log_mutex);
2930 goto out_wake_log_root;
2931 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002932 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002933
David Sterba6c417612011-04-13 15:41:04 +02002934 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002935 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002936 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002937 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002938
Yan Zheng7237f182009-01-21 12:54:03 -05002939 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002940 mutex_unlock(&log_root_tree->log_mutex);
2941
2942 /*
2943 * nobody else is going to jump in and write the the ctree
2944 * super here because the log_commit atomic below is protecting
2945 * us. We must be called with a transaction handle pinning
2946 * the running transaction open, so a full commit can't hop
2947 * in and cause problems either.
2948 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002949 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002950 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002951 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002952 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002953 goto out_wake_log_root;
2954 }
Yan Zheng7237f182009-01-21 12:54:03 -05002955
Chris Mason257c62e2009-10-13 13:21:08 -04002956 mutex_lock(&root->log_mutex);
2957 if (root->last_log_commit < log_transid)
2958 root->last_log_commit = log_transid;
2959 mutex_unlock(&root->log_mutex);
2960
Chris Mason12fcfd22009-03-24 10:24:20 -04002961out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002962 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002963 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2964
Miao Xied1433de2014-02-20 18:08:59 +08002965 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002966 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002967 mutex_unlock(&log_root_tree->log_mutex);
2968
David Sterba33a9eca2015-10-10 18:35:10 +02002969 /*
2970 * The barrier before waitqueue_active is implied by mutex_unlock
2971 */
Yan Zheng7237f182009-01-21 12:54:03 -05002972 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2973 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002974out:
Miao Xied1433de2014-02-20 18:08:59 +08002975 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002976 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002977 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002978 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002979 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002980
David Sterba33a9eca2015-10-10 18:35:10 +02002981 /*
2982 * The barrier before waitqueue_active is implied by mutex_unlock
2983 */
Yan Zheng7237f182009-01-21 12:54:03 -05002984 if (waitqueue_active(&root->log_commit_wait[index1]))
2985 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002986 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002987}
2988
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002989static void free_log_tree(struct btrfs_trans_handle *trans,
2990 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002991{
2992 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002993 u64 start;
2994 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002995 struct walk_control wc = {
2996 .free = 1,
2997 .process_func = process_one_buffer
2998 };
2999
Josef Bacik681ae502013-10-07 15:11:00 -04003000 ret = walk_log_tree(trans, log, &wc);
3001 /* I don't think this can happen but just in case */
3002 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003003 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003004
Chris Masond3977122009-01-05 21:25:51 -05003005 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003006 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04003007 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
3008 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003009 if (ret)
3010 break;
3011
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003012 clear_extent_bits(&log->dirty_log_pages, start, end,
David Sterba91166212016-04-26 23:54:39 +02003013 EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04003014 }
3015
Josef Bacik2ab28f32012-10-12 15:27:49 -04003016 /*
3017 * We may have short-circuited the log tree with the full commit logic
3018 * and left ordered extents on our list, so clear these out to keep us
3019 * from leaking inodes and memory.
3020 */
3021 btrfs_free_logged_extents(log, 0);
3022 btrfs_free_logged_extents(log, 1);
3023
Yan Zheng7237f182009-01-21 12:54:03 -05003024 free_extent_buffer(log->node);
3025 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003026}
3027
3028/*
3029 * free all the extents used by the tree log. This should be called
3030 * at commit time of the full transaction
3031 */
3032int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3033{
3034 if (root->log_root) {
3035 free_log_tree(trans, root->log_root);
3036 root->log_root = NULL;
3037 }
3038 return 0;
3039}
3040
3041int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3042 struct btrfs_fs_info *fs_info)
3043{
3044 if (fs_info->log_root_tree) {
3045 free_log_tree(trans, fs_info->log_root_tree);
3046 fs_info->log_root_tree = NULL;
3047 }
Chris Masone02119d2008-09-05 16:13:11 -04003048 return 0;
3049}
3050
3051/*
Chris Masone02119d2008-09-05 16:13:11 -04003052 * If both a file and directory are logged, and unlinks or renames are
3053 * mixed in, we have a few interesting corners:
3054 *
3055 * create file X in dir Y
3056 * link file X to X.link in dir Y
3057 * fsync file X
3058 * unlink file X but leave X.link
3059 * fsync dir Y
3060 *
3061 * After a crash we would expect only X.link to exist. But file X
3062 * didn't get fsync'd again so the log has back refs for X and X.link.
3063 *
3064 * We solve this by removing directory entries and inode backrefs from the
3065 * log when a file that was logged in the current transaction is
3066 * unlinked. Any later fsync will include the updated log entries, and
3067 * we'll be able to reconstruct the proper directory items from backrefs.
3068 *
3069 * This optimizations allows us to avoid relogging the entire inode
3070 * or the entire directory.
3071 */
3072int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3073 struct btrfs_root *root,
3074 const char *name, int name_len,
3075 struct inode *dir, u64 index)
3076{
3077 struct btrfs_root *log;
3078 struct btrfs_dir_item *di;
3079 struct btrfs_path *path;
3080 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003081 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003082 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003083 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003084
Chris Mason3a5f1d42008-09-11 15:53:37 -04003085 if (BTRFS_I(dir)->logged_trans < trans->transid)
3086 return 0;
3087
Chris Masone02119d2008-09-05 16:13:11 -04003088 ret = join_running_log_trans(root);
3089 if (ret)
3090 return 0;
3091
3092 mutex_lock(&BTRFS_I(dir)->log_mutex);
3093
3094 log = root->log_root;
3095 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003096 if (!path) {
3097 err = -ENOMEM;
3098 goto out_unlock;
3099 }
liubo2a29edc2011-01-26 06:22:08 +00003100
Li Zefan33345d012011-04-20 10:31:50 +08003101 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003102 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003103 if (IS_ERR(di)) {
3104 err = PTR_ERR(di);
3105 goto fail;
3106 }
3107 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003108 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3109 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003110 if (ret) {
3111 err = ret;
3112 goto fail;
3113 }
Chris Masone02119d2008-09-05 16:13:11 -04003114 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003115 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003116 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003117 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003118 if (IS_ERR(di)) {
3119 err = PTR_ERR(di);
3120 goto fail;
3121 }
3122 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003123 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3124 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003125 if (ret) {
3126 err = ret;
3127 goto fail;
3128 }
Chris Masone02119d2008-09-05 16:13:11 -04003129 }
3130
3131 /* update the directory size in the log to reflect the names
3132 * we have removed
3133 */
3134 if (bytes_del) {
3135 struct btrfs_key key;
3136
Li Zefan33345d012011-04-20 10:31:50 +08003137 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003138 key.offset = 0;
3139 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003140 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003141
3142 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003143 if (ret < 0) {
3144 err = ret;
3145 goto fail;
3146 }
Chris Masone02119d2008-09-05 16:13:11 -04003147 if (ret == 0) {
3148 struct btrfs_inode_item *item;
3149 u64 i_size;
3150
3151 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3152 struct btrfs_inode_item);
3153 i_size = btrfs_inode_size(path->nodes[0], item);
3154 if (i_size > bytes_del)
3155 i_size -= bytes_del;
3156 else
3157 i_size = 0;
3158 btrfs_set_inode_size(path->nodes[0], item, i_size);
3159 btrfs_mark_buffer_dirty(path->nodes[0]);
3160 } else
3161 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003162 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003163 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003164fail:
Chris Masone02119d2008-09-05 16:13:11 -04003165 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003166out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003167 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003168 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003169 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003170 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003171 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003172 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003173
Chris Mason12fcfd22009-03-24 10:24:20 -04003174 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003175
Andi Kleen411fc6b2010-10-29 15:14:31 -04003176 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003177}
3178
3179/* see comments for btrfs_del_dir_entries_in_log */
3180int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3181 struct btrfs_root *root,
3182 const char *name, int name_len,
3183 struct inode *inode, u64 dirid)
3184{
3185 struct btrfs_root *log;
3186 u64 index;
3187 int ret;
3188
Chris Mason3a5f1d42008-09-11 15:53:37 -04003189 if (BTRFS_I(inode)->logged_trans < trans->transid)
3190 return 0;
3191
Chris Masone02119d2008-09-05 16:13:11 -04003192 ret = join_running_log_trans(root);
3193 if (ret)
3194 return 0;
3195 log = root->log_root;
3196 mutex_lock(&BTRFS_I(inode)->log_mutex);
3197
Li Zefan33345d012011-04-20 10:31:50 +08003198 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003199 dirid, &index);
3200 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003201 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003202 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003203 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003204 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003205 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003206 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003207
Chris Masone02119d2008-09-05 16:13:11 -04003208 return ret;
3209}
3210
3211/*
3212 * creates a range item in the log for 'dirid'. first_offset and
3213 * last_offset tell us which parts of the key space the log should
3214 * be considered authoritative for.
3215 */
3216static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3217 struct btrfs_root *log,
3218 struct btrfs_path *path,
3219 int key_type, u64 dirid,
3220 u64 first_offset, u64 last_offset)
3221{
3222 int ret;
3223 struct btrfs_key key;
3224 struct btrfs_dir_log_item *item;
3225
3226 key.objectid = dirid;
3227 key.offset = first_offset;
3228 if (key_type == BTRFS_DIR_ITEM_KEY)
3229 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3230 else
3231 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3232 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003233 if (ret)
3234 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003235
3236 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3237 struct btrfs_dir_log_item);
3238 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3239 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003240 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003241 return 0;
3242}
3243
3244/*
3245 * log all the items included in the current transaction for a given
3246 * directory. This also creates the range items in the log tree required
3247 * to replay anything deleted before the fsync
3248 */
3249static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3250 struct btrfs_root *root, struct inode *inode,
3251 struct btrfs_path *path,
3252 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003253 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003254 u64 min_offset, u64 *last_offset_ret)
3255{
3256 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003257 struct btrfs_root *log = root->log_root;
3258 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003259 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003260 int ret;
3261 int i;
3262 int nritems;
3263 u64 first_offset = min_offset;
3264 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003265 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003266
3267 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003268
Li Zefan33345d012011-04-20 10:31:50 +08003269 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003270 min_key.type = key_type;
3271 min_key.offset = min_offset;
3272
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003273 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003274
3275 /*
3276 * we didn't find anything from this transaction, see if there
3277 * is anything at all
3278 */
Li Zefan33345d012011-04-20 10:31:50 +08003279 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3280 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003281 min_key.type = key_type;
3282 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003283 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003284 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3285 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003286 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003287 return ret;
3288 }
Li Zefan33345d012011-04-20 10:31:50 +08003289 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003290
3291 /* if ret == 0 there are items for this type,
3292 * create a range to tell us the last key of this type.
3293 * otherwise, there are no items in this directory after
3294 * *min_offset, and we create a range to indicate that.
3295 */
3296 if (ret == 0) {
3297 struct btrfs_key tmp;
3298 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3299 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003300 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003301 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003302 }
3303 goto done;
3304 }
3305
3306 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003307 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003308 if (ret == 0) {
3309 struct btrfs_key tmp;
3310 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3311 if (key_type == tmp.type) {
3312 first_offset = tmp.offset;
3313 ret = overwrite_item(trans, log, dst_path,
3314 path->nodes[0], path->slots[0],
3315 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003316 if (ret) {
3317 err = ret;
3318 goto done;
3319 }
Chris Masone02119d2008-09-05 16:13:11 -04003320 }
3321 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003322 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003323
3324 /* find the first key from this transaction again */
3325 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303326 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003327 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003328
3329 /*
3330 * we have a block from this transaction, log every item in it
3331 * from our directory
3332 */
Chris Masond3977122009-01-05 21:25:51 -05003333 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003334 struct btrfs_key tmp;
3335 src = path->nodes[0];
3336 nritems = btrfs_header_nritems(src);
3337 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003338 struct btrfs_dir_item *di;
3339
Chris Masone02119d2008-09-05 16:13:11 -04003340 btrfs_item_key_to_cpu(src, &min_key, i);
3341
Li Zefan33345d012011-04-20 10:31:50 +08003342 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003343 goto done;
3344 ret = overwrite_item(trans, log, dst_path, src, i,
3345 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003346 if (ret) {
3347 err = ret;
3348 goto done;
3349 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003350
3351 /*
3352 * We must make sure that when we log a directory entry,
3353 * the corresponding inode, after log replay, has a
3354 * matching link count. For example:
3355 *
3356 * touch foo
3357 * mkdir mydir
3358 * sync
3359 * ln foo mydir/bar
3360 * xfs_io -c "fsync" mydir
3361 * <crash>
3362 * <mount fs and log replay>
3363 *
3364 * Would result in a fsync log that when replayed, our
3365 * file inode would have a link count of 1, but we get
3366 * two directory entries pointing to the same inode.
3367 * After removing one of the names, it would not be
3368 * possible to remove the other name, which resulted
3369 * always in stale file handle errors, and would not
3370 * be possible to rmdir the parent directory, since
3371 * its i_size could never decrement to the value
3372 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3373 */
3374 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3375 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3376 if (ctx &&
3377 (btrfs_dir_transid(src, di) == trans->transid ||
3378 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3379 tmp.type != BTRFS_ROOT_ITEM_KEY)
3380 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003381 }
3382 path->slots[0] = nritems;
3383
3384 /*
3385 * look ahead to the next item and see if it is also
3386 * from this directory and from this transaction
3387 */
3388 ret = btrfs_next_leaf(root, path);
3389 if (ret == 1) {
3390 last_offset = (u64)-1;
3391 goto done;
3392 }
3393 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003394 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003395 last_offset = (u64)-1;
3396 goto done;
3397 }
3398 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3399 ret = overwrite_item(trans, log, dst_path,
3400 path->nodes[0], path->slots[0],
3401 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003402 if (ret)
3403 err = ret;
3404 else
3405 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003406 goto done;
3407 }
3408 }
3409done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003410 btrfs_release_path(path);
3411 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003412
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003413 if (err == 0) {
3414 *last_offset_ret = last_offset;
3415 /*
3416 * insert the log range keys to indicate where the log
3417 * is valid
3418 */
3419 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003420 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003421 if (ret)
3422 err = ret;
3423 }
3424 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003425}
3426
3427/*
3428 * logging directories is very similar to logging inodes, We find all the items
3429 * from the current transaction and write them to the log.
3430 *
3431 * The recovery code scans the directory in the subvolume, and if it finds a
3432 * key in the range logged that is not present in the log tree, then it means
3433 * that dir entry was unlinked during the transaction.
3434 *
3435 * In order for that scan to work, we must include one key smaller than
3436 * the smallest logged by this transaction and one key larger than the largest
3437 * key logged by this transaction.
3438 */
3439static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3440 struct btrfs_root *root, struct inode *inode,
3441 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003442 struct btrfs_path *dst_path,
3443 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003444{
3445 u64 min_key;
3446 u64 max_key;
3447 int ret;
3448 int key_type = BTRFS_DIR_ITEM_KEY;
3449
3450again:
3451 min_key = 0;
3452 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003453 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003454 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003455 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003456 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003457 if (ret)
3458 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003459 if (max_key == (u64)-1)
3460 break;
3461 min_key = max_key + 1;
3462 }
3463
3464 if (key_type == BTRFS_DIR_ITEM_KEY) {
3465 key_type = BTRFS_DIR_INDEX_KEY;
3466 goto again;
3467 }
3468 return 0;
3469}
3470
3471/*
3472 * a helper function to drop items from the log before we relog an
3473 * inode. max_key_type indicates the highest item type to remove.
3474 * This cannot be run for file data extents because it does not
3475 * free the extents they point to.
3476 */
3477static int drop_objectid_items(struct btrfs_trans_handle *trans,
3478 struct btrfs_root *log,
3479 struct btrfs_path *path,
3480 u64 objectid, int max_key_type)
3481{
3482 int ret;
3483 struct btrfs_key key;
3484 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003485 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003486
3487 key.objectid = objectid;
3488 key.type = max_key_type;
3489 key.offset = (u64)-1;
3490
Chris Masond3977122009-01-05 21:25:51 -05003491 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003492 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003493 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003494 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003495 break;
3496
3497 if (path->slots[0] == 0)
3498 break;
3499
3500 path->slots[0]--;
3501 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3502 path->slots[0]);
3503
3504 if (found_key.objectid != objectid)
3505 break;
3506
Josef Bacik18ec90d2012-09-28 11:56:28 -04003507 found_key.offset = 0;
3508 found_key.type = 0;
3509 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3510 &start_slot);
3511
3512 ret = btrfs_del_items(trans, log, path, start_slot,
3513 path->slots[0] - start_slot + 1);
3514 /*
3515 * If start slot isn't 0 then we don't need to re-search, we've
3516 * found the last guy with the objectid in this tree.
3517 */
3518 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003519 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003520 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003521 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003522 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003523 if (ret > 0)
3524 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003525 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003526}
3527
Josef Bacik94edf4a2012-09-25 14:56:25 -04003528static void fill_inode_item(struct btrfs_trans_handle *trans,
3529 struct extent_buffer *leaf,
3530 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003531 struct inode *inode, int log_inode_only,
3532 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003533{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003534 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003535
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003536 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003537
3538 if (log_inode_only) {
3539 /* set the generation to zero so the recover code
3540 * can tell the difference between an logging
3541 * just to say 'this inode exists' and a logging
3542 * to say 'update this inode with these values'
3543 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003544 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003545 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003546 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003547 btrfs_set_token_inode_generation(leaf, item,
3548 BTRFS_I(inode)->generation,
3549 &token);
3550 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003551 }
3552
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003553 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3554 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3555 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3556 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3557
David Sterbaa937b972014-12-12 17:39:12 +01003558 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003559 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003560 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003561 inode->i_atime.tv_nsec, &token);
3562
David Sterbaa937b972014-12-12 17:39:12 +01003563 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003564 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003565 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003566 inode->i_mtime.tv_nsec, &token);
3567
David Sterbaa937b972014-12-12 17:39:12 +01003568 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003569 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003570 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003571 inode->i_ctime.tv_nsec, &token);
3572
3573 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3574 &token);
3575
3576 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3577 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3578 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3579 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3580 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003581}
3582
Josef Bacika95249b2012-10-11 16:17:34 -04003583static int log_inode_item(struct btrfs_trans_handle *trans,
3584 struct btrfs_root *log, struct btrfs_path *path,
3585 struct inode *inode)
3586{
3587 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003588 int ret;
3589
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003590 ret = btrfs_insert_empty_item(trans, log, path,
3591 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003592 sizeof(*inode_item));
3593 if (ret && ret != -EEXIST)
3594 return ret;
3595 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3596 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003597 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003598 btrfs_release_path(path);
3599 return 0;
3600}
3601
Chris Mason31ff1cd2008-09-11 16:17:57 -04003602static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003603 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003604 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003605 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003606 int start_slot, int nr, int inode_only,
3607 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003608{
3609 unsigned long src_offset;
3610 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003611 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003612 struct btrfs_file_extent_item *extent;
3613 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003614 struct extent_buffer *src = src_path->nodes[0];
3615 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003616 int ret;
3617 struct btrfs_key *ins_keys;
3618 u32 *ins_sizes;
3619 char *ins_data;
3620 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003621 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003622 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003623 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003624 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003625 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003626
3627 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003628
3629 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3630 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003631 if (!ins_data)
3632 return -ENOMEM;
3633
Josef Bacik16e75492013-10-22 12:18:51 -04003634 first_key.objectid = (u64)-1;
3635
Chris Mason31ff1cd2008-09-11 16:17:57 -04003636 ins_sizes = (u32 *)ins_data;
3637 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3638
3639 for (i = 0; i < nr; i++) {
3640 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3641 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3642 }
3643 ret = btrfs_insert_empty_items(trans, log, dst_path,
3644 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003645 if (ret) {
3646 kfree(ins_data);
3647 return ret;
3648 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003650 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003651 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3652 dst_path->slots[0]);
3653
3654 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3655
Josef Bacik16e75492013-10-22 12:18:51 -04003656 if ((i == (nr - 1)))
3657 last_key = ins_keys[i];
3658
Josef Bacik94edf4a2012-09-25 14:56:25 -04003659 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003660 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3661 dst_path->slots[0],
3662 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003663 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003664 inode, inode_only == LOG_INODE_EXISTS,
3665 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003666 } else {
3667 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3668 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003669 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003670
Josef Bacik16e75492013-10-22 12:18:51 -04003671 /*
3672 * We set need_find_last_extent here in case we know we were
3673 * processing other items and then walk into the first extent in
3674 * the inode. If we don't hit an extent then nothing changes,
3675 * we'll do the last search the next time around.
3676 */
3677 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3678 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003679 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003680 first_key = ins_keys[i];
3681 } else {
3682 need_find_last_extent = false;
3683 }
3684
Chris Mason31ff1cd2008-09-11 16:17:57 -04003685 /* take a reference on file data extents so that truncates
3686 * or deletes of this inode don't have to relog the inode
3687 * again
3688 */
David Sterba962a2982014-06-04 18:41:45 +02003689 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003690 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003691 int found_type;
3692 extent = btrfs_item_ptr(src, start_slot + i,
3693 struct btrfs_file_extent_item);
3694
liubo8e531cd2011-05-06 10:36:09 +08003695 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3696 continue;
3697
Chris Mason31ff1cd2008-09-11 16:17:57 -04003698 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003699 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003700 u64 ds, dl, cs, cl;
3701 ds = btrfs_file_extent_disk_bytenr(src,
3702 extent);
3703 /* ds == 0 is a hole */
3704 if (ds == 0)
3705 continue;
3706
3707 dl = btrfs_file_extent_disk_num_bytes(src,
3708 extent);
3709 cs = btrfs_file_extent_offset(src, extent);
3710 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003711 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003712 if (btrfs_file_extent_compression(src,
3713 extent)) {
3714 cs = 0;
3715 cl = dl;
3716 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003717
3718 ret = btrfs_lookup_csums_range(
3719 log->fs_info->csum_root,
3720 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003721 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003722 if (ret) {
3723 btrfs_release_path(dst_path);
3724 kfree(ins_data);
3725 return ret;
3726 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003727 }
3728 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003729 }
3730
3731 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003732 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003733 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003734
3735 /*
3736 * we have to do this after the loop above to avoid changing the
3737 * log tree while trying to change the log tree.
3738 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003739 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003740 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003741 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3742 struct btrfs_ordered_sum,
3743 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003744 if (!ret)
3745 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003746 list_del(&sums->list);
3747 kfree(sums);
3748 }
Josef Bacik16e75492013-10-22 12:18:51 -04003749
3750 if (!has_extents)
3751 return ret;
3752
Filipe Manana74121f7c2014-08-07 12:00:44 +01003753 if (need_find_last_extent && *last_extent == first_key.offset) {
3754 /*
3755 * We don't have any leafs between our current one and the one
3756 * we processed before that can have file extent items for our
3757 * inode (and have a generation number smaller than our current
3758 * transaction id).
3759 */
3760 need_find_last_extent = false;
3761 }
3762
Josef Bacik16e75492013-10-22 12:18:51 -04003763 /*
3764 * Because we use btrfs_search_forward we could skip leaves that were
3765 * not modified and then assume *last_extent is valid when it really
3766 * isn't. So back up to the previous leaf and read the end of the last
3767 * extent before we go and fill in holes.
3768 */
3769 if (need_find_last_extent) {
3770 u64 len;
3771
3772 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3773 if (ret < 0)
3774 return ret;
3775 if (ret)
3776 goto fill_holes;
3777 if (src_path->slots[0])
3778 src_path->slots[0]--;
3779 src = src_path->nodes[0];
3780 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3781 if (key.objectid != btrfs_ino(inode) ||
3782 key.type != BTRFS_EXTENT_DATA_KEY)
3783 goto fill_holes;
3784 extent = btrfs_item_ptr(src, src_path->slots[0],
3785 struct btrfs_file_extent_item);
3786 if (btrfs_file_extent_type(src, extent) ==
3787 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003788 len = btrfs_file_extent_inline_len(src,
3789 src_path->slots[0],
3790 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003791 *last_extent = ALIGN(key.offset + len,
3792 log->sectorsize);
3793 } else {
3794 len = btrfs_file_extent_num_bytes(src, extent);
3795 *last_extent = key.offset + len;
3796 }
3797 }
3798fill_holes:
3799 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3800 * things could have happened
3801 *
3802 * 1) A merge could have happened, so we could currently be on a leaf
3803 * that holds what we were copying in the first place.
3804 * 2) A split could have happened, and now not all of the items we want
3805 * are on the same leaf.
3806 *
3807 * So we need to adjust how we search for holes, we need to drop the
3808 * path and re-search for the first extent key we found, and then walk
3809 * forward until we hit the last one we copied.
3810 */
3811 if (need_find_last_extent) {
3812 /* btrfs_prev_leaf could return 1 without releasing the path */
3813 btrfs_release_path(src_path);
3814 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3815 src_path, 0, 0);
3816 if (ret < 0)
3817 return ret;
3818 ASSERT(ret == 0);
3819 src = src_path->nodes[0];
3820 i = src_path->slots[0];
3821 } else {
3822 i = start_slot;
3823 }
3824
3825 /*
3826 * Ok so here we need to go through and fill in any holes we may have
3827 * to make sure that holes are punched for those areas in case they had
3828 * extents previously.
3829 */
3830 while (!done) {
3831 u64 offset, len;
3832 u64 extent_end;
3833
3834 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3835 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3836 if (ret < 0)
3837 return ret;
3838 ASSERT(ret == 0);
3839 src = src_path->nodes[0];
3840 i = 0;
3841 }
3842
3843 btrfs_item_key_to_cpu(src, &key, i);
3844 if (!btrfs_comp_cpu_keys(&key, &last_key))
3845 done = true;
3846 if (key.objectid != btrfs_ino(inode) ||
3847 key.type != BTRFS_EXTENT_DATA_KEY) {
3848 i++;
3849 continue;
3850 }
3851 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3852 if (btrfs_file_extent_type(src, extent) ==
3853 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003854 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003855 extent_end = ALIGN(key.offset + len, log->sectorsize);
3856 } else {
3857 len = btrfs_file_extent_num_bytes(src, extent);
3858 extent_end = key.offset + len;
3859 }
3860 i++;
3861
3862 if (*last_extent == key.offset) {
3863 *last_extent = extent_end;
3864 continue;
3865 }
3866 offset = *last_extent;
3867 len = key.offset - *last_extent;
3868 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3869 offset, 0, 0, len, 0, len, 0,
3870 0, 0);
3871 if (ret)
3872 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003873 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003874 }
3875 /*
3876 * Need to let the callers know we dropped the path so they should
3877 * re-search.
3878 */
3879 if (!ret && need_find_last_extent)
3880 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003881 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003882}
3883
Josef Bacik5dc562c2012-08-17 13:14:17 -04003884static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3885{
3886 struct extent_map *em1, *em2;
3887
3888 em1 = list_entry(a, struct extent_map, list);
3889 em2 = list_entry(b, struct extent_map, list);
3890
3891 if (em1->start < em2->start)
3892 return -1;
3893 else if (em1->start > em2->start)
3894 return 1;
3895 return 0;
3896}
3897
Filipe Manana8407f552014-09-05 15:14:39 +01003898static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3899 struct inode *inode,
3900 struct btrfs_root *root,
3901 const struct extent_map *em,
3902 const struct list_head *logged_list,
3903 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003904{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003905 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003906 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003907 u64 mod_start = em->mod_start;
3908 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003909 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003910 u64 csum_offset;
3911 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003912 LIST_HEAD(ordered_sums);
3913 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003914
Filipe Manana8407f552014-09-05 15:14:39 +01003915 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003916
Filipe Manana8407f552014-09-05 15:14:39 +01003917 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3918 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003919 return 0;
3920
Josef Bacik2ab28f32012-10-12 15:27:49 -04003921 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003922 * Wait far any ordered extent that covers our extent map. If it
3923 * finishes without an error, first check and see if our csums are on
3924 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003925 */
Miao Xie827463c2014-01-14 20:31:51 +08003926 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003927 struct btrfs_ordered_sum *sum;
3928
3929 if (!mod_len)
3930 break;
3931
Josef Bacik2ab28f32012-10-12 15:27:49 -04003932 if (ordered->file_offset + ordered->len <= mod_start ||
3933 mod_start + mod_len <= ordered->file_offset)
3934 continue;
3935
Filipe Manana8407f552014-09-05 15:14:39 +01003936 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3937 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3938 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3939 const u64 start = ordered->file_offset;
3940 const u64 end = ordered->file_offset + ordered->len - 1;
3941
3942 WARN_ON(ordered->inode != inode);
3943 filemap_fdatawrite_range(inode->i_mapping, start, end);
3944 }
3945
3946 wait_event(ordered->wait,
3947 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3948 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3949
3950 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003951 /*
3952 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3953 * i_mapping flags, so that the next fsync won't get
3954 * an outdated io error too.
3955 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003956 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003957 *ordered_io_error = true;
3958 break;
3959 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003960 /*
3961 * We are going to copy all the csums on this ordered extent, so
3962 * go ahead and adjust mod_start and mod_len in case this
3963 * ordered extent has already been logged.
3964 */
3965 if (ordered->file_offset > mod_start) {
3966 if (ordered->file_offset + ordered->len >=
3967 mod_start + mod_len)
3968 mod_len = ordered->file_offset - mod_start;
3969 /*
3970 * If we have this case
3971 *
3972 * |--------- logged extent ---------|
3973 * |----- ordered extent ----|
3974 *
3975 * Just don't mess with mod_start and mod_len, we'll
3976 * just end up logging more csums than we need and it
3977 * will be ok.
3978 */
3979 } else {
3980 if (ordered->file_offset + ordered->len <
3981 mod_start + mod_len) {
3982 mod_len = (mod_start + mod_len) -
3983 (ordered->file_offset + ordered->len);
3984 mod_start = ordered->file_offset +
3985 ordered->len;
3986 } else {
3987 mod_len = 0;
3988 }
3989 }
3990
Filipe Manana8407f552014-09-05 15:14:39 +01003991 if (skip_csum)
3992 continue;
3993
Josef Bacik2ab28f32012-10-12 15:27:49 -04003994 /*
3995 * To keep us from looping for the above case of an ordered
3996 * extent that falls inside of the logged extent.
3997 */
3998 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3999 &ordered->flags))
4000 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004001
Josef Bacik2ab28f32012-10-12 15:27:49 -04004002 list_for_each_entry(sum, &ordered->list, list) {
4003 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004004 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004005 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004006 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004007 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004008
Filipe Manana8407f552014-09-05 15:14:39 +01004009 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004010 return ret;
4011
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004012 if (em->compress_type) {
4013 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004014 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004015 } else {
4016 csum_offset = mod_start - em->start;
4017 csum_len = mod_len;
4018 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004019
Josef Bacik70c8a912012-10-11 16:54:30 -04004020 /* block start is already adjusted for the file extent offset. */
4021 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4022 em->block_start + csum_offset,
4023 em->block_start + csum_offset +
4024 csum_len - 1, &ordered_sums, 0);
4025 if (ret)
4026 return ret;
4027
4028 while (!list_empty(&ordered_sums)) {
4029 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4030 struct btrfs_ordered_sum,
4031 list);
4032 if (!ret)
4033 ret = btrfs_csum_file_blocks(trans, log, sums);
4034 list_del(&sums->list);
4035 kfree(sums);
4036 }
4037
4038 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004039}
4040
Filipe Manana8407f552014-09-05 15:14:39 +01004041static int log_one_extent(struct btrfs_trans_handle *trans,
4042 struct inode *inode, struct btrfs_root *root,
4043 const struct extent_map *em,
4044 struct btrfs_path *path,
4045 const struct list_head *logged_list,
4046 struct btrfs_log_ctx *ctx)
4047{
4048 struct btrfs_root *log = root->log_root;
4049 struct btrfs_file_extent_item *fi;
4050 struct extent_buffer *leaf;
4051 struct btrfs_map_token token;
4052 struct btrfs_key key;
4053 u64 extent_offset = em->start - em->orig_start;
4054 u64 block_len;
4055 int ret;
4056 int extent_inserted = 0;
4057 bool ordered_io_err = false;
4058
4059 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4060 &ordered_io_err);
4061 if (ret)
4062 return ret;
4063
4064 if (ordered_io_err) {
4065 ctx->io_err = -EIO;
4066 return 0;
4067 }
4068
4069 btrfs_init_map_token(&token);
4070
4071 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4072 em->start + em->len, NULL, 0, 1,
4073 sizeof(*fi), &extent_inserted);
4074 if (ret)
4075 return ret;
4076
4077 if (!extent_inserted) {
4078 key.objectid = btrfs_ino(inode);
4079 key.type = BTRFS_EXTENT_DATA_KEY;
4080 key.offset = em->start;
4081
4082 ret = btrfs_insert_empty_item(trans, log, path, &key,
4083 sizeof(*fi));
4084 if (ret)
4085 return ret;
4086 }
4087 leaf = path->nodes[0];
4088 fi = btrfs_item_ptr(leaf, path->slots[0],
4089 struct btrfs_file_extent_item);
4090
Josef Bacik50d9aa92014-11-21 14:52:38 -05004091 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004092 &token);
4093 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4094 btrfs_set_token_file_extent_type(leaf, fi,
4095 BTRFS_FILE_EXTENT_PREALLOC,
4096 &token);
4097 else
4098 btrfs_set_token_file_extent_type(leaf, fi,
4099 BTRFS_FILE_EXTENT_REG,
4100 &token);
4101
4102 block_len = max(em->block_len, em->orig_block_len);
4103 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4104 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4105 em->block_start,
4106 &token);
4107 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4108 &token);
4109 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4110 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4111 em->block_start -
4112 extent_offset, &token);
4113 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4114 &token);
4115 } else {
4116 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4117 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4118 &token);
4119 }
4120
4121 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4122 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4123 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4124 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4125 &token);
4126 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4127 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4128 btrfs_mark_buffer_dirty(leaf);
4129
4130 btrfs_release_path(path);
4131
4132 return ret;
4133}
4134
Josef Bacik5dc562c2012-08-17 13:14:17 -04004135static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4136 struct btrfs_root *root,
4137 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004138 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004139 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004140 struct btrfs_log_ctx *ctx,
4141 const u64 start,
4142 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004143{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004144 struct extent_map *em, *n;
4145 struct list_head extents;
4146 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4147 u64 test_gen;
4148 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004149 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004150
4151 INIT_LIST_HEAD(&extents);
4152
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004153 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004154 write_lock(&tree->lock);
4155 test_gen = root->fs_info->last_trans_committed;
4156
4157 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4158 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004159
4160 /*
4161 * Just an arbitrary number, this can be really CPU intensive
4162 * once we start getting a lot of extents, and really once we
4163 * have a bunch of extents we just want to commit since it will
4164 * be faster.
4165 */
4166 if (++num > 32768) {
4167 list_del_init(&tree->modified_extents);
4168 ret = -EFBIG;
4169 goto process;
4170 }
4171
Josef Bacik5dc562c2012-08-17 13:14:17 -04004172 if (em->generation <= test_gen)
4173 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004174 /* Need a ref to keep it from getting evicted from cache */
4175 atomic_inc(&em->refs);
4176 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004177 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004178 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004179 }
4180
4181 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004182 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004183 /*
4184 * Some ordered extents started by fsync might have completed
4185 * before we could collect them into the list logged_list, which
4186 * means they're gone, not in our logged_list nor in the inode's
4187 * ordered tree. We want the application/user space to know an
4188 * error happened while attempting to persist file data so that
4189 * it can take proper action. If such error happened, we leave
4190 * without writing to the log tree and the fsync must report the
4191 * file data write error and not commit the current transaction.
4192 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004193 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004194 if (ret)
4195 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004196process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004197 while (!list_empty(&extents)) {
4198 em = list_entry(extents.next, struct extent_map, list);
4199
4200 list_del_init(&em->list);
4201
4202 /*
4203 * If we had an error we just need to delete everybody from our
4204 * private list.
4205 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004206 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004207 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004208 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004209 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004210 }
4211
4212 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004213
Filipe Manana8407f552014-09-05 15:14:39 +01004214 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4215 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004216 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004217 clear_em_logging(tree, em);
4218 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004219 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004220 WARN_ON(!list_empty(&extents));
4221 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004222 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004223
Josef Bacik5dc562c2012-08-17 13:14:17 -04004224 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004225 return ret;
4226}
4227
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004228static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4229 struct btrfs_path *path, u64 *size_ret)
4230{
4231 struct btrfs_key key;
4232 int ret;
4233
4234 key.objectid = btrfs_ino(inode);
4235 key.type = BTRFS_INODE_ITEM_KEY;
4236 key.offset = 0;
4237
4238 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4239 if (ret < 0) {
4240 return ret;
4241 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004242 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004243 } else {
4244 struct btrfs_inode_item *item;
4245
4246 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4247 struct btrfs_inode_item);
4248 *size_ret = btrfs_inode_size(path->nodes[0], item);
4249 }
4250
4251 btrfs_release_path(path);
4252 return 0;
4253}
4254
Filipe Manana36283bf2015-06-20 00:44:51 +01004255/*
4256 * At the moment we always log all xattrs. This is to figure out at log replay
4257 * time which xattrs must have their deletion replayed. If a xattr is missing
4258 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4259 * because if a xattr is deleted, the inode is fsynced and a power failure
4260 * happens, causing the log to be replayed the next time the fs is mounted,
4261 * we want the xattr to not exist anymore (same behaviour as other filesystems
4262 * with a journal, ext3/4, xfs, f2fs, etc).
4263 */
4264static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4265 struct btrfs_root *root,
4266 struct inode *inode,
4267 struct btrfs_path *path,
4268 struct btrfs_path *dst_path)
4269{
4270 int ret;
4271 struct btrfs_key key;
4272 const u64 ino = btrfs_ino(inode);
4273 int ins_nr = 0;
4274 int start_slot = 0;
4275
4276 key.objectid = ino;
4277 key.type = BTRFS_XATTR_ITEM_KEY;
4278 key.offset = 0;
4279
4280 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4281 if (ret < 0)
4282 return ret;
4283
4284 while (true) {
4285 int slot = path->slots[0];
4286 struct extent_buffer *leaf = path->nodes[0];
4287 int nritems = btrfs_header_nritems(leaf);
4288
4289 if (slot >= nritems) {
4290 if (ins_nr > 0) {
4291 u64 last_extent = 0;
4292
4293 ret = copy_items(trans, inode, dst_path, path,
4294 &last_extent, start_slot,
4295 ins_nr, 1, 0);
4296 /* can't be 1, extent items aren't processed */
4297 ASSERT(ret <= 0);
4298 if (ret < 0)
4299 return ret;
4300 ins_nr = 0;
4301 }
4302 ret = btrfs_next_leaf(root, path);
4303 if (ret < 0)
4304 return ret;
4305 else if (ret > 0)
4306 break;
4307 continue;
4308 }
4309
4310 btrfs_item_key_to_cpu(leaf, &key, slot);
4311 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4312 break;
4313
4314 if (ins_nr == 0)
4315 start_slot = slot;
4316 ins_nr++;
4317 path->slots[0]++;
4318 cond_resched();
4319 }
4320 if (ins_nr > 0) {
4321 u64 last_extent = 0;
4322
4323 ret = copy_items(trans, inode, dst_path, path,
4324 &last_extent, start_slot,
4325 ins_nr, 1, 0);
4326 /* can't be 1, extent items aren't processed */
4327 ASSERT(ret <= 0);
4328 if (ret < 0)
4329 return ret;
4330 }
4331
4332 return 0;
4333}
4334
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004335/*
4336 * If the no holes feature is enabled we need to make sure any hole between the
4337 * last extent and the i_size of our inode is explicitly marked in the log. This
4338 * is to make sure that doing something like:
4339 *
4340 * 1) create file with 128Kb of data
4341 * 2) truncate file to 64Kb
4342 * 3) truncate file to 256Kb
4343 * 4) fsync file
4344 * 5) <crash/power failure>
4345 * 6) mount fs and trigger log replay
4346 *
4347 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4348 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4349 * file correspond to a hole. The presence of explicit holes in a log tree is
4350 * what guarantees that log replay will remove/adjust file extent items in the
4351 * fs/subvol tree.
4352 *
4353 * Here we do not need to care about holes between extents, that is already done
4354 * by copy_items(). We also only need to do this in the full sync path, where we
4355 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4356 * lookup the list of modified extent maps and if any represents a hole, we
4357 * insert a corresponding extent representing a hole in the log tree.
4358 */
4359static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4360 struct btrfs_root *root,
4361 struct inode *inode,
4362 struct btrfs_path *path)
4363{
4364 int ret;
4365 struct btrfs_key key;
4366 u64 hole_start;
4367 u64 hole_size;
4368 struct extent_buffer *leaf;
4369 struct btrfs_root *log = root->log_root;
4370 const u64 ino = btrfs_ino(inode);
4371 const u64 i_size = i_size_read(inode);
4372
4373 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4374 return 0;
4375
4376 key.objectid = ino;
4377 key.type = BTRFS_EXTENT_DATA_KEY;
4378 key.offset = (u64)-1;
4379
4380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4381 ASSERT(ret != 0);
4382 if (ret < 0)
4383 return ret;
4384
4385 ASSERT(path->slots[0] > 0);
4386 path->slots[0]--;
4387 leaf = path->nodes[0];
4388 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4389
4390 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4391 /* inode does not have any extents */
4392 hole_start = 0;
4393 hole_size = i_size;
4394 } else {
4395 struct btrfs_file_extent_item *extent;
4396 u64 len;
4397
4398 /*
4399 * If there's an extent beyond i_size, an explicit hole was
4400 * already inserted by copy_items().
4401 */
4402 if (key.offset >= i_size)
4403 return 0;
4404
4405 extent = btrfs_item_ptr(leaf, path->slots[0],
4406 struct btrfs_file_extent_item);
4407
4408 if (btrfs_file_extent_type(leaf, extent) ==
4409 BTRFS_FILE_EXTENT_INLINE) {
4410 len = btrfs_file_extent_inline_len(leaf,
4411 path->slots[0],
4412 extent);
4413 ASSERT(len == i_size);
4414 return 0;
4415 }
4416
4417 len = btrfs_file_extent_num_bytes(leaf, extent);
4418 /* Last extent goes beyond i_size, no need to log a hole. */
4419 if (key.offset + len > i_size)
4420 return 0;
4421 hole_start = key.offset + len;
4422 hole_size = i_size - hole_start;
4423 }
4424 btrfs_release_path(path);
4425
4426 /* Last extent ends at i_size. */
4427 if (hole_size == 0)
4428 return 0;
4429
4430 hole_size = ALIGN(hole_size, root->sectorsize);
4431 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4432 hole_size, 0, hole_size, 0, 0, 0);
4433 return ret;
4434}
4435
Filipe Manana56f23fd2016-03-30 23:37:21 +01004436/*
4437 * When we are logging a new inode X, check if it doesn't have a reference that
4438 * matches the reference from some other inode Y created in a past transaction
4439 * and that was renamed in the current transaction. If we don't do this, then at
4440 * log replay time we can lose inode Y (and all its files if it's a directory):
4441 *
4442 * mkdir /mnt/x
4443 * echo "hello world" > /mnt/x/foobar
4444 * sync
4445 * mv /mnt/x /mnt/y
4446 * mkdir /mnt/x # or touch /mnt/x
4447 * xfs_io -c fsync /mnt/x
4448 * <power fail>
4449 * mount fs, trigger log replay
4450 *
4451 * After the log replay procedure, we would lose the first directory and all its
4452 * files (file foobar).
4453 * For the case where inode Y is not a directory we simply end up losing it:
4454 *
4455 * echo "123" > /mnt/foo
4456 * sync
4457 * mv /mnt/foo /mnt/bar
4458 * echo "abc" > /mnt/foo
4459 * xfs_io -c fsync /mnt/foo
4460 * <power fail>
4461 *
4462 * We also need this for cases where a snapshot entry is replaced by some other
4463 * entry (file or directory) otherwise we end up with an unreplayable log due to
4464 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4465 * if it were a regular entry:
4466 *
4467 * mkdir /mnt/x
4468 * btrfs subvolume snapshot /mnt /mnt/x/snap
4469 * btrfs subvolume delete /mnt/x/snap
4470 * rmdir /mnt/x
4471 * mkdir /mnt/x
4472 * fsync /mnt/x or fsync some new file inside it
4473 * <power fail>
4474 *
4475 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4476 * the same transaction.
4477 */
4478static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4479 const int slot,
4480 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004481 struct inode *inode,
4482 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004483{
4484 int ret;
4485 struct btrfs_path *search_path;
4486 char *name = NULL;
4487 u32 name_len = 0;
4488 u32 item_size = btrfs_item_size_nr(eb, slot);
4489 u32 cur_offset = 0;
4490 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4491
4492 search_path = btrfs_alloc_path();
4493 if (!search_path)
4494 return -ENOMEM;
4495 search_path->search_commit_root = 1;
4496 search_path->skip_locking = 1;
4497
4498 while (cur_offset < item_size) {
4499 u64 parent;
4500 u32 this_name_len;
4501 u32 this_len;
4502 unsigned long name_ptr;
4503 struct btrfs_dir_item *di;
4504
4505 if (key->type == BTRFS_INODE_REF_KEY) {
4506 struct btrfs_inode_ref *iref;
4507
4508 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4509 parent = key->offset;
4510 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4511 name_ptr = (unsigned long)(iref + 1);
4512 this_len = sizeof(*iref) + this_name_len;
4513 } else {
4514 struct btrfs_inode_extref *extref;
4515
4516 extref = (struct btrfs_inode_extref *)(ptr +
4517 cur_offset);
4518 parent = btrfs_inode_extref_parent(eb, extref);
4519 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4520 name_ptr = (unsigned long)&extref->name;
4521 this_len = sizeof(*extref) + this_name_len;
4522 }
4523
4524 if (this_name_len > name_len) {
4525 char *new_name;
4526
4527 new_name = krealloc(name, this_name_len, GFP_NOFS);
4528 if (!new_name) {
4529 ret = -ENOMEM;
4530 goto out;
4531 }
4532 name_len = this_name_len;
4533 name = new_name;
4534 }
4535
4536 read_extent_buffer(eb, name, name_ptr, this_name_len);
4537 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4538 search_path, parent,
4539 name, this_name_len, 0);
4540 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004541 struct btrfs_key di_key;
4542
4543 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4544 di, &di_key);
4545 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4546 ret = 1;
4547 *other_ino = di_key.objectid;
4548 } else {
4549 ret = -EAGAIN;
4550 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004551 goto out;
4552 } else if (IS_ERR(di)) {
4553 ret = PTR_ERR(di);
4554 goto out;
4555 }
4556 btrfs_release_path(search_path);
4557
4558 cur_offset += this_len;
4559 }
4560 ret = 0;
4561out:
4562 btrfs_free_path(search_path);
4563 kfree(name);
4564 return ret;
4565}
4566
Chris Masone02119d2008-09-05 16:13:11 -04004567/* log a single inode in the tree log.
4568 * At least one parent directory for this inode must exist in the tree
4569 * or be logged already.
4570 *
4571 * Any items from this inode changed by the current transaction are copied
4572 * to the log tree. An extra reference is taken on any extents in this
4573 * file, allowing us to avoid a whole pile of corner cases around logging
4574 * blocks that have been removed from the tree.
4575 *
4576 * See LOG_INODE_ALL and related defines for a description of what inode_only
4577 * does.
4578 *
4579 * This handles both files and directories.
4580 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004581static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004582 struct btrfs_root *root, struct inode *inode,
4583 int inode_only,
4584 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004585 const loff_t end,
4586 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004587{
4588 struct btrfs_path *path;
4589 struct btrfs_path *dst_path;
4590 struct btrfs_key min_key;
4591 struct btrfs_key max_key;
4592 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004593 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004594 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004595 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004596 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004597 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004598 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004599 int ins_start_slot = 0;
4600 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004601 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004602 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004603 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004604 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004605 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004606
Chris Masone02119d2008-09-05 16:13:11 -04004607 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004608 if (!path)
4609 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004610 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004611 if (!dst_path) {
4612 btrfs_free_path(path);
4613 return -ENOMEM;
4614 }
Chris Masone02119d2008-09-05 16:13:11 -04004615
Li Zefan33345d012011-04-20 10:31:50 +08004616 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004617 min_key.type = BTRFS_INODE_ITEM_KEY;
4618 min_key.offset = 0;
4619
Li Zefan33345d012011-04-20 10:31:50 +08004620 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004621
Chris Mason12fcfd22009-03-24 10:24:20 -04004622
Josef Bacik5dc562c2012-08-17 13:14:17 -04004623 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004624 if (S_ISDIR(inode->i_mode) ||
4625 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4626 &BTRFS_I(inode)->runtime_flags) &&
4627 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004628 max_key.type = BTRFS_XATTR_ITEM_KEY;
4629 else
4630 max_key.type = (u8)-1;
4631 max_key.offset = (u64)-1;
4632
Filipe Manana2c2c4522015-01-13 16:40:04 +00004633 /*
4634 * Only run delayed items if we are a dir or a new file.
4635 * Otherwise commit the delayed inode only, which is needed in
4636 * order for the log replay code to mark inodes for link count
4637 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4638 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004639 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004640 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004641 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004642 else
4643 ret = btrfs_commit_inode_delayed_inode(inode);
4644
4645 if (ret) {
4646 btrfs_free_path(path);
4647 btrfs_free_path(dst_path);
4648 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004649 }
4650
Chris Masone02119d2008-09-05 16:13:11 -04004651 mutex_lock(&BTRFS_I(inode)->log_mutex);
4652
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004653 /*
Chris Masone02119d2008-09-05 16:13:11 -04004654 * a brute force approach to making sure we get the most uptodate
4655 * copies of everything.
4656 */
4657 if (S_ISDIR(inode->i_mode)) {
4658 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4659
Filipe Manana4f764e52015-02-23 19:53:35 +00004660 if (inode_only == LOG_INODE_EXISTS)
4661 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004662 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004663 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004664 if (inode_only == LOG_INODE_EXISTS) {
4665 /*
4666 * Make sure the new inode item we write to the log has
4667 * the same isize as the current one (if it exists).
4668 * This is necessary to prevent data loss after log
4669 * replay, and also to prevent doing a wrong expanding
4670 * truncate - for e.g. create file, write 4K into offset
4671 * 0, fsync, write 4K into offset 4096, add hard link,
4672 * fsync some other file (to sync log), power fail - if
4673 * we use the inode's current i_size, after log replay
4674 * we get a 8Kb file, with the last 4Kb extent as a hole
4675 * (zeroes), as if an expanding truncate happened,
4676 * instead of getting a file of 4Kb only.
4677 */
4678 err = logged_inode_size(log, inode, path,
4679 &logged_isize);
4680 if (err)
4681 goto out_unlock;
4682 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004683 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4684 &BTRFS_I(inode)->runtime_flags)) {
4685 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004686 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004687 ret = drop_objectid_items(trans, log, path, ino,
4688 max_key.type);
4689 } else {
4690 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4691 &BTRFS_I(inode)->runtime_flags);
4692 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4693 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004694 while(1) {
4695 ret = btrfs_truncate_inode_items(trans,
4696 log, inode, 0, 0);
4697 if (ret != -EAGAIN)
4698 break;
4699 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004700 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004701 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4702 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004703 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004704 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004705 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004706 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004707 ret = drop_objectid_items(trans, log, path, ino,
4708 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004709 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004710 if (inode_only == LOG_INODE_ALL)
4711 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004712 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004713 }
Josef Bacika95249b2012-10-11 16:17:34 -04004714
Chris Masone02119d2008-09-05 16:13:11 -04004715 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004716 if (ret) {
4717 err = ret;
4718 goto out_unlock;
4719 }
Chris Masone02119d2008-09-05 16:13:11 -04004720
Chris Masond3977122009-01-05 21:25:51 -05004721 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004722 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004723 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004724 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004725 if (ret < 0) {
4726 err = ret;
4727 goto out_unlock;
4728 }
Chris Masone02119d2008-09-05 16:13:11 -04004729 if (ret != 0)
4730 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004731again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004732 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004733 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004734 break;
4735 if (min_key.type > max_key.type)
4736 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004737
Filipe Mananae4545de2015-06-17 12:49:23 +01004738 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4739 need_log_inode_item = false;
4740
Filipe Manana56f23fd2016-03-30 23:37:21 +01004741 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4742 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4743 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004744 u64 other_ino = 0;
4745
Filipe Manana56f23fd2016-03-30 23:37:21 +01004746 ret = btrfs_check_ref_name_override(path->nodes[0],
4747 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004748 &min_key, inode,
4749 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004750 if (ret < 0) {
4751 err = ret;
4752 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004753 } else if (ret > 0 && ctx &&
4754 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004755 struct btrfs_key inode_key;
4756 struct inode *other_inode;
4757
4758 if (ins_nr > 0) {
4759 ins_nr++;
4760 } else {
4761 ins_nr = 1;
4762 ins_start_slot = path->slots[0];
4763 }
4764 ret = copy_items(trans, inode, dst_path, path,
4765 &last_extent, ins_start_slot,
4766 ins_nr, inode_only,
4767 logged_isize);
4768 if (ret < 0) {
4769 err = ret;
4770 goto out_unlock;
4771 }
4772 ins_nr = 0;
4773 btrfs_release_path(path);
4774 inode_key.objectid = other_ino;
4775 inode_key.type = BTRFS_INODE_ITEM_KEY;
4776 inode_key.offset = 0;
4777 other_inode = btrfs_iget(root->fs_info->sb,
4778 &inode_key, root,
4779 NULL);
4780 /*
4781 * If the other inode that had a conflicting dir
4782 * entry was deleted in the current transaction,
4783 * we don't need to do more work nor fallback to
4784 * a transaction commit.
4785 */
4786 if (IS_ERR(other_inode) &&
4787 PTR_ERR(other_inode) == -ENOENT) {
4788 goto next_key;
4789 } else if (IS_ERR(other_inode)) {
4790 err = PTR_ERR(other_inode);
4791 goto out_unlock;
4792 }
4793 /*
4794 * We are safe logging the other inode without
4795 * acquiring its i_mutex as long as we log with
4796 * the LOG_INODE_EXISTS mode. We're safe against
4797 * concurrent renames of the other inode as well
4798 * because during a rename we pin the log and
4799 * update the log with the new name before we
4800 * unpin it.
4801 */
4802 err = btrfs_log_inode(trans, root, other_inode,
4803 LOG_INODE_EXISTS,
4804 0, LLONG_MAX, ctx);
4805 iput(other_inode);
4806 if (err)
4807 goto out_unlock;
4808 else
4809 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004810 }
4811 }
4812
Filipe Manana36283bf2015-06-20 00:44:51 +01004813 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4814 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4815 if (ins_nr == 0)
4816 goto next_slot;
4817 ret = copy_items(trans, inode, dst_path, path,
4818 &last_extent, ins_start_slot,
4819 ins_nr, inode_only, logged_isize);
4820 if (ret < 0) {
4821 err = ret;
4822 goto out_unlock;
4823 }
4824 ins_nr = 0;
4825 if (ret) {
4826 btrfs_release_path(path);
4827 continue;
4828 }
4829 goto next_slot;
4830 }
4831
Chris Masone02119d2008-09-05 16:13:11 -04004832 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004833 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4834 ins_nr++;
4835 goto next_slot;
4836 } else if (!ins_nr) {
4837 ins_start_slot = path->slots[0];
4838 ins_nr = 1;
4839 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004840 }
4841
Josef Bacik16e75492013-10-22 12:18:51 -04004842 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004843 ins_start_slot, ins_nr, inode_only,
4844 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004845 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004846 err = ret;
4847 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004848 }
4849 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004850 ins_nr = 0;
4851 btrfs_release_path(path);
4852 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004853 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004854 ins_nr = 1;
4855 ins_start_slot = path->slots[0];
4856next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004857
Chris Mason3a5f1d42008-09-11 15:53:37 -04004858 nritems = btrfs_header_nritems(path->nodes[0]);
4859 path->slots[0]++;
4860 if (path->slots[0] < nritems) {
4861 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4862 path->slots[0]);
4863 goto again;
4864 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004865 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004866 ret = copy_items(trans, inode, dst_path, path,
4867 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004868 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004869 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004870 err = ret;
4871 goto out_unlock;
4872 }
Josef Bacik16e75492013-10-22 12:18:51 -04004873 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004874 ins_nr = 0;
4875 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004876 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004877next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004878 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004879 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004880 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004881 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004882 min_key.offset = 0;
4883 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004884 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004885 }
Chris Masone02119d2008-09-05 16:13:11 -04004886 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004887 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004888 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004889 ins_start_slot, ins_nr, inode_only,
4890 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004891 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004892 err = ret;
4893 goto out_unlock;
4894 }
Josef Bacik16e75492013-10-22 12:18:51 -04004895 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004896 ins_nr = 0;
4897 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004898
Filipe Manana36283bf2015-06-20 00:44:51 +01004899 btrfs_release_path(path);
4900 btrfs_release_path(dst_path);
4901 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4902 if (err)
4903 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004904 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4905 btrfs_release_path(path);
4906 btrfs_release_path(dst_path);
4907 err = btrfs_log_trailing_hole(trans, root, inode, path);
4908 if (err)
4909 goto out_unlock;
4910 }
Josef Bacika95249b2012-10-11 16:17:34 -04004911log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004912 btrfs_release_path(path);
4913 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004914 if (need_log_inode_item) {
4915 err = log_inode_item(trans, log, dst_path, inode);
4916 if (err)
4917 goto out_unlock;
4918 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004919 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004920 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004921 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004922 if (ret) {
4923 err = ret;
4924 goto out_unlock;
4925 }
Josef Bacikd006a042013-11-12 20:54:09 -05004926 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004927 struct extent_map *em, *n;
4928
Filipe Manana49dae1b2014-09-06 22:34:39 +01004929 write_lock(&em_tree->lock);
4930 /*
4931 * We can't just remove every em if we're called for a ranged
4932 * fsync - that is, one that doesn't cover the whole possible
4933 * file range (0 to LLONG_MAX). This is because we can have
4934 * em's that fall outside the range we're logging and therefore
4935 * their ordered operations haven't completed yet
4936 * (btrfs_finish_ordered_io() not invoked yet). This means we
4937 * didn't get their respective file extent item in the fs/subvol
4938 * tree yet, and need to let the next fast fsync (one which
4939 * consults the list of modified extent maps) find the em so
4940 * that it logs a matching file extent item and waits for the
4941 * respective ordered operation to complete (if it's still
4942 * running).
4943 *
4944 * Removing every em outside the range we're logging would make
4945 * the next fast fsync not log their matching file extent items,
4946 * therefore making us lose data after a log replay.
4947 */
4948 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4949 list) {
4950 const u64 mod_end = em->mod_start + em->mod_len - 1;
4951
4952 if (em->mod_start >= start && mod_end <= end)
4953 list_del_init(&em->list);
4954 }
4955 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004956 }
4957
Chris Mason9623f9a2008-09-11 17:42:42 -04004958 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004959 ret = log_directory_changes(trans, root, inode, path, dst_path,
4960 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004961 if (ret) {
4962 err = ret;
4963 goto out_unlock;
4964 }
Chris Masone02119d2008-09-05 16:13:11 -04004965 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004966
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004967 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01004968 BTRFS_I(inode)->logged_trans = trans->transid;
4969 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004970 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004971out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004972 if (unlikely(err))
4973 btrfs_put_logged_extents(&logged_list);
4974 else
4975 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004976 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4977
4978 btrfs_free_path(path);
4979 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004980 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004981}
4982
Chris Mason12fcfd22009-03-24 10:24:20 -04004983/*
Filipe Manana2be63d52016-02-12 11:34:23 +00004984 * Check if we must fallback to a transaction commit when logging an inode.
4985 * This must be called after logging the inode and is used only in the context
4986 * when fsyncing an inode requires the need to log some other inode - in which
4987 * case we can't lock the i_mutex of each other inode we need to log as that
4988 * can lead to deadlocks with concurrent fsync against other inodes (as we can
4989 * log inodes up or down in the hierarchy) or rename operations for example. So
4990 * we take the log_mutex of the inode after we have logged it and then check for
4991 * its last_unlink_trans value - this is safe because any task setting
4992 * last_unlink_trans must take the log_mutex and it must do this before it does
4993 * the actual unlink operation, so if we do this check before a concurrent task
4994 * sets last_unlink_trans it means we've logged a consistent version/state of
4995 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04004996 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00004997 * we logged the inode or it might have also done the unlink).
4998 */
4999static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5000 struct inode *inode)
5001{
5002 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5003 bool ret = false;
5004
5005 mutex_lock(&BTRFS_I(inode)->log_mutex);
5006 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5007 /*
5008 * Make sure any commits to the log are forced to be full
5009 * commits.
5010 */
5011 btrfs_set_log_full_commit(fs_info, trans);
5012 ret = true;
5013 }
5014 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5015
5016 return ret;
5017}
5018
5019/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005020 * follow the dentry parent pointers up the chain and see if any
5021 * of the directories in it require a full commit before they can
5022 * be logged. Returns zero if nothing special needs to be done or 1 if
5023 * a full commit is required.
5024 */
5025static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5026 struct inode *inode,
5027 struct dentry *parent,
5028 struct super_block *sb,
5029 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005030{
Chris Mason12fcfd22009-03-24 10:24:20 -04005031 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005032 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005033 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005034
Chris Masonaf4176b2009-03-24 10:24:31 -04005035 /*
5036 * for regular files, if its inode is already on disk, we don't
5037 * have to worry about the parents at all. This is because
5038 * we can use the last_unlink_trans field to record renames
5039 * and other fun in this file.
5040 */
5041 if (S_ISREG(inode->i_mode) &&
5042 BTRFS_I(inode)->generation <= last_committed &&
5043 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5044 goto out;
5045
Chris Mason12fcfd22009-03-24 10:24:20 -04005046 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005047 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005048 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005049 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005050 }
5051
5052 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005053 /*
5054 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005055 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005056 * logged_trans so that further down in the log code we don't
5057 * think this inode has already been logged.
5058 */
5059 if (inode != orig_inode)
5060 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005061 smp_mb();
5062
Filipe Manana2be63d52016-02-12 11:34:23 +00005063 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005064 ret = 1;
5065 break;
5066 }
5067
Al Virofc640052016-04-10 01:33:30 -04005068 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005069 break;
5070
Filipe Manana44f714d2016-06-06 16:11:13 +01005071 if (IS_ROOT(parent)) {
5072 inode = d_inode(parent);
5073 if (btrfs_must_commit_transaction(trans, inode))
5074 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005075 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005076 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005077
Josef Bacik6a912212010-11-20 09:48:00 +00005078 parent = dget_parent(parent);
5079 dput(old_parent);
5080 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005081 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005082
5083 }
Josef Bacik6a912212010-11-20 09:48:00 +00005084 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005085out:
Chris Masone02119d2008-09-05 16:13:11 -04005086 return ret;
5087}
5088
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005089struct btrfs_dir_list {
5090 u64 ino;
5091 struct list_head list;
5092};
5093
5094/*
5095 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5096 * details about the why it is needed.
5097 * This is a recursive operation - if an existing dentry corresponds to a
5098 * directory, that directory's new entries are logged too (same behaviour as
5099 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5100 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5101 * complains about the following circular lock dependency / possible deadlock:
5102 *
5103 * CPU0 CPU1
5104 * ---- ----
5105 * lock(&type->i_mutex_dir_key#3/2);
5106 * lock(sb_internal#2);
5107 * lock(&type->i_mutex_dir_key#3/2);
5108 * lock(&sb->s_type->i_mutex_key#14);
5109 *
5110 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5111 * sb_start_intwrite() in btrfs_start_transaction().
5112 * Not locking i_mutex of the inodes is still safe because:
5113 *
5114 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5115 * that while logging the inode new references (names) are added or removed
5116 * from the inode, leaving the logged inode item with a link count that does
5117 * not match the number of logged inode reference items. This is fine because
5118 * at log replay time we compute the real number of links and correct the
5119 * link count in the inode item (see replay_one_buffer() and
5120 * link_to_fixup_dir());
5121 *
5122 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5123 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5124 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5125 * has a size that doesn't match the sum of the lengths of all the logged
5126 * names. This does not result in a problem because if a dir_item key is
5127 * logged but its matching dir_index key is not logged, at log replay time we
5128 * don't use it to replay the respective name (see replay_one_name()). On the
5129 * other hand if only the dir_index key ends up being logged, the respective
5130 * name is added to the fs/subvol tree with both the dir_item and dir_index
5131 * keys created (see replay_one_name()).
5132 * The directory's inode item with a wrong i_size is not a problem as well,
5133 * since we don't use it at log replay time to set the i_size in the inode
5134 * item of the fs/subvol tree (see overwrite_item()).
5135 */
5136static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5137 struct btrfs_root *root,
5138 struct inode *start_inode,
5139 struct btrfs_log_ctx *ctx)
5140{
5141 struct btrfs_root *log = root->log_root;
5142 struct btrfs_path *path;
5143 LIST_HEAD(dir_list);
5144 struct btrfs_dir_list *dir_elem;
5145 int ret = 0;
5146
5147 path = btrfs_alloc_path();
5148 if (!path)
5149 return -ENOMEM;
5150
5151 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5152 if (!dir_elem) {
5153 btrfs_free_path(path);
5154 return -ENOMEM;
5155 }
5156 dir_elem->ino = btrfs_ino(start_inode);
5157 list_add_tail(&dir_elem->list, &dir_list);
5158
5159 while (!list_empty(&dir_list)) {
5160 struct extent_buffer *leaf;
5161 struct btrfs_key min_key;
5162 int nritems;
5163 int i;
5164
5165 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5166 list);
5167 if (ret)
5168 goto next_dir_inode;
5169
5170 min_key.objectid = dir_elem->ino;
5171 min_key.type = BTRFS_DIR_ITEM_KEY;
5172 min_key.offset = 0;
5173again:
5174 btrfs_release_path(path);
5175 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5176 if (ret < 0) {
5177 goto next_dir_inode;
5178 } else if (ret > 0) {
5179 ret = 0;
5180 goto next_dir_inode;
5181 }
5182
5183process_leaf:
5184 leaf = path->nodes[0];
5185 nritems = btrfs_header_nritems(leaf);
5186 for (i = path->slots[0]; i < nritems; i++) {
5187 struct btrfs_dir_item *di;
5188 struct btrfs_key di_key;
5189 struct inode *di_inode;
5190 struct btrfs_dir_list *new_dir_elem;
5191 int log_mode = LOG_INODE_EXISTS;
5192 int type;
5193
5194 btrfs_item_key_to_cpu(leaf, &min_key, i);
5195 if (min_key.objectid != dir_elem->ino ||
5196 min_key.type != BTRFS_DIR_ITEM_KEY)
5197 goto next_dir_inode;
5198
5199 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5200 type = btrfs_dir_type(leaf, di);
5201 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5202 type != BTRFS_FT_DIR)
5203 continue;
5204 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5205 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5206 continue;
5207
5208 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5209 root, NULL);
5210 if (IS_ERR(di_inode)) {
5211 ret = PTR_ERR(di_inode);
5212 goto next_dir_inode;
5213 }
5214
5215 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5216 iput(di_inode);
5217 continue;
5218 }
5219
5220 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005221 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005222 log_mode = LOG_INODE_ALL;
5223 btrfs_release_path(path);
5224 ret = btrfs_log_inode(trans, root, di_inode,
5225 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005226 if (!ret &&
5227 btrfs_must_commit_transaction(trans, di_inode))
5228 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005229 iput(di_inode);
5230 if (ret)
5231 goto next_dir_inode;
5232 if (ctx->log_new_dentries) {
5233 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5234 GFP_NOFS);
5235 if (!new_dir_elem) {
5236 ret = -ENOMEM;
5237 goto next_dir_inode;
5238 }
5239 new_dir_elem->ino = di_key.objectid;
5240 list_add_tail(&new_dir_elem->list, &dir_list);
5241 }
5242 break;
5243 }
5244 if (i == nritems) {
5245 ret = btrfs_next_leaf(log, path);
5246 if (ret < 0) {
5247 goto next_dir_inode;
5248 } else if (ret > 0) {
5249 ret = 0;
5250 goto next_dir_inode;
5251 }
5252 goto process_leaf;
5253 }
5254 if (min_key.offset < (u64)-1) {
5255 min_key.offset++;
5256 goto again;
5257 }
5258next_dir_inode:
5259 list_del(&dir_elem->list);
5260 kfree(dir_elem);
5261 }
5262
5263 btrfs_free_path(path);
5264 return ret;
5265}
5266
Filipe Manana18aa0922015-08-05 16:49:08 +01005267static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5268 struct inode *inode,
5269 struct btrfs_log_ctx *ctx)
5270{
5271 int ret;
5272 struct btrfs_path *path;
5273 struct btrfs_key key;
5274 struct btrfs_root *root = BTRFS_I(inode)->root;
5275 const u64 ino = btrfs_ino(inode);
5276
5277 path = btrfs_alloc_path();
5278 if (!path)
5279 return -ENOMEM;
5280 path->skip_locking = 1;
5281 path->search_commit_root = 1;
5282
5283 key.objectid = ino;
5284 key.type = BTRFS_INODE_REF_KEY;
5285 key.offset = 0;
5286 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5287 if (ret < 0)
5288 goto out;
5289
5290 while (true) {
5291 struct extent_buffer *leaf = path->nodes[0];
5292 int slot = path->slots[0];
5293 u32 cur_offset = 0;
5294 u32 item_size;
5295 unsigned long ptr;
5296
5297 if (slot >= btrfs_header_nritems(leaf)) {
5298 ret = btrfs_next_leaf(root, path);
5299 if (ret < 0)
5300 goto out;
5301 else if (ret > 0)
5302 break;
5303 continue;
5304 }
5305
5306 btrfs_item_key_to_cpu(leaf, &key, slot);
5307 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5308 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5309 break;
5310
5311 item_size = btrfs_item_size_nr(leaf, slot);
5312 ptr = btrfs_item_ptr_offset(leaf, slot);
5313 while (cur_offset < item_size) {
5314 struct btrfs_key inode_key;
5315 struct inode *dir_inode;
5316
5317 inode_key.type = BTRFS_INODE_ITEM_KEY;
5318 inode_key.offset = 0;
5319
5320 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5321 struct btrfs_inode_extref *extref;
5322
5323 extref = (struct btrfs_inode_extref *)
5324 (ptr + cur_offset);
5325 inode_key.objectid = btrfs_inode_extref_parent(
5326 leaf, extref);
5327 cur_offset += sizeof(*extref);
5328 cur_offset += btrfs_inode_extref_name_len(leaf,
5329 extref);
5330 } else {
5331 inode_key.objectid = key.offset;
5332 cur_offset = item_size;
5333 }
5334
5335 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5336 root, NULL);
5337 /* If parent inode was deleted, skip it. */
5338 if (IS_ERR(dir_inode))
5339 continue;
5340
Filipe Manana657ed1a2016-04-06 17:11:56 +01005341 if (ctx)
5342 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005343 ret = btrfs_log_inode(trans, root, dir_inode,
5344 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005345 if (!ret &&
5346 btrfs_must_commit_transaction(trans, dir_inode))
5347 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005348 if (!ret && ctx && ctx->log_new_dentries)
5349 ret = log_new_dir_dentries(trans, root,
5350 dir_inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005351 iput(dir_inode);
5352 if (ret)
5353 goto out;
5354 }
5355 path->slots[0]++;
5356 }
5357 ret = 0;
5358out:
5359 btrfs_free_path(path);
5360 return ret;
5361}
5362
Chris Masone02119d2008-09-05 16:13:11 -04005363/*
5364 * helper function around btrfs_log_inode to make sure newly created
5365 * parent directories also end up in the log. A minimal inode and backref
5366 * only logging is done of any parent directories that are older than
5367 * the last committed transaction
5368 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005369static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5370 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005371 struct dentry *parent,
5372 const loff_t start,
5373 const loff_t end,
5374 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005375 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005376{
Chris Mason12fcfd22009-03-24 10:24:20 -04005377 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005378 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005379 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005380 int ret = 0;
5381 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005382 bool log_dentries = false;
5383 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005384
5385 sb = inode->i_sb;
5386
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005387 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005388 ret = 1;
5389 goto end_no_trans;
5390 }
5391
Miao Xie995946d2014-04-02 19:51:06 +08005392 /*
5393 * The prev transaction commit doesn't complete, we need do
5394 * full commit by ourselves.
5395 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005396 if (root->fs_info->last_trans_log_full_commit >
5397 root->fs_info->last_trans_committed) {
5398 ret = 1;
5399 goto end_no_trans;
5400 }
5401
Yan, Zheng76dda932009-09-21 16:00:26 -04005402 if (root != BTRFS_I(inode)->root ||
5403 btrfs_root_refs(&root->root_item) == 0) {
5404 ret = 1;
5405 goto end_no_trans;
5406 }
5407
Chris Mason12fcfd22009-03-24 10:24:20 -04005408 ret = check_parent_dirs_for_sync(trans, inode, parent,
5409 sb, last_committed);
5410 if (ret)
5411 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005412
Josef Bacik22ee6982012-05-29 16:57:49 -04005413 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005414 ret = BTRFS_NO_LOG_SYNC;
5415 goto end_no_trans;
5416 }
5417
Miao Xie8b050d32014-02-20 18:08:58 +08005418 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005419 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005420 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005421
Filipe Manana8407f552014-09-05 15:14:39 +01005422 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005423 if (ret)
5424 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005425
Chris Masonaf4176b2009-03-24 10:24:31 -04005426 /*
5427 * for regular files, if its inode is already on disk, we don't
5428 * have to worry about the parents at all. This is because
5429 * we can use the last_unlink_trans field to record renames
5430 * and other fun in this file.
5431 */
5432 if (S_ISREG(inode->i_mode) &&
5433 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005434 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5435 ret = 0;
5436 goto end_trans;
5437 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005438
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005439 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5440 log_dentries = true;
5441
Filipe Manana18aa0922015-08-05 16:49:08 +01005442 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005443 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005444 * inodes are fully logged. This is to prevent leaving dangling
5445 * directory index entries in directories that were our parents but are
5446 * not anymore. Not doing this results in old parent directory being
5447 * impossible to delete after log replay (rmdir will always fail with
5448 * error -ENOTEMPTY).
5449 *
5450 * Example 1:
5451 *
5452 * mkdir testdir
5453 * touch testdir/foo
5454 * ln testdir/foo testdir/bar
5455 * sync
5456 * unlink testdir/bar
5457 * xfs_io -c fsync testdir/foo
5458 * <power failure>
5459 * mount fs, triggers log replay
5460 *
5461 * If we don't log the parent directory (testdir), after log replay the
5462 * directory still has an entry pointing to the file inode using the bar
5463 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5464 * the file inode has a link count of 1.
5465 *
5466 * Example 2:
5467 *
5468 * mkdir testdir
5469 * touch foo
5470 * ln foo testdir/foo2
5471 * ln foo testdir/foo3
5472 * sync
5473 * unlink testdir/foo3
5474 * xfs_io -c fsync foo
5475 * <power failure>
5476 * mount fs, triggers log replay
5477 *
5478 * Similar as the first example, after log replay the parent directory
5479 * testdir still has an entry pointing to the inode file with name foo3
5480 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5481 * and has a link count of 2.
5482 */
5483 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5484 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5485 if (ret)
5486 goto end_trans;
5487 }
5488
Chris Masond3977122009-01-05 21:25:51 -05005489 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005490 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005491 break;
5492
David Howells2b0143b2015-03-17 22:25:59 +00005493 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005494 if (root != BTRFS_I(inode)->root)
5495 break;
5496
Filipe Manana18aa0922015-08-05 16:49:08 +01005497 if (BTRFS_I(inode)->generation > last_committed) {
5498 ret = btrfs_log_inode(trans, root, inode,
5499 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005500 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005501 if (ret)
5502 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005503 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005504 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005505 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005506
Josef Bacik6a912212010-11-20 09:48:00 +00005507 parent = dget_parent(parent);
5508 dput(old_parent);
5509 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005510 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005511 if (log_dentries)
5512 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5513 else
5514 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005515end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005516 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005517 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005518 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005519 ret = 1;
5520 }
Miao Xie8b050d32014-02-20 18:08:58 +08005521
5522 if (ret)
5523 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005524 btrfs_end_log_trans(root);
5525end_no_trans:
5526 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005527}
5528
5529/*
5530 * it is not safe to log dentry if the chunk root has added new
5531 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5532 * If this returns 1, you must commit the transaction to safely get your
5533 * data on disk.
5534 */
5535int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005536 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005537 const loff_t start,
5538 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005539 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005540{
Josef Bacik6a912212010-11-20 09:48:00 +00005541 struct dentry *parent = dget_parent(dentry);
5542 int ret;
5543
David Howells2b0143b2015-03-17 22:25:59 +00005544 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005545 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005546 dput(parent);
5547
5548 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005549}
5550
5551/*
5552 * should be called during mount to recover any replay any log trees
5553 * from the FS
5554 */
5555int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5556{
5557 int ret;
5558 struct btrfs_path *path;
5559 struct btrfs_trans_handle *trans;
5560 struct btrfs_key key;
5561 struct btrfs_key found_key;
5562 struct btrfs_key tmp_key;
5563 struct btrfs_root *log;
5564 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5565 struct walk_control wc = {
5566 .process_func = process_one_buffer,
5567 .stage = 0,
5568 };
5569
Chris Masone02119d2008-09-05 16:13:11 -04005570 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005571 if (!path)
5572 return -ENOMEM;
5573
Josef Bacikafcdd122016-09-02 15:40:02 -04005574 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005575
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005576 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005577 if (IS_ERR(trans)) {
5578 ret = PTR_ERR(trans);
5579 goto error;
5580 }
Chris Masone02119d2008-09-05 16:13:11 -04005581
5582 wc.trans = trans;
5583 wc.pin = 1;
5584
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005585 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005586 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005587 btrfs_handle_fs_error(fs_info, ret,
5588 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005589 goto error;
5590 }
Chris Masone02119d2008-09-05 16:13:11 -04005591
5592again:
5593 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5594 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005595 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005596
Chris Masond3977122009-01-05 21:25:51 -05005597 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005598 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005599
5600 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005601 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005602 "Couldn't find tree log root.");
5603 goto error;
5604 }
Chris Masone02119d2008-09-05 16:13:11 -04005605 if (ret > 0) {
5606 if (path->slots[0] == 0)
5607 break;
5608 path->slots[0]--;
5609 }
5610 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5611 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005612 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005613 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5614 break;
5615
Miao Xiecb517ea2013-05-15 07:48:19 +00005616 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005617 if (IS_ERR(log)) {
5618 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005619 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005620 "Couldn't read tree log root.");
5621 goto error;
5622 }
Chris Masone02119d2008-09-05 16:13:11 -04005623
5624 tmp_key.objectid = found_key.offset;
5625 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5626 tmp_key.offset = (u64)-1;
5627
5628 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005629 if (IS_ERR(wc.replay_dest)) {
5630 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005631 free_extent_buffer(log->node);
5632 free_extent_buffer(log->commit_root);
5633 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005634 btrfs_handle_fs_error(fs_info, ret,
5635 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005636 goto error;
5637 }
Chris Masone02119d2008-09-05 16:13:11 -04005638
Yan Zheng07d400a2009-01-06 11:42:00 -05005639 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005640 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005641 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005642
Josef Bacikb50c6e22013-04-25 15:55:30 -04005643 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005644 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5645 path);
Chris Masone02119d2008-09-05 16:13:11 -04005646 }
Chris Masone02119d2008-09-05 16:13:11 -04005647
5648 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005649 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005650 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005651 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005652 kfree(log);
5653
Josef Bacikb50c6e22013-04-25 15:55:30 -04005654 if (ret)
5655 goto error;
5656
Chris Masone02119d2008-09-05 16:13:11 -04005657 if (found_key.offset == 0)
5658 break;
5659 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005660 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005661
5662 /* step one is to pin it all, step two is to replay just inodes */
5663 if (wc.pin) {
5664 wc.pin = 0;
5665 wc.process_func = replay_one_buffer;
5666 wc.stage = LOG_WALK_REPLAY_INODES;
5667 goto again;
5668 }
5669 /* step three is to replay everything */
5670 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5671 wc.stage++;
5672 goto again;
5673 }
5674
5675 btrfs_free_path(path);
5676
Josef Bacikabefa552013-04-24 16:40:05 -04005677 /* step 4: commit the transaction, which also unpins the blocks */
5678 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5679 if (ret)
5680 return ret;
5681
Chris Masone02119d2008-09-05 16:13:11 -04005682 free_extent_buffer(log_root_tree->node);
5683 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005684 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005685 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005686
Josef Bacikabefa552013-04-24 16:40:05 -04005687 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005688error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005689 if (wc.trans)
5690 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005691 btrfs_free_path(path);
5692 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005693}
Chris Mason12fcfd22009-03-24 10:24:20 -04005694
5695/*
5696 * there are some corner cases where we want to force a full
5697 * commit instead of allowing a directory to be logged.
5698 *
5699 * They revolve around files there were unlinked from the directory, and
5700 * this function updates the parent directory so that a full commit is
5701 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005702 *
5703 * Must be called before the unlink operations (updates to the subvolume tree,
5704 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005705 */
5706void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5707 struct inode *dir, struct inode *inode,
5708 int for_rename)
5709{
5710 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005711 * when we're logging a file, if it hasn't been renamed
5712 * or unlinked, and its inode is fully committed on disk,
5713 * we don't have to worry about walking up the directory chain
5714 * to log its parents.
5715 *
5716 * So, we use the last_unlink_trans field to put this transid
5717 * into the file. When the file is logged we check it and
5718 * don't log the parents if the file is fully on disk.
5719 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005720 mutex_lock(&BTRFS_I(inode)->log_mutex);
5721 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5722 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005723
5724 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005725 * if this directory was already logged any new
5726 * names for this file/dir will get recorded
5727 */
5728 smp_mb();
5729 if (BTRFS_I(dir)->logged_trans == trans->transid)
5730 return;
5731
5732 /*
5733 * if the inode we're about to unlink was logged,
5734 * the log will be properly updated for any new names
5735 */
5736 if (BTRFS_I(inode)->logged_trans == trans->transid)
5737 return;
5738
5739 /*
5740 * when renaming files across directories, if the directory
5741 * there we're unlinking from gets fsync'd later on, there's
5742 * no way to find the destination directory later and fsync it
5743 * properly. So, we have to be conservative and force commits
5744 * so the new name gets discovered.
5745 */
5746 if (for_rename)
5747 goto record;
5748
5749 /* we can safely do the unlink without any special recording */
5750 return;
5751
5752record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005753 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005754 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005755 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005756}
5757
5758/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005759 * Make sure that if someone attempts to fsync the parent directory of a deleted
5760 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5761 * that after replaying the log tree of the parent directory's root we will not
5762 * see the snapshot anymore and at log replay time we will not see any log tree
5763 * corresponding to the deleted snapshot's root, which could lead to replaying
5764 * it after replaying the log tree of the parent directory (which would replay
5765 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005766 *
5767 * Must be called before the actual snapshot destroy operation (updates to the
5768 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005769 */
5770void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5771 struct inode *dir)
5772{
Filipe Manana2be63d52016-02-12 11:34:23 +00005773 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005774 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005775 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005776}
5777
5778/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005779 * Call this after adding a new name for a file and it will properly
5780 * update the log to reflect the new name.
5781 *
5782 * It will return zero if all goes well, and it will return 1 if a
5783 * full transaction commit is required.
5784 */
5785int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5786 struct inode *inode, struct inode *old_dir,
5787 struct dentry *parent)
5788{
5789 struct btrfs_root * root = BTRFS_I(inode)->root;
5790
5791 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005792 * this will force the logging code to walk the dentry chain
5793 * up for the file
5794 */
5795 if (S_ISREG(inode->i_mode))
5796 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5797
5798 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005799 * if this inode hasn't been logged and directory we're renaming it
5800 * from hasn't been logged, we don't need to log it
5801 */
5802 if (BTRFS_I(inode)->logged_trans <=
5803 root->fs_info->last_trans_committed &&
5804 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5805 root->fs_info->last_trans_committed))
5806 return 0;
5807
Filipe Manana49dae1b2014-09-06 22:34:39 +01005808 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5809 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005810}
5811