blob: cf2baeb70462f3301afb37a5eafc264ec9d65d51 [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 BUG_ON(ret);
386 }
387 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400388 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400389 }
390 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
391 path->slots[0]);
392
393 /* don't overwrite an existing inode if the generation number
394 * was logged as zero. This is done when the tree logging code
395 * is just logging an inode to make sure it exists after recovery.
396 *
397 * Also, don't overwrite i_size on directories during replay.
398 * log replay inserts and removes directory items based on the
399 * state of the tree found in the subvolume, and i_size is modified
400 * as it goes
401 */
402 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
403 struct btrfs_inode_item *src_item;
404 struct btrfs_inode_item *dst_item;
405
406 src_item = (struct btrfs_inode_item *)src_ptr;
407 dst_item = (struct btrfs_inode_item *)dst_ptr;
408
409 if (btrfs_inode_generation(eb, src_item) == 0)
410 goto no_copy;
411
412 if (overwrite_root &&
413 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
414 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
415 save_old_i_size = 1;
416 saved_i_size = btrfs_inode_size(path->nodes[0],
417 dst_item);
418 }
419 }
420
421 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
422 src_ptr, item_size);
423
424 if (save_old_i_size) {
425 struct btrfs_inode_item *dst_item;
426 dst_item = (struct btrfs_inode_item *)dst_ptr;
427 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
428 }
429
430 /* make sure the generation is filled in */
431 if (key->type == BTRFS_INODE_ITEM_KEY) {
432 struct btrfs_inode_item *dst_item;
433 dst_item = (struct btrfs_inode_item *)dst_ptr;
434 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
435 btrfs_set_inode_generation(path->nodes[0], dst_item,
436 trans->transid);
437 }
438 }
439no_copy:
440 btrfs_mark_buffer_dirty(path->nodes[0]);
441 btrfs_release_path(root, path);
442 return 0;
443}
444
445/*
446 * simple helper to read an inode off the disk from a given root
447 * This can only be called for subvolume roots and not for the log
448 */
449static noinline struct inode *read_one_inode(struct btrfs_root *root,
450 u64 objectid)
451{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400452 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400453 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400454
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400455 key.objectid = objectid;
456 key.type = BTRFS_INODE_ITEM_KEY;
457 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000458 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400459 if (IS_ERR(inode)) {
460 inode = NULL;
461 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400462 iput(inode);
463 inode = NULL;
464 }
465 return inode;
466}
467
468/* replays a single extent in 'eb' at 'slot' with 'key' into the
469 * subvolume 'root'. path is released on entry and should be released
470 * on exit.
471 *
472 * extents in the log tree have not been allocated out of the extent
473 * tree yet. So, this completes the allocation, taking a reference
474 * as required if the extent already exists or creating a new extent
475 * if it isn't in the extent allocation tree yet.
476 *
477 * The extent is inserted into the file, dropping any existing extents
478 * from the file that overlap the new one.
479 */
480static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
481 struct btrfs_root *root,
482 struct btrfs_path *path,
483 struct extent_buffer *eb, int slot,
484 struct btrfs_key *key)
485{
486 int found_type;
487 u64 mask = root->sectorsize - 1;
488 u64 extent_end;
489 u64 alloc_hint;
490 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500491 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400492 struct btrfs_file_extent_item *item;
493 struct inode *inode = NULL;
494 unsigned long size;
495 int ret = 0;
496
497 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
498 found_type = btrfs_file_extent_type(eb, item);
499
Yan Zhengd899e052008-10-30 14:25:28 -0400500 if (found_type == BTRFS_FILE_EXTENT_REG ||
501 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400502 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
503 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400504 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400505 extent_end = (start + size + mask) & ~mask;
506 } else {
507 ret = 0;
508 goto out;
509 }
510
511 inode = read_one_inode(root, key->objectid);
512 if (!inode) {
513 ret = -EIO;
514 goto out;
515 }
516
517 /*
518 * first check to see if we already have this extent in the
519 * file. This must be done before the btrfs_drop_extents run
520 * so we don't try to drop this extent.
521 */
522 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
523 start, 0);
524
Yan Zhengd899e052008-10-30 14:25:28 -0400525 if (ret == 0 &&
526 (found_type == BTRFS_FILE_EXTENT_REG ||
527 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400528 struct btrfs_file_extent_item cmp1;
529 struct btrfs_file_extent_item cmp2;
530 struct btrfs_file_extent_item *existing;
531 struct extent_buffer *leaf;
532
533 leaf = path->nodes[0];
534 existing = btrfs_item_ptr(leaf, path->slots[0],
535 struct btrfs_file_extent_item);
536
537 read_extent_buffer(eb, &cmp1, (unsigned long)item,
538 sizeof(cmp1));
539 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
540 sizeof(cmp2));
541
542 /*
543 * we already have a pointer to this exact extent,
544 * we don't have to do anything
545 */
546 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
547 btrfs_release_path(root, path);
548 goto out;
549 }
550 }
551 btrfs_release_path(root, path);
552
Yan Zheng07d400a2009-01-06 11:42:00 -0500553 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400554 /* drop any overlapping extents */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000555 ret = btrfs_drop_extents(trans, inode, start, extent_end,
556 &alloc_hint, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400557 BUG_ON(ret);
558
Yan Zheng07d400a2009-01-06 11:42:00 -0500559 if (found_type == BTRFS_FILE_EXTENT_REG ||
560 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400561 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500562 unsigned long dest_offset;
563 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400564
Yan Zheng07d400a2009-01-06 11:42:00 -0500565 ret = btrfs_insert_empty_item(trans, root, path, key,
566 sizeof(*item));
567 BUG_ON(ret);
568 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
569 path->slots[0]);
570 copy_extent_buffer(path->nodes[0], eb, dest_offset,
571 (unsigned long)item, sizeof(*item));
572
573 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
574 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
575 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400576 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500577
578 if (ins.objectid > 0) {
579 u64 csum_start;
580 u64 csum_end;
581 LIST_HEAD(ordered_sums);
582 /*
583 * is this extent already allocated in the extent
584 * allocation tree? If so, just add a reference
585 */
586 ret = btrfs_lookup_extent(root, ins.objectid,
587 ins.offset);
588 if (ret == 0) {
589 ret = btrfs_inc_extent_ref(trans, root,
590 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400591 0, root->root_key.objectid,
592 key->objectid, offset);
Yan Zheng07d400a2009-01-06 11:42:00 -0500593 } else {
594 /*
595 * insert the extent pointer in the extent
596 * allocation tree
597 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 ret = btrfs_alloc_logged_file_extent(trans,
599 root, root->root_key.objectid,
600 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500601 BUG_ON(ret);
602 }
603 btrfs_release_path(root, path);
604
605 if (btrfs_file_extent_compression(eb, item)) {
606 csum_start = ins.objectid;
607 csum_end = csum_start + ins.offset;
608 } else {
609 csum_start = ins.objectid +
610 btrfs_file_extent_offset(eb, item);
611 csum_end = csum_start +
612 btrfs_file_extent_num_bytes(eb, item);
613 }
614
615 ret = btrfs_lookup_csums_range(root->log_root,
616 csum_start, csum_end - 1,
617 &ordered_sums);
618 BUG_ON(ret);
619 while (!list_empty(&ordered_sums)) {
620 struct btrfs_ordered_sum *sums;
621 sums = list_entry(ordered_sums.next,
622 struct btrfs_ordered_sum,
623 list);
624 ret = btrfs_csum_file_blocks(trans,
625 root->fs_info->csum_root,
626 sums);
627 BUG_ON(ret);
628 list_del(&sums->list);
629 kfree(sums);
630 }
631 } else {
632 btrfs_release_path(root, path);
633 }
634 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
635 /* inline extents are easy, we just overwrite them */
636 ret = overwrite_item(trans, root, path, eb, slot, key);
637 BUG_ON(ret);
638 }
639
640 inode_set_bytes(inode, saved_nbytes);
Chris Masone02119d2008-09-05 16:13:11 -0400641 btrfs_update_inode(trans, root, inode);
642out:
643 if (inode)
644 iput(inode);
645 return ret;
646}
647
648/*
649 * when cleaning up conflicts between the directory names in the
650 * subvolume, directory names in the log and directory names in the
651 * inode back references, we may have to unlink inodes from directories.
652 *
653 * This is a helper function to do the unlink of a specific directory
654 * item
655 */
656static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
657 struct btrfs_root *root,
658 struct btrfs_path *path,
659 struct inode *dir,
660 struct btrfs_dir_item *di)
661{
662 struct inode *inode;
663 char *name;
664 int name_len;
665 struct extent_buffer *leaf;
666 struct btrfs_key location;
667 int ret;
668
669 leaf = path->nodes[0];
670
671 btrfs_dir_item_key_to_cpu(leaf, di, &location);
672 name_len = btrfs_dir_name_len(leaf, di);
673 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000674 if (!name)
675 return -ENOMEM;
676
Chris Masone02119d2008-09-05 16:13:11 -0400677 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
678 btrfs_release_path(root, path);
679
680 inode = read_one_inode(root, location.objectid);
681 BUG_ON(!inode);
682
Yan Zhengec051c02009-01-05 15:43:42 -0500683 ret = link_to_fixup_dir(trans, root, path, location.objectid);
684 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400685
Chris Masone02119d2008-09-05 16:13:11 -0400686 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500687 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400688 kfree(name);
689
690 iput(inode);
691 return ret;
692}
693
694/*
695 * helper function to see if a given name and sequence number found
696 * in an inode back reference are already in a directory and correctly
697 * point to this inode
698 */
699static noinline int inode_in_dir(struct btrfs_root *root,
700 struct btrfs_path *path,
701 u64 dirid, u64 objectid, u64 index,
702 const char *name, int name_len)
703{
704 struct btrfs_dir_item *di;
705 struct btrfs_key location;
706 int match = 0;
707
708 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
709 index, name, name_len, 0);
710 if (di && !IS_ERR(di)) {
711 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
712 if (location.objectid != objectid)
713 goto out;
714 } else
715 goto out;
716 btrfs_release_path(root, path);
717
718 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
719 if (di && !IS_ERR(di)) {
720 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
721 if (location.objectid != objectid)
722 goto out;
723 } else
724 goto out;
725 match = 1;
726out:
727 btrfs_release_path(root, path);
728 return match;
729}
730
731/*
732 * helper function to check a log tree for a named back reference in
733 * an inode. This is used to decide if a back reference that is
734 * found in the subvolume conflicts with what we find in the log.
735 *
736 * inode backreferences may have multiple refs in a single item,
737 * during replay we process one reference at a time, and we don't
738 * want to delete valid links to a file from the subvolume if that
739 * link is also in the log.
740 */
741static noinline int backref_in_log(struct btrfs_root *log,
742 struct btrfs_key *key,
743 char *name, int namelen)
744{
745 struct btrfs_path *path;
746 struct btrfs_inode_ref *ref;
747 unsigned long ptr;
748 unsigned long ptr_end;
749 unsigned long name_ptr;
750 int found_name_len;
751 int item_size;
752 int ret;
753 int match = 0;
754
755 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000756 if (!path)
757 return -ENOMEM;
758
Chris Masone02119d2008-09-05 16:13:11 -0400759 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
760 if (ret != 0)
761 goto out;
762
763 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
764 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
765 ptr_end = ptr + item_size;
766 while (ptr < ptr_end) {
767 ref = (struct btrfs_inode_ref *)ptr;
768 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
769 if (found_name_len == namelen) {
770 name_ptr = (unsigned long)(ref + 1);
771 ret = memcmp_extent_buffer(path->nodes[0], name,
772 name_ptr, namelen);
773 if (ret == 0) {
774 match = 1;
775 goto out;
776 }
777 }
778 ptr = (unsigned long)(ref + 1) + found_name_len;
779 }
780out:
781 btrfs_free_path(path);
782 return match;
783}
784
785
786/*
787 * replay one inode back reference item found in the log tree.
788 * eb, slot and key refer to the buffer and key found in the log tree.
789 * root is the destination we are replaying into, and path is for temp
790 * use by this function. (it should be released on return).
791 */
792static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
793 struct btrfs_root *root,
794 struct btrfs_root *log,
795 struct btrfs_path *path,
796 struct extent_buffer *eb, int slot,
797 struct btrfs_key *key)
798{
799 struct inode *dir;
800 int ret;
Chris Masone02119d2008-09-05 16:13:11 -0400801 struct btrfs_inode_ref *ref;
Chris Masone02119d2008-09-05 16:13:11 -0400802 struct inode *inode;
803 char *name;
804 int namelen;
805 unsigned long ref_ptr;
806 unsigned long ref_end;
liuboc622ae62011-03-26 08:01:12 -0400807 int search_done = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400808
Chris Masone02119d2008-09-05 16:13:11 -0400809 /*
810 * it is possible that we didn't log all the parent directories
811 * for a given inode. If we don't find the dir, just don't
812 * copy the back ref in. The link count fixup code will take
813 * care of the rest
814 */
815 dir = read_one_inode(root, key->offset);
816 if (!dir)
817 return -ENOENT;
818
819 inode = read_one_inode(root, key->objectid);
Julia Lawall631c07c2009-07-27 13:57:00 -0400820 BUG_ON(!inode);
Chris Masone02119d2008-09-05 16:13:11 -0400821
822 ref_ptr = btrfs_item_ptr_offset(eb, slot);
823 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
824
825again:
826 ref = (struct btrfs_inode_ref *)ref_ptr;
827
828 namelen = btrfs_inode_ref_name_len(eb, ref);
829 name = kmalloc(namelen, GFP_NOFS);
830 BUG_ON(!name);
831
832 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
833
834 /* if we already have a perfect match, we're done */
835 if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
836 btrfs_inode_ref_index(eb, ref),
837 name, namelen)) {
838 goto out;
839 }
840
841 /*
842 * look for a conflicting back reference in the metadata.
843 * if we find one we have to unlink that name of the file
844 * before we add our new link. Later on, we overwrite any
845 * existing back reference, and we don't want to create
846 * dangling pointers in the directory.
847 */
liuboc622ae62011-03-26 08:01:12 -0400848
849 if (search_done)
850 goto insert;
851
Chris Masone02119d2008-09-05 16:13:11 -0400852 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
853 if (ret == 0) {
854 char *victim_name;
855 int victim_name_len;
856 struct btrfs_inode_ref *victim_ref;
857 unsigned long ptr;
858 unsigned long ptr_end;
859 struct extent_buffer *leaf = path->nodes[0];
860
861 /* are we trying to overwrite a back ref for the root directory
862 * if so, just jump out, we're done
863 */
864 if (key->objectid == key->offset)
865 goto out_nowrite;
866
867 /* check all the names in this back reference to see
868 * if they are in the log. if so, we allow them to stay
869 * otherwise they must be unlinked as a conflict
870 */
871 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
872 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500873 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400874 victim_ref = (struct btrfs_inode_ref *)ptr;
875 victim_name_len = btrfs_inode_ref_name_len(leaf,
876 victim_ref);
877 victim_name = kmalloc(victim_name_len, GFP_NOFS);
878 BUG_ON(!victim_name);
879
880 read_extent_buffer(leaf, victim_name,
881 (unsigned long)(victim_ref + 1),
882 victim_name_len);
883
884 if (!backref_in_log(log, key, victim_name,
885 victim_name_len)) {
886 btrfs_inc_nlink(inode);
887 btrfs_release_path(root, path);
Chris Mason12fcfd22009-03-24 10:24:20 -0400888
Chris Masone02119d2008-09-05 16:13:11 -0400889 ret = btrfs_unlink_inode(trans, root, dir,
890 inode, victim_name,
891 victim_name_len);
Chris Masone02119d2008-09-05 16:13:11 -0400892 }
893 kfree(victim_name);
894 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
895 }
896 BUG_ON(ret);
liuboc622ae62011-03-26 08:01:12 -0400897
898 /*
899 * NOTE: we have searched root tree and checked the
900 * coresponding ref, it does not need to check again.
901 */
902 search_done = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400903 }
904 btrfs_release_path(root, path);
905
liuboc622ae62011-03-26 08:01:12 -0400906insert:
Chris Masone02119d2008-09-05 16:13:11 -0400907 /* insert our name */
908 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
909 btrfs_inode_ref_index(eb, ref));
910 BUG_ON(ret);
911
912 btrfs_update_inode(trans, root, inode);
913
914out:
915 ref_ptr = (unsigned long)(ref + 1) + namelen;
916 kfree(name);
917 if (ref_ptr < ref_end)
918 goto again;
919
920 /* finally write the back reference in the inode */
921 ret = overwrite_item(trans, root, path, eb, slot, key);
922 BUG_ON(ret);
923
924out_nowrite:
925 btrfs_release_path(root, path);
926 iput(dir);
927 iput(inode);
928 return 0;
929}
930
Yan, Zhengc71bf092009-11-12 09:34:40 +0000931static int insert_orphan_item(struct btrfs_trans_handle *trans,
932 struct btrfs_root *root, u64 offset)
933{
934 int ret;
935 ret = btrfs_find_orphan_item(root, offset);
936 if (ret > 0)
937 ret = btrfs_insert_orphan_item(trans, root, offset);
938 return ret;
939}
940
941
Chris Masone02119d2008-09-05 16:13:11 -0400942/*
Chris Masone02119d2008-09-05 16:13:11 -0400943 * There are a few corners where the link count of the file can't
944 * be properly maintained during replay. So, instead of adding
945 * lots of complexity to the log code, we just scan the backrefs
946 * for any file that has been through replay.
947 *
948 * The scan will update the link count on the inode to reflect the
949 * number of back refs found. If it goes down to zero, the iput
950 * will free the inode.
951 */
952static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
953 struct btrfs_root *root,
954 struct inode *inode)
955{
956 struct btrfs_path *path;
957 int ret;
958 struct btrfs_key key;
959 u64 nlink = 0;
960 unsigned long ptr;
961 unsigned long ptr_end;
962 int name_len;
963
964 key.objectid = inode->i_ino;
965 key.type = BTRFS_INODE_REF_KEY;
966 key.offset = (u64)-1;
967
968 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000969 if (!path)
970 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -0400971
Chris Masond3977122009-01-05 21:25:51 -0500972 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -0400973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
974 if (ret < 0)
975 break;
976 if (ret > 0) {
977 if (path->slots[0] == 0)
978 break;
979 path->slots[0]--;
980 }
981 btrfs_item_key_to_cpu(path->nodes[0], &key,
982 path->slots[0]);
983 if (key.objectid != inode->i_ino ||
984 key.type != BTRFS_INODE_REF_KEY)
985 break;
986 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
987 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
988 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500989 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400990 struct btrfs_inode_ref *ref;
991
992 ref = (struct btrfs_inode_ref *)ptr;
993 name_len = btrfs_inode_ref_name_len(path->nodes[0],
994 ref);
995 ptr = (unsigned long)(ref + 1) + name_len;
996 nlink++;
997 }
998
999 if (key.offset == 0)
1000 break;
1001 key.offset--;
1002 btrfs_release_path(root, path);
1003 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001004 btrfs_release_path(root, path);
Chris Masone02119d2008-09-05 16:13:11 -04001005 if (nlink != inode->i_nlink) {
1006 inode->i_nlink = nlink;
1007 btrfs_update_inode(trans, root, inode);
1008 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001009 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001010
Yan, Zhengc71bf092009-11-12 09:34:40 +00001011 if (inode->i_nlink == 0) {
1012 if (S_ISDIR(inode->i_mode)) {
1013 ret = replay_dir_deletes(trans, root, NULL, path,
1014 inode->i_ino, 1);
1015 BUG_ON(ret);
1016 }
1017 ret = insert_orphan_item(trans, root, inode->i_ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001018 BUG_ON(ret);
1019 }
1020 btrfs_free_path(path);
1021
Chris Masone02119d2008-09-05 16:13:11 -04001022 return 0;
1023}
1024
1025static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1026 struct btrfs_root *root,
1027 struct btrfs_path *path)
1028{
1029 int ret;
1030 struct btrfs_key key;
1031 struct inode *inode;
1032
1033 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1034 key.type = BTRFS_ORPHAN_ITEM_KEY;
1035 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001036 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001037 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1038 if (ret < 0)
1039 break;
1040
1041 if (ret == 1) {
1042 if (path->slots[0] == 0)
1043 break;
1044 path->slots[0]--;
1045 }
1046
1047 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1048 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1049 key.type != BTRFS_ORPHAN_ITEM_KEY)
1050 break;
1051
1052 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001053 if (ret)
1054 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001055
1056 btrfs_release_path(root, path);
1057 inode = read_one_inode(root, key.offset);
1058 BUG_ON(!inode);
1059
1060 ret = fixup_inode_link_count(trans, root, inode);
1061 BUG_ON(ret);
1062
1063 iput(inode);
1064
Chris Mason12fcfd22009-03-24 10:24:20 -04001065 /*
1066 * fixup on a directory may create new entries,
1067 * make sure we always look for the highset possible
1068 * offset
1069 */
1070 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001071 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001072 ret = 0;
1073out:
Chris Masone02119d2008-09-05 16:13:11 -04001074 btrfs_release_path(root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001075 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001076}
1077
1078
1079/*
1080 * record a given inode in the fixup dir so we can check its link
1081 * count when replay is done. The link count is incremented here
1082 * so the inode won't go away until we check it
1083 */
1084static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1085 struct btrfs_root *root,
1086 struct btrfs_path *path,
1087 u64 objectid)
1088{
1089 struct btrfs_key key;
1090 int ret = 0;
1091 struct inode *inode;
1092
1093 inode = read_one_inode(root, objectid);
1094 BUG_ON(!inode);
1095
1096 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1097 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1098 key.offset = objectid;
1099
1100 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1101
1102 btrfs_release_path(root, path);
1103 if (ret == 0) {
1104 btrfs_inc_nlink(inode);
1105 btrfs_update_inode(trans, root, inode);
1106 } else if (ret == -EEXIST) {
1107 ret = 0;
1108 } else {
1109 BUG();
1110 }
1111 iput(inode);
1112
1113 return ret;
1114}
1115
1116/*
1117 * when replaying the log for a directory, we only insert names
1118 * for inodes that actually exist. This means an fsync on a directory
1119 * does not implicitly fsync all the new files in it
1120 */
1121static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1122 struct btrfs_root *root,
1123 struct btrfs_path *path,
1124 u64 dirid, u64 index,
1125 char *name, int name_len, u8 type,
1126 struct btrfs_key *location)
1127{
1128 struct inode *inode;
1129 struct inode *dir;
1130 int ret;
1131
1132 inode = read_one_inode(root, location->objectid);
1133 if (!inode)
1134 return -ENOENT;
1135
1136 dir = read_one_inode(root, dirid);
1137 if (!dir) {
1138 iput(inode);
1139 return -EIO;
1140 }
1141 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1142
1143 /* FIXME, put inode into FIXUP list */
1144
1145 iput(inode);
1146 iput(dir);
1147 return ret;
1148}
1149
1150/*
1151 * take a single entry in a log directory item and replay it into
1152 * the subvolume.
1153 *
1154 * if a conflicting item exists in the subdirectory already,
1155 * the inode it points to is unlinked and put into the link count
1156 * fix up tree.
1157 *
1158 * If a name from the log points to a file or directory that does
1159 * not exist in the FS, it is skipped. fsyncs on directories
1160 * do not force down inodes inside that directory, just changes to the
1161 * names or unlinks in a directory.
1162 */
1163static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1164 struct btrfs_root *root,
1165 struct btrfs_path *path,
1166 struct extent_buffer *eb,
1167 struct btrfs_dir_item *di,
1168 struct btrfs_key *key)
1169{
1170 char *name;
1171 int name_len;
1172 struct btrfs_dir_item *dst_di;
1173 struct btrfs_key found_key;
1174 struct btrfs_key log_key;
1175 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001176 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001177 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001178 int ret;
1179
1180 dir = read_one_inode(root, key->objectid);
1181 BUG_ON(!dir);
1182
1183 name_len = btrfs_dir_name_len(eb, di);
1184 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001185 if (!name)
1186 return -ENOMEM;
1187
Chris Masone02119d2008-09-05 16:13:11 -04001188 log_type = btrfs_dir_type(eb, di);
1189 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1190 name_len);
1191
1192 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001193 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1194 if (exists == 0)
1195 exists = 1;
1196 else
1197 exists = 0;
1198 btrfs_release_path(root, path);
1199
Chris Masone02119d2008-09-05 16:13:11 -04001200 if (key->type == BTRFS_DIR_ITEM_KEY) {
1201 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1202 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001203 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001204 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1205 key->objectid,
1206 key->offset, name,
1207 name_len, 1);
1208 } else {
1209 BUG();
1210 }
1211 if (!dst_di || IS_ERR(dst_di)) {
1212 /* we need a sequence number to insert, so we only
1213 * do inserts for the BTRFS_DIR_INDEX_KEY types
1214 */
1215 if (key->type != BTRFS_DIR_INDEX_KEY)
1216 goto out;
1217 goto insert;
1218 }
1219
1220 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1221 /* the existing item matches the logged item */
1222 if (found_key.objectid == log_key.objectid &&
1223 found_key.type == log_key.type &&
1224 found_key.offset == log_key.offset &&
1225 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1226 goto out;
1227 }
1228
1229 /*
1230 * don't drop the conflicting directory entry if the inode
1231 * for the new entry doesn't exist
1232 */
Chris Mason4bef0842008-09-08 11:18:08 -04001233 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001234 goto out;
1235
Chris Masone02119d2008-09-05 16:13:11 -04001236 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1237 BUG_ON(ret);
1238
1239 if (key->type == BTRFS_DIR_INDEX_KEY)
1240 goto insert;
1241out:
1242 btrfs_release_path(root, path);
1243 kfree(name);
1244 iput(dir);
1245 return 0;
1246
1247insert:
1248 btrfs_release_path(root, path);
1249 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1250 name, name_len, log_type, &log_key);
1251
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001252 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001253 goto out;
1254}
1255
1256/*
1257 * find all the names in a directory item and reconcile them into
1258 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1259 * one name in a directory item, but the same code gets used for
1260 * both directory index types
1261 */
1262static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1263 struct btrfs_root *root,
1264 struct btrfs_path *path,
1265 struct extent_buffer *eb, int slot,
1266 struct btrfs_key *key)
1267{
1268 int ret;
1269 u32 item_size = btrfs_item_size_nr(eb, slot);
1270 struct btrfs_dir_item *di;
1271 int name_len;
1272 unsigned long ptr;
1273 unsigned long ptr_end;
1274
1275 ptr = btrfs_item_ptr_offset(eb, slot);
1276 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001277 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001278 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001279 if (verify_dir_item(root, eb, di))
1280 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001281 name_len = btrfs_dir_name_len(eb, di);
1282 ret = replay_one_name(trans, root, path, eb, di, key);
1283 BUG_ON(ret);
1284 ptr = (unsigned long)(di + 1);
1285 ptr += name_len;
1286 }
1287 return 0;
1288}
1289
1290/*
1291 * directory replay has two parts. There are the standard directory
1292 * items in the log copied from the subvolume, and range items
1293 * created in the log while the subvolume was logged.
1294 *
1295 * The range items tell us which parts of the key space the log
1296 * is authoritative for. During replay, if a key in the subvolume
1297 * directory is in a logged range item, but not actually in the log
1298 * that means it was deleted from the directory before the fsync
1299 * and should be removed.
1300 */
1301static noinline int find_dir_range(struct btrfs_root *root,
1302 struct btrfs_path *path,
1303 u64 dirid, int key_type,
1304 u64 *start_ret, u64 *end_ret)
1305{
1306 struct btrfs_key key;
1307 u64 found_end;
1308 struct btrfs_dir_log_item *item;
1309 int ret;
1310 int nritems;
1311
1312 if (*start_ret == (u64)-1)
1313 return 1;
1314
1315 key.objectid = dirid;
1316 key.type = key_type;
1317 key.offset = *start_ret;
1318
1319 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1320 if (ret < 0)
1321 goto out;
1322 if (ret > 0) {
1323 if (path->slots[0] == 0)
1324 goto out;
1325 path->slots[0]--;
1326 }
1327 if (ret != 0)
1328 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1329
1330 if (key.type != key_type || key.objectid != dirid) {
1331 ret = 1;
1332 goto next;
1333 }
1334 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1335 struct btrfs_dir_log_item);
1336 found_end = btrfs_dir_log_end(path->nodes[0], item);
1337
1338 if (*start_ret >= key.offset && *start_ret <= found_end) {
1339 ret = 0;
1340 *start_ret = key.offset;
1341 *end_ret = found_end;
1342 goto out;
1343 }
1344 ret = 1;
1345next:
1346 /* check the next slot in the tree to see if it is a valid item */
1347 nritems = btrfs_header_nritems(path->nodes[0]);
1348 if (path->slots[0] >= nritems) {
1349 ret = btrfs_next_leaf(root, path);
1350 if (ret)
1351 goto out;
1352 } else {
1353 path->slots[0]++;
1354 }
1355
1356 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1357
1358 if (key.type != key_type || key.objectid != dirid) {
1359 ret = 1;
1360 goto out;
1361 }
1362 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1363 struct btrfs_dir_log_item);
1364 found_end = btrfs_dir_log_end(path->nodes[0], item);
1365 *start_ret = key.offset;
1366 *end_ret = found_end;
1367 ret = 0;
1368out:
1369 btrfs_release_path(root, path);
1370 return ret;
1371}
1372
1373/*
1374 * this looks for a given directory item in the log. If the directory
1375 * item is not in the log, the item is removed and the inode it points
1376 * to is unlinked
1377 */
1378static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1379 struct btrfs_root *root,
1380 struct btrfs_root *log,
1381 struct btrfs_path *path,
1382 struct btrfs_path *log_path,
1383 struct inode *dir,
1384 struct btrfs_key *dir_key)
1385{
1386 int ret;
1387 struct extent_buffer *eb;
1388 int slot;
1389 u32 item_size;
1390 struct btrfs_dir_item *di;
1391 struct btrfs_dir_item *log_di;
1392 int name_len;
1393 unsigned long ptr;
1394 unsigned long ptr_end;
1395 char *name;
1396 struct inode *inode;
1397 struct btrfs_key location;
1398
1399again:
1400 eb = path->nodes[0];
1401 slot = path->slots[0];
1402 item_size = btrfs_item_size_nr(eb, slot);
1403 ptr = btrfs_item_ptr_offset(eb, slot);
1404 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001405 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001406 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001407 if (verify_dir_item(root, eb, di)) {
1408 ret = -EIO;
1409 goto out;
1410 }
1411
Chris Masone02119d2008-09-05 16:13:11 -04001412 name_len = btrfs_dir_name_len(eb, di);
1413 name = kmalloc(name_len, GFP_NOFS);
1414 if (!name) {
1415 ret = -ENOMEM;
1416 goto out;
1417 }
1418 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1419 name_len);
1420 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001421 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001422 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1423 dir_key->objectid,
1424 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001425 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001426 log_di = btrfs_lookup_dir_index_item(trans, log,
1427 log_path,
1428 dir_key->objectid,
1429 dir_key->offset,
1430 name, name_len, 0);
1431 }
1432 if (!log_di || IS_ERR(log_di)) {
1433 btrfs_dir_item_key_to_cpu(eb, di, &location);
1434 btrfs_release_path(root, path);
1435 btrfs_release_path(log, log_path);
1436 inode = read_one_inode(root, location.objectid);
1437 BUG_ON(!inode);
1438
1439 ret = link_to_fixup_dir(trans, root,
1440 path, location.objectid);
1441 BUG_ON(ret);
1442 btrfs_inc_nlink(inode);
1443 ret = btrfs_unlink_inode(trans, root, dir, inode,
1444 name, name_len);
1445 BUG_ON(ret);
1446 kfree(name);
1447 iput(inode);
1448
1449 /* there might still be more names under this key
1450 * check and repeat if required
1451 */
1452 ret = btrfs_search_slot(NULL, root, dir_key, path,
1453 0, 0);
1454 if (ret == 0)
1455 goto again;
1456 ret = 0;
1457 goto out;
1458 }
1459 btrfs_release_path(log, log_path);
1460 kfree(name);
1461
1462 ptr = (unsigned long)(di + 1);
1463 ptr += name_len;
1464 }
1465 ret = 0;
1466out:
1467 btrfs_release_path(root, path);
1468 btrfs_release_path(log, log_path);
1469 return ret;
1470}
1471
1472/*
1473 * deletion replay happens before we copy any new directory items
1474 * out of the log or out of backreferences from inodes. It
1475 * scans the log to find ranges of keys that log is authoritative for,
1476 * and then scans the directory to find items in those ranges that are
1477 * not present in the log.
1478 *
1479 * Anything we don't find in the log is unlinked and removed from the
1480 * directory.
1481 */
1482static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1483 struct btrfs_root *root,
1484 struct btrfs_root *log,
1485 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001486 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001487{
1488 u64 range_start;
1489 u64 range_end;
1490 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1491 int ret = 0;
1492 struct btrfs_key dir_key;
1493 struct btrfs_key found_key;
1494 struct btrfs_path *log_path;
1495 struct inode *dir;
1496
1497 dir_key.objectid = dirid;
1498 dir_key.type = BTRFS_DIR_ITEM_KEY;
1499 log_path = btrfs_alloc_path();
1500 if (!log_path)
1501 return -ENOMEM;
1502
1503 dir = read_one_inode(root, dirid);
1504 /* it isn't an error if the inode isn't there, that can happen
1505 * because we replay the deletes before we copy in the inode item
1506 * from the log
1507 */
1508 if (!dir) {
1509 btrfs_free_path(log_path);
1510 return 0;
1511 }
1512again:
1513 range_start = 0;
1514 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001515 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001516 if (del_all)
1517 range_end = (u64)-1;
1518 else {
1519 ret = find_dir_range(log, path, dirid, key_type,
1520 &range_start, &range_end);
1521 if (ret != 0)
1522 break;
1523 }
Chris Masone02119d2008-09-05 16:13:11 -04001524
1525 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001526 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001527 int nritems;
1528 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1529 0, 0);
1530 if (ret < 0)
1531 goto out;
1532
1533 nritems = btrfs_header_nritems(path->nodes[0]);
1534 if (path->slots[0] >= nritems) {
1535 ret = btrfs_next_leaf(root, path);
1536 if (ret)
1537 break;
1538 }
1539 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1540 path->slots[0]);
1541 if (found_key.objectid != dirid ||
1542 found_key.type != dir_key.type)
1543 goto next_type;
1544
1545 if (found_key.offset > range_end)
1546 break;
1547
1548 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001549 log_path, dir,
1550 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001551 BUG_ON(ret);
1552 if (found_key.offset == (u64)-1)
1553 break;
1554 dir_key.offset = found_key.offset + 1;
1555 }
1556 btrfs_release_path(root, path);
1557 if (range_end == (u64)-1)
1558 break;
1559 range_start = range_end + 1;
1560 }
1561
1562next_type:
1563 ret = 0;
1564 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1565 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1566 dir_key.type = BTRFS_DIR_INDEX_KEY;
1567 btrfs_release_path(root, path);
1568 goto again;
1569 }
1570out:
1571 btrfs_release_path(root, path);
1572 btrfs_free_path(log_path);
1573 iput(dir);
1574 return ret;
1575}
1576
1577/*
1578 * the process_func used to replay items from the log tree. This
1579 * gets called in two different stages. The first stage just looks
1580 * for inodes and makes sure they are all copied into the subvolume.
1581 *
1582 * The second stage copies all the other item types from the log into
1583 * the subvolume. The two stage approach is slower, but gets rid of
1584 * lots of complexity around inodes referencing other inodes that exist
1585 * only in the log (references come from either directory items or inode
1586 * back refs).
1587 */
1588static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1589 struct walk_control *wc, u64 gen)
1590{
1591 int nritems;
1592 struct btrfs_path *path;
1593 struct btrfs_root *root = wc->replay_dest;
1594 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001595 int level;
1596 int i;
1597 int ret;
1598
1599 btrfs_read_buffer(eb, gen);
1600
1601 level = btrfs_header_level(eb);
1602
1603 if (level != 0)
1604 return 0;
1605
1606 path = btrfs_alloc_path();
1607 BUG_ON(!path);
1608
1609 nritems = btrfs_header_nritems(eb);
1610 for (i = 0; i < nritems; i++) {
1611 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001612
1613 /* inode keys are done during the first stage */
1614 if (key.type == BTRFS_INODE_ITEM_KEY &&
1615 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001616 struct btrfs_inode_item *inode_item;
1617 u32 mode;
1618
1619 inode_item = btrfs_item_ptr(eb, i,
1620 struct btrfs_inode_item);
1621 mode = btrfs_inode_mode(eb, inode_item);
1622 if (S_ISDIR(mode)) {
1623 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001624 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001625 BUG_ON(ret);
1626 }
1627 ret = overwrite_item(wc->trans, root, path,
1628 eb, i, &key);
1629 BUG_ON(ret);
1630
Yan, Zhengc71bf092009-11-12 09:34:40 +00001631 /* for regular files, make sure corresponding
1632 * orhpan item exist. extents past the new EOF
1633 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001634 */
1635 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001636 ret = insert_orphan_item(wc->trans, root,
1637 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001638 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001639 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001640
Chris Masone02119d2008-09-05 16:13:11 -04001641 ret = link_to_fixup_dir(wc->trans, root,
1642 path, key.objectid);
1643 BUG_ON(ret);
1644 }
1645 if (wc->stage < LOG_WALK_REPLAY_ALL)
1646 continue;
1647
1648 /* these keys are simply copied */
1649 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1650 ret = overwrite_item(wc->trans, root, path,
1651 eb, i, &key);
1652 BUG_ON(ret);
1653 } else if (key.type == BTRFS_INODE_REF_KEY) {
1654 ret = add_inode_ref(wc->trans, root, log, path,
1655 eb, i, &key);
1656 BUG_ON(ret && ret != -ENOENT);
1657 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1658 ret = replay_one_extent(wc->trans, root, path,
1659 eb, i, &key);
1660 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001661 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1662 key.type == BTRFS_DIR_INDEX_KEY) {
1663 ret = replay_one_dir_item(wc->trans, root, path,
1664 eb, i, &key);
1665 BUG_ON(ret);
1666 }
1667 }
1668 btrfs_free_path(path);
1669 return 0;
1670}
1671
Chris Masond3977122009-01-05 21:25:51 -05001672static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001673 struct btrfs_root *root,
1674 struct btrfs_path *path, int *level,
1675 struct walk_control *wc)
1676{
1677 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001678 u64 bytenr;
1679 u64 ptr_gen;
1680 struct extent_buffer *next;
1681 struct extent_buffer *cur;
1682 struct extent_buffer *parent;
1683 u32 blocksize;
1684 int ret = 0;
1685
1686 WARN_ON(*level < 0);
1687 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1688
Chris Masond3977122009-01-05 21:25:51 -05001689 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001690 WARN_ON(*level < 0);
1691 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1692 cur = path->nodes[*level];
1693
1694 if (btrfs_header_level(cur) != *level)
1695 WARN_ON(1);
1696
1697 if (path->slots[*level] >=
1698 btrfs_header_nritems(cur))
1699 break;
1700
1701 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1702 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1703 blocksize = btrfs_level_size(root, *level - 1);
1704
1705 parent = path->nodes[*level];
1706 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001707
1708 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00001709 if (!next)
1710 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001711
Chris Masone02119d2008-09-05 16:13:11 -04001712 if (*level == 1) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001713 wc->process_func(root, next, wc, ptr_gen);
1714
Chris Masone02119d2008-09-05 16:13:11 -04001715 path->slots[*level]++;
1716 if (wc->free) {
1717 btrfs_read_buffer(next, ptr_gen);
1718
1719 btrfs_tree_lock(next);
1720 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001721 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001722 btrfs_wait_tree_block_writeback(next);
1723 btrfs_tree_unlock(next);
1724
Chris Masone02119d2008-09-05 16:13:11 -04001725 WARN_ON(root_owner !=
1726 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001727 ret = btrfs_free_reserved_extent(root,
1728 bytenr, blocksize);
Chris Masone02119d2008-09-05 16:13:11 -04001729 BUG_ON(ret);
1730 }
1731 free_extent_buffer(next);
1732 continue;
1733 }
1734 btrfs_read_buffer(next, ptr_gen);
1735
1736 WARN_ON(*level <= 0);
1737 if (path->nodes[*level-1])
1738 free_extent_buffer(path->nodes[*level-1]);
1739 path->nodes[*level-1] = next;
1740 *level = btrfs_header_level(next);
1741 path->slots[*level] = 0;
1742 cond_resched();
1743 }
1744 WARN_ON(*level < 0);
1745 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1746
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001747 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04001748
1749 cond_resched();
1750 return 0;
1751}
1752
Chris Masond3977122009-01-05 21:25:51 -05001753static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001754 struct btrfs_root *root,
1755 struct btrfs_path *path, int *level,
1756 struct walk_control *wc)
1757{
1758 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001759 int i;
1760 int slot;
1761 int ret;
1762
Chris Masond3977122009-01-05 21:25:51 -05001763 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04001764 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001765 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04001766 path->slots[i]++;
1767 *level = i;
1768 WARN_ON(*level == 0);
1769 return 0;
1770 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001771 struct extent_buffer *parent;
1772 if (path->nodes[*level] == root->node)
1773 parent = path->nodes[*level];
1774 else
1775 parent = path->nodes[*level + 1];
1776
1777 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001778 wc->process_func(root, path->nodes[*level], wc,
1779 btrfs_header_generation(path->nodes[*level]));
1780 if (wc->free) {
1781 struct extent_buffer *next;
1782
1783 next = path->nodes[*level];
1784
1785 btrfs_tree_lock(next);
1786 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001787 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001788 btrfs_wait_tree_block_writeback(next);
1789 btrfs_tree_unlock(next);
1790
Chris Masone02119d2008-09-05 16:13:11 -04001791 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001792 ret = btrfs_free_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04001793 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04001794 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04001795 BUG_ON(ret);
1796 }
1797 free_extent_buffer(path->nodes[*level]);
1798 path->nodes[*level] = NULL;
1799 *level = i + 1;
1800 }
1801 }
1802 return 1;
1803}
1804
1805/*
1806 * drop the reference count on the tree rooted at 'snap'. This traverses
1807 * the tree freeing any blocks that have a ref count of zero after being
1808 * decremented.
1809 */
1810static int walk_log_tree(struct btrfs_trans_handle *trans,
1811 struct btrfs_root *log, struct walk_control *wc)
1812{
1813 int ret = 0;
1814 int wret;
1815 int level;
1816 struct btrfs_path *path;
1817 int i;
1818 int orig_level;
1819
1820 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001821 if (!path)
1822 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001823
1824 level = btrfs_header_level(log->node);
1825 orig_level = level;
1826 path->nodes[level] = log->node;
1827 extent_buffer_get(log->node);
1828 path->slots[level] = 0;
1829
Chris Masond3977122009-01-05 21:25:51 -05001830 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001831 wret = walk_down_log_tree(trans, log, path, &level, wc);
1832 if (wret > 0)
1833 break;
1834 if (wret < 0)
1835 ret = wret;
1836
1837 wret = walk_up_log_tree(trans, log, path, &level, wc);
1838 if (wret > 0)
1839 break;
1840 if (wret < 0)
1841 ret = wret;
1842 }
1843
1844 /* was the root node processed? if not, catch it here */
1845 if (path->nodes[orig_level]) {
1846 wc->process_func(log, path->nodes[orig_level], wc,
1847 btrfs_header_generation(path->nodes[orig_level]));
1848 if (wc->free) {
1849 struct extent_buffer *next;
1850
1851 next = path->nodes[orig_level];
1852
1853 btrfs_tree_lock(next);
1854 clean_tree_block(trans, log, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001855 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001856 btrfs_wait_tree_block_writeback(next);
1857 btrfs_tree_unlock(next);
1858
Chris Masone02119d2008-09-05 16:13:11 -04001859 WARN_ON(log->root_key.objectid !=
1860 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001861 ret = btrfs_free_reserved_extent(log, next->start,
1862 next->len);
Chris Masone02119d2008-09-05 16:13:11 -04001863 BUG_ON(ret);
1864 }
1865 }
1866
1867 for (i = 0; i <= orig_level; i++) {
1868 if (path->nodes[i]) {
1869 free_extent_buffer(path->nodes[i]);
1870 path->nodes[i] = NULL;
1871 }
1872 }
1873 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001874 return ret;
1875}
1876
Yan Zheng7237f182009-01-21 12:54:03 -05001877/*
1878 * helper function to update the item for a given subvolumes log root
1879 * in the tree of log roots
1880 */
1881static int update_log_root(struct btrfs_trans_handle *trans,
1882 struct btrfs_root *log)
1883{
1884 int ret;
1885
1886 if (log->log_transid == 1) {
1887 /* insert root item on the first sync */
1888 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1889 &log->root_key, &log->root_item);
1890 } else {
1891 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1892 &log->root_key, &log->root_item);
1893 }
1894 return ret;
1895}
1896
Chris Mason12fcfd22009-03-24 10:24:20 -04001897static int wait_log_commit(struct btrfs_trans_handle *trans,
1898 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04001899{
1900 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05001901 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04001902
Yan Zheng7237f182009-01-21 12:54:03 -05001903 /*
1904 * we only allow two pending log transactions at a time,
1905 * so we know that if ours is more than 2 older than the
1906 * current transaction, we're done
1907 */
Chris Masone02119d2008-09-05 16:13:11 -04001908 do {
Yan Zheng7237f182009-01-21 12:54:03 -05001909 prepare_to_wait(&root->log_commit_wait[index],
1910 &wait, TASK_UNINTERRUPTIBLE);
1911 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001912
1913 if (root->fs_info->last_trans_log_full_commit !=
1914 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001915 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04001916 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04001917
Yan Zheng7237f182009-01-21 12:54:03 -05001918 finish_wait(&root->log_commit_wait[index], &wait);
1919 mutex_lock(&root->log_mutex);
1920 } while (root->log_transid < transid + 2 &&
1921 atomic_read(&root->log_commit[index]));
1922 return 0;
1923}
1924
Chris Mason12fcfd22009-03-24 10:24:20 -04001925static int wait_for_writer(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05001927{
1928 DEFINE_WAIT(wait);
1929 while (atomic_read(&root->log_writers)) {
1930 prepare_to_wait(&root->log_writer_wait,
1931 &wait, TASK_UNINTERRUPTIBLE);
1932 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001933 if (root->fs_info->last_trans_log_full_commit !=
1934 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05001935 schedule();
1936 mutex_lock(&root->log_mutex);
1937 finish_wait(&root->log_writer_wait, &wait);
1938 }
Chris Masone02119d2008-09-05 16:13:11 -04001939 return 0;
1940}
1941
1942/*
1943 * btrfs_sync_log does sends a given tree log down to the disk and
1944 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04001945 * you know that any inodes previously logged are safely on disk only
1946 * if it returns 0.
1947 *
1948 * Any other return value means you need to call btrfs_commit_transaction.
1949 * Some of the edge cases for fsyncing directories that have had unlinks
1950 * or renames done in the past mean that sometimes the only safe
1951 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
1952 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04001953 */
1954int btrfs_sync_log(struct btrfs_trans_handle *trans,
1955 struct btrfs_root *root)
1956{
Yan Zheng7237f182009-01-21 12:54:03 -05001957 int index1;
1958 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001959 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04001960 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04001961 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05001962 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001963 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001964
Yan Zheng7237f182009-01-21 12:54:03 -05001965 mutex_lock(&root->log_mutex);
1966 index1 = root->log_transid % 2;
1967 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001968 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05001969 mutex_unlock(&root->log_mutex);
1970 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04001971 }
Yan Zheng7237f182009-01-21 12:54:03 -05001972 atomic_set(&root->log_commit[index1], 1);
1973
1974 /* wait for previous tree log sync to complete */
1975 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04001976 wait_log_commit(trans, root, root->log_transid - 1);
Chris Masone02119d2008-09-05 16:13:11 -04001977
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001978 while (1) {
Yan Zheng7237f182009-01-21 12:54:03 -05001979 unsigned long batch = root->log_batch;
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001980 if (root->log_multiple_pids) {
1981 mutex_unlock(&root->log_mutex);
1982 schedule_timeout_uninterruptible(1);
1983 mutex_lock(&root->log_mutex);
1984 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001985 wait_for_writer(trans, root);
Yan Zheng7237f182009-01-21 12:54:03 -05001986 if (batch == root->log_batch)
Chris Masone02119d2008-09-05 16:13:11 -04001987 break;
1988 }
Chris Masond0c803c2008-09-11 16:17:57 -04001989
Chris Mason12fcfd22009-03-24 10:24:20 -04001990 /* bail out if we need to do a full commit */
1991 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
1992 ret = -EAGAIN;
1993 mutex_unlock(&root->log_mutex);
1994 goto out;
1995 }
1996
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001997 log_transid = root->log_transid;
1998 if (log_transid % 2 == 0)
1999 mark = EXTENT_DIRTY;
2000 else
2001 mark = EXTENT_NEW;
2002
Chris Mason690587d2009-10-13 13:29:19 -04002003 /* we start IO on all the marked extents here, but we don't actually
2004 * wait for them until later.
2005 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002006 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002007 BUG_ON(ret);
Yan Zheng7237f182009-01-21 12:54:03 -05002008
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002009 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002010
2011 root->log_batch = 0;
2012 root->log_transid++;
2013 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002014 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002015 smp_mb();
2016 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002017 * IO has been started, blocks of the log tree have WRITTEN flag set
2018 * in their headers. new modifications of the log will be written to
2019 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002020 */
2021 mutex_unlock(&root->log_mutex);
2022
2023 mutex_lock(&log_root_tree->log_mutex);
2024 log_root_tree->log_batch++;
2025 atomic_inc(&log_root_tree->log_writers);
2026 mutex_unlock(&log_root_tree->log_mutex);
2027
2028 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002029
2030 mutex_lock(&log_root_tree->log_mutex);
2031 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2032 smp_mb();
2033 if (waitqueue_active(&log_root_tree->log_writer_wait))
2034 wake_up(&log_root_tree->log_writer_wait);
2035 }
2036
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002037 if (ret) {
2038 BUG_ON(ret != -ENOSPC);
2039 root->fs_info->last_trans_log_full_commit = trans->transid;
2040 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2041 mutex_unlock(&log_root_tree->log_mutex);
2042 ret = -EAGAIN;
2043 goto out;
2044 }
2045
Yan Zheng7237f182009-01-21 12:54:03 -05002046 index2 = log_root_tree->log_transid % 2;
2047 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002048 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002049 wait_log_commit(trans, log_root_tree,
2050 log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002051 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002052 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002053 goto out;
2054 }
2055 atomic_set(&log_root_tree->log_commit[index2], 1);
2056
Chris Mason12fcfd22009-03-24 10:24:20 -04002057 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2058 wait_log_commit(trans, log_root_tree,
2059 log_root_tree->log_transid - 1);
2060 }
Yan Zheng7237f182009-01-21 12:54:03 -05002061
Chris Mason12fcfd22009-03-24 10:24:20 -04002062 wait_for_writer(trans, log_root_tree);
2063
2064 /*
2065 * now that we've moved on to the tree of log tree roots,
2066 * check the full commit flag again
2067 */
2068 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002069 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002070 mutex_unlock(&log_root_tree->log_mutex);
2071 ret = -EAGAIN;
2072 goto out_wake_log_root;
2073 }
Yan Zheng7237f182009-01-21 12:54:03 -05002074
2075 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002076 &log_root_tree->dirty_log_pages,
2077 EXTENT_DIRTY | EXTENT_NEW);
Chris Masone02119d2008-09-05 16:13:11 -04002078 BUG_ON(ret);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002079 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002080
2081 btrfs_set_super_log_root(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002082 log_root_tree->node->start);
Chris Masone02119d2008-09-05 16:13:11 -04002083 btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002084 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002085
Yan Zheng7237f182009-01-21 12:54:03 -05002086 log_root_tree->log_batch = 0;
2087 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002088 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002089
2090 mutex_unlock(&log_root_tree->log_mutex);
2091
2092 /*
2093 * nobody else is going to jump in and write the the ctree
2094 * super here because the log_commit atomic below is protecting
2095 * us. We must be called with a transaction handle pinning
2096 * the running transaction open, so a full commit can't hop
2097 * in and cause problems either.
2098 */
Chris Mason47226072009-10-13 12:55:09 -04002099 write_ctree_super(trans, root->fs_info->tree_root, 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002100 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002101
Chris Mason257c62e2009-10-13 13:21:08 -04002102 mutex_lock(&root->log_mutex);
2103 if (root->last_log_commit < log_transid)
2104 root->last_log_commit = log_transid;
2105 mutex_unlock(&root->log_mutex);
2106
Chris Mason12fcfd22009-03-24 10:24:20 -04002107out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002108 atomic_set(&log_root_tree->log_commit[index2], 0);
2109 smp_mb();
2110 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2111 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002112out:
Yan Zheng7237f182009-01-21 12:54:03 -05002113 atomic_set(&root->log_commit[index1], 0);
2114 smp_mb();
2115 if (waitqueue_active(&root->log_commit_wait[index1]))
2116 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002117 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002118}
2119
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002120static void free_log_tree(struct btrfs_trans_handle *trans,
2121 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002122{
2123 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002124 u64 start;
2125 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002126 struct walk_control wc = {
2127 .free = 1,
2128 .process_func = process_one_buffer
2129 };
2130
Chris Masone02119d2008-09-05 16:13:11 -04002131 ret = walk_log_tree(trans, log, &wc);
2132 BUG_ON(ret);
2133
Chris Masond3977122009-01-05 21:25:51 -05002134 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002135 ret = find_first_extent_bit(&log->dirty_log_pages,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002136 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04002137 if (ret)
2138 break;
2139
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002140 clear_extent_bits(&log->dirty_log_pages, start, end,
2141 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002142 }
2143
Yan Zheng7237f182009-01-21 12:54:03 -05002144 free_extent_buffer(log->node);
2145 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002146}
2147
2148/*
2149 * free all the extents used by the tree log. This should be called
2150 * at commit time of the full transaction
2151 */
2152int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2153{
2154 if (root->log_root) {
2155 free_log_tree(trans, root->log_root);
2156 root->log_root = NULL;
2157 }
2158 return 0;
2159}
2160
2161int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2162 struct btrfs_fs_info *fs_info)
2163{
2164 if (fs_info->log_root_tree) {
2165 free_log_tree(trans, fs_info->log_root_tree);
2166 fs_info->log_root_tree = NULL;
2167 }
Chris Masone02119d2008-09-05 16:13:11 -04002168 return 0;
2169}
2170
2171/*
Chris Masone02119d2008-09-05 16:13:11 -04002172 * If both a file and directory are logged, and unlinks or renames are
2173 * mixed in, we have a few interesting corners:
2174 *
2175 * create file X in dir Y
2176 * link file X to X.link in dir Y
2177 * fsync file X
2178 * unlink file X but leave X.link
2179 * fsync dir Y
2180 *
2181 * After a crash we would expect only X.link to exist. But file X
2182 * didn't get fsync'd again so the log has back refs for X and X.link.
2183 *
2184 * We solve this by removing directory entries and inode backrefs from the
2185 * log when a file that was logged in the current transaction is
2186 * unlinked. Any later fsync will include the updated log entries, and
2187 * we'll be able to reconstruct the proper directory items from backrefs.
2188 *
2189 * This optimizations allows us to avoid relogging the entire inode
2190 * or the entire directory.
2191 */
2192int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2193 struct btrfs_root *root,
2194 const char *name, int name_len,
2195 struct inode *dir, u64 index)
2196{
2197 struct btrfs_root *log;
2198 struct btrfs_dir_item *di;
2199 struct btrfs_path *path;
2200 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002201 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002202 int bytes_del = 0;
2203
Chris Mason3a5f1d42008-09-11 15:53:37 -04002204 if (BTRFS_I(dir)->logged_trans < trans->transid)
2205 return 0;
2206
Chris Masone02119d2008-09-05 16:13:11 -04002207 ret = join_running_log_trans(root);
2208 if (ret)
2209 return 0;
2210
2211 mutex_lock(&BTRFS_I(dir)->log_mutex);
2212
2213 log = root->log_root;
2214 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002215 if (!path) {
2216 err = -ENOMEM;
2217 goto out_unlock;
2218 }
liubo2a29edc2011-01-26 06:22:08 +00002219
Chris Masone02119d2008-09-05 16:13:11 -04002220 di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
2221 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002222 if (IS_ERR(di)) {
2223 err = PTR_ERR(di);
2224 goto fail;
2225 }
2226 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002227 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2228 bytes_del += name_len;
2229 BUG_ON(ret);
2230 }
2231 btrfs_release_path(log, path);
2232 di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
2233 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002234 if (IS_ERR(di)) {
2235 err = PTR_ERR(di);
2236 goto fail;
2237 }
2238 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002239 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2240 bytes_del += name_len;
2241 BUG_ON(ret);
2242 }
2243
2244 /* update the directory size in the log to reflect the names
2245 * we have removed
2246 */
2247 if (bytes_del) {
2248 struct btrfs_key key;
2249
2250 key.objectid = dir->i_ino;
2251 key.offset = 0;
2252 key.type = BTRFS_INODE_ITEM_KEY;
2253 btrfs_release_path(log, path);
2254
2255 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002256 if (ret < 0) {
2257 err = ret;
2258 goto fail;
2259 }
Chris Masone02119d2008-09-05 16:13:11 -04002260 if (ret == 0) {
2261 struct btrfs_inode_item *item;
2262 u64 i_size;
2263
2264 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2265 struct btrfs_inode_item);
2266 i_size = btrfs_inode_size(path->nodes[0], item);
2267 if (i_size > bytes_del)
2268 i_size -= bytes_del;
2269 else
2270 i_size = 0;
2271 btrfs_set_inode_size(path->nodes[0], item, i_size);
2272 btrfs_mark_buffer_dirty(path->nodes[0]);
2273 } else
2274 ret = 0;
2275 btrfs_release_path(log, path);
2276 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002277fail:
Chris Masone02119d2008-09-05 16:13:11 -04002278 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002279out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002280 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002281 if (ret == -ENOSPC) {
2282 root->fs_info->last_trans_log_full_commit = trans->transid;
2283 ret = 0;
2284 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002285 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002286
Andi Kleen411fc6b2010-10-29 15:14:31 -04002287 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002288}
2289
2290/* see comments for btrfs_del_dir_entries_in_log */
2291int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2292 struct btrfs_root *root,
2293 const char *name, int name_len,
2294 struct inode *inode, u64 dirid)
2295{
2296 struct btrfs_root *log;
2297 u64 index;
2298 int ret;
2299
Chris Mason3a5f1d42008-09-11 15:53:37 -04002300 if (BTRFS_I(inode)->logged_trans < trans->transid)
2301 return 0;
2302
Chris Masone02119d2008-09-05 16:13:11 -04002303 ret = join_running_log_trans(root);
2304 if (ret)
2305 return 0;
2306 log = root->log_root;
2307 mutex_lock(&BTRFS_I(inode)->log_mutex);
2308
2309 ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
2310 dirid, &index);
2311 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002312 if (ret == -ENOSPC) {
2313 root->fs_info->last_trans_log_full_commit = trans->transid;
2314 ret = 0;
2315 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002316 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002317
Chris Masone02119d2008-09-05 16:13:11 -04002318 return ret;
2319}
2320
2321/*
2322 * creates a range item in the log for 'dirid'. first_offset and
2323 * last_offset tell us which parts of the key space the log should
2324 * be considered authoritative for.
2325 */
2326static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2327 struct btrfs_root *log,
2328 struct btrfs_path *path,
2329 int key_type, u64 dirid,
2330 u64 first_offset, u64 last_offset)
2331{
2332 int ret;
2333 struct btrfs_key key;
2334 struct btrfs_dir_log_item *item;
2335
2336 key.objectid = dirid;
2337 key.offset = first_offset;
2338 if (key_type == BTRFS_DIR_ITEM_KEY)
2339 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2340 else
2341 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2342 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002343 if (ret)
2344 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002345
2346 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2347 struct btrfs_dir_log_item);
2348 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2349 btrfs_mark_buffer_dirty(path->nodes[0]);
2350 btrfs_release_path(log, path);
2351 return 0;
2352}
2353
2354/*
2355 * log all the items included in the current transaction for a given
2356 * directory. This also creates the range items in the log tree required
2357 * to replay anything deleted before the fsync
2358 */
2359static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2360 struct btrfs_root *root, struct inode *inode,
2361 struct btrfs_path *path,
2362 struct btrfs_path *dst_path, int key_type,
2363 u64 min_offset, u64 *last_offset_ret)
2364{
2365 struct btrfs_key min_key;
2366 struct btrfs_key max_key;
2367 struct btrfs_root *log = root->log_root;
2368 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002369 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002370 int ret;
2371 int i;
2372 int nritems;
2373 u64 first_offset = min_offset;
2374 u64 last_offset = (u64)-1;
2375
2376 log = root->log_root;
2377 max_key.objectid = inode->i_ino;
2378 max_key.offset = (u64)-1;
2379 max_key.type = key_type;
2380
2381 min_key.objectid = inode->i_ino;
2382 min_key.type = key_type;
2383 min_key.offset = min_offset;
2384
2385 path->keep_locks = 1;
2386
2387 ret = btrfs_search_forward(root, &min_key, &max_key,
2388 path, 0, trans->transid);
2389
2390 /*
2391 * we didn't find anything from this transaction, see if there
2392 * is anything at all
2393 */
2394 if (ret != 0 || min_key.objectid != inode->i_ino ||
2395 min_key.type != key_type) {
2396 min_key.objectid = inode->i_ino;
2397 min_key.type = key_type;
2398 min_key.offset = (u64)-1;
2399 btrfs_release_path(root, path);
2400 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2401 if (ret < 0) {
2402 btrfs_release_path(root, path);
2403 return ret;
2404 }
2405 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2406
2407 /* if ret == 0 there are items for this type,
2408 * create a range to tell us the last key of this type.
2409 * otherwise, there are no items in this directory after
2410 * *min_offset, and we create a range to indicate that.
2411 */
2412 if (ret == 0) {
2413 struct btrfs_key tmp;
2414 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2415 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002416 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002417 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002418 }
2419 goto done;
2420 }
2421
2422 /* go backward to find any previous key */
2423 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2424 if (ret == 0) {
2425 struct btrfs_key tmp;
2426 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2427 if (key_type == tmp.type) {
2428 first_offset = tmp.offset;
2429 ret = overwrite_item(trans, log, dst_path,
2430 path->nodes[0], path->slots[0],
2431 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002432 if (ret) {
2433 err = ret;
2434 goto done;
2435 }
Chris Masone02119d2008-09-05 16:13:11 -04002436 }
2437 }
2438 btrfs_release_path(root, path);
2439
2440 /* find the first key from this transaction again */
2441 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2442 if (ret != 0) {
2443 WARN_ON(1);
2444 goto done;
2445 }
2446
2447 /*
2448 * we have a block from this transaction, log every item in it
2449 * from our directory
2450 */
Chris Masond3977122009-01-05 21:25:51 -05002451 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002452 struct btrfs_key tmp;
2453 src = path->nodes[0];
2454 nritems = btrfs_header_nritems(src);
2455 for (i = path->slots[0]; i < nritems; i++) {
2456 btrfs_item_key_to_cpu(src, &min_key, i);
2457
2458 if (min_key.objectid != inode->i_ino ||
2459 min_key.type != key_type)
2460 goto done;
2461 ret = overwrite_item(trans, log, dst_path, src, i,
2462 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002463 if (ret) {
2464 err = ret;
2465 goto done;
2466 }
Chris Masone02119d2008-09-05 16:13:11 -04002467 }
2468 path->slots[0] = nritems;
2469
2470 /*
2471 * look ahead to the next item and see if it is also
2472 * from this directory and from this transaction
2473 */
2474 ret = btrfs_next_leaf(root, path);
2475 if (ret == 1) {
2476 last_offset = (u64)-1;
2477 goto done;
2478 }
2479 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2480 if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
2481 last_offset = (u64)-1;
2482 goto done;
2483 }
2484 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2485 ret = overwrite_item(trans, log, dst_path,
2486 path->nodes[0], path->slots[0],
2487 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002488 if (ret)
2489 err = ret;
2490 else
2491 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002492 goto done;
2493 }
2494 }
2495done:
Chris Masone02119d2008-09-05 16:13:11 -04002496 btrfs_release_path(root, path);
2497 btrfs_release_path(log, dst_path);
2498
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002499 if (err == 0) {
2500 *last_offset_ret = last_offset;
2501 /*
2502 * insert the log range keys to indicate where the log
2503 * is valid
2504 */
2505 ret = insert_dir_log_key(trans, log, path, key_type,
2506 inode->i_ino, first_offset,
2507 last_offset);
2508 if (ret)
2509 err = ret;
2510 }
2511 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002512}
2513
2514/*
2515 * logging directories is very similar to logging inodes, We find all the items
2516 * from the current transaction and write them to the log.
2517 *
2518 * The recovery code scans the directory in the subvolume, and if it finds a
2519 * key in the range logged that is not present in the log tree, then it means
2520 * that dir entry was unlinked during the transaction.
2521 *
2522 * In order for that scan to work, we must include one key smaller than
2523 * the smallest logged by this transaction and one key larger than the largest
2524 * key logged by this transaction.
2525 */
2526static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2527 struct btrfs_root *root, struct inode *inode,
2528 struct btrfs_path *path,
2529 struct btrfs_path *dst_path)
2530{
2531 u64 min_key;
2532 u64 max_key;
2533 int ret;
2534 int key_type = BTRFS_DIR_ITEM_KEY;
2535
2536again:
2537 min_key = 0;
2538 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002539 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002540 ret = log_dir_items(trans, root, inode, path,
2541 dst_path, key_type, min_key,
2542 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002543 if (ret)
2544 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002545 if (max_key == (u64)-1)
2546 break;
2547 min_key = max_key + 1;
2548 }
2549
2550 if (key_type == BTRFS_DIR_ITEM_KEY) {
2551 key_type = BTRFS_DIR_INDEX_KEY;
2552 goto again;
2553 }
2554 return 0;
2555}
2556
2557/*
2558 * a helper function to drop items from the log before we relog an
2559 * inode. max_key_type indicates the highest item type to remove.
2560 * This cannot be run for file data extents because it does not
2561 * free the extents they point to.
2562 */
2563static int drop_objectid_items(struct btrfs_trans_handle *trans,
2564 struct btrfs_root *log,
2565 struct btrfs_path *path,
2566 u64 objectid, int max_key_type)
2567{
2568 int ret;
2569 struct btrfs_key key;
2570 struct btrfs_key found_key;
2571
2572 key.objectid = objectid;
2573 key.type = max_key_type;
2574 key.offset = (u64)-1;
2575
Chris Masond3977122009-01-05 21:25:51 -05002576 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002577 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002578 BUG_ON(ret == 0);
2579 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002580 break;
2581
2582 if (path->slots[0] == 0)
2583 break;
2584
2585 path->slots[0]--;
2586 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2587 path->slots[0]);
2588
2589 if (found_key.objectid != objectid)
2590 break;
2591
2592 ret = btrfs_del_item(trans, log, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002593 if (ret)
2594 break;
Chris Masone02119d2008-09-05 16:13:11 -04002595 btrfs_release_path(log, path);
2596 }
2597 btrfs_release_path(log, path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002598 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002599}
2600
Chris Mason31ff1cd2008-09-11 16:17:57 -04002601static noinline int copy_items(struct btrfs_trans_handle *trans,
2602 struct btrfs_root *log,
2603 struct btrfs_path *dst_path,
2604 struct extent_buffer *src,
2605 int start_slot, int nr, int inode_only)
2606{
2607 unsigned long src_offset;
2608 unsigned long dst_offset;
2609 struct btrfs_file_extent_item *extent;
2610 struct btrfs_inode_item *inode_item;
2611 int ret;
2612 struct btrfs_key *ins_keys;
2613 u32 *ins_sizes;
2614 char *ins_data;
2615 int i;
Chris Masond20f7042008-12-08 16:58:54 -05002616 struct list_head ordered_sums;
2617
2618 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002619
2620 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2621 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00002622 if (!ins_data)
2623 return -ENOMEM;
2624
Chris Mason31ff1cd2008-09-11 16:17:57 -04002625 ins_sizes = (u32 *)ins_data;
2626 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2627
2628 for (i = 0; i < nr; i++) {
2629 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2630 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2631 }
2632 ret = btrfs_insert_empty_items(trans, log, dst_path,
2633 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002634 if (ret) {
2635 kfree(ins_data);
2636 return ret;
2637 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002638
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002639 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002640 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2641 dst_path->slots[0]);
2642
2643 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2644
2645 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2646 src_offset, ins_sizes[i]);
2647
2648 if (inode_only == LOG_INODE_EXISTS &&
2649 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2650 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2651 dst_path->slots[0],
2652 struct btrfs_inode_item);
2653 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2654
2655 /* set the generation to zero so the recover code
2656 * can tell the difference between an logging
2657 * just to say 'this inode exists' and a logging
2658 * to say 'update this inode with these values'
2659 */
2660 btrfs_set_inode_generation(dst_path->nodes[0],
2661 inode_item, 0);
2662 }
2663 /* take a reference on file data extents so that truncates
2664 * or deletes of this inode don't have to relog the inode
2665 * again
2666 */
2667 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2668 int found_type;
2669 extent = btrfs_item_ptr(src, start_slot + i,
2670 struct btrfs_file_extent_item);
2671
2672 found_type = btrfs_file_extent_type(src, extent);
Yan Zhengd899e052008-10-30 14:25:28 -04002673 if (found_type == BTRFS_FILE_EXTENT_REG ||
2674 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002675 u64 ds, dl, cs, cl;
2676 ds = btrfs_file_extent_disk_bytenr(src,
2677 extent);
2678 /* ds == 0 is a hole */
2679 if (ds == 0)
2680 continue;
2681
2682 dl = btrfs_file_extent_disk_num_bytes(src,
2683 extent);
2684 cs = btrfs_file_extent_offset(src, extent);
2685 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07002686 extent);
Chris Mason580afd72008-12-08 19:15:39 -05002687 if (btrfs_file_extent_compression(src,
2688 extent)) {
2689 cs = 0;
2690 cl = dl;
2691 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002692
2693 ret = btrfs_lookup_csums_range(
2694 log->fs_info->csum_root,
2695 ds + cs, ds + cs + cl - 1,
2696 &ordered_sums);
2697 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002698 }
2699 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002700 }
2701
2702 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2703 btrfs_release_path(log, dst_path);
2704 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05002705
2706 /*
2707 * we have to do this after the loop above to avoid changing the
2708 * log tree while trying to change the log tree.
2709 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002710 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002711 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05002712 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2713 struct btrfs_ordered_sum,
2714 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002715 if (!ret)
2716 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05002717 list_del(&sums->list);
2718 kfree(sums);
2719 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002720 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002721}
2722
Chris Masone02119d2008-09-05 16:13:11 -04002723/* log a single inode in the tree log.
2724 * At least one parent directory for this inode must exist in the tree
2725 * or be logged already.
2726 *
2727 * Any items from this inode changed by the current transaction are copied
2728 * to the log tree. An extra reference is taken on any extents in this
2729 * file, allowing us to avoid a whole pile of corner cases around logging
2730 * blocks that have been removed from the tree.
2731 *
2732 * See LOG_INODE_ALL and related defines for a description of what inode_only
2733 * does.
2734 *
2735 * This handles both files and directories.
2736 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002737static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002738 struct btrfs_root *root, struct inode *inode,
2739 int inode_only)
2740{
2741 struct btrfs_path *path;
2742 struct btrfs_path *dst_path;
2743 struct btrfs_key min_key;
2744 struct btrfs_key max_key;
2745 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002746 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002747 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002748 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002749 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002750 int ins_start_slot = 0;
2751 int ins_nr;
Chris Masone02119d2008-09-05 16:13:11 -04002752
2753 log = root->log_root;
2754
2755 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002756 if (!path)
2757 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002758 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002759 if (!dst_path) {
2760 btrfs_free_path(path);
2761 return -ENOMEM;
2762 }
Chris Masone02119d2008-09-05 16:13:11 -04002763
2764 min_key.objectid = inode->i_ino;
2765 min_key.type = BTRFS_INODE_ITEM_KEY;
2766 min_key.offset = 0;
2767
2768 max_key.objectid = inode->i_ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04002769
2770 /* today the code can only do partial logging of directories */
2771 if (!S_ISDIR(inode->i_mode))
2772 inode_only = LOG_INODE_ALL;
2773
Chris Masone02119d2008-09-05 16:13:11 -04002774 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2775 max_key.type = BTRFS_XATTR_ITEM_KEY;
2776 else
2777 max_key.type = (u8)-1;
2778 max_key.offset = (u64)-1;
2779
Chris Masone02119d2008-09-05 16:13:11 -04002780 mutex_lock(&BTRFS_I(inode)->log_mutex);
2781
2782 /*
2783 * a brute force approach to making sure we get the most uptodate
2784 * copies of everything.
2785 */
2786 if (S_ISDIR(inode->i_mode)) {
2787 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2788
2789 if (inode_only == LOG_INODE_EXISTS)
2790 max_key_type = BTRFS_XATTR_ITEM_KEY;
2791 ret = drop_objectid_items(trans, log, path,
2792 inode->i_ino, max_key_type);
2793 } else {
2794 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2795 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002796 if (ret) {
2797 err = ret;
2798 goto out_unlock;
2799 }
Chris Masone02119d2008-09-05 16:13:11 -04002800 path->keep_locks = 1;
2801
Chris Masond3977122009-01-05 21:25:51 -05002802 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002803 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002804 ret = btrfs_search_forward(root, &min_key, &max_key,
2805 path, 0, trans->transid);
2806 if (ret != 0)
2807 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002808again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04002809 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Chris Masone02119d2008-09-05 16:13:11 -04002810 if (min_key.objectid != inode->i_ino)
2811 break;
2812 if (min_key.type > max_key.type)
2813 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002814
Chris Masone02119d2008-09-05 16:13:11 -04002815 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04002816 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2817 ins_nr++;
2818 goto next_slot;
2819 } else if (!ins_nr) {
2820 ins_start_slot = path->slots[0];
2821 ins_nr = 1;
2822 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002823 }
2824
Chris Mason31ff1cd2008-09-11 16:17:57 -04002825 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2826 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002827 if (ret) {
2828 err = ret;
2829 goto out_unlock;
2830 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002831 ins_nr = 1;
2832 ins_start_slot = path->slots[0];
2833next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04002834
Chris Mason3a5f1d42008-09-11 15:53:37 -04002835 nritems = btrfs_header_nritems(path->nodes[0]);
2836 path->slots[0]++;
2837 if (path->slots[0] < nritems) {
2838 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2839 path->slots[0]);
2840 goto again;
2841 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002842 if (ins_nr) {
2843 ret = copy_items(trans, log, dst_path, src,
2844 ins_start_slot,
2845 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002846 if (ret) {
2847 err = ret;
2848 goto out_unlock;
2849 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002850 ins_nr = 0;
2851 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002852 btrfs_release_path(root, path);
2853
Chris Masone02119d2008-09-05 16:13:11 -04002854 if (min_key.offset < (u64)-1)
2855 min_key.offset++;
2856 else if (min_key.type < (u8)-1)
2857 min_key.type++;
2858 else if (min_key.objectid < (u64)-1)
2859 min_key.objectid++;
2860 else
2861 break;
2862 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002863 if (ins_nr) {
2864 ret = copy_items(trans, log, dst_path, src,
2865 ins_start_slot,
2866 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002867 if (ret) {
2868 err = ret;
2869 goto out_unlock;
2870 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002871 ins_nr = 0;
2872 }
2873 WARN_ON(ins_nr);
Chris Mason9623f9a2008-09-11 17:42:42 -04002874 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04002875 btrfs_release_path(root, path);
2876 btrfs_release_path(log, dst_path);
2877 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002878 if (ret) {
2879 err = ret;
2880 goto out_unlock;
2881 }
Chris Masone02119d2008-09-05 16:13:11 -04002882 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002883 BTRFS_I(inode)->logged_trans = trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002884out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002885 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2886
2887 btrfs_free_path(path);
2888 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002889 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002890}
2891
Chris Mason12fcfd22009-03-24 10:24:20 -04002892/*
2893 * follow the dentry parent pointers up the chain and see if any
2894 * of the directories in it require a full commit before they can
2895 * be logged. Returns zero if nothing special needs to be done or 1 if
2896 * a full commit is required.
2897 */
2898static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
2899 struct inode *inode,
2900 struct dentry *parent,
2901 struct super_block *sb,
2902 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04002903{
Chris Mason12fcfd22009-03-24 10:24:20 -04002904 int ret = 0;
2905 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00002906 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002907
Chris Masonaf4176b2009-03-24 10:24:31 -04002908 /*
2909 * for regular files, if its inode is already on disk, we don't
2910 * have to worry about the parents at all. This is because
2911 * we can use the last_unlink_trans field to record renames
2912 * and other fun in this file.
2913 */
2914 if (S_ISREG(inode->i_mode) &&
2915 BTRFS_I(inode)->generation <= last_committed &&
2916 BTRFS_I(inode)->last_unlink_trans <= last_committed)
2917 goto out;
2918
Chris Mason12fcfd22009-03-24 10:24:20 -04002919 if (!S_ISDIR(inode->i_mode)) {
2920 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2921 goto out;
2922 inode = parent->d_inode;
2923 }
2924
2925 while (1) {
2926 BTRFS_I(inode)->logged_trans = trans->transid;
2927 smp_mb();
2928
2929 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
2930 root = BTRFS_I(inode)->root;
2931
2932 /*
2933 * make sure any commits to the log are forced
2934 * to be full commits
2935 */
2936 root->fs_info->last_trans_log_full_commit =
2937 trans->transid;
2938 ret = 1;
2939 break;
2940 }
2941
2942 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2943 break;
2944
Yan, Zheng76dda932009-09-21 16:00:26 -04002945 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04002946 break;
2947
Josef Bacik6a912212010-11-20 09:48:00 +00002948 parent = dget_parent(parent);
2949 dput(old_parent);
2950 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04002951 inode = parent->d_inode;
2952
2953 }
Josef Bacik6a912212010-11-20 09:48:00 +00002954 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04002955out:
Chris Masone02119d2008-09-05 16:13:11 -04002956 return ret;
2957}
2958
Chris Mason257c62e2009-10-13 13:21:08 -04002959static int inode_in_log(struct btrfs_trans_handle *trans,
2960 struct inode *inode)
2961{
2962 struct btrfs_root *root = BTRFS_I(inode)->root;
2963 int ret = 0;
2964
2965 mutex_lock(&root->log_mutex);
2966 if (BTRFS_I(inode)->logged_trans == trans->transid &&
2967 BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
2968 ret = 1;
2969 mutex_unlock(&root->log_mutex);
2970 return ret;
2971}
2972
2973
Chris Masone02119d2008-09-05 16:13:11 -04002974/*
2975 * helper function around btrfs_log_inode to make sure newly created
2976 * parent directories also end up in the log. A minimal inode and backref
2977 * only logging is done of any parent directories that are older than
2978 * the last committed transaction
2979 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002980int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
2981 struct btrfs_root *root, struct inode *inode,
2982 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04002983{
Chris Mason12fcfd22009-03-24 10:24:20 -04002984 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04002985 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00002986 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002987 int ret = 0;
2988 u64 last_committed = root->fs_info->last_trans_committed;
2989
2990 sb = inode->i_sb;
2991
Sage Weil3a5e1402009-04-02 16:49:40 -04002992 if (btrfs_test_opt(root, NOTREELOG)) {
2993 ret = 1;
2994 goto end_no_trans;
2995 }
2996
Chris Mason12fcfd22009-03-24 10:24:20 -04002997 if (root->fs_info->last_trans_log_full_commit >
2998 root->fs_info->last_trans_committed) {
2999 ret = 1;
3000 goto end_no_trans;
3001 }
3002
Yan, Zheng76dda932009-09-21 16:00:26 -04003003 if (root != BTRFS_I(inode)->root ||
3004 btrfs_root_refs(&root->root_item) == 0) {
3005 ret = 1;
3006 goto end_no_trans;
3007 }
3008
Chris Mason12fcfd22009-03-24 10:24:20 -04003009 ret = check_parent_dirs_for_sync(trans, inode, parent,
3010 sb, last_committed);
3011 if (ret)
3012 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003013
Chris Mason257c62e2009-10-13 13:21:08 -04003014 if (inode_in_log(trans, inode)) {
3015 ret = BTRFS_NO_LOG_SYNC;
3016 goto end_no_trans;
3017 }
3018
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003019 ret = start_log_trans(trans, root);
3020 if (ret)
3021 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003022
3023 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003024 if (ret)
3025 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003026
Chris Masonaf4176b2009-03-24 10:24:31 -04003027 /*
3028 * for regular files, if its inode is already on disk, we don't
3029 * have to worry about the parents at all. This is because
3030 * we can use the last_unlink_trans field to record renames
3031 * and other fun in this file.
3032 */
3033 if (S_ISREG(inode->i_mode) &&
3034 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003035 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3036 ret = 0;
3037 goto end_trans;
3038 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003039
3040 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003041 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003042 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003043 break;
3044
Chris Mason12fcfd22009-03-24 10:24:20 -04003045 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003046 if (root != BTRFS_I(inode)->root)
3047 break;
3048
Chris Mason12fcfd22009-03-24 10:24:20 -04003049 if (BTRFS_I(inode)->generation >
3050 root->fs_info->last_trans_committed) {
3051 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003052 if (ret)
3053 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003054 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003055 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003056 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003057
Josef Bacik6a912212010-11-20 09:48:00 +00003058 parent = dget_parent(parent);
3059 dput(old_parent);
3060 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003061 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003062 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003063end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003064 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003065 if (ret < 0) {
3066 BUG_ON(ret != -ENOSPC);
3067 root->fs_info->last_trans_log_full_commit = trans->transid;
3068 ret = 1;
3069 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003070 btrfs_end_log_trans(root);
3071end_no_trans:
3072 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003073}
3074
3075/*
3076 * it is not safe to log dentry if the chunk root has added new
3077 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3078 * If this returns 1, you must commit the transaction to safely get your
3079 * data on disk.
3080 */
3081int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3082 struct btrfs_root *root, struct dentry *dentry)
3083{
Josef Bacik6a912212010-11-20 09:48:00 +00003084 struct dentry *parent = dget_parent(dentry);
3085 int ret;
3086
3087 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3088 dput(parent);
3089
3090 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003091}
3092
3093/*
3094 * should be called during mount to recover any replay any log trees
3095 * from the FS
3096 */
3097int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3098{
3099 int ret;
3100 struct btrfs_path *path;
3101 struct btrfs_trans_handle *trans;
3102 struct btrfs_key key;
3103 struct btrfs_key found_key;
3104 struct btrfs_key tmp_key;
3105 struct btrfs_root *log;
3106 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3107 struct walk_control wc = {
3108 .process_func = process_one_buffer,
3109 .stage = 0,
3110 };
3111
Chris Masone02119d2008-09-05 16:13:11 -04003112 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003113 if (!path)
3114 return -ENOMEM;
3115
3116 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003117
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003118 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003119 BUG_ON(IS_ERR(trans));
Chris Masone02119d2008-09-05 16:13:11 -04003120
3121 wc.trans = trans;
3122 wc.pin = 1;
3123
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003124 ret = walk_log_tree(trans, log_root_tree, &wc);
3125 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003126
3127again:
3128 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3129 key.offset = (u64)-1;
3130 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3131
Chris Masond3977122009-01-05 21:25:51 -05003132 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003133 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3134 if (ret < 0)
3135 break;
3136 if (ret > 0) {
3137 if (path->slots[0] == 0)
3138 break;
3139 path->slots[0]--;
3140 }
3141 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3142 path->slots[0]);
3143 btrfs_release_path(log_root_tree, path);
3144 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3145 break;
3146
3147 log = btrfs_read_fs_root_no_radix(log_root_tree,
3148 &found_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003149 BUG_ON(IS_ERR(log));
Chris Masone02119d2008-09-05 16:13:11 -04003150
3151 tmp_key.objectid = found_key.offset;
3152 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3153 tmp_key.offset = (u64)-1;
3154
3155 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Chris Masone02119d2008-09-05 16:13:11 -04003156 BUG_ON(!wc.replay_dest);
3157
Yan Zheng07d400a2009-01-06 11:42:00 -05003158 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003159 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003160 ret = walk_log_tree(trans, log, &wc);
3161 BUG_ON(ret);
3162
3163 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3164 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3165 path);
3166 BUG_ON(ret);
3167 }
Chris Masone02119d2008-09-05 16:13:11 -04003168
3169 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003170 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003171 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003172 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003173 kfree(log);
3174
3175 if (found_key.offset == 0)
3176 break;
3177 }
3178 btrfs_release_path(log_root_tree, path);
3179
3180 /* step one is to pin it all, step two is to replay just inodes */
3181 if (wc.pin) {
3182 wc.pin = 0;
3183 wc.process_func = replay_one_buffer;
3184 wc.stage = LOG_WALK_REPLAY_INODES;
3185 goto again;
3186 }
3187 /* step three is to replay everything */
3188 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3189 wc.stage++;
3190 goto again;
3191 }
3192
3193 btrfs_free_path(path);
3194
3195 free_extent_buffer(log_root_tree->node);
3196 log_root_tree->log_root = NULL;
3197 fs_info->log_root_recovering = 0;
3198
3199 /* step 4: commit the transaction, which also unpins the blocks */
3200 btrfs_commit_transaction(trans, fs_info->tree_root);
3201
3202 kfree(log_root_tree);
3203 return 0;
3204}
Chris Mason12fcfd22009-03-24 10:24:20 -04003205
3206/*
3207 * there are some corner cases where we want to force a full
3208 * commit instead of allowing a directory to be logged.
3209 *
3210 * They revolve around files there were unlinked from the directory, and
3211 * this function updates the parent directory so that a full commit is
3212 * properly done if it is fsync'd later after the unlinks are done.
3213 */
3214void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3215 struct inode *dir, struct inode *inode,
3216 int for_rename)
3217{
3218 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003219 * when we're logging a file, if it hasn't been renamed
3220 * or unlinked, and its inode is fully committed on disk,
3221 * we don't have to worry about walking up the directory chain
3222 * to log its parents.
3223 *
3224 * So, we use the last_unlink_trans field to put this transid
3225 * into the file. When the file is logged we check it and
3226 * don't log the parents if the file is fully on disk.
3227 */
3228 if (S_ISREG(inode->i_mode))
3229 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3230
3231 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003232 * if this directory was already logged any new
3233 * names for this file/dir will get recorded
3234 */
3235 smp_mb();
3236 if (BTRFS_I(dir)->logged_trans == trans->transid)
3237 return;
3238
3239 /*
3240 * if the inode we're about to unlink was logged,
3241 * the log will be properly updated for any new names
3242 */
3243 if (BTRFS_I(inode)->logged_trans == trans->transid)
3244 return;
3245
3246 /*
3247 * when renaming files across directories, if the directory
3248 * there we're unlinking from gets fsync'd later on, there's
3249 * no way to find the destination directory later and fsync it
3250 * properly. So, we have to be conservative and force commits
3251 * so the new name gets discovered.
3252 */
3253 if (for_rename)
3254 goto record;
3255
3256 /* we can safely do the unlink without any special recording */
3257 return;
3258
3259record:
3260 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3261}
3262
3263/*
3264 * Call this after adding a new name for a file and it will properly
3265 * update the log to reflect the new name.
3266 *
3267 * It will return zero if all goes well, and it will return 1 if a
3268 * full transaction commit is required.
3269 */
3270int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3271 struct inode *inode, struct inode *old_dir,
3272 struct dentry *parent)
3273{
3274 struct btrfs_root * root = BTRFS_I(inode)->root;
3275
3276 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003277 * this will force the logging code to walk the dentry chain
3278 * up for the file
3279 */
3280 if (S_ISREG(inode->i_mode))
3281 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3282
3283 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003284 * if this inode hasn't been logged and directory we're renaming it
3285 * from hasn't been logged, we don't need to log it
3286 */
3287 if (BTRFS_I(inode)->logged_trans <=
3288 root->fs_info->last_trans_committed &&
3289 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3290 root->fs_info->last_trans_committed))
3291 return 0;
3292
3293 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3294}
3295