]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/btrfs/tree-log.c
Btrfs: Dir fsync optimizations
[linux-2.6-omap-h63xx.git] / 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         BTRFS_I(inode)->index_cnt = (u64)-1;
1031
1032         return 0;
1033 }
1034
1035 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1036                                             struct btrfs_root *root,
1037                                             struct btrfs_path *path)
1038 {
1039         int ret;
1040         struct btrfs_key key;
1041         struct inode *inode;
1042
1043         key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1044         key.type = BTRFS_ORPHAN_ITEM_KEY;
1045         key.offset = (u64)-1;
1046         while(1) {
1047                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1048                 if (ret < 0)
1049                         break;
1050
1051                 if (ret == 1) {
1052                         if (path->slots[0] == 0)
1053                                 break;
1054                         path->slots[0]--;
1055                 }
1056
1057                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1058                 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1059                     key.type != BTRFS_ORPHAN_ITEM_KEY)
1060                         break;
1061
1062                 ret = btrfs_del_item(trans, root, path);
1063                 BUG_ON(ret);
1064
1065                 btrfs_release_path(root, path);
1066                 inode = read_one_inode(root, key.offset);
1067                 BUG_ON(!inode);
1068
1069                 ret = fixup_inode_link_count(trans, root, inode);
1070                 BUG_ON(ret);
1071
1072                 iput(inode);
1073
1074                 if (key.offset == 0)
1075                         break;
1076                 key.offset--;
1077         }
1078         btrfs_release_path(root, path);
1079         return 0;
1080 }
1081
1082
1083 /*
1084  * record a given inode in the fixup dir so we can check its link
1085  * count when replay is done.  The link count is incremented here
1086  * so the inode won't go away until we check it
1087  */
1088 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1089                                       struct btrfs_root *root,
1090                                       struct btrfs_path *path,
1091                                       u64 objectid)
1092 {
1093         struct btrfs_key key;
1094         int ret = 0;
1095         struct inode *inode;
1096
1097         inode = read_one_inode(root, objectid);
1098         BUG_ON(!inode);
1099
1100         key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1101         btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1102         key.offset = objectid;
1103
1104         ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1105
1106         btrfs_release_path(root, path);
1107         if (ret == 0) {
1108                 btrfs_inc_nlink(inode);
1109                 btrfs_update_inode(trans, root, inode);
1110         } else if (ret == -EEXIST) {
1111                 ret = 0;
1112         } else {
1113                 BUG();
1114         }
1115         iput(inode);
1116
1117         return ret;
1118 }
1119
1120 /*
1121  * when replaying the log for a directory, we only insert names
1122  * for inodes that actually exist.  This means an fsync on a directory
1123  * does not implicitly fsync all the new files in it
1124  */
1125 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1126                                     struct btrfs_root *root,
1127                                     struct btrfs_path *path,
1128                                     u64 dirid, u64 index,
1129                                     char *name, int name_len, u8 type,
1130                                     struct btrfs_key *location)
1131 {
1132         struct inode *inode;
1133         struct inode *dir;
1134         int ret;
1135
1136         inode = read_one_inode(root, location->objectid);
1137         if (!inode)
1138                 return -ENOENT;
1139
1140         dir = read_one_inode(root, dirid);
1141         if (!dir) {
1142                 iput(inode);
1143                 return -EIO;
1144         }
1145         ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1146
1147         /* FIXME, put inode into FIXUP list */
1148
1149         iput(inode);
1150         iput(dir);
1151         return ret;
1152 }
1153
1154 /*
1155  * take a single entry in a log directory item and replay it into
1156  * the subvolume.
1157  *
1158  * if a conflicting item exists in the subdirectory already,
1159  * the inode it points to is unlinked and put into the link count
1160  * fix up tree.
1161  *
1162  * If a name from the log points to a file or directory that does
1163  * not exist in the FS, it is skipped.  fsyncs on directories
1164  * do not force down inodes inside that directory, just changes to the
1165  * names or unlinks in a directory.
1166  */
1167 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1168                                     struct btrfs_root *root,
1169                                     struct btrfs_path *path,
1170                                     struct extent_buffer *eb,
1171                                     struct btrfs_dir_item *di,
1172                                     struct btrfs_key *key)
1173 {
1174         char *name;
1175         int name_len;
1176         struct btrfs_dir_item *dst_di;
1177         struct btrfs_key found_key;
1178         struct btrfs_key log_key;
1179         struct inode *dir;
1180         u8 log_type;
1181         int exists;
1182         int ret;
1183
1184         dir = read_one_inode(root, key->objectid);
1185         BUG_ON(!dir);
1186
1187         name_len = btrfs_dir_name_len(eb, di);
1188         name = kmalloc(name_len, GFP_NOFS);
1189         log_type = btrfs_dir_type(eb, di);
1190         read_extent_buffer(eb, name, (unsigned long)(di + 1),
1191                    name_len);
1192
1193         btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1194         exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1195         if (exists == 0)
1196                 exists = 1;
1197         else
1198                 exists = 0;
1199         btrfs_release_path(root, path);
1200
1201         if (key->type == BTRFS_DIR_ITEM_KEY) {
1202                 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1203                                        name, name_len, 1);
1204         }
1205         else if (key->type == BTRFS_DIR_INDEX_KEY) {
1206                 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1207                                                      key->objectid,
1208                                                      key->offset, name,
1209                                                      name_len, 1);
1210         } else {
1211                 BUG();
1212         }
1213         if (!dst_di || IS_ERR(dst_di)) {
1214                 /* we need a sequence number to insert, so we only
1215                  * do inserts for the BTRFS_DIR_INDEX_KEY types
1216                  */
1217                 if (key->type != BTRFS_DIR_INDEX_KEY)
1218                         goto out;
1219                 goto insert;
1220         }
1221
1222         btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1223         /* the existing item matches the logged item */
1224         if (found_key.objectid == log_key.objectid &&
1225             found_key.type == log_key.type &&
1226             found_key.offset == log_key.offset &&
1227             btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1228                 goto out;
1229         }
1230
1231         /*
1232          * don't drop the conflicting directory entry if the inode
1233          * for the new entry doesn't exist
1234          */
1235         if (!exists)
1236                 goto out;
1237
1238         ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1239         BUG_ON(ret);
1240
1241         if (key->type == BTRFS_DIR_INDEX_KEY)
1242                 goto insert;
1243 out:
1244         btrfs_release_path(root, path);
1245         kfree(name);
1246         iput(dir);
1247         return 0;
1248
1249 insert:
1250         btrfs_release_path(root, path);
1251         ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1252                               name, name_len, log_type, &log_key);
1253
1254         if (ret && ret != -ENOENT)
1255                 BUG();
1256         goto out;
1257 }
1258
1259 /*
1260  * find all the names in a directory item and reconcile them into
1261  * the subvolume.  Only BTRFS_DIR_ITEM_KEY types will have more than
1262  * one name in a directory item, but the same code gets used for
1263  * both directory index types
1264  */
1265 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1266                                         struct btrfs_root *root,
1267                                         struct btrfs_path *path,
1268                                         struct extent_buffer *eb, int slot,
1269                                         struct btrfs_key *key)
1270 {
1271         int ret;
1272         u32 item_size = btrfs_item_size_nr(eb, slot);
1273         struct btrfs_dir_item *di;
1274         int name_len;
1275         unsigned long ptr;
1276         unsigned long ptr_end;
1277
1278         ptr = btrfs_item_ptr_offset(eb, slot);
1279         ptr_end = ptr + item_size;
1280         while(ptr < ptr_end) {
1281                 di = (struct btrfs_dir_item *)ptr;
1282                 name_len = btrfs_dir_name_len(eb, di);
1283                 ret = replay_one_name(trans, root, path, eb, di, key);
1284                 BUG_ON(ret);
1285                 ptr = (unsigned long)(di + 1);
1286                 ptr += name_len;
1287         }
1288         return 0;
1289 }
1290
1291 /*
1292  * directory replay has two parts.  There are the standard directory
1293  * items in the log copied from the subvolume, and range items
1294  * created in the log while the subvolume was logged.
1295  *
1296  * The range items tell us which parts of the key space the log
1297  * is authoritative for.  During replay, if a key in the subvolume
1298  * directory is in a logged range item, but not actually in the log
1299  * that means it was deleted from the directory before the fsync
1300  * and should be removed.
1301  */
1302 static noinline int find_dir_range(struct btrfs_root *root,
1303                                    struct btrfs_path *path,
1304                                    u64 dirid, int key_type,
1305                                    u64 *start_ret, u64 *end_ret)
1306 {
1307         struct btrfs_key key;
1308         u64 found_end;
1309         struct btrfs_dir_log_item *item;
1310         int ret;
1311         int nritems;
1312
1313         if (*start_ret == (u64)-1)
1314                 return 1;
1315
1316         key.objectid = dirid;
1317         key.type = key_type;
1318         key.offset = *start_ret;
1319
1320         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1321         if (ret < 0)
1322                 goto out;
1323         if (ret > 0) {
1324                 if (path->slots[0] == 0)
1325                         goto out;
1326                 path->slots[0]--;
1327         }
1328         if (ret != 0)
1329                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1330
1331         if (key.type != key_type || key.objectid != dirid) {
1332                 ret = 1;
1333                 goto next;
1334         }
1335         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1336                               struct btrfs_dir_log_item);
1337         found_end = btrfs_dir_log_end(path->nodes[0], item);
1338
1339         if (*start_ret >= key.offset && *start_ret <= found_end) {
1340                 ret = 0;
1341                 *start_ret = key.offset;
1342                 *end_ret = found_end;
1343                 goto out;
1344         }
1345         ret = 1;
1346 next:
1347         /* check the next slot in the tree to see if it is a valid item */
1348         nritems = btrfs_header_nritems(path->nodes[0]);
1349         if (path->slots[0] >= nritems) {
1350                 ret = btrfs_next_leaf(root, path);
1351                 if (ret)
1352                         goto out;
1353         } else {
1354                 path->slots[0]++;
1355         }
1356
1357         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1358
1359         if (key.type != key_type || key.objectid != dirid) {
1360                 ret = 1;
1361                 goto out;
1362         }
1363         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1364                               struct btrfs_dir_log_item);
1365         found_end = btrfs_dir_log_end(path->nodes[0], item);
1366         *start_ret = key.offset;
1367         *end_ret = found_end;
1368         ret = 0;
1369 out:
1370         btrfs_release_path(root, path);
1371         return ret;
1372 }
1373
1374 /*
1375  * this looks for a given directory item in the log.  If the directory
1376  * item is not in the log, the item is removed and the inode it points
1377  * to is unlinked
1378  */
1379 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1380                                       struct btrfs_root *root,
1381                                       struct btrfs_root *log,
1382                                       struct btrfs_path *path,
1383                                       struct btrfs_path *log_path,
1384                                       struct inode *dir,
1385                                       struct btrfs_key *dir_key)
1386 {
1387         int ret;
1388         struct extent_buffer *eb;
1389         int slot;
1390         u32 item_size;
1391         struct btrfs_dir_item *di;
1392         struct btrfs_dir_item *log_di;
1393         int name_len;
1394         unsigned long ptr;
1395         unsigned long ptr_end;
1396         char *name;
1397         struct inode *inode;
1398         struct btrfs_key location;
1399
1400 again:
1401         eb = path->nodes[0];
1402         slot = path->slots[0];
1403         item_size = btrfs_item_size_nr(eb, slot);
1404         ptr = btrfs_item_ptr_offset(eb, slot);
1405         ptr_end = ptr + item_size;
1406         while(ptr < ptr_end) {
1407                 di = (struct btrfs_dir_item *)ptr;
1408                 name_len = btrfs_dir_name_len(eb, di);
1409                 name = kmalloc(name_len, GFP_NOFS);
1410                 if (!name) {
1411                         ret = -ENOMEM;
1412                         goto out;
1413                 }
1414                 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1415                                   name_len);
1416                 log_di = NULL;
1417                 if (dir_key->type == BTRFS_DIR_ITEM_KEY) {
1418                         log_di = btrfs_lookup_dir_item(trans, log, log_path,
1419                                                        dir_key->objectid,
1420                                                        name, name_len, 0);
1421                 } else if (dir_key->type == BTRFS_DIR_INDEX_KEY) {
1422                         log_di = btrfs_lookup_dir_index_item(trans, log,
1423                                                      log_path,
1424                                                      dir_key->objectid,
1425                                                      dir_key->offset,
1426                                                      name, name_len, 0);
1427                 }
1428                 if (!log_di || IS_ERR(log_di)) {
1429                         btrfs_dir_item_key_to_cpu(eb, di, &location);
1430                         btrfs_release_path(root, path);
1431                         btrfs_release_path(log, log_path);
1432                         inode = read_one_inode(root, location.objectid);
1433                         BUG_ON(!inode);
1434
1435                         ret = link_to_fixup_dir(trans, root,
1436                                                 path, location.objectid);
1437                         BUG_ON(ret);
1438                         btrfs_inc_nlink(inode);
1439                         ret = btrfs_unlink_inode(trans, root, dir, inode,
1440                                                  name, name_len);
1441                         BUG_ON(ret);
1442                         kfree(name);
1443                         iput(inode);
1444
1445                         /* there might still be more names under this key
1446                          * check and repeat if required
1447                          */
1448                         ret = btrfs_search_slot(NULL, root, dir_key, path,
1449                                                 0, 0);
1450                         if (ret == 0)
1451                                 goto again;
1452                         ret = 0;
1453                         goto out;
1454                 }
1455                 btrfs_release_path(log, log_path);
1456                 kfree(name);
1457
1458                 ptr = (unsigned long)(di + 1);
1459                 ptr += name_len;
1460         }
1461         ret = 0;
1462 out:
1463         btrfs_release_path(root, path);
1464         btrfs_release_path(log, log_path);
1465         return ret;
1466 }
1467
1468 /*
1469  * deletion replay happens before we copy any new directory items
1470  * out of the log or out of backreferences from inodes.  It
1471  * scans the log to find ranges of keys that log is authoritative for,
1472  * and then scans the directory to find items in those ranges that are
1473  * not present in the log.
1474  *
1475  * Anything we don't find in the log is unlinked and removed from the
1476  * directory.
1477  */
1478 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1479                                        struct btrfs_root *root,
1480                                        struct btrfs_root *log,
1481                                        struct btrfs_path *path,
1482                                        u64 dirid)
1483 {
1484         u64 range_start;
1485         u64 range_end;
1486         int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1487         int ret = 0;
1488         struct btrfs_key dir_key;
1489         struct btrfs_key found_key;
1490         struct btrfs_path *log_path;
1491         struct inode *dir;
1492
1493         dir_key.objectid = dirid;
1494         dir_key.type = BTRFS_DIR_ITEM_KEY;
1495         log_path = btrfs_alloc_path();
1496         if (!log_path)
1497                 return -ENOMEM;
1498
1499         dir = read_one_inode(root, dirid);
1500         /* it isn't an error if the inode isn't there, that can happen
1501          * because we replay the deletes before we copy in the inode item
1502          * from the log
1503          */
1504         if (!dir) {
1505                 btrfs_free_path(log_path);
1506                 return 0;
1507         }
1508 again:
1509         range_start = 0;
1510         range_end = 0;
1511         while(1) {
1512                 ret = find_dir_range(log, path, dirid, key_type,
1513                                      &range_start, &range_end);
1514                 if (ret != 0)
1515                         break;
1516
1517                 dir_key.offset = range_start;
1518                 while(1) {
1519                         int nritems;
1520                         ret = btrfs_search_slot(NULL, root, &dir_key, path,
1521                                                 0, 0);
1522                         if (ret < 0)
1523                                 goto out;
1524
1525                         nritems = btrfs_header_nritems(path->nodes[0]);
1526                         if (path->slots[0] >= nritems) {
1527                                 ret = btrfs_next_leaf(root, path);
1528                                 if (ret)
1529                                         break;
1530                         }
1531                         btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1532                                               path->slots[0]);
1533                         if (found_key.objectid != dirid ||
1534                             found_key.type != dir_key.type)
1535                                 goto next_type;
1536
1537                         if (found_key.offset > range_end)
1538                                 break;
1539
1540                         ret = check_item_in_log(trans, root, log, path,
1541                                                 log_path, dir, &found_key);
1542                         BUG_ON(ret);
1543                         if (found_key.offset == (u64)-1)
1544                                 break;
1545                         dir_key.offset = found_key.offset + 1;
1546                 }
1547                 btrfs_release_path(root, path);
1548                 if (range_end == (u64)-1)
1549                         break;
1550                 range_start = range_end + 1;
1551         }
1552
1553 next_type:
1554         ret = 0;
1555         if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1556                 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1557                 dir_key.type = BTRFS_DIR_INDEX_KEY;
1558                 btrfs_release_path(root, path);
1559                 goto again;
1560         }
1561 out:
1562         btrfs_release_path(root, path);
1563         btrfs_free_path(log_path);
1564         iput(dir);
1565         return ret;
1566 }
1567
1568 /*
1569  * the process_func used to replay items from the log tree.  This
1570  * gets called in two different stages.  The first stage just looks
1571  * for inodes and makes sure they are all copied into the subvolume.
1572  *
1573  * The second stage copies all the other item types from the log into
1574  * the subvolume.  The two stage approach is slower, but gets rid of
1575  * lots of complexity around inodes referencing other inodes that exist
1576  * only in the log (references come from either directory items or inode
1577  * back refs).
1578  */
1579 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1580                              struct walk_control *wc, u64 gen)
1581 {
1582         int nritems;
1583         struct btrfs_path *path;
1584         struct btrfs_root *root = wc->replay_dest;
1585         struct btrfs_key key;
1586         u32 item_size;
1587         int level;
1588         int i;
1589         int ret;
1590
1591         btrfs_read_buffer(eb, gen);
1592
1593         level = btrfs_header_level(eb);
1594
1595         if (level != 0)
1596                 return 0;
1597
1598         path = btrfs_alloc_path();
1599         BUG_ON(!path);
1600
1601         nritems = btrfs_header_nritems(eb);
1602         for (i = 0; i < nritems; i++) {
1603                 btrfs_item_key_to_cpu(eb, &key, i);
1604                 item_size = btrfs_item_size_nr(eb, i);
1605
1606                 /* inode keys are done during the first stage */
1607                 if (key.type == BTRFS_INODE_ITEM_KEY &&
1608                     wc->stage == LOG_WALK_REPLAY_INODES) {
1609                         struct inode *inode;
1610                         struct btrfs_inode_item *inode_item;
1611                         u32 mode;
1612
1613                         inode_item = btrfs_item_ptr(eb, i,
1614                                             struct btrfs_inode_item);
1615                         mode = btrfs_inode_mode(eb, inode_item);
1616                         if (S_ISDIR(mode)) {
1617                                 ret = replay_dir_deletes(wc->trans,
1618                                          root, log, path, key.objectid);
1619                                 BUG_ON(ret);
1620                         }
1621                         ret = overwrite_item(wc->trans, root, path,
1622                                              eb, i, &key);
1623                         BUG_ON(ret);
1624
1625                         /* for regular files, truncate away
1626                          * extents past the new EOF
1627                          */
1628                         if (S_ISREG(mode)) {
1629                                 inode = read_one_inode(root,
1630                                                        key.objectid);
1631                                 BUG_ON(!inode);
1632
1633                                 ret = btrfs_truncate_inode_items(wc->trans,
1634                                         root, inode, inode->i_size,
1635                                         BTRFS_EXTENT_DATA_KEY);
1636                                 BUG_ON(ret);
1637                                 iput(inode);
1638                         }
1639                         ret = link_to_fixup_dir(wc->trans, root,
1640                                                 path, key.objectid);
1641                         BUG_ON(ret);
1642                 }
1643                 if (wc->stage < LOG_WALK_REPLAY_ALL)
1644                         continue;
1645
1646                 /* these keys are simply copied */
1647                 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1648                         ret = overwrite_item(wc->trans, root, path,
1649                                              eb, i, &key);
1650                         BUG_ON(ret);
1651                 } else if (key.type == BTRFS_INODE_REF_KEY) {
1652                         ret = add_inode_ref(wc->trans, root, log, path,
1653                                             eb, i, &key);
1654                         BUG_ON(ret && ret != -ENOENT);
1655                 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1656                         ret = replay_one_extent(wc->trans, root, path,
1657                                                 eb, i, &key);
1658                         BUG_ON(ret);
1659                 } else if (key.type == BTRFS_CSUM_ITEM_KEY) {
1660                         ret = replay_one_csum(wc->trans, root, path,
1661                                               eb, i, &key);
1662                         BUG_ON(ret);
1663                 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1664                            key.type == BTRFS_DIR_INDEX_KEY) {
1665                         ret = replay_one_dir_item(wc->trans, root, path,
1666                                                   eb, i, &key);
1667                         BUG_ON(ret);
1668                 }
1669         }
1670         btrfs_free_path(path);
1671         return 0;
1672 }
1673
1674 static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
1675                                    struct btrfs_root *root,
1676                                    struct btrfs_path *path, int *level,
1677                                    struct walk_control *wc)
1678 {
1679         u64 root_owner;
1680         u64 root_gen;
1681         u64 bytenr;
1682         u64 ptr_gen;
1683         struct extent_buffer *next;
1684         struct extent_buffer *cur;
1685         struct extent_buffer *parent;
1686         u32 blocksize;
1687         int ret = 0;
1688
1689         WARN_ON(*level < 0);
1690         WARN_ON(*level >= BTRFS_MAX_LEVEL);
1691
1692         while(*level > 0) {
1693                 WARN_ON(*level < 0);
1694                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1695                 cur = path->nodes[*level];
1696
1697                 if (btrfs_header_level(cur) != *level)
1698                         WARN_ON(1);
1699
1700                 if (path->slots[*level] >=
1701                     btrfs_header_nritems(cur))
1702                         break;
1703
1704                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1705                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1706                 blocksize = btrfs_level_size(root, *level - 1);
1707
1708                 parent = path->nodes[*level];
1709                 root_owner = btrfs_header_owner(parent);
1710                 root_gen = btrfs_header_generation(parent);
1711
1712                 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
1713
1714                 wc->process_func(root, next, wc, ptr_gen);
1715
1716                 if (*level == 1) {
1717                         path->slots[*level]++;
1718                         if (wc->free) {
1719                                 btrfs_read_buffer(next, ptr_gen);
1720
1721                                 btrfs_tree_lock(next);
1722                                 clean_tree_block(trans, root, next);
1723                                 btrfs_wait_tree_block_writeback(next);
1724                                 btrfs_tree_unlock(next);
1725
1726                                 ret = btrfs_drop_leaf_ref(trans, root, next);
1727                                 BUG_ON(ret);
1728
1729                                 WARN_ON(root_owner !=
1730                                         BTRFS_TREE_LOG_OBJECTID);
1731                                 ret = btrfs_free_extent(trans, root, bytenr,
1732                                                         blocksize, root_owner,
1733                                                         root_gen, 0, 0, 1);
1734                                 BUG_ON(ret);
1735                         }
1736                         free_extent_buffer(next);
1737                         continue;
1738                 }
1739                 btrfs_read_buffer(next, ptr_gen);
1740
1741                 WARN_ON(*level <= 0);
1742                 if (path->nodes[*level-1])
1743                         free_extent_buffer(path->nodes[*level-1]);
1744                 path->nodes[*level-1] = next;
1745                 *level = btrfs_header_level(next);
1746                 path->slots[*level] = 0;
1747                 cond_resched();
1748         }
1749         WARN_ON(*level < 0);
1750         WARN_ON(*level >= BTRFS_MAX_LEVEL);
1751
1752         if (path->nodes[*level] == root->node) {
1753                 parent = path->nodes[*level];
1754         } else {
1755                 parent = path->nodes[*level + 1];
1756         }
1757         bytenr = path->nodes[*level]->start;
1758
1759         blocksize = btrfs_level_size(root, *level);
1760         root_owner = btrfs_header_owner(parent);
1761         root_gen = btrfs_header_generation(parent);
1762
1763         wc->process_func(root, path->nodes[*level], wc,
1764                          btrfs_header_generation(path->nodes[*level]));
1765
1766         if (wc->free) {
1767                 next = path->nodes[*level];
1768                 btrfs_tree_lock(next);
1769                 clean_tree_block(trans, root, next);
1770                 btrfs_wait_tree_block_writeback(next);
1771                 btrfs_tree_unlock(next);
1772
1773                 if (*level == 0) {
1774                         ret = btrfs_drop_leaf_ref(trans, root, next);
1775                         BUG_ON(ret);
1776                 }
1777                 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
1778                 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
1779                                           root_owner, root_gen, 0, 0, 1);
1780                 BUG_ON(ret);
1781         }
1782         free_extent_buffer(path->nodes[*level]);
1783         path->nodes[*level] = NULL;
1784         *level += 1;
1785
1786         cond_resched();
1787         return 0;
1788 }
1789
1790 static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans,
1791                                  struct btrfs_root *root,
1792                                  struct btrfs_path *path, int *level,
1793                                  struct walk_control *wc)
1794 {
1795         u64 root_owner;
1796         u64 root_gen;
1797         int i;
1798         int slot;
1799         int ret;
1800
1801         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1802                 slot = path->slots[i];
1803                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1804                         struct extent_buffer *node;
1805                         node = path->nodes[i];
1806                         path->slots[i]++;
1807                         *level = i;
1808                         WARN_ON(*level == 0);
1809                         return 0;
1810                 } else {
1811                         if (path->nodes[*level] == root->node) {
1812                                 root_owner = root->root_key.objectid;
1813                                 root_gen =
1814                                    btrfs_header_generation(path->nodes[*level]);
1815                         } else {
1816                                 struct extent_buffer *node;
1817                                 node = path->nodes[*level + 1];
1818                                 root_owner = btrfs_header_owner(node);
1819                                 root_gen = btrfs_header_generation(node);
1820                         }
1821                         wc->process_func(root, path->nodes[*level], wc,
1822                                  btrfs_header_generation(path->nodes[*level]));
1823                         if (wc->free) {
1824                                 struct extent_buffer *next;
1825
1826                                 next = path->nodes[*level];
1827
1828                                 btrfs_tree_lock(next);
1829                                 clean_tree_block(trans, root, next);
1830                                 btrfs_wait_tree_block_writeback(next);
1831                                 btrfs_tree_unlock(next);
1832
1833                                 if (*level == 0) {
1834                                         ret = btrfs_drop_leaf_ref(trans, root,
1835                                                                   next);
1836                                         BUG_ON(ret);
1837                                 }
1838
1839                                 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
1840                                 ret = btrfs_free_extent(trans, root,
1841                                                 path->nodes[*level]->start,
1842                                                 path->nodes[*level]->len,
1843                                                 root_owner, root_gen, 0, 0, 1);
1844                                 BUG_ON(ret);
1845                         }
1846                         free_extent_buffer(path->nodes[*level]);
1847                         path->nodes[*level] = NULL;
1848                         *level = i + 1;
1849                 }
1850         }
1851         return 1;
1852 }
1853
1854 /*
1855  * drop the reference count on the tree rooted at 'snap'.  This traverses
1856  * the tree freeing any blocks that have a ref count of zero after being
1857  * decremented.
1858  */
1859 static int walk_log_tree(struct btrfs_trans_handle *trans,
1860                          struct btrfs_root *log, struct walk_control *wc)
1861 {
1862         int ret = 0;
1863         int wret;
1864         int level;
1865         struct btrfs_path *path;
1866         int i;
1867         int orig_level;
1868
1869         path = btrfs_alloc_path();
1870         BUG_ON(!path);
1871
1872         level = btrfs_header_level(log->node);
1873         orig_level = level;
1874         path->nodes[level] = log->node;
1875         extent_buffer_get(log->node);
1876         path->slots[level] = 0;
1877
1878         while(1) {
1879                 wret = walk_down_log_tree(trans, log, path, &level, wc);
1880                 if (wret > 0)
1881                         break;
1882                 if (wret < 0)
1883                         ret = wret;
1884
1885                 wret = walk_up_log_tree(trans, log, path, &level, wc);
1886                 if (wret > 0)
1887                         break;
1888                 if (wret < 0)
1889                         ret = wret;
1890         }
1891
1892         /* was the root node processed? if not, catch it here */
1893         if (path->nodes[orig_level]) {
1894                 wc->process_func(log, path->nodes[orig_level], wc,
1895                          btrfs_header_generation(path->nodes[orig_level]));
1896                 if (wc->free) {
1897                         struct extent_buffer *next;
1898
1899                         next = path->nodes[orig_level];
1900
1901                         btrfs_tree_lock(next);
1902                         clean_tree_block(trans, log, next);
1903                         btrfs_wait_tree_block_writeback(next);
1904                         btrfs_tree_unlock(next);
1905
1906                         if (orig_level == 0) {
1907                                 ret = btrfs_drop_leaf_ref(trans, log,
1908                                                           next);
1909                                 BUG_ON(ret);
1910                         }
1911                         WARN_ON(log->root_key.objectid !=
1912                                 BTRFS_TREE_LOG_OBJECTID);
1913                         ret = btrfs_free_extent(trans, log,
1914                                                 next->start, next->len,
1915                                                 log->root_key.objectid,
1916                                                 btrfs_header_generation(next),
1917                                                 0, 0, 1);
1918                         BUG_ON(ret);
1919                 }
1920         }
1921
1922         for (i = 0; i <= orig_level; i++) {
1923                 if (path->nodes[i]) {
1924                         free_extent_buffer(path->nodes[i]);
1925                         path->nodes[i] = NULL;
1926                 }
1927         }
1928         btrfs_free_path(path);
1929         if (wc->free)
1930                 free_extent_buffer(log->node);
1931         return ret;
1932 }
1933
1934 int wait_log_commit(struct btrfs_root *log)
1935 {
1936         DEFINE_WAIT(wait);
1937         u64 transid = log->fs_info->tree_log_transid;
1938
1939         do {
1940                 prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
1941                                 TASK_UNINTERRUPTIBLE);
1942                 mutex_unlock(&log->fs_info->tree_log_mutex);
1943                 if (atomic_read(&log->fs_info->tree_log_commit))
1944                         schedule();
1945                 finish_wait(&log->fs_info->tree_log_wait, &wait);
1946                 mutex_lock(&log->fs_info->tree_log_mutex);
1947         } while(transid == log->fs_info->tree_log_transid &&
1948                 atomic_read(&log->fs_info->tree_log_commit));
1949         return 0;
1950 }
1951
1952 /*
1953  * btrfs_sync_log does sends a given tree log down to the disk and
1954  * updates the super blocks to record it.  When this call is done,
1955  * you know that any inodes previously logged are safely on disk
1956  */
1957 int btrfs_sync_log(struct btrfs_trans_handle *trans,
1958                    struct btrfs_root *root)
1959 {
1960         int ret;
1961         unsigned long batch;
1962         struct btrfs_root *log = root->log_root;
1963         struct walk_control wc = {
1964                 .write = 1,
1965                 .process_func = process_one_buffer
1966         };
1967
1968         mutex_lock(&log->fs_info->tree_log_mutex);
1969         if (atomic_read(&log->fs_info->tree_log_commit)) {
1970                 wait_log_commit(log);
1971                 goto out;
1972         }
1973         atomic_set(&log->fs_info->tree_log_commit, 1);
1974
1975         while(1) {
1976                 batch = log->fs_info->tree_log_batch;
1977                 mutex_unlock(&log->fs_info->tree_log_mutex);
1978                 schedule_timeout_uninterruptible(1);
1979                 mutex_lock(&log->fs_info->tree_log_mutex);
1980
1981                 while(atomic_read(&log->fs_info->tree_log_writers)) {
1982                         DEFINE_WAIT(wait);
1983                         prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
1984                                         TASK_UNINTERRUPTIBLE);
1985                         batch = log->fs_info->tree_log_batch;
1986                         mutex_unlock(&log->fs_info->tree_log_mutex);
1987                         if (atomic_read(&log->fs_info->tree_log_writers))
1988                                 schedule();
1989                         mutex_lock(&log->fs_info->tree_log_mutex);
1990                         finish_wait(&log->fs_info->tree_log_wait, &wait);
1991                 }
1992                 if (batch == log->fs_info->tree_log_batch)
1993                         break;
1994         }
1995         ret = walk_log_tree(trans, log, &wc);
1996         BUG_ON(ret);
1997
1998         ret = walk_log_tree(trans, log->fs_info->log_root_tree, &wc);
1999         BUG_ON(ret);
2000
2001         wc.wait = 1;
2002
2003         ret = walk_log_tree(trans, log, &wc);
2004         BUG_ON(ret);
2005
2006         ret = walk_log_tree(trans, log->fs_info->log_root_tree, &wc);
2007         BUG_ON(ret);
2008
2009         btrfs_set_super_log_root(&root->fs_info->super_for_commit,
2010                                  log->fs_info->log_root_tree->node->start);
2011         btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
2012                        btrfs_header_level(log->fs_info->log_root_tree->node));
2013
2014         write_ctree_super(trans, log->fs_info->tree_root);
2015         log->fs_info->tree_log_transid++;
2016         log->fs_info->tree_log_batch = 0;
2017         atomic_set(&log->fs_info->tree_log_commit, 0);
2018         smp_mb();
2019         if (waitqueue_active(&log->fs_info->tree_log_wait))
2020                 wake_up(&log->fs_info->tree_log_wait);
2021 out:
2022         mutex_unlock(&log->fs_info->tree_log_mutex);
2023         return 0;
2024
2025 }
2026
2027 /*
2028  * free all the extents used by the tree log.  This should be called
2029  * at commit time of the full transaction
2030  */
2031 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2032 {
2033         int ret;
2034         struct btrfs_root *log;
2035         struct key;
2036         struct walk_control wc = {
2037                 .free = 1,
2038                 .process_func = process_one_buffer
2039         };
2040
2041         if (!root->log_root)
2042                 return 0;
2043
2044         log = root->log_root;
2045         ret = walk_log_tree(trans, log, &wc);
2046         BUG_ON(ret);
2047
2048         log = root->log_root;
2049         ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
2050                              &log->root_key);
2051         BUG_ON(ret);
2052         root->log_root = NULL;
2053         kfree(root->log_root);
2054         return 0;
2055 }
2056
2057 /*
2058  * helper function to update the item for a given subvolumes log root
2059  * in the tree of log roots
2060  */
2061 static int update_log_root(struct btrfs_trans_handle *trans,
2062                            struct btrfs_root *log)
2063 {
2064         u64 bytenr = btrfs_root_bytenr(&log->root_item);
2065         int ret;
2066
2067         if (log->node->start == bytenr)
2068                 return 0;
2069
2070         btrfs_set_root_bytenr(&log->root_item, log->node->start);
2071         btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
2072         ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2073                                 &log->root_key, &log->root_item);
2074         BUG_ON(ret);
2075         return ret;
2076 }
2077
2078 /*
2079  * If both a file and directory are logged, and unlinks or renames are
2080  * mixed in, we have a few interesting corners:
2081  *
2082  * create file X in dir Y
2083  * link file X to X.link in dir Y
2084  * fsync file X
2085  * unlink file X but leave X.link
2086  * fsync dir Y
2087  *
2088  * After a crash we would expect only X.link to exist.  But file X
2089  * didn't get fsync'd again so the log has back refs for X and X.link.
2090  *
2091  * We solve this by removing directory entries and inode backrefs from the
2092  * log when a file that was logged in the current transaction is
2093  * unlinked.  Any later fsync will include the updated log entries, and
2094  * we'll be able to reconstruct the proper directory items from backrefs.
2095  *
2096  * This optimizations allows us to avoid relogging the entire inode
2097  * or the entire directory.
2098  */
2099 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2100                                  struct btrfs_root *root,
2101                                  const char *name, int name_len,
2102                                  struct inode *dir, u64 index)
2103 {
2104         struct btrfs_root *log;
2105         struct btrfs_dir_item *di;
2106         struct btrfs_path *path;
2107         int ret;
2108         int bytes_del = 0;
2109
2110         ret = join_running_log_trans(root);
2111         if (ret)
2112                 return 0;
2113
2114         mutex_lock(&BTRFS_I(dir)->log_mutex);
2115
2116         log = root->log_root;
2117         path = btrfs_alloc_path();
2118         di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
2119                                    name, name_len, -1);
2120         if (di && !IS_ERR(di)) {
2121                 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2122                 bytes_del += name_len;
2123                 BUG_ON(ret);
2124         }
2125         btrfs_release_path(log, path);
2126         di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
2127                                          index, name, name_len, -1);
2128         if (di && !IS_ERR(di)) {
2129                 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2130                 bytes_del += name_len;
2131                 BUG_ON(ret);
2132         }
2133
2134         /* update the directory size in the log to reflect the names
2135          * we have removed
2136          */
2137         if (bytes_del) {
2138                 struct btrfs_key key;
2139
2140                 key.objectid = dir->i_ino;
2141                 key.offset = 0;
2142                 key.type = BTRFS_INODE_ITEM_KEY;
2143                 btrfs_release_path(log, path);
2144
2145                 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
2146                 if (ret == 0) {
2147                         struct btrfs_inode_item *item;
2148                         u64 i_size;
2149
2150                         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2151                                               struct btrfs_inode_item);
2152                         i_size = btrfs_inode_size(path->nodes[0], item);
2153                         if (i_size > bytes_del)
2154                                 i_size -= bytes_del;
2155                         else
2156                                 i_size = 0;
2157                         btrfs_set_inode_size(path->nodes[0], item, i_size);
2158                         btrfs_mark_buffer_dirty(path->nodes[0]);
2159                 } else
2160                         ret = 0;
2161                 btrfs_release_path(log, path);
2162         }
2163
2164         btrfs_free_path(path);
2165         mutex_unlock(&BTRFS_I(dir)->log_mutex);
2166         end_log_trans(root);
2167
2168         return 0;
2169 }
2170
2171 /* see comments for btrfs_del_dir_entries_in_log */
2172 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2173                                struct btrfs_root *root,
2174                                const char *name, int name_len,
2175                                struct inode *inode, u64 dirid)
2176 {
2177         struct btrfs_root *log;
2178         u64 index;
2179         int ret;
2180
2181         ret = join_running_log_trans(root);
2182         if (ret)
2183                 return 0;
2184         log = root->log_root;
2185         mutex_lock(&BTRFS_I(inode)->log_mutex);
2186
2187         ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
2188                                   dirid, &index);
2189         mutex_unlock(&BTRFS_I(inode)->log_mutex);
2190         end_log_trans(root);
2191
2192         return ret;
2193 }
2194
2195 /*
2196  * creates a range item in the log for 'dirid'.  first_offset and
2197  * last_offset tell us which parts of the key space the log should
2198  * be considered authoritative for.
2199  */
2200 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2201                                        struct btrfs_root *log,
2202                                        struct btrfs_path *path,
2203                                        int key_type, u64 dirid,
2204                                        u64 first_offset, u64 last_offset)
2205 {
2206         int ret;
2207         struct btrfs_key key;
2208         struct btrfs_dir_log_item *item;
2209
2210         key.objectid = dirid;
2211         key.offset = first_offset;
2212         if (key_type == BTRFS_DIR_ITEM_KEY)
2213                 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2214         else
2215                 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2216         ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
2217         BUG_ON(ret);
2218
2219         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2220                               struct btrfs_dir_log_item);
2221         btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2222         btrfs_mark_buffer_dirty(path->nodes[0]);
2223         btrfs_release_path(log, path);
2224         return 0;
2225 }
2226
2227 /*
2228  * log all the items included in the current transaction for a given
2229  * directory.  This also creates the range items in the log tree required
2230  * to replay anything deleted before the fsync
2231  */
2232 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2233                           struct btrfs_root *root, struct inode *inode,
2234                           struct btrfs_path *path,
2235                           struct btrfs_path *dst_path, int key_type,
2236                           u64 min_offset, u64 *last_offset_ret)
2237 {
2238         struct btrfs_key min_key;
2239         struct btrfs_key max_key;
2240         struct btrfs_root *log = root->log_root;
2241         struct extent_buffer *src;
2242         int ret;
2243         int i;
2244         int nritems;
2245         u64 first_offset = min_offset;
2246         u64 last_offset = (u64)-1;
2247
2248         log = root->log_root;
2249         max_key.objectid = inode->i_ino;
2250         max_key.offset = (u64)-1;
2251         max_key.type = key_type;
2252
2253         min_key.objectid = inode->i_ino;
2254         min_key.type = key_type;
2255         min_key.offset = min_offset;
2256
2257         path->keep_locks = 1;
2258
2259         ret = btrfs_search_forward(root, &min_key, &max_key,
2260                                    path, 0, trans->transid);
2261
2262         /*
2263          * we didn't find anything from this transaction, see if there
2264          * is anything at all
2265          */
2266         if (ret != 0 || min_key.objectid != inode->i_ino ||
2267             min_key.type != key_type) {
2268                 min_key.objectid = inode->i_ino;
2269                 min_key.type = key_type;
2270                 min_key.offset = (u64)-1;
2271                 btrfs_release_path(root, path);
2272                 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2273                 if (ret < 0) {
2274                         btrfs_release_path(root, path);
2275                         return ret;
2276                 }
2277                 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2278
2279                 /* if ret == 0 there are items for this type,
2280                  * create a range to tell us the last key of this type.
2281                  * otherwise, there are no items in this directory after
2282                  * *min_offset, and we create a range to indicate that.
2283                  */
2284                 if (ret == 0) {
2285                         struct btrfs_key tmp;
2286                         btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2287                                               path->slots[0]);
2288                         if (key_type == tmp.type) {
2289                                 first_offset = max(min_offset, tmp.offset) + 1;
2290                         }
2291                 }
2292                 goto done;
2293         }
2294
2295         /* go backward to find any previous key */
2296         ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2297         if (ret == 0) {
2298                 struct btrfs_key tmp;
2299                 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2300                 if (key_type == tmp.type) {
2301                         first_offset = tmp.offset;
2302                         ret = overwrite_item(trans, log, dst_path,
2303                                              path->nodes[0], path->slots[0],
2304                                              &tmp);
2305                 }
2306         }
2307         btrfs_release_path(root, path);
2308
2309         /* find the first key from this transaction again */
2310         ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2311         if (ret != 0) {
2312                 WARN_ON(1);
2313                 goto done;
2314         }
2315
2316         /*
2317          * we have a block from this transaction, log every item in it
2318          * from our directory
2319          */
2320         while(1) {
2321                 struct btrfs_key tmp;
2322                 src = path->nodes[0];
2323                 nritems = btrfs_header_nritems(src);
2324                 for (i = path->slots[0]; i < nritems; i++) {
2325                         btrfs_item_key_to_cpu(src, &min_key, i);
2326
2327                         if (min_key.objectid != inode->i_ino ||
2328                             min_key.type != key_type)
2329                                 goto done;
2330                         ret = overwrite_item(trans, log, dst_path, src, i,
2331                                              &min_key);
2332                         BUG_ON(ret);
2333                 }
2334                 path->slots[0] = nritems;
2335
2336                 /*
2337                  * look ahead to the next item and see if it is also
2338                  * from this directory and from this transaction
2339                  */
2340                 ret = btrfs_next_leaf(root, path);
2341                 if (ret == 1) {
2342                         last_offset = (u64)-1;
2343                         goto done;
2344                 }
2345                 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2346                 if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
2347                         last_offset = (u64)-1;
2348                         goto done;
2349                 }
2350                 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2351                         ret = overwrite_item(trans, log, dst_path,
2352                                              path->nodes[0], path->slots[0],
2353                                              &tmp);
2354
2355                         BUG_ON(ret);
2356                         last_offset = tmp.offset;
2357                         goto done;
2358                 }
2359         }
2360 done:
2361         *last_offset_ret = last_offset;
2362         btrfs_release_path(root, path);
2363         btrfs_release_path(log, dst_path);
2364
2365         /* insert the log range keys to indicate where the log is valid */
2366         ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino,
2367                                  first_offset, last_offset);
2368         BUG_ON(ret);
2369         return 0;
2370 }
2371
2372 /*
2373  * logging directories is very similar to logging inodes, We find all the items
2374  * from the current transaction and write them to the log.
2375  *
2376  * The recovery code scans the directory in the subvolume, and if it finds a
2377  * key in the range logged that is not present in the log tree, then it means
2378  * that dir entry was unlinked during the transaction.
2379  *
2380  * In order for that scan to work, we must include one key smaller than
2381  * the smallest logged by this transaction and one key larger than the largest
2382  * key logged by this transaction.
2383  */
2384 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2385                           struct btrfs_root *root, struct inode *inode,
2386                           struct btrfs_path *path,
2387                           struct btrfs_path *dst_path)
2388 {
2389         u64 min_key;
2390         u64 max_key;
2391         int ret;
2392         int key_type = BTRFS_DIR_ITEM_KEY;
2393
2394 again:
2395         min_key = 0;
2396         max_key = 0;
2397         while(1) {
2398                 ret = log_dir_items(trans, root, inode, path,
2399                                     dst_path, key_type, min_key,
2400                                     &max_key);
2401                 BUG_ON(ret);
2402                 if (max_key == (u64)-1)
2403                         break;
2404                 min_key = max_key + 1;
2405         }
2406
2407         if (key_type == BTRFS_DIR_ITEM_KEY) {
2408                 key_type = BTRFS_DIR_INDEX_KEY;
2409                 goto again;
2410         }
2411         return 0;
2412 }
2413
2414 /*
2415  * a helper function to drop items from the log before we relog an
2416  * inode.  max_key_type indicates the highest item type to remove.
2417  * This cannot be run for file data extents because it does not
2418  * free the extents they point to.
2419  */
2420 static int drop_objectid_items(struct btrfs_trans_handle *trans,
2421                                   struct btrfs_root *log,
2422                                   struct btrfs_path *path,
2423                                   u64 objectid, int max_key_type)
2424 {
2425         int ret;
2426         struct btrfs_key key;
2427         struct btrfs_key found_key;
2428
2429         key.objectid = objectid;
2430         key.type = max_key_type;
2431         key.offset = (u64)-1;
2432
2433         while(1) {
2434                 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
2435
2436                 if (ret != 1)
2437                         break;
2438
2439                 if (path->slots[0] == 0)
2440                         break;
2441
2442                 path->slots[0]--;
2443                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2444                                       path->slots[0]);
2445
2446                 if (found_key.objectid != objectid)
2447                         break;
2448
2449                 ret = btrfs_del_item(trans, log, path);
2450                 BUG_ON(ret);
2451                 btrfs_release_path(log, path);
2452         }
2453         btrfs_release_path(log, path);
2454         return 0;
2455 }
2456
2457 /* log a single inode in the tree log.
2458  * At least one parent directory for this inode must exist in the tree
2459  * or be logged already.
2460  *
2461  * Any items from this inode changed by the current transaction are copied
2462  * to the log tree.  An extra reference is taken on any extents in this
2463  * file, allowing us to avoid a whole pile of corner cases around logging
2464  * blocks that have been removed from the tree.
2465  *
2466  * See LOG_INODE_ALL and related defines for a description of what inode_only
2467  * does.
2468  *
2469  * This handles both files and directories.
2470  */
2471 static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
2472                              struct btrfs_root *root, struct inode *inode,
2473                              int inode_only)
2474 {
2475         struct btrfs_path *path;
2476         struct btrfs_path *dst_path;
2477         struct btrfs_key min_key;
2478         struct btrfs_key max_key;
2479         struct btrfs_root *log = root->log_root;
2480         unsigned long src_offset;
2481         unsigned long dst_offset;
2482         struct extent_buffer *src;
2483         struct btrfs_file_extent_item *extent;
2484         struct btrfs_inode_item *inode_item;
2485         u32 size;
2486         int ret;
2487
2488         log = root->log_root;
2489
2490         path = btrfs_alloc_path();
2491         dst_path = btrfs_alloc_path();
2492
2493         min_key.objectid = inode->i_ino;
2494         min_key.type = BTRFS_INODE_ITEM_KEY;
2495         min_key.offset = 0;
2496
2497         max_key.objectid = inode->i_ino;
2498         if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2499                 max_key.type = BTRFS_XATTR_ITEM_KEY;
2500         else
2501                 max_key.type = (u8)-1;
2502         max_key.offset = (u64)-1;
2503
2504         /*
2505          * if this inode has already been logged and we're in inode_only
2506          * mode, we don't want to delete the things that have already
2507          * been written to the log.
2508          *
2509          * But, if the inode has been through an inode_only log,
2510          * the logged_trans field is not set.  This allows us to catch
2511          * any new names for this inode in the backrefs by logging it
2512          * again
2513          */
2514         if (inode_only == LOG_INODE_EXISTS &&
2515             BTRFS_I(inode)->logged_trans == trans->transid) {
2516                 btrfs_free_path(path);
2517                 btrfs_free_path(dst_path);
2518                 goto out;
2519         }
2520         mutex_lock(&BTRFS_I(inode)->log_mutex);
2521
2522         /*
2523          * a brute force approach to making sure we get the most uptodate
2524          * copies of everything.
2525          */
2526         if (S_ISDIR(inode->i_mode)) {
2527                 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2528
2529                 if (inode_only == LOG_INODE_EXISTS)
2530                         max_key_type = BTRFS_XATTR_ITEM_KEY;
2531                 ret = drop_objectid_items(trans, log, path,
2532                                           inode->i_ino, max_key_type);
2533         } else {
2534                 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2535         }
2536         BUG_ON(ret);
2537         path->keep_locks = 1;
2538
2539         while(1) {
2540                 ret = btrfs_search_forward(root, &min_key, &max_key,
2541                                            path, 0, trans->transid);
2542                 if (ret != 0)
2543                         break;
2544
2545                 if (min_key.objectid != inode->i_ino)
2546                         break;
2547                 if (min_key.type > max_key.type)
2548                         break;
2549
2550                 src = path->nodes[0];
2551                 size = btrfs_item_size_nr(src, path->slots[0]);
2552                 ret = btrfs_insert_empty_item(trans, log, dst_path, &min_key,
2553                                               size);
2554                 if (ret)
2555                         BUG();
2556
2557                 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2558                                                    dst_path->slots[0]);
2559
2560                 src_offset = btrfs_item_ptr_offset(src, path->slots[0]);
2561
2562                 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2563                                    src_offset, size);
2564
2565                 if (inode_only == LOG_INODE_EXISTS &&
2566                     min_key.type == BTRFS_INODE_ITEM_KEY) {
2567                         inode_item = btrfs_item_ptr(dst_path->nodes[0],
2568                                                     dst_path->slots[0],
2569                                                     struct btrfs_inode_item);
2570                         btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2571
2572                         /* set the generation to zero so the recover code
2573                          * can tell the difference between an logging
2574                          * just to say 'this inode exists' and a logging
2575                          * to say 'update this inode with these values'
2576                          */
2577                         btrfs_set_inode_generation(dst_path->nodes[0],
2578                                                    inode_item, 0);
2579                 }
2580                 /* take a reference on file data extents so that truncates
2581                  * or deletes of this inode don't have to relog the inode
2582                  * again
2583                  */
2584                 if (btrfs_key_type(&min_key) == BTRFS_EXTENT_DATA_KEY) {
2585                         int found_type;
2586                         extent = btrfs_item_ptr(src, path->slots[0],
2587                                                 struct btrfs_file_extent_item);
2588
2589                         found_type = btrfs_file_extent_type(src, extent);
2590                         if (found_type == BTRFS_FILE_EXTENT_REG) {
2591                                 u64 ds = btrfs_file_extent_disk_bytenr(src,
2592                                                                    extent);
2593                                 u64 dl = btrfs_file_extent_disk_num_bytes(src,
2594                                                                       extent);
2595                                 /* ds == 0 is a hole */
2596                                 if (ds != 0) {
2597                                         ret = btrfs_inc_extent_ref(trans, log,
2598                                                    ds, dl,
2599                                                    log->root_key.objectid,
2600                                                    0,
2601                                                    inode->i_ino,
2602                                                    min_key.offset);
2603                                         BUG_ON(ret);
2604                                 }
2605                         }
2606                 }
2607
2608                 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2609                 btrfs_release_path(root, path);
2610                 btrfs_release_path(log, dst_path);
2611
2612                 if (min_key.offset < (u64)-1)
2613                         min_key.offset++;
2614                 else if (min_key.type < (u8)-1)
2615                         min_key.type++;
2616                 else if (min_key.objectid < (u64)-1)
2617                         min_key.objectid++;
2618                 else
2619                         break;
2620         }
2621         if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode) &&
2622             BTRFS_I(inode)->log_dirty_trans >= trans->transid) {
2623                 btrfs_release_path(root, path);
2624                 btrfs_release_path(log, dst_path);
2625                 BTRFS_I(inode)->log_dirty_trans = 0;
2626                 ret = log_directory_changes(trans, root, inode, path, dst_path);
2627                 BUG_ON(ret);
2628         }
2629         mutex_unlock(&BTRFS_I(inode)->log_mutex);
2630
2631         btrfs_free_path(path);
2632         btrfs_free_path(dst_path);
2633
2634         mutex_lock(&root->fs_info->tree_log_mutex);
2635         ret = update_log_root(trans, log);
2636         BUG_ON(ret);
2637         mutex_unlock(&root->fs_info->tree_log_mutex);
2638 out:
2639         return 0;
2640 }
2641
2642 int btrfs_log_inode(struct btrfs_trans_handle *trans,
2643                     struct btrfs_root *root, struct inode *inode,
2644                     int inode_only)
2645 {
2646         int ret;
2647
2648         start_log_trans(trans, root);
2649         ret = __btrfs_log_inode(trans, root, inode, inode_only);
2650         end_log_trans(root);
2651         return ret;
2652 }
2653
2654 /*
2655  * helper function around btrfs_log_inode to make sure newly created
2656  * parent directories also end up in the log.  A minimal inode and backref
2657  * only logging is done of any parent directories that are older than
2658  * the last committed transaction
2659  */
2660 int btrfs_log_dentry(struct btrfs_trans_handle *trans,
2661                     struct btrfs_root *root, struct dentry *dentry)
2662 {
2663         int inode_only = LOG_INODE_ALL;
2664         struct super_block *sb;
2665         int ret;
2666
2667         start_log_trans(trans, root);
2668         sb = dentry->d_inode->i_sb;
2669         while(1) {
2670                 ret = __btrfs_log_inode(trans, root, dentry->d_inode,
2671                                         inode_only);
2672                 BUG_ON(ret);
2673                 inode_only = LOG_INODE_EXISTS;
2674
2675                 dentry = dentry->d_parent;
2676                 if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb)
2677                         break;
2678
2679                 if (BTRFS_I(dentry->d_inode)->generation <=
2680                     root->fs_info->last_trans_committed)
2681                         break;
2682         }
2683         end_log_trans(root);
2684         return 0;
2685 }
2686
2687 /*
2688  * it is not safe to log dentry if the chunk root has added new
2689  * chunks.  This returns 0 if the dentry was logged, and 1 otherwise.
2690  * If this returns 1, you must commit the transaction to safely get your
2691  * data on disk.
2692  */
2693 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
2694                           struct btrfs_root *root, struct dentry *dentry)
2695 {
2696         u64 gen;
2697         gen = root->fs_info->last_trans_new_blockgroup;
2698         if (gen > root->fs_info->last_trans_committed)
2699                 return 1;
2700         else
2701                 return btrfs_log_dentry(trans, root, dentry);
2702 }
2703
2704 /*
2705  * should be called during mount to recover any replay any log trees
2706  * from the FS
2707  */
2708 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
2709 {
2710         int ret;
2711         struct btrfs_path *path;
2712         struct btrfs_trans_handle *trans;
2713         struct btrfs_key key;
2714         struct btrfs_key found_key;
2715         struct btrfs_key tmp_key;
2716         struct btrfs_root *log;
2717         struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
2718         u64 highest_inode;
2719         struct walk_control wc = {
2720                 .process_func = process_one_buffer,
2721                 .stage = 0,
2722         };
2723
2724         fs_info->log_root_recovering = 1;
2725         path = btrfs_alloc_path();
2726         BUG_ON(!path);
2727
2728         trans = btrfs_start_transaction(fs_info->tree_root, 1);
2729
2730         wc.trans = trans;
2731         wc.pin = 1;
2732
2733         walk_log_tree(trans, log_root_tree, &wc);
2734
2735 again:
2736         key.objectid = BTRFS_TREE_LOG_OBJECTID;
2737         key.offset = (u64)-1;
2738         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2739
2740         while(1) {
2741                 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
2742                 if (ret < 0)
2743                         break;
2744                 if (ret > 0) {
2745                         if (path->slots[0] == 0)
2746                                 break;
2747                         path->slots[0]--;
2748                 }
2749                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2750                                       path->slots[0]);
2751                 btrfs_release_path(log_root_tree, path);
2752                 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
2753                         break;
2754
2755                 log = btrfs_read_fs_root_no_radix(log_root_tree,
2756                                                   &found_key);
2757                 BUG_ON(!log);
2758
2759
2760                 tmp_key.objectid = found_key.offset;
2761                 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
2762                 tmp_key.offset = (u64)-1;
2763
2764                 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
2765
2766                 BUG_ON(!wc.replay_dest);
2767
2768                 btrfs_record_root_in_trans(wc.replay_dest);
2769                 ret = walk_log_tree(trans, log, &wc);
2770                 BUG_ON(ret);
2771
2772                 if (wc.stage == LOG_WALK_REPLAY_ALL) {
2773                         ret = fixup_inode_link_counts(trans, wc.replay_dest,
2774                                                       path);
2775                         BUG_ON(ret);
2776                 }
2777                 ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode);
2778                 if (ret == 0) {
2779                         wc.replay_dest->highest_inode = highest_inode;
2780                         wc.replay_dest->last_inode_alloc = highest_inode;
2781                 }
2782
2783                 key.offset = found_key.offset - 1;
2784                 free_extent_buffer(log->node);
2785                 kfree(log);
2786
2787                 if (found_key.offset == 0)
2788                         break;
2789         }
2790         btrfs_release_path(log_root_tree, path);
2791
2792         /* step one is to pin it all, step two is to replay just inodes */
2793         if (wc.pin) {
2794                 wc.pin = 0;
2795                 wc.process_func = replay_one_buffer;
2796                 wc.stage = LOG_WALK_REPLAY_INODES;
2797                 goto again;
2798         }
2799         /* step three is to replay everything */
2800         if (wc.stage < LOG_WALK_REPLAY_ALL) {
2801                 wc.stage++;
2802                 goto again;
2803         }
2804
2805         btrfs_free_path(path);
2806
2807         free_extent_buffer(log_root_tree->node);
2808         log_root_tree->log_root = NULL;
2809         fs_info->log_root_recovering = 0;
2810
2811         /* step 4: commit the transaction, which also unpins the blocks */
2812         btrfs_commit_transaction(trans, fs_info->tree_root);
2813
2814         kfree(log_root_tree);
2815         return 0;
2816 }