blob: 4f59140c8c006ca0b91be6bee478a4d6ebd681bb [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"
Chris Masone02119d2008-09-05 16:13:11 -040029
30/* magic values for the inode_only field in btrfs_log_inode:
31 *
32 * LOG_INODE_ALL means to log everything
33 * LOG_INODE_EXISTS means to log just enough to recreate the inode
34 * during log replay
35 */
36#define LOG_INODE_ALL 0
37#define LOG_INODE_EXISTS 1
38
39/*
Chris Mason12fcfd22009-03-24 10:24:20 -040040 * directory trouble cases
41 *
42 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
43 * log, we must force a full commit before doing an fsync of the directory
44 * where the unlink was done.
45 * ---> record transid of last unlink/rename per directory
46 *
47 * mkdir foo/some_dir
48 * normal commit
49 * rename foo/some_dir foo2/some_dir
50 * mkdir foo/some_dir
51 * fsync foo/some_dir/some_file
52 *
53 * The fsync above will unlink the original some_dir without recording
54 * it in its new location (foo2). After a crash, some_dir will be gone
55 * unless the fsync of some_file forces a full commit
56 *
57 * 2) we must log any new names for any file or dir that is in the fsync
58 * log. ---> check inode while renaming/linking.
59 *
60 * 2a) we must log any new names for any file or dir during rename
61 * when the directory they are being removed from was logged.
62 * ---> check inode and old parent dir during rename
63 *
64 * 2a is actually the more important variant. With the extra logging
65 * a crash might unlink the old name without recreating the new one
66 *
67 * 3) after a crash, we must go through any directories with a link count
68 * of zero and redo the rm -rf
69 *
70 * mkdir f1/foo
71 * normal commit
72 * rm -rf f1/foo
73 * fsync(f1)
74 *
75 * The directory f1 was fully removed from the FS, but fsync was never
76 * called on f1, only its parent dir. After a crash the rm -rf must
77 * be replayed. This must be able to recurse down the entire
78 * directory tree. The inode link count fixup code takes care of the
79 * ugly details.
80 */
81
82/*
Chris Masone02119d2008-09-05 16:13:11 -040083 * stages for the tree walking. The first
84 * stage (0) is to only pin down the blocks we find
85 * the second stage (1) is to make sure that all the inodes
86 * we find in the log are created in the subvolume.
87 *
88 * The last stage is to deal with directories and links and extents
89 * and all the other fun semantics
90 */
91#define LOG_WALK_PIN_ONLY 0
92#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040093#define LOG_WALK_REPLAY_DIR_INDEX 2
94#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040095
Chris Mason12fcfd22009-03-24 10:24:20 -040096static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +010097 struct btrfs_root *root, struct inode *inode,
98 int inode_only,
99 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100100 const loff_t end,
101 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500102static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400105static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
107 struct btrfs_root *log,
108 struct btrfs_path *path,
109 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400110
111/*
112 * tree logging is a special write ahead log used to make sure that
113 * fsyncs and O_SYNCs can happen without doing full tree commits.
114 *
115 * Full tree commits are expensive because they require commonly
116 * modified blocks to be recowed, creating many dirty pages in the
117 * extent tree an 4x-6x higher write load than ext3.
118 *
119 * Instead of doing a tree commit on every fsync, we use the
120 * key ranges and transaction ids to find items for a given file or directory
121 * that have changed in this transaction. Those items are copied into
122 * a special tree (one per subvolume root), that tree is written to disk
123 * and then the fsync is considered complete.
124 *
125 * After a crash, items are copied out of the log-tree back into the
126 * subvolume tree. Any file data extents found are recorded in the extent
127 * allocation tree, and the log-tree freed.
128 *
129 * The log tree is read three times, once to pin down all the extents it is
130 * using in ram and once, once to create all the inodes logged in the tree
131 * and once to do all the other items.
132 */
133
134/*
Chris Masone02119d2008-09-05 16:13:11 -0400135 * start a sub transaction and setup the log tree
136 * this increments the log tree writer count to make the people
137 * syncing the tree wait for us to finish
138 */
139static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800140 struct btrfs_root *root,
141 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400142{
Miao Xie8b050d32014-02-20 18:08:58 +0800143 int index;
Chris Masone02119d2008-09-05 16:13:11 -0400144 int ret;
Yan Zheng7237f182009-01-21 12:54:03 -0500145
146 mutex_lock(&root->log_mutex);
147 if (root->log_root) {
Miao Xie995946d2014-04-02 19:51:06 +0800148 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800149 ret = -EAGAIN;
150 goto out;
151 }
Josef Bacikff782e02009-10-08 15:30:04 -0400152 if (!root->log_start_pid) {
153 root->log_start_pid = current->pid;
Miao Xie27cdeb72014-04-02 19:51:05 +0800154 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400155 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800156 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400157 }
158
Miao Xie2ecb7922012-09-06 04:04:27 -0600159 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500160 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800161 if (ctx) {
162 index = root->log_transid % 2;
163 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800164 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800165 }
Yan Zheng7237f182009-01-21 12:54:03 -0500166 mutex_unlock(&root->log_mutex);
167 return 0;
168 }
Miao Xiee87ac132014-02-20 18:08:53 +0800169
170 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400171 mutex_lock(&root->fs_info->tree_log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800172 if (!root->fs_info->log_root_tree)
Chris Masone02119d2008-09-05 16:13:11 -0400173 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Miao Xiee87ac132014-02-20 18:08:53 +0800174 mutex_unlock(&root->fs_info->tree_log_mutex);
175 if (ret)
176 goto out;
177
178 if (!root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400179 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400180 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800181 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400182 }
Miao Xie27cdeb72014-04-02 19:51:05 +0800183 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Miao Xiee87ac132014-02-20 18:08:53 +0800184 root->log_start_pid = current->pid;
Miao Xie2ecb7922012-09-06 04:04:27 -0600185 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500186 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800187 if (ctx) {
188 index = root->log_transid % 2;
189 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800190 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800191 }
Miao Xiee87ac132014-02-20 18:08:53 +0800192out:
Yan Zheng7237f182009-01-21 12:54:03 -0500193 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800194 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400195}
196
197/*
198 * returns 0 if there was a log transaction running and we were able
199 * to join, or returns -ENOENT if there were not transactions
200 * in progress
201 */
202static int join_running_log_trans(struct btrfs_root *root)
203{
204 int ret = -ENOENT;
205
206 smp_mb();
207 if (!root->log_root)
208 return -ENOENT;
209
Yan Zheng7237f182009-01-21 12:54:03 -0500210 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400211 if (root->log_root) {
212 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500213 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400214 }
Yan Zheng7237f182009-01-21 12:54:03 -0500215 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400216 return ret;
217}
218
219/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400220 * This either makes the current running log transaction wait
221 * until you call btrfs_end_log_trans() or it makes any future
222 * log transactions wait until you call btrfs_end_log_trans()
223 */
224int btrfs_pin_log_trans(struct btrfs_root *root)
225{
226 int ret = -ENOENT;
227
228 mutex_lock(&root->log_mutex);
229 atomic_inc(&root->log_writers);
230 mutex_unlock(&root->log_mutex);
231 return ret;
232}
233
234/*
Chris Masone02119d2008-09-05 16:13:11 -0400235 * indicate we're done making changes to the log tree
236 * and wake up anyone waiting to do a sync
237 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100238void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400239{
Yan Zheng7237f182009-01-21 12:54:03 -0500240 if (atomic_dec_and_test(&root->log_writers)) {
241 smp_mb();
242 if (waitqueue_active(&root->log_writer_wait))
243 wake_up(&root->log_writer_wait);
244 }
Chris Masone02119d2008-09-05 16:13:11 -0400245}
246
247
248/*
249 * the walk control struct is used to pass state down the chain when
250 * processing the log tree. The stage field tells us which part
251 * of the log tree processing we are currently doing. The others
252 * are state fields used for that specific part
253 */
254struct walk_control {
255 /* should we free the extent on disk when done? This is used
256 * at transaction commit time while freeing a log tree
257 */
258 int free;
259
260 /* should we write out the extent buffer? This is used
261 * while flushing the log tree to disk during a sync
262 */
263 int write;
264
265 /* should we wait for the extent buffer io to finish? Also used
266 * while flushing the log tree to disk for a sync
267 */
268 int wait;
269
270 /* pin only walk, we record which extents on disk belong to the
271 * log trees
272 */
273 int pin;
274
275 /* what stage of the replay code we're currently in */
276 int stage;
277
278 /* the root we are currently replaying */
279 struct btrfs_root *replay_dest;
280
281 /* the trans handle for the current replay */
282 struct btrfs_trans_handle *trans;
283
284 /* the function that gets used to process blocks we find in the
285 * tree. Note the extent_buffer might not be up to date when it is
286 * passed in, and it must be checked or read if you need the data
287 * inside it
288 */
289 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
290 struct walk_control *wc, u64 gen);
291};
292
293/*
294 * process_func used to pin down extents, write them or wait on them
295 */
296static int process_one_buffer(struct btrfs_root *log,
297 struct extent_buffer *eb,
298 struct walk_control *wc, u64 gen)
299{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400300 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400301
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400302 /*
303 * If this fs is mixed then we need to be able to process the leaves to
304 * pin down any logged extents, so we have to read the block.
305 */
306 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
307 ret = btrfs_read_buffer(eb, gen);
308 if (ret)
309 return ret;
310 }
311
Josef Bacikb50c6e22013-04-25 15:55:30 -0400312 if (wc->pin)
313 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
314 eb->start, eb->len);
315
316 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400317 if (wc->pin && btrfs_header_level(eb) == 0)
318 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400319 if (wc->write)
320 btrfs_write_tree_block(eb);
321 if (wc->wait)
322 btrfs_wait_tree_block_writeback(eb);
323 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400324 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400325}
326
327/*
328 * Item overwrite used by replay and tree logging. eb, slot and key all refer
329 * to the src data we are copying out.
330 *
331 * root is the tree we are copying into, and path is a scratch
332 * path for use in this function (it should be released on entry and
333 * will be released on exit).
334 *
335 * If the key is already in the destination tree the existing item is
336 * overwritten. If the existing item isn't big enough, it is extended.
337 * If it is too large, it is truncated.
338 *
339 * If the key isn't in the destination yet, a new item is inserted.
340 */
341static noinline int overwrite_item(struct btrfs_trans_handle *trans,
342 struct btrfs_root *root,
343 struct btrfs_path *path,
344 struct extent_buffer *eb, int slot,
345 struct btrfs_key *key)
346{
347 int ret;
348 u32 item_size;
349 u64 saved_i_size = 0;
350 int save_old_i_size = 0;
351 unsigned long src_ptr;
352 unsigned long dst_ptr;
353 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000354 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400355
356 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
357 overwrite_root = 1;
358
359 item_size = btrfs_item_size_nr(eb, slot);
360 src_ptr = btrfs_item_ptr_offset(eb, slot);
361
362 /* look for the key in the destination tree */
363 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000364 if (ret < 0)
365 return ret;
366
Chris Masone02119d2008-09-05 16:13:11 -0400367 if (ret == 0) {
368 char *src_copy;
369 char *dst_copy;
370 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
371 path->slots[0]);
372 if (dst_size != item_size)
373 goto insert;
374
375 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200376 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400377 return 0;
378 }
379 dst_copy = kmalloc(item_size, GFP_NOFS);
380 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000381 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200382 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000383 kfree(dst_copy);
384 kfree(src_copy);
385 return -ENOMEM;
386 }
Chris Masone02119d2008-09-05 16:13:11 -0400387
388 read_extent_buffer(eb, src_copy, src_ptr, item_size);
389
390 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
391 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
392 item_size);
393 ret = memcmp(dst_copy, src_copy, item_size);
394
395 kfree(dst_copy);
396 kfree(src_copy);
397 /*
398 * they have the same contents, just return, this saves
399 * us from cowing blocks in the destination tree and doing
400 * extra writes that may not have been done by a previous
401 * sync
402 */
403 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200404 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400405 return 0;
406 }
407
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000408 /*
409 * We need to load the old nbytes into the inode so when we
410 * replay the extents we've logged we get the right nbytes.
411 */
412 if (inode_item) {
413 struct btrfs_inode_item *item;
414 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400415 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000416
417 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
418 struct btrfs_inode_item);
419 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
420 item = btrfs_item_ptr(eb, slot,
421 struct btrfs_inode_item);
422 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400423
424 /*
425 * If this is a directory we need to reset the i_size to
426 * 0 so that we can set it up properly when replaying
427 * the rest of the items in this log.
428 */
429 mode = btrfs_inode_mode(eb, item);
430 if (S_ISDIR(mode))
431 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000432 }
433 } else if (inode_item) {
434 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400435 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000436
437 /*
438 * New inode, set nbytes to 0 so that the nbytes comes out
439 * properly when we replay the extents.
440 */
441 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
442 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400443
444 /*
445 * If this is a directory we need to reset the i_size to 0 so
446 * that we can set it up properly when replaying the rest of
447 * the items in this log.
448 */
449 mode = btrfs_inode_mode(eb, item);
450 if (S_ISDIR(mode))
451 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400452 }
453insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200454 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400455 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000456 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400457 ret = btrfs_insert_empty_item(trans, root, path,
458 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000459 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400460
461 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000462 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400463 u32 found_size;
464 found_size = btrfs_item_size_nr(path->nodes[0],
465 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100466 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000467 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100468 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000469 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100470 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400471 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400472 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400473 }
474 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
475 path->slots[0]);
476
477 /* don't overwrite an existing inode if the generation number
478 * was logged as zero. This is done when the tree logging code
479 * is just logging an inode to make sure it exists after recovery.
480 *
481 * Also, don't overwrite i_size on directories during replay.
482 * log replay inserts and removes directory items based on the
483 * state of the tree found in the subvolume, and i_size is modified
484 * as it goes
485 */
486 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
487 struct btrfs_inode_item *src_item;
488 struct btrfs_inode_item *dst_item;
489
490 src_item = (struct btrfs_inode_item *)src_ptr;
491 dst_item = (struct btrfs_inode_item *)dst_ptr;
492
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000493 if (btrfs_inode_generation(eb, src_item) == 0) {
494 struct extent_buffer *dst_eb = path->nodes[0];
495
496 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
497 S_ISREG(btrfs_inode_mode(dst_eb, dst_item))) {
498 struct btrfs_map_token token;
499 u64 ino_size = btrfs_inode_size(eb, src_item);
500
501 btrfs_init_map_token(&token);
502 btrfs_set_token_inode_size(dst_eb, dst_item,
503 ino_size, &token);
504 }
Chris Masone02119d2008-09-05 16:13:11 -0400505 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000506 }
Chris Masone02119d2008-09-05 16:13:11 -0400507
508 if (overwrite_root &&
509 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
510 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
511 save_old_i_size = 1;
512 saved_i_size = btrfs_inode_size(path->nodes[0],
513 dst_item);
514 }
515 }
516
517 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
518 src_ptr, item_size);
519
520 if (save_old_i_size) {
521 struct btrfs_inode_item *dst_item;
522 dst_item = (struct btrfs_inode_item *)dst_ptr;
523 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
524 }
525
526 /* make sure the generation is filled in */
527 if (key->type == BTRFS_INODE_ITEM_KEY) {
528 struct btrfs_inode_item *dst_item;
529 dst_item = (struct btrfs_inode_item *)dst_ptr;
530 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
531 btrfs_set_inode_generation(path->nodes[0], dst_item,
532 trans->transid);
533 }
534 }
535no_copy:
536 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200537 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400538 return 0;
539}
540
541/*
542 * simple helper to read an inode off the disk from a given root
543 * This can only be called for subvolume roots and not for the log
544 */
545static noinline struct inode *read_one_inode(struct btrfs_root *root,
546 u64 objectid)
547{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400549 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400550
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400551 key.objectid = objectid;
552 key.type = BTRFS_INODE_ITEM_KEY;
553 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000554 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400555 if (IS_ERR(inode)) {
556 inode = NULL;
557 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400558 iput(inode);
559 inode = NULL;
560 }
561 return inode;
562}
563
564/* replays a single extent in 'eb' at 'slot' with 'key' into the
565 * subvolume 'root'. path is released on entry and should be released
566 * on exit.
567 *
568 * extents in the log tree have not been allocated out of the extent
569 * tree yet. So, this completes the allocation, taking a reference
570 * as required if the extent already exists or creating a new extent
571 * if it isn't in the extent allocation tree yet.
572 *
573 * The extent is inserted into the file, dropping any existing extents
574 * from the file that overlap the new one.
575 */
576static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
577 struct btrfs_root *root,
578 struct btrfs_path *path,
579 struct extent_buffer *eb, int slot,
580 struct btrfs_key *key)
581{
582 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400583 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400584 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000585 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400586 struct btrfs_file_extent_item *item;
587 struct inode *inode = NULL;
588 unsigned long size;
589 int ret = 0;
590
591 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
592 found_type = btrfs_file_extent_type(eb, item);
593
Yan Zhengd899e052008-10-30 14:25:28 -0400594 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000595 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
596 nbytes = btrfs_file_extent_num_bytes(eb, item);
597 extent_end = start + nbytes;
598
599 /*
600 * We don't add to the inodes nbytes if we are prealloc or a
601 * hole.
602 */
603 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
604 nbytes = 0;
605 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800606 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000607 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000608 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400609 } else {
610 ret = 0;
611 goto out;
612 }
613
614 inode = read_one_inode(root, key->objectid);
615 if (!inode) {
616 ret = -EIO;
617 goto out;
618 }
619
620 /*
621 * first check to see if we already have this extent in the
622 * file. This must be done before the btrfs_drop_extents run
623 * so we don't try to drop this extent.
624 */
Li Zefan33345d012011-04-20 10:31:50 +0800625 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400626 start, 0);
627
Yan Zhengd899e052008-10-30 14:25:28 -0400628 if (ret == 0 &&
629 (found_type == BTRFS_FILE_EXTENT_REG ||
630 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400631 struct btrfs_file_extent_item cmp1;
632 struct btrfs_file_extent_item cmp2;
633 struct btrfs_file_extent_item *existing;
634 struct extent_buffer *leaf;
635
636 leaf = path->nodes[0];
637 existing = btrfs_item_ptr(leaf, path->slots[0],
638 struct btrfs_file_extent_item);
639
640 read_extent_buffer(eb, &cmp1, (unsigned long)item,
641 sizeof(cmp1));
642 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
643 sizeof(cmp2));
644
645 /*
646 * we already have a pointer to this exact extent,
647 * we don't have to do anything
648 */
649 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200650 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400651 goto out;
652 }
653 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200654 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400655
656 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400657 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400658 if (ret)
659 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400660
Yan Zheng07d400a2009-01-06 11:42:00 -0500661 if (found_type == BTRFS_FILE_EXTENT_REG ||
662 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400663 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500664 unsigned long dest_offset;
665 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400666
Yan Zheng07d400a2009-01-06 11:42:00 -0500667 ret = btrfs_insert_empty_item(trans, root, path, key,
668 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400669 if (ret)
670 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500671 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
672 path->slots[0]);
673 copy_extent_buffer(path->nodes[0], eb, dest_offset,
674 (unsigned long)item, sizeof(*item));
675
676 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
677 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
678 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400679 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500680
681 if (ins.objectid > 0) {
682 u64 csum_start;
683 u64 csum_end;
684 LIST_HEAD(ordered_sums);
685 /*
686 * is this extent already allocated in the extent
687 * allocation tree? If so, just add a reference
688 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000689 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500690 ins.offset);
691 if (ret == 0) {
692 ret = btrfs_inc_extent_ref(trans, root,
693 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400694 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200695 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400696 if (ret)
697 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500698 } else {
699 /*
700 * insert the extent pointer in the extent
701 * allocation tree
702 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400703 ret = btrfs_alloc_logged_file_extent(trans,
704 root, root->root_key.objectid,
705 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400706 if (ret)
707 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500708 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200709 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500710
711 if (btrfs_file_extent_compression(eb, item)) {
712 csum_start = ins.objectid;
713 csum_end = csum_start + ins.offset;
714 } else {
715 csum_start = ins.objectid +
716 btrfs_file_extent_offset(eb, item);
717 csum_end = csum_start +
718 btrfs_file_extent_num_bytes(eb, item);
719 }
720
721 ret = btrfs_lookup_csums_range(root->log_root,
722 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100723 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400724 if (ret)
725 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500726 while (!list_empty(&ordered_sums)) {
727 struct btrfs_ordered_sum *sums;
728 sums = list_entry(ordered_sums.next,
729 struct btrfs_ordered_sum,
730 list);
Josef Bacik36508602013-04-25 16:23:32 -0400731 if (!ret)
732 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500733 root->fs_info->csum_root,
734 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500735 list_del(&sums->list);
736 kfree(sums);
737 }
Josef Bacik36508602013-04-25 16:23:32 -0400738 if (ret)
739 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500740 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200741 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500742 }
743 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
744 /* inline extents are easy, we just overwrite them */
745 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400746 if (ret)
747 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500748 }
749
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000750 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600751 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400752out:
753 if (inode)
754 iput(inode);
755 return ret;
756}
757
758/*
759 * when cleaning up conflicts between the directory names in the
760 * subvolume, directory names in the log and directory names in the
761 * inode back references, we may have to unlink inodes from directories.
762 *
763 * This is a helper function to do the unlink of a specific directory
764 * item
765 */
766static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
767 struct btrfs_root *root,
768 struct btrfs_path *path,
769 struct inode *dir,
770 struct btrfs_dir_item *di)
771{
772 struct inode *inode;
773 char *name;
774 int name_len;
775 struct extent_buffer *leaf;
776 struct btrfs_key location;
777 int ret;
778
779 leaf = path->nodes[0];
780
781 btrfs_dir_item_key_to_cpu(leaf, di, &location);
782 name_len = btrfs_dir_name_len(leaf, di);
783 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000784 if (!name)
785 return -ENOMEM;
786
Chris Masone02119d2008-09-05 16:13:11 -0400787 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200788 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400789
790 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000791 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400792 ret = -EIO;
793 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000794 }
Chris Masone02119d2008-09-05 16:13:11 -0400795
Yan Zhengec051c02009-01-05 15:43:42 -0500796 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400797 if (ret)
798 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400799
Chris Masone02119d2008-09-05 16:13:11 -0400800 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400801 if (ret)
802 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100803 else
804 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400805out:
806 kfree(name);
807 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400808 return ret;
809}
810
811/*
812 * helper function to see if a given name and sequence number found
813 * in an inode back reference are already in a directory and correctly
814 * point to this inode
815 */
816static noinline int inode_in_dir(struct btrfs_root *root,
817 struct btrfs_path *path,
818 u64 dirid, u64 objectid, u64 index,
819 const char *name, int name_len)
820{
821 struct btrfs_dir_item *di;
822 struct btrfs_key location;
823 int match = 0;
824
825 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
826 index, name, name_len, 0);
827 if (di && !IS_ERR(di)) {
828 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
829 if (location.objectid != objectid)
830 goto out;
831 } else
832 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200833 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400834
835 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
836 if (di && !IS_ERR(di)) {
837 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
838 if (location.objectid != objectid)
839 goto out;
840 } else
841 goto out;
842 match = 1;
843out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200844 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400845 return match;
846}
847
848/*
849 * helper function to check a log tree for a named back reference in
850 * an inode. This is used to decide if a back reference that is
851 * found in the subvolume conflicts with what we find in the log.
852 *
853 * inode backreferences may have multiple refs in a single item,
854 * during replay we process one reference at a time, and we don't
855 * want to delete valid links to a file from the subvolume if that
856 * link is also in the log.
857 */
858static noinline int backref_in_log(struct btrfs_root *log,
859 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700860 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000861 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400862{
863 struct btrfs_path *path;
864 struct btrfs_inode_ref *ref;
865 unsigned long ptr;
866 unsigned long ptr_end;
867 unsigned long name_ptr;
868 int found_name_len;
869 int item_size;
870 int ret;
871 int match = 0;
872
873 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000874 if (!path)
875 return -ENOMEM;
876
Chris Masone02119d2008-09-05 16:13:11 -0400877 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
878 if (ret != 0)
879 goto out;
880
Chris Masone02119d2008-09-05 16:13:11 -0400881 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700882
883 if (key->type == BTRFS_INODE_EXTREF_KEY) {
884 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
885 name, namelen, NULL))
886 match = 1;
887
888 goto out;
889 }
890
891 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400892 ptr_end = ptr + item_size;
893 while (ptr < ptr_end) {
894 ref = (struct btrfs_inode_ref *)ptr;
895 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
896 if (found_name_len == namelen) {
897 name_ptr = (unsigned long)(ref + 1);
898 ret = memcmp_extent_buffer(path->nodes[0], name,
899 name_ptr, namelen);
900 if (ret == 0) {
901 match = 1;
902 goto out;
903 }
904 }
905 ptr = (unsigned long)(ref + 1) + found_name_len;
906 }
907out:
908 btrfs_free_path(path);
909 return match;
910}
911
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700912static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
913 struct btrfs_root *root,
914 struct btrfs_path *path,
915 struct btrfs_root *log_root,
916 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700917 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700918 u64 inode_objectid, u64 parent_objectid,
919 u64 ref_index, char *name, int namelen,
920 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700921{
922 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700923 char *victim_name;
924 int victim_name_len;
925 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700926 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700927 struct btrfs_key search_key;
928 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700929
Mark Fashehf1863732012-08-08 11:32:27 -0700930again:
931 /* Search old style refs */
932 search_key.objectid = inode_objectid;
933 search_key.type = BTRFS_INODE_REF_KEY;
934 search_key.offset = parent_objectid;
935 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700936 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700937 struct btrfs_inode_ref *victim_ref;
938 unsigned long ptr;
939 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700940
941 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700942
943 /* are we trying to overwrite a back ref for the root directory
944 * if so, just jump out, we're done
945 */
Mark Fashehf1863732012-08-08 11:32:27 -0700946 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700947 return 1;
948
949 /* check all the names in this back reference to see
950 * if they are in the log. if so, we allow them to stay
951 * otherwise they must be unlinked as a conflict
952 */
953 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
954 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
955 while (ptr < ptr_end) {
956 victim_ref = (struct btrfs_inode_ref *)ptr;
957 victim_name_len = btrfs_inode_ref_name_len(leaf,
958 victim_ref);
959 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400960 if (!victim_name)
961 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700962
963 read_extent_buffer(leaf, victim_name,
964 (unsigned long)(victim_ref + 1),
965 victim_name_len);
966
Mark Fashehf1863732012-08-08 11:32:27 -0700967 if (!backref_in_log(log_root, &search_key,
968 parent_objectid,
969 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700970 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700971 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700972 btrfs_release_path(path);
973
974 ret = btrfs_unlink_inode(trans, root, dir,
975 inode, victim_name,
976 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700977 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400978 if (ret)
979 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100980 ret = btrfs_run_delayed_items(trans, root);
981 if (ret)
982 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700983 *search_done = 1;
984 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700985 }
986 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700987
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700988 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
989 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700990
991 /*
992 * NOTE: we have searched root tree and checked the
993 * coresponding ref, it does not need to check again.
994 */
995 *search_done = 1;
996 }
997 btrfs_release_path(path);
998
Mark Fashehf1863732012-08-08 11:32:27 -0700999 /* Same search but for extended refs */
1000 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1001 inode_objectid, parent_objectid, 0,
1002 0);
1003 if (!IS_ERR_OR_NULL(extref)) {
1004 u32 item_size;
1005 u32 cur_offset = 0;
1006 unsigned long base;
1007 struct inode *victim_parent;
1008
1009 leaf = path->nodes[0];
1010
1011 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1012 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1013
1014 while (cur_offset < item_size) {
1015 extref = (struct btrfs_inode_extref *)base + cur_offset;
1016
1017 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1018
1019 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1020 goto next;
1021
1022 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001023 if (!victim_name)
1024 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001025 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1026 victim_name_len);
1027
1028 search_key.objectid = inode_objectid;
1029 search_key.type = BTRFS_INODE_EXTREF_KEY;
1030 search_key.offset = btrfs_extref_hash(parent_objectid,
1031 victim_name,
1032 victim_name_len);
1033 ret = 0;
1034 if (!backref_in_log(log_root, &search_key,
1035 parent_objectid, victim_name,
1036 victim_name_len)) {
1037 ret = -ENOENT;
1038 victim_parent = read_one_inode(root,
1039 parent_objectid);
1040 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001041 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001042 btrfs_release_path(path);
1043
1044 ret = btrfs_unlink_inode(trans, root,
1045 victim_parent,
1046 inode,
1047 victim_name,
1048 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001049 if (!ret)
1050 ret = btrfs_run_delayed_items(
1051 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001052 }
Mark Fashehf1863732012-08-08 11:32:27 -07001053 iput(victim_parent);
1054 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001055 if (ret)
1056 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001057 *search_done = 1;
1058 goto again;
1059 }
1060 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001061 if (ret)
1062 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001063next:
1064 cur_offset += victim_name_len + sizeof(*extref);
1065 }
1066 *search_done = 1;
1067 }
1068 btrfs_release_path(path);
1069
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001070 /* look for a conflicting sequence number */
1071 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001072 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001073 if (di && !IS_ERR(di)) {
1074 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001075 if (ret)
1076 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001077 }
1078 btrfs_release_path(path);
1079
1080 /* look for a conflicing name */
1081 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1082 name, namelen, 0);
1083 if (di && !IS_ERR(di)) {
1084 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001085 if (ret)
1086 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001087 }
1088 btrfs_release_path(path);
1089
1090 return 0;
1091}
Chris Masone02119d2008-09-05 16:13:11 -04001092
Mark Fashehf1863732012-08-08 11:32:27 -07001093static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1094 u32 *namelen, char **name, u64 *index,
1095 u64 *parent_objectid)
1096{
1097 struct btrfs_inode_extref *extref;
1098
1099 extref = (struct btrfs_inode_extref *)ref_ptr;
1100
1101 *namelen = btrfs_inode_extref_name_len(eb, extref);
1102 *name = kmalloc(*namelen, GFP_NOFS);
1103 if (*name == NULL)
1104 return -ENOMEM;
1105
1106 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1107 *namelen);
1108
1109 *index = btrfs_inode_extref_index(eb, extref);
1110 if (parent_objectid)
1111 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1112
1113 return 0;
1114}
1115
1116static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1117 u32 *namelen, char **name, u64 *index)
1118{
1119 struct btrfs_inode_ref *ref;
1120
1121 ref = (struct btrfs_inode_ref *)ref_ptr;
1122
1123 *namelen = btrfs_inode_ref_name_len(eb, ref);
1124 *name = kmalloc(*namelen, GFP_NOFS);
1125 if (*name == NULL)
1126 return -ENOMEM;
1127
1128 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1129
1130 *index = btrfs_inode_ref_index(eb, ref);
1131
1132 return 0;
1133}
1134
Chris Masone02119d2008-09-05 16:13:11 -04001135/*
1136 * replay one inode back reference item found in the log tree.
1137 * eb, slot and key refer to the buffer and key found in the log tree.
1138 * root is the destination we are replaying into, and path is for temp
1139 * use by this function. (it should be released on return).
1140 */
1141static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1142 struct btrfs_root *root,
1143 struct btrfs_root *log,
1144 struct btrfs_path *path,
1145 struct extent_buffer *eb, int slot,
1146 struct btrfs_key *key)
1147{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001148 struct inode *dir = NULL;
1149 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001150 unsigned long ref_ptr;
1151 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001152 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001153 int namelen;
1154 int ret;
liuboc622ae62011-03-26 08:01:12 -04001155 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001156 int log_ref_ver = 0;
1157 u64 parent_objectid;
1158 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001159 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001160 int ref_struct_size;
1161
1162 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1163 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1164
1165 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1166 struct btrfs_inode_extref *r;
1167
1168 ref_struct_size = sizeof(struct btrfs_inode_extref);
1169 log_ref_ver = 1;
1170 r = (struct btrfs_inode_extref *)ref_ptr;
1171 parent_objectid = btrfs_inode_extref_parent(eb, r);
1172 } else {
1173 ref_struct_size = sizeof(struct btrfs_inode_ref);
1174 parent_objectid = key->offset;
1175 }
1176 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001177
Chris Masone02119d2008-09-05 16:13:11 -04001178 /*
1179 * it is possible that we didn't log all the parent directories
1180 * for a given inode. If we don't find the dir, just don't
1181 * copy the back ref in. The link count fixup code will take
1182 * care of the rest
1183 */
Mark Fashehf1863732012-08-08 11:32:27 -07001184 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001185 if (!dir) {
1186 ret = -ENOENT;
1187 goto out;
1188 }
Chris Masone02119d2008-09-05 16:13:11 -04001189
Mark Fashehf1863732012-08-08 11:32:27 -07001190 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001191 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001192 ret = -EIO;
1193 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001194 }
Chris Masone02119d2008-09-05 16:13:11 -04001195
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001196 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001197 if (log_ref_ver) {
1198 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1199 &ref_index, &parent_objectid);
1200 /*
1201 * parent object can change from one array
1202 * item to another.
1203 */
1204 if (!dir)
1205 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001206 if (!dir) {
1207 ret = -ENOENT;
1208 goto out;
1209 }
Mark Fashehf1863732012-08-08 11:32:27 -07001210 } else {
1211 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1212 &ref_index);
1213 }
1214 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001215 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001216
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001217 /* if we already have a perfect match, we're done */
1218 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001219 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001220 /*
1221 * look for a conflicting back reference in the
1222 * metadata. if we find one we have to unlink that name
1223 * of the file before we add our new link. Later on, we
1224 * overwrite any existing back reference, and we don't
1225 * want to create dangling pointers in the directory.
1226 */
Chris Masone02119d2008-09-05 16:13:11 -04001227
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001228 if (!search_done) {
1229 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001230 dir, inode, eb,
1231 inode_objectid,
1232 parent_objectid,
1233 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001234 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001235 if (ret) {
1236 if (ret == 1)
1237 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001238 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001239 }
Chris Masone02119d2008-09-05 16:13:11 -04001240 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001241
1242 /* insert our name */
1243 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001244 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001245 if (ret)
1246 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001247
1248 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001249 }
liuboc622ae62011-03-26 08:01:12 -04001250
Mark Fashehf1863732012-08-08 11:32:27 -07001251 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001252 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001253 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001254 if (log_ref_ver) {
1255 iput(dir);
1256 dir = NULL;
1257 }
Chris Masone02119d2008-09-05 16:13:11 -04001258 }
Chris Masone02119d2008-09-05 16:13:11 -04001259
1260 /* finally write the back reference in the inode */
1261 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001262out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001263 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001264 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001265 iput(dir);
1266 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001267 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001268}
1269
Yan, Zhengc71bf092009-11-12 09:34:40 +00001270static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001271 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001272{
1273 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001274
David Sterba9c4f61f2015-01-02 19:12:57 +01001275 ret = btrfs_insert_orphan_item(trans, root, ino);
1276 if (ret == -EEXIST)
1277 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001278
Yan, Zhengc71bf092009-11-12 09:34:40 +00001279 return ret;
1280}
1281
Mark Fashehf1863732012-08-08 11:32:27 -07001282static int count_inode_extrefs(struct btrfs_root *root,
1283 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001284{
Mark Fashehf1863732012-08-08 11:32:27 -07001285 int ret = 0;
1286 int name_len;
1287 unsigned int nlink = 0;
1288 u32 item_size;
1289 u32 cur_offset = 0;
1290 u64 inode_objectid = btrfs_ino(inode);
1291 u64 offset = 0;
1292 unsigned long ptr;
1293 struct btrfs_inode_extref *extref;
1294 struct extent_buffer *leaf;
1295
1296 while (1) {
1297 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1298 &extref, &offset);
1299 if (ret)
1300 break;
1301
1302 leaf = path->nodes[0];
1303 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1304 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001305 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001306
1307 while (cur_offset < item_size) {
1308 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1309 name_len = btrfs_inode_extref_name_len(leaf, extref);
1310
1311 nlink++;
1312
1313 cur_offset += name_len + sizeof(*extref);
1314 }
1315
1316 offset++;
1317 btrfs_release_path(path);
1318 }
1319 btrfs_release_path(path);
1320
Filipe Manana2c2c4522015-01-13 16:40:04 +00001321 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001322 return ret;
1323 return nlink;
1324}
1325
1326static int count_inode_refs(struct btrfs_root *root,
1327 struct inode *inode, struct btrfs_path *path)
1328{
Chris Masone02119d2008-09-05 16:13:11 -04001329 int ret;
1330 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001331 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001332 unsigned long ptr;
1333 unsigned long ptr_end;
1334 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001335 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001336
Li Zefan33345d012011-04-20 10:31:50 +08001337 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001338 key.type = BTRFS_INODE_REF_KEY;
1339 key.offset = (u64)-1;
1340
Chris Masond3977122009-01-05 21:25:51 -05001341 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1343 if (ret < 0)
1344 break;
1345 if (ret > 0) {
1346 if (path->slots[0] == 0)
1347 break;
1348 path->slots[0]--;
1349 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001350process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001351 btrfs_item_key_to_cpu(path->nodes[0], &key,
1352 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001353 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001354 key.type != BTRFS_INODE_REF_KEY)
1355 break;
1356 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1357 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1358 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001359 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001360 struct btrfs_inode_ref *ref;
1361
1362 ref = (struct btrfs_inode_ref *)ptr;
1363 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1364 ref);
1365 ptr = (unsigned long)(ref + 1) + name_len;
1366 nlink++;
1367 }
1368
1369 if (key.offset == 0)
1370 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001371 if (path->slots[0] > 0) {
1372 path->slots[0]--;
1373 goto process_slot;
1374 }
Chris Masone02119d2008-09-05 16:13:11 -04001375 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001376 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001377 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001378 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001379
1380 return nlink;
1381}
1382
1383/*
1384 * There are a few corners where the link count of the file can't
1385 * be properly maintained during replay. So, instead of adding
1386 * lots of complexity to the log code, we just scan the backrefs
1387 * for any file that has been through replay.
1388 *
1389 * The scan will update the link count on the inode to reflect the
1390 * number of back refs found. If it goes down to zero, the iput
1391 * will free the inode.
1392 */
1393static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1394 struct btrfs_root *root,
1395 struct inode *inode)
1396{
1397 struct btrfs_path *path;
1398 int ret;
1399 u64 nlink = 0;
1400 u64 ino = btrfs_ino(inode);
1401
1402 path = btrfs_alloc_path();
1403 if (!path)
1404 return -ENOMEM;
1405
1406 ret = count_inode_refs(root, inode, path);
1407 if (ret < 0)
1408 goto out;
1409
1410 nlink = ret;
1411
1412 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001413 if (ret < 0)
1414 goto out;
1415
1416 nlink += ret;
1417
1418 ret = 0;
1419
Chris Masone02119d2008-09-05 16:13:11 -04001420 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001421 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001422 btrfs_update_inode(trans, root, inode);
1423 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001424 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001425
Yan, Zhengc71bf092009-11-12 09:34:40 +00001426 if (inode->i_nlink == 0) {
1427 if (S_ISDIR(inode->i_mode)) {
1428 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001429 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001430 if (ret)
1431 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001432 }
Li Zefan33345d012011-04-20 10:31:50 +08001433 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001434 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001435
Mark Fashehf1863732012-08-08 11:32:27 -07001436out:
1437 btrfs_free_path(path);
1438 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001439}
1440
1441static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1442 struct btrfs_root *root,
1443 struct btrfs_path *path)
1444{
1445 int ret;
1446 struct btrfs_key key;
1447 struct inode *inode;
1448
1449 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1450 key.type = BTRFS_ORPHAN_ITEM_KEY;
1451 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001452 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001453 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1454 if (ret < 0)
1455 break;
1456
1457 if (ret == 1) {
1458 if (path->slots[0] == 0)
1459 break;
1460 path->slots[0]--;
1461 }
1462
1463 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1464 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1465 key.type != BTRFS_ORPHAN_ITEM_KEY)
1466 break;
1467
1468 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001469 if (ret)
1470 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001471
David Sterbab3b4aa72011-04-21 01:20:15 +02001472 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001473 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001474 if (!inode)
1475 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001476
1477 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001478 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001479 if (ret)
1480 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001481
Chris Mason12fcfd22009-03-24 10:24:20 -04001482 /*
1483 * fixup on a directory may create new entries,
1484 * make sure we always look for the highset possible
1485 * offset
1486 */
1487 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001488 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001489 ret = 0;
1490out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001491 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001492 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001493}
1494
1495
1496/*
1497 * record a given inode in the fixup dir so we can check its link
1498 * count when replay is done. The link count is incremented here
1499 * so the inode won't go away until we check it
1500 */
1501static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1502 struct btrfs_root *root,
1503 struct btrfs_path *path,
1504 u64 objectid)
1505{
1506 struct btrfs_key key;
1507 int ret = 0;
1508 struct inode *inode;
1509
1510 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001511 if (!inode)
1512 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001513
1514 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001515 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001516 key.offset = objectid;
1517
1518 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1519
David Sterbab3b4aa72011-04-21 01:20:15 +02001520 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001521 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001522 if (!inode->i_nlink)
1523 set_nlink(inode, 1);
1524 else
Zach Brown8b558c52013-10-16 12:10:34 -07001525 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001526 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001527 } else if (ret == -EEXIST) {
1528 ret = 0;
1529 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001530 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001531 }
1532 iput(inode);
1533
1534 return ret;
1535}
1536
1537/*
1538 * when replaying the log for a directory, we only insert names
1539 * for inodes that actually exist. This means an fsync on a directory
1540 * does not implicitly fsync all the new files in it
1541 */
1542static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1543 struct btrfs_root *root,
1544 struct btrfs_path *path,
1545 u64 dirid, u64 index,
1546 char *name, int name_len, u8 type,
1547 struct btrfs_key *location)
1548{
1549 struct inode *inode;
1550 struct inode *dir;
1551 int ret;
1552
1553 inode = read_one_inode(root, location->objectid);
1554 if (!inode)
1555 return -ENOENT;
1556
1557 dir = read_one_inode(root, dirid);
1558 if (!dir) {
1559 iput(inode);
1560 return -EIO;
1561 }
Josef Bacikd5554382013-09-11 14:17:00 -04001562
Chris Masone02119d2008-09-05 16:13:11 -04001563 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1564
1565 /* FIXME, put inode into FIXUP list */
1566
1567 iput(inode);
1568 iput(dir);
1569 return ret;
1570}
1571
1572/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001573 * Return true if an inode reference exists in the log for the given name,
1574 * inode and parent inode.
1575 */
1576static bool name_in_log_ref(struct btrfs_root *log_root,
1577 const char *name, const int name_len,
1578 const u64 dirid, const u64 ino)
1579{
1580 struct btrfs_key search_key;
1581
1582 search_key.objectid = ino;
1583 search_key.type = BTRFS_INODE_REF_KEY;
1584 search_key.offset = dirid;
1585 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1586 return true;
1587
1588 search_key.type = BTRFS_INODE_EXTREF_KEY;
1589 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1590 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1591 return true;
1592
1593 return false;
1594}
1595
1596/*
Chris Masone02119d2008-09-05 16:13:11 -04001597 * take a single entry in a log directory item and replay it into
1598 * the subvolume.
1599 *
1600 * if a conflicting item exists in the subdirectory already,
1601 * the inode it points to is unlinked and put into the link count
1602 * fix up tree.
1603 *
1604 * If a name from the log points to a file or directory that does
1605 * not exist in the FS, it is skipped. fsyncs on directories
1606 * do not force down inodes inside that directory, just changes to the
1607 * names or unlinks in a directory.
1608 */
1609static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1610 struct btrfs_root *root,
1611 struct btrfs_path *path,
1612 struct extent_buffer *eb,
1613 struct btrfs_dir_item *di,
1614 struct btrfs_key *key)
1615{
1616 char *name;
1617 int name_len;
1618 struct btrfs_dir_item *dst_di;
1619 struct btrfs_key found_key;
1620 struct btrfs_key log_key;
1621 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001622 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001623 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001624 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001625 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001626
1627 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001628 if (!dir)
1629 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001630
1631 name_len = btrfs_dir_name_len(eb, di);
1632 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001633 if (!name) {
1634 ret = -ENOMEM;
1635 goto out;
1636 }
liubo2a29edc2011-01-26 06:22:08 +00001637
Chris Masone02119d2008-09-05 16:13:11 -04001638 log_type = btrfs_dir_type(eb, di);
1639 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1640 name_len);
1641
1642 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001643 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1644 if (exists == 0)
1645 exists = 1;
1646 else
1647 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001648 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001649
Chris Masone02119d2008-09-05 16:13:11 -04001650 if (key->type == BTRFS_DIR_ITEM_KEY) {
1651 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1652 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001653 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001654 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1655 key->objectid,
1656 key->offset, name,
1657 name_len, 1);
1658 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001659 /* Corruption */
1660 ret = -EINVAL;
1661 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001662 }
David Sterbac7040052011-04-19 18:00:01 +02001663 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001664 /* we need a sequence number to insert, so we only
1665 * do inserts for the BTRFS_DIR_INDEX_KEY types
1666 */
1667 if (key->type != BTRFS_DIR_INDEX_KEY)
1668 goto out;
1669 goto insert;
1670 }
1671
1672 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1673 /* the existing item matches the logged item */
1674 if (found_key.objectid == log_key.objectid &&
1675 found_key.type == log_key.type &&
1676 found_key.offset == log_key.offset &&
1677 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001678 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001679 goto out;
1680 }
1681
1682 /*
1683 * don't drop the conflicting directory entry if the inode
1684 * for the new entry doesn't exist
1685 */
Chris Mason4bef0842008-09-08 11:18:08 -04001686 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001687 goto out;
1688
Chris Masone02119d2008-09-05 16:13:11 -04001689 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001690 if (ret)
1691 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001692
1693 if (key->type == BTRFS_DIR_INDEX_KEY)
1694 goto insert;
1695out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001696 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001697 if (!ret && update_size) {
1698 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1699 ret = btrfs_update_inode(trans, root, dir);
1700 }
Chris Masone02119d2008-09-05 16:13:11 -04001701 kfree(name);
1702 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001703 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001704
1705insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001706 if (name_in_log_ref(root->log_root, name, name_len,
1707 key->objectid, log_key.objectid)) {
1708 /* The dentry will be added later. */
1709 ret = 0;
1710 update_size = false;
1711 goto out;
1712 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001713 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001714 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1715 name, name_len, log_type, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001716 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001717 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001718 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001719 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001720 goto out;
1721}
1722
1723/*
1724 * find all the names in a directory item and reconcile them into
1725 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1726 * one name in a directory item, but the same code gets used for
1727 * both directory index types
1728 */
1729static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1730 struct btrfs_root *root,
1731 struct btrfs_path *path,
1732 struct extent_buffer *eb, int slot,
1733 struct btrfs_key *key)
1734{
1735 int ret;
1736 u32 item_size = btrfs_item_size_nr(eb, slot);
1737 struct btrfs_dir_item *di;
1738 int name_len;
1739 unsigned long ptr;
1740 unsigned long ptr_end;
1741
1742 ptr = btrfs_item_ptr_offset(eb, slot);
1743 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001744 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001745 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001746 if (verify_dir_item(root, eb, di))
1747 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001748 name_len = btrfs_dir_name_len(eb, di);
1749 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001750 if (ret)
1751 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001752 ptr = (unsigned long)(di + 1);
1753 ptr += name_len;
1754 }
1755 return 0;
1756}
1757
1758/*
1759 * directory replay has two parts. There are the standard directory
1760 * items in the log copied from the subvolume, and range items
1761 * created in the log while the subvolume was logged.
1762 *
1763 * The range items tell us which parts of the key space the log
1764 * is authoritative for. During replay, if a key in the subvolume
1765 * directory is in a logged range item, but not actually in the log
1766 * that means it was deleted from the directory before the fsync
1767 * and should be removed.
1768 */
1769static noinline int find_dir_range(struct btrfs_root *root,
1770 struct btrfs_path *path,
1771 u64 dirid, int key_type,
1772 u64 *start_ret, u64 *end_ret)
1773{
1774 struct btrfs_key key;
1775 u64 found_end;
1776 struct btrfs_dir_log_item *item;
1777 int ret;
1778 int nritems;
1779
1780 if (*start_ret == (u64)-1)
1781 return 1;
1782
1783 key.objectid = dirid;
1784 key.type = key_type;
1785 key.offset = *start_ret;
1786
1787 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1788 if (ret < 0)
1789 goto out;
1790 if (ret > 0) {
1791 if (path->slots[0] == 0)
1792 goto out;
1793 path->slots[0]--;
1794 }
1795 if (ret != 0)
1796 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1797
1798 if (key.type != key_type || key.objectid != dirid) {
1799 ret = 1;
1800 goto next;
1801 }
1802 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1803 struct btrfs_dir_log_item);
1804 found_end = btrfs_dir_log_end(path->nodes[0], item);
1805
1806 if (*start_ret >= key.offset && *start_ret <= found_end) {
1807 ret = 0;
1808 *start_ret = key.offset;
1809 *end_ret = found_end;
1810 goto out;
1811 }
1812 ret = 1;
1813next:
1814 /* check the next slot in the tree to see if it is a valid item */
1815 nritems = btrfs_header_nritems(path->nodes[0]);
1816 if (path->slots[0] >= nritems) {
1817 ret = btrfs_next_leaf(root, path);
1818 if (ret)
1819 goto out;
1820 } else {
1821 path->slots[0]++;
1822 }
1823
1824 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1825
1826 if (key.type != key_type || key.objectid != dirid) {
1827 ret = 1;
1828 goto out;
1829 }
1830 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1831 struct btrfs_dir_log_item);
1832 found_end = btrfs_dir_log_end(path->nodes[0], item);
1833 *start_ret = key.offset;
1834 *end_ret = found_end;
1835 ret = 0;
1836out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001837 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001838 return ret;
1839}
1840
1841/*
1842 * this looks for a given directory item in the log. If the directory
1843 * item is not in the log, the item is removed and the inode it points
1844 * to is unlinked
1845 */
1846static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1847 struct btrfs_root *root,
1848 struct btrfs_root *log,
1849 struct btrfs_path *path,
1850 struct btrfs_path *log_path,
1851 struct inode *dir,
1852 struct btrfs_key *dir_key)
1853{
1854 int ret;
1855 struct extent_buffer *eb;
1856 int slot;
1857 u32 item_size;
1858 struct btrfs_dir_item *di;
1859 struct btrfs_dir_item *log_di;
1860 int name_len;
1861 unsigned long ptr;
1862 unsigned long ptr_end;
1863 char *name;
1864 struct inode *inode;
1865 struct btrfs_key location;
1866
1867again:
1868 eb = path->nodes[0];
1869 slot = path->slots[0];
1870 item_size = btrfs_item_size_nr(eb, slot);
1871 ptr = btrfs_item_ptr_offset(eb, slot);
1872 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001873 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001874 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001875 if (verify_dir_item(root, eb, di)) {
1876 ret = -EIO;
1877 goto out;
1878 }
1879
Chris Masone02119d2008-09-05 16:13:11 -04001880 name_len = btrfs_dir_name_len(eb, di);
1881 name = kmalloc(name_len, GFP_NOFS);
1882 if (!name) {
1883 ret = -ENOMEM;
1884 goto out;
1885 }
1886 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1887 name_len);
1888 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001889 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001890 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1891 dir_key->objectid,
1892 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001893 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001894 log_di = btrfs_lookup_dir_index_item(trans, log,
1895 log_path,
1896 dir_key->objectid,
1897 dir_key->offset,
1898 name, name_len, 0);
1899 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001900 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001901 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001902 btrfs_release_path(path);
1903 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001904 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001905 if (!inode) {
1906 kfree(name);
1907 return -EIO;
1908 }
Chris Masone02119d2008-09-05 16:13:11 -04001909
1910 ret = link_to_fixup_dir(trans, root,
1911 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001912 if (ret) {
1913 kfree(name);
1914 iput(inode);
1915 goto out;
1916 }
1917
Zach Brown8b558c52013-10-16 12:10:34 -07001918 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001919 ret = btrfs_unlink_inode(trans, root, dir, inode,
1920 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001921 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001922 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001923 kfree(name);
1924 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001925 if (ret)
1926 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001927
1928 /* there might still be more names under this key
1929 * check and repeat if required
1930 */
1931 ret = btrfs_search_slot(NULL, root, dir_key, path,
1932 0, 0);
1933 if (ret == 0)
1934 goto again;
1935 ret = 0;
1936 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001937 } else if (IS_ERR(log_di)) {
1938 kfree(name);
1939 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001940 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001941 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001942 kfree(name);
1943
1944 ptr = (unsigned long)(di + 1);
1945 ptr += name_len;
1946 }
1947 ret = 0;
1948out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001949 btrfs_release_path(path);
1950 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001951 return ret;
1952}
1953
1954/*
1955 * deletion replay happens before we copy any new directory items
1956 * out of the log or out of backreferences from inodes. It
1957 * scans the log to find ranges of keys that log is authoritative for,
1958 * and then scans the directory to find items in those ranges that are
1959 * not present in the log.
1960 *
1961 * Anything we don't find in the log is unlinked and removed from the
1962 * directory.
1963 */
1964static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1965 struct btrfs_root *root,
1966 struct btrfs_root *log,
1967 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001968 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001969{
1970 u64 range_start;
1971 u64 range_end;
1972 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1973 int ret = 0;
1974 struct btrfs_key dir_key;
1975 struct btrfs_key found_key;
1976 struct btrfs_path *log_path;
1977 struct inode *dir;
1978
1979 dir_key.objectid = dirid;
1980 dir_key.type = BTRFS_DIR_ITEM_KEY;
1981 log_path = btrfs_alloc_path();
1982 if (!log_path)
1983 return -ENOMEM;
1984
1985 dir = read_one_inode(root, dirid);
1986 /* it isn't an error if the inode isn't there, that can happen
1987 * because we replay the deletes before we copy in the inode item
1988 * from the log
1989 */
1990 if (!dir) {
1991 btrfs_free_path(log_path);
1992 return 0;
1993 }
1994again:
1995 range_start = 0;
1996 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001997 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001998 if (del_all)
1999 range_end = (u64)-1;
2000 else {
2001 ret = find_dir_range(log, path, dirid, key_type,
2002 &range_start, &range_end);
2003 if (ret != 0)
2004 break;
2005 }
Chris Masone02119d2008-09-05 16:13:11 -04002006
2007 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002008 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002009 int nritems;
2010 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2011 0, 0);
2012 if (ret < 0)
2013 goto out;
2014
2015 nritems = btrfs_header_nritems(path->nodes[0]);
2016 if (path->slots[0] >= nritems) {
2017 ret = btrfs_next_leaf(root, path);
2018 if (ret)
2019 break;
2020 }
2021 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2022 path->slots[0]);
2023 if (found_key.objectid != dirid ||
2024 found_key.type != dir_key.type)
2025 goto next_type;
2026
2027 if (found_key.offset > range_end)
2028 break;
2029
2030 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002031 log_path, dir,
2032 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002033 if (ret)
2034 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002035 if (found_key.offset == (u64)-1)
2036 break;
2037 dir_key.offset = found_key.offset + 1;
2038 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002039 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002040 if (range_end == (u64)-1)
2041 break;
2042 range_start = range_end + 1;
2043 }
2044
2045next_type:
2046 ret = 0;
2047 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2048 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2049 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002050 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002051 goto again;
2052 }
2053out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002054 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002055 btrfs_free_path(log_path);
2056 iput(dir);
2057 return ret;
2058}
2059
2060/*
2061 * the process_func used to replay items from the log tree. This
2062 * gets called in two different stages. The first stage just looks
2063 * for inodes and makes sure they are all copied into the subvolume.
2064 *
2065 * The second stage copies all the other item types from the log into
2066 * the subvolume. The two stage approach is slower, but gets rid of
2067 * lots of complexity around inodes referencing other inodes that exist
2068 * only in the log (references come from either directory items or inode
2069 * back refs).
2070 */
2071static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2072 struct walk_control *wc, u64 gen)
2073{
2074 int nritems;
2075 struct btrfs_path *path;
2076 struct btrfs_root *root = wc->replay_dest;
2077 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002078 int level;
2079 int i;
2080 int ret;
2081
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002082 ret = btrfs_read_buffer(eb, gen);
2083 if (ret)
2084 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002085
2086 level = btrfs_header_level(eb);
2087
2088 if (level != 0)
2089 return 0;
2090
2091 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002092 if (!path)
2093 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002094
2095 nritems = btrfs_header_nritems(eb);
2096 for (i = 0; i < nritems; i++) {
2097 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002098
2099 /* inode keys are done during the first stage */
2100 if (key.type == BTRFS_INODE_ITEM_KEY &&
2101 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002102 struct btrfs_inode_item *inode_item;
2103 u32 mode;
2104
2105 inode_item = btrfs_item_ptr(eb, i,
2106 struct btrfs_inode_item);
2107 mode = btrfs_inode_mode(eb, inode_item);
2108 if (S_ISDIR(mode)) {
2109 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002110 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002111 if (ret)
2112 break;
Chris Masone02119d2008-09-05 16:13:11 -04002113 }
2114 ret = overwrite_item(wc->trans, root, path,
2115 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002116 if (ret)
2117 break;
Chris Masone02119d2008-09-05 16:13:11 -04002118
Yan, Zhengc71bf092009-11-12 09:34:40 +00002119 /* for regular files, make sure corresponding
2120 * orhpan item exist. extents past the new EOF
2121 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002122 */
2123 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002124 ret = insert_orphan_item(wc->trans, root,
2125 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002126 if (ret)
2127 break;
Chris Masone02119d2008-09-05 16:13:11 -04002128 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002129
Chris Masone02119d2008-09-05 16:13:11 -04002130 ret = link_to_fixup_dir(wc->trans, root,
2131 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002132 if (ret)
2133 break;
Chris Masone02119d2008-09-05 16:13:11 -04002134 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002135
2136 if (key.type == BTRFS_DIR_INDEX_KEY &&
2137 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2138 ret = replay_one_dir_item(wc->trans, root, path,
2139 eb, i, &key);
2140 if (ret)
2141 break;
2142 }
2143
Chris Masone02119d2008-09-05 16:13:11 -04002144 if (wc->stage < LOG_WALK_REPLAY_ALL)
2145 continue;
2146
2147 /* these keys are simply copied */
2148 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2149 ret = overwrite_item(wc->trans, root, path,
2150 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002151 if (ret)
2152 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002153 } else if (key.type == BTRFS_INODE_REF_KEY ||
2154 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002155 ret = add_inode_ref(wc->trans, root, log, path,
2156 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002157 if (ret && ret != -ENOENT)
2158 break;
2159 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002160 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2161 ret = replay_one_extent(wc->trans, root, path,
2162 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002163 if (ret)
2164 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002165 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002166 ret = replay_one_dir_item(wc->trans, root, path,
2167 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002168 if (ret)
2169 break;
Chris Masone02119d2008-09-05 16:13:11 -04002170 }
2171 }
2172 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002173 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002174}
2175
Chris Masond3977122009-01-05 21:25:51 -05002176static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002177 struct btrfs_root *root,
2178 struct btrfs_path *path, int *level,
2179 struct walk_control *wc)
2180{
2181 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002182 u64 bytenr;
2183 u64 ptr_gen;
2184 struct extent_buffer *next;
2185 struct extent_buffer *cur;
2186 struct extent_buffer *parent;
2187 u32 blocksize;
2188 int ret = 0;
2189
2190 WARN_ON(*level < 0);
2191 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2192
Chris Masond3977122009-01-05 21:25:51 -05002193 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002194 WARN_ON(*level < 0);
2195 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2196 cur = path->nodes[*level];
2197
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302198 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002199
2200 if (path->slots[*level] >=
2201 btrfs_header_nritems(cur))
2202 break;
2203
2204 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2205 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002206 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002207
2208 parent = path->nodes[*level];
2209 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002210
David Sterbaa83fffb2014-06-15 02:39:54 +02002211 next = btrfs_find_create_tree_block(root, bytenr);
liubo2a29edc2011-01-26 06:22:08 +00002212 if (!next)
2213 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002214
Chris Masone02119d2008-09-05 16:13:11 -04002215 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002216 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002217 if (ret) {
2218 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002219 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002220 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002221
Chris Masone02119d2008-09-05 16:13:11 -04002222 path->slots[*level]++;
2223 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002224 ret = btrfs_read_buffer(next, ptr_gen);
2225 if (ret) {
2226 free_extent_buffer(next);
2227 return ret;
2228 }
Chris Masone02119d2008-09-05 16:13:11 -04002229
Josef Bacik681ae502013-10-07 15:11:00 -04002230 if (trans) {
2231 btrfs_tree_lock(next);
2232 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002233 clean_tree_block(trans, root->fs_info,
2234 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002235 btrfs_wait_tree_block_writeback(next);
2236 btrfs_tree_unlock(next);
2237 }
Chris Masone02119d2008-09-05 16:13:11 -04002238
Chris Masone02119d2008-09-05 16:13:11 -04002239 WARN_ON(root_owner !=
2240 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002241 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002242 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002243 if (ret) {
2244 free_extent_buffer(next);
2245 return ret;
2246 }
Chris Masone02119d2008-09-05 16:13:11 -04002247 }
2248 free_extent_buffer(next);
2249 continue;
2250 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002251 ret = btrfs_read_buffer(next, ptr_gen);
2252 if (ret) {
2253 free_extent_buffer(next);
2254 return ret;
2255 }
Chris Masone02119d2008-09-05 16:13:11 -04002256
2257 WARN_ON(*level <= 0);
2258 if (path->nodes[*level-1])
2259 free_extent_buffer(path->nodes[*level-1]);
2260 path->nodes[*level-1] = next;
2261 *level = btrfs_header_level(next);
2262 path->slots[*level] = 0;
2263 cond_resched();
2264 }
2265 WARN_ON(*level < 0);
2266 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2267
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002268 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002269
2270 cond_resched();
2271 return 0;
2272}
2273
Chris Masond3977122009-01-05 21:25:51 -05002274static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002275 struct btrfs_root *root,
2276 struct btrfs_path *path, int *level,
2277 struct walk_control *wc)
2278{
2279 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002280 int i;
2281 int slot;
2282 int ret;
2283
Chris Masond3977122009-01-05 21:25:51 -05002284 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002285 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002286 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002287 path->slots[i]++;
2288 *level = i;
2289 WARN_ON(*level == 0);
2290 return 0;
2291 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002292 struct extent_buffer *parent;
2293 if (path->nodes[*level] == root->node)
2294 parent = path->nodes[*level];
2295 else
2296 parent = path->nodes[*level + 1];
2297
2298 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002299 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002300 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002301 if (ret)
2302 return ret;
2303
Chris Masone02119d2008-09-05 16:13:11 -04002304 if (wc->free) {
2305 struct extent_buffer *next;
2306
2307 next = path->nodes[*level];
2308
Josef Bacik681ae502013-10-07 15:11:00 -04002309 if (trans) {
2310 btrfs_tree_lock(next);
2311 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002312 clean_tree_block(trans, root->fs_info,
2313 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002314 btrfs_wait_tree_block_writeback(next);
2315 btrfs_tree_unlock(next);
2316 }
Chris Masone02119d2008-09-05 16:13:11 -04002317
Chris Masone02119d2008-09-05 16:13:11 -04002318 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002319 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002320 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002321 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002322 if (ret)
2323 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002324 }
2325 free_extent_buffer(path->nodes[*level]);
2326 path->nodes[*level] = NULL;
2327 *level = i + 1;
2328 }
2329 }
2330 return 1;
2331}
2332
2333/*
2334 * drop the reference count on the tree rooted at 'snap'. This traverses
2335 * the tree freeing any blocks that have a ref count of zero after being
2336 * decremented.
2337 */
2338static int walk_log_tree(struct btrfs_trans_handle *trans,
2339 struct btrfs_root *log, struct walk_control *wc)
2340{
2341 int ret = 0;
2342 int wret;
2343 int level;
2344 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002345 int orig_level;
2346
2347 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002348 if (!path)
2349 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002350
2351 level = btrfs_header_level(log->node);
2352 orig_level = level;
2353 path->nodes[level] = log->node;
2354 extent_buffer_get(log->node);
2355 path->slots[level] = 0;
2356
Chris Masond3977122009-01-05 21:25:51 -05002357 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002358 wret = walk_down_log_tree(trans, log, path, &level, wc);
2359 if (wret > 0)
2360 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002361 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002362 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002363 goto out;
2364 }
Chris Masone02119d2008-09-05 16:13:11 -04002365
2366 wret = walk_up_log_tree(trans, log, path, &level, wc);
2367 if (wret > 0)
2368 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002369 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002370 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002371 goto out;
2372 }
Chris Masone02119d2008-09-05 16:13:11 -04002373 }
2374
2375 /* was the root node processed? if not, catch it here */
2376 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002377 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002378 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002379 if (ret)
2380 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002381 if (wc->free) {
2382 struct extent_buffer *next;
2383
2384 next = path->nodes[orig_level];
2385
Josef Bacik681ae502013-10-07 15:11:00 -04002386 if (trans) {
2387 btrfs_tree_lock(next);
2388 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002389 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002390 btrfs_wait_tree_block_writeback(next);
2391 btrfs_tree_unlock(next);
2392 }
Chris Masone02119d2008-09-05 16:13:11 -04002393
Chris Masone02119d2008-09-05 16:13:11 -04002394 WARN_ON(log->root_key.objectid !=
2395 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002396 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002397 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002398 if (ret)
2399 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002400 }
2401 }
2402
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002403out:
Chris Masone02119d2008-09-05 16:13:11 -04002404 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002405 return ret;
2406}
2407
Yan Zheng7237f182009-01-21 12:54:03 -05002408/*
2409 * helper function to update the item for a given subvolumes log root
2410 * in the tree of log roots
2411 */
2412static int update_log_root(struct btrfs_trans_handle *trans,
2413 struct btrfs_root *log)
2414{
2415 int ret;
2416
2417 if (log->log_transid == 1) {
2418 /* insert root item on the first sync */
2419 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2420 &log->root_key, &log->root_item);
2421 } else {
2422 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2423 &log->root_key, &log->root_item);
2424 }
2425 return ret;
2426}
2427
Miao Xie8b050d32014-02-20 18:08:58 +08002428static void wait_log_commit(struct btrfs_trans_handle *trans,
2429 struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002430{
2431 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002432 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002433
Yan Zheng7237f182009-01-21 12:54:03 -05002434 /*
2435 * we only allow two pending log transactions at a time,
2436 * so we know that if ours is more than 2 older than the
2437 * current transaction, we're done
2438 */
Chris Masone02119d2008-09-05 16:13:11 -04002439 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002440 prepare_to_wait(&root->log_commit_wait[index],
2441 &wait, TASK_UNINTERRUPTIBLE);
2442 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002443
Miao Xied1433de2014-02-20 18:08:59 +08002444 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002445 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002446 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002447
Yan Zheng7237f182009-01-21 12:54:03 -05002448 finish_wait(&root->log_commit_wait[index], &wait);
2449 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002450 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002451 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002452}
2453
Jeff Mahoney143bede2012-03-01 14:56:26 +01002454static void wait_for_writer(struct btrfs_trans_handle *trans,
2455 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002456{
2457 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002458
2459 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002460 prepare_to_wait(&root->log_writer_wait,
2461 &wait, TASK_UNINTERRUPTIBLE);
2462 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002463 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002464 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002465 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002466 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002467 }
Chris Masone02119d2008-09-05 16:13:11 -04002468}
2469
Miao Xie8b050d32014-02-20 18:08:58 +08002470static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2471 struct btrfs_log_ctx *ctx)
2472{
2473 if (!ctx)
2474 return;
2475
2476 mutex_lock(&root->log_mutex);
2477 list_del_init(&ctx->list);
2478 mutex_unlock(&root->log_mutex);
2479}
2480
2481/*
2482 * Invoked in log mutex context, or be sure there is no other task which
2483 * can access the list.
2484 */
2485static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2486 int index, int error)
2487{
2488 struct btrfs_log_ctx *ctx;
2489
2490 if (!error) {
2491 INIT_LIST_HEAD(&root->log_ctxs[index]);
2492 return;
2493 }
2494
2495 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2496 ctx->log_ret = error;
2497
2498 INIT_LIST_HEAD(&root->log_ctxs[index]);
2499}
2500
Chris Masone02119d2008-09-05 16:13:11 -04002501/*
2502 * btrfs_sync_log does sends a given tree log down to the disk and
2503 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002504 * you know that any inodes previously logged are safely on disk only
2505 * if it returns 0.
2506 *
2507 * Any other return value means you need to call btrfs_commit_transaction.
2508 * Some of the edge cases for fsyncing directories that have had unlinks
2509 * or renames done in the past mean that sometimes the only safe
2510 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2511 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002512 */
2513int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002514 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002515{
Yan Zheng7237f182009-01-21 12:54:03 -05002516 int index1;
2517 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002518 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002519 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002520 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002521 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002522 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002523 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002524 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002525
Yan Zheng7237f182009-01-21 12:54:03 -05002526 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002527 log_transid = ctx->log_transid;
2528 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002529 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002530 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002531 }
Miao Xied1433de2014-02-20 18:08:59 +08002532
2533 index1 = log_transid % 2;
2534 if (atomic_read(&root->log_commit[index1])) {
2535 wait_log_commit(trans, root, log_transid);
2536 mutex_unlock(&root->log_mutex);
2537 return ctx->log_ret;
2538 }
2539 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002540 atomic_set(&root->log_commit[index1], 1);
2541
2542 /* wait for previous tree log sync to complete */
2543 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Miao Xied1433de2014-02-20 18:08:59 +08002544 wait_log_commit(trans, root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002545
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002546 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002547 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002548 /* when we're on an ssd, just kick the log commit out */
Miao Xie27cdeb72014-04-02 19:51:05 +08002549 if (!btrfs_test_opt(root, SSD) &&
2550 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002551 mutex_unlock(&root->log_mutex);
2552 schedule_timeout_uninterruptible(1);
2553 mutex_lock(&root->log_mutex);
2554 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002555 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002556 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002557 break;
2558 }
Chris Masond0c803c2008-09-11 16:17:57 -04002559
Chris Mason12fcfd22009-03-24 10:24:20 -04002560 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002561 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002562 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002563 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002564 mutex_unlock(&root->log_mutex);
2565 goto out;
2566 }
2567
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002568 if (log_transid % 2 == 0)
2569 mark = EXTENT_DIRTY;
2570 else
2571 mark = EXTENT_NEW;
2572
Chris Mason690587d2009-10-13 13:29:19 -04002573 /* we start IO on all the marked extents here, but we don't actually
2574 * wait for them until later.
2575 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002576 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002577 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002578 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002579 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002580 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002581 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002582 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002583 mutex_unlock(&root->log_mutex);
2584 goto out;
2585 }
Yan Zheng7237f182009-01-21 12:54:03 -05002586
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002587 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002588
Yan Zheng7237f182009-01-21 12:54:03 -05002589 root->log_transid++;
2590 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002591 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002592 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002593 * IO has been started, blocks of the log tree have WRITTEN flag set
2594 * in their headers. new modifications of the log will be written to
2595 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002596 */
2597 mutex_unlock(&root->log_mutex);
2598
Miao Xied1433de2014-02-20 18:08:59 +08002599 btrfs_init_log_ctx(&root_log_ctx);
2600
Yan Zheng7237f182009-01-21 12:54:03 -05002601 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002602 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002603 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002604
2605 index2 = log_root_tree->log_transid % 2;
2606 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2607 root_log_ctx.log_transid = log_root_tree->log_transid;
2608
Yan Zheng7237f182009-01-21 12:54:03 -05002609 mutex_unlock(&log_root_tree->log_mutex);
2610
2611 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002612
2613 mutex_lock(&log_root_tree->log_mutex);
2614 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2615 smp_mb();
2616 if (waitqueue_active(&log_root_tree->log_writer_wait))
2617 wake_up(&log_root_tree->log_writer_wait);
2618 }
2619
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002620 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002621 if (!list_empty(&root_log_ctx.list))
2622 list_del_init(&root_log_ctx.list);
2623
Miao Xiec6adc9c2013-05-28 10:05:39 +00002624 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002625 btrfs_set_log_full_commit(root->fs_info, trans);
2626
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002627 if (ret != -ENOSPC) {
2628 btrfs_abort_transaction(trans, root, ret);
2629 mutex_unlock(&log_root_tree->log_mutex);
2630 goto out;
2631 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002632 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002633 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002634 mutex_unlock(&log_root_tree->log_mutex);
2635 ret = -EAGAIN;
2636 goto out;
2637 }
2638
Miao Xied1433de2014-02-20 18:08:59 +08002639 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
2640 mutex_unlock(&log_root_tree->log_mutex);
2641 ret = root_log_ctx.log_ret;
2642 goto out;
2643 }
Miao Xie8b050d32014-02-20 18:08:58 +08002644
Miao Xied1433de2014-02-20 18:08:59 +08002645 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002646 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002647 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002648 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2649 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002650 btrfs_wait_logged_extents(trans, log, log_transid);
Miao Xie8b050d32014-02-20 18:08:58 +08002651 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002652 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002653 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002654 if (!ret)
2655 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002656 goto out;
2657 }
Miao Xied1433de2014-02-20 18:08:59 +08002658 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002659 atomic_set(&log_root_tree->log_commit[index2], 1);
2660
Chris Mason12fcfd22009-03-24 10:24:20 -04002661 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2662 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002663 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002664 }
Yan Zheng7237f182009-01-21 12:54:03 -05002665
Chris Mason12fcfd22009-03-24 10:24:20 -04002666 wait_for_writer(trans, log_root_tree);
2667
2668 /*
2669 * now that we've moved on to the tree of log tree roots,
2670 * check the full commit flag again
2671 */
Miao Xie995946d2014-04-02 19:51:06 +08002672 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002673 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002674 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002675 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002676 mutex_unlock(&log_root_tree->log_mutex);
2677 ret = -EAGAIN;
2678 goto out_wake_log_root;
2679 }
Yan Zheng7237f182009-01-21 12:54:03 -05002680
Miao Xiec6adc9c2013-05-28 10:05:39 +00002681 ret = btrfs_write_marked_extents(log_root_tree,
2682 &log_root_tree->dirty_log_pages,
2683 EXTENT_DIRTY | EXTENT_NEW);
2684 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002685 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002686 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002687 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002688 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002689 mutex_unlock(&log_root_tree->log_mutex);
2690 goto out_wake_log_root;
2691 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002692 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2693 if (!ret)
2694 ret = btrfs_wait_marked_extents(log_root_tree,
2695 &log_root_tree->dirty_log_pages,
2696 EXTENT_NEW | EXTENT_DIRTY);
2697 if (ret) {
2698 btrfs_set_log_full_commit(root->fs_info, trans);
2699 btrfs_free_logged_extents(log, log_transid);
2700 mutex_unlock(&log_root_tree->log_mutex);
2701 goto out_wake_log_root;
2702 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002703 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002704
David Sterba6c417612011-04-13 15:41:04 +02002705 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002706 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002707 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002708 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002709
Yan Zheng7237f182009-01-21 12:54:03 -05002710 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002711 mutex_unlock(&log_root_tree->log_mutex);
2712
2713 /*
2714 * nobody else is going to jump in and write the the ctree
2715 * super here because the log_commit atomic below is protecting
2716 * us. We must be called with a transaction handle pinning
2717 * the running transaction open, so a full commit can't hop
2718 * in and cause problems either.
2719 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002720 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002721 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002722 btrfs_set_log_full_commit(root->fs_info, trans);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002723 btrfs_abort_transaction(trans, root, ret);
2724 goto out_wake_log_root;
2725 }
Yan Zheng7237f182009-01-21 12:54:03 -05002726
Chris Mason257c62e2009-10-13 13:21:08 -04002727 mutex_lock(&root->log_mutex);
2728 if (root->last_log_commit < log_transid)
2729 root->last_log_commit = log_transid;
2730 mutex_unlock(&root->log_mutex);
2731
Chris Mason12fcfd22009-03-24 10:24:20 -04002732out_wake_log_root:
Miao Xie8b050d32014-02-20 18:08:58 +08002733 /*
2734 * We needn't get log_mutex here because we are sure all
2735 * the other tasks are blocked.
2736 */
2737 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2738
Miao Xied1433de2014-02-20 18:08:59 +08002739 mutex_lock(&log_root_tree->log_mutex);
2740 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002741 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002742 mutex_unlock(&log_root_tree->log_mutex);
2743
Yan Zheng7237f182009-01-21 12:54:03 -05002744 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2745 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002746out:
Miao Xie8b050d32014-02-20 18:08:58 +08002747 /* See above. */
2748 btrfs_remove_all_log_ctxs(root, index1, ret);
2749
Miao Xied1433de2014-02-20 18:08:59 +08002750 mutex_lock(&root->log_mutex);
2751 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002752 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002753 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002754
Yan Zheng7237f182009-01-21 12:54:03 -05002755 if (waitqueue_active(&root->log_commit_wait[index1]))
2756 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002757 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002758}
2759
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002760static void free_log_tree(struct btrfs_trans_handle *trans,
2761 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002762{
2763 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002764 u64 start;
2765 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002766 struct walk_control wc = {
2767 .free = 1,
2768 .process_func = process_one_buffer
2769 };
2770
Josef Bacik681ae502013-10-07 15:11:00 -04002771 ret = walk_log_tree(trans, log, &wc);
2772 /* I don't think this can happen but just in case */
2773 if (ret)
2774 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002775
Chris Masond3977122009-01-05 21:25:51 -05002776 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002777 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002778 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2779 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002780 if (ret)
2781 break;
2782
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002783 clear_extent_bits(&log->dirty_log_pages, start, end,
2784 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002785 }
2786
Josef Bacik2ab28f32012-10-12 15:27:49 -04002787 /*
2788 * We may have short-circuited the log tree with the full commit logic
2789 * and left ordered extents on our list, so clear these out to keep us
2790 * from leaking inodes and memory.
2791 */
2792 btrfs_free_logged_extents(log, 0);
2793 btrfs_free_logged_extents(log, 1);
2794
Yan Zheng7237f182009-01-21 12:54:03 -05002795 free_extent_buffer(log->node);
2796 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002797}
2798
2799/*
2800 * free all the extents used by the tree log. This should be called
2801 * at commit time of the full transaction
2802 */
2803int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2804{
2805 if (root->log_root) {
2806 free_log_tree(trans, root->log_root);
2807 root->log_root = NULL;
2808 }
2809 return 0;
2810}
2811
2812int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2813 struct btrfs_fs_info *fs_info)
2814{
2815 if (fs_info->log_root_tree) {
2816 free_log_tree(trans, fs_info->log_root_tree);
2817 fs_info->log_root_tree = NULL;
2818 }
Chris Masone02119d2008-09-05 16:13:11 -04002819 return 0;
2820}
2821
2822/*
Chris Masone02119d2008-09-05 16:13:11 -04002823 * If both a file and directory are logged, and unlinks or renames are
2824 * mixed in, we have a few interesting corners:
2825 *
2826 * create file X in dir Y
2827 * link file X to X.link in dir Y
2828 * fsync file X
2829 * unlink file X but leave X.link
2830 * fsync dir Y
2831 *
2832 * After a crash we would expect only X.link to exist. But file X
2833 * didn't get fsync'd again so the log has back refs for X and X.link.
2834 *
2835 * We solve this by removing directory entries and inode backrefs from the
2836 * log when a file that was logged in the current transaction is
2837 * unlinked. Any later fsync will include the updated log entries, and
2838 * we'll be able to reconstruct the proper directory items from backrefs.
2839 *
2840 * This optimizations allows us to avoid relogging the entire inode
2841 * or the entire directory.
2842 */
2843int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2844 struct btrfs_root *root,
2845 const char *name, int name_len,
2846 struct inode *dir, u64 index)
2847{
2848 struct btrfs_root *log;
2849 struct btrfs_dir_item *di;
2850 struct btrfs_path *path;
2851 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002852 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002853 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002854 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002855
Chris Mason3a5f1d42008-09-11 15:53:37 -04002856 if (BTRFS_I(dir)->logged_trans < trans->transid)
2857 return 0;
2858
Chris Masone02119d2008-09-05 16:13:11 -04002859 ret = join_running_log_trans(root);
2860 if (ret)
2861 return 0;
2862
2863 mutex_lock(&BTRFS_I(dir)->log_mutex);
2864
2865 log = root->log_root;
2866 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002867 if (!path) {
2868 err = -ENOMEM;
2869 goto out_unlock;
2870 }
liubo2a29edc2011-01-26 06:22:08 +00002871
Li Zefan33345d012011-04-20 10:31:50 +08002872 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002873 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002874 if (IS_ERR(di)) {
2875 err = PTR_ERR(di);
2876 goto fail;
2877 }
2878 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002879 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2880 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002881 if (ret) {
2882 err = ret;
2883 goto fail;
2884 }
Chris Masone02119d2008-09-05 16:13:11 -04002885 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002886 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002887 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002888 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002889 if (IS_ERR(di)) {
2890 err = PTR_ERR(di);
2891 goto fail;
2892 }
2893 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002894 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2895 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002896 if (ret) {
2897 err = ret;
2898 goto fail;
2899 }
Chris Masone02119d2008-09-05 16:13:11 -04002900 }
2901
2902 /* update the directory size in the log to reflect the names
2903 * we have removed
2904 */
2905 if (bytes_del) {
2906 struct btrfs_key key;
2907
Li Zefan33345d012011-04-20 10:31:50 +08002908 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002909 key.offset = 0;
2910 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002911 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002912
2913 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002914 if (ret < 0) {
2915 err = ret;
2916 goto fail;
2917 }
Chris Masone02119d2008-09-05 16:13:11 -04002918 if (ret == 0) {
2919 struct btrfs_inode_item *item;
2920 u64 i_size;
2921
2922 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2923 struct btrfs_inode_item);
2924 i_size = btrfs_inode_size(path->nodes[0], item);
2925 if (i_size > bytes_del)
2926 i_size -= bytes_del;
2927 else
2928 i_size = 0;
2929 btrfs_set_inode_size(path->nodes[0], item, i_size);
2930 btrfs_mark_buffer_dirty(path->nodes[0]);
2931 } else
2932 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002933 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002934 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002935fail:
Chris Masone02119d2008-09-05 16:13:11 -04002936 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002937out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002938 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002939 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08002940 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002941 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002942 } else if (ret < 0)
2943 btrfs_abort_transaction(trans, root, ret);
2944
Chris Mason12fcfd22009-03-24 10:24:20 -04002945 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002946
Andi Kleen411fc6b2010-10-29 15:14:31 -04002947 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002948}
2949
2950/* see comments for btrfs_del_dir_entries_in_log */
2951int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2952 struct btrfs_root *root,
2953 const char *name, int name_len,
2954 struct inode *inode, u64 dirid)
2955{
2956 struct btrfs_root *log;
2957 u64 index;
2958 int ret;
2959
Chris Mason3a5f1d42008-09-11 15:53:37 -04002960 if (BTRFS_I(inode)->logged_trans < trans->transid)
2961 return 0;
2962
Chris Masone02119d2008-09-05 16:13:11 -04002963 ret = join_running_log_trans(root);
2964 if (ret)
2965 return 0;
2966 log = root->log_root;
2967 mutex_lock(&BTRFS_I(inode)->log_mutex);
2968
Li Zefan33345d012011-04-20 10:31:50 +08002969 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002970 dirid, &index);
2971 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002972 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08002973 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002974 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002975 } else if (ret < 0 && ret != -ENOENT)
2976 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002977 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002978
Chris Masone02119d2008-09-05 16:13:11 -04002979 return ret;
2980}
2981
2982/*
2983 * creates a range item in the log for 'dirid'. first_offset and
2984 * last_offset tell us which parts of the key space the log should
2985 * be considered authoritative for.
2986 */
2987static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2988 struct btrfs_root *log,
2989 struct btrfs_path *path,
2990 int key_type, u64 dirid,
2991 u64 first_offset, u64 last_offset)
2992{
2993 int ret;
2994 struct btrfs_key key;
2995 struct btrfs_dir_log_item *item;
2996
2997 key.objectid = dirid;
2998 key.offset = first_offset;
2999 if (key_type == BTRFS_DIR_ITEM_KEY)
3000 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3001 else
3002 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3003 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003004 if (ret)
3005 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003006
3007 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3008 struct btrfs_dir_log_item);
3009 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3010 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003011 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003012 return 0;
3013}
3014
3015/*
3016 * log all the items included in the current transaction for a given
3017 * directory. This also creates the range items in the log tree required
3018 * to replay anything deleted before the fsync
3019 */
3020static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3021 struct btrfs_root *root, struct inode *inode,
3022 struct btrfs_path *path,
3023 struct btrfs_path *dst_path, int key_type,
3024 u64 min_offset, u64 *last_offset_ret)
3025{
3026 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003027 struct btrfs_root *log = root->log_root;
3028 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003029 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003030 int ret;
3031 int i;
3032 int nritems;
3033 u64 first_offset = min_offset;
3034 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003035 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003036
3037 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003038
Li Zefan33345d012011-04-20 10:31:50 +08003039 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003040 min_key.type = key_type;
3041 min_key.offset = min_offset;
3042
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003043 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003044
3045 /*
3046 * we didn't find anything from this transaction, see if there
3047 * is anything at all
3048 */
Li Zefan33345d012011-04-20 10:31:50 +08003049 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3050 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003051 min_key.type = key_type;
3052 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003053 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003054 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3055 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003056 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003057 return ret;
3058 }
Li Zefan33345d012011-04-20 10:31:50 +08003059 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003060
3061 /* if ret == 0 there are items for this type,
3062 * create a range to tell us the last key of this type.
3063 * otherwise, there are no items in this directory after
3064 * *min_offset, and we create a range to indicate that.
3065 */
3066 if (ret == 0) {
3067 struct btrfs_key tmp;
3068 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3069 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003070 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003071 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003072 }
3073 goto done;
3074 }
3075
3076 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003077 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003078 if (ret == 0) {
3079 struct btrfs_key tmp;
3080 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3081 if (key_type == tmp.type) {
3082 first_offset = tmp.offset;
3083 ret = overwrite_item(trans, log, dst_path,
3084 path->nodes[0], path->slots[0],
3085 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003086 if (ret) {
3087 err = ret;
3088 goto done;
3089 }
Chris Masone02119d2008-09-05 16:13:11 -04003090 }
3091 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003092 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003093
3094 /* find the first key from this transaction again */
3095 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303096 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003097 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003098
3099 /*
3100 * we have a block from this transaction, log every item in it
3101 * from our directory
3102 */
Chris Masond3977122009-01-05 21:25:51 -05003103 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003104 struct btrfs_key tmp;
3105 src = path->nodes[0];
3106 nritems = btrfs_header_nritems(src);
3107 for (i = path->slots[0]; i < nritems; i++) {
3108 btrfs_item_key_to_cpu(src, &min_key, i);
3109
Li Zefan33345d012011-04-20 10:31:50 +08003110 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003111 goto done;
3112 ret = overwrite_item(trans, log, dst_path, src, i,
3113 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003114 if (ret) {
3115 err = ret;
3116 goto done;
3117 }
Chris Masone02119d2008-09-05 16:13:11 -04003118 }
3119 path->slots[0] = nritems;
3120
3121 /*
3122 * look ahead to the next item and see if it is also
3123 * from this directory and from this transaction
3124 */
3125 ret = btrfs_next_leaf(root, path);
3126 if (ret == 1) {
3127 last_offset = (u64)-1;
3128 goto done;
3129 }
3130 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003131 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003132 last_offset = (u64)-1;
3133 goto done;
3134 }
3135 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3136 ret = overwrite_item(trans, log, dst_path,
3137 path->nodes[0], path->slots[0],
3138 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003139 if (ret)
3140 err = ret;
3141 else
3142 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003143 goto done;
3144 }
3145 }
3146done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003147 btrfs_release_path(path);
3148 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003149
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003150 if (err == 0) {
3151 *last_offset_ret = last_offset;
3152 /*
3153 * insert the log range keys to indicate where the log
3154 * is valid
3155 */
3156 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003157 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003158 if (ret)
3159 err = ret;
3160 }
3161 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003162}
3163
3164/*
3165 * logging directories is very similar to logging inodes, We find all the items
3166 * from the current transaction and write them to the log.
3167 *
3168 * The recovery code scans the directory in the subvolume, and if it finds a
3169 * key in the range logged that is not present in the log tree, then it means
3170 * that dir entry was unlinked during the transaction.
3171 *
3172 * In order for that scan to work, we must include one key smaller than
3173 * the smallest logged by this transaction and one key larger than the largest
3174 * key logged by this transaction.
3175 */
3176static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3177 struct btrfs_root *root, struct inode *inode,
3178 struct btrfs_path *path,
3179 struct btrfs_path *dst_path)
3180{
3181 u64 min_key;
3182 u64 max_key;
3183 int ret;
3184 int key_type = BTRFS_DIR_ITEM_KEY;
3185
3186again:
3187 min_key = 0;
3188 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003189 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003190 ret = log_dir_items(trans, root, inode, path,
3191 dst_path, key_type, min_key,
3192 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003193 if (ret)
3194 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003195 if (max_key == (u64)-1)
3196 break;
3197 min_key = max_key + 1;
3198 }
3199
3200 if (key_type == BTRFS_DIR_ITEM_KEY) {
3201 key_type = BTRFS_DIR_INDEX_KEY;
3202 goto again;
3203 }
3204 return 0;
3205}
3206
3207/*
3208 * a helper function to drop items from the log before we relog an
3209 * inode. max_key_type indicates the highest item type to remove.
3210 * This cannot be run for file data extents because it does not
3211 * free the extents they point to.
3212 */
3213static int drop_objectid_items(struct btrfs_trans_handle *trans,
3214 struct btrfs_root *log,
3215 struct btrfs_path *path,
3216 u64 objectid, int max_key_type)
3217{
3218 int ret;
3219 struct btrfs_key key;
3220 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003221 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003222
3223 key.objectid = objectid;
3224 key.type = max_key_type;
3225 key.offset = (u64)-1;
3226
Chris Masond3977122009-01-05 21:25:51 -05003227 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003228 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003229 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003230 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003231 break;
3232
3233 if (path->slots[0] == 0)
3234 break;
3235
3236 path->slots[0]--;
3237 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3238 path->slots[0]);
3239
3240 if (found_key.objectid != objectid)
3241 break;
3242
Josef Bacik18ec90d2012-09-28 11:56:28 -04003243 found_key.offset = 0;
3244 found_key.type = 0;
3245 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3246 &start_slot);
3247
3248 ret = btrfs_del_items(trans, log, path, start_slot,
3249 path->slots[0] - start_slot + 1);
3250 /*
3251 * If start slot isn't 0 then we don't need to re-search, we've
3252 * found the last guy with the objectid in this tree.
3253 */
3254 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003255 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003256 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003257 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003258 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003259 if (ret > 0)
3260 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003261 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003262}
3263
Josef Bacik94edf4a2012-09-25 14:56:25 -04003264static void fill_inode_item(struct btrfs_trans_handle *trans,
3265 struct extent_buffer *leaf,
3266 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003267 struct inode *inode, int log_inode_only,
3268 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003269{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003270 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003271
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003272 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003273
3274 if (log_inode_only) {
3275 /* set the generation to zero so the recover code
3276 * can tell the difference between an logging
3277 * just to say 'this inode exists' and a logging
3278 * to say 'update this inode with these values'
3279 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003280 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003281 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003282 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003283 btrfs_set_token_inode_generation(leaf, item,
3284 BTRFS_I(inode)->generation,
3285 &token);
3286 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003287 }
3288
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003289 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3290 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3291 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3292 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3293
David Sterbaa937b972014-12-12 17:39:12 +01003294 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003295 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003296 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003297 inode->i_atime.tv_nsec, &token);
3298
David Sterbaa937b972014-12-12 17:39:12 +01003299 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003300 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003301 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003302 inode->i_mtime.tv_nsec, &token);
3303
David Sterbaa937b972014-12-12 17:39:12 +01003304 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003305 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003306 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003307 inode->i_ctime.tv_nsec, &token);
3308
3309 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3310 &token);
3311
3312 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3313 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3314 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3315 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3316 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003317}
3318
Josef Bacika95249b2012-10-11 16:17:34 -04003319static int log_inode_item(struct btrfs_trans_handle *trans,
3320 struct btrfs_root *log, struct btrfs_path *path,
3321 struct inode *inode)
3322{
3323 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003324 int ret;
3325
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003326 ret = btrfs_insert_empty_item(trans, log, path,
3327 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003328 sizeof(*inode_item));
3329 if (ret && ret != -EEXIST)
3330 return ret;
3331 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3332 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003333 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003334 btrfs_release_path(path);
3335 return 0;
3336}
3337
Chris Mason31ff1cd2008-09-11 16:17:57 -04003338static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003339 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003340 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003341 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003342 int start_slot, int nr, int inode_only,
3343 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003344{
3345 unsigned long src_offset;
3346 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003347 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003348 struct btrfs_file_extent_item *extent;
3349 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003350 struct extent_buffer *src = src_path->nodes[0];
3351 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003352 int ret;
3353 struct btrfs_key *ins_keys;
3354 u32 *ins_sizes;
3355 char *ins_data;
3356 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003357 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003358 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003359 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003360 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003361 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003362
3363 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003364
3365 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3366 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003367 if (!ins_data)
3368 return -ENOMEM;
3369
Josef Bacik16e75492013-10-22 12:18:51 -04003370 first_key.objectid = (u64)-1;
3371
Chris Mason31ff1cd2008-09-11 16:17:57 -04003372 ins_sizes = (u32 *)ins_data;
3373 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3374
3375 for (i = 0; i < nr; i++) {
3376 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3377 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3378 }
3379 ret = btrfs_insert_empty_items(trans, log, dst_path,
3380 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003381 if (ret) {
3382 kfree(ins_data);
3383 return ret;
3384 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003385
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003387 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3388 dst_path->slots[0]);
3389
3390 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3391
Josef Bacik16e75492013-10-22 12:18:51 -04003392 if ((i == (nr - 1)))
3393 last_key = ins_keys[i];
3394
Josef Bacik94edf4a2012-09-25 14:56:25 -04003395 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003396 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3397 dst_path->slots[0],
3398 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003399 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003400 inode, inode_only == LOG_INODE_EXISTS,
3401 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003402 } else {
3403 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3404 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003405 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003406
Josef Bacik16e75492013-10-22 12:18:51 -04003407 /*
3408 * We set need_find_last_extent here in case we know we were
3409 * processing other items and then walk into the first extent in
3410 * the inode. If we don't hit an extent then nothing changes,
3411 * we'll do the last search the next time around.
3412 */
3413 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3414 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003415 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003416 first_key = ins_keys[i];
3417 } else {
3418 need_find_last_extent = false;
3419 }
3420
Chris Mason31ff1cd2008-09-11 16:17:57 -04003421 /* take a reference on file data extents so that truncates
3422 * or deletes of this inode don't have to relog the inode
3423 * again
3424 */
David Sterba962a2982014-06-04 18:41:45 +02003425 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003426 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003427 int found_type;
3428 extent = btrfs_item_ptr(src, start_slot + i,
3429 struct btrfs_file_extent_item);
3430
liubo8e531cd2011-05-06 10:36:09 +08003431 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3432 continue;
3433
Chris Mason31ff1cd2008-09-11 16:17:57 -04003434 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003435 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003436 u64 ds, dl, cs, cl;
3437 ds = btrfs_file_extent_disk_bytenr(src,
3438 extent);
3439 /* ds == 0 is a hole */
3440 if (ds == 0)
3441 continue;
3442
3443 dl = btrfs_file_extent_disk_num_bytes(src,
3444 extent);
3445 cs = btrfs_file_extent_offset(src, extent);
3446 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003447 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003448 if (btrfs_file_extent_compression(src,
3449 extent)) {
3450 cs = 0;
3451 cl = dl;
3452 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003453
3454 ret = btrfs_lookup_csums_range(
3455 log->fs_info->csum_root,
3456 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003457 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003458 if (ret) {
3459 btrfs_release_path(dst_path);
3460 kfree(ins_data);
3461 return ret;
3462 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003463 }
3464 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003465 }
3466
3467 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003468 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003469 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003470
3471 /*
3472 * we have to do this after the loop above to avoid changing the
3473 * log tree while trying to change the log tree.
3474 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003475 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003476 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003477 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3478 struct btrfs_ordered_sum,
3479 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003480 if (!ret)
3481 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003482 list_del(&sums->list);
3483 kfree(sums);
3484 }
Josef Bacik16e75492013-10-22 12:18:51 -04003485
3486 if (!has_extents)
3487 return ret;
3488
Filipe Manana74121f72014-08-07 12:00:44 +01003489 if (need_find_last_extent && *last_extent == first_key.offset) {
3490 /*
3491 * We don't have any leafs between our current one and the one
3492 * we processed before that can have file extent items for our
3493 * inode (and have a generation number smaller than our current
3494 * transaction id).
3495 */
3496 need_find_last_extent = false;
3497 }
3498
Josef Bacik16e75492013-10-22 12:18:51 -04003499 /*
3500 * Because we use btrfs_search_forward we could skip leaves that were
3501 * not modified and then assume *last_extent is valid when it really
3502 * isn't. So back up to the previous leaf and read the end of the last
3503 * extent before we go and fill in holes.
3504 */
3505 if (need_find_last_extent) {
3506 u64 len;
3507
3508 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3509 if (ret < 0)
3510 return ret;
3511 if (ret)
3512 goto fill_holes;
3513 if (src_path->slots[0])
3514 src_path->slots[0]--;
3515 src = src_path->nodes[0];
3516 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3517 if (key.objectid != btrfs_ino(inode) ||
3518 key.type != BTRFS_EXTENT_DATA_KEY)
3519 goto fill_holes;
3520 extent = btrfs_item_ptr(src, src_path->slots[0],
3521 struct btrfs_file_extent_item);
3522 if (btrfs_file_extent_type(src, extent) ==
3523 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003524 len = btrfs_file_extent_inline_len(src,
3525 src_path->slots[0],
3526 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003527 *last_extent = ALIGN(key.offset + len,
3528 log->sectorsize);
3529 } else {
3530 len = btrfs_file_extent_num_bytes(src, extent);
3531 *last_extent = key.offset + len;
3532 }
3533 }
3534fill_holes:
3535 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3536 * things could have happened
3537 *
3538 * 1) A merge could have happened, so we could currently be on a leaf
3539 * that holds what we were copying in the first place.
3540 * 2) A split could have happened, and now not all of the items we want
3541 * are on the same leaf.
3542 *
3543 * So we need to adjust how we search for holes, we need to drop the
3544 * path and re-search for the first extent key we found, and then walk
3545 * forward until we hit the last one we copied.
3546 */
3547 if (need_find_last_extent) {
3548 /* btrfs_prev_leaf could return 1 without releasing the path */
3549 btrfs_release_path(src_path);
3550 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3551 src_path, 0, 0);
3552 if (ret < 0)
3553 return ret;
3554 ASSERT(ret == 0);
3555 src = src_path->nodes[0];
3556 i = src_path->slots[0];
3557 } else {
3558 i = start_slot;
3559 }
3560
3561 /*
3562 * Ok so here we need to go through and fill in any holes we may have
3563 * to make sure that holes are punched for those areas in case they had
3564 * extents previously.
3565 */
3566 while (!done) {
3567 u64 offset, len;
3568 u64 extent_end;
3569
3570 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3571 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3572 if (ret < 0)
3573 return ret;
3574 ASSERT(ret == 0);
3575 src = src_path->nodes[0];
3576 i = 0;
3577 }
3578
3579 btrfs_item_key_to_cpu(src, &key, i);
3580 if (!btrfs_comp_cpu_keys(&key, &last_key))
3581 done = true;
3582 if (key.objectid != btrfs_ino(inode) ||
3583 key.type != BTRFS_EXTENT_DATA_KEY) {
3584 i++;
3585 continue;
3586 }
3587 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3588 if (btrfs_file_extent_type(src, extent) ==
3589 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003590 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003591 extent_end = ALIGN(key.offset + len, log->sectorsize);
3592 } else {
3593 len = btrfs_file_extent_num_bytes(src, extent);
3594 extent_end = key.offset + len;
3595 }
3596 i++;
3597
3598 if (*last_extent == key.offset) {
3599 *last_extent = extent_end;
3600 continue;
3601 }
3602 offset = *last_extent;
3603 len = key.offset - *last_extent;
3604 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3605 offset, 0, 0, len, 0, len, 0,
3606 0, 0);
3607 if (ret)
3608 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003609 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003610 }
3611 /*
3612 * Need to let the callers know we dropped the path so they should
3613 * re-search.
3614 */
3615 if (!ret && need_find_last_extent)
3616 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003617 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003618}
3619
Josef Bacik5dc562c2012-08-17 13:14:17 -04003620static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3621{
3622 struct extent_map *em1, *em2;
3623
3624 em1 = list_entry(a, struct extent_map, list);
3625 em2 = list_entry(b, struct extent_map, list);
3626
3627 if (em1->start < em2->start)
3628 return -1;
3629 else if (em1->start > em2->start)
3630 return 1;
3631 return 0;
3632}
3633
Filipe Manana8407f552014-09-05 15:14:39 +01003634static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3635 struct inode *inode,
3636 struct btrfs_root *root,
3637 const struct extent_map *em,
3638 const struct list_head *logged_list,
3639 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003640{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003641 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003642 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003643 u64 mod_start = em->mod_start;
3644 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003645 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003646 u64 csum_offset;
3647 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003648 LIST_HEAD(ordered_sums);
3649 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003650
Filipe Manana8407f552014-09-05 15:14:39 +01003651 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003652
Filipe Manana8407f552014-09-05 15:14:39 +01003653 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3654 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003655 return 0;
3656
Josef Bacik2ab28f32012-10-12 15:27:49 -04003657 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003658 * Wait far any ordered extent that covers our extent map. If it
3659 * finishes without an error, first check and see if our csums are on
3660 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003661 */
Miao Xie827463c2014-01-14 20:31:51 +08003662 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003663 struct btrfs_ordered_sum *sum;
3664
3665 if (!mod_len)
3666 break;
3667
Josef Bacik2ab28f32012-10-12 15:27:49 -04003668 if (ordered->file_offset + ordered->len <= mod_start ||
3669 mod_start + mod_len <= ordered->file_offset)
3670 continue;
3671
Filipe Manana8407f552014-09-05 15:14:39 +01003672 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3673 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3674 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3675 const u64 start = ordered->file_offset;
3676 const u64 end = ordered->file_offset + ordered->len - 1;
3677
3678 WARN_ON(ordered->inode != inode);
3679 filemap_fdatawrite_range(inode->i_mapping, start, end);
3680 }
3681
3682 wait_event(ordered->wait,
3683 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3684 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3685
3686 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003687 /*
3688 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3689 * i_mapping flags, so that the next fsync won't get
3690 * an outdated io error too.
3691 */
3692 btrfs_inode_check_errors(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01003693 *ordered_io_error = true;
3694 break;
3695 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003696 /*
3697 * We are going to copy all the csums on this ordered extent, so
3698 * go ahead and adjust mod_start and mod_len in case this
3699 * ordered extent has already been logged.
3700 */
3701 if (ordered->file_offset > mod_start) {
3702 if (ordered->file_offset + ordered->len >=
3703 mod_start + mod_len)
3704 mod_len = ordered->file_offset - mod_start;
3705 /*
3706 * If we have this case
3707 *
3708 * |--------- logged extent ---------|
3709 * |----- ordered extent ----|
3710 *
3711 * Just don't mess with mod_start and mod_len, we'll
3712 * just end up logging more csums than we need and it
3713 * will be ok.
3714 */
3715 } else {
3716 if (ordered->file_offset + ordered->len <
3717 mod_start + mod_len) {
3718 mod_len = (mod_start + mod_len) -
3719 (ordered->file_offset + ordered->len);
3720 mod_start = ordered->file_offset +
3721 ordered->len;
3722 } else {
3723 mod_len = 0;
3724 }
3725 }
3726
Filipe Manana8407f552014-09-05 15:14:39 +01003727 if (skip_csum)
3728 continue;
3729
Josef Bacik2ab28f32012-10-12 15:27:49 -04003730 /*
3731 * To keep us from looping for the above case of an ordered
3732 * extent that falls inside of the logged extent.
3733 */
3734 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3735 &ordered->flags))
3736 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003737
Miao Xie23c671a2014-01-14 20:31:52 +08003738 if (ordered->csum_bytes_left) {
3739 btrfs_start_ordered_extent(inode, ordered, 0);
3740 wait_event(ordered->wait,
3741 ordered->csum_bytes_left == 0);
3742 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003743
3744 list_for_each_entry(sum, &ordered->list, list) {
3745 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003746 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01003747 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003748 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003749 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003750
Filipe Manana8407f552014-09-05 15:14:39 +01003751 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003752 return ret;
3753
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003754 if (em->compress_type) {
3755 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01003756 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003757 } else {
3758 csum_offset = mod_start - em->start;
3759 csum_len = mod_len;
3760 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003761
Josef Bacik70c8a912012-10-11 16:54:30 -04003762 /* block start is already adjusted for the file extent offset. */
3763 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3764 em->block_start + csum_offset,
3765 em->block_start + csum_offset +
3766 csum_len - 1, &ordered_sums, 0);
3767 if (ret)
3768 return ret;
3769
3770 while (!list_empty(&ordered_sums)) {
3771 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3772 struct btrfs_ordered_sum,
3773 list);
3774 if (!ret)
3775 ret = btrfs_csum_file_blocks(trans, log, sums);
3776 list_del(&sums->list);
3777 kfree(sums);
3778 }
3779
3780 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003781}
3782
Filipe Manana8407f552014-09-05 15:14:39 +01003783static int log_one_extent(struct btrfs_trans_handle *trans,
3784 struct inode *inode, struct btrfs_root *root,
3785 const struct extent_map *em,
3786 struct btrfs_path *path,
3787 const struct list_head *logged_list,
3788 struct btrfs_log_ctx *ctx)
3789{
3790 struct btrfs_root *log = root->log_root;
3791 struct btrfs_file_extent_item *fi;
3792 struct extent_buffer *leaf;
3793 struct btrfs_map_token token;
3794 struct btrfs_key key;
3795 u64 extent_offset = em->start - em->orig_start;
3796 u64 block_len;
3797 int ret;
3798 int extent_inserted = 0;
3799 bool ordered_io_err = false;
3800
3801 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
3802 &ordered_io_err);
3803 if (ret)
3804 return ret;
3805
3806 if (ordered_io_err) {
3807 ctx->io_err = -EIO;
3808 return 0;
3809 }
3810
3811 btrfs_init_map_token(&token);
3812
3813 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3814 em->start + em->len, NULL, 0, 1,
3815 sizeof(*fi), &extent_inserted);
3816 if (ret)
3817 return ret;
3818
3819 if (!extent_inserted) {
3820 key.objectid = btrfs_ino(inode);
3821 key.type = BTRFS_EXTENT_DATA_KEY;
3822 key.offset = em->start;
3823
3824 ret = btrfs_insert_empty_item(trans, log, path, &key,
3825 sizeof(*fi));
3826 if (ret)
3827 return ret;
3828 }
3829 leaf = path->nodes[0];
3830 fi = btrfs_item_ptr(leaf, path->slots[0],
3831 struct btrfs_file_extent_item);
3832
Josef Bacik50d9aa92014-11-21 14:52:38 -05003833 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01003834 &token);
3835 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3836 btrfs_set_token_file_extent_type(leaf, fi,
3837 BTRFS_FILE_EXTENT_PREALLOC,
3838 &token);
3839 else
3840 btrfs_set_token_file_extent_type(leaf, fi,
3841 BTRFS_FILE_EXTENT_REG,
3842 &token);
3843
3844 block_len = max(em->block_len, em->orig_block_len);
3845 if (em->compress_type != BTRFS_COMPRESS_NONE) {
3846 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3847 em->block_start,
3848 &token);
3849 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3850 &token);
3851 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
3852 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3853 em->block_start -
3854 extent_offset, &token);
3855 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3856 &token);
3857 } else {
3858 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3859 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3860 &token);
3861 }
3862
3863 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
3864 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
3865 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
3866 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3867 &token);
3868 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3869 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
3870 btrfs_mark_buffer_dirty(leaf);
3871
3872 btrfs_release_path(path);
3873
3874 return ret;
3875}
3876
Josef Bacik5dc562c2012-08-17 13:14:17 -04003877static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3878 struct btrfs_root *root,
3879 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08003880 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01003881 struct list_head *logged_list,
3882 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003883{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003884 struct extent_map *em, *n;
3885 struct list_head extents;
3886 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3887 u64 test_gen;
3888 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003889 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003890
3891 INIT_LIST_HEAD(&extents);
3892
Josef Bacik5dc562c2012-08-17 13:14:17 -04003893 write_lock(&tree->lock);
3894 test_gen = root->fs_info->last_trans_committed;
3895
3896 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3897 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003898
3899 /*
3900 * Just an arbitrary number, this can be really CPU intensive
3901 * once we start getting a lot of extents, and really once we
3902 * have a bunch of extents we just want to commit since it will
3903 * be faster.
3904 */
3905 if (++num > 32768) {
3906 list_del_init(&tree->modified_extents);
3907 ret = -EFBIG;
3908 goto process;
3909 }
3910
Josef Bacik5dc562c2012-08-17 13:14:17 -04003911 if (em->generation <= test_gen)
3912 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003913 /* Need a ref to keep it from getting evicted from cache */
3914 atomic_inc(&em->refs);
3915 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003916 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003917 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003918 }
3919
3920 list_sort(NULL, &extents, extent_cmp);
3921
Josef Bacik2ab28f32012-10-12 15:27:49 -04003922process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003923 while (!list_empty(&extents)) {
3924 em = list_entry(extents.next, struct extent_map, list);
3925
3926 list_del_init(&em->list);
3927
3928 /*
3929 * If we had an error we just need to delete everybody from our
3930 * private list.
3931 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003932 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003933 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003934 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003935 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003936 }
3937
3938 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003939
Filipe Manana8407f552014-09-05 15:14:39 +01003940 ret = log_one_extent(trans, inode, root, em, path, logged_list,
3941 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003942 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003943 clear_em_logging(tree, em);
3944 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003945 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003946 WARN_ON(!list_empty(&extents));
3947 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003948
Josef Bacik5dc562c2012-08-17 13:14:17 -04003949 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003950 return ret;
3951}
3952
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003953static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
3954 struct btrfs_path *path, u64 *size_ret)
3955{
3956 struct btrfs_key key;
3957 int ret;
3958
3959 key.objectid = btrfs_ino(inode);
3960 key.type = BTRFS_INODE_ITEM_KEY;
3961 key.offset = 0;
3962
3963 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
3964 if (ret < 0) {
3965 return ret;
3966 } else if (ret > 0) {
3967 *size_ret = i_size_read(inode);
3968 } else {
3969 struct btrfs_inode_item *item;
3970
3971 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3972 struct btrfs_inode_item);
3973 *size_ret = btrfs_inode_size(path->nodes[0], item);
3974 }
3975
3976 btrfs_release_path(path);
3977 return 0;
3978}
3979
Chris Masone02119d2008-09-05 16:13:11 -04003980/* log a single inode in the tree log.
3981 * At least one parent directory for this inode must exist in the tree
3982 * or be logged already.
3983 *
3984 * Any items from this inode changed by the current transaction are copied
3985 * to the log tree. An extra reference is taken on any extents in this
3986 * file, allowing us to avoid a whole pile of corner cases around logging
3987 * blocks that have been removed from the tree.
3988 *
3989 * See LOG_INODE_ALL and related defines for a description of what inode_only
3990 * does.
3991 *
3992 * This handles both files and directories.
3993 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003994static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01003995 struct btrfs_root *root, struct inode *inode,
3996 int inode_only,
3997 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01003998 const loff_t end,
3999 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004000{
4001 struct btrfs_path *path;
4002 struct btrfs_path *dst_path;
4003 struct btrfs_key min_key;
4004 struct btrfs_key max_key;
4005 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004006 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004007 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004008 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004009 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004010 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004011 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004012 int ins_start_slot = 0;
4013 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004014 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004015 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004016 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004017 u64 logged_isize = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004018
Chris Masone02119d2008-09-05 16:13:11 -04004019 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004020 if (!path)
4021 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004022 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004023 if (!dst_path) {
4024 btrfs_free_path(path);
4025 return -ENOMEM;
4026 }
Chris Masone02119d2008-09-05 16:13:11 -04004027
Li Zefan33345d012011-04-20 10:31:50 +08004028 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004029 min_key.type = BTRFS_INODE_ITEM_KEY;
4030 min_key.offset = 0;
4031
Li Zefan33345d012011-04-20 10:31:50 +08004032 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004033
Chris Mason12fcfd22009-03-24 10:24:20 -04004034
Josef Bacik5dc562c2012-08-17 13:14:17 -04004035 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004036 if (S_ISDIR(inode->i_mode) ||
4037 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4038 &BTRFS_I(inode)->runtime_flags) &&
4039 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004040 max_key.type = BTRFS_XATTR_ITEM_KEY;
4041 else
4042 max_key.type = (u8)-1;
4043 max_key.offset = (u64)-1;
4044
Filipe Manana2c2c4522015-01-13 16:40:04 +00004045 /*
4046 * Only run delayed items if we are a dir or a new file.
4047 * Otherwise commit the delayed inode only, which is needed in
4048 * order for the log replay code to mark inodes for link count
4049 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4050 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004051 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004052 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004053 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004054 else
4055 ret = btrfs_commit_inode_delayed_inode(inode);
4056
4057 if (ret) {
4058 btrfs_free_path(path);
4059 btrfs_free_path(dst_path);
4060 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004061 }
4062
Chris Masone02119d2008-09-05 16:13:11 -04004063 mutex_lock(&BTRFS_I(inode)->log_mutex);
4064
Filipe Manana08702952014-11-13 17:00:35 +00004065 btrfs_get_logged_extents(inode, &logged_list, start, end);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004066
Chris Masone02119d2008-09-05 16:13:11 -04004067 /*
4068 * a brute force approach to making sure we get the most uptodate
4069 * copies of everything.
4070 */
4071 if (S_ISDIR(inode->i_mode)) {
4072 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4073
Filipe Mananaa7429942015-02-13 16:56:14 +00004074 if (inode_only == LOG_INODE_EXISTS) {
4075 max_key_type = BTRFS_INODE_EXTREF_KEY;
4076 max_key.type = max_key_type;
4077 }
Li Zefan33345d012011-04-20 10:31:50 +08004078 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004079 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004080 if (inode_only == LOG_INODE_EXISTS) {
4081 /*
4082 * Make sure the new inode item we write to the log has
4083 * the same isize as the current one (if it exists).
4084 * This is necessary to prevent data loss after log
4085 * replay, and also to prevent doing a wrong expanding
4086 * truncate - for e.g. create file, write 4K into offset
4087 * 0, fsync, write 4K into offset 4096, add hard link,
4088 * fsync some other file (to sync log), power fail - if
4089 * we use the inode's current i_size, after log replay
4090 * we get a 8Kb file, with the last 4Kb extent as a hole
4091 * (zeroes), as if an expanding truncate happened,
4092 * instead of getting a file of 4Kb only.
4093 */
4094 err = logged_inode_size(log, inode, path,
4095 &logged_isize);
4096 if (err)
4097 goto out_unlock;
4098 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004099 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4100 &BTRFS_I(inode)->runtime_flags)) {
4101 if (inode_only == LOG_INODE_EXISTS) {
4102 max_key.type = BTRFS_INODE_EXTREF_KEY;
4103 ret = drop_objectid_items(trans, log, path, ino,
4104 max_key.type);
4105 } else {
4106 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4107 &BTRFS_I(inode)->runtime_flags);
4108 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4109 &BTRFS_I(inode)->runtime_flags);
4110 ret = btrfs_truncate_inode_items(trans, log,
4111 inode, 0, 0);
4112 }
4113 } else if (test_bit(BTRFS_INODE_COPY_EVERYTHING,
4114 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004115 inode_only == LOG_INODE_EXISTS) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004116 if (inode_only == LOG_INODE_ALL) {
4117 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4118 &BTRFS_I(inode)->runtime_flags);
Josef Bacika95249b2012-10-11 16:17:34 -04004119 fast_search = true;
Filipe Mananaa7429942015-02-13 16:56:14 +00004120 max_key.type = BTRFS_XATTR_ITEM_KEY;
4121 } else {
4122 max_key.type = BTRFS_INODE_EXTREF_KEY;
4123 }
Josef Bacika95249b2012-10-11 16:17:34 -04004124 ret = drop_objectid_items(trans, log, path, ino,
4125 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004127 if (inode_only == LOG_INODE_ALL)
4128 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004129 ret = log_inode_item(trans, log, dst_path, inode);
4130 if (ret) {
4131 err = ret;
4132 goto out_unlock;
4133 }
4134 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004135 }
Josef Bacika95249b2012-10-11 16:17:34 -04004136
Chris Masone02119d2008-09-05 16:13:11 -04004137 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004138 if (ret) {
4139 err = ret;
4140 goto out_unlock;
4141 }
Chris Masone02119d2008-09-05 16:13:11 -04004142
Chris Masond3977122009-01-05 21:25:51 -05004143 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004144 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004145 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004146 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04004147 if (ret != 0)
4148 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004149again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004150 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004151 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004152 break;
4153 if (min_key.type > max_key.type)
4154 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004155
Chris Masone02119d2008-09-05 16:13:11 -04004156 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004157 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4158 ins_nr++;
4159 goto next_slot;
4160 } else if (!ins_nr) {
4161 ins_start_slot = path->slots[0];
4162 ins_nr = 1;
4163 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004164 }
4165
Josef Bacik16e75492013-10-22 12:18:51 -04004166 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004167 ins_start_slot, ins_nr, inode_only,
4168 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004169 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004170 err = ret;
4171 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004172 }
4173 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004174 ins_nr = 0;
4175 btrfs_release_path(path);
4176 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004177 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004178 ins_nr = 1;
4179 ins_start_slot = path->slots[0];
4180next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004181
Chris Mason3a5f1d42008-09-11 15:53:37 -04004182 nritems = btrfs_header_nritems(path->nodes[0]);
4183 path->slots[0]++;
4184 if (path->slots[0] < nritems) {
4185 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4186 path->slots[0]);
4187 goto again;
4188 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004189 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004190 ret = copy_items(trans, inode, dst_path, path,
4191 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004192 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004193 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004194 err = ret;
4195 goto out_unlock;
4196 }
Josef Bacik16e75492013-10-22 12:18:51 -04004197 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004198 ins_nr = 0;
4199 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004200 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04004201
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004202 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004203 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004204 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004205 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004206 min_key.offset = 0;
4207 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004208 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004209 }
Chris Masone02119d2008-09-05 16:13:11 -04004210 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004211 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004212 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004213 ins_start_slot, ins_nr, inode_only,
4214 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004215 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004216 err = ret;
4217 goto out_unlock;
4218 }
Josef Bacik16e75492013-10-22 12:18:51 -04004219 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004220 ins_nr = 0;
4221 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004222
Josef Bacika95249b2012-10-11 16:17:34 -04004223log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004224 btrfs_release_path(path);
4225 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004226 if (fast_search) {
Filipe Mananab38ef712014-11-13 17:01:45 +00004227 /*
4228 * Some ordered extents started by fsync might have completed
4229 * before we collected the ordered extents in logged_list, which
4230 * means they're gone, not in our logged_list nor in the inode's
4231 * ordered tree. We want the application/user space to know an
4232 * error happened while attempting to persist file data so that
4233 * it can take proper action. If such error happened, we leave
4234 * without writing to the log tree and the fsync must report the
4235 * file data write error and not commit the current transaction.
4236 */
4237 err = btrfs_inode_check_errors(inode);
4238 if (err) {
4239 ctx->io_err = err;
4240 goto out_unlock;
4241 }
Miao Xie827463c2014-01-14 20:31:51 +08004242 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Manana8407f552014-09-05 15:14:39 +01004243 &logged_list, ctx);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004244 if (ret) {
4245 err = ret;
4246 goto out_unlock;
4247 }
Josef Bacikd006a042013-11-12 20:54:09 -05004248 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004249 struct extent_map *em, *n;
4250
Filipe Manana49dae1b2014-09-06 22:34:39 +01004251 write_lock(&em_tree->lock);
4252 /*
4253 * We can't just remove every em if we're called for a ranged
4254 * fsync - that is, one that doesn't cover the whole possible
4255 * file range (0 to LLONG_MAX). This is because we can have
4256 * em's that fall outside the range we're logging and therefore
4257 * their ordered operations haven't completed yet
4258 * (btrfs_finish_ordered_io() not invoked yet). This means we
4259 * didn't get their respective file extent item in the fs/subvol
4260 * tree yet, and need to let the next fast fsync (one which
4261 * consults the list of modified extent maps) find the em so
4262 * that it logs a matching file extent item and waits for the
4263 * respective ordered operation to complete (if it's still
4264 * running).
4265 *
4266 * Removing every em outside the range we're logging would make
4267 * the next fast fsync not log their matching file extent items,
4268 * therefore making us lose data after a log replay.
4269 */
4270 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4271 list) {
4272 const u64 mod_end = em->mod_start + em->mod_len - 1;
4273
4274 if (em->mod_start >= start && mod_end <= end)
4275 list_del_init(&em->list);
4276 }
4277 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004278 }
4279
Chris Mason9623f9a2008-09-11 17:42:42 -04004280 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004281 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004282 if (ret) {
4283 err = ret;
4284 goto out_unlock;
4285 }
Chris Masone02119d2008-09-05 16:13:11 -04004286 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004287
Filipe Manana125c4cf92014-09-11 21:22:14 +01004288 BTRFS_I(inode)->logged_trans = trans->transid;
4289 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004290out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004291 if (unlikely(err))
4292 btrfs_put_logged_extents(&logged_list);
4293 else
4294 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004295 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4296
4297 btrfs_free_path(path);
4298 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004299 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004300}
4301
Chris Mason12fcfd22009-03-24 10:24:20 -04004302/*
4303 * follow the dentry parent pointers up the chain and see if any
4304 * of the directories in it require a full commit before they can
4305 * be logged. Returns zero if nothing special needs to be done or 1 if
4306 * a full commit is required.
4307 */
4308static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4309 struct inode *inode,
4310 struct dentry *parent,
4311 struct super_block *sb,
4312 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004313{
Chris Mason12fcfd22009-03-24 10:24:20 -04004314 int ret = 0;
4315 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004316 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004317 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004318
Chris Masonaf4176b2009-03-24 10:24:31 -04004319 /*
4320 * for regular files, if its inode is already on disk, we don't
4321 * have to worry about the parents at all. This is because
4322 * we can use the last_unlink_trans field to record renames
4323 * and other fun in this file.
4324 */
4325 if (S_ISREG(inode->i_mode) &&
4326 BTRFS_I(inode)->generation <= last_committed &&
4327 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4328 goto out;
4329
Chris Mason12fcfd22009-03-24 10:24:20 -04004330 if (!S_ISDIR(inode->i_mode)) {
4331 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4332 goto out;
4333 inode = parent->d_inode;
4334 }
4335
4336 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004337 /*
4338 * If we are logging a directory then we start with our inode,
4339 * not our parents inode, so we need to skipp setting the
4340 * logged_trans so that further down in the log code we don't
4341 * think this inode has already been logged.
4342 */
4343 if (inode != orig_inode)
4344 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004345 smp_mb();
4346
4347 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4348 root = BTRFS_I(inode)->root;
4349
4350 /*
4351 * make sure any commits to the log are forced
4352 * to be full commits
4353 */
Miao Xie995946d2014-04-02 19:51:06 +08004354 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04004355 ret = 1;
4356 break;
4357 }
4358
4359 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4360 break;
4361
Yan, Zheng76dda932009-09-21 16:00:26 -04004362 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004363 break;
4364
Josef Bacik6a912212010-11-20 09:48:00 +00004365 parent = dget_parent(parent);
4366 dput(old_parent);
4367 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04004368 inode = parent->d_inode;
4369
4370 }
Josef Bacik6a912212010-11-20 09:48:00 +00004371 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004372out:
Chris Masone02119d2008-09-05 16:13:11 -04004373 return ret;
4374}
4375
4376/*
4377 * helper function around btrfs_log_inode to make sure newly created
4378 * parent directories also end up in the log. A minimal inode and backref
4379 * only logging is done of any parent directories that are older than
4380 * the last committed transaction
4381 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004382static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4383 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004384 struct dentry *parent,
4385 const loff_t start,
4386 const loff_t end,
4387 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08004388 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004389{
Chris Mason12fcfd22009-03-24 10:24:20 -04004390 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04004391 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00004392 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04004393 int ret = 0;
4394 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Mananad36808e2015-01-10 10:56:48 +00004395 const struct dentry * const first_parent = parent;
4396 const bool did_unlink = (BTRFS_I(inode)->last_unlink_trans >
4397 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04004398
4399 sb = inode->i_sb;
4400
Sage Weil3a5e1402009-04-02 16:49:40 -04004401 if (btrfs_test_opt(root, NOTREELOG)) {
4402 ret = 1;
4403 goto end_no_trans;
4404 }
4405
Miao Xie995946d2014-04-02 19:51:06 +08004406 /*
4407 * The prev transaction commit doesn't complete, we need do
4408 * full commit by ourselves.
4409 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004410 if (root->fs_info->last_trans_log_full_commit >
4411 root->fs_info->last_trans_committed) {
4412 ret = 1;
4413 goto end_no_trans;
4414 }
4415
Yan, Zheng76dda932009-09-21 16:00:26 -04004416 if (root != BTRFS_I(inode)->root ||
4417 btrfs_root_refs(&root->root_item) == 0) {
4418 ret = 1;
4419 goto end_no_trans;
4420 }
4421
Chris Mason12fcfd22009-03-24 10:24:20 -04004422 ret = check_parent_dirs_for_sync(trans, inode, parent,
4423 sb, last_committed);
4424 if (ret)
4425 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04004426
Josef Bacik22ee6982012-05-29 16:57:49 -04004427 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04004428 ret = BTRFS_NO_LOG_SYNC;
4429 goto end_no_trans;
4430 }
4431
Miao Xie8b050d32014-02-20 18:08:58 +08004432 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004433 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08004434 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004435
Filipe Manana8407f552014-09-05 15:14:39 +01004436 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004437 if (ret)
4438 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004439
Chris Masonaf4176b2009-03-24 10:24:31 -04004440 /*
4441 * for regular files, if its inode is already on disk, we don't
4442 * have to worry about the parents at all. This is because
4443 * we can use the last_unlink_trans field to record renames
4444 * and other fun in this file.
4445 */
4446 if (S_ISREG(inode->i_mode) &&
4447 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004448 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4449 ret = 0;
4450 goto end_trans;
4451 }
Chris Masonaf4176b2009-03-24 10:24:31 -04004452
Chris Masond3977122009-01-05 21:25:51 -05004453 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04004454 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04004455 break;
4456
Chris Mason12fcfd22009-03-24 10:24:20 -04004457 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004458 if (root != BTRFS_I(inode)->root)
4459 break;
4460
Filipe Mananad36808e2015-01-10 10:56:48 +00004461 /*
4462 * On unlink we must make sure our immediate parent directory
4463 * inode is fully logged. This is to prevent leaving dangling
4464 * directory index entries and a wrong directory inode's i_size.
4465 * Not doing so can result in a directory being impossible to
4466 * delete after log replay (rmdir will always fail with error
4467 * -ENOTEMPTY).
4468 */
4469 if (did_unlink && parent == first_parent)
4470 inode_only = LOG_INODE_ALL;
4471 else
4472 inode_only = LOG_INODE_EXISTS;
4473
Chris Mason12fcfd22009-03-24 10:24:20 -04004474 if (BTRFS_I(inode)->generation >
Filipe Mananad36808e2015-01-10 10:56:48 +00004475 root->fs_info->last_trans_committed ||
4476 inode_only == LOG_INODE_ALL) {
Filipe Manana49dae1b2014-09-06 22:34:39 +01004477 ret = btrfs_log_inode(trans, root, inode, inode_only,
Filipe Manana8407f552014-09-05 15:14:39 +01004478 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004479 if (ret)
4480 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004481 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004482 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04004483 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04004484
Josef Bacik6a912212010-11-20 09:48:00 +00004485 parent = dget_parent(parent);
4486 dput(old_parent);
4487 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04004488 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004489 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004490end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004491 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004492 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08004493 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004494 ret = 1;
4495 }
Miao Xie8b050d32014-02-20 18:08:58 +08004496
4497 if (ret)
4498 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04004499 btrfs_end_log_trans(root);
4500end_no_trans:
4501 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004502}
4503
4504/*
4505 * it is not safe to log dentry if the chunk root has added new
4506 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4507 * If this returns 1, you must commit the transaction to safely get your
4508 * data on disk.
4509 */
4510int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08004511 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004512 const loff_t start,
4513 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08004514 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004515{
Josef Bacik6a912212010-11-20 09:48:00 +00004516 struct dentry *parent = dget_parent(dentry);
4517 int ret;
4518
Miao Xie8b050d32014-02-20 18:08:58 +08004519 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004520 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00004521 dput(parent);
4522
4523 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004524}
4525
4526/*
4527 * should be called during mount to recover any replay any log trees
4528 * from the FS
4529 */
4530int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4531{
4532 int ret;
4533 struct btrfs_path *path;
4534 struct btrfs_trans_handle *trans;
4535 struct btrfs_key key;
4536 struct btrfs_key found_key;
4537 struct btrfs_key tmp_key;
4538 struct btrfs_root *log;
4539 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4540 struct walk_control wc = {
4541 .process_func = process_one_buffer,
4542 .stage = 0,
4543 };
4544
Chris Masone02119d2008-09-05 16:13:11 -04004545 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004546 if (!path)
4547 return -ENOMEM;
4548
4549 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004550
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004551 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004552 if (IS_ERR(trans)) {
4553 ret = PTR_ERR(trans);
4554 goto error;
4555 }
Chris Masone02119d2008-09-05 16:13:11 -04004556
4557 wc.trans = trans;
4558 wc.pin = 1;
4559
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004560 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004561 if (ret) {
4562 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4563 "recovering log root tree.");
4564 goto error;
4565 }
Chris Masone02119d2008-09-05 16:13:11 -04004566
4567again:
4568 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4569 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02004570 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04004571
Chris Masond3977122009-01-05 21:25:51 -05004572 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004573 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004574
4575 if (ret < 0) {
4576 btrfs_error(fs_info, ret,
4577 "Couldn't find tree log root.");
4578 goto error;
4579 }
Chris Masone02119d2008-09-05 16:13:11 -04004580 if (ret > 0) {
4581 if (path->slots[0] == 0)
4582 break;
4583 path->slots[0]--;
4584 }
4585 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4586 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004587 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004588 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4589 break;
4590
Miao Xiecb517ea2013-05-15 07:48:19 +00004591 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004592 if (IS_ERR(log)) {
4593 ret = PTR_ERR(log);
4594 btrfs_error(fs_info, ret,
4595 "Couldn't read tree log root.");
4596 goto error;
4597 }
Chris Masone02119d2008-09-05 16:13:11 -04004598
4599 tmp_key.objectid = found_key.offset;
4600 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4601 tmp_key.offset = (u64)-1;
4602
4603 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004604 if (IS_ERR(wc.replay_dest)) {
4605 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004606 free_extent_buffer(log->node);
4607 free_extent_buffer(log->commit_root);
4608 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004609 btrfs_error(fs_info, ret, "Couldn't read target root "
4610 "for tree log recovery.");
4611 goto error;
4612 }
Chris Masone02119d2008-09-05 16:13:11 -04004613
Yan Zheng07d400a2009-01-06 11:42:00 -05004614 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004615 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004616 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004617
Josef Bacikb50c6e22013-04-25 15:55:30 -04004618 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004619 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4620 path);
Chris Masone02119d2008-09-05 16:13:11 -04004621 }
Chris Masone02119d2008-09-05 16:13:11 -04004622
4623 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004624 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004625 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004626 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004627 kfree(log);
4628
Josef Bacikb50c6e22013-04-25 15:55:30 -04004629 if (ret)
4630 goto error;
4631
Chris Masone02119d2008-09-05 16:13:11 -04004632 if (found_key.offset == 0)
4633 break;
4634 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004635 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004636
4637 /* step one is to pin it all, step two is to replay just inodes */
4638 if (wc.pin) {
4639 wc.pin = 0;
4640 wc.process_func = replay_one_buffer;
4641 wc.stage = LOG_WALK_REPLAY_INODES;
4642 goto again;
4643 }
4644 /* step three is to replay everything */
4645 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4646 wc.stage++;
4647 goto again;
4648 }
4649
4650 btrfs_free_path(path);
4651
Josef Bacikabefa552013-04-24 16:40:05 -04004652 /* step 4: commit the transaction, which also unpins the blocks */
4653 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4654 if (ret)
4655 return ret;
4656
Chris Masone02119d2008-09-05 16:13:11 -04004657 free_extent_buffer(log_root_tree->node);
4658 log_root_tree->log_root = NULL;
4659 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004660 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004661
Josef Bacikabefa552013-04-24 16:40:05 -04004662 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004663error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004664 if (wc.trans)
4665 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004666 btrfs_free_path(path);
4667 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004668}
Chris Mason12fcfd22009-03-24 10:24:20 -04004669
4670/*
4671 * there are some corner cases where we want to force a full
4672 * commit instead of allowing a directory to be logged.
4673 *
4674 * They revolve around files there were unlinked from the directory, and
4675 * this function updates the parent directory so that a full commit is
4676 * properly done if it is fsync'd later after the unlinks are done.
4677 */
4678void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4679 struct inode *dir, struct inode *inode,
4680 int for_rename)
4681{
4682 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004683 * when we're logging a file, if it hasn't been renamed
4684 * or unlinked, and its inode is fully committed on disk,
4685 * we don't have to worry about walking up the directory chain
4686 * to log its parents.
4687 *
4688 * So, we use the last_unlink_trans field to put this transid
4689 * into the file. When the file is logged we check it and
4690 * don't log the parents if the file is fully on disk.
4691 */
4692 if (S_ISREG(inode->i_mode))
4693 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4694
4695 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004696 * if this directory was already logged any new
4697 * names for this file/dir will get recorded
4698 */
4699 smp_mb();
4700 if (BTRFS_I(dir)->logged_trans == trans->transid)
4701 return;
4702
4703 /*
4704 * if the inode we're about to unlink was logged,
4705 * the log will be properly updated for any new names
4706 */
4707 if (BTRFS_I(inode)->logged_trans == trans->transid)
4708 return;
4709
4710 /*
4711 * when renaming files across directories, if the directory
4712 * there we're unlinking from gets fsync'd later on, there's
4713 * no way to find the destination directory later and fsync it
4714 * properly. So, we have to be conservative and force commits
4715 * so the new name gets discovered.
4716 */
4717 if (for_rename)
4718 goto record;
4719
4720 /* we can safely do the unlink without any special recording */
4721 return;
4722
4723record:
4724 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4725}
4726
4727/*
4728 * Call this after adding a new name for a file and it will properly
4729 * update the log to reflect the new name.
4730 *
4731 * It will return zero if all goes well, and it will return 1 if a
4732 * full transaction commit is required.
4733 */
4734int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4735 struct inode *inode, struct inode *old_dir,
4736 struct dentry *parent)
4737{
4738 struct btrfs_root * root = BTRFS_I(inode)->root;
4739
4740 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004741 * this will force the logging code to walk the dentry chain
4742 * up for the file
4743 */
4744 if (S_ISREG(inode->i_mode))
4745 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4746
4747 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004748 * if this inode hasn't been logged and directory we're renaming it
4749 * from hasn't been logged, we don't need to log it
4750 */
4751 if (BTRFS_I(inode)->logged_trans <=
4752 root->fs_info->last_trans_committed &&
4753 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4754 root->fs_info->last_trans_committed))
4755 return 0;
4756
Filipe Manana49dae1b2014-09-06 22:34:39 +01004757 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
4758 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04004759}
4760