blob: 46b7b57650ab8aba7731a9a639944750966e08a4 [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>
Chris Masone02119d2008-09-05 16:13:11 -040021#include "ctree.h"
22#include "transaction.h"
23#include "disk-io.h"
24#include "locking.h"
25#include "print-tree.h"
26#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050027#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040028
29/* magic values for the inode_only field in btrfs_log_inode:
30 *
31 * LOG_INODE_ALL means to log everything
32 * LOG_INODE_EXISTS means to log just enough to recreate the inode
33 * during log replay
34 */
35#define LOG_INODE_ALL 0
36#define LOG_INODE_EXISTS 1
37
38/*
Chris Mason12fcfd22009-03-24 10:24:20 -040039 * directory trouble cases
40 *
41 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
42 * log, we must force a full commit before doing an fsync of the directory
43 * where the unlink was done.
44 * ---> record transid of last unlink/rename per directory
45 *
46 * mkdir foo/some_dir
47 * normal commit
48 * rename foo/some_dir foo2/some_dir
49 * mkdir foo/some_dir
50 * fsync foo/some_dir/some_file
51 *
52 * The fsync above will unlink the original some_dir without recording
53 * it in its new location (foo2). After a crash, some_dir will be gone
54 * unless the fsync of some_file forces a full commit
55 *
56 * 2) we must log any new names for any file or dir that is in the fsync
57 * log. ---> check inode while renaming/linking.
58 *
59 * 2a) we must log any new names for any file or dir during rename
60 * when the directory they are being removed from was logged.
61 * ---> check inode and old parent dir during rename
62 *
63 * 2a is actually the more important variant. With the extra logging
64 * a crash might unlink the old name without recreating the new one
65 *
66 * 3) after a crash, we must go through any directories with a link count
67 * of zero and redo the rm -rf
68 *
69 * mkdir f1/foo
70 * normal commit
71 * rm -rf f1/foo
72 * fsync(f1)
73 *
74 * The directory f1 was fully removed from the FS, but fsync was never
75 * called on f1, only its parent dir. After a crash the rm -rf must
76 * be replayed. This must be able to recurse down the entire
77 * directory tree. The inode link count fixup code takes care of the
78 * ugly details.
79 */
80
81/*
Chris Masone02119d2008-09-05 16:13:11 -040082 * stages for the tree walking. The first
83 * stage (0) is to only pin down the blocks we find
84 * the second stage (1) is to make sure that all the inodes
85 * we find in the log are created in the subvolume.
86 *
87 * The last stage is to deal with directories and links and extents
88 * and all the other fun semantics
89 */
90#define LOG_WALK_PIN_ONLY 0
91#define LOG_WALK_REPLAY_INODES 1
92#define LOG_WALK_REPLAY_ALL 2
93
Chris Mason12fcfd22009-03-24 10:24:20 -040094static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040095 struct btrfs_root *root, struct inode *inode,
96 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -050097static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400100static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_root *log,
103 struct btrfs_path *path,
104 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400105
106/*
107 * tree logging is a special write ahead log used to make sure that
108 * fsyncs and O_SYNCs can happen without doing full tree commits.
109 *
110 * Full tree commits are expensive because they require commonly
111 * modified blocks to be recowed, creating many dirty pages in the
112 * extent tree an 4x-6x higher write load than ext3.
113 *
114 * Instead of doing a tree commit on every fsync, we use the
115 * key ranges and transaction ids to find items for a given file or directory
116 * that have changed in this transaction. Those items are copied into
117 * a special tree (one per subvolume root), that tree is written to disk
118 * and then the fsync is considered complete.
119 *
120 * After a crash, items are copied out of the log-tree back into the
121 * subvolume tree. Any file data extents found are recorded in the extent
122 * allocation tree, and the log-tree freed.
123 *
124 * The log tree is read three times, once to pin down all the extents it is
125 * using in ram and once, once to create all the inodes logged in the tree
126 * and once to do all the other items.
127 */
128
129/*
Chris Masone02119d2008-09-05 16:13:11 -0400130 * start a sub transaction and setup the log tree
131 * this increments the log tree writer count to make the people
132 * syncing the tree wait for us to finish
133 */
134static int start_log_trans(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root)
136{
137 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400138 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500139
140 mutex_lock(&root->log_mutex);
141 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400142 if (!root->log_start_pid) {
143 root->log_start_pid = current->pid;
144 root->log_multiple_pids = false;
145 } else if (root->log_start_pid != current->pid) {
146 root->log_multiple_pids = true;
147 }
148
Yan Zheng7237f182009-01-21 12:54:03 -0500149 root->log_batch++;
150 atomic_inc(&root->log_writers);
151 mutex_unlock(&root->log_mutex);
152 return 0;
153 }
Josef Bacikff782e02009-10-08 15:30:04 -0400154 root->log_multiple_pids = false;
155 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400156 mutex_lock(&root->fs_info->tree_log_mutex);
157 if (!root->fs_info->log_root_tree) {
158 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400159 if (ret)
160 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400161 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400162 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400163 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400164 if (ret)
165 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Chris Masone02119d2008-09-05 16:13:11 -0400167 mutex_unlock(&root->fs_info->tree_log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -0500168 root->log_batch++;
169 atomic_inc(&root->log_writers);
170 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400171 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400172}
173
174/*
175 * returns 0 if there was a log transaction running and we were able
176 * to join, or returns -ENOENT if there were not transactions
177 * in progress
178 */
179static int join_running_log_trans(struct btrfs_root *root)
180{
181 int ret = -ENOENT;
182
183 smp_mb();
184 if (!root->log_root)
185 return -ENOENT;
186
Yan Zheng7237f182009-01-21 12:54:03 -0500187 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400188 if (root->log_root) {
189 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500190 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400191 }
Yan Zheng7237f182009-01-21 12:54:03 -0500192 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400193 return ret;
194}
195
196/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400197 * This either makes the current running log transaction wait
198 * until you call btrfs_end_log_trans() or it makes any future
199 * log transactions wait until you call btrfs_end_log_trans()
200 */
201int btrfs_pin_log_trans(struct btrfs_root *root)
202{
203 int ret = -ENOENT;
204
205 mutex_lock(&root->log_mutex);
206 atomic_inc(&root->log_writers);
207 mutex_unlock(&root->log_mutex);
208 return ret;
209}
210
211/*
Chris Masone02119d2008-09-05 16:13:11 -0400212 * indicate we're done making changes to the log tree
213 * and wake up anyone waiting to do a sync
214 */
Chris Mason12fcfd22009-03-24 10:24:20 -0400215int btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400216{
Yan Zheng7237f182009-01-21 12:54:03 -0500217 if (atomic_dec_and_test(&root->log_writers)) {
218 smp_mb();
219 if (waitqueue_active(&root->log_writer_wait))
220 wake_up(&root->log_writer_wait);
221 }
Chris Masone02119d2008-09-05 16:13:11 -0400222 return 0;
223}
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)
Yan Zheng11833d62009-09-11 16:11:19 -0400279 btrfs_pin_extent(log->fs_info->extent_root,
280 eb->start, eb->len, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400281
282 if (btrfs_buffer_uptodate(eb, gen)) {
283 if (wc->write)
284 btrfs_write_tree_block(eb);
285 if (wc->wait)
286 btrfs_wait_tree_block_writeback(eb);
287 }
288 return 0;
289}
290
291/*
292 * Item overwrite used by replay and tree logging. eb, slot and key all refer
293 * to the src data we are copying out.
294 *
295 * root is the tree we are copying into, and path is a scratch
296 * path for use in this function (it should be released on entry and
297 * will be released on exit).
298 *
299 * If the key is already in the destination tree the existing item is
300 * overwritten. If the existing item isn't big enough, it is extended.
301 * If it is too large, it is truncated.
302 *
303 * If the key isn't in the destination yet, a new item is inserted.
304 */
305static noinline int overwrite_item(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root,
307 struct btrfs_path *path,
308 struct extent_buffer *eb, int slot,
309 struct btrfs_key *key)
310{
311 int ret;
312 u32 item_size;
313 u64 saved_i_size = 0;
314 int save_old_i_size = 0;
315 unsigned long src_ptr;
316 unsigned long dst_ptr;
317 int overwrite_root = 0;
318
319 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
320 overwrite_root = 1;
321
322 item_size = btrfs_item_size_nr(eb, slot);
323 src_ptr = btrfs_item_ptr_offset(eb, slot);
324
325 /* look for the key in the destination tree */
326 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
327 if (ret == 0) {
328 char *src_copy;
329 char *dst_copy;
330 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
331 path->slots[0]);
332 if (dst_size != item_size)
333 goto insert;
334
335 if (item_size == 0) {
336 btrfs_release_path(root, path);
337 return 0;
338 }
339 dst_copy = kmalloc(item_size, GFP_NOFS);
340 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000341 if (!dst_copy || !src_copy) {
342 btrfs_release_path(root, path);
343 kfree(dst_copy);
344 kfree(src_copy);
345 return -ENOMEM;
346 }
Chris Masone02119d2008-09-05 16:13:11 -0400347
348 read_extent_buffer(eb, src_copy, src_ptr, item_size);
349
350 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
351 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
352 item_size);
353 ret = memcmp(dst_copy, src_copy, item_size);
354
355 kfree(dst_copy);
356 kfree(src_copy);
357 /*
358 * they have the same contents, just return, this saves
359 * us from cowing blocks in the destination tree and doing
360 * extra writes that may not have been done by a previous
361 * sync
362 */
363 if (ret == 0) {
364 btrfs_release_path(root, path);
365 return 0;
366 }
367
368 }
369insert:
370 btrfs_release_path(root, path);
371 /* try to insert the key into the destination tree */
372 ret = btrfs_insert_empty_item(trans, root, path,
373 key, item_size);
374
375 /* make sure any existing item is the correct size */
376 if (ret == -EEXIST) {
377 u32 found_size;
378 found_size = btrfs_item_size_nr(path->nodes[0],
379 path->slots[0]);
380 if (found_size > item_size) {
381 btrfs_truncate_item(trans, root, path, item_size, 1);
382 } else if (found_size < item_size) {
Yan Zheng87b29b22008-12-17 10:21:48 -0500383 ret = btrfs_extend_item(trans, root, path,
384 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400385 }
386 } 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]);
440 btrfs_release_path(root, path);
441 return 0;
442}
443
444/*
445 * simple helper to read an inode off the disk from a given root
446 * This can only be called for subvolume roots and not for the log
447 */
448static noinline struct inode *read_one_inode(struct btrfs_root *root,
449 u64 objectid)
450{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400451 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400452 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400453
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400454 key.objectid = objectid;
455 key.type = BTRFS_INODE_ITEM_KEY;
456 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000457 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400458 if (IS_ERR(inode)) {
459 inode = NULL;
460 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400461 iput(inode);
462 inode = NULL;
463 }
464 return inode;
465}
466
467/* replays a single extent in 'eb' at 'slot' with 'key' into the
468 * subvolume 'root'. path is released on entry and should be released
469 * on exit.
470 *
471 * extents in the log tree have not been allocated out of the extent
472 * tree yet. So, this completes the allocation, taking a reference
473 * as required if the extent already exists or creating a new extent
474 * if it isn't in the extent allocation tree yet.
475 *
476 * The extent is inserted into the file, dropping any existing extents
477 * from the file that overlap the new one.
478 */
479static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root,
481 struct btrfs_path *path,
482 struct extent_buffer *eb, int slot,
483 struct btrfs_key *key)
484{
485 int found_type;
486 u64 mask = root->sectorsize - 1;
487 u64 extent_end;
488 u64 alloc_hint;
489 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500490 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400491 struct btrfs_file_extent_item *item;
492 struct inode *inode = NULL;
493 unsigned long size;
494 int ret = 0;
495
496 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
497 found_type = btrfs_file_extent_type(eb, item);
498
Yan Zhengd899e052008-10-30 14:25:28 -0400499 if (found_type == BTRFS_FILE_EXTENT_REG ||
500 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400501 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
502 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400504 extent_end = (start + size + mask) & ~mask;
505 } else {
506 ret = 0;
507 goto out;
508 }
509
510 inode = read_one_inode(root, key->objectid);
511 if (!inode) {
512 ret = -EIO;
513 goto out;
514 }
515
516 /*
517 * first check to see if we already have this extent in the
518 * file. This must be done before the btrfs_drop_extents run
519 * so we don't try to drop this extent.
520 */
521 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
522 start, 0);
523
Yan Zhengd899e052008-10-30 14:25:28 -0400524 if (ret == 0 &&
525 (found_type == BTRFS_FILE_EXTENT_REG ||
526 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400527 struct btrfs_file_extent_item cmp1;
528 struct btrfs_file_extent_item cmp2;
529 struct btrfs_file_extent_item *existing;
530 struct extent_buffer *leaf;
531
532 leaf = path->nodes[0];
533 existing = btrfs_item_ptr(leaf, path->slots[0],
534 struct btrfs_file_extent_item);
535
536 read_extent_buffer(eb, &cmp1, (unsigned long)item,
537 sizeof(cmp1));
538 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
539 sizeof(cmp2));
540
541 /*
542 * we already have a pointer to this exact extent,
543 * we don't have to do anything
544 */
545 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
546 btrfs_release_path(root, path);
547 goto out;
548 }
549 }
550 btrfs_release_path(root, path);
551
Yan Zheng07d400a2009-01-06 11:42:00 -0500552 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400553 /* drop any overlapping extents */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000554 ret = btrfs_drop_extents(trans, inode, start, extent_end,
555 &alloc_hint, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400556 BUG_ON(ret);
557
Yan Zheng07d400a2009-01-06 11:42:00 -0500558 if (found_type == BTRFS_FILE_EXTENT_REG ||
559 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500561 unsigned long dest_offset;
562 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400563
Yan Zheng07d400a2009-01-06 11:42:00 -0500564 ret = btrfs_insert_empty_item(trans, root, path, key,
565 sizeof(*item));
566 BUG_ON(ret);
567 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
568 path->slots[0]);
569 copy_extent_buffer(path->nodes[0], eb, dest_offset,
570 (unsigned long)item, sizeof(*item));
571
572 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
573 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
574 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400575 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500576
577 if (ins.objectid > 0) {
578 u64 csum_start;
579 u64 csum_end;
580 LIST_HEAD(ordered_sums);
581 /*
582 * is this extent already allocated in the extent
583 * allocation tree? If so, just add a reference
584 */
585 ret = btrfs_lookup_extent(root, ins.objectid,
586 ins.offset);
587 if (ret == 0) {
588 ret = btrfs_inc_extent_ref(trans, root,
589 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 0, root->root_key.objectid,
591 key->objectid, offset);
Yan Zheng07d400a2009-01-06 11:42:00 -0500592 } else {
593 /*
594 * insert the extent pointer in the extent
595 * allocation tree
596 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400597 ret = btrfs_alloc_logged_file_extent(trans,
598 root, root->root_key.objectid,
599 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500600 BUG_ON(ret);
601 }
602 btrfs_release_path(root, path);
603
604 if (btrfs_file_extent_compression(eb, item)) {
605 csum_start = ins.objectid;
606 csum_end = csum_start + ins.offset;
607 } else {
608 csum_start = ins.objectid +
609 btrfs_file_extent_offset(eb, item);
610 csum_end = csum_start +
611 btrfs_file_extent_num_bytes(eb, item);
612 }
613
614 ret = btrfs_lookup_csums_range(root->log_root,
615 csum_start, csum_end - 1,
616 &ordered_sums);
617 BUG_ON(ret);
618 while (!list_empty(&ordered_sums)) {
619 struct btrfs_ordered_sum *sums;
620 sums = list_entry(ordered_sums.next,
621 struct btrfs_ordered_sum,
622 list);
623 ret = btrfs_csum_file_blocks(trans,
624 root->fs_info->csum_root,
625 sums);
626 BUG_ON(ret);
627 list_del(&sums->list);
628 kfree(sums);
629 }
630 } else {
631 btrfs_release_path(root, path);
632 }
633 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
634 /* inline extents are easy, we just overwrite them */
635 ret = overwrite_item(trans, root, path, eb, slot, key);
636 BUG_ON(ret);
637 }
638
639 inode_set_bytes(inode, saved_nbytes);
Chris Masone02119d2008-09-05 16:13:11 -0400640 btrfs_update_inode(trans, root, inode);
641out:
642 if (inode)
643 iput(inode);
644 return ret;
645}
646
647/*
648 * when cleaning up conflicts between the directory names in the
649 * subvolume, directory names in the log and directory names in the
650 * inode back references, we may have to unlink inodes from directories.
651 *
652 * This is a helper function to do the unlink of a specific directory
653 * item
654 */
655static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
656 struct btrfs_root *root,
657 struct btrfs_path *path,
658 struct inode *dir,
659 struct btrfs_dir_item *di)
660{
661 struct inode *inode;
662 char *name;
663 int name_len;
664 struct extent_buffer *leaf;
665 struct btrfs_key location;
666 int ret;
667
668 leaf = path->nodes[0];
669
670 btrfs_dir_item_key_to_cpu(leaf, di, &location);
671 name_len = btrfs_dir_name_len(leaf, di);
672 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000673 if (!name)
674 return -ENOMEM;
675
Chris Masone02119d2008-09-05 16:13:11 -0400676 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
677 btrfs_release_path(root, path);
678
679 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000680 if (!inode) {
681 kfree(name);
682 return -EIO;
683 }
Chris Masone02119d2008-09-05 16:13:11 -0400684
Yan Zhengec051c02009-01-05 15:43:42 -0500685 ret = link_to_fixup_dir(trans, root, path, location.objectid);
686 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400687
Chris Masone02119d2008-09-05 16:13:11 -0400688 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500689 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400690 kfree(name);
691
692 iput(inode);
693 return ret;
694}
695
696/*
697 * helper function to see if a given name and sequence number found
698 * in an inode back reference are already in a directory and correctly
699 * point to this inode
700 */
701static noinline int inode_in_dir(struct btrfs_root *root,
702 struct btrfs_path *path,
703 u64 dirid, u64 objectid, u64 index,
704 const char *name, int name_len)
705{
706 struct btrfs_dir_item *di;
707 struct btrfs_key location;
708 int match = 0;
709
710 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
711 index, name, name_len, 0);
712 if (di && !IS_ERR(di)) {
713 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
714 if (location.objectid != objectid)
715 goto out;
716 } else
717 goto out;
718 btrfs_release_path(root, path);
719
720 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
721 if (di && !IS_ERR(di)) {
722 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
723 if (location.objectid != objectid)
724 goto out;
725 } else
726 goto out;
727 match = 1;
728out:
729 btrfs_release_path(root, path);
730 return match;
731}
732
733/*
734 * helper function to check a log tree for a named back reference in
735 * an inode. This is used to decide if a back reference that is
736 * found in the subvolume conflicts with what we find in the log.
737 *
738 * inode backreferences may have multiple refs in a single item,
739 * during replay we process one reference at a time, and we don't
740 * want to delete valid links to a file from the subvolume if that
741 * link is also in the log.
742 */
743static noinline int backref_in_log(struct btrfs_root *log,
744 struct btrfs_key *key,
745 char *name, int namelen)
746{
747 struct btrfs_path *path;
748 struct btrfs_inode_ref *ref;
749 unsigned long ptr;
750 unsigned long ptr_end;
751 unsigned long name_ptr;
752 int found_name_len;
753 int item_size;
754 int ret;
755 int match = 0;
756
757 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000758 if (!path)
759 return -ENOMEM;
760
Chris Masone02119d2008-09-05 16:13:11 -0400761 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
762 if (ret != 0)
763 goto out;
764
765 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
766 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
767 ptr_end = ptr + item_size;
768 while (ptr < ptr_end) {
769 ref = (struct btrfs_inode_ref *)ptr;
770 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
771 if (found_name_len == namelen) {
772 name_ptr = (unsigned long)(ref + 1);
773 ret = memcmp_extent_buffer(path->nodes[0], name,
774 name_ptr, namelen);
775 if (ret == 0) {
776 match = 1;
777 goto out;
778 }
779 }
780 ptr = (unsigned long)(ref + 1) + found_name_len;
781 }
782out:
783 btrfs_free_path(path);
784 return match;
785}
786
787
788/*
789 * replay one inode back reference item found in the log tree.
790 * eb, slot and key refer to the buffer and key found in the log tree.
791 * root is the destination we are replaying into, and path is for temp
792 * use by this function. (it should be released on return).
793 */
794static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
795 struct btrfs_root *root,
796 struct btrfs_root *log,
797 struct btrfs_path *path,
798 struct extent_buffer *eb, int slot,
799 struct btrfs_key *key)
800{
801 struct inode *dir;
802 int ret;
Chris Masone02119d2008-09-05 16:13:11 -0400803 struct btrfs_inode_ref *ref;
Chris Masone02119d2008-09-05 16:13:11 -0400804 struct inode *inode;
805 char *name;
806 int namelen;
807 unsigned long ref_ptr;
808 unsigned long ref_end;
liuboc622ae62011-03-26 08:01:12 -0400809 int search_done = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400810
Chris Masone02119d2008-09-05 16:13:11 -0400811 /*
812 * it is possible that we didn't log all the parent directories
813 * for a given inode. If we don't find the dir, just don't
814 * copy the back ref in. The link count fixup code will take
815 * care of the rest
816 */
817 dir = read_one_inode(root, key->offset);
818 if (!dir)
819 return -ENOENT;
820
821 inode = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000822 if (!inode) {
823 iput(dir);
824 return -EIO;
825 }
Chris Masone02119d2008-09-05 16:13:11 -0400826
827 ref_ptr = btrfs_item_ptr_offset(eb, slot);
828 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
829
830again:
831 ref = (struct btrfs_inode_ref *)ref_ptr;
832
833 namelen = btrfs_inode_ref_name_len(eb, ref);
834 name = kmalloc(namelen, GFP_NOFS);
835 BUG_ON(!name);
836
837 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
838
839 /* if we already have a perfect match, we're done */
840 if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
841 btrfs_inode_ref_index(eb, ref),
842 name, namelen)) {
843 goto out;
844 }
845
846 /*
847 * look for a conflicting back reference in the metadata.
848 * if we find one we have to unlink that name of the file
849 * before we add our new link. Later on, we overwrite any
850 * existing back reference, and we don't want to create
851 * dangling pointers in the directory.
852 */
liuboc622ae62011-03-26 08:01:12 -0400853
854 if (search_done)
855 goto insert;
856
Chris Masone02119d2008-09-05 16:13:11 -0400857 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
858 if (ret == 0) {
859 char *victim_name;
860 int victim_name_len;
861 struct btrfs_inode_ref *victim_ref;
862 unsigned long ptr;
863 unsigned long ptr_end;
864 struct extent_buffer *leaf = path->nodes[0];
865
866 /* are we trying to overwrite a back ref for the root directory
867 * if so, just jump out, we're done
868 */
869 if (key->objectid == key->offset)
870 goto out_nowrite;
871
872 /* check all the names in this back reference to see
873 * if they are in the log. if so, we allow them to stay
874 * otherwise they must be unlinked as a conflict
875 */
876 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
877 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500878 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400879 victim_ref = (struct btrfs_inode_ref *)ptr;
880 victim_name_len = btrfs_inode_ref_name_len(leaf,
881 victim_ref);
882 victim_name = kmalloc(victim_name_len, GFP_NOFS);
883 BUG_ON(!victim_name);
884
885 read_extent_buffer(leaf, victim_name,
886 (unsigned long)(victim_ref + 1),
887 victim_name_len);
888
889 if (!backref_in_log(log, key, victim_name,
890 victim_name_len)) {
891 btrfs_inc_nlink(inode);
892 btrfs_release_path(root, path);
Chris Mason12fcfd22009-03-24 10:24:20 -0400893
Chris Masone02119d2008-09-05 16:13:11 -0400894 ret = btrfs_unlink_inode(trans, root, dir,
895 inode, victim_name,
896 victim_name_len);
Chris Masone02119d2008-09-05 16:13:11 -0400897 }
898 kfree(victim_name);
899 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
900 }
901 BUG_ON(ret);
liuboc622ae62011-03-26 08:01:12 -0400902
903 /*
904 * NOTE: we have searched root tree and checked the
905 * coresponding ref, it does not need to check again.
906 */
907 search_done = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400908 }
909 btrfs_release_path(root, path);
910
liuboc622ae62011-03-26 08:01:12 -0400911insert:
Chris Masone02119d2008-09-05 16:13:11 -0400912 /* insert our name */
913 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
914 btrfs_inode_ref_index(eb, ref));
915 BUG_ON(ret);
916
917 btrfs_update_inode(trans, root, inode);
918
919out:
920 ref_ptr = (unsigned long)(ref + 1) + namelen;
921 kfree(name);
922 if (ref_ptr < ref_end)
923 goto again;
924
925 /* finally write the back reference in the inode */
926 ret = overwrite_item(trans, root, path, eb, slot, key);
927 BUG_ON(ret);
928
929out_nowrite:
930 btrfs_release_path(root, path);
931 iput(dir);
932 iput(inode);
933 return 0;
934}
935
Yan, Zhengc71bf092009-11-12 09:34:40 +0000936static int insert_orphan_item(struct btrfs_trans_handle *trans,
937 struct btrfs_root *root, u64 offset)
938{
939 int ret;
940 ret = btrfs_find_orphan_item(root, offset);
941 if (ret > 0)
942 ret = btrfs_insert_orphan_item(trans, root, offset);
943 return ret;
944}
945
946
Chris Masone02119d2008-09-05 16:13:11 -0400947/*
Chris Masone02119d2008-09-05 16:13:11 -0400948 * There are a few corners where the link count of the file can't
949 * be properly maintained during replay. So, instead of adding
950 * lots of complexity to the log code, we just scan the backrefs
951 * for any file that has been through replay.
952 *
953 * The scan will update the link count on the inode to reflect the
954 * number of back refs found. If it goes down to zero, the iput
955 * will free the inode.
956 */
957static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
958 struct btrfs_root *root,
959 struct inode *inode)
960{
961 struct btrfs_path *path;
962 int ret;
963 struct btrfs_key key;
964 u64 nlink = 0;
965 unsigned long ptr;
966 unsigned long ptr_end;
967 int name_len;
968
969 key.objectid = inode->i_ino;
970 key.type = BTRFS_INODE_REF_KEY;
971 key.offset = (u64)-1;
972
973 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000974 if (!path)
975 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -0400976
Chris Masond3977122009-01-05 21:25:51 -0500977 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -0400978 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
979 if (ret < 0)
980 break;
981 if (ret > 0) {
982 if (path->slots[0] == 0)
983 break;
984 path->slots[0]--;
985 }
986 btrfs_item_key_to_cpu(path->nodes[0], &key,
987 path->slots[0]);
988 if (key.objectid != inode->i_ino ||
989 key.type != BTRFS_INODE_REF_KEY)
990 break;
991 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
992 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
993 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500994 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400995 struct btrfs_inode_ref *ref;
996
997 ref = (struct btrfs_inode_ref *)ptr;
998 name_len = btrfs_inode_ref_name_len(path->nodes[0],
999 ref);
1000 ptr = (unsigned long)(ref + 1) + name_len;
1001 nlink++;
1002 }
1003
1004 if (key.offset == 0)
1005 break;
1006 key.offset--;
1007 btrfs_release_path(root, path);
1008 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001009 btrfs_release_path(root, path);
Chris Masone02119d2008-09-05 16:13:11 -04001010 if (nlink != inode->i_nlink) {
1011 inode->i_nlink = nlink;
1012 btrfs_update_inode(trans, root, inode);
1013 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001014 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001015
Yan, Zhengc71bf092009-11-12 09:34:40 +00001016 if (inode->i_nlink == 0) {
1017 if (S_ISDIR(inode->i_mode)) {
1018 ret = replay_dir_deletes(trans, root, NULL, path,
1019 inode->i_ino, 1);
1020 BUG_ON(ret);
1021 }
1022 ret = insert_orphan_item(trans, root, inode->i_ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001023 BUG_ON(ret);
1024 }
1025 btrfs_free_path(path);
1026
Chris Masone02119d2008-09-05 16:13:11 -04001027 return 0;
1028}
1029
1030static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1031 struct btrfs_root *root,
1032 struct btrfs_path *path)
1033{
1034 int ret;
1035 struct btrfs_key key;
1036 struct inode *inode;
1037
1038 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1039 key.type = BTRFS_ORPHAN_ITEM_KEY;
1040 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001041 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001042 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1043 if (ret < 0)
1044 break;
1045
1046 if (ret == 1) {
1047 if (path->slots[0] == 0)
1048 break;
1049 path->slots[0]--;
1050 }
1051
1052 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1053 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1054 key.type != BTRFS_ORPHAN_ITEM_KEY)
1055 break;
1056
1057 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001058 if (ret)
1059 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001060
1061 btrfs_release_path(root, path);
1062 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001063 if (!inode)
1064 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001065
1066 ret = fixup_inode_link_count(trans, root, inode);
1067 BUG_ON(ret);
1068
1069 iput(inode);
1070
Chris Mason12fcfd22009-03-24 10:24:20 -04001071 /*
1072 * fixup on a directory may create new entries,
1073 * make sure we always look for the highset possible
1074 * offset
1075 */
1076 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001077 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001078 ret = 0;
1079out:
Chris Masone02119d2008-09-05 16:13:11 -04001080 btrfs_release_path(root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001081 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001082}
1083
1084
1085/*
1086 * record a given inode in the fixup dir so we can check its link
1087 * count when replay is done. The link count is incremented here
1088 * so the inode won't go away until we check it
1089 */
1090static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1091 struct btrfs_root *root,
1092 struct btrfs_path *path,
1093 u64 objectid)
1094{
1095 struct btrfs_key key;
1096 int ret = 0;
1097 struct inode *inode;
1098
1099 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001100 if (!inode)
1101 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001102
1103 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1104 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1105 key.offset = objectid;
1106
1107 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1108
1109 btrfs_release_path(root, path);
1110 if (ret == 0) {
1111 btrfs_inc_nlink(inode);
1112 btrfs_update_inode(trans, root, inode);
1113 } else if (ret == -EEXIST) {
1114 ret = 0;
1115 } else {
1116 BUG();
1117 }
1118 iput(inode);
1119
1120 return ret;
1121}
1122
1123/*
1124 * when replaying the log for a directory, we only insert names
1125 * for inodes that actually exist. This means an fsync on a directory
1126 * does not implicitly fsync all the new files in it
1127 */
1128static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1129 struct btrfs_root *root,
1130 struct btrfs_path *path,
1131 u64 dirid, u64 index,
1132 char *name, int name_len, u8 type,
1133 struct btrfs_key *location)
1134{
1135 struct inode *inode;
1136 struct inode *dir;
1137 int ret;
1138
1139 inode = read_one_inode(root, location->objectid);
1140 if (!inode)
1141 return -ENOENT;
1142
1143 dir = read_one_inode(root, dirid);
1144 if (!dir) {
1145 iput(inode);
1146 return -EIO;
1147 }
1148 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1149
1150 /* FIXME, put inode into FIXUP list */
1151
1152 iput(inode);
1153 iput(dir);
1154 return ret;
1155}
1156
1157/*
1158 * take a single entry in a log directory item and replay it into
1159 * the subvolume.
1160 *
1161 * if a conflicting item exists in the subdirectory already,
1162 * the inode it points to is unlinked and put into the link count
1163 * fix up tree.
1164 *
1165 * If a name from the log points to a file or directory that does
1166 * not exist in the FS, it is skipped. fsyncs on directories
1167 * do not force down inodes inside that directory, just changes to the
1168 * names or unlinks in a directory.
1169 */
1170static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1171 struct btrfs_root *root,
1172 struct btrfs_path *path,
1173 struct extent_buffer *eb,
1174 struct btrfs_dir_item *di,
1175 struct btrfs_key *key)
1176{
1177 char *name;
1178 int name_len;
1179 struct btrfs_dir_item *dst_di;
1180 struct btrfs_key found_key;
1181 struct btrfs_key log_key;
1182 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001183 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001184 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001185 int ret;
1186
1187 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001188 if (!dir)
1189 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001190
1191 name_len = btrfs_dir_name_len(eb, di);
1192 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001193 if (!name)
1194 return -ENOMEM;
1195
Chris Masone02119d2008-09-05 16:13:11 -04001196 log_type = btrfs_dir_type(eb, di);
1197 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1198 name_len);
1199
1200 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001201 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1202 if (exists == 0)
1203 exists = 1;
1204 else
1205 exists = 0;
1206 btrfs_release_path(root, path);
1207
Chris Masone02119d2008-09-05 16:13:11 -04001208 if (key->type == BTRFS_DIR_ITEM_KEY) {
1209 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1210 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001211 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001212 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1213 key->objectid,
1214 key->offset, name,
1215 name_len, 1);
1216 } else {
1217 BUG();
1218 }
1219 if (!dst_di || IS_ERR(dst_di)) {
1220 /* we need a sequence number to insert, so we only
1221 * do inserts for the BTRFS_DIR_INDEX_KEY types
1222 */
1223 if (key->type != BTRFS_DIR_INDEX_KEY)
1224 goto out;
1225 goto insert;
1226 }
1227
1228 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1229 /* the existing item matches the logged item */
1230 if (found_key.objectid == log_key.objectid &&
1231 found_key.type == log_key.type &&
1232 found_key.offset == log_key.offset &&
1233 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1234 goto out;
1235 }
1236
1237 /*
1238 * don't drop the conflicting directory entry if the inode
1239 * for the new entry doesn't exist
1240 */
Chris Mason4bef0842008-09-08 11:18:08 -04001241 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001242 goto out;
1243
Chris Masone02119d2008-09-05 16:13:11 -04001244 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1245 BUG_ON(ret);
1246
1247 if (key->type == BTRFS_DIR_INDEX_KEY)
1248 goto insert;
1249out:
1250 btrfs_release_path(root, path);
1251 kfree(name);
1252 iput(dir);
1253 return 0;
1254
1255insert:
1256 btrfs_release_path(root, path);
1257 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1258 name, name_len, log_type, &log_key);
1259
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001260 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001261 goto out;
1262}
1263
1264/*
1265 * find all the names in a directory item and reconcile them into
1266 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1267 * one name in a directory item, but the same code gets used for
1268 * both directory index types
1269 */
1270static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1271 struct btrfs_root *root,
1272 struct btrfs_path *path,
1273 struct extent_buffer *eb, int slot,
1274 struct btrfs_key *key)
1275{
1276 int ret;
1277 u32 item_size = btrfs_item_size_nr(eb, slot);
1278 struct btrfs_dir_item *di;
1279 int name_len;
1280 unsigned long ptr;
1281 unsigned long ptr_end;
1282
1283 ptr = btrfs_item_ptr_offset(eb, slot);
1284 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001285 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001286 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001287 if (verify_dir_item(root, eb, di))
1288 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001289 name_len = btrfs_dir_name_len(eb, di);
1290 ret = replay_one_name(trans, root, path, eb, di, key);
1291 BUG_ON(ret);
1292 ptr = (unsigned long)(di + 1);
1293 ptr += name_len;
1294 }
1295 return 0;
1296}
1297
1298/*
1299 * directory replay has two parts. There are the standard directory
1300 * items in the log copied from the subvolume, and range items
1301 * created in the log while the subvolume was logged.
1302 *
1303 * The range items tell us which parts of the key space the log
1304 * is authoritative for. During replay, if a key in the subvolume
1305 * directory is in a logged range item, but not actually in the log
1306 * that means it was deleted from the directory before the fsync
1307 * and should be removed.
1308 */
1309static noinline int find_dir_range(struct btrfs_root *root,
1310 struct btrfs_path *path,
1311 u64 dirid, int key_type,
1312 u64 *start_ret, u64 *end_ret)
1313{
1314 struct btrfs_key key;
1315 u64 found_end;
1316 struct btrfs_dir_log_item *item;
1317 int ret;
1318 int nritems;
1319
1320 if (*start_ret == (u64)-1)
1321 return 1;
1322
1323 key.objectid = dirid;
1324 key.type = key_type;
1325 key.offset = *start_ret;
1326
1327 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1328 if (ret < 0)
1329 goto out;
1330 if (ret > 0) {
1331 if (path->slots[0] == 0)
1332 goto out;
1333 path->slots[0]--;
1334 }
1335 if (ret != 0)
1336 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1337
1338 if (key.type != key_type || key.objectid != dirid) {
1339 ret = 1;
1340 goto next;
1341 }
1342 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1343 struct btrfs_dir_log_item);
1344 found_end = btrfs_dir_log_end(path->nodes[0], item);
1345
1346 if (*start_ret >= key.offset && *start_ret <= found_end) {
1347 ret = 0;
1348 *start_ret = key.offset;
1349 *end_ret = found_end;
1350 goto out;
1351 }
1352 ret = 1;
1353next:
1354 /* check the next slot in the tree to see if it is a valid item */
1355 nritems = btrfs_header_nritems(path->nodes[0]);
1356 if (path->slots[0] >= nritems) {
1357 ret = btrfs_next_leaf(root, path);
1358 if (ret)
1359 goto out;
1360 } else {
1361 path->slots[0]++;
1362 }
1363
1364 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1365
1366 if (key.type != key_type || key.objectid != dirid) {
1367 ret = 1;
1368 goto out;
1369 }
1370 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1371 struct btrfs_dir_log_item);
1372 found_end = btrfs_dir_log_end(path->nodes[0], item);
1373 *start_ret = key.offset;
1374 *end_ret = found_end;
1375 ret = 0;
1376out:
1377 btrfs_release_path(root, path);
1378 return ret;
1379}
1380
1381/*
1382 * this looks for a given directory item in the log. If the directory
1383 * item is not in the log, the item is removed and the inode it points
1384 * to is unlinked
1385 */
1386static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1387 struct btrfs_root *root,
1388 struct btrfs_root *log,
1389 struct btrfs_path *path,
1390 struct btrfs_path *log_path,
1391 struct inode *dir,
1392 struct btrfs_key *dir_key)
1393{
1394 int ret;
1395 struct extent_buffer *eb;
1396 int slot;
1397 u32 item_size;
1398 struct btrfs_dir_item *di;
1399 struct btrfs_dir_item *log_di;
1400 int name_len;
1401 unsigned long ptr;
1402 unsigned long ptr_end;
1403 char *name;
1404 struct inode *inode;
1405 struct btrfs_key location;
1406
1407again:
1408 eb = path->nodes[0];
1409 slot = path->slots[0];
1410 item_size = btrfs_item_size_nr(eb, slot);
1411 ptr = btrfs_item_ptr_offset(eb, slot);
1412 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001413 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001414 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001415 if (verify_dir_item(root, eb, di)) {
1416 ret = -EIO;
1417 goto out;
1418 }
1419
Chris Masone02119d2008-09-05 16:13:11 -04001420 name_len = btrfs_dir_name_len(eb, di);
1421 name = kmalloc(name_len, GFP_NOFS);
1422 if (!name) {
1423 ret = -ENOMEM;
1424 goto out;
1425 }
1426 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1427 name_len);
1428 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001429 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001430 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1431 dir_key->objectid,
1432 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001433 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001434 log_di = btrfs_lookup_dir_index_item(trans, log,
1435 log_path,
1436 dir_key->objectid,
1437 dir_key->offset,
1438 name, name_len, 0);
1439 }
1440 if (!log_di || IS_ERR(log_di)) {
1441 btrfs_dir_item_key_to_cpu(eb, di, &location);
1442 btrfs_release_path(root, path);
1443 btrfs_release_path(log, log_path);
1444 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001445 if (!inode) {
1446 kfree(name);
1447 return -EIO;
1448 }
Chris Masone02119d2008-09-05 16:13:11 -04001449
1450 ret = link_to_fixup_dir(trans, root,
1451 path, location.objectid);
1452 BUG_ON(ret);
1453 btrfs_inc_nlink(inode);
1454 ret = btrfs_unlink_inode(trans, root, dir, inode,
1455 name, name_len);
1456 BUG_ON(ret);
1457 kfree(name);
1458 iput(inode);
1459
1460 /* there might still be more names under this key
1461 * check and repeat if required
1462 */
1463 ret = btrfs_search_slot(NULL, root, dir_key, path,
1464 0, 0);
1465 if (ret == 0)
1466 goto again;
1467 ret = 0;
1468 goto out;
1469 }
1470 btrfs_release_path(log, log_path);
1471 kfree(name);
1472
1473 ptr = (unsigned long)(di + 1);
1474 ptr += name_len;
1475 }
1476 ret = 0;
1477out:
1478 btrfs_release_path(root, path);
1479 btrfs_release_path(log, log_path);
1480 return ret;
1481}
1482
1483/*
1484 * deletion replay happens before we copy any new directory items
1485 * out of the log or out of backreferences from inodes. It
1486 * scans the log to find ranges of keys that log is authoritative for,
1487 * and then scans the directory to find items in those ranges that are
1488 * not present in the log.
1489 *
1490 * Anything we don't find in the log is unlinked and removed from the
1491 * directory.
1492 */
1493static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1494 struct btrfs_root *root,
1495 struct btrfs_root *log,
1496 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001497 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001498{
1499 u64 range_start;
1500 u64 range_end;
1501 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1502 int ret = 0;
1503 struct btrfs_key dir_key;
1504 struct btrfs_key found_key;
1505 struct btrfs_path *log_path;
1506 struct inode *dir;
1507
1508 dir_key.objectid = dirid;
1509 dir_key.type = BTRFS_DIR_ITEM_KEY;
1510 log_path = btrfs_alloc_path();
1511 if (!log_path)
1512 return -ENOMEM;
1513
1514 dir = read_one_inode(root, dirid);
1515 /* it isn't an error if the inode isn't there, that can happen
1516 * because we replay the deletes before we copy in the inode item
1517 * from the log
1518 */
1519 if (!dir) {
1520 btrfs_free_path(log_path);
1521 return 0;
1522 }
1523again:
1524 range_start = 0;
1525 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001526 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001527 if (del_all)
1528 range_end = (u64)-1;
1529 else {
1530 ret = find_dir_range(log, path, dirid, key_type,
1531 &range_start, &range_end);
1532 if (ret != 0)
1533 break;
1534 }
Chris Masone02119d2008-09-05 16:13:11 -04001535
1536 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001537 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001538 int nritems;
1539 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1540 0, 0);
1541 if (ret < 0)
1542 goto out;
1543
1544 nritems = btrfs_header_nritems(path->nodes[0]);
1545 if (path->slots[0] >= nritems) {
1546 ret = btrfs_next_leaf(root, path);
1547 if (ret)
1548 break;
1549 }
1550 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1551 path->slots[0]);
1552 if (found_key.objectid != dirid ||
1553 found_key.type != dir_key.type)
1554 goto next_type;
1555
1556 if (found_key.offset > range_end)
1557 break;
1558
1559 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001560 log_path, dir,
1561 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001562 BUG_ON(ret);
1563 if (found_key.offset == (u64)-1)
1564 break;
1565 dir_key.offset = found_key.offset + 1;
1566 }
1567 btrfs_release_path(root, path);
1568 if (range_end == (u64)-1)
1569 break;
1570 range_start = range_end + 1;
1571 }
1572
1573next_type:
1574 ret = 0;
1575 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1576 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1577 dir_key.type = BTRFS_DIR_INDEX_KEY;
1578 btrfs_release_path(root, path);
1579 goto again;
1580 }
1581out:
1582 btrfs_release_path(root, path);
1583 btrfs_free_path(log_path);
1584 iput(dir);
1585 return ret;
1586}
1587
1588/*
1589 * the process_func used to replay items from the log tree. This
1590 * gets called in two different stages. The first stage just looks
1591 * for inodes and makes sure they are all copied into the subvolume.
1592 *
1593 * The second stage copies all the other item types from the log into
1594 * the subvolume. The two stage approach is slower, but gets rid of
1595 * lots of complexity around inodes referencing other inodes that exist
1596 * only in the log (references come from either directory items or inode
1597 * back refs).
1598 */
1599static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1600 struct walk_control *wc, u64 gen)
1601{
1602 int nritems;
1603 struct btrfs_path *path;
1604 struct btrfs_root *root = wc->replay_dest;
1605 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001606 int level;
1607 int i;
1608 int ret;
1609
1610 btrfs_read_buffer(eb, gen);
1611
1612 level = btrfs_header_level(eb);
1613
1614 if (level != 0)
1615 return 0;
1616
1617 path = btrfs_alloc_path();
1618 BUG_ON(!path);
1619
1620 nritems = btrfs_header_nritems(eb);
1621 for (i = 0; i < nritems; i++) {
1622 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001623
1624 /* inode keys are done during the first stage */
1625 if (key.type == BTRFS_INODE_ITEM_KEY &&
1626 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001627 struct btrfs_inode_item *inode_item;
1628 u32 mode;
1629
1630 inode_item = btrfs_item_ptr(eb, i,
1631 struct btrfs_inode_item);
1632 mode = btrfs_inode_mode(eb, inode_item);
1633 if (S_ISDIR(mode)) {
1634 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001635 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001636 BUG_ON(ret);
1637 }
1638 ret = overwrite_item(wc->trans, root, path,
1639 eb, i, &key);
1640 BUG_ON(ret);
1641
Yan, Zhengc71bf092009-11-12 09:34:40 +00001642 /* for regular files, make sure corresponding
1643 * orhpan item exist. extents past the new EOF
1644 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001645 */
1646 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001647 ret = insert_orphan_item(wc->trans, root,
1648 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001649 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001650 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001651
Chris Masone02119d2008-09-05 16:13:11 -04001652 ret = link_to_fixup_dir(wc->trans, root,
1653 path, key.objectid);
1654 BUG_ON(ret);
1655 }
1656 if (wc->stage < LOG_WALK_REPLAY_ALL)
1657 continue;
1658
1659 /* these keys are simply copied */
1660 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1661 ret = overwrite_item(wc->trans, root, path,
1662 eb, i, &key);
1663 BUG_ON(ret);
1664 } else if (key.type == BTRFS_INODE_REF_KEY) {
1665 ret = add_inode_ref(wc->trans, root, log, path,
1666 eb, i, &key);
1667 BUG_ON(ret && ret != -ENOENT);
1668 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1669 ret = replay_one_extent(wc->trans, root, path,
1670 eb, i, &key);
1671 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001672 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1673 key.type == BTRFS_DIR_INDEX_KEY) {
1674 ret = replay_one_dir_item(wc->trans, root, path,
1675 eb, i, &key);
1676 BUG_ON(ret);
1677 }
1678 }
1679 btrfs_free_path(path);
1680 return 0;
1681}
1682
Chris Masond3977122009-01-05 21:25:51 -05001683static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001684 struct btrfs_root *root,
1685 struct btrfs_path *path, int *level,
1686 struct walk_control *wc)
1687{
1688 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001689 u64 bytenr;
1690 u64 ptr_gen;
1691 struct extent_buffer *next;
1692 struct extent_buffer *cur;
1693 struct extent_buffer *parent;
1694 u32 blocksize;
1695 int ret = 0;
1696
1697 WARN_ON(*level < 0);
1698 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1699
Chris Masond3977122009-01-05 21:25:51 -05001700 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001701 WARN_ON(*level < 0);
1702 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1703 cur = path->nodes[*level];
1704
1705 if (btrfs_header_level(cur) != *level)
1706 WARN_ON(1);
1707
1708 if (path->slots[*level] >=
1709 btrfs_header_nritems(cur))
1710 break;
1711
1712 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1713 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1714 blocksize = btrfs_level_size(root, *level - 1);
1715
1716 parent = path->nodes[*level];
1717 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001718
1719 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00001720 if (!next)
1721 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001722
Chris Masone02119d2008-09-05 16:13:11 -04001723 if (*level == 1) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001724 wc->process_func(root, next, wc, ptr_gen);
1725
Chris Masone02119d2008-09-05 16:13:11 -04001726 path->slots[*level]++;
1727 if (wc->free) {
1728 btrfs_read_buffer(next, ptr_gen);
1729
1730 btrfs_tree_lock(next);
1731 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001732 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001733 btrfs_wait_tree_block_writeback(next);
1734 btrfs_tree_unlock(next);
1735
Chris Masone02119d2008-09-05 16:13:11 -04001736 WARN_ON(root_owner !=
1737 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001738 ret = btrfs_free_reserved_extent(root,
1739 bytenr, blocksize);
Chris Masone02119d2008-09-05 16:13:11 -04001740 BUG_ON(ret);
1741 }
1742 free_extent_buffer(next);
1743 continue;
1744 }
1745 btrfs_read_buffer(next, ptr_gen);
1746
1747 WARN_ON(*level <= 0);
1748 if (path->nodes[*level-1])
1749 free_extent_buffer(path->nodes[*level-1]);
1750 path->nodes[*level-1] = next;
1751 *level = btrfs_header_level(next);
1752 path->slots[*level] = 0;
1753 cond_resched();
1754 }
1755 WARN_ON(*level < 0);
1756 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1757
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001758 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04001759
1760 cond_resched();
1761 return 0;
1762}
1763
Chris Masond3977122009-01-05 21:25:51 -05001764static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001765 struct btrfs_root *root,
1766 struct btrfs_path *path, int *level,
1767 struct walk_control *wc)
1768{
1769 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001770 int i;
1771 int slot;
1772 int ret;
1773
Chris Masond3977122009-01-05 21:25:51 -05001774 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04001775 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001776 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04001777 path->slots[i]++;
1778 *level = i;
1779 WARN_ON(*level == 0);
1780 return 0;
1781 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001782 struct extent_buffer *parent;
1783 if (path->nodes[*level] == root->node)
1784 parent = path->nodes[*level];
1785 else
1786 parent = path->nodes[*level + 1];
1787
1788 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001789 wc->process_func(root, path->nodes[*level], wc,
1790 btrfs_header_generation(path->nodes[*level]));
1791 if (wc->free) {
1792 struct extent_buffer *next;
1793
1794 next = path->nodes[*level];
1795
1796 btrfs_tree_lock(next);
1797 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001798 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001799 btrfs_wait_tree_block_writeback(next);
1800 btrfs_tree_unlock(next);
1801
Chris Masone02119d2008-09-05 16:13:11 -04001802 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001803 ret = btrfs_free_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04001804 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04001805 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04001806 BUG_ON(ret);
1807 }
1808 free_extent_buffer(path->nodes[*level]);
1809 path->nodes[*level] = NULL;
1810 *level = i + 1;
1811 }
1812 }
1813 return 1;
1814}
1815
1816/*
1817 * drop the reference count on the tree rooted at 'snap'. This traverses
1818 * the tree freeing any blocks that have a ref count of zero after being
1819 * decremented.
1820 */
1821static int walk_log_tree(struct btrfs_trans_handle *trans,
1822 struct btrfs_root *log, struct walk_control *wc)
1823{
1824 int ret = 0;
1825 int wret;
1826 int level;
1827 struct btrfs_path *path;
1828 int i;
1829 int orig_level;
1830
1831 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001832 if (!path)
1833 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001834
1835 level = btrfs_header_level(log->node);
1836 orig_level = level;
1837 path->nodes[level] = log->node;
1838 extent_buffer_get(log->node);
1839 path->slots[level] = 0;
1840
Chris Masond3977122009-01-05 21:25:51 -05001841 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001842 wret = walk_down_log_tree(trans, log, path, &level, wc);
1843 if (wret > 0)
1844 break;
1845 if (wret < 0)
1846 ret = wret;
1847
1848 wret = walk_up_log_tree(trans, log, path, &level, wc);
1849 if (wret > 0)
1850 break;
1851 if (wret < 0)
1852 ret = wret;
1853 }
1854
1855 /* was the root node processed? if not, catch it here */
1856 if (path->nodes[orig_level]) {
1857 wc->process_func(log, path->nodes[orig_level], wc,
1858 btrfs_header_generation(path->nodes[orig_level]));
1859 if (wc->free) {
1860 struct extent_buffer *next;
1861
1862 next = path->nodes[orig_level];
1863
1864 btrfs_tree_lock(next);
1865 clean_tree_block(trans, log, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001866 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001867 btrfs_wait_tree_block_writeback(next);
1868 btrfs_tree_unlock(next);
1869
Chris Masone02119d2008-09-05 16:13:11 -04001870 WARN_ON(log->root_key.objectid !=
1871 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001872 ret = btrfs_free_reserved_extent(log, next->start,
1873 next->len);
Chris Masone02119d2008-09-05 16:13:11 -04001874 BUG_ON(ret);
1875 }
1876 }
1877
1878 for (i = 0; i <= orig_level; i++) {
1879 if (path->nodes[i]) {
1880 free_extent_buffer(path->nodes[i]);
1881 path->nodes[i] = NULL;
1882 }
1883 }
1884 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001885 return ret;
1886}
1887
Yan Zheng7237f182009-01-21 12:54:03 -05001888/*
1889 * helper function to update the item for a given subvolumes log root
1890 * in the tree of log roots
1891 */
1892static int update_log_root(struct btrfs_trans_handle *trans,
1893 struct btrfs_root *log)
1894{
1895 int ret;
1896
1897 if (log->log_transid == 1) {
1898 /* insert root item on the first sync */
1899 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1900 &log->root_key, &log->root_item);
1901 } else {
1902 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1903 &log->root_key, &log->root_item);
1904 }
1905 return ret;
1906}
1907
Chris Mason12fcfd22009-03-24 10:24:20 -04001908static int wait_log_commit(struct btrfs_trans_handle *trans,
1909 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04001910{
1911 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05001912 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04001913
Yan Zheng7237f182009-01-21 12:54:03 -05001914 /*
1915 * we only allow two pending log transactions at a time,
1916 * so we know that if ours is more than 2 older than the
1917 * current transaction, we're done
1918 */
Chris Masone02119d2008-09-05 16:13:11 -04001919 do {
Yan Zheng7237f182009-01-21 12:54:03 -05001920 prepare_to_wait(&root->log_commit_wait[index],
1921 &wait, TASK_UNINTERRUPTIBLE);
1922 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001923
1924 if (root->fs_info->last_trans_log_full_commit !=
1925 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001926 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04001927 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04001928
Yan Zheng7237f182009-01-21 12:54:03 -05001929 finish_wait(&root->log_commit_wait[index], &wait);
1930 mutex_lock(&root->log_mutex);
1931 } while (root->log_transid < transid + 2 &&
1932 atomic_read(&root->log_commit[index]));
1933 return 0;
1934}
1935
Chris Mason12fcfd22009-03-24 10:24:20 -04001936static int wait_for_writer(struct btrfs_trans_handle *trans,
1937 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05001938{
1939 DEFINE_WAIT(wait);
1940 while (atomic_read(&root->log_writers)) {
1941 prepare_to_wait(&root->log_writer_wait,
1942 &wait, TASK_UNINTERRUPTIBLE);
1943 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001944 if (root->fs_info->last_trans_log_full_commit !=
1945 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05001946 schedule();
1947 mutex_lock(&root->log_mutex);
1948 finish_wait(&root->log_writer_wait, &wait);
1949 }
Chris Masone02119d2008-09-05 16:13:11 -04001950 return 0;
1951}
1952
1953/*
1954 * btrfs_sync_log does sends a given tree log down to the disk and
1955 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04001956 * you know that any inodes previously logged are safely on disk only
1957 * if it returns 0.
1958 *
1959 * Any other return value means you need to call btrfs_commit_transaction.
1960 * Some of the edge cases for fsyncing directories that have had unlinks
1961 * or renames done in the past mean that sometimes the only safe
1962 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
1963 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04001964 */
1965int btrfs_sync_log(struct btrfs_trans_handle *trans,
1966 struct btrfs_root *root)
1967{
Yan Zheng7237f182009-01-21 12:54:03 -05001968 int index1;
1969 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001970 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04001971 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04001972 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05001973 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001974 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001975
Yan Zheng7237f182009-01-21 12:54:03 -05001976 mutex_lock(&root->log_mutex);
1977 index1 = root->log_transid % 2;
1978 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001979 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05001980 mutex_unlock(&root->log_mutex);
1981 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04001982 }
Yan Zheng7237f182009-01-21 12:54:03 -05001983 atomic_set(&root->log_commit[index1], 1);
1984
1985 /* wait for previous tree log sync to complete */
1986 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04001987 wait_log_commit(trans, root, root->log_transid - 1);
Chris Masone02119d2008-09-05 16:13:11 -04001988
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001989 while (1) {
Yan Zheng7237f182009-01-21 12:54:03 -05001990 unsigned long batch = root->log_batch;
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001991 if (root->log_multiple_pids) {
1992 mutex_unlock(&root->log_mutex);
1993 schedule_timeout_uninterruptible(1);
1994 mutex_lock(&root->log_mutex);
1995 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001996 wait_for_writer(trans, root);
Yan Zheng7237f182009-01-21 12:54:03 -05001997 if (batch == root->log_batch)
Chris Masone02119d2008-09-05 16:13:11 -04001998 break;
1999 }
Chris Masond0c803c2008-09-11 16:17:57 -04002000
Chris Mason12fcfd22009-03-24 10:24:20 -04002001 /* bail out if we need to do a full commit */
2002 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2003 ret = -EAGAIN;
2004 mutex_unlock(&root->log_mutex);
2005 goto out;
2006 }
2007
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002008 log_transid = root->log_transid;
2009 if (log_transid % 2 == 0)
2010 mark = EXTENT_DIRTY;
2011 else
2012 mark = EXTENT_NEW;
2013
Chris Mason690587d2009-10-13 13:29:19 -04002014 /* we start IO on all the marked extents here, but we don't actually
2015 * wait for them until later.
2016 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002017 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002018 BUG_ON(ret);
Yan Zheng7237f182009-01-21 12:54:03 -05002019
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002020 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002021
2022 root->log_batch = 0;
2023 root->log_transid++;
2024 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002025 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002026 smp_mb();
2027 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002028 * IO has been started, blocks of the log tree have WRITTEN flag set
2029 * in their headers. new modifications of the log will be written to
2030 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002031 */
2032 mutex_unlock(&root->log_mutex);
2033
2034 mutex_lock(&log_root_tree->log_mutex);
2035 log_root_tree->log_batch++;
2036 atomic_inc(&log_root_tree->log_writers);
2037 mutex_unlock(&log_root_tree->log_mutex);
2038
2039 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002040
2041 mutex_lock(&log_root_tree->log_mutex);
2042 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2043 smp_mb();
2044 if (waitqueue_active(&log_root_tree->log_writer_wait))
2045 wake_up(&log_root_tree->log_writer_wait);
2046 }
2047
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002048 if (ret) {
2049 BUG_ON(ret != -ENOSPC);
2050 root->fs_info->last_trans_log_full_commit = trans->transid;
2051 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2052 mutex_unlock(&log_root_tree->log_mutex);
2053 ret = -EAGAIN;
2054 goto out;
2055 }
2056
Yan Zheng7237f182009-01-21 12:54:03 -05002057 index2 = log_root_tree->log_transid % 2;
2058 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002059 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002060 wait_log_commit(trans, log_root_tree,
2061 log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002062 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002063 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002064 goto out;
2065 }
2066 atomic_set(&log_root_tree->log_commit[index2], 1);
2067
Chris Mason12fcfd22009-03-24 10:24:20 -04002068 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2069 wait_log_commit(trans, log_root_tree,
2070 log_root_tree->log_transid - 1);
2071 }
Yan Zheng7237f182009-01-21 12:54:03 -05002072
Chris Mason12fcfd22009-03-24 10:24:20 -04002073 wait_for_writer(trans, log_root_tree);
2074
2075 /*
2076 * now that we've moved on to the tree of log tree roots,
2077 * check the full commit flag again
2078 */
2079 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002080 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002081 mutex_unlock(&log_root_tree->log_mutex);
2082 ret = -EAGAIN;
2083 goto out_wake_log_root;
2084 }
Yan Zheng7237f182009-01-21 12:54:03 -05002085
2086 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002087 &log_root_tree->dirty_log_pages,
2088 EXTENT_DIRTY | EXTENT_NEW);
Chris Masone02119d2008-09-05 16:13:11 -04002089 BUG_ON(ret);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002090 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002091
2092 btrfs_set_super_log_root(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002093 log_root_tree->node->start);
Chris Masone02119d2008-09-05 16:13:11 -04002094 btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002095 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002096
Yan Zheng7237f182009-01-21 12:54:03 -05002097 log_root_tree->log_batch = 0;
2098 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002099 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002100
2101 mutex_unlock(&log_root_tree->log_mutex);
2102
2103 /*
2104 * nobody else is going to jump in and write the the ctree
2105 * super here because the log_commit atomic below is protecting
2106 * us. We must be called with a transaction handle pinning
2107 * the running transaction open, so a full commit can't hop
2108 * in and cause problems either.
2109 */
Chris Mason47226072009-10-13 12:55:09 -04002110 write_ctree_super(trans, root->fs_info->tree_root, 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002111 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002112
Chris Mason257c62e2009-10-13 13:21:08 -04002113 mutex_lock(&root->log_mutex);
2114 if (root->last_log_commit < log_transid)
2115 root->last_log_commit = log_transid;
2116 mutex_unlock(&root->log_mutex);
2117
Chris Mason12fcfd22009-03-24 10:24:20 -04002118out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002119 atomic_set(&log_root_tree->log_commit[index2], 0);
2120 smp_mb();
2121 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2122 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002123out:
Yan Zheng7237f182009-01-21 12:54:03 -05002124 atomic_set(&root->log_commit[index1], 0);
2125 smp_mb();
2126 if (waitqueue_active(&root->log_commit_wait[index1]))
2127 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002128 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002129}
2130
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002131static void free_log_tree(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002133{
2134 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002135 u64 start;
2136 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002137 struct walk_control wc = {
2138 .free = 1,
2139 .process_func = process_one_buffer
2140 };
2141
Chris Masone02119d2008-09-05 16:13:11 -04002142 ret = walk_log_tree(trans, log, &wc);
2143 BUG_ON(ret);
2144
Chris Masond3977122009-01-05 21:25:51 -05002145 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002146 ret = find_first_extent_bit(&log->dirty_log_pages,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002147 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04002148 if (ret)
2149 break;
2150
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002151 clear_extent_bits(&log->dirty_log_pages, start, end,
2152 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002153 }
2154
Yan Zheng7237f182009-01-21 12:54:03 -05002155 free_extent_buffer(log->node);
2156 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002157}
2158
2159/*
2160 * free all the extents used by the tree log. This should be called
2161 * at commit time of the full transaction
2162 */
2163int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2164{
2165 if (root->log_root) {
2166 free_log_tree(trans, root->log_root);
2167 root->log_root = NULL;
2168 }
2169 return 0;
2170}
2171
2172int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2173 struct btrfs_fs_info *fs_info)
2174{
2175 if (fs_info->log_root_tree) {
2176 free_log_tree(trans, fs_info->log_root_tree);
2177 fs_info->log_root_tree = NULL;
2178 }
Chris Masone02119d2008-09-05 16:13:11 -04002179 return 0;
2180}
2181
2182/*
Chris Masone02119d2008-09-05 16:13:11 -04002183 * If both a file and directory are logged, and unlinks or renames are
2184 * mixed in, we have a few interesting corners:
2185 *
2186 * create file X in dir Y
2187 * link file X to X.link in dir Y
2188 * fsync file X
2189 * unlink file X but leave X.link
2190 * fsync dir Y
2191 *
2192 * After a crash we would expect only X.link to exist. But file X
2193 * didn't get fsync'd again so the log has back refs for X and X.link.
2194 *
2195 * We solve this by removing directory entries and inode backrefs from the
2196 * log when a file that was logged in the current transaction is
2197 * unlinked. Any later fsync will include the updated log entries, and
2198 * we'll be able to reconstruct the proper directory items from backrefs.
2199 *
2200 * This optimizations allows us to avoid relogging the entire inode
2201 * or the entire directory.
2202 */
2203int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2204 struct btrfs_root *root,
2205 const char *name, int name_len,
2206 struct inode *dir, u64 index)
2207{
2208 struct btrfs_root *log;
2209 struct btrfs_dir_item *di;
2210 struct btrfs_path *path;
2211 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002212 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002213 int bytes_del = 0;
2214
Chris Mason3a5f1d42008-09-11 15:53:37 -04002215 if (BTRFS_I(dir)->logged_trans < trans->transid)
2216 return 0;
2217
Chris Masone02119d2008-09-05 16:13:11 -04002218 ret = join_running_log_trans(root);
2219 if (ret)
2220 return 0;
2221
2222 mutex_lock(&BTRFS_I(dir)->log_mutex);
2223
2224 log = root->log_root;
2225 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002226 if (!path) {
2227 err = -ENOMEM;
2228 goto out_unlock;
2229 }
liubo2a29edc2011-01-26 06:22:08 +00002230
Chris Masone02119d2008-09-05 16:13:11 -04002231 di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
2232 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002233 if (IS_ERR(di)) {
2234 err = PTR_ERR(di);
2235 goto fail;
2236 }
2237 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002238 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2239 bytes_del += name_len;
2240 BUG_ON(ret);
2241 }
2242 btrfs_release_path(log, path);
2243 di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
2244 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002245 if (IS_ERR(di)) {
2246 err = PTR_ERR(di);
2247 goto fail;
2248 }
2249 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002250 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2251 bytes_del += name_len;
2252 BUG_ON(ret);
2253 }
2254
2255 /* update the directory size in the log to reflect the names
2256 * we have removed
2257 */
2258 if (bytes_del) {
2259 struct btrfs_key key;
2260
2261 key.objectid = dir->i_ino;
2262 key.offset = 0;
2263 key.type = BTRFS_INODE_ITEM_KEY;
2264 btrfs_release_path(log, path);
2265
2266 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002267 if (ret < 0) {
2268 err = ret;
2269 goto fail;
2270 }
Chris Masone02119d2008-09-05 16:13:11 -04002271 if (ret == 0) {
2272 struct btrfs_inode_item *item;
2273 u64 i_size;
2274
2275 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2276 struct btrfs_inode_item);
2277 i_size = btrfs_inode_size(path->nodes[0], item);
2278 if (i_size > bytes_del)
2279 i_size -= bytes_del;
2280 else
2281 i_size = 0;
2282 btrfs_set_inode_size(path->nodes[0], item, i_size);
2283 btrfs_mark_buffer_dirty(path->nodes[0]);
2284 } else
2285 ret = 0;
2286 btrfs_release_path(log, path);
2287 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002288fail:
Chris Masone02119d2008-09-05 16:13:11 -04002289 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002290out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002291 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002292 if (ret == -ENOSPC) {
2293 root->fs_info->last_trans_log_full_commit = trans->transid;
2294 ret = 0;
2295 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002296 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002297
Andi Kleen411fc6b2010-10-29 15:14:31 -04002298 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002299}
2300
2301/* see comments for btrfs_del_dir_entries_in_log */
2302int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root,
2304 const char *name, int name_len,
2305 struct inode *inode, u64 dirid)
2306{
2307 struct btrfs_root *log;
2308 u64 index;
2309 int ret;
2310
Chris Mason3a5f1d42008-09-11 15:53:37 -04002311 if (BTRFS_I(inode)->logged_trans < trans->transid)
2312 return 0;
2313
Chris Masone02119d2008-09-05 16:13:11 -04002314 ret = join_running_log_trans(root);
2315 if (ret)
2316 return 0;
2317 log = root->log_root;
2318 mutex_lock(&BTRFS_I(inode)->log_mutex);
2319
2320 ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
2321 dirid, &index);
2322 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002323 if (ret == -ENOSPC) {
2324 root->fs_info->last_trans_log_full_commit = trans->transid;
2325 ret = 0;
2326 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002327 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002328
Chris Masone02119d2008-09-05 16:13:11 -04002329 return ret;
2330}
2331
2332/*
2333 * creates a range item in the log for 'dirid'. first_offset and
2334 * last_offset tell us which parts of the key space the log should
2335 * be considered authoritative for.
2336 */
2337static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2338 struct btrfs_root *log,
2339 struct btrfs_path *path,
2340 int key_type, u64 dirid,
2341 u64 first_offset, u64 last_offset)
2342{
2343 int ret;
2344 struct btrfs_key key;
2345 struct btrfs_dir_log_item *item;
2346
2347 key.objectid = dirid;
2348 key.offset = first_offset;
2349 if (key_type == BTRFS_DIR_ITEM_KEY)
2350 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2351 else
2352 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2353 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002354 if (ret)
2355 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002356
2357 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2358 struct btrfs_dir_log_item);
2359 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2360 btrfs_mark_buffer_dirty(path->nodes[0]);
2361 btrfs_release_path(log, path);
2362 return 0;
2363}
2364
2365/*
2366 * log all the items included in the current transaction for a given
2367 * directory. This also creates the range items in the log tree required
2368 * to replay anything deleted before the fsync
2369 */
2370static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2371 struct btrfs_root *root, struct inode *inode,
2372 struct btrfs_path *path,
2373 struct btrfs_path *dst_path, int key_type,
2374 u64 min_offset, u64 *last_offset_ret)
2375{
2376 struct btrfs_key min_key;
2377 struct btrfs_key max_key;
2378 struct btrfs_root *log = root->log_root;
2379 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002380 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002381 int ret;
2382 int i;
2383 int nritems;
2384 u64 first_offset = min_offset;
2385 u64 last_offset = (u64)-1;
2386
2387 log = root->log_root;
2388 max_key.objectid = inode->i_ino;
2389 max_key.offset = (u64)-1;
2390 max_key.type = key_type;
2391
2392 min_key.objectid = inode->i_ino;
2393 min_key.type = key_type;
2394 min_key.offset = min_offset;
2395
2396 path->keep_locks = 1;
2397
2398 ret = btrfs_search_forward(root, &min_key, &max_key,
2399 path, 0, trans->transid);
2400
2401 /*
2402 * we didn't find anything from this transaction, see if there
2403 * is anything at all
2404 */
2405 if (ret != 0 || min_key.objectid != inode->i_ino ||
2406 min_key.type != key_type) {
2407 min_key.objectid = inode->i_ino;
2408 min_key.type = key_type;
2409 min_key.offset = (u64)-1;
2410 btrfs_release_path(root, path);
2411 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2412 if (ret < 0) {
2413 btrfs_release_path(root, path);
2414 return ret;
2415 }
2416 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2417
2418 /* if ret == 0 there are items for this type,
2419 * create a range to tell us the last key of this type.
2420 * otherwise, there are no items in this directory after
2421 * *min_offset, and we create a range to indicate that.
2422 */
2423 if (ret == 0) {
2424 struct btrfs_key tmp;
2425 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2426 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002427 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002428 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002429 }
2430 goto done;
2431 }
2432
2433 /* go backward to find any previous key */
2434 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2435 if (ret == 0) {
2436 struct btrfs_key tmp;
2437 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2438 if (key_type == tmp.type) {
2439 first_offset = tmp.offset;
2440 ret = overwrite_item(trans, log, dst_path,
2441 path->nodes[0], path->slots[0],
2442 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002443 if (ret) {
2444 err = ret;
2445 goto done;
2446 }
Chris Masone02119d2008-09-05 16:13:11 -04002447 }
2448 }
2449 btrfs_release_path(root, path);
2450
2451 /* find the first key from this transaction again */
2452 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2453 if (ret != 0) {
2454 WARN_ON(1);
2455 goto done;
2456 }
2457
2458 /*
2459 * we have a block from this transaction, log every item in it
2460 * from our directory
2461 */
Chris Masond3977122009-01-05 21:25:51 -05002462 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002463 struct btrfs_key tmp;
2464 src = path->nodes[0];
2465 nritems = btrfs_header_nritems(src);
2466 for (i = path->slots[0]; i < nritems; i++) {
2467 btrfs_item_key_to_cpu(src, &min_key, i);
2468
2469 if (min_key.objectid != inode->i_ino ||
2470 min_key.type != key_type)
2471 goto done;
2472 ret = overwrite_item(trans, log, dst_path, src, i,
2473 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002474 if (ret) {
2475 err = ret;
2476 goto done;
2477 }
Chris Masone02119d2008-09-05 16:13:11 -04002478 }
2479 path->slots[0] = nritems;
2480
2481 /*
2482 * look ahead to the next item and see if it is also
2483 * from this directory and from this transaction
2484 */
2485 ret = btrfs_next_leaf(root, path);
2486 if (ret == 1) {
2487 last_offset = (u64)-1;
2488 goto done;
2489 }
2490 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2491 if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
2492 last_offset = (u64)-1;
2493 goto done;
2494 }
2495 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2496 ret = overwrite_item(trans, log, dst_path,
2497 path->nodes[0], path->slots[0],
2498 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002499 if (ret)
2500 err = ret;
2501 else
2502 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002503 goto done;
2504 }
2505 }
2506done:
Chris Masone02119d2008-09-05 16:13:11 -04002507 btrfs_release_path(root, path);
2508 btrfs_release_path(log, dst_path);
2509
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002510 if (err == 0) {
2511 *last_offset_ret = last_offset;
2512 /*
2513 * insert the log range keys to indicate where the log
2514 * is valid
2515 */
2516 ret = insert_dir_log_key(trans, log, path, key_type,
2517 inode->i_ino, first_offset,
2518 last_offset);
2519 if (ret)
2520 err = ret;
2521 }
2522 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002523}
2524
2525/*
2526 * logging directories is very similar to logging inodes, We find all the items
2527 * from the current transaction and write them to the log.
2528 *
2529 * The recovery code scans the directory in the subvolume, and if it finds a
2530 * key in the range logged that is not present in the log tree, then it means
2531 * that dir entry was unlinked during the transaction.
2532 *
2533 * In order for that scan to work, we must include one key smaller than
2534 * the smallest logged by this transaction and one key larger than the largest
2535 * key logged by this transaction.
2536 */
2537static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2538 struct btrfs_root *root, struct inode *inode,
2539 struct btrfs_path *path,
2540 struct btrfs_path *dst_path)
2541{
2542 u64 min_key;
2543 u64 max_key;
2544 int ret;
2545 int key_type = BTRFS_DIR_ITEM_KEY;
2546
2547again:
2548 min_key = 0;
2549 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002550 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002551 ret = log_dir_items(trans, root, inode, path,
2552 dst_path, key_type, min_key,
2553 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002554 if (ret)
2555 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002556 if (max_key == (u64)-1)
2557 break;
2558 min_key = max_key + 1;
2559 }
2560
2561 if (key_type == BTRFS_DIR_ITEM_KEY) {
2562 key_type = BTRFS_DIR_INDEX_KEY;
2563 goto again;
2564 }
2565 return 0;
2566}
2567
2568/*
2569 * a helper function to drop items from the log before we relog an
2570 * inode. max_key_type indicates the highest item type to remove.
2571 * This cannot be run for file data extents because it does not
2572 * free the extents they point to.
2573 */
2574static int drop_objectid_items(struct btrfs_trans_handle *trans,
2575 struct btrfs_root *log,
2576 struct btrfs_path *path,
2577 u64 objectid, int max_key_type)
2578{
2579 int ret;
2580 struct btrfs_key key;
2581 struct btrfs_key found_key;
2582
2583 key.objectid = objectid;
2584 key.type = max_key_type;
2585 key.offset = (u64)-1;
2586
Chris Masond3977122009-01-05 21:25:51 -05002587 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002588 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002589 BUG_ON(ret == 0);
2590 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002591 break;
2592
2593 if (path->slots[0] == 0)
2594 break;
2595
2596 path->slots[0]--;
2597 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2598 path->slots[0]);
2599
2600 if (found_key.objectid != objectid)
2601 break;
2602
2603 ret = btrfs_del_item(trans, log, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002604 if (ret)
2605 break;
Chris Masone02119d2008-09-05 16:13:11 -04002606 btrfs_release_path(log, path);
2607 }
2608 btrfs_release_path(log, path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002609 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002610}
2611
Chris Mason31ff1cd2008-09-11 16:17:57 -04002612static noinline int copy_items(struct btrfs_trans_handle *trans,
2613 struct btrfs_root *log,
2614 struct btrfs_path *dst_path,
2615 struct extent_buffer *src,
2616 int start_slot, int nr, int inode_only)
2617{
2618 unsigned long src_offset;
2619 unsigned long dst_offset;
2620 struct btrfs_file_extent_item *extent;
2621 struct btrfs_inode_item *inode_item;
2622 int ret;
2623 struct btrfs_key *ins_keys;
2624 u32 *ins_sizes;
2625 char *ins_data;
2626 int i;
Chris Masond20f7042008-12-08 16:58:54 -05002627 struct list_head ordered_sums;
2628
2629 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002630
2631 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2632 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00002633 if (!ins_data)
2634 return -ENOMEM;
2635
Chris Mason31ff1cd2008-09-11 16:17:57 -04002636 ins_sizes = (u32 *)ins_data;
2637 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2638
2639 for (i = 0; i < nr; i++) {
2640 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2641 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2642 }
2643 ret = btrfs_insert_empty_items(trans, log, dst_path,
2644 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002645 if (ret) {
2646 kfree(ins_data);
2647 return ret;
2648 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002650 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002651 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2652 dst_path->slots[0]);
2653
2654 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2655
2656 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2657 src_offset, ins_sizes[i]);
2658
2659 if (inode_only == LOG_INODE_EXISTS &&
2660 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2661 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2662 dst_path->slots[0],
2663 struct btrfs_inode_item);
2664 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2665
2666 /* set the generation to zero so the recover code
2667 * can tell the difference between an logging
2668 * just to say 'this inode exists' and a logging
2669 * to say 'update this inode with these values'
2670 */
2671 btrfs_set_inode_generation(dst_path->nodes[0],
2672 inode_item, 0);
2673 }
2674 /* take a reference on file data extents so that truncates
2675 * or deletes of this inode don't have to relog the inode
2676 * again
2677 */
2678 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2679 int found_type;
2680 extent = btrfs_item_ptr(src, start_slot + i,
2681 struct btrfs_file_extent_item);
2682
2683 found_type = btrfs_file_extent_type(src, extent);
Yan Zhengd899e052008-10-30 14:25:28 -04002684 if (found_type == BTRFS_FILE_EXTENT_REG ||
2685 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002686 u64 ds, dl, cs, cl;
2687 ds = btrfs_file_extent_disk_bytenr(src,
2688 extent);
2689 /* ds == 0 is a hole */
2690 if (ds == 0)
2691 continue;
2692
2693 dl = btrfs_file_extent_disk_num_bytes(src,
2694 extent);
2695 cs = btrfs_file_extent_offset(src, extent);
2696 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07002697 extent);
Chris Mason580afd72008-12-08 19:15:39 -05002698 if (btrfs_file_extent_compression(src,
2699 extent)) {
2700 cs = 0;
2701 cl = dl;
2702 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002703
2704 ret = btrfs_lookup_csums_range(
2705 log->fs_info->csum_root,
2706 ds + cs, ds + cs + cl - 1,
2707 &ordered_sums);
2708 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002709 }
2710 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002711 }
2712
2713 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2714 btrfs_release_path(log, dst_path);
2715 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05002716
2717 /*
2718 * we have to do this after the loop above to avoid changing the
2719 * log tree while trying to change the log tree.
2720 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002721 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002722 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05002723 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2724 struct btrfs_ordered_sum,
2725 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002726 if (!ret)
2727 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05002728 list_del(&sums->list);
2729 kfree(sums);
2730 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002731 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002732}
2733
Chris Masone02119d2008-09-05 16:13:11 -04002734/* log a single inode in the tree log.
2735 * At least one parent directory for this inode must exist in the tree
2736 * or be logged already.
2737 *
2738 * Any items from this inode changed by the current transaction are copied
2739 * to the log tree. An extra reference is taken on any extents in this
2740 * file, allowing us to avoid a whole pile of corner cases around logging
2741 * blocks that have been removed from the tree.
2742 *
2743 * See LOG_INODE_ALL and related defines for a description of what inode_only
2744 * does.
2745 *
2746 * This handles both files and directories.
2747 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002748static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002749 struct btrfs_root *root, struct inode *inode,
2750 int inode_only)
2751{
2752 struct btrfs_path *path;
2753 struct btrfs_path *dst_path;
2754 struct btrfs_key min_key;
2755 struct btrfs_key max_key;
2756 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002757 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002758 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002759 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002760 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002761 int ins_start_slot = 0;
2762 int ins_nr;
Chris Masone02119d2008-09-05 16:13:11 -04002763
2764 log = root->log_root;
2765
2766 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002767 if (!path)
2768 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002769 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002770 if (!dst_path) {
2771 btrfs_free_path(path);
2772 return -ENOMEM;
2773 }
Chris Masone02119d2008-09-05 16:13:11 -04002774
2775 min_key.objectid = inode->i_ino;
2776 min_key.type = BTRFS_INODE_ITEM_KEY;
2777 min_key.offset = 0;
2778
2779 max_key.objectid = inode->i_ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04002780
2781 /* today the code can only do partial logging of directories */
2782 if (!S_ISDIR(inode->i_mode))
2783 inode_only = LOG_INODE_ALL;
2784
Chris Masone02119d2008-09-05 16:13:11 -04002785 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2786 max_key.type = BTRFS_XATTR_ITEM_KEY;
2787 else
2788 max_key.type = (u8)-1;
2789 max_key.offset = (u64)-1;
2790
Chris Masone02119d2008-09-05 16:13:11 -04002791 mutex_lock(&BTRFS_I(inode)->log_mutex);
2792
2793 /*
2794 * a brute force approach to making sure we get the most uptodate
2795 * copies of everything.
2796 */
2797 if (S_ISDIR(inode->i_mode)) {
2798 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2799
2800 if (inode_only == LOG_INODE_EXISTS)
2801 max_key_type = BTRFS_XATTR_ITEM_KEY;
2802 ret = drop_objectid_items(trans, log, path,
2803 inode->i_ino, max_key_type);
2804 } else {
2805 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2806 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002807 if (ret) {
2808 err = ret;
2809 goto out_unlock;
2810 }
Chris Masone02119d2008-09-05 16:13:11 -04002811 path->keep_locks = 1;
2812
Chris Masond3977122009-01-05 21:25:51 -05002813 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002814 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002815 ret = btrfs_search_forward(root, &min_key, &max_key,
2816 path, 0, trans->transid);
2817 if (ret != 0)
2818 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002819again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04002820 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Chris Masone02119d2008-09-05 16:13:11 -04002821 if (min_key.objectid != inode->i_ino)
2822 break;
2823 if (min_key.type > max_key.type)
2824 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002825
Chris Masone02119d2008-09-05 16:13:11 -04002826 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04002827 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2828 ins_nr++;
2829 goto next_slot;
2830 } else if (!ins_nr) {
2831 ins_start_slot = path->slots[0];
2832 ins_nr = 1;
2833 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002834 }
2835
Chris Mason31ff1cd2008-09-11 16:17:57 -04002836 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2837 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002838 if (ret) {
2839 err = ret;
2840 goto out_unlock;
2841 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002842 ins_nr = 1;
2843 ins_start_slot = path->slots[0];
2844next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04002845
Chris Mason3a5f1d42008-09-11 15:53:37 -04002846 nritems = btrfs_header_nritems(path->nodes[0]);
2847 path->slots[0]++;
2848 if (path->slots[0] < nritems) {
2849 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2850 path->slots[0]);
2851 goto again;
2852 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002853 if (ins_nr) {
2854 ret = copy_items(trans, log, dst_path, src,
2855 ins_start_slot,
2856 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002857 if (ret) {
2858 err = ret;
2859 goto out_unlock;
2860 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002861 ins_nr = 0;
2862 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002863 btrfs_release_path(root, path);
2864
Chris Masone02119d2008-09-05 16:13:11 -04002865 if (min_key.offset < (u64)-1)
2866 min_key.offset++;
2867 else if (min_key.type < (u8)-1)
2868 min_key.type++;
2869 else if (min_key.objectid < (u64)-1)
2870 min_key.objectid++;
2871 else
2872 break;
2873 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002874 if (ins_nr) {
2875 ret = copy_items(trans, log, dst_path, src,
2876 ins_start_slot,
2877 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002878 if (ret) {
2879 err = ret;
2880 goto out_unlock;
2881 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002882 ins_nr = 0;
2883 }
2884 WARN_ON(ins_nr);
Chris Mason9623f9a2008-09-11 17:42:42 -04002885 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04002886 btrfs_release_path(root, path);
2887 btrfs_release_path(log, dst_path);
2888 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002889 if (ret) {
2890 err = ret;
2891 goto out_unlock;
2892 }
Chris Masone02119d2008-09-05 16:13:11 -04002893 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002894 BTRFS_I(inode)->logged_trans = trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002895out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002896 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2897
2898 btrfs_free_path(path);
2899 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002900 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002901}
2902
Chris Mason12fcfd22009-03-24 10:24:20 -04002903/*
2904 * follow the dentry parent pointers up the chain and see if any
2905 * of the directories in it require a full commit before they can
2906 * be logged. Returns zero if nothing special needs to be done or 1 if
2907 * a full commit is required.
2908 */
2909static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
2910 struct inode *inode,
2911 struct dentry *parent,
2912 struct super_block *sb,
2913 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04002914{
Chris Mason12fcfd22009-03-24 10:24:20 -04002915 int ret = 0;
2916 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00002917 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002918
Chris Masonaf4176b2009-03-24 10:24:31 -04002919 /*
2920 * for regular files, if its inode is already on disk, we don't
2921 * have to worry about the parents at all. This is because
2922 * we can use the last_unlink_trans field to record renames
2923 * and other fun in this file.
2924 */
2925 if (S_ISREG(inode->i_mode) &&
2926 BTRFS_I(inode)->generation <= last_committed &&
2927 BTRFS_I(inode)->last_unlink_trans <= last_committed)
2928 goto out;
2929
Chris Mason12fcfd22009-03-24 10:24:20 -04002930 if (!S_ISDIR(inode->i_mode)) {
2931 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2932 goto out;
2933 inode = parent->d_inode;
2934 }
2935
2936 while (1) {
2937 BTRFS_I(inode)->logged_trans = trans->transid;
2938 smp_mb();
2939
2940 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
2941 root = BTRFS_I(inode)->root;
2942
2943 /*
2944 * make sure any commits to the log are forced
2945 * to be full commits
2946 */
2947 root->fs_info->last_trans_log_full_commit =
2948 trans->transid;
2949 ret = 1;
2950 break;
2951 }
2952
2953 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2954 break;
2955
Yan, Zheng76dda932009-09-21 16:00:26 -04002956 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04002957 break;
2958
Josef Bacik6a912212010-11-20 09:48:00 +00002959 parent = dget_parent(parent);
2960 dput(old_parent);
2961 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04002962 inode = parent->d_inode;
2963
2964 }
Josef Bacik6a912212010-11-20 09:48:00 +00002965 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04002966out:
Chris Masone02119d2008-09-05 16:13:11 -04002967 return ret;
2968}
2969
Chris Mason257c62e2009-10-13 13:21:08 -04002970static int inode_in_log(struct btrfs_trans_handle *trans,
2971 struct inode *inode)
2972{
2973 struct btrfs_root *root = BTRFS_I(inode)->root;
2974 int ret = 0;
2975
2976 mutex_lock(&root->log_mutex);
2977 if (BTRFS_I(inode)->logged_trans == trans->transid &&
2978 BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
2979 ret = 1;
2980 mutex_unlock(&root->log_mutex);
2981 return ret;
2982}
2983
2984
Chris Masone02119d2008-09-05 16:13:11 -04002985/*
2986 * helper function around btrfs_log_inode to make sure newly created
2987 * parent directories also end up in the log. A minimal inode and backref
2988 * only logging is done of any parent directories that are older than
2989 * the last committed transaction
2990 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002991int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
2992 struct btrfs_root *root, struct inode *inode,
2993 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04002994{
Chris Mason12fcfd22009-03-24 10:24:20 -04002995 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04002996 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00002997 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002998 int ret = 0;
2999 u64 last_committed = root->fs_info->last_trans_committed;
3000
3001 sb = inode->i_sb;
3002
Sage Weil3a5e1402009-04-02 16:49:40 -04003003 if (btrfs_test_opt(root, NOTREELOG)) {
3004 ret = 1;
3005 goto end_no_trans;
3006 }
3007
Chris Mason12fcfd22009-03-24 10:24:20 -04003008 if (root->fs_info->last_trans_log_full_commit >
3009 root->fs_info->last_trans_committed) {
3010 ret = 1;
3011 goto end_no_trans;
3012 }
3013
Yan, Zheng76dda932009-09-21 16:00:26 -04003014 if (root != BTRFS_I(inode)->root ||
3015 btrfs_root_refs(&root->root_item) == 0) {
3016 ret = 1;
3017 goto end_no_trans;
3018 }
3019
Chris Mason12fcfd22009-03-24 10:24:20 -04003020 ret = check_parent_dirs_for_sync(trans, inode, parent,
3021 sb, last_committed);
3022 if (ret)
3023 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003024
Chris Mason257c62e2009-10-13 13:21:08 -04003025 if (inode_in_log(trans, inode)) {
3026 ret = BTRFS_NO_LOG_SYNC;
3027 goto end_no_trans;
3028 }
3029
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003030 ret = start_log_trans(trans, root);
3031 if (ret)
3032 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003033
3034 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003035 if (ret)
3036 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003037
Chris Masonaf4176b2009-03-24 10:24:31 -04003038 /*
3039 * for regular files, if its inode is already on disk, we don't
3040 * have to worry about the parents at all. This is because
3041 * we can use the last_unlink_trans field to record renames
3042 * and other fun in this file.
3043 */
3044 if (S_ISREG(inode->i_mode) &&
3045 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003046 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3047 ret = 0;
3048 goto end_trans;
3049 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003050
3051 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003052 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003053 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003054 break;
3055
Chris Mason12fcfd22009-03-24 10:24:20 -04003056 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003057 if (root != BTRFS_I(inode)->root)
3058 break;
3059
Chris Mason12fcfd22009-03-24 10:24:20 -04003060 if (BTRFS_I(inode)->generation >
3061 root->fs_info->last_trans_committed) {
3062 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003063 if (ret)
3064 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003065 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003066 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003067 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003068
Josef Bacik6a912212010-11-20 09:48:00 +00003069 parent = dget_parent(parent);
3070 dput(old_parent);
3071 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003072 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003073 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003074end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003075 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003076 if (ret < 0) {
3077 BUG_ON(ret != -ENOSPC);
3078 root->fs_info->last_trans_log_full_commit = trans->transid;
3079 ret = 1;
3080 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003081 btrfs_end_log_trans(root);
3082end_no_trans:
3083 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003084}
3085
3086/*
3087 * it is not safe to log dentry if the chunk root has added new
3088 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3089 * If this returns 1, you must commit the transaction to safely get your
3090 * data on disk.
3091 */
3092int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3093 struct btrfs_root *root, struct dentry *dentry)
3094{
Josef Bacik6a912212010-11-20 09:48:00 +00003095 struct dentry *parent = dget_parent(dentry);
3096 int ret;
3097
3098 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3099 dput(parent);
3100
3101 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003102}
3103
3104/*
3105 * should be called during mount to recover any replay any log trees
3106 * from the FS
3107 */
3108int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3109{
3110 int ret;
3111 struct btrfs_path *path;
3112 struct btrfs_trans_handle *trans;
3113 struct btrfs_key key;
3114 struct btrfs_key found_key;
3115 struct btrfs_key tmp_key;
3116 struct btrfs_root *log;
3117 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3118 struct walk_control wc = {
3119 .process_func = process_one_buffer,
3120 .stage = 0,
3121 };
3122
Chris Masone02119d2008-09-05 16:13:11 -04003123 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003124 if (!path)
3125 return -ENOMEM;
3126
3127 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003128
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003129 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003130 BUG_ON(IS_ERR(trans));
Chris Masone02119d2008-09-05 16:13:11 -04003131
3132 wc.trans = trans;
3133 wc.pin = 1;
3134
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003135 ret = walk_log_tree(trans, log_root_tree, &wc);
3136 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003137
3138again:
3139 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3140 key.offset = (u64)-1;
3141 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3142
Chris Masond3977122009-01-05 21:25:51 -05003143 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003144 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3145 if (ret < 0)
3146 break;
3147 if (ret > 0) {
3148 if (path->slots[0] == 0)
3149 break;
3150 path->slots[0]--;
3151 }
3152 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3153 path->slots[0]);
3154 btrfs_release_path(log_root_tree, path);
3155 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3156 break;
3157
3158 log = btrfs_read_fs_root_no_radix(log_root_tree,
3159 &found_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003160 BUG_ON(IS_ERR(log));
Chris Masone02119d2008-09-05 16:13:11 -04003161
3162 tmp_key.objectid = found_key.offset;
3163 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3164 tmp_key.offset = (u64)-1;
3165
3166 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Chris Masone02119d2008-09-05 16:13:11 -04003167 BUG_ON(!wc.replay_dest);
3168
Yan Zheng07d400a2009-01-06 11:42:00 -05003169 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003170 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003171 ret = walk_log_tree(trans, log, &wc);
3172 BUG_ON(ret);
3173
3174 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3175 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3176 path);
3177 BUG_ON(ret);
3178 }
Chris Masone02119d2008-09-05 16:13:11 -04003179
3180 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003181 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003182 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003183 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003184 kfree(log);
3185
3186 if (found_key.offset == 0)
3187 break;
3188 }
3189 btrfs_release_path(log_root_tree, path);
3190
3191 /* step one is to pin it all, step two is to replay just inodes */
3192 if (wc.pin) {
3193 wc.pin = 0;
3194 wc.process_func = replay_one_buffer;
3195 wc.stage = LOG_WALK_REPLAY_INODES;
3196 goto again;
3197 }
3198 /* step three is to replay everything */
3199 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3200 wc.stage++;
3201 goto again;
3202 }
3203
3204 btrfs_free_path(path);
3205
3206 free_extent_buffer(log_root_tree->node);
3207 log_root_tree->log_root = NULL;
3208 fs_info->log_root_recovering = 0;
3209
3210 /* step 4: commit the transaction, which also unpins the blocks */
3211 btrfs_commit_transaction(trans, fs_info->tree_root);
3212
3213 kfree(log_root_tree);
3214 return 0;
3215}
Chris Mason12fcfd22009-03-24 10:24:20 -04003216
3217/*
3218 * there are some corner cases where we want to force a full
3219 * commit instead of allowing a directory to be logged.
3220 *
3221 * They revolve around files there were unlinked from the directory, and
3222 * this function updates the parent directory so that a full commit is
3223 * properly done if it is fsync'd later after the unlinks are done.
3224 */
3225void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3226 struct inode *dir, struct inode *inode,
3227 int for_rename)
3228{
3229 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003230 * when we're logging a file, if it hasn't been renamed
3231 * or unlinked, and its inode is fully committed on disk,
3232 * we don't have to worry about walking up the directory chain
3233 * to log its parents.
3234 *
3235 * So, we use the last_unlink_trans field to put this transid
3236 * into the file. When the file is logged we check it and
3237 * don't log the parents if the file is fully on disk.
3238 */
3239 if (S_ISREG(inode->i_mode))
3240 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3241
3242 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003243 * if this directory was already logged any new
3244 * names for this file/dir will get recorded
3245 */
3246 smp_mb();
3247 if (BTRFS_I(dir)->logged_trans == trans->transid)
3248 return;
3249
3250 /*
3251 * if the inode we're about to unlink was logged,
3252 * the log will be properly updated for any new names
3253 */
3254 if (BTRFS_I(inode)->logged_trans == trans->transid)
3255 return;
3256
3257 /*
3258 * when renaming files across directories, if the directory
3259 * there we're unlinking from gets fsync'd later on, there's
3260 * no way to find the destination directory later and fsync it
3261 * properly. So, we have to be conservative and force commits
3262 * so the new name gets discovered.
3263 */
3264 if (for_rename)
3265 goto record;
3266
3267 /* we can safely do the unlink without any special recording */
3268 return;
3269
3270record:
3271 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3272}
3273
3274/*
3275 * Call this after adding a new name for a file and it will properly
3276 * update the log to reflect the new name.
3277 *
3278 * It will return zero if all goes well, and it will return 1 if a
3279 * full transaction commit is required.
3280 */
3281int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3282 struct inode *inode, struct inode *old_dir,
3283 struct dentry *parent)
3284{
3285 struct btrfs_root * root = BTRFS_I(inode)->root;
3286
3287 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003288 * this will force the logging code to walk the dentry chain
3289 * up for the file
3290 */
3291 if (S_ISREG(inode->i_mode))
3292 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3293
3294 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003295 * if this inode hasn't been logged and directory we're renaming it
3296 * from hasn't been logged, we don't need to log it
3297 */
3298 if (BTRFS_I(inode)->logged_trans <=
3299 root->fs_info->last_trans_committed &&
3300 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3301 root->fs_info->last_trans_committed))
3302 return 0;
3303
3304 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3305}
3306