blob: 0350147106d5be81a0163f6d7f931c6ca94e7b29 [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masone02119d2008-09-05 16:13:11 -040021#include "ctree.h"
22#include "transaction.h"
23#include "disk-io.h"
24#include "locking.h"
25#include "print-tree.h"
26#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050027#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040028
29/* magic values for the inode_only field in btrfs_log_inode:
30 *
31 * LOG_INODE_ALL means to log everything
32 * LOG_INODE_EXISTS means to log just enough to recreate the inode
33 * during log replay
34 */
35#define LOG_INODE_ALL 0
36#define LOG_INODE_EXISTS 1
37
38/*
Chris Mason12fcfd22009-03-24 10:24:20 -040039 * directory trouble cases
40 *
41 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
42 * log, we must force a full commit before doing an fsync of the directory
43 * where the unlink was done.
44 * ---> record transid of last unlink/rename per directory
45 *
46 * mkdir foo/some_dir
47 * normal commit
48 * rename foo/some_dir foo2/some_dir
49 * mkdir foo/some_dir
50 * fsync foo/some_dir/some_file
51 *
52 * The fsync above will unlink the original some_dir without recording
53 * it in its new location (foo2). After a crash, some_dir will be gone
54 * unless the fsync of some_file forces a full commit
55 *
56 * 2) we must log any new names for any file or dir that is in the fsync
57 * log. ---> check inode while renaming/linking.
58 *
59 * 2a) we must log any new names for any file or dir during rename
60 * when the directory they are being removed from was logged.
61 * ---> check inode and old parent dir during rename
62 *
63 * 2a is actually the more important variant. With the extra logging
64 * a crash might unlink the old name without recreating the new one
65 *
66 * 3) after a crash, we must go through any directories with a link count
67 * of zero and redo the rm -rf
68 *
69 * mkdir f1/foo
70 * normal commit
71 * rm -rf f1/foo
72 * fsync(f1)
73 *
74 * The directory f1 was fully removed from the FS, but fsync was never
75 * called on f1, only its parent dir. After a crash the rm -rf must
76 * be replayed. This must be able to recurse down the entire
77 * directory tree. The inode link count fixup code takes care of the
78 * ugly details.
79 */
80
81/*
Chris Masone02119d2008-09-05 16:13:11 -040082 * stages for the tree walking. The first
83 * stage (0) is to only pin down the blocks we find
84 * the second stage (1) is to make sure that all the inodes
85 * we find in the log are created in the subvolume.
86 *
87 * The last stage is to deal with directories and links and extents
88 * and all the other fun semantics
89 */
90#define LOG_WALK_PIN_ONLY 0
91#define LOG_WALK_REPLAY_INODES 1
92#define LOG_WALK_REPLAY_ALL 2
93
Chris Mason12fcfd22009-03-24 10:24:20 -040094static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040095 struct btrfs_root *root, struct inode *inode,
96 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -050097static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400100static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_root *log,
103 struct btrfs_path *path,
104 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400105
106/*
107 * tree logging is a special write ahead log used to make sure that
108 * fsyncs and O_SYNCs can happen without doing full tree commits.
109 *
110 * Full tree commits are expensive because they require commonly
111 * modified blocks to be recowed, creating many dirty pages in the
112 * extent tree an 4x-6x higher write load than ext3.
113 *
114 * Instead of doing a tree commit on every fsync, we use the
115 * key ranges and transaction ids to find items for a given file or directory
116 * that have changed in this transaction. Those items are copied into
117 * a special tree (one per subvolume root), that tree is written to disk
118 * and then the fsync is considered complete.
119 *
120 * After a crash, items are copied out of the log-tree back into the
121 * subvolume tree. Any file data extents found are recorded in the extent
122 * allocation tree, and the log-tree freed.
123 *
124 * The log tree is read three times, once to pin down all the extents it is
125 * using in ram and once, once to create all the inodes logged in the tree
126 * and once to do all the other items.
127 */
128
129/*
Chris Masone02119d2008-09-05 16:13:11 -0400130 * start a sub transaction and setup the log tree
131 * this increments the log tree writer count to make the people
132 * syncing the tree wait for us to finish
133 */
134static int start_log_trans(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root)
136{
137 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400138 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500139
140 mutex_lock(&root->log_mutex);
141 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400142 if (!root->log_start_pid) {
143 root->log_start_pid = current->pid;
144 root->log_multiple_pids = false;
145 } else if (root->log_start_pid != current->pid) {
146 root->log_multiple_pids = true;
147 }
148
Yan Zheng7237f182009-01-21 12:54:03 -0500149 root->log_batch++;
150 atomic_inc(&root->log_writers);
151 mutex_unlock(&root->log_mutex);
152 return 0;
153 }
Josef Bacikff782e02009-10-08 15:30:04 -0400154 root->log_multiple_pids = false;
155 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400156 mutex_lock(&root->fs_info->tree_log_mutex);
157 if (!root->fs_info->log_root_tree) {
158 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400159 if (ret)
160 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400161 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400162 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400163 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400164 if (ret)
165 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Chris Masone02119d2008-09-05 16:13:11 -0400167 mutex_unlock(&root->fs_info->tree_log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -0500168 root->log_batch++;
169 atomic_inc(&root->log_writers);
170 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400171 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400172}
173
174/*
175 * returns 0 if there was a log transaction running and we were able
176 * to join, or returns -ENOENT if there were not transactions
177 * in progress
178 */
179static int join_running_log_trans(struct btrfs_root *root)
180{
181 int ret = -ENOENT;
182
183 smp_mb();
184 if (!root->log_root)
185 return -ENOENT;
186
Yan Zheng7237f182009-01-21 12:54:03 -0500187 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400188 if (root->log_root) {
189 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500190 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400191 }
Yan Zheng7237f182009-01-21 12:54:03 -0500192 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400193 return ret;
194}
195
196/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400197 * This either makes the current running log transaction wait
198 * until you call btrfs_end_log_trans() or it makes any future
199 * log transactions wait until you call btrfs_end_log_trans()
200 */
201int btrfs_pin_log_trans(struct btrfs_root *root)
202{
203 int ret = -ENOENT;
204
205 mutex_lock(&root->log_mutex);
206 atomic_inc(&root->log_writers);
207 mutex_unlock(&root->log_mutex);
208 return ret;
209}
210
211/*
Chris Masone02119d2008-09-05 16:13:11 -0400212 * indicate we're done making changes to the log tree
213 * and wake up anyone waiting to do a sync
214 */
Chris Mason12fcfd22009-03-24 10:24:20 -0400215int btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400216{
Yan Zheng7237f182009-01-21 12:54:03 -0500217 if (atomic_dec_and_test(&root->log_writers)) {
218 smp_mb();
219 if (waitqueue_active(&root->log_writer_wait))
220 wake_up(&root->log_writer_wait);
221 }
Chris Masone02119d2008-09-05 16:13:11 -0400222 return 0;
223}
224
225
226/*
227 * the walk control struct is used to pass state down the chain when
228 * processing the log tree. The stage field tells us which part
229 * of the log tree processing we are currently doing. The others
230 * are state fields used for that specific part
231 */
232struct walk_control {
233 /* should we free the extent on disk when done? This is used
234 * at transaction commit time while freeing a log tree
235 */
236 int free;
237
238 /* should we write out the extent buffer? This is used
239 * while flushing the log tree to disk during a sync
240 */
241 int write;
242
243 /* should we wait for the extent buffer io to finish? Also used
244 * while flushing the log tree to disk for a sync
245 */
246 int wait;
247
248 /* pin only walk, we record which extents on disk belong to the
249 * log trees
250 */
251 int pin;
252
253 /* what stage of the replay code we're currently in */
254 int stage;
255
256 /* the root we are currently replaying */
257 struct btrfs_root *replay_dest;
258
259 /* the trans handle for the current replay */
260 struct btrfs_trans_handle *trans;
261
262 /* the function that gets used to process blocks we find in the
263 * tree. Note the extent_buffer might not be up to date when it is
264 * passed in, and it must be checked or read if you need the data
265 * inside it
266 */
267 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
268 struct walk_control *wc, u64 gen);
269};
270
271/*
272 * process_func used to pin down extents, write them or wait on them
273 */
274static int process_one_buffer(struct btrfs_root *log,
275 struct extent_buffer *eb,
276 struct walk_control *wc, u64 gen)
277{
Josef Bacik04018de2009-04-03 10:14:18 -0400278 if (wc->pin)
Yan Zheng11833d62009-09-11 16:11:19 -0400279 btrfs_pin_extent(log->fs_info->extent_root,
280 eb->start, eb->len, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400281
282 if (btrfs_buffer_uptodate(eb, gen)) {
283 if (wc->write)
284 btrfs_write_tree_block(eb);
285 if (wc->wait)
286 btrfs_wait_tree_block_writeback(eb);
287 }
288 return 0;
289}
290
291/*
292 * Item overwrite used by replay and tree logging. eb, slot and key all refer
293 * to the src data we are copying out.
294 *
295 * root is the tree we are copying into, and path is a scratch
296 * path for use in this function (it should be released on entry and
297 * will be released on exit).
298 *
299 * If the key is already in the destination tree the existing item is
300 * overwritten. If the existing item isn't big enough, it is extended.
301 * If it is too large, it is truncated.
302 *
303 * If the key isn't in the destination yet, a new item is inserted.
304 */
305static noinline int overwrite_item(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root,
307 struct btrfs_path *path,
308 struct extent_buffer *eb, int slot,
309 struct btrfs_key *key)
310{
311 int ret;
312 u32 item_size;
313 u64 saved_i_size = 0;
314 int save_old_i_size = 0;
315 unsigned long src_ptr;
316 unsigned long dst_ptr;
317 int overwrite_root = 0;
318
319 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
320 overwrite_root = 1;
321
322 item_size = btrfs_item_size_nr(eb, slot);
323 src_ptr = btrfs_item_ptr_offset(eb, slot);
324
325 /* look for the key in the destination tree */
326 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
327 if (ret == 0) {
328 char *src_copy;
329 char *dst_copy;
330 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
331 path->slots[0]);
332 if (dst_size != item_size)
333 goto insert;
334
335 if (item_size == 0) {
336 btrfs_release_path(root, path);
337 return 0;
338 }
339 dst_copy = kmalloc(item_size, GFP_NOFS);
340 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000341 if (!dst_copy || !src_copy) {
342 btrfs_release_path(root, path);
343 kfree(dst_copy);
344 kfree(src_copy);
345 return -ENOMEM;
346 }
Chris Masone02119d2008-09-05 16:13:11 -0400347
348 read_extent_buffer(eb, src_copy, src_ptr, item_size);
349
350 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
351 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
352 item_size);
353 ret = memcmp(dst_copy, src_copy, item_size);
354
355 kfree(dst_copy);
356 kfree(src_copy);
357 /*
358 * they have the same contents, just return, this saves
359 * us from cowing blocks in the destination tree and doing
360 * extra writes that may not have been done by a previous
361 * sync
362 */
363 if (ret == 0) {
364 btrfs_release_path(root, path);
365 return 0;
366 }
367
368 }
369insert:
370 btrfs_release_path(root, path);
371 /* try to insert the key into the destination tree */
372 ret = btrfs_insert_empty_item(trans, root, path,
373 key, item_size);
374
375 /* make sure any existing item is the correct size */
376 if (ret == -EEXIST) {
377 u32 found_size;
378 found_size = btrfs_item_size_nr(path->nodes[0],
379 path->slots[0]);
380 if (found_size > item_size) {
381 btrfs_truncate_item(trans, root, path, item_size, 1);
382 } else if (found_size < item_size) {
Yan Zheng87b29b22008-12-17 10:21:48 -0500383 ret = btrfs_extend_item(trans, root, path,
384 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400385 }
386 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400387 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400388 }
389 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
390 path->slots[0]);
391
392 /* don't overwrite an existing inode if the generation number
393 * was logged as zero. This is done when the tree logging code
394 * is just logging an inode to make sure it exists after recovery.
395 *
396 * Also, don't overwrite i_size on directories during replay.
397 * log replay inserts and removes directory items based on the
398 * state of the tree found in the subvolume, and i_size is modified
399 * as it goes
400 */
401 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
402 struct btrfs_inode_item *src_item;
403 struct btrfs_inode_item *dst_item;
404
405 src_item = (struct btrfs_inode_item *)src_ptr;
406 dst_item = (struct btrfs_inode_item *)dst_ptr;
407
408 if (btrfs_inode_generation(eb, src_item) == 0)
409 goto no_copy;
410
411 if (overwrite_root &&
412 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
413 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
414 save_old_i_size = 1;
415 saved_i_size = btrfs_inode_size(path->nodes[0],
416 dst_item);
417 }
418 }
419
420 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
421 src_ptr, item_size);
422
423 if (save_old_i_size) {
424 struct btrfs_inode_item *dst_item;
425 dst_item = (struct btrfs_inode_item *)dst_ptr;
426 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
427 }
428
429 /* make sure the generation is filled in */
430 if (key->type == BTRFS_INODE_ITEM_KEY) {
431 struct btrfs_inode_item *dst_item;
432 dst_item = (struct btrfs_inode_item *)dst_ptr;
433 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
434 btrfs_set_inode_generation(path->nodes[0], dst_item,
435 trans->transid);
436 }
437 }
438no_copy:
439 btrfs_mark_buffer_dirty(path->nodes[0]);
440 btrfs_release_path(root, path);
441 return 0;
442}
443
444/*
445 * simple helper to read an inode off the disk from a given root
446 * This can only be called for subvolume roots and not for the log
447 */
448static noinline struct inode *read_one_inode(struct btrfs_root *root,
449 u64 objectid)
450{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400451 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400452 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400453
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400454 key.objectid = objectid;
455 key.type = BTRFS_INODE_ITEM_KEY;
456 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000457 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400458 if (IS_ERR(inode)) {
459 inode = NULL;
460 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400461 iput(inode);
462 inode = NULL;
463 }
464 return inode;
465}
466
467/* replays a single extent in 'eb' at 'slot' with 'key' into the
468 * subvolume 'root'. path is released on entry and should be released
469 * on exit.
470 *
471 * extents in the log tree have not been allocated out of the extent
472 * tree yet. So, this completes the allocation, taking a reference
473 * as required if the extent already exists or creating a new extent
474 * if it isn't in the extent allocation tree yet.
475 *
476 * The extent is inserted into the file, dropping any existing extents
477 * from the file that overlap the new one.
478 */
479static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root,
481 struct btrfs_path *path,
482 struct extent_buffer *eb, int slot,
483 struct btrfs_key *key)
484{
485 int found_type;
486 u64 mask = root->sectorsize - 1;
487 u64 extent_end;
488 u64 alloc_hint;
489 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500490 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400491 struct btrfs_file_extent_item *item;
492 struct inode *inode = NULL;
493 unsigned long size;
494 int ret = 0;
495
496 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
497 found_type = btrfs_file_extent_type(eb, item);
498
Yan Zhengd899e052008-10-30 14:25:28 -0400499 if (found_type == BTRFS_FILE_EXTENT_REG ||
500 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400501 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
502 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400504 extent_end = (start + size + mask) & ~mask;
505 } else {
506 ret = 0;
507 goto out;
508 }
509
510 inode = read_one_inode(root, key->objectid);
511 if (!inode) {
512 ret = -EIO;
513 goto out;
514 }
515
516 /*
517 * first check to see if we already have this extent in the
518 * file. This must be done before the btrfs_drop_extents run
519 * so we don't try to drop this extent.
520 */
521 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
522 start, 0);
523
Yan Zhengd899e052008-10-30 14:25:28 -0400524 if (ret == 0 &&
525 (found_type == BTRFS_FILE_EXTENT_REG ||
526 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400527 struct btrfs_file_extent_item cmp1;
528 struct btrfs_file_extent_item cmp2;
529 struct btrfs_file_extent_item *existing;
530 struct extent_buffer *leaf;
531
532 leaf = path->nodes[0];
533 existing = btrfs_item_ptr(leaf, path->slots[0],
534 struct btrfs_file_extent_item);
535
536 read_extent_buffer(eb, &cmp1, (unsigned long)item,
537 sizeof(cmp1));
538 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
539 sizeof(cmp2));
540
541 /*
542 * we already have a pointer to this exact extent,
543 * we don't have to do anything
544 */
545 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
546 btrfs_release_path(root, path);
547 goto out;
548 }
549 }
550 btrfs_release_path(root, path);
551
Yan Zheng07d400a2009-01-06 11:42:00 -0500552 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400553 /* drop any overlapping extents */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000554 ret = btrfs_drop_extents(trans, inode, start, extent_end,
555 &alloc_hint, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400556 BUG_ON(ret);
557
Yan Zheng07d400a2009-01-06 11:42:00 -0500558 if (found_type == BTRFS_FILE_EXTENT_REG ||
559 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500561 unsigned long dest_offset;
562 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400563
Yan Zheng07d400a2009-01-06 11:42:00 -0500564 ret = btrfs_insert_empty_item(trans, root, path, key,
565 sizeof(*item));
566 BUG_ON(ret);
567 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
568 path->slots[0]);
569 copy_extent_buffer(path->nodes[0], eb, dest_offset,
570 (unsigned long)item, sizeof(*item));
571
572 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
573 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
574 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400575 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500576
577 if (ins.objectid > 0) {
578 u64 csum_start;
579 u64 csum_end;
580 LIST_HEAD(ordered_sums);
581 /*
582 * is this extent already allocated in the extent
583 * allocation tree? If so, just add a reference
584 */
585 ret = btrfs_lookup_extent(root, ins.objectid,
586 ins.offset);
587 if (ret == 0) {
588 ret = btrfs_inc_extent_ref(trans, root,
589 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 0, root->root_key.objectid,
591 key->objectid, offset);
Yan Zheng07d400a2009-01-06 11:42:00 -0500592 } else {
593 /*
594 * insert the extent pointer in the extent
595 * allocation tree
596 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400597 ret = btrfs_alloc_logged_file_extent(trans,
598 root, root->root_key.objectid,
599 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500600 BUG_ON(ret);
601 }
602 btrfs_release_path(root, path);
603
604 if (btrfs_file_extent_compression(eb, item)) {
605 csum_start = ins.objectid;
606 csum_end = csum_start + ins.offset;
607 } else {
608 csum_start = ins.objectid +
609 btrfs_file_extent_offset(eb, item);
610 csum_end = csum_start +
611 btrfs_file_extent_num_bytes(eb, item);
612 }
613
614 ret = btrfs_lookup_csums_range(root->log_root,
615 csum_start, csum_end - 1,
616 &ordered_sums);
617 BUG_ON(ret);
618 while (!list_empty(&ordered_sums)) {
619 struct btrfs_ordered_sum *sums;
620 sums = list_entry(ordered_sums.next,
621 struct btrfs_ordered_sum,
622 list);
623 ret = btrfs_csum_file_blocks(trans,
624 root->fs_info->csum_root,
625 sums);
626 BUG_ON(ret);
627 list_del(&sums->list);
628 kfree(sums);
629 }
630 } else {
631 btrfs_release_path(root, path);
632 }
633 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
634 /* inline extents are easy, we just overwrite them */
635 ret = overwrite_item(trans, root, path, eb, slot, key);
636 BUG_ON(ret);
637 }
638
639 inode_set_bytes(inode, saved_nbytes);
Chris Masone02119d2008-09-05 16:13:11 -0400640 btrfs_update_inode(trans, root, inode);
641out:
642 if (inode)
643 iput(inode);
644 return ret;
645}
646
647/*
648 * when cleaning up conflicts between the directory names in the
649 * subvolume, directory names in the log and directory names in the
650 * inode back references, we may have to unlink inodes from directories.
651 *
652 * This is a helper function to do the unlink of a specific directory
653 * item
654 */
655static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
656 struct btrfs_root *root,
657 struct btrfs_path *path,
658 struct inode *dir,
659 struct btrfs_dir_item *di)
660{
661 struct inode *inode;
662 char *name;
663 int name_len;
664 struct extent_buffer *leaf;
665 struct btrfs_key location;
666 int ret;
667
668 leaf = path->nodes[0];
669
670 btrfs_dir_item_key_to_cpu(leaf, di, &location);
671 name_len = btrfs_dir_name_len(leaf, di);
672 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000673 if (!name)
674 return -ENOMEM;
675
Chris Masone02119d2008-09-05 16:13:11 -0400676 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
677 btrfs_release_path(root, path);
678
679 inode = read_one_inode(root, location.objectid);
680 BUG_ON(!inode);
681
Yan Zhengec051c02009-01-05 15:43:42 -0500682 ret = link_to_fixup_dir(trans, root, path, location.objectid);
683 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400684
Chris Masone02119d2008-09-05 16:13:11 -0400685 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500686 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400687 kfree(name);
688
689 iput(inode);
690 return ret;
691}
692
693/*
694 * helper function to see if a given name and sequence number found
695 * in an inode back reference are already in a directory and correctly
696 * point to this inode
697 */
698static noinline int inode_in_dir(struct btrfs_root *root,
699 struct btrfs_path *path,
700 u64 dirid, u64 objectid, u64 index,
701 const char *name, int name_len)
702{
703 struct btrfs_dir_item *di;
704 struct btrfs_key location;
705 int match = 0;
706
707 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
708 index, name, name_len, 0);
709 if (di && !IS_ERR(di)) {
710 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
711 if (location.objectid != objectid)
712 goto out;
713 } else
714 goto out;
715 btrfs_release_path(root, path);
716
717 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
718 if (di && !IS_ERR(di)) {
719 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
720 if (location.objectid != objectid)
721 goto out;
722 } else
723 goto out;
724 match = 1;
725out:
726 btrfs_release_path(root, path);
727 return match;
728}
729
730/*
731 * helper function to check a log tree for a named back reference in
732 * an inode. This is used to decide if a back reference that is
733 * found in the subvolume conflicts with what we find in the log.
734 *
735 * inode backreferences may have multiple refs in a single item,
736 * during replay we process one reference at a time, and we don't
737 * want to delete valid links to a file from the subvolume if that
738 * link is also in the log.
739 */
740static noinline int backref_in_log(struct btrfs_root *log,
741 struct btrfs_key *key,
742 char *name, int namelen)
743{
744 struct btrfs_path *path;
745 struct btrfs_inode_ref *ref;
746 unsigned long ptr;
747 unsigned long ptr_end;
748 unsigned long name_ptr;
749 int found_name_len;
750 int item_size;
751 int ret;
752 int match = 0;
753
754 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000755 if (!path)
756 return -ENOMEM;
757
Chris Masone02119d2008-09-05 16:13:11 -0400758 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
759 if (ret != 0)
760 goto out;
761
762 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
763 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
764 ptr_end = ptr + item_size;
765 while (ptr < ptr_end) {
766 ref = (struct btrfs_inode_ref *)ptr;
767 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
768 if (found_name_len == namelen) {
769 name_ptr = (unsigned long)(ref + 1);
770 ret = memcmp_extent_buffer(path->nodes[0], name,
771 name_ptr, namelen);
772 if (ret == 0) {
773 match = 1;
774 goto out;
775 }
776 }
777 ptr = (unsigned long)(ref + 1) + found_name_len;
778 }
779out:
780 btrfs_free_path(path);
781 return match;
782}
783
784
785/*
786 * replay one inode back reference item found in the log tree.
787 * eb, slot and key refer to the buffer and key found in the log tree.
788 * root is the destination we are replaying into, and path is for temp
789 * use by this function. (it should be released on return).
790 */
791static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
792 struct btrfs_root *root,
793 struct btrfs_root *log,
794 struct btrfs_path *path,
795 struct extent_buffer *eb, int slot,
796 struct btrfs_key *key)
797{
798 struct inode *dir;
799 int ret;
Chris Masone02119d2008-09-05 16:13:11 -0400800 struct btrfs_inode_ref *ref;
Chris Masone02119d2008-09-05 16:13:11 -0400801 struct inode *inode;
802 char *name;
803 int namelen;
804 unsigned long ref_ptr;
805 unsigned long ref_end;
liuboc622ae62011-03-26 08:01:12 -0400806 int search_done = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400807
Chris Masone02119d2008-09-05 16:13:11 -0400808 /*
809 * it is possible that we didn't log all the parent directories
810 * for a given inode. If we don't find the dir, just don't
811 * copy the back ref in. The link count fixup code will take
812 * care of the rest
813 */
814 dir = read_one_inode(root, key->offset);
815 if (!dir)
816 return -ENOENT;
817
818 inode = read_one_inode(root, key->objectid);
Julia Lawall631c07c2009-07-27 13:57:00 -0400819 BUG_ON(!inode);
Chris Masone02119d2008-09-05 16:13:11 -0400820
821 ref_ptr = btrfs_item_ptr_offset(eb, slot);
822 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
823
824again:
825 ref = (struct btrfs_inode_ref *)ref_ptr;
826
827 namelen = btrfs_inode_ref_name_len(eb, ref);
828 name = kmalloc(namelen, GFP_NOFS);
829 BUG_ON(!name);
830
831 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
832
833 /* if we already have a perfect match, we're done */
834 if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
835 btrfs_inode_ref_index(eb, ref),
836 name, namelen)) {
837 goto out;
838 }
839
840 /*
841 * look for a conflicting back reference in the metadata.
842 * if we find one we have to unlink that name of the file
843 * before we add our new link. Later on, we overwrite any
844 * existing back reference, and we don't want to create
845 * dangling pointers in the directory.
846 */
liuboc622ae62011-03-26 08:01:12 -0400847
848 if (search_done)
849 goto insert;
850
Chris Masone02119d2008-09-05 16:13:11 -0400851 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
852 if (ret == 0) {
853 char *victim_name;
854 int victim_name_len;
855 struct btrfs_inode_ref *victim_ref;
856 unsigned long ptr;
857 unsigned long ptr_end;
858 struct extent_buffer *leaf = path->nodes[0];
859
860 /* are we trying to overwrite a back ref for the root directory
861 * if so, just jump out, we're done
862 */
863 if (key->objectid == key->offset)
864 goto out_nowrite;
865
866 /* check all the names in this back reference to see
867 * if they are in the log. if so, we allow them to stay
868 * otherwise they must be unlinked as a conflict
869 */
870 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
871 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500872 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400873 victim_ref = (struct btrfs_inode_ref *)ptr;
874 victim_name_len = btrfs_inode_ref_name_len(leaf,
875 victim_ref);
876 victim_name = kmalloc(victim_name_len, GFP_NOFS);
877 BUG_ON(!victim_name);
878
879 read_extent_buffer(leaf, victim_name,
880 (unsigned long)(victim_ref + 1),
881 victim_name_len);
882
883 if (!backref_in_log(log, key, victim_name,
884 victim_name_len)) {
885 btrfs_inc_nlink(inode);
886 btrfs_release_path(root, path);
Chris Mason12fcfd22009-03-24 10:24:20 -0400887
Chris Masone02119d2008-09-05 16:13:11 -0400888 ret = btrfs_unlink_inode(trans, root, dir,
889 inode, victim_name,
890 victim_name_len);
Chris Masone02119d2008-09-05 16:13:11 -0400891 }
892 kfree(victim_name);
893 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
894 }
895 BUG_ON(ret);
liuboc622ae62011-03-26 08:01:12 -0400896
897 /*
898 * NOTE: we have searched root tree and checked the
899 * coresponding ref, it does not need to check again.
900 */
901 search_done = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400902 }
903 btrfs_release_path(root, path);
904
liuboc622ae62011-03-26 08:01:12 -0400905insert:
Chris Masone02119d2008-09-05 16:13:11 -0400906 /* insert our name */
907 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
908 btrfs_inode_ref_index(eb, ref));
909 BUG_ON(ret);
910
911 btrfs_update_inode(trans, root, inode);
912
913out:
914 ref_ptr = (unsigned long)(ref + 1) + namelen;
915 kfree(name);
916 if (ref_ptr < ref_end)
917 goto again;
918
919 /* finally write the back reference in the inode */
920 ret = overwrite_item(trans, root, path, eb, slot, key);
921 BUG_ON(ret);
922
923out_nowrite:
924 btrfs_release_path(root, path);
925 iput(dir);
926 iput(inode);
927 return 0;
928}
929
Yan, Zhengc71bf092009-11-12 09:34:40 +0000930static int insert_orphan_item(struct btrfs_trans_handle *trans,
931 struct btrfs_root *root, u64 offset)
932{
933 int ret;
934 ret = btrfs_find_orphan_item(root, offset);
935 if (ret > 0)
936 ret = btrfs_insert_orphan_item(trans, root, offset);
937 return ret;
938}
939
940
Chris Masone02119d2008-09-05 16:13:11 -0400941/*
Chris Masone02119d2008-09-05 16:13:11 -0400942 * There are a few corners where the link count of the file can't
943 * be properly maintained during replay. So, instead of adding
944 * lots of complexity to the log code, we just scan the backrefs
945 * for any file that has been through replay.
946 *
947 * The scan will update the link count on the inode to reflect the
948 * number of back refs found. If it goes down to zero, the iput
949 * will free the inode.
950 */
951static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
952 struct btrfs_root *root,
953 struct inode *inode)
954{
955 struct btrfs_path *path;
956 int ret;
957 struct btrfs_key key;
958 u64 nlink = 0;
959 unsigned long ptr;
960 unsigned long ptr_end;
961 int name_len;
962
963 key.objectid = inode->i_ino;
964 key.type = BTRFS_INODE_REF_KEY;
965 key.offset = (u64)-1;
966
967 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000968 if (!path)
969 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -0400970
Chris Masond3977122009-01-05 21:25:51 -0500971 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -0400972 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
973 if (ret < 0)
974 break;
975 if (ret > 0) {
976 if (path->slots[0] == 0)
977 break;
978 path->slots[0]--;
979 }
980 btrfs_item_key_to_cpu(path->nodes[0], &key,
981 path->slots[0]);
982 if (key.objectid != inode->i_ino ||
983 key.type != BTRFS_INODE_REF_KEY)
984 break;
985 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
986 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
987 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500988 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400989 struct btrfs_inode_ref *ref;
990
991 ref = (struct btrfs_inode_ref *)ptr;
992 name_len = btrfs_inode_ref_name_len(path->nodes[0],
993 ref);
994 ptr = (unsigned long)(ref + 1) + name_len;
995 nlink++;
996 }
997
998 if (key.offset == 0)
999 break;
1000 key.offset--;
1001 btrfs_release_path(root, path);
1002 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001003 btrfs_release_path(root, path);
Chris Masone02119d2008-09-05 16:13:11 -04001004 if (nlink != inode->i_nlink) {
1005 inode->i_nlink = nlink;
1006 btrfs_update_inode(trans, root, inode);
1007 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001008 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001009
Yan, Zhengc71bf092009-11-12 09:34:40 +00001010 if (inode->i_nlink == 0) {
1011 if (S_ISDIR(inode->i_mode)) {
1012 ret = replay_dir_deletes(trans, root, NULL, path,
1013 inode->i_ino, 1);
1014 BUG_ON(ret);
1015 }
1016 ret = insert_orphan_item(trans, root, inode->i_ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001017 BUG_ON(ret);
1018 }
1019 btrfs_free_path(path);
1020
Chris Masone02119d2008-09-05 16:13:11 -04001021 return 0;
1022}
1023
1024static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1025 struct btrfs_root *root,
1026 struct btrfs_path *path)
1027{
1028 int ret;
1029 struct btrfs_key key;
1030 struct inode *inode;
1031
1032 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1033 key.type = BTRFS_ORPHAN_ITEM_KEY;
1034 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001035 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001036 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1037 if (ret < 0)
1038 break;
1039
1040 if (ret == 1) {
1041 if (path->slots[0] == 0)
1042 break;
1043 path->slots[0]--;
1044 }
1045
1046 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1047 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1048 key.type != BTRFS_ORPHAN_ITEM_KEY)
1049 break;
1050
1051 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001052 if (ret)
1053 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001054
1055 btrfs_release_path(root, path);
1056 inode = read_one_inode(root, key.offset);
1057 BUG_ON(!inode);
1058
1059 ret = fixup_inode_link_count(trans, root, inode);
1060 BUG_ON(ret);
1061
1062 iput(inode);
1063
Chris Mason12fcfd22009-03-24 10:24:20 -04001064 /*
1065 * fixup on a directory may create new entries,
1066 * make sure we always look for the highset possible
1067 * offset
1068 */
1069 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001070 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001071 ret = 0;
1072out:
Chris Masone02119d2008-09-05 16:13:11 -04001073 btrfs_release_path(root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001074 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001075}
1076
1077
1078/*
1079 * record a given inode in the fixup dir so we can check its link
1080 * count when replay is done. The link count is incremented here
1081 * so the inode won't go away until we check it
1082 */
1083static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1084 struct btrfs_root *root,
1085 struct btrfs_path *path,
1086 u64 objectid)
1087{
1088 struct btrfs_key key;
1089 int ret = 0;
1090 struct inode *inode;
1091
1092 inode = read_one_inode(root, objectid);
1093 BUG_ON(!inode);
1094
1095 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1096 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1097 key.offset = objectid;
1098
1099 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1100
1101 btrfs_release_path(root, path);
1102 if (ret == 0) {
1103 btrfs_inc_nlink(inode);
1104 btrfs_update_inode(trans, root, inode);
1105 } else if (ret == -EEXIST) {
1106 ret = 0;
1107 } else {
1108 BUG();
1109 }
1110 iput(inode);
1111
1112 return ret;
1113}
1114
1115/*
1116 * when replaying the log for a directory, we only insert names
1117 * for inodes that actually exist. This means an fsync on a directory
1118 * does not implicitly fsync all the new files in it
1119 */
1120static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1121 struct btrfs_root *root,
1122 struct btrfs_path *path,
1123 u64 dirid, u64 index,
1124 char *name, int name_len, u8 type,
1125 struct btrfs_key *location)
1126{
1127 struct inode *inode;
1128 struct inode *dir;
1129 int ret;
1130
1131 inode = read_one_inode(root, location->objectid);
1132 if (!inode)
1133 return -ENOENT;
1134
1135 dir = read_one_inode(root, dirid);
1136 if (!dir) {
1137 iput(inode);
1138 return -EIO;
1139 }
1140 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1141
1142 /* FIXME, put inode into FIXUP list */
1143
1144 iput(inode);
1145 iput(dir);
1146 return ret;
1147}
1148
1149/*
1150 * take a single entry in a log directory item and replay it into
1151 * the subvolume.
1152 *
1153 * if a conflicting item exists in the subdirectory already,
1154 * the inode it points to is unlinked and put into the link count
1155 * fix up tree.
1156 *
1157 * If a name from the log points to a file or directory that does
1158 * not exist in the FS, it is skipped. fsyncs on directories
1159 * do not force down inodes inside that directory, just changes to the
1160 * names or unlinks in a directory.
1161 */
1162static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1163 struct btrfs_root *root,
1164 struct btrfs_path *path,
1165 struct extent_buffer *eb,
1166 struct btrfs_dir_item *di,
1167 struct btrfs_key *key)
1168{
1169 char *name;
1170 int name_len;
1171 struct btrfs_dir_item *dst_di;
1172 struct btrfs_key found_key;
1173 struct btrfs_key log_key;
1174 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001175 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001176 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001177 int ret;
1178
1179 dir = read_one_inode(root, key->objectid);
1180 BUG_ON(!dir);
1181
1182 name_len = btrfs_dir_name_len(eb, di);
1183 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001184 if (!name)
1185 return -ENOMEM;
1186
Chris Masone02119d2008-09-05 16:13:11 -04001187 log_type = btrfs_dir_type(eb, di);
1188 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1189 name_len);
1190
1191 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001192 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1193 if (exists == 0)
1194 exists = 1;
1195 else
1196 exists = 0;
1197 btrfs_release_path(root, path);
1198
Chris Masone02119d2008-09-05 16:13:11 -04001199 if (key->type == BTRFS_DIR_ITEM_KEY) {
1200 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1201 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001202 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001203 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1204 key->objectid,
1205 key->offset, name,
1206 name_len, 1);
1207 } else {
1208 BUG();
1209 }
1210 if (!dst_di || IS_ERR(dst_di)) {
1211 /* we need a sequence number to insert, so we only
1212 * do inserts for the BTRFS_DIR_INDEX_KEY types
1213 */
1214 if (key->type != BTRFS_DIR_INDEX_KEY)
1215 goto out;
1216 goto insert;
1217 }
1218
1219 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1220 /* the existing item matches the logged item */
1221 if (found_key.objectid == log_key.objectid &&
1222 found_key.type == log_key.type &&
1223 found_key.offset == log_key.offset &&
1224 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1225 goto out;
1226 }
1227
1228 /*
1229 * don't drop the conflicting directory entry if the inode
1230 * for the new entry doesn't exist
1231 */
Chris Mason4bef0842008-09-08 11:18:08 -04001232 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001233 goto out;
1234
Chris Masone02119d2008-09-05 16:13:11 -04001235 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1236 BUG_ON(ret);
1237
1238 if (key->type == BTRFS_DIR_INDEX_KEY)
1239 goto insert;
1240out:
1241 btrfs_release_path(root, path);
1242 kfree(name);
1243 iput(dir);
1244 return 0;
1245
1246insert:
1247 btrfs_release_path(root, path);
1248 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1249 name, name_len, log_type, &log_key);
1250
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001251 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001252 goto out;
1253}
1254
1255/*
1256 * find all the names in a directory item and reconcile them into
1257 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1258 * one name in a directory item, but the same code gets used for
1259 * both directory index types
1260 */
1261static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1262 struct btrfs_root *root,
1263 struct btrfs_path *path,
1264 struct extent_buffer *eb, int slot,
1265 struct btrfs_key *key)
1266{
1267 int ret;
1268 u32 item_size = btrfs_item_size_nr(eb, slot);
1269 struct btrfs_dir_item *di;
1270 int name_len;
1271 unsigned long ptr;
1272 unsigned long ptr_end;
1273
1274 ptr = btrfs_item_ptr_offset(eb, slot);
1275 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001276 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001277 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001278 if (verify_dir_item(root, eb, di))
1279 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001280 name_len = btrfs_dir_name_len(eb, di);
1281 ret = replay_one_name(trans, root, path, eb, di, key);
1282 BUG_ON(ret);
1283 ptr = (unsigned long)(di + 1);
1284 ptr += name_len;
1285 }
1286 return 0;
1287}
1288
1289/*
1290 * directory replay has two parts. There are the standard directory
1291 * items in the log copied from the subvolume, and range items
1292 * created in the log while the subvolume was logged.
1293 *
1294 * The range items tell us which parts of the key space the log
1295 * is authoritative for. During replay, if a key in the subvolume
1296 * directory is in a logged range item, but not actually in the log
1297 * that means it was deleted from the directory before the fsync
1298 * and should be removed.
1299 */
1300static noinline int find_dir_range(struct btrfs_root *root,
1301 struct btrfs_path *path,
1302 u64 dirid, int key_type,
1303 u64 *start_ret, u64 *end_ret)
1304{
1305 struct btrfs_key key;
1306 u64 found_end;
1307 struct btrfs_dir_log_item *item;
1308 int ret;
1309 int nritems;
1310
1311 if (*start_ret == (u64)-1)
1312 return 1;
1313
1314 key.objectid = dirid;
1315 key.type = key_type;
1316 key.offset = *start_ret;
1317
1318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1319 if (ret < 0)
1320 goto out;
1321 if (ret > 0) {
1322 if (path->slots[0] == 0)
1323 goto out;
1324 path->slots[0]--;
1325 }
1326 if (ret != 0)
1327 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1328
1329 if (key.type != key_type || key.objectid != dirid) {
1330 ret = 1;
1331 goto next;
1332 }
1333 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1334 struct btrfs_dir_log_item);
1335 found_end = btrfs_dir_log_end(path->nodes[0], item);
1336
1337 if (*start_ret >= key.offset && *start_ret <= found_end) {
1338 ret = 0;
1339 *start_ret = key.offset;
1340 *end_ret = found_end;
1341 goto out;
1342 }
1343 ret = 1;
1344next:
1345 /* check the next slot in the tree to see if it is a valid item */
1346 nritems = btrfs_header_nritems(path->nodes[0]);
1347 if (path->slots[0] >= nritems) {
1348 ret = btrfs_next_leaf(root, path);
1349 if (ret)
1350 goto out;
1351 } else {
1352 path->slots[0]++;
1353 }
1354
1355 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1356
1357 if (key.type != key_type || key.objectid != dirid) {
1358 ret = 1;
1359 goto out;
1360 }
1361 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1362 struct btrfs_dir_log_item);
1363 found_end = btrfs_dir_log_end(path->nodes[0], item);
1364 *start_ret = key.offset;
1365 *end_ret = found_end;
1366 ret = 0;
1367out:
1368 btrfs_release_path(root, path);
1369 return ret;
1370}
1371
1372/*
1373 * this looks for a given directory item in the log. If the directory
1374 * item is not in the log, the item is removed and the inode it points
1375 * to is unlinked
1376 */
1377static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1378 struct btrfs_root *root,
1379 struct btrfs_root *log,
1380 struct btrfs_path *path,
1381 struct btrfs_path *log_path,
1382 struct inode *dir,
1383 struct btrfs_key *dir_key)
1384{
1385 int ret;
1386 struct extent_buffer *eb;
1387 int slot;
1388 u32 item_size;
1389 struct btrfs_dir_item *di;
1390 struct btrfs_dir_item *log_di;
1391 int name_len;
1392 unsigned long ptr;
1393 unsigned long ptr_end;
1394 char *name;
1395 struct inode *inode;
1396 struct btrfs_key location;
1397
1398again:
1399 eb = path->nodes[0];
1400 slot = path->slots[0];
1401 item_size = btrfs_item_size_nr(eb, slot);
1402 ptr = btrfs_item_ptr_offset(eb, slot);
1403 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001404 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001405 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001406 if (verify_dir_item(root, eb, di)) {
1407 ret = -EIO;
1408 goto out;
1409 }
1410
Chris Masone02119d2008-09-05 16:13:11 -04001411 name_len = btrfs_dir_name_len(eb, di);
1412 name = kmalloc(name_len, GFP_NOFS);
1413 if (!name) {
1414 ret = -ENOMEM;
1415 goto out;
1416 }
1417 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1418 name_len);
1419 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001420 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001421 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1422 dir_key->objectid,
1423 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001424 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001425 log_di = btrfs_lookup_dir_index_item(trans, log,
1426 log_path,
1427 dir_key->objectid,
1428 dir_key->offset,
1429 name, name_len, 0);
1430 }
1431 if (!log_di || IS_ERR(log_di)) {
1432 btrfs_dir_item_key_to_cpu(eb, di, &location);
1433 btrfs_release_path(root, path);
1434 btrfs_release_path(log, log_path);
1435 inode = read_one_inode(root, location.objectid);
1436 BUG_ON(!inode);
1437
1438 ret = link_to_fixup_dir(trans, root,
1439 path, location.objectid);
1440 BUG_ON(ret);
1441 btrfs_inc_nlink(inode);
1442 ret = btrfs_unlink_inode(trans, root, dir, inode,
1443 name, name_len);
1444 BUG_ON(ret);
1445 kfree(name);
1446 iput(inode);
1447
1448 /* there might still be more names under this key
1449 * check and repeat if required
1450 */
1451 ret = btrfs_search_slot(NULL, root, dir_key, path,
1452 0, 0);
1453 if (ret == 0)
1454 goto again;
1455 ret = 0;
1456 goto out;
1457 }
1458 btrfs_release_path(log, log_path);
1459 kfree(name);
1460
1461 ptr = (unsigned long)(di + 1);
1462 ptr += name_len;
1463 }
1464 ret = 0;
1465out:
1466 btrfs_release_path(root, path);
1467 btrfs_release_path(log, log_path);
1468 return ret;
1469}
1470
1471/*
1472 * deletion replay happens before we copy any new directory items
1473 * out of the log or out of backreferences from inodes. It
1474 * scans the log to find ranges of keys that log is authoritative for,
1475 * and then scans the directory to find items in those ranges that are
1476 * not present in the log.
1477 *
1478 * Anything we don't find in the log is unlinked and removed from the
1479 * directory.
1480 */
1481static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root,
1483 struct btrfs_root *log,
1484 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001485 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001486{
1487 u64 range_start;
1488 u64 range_end;
1489 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1490 int ret = 0;
1491 struct btrfs_key dir_key;
1492 struct btrfs_key found_key;
1493 struct btrfs_path *log_path;
1494 struct inode *dir;
1495
1496 dir_key.objectid = dirid;
1497 dir_key.type = BTRFS_DIR_ITEM_KEY;
1498 log_path = btrfs_alloc_path();
1499 if (!log_path)
1500 return -ENOMEM;
1501
1502 dir = read_one_inode(root, dirid);
1503 /* it isn't an error if the inode isn't there, that can happen
1504 * because we replay the deletes before we copy in the inode item
1505 * from the log
1506 */
1507 if (!dir) {
1508 btrfs_free_path(log_path);
1509 return 0;
1510 }
1511again:
1512 range_start = 0;
1513 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001514 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001515 if (del_all)
1516 range_end = (u64)-1;
1517 else {
1518 ret = find_dir_range(log, path, dirid, key_type,
1519 &range_start, &range_end);
1520 if (ret != 0)
1521 break;
1522 }
Chris Masone02119d2008-09-05 16:13:11 -04001523
1524 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001525 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001526 int nritems;
1527 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1528 0, 0);
1529 if (ret < 0)
1530 goto out;
1531
1532 nritems = btrfs_header_nritems(path->nodes[0]);
1533 if (path->slots[0] >= nritems) {
1534 ret = btrfs_next_leaf(root, path);
1535 if (ret)
1536 break;
1537 }
1538 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1539 path->slots[0]);
1540 if (found_key.objectid != dirid ||
1541 found_key.type != dir_key.type)
1542 goto next_type;
1543
1544 if (found_key.offset > range_end)
1545 break;
1546
1547 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001548 log_path, dir,
1549 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001550 BUG_ON(ret);
1551 if (found_key.offset == (u64)-1)
1552 break;
1553 dir_key.offset = found_key.offset + 1;
1554 }
1555 btrfs_release_path(root, path);
1556 if (range_end == (u64)-1)
1557 break;
1558 range_start = range_end + 1;
1559 }
1560
1561next_type:
1562 ret = 0;
1563 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1564 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1565 dir_key.type = BTRFS_DIR_INDEX_KEY;
1566 btrfs_release_path(root, path);
1567 goto again;
1568 }
1569out:
1570 btrfs_release_path(root, path);
1571 btrfs_free_path(log_path);
1572 iput(dir);
1573 return ret;
1574}
1575
1576/*
1577 * the process_func used to replay items from the log tree. This
1578 * gets called in two different stages. The first stage just looks
1579 * for inodes and makes sure they are all copied into the subvolume.
1580 *
1581 * The second stage copies all the other item types from the log into
1582 * the subvolume. The two stage approach is slower, but gets rid of
1583 * lots of complexity around inodes referencing other inodes that exist
1584 * only in the log (references come from either directory items or inode
1585 * back refs).
1586 */
1587static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1588 struct walk_control *wc, u64 gen)
1589{
1590 int nritems;
1591 struct btrfs_path *path;
1592 struct btrfs_root *root = wc->replay_dest;
1593 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001594 int level;
1595 int i;
1596 int ret;
1597
1598 btrfs_read_buffer(eb, gen);
1599
1600 level = btrfs_header_level(eb);
1601
1602 if (level != 0)
1603 return 0;
1604
1605 path = btrfs_alloc_path();
1606 BUG_ON(!path);
1607
1608 nritems = btrfs_header_nritems(eb);
1609 for (i = 0; i < nritems; i++) {
1610 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001611
1612 /* inode keys are done during the first stage */
1613 if (key.type == BTRFS_INODE_ITEM_KEY &&
1614 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001615 struct btrfs_inode_item *inode_item;
1616 u32 mode;
1617
1618 inode_item = btrfs_item_ptr(eb, i,
1619 struct btrfs_inode_item);
1620 mode = btrfs_inode_mode(eb, inode_item);
1621 if (S_ISDIR(mode)) {
1622 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001623 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001624 BUG_ON(ret);
1625 }
1626 ret = overwrite_item(wc->trans, root, path,
1627 eb, i, &key);
1628 BUG_ON(ret);
1629
Yan, Zhengc71bf092009-11-12 09:34:40 +00001630 /* for regular files, make sure corresponding
1631 * orhpan item exist. extents past the new EOF
1632 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001633 */
1634 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001635 ret = insert_orphan_item(wc->trans, root,
1636 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001637 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001638 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001639
Chris Masone02119d2008-09-05 16:13:11 -04001640 ret = link_to_fixup_dir(wc->trans, root,
1641 path, key.objectid);
1642 BUG_ON(ret);
1643 }
1644 if (wc->stage < LOG_WALK_REPLAY_ALL)
1645 continue;
1646
1647 /* these keys are simply copied */
1648 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1649 ret = overwrite_item(wc->trans, root, path,
1650 eb, i, &key);
1651 BUG_ON(ret);
1652 } else if (key.type == BTRFS_INODE_REF_KEY) {
1653 ret = add_inode_ref(wc->trans, root, log, path,
1654 eb, i, &key);
1655 BUG_ON(ret && ret != -ENOENT);
1656 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1657 ret = replay_one_extent(wc->trans, root, path,
1658 eb, i, &key);
1659 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001660 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1661 key.type == BTRFS_DIR_INDEX_KEY) {
1662 ret = replay_one_dir_item(wc->trans, root, path,
1663 eb, i, &key);
1664 BUG_ON(ret);
1665 }
1666 }
1667 btrfs_free_path(path);
1668 return 0;
1669}
1670
Chris Masond3977122009-01-05 21:25:51 -05001671static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001672 struct btrfs_root *root,
1673 struct btrfs_path *path, int *level,
1674 struct walk_control *wc)
1675{
1676 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001677 u64 bytenr;
1678 u64 ptr_gen;
1679 struct extent_buffer *next;
1680 struct extent_buffer *cur;
1681 struct extent_buffer *parent;
1682 u32 blocksize;
1683 int ret = 0;
1684
1685 WARN_ON(*level < 0);
1686 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1687
Chris Masond3977122009-01-05 21:25:51 -05001688 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001689 WARN_ON(*level < 0);
1690 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1691 cur = path->nodes[*level];
1692
1693 if (btrfs_header_level(cur) != *level)
1694 WARN_ON(1);
1695
1696 if (path->slots[*level] >=
1697 btrfs_header_nritems(cur))
1698 break;
1699
1700 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1701 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1702 blocksize = btrfs_level_size(root, *level - 1);
1703
1704 parent = path->nodes[*level];
1705 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001706
1707 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00001708 if (!next)
1709 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001710
Chris Masone02119d2008-09-05 16:13:11 -04001711 if (*level == 1) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001712 wc->process_func(root, next, wc, ptr_gen);
1713
Chris Masone02119d2008-09-05 16:13:11 -04001714 path->slots[*level]++;
1715 if (wc->free) {
1716 btrfs_read_buffer(next, ptr_gen);
1717
1718 btrfs_tree_lock(next);
1719 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001720 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001721 btrfs_wait_tree_block_writeback(next);
1722 btrfs_tree_unlock(next);
1723
Chris Masone02119d2008-09-05 16:13:11 -04001724 WARN_ON(root_owner !=
1725 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001726 ret = btrfs_free_reserved_extent(root,
1727 bytenr, blocksize);
Chris Masone02119d2008-09-05 16:13:11 -04001728 BUG_ON(ret);
1729 }
1730 free_extent_buffer(next);
1731 continue;
1732 }
1733 btrfs_read_buffer(next, ptr_gen);
1734
1735 WARN_ON(*level <= 0);
1736 if (path->nodes[*level-1])
1737 free_extent_buffer(path->nodes[*level-1]);
1738 path->nodes[*level-1] = next;
1739 *level = btrfs_header_level(next);
1740 path->slots[*level] = 0;
1741 cond_resched();
1742 }
1743 WARN_ON(*level < 0);
1744 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1745
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001746 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04001747
1748 cond_resched();
1749 return 0;
1750}
1751
Chris Masond3977122009-01-05 21:25:51 -05001752static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001753 struct btrfs_root *root,
1754 struct btrfs_path *path, int *level,
1755 struct walk_control *wc)
1756{
1757 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001758 int i;
1759 int slot;
1760 int ret;
1761
Chris Masond3977122009-01-05 21:25:51 -05001762 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04001763 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001764 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04001765 path->slots[i]++;
1766 *level = i;
1767 WARN_ON(*level == 0);
1768 return 0;
1769 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001770 struct extent_buffer *parent;
1771 if (path->nodes[*level] == root->node)
1772 parent = path->nodes[*level];
1773 else
1774 parent = path->nodes[*level + 1];
1775
1776 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001777 wc->process_func(root, path->nodes[*level], wc,
1778 btrfs_header_generation(path->nodes[*level]));
1779 if (wc->free) {
1780 struct extent_buffer *next;
1781
1782 next = path->nodes[*level];
1783
1784 btrfs_tree_lock(next);
1785 clean_tree_block(trans, root, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001786 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001787 btrfs_wait_tree_block_writeback(next);
1788 btrfs_tree_unlock(next);
1789
Chris Masone02119d2008-09-05 16:13:11 -04001790 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001791 ret = btrfs_free_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04001792 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04001793 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04001794 BUG_ON(ret);
1795 }
1796 free_extent_buffer(path->nodes[*level]);
1797 path->nodes[*level] = NULL;
1798 *level = i + 1;
1799 }
1800 }
1801 return 1;
1802}
1803
1804/*
1805 * drop the reference count on the tree rooted at 'snap'. This traverses
1806 * the tree freeing any blocks that have a ref count of zero after being
1807 * decremented.
1808 */
1809static int walk_log_tree(struct btrfs_trans_handle *trans,
1810 struct btrfs_root *log, struct walk_control *wc)
1811{
1812 int ret = 0;
1813 int wret;
1814 int level;
1815 struct btrfs_path *path;
1816 int i;
1817 int orig_level;
1818
1819 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001820 if (!path)
1821 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001822
1823 level = btrfs_header_level(log->node);
1824 orig_level = level;
1825 path->nodes[level] = log->node;
1826 extent_buffer_get(log->node);
1827 path->slots[level] = 0;
1828
Chris Masond3977122009-01-05 21:25:51 -05001829 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001830 wret = walk_down_log_tree(trans, log, path, &level, wc);
1831 if (wret > 0)
1832 break;
1833 if (wret < 0)
1834 ret = wret;
1835
1836 wret = walk_up_log_tree(trans, log, path, &level, wc);
1837 if (wret > 0)
1838 break;
1839 if (wret < 0)
1840 ret = wret;
1841 }
1842
1843 /* was the root node processed? if not, catch it here */
1844 if (path->nodes[orig_level]) {
1845 wc->process_func(log, path->nodes[orig_level], wc,
1846 btrfs_header_generation(path->nodes[orig_level]));
1847 if (wc->free) {
1848 struct extent_buffer *next;
1849
1850 next = path->nodes[orig_level];
1851
1852 btrfs_tree_lock(next);
1853 clean_tree_block(trans, log, next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001854 btrfs_set_lock_blocking(next);
Chris Masone02119d2008-09-05 16:13:11 -04001855 btrfs_wait_tree_block_writeback(next);
1856 btrfs_tree_unlock(next);
1857
Chris Masone02119d2008-09-05 16:13:11 -04001858 WARN_ON(log->root_key.objectid !=
1859 BTRFS_TREE_LOG_OBJECTID);
Chris Masond00aff02008-09-11 15:54:42 -04001860 ret = btrfs_free_reserved_extent(log, next->start,
1861 next->len);
Chris Masone02119d2008-09-05 16:13:11 -04001862 BUG_ON(ret);
1863 }
1864 }
1865
1866 for (i = 0; i <= orig_level; i++) {
1867 if (path->nodes[i]) {
1868 free_extent_buffer(path->nodes[i]);
1869 path->nodes[i] = NULL;
1870 }
1871 }
1872 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001873 return ret;
1874}
1875
Yan Zheng7237f182009-01-21 12:54:03 -05001876/*
1877 * helper function to update the item for a given subvolumes log root
1878 * in the tree of log roots
1879 */
1880static int update_log_root(struct btrfs_trans_handle *trans,
1881 struct btrfs_root *log)
1882{
1883 int ret;
1884
1885 if (log->log_transid == 1) {
1886 /* insert root item on the first sync */
1887 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1888 &log->root_key, &log->root_item);
1889 } else {
1890 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1891 &log->root_key, &log->root_item);
1892 }
1893 return ret;
1894}
1895
Chris Mason12fcfd22009-03-24 10:24:20 -04001896static int wait_log_commit(struct btrfs_trans_handle *trans,
1897 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04001898{
1899 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05001900 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04001901
Yan Zheng7237f182009-01-21 12:54:03 -05001902 /*
1903 * we only allow two pending log transactions at a time,
1904 * so we know that if ours is more than 2 older than the
1905 * current transaction, we're done
1906 */
Chris Masone02119d2008-09-05 16:13:11 -04001907 do {
Yan Zheng7237f182009-01-21 12:54:03 -05001908 prepare_to_wait(&root->log_commit_wait[index],
1909 &wait, TASK_UNINTERRUPTIBLE);
1910 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001911
1912 if (root->fs_info->last_trans_log_full_commit !=
1913 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05001914 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04001915 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04001916
Yan Zheng7237f182009-01-21 12:54:03 -05001917 finish_wait(&root->log_commit_wait[index], &wait);
1918 mutex_lock(&root->log_mutex);
1919 } while (root->log_transid < transid + 2 &&
1920 atomic_read(&root->log_commit[index]));
1921 return 0;
1922}
1923
Chris Mason12fcfd22009-03-24 10:24:20 -04001924static int wait_for_writer(struct btrfs_trans_handle *trans,
1925 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05001926{
1927 DEFINE_WAIT(wait);
1928 while (atomic_read(&root->log_writers)) {
1929 prepare_to_wait(&root->log_writer_wait,
1930 &wait, TASK_UNINTERRUPTIBLE);
1931 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001932 if (root->fs_info->last_trans_log_full_commit !=
1933 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05001934 schedule();
1935 mutex_lock(&root->log_mutex);
1936 finish_wait(&root->log_writer_wait, &wait);
1937 }
Chris Masone02119d2008-09-05 16:13:11 -04001938 return 0;
1939}
1940
1941/*
1942 * btrfs_sync_log does sends a given tree log down to the disk and
1943 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04001944 * you know that any inodes previously logged are safely on disk only
1945 * if it returns 0.
1946 *
1947 * Any other return value means you need to call btrfs_commit_transaction.
1948 * Some of the edge cases for fsyncing directories that have had unlinks
1949 * or renames done in the past mean that sometimes the only safe
1950 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
1951 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04001952 */
1953int btrfs_sync_log(struct btrfs_trans_handle *trans,
1954 struct btrfs_root *root)
1955{
Yan Zheng7237f182009-01-21 12:54:03 -05001956 int index1;
1957 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001958 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04001959 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04001960 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05001961 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001962 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001963
Yan Zheng7237f182009-01-21 12:54:03 -05001964 mutex_lock(&root->log_mutex);
1965 index1 = root->log_transid % 2;
1966 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001967 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05001968 mutex_unlock(&root->log_mutex);
1969 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04001970 }
Yan Zheng7237f182009-01-21 12:54:03 -05001971 atomic_set(&root->log_commit[index1], 1);
1972
1973 /* wait for previous tree log sync to complete */
1974 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04001975 wait_log_commit(trans, root, root->log_transid - 1);
Chris Masone02119d2008-09-05 16:13:11 -04001976
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001977 while (1) {
Yan Zheng7237f182009-01-21 12:54:03 -05001978 unsigned long batch = root->log_batch;
Yan, Zheng86df7eb2009-10-14 09:24:59 -04001979 if (root->log_multiple_pids) {
1980 mutex_unlock(&root->log_mutex);
1981 schedule_timeout_uninterruptible(1);
1982 mutex_lock(&root->log_mutex);
1983 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001984 wait_for_writer(trans, root);
Yan Zheng7237f182009-01-21 12:54:03 -05001985 if (batch == root->log_batch)
Chris Masone02119d2008-09-05 16:13:11 -04001986 break;
1987 }
Chris Masond0c803c2008-09-11 16:17:57 -04001988
Chris Mason12fcfd22009-03-24 10:24:20 -04001989 /* bail out if we need to do a full commit */
1990 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
1991 ret = -EAGAIN;
1992 mutex_unlock(&root->log_mutex);
1993 goto out;
1994 }
1995
Yan, Zheng8cef4e12009-11-12 09:33:26 +00001996 log_transid = root->log_transid;
1997 if (log_transid % 2 == 0)
1998 mark = EXTENT_DIRTY;
1999 else
2000 mark = EXTENT_NEW;
2001
Chris Mason690587d2009-10-13 13:29:19 -04002002 /* we start IO on all the marked extents here, but we don't actually
2003 * wait for them until later.
2004 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002005 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002006 BUG_ON(ret);
Yan Zheng7237f182009-01-21 12:54:03 -05002007
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002009
2010 root->log_batch = 0;
2011 root->log_transid++;
2012 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002013 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002014 smp_mb();
2015 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002016 * IO has been started, blocks of the log tree have WRITTEN flag set
2017 * in their headers. new modifications of the log will be written to
2018 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002019 */
2020 mutex_unlock(&root->log_mutex);
2021
2022 mutex_lock(&log_root_tree->log_mutex);
2023 log_root_tree->log_batch++;
2024 atomic_inc(&log_root_tree->log_writers);
2025 mutex_unlock(&log_root_tree->log_mutex);
2026
2027 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002028
2029 mutex_lock(&log_root_tree->log_mutex);
2030 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2031 smp_mb();
2032 if (waitqueue_active(&log_root_tree->log_writer_wait))
2033 wake_up(&log_root_tree->log_writer_wait);
2034 }
2035
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002036 if (ret) {
2037 BUG_ON(ret != -ENOSPC);
2038 root->fs_info->last_trans_log_full_commit = trans->transid;
2039 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2040 mutex_unlock(&log_root_tree->log_mutex);
2041 ret = -EAGAIN;
2042 goto out;
2043 }
2044
Yan Zheng7237f182009-01-21 12:54:03 -05002045 index2 = log_root_tree->log_transid % 2;
2046 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002047 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002048 wait_log_commit(trans, log_root_tree,
2049 log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002050 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002051 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002052 goto out;
2053 }
2054 atomic_set(&log_root_tree->log_commit[index2], 1);
2055
Chris Mason12fcfd22009-03-24 10:24:20 -04002056 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2057 wait_log_commit(trans, log_root_tree,
2058 log_root_tree->log_transid - 1);
2059 }
Yan Zheng7237f182009-01-21 12:54:03 -05002060
Chris Mason12fcfd22009-03-24 10:24:20 -04002061 wait_for_writer(trans, log_root_tree);
2062
2063 /*
2064 * now that we've moved on to the tree of log tree roots,
2065 * check the full commit flag again
2066 */
2067 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002068 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002069 mutex_unlock(&log_root_tree->log_mutex);
2070 ret = -EAGAIN;
2071 goto out_wake_log_root;
2072 }
Yan Zheng7237f182009-01-21 12:54:03 -05002073
2074 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002075 &log_root_tree->dirty_log_pages,
2076 EXTENT_DIRTY | EXTENT_NEW);
Chris Masone02119d2008-09-05 16:13:11 -04002077 BUG_ON(ret);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002078 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002079
2080 btrfs_set_super_log_root(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002081 log_root_tree->node->start);
Chris Masone02119d2008-09-05 16:13:11 -04002082 btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002083 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002084
Yan Zheng7237f182009-01-21 12:54:03 -05002085 log_root_tree->log_batch = 0;
2086 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002087 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002088
2089 mutex_unlock(&log_root_tree->log_mutex);
2090
2091 /*
2092 * nobody else is going to jump in and write the the ctree
2093 * super here because the log_commit atomic below is protecting
2094 * us. We must be called with a transaction handle pinning
2095 * the running transaction open, so a full commit can't hop
2096 * in and cause problems either.
2097 */
Chris Mason47226072009-10-13 12:55:09 -04002098 write_ctree_super(trans, root->fs_info->tree_root, 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002099 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002100
Chris Mason257c62e2009-10-13 13:21:08 -04002101 mutex_lock(&root->log_mutex);
2102 if (root->last_log_commit < log_transid)
2103 root->last_log_commit = log_transid;
2104 mutex_unlock(&root->log_mutex);
2105
Chris Mason12fcfd22009-03-24 10:24:20 -04002106out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002107 atomic_set(&log_root_tree->log_commit[index2], 0);
2108 smp_mb();
2109 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2110 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002111out:
Yan Zheng7237f182009-01-21 12:54:03 -05002112 atomic_set(&root->log_commit[index1], 0);
2113 smp_mb();
2114 if (waitqueue_active(&root->log_commit_wait[index1]))
2115 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002116 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002117}
2118
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002119static void free_log_tree(struct btrfs_trans_handle *trans,
2120 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002121{
2122 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002123 u64 start;
2124 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002125 struct walk_control wc = {
2126 .free = 1,
2127 .process_func = process_one_buffer
2128 };
2129
Chris Masone02119d2008-09-05 16:13:11 -04002130 ret = walk_log_tree(trans, log, &wc);
2131 BUG_ON(ret);
2132
Chris Masond3977122009-01-05 21:25:51 -05002133 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002134 ret = find_first_extent_bit(&log->dirty_log_pages,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002135 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04002136 if (ret)
2137 break;
2138
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002139 clear_extent_bits(&log->dirty_log_pages, start, end,
2140 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002141 }
2142
Yan Zheng7237f182009-01-21 12:54:03 -05002143 free_extent_buffer(log->node);
2144 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002145}
2146
2147/*
2148 * free all the extents used by the tree log. This should be called
2149 * at commit time of the full transaction
2150 */
2151int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2152{
2153 if (root->log_root) {
2154 free_log_tree(trans, root->log_root);
2155 root->log_root = NULL;
2156 }
2157 return 0;
2158}
2159
2160int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2161 struct btrfs_fs_info *fs_info)
2162{
2163 if (fs_info->log_root_tree) {
2164 free_log_tree(trans, fs_info->log_root_tree);
2165 fs_info->log_root_tree = NULL;
2166 }
Chris Masone02119d2008-09-05 16:13:11 -04002167 return 0;
2168}
2169
2170/*
Chris Masone02119d2008-09-05 16:13:11 -04002171 * If both a file and directory are logged, and unlinks or renames are
2172 * mixed in, we have a few interesting corners:
2173 *
2174 * create file X in dir Y
2175 * link file X to X.link in dir Y
2176 * fsync file X
2177 * unlink file X but leave X.link
2178 * fsync dir Y
2179 *
2180 * After a crash we would expect only X.link to exist. But file X
2181 * didn't get fsync'd again so the log has back refs for X and X.link.
2182 *
2183 * We solve this by removing directory entries and inode backrefs from the
2184 * log when a file that was logged in the current transaction is
2185 * unlinked. Any later fsync will include the updated log entries, and
2186 * we'll be able to reconstruct the proper directory items from backrefs.
2187 *
2188 * This optimizations allows us to avoid relogging the entire inode
2189 * or the entire directory.
2190 */
2191int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2192 struct btrfs_root *root,
2193 const char *name, int name_len,
2194 struct inode *dir, u64 index)
2195{
2196 struct btrfs_root *log;
2197 struct btrfs_dir_item *di;
2198 struct btrfs_path *path;
2199 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002200 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002201 int bytes_del = 0;
2202
Chris Mason3a5f1d42008-09-11 15:53:37 -04002203 if (BTRFS_I(dir)->logged_trans < trans->transid)
2204 return 0;
2205
Chris Masone02119d2008-09-05 16:13:11 -04002206 ret = join_running_log_trans(root);
2207 if (ret)
2208 return 0;
2209
2210 mutex_lock(&BTRFS_I(dir)->log_mutex);
2211
2212 log = root->log_root;
2213 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002214 if (!path) {
2215 err = -ENOMEM;
2216 goto out_unlock;
2217 }
liubo2a29edc2011-01-26 06:22:08 +00002218
Chris Masone02119d2008-09-05 16:13:11 -04002219 di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
2220 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002221 if (IS_ERR(di)) {
2222 err = PTR_ERR(di);
2223 goto fail;
2224 }
2225 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002226 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2227 bytes_del += name_len;
2228 BUG_ON(ret);
2229 }
2230 btrfs_release_path(log, path);
2231 di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
2232 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002233 if (IS_ERR(di)) {
2234 err = PTR_ERR(di);
2235 goto fail;
2236 }
2237 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002238 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2239 bytes_del += name_len;
2240 BUG_ON(ret);
2241 }
2242
2243 /* update the directory size in the log to reflect the names
2244 * we have removed
2245 */
2246 if (bytes_del) {
2247 struct btrfs_key key;
2248
2249 key.objectid = dir->i_ino;
2250 key.offset = 0;
2251 key.type = BTRFS_INODE_ITEM_KEY;
2252 btrfs_release_path(log, path);
2253
2254 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002255 if (ret < 0) {
2256 err = ret;
2257 goto fail;
2258 }
Chris Masone02119d2008-09-05 16:13:11 -04002259 if (ret == 0) {
2260 struct btrfs_inode_item *item;
2261 u64 i_size;
2262
2263 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2264 struct btrfs_inode_item);
2265 i_size = btrfs_inode_size(path->nodes[0], item);
2266 if (i_size > bytes_del)
2267 i_size -= bytes_del;
2268 else
2269 i_size = 0;
2270 btrfs_set_inode_size(path->nodes[0], item, i_size);
2271 btrfs_mark_buffer_dirty(path->nodes[0]);
2272 } else
2273 ret = 0;
2274 btrfs_release_path(log, path);
2275 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002276fail:
Chris Masone02119d2008-09-05 16:13:11 -04002277 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002278out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002279 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002280 if (ret == -ENOSPC) {
2281 root->fs_info->last_trans_log_full_commit = trans->transid;
2282 ret = 0;
2283 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002284 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002285
Andi Kleen411fc6b2010-10-29 15:14:31 -04002286 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002287}
2288
2289/* see comments for btrfs_del_dir_entries_in_log */
2290int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2291 struct btrfs_root *root,
2292 const char *name, int name_len,
2293 struct inode *inode, u64 dirid)
2294{
2295 struct btrfs_root *log;
2296 u64 index;
2297 int ret;
2298
Chris Mason3a5f1d42008-09-11 15:53:37 -04002299 if (BTRFS_I(inode)->logged_trans < trans->transid)
2300 return 0;
2301
Chris Masone02119d2008-09-05 16:13:11 -04002302 ret = join_running_log_trans(root);
2303 if (ret)
2304 return 0;
2305 log = root->log_root;
2306 mutex_lock(&BTRFS_I(inode)->log_mutex);
2307
2308 ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
2309 dirid, &index);
2310 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002311 if (ret == -ENOSPC) {
2312 root->fs_info->last_trans_log_full_commit = trans->transid;
2313 ret = 0;
2314 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002315 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002316
Chris Masone02119d2008-09-05 16:13:11 -04002317 return ret;
2318}
2319
2320/*
2321 * creates a range item in the log for 'dirid'. first_offset and
2322 * last_offset tell us which parts of the key space the log should
2323 * be considered authoritative for.
2324 */
2325static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2326 struct btrfs_root *log,
2327 struct btrfs_path *path,
2328 int key_type, u64 dirid,
2329 u64 first_offset, u64 last_offset)
2330{
2331 int ret;
2332 struct btrfs_key key;
2333 struct btrfs_dir_log_item *item;
2334
2335 key.objectid = dirid;
2336 key.offset = first_offset;
2337 if (key_type == BTRFS_DIR_ITEM_KEY)
2338 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2339 else
2340 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2341 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002342 if (ret)
2343 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002344
2345 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2346 struct btrfs_dir_log_item);
2347 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2348 btrfs_mark_buffer_dirty(path->nodes[0]);
2349 btrfs_release_path(log, path);
2350 return 0;
2351}
2352
2353/*
2354 * log all the items included in the current transaction for a given
2355 * directory. This also creates the range items in the log tree required
2356 * to replay anything deleted before the fsync
2357 */
2358static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2359 struct btrfs_root *root, struct inode *inode,
2360 struct btrfs_path *path,
2361 struct btrfs_path *dst_path, int key_type,
2362 u64 min_offset, u64 *last_offset_ret)
2363{
2364 struct btrfs_key min_key;
2365 struct btrfs_key max_key;
2366 struct btrfs_root *log = root->log_root;
2367 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002368 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002369 int ret;
2370 int i;
2371 int nritems;
2372 u64 first_offset = min_offset;
2373 u64 last_offset = (u64)-1;
2374
2375 log = root->log_root;
2376 max_key.objectid = inode->i_ino;
2377 max_key.offset = (u64)-1;
2378 max_key.type = key_type;
2379
2380 min_key.objectid = inode->i_ino;
2381 min_key.type = key_type;
2382 min_key.offset = min_offset;
2383
2384 path->keep_locks = 1;
2385
2386 ret = btrfs_search_forward(root, &min_key, &max_key,
2387 path, 0, trans->transid);
2388
2389 /*
2390 * we didn't find anything from this transaction, see if there
2391 * is anything at all
2392 */
2393 if (ret != 0 || min_key.objectid != inode->i_ino ||
2394 min_key.type != key_type) {
2395 min_key.objectid = inode->i_ino;
2396 min_key.type = key_type;
2397 min_key.offset = (u64)-1;
2398 btrfs_release_path(root, path);
2399 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2400 if (ret < 0) {
2401 btrfs_release_path(root, path);
2402 return ret;
2403 }
2404 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2405
2406 /* if ret == 0 there are items for this type,
2407 * create a range to tell us the last key of this type.
2408 * otherwise, there are no items in this directory after
2409 * *min_offset, and we create a range to indicate that.
2410 */
2411 if (ret == 0) {
2412 struct btrfs_key tmp;
2413 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2414 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002415 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002416 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002417 }
2418 goto done;
2419 }
2420
2421 /* go backward to find any previous key */
2422 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2423 if (ret == 0) {
2424 struct btrfs_key tmp;
2425 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2426 if (key_type == tmp.type) {
2427 first_offset = tmp.offset;
2428 ret = overwrite_item(trans, log, dst_path,
2429 path->nodes[0], path->slots[0],
2430 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002431 if (ret) {
2432 err = ret;
2433 goto done;
2434 }
Chris Masone02119d2008-09-05 16:13:11 -04002435 }
2436 }
2437 btrfs_release_path(root, path);
2438
2439 /* find the first key from this transaction again */
2440 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2441 if (ret != 0) {
2442 WARN_ON(1);
2443 goto done;
2444 }
2445
2446 /*
2447 * we have a block from this transaction, log every item in it
2448 * from our directory
2449 */
Chris Masond3977122009-01-05 21:25:51 -05002450 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002451 struct btrfs_key tmp;
2452 src = path->nodes[0];
2453 nritems = btrfs_header_nritems(src);
2454 for (i = path->slots[0]; i < nritems; i++) {
2455 btrfs_item_key_to_cpu(src, &min_key, i);
2456
2457 if (min_key.objectid != inode->i_ino ||
2458 min_key.type != key_type)
2459 goto done;
2460 ret = overwrite_item(trans, log, dst_path, src, i,
2461 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002462 if (ret) {
2463 err = ret;
2464 goto done;
2465 }
Chris Masone02119d2008-09-05 16:13:11 -04002466 }
2467 path->slots[0] = nritems;
2468
2469 /*
2470 * look ahead to the next item and see if it is also
2471 * from this directory and from this transaction
2472 */
2473 ret = btrfs_next_leaf(root, path);
2474 if (ret == 1) {
2475 last_offset = (u64)-1;
2476 goto done;
2477 }
2478 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2479 if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
2480 last_offset = (u64)-1;
2481 goto done;
2482 }
2483 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2484 ret = overwrite_item(trans, log, dst_path,
2485 path->nodes[0], path->slots[0],
2486 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002487 if (ret)
2488 err = ret;
2489 else
2490 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002491 goto done;
2492 }
2493 }
2494done:
Chris Masone02119d2008-09-05 16:13:11 -04002495 btrfs_release_path(root, path);
2496 btrfs_release_path(log, dst_path);
2497
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002498 if (err == 0) {
2499 *last_offset_ret = last_offset;
2500 /*
2501 * insert the log range keys to indicate where the log
2502 * is valid
2503 */
2504 ret = insert_dir_log_key(trans, log, path, key_type,
2505 inode->i_ino, first_offset,
2506 last_offset);
2507 if (ret)
2508 err = ret;
2509 }
2510 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002511}
2512
2513/*
2514 * logging directories is very similar to logging inodes, We find all the items
2515 * from the current transaction and write them to the log.
2516 *
2517 * The recovery code scans the directory in the subvolume, and if it finds a
2518 * key in the range logged that is not present in the log tree, then it means
2519 * that dir entry was unlinked during the transaction.
2520 *
2521 * In order for that scan to work, we must include one key smaller than
2522 * the smallest logged by this transaction and one key larger than the largest
2523 * key logged by this transaction.
2524 */
2525static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2526 struct btrfs_root *root, struct inode *inode,
2527 struct btrfs_path *path,
2528 struct btrfs_path *dst_path)
2529{
2530 u64 min_key;
2531 u64 max_key;
2532 int ret;
2533 int key_type = BTRFS_DIR_ITEM_KEY;
2534
2535again:
2536 min_key = 0;
2537 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002538 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002539 ret = log_dir_items(trans, root, inode, path,
2540 dst_path, key_type, min_key,
2541 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002542 if (ret)
2543 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002544 if (max_key == (u64)-1)
2545 break;
2546 min_key = max_key + 1;
2547 }
2548
2549 if (key_type == BTRFS_DIR_ITEM_KEY) {
2550 key_type = BTRFS_DIR_INDEX_KEY;
2551 goto again;
2552 }
2553 return 0;
2554}
2555
2556/*
2557 * a helper function to drop items from the log before we relog an
2558 * inode. max_key_type indicates the highest item type to remove.
2559 * This cannot be run for file data extents because it does not
2560 * free the extents they point to.
2561 */
2562static int drop_objectid_items(struct btrfs_trans_handle *trans,
2563 struct btrfs_root *log,
2564 struct btrfs_path *path,
2565 u64 objectid, int max_key_type)
2566{
2567 int ret;
2568 struct btrfs_key key;
2569 struct btrfs_key found_key;
2570
2571 key.objectid = objectid;
2572 key.type = max_key_type;
2573 key.offset = (u64)-1;
2574
Chris Masond3977122009-01-05 21:25:51 -05002575 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002576 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002577 BUG_ON(ret == 0);
2578 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002579 break;
2580
2581 if (path->slots[0] == 0)
2582 break;
2583
2584 path->slots[0]--;
2585 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2586 path->slots[0]);
2587
2588 if (found_key.objectid != objectid)
2589 break;
2590
2591 ret = btrfs_del_item(trans, log, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002592 if (ret)
2593 break;
Chris Masone02119d2008-09-05 16:13:11 -04002594 btrfs_release_path(log, path);
2595 }
2596 btrfs_release_path(log, path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002597 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002598}
2599
Chris Mason31ff1cd2008-09-11 16:17:57 -04002600static noinline int copy_items(struct btrfs_trans_handle *trans,
2601 struct btrfs_root *log,
2602 struct btrfs_path *dst_path,
2603 struct extent_buffer *src,
2604 int start_slot, int nr, int inode_only)
2605{
2606 unsigned long src_offset;
2607 unsigned long dst_offset;
2608 struct btrfs_file_extent_item *extent;
2609 struct btrfs_inode_item *inode_item;
2610 int ret;
2611 struct btrfs_key *ins_keys;
2612 u32 *ins_sizes;
2613 char *ins_data;
2614 int i;
Chris Masond20f7042008-12-08 16:58:54 -05002615 struct list_head ordered_sums;
2616
2617 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002618
2619 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2620 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00002621 if (!ins_data)
2622 return -ENOMEM;
2623
Chris Mason31ff1cd2008-09-11 16:17:57 -04002624 ins_sizes = (u32 *)ins_data;
2625 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2626
2627 for (i = 0; i < nr; i++) {
2628 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2629 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2630 }
2631 ret = btrfs_insert_empty_items(trans, log, dst_path,
2632 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002633 if (ret) {
2634 kfree(ins_data);
2635 return ret;
2636 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002637
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002638 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002639 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2640 dst_path->slots[0]);
2641
2642 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2643
2644 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2645 src_offset, ins_sizes[i]);
2646
2647 if (inode_only == LOG_INODE_EXISTS &&
2648 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2649 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2650 dst_path->slots[0],
2651 struct btrfs_inode_item);
2652 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2653
2654 /* set the generation to zero so the recover code
2655 * can tell the difference between an logging
2656 * just to say 'this inode exists' and a logging
2657 * to say 'update this inode with these values'
2658 */
2659 btrfs_set_inode_generation(dst_path->nodes[0],
2660 inode_item, 0);
2661 }
2662 /* take a reference on file data extents so that truncates
2663 * or deletes of this inode don't have to relog the inode
2664 * again
2665 */
2666 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2667 int found_type;
2668 extent = btrfs_item_ptr(src, start_slot + i,
2669 struct btrfs_file_extent_item);
2670
2671 found_type = btrfs_file_extent_type(src, extent);
Yan Zhengd899e052008-10-30 14:25:28 -04002672 if (found_type == BTRFS_FILE_EXTENT_REG ||
2673 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002674 u64 ds, dl, cs, cl;
2675 ds = btrfs_file_extent_disk_bytenr(src,
2676 extent);
2677 /* ds == 0 is a hole */
2678 if (ds == 0)
2679 continue;
2680
2681 dl = btrfs_file_extent_disk_num_bytes(src,
2682 extent);
2683 cs = btrfs_file_extent_offset(src, extent);
2684 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07002685 extent);
Chris Mason580afd72008-12-08 19:15:39 -05002686 if (btrfs_file_extent_compression(src,
2687 extent)) {
2688 cs = 0;
2689 cl = dl;
2690 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002691
2692 ret = btrfs_lookup_csums_range(
2693 log->fs_info->csum_root,
2694 ds + cs, ds + cs + cl - 1,
2695 &ordered_sums);
2696 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002697 }
2698 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002699 }
2700
2701 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2702 btrfs_release_path(log, dst_path);
2703 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05002704
2705 /*
2706 * we have to do this after the loop above to avoid changing the
2707 * log tree while trying to change the log tree.
2708 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002709 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002710 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05002711 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2712 struct btrfs_ordered_sum,
2713 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002714 if (!ret)
2715 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05002716 list_del(&sums->list);
2717 kfree(sums);
2718 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002719 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002720}
2721
Chris Masone02119d2008-09-05 16:13:11 -04002722/* log a single inode in the tree log.
2723 * At least one parent directory for this inode must exist in the tree
2724 * or be logged already.
2725 *
2726 * Any items from this inode changed by the current transaction are copied
2727 * to the log tree. An extra reference is taken on any extents in this
2728 * file, allowing us to avoid a whole pile of corner cases around logging
2729 * blocks that have been removed from the tree.
2730 *
2731 * See LOG_INODE_ALL and related defines for a description of what inode_only
2732 * does.
2733 *
2734 * This handles both files and directories.
2735 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002736static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002737 struct btrfs_root *root, struct inode *inode,
2738 int inode_only)
2739{
2740 struct btrfs_path *path;
2741 struct btrfs_path *dst_path;
2742 struct btrfs_key min_key;
2743 struct btrfs_key max_key;
2744 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002745 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002746 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002747 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002748 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002749 int ins_start_slot = 0;
2750 int ins_nr;
Chris Masone02119d2008-09-05 16:13:11 -04002751
2752 log = root->log_root;
2753
2754 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002755 if (!path)
2756 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002757 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002758 if (!dst_path) {
2759 btrfs_free_path(path);
2760 return -ENOMEM;
2761 }
Chris Masone02119d2008-09-05 16:13:11 -04002762
2763 min_key.objectid = inode->i_ino;
2764 min_key.type = BTRFS_INODE_ITEM_KEY;
2765 min_key.offset = 0;
2766
2767 max_key.objectid = inode->i_ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04002768
2769 /* today the code can only do partial logging of directories */
2770 if (!S_ISDIR(inode->i_mode))
2771 inode_only = LOG_INODE_ALL;
2772
Chris Masone02119d2008-09-05 16:13:11 -04002773 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2774 max_key.type = BTRFS_XATTR_ITEM_KEY;
2775 else
2776 max_key.type = (u8)-1;
2777 max_key.offset = (u64)-1;
2778
Chris Masone02119d2008-09-05 16:13:11 -04002779 mutex_lock(&BTRFS_I(inode)->log_mutex);
2780
2781 /*
2782 * a brute force approach to making sure we get the most uptodate
2783 * copies of everything.
2784 */
2785 if (S_ISDIR(inode->i_mode)) {
2786 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2787
2788 if (inode_only == LOG_INODE_EXISTS)
2789 max_key_type = BTRFS_XATTR_ITEM_KEY;
2790 ret = drop_objectid_items(trans, log, path,
2791 inode->i_ino, max_key_type);
2792 } else {
2793 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2794 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002795 if (ret) {
2796 err = ret;
2797 goto out_unlock;
2798 }
Chris Masone02119d2008-09-05 16:13:11 -04002799 path->keep_locks = 1;
2800
Chris Masond3977122009-01-05 21:25:51 -05002801 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002802 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002803 ret = btrfs_search_forward(root, &min_key, &max_key,
2804 path, 0, trans->transid);
2805 if (ret != 0)
2806 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002807again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04002808 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Chris Masone02119d2008-09-05 16:13:11 -04002809 if (min_key.objectid != inode->i_ino)
2810 break;
2811 if (min_key.type > max_key.type)
2812 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002813
Chris Masone02119d2008-09-05 16:13:11 -04002814 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04002815 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2816 ins_nr++;
2817 goto next_slot;
2818 } else if (!ins_nr) {
2819 ins_start_slot = path->slots[0];
2820 ins_nr = 1;
2821 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002822 }
2823
Chris Mason31ff1cd2008-09-11 16:17:57 -04002824 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2825 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002826 if (ret) {
2827 err = ret;
2828 goto out_unlock;
2829 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002830 ins_nr = 1;
2831 ins_start_slot = path->slots[0];
2832next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04002833
Chris Mason3a5f1d42008-09-11 15:53:37 -04002834 nritems = btrfs_header_nritems(path->nodes[0]);
2835 path->slots[0]++;
2836 if (path->slots[0] < nritems) {
2837 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2838 path->slots[0]);
2839 goto again;
2840 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002841 if (ins_nr) {
2842 ret = copy_items(trans, log, dst_path, src,
2843 ins_start_slot,
2844 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002845 if (ret) {
2846 err = ret;
2847 goto out_unlock;
2848 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002849 ins_nr = 0;
2850 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002851 btrfs_release_path(root, path);
2852
Chris Masone02119d2008-09-05 16:13:11 -04002853 if (min_key.offset < (u64)-1)
2854 min_key.offset++;
2855 else if (min_key.type < (u8)-1)
2856 min_key.type++;
2857 else if (min_key.objectid < (u64)-1)
2858 min_key.objectid++;
2859 else
2860 break;
2861 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002862 if (ins_nr) {
2863 ret = copy_items(trans, log, dst_path, src,
2864 ins_start_slot,
2865 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002866 if (ret) {
2867 err = ret;
2868 goto out_unlock;
2869 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002870 ins_nr = 0;
2871 }
2872 WARN_ON(ins_nr);
Chris Mason9623f9a2008-09-11 17:42:42 -04002873 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04002874 btrfs_release_path(root, path);
2875 btrfs_release_path(log, dst_path);
2876 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002877 if (ret) {
2878 err = ret;
2879 goto out_unlock;
2880 }
Chris Masone02119d2008-09-05 16:13:11 -04002881 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002882 BTRFS_I(inode)->logged_trans = trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002883out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002884 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2885
2886 btrfs_free_path(path);
2887 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002888 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002889}
2890
Chris Mason12fcfd22009-03-24 10:24:20 -04002891/*
2892 * follow the dentry parent pointers up the chain and see if any
2893 * of the directories in it require a full commit before they can
2894 * be logged. Returns zero if nothing special needs to be done or 1 if
2895 * a full commit is required.
2896 */
2897static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
2898 struct inode *inode,
2899 struct dentry *parent,
2900 struct super_block *sb,
2901 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04002902{
Chris Mason12fcfd22009-03-24 10:24:20 -04002903 int ret = 0;
2904 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00002905 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002906
Chris Masonaf4176b2009-03-24 10:24:31 -04002907 /*
2908 * for regular files, if its inode is already on disk, we don't
2909 * have to worry about the parents at all. This is because
2910 * we can use the last_unlink_trans field to record renames
2911 * and other fun in this file.
2912 */
2913 if (S_ISREG(inode->i_mode) &&
2914 BTRFS_I(inode)->generation <= last_committed &&
2915 BTRFS_I(inode)->last_unlink_trans <= last_committed)
2916 goto out;
2917
Chris Mason12fcfd22009-03-24 10:24:20 -04002918 if (!S_ISDIR(inode->i_mode)) {
2919 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2920 goto out;
2921 inode = parent->d_inode;
2922 }
2923
2924 while (1) {
2925 BTRFS_I(inode)->logged_trans = trans->transid;
2926 smp_mb();
2927
2928 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
2929 root = BTRFS_I(inode)->root;
2930
2931 /*
2932 * make sure any commits to the log are forced
2933 * to be full commits
2934 */
2935 root->fs_info->last_trans_log_full_commit =
2936 trans->transid;
2937 ret = 1;
2938 break;
2939 }
2940
2941 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2942 break;
2943
Yan, Zheng76dda932009-09-21 16:00:26 -04002944 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04002945 break;
2946
Josef Bacik6a912212010-11-20 09:48:00 +00002947 parent = dget_parent(parent);
2948 dput(old_parent);
2949 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04002950 inode = parent->d_inode;
2951
2952 }
Josef Bacik6a912212010-11-20 09:48:00 +00002953 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04002954out:
Chris Masone02119d2008-09-05 16:13:11 -04002955 return ret;
2956}
2957
Chris Mason257c62e2009-10-13 13:21:08 -04002958static int inode_in_log(struct btrfs_trans_handle *trans,
2959 struct inode *inode)
2960{
2961 struct btrfs_root *root = BTRFS_I(inode)->root;
2962 int ret = 0;
2963
2964 mutex_lock(&root->log_mutex);
2965 if (BTRFS_I(inode)->logged_trans == trans->transid &&
2966 BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
2967 ret = 1;
2968 mutex_unlock(&root->log_mutex);
2969 return ret;
2970}
2971
2972
Chris Masone02119d2008-09-05 16:13:11 -04002973/*
2974 * helper function around btrfs_log_inode to make sure newly created
2975 * parent directories also end up in the log. A minimal inode and backref
2976 * only logging is done of any parent directories that are older than
2977 * the last committed transaction
2978 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002979int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
2980 struct btrfs_root *root, struct inode *inode,
2981 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04002982{
Chris Mason12fcfd22009-03-24 10:24:20 -04002983 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04002984 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00002985 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002986 int ret = 0;
2987 u64 last_committed = root->fs_info->last_trans_committed;
2988
2989 sb = inode->i_sb;
2990
Sage Weil3a5e1402009-04-02 16:49:40 -04002991 if (btrfs_test_opt(root, NOTREELOG)) {
2992 ret = 1;
2993 goto end_no_trans;
2994 }
2995
Chris Mason12fcfd22009-03-24 10:24:20 -04002996 if (root->fs_info->last_trans_log_full_commit >
2997 root->fs_info->last_trans_committed) {
2998 ret = 1;
2999 goto end_no_trans;
3000 }
3001
Yan, Zheng76dda932009-09-21 16:00:26 -04003002 if (root != BTRFS_I(inode)->root ||
3003 btrfs_root_refs(&root->root_item) == 0) {
3004 ret = 1;
3005 goto end_no_trans;
3006 }
3007
Chris Mason12fcfd22009-03-24 10:24:20 -04003008 ret = check_parent_dirs_for_sync(trans, inode, parent,
3009 sb, last_committed);
3010 if (ret)
3011 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003012
Chris Mason257c62e2009-10-13 13:21:08 -04003013 if (inode_in_log(trans, inode)) {
3014 ret = BTRFS_NO_LOG_SYNC;
3015 goto end_no_trans;
3016 }
3017
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003018 ret = start_log_trans(trans, root);
3019 if (ret)
3020 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003021
3022 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003023 if (ret)
3024 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003025
Chris Masonaf4176b2009-03-24 10:24:31 -04003026 /*
3027 * for regular files, if its inode is already on disk, we don't
3028 * have to worry about the parents at all. This is because
3029 * we can use the last_unlink_trans field to record renames
3030 * and other fun in this file.
3031 */
3032 if (S_ISREG(inode->i_mode) &&
3033 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003034 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3035 ret = 0;
3036 goto end_trans;
3037 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003038
3039 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003040 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003041 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003042 break;
3043
Chris Mason12fcfd22009-03-24 10:24:20 -04003044 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003045 if (root != BTRFS_I(inode)->root)
3046 break;
3047
Chris Mason12fcfd22009-03-24 10:24:20 -04003048 if (BTRFS_I(inode)->generation >
3049 root->fs_info->last_trans_committed) {
3050 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003051 if (ret)
3052 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003053 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003054 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003055 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003056
Josef Bacik6a912212010-11-20 09:48:00 +00003057 parent = dget_parent(parent);
3058 dput(old_parent);
3059 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003060 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003061 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003062end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003063 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003064 if (ret < 0) {
3065 BUG_ON(ret != -ENOSPC);
3066 root->fs_info->last_trans_log_full_commit = trans->transid;
3067 ret = 1;
3068 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003069 btrfs_end_log_trans(root);
3070end_no_trans:
3071 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003072}
3073
3074/*
3075 * it is not safe to log dentry if the chunk root has added new
3076 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3077 * If this returns 1, you must commit the transaction to safely get your
3078 * data on disk.
3079 */
3080int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3081 struct btrfs_root *root, struct dentry *dentry)
3082{
Josef Bacik6a912212010-11-20 09:48:00 +00003083 struct dentry *parent = dget_parent(dentry);
3084 int ret;
3085
3086 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3087 dput(parent);
3088
3089 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003090}
3091
3092/*
3093 * should be called during mount to recover any replay any log trees
3094 * from the FS
3095 */
3096int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3097{
3098 int ret;
3099 struct btrfs_path *path;
3100 struct btrfs_trans_handle *trans;
3101 struct btrfs_key key;
3102 struct btrfs_key found_key;
3103 struct btrfs_key tmp_key;
3104 struct btrfs_root *log;
3105 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3106 struct walk_control wc = {
3107 .process_func = process_one_buffer,
3108 .stage = 0,
3109 };
3110
Chris Masone02119d2008-09-05 16:13:11 -04003111 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003112 if (!path)
3113 return -ENOMEM;
3114
3115 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003116
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003117 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003118 BUG_ON(IS_ERR(trans));
Chris Masone02119d2008-09-05 16:13:11 -04003119
3120 wc.trans = trans;
3121 wc.pin = 1;
3122
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003123 ret = walk_log_tree(trans, log_root_tree, &wc);
3124 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003125
3126again:
3127 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3128 key.offset = (u64)-1;
3129 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3130
Chris Masond3977122009-01-05 21:25:51 -05003131 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003132 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3133 if (ret < 0)
3134 break;
3135 if (ret > 0) {
3136 if (path->slots[0] == 0)
3137 break;
3138 path->slots[0]--;
3139 }
3140 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3141 path->slots[0]);
3142 btrfs_release_path(log_root_tree, path);
3143 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3144 break;
3145
3146 log = btrfs_read_fs_root_no_radix(log_root_tree,
3147 &found_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003148 BUG_ON(IS_ERR(log));
Chris Masone02119d2008-09-05 16:13:11 -04003149
3150 tmp_key.objectid = found_key.offset;
3151 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3152 tmp_key.offset = (u64)-1;
3153
3154 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Chris Masone02119d2008-09-05 16:13:11 -04003155 BUG_ON(!wc.replay_dest);
3156
Yan Zheng07d400a2009-01-06 11:42:00 -05003157 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003158 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003159 ret = walk_log_tree(trans, log, &wc);
3160 BUG_ON(ret);
3161
3162 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3163 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3164 path);
3165 BUG_ON(ret);
3166 }
Chris Masone02119d2008-09-05 16:13:11 -04003167
3168 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003169 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003170 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003171 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003172 kfree(log);
3173
3174 if (found_key.offset == 0)
3175 break;
3176 }
3177 btrfs_release_path(log_root_tree, path);
3178
3179 /* step one is to pin it all, step two is to replay just inodes */
3180 if (wc.pin) {
3181 wc.pin = 0;
3182 wc.process_func = replay_one_buffer;
3183 wc.stage = LOG_WALK_REPLAY_INODES;
3184 goto again;
3185 }
3186 /* step three is to replay everything */
3187 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3188 wc.stage++;
3189 goto again;
3190 }
3191
3192 btrfs_free_path(path);
3193
3194 free_extent_buffer(log_root_tree->node);
3195 log_root_tree->log_root = NULL;
3196 fs_info->log_root_recovering = 0;
3197
3198 /* step 4: commit the transaction, which also unpins the blocks */
3199 btrfs_commit_transaction(trans, fs_info->tree_root);
3200
3201 kfree(log_root_tree);
3202 return 0;
3203}
Chris Mason12fcfd22009-03-24 10:24:20 -04003204
3205/*
3206 * there are some corner cases where we want to force a full
3207 * commit instead of allowing a directory to be logged.
3208 *
3209 * They revolve around files there were unlinked from the directory, and
3210 * this function updates the parent directory so that a full commit is
3211 * properly done if it is fsync'd later after the unlinks are done.
3212 */
3213void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3214 struct inode *dir, struct inode *inode,
3215 int for_rename)
3216{
3217 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003218 * when we're logging a file, if it hasn't been renamed
3219 * or unlinked, and its inode is fully committed on disk,
3220 * we don't have to worry about walking up the directory chain
3221 * to log its parents.
3222 *
3223 * So, we use the last_unlink_trans field to put this transid
3224 * into the file. When the file is logged we check it and
3225 * don't log the parents if the file is fully on disk.
3226 */
3227 if (S_ISREG(inode->i_mode))
3228 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3229
3230 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003231 * if this directory was already logged any new
3232 * names for this file/dir will get recorded
3233 */
3234 smp_mb();
3235 if (BTRFS_I(dir)->logged_trans == trans->transid)
3236 return;
3237
3238 /*
3239 * if the inode we're about to unlink was logged,
3240 * the log will be properly updated for any new names
3241 */
3242 if (BTRFS_I(inode)->logged_trans == trans->transid)
3243 return;
3244
3245 /*
3246 * when renaming files across directories, if the directory
3247 * there we're unlinking from gets fsync'd later on, there's
3248 * no way to find the destination directory later and fsync it
3249 * properly. So, we have to be conservative and force commits
3250 * so the new name gets discovered.
3251 */
3252 if (for_rename)
3253 goto record;
3254
3255 /* we can safely do the unlink without any special recording */
3256 return;
3257
3258record:
3259 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3260}
3261
3262/*
3263 * Call this after adding a new name for a file and it will properly
3264 * update the log to reflect the new name.
3265 *
3266 * It will return zero if all goes well, and it will return 1 if a
3267 * full transaction commit is required.
3268 */
3269int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3270 struct inode *inode, struct inode *old_dir,
3271 struct dentry *parent)
3272{
3273 struct btrfs_root * root = BTRFS_I(inode)->root;
3274
3275 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003276 * this will force the logging code to walk the dentry chain
3277 * up for the file
3278 */
3279 if (S_ISREG(inode->i_mode))
3280 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3281
3282 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003283 * if this inode hasn't been logged and directory we're renaming it
3284 * from hasn't been logged, we don't need to log it
3285 */
3286 if (BTRFS_I(inode)->logged_trans <=
3287 root->fs_info->last_trans_committed &&
3288 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3289 root->fs_info->last_trans_committed))
3290 return 0;
3291
3292 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3293}
3294