blob: da6da274dce3315fc6f3730046303c0d7987a044 [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>
Chris Masone02119d2008-09-05 16:13:11 -040023#include "ctree.h"
24#include "transaction.h"
25#include "disk-io.h"
26#include "locking.h"
27#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "backref.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050029#include "tree-log.h"
Mark Fashehf1863732012-08-08 11:32:27 -070030#include "hash.h"
Chris Masone02119d2008-09-05 16:13:11 -040031
32/* magic values for the inode_only field in btrfs_log_inode:
33 *
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
36 * during log replay
37 */
38#define LOG_INODE_ALL 0
39#define LOG_INODE_EXISTS 1
40
41/*
Chris Mason12fcfd22009-03-24 10:24:20 -040042 * directory trouble cases
43 *
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
48 *
49 * mkdir foo/some_dir
50 * normal commit
51 * rename foo/some_dir foo2/some_dir
52 * mkdir foo/some_dir
53 * fsync foo/some_dir/some_file
54 *
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
58 *
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
61 *
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
65 *
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
68 *
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
71 *
72 * mkdir f1/foo
73 * normal commit
74 * rm -rf f1/foo
75 * fsync(f1)
76 *
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
81 * ugly details.
82 */
83
84/*
Chris Masone02119d2008-09-05 16:13:11 -040085 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
89 *
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
92 */
93#define LOG_WALK_PIN_ONLY 0
94#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040095#define LOG_WALK_REPLAY_DIR_INDEX 2
96#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040097
Chris Mason12fcfd22009-03-24 10:24:20 -040098static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040099 struct btrfs_root *root, struct inode *inode,
100 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -0500101static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root,
103 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400104static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root,
106 struct btrfs_root *log,
107 struct btrfs_path *path,
108 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400109
110/*
111 * tree logging is a special write ahead log used to make sure that
112 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 *
114 * Full tree commits are expensive because they require commonly
115 * modified blocks to be recowed, creating many dirty pages in the
116 * extent tree an 4x-6x higher write load than ext3.
117 *
118 * Instead of doing a tree commit on every fsync, we use the
119 * key ranges and transaction ids to find items for a given file or directory
120 * that have changed in this transaction. Those items are copied into
121 * a special tree (one per subvolume root), that tree is written to disk
122 * and then the fsync is considered complete.
123 *
124 * After a crash, items are copied out of the log-tree back into the
125 * subvolume tree. Any file data extents found are recorded in the extent
126 * allocation tree, and the log-tree freed.
127 *
128 * The log tree is read three times, once to pin down all the extents it is
129 * using in ram and once, once to create all the inodes logged in the tree
130 * and once to do all the other items.
131 */
132
133/*
Chris Masone02119d2008-09-05 16:13:11 -0400134 * start a sub transaction and setup the log tree
135 * this increments the log tree writer count to make the people
136 * syncing the tree wait for us to finish
137 */
138static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800139 struct btrfs_root *root,
140 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400141{
Miao Xie8b050d32014-02-20 18:08:58 +0800142 int index;
Chris Masone02119d2008-09-05 16:13:11 -0400143 int ret;
Yan Zheng7237f182009-01-21 12:54:03 -0500144
145 mutex_lock(&root->log_mutex);
146 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400147 if (!root->log_start_pid) {
148 root->log_start_pid = current->pid;
149 root->log_multiple_pids = false;
150 } else if (root->log_start_pid != current->pid) {
151 root->log_multiple_pids = true;
152 }
153
Miao Xie2ecb7922012-09-06 04:04:27 -0600154 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500155 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800156 if (ctx) {
157 index = root->log_transid % 2;
158 list_add_tail(&ctx->list, &root->log_ctxs[index]);
159 }
Yan Zheng7237f182009-01-21 12:54:03 -0500160 mutex_unlock(&root->log_mutex);
161 return 0;
162 }
Miao Xiee87ac132014-02-20 18:08:53 +0800163
164 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400165 mutex_lock(&root->fs_info->tree_log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800166 if (!root->fs_info->log_root_tree)
Chris Masone02119d2008-09-05 16:13:11 -0400167 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Miao Xiee87ac132014-02-20 18:08:53 +0800168 mutex_unlock(&root->fs_info->tree_log_mutex);
169 if (ret)
170 goto out;
171
172 if (!root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400173 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400174 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800175 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400176 }
Miao Xiee87ac132014-02-20 18:08:53 +0800177 root->log_multiple_pids = false;
178 root->log_start_pid = current->pid;
Miao Xie2ecb7922012-09-06 04:04:27 -0600179 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500180 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800181 if (ctx) {
182 index = root->log_transid % 2;
183 list_add_tail(&ctx->list, &root->log_ctxs[index]);
184 }
Miao Xiee87ac132014-02-20 18:08:53 +0800185out:
Yan Zheng7237f182009-01-21 12:54:03 -0500186 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800187 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400188}
189
190/*
191 * returns 0 if there was a log transaction running and we were able
192 * to join, or returns -ENOENT if there were not transactions
193 * in progress
194 */
195static int join_running_log_trans(struct btrfs_root *root)
196{
197 int ret = -ENOENT;
198
199 smp_mb();
200 if (!root->log_root)
201 return -ENOENT;
202
Yan Zheng7237f182009-01-21 12:54:03 -0500203 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400204 if (root->log_root) {
205 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500206 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400207 }
Yan Zheng7237f182009-01-21 12:54:03 -0500208 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400209 return ret;
210}
211
212/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400213 * This either makes the current running log transaction wait
214 * until you call btrfs_end_log_trans() or it makes any future
215 * log transactions wait until you call btrfs_end_log_trans()
216 */
217int btrfs_pin_log_trans(struct btrfs_root *root)
218{
219 int ret = -ENOENT;
220
221 mutex_lock(&root->log_mutex);
222 atomic_inc(&root->log_writers);
223 mutex_unlock(&root->log_mutex);
224 return ret;
225}
226
227/*
Chris Masone02119d2008-09-05 16:13:11 -0400228 * indicate we're done making changes to the log tree
229 * and wake up anyone waiting to do a sync
230 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100231void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400232{
Yan Zheng7237f182009-01-21 12:54:03 -0500233 if (atomic_dec_and_test(&root->log_writers)) {
234 smp_mb();
235 if (waitqueue_active(&root->log_writer_wait))
236 wake_up(&root->log_writer_wait);
237 }
Chris Masone02119d2008-09-05 16:13:11 -0400238}
239
240
241/*
242 * the walk control struct is used to pass state down the chain when
243 * processing the log tree. The stage field tells us which part
244 * of the log tree processing we are currently doing. The others
245 * are state fields used for that specific part
246 */
247struct walk_control {
248 /* should we free the extent on disk when done? This is used
249 * at transaction commit time while freeing a log tree
250 */
251 int free;
252
253 /* should we write out the extent buffer? This is used
254 * while flushing the log tree to disk during a sync
255 */
256 int write;
257
258 /* should we wait for the extent buffer io to finish? Also used
259 * while flushing the log tree to disk for a sync
260 */
261 int wait;
262
263 /* pin only walk, we record which extents on disk belong to the
264 * log trees
265 */
266 int pin;
267
268 /* what stage of the replay code we're currently in */
269 int stage;
270
271 /* the root we are currently replaying */
272 struct btrfs_root *replay_dest;
273
274 /* the trans handle for the current replay */
275 struct btrfs_trans_handle *trans;
276
277 /* the function that gets used to process blocks we find in the
278 * tree. Note the extent_buffer might not be up to date when it is
279 * passed in, and it must be checked or read if you need the data
280 * inside it
281 */
282 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
283 struct walk_control *wc, u64 gen);
284};
285
286/*
287 * process_func used to pin down extents, write them or wait on them
288 */
289static int process_one_buffer(struct btrfs_root *log,
290 struct extent_buffer *eb,
291 struct walk_control *wc, u64 gen)
292{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400293 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400294
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400295 /*
296 * If this fs is mixed then we need to be able to process the leaves to
297 * pin down any logged extents, so we have to read the block.
298 */
299 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
300 ret = btrfs_read_buffer(eb, gen);
301 if (ret)
302 return ret;
303 }
304
Josef Bacikb50c6e22013-04-25 15:55:30 -0400305 if (wc->pin)
306 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
307 eb->start, eb->len);
308
309 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400310 if (wc->pin && btrfs_header_level(eb) == 0)
311 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400312 if (wc->write)
313 btrfs_write_tree_block(eb);
314 if (wc->wait)
315 btrfs_wait_tree_block_writeback(eb);
316 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400317 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400318}
319
320/*
321 * Item overwrite used by replay and tree logging. eb, slot and key all refer
322 * to the src data we are copying out.
323 *
324 * root is the tree we are copying into, and path is a scratch
325 * path for use in this function (it should be released on entry and
326 * will be released on exit).
327 *
328 * If the key is already in the destination tree the existing item is
329 * overwritten. If the existing item isn't big enough, it is extended.
330 * If it is too large, it is truncated.
331 *
332 * If the key isn't in the destination yet, a new item is inserted.
333 */
334static noinline int overwrite_item(struct btrfs_trans_handle *trans,
335 struct btrfs_root *root,
336 struct btrfs_path *path,
337 struct extent_buffer *eb, int slot,
338 struct btrfs_key *key)
339{
340 int ret;
341 u32 item_size;
342 u64 saved_i_size = 0;
343 int save_old_i_size = 0;
344 unsigned long src_ptr;
345 unsigned long dst_ptr;
346 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000347 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400348
349 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
350 overwrite_root = 1;
351
352 item_size = btrfs_item_size_nr(eb, slot);
353 src_ptr = btrfs_item_ptr_offset(eb, slot);
354
355 /* look for the key in the destination tree */
356 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000357 if (ret < 0)
358 return ret;
359
Chris Masone02119d2008-09-05 16:13:11 -0400360 if (ret == 0) {
361 char *src_copy;
362 char *dst_copy;
363 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
364 path->slots[0]);
365 if (dst_size != item_size)
366 goto insert;
367
368 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200369 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400370 return 0;
371 }
372 dst_copy = kmalloc(item_size, GFP_NOFS);
373 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000374 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200375 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000376 kfree(dst_copy);
377 kfree(src_copy);
378 return -ENOMEM;
379 }
Chris Masone02119d2008-09-05 16:13:11 -0400380
381 read_extent_buffer(eb, src_copy, src_ptr, item_size);
382
383 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
384 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
385 item_size);
386 ret = memcmp(dst_copy, src_copy, item_size);
387
388 kfree(dst_copy);
389 kfree(src_copy);
390 /*
391 * they have the same contents, just return, this saves
392 * us from cowing blocks in the destination tree and doing
393 * extra writes that may not have been done by a previous
394 * sync
395 */
396 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200397 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400398 return 0;
399 }
400
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000401 /*
402 * We need to load the old nbytes into the inode so when we
403 * replay the extents we've logged we get the right nbytes.
404 */
405 if (inode_item) {
406 struct btrfs_inode_item *item;
407 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400408 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000409
410 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
411 struct btrfs_inode_item);
412 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
413 item = btrfs_item_ptr(eb, slot,
414 struct btrfs_inode_item);
415 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400416
417 /*
418 * If this is a directory we need to reset the i_size to
419 * 0 so that we can set it up properly when replaying
420 * the rest of the items in this log.
421 */
422 mode = btrfs_inode_mode(eb, item);
423 if (S_ISDIR(mode))
424 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000425 }
426 } else if (inode_item) {
427 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400428 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000429
430 /*
431 * New inode, set nbytes to 0 so that the nbytes comes out
432 * properly when we replay the extents.
433 */
434 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
435 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400436
437 /*
438 * If this is a directory we need to reset the i_size to 0 so
439 * that we can set it up properly when replaying the rest of
440 * the items in this log.
441 */
442 mode = btrfs_inode_mode(eb, item);
443 if (S_ISDIR(mode))
444 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400445 }
446insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200447 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400448 /* try to insert the key into the destination tree */
449 ret = btrfs_insert_empty_item(trans, root, path,
450 key, item_size);
451
452 /* make sure any existing item is the correct size */
453 if (ret == -EEXIST) {
454 u32 found_size;
455 found_size = btrfs_item_size_nr(path->nodes[0],
456 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100457 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000458 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100459 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000460 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100461 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400462 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400463 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400464 }
465 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
466 path->slots[0]);
467
468 /* don't overwrite an existing inode if the generation number
469 * was logged as zero. This is done when the tree logging code
470 * is just logging an inode to make sure it exists after recovery.
471 *
472 * Also, don't overwrite i_size on directories during replay.
473 * log replay inserts and removes directory items based on the
474 * state of the tree found in the subvolume, and i_size is modified
475 * as it goes
476 */
477 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
478 struct btrfs_inode_item *src_item;
479 struct btrfs_inode_item *dst_item;
480
481 src_item = (struct btrfs_inode_item *)src_ptr;
482 dst_item = (struct btrfs_inode_item *)dst_ptr;
483
484 if (btrfs_inode_generation(eb, src_item) == 0)
485 goto no_copy;
486
487 if (overwrite_root &&
488 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
489 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
490 save_old_i_size = 1;
491 saved_i_size = btrfs_inode_size(path->nodes[0],
492 dst_item);
493 }
494 }
495
496 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
497 src_ptr, item_size);
498
499 if (save_old_i_size) {
500 struct btrfs_inode_item *dst_item;
501 dst_item = (struct btrfs_inode_item *)dst_ptr;
502 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
503 }
504
505 /* make sure the generation is filled in */
506 if (key->type == BTRFS_INODE_ITEM_KEY) {
507 struct btrfs_inode_item *dst_item;
508 dst_item = (struct btrfs_inode_item *)dst_ptr;
509 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
510 btrfs_set_inode_generation(path->nodes[0], dst_item,
511 trans->transid);
512 }
513 }
514no_copy:
515 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200516 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400517 return 0;
518}
519
520/*
521 * simple helper to read an inode off the disk from a given root
522 * This can only be called for subvolume roots and not for the log
523 */
524static noinline struct inode *read_one_inode(struct btrfs_root *root,
525 u64 objectid)
526{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400527 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400528 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400529
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400530 key.objectid = objectid;
531 key.type = BTRFS_INODE_ITEM_KEY;
532 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000533 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400534 if (IS_ERR(inode)) {
535 inode = NULL;
536 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400537 iput(inode);
538 inode = NULL;
539 }
540 return inode;
541}
542
543/* replays a single extent in 'eb' at 'slot' with 'key' into the
544 * subvolume 'root'. path is released on entry and should be released
545 * on exit.
546 *
547 * extents in the log tree have not been allocated out of the extent
548 * tree yet. So, this completes the allocation, taking a reference
549 * as required if the extent already exists or creating a new extent
550 * if it isn't in the extent allocation tree yet.
551 *
552 * The extent is inserted into the file, dropping any existing extents
553 * from the file that overlap the new one.
554 */
555static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root,
557 struct btrfs_path *path,
558 struct extent_buffer *eb, int slot,
559 struct btrfs_key *key)
560{
561 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400562 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400563 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000564 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400565 struct btrfs_file_extent_item *item;
566 struct inode *inode = NULL;
567 unsigned long size;
568 int ret = 0;
569
570 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
571 found_type = btrfs_file_extent_type(eb, item);
572
Yan Zhengd899e052008-10-30 14:25:28 -0400573 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000574 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
575 nbytes = btrfs_file_extent_num_bytes(eb, item);
576 extent_end = start + nbytes;
577
578 /*
579 * We don't add to the inodes nbytes if we are prealloc or a
580 * hole.
581 */
582 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
583 nbytes = 0;
584 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800585 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000586 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000587 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400588 } else {
589 ret = 0;
590 goto out;
591 }
592
593 inode = read_one_inode(root, key->objectid);
594 if (!inode) {
595 ret = -EIO;
596 goto out;
597 }
598
599 /*
600 * first check to see if we already have this extent in the
601 * file. This must be done before the btrfs_drop_extents run
602 * so we don't try to drop this extent.
603 */
Li Zefan33345d012011-04-20 10:31:50 +0800604 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400605 start, 0);
606
Yan Zhengd899e052008-10-30 14:25:28 -0400607 if (ret == 0 &&
608 (found_type == BTRFS_FILE_EXTENT_REG ||
609 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400610 struct btrfs_file_extent_item cmp1;
611 struct btrfs_file_extent_item cmp2;
612 struct btrfs_file_extent_item *existing;
613 struct extent_buffer *leaf;
614
615 leaf = path->nodes[0];
616 existing = btrfs_item_ptr(leaf, path->slots[0],
617 struct btrfs_file_extent_item);
618
619 read_extent_buffer(eb, &cmp1, (unsigned long)item,
620 sizeof(cmp1));
621 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
622 sizeof(cmp2));
623
624 /*
625 * we already have a pointer to this exact extent,
626 * we don't have to do anything
627 */
628 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200629 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400630 goto out;
631 }
632 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200633 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400634
635 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400636 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400637 if (ret)
638 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400639
Yan Zheng07d400a2009-01-06 11:42:00 -0500640 if (found_type == BTRFS_FILE_EXTENT_REG ||
641 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400642 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500643 unsigned long dest_offset;
644 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400645
Yan Zheng07d400a2009-01-06 11:42:00 -0500646 ret = btrfs_insert_empty_item(trans, root, path, key,
647 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400648 if (ret)
649 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500650 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
651 path->slots[0]);
652 copy_extent_buffer(path->nodes[0], eb, dest_offset,
653 (unsigned long)item, sizeof(*item));
654
655 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
656 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
657 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500659
660 if (ins.objectid > 0) {
661 u64 csum_start;
662 u64 csum_end;
663 LIST_HEAD(ordered_sums);
664 /*
665 * is this extent already allocated in the extent
666 * allocation tree? If so, just add a reference
667 */
668 ret = btrfs_lookup_extent(root, ins.objectid,
669 ins.offset);
670 if (ret == 0) {
671 ret = btrfs_inc_extent_ref(trans, root,
672 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400673 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200674 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400675 if (ret)
676 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500677 } else {
678 /*
679 * insert the extent pointer in the extent
680 * allocation tree
681 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 ret = btrfs_alloc_logged_file_extent(trans,
683 root, root->root_key.objectid,
684 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400685 if (ret)
686 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500687 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200688 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500689
690 if (btrfs_file_extent_compression(eb, item)) {
691 csum_start = ins.objectid;
692 csum_end = csum_start + ins.offset;
693 } else {
694 csum_start = ins.objectid +
695 btrfs_file_extent_offset(eb, item);
696 csum_end = csum_start +
697 btrfs_file_extent_num_bytes(eb, item);
698 }
699
700 ret = btrfs_lookup_csums_range(root->log_root,
701 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100702 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400703 if (ret)
704 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500705 while (!list_empty(&ordered_sums)) {
706 struct btrfs_ordered_sum *sums;
707 sums = list_entry(ordered_sums.next,
708 struct btrfs_ordered_sum,
709 list);
Josef Bacik36508602013-04-25 16:23:32 -0400710 if (!ret)
711 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500712 root->fs_info->csum_root,
713 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500714 list_del(&sums->list);
715 kfree(sums);
716 }
Josef Bacik36508602013-04-25 16:23:32 -0400717 if (ret)
718 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500719 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200720 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500721 }
722 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
723 /* inline extents are easy, we just overwrite them */
724 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400725 if (ret)
726 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500727 }
728
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000729 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600730 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400731out:
732 if (inode)
733 iput(inode);
734 return ret;
735}
736
737/*
738 * when cleaning up conflicts between the directory names in the
739 * subvolume, directory names in the log and directory names in the
740 * inode back references, we may have to unlink inodes from directories.
741 *
742 * This is a helper function to do the unlink of a specific directory
743 * item
744 */
745static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
746 struct btrfs_root *root,
747 struct btrfs_path *path,
748 struct inode *dir,
749 struct btrfs_dir_item *di)
750{
751 struct inode *inode;
752 char *name;
753 int name_len;
754 struct extent_buffer *leaf;
755 struct btrfs_key location;
756 int ret;
757
758 leaf = path->nodes[0];
759
760 btrfs_dir_item_key_to_cpu(leaf, di, &location);
761 name_len = btrfs_dir_name_len(leaf, di);
762 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000763 if (!name)
764 return -ENOMEM;
765
Chris Masone02119d2008-09-05 16:13:11 -0400766 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200767 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400768
769 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000770 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400771 ret = -EIO;
772 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000773 }
Chris Masone02119d2008-09-05 16:13:11 -0400774
Yan Zhengec051c02009-01-05 15:43:42 -0500775 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400776 if (ret)
777 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400778
Chris Masone02119d2008-09-05 16:13:11 -0400779 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400780 if (ret)
781 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100782 else
783 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400784out:
785 kfree(name);
786 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400787 return ret;
788}
789
790/*
791 * helper function to see if a given name and sequence number found
792 * in an inode back reference are already in a directory and correctly
793 * point to this inode
794 */
795static noinline int inode_in_dir(struct btrfs_root *root,
796 struct btrfs_path *path,
797 u64 dirid, u64 objectid, u64 index,
798 const char *name, int name_len)
799{
800 struct btrfs_dir_item *di;
801 struct btrfs_key location;
802 int match = 0;
803
804 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
805 index, name, name_len, 0);
806 if (di && !IS_ERR(di)) {
807 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
808 if (location.objectid != objectid)
809 goto out;
810 } else
811 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200812 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400813
814 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
815 if (di && !IS_ERR(di)) {
816 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
817 if (location.objectid != objectid)
818 goto out;
819 } else
820 goto out;
821 match = 1;
822out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200823 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400824 return match;
825}
826
827/*
828 * helper function to check a log tree for a named back reference in
829 * an inode. This is used to decide if a back reference that is
830 * found in the subvolume conflicts with what we find in the log.
831 *
832 * inode backreferences may have multiple refs in a single item,
833 * during replay we process one reference at a time, and we don't
834 * want to delete valid links to a file from the subvolume if that
835 * link is also in the log.
836 */
837static noinline int backref_in_log(struct btrfs_root *log,
838 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700839 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400840 char *name, int namelen)
841{
842 struct btrfs_path *path;
843 struct btrfs_inode_ref *ref;
844 unsigned long ptr;
845 unsigned long ptr_end;
846 unsigned long name_ptr;
847 int found_name_len;
848 int item_size;
849 int ret;
850 int match = 0;
851
852 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000853 if (!path)
854 return -ENOMEM;
855
Chris Masone02119d2008-09-05 16:13:11 -0400856 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
857 if (ret != 0)
858 goto out;
859
Chris Masone02119d2008-09-05 16:13:11 -0400860 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700861
862 if (key->type == BTRFS_INODE_EXTREF_KEY) {
863 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
864 name, namelen, NULL))
865 match = 1;
866
867 goto out;
868 }
869
870 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400871 ptr_end = ptr + item_size;
872 while (ptr < ptr_end) {
873 ref = (struct btrfs_inode_ref *)ptr;
874 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
875 if (found_name_len == namelen) {
876 name_ptr = (unsigned long)(ref + 1);
877 ret = memcmp_extent_buffer(path->nodes[0], name,
878 name_ptr, namelen);
879 if (ret == 0) {
880 match = 1;
881 goto out;
882 }
883 }
884 ptr = (unsigned long)(ref + 1) + found_name_len;
885 }
886out:
887 btrfs_free_path(path);
888 return match;
889}
890
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700891static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
892 struct btrfs_root *root,
893 struct btrfs_path *path,
894 struct btrfs_root *log_root,
895 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700896 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700897 u64 inode_objectid, u64 parent_objectid,
898 u64 ref_index, char *name, int namelen,
899 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700900{
901 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700902 char *victim_name;
903 int victim_name_len;
904 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700905 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700906 struct btrfs_key search_key;
907 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700908
Mark Fashehf1863732012-08-08 11:32:27 -0700909again:
910 /* Search old style refs */
911 search_key.objectid = inode_objectid;
912 search_key.type = BTRFS_INODE_REF_KEY;
913 search_key.offset = parent_objectid;
914 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700915 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700916 struct btrfs_inode_ref *victim_ref;
917 unsigned long ptr;
918 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700919
920 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700921
922 /* are we trying to overwrite a back ref for the root directory
923 * if so, just jump out, we're done
924 */
Mark Fashehf1863732012-08-08 11:32:27 -0700925 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700926 return 1;
927
928 /* check all the names in this back reference to see
929 * if they are in the log. if so, we allow them to stay
930 * otherwise they must be unlinked as a conflict
931 */
932 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
933 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
934 while (ptr < ptr_end) {
935 victim_ref = (struct btrfs_inode_ref *)ptr;
936 victim_name_len = btrfs_inode_ref_name_len(leaf,
937 victim_ref);
938 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400939 if (!victim_name)
940 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700941
942 read_extent_buffer(leaf, victim_name,
943 (unsigned long)(victim_ref + 1),
944 victim_name_len);
945
Mark Fashehf1863732012-08-08 11:32:27 -0700946 if (!backref_in_log(log_root, &search_key,
947 parent_objectid,
948 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700949 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700950 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700951 btrfs_release_path(path);
952
953 ret = btrfs_unlink_inode(trans, root, dir,
954 inode, victim_name,
955 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700956 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400957 if (ret)
958 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100959 ret = btrfs_run_delayed_items(trans, root);
960 if (ret)
961 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700962 *search_done = 1;
963 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700964 }
965 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700966
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700967 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
968 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700969
970 /*
971 * NOTE: we have searched root tree and checked the
972 * coresponding ref, it does not need to check again.
973 */
974 *search_done = 1;
975 }
976 btrfs_release_path(path);
977
Mark Fashehf1863732012-08-08 11:32:27 -0700978 /* Same search but for extended refs */
979 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
980 inode_objectid, parent_objectid, 0,
981 0);
982 if (!IS_ERR_OR_NULL(extref)) {
983 u32 item_size;
984 u32 cur_offset = 0;
985 unsigned long base;
986 struct inode *victim_parent;
987
988 leaf = path->nodes[0];
989
990 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
991 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
992
993 while (cur_offset < item_size) {
994 extref = (struct btrfs_inode_extref *)base + cur_offset;
995
996 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
997
998 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
999 goto next;
1000
1001 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001002 if (!victim_name)
1003 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001004 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1005 victim_name_len);
1006
1007 search_key.objectid = inode_objectid;
1008 search_key.type = BTRFS_INODE_EXTREF_KEY;
1009 search_key.offset = btrfs_extref_hash(parent_objectid,
1010 victim_name,
1011 victim_name_len);
1012 ret = 0;
1013 if (!backref_in_log(log_root, &search_key,
1014 parent_objectid, victim_name,
1015 victim_name_len)) {
1016 ret = -ENOENT;
1017 victim_parent = read_one_inode(root,
1018 parent_objectid);
1019 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001020 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001021 btrfs_release_path(path);
1022
1023 ret = btrfs_unlink_inode(trans, root,
1024 victim_parent,
1025 inode,
1026 victim_name,
1027 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001028 if (!ret)
1029 ret = btrfs_run_delayed_items(
1030 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001031 }
Mark Fashehf1863732012-08-08 11:32:27 -07001032 iput(victim_parent);
1033 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001034 if (ret)
1035 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001036 *search_done = 1;
1037 goto again;
1038 }
1039 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001040 if (ret)
1041 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001042next:
1043 cur_offset += victim_name_len + sizeof(*extref);
1044 }
1045 *search_done = 1;
1046 }
1047 btrfs_release_path(path);
1048
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001049 /* look for a conflicting sequence number */
1050 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001051 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001052 if (di && !IS_ERR(di)) {
1053 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001054 if (ret)
1055 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001056 }
1057 btrfs_release_path(path);
1058
1059 /* look for a conflicing name */
1060 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1061 name, namelen, 0);
1062 if (di && !IS_ERR(di)) {
1063 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001064 if (ret)
1065 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001066 }
1067 btrfs_release_path(path);
1068
1069 return 0;
1070}
Chris Masone02119d2008-09-05 16:13:11 -04001071
Mark Fashehf1863732012-08-08 11:32:27 -07001072static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1073 u32 *namelen, char **name, u64 *index,
1074 u64 *parent_objectid)
1075{
1076 struct btrfs_inode_extref *extref;
1077
1078 extref = (struct btrfs_inode_extref *)ref_ptr;
1079
1080 *namelen = btrfs_inode_extref_name_len(eb, extref);
1081 *name = kmalloc(*namelen, GFP_NOFS);
1082 if (*name == NULL)
1083 return -ENOMEM;
1084
1085 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1086 *namelen);
1087
1088 *index = btrfs_inode_extref_index(eb, extref);
1089 if (parent_objectid)
1090 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1091
1092 return 0;
1093}
1094
1095static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1096 u32 *namelen, char **name, u64 *index)
1097{
1098 struct btrfs_inode_ref *ref;
1099
1100 ref = (struct btrfs_inode_ref *)ref_ptr;
1101
1102 *namelen = btrfs_inode_ref_name_len(eb, ref);
1103 *name = kmalloc(*namelen, GFP_NOFS);
1104 if (*name == NULL)
1105 return -ENOMEM;
1106
1107 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1108
1109 *index = btrfs_inode_ref_index(eb, ref);
1110
1111 return 0;
1112}
1113
Chris Masone02119d2008-09-05 16:13:11 -04001114/*
1115 * replay one inode back reference item found in the log tree.
1116 * eb, slot and key refer to the buffer and key found in the log tree.
1117 * root is the destination we are replaying into, and path is for temp
1118 * use by this function. (it should be released on return).
1119 */
1120static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1121 struct btrfs_root *root,
1122 struct btrfs_root *log,
1123 struct btrfs_path *path,
1124 struct extent_buffer *eb, int slot,
1125 struct btrfs_key *key)
1126{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001127 struct inode *dir = NULL;
1128 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001129 unsigned long ref_ptr;
1130 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001131 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001132 int namelen;
1133 int ret;
liuboc622ae62011-03-26 08:01:12 -04001134 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001135 int log_ref_ver = 0;
1136 u64 parent_objectid;
1137 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001138 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001139 int ref_struct_size;
1140
1141 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1142 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1143
1144 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1145 struct btrfs_inode_extref *r;
1146
1147 ref_struct_size = sizeof(struct btrfs_inode_extref);
1148 log_ref_ver = 1;
1149 r = (struct btrfs_inode_extref *)ref_ptr;
1150 parent_objectid = btrfs_inode_extref_parent(eb, r);
1151 } else {
1152 ref_struct_size = sizeof(struct btrfs_inode_ref);
1153 parent_objectid = key->offset;
1154 }
1155 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001156
Chris Masone02119d2008-09-05 16:13:11 -04001157 /*
1158 * it is possible that we didn't log all the parent directories
1159 * for a given inode. If we don't find the dir, just don't
1160 * copy the back ref in. The link count fixup code will take
1161 * care of the rest
1162 */
Mark Fashehf1863732012-08-08 11:32:27 -07001163 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001164 if (!dir) {
1165 ret = -ENOENT;
1166 goto out;
1167 }
Chris Masone02119d2008-09-05 16:13:11 -04001168
Mark Fashehf1863732012-08-08 11:32:27 -07001169 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001170 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001171 ret = -EIO;
1172 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001173 }
Chris Masone02119d2008-09-05 16:13:11 -04001174
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001175 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001176 if (log_ref_ver) {
1177 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1178 &ref_index, &parent_objectid);
1179 /*
1180 * parent object can change from one array
1181 * item to another.
1182 */
1183 if (!dir)
1184 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 }
Mark Fashehf1863732012-08-08 11:32:27 -07001189 } else {
1190 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1191 &ref_index);
1192 }
1193 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001194 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001195
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001196 /* if we already have a perfect match, we're done */
1197 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001198 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001199 /*
1200 * look for a conflicting back reference in the
1201 * metadata. if we find one we have to unlink that name
1202 * of the file before we add our new link. Later on, we
1203 * overwrite any existing back reference, and we don't
1204 * want to create dangling pointers in the directory.
1205 */
Chris Masone02119d2008-09-05 16:13:11 -04001206
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001207 if (!search_done) {
1208 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001209 dir, inode, eb,
1210 inode_objectid,
1211 parent_objectid,
1212 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001213 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001214 if (ret) {
1215 if (ret == 1)
1216 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001217 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001218 }
Chris Masone02119d2008-09-05 16:13:11 -04001219 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001220
1221 /* insert our name */
1222 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001223 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001224 if (ret)
1225 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001226
1227 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001228 }
liuboc622ae62011-03-26 08:01:12 -04001229
Mark Fashehf1863732012-08-08 11:32:27 -07001230 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001231 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001232 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001233 if (log_ref_ver) {
1234 iput(dir);
1235 dir = NULL;
1236 }
Chris Masone02119d2008-09-05 16:13:11 -04001237 }
Chris Masone02119d2008-09-05 16:13:11 -04001238
1239 /* finally write the back reference in the inode */
1240 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001241out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001242 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001243 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001244 iput(dir);
1245 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001246 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001247}
1248
Yan, Zhengc71bf092009-11-12 09:34:40 +00001249static int insert_orphan_item(struct btrfs_trans_handle *trans,
1250 struct btrfs_root *root, u64 offset)
1251{
1252 int ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08001253 ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
1254 offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001255 if (ret > 0)
1256 ret = btrfs_insert_orphan_item(trans, root, offset);
1257 return ret;
1258}
1259
Mark Fashehf1863732012-08-08 11:32:27 -07001260static int count_inode_extrefs(struct btrfs_root *root,
1261 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001262{
Mark Fashehf1863732012-08-08 11:32:27 -07001263 int ret = 0;
1264 int name_len;
1265 unsigned int nlink = 0;
1266 u32 item_size;
1267 u32 cur_offset = 0;
1268 u64 inode_objectid = btrfs_ino(inode);
1269 u64 offset = 0;
1270 unsigned long ptr;
1271 struct btrfs_inode_extref *extref;
1272 struct extent_buffer *leaf;
1273
1274 while (1) {
1275 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1276 &extref, &offset);
1277 if (ret)
1278 break;
1279
1280 leaf = path->nodes[0];
1281 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1282 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1283
1284 while (cur_offset < item_size) {
1285 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1286 name_len = btrfs_inode_extref_name_len(leaf, extref);
1287
1288 nlink++;
1289
1290 cur_offset += name_len + sizeof(*extref);
1291 }
1292
1293 offset++;
1294 btrfs_release_path(path);
1295 }
1296 btrfs_release_path(path);
1297
1298 if (ret < 0)
1299 return ret;
1300 return nlink;
1301}
1302
1303static int count_inode_refs(struct btrfs_root *root,
1304 struct inode *inode, struct btrfs_path *path)
1305{
Chris Masone02119d2008-09-05 16:13:11 -04001306 int ret;
1307 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001308 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001309 unsigned long ptr;
1310 unsigned long ptr_end;
1311 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001312 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001313
Li Zefan33345d012011-04-20 10:31:50 +08001314 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001315 key.type = BTRFS_INODE_REF_KEY;
1316 key.offset = (u64)-1;
1317
Chris Masond3977122009-01-05 21:25:51 -05001318 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001319 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1320 if (ret < 0)
1321 break;
1322 if (ret > 0) {
1323 if (path->slots[0] == 0)
1324 break;
1325 path->slots[0]--;
1326 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001327process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001328 btrfs_item_key_to_cpu(path->nodes[0], &key,
1329 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001330 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001331 key.type != BTRFS_INODE_REF_KEY)
1332 break;
1333 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1334 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1335 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001336 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001337 struct btrfs_inode_ref *ref;
1338
1339 ref = (struct btrfs_inode_ref *)ptr;
1340 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1341 ref);
1342 ptr = (unsigned long)(ref + 1) + name_len;
1343 nlink++;
1344 }
1345
1346 if (key.offset == 0)
1347 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001348 if (path->slots[0] > 0) {
1349 path->slots[0]--;
1350 goto process_slot;
1351 }
Chris Masone02119d2008-09-05 16:13:11 -04001352 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001353 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001354 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001355 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001356
1357 return nlink;
1358}
1359
1360/*
1361 * There are a few corners where the link count of the file can't
1362 * be properly maintained during replay. So, instead of adding
1363 * lots of complexity to the log code, we just scan the backrefs
1364 * for any file that has been through replay.
1365 *
1366 * The scan will update the link count on the inode to reflect the
1367 * number of back refs found. If it goes down to zero, the iput
1368 * will free the inode.
1369 */
1370static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1371 struct btrfs_root *root,
1372 struct inode *inode)
1373{
1374 struct btrfs_path *path;
1375 int ret;
1376 u64 nlink = 0;
1377 u64 ino = btrfs_ino(inode);
1378
1379 path = btrfs_alloc_path();
1380 if (!path)
1381 return -ENOMEM;
1382
1383 ret = count_inode_refs(root, inode, path);
1384 if (ret < 0)
1385 goto out;
1386
1387 nlink = ret;
1388
1389 ret = count_inode_extrefs(root, inode, path);
1390 if (ret == -ENOENT)
1391 ret = 0;
1392
1393 if (ret < 0)
1394 goto out;
1395
1396 nlink += ret;
1397
1398 ret = 0;
1399
Chris Masone02119d2008-09-05 16:13:11 -04001400 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001401 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001402 btrfs_update_inode(trans, root, inode);
1403 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001404 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001405
Yan, Zhengc71bf092009-11-12 09:34:40 +00001406 if (inode->i_nlink == 0) {
1407 if (S_ISDIR(inode->i_mode)) {
1408 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001409 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001410 if (ret)
1411 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001412 }
Li Zefan33345d012011-04-20 10:31:50 +08001413 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001414 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001415
Mark Fashehf1863732012-08-08 11:32:27 -07001416out:
1417 btrfs_free_path(path);
1418 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001419}
1420
1421static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1422 struct btrfs_root *root,
1423 struct btrfs_path *path)
1424{
1425 int ret;
1426 struct btrfs_key key;
1427 struct inode *inode;
1428
1429 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1430 key.type = BTRFS_ORPHAN_ITEM_KEY;
1431 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001432 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001433 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1434 if (ret < 0)
1435 break;
1436
1437 if (ret == 1) {
1438 if (path->slots[0] == 0)
1439 break;
1440 path->slots[0]--;
1441 }
1442
1443 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1444 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1445 key.type != BTRFS_ORPHAN_ITEM_KEY)
1446 break;
1447
1448 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001449 if (ret)
1450 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001451
David Sterbab3b4aa72011-04-21 01:20:15 +02001452 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001453 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001454 if (!inode)
1455 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001456
1457 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001458 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001459 if (ret)
1460 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001461
Chris Mason12fcfd22009-03-24 10:24:20 -04001462 /*
1463 * fixup on a directory may create new entries,
1464 * make sure we always look for the highset possible
1465 * offset
1466 */
1467 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001468 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001469 ret = 0;
1470out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001471 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001472 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001473}
1474
1475
1476/*
1477 * record a given inode in the fixup dir so we can check its link
1478 * count when replay is done. The link count is incremented here
1479 * so the inode won't go away until we check it
1480 */
1481static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root,
1483 struct btrfs_path *path,
1484 u64 objectid)
1485{
1486 struct btrfs_key key;
1487 int ret = 0;
1488 struct inode *inode;
1489
1490 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001491 if (!inode)
1492 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001493
1494 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1495 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1496 key.offset = objectid;
1497
1498 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1499
David Sterbab3b4aa72011-04-21 01:20:15 +02001500 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001501 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001502 if (!inode->i_nlink)
1503 set_nlink(inode, 1);
1504 else
Zach Brown8b558c52013-10-16 12:10:34 -07001505 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001506 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001507 } else if (ret == -EEXIST) {
1508 ret = 0;
1509 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001510 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001511 }
1512 iput(inode);
1513
1514 return ret;
1515}
1516
1517/*
1518 * when replaying the log for a directory, we only insert names
1519 * for inodes that actually exist. This means an fsync on a directory
1520 * does not implicitly fsync all the new files in it
1521 */
1522static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1523 struct btrfs_root *root,
1524 struct btrfs_path *path,
1525 u64 dirid, u64 index,
1526 char *name, int name_len, u8 type,
1527 struct btrfs_key *location)
1528{
1529 struct inode *inode;
1530 struct inode *dir;
1531 int ret;
1532
1533 inode = read_one_inode(root, location->objectid);
1534 if (!inode)
1535 return -ENOENT;
1536
1537 dir = read_one_inode(root, dirid);
1538 if (!dir) {
1539 iput(inode);
1540 return -EIO;
1541 }
Josef Bacikd5554382013-09-11 14:17:00 -04001542
Chris Masone02119d2008-09-05 16:13:11 -04001543 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1544
1545 /* FIXME, put inode into FIXUP list */
1546
1547 iput(inode);
1548 iput(dir);
1549 return ret;
1550}
1551
1552/*
1553 * take a single entry in a log directory item and replay it into
1554 * the subvolume.
1555 *
1556 * if a conflicting item exists in the subdirectory already,
1557 * the inode it points to is unlinked and put into the link count
1558 * fix up tree.
1559 *
1560 * If a name from the log points to a file or directory that does
1561 * not exist in the FS, it is skipped. fsyncs on directories
1562 * do not force down inodes inside that directory, just changes to the
1563 * names or unlinks in a directory.
1564 */
1565static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1566 struct btrfs_root *root,
1567 struct btrfs_path *path,
1568 struct extent_buffer *eb,
1569 struct btrfs_dir_item *di,
1570 struct btrfs_key *key)
1571{
1572 char *name;
1573 int name_len;
1574 struct btrfs_dir_item *dst_di;
1575 struct btrfs_key found_key;
1576 struct btrfs_key log_key;
1577 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001578 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001579 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001580 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001581 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001582
1583 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001584 if (!dir)
1585 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001586
1587 name_len = btrfs_dir_name_len(eb, di);
1588 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001589 if (!name) {
1590 ret = -ENOMEM;
1591 goto out;
1592 }
liubo2a29edc2011-01-26 06:22:08 +00001593
Chris Masone02119d2008-09-05 16:13:11 -04001594 log_type = btrfs_dir_type(eb, di);
1595 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1596 name_len);
1597
1598 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001599 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1600 if (exists == 0)
1601 exists = 1;
1602 else
1603 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001604 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001605
Chris Masone02119d2008-09-05 16:13:11 -04001606 if (key->type == BTRFS_DIR_ITEM_KEY) {
1607 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1608 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001609 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001610 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1611 key->objectid,
1612 key->offset, name,
1613 name_len, 1);
1614 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001615 /* Corruption */
1616 ret = -EINVAL;
1617 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001618 }
David Sterbac7040052011-04-19 18:00:01 +02001619 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001620 /* we need a sequence number to insert, so we only
1621 * do inserts for the BTRFS_DIR_INDEX_KEY types
1622 */
1623 if (key->type != BTRFS_DIR_INDEX_KEY)
1624 goto out;
1625 goto insert;
1626 }
1627
1628 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1629 /* the existing item matches the logged item */
1630 if (found_key.objectid == log_key.objectid &&
1631 found_key.type == log_key.type &&
1632 found_key.offset == log_key.offset &&
1633 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1634 goto out;
1635 }
1636
1637 /*
1638 * don't drop the conflicting directory entry if the inode
1639 * for the new entry doesn't exist
1640 */
Chris Mason4bef0842008-09-08 11:18:08 -04001641 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001642 goto out;
1643
Chris Masone02119d2008-09-05 16:13:11 -04001644 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001645 if (ret)
1646 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001647
1648 if (key->type == BTRFS_DIR_INDEX_KEY)
1649 goto insert;
1650out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001651 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001652 if (!ret && update_size) {
1653 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1654 ret = btrfs_update_inode(trans, root, dir);
1655 }
Chris Masone02119d2008-09-05 16:13:11 -04001656 kfree(name);
1657 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001658 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001659
1660insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001661 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001662 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1663 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001664 if (ret && ret != -ENOENT)
1665 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001666 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001667 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001668 goto out;
1669}
1670
1671/*
1672 * find all the names in a directory item and reconcile them into
1673 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1674 * one name in a directory item, but the same code gets used for
1675 * both directory index types
1676 */
1677static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1678 struct btrfs_root *root,
1679 struct btrfs_path *path,
1680 struct extent_buffer *eb, int slot,
1681 struct btrfs_key *key)
1682{
1683 int ret;
1684 u32 item_size = btrfs_item_size_nr(eb, slot);
1685 struct btrfs_dir_item *di;
1686 int name_len;
1687 unsigned long ptr;
1688 unsigned long ptr_end;
1689
1690 ptr = btrfs_item_ptr_offset(eb, slot);
1691 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001692 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001693 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001694 if (verify_dir_item(root, eb, di))
1695 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001696 name_len = btrfs_dir_name_len(eb, di);
1697 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001698 if (ret)
1699 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001700 ptr = (unsigned long)(di + 1);
1701 ptr += name_len;
1702 }
1703 return 0;
1704}
1705
1706/*
1707 * directory replay has two parts. There are the standard directory
1708 * items in the log copied from the subvolume, and range items
1709 * created in the log while the subvolume was logged.
1710 *
1711 * The range items tell us which parts of the key space the log
1712 * is authoritative for. During replay, if a key in the subvolume
1713 * directory is in a logged range item, but not actually in the log
1714 * that means it was deleted from the directory before the fsync
1715 * and should be removed.
1716 */
1717static noinline int find_dir_range(struct btrfs_root *root,
1718 struct btrfs_path *path,
1719 u64 dirid, int key_type,
1720 u64 *start_ret, u64 *end_ret)
1721{
1722 struct btrfs_key key;
1723 u64 found_end;
1724 struct btrfs_dir_log_item *item;
1725 int ret;
1726 int nritems;
1727
1728 if (*start_ret == (u64)-1)
1729 return 1;
1730
1731 key.objectid = dirid;
1732 key.type = key_type;
1733 key.offset = *start_ret;
1734
1735 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1736 if (ret < 0)
1737 goto out;
1738 if (ret > 0) {
1739 if (path->slots[0] == 0)
1740 goto out;
1741 path->slots[0]--;
1742 }
1743 if (ret != 0)
1744 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1745
1746 if (key.type != key_type || key.objectid != dirid) {
1747 ret = 1;
1748 goto next;
1749 }
1750 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1751 struct btrfs_dir_log_item);
1752 found_end = btrfs_dir_log_end(path->nodes[0], item);
1753
1754 if (*start_ret >= key.offset && *start_ret <= found_end) {
1755 ret = 0;
1756 *start_ret = key.offset;
1757 *end_ret = found_end;
1758 goto out;
1759 }
1760 ret = 1;
1761next:
1762 /* check the next slot in the tree to see if it is a valid item */
1763 nritems = btrfs_header_nritems(path->nodes[0]);
1764 if (path->slots[0] >= nritems) {
1765 ret = btrfs_next_leaf(root, path);
1766 if (ret)
1767 goto out;
1768 } else {
1769 path->slots[0]++;
1770 }
1771
1772 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1773
1774 if (key.type != key_type || key.objectid != dirid) {
1775 ret = 1;
1776 goto out;
1777 }
1778 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1779 struct btrfs_dir_log_item);
1780 found_end = btrfs_dir_log_end(path->nodes[0], item);
1781 *start_ret = key.offset;
1782 *end_ret = found_end;
1783 ret = 0;
1784out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001785 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001786 return ret;
1787}
1788
1789/*
1790 * this looks for a given directory item in the log. If the directory
1791 * item is not in the log, the item is removed and the inode it points
1792 * to is unlinked
1793 */
1794static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1795 struct btrfs_root *root,
1796 struct btrfs_root *log,
1797 struct btrfs_path *path,
1798 struct btrfs_path *log_path,
1799 struct inode *dir,
1800 struct btrfs_key *dir_key)
1801{
1802 int ret;
1803 struct extent_buffer *eb;
1804 int slot;
1805 u32 item_size;
1806 struct btrfs_dir_item *di;
1807 struct btrfs_dir_item *log_di;
1808 int name_len;
1809 unsigned long ptr;
1810 unsigned long ptr_end;
1811 char *name;
1812 struct inode *inode;
1813 struct btrfs_key location;
1814
1815again:
1816 eb = path->nodes[0];
1817 slot = path->slots[0];
1818 item_size = btrfs_item_size_nr(eb, slot);
1819 ptr = btrfs_item_ptr_offset(eb, slot);
1820 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001821 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001822 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001823 if (verify_dir_item(root, eb, di)) {
1824 ret = -EIO;
1825 goto out;
1826 }
1827
Chris Masone02119d2008-09-05 16:13:11 -04001828 name_len = btrfs_dir_name_len(eb, di);
1829 name = kmalloc(name_len, GFP_NOFS);
1830 if (!name) {
1831 ret = -ENOMEM;
1832 goto out;
1833 }
1834 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1835 name_len);
1836 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001837 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001838 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1839 dir_key->objectid,
1840 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001841 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001842 log_di = btrfs_lookup_dir_index_item(trans, log,
1843 log_path,
1844 dir_key->objectid,
1845 dir_key->offset,
1846 name, name_len, 0);
1847 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001848 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001849 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001850 btrfs_release_path(path);
1851 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001852 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001853 if (!inode) {
1854 kfree(name);
1855 return -EIO;
1856 }
Chris Masone02119d2008-09-05 16:13:11 -04001857
1858 ret = link_to_fixup_dir(trans, root,
1859 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001860 if (ret) {
1861 kfree(name);
1862 iput(inode);
1863 goto out;
1864 }
1865
Zach Brown8b558c52013-10-16 12:10:34 -07001866 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001867 ret = btrfs_unlink_inode(trans, root, dir, inode,
1868 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001869 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001870 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001871 kfree(name);
1872 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001873 if (ret)
1874 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001875
1876 /* there might still be more names under this key
1877 * check and repeat if required
1878 */
1879 ret = btrfs_search_slot(NULL, root, dir_key, path,
1880 0, 0);
1881 if (ret == 0)
1882 goto again;
1883 ret = 0;
1884 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001885 } else if (IS_ERR(log_di)) {
1886 kfree(name);
1887 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001888 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001889 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001890 kfree(name);
1891
1892 ptr = (unsigned long)(di + 1);
1893 ptr += name_len;
1894 }
1895 ret = 0;
1896out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001897 btrfs_release_path(path);
1898 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001899 return ret;
1900}
1901
1902/*
1903 * deletion replay happens before we copy any new directory items
1904 * out of the log or out of backreferences from inodes. It
1905 * scans the log to find ranges of keys that log is authoritative for,
1906 * and then scans the directory to find items in those ranges that are
1907 * not present in the log.
1908 *
1909 * Anything we don't find in the log is unlinked and removed from the
1910 * directory.
1911 */
1912static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1913 struct btrfs_root *root,
1914 struct btrfs_root *log,
1915 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001916 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001917{
1918 u64 range_start;
1919 u64 range_end;
1920 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1921 int ret = 0;
1922 struct btrfs_key dir_key;
1923 struct btrfs_key found_key;
1924 struct btrfs_path *log_path;
1925 struct inode *dir;
1926
1927 dir_key.objectid = dirid;
1928 dir_key.type = BTRFS_DIR_ITEM_KEY;
1929 log_path = btrfs_alloc_path();
1930 if (!log_path)
1931 return -ENOMEM;
1932
1933 dir = read_one_inode(root, dirid);
1934 /* it isn't an error if the inode isn't there, that can happen
1935 * because we replay the deletes before we copy in the inode item
1936 * from the log
1937 */
1938 if (!dir) {
1939 btrfs_free_path(log_path);
1940 return 0;
1941 }
1942again:
1943 range_start = 0;
1944 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001945 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001946 if (del_all)
1947 range_end = (u64)-1;
1948 else {
1949 ret = find_dir_range(log, path, dirid, key_type,
1950 &range_start, &range_end);
1951 if (ret != 0)
1952 break;
1953 }
Chris Masone02119d2008-09-05 16:13:11 -04001954
1955 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001956 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001957 int nritems;
1958 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1959 0, 0);
1960 if (ret < 0)
1961 goto out;
1962
1963 nritems = btrfs_header_nritems(path->nodes[0]);
1964 if (path->slots[0] >= nritems) {
1965 ret = btrfs_next_leaf(root, path);
1966 if (ret)
1967 break;
1968 }
1969 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1970 path->slots[0]);
1971 if (found_key.objectid != dirid ||
1972 found_key.type != dir_key.type)
1973 goto next_type;
1974
1975 if (found_key.offset > range_end)
1976 break;
1977
1978 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001979 log_path, dir,
1980 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001981 if (ret)
1982 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001983 if (found_key.offset == (u64)-1)
1984 break;
1985 dir_key.offset = found_key.offset + 1;
1986 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001987 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001988 if (range_end == (u64)-1)
1989 break;
1990 range_start = range_end + 1;
1991 }
1992
1993next_type:
1994 ret = 0;
1995 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1996 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1997 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001998 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001999 goto again;
2000 }
2001out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002002 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002003 btrfs_free_path(log_path);
2004 iput(dir);
2005 return ret;
2006}
2007
2008/*
2009 * the process_func used to replay items from the log tree. This
2010 * gets called in two different stages. The first stage just looks
2011 * for inodes and makes sure they are all copied into the subvolume.
2012 *
2013 * The second stage copies all the other item types from the log into
2014 * the subvolume. The two stage approach is slower, but gets rid of
2015 * lots of complexity around inodes referencing other inodes that exist
2016 * only in the log (references come from either directory items or inode
2017 * back refs).
2018 */
2019static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2020 struct walk_control *wc, u64 gen)
2021{
2022 int nritems;
2023 struct btrfs_path *path;
2024 struct btrfs_root *root = wc->replay_dest;
2025 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002026 int level;
2027 int i;
2028 int ret;
2029
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002030 ret = btrfs_read_buffer(eb, gen);
2031 if (ret)
2032 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002033
2034 level = btrfs_header_level(eb);
2035
2036 if (level != 0)
2037 return 0;
2038
2039 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002040 if (!path)
2041 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002042
2043 nritems = btrfs_header_nritems(eb);
2044 for (i = 0; i < nritems; i++) {
2045 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002046
2047 /* inode keys are done during the first stage */
2048 if (key.type == BTRFS_INODE_ITEM_KEY &&
2049 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002050 struct btrfs_inode_item *inode_item;
2051 u32 mode;
2052
2053 inode_item = btrfs_item_ptr(eb, i,
2054 struct btrfs_inode_item);
2055 mode = btrfs_inode_mode(eb, inode_item);
2056 if (S_ISDIR(mode)) {
2057 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002058 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002059 if (ret)
2060 break;
Chris Masone02119d2008-09-05 16:13:11 -04002061 }
2062 ret = overwrite_item(wc->trans, root, path,
2063 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002064 if (ret)
2065 break;
Chris Masone02119d2008-09-05 16:13:11 -04002066
Yan, Zhengc71bf092009-11-12 09:34:40 +00002067 /* for regular files, make sure corresponding
2068 * orhpan item exist. extents past the new EOF
2069 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002070 */
2071 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002072 ret = insert_orphan_item(wc->trans, root,
2073 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002074 if (ret)
2075 break;
Chris Masone02119d2008-09-05 16:13:11 -04002076 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002077
Chris Masone02119d2008-09-05 16:13:11 -04002078 ret = link_to_fixup_dir(wc->trans, root,
2079 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002080 if (ret)
2081 break;
Chris Masone02119d2008-09-05 16:13:11 -04002082 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002083
2084 if (key.type == BTRFS_DIR_INDEX_KEY &&
2085 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2086 ret = replay_one_dir_item(wc->trans, root, path,
2087 eb, i, &key);
2088 if (ret)
2089 break;
2090 }
2091
Chris Masone02119d2008-09-05 16:13:11 -04002092 if (wc->stage < LOG_WALK_REPLAY_ALL)
2093 continue;
2094
2095 /* these keys are simply copied */
2096 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2097 ret = overwrite_item(wc->trans, root, path,
2098 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002099 if (ret)
2100 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002101 } else if (key.type == BTRFS_INODE_REF_KEY ||
2102 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002103 ret = add_inode_ref(wc->trans, root, log, path,
2104 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002105 if (ret && ret != -ENOENT)
2106 break;
2107 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002108 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2109 ret = replay_one_extent(wc->trans, root, path,
2110 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002111 if (ret)
2112 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002113 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002114 ret = replay_one_dir_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 }
2119 }
2120 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002121 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002122}
2123
Chris Masond3977122009-01-05 21:25:51 -05002124static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002125 struct btrfs_root *root,
2126 struct btrfs_path *path, int *level,
2127 struct walk_control *wc)
2128{
2129 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002130 u64 bytenr;
2131 u64 ptr_gen;
2132 struct extent_buffer *next;
2133 struct extent_buffer *cur;
2134 struct extent_buffer *parent;
2135 u32 blocksize;
2136 int ret = 0;
2137
2138 WARN_ON(*level < 0);
2139 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2140
Chris Masond3977122009-01-05 21:25:51 -05002141 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002142 WARN_ON(*level < 0);
2143 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2144 cur = path->nodes[*level];
2145
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302146 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002147
2148 if (path->slots[*level] >=
2149 btrfs_header_nritems(cur))
2150 break;
2151
2152 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2153 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2154 blocksize = btrfs_level_size(root, *level - 1);
2155
2156 parent = path->nodes[*level];
2157 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002158
2159 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002160 if (!next)
2161 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002162
Chris Masone02119d2008-09-05 16:13:11 -04002163 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002164 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002165 if (ret) {
2166 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002167 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002168 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002169
Chris Masone02119d2008-09-05 16:13:11 -04002170 path->slots[*level]++;
2171 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002172 ret = btrfs_read_buffer(next, ptr_gen);
2173 if (ret) {
2174 free_extent_buffer(next);
2175 return ret;
2176 }
Chris Masone02119d2008-09-05 16:13:11 -04002177
Josef Bacik681ae502013-10-07 15:11:00 -04002178 if (trans) {
2179 btrfs_tree_lock(next);
2180 btrfs_set_lock_blocking(next);
2181 clean_tree_block(trans, root, next);
2182 btrfs_wait_tree_block_writeback(next);
2183 btrfs_tree_unlock(next);
2184 }
Chris Masone02119d2008-09-05 16:13:11 -04002185
Chris Masone02119d2008-09-05 16:13:11 -04002186 WARN_ON(root_owner !=
2187 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002188 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002189 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002190 if (ret) {
2191 free_extent_buffer(next);
2192 return ret;
2193 }
Chris Masone02119d2008-09-05 16:13:11 -04002194 }
2195 free_extent_buffer(next);
2196 continue;
2197 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002198 ret = btrfs_read_buffer(next, ptr_gen);
2199 if (ret) {
2200 free_extent_buffer(next);
2201 return ret;
2202 }
Chris Masone02119d2008-09-05 16:13:11 -04002203
2204 WARN_ON(*level <= 0);
2205 if (path->nodes[*level-1])
2206 free_extent_buffer(path->nodes[*level-1]);
2207 path->nodes[*level-1] = next;
2208 *level = btrfs_header_level(next);
2209 path->slots[*level] = 0;
2210 cond_resched();
2211 }
2212 WARN_ON(*level < 0);
2213 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2214
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002215 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002216
2217 cond_resched();
2218 return 0;
2219}
2220
Chris Masond3977122009-01-05 21:25:51 -05002221static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002222 struct btrfs_root *root,
2223 struct btrfs_path *path, int *level,
2224 struct walk_control *wc)
2225{
2226 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002227 int i;
2228 int slot;
2229 int ret;
2230
Chris Masond3977122009-01-05 21:25:51 -05002231 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002232 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002233 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002234 path->slots[i]++;
2235 *level = i;
2236 WARN_ON(*level == 0);
2237 return 0;
2238 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002239 struct extent_buffer *parent;
2240 if (path->nodes[*level] == root->node)
2241 parent = path->nodes[*level];
2242 else
2243 parent = path->nodes[*level + 1];
2244
2245 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002246 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002247 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002248 if (ret)
2249 return ret;
2250
Chris Masone02119d2008-09-05 16:13:11 -04002251 if (wc->free) {
2252 struct extent_buffer *next;
2253
2254 next = path->nodes[*level];
2255
Josef Bacik681ae502013-10-07 15:11:00 -04002256 if (trans) {
2257 btrfs_tree_lock(next);
2258 btrfs_set_lock_blocking(next);
2259 clean_tree_block(trans, root, next);
2260 btrfs_wait_tree_block_writeback(next);
2261 btrfs_tree_unlock(next);
2262 }
Chris Masone02119d2008-09-05 16:13:11 -04002263
Chris Masone02119d2008-09-05 16:13:11 -04002264 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002265 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002266 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002267 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002268 if (ret)
2269 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002270 }
2271 free_extent_buffer(path->nodes[*level]);
2272 path->nodes[*level] = NULL;
2273 *level = i + 1;
2274 }
2275 }
2276 return 1;
2277}
2278
2279/*
2280 * drop the reference count on the tree rooted at 'snap'. This traverses
2281 * the tree freeing any blocks that have a ref count of zero after being
2282 * decremented.
2283 */
2284static int walk_log_tree(struct btrfs_trans_handle *trans,
2285 struct btrfs_root *log, struct walk_control *wc)
2286{
2287 int ret = 0;
2288 int wret;
2289 int level;
2290 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002291 int orig_level;
2292
2293 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002294 if (!path)
2295 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002296
2297 level = btrfs_header_level(log->node);
2298 orig_level = level;
2299 path->nodes[level] = log->node;
2300 extent_buffer_get(log->node);
2301 path->slots[level] = 0;
2302
Chris Masond3977122009-01-05 21:25:51 -05002303 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002304 wret = walk_down_log_tree(trans, log, path, &level, wc);
2305 if (wret > 0)
2306 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002307 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002308 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002309 goto out;
2310 }
Chris Masone02119d2008-09-05 16:13:11 -04002311
2312 wret = walk_up_log_tree(trans, log, path, &level, wc);
2313 if (wret > 0)
2314 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002315 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002316 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002317 goto out;
2318 }
Chris Masone02119d2008-09-05 16:13:11 -04002319 }
2320
2321 /* was the root node processed? if not, catch it here */
2322 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002323 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002324 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002325 if (ret)
2326 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002327 if (wc->free) {
2328 struct extent_buffer *next;
2329
2330 next = path->nodes[orig_level];
2331
Josef Bacik681ae502013-10-07 15:11:00 -04002332 if (trans) {
2333 btrfs_tree_lock(next);
2334 btrfs_set_lock_blocking(next);
2335 clean_tree_block(trans, log, next);
2336 btrfs_wait_tree_block_writeback(next);
2337 btrfs_tree_unlock(next);
2338 }
Chris Masone02119d2008-09-05 16:13:11 -04002339
Chris Masone02119d2008-09-05 16:13:11 -04002340 WARN_ON(log->root_key.objectid !=
2341 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002342 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002343 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002344 if (ret)
2345 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002346 }
2347 }
2348
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002349out:
Chris Masone02119d2008-09-05 16:13:11 -04002350 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002351 return ret;
2352}
2353
Yan Zheng7237f182009-01-21 12:54:03 -05002354/*
2355 * helper function to update the item for a given subvolumes log root
2356 * in the tree of log roots
2357 */
2358static int update_log_root(struct btrfs_trans_handle *trans,
2359 struct btrfs_root *log)
2360{
2361 int ret;
2362
2363 if (log->log_transid == 1) {
2364 /* insert root item on the first sync */
2365 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2366 &log->root_key, &log->root_item);
2367 } else {
2368 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2369 &log->root_key, &log->root_item);
2370 }
2371 return ret;
2372}
2373
Miao Xie8b050d32014-02-20 18:08:58 +08002374static void wait_log_commit(struct btrfs_trans_handle *trans,
2375 struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002376{
2377 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002378 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002379
Yan Zheng7237f182009-01-21 12:54:03 -05002380 /*
2381 * we only allow two pending log transactions at a time,
2382 * so we know that if ours is more than 2 older than the
2383 * current transaction, we're done
2384 */
Chris Masone02119d2008-09-05 16:13:11 -04002385 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002386 prepare_to_wait(&root->log_commit_wait[index],
2387 &wait, TASK_UNINTERRUPTIBLE);
2388 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002389
Miao Xie48cab2e2014-02-20 18:08:52 +08002390 if (root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002391 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002392 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002393
Yan Zheng7237f182009-01-21 12:54:03 -05002394 finish_wait(&root->log_commit_wait[index], &wait);
2395 mutex_lock(&root->log_mutex);
Miao Xie48cab2e2014-02-20 18:08:52 +08002396 } while (root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002397 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002398}
2399
Jeff Mahoney143bede2012-03-01 14:56:26 +01002400static void wait_for_writer(struct btrfs_trans_handle *trans,
2401 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002402{
2403 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002404
2405 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002406 prepare_to_wait(&root->log_writer_wait,
2407 &wait, TASK_UNINTERRUPTIBLE);
2408 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002409 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002410 schedule();
2411 mutex_lock(&root->log_mutex);
2412 finish_wait(&root->log_writer_wait, &wait);
2413 }
Chris Masone02119d2008-09-05 16:13:11 -04002414}
2415
Miao Xie8b050d32014-02-20 18:08:58 +08002416static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2417 struct btrfs_log_ctx *ctx)
2418{
2419 if (!ctx)
2420 return;
2421
2422 mutex_lock(&root->log_mutex);
2423 list_del_init(&ctx->list);
2424 mutex_unlock(&root->log_mutex);
2425}
2426
2427/*
2428 * Invoked in log mutex context, or be sure there is no other task which
2429 * can access the list.
2430 */
2431static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2432 int index, int error)
2433{
2434 struct btrfs_log_ctx *ctx;
2435
2436 if (!error) {
2437 INIT_LIST_HEAD(&root->log_ctxs[index]);
2438 return;
2439 }
2440
2441 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2442 ctx->log_ret = error;
2443
2444 INIT_LIST_HEAD(&root->log_ctxs[index]);
2445}
2446
Chris Masone02119d2008-09-05 16:13:11 -04002447/*
2448 * btrfs_sync_log does sends a given tree log down to the disk and
2449 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002450 * you know that any inodes previously logged are safely on disk only
2451 * if it returns 0.
2452 *
2453 * Any other return value means you need to call btrfs_commit_transaction.
2454 * Some of the edge cases for fsyncing directories that have had unlinks
2455 * or renames done in the past mean that sometimes the only safe
2456 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2457 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002458 */
2459int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002460 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002461{
Yan Zheng7237f182009-01-21 12:54:03 -05002462 int index1;
2463 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002464 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002465 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002466 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002467 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002468 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002469 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002470 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002471
Yan Zheng7237f182009-01-21 12:54:03 -05002472 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002473 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002474 index1 = root->log_transid % 2;
2475 if (atomic_read(&root->log_commit[index1])) {
Miao Xie8b050d32014-02-20 18:08:58 +08002476 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002477 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002478 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002479 }
Yan Zheng7237f182009-01-21 12:54:03 -05002480 atomic_set(&root->log_commit[index1], 1);
2481
2482 /* wait for previous tree log sync to complete */
2483 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002484 wait_log_commit(trans, root, root->log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002485
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002486 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002487 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002488 /* when we're on an ssd, just kick the log commit out */
2489 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002490 mutex_unlock(&root->log_mutex);
2491 schedule_timeout_uninterruptible(1);
2492 mutex_lock(&root->log_mutex);
2493 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002494 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002495 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002496 break;
2497 }
Chris Masond0c803c2008-09-11 16:17:57 -04002498
Chris Mason12fcfd22009-03-24 10:24:20 -04002499 /* bail out if we need to do a full commit */
Miao Xie5c902ba2014-02-20 18:08:51 +08002500 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2501 trans->transid) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002502 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002503 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002504 mutex_unlock(&root->log_mutex);
2505 goto out;
2506 }
2507
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002508 if (log_transid % 2 == 0)
2509 mark = EXTENT_DIRTY;
2510 else
2511 mark = EXTENT_NEW;
2512
Chris Mason690587d2009-10-13 13:29:19 -04002513 /* we start IO on all the marked extents here, but we don't actually
2514 * wait for them until later.
2515 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002516 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002517 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002518 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002519 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002520 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002521 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002522 mutex_unlock(&root->log_mutex);
2523 goto out;
2524 }
Yan Zheng7237f182009-01-21 12:54:03 -05002525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002526 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002527
Yan Zheng7237f182009-01-21 12:54:03 -05002528 root->log_transid++;
2529 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002530 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002531 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002532 * IO has been started, blocks of the log tree have WRITTEN flag set
2533 * in their headers. new modifications of the log will be written to
2534 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002535 */
2536 mutex_unlock(&root->log_mutex);
2537
2538 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002539 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002540 atomic_inc(&log_root_tree->log_writers);
2541 mutex_unlock(&log_root_tree->log_mutex);
2542
2543 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002544
2545 mutex_lock(&log_root_tree->log_mutex);
2546 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2547 smp_mb();
2548 if (waitqueue_active(&log_root_tree->log_writer_wait))
2549 wake_up(&log_root_tree->log_writer_wait);
2550 }
2551
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002552 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002553 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002554 if (ret != -ENOSPC) {
2555 btrfs_abort_transaction(trans, root, ret);
2556 mutex_unlock(&log_root_tree->log_mutex);
2557 goto out;
2558 }
Miao Xie5c902ba2014-02-20 18:08:51 +08002559 ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) =
2560 trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002561 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002562 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002563 mutex_unlock(&log_root_tree->log_mutex);
2564 ret = -EAGAIN;
2565 goto out;
2566 }
2567
Yan Zheng7237f182009-01-21 12:54:03 -05002568 index2 = log_root_tree->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +08002569
2570 btrfs_init_log_ctx(&root_log_ctx);
2571 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2572
Yan Zheng7237f182009-01-21 12:54:03 -05002573 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002574 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002575 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xie8b050d32014-02-20 18:08:58 +08002576 wait_log_commit(trans, log_root_tree,
2577 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002578 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002579 mutex_unlock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002580 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002581 goto out;
2582 }
2583 atomic_set(&log_root_tree->log_commit[index2], 1);
2584
Chris Mason12fcfd22009-03-24 10:24:20 -04002585 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2586 wait_log_commit(trans, log_root_tree,
2587 log_root_tree->log_transid - 1);
2588 }
Yan Zheng7237f182009-01-21 12:54:03 -05002589
Chris Mason12fcfd22009-03-24 10:24:20 -04002590 wait_for_writer(trans, log_root_tree);
2591
2592 /*
2593 * now that we've moved on to the tree of log tree roots,
2594 * check the full commit flag again
2595 */
Miao Xie5c902ba2014-02-20 18:08:51 +08002596 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2597 trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002598 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002599 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002600 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002601 mutex_unlock(&log_root_tree->log_mutex);
2602 ret = -EAGAIN;
2603 goto out_wake_log_root;
2604 }
Yan Zheng7237f182009-01-21 12:54:03 -05002605
Miao Xiec6adc9c2013-05-28 10:05:39 +00002606 ret = btrfs_write_marked_extents(log_root_tree,
2607 &log_root_tree->dirty_log_pages,
2608 EXTENT_DIRTY | EXTENT_NEW);
2609 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002610 if (ret) {
2611 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002612 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002613 mutex_unlock(&log_root_tree->log_mutex);
2614 goto out_wake_log_root;
2615 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002616 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002617 btrfs_wait_marked_extents(log_root_tree,
2618 &log_root_tree->dirty_log_pages,
2619 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002620 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002621
David Sterba6c417612011-04-13 15:41:04 +02002622 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002623 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002624 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002625 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002626
Yan Zheng7237f182009-01-21 12:54:03 -05002627 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002628 mutex_unlock(&log_root_tree->log_mutex);
2629
2630 /*
2631 * nobody else is going to jump in and write the the ctree
2632 * super here because the log_commit atomic below is protecting
2633 * us. We must be called with a transaction handle pinning
2634 * the running transaction open, so a full commit can't hop
2635 * in and cause problems either.
2636 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002637 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002638 if (ret) {
2639 btrfs_abort_transaction(trans, root, ret);
2640 goto out_wake_log_root;
2641 }
Yan Zheng7237f182009-01-21 12:54:03 -05002642
Chris Mason257c62e2009-10-13 13:21:08 -04002643 mutex_lock(&root->log_mutex);
2644 if (root->last_log_commit < log_transid)
2645 root->last_log_commit = log_transid;
2646 mutex_unlock(&root->log_mutex);
2647
Chris Mason12fcfd22009-03-24 10:24:20 -04002648out_wake_log_root:
Miao Xie8b050d32014-02-20 18:08:58 +08002649 /*
2650 * We needn't get log_mutex here because we are sure all
2651 * the other tasks are blocked.
2652 */
2653 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2654
2655 /*
2656 * It is dangerous if log_commit is changed before we set
2657 * ->log_ret of log ctx. Because the readers may not get
2658 * the return value.
2659 */
2660 smp_wmb();
2661
Yan Zheng7237f182009-01-21 12:54:03 -05002662 atomic_set(&log_root_tree->log_commit[index2], 0);
2663 smp_mb();
2664 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2665 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002666out:
Miao Xie8b050d32014-02-20 18:08:58 +08002667 /* See above. */
2668 btrfs_remove_all_log_ctxs(root, index1, ret);
2669
2670 /* See above. */
2671 smp_wmb();
Yan Zheng7237f182009-01-21 12:54:03 -05002672 atomic_set(&root->log_commit[index1], 0);
Miao Xie8b050d32014-02-20 18:08:58 +08002673
Yan Zheng7237f182009-01-21 12:54:03 -05002674 smp_mb();
2675 if (waitqueue_active(&root->log_commit_wait[index1]))
2676 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002677 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002678}
2679
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002680static void free_log_tree(struct btrfs_trans_handle *trans,
2681 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002682{
2683 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002684 u64 start;
2685 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002686 struct walk_control wc = {
2687 .free = 1,
2688 .process_func = process_one_buffer
2689 };
2690
Josef Bacik681ae502013-10-07 15:11:00 -04002691 ret = walk_log_tree(trans, log, &wc);
2692 /* I don't think this can happen but just in case */
2693 if (ret)
2694 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002695
Chris Masond3977122009-01-05 21:25:51 -05002696 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002697 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002698 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2699 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002700 if (ret)
2701 break;
2702
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002703 clear_extent_bits(&log->dirty_log_pages, start, end,
2704 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002705 }
2706
Josef Bacik2ab28f32012-10-12 15:27:49 -04002707 /*
2708 * We may have short-circuited the log tree with the full commit logic
2709 * and left ordered extents on our list, so clear these out to keep us
2710 * from leaking inodes and memory.
2711 */
2712 btrfs_free_logged_extents(log, 0);
2713 btrfs_free_logged_extents(log, 1);
2714
Yan Zheng7237f182009-01-21 12:54:03 -05002715 free_extent_buffer(log->node);
2716 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002717}
2718
2719/*
2720 * free all the extents used by the tree log. This should be called
2721 * at commit time of the full transaction
2722 */
2723int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2724{
2725 if (root->log_root) {
2726 free_log_tree(trans, root->log_root);
2727 root->log_root = NULL;
2728 }
2729 return 0;
2730}
2731
2732int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2733 struct btrfs_fs_info *fs_info)
2734{
2735 if (fs_info->log_root_tree) {
2736 free_log_tree(trans, fs_info->log_root_tree);
2737 fs_info->log_root_tree = NULL;
2738 }
Chris Masone02119d2008-09-05 16:13:11 -04002739 return 0;
2740}
2741
2742/*
Chris Masone02119d2008-09-05 16:13:11 -04002743 * If both a file and directory are logged, and unlinks or renames are
2744 * mixed in, we have a few interesting corners:
2745 *
2746 * create file X in dir Y
2747 * link file X to X.link in dir Y
2748 * fsync file X
2749 * unlink file X but leave X.link
2750 * fsync dir Y
2751 *
2752 * After a crash we would expect only X.link to exist. But file X
2753 * didn't get fsync'd again so the log has back refs for X and X.link.
2754 *
2755 * We solve this by removing directory entries and inode backrefs from the
2756 * log when a file that was logged in the current transaction is
2757 * unlinked. Any later fsync will include the updated log entries, and
2758 * we'll be able to reconstruct the proper directory items from backrefs.
2759 *
2760 * This optimizations allows us to avoid relogging the entire inode
2761 * or the entire directory.
2762 */
2763int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2764 struct btrfs_root *root,
2765 const char *name, int name_len,
2766 struct inode *dir, u64 index)
2767{
2768 struct btrfs_root *log;
2769 struct btrfs_dir_item *di;
2770 struct btrfs_path *path;
2771 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002772 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002773 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002774 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002775
Chris Mason3a5f1d42008-09-11 15:53:37 -04002776 if (BTRFS_I(dir)->logged_trans < trans->transid)
2777 return 0;
2778
Chris Masone02119d2008-09-05 16:13:11 -04002779 ret = join_running_log_trans(root);
2780 if (ret)
2781 return 0;
2782
2783 mutex_lock(&BTRFS_I(dir)->log_mutex);
2784
2785 log = root->log_root;
2786 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002787 if (!path) {
2788 err = -ENOMEM;
2789 goto out_unlock;
2790 }
liubo2a29edc2011-01-26 06:22:08 +00002791
Li Zefan33345d012011-04-20 10:31:50 +08002792 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002793 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002794 if (IS_ERR(di)) {
2795 err = PTR_ERR(di);
2796 goto fail;
2797 }
2798 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002799 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2800 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002801 if (ret) {
2802 err = ret;
2803 goto fail;
2804 }
Chris Masone02119d2008-09-05 16:13:11 -04002805 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002806 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002807 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002808 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002809 if (IS_ERR(di)) {
2810 err = PTR_ERR(di);
2811 goto fail;
2812 }
2813 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002814 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2815 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002816 if (ret) {
2817 err = ret;
2818 goto fail;
2819 }
Chris Masone02119d2008-09-05 16:13:11 -04002820 }
2821
2822 /* update the directory size in the log to reflect the names
2823 * we have removed
2824 */
2825 if (bytes_del) {
2826 struct btrfs_key key;
2827
Li Zefan33345d012011-04-20 10:31:50 +08002828 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002829 key.offset = 0;
2830 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002831 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002832
2833 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002834 if (ret < 0) {
2835 err = ret;
2836 goto fail;
2837 }
Chris Masone02119d2008-09-05 16:13:11 -04002838 if (ret == 0) {
2839 struct btrfs_inode_item *item;
2840 u64 i_size;
2841
2842 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2843 struct btrfs_inode_item);
2844 i_size = btrfs_inode_size(path->nodes[0], item);
2845 if (i_size > bytes_del)
2846 i_size -= bytes_del;
2847 else
2848 i_size = 0;
2849 btrfs_set_inode_size(path->nodes[0], item, i_size);
2850 btrfs_mark_buffer_dirty(path->nodes[0]);
2851 } else
2852 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002853 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002854 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002855fail:
Chris Masone02119d2008-09-05 16:13:11 -04002856 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002857out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002858 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002859 if (ret == -ENOSPC) {
2860 root->fs_info->last_trans_log_full_commit = trans->transid;
2861 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002862 } else if (ret < 0)
2863 btrfs_abort_transaction(trans, root, ret);
2864
Chris Mason12fcfd22009-03-24 10:24:20 -04002865 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002866
Andi Kleen411fc6b2010-10-29 15:14:31 -04002867 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002868}
2869
2870/* see comments for btrfs_del_dir_entries_in_log */
2871int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2872 struct btrfs_root *root,
2873 const char *name, int name_len,
2874 struct inode *inode, u64 dirid)
2875{
2876 struct btrfs_root *log;
2877 u64 index;
2878 int ret;
2879
Chris Mason3a5f1d42008-09-11 15:53:37 -04002880 if (BTRFS_I(inode)->logged_trans < trans->transid)
2881 return 0;
2882
Chris Masone02119d2008-09-05 16:13:11 -04002883 ret = join_running_log_trans(root);
2884 if (ret)
2885 return 0;
2886 log = root->log_root;
2887 mutex_lock(&BTRFS_I(inode)->log_mutex);
2888
Li Zefan33345d012011-04-20 10:31:50 +08002889 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002890 dirid, &index);
2891 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002892 if (ret == -ENOSPC) {
2893 root->fs_info->last_trans_log_full_commit = trans->transid;
2894 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002895 } else if (ret < 0 && ret != -ENOENT)
2896 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002897 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002898
Chris Masone02119d2008-09-05 16:13:11 -04002899 return ret;
2900}
2901
2902/*
2903 * creates a range item in the log for 'dirid'. first_offset and
2904 * last_offset tell us which parts of the key space the log should
2905 * be considered authoritative for.
2906 */
2907static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2908 struct btrfs_root *log,
2909 struct btrfs_path *path,
2910 int key_type, u64 dirid,
2911 u64 first_offset, u64 last_offset)
2912{
2913 int ret;
2914 struct btrfs_key key;
2915 struct btrfs_dir_log_item *item;
2916
2917 key.objectid = dirid;
2918 key.offset = first_offset;
2919 if (key_type == BTRFS_DIR_ITEM_KEY)
2920 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2921 else
2922 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2923 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002924 if (ret)
2925 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002926
2927 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2928 struct btrfs_dir_log_item);
2929 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2930 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002931 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002932 return 0;
2933}
2934
2935/*
2936 * log all the items included in the current transaction for a given
2937 * directory. This also creates the range items in the log tree required
2938 * to replay anything deleted before the fsync
2939 */
2940static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2941 struct btrfs_root *root, struct inode *inode,
2942 struct btrfs_path *path,
2943 struct btrfs_path *dst_path, int key_type,
2944 u64 min_offset, u64 *last_offset_ret)
2945{
2946 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002947 struct btrfs_root *log = root->log_root;
2948 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002949 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002950 int ret;
2951 int i;
2952 int nritems;
2953 u64 first_offset = min_offset;
2954 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002955 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002956
2957 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002958
Li Zefan33345d012011-04-20 10:31:50 +08002959 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002960 min_key.type = key_type;
2961 min_key.offset = min_offset;
2962
2963 path->keep_locks = 1;
2964
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002965 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002966
2967 /*
2968 * we didn't find anything from this transaction, see if there
2969 * is anything at all
2970 */
Li Zefan33345d012011-04-20 10:31:50 +08002971 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2972 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002973 min_key.type = key_type;
2974 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002975 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002976 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2977 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002978 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002979 return ret;
2980 }
Li Zefan33345d012011-04-20 10:31:50 +08002981 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002982
2983 /* if ret == 0 there are items for this type,
2984 * create a range to tell us the last key of this type.
2985 * otherwise, there are no items in this directory after
2986 * *min_offset, and we create a range to indicate that.
2987 */
2988 if (ret == 0) {
2989 struct btrfs_key tmp;
2990 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2991 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002992 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002993 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002994 }
2995 goto done;
2996 }
2997
2998 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002999 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003000 if (ret == 0) {
3001 struct btrfs_key tmp;
3002 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3003 if (key_type == tmp.type) {
3004 first_offset = tmp.offset;
3005 ret = overwrite_item(trans, log, dst_path,
3006 path->nodes[0], path->slots[0],
3007 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003008 if (ret) {
3009 err = ret;
3010 goto done;
3011 }
Chris Masone02119d2008-09-05 16:13:11 -04003012 }
3013 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003014 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003015
3016 /* find the first key from this transaction again */
3017 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303018 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003019 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003020
3021 /*
3022 * we have a block from this transaction, log every item in it
3023 * from our directory
3024 */
Chris Masond3977122009-01-05 21:25:51 -05003025 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003026 struct btrfs_key tmp;
3027 src = path->nodes[0];
3028 nritems = btrfs_header_nritems(src);
3029 for (i = path->slots[0]; i < nritems; i++) {
3030 btrfs_item_key_to_cpu(src, &min_key, i);
3031
Li Zefan33345d012011-04-20 10:31:50 +08003032 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003033 goto done;
3034 ret = overwrite_item(trans, log, dst_path, src, i,
3035 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003036 if (ret) {
3037 err = ret;
3038 goto done;
3039 }
Chris Masone02119d2008-09-05 16:13:11 -04003040 }
3041 path->slots[0] = nritems;
3042
3043 /*
3044 * look ahead to the next item and see if it is also
3045 * from this directory and from this transaction
3046 */
3047 ret = btrfs_next_leaf(root, path);
3048 if (ret == 1) {
3049 last_offset = (u64)-1;
3050 goto done;
3051 }
3052 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003053 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003054 last_offset = (u64)-1;
3055 goto done;
3056 }
3057 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3058 ret = overwrite_item(trans, log, dst_path,
3059 path->nodes[0], path->slots[0],
3060 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003061 if (ret)
3062 err = ret;
3063 else
3064 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003065 goto done;
3066 }
3067 }
3068done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003069 btrfs_release_path(path);
3070 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003071
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003072 if (err == 0) {
3073 *last_offset_ret = last_offset;
3074 /*
3075 * insert the log range keys to indicate where the log
3076 * is valid
3077 */
3078 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003079 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003080 if (ret)
3081 err = ret;
3082 }
3083 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003084}
3085
3086/*
3087 * logging directories is very similar to logging inodes, We find all the items
3088 * from the current transaction and write them to the log.
3089 *
3090 * The recovery code scans the directory in the subvolume, and if it finds a
3091 * key in the range logged that is not present in the log tree, then it means
3092 * that dir entry was unlinked during the transaction.
3093 *
3094 * In order for that scan to work, we must include one key smaller than
3095 * the smallest logged by this transaction and one key larger than the largest
3096 * key logged by this transaction.
3097 */
3098static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3099 struct btrfs_root *root, struct inode *inode,
3100 struct btrfs_path *path,
3101 struct btrfs_path *dst_path)
3102{
3103 u64 min_key;
3104 u64 max_key;
3105 int ret;
3106 int key_type = BTRFS_DIR_ITEM_KEY;
3107
3108again:
3109 min_key = 0;
3110 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003111 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003112 ret = log_dir_items(trans, root, inode, path,
3113 dst_path, key_type, min_key,
3114 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003115 if (ret)
3116 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003117 if (max_key == (u64)-1)
3118 break;
3119 min_key = max_key + 1;
3120 }
3121
3122 if (key_type == BTRFS_DIR_ITEM_KEY) {
3123 key_type = BTRFS_DIR_INDEX_KEY;
3124 goto again;
3125 }
3126 return 0;
3127}
3128
3129/*
3130 * a helper function to drop items from the log before we relog an
3131 * inode. max_key_type indicates the highest item type to remove.
3132 * This cannot be run for file data extents because it does not
3133 * free the extents they point to.
3134 */
3135static int drop_objectid_items(struct btrfs_trans_handle *trans,
3136 struct btrfs_root *log,
3137 struct btrfs_path *path,
3138 u64 objectid, int max_key_type)
3139{
3140 int ret;
3141 struct btrfs_key key;
3142 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003143 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003144
3145 key.objectid = objectid;
3146 key.type = max_key_type;
3147 key.offset = (u64)-1;
3148
Chris Masond3977122009-01-05 21:25:51 -05003149 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003150 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003151 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003152 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003153 break;
3154
3155 if (path->slots[0] == 0)
3156 break;
3157
3158 path->slots[0]--;
3159 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3160 path->slots[0]);
3161
3162 if (found_key.objectid != objectid)
3163 break;
3164
Josef Bacik18ec90d2012-09-28 11:56:28 -04003165 found_key.offset = 0;
3166 found_key.type = 0;
3167 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3168 &start_slot);
3169
3170 ret = btrfs_del_items(trans, log, path, start_slot,
3171 path->slots[0] - start_slot + 1);
3172 /*
3173 * If start slot isn't 0 then we don't need to re-search, we've
3174 * found the last guy with the objectid in this tree.
3175 */
3176 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003177 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003178 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003179 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003180 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003181 if (ret > 0)
3182 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003183 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003184}
3185
Josef Bacik94edf4a2012-09-25 14:56:25 -04003186static void fill_inode_item(struct btrfs_trans_handle *trans,
3187 struct extent_buffer *leaf,
3188 struct btrfs_inode_item *item,
3189 struct inode *inode, int log_inode_only)
3190{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003191 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003192
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003193 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003194
3195 if (log_inode_only) {
3196 /* set the generation to zero so the recover code
3197 * can tell the difference between an logging
3198 * just to say 'this inode exists' and a logging
3199 * to say 'update this inode with these values'
3200 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003201 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3202 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003203 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003204 btrfs_set_token_inode_generation(leaf, item,
3205 BTRFS_I(inode)->generation,
3206 &token);
3207 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003208 }
3209
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003210 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3211 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3212 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3213 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3214
3215 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3216 inode->i_atime.tv_sec, &token);
3217 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3218 inode->i_atime.tv_nsec, &token);
3219
3220 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3221 inode->i_mtime.tv_sec, &token);
3222 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3223 inode->i_mtime.tv_nsec, &token);
3224
3225 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3226 inode->i_ctime.tv_sec, &token);
3227 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3228 inode->i_ctime.tv_nsec, &token);
3229
3230 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3231 &token);
3232
3233 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3234 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3235 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3236 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3237 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003238}
3239
Josef Bacika95249b2012-10-11 16:17:34 -04003240static int log_inode_item(struct btrfs_trans_handle *trans,
3241 struct btrfs_root *log, struct btrfs_path *path,
3242 struct inode *inode)
3243{
3244 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003245 int ret;
3246
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003247 ret = btrfs_insert_empty_item(trans, log, path,
3248 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003249 sizeof(*inode_item));
3250 if (ret && ret != -EEXIST)
3251 return ret;
3252 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3253 struct btrfs_inode_item);
3254 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3255 btrfs_release_path(path);
3256 return 0;
3257}
3258
Chris Mason31ff1cd2008-09-11 16:17:57 -04003259static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003260 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003261 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003262 struct btrfs_path *src_path, u64 *last_extent,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003263 int start_slot, int nr, int inode_only)
3264{
3265 unsigned long src_offset;
3266 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003267 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003268 struct btrfs_file_extent_item *extent;
3269 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003270 struct extent_buffer *src = src_path->nodes[0];
3271 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003272 int ret;
3273 struct btrfs_key *ins_keys;
3274 u32 *ins_sizes;
3275 char *ins_data;
3276 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003277 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003278 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003279 bool has_extents = false;
3280 bool need_find_last_extent = (*last_extent == 0);
3281 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003282
3283 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003284
3285 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3286 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003287 if (!ins_data)
3288 return -ENOMEM;
3289
Josef Bacik16e75492013-10-22 12:18:51 -04003290 first_key.objectid = (u64)-1;
3291
Chris Mason31ff1cd2008-09-11 16:17:57 -04003292 ins_sizes = (u32 *)ins_data;
3293 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3294
3295 for (i = 0; i < nr; i++) {
3296 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3297 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3298 }
3299 ret = btrfs_insert_empty_items(trans, log, dst_path,
3300 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003301 if (ret) {
3302 kfree(ins_data);
3303 return ret;
3304 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003305
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003306 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003307 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3308 dst_path->slots[0]);
3309
3310 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3311
Josef Bacik16e75492013-10-22 12:18:51 -04003312 if ((i == (nr - 1)))
3313 last_key = ins_keys[i];
3314
Josef Bacik94edf4a2012-09-25 14:56:25 -04003315 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003316 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3317 dst_path->slots[0],
3318 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003319 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3320 inode, inode_only == LOG_INODE_EXISTS);
3321 } else {
3322 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3323 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003324 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003325
Josef Bacik16e75492013-10-22 12:18:51 -04003326 /*
3327 * We set need_find_last_extent here in case we know we were
3328 * processing other items and then walk into the first extent in
3329 * the inode. If we don't hit an extent then nothing changes,
3330 * we'll do the last search the next time around.
3331 */
3332 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3333 has_extents = true;
3334 if (need_find_last_extent &&
3335 first_key.objectid == (u64)-1)
3336 first_key = ins_keys[i];
3337 } else {
3338 need_find_last_extent = false;
3339 }
3340
Chris Mason31ff1cd2008-09-11 16:17:57 -04003341 /* take a reference on file data extents so that truncates
3342 * or deletes of this inode don't have to relog the inode
3343 * again
3344 */
Liu Bod2794402012-08-29 01:07:56 -06003345 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3346 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003347 int found_type;
3348 extent = btrfs_item_ptr(src, start_slot + i,
3349 struct btrfs_file_extent_item);
3350
liubo8e531cd2011-05-06 10:36:09 +08003351 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3352 continue;
3353
Chris Mason31ff1cd2008-09-11 16:17:57 -04003354 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003355 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003356 u64 ds, dl, cs, cl;
3357 ds = btrfs_file_extent_disk_bytenr(src,
3358 extent);
3359 /* ds == 0 is a hole */
3360 if (ds == 0)
3361 continue;
3362
3363 dl = btrfs_file_extent_disk_num_bytes(src,
3364 extent);
3365 cs = btrfs_file_extent_offset(src, extent);
3366 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003367 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003368 if (btrfs_file_extent_compression(src,
3369 extent)) {
3370 cs = 0;
3371 cl = dl;
3372 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003373
3374 ret = btrfs_lookup_csums_range(
3375 log->fs_info->csum_root,
3376 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003377 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003378 if (ret) {
3379 btrfs_release_path(dst_path);
3380 kfree(ins_data);
3381 return ret;
3382 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003383 }
3384 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003385 }
3386
3387 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003388 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003389 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003390
3391 /*
3392 * we have to do this after the loop above to avoid changing the
3393 * log tree while trying to change the log tree.
3394 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003395 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003396 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003397 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3398 struct btrfs_ordered_sum,
3399 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003400 if (!ret)
3401 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003402 list_del(&sums->list);
3403 kfree(sums);
3404 }
Josef Bacik16e75492013-10-22 12:18:51 -04003405
3406 if (!has_extents)
3407 return ret;
3408
3409 /*
3410 * Because we use btrfs_search_forward we could skip leaves that were
3411 * not modified and then assume *last_extent is valid when it really
3412 * isn't. So back up to the previous leaf and read the end of the last
3413 * extent before we go and fill in holes.
3414 */
3415 if (need_find_last_extent) {
3416 u64 len;
3417
3418 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3419 if (ret < 0)
3420 return ret;
3421 if (ret)
3422 goto fill_holes;
3423 if (src_path->slots[0])
3424 src_path->slots[0]--;
3425 src = src_path->nodes[0];
3426 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3427 if (key.objectid != btrfs_ino(inode) ||
3428 key.type != BTRFS_EXTENT_DATA_KEY)
3429 goto fill_holes;
3430 extent = btrfs_item_ptr(src, src_path->slots[0],
3431 struct btrfs_file_extent_item);
3432 if (btrfs_file_extent_type(src, extent) ==
3433 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003434 len = btrfs_file_extent_inline_len(src,
3435 src_path->slots[0],
3436 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003437 *last_extent = ALIGN(key.offset + len,
3438 log->sectorsize);
3439 } else {
3440 len = btrfs_file_extent_num_bytes(src, extent);
3441 *last_extent = key.offset + len;
3442 }
3443 }
3444fill_holes:
3445 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3446 * things could have happened
3447 *
3448 * 1) A merge could have happened, so we could currently be on a leaf
3449 * that holds what we were copying in the first place.
3450 * 2) A split could have happened, and now not all of the items we want
3451 * are on the same leaf.
3452 *
3453 * So we need to adjust how we search for holes, we need to drop the
3454 * path and re-search for the first extent key we found, and then walk
3455 * forward until we hit the last one we copied.
3456 */
3457 if (need_find_last_extent) {
3458 /* btrfs_prev_leaf could return 1 without releasing the path */
3459 btrfs_release_path(src_path);
3460 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3461 src_path, 0, 0);
3462 if (ret < 0)
3463 return ret;
3464 ASSERT(ret == 0);
3465 src = src_path->nodes[0];
3466 i = src_path->slots[0];
3467 } else {
3468 i = start_slot;
3469 }
3470
3471 /*
3472 * Ok so here we need to go through and fill in any holes we may have
3473 * to make sure that holes are punched for those areas in case they had
3474 * extents previously.
3475 */
3476 while (!done) {
3477 u64 offset, len;
3478 u64 extent_end;
3479
3480 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3481 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3482 if (ret < 0)
3483 return ret;
3484 ASSERT(ret == 0);
3485 src = src_path->nodes[0];
3486 i = 0;
3487 }
3488
3489 btrfs_item_key_to_cpu(src, &key, i);
3490 if (!btrfs_comp_cpu_keys(&key, &last_key))
3491 done = true;
3492 if (key.objectid != btrfs_ino(inode) ||
3493 key.type != BTRFS_EXTENT_DATA_KEY) {
3494 i++;
3495 continue;
3496 }
3497 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3498 if (btrfs_file_extent_type(src, extent) ==
3499 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003500 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003501 extent_end = ALIGN(key.offset + len, log->sectorsize);
3502 } else {
3503 len = btrfs_file_extent_num_bytes(src, extent);
3504 extent_end = key.offset + len;
3505 }
3506 i++;
3507
3508 if (*last_extent == key.offset) {
3509 *last_extent = extent_end;
3510 continue;
3511 }
3512 offset = *last_extent;
3513 len = key.offset - *last_extent;
3514 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3515 offset, 0, 0, len, 0, len, 0,
3516 0, 0);
3517 if (ret)
3518 break;
3519 *last_extent = offset + len;
3520 }
3521 /*
3522 * Need to let the callers know we dropped the path so they should
3523 * re-search.
3524 */
3525 if (!ret && need_find_last_extent)
3526 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003527 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003528}
3529
Josef Bacik5dc562c2012-08-17 13:14:17 -04003530static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3531{
3532 struct extent_map *em1, *em2;
3533
3534 em1 = list_entry(a, struct extent_map, list);
3535 em2 = list_entry(b, struct extent_map, list);
3536
3537 if (em1->start < em2->start)
3538 return -1;
3539 else if (em1->start > em2->start)
3540 return 1;
3541 return 0;
3542}
3543
Josef Bacik5dc562c2012-08-17 13:14:17 -04003544static int log_one_extent(struct btrfs_trans_handle *trans,
3545 struct inode *inode, struct btrfs_root *root,
Miao Xie827463c2014-01-14 20:31:51 +08003546 struct extent_map *em, struct btrfs_path *path,
3547 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003548{
3549 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003550 struct btrfs_file_extent_item *fi;
3551 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003552 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003553 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003554 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003555 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003556 u64 mod_start = em->mod_start;
3557 u64 mod_len = em->mod_len;
3558 u64 csum_offset;
3559 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003560 u64 extent_offset = em->start - em->orig_start;
3561 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003562 int ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003563 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003564 int extent_inserted = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003565
Josef Bacik70c8a912012-10-11 16:54:30 -04003566 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003567 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003568
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003569 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3570 em->start + em->len, NULL, 0, 1,
3571 sizeof(*fi), &extent_inserted);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003572 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003573 return ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003574
3575 if (!extent_inserted) {
3576 key.objectid = btrfs_ino(inode);
3577 key.type = BTRFS_EXTENT_DATA_KEY;
3578 key.offset = em->start;
3579
3580 ret = btrfs_insert_empty_item(trans, log, path, &key,
3581 sizeof(*fi));
3582 if (ret)
3583 return ret;
3584 }
Josef Bacik70c8a912012-10-11 16:54:30 -04003585 leaf = path->nodes[0];
3586 fi = btrfs_item_ptr(leaf, path->slots[0],
3587 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003588
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003589 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3590 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003591 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3592 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003593 btrfs_set_token_file_extent_type(leaf, fi,
3594 BTRFS_FILE_EXTENT_PREALLOC,
3595 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003596 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003597 btrfs_set_token_file_extent_type(leaf, fi,
3598 BTRFS_FILE_EXTENT_REG,
3599 &token);
Josef Baciked9e8af2013-10-14 17:23:08 -04003600 if (em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003601 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003602 }
3603
Josef Bacik70c8a912012-10-11 16:54:30 -04003604 block_len = max(em->block_len, em->orig_block_len);
3605 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003606 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3607 em->block_start,
3608 &token);
3609 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3610 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003611 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003612 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3613 em->block_start -
3614 extent_offset, &token);
3615 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3616 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003617 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003618 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3619 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3620 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003621 }
3622
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003623 btrfs_set_token_file_extent_offset(leaf, fi,
3624 em->start - em->orig_start,
3625 &token);
3626 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003627 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003628 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3629 &token);
3630 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3631 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003632 btrfs_mark_buffer_dirty(leaf);
3633
Josef Bacik70c8a912012-10-11 16:54:30 -04003634 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003635 if (ret) {
3636 return ret;
3637 }
3638
3639 if (skip_csum)
3640 return 0;
3641
Josef Bacik2ab28f32012-10-12 15:27:49 -04003642 /*
3643 * First check and see if our csums are on our outstanding ordered
3644 * extents.
3645 */
Miao Xie827463c2014-01-14 20:31:51 +08003646 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003647 struct btrfs_ordered_sum *sum;
3648
3649 if (!mod_len)
3650 break;
3651
Josef Bacik2ab28f32012-10-12 15:27:49 -04003652 if (ordered->file_offset + ordered->len <= mod_start ||
3653 mod_start + mod_len <= ordered->file_offset)
3654 continue;
3655
3656 /*
3657 * We are going to copy all the csums on this ordered extent, so
3658 * go ahead and adjust mod_start and mod_len in case this
3659 * ordered extent has already been logged.
3660 */
3661 if (ordered->file_offset > mod_start) {
3662 if (ordered->file_offset + ordered->len >=
3663 mod_start + mod_len)
3664 mod_len = ordered->file_offset - mod_start;
3665 /*
3666 * If we have this case
3667 *
3668 * |--------- logged extent ---------|
3669 * |----- ordered extent ----|
3670 *
3671 * Just don't mess with mod_start and mod_len, we'll
3672 * just end up logging more csums than we need and it
3673 * will be ok.
3674 */
3675 } else {
3676 if (ordered->file_offset + ordered->len <
3677 mod_start + mod_len) {
3678 mod_len = (mod_start + mod_len) -
3679 (ordered->file_offset + ordered->len);
3680 mod_start = ordered->file_offset +
3681 ordered->len;
3682 } else {
3683 mod_len = 0;
3684 }
3685 }
3686
3687 /*
3688 * To keep us from looping for the above case of an ordered
3689 * extent that falls inside of the logged extent.
3690 */
3691 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3692 &ordered->flags))
3693 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003694
Miao Xie23c671a2014-01-14 20:31:52 +08003695 if (ordered->csum_bytes_left) {
3696 btrfs_start_ordered_extent(inode, ordered, 0);
3697 wait_event(ordered->wait,
3698 ordered->csum_bytes_left == 0);
3699 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003700
3701 list_for_each_entry(sum, &ordered->list, list) {
3702 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003703 if (ret)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003704 goto unlocked;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003705 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003706
3707 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003708unlocked:
3709
3710 if (!mod_len || ret)
3711 return ret;
3712
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003713 if (em->compress_type) {
3714 csum_offset = 0;
3715 csum_len = block_len;
3716 } else {
3717 csum_offset = mod_start - em->start;
3718 csum_len = mod_len;
3719 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003720
Josef Bacik70c8a912012-10-11 16:54:30 -04003721 /* block start is already adjusted for the file extent offset. */
3722 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3723 em->block_start + csum_offset,
3724 em->block_start + csum_offset +
3725 csum_len - 1, &ordered_sums, 0);
3726 if (ret)
3727 return ret;
3728
3729 while (!list_empty(&ordered_sums)) {
3730 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3731 struct btrfs_ordered_sum,
3732 list);
3733 if (!ret)
3734 ret = btrfs_csum_file_blocks(trans, log, sums);
3735 list_del(&sums->list);
3736 kfree(sums);
3737 }
3738
3739 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003740}
3741
3742static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3743 struct btrfs_root *root,
3744 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08003745 struct btrfs_path *path,
3746 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003747{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003748 struct extent_map *em, *n;
3749 struct list_head extents;
3750 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3751 u64 test_gen;
3752 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003753 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003754
3755 INIT_LIST_HEAD(&extents);
3756
Josef Bacik5dc562c2012-08-17 13:14:17 -04003757 write_lock(&tree->lock);
3758 test_gen = root->fs_info->last_trans_committed;
3759
3760 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3761 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003762
3763 /*
3764 * Just an arbitrary number, this can be really CPU intensive
3765 * once we start getting a lot of extents, and really once we
3766 * have a bunch of extents we just want to commit since it will
3767 * be faster.
3768 */
3769 if (++num > 32768) {
3770 list_del_init(&tree->modified_extents);
3771 ret = -EFBIG;
3772 goto process;
3773 }
3774
Josef Bacik5dc562c2012-08-17 13:14:17 -04003775 if (em->generation <= test_gen)
3776 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003777 /* Need a ref to keep it from getting evicted from cache */
3778 atomic_inc(&em->refs);
3779 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003780 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003781 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003782 }
3783
3784 list_sort(NULL, &extents, extent_cmp);
3785
Josef Bacik2ab28f32012-10-12 15:27:49 -04003786process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003787 while (!list_empty(&extents)) {
3788 em = list_entry(extents.next, struct extent_map, list);
3789
3790 list_del_init(&em->list);
3791
3792 /*
3793 * If we had an error we just need to delete everybody from our
3794 * private list.
3795 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003796 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003797 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003798 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003799 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003800 }
3801
3802 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003803
Miao Xie827463c2014-01-14 20:31:51 +08003804 ret = log_one_extent(trans, inode, root, em, path, logged_list);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003805 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003806 clear_em_logging(tree, em);
3807 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003808 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003809 WARN_ON(!list_empty(&extents));
3810 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003811
Josef Bacik5dc562c2012-08-17 13:14:17 -04003812 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003813 return ret;
3814}
3815
Chris Masone02119d2008-09-05 16:13:11 -04003816/* log a single inode in the tree log.
3817 * At least one parent directory for this inode must exist in the tree
3818 * or be logged already.
3819 *
3820 * Any items from this inode changed by the current transaction are copied
3821 * to the log tree. An extra reference is taken on any extents in this
3822 * file, allowing us to avoid a whole pile of corner cases around logging
3823 * blocks that have been removed from the tree.
3824 *
3825 * See LOG_INODE_ALL and related defines for a description of what inode_only
3826 * does.
3827 *
3828 * This handles both files and directories.
3829 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003830static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003831 struct btrfs_root *root, struct inode *inode,
3832 int inode_only)
3833{
3834 struct btrfs_path *path;
3835 struct btrfs_path *dst_path;
3836 struct btrfs_key min_key;
3837 struct btrfs_key max_key;
3838 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003839 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08003840 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04003841 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003842 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003843 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003844 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003845 int ins_start_slot = 0;
3846 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003847 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003848 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003849
Chris Masone02119d2008-09-05 16:13:11 -04003850 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003851 if (!path)
3852 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003853 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003854 if (!dst_path) {
3855 btrfs_free_path(path);
3856 return -ENOMEM;
3857 }
Chris Masone02119d2008-09-05 16:13:11 -04003858
Li Zefan33345d012011-04-20 10:31:50 +08003859 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003860 min_key.type = BTRFS_INODE_ITEM_KEY;
3861 min_key.offset = 0;
3862
Li Zefan33345d012011-04-20 10:31:50 +08003863 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003864
Chris Mason12fcfd22009-03-24 10:24:20 -04003865
Josef Bacik5dc562c2012-08-17 13:14:17 -04003866 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003867 if (S_ISDIR(inode->i_mode) ||
3868 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3869 &BTRFS_I(inode)->runtime_flags) &&
3870 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003871 max_key.type = BTRFS_XATTR_ITEM_KEY;
3872 else
3873 max_key.type = (u8)-1;
3874 max_key.offset = (u64)-1;
3875
Josef Bacik94edf4a2012-09-25 14:56:25 -04003876 /* Only run delayed items if we are a dir or a new file */
3877 if (S_ISDIR(inode->i_mode) ||
3878 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3879 ret = btrfs_commit_inode_delayed_items(trans, inode);
3880 if (ret) {
3881 btrfs_free_path(path);
3882 btrfs_free_path(dst_path);
3883 return ret;
3884 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003885 }
3886
Chris Masone02119d2008-09-05 16:13:11 -04003887 mutex_lock(&BTRFS_I(inode)->log_mutex);
3888
Miao Xie827463c2014-01-14 20:31:51 +08003889 btrfs_get_logged_extents(inode, &logged_list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003890
Chris Masone02119d2008-09-05 16:13:11 -04003891 /*
3892 * a brute force approach to making sure we get the most uptodate
3893 * copies of everything.
3894 */
3895 if (S_ISDIR(inode->i_mode)) {
3896 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3897
3898 if (inode_only == LOG_INODE_EXISTS)
3899 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003900 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003901 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003902 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3903 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003904 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3905 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003906 ret = btrfs_truncate_inode_items(trans, log,
3907 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003908 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Josef Bacik6cfab852013-11-12 16:25:58 -05003909 &BTRFS_I(inode)->runtime_flags) ||
3910 inode_only == LOG_INODE_EXISTS) {
Josef Bacika95249b2012-10-11 16:17:34 -04003911 if (inode_only == LOG_INODE_ALL)
3912 fast_search = true;
3913 max_key.type = BTRFS_XATTR_ITEM_KEY;
3914 ret = drop_objectid_items(trans, log, path, ino,
3915 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003916 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003917 if (inode_only == LOG_INODE_ALL)
3918 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003919 ret = log_inode_item(trans, log, dst_path, inode);
3920 if (ret) {
3921 err = ret;
3922 goto out_unlock;
3923 }
3924 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003925 }
Josef Bacika95249b2012-10-11 16:17:34 -04003926
Chris Masone02119d2008-09-05 16:13:11 -04003927 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003928 if (ret) {
3929 err = ret;
3930 goto out_unlock;
3931 }
Chris Masone02119d2008-09-05 16:13:11 -04003932 path->keep_locks = 1;
3933
Chris Masond3977122009-01-05 21:25:51 -05003934 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003935 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003936 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003937 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003938 if (ret != 0)
3939 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003940again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003941 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003942 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003943 break;
3944 if (min_key.type > max_key.type)
3945 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003946
Chris Masone02119d2008-09-05 16:13:11 -04003947 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003948 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3949 ins_nr++;
3950 goto next_slot;
3951 } else if (!ins_nr) {
3952 ins_start_slot = path->slots[0];
3953 ins_nr = 1;
3954 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003955 }
3956
Josef Bacik16e75492013-10-22 12:18:51 -04003957 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3958 ins_start_slot, ins_nr, inode_only);
3959 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003960 err = ret;
3961 goto out_unlock;
Josef Bacik16e75492013-10-22 12:18:51 -04003962 } if (ret) {
3963 ins_nr = 0;
3964 btrfs_release_path(path);
3965 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003966 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003967 ins_nr = 1;
3968 ins_start_slot = path->slots[0];
3969next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003970
Chris Mason3a5f1d42008-09-11 15:53:37 -04003971 nritems = btrfs_header_nritems(path->nodes[0]);
3972 path->slots[0]++;
3973 if (path->slots[0] < nritems) {
3974 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3975 path->slots[0]);
3976 goto again;
3977 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003978 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04003979 ret = copy_items(trans, inode, dst_path, path,
3980 &last_extent, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003981 ins_nr, inode_only);
Josef Bacik16e75492013-10-22 12:18:51 -04003982 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003983 err = ret;
3984 goto out_unlock;
3985 }
Josef Bacik16e75492013-10-22 12:18:51 -04003986 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003987 ins_nr = 0;
3988 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003989 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003990
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003991 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04003992 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003993 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04003994 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003995 min_key.offset = 0;
3996 } else {
Chris Masone02119d2008-09-05 16:13:11 -04003997 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003998 }
Chris Masone02119d2008-09-05 16:13:11 -04003999 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004000 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004001 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4002 ins_start_slot, ins_nr, inode_only);
4003 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004004 err = ret;
4005 goto out_unlock;
4006 }
Josef Bacik16e75492013-10-22 12:18:51 -04004007 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004008 ins_nr = 0;
4009 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004010
Josef Bacika95249b2012-10-11 16:17:34 -04004011log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004012 btrfs_release_path(path);
4013 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004014 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004015 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
4016 &logged_list);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004017 if (ret) {
4018 err = ret;
4019 goto out_unlock;
4020 }
Josef Bacikd006a042013-11-12 20:54:09 -05004021 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004022 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4023 struct extent_map *em, *n;
4024
Miao Xiebbe14262012-11-01 07:34:54 +00004025 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06004026 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
4027 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00004028 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004029 }
4030
Chris Mason9623f9a2008-09-11 17:42:42 -04004031 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004032 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004033 if (ret) {
4034 err = ret;
4035 goto out_unlock;
4036 }
Chris Masone02119d2008-09-05 16:13:11 -04004037 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04004038 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06004039 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004040out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004041 if (unlikely(err))
4042 btrfs_put_logged_extents(&logged_list);
4043 else
4044 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004045 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4046
4047 btrfs_free_path(path);
4048 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004049 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004050}
4051
Chris Mason12fcfd22009-03-24 10:24:20 -04004052/*
4053 * follow the dentry parent pointers up the chain and see if any
4054 * of the directories in it require a full commit before they can
4055 * be logged. Returns zero if nothing special needs to be done or 1 if
4056 * a full commit is required.
4057 */
4058static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4059 struct inode *inode,
4060 struct dentry *parent,
4061 struct super_block *sb,
4062 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004063{
Chris Mason12fcfd22009-03-24 10:24:20 -04004064 int ret = 0;
4065 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004066 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004067 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004068
Chris Masonaf4176b2009-03-24 10:24:31 -04004069 /*
4070 * for regular files, if its inode is already on disk, we don't
4071 * have to worry about the parents at all. This is because
4072 * we can use the last_unlink_trans field to record renames
4073 * and other fun in this file.
4074 */
4075 if (S_ISREG(inode->i_mode) &&
4076 BTRFS_I(inode)->generation <= last_committed &&
4077 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4078 goto out;
4079
Chris Mason12fcfd22009-03-24 10:24:20 -04004080 if (!S_ISDIR(inode->i_mode)) {
4081 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4082 goto out;
4083 inode = parent->d_inode;
4084 }
4085
4086 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004087 /*
4088 * If we are logging a directory then we start with our inode,
4089 * not our parents inode, so we need to skipp setting the
4090 * logged_trans so that further down in the log code we don't
4091 * think this inode has already been logged.
4092 */
4093 if (inode != orig_inode)
4094 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004095 smp_mb();
4096
4097 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4098 root = BTRFS_I(inode)->root;
4099
4100 /*
4101 * make sure any commits to the log are forced
4102 * to be full commits
4103 */
4104 root->fs_info->last_trans_log_full_commit =
4105 trans->transid;
4106 ret = 1;
4107 break;
4108 }
4109
4110 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4111 break;
4112
Yan, Zheng76dda932009-09-21 16:00:26 -04004113 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004114 break;
4115
Josef Bacik6a912212010-11-20 09:48:00 +00004116 parent = dget_parent(parent);
4117 dput(old_parent);
4118 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04004119 inode = parent->d_inode;
4120
4121 }
Josef Bacik6a912212010-11-20 09:48:00 +00004122 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004123out:
Chris Masone02119d2008-09-05 16:13:11 -04004124 return ret;
4125}
4126
4127/*
4128 * helper function around btrfs_log_inode to make sure newly created
4129 * parent directories also end up in the log. A minimal inode and backref
4130 * only logging is done of any parent directories that are older than
4131 * the last committed transaction
4132 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004133static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4134 struct btrfs_root *root, struct inode *inode,
Miao Xie8b050d32014-02-20 18:08:58 +08004135 struct dentry *parent, int exists_only,
4136 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004137{
Chris Mason12fcfd22009-03-24 10:24:20 -04004138 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04004139 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00004140 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04004141 int ret = 0;
4142 u64 last_committed = root->fs_info->last_trans_committed;
4143
4144 sb = inode->i_sb;
4145
Sage Weil3a5e1402009-04-02 16:49:40 -04004146 if (btrfs_test_opt(root, NOTREELOG)) {
4147 ret = 1;
4148 goto end_no_trans;
4149 }
4150
Chris Mason12fcfd22009-03-24 10:24:20 -04004151 if (root->fs_info->last_trans_log_full_commit >
4152 root->fs_info->last_trans_committed) {
4153 ret = 1;
4154 goto end_no_trans;
4155 }
4156
Yan, Zheng76dda932009-09-21 16:00:26 -04004157 if (root != BTRFS_I(inode)->root ||
4158 btrfs_root_refs(&root->root_item) == 0) {
4159 ret = 1;
4160 goto end_no_trans;
4161 }
4162
Chris Mason12fcfd22009-03-24 10:24:20 -04004163 ret = check_parent_dirs_for_sync(trans, inode, parent,
4164 sb, last_committed);
4165 if (ret)
4166 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04004167
Josef Bacik22ee6982012-05-29 16:57:49 -04004168 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04004169 ret = BTRFS_NO_LOG_SYNC;
4170 goto end_no_trans;
4171 }
4172
Miao Xie8b050d32014-02-20 18:08:58 +08004173 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004174 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08004175 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004176
4177 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004178 if (ret)
4179 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004180
Chris Masonaf4176b2009-03-24 10:24:31 -04004181 /*
4182 * for regular files, if its inode is already on disk, we don't
4183 * have to worry about the parents at all. This is because
4184 * we can use the last_unlink_trans field to record renames
4185 * and other fun in this file.
4186 */
4187 if (S_ISREG(inode->i_mode) &&
4188 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004189 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4190 ret = 0;
4191 goto end_trans;
4192 }
Chris Masonaf4176b2009-03-24 10:24:31 -04004193
4194 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05004195 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04004196 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04004197 break;
4198
Chris Mason12fcfd22009-03-24 10:24:20 -04004199 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004200 if (root != BTRFS_I(inode)->root)
4201 break;
4202
Chris Mason12fcfd22009-03-24 10:24:20 -04004203 if (BTRFS_I(inode)->generation >
4204 root->fs_info->last_trans_committed) {
4205 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004206 if (ret)
4207 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004208 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004209 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04004210 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04004211
Josef Bacik6a912212010-11-20 09:48:00 +00004212 parent = dget_parent(parent);
4213 dput(old_parent);
4214 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04004215 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004216 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004217end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004218 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004219 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004220 root->fs_info->last_trans_log_full_commit = trans->transid;
4221 ret = 1;
4222 }
Miao Xie8b050d32014-02-20 18:08:58 +08004223
4224 if (ret)
4225 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04004226 btrfs_end_log_trans(root);
4227end_no_trans:
4228 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004229}
4230
4231/*
4232 * it is not safe to log dentry if the chunk root has added new
4233 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4234 * If this returns 1, you must commit the transaction to safely get your
4235 * data on disk.
4236 */
4237int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08004238 struct btrfs_root *root, struct dentry *dentry,
4239 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004240{
Josef Bacik6a912212010-11-20 09:48:00 +00004241 struct dentry *parent = dget_parent(dentry);
4242 int ret;
4243
Miao Xie8b050d32014-02-20 18:08:58 +08004244 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
4245 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00004246 dput(parent);
4247
4248 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004249}
4250
4251/*
4252 * should be called during mount to recover any replay any log trees
4253 * from the FS
4254 */
4255int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4256{
4257 int ret;
4258 struct btrfs_path *path;
4259 struct btrfs_trans_handle *trans;
4260 struct btrfs_key key;
4261 struct btrfs_key found_key;
4262 struct btrfs_key tmp_key;
4263 struct btrfs_root *log;
4264 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4265 struct walk_control wc = {
4266 .process_func = process_one_buffer,
4267 .stage = 0,
4268 };
4269
Chris Masone02119d2008-09-05 16:13:11 -04004270 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004271 if (!path)
4272 return -ENOMEM;
4273
4274 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004275
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004276 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 if (IS_ERR(trans)) {
4278 ret = PTR_ERR(trans);
4279 goto error;
4280 }
Chris Masone02119d2008-09-05 16:13:11 -04004281
4282 wc.trans = trans;
4283 wc.pin = 1;
4284
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004285 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004286 if (ret) {
4287 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4288 "recovering log root tree.");
4289 goto error;
4290 }
Chris Masone02119d2008-09-05 16:13:11 -04004291
4292again:
4293 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4294 key.offset = (u64)-1;
4295 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4296
Chris Masond3977122009-01-05 21:25:51 -05004297 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004298 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299
4300 if (ret < 0) {
4301 btrfs_error(fs_info, ret,
4302 "Couldn't find tree log root.");
4303 goto error;
4304 }
Chris Masone02119d2008-09-05 16:13:11 -04004305 if (ret > 0) {
4306 if (path->slots[0] == 0)
4307 break;
4308 path->slots[0]--;
4309 }
4310 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4311 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004312 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004313 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4314 break;
4315
Miao Xiecb517ea2013-05-15 07:48:19 +00004316 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004317 if (IS_ERR(log)) {
4318 ret = PTR_ERR(log);
4319 btrfs_error(fs_info, ret,
4320 "Couldn't read tree log root.");
4321 goto error;
4322 }
Chris Masone02119d2008-09-05 16:13:11 -04004323
4324 tmp_key.objectid = found_key.offset;
4325 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4326 tmp_key.offset = (u64)-1;
4327
4328 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 if (IS_ERR(wc.replay_dest)) {
4330 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004331 free_extent_buffer(log->node);
4332 free_extent_buffer(log->commit_root);
4333 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004334 btrfs_error(fs_info, ret, "Couldn't read target root "
4335 "for tree log recovery.");
4336 goto error;
4337 }
Chris Masone02119d2008-09-05 16:13:11 -04004338
Yan Zheng07d400a2009-01-06 11:42:00 -05004339 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004340 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004341 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004342
Josef Bacikb50c6e22013-04-25 15:55:30 -04004343 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004344 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4345 path);
Chris Masone02119d2008-09-05 16:13:11 -04004346 }
Chris Masone02119d2008-09-05 16:13:11 -04004347
4348 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004349 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004350 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004351 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004352 kfree(log);
4353
Josef Bacikb50c6e22013-04-25 15:55:30 -04004354 if (ret)
4355 goto error;
4356
Chris Masone02119d2008-09-05 16:13:11 -04004357 if (found_key.offset == 0)
4358 break;
4359 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004360 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004361
4362 /* step one is to pin it all, step two is to replay just inodes */
4363 if (wc.pin) {
4364 wc.pin = 0;
4365 wc.process_func = replay_one_buffer;
4366 wc.stage = LOG_WALK_REPLAY_INODES;
4367 goto again;
4368 }
4369 /* step three is to replay everything */
4370 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4371 wc.stage++;
4372 goto again;
4373 }
4374
4375 btrfs_free_path(path);
4376
Josef Bacikabefa552013-04-24 16:40:05 -04004377 /* step 4: commit the transaction, which also unpins the blocks */
4378 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4379 if (ret)
4380 return ret;
4381
Chris Masone02119d2008-09-05 16:13:11 -04004382 free_extent_buffer(log_root_tree->node);
4383 log_root_tree->log_root = NULL;
4384 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004385 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004386
Josef Bacikabefa552013-04-24 16:40:05 -04004387 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004388error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004389 if (wc.trans)
4390 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004391 btrfs_free_path(path);
4392 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004393}
Chris Mason12fcfd22009-03-24 10:24:20 -04004394
4395/*
4396 * there are some corner cases where we want to force a full
4397 * commit instead of allowing a directory to be logged.
4398 *
4399 * They revolve around files there were unlinked from the directory, and
4400 * this function updates the parent directory so that a full commit is
4401 * properly done if it is fsync'd later after the unlinks are done.
4402 */
4403void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4404 struct inode *dir, struct inode *inode,
4405 int for_rename)
4406{
4407 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004408 * when we're logging a file, if it hasn't been renamed
4409 * or unlinked, and its inode is fully committed on disk,
4410 * we don't have to worry about walking up the directory chain
4411 * to log its parents.
4412 *
4413 * So, we use the last_unlink_trans field to put this transid
4414 * into the file. When the file is logged we check it and
4415 * don't log the parents if the file is fully on disk.
4416 */
4417 if (S_ISREG(inode->i_mode))
4418 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4419
4420 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004421 * if this directory was already logged any new
4422 * names for this file/dir will get recorded
4423 */
4424 smp_mb();
4425 if (BTRFS_I(dir)->logged_trans == trans->transid)
4426 return;
4427
4428 /*
4429 * if the inode we're about to unlink was logged,
4430 * the log will be properly updated for any new names
4431 */
4432 if (BTRFS_I(inode)->logged_trans == trans->transid)
4433 return;
4434
4435 /*
4436 * when renaming files across directories, if the directory
4437 * there we're unlinking from gets fsync'd later on, there's
4438 * no way to find the destination directory later and fsync it
4439 * properly. So, we have to be conservative and force commits
4440 * so the new name gets discovered.
4441 */
4442 if (for_rename)
4443 goto record;
4444
4445 /* we can safely do the unlink without any special recording */
4446 return;
4447
4448record:
4449 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4450}
4451
4452/*
4453 * Call this after adding a new name for a file and it will properly
4454 * update the log to reflect the new name.
4455 *
4456 * It will return zero if all goes well, and it will return 1 if a
4457 * full transaction commit is required.
4458 */
4459int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4460 struct inode *inode, struct inode *old_dir,
4461 struct dentry *parent)
4462{
4463 struct btrfs_root * root = BTRFS_I(inode)->root;
4464
4465 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004466 * this will force the logging code to walk the dentry chain
4467 * up for the file
4468 */
4469 if (S_ISREG(inode->i_mode))
4470 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4471
4472 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004473 * if this inode hasn't been logged and directory we're renaming it
4474 * from hasn't been logged, we don't need to log it
4475 */
4476 if (BTRFS_I(inode)->logged_trans <=
4477 root->fs_info->last_trans_committed &&
4478 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4479 root->fs_info->last_trans_committed))
4480 return 0;
4481
Miao Xie8b050d32014-02-20 18:08:58 +08004482 return btrfs_log_inode_parent(trans, root, inode, parent, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04004483}
4484