]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/tree-log.c
Btrfs: remove dead code
[net-next-2.6.git] / fs / btrfs / tree-log.c
CommitLineData
e02119d5
CM
1/*
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>
20#include "ctree.h"
21#include "transaction.h"
22#include "disk-io.h"
23#include "locking.h"
24#include "print-tree.h"
25#include "compat.h"
b2950863 26#include "tree-log.h"
e02119d5
CM
27
28/* magic values for the inode_only field in btrfs_log_inode:
29 *
30 * LOG_INODE_ALL means to log everything
31 * LOG_INODE_EXISTS means to log just enough to recreate the inode
32 * during log replay
33 */
34#define LOG_INODE_ALL 0
35#define LOG_INODE_EXISTS 1
36
12fcfd22
CM
37/*
38 * directory trouble cases
39 *
40 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
41 * log, we must force a full commit before doing an fsync of the directory
42 * where the unlink was done.
43 * ---> record transid of last unlink/rename per directory
44 *
45 * mkdir foo/some_dir
46 * normal commit
47 * rename foo/some_dir foo2/some_dir
48 * mkdir foo/some_dir
49 * fsync foo/some_dir/some_file
50 *
51 * The fsync above will unlink the original some_dir without recording
52 * it in its new location (foo2). After a crash, some_dir will be gone
53 * unless the fsync of some_file forces a full commit
54 *
55 * 2) we must log any new names for any file or dir that is in the fsync
56 * log. ---> check inode while renaming/linking.
57 *
58 * 2a) we must log any new names for any file or dir during rename
59 * when the directory they are being removed from was logged.
60 * ---> check inode and old parent dir during rename
61 *
62 * 2a is actually the more important variant. With the extra logging
63 * a crash might unlink the old name without recreating the new one
64 *
65 * 3) after a crash, we must go through any directories with a link count
66 * of zero and redo the rm -rf
67 *
68 * mkdir f1/foo
69 * normal commit
70 * rm -rf f1/foo
71 * fsync(f1)
72 *
73 * The directory f1 was fully removed from the FS, but fsync was never
74 * called on f1, only its parent dir. After a crash the rm -rf must
75 * be replayed. This must be able to recurse down the entire
76 * directory tree. The inode link count fixup code takes care of the
77 * ugly details.
78 */
79
e02119d5
CM
80/*
81 * stages for the tree walking. The first
82 * stage (0) is to only pin down the blocks we find
83 * the second stage (1) is to make sure that all the inodes
84 * we find in the log are created in the subvolume.
85 *
86 * The last stage is to deal with directories and links and extents
87 * and all the other fun semantics
88 */
89#define LOG_WALK_PIN_ONLY 0
90#define LOG_WALK_REPLAY_INODES 1
91#define LOG_WALK_REPLAY_ALL 2
92
12fcfd22 93static int btrfs_log_inode(struct btrfs_trans_handle *trans,
e02119d5
CM
94 struct btrfs_root *root, struct inode *inode,
95 int inode_only);
ec051c0f
YZ
96static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
97 struct btrfs_root *root,
98 struct btrfs_path *path, u64 objectid);
12fcfd22
CM
99static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
100 struct btrfs_root *root,
101 struct btrfs_root *log,
102 struct btrfs_path *path,
103 u64 dirid, int del_all);
e02119d5
CM
104
105/*
106 * tree logging is a special write ahead log used to make sure that
107 * fsyncs and O_SYNCs can happen without doing full tree commits.
108 *
109 * Full tree commits are expensive because they require commonly
110 * modified blocks to be recowed, creating many dirty pages in the
111 * extent tree an 4x-6x higher write load than ext3.
112 *
113 * Instead of doing a tree commit on every fsync, we use the
114 * key ranges and transaction ids to find items for a given file or directory
115 * that have changed in this transaction. Those items are copied into
116 * a special tree (one per subvolume root), that tree is written to disk
117 * and then the fsync is considered complete.
118 *
119 * After a crash, items are copied out of the log-tree back into the
120 * subvolume tree. Any file data extents found are recorded in the extent
121 * allocation tree, and the log-tree freed.
122 *
123 * The log tree is read three times, once to pin down all the extents it is
124 * using in ram and once, once to create all the inodes logged in the tree
125 * and once to do all the other items.
126 */
127
e02119d5
CM
128/*
129 * start a sub transaction and setup the log tree
130 * this increments the log tree writer count to make the people
131 * syncing the tree wait for us to finish
132 */
133static int start_log_trans(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root)
135{
136 int ret;
7237f183
YZ
137
138 mutex_lock(&root->log_mutex);
139 if (root->log_root) {
140 root->log_batch++;
141 atomic_inc(&root->log_writers);
142 mutex_unlock(&root->log_mutex);
143 return 0;
144 }
e02119d5
CM
145 mutex_lock(&root->fs_info->tree_log_mutex);
146 if (!root->fs_info->log_root_tree) {
147 ret = btrfs_init_log_root_tree(trans, root->fs_info);
148 BUG_ON(ret);
149 }
150 if (!root->log_root) {
151 ret = btrfs_add_log_tree(trans, root);
152 BUG_ON(ret);
153 }
e02119d5 154 mutex_unlock(&root->fs_info->tree_log_mutex);
7237f183
YZ
155 root->log_batch++;
156 atomic_inc(&root->log_writers);
157 mutex_unlock(&root->log_mutex);
e02119d5
CM
158 return 0;
159}
160
161/*
162 * returns 0 if there was a log transaction running and we were able
163 * to join, or returns -ENOENT if there were not transactions
164 * in progress
165 */
166static int join_running_log_trans(struct btrfs_root *root)
167{
168 int ret = -ENOENT;
169
170 smp_mb();
171 if (!root->log_root)
172 return -ENOENT;
173
7237f183 174 mutex_lock(&root->log_mutex);
e02119d5
CM
175 if (root->log_root) {
176 ret = 0;
7237f183 177 atomic_inc(&root->log_writers);
e02119d5 178 }
7237f183 179 mutex_unlock(&root->log_mutex);
e02119d5
CM
180 return ret;
181}
182
12fcfd22
CM
183/*
184 * This either makes the current running log transaction wait
185 * until you call btrfs_end_log_trans() or it makes any future
186 * log transactions wait until you call btrfs_end_log_trans()
187 */
188int btrfs_pin_log_trans(struct btrfs_root *root)
189{
190 int ret = -ENOENT;
191
192 mutex_lock(&root->log_mutex);
193 atomic_inc(&root->log_writers);
194 mutex_unlock(&root->log_mutex);
195 return ret;
196}
197
e02119d5
CM
198/*
199 * indicate we're done making changes to the log tree
200 * and wake up anyone waiting to do a sync
201 */
12fcfd22 202int btrfs_end_log_trans(struct btrfs_root *root)
e02119d5 203{
7237f183
YZ
204 if (atomic_dec_and_test(&root->log_writers)) {
205 smp_mb();
206 if (waitqueue_active(&root->log_writer_wait))
207 wake_up(&root->log_writer_wait);
208 }
e02119d5
CM
209 return 0;
210}
211
212
213/*
214 * the walk control struct is used to pass state down the chain when
215 * processing the log tree. The stage field tells us which part
216 * of the log tree processing we are currently doing. The others
217 * are state fields used for that specific part
218 */
219struct walk_control {
220 /* should we free the extent on disk when done? This is used
221 * at transaction commit time while freeing a log tree
222 */
223 int free;
224
225 /* should we write out the extent buffer? This is used
226 * while flushing the log tree to disk during a sync
227 */
228 int write;
229
230 /* should we wait for the extent buffer io to finish? Also used
231 * while flushing the log tree to disk for a sync
232 */
233 int wait;
234
235 /* pin only walk, we record which extents on disk belong to the
236 * log trees
237 */
238 int pin;
239
240 /* what stage of the replay code we're currently in */
241 int stage;
242
243 /* the root we are currently replaying */
244 struct btrfs_root *replay_dest;
245
246 /* the trans handle for the current replay */
247 struct btrfs_trans_handle *trans;
248
249 /* the function that gets used to process blocks we find in the
250 * tree. Note the extent_buffer might not be up to date when it is
251 * passed in, and it must be checked or read if you need the data
252 * inside it
253 */
254 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
255 struct walk_control *wc, u64 gen);
256};
257
258/*
259 * process_func used to pin down extents, write them or wait on them
260 */
261static int process_one_buffer(struct btrfs_root *log,
262 struct extent_buffer *eb,
263 struct walk_control *wc, u64 gen)
264{
04018de5 265 if (wc->pin)
e02119d5
CM
266 btrfs_update_pinned_extents(log->fs_info->extent_root,
267 eb->start, eb->len, 1);
e02119d5
CM
268
269 if (btrfs_buffer_uptodate(eb, gen)) {
270 if (wc->write)
271 btrfs_write_tree_block(eb);
272 if (wc->wait)
273 btrfs_wait_tree_block_writeback(eb);
274 }
275 return 0;
276}
277
278/*
279 * Item overwrite used by replay and tree logging. eb, slot and key all refer
280 * to the src data we are copying out.
281 *
282 * root is the tree we are copying into, and path is a scratch
283 * path for use in this function (it should be released on entry and
284 * will be released on exit).
285 *
286 * If the key is already in the destination tree the existing item is
287 * overwritten. If the existing item isn't big enough, it is extended.
288 * If it is too large, it is truncated.
289 *
290 * If the key isn't in the destination yet, a new item is inserted.
291 */
292static noinline int overwrite_item(struct btrfs_trans_handle *trans,
293 struct btrfs_root *root,
294 struct btrfs_path *path,
295 struct extent_buffer *eb, int slot,
296 struct btrfs_key *key)
297{
298 int ret;
299 u32 item_size;
300 u64 saved_i_size = 0;
301 int save_old_i_size = 0;
302 unsigned long src_ptr;
303 unsigned long dst_ptr;
304 int overwrite_root = 0;
305
306 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
307 overwrite_root = 1;
308
309 item_size = btrfs_item_size_nr(eb, slot);
310 src_ptr = btrfs_item_ptr_offset(eb, slot);
311
312 /* look for the key in the destination tree */
313 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
314 if (ret == 0) {
315 char *src_copy;
316 char *dst_copy;
317 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
318 path->slots[0]);
319 if (dst_size != item_size)
320 goto insert;
321
322 if (item_size == 0) {
323 btrfs_release_path(root, path);
324 return 0;
325 }
326 dst_copy = kmalloc(item_size, GFP_NOFS);
327 src_copy = kmalloc(item_size, GFP_NOFS);
328
329 read_extent_buffer(eb, src_copy, src_ptr, item_size);
330
331 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
332 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
333 item_size);
334 ret = memcmp(dst_copy, src_copy, item_size);
335
336 kfree(dst_copy);
337 kfree(src_copy);
338 /*
339 * they have the same contents, just return, this saves
340 * us from cowing blocks in the destination tree and doing
341 * extra writes that may not have been done by a previous
342 * sync
343 */
344 if (ret == 0) {
345 btrfs_release_path(root, path);
346 return 0;
347 }
348
349 }
350insert:
351 btrfs_release_path(root, path);
352 /* try to insert the key into the destination tree */
353 ret = btrfs_insert_empty_item(trans, root, path,
354 key, item_size);
355
356 /* make sure any existing item is the correct size */
357 if (ret == -EEXIST) {
358 u32 found_size;
359 found_size = btrfs_item_size_nr(path->nodes[0],
360 path->slots[0]);
361 if (found_size > item_size) {
362 btrfs_truncate_item(trans, root, path, item_size, 1);
363 } else if (found_size < item_size) {
87b29b20
YZ
364 ret = btrfs_extend_item(trans, root, path,
365 item_size - found_size);
e02119d5
CM
366 BUG_ON(ret);
367 }
368 } else if (ret) {
369 BUG();
370 }
371 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
372 path->slots[0]);
373
374 /* don't overwrite an existing inode if the generation number
375 * was logged as zero. This is done when the tree logging code
376 * is just logging an inode to make sure it exists after recovery.
377 *
378 * Also, don't overwrite i_size on directories during replay.
379 * log replay inserts and removes directory items based on the
380 * state of the tree found in the subvolume, and i_size is modified
381 * as it goes
382 */
383 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
384 struct btrfs_inode_item *src_item;
385 struct btrfs_inode_item *dst_item;
386
387 src_item = (struct btrfs_inode_item *)src_ptr;
388 dst_item = (struct btrfs_inode_item *)dst_ptr;
389
390 if (btrfs_inode_generation(eb, src_item) == 0)
391 goto no_copy;
392
393 if (overwrite_root &&
394 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
395 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
396 save_old_i_size = 1;
397 saved_i_size = btrfs_inode_size(path->nodes[0],
398 dst_item);
399 }
400 }
401
402 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
403 src_ptr, item_size);
404
405 if (save_old_i_size) {
406 struct btrfs_inode_item *dst_item;
407 dst_item = (struct btrfs_inode_item *)dst_ptr;
408 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
409 }
410
411 /* make sure the generation is filled in */
412 if (key->type == BTRFS_INODE_ITEM_KEY) {
413 struct btrfs_inode_item *dst_item;
414 dst_item = (struct btrfs_inode_item *)dst_ptr;
415 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
416 btrfs_set_inode_generation(path->nodes[0], dst_item,
417 trans->transid);
418 }
419 }
420no_copy:
421 btrfs_mark_buffer_dirty(path->nodes[0]);
422 btrfs_release_path(root, path);
423 return 0;
424}
425
426/*
427 * simple helper to read an inode off the disk from a given root
428 * This can only be called for subvolume roots and not for the log
429 */
430static noinline struct inode *read_one_inode(struct btrfs_root *root,
431 u64 objectid)
432{
433 struct inode *inode;
434 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
435 if (inode->i_state & I_NEW) {
436 BTRFS_I(inode)->root = root;
437 BTRFS_I(inode)->location.objectid = objectid;
438 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
439 BTRFS_I(inode)->location.offset = 0;
440 btrfs_read_locked_inode(inode);
441 unlock_new_inode(inode);
442
443 }
444 if (is_bad_inode(inode)) {
445 iput(inode);
446 inode = NULL;
447 }
448 return inode;
449}
450
451/* replays a single extent in 'eb' at 'slot' with 'key' into the
452 * subvolume 'root'. path is released on entry and should be released
453 * on exit.
454 *
455 * extents in the log tree have not been allocated out of the extent
456 * tree yet. So, this completes the allocation, taking a reference
457 * as required if the extent already exists or creating a new extent
458 * if it isn't in the extent allocation tree yet.
459 *
460 * The extent is inserted into the file, dropping any existing extents
461 * from the file that overlap the new one.
462 */
463static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
464 struct btrfs_root *root,
465 struct btrfs_path *path,
466 struct extent_buffer *eb, int slot,
467 struct btrfs_key *key)
468{
469 int found_type;
470 u64 mask = root->sectorsize - 1;
471 u64 extent_end;
472 u64 alloc_hint;
473 u64 start = key->offset;
07d400a6 474 u64 saved_nbytes;
e02119d5
CM
475 struct btrfs_file_extent_item *item;
476 struct inode *inode = NULL;
477 unsigned long size;
478 int ret = 0;
479
480 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
481 found_type = btrfs_file_extent_type(eb, item);
482
d899e052
YZ
483 if (found_type == BTRFS_FILE_EXTENT_REG ||
484 found_type == BTRFS_FILE_EXTENT_PREALLOC)
e02119d5
CM
485 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
486 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
c8b97818 487 size = btrfs_file_extent_inline_len(eb, item);
e02119d5
CM
488 extent_end = (start + size + mask) & ~mask;
489 } else {
490 ret = 0;
491 goto out;
492 }
493
494 inode = read_one_inode(root, key->objectid);
495 if (!inode) {
496 ret = -EIO;
497 goto out;
498 }
499
500 /*
501 * first check to see if we already have this extent in the
502 * file. This must be done before the btrfs_drop_extents run
503 * so we don't try to drop this extent.
504 */
505 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
506 start, 0);
507
d899e052
YZ
508 if (ret == 0 &&
509 (found_type == BTRFS_FILE_EXTENT_REG ||
510 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
e02119d5
CM
511 struct btrfs_file_extent_item cmp1;
512 struct btrfs_file_extent_item cmp2;
513 struct btrfs_file_extent_item *existing;
514 struct extent_buffer *leaf;
515
516 leaf = path->nodes[0];
517 existing = btrfs_item_ptr(leaf, path->slots[0],
518 struct btrfs_file_extent_item);
519
520 read_extent_buffer(eb, &cmp1, (unsigned long)item,
521 sizeof(cmp1));
522 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
523 sizeof(cmp2));
524
525 /*
526 * we already have a pointer to this exact extent,
527 * we don't have to do anything
528 */
529 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
530 btrfs_release_path(root, path);
531 goto out;
532 }
533 }
534 btrfs_release_path(root, path);
535
07d400a6 536 saved_nbytes = inode_get_bytes(inode);
e02119d5
CM
537 /* drop any overlapping extents */
538 ret = btrfs_drop_extents(trans, root, inode,
539 start, extent_end, start, &alloc_hint);
540 BUG_ON(ret);
541
07d400a6
YZ
542 if (found_type == BTRFS_FILE_EXTENT_REG ||
543 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
544 unsigned long dest_offset;
545 struct btrfs_key ins;
546
547 ret = btrfs_insert_empty_item(trans, root, path, key,
548 sizeof(*item));
549 BUG_ON(ret);
550 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
551 path->slots[0]);
552 copy_extent_buffer(path->nodes[0], eb, dest_offset,
553 (unsigned long)item, sizeof(*item));
554
555 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
556 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
557 ins.type = BTRFS_EXTENT_ITEM_KEY;
558
559 if (ins.objectid > 0) {
560 u64 csum_start;
561 u64 csum_end;
562 LIST_HEAD(ordered_sums);
563 /*
564 * is this extent already allocated in the extent
565 * allocation tree? If so, just add a reference
566 */
567 ret = btrfs_lookup_extent(root, ins.objectid,
568 ins.offset);
569 if (ret == 0) {
570 ret = btrfs_inc_extent_ref(trans, root,
571 ins.objectid, ins.offset,
572 path->nodes[0]->start,
573 root->root_key.objectid,
574 trans->transid, key->objectid);
575 } else {
576 /*
577 * insert the extent pointer in the extent
578 * allocation tree
579 */
580 ret = btrfs_alloc_logged_extent(trans, root,
581 path->nodes[0]->start,
582 root->root_key.objectid,
583 trans->transid, key->objectid,
584 &ins);
585 BUG_ON(ret);
586 }
587 btrfs_release_path(root, path);
588
589 if (btrfs_file_extent_compression(eb, item)) {
590 csum_start = ins.objectid;
591 csum_end = csum_start + ins.offset;
592 } else {
593 csum_start = ins.objectid +
594 btrfs_file_extent_offset(eb, item);
595 csum_end = csum_start +
596 btrfs_file_extent_num_bytes(eb, item);
597 }
598
599 ret = btrfs_lookup_csums_range(root->log_root,
600 csum_start, csum_end - 1,
601 &ordered_sums);
602 BUG_ON(ret);
603 while (!list_empty(&ordered_sums)) {
604 struct btrfs_ordered_sum *sums;
605 sums = list_entry(ordered_sums.next,
606 struct btrfs_ordered_sum,
607 list);
608 ret = btrfs_csum_file_blocks(trans,
609 root->fs_info->csum_root,
610 sums);
611 BUG_ON(ret);
612 list_del(&sums->list);
613 kfree(sums);
614 }
615 } else {
616 btrfs_release_path(root, path);
617 }
618 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
619 /* inline extents are easy, we just overwrite them */
620 ret = overwrite_item(trans, root, path, eb, slot, key);
621 BUG_ON(ret);
622 }
e02119d5 623
07d400a6 624 inode_set_bytes(inode, saved_nbytes);
e02119d5
CM
625 btrfs_update_inode(trans, root, inode);
626out:
627 if (inode)
628 iput(inode);
629 return ret;
630}
631
632/*
633 * when cleaning up conflicts between the directory names in the
634 * subvolume, directory names in the log and directory names in the
635 * inode back references, we may have to unlink inodes from directories.
636 *
637 * This is a helper function to do the unlink of a specific directory
638 * item
639 */
640static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
641 struct btrfs_root *root,
642 struct btrfs_path *path,
643 struct inode *dir,
644 struct btrfs_dir_item *di)
645{
646 struct inode *inode;
647 char *name;
648 int name_len;
649 struct extent_buffer *leaf;
650 struct btrfs_key location;
651 int ret;
652
653 leaf = path->nodes[0];
654
655 btrfs_dir_item_key_to_cpu(leaf, di, &location);
656 name_len = btrfs_dir_name_len(leaf, di);
657 name = kmalloc(name_len, GFP_NOFS);
658 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
659 btrfs_release_path(root, path);
660
661 inode = read_one_inode(root, location.objectid);
662 BUG_ON(!inode);
663
ec051c0f
YZ
664 ret = link_to_fixup_dir(trans, root, path, location.objectid);
665 BUG_ON(ret);
12fcfd22 666
e02119d5 667 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
ec051c0f 668 BUG_ON(ret);
e02119d5
CM
669 kfree(name);
670
671 iput(inode);
672 return ret;
673}
674
675/*
676 * helper function to see if a given name and sequence number found
677 * in an inode back reference are already in a directory and correctly
678 * point to this inode
679 */
680static noinline int inode_in_dir(struct btrfs_root *root,
681 struct btrfs_path *path,
682 u64 dirid, u64 objectid, u64 index,
683 const char *name, int name_len)
684{
685 struct btrfs_dir_item *di;
686 struct btrfs_key location;
687 int match = 0;
688
689 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
690 index, name, name_len, 0);
691 if (di && !IS_ERR(di)) {
692 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
693 if (location.objectid != objectid)
694 goto out;
695 } else
696 goto out;
697 btrfs_release_path(root, path);
698
699 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
700 if (di && !IS_ERR(di)) {
701 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
702 if (location.objectid != objectid)
703 goto out;
704 } else
705 goto out;
706 match = 1;
707out:
708 btrfs_release_path(root, path);
709 return match;
710}
711
712/*
713 * helper function to check a log tree for a named back reference in
714 * an inode. This is used to decide if a back reference that is
715 * found in the subvolume conflicts with what we find in the log.
716 *
717 * inode backreferences may have multiple refs in a single item,
718 * during replay we process one reference at a time, and we don't
719 * want to delete valid links to a file from the subvolume if that
720 * link is also in the log.
721 */
722static noinline int backref_in_log(struct btrfs_root *log,
723 struct btrfs_key *key,
724 char *name, int namelen)
725{
726 struct btrfs_path *path;
727 struct btrfs_inode_ref *ref;
728 unsigned long ptr;
729 unsigned long ptr_end;
730 unsigned long name_ptr;
731 int found_name_len;
732 int item_size;
733 int ret;
734 int match = 0;
735
736 path = btrfs_alloc_path();
737 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
738 if (ret != 0)
739 goto out;
740
741 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
742 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
743 ptr_end = ptr + item_size;
744 while (ptr < ptr_end) {
745 ref = (struct btrfs_inode_ref *)ptr;
746 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
747 if (found_name_len == namelen) {
748 name_ptr = (unsigned long)(ref + 1);
749 ret = memcmp_extent_buffer(path->nodes[0], name,
750 name_ptr, namelen);
751 if (ret == 0) {
752 match = 1;
753 goto out;
754 }
755 }
756 ptr = (unsigned long)(ref + 1) + found_name_len;
757 }
758out:
759 btrfs_free_path(path);
760 return match;
761}
762
763
764/*
765 * replay one inode back reference item found in the log tree.
766 * eb, slot and key refer to the buffer and key found in the log tree.
767 * root is the destination we are replaying into, and path is for temp
768 * use by this function. (it should be released on return).
769 */
770static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
771 struct btrfs_root *root,
772 struct btrfs_root *log,
773 struct btrfs_path *path,
774 struct extent_buffer *eb, int slot,
775 struct btrfs_key *key)
776{
777 struct inode *dir;
778 int ret;
779 struct btrfs_key location;
780 struct btrfs_inode_ref *ref;
781 struct btrfs_dir_item *di;
782 struct inode *inode;
783 char *name;
784 int namelen;
785 unsigned long ref_ptr;
786 unsigned long ref_end;
787
788 location.objectid = key->objectid;
789 location.type = BTRFS_INODE_ITEM_KEY;
790 location.offset = 0;
791
792 /*
793 * it is possible that we didn't log all the parent directories
794 * for a given inode. If we don't find the dir, just don't
795 * copy the back ref in. The link count fixup code will take
796 * care of the rest
797 */
798 dir = read_one_inode(root, key->offset);
799 if (!dir)
800 return -ENOENT;
801
802 inode = read_one_inode(root, key->objectid);
803 BUG_ON(!dir);
804
805 ref_ptr = btrfs_item_ptr_offset(eb, slot);
806 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
807
808again:
809 ref = (struct btrfs_inode_ref *)ref_ptr;
810
811 namelen = btrfs_inode_ref_name_len(eb, ref);
812 name = kmalloc(namelen, GFP_NOFS);
813 BUG_ON(!name);
814
815 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
816
817 /* if we already have a perfect match, we're done */
818 if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
819 btrfs_inode_ref_index(eb, ref),
820 name, namelen)) {
821 goto out;
822 }
823
824 /*
825 * look for a conflicting back reference in the metadata.
826 * if we find one we have to unlink that name of the file
827 * before we add our new link. Later on, we overwrite any
828 * existing back reference, and we don't want to create
829 * dangling pointers in the directory.
830 */
831conflict_again:
832 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
833 if (ret == 0) {
834 char *victim_name;
835 int victim_name_len;
836 struct btrfs_inode_ref *victim_ref;
837 unsigned long ptr;
838 unsigned long ptr_end;
839 struct extent_buffer *leaf = path->nodes[0];
840
841 /* are we trying to overwrite a back ref for the root directory
842 * if so, just jump out, we're done
843 */
844 if (key->objectid == key->offset)
845 goto out_nowrite;
846
847 /* check all the names in this back reference to see
848 * if they are in the log. if so, we allow them to stay
849 * otherwise they must be unlinked as a conflict
850 */
851 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
852 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
d397712b 853 while (ptr < ptr_end) {
e02119d5
CM
854 victim_ref = (struct btrfs_inode_ref *)ptr;
855 victim_name_len = btrfs_inode_ref_name_len(leaf,
856 victim_ref);
857 victim_name = kmalloc(victim_name_len, GFP_NOFS);
858 BUG_ON(!victim_name);
859
860 read_extent_buffer(leaf, victim_name,
861 (unsigned long)(victim_ref + 1),
862 victim_name_len);
863
864 if (!backref_in_log(log, key, victim_name,
865 victim_name_len)) {
866 btrfs_inc_nlink(inode);
867 btrfs_release_path(root, path);
12fcfd22 868
e02119d5
CM
869 ret = btrfs_unlink_inode(trans, root, dir,
870 inode, victim_name,
871 victim_name_len);
872 kfree(victim_name);
873 btrfs_release_path(root, path);
874 goto conflict_again;
875 }
876 kfree(victim_name);
877 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
878 }
879 BUG_ON(ret);
880 }
881 btrfs_release_path(root, path);
882
883 /* look for a conflicting sequence number */
884 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
885 btrfs_inode_ref_index(eb, ref),
886 name, namelen, 0);
887 if (di && !IS_ERR(di)) {
888 ret = drop_one_dir_item(trans, root, path, dir, di);
889 BUG_ON(ret);
890 }
891 btrfs_release_path(root, path);
892
893
894 /* look for a conflicting name */
895 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
896 name, namelen, 0);
897 if (di && !IS_ERR(di)) {
898 ret = drop_one_dir_item(trans, root, path, dir, di);
899 BUG_ON(ret);
900 }
901 btrfs_release_path(root, path);
902
903 /* insert our name */
904 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
905 btrfs_inode_ref_index(eb, ref));
906 BUG_ON(ret);
907
908 btrfs_update_inode(trans, root, inode);
909
910out:
911 ref_ptr = (unsigned long)(ref + 1) + namelen;
912 kfree(name);
913 if (ref_ptr < ref_end)
914 goto again;
915
916 /* finally write the back reference in the inode */
917 ret = overwrite_item(trans, root, path, eb, slot, key);
918 BUG_ON(ret);
919
920out_nowrite:
921 btrfs_release_path(root, path);
922 iput(dir);
923 iput(inode);
924 return 0;
925}
926
e02119d5
CM
927/*
928 * There are a few corners where the link count of the file can't
929 * be properly maintained during replay. So, instead of adding
930 * lots of complexity to the log code, we just scan the backrefs
931 * for any file that has been through replay.
932 *
933 * The scan will update the link count on the inode to reflect the
934 * number of back refs found. If it goes down to zero, the iput
935 * will free the inode.
936 */
937static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root,
939 struct inode *inode)
940{
941 struct btrfs_path *path;
942 int ret;
943 struct btrfs_key key;
944 u64 nlink = 0;
945 unsigned long ptr;
946 unsigned long ptr_end;
947 int name_len;
948
949 key.objectid = inode->i_ino;
950 key.type = BTRFS_INODE_REF_KEY;
951 key.offset = (u64)-1;
952
953 path = btrfs_alloc_path();
954
d397712b 955 while (1) {
e02119d5
CM
956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
957 if (ret < 0)
958 break;
959 if (ret > 0) {
960 if (path->slots[0] == 0)
961 break;
962 path->slots[0]--;
963 }
964 btrfs_item_key_to_cpu(path->nodes[0], &key,
965 path->slots[0]);
966 if (key.objectid != inode->i_ino ||
967 key.type != BTRFS_INODE_REF_KEY)
968 break;
969 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
970 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
971 path->slots[0]);
d397712b 972 while (ptr < ptr_end) {
e02119d5
CM
973 struct btrfs_inode_ref *ref;
974
975 ref = (struct btrfs_inode_ref *)ptr;
976 name_len = btrfs_inode_ref_name_len(path->nodes[0],
977 ref);
978 ptr = (unsigned long)(ref + 1) + name_len;
979 nlink++;
980 }
981
982 if (key.offset == 0)
983 break;
984 key.offset--;
985 btrfs_release_path(root, path);
986 }
12fcfd22 987 btrfs_release_path(root, path);
e02119d5
CM
988 if (nlink != inode->i_nlink) {
989 inode->i_nlink = nlink;
990 btrfs_update_inode(trans, root, inode);
991 }
8d5bf1cb 992 BTRFS_I(inode)->index_cnt = (u64)-1;
e02119d5 993
12fcfd22
CM
994 if (inode->i_nlink == 0 && S_ISDIR(inode->i_mode)) {
995 ret = replay_dir_deletes(trans, root, NULL, path,
996 inode->i_ino, 1);
997 BUG_ON(ret);
998 }
999 btrfs_free_path(path);
1000
e02119d5
CM
1001 return 0;
1002}
1003
1004static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1005 struct btrfs_root *root,
1006 struct btrfs_path *path)
1007{
1008 int ret;
1009 struct btrfs_key key;
1010 struct inode *inode;
1011
1012 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1013 key.type = BTRFS_ORPHAN_ITEM_KEY;
1014 key.offset = (u64)-1;
d397712b 1015 while (1) {
e02119d5
CM
1016 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1017 if (ret < 0)
1018 break;
1019
1020 if (ret == 1) {
1021 if (path->slots[0] == 0)
1022 break;
1023 path->slots[0]--;
1024 }
1025
1026 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1027 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1028 key.type != BTRFS_ORPHAN_ITEM_KEY)
1029 break;
1030
1031 ret = btrfs_del_item(trans, root, path);
1032 BUG_ON(ret);
1033
1034 btrfs_release_path(root, path);
1035 inode = read_one_inode(root, key.offset);
1036 BUG_ON(!inode);
1037
1038 ret = fixup_inode_link_count(trans, root, inode);
1039 BUG_ON(ret);
1040
1041 iput(inode);
1042
12fcfd22
CM
1043 /*
1044 * fixup on a directory may create new entries,
1045 * make sure we always look for the highset possible
1046 * offset
1047 */
1048 key.offset = (u64)-1;
e02119d5
CM
1049 }
1050 btrfs_release_path(root, path);
1051 return 0;
1052}
1053
1054
1055/*
1056 * record a given inode in the fixup dir so we can check its link
1057 * count when replay is done. The link count is incremented here
1058 * so the inode won't go away until we check it
1059 */
1060static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1061 struct btrfs_root *root,
1062 struct btrfs_path *path,
1063 u64 objectid)
1064{
1065 struct btrfs_key key;
1066 int ret = 0;
1067 struct inode *inode;
1068
1069 inode = read_one_inode(root, objectid);
1070 BUG_ON(!inode);
1071
1072 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1073 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1074 key.offset = objectid;
1075
1076 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1077
1078 btrfs_release_path(root, path);
1079 if (ret == 0) {
1080 btrfs_inc_nlink(inode);
1081 btrfs_update_inode(trans, root, inode);
1082 } else if (ret == -EEXIST) {
1083 ret = 0;
1084 } else {
1085 BUG();
1086 }
1087 iput(inode);
1088
1089 return ret;
1090}
1091
1092/*
1093 * when replaying the log for a directory, we only insert names
1094 * for inodes that actually exist. This means an fsync on a directory
1095 * does not implicitly fsync all the new files in it
1096 */
1097static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1098 struct btrfs_root *root,
1099 struct btrfs_path *path,
1100 u64 dirid, u64 index,
1101 char *name, int name_len, u8 type,
1102 struct btrfs_key *location)
1103{
1104 struct inode *inode;
1105 struct inode *dir;
1106 int ret;
1107
1108 inode = read_one_inode(root, location->objectid);
1109 if (!inode)
1110 return -ENOENT;
1111
1112 dir = read_one_inode(root, dirid);
1113 if (!dir) {
1114 iput(inode);
1115 return -EIO;
1116 }
1117 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1118
1119 /* FIXME, put inode into FIXUP list */
1120
1121 iput(inode);
1122 iput(dir);
1123 return ret;
1124}
1125
1126/*
1127 * take a single entry in a log directory item and replay it into
1128 * the subvolume.
1129 *
1130 * if a conflicting item exists in the subdirectory already,
1131 * the inode it points to is unlinked and put into the link count
1132 * fix up tree.
1133 *
1134 * If a name from the log points to a file or directory that does
1135 * not exist in the FS, it is skipped. fsyncs on directories
1136 * do not force down inodes inside that directory, just changes to the
1137 * names or unlinks in a directory.
1138 */
1139static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1140 struct btrfs_root *root,
1141 struct btrfs_path *path,
1142 struct extent_buffer *eb,
1143 struct btrfs_dir_item *di,
1144 struct btrfs_key *key)
1145{
1146 char *name;
1147 int name_len;
1148 struct btrfs_dir_item *dst_di;
1149 struct btrfs_key found_key;
1150 struct btrfs_key log_key;
1151 struct inode *dir;
e02119d5 1152 u8 log_type;
4bef0848 1153 int exists;
e02119d5
CM
1154 int ret;
1155
1156 dir = read_one_inode(root, key->objectid);
1157 BUG_ON(!dir);
1158
1159 name_len = btrfs_dir_name_len(eb, di);
1160 name = kmalloc(name_len, GFP_NOFS);
1161 log_type = btrfs_dir_type(eb, di);
1162 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1163 name_len);
1164
1165 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
4bef0848
CM
1166 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1167 if (exists == 0)
1168 exists = 1;
1169 else
1170 exists = 0;
1171 btrfs_release_path(root, path);
1172
e02119d5
CM
1173 if (key->type == BTRFS_DIR_ITEM_KEY) {
1174 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1175 name, name_len, 1);
d397712b 1176 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
e02119d5
CM
1177 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1178 key->objectid,
1179 key->offset, name,
1180 name_len, 1);
1181 } else {
1182 BUG();
1183 }
1184 if (!dst_di || IS_ERR(dst_di)) {
1185 /* we need a sequence number to insert, so we only
1186 * do inserts for the BTRFS_DIR_INDEX_KEY types
1187 */
1188 if (key->type != BTRFS_DIR_INDEX_KEY)
1189 goto out;
1190 goto insert;
1191 }
1192
1193 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1194 /* the existing item matches the logged item */
1195 if (found_key.objectid == log_key.objectid &&
1196 found_key.type == log_key.type &&
1197 found_key.offset == log_key.offset &&
1198 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1199 goto out;
1200 }
1201
1202 /*
1203 * don't drop the conflicting directory entry if the inode
1204 * for the new entry doesn't exist
1205 */
4bef0848 1206 if (!exists)
e02119d5
CM
1207 goto out;
1208
e02119d5
CM
1209 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1210 BUG_ON(ret);
1211
1212 if (key->type == BTRFS_DIR_INDEX_KEY)
1213 goto insert;
1214out:
1215 btrfs_release_path(root, path);
1216 kfree(name);
1217 iput(dir);
1218 return 0;
1219
1220insert:
1221 btrfs_release_path(root, path);
1222 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1223 name, name_len, log_type, &log_key);
1224
1225 if (ret && ret != -ENOENT)
1226 BUG();
1227 goto out;
1228}
1229
1230/*
1231 * find all the names in a directory item and reconcile them into
1232 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1233 * one name in a directory item, but the same code gets used for
1234 * both directory index types
1235 */
1236static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1237 struct btrfs_root *root,
1238 struct btrfs_path *path,
1239 struct extent_buffer *eb, int slot,
1240 struct btrfs_key *key)
1241{
1242 int ret;
1243 u32 item_size = btrfs_item_size_nr(eb, slot);
1244 struct btrfs_dir_item *di;
1245 int name_len;
1246 unsigned long ptr;
1247 unsigned long ptr_end;
1248
1249 ptr = btrfs_item_ptr_offset(eb, slot);
1250 ptr_end = ptr + item_size;
d397712b 1251 while (ptr < ptr_end) {
e02119d5
CM
1252 di = (struct btrfs_dir_item *)ptr;
1253 name_len = btrfs_dir_name_len(eb, di);
1254 ret = replay_one_name(trans, root, path, eb, di, key);
1255 BUG_ON(ret);
1256 ptr = (unsigned long)(di + 1);
1257 ptr += name_len;
1258 }
1259 return 0;
1260}
1261
1262/*
1263 * directory replay has two parts. There are the standard directory
1264 * items in the log copied from the subvolume, and range items
1265 * created in the log while the subvolume was logged.
1266 *
1267 * The range items tell us which parts of the key space the log
1268 * is authoritative for. During replay, if a key in the subvolume
1269 * directory is in a logged range item, but not actually in the log
1270 * that means it was deleted from the directory before the fsync
1271 * and should be removed.
1272 */
1273static noinline int find_dir_range(struct btrfs_root *root,
1274 struct btrfs_path *path,
1275 u64 dirid, int key_type,
1276 u64 *start_ret, u64 *end_ret)
1277{
1278 struct btrfs_key key;
1279 u64 found_end;
1280 struct btrfs_dir_log_item *item;
1281 int ret;
1282 int nritems;
1283
1284 if (*start_ret == (u64)-1)
1285 return 1;
1286
1287 key.objectid = dirid;
1288 key.type = key_type;
1289 key.offset = *start_ret;
1290
1291 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1292 if (ret < 0)
1293 goto out;
1294 if (ret > 0) {
1295 if (path->slots[0] == 0)
1296 goto out;
1297 path->slots[0]--;
1298 }
1299 if (ret != 0)
1300 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1301
1302 if (key.type != key_type || key.objectid != dirid) {
1303 ret = 1;
1304 goto next;
1305 }
1306 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1307 struct btrfs_dir_log_item);
1308 found_end = btrfs_dir_log_end(path->nodes[0], item);
1309
1310 if (*start_ret >= key.offset && *start_ret <= found_end) {
1311 ret = 0;
1312 *start_ret = key.offset;
1313 *end_ret = found_end;
1314 goto out;
1315 }
1316 ret = 1;
1317next:
1318 /* check the next slot in the tree to see if it is a valid item */
1319 nritems = btrfs_header_nritems(path->nodes[0]);
1320 if (path->slots[0] >= nritems) {
1321 ret = btrfs_next_leaf(root, path);
1322 if (ret)
1323 goto out;
1324 } else {
1325 path->slots[0]++;
1326 }
1327
1328 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1329
1330 if (key.type != key_type || key.objectid != dirid) {
1331 ret = 1;
1332 goto out;
1333 }
1334 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1335 struct btrfs_dir_log_item);
1336 found_end = btrfs_dir_log_end(path->nodes[0], item);
1337 *start_ret = key.offset;
1338 *end_ret = found_end;
1339 ret = 0;
1340out:
1341 btrfs_release_path(root, path);
1342 return ret;
1343}
1344
1345/*
1346 * this looks for a given directory item in the log. If the directory
1347 * item is not in the log, the item is removed and the inode it points
1348 * to is unlinked
1349 */
1350static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1351 struct btrfs_root *root,
1352 struct btrfs_root *log,
1353 struct btrfs_path *path,
1354 struct btrfs_path *log_path,
1355 struct inode *dir,
1356 struct btrfs_key *dir_key)
1357{
1358 int ret;
1359 struct extent_buffer *eb;
1360 int slot;
1361 u32 item_size;
1362 struct btrfs_dir_item *di;
1363 struct btrfs_dir_item *log_di;
1364 int name_len;
1365 unsigned long ptr;
1366 unsigned long ptr_end;
1367 char *name;
1368 struct inode *inode;
1369 struct btrfs_key location;
1370
1371again:
1372 eb = path->nodes[0];
1373 slot = path->slots[0];
1374 item_size = btrfs_item_size_nr(eb, slot);
1375 ptr = btrfs_item_ptr_offset(eb, slot);
1376 ptr_end = ptr + item_size;
d397712b 1377 while (ptr < ptr_end) {
e02119d5
CM
1378 di = (struct btrfs_dir_item *)ptr;
1379 name_len = btrfs_dir_name_len(eb, di);
1380 name = kmalloc(name_len, GFP_NOFS);
1381 if (!name) {
1382 ret = -ENOMEM;
1383 goto out;
1384 }
1385 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1386 name_len);
1387 log_di = NULL;
12fcfd22 1388 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
e02119d5
CM
1389 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1390 dir_key->objectid,
1391 name, name_len, 0);
12fcfd22 1392 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
e02119d5
CM
1393 log_di = btrfs_lookup_dir_index_item(trans, log,
1394 log_path,
1395 dir_key->objectid,
1396 dir_key->offset,
1397 name, name_len, 0);
1398 }
1399 if (!log_di || IS_ERR(log_di)) {
1400 btrfs_dir_item_key_to_cpu(eb, di, &location);
1401 btrfs_release_path(root, path);
1402 btrfs_release_path(log, log_path);
1403 inode = read_one_inode(root, location.objectid);
1404 BUG_ON(!inode);
1405
1406 ret = link_to_fixup_dir(trans, root,
1407 path, location.objectid);
1408 BUG_ON(ret);
1409 btrfs_inc_nlink(inode);
1410 ret = btrfs_unlink_inode(trans, root, dir, inode,
1411 name, name_len);
1412 BUG_ON(ret);
1413 kfree(name);
1414 iput(inode);
1415
1416 /* there might still be more names under this key
1417 * check and repeat if required
1418 */
1419 ret = btrfs_search_slot(NULL, root, dir_key, path,
1420 0, 0);
1421 if (ret == 0)
1422 goto again;
1423 ret = 0;
1424 goto out;
1425 }
1426 btrfs_release_path(log, log_path);
1427 kfree(name);
1428
1429 ptr = (unsigned long)(di + 1);
1430 ptr += name_len;
1431 }
1432 ret = 0;
1433out:
1434 btrfs_release_path(root, path);
1435 btrfs_release_path(log, log_path);
1436 return ret;
1437}
1438
1439/*
1440 * deletion replay happens before we copy any new directory items
1441 * out of the log or out of backreferences from inodes. It
1442 * scans the log to find ranges of keys that log is authoritative for,
1443 * and then scans the directory to find items in those ranges that are
1444 * not present in the log.
1445 *
1446 * Anything we don't find in the log is unlinked and removed from the
1447 * directory.
1448 */
1449static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1450 struct btrfs_root *root,
1451 struct btrfs_root *log,
1452 struct btrfs_path *path,
12fcfd22 1453 u64 dirid, int del_all)
e02119d5
CM
1454{
1455 u64 range_start;
1456 u64 range_end;
1457 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1458 int ret = 0;
1459 struct btrfs_key dir_key;
1460 struct btrfs_key found_key;
1461 struct btrfs_path *log_path;
1462 struct inode *dir;
1463
1464 dir_key.objectid = dirid;
1465 dir_key.type = BTRFS_DIR_ITEM_KEY;
1466 log_path = btrfs_alloc_path();
1467 if (!log_path)
1468 return -ENOMEM;
1469
1470 dir = read_one_inode(root, dirid);
1471 /* it isn't an error if the inode isn't there, that can happen
1472 * because we replay the deletes before we copy in the inode item
1473 * from the log
1474 */
1475 if (!dir) {
1476 btrfs_free_path(log_path);
1477 return 0;
1478 }
1479again:
1480 range_start = 0;
1481 range_end = 0;
d397712b 1482 while (1) {
12fcfd22
CM
1483 if (del_all)
1484 range_end = (u64)-1;
1485 else {
1486 ret = find_dir_range(log, path, dirid, key_type,
1487 &range_start, &range_end);
1488 if (ret != 0)
1489 break;
1490 }
e02119d5
CM
1491
1492 dir_key.offset = range_start;
d397712b 1493 while (1) {
e02119d5
CM
1494 int nritems;
1495 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1496 0, 0);
1497 if (ret < 0)
1498 goto out;
1499
1500 nritems = btrfs_header_nritems(path->nodes[0]);
1501 if (path->slots[0] >= nritems) {
1502 ret = btrfs_next_leaf(root, path);
1503 if (ret)
1504 break;
1505 }
1506 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1507 path->slots[0]);
1508 if (found_key.objectid != dirid ||
1509 found_key.type != dir_key.type)
1510 goto next_type;
1511
1512 if (found_key.offset > range_end)
1513 break;
1514
1515 ret = check_item_in_log(trans, root, log, path,
12fcfd22
CM
1516 log_path, dir,
1517 &found_key);
e02119d5
CM
1518 BUG_ON(ret);
1519 if (found_key.offset == (u64)-1)
1520 break;
1521 dir_key.offset = found_key.offset + 1;
1522 }
1523 btrfs_release_path(root, path);
1524 if (range_end == (u64)-1)
1525 break;
1526 range_start = range_end + 1;
1527 }
1528
1529next_type:
1530 ret = 0;
1531 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1532 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1533 dir_key.type = BTRFS_DIR_INDEX_KEY;
1534 btrfs_release_path(root, path);
1535 goto again;
1536 }
1537out:
1538 btrfs_release_path(root, path);
1539 btrfs_free_path(log_path);
1540 iput(dir);
1541 return ret;
1542}
1543
1544/*
1545 * the process_func used to replay items from the log tree. This
1546 * gets called in two different stages. The first stage just looks
1547 * for inodes and makes sure they are all copied into the subvolume.
1548 *
1549 * The second stage copies all the other item types from the log into
1550 * the subvolume. The two stage approach is slower, but gets rid of
1551 * lots of complexity around inodes referencing other inodes that exist
1552 * only in the log (references come from either directory items or inode
1553 * back refs).
1554 */
1555static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1556 struct walk_control *wc, u64 gen)
1557{
1558 int nritems;
1559 struct btrfs_path *path;
1560 struct btrfs_root *root = wc->replay_dest;
1561 struct btrfs_key key;
1562 u32 item_size;
1563 int level;
1564 int i;
1565 int ret;
1566
1567 btrfs_read_buffer(eb, gen);
1568
1569 level = btrfs_header_level(eb);
1570
1571 if (level != 0)
1572 return 0;
1573
1574 path = btrfs_alloc_path();
1575 BUG_ON(!path);
1576
1577 nritems = btrfs_header_nritems(eb);
1578 for (i = 0; i < nritems; i++) {
1579 btrfs_item_key_to_cpu(eb, &key, i);
1580 item_size = btrfs_item_size_nr(eb, i);
1581
1582 /* inode keys are done during the first stage */
1583 if (key.type == BTRFS_INODE_ITEM_KEY &&
1584 wc->stage == LOG_WALK_REPLAY_INODES) {
1585 struct inode *inode;
1586 struct btrfs_inode_item *inode_item;
1587 u32 mode;
1588
1589 inode_item = btrfs_item_ptr(eb, i,
1590 struct btrfs_inode_item);
1591 mode = btrfs_inode_mode(eb, inode_item);
1592 if (S_ISDIR(mode)) {
1593 ret = replay_dir_deletes(wc->trans,
12fcfd22 1594 root, log, path, key.objectid, 0);
e02119d5
CM
1595 BUG_ON(ret);
1596 }
1597 ret = overwrite_item(wc->trans, root, path,
1598 eb, i, &key);
1599 BUG_ON(ret);
1600
1601 /* for regular files, truncate away
1602 * extents past the new EOF
1603 */
1604 if (S_ISREG(mode)) {
1605 inode = read_one_inode(root,
1606 key.objectid);
1607 BUG_ON(!inode);
1608
1609 ret = btrfs_truncate_inode_items(wc->trans,
1610 root, inode, inode->i_size,
1611 BTRFS_EXTENT_DATA_KEY);
1612 BUG_ON(ret);
a74ac322
CM
1613
1614 /* if the nlink count is zero here, the iput
1615 * will free the inode. We bump it to make
1616 * sure it doesn't get freed until the link
1617 * count fixup is done
1618 */
1619 if (inode->i_nlink == 0) {
1620 btrfs_inc_nlink(inode);
1621 btrfs_update_inode(wc->trans,
1622 root, inode);
1623 }
e02119d5
CM
1624 iput(inode);
1625 }
1626 ret = link_to_fixup_dir(wc->trans, root,
1627 path, key.objectid);
1628 BUG_ON(ret);
1629 }
1630 if (wc->stage < LOG_WALK_REPLAY_ALL)
1631 continue;
1632
1633 /* these keys are simply copied */
1634 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1635 ret = overwrite_item(wc->trans, root, path,
1636 eb, i, &key);
1637 BUG_ON(ret);
1638 } else if (key.type == BTRFS_INODE_REF_KEY) {
1639 ret = add_inode_ref(wc->trans, root, log, path,
1640 eb, i, &key);
1641 BUG_ON(ret && ret != -ENOENT);
1642 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1643 ret = replay_one_extent(wc->trans, root, path,
1644 eb, i, &key);
1645 BUG_ON(ret);
e02119d5
CM
1646 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1647 key.type == BTRFS_DIR_INDEX_KEY) {
1648 ret = replay_one_dir_item(wc->trans, root, path,
1649 eb, i, &key);
1650 BUG_ON(ret);
1651 }
1652 }
1653 btrfs_free_path(path);
1654 return 0;
1655}
1656
d397712b 1657static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
e02119d5
CM
1658 struct btrfs_root *root,
1659 struct btrfs_path *path, int *level,
1660 struct walk_control *wc)
1661{
1662 u64 root_owner;
1663 u64 root_gen;
1664 u64 bytenr;
1665 u64 ptr_gen;
1666 struct extent_buffer *next;
1667 struct extent_buffer *cur;
1668 struct extent_buffer *parent;
1669 u32 blocksize;
1670 int ret = 0;
1671
1672 WARN_ON(*level < 0);
1673 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1674
d397712b 1675 while (*level > 0) {
e02119d5
CM
1676 WARN_ON(*level < 0);
1677 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1678 cur = path->nodes[*level];
1679
1680 if (btrfs_header_level(cur) != *level)
1681 WARN_ON(1);
1682
1683 if (path->slots[*level] >=
1684 btrfs_header_nritems(cur))
1685 break;
1686
1687 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1688 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1689 blocksize = btrfs_level_size(root, *level - 1);
1690
1691 parent = path->nodes[*level];
1692 root_owner = btrfs_header_owner(parent);
1693 root_gen = btrfs_header_generation(parent);
1694
1695 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
1696
1697 wc->process_func(root, next, wc, ptr_gen);
1698
1699 if (*level == 1) {
1700 path->slots[*level]++;
1701 if (wc->free) {
1702 btrfs_read_buffer(next, ptr_gen);
1703
1704 btrfs_tree_lock(next);
1705 clean_tree_block(trans, root, next);
b4ce94de 1706 btrfs_set_lock_blocking(next);
e02119d5
CM
1707 btrfs_wait_tree_block_writeback(next);
1708 btrfs_tree_unlock(next);
1709
1710 ret = btrfs_drop_leaf_ref(trans, root, next);
1711 BUG_ON(ret);
1712
1713 WARN_ON(root_owner !=
1714 BTRFS_TREE_LOG_OBJECTID);
d00aff00
CM
1715 ret = btrfs_free_reserved_extent(root,
1716 bytenr, blocksize);
e02119d5
CM
1717 BUG_ON(ret);
1718 }
1719 free_extent_buffer(next);
1720 continue;
1721 }
1722 btrfs_read_buffer(next, ptr_gen);
1723
1724 WARN_ON(*level <= 0);
1725 if (path->nodes[*level-1])
1726 free_extent_buffer(path->nodes[*level-1]);
1727 path->nodes[*level-1] = next;
1728 *level = btrfs_header_level(next);
1729 path->slots[*level] = 0;
1730 cond_resched();
1731 }
1732 WARN_ON(*level < 0);
1733 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1734
d397712b 1735 if (path->nodes[*level] == root->node)
e02119d5 1736 parent = path->nodes[*level];
d397712b 1737 else
e02119d5 1738 parent = path->nodes[*level + 1];
d397712b 1739
e02119d5
CM
1740 bytenr = path->nodes[*level]->start;
1741
1742 blocksize = btrfs_level_size(root, *level);
1743 root_owner = btrfs_header_owner(parent);
1744 root_gen = btrfs_header_generation(parent);
1745
1746 wc->process_func(root, path->nodes[*level], wc,
1747 btrfs_header_generation(path->nodes[*level]));
1748
1749 if (wc->free) {
1750 next = path->nodes[*level];
1751 btrfs_tree_lock(next);
1752 clean_tree_block(trans, root, next);
b4ce94de 1753 btrfs_set_lock_blocking(next);
e02119d5
CM
1754 btrfs_wait_tree_block_writeback(next);
1755 btrfs_tree_unlock(next);
1756
1757 if (*level == 0) {
1758 ret = btrfs_drop_leaf_ref(trans, root, next);
1759 BUG_ON(ret);
1760 }
1761 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
d00aff00 1762 ret = btrfs_free_reserved_extent(root, bytenr, blocksize);
e02119d5
CM
1763 BUG_ON(ret);
1764 }
1765 free_extent_buffer(path->nodes[*level]);
1766 path->nodes[*level] = NULL;
1767 *level += 1;
1768
1769 cond_resched();
1770 return 0;
1771}
1772
d397712b 1773static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
e02119d5
CM
1774 struct btrfs_root *root,
1775 struct btrfs_path *path, int *level,
1776 struct walk_control *wc)
1777{
1778 u64 root_owner;
1779 u64 root_gen;
1780 int i;
1781 int slot;
1782 int ret;
1783
d397712b 1784 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
e02119d5
CM
1785 slot = path->slots[i];
1786 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1787 struct extent_buffer *node;
1788 node = path->nodes[i];
1789 path->slots[i]++;
1790 *level = i;
1791 WARN_ON(*level == 0);
1792 return 0;
1793 } else {
31840ae1
ZY
1794 struct extent_buffer *parent;
1795 if (path->nodes[*level] == root->node)
1796 parent = path->nodes[*level];
1797 else
1798 parent = path->nodes[*level + 1];
1799
1800 root_owner = btrfs_header_owner(parent);
1801 root_gen = btrfs_header_generation(parent);
e02119d5
CM
1802 wc->process_func(root, path->nodes[*level], wc,
1803 btrfs_header_generation(path->nodes[*level]));
1804 if (wc->free) {
1805 struct extent_buffer *next;
1806
1807 next = path->nodes[*level];
1808
1809 btrfs_tree_lock(next);
1810 clean_tree_block(trans, root, next);
b4ce94de 1811 btrfs_set_lock_blocking(next);
e02119d5
CM
1812 btrfs_wait_tree_block_writeback(next);
1813 btrfs_tree_unlock(next);
1814
1815 if (*level == 0) {
1816 ret = btrfs_drop_leaf_ref(trans, root,
1817 next);
1818 BUG_ON(ret);
1819 }
1820
1821 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
d00aff00 1822 ret = btrfs_free_reserved_extent(root,
e02119d5 1823 path->nodes[*level]->start,
d00aff00 1824 path->nodes[*level]->len);
e02119d5
CM
1825 BUG_ON(ret);
1826 }
1827 free_extent_buffer(path->nodes[*level]);
1828 path->nodes[*level] = NULL;
1829 *level = i + 1;
1830 }
1831 }
1832 return 1;
1833}
1834
1835/*
1836 * drop the reference count on the tree rooted at 'snap'. This traverses
1837 * the tree freeing any blocks that have a ref count of zero after being
1838 * decremented.
1839 */
1840static int walk_log_tree(struct btrfs_trans_handle *trans,
1841 struct btrfs_root *log, struct walk_control *wc)
1842{
1843 int ret = 0;
1844 int wret;
1845 int level;
1846 struct btrfs_path *path;
1847 int i;
1848 int orig_level;
1849
1850 path = btrfs_alloc_path();
1851 BUG_ON(!path);
1852
1853 level = btrfs_header_level(log->node);
1854 orig_level = level;
1855 path->nodes[level] = log->node;
1856 extent_buffer_get(log->node);
1857 path->slots[level] = 0;
1858
d397712b 1859 while (1) {
e02119d5
CM
1860 wret = walk_down_log_tree(trans, log, path, &level, wc);
1861 if (wret > 0)
1862 break;
1863 if (wret < 0)
1864 ret = wret;
1865
1866 wret = walk_up_log_tree(trans, log, path, &level, wc);
1867 if (wret > 0)
1868 break;
1869 if (wret < 0)
1870 ret = wret;
1871 }
1872
1873 /* was the root node processed? if not, catch it here */
1874 if (path->nodes[orig_level]) {
1875 wc->process_func(log, path->nodes[orig_level], wc,
1876 btrfs_header_generation(path->nodes[orig_level]));
1877 if (wc->free) {
1878 struct extent_buffer *next;
1879
1880 next = path->nodes[orig_level];
1881
1882 btrfs_tree_lock(next);
1883 clean_tree_block(trans, log, next);
b4ce94de 1884 btrfs_set_lock_blocking(next);
e02119d5
CM
1885 btrfs_wait_tree_block_writeback(next);
1886 btrfs_tree_unlock(next);
1887
1888 if (orig_level == 0) {
1889 ret = btrfs_drop_leaf_ref(trans, log,
1890 next);
1891 BUG_ON(ret);
1892 }
1893 WARN_ON(log->root_key.objectid !=
1894 BTRFS_TREE_LOG_OBJECTID);
d00aff00
CM
1895 ret = btrfs_free_reserved_extent(log, next->start,
1896 next->len);
e02119d5
CM
1897 BUG_ON(ret);
1898 }
1899 }
1900
1901 for (i = 0; i <= orig_level; i++) {
1902 if (path->nodes[i]) {
1903 free_extent_buffer(path->nodes[i]);
1904 path->nodes[i] = NULL;
1905 }
1906 }
1907 btrfs_free_path(path);
e02119d5
CM
1908 return ret;
1909}
1910
7237f183
YZ
1911/*
1912 * helper function to update the item for a given subvolumes log root
1913 * in the tree of log roots
1914 */
1915static int update_log_root(struct btrfs_trans_handle *trans,
1916 struct btrfs_root *log)
1917{
1918 int ret;
1919
1920 if (log->log_transid == 1) {
1921 /* insert root item on the first sync */
1922 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1923 &log->root_key, &log->root_item);
1924 } else {
1925 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1926 &log->root_key, &log->root_item);
1927 }
1928 return ret;
1929}
1930
12fcfd22
CM
1931static int wait_log_commit(struct btrfs_trans_handle *trans,
1932 struct btrfs_root *root, unsigned long transid)
e02119d5
CM
1933{
1934 DEFINE_WAIT(wait);
7237f183 1935 int index = transid % 2;
e02119d5 1936
7237f183
YZ
1937 /*
1938 * we only allow two pending log transactions at a time,
1939 * so we know that if ours is more than 2 older than the
1940 * current transaction, we're done
1941 */
e02119d5 1942 do {
7237f183
YZ
1943 prepare_to_wait(&root->log_commit_wait[index],
1944 &wait, TASK_UNINTERRUPTIBLE);
1945 mutex_unlock(&root->log_mutex);
12fcfd22
CM
1946
1947 if (root->fs_info->last_trans_log_full_commit !=
1948 trans->transid && root->log_transid < transid + 2 &&
7237f183
YZ
1949 atomic_read(&root->log_commit[index]))
1950 schedule();
12fcfd22 1951
7237f183
YZ
1952 finish_wait(&root->log_commit_wait[index], &wait);
1953 mutex_lock(&root->log_mutex);
1954 } while (root->log_transid < transid + 2 &&
1955 atomic_read(&root->log_commit[index]));
1956 return 0;
1957}
1958
12fcfd22
CM
1959static int wait_for_writer(struct btrfs_trans_handle *trans,
1960 struct btrfs_root *root)
7237f183
YZ
1961{
1962 DEFINE_WAIT(wait);
1963 while (atomic_read(&root->log_writers)) {
1964 prepare_to_wait(&root->log_writer_wait,
1965 &wait, TASK_UNINTERRUPTIBLE);
1966 mutex_unlock(&root->log_mutex);
12fcfd22
CM
1967 if (root->fs_info->last_trans_log_full_commit !=
1968 trans->transid && atomic_read(&root->log_writers))
e02119d5 1969 schedule();
7237f183
YZ
1970 mutex_lock(&root->log_mutex);
1971 finish_wait(&root->log_writer_wait, &wait);
1972 }
e02119d5
CM
1973 return 0;
1974}
1975
1976/*
1977 * btrfs_sync_log does sends a given tree log down to the disk and
1978 * updates the super blocks to record it. When this call is done,
12fcfd22
CM
1979 * you know that any inodes previously logged are safely on disk only
1980 * if it returns 0.
1981 *
1982 * Any other return value means you need to call btrfs_commit_transaction.
1983 * Some of the edge cases for fsyncing directories that have had unlinks
1984 * or renames done in the past mean that sometimes the only safe
1985 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
1986 * that has happened.
e02119d5
CM
1987 */
1988int btrfs_sync_log(struct btrfs_trans_handle *trans,
1989 struct btrfs_root *root)
1990{
7237f183
YZ
1991 int index1;
1992 int index2;
e02119d5 1993 int ret;
e02119d5 1994 struct btrfs_root *log = root->log_root;
7237f183 1995 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
e02119d5 1996
7237f183
YZ
1997 mutex_lock(&root->log_mutex);
1998 index1 = root->log_transid % 2;
1999 if (atomic_read(&root->log_commit[index1])) {
12fcfd22 2000 wait_log_commit(trans, root, root->log_transid);
7237f183
YZ
2001 mutex_unlock(&root->log_mutex);
2002 return 0;
e02119d5 2003 }
7237f183
YZ
2004 atomic_set(&root->log_commit[index1], 1);
2005
2006 /* wait for previous tree log sync to complete */
2007 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
12fcfd22 2008 wait_log_commit(trans, root, root->log_transid - 1);
e02119d5 2009
d397712b 2010 while (1) {
7237f183
YZ
2011 unsigned long batch = root->log_batch;
2012 mutex_unlock(&root->log_mutex);
e02119d5 2013 schedule_timeout_uninterruptible(1);
7237f183 2014 mutex_lock(&root->log_mutex);
12fcfd22
CM
2015
2016 wait_for_writer(trans, root);
7237f183 2017 if (batch == root->log_batch)
e02119d5
CM
2018 break;
2019 }
e02119d5 2020
12fcfd22
CM
2021 /* bail out if we need to do a full commit */
2022 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2023 ret = -EAGAIN;
2024 mutex_unlock(&root->log_mutex);
2025 goto out;
2026 }
2027
d0c803c4 2028 ret = btrfs_write_and_wait_marked_extents(log, &log->dirty_log_pages);
e02119d5 2029 BUG_ON(ret);
7237f183
YZ
2030
2031 btrfs_set_root_bytenr(&log->root_item, log->node->start);
2032 btrfs_set_root_generation(&log->root_item, trans->transid);
2033 btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
2034
2035 root->log_batch = 0;
2036 root->log_transid++;
2037 log->log_transid = root->log_transid;
2038 smp_mb();
2039 /*
2040 * log tree has been flushed to disk, new modifications of
2041 * the log will be written to new positions. so it's safe to
2042 * allow log writers to go in.
2043 */
2044 mutex_unlock(&root->log_mutex);
2045
2046 mutex_lock(&log_root_tree->log_mutex);
2047 log_root_tree->log_batch++;
2048 atomic_inc(&log_root_tree->log_writers);
2049 mutex_unlock(&log_root_tree->log_mutex);
2050
2051 ret = update_log_root(trans, log);
2052 BUG_ON(ret);
2053
2054 mutex_lock(&log_root_tree->log_mutex);
2055 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2056 smp_mb();
2057 if (waitqueue_active(&log_root_tree->log_writer_wait))
2058 wake_up(&log_root_tree->log_writer_wait);
2059 }
2060
2061 index2 = log_root_tree->log_transid % 2;
2062 if (atomic_read(&log_root_tree->log_commit[index2])) {
12fcfd22
CM
2063 wait_log_commit(trans, log_root_tree,
2064 log_root_tree->log_transid);
7237f183
YZ
2065 mutex_unlock(&log_root_tree->log_mutex);
2066 goto out;
2067 }
2068 atomic_set(&log_root_tree->log_commit[index2], 1);
2069
12fcfd22
CM
2070 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2071 wait_log_commit(trans, log_root_tree,
2072 log_root_tree->log_transid - 1);
2073 }
2074
2075 wait_for_writer(trans, log_root_tree);
7237f183 2076
12fcfd22
CM
2077 /*
2078 * now that we've moved on to the tree of log tree roots,
2079 * check the full commit flag again
2080 */
2081 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2082 mutex_unlock(&log_root_tree->log_mutex);
2083 ret = -EAGAIN;
2084 goto out_wake_log_root;
2085 }
7237f183
YZ
2086
2087 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
2088 &log_root_tree->dirty_log_pages);
e02119d5
CM
2089 BUG_ON(ret);
2090
2091 btrfs_set_super_log_root(&root->fs_info->super_for_commit,
7237f183 2092 log_root_tree->node->start);
e02119d5 2093 btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
7237f183 2094 btrfs_header_level(log_root_tree->node));
e02119d5 2095
7237f183
YZ
2096 log_root_tree->log_batch = 0;
2097 log_root_tree->log_transid++;
e02119d5 2098 smp_mb();
7237f183
YZ
2099
2100 mutex_unlock(&log_root_tree->log_mutex);
2101
2102 /*
2103 * nobody else is going to jump in and write the the ctree
2104 * super here because the log_commit atomic below is protecting
2105 * us. We must be called with a transaction handle pinning
2106 * the running transaction open, so a full commit can't hop
2107 * in and cause problems either.
2108 */
2109 write_ctree_super(trans, root->fs_info->tree_root, 2);
12fcfd22 2110 ret = 0;
7237f183 2111
12fcfd22 2112out_wake_log_root:
7237f183
YZ
2113 atomic_set(&log_root_tree->log_commit[index2], 0);
2114 smp_mb();
2115 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2116 wake_up(&log_root_tree->log_commit_wait[index2]);
e02119d5 2117out:
7237f183
YZ
2118 atomic_set(&root->log_commit[index1], 0);
2119 smp_mb();
2120 if (waitqueue_active(&root->log_commit_wait[index1]))
2121 wake_up(&root->log_commit_wait[index1]);
e02119d5 2122 return 0;
e02119d5
CM
2123}
2124
12fcfd22
CM
2125/*
2126 * free all the extents used by the tree log. This should be called
e02119d5
CM
2127 * at commit time of the full transaction
2128 */
2129int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2130{
2131 int ret;
2132 struct btrfs_root *log;
2133 struct key;
d0c803c4
CM
2134 u64 start;
2135 u64 end;
e02119d5
CM
2136 struct walk_control wc = {
2137 .free = 1,
2138 .process_func = process_one_buffer
2139 };
2140
07d400a6 2141 if (!root->log_root || root->fs_info->log_root_recovering)
e02119d5
CM
2142 return 0;
2143
2144 log = root->log_root;
2145 ret = walk_log_tree(trans, log, &wc);
2146 BUG_ON(ret);
2147
d397712b 2148 while (1) {
d0c803c4
CM
2149 ret = find_first_extent_bit(&log->dirty_log_pages,
2150 0, &start, &end, EXTENT_DIRTY);
2151 if (ret)
2152 break;
2153
2154 clear_extent_dirty(&log->dirty_log_pages,
2155 start, end, GFP_NOFS);
2156 }
2157
7237f183
YZ
2158 if (log->log_transid > 0) {
2159 ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
2160 &log->root_key);
2161 BUG_ON(ret);
2162 }
e02119d5 2163 root->log_root = NULL;
7237f183
YZ
2164 free_extent_buffer(log->node);
2165 kfree(log);
e02119d5
CM
2166 return 0;
2167}
2168
e02119d5
CM
2169/*
2170 * If both a file and directory are logged, and unlinks or renames are
2171 * mixed in, we have a few interesting corners:
2172 *
2173 * create file X in dir Y
2174 * link file X to X.link in dir Y
2175 * fsync file X
2176 * unlink file X but leave X.link
2177 * fsync dir Y
2178 *
2179 * After a crash we would expect only X.link to exist. But file X
2180 * didn't get fsync'd again so the log has back refs for X and X.link.
2181 *
2182 * We solve this by removing directory entries and inode backrefs from the
2183 * log when a file that was logged in the current transaction is
2184 * unlinked. Any later fsync will include the updated log entries, and
2185 * we'll be able to reconstruct the proper directory items from backrefs.
2186 *
2187 * This optimizations allows us to avoid relogging the entire inode
2188 * or the entire directory.
2189 */
2190int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2191 struct btrfs_root *root,
2192 const char *name, int name_len,
2193 struct inode *dir, u64 index)
2194{
2195 struct btrfs_root *log;
2196 struct btrfs_dir_item *di;
2197 struct btrfs_path *path;
2198 int ret;
2199 int bytes_del = 0;
2200
3a5f1d45
CM
2201 if (BTRFS_I(dir)->logged_trans < trans->transid)
2202 return 0;
2203
e02119d5
CM
2204 ret = join_running_log_trans(root);
2205 if (ret)
2206 return 0;
2207
2208 mutex_lock(&BTRFS_I(dir)->log_mutex);
2209
2210 log = root->log_root;
2211 path = btrfs_alloc_path();
2212 di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
2213 name, name_len, -1);
2214 if (di && !IS_ERR(di)) {
2215 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2216 bytes_del += name_len;
2217 BUG_ON(ret);
2218 }
2219 btrfs_release_path(log, path);
2220 di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
2221 index, name, name_len, -1);
2222 if (di && !IS_ERR(di)) {
2223 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2224 bytes_del += name_len;
2225 BUG_ON(ret);
2226 }
2227
2228 /* update the directory size in the log to reflect the names
2229 * we have removed
2230 */
2231 if (bytes_del) {
2232 struct btrfs_key key;
2233
2234 key.objectid = dir->i_ino;
2235 key.offset = 0;
2236 key.type = BTRFS_INODE_ITEM_KEY;
2237 btrfs_release_path(log, path);
2238
2239 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
2240 if (ret == 0) {
2241 struct btrfs_inode_item *item;
2242 u64 i_size;
2243
2244 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2245 struct btrfs_inode_item);
2246 i_size = btrfs_inode_size(path->nodes[0], item);
2247 if (i_size > bytes_del)
2248 i_size -= bytes_del;
2249 else
2250 i_size = 0;
2251 btrfs_set_inode_size(path->nodes[0], item, i_size);
2252 btrfs_mark_buffer_dirty(path->nodes[0]);
2253 } else
2254 ret = 0;
2255 btrfs_release_path(log, path);
2256 }
2257
2258 btrfs_free_path(path);
2259 mutex_unlock(&BTRFS_I(dir)->log_mutex);
12fcfd22 2260 btrfs_end_log_trans(root);
e02119d5
CM
2261
2262 return 0;
2263}
2264
2265/* see comments for btrfs_del_dir_entries_in_log */
2266int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2267 struct btrfs_root *root,
2268 const char *name, int name_len,
2269 struct inode *inode, u64 dirid)
2270{
2271 struct btrfs_root *log;
2272 u64 index;
2273 int ret;
2274
3a5f1d45
CM
2275 if (BTRFS_I(inode)->logged_trans < trans->transid)
2276 return 0;
2277
e02119d5
CM
2278 ret = join_running_log_trans(root);
2279 if (ret)
2280 return 0;
2281 log = root->log_root;
2282 mutex_lock(&BTRFS_I(inode)->log_mutex);
2283
2284 ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
2285 dirid, &index);
2286 mutex_unlock(&BTRFS_I(inode)->log_mutex);
12fcfd22 2287 btrfs_end_log_trans(root);
e02119d5 2288
e02119d5
CM
2289 return ret;
2290}
2291
2292/*
2293 * creates a range item in the log for 'dirid'. first_offset and
2294 * last_offset tell us which parts of the key space the log should
2295 * be considered authoritative for.
2296 */
2297static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2298 struct btrfs_root *log,
2299 struct btrfs_path *path,
2300 int key_type, u64 dirid,
2301 u64 first_offset, u64 last_offset)
2302{
2303 int ret;
2304 struct btrfs_key key;
2305 struct btrfs_dir_log_item *item;
2306
2307 key.objectid = dirid;
2308 key.offset = first_offset;
2309 if (key_type == BTRFS_DIR_ITEM_KEY)
2310 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2311 else
2312 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2313 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
2314 BUG_ON(ret);
2315
2316 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2317 struct btrfs_dir_log_item);
2318 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2319 btrfs_mark_buffer_dirty(path->nodes[0]);
2320 btrfs_release_path(log, path);
2321 return 0;
2322}
2323
2324/*
2325 * log all the items included in the current transaction for a given
2326 * directory. This also creates the range items in the log tree required
2327 * to replay anything deleted before the fsync
2328 */
2329static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2330 struct btrfs_root *root, struct inode *inode,
2331 struct btrfs_path *path,
2332 struct btrfs_path *dst_path, int key_type,
2333 u64 min_offset, u64 *last_offset_ret)
2334{
2335 struct btrfs_key min_key;
2336 struct btrfs_key max_key;
2337 struct btrfs_root *log = root->log_root;
2338 struct extent_buffer *src;
2339 int ret;
2340 int i;
2341 int nritems;
2342 u64 first_offset = min_offset;
2343 u64 last_offset = (u64)-1;
2344
2345 log = root->log_root;
2346 max_key.objectid = inode->i_ino;
2347 max_key.offset = (u64)-1;
2348 max_key.type = key_type;
2349
2350 min_key.objectid = inode->i_ino;
2351 min_key.type = key_type;
2352 min_key.offset = min_offset;
2353
2354 path->keep_locks = 1;
2355
2356 ret = btrfs_search_forward(root, &min_key, &max_key,
2357 path, 0, trans->transid);
2358
2359 /*
2360 * we didn't find anything from this transaction, see if there
2361 * is anything at all
2362 */
2363 if (ret != 0 || min_key.objectid != inode->i_ino ||
2364 min_key.type != key_type) {
2365 min_key.objectid = inode->i_ino;
2366 min_key.type = key_type;
2367 min_key.offset = (u64)-1;
2368 btrfs_release_path(root, path);
2369 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2370 if (ret < 0) {
2371 btrfs_release_path(root, path);
2372 return ret;
2373 }
2374 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2375
2376 /* if ret == 0 there are items for this type,
2377 * create a range to tell us the last key of this type.
2378 * otherwise, there are no items in this directory after
2379 * *min_offset, and we create a range to indicate that.
2380 */
2381 if (ret == 0) {
2382 struct btrfs_key tmp;
2383 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2384 path->slots[0]);
d397712b 2385 if (key_type == tmp.type)
e02119d5 2386 first_offset = max(min_offset, tmp.offset) + 1;
e02119d5
CM
2387 }
2388 goto done;
2389 }
2390
2391 /* go backward to find any previous key */
2392 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2393 if (ret == 0) {
2394 struct btrfs_key tmp;
2395 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2396 if (key_type == tmp.type) {
2397 first_offset = tmp.offset;
2398 ret = overwrite_item(trans, log, dst_path,
2399 path->nodes[0], path->slots[0],
2400 &tmp);
2401 }
2402 }
2403 btrfs_release_path(root, path);
2404
2405 /* find the first key from this transaction again */
2406 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2407 if (ret != 0) {
2408 WARN_ON(1);
2409 goto done;
2410 }
2411
2412 /*
2413 * we have a block from this transaction, log every item in it
2414 * from our directory
2415 */
d397712b 2416 while (1) {
e02119d5
CM
2417 struct btrfs_key tmp;
2418 src = path->nodes[0];
2419 nritems = btrfs_header_nritems(src);
2420 for (i = path->slots[0]; i < nritems; i++) {
2421 btrfs_item_key_to_cpu(src, &min_key, i);
2422
2423 if (min_key.objectid != inode->i_ino ||
2424 min_key.type != key_type)
2425 goto done;
2426 ret = overwrite_item(trans, log, dst_path, src, i,
2427 &min_key);
2428 BUG_ON(ret);
2429 }
2430 path->slots[0] = nritems;
2431
2432 /*
2433 * look ahead to the next item and see if it is also
2434 * from this directory and from this transaction
2435 */
2436 ret = btrfs_next_leaf(root, path);
2437 if (ret == 1) {
2438 last_offset = (u64)-1;
2439 goto done;
2440 }
2441 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2442 if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
2443 last_offset = (u64)-1;
2444 goto done;
2445 }
2446 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2447 ret = overwrite_item(trans, log, dst_path,
2448 path->nodes[0], path->slots[0],
2449 &tmp);
2450
2451 BUG_ON(ret);
2452 last_offset = tmp.offset;
2453 goto done;
2454 }
2455 }
2456done:
2457 *last_offset_ret = last_offset;
2458 btrfs_release_path(root, path);
2459 btrfs_release_path(log, dst_path);
2460
2461 /* insert the log range keys to indicate where the log is valid */
2462 ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino,
2463 first_offset, last_offset);
2464 BUG_ON(ret);
2465 return 0;
2466}
2467
2468/*
2469 * logging directories is very similar to logging inodes, We find all the items
2470 * from the current transaction and write them to the log.
2471 *
2472 * The recovery code scans the directory in the subvolume, and if it finds a
2473 * key in the range logged that is not present in the log tree, then it means
2474 * that dir entry was unlinked during the transaction.
2475 *
2476 * In order for that scan to work, we must include one key smaller than
2477 * the smallest logged by this transaction and one key larger than the largest
2478 * key logged by this transaction.
2479 */
2480static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2481 struct btrfs_root *root, struct inode *inode,
2482 struct btrfs_path *path,
2483 struct btrfs_path *dst_path)
2484{
2485 u64 min_key;
2486 u64 max_key;
2487 int ret;
2488 int key_type = BTRFS_DIR_ITEM_KEY;
2489
2490again:
2491 min_key = 0;
2492 max_key = 0;
d397712b 2493 while (1) {
e02119d5
CM
2494 ret = log_dir_items(trans, root, inode, path,
2495 dst_path, key_type, min_key,
2496 &max_key);
2497 BUG_ON(ret);
2498 if (max_key == (u64)-1)
2499 break;
2500 min_key = max_key + 1;
2501 }
2502
2503 if (key_type == BTRFS_DIR_ITEM_KEY) {
2504 key_type = BTRFS_DIR_INDEX_KEY;
2505 goto again;
2506 }
2507 return 0;
2508}
2509
2510/*
2511 * a helper function to drop items from the log before we relog an
2512 * inode. max_key_type indicates the highest item type to remove.
2513 * This cannot be run for file data extents because it does not
2514 * free the extents they point to.
2515 */
2516static int drop_objectid_items(struct btrfs_trans_handle *trans,
2517 struct btrfs_root *log,
2518 struct btrfs_path *path,
2519 u64 objectid, int max_key_type)
2520{
2521 int ret;
2522 struct btrfs_key key;
2523 struct btrfs_key found_key;
2524
2525 key.objectid = objectid;
2526 key.type = max_key_type;
2527 key.offset = (u64)-1;
2528
d397712b 2529 while (1) {
e02119d5
CM
2530 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
2531
2532 if (ret != 1)
2533 break;
2534
2535 if (path->slots[0] == 0)
2536 break;
2537
2538 path->slots[0]--;
2539 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2540 path->slots[0]);
2541
2542 if (found_key.objectid != objectid)
2543 break;
2544
2545 ret = btrfs_del_item(trans, log, path);
2546 BUG_ON(ret);
2547 btrfs_release_path(log, path);
2548 }
2549 btrfs_release_path(log, path);
2550 return 0;
2551}
2552
31ff1cd2
CM
2553static noinline int copy_items(struct btrfs_trans_handle *trans,
2554 struct btrfs_root *log,
2555 struct btrfs_path *dst_path,
2556 struct extent_buffer *src,
2557 int start_slot, int nr, int inode_only)
2558{
2559 unsigned long src_offset;
2560 unsigned long dst_offset;
2561 struct btrfs_file_extent_item *extent;
2562 struct btrfs_inode_item *inode_item;
2563 int ret;
2564 struct btrfs_key *ins_keys;
2565 u32 *ins_sizes;
2566 char *ins_data;
2567 int i;
d20f7043
CM
2568 struct list_head ordered_sums;
2569
2570 INIT_LIST_HEAD(&ordered_sums);
31ff1cd2
CM
2571
2572 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2573 nr * sizeof(u32), GFP_NOFS);
2574 ins_sizes = (u32 *)ins_data;
2575 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2576
2577 for (i = 0; i < nr; i++) {
2578 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2579 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2580 }
2581 ret = btrfs_insert_empty_items(trans, log, dst_path,
2582 ins_keys, ins_sizes, nr);
2583 BUG_ON(ret);
2584
2585 for (i = 0; i < nr; i++) {
2586 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2587 dst_path->slots[0]);
2588
2589 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2590
2591 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2592 src_offset, ins_sizes[i]);
2593
2594 if (inode_only == LOG_INODE_EXISTS &&
2595 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2596 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2597 dst_path->slots[0],
2598 struct btrfs_inode_item);
2599 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2600
2601 /* set the generation to zero so the recover code
2602 * can tell the difference between an logging
2603 * just to say 'this inode exists' and a logging
2604 * to say 'update this inode with these values'
2605 */
2606 btrfs_set_inode_generation(dst_path->nodes[0],
2607 inode_item, 0);
2608 }
2609 /* take a reference on file data extents so that truncates
2610 * or deletes of this inode don't have to relog the inode
2611 * again
2612 */
2613 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2614 int found_type;
2615 extent = btrfs_item_ptr(src, start_slot + i,
2616 struct btrfs_file_extent_item);
2617
2618 found_type = btrfs_file_extent_type(src, extent);
d899e052
YZ
2619 if (found_type == BTRFS_FILE_EXTENT_REG ||
2620 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
31ff1cd2
CM
2621 u64 ds = btrfs_file_extent_disk_bytenr(src,
2622 extent);
2623 u64 dl = btrfs_file_extent_disk_num_bytes(src,
2624 extent);
d20f7043
CM
2625 u64 cs = btrfs_file_extent_offset(src, extent);
2626 u64 cl = btrfs_file_extent_num_bytes(src,
2627 extent);;
580afd76
CM
2628 if (btrfs_file_extent_compression(src,
2629 extent)) {
2630 cs = 0;
2631 cl = dl;
2632 }
31ff1cd2
CM
2633 /* ds == 0 is a hole */
2634 if (ds != 0) {
2635 ret = btrfs_inc_extent_ref(trans, log,
2636 ds, dl,
31840ae1 2637 dst_path->nodes[0]->start,
31ff1cd2 2638 BTRFS_TREE_LOG_OBJECTID,
31840ae1 2639 trans->transid,
3bb1a1bc 2640 ins_keys[i].objectid);
31ff1cd2 2641 BUG_ON(ret);
07d400a6
YZ
2642 ret = btrfs_lookup_csums_range(
2643 log->fs_info->csum_root,
2644 ds + cs, ds + cs + cl - 1,
2645 &ordered_sums);
d20f7043 2646 BUG_ON(ret);
31ff1cd2
CM
2647 }
2648 }
2649 }
2650 dst_path->slots[0]++;
2651 }
2652
2653 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2654 btrfs_release_path(log, dst_path);
2655 kfree(ins_data);
d20f7043
CM
2656
2657 /*
2658 * we have to do this after the loop above to avoid changing the
2659 * log tree while trying to change the log tree.
2660 */
d397712b 2661 while (!list_empty(&ordered_sums)) {
d20f7043
CM
2662 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2663 struct btrfs_ordered_sum,
2664 list);
2665 ret = btrfs_csum_file_blocks(trans, log, sums);
2666 BUG_ON(ret);
2667 list_del(&sums->list);
2668 kfree(sums);
2669 }
31ff1cd2
CM
2670 return 0;
2671}
2672
e02119d5
CM
2673/* log a single inode in the tree log.
2674 * At least one parent directory for this inode must exist in the tree
2675 * or be logged already.
2676 *
2677 * Any items from this inode changed by the current transaction are copied
2678 * to the log tree. An extra reference is taken on any extents in this
2679 * file, allowing us to avoid a whole pile of corner cases around logging
2680 * blocks that have been removed from the tree.
2681 *
2682 * See LOG_INODE_ALL and related defines for a description of what inode_only
2683 * does.
2684 *
2685 * This handles both files and directories.
2686 */
12fcfd22 2687static int btrfs_log_inode(struct btrfs_trans_handle *trans,
e02119d5
CM
2688 struct btrfs_root *root, struct inode *inode,
2689 int inode_only)
2690{
2691 struct btrfs_path *path;
2692 struct btrfs_path *dst_path;
2693 struct btrfs_key min_key;
2694 struct btrfs_key max_key;
2695 struct btrfs_root *log = root->log_root;
31ff1cd2 2696 struct extent_buffer *src = NULL;
e02119d5
CM
2697 u32 size;
2698 int ret;
3a5f1d45 2699 int nritems;
31ff1cd2
CM
2700 int ins_start_slot = 0;
2701 int ins_nr;
e02119d5
CM
2702
2703 log = root->log_root;
2704
2705 path = btrfs_alloc_path();
2706 dst_path = btrfs_alloc_path();
2707
2708 min_key.objectid = inode->i_ino;
2709 min_key.type = BTRFS_INODE_ITEM_KEY;
2710 min_key.offset = 0;
2711
2712 max_key.objectid = inode->i_ino;
12fcfd22
CM
2713
2714 /* today the code can only do partial logging of directories */
2715 if (!S_ISDIR(inode->i_mode))
2716 inode_only = LOG_INODE_ALL;
2717
e02119d5
CM
2718 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2719 max_key.type = BTRFS_XATTR_ITEM_KEY;
2720 else
2721 max_key.type = (u8)-1;
2722 max_key.offset = (u64)-1;
2723
e02119d5
CM
2724 mutex_lock(&BTRFS_I(inode)->log_mutex);
2725
2726 /*
2727 * a brute force approach to making sure we get the most uptodate
2728 * copies of everything.
2729 */
2730 if (S_ISDIR(inode->i_mode)) {
2731 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2732
2733 if (inode_only == LOG_INODE_EXISTS)
2734 max_key_type = BTRFS_XATTR_ITEM_KEY;
2735 ret = drop_objectid_items(trans, log, path,
2736 inode->i_ino, max_key_type);
2737 } else {
2738 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2739 }
2740 BUG_ON(ret);
2741 path->keep_locks = 1;
2742
d397712b 2743 while (1) {
31ff1cd2 2744 ins_nr = 0;
e02119d5
CM
2745 ret = btrfs_search_forward(root, &min_key, &max_key,
2746 path, 0, trans->transid);
2747 if (ret != 0)
2748 break;
3a5f1d45 2749again:
31ff1cd2 2750 /* note, ins_nr might be > 0 here, cleanup outside the loop */
e02119d5
CM
2751 if (min_key.objectid != inode->i_ino)
2752 break;
2753 if (min_key.type > max_key.type)
2754 break;
31ff1cd2 2755
e02119d5
CM
2756 src = path->nodes[0];
2757 size = btrfs_item_size_nr(src, path->slots[0]);
31ff1cd2
CM
2758 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2759 ins_nr++;
2760 goto next_slot;
2761 } else if (!ins_nr) {
2762 ins_start_slot = path->slots[0];
2763 ins_nr = 1;
2764 goto next_slot;
e02119d5
CM
2765 }
2766
31ff1cd2
CM
2767 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2768 ins_nr, inode_only);
2769 BUG_ON(ret);
2770 ins_nr = 1;
2771 ins_start_slot = path->slots[0];
2772next_slot:
e02119d5 2773
3a5f1d45
CM
2774 nritems = btrfs_header_nritems(path->nodes[0]);
2775 path->slots[0]++;
2776 if (path->slots[0] < nritems) {
2777 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2778 path->slots[0]);
2779 goto again;
2780 }
31ff1cd2
CM
2781 if (ins_nr) {
2782 ret = copy_items(trans, log, dst_path, src,
2783 ins_start_slot,
2784 ins_nr, inode_only);
2785 BUG_ON(ret);
2786 ins_nr = 0;
2787 }
3a5f1d45
CM
2788 btrfs_release_path(root, path);
2789
e02119d5
CM
2790 if (min_key.offset < (u64)-1)
2791 min_key.offset++;
2792 else if (min_key.type < (u8)-1)
2793 min_key.type++;
2794 else if (min_key.objectid < (u64)-1)
2795 min_key.objectid++;
2796 else
2797 break;
2798 }
31ff1cd2
CM
2799 if (ins_nr) {
2800 ret = copy_items(trans, log, dst_path, src,
2801 ins_start_slot,
2802 ins_nr, inode_only);
2803 BUG_ON(ret);
2804 ins_nr = 0;
2805 }
2806 WARN_ON(ins_nr);
9623f9a3 2807 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
e02119d5
CM
2808 btrfs_release_path(root, path);
2809 btrfs_release_path(log, dst_path);
2810 ret = log_directory_changes(trans, root, inode, path, dst_path);
2811 BUG_ON(ret);
2812 }
3a5f1d45 2813 BTRFS_I(inode)->logged_trans = trans->transid;
e02119d5
CM
2814 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2815
2816 btrfs_free_path(path);
2817 btrfs_free_path(dst_path);
e02119d5
CM
2818 return 0;
2819}
2820
12fcfd22
CM
2821/*
2822 * follow the dentry parent pointers up the chain and see if any
2823 * of the directories in it require a full commit before they can
2824 * be logged. Returns zero if nothing special needs to be done or 1 if
2825 * a full commit is required.
2826 */
2827static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
2828 struct inode *inode,
2829 struct dentry *parent,
2830 struct super_block *sb,
2831 u64 last_committed)
e02119d5 2832{
12fcfd22
CM
2833 int ret = 0;
2834 struct btrfs_root *root;
e02119d5 2835
af4176b4
CM
2836 /*
2837 * for regular files, if its inode is already on disk, we don't
2838 * have to worry about the parents at all. This is because
2839 * we can use the last_unlink_trans field to record renames
2840 * and other fun in this file.
2841 */
2842 if (S_ISREG(inode->i_mode) &&
2843 BTRFS_I(inode)->generation <= last_committed &&
2844 BTRFS_I(inode)->last_unlink_trans <= last_committed)
2845 goto out;
2846
12fcfd22
CM
2847 if (!S_ISDIR(inode->i_mode)) {
2848 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2849 goto out;
2850 inode = parent->d_inode;
2851 }
2852
2853 while (1) {
2854 BTRFS_I(inode)->logged_trans = trans->transid;
2855 smp_mb();
2856
2857 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
2858 root = BTRFS_I(inode)->root;
2859
2860 /*
2861 * make sure any commits to the log are forced
2862 * to be full commits
2863 */
2864 root->fs_info->last_trans_log_full_commit =
2865 trans->transid;
2866 ret = 1;
2867 break;
2868 }
2869
2870 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
2871 break;
2872
2873 if (parent == sb->s_root)
2874 break;
2875
2876 parent = parent->d_parent;
2877 inode = parent->d_inode;
2878
2879 }
2880out:
e02119d5
CM
2881 return ret;
2882}
2883
2884/*
2885 * helper function around btrfs_log_inode to make sure newly created
2886 * parent directories also end up in the log. A minimal inode and backref
2887 * only logging is done of any parent directories that are older than
2888 * the last committed transaction
2889 */
12fcfd22
CM
2890int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
2891 struct btrfs_root *root, struct inode *inode,
2892 struct dentry *parent, int exists_only)
e02119d5 2893{
12fcfd22 2894 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
e02119d5 2895 struct super_block *sb;
12fcfd22
CM
2896 int ret = 0;
2897 u64 last_committed = root->fs_info->last_trans_committed;
2898
2899 sb = inode->i_sb;
2900
3a5e1404
SW
2901 if (btrfs_test_opt(root, NOTREELOG)) {
2902 ret = 1;
2903 goto end_no_trans;
2904 }
2905
12fcfd22
CM
2906 if (root->fs_info->last_trans_log_full_commit >
2907 root->fs_info->last_trans_committed) {
2908 ret = 1;
2909 goto end_no_trans;
2910 }
2911
2912 ret = check_parent_dirs_for_sync(trans, inode, parent,
2913 sb, last_committed);
2914 if (ret)
2915 goto end_no_trans;
e02119d5
CM
2916
2917 start_log_trans(trans, root);
e02119d5 2918
12fcfd22
CM
2919 ret = btrfs_log_inode(trans, root, inode, inode_only);
2920 BUG_ON(ret);
12fcfd22 2921
af4176b4
CM
2922 /*
2923 * for regular files, if its inode is already on disk, we don't
2924 * have to worry about the parents at all. This is because
2925 * we can use the last_unlink_trans field to record renames
2926 * and other fun in this file.
2927 */
2928 if (S_ISREG(inode->i_mode) &&
2929 BTRFS_I(inode)->generation <= last_committed &&
2930 BTRFS_I(inode)->last_unlink_trans <= last_committed)
2931 goto no_parent;
2932
2933 inode_only = LOG_INODE_EXISTS;
12fcfd22
CM
2934 while (1) {
2935 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
e02119d5
CM
2936 break;
2937
12fcfd22
CM
2938 inode = parent->d_inode;
2939 if (BTRFS_I(inode)->generation >
2940 root->fs_info->last_trans_committed) {
2941 ret = btrfs_log_inode(trans, root, inode, inode_only);
2942 BUG_ON(ret);
2943 }
2944 if (parent == sb->s_root)
e02119d5 2945 break;
12fcfd22
CM
2946
2947 parent = parent->d_parent;
e02119d5 2948 }
af4176b4 2949no_parent:
12fcfd22
CM
2950 ret = 0;
2951 btrfs_end_log_trans(root);
2952end_no_trans:
2953 return ret;
e02119d5
CM
2954}
2955
2956/*
2957 * it is not safe to log dentry if the chunk root has added new
2958 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
2959 * If this returns 1, you must commit the transaction to safely get your
2960 * data on disk.
2961 */
2962int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
2963 struct btrfs_root *root, struct dentry *dentry)
2964{
12fcfd22
CM
2965 return btrfs_log_inode_parent(trans, root, dentry->d_inode,
2966 dentry->d_parent, 0);
e02119d5
CM
2967}
2968
2969/*
2970 * should be called during mount to recover any replay any log trees
2971 * from the FS
2972 */
2973int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
2974{
2975 int ret;
2976 struct btrfs_path *path;
2977 struct btrfs_trans_handle *trans;
2978 struct btrfs_key key;
2979 struct btrfs_key found_key;
2980 struct btrfs_key tmp_key;
2981 struct btrfs_root *log;
2982 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
8d5bf1cb 2983 u64 highest_inode;
e02119d5
CM
2984 struct walk_control wc = {
2985 .process_func = process_one_buffer,
2986 .stage = 0,
2987 };
2988
2989 fs_info->log_root_recovering = 1;
2990 path = btrfs_alloc_path();
2991 BUG_ON(!path);
2992
2993 trans = btrfs_start_transaction(fs_info->tree_root, 1);
2994
2995 wc.trans = trans;
2996 wc.pin = 1;
2997
2998 walk_log_tree(trans, log_root_tree, &wc);
2999
3000again:
3001 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3002 key.offset = (u64)-1;
3003 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3004
d397712b 3005 while (1) {
e02119d5
CM
3006 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3007 if (ret < 0)
3008 break;
3009 if (ret > 0) {
3010 if (path->slots[0] == 0)
3011 break;
3012 path->slots[0]--;
3013 }
3014 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3015 path->slots[0]);
3016 btrfs_release_path(log_root_tree, path);
3017 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3018 break;
3019
3020 log = btrfs_read_fs_root_no_radix(log_root_tree,
3021 &found_key);
3022 BUG_ON(!log);
3023
3024
3025 tmp_key.objectid = found_key.offset;
3026 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3027 tmp_key.offset = (u64)-1;
3028
3029 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
e02119d5
CM
3030 BUG_ON(!wc.replay_dest);
3031
07d400a6 3032 wc.replay_dest->log_root = log;
24562425 3033 mutex_lock(&fs_info->trans_mutex);
e02119d5 3034 btrfs_record_root_in_trans(wc.replay_dest);
24562425 3035 mutex_unlock(&fs_info->trans_mutex);
e02119d5
CM
3036 ret = walk_log_tree(trans, log, &wc);
3037 BUG_ON(ret);
3038
3039 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3040 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3041 path);
3042 BUG_ON(ret);
3043 }
8d5bf1cb
CM
3044 ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode);
3045 if (ret == 0) {
3046 wc.replay_dest->highest_inode = highest_inode;
3047 wc.replay_dest->last_inode_alloc = highest_inode;
3048 }
e02119d5
CM
3049
3050 key.offset = found_key.offset - 1;
07d400a6 3051 wc.replay_dest->log_root = NULL;
e02119d5
CM
3052 free_extent_buffer(log->node);
3053 kfree(log);
3054
3055 if (found_key.offset == 0)
3056 break;
3057 }
3058 btrfs_release_path(log_root_tree, path);
3059
3060 /* step one is to pin it all, step two is to replay just inodes */
3061 if (wc.pin) {
3062 wc.pin = 0;
3063 wc.process_func = replay_one_buffer;
3064 wc.stage = LOG_WALK_REPLAY_INODES;
3065 goto again;
3066 }
3067 /* step three is to replay everything */
3068 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3069 wc.stage++;
3070 goto again;
3071 }
3072
3073 btrfs_free_path(path);
3074
3075 free_extent_buffer(log_root_tree->node);
3076 log_root_tree->log_root = NULL;
3077 fs_info->log_root_recovering = 0;
3078
3079 /* step 4: commit the transaction, which also unpins the blocks */
3080 btrfs_commit_transaction(trans, fs_info->tree_root);
3081
3082 kfree(log_root_tree);
3083 return 0;
3084}
12fcfd22
CM
3085
3086/*
3087 * there are some corner cases where we want to force a full
3088 * commit instead of allowing a directory to be logged.
3089 *
3090 * They revolve around files there were unlinked from the directory, and
3091 * this function updates the parent directory so that a full commit is
3092 * properly done if it is fsync'd later after the unlinks are done.
3093 */
3094void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3095 struct inode *dir, struct inode *inode,
3096 int for_rename)
3097{
af4176b4
CM
3098 /*
3099 * when we're logging a file, if it hasn't been renamed
3100 * or unlinked, and its inode is fully committed on disk,
3101 * we don't have to worry about walking up the directory chain
3102 * to log its parents.
3103 *
3104 * So, we use the last_unlink_trans field to put this transid
3105 * into the file. When the file is logged we check it and
3106 * don't log the parents if the file is fully on disk.
3107 */
3108 if (S_ISREG(inode->i_mode))
3109 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3110
12fcfd22
CM
3111 /*
3112 * if this directory was already logged any new
3113 * names for this file/dir will get recorded
3114 */
3115 smp_mb();
3116 if (BTRFS_I(dir)->logged_trans == trans->transid)
3117 return;
3118
3119 /*
3120 * if the inode we're about to unlink was logged,
3121 * the log will be properly updated for any new names
3122 */
3123 if (BTRFS_I(inode)->logged_trans == trans->transid)
3124 return;
3125
3126 /*
3127 * when renaming files across directories, if the directory
3128 * there we're unlinking from gets fsync'd later on, there's
3129 * no way to find the destination directory later and fsync it
3130 * properly. So, we have to be conservative and force commits
3131 * so the new name gets discovered.
3132 */
3133 if (for_rename)
3134 goto record;
3135
3136 /* we can safely do the unlink without any special recording */
3137 return;
3138
3139record:
3140 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3141}
3142
3143/*
3144 * Call this after adding a new name for a file and it will properly
3145 * update the log to reflect the new name.
3146 *
3147 * It will return zero if all goes well, and it will return 1 if a
3148 * full transaction commit is required.
3149 */
3150int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3151 struct inode *inode, struct inode *old_dir,
3152 struct dentry *parent)
3153{
3154 struct btrfs_root * root = BTRFS_I(inode)->root;
3155
af4176b4
CM
3156 /*
3157 * this will force the logging code to walk the dentry chain
3158 * up for the file
3159 */
3160 if (S_ISREG(inode->i_mode))
3161 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3162
12fcfd22
CM
3163 /*
3164 * if this inode hasn't been logged and directory we're renaming it
3165 * from hasn't been logged, we don't need to log it
3166 */
3167 if (BTRFS_I(inode)->logged_trans <=
3168 root->fs_info->last_trans_committed &&
3169 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3170 root->fs_info->last_trans_committed))
3171 return 0;
3172
3173 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3174}
3175