blob: f2ff02c5513092419597ff8f6e714e1abcb7d205 [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>
Josef Bacik5dc562c2012-08-17 13:14:17 -040021#include <linux/list_sort.h>
Chris Masone02119d2008-09-05 16:13:11 -040022#include "ctree.h"
23#include "transaction.h"
24#include "disk-io.h"
25#include "locking.h"
26#include "print-tree.h"
27#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050028#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040029
30/* magic values for the inode_only field in btrfs_log_inode:
31 *
32 * LOG_INODE_ALL means to log everything
33 * LOG_INODE_EXISTS means to log just enough to recreate the inode
34 * during log replay
35 */
36#define LOG_INODE_ALL 0
37#define LOG_INODE_EXISTS 1
38
39/*
Chris Mason12fcfd22009-03-24 10:24:20 -040040 * directory trouble cases
41 *
42 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
43 * log, we must force a full commit before doing an fsync of the directory
44 * where the unlink was done.
45 * ---> record transid of last unlink/rename per directory
46 *
47 * mkdir foo/some_dir
48 * normal commit
49 * rename foo/some_dir foo2/some_dir
50 * mkdir foo/some_dir
51 * fsync foo/some_dir/some_file
52 *
53 * The fsync above will unlink the original some_dir without recording
54 * it in its new location (foo2). After a crash, some_dir will be gone
55 * unless the fsync of some_file forces a full commit
56 *
57 * 2) we must log any new names for any file or dir that is in the fsync
58 * log. ---> check inode while renaming/linking.
59 *
60 * 2a) we must log any new names for any file or dir during rename
61 * when the directory they are being removed from was logged.
62 * ---> check inode and old parent dir during rename
63 *
64 * 2a is actually the more important variant. With the extra logging
65 * a crash might unlink the old name without recreating the new one
66 *
67 * 3) after a crash, we must go through any directories with a link count
68 * of zero and redo the rm -rf
69 *
70 * mkdir f1/foo
71 * normal commit
72 * rm -rf f1/foo
73 * fsync(f1)
74 *
75 * The directory f1 was fully removed from the FS, but fsync was never
76 * called on f1, only its parent dir. After a crash the rm -rf must
77 * be replayed. This must be able to recurse down the entire
78 * directory tree. The inode link count fixup code takes care of the
79 * ugly details.
80 */
81
82/*
Chris Masone02119d2008-09-05 16:13:11 -040083 * stages for the tree walking. The first
84 * stage (0) is to only pin down the blocks we find
85 * the second stage (1) is to make sure that all the inodes
86 * we find in the log are created in the subvolume.
87 *
88 * The last stage is to deal with directories and links and extents
89 * and all the other fun semantics
90 */
91#define LOG_WALK_PIN_ONLY 0
92#define LOG_WALK_REPLAY_INODES 1
93#define LOG_WALK_REPLAY_ALL 2
94
Chris Mason12fcfd22009-03-24 10:24:20 -040095static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040096 struct btrfs_root *root, struct inode *inode,
97 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -050098static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
99 struct btrfs_root *root,
100 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400101static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root,
103 struct btrfs_root *log,
104 struct btrfs_path *path,
105 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400106
107/*
108 * tree logging is a special write ahead log used to make sure that
109 * fsyncs and O_SYNCs can happen without doing full tree commits.
110 *
111 * Full tree commits are expensive because they require commonly
112 * modified blocks to be recowed, creating many dirty pages in the
113 * extent tree an 4x-6x higher write load than ext3.
114 *
115 * Instead of doing a tree commit on every fsync, we use the
116 * key ranges and transaction ids to find items for a given file or directory
117 * that have changed in this transaction. Those items are copied into
118 * a special tree (one per subvolume root), that tree is written to disk
119 * and then the fsync is considered complete.
120 *
121 * After a crash, items are copied out of the log-tree back into the
122 * subvolume tree. Any file data extents found are recorded in the extent
123 * allocation tree, and the log-tree freed.
124 *
125 * The log tree is read three times, once to pin down all the extents it is
126 * using in ram and once, once to create all the inodes logged in the tree
127 * and once to do all the other items.
128 */
129
130/*
Chris Masone02119d2008-09-05 16:13:11 -0400131 * start a sub transaction and setup the log tree
132 * this increments the log tree writer count to make the people
133 * syncing the tree wait for us to finish
134 */
135static int start_log_trans(struct btrfs_trans_handle *trans,
136 struct btrfs_root *root)
137{
138 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400139 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500140
141 mutex_lock(&root->log_mutex);
142 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400143 if (!root->log_start_pid) {
144 root->log_start_pid = current->pid;
145 root->log_multiple_pids = false;
146 } else if (root->log_start_pid != current->pid) {
147 root->log_multiple_pids = true;
148 }
149
Yan Zheng7237f182009-01-21 12:54:03 -0500150 root->log_batch++;
151 atomic_inc(&root->log_writers);
152 mutex_unlock(&root->log_mutex);
153 return 0;
154 }
Josef Bacikff782e02009-10-08 15:30:04 -0400155 root->log_multiple_pids = false;
156 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400157 mutex_lock(&root->fs_info->tree_log_mutex);
158 if (!root->fs_info->log_root_tree) {
159 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400160 if (ret)
161 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400162 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400163 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400164 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400165 if (ret)
166 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400167 }
Chris Masone02119d2008-09-05 16:13:11 -0400168 mutex_unlock(&root->fs_info->tree_log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -0500169 root->log_batch++;
170 atomic_inc(&root->log_writers);
171 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400172 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400173}
174
175/*
176 * returns 0 if there was a log transaction running and we were able
177 * to join, or returns -ENOENT if there were not transactions
178 * in progress
179 */
180static int join_running_log_trans(struct btrfs_root *root)
181{
182 int ret = -ENOENT;
183
184 smp_mb();
185 if (!root->log_root)
186 return -ENOENT;
187
Yan Zheng7237f182009-01-21 12:54:03 -0500188 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400189 if (root->log_root) {
190 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500191 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400192 }
Yan Zheng7237f182009-01-21 12:54:03 -0500193 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400194 return ret;
195}
196
197/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400198 * This either makes the current running log transaction wait
199 * until you call btrfs_end_log_trans() or it makes any future
200 * log transactions wait until you call btrfs_end_log_trans()
201 */
202int btrfs_pin_log_trans(struct btrfs_root *root)
203{
204 int ret = -ENOENT;
205
206 mutex_lock(&root->log_mutex);
207 atomic_inc(&root->log_writers);
208 mutex_unlock(&root->log_mutex);
209 return ret;
210}
211
212/*
Chris Masone02119d2008-09-05 16:13:11 -0400213 * indicate we're done making changes to the log tree
214 * and wake up anyone waiting to do a sync
215 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100216void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400217{
Yan Zheng7237f182009-01-21 12:54:03 -0500218 if (atomic_dec_and_test(&root->log_writers)) {
219 smp_mb();
220 if (waitqueue_active(&root->log_writer_wait))
221 wake_up(&root->log_writer_wait);
222 }
Chris Masone02119d2008-09-05 16:13:11 -0400223}
224
225
226/*
227 * the walk control struct is used to pass state down the chain when
228 * processing the log tree. The stage field tells us which part
229 * of the log tree processing we are currently doing. The others
230 * are state fields used for that specific part
231 */
232struct walk_control {
233 /* should we free the extent on disk when done? This is used
234 * at transaction commit time while freeing a log tree
235 */
236 int free;
237
238 /* should we write out the extent buffer? This is used
239 * while flushing the log tree to disk during a sync
240 */
241 int write;
242
243 /* should we wait for the extent buffer io to finish? Also used
244 * while flushing the log tree to disk for a sync
245 */
246 int wait;
247
248 /* pin only walk, we record which extents on disk belong to the
249 * log trees
250 */
251 int pin;
252
253 /* what stage of the replay code we're currently in */
254 int stage;
255
256 /* the root we are currently replaying */
257 struct btrfs_root *replay_dest;
258
259 /* the trans handle for the current replay */
260 struct btrfs_trans_handle *trans;
261
262 /* the function that gets used to process blocks we find in the
263 * tree. Note the extent_buffer might not be up to date when it is
264 * passed in, and it must be checked or read if you need the data
265 * inside it
266 */
267 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
268 struct walk_control *wc, u64 gen);
269};
270
271/*
272 * process_func used to pin down extents, write them or wait on them
273 */
274static int process_one_buffer(struct btrfs_root *log,
275 struct extent_buffer *eb,
276 struct walk_control *wc, u64 gen)
277{
Josef Bacik04018de2009-04-03 10:14:18 -0400278 if (wc->pin)
Chris Masone688b7252011-10-31 20:52:39 -0400279 btrfs_pin_extent_for_log_replay(wc->trans,
280 log->fs_info->extent_root,
281 eb->start, eb->len);
Chris Masone02119d2008-09-05 16:13:11 -0400282
Chris Masonb9fab912012-05-06 07:23:47 -0400283 if (btrfs_buffer_uptodate(eb, gen, 0)) {
Chris Masone02119d2008-09-05 16:13:11 -0400284 if (wc->write)
285 btrfs_write_tree_block(eb);
286 if (wc->wait)
287 btrfs_wait_tree_block_writeback(eb);
288 }
289 return 0;
290}
291
292/*
293 * Item overwrite used by replay and tree logging. eb, slot and key all refer
294 * to the src data we are copying out.
295 *
296 * root is the tree we are copying into, and path is a scratch
297 * path for use in this function (it should be released on entry and
298 * will be released on exit).
299 *
300 * If the key is already in the destination tree the existing item is
301 * overwritten. If the existing item isn't big enough, it is extended.
302 * If it is too large, it is truncated.
303 *
304 * If the key isn't in the destination yet, a new item is inserted.
305 */
306static noinline int overwrite_item(struct btrfs_trans_handle *trans,
307 struct btrfs_root *root,
308 struct btrfs_path *path,
309 struct extent_buffer *eb, int slot,
310 struct btrfs_key *key)
311{
312 int ret;
313 u32 item_size;
314 u64 saved_i_size = 0;
315 int save_old_i_size = 0;
316 unsigned long src_ptr;
317 unsigned long dst_ptr;
318 int overwrite_root = 0;
319
320 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
321 overwrite_root = 1;
322
323 item_size = btrfs_item_size_nr(eb, slot);
324 src_ptr = btrfs_item_ptr_offset(eb, slot);
325
326 /* look for the key in the destination tree */
327 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
328 if (ret == 0) {
329 char *src_copy;
330 char *dst_copy;
331 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
332 path->slots[0]);
333 if (dst_size != item_size)
334 goto insert;
335
336 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200337 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400338 return 0;
339 }
340 dst_copy = kmalloc(item_size, GFP_NOFS);
341 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000342 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200343 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000344 kfree(dst_copy);
345 kfree(src_copy);
346 return -ENOMEM;
347 }
Chris Masone02119d2008-09-05 16:13:11 -0400348
349 read_extent_buffer(eb, src_copy, src_ptr, item_size);
350
351 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
352 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
353 item_size);
354 ret = memcmp(dst_copy, src_copy, item_size);
355
356 kfree(dst_copy);
357 kfree(src_copy);
358 /*
359 * they have the same contents, just return, this saves
360 * us from cowing blocks in the destination tree and doing
361 * extra writes that may not have been done by a previous
362 * sync
363 */
364 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200365 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400366 return 0;
367 }
368
369 }
370insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200371 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400372 /* try to insert the key into the destination tree */
373 ret = btrfs_insert_empty_item(trans, root, path,
374 key, item_size);
375
376 /* make sure any existing item is the correct size */
377 if (ret == -EEXIST) {
378 u32 found_size;
379 found_size = btrfs_item_size_nr(path->nodes[0],
380 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100381 if (found_size > item_size)
Chris Masone02119d2008-09-05 16:13:11 -0400382 btrfs_truncate_item(trans, root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100383 else if (found_size < item_size)
384 btrfs_extend_item(trans, root, path,
385 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400386 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400387 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400388 }
389 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
390 path->slots[0]);
391
392 /* don't overwrite an existing inode if the generation number
393 * was logged as zero. This is done when the tree logging code
394 * is just logging an inode to make sure it exists after recovery.
395 *
396 * Also, don't overwrite i_size on directories during replay.
397 * log replay inserts and removes directory items based on the
398 * state of the tree found in the subvolume, and i_size is modified
399 * as it goes
400 */
401 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
402 struct btrfs_inode_item *src_item;
403 struct btrfs_inode_item *dst_item;
404
405 src_item = (struct btrfs_inode_item *)src_ptr;
406 dst_item = (struct btrfs_inode_item *)dst_ptr;
407
408 if (btrfs_inode_generation(eb, src_item) == 0)
409 goto no_copy;
410
411 if (overwrite_root &&
412 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
413 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
414 save_old_i_size = 1;
415 saved_i_size = btrfs_inode_size(path->nodes[0],
416 dst_item);
417 }
418 }
419
420 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
421 src_ptr, item_size);
422
423 if (save_old_i_size) {
424 struct btrfs_inode_item *dst_item;
425 dst_item = (struct btrfs_inode_item *)dst_ptr;
426 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
427 }
428
429 /* make sure the generation is filled in */
430 if (key->type == BTRFS_INODE_ITEM_KEY) {
431 struct btrfs_inode_item *dst_item;
432 dst_item = (struct btrfs_inode_item *)dst_ptr;
433 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
434 btrfs_set_inode_generation(path->nodes[0], dst_item,
435 trans->transid);
436 }
437 }
438no_copy:
439 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200440 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400441 return 0;
442}
443
444/*
445 * simple helper to read an inode off the disk from a given root
446 * This can only be called for subvolume roots and not for the log
447 */
448static noinline struct inode *read_one_inode(struct btrfs_root *root,
449 u64 objectid)
450{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400451 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400452 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400453
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400454 key.objectid = objectid;
455 key.type = BTRFS_INODE_ITEM_KEY;
456 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000457 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400458 if (IS_ERR(inode)) {
459 inode = NULL;
460 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400461 iput(inode);
462 inode = NULL;
463 }
464 return inode;
465}
466
467/* replays a single extent in 'eb' at 'slot' with 'key' into the
468 * subvolume 'root'. path is released on entry and should be released
469 * on exit.
470 *
471 * extents in the log tree have not been allocated out of the extent
472 * tree yet. So, this completes the allocation, taking a reference
473 * as required if the extent already exists or creating a new extent
474 * if it isn't in the extent allocation tree yet.
475 *
476 * The extent is inserted into the file, dropping any existing extents
477 * from the file that overlap the new one.
478 */
479static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root,
481 struct btrfs_path *path,
482 struct extent_buffer *eb, int slot,
483 struct btrfs_key *key)
484{
485 int found_type;
486 u64 mask = root->sectorsize - 1;
487 u64 extent_end;
488 u64 alloc_hint;
489 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500490 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400491 struct btrfs_file_extent_item *item;
492 struct inode *inode = NULL;
493 unsigned long size;
494 int ret = 0;
495
496 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
497 found_type = btrfs_file_extent_type(eb, item);
498
Yan Zhengd899e052008-10-30 14:25:28 -0400499 if (found_type == BTRFS_FILE_EXTENT_REG ||
500 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400501 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
502 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400504 extent_end = (start + size + mask) & ~mask;
505 } else {
506 ret = 0;
507 goto out;
508 }
509
510 inode = read_one_inode(root, key->objectid);
511 if (!inode) {
512 ret = -EIO;
513 goto out;
514 }
515
516 /*
517 * first check to see if we already have this extent in the
518 * file. This must be done before the btrfs_drop_extents run
519 * so we don't try to drop this extent.
520 */
Li Zefan33345d012011-04-20 10:31:50 +0800521 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400522 start, 0);
523
Yan Zhengd899e052008-10-30 14:25:28 -0400524 if (ret == 0 &&
525 (found_type == BTRFS_FILE_EXTENT_REG ||
526 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400527 struct btrfs_file_extent_item cmp1;
528 struct btrfs_file_extent_item cmp2;
529 struct btrfs_file_extent_item *existing;
530 struct extent_buffer *leaf;
531
532 leaf = path->nodes[0];
533 existing = btrfs_item_ptr(leaf, path->slots[0],
534 struct btrfs_file_extent_item);
535
536 read_extent_buffer(eb, &cmp1, (unsigned long)item,
537 sizeof(cmp1));
538 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
539 sizeof(cmp2));
540
541 /*
542 * we already have a pointer to this exact extent,
543 * we don't have to do anything
544 */
545 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200546 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400547 goto out;
548 }
549 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200550 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400551
Yan Zheng07d400a2009-01-06 11:42:00 -0500552 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400553 /* drop any overlapping extents */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400554 ret = btrfs_drop_extents(trans, root, inode, start, extent_end,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000555 &alloc_hint, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400556 BUG_ON(ret);
557
Yan Zheng07d400a2009-01-06 11:42:00 -0500558 if (found_type == BTRFS_FILE_EXTENT_REG ||
559 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500561 unsigned long dest_offset;
562 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400563
Yan Zheng07d400a2009-01-06 11:42:00 -0500564 ret = btrfs_insert_empty_item(trans, root, path, key,
565 sizeof(*item));
566 BUG_ON(ret);
567 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
568 path->slots[0]);
569 copy_extent_buffer(path->nodes[0], eb, dest_offset,
570 (unsigned long)item, sizeof(*item));
571
572 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
573 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
574 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400575 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500576
577 if (ins.objectid > 0) {
578 u64 csum_start;
579 u64 csum_end;
580 LIST_HEAD(ordered_sums);
581 /*
582 * is this extent already allocated in the extent
583 * allocation tree? If so, just add a reference
584 */
585 ret = btrfs_lookup_extent(root, ins.objectid,
586 ins.offset);
587 if (ret == 0) {
588 ret = btrfs_inc_extent_ref(trans, root,
589 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200591 key->objectid, offset, 0);
Tsutomu Itoh37daa4f2011-04-28 09:18:21 +0000592 BUG_ON(ret);
Yan Zheng07d400a2009-01-06 11:42:00 -0500593 } else {
594 /*
595 * insert the extent pointer in the extent
596 * allocation tree
597 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 ret = btrfs_alloc_logged_file_extent(trans,
599 root, root->root_key.objectid,
600 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500601 BUG_ON(ret);
602 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200603 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500604
605 if (btrfs_file_extent_compression(eb, item)) {
606 csum_start = ins.objectid;
607 csum_end = csum_start + ins.offset;
608 } else {
609 csum_start = ins.objectid +
610 btrfs_file_extent_offset(eb, item);
611 csum_end = csum_start +
612 btrfs_file_extent_num_bytes(eb, item);
613 }
614
615 ret = btrfs_lookup_csums_range(root->log_root,
616 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100617 &ordered_sums, 0);
Yan Zheng07d400a2009-01-06 11:42:00 -0500618 BUG_ON(ret);
619 while (!list_empty(&ordered_sums)) {
620 struct btrfs_ordered_sum *sums;
621 sums = list_entry(ordered_sums.next,
622 struct btrfs_ordered_sum,
623 list);
624 ret = btrfs_csum_file_blocks(trans,
625 root->fs_info->csum_root,
626 sums);
627 BUG_ON(ret);
628 list_del(&sums->list);
629 kfree(sums);
630 }
631 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200632 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500633 }
634 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
635 /* inline extents are easy, we just overwrite them */
636 ret = overwrite_item(trans, root, path, eb, slot, key);
637 BUG_ON(ret);
638 }
639
640 inode_set_bytes(inode, saved_nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600641 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400642out:
643 if (inode)
644 iput(inode);
645 return ret;
646}
647
648/*
649 * when cleaning up conflicts between the directory names in the
650 * subvolume, directory names in the log and directory names in the
651 * inode back references, we may have to unlink inodes from directories.
652 *
653 * This is a helper function to do the unlink of a specific directory
654 * item
655 */
656static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
657 struct btrfs_root *root,
658 struct btrfs_path *path,
659 struct inode *dir,
660 struct btrfs_dir_item *di)
661{
662 struct inode *inode;
663 char *name;
664 int name_len;
665 struct extent_buffer *leaf;
666 struct btrfs_key location;
667 int ret;
668
669 leaf = path->nodes[0];
670
671 btrfs_dir_item_key_to_cpu(leaf, di, &location);
672 name_len = btrfs_dir_name_len(leaf, di);
673 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000674 if (!name)
675 return -ENOMEM;
676
Chris Masone02119d2008-09-05 16:13:11 -0400677 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200678 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400679
680 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000681 if (!inode) {
682 kfree(name);
683 return -EIO;
684 }
Chris Masone02119d2008-09-05 16:13:11 -0400685
Yan Zhengec051c02009-01-05 15:43:42 -0500686 ret = link_to_fixup_dir(trans, root, path, location.objectid);
687 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400688
Chris Masone02119d2008-09-05 16:13:11 -0400689 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500690 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400691 kfree(name);
692
693 iput(inode);
Chris Masonb6305562012-07-02 15:29:53 -0400694
695 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400696 return ret;
697}
698
699/*
700 * helper function to see if a given name and sequence number found
701 * in an inode back reference are already in a directory and correctly
702 * point to this inode
703 */
704static noinline int inode_in_dir(struct btrfs_root *root,
705 struct btrfs_path *path,
706 u64 dirid, u64 objectid, u64 index,
707 const char *name, int name_len)
708{
709 struct btrfs_dir_item *di;
710 struct btrfs_key location;
711 int match = 0;
712
713 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
714 index, name, name_len, 0);
715 if (di && !IS_ERR(di)) {
716 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
717 if (location.objectid != objectid)
718 goto out;
719 } else
720 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200721 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400722
723 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
724 if (di && !IS_ERR(di)) {
725 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
726 if (location.objectid != objectid)
727 goto out;
728 } else
729 goto out;
730 match = 1;
731out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200732 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400733 return match;
734}
735
736/*
737 * helper function to check a log tree for a named back reference in
738 * an inode. This is used to decide if a back reference that is
739 * found in the subvolume conflicts with what we find in the log.
740 *
741 * inode backreferences may have multiple refs in a single item,
742 * during replay we process one reference at a time, and we don't
743 * want to delete valid links to a file from the subvolume if that
744 * link is also in the log.
745 */
746static noinline int backref_in_log(struct btrfs_root *log,
747 struct btrfs_key *key,
748 char *name, int namelen)
749{
750 struct btrfs_path *path;
751 struct btrfs_inode_ref *ref;
752 unsigned long ptr;
753 unsigned long ptr_end;
754 unsigned long name_ptr;
755 int found_name_len;
756 int item_size;
757 int ret;
758 int match = 0;
759
760 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000761 if (!path)
762 return -ENOMEM;
763
Chris Masone02119d2008-09-05 16:13:11 -0400764 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
765 if (ret != 0)
766 goto out;
767
768 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
769 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
770 ptr_end = ptr + item_size;
771 while (ptr < ptr_end) {
772 ref = (struct btrfs_inode_ref *)ptr;
773 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
774 if (found_name_len == namelen) {
775 name_ptr = (unsigned long)(ref + 1);
776 ret = memcmp_extent_buffer(path->nodes[0], name,
777 name_ptr, namelen);
778 if (ret == 0) {
779 match = 1;
780 goto out;
781 }
782 }
783 ptr = (unsigned long)(ref + 1) + found_name_len;
784 }
785out:
786 btrfs_free_path(path);
787 return match;
788}
789
790
791/*
792 * replay one inode back reference item found in the log tree.
793 * eb, slot and key refer to the buffer and key found in the log tree.
794 * root is the destination we are replaying into, and path is for temp
795 * use by this function. (it should be released on return).
796 */
797static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
798 struct btrfs_root *root,
799 struct btrfs_root *log,
800 struct btrfs_path *path,
801 struct extent_buffer *eb, int slot,
802 struct btrfs_key *key)
803{
Chris Masone02119d2008-09-05 16:13:11 -0400804 struct btrfs_inode_ref *ref;
liubo34f3e4f2011-08-06 08:35:23 +0000805 struct btrfs_dir_item *di;
806 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -0400807 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400808 unsigned long ref_ptr;
809 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +0000810 char *name;
811 int namelen;
812 int ret;
liuboc622ae62011-03-26 08:01:12 -0400813 int search_done = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400814
Chris Masone02119d2008-09-05 16:13:11 -0400815 /*
816 * it is possible that we didn't log all the parent directories
817 * for a given inode. If we don't find the dir, just don't
818 * copy the back ref in. The link count fixup code will take
819 * care of the rest
820 */
821 dir = read_one_inode(root, key->offset);
822 if (!dir)
823 return -ENOENT;
824
825 inode = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000826 if (!inode) {
827 iput(dir);
828 return -EIO;
829 }
Chris Masone02119d2008-09-05 16:13:11 -0400830
831 ref_ptr = btrfs_item_ptr_offset(eb, slot);
832 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
833
834again:
835 ref = (struct btrfs_inode_ref *)ref_ptr;
836
837 namelen = btrfs_inode_ref_name_len(eb, ref);
838 name = kmalloc(namelen, GFP_NOFS);
839 BUG_ON(!name);
840
841 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
842
843 /* if we already have a perfect match, we're done */
Li Zefan33345d012011-04-20 10:31:50 +0800844 if (inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400845 btrfs_inode_ref_index(eb, ref),
846 name, namelen)) {
847 goto out;
848 }
849
850 /*
851 * look for a conflicting back reference in the metadata.
852 * if we find one we have to unlink that name of the file
853 * before we add our new link. Later on, we overwrite any
854 * existing back reference, and we don't want to create
855 * dangling pointers in the directory.
856 */
liuboc622ae62011-03-26 08:01:12 -0400857
858 if (search_done)
859 goto insert;
860
Chris Masone02119d2008-09-05 16:13:11 -0400861 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
862 if (ret == 0) {
863 char *victim_name;
864 int victim_name_len;
865 struct btrfs_inode_ref *victim_ref;
866 unsigned long ptr;
867 unsigned long ptr_end;
868 struct extent_buffer *leaf = path->nodes[0];
869
870 /* are we trying to overwrite a back ref for the root directory
871 * if so, just jump out, we're done
872 */
873 if (key->objectid == key->offset)
874 goto out_nowrite;
875
876 /* check all the names in this back reference to see
877 * if they are in the log. if so, we allow them to stay
878 * otherwise they must be unlinked as a conflict
879 */
880 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
881 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500882 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400883 victim_ref = (struct btrfs_inode_ref *)ptr;
884 victim_name_len = btrfs_inode_ref_name_len(leaf,
885 victim_ref);
886 victim_name = kmalloc(victim_name_len, GFP_NOFS);
887 BUG_ON(!victim_name);
888
889 read_extent_buffer(leaf, victim_name,
890 (unsigned long)(victim_ref + 1),
891 victim_name_len);
892
893 if (!backref_in_log(log, key, victim_name,
894 victim_name_len)) {
895 btrfs_inc_nlink(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +0200896 btrfs_release_path(path);
Chris Mason12fcfd22009-03-24 10:24:20 -0400897
Chris Masone02119d2008-09-05 16:13:11 -0400898 ret = btrfs_unlink_inode(trans, root, dir,
899 inode, victim_name,
900 victim_name_len);
Chris Masonb6305562012-07-02 15:29:53 -0400901 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400902 }
903 kfree(victim_name);
904 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
905 }
906 BUG_ON(ret);
liuboc622ae62011-03-26 08:01:12 -0400907
908 /*
909 * NOTE: we have searched root tree and checked the
910 * coresponding ref, it does not need to check again.
911 */
912 search_done = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400913 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200914 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400915
liubo34f3e4f2011-08-06 08:35:23 +0000916 /* look for a conflicting sequence number */
917 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
918 btrfs_inode_ref_index(eb, ref),
919 name, namelen, 0);
920 if (di && !IS_ERR(di)) {
921 ret = drop_one_dir_item(trans, root, path, dir, di);
922 BUG_ON(ret);
923 }
924 btrfs_release_path(path);
925
926 /* look for a conflicing name */
927 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
928 name, namelen, 0);
929 if (di && !IS_ERR(di)) {
930 ret = drop_one_dir_item(trans, root, path, dir, di);
931 BUG_ON(ret);
932 }
933 btrfs_release_path(path);
934
liuboc622ae62011-03-26 08:01:12 -0400935insert:
Chris Masone02119d2008-09-05 16:13:11 -0400936 /* insert our name */
937 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
938 btrfs_inode_ref_index(eb, ref));
939 BUG_ON(ret);
940
941 btrfs_update_inode(trans, root, inode);
942
943out:
944 ref_ptr = (unsigned long)(ref + 1) + namelen;
945 kfree(name);
946 if (ref_ptr < ref_end)
947 goto again;
948
949 /* finally write the back reference in the inode */
950 ret = overwrite_item(trans, root, path, eb, slot, key);
951 BUG_ON(ret);
952
953out_nowrite:
David Sterbab3b4aa72011-04-21 01:20:15 +0200954 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400955 iput(dir);
956 iput(inode);
957 return 0;
958}
959
Yan, Zhengc71bf092009-11-12 09:34:40 +0000960static int insert_orphan_item(struct btrfs_trans_handle *trans,
961 struct btrfs_root *root, u64 offset)
962{
963 int ret;
964 ret = btrfs_find_orphan_item(root, offset);
965 if (ret > 0)
966 ret = btrfs_insert_orphan_item(trans, root, offset);
967 return ret;
968}
969
970
Chris Masone02119d2008-09-05 16:13:11 -0400971/*
Chris Masone02119d2008-09-05 16:13:11 -0400972 * There are a few corners where the link count of the file can't
973 * be properly maintained during replay. So, instead of adding
974 * lots of complexity to the log code, we just scan the backrefs
975 * for any file that has been through replay.
976 *
977 * The scan will update the link count on the inode to reflect the
978 * number of back refs found. If it goes down to zero, the iput
979 * will free the inode.
980 */
981static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
982 struct btrfs_root *root,
983 struct inode *inode)
984{
985 struct btrfs_path *path;
986 int ret;
987 struct btrfs_key key;
988 u64 nlink = 0;
989 unsigned long ptr;
990 unsigned long ptr_end;
991 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +0800992 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400993
Li Zefan33345d012011-04-20 10:31:50 +0800994 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -0400995 key.type = BTRFS_INODE_REF_KEY;
996 key.offset = (u64)-1;
997
998 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000999 if (!path)
1000 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001001
Chris Masond3977122009-01-05 21:25:51 -05001002 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001003 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1004 if (ret < 0)
1005 break;
1006 if (ret > 0) {
1007 if (path->slots[0] == 0)
1008 break;
1009 path->slots[0]--;
1010 }
1011 btrfs_item_key_to_cpu(path->nodes[0], &key,
1012 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001013 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001014 key.type != BTRFS_INODE_REF_KEY)
1015 break;
1016 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1017 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1018 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001019 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001020 struct btrfs_inode_ref *ref;
1021
1022 ref = (struct btrfs_inode_ref *)ptr;
1023 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1024 ref);
1025 ptr = (unsigned long)(ref + 1) + name_len;
1026 nlink++;
1027 }
1028
1029 if (key.offset == 0)
1030 break;
1031 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001032 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001033 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001034 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001035 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001036 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001037 btrfs_update_inode(trans, root, inode);
1038 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001039 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001040
Yan, Zhengc71bf092009-11-12 09:34:40 +00001041 if (inode->i_nlink == 0) {
1042 if (S_ISDIR(inode->i_mode)) {
1043 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001044 ino, 1);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001045 BUG_ON(ret);
1046 }
Li Zefan33345d012011-04-20 10:31:50 +08001047 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001048 BUG_ON(ret);
1049 }
1050 btrfs_free_path(path);
1051
Chris Masone02119d2008-09-05 16:13:11 -04001052 return 0;
1053}
1054
1055static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1056 struct btrfs_root *root,
1057 struct btrfs_path *path)
1058{
1059 int ret;
1060 struct btrfs_key key;
1061 struct inode *inode;
1062
1063 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1064 key.type = BTRFS_ORPHAN_ITEM_KEY;
1065 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001066 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001067 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1068 if (ret < 0)
1069 break;
1070
1071 if (ret == 1) {
1072 if (path->slots[0] == 0)
1073 break;
1074 path->slots[0]--;
1075 }
1076
1077 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1078 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1079 key.type != BTRFS_ORPHAN_ITEM_KEY)
1080 break;
1081
1082 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001083 if (ret)
1084 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001085
David Sterbab3b4aa72011-04-21 01:20:15 +02001086 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001087 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001088 if (!inode)
1089 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001090
1091 ret = fixup_inode_link_count(trans, root, inode);
1092 BUG_ON(ret);
1093
1094 iput(inode);
1095
Chris Mason12fcfd22009-03-24 10:24:20 -04001096 /*
1097 * fixup on a directory may create new entries,
1098 * make sure we always look for the highset possible
1099 * offset
1100 */
1101 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001102 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001103 ret = 0;
1104out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001105 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001106 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001107}
1108
1109
1110/*
1111 * record a given inode in the fixup dir so we can check its link
1112 * count when replay is done. The link count is incremented here
1113 * so the inode won't go away until we check it
1114 */
1115static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_path *path,
1118 u64 objectid)
1119{
1120 struct btrfs_key key;
1121 int ret = 0;
1122 struct inode *inode;
1123
1124 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001125 if (!inode)
1126 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001127
1128 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1129 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1130 key.offset = objectid;
1131
1132 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1133
David Sterbab3b4aa72011-04-21 01:20:15 +02001134 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001135 if (ret == 0) {
1136 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001137 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001138 } else if (ret == -EEXIST) {
1139 ret = 0;
1140 } else {
1141 BUG();
1142 }
1143 iput(inode);
1144
1145 return ret;
1146}
1147
1148/*
1149 * when replaying the log for a directory, we only insert names
1150 * for inodes that actually exist. This means an fsync on a directory
1151 * does not implicitly fsync all the new files in it
1152 */
1153static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1154 struct btrfs_root *root,
1155 struct btrfs_path *path,
1156 u64 dirid, u64 index,
1157 char *name, int name_len, u8 type,
1158 struct btrfs_key *location)
1159{
1160 struct inode *inode;
1161 struct inode *dir;
1162 int ret;
1163
1164 inode = read_one_inode(root, location->objectid);
1165 if (!inode)
1166 return -ENOENT;
1167
1168 dir = read_one_inode(root, dirid);
1169 if (!dir) {
1170 iput(inode);
1171 return -EIO;
1172 }
1173 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1174
1175 /* FIXME, put inode into FIXUP list */
1176
1177 iput(inode);
1178 iput(dir);
1179 return ret;
1180}
1181
1182/*
1183 * take a single entry in a log directory item and replay it into
1184 * the subvolume.
1185 *
1186 * if a conflicting item exists in the subdirectory already,
1187 * the inode it points to is unlinked and put into the link count
1188 * fix up tree.
1189 *
1190 * If a name from the log points to a file or directory that does
1191 * not exist in the FS, it is skipped. fsyncs on directories
1192 * do not force down inodes inside that directory, just changes to the
1193 * names or unlinks in a directory.
1194 */
1195static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *root,
1197 struct btrfs_path *path,
1198 struct extent_buffer *eb,
1199 struct btrfs_dir_item *di,
1200 struct btrfs_key *key)
1201{
1202 char *name;
1203 int name_len;
1204 struct btrfs_dir_item *dst_di;
1205 struct btrfs_key found_key;
1206 struct btrfs_key log_key;
1207 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001208 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001209 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001210 int ret;
1211
1212 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001213 if (!dir)
1214 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001215
1216 name_len = btrfs_dir_name_len(eb, di);
1217 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001218 if (!name)
1219 return -ENOMEM;
1220
Chris Masone02119d2008-09-05 16:13:11 -04001221 log_type = btrfs_dir_type(eb, di);
1222 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1223 name_len);
1224
1225 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001226 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1227 if (exists == 0)
1228 exists = 1;
1229 else
1230 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001231 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001232
Chris Masone02119d2008-09-05 16:13:11 -04001233 if (key->type == BTRFS_DIR_ITEM_KEY) {
1234 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1235 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001236 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001237 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1238 key->objectid,
1239 key->offset, name,
1240 name_len, 1);
1241 } else {
1242 BUG();
1243 }
David Sterbac7040052011-04-19 18:00:01 +02001244 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001245 /* we need a sequence number to insert, so we only
1246 * do inserts for the BTRFS_DIR_INDEX_KEY types
1247 */
1248 if (key->type != BTRFS_DIR_INDEX_KEY)
1249 goto out;
1250 goto insert;
1251 }
1252
1253 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1254 /* the existing item matches the logged item */
1255 if (found_key.objectid == log_key.objectid &&
1256 found_key.type == log_key.type &&
1257 found_key.offset == log_key.offset &&
1258 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1259 goto out;
1260 }
1261
1262 /*
1263 * don't drop the conflicting directory entry if the inode
1264 * for the new entry doesn't exist
1265 */
Chris Mason4bef0842008-09-08 11:18:08 -04001266 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001267 goto out;
1268
Chris Masone02119d2008-09-05 16:13:11 -04001269 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1270 BUG_ON(ret);
1271
1272 if (key->type == BTRFS_DIR_INDEX_KEY)
1273 goto insert;
1274out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001275 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001276 kfree(name);
1277 iput(dir);
1278 return 0;
1279
1280insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001281 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001282 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1283 name, name_len, log_type, &log_key);
1284
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001285 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001286 goto out;
1287}
1288
1289/*
1290 * find all the names in a directory item and reconcile them into
1291 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1292 * one name in a directory item, but the same code gets used for
1293 * both directory index types
1294 */
1295static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1296 struct btrfs_root *root,
1297 struct btrfs_path *path,
1298 struct extent_buffer *eb, int slot,
1299 struct btrfs_key *key)
1300{
1301 int ret;
1302 u32 item_size = btrfs_item_size_nr(eb, slot);
1303 struct btrfs_dir_item *di;
1304 int name_len;
1305 unsigned long ptr;
1306 unsigned long ptr_end;
1307
1308 ptr = btrfs_item_ptr_offset(eb, slot);
1309 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001310 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001311 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001312 if (verify_dir_item(root, eb, di))
1313 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001314 name_len = btrfs_dir_name_len(eb, di);
1315 ret = replay_one_name(trans, root, path, eb, di, key);
1316 BUG_ON(ret);
1317 ptr = (unsigned long)(di + 1);
1318 ptr += name_len;
1319 }
1320 return 0;
1321}
1322
1323/*
1324 * directory replay has two parts. There are the standard directory
1325 * items in the log copied from the subvolume, and range items
1326 * created in the log while the subvolume was logged.
1327 *
1328 * The range items tell us which parts of the key space the log
1329 * is authoritative for. During replay, if a key in the subvolume
1330 * directory is in a logged range item, but not actually in the log
1331 * that means it was deleted from the directory before the fsync
1332 * and should be removed.
1333 */
1334static noinline int find_dir_range(struct btrfs_root *root,
1335 struct btrfs_path *path,
1336 u64 dirid, int key_type,
1337 u64 *start_ret, u64 *end_ret)
1338{
1339 struct btrfs_key key;
1340 u64 found_end;
1341 struct btrfs_dir_log_item *item;
1342 int ret;
1343 int nritems;
1344
1345 if (*start_ret == (u64)-1)
1346 return 1;
1347
1348 key.objectid = dirid;
1349 key.type = key_type;
1350 key.offset = *start_ret;
1351
1352 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1353 if (ret < 0)
1354 goto out;
1355 if (ret > 0) {
1356 if (path->slots[0] == 0)
1357 goto out;
1358 path->slots[0]--;
1359 }
1360 if (ret != 0)
1361 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1362
1363 if (key.type != key_type || key.objectid != dirid) {
1364 ret = 1;
1365 goto next;
1366 }
1367 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1368 struct btrfs_dir_log_item);
1369 found_end = btrfs_dir_log_end(path->nodes[0], item);
1370
1371 if (*start_ret >= key.offset && *start_ret <= found_end) {
1372 ret = 0;
1373 *start_ret = key.offset;
1374 *end_ret = found_end;
1375 goto out;
1376 }
1377 ret = 1;
1378next:
1379 /* check the next slot in the tree to see if it is a valid item */
1380 nritems = btrfs_header_nritems(path->nodes[0]);
1381 if (path->slots[0] >= nritems) {
1382 ret = btrfs_next_leaf(root, path);
1383 if (ret)
1384 goto out;
1385 } else {
1386 path->slots[0]++;
1387 }
1388
1389 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1390
1391 if (key.type != key_type || key.objectid != dirid) {
1392 ret = 1;
1393 goto out;
1394 }
1395 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1396 struct btrfs_dir_log_item);
1397 found_end = btrfs_dir_log_end(path->nodes[0], item);
1398 *start_ret = key.offset;
1399 *end_ret = found_end;
1400 ret = 0;
1401out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001402 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001403 return ret;
1404}
1405
1406/*
1407 * this looks for a given directory item in the log. If the directory
1408 * item is not in the log, the item is removed and the inode it points
1409 * to is unlinked
1410 */
1411static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1412 struct btrfs_root *root,
1413 struct btrfs_root *log,
1414 struct btrfs_path *path,
1415 struct btrfs_path *log_path,
1416 struct inode *dir,
1417 struct btrfs_key *dir_key)
1418{
1419 int ret;
1420 struct extent_buffer *eb;
1421 int slot;
1422 u32 item_size;
1423 struct btrfs_dir_item *di;
1424 struct btrfs_dir_item *log_di;
1425 int name_len;
1426 unsigned long ptr;
1427 unsigned long ptr_end;
1428 char *name;
1429 struct inode *inode;
1430 struct btrfs_key location;
1431
1432again:
1433 eb = path->nodes[0];
1434 slot = path->slots[0];
1435 item_size = btrfs_item_size_nr(eb, slot);
1436 ptr = btrfs_item_ptr_offset(eb, slot);
1437 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001438 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001439 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001440 if (verify_dir_item(root, eb, di)) {
1441 ret = -EIO;
1442 goto out;
1443 }
1444
Chris Masone02119d2008-09-05 16:13:11 -04001445 name_len = btrfs_dir_name_len(eb, di);
1446 name = kmalloc(name_len, GFP_NOFS);
1447 if (!name) {
1448 ret = -ENOMEM;
1449 goto out;
1450 }
1451 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1452 name_len);
1453 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001454 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001455 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1456 dir_key->objectid,
1457 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001458 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001459 log_di = btrfs_lookup_dir_index_item(trans, log,
1460 log_path,
1461 dir_key->objectid,
1462 dir_key->offset,
1463 name, name_len, 0);
1464 }
David Sterbac7040052011-04-19 18:00:01 +02001465 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001466 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001467 btrfs_release_path(path);
1468 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001469 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001470 if (!inode) {
1471 kfree(name);
1472 return -EIO;
1473 }
Chris Masone02119d2008-09-05 16:13:11 -04001474
1475 ret = link_to_fixup_dir(trans, root,
1476 path, location.objectid);
1477 BUG_ON(ret);
1478 btrfs_inc_nlink(inode);
1479 ret = btrfs_unlink_inode(trans, root, dir, inode,
1480 name, name_len);
1481 BUG_ON(ret);
Chris Masonb6305562012-07-02 15:29:53 -04001482
1483 btrfs_run_delayed_items(trans, root);
1484
Chris Masone02119d2008-09-05 16:13:11 -04001485 kfree(name);
1486 iput(inode);
1487
1488 /* there might still be more names under this key
1489 * check and repeat if required
1490 */
1491 ret = btrfs_search_slot(NULL, root, dir_key, path,
1492 0, 0);
1493 if (ret == 0)
1494 goto again;
1495 ret = 0;
1496 goto out;
1497 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001498 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001499 kfree(name);
1500
1501 ptr = (unsigned long)(di + 1);
1502 ptr += name_len;
1503 }
1504 ret = 0;
1505out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001506 btrfs_release_path(path);
1507 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001508 return ret;
1509}
1510
1511/*
1512 * deletion replay happens before we copy any new directory items
1513 * out of the log or out of backreferences from inodes. It
1514 * scans the log to find ranges of keys that log is authoritative for,
1515 * and then scans the directory to find items in those ranges that are
1516 * not present in the log.
1517 *
1518 * Anything we don't find in the log is unlinked and removed from the
1519 * directory.
1520 */
1521static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1522 struct btrfs_root *root,
1523 struct btrfs_root *log,
1524 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001525 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001526{
1527 u64 range_start;
1528 u64 range_end;
1529 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1530 int ret = 0;
1531 struct btrfs_key dir_key;
1532 struct btrfs_key found_key;
1533 struct btrfs_path *log_path;
1534 struct inode *dir;
1535
1536 dir_key.objectid = dirid;
1537 dir_key.type = BTRFS_DIR_ITEM_KEY;
1538 log_path = btrfs_alloc_path();
1539 if (!log_path)
1540 return -ENOMEM;
1541
1542 dir = read_one_inode(root, dirid);
1543 /* it isn't an error if the inode isn't there, that can happen
1544 * because we replay the deletes before we copy in the inode item
1545 * from the log
1546 */
1547 if (!dir) {
1548 btrfs_free_path(log_path);
1549 return 0;
1550 }
1551again:
1552 range_start = 0;
1553 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001554 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001555 if (del_all)
1556 range_end = (u64)-1;
1557 else {
1558 ret = find_dir_range(log, path, dirid, key_type,
1559 &range_start, &range_end);
1560 if (ret != 0)
1561 break;
1562 }
Chris Masone02119d2008-09-05 16:13:11 -04001563
1564 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001565 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001566 int nritems;
1567 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1568 0, 0);
1569 if (ret < 0)
1570 goto out;
1571
1572 nritems = btrfs_header_nritems(path->nodes[0]);
1573 if (path->slots[0] >= nritems) {
1574 ret = btrfs_next_leaf(root, path);
1575 if (ret)
1576 break;
1577 }
1578 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1579 path->slots[0]);
1580 if (found_key.objectid != dirid ||
1581 found_key.type != dir_key.type)
1582 goto next_type;
1583
1584 if (found_key.offset > range_end)
1585 break;
1586
1587 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001588 log_path, dir,
1589 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001590 BUG_ON(ret);
1591 if (found_key.offset == (u64)-1)
1592 break;
1593 dir_key.offset = found_key.offset + 1;
1594 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001595 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001596 if (range_end == (u64)-1)
1597 break;
1598 range_start = range_end + 1;
1599 }
1600
1601next_type:
1602 ret = 0;
1603 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1604 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1605 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001606 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001607 goto again;
1608 }
1609out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001610 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001611 btrfs_free_path(log_path);
1612 iput(dir);
1613 return ret;
1614}
1615
1616/*
1617 * the process_func used to replay items from the log tree. This
1618 * gets called in two different stages. The first stage just looks
1619 * for inodes and makes sure they are all copied into the subvolume.
1620 *
1621 * The second stage copies all the other item types from the log into
1622 * the subvolume. The two stage approach is slower, but gets rid of
1623 * lots of complexity around inodes referencing other inodes that exist
1624 * only in the log (references come from either directory items or inode
1625 * back refs).
1626 */
1627static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1628 struct walk_control *wc, u64 gen)
1629{
1630 int nritems;
1631 struct btrfs_path *path;
1632 struct btrfs_root *root = wc->replay_dest;
1633 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001634 int level;
1635 int i;
1636 int ret;
1637
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001638 ret = btrfs_read_buffer(eb, gen);
1639 if (ret)
1640 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001641
1642 level = btrfs_header_level(eb);
1643
1644 if (level != 0)
1645 return 0;
1646
1647 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001648 if (!path)
1649 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001650
1651 nritems = btrfs_header_nritems(eb);
1652 for (i = 0; i < nritems; i++) {
1653 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001654
1655 /* inode keys are done during the first stage */
1656 if (key.type == BTRFS_INODE_ITEM_KEY &&
1657 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001658 struct btrfs_inode_item *inode_item;
1659 u32 mode;
1660
1661 inode_item = btrfs_item_ptr(eb, i,
1662 struct btrfs_inode_item);
1663 mode = btrfs_inode_mode(eb, inode_item);
1664 if (S_ISDIR(mode)) {
1665 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001666 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001667 BUG_ON(ret);
1668 }
1669 ret = overwrite_item(wc->trans, root, path,
1670 eb, i, &key);
1671 BUG_ON(ret);
1672
Yan, Zhengc71bf092009-11-12 09:34:40 +00001673 /* for regular files, make sure corresponding
1674 * orhpan item exist. extents past the new EOF
1675 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001676 */
1677 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001678 ret = insert_orphan_item(wc->trans, root,
1679 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001680 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001681 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001682
Chris Masone02119d2008-09-05 16:13:11 -04001683 ret = link_to_fixup_dir(wc->trans, root,
1684 path, key.objectid);
1685 BUG_ON(ret);
1686 }
1687 if (wc->stage < LOG_WALK_REPLAY_ALL)
1688 continue;
1689
1690 /* these keys are simply copied */
1691 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1692 ret = overwrite_item(wc->trans, root, path,
1693 eb, i, &key);
1694 BUG_ON(ret);
1695 } else if (key.type == BTRFS_INODE_REF_KEY) {
1696 ret = add_inode_ref(wc->trans, root, log, path,
1697 eb, i, &key);
1698 BUG_ON(ret && ret != -ENOENT);
1699 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1700 ret = replay_one_extent(wc->trans, root, path,
1701 eb, i, &key);
1702 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001703 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1704 key.type == BTRFS_DIR_INDEX_KEY) {
1705 ret = replay_one_dir_item(wc->trans, root, path,
1706 eb, i, &key);
1707 BUG_ON(ret);
1708 }
1709 }
1710 btrfs_free_path(path);
1711 return 0;
1712}
1713
Chris Masond3977122009-01-05 21:25:51 -05001714static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001715 struct btrfs_root *root,
1716 struct btrfs_path *path, int *level,
1717 struct walk_control *wc)
1718{
1719 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001720 u64 bytenr;
1721 u64 ptr_gen;
1722 struct extent_buffer *next;
1723 struct extent_buffer *cur;
1724 struct extent_buffer *parent;
1725 u32 blocksize;
1726 int ret = 0;
1727
1728 WARN_ON(*level < 0);
1729 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1730
Chris Masond3977122009-01-05 21:25:51 -05001731 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001732 WARN_ON(*level < 0);
1733 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1734 cur = path->nodes[*level];
1735
1736 if (btrfs_header_level(cur) != *level)
1737 WARN_ON(1);
1738
1739 if (path->slots[*level] >=
1740 btrfs_header_nritems(cur))
1741 break;
1742
1743 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1744 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1745 blocksize = btrfs_level_size(root, *level - 1);
1746
1747 parent = path->nodes[*level];
1748 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001749
1750 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00001751 if (!next)
1752 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001753
Chris Masone02119d2008-09-05 16:13:11 -04001754 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001755 ret = wc->process_func(root, next, wc, ptr_gen);
1756 if (ret)
1757 return ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001758
Chris Masone02119d2008-09-05 16:13:11 -04001759 path->slots[*level]++;
1760 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001761 ret = btrfs_read_buffer(next, ptr_gen);
1762 if (ret) {
1763 free_extent_buffer(next);
1764 return ret;
1765 }
Chris Masone02119d2008-09-05 16:13:11 -04001766
1767 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001768 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001769 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04001770 btrfs_wait_tree_block_writeback(next);
1771 btrfs_tree_unlock(next);
1772
Chris Masone02119d2008-09-05 16:13:11 -04001773 WARN_ON(root_owner !=
1774 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04001775 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04001776 bytenr, blocksize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001777 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04001778 }
1779 free_extent_buffer(next);
1780 continue;
1781 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001782 ret = btrfs_read_buffer(next, ptr_gen);
1783 if (ret) {
1784 free_extent_buffer(next);
1785 return ret;
1786 }
Chris Masone02119d2008-09-05 16:13:11 -04001787
1788 WARN_ON(*level <= 0);
1789 if (path->nodes[*level-1])
1790 free_extent_buffer(path->nodes[*level-1]);
1791 path->nodes[*level-1] = next;
1792 *level = btrfs_header_level(next);
1793 path->slots[*level] = 0;
1794 cond_resched();
1795 }
1796 WARN_ON(*level < 0);
1797 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1798
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001799 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04001800
1801 cond_resched();
1802 return 0;
1803}
1804
Chris Masond3977122009-01-05 21:25:51 -05001805static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001806 struct btrfs_root *root,
1807 struct btrfs_path *path, int *level,
1808 struct walk_control *wc)
1809{
1810 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001811 int i;
1812 int slot;
1813 int ret;
1814
Chris Masond3977122009-01-05 21:25:51 -05001815 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04001816 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001817 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04001818 path->slots[i]++;
1819 *level = i;
1820 WARN_ON(*level == 0);
1821 return 0;
1822 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001823 struct extent_buffer *parent;
1824 if (path->nodes[*level] == root->node)
1825 parent = path->nodes[*level];
1826 else
1827 parent = path->nodes[*level + 1];
1828
1829 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001830 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04001831 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001832 if (ret)
1833 return ret;
1834
Chris Masone02119d2008-09-05 16:13:11 -04001835 if (wc->free) {
1836 struct extent_buffer *next;
1837
1838 next = path->nodes[*level];
1839
1840 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001841 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001842 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04001843 btrfs_wait_tree_block_writeback(next);
1844 btrfs_tree_unlock(next);
1845
Chris Masone02119d2008-09-05 16:13:11 -04001846 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04001847 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04001848 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04001849 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04001850 BUG_ON(ret);
1851 }
1852 free_extent_buffer(path->nodes[*level]);
1853 path->nodes[*level] = NULL;
1854 *level = i + 1;
1855 }
1856 }
1857 return 1;
1858}
1859
1860/*
1861 * drop the reference count on the tree rooted at 'snap'. This traverses
1862 * the tree freeing any blocks that have a ref count of zero after being
1863 * decremented.
1864 */
1865static int walk_log_tree(struct btrfs_trans_handle *trans,
1866 struct btrfs_root *log, struct walk_control *wc)
1867{
1868 int ret = 0;
1869 int wret;
1870 int level;
1871 struct btrfs_path *path;
1872 int i;
1873 int orig_level;
1874
1875 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001876 if (!path)
1877 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001878
1879 level = btrfs_header_level(log->node);
1880 orig_level = level;
1881 path->nodes[level] = log->node;
1882 extent_buffer_get(log->node);
1883 path->slots[level] = 0;
1884
Chris Masond3977122009-01-05 21:25:51 -05001885 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001886 wret = walk_down_log_tree(trans, log, path, &level, wc);
1887 if (wret > 0)
1888 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001889 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001890 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001891 goto out;
1892 }
Chris Masone02119d2008-09-05 16:13:11 -04001893
1894 wret = walk_up_log_tree(trans, log, path, &level, wc);
1895 if (wret > 0)
1896 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001897 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001898 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001899 goto out;
1900 }
Chris Masone02119d2008-09-05 16:13:11 -04001901 }
1902
1903 /* was the root node processed? if not, catch it here */
1904 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04001906 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001907 if (ret)
1908 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001909 if (wc->free) {
1910 struct extent_buffer *next;
1911
1912 next = path->nodes[orig_level];
1913
1914 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001915 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001916 clean_tree_block(trans, log, next);
Chris Masone02119d2008-09-05 16:13:11 -04001917 btrfs_wait_tree_block_writeback(next);
1918 btrfs_tree_unlock(next);
1919
Chris Masone02119d2008-09-05 16:13:11 -04001920 WARN_ON(log->root_key.objectid !=
1921 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04001922 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04001923 next->len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04001925 }
1926 }
1927
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928out:
Chris Masone02119d2008-09-05 16:13:11 -04001929 for (i = 0; i <= orig_level; i++) {
1930 if (path->nodes[i]) {
1931 free_extent_buffer(path->nodes[i]);
1932 path->nodes[i] = NULL;
1933 }
1934 }
1935 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001936 return ret;
1937}
1938
Yan Zheng7237f182009-01-21 12:54:03 -05001939/*
1940 * helper function to update the item for a given subvolumes log root
1941 * in the tree of log roots
1942 */
1943static int update_log_root(struct btrfs_trans_handle *trans,
1944 struct btrfs_root *log)
1945{
1946 int ret;
1947
1948 if (log->log_transid == 1) {
1949 /* insert root item on the first sync */
1950 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1951 &log->root_key, &log->root_item);
1952 } else {
1953 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1954 &log->root_key, &log->root_item);
1955 }
1956 return ret;
1957}
1958
Chris Mason12fcfd22009-03-24 10:24:20 -04001959static int wait_log_commit(struct btrfs_trans_handle *trans,
1960 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04001961{
1962 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05001963 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04001964
Yan Zheng7237f182009-01-21 12:54:03 -05001965 /*
1966 * we only allow two pending log transactions at a time,
1967 * so we know that if ours is more than 2 older than the
1968 * current transaction, we're done
1969 */
Chris Masone02119d2008-09-05 16:13:11 -04001970 do {
Yan Zheng7237f182009-01-21 12:54:03 -05001971 prepare_to_wait(&root->log_commit_wait[index],
1972 &wait, TASK_UNINTERRUPTIBLE);
1973 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001974
1975 if (root->fs_info->last_trans_log_full_commit !=
1976 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001977 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04001978 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04001979
Yan Zheng7237f182009-01-21 12:54:03 -05001980 finish_wait(&root->log_commit_wait[index], &wait);
1981 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05001982 } while (root->fs_info->last_trans_log_full_commit !=
1983 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001984 atomic_read(&root->log_commit[index]));
1985 return 0;
1986}
1987
Jeff Mahoney143bede2012-03-01 14:56:26 +01001988static void wait_for_writer(struct btrfs_trans_handle *trans,
1989 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05001990{
1991 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05001992 while (root->fs_info->last_trans_log_full_commit !=
1993 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05001994 prepare_to_wait(&root->log_writer_wait,
1995 &wait, TASK_UNINTERRUPTIBLE);
1996 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001997 if (root->fs_info->last_trans_log_full_commit !=
1998 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05001999 schedule();
2000 mutex_lock(&root->log_mutex);
2001 finish_wait(&root->log_writer_wait, &wait);
2002 }
Chris Masone02119d2008-09-05 16:13:11 -04002003}
2004
2005/*
2006 * btrfs_sync_log does sends a given tree log down to the disk and
2007 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002008 * you know that any inodes previously logged are safely on disk only
2009 * if it returns 0.
2010 *
2011 * Any other return value means you need to call btrfs_commit_transaction.
2012 * Some of the edge cases for fsyncing directories that have had unlinks
2013 * or renames done in the past mean that sometimes the only safe
2014 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2015 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002016 */
2017int btrfs_sync_log(struct btrfs_trans_handle *trans,
2018 struct btrfs_root *root)
2019{
Yan Zheng7237f182009-01-21 12:54:03 -05002020 int index1;
2021 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002022 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002023 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002024 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002025 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002026 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002027
Yan Zheng7237f182009-01-21 12:54:03 -05002028 mutex_lock(&root->log_mutex);
2029 index1 = root->log_transid % 2;
2030 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002031 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002032 mutex_unlock(&root->log_mutex);
2033 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002034 }
Yan Zheng7237f182009-01-21 12:54:03 -05002035 atomic_set(&root->log_commit[index1], 1);
2036
2037 /* wait for previous tree log sync to complete */
2038 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002039 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002040 while (1) {
Yan Zheng7237f182009-01-21 12:54:03 -05002041 unsigned long batch = root->log_batch;
Chris Masoncd354ad2011-10-20 15:45:37 -04002042 /* when we're on an ssd, just kick the log commit out */
2043 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002044 mutex_unlock(&root->log_mutex);
2045 schedule_timeout_uninterruptible(1);
2046 mutex_lock(&root->log_mutex);
2047 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002048 wait_for_writer(trans, root);
Yan Zheng7237f182009-01-21 12:54:03 -05002049 if (batch == root->log_batch)
Chris Masone02119d2008-09-05 16:13:11 -04002050 break;
2051 }
Chris Masond0c803c2008-09-11 16:17:57 -04002052
Chris Mason12fcfd22009-03-24 10:24:20 -04002053 /* bail out if we need to do a full commit */
2054 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2055 ret = -EAGAIN;
2056 mutex_unlock(&root->log_mutex);
2057 goto out;
2058 }
2059
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002060 log_transid = root->log_transid;
2061 if (log_transid % 2 == 0)
2062 mark = EXTENT_DIRTY;
2063 else
2064 mark = EXTENT_NEW;
2065
Chris Mason690587d2009-10-13 13:29:19 -04002066 /* we start IO on all the marked extents here, but we don't actually
2067 * wait for them until later.
2068 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002069 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002070 if (ret) {
2071 btrfs_abort_transaction(trans, root, ret);
2072 mutex_unlock(&root->log_mutex);
2073 goto out;
2074 }
Yan Zheng7237f182009-01-21 12:54:03 -05002075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002077
2078 root->log_batch = 0;
2079 root->log_transid++;
2080 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002081 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002082 smp_mb();
2083 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002084 * IO has been started, blocks of the log tree have WRITTEN flag set
2085 * in their headers. new modifications of the log will be written to
2086 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002087 */
2088 mutex_unlock(&root->log_mutex);
2089
2090 mutex_lock(&log_root_tree->log_mutex);
2091 log_root_tree->log_batch++;
2092 atomic_inc(&log_root_tree->log_writers);
2093 mutex_unlock(&log_root_tree->log_mutex);
2094
2095 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002096
2097 mutex_lock(&log_root_tree->log_mutex);
2098 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2099 smp_mb();
2100 if (waitqueue_active(&log_root_tree->log_writer_wait))
2101 wake_up(&log_root_tree->log_writer_wait);
2102 }
2103
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002104 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002105 if (ret != -ENOSPC) {
2106 btrfs_abort_transaction(trans, root, ret);
2107 mutex_unlock(&log_root_tree->log_mutex);
2108 goto out;
2109 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002110 root->fs_info->last_trans_log_full_commit = trans->transid;
2111 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2112 mutex_unlock(&log_root_tree->log_mutex);
2113 ret = -EAGAIN;
2114 goto out;
2115 }
2116
Yan Zheng7237f182009-01-21 12:54:03 -05002117 index2 = log_root_tree->log_transid % 2;
2118 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002119 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002120 wait_log_commit(trans, log_root_tree,
2121 log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002122 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002123 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002124 goto out;
2125 }
2126 atomic_set(&log_root_tree->log_commit[index2], 1);
2127
Chris Mason12fcfd22009-03-24 10:24:20 -04002128 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2129 wait_log_commit(trans, log_root_tree,
2130 log_root_tree->log_transid - 1);
2131 }
Yan Zheng7237f182009-01-21 12:54:03 -05002132
Chris Mason12fcfd22009-03-24 10:24:20 -04002133 wait_for_writer(trans, log_root_tree);
2134
2135 /*
2136 * now that we've moved on to the tree of log tree roots,
2137 * check the full commit flag again
2138 */
2139 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002140 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002141 mutex_unlock(&log_root_tree->log_mutex);
2142 ret = -EAGAIN;
2143 goto out_wake_log_root;
2144 }
Yan Zheng7237f182009-01-21 12:54:03 -05002145
2146 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002147 &log_root_tree->dirty_log_pages,
2148 EXTENT_DIRTY | EXTENT_NEW);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002149 if (ret) {
2150 btrfs_abort_transaction(trans, root, ret);
2151 mutex_unlock(&log_root_tree->log_mutex);
2152 goto out_wake_log_root;
2153 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002154 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002155
David Sterba6c417612011-04-13 15:41:04 +02002156 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002157 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002158 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002159 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002160
Yan Zheng7237f182009-01-21 12:54:03 -05002161 log_root_tree->log_batch = 0;
2162 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002163 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002164
2165 mutex_unlock(&log_root_tree->log_mutex);
2166
2167 /*
2168 * nobody else is going to jump in and write the the ctree
2169 * super here because the log_commit atomic below is protecting
2170 * us. We must be called with a transaction handle pinning
2171 * the running transaction open, so a full commit can't hop
2172 * in and cause problems either.
2173 */
Arne Jansena2de7332011-03-08 14:14:00 +01002174 btrfs_scrub_pause_super(root);
Chris Mason47226072009-10-13 12:55:09 -04002175 write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002176 btrfs_scrub_continue_super(root);
Chris Mason12fcfd22009-03-24 10:24:20 -04002177 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002178
Chris Mason257c62e2009-10-13 13:21:08 -04002179 mutex_lock(&root->log_mutex);
2180 if (root->last_log_commit < log_transid)
2181 root->last_log_commit = log_transid;
2182 mutex_unlock(&root->log_mutex);
2183
Chris Mason12fcfd22009-03-24 10:24:20 -04002184out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002185 atomic_set(&log_root_tree->log_commit[index2], 0);
2186 smp_mb();
2187 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2188 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002189out:
Yan Zheng7237f182009-01-21 12:54:03 -05002190 atomic_set(&root->log_commit[index1], 0);
2191 smp_mb();
2192 if (waitqueue_active(&root->log_commit_wait[index1]))
2193 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002194 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002195}
2196
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002197static void free_log_tree(struct btrfs_trans_handle *trans,
2198 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002199{
2200 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002201 u64 start;
2202 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002203 struct walk_control wc = {
2204 .free = 1,
2205 .process_func = process_one_buffer
2206 };
2207
Chris Masone02119d2008-09-05 16:13:11 -04002208 ret = walk_log_tree(trans, log, &wc);
2209 BUG_ON(ret);
2210
Chris Masond3977122009-01-05 21:25:51 -05002211 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002212 ret = find_first_extent_bit(&log->dirty_log_pages,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002213 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04002214 if (ret)
2215 break;
2216
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002217 clear_extent_bits(&log->dirty_log_pages, start, end,
2218 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002219 }
2220
Yan Zheng7237f182009-01-21 12:54:03 -05002221 free_extent_buffer(log->node);
2222 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002223}
2224
2225/*
2226 * free all the extents used by the tree log. This should be called
2227 * at commit time of the full transaction
2228 */
2229int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2230{
2231 if (root->log_root) {
2232 free_log_tree(trans, root->log_root);
2233 root->log_root = NULL;
2234 }
2235 return 0;
2236}
2237
2238int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2239 struct btrfs_fs_info *fs_info)
2240{
2241 if (fs_info->log_root_tree) {
2242 free_log_tree(trans, fs_info->log_root_tree);
2243 fs_info->log_root_tree = NULL;
2244 }
Chris Masone02119d2008-09-05 16:13:11 -04002245 return 0;
2246}
2247
2248/*
Chris Masone02119d2008-09-05 16:13:11 -04002249 * If both a file and directory are logged, and unlinks or renames are
2250 * mixed in, we have a few interesting corners:
2251 *
2252 * create file X in dir Y
2253 * link file X to X.link in dir Y
2254 * fsync file X
2255 * unlink file X but leave X.link
2256 * fsync dir Y
2257 *
2258 * After a crash we would expect only X.link to exist. But file X
2259 * didn't get fsync'd again so the log has back refs for X and X.link.
2260 *
2261 * We solve this by removing directory entries and inode backrefs from the
2262 * log when a file that was logged in the current transaction is
2263 * unlinked. Any later fsync will include the updated log entries, and
2264 * we'll be able to reconstruct the proper directory items from backrefs.
2265 *
2266 * This optimizations allows us to avoid relogging the entire inode
2267 * or the entire directory.
2268 */
2269int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2270 struct btrfs_root *root,
2271 const char *name, int name_len,
2272 struct inode *dir, u64 index)
2273{
2274 struct btrfs_root *log;
2275 struct btrfs_dir_item *di;
2276 struct btrfs_path *path;
2277 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002278 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002279 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002280 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002281
Chris Mason3a5f1d42008-09-11 15:53:37 -04002282 if (BTRFS_I(dir)->logged_trans < trans->transid)
2283 return 0;
2284
Chris Masone02119d2008-09-05 16:13:11 -04002285 ret = join_running_log_trans(root);
2286 if (ret)
2287 return 0;
2288
2289 mutex_lock(&BTRFS_I(dir)->log_mutex);
2290
2291 log = root->log_root;
2292 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002293 if (!path) {
2294 err = -ENOMEM;
2295 goto out_unlock;
2296 }
liubo2a29edc2011-01-26 06:22:08 +00002297
Li Zefan33345d012011-04-20 10:31:50 +08002298 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002299 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002300 if (IS_ERR(di)) {
2301 err = PTR_ERR(di);
2302 goto fail;
2303 }
2304 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002305 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2306 bytes_del += name_len;
2307 BUG_ON(ret);
2308 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002309 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002310 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002311 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002312 if (IS_ERR(di)) {
2313 err = PTR_ERR(di);
2314 goto fail;
2315 }
2316 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002317 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2318 bytes_del += name_len;
2319 BUG_ON(ret);
2320 }
2321
2322 /* update the directory size in the log to reflect the names
2323 * we have removed
2324 */
2325 if (bytes_del) {
2326 struct btrfs_key key;
2327
Li Zefan33345d012011-04-20 10:31:50 +08002328 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002329 key.offset = 0;
2330 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002331 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002332
2333 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002334 if (ret < 0) {
2335 err = ret;
2336 goto fail;
2337 }
Chris Masone02119d2008-09-05 16:13:11 -04002338 if (ret == 0) {
2339 struct btrfs_inode_item *item;
2340 u64 i_size;
2341
2342 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2343 struct btrfs_inode_item);
2344 i_size = btrfs_inode_size(path->nodes[0], item);
2345 if (i_size > bytes_del)
2346 i_size -= bytes_del;
2347 else
2348 i_size = 0;
2349 btrfs_set_inode_size(path->nodes[0], item, i_size);
2350 btrfs_mark_buffer_dirty(path->nodes[0]);
2351 } else
2352 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002353 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002354 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002355fail:
Chris Masone02119d2008-09-05 16:13:11 -04002356 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002357out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002358 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002359 if (ret == -ENOSPC) {
2360 root->fs_info->last_trans_log_full_commit = trans->transid;
2361 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002362 } else if (ret < 0)
2363 btrfs_abort_transaction(trans, root, ret);
2364
Chris Mason12fcfd22009-03-24 10:24:20 -04002365 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002366
Andi Kleen411fc6b2010-10-29 15:14:31 -04002367 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002368}
2369
2370/* see comments for btrfs_del_dir_entries_in_log */
2371int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2372 struct btrfs_root *root,
2373 const char *name, int name_len,
2374 struct inode *inode, u64 dirid)
2375{
2376 struct btrfs_root *log;
2377 u64 index;
2378 int ret;
2379
Chris Mason3a5f1d42008-09-11 15:53:37 -04002380 if (BTRFS_I(inode)->logged_trans < trans->transid)
2381 return 0;
2382
Chris Masone02119d2008-09-05 16:13:11 -04002383 ret = join_running_log_trans(root);
2384 if (ret)
2385 return 0;
2386 log = root->log_root;
2387 mutex_lock(&BTRFS_I(inode)->log_mutex);
2388
Li Zefan33345d012011-04-20 10:31:50 +08002389 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002390 dirid, &index);
2391 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002392 if (ret == -ENOSPC) {
2393 root->fs_info->last_trans_log_full_commit = trans->transid;
2394 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002395 } else if (ret < 0 && ret != -ENOENT)
2396 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002397 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002398
Chris Masone02119d2008-09-05 16:13:11 -04002399 return ret;
2400}
2401
2402/*
2403 * creates a range item in the log for 'dirid'. first_offset and
2404 * last_offset tell us which parts of the key space the log should
2405 * be considered authoritative for.
2406 */
2407static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2408 struct btrfs_root *log,
2409 struct btrfs_path *path,
2410 int key_type, u64 dirid,
2411 u64 first_offset, u64 last_offset)
2412{
2413 int ret;
2414 struct btrfs_key key;
2415 struct btrfs_dir_log_item *item;
2416
2417 key.objectid = dirid;
2418 key.offset = first_offset;
2419 if (key_type == BTRFS_DIR_ITEM_KEY)
2420 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2421 else
2422 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2423 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002424 if (ret)
2425 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002426
2427 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2428 struct btrfs_dir_log_item);
2429 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2430 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002431 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002432 return 0;
2433}
2434
2435/*
2436 * log all the items included in the current transaction for a given
2437 * directory. This also creates the range items in the log tree required
2438 * to replay anything deleted before the fsync
2439 */
2440static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2441 struct btrfs_root *root, struct inode *inode,
2442 struct btrfs_path *path,
2443 struct btrfs_path *dst_path, int key_type,
2444 u64 min_offset, u64 *last_offset_ret)
2445{
2446 struct btrfs_key min_key;
2447 struct btrfs_key max_key;
2448 struct btrfs_root *log = root->log_root;
2449 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002450 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002451 int ret;
2452 int i;
2453 int nritems;
2454 u64 first_offset = min_offset;
2455 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002456 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002457
2458 log = root->log_root;
Li Zefan33345d012011-04-20 10:31:50 +08002459 max_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002460 max_key.offset = (u64)-1;
2461 max_key.type = key_type;
2462
Li Zefan33345d012011-04-20 10:31:50 +08002463 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002464 min_key.type = key_type;
2465 min_key.offset = min_offset;
2466
2467 path->keep_locks = 1;
2468
2469 ret = btrfs_search_forward(root, &min_key, &max_key,
2470 path, 0, trans->transid);
2471
2472 /*
2473 * we didn't find anything from this transaction, see if there
2474 * is anything at all
2475 */
Li Zefan33345d012011-04-20 10:31:50 +08002476 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2477 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002478 min_key.type = key_type;
2479 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002480 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002481 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2482 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002483 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002484 return ret;
2485 }
Li Zefan33345d012011-04-20 10:31:50 +08002486 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002487
2488 /* if ret == 0 there are items for this type,
2489 * create a range to tell us the last key of this type.
2490 * otherwise, there are no items in this directory after
2491 * *min_offset, and we create a range to indicate that.
2492 */
2493 if (ret == 0) {
2494 struct btrfs_key tmp;
2495 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2496 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002497 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002498 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002499 }
2500 goto done;
2501 }
2502
2503 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002504 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002505 if (ret == 0) {
2506 struct btrfs_key tmp;
2507 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2508 if (key_type == tmp.type) {
2509 first_offset = tmp.offset;
2510 ret = overwrite_item(trans, log, dst_path,
2511 path->nodes[0], path->slots[0],
2512 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002513 if (ret) {
2514 err = ret;
2515 goto done;
2516 }
Chris Masone02119d2008-09-05 16:13:11 -04002517 }
2518 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002519 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002520
2521 /* find the first key from this transaction again */
2522 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2523 if (ret != 0) {
2524 WARN_ON(1);
2525 goto done;
2526 }
2527
2528 /*
2529 * we have a block from this transaction, log every item in it
2530 * from our directory
2531 */
Chris Masond3977122009-01-05 21:25:51 -05002532 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002533 struct btrfs_key tmp;
2534 src = path->nodes[0];
2535 nritems = btrfs_header_nritems(src);
2536 for (i = path->slots[0]; i < nritems; i++) {
2537 btrfs_item_key_to_cpu(src, &min_key, i);
2538
Li Zefan33345d012011-04-20 10:31:50 +08002539 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002540 goto done;
2541 ret = overwrite_item(trans, log, dst_path, src, i,
2542 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002543 if (ret) {
2544 err = ret;
2545 goto done;
2546 }
Chris Masone02119d2008-09-05 16:13:11 -04002547 }
2548 path->slots[0] = nritems;
2549
2550 /*
2551 * look ahead to the next item and see if it is also
2552 * from this directory and from this transaction
2553 */
2554 ret = btrfs_next_leaf(root, path);
2555 if (ret == 1) {
2556 last_offset = (u64)-1;
2557 goto done;
2558 }
2559 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002560 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002561 last_offset = (u64)-1;
2562 goto done;
2563 }
2564 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2565 ret = overwrite_item(trans, log, dst_path,
2566 path->nodes[0], path->slots[0],
2567 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002568 if (ret)
2569 err = ret;
2570 else
2571 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002572 goto done;
2573 }
2574 }
2575done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002576 btrfs_release_path(path);
2577 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002578
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002579 if (err == 0) {
2580 *last_offset_ret = last_offset;
2581 /*
2582 * insert the log range keys to indicate where the log
2583 * is valid
2584 */
2585 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08002586 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002587 if (ret)
2588 err = ret;
2589 }
2590 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002591}
2592
2593/*
2594 * logging directories is very similar to logging inodes, We find all the items
2595 * from the current transaction and write them to the log.
2596 *
2597 * The recovery code scans the directory in the subvolume, and if it finds a
2598 * key in the range logged that is not present in the log tree, then it means
2599 * that dir entry was unlinked during the transaction.
2600 *
2601 * In order for that scan to work, we must include one key smaller than
2602 * the smallest logged by this transaction and one key larger than the largest
2603 * key logged by this transaction.
2604 */
2605static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2606 struct btrfs_root *root, struct inode *inode,
2607 struct btrfs_path *path,
2608 struct btrfs_path *dst_path)
2609{
2610 u64 min_key;
2611 u64 max_key;
2612 int ret;
2613 int key_type = BTRFS_DIR_ITEM_KEY;
2614
2615again:
2616 min_key = 0;
2617 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002618 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002619 ret = log_dir_items(trans, root, inode, path,
2620 dst_path, key_type, min_key,
2621 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002622 if (ret)
2623 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002624 if (max_key == (u64)-1)
2625 break;
2626 min_key = max_key + 1;
2627 }
2628
2629 if (key_type == BTRFS_DIR_ITEM_KEY) {
2630 key_type = BTRFS_DIR_INDEX_KEY;
2631 goto again;
2632 }
2633 return 0;
2634}
2635
2636/*
2637 * a helper function to drop items from the log before we relog an
2638 * inode. max_key_type indicates the highest item type to remove.
2639 * This cannot be run for file data extents because it does not
2640 * free the extents they point to.
2641 */
2642static int drop_objectid_items(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *log,
2644 struct btrfs_path *path,
2645 u64 objectid, int max_key_type)
2646{
2647 int ret;
2648 struct btrfs_key key;
2649 struct btrfs_key found_key;
2650
2651 key.objectid = objectid;
2652 key.type = max_key_type;
2653 key.offset = (u64)-1;
2654
Chris Masond3977122009-01-05 21:25:51 -05002655 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002656 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002657 BUG_ON(ret == 0);
2658 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002659 break;
2660
2661 if (path->slots[0] == 0)
2662 break;
2663
2664 path->slots[0]--;
2665 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2666 path->slots[0]);
2667
2668 if (found_key.objectid != objectid)
2669 break;
2670
2671 ret = btrfs_del_item(trans, log, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002672 if (ret)
2673 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02002674 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002675 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002676 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04002677 if (ret > 0)
2678 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002679 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002680}
2681
Chris Mason31ff1cd2008-09-11 16:17:57 -04002682static noinline int copy_items(struct btrfs_trans_handle *trans,
2683 struct btrfs_root *log,
2684 struct btrfs_path *dst_path,
2685 struct extent_buffer *src,
2686 int start_slot, int nr, int inode_only)
2687{
2688 unsigned long src_offset;
2689 unsigned long dst_offset;
2690 struct btrfs_file_extent_item *extent;
2691 struct btrfs_inode_item *inode_item;
2692 int ret;
2693 struct btrfs_key *ins_keys;
2694 u32 *ins_sizes;
2695 char *ins_data;
2696 int i;
Chris Masond20f7042008-12-08 16:58:54 -05002697 struct list_head ordered_sums;
2698
2699 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002700
2701 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2702 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00002703 if (!ins_data)
2704 return -ENOMEM;
2705
Chris Mason31ff1cd2008-09-11 16:17:57 -04002706 ins_sizes = (u32 *)ins_data;
2707 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2708
2709 for (i = 0; i < nr; i++) {
2710 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2711 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2712 }
2713 ret = btrfs_insert_empty_items(trans, log, dst_path,
2714 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002715 if (ret) {
2716 kfree(ins_data);
2717 return ret;
2718 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002719
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002720 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002721 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2722 dst_path->slots[0]);
2723
2724 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2725
2726 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2727 src_offset, ins_sizes[i]);
2728
2729 if (inode_only == LOG_INODE_EXISTS &&
2730 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2731 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2732 dst_path->slots[0],
2733 struct btrfs_inode_item);
2734 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2735
2736 /* set the generation to zero so the recover code
2737 * can tell the difference between an logging
2738 * just to say 'this inode exists' and a logging
2739 * to say 'update this inode with these values'
2740 */
2741 btrfs_set_inode_generation(dst_path->nodes[0],
2742 inode_item, 0);
2743 }
2744 /* take a reference on file data extents so that truncates
2745 * or deletes of this inode don't have to relog the inode
2746 * again
2747 */
2748 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2749 int found_type;
2750 extent = btrfs_item_ptr(src, start_slot + i,
2751 struct btrfs_file_extent_item);
2752
liubo8e531cd2011-05-06 10:36:09 +08002753 if (btrfs_file_extent_generation(src, extent) < trans->transid)
2754 continue;
2755
Chris Mason31ff1cd2008-09-11 16:17:57 -04002756 found_type = btrfs_file_extent_type(src, extent);
Yan Zhengd899e052008-10-30 14:25:28 -04002757 if (found_type == BTRFS_FILE_EXTENT_REG ||
2758 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002759 u64 ds, dl, cs, cl;
2760 ds = btrfs_file_extent_disk_bytenr(src,
2761 extent);
2762 /* ds == 0 is a hole */
2763 if (ds == 0)
2764 continue;
2765
2766 dl = btrfs_file_extent_disk_num_bytes(src,
2767 extent);
2768 cs = btrfs_file_extent_offset(src, extent);
2769 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07002770 extent);
Chris Mason580afd72008-12-08 19:15:39 -05002771 if (btrfs_file_extent_compression(src,
2772 extent)) {
2773 cs = 0;
2774 cl = dl;
2775 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002776
2777 ret = btrfs_lookup_csums_range(
2778 log->fs_info->csum_root,
2779 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01002780 &ordered_sums, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002781 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002782 }
2783 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002784 }
2785
2786 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002787 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002788 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05002789
2790 /*
2791 * we have to do this after the loop above to avoid changing the
2792 * log tree while trying to change the log tree.
2793 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002794 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002795 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05002796 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2797 struct btrfs_ordered_sum,
2798 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002799 if (!ret)
2800 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05002801 list_del(&sums->list);
2802 kfree(sums);
2803 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002804 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002805}
2806
Josef Bacik5dc562c2012-08-17 13:14:17 -04002807static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
2808{
2809 struct extent_map *em1, *em2;
2810
2811 em1 = list_entry(a, struct extent_map, list);
2812 em2 = list_entry(b, struct extent_map, list);
2813
2814 if (em1->start < em2->start)
2815 return -1;
2816 else if (em1->start > em2->start)
2817 return 1;
2818 return 0;
2819}
2820
2821struct log_args {
2822 struct extent_buffer *src;
2823 u64 next_offset;
2824 int start_slot;
2825 int nr;
2826};
2827
2828static int log_one_extent(struct btrfs_trans_handle *trans,
2829 struct inode *inode, struct btrfs_root *root,
2830 struct extent_map *em, struct btrfs_path *path,
2831 struct btrfs_path *dst_path, struct log_args *args)
2832{
2833 struct btrfs_root *log = root->log_root;
2834 struct btrfs_file_extent_item *fi;
2835 struct btrfs_key key;
2836 u64 start = em->start;
2837 u64 len = em->len;
2838 u64 num_bytes;
2839 int nritems;
2840 int ret;
2841
2842 if (BTRFS_I(inode)->logged_trans == trans->transid) {
2843 u64 tmp;
2844 ret = __btrfs_drop_extents(trans, log, inode, dst_path, start,
2845 start + len, &tmp, 0);
2846 if (ret)
2847 return ret;
2848 }
2849
2850 while (len) {
2851 if (args->nr)
2852 goto next_slot;
2853 key.objectid = btrfs_ino(inode);
2854 key.type = BTRFS_EXTENT_DATA_KEY;
2855 key.offset = start;
2856
2857 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2858 if (ret < 0)
2859 return ret;
2860 if (ret) {
2861 /*
2862 * This shouldn't happen, but it might so warn and
2863 * return an error.
2864 */
2865 WARN_ON(1);
2866 return -ENOENT;
2867 }
2868 args->src = path->nodes[0];
2869next_slot:
2870 fi = btrfs_item_ptr(args->src, path->slots[0],
2871 struct btrfs_file_extent_item);
2872 if (args->nr &&
2873 args->start_slot + args->nr == path->slots[0]) {
2874 args->nr++;
2875 } else if (args->nr) {
2876 ret = copy_items(trans, log, dst_path, args->src,
2877 args->start_slot, args->nr,
2878 LOG_INODE_ALL);
2879 if (ret)
2880 return ret;
2881 args->nr = 1;
2882 args->start_slot = path->slots[0];
2883 } else if (!args->nr) {
2884 args->nr = 1;
2885 args->start_slot = path->slots[0];
2886 }
2887 nritems = btrfs_header_nritems(path->nodes[0]);
2888 path->slots[0]++;
2889 num_bytes = btrfs_file_extent_num_bytes(args->src, fi);
2890 if (len < num_bytes) {
2891 /* I _think_ this is ok, envision we write to a
2892 * preallocated space that is adjacent to a previously
2893 * written preallocated space that gets merged when we
2894 * mark this preallocated space written. If we do not
2895 * have the adjacent extent in cache then when we copy
2896 * this extent it could end up being larger than our EM
2897 * thinks it is, which is a-ok, so just set len to 0.
2898 */
2899 len = 0;
2900 } else {
2901 len -= num_bytes;
2902 }
2903 start += btrfs_file_extent_num_bytes(args->src, fi);
2904 args->next_offset = start;
2905
2906 if (path->slots[0] < nritems) {
2907 if (len)
2908 goto next_slot;
2909 break;
2910 }
2911
2912 if (args->nr) {
2913 ret = copy_items(trans, log, dst_path, args->src,
2914 args->start_slot, args->nr,
2915 LOG_INODE_ALL);
2916 if (ret)
2917 return ret;
2918 args->nr = 0;
2919 btrfs_release_path(path);
2920 }
2921 }
2922
2923 return 0;
2924}
2925
2926static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
2927 struct btrfs_root *root,
2928 struct inode *inode,
2929 struct btrfs_path *path,
2930 struct btrfs_path *dst_path)
2931{
2932 struct log_args args;
2933 struct btrfs_root *log = root->log_root;
2934 struct extent_map *em, *n;
2935 struct list_head extents;
2936 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
2937 u64 test_gen;
2938 int ret = 0;
2939
2940 INIT_LIST_HEAD(&extents);
2941
2942 memset(&args, 0, sizeof(args));
2943
2944 write_lock(&tree->lock);
2945 test_gen = root->fs_info->last_trans_committed;
2946
2947 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
2948 list_del_init(&em->list);
2949 if (em->generation <= test_gen)
2950 continue;
2951 list_add_tail(&em->list, &extents);
2952 }
2953
2954 list_sort(NULL, &extents, extent_cmp);
2955
2956 while (!list_empty(&extents)) {
2957 em = list_entry(extents.next, struct extent_map, list);
2958
2959 list_del_init(&em->list);
2960
2961 /*
2962 * If we had an error we just need to delete everybody from our
2963 * private list.
2964 */
2965 if (ret)
2966 continue;
2967
2968 /*
2969 * If the previous EM and the last extent we left off on aren't
2970 * sequential then we need to copy the items we have and redo
2971 * our search
2972 */
2973 if (args.nr && em->start != args.next_offset) {
2974 ret = copy_items(trans, log, dst_path, args.src,
2975 args.start_slot, args.nr,
2976 LOG_INODE_ALL);
2977 if (ret)
2978 continue;
2979 btrfs_release_path(path);
2980 args.nr = 0;
2981 }
2982
2983 ret = log_one_extent(trans, inode, root, em, path, dst_path, &args);
2984 }
2985
2986 if (!ret && args.nr)
2987 ret = copy_items(trans, log, dst_path, args.src,
2988 args.start_slot, args.nr, LOG_INODE_ALL);
2989 btrfs_release_path(path);
2990 WARN_ON(!list_empty(&extents));
2991 write_unlock(&tree->lock);
2992 return ret;
2993}
2994
Chris Masone02119d2008-09-05 16:13:11 -04002995/* log a single inode in the tree log.
2996 * At least one parent directory for this inode must exist in the tree
2997 * or be logged already.
2998 *
2999 * Any items from this inode changed by the current transaction are copied
3000 * to the log tree. An extra reference is taken on any extents in this
3001 * file, allowing us to avoid a whole pile of corner cases around logging
3002 * blocks that have been removed from the tree.
3003 *
3004 * See LOG_INODE_ALL and related defines for a description of what inode_only
3005 * does.
3006 *
3007 * This handles both files and directories.
3008 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003009static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003010 struct btrfs_root *root, struct inode *inode,
3011 int inode_only)
3012{
3013 struct btrfs_path *path;
3014 struct btrfs_path *dst_path;
3015 struct btrfs_key min_key;
3016 struct btrfs_key max_key;
3017 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003018 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003019 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003020 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003021 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003022 int ins_start_slot = 0;
3023 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003024 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003025 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003026
3027 log = root->log_root;
3028
3029 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003030 if (!path)
3031 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003032 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003033 if (!dst_path) {
3034 btrfs_free_path(path);
3035 return -ENOMEM;
3036 }
Chris Masone02119d2008-09-05 16:13:11 -04003037
Li Zefan33345d012011-04-20 10:31:50 +08003038 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003039 min_key.type = BTRFS_INODE_ITEM_KEY;
3040 min_key.offset = 0;
3041
Li Zefan33345d012011-04-20 10:31:50 +08003042 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003043
Chris Mason12fcfd22009-03-24 10:24:20 -04003044
Josef Bacik5dc562c2012-08-17 13:14:17 -04003045 /* today the code can only do partial logging of directories */
Chris Masone02119d2008-09-05 16:13:11 -04003046 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
3047 max_key.type = BTRFS_XATTR_ITEM_KEY;
3048 else
3049 max_key.type = (u8)-1;
3050 max_key.offset = (u64)-1;
3051
Miao Xie16cdcec2011-04-22 18:12:22 +08003052 ret = btrfs_commit_inode_delayed_items(trans, inode);
3053 if (ret) {
3054 btrfs_free_path(path);
3055 btrfs_free_path(dst_path);
3056 return ret;
3057 }
3058
Chris Masone02119d2008-09-05 16:13:11 -04003059 mutex_lock(&BTRFS_I(inode)->log_mutex);
3060
3061 /*
3062 * a brute force approach to making sure we get the most uptodate
3063 * copies of everything.
3064 */
3065 if (S_ISDIR(inode->i_mode)) {
3066 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3067
3068 if (inode_only == LOG_INODE_EXISTS)
3069 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003070 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003071 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003072 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3073 &BTRFS_I(inode)->runtime_flags)) {
3074 ret = btrfs_truncate_inode_items(trans, log,
3075 inode, 0, 0);
3076 } else {
3077 fast_search = true;
3078 max_key.type = BTRFS_XATTR_ITEM_KEY;
3079 ret = drop_objectid_items(trans, log, path, ino,
3080 BTRFS_XATTR_ITEM_KEY);
3081 }
Chris Masone02119d2008-09-05 16:13:11 -04003082 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003083 if (ret) {
3084 err = ret;
3085 goto out_unlock;
3086 }
Chris Masone02119d2008-09-05 16:13:11 -04003087 path->keep_locks = 1;
3088
Chris Masond3977122009-01-05 21:25:51 -05003089 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003090 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003091 ret = btrfs_search_forward(root, &min_key, &max_key,
3092 path, 0, trans->transid);
3093 if (ret != 0)
3094 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003095again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003096 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003097 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003098 break;
3099 if (min_key.type > max_key.type)
3100 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003101
Chris Masone02119d2008-09-05 16:13:11 -04003102 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003103 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3104 ins_nr++;
3105 goto next_slot;
3106 } else if (!ins_nr) {
3107 ins_start_slot = path->slots[0];
3108 ins_nr = 1;
3109 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003110 }
3111
Chris Mason31ff1cd2008-09-11 16:17:57 -04003112 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
3113 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003114 if (ret) {
3115 err = ret;
3116 goto out_unlock;
3117 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003118 ins_nr = 1;
3119 ins_start_slot = path->slots[0];
3120next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003121
Chris Mason3a5f1d42008-09-11 15:53:37 -04003122 nritems = btrfs_header_nritems(path->nodes[0]);
3123 path->slots[0]++;
3124 if (path->slots[0] < nritems) {
3125 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3126 path->slots[0]);
3127 goto again;
3128 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003129 if (ins_nr) {
3130 ret = copy_items(trans, log, dst_path, src,
3131 ins_start_slot,
3132 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003133 if (ret) {
3134 err = ret;
3135 goto out_unlock;
3136 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003137 ins_nr = 0;
3138 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003139 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003140
Chris Masone02119d2008-09-05 16:13:11 -04003141 if (min_key.offset < (u64)-1)
3142 min_key.offset++;
3143 else if (min_key.type < (u8)-1)
3144 min_key.type++;
3145 else if (min_key.objectid < (u64)-1)
3146 min_key.objectid++;
3147 else
3148 break;
3149 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003150 if (ins_nr) {
3151 ret = copy_items(trans, log, dst_path, src,
3152 ins_start_slot,
3153 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003154 if (ret) {
3155 err = ret;
3156 goto out_unlock;
3157 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003158 ins_nr = 0;
3159 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003160
3161 if (fast_search) {
3162 btrfs_release_path(path);
3163 btrfs_release_path(dst_path);
3164 ret = btrfs_log_changed_extents(trans, root, inode, path,
3165 dst_path);
3166 if (ret) {
3167 err = ret;
3168 goto out_unlock;
3169 }
3170 }
3171
Chris Mason9623f9a2008-09-11 17:42:42 -04003172 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003173 btrfs_release_path(path);
3174 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003175 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003176 if (ret) {
3177 err = ret;
3178 goto out_unlock;
3179 }
Chris Masone02119d2008-09-05 16:13:11 -04003180 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003181 BTRFS_I(inode)->logged_trans = trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003182out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003183 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3184
3185 btrfs_free_path(path);
3186 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003187 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003188}
3189
Chris Mason12fcfd22009-03-24 10:24:20 -04003190/*
3191 * follow the dentry parent pointers up the chain and see if any
3192 * of the directories in it require a full commit before they can
3193 * be logged. Returns zero if nothing special needs to be done or 1 if
3194 * a full commit is required.
3195 */
3196static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3197 struct inode *inode,
3198 struct dentry *parent,
3199 struct super_block *sb,
3200 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003201{
Chris Mason12fcfd22009-03-24 10:24:20 -04003202 int ret = 0;
3203 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003204 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003205
Chris Masonaf4176b2009-03-24 10:24:31 -04003206 /*
3207 * for regular files, if its inode is already on disk, we don't
3208 * have to worry about the parents at all. This is because
3209 * we can use the last_unlink_trans field to record renames
3210 * and other fun in this file.
3211 */
3212 if (S_ISREG(inode->i_mode) &&
3213 BTRFS_I(inode)->generation <= last_committed &&
3214 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3215 goto out;
3216
Chris Mason12fcfd22009-03-24 10:24:20 -04003217 if (!S_ISDIR(inode->i_mode)) {
3218 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3219 goto out;
3220 inode = parent->d_inode;
3221 }
3222
3223 while (1) {
3224 BTRFS_I(inode)->logged_trans = trans->transid;
3225 smp_mb();
3226
3227 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3228 root = BTRFS_I(inode)->root;
3229
3230 /*
3231 * make sure any commits to the log are forced
3232 * to be full commits
3233 */
3234 root->fs_info->last_trans_log_full_commit =
3235 trans->transid;
3236 ret = 1;
3237 break;
3238 }
3239
3240 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3241 break;
3242
Yan, Zheng76dda932009-09-21 16:00:26 -04003243 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003244 break;
3245
Josef Bacik6a912212010-11-20 09:48:00 +00003246 parent = dget_parent(parent);
3247 dput(old_parent);
3248 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003249 inode = parent->d_inode;
3250
3251 }
Josef Bacik6a912212010-11-20 09:48:00 +00003252 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003253out:
Chris Masone02119d2008-09-05 16:13:11 -04003254 return ret;
3255}
3256
3257/*
3258 * helper function around btrfs_log_inode to make sure newly created
3259 * parent directories also end up in the log. A minimal inode and backref
3260 * only logging is done of any parent directories that are older than
3261 * the last committed transaction
3262 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003263int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3264 struct btrfs_root *root, struct inode *inode,
3265 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003266{
Chris Mason12fcfd22009-03-24 10:24:20 -04003267 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003268 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003269 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003270 int ret = 0;
3271 u64 last_committed = root->fs_info->last_trans_committed;
3272
3273 sb = inode->i_sb;
3274
Sage Weil3a5e1402009-04-02 16:49:40 -04003275 if (btrfs_test_opt(root, NOTREELOG)) {
3276 ret = 1;
3277 goto end_no_trans;
3278 }
3279
Chris Mason12fcfd22009-03-24 10:24:20 -04003280 if (root->fs_info->last_trans_log_full_commit >
3281 root->fs_info->last_trans_committed) {
3282 ret = 1;
3283 goto end_no_trans;
3284 }
3285
Yan, Zheng76dda932009-09-21 16:00:26 -04003286 if (root != BTRFS_I(inode)->root ||
3287 btrfs_root_refs(&root->root_item) == 0) {
3288 ret = 1;
3289 goto end_no_trans;
3290 }
3291
Chris Mason12fcfd22009-03-24 10:24:20 -04003292 ret = check_parent_dirs_for_sync(trans, inode, parent,
3293 sb, last_committed);
3294 if (ret)
3295 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003296
Josef Bacik22ee6982012-05-29 16:57:49 -04003297 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003298 ret = BTRFS_NO_LOG_SYNC;
3299 goto end_no_trans;
3300 }
3301
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003302 ret = start_log_trans(trans, root);
3303 if (ret)
3304 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003305
3306 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003307 if (ret)
3308 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003309
Chris Masonaf4176b2009-03-24 10:24:31 -04003310 /*
3311 * for regular files, if its inode is already on disk, we don't
3312 * have to worry about the parents at all. This is because
3313 * we can use the last_unlink_trans field to record renames
3314 * and other fun in this file.
3315 */
3316 if (S_ISREG(inode->i_mode) &&
3317 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003318 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3319 ret = 0;
3320 goto end_trans;
3321 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003322
3323 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003324 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003325 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003326 break;
3327
Chris Mason12fcfd22009-03-24 10:24:20 -04003328 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003329 if (root != BTRFS_I(inode)->root)
3330 break;
3331
Chris Mason12fcfd22009-03-24 10:24:20 -04003332 if (BTRFS_I(inode)->generation >
3333 root->fs_info->last_trans_committed) {
3334 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003335 if (ret)
3336 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003337 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003338 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003339 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003340
Josef Bacik6a912212010-11-20 09:48:00 +00003341 parent = dget_parent(parent);
3342 dput(old_parent);
3343 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003344 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003345 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003346end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003347 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003348 if (ret < 0) {
3349 BUG_ON(ret != -ENOSPC);
3350 root->fs_info->last_trans_log_full_commit = trans->transid;
3351 ret = 1;
3352 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003353 btrfs_end_log_trans(root);
3354end_no_trans:
3355 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003356}
3357
3358/*
3359 * it is not safe to log dentry if the chunk root has added new
3360 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3361 * If this returns 1, you must commit the transaction to safely get your
3362 * data on disk.
3363 */
3364int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3365 struct btrfs_root *root, struct dentry *dentry)
3366{
Josef Bacik6a912212010-11-20 09:48:00 +00003367 struct dentry *parent = dget_parent(dentry);
3368 int ret;
3369
3370 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3371 dput(parent);
3372
3373 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003374}
3375
3376/*
3377 * should be called during mount to recover any replay any log trees
3378 * from the FS
3379 */
3380int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3381{
3382 int ret;
3383 struct btrfs_path *path;
3384 struct btrfs_trans_handle *trans;
3385 struct btrfs_key key;
3386 struct btrfs_key found_key;
3387 struct btrfs_key tmp_key;
3388 struct btrfs_root *log;
3389 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3390 struct walk_control wc = {
3391 .process_func = process_one_buffer,
3392 .stage = 0,
3393 };
3394
Chris Masone02119d2008-09-05 16:13:11 -04003395 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003396 if (!path)
3397 return -ENOMEM;
3398
3399 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003400
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003401 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003402 if (IS_ERR(trans)) {
3403 ret = PTR_ERR(trans);
3404 goto error;
3405 }
Chris Masone02119d2008-09-05 16:13:11 -04003406
3407 wc.trans = trans;
3408 wc.pin = 1;
3409
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003410 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003411 if (ret) {
3412 btrfs_error(fs_info, ret, "Failed to pin buffers while "
3413 "recovering log root tree.");
3414 goto error;
3415 }
Chris Masone02119d2008-09-05 16:13:11 -04003416
3417again:
3418 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3419 key.offset = (u64)-1;
3420 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3421
Chris Masond3977122009-01-05 21:25:51 -05003422 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003423 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003424
3425 if (ret < 0) {
3426 btrfs_error(fs_info, ret,
3427 "Couldn't find tree log root.");
3428 goto error;
3429 }
Chris Masone02119d2008-09-05 16:13:11 -04003430 if (ret > 0) {
3431 if (path->slots[0] == 0)
3432 break;
3433 path->slots[0]--;
3434 }
3435 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3436 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003437 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003438 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3439 break;
3440
3441 log = btrfs_read_fs_root_no_radix(log_root_tree,
3442 &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003443 if (IS_ERR(log)) {
3444 ret = PTR_ERR(log);
3445 btrfs_error(fs_info, ret,
3446 "Couldn't read tree log root.");
3447 goto error;
3448 }
Chris Masone02119d2008-09-05 16:13:11 -04003449
3450 tmp_key.objectid = found_key.offset;
3451 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3452 tmp_key.offset = (u64)-1;
3453
3454 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003455 if (IS_ERR(wc.replay_dest)) {
3456 ret = PTR_ERR(wc.replay_dest);
3457 btrfs_error(fs_info, ret, "Couldn't read target root "
3458 "for tree log recovery.");
3459 goto error;
3460 }
Chris Masone02119d2008-09-05 16:13:11 -04003461
Yan Zheng07d400a2009-01-06 11:42:00 -05003462 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003463 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003464 ret = walk_log_tree(trans, log, &wc);
3465 BUG_ON(ret);
3466
3467 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3468 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3469 path);
3470 BUG_ON(ret);
3471 }
Chris Masone02119d2008-09-05 16:13:11 -04003472
3473 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003474 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003475 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003476 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003477 kfree(log);
3478
3479 if (found_key.offset == 0)
3480 break;
3481 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003482 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003483
3484 /* step one is to pin it all, step two is to replay just inodes */
3485 if (wc.pin) {
3486 wc.pin = 0;
3487 wc.process_func = replay_one_buffer;
3488 wc.stage = LOG_WALK_REPLAY_INODES;
3489 goto again;
3490 }
3491 /* step three is to replay everything */
3492 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3493 wc.stage++;
3494 goto again;
3495 }
3496
3497 btrfs_free_path(path);
3498
3499 free_extent_buffer(log_root_tree->node);
3500 log_root_tree->log_root = NULL;
3501 fs_info->log_root_recovering = 0;
3502
3503 /* step 4: commit the transaction, which also unpins the blocks */
3504 btrfs_commit_transaction(trans, fs_info->tree_root);
3505
3506 kfree(log_root_tree);
3507 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003508
3509error:
3510 btrfs_free_path(path);
3511 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003512}
Chris Mason12fcfd22009-03-24 10:24:20 -04003513
3514/*
3515 * there are some corner cases where we want to force a full
3516 * commit instead of allowing a directory to be logged.
3517 *
3518 * They revolve around files there were unlinked from the directory, and
3519 * this function updates the parent directory so that a full commit is
3520 * properly done if it is fsync'd later after the unlinks are done.
3521 */
3522void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3523 struct inode *dir, struct inode *inode,
3524 int for_rename)
3525{
3526 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003527 * when we're logging a file, if it hasn't been renamed
3528 * or unlinked, and its inode is fully committed on disk,
3529 * we don't have to worry about walking up the directory chain
3530 * to log its parents.
3531 *
3532 * So, we use the last_unlink_trans field to put this transid
3533 * into the file. When the file is logged we check it and
3534 * don't log the parents if the file is fully on disk.
3535 */
3536 if (S_ISREG(inode->i_mode))
3537 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3538
3539 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003540 * if this directory was already logged any new
3541 * names for this file/dir will get recorded
3542 */
3543 smp_mb();
3544 if (BTRFS_I(dir)->logged_trans == trans->transid)
3545 return;
3546
3547 /*
3548 * if the inode we're about to unlink was logged,
3549 * the log will be properly updated for any new names
3550 */
3551 if (BTRFS_I(inode)->logged_trans == trans->transid)
3552 return;
3553
3554 /*
3555 * when renaming files across directories, if the directory
3556 * there we're unlinking from gets fsync'd later on, there's
3557 * no way to find the destination directory later and fsync it
3558 * properly. So, we have to be conservative and force commits
3559 * so the new name gets discovered.
3560 */
3561 if (for_rename)
3562 goto record;
3563
3564 /* we can safely do the unlink without any special recording */
3565 return;
3566
3567record:
3568 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3569}
3570
3571/*
3572 * Call this after adding a new name for a file and it will properly
3573 * update the log to reflect the new name.
3574 *
3575 * It will return zero if all goes well, and it will return 1 if a
3576 * full transaction commit is required.
3577 */
3578int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3579 struct inode *inode, struct inode *old_dir,
3580 struct dentry *parent)
3581{
3582 struct btrfs_root * root = BTRFS_I(inode)->root;
3583
3584 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003585 * this will force the logging code to walk the dentry chain
3586 * up for the file
3587 */
3588 if (S_ISREG(inode->i_mode))
3589 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3590
3591 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003592 * if this inode hasn't been logged and directory we're renaming it
3593 * from hasn't been logged, we don't need to log it
3594 */
3595 if (BTRFS_I(inode)->logged_trans <=
3596 root->fs_info->last_trans_committed &&
3597 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3598 root->fs_info->last_trans_committed))
3599 return 0;
3600
3601 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3602}
3603