blob: ca960ad271fe908a294d91b0e3b30f306c89c208 [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,
139 struct btrfs_root *root)
140{
141 int ret;
Yan Zheng7237f182009-01-21 12:54:03 -0500142
143 mutex_lock(&root->log_mutex);
144 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400145 if (!root->log_start_pid) {
146 root->log_start_pid = current->pid;
147 root->log_multiple_pids = false;
148 } else if (root->log_start_pid != current->pid) {
149 root->log_multiple_pids = true;
150 }
151
Miao Xie2ecb7922012-09-06 04:04:27 -0600152 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500153 atomic_inc(&root->log_writers);
154 mutex_unlock(&root->log_mutex);
155 return 0;
156 }
Miao Xiee87ac132014-02-20 18:08:53 +0800157
158 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400159 mutex_lock(&root->fs_info->tree_log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800160 if (!root->fs_info->log_root_tree)
Chris Masone02119d2008-09-05 16:13:11 -0400161 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Miao Xiee87ac132014-02-20 18:08:53 +0800162 mutex_unlock(&root->fs_info->tree_log_mutex);
163 if (ret)
164 goto out;
165
166 if (!root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400167 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400168 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800169 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400170 }
Miao Xiee87ac132014-02-20 18:08:53 +0800171 root->log_multiple_pids = false;
172 root->log_start_pid = current->pid;
Miao Xie2ecb7922012-09-06 04:04:27 -0600173 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500174 atomic_inc(&root->log_writers);
Miao Xiee87ac132014-02-20 18:08:53 +0800175out:
Yan Zheng7237f182009-01-21 12:54:03 -0500176 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800177 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400178}
179
180/*
181 * returns 0 if there was a log transaction running and we were able
182 * to join, or returns -ENOENT if there were not transactions
183 * in progress
184 */
185static int join_running_log_trans(struct btrfs_root *root)
186{
187 int ret = -ENOENT;
188
189 smp_mb();
190 if (!root->log_root)
191 return -ENOENT;
192
Yan Zheng7237f182009-01-21 12:54:03 -0500193 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400194 if (root->log_root) {
195 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500196 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400197 }
Yan Zheng7237f182009-01-21 12:54:03 -0500198 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400199 return ret;
200}
201
202/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400203 * This either makes the current running log transaction wait
204 * until you call btrfs_end_log_trans() or it makes any future
205 * log transactions wait until you call btrfs_end_log_trans()
206 */
207int btrfs_pin_log_trans(struct btrfs_root *root)
208{
209 int ret = -ENOENT;
210
211 mutex_lock(&root->log_mutex);
212 atomic_inc(&root->log_writers);
213 mutex_unlock(&root->log_mutex);
214 return ret;
215}
216
217/*
Chris Masone02119d2008-09-05 16:13:11 -0400218 * indicate we're done making changes to the log tree
219 * and wake up anyone waiting to do a sync
220 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100221void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400222{
Yan Zheng7237f182009-01-21 12:54:03 -0500223 if (atomic_dec_and_test(&root->log_writers)) {
224 smp_mb();
225 if (waitqueue_active(&root->log_writer_wait))
226 wake_up(&root->log_writer_wait);
227 }
Chris Masone02119d2008-09-05 16:13:11 -0400228}
229
230
231/*
232 * the walk control struct is used to pass state down the chain when
233 * processing the log tree. The stage field tells us which part
234 * of the log tree processing we are currently doing. The others
235 * are state fields used for that specific part
236 */
237struct walk_control {
238 /* should we free the extent on disk when done? This is used
239 * at transaction commit time while freeing a log tree
240 */
241 int free;
242
243 /* should we write out the extent buffer? This is used
244 * while flushing the log tree to disk during a sync
245 */
246 int write;
247
248 /* should we wait for the extent buffer io to finish? Also used
249 * while flushing the log tree to disk for a sync
250 */
251 int wait;
252
253 /* pin only walk, we record which extents on disk belong to the
254 * log trees
255 */
256 int pin;
257
258 /* what stage of the replay code we're currently in */
259 int stage;
260
261 /* the root we are currently replaying */
262 struct btrfs_root *replay_dest;
263
264 /* the trans handle for the current replay */
265 struct btrfs_trans_handle *trans;
266
267 /* the function that gets used to process blocks we find in the
268 * tree. Note the extent_buffer might not be up to date when it is
269 * passed in, and it must be checked or read if you need the data
270 * inside it
271 */
272 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
273 struct walk_control *wc, u64 gen);
274};
275
276/*
277 * process_func used to pin down extents, write them or wait on them
278 */
279static int process_one_buffer(struct btrfs_root *log,
280 struct extent_buffer *eb,
281 struct walk_control *wc, u64 gen)
282{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400283 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400284
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400285 /*
286 * If this fs is mixed then we need to be able to process the leaves to
287 * pin down any logged extents, so we have to read the block.
288 */
289 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
290 ret = btrfs_read_buffer(eb, gen);
291 if (ret)
292 return ret;
293 }
294
Josef Bacikb50c6e22013-04-25 15:55:30 -0400295 if (wc->pin)
296 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
297 eb->start, eb->len);
298
299 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400300 if (wc->pin && btrfs_header_level(eb) == 0)
301 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400302 if (wc->write)
303 btrfs_write_tree_block(eb);
304 if (wc->wait)
305 btrfs_wait_tree_block_writeback(eb);
306 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400307 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400308}
309
310/*
311 * Item overwrite used by replay and tree logging. eb, slot and key all refer
312 * to the src data we are copying out.
313 *
314 * root is the tree we are copying into, and path is a scratch
315 * path for use in this function (it should be released on entry and
316 * will be released on exit).
317 *
318 * If the key is already in the destination tree the existing item is
319 * overwritten. If the existing item isn't big enough, it is extended.
320 * If it is too large, it is truncated.
321 *
322 * If the key isn't in the destination yet, a new item is inserted.
323 */
324static noinline int overwrite_item(struct btrfs_trans_handle *trans,
325 struct btrfs_root *root,
326 struct btrfs_path *path,
327 struct extent_buffer *eb, int slot,
328 struct btrfs_key *key)
329{
330 int ret;
331 u32 item_size;
332 u64 saved_i_size = 0;
333 int save_old_i_size = 0;
334 unsigned long src_ptr;
335 unsigned long dst_ptr;
336 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000337 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400338
339 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
340 overwrite_root = 1;
341
342 item_size = btrfs_item_size_nr(eb, slot);
343 src_ptr = btrfs_item_ptr_offset(eb, slot);
344
345 /* look for the key in the destination tree */
346 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000347 if (ret < 0)
348 return ret;
349
Chris Masone02119d2008-09-05 16:13:11 -0400350 if (ret == 0) {
351 char *src_copy;
352 char *dst_copy;
353 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
354 path->slots[0]);
355 if (dst_size != item_size)
356 goto insert;
357
358 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200359 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400360 return 0;
361 }
362 dst_copy = kmalloc(item_size, GFP_NOFS);
363 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000364 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200365 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000366 kfree(dst_copy);
367 kfree(src_copy);
368 return -ENOMEM;
369 }
Chris Masone02119d2008-09-05 16:13:11 -0400370
371 read_extent_buffer(eb, src_copy, src_ptr, item_size);
372
373 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
374 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
375 item_size);
376 ret = memcmp(dst_copy, src_copy, item_size);
377
378 kfree(dst_copy);
379 kfree(src_copy);
380 /*
381 * they have the same contents, just return, this saves
382 * us from cowing blocks in the destination tree and doing
383 * extra writes that may not have been done by a previous
384 * sync
385 */
386 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200387 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400388 return 0;
389 }
390
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000391 /*
392 * We need to load the old nbytes into the inode so when we
393 * replay the extents we've logged we get the right nbytes.
394 */
395 if (inode_item) {
396 struct btrfs_inode_item *item;
397 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400398 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000399
400 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
401 struct btrfs_inode_item);
402 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
403 item = btrfs_item_ptr(eb, slot,
404 struct btrfs_inode_item);
405 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400406
407 /*
408 * If this is a directory we need to reset the i_size to
409 * 0 so that we can set it up properly when replaying
410 * the rest of the items in this log.
411 */
412 mode = btrfs_inode_mode(eb, item);
413 if (S_ISDIR(mode))
414 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000415 }
416 } else if (inode_item) {
417 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400418 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000419
420 /*
421 * New inode, set nbytes to 0 so that the nbytes comes out
422 * properly when we replay the extents.
423 */
424 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
425 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400426
427 /*
428 * If this is a directory we need to reset the i_size to 0 so
429 * that we can set it up properly when replaying the rest of
430 * the items in this log.
431 */
432 mode = btrfs_inode_mode(eb, item);
433 if (S_ISDIR(mode))
434 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400435 }
436insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200437 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400438 /* try to insert the key into the destination tree */
439 ret = btrfs_insert_empty_item(trans, root, path,
440 key, item_size);
441
442 /* make sure any existing item is the correct size */
443 if (ret == -EEXIST) {
444 u32 found_size;
445 found_size = btrfs_item_size_nr(path->nodes[0],
446 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100447 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000448 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100449 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000450 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100451 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400452 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400453 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400454 }
455 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
456 path->slots[0]);
457
458 /* don't overwrite an existing inode if the generation number
459 * was logged as zero. This is done when the tree logging code
460 * is just logging an inode to make sure it exists after recovery.
461 *
462 * Also, don't overwrite i_size on directories during replay.
463 * log replay inserts and removes directory items based on the
464 * state of the tree found in the subvolume, and i_size is modified
465 * as it goes
466 */
467 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
468 struct btrfs_inode_item *src_item;
469 struct btrfs_inode_item *dst_item;
470
471 src_item = (struct btrfs_inode_item *)src_ptr;
472 dst_item = (struct btrfs_inode_item *)dst_ptr;
473
474 if (btrfs_inode_generation(eb, src_item) == 0)
475 goto no_copy;
476
477 if (overwrite_root &&
478 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
479 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
480 save_old_i_size = 1;
481 saved_i_size = btrfs_inode_size(path->nodes[0],
482 dst_item);
483 }
484 }
485
486 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
487 src_ptr, item_size);
488
489 if (save_old_i_size) {
490 struct btrfs_inode_item *dst_item;
491 dst_item = (struct btrfs_inode_item *)dst_ptr;
492 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
493 }
494
495 /* make sure the generation is filled in */
496 if (key->type == BTRFS_INODE_ITEM_KEY) {
497 struct btrfs_inode_item *dst_item;
498 dst_item = (struct btrfs_inode_item *)dst_ptr;
499 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
500 btrfs_set_inode_generation(path->nodes[0], dst_item,
501 trans->transid);
502 }
503 }
504no_copy:
505 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200506 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400507 return 0;
508}
509
510/*
511 * simple helper to read an inode off the disk from a given root
512 * This can only be called for subvolume roots and not for the log
513 */
514static noinline struct inode *read_one_inode(struct btrfs_root *root,
515 u64 objectid)
516{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400517 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400518 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400519
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400520 key.objectid = objectid;
521 key.type = BTRFS_INODE_ITEM_KEY;
522 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000523 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400524 if (IS_ERR(inode)) {
525 inode = NULL;
526 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400527 iput(inode);
528 inode = NULL;
529 }
530 return inode;
531}
532
533/* replays a single extent in 'eb' at 'slot' with 'key' into the
534 * subvolume 'root'. path is released on entry and should be released
535 * on exit.
536 *
537 * extents in the log tree have not been allocated out of the extent
538 * tree yet. So, this completes the allocation, taking a reference
539 * as required if the extent already exists or creating a new extent
540 * if it isn't in the extent allocation tree yet.
541 *
542 * The extent is inserted into the file, dropping any existing extents
543 * from the file that overlap the new one.
544 */
545static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
546 struct btrfs_root *root,
547 struct btrfs_path *path,
548 struct extent_buffer *eb, int slot,
549 struct btrfs_key *key)
550{
551 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400552 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400553 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000554 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400555 struct btrfs_file_extent_item *item;
556 struct inode *inode = NULL;
557 unsigned long size;
558 int ret = 0;
559
560 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
561 found_type = btrfs_file_extent_type(eb, item);
562
Yan Zhengd899e052008-10-30 14:25:28 -0400563 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000564 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
565 nbytes = btrfs_file_extent_num_bytes(eb, item);
566 extent_end = start + nbytes;
567
568 /*
569 * We don't add to the inodes nbytes if we are prealloc or a
570 * hole.
571 */
572 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
573 nbytes = 0;
574 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800575 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000576 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000577 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400578 } else {
579 ret = 0;
580 goto out;
581 }
582
583 inode = read_one_inode(root, key->objectid);
584 if (!inode) {
585 ret = -EIO;
586 goto out;
587 }
588
589 /*
590 * first check to see if we already have this extent in the
591 * file. This must be done before the btrfs_drop_extents run
592 * so we don't try to drop this extent.
593 */
Li Zefan33345d012011-04-20 10:31:50 +0800594 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400595 start, 0);
596
Yan Zhengd899e052008-10-30 14:25:28 -0400597 if (ret == 0 &&
598 (found_type == BTRFS_FILE_EXTENT_REG ||
599 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400600 struct btrfs_file_extent_item cmp1;
601 struct btrfs_file_extent_item cmp2;
602 struct btrfs_file_extent_item *existing;
603 struct extent_buffer *leaf;
604
605 leaf = path->nodes[0];
606 existing = btrfs_item_ptr(leaf, path->slots[0],
607 struct btrfs_file_extent_item);
608
609 read_extent_buffer(eb, &cmp1, (unsigned long)item,
610 sizeof(cmp1));
611 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
612 sizeof(cmp2));
613
614 /*
615 * we already have a pointer to this exact extent,
616 * we don't have to do anything
617 */
618 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200619 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400620 goto out;
621 }
622 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200623 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400624
625 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400626 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400627 if (ret)
628 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400629
Yan Zheng07d400a2009-01-06 11:42:00 -0500630 if (found_type == BTRFS_FILE_EXTENT_REG ||
631 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500633 unsigned long dest_offset;
634 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400635
Yan Zheng07d400a2009-01-06 11:42:00 -0500636 ret = btrfs_insert_empty_item(trans, root, path, key,
637 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400638 if (ret)
639 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500640 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
641 path->slots[0]);
642 copy_extent_buffer(path->nodes[0], eb, dest_offset,
643 (unsigned long)item, sizeof(*item));
644
645 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
646 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
647 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400648 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500649
650 if (ins.objectid > 0) {
651 u64 csum_start;
652 u64 csum_end;
653 LIST_HEAD(ordered_sums);
654 /*
655 * is this extent already allocated in the extent
656 * allocation tree? If so, just add a reference
657 */
658 ret = btrfs_lookup_extent(root, ins.objectid,
659 ins.offset);
660 if (ret == 0) {
661 ret = btrfs_inc_extent_ref(trans, root,
662 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400663 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200664 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400665 if (ret)
666 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500667 } else {
668 /*
669 * insert the extent pointer in the extent
670 * allocation tree
671 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 ret = btrfs_alloc_logged_file_extent(trans,
673 root, root->root_key.objectid,
674 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400675 if (ret)
676 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500677 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200678 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500679
680 if (btrfs_file_extent_compression(eb, item)) {
681 csum_start = ins.objectid;
682 csum_end = csum_start + ins.offset;
683 } else {
684 csum_start = ins.objectid +
685 btrfs_file_extent_offset(eb, item);
686 csum_end = csum_start +
687 btrfs_file_extent_num_bytes(eb, item);
688 }
689
690 ret = btrfs_lookup_csums_range(root->log_root,
691 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100692 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400693 if (ret)
694 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500695 while (!list_empty(&ordered_sums)) {
696 struct btrfs_ordered_sum *sums;
697 sums = list_entry(ordered_sums.next,
698 struct btrfs_ordered_sum,
699 list);
Josef Bacik36508602013-04-25 16:23:32 -0400700 if (!ret)
701 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500702 root->fs_info->csum_root,
703 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500704 list_del(&sums->list);
705 kfree(sums);
706 }
Josef Bacik36508602013-04-25 16:23:32 -0400707 if (ret)
708 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500709 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200710 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500711 }
712 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
713 /* inline extents are easy, we just overwrite them */
714 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400715 if (ret)
716 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500717 }
718
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000719 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600720 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400721out:
722 if (inode)
723 iput(inode);
724 return ret;
725}
726
727/*
728 * when cleaning up conflicts between the directory names in the
729 * subvolume, directory names in the log and directory names in the
730 * inode back references, we may have to unlink inodes from directories.
731 *
732 * This is a helper function to do the unlink of a specific directory
733 * item
734 */
735static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
736 struct btrfs_root *root,
737 struct btrfs_path *path,
738 struct inode *dir,
739 struct btrfs_dir_item *di)
740{
741 struct inode *inode;
742 char *name;
743 int name_len;
744 struct extent_buffer *leaf;
745 struct btrfs_key location;
746 int ret;
747
748 leaf = path->nodes[0];
749
750 btrfs_dir_item_key_to_cpu(leaf, di, &location);
751 name_len = btrfs_dir_name_len(leaf, di);
752 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000753 if (!name)
754 return -ENOMEM;
755
Chris Masone02119d2008-09-05 16:13:11 -0400756 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200757 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400758
759 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000760 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400761 ret = -EIO;
762 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000763 }
Chris Masone02119d2008-09-05 16:13:11 -0400764
Yan Zhengec051c02009-01-05 15:43:42 -0500765 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400766 if (ret)
767 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400768
Chris Masone02119d2008-09-05 16:13:11 -0400769 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400770 if (ret)
771 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100772 else
773 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400774out:
775 kfree(name);
776 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400777 return ret;
778}
779
780/*
781 * helper function to see if a given name and sequence number found
782 * in an inode back reference are already in a directory and correctly
783 * point to this inode
784 */
785static noinline int inode_in_dir(struct btrfs_root *root,
786 struct btrfs_path *path,
787 u64 dirid, u64 objectid, u64 index,
788 const char *name, int name_len)
789{
790 struct btrfs_dir_item *di;
791 struct btrfs_key location;
792 int match = 0;
793
794 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
795 index, name, name_len, 0);
796 if (di && !IS_ERR(di)) {
797 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
798 if (location.objectid != objectid)
799 goto out;
800 } else
801 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200802 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400803
804 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
805 if (di && !IS_ERR(di)) {
806 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
807 if (location.objectid != objectid)
808 goto out;
809 } else
810 goto out;
811 match = 1;
812out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200813 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400814 return match;
815}
816
817/*
818 * helper function to check a log tree for a named back reference in
819 * an inode. This is used to decide if a back reference that is
820 * found in the subvolume conflicts with what we find in the log.
821 *
822 * inode backreferences may have multiple refs in a single item,
823 * during replay we process one reference at a time, and we don't
824 * want to delete valid links to a file from the subvolume if that
825 * link is also in the log.
826 */
827static noinline int backref_in_log(struct btrfs_root *log,
828 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700829 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400830 char *name, int namelen)
831{
832 struct btrfs_path *path;
833 struct btrfs_inode_ref *ref;
834 unsigned long ptr;
835 unsigned long ptr_end;
836 unsigned long name_ptr;
837 int found_name_len;
838 int item_size;
839 int ret;
840 int match = 0;
841
842 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000843 if (!path)
844 return -ENOMEM;
845
Chris Masone02119d2008-09-05 16:13:11 -0400846 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
847 if (ret != 0)
848 goto out;
849
Chris Masone02119d2008-09-05 16:13:11 -0400850 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700851
852 if (key->type == BTRFS_INODE_EXTREF_KEY) {
853 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
854 name, namelen, NULL))
855 match = 1;
856
857 goto out;
858 }
859
860 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400861 ptr_end = ptr + item_size;
862 while (ptr < ptr_end) {
863 ref = (struct btrfs_inode_ref *)ptr;
864 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
865 if (found_name_len == namelen) {
866 name_ptr = (unsigned long)(ref + 1);
867 ret = memcmp_extent_buffer(path->nodes[0], name,
868 name_ptr, namelen);
869 if (ret == 0) {
870 match = 1;
871 goto out;
872 }
873 }
874 ptr = (unsigned long)(ref + 1) + found_name_len;
875 }
876out:
877 btrfs_free_path(path);
878 return match;
879}
880
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700881static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
882 struct btrfs_root *root,
883 struct btrfs_path *path,
884 struct btrfs_root *log_root,
885 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700886 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700887 u64 inode_objectid, u64 parent_objectid,
888 u64 ref_index, char *name, int namelen,
889 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700890{
891 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700892 char *victim_name;
893 int victim_name_len;
894 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700895 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700896 struct btrfs_key search_key;
897 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700898
Mark Fashehf1863732012-08-08 11:32:27 -0700899again:
900 /* Search old style refs */
901 search_key.objectid = inode_objectid;
902 search_key.type = BTRFS_INODE_REF_KEY;
903 search_key.offset = parent_objectid;
904 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700905 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700906 struct btrfs_inode_ref *victim_ref;
907 unsigned long ptr;
908 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700909
910 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700911
912 /* are we trying to overwrite a back ref for the root directory
913 * if so, just jump out, we're done
914 */
Mark Fashehf1863732012-08-08 11:32:27 -0700915 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700916 return 1;
917
918 /* check all the names in this back reference to see
919 * if they are in the log. if so, we allow them to stay
920 * otherwise they must be unlinked as a conflict
921 */
922 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
923 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
924 while (ptr < ptr_end) {
925 victim_ref = (struct btrfs_inode_ref *)ptr;
926 victim_name_len = btrfs_inode_ref_name_len(leaf,
927 victim_ref);
928 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400929 if (!victim_name)
930 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700931
932 read_extent_buffer(leaf, victim_name,
933 (unsigned long)(victim_ref + 1),
934 victim_name_len);
935
Mark Fashehf1863732012-08-08 11:32:27 -0700936 if (!backref_in_log(log_root, &search_key,
937 parent_objectid,
938 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700939 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700940 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700941 btrfs_release_path(path);
942
943 ret = btrfs_unlink_inode(trans, root, dir,
944 inode, victim_name,
945 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700946 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400947 if (ret)
948 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100949 ret = btrfs_run_delayed_items(trans, root);
950 if (ret)
951 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700952 *search_done = 1;
953 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700954 }
955 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700956
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700957 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
958 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700959
960 /*
961 * NOTE: we have searched root tree and checked the
962 * coresponding ref, it does not need to check again.
963 */
964 *search_done = 1;
965 }
966 btrfs_release_path(path);
967
Mark Fashehf1863732012-08-08 11:32:27 -0700968 /* Same search but for extended refs */
969 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
970 inode_objectid, parent_objectid, 0,
971 0);
972 if (!IS_ERR_OR_NULL(extref)) {
973 u32 item_size;
974 u32 cur_offset = 0;
975 unsigned long base;
976 struct inode *victim_parent;
977
978 leaf = path->nodes[0];
979
980 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
981 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
982
983 while (cur_offset < item_size) {
984 extref = (struct btrfs_inode_extref *)base + cur_offset;
985
986 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
987
988 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
989 goto next;
990
991 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400992 if (!victim_name)
993 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -0700994 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
995 victim_name_len);
996
997 search_key.objectid = inode_objectid;
998 search_key.type = BTRFS_INODE_EXTREF_KEY;
999 search_key.offset = btrfs_extref_hash(parent_objectid,
1000 victim_name,
1001 victim_name_len);
1002 ret = 0;
1003 if (!backref_in_log(log_root, &search_key,
1004 parent_objectid, victim_name,
1005 victim_name_len)) {
1006 ret = -ENOENT;
1007 victim_parent = read_one_inode(root,
1008 parent_objectid);
1009 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001010 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001011 btrfs_release_path(path);
1012
1013 ret = btrfs_unlink_inode(trans, root,
1014 victim_parent,
1015 inode,
1016 victim_name,
1017 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001018 if (!ret)
1019 ret = btrfs_run_delayed_items(
1020 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001021 }
Mark Fashehf1863732012-08-08 11:32:27 -07001022 iput(victim_parent);
1023 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001024 if (ret)
1025 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001026 *search_done = 1;
1027 goto again;
1028 }
1029 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001030 if (ret)
1031 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001032next:
1033 cur_offset += victim_name_len + sizeof(*extref);
1034 }
1035 *search_done = 1;
1036 }
1037 btrfs_release_path(path);
1038
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001039 /* look for a conflicting sequence number */
1040 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001041 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001042 if (di && !IS_ERR(di)) {
1043 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001044 if (ret)
1045 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001046 }
1047 btrfs_release_path(path);
1048
1049 /* look for a conflicing name */
1050 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1051 name, namelen, 0);
1052 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 return 0;
1060}
Chris Masone02119d2008-09-05 16:13:11 -04001061
Mark Fashehf1863732012-08-08 11:32:27 -07001062static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1063 u32 *namelen, char **name, u64 *index,
1064 u64 *parent_objectid)
1065{
1066 struct btrfs_inode_extref *extref;
1067
1068 extref = (struct btrfs_inode_extref *)ref_ptr;
1069
1070 *namelen = btrfs_inode_extref_name_len(eb, extref);
1071 *name = kmalloc(*namelen, GFP_NOFS);
1072 if (*name == NULL)
1073 return -ENOMEM;
1074
1075 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1076 *namelen);
1077
1078 *index = btrfs_inode_extref_index(eb, extref);
1079 if (parent_objectid)
1080 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1081
1082 return 0;
1083}
1084
1085static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1086 u32 *namelen, char **name, u64 *index)
1087{
1088 struct btrfs_inode_ref *ref;
1089
1090 ref = (struct btrfs_inode_ref *)ref_ptr;
1091
1092 *namelen = btrfs_inode_ref_name_len(eb, ref);
1093 *name = kmalloc(*namelen, GFP_NOFS);
1094 if (*name == NULL)
1095 return -ENOMEM;
1096
1097 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1098
1099 *index = btrfs_inode_ref_index(eb, ref);
1100
1101 return 0;
1102}
1103
Chris Masone02119d2008-09-05 16:13:11 -04001104/*
1105 * replay one inode back reference item found in the log tree.
1106 * eb, slot and key refer to the buffer and key found in the log tree.
1107 * root is the destination we are replaying into, and path is for temp
1108 * use by this function. (it should be released on return).
1109 */
1110static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1111 struct btrfs_root *root,
1112 struct btrfs_root *log,
1113 struct btrfs_path *path,
1114 struct extent_buffer *eb, int slot,
1115 struct btrfs_key *key)
1116{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001117 struct inode *dir = NULL;
1118 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001119 unsigned long ref_ptr;
1120 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001121 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001122 int namelen;
1123 int ret;
liuboc622ae62011-03-26 08:01:12 -04001124 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001125 int log_ref_ver = 0;
1126 u64 parent_objectid;
1127 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001128 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001129 int ref_struct_size;
1130
1131 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1132 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1133
1134 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1135 struct btrfs_inode_extref *r;
1136
1137 ref_struct_size = sizeof(struct btrfs_inode_extref);
1138 log_ref_ver = 1;
1139 r = (struct btrfs_inode_extref *)ref_ptr;
1140 parent_objectid = btrfs_inode_extref_parent(eb, r);
1141 } else {
1142 ref_struct_size = sizeof(struct btrfs_inode_ref);
1143 parent_objectid = key->offset;
1144 }
1145 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001146
Chris Masone02119d2008-09-05 16:13:11 -04001147 /*
1148 * it is possible that we didn't log all the parent directories
1149 * for a given inode. If we don't find the dir, just don't
1150 * copy the back ref in. The link count fixup code will take
1151 * care of the rest
1152 */
Mark Fashehf1863732012-08-08 11:32:27 -07001153 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001154 if (!dir) {
1155 ret = -ENOENT;
1156 goto out;
1157 }
Chris Masone02119d2008-09-05 16:13:11 -04001158
Mark Fashehf1863732012-08-08 11:32:27 -07001159 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001160 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001161 ret = -EIO;
1162 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001163 }
Chris Masone02119d2008-09-05 16:13:11 -04001164
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001165 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001166 if (log_ref_ver) {
1167 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1168 &ref_index, &parent_objectid);
1169 /*
1170 * parent object can change from one array
1171 * item to another.
1172 */
1173 if (!dir)
1174 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001175 if (!dir) {
1176 ret = -ENOENT;
1177 goto out;
1178 }
Mark Fashehf1863732012-08-08 11:32:27 -07001179 } else {
1180 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1181 &ref_index);
1182 }
1183 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001184 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001185
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001186 /* if we already have a perfect match, we're done */
1187 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001188 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001189 /*
1190 * look for a conflicting back reference in the
1191 * metadata. if we find one we have to unlink that name
1192 * of the file before we add our new link. Later on, we
1193 * overwrite any existing back reference, and we don't
1194 * want to create dangling pointers in the directory.
1195 */
Chris Masone02119d2008-09-05 16:13:11 -04001196
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001197 if (!search_done) {
1198 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001199 dir, inode, eb,
1200 inode_objectid,
1201 parent_objectid,
1202 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001203 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001204 if (ret) {
1205 if (ret == 1)
1206 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001207 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001208 }
Chris Masone02119d2008-09-05 16:13:11 -04001209 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001210
1211 /* insert our name */
1212 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001213 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001214 if (ret)
1215 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001216
1217 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001218 }
liuboc622ae62011-03-26 08:01:12 -04001219
Mark Fashehf1863732012-08-08 11:32:27 -07001220 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001221 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001222 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001223 if (log_ref_ver) {
1224 iput(dir);
1225 dir = NULL;
1226 }
Chris Masone02119d2008-09-05 16:13:11 -04001227 }
Chris Masone02119d2008-09-05 16:13:11 -04001228
1229 /* finally write the back reference in the inode */
1230 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001231out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001232 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001233 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001234 iput(dir);
1235 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001236 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001237}
1238
Yan, Zhengc71bf092009-11-12 09:34:40 +00001239static int insert_orphan_item(struct btrfs_trans_handle *trans,
1240 struct btrfs_root *root, u64 offset)
1241{
1242 int ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08001243 ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
1244 offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001245 if (ret > 0)
1246 ret = btrfs_insert_orphan_item(trans, root, offset);
1247 return ret;
1248}
1249
Mark Fashehf1863732012-08-08 11:32:27 -07001250static int count_inode_extrefs(struct btrfs_root *root,
1251 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001252{
Mark Fashehf1863732012-08-08 11:32:27 -07001253 int ret = 0;
1254 int name_len;
1255 unsigned int nlink = 0;
1256 u32 item_size;
1257 u32 cur_offset = 0;
1258 u64 inode_objectid = btrfs_ino(inode);
1259 u64 offset = 0;
1260 unsigned long ptr;
1261 struct btrfs_inode_extref *extref;
1262 struct extent_buffer *leaf;
1263
1264 while (1) {
1265 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1266 &extref, &offset);
1267 if (ret)
1268 break;
1269
1270 leaf = path->nodes[0];
1271 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1272 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1273
1274 while (cur_offset < item_size) {
1275 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1276 name_len = btrfs_inode_extref_name_len(leaf, extref);
1277
1278 nlink++;
1279
1280 cur_offset += name_len + sizeof(*extref);
1281 }
1282
1283 offset++;
1284 btrfs_release_path(path);
1285 }
1286 btrfs_release_path(path);
1287
1288 if (ret < 0)
1289 return ret;
1290 return nlink;
1291}
1292
1293static int count_inode_refs(struct btrfs_root *root,
1294 struct inode *inode, struct btrfs_path *path)
1295{
Chris Masone02119d2008-09-05 16:13:11 -04001296 int ret;
1297 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001298 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001299 unsigned long ptr;
1300 unsigned long ptr_end;
1301 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001302 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001303
Li Zefan33345d012011-04-20 10:31:50 +08001304 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001305 key.type = BTRFS_INODE_REF_KEY;
1306 key.offset = (u64)-1;
1307
Chris Masond3977122009-01-05 21:25:51 -05001308 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001309 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1310 if (ret < 0)
1311 break;
1312 if (ret > 0) {
1313 if (path->slots[0] == 0)
1314 break;
1315 path->slots[0]--;
1316 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001317process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001318 btrfs_item_key_to_cpu(path->nodes[0], &key,
1319 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001320 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001321 key.type != BTRFS_INODE_REF_KEY)
1322 break;
1323 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1324 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1325 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001326 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001327 struct btrfs_inode_ref *ref;
1328
1329 ref = (struct btrfs_inode_ref *)ptr;
1330 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1331 ref);
1332 ptr = (unsigned long)(ref + 1) + name_len;
1333 nlink++;
1334 }
1335
1336 if (key.offset == 0)
1337 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001338 if (path->slots[0] > 0) {
1339 path->slots[0]--;
1340 goto process_slot;
1341 }
Chris Masone02119d2008-09-05 16:13:11 -04001342 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001343 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001344 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001345 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001346
1347 return nlink;
1348}
1349
1350/*
1351 * There are a few corners where the link count of the file can't
1352 * be properly maintained during replay. So, instead of adding
1353 * lots of complexity to the log code, we just scan the backrefs
1354 * for any file that has been through replay.
1355 *
1356 * The scan will update the link count on the inode to reflect the
1357 * number of back refs found. If it goes down to zero, the iput
1358 * will free the inode.
1359 */
1360static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1361 struct btrfs_root *root,
1362 struct inode *inode)
1363{
1364 struct btrfs_path *path;
1365 int ret;
1366 u64 nlink = 0;
1367 u64 ino = btrfs_ino(inode);
1368
1369 path = btrfs_alloc_path();
1370 if (!path)
1371 return -ENOMEM;
1372
1373 ret = count_inode_refs(root, inode, path);
1374 if (ret < 0)
1375 goto out;
1376
1377 nlink = ret;
1378
1379 ret = count_inode_extrefs(root, inode, path);
1380 if (ret == -ENOENT)
1381 ret = 0;
1382
1383 if (ret < 0)
1384 goto out;
1385
1386 nlink += ret;
1387
1388 ret = 0;
1389
Chris Masone02119d2008-09-05 16:13:11 -04001390 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001391 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001392 btrfs_update_inode(trans, root, inode);
1393 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001394 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001395
Yan, Zhengc71bf092009-11-12 09:34:40 +00001396 if (inode->i_nlink == 0) {
1397 if (S_ISDIR(inode->i_mode)) {
1398 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001399 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001400 if (ret)
1401 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001402 }
Li Zefan33345d012011-04-20 10:31:50 +08001403 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001404 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001405
Mark Fashehf1863732012-08-08 11:32:27 -07001406out:
1407 btrfs_free_path(path);
1408 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001409}
1410
1411static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1412 struct btrfs_root *root,
1413 struct btrfs_path *path)
1414{
1415 int ret;
1416 struct btrfs_key key;
1417 struct inode *inode;
1418
1419 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1420 key.type = BTRFS_ORPHAN_ITEM_KEY;
1421 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001422 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001423 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1424 if (ret < 0)
1425 break;
1426
1427 if (ret == 1) {
1428 if (path->slots[0] == 0)
1429 break;
1430 path->slots[0]--;
1431 }
1432
1433 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1434 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1435 key.type != BTRFS_ORPHAN_ITEM_KEY)
1436 break;
1437
1438 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001439 if (ret)
1440 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001441
David Sterbab3b4aa72011-04-21 01:20:15 +02001442 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001443 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001444 if (!inode)
1445 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001446
1447 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001448 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001449 if (ret)
1450 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001451
Chris Mason12fcfd22009-03-24 10:24:20 -04001452 /*
1453 * fixup on a directory may create new entries,
1454 * make sure we always look for the highset possible
1455 * offset
1456 */
1457 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001458 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001459 ret = 0;
1460out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001461 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001462 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001463}
1464
1465
1466/*
1467 * record a given inode in the fixup dir so we can check its link
1468 * count when replay is done. The link count is incremented here
1469 * so the inode won't go away until we check it
1470 */
1471static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1472 struct btrfs_root *root,
1473 struct btrfs_path *path,
1474 u64 objectid)
1475{
1476 struct btrfs_key key;
1477 int ret = 0;
1478 struct inode *inode;
1479
1480 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001481 if (!inode)
1482 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001483
1484 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1485 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1486 key.offset = objectid;
1487
1488 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1489
David Sterbab3b4aa72011-04-21 01:20:15 +02001490 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001491 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001492 if (!inode->i_nlink)
1493 set_nlink(inode, 1);
1494 else
Zach Brown8b558c52013-10-16 12:10:34 -07001495 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001496 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001497 } else if (ret == -EEXIST) {
1498 ret = 0;
1499 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001500 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001501 }
1502 iput(inode);
1503
1504 return ret;
1505}
1506
1507/*
1508 * when replaying the log for a directory, we only insert names
1509 * for inodes that actually exist. This means an fsync on a directory
1510 * does not implicitly fsync all the new files in it
1511 */
1512static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1513 struct btrfs_root *root,
1514 struct btrfs_path *path,
1515 u64 dirid, u64 index,
1516 char *name, int name_len, u8 type,
1517 struct btrfs_key *location)
1518{
1519 struct inode *inode;
1520 struct inode *dir;
1521 int ret;
1522
1523 inode = read_one_inode(root, location->objectid);
1524 if (!inode)
1525 return -ENOENT;
1526
1527 dir = read_one_inode(root, dirid);
1528 if (!dir) {
1529 iput(inode);
1530 return -EIO;
1531 }
Josef Bacikd5554382013-09-11 14:17:00 -04001532
Chris Masone02119d2008-09-05 16:13:11 -04001533 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1534
1535 /* FIXME, put inode into FIXUP list */
1536
1537 iput(inode);
1538 iput(dir);
1539 return ret;
1540}
1541
1542/*
1543 * take a single entry in a log directory item and replay it into
1544 * the subvolume.
1545 *
1546 * if a conflicting item exists in the subdirectory already,
1547 * the inode it points to is unlinked and put into the link count
1548 * fix up tree.
1549 *
1550 * If a name from the log points to a file or directory that does
1551 * not exist in the FS, it is skipped. fsyncs on directories
1552 * do not force down inodes inside that directory, just changes to the
1553 * names or unlinks in a directory.
1554 */
1555static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1556 struct btrfs_root *root,
1557 struct btrfs_path *path,
1558 struct extent_buffer *eb,
1559 struct btrfs_dir_item *di,
1560 struct btrfs_key *key)
1561{
1562 char *name;
1563 int name_len;
1564 struct btrfs_dir_item *dst_di;
1565 struct btrfs_key found_key;
1566 struct btrfs_key log_key;
1567 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001568 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001569 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001570 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001571 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001572
1573 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001574 if (!dir)
1575 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001576
1577 name_len = btrfs_dir_name_len(eb, di);
1578 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001579 if (!name) {
1580 ret = -ENOMEM;
1581 goto out;
1582 }
liubo2a29edc2011-01-26 06:22:08 +00001583
Chris Masone02119d2008-09-05 16:13:11 -04001584 log_type = btrfs_dir_type(eb, di);
1585 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1586 name_len);
1587
1588 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001589 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1590 if (exists == 0)
1591 exists = 1;
1592 else
1593 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001594 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001595
Chris Masone02119d2008-09-05 16:13:11 -04001596 if (key->type == BTRFS_DIR_ITEM_KEY) {
1597 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1598 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001599 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001600 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1601 key->objectid,
1602 key->offset, name,
1603 name_len, 1);
1604 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001605 /* Corruption */
1606 ret = -EINVAL;
1607 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001608 }
David Sterbac7040052011-04-19 18:00:01 +02001609 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001610 /* we need a sequence number to insert, so we only
1611 * do inserts for the BTRFS_DIR_INDEX_KEY types
1612 */
1613 if (key->type != BTRFS_DIR_INDEX_KEY)
1614 goto out;
1615 goto insert;
1616 }
1617
1618 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1619 /* the existing item matches the logged item */
1620 if (found_key.objectid == log_key.objectid &&
1621 found_key.type == log_key.type &&
1622 found_key.offset == log_key.offset &&
1623 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1624 goto out;
1625 }
1626
1627 /*
1628 * don't drop the conflicting directory entry if the inode
1629 * for the new entry doesn't exist
1630 */
Chris Mason4bef0842008-09-08 11:18:08 -04001631 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001632 goto out;
1633
Chris Masone02119d2008-09-05 16:13:11 -04001634 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001635 if (ret)
1636 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001637
1638 if (key->type == BTRFS_DIR_INDEX_KEY)
1639 goto insert;
1640out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001641 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001642 if (!ret && update_size) {
1643 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1644 ret = btrfs_update_inode(trans, root, dir);
1645 }
Chris Masone02119d2008-09-05 16:13:11 -04001646 kfree(name);
1647 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001648 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001649
1650insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001651 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001652 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1653 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001654 if (ret && ret != -ENOENT)
1655 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001656 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001657 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001658 goto out;
1659}
1660
1661/*
1662 * find all the names in a directory item and reconcile them into
1663 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1664 * one name in a directory item, but the same code gets used for
1665 * both directory index types
1666 */
1667static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1668 struct btrfs_root *root,
1669 struct btrfs_path *path,
1670 struct extent_buffer *eb, int slot,
1671 struct btrfs_key *key)
1672{
1673 int ret;
1674 u32 item_size = btrfs_item_size_nr(eb, slot);
1675 struct btrfs_dir_item *di;
1676 int name_len;
1677 unsigned long ptr;
1678 unsigned long ptr_end;
1679
1680 ptr = btrfs_item_ptr_offset(eb, slot);
1681 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001682 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001683 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001684 if (verify_dir_item(root, eb, di))
1685 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001686 name_len = btrfs_dir_name_len(eb, di);
1687 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001688 if (ret)
1689 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001690 ptr = (unsigned long)(di + 1);
1691 ptr += name_len;
1692 }
1693 return 0;
1694}
1695
1696/*
1697 * directory replay has two parts. There are the standard directory
1698 * items in the log copied from the subvolume, and range items
1699 * created in the log while the subvolume was logged.
1700 *
1701 * The range items tell us which parts of the key space the log
1702 * is authoritative for. During replay, if a key in the subvolume
1703 * directory is in a logged range item, but not actually in the log
1704 * that means it was deleted from the directory before the fsync
1705 * and should be removed.
1706 */
1707static noinline int find_dir_range(struct btrfs_root *root,
1708 struct btrfs_path *path,
1709 u64 dirid, int key_type,
1710 u64 *start_ret, u64 *end_ret)
1711{
1712 struct btrfs_key key;
1713 u64 found_end;
1714 struct btrfs_dir_log_item *item;
1715 int ret;
1716 int nritems;
1717
1718 if (*start_ret == (u64)-1)
1719 return 1;
1720
1721 key.objectid = dirid;
1722 key.type = key_type;
1723 key.offset = *start_ret;
1724
1725 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1726 if (ret < 0)
1727 goto out;
1728 if (ret > 0) {
1729 if (path->slots[0] == 0)
1730 goto out;
1731 path->slots[0]--;
1732 }
1733 if (ret != 0)
1734 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1735
1736 if (key.type != key_type || key.objectid != dirid) {
1737 ret = 1;
1738 goto next;
1739 }
1740 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1741 struct btrfs_dir_log_item);
1742 found_end = btrfs_dir_log_end(path->nodes[0], item);
1743
1744 if (*start_ret >= key.offset && *start_ret <= found_end) {
1745 ret = 0;
1746 *start_ret = key.offset;
1747 *end_ret = found_end;
1748 goto out;
1749 }
1750 ret = 1;
1751next:
1752 /* check the next slot in the tree to see if it is a valid item */
1753 nritems = btrfs_header_nritems(path->nodes[0]);
1754 if (path->slots[0] >= nritems) {
1755 ret = btrfs_next_leaf(root, path);
1756 if (ret)
1757 goto out;
1758 } else {
1759 path->slots[0]++;
1760 }
1761
1762 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1763
1764 if (key.type != key_type || key.objectid != dirid) {
1765 ret = 1;
1766 goto out;
1767 }
1768 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1769 struct btrfs_dir_log_item);
1770 found_end = btrfs_dir_log_end(path->nodes[0], item);
1771 *start_ret = key.offset;
1772 *end_ret = found_end;
1773 ret = 0;
1774out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001775 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001776 return ret;
1777}
1778
1779/*
1780 * this looks for a given directory item in the log. If the directory
1781 * item is not in the log, the item is removed and the inode it points
1782 * to is unlinked
1783 */
1784static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root,
1786 struct btrfs_root *log,
1787 struct btrfs_path *path,
1788 struct btrfs_path *log_path,
1789 struct inode *dir,
1790 struct btrfs_key *dir_key)
1791{
1792 int ret;
1793 struct extent_buffer *eb;
1794 int slot;
1795 u32 item_size;
1796 struct btrfs_dir_item *di;
1797 struct btrfs_dir_item *log_di;
1798 int name_len;
1799 unsigned long ptr;
1800 unsigned long ptr_end;
1801 char *name;
1802 struct inode *inode;
1803 struct btrfs_key location;
1804
1805again:
1806 eb = path->nodes[0];
1807 slot = path->slots[0];
1808 item_size = btrfs_item_size_nr(eb, slot);
1809 ptr = btrfs_item_ptr_offset(eb, slot);
1810 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001811 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001812 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001813 if (verify_dir_item(root, eb, di)) {
1814 ret = -EIO;
1815 goto out;
1816 }
1817
Chris Masone02119d2008-09-05 16:13:11 -04001818 name_len = btrfs_dir_name_len(eb, di);
1819 name = kmalloc(name_len, GFP_NOFS);
1820 if (!name) {
1821 ret = -ENOMEM;
1822 goto out;
1823 }
1824 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1825 name_len);
1826 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001827 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001828 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1829 dir_key->objectid,
1830 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001831 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001832 log_di = btrfs_lookup_dir_index_item(trans, log,
1833 log_path,
1834 dir_key->objectid,
1835 dir_key->offset,
1836 name, name_len, 0);
1837 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001838 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001839 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001840 btrfs_release_path(path);
1841 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001842 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001843 if (!inode) {
1844 kfree(name);
1845 return -EIO;
1846 }
Chris Masone02119d2008-09-05 16:13:11 -04001847
1848 ret = link_to_fixup_dir(trans, root,
1849 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001850 if (ret) {
1851 kfree(name);
1852 iput(inode);
1853 goto out;
1854 }
1855
Zach Brown8b558c52013-10-16 12:10:34 -07001856 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001857 ret = btrfs_unlink_inode(trans, root, dir, inode,
1858 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001859 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001860 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001861 kfree(name);
1862 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001863 if (ret)
1864 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001865
1866 /* there might still be more names under this key
1867 * check and repeat if required
1868 */
1869 ret = btrfs_search_slot(NULL, root, dir_key, path,
1870 0, 0);
1871 if (ret == 0)
1872 goto again;
1873 ret = 0;
1874 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001875 } else if (IS_ERR(log_di)) {
1876 kfree(name);
1877 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001878 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001879 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001880 kfree(name);
1881
1882 ptr = (unsigned long)(di + 1);
1883 ptr += name_len;
1884 }
1885 ret = 0;
1886out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001887 btrfs_release_path(path);
1888 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001889 return ret;
1890}
1891
1892/*
1893 * deletion replay happens before we copy any new directory items
1894 * out of the log or out of backreferences from inodes. It
1895 * scans the log to find ranges of keys that log is authoritative for,
1896 * and then scans the directory to find items in those ranges that are
1897 * not present in the log.
1898 *
1899 * Anything we don't find in the log is unlinked and removed from the
1900 * directory.
1901 */
1902static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1903 struct btrfs_root *root,
1904 struct btrfs_root *log,
1905 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001906 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001907{
1908 u64 range_start;
1909 u64 range_end;
1910 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1911 int ret = 0;
1912 struct btrfs_key dir_key;
1913 struct btrfs_key found_key;
1914 struct btrfs_path *log_path;
1915 struct inode *dir;
1916
1917 dir_key.objectid = dirid;
1918 dir_key.type = BTRFS_DIR_ITEM_KEY;
1919 log_path = btrfs_alloc_path();
1920 if (!log_path)
1921 return -ENOMEM;
1922
1923 dir = read_one_inode(root, dirid);
1924 /* it isn't an error if the inode isn't there, that can happen
1925 * because we replay the deletes before we copy in the inode item
1926 * from the log
1927 */
1928 if (!dir) {
1929 btrfs_free_path(log_path);
1930 return 0;
1931 }
1932again:
1933 range_start = 0;
1934 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001935 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001936 if (del_all)
1937 range_end = (u64)-1;
1938 else {
1939 ret = find_dir_range(log, path, dirid, key_type,
1940 &range_start, &range_end);
1941 if (ret != 0)
1942 break;
1943 }
Chris Masone02119d2008-09-05 16:13:11 -04001944
1945 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001946 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001947 int nritems;
1948 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1949 0, 0);
1950 if (ret < 0)
1951 goto out;
1952
1953 nritems = btrfs_header_nritems(path->nodes[0]);
1954 if (path->slots[0] >= nritems) {
1955 ret = btrfs_next_leaf(root, path);
1956 if (ret)
1957 break;
1958 }
1959 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1960 path->slots[0]);
1961 if (found_key.objectid != dirid ||
1962 found_key.type != dir_key.type)
1963 goto next_type;
1964
1965 if (found_key.offset > range_end)
1966 break;
1967
1968 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001969 log_path, dir,
1970 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001971 if (ret)
1972 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001973 if (found_key.offset == (u64)-1)
1974 break;
1975 dir_key.offset = found_key.offset + 1;
1976 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001977 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001978 if (range_end == (u64)-1)
1979 break;
1980 range_start = range_end + 1;
1981 }
1982
1983next_type:
1984 ret = 0;
1985 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1986 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1987 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001988 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001989 goto again;
1990 }
1991out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001992 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001993 btrfs_free_path(log_path);
1994 iput(dir);
1995 return ret;
1996}
1997
1998/*
1999 * the process_func used to replay items from the log tree. This
2000 * gets called in two different stages. The first stage just looks
2001 * for inodes and makes sure they are all copied into the subvolume.
2002 *
2003 * The second stage copies all the other item types from the log into
2004 * the subvolume. The two stage approach is slower, but gets rid of
2005 * lots of complexity around inodes referencing other inodes that exist
2006 * only in the log (references come from either directory items or inode
2007 * back refs).
2008 */
2009static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2010 struct walk_control *wc, u64 gen)
2011{
2012 int nritems;
2013 struct btrfs_path *path;
2014 struct btrfs_root *root = wc->replay_dest;
2015 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002016 int level;
2017 int i;
2018 int ret;
2019
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002020 ret = btrfs_read_buffer(eb, gen);
2021 if (ret)
2022 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002023
2024 level = btrfs_header_level(eb);
2025
2026 if (level != 0)
2027 return 0;
2028
2029 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002030 if (!path)
2031 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002032
2033 nritems = btrfs_header_nritems(eb);
2034 for (i = 0; i < nritems; i++) {
2035 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002036
2037 /* inode keys are done during the first stage */
2038 if (key.type == BTRFS_INODE_ITEM_KEY &&
2039 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002040 struct btrfs_inode_item *inode_item;
2041 u32 mode;
2042
2043 inode_item = btrfs_item_ptr(eb, i,
2044 struct btrfs_inode_item);
2045 mode = btrfs_inode_mode(eb, inode_item);
2046 if (S_ISDIR(mode)) {
2047 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002048 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002049 if (ret)
2050 break;
Chris Masone02119d2008-09-05 16:13:11 -04002051 }
2052 ret = overwrite_item(wc->trans, root, path,
2053 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002054 if (ret)
2055 break;
Chris Masone02119d2008-09-05 16:13:11 -04002056
Yan, Zhengc71bf092009-11-12 09:34:40 +00002057 /* for regular files, make sure corresponding
2058 * orhpan item exist. extents past the new EOF
2059 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002060 */
2061 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002062 ret = insert_orphan_item(wc->trans, root,
2063 key.objectid);
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
Chris Masone02119d2008-09-05 16:13:11 -04002068 ret = link_to_fixup_dir(wc->trans, root,
2069 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002070 if (ret)
2071 break;
Chris Masone02119d2008-09-05 16:13:11 -04002072 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002073
2074 if (key.type == BTRFS_DIR_INDEX_KEY &&
2075 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2076 ret = replay_one_dir_item(wc->trans, root, path,
2077 eb, i, &key);
2078 if (ret)
2079 break;
2080 }
2081
Chris Masone02119d2008-09-05 16:13:11 -04002082 if (wc->stage < LOG_WALK_REPLAY_ALL)
2083 continue;
2084
2085 /* these keys are simply copied */
2086 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2087 ret = overwrite_item(wc->trans, root, path,
2088 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002089 if (ret)
2090 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002091 } else if (key.type == BTRFS_INODE_REF_KEY ||
2092 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002093 ret = add_inode_ref(wc->trans, root, log, path,
2094 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002095 if (ret && ret != -ENOENT)
2096 break;
2097 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002098 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2099 ret = replay_one_extent(wc->trans, root, path,
2100 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002101 if (ret)
2102 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002103 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002104 ret = replay_one_dir_item(wc->trans, root, path,
2105 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002106 if (ret)
2107 break;
Chris Masone02119d2008-09-05 16:13:11 -04002108 }
2109 }
2110 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002111 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002112}
2113
Chris Masond3977122009-01-05 21:25:51 -05002114static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002115 struct btrfs_root *root,
2116 struct btrfs_path *path, int *level,
2117 struct walk_control *wc)
2118{
2119 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002120 u64 bytenr;
2121 u64 ptr_gen;
2122 struct extent_buffer *next;
2123 struct extent_buffer *cur;
2124 struct extent_buffer *parent;
2125 u32 blocksize;
2126 int ret = 0;
2127
2128 WARN_ON(*level < 0);
2129 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2130
Chris Masond3977122009-01-05 21:25:51 -05002131 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002132 WARN_ON(*level < 0);
2133 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2134 cur = path->nodes[*level];
2135
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302136 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002137
2138 if (path->slots[*level] >=
2139 btrfs_header_nritems(cur))
2140 break;
2141
2142 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2143 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2144 blocksize = btrfs_level_size(root, *level - 1);
2145
2146 parent = path->nodes[*level];
2147 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002148
2149 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002150 if (!next)
2151 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002152
Chris Masone02119d2008-09-05 16:13:11 -04002153 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002154 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002155 if (ret) {
2156 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002157 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002158 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002159
Chris Masone02119d2008-09-05 16:13:11 -04002160 path->slots[*level]++;
2161 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002162 ret = btrfs_read_buffer(next, ptr_gen);
2163 if (ret) {
2164 free_extent_buffer(next);
2165 return ret;
2166 }
Chris Masone02119d2008-09-05 16:13:11 -04002167
Josef Bacik681ae502013-10-07 15:11:00 -04002168 if (trans) {
2169 btrfs_tree_lock(next);
2170 btrfs_set_lock_blocking(next);
2171 clean_tree_block(trans, root, next);
2172 btrfs_wait_tree_block_writeback(next);
2173 btrfs_tree_unlock(next);
2174 }
Chris Masone02119d2008-09-05 16:13:11 -04002175
Chris Masone02119d2008-09-05 16:13:11 -04002176 WARN_ON(root_owner !=
2177 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002178 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002179 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002180 if (ret) {
2181 free_extent_buffer(next);
2182 return ret;
2183 }
Chris Masone02119d2008-09-05 16:13:11 -04002184 }
2185 free_extent_buffer(next);
2186 continue;
2187 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002188 ret = btrfs_read_buffer(next, ptr_gen);
2189 if (ret) {
2190 free_extent_buffer(next);
2191 return ret;
2192 }
Chris Masone02119d2008-09-05 16:13:11 -04002193
2194 WARN_ON(*level <= 0);
2195 if (path->nodes[*level-1])
2196 free_extent_buffer(path->nodes[*level-1]);
2197 path->nodes[*level-1] = next;
2198 *level = btrfs_header_level(next);
2199 path->slots[*level] = 0;
2200 cond_resched();
2201 }
2202 WARN_ON(*level < 0);
2203 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2204
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002205 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002206
2207 cond_resched();
2208 return 0;
2209}
2210
Chris Masond3977122009-01-05 21:25:51 -05002211static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002212 struct btrfs_root *root,
2213 struct btrfs_path *path, int *level,
2214 struct walk_control *wc)
2215{
2216 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002217 int i;
2218 int slot;
2219 int ret;
2220
Chris Masond3977122009-01-05 21:25:51 -05002221 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002222 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002223 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002224 path->slots[i]++;
2225 *level = i;
2226 WARN_ON(*level == 0);
2227 return 0;
2228 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002229 struct extent_buffer *parent;
2230 if (path->nodes[*level] == root->node)
2231 parent = path->nodes[*level];
2232 else
2233 parent = path->nodes[*level + 1];
2234
2235 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002236 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002237 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002238 if (ret)
2239 return ret;
2240
Chris Masone02119d2008-09-05 16:13:11 -04002241 if (wc->free) {
2242 struct extent_buffer *next;
2243
2244 next = path->nodes[*level];
2245
Josef Bacik681ae502013-10-07 15:11:00 -04002246 if (trans) {
2247 btrfs_tree_lock(next);
2248 btrfs_set_lock_blocking(next);
2249 clean_tree_block(trans, root, next);
2250 btrfs_wait_tree_block_writeback(next);
2251 btrfs_tree_unlock(next);
2252 }
Chris Masone02119d2008-09-05 16:13:11 -04002253
Chris Masone02119d2008-09-05 16:13:11 -04002254 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002255 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002256 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002257 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002258 if (ret)
2259 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002260 }
2261 free_extent_buffer(path->nodes[*level]);
2262 path->nodes[*level] = NULL;
2263 *level = i + 1;
2264 }
2265 }
2266 return 1;
2267}
2268
2269/*
2270 * drop the reference count on the tree rooted at 'snap'. This traverses
2271 * the tree freeing any blocks that have a ref count of zero after being
2272 * decremented.
2273 */
2274static int walk_log_tree(struct btrfs_trans_handle *trans,
2275 struct btrfs_root *log, struct walk_control *wc)
2276{
2277 int ret = 0;
2278 int wret;
2279 int level;
2280 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002281 int orig_level;
2282
2283 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002284 if (!path)
2285 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002286
2287 level = btrfs_header_level(log->node);
2288 orig_level = level;
2289 path->nodes[level] = log->node;
2290 extent_buffer_get(log->node);
2291 path->slots[level] = 0;
2292
Chris Masond3977122009-01-05 21:25:51 -05002293 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002294 wret = walk_down_log_tree(trans, log, path, &level, wc);
2295 if (wret > 0)
2296 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002297 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002298 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299 goto out;
2300 }
Chris Masone02119d2008-09-05 16:13:11 -04002301
2302 wret = walk_up_log_tree(trans, log, path, &level, wc);
2303 if (wret > 0)
2304 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002305 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002306 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002307 goto out;
2308 }
Chris Masone02119d2008-09-05 16:13:11 -04002309 }
2310
2311 /* was the root node processed? if not, catch it here */
2312 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002313 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002314 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002315 if (ret)
2316 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002317 if (wc->free) {
2318 struct extent_buffer *next;
2319
2320 next = path->nodes[orig_level];
2321
Josef Bacik681ae502013-10-07 15:11:00 -04002322 if (trans) {
2323 btrfs_tree_lock(next);
2324 btrfs_set_lock_blocking(next);
2325 clean_tree_block(trans, log, next);
2326 btrfs_wait_tree_block_writeback(next);
2327 btrfs_tree_unlock(next);
2328 }
Chris Masone02119d2008-09-05 16:13:11 -04002329
Chris Masone02119d2008-09-05 16:13:11 -04002330 WARN_ON(log->root_key.objectid !=
2331 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002332 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002333 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002334 if (ret)
2335 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002336 }
2337 }
2338
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002339out:
Chris Masone02119d2008-09-05 16:13:11 -04002340 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002341 return ret;
2342}
2343
Yan Zheng7237f182009-01-21 12:54:03 -05002344/*
2345 * helper function to update the item for a given subvolumes log root
2346 * in the tree of log roots
2347 */
2348static int update_log_root(struct btrfs_trans_handle *trans,
2349 struct btrfs_root *log)
2350{
2351 int ret;
2352
2353 if (log->log_transid == 1) {
2354 /* insert root item on the first sync */
2355 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2356 &log->root_key, &log->root_item);
2357 } else {
2358 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2359 &log->root_key, &log->root_item);
2360 }
2361 return ret;
2362}
2363
Chris Mason12fcfd22009-03-24 10:24:20 -04002364static int wait_log_commit(struct btrfs_trans_handle *trans,
2365 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002366{
2367 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002368 int index = transid % 2;
Miao Xie48cab2e2014-02-20 18:08:52 +08002369 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002370
Yan Zheng7237f182009-01-21 12:54:03 -05002371 /*
2372 * we only allow two pending log transactions at a time,
2373 * so we know that if ours is more than 2 older than the
2374 * current transaction, we're done
2375 */
Chris Masone02119d2008-09-05 16:13:11 -04002376 do {
Miao Xie48cab2e2014-02-20 18:08:52 +08002377 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2378 trans->transid) {
2379 ret = -EAGAIN;
2380 break;
2381 }
2382
Yan Zheng7237f182009-01-21 12:54:03 -05002383 prepare_to_wait(&root->log_commit_wait[index],
2384 &wait, TASK_UNINTERRUPTIBLE);
2385 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002386
Miao Xie48cab2e2014-02-20 18:08:52 +08002387 if (root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002388 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002389 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002390
Yan Zheng7237f182009-01-21 12:54:03 -05002391 finish_wait(&root->log_commit_wait[index], &wait);
2392 mutex_lock(&root->log_mutex);
Miao Xie48cab2e2014-02-20 18:08:52 +08002393 } while (root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002394 atomic_read(&root->log_commit[index]));
Miao Xie48cab2e2014-02-20 18:08:52 +08002395
2396 return ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002397}
2398
Jeff Mahoney143bede2012-03-01 14:56:26 +01002399static void wait_for_writer(struct btrfs_trans_handle *trans,
2400 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002401{
2402 DEFINE_WAIT(wait);
Miao Xie5c902ba2014-02-20 18:08:51 +08002403 while (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) !=
Jan Kara6dd70ce2012-01-26 15:01:11 -05002404 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002405 prepare_to_wait(&root->log_writer_wait,
2406 &wait, TASK_UNINTERRUPTIBLE);
2407 mutex_unlock(&root->log_mutex);
Miao Xie5c902ba2014-02-20 18:08:51 +08002408 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) !=
Chris Mason12fcfd22009-03-24 10:24:20 -04002409 trans->transid && 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
2416/*
2417 * btrfs_sync_log does sends a given tree log down to the disk and
2418 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002419 * you know that any inodes previously logged are safely on disk only
2420 * if it returns 0.
2421 *
2422 * Any other return value means you need to call btrfs_commit_transaction.
2423 * Some of the edge cases for fsyncing directories that have had unlinks
2424 * or renames done in the past mean that sometimes the only safe
2425 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2426 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002427 */
2428int btrfs_sync_log(struct btrfs_trans_handle *trans,
2429 struct btrfs_root *root)
2430{
Yan Zheng7237f182009-01-21 12:54:03 -05002431 int index1;
2432 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002433 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002434 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002435 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002436 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002437 unsigned long log_transid = 0;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002438 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002439
Yan Zheng7237f182009-01-21 12:54:03 -05002440 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002441 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002442 index1 = root->log_transid % 2;
2443 if (atomic_read(&root->log_commit[index1])) {
Miao Xie48cab2e2014-02-20 18:08:52 +08002444 ret = wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002445 mutex_unlock(&root->log_mutex);
Miao Xie48cab2e2014-02-20 18:08:52 +08002446 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002447 }
Yan Zheng7237f182009-01-21 12:54:03 -05002448 atomic_set(&root->log_commit[index1], 1);
2449
2450 /* wait for previous tree log sync to complete */
2451 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002452 wait_log_commit(trans, root, root->log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002453
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002454 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002455 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002456 /* when we're on an ssd, just kick the log commit out */
2457 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002458 mutex_unlock(&root->log_mutex);
2459 schedule_timeout_uninterruptible(1);
2460 mutex_lock(&root->log_mutex);
2461 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002462 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002463 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002464 break;
2465 }
Chris Masond0c803c2008-09-11 16:17:57 -04002466
Chris Mason12fcfd22009-03-24 10:24:20 -04002467 /* bail out if we need to do a full commit */
Miao Xie5c902ba2014-02-20 18:08:51 +08002468 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2469 trans->transid) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002470 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002471 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002472 mutex_unlock(&root->log_mutex);
2473 goto out;
2474 }
2475
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002476 if (log_transid % 2 == 0)
2477 mark = EXTENT_DIRTY;
2478 else
2479 mark = EXTENT_NEW;
2480
Chris Mason690587d2009-10-13 13:29:19 -04002481 /* we start IO on all the marked extents here, but we don't actually
2482 * wait for them until later.
2483 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002484 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002485 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002486 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002487 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002488 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002489 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002490 mutex_unlock(&root->log_mutex);
2491 goto out;
2492 }
Yan Zheng7237f182009-01-21 12:54:03 -05002493
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002494 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002495
Yan Zheng7237f182009-01-21 12:54:03 -05002496 root->log_transid++;
2497 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002498 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002499 smp_mb();
2500 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002501 * IO has been started, blocks of the log tree have WRITTEN flag set
2502 * in their headers. new modifications of the log will be written to
2503 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002504 */
2505 mutex_unlock(&root->log_mutex);
2506
2507 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002508 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002509 atomic_inc(&log_root_tree->log_writers);
2510 mutex_unlock(&log_root_tree->log_mutex);
2511
2512 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002513
2514 mutex_lock(&log_root_tree->log_mutex);
2515 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2516 smp_mb();
2517 if (waitqueue_active(&log_root_tree->log_writer_wait))
2518 wake_up(&log_root_tree->log_writer_wait);
2519 }
2520
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002521 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002522 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002523 if (ret != -ENOSPC) {
2524 btrfs_abort_transaction(trans, root, ret);
2525 mutex_unlock(&log_root_tree->log_mutex);
2526 goto out;
2527 }
Miao Xie5c902ba2014-02-20 18:08:51 +08002528 ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) =
2529 trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002530 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002531 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002532 mutex_unlock(&log_root_tree->log_mutex);
2533 ret = -EAGAIN;
2534 goto out;
2535 }
2536
Yan Zheng7237f182009-01-21 12:54:03 -05002537 index2 = log_root_tree->log_transid % 2;
2538 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002539 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002540 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xie48cab2e2014-02-20 18:08:52 +08002541 ret = wait_log_commit(trans, log_root_tree,
2542 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002543 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002544 mutex_unlock(&log_root_tree->log_mutex);
2545 goto out;
2546 }
2547 atomic_set(&log_root_tree->log_commit[index2], 1);
2548
Chris Mason12fcfd22009-03-24 10:24:20 -04002549 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2550 wait_log_commit(trans, log_root_tree,
2551 log_root_tree->log_transid - 1);
2552 }
Yan Zheng7237f182009-01-21 12:54:03 -05002553
Chris Mason12fcfd22009-03-24 10:24:20 -04002554 wait_for_writer(trans, log_root_tree);
2555
2556 /*
2557 * now that we've moved on to the tree of log tree roots,
2558 * check the full commit flag again
2559 */
Miao Xie5c902ba2014-02-20 18:08:51 +08002560 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2561 trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002562 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002563 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002564 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002565 mutex_unlock(&log_root_tree->log_mutex);
2566 ret = -EAGAIN;
2567 goto out_wake_log_root;
2568 }
Yan Zheng7237f182009-01-21 12:54:03 -05002569
Miao Xiec6adc9c2013-05-28 10:05:39 +00002570 ret = btrfs_write_marked_extents(log_root_tree,
2571 &log_root_tree->dirty_log_pages,
2572 EXTENT_DIRTY | EXTENT_NEW);
2573 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002574 if (ret) {
2575 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002576 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002577 mutex_unlock(&log_root_tree->log_mutex);
2578 goto out_wake_log_root;
2579 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002580 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002581 btrfs_wait_marked_extents(log_root_tree,
2582 &log_root_tree->dirty_log_pages,
2583 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002584 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002585
David Sterba6c417612011-04-13 15:41:04 +02002586 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002587 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002588 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002589 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002590
Yan Zheng7237f182009-01-21 12:54:03 -05002591 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002592 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002593
2594 mutex_unlock(&log_root_tree->log_mutex);
2595
2596 /*
2597 * nobody else is going to jump in and write the the ctree
2598 * super here because the log_commit atomic below is protecting
2599 * us. We must be called with a transaction handle pinning
2600 * the running transaction open, so a full commit can't hop
2601 * in and cause problems either.
2602 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002603 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002604 if (ret) {
2605 btrfs_abort_transaction(trans, root, ret);
2606 goto out_wake_log_root;
2607 }
Yan Zheng7237f182009-01-21 12:54:03 -05002608
Chris Mason257c62e2009-10-13 13:21:08 -04002609 mutex_lock(&root->log_mutex);
2610 if (root->last_log_commit < log_transid)
2611 root->last_log_commit = log_transid;
2612 mutex_unlock(&root->log_mutex);
2613
Chris Mason12fcfd22009-03-24 10:24:20 -04002614out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002615 atomic_set(&log_root_tree->log_commit[index2], 0);
2616 smp_mb();
2617 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2618 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002619out:
Yan Zheng7237f182009-01-21 12:54:03 -05002620 atomic_set(&root->log_commit[index1], 0);
2621 smp_mb();
2622 if (waitqueue_active(&root->log_commit_wait[index1]))
2623 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002624 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002625}
2626
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002627static void free_log_tree(struct btrfs_trans_handle *trans,
2628 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002629{
2630 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002631 u64 start;
2632 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002633 struct walk_control wc = {
2634 .free = 1,
2635 .process_func = process_one_buffer
2636 };
2637
Josef Bacik681ae502013-10-07 15:11:00 -04002638 ret = walk_log_tree(trans, log, &wc);
2639 /* I don't think this can happen but just in case */
2640 if (ret)
2641 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002642
Chris Masond3977122009-01-05 21:25:51 -05002643 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002644 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002645 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2646 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002647 if (ret)
2648 break;
2649
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002650 clear_extent_bits(&log->dirty_log_pages, start, end,
2651 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002652 }
2653
Josef Bacik2ab28f32012-10-12 15:27:49 -04002654 /*
2655 * We may have short-circuited the log tree with the full commit logic
2656 * and left ordered extents on our list, so clear these out to keep us
2657 * from leaking inodes and memory.
2658 */
2659 btrfs_free_logged_extents(log, 0);
2660 btrfs_free_logged_extents(log, 1);
2661
Yan Zheng7237f182009-01-21 12:54:03 -05002662 free_extent_buffer(log->node);
2663 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002664}
2665
2666/*
2667 * free all the extents used by the tree log. This should be called
2668 * at commit time of the full transaction
2669 */
2670int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2671{
2672 if (root->log_root) {
2673 free_log_tree(trans, root->log_root);
2674 root->log_root = NULL;
2675 }
2676 return 0;
2677}
2678
2679int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2680 struct btrfs_fs_info *fs_info)
2681{
2682 if (fs_info->log_root_tree) {
2683 free_log_tree(trans, fs_info->log_root_tree);
2684 fs_info->log_root_tree = NULL;
2685 }
Chris Masone02119d2008-09-05 16:13:11 -04002686 return 0;
2687}
2688
2689/*
Chris Masone02119d2008-09-05 16:13:11 -04002690 * If both a file and directory are logged, and unlinks or renames are
2691 * mixed in, we have a few interesting corners:
2692 *
2693 * create file X in dir Y
2694 * link file X to X.link in dir Y
2695 * fsync file X
2696 * unlink file X but leave X.link
2697 * fsync dir Y
2698 *
2699 * After a crash we would expect only X.link to exist. But file X
2700 * didn't get fsync'd again so the log has back refs for X and X.link.
2701 *
2702 * We solve this by removing directory entries and inode backrefs from the
2703 * log when a file that was logged in the current transaction is
2704 * unlinked. Any later fsync will include the updated log entries, and
2705 * we'll be able to reconstruct the proper directory items from backrefs.
2706 *
2707 * This optimizations allows us to avoid relogging the entire inode
2708 * or the entire directory.
2709 */
2710int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2711 struct btrfs_root *root,
2712 const char *name, int name_len,
2713 struct inode *dir, u64 index)
2714{
2715 struct btrfs_root *log;
2716 struct btrfs_dir_item *di;
2717 struct btrfs_path *path;
2718 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002719 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002720 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002721 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002722
Chris Mason3a5f1d42008-09-11 15:53:37 -04002723 if (BTRFS_I(dir)->logged_trans < trans->transid)
2724 return 0;
2725
Chris Masone02119d2008-09-05 16:13:11 -04002726 ret = join_running_log_trans(root);
2727 if (ret)
2728 return 0;
2729
2730 mutex_lock(&BTRFS_I(dir)->log_mutex);
2731
2732 log = root->log_root;
2733 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002734 if (!path) {
2735 err = -ENOMEM;
2736 goto out_unlock;
2737 }
liubo2a29edc2011-01-26 06:22:08 +00002738
Li Zefan33345d012011-04-20 10:31:50 +08002739 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002740 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002741 if (IS_ERR(di)) {
2742 err = PTR_ERR(di);
2743 goto fail;
2744 }
2745 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002746 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2747 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002748 if (ret) {
2749 err = ret;
2750 goto fail;
2751 }
Chris Masone02119d2008-09-05 16:13:11 -04002752 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002753 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002754 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002755 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002756 if (IS_ERR(di)) {
2757 err = PTR_ERR(di);
2758 goto fail;
2759 }
2760 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002761 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2762 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002763 if (ret) {
2764 err = ret;
2765 goto fail;
2766 }
Chris Masone02119d2008-09-05 16:13:11 -04002767 }
2768
2769 /* update the directory size in the log to reflect the names
2770 * we have removed
2771 */
2772 if (bytes_del) {
2773 struct btrfs_key key;
2774
Li Zefan33345d012011-04-20 10:31:50 +08002775 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002776 key.offset = 0;
2777 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002778 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002779
2780 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002781 if (ret < 0) {
2782 err = ret;
2783 goto fail;
2784 }
Chris Masone02119d2008-09-05 16:13:11 -04002785 if (ret == 0) {
2786 struct btrfs_inode_item *item;
2787 u64 i_size;
2788
2789 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2790 struct btrfs_inode_item);
2791 i_size = btrfs_inode_size(path->nodes[0], item);
2792 if (i_size > bytes_del)
2793 i_size -= bytes_del;
2794 else
2795 i_size = 0;
2796 btrfs_set_inode_size(path->nodes[0], item, i_size);
2797 btrfs_mark_buffer_dirty(path->nodes[0]);
2798 } else
2799 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002800 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002801 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002802fail:
Chris Masone02119d2008-09-05 16:13:11 -04002803 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002804out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002805 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002806 if (ret == -ENOSPC) {
2807 root->fs_info->last_trans_log_full_commit = trans->transid;
2808 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002809 } else if (ret < 0)
2810 btrfs_abort_transaction(trans, root, ret);
2811
Chris Mason12fcfd22009-03-24 10:24:20 -04002812 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002813
Andi Kleen411fc6b2010-10-29 15:14:31 -04002814 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002815}
2816
2817/* see comments for btrfs_del_dir_entries_in_log */
2818int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2819 struct btrfs_root *root,
2820 const char *name, int name_len,
2821 struct inode *inode, u64 dirid)
2822{
2823 struct btrfs_root *log;
2824 u64 index;
2825 int ret;
2826
Chris Mason3a5f1d42008-09-11 15:53:37 -04002827 if (BTRFS_I(inode)->logged_trans < trans->transid)
2828 return 0;
2829
Chris Masone02119d2008-09-05 16:13:11 -04002830 ret = join_running_log_trans(root);
2831 if (ret)
2832 return 0;
2833 log = root->log_root;
2834 mutex_lock(&BTRFS_I(inode)->log_mutex);
2835
Li Zefan33345d012011-04-20 10:31:50 +08002836 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002837 dirid, &index);
2838 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002839 if (ret == -ENOSPC) {
2840 root->fs_info->last_trans_log_full_commit = trans->transid;
2841 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002842 } else if (ret < 0 && ret != -ENOENT)
2843 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002844 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002845
Chris Masone02119d2008-09-05 16:13:11 -04002846 return ret;
2847}
2848
2849/*
2850 * creates a range item in the log for 'dirid'. first_offset and
2851 * last_offset tell us which parts of the key space the log should
2852 * be considered authoritative for.
2853 */
2854static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2855 struct btrfs_root *log,
2856 struct btrfs_path *path,
2857 int key_type, u64 dirid,
2858 u64 first_offset, u64 last_offset)
2859{
2860 int ret;
2861 struct btrfs_key key;
2862 struct btrfs_dir_log_item *item;
2863
2864 key.objectid = dirid;
2865 key.offset = first_offset;
2866 if (key_type == BTRFS_DIR_ITEM_KEY)
2867 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2868 else
2869 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2870 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002871 if (ret)
2872 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002873
2874 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2875 struct btrfs_dir_log_item);
2876 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2877 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002878 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002879 return 0;
2880}
2881
2882/*
2883 * log all the items included in the current transaction for a given
2884 * directory. This also creates the range items in the log tree required
2885 * to replay anything deleted before the fsync
2886 */
2887static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2888 struct btrfs_root *root, struct inode *inode,
2889 struct btrfs_path *path,
2890 struct btrfs_path *dst_path, int key_type,
2891 u64 min_offset, u64 *last_offset_ret)
2892{
2893 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002894 struct btrfs_root *log = root->log_root;
2895 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002896 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002897 int ret;
2898 int i;
2899 int nritems;
2900 u64 first_offset = min_offset;
2901 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002902 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002903
2904 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002905
Li Zefan33345d012011-04-20 10:31:50 +08002906 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002907 min_key.type = key_type;
2908 min_key.offset = min_offset;
2909
2910 path->keep_locks = 1;
2911
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002912 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002913
2914 /*
2915 * we didn't find anything from this transaction, see if there
2916 * is anything at all
2917 */
Li Zefan33345d012011-04-20 10:31:50 +08002918 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2919 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002920 min_key.type = key_type;
2921 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002922 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002923 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2924 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002925 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002926 return ret;
2927 }
Li Zefan33345d012011-04-20 10:31:50 +08002928 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002929
2930 /* if ret == 0 there are items for this type,
2931 * create a range to tell us the last key of this type.
2932 * otherwise, there are no items in this directory after
2933 * *min_offset, and we create a range to indicate that.
2934 */
2935 if (ret == 0) {
2936 struct btrfs_key tmp;
2937 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2938 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002939 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002940 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002941 }
2942 goto done;
2943 }
2944
2945 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002946 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002947 if (ret == 0) {
2948 struct btrfs_key tmp;
2949 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2950 if (key_type == tmp.type) {
2951 first_offset = tmp.offset;
2952 ret = overwrite_item(trans, log, dst_path,
2953 path->nodes[0], path->slots[0],
2954 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002955 if (ret) {
2956 err = ret;
2957 goto done;
2958 }
Chris Masone02119d2008-09-05 16:13:11 -04002959 }
2960 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002961 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002962
2963 /* find the first key from this transaction again */
2964 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302965 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04002966 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04002967
2968 /*
2969 * we have a block from this transaction, log every item in it
2970 * from our directory
2971 */
Chris Masond3977122009-01-05 21:25:51 -05002972 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002973 struct btrfs_key tmp;
2974 src = path->nodes[0];
2975 nritems = btrfs_header_nritems(src);
2976 for (i = path->slots[0]; i < nritems; i++) {
2977 btrfs_item_key_to_cpu(src, &min_key, i);
2978
Li Zefan33345d012011-04-20 10:31:50 +08002979 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002980 goto done;
2981 ret = overwrite_item(trans, log, dst_path, src, i,
2982 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002983 if (ret) {
2984 err = ret;
2985 goto done;
2986 }
Chris Masone02119d2008-09-05 16:13:11 -04002987 }
2988 path->slots[0] = nritems;
2989
2990 /*
2991 * look ahead to the next item and see if it is also
2992 * from this directory and from this transaction
2993 */
2994 ret = btrfs_next_leaf(root, path);
2995 if (ret == 1) {
2996 last_offset = (u64)-1;
2997 goto done;
2998 }
2999 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003000 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003001 last_offset = (u64)-1;
3002 goto done;
3003 }
3004 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
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 else
3011 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003012 goto done;
3013 }
3014 }
3015done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003016 btrfs_release_path(path);
3017 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003018
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003019 if (err == 0) {
3020 *last_offset_ret = last_offset;
3021 /*
3022 * insert the log range keys to indicate where the log
3023 * is valid
3024 */
3025 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003026 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003027 if (ret)
3028 err = ret;
3029 }
3030 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003031}
3032
3033/*
3034 * logging directories is very similar to logging inodes, We find all the items
3035 * from the current transaction and write them to the log.
3036 *
3037 * The recovery code scans the directory in the subvolume, and if it finds a
3038 * key in the range logged that is not present in the log tree, then it means
3039 * that dir entry was unlinked during the transaction.
3040 *
3041 * In order for that scan to work, we must include one key smaller than
3042 * the smallest logged by this transaction and one key larger than the largest
3043 * key logged by this transaction.
3044 */
3045static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root, struct inode *inode,
3047 struct btrfs_path *path,
3048 struct btrfs_path *dst_path)
3049{
3050 u64 min_key;
3051 u64 max_key;
3052 int ret;
3053 int key_type = BTRFS_DIR_ITEM_KEY;
3054
3055again:
3056 min_key = 0;
3057 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003058 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003059 ret = log_dir_items(trans, root, inode, path,
3060 dst_path, key_type, min_key,
3061 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003062 if (ret)
3063 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003064 if (max_key == (u64)-1)
3065 break;
3066 min_key = max_key + 1;
3067 }
3068
3069 if (key_type == BTRFS_DIR_ITEM_KEY) {
3070 key_type = BTRFS_DIR_INDEX_KEY;
3071 goto again;
3072 }
3073 return 0;
3074}
3075
3076/*
3077 * a helper function to drop items from the log before we relog an
3078 * inode. max_key_type indicates the highest item type to remove.
3079 * This cannot be run for file data extents because it does not
3080 * free the extents they point to.
3081 */
3082static int drop_objectid_items(struct btrfs_trans_handle *trans,
3083 struct btrfs_root *log,
3084 struct btrfs_path *path,
3085 u64 objectid, int max_key_type)
3086{
3087 int ret;
3088 struct btrfs_key key;
3089 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003090 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003091
3092 key.objectid = objectid;
3093 key.type = max_key_type;
3094 key.offset = (u64)-1;
3095
Chris Masond3977122009-01-05 21:25:51 -05003096 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003097 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003098 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003099 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003100 break;
3101
3102 if (path->slots[0] == 0)
3103 break;
3104
3105 path->slots[0]--;
3106 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3107 path->slots[0]);
3108
3109 if (found_key.objectid != objectid)
3110 break;
3111
Josef Bacik18ec90d2012-09-28 11:56:28 -04003112 found_key.offset = 0;
3113 found_key.type = 0;
3114 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3115 &start_slot);
3116
3117 ret = btrfs_del_items(trans, log, path, start_slot,
3118 path->slots[0] - start_slot + 1);
3119 /*
3120 * If start slot isn't 0 then we don't need to re-search, we've
3121 * found the last guy with the objectid in this tree.
3122 */
3123 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003124 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003125 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003126 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003127 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003128 if (ret > 0)
3129 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003130 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003131}
3132
Josef Bacik94edf4a2012-09-25 14:56:25 -04003133static void fill_inode_item(struct btrfs_trans_handle *trans,
3134 struct extent_buffer *leaf,
3135 struct btrfs_inode_item *item,
3136 struct inode *inode, int log_inode_only)
3137{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003138 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003139
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003140 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003141
3142 if (log_inode_only) {
3143 /* set the generation to zero so the recover code
3144 * can tell the difference between an logging
3145 * just to say 'this inode exists' and a logging
3146 * to say 'update this inode with these values'
3147 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003148 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3149 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003150 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003151 btrfs_set_token_inode_generation(leaf, item,
3152 BTRFS_I(inode)->generation,
3153 &token);
3154 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003155 }
3156
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003157 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3158 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3159 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3160 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3161
3162 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3163 inode->i_atime.tv_sec, &token);
3164 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3165 inode->i_atime.tv_nsec, &token);
3166
3167 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3168 inode->i_mtime.tv_sec, &token);
3169 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3170 inode->i_mtime.tv_nsec, &token);
3171
3172 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3173 inode->i_ctime.tv_sec, &token);
3174 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3175 inode->i_ctime.tv_nsec, &token);
3176
3177 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3178 &token);
3179
3180 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3181 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3182 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3183 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3184 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003185}
3186
Josef Bacika95249b2012-10-11 16:17:34 -04003187static int log_inode_item(struct btrfs_trans_handle *trans,
3188 struct btrfs_root *log, struct btrfs_path *path,
3189 struct inode *inode)
3190{
3191 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003192 int ret;
3193
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003194 ret = btrfs_insert_empty_item(trans, log, path,
3195 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003196 sizeof(*inode_item));
3197 if (ret && ret != -EEXIST)
3198 return ret;
3199 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3200 struct btrfs_inode_item);
3201 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3202 btrfs_release_path(path);
3203 return 0;
3204}
3205
Chris Mason31ff1cd2008-09-11 16:17:57 -04003206static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003207 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003208 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003209 struct btrfs_path *src_path, u64 *last_extent,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003210 int start_slot, int nr, int inode_only)
3211{
3212 unsigned long src_offset;
3213 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003214 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003215 struct btrfs_file_extent_item *extent;
3216 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003217 struct extent_buffer *src = src_path->nodes[0];
3218 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003219 int ret;
3220 struct btrfs_key *ins_keys;
3221 u32 *ins_sizes;
3222 char *ins_data;
3223 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003224 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003225 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003226 bool has_extents = false;
3227 bool need_find_last_extent = (*last_extent == 0);
3228 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003229
3230 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003231
3232 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3233 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003234 if (!ins_data)
3235 return -ENOMEM;
3236
Josef Bacik16e75492013-10-22 12:18:51 -04003237 first_key.objectid = (u64)-1;
3238
Chris Mason31ff1cd2008-09-11 16:17:57 -04003239 ins_sizes = (u32 *)ins_data;
3240 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3241
3242 for (i = 0; i < nr; i++) {
3243 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3244 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3245 }
3246 ret = btrfs_insert_empty_items(trans, log, dst_path,
3247 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003248 if (ret) {
3249 kfree(ins_data);
3250 return ret;
3251 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003252
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003253 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003254 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3255 dst_path->slots[0]);
3256
3257 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3258
Josef Bacik16e75492013-10-22 12:18:51 -04003259 if ((i == (nr - 1)))
3260 last_key = ins_keys[i];
3261
Josef Bacik94edf4a2012-09-25 14:56:25 -04003262 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003263 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3264 dst_path->slots[0],
3265 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003266 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3267 inode, inode_only == LOG_INODE_EXISTS);
3268 } else {
3269 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3270 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003271 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003272
Josef Bacik16e75492013-10-22 12:18:51 -04003273 /*
3274 * We set need_find_last_extent here in case we know we were
3275 * processing other items and then walk into the first extent in
3276 * the inode. If we don't hit an extent then nothing changes,
3277 * we'll do the last search the next time around.
3278 */
3279 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3280 has_extents = true;
3281 if (need_find_last_extent &&
3282 first_key.objectid == (u64)-1)
3283 first_key = ins_keys[i];
3284 } else {
3285 need_find_last_extent = false;
3286 }
3287
Chris Mason31ff1cd2008-09-11 16:17:57 -04003288 /* take a reference on file data extents so that truncates
3289 * or deletes of this inode don't have to relog the inode
3290 * again
3291 */
Liu Bod2794402012-08-29 01:07:56 -06003292 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3293 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003294 int found_type;
3295 extent = btrfs_item_ptr(src, start_slot + i,
3296 struct btrfs_file_extent_item);
3297
liubo8e531cd2011-05-06 10:36:09 +08003298 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3299 continue;
3300
Chris Mason31ff1cd2008-09-11 16:17:57 -04003301 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003302 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003303 u64 ds, dl, cs, cl;
3304 ds = btrfs_file_extent_disk_bytenr(src,
3305 extent);
3306 /* ds == 0 is a hole */
3307 if (ds == 0)
3308 continue;
3309
3310 dl = btrfs_file_extent_disk_num_bytes(src,
3311 extent);
3312 cs = btrfs_file_extent_offset(src, extent);
3313 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003314 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003315 if (btrfs_file_extent_compression(src,
3316 extent)) {
3317 cs = 0;
3318 cl = dl;
3319 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320
3321 ret = btrfs_lookup_csums_range(
3322 log->fs_info->csum_root,
3323 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003324 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003325 if (ret) {
3326 btrfs_release_path(dst_path);
3327 kfree(ins_data);
3328 return ret;
3329 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003330 }
3331 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003332 }
3333
3334 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003335 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003336 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003337
3338 /*
3339 * we have to do this after the loop above to avoid changing the
3340 * log tree while trying to change the log tree.
3341 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003342 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003343 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003344 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3345 struct btrfs_ordered_sum,
3346 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003347 if (!ret)
3348 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003349 list_del(&sums->list);
3350 kfree(sums);
3351 }
Josef Bacik16e75492013-10-22 12:18:51 -04003352
3353 if (!has_extents)
3354 return ret;
3355
3356 /*
3357 * Because we use btrfs_search_forward we could skip leaves that were
3358 * not modified and then assume *last_extent is valid when it really
3359 * isn't. So back up to the previous leaf and read the end of the last
3360 * extent before we go and fill in holes.
3361 */
3362 if (need_find_last_extent) {
3363 u64 len;
3364
3365 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3366 if (ret < 0)
3367 return ret;
3368 if (ret)
3369 goto fill_holes;
3370 if (src_path->slots[0])
3371 src_path->slots[0]--;
3372 src = src_path->nodes[0];
3373 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3374 if (key.objectid != btrfs_ino(inode) ||
3375 key.type != BTRFS_EXTENT_DATA_KEY)
3376 goto fill_holes;
3377 extent = btrfs_item_ptr(src, src_path->slots[0],
3378 struct btrfs_file_extent_item);
3379 if (btrfs_file_extent_type(src, extent) ==
3380 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003381 len = btrfs_file_extent_inline_len(src,
3382 src_path->slots[0],
3383 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003384 *last_extent = ALIGN(key.offset + len,
3385 log->sectorsize);
3386 } else {
3387 len = btrfs_file_extent_num_bytes(src, extent);
3388 *last_extent = key.offset + len;
3389 }
3390 }
3391fill_holes:
3392 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3393 * things could have happened
3394 *
3395 * 1) A merge could have happened, so we could currently be on a leaf
3396 * that holds what we were copying in the first place.
3397 * 2) A split could have happened, and now not all of the items we want
3398 * are on the same leaf.
3399 *
3400 * So we need to adjust how we search for holes, we need to drop the
3401 * path and re-search for the first extent key we found, and then walk
3402 * forward until we hit the last one we copied.
3403 */
3404 if (need_find_last_extent) {
3405 /* btrfs_prev_leaf could return 1 without releasing the path */
3406 btrfs_release_path(src_path);
3407 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3408 src_path, 0, 0);
3409 if (ret < 0)
3410 return ret;
3411 ASSERT(ret == 0);
3412 src = src_path->nodes[0];
3413 i = src_path->slots[0];
3414 } else {
3415 i = start_slot;
3416 }
3417
3418 /*
3419 * Ok so here we need to go through and fill in any holes we may have
3420 * to make sure that holes are punched for those areas in case they had
3421 * extents previously.
3422 */
3423 while (!done) {
3424 u64 offset, len;
3425 u64 extent_end;
3426
3427 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3428 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3429 if (ret < 0)
3430 return ret;
3431 ASSERT(ret == 0);
3432 src = src_path->nodes[0];
3433 i = 0;
3434 }
3435
3436 btrfs_item_key_to_cpu(src, &key, i);
3437 if (!btrfs_comp_cpu_keys(&key, &last_key))
3438 done = true;
3439 if (key.objectid != btrfs_ino(inode) ||
3440 key.type != BTRFS_EXTENT_DATA_KEY) {
3441 i++;
3442 continue;
3443 }
3444 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3445 if (btrfs_file_extent_type(src, extent) ==
3446 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003447 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003448 extent_end = ALIGN(key.offset + len, log->sectorsize);
3449 } else {
3450 len = btrfs_file_extent_num_bytes(src, extent);
3451 extent_end = key.offset + len;
3452 }
3453 i++;
3454
3455 if (*last_extent == key.offset) {
3456 *last_extent = extent_end;
3457 continue;
3458 }
3459 offset = *last_extent;
3460 len = key.offset - *last_extent;
3461 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3462 offset, 0, 0, len, 0, len, 0,
3463 0, 0);
3464 if (ret)
3465 break;
3466 *last_extent = offset + len;
3467 }
3468 /*
3469 * Need to let the callers know we dropped the path so they should
3470 * re-search.
3471 */
3472 if (!ret && need_find_last_extent)
3473 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003474 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003475}
3476
Josef Bacik5dc562c2012-08-17 13:14:17 -04003477static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3478{
3479 struct extent_map *em1, *em2;
3480
3481 em1 = list_entry(a, struct extent_map, list);
3482 em2 = list_entry(b, struct extent_map, list);
3483
3484 if (em1->start < em2->start)
3485 return -1;
3486 else if (em1->start > em2->start)
3487 return 1;
3488 return 0;
3489}
3490
Josef Bacik5dc562c2012-08-17 13:14:17 -04003491static int log_one_extent(struct btrfs_trans_handle *trans,
3492 struct inode *inode, struct btrfs_root *root,
Miao Xie827463c2014-01-14 20:31:51 +08003493 struct extent_map *em, struct btrfs_path *path,
3494 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003495{
3496 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003497 struct btrfs_file_extent_item *fi;
3498 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003499 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003500 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003501 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003502 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003503 u64 mod_start = em->mod_start;
3504 u64 mod_len = em->mod_len;
3505 u64 csum_offset;
3506 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003507 u64 extent_offset = em->start - em->orig_start;
3508 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003509 int ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003510 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003511 int extent_inserted = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003512
Josef Bacik70c8a912012-10-11 16:54:30 -04003513 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003514 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003515
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003516 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3517 em->start + em->len, NULL, 0, 1,
3518 sizeof(*fi), &extent_inserted);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003519 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003520 return ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003521
3522 if (!extent_inserted) {
3523 key.objectid = btrfs_ino(inode);
3524 key.type = BTRFS_EXTENT_DATA_KEY;
3525 key.offset = em->start;
3526
3527 ret = btrfs_insert_empty_item(trans, log, path, &key,
3528 sizeof(*fi));
3529 if (ret)
3530 return ret;
3531 }
Josef Bacik70c8a912012-10-11 16:54:30 -04003532 leaf = path->nodes[0];
3533 fi = btrfs_item_ptr(leaf, path->slots[0],
3534 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003535
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003536 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3537 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003538 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3539 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003540 btrfs_set_token_file_extent_type(leaf, fi,
3541 BTRFS_FILE_EXTENT_PREALLOC,
3542 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003543 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003544 btrfs_set_token_file_extent_type(leaf, fi,
3545 BTRFS_FILE_EXTENT_REG,
3546 &token);
Josef Baciked9e8af2013-10-14 17:23:08 -04003547 if (em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003548 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003549 }
3550
Josef Bacik70c8a912012-10-11 16:54:30 -04003551 block_len = max(em->block_len, em->orig_block_len);
3552 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003553 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3554 em->block_start,
3555 &token);
3556 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3557 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003558 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003559 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3560 em->block_start -
3561 extent_offset, &token);
3562 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3563 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003564 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003565 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3566 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3567 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003568 }
3569
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003570 btrfs_set_token_file_extent_offset(leaf, fi,
3571 em->start - em->orig_start,
3572 &token);
3573 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003574 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003575 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3576 &token);
3577 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3578 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003579 btrfs_mark_buffer_dirty(leaf);
3580
Josef Bacik70c8a912012-10-11 16:54:30 -04003581 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003582 if (ret) {
3583 return ret;
3584 }
3585
3586 if (skip_csum)
3587 return 0;
3588
Josef Bacik2ab28f32012-10-12 15:27:49 -04003589 /*
3590 * First check and see if our csums are on our outstanding ordered
3591 * extents.
3592 */
Miao Xie827463c2014-01-14 20:31:51 +08003593 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003594 struct btrfs_ordered_sum *sum;
3595
3596 if (!mod_len)
3597 break;
3598
Josef Bacik2ab28f32012-10-12 15:27:49 -04003599 if (ordered->file_offset + ordered->len <= mod_start ||
3600 mod_start + mod_len <= ordered->file_offset)
3601 continue;
3602
3603 /*
3604 * We are going to copy all the csums on this ordered extent, so
3605 * go ahead and adjust mod_start and mod_len in case this
3606 * ordered extent has already been logged.
3607 */
3608 if (ordered->file_offset > mod_start) {
3609 if (ordered->file_offset + ordered->len >=
3610 mod_start + mod_len)
3611 mod_len = ordered->file_offset - mod_start;
3612 /*
3613 * If we have this case
3614 *
3615 * |--------- logged extent ---------|
3616 * |----- ordered extent ----|
3617 *
3618 * Just don't mess with mod_start and mod_len, we'll
3619 * just end up logging more csums than we need and it
3620 * will be ok.
3621 */
3622 } else {
3623 if (ordered->file_offset + ordered->len <
3624 mod_start + mod_len) {
3625 mod_len = (mod_start + mod_len) -
3626 (ordered->file_offset + ordered->len);
3627 mod_start = ordered->file_offset +
3628 ordered->len;
3629 } else {
3630 mod_len = 0;
3631 }
3632 }
3633
3634 /*
3635 * To keep us from looping for the above case of an ordered
3636 * extent that falls inside of the logged extent.
3637 */
3638 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3639 &ordered->flags))
3640 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003641
Miao Xie23c671a2014-01-14 20:31:52 +08003642 if (ordered->csum_bytes_left) {
3643 btrfs_start_ordered_extent(inode, ordered, 0);
3644 wait_event(ordered->wait,
3645 ordered->csum_bytes_left == 0);
3646 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003647
3648 list_for_each_entry(sum, &ordered->list, list) {
3649 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003650 if (ret)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003651 goto unlocked;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003652 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003653
3654 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003655unlocked:
3656
3657 if (!mod_len || ret)
3658 return ret;
3659
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003660 if (em->compress_type) {
3661 csum_offset = 0;
3662 csum_len = block_len;
3663 } else {
3664 csum_offset = mod_start - em->start;
3665 csum_len = mod_len;
3666 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003667
Josef Bacik70c8a912012-10-11 16:54:30 -04003668 /* block start is already adjusted for the file extent offset. */
3669 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3670 em->block_start + csum_offset,
3671 em->block_start + csum_offset +
3672 csum_len - 1, &ordered_sums, 0);
3673 if (ret)
3674 return ret;
3675
3676 while (!list_empty(&ordered_sums)) {
3677 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3678 struct btrfs_ordered_sum,
3679 list);
3680 if (!ret)
3681 ret = btrfs_csum_file_blocks(trans, log, sums);
3682 list_del(&sums->list);
3683 kfree(sums);
3684 }
3685
3686 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003687}
3688
3689static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3690 struct btrfs_root *root,
3691 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08003692 struct btrfs_path *path,
3693 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003694{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003695 struct extent_map *em, *n;
3696 struct list_head extents;
3697 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3698 u64 test_gen;
3699 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003700 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003701
3702 INIT_LIST_HEAD(&extents);
3703
Josef Bacik5dc562c2012-08-17 13:14:17 -04003704 write_lock(&tree->lock);
3705 test_gen = root->fs_info->last_trans_committed;
3706
3707 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3708 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003709
3710 /*
3711 * Just an arbitrary number, this can be really CPU intensive
3712 * once we start getting a lot of extents, and really once we
3713 * have a bunch of extents we just want to commit since it will
3714 * be faster.
3715 */
3716 if (++num > 32768) {
3717 list_del_init(&tree->modified_extents);
3718 ret = -EFBIG;
3719 goto process;
3720 }
3721
Josef Bacik5dc562c2012-08-17 13:14:17 -04003722 if (em->generation <= test_gen)
3723 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003724 /* Need a ref to keep it from getting evicted from cache */
3725 atomic_inc(&em->refs);
3726 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003727 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003728 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003729 }
3730
3731 list_sort(NULL, &extents, extent_cmp);
3732
Josef Bacik2ab28f32012-10-12 15:27:49 -04003733process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003734 while (!list_empty(&extents)) {
3735 em = list_entry(extents.next, struct extent_map, list);
3736
3737 list_del_init(&em->list);
3738
3739 /*
3740 * If we had an error we just need to delete everybody from our
3741 * private list.
3742 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003743 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003744 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003745 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003746 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003747 }
3748
3749 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003750
Miao Xie827463c2014-01-14 20:31:51 +08003751 ret = log_one_extent(trans, inode, root, em, path, logged_list);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003752 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003753 clear_em_logging(tree, em);
3754 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003755 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003756 WARN_ON(!list_empty(&extents));
3757 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003758
Josef Bacik5dc562c2012-08-17 13:14:17 -04003759 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003760 return ret;
3761}
3762
Chris Masone02119d2008-09-05 16:13:11 -04003763/* log a single inode in the tree log.
3764 * At least one parent directory for this inode must exist in the tree
3765 * or be logged already.
3766 *
3767 * Any items from this inode changed by the current transaction are copied
3768 * to the log tree. An extra reference is taken on any extents in this
3769 * file, allowing us to avoid a whole pile of corner cases around logging
3770 * blocks that have been removed from the tree.
3771 *
3772 * See LOG_INODE_ALL and related defines for a description of what inode_only
3773 * does.
3774 *
3775 * This handles both files and directories.
3776 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003777static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003778 struct btrfs_root *root, struct inode *inode,
3779 int inode_only)
3780{
3781 struct btrfs_path *path;
3782 struct btrfs_path *dst_path;
3783 struct btrfs_key min_key;
3784 struct btrfs_key max_key;
3785 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003786 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08003787 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04003788 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003789 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003790 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003791 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003792 int ins_start_slot = 0;
3793 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003794 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003795 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003796
Chris Masone02119d2008-09-05 16:13:11 -04003797 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003798 if (!path)
3799 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003800 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003801 if (!dst_path) {
3802 btrfs_free_path(path);
3803 return -ENOMEM;
3804 }
Chris Masone02119d2008-09-05 16:13:11 -04003805
Li Zefan33345d012011-04-20 10:31:50 +08003806 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003807 min_key.type = BTRFS_INODE_ITEM_KEY;
3808 min_key.offset = 0;
3809
Li Zefan33345d012011-04-20 10:31:50 +08003810 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003811
Chris Mason12fcfd22009-03-24 10:24:20 -04003812
Josef Bacik5dc562c2012-08-17 13:14:17 -04003813 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003814 if (S_ISDIR(inode->i_mode) ||
3815 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3816 &BTRFS_I(inode)->runtime_flags) &&
3817 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003818 max_key.type = BTRFS_XATTR_ITEM_KEY;
3819 else
3820 max_key.type = (u8)-1;
3821 max_key.offset = (u64)-1;
3822
Josef Bacik94edf4a2012-09-25 14:56:25 -04003823 /* Only run delayed items if we are a dir or a new file */
3824 if (S_ISDIR(inode->i_mode) ||
3825 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3826 ret = btrfs_commit_inode_delayed_items(trans, inode);
3827 if (ret) {
3828 btrfs_free_path(path);
3829 btrfs_free_path(dst_path);
3830 return ret;
3831 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003832 }
3833
Chris Masone02119d2008-09-05 16:13:11 -04003834 mutex_lock(&BTRFS_I(inode)->log_mutex);
3835
Miao Xie827463c2014-01-14 20:31:51 +08003836 btrfs_get_logged_extents(inode, &logged_list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003837
Chris Masone02119d2008-09-05 16:13:11 -04003838 /*
3839 * a brute force approach to making sure we get the most uptodate
3840 * copies of everything.
3841 */
3842 if (S_ISDIR(inode->i_mode)) {
3843 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3844
3845 if (inode_only == LOG_INODE_EXISTS)
3846 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003847 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003848 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003849 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3850 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003851 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3852 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003853 ret = btrfs_truncate_inode_items(trans, log,
3854 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003855 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Josef Bacik6cfab852013-11-12 16:25:58 -05003856 &BTRFS_I(inode)->runtime_flags) ||
3857 inode_only == LOG_INODE_EXISTS) {
Josef Bacika95249b2012-10-11 16:17:34 -04003858 if (inode_only == LOG_INODE_ALL)
3859 fast_search = true;
3860 max_key.type = BTRFS_XATTR_ITEM_KEY;
3861 ret = drop_objectid_items(trans, log, path, ino,
3862 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003863 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003864 if (inode_only == LOG_INODE_ALL)
3865 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003866 ret = log_inode_item(trans, log, dst_path, inode);
3867 if (ret) {
3868 err = ret;
3869 goto out_unlock;
3870 }
3871 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003872 }
Josef Bacika95249b2012-10-11 16:17:34 -04003873
Chris Masone02119d2008-09-05 16:13:11 -04003874 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003875 if (ret) {
3876 err = ret;
3877 goto out_unlock;
3878 }
Chris Masone02119d2008-09-05 16:13:11 -04003879 path->keep_locks = 1;
3880
Chris Masond3977122009-01-05 21:25:51 -05003881 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003882 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003883 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003884 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003885 if (ret != 0)
3886 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003887again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003888 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003889 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003890 break;
3891 if (min_key.type > max_key.type)
3892 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003893
Chris Masone02119d2008-09-05 16:13:11 -04003894 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003895 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3896 ins_nr++;
3897 goto next_slot;
3898 } else if (!ins_nr) {
3899 ins_start_slot = path->slots[0];
3900 ins_nr = 1;
3901 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003902 }
3903
Josef Bacik16e75492013-10-22 12:18:51 -04003904 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3905 ins_start_slot, ins_nr, inode_only);
3906 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003907 err = ret;
3908 goto out_unlock;
Josef Bacik16e75492013-10-22 12:18:51 -04003909 } if (ret) {
3910 ins_nr = 0;
3911 btrfs_release_path(path);
3912 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003913 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003914 ins_nr = 1;
3915 ins_start_slot = path->slots[0];
3916next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003917
Chris Mason3a5f1d42008-09-11 15:53:37 -04003918 nritems = btrfs_header_nritems(path->nodes[0]);
3919 path->slots[0]++;
3920 if (path->slots[0] < nritems) {
3921 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3922 path->slots[0]);
3923 goto again;
3924 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003925 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04003926 ret = copy_items(trans, inode, dst_path, path,
3927 &last_extent, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003928 ins_nr, inode_only);
Josef Bacik16e75492013-10-22 12:18:51 -04003929 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003930 err = ret;
3931 goto out_unlock;
3932 }
Josef Bacik16e75492013-10-22 12:18:51 -04003933 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003934 ins_nr = 0;
3935 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003936 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003937
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003938 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04003939 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003940 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04003941 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003942 min_key.offset = 0;
3943 } else {
Chris Masone02119d2008-09-05 16:13:11 -04003944 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003945 }
Chris Masone02119d2008-09-05 16:13:11 -04003946 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003947 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04003948 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3949 ins_start_slot, ins_nr, inode_only);
3950 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003951 err = ret;
3952 goto out_unlock;
3953 }
Josef Bacik16e75492013-10-22 12:18:51 -04003954 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003955 ins_nr = 0;
3956 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003957
Josef Bacika95249b2012-10-11 16:17:34 -04003958log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04003959 btrfs_release_path(path);
3960 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003961 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08003962 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
3963 &logged_list);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003964 if (ret) {
3965 err = ret;
3966 goto out_unlock;
3967 }
Josef Bacikd006a042013-11-12 20:54:09 -05003968 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06003969 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3970 struct extent_map *em, *n;
3971
Miao Xiebbe14262012-11-01 07:34:54 +00003972 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003973 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3974 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003975 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003976 }
3977
Chris Mason9623f9a2008-09-11 17:42:42 -04003978 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04003979 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003980 if (ret) {
3981 err = ret;
3982 goto out_unlock;
3983 }
Chris Masone02119d2008-09-05 16:13:11 -04003984 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003985 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003986 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003987out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08003988 if (unlikely(err))
3989 btrfs_put_logged_extents(&logged_list);
3990 else
3991 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04003992 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3993
3994 btrfs_free_path(path);
3995 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003996 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003997}
3998
Chris Mason12fcfd22009-03-24 10:24:20 -04003999/*
4000 * follow the dentry parent pointers up the chain and see if any
4001 * of the directories in it require a full commit before they can
4002 * be logged. Returns zero if nothing special needs to be done or 1 if
4003 * a full commit is required.
4004 */
4005static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4006 struct inode *inode,
4007 struct dentry *parent,
4008 struct super_block *sb,
4009 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004010{
Chris Mason12fcfd22009-03-24 10:24:20 -04004011 int ret = 0;
4012 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004013 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004014 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004015
Chris Masonaf4176b2009-03-24 10:24:31 -04004016 /*
4017 * for regular files, if its inode is already on disk, we don't
4018 * have to worry about the parents at all. This is because
4019 * we can use the last_unlink_trans field to record renames
4020 * and other fun in this file.
4021 */
4022 if (S_ISREG(inode->i_mode) &&
4023 BTRFS_I(inode)->generation <= last_committed &&
4024 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4025 goto out;
4026
Chris Mason12fcfd22009-03-24 10:24:20 -04004027 if (!S_ISDIR(inode->i_mode)) {
4028 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4029 goto out;
4030 inode = parent->d_inode;
4031 }
4032
4033 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004034 /*
4035 * If we are logging a directory then we start with our inode,
4036 * not our parents inode, so we need to skipp setting the
4037 * logged_trans so that further down in the log code we don't
4038 * think this inode has already been logged.
4039 */
4040 if (inode != orig_inode)
4041 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004042 smp_mb();
4043
4044 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4045 root = BTRFS_I(inode)->root;
4046
4047 /*
4048 * make sure any commits to the log are forced
4049 * to be full commits
4050 */
4051 root->fs_info->last_trans_log_full_commit =
4052 trans->transid;
4053 ret = 1;
4054 break;
4055 }
4056
4057 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4058 break;
4059
Yan, Zheng76dda932009-09-21 16:00:26 -04004060 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004061 break;
4062
Josef Bacik6a912212010-11-20 09:48:00 +00004063 parent = dget_parent(parent);
4064 dput(old_parent);
4065 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04004066 inode = parent->d_inode;
4067
4068 }
Josef Bacik6a912212010-11-20 09:48:00 +00004069 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004070out:
Chris Masone02119d2008-09-05 16:13:11 -04004071 return ret;
4072}
4073
4074/*
4075 * helper function around btrfs_log_inode to make sure newly created
4076 * parent directories also end up in the log. A minimal inode and backref
4077 * only logging is done of any parent directories that are older than
4078 * the last committed transaction
4079 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004080static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4081 struct btrfs_root *root, struct inode *inode,
4082 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04004083{
Chris Mason12fcfd22009-03-24 10:24:20 -04004084 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04004085 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00004086 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04004087 int ret = 0;
4088 u64 last_committed = root->fs_info->last_trans_committed;
4089
4090 sb = inode->i_sb;
4091
Sage Weil3a5e1402009-04-02 16:49:40 -04004092 if (btrfs_test_opt(root, NOTREELOG)) {
4093 ret = 1;
4094 goto end_no_trans;
4095 }
4096
Chris Mason12fcfd22009-03-24 10:24:20 -04004097 if (root->fs_info->last_trans_log_full_commit >
4098 root->fs_info->last_trans_committed) {
4099 ret = 1;
4100 goto end_no_trans;
4101 }
4102
Yan, Zheng76dda932009-09-21 16:00:26 -04004103 if (root != BTRFS_I(inode)->root ||
4104 btrfs_root_refs(&root->root_item) == 0) {
4105 ret = 1;
4106 goto end_no_trans;
4107 }
4108
Chris Mason12fcfd22009-03-24 10:24:20 -04004109 ret = check_parent_dirs_for_sync(trans, inode, parent,
4110 sb, last_committed);
4111 if (ret)
4112 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04004113
Josef Bacik22ee6982012-05-29 16:57:49 -04004114 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04004115 ret = BTRFS_NO_LOG_SYNC;
4116 goto end_no_trans;
4117 }
4118
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004119 ret = start_log_trans(trans, root);
4120 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08004121 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004122
4123 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004124 if (ret)
4125 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004126
Chris Masonaf4176b2009-03-24 10:24:31 -04004127 /*
4128 * for regular files, if its inode is already on disk, we don't
4129 * have to worry about the parents at all. This is because
4130 * we can use the last_unlink_trans field to record renames
4131 * and other fun in this file.
4132 */
4133 if (S_ISREG(inode->i_mode) &&
4134 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004135 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4136 ret = 0;
4137 goto end_trans;
4138 }
Chris Masonaf4176b2009-03-24 10:24:31 -04004139
4140 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05004141 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04004142 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04004143 break;
4144
Chris Mason12fcfd22009-03-24 10:24:20 -04004145 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004146 if (root != BTRFS_I(inode)->root)
4147 break;
4148
Chris Mason12fcfd22009-03-24 10:24:20 -04004149 if (BTRFS_I(inode)->generation >
4150 root->fs_info->last_trans_committed) {
4151 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004152 if (ret)
4153 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004154 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004155 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04004156 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04004157
Josef Bacik6a912212010-11-20 09:48:00 +00004158 parent = dget_parent(parent);
4159 dput(old_parent);
4160 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04004161 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004162 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004163end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004164 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004165 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004166 root->fs_info->last_trans_log_full_commit = trans->transid;
4167 ret = 1;
4168 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004169 btrfs_end_log_trans(root);
4170end_no_trans:
4171 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004172}
4173
4174/*
4175 * it is not safe to log dentry if the chunk root has added new
4176 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4177 * If this returns 1, you must commit the transaction to safely get your
4178 * data on disk.
4179 */
4180int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
4181 struct btrfs_root *root, struct dentry *dentry)
4182{
Josef Bacik6a912212010-11-20 09:48:00 +00004183 struct dentry *parent = dget_parent(dentry);
4184 int ret;
4185
4186 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
4187 dput(parent);
4188
4189 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004190}
4191
4192/*
4193 * should be called during mount to recover any replay any log trees
4194 * from the FS
4195 */
4196int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4197{
4198 int ret;
4199 struct btrfs_path *path;
4200 struct btrfs_trans_handle *trans;
4201 struct btrfs_key key;
4202 struct btrfs_key found_key;
4203 struct btrfs_key tmp_key;
4204 struct btrfs_root *log;
4205 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4206 struct walk_control wc = {
4207 .process_func = process_one_buffer,
4208 .stage = 0,
4209 };
4210
Chris Masone02119d2008-09-05 16:13:11 -04004211 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004212 if (!path)
4213 return -ENOMEM;
4214
4215 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004216
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004217 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004218 if (IS_ERR(trans)) {
4219 ret = PTR_ERR(trans);
4220 goto error;
4221 }
Chris Masone02119d2008-09-05 16:13:11 -04004222
4223 wc.trans = trans;
4224 wc.pin = 1;
4225
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004226 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004227 if (ret) {
4228 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4229 "recovering log root tree.");
4230 goto error;
4231 }
Chris Masone02119d2008-09-05 16:13:11 -04004232
4233again:
4234 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4235 key.offset = (u64)-1;
4236 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4237
Chris Masond3977122009-01-05 21:25:51 -05004238 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004239 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240
4241 if (ret < 0) {
4242 btrfs_error(fs_info, ret,
4243 "Couldn't find tree log root.");
4244 goto error;
4245 }
Chris Masone02119d2008-09-05 16:13:11 -04004246 if (ret > 0) {
4247 if (path->slots[0] == 0)
4248 break;
4249 path->slots[0]--;
4250 }
4251 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4252 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004253 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004254 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4255 break;
4256
Miao Xiecb517ea2013-05-15 07:48:19 +00004257 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004258 if (IS_ERR(log)) {
4259 ret = PTR_ERR(log);
4260 btrfs_error(fs_info, ret,
4261 "Couldn't read tree log root.");
4262 goto error;
4263 }
Chris Masone02119d2008-09-05 16:13:11 -04004264
4265 tmp_key.objectid = found_key.offset;
4266 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4267 tmp_key.offset = (u64)-1;
4268
4269 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270 if (IS_ERR(wc.replay_dest)) {
4271 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004272 free_extent_buffer(log->node);
4273 free_extent_buffer(log->commit_root);
4274 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004275 btrfs_error(fs_info, ret, "Couldn't read target root "
4276 "for tree log recovery.");
4277 goto error;
4278 }
Chris Masone02119d2008-09-05 16:13:11 -04004279
Yan Zheng07d400a2009-01-06 11:42:00 -05004280 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004281 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004282 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004283
Josef Bacikb50c6e22013-04-25 15:55:30 -04004284 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004285 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4286 path);
Chris Masone02119d2008-09-05 16:13:11 -04004287 }
Chris Masone02119d2008-09-05 16:13:11 -04004288
4289 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004290 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004291 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004292 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004293 kfree(log);
4294
Josef Bacikb50c6e22013-04-25 15:55:30 -04004295 if (ret)
4296 goto error;
4297
Chris Masone02119d2008-09-05 16:13:11 -04004298 if (found_key.offset == 0)
4299 break;
4300 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004301 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004302
4303 /* step one is to pin it all, step two is to replay just inodes */
4304 if (wc.pin) {
4305 wc.pin = 0;
4306 wc.process_func = replay_one_buffer;
4307 wc.stage = LOG_WALK_REPLAY_INODES;
4308 goto again;
4309 }
4310 /* step three is to replay everything */
4311 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4312 wc.stage++;
4313 goto again;
4314 }
4315
4316 btrfs_free_path(path);
4317
Josef Bacikabefa552013-04-24 16:40:05 -04004318 /* step 4: commit the transaction, which also unpins the blocks */
4319 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4320 if (ret)
4321 return ret;
4322
Chris Masone02119d2008-09-05 16:13:11 -04004323 free_extent_buffer(log_root_tree->node);
4324 log_root_tree->log_root = NULL;
4325 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004326 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004327
Josef Bacikabefa552013-04-24 16:40:05 -04004328 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004330 if (wc.trans)
4331 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004332 btrfs_free_path(path);
4333 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004334}
Chris Mason12fcfd22009-03-24 10:24:20 -04004335
4336/*
4337 * there are some corner cases where we want to force a full
4338 * commit instead of allowing a directory to be logged.
4339 *
4340 * They revolve around files there were unlinked from the directory, and
4341 * this function updates the parent directory so that a full commit is
4342 * properly done if it is fsync'd later after the unlinks are done.
4343 */
4344void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4345 struct inode *dir, struct inode *inode,
4346 int for_rename)
4347{
4348 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004349 * when we're logging a file, if it hasn't been renamed
4350 * or unlinked, and its inode is fully committed on disk,
4351 * we don't have to worry about walking up the directory chain
4352 * to log its parents.
4353 *
4354 * So, we use the last_unlink_trans field to put this transid
4355 * into the file. When the file is logged we check it and
4356 * don't log the parents if the file is fully on disk.
4357 */
4358 if (S_ISREG(inode->i_mode))
4359 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4360
4361 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004362 * if this directory was already logged any new
4363 * names for this file/dir will get recorded
4364 */
4365 smp_mb();
4366 if (BTRFS_I(dir)->logged_trans == trans->transid)
4367 return;
4368
4369 /*
4370 * if the inode we're about to unlink was logged,
4371 * the log will be properly updated for any new names
4372 */
4373 if (BTRFS_I(inode)->logged_trans == trans->transid)
4374 return;
4375
4376 /*
4377 * when renaming files across directories, if the directory
4378 * there we're unlinking from gets fsync'd later on, there's
4379 * no way to find the destination directory later and fsync it
4380 * properly. So, we have to be conservative and force commits
4381 * so the new name gets discovered.
4382 */
4383 if (for_rename)
4384 goto record;
4385
4386 /* we can safely do the unlink without any special recording */
4387 return;
4388
4389record:
4390 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4391}
4392
4393/*
4394 * Call this after adding a new name for a file and it will properly
4395 * update the log to reflect the new name.
4396 *
4397 * It will return zero if all goes well, and it will return 1 if a
4398 * full transaction commit is required.
4399 */
4400int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4401 struct inode *inode, struct inode *old_dir,
4402 struct dentry *parent)
4403{
4404 struct btrfs_root * root = BTRFS_I(inode)->root;
4405
4406 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004407 * this will force the logging code to walk the dentry chain
4408 * up for the file
4409 */
4410 if (S_ISREG(inode->i_mode))
4411 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4412
4413 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004414 * if this inode hasn't been logged and directory we're renaming it
4415 * from hasn't been logged, we don't need to log it
4416 */
4417 if (BTRFS_I(inode)->logged_trans <=
4418 root->fs_info->last_trans_committed &&
4419 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4420 root->fs_info->last_trans_committed))
4421 return 0;
4422
4423 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4424}
4425