blob: 15dae589e59fec27e3056302198074dfde3e0802 [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
Miao Xie2ecb7922012-09-06 04:04:27 -0600150 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500151 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);
Miao Xie2ecb7922012-09-06 04:04:27 -0600169 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500170 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;
Chris Masone02119d2008-09-05 16:13:11 -0400488 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500489 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400490 struct btrfs_file_extent_item *item;
491 struct inode *inode = NULL;
492 unsigned long size;
493 int ret = 0;
494
495 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
496 found_type = btrfs_file_extent_type(eb, item);
497
Yan Zhengd899e052008-10-30 14:25:28 -0400498 if (found_type == BTRFS_FILE_EXTENT_REG ||
499 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400500 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
501 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400503 extent_end = (start + size + mask) & ~mask;
504 } else {
505 ret = 0;
506 goto out;
507 }
508
509 inode = read_one_inode(root, key->objectid);
510 if (!inode) {
511 ret = -EIO;
512 goto out;
513 }
514
515 /*
516 * first check to see if we already have this extent in the
517 * file. This must be done before the btrfs_drop_extents run
518 * so we don't try to drop this extent.
519 */
Li Zefan33345d012011-04-20 10:31:50 +0800520 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400521 start, 0);
522
Yan Zhengd899e052008-10-30 14:25:28 -0400523 if (ret == 0 &&
524 (found_type == BTRFS_FILE_EXTENT_REG ||
525 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400526 struct btrfs_file_extent_item cmp1;
527 struct btrfs_file_extent_item cmp2;
528 struct btrfs_file_extent_item *existing;
529 struct extent_buffer *leaf;
530
531 leaf = path->nodes[0];
532 existing = btrfs_item_ptr(leaf, path->slots[0],
533 struct btrfs_file_extent_item);
534
535 read_extent_buffer(eb, &cmp1, (unsigned long)item,
536 sizeof(cmp1));
537 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
538 sizeof(cmp2));
539
540 /*
541 * we already have a pointer to this exact extent,
542 * we don't have to do anything
543 */
544 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200545 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400546 goto out;
547 }
548 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200549 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400550
Yan Zheng07d400a2009-01-06 11:42:00 -0500551 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400552 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400553 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400554 BUG_ON(ret);
555
Yan Zheng07d400a2009-01-06 11:42:00 -0500556 if (found_type == BTRFS_FILE_EXTENT_REG ||
557 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400558 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500559 unsigned long dest_offset;
560 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400561
Yan Zheng07d400a2009-01-06 11:42:00 -0500562 ret = btrfs_insert_empty_item(trans, root, path, key,
563 sizeof(*item));
564 BUG_ON(ret);
565 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
566 path->slots[0]);
567 copy_extent_buffer(path->nodes[0], eb, dest_offset,
568 (unsigned long)item, sizeof(*item));
569
570 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
571 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
572 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400573 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500574
575 if (ins.objectid > 0) {
576 u64 csum_start;
577 u64 csum_end;
578 LIST_HEAD(ordered_sums);
579 /*
580 * is this extent already allocated in the extent
581 * allocation tree? If so, just add a reference
582 */
583 ret = btrfs_lookup_extent(root, ins.objectid,
584 ins.offset);
585 if (ret == 0) {
586 ret = btrfs_inc_extent_ref(trans, root,
587 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400588 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200589 key->objectid, offset, 0);
Tsutomu Itoh37daa4f2011-04-28 09:18:21 +0000590 BUG_ON(ret);
Yan Zheng07d400a2009-01-06 11:42:00 -0500591 } else {
592 /*
593 * insert the extent pointer in the extent
594 * allocation tree
595 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400596 ret = btrfs_alloc_logged_file_extent(trans,
597 root, root->root_key.objectid,
598 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500599 BUG_ON(ret);
600 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200601 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500602
603 if (btrfs_file_extent_compression(eb, item)) {
604 csum_start = ins.objectid;
605 csum_end = csum_start + ins.offset;
606 } else {
607 csum_start = ins.objectid +
608 btrfs_file_extent_offset(eb, item);
609 csum_end = csum_start +
610 btrfs_file_extent_num_bytes(eb, item);
611 }
612
613 ret = btrfs_lookup_csums_range(root->log_root,
614 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100615 &ordered_sums, 0);
Yan Zheng07d400a2009-01-06 11:42:00 -0500616 BUG_ON(ret);
617 while (!list_empty(&ordered_sums)) {
618 struct btrfs_ordered_sum *sums;
619 sums = list_entry(ordered_sums.next,
620 struct btrfs_ordered_sum,
621 list);
622 ret = btrfs_csum_file_blocks(trans,
623 root->fs_info->csum_root,
624 sums);
625 BUG_ON(ret);
626 list_del(&sums->list);
627 kfree(sums);
628 }
629 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200630 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500631 }
632 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
633 /* inline extents are easy, we just overwrite them */
634 ret = overwrite_item(trans, root, path, eb, slot, key);
635 BUG_ON(ret);
636 }
637
638 inode_set_bytes(inode, saved_nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600639 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400640out:
641 if (inode)
642 iput(inode);
643 return ret;
644}
645
646/*
647 * when cleaning up conflicts between the directory names in the
648 * subvolume, directory names in the log and directory names in the
649 * inode back references, we may have to unlink inodes from directories.
650 *
651 * This is a helper function to do the unlink of a specific directory
652 * item
653 */
654static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
655 struct btrfs_root *root,
656 struct btrfs_path *path,
657 struct inode *dir,
658 struct btrfs_dir_item *di)
659{
660 struct inode *inode;
661 char *name;
662 int name_len;
663 struct extent_buffer *leaf;
664 struct btrfs_key location;
665 int ret;
666
667 leaf = path->nodes[0];
668
669 btrfs_dir_item_key_to_cpu(leaf, di, &location);
670 name_len = btrfs_dir_name_len(leaf, di);
671 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000672 if (!name)
673 return -ENOMEM;
674
Chris Masone02119d2008-09-05 16:13:11 -0400675 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200676 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400677
678 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000679 if (!inode) {
680 kfree(name);
681 return -EIO;
682 }
Chris Masone02119d2008-09-05 16:13:11 -0400683
Yan Zhengec051c02009-01-05 15:43:42 -0500684 ret = link_to_fixup_dir(trans, root, path, location.objectid);
685 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400686
Chris Masone02119d2008-09-05 16:13:11 -0400687 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500688 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400689 kfree(name);
690
691 iput(inode);
Chris Masonb6305562012-07-02 15:29:53 -0400692
693 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400694 return ret;
695}
696
697/*
698 * helper function to see if a given name and sequence number found
699 * in an inode back reference are already in a directory and correctly
700 * point to this inode
701 */
702static noinline int inode_in_dir(struct btrfs_root *root,
703 struct btrfs_path *path,
704 u64 dirid, u64 objectid, u64 index,
705 const char *name, int name_len)
706{
707 struct btrfs_dir_item *di;
708 struct btrfs_key location;
709 int match = 0;
710
711 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
712 index, name, name_len, 0);
713 if (di && !IS_ERR(di)) {
714 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
715 if (location.objectid != objectid)
716 goto out;
717 } else
718 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200719 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400720
721 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
722 if (di && !IS_ERR(di)) {
723 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
724 if (location.objectid != objectid)
725 goto out;
726 } else
727 goto out;
728 match = 1;
729out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200730 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400731 return match;
732}
733
734/*
735 * helper function to check a log tree for a named back reference in
736 * an inode. This is used to decide if a back reference that is
737 * found in the subvolume conflicts with what we find in the log.
738 *
739 * inode backreferences may have multiple refs in a single item,
740 * during replay we process one reference at a time, and we don't
741 * want to delete valid links to a file from the subvolume if that
742 * link is also in the log.
743 */
744static noinline int backref_in_log(struct btrfs_root *log,
745 struct btrfs_key *key,
746 char *name, int namelen)
747{
748 struct btrfs_path *path;
749 struct btrfs_inode_ref *ref;
750 unsigned long ptr;
751 unsigned long ptr_end;
752 unsigned long name_ptr;
753 int found_name_len;
754 int item_size;
755 int ret;
756 int match = 0;
757
758 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000759 if (!path)
760 return -ENOMEM;
761
Chris Masone02119d2008-09-05 16:13:11 -0400762 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
763 if (ret != 0)
764 goto out;
765
766 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
767 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
768 ptr_end = ptr + item_size;
769 while (ptr < ptr_end) {
770 ref = (struct btrfs_inode_ref *)ptr;
771 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
772 if (found_name_len == namelen) {
773 name_ptr = (unsigned long)(ref + 1);
774 ret = memcmp_extent_buffer(path->nodes[0], name,
775 name_ptr, namelen);
776 if (ret == 0) {
777 match = 1;
778 goto out;
779 }
780 }
781 ptr = (unsigned long)(ref + 1) + found_name_len;
782 }
783out:
784 btrfs_free_path(path);
785 return match;
786}
787
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700788static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
789 struct btrfs_root *root,
790 struct btrfs_path *path,
791 struct btrfs_root *log_root,
792 struct inode *dir, struct inode *inode,
793 struct btrfs_key *key,
794 struct extent_buffer *eb,
795 struct btrfs_inode_ref *ref,
796 char *name, int namelen, int *search_done)
797{
798 int ret;
799 struct btrfs_dir_item *di;
800
801 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
802 if (ret == 0) {
803 char *victim_name;
804 int victim_name_len;
805 struct btrfs_inode_ref *victim_ref;
806 unsigned long ptr;
807 unsigned long ptr_end;
808 struct extent_buffer *leaf = path->nodes[0];
809
810 /* are we trying to overwrite a back ref for the root directory
811 * if so, just jump out, we're done
812 */
813 if (key->objectid == key->offset)
814 return 1;
815
816 /* check all the names in this back reference to see
817 * if they are in the log. if so, we allow them to stay
818 * otherwise they must be unlinked as a conflict
819 */
820 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
821 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
822 while (ptr < ptr_end) {
823 victim_ref = (struct btrfs_inode_ref *)ptr;
824 victim_name_len = btrfs_inode_ref_name_len(leaf,
825 victim_ref);
826 victim_name = kmalloc(victim_name_len, GFP_NOFS);
827 BUG_ON(!victim_name);
828
829 read_extent_buffer(leaf, victim_name,
830 (unsigned long)(victim_ref + 1),
831 victim_name_len);
832
833 if (!backref_in_log(log_root, key, victim_name,
834 victim_name_len)) {
835 btrfs_inc_nlink(inode);
836 btrfs_release_path(path);
837
838 ret = btrfs_unlink_inode(trans, root, dir,
839 inode, victim_name,
840 victim_name_len);
841 btrfs_run_delayed_items(trans, root);
842 }
843 kfree(victim_name);
844 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
845 }
846 BUG_ON(ret);
847
848 /*
849 * NOTE: we have searched root tree and checked the
850 * coresponding ref, it does not need to check again.
851 */
852 *search_done = 1;
853 }
854 btrfs_release_path(path);
855
856 /* look for a conflicting sequence number */
857 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
858 btrfs_inode_ref_index(eb, ref),
859 name, namelen, 0);
860 if (di && !IS_ERR(di)) {
861 ret = drop_one_dir_item(trans, root, path, dir, di);
862 BUG_ON(ret);
863 }
864 btrfs_release_path(path);
865
866 /* look for a conflicing name */
867 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
868 name, namelen, 0);
869 if (di && !IS_ERR(di)) {
870 ret = drop_one_dir_item(trans, root, path, dir, di);
871 BUG_ON(ret);
872 }
873 btrfs_release_path(path);
874
875 return 0;
876}
Chris Masone02119d2008-09-05 16:13:11 -0400877
878/*
879 * replay one inode back reference item found in the log tree.
880 * eb, slot and key refer to the buffer and key found in the log tree.
881 * root is the destination we are replaying into, and path is for temp
882 * use by this function. (it should be released on return).
883 */
884static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root,
886 struct btrfs_root *log,
887 struct btrfs_path *path,
888 struct extent_buffer *eb, int slot,
889 struct btrfs_key *key)
890{
Chris Masone02119d2008-09-05 16:13:11 -0400891 struct btrfs_inode_ref *ref;
liubo34f3e4f2011-08-06 08:35:23 +0000892 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -0400893 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400894 unsigned long ref_ptr;
895 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +0000896 char *name;
897 int namelen;
898 int ret;
liuboc622ae62011-03-26 08:01:12 -0400899 int search_done = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400900
Chris Masone02119d2008-09-05 16:13:11 -0400901 /*
902 * it is possible that we didn't log all the parent directories
903 * for a given inode. If we don't find the dir, just don't
904 * copy the back ref in. The link count fixup code will take
905 * care of the rest
906 */
907 dir = read_one_inode(root, key->offset);
908 if (!dir)
909 return -ENOENT;
910
911 inode = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000912 if (!inode) {
913 iput(dir);
914 return -EIO;
915 }
Chris Masone02119d2008-09-05 16:13:11 -0400916
917 ref_ptr = btrfs_item_ptr_offset(eb, slot);
918 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
919
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700920 while (ref_ptr < ref_end) {
921 ref = (struct btrfs_inode_ref *)ref_ptr;
Chris Masone02119d2008-09-05 16:13:11 -0400922
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700923 namelen = btrfs_inode_ref_name_len(eb, ref);
924 name = kmalloc(namelen, GFP_NOFS);
925 BUG_ON(!name);
Chris Masone02119d2008-09-05 16:13:11 -0400926
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700927 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
Chris Masone02119d2008-09-05 16:13:11 -0400928
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700929 /* if we already have a perfect match, we're done */
930 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
931 btrfs_inode_ref_index(eb, ref),
932 name, namelen)) {
933 /*
934 * look for a conflicting back reference in the
935 * metadata. if we find one we have to unlink that name
936 * of the file before we add our new link. Later on, we
937 * overwrite any existing back reference, and we don't
938 * want to create dangling pointers in the directory.
939 */
Chris Masone02119d2008-09-05 16:13:11 -0400940
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700941 if (!search_done) {
942 ret = __add_inode_ref(trans, root, path, log,
943 dir, inode, key, eb, ref,
944 name, namelen,
945 &search_done);
946 if (ret == 1)
947 goto out;
948 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400949 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700950
951 /* insert our name */
952 ret = btrfs_add_link(trans, dir, inode, name, namelen,
953 0, btrfs_inode_ref_index(eb, ref));
954 BUG_ON(ret);
955
956 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400957 }
liuboc622ae62011-03-26 08:01:12 -0400958
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700959 ref_ptr = (unsigned long)(ref + 1) + namelen;
960 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -0400961 }
Chris Masone02119d2008-09-05 16:13:11 -0400962
963 /* finally write the back reference in the inode */
964 ret = overwrite_item(trans, root, path, eb, slot, key);
965 BUG_ON(ret);
966
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700967out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200968 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400969 iput(dir);
970 iput(inode);
971 return 0;
972}
973
Yan, Zhengc71bf092009-11-12 09:34:40 +0000974static int insert_orphan_item(struct btrfs_trans_handle *trans,
975 struct btrfs_root *root, u64 offset)
976{
977 int ret;
978 ret = btrfs_find_orphan_item(root, offset);
979 if (ret > 0)
980 ret = btrfs_insert_orphan_item(trans, root, offset);
981 return ret;
982}
983
984
Chris Masone02119d2008-09-05 16:13:11 -0400985/*
Chris Masone02119d2008-09-05 16:13:11 -0400986 * There are a few corners where the link count of the file can't
987 * be properly maintained during replay. So, instead of adding
988 * lots of complexity to the log code, we just scan the backrefs
989 * for any file that has been through replay.
990 *
991 * The scan will update the link count on the inode to reflect the
992 * number of back refs found. If it goes down to zero, the iput
993 * will free the inode.
994 */
995static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
996 struct btrfs_root *root,
997 struct inode *inode)
998{
999 struct btrfs_path *path;
1000 int ret;
1001 struct btrfs_key key;
1002 u64 nlink = 0;
1003 unsigned long ptr;
1004 unsigned long ptr_end;
1005 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001006 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001007
Li Zefan33345d012011-04-20 10:31:50 +08001008 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001009 key.type = BTRFS_INODE_REF_KEY;
1010 key.offset = (u64)-1;
1011
1012 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +00001013 if (!path)
1014 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001015
Chris Masond3977122009-01-05 21:25:51 -05001016 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001017 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1018 if (ret < 0)
1019 break;
1020 if (ret > 0) {
1021 if (path->slots[0] == 0)
1022 break;
1023 path->slots[0]--;
1024 }
1025 btrfs_item_key_to_cpu(path->nodes[0], &key,
1026 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001027 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001028 key.type != BTRFS_INODE_REF_KEY)
1029 break;
1030 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1031 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1032 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001033 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001034 struct btrfs_inode_ref *ref;
1035
1036 ref = (struct btrfs_inode_ref *)ptr;
1037 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1038 ref);
1039 ptr = (unsigned long)(ref + 1) + name_len;
1040 nlink++;
1041 }
1042
1043 if (key.offset == 0)
1044 break;
1045 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001046 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001047 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001048 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001049 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001050 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001051 btrfs_update_inode(trans, root, inode);
1052 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001053 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001054
Yan, Zhengc71bf092009-11-12 09:34:40 +00001055 if (inode->i_nlink == 0) {
1056 if (S_ISDIR(inode->i_mode)) {
1057 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001058 ino, 1);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001059 BUG_ON(ret);
1060 }
Li Zefan33345d012011-04-20 10:31:50 +08001061 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001062 BUG_ON(ret);
1063 }
1064 btrfs_free_path(path);
1065
Chris Masone02119d2008-09-05 16:13:11 -04001066 return 0;
1067}
1068
1069static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1070 struct btrfs_root *root,
1071 struct btrfs_path *path)
1072{
1073 int ret;
1074 struct btrfs_key key;
1075 struct inode *inode;
1076
1077 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1078 key.type = BTRFS_ORPHAN_ITEM_KEY;
1079 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001080 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001081 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1082 if (ret < 0)
1083 break;
1084
1085 if (ret == 1) {
1086 if (path->slots[0] == 0)
1087 break;
1088 path->slots[0]--;
1089 }
1090
1091 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1092 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1093 key.type != BTRFS_ORPHAN_ITEM_KEY)
1094 break;
1095
1096 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001097 if (ret)
1098 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001099
David Sterbab3b4aa72011-04-21 01:20:15 +02001100 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001101 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001102 if (!inode)
1103 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001104
1105 ret = fixup_inode_link_count(trans, root, inode);
1106 BUG_ON(ret);
1107
1108 iput(inode);
1109
Chris Mason12fcfd22009-03-24 10:24:20 -04001110 /*
1111 * fixup on a directory may create new entries,
1112 * make sure we always look for the highset possible
1113 * offset
1114 */
1115 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001116 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001117 ret = 0;
1118out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001119 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001120 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001121}
1122
1123
1124/*
1125 * record a given inode in the fixup dir so we can check its link
1126 * count when replay is done. The link count is incremented here
1127 * so the inode won't go away until we check it
1128 */
1129static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1130 struct btrfs_root *root,
1131 struct btrfs_path *path,
1132 u64 objectid)
1133{
1134 struct btrfs_key key;
1135 int ret = 0;
1136 struct inode *inode;
1137
1138 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001139 if (!inode)
1140 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001141
1142 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1143 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1144 key.offset = objectid;
1145
1146 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1147
David Sterbab3b4aa72011-04-21 01:20:15 +02001148 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001149 if (ret == 0) {
1150 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001151 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001152 } else if (ret == -EEXIST) {
1153 ret = 0;
1154 } else {
1155 BUG();
1156 }
1157 iput(inode);
1158
1159 return ret;
1160}
1161
1162/*
1163 * when replaying the log for a directory, we only insert names
1164 * for inodes that actually exist. This means an fsync on a directory
1165 * does not implicitly fsync all the new files in it
1166 */
1167static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1168 struct btrfs_root *root,
1169 struct btrfs_path *path,
1170 u64 dirid, u64 index,
1171 char *name, int name_len, u8 type,
1172 struct btrfs_key *location)
1173{
1174 struct inode *inode;
1175 struct inode *dir;
1176 int ret;
1177
1178 inode = read_one_inode(root, location->objectid);
1179 if (!inode)
1180 return -ENOENT;
1181
1182 dir = read_one_inode(root, dirid);
1183 if (!dir) {
1184 iput(inode);
1185 return -EIO;
1186 }
1187 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1188
1189 /* FIXME, put inode into FIXUP list */
1190
1191 iput(inode);
1192 iput(dir);
1193 return ret;
1194}
1195
1196/*
1197 * take a single entry in a log directory item and replay it into
1198 * the subvolume.
1199 *
1200 * if a conflicting item exists in the subdirectory already,
1201 * the inode it points to is unlinked and put into the link count
1202 * fix up tree.
1203 *
1204 * If a name from the log points to a file or directory that does
1205 * not exist in the FS, it is skipped. fsyncs on directories
1206 * do not force down inodes inside that directory, just changes to the
1207 * names or unlinks in a directory.
1208 */
1209static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1210 struct btrfs_root *root,
1211 struct btrfs_path *path,
1212 struct extent_buffer *eb,
1213 struct btrfs_dir_item *di,
1214 struct btrfs_key *key)
1215{
1216 char *name;
1217 int name_len;
1218 struct btrfs_dir_item *dst_di;
1219 struct btrfs_key found_key;
1220 struct btrfs_key log_key;
1221 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001222 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001223 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001224 int ret;
1225
1226 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001227 if (!dir)
1228 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001229
1230 name_len = btrfs_dir_name_len(eb, di);
1231 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001232 if (!name)
1233 return -ENOMEM;
1234
Chris Masone02119d2008-09-05 16:13:11 -04001235 log_type = btrfs_dir_type(eb, di);
1236 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1237 name_len);
1238
1239 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001240 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1241 if (exists == 0)
1242 exists = 1;
1243 else
1244 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001245 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001246
Chris Masone02119d2008-09-05 16:13:11 -04001247 if (key->type == BTRFS_DIR_ITEM_KEY) {
1248 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1249 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001250 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001251 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1252 key->objectid,
1253 key->offset, name,
1254 name_len, 1);
1255 } else {
1256 BUG();
1257 }
David Sterbac7040052011-04-19 18:00:01 +02001258 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001259 /* we need a sequence number to insert, so we only
1260 * do inserts for the BTRFS_DIR_INDEX_KEY types
1261 */
1262 if (key->type != BTRFS_DIR_INDEX_KEY)
1263 goto out;
1264 goto insert;
1265 }
1266
1267 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1268 /* the existing item matches the logged item */
1269 if (found_key.objectid == log_key.objectid &&
1270 found_key.type == log_key.type &&
1271 found_key.offset == log_key.offset &&
1272 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1273 goto out;
1274 }
1275
1276 /*
1277 * don't drop the conflicting directory entry if the inode
1278 * for the new entry doesn't exist
1279 */
Chris Mason4bef0842008-09-08 11:18:08 -04001280 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001281 goto out;
1282
Chris Masone02119d2008-09-05 16:13:11 -04001283 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1284 BUG_ON(ret);
1285
1286 if (key->type == BTRFS_DIR_INDEX_KEY)
1287 goto insert;
1288out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001289 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001290 kfree(name);
1291 iput(dir);
1292 return 0;
1293
1294insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001295 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001296 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1297 name, name_len, log_type, &log_key);
1298
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001299 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001300 goto out;
1301}
1302
1303/*
1304 * find all the names in a directory item and reconcile them into
1305 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1306 * one name in a directory item, but the same code gets used for
1307 * both directory index types
1308 */
1309static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1310 struct btrfs_root *root,
1311 struct btrfs_path *path,
1312 struct extent_buffer *eb, int slot,
1313 struct btrfs_key *key)
1314{
1315 int ret;
1316 u32 item_size = btrfs_item_size_nr(eb, slot);
1317 struct btrfs_dir_item *di;
1318 int name_len;
1319 unsigned long ptr;
1320 unsigned long ptr_end;
1321
1322 ptr = btrfs_item_ptr_offset(eb, slot);
1323 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001324 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001325 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001326 if (verify_dir_item(root, eb, di))
1327 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001328 name_len = btrfs_dir_name_len(eb, di);
1329 ret = replay_one_name(trans, root, path, eb, di, key);
1330 BUG_ON(ret);
1331 ptr = (unsigned long)(di + 1);
1332 ptr += name_len;
1333 }
1334 return 0;
1335}
1336
1337/*
1338 * directory replay has two parts. There are the standard directory
1339 * items in the log copied from the subvolume, and range items
1340 * created in the log while the subvolume was logged.
1341 *
1342 * The range items tell us which parts of the key space the log
1343 * is authoritative for. During replay, if a key in the subvolume
1344 * directory is in a logged range item, but not actually in the log
1345 * that means it was deleted from the directory before the fsync
1346 * and should be removed.
1347 */
1348static noinline int find_dir_range(struct btrfs_root *root,
1349 struct btrfs_path *path,
1350 u64 dirid, int key_type,
1351 u64 *start_ret, u64 *end_ret)
1352{
1353 struct btrfs_key key;
1354 u64 found_end;
1355 struct btrfs_dir_log_item *item;
1356 int ret;
1357 int nritems;
1358
1359 if (*start_ret == (u64)-1)
1360 return 1;
1361
1362 key.objectid = dirid;
1363 key.type = key_type;
1364 key.offset = *start_ret;
1365
1366 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1367 if (ret < 0)
1368 goto out;
1369 if (ret > 0) {
1370 if (path->slots[0] == 0)
1371 goto out;
1372 path->slots[0]--;
1373 }
1374 if (ret != 0)
1375 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1376
1377 if (key.type != key_type || key.objectid != dirid) {
1378 ret = 1;
1379 goto next;
1380 }
1381 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1382 struct btrfs_dir_log_item);
1383 found_end = btrfs_dir_log_end(path->nodes[0], item);
1384
1385 if (*start_ret >= key.offset && *start_ret <= found_end) {
1386 ret = 0;
1387 *start_ret = key.offset;
1388 *end_ret = found_end;
1389 goto out;
1390 }
1391 ret = 1;
1392next:
1393 /* check the next slot in the tree to see if it is a valid item */
1394 nritems = btrfs_header_nritems(path->nodes[0]);
1395 if (path->slots[0] >= nritems) {
1396 ret = btrfs_next_leaf(root, path);
1397 if (ret)
1398 goto out;
1399 } else {
1400 path->slots[0]++;
1401 }
1402
1403 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1404
1405 if (key.type != key_type || key.objectid != dirid) {
1406 ret = 1;
1407 goto out;
1408 }
1409 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1410 struct btrfs_dir_log_item);
1411 found_end = btrfs_dir_log_end(path->nodes[0], item);
1412 *start_ret = key.offset;
1413 *end_ret = found_end;
1414 ret = 0;
1415out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001416 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001417 return ret;
1418}
1419
1420/*
1421 * this looks for a given directory item in the log. If the directory
1422 * item is not in the log, the item is removed and the inode it points
1423 * to is unlinked
1424 */
1425static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1426 struct btrfs_root *root,
1427 struct btrfs_root *log,
1428 struct btrfs_path *path,
1429 struct btrfs_path *log_path,
1430 struct inode *dir,
1431 struct btrfs_key *dir_key)
1432{
1433 int ret;
1434 struct extent_buffer *eb;
1435 int slot;
1436 u32 item_size;
1437 struct btrfs_dir_item *di;
1438 struct btrfs_dir_item *log_di;
1439 int name_len;
1440 unsigned long ptr;
1441 unsigned long ptr_end;
1442 char *name;
1443 struct inode *inode;
1444 struct btrfs_key location;
1445
1446again:
1447 eb = path->nodes[0];
1448 slot = path->slots[0];
1449 item_size = btrfs_item_size_nr(eb, slot);
1450 ptr = btrfs_item_ptr_offset(eb, slot);
1451 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001452 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001453 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001454 if (verify_dir_item(root, eb, di)) {
1455 ret = -EIO;
1456 goto out;
1457 }
1458
Chris Masone02119d2008-09-05 16:13:11 -04001459 name_len = btrfs_dir_name_len(eb, di);
1460 name = kmalloc(name_len, GFP_NOFS);
1461 if (!name) {
1462 ret = -ENOMEM;
1463 goto out;
1464 }
1465 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1466 name_len);
1467 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001468 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001469 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1470 dir_key->objectid,
1471 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001472 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001473 log_di = btrfs_lookup_dir_index_item(trans, log,
1474 log_path,
1475 dir_key->objectid,
1476 dir_key->offset,
1477 name, name_len, 0);
1478 }
David Sterbac7040052011-04-19 18:00:01 +02001479 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001480 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001481 btrfs_release_path(path);
1482 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001483 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001484 if (!inode) {
1485 kfree(name);
1486 return -EIO;
1487 }
Chris Masone02119d2008-09-05 16:13:11 -04001488
1489 ret = link_to_fixup_dir(trans, root,
1490 path, location.objectid);
1491 BUG_ON(ret);
1492 btrfs_inc_nlink(inode);
1493 ret = btrfs_unlink_inode(trans, root, dir, inode,
1494 name, name_len);
1495 BUG_ON(ret);
Chris Masonb6305562012-07-02 15:29:53 -04001496
1497 btrfs_run_delayed_items(trans, root);
1498
Chris Masone02119d2008-09-05 16:13:11 -04001499 kfree(name);
1500 iput(inode);
1501
1502 /* there might still be more names under this key
1503 * check and repeat if required
1504 */
1505 ret = btrfs_search_slot(NULL, root, dir_key, path,
1506 0, 0);
1507 if (ret == 0)
1508 goto again;
1509 ret = 0;
1510 goto out;
1511 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001512 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001513 kfree(name);
1514
1515 ptr = (unsigned long)(di + 1);
1516 ptr += name_len;
1517 }
1518 ret = 0;
1519out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001520 btrfs_release_path(path);
1521 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001522 return ret;
1523}
1524
1525/*
1526 * deletion replay happens before we copy any new directory items
1527 * out of the log or out of backreferences from inodes. It
1528 * scans the log to find ranges of keys that log is authoritative for,
1529 * and then scans the directory to find items in those ranges that are
1530 * not present in the log.
1531 *
1532 * Anything we don't find in the log is unlinked and removed from the
1533 * directory.
1534 */
1535static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1536 struct btrfs_root *root,
1537 struct btrfs_root *log,
1538 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001539 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001540{
1541 u64 range_start;
1542 u64 range_end;
1543 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1544 int ret = 0;
1545 struct btrfs_key dir_key;
1546 struct btrfs_key found_key;
1547 struct btrfs_path *log_path;
1548 struct inode *dir;
1549
1550 dir_key.objectid = dirid;
1551 dir_key.type = BTRFS_DIR_ITEM_KEY;
1552 log_path = btrfs_alloc_path();
1553 if (!log_path)
1554 return -ENOMEM;
1555
1556 dir = read_one_inode(root, dirid);
1557 /* it isn't an error if the inode isn't there, that can happen
1558 * because we replay the deletes before we copy in the inode item
1559 * from the log
1560 */
1561 if (!dir) {
1562 btrfs_free_path(log_path);
1563 return 0;
1564 }
1565again:
1566 range_start = 0;
1567 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001568 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001569 if (del_all)
1570 range_end = (u64)-1;
1571 else {
1572 ret = find_dir_range(log, path, dirid, key_type,
1573 &range_start, &range_end);
1574 if (ret != 0)
1575 break;
1576 }
Chris Masone02119d2008-09-05 16:13:11 -04001577
1578 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001579 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001580 int nritems;
1581 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1582 0, 0);
1583 if (ret < 0)
1584 goto out;
1585
1586 nritems = btrfs_header_nritems(path->nodes[0]);
1587 if (path->slots[0] >= nritems) {
1588 ret = btrfs_next_leaf(root, path);
1589 if (ret)
1590 break;
1591 }
1592 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1593 path->slots[0]);
1594 if (found_key.objectid != dirid ||
1595 found_key.type != dir_key.type)
1596 goto next_type;
1597
1598 if (found_key.offset > range_end)
1599 break;
1600
1601 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001602 log_path, dir,
1603 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001604 BUG_ON(ret);
1605 if (found_key.offset == (u64)-1)
1606 break;
1607 dir_key.offset = found_key.offset + 1;
1608 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001609 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001610 if (range_end == (u64)-1)
1611 break;
1612 range_start = range_end + 1;
1613 }
1614
1615next_type:
1616 ret = 0;
1617 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1618 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1619 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001620 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001621 goto again;
1622 }
1623out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001624 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001625 btrfs_free_path(log_path);
1626 iput(dir);
1627 return ret;
1628}
1629
1630/*
1631 * the process_func used to replay items from the log tree. This
1632 * gets called in two different stages. The first stage just looks
1633 * for inodes and makes sure they are all copied into the subvolume.
1634 *
1635 * The second stage copies all the other item types from the log into
1636 * the subvolume. The two stage approach is slower, but gets rid of
1637 * lots of complexity around inodes referencing other inodes that exist
1638 * only in the log (references come from either directory items or inode
1639 * back refs).
1640 */
1641static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1642 struct walk_control *wc, u64 gen)
1643{
1644 int nritems;
1645 struct btrfs_path *path;
1646 struct btrfs_root *root = wc->replay_dest;
1647 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001648 int level;
1649 int i;
1650 int ret;
1651
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001652 ret = btrfs_read_buffer(eb, gen);
1653 if (ret)
1654 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001655
1656 level = btrfs_header_level(eb);
1657
1658 if (level != 0)
1659 return 0;
1660
1661 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001662 if (!path)
1663 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001664
1665 nritems = btrfs_header_nritems(eb);
1666 for (i = 0; i < nritems; i++) {
1667 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001668
1669 /* inode keys are done during the first stage */
1670 if (key.type == BTRFS_INODE_ITEM_KEY &&
1671 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001672 struct btrfs_inode_item *inode_item;
1673 u32 mode;
1674
1675 inode_item = btrfs_item_ptr(eb, i,
1676 struct btrfs_inode_item);
1677 mode = btrfs_inode_mode(eb, inode_item);
1678 if (S_ISDIR(mode)) {
1679 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001680 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001681 BUG_ON(ret);
1682 }
1683 ret = overwrite_item(wc->trans, root, path,
1684 eb, i, &key);
1685 BUG_ON(ret);
1686
Yan, Zhengc71bf092009-11-12 09:34:40 +00001687 /* for regular files, make sure corresponding
1688 * orhpan item exist. extents past the new EOF
1689 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001690 */
1691 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001692 ret = insert_orphan_item(wc->trans, root,
1693 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001694 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001695 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001696
Chris Masone02119d2008-09-05 16:13:11 -04001697 ret = link_to_fixup_dir(wc->trans, root,
1698 path, key.objectid);
1699 BUG_ON(ret);
1700 }
1701 if (wc->stage < LOG_WALK_REPLAY_ALL)
1702 continue;
1703
1704 /* these keys are simply copied */
1705 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1706 ret = overwrite_item(wc->trans, root, path,
1707 eb, i, &key);
1708 BUG_ON(ret);
1709 } else if (key.type == BTRFS_INODE_REF_KEY) {
1710 ret = add_inode_ref(wc->trans, root, log, path,
1711 eb, i, &key);
1712 BUG_ON(ret && ret != -ENOENT);
1713 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1714 ret = replay_one_extent(wc->trans, root, path,
1715 eb, i, &key);
1716 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001717 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1718 key.type == BTRFS_DIR_INDEX_KEY) {
1719 ret = replay_one_dir_item(wc->trans, root, path,
1720 eb, i, &key);
1721 BUG_ON(ret);
1722 }
1723 }
1724 btrfs_free_path(path);
1725 return 0;
1726}
1727
Chris Masond3977122009-01-05 21:25:51 -05001728static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001729 struct btrfs_root *root,
1730 struct btrfs_path *path, int *level,
1731 struct walk_control *wc)
1732{
1733 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001734 u64 bytenr;
1735 u64 ptr_gen;
1736 struct extent_buffer *next;
1737 struct extent_buffer *cur;
1738 struct extent_buffer *parent;
1739 u32 blocksize;
1740 int ret = 0;
1741
1742 WARN_ON(*level < 0);
1743 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1744
Chris Masond3977122009-01-05 21:25:51 -05001745 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001746 WARN_ON(*level < 0);
1747 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1748 cur = path->nodes[*level];
1749
1750 if (btrfs_header_level(cur) != *level)
1751 WARN_ON(1);
1752
1753 if (path->slots[*level] >=
1754 btrfs_header_nritems(cur))
1755 break;
1756
1757 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1758 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1759 blocksize = btrfs_level_size(root, *level - 1);
1760
1761 parent = path->nodes[*level];
1762 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001763
1764 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00001765 if (!next)
1766 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001767
Chris Masone02119d2008-09-05 16:13:11 -04001768 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001769 ret = wc->process_func(root, next, wc, ptr_gen);
1770 if (ret)
1771 return ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001772
Chris Masone02119d2008-09-05 16:13:11 -04001773 path->slots[*level]++;
1774 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001775 ret = btrfs_read_buffer(next, ptr_gen);
1776 if (ret) {
1777 free_extent_buffer(next);
1778 return ret;
1779 }
Chris Masone02119d2008-09-05 16:13:11 -04001780
1781 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001782 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001783 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04001784 btrfs_wait_tree_block_writeback(next);
1785 btrfs_tree_unlock(next);
1786
Chris Masone02119d2008-09-05 16:13:11 -04001787 WARN_ON(root_owner !=
1788 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04001789 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04001790 bytenr, blocksize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001791 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04001792 }
1793 free_extent_buffer(next);
1794 continue;
1795 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001796 ret = btrfs_read_buffer(next, ptr_gen);
1797 if (ret) {
1798 free_extent_buffer(next);
1799 return ret;
1800 }
Chris Masone02119d2008-09-05 16:13:11 -04001801
1802 WARN_ON(*level <= 0);
1803 if (path->nodes[*level-1])
1804 free_extent_buffer(path->nodes[*level-1]);
1805 path->nodes[*level-1] = next;
1806 *level = btrfs_header_level(next);
1807 path->slots[*level] = 0;
1808 cond_resched();
1809 }
1810 WARN_ON(*level < 0);
1811 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1812
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001813 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04001814
1815 cond_resched();
1816 return 0;
1817}
1818
Chris Masond3977122009-01-05 21:25:51 -05001819static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001820 struct btrfs_root *root,
1821 struct btrfs_path *path, int *level,
1822 struct walk_control *wc)
1823{
1824 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001825 int i;
1826 int slot;
1827 int ret;
1828
Chris Masond3977122009-01-05 21:25:51 -05001829 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04001830 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001831 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04001832 path->slots[i]++;
1833 *level = i;
1834 WARN_ON(*level == 0);
1835 return 0;
1836 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001837 struct extent_buffer *parent;
1838 if (path->nodes[*level] == root->node)
1839 parent = path->nodes[*level];
1840 else
1841 parent = path->nodes[*level + 1];
1842
1843 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001844 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04001845 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001846 if (ret)
1847 return ret;
1848
Chris Masone02119d2008-09-05 16:13:11 -04001849 if (wc->free) {
1850 struct extent_buffer *next;
1851
1852 next = path->nodes[*level];
1853
1854 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001855 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001856 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04001857 btrfs_wait_tree_block_writeback(next);
1858 btrfs_tree_unlock(next);
1859
Chris Masone02119d2008-09-05 16:13:11 -04001860 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04001861 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04001862 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04001863 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04001864 BUG_ON(ret);
1865 }
1866 free_extent_buffer(path->nodes[*level]);
1867 path->nodes[*level] = NULL;
1868 *level = i + 1;
1869 }
1870 }
1871 return 1;
1872}
1873
1874/*
1875 * drop the reference count on the tree rooted at 'snap'. This traverses
1876 * the tree freeing any blocks that have a ref count of zero after being
1877 * decremented.
1878 */
1879static int walk_log_tree(struct btrfs_trans_handle *trans,
1880 struct btrfs_root *log, struct walk_control *wc)
1881{
1882 int ret = 0;
1883 int wret;
1884 int level;
1885 struct btrfs_path *path;
1886 int i;
1887 int orig_level;
1888
1889 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001890 if (!path)
1891 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001892
1893 level = btrfs_header_level(log->node);
1894 orig_level = level;
1895 path->nodes[level] = log->node;
1896 extent_buffer_get(log->node);
1897 path->slots[level] = 0;
1898
Chris Masond3977122009-01-05 21:25:51 -05001899 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001900 wret = walk_down_log_tree(trans, log, path, &level, wc);
1901 if (wret > 0)
1902 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001903 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001904 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 goto out;
1906 }
Chris Masone02119d2008-09-05 16:13:11 -04001907
1908 wret = walk_up_log_tree(trans, log, path, &level, wc);
1909 if (wret > 0)
1910 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001911 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001912 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001913 goto out;
1914 }
Chris Masone02119d2008-09-05 16:13:11 -04001915 }
1916
1917 /* was the root node processed? if not, catch it here */
1918 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001919 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04001920 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001921 if (ret)
1922 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001923 if (wc->free) {
1924 struct extent_buffer *next;
1925
1926 next = path->nodes[orig_level];
1927
1928 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001929 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001930 clean_tree_block(trans, log, next);
Chris Masone02119d2008-09-05 16:13:11 -04001931 btrfs_wait_tree_block_writeback(next);
1932 btrfs_tree_unlock(next);
1933
Chris Masone02119d2008-09-05 16:13:11 -04001934 WARN_ON(log->root_key.objectid !=
1935 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04001936 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04001937 next->len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001938 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04001939 }
1940 }
1941
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001942out:
Chris Masone02119d2008-09-05 16:13:11 -04001943 for (i = 0; i <= orig_level; i++) {
1944 if (path->nodes[i]) {
1945 free_extent_buffer(path->nodes[i]);
1946 path->nodes[i] = NULL;
1947 }
1948 }
1949 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001950 return ret;
1951}
1952
Yan Zheng7237f182009-01-21 12:54:03 -05001953/*
1954 * helper function to update the item for a given subvolumes log root
1955 * in the tree of log roots
1956 */
1957static int update_log_root(struct btrfs_trans_handle *trans,
1958 struct btrfs_root *log)
1959{
1960 int ret;
1961
1962 if (log->log_transid == 1) {
1963 /* insert root item on the first sync */
1964 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1965 &log->root_key, &log->root_item);
1966 } else {
1967 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1968 &log->root_key, &log->root_item);
1969 }
1970 return ret;
1971}
1972
Chris Mason12fcfd22009-03-24 10:24:20 -04001973static int wait_log_commit(struct btrfs_trans_handle *trans,
1974 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04001975{
1976 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05001977 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04001978
Yan Zheng7237f182009-01-21 12:54:03 -05001979 /*
1980 * we only allow two pending log transactions at a time,
1981 * so we know that if ours is more than 2 older than the
1982 * current transaction, we're done
1983 */
Chris Masone02119d2008-09-05 16:13:11 -04001984 do {
Yan Zheng7237f182009-01-21 12:54:03 -05001985 prepare_to_wait(&root->log_commit_wait[index],
1986 &wait, TASK_UNINTERRUPTIBLE);
1987 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001988
1989 if (root->fs_info->last_trans_log_full_commit !=
1990 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001991 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04001992 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04001993
Yan Zheng7237f182009-01-21 12:54:03 -05001994 finish_wait(&root->log_commit_wait[index], &wait);
1995 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05001996 } while (root->fs_info->last_trans_log_full_commit !=
1997 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001998 atomic_read(&root->log_commit[index]));
1999 return 0;
2000}
2001
Jeff Mahoney143bede2012-03-01 14:56:26 +01002002static void wait_for_writer(struct btrfs_trans_handle *trans,
2003 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002004{
2005 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002006 while (root->fs_info->last_trans_log_full_commit !=
2007 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002008 prepare_to_wait(&root->log_writer_wait,
2009 &wait, TASK_UNINTERRUPTIBLE);
2010 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002011 if (root->fs_info->last_trans_log_full_commit !=
2012 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002013 schedule();
2014 mutex_lock(&root->log_mutex);
2015 finish_wait(&root->log_writer_wait, &wait);
2016 }
Chris Masone02119d2008-09-05 16:13:11 -04002017}
2018
2019/*
2020 * btrfs_sync_log does sends a given tree log down to the disk and
2021 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002022 * you know that any inodes previously logged are safely on disk only
2023 * if it returns 0.
2024 *
2025 * Any other return value means you need to call btrfs_commit_transaction.
2026 * Some of the edge cases for fsyncing directories that have had unlinks
2027 * or renames done in the past mean that sometimes the only safe
2028 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2029 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002030 */
2031int btrfs_sync_log(struct btrfs_trans_handle *trans,
2032 struct btrfs_root *root)
2033{
Yan Zheng7237f182009-01-21 12:54:03 -05002034 int index1;
2035 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002036 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002037 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002038 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002039 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002040 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002041
Yan Zheng7237f182009-01-21 12:54:03 -05002042 mutex_lock(&root->log_mutex);
2043 index1 = root->log_transid % 2;
2044 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002045 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002046 mutex_unlock(&root->log_mutex);
2047 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002048 }
Yan Zheng7237f182009-01-21 12:54:03 -05002049 atomic_set(&root->log_commit[index1], 1);
2050
2051 /* wait for previous tree log sync to complete */
2052 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002053 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002054 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002055 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002056 /* when we're on an ssd, just kick the log commit out */
2057 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002058 mutex_unlock(&root->log_mutex);
2059 schedule_timeout_uninterruptible(1);
2060 mutex_lock(&root->log_mutex);
2061 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002062 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002063 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002064 break;
2065 }
Chris Masond0c803c2008-09-11 16:17:57 -04002066
Chris Mason12fcfd22009-03-24 10:24:20 -04002067 /* bail out if we need to do a full commit */
2068 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2069 ret = -EAGAIN;
2070 mutex_unlock(&root->log_mutex);
2071 goto out;
2072 }
2073
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002074 log_transid = root->log_transid;
2075 if (log_transid % 2 == 0)
2076 mark = EXTENT_DIRTY;
2077 else
2078 mark = EXTENT_NEW;
2079
Chris Mason690587d2009-10-13 13:29:19 -04002080 /* we start IO on all the marked extents here, but we don't actually
2081 * wait for them until later.
2082 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002083 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002084 if (ret) {
2085 btrfs_abort_transaction(trans, root, ret);
2086 mutex_unlock(&root->log_mutex);
2087 goto out;
2088 }
Yan Zheng7237f182009-01-21 12:54:03 -05002089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002090 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002091
Yan Zheng7237f182009-01-21 12:54:03 -05002092 root->log_transid++;
2093 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002094 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002095 smp_mb();
2096 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002097 * IO has been started, blocks of the log tree have WRITTEN flag set
2098 * in their headers. new modifications of the log will be written to
2099 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002100 */
2101 mutex_unlock(&root->log_mutex);
2102
2103 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002104 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002105 atomic_inc(&log_root_tree->log_writers);
2106 mutex_unlock(&log_root_tree->log_mutex);
2107
2108 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002109
2110 mutex_lock(&log_root_tree->log_mutex);
2111 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2112 smp_mb();
2113 if (waitqueue_active(&log_root_tree->log_writer_wait))
2114 wake_up(&log_root_tree->log_writer_wait);
2115 }
2116
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002117 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002118 if (ret != -ENOSPC) {
2119 btrfs_abort_transaction(trans, root, ret);
2120 mutex_unlock(&log_root_tree->log_mutex);
2121 goto out;
2122 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002123 root->fs_info->last_trans_log_full_commit = trans->transid;
2124 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2125 mutex_unlock(&log_root_tree->log_mutex);
2126 ret = -EAGAIN;
2127 goto out;
2128 }
2129
Yan Zheng7237f182009-01-21 12:54:03 -05002130 index2 = log_root_tree->log_transid % 2;
2131 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002132 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002133 wait_log_commit(trans, log_root_tree,
2134 log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002135 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002136 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002137 goto out;
2138 }
2139 atomic_set(&log_root_tree->log_commit[index2], 1);
2140
Chris Mason12fcfd22009-03-24 10:24:20 -04002141 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2142 wait_log_commit(trans, log_root_tree,
2143 log_root_tree->log_transid - 1);
2144 }
Yan Zheng7237f182009-01-21 12:54:03 -05002145
Chris Mason12fcfd22009-03-24 10:24:20 -04002146 wait_for_writer(trans, log_root_tree);
2147
2148 /*
2149 * now that we've moved on to the tree of log tree roots,
2150 * check the full commit flag again
2151 */
2152 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002153 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002154 mutex_unlock(&log_root_tree->log_mutex);
2155 ret = -EAGAIN;
2156 goto out_wake_log_root;
2157 }
Yan Zheng7237f182009-01-21 12:54:03 -05002158
2159 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002160 &log_root_tree->dirty_log_pages,
2161 EXTENT_DIRTY | EXTENT_NEW);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002162 if (ret) {
2163 btrfs_abort_transaction(trans, root, ret);
2164 mutex_unlock(&log_root_tree->log_mutex);
2165 goto out_wake_log_root;
2166 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002167 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002168
David Sterba6c417612011-04-13 15:41:04 +02002169 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002170 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002171 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002172 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002173
Yan Zheng7237f182009-01-21 12:54:03 -05002174 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002175 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002176
2177 mutex_unlock(&log_root_tree->log_mutex);
2178
2179 /*
2180 * nobody else is going to jump in and write the the ctree
2181 * super here because the log_commit atomic below is protecting
2182 * us. We must be called with a transaction handle pinning
2183 * the running transaction open, so a full commit can't hop
2184 * in and cause problems either.
2185 */
Arne Jansena2de7332011-03-08 14:14:00 +01002186 btrfs_scrub_pause_super(root);
Chris Mason47226072009-10-13 12:55:09 -04002187 write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002188 btrfs_scrub_continue_super(root);
Chris Mason12fcfd22009-03-24 10:24:20 -04002189 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002190
Chris Mason257c62e2009-10-13 13:21:08 -04002191 mutex_lock(&root->log_mutex);
2192 if (root->last_log_commit < log_transid)
2193 root->last_log_commit = log_transid;
2194 mutex_unlock(&root->log_mutex);
2195
Chris Mason12fcfd22009-03-24 10:24:20 -04002196out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002197 atomic_set(&log_root_tree->log_commit[index2], 0);
2198 smp_mb();
2199 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2200 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002201out:
Yan Zheng7237f182009-01-21 12:54:03 -05002202 atomic_set(&root->log_commit[index1], 0);
2203 smp_mb();
2204 if (waitqueue_active(&root->log_commit_wait[index1]))
2205 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002206 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002207}
2208
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002209static void free_log_tree(struct btrfs_trans_handle *trans,
2210 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002211{
2212 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002213 u64 start;
2214 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002215 struct walk_control wc = {
2216 .free = 1,
2217 .process_func = process_one_buffer
2218 };
2219
Chris Masone02119d2008-09-05 16:13:11 -04002220 ret = walk_log_tree(trans, log, &wc);
2221 BUG_ON(ret);
2222
Chris Masond3977122009-01-05 21:25:51 -05002223 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002224 ret = find_first_extent_bit(&log->dirty_log_pages,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002225 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04002226 if (ret)
2227 break;
2228
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002229 clear_extent_bits(&log->dirty_log_pages, start, end,
2230 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002231 }
2232
Yan Zheng7237f182009-01-21 12:54:03 -05002233 free_extent_buffer(log->node);
2234 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002235}
2236
2237/*
2238 * free all the extents used by the tree log. This should be called
2239 * at commit time of the full transaction
2240 */
2241int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2242{
2243 if (root->log_root) {
2244 free_log_tree(trans, root->log_root);
2245 root->log_root = NULL;
2246 }
2247 return 0;
2248}
2249
2250int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2251 struct btrfs_fs_info *fs_info)
2252{
2253 if (fs_info->log_root_tree) {
2254 free_log_tree(trans, fs_info->log_root_tree);
2255 fs_info->log_root_tree = NULL;
2256 }
Chris Masone02119d2008-09-05 16:13:11 -04002257 return 0;
2258}
2259
2260/*
Chris Masone02119d2008-09-05 16:13:11 -04002261 * If both a file and directory are logged, and unlinks or renames are
2262 * mixed in, we have a few interesting corners:
2263 *
2264 * create file X in dir Y
2265 * link file X to X.link in dir Y
2266 * fsync file X
2267 * unlink file X but leave X.link
2268 * fsync dir Y
2269 *
2270 * After a crash we would expect only X.link to exist. But file X
2271 * didn't get fsync'd again so the log has back refs for X and X.link.
2272 *
2273 * We solve this by removing directory entries and inode backrefs from the
2274 * log when a file that was logged in the current transaction is
2275 * unlinked. Any later fsync will include the updated log entries, and
2276 * we'll be able to reconstruct the proper directory items from backrefs.
2277 *
2278 * This optimizations allows us to avoid relogging the entire inode
2279 * or the entire directory.
2280 */
2281int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2282 struct btrfs_root *root,
2283 const char *name, int name_len,
2284 struct inode *dir, u64 index)
2285{
2286 struct btrfs_root *log;
2287 struct btrfs_dir_item *di;
2288 struct btrfs_path *path;
2289 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002290 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002291 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002292 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002293
Chris Mason3a5f1d42008-09-11 15:53:37 -04002294 if (BTRFS_I(dir)->logged_trans < trans->transid)
2295 return 0;
2296
Chris Masone02119d2008-09-05 16:13:11 -04002297 ret = join_running_log_trans(root);
2298 if (ret)
2299 return 0;
2300
2301 mutex_lock(&BTRFS_I(dir)->log_mutex);
2302
2303 log = root->log_root;
2304 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002305 if (!path) {
2306 err = -ENOMEM;
2307 goto out_unlock;
2308 }
liubo2a29edc2011-01-26 06:22:08 +00002309
Li Zefan33345d012011-04-20 10:31:50 +08002310 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002311 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 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002321 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002322 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002323 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002324 if (IS_ERR(di)) {
2325 err = PTR_ERR(di);
2326 goto fail;
2327 }
2328 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002329 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2330 bytes_del += name_len;
2331 BUG_ON(ret);
2332 }
2333
2334 /* update the directory size in the log to reflect the names
2335 * we have removed
2336 */
2337 if (bytes_del) {
2338 struct btrfs_key key;
2339
Li Zefan33345d012011-04-20 10:31:50 +08002340 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002341 key.offset = 0;
2342 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002343 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002344
2345 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002346 if (ret < 0) {
2347 err = ret;
2348 goto fail;
2349 }
Chris Masone02119d2008-09-05 16:13:11 -04002350 if (ret == 0) {
2351 struct btrfs_inode_item *item;
2352 u64 i_size;
2353
2354 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2355 struct btrfs_inode_item);
2356 i_size = btrfs_inode_size(path->nodes[0], item);
2357 if (i_size > bytes_del)
2358 i_size -= bytes_del;
2359 else
2360 i_size = 0;
2361 btrfs_set_inode_size(path->nodes[0], item, i_size);
2362 btrfs_mark_buffer_dirty(path->nodes[0]);
2363 } else
2364 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002365 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002366 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002367fail:
Chris Masone02119d2008-09-05 16:13:11 -04002368 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002369out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002370 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002371 if (ret == -ENOSPC) {
2372 root->fs_info->last_trans_log_full_commit = trans->transid;
2373 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002374 } else if (ret < 0)
2375 btrfs_abort_transaction(trans, root, ret);
2376
Chris Mason12fcfd22009-03-24 10:24:20 -04002377 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002378
Andi Kleen411fc6b2010-10-29 15:14:31 -04002379 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002380}
2381
2382/* see comments for btrfs_del_dir_entries_in_log */
2383int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2384 struct btrfs_root *root,
2385 const char *name, int name_len,
2386 struct inode *inode, u64 dirid)
2387{
2388 struct btrfs_root *log;
2389 u64 index;
2390 int ret;
2391
Chris Mason3a5f1d42008-09-11 15:53:37 -04002392 if (BTRFS_I(inode)->logged_trans < trans->transid)
2393 return 0;
2394
Chris Masone02119d2008-09-05 16:13:11 -04002395 ret = join_running_log_trans(root);
2396 if (ret)
2397 return 0;
2398 log = root->log_root;
2399 mutex_lock(&BTRFS_I(inode)->log_mutex);
2400
Li Zefan33345d012011-04-20 10:31:50 +08002401 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002402 dirid, &index);
2403 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002404 if (ret == -ENOSPC) {
2405 root->fs_info->last_trans_log_full_commit = trans->transid;
2406 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002407 } else if (ret < 0 && ret != -ENOENT)
2408 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002409 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002410
Chris Masone02119d2008-09-05 16:13:11 -04002411 return ret;
2412}
2413
2414/*
2415 * creates a range item in the log for 'dirid'. first_offset and
2416 * last_offset tell us which parts of the key space the log should
2417 * be considered authoritative for.
2418 */
2419static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2420 struct btrfs_root *log,
2421 struct btrfs_path *path,
2422 int key_type, u64 dirid,
2423 u64 first_offset, u64 last_offset)
2424{
2425 int ret;
2426 struct btrfs_key key;
2427 struct btrfs_dir_log_item *item;
2428
2429 key.objectid = dirid;
2430 key.offset = first_offset;
2431 if (key_type == BTRFS_DIR_ITEM_KEY)
2432 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2433 else
2434 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2435 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002436 if (ret)
2437 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002438
2439 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2440 struct btrfs_dir_log_item);
2441 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2442 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002443 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002444 return 0;
2445}
2446
2447/*
2448 * log all the items included in the current transaction for a given
2449 * directory. This also creates the range items in the log tree required
2450 * to replay anything deleted before the fsync
2451 */
2452static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2453 struct btrfs_root *root, struct inode *inode,
2454 struct btrfs_path *path,
2455 struct btrfs_path *dst_path, int key_type,
2456 u64 min_offset, u64 *last_offset_ret)
2457{
2458 struct btrfs_key min_key;
2459 struct btrfs_key max_key;
2460 struct btrfs_root *log = root->log_root;
2461 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002462 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002463 int ret;
2464 int i;
2465 int nritems;
2466 u64 first_offset = min_offset;
2467 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002468 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002469
2470 log = root->log_root;
Li Zefan33345d012011-04-20 10:31:50 +08002471 max_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002472 max_key.offset = (u64)-1;
2473 max_key.type = key_type;
2474
Li Zefan33345d012011-04-20 10:31:50 +08002475 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002476 min_key.type = key_type;
2477 min_key.offset = min_offset;
2478
2479 path->keep_locks = 1;
2480
2481 ret = btrfs_search_forward(root, &min_key, &max_key,
2482 path, 0, trans->transid);
2483
2484 /*
2485 * we didn't find anything from this transaction, see if there
2486 * is anything at all
2487 */
Li Zefan33345d012011-04-20 10:31:50 +08002488 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2489 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002490 min_key.type = key_type;
2491 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002492 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002493 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2494 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002495 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002496 return ret;
2497 }
Li Zefan33345d012011-04-20 10:31:50 +08002498 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002499
2500 /* if ret == 0 there are items for this type,
2501 * create a range to tell us the last key of this type.
2502 * otherwise, there are no items in this directory after
2503 * *min_offset, and we create a range to indicate that.
2504 */
2505 if (ret == 0) {
2506 struct btrfs_key tmp;
2507 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2508 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002509 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002510 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002511 }
2512 goto done;
2513 }
2514
2515 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002516 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002517 if (ret == 0) {
2518 struct btrfs_key tmp;
2519 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2520 if (key_type == tmp.type) {
2521 first_offset = tmp.offset;
2522 ret = overwrite_item(trans, log, dst_path,
2523 path->nodes[0], path->slots[0],
2524 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002525 if (ret) {
2526 err = ret;
2527 goto done;
2528 }
Chris Masone02119d2008-09-05 16:13:11 -04002529 }
2530 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002531 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002532
2533 /* find the first key from this transaction again */
2534 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2535 if (ret != 0) {
2536 WARN_ON(1);
2537 goto done;
2538 }
2539
2540 /*
2541 * we have a block from this transaction, log every item in it
2542 * from our directory
2543 */
Chris Masond3977122009-01-05 21:25:51 -05002544 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002545 struct btrfs_key tmp;
2546 src = path->nodes[0];
2547 nritems = btrfs_header_nritems(src);
2548 for (i = path->slots[0]; i < nritems; i++) {
2549 btrfs_item_key_to_cpu(src, &min_key, i);
2550
Li Zefan33345d012011-04-20 10:31:50 +08002551 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002552 goto done;
2553 ret = overwrite_item(trans, log, dst_path, src, i,
2554 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002555 if (ret) {
2556 err = ret;
2557 goto done;
2558 }
Chris Masone02119d2008-09-05 16:13:11 -04002559 }
2560 path->slots[0] = nritems;
2561
2562 /*
2563 * look ahead to the next item and see if it is also
2564 * from this directory and from this transaction
2565 */
2566 ret = btrfs_next_leaf(root, path);
2567 if (ret == 1) {
2568 last_offset = (u64)-1;
2569 goto done;
2570 }
2571 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002572 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002573 last_offset = (u64)-1;
2574 goto done;
2575 }
2576 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2577 ret = overwrite_item(trans, log, dst_path,
2578 path->nodes[0], path->slots[0],
2579 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002580 if (ret)
2581 err = ret;
2582 else
2583 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002584 goto done;
2585 }
2586 }
2587done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002588 btrfs_release_path(path);
2589 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002590
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002591 if (err == 0) {
2592 *last_offset_ret = last_offset;
2593 /*
2594 * insert the log range keys to indicate where the log
2595 * is valid
2596 */
2597 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08002598 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002599 if (ret)
2600 err = ret;
2601 }
2602 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002603}
2604
2605/*
2606 * logging directories is very similar to logging inodes, We find all the items
2607 * from the current transaction and write them to the log.
2608 *
2609 * The recovery code scans the directory in the subvolume, and if it finds a
2610 * key in the range logged that is not present in the log tree, then it means
2611 * that dir entry was unlinked during the transaction.
2612 *
2613 * In order for that scan to work, we must include one key smaller than
2614 * the smallest logged by this transaction and one key larger than the largest
2615 * key logged by this transaction.
2616 */
2617static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2618 struct btrfs_root *root, struct inode *inode,
2619 struct btrfs_path *path,
2620 struct btrfs_path *dst_path)
2621{
2622 u64 min_key;
2623 u64 max_key;
2624 int ret;
2625 int key_type = BTRFS_DIR_ITEM_KEY;
2626
2627again:
2628 min_key = 0;
2629 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002630 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002631 ret = log_dir_items(trans, root, inode, path,
2632 dst_path, key_type, min_key,
2633 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002634 if (ret)
2635 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002636 if (max_key == (u64)-1)
2637 break;
2638 min_key = max_key + 1;
2639 }
2640
2641 if (key_type == BTRFS_DIR_ITEM_KEY) {
2642 key_type = BTRFS_DIR_INDEX_KEY;
2643 goto again;
2644 }
2645 return 0;
2646}
2647
2648/*
2649 * a helper function to drop items from the log before we relog an
2650 * inode. max_key_type indicates the highest item type to remove.
2651 * This cannot be run for file data extents because it does not
2652 * free the extents they point to.
2653 */
2654static int drop_objectid_items(struct btrfs_trans_handle *trans,
2655 struct btrfs_root *log,
2656 struct btrfs_path *path,
2657 u64 objectid, int max_key_type)
2658{
2659 int ret;
2660 struct btrfs_key key;
2661 struct btrfs_key found_key;
2662
2663 key.objectid = objectid;
2664 key.type = max_key_type;
2665 key.offset = (u64)-1;
2666
Chris Masond3977122009-01-05 21:25:51 -05002667 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002668 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002669 BUG_ON(ret == 0);
2670 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002671 break;
2672
2673 if (path->slots[0] == 0)
2674 break;
2675
2676 path->slots[0]--;
2677 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2678 path->slots[0]);
2679
2680 if (found_key.objectid != objectid)
2681 break;
2682
2683 ret = btrfs_del_item(trans, log, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002684 if (ret)
2685 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02002686 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002687 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002688 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04002689 if (ret > 0)
2690 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002691 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002692}
2693
Chris Mason31ff1cd2008-09-11 16:17:57 -04002694static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06002695 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04002696 struct btrfs_path *dst_path,
2697 struct extent_buffer *src,
2698 int start_slot, int nr, int inode_only)
2699{
2700 unsigned long src_offset;
2701 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06002702 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002703 struct btrfs_file_extent_item *extent;
2704 struct btrfs_inode_item *inode_item;
2705 int ret;
2706 struct btrfs_key *ins_keys;
2707 u32 *ins_sizes;
2708 char *ins_data;
2709 int i;
Chris Masond20f7042008-12-08 16:58:54 -05002710 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06002711 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05002712
2713 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002714
2715 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2716 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00002717 if (!ins_data)
2718 return -ENOMEM;
2719
Chris Mason31ff1cd2008-09-11 16:17:57 -04002720 ins_sizes = (u32 *)ins_data;
2721 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2722
2723 for (i = 0; i < nr; i++) {
2724 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2725 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2726 }
2727 ret = btrfs_insert_empty_items(trans, log, dst_path,
2728 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002729 if (ret) {
2730 kfree(ins_data);
2731 return ret;
2732 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002733
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002734 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002735 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2736 dst_path->slots[0]);
2737
2738 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2739
2740 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2741 src_offset, ins_sizes[i]);
2742
2743 if (inode_only == LOG_INODE_EXISTS &&
2744 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2745 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2746 dst_path->slots[0],
2747 struct btrfs_inode_item);
2748 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2749
2750 /* set the generation to zero so the recover code
2751 * can tell the difference between an logging
2752 * just to say 'this inode exists' and a logging
2753 * to say 'update this inode with these values'
2754 */
2755 btrfs_set_inode_generation(dst_path->nodes[0],
2756 inode_item, 0);
2757 }
2758 /* take a reference on file data extents so that truncates
2759 * or deletes of this inode don't have to relog the inode
2760 * again
2761 */
Liu Bod2794402012-08-29 01:07:56 -06002762 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
2763 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002764 int found_type;
2765 extent = btrfs_item_ptr(src, start_slot + i,
2766 struct btrfs_file_extent_item);
2767
liubo8e531cd2011-05-06 10:36:09 +08002768 if (btrfs_file_extent_generation(src, extent) < trans->transid)
2769 continue;
2770
Chris Mason31ff1cd2008-09-11 16:17:57 -04002771 found_type = btrfs_file_extent_type(src, extent);
Yan Zhengd899e052008-10-30 14:25:28 -04002772 if (found_type == BTRFS_FILE_EXTENT_REG ||
2773 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002774 u64 ds, dl, cs, cl;
2775 ds = btrfs_file_extent_disk_bytenr(src,
2776 extent);
2777 /* ds == 0 is a hole */
2778 if (ds == 0)
2779 continue;
2780
2781 dl = btrfs_file_extent_disk_num_bytes(src,
2782 extent);
2783 cs = btrfs_file_extent_offset(src, extent);
2784 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07002785 extent);
Chris Mason580afd72008-12-08 19:15:39 -05002786 if (btrfs_file_extent_compression(src,
2787 extent)) {
2788 cs = 0;
2789 cl = dl;
2790 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002791
2792 ret = btrfs_lookup_csums_range(
2793 log->fs_info->csum_root,
2794 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01002795 &ordered_sums, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002796 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002797 }
2798 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002799 }
2800
2801 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002802 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002803 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05002804
2805 /*
2806 * we have to do this after the loop above to avoid changing the
2807 * log tree while trying to change the log tree.
2808 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002809 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002810 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05002811 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2812 struct btrfs_ordered_sum,
2813 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002814 if (!ret)
2815 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05002816 list_del(&sums->list);
2817 kfree(sums);
2818 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002819 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002820}
2821
Josef Bacik5dc562c2012-08-17 13:14:17 -04002822static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
2823{
2824 struct extent_map *em1, *em2;
2825
2826 em1 = list_entry(a, struct extent_map, list);
2827 em2 = list_entry(b, struct extent_map, list);
2828
2829 if (em1->start < em2->start)
2830 return -1;
2831 else if (em1->start > em2->start)
2832 return 1;
2833 return 0;
2834}
2835
2836struct log_args {
2837 struct extent_buffer *src;
2838 u64 next_offset;
2839 int start_slot;
2840 int nr;
2841};
2842
2843static int log_one_extent(struct btrfs_trans_handle *trans,
2844 struct inode *inode, struct btrfs_root *root,
2845 struct extent_map *em, struct btrfs_path *path,
2846 struct btrfs_path *dst_path, struct log_args *args)
2847{
2848 struct btrfs_root *log = root->log_root;
2849 struct btrfs_file_extent_item *fi;
2850 struct btrfs_key key;
Liu Bo4e2f84e2012-08-27 10:52:20 -06002851 u64 start = em->mod_start;
Josef Bacik0aa4a172012-09-19 15:42:38 -04002852 u64 search_start = start;
Liu Bo4e2f84e2012-08-27 10:52:20 -06002853 u64 len = em->mod_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04002854 u64 num_bytes;
2855 int nritems;
2856 int ret;
2857
2858 if (BTRFS_I(inode)->logged_trans == trans->transid) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04002859 ret = __btrfs_drop_extents(trans, log, inode, dst_path, start,
Josef Bacik2aaa6652012-08-29 14:27:18 -04002860 start + len, NULL, 0);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002861 if (ret)
2862 return ret;
2863 }
2864
2865 while (len) {
2866 if (args->nr)
2867 goto next_slot;
Josef Bacik0aa4a172012-09-19 15:42:38 -04002868again:
Josef Bacik5dc562c2012-08-17 13:14:17 -04002869 key.objectid = btrfs_ino(inode);
2870 key.type = BTRFS_EXTENT_DATA_KEY;
Josef Bacik0aa4a172012-09-19 15:42:38 -04002871 key.offset = search_start;
Josef Bacik5dc562c2012-08-17 13:14:17 -04002872
2873 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2874 if (ret < 0)
2875 return ret;
Josef Bacik0aa4a172012-09-19 15:42:38 -04002876
Josef Bacik5dc562c2012-08-17 13:14:17 -04002877 if (ret) {
2878 /*
Josef Bacik0aa4a172012-09-19 15:42:38 -04002879 * A rare case were we can have an em for a section of a
2880 * larger extent so we need to make sure that this em
2881 * falls within the extent we've found. If not we just
2882 * bail and go back to ye-olde way of doing things but
2883 * it happens often enough in testing that we need to do
2884 * this dance to make sure.
Josef Bacik5dc562c2012-08-17 13:14:17 -04002885 */
Josef Bacik0aa4a172012-09-19 15:42:38 -04002886 do {
2887 if (path->slots[0] == 0) {
2888 btrfs_release_path(path);
2889 if (search_start == 0)
2890 return -ENOENT;
2891 search_start--;
2892 goto again;
2893 }
2894
2895 path->slots[0]--;
2896 btrfs_item_key_to_cpu(path->nodes[0], &key,
2897 path->slots[0]);
2898 if (key.objectid != btrfs_ino(inode) ||
2899 key.type != BTRFS_EXTENT_DATA_KEY) {
2900 btrfs_release_path(path);
2901 return -ENOENT;
2902 }
2903 } while (key.offset > start);
2904
2905 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
2906 struct btrfs_file_extent_item);
2907 num_bytes = btrfs_file_extent_num_bytes(path->nodes[0],
2908 fi);
2909 if (key.offset + num_bytes <= start) {
2910 btrfs_release_path(path);
2911 return -ENOENT;
2912 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002913 }
2914 args->src = path->nodes[0];
2915next_slot:
Josef Bacik0aa4a172012-09-19 15:42:38 -04002916 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002917 fi = btrfs_item_ptr(args->src, path->slots[0],
2918 struct btrfs_file_extent_item);
2919 if (args->nr &&
2920 args->start_slot + args->nr == path->slots[0]) {
2921 args->nr++;
2922 } else if (args->nr) {
Liu Bod2794402012-08-29 01:07:56 -06002923 ret = copy_items(trans, inode, dst_path, args->src,
Josef Bacik5dc562c2012-08-17 13:14:17 -04002924 args->start_slot, args->nr,
2925 LOG_INODE_ALL);
2926 if (ret)
2927 return ret;
2928 args->nr = 1;
2929 args->start_slot = path->slots[0];
2930 } else if (!args->nr) {
2931 args->nr = 1;
2932 args->start_slot = path->slots[0];
2933 }
2934 nritems = btrfs_header_nritems(path->nodes[0]);
2935 path->slots[0]++;
2936 num_bytes = btrfs_file_extent_num_bytes(args->src, fi);
2937 if (len < num_bytes) {
2938 /* I _think_ this is ok, envision we write to a
2939 * preallocated space that is adjacent to a previously
2940 * written preallocated space that gets merged when we
2941 * mark this preallocated space written. If we do not
2942 * have the adjacent extent in cache then when we copy
2943 * this extent it could end up being larger than our EM
2944 * thinks it is, which is a-ok, so just set len to 0.
2945 */
2946 len = 0;
2947 } else {
2948 len -= num_bytes;
2949 }
Josef Bacik0aa4a172012-09-19 15:42:38 -04002950 start = key.offset + num_bytes;
Josef Bacik5dc562c2012-08-17 13:14:17 -04002951 args->next_offset = start;
Josef Bacik0aa4a172012-09-19 15:42:38 -04002952 search_start = start;
Josef Bacik5dc562c2012-08-17 13:14:17 -04002953
2954 if (path->slots[0] < nritems) {
2955 if (len)
2956 goto next_slot;
2957 break;
2958 }
2959
2960 if (args->nr) {
Liu Bod2794402012-08-29 01:07:56 -06002961 ret = copy_items(trans, inode, dst_path, args->src,
Josef Bacik5dc562c2012-08-17 13:14:17 -04002962 args->start_slot, args->nr,
2963 LOG_INODE_ALL);
2964 if (ret)
2965 return ret;
2966 args->nr = 0;
2967 btrfs_release_path(path);
2968 }
2969 }
2970
2971 return 0;
2972}
2973
2974static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
2975 struct btrfs_root *root,
2976 struct inode *inode,
2977 struct btrfs_path *path,
2978 struct btrfs_path *dst_path)
2979{
2980 struct log_args args;
Josef Bacik5dc562c2012-08-17 13:14:17 -04002981 struct extent_map *em, *n;
2982 struct list_head extents;
2983 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
2984 u64 test_gen;
2985 int ret = 0;
2986
2987 INIT_LIST_HEAD(&extents);
2988
2989 memset(&args, 0, sizeof(args));
2990
2991 write_lock(&tree->lock);
2992 test_gen = root->fs_info->last_trans_committed;
2993
2994 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
2995 list_del_init(&em->list);
2996 if (em->generation <= test_gen)
2997 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04002998 /* Need a ref to keep it from getting evicted from cache */
2999 atomic_inc(&em->refs);
3000 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003001 list_add_tail(&em->list, &extents);
3002 }
3003
3004 list_sort(NULL, &extents, extent_cmp);
3005
3006 while (!list_empty(&extents)) {
3007 em = list_entry(extents.next, struct extent_map, list);
3008
3009 list_del_init(&em->list);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003010 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003011
3012 /*
3013 * If we had an error we just need to delete everybody from our
3014 * private list.
3015 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003016 if (ret) {
3017 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003018 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003019 }
3020
3021 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003022
3023 /*
3024 * If the previous EM and the last extent we left off on aren't
3025 * sequential then we need to copy the items we have and redo
3026 * our search
3027 */
Liu Bo4e2f84e2012-08-27 10:52:20 -06003028 if (args.nr && em->mod_start != args.next_offset) {
Liu Bod2794402012-08-29 01:07:56 -06003029 ret = copy_items(trans, inode, dst_path, args.src,
Josef Bacik5dc562c2012-08-17 13:14:17 -04003030 args.start_slot, args.nr,
3031 LOG_INODE_ALL);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003032 if (ret) {
3033 free_extent_map(em);
3034 write_lock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003035 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003036 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003037 btrfs_release_path(path);
3038 args.nr = 0;
3039 }
3040
3041 ret = log_one_extent(trans, inode, root, em, path, dst_path, &args);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003042 free_extent_map(em);
3043 write_lock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003044 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003045 WARN_ON(!list_empty(&extents));
3046 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003047
3048 if (!ret && args.nr)
Liu Bod2794402012-08-29 01:07:56 -06003049 ret = copy_items(trans, inode, dst_path, args.src,
Josef Bacik5dc562c2012-08-17 13:14:17 -04003050 args.start_slot, args.nr, LOG_INODE_ALL);
3051 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003052 return ret;
3053}
3054
Chris Masone02119d2008-09-05 16:13:11 -04003055/* log a single inode in the tree log.
3056 * At least one parent directory for this inode must exist in the tree
3057 * or be logged already.
3058 *
3059 * Any items from this inode changed by the current transaction are copied
3060 * to the log tree. An extra reference is taken on any extents in this
3061 * file, allowing us to avoid a whole pile of corner cases around logging
3062 * blocks that have been removed from the tree.
3063 *
3064 * See LOG_INODE_ALL and related defines for a description of what inode_only
3065 * does.
3066 *
3067 * This handles both files and directories.
3068 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003069static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003070 struct btrfs_root *root, struct inode *inode,
3071 int inode_only)
3072{
3073 struct btrfs_path *path;
3074 struct btrfs_path *dst_path;
3075 struct btrfs_key min_key;
3076 struct btrfs_key max_key;
3077 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003078 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003079 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003080 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003081 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003082 int ins_start_slot = 0;
3083 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003084 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003085 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003086
3087 log = root->log_root;
3088
3089 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003090 if (!path)
3091 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003092 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003093 if (!dst_path) {
3094 btrfs_free_path(path);
3095 return -ENOMEM;
3096 }
Chris Masone02119d2008-09-05 16:13:11 -04003097
Li Zefan33345d012011-04-20 10:31:50 +08003098 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003099 min_key.type = BTRFS_INODE_ITEM_KEY;
3100 min_key.offset = 0;
3101
Li Zefan33345d012011-04-20 10:31:50 +08003102 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003103
Chris Mason12fcfd22009-03-24 10:24:20 -04003104
Josef Bacik5dc562c2012-08-17 13:14:17 -04003105 /* today the code can only do partial logging of directories */
Chris Masone02119d2008-09-05 16:13:11 -04003106 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
3107 max_key.type = BTRFS_XATTR_ITEM_KEY;
3108 else
3109 max_key.type = (u8)-1;
3110 max_key.offset = (u64)-1;
3111
Miao Xie16cdcec2011-04-22 18:12:22 +08003112 ret = btrfs_commit_inode_delayed_items(trans, inode);
3113 if (ret) {
3114 btrfs_free_path(path);
3115 btrfs_free_path(dst_path);
3116 return ret;
3117 }
3118
Chris Masone02119d2008-09-05 16:13:11 -04003119 mutex_lock(&BTRFS_I(inode)->log_mutex);
3120
3121 /*
3122 * a brute force approach to making sure we get the most uptodate
3123 * copies of everything.
3124 */
3125 if (S_ISDIR(inode->i_mode)) {
3126 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3127
3128 if (inode_only == LOG_INODE_EXISTS)
3129 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003130 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003131 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003132 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3133 &BTRFS_I(inode)->runtime_flags)) {
3134 ret = btrfs_truncate_inode_items(trans, log,
3135 inode, 0, 0);
3136 } else {
3137 fast_search = true;
3138 max_key.type = BTRFS_XATTR_ITEM_KEY;
3139 ret = drop_objectid_items(trans, log, path, ino,
3140 BTRFS_XATTR_ITEM_KEY);
3141 }
Chris Masone02119d2008-09-05 16:13:11 -04003142 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003143 if (ret) {
3144 err = ret;
3145 goto out_unlock;
3146 }
Chris Masone02119d2008-09-05 16:13:11 -04003147 path->keep_locks = 1;
3148
Chris Masond3977122009-01-05 21:25:51 -05003149 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003150 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003151 ret = btrfs_search_forward(root, &min_key, &max_key,
3152 path, 0, trans->transid);
3153 if (ret != 0)
3154 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003155again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003156 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003157 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003158 break;
3159 if (min_key.type > max_key.type)
3160 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003161
Chris Masone02119d2008-09-05 16:13:11 -04003162 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003163 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3164 ins_nr++;
3165 goto next_slot;
3166 } else if (!ins_nr) {
3167 ins_start_slot = path->slots[0];
3168 ins_nr = 1;
3169 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003170 }
3171
Liu Bod2794402012-08-29 01:07:56 -06003172 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003173 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003174 if (ret) {
3175 err = ret;
3176 goto out_unlock;
3177 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003178 ins_nr = 1;
3179 ins_start_slot = path->slots[0];
3180next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003181
Chris Mason3a5f1d42008-09-11 15:53:37 -04003182 nritems = btrfs_header_nritems(path->nodes[0]);
3183 path->slots[0]++;
3184 if (path->slots[0] < nritems) {
3185 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3186 path->slots[0]);
3187 goto again;
3188 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003189 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003190 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003191 ins_start_slot,
3192 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003193 if (ret) {
3194 err = ret;
3195 goto out_unlock;
3196 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003197 ins_nr = 0;
3198 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003199 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003200
Chris Masone02119d2008-09-05 16:13:11 -04003201 if (min_key.offset < (u64)-1)
3202 min_key.offset++;
3203 else if (min_key.type < (u8)-1)
3204 min_key.type++;
3205 else if (min_key.objectid < (u64)-1)
3206 min_key.objectid++;
3207 else
3208 break;
3209 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003210 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003211 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003212 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003213 if (ret) {
3214 err = ret;
3215 goto out_unlock;
3216 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003217 ins_nr = 0;
3218 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003219
3220 if (fast_search) {
3221 btrfs_release_path(path);
3222 btrfs_release_path(dst_path);
3223 ret = btrfs_log_changed_extents(trans, root, inode, path,
3224 dst_path);
3225 if (ret) {
3226 err = ret;
3227 goto out_unlock;
3228 }
Liu Bo06d3d222012-08-27 10:52:19 -06003229 } else {
3230 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3231 struct extent_map *em, *n;
3232
3233 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3234 list_del_init(&em->list);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003235 }
3236
Chris Mason9623f9a2008-09-11 17:42:42 -04003237 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003238 btrfs_release_path(path);
3239 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003240 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003241 if (ret) {
3242 err = ret;
3243 goto out_unlock;
3244 }
Chris Masone02119d2008-09-05 16:13:11 -04003245 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003246 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003247 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003248out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003249 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3250
3251 btrfs_free_path(path);
3252 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003253 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003254}
3255
Chris Mason12fcfd22009-03-24 10:24:20 -04003256/*
3257 * follow the dentry parent pointers up the chain and see if any
3258 * of the directories in it require a full commit before they can
3259 * be logged. Returns zero if nothing special needs to be done or 1 if
3260 * a full commit is required.
3261 */
3262static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3263 struct inode *inode,
3264 struct dentry *parent,
3265 struct super_block *sb,
3266 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003267{
Chris Mason12fcfd22009-03-24 10:24:20 -04003268 int ret = 0;
3269 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003270 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003271
Chris Masonaf4176b2009-03-24 10:24:31 -04003272 /*
3273 * for regular files, if its inode is already on disk, we don't
3274 * have to worry about the parents at all. This is because
3275 * we can use the last_unlink_trans field to record renames
3276 * and other fun in this file.
3277 */
3278 if (S_ISREG(inode->i_mode) &&
3279 BTRFS_I(inode)->generation <= last_committed &&
3280 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3281 goto out;
3282
Chris Mason12fcfd22009-03-24 10:24:20 -04003283 if (!S_ISDIR(inode->i_mode)) {
3284 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3285 goto out;
3286 inode = parent->d_inode;
3287 }
3288
3289 while (1) {
3290 BTRFS_I(inode)->logged_trans = trans->transid;
3291 smp_mb();
3292
3293 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3294 root = BTRFS_I(inode)->root;
3295
3296 /*
3297 * make sure any commits to the log are forced
3298 * to be full commits
3299 */
3300 root->fs_info->last_trans_log_full_commit =
3301 trans->transid;
3302 ret = 1;
3303 break;
3304 }
3305
3306 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3307 break;
3308
Yan, Zheng76dda932009-09-21 16:00:26 -04003309 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003310 break;
3311
Josef Bacik6a912212010-11-20 09:48:00 +00003312 parent = dget_parent(parent);
3313 dput(old_parent);
3314 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003315 inode = parent->d_inode;
3316
3317 }
Josef Bacik6a912212010-11-20 09:48:00 +00003318 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003319out:
Chris Masone02119d2008-09-05 16:13:11 -04003320 return ret;
3321}
3322
3323/*
3324 * helper function around btrfs_log_inode to make sure newly created
3325 * parent directories also end up in the log. A minimal inode and backref
3326 * only logging is done of any parent directories that are older than
3327 * the last committed transaction
3328 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003329int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3330 struct btrfs_root *root, struct inode *inode,
3331 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003332{
Chris Mason12fcfd22009-03-24 10:24:20 -04003333 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003334 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003335 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003336 int ret = 0;
3337 u64 last_committed = root->fs_info->last_trans_committed;
3338
3339 sb = inode->i_sb;
3340
Sage Weil3a5e1402009-04-02 16:49:40 -04003341 if (btrfs_test_opt(root, NOTREELOG)) {
3342 ret = 1;
3343 goto end_no_trans;
3344 }
3345
Chris Mason12fcfd22009-03-24 10:24:20 -04003346 if (root->fs_info->last_trans_log_full_commit >
3347 root->fs_info->last_trans_committed) {
3348 ret = 1;
3349 goto end_no_trans;
3350 }
3351
Yan, Zheng76dda932009-09-21 16:00:26 -04003352 if (root != BTRFS_I(inode)->root ||
3353 btrfs_root_refs(&root->root_item) == 0) {
3354 ret = 1;
3355 goto end_no_trans;
3356 }
3357
Chris Mason12fcfd22009-03-24 10:24:20 -04003358 ret = check_parent_dirs_for_sync(trans, inode, parent,
3359 sb, last_committed);
3360 if (ret)
3361 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003362
Josef Bacik22ee6982012-05-29 16:57:49 -04003363 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003364 ret = BTRFS_NO_LOG_SYNC;
3365 goto end_no_trans;
3366 }
3367
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003368 ret = start_log_trans(trans, root);
3369 if (ret)
3370 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003371
3372 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003373 if (ret)
3374 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003375
Chris Masonaf4176b2009-03-24 10:24:31 -04003376 /*
3377 * for regular files, if its inode is already on disk, we don't
3378 * have to worry about the parents at all. This is because
3379 * we can use the last_unlink_trans field to record renames
3380 * and other fun in this file.
3381 */
3382 if (S_ISREG(inode->i_mode) &&
3383 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003384 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3385 ret = 0;
3386 goto end_trans;
3387 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003388
3389 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003390 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003391 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003392 break;
3393
Chris Mason12fcfd22009-03-24 10:24:20 -04003394 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003395 if (root != BTRFS_I(inode)->root)
3396 break;
3397
Chris Mason12fcfd22009-03-24 10:24:20 -04003398 if (BTRFS_I(inode)->generation >
3399 root->fs_info->last_trans_committed) {
3400 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003401 if (ret)
3402 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003403 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003404 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003405 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003406
Josef Bacik6a912212010-11-20 09:48:00 +00003407 parent = dget_parent(parent);
3408 dput(old_parent);
3409 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003410 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003411 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003412end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003413 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003414 if (ret < 0) {
Josef Bacik0fa83cd2012-08-24 14:48:11 -04003415 WARN_ON(ret != -ENOSPC);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003416 root->fs_info->last_trans_log_full_commit = trans->transid;
3417 ret = 1;
3418 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003419 btrfs_end_log_trans(root);
3420end_no_trans:
3421 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003422}
3423
3424/*
3425 * it is not safe to log dentry if the chunk root has added new
3426 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3427 * If this returns 1, you must commit the transaction to safely get your
3428 * data on disk.
3429 */
3430int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3431 struct btrfs_root *root, struct dentry *dentry)
3432{
Josef Bacik6a912212010-11-20 09:48:00 +00003433 struct dentry *parent = dget_parent(dentry);
3434 int ret;
3435
3436 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3437 dput(parent);
3438
3439 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003440}
3441
3442/*
3443 * should be called during mount to recover any replay any log trees
3444 * from the FS
3445 */
3446int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3447{
3448 int ret;
3449 struct btrfs_path *path;
3450 struct btrfs_trans_handle *trans;
3451 struct btrfs_key key;
3452 struct btrfs_key found_key;
3453 struct btrfs_key tmp_key;
3454 struct btrfs_root *log;
3455 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3456 struct walk_control wc = {
3457 .process_func = process_one_buffer,
3458 .stage = 0,
3459 };
3460
Chris Masone02119d2008-09-05 16:13:11 -04003461 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003462 if (!path)
3463 return -ENOMEM;
3464
3465 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003466
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003467 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003468 if (IS_ERR(trans)) {
3469 ret = PTR_ERR(trans);
3470 goto error;
3471 }
Chris Masone02119d2008-09-05 16:13:11 -04003472
3473 wc.trans = trans;
3474 wc.pin = 1;
3475
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003476 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003477 if (ret) {
3478 btrfs_error(fs_info, ret, "Failed to pin buffers while "
3479 "recovering log root tree.");
3480 goto error;
3481 }
Chris Masone02119d2008-09-05 16:13:11 -04003482
3483again:
3484 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3485 key.offset = (u64)-1;
3486 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3487
Chris Masond3977122009-01-05 21:25:51 -05003488 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003489 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003490
3491 if (ret < 0) {
3492 btrfs_error(fs_info, ret,
3493 "Couldn't find tree log root.");
3494 goto error;
3495 }
Chris Masone02119d2008-09-05 16:13:11 -04003496 if (ret > 0) {
3497 if (path->slots[0] == 0)
3498 break;
3499 path->slots[0]--;
3500 }
3501 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3502 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003503 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003504 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3505 break;
3506
3507 log = btrfs_read_fs_root_no_radix(log_root_tree,
3508 &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003509 if (IS_ERR(log)) {
3510 ret = PTR_ERR(log);
3511 btrfs_error(fs_info, ret,
3512 "Couldn't read tree log root.");
3513 goto error;
3514 }
Chris Masone02119d2008-09-05 16:13:11 -04003515
3516 tmp_key.objectid = found_key.offset;
3517 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3518 tmp_key.offset = (u64)-1;
3519
3520 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003521 if (IS_ERR(wc.replay_dest)) {
3522 ret = PTR_ERR(wc.replay_dest);
3523 btrfs_error(fs_info, ret, "Couldn't read target root "
3524 "for tree log recovery.");
3525 goto error;
3526 }
Chris Masone02119d2008-09-05 16:13:11 -04003527
Yan Zheng07d400a2009-01-06 11:42:00 -05003528 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003529 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003530 ret = walk_log_tree(trans, log, &wc);
3531 BUG_ON(ret);
3532
3533 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3534 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3535 path);
3536 BUG_ON(ret);
3537 }
Chris Masone02119d2008-09-05 16:13:11 -04003538
3539 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003540 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003541 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003542 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003543 kfree(log);
3544
3545 if (found_key.offset == 0)
3546 break;
3547 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003548 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003549
3550 /* step one is to pin it all, step two is to replay just inodes */
3551 if (wc.pin) {
3552 wc.pin = 0;
3553 wc.process_func = replay_one_buffer;
3554 wc.stage = LOG_WALK_REPLAY_INODES;
3555 goto again;
3556 }
3557 /* step three is to replay everything */
3558 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3559 wc.stage++;
3560 goto again;
3561 }
3562
3563 btrfs_free_path(path);
3564
3565 free_extent_buffer(log_root_tree->node);
3566 log_root_tree->log_root = NULL;
3567 fs_info->log_root_recovering = 0;
3568
3569 /* step 4: commit the transaction, which also unpins the blocks */
3570 btrfs_commit_transaction(trans, fs_info->tree_root);
3571
3572 kfree(log_root_tree);
3573 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003574
3575error:
3576 btrfs_free_path(path);
3577 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003578}
Chris Mason12fcfd22009-03-24 10:24:20 -04003579
3580/*
3581 * there are some corner cases where we want to force a full
3582 * commit instead of allowing a directory to be logged.
3583 *
3584 * They revolve around files there were unlinked from the directory, and
3585 * this function updates the parent directory so that a full commit is
3586 * properly done if it is fsync'd later after the unlinks are done.
3587 */
3588void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3589 struct inode *dir, struct inode *inode,
3590 int for_rename)
3591{
3592 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003593 * when we're logging a file, if it hasn't been renamed
3594 * or unlinked, and its inode is fully committed on disk,
3595 * we don't have to worry about walking up the directory chain
3596 * to log its parents.
3597 *
3598 * So, we use the last_unlink_trans field to put this transid
3599 * into the file. When the file is logged we check it and
3600 * don't log the parents if the file is fully on disk.
3601 */
3602 if (S_ISREG(inode->i_mode))
3603 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3604
3605 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003606 * if this directory was already logged any new
3607 * names for this file/dir will get recorded
3608 */
3609 smp_mb();
3610 if (BTRFS_I(dir)->logged_trans == trans->transid)
3611 return;
3612
3613 /*
3614 * if the inode we're about to unlink was logged,
3615 * the log will be properly updated for any new names
3616 */
3617 if (BTRFS_I(inode)->logged_trans == trans->transid)
3618 return;
3619
3620 /*
3621 * when renaming files across directories, if the directory
3622 * there we're unlinking from gets fsync'd later on, there's
3623 * no way to find the destination directory later and fsync it
3624 * properly. So, we have to be conservative and force commits
3625 * so the new name gets discovered.
3626 */
3627 if (for_rename)
3628 goto record;
3629
3630 /* we can safely do the unlink without any special recording */
3631 return;
3632
3633record:
3634 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3635}
3636
3637/*
3638 * Call this after adding a new name for a file and it will properly
3639 * update the log to reflect the new name.
3640 *
3641 * It will return zero if all goes well, and it will return 1 if a
3642 * full transaction commit is required.
3643 */
3644int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3645 struct inode *inode, struct inode *old_dir,
3646 struct dentry *parent)
3647{
3648 struct btrfs_root * root = BTRFS_I(inode)->root;
3649
3650 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003651 * this will force the logging code to walk the dentry chain
3652 * up for the file
3653 */
3654 if (S_ISREG(inode->i_mode))
3655 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3656
3657 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003658 * if this inode hasn't been logged and directory we're renaming it
3659 * from hasn't been logged, we don't need to log it
3660 */
3661 if (BTRFS_I(inode)->logged_trans <=
3662 root->fs_info->last_trans_committed &&
3663 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3664 root->fs_info->last_trans_committed))
3665 return 0;
3666
3667 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3668}
3669