blob: c2d88756640018858964674b71b3c155977cc2a2 [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masone02119d2008-09-05 16:13:11 -040021#include "ctree.h"
22#include "transaction.h"
23#include "disk-io.h"
24#include "locking.h"
25#include "print-tree.h"
26#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050027#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040028
29/* magic values for the inode_only field in btrfs_log_inode:
30 *
31 * LOG_INODE_ALL means to log everything
32 * LOG_INODE_EXISTS means to log just enough to recreate the inode
33 * during log replay
34 */
35#define LOG_INODE_ALL 0
36#define LOG_INODE_EXISTS 1
37
38/*
Chris Mason12fcfd22009-03-24 10:24:20 -040039 * directory trouble cases
40 *
41 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
42 * log, we must force a full commit before doing an fsync of the directory
43 * where the unlink was done.
44 * ---> record transid of last unlink/rename per directory
45 *
46 * mkdir foo/some_dir
47 * normal commit
48 * rename foo/some_dir foo2/some_dir
49 * mkdir foo/some_dir
50 * fsync foo/some_dir/some_file
51 *
52 * The fsync above will unlink the original some_dir without recording
53 * it in its new location (foo2). After a crash, some_dir will be gone
54 * unless the fsync of some_file forces a full commit
55 *
56 * 2) we must log any new names for any file or dir that is in the fsync
57 * log. ---> check inode while renaming/linking.
58 *
59 * 2a) we must log any new names for any file or dir during rename
60 * when the directory they are being removed from was logged.
61 * ---> check inode and old parent dir during rename
62 *
63 * 2a is actually the more important variant. With the extra logging
64 * a crash might unlink the old name without recreating the new one
65 *
66 * 3) after a crash, we must go through any directories with a link count
67 * of zero and redo the rm -rf
68 *
69 * mkdir f1/foo
70 * normal commit
71 * rm -rf f1/foo
72 * fsync(f1)
73 *
74 * The directory f1 was fully removed from the FS, but fsync was never
75 * called on f1, only its parent dir. After a crash the rm -rf must
76 * be replayed. This must be able to recurse down the entire
77 * directory tree. The inode link count fixup code takes care of the
78 * ugly details.
79 */
80
81/*
Chris Masone02119d2008-09-05 16:13:11 -040082 * stages for the tree walking. The first
83 * stage (0) is to only pin down the blocks we find
84 * the second stage (1) is to make sure that all the inodes
85 * we find in the log are created in the subvolume.
86 *
87 * The last stage is to deal with directories and links and extents
88 * and all the other fun semantics
89 */
90#define LOG_WALK_PIN_ONLY 0
91#define LOG_WALK_REPLAY_INODES 1
92#define LOG_WALK_REPLAY_ALL 2
93
Chris Mason12fcfd22009-03-24 10:24:20 -040094static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040095 struct btrfs_root *root, struct inode *inode,
96 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -050097static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400100static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_root *log,
103 struct btrfs_path *path,
104 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400105
106/*
107 * tree logging is a special write ahead log used to make sure that
108 * fsyncs and O_SYNCs can happen without doing full tree commits.
109 *
110 * Full tree commits are expensive because they require commonly
111 * modified blocks to be recowed, creating many dirty pages in the
112 * extent tree an 4x-6x higher write load than ext3.
113 *
114 * Instead of doing a tree commit on every fsync, we use the
115 * key ranges and transaction ids to find items for a given file or directory
116 * that have changed in this transaction. Those items are copied into
117 * a special tree (one per subvolume root), that tree is written to disk
118 * and then the fsync is considered complete.
119 *
120 * After a crash, items are copied out of the log-tree back into the
121 * subvolume tree. Any file data extents found are recorded in the extent
122 * allocation tree, and the log-tree freed.
123 *
124 * The log tree is read three times, once to pin down all the extents it is
125 * using in ram and once, once to create all the inodes logged in the tree
126 * and once to do all the other items.
127 */
128
129/*
Chris Masone02119d2008-09-05 16:13:11 -0400130 * start a sub transaction and setup the log tree
131 * this increments the log tree writer count to make the people
132 * syncing the tree wait for us to finish
133 */
134static int start_log_trans(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root)
136{
137 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400138 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500139
140 mutex_lock(&root->log_mutex);
141 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400142 if (!root->log_start_pid) {
143 root->log_start_pid = current->pid;
144 root->log_multiple_pids = false;
145 } else if (root->log_start_pid != current->pid) {
146 root->log_multiple_pids = true;
147 }
148
Yan Zheng7237f182009-01-21 12:54:03 -0500149 root->log_batch++;
150 atomic_inc(&root->log_writers);
151 mutex_unlock(&root->log_mutex);
152 return 0;
153 }
Josef Bacikff782e02009-10-08 15:30:04 -0400154 root->log_multiple_pids = false;
155 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400156 mutex_lock(&root->fs_info->tree_log_mutex);
157 if (!root->fs_info->log_root_tree) {
158 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400159 if (ret)
160 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400161 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400162 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400163 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400164 if (ret)
165 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Chris Masone02119d2008-09-05 16:13:11 -0400167 mutex_unlock(&root->fs_info->tree_log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -0500168 root->log_batch++;
169 atomic_inc(&root->log_writers);
170 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400171 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400172}
173
174/*
175 * returns 0 if there was a log transaction running and we were able
176 * to join, or returns -ENOENT if there were not transactions
177 * in progress
178 */
179static int join_running_log_trans(struct btrfs_root *root)
180{
181 int ret = -ENOENT;
182
183 smp_mb();
184 if (!root->log_root)
185 return -ENOENT;
186
Yan Zheng7237f182009-01-21 12:54:03 -0500187 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400188 if (root->log_root) {
189 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500190 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400191 }
Yan Zheng7237f182009-01-21 12:54:03 -0500192 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400193 return ret;
194}
195
196/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400197 * This either makes the current running log transaction wait
198 * until you call btrfs_end_log_trans() or it makes any future
199 * log transactions wait until you call btrfs_end_log_trans()
200 */
201int btrfs_pin_log_trans(struct btrfs_root *root)
202{
203 int ret = -ENOENT;
204
205 mutex_lock(&root->log_mutex);
206 atomic_inc(&root->log_writers);
207 mutex_unlock(&root->log_mutex);
208 return ret;
209}
210
211/*
Chris Masone02119d2008-09-05 16:13:11 -0400212 * indicate we're done making changes to the log tree
213 * and wake up anyone waiting to do a sync
214 */
Chris Mason12fcfd22009-03-24 10:24:20 -0400215int btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400216{
Yan Zheng7237f182009-01-21 12:54:03 -0500217 if (atomic_dec_and_test(&root->log_writers)) {
218 smp_mb();
219 if (waitqueue_active(&root->log_writer_wait))
220 wake_up(&root->log_writer_wait);
221 }
Chris Masone02119d2008-09-05 16:13:11 -0400222 return 0;
223}
224
225
226/*
227 * the walk control struct is used to pass state down the chain when
228 * processing the log tree. The stage field tells us which part
229 * of the log tree processing we are currently doing. The others
230 * are state fields used for that specific part
231 */
232struct walk_control {
233 /* should we free the extent on disk when done? This is used
234 * at transaction commit time while freeing a log tree
235 */
236 int free;
237
238 /* should we write out the extent buffer? This is used
239 * while flushing the log tree to disk during a sync
240 */
241 int write;
242
243 /* should we wait for the extent buffer io to finish? Also used
244 * while flushing the log tree to disk for a sync
245 */
246 int wait;
247
248 /* pin only walk, we record which extents on disk belong to the
249 * log trees
250 */
251 int pin;
252
253 /* what stage of the replay code we're currently in */
254 int stage;
255
256 /* the root we are currently replaying */
257 struct btrfs_root *replay_dest;
258
259 /* the trans handle for the current replay */
260 struct btrfs_trans_handle *trans;
261
262 /* the function that gets used to process blocks we find in the
263 * tree. Note the extent_buffer might not be up to date when it is
264 * passed in, and it must be checked or read if you need the data
265 * inside it
266 */
267 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
268 struct walk_control *wc, u64 gen);
269};
270
271/*
272 * process_func used to pin down extents, write them or wait on them
273 */
274static int process_one_buffer(struct btrfs_root *log,
275 struct extent_buffer *eb,
276 struct walk_control *wc, u64 gen)
277{
Josef Bacik04018de2009-04-03 10:14:18 -0400278 if (wc->pin)
Yan Zheng11833d62009-09-11 16:11:19 -0400279 btrfs_pin_extent(log->fs_info->extent_root,
280 eb->start, eb->len, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400281
282 if (btrfs_buffer_uptodate(eb, gen)) {
283 if (wc->write)
284 btrfs_write_tree_block(eb);
285 if (wc->wait)
286 btrfs_wait_tree_block_writeback(eb);
287 }
288 return 0;
289}
290
291/*
292 * Item overwrite used by replay and tree logging. eb, slot and key all refer
293 * to the src data we are copying out.
294 *
295 * root is the tree we are copying into, and path is a scratch
296 * path for use in this function (it should be released on entry and
297 * will be released on exit).
298 *
299 * If the key is already in the destination tree the existing item is
300 * overwritten. If the existing item isn't big enough, it is extended.
301 * If it is too large, it is truncated.
302 *
303 * If the key isn't in the destination yet, a new item is inserted.
304 */
305static noinline int overwrite_item(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root,
307 struct btrfs_path *path,
308 struct extent_buffer *eb, int slot,
309 struct btrfs_key *key)
310{
311 int ret;
312 u32 item_size;
313 u64 saved_i_size = 0;
314 int save_old_i_size = 0;
315 unsigned long src_ptr;
316 unsigned long dst_ptr;
317 int overwrite_root = 0;
318
319 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
320 overwrite_root = 1;
321
322 item_size = btrfs_item_size_nr(eb, slot);
323 src_ptr = btrfs_item_ptr_offset(eb, slot);
324
325 /* look for the key in the destination tree */
326 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
327 if (ret == 0) {
328 char *src_copy;
329 char *dst_copy;
330 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
331 path->slots[0]);
332 if (dst_size != item_size)
333 goto insert;
334
335 if (item_size == 0) {
336 btrfs_release_path(root, path);
337 return 0;
338 }
339 dst_copy = kmalloc(item_size, GFP_NOFS);
340 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000341 if (!dst_copy || !src_copy) {
342 btrfs_release_path(root, path);
343 kfree(dst_copy);
344 kfree(src_copy);
345 return -ENOMEM;
346 }
Chris Masone02119d2008-09-05 16:13:11 -0400347
348 read_extent_buffer(eb, src_copy, src_ptr, item_size);
349
350 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
351 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
352 item_size);
353 ret = memcmp(dst_copy, src_copy, item_size);
354
355 kfree(dst_copy);
356 kfree(src_copy);
357 /*
358 * they have the same contents, just return, this saves
359 * us from cowing blocks in the destination tree and doing
360 * extra writes that may not have been done by a previous
361 * sync
362 */
363 if (ret == 0) {
364 btrfs_release_path(root, path);
365 return 0;
366 }
367
368 }
369insert:
370 btrfs_release_path(root, path);
371 /* try to insert the key into the destination tree */
372 ret = btrfs_insert_empty_item(trans, root, path,
373 key, item_size);
374
375 /* make sure any existing item is the correct size */
376 if (ret == -EEXIST) {
377 u32 found_size;
378 found_size = btrfs_item_size_nr(path->nodes[0],
379 path->slots[0]);
380 if (found_size > item_size) {
381 btrfs_truncate_item(trans, root, path, item_size, 1);
382 } else if (found_size < item_size) {
Yan Zheng87b29b22008-12-17 10:21:48 -0500383 ret = btrfs_extend_item(trans, root, path,
384 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400385 }
386 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400387 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400388 }
389 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
390 path->slots[0]);
391
392 /* don't overwrite an existing inode if the generation number
393 * was logged as zero. This is done when the tree logging code
394 * is just logging an inode to make sure it exists after recovery.
395 *
396 * Also, don't overwrite i_size on directories during replay.
397 * log replay inserts and removes directory items based on the
398 * state of the tree found in the subvolume, and i_size is modified
399 * as it goes
400 */
401 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
402 struct btrfs_inode_item *src_item;
403 struct btrfs_inode_item *dst_item;
404
405 src_item = (struct btrfs_inode_item *)src_ptr;
406 dst_item = (struct btrfs_inode_item *)dst_ptr;
407
408 if (btrfs_inode_generation(eb, src_item) == 0)
409 goto no_copy;
410
411 if (overwrite_root &&
412 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
413 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
414 save_old_i_size = 1;
415 saved_i_size = btrfs_inode_size(path->nodes[0],
416 dst_item);
417 }
418 }
419
420 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
421 src_ptr, item_size);
422
423 if (save_old_i_size) {
424 struct btrfs_inode_item *dst_item;
425 dst_item = (struct btrfs_inode_item *)dst_ptr;
426 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
427 }
428
429 /* make sure the generation is filled in */
430 if (key->type == BTRFS_INODE_ITEM_KEY) {
431 struct btrfs_inode_item *dst_item;
432 dst_item = (struct btrfs_inode_item *)dst_ptr;
433 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
434 btrfs_set_inode_generation(path->nodes[0], dst_item,
435 trans->transid);
436 }
437 }
438no_copy:
439 btrfs_mark_buffer_dirty(path->nodes[0]);
440 btrfs_release_path(root, path);
441 return 0;
442}
443
444/*
445 * simple helper to read an inode off the disk from a given root
446 * This can only be called for subvolume roots and not for the log
447 */
448static noinline struct inode *read_one_inode(struct btrfs_root *root,
449 u64 objectid)
450{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400451 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400452 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400453
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400454 key.objectid = objectid;
455 key.type = BTRFS_INODE_ITEM_KEY;
456 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000457 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400458 if (IS_ERR(inode)) {
459 inode = NULL;
460 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400461 iput(inode);
462 inode = NULL;
463 }
464 return inode;
465}
466
467/* replays a single extent in 'eb' at 'slot' with 'key' into the
468 * subvolume 'root'. path is released on entry and should be released
469 * on exit.
470 *
471 * extents in the log tree have not been allocated out of the extent
472 * tree yet. So, this completes the allocation, taking a reference
473 * as required if the extent already exists or creating a new extent
474 * if it isn't in the extent allocation tree yet.
475 *
476 * The extent is inserted into the file, dropping any existing extents
477 * from the file that overlap the new one.
478 */
479static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root,
481 struct btrfs_path *path,
482 struct extent_buffer *eb, int slot,
483 struct btrfs_key *key)
484{
485 int found_type;
486 u64 mask = root->sectorsize - 1;
487 u64 extent_end;
488 u64 alloc_hint;
489 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500490 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400491 struct btrfs_file_extent_item *item;
492 struct inode *inode = NULL;
493 unsigned long size;
494 int ret = 0;
495
496 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
497 found_type = btrfs_file_extent_type(eb, item);
498
Yan Zhengd899e052008-10-30 14:25:28 -0400499 if (found_type == BTRFS_FILE_EXTENT_REG ||
500 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400501 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
502 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400504 extent_end = (start + size + mask) & ~mask;
505 } else {
506 ret = 0;
507 goto out;
508 }
509
510 inode = read_one_inode(root, key->objectid);
511 if (!inode) {
512 ret = -EIO;
513 goto out;
514 }
515
516 /*
517 * first check to see if we already have this extent in the
518 * file. This must be done before the btrfs_drop_extents run
519 * so we don't try to drop this extent.
520 */
521 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
522 start, 0);
523
Yan Zhengd899e052008-10-30 14:25:28 -0400524 if (ret == 0 &&
525 (found_type == BTRFS_FILE_EXTENT_REG ||
526 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400527 struct btrfs_file_extent_item cmp1;
528 struct btrfs_file_extent_item cmp2;
529 struct btrfs_file_extent_item *existing;
530 struct extent_buffer *leaf;
531
532 leaf = path->nodes[0];
533 existing = btrfs_item_ptr(leaf, path->slots[0],
534 struct btrfs_file_extent_item);
535
536 read_extent_buffer(eb, &cmp1, (unsigned long)item,
537 sizeof(cmp1));
538 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
539 sizeof(cmp2));
540
541 /*
542 * we already have a pointer to this exact extent,
543 * we don't have to do anything
544 */
545 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
546 btrfs_release_path(root, path);
547 goto out;
548 }
549 }
550 btrfs_release_path(root, path);
551
Yan Zheng07d400a2009-01-06 11:42:00 -0500552 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400553 /* drop any overlapping extents */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000554 ret = btrfs_drop_extents(trans, inode, start, extent_end,
555 &alloc_hint, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400556 BUG_ON(ret);
557
Yan Zheng07d400a2009-01-06 11:42:00 -0500558 if (found_type == BTRFS_FILE_EXTENT_REG ||
559 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500561 unsigned long dest_offset;
562 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400563
Yan Zheng07d400a2009-01-06 11:42:00 -0500564 ret = btrfs_insert_empty_item(trans, root, path, key,
565 sizeof(*item));
566 BUG_ON(ret);
567 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
568 path->slots[0]);
569 copy_extent_buffer(path->nodes[0], eb, dest_offset,
570 (unsigned long)item, sizeof(*item));
571
572 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
573 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
574 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400575 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500576
577 if (ins.objectid > 0) {
578 u64 csum_start;
579 u64 csum_end;
580 LIST_HEAD(ordered_sums);
581 /*
582 * is this extent already allocated in the extent
583 * allocation tree? If so, just add a reference
584 */
585 ret = btrfs_lookup_extent(root, ins.objectid,
586 ins.offset);
587 if (ret == 0) {
588 ret = btrfs_inc_extent_ref(trans, root,
589 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 0, root->root_key.objectid,
591 key->objectid, offset);
Tsutomu Itoh37daa4f2011-04-28 09:18:21 +0000592 BUG_ON(ret);
Yan Zheng07d400a2009-01-06 11:42:00 -0500593 } else {
594 /*
595 * insert the extent pointer in the extent
596 * allocation tree
597 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 ret = btrfs_alloc_logged_file_extent(trans,
599 root, root->root_key.objectid,
600 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500601 BUG_ON(ret);
602 }
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);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000681 if (!inode) {
682 kfree(name);
683 return -EIO;
684 }
Chris Masone02119d2008-09-05 16:13:11 -0400685
Yan Zhengec051c02009-01-05 15:43:42 -0500686 ret = link_to_fixup_dir(trans, root, path, location.objectid);
687 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400688
Chris Masone02119d2008-09-05 16:13:11 -0400689 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500690 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400691 kfree(name);
692
693 iput(inode);
694 return ret;
695}
696
697/*
698 * helper function to see if a given name and sequence number found
699 * in an inode back reference are already in a directory and correctly
700 * point to this inode
701 */
702static noinline int inode_in_dir(struct btrfs_root *root,
703 struct btrfs_path *path,
704 u64 dirid, u64 objectid, u64 index,
705 const char *name, int name_len)
706{
707 struct btrfs_dir_item *di;
708 struct btrfs_key location;
709 int match = 0;
710
711 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
712 index, name, name_len, 0);
713 if (di && !IS_ERR(di)) {
714 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
715 if (location.objectid != objectid)
716 goto out;
717 } else
718 goto out;
719 btrfs_release_path(root, path);
720
721 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
722 if (di && !IS_ERR(di)) {
723 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
724 if (location.objectid != objectid)
725 goto out;
726 } else
727 goto out;
728 match = 1;
729out:
730 btrfs_release_path(root, path);
731 return match;
732}
733
734/*
735 * helper function to check a log tree for a named back reference in
736 * an inode. This is used to decide if a back reference that is
737 * found in the subvolume conflicts with what we find in the log.
738 *
739 * inode backreferences may have multiple refs in a single item,
740 * during replay we process one reference at a time, and we don't
741 * want to delete valid links to a file from the subvolume if that
742 * link is also in the log.
743 */
744static noinline int backref_in_log(struct btrfs_root *log,
745 struct btrfs_key *key,
746 char *name, int namelen)
747{
748 struct btrfs_path *path;
749 struct btrfs_inode_ref *ref;
750 unsigned long ptr;
751 unsigned long ptr_end;
752 unsigned long name_ptr;
753 int found_name_len;
754 int item_size;
755 int ret;
756 int match = 0;
757
758 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000759 if (!path)
760 return -ENOMEM;
761
Chris Masone02119d2008-09-05 16:13:11 -0400762 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
763 if (ret != 0)
764 goto out;
765
766 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
767 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
768 ptr_end = ptr + item_size;
769 while (ptr < ptr_end) {
770 ref = (struct btrfs_inode_ref *)ptr;
771 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
772 if (found_name_len == namelen) {
773 name_ptr = (unsigned long)(ref + 1);
774 ret = memcmp_extent_buffer(path->nodes[0], name,
775 name_ptr, namelen);
776 if (ret == 0) {
777 match = 1;
778 goto out;
779 }
780 }
781 ptr = (unsigned long)(ref + 1) + found_name_len;
782 }
783out:
784 btrfs_free_path(path);
785 return match;
786}
787
788
789/*
790 * replay one inode back reference item found in the log tree.
791 * eb, slot and key refer to the buffer and key found in the log tree.
792 * root is the destination we are replaying into, and path is for temp
793 * use by this function. (it should be released on return).
794 */
795static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
796 struct btrfs_root *root,
797 struct btrfs_root *log,
798 struct btrfs_path *path,
799 struct extent_buffer *eb, int slot,
800 struct btrfs_key *key)
801{
802 struct inode *dir;
803 int ret;
Chris Masone02119d2008-09-05 16:13:11 -0400804 struct btrfs_inode_ref *ref;
Chris Masone02119d2008-09-05 16:13:11 -0400805 struct inode *inode;
806 char *name;
807 int namelen;
808 unsigned long ref_ptr;
809 unsigned long ref_end;
liuboc622ae62011-03-26 08:01:12 -0400810 int search_done = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400811
Chris Masone02119d2008-09-05 16:13:11 -0400812 /*
813 * it is possible that we didn't log all the parent directories
814 * for a given inode. If we don't find the dir, just don't
815 * copy the back ref in. The link count fixup code will take
816 * care of the rest
817 */
818 dir = read_one_inode(root, key->offset);
819 if (!dir)
820 return -ENOENT;
821
822 inode = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000823 if (!inode) {
824 iput(dir);
825 return -EIO;
826 }
Chris Masone02119d2008-09-05 16:13:11 -0400827
828 ref_ptr = btrfs_item_ptr_offset(eb, slot);
829 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
830
831again:
832 ref = (struct btrfs_inode_ref *)ref_ptr;
833
834 namelen = btrfs_inode_ref_name_len(eb, ref);
835 name = kmalloc(namelen, GFP_NOFS);
836 BUG_ON(!name);
837
838 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
839
840 /* if we already have a perfect match, we're done */
841 if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
842 btrfs_inode_ref_index(eb, ref),
843 name, namelen)) {
844 goto out;
845 }
846
847 /*
848 * look for a conflicting back reference in the metadata.
849 * if we find one we have to unlink that name of the file
850 * before we add our new link. Later on, we overwrite any
851 * existing back reference, and we don't want to create
852 * dangling pointers in the directory.
853 */
liuboc622ae62011-03-26 08:01:12 -0400854
855 if (search_done)
856 goto insert;
857
Chris Masone02119d2008-09-05 16:13:11 -0400858 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
859 if (ret == 0) {
860 char *victim_name;
861 int victim_name_len;
862 struct btrfs_inode_ref *victim_ref;
863 unsigned long ptr;
864 unsigned long ptr_end;
865 struct extent_buffer *leaf = path->nodes[0];
866
867 /* are we trying to overwrite a back ref for the root directory
868 * if so, just jump out, we're done
869 */
870 if (key->objectid == key->offset)
871 goto out_nowrite;
872
873 /* check all the names in this back reference to see
874 * if they are in the log. if so, we allow them to stay
875 * otherwise they must be unlinked as a conflict
876 */
877 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
878 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500879 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400880 victim_ref = (struct btrfs_inode_ref *)ptr;
881 victim_name_len = btrfs_inode_ref_name_len(leaf,
882 victim_ref);
883 victim_name = kmalloc(victim_name_len, GFP_NOFS);
884 BUG_ON(!victim_name);
885
886 read_extent_buffer(leaf, victim_name,
887 (unsigned long)(victim_ref + 1),
888 victim_name_len);
889
890 if (!backref_in_log(log, key, victim_name,
891 victim_name_len)) {
892 btrfs_inc_nlink(inode);
893 btrfs_release_path(root, path);
Chris Mason12fcfd22009-03-24 10:24:20 -0400894
Chris Masone02119d2008-09-05 16:13:11 -0400895 ret = btrfs_unlink_inode(trans, root, dir,
896 inode, victim_name,
897 victim_name_len);
Chris Masone02119d2008-09-05 16:13:11 -0400898 }
899 kfree(victim_name);
900 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
901 }
902 BUG_ON(ret);
liuboc622ae62011-03-26 08:01:12 -0400903
904 /*
905 * NOTE: we have searched root tree and checked the
906 * coresponding ref, it does not need to check again.
907 */
908 search_done = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400909 }
910 btrfs_release_path(root, path);
911
liuboc622ae62011-03-26 08:01:12 -0400912insert:
Chris Masone02119d2008-09-05 16:13:11 -0400913 /* insert our name */
914 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
915 btrfs_inode_ref_index(eb, ref));
916 BUG_ON(ret);
917
918 btrfs_update_inode(trans, root, inode);
919
920out:
921 ref_ptr = (unsigned long)(ref + 1) + namelen;
922 kfree(name);
923 if (ref_ptr < ref_end)
924 goto again;
925
926 /* finally write the back reference in the inode */
927 ret = overwrite_item(trans, root, path, eb, slot, key);
928 BUG_ON(ret);
929
930out_nowrite:
931 btrfs_release_path(root, path);
932 iput(dir);
933 iput(inode);
934 return 0;
935}
936
Yan, Zhengc71bf092009-11-12 09:34:40 +0000937static int insert_orphan_item(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root, u64 offset)
939{
940 int ret;
941 ret = btrfs_find_orphan_item(root, offset);
942 if (ret > 0)
943 ret = btrfs_insert_orphan_item(trans, root, offset);
944 return ret;
945}
946
947
Chris Masone02119d2008-09-05 16:13:11 -0400948/*
Chris Masone02119d2008-09-05 16:13:11 -0400949 * There are a few corners where the link count of the file can't
950 * be properly maintained during replay. So, instead of adding
951 * lots of complexity to the log code, we just scan the backrefs
952 * for any file that has been through replay.
953 *
954 * The scan will update the link count on the inode to reflect the
955 * number of back refs found. If it goes down to zero, the iput
956 * will free the inode.
957 */
958static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
959 struct btrfs_root *root,
960 struct inode *inode)
961{
962 struct btrfs_path *path;
963 int ret;
964 struct btrfs_key key;
965 u64 nlink = 0;
966 unsigned long ptr;
967 unsigned long ptr_end;
968 int name_len;
969
970 key.objectid = inode->i_ino;
971 key.type = BTRFS_INODE_REF_KEY;
972 key.offset = (u64)-1;
973
974 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000975 if (!path)
976 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -0400977
Chris Masond3977122009-01-05 21:25:51 -0500978 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -0400979 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
980 if (ret < 0)
981 break;
982 if (ret > 0) {
983 if (path->slots[0] == 0)
984 break;
985 path->slots[0]--;
986 }
987 btrfs_item_key_to_cpu(path->nodes[0], &key,
988 path->slots[0]);
989 if (key.objectid != inode->i_ino ||
990 key.type != BTRFS_INODE_REF_KEY)
991 break;
992 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
993 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
994 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500995 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400996 struct btrfs_inode_ref *ref;
997
998 ref = (struct btrfs_inode_ref *)ptr;
999 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1000 ref);
1001 ptr = (unsigned long)(ref + 1) + name_len;
1002 nlink++;
1003 }
1004
1005 if (key.offset == 0)
1006 break;
1007 key.offset--;
1008 btrfs_release_path(root, path);
1009 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001010 btrfs_release_path(root, path);
Chris Masone02119d2008-09-05 16:13:11 -04001011 if (nlink != inode->i_nlink) {
1012 inode->i_nlink = nlink;
1013 btrfs_update_inode(trans, root, inode);
1014 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001015 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001016
Yan, Zhengc71bf092009-11-12 09:34:40 +00001017 if (inode->i_nlink == 0) {
1018 if (S_ISDIR(inode->i_mode)) {
1019 ret = replay_dir_deletes(trans, root, NULL, path,
1020 inode->i_ino, 1);
1021 BUG_ON(ret);
1022 }
1023 ret = insert_orphan_item(trans, root, inode->i_ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001024 BUG_ON(ret);
1025 }
1026 btrfs_free_path(path);
1027
Chris Masone02119d2008-09-05 16:13:11 -04001028 return 0;
1029}
1030
1031static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1032 struct btrfs_root *root,
1033 struct btrfs_path *path)
1034{
1035 int ret;
1036 struct btrfs_key key;
1037 struct inode *inode;
1038
1039 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1040 key.type = BTRFS_ORPHAN_ITEM_KEY;
1041 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001042 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001043 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1044 if (ret < 0)
1045 break;
1046
1047 if (ret == 1) {
1048 if (path->slots[0] == 0)
1049 break;
1050 path->slots[0]--;
1051 }
1052
1053 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1054 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1055 key.type != BTRFS_ORPHAN_ITEM_KEY)
1056 break;
1057
1058 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001059 if (ret)
1060 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001061
1062 btrfs_release_path(root, path);
1063 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001064 if (!inode)
1065 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001066
1067 ret = fixup_inode_link_count(trans, root, inode);
1068 BUG_ON(ret);
1069
1070 iput(inode);
1071
Chris Mason12fcfd22009-03-24 10:24:20 -04001072 /*
1073 * fixup on a directory may create new entries,
1074 * make sure we always look for the highset possible
1075 * offset
1076 */
1077 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001078 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001079 ret = 0;
1080out:
Chris Masone02119d2008-09-05 16:13:11 -04001081 btrfs_release_path(root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001082 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001083}
1084
1085
1086/*
1087 * record a given inode in the fixup dir so we can check its link
1088 * count when replay is done. The link count is incremented here
1089 * so the inode won't go away until we check it
1090 */
1091static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1092 struct btrfs_root *root,
1093 struct btrfs_path *path,
1094 u64 objectid)
1095{
1096 struct btrfs_key key;
1097 int ret = 0;
1098 struct inode *inode;
1099
1100 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001101 if (!inode)
1102 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001103
1104 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1105 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1106 key.offset = objectid;
1107
1108 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1109
1110 btrfs_release_path(root, path);
1111 if (ret == 0) {
1112 btrfs_inc_nlink(inode);
1113 btrfs_update_inode(trans, root, inode);
1114 } else if (ret == -EEXIST) {
1115 ret = 0;
1116 } else {
1117 BUG();
1118 }
1119 iput(inode);
1120
1121 return ret;
1122}
1123
1124/*
1125 * when replaying the log for a directory, we only insert names
1126 * for inodes that actually exist. This means an fsync on a directory
1127 * does not implicitly fsync all the new files in it
1128 */
1129static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1130 struct btrfs_root *root,
1131 struct btrfs_path *path,
1132 u64 dirid, u64 index,
1133 char *name, int name_len, u8 type,
1134 struct btrfs_key *location)
1135{
1136 struct inode *inode;
1137 struct inode *dir;
1138 int ret;
1139
1140 inode = read_one_inode(root, location->objectid);
1141 if (!inode)
1142 return -ENOENT;
1143
1144 dir = read_one_inode(root, dirid);
1145 if (!dir) {
1146 iput(inode);
1147 return -EIO;
1148 }
1149 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1150
1151 /* FIXME, put inode into FIXUP list */
1152
1153 iput(inode);
1154 iput(dir);
1155 return ret;
1156}
1157
1158/*
1159 * take a single entry in a log directory item and replay it into
1160 * the subvolume.
1161 *
1162 * if a conflicting item exists in the subdirectory already,
1163 * the inode it points to is unlinked and put into the link count
1164 * fix up tree.
1165 *
1166 * If a name from the log points to a file or directory that does
1167 * not exist in the FS, it is skipped. fsyncs on directories
1168 * do not force down inodes inside that directory, just changes to the
1169 * names or unlinks in a directory.
1170 */
1171static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root,
1173 struct btrfs_path *path,
1174 struct extent_buffer *eb,
1175 struct btrfs_dir_item *di,
1176 struct btrfs_key *key)
1177{
1178 char *name;
1179 int name_len;
1180 struct btrfs_dir_item *dst_di;
1181 struct btrfs_key found_key;
1182 struct btrfs_key log_key;
1183 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001184 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001185 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001186 int ret;
1187
1188 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001189 if (!dir)
1190 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001191
1192 name_len = btrfs_dir_name_len(eb, di);
1193 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001194 if (!name)
1195 return -ENOMEM;
1196
Chris Masone02119d2008-09-05 16:13:11 -04001197 log_type = btrfs_dir_type(eb, di);
1198 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1199 name_len);
1200
1201 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001202 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1203 if (exists == 0)
1204 exists = 1;
1205 else
1206 exists = 0;
1207 btrfs_release_path(root, path);
1208
Chris Masone02119d2008-09-05 16:13:11 -04001209 if (key->type == BTRFS_DIR_ITEM_KEY) {
1210 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1211 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001212 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001213 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1214 key->objectid,
1215 key->offset, name,
1216 name_len, 1);
1217 } else {
1218 BUG();
1219 }
1220 if (!dst_di || IS_ERR(dst_di)) {
1221 /* we need a sequence number to insert, so we only
1222 * do inserts for the BTRFS_DIR_INDEX_KEY types
1223 */
1224 if (key->type != BTRFS_DIR_INDEX_KEY)
1225 goto out;
1226 goto insert;
1227 }
1228
1229 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1230 /* the existing item matches the logged item */
1231 if (found_key.objectid == log_key.objectid &&
1232 found_key.type == log_key.type &&
1233 found_key.offset == log_key.offset &&
1234 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1235 goto out;
1236 }
1237
1238 /*
1239 * don't drop the conflicting directory entry if the inode
1240 * for the new entry doesn't exist
1241 */
Chris Mason4bef0842008-09-08 11:18:08 -04001242 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001243 goto out;
1244
Chris Masone02119d2008-09-05 16:13:11 -04001245 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1246 BUG_ON(ret);
1247
1248 if (key->type == BTRFS_DIR_INDEX_KEY)
1249 goto insert;
1250out:
1251 btrfs_release_path(root, path);
1252 kfree(name);
1253 iput(dir);
1254 return 0;
1255
1256insert:
1257 btrfs_release_path(root, path);
1258 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1259 name, name_len, log_type, &log_key);
1260
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001261 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001262 goto out;
1263}
1264
1265/*
1266 * find all the names in a directory item and reconcile them into
1267 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1268 * one name in a directory item, but the same code gets used for
1269 * both directory index types
1270 */
1271static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1272 struct btrfs_root *root,
1273 struct btrfs_path *path,
1274 struct extent_buffer *eb, int slot,
1275 struct btrfs_key *key)
1276{
1277 int ret;
1278 u32 item_size = btrfs_item_size_nr(eb, slot);
1279 struct btrfs_dir_item *di;
1280 int name_len;
1281 unsigned long ptr;
1282 unsigned long ptr_end;
1283
1284 ptr = btrfs_item_ptr_offset(eb, slot);
1285 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001286 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001287 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001288 if (verify_dir_item(root, eb, di))
1289 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001290 name_len = btrfs_dir_name_len(eb, di);
1291 ret = replay_one_name(trans, root, path, eb, di, key);
1292 BUG_ON(ret);
1293 ptr = (unsigned long)(di + 1);
1294 ptr += name_len;
1295 }
1296 return 0;
1297}
1298
1299/*
1300 * directory replay has two parts. There are the standard directory
1301 * items in the log copied from the subvolume, and range items
1302 * created in the log while the subvolume was logged.
1303 *
1304 * The range items tell us which parts of the key space the log
1305 * is authoritative for. During replay, if a key in the subvolume
1306 * directory is in a logged range item, but not actually in the log
1307 * that means it was deleted from the directory before the fsync
1308 * and should be removed.
1309 */
1310static noinline int find_dir_range(struct btrfs_root *root,
1311 struct btrfs_path *path,
1312 u64 dirid, int key_type,
1313 u64 *start_ret, u64 *end_ret)
1314{
1315 struct btrfs_key key;
1316 u64 found_end;
1317 struct btrfs_dir_log_item *item;
1318 int ret;
1319 int nritems;
1320
1321 if (*start_ret == (u64)-1)
1322 return 1;
1323
1324 key.objectid = dirid;
1325 key.type = key_type;
1326 key.offset = *start_ret;
1327
1328 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1329 if (ret < 0)
1330 goto out;
1331 if (ret > 0) {
1332 if (path->slots[0] == 0)
1333 goto out;
1334 path->slots[0]--;
1335 }
1336 if (ret != 0)
1337 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1338
1339 if (key.type != key_type || key.objectid != dirid) {
1340 ret = 1;
1341 goto next;
1342 }
1343 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1344 struct btrfs_dir_log_item);
1345 found_end = btrfs_dir_log_end(path->nodes[0], item);
1346
1347 if (*start_ret >= key.offset && *start_ret <= found_end) {
1348 ret = 0;
1349 *start_ret = key.offset;
1350 *end_ret = found_end;
1351 goto out;
1352 }
1353 ret = 1;
1354next:
1355 /* check the next slot in the tree to see if it is a valid item */
1356 nritems = btrfs_header_nritems(path->nodes[0]);
1357 if (path->slots[0] >= nritems) {
1358 ret = btrfs_next_leaf(root, path);
1359 if (ret)
1360 goto out;
1361 } else {
1362 path->slots[0]++;
1363 }
1364
1365 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1366
1367 if (key.type != key_type || key.objectid != dirid) {
1368 ret = 1;
1369 goto out;
1370 }
1371 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1372 struct btrfs_dir_log_item);
1373 found_end = btrfs_dir_log_end(path->nodes[0], item);
1374 *start_ret = key.offset;
1375 *end_ret = found_end;
1376 ret = 0;
1377out:
1378 btrfs_release_path(root, path);
1379 return ret;
1380}
1381
1382/*
1383 * this looks for a given directory item in the log. If the directory
1384 * item is not in the log, the item is removed and the inode it points
1385 * to is unlinked
1386 */
1387static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *root,
1389 struct btrfs_root *log,
1390 struct btrfs_path *path,
1391 struct btrfs_path *log_path,
1392 struct inode *dir,
1393 struct btrfs_key *dir_key)
1394{
1395 int ret;
1396 struct extent_buffer *eb;
1397 int slot;
1398 u32 item_size;
1399 struct btrfs_dir_item *di;
1400 struct btrfs_dir_item *log_di;
1401 int name_len;
1402 unsigned long ptr;
1403 unsigned long ptr_end;
1404 char *name;
1405 struct inode *inode;
1406 struct btrfs_key location;
1407
1408again:
1409 eb = path->nodes[0];
1410 slot = path->slots[0];
1411 item_size = btrfs_item_size_nr(eb, slot);
1412 ptr = btrfs_item_ptr_offset(eb, slot);
1413 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001414 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001415 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001416 if (verify_dir_item(root, eb, di)) {
1417 ret = -EIO;
1418 goto out;
1419 }
1420
Chris Masone02119d2008-09-05 16:13:11 -04001421 name_len = btrfs_dir_name_len(eb, di);
1422 name = kmalloc(name_len, GFP_NOFS);
1423 if (!name) {
1424 ret = -ENOMEM;
1425 goto out;
1426 }
1427 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1428 name_len);
1429 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001430 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001431 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1432 dir_key->objectid,
1433 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001434 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001435 log_di = btrfs_lookup_dir_index_item(trans, log,
1436 log_path,
1437 dir_key->objectid,
1438 dir_key->offset,
1439 name, name_len, 0);
1440 }
1441 if (!log_di || IS_ERR(log_di)) {
1442 btrfs_dir_item_key_to_cpu(eb, di, &location);
1443 btrfs_release_path(root, path);
1444 btrfs_release_path(log, log_path);
1445 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001446 if (!inode) {
1447 kfree(name);
1448 return -EIO;
1449 }
Chris Masone02119d2008-09-05 16:13:11 -04001450
1451 ret = link_to_fixup_dir(trans, root,
1452 path, location.objectid);
1453 BUG_ON(ret);
1454 btrfs_inc_nlink(inode);
1455 ret = btrfs_unlink_inode(trans, root, dir, inode,
1456 name, name_len);
1457 BUG_ON(ret);
1458 kfree(name);
1459 iput(inode);
1460
1461 /* there might still be more names under this key
1462 * check and repeat if required
1463 */
1464 ret = btrfs_search_slot(NULL, root, dir_key, path,
1465 0, 0);
1466 if (ret == 0)
1467 goto again;
1468 ret = 0;
1469 goto out;
1470 }
1471 btrfs_release_path(log, log_path);
1472 kfree(name);
1473
1474 ptr = (unsigned long)(di + 1);
1475 ptr += name_len;
1476 }
1477 ret = 0;
1478out:
1479 btrfs_release_path(root, path);
1480 btrfs_release_path(log, log_path);
1481 return ret;
1482}
1483
1484/*
1485 * deletion replay happens before we copy any new directory items
1486 * out of the log or out of backreferences from inodes. It
1487 * scans the log to find ranges of keys that log is authoritative for,
1488 * and then scans the directory to find items in those ranges that are
1489 * not present in the log.
1490 *
1491 * Anything we don't find in the log is unlinked and removed from the
1492 * directory.
1493 */
1494static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1495 struct btrfs_root *root,
1496 struct btrfs_root *log,
1497 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001498 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001499{
1500 u64 range_start;
1501 u64 range_end;
1502 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1503 int ret = 0;
1504 struct btrfs_key dir_key;
1505 struct btrfs_key found_key;
1506 struct btrfs_path *log_path;
1507 struct inode *dir;
1508
1509 dir_key.objectid = dirid;
1510 dir_key.type = BTRFS_DIR_ITEM_KEY;
1511 log_path = btrfs_alloc_path();
1512 if (!log_path)
1513 return -ENOMEM;
1514
1515 dir = read_one_inode(root, dirid);
1516 /* it isn't an error if the inode isn't there, that can happen
1517 * because we replay the deletes before we copy in the inode item
1518 * from the log
1519 */
1520 if (!dir) {
1521 btrfs_free_path(log_path);
1522 return 0;
1523 }
1524again:
1525 range_start = 0;
1526 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001527 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001528 if (del_all)
1529 range_end = (u64)-1;
1530 else {
1531 ret = find_dir_range(log, path, dirid, key_type,
1532 &range_start, &range_end);
1533 if (ret != 0)
1534 break;
1535 }
Chris Masone02119d2008-09-05 16:13:11 -04001536
1537 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001538 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001539 int nritems;
1540 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1541 0, 0);
1542 if (ret < 0)
1543 goto out;
1544
1545 nritems = btrfs_header_nritems(path->nodes[0]);
1546 if (path->slots[0] >= nritems) {
1547 ret = btrfs_next_leaf(root, path);
1548 if (ret)
1549 break;
1550 }
1551 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1552 path->slots[0]);
1553 if (found_key.objectid != dirid ||
1554 found_key.type != dir_key.type)
1555 goto next_type;
1556
1557 if (found_key.offset > range_end)
1558 break;
1559
1560 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001561 log_path, dir,
1562 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001563 BUG_ON(ret);
1564 if (found_key.offset == (u64)-1)
1565 break;
1566 dir_key.offset = found_key.offset + 1;
1567 }
1568 btrfs_release_path(root, path);
1569 if (range_end == (u64)-1)
1570 break;
1571 range_start = range_end + 1;
1572 }
1573
1574next_type:
1575 ret = 0;
1576 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1577 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1578 dir_key.type = BTRFS_DIR_INDEX_KEY;
1579 btrfs_release_path(root, path);
1580 goto again;
1581 }
1582out:
1583 btrfs_release_path(root, path);
1584 btrfs_free_path(log_path);
1585 iput(dir);
1586 return ret;
1587}
1588
1589/*
1590 * the process_func used to replay items from the log tree. This
1591 * gets called in two different stages. The first stage just looks
1592 * for inodes and makes sure they are all copied into the subvolume.
1593 *
1594 * The second stage copies all the other item types from the log into
1595 * the subvolume. The two stage approach is slower, but gets rid of
1596 * lots of complexity around inodes referencing other inodes that exist
1597 * only in the log (references come from either directory items or inode
1598 * back refs).
1599 */
1600static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1601 struct walk_control *wc, u64 gen)
1602{
1603 int nritems;
1604 struct btrfs_path *path;
1605 struct btrfs_root *root = wc->replay_dest;
1606 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001607 int level;
1608 int i;
1609 int ret;
1610
1611 btrfs_read_buffer(eb, gen);
1612
1613 level = btrfs_header_level(eb);
1614
1615 if (level != 0)
1616 return 0;
1617
1618 path = btrfs_alloc_path();
1619 BUG_ON(!path);
1620
1621 nritems = btrfs_header_nritems(eb);
1622 for (i = 0; i < nritems; i++) {
1623 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001624
1625 /* inode keys are done during the first stage */
1626 if (key.type == BTRFS_INODE_ITEM_KEY &&
1627 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001628 struct btrfs_inode_item *inode_item;
1629 u32 mode;
1630
1631 inode_item = btrfs_item_ptr(eb, i,
1632 struct btrfs_inode_item);
1633 mode = btrfs_inode_mode(eb, inode_item);
1634 if (S_ISDIR(mode)) {
1635 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001636 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001637 BUG_ON(ret);
1638 }
1639 ret = overwrite_item(wc->trans, root, path,
1640 eb, i, &key);
1641 BUG_ON(ret);
1642
Yan, Zhengc71bf092009-11-12 09:34:40 +00001643 /* for regular files, make sure corresponding
1644 * orhpan item exist. extents past the new EOF
1645 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001646 */
1647 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001648 ret = insert_orphan_item(wc->trans, root,
1649 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001650 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001651 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001652
Chris Masone02119d2008-09-05 16:13:11 -04001653 ret = link_to_fixup_dir(wc->trans, root,
1654 path, key.objectid);
1655 BUG_ON(ret);
1656 }
1657 if (wc->stage < LOG_WALK_REPLAY_ALL)
1658 continue;
1659
1660 /* these keys are simply copied */
1661 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1662 ret = overwrite_item(wc->trans, root, path,
1663 eb, i, &key);
1664 BUG_ON(ret);
1665 } else if (key.type == BTRFS_INODE_REF_KEY) {
1666 ret = add_inode_ref(wc->trans, root, log, path,
1667 eb, i, &key);
1668 BUG_ON(ret && ret != -ENOENT);
1669 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1670 ret = replay_one_extent(wc->trans, root, path,
1671 eb, i, &key);
1672 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001673 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1674 key.type == BTRFS_DIR_INDEX_KEY) {
1675 ret = replay_one_dir_item(wc->trans, root, path,
1676 eb, i, &key);
1677 BUG_ON(ret);
1678 }
1679 }
1680 btrfs_free_path(path);
1681 return 0;
1682}
1683
Chris Masond3977122009-01-05 21:25:51 -05001684static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001685 struct btrfs_root *root,
1686 struct btrfs_path *path, int *level,
1687 struct walk_control *wc)
1688{
1689 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001690 u64 bytenr;
1691 u64 ptr_gen;
1692 struct extent_buffer *next;
1693 struct extent_buffer *cur;
1694 struct extent_buffer *parent;
1695 u32 blocksize;
1696 int ret = 0;
1697
1698 WARN_ON(*level < 0);
1699 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1700
Chris Masond3977122009-01-05 21:25:51 -05001701 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001702 WARN_ON(*level < 0);
1703 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1704 cur = path->nodes[*level];
1705
1706 if (btrfs_header_level(cur) != *level)
1707 WARN_ON(1);
1708
1709 if (path->slots[*level] >=
1710 btrfs_header_nritems(cur))
1711 break;
1712
1713 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1714 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1715 blocksize = btrfs_level_size(root, *level - 1);
1716
1717 parent = path->nodes[*level];
1718 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001719
1720 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00001721 if (!next)
1722 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001723
Chris Masone02119d2008-09-05 16:13:11 -04001724 if (*level == 1) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001725 wc->process_func(root, next, wc, ptr_gen);
1726
Chris Masone02119d2008-09-05 16:13:11 -04001727 path->slots[*level]++;
1728 if (wc->free) {
1729 btrfs_read_buffer(next, ptr_gen);
1730
1731 btrfs_tree_lock(next);
1732 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001733 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001734 btrfs_wait_tree_block_writeback(next);
1735 btrfs_tree_unlock(next);
1736
Chris Masone02119d2008-09-05 16:13:11 -04001737 WARN_ON(root_owner !=
1738 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001739 ret = btrfs_free_reserved_extent(root,
1740 bytenr, blocksize);
Chris Masone02119d2008-09-05 16:13:11 -04001741 BUG_ON(ret);
1742 }
1743 free_extent_buffer(next);
1744 continue;
1745 }
1746 btrfs_read_buffer(next, ptr_gen);
1747
1748 WARN_ON(*level <= 0);
1749 if (path->nodes[*level-1])
1750 free_extent_buffer(path->nodes[*level-1]);
1751 path->nodes[*level-1] = next;
1752 *level = btrfs_header_level(next);
1753 path->slots[*level] = 0;
1754 cond_resched();
1755 }
1756 WARN_ON(*level < 0);
1757 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1758
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001759 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04001760
1761 cond_resched();
1762 return 0;
1763}
1764
Chris Masond3977122009-01-05 21:25:51 -05001765static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001766 struct btrfs_root *root,
1767 struct btrfs_path *path, int *level,
1768 struct walk_control *wc)
1769{
1770 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001771 int i;
1772 int slot;
1773 int ret;
1774
Chris Masond3977122009-01-05 21:25:51 -05001775 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04001776 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001777 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04001778 path->slots[i]++;
1779 *level = i;
1780 WARN_ON(*level == 0);
1781 return 0;
1782 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001783 struct extent_buffer *parent;
1784 if (path->nodes[*level] == root->node)
1785 parent = path->nodes[*level];
1786 else
1787 parent = path->nodes[*level + 1];
1788
1789 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001790 wc->process_func(root, path->nodes[*level], wc,
1791 btrfs_header_generation(path->nodes[*level]));
1792 if (wc->free) {
1793 struct extent_buffer *next;
1794
1795 next = path->nodes[*level];
1796
1797 btrfs_tree_lock(next);
1798 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001799 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001800 btrfs_wait_tree_block_writeback(next);
1801 btrfs_tree_unlock(next);
1802
Chris Masone02119d2008-09-05 16:13:11 -04001803 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001804 ret = btrfs_free_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04001805 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04001806 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04001807 BUG_ON(ret);
1808 }
1809 free_extent_buffer(path->nodes[*level]);
1810 path->nodes[*level] = NULL;
1811 *level = i + 1;
1812 }
1813 }
1814 return 1;
1815}
1816
1817/*
1818 * drop the reference count on the tree rooted at 'snap'. This traverses
1819 * the tree freeing any blocks that have a ref count of zero after being
1820 * decremented.
1821 */
1822static int walk_log_tree(struct btrfs_trans_handle *trans,
1823 struct btrfs_root *log, struct walk_control *wc)
1824{
1825 int ret = 0;
1826 int wret;
1827 int level;
1828 struct btrfs_path *path;
1829 int i;
1830 int orig_level;
1831
1832 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001833 if (!path)
1834 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001835
1836 level = btrfs_header_level(log->node);
1837 orig_level = level;
1838 path->nodes[level] = log->node;
1839 extent_buffer_get(log->node);
1840 path->slots[level] = 0;
1841
Chris Masond3977122009-01-05 21:25:51 -05001842 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001843 wret = walk_down_log_tree(trans, log, path, &level, wc);
1844 if (wret > 0)
1845 break;
1846 if (wret < 0)
1847 ret = wret;
1848
1849 wret = walk_up_log_tree(trans, log, path, &level, wc);
1850 if (wret > 0)
1851 break;
1852 if (wret < 0)
1853 ret = wret;
1854 }
1855
1856 /* was the root node processed? if not, catch it here */
1857 if (path->nodes[orig_level]) {
1858 wc->process_func(log, path->nodes[orig_level], wc,
1859 btrfs_header_generation(path->nodes[orig_level]));
1860 if (wc->free) {
1861 struct extent_buffer *next;
1862
1863 next = path->nodes[orig_level];
1864
1865 btrfs_tree_lock(next);
1866 clean_tree_block(trans, log, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001867 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001868 btrfs_wait_tree_block_writeback(next);
1869 btrfs_tree_unlock(next);
1870
Chris Masone02119d2008-09-05 16:13:11 -04001871 WARN_ON(log->root_key.objectid !=
1872 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001873 ret = btrfs_free_reserved_extent(log, next->start,
1874 next->len);
Chris Masone02119d2008-09-05 16:13:11 -04001875 BUG_ON(ret);
1876 }
1877 }
1878
1879 for (i = 0; i <= orig_level; i++) {
1880 if (path->nodes[i]) {
1881 free_extent_buffer(path->nodes[i]);
1882 path->nodes[i] = NULL;
1883 }
1884 }
1885 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001886 return ret;
1887}
1888
Yan Zheng7237f182009-01-21 12:54:03 -05001889/*
1890 * helper function to update the item for a given subvolumes log root
1891 * in the tree of log roots
1892 */
1893static int update_log_root(struct btrfs_trans_handle *trans,
1894 struct btrfs_root *log)
1895{
1896 int ret;
1897
1898 if (log->log_transid == 1) {
1899 /* insert root item on the first sync */
1900 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1901 &log->root_key, &log->root_item);
1902 } else {
1903 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1904 &log->root_key, &log->root_item);
1905 }
1906 return ret;
1907}
1908
Chris Mason12fcfd22009-03-24 10:24:20 -04001909static int wait_log_commit(struct btrfs_trans_handle *trans,
1910 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04001911{
1912 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05001913 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04001914
Yan Zheng7237f182009-01-21 12:54:03 -05001915 /*
1916 * we only allow two pending log transactions at a time,
1917 * so we know that if ours is more than 2 older than the
1918 * current transaction, we're done
1919 */
Chris Masone02119d2008-09-05 16:13:11 -04001920 do {
Yan Zheng7237f182009-01-21 12:54:03 -05001921 prepare_to_wait(&root->log_commit_wait[index],
1922 &wait, TASK_UNINTERRUPTIBLE);
1923 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001924
1925 if (root->fs_info->last_trans_log_full_commit !=
1926 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001927 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04001928 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04001929
Yan Zheng7237f182009-01-21 12:54:03 -05001930 finish_wait(&root->log_commit_wait[index], &wait);
1931 mutex_lock(&root->log_mutex);
1932 } while (root->log_transid < transid + 2 &&
1933 atomic_read(&root->log_commit[index]));
1934 return 0;
1935}
1936
Chris Mason12fcfd22009-03-24 10:24:20 -04001937static int wait_for_writer(struct btrfs_trans_handle *trans,
1938 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05001939{
1940 DEFINE_WAIT(wait);
1941 while (atomic_read(&root->log_writers)) {
1942 prepare_to_wait(&root->log_writer_wait,
1943 &wait, TASK_UNINTERRUPTIBLE);
1944 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001945 if (root->fs_info->last_trans_log_full_commit !=
1946 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05001947 schedule();
1948 mutex_lock(&root->log_mutex);
1949 finish_wait(&root->log_writer_wait, &wait);
1950 }
Chris Masone02119d2008-09-05 16:13:11 -04001951 return 0;
1952}
1953
1954/*
1955 * btrfs_sync_log does sends a given tree log down to the disk and
1956 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04001957 * you know that any inodes previously logged are safely on disk only
1958 * if it returns 0.
1959 *
1960 * Any other return value means you need to call btrfs_commit_transaction.
1961 * Some of the edge cases for fsyncing directories that have had unlinks
1962 * or renames done in the past mean that sometimes the only safe
1963 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
1964 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04001965 */
1966int btrfs_sync_log(struct btrfs_trans_handle *trans,
1967 struct btrfs_root *root)
1968{
Yan Zheng7237f182009-01-21 12:54:03 -05001969 int index1;
1970 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001971 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04001972 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04001973 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05001974 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001975 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001976
Yan Zheng7237f182009-01-21 12:54:03 -05001977 mutex_lock(&root->log_mutex);
1978 index1 = root->log_transid % 2;
1979 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001980 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05001981 mutex_unlock(&root->log_mutex);
1982 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04001983 }
Yan Zheng7237f182009-01-21 12:54:03 -05001984 atomic_set(&root->log_commit[index1], 1);
1985
1986 /* wait for previous tree log sync to complete */
1987 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04001988 wait_log_commit(trans, root, root->log_transid - 1);
Chris Masone02119d2008-09-05 16:13:11 -04001989
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001990 while (1) {
Yan Zheng7237f182009-01-21 12:54:03 -05001991 unsigned long batch = root->log_batch;
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001992 if (root->log_multiple_pids) {
1993 mutex_unlock(&root->log_mutex);
1994 schedule_timeout_uninterruptible(1);
1995 mutex_lock(&root->log_mutex);
1996 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001997 wait_for_writer(trans, root);
Yan Zheng7237f182009-01-21 12:54:03 -05001998 if (batch == root->log_batch)
Chris Masone02119d2008-09-05 16:13:11 -04001999 break;
2000 }
Chris Masond0c803c2008-09-11 16:17:57 -04002001
Chris Mason12fcfd22009-03-24 10:24:20 -04002002 /* bail out if we need to do a full commit */
2003 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2004 ret = -EAGAIN;
2005 mutex_unlock(&root->log_mutex);
2006 goto out;
2007 }
2008
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002009 log_transid = root->log_transid;
2010 if (log_transid % 2 == 0)
2011 mark = EXTENT_DIRTY;
2012 else
2013 mark = EXTENT_NEW;
2014
Chris Mason690587d2009-10-13 13:29:19 -04002015 /* we start IO on all the marked extents here, but we don't actually
2016 * wait for them until later.
2017 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002018 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002019 BUG_ON(ret);
Yan Zheng7237f182009-01-21 12:54:03 -05002020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002022
2023 root->log_batch = 0;
2024 root->log_transid++;
2025 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002026 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002027 smp_mb();
2028 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002029 * IO has been started, blocks of the log tree have WRITTEN flag set
2030 * in their headers. new modifications of the log will be written to
2031 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002032 */
2033 mutex_unlock(&root->log_mutex);
2034
2035 mutex_lock(&log_root_tree->log_mutex);
2036 log_root_tree->log_batch++;
2037 atomic_inc(&log_root_tree->log_writers);
2038 mutex_unlock(&log_root_tree->log_mutex);
2039
2040 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002041
2042 mutex_lock(&log_root_tree->log_mutex);
2043 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2044 smp_mb();
2045 if (waitqueue_active(&log_root_tree->log_writer_wait))
2046 wake_up(&log_root_tree->log_writer_wait);
2047 }
2048
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002049 if (ret) {
2050 BUG_ON(ret != -ENOSPC);
2051 root->fs_info->last_trans_log_full_commit = trans->transid;
2052 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2053 mutex_unlock(&log_root_tree->log_mutex);
2054 ret = -EAGAIN;
2055 goto out;
2056 }
2057
Yan Zheng7237f182009-01-21 12:54:03 -05002058 index2 = log_root_tree->log_transid % 2;
2059 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002060 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002061 wait_log_commit(trans, log_root_tree,
2062 log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002063 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002064 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002065 goto out;
2066 }
2067 atomic_set(&log_root_tree->log_commit[index2], 1);
2068
Chris Mason12fcfd22009-03-24 10:24:20 -04002069 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2070 wait_log_commit(trans, log_root_tree,
2071 log_root_tree->log_transid - 1);
2072 }
Yan Zheng7237f182009-01-21 12:54:03 -05002073
Chris Mason12fcfd22009-03-24 10:24:20 -04002074 wait_for_writer(trans, log_root_tree);
2075
2076 /*
2077 * now that we've moved on to the tree of log tree roots,
2078 * check the full commit flag again
2079 */
2080 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002081 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002082 mutex_unlock(&log_root_tree->log_mutex);
2083 ret = -EAGAIN;
2084 goto out_wake_log_root;
2085 }
Yan Zheng7237f182009-01-21 12:54:03 -05002086
2087 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002088 &log_root_tree->dirty_log_pages,
2089 EXTENT_DIRTY | EXTENT_NEW);
Chris Masone02119d2008-09-05 16:13:11 -04002090 BUG_ON(ret);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002091 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002092
2093 btrfs_set_super_log_root(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002094 log_root_tree->node->start);
Chris Masone02119d2008-09-05 16:13:11 -04002095 btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002096 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002097
Yan Zheng7237f182009-01-21 12:54:03 -05002098 log_root_tree->log_batch = 0;
2099 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002100 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002101
2102 mutex_unlock(&log_root_tree->log_mutex);
2103
2104 /*
2105 * nobody else is going to jump in and write the the ctree
2106 * super here because the log_commit atomic below is protecting
2107 * us. We must be called with a transaction handle pinning
2108 * the running transaction open, so a full commit can't hop
2109 * in and cause problems either.
2110 */
Chris Mason47226072009-10-13 12:55:09 -04002111 write_ctree_super(trans, root->fs_info->tree_root, 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002112 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002113
Chris Mason257c62e2009-10-13 13:21:08 -04002114 mutex_lock(&root->log_mutex);
2115 if (root->last_log_commit < log_transid)
2116 root->last_log_commit = log_transid;
2117 mutex_unlock(&root->log_mutex);
2118
Chris Mason12fcfd22009-03-24 10:24:20 -04002119out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002120 atomic_set(&log_root_tree->log_commit[index2], 0);
2121 smp_mb();
2122 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2123 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002124out:
Yan Zheng7237f182009-01-21 12:54:03 -05002125 atomic_set(&root->log_commit[index1], 0);
2126 smp_mb();
2127 if (waitqueue_active(&root->log_commit_wait[index1]))
2128 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002129 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002130}
2131
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002132static void free_log_tree(struct btrfs_trans_handle *trans,
2133 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002134{
2135 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002136 u64 start;
2137 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002138 struct walk_control wc = {
2139 .free = 1,
2140 .process_func = process_one_buffer
2141 };
2142
Chris Masone02119d2008-09-05 16:13:11 -04002143 ret = walk_log_tree(trans, log, &wc);
2144 BUG_ON(ret);
2145
Chris Masond3977122009-01-05 21:25:51 -05002146 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002147 ret = find_first_extent_bit(&log->dirty_log_pages,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002148 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04002149 if (ret)
2150 break;
2151
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002152 clear_extent_bits(&log->dirty_log_pages, start, end,
2153 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002154 }
2155
Yan Zheng7237f182009-01-21 12:54:03 -05002156 free_extent_buffer(log->node);
2157 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002158}
2159
2160/*
2161 * free all the extents used by the tree log. This should be called
2162 * at commit time of the full transaction
2163 */
2164int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2165{
2166 if (root->log_root) {
2167 free_log_tree(trans, root->log_root);
2168 root->log_root = NULL;
2169 }
2170 return 0;
2171}
2172
2173int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2174 struct btrfs_fs_info *fs_info)
2175{
2176 if (fs_info->log_root_tree) {
2177 free_log_tree(trans, fs_info->log_root_tree);
2178 fs_info->log_root_tree = NULL;
2179 }
Chris Masone02119d2008-09-05 16:13:11 -04002180 return 0;
2181}
2182
2183/*
Chris Masone02119d2008-09-05 16:13:11 -04002184 * If both a file and directory are logged, and unlinks or renames are
2185 * mixed in, we have a few interesting corners:
2186 *
2187 * create file X in dir Y
2188 * link file X to X.link in dir Y
2189 * fsync file X
2190 * unlink file X but leave X.link
2191 * fsync dir Y
2192 *
2193 * After a crash we would expect only X.link to exist. But file X
2194 * didn't get fsync'd again so the log has back refs for X and X.link.
2195 *
2196 * We solve this by removing directory entries and inode backrefs from the
2197 * log when a file that was logged in the current transaction is
2198 * unlinked. Any later fsync will include the updated log entries, and
2199 * we'll be able to reconstruct the proper directory items from backrefs.
2200 *
2201 * This optimizations allows us to avoid relogging the entire inode
2202 * or the entire directory.
2203 */
2204int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2205 struct btrfs_root *root,
2206 const char *name, int name_len,
2207 struct inode *dir, u64 index)
2208{
2209 struct btrfs_root *log;
2210 struct btrfs_dir_item *di;
2211 struct btrfs_path *path;
2212 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002213 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002214 int bytes_del = 0;
2215
Chris Mason3a5f1d42008-09-11 15:53:37 -04002216 if (BTRFS_I(dir)->logged_trans < trans->transid)
2217 return 0;
2218
Chris Masone02119d2008-09-05 16:13:11 -04002219 ret = join_running_log_trans(root);
2220 if (ret)
2221 return 0;
2222
2223 mutex_lock(&BTRFS_I(dir)->log_mutex);
2224
2225 log = root->log_root;
2226 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002227 if (!path) {
2228 err = -ENOMEM;
2229 goto out_unlock;
2230 }
liubo2a29edc2011-01-26 06:22:08 +00002231
Chris Masone02119d2008-09-05 16:13:11 -04002232 di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
2233 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 btrfs_release_path(log, path);
2244 di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
2245 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002246 if (IS_ERR(di)) {
2247 err = PTR_ERR(di);
2248 goto fail;
2249 }
2250 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002251 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2252 bytes_del += name_len;
2253 BUG_ON(ret);
2254 }
2255
2256 /* update the directory size in the log to reflect the names
2257 * we have removed
2258 */
2259 if (bytes_del) {
2260 struct btrfs_key key;
2261
2262 key.objectid = dir->i_ino;
2263 key.offset = 0;
2264 key.type = BTRFS_INODE_ITEM_KEY;
2265 btrfs_release_path(log, path);
2266
2267 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002268 if (ret < 0) {
2269 err = ret;
2270 goto fail;
2271 }
Chris Masone02119d2008-09-05 16:13:11 -04002272 if (ret == 0) {
2273 struct btrfs_inode_item *item;
2274 u64 i_size;
2275
2276 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2277 struct btrfs_inode_item);
2278 i_size = btrfs_inode_size(path->nodes[0], item);
2279 if (i_size > bytes_del)
2280 i_size -= bytes_del;
2281 else
2282 i_size = 0;
2283 btrfs_set_inode_size(path->nodes[0], item, i_size);
2284 btrfs_mark_buffer_dirty(path->nodes[0]);
2285 } else
2286 ret = 0;
2287 btrfs_release_path(log, path);
2288 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002289fail:
Chris Masone02119d2008-09-05 16:13:11 -04002290 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002291out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002292 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002293 if (ret == -ENOSPC) {
2294 root->fs_info->last_trans_log_full_commit = trans->transid;
2295 ret = 0;
2296 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002297 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002298
Andi Kleen411fc6b2010-10-29 15:14:31 -04002299 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002300}
2301
2302/* see comments for btrfs_del_dir_entries_in_log */
2303int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2304 struct btrfs_root *root,
2305 const char *name, int name_len,
2306 struct inode *inode, u64 dirid)
2307{
2308 struct btrfs_root *log;
2309 u64 index;
2310 int ret;
2311
Chris Mason3a5f1d42008-09-11 15:53:37 -04002312 if (BTRFS_I(inode)->logged_trans < trans->transid)
2313 return 0;
2314
Chris Masone02119d2008-09-05 16:13:11 -04002315 ret = join_running_log_trans(root);
2316 if (ret)
2317 return 0;
2318 log = root->log_root;
2319 mutex_lock(&BTRFS_I(inode)->log_mutex);
2320
2321 ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
2322 dirid, &index);
2323 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002324 if (ret == -ENOSPC) {
2325 root->fs_info->last_trans_log_full_commit = trans->transid;
2326 ret = 0;
2327 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002328 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002329
Chris Masone02119d2008-09-05 16:13:11 -04002330 return ret;
2331}
2332
2333/*
2334 * creates a range item in the log for 'dirid'. first_offset and
2335 * last_offset tell us which parts of the key space the log should
2336 * be considered authoritative for.
2337 */
2338static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2339 struct btrfs_root *log,
2340 struct btrfs_path *path,
2341 int key_type, u64 dirid,
2342 u64 first_offset, u64 last_offset)
2343{
2344 int ret;
2345 struct btrfs_key key;
2346 struct btrfs_dir_log_item *item;
2347
2348 key.objectid = dirid;
2349 key.offset = first_offset;
2350 if (key_type == BTRFS_DIR_ITEM_KEY)
2351 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2352 else
2353 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2354 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002355 if (ret)
2356 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002357
2358 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2359 struct btrfs_dir_log_item);
2360 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2361 btrfs_mark_buffer_dirty(path->nodes[0]);
2362 btrfs_release_path(log, path);
2363 return 0;
2364}
2365
2366/*
2367 * log all the items included in the current transaction for a given
2368 * directory. This also creates the range items in the log tree required
2369 * to replay anything deleted before the fsync
2370 */
2371static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2372 struct btrfs_root *root, struct inode *inode,
2373 struct btrfs_path *path,
2374 struct btrfs_path *dst_path, int key_type,
2375 u64 min_offset, u64 *last_offset_ret)
2376{
2377 struct btrfs_key min_key;
2378 struct btrfs_key max_key;
2379 struct btrfs_root *log = root->log_root;
2380 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002381 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002382 int ret;
2383 int i;
2384 int nritems;
2385 u64 first_offset = min_offset;
2386 u64 last_offset = (u64)-1;
2387
2388 log = root->log_root;
2389 max_key.objectid = inode->i_ino;
2390 max_key.offset = (u64)-1;
2391 max_key.type = key_type;
2392
2393 min_key.objectid = inode->i_ino;
2394 min_key.type = key_type;
2395 min_key.offset = min_offset;
2396
2397 path->keep_locks = 1;
2398
2399 ret = btrfs_search_forward(root, &min_key, &max_key,
2400 path, 0, trans->transid);
2401
2402 /*
2403 * we didn't find anything from this transaction, see if there
2404 * is anything at all
2405 */
2406 if (ret != 0 || min_key.objectid != inode->i_ino ||
2407 min_key.type != key_type) {
2408 min_key.objectid = inode->i_ino;
2409 min_key.type = key_type;
2410 min_key.offset = (u64)-1;
2411 btrfs_release_path(root, path);
2412 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2413 if (ret < 0) {
2414 btrfs_release_path(root, path);
2415 return ret;
2416 }
2417 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2418
2419 /* if ret == 0 there are items for this type,
2420 * create a range to tell us the last key of this type.
2421 * otherwise, there are no items in this directory after
2422 * *min_offset, and we create a range to indicate that.
2423 */
2424 if (ret == 0) {
2425 struct btrfs_key tmp;
2426 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2427 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002428 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002429 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002430 }
2431 goto done;
2432 }
2433
2434 /* go backward to find any previous key */
2435 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2436 if (ret == 0) {
2437 struct btrfs_key tmp;
2438 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2439 if (key_type == tmp.type) {
2440 first_offset = tmp.offset;
2441 ret = overwrite_item(trans, log, dst_path,
2442 path->nodes[0], path->slots[0],
2443 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002444 if (ret) {
2445 err = ret;
2446 goto done;
2447 }
Chris Masone02119d2008-09-05 16:13:11 -04002448 }
2449 }
2450 btrfs_release_path(root, path);
2451
2452 /* find the first key from this transaction again */
2453 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2454 if (ret != 0) {
2455 WARN_ON(1);
2456 goto done;
2457 }
2458
2459 /*
2460 * we have a block from this transaction, log every item in it
2461 * from our directory
2462 */
Chris Masond3977122009-01-05 21:25:51 -05002463 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002464 struct btrfs_key tmp;
2465 src = path->nodes[0];
2466 nritems = btrfs_header_nritems(src);
2467 for (i = path->slots[0]; i < nritems; i++) {
2468 btrfs_item_key_to_cpu(src, &min_key, i);
2469
2470 if (min_key.objectid != inode->i_ino ||
2471 min_key.type != key_type)
2472 goto done;
2473 ret = overwrite_item(trans, log, dst_path, src, i,
2474 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002475 if (ret) {
2476 err = ret;
2477 goto done;
2478 }
Chris Masone02119d2008-09-05 16:13:11 -04002479 }
2480 path->slots[0] = nritems;
2481
2482 /*
2483 * look ahead to the next item and see if it is also
2484 * from this directory and from this transaction
2485 */
2486 ret = btrfs_next_leaf(root, path);
2487 if (ret == 1) {
2488 last_offset = (u64)-1;
2489 goto done;
2490 }
2491 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2492 if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
2493 last_offset = (u64)-1;
2494 goto done;
2495 }
2496 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2497 ret = overwrite_item(trans, log, dst_path,
2498 path->nodes[0], path->slots[0],
2499 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002500 if (ret)
2501 err = ret;
2502 else
2503 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002504 goto done;
2505 }
2506 }
2507done:
Chris Masone02119d2008-09-05 16:13:11 -04002508 btrfs_release_path(root, path);
2509 btrfs_release_path(log, dst_path);
2510
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002511 if (err == 0) {
2512 *last_offset_ret = last_offset;
2513 /*
2514 * insert the log range keys to indicate where the log
2515 * is valid
2516 */
2517 ret = insert_dir_log_key(trans, log, path, key_type,
2518 inode->i_ino, first_offset,
2519 last_offset);
2520 if (ret)
2521 err = ret;
2522 }
2523 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002524}
2525
2526/*
2527 * logging directories is very similar to logging inodes, We find all the items
2528 * from the current transaction and write them to the log.
2529 *
2530 * The recovery code scans the directory in the subvolume, and if it finds a
2531 * key in the range logged that is not present in the log tree, then it means
2532 * that dir entry was unlinked during the transaction.
2533 *
2534 * In order for that scan to work, we must include one key smaller than
2535 * the smallest logged by this transaction and one key larger than the largest
2536 * key logged by this transaction.
2537 */
2538static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2539 struct btrfs_root *root, struct inode *inode,
2540 struct btrfs_path *path,
2541 struct btrfs_path *dst_path)
2542{
2543 u64 min_key;
2544 u64 max_key;
2545 int ret;
2546 int key_type = BTRFS_DIR_ITEM_KEY;
2547
2548again:
2549 min_key = 0;
2550 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002551 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002552 ret = log_dir_items(trans, root, inode, path,
2553 dst_path, key_type, min_key,
2554 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002555 if (ret)
2556 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002557 if (max_key == (u64)-1)
2558 break;
2559 min_key = max_key + 1;
2560 }
2561
2562 if (key_type == BTRFS_DIR_ITEM_KEY) {
2563 key_type = BTRFS_DIR_INDEX_KEY;
2564 goto again;
2565 }
2566 return 0;
2567}
2568
2569/*
2570 * a helper function to drop items from the log before we relog an
2571 * inode. max_key_type indicates the highest item type to remove.
2572 * This cannot be run for file data extents because it does not
2573 * free the extents they point to.
2574 */
2575static int drop_objectid_items(struct btrfs_trans_handle *trans,
2576 struct btrfs_root *log,
2577 struct btrfs_path *path,
2578 u64 objectid, int max_key_type)
2579{
2580 int ret;
2581 struct btrfs_key key;
2582 struct btrfs_key found_key;
2583
2584 key.objectid = objectid;
2585 key.type = max_key_type;
2586 key.offset = (u64)-1;
2587
Chris Masond3977122009-01-05 21:25:51 -05002588 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002589 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002590 BUG_ON(ret == 0);
2591 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002592 break;
2593
2594 if (path->slots[0] == 0)
2595 break;
2596
2597 path->slots[0]--;
2598 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2599 path->slots[0]);
2600
2601 if (found_key.objectid != objectid)
2602 break;
2603
2604 ret = btrfs_del_item(trans, log, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002605 if (ret)
2606 break;
Chris Masone02119d2008-09-05 16:13:11 -04002607 btrfs_release_path(log, path);
2608 }
2609 btrfs_release_path(log, path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002610 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002611}
2612
Chris Mason31ff1cd2008-09-11 16:17:57 -04002613static noinline int copy_items(struct btrfs_trans_handle *trans,
2614 struct btrfs_root *log,
2615 struct btrfs_path *dst_path,
2616 struct extent_buffer *src,
2617 int start_slot, int nr, int inode_only)
2618{
2619 unsigned long src_offset;
2620 unsigned long dst_offset;
2621 struct btrfs_file_extent_item *extent;
2622 struct btrfs_inode_item *inode_item;
2623 int ret;
2624 struct btrfs_key *ins_keys;
2625 u32 *ins_sizes;
2626 char *ins_data;
2627 int i;
Chris Masond20f7042008-12-08 16:58:54 -05002628 struct list_head ordered_sums;
2629
2630 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002631
2632 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2633 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00002634 if (!ins_data)
2635 return -ENOMEM;
2636
Chris Mason31ff1cd2008-09-11 16:17:57 -04002637 ins_sizes = (u32 *)ins_data;
2638 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2639
2640 for (i = 0; i < nr; i++) {
2641 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2642 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2643 }
2644 ret = btrfs_insert_empty_items(trans, log, dst_path,
2645 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002646 if (ret) {
2647 kfree(ins_data);
2648 return ret;
2649 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002650
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002651 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002652 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2653 dst_path->slots[0]);
2654
2655 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2656
2657 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2658 src_offset, ins_sizes[i]);
2659
2660 if (inode_only == LOG_INODE_EXISTS &&
2661 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2662 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2663 dst_path->slots[0],
2664 struct btrfs_inode_item);
2665 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2666
2667 /* set the generation to zero so the recover code
2668 * can tell the difference between an logging
2669 * just to say 'this inode exists' and a logging
2670 * to say 'update this inode with these values'
2671 */
2672 btrfs_set_inode_generation(dst_path->nodes[0],
2673 inode_item, 0);
2674 }
2675 /* take a reference on file data extents so that truncates
2676 * or deletes of this inode don't have to relog the inode
2677 * again
2678 */
2679 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2680 int found_type;
2681 extent = btrfs_item_ptr(src, start_slot + i,
2682 struct btrfs_file_extent_item);
2683
2684 found_type = btrfs_file_extent_type(src, extent);
Yan Zhengd899e052008-10-30 14:25:28 -04002685 if (found_type == BTRFS_FILE_EXTENT_REG ||
2686 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002687 u64 ds, dl, cs, cl;
2688 ds = btrfs_file_extent_disk_bytenr(src,
2689 extent);
2690 /* ds == 0 is a hole */
2691 if (ds == 0)
2692 continue;
2693
2694 dl = btrfs_file_extent_disk_num_bytes(src,
2695 extent);
2696 cs = btrfs_file_extent_offset(src, extent);
2697 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07002698 extent);
Chris Mason580afd72008-12-08 19:15:39 -05002699 if (btrfs_file_extent_compression(src,
2700 extent)) {
2701 cs = 0;
2702 cl = dl;
2703 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002704
2705 ret = btrfs_lookup_csums_range(
2706 log->fs_info->csum_root,
2707 ds + cs, ds + cs + cl - 1,
2708 &ordered_sums);
2709 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002710 }
2711 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002712 }
2713
2714 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2715 btrfs_release_path(log, dst_path);
2716 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05002717
2718 /*
2719 * we have to do this after the loop above to avoid changing the
2720 * log tree while trying to change the log tree.
2721 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002722 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002723 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05002724 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2725 struct btrfs_ordered_sum,
2726 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002727 if (!ret)
2728 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05002729 list_del(&sums->list);
2730 kfree(sums);
2731 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002732 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002733}
2734
Chris Masone02119d2008-09-05 16:13:11 -04002735/* log a single inode in the tree log.
2736 * At least one parent directory for this inode must exist in the tree
2737 * or be logged already.
2738 *
2739 * Any items from this inode changed by the current transaction are copied
2740 * to the log tree. An extra reference is taken on any extents in this
2741 * file, allowing us to avoid a whole pile of corner cases around logging
2742 * blocks that have been removed from the tree.
2743 *
2744 * See LOG_INODE_ALL and related defines for a description of what inode_only
2745 * does.
2746 *
2747 * This handles both files and directories.
2748 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002749static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002750 struct btrfs_root *root, struct inode *inode,
2751 int inode_only)
2752{
2753 struct btrfs_path *path;
2754 struct btrfs_path *dst_path;
2755 struct btrfs_key min_key;
2756 struct btrfs_key max_key;
2757 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002758 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002759 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002760 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002761 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002762 int ins_start_slot = 0;
2763 int ins_nr;
Chris Masone02119d2008-09-05 16:13:11 -04002764
2765 log = root->log_root;
2766
2767 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002768 if (!path)
2769 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002770 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002771 if (!dst_path) {
2772 btrfs_free_path(path);
2773 return -ENOMEM;
2774 }
Chris Masone02119d2008-09-05 16:13:11 -04002775
2776 min_key.objectid = inode->i_ino;
2777 min_key.type = BTRFS_INODE_ITEM_KEY;
2778 min_key.offset = 0;
2779
2780 max_key.objectid = inode->i_ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04002781
2782 /* today the code can only do partial logging of directories */
2783 if (!S_ISDIR(inode->i_mode))
2784 inode_only = LOG_INODE_ALL;
2785
Chris Masone02119d2008-09-05 16:13:11 -04002786 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2787 max_key.type = BTRFS_XATTR_ITEM_KEY;
2788 else
2789 max_key.type = (u8)-1;
2790 max_key.offset = (u64)-1;
2791
Chris Masone02119d2008-09-05 16:13:11 -04002792 mutex_lock(&BTRFS_I(inode)->log_mutex);
2793
2794 /*
2795 * a brute force approach to making sure we get the most uptodate
2796 * copies of everything.
2797 */
2798 if (S_ISDIR(inode->i_mode)) {
2799 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2800
2801 if (inode_only == LOG_INODE_EXISTS)
2802 max_key_type = BTRFS_XATTR_ITEM_KEY;
2803 ret = drop_objectid_items(trans, log, path,
2804 inode->i_ino, max_key_type);
2805 } else {
2806 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2807 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002808 if (ret) {
2809 err = ret;
2810 goto out_unlock;
2811 }
Chris Masone02119d2008-09-05 16:13:11 -04002812 path->keep_locks = 1;
2813
Chris Masond3977122009-01-05 21:25:51 -05002814 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002815 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002816 ret = btrfs_search_forward(root, &min_key, &max_key,
2817 path, 0, trans->transid);
2818 if (ret != 0)
2819 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002820again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04002821 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Chris Masone02119d2008-09-05 16:13:11 -04002822 if (min_key.objectid != inode->i_ino)
2823 break;
2824 if (min_key.type > max_key.type)
2825 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002826
Chris Masone02119d2008-09-05 16:13:11 -04002827 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04002828 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2829 ins_nr++;
2830 goto next_slot;
2831 } else if (!ins_nr) {
2832 ins_start_slot = path->slots[0];
2833 ins_nr = 1;
2834 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002835 }
2836
Chris Mason31ff1cd2008-09-11 16:17:57 -04002837 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2838 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002839 if (ret) {
2840 err = ret;
2841 goto out_unlock;
2842 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002843 ins_nr = 1;
2844 ins_start_slot = path->slots[0];
2845next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04002846
Chris Mason3a5f1d42008-09-11 15:53:37 -04002847 nritems = btrfs_header_nritems(path->nodes[0]);
2848 path->slots[0]++;
2849 if (path->slots[0] < nritems) {
2850 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2851 path->slots[0]);
2852 goto again;
2853 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002854 if (ins_nr) {
2855 ret = copy_items(trans, log, dst_path, src,
2856 ins_start_slot,
2857 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002858 if (ret) {
2859 err = ret;
2860 goto out_unlock;
2861 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002862 ins_nr = 0;
2863 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002864 btrfs_release_path(root, path);
2865
Chris Masone02119d2008-09-05 16:13:11 -04002866 if (min_key.offset < (u64)-1)
2867 min_key.offset++;
2868 else if (min_key.type < (u8)-1)
2869 min_key.type++;
2870 else if (min_key.objectid < (u64)-1)
2871 min_key.objectid++;
2872 else
2873 break;
2874 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002875 if (ins_nr) {
2876 ret = copy_items(trans, log, dst_path, src,
2877 ins_start_slot,
2878 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002879 if (ret) {
2880 err = ret;
2881 goto out_unlock;
2882 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002883 ins_nr = 0;
2884 }
2885 WARN_ON(ins_nr);
Chris Mason9623f9a2008-09-11 17:42:42 -04002886 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04002887 btrfs_release_path(root, path);
2888 btrfs_release_path(log, dst_path);
2889 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002890 if (ret) {
2891 err = ret;
2892 goto out_unlock;
2893 }
Chris Masone02119d2008-09-05 16:13:11 -04002894 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002895 BTRFS_I(inode)->logged_trans = trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002896out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002897 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2898
2899 btrfs_free_path(path);
2900 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002901 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002902}
2903
Chris Mason12fcfd22009-03-24 10:24:20 -04002904/*
2905 * follow the dentry parent pointers up the chain and see if any
2906 * of the directories in it require a full commit before they can
2907 * be logged. Returns zero if nothing special needs to be done or 1 if
2908 * a full commit is required.
2909 */
2910static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
2911 struct inode *inode,
2912 struct dentry *parent,
2913 struct super_block *sb,
2914 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04002915{
Chris Mason12fcfd22009-03-24 10:24:20 -04002916 int ret = 0;
2917 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00002918 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002919
Chris Masonaf4176b2009-03-24 10:24:31 -04002920 /*
2921 * for regular files, if its inode is already on disk, we don't
2922 * have to worry about the parents at all. This is because
2923 * we can use the last_unlink_trans field to record renames
2924 * and other fun in this file.
2925 */
2926 if (S_ISREG(inode->i_mode) &&
2927 BTRFS_I(inode)->generation <= last_committed &&
2928 BTRFS_I(inode)->last_unlink_trans <= last_committed)
2929 goto out;
2930
Chris Mason12fcfd22009-03-24 10:24:20 -04002931 if (!S_ISDIR(inode->i_mode)) {
2932 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2933 goto out;
2934 inode = parent->d_inode;
2935 }
2936
2937 while (1) {
2938 BTRFS_I(inode)->logged_trans = trans->transid;
2939 smp_mb();
2940
2941 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
2942 root = BTRFS_I(inode)->root;
2943
2944 /*
2945 * make sure any commits to the log are forced
2946 * to be full commits
2947 */
2948 root->fs_info->last_trans_log_full_commit =
2949 trans->transid;
2950 ret = 1;
2951 break;
2952 }
2953
2954 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2955 break;
2956
Yan, Zheng76dda932009-09-21 16:00:26 -04002957 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04002958 break;
2959
Josef Bacik6a912212010-11-20 09:48:00 +00002960 parent = dget_parent(parent);
2961 dput(old_parent);
2962 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04002963 inode = parent->d_inode;
2964
2965 }
Josef Bacik6a912212010-11-20 09:48:00 +00002966 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04002967out:
Chris Masone02119d2008-09-05 16:13:11 -04002968 return ret;
2969}
2970
Chris Mason257c62e2009-10-13 13:21:08 -04002971static int inode_in_log(struct btrfs_trans_handle *trans,
2972 struct inode *inode)
2973{
2974 struct btrfs_root *root = BTRFS_I(inode)->root;
2975 int ret = 0;
2976
2977 mutex_lock(&root->log_mutex);
2978 if (BTRFS_I(inode)->logged_trans == trans->transid &&
2979 BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
2980 ret = 1;
2981 mutex_unlock(&root->log_mutex);
2982 return ret;
2983}
2984
2985
Chris Masone02119d2008-09-05 16:13:11 -04002986/*
2987 * helper function around btrfs_log_inode to make sure newly created
2988 * parent directories also end up in the log. A minimal inode and backref
2989 * only logging is done of any parent directories that are older than
2990 * the last committed transaction
2991 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002992int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
2993 struct btrfs_root *root, struct inode *inode,
2994 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04002995{
Chris Mason12fcfd22009-03-24 10:24:20 -04002996 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04002997 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00002998 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002999 int ret = 0;
3000 u64 last_committed = root->fs_info->last_trans_committed;
3001
3002 sb = inode->i_sb;
3003
Sage Weil3a5e1402009-04-02 16:49:40 -04003004 if (btrfs_test_opt(root, NOTREELOG)) {
3005 ret = 1;
3006 goto end_no_trans;
3007 }
3008
Chris Mason12fcfd22009-03-24 10:24:20 -04003009 if (root->fs_info->last_trans_log_full_commit >
3010 root->fs_info->last_trans_committed) {
3011 ret = 1;
3012 goto end_no_trans;
3013 }
3014
Yan, Zheng76dda932009-09-21 16:00:26 -04003015 if (root != BTRFS_I(inode)->root ||
3016 btrfs_root_refs(&root->root_item) == 0) {
3017 ret = 1;
3018 goto end_no_trans;
3019 }
3020
Chris Mason12fcfd22009-03-24 10:24:20 -04003021 ret = check_parent_dirs_for_sync(trans, inode, parent,
3022 sb, last_committed);
3023 if (ret)
3024 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003025
Chris Mason257c62e2009-10-13 13:21:08 -04003026 if (inode_in_log(trans, inode)) {
3027 ret = BTRFS_NO_LOG_SYNC;
3028 goto end_no_trans;
3029 }
3030
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003031 ret = start_log_trans(trans, root);
3032 if (ret)
3033 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003034
3035 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003036 if (ret)
3037 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003038
Chris Masonaf4176b2009-03-24 10:24:31 -04003039 /*
3040 * for regular files, if its inode is already on disk, we don't
3041 * have to worry about the parents at all. This is because
3042 * we can use the last_unlink_trans field to record renames
3043 * and other fun in this file.
3044 */
3045 if (S_ISREG(inode->i_mode) &&
3046 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003047 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3048 ret = 0;
3049 goto end_trans;
3050 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003051
3052 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003053 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003054 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003055 break;
3056
Chris Mason12fcfd22009-03-24 10:24:20 -04003057 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003058 if (root != BTRFS_I(inode)->root)
3059 break;
3060
Chris Mason12fcfd22009-03-24 10:24:20 -04003061 if (BTRFS_I(inode)->generation >
3062 root->fs_info->last_trans_committed) {
3063 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003064 if (ret)
3065 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003066 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003067 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003068 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003069
Josef Bacik6a912212010-11-20 09:48:00 +00003070 parent = dget_parent(parent);
3071 dput(old_parent);
3072 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003073 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003074 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003075end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003076 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003077 if (ret < 0) {
3078 BUG_ON(ret != -ENOSPC);
3079 root->fs_info->last_trans_log_full_commit = trans->transid;
3080 ret = 1;
3081 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003082 btrfs_end_log_trans(root);
3083end_no_trans:
3084 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003085}
3086
3087/*
3088 * it is not safe to log dentry if the chunk root has added new
3089 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3090 * If this returns 1, you must commit the transaction to safely get your
3091 * data on disk.
3092 */
3093int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3094 struct btrfs_root *root, struct dentry *dentry)
3095{
Josef Bacik6a912212010-11-20 09:48:00 +00003096 struct dentry *parent = dget_parent(dentry);
3097 int ret;
3098
3099 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3100 dput(parent);
3101
3102 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003103}
3104
3105/*
3106 * should be called during mount to recover any replay any log trees
3107 * from the FS
3108 */
3109int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3110{
3111 int ret;
3112 struct btrfs_path *path;
3113 struct btrfs_trans_handle *trans;
3114 struct btrfs_key key;
3115 struct btrfs_key found_key;
3116 struct btrfs_key tmp_key;
3117 struct btrfs_root *log;
3118 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3119 struct walk_control wc = {
3120 .process_func = process_one_buffer,
3121 .stage = 0,
3122 };
3123
Chris Masone02119d2008-09-05 16:13:11 -04003124 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003125 if (!path)
3126 return -ENOMEM;
3127
3128 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003129
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003130 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003131 BUG_ON(IS_ERR(trans));
Chris Masone02119d2008-09-05 16:13:11 -04003132
3133 wc.trans = trans;
3134 wc.pin = 1;
3135
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003136 ret = walk_log_tree(trans, log_root_tree, &wc);
3137 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003138
3139again:
3140 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3141 key.offset = (u64)-1;
3142 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3143
Chris Masond3977122009-01-05 21:25:51 -05003144 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003145 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3146 if (ret < 0)
3147 break;
3148 if (ret > 0) {
3149 if (path->slots[0] == 0)
3150 break;
3151 path->slots[0]--;
3152 }
3153 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3154 path->slots[0]);
3155 btrfs_release_path(log_root_tree, path);
3156 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3157 break;
3158
3159 log = btrfs_read_fs_root_no_radix(log_root_tree,
3160 &found_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003161 BUG_ON(IS_ERR(log));
Chris Masone02119d2008-09-05 16:13:11 -04003162
3163 tmp_key.objectid = found_key.offset;
3164 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3165 tmp_key.offset = (u64)-1;
3166
3167 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Chris Masone02119d2008-09-05 16:13:11 -04003168 BUG_ON(!wc.replay_dest);
3169
Yan Zheng07d400a2009-01-06 11:42:00 -05003170 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003171 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003172 ret = walk_log_tree(trans, log, &wc);
3173 BUG_ON(ret);
3174
3175 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3176 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3177 path);
3178 BUG_ON(ret);
3179 }
Chris Masone02119d2008-09-05 16:13:11 -04003180
3181 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003182 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003183 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003184 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003185 kfree(log);
3186
3187 if (found_key.offset == 0)
3188 break;
3189 }
3190 btrfs_release_path(log_root_tree, path);
3191
3192 /* step one is to pin it all, step two is to replay just inodes */
3193 if (wc.pin) {
3194 wc.pin = 0;
3195 wc.process_func = replay_one_buffer;
3196 wc.stage = LOG_WALK_REPLAY_INODES;
3197 goto again;
3198 }
3199 /* step three is to replay everything */
3200 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3201 wc.stage++;
3202 goto again;
3203 }
3204
3205 btrfs_free_path(path);
3206
3207 free_extent_buffer(log_root_tree->node);
3208 log_root_tree->log_root = NULL;
3209 fs_info->log_root_recovering = 0;
3210
3211 /* step 4: commit the transaction, which also unpins the blocks */
3212 btrfs_commit_transaction(trans, fs_info->tree_root);
3213
3214 kfree(log_root_tree);
3215 return 0;
3216}
Chris Mason12fcfd22009-03-24 10:24:20 -04003217
3218/*
3219 * there are some corner cases where we want to force a full
3220 * commit instead of allowing a directory to be logged.
3221 *
3222 * They revolve around files there were unlinked from the directory, and
3223 * this function updates the parent directory so that a full commit is
3224 * properly done if it is fsync'd later after the unlinks are done.
3225 */
3226void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3227 struct inode *dir, struct inode *inode,
3228 int for_rename)
3229{
3230 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003231 * when we're logging a file, if it hasn't been renamed
3232 * or unlinked, and its inode is fully committed on disk,
3233 * we don't have to worry about walking up the directory chain
3234 * to log its parents.
3235 *
3236 * So, we use the last_unlink_trans field to put this transid
3237 * into the file. When the file is logged we check it and
3238 * don't log the parents if the file is fully on disk.
3239 */
3240 if (S_ISREG(inode->i_mode))
3241 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3242
3243 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003244 * if this directory was already logged any new
3245 * names for this file/dir will get recorded
3246 */
3247 smp_mb();
3248 if (BTRFS_I(dir)->logged_trans == trans->transid)
3249 return;
3250
3251 /*
3252 * if the inode we're about to unlink was logged,
3253 * the log will be properly updated for any new names
3254 */
3255 if (BTRFS_I(inode)->logged_trans == trans->transid)
3256 return;
3257
3258 /*
3259 * when renaming files across directories, if the directory
3260 * there we're unlinking from gets fsync'd later on, there's
3261 * no way to find the destination directory later and fsync it
3262 * properly. So, we have to be conservative and force commits
3263 * so the new name gets discovered.
3264 */
3265 if (for_rename)
3266 goto record;
3267
3268 /* we can safely do the unlink without any special recording */
3269 return;
3270
3271record:
3272 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3273}
3274
3275/*
3276 * Call this after adding a new name for a file and it will properly
3277 * update the log to reflect the new name.
3278 *
3279 * It will return zero if all goes well, and it will return 1 if a
3280 * full transaction commit is required.
3281 */
3282int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3283 struct inode *inode, struct inode *old_dir,
3284 struct dentry *parent)
3285{
3286 struct btrfs_root * root = BTRFS_I(inode)->root;
3287
3288 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003289 * this will force the logging code to walk the dentry chain
3290 * up for the file
3291 */
3292 if (S_ISREG(inode->i_mode))
3293 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3294
3295 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003296 * if this inode hasn't been logged and directory we're renaming it
3297 * from hasn't been logged, we don't need to log it
3298 */
3299 if (BTRFS_I(inode)->logged_trans <=
3300 root->fs_info->last_trans_committed &&
3301 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3302 root->fs_info->last_trans_committed))
3303 return 0;
3304
3305 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3306}
3307