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