blob: ad3ae2ef0cd4f1dde3558879c1a6df127c31c435 [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);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400611 extent_end = ALIGN(start + size,
612 root->fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400613 } else {
614 ret = 0;
615 goto out;
616 }
617
618 inode = read_one_inode(root, key->objectid);
619 if (!inode) {
620 ret = -EIO;
621 goto out;
622 }
623
624 /*
625 * first check to see if we already have this extent in the
626 * file. This must be done before the btrfs_drop_extents run
627 * so we don't try to drop this extent.
628 */
Li Zefan33345d012011-04-20 10:31:50 +0800629 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400630 start, 0);
631
Yan Zhengd899e052008-10-30 14:25:28 -0400632 if (ret == 0 &&
633 (found_type == BTRFS_FILE_EXTENT_REG ||
634 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400635 struct btrfs_file_extent_item cmp1;
636 struct btrfs_file_extent_item cmp2;
637 struct btrfs_file_extent_item *existing;
638 struct extent_buffer *leaf;
639
640 leaf = path->nodes[0];
641 existing = btrfs_item_ptr(leaf, path->slots[0],
642 struct btrfs_file_extent_item);
643
644 read_extent_buffer(eb, &cmp1, (unsigned long)item,
645 sizeof(cmp1));
646 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
647 sizeof(cmp2));
648
649 /*
650 * we already have a pointer to this exact extent,
651 * we don't have to do anything
652 */
653 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200654 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400655 goto out;
656 }
657 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200658 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400659
660 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400661 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400662 if (ret)
663 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400664
Yan Zheng07d400a2009-01-06 11:42:00 -0500665 if (found_type == BTRFS_FILE_EXTENT_REG ||
666 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400667 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500668 unsigned long dest_offset;
669 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400670
Yan Zheng07d400a2009-01-06 11:42:00 -0500671 ret = btrfs_insert_empty_item(trans, root, path, key,
672 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400673 if (ret)
674 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500675 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
676 path->slots[0]);
677 copy_extent_buffer(path->nodes[0], eb, dest_offset,
678 (unsigned long)item, sizeof(*item));
679
680 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
681 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
682 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400683 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500684
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800685 /*
686 * Manually record dirty extent, as here we did a shallow
687 * file extent item copy and skip normal backref update,
688 * but modifying extent tree all by ourselves.
689 * So need to manually record dirty extent for qgroup,
690 * as the owner of the file extent changed from log tree
691 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
692 */
Qu Wenruo50b3e042016-10-18 09:31:27 +0800693 ret = btrfs_qgroup_trace_extent(trans, root->fs_info,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800694 btrfs_file_extent_disk_bytenr(eb, item),
695 btrfs_file_extent_disk_num_bytes(eb, item),
696 GFP_NOFS);
697 if (ret < 0)
698 goto out;
699
Yan Zheng07d400a2009-01-06 11:42:00 -0500700 if (ins.objectid > 0) {
701 u64 csum_start;
702 u64 csum_end;
703 LIST_HEAD(ordered_sums);
704 /*
705 * is this extent already allocated in the extent
706 * allocation tree? If so, just add a reference
707 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000708 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500709 ins.offset);
710 if (ret == 0) {
711 ret = btrfs_inc_extent_ref(trans, root,
712 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400713 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100714 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400715 if (ret)
716 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500717 } else {
718 /*
719 * insert the extent pointer in the extent
720 * allocation tree
721 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 ret = btrfs_alloc_logged_file_extent(trans,
723 root, root->root_key.objectid,
724 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400725 if (ret)
726 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500727 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200728 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500729
730 if (btrfs_file_extent_compression(eb, item)) {
731 csum_start = ins.objectid;
732 csum_end = csum_start + ins.offset;
733 } else {
734 csum_start = ins.objectid +
735 btrfs_file_extent_offset(eb, item);
736 csum_end = csum_start +
737 btrfs_file_extent_num_bytes(eb, item);
738 }
739
740 ret = btrfs_lookup_csums_range(root->log_root,
741 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100742 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400743 if (ret)
744 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100745 /*
746 * Now delete all existing cums in the csum root that
747 * cover our range. We do this because we can have an
748 * extent that is completely referenced by one file
749 * extent item and partially referenced by another
750 * file extent item (like after using the clone or
751 * extent_same ioctls). In this case if we end up doing
752 * the replay of the one that partially references the
753 * extent first, and we do not do the csum deletion
754 * below, we can get 2 csum items in the csum tree that
755 * overlap each other. For example, imagine our log has
756 * the two following file extent items:
757 *
758 * key (257 EXTENT_DATA 409600)
759 * extent data disk byte 12845056 nr 102400
760 * extent data offset 20480 nr 20480 ram 102400
761 *
762 * key (257 EXTENT_DATA 819200)
763 * extent data disk byte 12845056 nr 102400
764 * extent data offset 0 nr 102400 ram 102400
765 *
766 * Where the second one fully references the 100K extent
767 * that starts at disk byte 12845056, and the log tree
768 * has a single csum item that covers the entire range
769 * of the extent:
770 *
771 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
772 *
773 * After the first file extent item is replayed, the
774 * csum tree gets the following csum item:
775 *
776 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
777 *
778 * Which covers the 20K sub-range starting at offset 20K
779 * of our extent. Now when we replay the second file
780 * extent item, if we do not delete existing csum items
781 * that cover any of its blocks, we end up getting two
782 * csum items in our csum tree that overlap each other:
783 *
784 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
785 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
786 *
787 * Which is a problem, because after this anyone trying
788 * to lookup up for the checksum of any block of our
789 * extent starting at an offset of 40K or higher, will
790 * end up looking at the second csum item only, which
791 * does not contain the checksum for any block starting
792 * at offset 40K or higher of our extent.
793 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500794 while (!list_empty(&ordered_sums)) {
795 struct btrfs_ordered_sum *sums;
796 sums = list_entry(ordered_sums.next,
797 struct btrfs_ordered_sum,
798 list);
Josef Bacik36508602013-04-25 16:23:32 -0400799 if (!ret)
Filipe Mananab84b8392015-08-19 11:09:40 +0100800 ret = btrfs_del_csums(trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400801 root->fs_info,
802 sums->bytenr,
803 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100804 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400805 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500806 root->fs_info->csum_root,
807 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500808 list_del(&sums->list);
809 kfree(sums);
810 }
Josef Bacik36508602013-04-25 16:23:32 -0400811 if (ret)
812 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500813 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200814 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500815 }
816 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
817 /* inline extents are easy, we just overwrite them */
818 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400819 if (ret)
820 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500821 }
822
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000823 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600824 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400825out:
826 if (inode)
827 iput(inode);
828 return ret;
829}
830
831/*
832 * when cleaning up conflicts between the directory names in the
833 * subvolume, directory names in the log and directory names in the
834 * inode back references, we may have to unlink inodes from directories.
835 *
836 * This is a helper function to do the unlink of a specific directory
837 * item
838 */
839static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
840 struct btrfs_root *root,
841 struct btrfs_path *path,
842 struct inode *dir,
843 struct btrfs_dir_item *di)
844{
845 struct inode *inode;
846 char *name;
847 int name_len;
848 struct extent_buffer *leaf;
849 struct btrfs_key location;
850 int ret;
851
852 leaf = path->nodes[0];
853
854 btrfs_dir_item_key_to_cpu(leaf, di, &location);
855 name_len = btrfs_dir_name_len(leaf, di);
856 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000857 if (!name)
858 return -ENOMEM;
859
Chris Masone02119d2008-09-05 16:13:11 -0400860 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200861 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400862
863 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000864 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400865 ret = -EIO;
866 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000867 }
Chris Masone02119d2008-09-05 16:13:11 -0400868
Yan Zhengec051c02009-01-05 15:43:42 -0500869 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400870 if (ret)
871 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400872
Chris Masone02119d2008-09-05 16:13:11 -0400873 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400874 if (ret)
875 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100876 else
877 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400878out:
879 kfree(name);
880 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400881 return ret;
882}
883
884/*
885 * helper function to see if a given name and sequence number found
886 * in an inode back reference are already in a directory and correctly
887 * point to this inode
888 */
889static noinline int inode_in_dir(struct btrfs_root *root,
890 struct btrfs_path *path,
891 u64 dirid, u64 objectid, u64 index,
892 const char *name, int name_len)
893{
894 struct btrfs_dir_item *di;
895 struct btrfs_key location;
896 int match = 0;
897
898 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
899 index, name, name_len, 0);
900 if (di && !IS_ERR(di)) {
901 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
902 if (location.objectid != objectid)
903 goto out;
904 } else
905 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200906 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400907
908 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
909 if (di && !IS_ERR(di)) {
910 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
911 if (location.objectid != objectid)
912 goto out;
913 } else
914 goto out;
915 match = 1;
916out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200917 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400918 return match;
919}
920
921/*
922 * helper function to check a log tree for a named back reference in
923 * an inode. This is used to decide if a back reference that is
924 * found in the subvolume conflicts with what we find in the log.
925 *
926 * inode backreferences may have multiple refs in a single item,
927 * during replay we process one reference at a time, and we don't
928 * want to delete valid links to a file from the subvolume if that
929 * link is also in the log.
930 */
931static noinline int backref_in_log(struct btrfs_root *log,
932 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700933 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000934 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400935{
936 struct btrfs_path *path;
937 struct btrfs_inode_ref *ref;
938 unsigned long ptr;
939 unsigned long ptr_end;
940 unsigned long name_ptr;
941 int found_name_len;
942 int item_size;
943 int ret;
944 int match = 0;
945
946 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000947 if (!path)
948 return -ENOMEM;
949
Chris Masone02119d2008-09-05 16:13:11 -0400950 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
951 if (ret != 0)
952 goto out;
953
Chris Masone02119d2008-09-05 16:13:11 -0400954 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700955
956 if (key->type == BTRFS_INODE_EXTREF_KEY) {
957 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
958 name, namelen, NULL))
959 match = 1;
960
961 goto out;
962 }
963
964 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400965 ptr_end = ptr + item_size;
966 while (ptr < ptr_end) {
967 ref = (struct btrfs_inode_ref *)ptr;
968 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
969 if (found_name_len == namelen) {
970 name_ptr = (unsigned long)(ref + 1);
971 ret = memcmp_extent_buffer(path->nodes[0], name,
972 name_ptr, namelen);
973 if (ret == 0) {
974 match = 1;
975 goto out;
976 }
977 }
978 ptr = (unsigned long)(ref + 1) + found_name_len;
979 }
980out:
981 btrfs_free_path(path);
982 return match;
983}
984
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700985static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path,
988 struct btrfs_root *log_root,
989 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700990 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700991 u64 inode_objectid, u64 parent_objectid,
992 u64 ref_index, char *name, int namelen,
993 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700994{
995 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700996 char *victim_name;
997 int victim_name_len;
998 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700999 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001000 struct btrfs_key search_key;
1001 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001002
Mark Fashehf1863732012-08-08 11:32:27 -07001003again:
1004 /* Search old style refs */
1005 search_key.objectid = inode_objectid;
1006 search_key.type = BTRFS_INODE_REF_KEY;
1007 search_key.offset = parent_objectid;
1008 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001009 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010 struct btrfs_inode_ref *victim_ref;
1011 unsigned long ptr;
1012 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001013
1014 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001015
1016 /* are we trying to overwrite a back ref for the root directory
1017 * if so, just jump out, we're done
1018 */
Mark Fashehf1863732012-08-08 11:32:27 -07001019 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001020 return 1;
1021
1022 /* check all the names in this back reference to see
1023 * if they are in the log. if so, we allow them to stay
1024 * otherwise they must be unlinked as a conflict
1025 */
1026 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1027 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1028 while (ptr < ptr_end) {
1029 victim_ref = (struct btrfs_inode_ref *)ptr;
1030 victim_name_len = btrfs_inode_ref_name_len(leaf,
1031 victim_ref);
1032 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001033 if (!victim_name)
1034 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001035
1036 read_extent_buffer(leaf, victim_name,
1037 (unsigned long)(victim_ref + 1),
1038 victim_name_len);
1039
Mark Fashehf1863732012-08-08 11:32:27 -07001040 if (!backref_in_log(log_root, &search_key,
1041 parent_objectid,
1042 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001043 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -07001044 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001045 btrfs_release_path(path);
1046
1047 ret = btrfs_unlink_inode(trans, root, dir,
1048 inode, victim_name,
1049 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001050 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001051 if (ret)
1052 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001053 ret = btrfs_run_delayed_items(trans, root);
1054 if (ret)
1055 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001056 *search_done = 1;
1057 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 }
1059 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001060
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001061 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1062 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001063
1064 /*
1065 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001066 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001067 */
1068 *search_done = 1;
1069 }
1070 btrfs_release_path(path);
1071
Mark Fashehf1863732012-08-08 11:32:27 -07001072 /* Same search but for extended refs */
1073 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1074 inode_objectid, parent_objectid, 0,
1075 0);
1076 if (!IS_ERR_OR_NULL(extref)) {
1077 u32 item_size;
1078 u32 cur_offset = 0;
1079 unsigned long base;
1080 struct inode *victim_parent;
1081
1082 leaf = path->nodes[0];
1083
1084 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1085 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1086
1087 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001088 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001089
1090 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1091
1092 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1093 goto next;
1094
1095 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001096 if (!victim_name)
1097 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001098 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1099 victim_name_len);
1100
1101 search_key.objectid = inode_objectid;
1102 search_key.type = BTRFS_INODE_EXTREF_KEY;
1103 search_key.offset = btrfs_extref_hash(parent_objectid,
1104 victim_name,
1105 victim_name_len);
1106 ret = 0;
1107 if (!backref_in_log(log_root, &search_key,
1108 parent_objectid, victim_name,
1109 victim_name_len)) {
1110 ret = -ENOENT;
1111 victim_parent = read_one_inode(root,
1112 parent_objectid);
1113 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001114 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001115 btrfs_release_path(path);
1116
1117 ret = btrfs_unlink_inode(trans, root,
1118 victim_parent,
1119 inode,
1120 victim_name,
1121 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001122 if (!ret)
1123 ret = btrfs_run_delayed_items(
1124 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001125 }
Mark Fashehf1863732012-08-08 11:32:27 -07001126 iput(victim_parent);
1127 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001128 if (ret)
1129 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001130 *search_done = 1;
1131 goto again;
1132 }
1133 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001134 if (ret)
1135 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001136next:
1137 cur_offset += victim_name_len + sizeof(*extref);
1138 }
1139 *search_done = 1;
1140 }
1141 btrfs_release_path(path);
1142
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001143 /* look for a conflicting sequence number */
1144 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001145 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001146 if (di && !IS_ERR(di)) {
1147 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001148 if (ret)
1149 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001150 }
1151 btrfs_release_path(path);
1152
1153 /* look for a conflicing name */
1154 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1155 name, namelen, 0);
1156 if (di && !IS_ERR(di)) {
1157 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001158 if (ret)
1159 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001160 }
1161 btrfs_release_path(path);
1162
1163 return 0;
1164}
Chris Masone02119d2008-09-05 16:13:11 -04001165
Mark Fashehf1863732012-08-08 11:32:27 -07001166static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1167 u32 *namelen, char **name, u64 *index,
1168 u64 *parent_objectid)
1169{
1170 struct btrfs_inode_extref *extref;
1171
1172 extref = (struct btrfs_inode_extref *)ref_ptr;
1173
1174 *namelen = btrfs_inode_extref_name_len(eb, extref);
1175 *name = kmalloc(*namelen, GFP_NOFS);
1176 if (*name == NULL)
1177 return -ENOMEM;
1178
1179 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1180 *namelen);
1181
1182 *index = btrfs_inode_extref_index(eb, extref);
1183 if (parent_objectid)
1184 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1185
1186 return 0;
1187}
1188
1189static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1190 u32 *namelen, char **name, u64 *index)
1191{
1192 struct btrfs_inode_ref *ref;
1193
1194 ref = (struct btrfs_inode_ref *)ref_ptr;
1195
1196 *namelen = btrfs_inode_ref_name_len(eb, ref);
1197 *name = kmalloc(*namelen, GFP_NOFS);
1198 if (*name == NULL)
1199 return -ENOMEM;
1200
1201 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1202
1203 *index = btrfs_inode_ref_index(eb, ref);
1204
1205 return 0;
1206}
1207
Chris Masone02119d2008-09-05 16:13:11 -04001208/*
1209 * replay one inode back reference item found in the log tree.
1210 * eb, slot and key refer to the buffer and key found in the log tree.
1211 * root is the destination we are replaying into, and path is for temp
1212 * use by this function. (it should be released on return).
1213 */
1214static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 struct btrfs_root *log,
1217 struct btrfs_path *path,
1218 struct extent_buffer *eb, int slot,
1219 struct btrfs_key *key)
1220{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001221 struct inode *dir = NULL;
1222 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001223 unsigned long ref_ptr;
1224 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001225 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001226 int namelen;
1227 int ret;
liuboc622ae62011-03-26 08:01:12 -04001228 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001229 int log_ref_ver = 0;
1230 u64 parent_objectid;
1231 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001232 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001233 int ref_struct_size;
1234
1235 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1236 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1237
1238 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1239 struct btrfs_inode_extref *r;
1240
1241 ref_struct_size = sizeof(struct btrfs_inode_extref);
1242 log_ref_ver = 1;
1243 r = (struct btrfs_inode_extref *)ref_ptr;
1244 parent_objectid = btrfs_inode_extref_parent(eb, r);
1245 } else {
1246 ref_struct_size = sizeof(struct btrfs_inode_ref);
1247 parent_objectid = key->offset;
1248 }
1249 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001250
Chris Masone02119d2008-09-05 16:13:11 -04001251 /*
1252 * it is possible that we didn't log all the parent directories
1253 * for a given inode. If we don't find the dir, just don't
1254 * copy the back ref in. The link count fixup code will take
1255 * care of the rest
1256 */
Mark Fashehf1863732012-08-08 11:32:27 -07001257 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001258 if (!dir) {
1259 ret = -ENOENT;
1260 goto out;
1261 }
Chris Masone02119d2008-09-05 16:13:11 -04001262
Mark Fashehf1863732012-08-08 11:32:27 -07001263 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001264 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001265 ret = -EIO;
1266 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001267 }
Chris Masone02119d2008-09-05 16:13:11 -04001268
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001269 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001270 if (log_ref_ver) {
1271 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1272 &ref_index, &parent_objectid);
1273 /*
1274 * parent object can change from one array
1275 * item to another.
1276 */
1277 if (!dir)
1278 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001279 if (!dir) {
1280 ret = -ENOENT;
1281 goto out;
1282 }
Mark Fashehf1863732012-08-08 11:32:27 -07001283 } else {
1284 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1285 &ref_index);
1286 }
1287 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001288 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001289
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001290 /* if we already have a perfect match, we're done */
1291 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001292 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001293 /*
1294 * look for a conflicting back reference in the
1295 * metadata. if we find one we have to unlink that name
1296 * of the file before we add our new link. Later on, we
1297 * overwrite any existing back reference, and we don't
1298 * want to create dangling pointers in the directory.
1299 */
Chris Masone02119d2008-09-05 16:13:11 -04001300
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001301 if (!search_done) {
1302 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001303 dir, inode, eb,
1304 inode_objectid,
1305 parent_objectid,
1306 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001307 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001308 if (ret) {
1309 if (ret == 1)
1310 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001311 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001312 }
Chris Masone02119d2008-09-05 16:13:11 -04001313 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001314
1315 /* insert our name */
1316 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001317 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001318 if (ret)
1319 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001320
1321 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001322 }
liuboc622ae62011-03-26 08:01:12 -04001323
Mark Fashehf1863732012-08-08 11:32:27 -07001324 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001325 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001326 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001327 if (log_ref_ver) {
1328 iput(dir);
1329 dir = NULL;
1330 }
Chris Masone02119d2008-09-05 16:13:11 -04001331 }
Chris Masone02119d2008-09-05 16:13:11 -04001332
1333 /* finally write the back reference in the inode */
1334 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001335out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001336 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001337 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001338 iput(dir);
1339 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001340 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001341}
1342
Yan, Zhengc71bf092009-11-12 09:34:40 +00001343static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001344 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001345{
1346 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001347
David Sterba9c4f61f2015-01-02 19:12:57 +01001348 ret = btrfs_insert_orphan_item(trans, root, ino);
1349 if (ret == -EEXIST)
1350 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001351
Yan, Zhengc71bf092009-11-12 09:34:40 +00001352 return ret;
1353}
1354
Mark Fashehf1863732012-08-08 11:32:27 -07001355static int count_inode_extrefs(struct btrfs_root *root,
1356 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001357{
Mark Fashehf1863732012-08-08 11:32:27 -07001358 int ret = 0;
1359 int name_len;
1360 unsigned int nlink = 0;
1361 u32 item_size;
1362 u32 cur_offset = 0;
1363 u64 inode_objectid = btrfs_ino(inode);
1364 u64 offset = 0;
1365 unsigned long ptr;
1366 struct btrfs_inode_extref *extref;
1367 struct extent_buffer *leaf;
1368
1369 while (1) {
1370 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1371 &extref, &offset);
1372 if (ret)
1373 break;
1374
1375 leaf = path->nodes[0];
1376 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1377 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001378 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001379
1380 while (cur_offset < item_size) {
1381 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1382 name_len = btrfs_inode_extref_name_len(leaf, extref);
1383
1384 nlink++;
1385
1386 cur_offset += name_len + sizeof(*extref);
1387 }
1388
1389 offset++;
1390 btrfs_release_path(path);
1391 }
1392 btrfs_release_path(path);
1393
Filipe Manana2c2c4522015-01-13 16:40:04 +00001394 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001395 return ret;
1396 return nlink;
1397}
1398
1399static int count_inode_refs(struct btrfs_root *root,
1400 struct inode *inode, struct btrfs_path *path)
1401{
Chris Masone02119d2008-09-05 16:13:11 -04001402 int ret;
1403 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001404 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001405 unsigned long ptr;
1406 unsigned long ptr_end;
1407 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001408 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001409
Li Zefan33345d012011-04-20 10:31:50 +08001410 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001411 key.type = BTRFS_INODE_REF_KEY;
1412 key.offset = (u64)-1;
1413
Chris Masond3977122009-01-05 21:25:51 -05001414 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001415 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1416 if (ret < 0)
1417 break;
1418 if (ret > 0) {
1419 if (path->slots[0] == 0)
1420 break;
1421 path->slots[0]--;
1422 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001423process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001424 btrfs_item_key_to_cpu(path->nodes[0], &key,
1425 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001426 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001427 key.type != BTRFS_INODE_REF_KEY)
1428 break;
1429 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1430 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1431 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001432 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001433 struct btrfs_inode_ref *ref;
1434
1435 ref = (struct btrfs_inode_ref *)ptr;
1436 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1437 ref);
1438 ptr = (unsigned long)(ref + 1) + name_len;
1439 nlink++;
1440 }
1441
1442 if (key.offset == 0)
1443 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001444 if (path->slots[0] > 0) {
1445 path->slots[0]--;
1446 goto process_slot;
1447 }
Chris Masone02119d2008-09-05 16:13:11 -04001448 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001449 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001450 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001451 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001452
1453 return nlink;
1454}
1455
1456/*
1457 * There are a few corners where the link count of the file can't
1458 * be properly maintained during replay. So, instead of adding
1459 * lots of complexity to the log code, we just scan the backrefs
1460 * for any file that has been through replay.
1461 *
1462 * The scan will update the link count on the inode to reflect the
1463 * number of back refs found. If it goes down to zero, the iput
1464 * will free the inode.
1465 */
1466static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1467 struct btrfs_root *root,
1468 struct inode *inode)
1469{
1470 struct btrfs_path *path;
1471 int ret;
1472 u64 nlink = 0;
1473 u64 ino = btrfs_ino(inode);
1474
1475 path = btrfs_alloc_path();
1476 if (!path)
1477 return -ENOMEM;
1478
1479 ret = count_inode_refs(root, inode, path);
1480 if (ret < 0)
1481 goto out;
1482
1483 nlink = ret;
1484
1485 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001486 if (ret < 0)
1487 goto out;
1488
1489 nlink += ret;
1490
1491 ret = 0;
1492
Chris Masone02119d2008-09-05 16:13:11 -04001493 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001494 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001495 btrfs_update_inode(trans, root, inode);
1496 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001497 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001498
Yan, Zhengc71bf092009-11-12 09:34:40 +00001499 if (inode->i_nlink == 0) {
1500 if (S_ISDIR(inode->i_mode)) {
1501 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001502 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001503 if (ret)
1504 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001505 }
Li Zefan33345d012011-04-20 10:31:50 +08001506 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001507 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001508
Mark Fashehf1863732012-08-08 11:32:27 -07001509out:
1510 btrfs_free_path(path);
1511 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001512}
1513
1514static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1515 struct btrfs_root *root,
1516 struct btrfs_path *path)
1517{
1518 int ret;
1519 struct btrfs_key key;
1520 struct inode *inode;
1521
1522 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1523 key.type = BTRFS_ORPHAN_ITEM_KEY;
1524 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001525 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1527 if (ret < 0)
1528 break;
1529
1530 if (ret == 1) {
1531 if (path->slots[0] == 0)
1532 break;
1533 path->slots[0]--;
1534 }
1535
1536 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1537 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1538 key.type != BTRFS_ORPHAN_ITEM_KEY)
1539 break;
1540
1541 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001542 if (ret)
1543 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001544
David Sterbab3b4aa72011-04-21 01:20:15 +02001545 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001546 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001547 if (!inode)
1548 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001549
1550 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001551 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001552 if (ret)
1553 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001554
Chris Mason12fcfd22009-03-24 10:24:20 -04001555 /*
1556 * fixup on a directory may create new entries,
1557 * make sure we always look for the highset possible
1558 * offset
1559 */
1560 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001561 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001562 ret = 0;
1563out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001564 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001565 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001566}
1567
1568
1569/*
1570 * record a given inode in the fixup dir so we can check its link
1571 * count when replay is done. The link count is incremented here
1572 * so the inode won't go away until we check it
1573 */
1574static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1575 struct btrfs_root *root,
1576 struct btrfs_path *path,
1577 u64 objectid)
1578{
1579 struct btrfs_key key;
1580 int ret = 0;
1581 struct inode *inode;
1582
1583 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001584 if (!inode)
1585 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001586
1587 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001588 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001589 key.offset = objectid;
1590
1591 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1592
David Sterbab3b4aa72011-04-21 01:20:15 +02001593 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001594 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001595 if (!inode->i_nlink)
1596 set_nlink(inode, 1);
1597 else
Zach Brown8b558c52013-10-16 12:10:34 -07001598 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001599 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001600 } else if (ret == -EEXIST) {
1601 ret = 0;
1602 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001603 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001604 }
1605 iput(inode);
1606
1607 return ret;
1608}
1609
1610/*
1611 * when replaying the log for a directory, we only insert names
1612 * for inodes that actually exist. This means an fsync on a directory
1613 * does not implicitly fsync all the new files in it
1614 */
1615static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1616 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001617 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001618 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001619 struct btrfs_key *location)
1620{
1621 struct inode *inode;
1622 struct inode *dir;
1623 int ret;
1624
1625 inode = read_one_inode(root, location->objectid);
1626 if (!inode)
1627 return -ENOENT;
1628
1629 dir = read_one_inode(root, dirid);
1630 if (!dir) {
1631 iput(inode);
1632 return -EIO;
1633 }
Josef Bacikd5554382013-09-11 14:17:00 -04001634
Chris Masone02119d2008-09-05 16:13:11 -04001635 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1636
1637 /* FIXME, put inode into FIXUP list */
1638
1639 iput(inode);
1640 iput(dir);
1641 return ret;
1642}
1643
1644/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001645 * Return true if an inode reference exists in the log for the given name,
1646 * inode and parent inode.
1647 */
1648static bool name_in_log_ref(struct btrfs_root *log_root,
1649 const char *name, const int name_len,
1650 const u64 dirid, const u64 ino)
1651{
1652 struct btrfs_key search_key;
1653
1654 search_key.objectid = ino;
1655 search_key.type = BTRFS_INODE_REF_KEY;
1656 search_key.offset = dirid;
1657 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1658 return true;
1659
1660 search_key.type = BTRFS_INODE_EXTREF_KEY;
1661 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1662 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1663 return true;
1664
1665 return false;
1666}
1667
1668/*
Chris Masone02119d2008-09-05 16:13:11 -04001669 * take a single entry in a log directory item and replay it into
1670 * the subvolume.
1671 *
1672 * if a conflicting item exists in the subdirectory already,
1673 * the inode it points to is unlinked and put into the link count
1674 * fix up tree.
1675 *
1676 * If a name from the log points to a file or directory that does
1677 * not exist in the FS, it is skipped. fsyncs on directories
1678 * do not force down inodes inside that directory, just changes to the
1679 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001680 *
1681 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1682 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001683 */
1684static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1685 struct btrfs_root *root,
1686 struct btrfs_path *path,
1687 struct extent_buffer *eb,
1688 struct btrfs_dir_item *di,
1689 struct btrfs_key *key)
1690{
1691 char *name;
1692 int name_len;
1693 struct btrfs_dir_item *dst_di;
1694 struct btrfs_key found_key;
1695 struct btrfs_key log_key;
1696 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001697 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001698 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001699 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001700 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001701 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001702
1703 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001704 if (!dir)
1705 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001706
1707 name_len = btrfs_dir_name_len(eb, di);
1708 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001709 if (!name) {
1710 ret = -ENOMEM;
1711 goto out;
1712 }
liubo2a29edc2011-01-26 06:22:08 +00001713
Chris Masone02119d2008-09-05 16:13:11 -04001714 log_type = btrfs_dir_type(eb, di);
1715 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1716 name_len);
1717
1718 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001719 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1720 if (exists == 0)
1721 exists = 1;
1722 else
1723 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001724 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001725
Chris Masone02119d2008-09-05 16:13:11 -04001726 if (key->type == BTRFS_DIR_ITEM_KEY) {
1727 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1728 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001729 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001730 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1731 key->objectid,
1732 key->offset, name,
1733 name_len, 1);
1734 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001735 /* Corruption */
1736 ret = -EINVAL;
1737 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001738 }
David Sterbac7040052011-04-19 18:00:01 +02001739 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001740 /* we need a sequence number to insert, so we only
1741 * do inserts for the BTRFS_DIR_INDEX_KEY types
1742 */
1743 if (key->type != BTRFS_DIR_INDEX_KEY)
1744 goto out;
1745 goto insert;
1746 }
1747
1748 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1749 /* the existing item matches the logged item */
1750 if (found_key.objectid == log_key.objectid &&
1751 found_key.type == log_key.type &&
1752 found_key.offset == log_key.offset &&
1753 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001754 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001755 goto out;
1756 }
1757
1758 /*
1759 * don't drop the conflicting directory entry if the inode
1760 * for the new entry doesn't exist
1761 */
Chris Mason4bef0842008-09-08 11:18:08 -04001762 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001763 goto out;
1764
Chris Masone02119d2008-09-05 16:13:11 -04001765 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001766 if (ret)
1767 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001768
1769 if (key->type == BTRFS_DIR_INDEX_KEY)
1770 goto insert;
1771out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001772 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001773 if (!ret && update_size) {
1774 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1775 ret = btrfs_update_inode(trans, root, dir);
1776 }
Chris Masone02119d2008-09-05 16:13:11 -04001777 kfree(name);
1778 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001779 if (!ret && name_added)
1780 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001781 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001782
1783insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001784 if (name_in_log_ref(root->log_root, name, name_len,
1785 key->objectid, log_key.objectid)) {
1786 /* The dentry will be added later. */
1787 ret = 0;
1788 update_size = false;
1789 goto out;
1790 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001791 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001792 ret = insert_one_name(trans, root, key->objectid, key->offset,
1793 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001794 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001795 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001796 if (!ret)
1797 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001798 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001799 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001800 goto out;
1801}
1802
1803/*
1804 * find all the names in a directory item and reconcile them into
1805 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1806 * one name in a directory item, but the same code gets used for
1807 * both directory index types
1808 */
1809static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1810 struct btrfs_root *root,
1811 struct btrfs_path *path,
1812 struct extent_buffer *eb, int slot,
1813 struct btrfs_key *key)
1814{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001815 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001816 u32 item_size = btrfs_item_size_nr(eb, slot);
1817 struct btrfs_dir_item *di;
1818 int name_len;
1819 unsigned long ptr;
1820 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001821 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001822
1823 ptr = btrfs_item_ptr_offset(eb, slot);
1824 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001825 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001826 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001827 if (verify_dir_item(root, eb, di))
1828 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001829 name_len = btrfs_dir_name_len(eb, di);
1830 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001831 if (ret < 0)
1832 break;
Chris Masone02119d2008-09-05 16:13:11 -04001833 ptr = (unsigned long)(di + 1);
1834 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001835
1836 /*
1837 * If this entry refers to a non-directory (directories can not
1838 * have a link count > 1) and it was added in the transaction
1839 * that was not committed, make sure we fixup the link count of
1840 * the inode it the entry points to. Otherwise something like
1841 * the following would result in a directory pointing to an
1842 * inode with a wrong link that does not account for this dir
1843 * entry:
1844 *
1845 * mkdir testdir
1846 * touch testdir/foo
1847 * touch testdir/bar
1848 * sync
1849 *
1850 * ln testdir/bar testdir/bar_link
1851 * ln testdir/foo testdir/foo_link
1852 * xfs_io -c "fsync" testdir/bar
1853 *
1854 * <power failure>
1855 *
1856 * mount fs, log replay happens
1857 *
1858 * File foo would remain with a link count of 1 when it has two
1859 * entries pointing to it in the directory testdir. This would
1860 * make it impossible to ever delete the parent directory has
1861 * it would result in stale dentries that can never be deleted.
1862 */
1863 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1864 struct btrfs_key di_key;
1865
1866 if (!fixup_path) {
1867 fixup_path = btrfs_alloc_path();
1868 if (!fixup_path) {
1869 ret = -ENOMEM;
1870 break;
1871 }
1872 }
1873
1874 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1875 ret = link_to_fixup_dir(trans, root, fixup_path,
1876 di_key.objectid);
1877 if (ret)
1878 break;
1879 }
1880 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001881 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001882 btrfs_free_path(fixup_path);
1883 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001884}
1885
1886/*
1887 * directory replay has two parts. There are the standard directory
1888 * items in the log copied from the subvolume, and range items
1889 * created in the log while the subvolume was logged.
1890 *
1891 * The range items tell us which parts of the key space the log
1892 * is authoritative for. During replay, if a key in the subvolume
1893 * directory is in a logged range item, but not actually in the log
1894 * that means it was deleted from the directory before the fsync
1895 * and should be removed.
1896 */
1897static noinline int find_dir_range(struct btrfs_root *root,
1898 struct btrfs_path *path,
1899 u64 dirid, int key_type,
1900 u64 *start_ret, u64 *end_ret)
1901{
1902 struct btrfs_key key;
1903 u64 found_end;
1904 struct btrfs_dir_log_item *item;
1905 int ret;
1906 int nritems;
1907
1908 if (*start_ret == (u64)-1)
1909 return 1;
1910
1911 key.objectid = dirid;
1912 key.type = key_type;
1913 key.offset = *start_ret;
1914
1915 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1916 if (ret < 0)
1917 goto out;
1918 if (ret > 0) {
1919 if (path->slots[0] == 0)
1920 goto out;
1921 path->slots[0]--;
1922 }
1923 if (ret != 0)
1924 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1925
1926 if (key.type != key_type || key.objectid != dirid) {
1927 ret = 1;
1928 goto next;
1929 }
1930 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1931 struct btrfs_dir_log_item);
1932 found_end = btrfs_dir_log_end(path->nodes[0], item);
1933
1934 if (*start_ret >= key.offset && *start_ret <= found_end) {
1935 ret = 0;
1936 *start_ret = key.offset;
1937 *end_ret = found_end;
1938 goto out;
1939 }
1940 ret = 1;
1941next:
1942 /* check the next slot in the tree to see if it is a valid item */
1943 nritems = btrfs_header_nritems(path->nodes[0]);
1944 if (path->slots[0] >= nritems) {
1945 ret = btrfs_next_leaf(root, path);
1946 if (ret)
1947 goto out;
1948 } else {
1949 path->slots[0]++;
1950 }
1951
1952 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1953
1954 if (key.type != key_type || key.objectid != dirid) {
1955 ret = 1;
1956 goto out;
1957 }
1958 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1959 struct btrfs_dir_log_item);
1960 found_end = btrfs_dir_log_end(path->nodes[0], item);
1961 *start_ret = key.offset;
1962 *end_ret = found_end;
1963 ret = 0;
1964out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001965 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001966 return ret;
1967}
1968
1969/*
1970 * this looks for a given directory item in the log. If the directory
1971 * item is not in the log, the item is removed and the inode it points
1972 * to is unlinked
1973 */
1974static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1975 struct btrfs_root *root,
1976 struct btrfs_root *log,
1977 struct btrfs_path *path,
1978 struct btrfs_path *log_path,
1979 struct inode *dir,
1980 struct btrfs_key *dir_key)
1981{
1982 int ret;
1983 struct extent_buffer *eb;
1984 int slot;
1985 u32 item_size;
1986 struct btrfs_dir_item *di;
1987 struct btrfs_dir_item *log_di;
1988 int name_len;
1989 unsigned long ptr;
1990 unsigned long ptr_end;
1991 char *name;
1992 struct inode *inode;
1993 struct btrfs_key location;
1994
1995again:
1996 eb = path->nodes[0];
1997 slot = path->slots[0];
1998 item_size = btrfs_item_size_nr(eb, slot);
1999 ptr = btrfs_item_ptr_offset(eb, slot);
2000 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002001 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002002 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04002003 if (verify_dir_item(root, eb, di)) {
2004 ret = -EIO;
2005 goto out;
2006 }
2007
Chris Masone02119d2008-09-05 16:13:11 -04002008 name_len = btrfs_dir_name_len(eb, di);
2009 name = kmalloc(name_len, GFP_NOFS);
2010 if (!name) {
2011 ret = -ENOMEM;
2012 goto out;
2013 }
2014 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2015 name_len);
2016 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002017 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002018 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2019 dir_key->objectid,
2020 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002021 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002022 log_di = btrfs_lookup_dir_index_item(trans, log,
2023 log_path,
2024 dir_key->objectid,
2025 dir_key->offset,
2026 name, name_len, 0);
2027 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002028 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002029 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002030 btrfs_release_path(path);
2031 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002032 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002033 if (!inode) {
2034 kfree(name);
2035 return -EIO;
2036 }
Chris Masone02119d2008-09-05 16:13:11 -04002037
2038 ret = link_to_fixup_dir(trans, root,
2039 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002040 if (ret) {
2041 kfree(name);
2042 iput(inode);
2043 goto out;
2044 }
2045
Zach Brown8b558c52013-10-16 12:10:34 -07002046 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002047 ret = btrfs_unlink_inode(trans, root, dir, inode,
2048 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002049 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01002050 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002051 kfree(name);
2052 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002053 if (ret)
2054 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002055
2056 /* there might still be more names under this key
2057 * check and repeat if required
2058 */
2059 ret = btrfs_search_slot(NULL, root, dir_key, path,
2060 0, 0);
2061 if (ret == 0)
2062 goto again;
2063 ret = 0;
2064 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002065 } else if (IS_ERR(log_di)) {
2066 kfree(name);
2067 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002068 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002069 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002070 kfree(name);
2071
2072 ptr = (unsigned long)(di + 1);
2073 ptr += name_len;
2074 }
2075 ret = 0;
2076out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002077 btrfs_release_path(path);
2078 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002079 return ret;
2080}
2081
Filipe Manana4f764e52015-02-23 19:53:35 +00002082static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2083 struct btrfs_root *root,
2084 struct btrfs_root *log,
2085 struct btrfs_path *path,
2086 const u64 ino)
2087{
2088 struct btrfs_key search_key;
2089 struct btrfs_path *log_path;
2090 int i;
2091 int nritems;
2092 int ret;
2093
2094 log_path = btrfs_alloc_path();
2095 if (!log_path)
2096 return -ENOMEM;
2097
2098 search_key.objectid = ino;
2099 search_key.type = BTRFS_XATTR_ITEM_KEY;
2100 search_key.offset = 0;
2101again:
2102 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2103 if (ret < 0)
2104 goto out;
2105process_leaf:
2106 nritems = btrfs_header_nritems(path->nodes[0]);
2107 for (i = path->slots[0]; i < nritems; i++) {
2108 struct btrfs_key key;
2109 struct btrfs_dir_item *di;
2110 struct btrfs_dir_item *log_di;
2111 u32 total_size;
2112 u32 cur;
2113
2114 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2115 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2116 ret = 0;
2117 goto out;
2118 }
2119
2120 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2121 total_size = btrfs_item_size_nr(path->nodes[0], i);
2122 cur = 0;
2123 while (cur < total_size) {
2124 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2125 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2126 u32 this_len = sizeof(*di) + name_len + data_len;
2127 char *name;
2128
2129 name = kmalloc(name_len, GFP_NOFS);
2130 if (!name) {
2131 ret = -ENOMEM;
2132 goto out;
2133 }
2134 read_extent_buffer(path->nodes[0], name,
2135 (unsigned long)(di + 1), name_len);
2136
2137 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2138 name, name_len, 0);
2139 btrfs_release_path(log_path);
2140 if (!log_di) {
2141 /* Doesn't exist in log tree, so delete it. */
2142 btrfs_release_path(path);
2143 di = btrfs_lookup_xattr(trans, root, path, ino,
2144 name, name_len, -1);
2145 kfree(name);
2146 if (IS_ERR(di)) {
2147 ret = PTR_ERR(di);
2148 goto out;
2149 }
2150 ASSERT(di);
2151 ret = btrfs_delete_one_dir_name(trans, root,
2152 path, di);
2153 if (ret)
2154 goto out;
2155 btrfs_release_path(path);
2156 search_key = key;
2157 goto again;
2158 }
2159 kfree(name);
2160 if (IS_ERR(log_di)) {
2161 ret = PTR_ERR(log_di);
2162 goto out;
2163 }
2164 cur += this_len;
2165 di = (struct btrfs_dir_item *)((char *)di + this_len);
2166 }
2167 }
2168 ret = btrfs_next_leaf(root, path);
2169 if (ret > 0)
2170 ret = 0;
2171 else if (ret == 0)
2172 goto process_leaf;
2173out:
2174 btrfs_free_path(log_path);
2175 btrfs_release_path(path);
2176 return ret;
2177}
2178
2179
Chris Masone02119d2008-09-05 16:13:11 -04002180/*
2181 * deletion replay happens before we copy any new directory items
2182 * out of the log or out of backreferences from inodes. It
2183 * scans the log to find ranges of keys that log is authoritative for,
2184 * and then scans the directory to find items in those ranges that are
2185 * not present in the log.
2186 *
2187 * Anything we don't find in the log is unlinked and removed from the
2188 * directory.
2189 */
2190static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2191 struct btrfs_root *root,
2192 struct btrfs_root *log,
2193 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002194 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002195{
2196 u64 range_start;
2197 u64 range_end;
2198 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2199 int ret = 0;
2200 struct btrfs_key dir_key;
2201 struct btrfs_key found_key;
2202 struct btrfs_path *log_path;
2203 struct inode *dir;
2204
2205 dir_key.objectid = dirid;
2206 dir_key.type = BTRFS_DIR_ITEM_KEY;
2207 log_path = btrfs_alloc_path();
2208 if (!log_path)
2209 return -ENOMEM;
2210
2211 dir = read_one_inode(root, dirid);
2212 /* it isn't an error if the inode isn't there, that can happen
2213 * because we replay the deletes before we copy in the inode item
2214 * from the log
2215 */
2216 if (!dir) {
2217 btrfs_free_path(log_path);
2218 return 0;
2219 }
2220again:
2221 range_start = 0;
2222 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002223 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002224 if (del_all)
2225 range_end = (u64)-1;
2226 else {
2227 ret = find_dir_range(log, path, dirid, key_type,
2228 &range_start, &range_end);
2229 if (ret != 0)
2230 break;
2231 }
Chris Masone02119d2008-09-05 16:13:11 -04002232
2233 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002234 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002235 int nritems;
2236 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2237 0, 0);
2238 if (ret < 0)
2239 goto out;
2240
2241 nritems = btrfs_header_nritems(path->nodes[0]);
2242 if (path->slots[0] >= nritems) {
2243 ret = btrfs_next_leaf(root, path);
2244 if (ret)
2245 break;
2246 }
2247 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2248 path->slots[0]);
2249 if (found_key.objectid != dirid ||
2250 found_key.type != dir_key.type)
2251 goto next_type;
2252
2253 if (found_key.offset > range_end)
2254 break;
2255
2256 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002257 log_path, dir,
2258 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002259 if (ret)
2260 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002261 if (found_key.offset == (u64)-1)
2262 break;
2263 dir_key.offset = found_key.offset + 1;
2264 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002265 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002266 if (range_end == (u64)-1)
2267 break;
2268 range_start = range_end + 1;
2269 }
2270
2271next_type:
2272 ret = 0;
2273 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2274 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2275 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002276 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002277 goto again;
2278 }
2279out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002280 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002281 btrfs_free_path(log_path);
2282 iput(dir);
2283 return ret;
2284}
2285
2286/*
2287 * the process_func used to replay items from the log tree. This
2288 * gets called in two different stages. The first stage just looks
2289 * for inodes and makes sure they are all copied into the subvolume.
2290 *
2291 * The second stage copies all the other item types from the log into
2292 * the subvolume. The two stage approach is slower, but gets rid of
2293 * lots of complexity around inodes referencing other inodes that exist
2294 * only in the log (references come from either directory items or inode
2295 * back refs).
2296 */
2297static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2298 struct walk_control *wc, u64 gen)
2299{
2300 int nritems;
2301 struct btrfs_path *path;
2302 struct btrfs_root *root = wc->replay_dest;
2303 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002304 int level;
2305 int i;
2306 int ret;
2307
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002308 ret = btrfs_read_buffer(eb, gen);
2309 if (ret)
2310 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002311
2312 level = btrfs_header_level(eb);
2313
2314 if (level != 0)
2315 return 0;
2316
2317 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002318 if (!path)
2319 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002320
2321 nritems = btrfs_header_nritems(eb);
2322 for (i = 0; i < nritems; i++) {
2323 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002324
2325 /* inode keys are done during the first stage */
2326 if (key.type == BTRFS_INODE_ITEM_KEY &&
2327 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002328 struct btrfs_inode_item *inode_item;
2329 u32 mode;
2330
2331 inode_item = btrfs_item_ptr(eb, i,
2332 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002333 ret = replay_xattr_deletes(wc->trans, root, log,
2334 path, key.objectid);
2335 if (ret)
2336 break;
Chris Masone02119d2008-09-05 16:13:11 -04002337 mode = btrfs_inode_mode(eb, inode_item);
2338 if (S_ISDIR(mode)) {
2339 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002340 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002341 if (ret)
2342 break;
Chris Masone02119d2008-09-05 16:13:11 -04002343 }
2344 ret = overwrite_item(wc->trans, root, path,
2345 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002346 if (ret)
2347 break;
Chris Masone02119d2008-09-05 16:13:11 -04002348
Yan, Zhengc71bf092009-11-12 09:34:40 +00002349 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002350 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002351 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002352 */
2353 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002354 ret = insert_orphan_item(wc->trans, root,
2355 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002356 if (ret)
2357 break;
Chris Masone02119d2008-09-05 16:13:11 -04002358 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002359
Chris Masone02119d2008-09-05 16:13:11 -04002360 ret = link_to_fixup_dir(wc->trans, root,
2361 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002362 if (ret)
2363 break;
Chris Masone02119d2008-09-05 16:13:11 -04002364 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002365
2366 if (key.type == BTRFS_DIR_INDEX_KEY &&
2367 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2368 ret = replay_one_dir_item(wc->trans, root, path,
2369 eb, i, &key);
2370 if (ret)
2371 break;
2372 }
2373
Chris Masone02119d2008-09-05 16:13:11 -04002374 if (wc->stage < LOG_WALK_REPLAY_ALL)
2375 continue;
2376
2377 /* these keys are simply copied */
2378 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2379 ret = overwrite_item(wc->trans, root, path,
2380 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002381 if (ret)
2382 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002383 } else if (key.type == BTRFS_INODE_REF_KEY ||
2384 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002385 ret = add_inode_ref(wc->trans, root, log, path,
2386 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002387 if (ret && ret != -ENOENT)
2388 break;
2389 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002390 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2391 ret = replay_one_extent(wc->trans, root, path,
2392 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002393 if (ret)
2394 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002395 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002396 ret = replay_one_dir_item(wc->trans, root, path,
2397 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002398 if (ret)
2399 break;
Chris Masone02119d2008-09-05 16:13:11 -04002400 }
2401 }
2402 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002403 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002404}
2405
Chris Masond3977122009-01-05 21:25:51 -05002406static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002407 struct btrfs_root *root,
2408 struct btrfs_path *path, int *level,
2409 struct walk_control *wc)
2410{
2411 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002412 u64 bytenr;
2413 u64 ptr_gen;
2414 struct extent_buffer *next;
2415 struct extent_buffer *cur;
2416 struct extent_buffer *parent;
2417 u32 blocksize;
2418 int ret = 0;
2419
2420 WARN_ON(*level < 0);
2421 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2422
Chris Masond3977122009-01-05 21:25:51 -05002423 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002424 WARN_ON(*level < 0);
2425 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2426 cur = path->nodes[*level];
2427
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302428 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002429
2430 if (path->slots[*level] >=
2431 btrfs_header_nritems(cur))
2432 break;
2433
2434 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2435 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002436 blocksize = root->fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002437
2438 parent = path->nodes[*level];
2439 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002440
David Sterbaa83fffb2014-06-15 02:39:54 +02002441 next = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002442 if (IS_ERR(next))
2443 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002444
Chris Masone02119d2008-09-05 16:13:11 -04002445 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002446 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002447 if (ret) {
2448 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002449 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002450 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002451
Chris Masone02119d2008-09-05 16:13:11 -04002452 path->slots[*level]++;
2453 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002454 ret = btrfs_read_buffer(next, ptr_gen);
2455 if (ret) {
2456 free_extent_buffer(next);
2457 return ret;
2458 }
Chris Masone02119d2008-09-05 16:13:11 -04002459
Josef Bacik681ae502013-10-07 15:11:00 -04002460 if (trans) {
2461 btrfs_tree_lock(next);
2462 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002463 clean_tree_block(trans, root->fs_info,
2464 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002465 btrfs_wait_tree_block_writeback(next);
2466 btrfs_tree_unlock(next);
2467 }
Chris Masone02119d2008-09-05 16:13:11 -04002468
Chris Masone02119d2008-09-05 16:13:11 -04002469 WARN_ON(root_owner !=
2470 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002471 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002472 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002473 if (ret) {
2474 free_extent_buffer(next);
2475 return ret;
2476 }
Chris Masone02119d2008-09-05 16:13:11 -04002477 }
2478 free_extent_buffer(next);
2479 continue;
2480 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002481 ret = btrfs_read_buffer(next, ptr_gen);
2482 if (ret) {
2483 free_extent_buffer(next);
2484 return ret;
2485 }
Chris Masone02119d2008-09-05 16:13:11 -04002486
2487 WARN_ON(*level <= 0);
2488 if (path->nodes[*level-1])
2489 free_extent_buffer(path->nodes[*level-1]);
2490 path->nodes[*level-1] = next;
2491 *level = btrfs_header_level(next);
2492 path->slots[*level] = 0;
2493 cond_resched();
2494 }
2495 WARN_ON(*level < 0);
2496 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2497
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002498 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002499
2500 cond_resched();
2501 return 0;
2502}
2503
Chris Masond3977122009-01-05 21:25:51 -05002504static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002505 struct btrfs_root *root,
2506 struct btrfs_path *path, int *level,
2507 struct walk_control *wc)
2508{
2509 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002510 int i;
2511 int slot;
2512 int ret;
2513
Chris Masond3977122009-01-05 21:25:51 -05002514 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002515 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002516 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002517 path->slots[i]++;
2518 *level = i;
2519 WARN_ON(*level == 0);
2520 return 0;
2521 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002522 struct extent_buffer *parent;
2523 if (path->nodes[*level] == root->node)
2524 parent = path->nodes[*level];
2525 else
2526 parent = path->nodes[*level + 1];
2527
2528 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002529 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002530 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002531 if (ret)
2532 return ret;
2533
Chris Masone02119d2008-09-05 16:13:11 -04002534 if (wc->free) {
2535 struct extent_buffer *next;
2536
2537 next = path->nodes[*level];
2538
Josef Bacik681ae502013-10-07 15:11:00 -04002539 if (trans) {
2540 btrfs_tree_lock(next);
2541 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002542 clean_tree_block(trans, root->fs_info,
2543 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002544 btrfs_wait_tree_block_writeback(next);
2545 btrfs_tree_unlock(next);
2546 }
Chris Masone02119d2008-09-05 16:13:11 -04002547
Chris Masone02119d2008-09-05 16:13:11 -04002548 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002549 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002550 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002551 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002552 if (ret)
2553 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002554 }
2555 free_extent_buffer(path->nodes[*level]);
2556 path->nodes[*level] = NULL;
2557 *level = i + 1;
2558 }
2559 }
2560 return 1;
2561}
2562
2563/*
2564 * drop the reference count on the tree rooted at 'snap'. This traverses
2565 * the tree freeing any blocks that have a ref count of zero after being
2566 * decremented.
2567 */
2568static int walk_log_tree(struct btrfs_trans_handle *trans,
2569 struct btrfs_root *log, struct walk_control *wc)
2570{
2571 int ret = 0;
2572 int wret;
2573 int level;
2574 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002575 int orig_level;
2576
2577 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002578 if (!path)
2579 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002580
2581 level = btrfs_header_level(log->node);
2582 orig_level = level;
2583 path->nodes[level] = log->node;
2584 extent_buffer_get(log->node);
2585 path->slots[level] = 0;
2586
Chris Masond3977122009-01-05 21:25:51 -05002587 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002588 wret = walk_down_log_tree(trans, log, path, &level, wc);
2589 if (wret > 0)
2590 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002591 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002592 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002593 goto out;
2594 }
Chris Masone02119d2008-09-05 16:13:11 -04002595
2596 wret = walk_up_log_tree(trans, log, path, &level, wc);
2597 if (wret > 0)
2598 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002599 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002600 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002601 goto out;
2602 }
Chris Masone02119d2008-09-05 16:13:11 -04002603 }
2604
2605 /* was the root node processed? if not, catch it here */
2606 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002607 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002608 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002609 if (ret)
2610 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002611 if (wc->free) {
2612 struct extent_buffer *next;
2613
2614 next = path->nodes[orig_level];
2615
Josef Bacik681ae502013-10-07 15:11:00 -04002616 if (trans) {
2617 btrfs_tree_lock(next);
2618 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002619 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002620 btrfs_wait_tree_block_writeback(next);
2621 btrfs_tree_unlock(next);
2622 }
Chris Masone02119d2008-09-05 16:13:11 -04002623
Chris Masone02119d2008-09-05 16:13:11 -04002624 WARN_ON(log->root_key.objectid !=
2625 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002626 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002627 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002628 if (ret)
2629 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002630 }
2631 }
2632
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002633out:
Chris Masone02119d2008-09-05 16:13:11 -04002634 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002635 return ret;
2636}
2637
Yan Zheng7237f182009-01-21 12:54:03 -05002638/*
2639 * helper function to update the item for a given subvolumes log root
2640 * in the tree of log roots
2641 */
2642static int update_log_root(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *log)
2644{
2645 int ret;
2646
2647 if (log->log_transid == 1) {
2648 /* insert root item on the first sync */
2649 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2650 &log->root_key, &log->root_item);
2651 } else {
2652 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2653 &log->root_key, &log->root_item);
2654 }
2655 return ret;
2656}
2657
Zhaolei60d53eb2015-08-17 18:44:46 +08002658static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002659{
2660 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002661 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002662
Yan Zheng7237f182009-01-21 12:54:03 -05002663 /*
2664 * we only allow two pending log transactions at a time,
2665 * so we know that if ours is more than 2 older than the
2666 * current transaction, we're done
2667 */
Chris Masone02119d2008-09-05 16:13:11 -04002668 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002669 prepare_to_wait(&root->log_commit_wait[index],
2670 &wait, TASK_UNINTERRUPTIBLE);
2671 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002672
Miao Xied1433de2014-02-20 18:08:59 +08002673 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002674 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002675 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002676
Yan Zheng7237f182009-01-21 12:54:03 -05002677 finish_wait(&root->log_commit_wait[index], &wait);
2678 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002679 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002680 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002681}
2682
Zhaolei60d53eb2015-08-17 18:44:46 +08002683static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002684{
2685 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002686
2687 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002688 prepare_to_wait(&root->log_writer_wait,
2689 &wait, TASK_UNINTERRUPTIBLE);
2690 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002691 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002692 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002693 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002694 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002695 }
Chris Masone02119d2008-09-05 16:13:11 -04002696}
2697
Miao Xie8b050d32014-02-20 18:08:58 +08002698static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2699 struct btrfs_log_ctx *ctx)
2700{
2701 if (!ctx)
2702 return;
2703
2704 mutex_lock(&root->log_mutex);
2705 list_del_init(&ctx->list);
2706 mutex_unlock(&root->log_mutex);
2707}
2708
2709/*
2710 * Invoked in log mutex context, or be sure there is no other task which
2711 * can access the list.
2712 */
2713static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2714 int index, int error)
2715{
2716 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002717 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002718
Chris Mason570dd452016-10-27 10:42:20 -07002719 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2720 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002721 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002722 }
Miao Xie8b050d32014-02-20 18:08:58 +08002723
2724 INIT_LIST_HEAD(&root->log_ctxs[index]);
2725}
2726
Chris Masone02119d2008-09-05 16:13:11 -04002727/*
2728 * btrfs_sync_log does sends a given tree log down to the disk and
2729 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002730 * you know that any inodes previously logged are safely on disk only
2731 * if it returns 0.
2732 *
2733 * Any other return value means you need to call btrfs_commit_transaction.
2734 * Some of the edge cases for fsyncing directories that have had unlinks
2735 * or renames done in the past mean that sometimes the only safe
2736 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2737 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002738 */
2739int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002740 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002741{
Yan Zheng7237f182009-01-21 12:54:03 -05002742 int index1;
2743 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002744 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002745 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002746 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002747 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002748 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002749 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002750 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002751
Yan Zheng7237f182009-01-21 12:54:03 -05002752 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002753 log_transid = ctx->log_transid;
2754 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002755 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002756 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002757 }
Miao Xied1433de2014-02-20 18:08:59 +08002758
2759 index1 = log_transid % 2;
2760 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002761 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002762 mutex_unlock(&root->log_mutex);
2763 return ctx->log_ret;
2764 }
2765 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002766 atomic_set(&root->log_commit[index1], 1);
2767
2768 /* wait for previous tree log sync to complete */
2769 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002770 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002771
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002772 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002773 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002774 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002775 if (!btrfs_test_opt(root->fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002776 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002777 mutex_unlock(&root->log_mutex);
2778 schedule_timeout_uninterruptible(1);
2779 mutex_lock(&root->log_mutex);
2780 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002781 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002782 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002783 break;
2784 }
Chris Masond0c803c2008-09-11 16:17:57 -04002785
Chris Mason12fcfd22009-03-24 10:24:20 -04002786 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002787 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002788 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002789 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002790 mutex_unlock(&root->log_mutex);
2791 goto out;
2792 }
2793
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002794 if (log_transid % 2 == 0)
2795 mark = EXTENT_DIRTY;
2796 else
2797 mark = EXTENT_NEW;
2798
Chris Mason690587d2009-10-13 13:29:19 -04002799 /* we start IO on all the marked extents here, but we don't actually
2800 * wait for them until later.
2801 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002802 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002803 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002804 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002805 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002806 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002807 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002808 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002809 mutex_unlock(&root->log_mutex);
2810 goto out;
2811 }
Yan Zheng7237f182009-01-21 12:54:03 -05002812
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002813 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002814
Yan Zheng7237f182009-01-21 12:54:03 -05002815 root->log_transid++;
2816 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002817 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002818 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002819 * IO has been started, blocks of the log tree have WRITTEN flag set
2820 * in their headers. new modifications of the log will be written to
2821 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002822 */
2823 mutex_unlock(&root->log_mutex);
2824
Filipe Manana28a23592016-08-23 21:13:51 +01002825 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002826
Yan Zheng7237f182009-01-21 12:54:03 -05002827 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002828 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002829 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002830
2831 index2 = log_root_tree->log_transid % 2;
2832 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2833 root_log_ctx.log_transid = log_root_tree->log_transid;
2834
Yan Zheng7237f182009-01-21 12:54:03 -05002835 mutex_unlock(&log_root_tree->log_mutex);
2836
2837 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002838
2839 mutex_lock(&log_root_tree->log_mutex);
2840 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002841 /*
2842 * Implicit memory barrier after atomic_dec_and_test
2843 */
Yan Zheng7237f182009-01-21 12:54:03 -05002844 if (waitqueue_active(&log_root_tree->log_writer_wait))
2845 wake_up(&log_root_tree->log_writer_wait);
2846 }
2847
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002848 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002849 if (!list_empty(&root_log_ctx.list))
2850 list_del_init(&root_log_ctx.list);
2851
Miao Xiec6adc9c2013-05-28 10:05:39 +00002852 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002853 btrfs_set_log_full_commit(root->fs_info, trans);
2854
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002855 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002856 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002857 mutex_unlock(&log_root_tree->log_mutex);
2858 goto out;
2859 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002860 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002861 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002862 mutex_unlock(&log_root_tree->log_mutex);
2863 ret = -EAGAIN;
2864 goto out;
2865 }
2866
Miao Xied1433de2014-02-20 18:08:59 +08002867 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002868 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002869 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002870 mutex_unlock(&log_root_tree->log_mutex);
2871 ret = root_log_ctx.log_ret;
2872 goto out;
2873 }
Miao Xie8b050d32014-02-20 18:08:58 +08002874
Miao Xied1433de2014-02-20 18:08:59 +08002875 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002876 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002877 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002878 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2879 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002880 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002881 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002882 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002883 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002884 if (!ret)
2885 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002886 goto out;
2887 }
Miao Xied1433de2014-02-20 18:08:59 +08002888 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002889 atomic_set(&log_root_tree->log_commit[index2], 1);
2890
Chris Mason12fcfd22009-03-24 10:24:20 -04002891 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002892 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002893 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002894 }
Yan Zheng7237f182009-01-21 12:54:03 -05002895
Zhaolei60d53eb2015-08-17 18:44:46 +08002896 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002897
2898 /*
2899 * now that we've moved on to the tree of log tree roots,
2900 * check the full commit flag again
2901 */
Miao Xie995946d2014-04-02 19:51:06 +08002902 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002903 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002904 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002905 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002906 mutex_unlock(&log_root_tree->log_mutex);
2907 ret = -EAGAIN;
2908 goto out_wake_log_root;
2909 }
Yan Zheng7237f182009-01-21 12:54:03 -05002910
Miao Xiec6adc9c2013-05-28 10:05:39 +00002911 ret = btrfs_write_marked_extents(log_root_tree,
2912 &log_root_tree->dirty_log_pages,
2913 EXTENT_DIRTY | EXTENT_NEW);
2914 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002915 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002916 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002917 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002918 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002919 mutex_unlock(&log_root_tree->log_mutex);
2920 goto out_wake_log_root;
2921 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002922 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2923 if (!ret)
2924 ret = btrfs_wait_marked_extents(log_root_tree,
2925 &log_root_tree->dirty_log_pages,
2926 EXTENT_NEW | EXTENT_DIRTY);
2927 if (ret) {
2928 btrfs_set_log_full_commit(root->fs_info, trans);
2929 btrfs_free_logged_extents(log, log_transid);
2930 mutex_unlock(&log_root_tree->log_mutex);
2931 goto out_wake_log_root;
2932 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002933 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002934
David Sterba6c417612011-04-13 15:41:04 +02002935 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002936 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002937 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002938 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002939
Yan Zheng7237f182009-01-21 12:54:03 -05002940 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002941 mutex_unlock(&log_root_tree->log_mutex);
2942
2943 /*
2944 * nobody else is going to jump in and write the the ctree
2945 * super here because the log_commit atomic below is protecting
2946 * us. We must be called with a transaction handle pinning
2947 * the running transaction open, so a full commit can't hop
2948 * in and cause problems either.
2949 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002950 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002951 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002952 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002953 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002954 goto out_wake_log_root;
2955 }
Yan Zheng7237f182009-01-21 12:54:03 -05002956
Chris Mason257c62e2009-10-13 13:21:08 -04002957 mutex_lock(&root->log_mutex);
2958 if (root->last_log_commit < log_transid)
2959 root->last_log_commit = log_transid;
2960 mutex_unlock(&root->log_mutex);
2961
Chris Mason12fcfd22009-03-24 10:24:20 -04002962out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002963 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002964 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2965
Miao Xied1433de2014-02-20 18:08:59 +08002966 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002967 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002968 mutex_unlock(&log_root_tree->log_mutex);
2969
David Sterba33a9eca2015-10-10 18:35:10 +02002970 /*
2971 * The barrier before waitqueue_active is implied by mutex_unlock
2972 */
Yan Zheng7237f182009-01-21 12:54:03 -05002973 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2974 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002975out:
Miao Xied1433de2014-02-20 18:08:59 +08002976 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002977 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002978 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002979 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002980 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002981
David Sterba33a9eca2015-10-10 18:35:10 +02002982 /*
2983 * The barrier before waitqueue_active is implied by mutex_unlock
2984 */
Yan Zheng7237f182009-01-21 12:54:03 -05002985 if (waitqueue_active(&root->log_commit_wait[index1]))
2986 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002987 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002988}
2989
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002990static void free_log_tree(struct btrfs_trans_handle *trans,
2991 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002992{
2993 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002994 u64 start;
2995 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002996 struct walk_control wc = {
2997 .free = 1,
2998 .process_func = process_one_buffer
2999 };
3000
Josef Bacik681ae502013-10-07 15:11:00 -04003001 ret = walk_log_tree(trans, log, &wc);
3002 /* I don't think this can happen but just in case */
3003 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003004 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003005
Chris Masond3977122009-01-05 21:25:51 -05003006 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003007 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04003008 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
3009 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003010 if (ret)
3011 break;
3012
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003013 clear_extent_bits(&log->dirty_log_pages, start, end,
David Sterba91166212016-04-26 23:54:39 +02003014 EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04003015 }
3016
Josef Bacik2ab28f32012-10-12 15:27:49 -04003017 /*
3018 * We may have short-circuited the log tree with the full commit logic
3019 * and left ordered extents on our list, so clear these out to keep us
3020 * from leaking inodes and memory.
3021 */
3022 btrfs_free_logged_extents(log, 0);
3023 btrfs_free_logged_extents(log, 1);
3024
Yan Zheng7237f182009-01-21 12:54:03 -05003025 free_extent_buffer(log->node);
3026 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003027}
3028
3029/*
3030 * free all the extents used by the tree log. This should be called
3031 * at commit time of the full transaction
3032 */
3033int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3034{
3035 if (root->log_root) {
3036 free_log_tree(trans, root->log_root);
3037 root->log_root = NULL;
3038 }
3039 return 0;
3040}
3041
3042int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3043 struct btrfs_fs_info *fs_info)
3044{
3045 if (fs_info->log_root_tree) {
3046 free_log_tree(trans, fs_info->log_root_tree);
3047 fs_info->log_root_tree = NULL;
3048 }
Chris Masone02119d2008-09-05 16:13:11 -04003049 return 0;
3050}
3051
3052/*
Chris Masone02119d2008-09-05 16:13:11 -04003053 * If both a file and directory are logged, and unlinks or renames are
3054 * mixed in, we have a few interesting corners:
3055 *
3056 * create file X in dir Y
3057 * link file X to X.link in dir Y
3058 * fsync file X
3059 * unlink file X but leave X.link
3060 * fsync dir Y
3061 *
3062 * After a crash we would expect only X.link to exist. But file X
3063 * didn't get fsync'd again so the log has back refs for X and X.link.
3064 *
3065 * We solve this by removing directory entries and inode backrefs from the
3066 * log when a file that was logged in the current transaction is
3067 * unlinked. Any later fsync will include the updated log entries, and
3068 * we'll be able to reconstruct the proper directory items from backrefs.
3069 *
3070 * This optimizations allows us to avoid relogging the entire inode
3071 * or the entire directory.
3072 */
3073int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3074 struct btrfs_root *root,
3075 const char *name, int name_len,
3076 struct inode *dir, u64 index)
3077{
3078 struct btrfs_root *log;
3079 struct btrfs_dir_item *di;
3080 struct btrfs_path *path;
3081 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003082 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003083 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003084 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003085
Chris Mason3a5f1d42008-09-11 15:53:37 -04003086 if (BTRFS_I(dir)->logged_trans < trans->transid)
3087 return 0;
3088
Chris Masone02119d2008-09-05 16:13:11 -04003089 ret = join_running_log_trans(root);
3090 if (ret)
3091 return 0;
3092
3093 mutex_lock(&BTRFS_I(dir)->log_mutex);
3094
3095 log = root->log_root;
3096 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003097 if (!path) {
3098 err = -ENOMEM;
3099 goto out_unlock;
3100 }
liubo2a29edc2011-01-26 06:22:08 +00003101
Li Zefan33345d012011-04-20 10:31:50 +08003102 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003103 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003104 if (IS_ERR(di)) {
3105 err = PTR_ERR(di);
3106 goto fail;
3107 }
3108 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003109 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3110 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003111 if (ret) {
3112 err = ret;
3113 goto fail;
3114 }
Chris Masone02119d2008-09-05 16:13:11 -04003115 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003116 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003117 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003118 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003119 if (IS_ERR(di)) {
3120 err = PTR_ERR(di);
3121 goto fail;
3122 }
3123 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003124 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3125 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003126 if (ret) {
3127 err = ret;
3128 goto fail;
3129 }
Chris Masone02119d2008-09-05 16:13:11 -04003130 }
3131
3132 /* update the directory size in the log to reflect the names
3133 * we have removed
3134 */
3135 if (bytes_del) {
3136 struct btrfs_key key;
3137
Li Zefan33345d012011-04-20 10:31:50 +08003138 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003139 key.offset = 0;
3140 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003141 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003142
3143 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003144 if (ret < 0) {
3145 err = ret;
3146 goto fail;
3147 }
Chris Masone02119d2008-09-05 16:13:11 -04003148 if (ret == 0) {
3149 struct btrfs_inode_item *item;
3150 u64 i_size;
3151
3152 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3153 struct btrfs_inode_item);
3154 i_size = btrfs_inode_size(path->nodes[0], item);
3155 if (i_size > bytes_del)
3156 i_size -= bytes_del;
3157 else
3158 i_size = 0;
3159 btrfs_set_inode_size(path->nodes[0], item, i_size);
3160 btrfs_mark_buffer_dirty(path->nodes[0]);
3161 } else
3162 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003163 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003164 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003165fail:
Chris Masone02119d2008-09-05 16:13:11 -04003166 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003167out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003168 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003169 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003170 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003171 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003172 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003173 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003174
Chris Mason12fcfd22009-03-24 10:24:20 -04003175 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003176
Andi Kleen411fc6b2010-10-29 15:14:31 -04003177 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003178}
3179
3180/* see comments for btrfs_del_dir_entries_in_log */
3181int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3182 struct btrfs_root *root,
3183 const char *name, int name_len,
3184 struct inode *inode, u64 dirid)
3185{
3186 struct btrfs_root *log;
3187 u64 index;
3188 int ret;
3189
Chris Mason3a5f1d42008-09-11 15:53:37 -04003190 if (BTRFS_I(inode)->logged_trans < trans->transid)
3191 return 0;
3192
Chris Masone02119d2008-09-05 16:13:11 -04003193 ret = join_running_log_trans(root);
3194 if (ret)
3195 return 0;
3196 log = root->log_root;
3197 mutex_lock(&BTRFS_I(inode)->log_mutex);
3198
Li Zefan33345d012011-04-20 10:31:50 +08003199 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003200 dirid, &index);
3201 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003202 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003203 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003204 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003205 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003206 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003207 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003208
Chris Masone02119d2008-09-05 16:13:11 -04003209 return ret;
3210}
3211
3212/*
3213 * creates a range item in the log for 'dirid'. first_offset and
3214 * last_offset tell us which parts of the key space the log should
3215 * be considered authoritative for.
3216 */
3217static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3218 struct btrfs_root *log,
3219 struct btrfs_path *path,
3220 int key_type, u64 dirid,
3221 u64 first_offset, u64 last_offset)
3222{
3223 int ret;
3224 struct btrfs_key key;
3225 struct btrfs_dir_log_item *item;
3226
3227 key.objectid = dirid;
3228 key.offset = first_offset;
3229 if (key_type == BTRFS_DIR_ITEM_KEY)
3230 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3231 else
3232 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3233 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003234 if (ret)
3235 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003236
3237 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3238 struct btrfs_dir_log_item);
3239 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3240 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003241 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003242 return 0;
3243}
3244
3245/*
3246 * log all the items included in the current transaction for a given
3247 * directory. This also creates the range items in the log tree required
3248 * to replay anything deleted before the fsync
3249 */
3250static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3251 struct btrfs_root *root, struct inode *inode,
3252 struct btrfs_path *path,
3253 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003254 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003255 u64 min_offset, u64 *last_offset_ret)
3256{
3257 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003258 struct btrfs_root *log = root->log_root;
3259 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003260 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003261 int ret;
3262 int i;
3263 int nritems;
3264 u64 first_offset = min_offset;
3265 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003266 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003267
3268 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003269
Li Zefan33345d012011-04-20 10:31:50 +08003270 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003271 min_key.type = key_type;
3272 min_key.offset = min_offset;
3273
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003274 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003275
3276 /*
3277 * we didn't find anything from this transaction, see if there
3278 * is anything at all
3279 */
Li Zefan33345d012011-04-20 10:31:50 +08003280 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3281 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003282 min_key.type = key_type;
3283 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003284 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003285 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3286 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003287 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003288 return ret;
3289 }
Li Zefan33345d012011-04-20 10:31:50 +08003290 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003291
3292 /* if ret == 0 there are items for this type,
3293 * create a range to tell us the last key of this type.
3294 * otherwise, there are no items in this directory after
3295 * *min_offset, and we create a range to indicate that.
3296 */
3297 if (ret == 0) {
3298 struct btrfs_key tmp;
3299 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3300 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003301 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003302 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003303 }
3304 goto done;
3305 }
3306
3307 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003308 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003309 if (ret == 0) {
3310 struct btrfs_key tmp;
3311 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3312 if (key_type == tmp.type) {
3313 first_offset = tmp.offset;
3314 ret = overwrite_item(trans, log, dst_path,
3315 path->nodes[0], path->slots[0],
3316 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003317 if (ret) {
3318 err = ret;
3319 goto done;
3320 }
Chris Masone02119d2008-09-05 16:13:11 -04003321 }
3322 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003323 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003324
3325 /* find the first key from this transaction again */
3326 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303327 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003328 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003329
3330 /*
3331 * we have a block from this transaction, log every item in it
3332 * from our directory
3333 */
Chris Masond3977122009-01-05 21:25:51 -05003334 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003335 struct btrfs_key tmp;
3336 src = path->nodes[0];
3337 nritems = btrfs_header_nritems(src);
3338 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003339 struct btrfs_dir_item *di;
3340
Chris Masone02119d2008-09-05 16:13:11 -04003341 btrfs_item_key_to_cpu(src, &min_key, i);
3342
Li Zefan33345d012011-04-20 10:31:50 +08003343 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003344 goto done;
3345 ret = overwrite_item(trans, log, dst_path, src, i,
3346 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003347 if (ret) {
3348 err = ret;
3349 goto done;
3350 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003351
3352 /*
3353 * We must make sure that when we log a directory entry,
3354 * the corresponding inode, after log replay, has a
3355 * matching link count. For example:
3356 *
3357 * touch foo
3358 * mkdir mydir
3359 * sync
3360 * ln foo mydir/bar
3361 * xfs_io -c "fsync" mydir
3362 * <crash>
3363 * <mount fs and log replay>
3364 *
3365 * Would result in a fsync log that when replayed, our
3366 * file inode would have a link count of 1, but we get
3367 * two directory entries pointing to the same inode.
3368 * After removing one of the names, it would not be
3369 * possible to remove the other name, which resulted
3370 * always in stale file handle errors, and would not
3371 * be possible to rmdir the parent directory, since
3372 * its i_size could never decrement to the value
3373 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3374 */
3375 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3376 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3377 if (ctx &&
3378 (btrfs_dir_transid(src, di) == trans->transid ||
3379 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3380 tmp.type != BTRFS_ROOT_ITEM_KEY)
3381 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003382 }
3383 path->slots[0] = nritems;
3384
3385 /*
3386 * look ahead to the next item and see if it is also
3387 * from this directory and from this transaction
3388 */
3389 ret = btrfs_next_leaf(root, path);
3390 if (ret == 1) {
3391 last_offset = (u64)-1;
3392 goto done;
3393 }
3394 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003395 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003396 last_offset = (u64)-1;
3397 goto done;
3398 }
3399 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3400 ret = overwrite_item(trans, log, dst_path,
3401 path->nodes[0], path->slots[0],
3402 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003403 if (ret)
3404 err = ret;
3405 else
3406 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003407 goto done;
3408 }
3409 }
3410done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003411 btrfs_release_path(path);
3412 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003413
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003414 if (err == 0) {
3415 *last_offset_ret = last_offset;
3416 /*
3417 * insert the log range keys to indicate where the log
3418 * is valid
3419 */
3420 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003421 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003422 if (ret)
3423 err = ret;
3424 }
3425 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003426}
3427
3428/*
3429 * logging directories is very similar to logging inodes, We find all the items
3430 * from the current transaction and write them to the log.
3431 *
3432 * The recovery code scans the directory in the subvolume, and if it finds a
3433 * key in the range logged that is not present in the log tree, then it means
3434 * that dir entry was unlinked during the transaction.
3435 *
3436 * In order for that scan to work, we must include one key smaller than
3437 * the smallest logged by this transaction and one key larger than the largest
3438 * key logged by this transaction.
3439 */
3440static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3441 struct btrfs_root *root, struct inode *inode,
3442 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003443 struct btrfs_path *dst_path,
3444 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003445{
3446 u64 min_key;
3447 u64 max_key;
3448 int ret;
3449 int key_type = BTRFS_DIR_ITEM_KEY;
3450
3451again:
3452 min_key = 0;
3453 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003454 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003455 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003456 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003457 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003458 if (ret)
3459 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003460 if (max_key == (u64)-1)
3461 break;
3462 min_key = max_key + 1;
3463 }
3464
3465 if (key_type == BTRFS_DIR_ITEM_KEY) {
3466 key_type = BTRFS_DIR_INDEX_KEY;
3467 goto again;
3468 }
3469 return 0;
3470}
3471
3472/*
3473 * a helper function to drop items from the log before we relog an
3474 * inode. max_key_type indicates the highest item type to remove.
3475 * This cannot be run for file data extents because it does not
3476 * free the extents they point to.
3477 */
3478static int drop_objectid_items(struct btrfs_trans_handle *trans,
3479 struct btrfs_root *log,
3480 struct btrfs_path *path,
3481 u64 objectid, int max_key_type)
3482{
3483 int ret;
3484 struct btrfs_key key;
3485 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003486 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003487
3488 key.objectid = objectid;
3489 key.type = max_key_type;
3490 key.offset = (u64)-1;
3491
Chris Masond3977122009-01-05 21:25:51 -05003492 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003493 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003494 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003495 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003496 break;
3497
3498 if (path->slots[0] == 0)
3499 break;
3500
3501 path->slots[0]--;
3502 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3503 path->slots[0]);
3504
3505 if (found_key.objectid != objectid)
3506 break;
3507
Josef Bacik18ec90d2012-09-28 11:56:28 -04003508 found_key.offset = 0;
3509 found_key.type = 0;
3510 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3511 &start_slot);
3512
3513 ret = btrfs_del_items(trans, log, path, start_slot,
3514 path->slots[0] - start_slot + 1);
3515 /*
3516 * If start slot isn't 0 then we don't need to re-search, we've
3517 * found the last guy with the objectid in this tree.
3518 */
3519 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003520 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003521 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003522 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003523 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003524 if (ret > 0)
3525 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003526 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003527}
3528
Josef Bacik94edf4a2012-09-25 14:56:25 -04003529static void fill_inode_item(struct btrfs_trans_handle *trans,
3530 struct extent_buffer *leaf,
3531 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003532 struct inode *inode, int log_inode_only,
3533 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003534{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003535 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003536
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003537 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003538
3539 if (log_inode_only) {
3540 /* set the generation to zero so the recover code
3541 * can tell the difference between an logging
3542 * just to say 'this inode exists' and a logging
3543 * to say 'update this inode with these values'
3544 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003545 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003546 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003547 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003548 btrfs_set_token_inode_generation(leaf, item,
3549 BTRFS_I(inode)->generation,
3550 &token);
3551 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003552 }
3553
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003554 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3555 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3556 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3557 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3558
David Sterbaa937b972014-12-12 17:39:12 +01003559 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003560 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003561 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003562 inode->i_atime.tv_nsec, &token);
3563
David Sterbaa937b972014-12-12 17:39:12 +01003564 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003565 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003566 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003567 inode->i_mtime.tv_nsec, &token);
3568
David Sterbaa937b972014-12-12 17:39:12 +01003569 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003570 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003571 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003572 inode->i_ctime.tv_nsec, &token);
3573
3574 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3575 &token);
3576
3577 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3578 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3579 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3580 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3581 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003582}
3583
Josef Bacika95249b2012-10-11 16:17:34 -04003584static int log_inode_item(struct btrfs_trans_handle *trans,
3585 struct btrfs_root *log, struct btrfs_path *path,
3586 struct inode *inode)
3587{
3588 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003589 int ret;
3590
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003591 ret = btrfs_insert_empty_item(trans, log, path,
3592 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003593 sizeof(*inode_item));
3594 if (ret && ret != -EEXIST)
3595 return ret;
3596 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3597 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003598 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003599 btrfs_release_path(path);
3600 return 0;
3601}
3602
Chris Mason31ff1cd2008-09-11 16:17:57 -04003603static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003604 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003605 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003606 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003607 int start_slot, int nr, int inode_only,
3608 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003609{
3610 unsigned long src_offset;
3611 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003612 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003613 struct btrfs_file_extent_item *extent;
3614 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003615 struct extent_buffer *src = src_path->nodes[0];
3616 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003617 int ret;
3618 struct btrfs_key *ins_keys;
3619 u32 *ins_sizes;
3620 char *ins_data;
3621 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003622 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003623 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003624 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003625 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003626 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003627
3628 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003629
3630 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3631 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003632 if (!ins_data)
3633 return -ENOMEM;
3634
Josef Bacik16e75492013-10-22 12:18:51 -04003635 first_key.objectid = (u64)-1;
3636
Chris Mason31ff1cd2008-09-11 16:17:57 -04003637 ins_sizes = (u32 *)ins_data;
3638 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3639
3640 for (i = 0; i < nr; i++) {
3641 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3642 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3643 }
3644 ret = btrfs_insert_empty_items(trans, log, dst_path,
3645 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003646 if (ret) {
3647 kfree(ins_data);
3648 return ret;
3649 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003650
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003651 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003652 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3653 dst_path->slots[0]);
3654
3655 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3656
Josef Bacik16e75492013-10-22 12:18:51 -04003657 if ((i == (nr - 1)))
3658 last_key = ins_keys[i];
3659
Josef Bacik94edf4a2012-09-25 14:56:25 -04003660 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003661 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3662 dst_path->slots[0],
3663 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003664 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003665 inode, inode_only == LOG_INODE_EXISTS,
3666 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003667 } else {
3668 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3669 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003670 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003671
Josef Bacik16e75492013-10-22 12:18:51 -04003672 /*
3673 * We set need_find_last_extent here in case we know we were
3674 * processing other items and then walk into the first extent in
3675 * the inode. If we don't hit an extent then nothing changes,
3676 * we'll do the last search the next time around.
3677 */
3678 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3679 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003680 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003681 first_key = ins_keys[i];
3682 } else {
3683 need_find_last_extent = false;
3684 }
3685
Chris Mason31ff1cd2008-09-11 16:17:57 -04003686 /* take a reference on file data extents so that truncates
3687 * or deletes of this inode don't have to relog the inode
3688 * again
3689 */
David Sterba962a2982014-06-04 18:41:45 +02003690 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003691 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003692 int found_type;
3693 extent = btrfs_item_ptr(src, start_slot + i,
3694 struct btrfs_file_extent_item);
3695
liubo8e531cd2011-05-06 10:36:09 +08003696 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3697 continue;
3698
Chris Mason31ff1cd2008-09-11 16:17:57 -04003699 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003700 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003701 u64 ds, dl, cs, cl;
3702 ds = btrfs_file_extent_disk_bytenr(src,
3703 extent);
3704 /* ds == 0 is a hole */
3705 if (ds == 0)
3706 continue;
3707
3708 dl = btrfs_file_extent_disk_num_bytes(src,
3709 extent);
3710 cs = btrfs_file_extent_offset(src, extent);
3711 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003712 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003713 if (btrfs_file_extent_compression(src,
3714 extent)) {
3715 cs = 0;
3716 cl = dl;
3717 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003718
3719 ret = btrfs_lookup_csums_range(
3720 log->fs_info->csum_root,
3721 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003722 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003723 if (ret) {
3724 btrfs_release_path(dst_path);
3725 kfree(ins_data);
3726 return ret;
3727 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003728 }
3729 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003730 }
3731
3732 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003733 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003734 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003735
3736 /*
3737 * we have to do this after the loop above to avoid changing the
3738 * log tree while trying to change the log tree.
3739 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003740 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003741 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003742 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3743 struct btrfs_ordered_sum,
3744 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003745 if (!ret)
3746 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003747 list_del(&sums->list);
3748 kfree(sums);
3749 }
Josef Bacik16e75492013-10-22 12:18:51 -04003750
3751 if (!has_extents)
3752 return ret;
3753
Filipe Manana74121f7c2014-08-07 12:00:44 +01003754 if (need_find_last_extent && *last_extent == first_key.offset) {
3755 /*
3756 * We don't have any leafs between our current one and the one
3757 * we processed before that can have file extent items for our
3758 * inode (and have a generation number smaller than our current
3759 * transaction id).
3760 */
3761 need_find_last_extent = false;
3762 }
3763
Josef Bacik16e75492013-10-22 12:18:51 -04003764 /*
3765 * Because we use btrfs_search_forward we could skip leaves that were
3766 * not modified and then assume *last_extent is valid when it really
3767 * isn't. So back up to the previous leaf and read the end of the last
3768 * extent before we go and fill in holes.
3769 */
3770 if (need_find_last_extent) {
3771 u64 len;
3772
3773 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3774 if (ret < 0)
3775 return ret;
3776 if (ret)
3777 goto fill_holes;
3778 if (src_path->slots[0])
3779 src_path->slots[0]--;
3780 src = src_path->nodes[0];
3781 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3782 if (key.objectid != btrfs_ino(inode) ||
3783 key.type != BTRFS_EXTENT_DATA_KEY)
3784 goto fill_holes;
3785 extent = btrfs_item_ptr(src, src_path->slots[0],
3786 struct btrfs_file_extent_item);
3787 if (btrfs_file_extent_type(src, extent) ==
3788 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003789 len = btrfs_file_extent_inline_len(src,
3790 src_path->slots[0],
3791 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003792 *last_extent = ALIGN(key.offset + len,
Jeff Mahoneyda170662016-06-15 09:22:56 -04003793 log->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003794 } else {
3795 len = btrfs_file_extent_num_bytes(src, extent);
3796 *last_extent = key.offset + len;
3797 }
3798 }
3799fill_holes:
3800 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3801 * things could have happened
3802 *
3803 * 1) A merge could have happened, so we could currently be on a leaf
3804 * that holds what we were copying in the first place.
3805 * 2) A split could have happened, and now not all of the items we want
3806 * are on the same leaf.
3807 *
3808 * So we need to adjust how we search for holes, we need to drop the
3809 * path and re-search for the first extent key we found, and then walk
3810 * forward until we hit the last one we copied.
3811 */
3812 if (need_find_last_extent) {
3813 /* btrfs_prev_leaf could return 1 without releasing the path */
3814 btrfs_release_path(src_path);
3815 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3816 src_path, 0, 0);
3817 if (ret < 0)
3818 return ret;
3819 ASSERT(ret == 0);
3820 src = src_path->nodes[0];
3821 i = src_path->slots[0];
3822 } else {
3823 i = start_slot;
3824 }
3825
3826 /*
3827 * Ok so here we need to go through and fill in any holes we may have
3828 * to make sure that holes are punched for those areas in case they had
3829 * extents previously.
3830 */
3831 while (!done) {
3832 u64 offset, len;
3833 u64 extent_end;
3834
3835 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3836 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3837 if (ret < 0)
3838 return ret;
3839 ASSERT(ret == 0);
3840 src = src_path->nodes[0];
3841 i = 0;
3842 }
3843
3844 btrfs_item_key_to_cpu(src, &key, i);
3845 if (!btrfs_comp_cpu_keys(&key, &last_key))
3846 done = true;
3847 if (key.objectid != btrfs_ino(inode) ||
3848 key.type != BTRFS_EXTENT_DATA_KEY) {
3849 i++;
3850 continue;
3851 }
3852 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3853 if (btrfs_file_extent_type(src, extent) ==
3854 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003855 len = btrfs_file_extent_inline_len(src, i, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04003856 extent_end = ALIGN(key.offset + len,
3857 log->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003858 } else {
3859 len = btrfs_file_extent_num_bytes(src, extent);
3860 extent_end = key.offset + len;
3861 }
3862 i++;
3863
3864 if (*last_extent == key.offset) {
3865 *last_extent = extent_end;
3866 continue;
3867 }
3868 offset = *last_extent;
3869 len = key.offset - *last_extent;
3870 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3871 offset, 0, 0, len, 0, len, 0,
3872 0, 0);
3873 if (ret)
3874 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003875 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003876 }
3877 /*
3878 * Need to let the callers know we dropped the path so they should
3879 * re-search.
3880 */
3881 if (!ret && need_find_last_extent)
3882 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003883 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003884}
3885
Josef Bacik5dc562c2012-08-17 13:14:17 -04003886static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3887{
3888 struct extent_map *em1, *em2;
3889
3890 em1 = list_entry(a, struct extent_map, list);
3891 em2 = list_entry(b, struct extent_map, list);
3892
3893 if (em1->start < em2->start)
3894 return -1;
3895 else if (em1->start > em2->start)
3896 return 1;
3897 return 0;
3898}
3899
Filipe Manana8407f552014-09-05 15:14:39 +01003900static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3901 struct inode *inode,
3902 struct btrfs_root *root,
3903 const struct extent_map *em,
3904 const struct list_head *logged_list,
3905 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003906{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003907 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003908 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003909 u64 mod_start = em->mod_start;
3910 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003911 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003912 u64 csum_offset;
3913 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003914 LIST_HEAD(ordered_sums);
3915 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003916
Filipe Manana8407f552014-09-05 15:14:39 +01003917 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003918
Filipe Manana8407f552014-09-05 15:14:39 +01003919 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3920 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003921 return 0;
3922
Josef Bacik2ab28f32012-10-12 15:27:49 -04003923 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003924 * Wait far any ordered extent that covers our extent map. If it
3925 * finishes without an error, first check and see if our csums are on
3926 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003927 */
Miao Xie827463c2014-01-14 20:31:51 +08003928 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003929 struct btrfs_ordered_sum *sum;
3930
3931 if (!mod_len)
3932 break;
3933
Josef Bacik2ab28f32012-10-12 15:27:49 -04003934 if (ordered->file_offset + ordered->len <= mod_start ||
3935 mod_start + mod_len <= ordered->file_offset)
3936 continue;
3937
Filipe Manana8407f552014-09-05 15:14:39 +01003938 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3939 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3940 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3941 const u64 start = ordered->file_offset;
3942 const u64 end = ordered->file_offset + ordered->len - 1;
3943
3944 WARN_ON(ordered->inode != inode);
3945 filemap_fdatawrite_range(inode->i_mapping, start, end);
3946 }
3947
3948 wait_event(ordered->wait,
3949 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3950 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3951
3952 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003953 /*
3954 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3955 * i_mapping flags, so that the next fsync won't get
3956 * an outdated io error too.
3957 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003958 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003959 *ordered_io_error = true;
3960 break;
3961 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003962 /*
3963 * We are going to copy all the csums on this ordered extent, so
3964 * go ahead and adjust mod_start and mod_len in case this
3965 * ordered extent has already been logged.
3966 */
3967 if (ordered->file_offset > mod_start) {
3968 if (ordered->file_offset + ordered->len >=
3969 mod_start + mod_len)
3970 mod_len = ordered->file_offset - mod_start;
3971 /*
3972 * If we have this case
3973 *
3974 * |--------- logged extent ---------|
3975 * |----- ordered extent ----|
3976 *
3977 * Just don't mess with mod_start and mod_len, we'll
3978 * just end up logging more csums than we need and it
3979 * will be ok.
3980 */
3981 } else {
3982 if (ordered->file_offset + ordered->len <
3983 mod_start + mod_len) {
3984 mod_len = (mod_start + mod_len) -
3985 (ordered->file_offset + ordered->len);
3986 mod_start = ordered->file_offset +
3987 ordered->len;
3988 } else {
3989 mod_len = 0;
3990 }
3991 }
3992
Filipe Manana8407f552014-09-05 15:14:39 +01003993 if (skip_csum)
3994 continue;
3995
Josef Bacik2ab28f32012-10-12 15:27:49 -04003996 /*
3997 * To keep us from looping for the above case of an ordered
3998 * extent that falls inside of the logged extent.
3999 */
4000 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4001 &ordered->flags))
4002 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004003
Josef Bacik2ab28f32012-10-12 15:27:49 -04004004 list_for_each_entry(sum, &ordered->list, list) {
4005 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004006 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004007 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004008 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004009 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004010
Filipe Manana8407f552014-09-05 15:14:39 +01004011 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004012 return ret;
4013
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004014 if (em->compress_type) {
4015 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004016 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004017 } else {
4018 csum_offset = mod_start - em->start;
4019 csum_len = mod_len;
4020 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004021
Josef Bacik70c8a912012-10-11 16:54:30 -04004022 /* block start is already adjusted for the file extent offset. */
4023 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4024 em->block_start + csum_offset,
4025 em->block_start + csum_offset +
4026 csum_len - 1, &ordered_sums, 0);
4027 if (ret)
4028 return ret;
4029
4030 while (!list_empty(&ordered_sums)) {
4031 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4032 struct btrfs_ordered_sum,
4033 list);
4034 if (!ret)
4035 ret = btrfs_csum_file_blocks(trans, log, sums);
4036 list_del(&sums->list);
4037 kfree(sums);
4038 }
4039
4040 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004041}
4042
Filipe Manana8407f552014-09-05 15:14:39 +01004043static int log_one_extent(struct btrfs_trans_handle *trans,
4044 struct inode *inode, struct btrfs_root *root,
4045 const struct extent_map *em,
4046 struct btrfs_path *path,
4047 const struct list_head *logged_list,
4048 struct btrfs_log_ctx *ctx)
4049{
4050 struct btrfs_root *log = root->log_root;
4051 struct btrfs_file_extent_item *fi;
4052 struct extent_buffer *leaf;
4053 struct btrfs_map_token token;
4054 struct btrfs_key key;
4055 u64 extent_offset = em->start - em->orig_start;
4056 u64 block_len;
4057 int ret;
4058 int extent_inserted = 0;
4059 bool ordered_io_err = false;
4060
4061 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4062 &ordered_io_err);
4063 if (ret)
4064 return ret;
4065
4066 if (ordered_io_err) {
4067 ctx->io_err = -EIO;
4068 return 0;
4069 }
4070
4071 btrfs_init_map_token(&token);
4072
4073 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4074 em->start + em->len, NULL, 0, 1,
4075 sizeof(*fi), &extent_inserted);
4076 if (ret)
4077 return ret;
4078
4079 if (!extent_inserted) {
4080 key.objectid = btrfs_ino(inode);
4081 key.type = BTRFS_EXTENT_DATA_KEY;
4082 key.offset = em->start;
4083
4084 ret = btrfs_insert_empty_item(trans, log, path, &key,
4085 sizeof(*fi));
4086 if (ret)
4087 return ret;
4088 }
4089 leaf = path->nodes[0];
4090 fi = btrfs_item_ptr(leaf, path->slots[0],
4091 struct btrfs_file_extent_item);
4092
Josef Bacik50d9aa92014-11-21 14:52:38 -05004093 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004094 &token);
4095 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4096 btrfs_set_token_file_extent_type(leaf, fi,
4097 BTRFS_FILE_EXTENT_PREALLOC,
4098 &token);
4099 else
4100 btrfs_set_token_file_extent_type(leaf, fi,
4101 BTRFS_FILE_EXTENT_REG,
4102 &token);
4103
4104 block_len = max(em->block_len, em->orig_block_len);
4105 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4106 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4107 em->block_start,
4108 &token);
4109 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4110 &token);
4111 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4112 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4113 em->block_start -
4114 extent_offset, &token);
4115 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4116 &token);
4117 } else {
4118 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4119 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4120 &token);
4121 }
4122
4123 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4124 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4125 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4126 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4127 &token);
4128 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4129 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4130 btrfs_mark_buffer_dirty(leaf);
4131
4132 btrfs_release_path(path);
4133
4134 return ret;
4135}
4136
Josef Bacik5dc562c2012-08-17 13:14:17 -04004137static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4138 struct btrfs_root *root,
4139 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004140 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004141 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004142 struct btrfs_log_ctx *ctx,
4143 const u64 start,
4144 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004145{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004146 struct extent_map *em, *n;
4147 struct list_head extents;
4148 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4149 u64 test_gen;
4150 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004151 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004152
4153 INIT_LIST_HEAD(&extents);
4154
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004155 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004156 write_lock(&tree->lock);
4157 test_gen = root->fs_info->last_trans_committed;
4158
4159 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4160 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004161
4162 /*
4163 * Just an arbitrary number, this can be really CPU intensive
4164 * once we start getting a lot of extents, and really once we
4165 * have a bunch of extents we just want to commit since it will
4166 * be faster.
4167 */
4168 if (++num > 32768) {
4169 list_del_init(&tree->modified_extents);
4170 ret = -EFBIG;
4171 goto process;
4172 }
4173
Josef Bacik5dc562c2012-08-17 13:14:17 -04004174 if (em->generation <= test_gen)
4175 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004176 /* Need a ref to keep it from getting evicted from cache */
4177 atomic_inc(&em->refs);
4178 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004179 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004180 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004181 }
4182
4183 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004184 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004185 /*
4186 * Some ordered extents started by fsync might have completed
4187 * before we could collect them into the list logged_list, which
4188 * means they're gone, not in our logged_list nor in the inode's
4189 * ordered tree. We want the application/user space to know an
4190 * error happened while attempting to persist file data so that
4191 * it can take proper action. If such error happened, we leave
4192 * without writing to the log tree and the fsync must report the
4193 * file data write error and not commit the current transaction.
4194 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004195 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004196 if (ret)
4197 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004198process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004199 while (!list_empty(&extents)) {
4200 em = list_entry(extents.next, struct extent_map, list);
4201
4202 list_del_init(&em->list);
4203
4204 /*
4205 * If we had an error we just need to delete everybody from our
4206 * private list.
4207 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004208 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004209 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004210 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004211 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004212 }
4213
4214 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004215
Filipe Manana8407f552014-09-05 15:14:39 +01004216 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4217 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004218 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004219 clear_em_logging(tree, em);
4220 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004221 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004222 WARN_ON(!list_empty(&extents));
4223 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004224 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004225
Josef Bacik5dc562c2012-08-17 13:14:17 -04004226 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004227 return ret;
4228}
4229
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004230static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4231 struct btrfs_path *path, u64 *size_ret)
4232{
4233 struct btrfs_key key;
4234 int ret;
4235
4236 key.objectid = btrfs_ino(inode);
4237 key.type = BTRFS_INODE_ITEM_KEY;
4238 key.offset = 0;
4239
4240 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4241 if (ret < 0) {
4242 return ret;
4243 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004244 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004245 } else {
4246 struct btrfs_inode_item *item;
4247
4248 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4249 struct btrfs_inode_item);
4250 *size_ret = btrfs_inode_size(path->nodes[0], item);
4251 }
4252
4253 btrfs_release_path(path);
4254 return 0;
4255}
4256
Filipe Manana36283bf2015-06-20 00:44:51 +01004257/*
4258 * At the moment we always log all xattrs. This is to figure out at log replay
4259 * time which xattrs must have their deletion replayed. If a xattr is missing
4260 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4261 * because if a xattr is deleted, the inode is fsynced and a power failure
4262 * happens, causing the log to be replayed the next time the fs is mounted,
4263 * we want the xattr to not exist anymore (same behaviour as other filesystems
4264 * with a journal, ext3/4, xfs, f2fs, etc).
4265 */
4266static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4267 struct btrfs_root *root,
4268 struct inode *inode,
4269 struct btrfs_path *path,
4270 struct btrfs_path *dst_path)
4271{
4272 int ret;
4273 struct btrfs_key key;
4274 const u64 ino = btrfs_ino(inode);
4275 int ins_nr = 0;
4276 int start_slot = 0;
4277
4278 key.objectid = ino;
4279 key.type = BTRFS_XATTR_ITEM_KEY;
4280 key.offset = 0;
4281
4282 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4283 if (ret < 0)
4284 return ret;
4285
4286 while (true) {
4287 int slot = path->slots[0];
4288 struct extent_buffer *leaf = path->nodes[0];
4289 int nritems = btrfs_header_nritems(leaf);
4290
4291 if (slot >= nritems) {
4292 if (ins_nr > 0) {
4293 u64 last_extent = 0;
4294
4295 ret = copy_items(trans, inode, dst_path, path,
4296 &last_extent, start_slot,
4297 ins_nr, 1, 0);
4298 /* can't be 1, extent items aren't processed */
4299 ASSERT(ret <= 0);
4300 if (ret < 0)
4301 return ret;
4302 ins_nr = 0;
4303 }
4304 ret = btrfs_next_leaf(root, path);
4305 if (ret < 0)
4306 return ret;
4307 else if (ret > 0)
4308 break;
4309 continue;
4310 }
4311
4312 btrfs_item_key_to_cpu(leaf, &key, slot);
4313 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4314 break;
4315
4316 if (ins_nr == 0)
4317 start_slot = slot;
4318 ins_nr++;
4319 path->slots[0]++;
4320 cond_resched();
4321 }
4322 if (ins_nr > 0) {
4323 u64 last_extent = 0;
4324
4325 ret = copy_items(trans, inode, dst_path, path,
4326 &last_extent, start_slot,
4327 ins_nr, 1, 0);
4328 /* can't be 1, extent items aren't processed */
4329 ASSERT(ret <= 0);
4330 if (ret < 0)
4331 return ret;
4332 }
4333
4334 return 0;
4335}
4336
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004337/*
4338 * If the no holes feature is enabled we need to make sure any hole between the
4339 * last extent and the i_size of our inode is explicitly marked in the log. This
4340 * is to make sure that doing something like:
4341 *
4342 * 1) create file with 128Kb of data
4343 * 2) truncate file to 64Kb
4344 * 3) truncate file to 256Kb
4345 * 4) fsync file
4346 * 5) <crash/power failure>
4347 * 6) mount fs and trigger log replay
4348 *
4349 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4350 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4351 * file correspond to a hole. The presence of explicit holes in a log tree is
4352 * what guarantees that log replay will remove/adjust file extent items in the
4353 * fs/subvol tree.
4354 *
4355 * Here we do not need to care about holes between extents, that is already done
4356 * by copy_items(). We also only need to do this in the full sync path, where we
4357 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4358 * lookup the list of modified extent maps and if any represents a hole, we
4359 * insert a corresponding extent representing a hole in the log tree.
4360 */
4361static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4362 struct btrfs_root *root,
4363 struct inode *inode,
4364 struct btrfs_path *path)
4365{
4366 int ret;
4367 struct btrfs_key key;
4368 u64 hole_start;
4369 u64 hole_size;
4370 struct extent_buffer *leaf;
4371 struct btrfs_root *log = root->log_root;
4372 const u64 ino = btrfs_ino(inode);
4373 const u64 i_size = i_size_read(inode);
4374
4375 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4376 return 0;
4377
4378 key.objectid = ino;
4379 key.type = BTRFS_EXTENT_DATA_KEY;
4380 key.offset = (u64)-1;
4381
4382 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4383 ASSERT(ret != 0);
4384 if (ret < 0)
4385 return ret;
4386
4387 ASSERT(path->slots[0] > 0);
4388 path->slots[0]--;
4389 leaf = path->nodes[0];
4390 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4391
4392 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4393 /* inode does not have any extents */
4394 hole_start = 0;
4395 hole_size = i_size;
4396 } else {
4397 struct btrfs_file_extent_item *extent;
4398 u64 len;
4399
4400 /*
4401 * If there's an extent beyond i_size, an explicit hole was
4402 * already inserted by copy_items().
4403 */
4404 if (key.offset >= i_size)
4405 return 0;
4406
4407 extent = btrfs_item_ptr(leaf, path->slots[0],
4408 struct btrfs_file_extent_item);
4409
4410 if (btrfs_file_extent_type(leaf, extent) ==
4411 BTRFS_FILE_EXTENT_INLINE) {
4412 len = btrfs_file_extent_inline_len(leaf,
4413 path->slots[0],
4414 extent);
4415 ASSERT(len == i_size);
4416 return 0;
4417 }
4418
4419 len = btrfs_file_extent_num_bytes(leaf, extent);
4420 /* Last extent goes beyond i_size, no need to log a hole. */
4421 if (key.offset + len > i_size)
4422 return 0;
4423 hole_start = key.offset + len;
4424 hole_size = i_size - hole_start;
4425 }
4426 btrfs_release_path(path);
4427
4428 /* Last extent ends at i_size. */
4429 if (hole_size == 0)
4430 return 0;
4431
Jeff Mahoneyda170662016-06-15 09:22:56 -04004432 hole_size = ALIGN(hole_size, root->fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004433 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4434 hole_size, 0, hole_size, 0, 0, 0);
4435 return ret;
4436}
4437
Filipe Manana56f23fd2016-03-30 23:37:21 +01004438/*
4439 * When we are logging a new inode X, check if it doesn't have a reference that
4440 * matches the reference from some other inode Y created in a past transaction
4441 * and that was renamed in the current transaction. If we don't do this, then at
4442 * log replay time we can lose inode Y (and all its files if it's a directory):
4443 *
4444 * mkdir /mnt/x
4445 * echo "hello world" > /mnt/x/foobar
4446 * sync
4447 * mv /mnt/x /mnt/y
4448 * mkdir /mnt/x # or touch /mnt/x
4449 * xfs_io -c fsync /mnt/x
4450 * <power fail>
4451 * mount fs, trigger log replay
4452 *
4453 * After the log replay procedure, we would lose the first directory and all its
4454 * files (file foobar).
4455 * For the case where inode Y is not a directory we simply end up losing it:
4456 *
4457 * echo "123" > /mnt/foo
4458 * sync
4459 * mv /mnt/foo /mnt/bar
4460 * echo "abc" > /mnt/foo
4461 * xfs_io -c fsync /mnt/foo
4462 * <power fail>
4463 *
4464 * We also need this for cases where a snapshot entry is replaced by some other
4465 * entry (file or directory) otherwise we end up with an unreplayable log due to
4466 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4467 * if it were a regular entry:
4468 *
4469 * mkdir /mnt/x
4470 * btrfs subvolume snapshot /mnt /mnt/x/snap
4471 * btrfs subvolume delete /mnt/x/snap
4472 * rmdir /mnt/x
4473 * mkdir /mnt/x
4474 * fsync /mnt/x or fsync some new file inside it
4475 * <power fail>
4476 *
4477 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4478 * the same transaction.
4479 */
4480static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4481 const int slot,
4482 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004483 struct inode *inode,
4484 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004485{
4486 int ret;
4487 struct btrfs_path *search_path;
4488 char *name = NULL;
4489 u32 name_len = 0;
4490 u32 item_size = btrfs_item_size_nr(eb, slot);
4491 u32 cur_offset = 0;
4492 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4493
4494 search_path = btrfs_alloc_path();
4495 if (!search_path)
4496 return -ENOMEM;
4497 search_path->search_commit_root = 1;
4498 search_path->skip_locking = 1;
4499
4500 while (cur_offset < item_size) {
4501 u64 parent;
4502 u32 this_name_len;
4503 u32 this_len;
4504 unsigned long name_ptr;
4505 struct btrfs_dir_item *di;
4506
4507 if (key->type == BTRFS_INODE_REF_KEY) {
4508 struct btrfs_inode_ref *iref;
4509
4510 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4511 parent = key->offset;
4512 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4513 name_ptr = (unsigned long)(iref + 1);
4514 this_len = sizeof(*iref) + this_name_len;
4515 } else {
4516 struct btrfs_inode_extref *extref;
4517
4518 extref = (struct btrfs_inode_extref *)(ptr +
4519 cur_offset);
4520 parent = btrfs_inode_extref_parent(eb, extref);
4521 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4522 name_ptr = (unsigned long)&extref->name;
4523 this_len = sizeof(*extref) + this_name_len;
4524 }
4525
4526 if (this_name_len > name_len) {
4527 char *new_name;
4528
4529 new_name = krealloc(name, this_name_len, GFP_NOFS);
4530 if (!new_name) {
4531 ret = -ENOMEM;
4532 goto out;
4533 }
4534 name_len = this_name_len;
4535 name = new_name;
4536 }
4537
4538 read_extent_buffer(eb, name, name_ptr, this_name_len);
4539 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4540 search_path, parent,
4541 name, this_name_len, 0);
4542 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004543 struct btrfs_key di_key;
4544
4545 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4546 di, &di_key);
4547 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4548 ret = 1;
4549 *other_ino = di_key.objectid;
4550 } else {
4551 ret = -EAGAIN;
4552 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004553 goto out;
4554 } else if (IS_ERR(di)) {
4555 ret = PTR_ERR(di);
4556 goto out;
4557 }
4558 btrfs_release_path(search_path);
4559
4560 cur_offset += this_len;
4561 }
4562 ret = 0;
4563out:
4564 btrfs_free_path(search_path);
4565 kfree(name);
4566 return ret;
4567}
4568
Chris Masone02119d2008-09-05 16:13:11 -04004569/* log a single inode in the tree log.
4570 * At least one parent directory for this inode must exist in the tree
4571 * or be logged already.
4572 *
4573 * Any items from this inode changed by the current transaction are copied
4574 * to the log tree. An extra reference is taken on any extents in this
4575 * file, allowing us to avoid a whole pile of corner cases around logging
4576 * blocks that have been removed from the tree.
4577 *
4578 * See LOG_INODE_ALL and related defines for a description of what inode_only
4579 * does.
4580 *
4581 * This handles both files and directories.
4582 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004583static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004584 struct btrfs_root *root, struct inode *inode,
4585 int inode_only,
4586 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004587 const loff_t end,
4588 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004589{
4590 struct btrfs_path *path;
4591 struct btrfs_path *dst_path;
4592 struct btrfs_key min_key;
4593 struct btrfs_key max_key;
4594 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004595 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004596 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004597 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004598 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004599 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004600 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004601 int ins_start_slot = 0;
4602 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004603 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004604 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004605 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004606 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004607 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004608
Chris Masone02119d2008-09-05 16:13:11 -04004609 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004610 if (!path)
4611 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004612 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004613 if (!dst_path) {
4614 btrfs_free_path(path);
4615 return -ENOMEM;
4616 }
Chris Masone02119d2008-09-05 16:13:11 -04004617
Li Zefan33345d012011-04-20 10:31:50 +08004618 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004619 min_key.type = BTRFS_INODE_ITEM_KEY;
4620 min_key.offset = 0;
4621
Li Zefan33345d012011-04-20 10:31:50 +08004622 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004623
Chris Mason12fcfd22009-03-24 10:24:20 -04004624
Josef Bacik5dc562c2012-08-17 13:14:17 -04004625 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004626 if (S_ISDIR(inode->i_mode) ||
4627 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4628 &BTRFS_I(inode)->runtime_flags) &&
4629 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004630 max_key.type = BTRFS_XATTR_ITEM_KEY;
4631 else
4632 max_key.type = (u8)-1;
4633 max_key.offset = (u64)-1;
4634
Filipe Manana2c2c4522015-01-13 16:40:04 +00004635 /*
4636 * Only run delayed items if we are a dir or a new file.
4637 * Otherwise commit the delayed inode only, which is needed in
4638 * order for the log replay code to mark inodes for link count
4639 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4640 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004641 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004642 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004643 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004644 else
4645 ret = btrfs_commit_inode_delayed_inode(inode);
4646
4647 if (ret) {
4648 btrfs_free_path(path);
4649 btrfs_free_path(dst_path);
4650 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004651 }
4652
Chris Masone02119d2008-09-05 16:13:11 -04004653 mutex_lock(&BTRFS_I(inode)->log_mutex);
4654
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004655 /*
Chris Masone02119d2008-09-05 16:13:11 -04004656 * a brute force approach to making sure we get the most uptodate
4657 * copies of everything.
4658 */
4659 if (S_ISDIR(inode->i_mode)) {
4660 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4661
Filipe Manana4f764e52015-02-23 19:53:35 +00004662 if (inode_only == LOG_INODE_EXISTS)
4663 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004664 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004665 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004666 if (inode_only == LOG_INODE_EXISTS) {
4667 /*
4668 * Make sure the new inode item we write to the log has
4669 * the same isize as the current one (if it exists).
4670 * This is necessary to prevent data loss after log
4671 * replay, and also to prevent doing a wrong expanding
4672 * truncate - for e.g. create file, write 4K into offset
4673 * 0, fsync, write 4K into offset 4096, add hard link,
4674 * fsync some other file (to sync log), power fail - if
4675 * we use the inode's current i_size, after log replay
4676 * we get a 8Kb file, with the last 4Kb extent as a hole
4677 * (zeroes), as if an expanding truncate happened,
4678 * instead of getting a file of 4Kb only.
4679 */
4680 err = logged_inode_size(log, inode, path,
4681 &logged_isize);
4682 if (err)
4683 goto out_unlock;
4684 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004685 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4686 &BTRFS_I(inode)->runtime_flags)) {
4687 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004688 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004689 ret = drop_objectid_items(trans, log, path, ino,
4690 max_key.type);
4691 } else {
4692 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4693 &BTRFS_I(inode)->runtime_flags);
4694 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4695 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004696 while(1) {
4697 ret = btrfs_truncate_inode_items(trans,
4698 log, inode, 0, 0);
4699 if (ret != -EAGAIN)
4700 break;
4701 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004702 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004703 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4704 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004705 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004706 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004707 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004708 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004709 ret = drop_objectid_items(trans, log, path, ino,
4710 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004711 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004712 if (inode_only == LOG_INODE_ALL)
4713 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004714 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004715 }
Josef Bacika95249b2012-10-11 16:17:34 -04004716
Chris Masone02119d2008-09-05 16:13:11 -04004717 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004718 if (ret) {
4719 err = ret;
4720 goto out_unlock;
4721 }
Chris Masone02119d2008-09-05 16:13:11 -04004722
Chris Masond3977122009-01-05 21:25:51 -05004723 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004724 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004725 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004726 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004727 if (ret < 0) {
4728 err = ret;
4729 goto out_unlock;
4730 }
Chris Masone02119d2008-09-05 16:13:11 -04004731 if (ret != 0)
4732 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004733again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004734 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004735 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004736 break;
4737 if (min_key.type > max_key.type)
4738 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004739
Filipe Mananae4545de2015-06-17 12:49:23 +01004740 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4741 need_log_inode_item = false;
4742
Filipe Manana56f23fd2016-03-30 23:37:21 +01004743 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4744 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4745 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004746 u64 other_ino = 0;
4747
Filipe Manana56f23fd2016-03-30 23:37:21 +01004748 ret = btrfs_check_ref_name_override(path->nodes[0],
4749 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004750 &min_key, inode,
4751 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004752 if (ret < 0) {
4753 err = ret;
4754 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004755 } else if (ret > 0 && ctx &&
4756 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004757 struct btrfs_key inode_key;
4758 struct inode *other_inode;
4759
4760 if (ins_nr > 0) {
4761 ins_nr++;
4762 } else {
4763 ins_nr = 1;
4764 ins_start_slot = path->slots[0];
4765 }
4766 ret = copy_items(trans, inode, dst_path, path,
4767 &last_extent, ins_start_slot,
4768 ins_nr, inode_only,
4769 logged_isize);
4770 if (ret < 0) {
4771 err = ret;
4772 goto out_unlock;
4773 }
4774 ins_nr = 0;
4775 btrfs_release_path(path);
4776 inode_key.objectid = other_ino;
4777 inode_key.type = BTRFS_INODE_ITEM_KEY;
4778 inode_key.offset = 0;
4779 other_inode = btrfs_iget(root->fs_info->sb,
4780 &inode_key, root,
4781 NULL);
4782 /*
4783 * If the other inode that had a conflicting dir
4784 * entry was deleted in the current transaction,
4785 * we don't need to do more work nor fallback to
4786 * a transaction commit.
4787 */
4788 if (IS_ERR(other_inode) &&
4789 PTR_ERR(other_inode) == -ENOENT) {
4790 goto next_key;
4791 } else if (IS_ERR(other_inode)) {
4792 err = PTR_ERR(other_inode);
4793 goto out_unlock;
4794 }
4795 /*
4796 * We are safe logging the other inode without
4797 * acquiring its i_mutex as long as we log with
4798 * the LOG_INODE_EXISTS mode. We're safe against
4799 * concurrent renames of the other inode as well
4800 * because during a rename we pin the log and
4801 * update the log with the new name before we
4802 * unpin it.
4803 */
4804 err = btrfs_log_inode(trans, root, other_inode,
4805 LOG_INODE_EXISTS,
4806 0, LLONG_MAX, ctx);
4807 iput(other_inode);
4808 if (err)
4809 goto out_unlock;
4810 else
4811 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004812 }
4813 }
4814
Filipe Manana36283bf2015-06-20 00:44:51 +01004815 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4816 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4817 if (ins_nr == 0)
4818 goto next_slot;
4819 ret = copy_items(trans, inode, dst_path, path,
4820 &last_extent, ins_start_slot,
4821 ins_nr, inode_only, logged_isize);
4822 if (ret < 0) {
4823 err = ret;
4824 goto out_unlock;
4825 }
4826 ins_nr = 0;
4827 if (ret) {
4828 btrfs_release_path(path);
4829 continue;
4830 }
4831 goto next_slot;
4832 }
4833
Chris Masone02119d2008-09-05 16:13:11 -04004834 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004835 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4836 ins_nr++;
4837 goto next_slot;
4838 } else if (!ins_nr) {
4839 ins_start_slot = path->slots[0];
4840 ins_nr = 1;
4841 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004842 }
4843
Josef Bacik16e75492013-10-22 12:18:51 -04004844 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004845 ins_start_slot, ins_nr, inode_only,
4846 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004847 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004848 err = ret;
4849 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004850 }
4851 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004852 ins_nr = 0;
4853 btrfs_release_path(path);
4854 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004855 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004856 ins_nr = 1;
4857 ins_start_slot = path->slots[0];
4858next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004859
Chris Mason3a5f1d42008-09-11 15:53:37 -04004860 nritems = btrfs_header_nritems(path->nodes[0]);
4861 path->slots[0]++;
4862 if (path->slots[0] < nritems) {
4863 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4864 path->slots[0]);
4865 goto again;
4866 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004867 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004868 ret = copy_items(trans, inode, dst_path, path,
4869 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004870 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004871 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004872 err = ret;
4873 goto out_unlock;
4874 }
Josef Bacik16e75492013-10-22 12:18:51 -04004875 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004876 ins_nr = 0;
4877 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004878 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004879next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004880 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004881 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004882 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004883 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004884 min_key.offset = 0;
4885 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004886 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004887 }
Chris Masone02119d2008-09-05 16:13:11 -04004888 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004889 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004890 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004891 ins_start_slot, ins_nr, inode_only,
4892 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004893 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004894 err = ret;
4895 goto out_unlock;
4896 }
Josef Bacik16e75492013-10-22 12:18:51 -04004897 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004898 ins_nr = 0;
4899 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004900
Filipe Manana36283bf2015-06-20 00:44:51 +01004901 btrfs_release_path(path);
4902 btrfs_release_path(dst_path);
4903 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4904 if (err)
4905 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004906 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4907 btrfs_release_path(path);
4908 btrfs_release_path(dst_path);
4909 err = btrfs_log_trailing_hole(trans, root, inode, path);
4910 if (err)
4911 goto out_unlock;
4912 }
Josef Bacika95249b2012-10-11 16:17:34 -04004913log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004914 btrfs_release_path(path);
4915 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004916 if (need_log_inode_item) {
4917 err = log_inode_item(trans, log, dst_path, inode);
4918 if (err)
4919 goto out_unlock;
4920 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004921 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004922 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004923 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004924 if (ret) {
4925 err = ret;
4926 goto out_unlock;
4927 }
Josef Bacikd006a042013-11-12 20:54:09 -05004928 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004929 struct extent_map *em, *n;
4930
Filipe Manana49dae1b2014-09-06 22:34:39 +01004931 write_lock(&em_tree->lock);
4932 /*
4933 * We can't just remove every em if we're called for a ranged
4934 * fsync - that is, one that doesn't cover the whole possible
4935 * file range (0 to LLONG_MAX). This is because we can have
4936 * em's that fall outside the range we're logging and therefore
4937 * their ordered operations haven't completed yet
4938 * (btrfs_finish_ordered_io() not invoked yet). This means we
4939 * didn't get their respective file extent item in the fs/subvol
4940 * tree yet, and need to let the next fast fsync (one which
4941 * consults the list of modified extent maps) find the em so
4942 * that it logs a matching file extent item and waits for the
4943 * respective ordered operation to complete (if it's still
4944 * running).
4945 *
4946 * Removing every em outside the range we're logging would make
4947 * the next fast fsync not log their matching file extent items,
4948 * therefore making us lose data after a log replay.
4949 */
4950 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4951 list) {
4952 const u64 mod_end = em->mod_start + em->mod_len - 1;
4953
4954 if (em->mod_start >= start && mod_end <= end)
4955 list_del_init(&em->list);
4956 }
4957 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004958 }
4959
Chris Mason9623f9a2008-09-11 17:42:42 -04004960 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004961 ret = log_directory_changes(trans, root, inode, path, dst_path,
4962 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004963 if (ret) {
4964 err = ret;
4965 goto out_unlock;
4966 }
Chris Masone02119d2008-09-05 16:13:11 -04004967 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004968
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004969 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01004970 BTRFS_I(inode)->logged_trans = trans->transid;
4971 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004972 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004973out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004974 if (unlikely(err))
4975 btrfs_put_logged_extents(&logged_list);
4976 else
4977 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004978 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4979
4980 btrfs_free_path(path);
4981 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004982 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004983}
4984
Chris Mason12fcfd22009-03-24 10:24:20 -04004985/*
Filipe Manana2be63d52016-02-12 11:34:23 +00004986 * Check if we must fallback to a transaction commit when logging an inode.
4987 * This must be called after logging the inode and is used only in the context
4988 * when fsyncing an inode requires the need to log some other inode - in which
4989 * case we can't lock the i_mutex of each other inode we need to log as that
4990 * can lead to deadlocks with concurrent fsync against other inodes (as we can
4991 * log inodes up or down in the hierarchy) or rename operations for example. So
4992 * we take the log_mutex of the inode after we have logged it and then check for
4993 * its last_unlink_trans value - this is safe because any task setting
4994 * last_unlink_trans must take the log_mutex and it must do this before it does
4995 * the actual unlink operation, so if we do this check before a concurrent task
4996 * sets last_unlink_trans it means we've logged a consistent version/state of
4997 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04004998 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00004999 * we logged the inode or it might have also done the unlink).
5000 */
5001static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5002 struct inode *inode)
5003{
5004 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5005 bool ret = false;
5006
5007 mutex_lock(&BTRFS_I(inode)->log_mutex);
5008 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5009 /*
5010 * Make sure any commits to the log are forced to be full
5011 * commits.
5012 */
5013 btrfs_set_log_full_commit(fs_info, trans);
5014 ret = true;
5015 }
5016 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5017
5018 return ret;
5019}
5020
5021/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005022 * follow the dentry parent pointers up the chain and see if any
5023 * of the directories in it require a full commit before they can
5024 * be logged. Returns zero if nothing special needs to be done or 1 if
5025 * a full commit is required.
5026 */
5027static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5028 struct inode *inode,
5029 struct dentry *parent,
5030 struct super_block *sb,
5031 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005032{
Chris Mason12fcfd22009-03-24 10:24:20 -04005033 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005034 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005035 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005036
Chris Masonaf4176b2009-03-24 10:24:31 -04005037 /*
5038 * for regular files, if its inode is already on disk, we don't
5039 * have to worry about the parents at all. This is because
5040 * we can use the last_unlink_trans field to record renames
5041 * and other fun in this file.
5042 */
5043 if (S_ISREG(inode->i_mode) &&
5044 BTRFS_I(inode)->generation <= last_committed &&
5045 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5046 goto out;
5047
Chris Mason12fcfd22009-03-24 10:24:20 -04005048 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005049 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005050 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005051 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005052 }
5053
5054 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005055 /*
5056 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005057 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005058 * logged_trans so that further down in the log code we don't
5059 * think this inode has already been logged.
5060 */
5061 if (inode != orig_inode)
5062 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005063 smp_mb();
5064
Filipe Manana2be63d52016-02-12 11:34:23 +00005065 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005066 ret = 1;
5067 break;
5068 }
5069
Al Virofc640052016-04-10 01:33:30 -04005070 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005071 break;
5072
Filipe Manana44f714d2016-06-06 16:11:13 +01005073 if (IS_ROOT(parent)) {
5074 inode = d_inode(parent);
5075 if (btrfs_must_commit_transaction(trans, inode))
5076 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005077 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005078 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005079
Josef Bacik6a912212010-11-20 09:48:00 +00005080 parent = dget_parent(parent);
5081 dput(old_parent);
5082 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005083 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005084
5085 }
Josef Bacik6a912212010-11-20 09:48:00 +00005086 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005087out:
Chris Masone02119d2008-09-05 16:13:11 -04005088 return ret;
5089}
5090
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005091struct btrfs_dir_list {
5092 u64 ino;
5093 struct list_head list;
5094};
5095
5096/*
5097 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5098 * details about the why it is needed.
5099 * This is a recursive operation - if an existing dentry corresponds to a
5100 * directory, that directory's new entries are logged too (same behaviour as
5101 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5102 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5103 * complains about the following circular lock dependency / possible deadlock:
5104 *
5105 * CPU0 CPU1
5106 * ---- ----
5107 * lock(&type->i_mutex_dir_key#3/2);
5108 * lock(sb_internal#2);
5109 * lock(&type->i_mutex_dir_key#3/2);
5110 * lock(&sb->s_type->i_mutex_key#14);
5111 *
5112 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5113 * sb_start_intwrite() in btrfs_start_transaction().
5114 * Not locking i_mutex of the inodes is still safe because:
5115 *
5116 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5117 * that while logging the inode new references (names) are added or removed
5118 * from the inode, leaving the logged inode item with a link count that does
5119 * not match the number of logged inode reference items. This is fine because
5120 * at log replay time we compute the real number of links and correct the
5121 * link count in the inode item (see replay_one_buffer() and
5122 * link_to_fixup_dir());
5123 *
5124 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5125 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5126 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5127 * has a size that doesn't match the sum of the lengths of all the logged
5128 * names. This does not result in a problem because if a dir_item key is
5129 * logged but its matching dir_index key is not logged, at log replay time we
5130 * don't use it to replay the respective name (see replay_one_name()). On the
5131 * other hand if only the dir_index key ends up being logged, the respective
5132 * name is added to the fs/subvol tree with both the dir_item and dir_index
5133 * keys created (see replay_one_name()).
5134 * The directory's inode item with a wrong i_size is not a problem as well,
5135 * since we don't use it at log replay time to set the i_size in the inode
5136 * item of the fs/subvol tree (see overwrite_item()).
5137 */
5138static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5139 struct btrfs_root *root,
5140 struct inode *start_inode,
5141 struct btrfs_log_ctx *ctx)
5142{
5143 struct btrfs_root *log = root->log_root;
5144 struct btrfs_path *path;
5145 LIST_HEAD(dir_list);
5146 struct btrfs_dir_list *dir_elem;
5147 int ret = 0;
5148
5149 path = btrfs_alloc_path();
5150 if (!path)
5151 return -ENOMEM;
5152
5153 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5154 if (!dir_elem) {
5155 btrfs_free_path(path);
5156 return -ENOMEM;
5157 }
5158 dir_elem->ino = btrfs_ino(start_inode);
5159 list_add_tail(&dir_elem->list, &dir_list);
5160
5161 while (!list_empty(&dir_list)) {
5162 struct extent_buffer *leaf;
5163 struct btrfs_key min_key;
5164 int nritems;
5165 int i;
5166
5167 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5168 list);
5169 if (ret)
5170 goto next_dir_inode;
5171
5172 min_key.objectid = dir_elem->ino;
5173 min_key.type = BTRFS_DIR_ITEM_KEY;
5174 min_key.offset = 0;
5175again:
5176 btrfs_release_path(path);
5177 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5178 if (ret < 0) {
5179 goto next_dir_inode;
5180 } else if (ret > 0) {
5181 ret = 0;
5182 goto next_dir_inode;
5183 }
5184
5185process_leaf:
5186 leaf = path->nodes[0];
5187 nritems = btrfs_header_nritems(leaf);
5188 for (i = path->slots[0]; i < nritems; i++) {
5189 struct btrfs_dir_item *di;
5190 struct btrfs_key di_key;
5191 struct inode *di_inode;
5192 struct btrfs_dir_list *new_dir_elem;
5193 int log_mode = LOG_INODE_EXISTS;
5194 int type;
5195
5196 btrfs_item_key_to_cpu(leaf, &min_key, i);
5197 if (min_key.objectid != dir_elem->ino ||
5198 min_key.type != BTRFS_DIR_ITEM_KEY)
5199 goto next_dir_inode;
5200
5201 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5202 type = btrfs_dir_type(leaf, di);
5203 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5204 type != BTRFS_FT_DIR)
5205 continue;
5206 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5207 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5208 continue;
5209
5210 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5211 root, NULL);
5212 if (IS_ERR(di_inode)) {
5213 ret = PTR_ERR(di_inode);
5214 goto next_dir_inode;
5215 }
5216
5217 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5218 iput(di_inode);
5219 continue;
5220 }
5221
5222 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005223 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005224 log_mode = LOG_INODE_ALL;
5225 btrfs_release_path(path);
5226 ret = btrfs_log_inode(trans, root, di_inode,
5227 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005228 if (!ret &&
5229 btrfs_must_commit_transaction(trans, di_inode))
5230 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005231 iput(di_inode);
5232 if (ret)
5233 goto next_dir_inode;
5234 if (ctx->log_new_dentries) {
5235 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5236 GFP_NOFS);
5237 if (!new_dir_elem) {
5238 ret = -ENOMEM;
5239 goto next_dir_inode;
5240 }
5241 new_dir_elem->ino = di_key.objectid;
5242 list_add_tail(&new_dir_elem->list, &dir_list);
5243 }
5244 break;
5245 }
5246 if (i == nritems) {
5247 ret = btrfs_next_leaf(log, path);
5248 if (ret < 0) {
5249 goto next_dir_inode;
5250 } else if (ret > 0) {
5251 ret = 0;
5252 goto next_dir_inode;
5253 }
5254 goto process_leaf;
5255 }
5256 if (min_key.offset < (u64)-1) {
5257 min_key.offset++;
5258 goto again;
5259 }
5260next_dir_inode:
5261 list_del(&dir_elem->list);
5262 kfree(dir_elem);
5263 }
5264
5265 btrfs_free_path(path);
5266 return ret;
5267}
5268
Filipe Manana18aa0922015-08-05 16:49:08 +01005269static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5270 struct inode *inode,
5271 struct btrfs_log_ctx *ctx)
5272{
5273 int ret;
5274 struct btrfs_path *path;
5275 struct btrfs_key key;
5276 struct btrfs_root *root = BTRFS_I(inode)->root;
5277 const u64 ino = btrfs_ino(inode);
5278
5279 path = btrfs_alloc_path();
5280 if (!path)
5281 return -ENOMEM;
5282 path->skip_locking = 1;
5283 path->search_commit_root = 1;
5284
5285 key.objectid = ino;
5286 key.type = BTRFS_INODE_REF_KEY;
5287 key.offset = 0;
5288 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5289 if (ret < 0)
5290 goto out;
5291
5292 while (true) {
5293 struct extent_buffer *leaf = path->nodes[0];
5294 int slot = path->slots[0];
5295 u32 cur_offset = 0;
5296 u32 item_size;
5297 unsigned long ptr;
5298
5299 if (slot >= btrfs_header_nritems(leaf)) {
5300 ret = btrfs_next_leaf(root, path);
5301 if (ret < 0)
5302 goto out;
5303 else if (ret > 0)
5304 break;
5305 continue;
5306 }
5307
5308 btrfs_item_key_to_cpu(leaf, &key, slot);
5309 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5310 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5311 break;
5312
5313 item_size = btrfs_item_size_nr(leaf, slot);
5314 ptr = btrfs_item_ptr_offset(leaf, slot);
5315 while (cur_offset < item_size) {
5316 struct btrfs_key inode_key;
5317 struct inode *dir_inode;
5318
5319 inode_key.type = BTRFS_INODE_ITEM_KEY;
5320 inode_key.offset = 0;
5321
5322 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5323 struct btrfs_inode_extref *extref;
5324
5325 extref = (struct btrfs_inode_extref *)
5326 (ptr + cur_offset);
5327 inode_key.objectid = btrfs_inode_extref_parent(
5328 leaf, extref);
5329 cur_offset += sizeof(*extref);
5330 cur_offset += btrfs_inode_extref_name_len(leaf,
5331 extref);
5332 } else {
5333 inode_key.objectid = key.offset;
5334 cur_offset = item_size;
5335 }
5336
5337 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5338 root, NULL);
5339 /* If parent inode was deleted, skip it. */
5340 if (IS_ERR(dir_inode))
5341 continue;
5342
Filipe Manana657ed1a2016-04-06 17:11:56 +01005343 if (ctx)
5344 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005345 ret = btrfs_log_inode(trans, root, dir_inode,
5346 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005347 if (!ret &&
5348 btrfs_must_commit_transaction(trans, dir_inode))
5349 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005350 if (!ret && ctx && ctx->log_new_dentries)
5351 ret = log_new_dir_dentries(trans, root,
5352 dir_inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005353 iput(dir_inode);
5354 if (ret)
5355 goto out;
5356 }
5357 path->slots[0]++;
5358 }
5359 ret = 0;
5360out:
5361 btrfs_free_path(path);
5362 return ret;
5363}
5364
Chris Masone02119d2008-09-05 16:13:11 -04005365/*
5366 * helper function around btrfs_log_inode to make sure newly created
5367 * parent directories also end up in the log. A minimal inode and backref
5368 * only logging is done of any parent directories that are older than
5369 * the last committed transaction
5370 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005371static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5372 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005373 struct dentry *parent,
5374 const loff_t start,
5375 const loff_t end,
5376 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005377 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005378{
Chris Mason12fcfd22009-03-24 10:24:20 -04005379 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005380 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005381 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005382 int ret = 0;
5383 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005384 bool log_dentries = false;
5385 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005386
5387 sb = inode->i_sb;
5388
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005389 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005390 ret = 1;
5391 goto end_no_trans;
5392 }
5393
Miao Xie995946d2014-04-02 19:51:06 +08005394 /*
5395 * The prev transaction commit doesn't complete, we need do
5396 * full commit by ourselves.
5397 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005398 if (root->fs_info->last_trans_log_full_commit >
5399 root->fs_info->last_trans_committed) {
5400 ret = 1;
5401 goto end_no_trans;
5402 }
5403
Yan, Zheng76dda932009-09-21 16:00:26 -04005404 if (root != BTRFS_I(inode)->root ||
5405 btrfs_root_refs(&root->root_item) == 0) {
5406 ret = 1;
5407 goto end_no_trans;
5408 }
5409
Chris Mason12fcfd22009-03-24 10:24:20 -04005410 ret = check_parent_dirs_for_sync(trans, inode, parent,
5411 sb, last_committed);
5412 if (ret)
5413 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005414
Josef Bacik22ee6985d2012-05-29 16:57:49 -04005415 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005416 ret = BTRFS_NO_LOG_SYNC;
5417 goto end_no_trans;
5418 }
5419
Miao Xie8b050d32014-02-20 18:08:58 +08005420 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005421 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005422 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005423
Filipe Manana8407f552014-09-05 15:14:39 +01005424 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005425 if (ret)
5426 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005427
Chris Masonaf4176b2009-03-24 10:24:31 -04005428 /*
5429 * for regular files, if its inode is already on disk, we don't
5430 * have to worry about the parents at all. This is because
5431 * we can use the last_unlink_trans field to record renames
5432 * and other fun in this file.
5433 */
5434 if (S_ISREG(inode->i_mode) &&
5435 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005436 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5437 ret = 0;
5438 goto end_trans;
5439 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005440
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005441 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5442 log_dentries = true;
5443
Filipe Manana18aa0922015-08-05 16:49:08 +01005444 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005445 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005446 * inodes are fully logged. This is to prevent leaving dangling
5447 * directory index entries in directories that were our parents but are
5448 * not anymore. Not doing this results in old parent directory being
5449 * impossible to delete after log replay (rmdir will always fail with
5450 * error -ENOTEMPTY).
5451 *
5452 * Example 1:
5453 *
5454 * mkdir testdir
5455 * touch testdir/foo
5456 * ln testdir/foo testdir/bar
5457 * sync
5458 * unlink testdir/bar
5459 * xfs_io -c fsync testdir/foo
5460 * <power failure>
5461 * mount fs, triggers log replay
5462 *
5463 * If we don't log the parent directory (testdir), after log replay the
5464 * directory still has an entry pointing to the file inode using the bar
5465 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5466 * the file inode has a link count of 1.
5467 *
5468 * Example 2:
5469 *
5470 * mkdir testdir
5471 * touch foo
5472 * ln foo testdir/foo2
5473 * ln foo testdir/foo3
5474 * sync
5475 * unlink testdir/foo3
5476 * xfs_io -c fsync foo
5477 * <power failure>
5478 * mount fs, triggers log replay
5479 *
5480 * Similar as the first example, after log replay the parent directory
5481 * testdir still has an entry pointing to the inode file with name foo3
5482 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5483 * and has a link count of 2.
5484 */
5485 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5486 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5487 if (ret)
5488 goto end_trans;
5489 }
5490
Chris Masond3977122009-01-05 21:25:51 -05005491 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005492 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005493 break;
5494
David Howells2b0143b2015-03-17 22:25:59 +00005495 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005496 if (root != BTRFS_I(inode)->root)
5497 break;
5498
Filipe Manana18aa0922015-08-05 16:49:08 +01005499 if (BTRFS_I(inode)->generation > last_committed) {
5500 ret = btrfs_log_inode(trans, root, inode,
5501 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005502 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005503 if (ret)
5504 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005505 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005506 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005507 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005508
Josef Bacik6a912212010-11-20 09:48:00 +00005509 parent = dget_parent(parent);
5510 dput(old_parent);
5511 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005512 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005513 if (log_dentries)
5514 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5515 else
5516 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005517end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005518 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005519 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005520 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005521 ret = 1;
5522 }
Miao Xie8b050d32014-02-20 18:08:58 +08005523
5524 if (ret)
5525 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005526 btrfs_end_log_trans(root);
5527end_no_trans:
5528 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005529}
5530
5531/*
5532 * it is not safe to log dentry if the chunk root has added new
5533 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5534 * If this returns 1, you must commit the transaction to safely get your
5535 * data on disk.
5536 */
5537int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005538 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005539 const loff_t start,
5540 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005541 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005542{
Josef Bacik6a912212010-11-20 09:48:00 +00005543 struct dentry *parent = dget_parent(dentry);
5544 int ret;
5545
David Howells2b0143b2015-03-17 22:25:59 +00005546 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005547 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005548 dput(parent);
5549
5550 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005551}
5552
5553/*
5554 * should be called during mount to recover any replay any log trees
5555 * from the FS
5556 */
5557int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5558{
5559 int ret;
5560 struct btrfs_path *path;
5561 struct btrfs_trans_handle *trans;
5562 struct btrfs_key key;
5563 struct btrfs_key found_key;
5564 struct btrfs_key tmp_key;
5565 struct btrfs_root *log;
5566 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5567 struct walk_control wc = {
5568 .process_func = process_one_buffer,
5569 .stage = 0,
5570 };
5571
Chris Masone02119d2008-09-05 16:13:11 -04005572 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005573 if (!path)
5574 return -ENOMEM;
5575
Josef Bacikafcdd122016-09-02 15:40:02 -04005576 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005577
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005578 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005579 if (IS_ERR(trans)) {
5580 ret = PTR_ERR(trans);
5581 goto error;
5582 }
Chris Masone02119d2008-09-05 16:13:11 -04005583
5584 wc.trans = trans;
5585 wc.pin = 1;
5586
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005587 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005588 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005589 btrfs_handle_fs_error(fs_info, ret,
5590 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005591 goto error;
5592 }
Chris Masone02119d2008-09-05 16:13:11 -04005593
5594again:
5595 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5596 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005597 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005598
Chris Masond3977122009-01-05 21:25:51 -05005599 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005600 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005601
5602 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005603 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005604 "Couldn't find tree log root.");
5605 goto error;
5606 }
Chris Masone02119d2008-09-05 16:13:11 -04005607 if (ret > 0) {
5608 if (path->slots[0] == 0)
5609 break;
5610 path->slots[0]--;
5611 }
5612 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5613 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005614 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005615 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5616 break;
5617
Miao Xiecb517ea2013-05-15 07:48:19 +00005618 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005619 if (IS_ERR(log)) {
5620 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005621 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005622 "Couldn't read tree log root.");
5623 goto error;
5624 }
Chris Masone02119d2008-09-05 16:13:11 -04005625
5626 tmp_key.objectid = found_key.offset;
5627 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5628 tmp_key.offset = (u64)-1;
5629
5630 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005631 if (IS_ERR(wc.replay_dest)) {
5632 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005633 free_extent_buffer(log->node);
5634 free_extent_buffer(log->commit_root);
5635 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005636 btrfs_handle_fs_error(fs_info, ret,
5637 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005638 goto error;
5639 }
Chris Masone02119d2008-09-05 16:13:11 -04005640
Yan Zheng07d400a2009-01-06 11:42:00 -05005641 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005643 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005644
Josef Bacikb50c6e22013-04-25 15:55:30 -04005645 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005646 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5647 path);
Chris Masone02119d2008-09-05 16:13:11 -04005648 }
Chris Masone02119d2008-09-05 16:13:11 -04005649
5650 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005651 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005652 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005653 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005654 kfree(log);
5655
Josef Bacikb50c6e22013-04-25 15:55:30 -04005656 if (ret)
5657 goto error;
5658
Chris Masone02119d2008-09-05 16:13:11 -04005659 if (found_key.offset == 0)
5660 break;
5661 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005662 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005663
5664 /* step one is to pin it all, step two is to replay just inodes */
5665 if (wc.pin) {
5666 wc.pin = 0;
5667 wc.process_func = replay_one_buffer;
5668 wc.stage = LOG_WALK_REPLAY_INODES;
5669 goto again;
5670 }
5671 /* step three is to replay everything */
5672 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5673 wc.stage++;
5674 goto again;
5675 }
5676
5677 btrfs_free_path(path);
5678
Josef Bacikabefa552013-04-24 16:40:05 -04005679 /* step 4: commit the transaction, which also unpins the blocks */
5680 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5681 if (ret)
5682 return ret;
5683
Chris Masone02119d2008-09-05 16:13:11 -04005684 free_extent_buffer(log_root_tree->node);
5685 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005686 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005687 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005688
Josef Bacikabefa552013-04-24 16:40:05 -04005689 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005690error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005691 if (wc.trans)
5692 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005693 btrfs_free_path(path);
5694 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005695}
Chris Mason12fcfd22009-03-24 10:24:20 -04005696
5697/*
5698 * there are some corner cases where we want to force a full
5699 * commit instead of allowing a directory to be logged.
5700 *
5701 * They revolve around files there were unlinked from the directory, and
5702 * this function updates the parent directory so that a full commit is
5703 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005704 *
5705 * Must be called before the unlink operations (updates to the subvolume tree,
5706 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005707 */
5708void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5709 struct inode *dir, struct inode *inode,
5710 int for_rename)
5711{
5712 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005713 * when we're logging a file, if it hasn't been renamed
5714 * or unlinked, and its inode is fully committed on disk,
5715 * we don't have to worry about walking up the directory chain
5716 * to log its parents.
5717 *
5718 * So, we use the last_unlink_trans field to put this transid
5719 * into the file. When the file is logged we check it and
5720 * don't log the parents if the file is fully on disk.
5721 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005722 mutex_lock(&BTRFS_I(inode)->log_mutex);
5723 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5724 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005725
5726 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005727 * if this directory was already logged any new
5728 * names for this file/dir will get recorded
5729 */
5730 smp_mb();
5731 if (BTRFS_I(dir)->logged_trans == trans->transid)
5732 return;
5733
5734 /*
5735 * if the inode we're about to unlink was logged,
5736 * the log will be properly updated for any new names
5737 */
5738 if (BTRFS_I(inode)->logged_trans == trans->transid)
5739 return;
5740
5741 /*
5742 * when renaming files across directories, if the directory
5743 * there we're unlinking from gets fsync'd later on, there's
5744 * no way to find the destination directory later and fsync it
5745 * properly. So, we have to be conservative and force commits
5746 * so the new name gets discovered.
5747 */
5748 if (for_rename)
5749 goto record;
5750
5751 /* we can safely do the unlink without any special recording */
5752 return;
5753
5754record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005755 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005756 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005757 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005758}
5759
5760/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005761 * Make sure that if someone attempts to fsync the parent directory of a deleted
5762 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5763 * that after replaying the log tree of the parent directory's root we will not
5764 * see the snapshot anymore and at log replay time we will not see any log tree
5765 * corresponding to the deleted snapshot's root, which could lead to replaying
5766 * it after replaying the log tree of the parent directory (which would replay
5767 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005768 *
5769 * Must be called before the actual snapshot destroy operation (updates to the
5770 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005771 */
5772void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5773 struct inode *dir)
5774{
Filipe Manana2be63d52016-02-12 11:34:23 +00005775 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005776 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005777 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005778}
5779
5780/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005781 * Call this after adding a new name for a file and it will properly
5782 * update the log to reflect the new name.
5783 *
5784 * It will return zero if all goes well, and it will return 1 if a
5785 * full transaction commit is required.
5786 */
5787int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5788 struct inode *inode, struct inode *old_dir,
5789 struct dentry *parent)
5790{
5791 struct btrfs_root * root = BTRFS_I(inode)->root;
5792
5793 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005794 * this will force the logging code to walk the dentry chain
5795 * up for the file
5796 */
5797 if (S_ISREG(inode->i_mode))
5798 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5799
5800 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005801 * if this inode hasn't been logged and directory we're renaming it
5802 * from hasn't been logged, we don't need to log it
5803 */
5804 if (BTRFS_I(inode)->logged_trans <=
5805 root->fs_info->last_trans_committed &&
5806 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5807 root->fs_info->last_trans_committed))
5808 return 0;
5809
Filipe Manana49dae1b2014-09-06 22:34:39 +01005810 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5811 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005812}
5813