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