GNU Linux-libre 4.19.245-gnu1
[releases.git] / fs / btrfs / tree-log.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2008 Oracle.  All rights reserved.
4  */
5
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/blkdev.h>
9 #include <linux/list_sort.h>
10 #include <linux/iversion.h>
11 #include "ctree.h"
12 #include "tree-log.h"
13 #include "disk-io.h"
14 #include "locking.h"
15 #include "print-tree.h"
16 #include "backref.h"
17 #include "compression.h"
18 #include "qgroup.h"
19 #include "inode-map.h"
20
21 /* magic values for the inode_only field in btrfs_log_inode:
22  *
23  * LOG_INODE_ALL means to log everything
24  * LOG_INODE_EXISTS means to log just enough to recreate the inode
25  * during log replay
26  */
27 #define LOG_INODE_ALL 0
28 #define LOG_INODE_EXISTS 1
29 #define LOG_OTHER_INODE 2
30
31 /*
32  * directory trouble cases
33  *
34  * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35  * log, we must force a full commit before doing an fsync of the directory
36  * where the unlink was done.
37  * ---> record transid of last unlink/rename per directory
38  *
39  * mkdir foo/some_dir
40  * normal commit
41  * rename foo/some_dir foo2/some_dir
42  * mkdir foo/some_dir
43  * fsync foo/some_dir/some_file
44  *
45  * The fsync above will unlink the original some_dir without recording
46  * it in its new location (foo2).  After a crash, some_dir will be gone
47  * unless the fsync of some_file forces a full commit
48  *
49  * 2) we must log any new names for any file or dir that is in the fsync
50  * log. ---> check inode while renaming/linking.
51  *
52  * 2a) we must log any new names for any file or dir during rename
53  * when the directory they are being removed from was logged.
54  * ---> check inode and old parent dir during rename
55  *
56  *  2a is actually the more important variant.  With the extra logging
57  *  a crash might unlink the old name without recreating the new one
58  *
59  * 3) after a crash, we must go through any directories with a link count
60  * of zero and redo the rm -rf
61  *
62  * mkdir f1/foo
63  * normal commit
64  * rm -rf f1/foo
65  * fsync(f1)
66  *
67  * The directory f1 was fully removed from the FS, but fsync was never
68  * called on f1, only its parent dir.  After a crash the rm -rf must
69  * be replayed.  This must be able to recurse down the entire
70  * directory tree.  The inode link count fixup code takes care of the
71  * ugly details.
72  */
73
74 /*
75  * stages for the tree walking.  The first
76  * stage (0) is to only pin down the blocks we find
77  * the second stage (1) is to make sure that all the inodes
78  * we find in the log are created in the subvolume.
79  *
80  * The last stage is to deal with directories and links and extents
81  * and all the other fun semantics
82  */
83 #define LOG_WALK_PIN_ONLY 0
84 #define LOG_WALK_REPLAY_INODES 1
85 #define LOG_WALK_REPLAY_DIR_INDEX 2
86 #define LOG_WALK_REPLAY_ALL 3
87
88 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
89                            struct btrfs_root *root, struct btrfs_inode *inode,
90                            int inode_only,
91                            const loff_t start,
92                            const loff_t end,
93                            struct btrfs_log_ctx *ctx);
94 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
95                              struct btrfs_root *root,
96                              struct btrfs_path *path, u64 objectid);
97 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
98                                        struct btrfs_root *root,
99                                        struct btrfs_root *log,
100                                        struct btrfs_path *path,
101                                        u64 dirid, int del_all);
102
103 /*
104  * tree logging is a special write ahead log used to make sure that
105  * fsyncs and O_SYNCs can happen without doing full tree commits.
106  *
107  * Full tree commits are expensive because they require commonly
108  * modified blocks to be recowed, creating many dirty pages in the
109  * extent tree an 4x-6x higher write load than ext3.
110  *
111  * Instead of doing a tree commit on every fsync, we use the
112  * key ranges and transaction ids to find items for a given file or directory
113  * that have changed in this transaction.  Those items are copied into
114  * a special tree (one per subvolume root), that tree is written to disk
115  * and then the fsync is considered complete.
116  *
117  * After a crash, items are copied out of the log-tree back into the
118  * subvolume tree.  Any file data extents found are recorded in the extent
119  * allocation tree, and the log-tree freed.
120  *
121  * The log tree is read three times, once to pin down all the extents it is
122  * using in ram and once, once to create all the inodes logged in the tree
123  * and once to do all the other items.
124  */
125
126 /*
127  * start a sub transaction and setup the log tree
128  * this increments the log tree writer count to make the people
129  * syncing the tree wait for us to finish
130  */
131 static int start_log_trans(struct btrfs_trans_handle *trans,
132                            struct btrfs_root *root,
133                            struct btrfs_log_ctx *ctx)
134 {
135         struct btrfs_fs_info *fs_info = root->fs_info;
136         int ret = 0;
137
138         mutex_lock(&root->log_mutex);
139
140         if (root->log_root) {
141                 if (btrfs_need_log_full_commit(fs_info, trans)) {
142                         ret = -EAGAIN;
143                         goto out;
144                 }
145
146                 if (!root->log_start_pid) {
147                         clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
148                         root->log_start_pid = current->pid;
149                 } else if (root->log_start_pid != current->pid) {
150                         set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
151                 }
152         } else {
153                 mutex_lock(&fs_info->tree_log_mutex);
154                 if (!fs_info->log_root_tree)
155                         ret = btrfs_init_log_root_tree(trans, fs_info);
156                 mutex_unlock(&fs_info->tree_log_mutex);
157                 if (ret)
158                         goto out;
159
160                 ret = btrfs_add_log_tree(trans, root);
161                 if (ret)
162                         goto out;
163
164                 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
165                 root->log_start_pid = current->pid;
166         }
167
168         atomic_inc(&root->log_batch);
169         atomic_inc(&root->log_writers);
170         if (ctx) {
171                 int index = root->log_transid % 2;
172                 list_add_tail(&ctx->list, &root->log_ctxs[index]);
173                 ctx->log_transid = root->log_transid;
174         }
175
176 out:
177         mutex_unlock(&root->log_mutex);
178         return ret;
179 }
180
181 /*
182  * returns 0 if there was a log transaction running and we were able
183  * to join, or returns -ENOENT if there were not transactions
184  * in progress
185  */
186 static int join_running_log_trans(struct btrfs_root *root)
187 {
188         int ret = -ENOENT;
189
190         smp_mb();
191         if (!root->log_root)
192                 return -ENOENT;
193
194         mutex_lock(&root->log_mutex);
195         if (root->log_root) {
196                 ret = 0;
197                 atomic_inc(&root->log_writers);
198         }
199         mutex_unlock(&root->log_mutex);
200         return ret;
201 }
202
203 /*
204  * This either makes the current running log transaction wait
205  * until you call btrfs_end_log_trans() or it makes any future
206  * log transactions wait until you call btrfs_end_log_trans()
207  */
208 int btrfs_pin_log_trans(struct btrfs_root *root)
209 {
210         int ret = -ENOENT;
211
212         mutex_lock(&root->log_mutex);
213         atomic_inc(&root->log_writers);
214         mutex_unlock(&root->log_mutex);
215         return ret;
216 }
217
218 /*
219  * indicate we're done making changes to the log tree
220  * and wake up anyone waiting to do a sync
221  */
222 void btrfs_end_log_trans(struct btrfs_root *root)
223 {
224         if (atomic_dec_and_test(&root->log_writers)) {
225                 /* atomic_dec_and_test implies a barrier */
226                 cond_wake_up_nomb(&root->log_writer_wait);
227         }
228 }
229
230
231 /*
232  * the walk control struct is used to pass state down the chain when
233  * processing the log tree.  The stage field tells us which part
234  * of the log tree processing we are currently doing.  The others
235  * are state fields used for that specific part
236  */
237 struct walk_control {
238         /* should we free the extent on disk when done?  This is used
239          * at transaction commit time while freeing a log tree
240          */
241         int free;
242
243         /* should we write out the extent buffer?  This is used
244          * while flushing the log tree to disk during a sync
245          */
246         int write;
247
248         /* should we wait for the extent buffer io to finish?  Also used
249          * while flushing the log tree to disk for a sync
250          */
251         int wait;
252
253         /* pin only walk, we record which extents on disk belong to the
254          * log trees
255          */
256         int pin;
257
258         /* what stage of the replay code we're currently in */
259         int stage;
260
261         /*
262          * Ignore any items from the inode currently being processed. Needs
263          * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
264          * the LOG_WALK_REPLAY_INODES stage.
265          */
266         bool ignore_cur_inode;
267
268         /* the root we are currently replaying */
269         struct btrfs_root *replay_dest;
270
271         /* the trans handle for the current replay */
272         struct btrfs_trans_handle *trans;
273
274         /* the function that gets used to process blocks we find in the
275          * tree.  Note the extent_buffer might not be up to date when it is
276          * passed in, and it must be checked or read if you need the data
277          * inside it
278          */
279         int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
280                             struct walk_control *wc, u64 gen, int level);
281 };
282
283 /*
284  * process_func used to pin down extents, write them or wait on them
285  */
286 static int process_one_buffer(struct btrfs_root *log,
287                               struct extent_buffer *eb,
288                               struct walk_control *wc, u64 gen, int level)
289 {
290         struct btrfs_fs_info *fs_info = log->fs_info;
291         int ret = 0;
292
293         /*
294          * If this fs is mixed then we need to be able to process the leaves to
295          * pin down any logged extents, so we have to read the block.
296          */
297         if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
298                 ret = btrfs_read_buffer(eb, gen, level, NULL);
299                 if (ret)
300                         return ret;
301         }
302
303         if (wc->pin)
304                 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
305                                                       eb->len);
306
307         if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
308                 if (wc->pin && btrfs_header_level(eb) == 0)
309                         ret = btrfs_exclude_logged_extents(fs_info, eb);
310                 if (wc->write)
311                         btrfs_write_tree_block(eb);
312                 if (wc->wait)
313                         btrfs_wait_tree_block_writeback(eb);
314         }
315         return ret;
316 }
317
318 /*
319  * Item overwrite used by replay and tree logging.  eb, slot and key all refer
320  * to the src data we are copying out.
321  *
322  * root is the tree we are copying into, and path is a scratch
323  * path for use in this function (it should be released on entry and
324  * will be released on exit).
325  *
326  * If the key is already in the destination tree the existing item is
327  * overwritten.  If the existing item isn't big enough, it is extended.
328  * If it is too large, it is truncated.
329  *
330  * If the key isn't in the destination yet, a new item is inserted.
331  */
332 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
333                                    struct btrfs_root *root,
334                                    struct btrfs_path *path,
335                                    struct extent_buffer *eb, int slot,
336                                    struct btrfs_key *key)
337 {
338         struct btrfs_fs_info *fs_info = root->fs_info;
339         int ret;
340         u32 item_size;
341         u64 saved_i_size = 0;
342         int save_old_i_size = 0;
343         unsigned long src_ptr;
344         unsigned long dst_ptr;
345         int overwrite_root = 0;
346         bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
347
348         if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
349                 overwrite_root = 1;
350
351         item_size = btrfs_item_size_nr(eb, slot);
352         src_ptr = btrfs_item_ptr_offset(eb, slot);
353
354         /* look for the key in the destination tree */
355         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
356         if (ret < 0)
357                 return ret;
358
359         if (ret == 0) {
360                 char *src_copy;
361                 char *dst_copy;
362                 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
363                                                   path->slots[0]);
364                 if (dst_size != item_size)
365                         goto insert;
366
367                 if (item_size == 0) {
368                         btrfs_release_path(path);
369                         return 0;
370                 }
371                 dst_copy = kmalloc(item_size, GFP_NOFS);
372                 src_copy = kmalloc(item_size, GFP_NOFS);
373                 if (!dst_copy || !src_copy) {
374                         btrfs_release_path(path);
375                         kfree(dst_copy);
376                         kfree(src_copy);
377                         return -ENOMEM;
378                 }
379
380                 read_extent_buffer(eb, src_copy, src_ptr, item_size);
381
382                 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
383                 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
384                                    item_size);
385                 ret = memcmp(dst_copy, src_copy, item_size);
386
387                 kfree(dst_copy);
388                 kfree(src_copy);
389                 /*
390                  * they have the same contents, just return, this saves
391                  * us from cowing blocks in the destination tree and doing
392                  * extra writes that may not have been done by a previous
393                  * sync
394                  */
395                 if (ret == 0) {
396                         btrfs_release_path(path);
397                         return 0;
398                 }
399
400                 /*
401                  * We need to load the old nbytes into the inode so when we
402                  * replay the extents we've logged we get the right nbytes.
403                  */
404                 if (inode_item) {
405                         struct btrfs_inode_item *item;
406                         u64 nbytes;
407                         u32 mode;
408
409                         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
410                                               struct btrfs_inode_item);
411                         nbytes = btrfs_inode_nbytes(path->nodes[0], item);
412                         item = btrfs_item_ptr(eb, slot,
413                                               struct btrfs_inode_item);
414                         btrfs_set_inode_nbytes(eb, item, nbytes);
415
416                         /*
417                          * If this is a directory we need to reset the i_size to
418                          * 0 so that we can set it up properly when replaying
419                          * the rest of the items in this log.
420                          */
421                         mode = btrfs_inode_mode(eb, item);
422                         if (S_ISDIR(mode))
423                                 btrfs_set_inode_size(eb, item, 0);
424                 }
425         } else if (inode_item) {
426                 struct btrfs_inode_item *item;
427                 u32 mode;
428
429                 /*
430                  * New inode, set nbytes to 0 so that the nbytes comes out
431                  * properly when we replay the extents.
432                  */
433                 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
434                 btrfs_set_inode_nbytes(eb, item, 0);
435
436                 /*
437                  * If this is a directory we need to reset the i_size to 0 so
438                  * that we can set it up properly when replaying the rest of
439                  * the items in this log.
440                  */
441                 mode = btrfs_inode_mode(eb, item);
442                 if (S_ISDIR(mode))
443                         btrfs_set_inode_size(eb, item, 0);
444         }
445 insert:
446         btrfs_release_path(path);
447         /* try to insert the key into the destination tree */
448         path->skip_release_on_error = 1;
449         ret = btrfs_insert_empty_item(trans, root, path,
450                                       key, item_size);
451         path->skip_release_on_error = 0;
452
453         /* make sure any existing item is the correct size */
454         if (ret == -EEXIST || ret == -EOVERFLOW) {
455                 u32 found_size;
456                 found_size = btrfs_item_size_nr(path->nodes[0],
457                                                 path->slots[0]);
458                 if (found_size > item_size)
459                         btrfs_truncate_item(fs_info, path, item_size, 1);
460                 else if (found_size < item_size)
461                         btrfs_extend_item(fs_info, path,
462                                           item_size - found_size);
463         } else if (ret) {
464                 return ret;
465         }
466         dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
467                                         path->slots[0]);
468
469         /* don't overwrite an existing inode if the generation number
470          * was logged as zero.  This is done when the tree logging code
471          * is just logging an inode to make sure it exists after recovery.
472          *
473          * Also, don't overwrite i_size on directories during replay.
474          * log replay inserts and removes directory items based on the
475          * state of the tree found in the subvolume, and i_size is modified
476          * as it goes
477          */
478         if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
479                 struct btrfs_inode_item *src_item;
480                 struct btrfs_inode_item *dst_item;
481
482                 src_item = (struct btrfs_inode_item *)src_ptr;
483                 dst_item = (struct btrfs_inode_item *)dst_ptr;
484
485                 if (btrfs_inode_generation(eb, src_item) == 0) {
486                         struct extent_buffer *dst_eb = path->nodes[0];
487                         const u64 ino_size = btrfs_inode_size(eb, src_item);
488
489                         /*
490                          * For regular files an ino_size == 0 is used only when
491                          * logging that an inode exists, as part of a directory
492                          * fsync, and the inode wasn't fsynced before. In this
493                          * case don't set the size of the inode in the fs/subvol
494                          * tree, otherwise we would be throwing valid data away.
495                          */
496                         if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
497                             S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
498                             ino_size != 0) {
499                                 struct btrfs_map_token token;
500
501                                 btrfs_init_map_token(&token);
502                                 btrfs_set_token_inode_size(dst_eb, dst_item,
503                                                            ino_size, &token);
504                         }
505                         goto no_copy;
506                 }
507
508                 if (overwrite_root &&
509                     S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
510                     S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
511                         save_old_i_size = 1;
512                         saved_i_size = btrfs_inode_size(path->nodes[0],
513                                                         dst_item);
514                 }
515         }
516
517         copy_extent_buffer(path->nodes[0], eb, dst_ptr,
518                            src_ptr, item_size);
519
520         if (save_old_i_size) {
521                 struct btrfs_inode_item *dst_item;
522                 dst_item = (struct btrfs_inode_item *)dst_ptr;
523                 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
524         }
525
526         /* make sure the generation is filled in */
527         if (key->type == BTRFS_INODE_ITEM_KEY) {
528                 struct btrfs_inode_item *dst_item;
529                 dst_item = (struct btrfs_inode_item *)dst_ptr;
530                 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
531                         btrfs_set_inode_generation(path->nodes[0], dst_item,
532                                                    trans->transid);
533                 }
534         }
535 no_copy:
536         btrfs_mark_buffer_dirty(path->nodes[0]);
537         btrfs_release_path(path);
538         return 0;
539 }
540
541 /*
542  * simple helper to read an inode off the disk from a given root
543  * This can only be called for subvolume roots and not for the log
544  */
545 static noinline struct inode *read_one_inode(struct btrfs_root *root,
546                                              u64 objectid)
547 {
548         struct btrfs_key key;
549         struct inode *inode;
550
551         key.objectid = objectid;
552         key.type = BTRFS_INODE_ITEM_KEY;
553         key.offset = 0;
554         inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
555         if (IS_ERR(inode))
556                 inode = NULL;
557         return inode;
558 }
559
560 /* replays a single extent in 'eb' at 'slot' with 'key' into the
561  * subvolume 'root'.  path is released on entry and should be released
562  * on exit.
563  *
564  * extents in the log tree have not been allocated out of the extent
565  * tree yet.  So, this completes the allocation, taking a reference
566  * as required if the extent already exists or creating a new extent
567  * if it isn't in the extent allocation tree yet.
568  *
569  * The extent is inserted into the file, dropping any existing extents
570  * from the file that overlap the new one.
571  */
572 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
573                                       struct btrfs_root *root,
574                                       struct btrfs_path *path,
575                                       struct extent_buffer *eb, int slot,
576                                       struct btrfs_key *key)
577 {
578         struct btrfs_fs_info *fs_info = root->fs_info;
579         int found_type;
580         u64 extent_end;
581         u64 start = key->offset;
582         u64 nbytes = 0;
583         struct btrfs_file_extent_item *item;
584         struct inode *inode = NULL;
585         unsigned long size;
586         int ret = 0;
587
588         item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
589         found_type = btrfs_file_extent_type(eb, item);
590
591         if (found_type == BTRFS_FILE_EXTENT_REG ||
592             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
593                 nbytes = btrfs_file_extent_num_bytes(eb, item);
594                 extent_end = start + nbytes;
595
596                 /*
597                  * We don't add to the inodes nbytes if we are prealloc or a
598                  * hole.
599                  */
600                 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
601                         nbytes = 0;
602         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
603                 size = btrfs_file_extent_ram_bytes(eb, item);
604                 nbytes = btrfs_file_extent_ram_bytes(eb, item);
605                 extent_end = ALIGN(start + size,
606                                    fs_info->sectorsize);
607         } else {
608                 ret = 0;
609                 goto out;
610         }
611
612         inode = read_one_inode(root, key->objectid);
613         if (!inode) {
614                 ret = -EIO;
615                 goto out;
616         }
617
618         /*
619          * first check to see if we already have this extent in the
620          * file.  This must be done before the btrfs_drop_extents run
621          * so we don't try to drop this extent.
622          */
623         ret = btrfs_lookup_file_extent(trans, root, path,
624                         btrfs_ino(BTRFS_I(inode)), start, 0);
625
626         if (ret == 0 &&
627             (found_type == BTRFS_FILE_EXTENT_REG ||
628              found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
629                 struct btrfs_file_extent_item cmp1;
630                 struct btrfs_file_extent_item cmp2;
631                 struct btrfs_file_extent_item *existing;
632                 struct extent_buffer *leaf;
633
634                 leaf = path->nodes[0];
635                 existing = btrfs_item_ptr(leaf, path->slots[0],
636                                           struct btrfs_file_extent_item);
637
638                 read_extent_buffer(eb, &cmp1, (unsigned long)item,
639                                    sizeof(cmp1));
640                 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
641                                    sizeof(cmp2));
642
643                 /*
644                  * we already have a pointer to this exact extent,
645                  * we don't have to do anything
646                  */
647                 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
648                         btrfs_release_path(path);
649                         goto out;
650                 }
651         }
652         btrfs_release_path(path);
653
654         /* drop any overlapping extents */
655         ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
656         if (ret)
657                 goto out;
658
659         if (found_type == BTRFS_FILE_EXTENT_REG ||
660             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
661                 u64 offset;
662                 unsigned long dest_offset;
663                 struct btrfs_key ins;
664
665                 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
666                     btrfs_fs_incompat(fs_info, NO_HOLES))
667                         goto update_inode;
668
669                 ret = btrfs_insert_empty_item(trans, root, path, key,
670                                               sizeof(*item));
671                 if (ret)
672                         goto out;
673                 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
674                                                     path->slots[0]);
675                 copy_extent_buffer(path->nodes[0], eb, dest_offset,
676                                 (unsigned long)item,  sizeof(*item));
677
678                 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
679                 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
680                 ins.type = BTRFS_EXTENT_ITEM_KEY;
681                 offset = key->offset - btrfs_file_extent_offset(eb, item);
682
683                 /*
684                  * Manually record dirty extent, as here we did a shallow
685                  * file extent item copy and skip normal backref update,
686                  * but modifying extent tree all by ourselves.
687                  * So need to manually record dirty extent for qgroup,
688                  * as the owner of the file extent changed from log tree
689                  * (doesn't affect qgroup) to fs/file tree(affects qgroup)
690                  */
691                 ret = btrfs_qgroup_trace_extent(trans,
692                                 btrfs_file_extent_disk_bytenr(eb, item),
693                                 btrfs_file_extent_disk_num_bytes(eb, item),
694                                 GFP_NOFS);
695                 if (ret < 0)
696                         goto out;
697
698                 if (ins.objectid > 0) {
699                         u64 csum_start;
700                         u64 csum_end;
701                         LIST_HEAD(ordered_sums);
702                         /*
703                          * is this extent already allocated in the extent
704                          * allocation tree?  If so, just add a reference
705                          */
706                         ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
707                                                 ins.offset);
708                         if (ret == 0) {
709                                 ret = btrfs_inc_extent_ref(trans, root,
710                                                 ins.objectid, ins.offset,
711                                                 0, root->root_key.objectid,
712                                                 key->objectid, offset);
713                                 if (ret)
714                                         goto out;
715                         } else {
716                                 /*
717                                  * insert the extent pointer in the extent
718                                  * allocation tree
719                                  */
720                                 ret = btrfs_alloc_logged_file_extent(trans,
721                                                 root->root_key.objectid,
722                                                 key->objectid, offset, &ins);
723                                 if (ret)
724                                         goto out;
725                         }
726                         btrfs_release_path(path);
727
728                         if (btrfs_file_extent_compression(eb, item)) {
729                                 csum_start = ins.objectid;
730                                 csum_end = csum_start + ins.offset;
731                         } else {
732                                 csum_start = ins.objectid +
733                                         btrfs_file_extent_offset(eb, item);
734                                 csum_end = csum_start +
735                                         btrfs_file_extent_num_bytes(eb, item);
736                         }
737
738                         ret = btrfs_lookup_csums_range(root->log_root,
739                                                 csum_start, csum_end - 1,
740                                                 &ordered_sums, 0);
741                         if (ret)
742                                 goto out;
743                         /*
744                          * Now delete all existing cums in the csum root that
745                          * cover our range. We do this because we can have an
746                          * extent that is completely referenced by one file
747                          * extent item and partially referenced by another
748                          * file extent item (like after using the clone or
749                          * extent_same ioctls). In this case if we end up doing
750                          * the replay of the one that partially references the
751                          * extent first, and we do not do the csum deletion
752                          * below, we can get 2 csum items in the csum tree that
753                          * overlap each other. For example, imagine our log has
754                          * the two following file extent items:
755                          *
756                          * key (257 EXTENT_DATA 409600)
757                          *     extent data disk byte 12845056 nr 102400
758                          *     extent data offset 20480 nr 20480 ram 102400
759                          *
760                          * key (257 EXTENT_DATA 819200)
761                          *     extent data disk byte 12845056 nr 102400
762                          *     extent data offset 0 nr 102400 ram 102400
763                          *
764                          * Where the second one fully references the 100K extent
765                          * that starts at disk byte 12845056, and the log tree
766                          * has a single csum item that covers the entire range
767                          * of the extent:
768                          *
769                          * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
770                          *
771                          * After the first file extent item is replayed, the
772                          * csum tree gets the following csum item:
773                          *
774                          * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
775                          *
776                          * Which covers the 20K sub-range starting at offset 20K
777                          * of our extent. Now when we replay the second file
778                          * extent item, if we do not delete existing csum items
779                          * that cover any of its blocks, we end up getting two
780                          * csum items in our csum tree that overlap each other:
781                          *
782                          * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
783                          * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
784                          *
785                          * Which is a problem, because after this anyone trying
786                          * to lookup up for the checksum of any block of our
787                          * extent starting at an offset of 40K or higher, will
788                          * end up looking at the second csum item only, which
789                          * does not contain the checksum for any block starting
790                          * at offset 40K or higher of our extent.
791                          */
792                         while (!list_empty(&ordered_sums)) {
793                                 struct btrfs_ordered_sum *sums;
794                                 sums = list_entry(ordered_sums.next,
795                                                 struct btrfs_ordered_sum,
796                                                 list);
797                                 if (!ret)
798                                         ret = btrfs_del_csums(trans,
799                                                               fs_info->csum_root,
800                                                               sums->bytenr,
801                                                               sums->len);
802                                 if (!ret)
803                                         ret = btrfs_csum_file_blocks(trans,
804                                                 fs_info->csum_root, sums);
805                                 list_del(&sums->list);
806                                 kfree(sums);
807                         }
808                         if (ret)
809                                 goto out;
810                 } else {
811                         btrfs_release_path(path);
812                 }
813         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
814                 /* inline extents are easy, we just overwrite them */
815                 ret = overwrite_item(trans, root, path, eb, slot, key);
816                 if (ret)
817                         goto out;
818         }
819
820         inode_add_bytes(inode, nbytes);
821 update_inode:
822         ret = btrfs_update_inode(trans, root, inode);
823 out:
824         if (inode)
825                 iput(inode);
826         return ret;
827 }
828
829 /*
830  * when cleaning up conflicts between the directory names in the
831  * subvolume, directory names in the log and directory names in the
832  * inode back references, we may have to unlink inodes from directories.
833  *
834  * This is a helper function to do the unlink of a specific directory
835  * item
836  */
837 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
838                                       struct btrfs_root *root,
839                                       struct btrfs_path *path,
840                                       struct btrfs_inode *dir,
841                                       struct btrfs_dir_item *di)
842 {
843         struct inode *inode;
844         char *name;
845         int name_len;
846         struct extent_buffer *leaf;
847         struct btrfs_key location;
848         int ret;
849
850         leaf = path->nodes[0];
851
852         btrfs_dir_item_key_to_cpu(leaf, di, &location);
853         name_len = btrfs_dir_name_len(leaf, di);
854         name = kmalloc(name_len, GFP_NOFS);
855         if (!name)
856                 return -ENOMEM;
857
858         read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
859         btrfs_release_path(path);
860
861         inode = read_one_inode(root, location.objectid);
862         if (!inode) {
863                 ret = -EIO;
864                 goto out;
865         }
866
867         ret = link_to_fixup_dir(trans, root, path, location.objectid);
868         if (ret)
869                 goto out;
870
871         ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
872                         name_len);
873         if (ret)
874                 goto out;
875         else
876                 ret = btrfs_run_delayed_items(trans);
877 out:
878         kfree(name);
879         iput(inode);
880         return ret;
881 }
882
883 /*
884  * See if a given name and sequence number found in an inode back reference are
885  * already in a directory and correctly point to this inode.
886  *
887  * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
888  * exists.
889  */
890 static noinline int inode_in_dir(struct btrfs_root *root,
891                                  struct btrfs_path *path,
892                                  u64 dirid, u64 objectid, u64 index,
893                                  const char *name, int name_len)
894 {
895         struct btrfs_dir_item *di;
896         struct btrfs_key location;
897         int ret = 0;
898
899         di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
900                                          index, name, name_len, 0);
901         if (IS_ERR(di)) {
902                 if (PTR_ERR(di) != -ENOENT)
903                         ret = PTR_ERR(di);
904                 goto out;
905         } else if (di) {
906                 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
907                 if (location.objectid != objectid)
908                         goto out;
909         } else {
910                 goto out;
911         }
912
913         btrfs_release_path(path);
914         di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
915         if (IS_ERR(di)) {
916                 ret = PTR_ERR(di);
917                 goto out;
918         } else if (di) {
919                 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
920                 if (location.objectid == objectid)
921                         ret = 1;
922         }
923 out:
924         btrfs_release_path(path);
925         return ret;
926 }
927
928 /*
929  * helper function to check a log tree for a named back reference in
930  * an inode.  This is used to decide if a back reference that is
931  * found in the subvolume conflicts with what we find in the log.
932  *
933  * inode backreferences may have multiple refs in a single item,
934  * during replay we process one reference at a time, and we don't
935  * want to delete valid links to a file from the subvolume if that
936  * link is also in the log.
937  */
938 static noinline int backref_in_log(struct btrfs_root *log,
939                                    struct btrfs_key *key,
940                                    u64 ref_objectid,
941                                    const char *name, int namelen)
942 {
943         struct btrfs_path *path;
944         struct btrfs_inode_ref *ref;
945         unsigned long ptr;
946         unsigned long ptr_end;
947         unsigned long name_ptr;
948         int found_name_len;
949         int item_size;
950         int ret;
951         int match = 0;
952
953         path = btrfs_alloc_path();
954         if (!path)
955                 return -ENOMEM;
956
957         ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
958         if (ret != 0)
959                 goto out;
960
961         ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
962
963         if (key->type == BTRFS_INODE_EXTREF_KEY) {
964                 if (btrfs_find_name_in_ext_backref(path->nodes[0],
965                                                    path->slots[0],
966                                                    ref_objectid,
967                                                    name, namelen, NULL))
968                         match = 1;
969
970                 goto out;
971         }
972
973         item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
974         ptr_end = ptr + item_size;
975         while (ptr < ptr_end) {
976                 ref = (struct btrfs_inode_ref *)ptr;
977                 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
978                 if (found_name_len == namelen) {
979                         name_ptr = (unsigned long)(ref + 1);
980                         ret = memcmp_extent_buffer(path->nodes[0], name,
981                                                    name_ptr, namelen);
982                         if (ret == 0) {
983                                 match = 1;
984                                 goto out;
985                         }
986                 }
987                 ptr = (unsigned long)(ref + 1) + found_name_len;
988         }
989 out:
990         btrfs_free_path(path);
991         return match;
992 }
993
994 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
995                                   struct btrfs_root *root,
996                                   struct btrfs_path *path,
997                                   struct btrfs_root *log_root,
998                                   struct btrfs_inode *dir,
999                                   struct btrfs_inode *inode,
1000                                   u64 inode_objectid, u64 parent_objectid,
1001                                   u64 ref_index, char *name, int namelen,
1002                                   int *search_done)
1003 {
1004         int ret;
1005         char *victim_name;
1006         int victim_name_len;
1007         struct extent_buffer *leaf;
1008         struct btrfs_dir_item *di;
1009         struct btrfs_key search_key;
1010         struct btrfs_inode_extref *extref;
1011
1012 again:
1013         /* Search old style refs */
1014         search_key.objectid = inode_objectid;
1015         search_key.type = BTRFS_INODE_REF_KEY;
1016         search_key.offset = parent_objectid;
1017         ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1018         if (ret == 0) {
1019                 struct btrfs_inode_ref *victim_ref;
1020                 unsigned long ptr;
1021                 unsigned long ptr_end;
1022
1023                 leaf = path->nodes[0];
1024
1025                 /* are we trying to overwrite a back ref for the root directory
1026                  * if so, just jump out, we're done
1027                  */
1028                 if (search_key.objectid == search_key.offset)
1029                         return 1;
1030
1031                 /* check all the names in this back reference to see
1032                  * if they are in the log.  if so, we allow them to stay
1033                  * otherwise they must be unlinked as a conflict
1034                  */
1035                 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1036                 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1037                 while (ptr < ptr_end) {
1038                         victim_ref = (struct btrfs_inode_ref *)ptr;
1039                         victim_name_len = btrfs_inode_ref_name_len(leaf,
1040                                                                    victim_ref);
1041                         victim_name = kmalloc(victim_name_len, GFP_NOFS);
1042                         if (!victim_name)
1043                                 return -ENOMEM;
1044
1045                         read_extent_buffer(leaf, victim_name,
1046                                            (unsigned long)(victim_ref + 1),
1047                                            victim_name_len);
1048
1049                         if (!backref_in_log(log_root, &search_key,
1050                                             parent_objectid,
1051                                             victim_name,
1052                                             victim_name_len)) {
1053                                 inc_nlink(&inode->vfs_inode);
1054                                 btrfs_release_path(path);
1055
1056                                 ret = btrfs_unlink_inode(trans, root, dir, inode,
1057                                                 victim_name, victim_name_len);
1058                                 kfree(victim_name);
1059                                 if (ret)
1060                                         return ret;
1061                                 ret = btrfs_run_delayed_items(trans);
1062                                 if (ret)
1063                                         return ret;
1064                                 *search_done = 1;
1065                                 goto again;
1066                         }
1067                         kfree(victim_name);
1068
1069                         ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1070                 }
1071
1072                 /*
1073                  * NOTE: we have searched root tree and checked the
1074                  * corresponding ref, it does not need to check again.
1075                  */
1076                 *search_done = 1;
1077         }
1078         btrfs_release_path(path);
1079
1080         /* Same search but for extended refs */
1081         extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1082                                            inode_objectid, parent_objectid, 0,
1083                                            0);
1084         if (!IS_ERR_OR_NULL(extref)) {
1085                 u32 item_size;
1086                 u32 cur_offset = 0;
1087                 unsigned long base;
1088                 struct inode *victim_parent;
1089
1090                 leaf = path->nodes[0];
1091
1092                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1093                 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1094
1095                 while (cur_offset < item_size) {
1096                         extref = (struct btrfs_inode_extref *)(base + cur_offset);
1097
1098                         victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1099
1100                         if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1101                                 goto next;
1102
1103                         victim_name = kmalloc(victim_name_len, GFP_NOFS);
1104                         if (!victim_name)
1105                                 return -ENOMEM;
1106                         read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1107                                            victim_name_len);
1108
1109                         search_key.objectid = inode_objectid;
1110                         search_key.type = BTRFS_INODE_EXTREF_KEY;
1111                         search_key.offset = btrfs_extref_hash(parent_objectid,
1112                                                               victim_name,
1113                                                               victim_name_len);
1114                         ret = 0;
1115                         if (!backref_in_log(log_root, &search_key,
1116                                             parent_objectid, victim_name,
1117                                             victim_name_len)) {
1118                                 ret = -ENOENT;
1119                                 victim_parent = read_one_inode(root,
1120                                                 parent_objectid);
1121                                 if (victim_parent) {
1122                                         inc_nlink(&inode->vfs_inode);
1123                                         btrfs_release_path(path);
1124
1125                                         ret = btrfs_unlink_inode(trans, root,
1126                                                         BTRFS_I(victim_parent),
1127                                                         inode,
1128                                                         victim_name,
1129                                                         victim_name_len);
1130                                         if (!ret)
1131                                                 ret = btrfs_run_delayed_items(
1132                                                                   trans);
1133                                 }
1134                                 iput(victim_parent);
1135                                 kfree(victim_name);
1136                                 if (ret)
1137                                         return ret;
1138                                 *search_done = 1;
1139                                 goto again;
1140                         }
1141                         kfree(victim_name);
1142 next:
1143                         cur_offset += victim_name_len + sizeof(*extref);
1144                 }
1145                 *search_done = 1;
1146         }
1147         btrfs_release_path(path);
1148
1149         /* look for a conflicting sequence number */
1150         di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1151                                          ref_index, name, namelen, 0);
1152         if (IS_ERR(di)) {
1153                 if (PTR_ERR(di) != -ENOENT)
1154                         return PTR_ERR(di);
1155         } else if (di) {
1156                 ret = drop_one_dir_item(trans, root, path, dir, di);
1157                 if (ret)
1158                         return ret;
1159         }
1160         btrfs_release_path(path);
1161
1162         /* look for a conflicing name */
1163         di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1164                                    name, namelen, 0);
1165         if (IS_ERR(di)) {
1166                 return PTR_ERR(di);
1167         } else if (di) {
1168                 ret = drop_one_dir_item(trans, root, path, dir, di);
1169                 if (ret)
1170                         return ret;
1171         }
1172         btrfs_release_path(path);
1173
1174         return 0;
1175 }
1176
1177 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1178                              u32 *namelen, char **name, u64 *index,
1179                              u64 *parent_objectid)
1180 {
1181         struct btrfs_inode_extref *extref;
1182
1183         extref = (struct btrfs_inode_extref *)ref_ptr;
1184
1185         *namelen = btrfs_inode_extref_name_len(eb, extref);
1186         *name = kmalloc(*namelen, GFP_NOFS);
1187         if (*name == NULL)
1188                 return -ENOMEM;
1189
1190         read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1191                            *namelen);
1192
1193         if (index)
1194                 *index = btrfs_inode_extref_index(eb, extref);
1195         if (parent_objectid)
1196                 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1197
1198         return 0;
1199 }
1200
1201 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1202                           u32 *namelen, char **name, u64 *index)
1203 {
1204         struct btrfs_inode_ref *ref;
1205
1206         ref = (struct btrfs_inode_ref *)ref_ptr;
1207
1208         *namelen = btrfs_inode_ref_name_len(eb, ref);
1209         *name = kmalloc(*namelen, GFP_NOFS);
1210         if (*name == NULL)
1211                 return -ENOMEM;
1212
1213         read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1214
1215         if (index)
1216                 *index = btrfs_inode_ref_index(eb, ref);
1217
1218         return 0;
1219 }
1220
1221 /*
1222  * Take an inode reference item from the log tree and iterate all names from the
1223  * inode reference item in the subvolume tree with the same key (if it exists).
1224  * For any name that is not in the inode reference item from the log tree, do a
1225  * proper unlink of that name (that is, remove its entry from the inode
1226  * reference item and both dir index keys).
1227  */
1228 static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1229                                  struct btrfs_root *root,
1230                                  struct btrfs_path *path,
1231                                  struct btrfs_inode *inode,
1232                                  struct extent_buffer *log_eb,
1233                                  int log_slot,
1234                                  struct btrfs_key *key)
1235 {
1236         int ret;
1237         unsigned long ref_ptr;
1238         unsigned long ref_end;
1239         struct extent_buffer *eb;
1240
1241 again:
1242         btrfs_release_path(path);
1243         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1244         if (ret > 0) {
1245                 ret = 0;
1246                 goto out;
1247         }
1248         if (ret < 0)
1249                 goto out;
1250
1251         eb = path->nodes[0];
1252         ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1253         ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1254         while (ref_ptr < ref_end) {
1255                 char *name = NULL;
1256                 int namelen;
1257                 u64 parent_id;
1258
1259                 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1260                         ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1261                                                 NULL, &parent_id);
1262                 } else {
1263                         parent_id = key->offset;
1264                         ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1265                                              NULL);
1266                 }
1267                 if (ret)
1268                         goto out;
1269
1270                 if (key->type == BTRFS_INODE_EXTREF_KEY)
1271                         ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1272                                                              parent_id, name,
1273                                                              namelen, NULL);
1274                 else
1275                         ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1276                                                          namelen, NULL);
1277
1278                 if (!ret) {
1279                         struct inode *dir;
1280
1281                         btrfs_release_path(path);
1282                         dir = read_one_inode(root, parent_id);
1283                         if (!dir) {
1284                                 ret = -ENOENT;
1285                                 kfree(name);
1286                                 goto out;
1287                         }
1288                         ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1289                                                  inode, name, namelen);
1290                         kfree(name);
1291                         iput(dir);
1292                         /*
1293                          * Whenever we need to check if a name exists or not, we
1294                          * check the subvolume tree. So after an unlink we must
1295                          * run delayed items, so that future checks for a name
1296                          * during log replay see that the name does not exists
1297                          * anymore.
1298                          */
1299                         if (!ret)
1300                                 ret = btrfs_run_delayed_items(trans);
1301                         if (ret)
1302                                 goto out;
1303                         goto again;
1304                 }
1305
1306                 kfree(name);
1307                 ref_ptr += namelen;
1308                 if (key->type == BTRFS_INODE_EXTREF_KEY)
1309                         ref_ptr += sizeof(struct btrfs_inode_extref);
1310                 else
1311                         ref_ptr += sizeof(struct btrfs_inode_ref);
1312         }
1313         ret = 0;
1314  out:
1315         btrfs_release_path(path);
1316         return ret;
1317 }
1318
1319 static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1320                                   const u8 ref_type, const char *name,
1321                                   const int namelen)
1322 {
1323         struct btrfs_key key;
1324         struct btrfs_path *path;
1325         const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1326         int ret;
1327
1328         path = btrfs_alloc_path();
1329         if (!path)
1330                 return -ENOMEM;
1331
1332         key.objectid = btrfs_ino(BTRFS_I(inode));
1333         key.type = ref_type;
1334         if (key.type == BTRFS_INODE_REF_KEY)
1335                 key.offset = parent_id;
1336         else
1337                 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1338
1339         ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1340         if (ret < 0)
1341                 goto out;
1342         if (ret > 0) {
1343                 ret = 0;
1344                 goto out;
1345         }
1346         if (key.type == BTRFS_INODE_EXTREF_KEY)
1347                 ret = btrfs_find_name_in_ext_backref(path->nodes[0],
1348                                                      path->slots[0], parent_id,
1349                                                      name, namelen, NULL);
1350         else
1351                 ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1352                                                  name, namelen, NULL);
1353
1354 out:
1355         btrfs_free_path(path);
1356         return ret;
1357 }
1358
1359 /*
1360  * replay one inode back reference item found in the log tree.
1361  * eb, slot and key refer to the buffer and key found in the log tree.
1362  * root is the destination we are replaying into, and path is for temp
1363  * use by this function.  (it should be released on return).
1364  */
1365 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1366                                   struct btrfs_root *root,
1367                                   struct btrfs_root *log,
1368                                   struct btrfs_path *path,
1369                                   struct extent_buffer *eb, int slot,
1370                                   struct btrfs_key *key)
1371 {
1372         struct inode *dir = NULL;
1373         struct inode *inode = NULL;
1374         unsigned long ref_ptr;
1375         unsigned long ref_end;
1376         char *name = NULL;
1377         int namelen;
1378         int ret;
1379         int search_done = 0;
1380         int log_ref_ver = 0;
1381         u64 parent_objectid;
1382         u64 inode_objectid;
1383         u64 ref_index = 0;
1384         int ref_struct_size;
1385
1386         ref_ptr = btrfs_item_ptr_offset(eb, slot);
1387         ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1388
1389         if (key->type == BTRFS_INODE_EXTREF_KEY) {
1390                 struct btrfs_inode_extref *r;
1391
1392                 ref_struct_size = sizeof(struct btrfs_inode_extref);
1393                 log_ref_ver = 1;
1394                 r = (struct btrfs_inode_extref *)ref_ptr;
1395                 parent_objectid = btrfs_inode_extref_parent(eb, r);
1396         } else {
1397                 ref_struct_size = sizeof(struct btrfs_inode_ref);
1398                 parent_objectid = key->offset;
1399         }
1400         inode_objectid = key->objectid;
1401
1402         /*
1403          * it is possible that we didn't log all the parent directories
1404          * for a given inode.  If we don't find the dir, just don't
1405          * copy the back ref in.  The link count fixup code will take
1406          * care of the rest
1407          */
1408         dir = read_one_inode(root, parent_objectid);
1409         if (!dir) {
1410                 ret = -ENOENT;
1411                 goto out;
1412         }
1413
1414         inode = read_one_inode(root, inode_objectid);
1415         if (!inode) {
1416                 ret = -EIO;
1417                 goto out;
1418         }
1419
1420         while (ref_ptr < ref_end) {
1421                 if (log_ref_ver) {
1422                         ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1423                                                 &ref_index, &parent_objectid);
1424                         /*
1425                          * parent object can change from one array
1426                          * item to another.
1427                          */
1428                         if (!dir)
1429                                 dir = read_one_inode(root, parent_objectid);
1430                         if (!dir) {
1431                                 ret = -ENOENT;
1432                                 goto out;
1433                         }
1434                 } else {
1435                         ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1436                                              &ref_index);
1437                 }
1438                 if (ret)
1439                         goto out;
1440
1441                 ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1442                                    btrfs_ino(BTRFS_I(inode)), ref_index,
1443                                    name, namelen);
1444                 if (ret < 0) {
1445                         goto out;
1446                 } else if (ret == 0) {
1447                         /*
1448                          * look for a conflicting back reference in the
1449                          * metadata. if we find one we have to unlink that name
1450                          * of the file before we add our new link.  Later on, we
1451                          * overwrite any existing back reference, and we don't
1452                          * want to create dangling pointers in the directory.
1453                          */
1454
1455                         if (!search_done) {
1456                                 ret = __add_inode_ref(trans, root, path, log,
1457                                                       BTRFS_I(dir),
1458                                                       BTRFS_I(inode),
1459                                                       inode_objectid,
1460                                                       parent_objectid,
1461                                                       ref_index, name, namelen,
1462                                                       &search_done);
1463                                 if (ret) {
1464                                         if (ret == 1)
1465                                                 ret = 0;
1466                                         goto out;
1467                                 }
1468                         }
1469
1470                         /*
1471                          * If a reference item already exists for this inode
1472                          * with the same parent and name, but different index,
1473                          * drop it and the corresponding directory index entries
1474                          * from the parent before adding the new reference item
1475                          * and dir index entries, otherwise we would fail with
1476                          * -EEXIST returned from btrfs_add_link() below.
1477                          */
1478                         ret = btrfs_inode_ref_exists(inode, dir, key->type,
1479                                                      name, namelen);
1480                         if (ret > 0) {
1481                                 ret = btrfs_unlink_inode(trans, root,
1482                                                          BTRFS_I(dir),
1483                                                          BTRFS_I(inode),
1484                                                          name, namelen);
1485                                 /*
1486                                  * If we dropped the link count to 0, bump it so
1487                                  * that later the iput() on the inode will not
1488                                  * free it. We will fixup the link count later.
1489                                  */
1490                                 if (!ret && inode->i_nlink == 0)
1491                                         inc_nlink(inode);
1492                                 /*
1493                                  * Whenever we need to check if a name exists or
1494                                  * not, we check the subvolume tree. So after an
1495                                  * unlink we must run delayed items, so that future
1496                                  * checks for a name during log replay see that the
1497                                  * name does not exists anymore.
1498                                  */
1499                                 if (!ret)
1500                                         ret = btrfs_run_delayed_items(trans);
1501                         }
1502                         if (ret < 0)
1503                                 goto out;
1504
1505                         /* insert our name */
1506                         ret = btrfs_add_link(trans, BTRFS_I(dir),
1507                                         BTRFS_I(inode),
1508                                         name, namelen, 0, ref_index);
1509                         if (ret)
1510                                 goto out;
1511
1512                         btrfs_update_inode(trans, root, inode);
1513                 }
1514                 /* Else, ret == 1, we already have a perfect match, we're done. */
1515
1516                 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1517                 kfree(name);
1518                 name = NULL;
1519                 if (log_ref_ver) {
1520                         iput(dir);
1521                         dir = NULL;
1522                 }
1523         }
1524
1525         /*
1526          * Before we overwrite the inode reference item in the subvolume tree
1527          * with the item from the log tree, we must unlink all names from the
1528          * parent directory that are in the subvolume's tree inode reference
1529          * item, otherwise we end up with an inconsistent subvolume tree where
1530          * dir index entries exist for a name but there is no inode reference
1531          * item with the same name.
1532          */
1533         ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1534                                     key);
1535         if (ret)
1536                 goto out;
1537
1538         /* finally write the back reference in the inode */
1539         ret = overwrite_item(trans, root, path, eb, slot, key);
1540 out:
1541         btrfs_release_path(path);
1542         kfree(name);
1543         iput(dir);
1544         iput(inode);
1545         return ret;
1546 }
1547
1548 static int insert_orphan_item(struct btrfs_trans_handle *trans,
1549                               struct btrfs_root *root, u64 ino)
1550 {
1551         int ret;
1552
1553         ret = btrfs_insert_orphan_item(trans, root, ino);
1554         if (ret == -EEXIST)
1555                 ret = 0;
1556
1557         return ret;
1558 }
1559
1560 static int count_inode_extrefs(struct btrfs_root *root,
1561                 struct btrfs_inode *inode, struct btrfs_path *path)
1562 {
1563         int ret = 0;
1564         int name_len;
1565         unsigned int nlink = 0;
1566         u32 item_size;
1567         u32 cur_offset = 0;
1568         u64 inode_objectid = btrfs_ino(inode);
1569         u64 offset = 0;
1570         unsigned long ptr;
1571         struct btrfs_inode_extref *extref;
1572         struct extent_buffer *leaf;
1573
1574         while (1) {
1575                 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1576                                             &extref, &offset);
1577                 if (ret)
1578                         break;
1579
1580                 leaf = path->nodes[0];
1581                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1582                 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1583                 cur_offset = 0;
1584
1585                 while (cur_offset < item_size) {
1586                         extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1587                         name_len = btrfs_inode_extref_name_len(leaf, extref);
1588
1589                         nlink++;
1590
1591                         cur_offset += name_len + sizeof(*extref);
1592                 }
1593
1594                 offset++;
1595                 btrfs_release_path(path);
1596         }
1597         btrfs_release_path(path);
1598
1599         if (ret < 0 && ret != -ENOENT)
1600                 return ret;
1601         return nlink;
1602 }
1603
1604 static int count_inode_refs(struct btrfs_root *root,
1605                         struct btrfs_inode *inode, struct btrfs_path *path)
1606 {
1607         int ret;
1608         struct btrfs_key key;
1609         unsigned int nlink = 0;
1610         unsigned long ptr;
1611         unsigned long ptr_end;
1612         int name_len;
1613         u64 ino = btrfs_ino(inode);
1614
1615         key.objectid = ino;
1616         key.type = BTRFS_INODE_REF_KEY;
1617         key.offset = (u64)-1;
1618
1619         while (1) {
1620                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1621                 if (ret < 0)
1622                         break;
1623                 if (ret > 0) {
1624                         if (path->slots[0] == 0)
1625                                 break;
1626                         path->slots[0]--;
1627                 }
1628 process_slot:
1629                 btrfs_item_key_to_cpu(path->nodes[0], &key,
1630                                       path->slots[0]);
1631                 if (key.objectid != ino ||
1632                     key.type != BTRFS_INODE_REF_KEY)
1633                         break;
1634                 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1635                 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1636                                                    path->slots[0]);
1637                 while (ptr < ptr_end) {
1638                         struct btrfs_inode_ref *ref;
1639
1640                         ref = (struct btrfs_inode_ref *)ptr;
1641                         name_len = btrfs_inode_ref_name_len(path->nodes[0],
1642                                                             ref);
1643                         ptr = (unsigned long)(ref + 1) + name_len;
1644                         nlink++;
1645                 }
1646
1647                 if (key.offset == 0)
1648                         break;
1649                 if (path->slots[0] > 0) {
1650                         path->slots[0]--;
1651                         goto process_slot;
1652                 }
1653                 key.offset--;
1654                 btrfs_release_path(path);
1655         }
1656         btrfs_release_path(path);
1657
1658         return nlink;
1659 }
1660
1661 /*
1662  * There are a few corners where the link count of the file can't
1663  * be properly maintained during replay.  So, instead of adding
1664  * lots of complexity to the log code, we just scan the backrefs
1665  * for any file that has been through replay.
1666  *
1667  * The scan will update the link count on the inode to reflect the
1668  * number of back refs found.  If it goes down to zero, the iput
1669  * will free the inode.
1670  */
1671 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1672                                            struct btrfs_root *root,
1673                                            struct inode *inode)
1674 {
1675         struct btrfs_path *path;
1676         int ret;
1677         u64 nlink = 0;
1678         u64 ino = btrfs_ino(BTRFS_I(inode));
1679
1680         path = btrfs_alloc_path();
1681         if (!path)
1682                 return -ENOMEM;
1683
1684         ret = count_inode_refs(root, BTRFS_I(inode), path);
1685         if (ret < 0)
1686                 goto out;
1687
1688         nlink = ret;
1689
1690         ret = count_inode_extrefs(root, BTRFS_I(inode), path);
1691         if (ret < 0)
1692                 goto out;
1693
1694         nlink += ret;
1695
1696         ret = 0;
1697
1698         if (nlink != inode->i_nlink) {
1699                 set_nlink(inode, nlink);
1700                 btrfs_update_inode(trans, root, inode);
1701         }
1702         BTRFS_I(inode)->index_cnt = (u64)-1;
1703
1704         if (inode->i_nlink == 0) {
1705                 if (S_ISDIR(inode->i_mode)) {
1706                         ret = replay_dir_deletes(trans, root, NULL, path,
1707                                                  ino, 1);
1708                         if (ret)
1709                                 goto out;
1710                 }
1711                 ret = insert_orphan_item(trans, root, ino);
1712         }
1713
1714 out:
1715         btrfs_free_path(path);
1716         return ret;
1717 }
1718
1719 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1720                                             struct btrfs_root *root,
1721                                             struct btrfs_path *path)
1722 {
1723         int ret;
1724         struct btrfs_key key;
1725         struct inode *inode;
1726
1727         key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1728         key.type = BTRFS_ORPHAN_ITEM_KEY;
1729         key.offset = (u64)-1;
1730         while (1) {
1731                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1732                 if (ret < 0)
1733                         break;
1734
1735                 if (ret == 1) {
1736                         ret = 0;
1737                         if (path->slots[0] == 0)
1738                                 break;
1739                         path->slots[0]--;
1740                 }
1741
1742                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1743                 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1744                     key.type != BTRFS_ORPHAN_ITEM_KEY)
1745                         break;
1746
1747                 ret = btrfs_del_item(trans, root, path);
1748                 if (ret)
1749                         break;
1750
1751                 btrfs_release_path(path);
1752                 inode = read_one_inode(root, key.offset);
1753                 if (!inode) {
1754                         ret = -EIO;
1755                         break;
1756                 }
1757
1758                 ret = fixup_inode_link_count(trans, root, inode);
1759                 iput(inode);
1760                 if (ret)
1761                         break;
1762
1763                 /*
1764                  * fixup on a directory may create new entries,
1765                  * make sure we always look for the highset possible
1766                  * offset
1767                  */
1768                 key.offset = (u64)-1;
1769         }
1770         btrfs_release_path(path);
1771         return ret;
1772 }
1773
1774
1775 /*
1776  * record a given inode in the fixup dir so we can check its link
1777  * count when replay is done.  The link count is incremented here
1778  * so the inode won't go away until we check it
1779  */
1780 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1781                                       struct btrfs_root *root,
1782                                       struct btrfs_path *path,
1783                                       u64 objectid)
1784 {
1785         struct btrfs_key key;
1786         int ret = 0;
1787         struct inode *inode;
1788
1789         inode = read_one_inode(root, objectid);
1790         if (!inode)
1791                 return -EIO;
1792
1793         key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1794         key.type = BTRFS_ORPHAN_ITEM_KEY;
1795         key.offset = objectid;
1796
1797         ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1798
1799         btrfs_release_path(path);
1800         if (ret == 0) {
1801                 if (!inode->i_nlink)
1802                         set_nlink(inode, 1);
1803                 else
1804                         inc_nlink(inode);
1805                 ret = btrfs_update_inode(trans, root, inode);
1806         } else if (ret == -EEXIST) {
1807                 ret = 0;
1808         }
1809         iput(inode);
1810
1811         return ret;
1812 }
1813
1814 /*
1815  * when replaying the log for a directory, we only insert names
1816  * for inodes that actually exist.  This means an fsync on a directory
1817  * does not implicitly fsync all the new files in it
1818  */
1819 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1820                                     struct btrfs_root *root,
1821                                     u64 dirid, u64 index,
1822                                     char *name, int name_len,
1823                                     struct btrfs_key *location)
1824 {
1825         struct inode *inode;
1826         struct inode *dir;
1827         int ret;
1828
1829         inode = read_one_inode(root, location->objectid);
1830         if (!inode)
1831                 return -ENOENT;
1832
1833         dir = read_one_inode(root, dirid);
1834         if (!dir) {
1835                 iput(inode);
1836                 return -EIO;
1837         }
1838
1839         ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1840                         name_len, 1, index);
1841
1842         /* FIXME, put inode into FIXUP list */
1843
1844         iput(inode);
1845         iput(dir);
1846         return ret;
1847 }
1848
1849 /*
1850  * Return true if an inode reference exists in the log for the given name,
1851  * inode and parent inode.
1852  */
1853 static bool name_in_log_ref(struct btrfs_root *log_root,
1854                             const char *name, const int name_len,
1855                             const u64 dirid, const u64 ino)
1856 {
1857         struct btrfs_key search_key;
1858
1859         search_key.objectid = ino;
1860         search_key.type = BTRFS_INODE_REF_KEY;
1861         search_key.offset = dirid;
1862         if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1863                 return true;
1864
1865         search_key.type = BTRFS_INODE_EXTREF_KEY;
1866         search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1867         if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1868                 return true;
1869
1870         return false;
1871 }
1872
1873 /*
1874  * take a single entry in a log directory item and replay it into
1875  * the subvolume.
1876  *
1877  * if a conflicting item exists in the subdirectory already,
1878  * the inode it points to is unlinked and put into the link count
1879  * fix up tree.
1880  *
1881  * If a name from the log points to a file or directory that does
1882  * not exist in the FS, it is skipped.  fsyncs on directories
1883  * do not force down inodes inside that directory, just changes to the
1884  * names or unlinks in a directory.
1885  *
1886  * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1887  * non-existing inode) and 1 if the name was replayed.
1888  */
1889 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1890                                     struct btrfs_root *root,
1891                                     struct btrfs_path *path,
1892                                     struct extent_buffer *eb,
1893                                     struct btrfs_dir_item *di,
1894                                     struct btrfs_key *key)
1895 {
1896         char *name;
1897         int name_len;
1898         struct btrfs_dir_item *dst_di;
1899         struct btrfs_key found_key;
1900         struct btrfs_key log_key;
1901         struct inode *dir;
1902         u8 log_type;
1903         bool exists;
1904         int ret;
1905         bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
1906         bool name_added = false;
1907
1908         dir = read_one_inode(root, key->objectid);
1909         if (!dir)
1910                 return -EIO;
1911
1912         name_len = btrfs_dir_name_len(eb, di);
1913         name = kmalloc(name_len, GFP_NOFS);
1914         if (!name) {
1915                 ret = -ENOMEM;
1916                 goto out;
1917         }
1918
1919         log_type = btrfs_dir_type(eb, di);
1920         read_extent_buffer(eb, name, (unsigned long)(di + 1),
1921                    name_len);
1922
1923         btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1924         ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1925         btrfs_release_path(path);
1926         if (ret < 0)
1927                 goto out;
1928         exists = (ret == 0);
1929         ret = 0;
1930
1931         if (key->type == BTRFS_DIR_ITEM_KEY) {
1932                 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1933                                        name, name_len, 1);
1934         } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1935                 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1936                                                      key->objectid,
1937                                                      key->offset, name,
1938                                                      name_len, 1);
1939         } else {
1940                 /* Corruption */
1941                 ret = -EINVAL;
1942                 goto out;
1943         }
1944
1945         if (dst_di == ERR_PTR(-ENOENT))
1946                 dst_di = NULL;
1947
1948         if (IS_ERR(dst_di)) {
1949                 ret = PTR_ERR(dst_di);
1950                 goto out;
1951         } else if (!dst_di) {
1952                 /* we need a sequence number to insert, so we only
1953                  * do inserts for the BTRFS_DIR_INDEX_KEY types
1954                  */
1955                 if (key->type != BTRFS_DIR_INDEX_KEY)
1956                         goto out;
1957                 goto insert;
1958         }
1959
1960         btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1961         /* the existing item matches the logged item */
1962         if (found_key.objectid == log_key.objectid &&
1963             found_key.type == log_key.type &&
1964             found_key.offset == log_key.offset &&
1965             btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1966                 update_size = false;
1967                 goto out;
1968         }
1969
1970         /*
1971          * don't drop the conflicting directory entry if the inode
1972          * for the new entry doesn't exist
1973          */
1974         if (!exists)
1975                 goto out;
1976
1977         ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
1978         if (ret)
1979                 goto out;
1980
1981         if (key->type == BTRFS_DIR_INDEX_KEY)
1982                 goto insert;
1983 out:
1984         btrfs_release_path(path);
1985         if (!ret && update_size) {
1986                 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
1987                 ret = btrfs_update_inode(trans, root, dir);
1988         }
1989         kfree(name);
1990         iput(dir);
1991         if (!ret && name_added)
1992                 ret = 1;
1993         return ret;
1994
1995 insert:
1996         if (name_in_log_ref(root->log_root, name, name_len,
1997                             key->objectid, log_key.objectid)) {
1998                 /* The dentry will be added later. */
1999                 ret = 0;
2000                 update_size = false;
2001                 goto out;
2002         }
2003         btrfs_release_path(path);
2004         ret = insert_one_name(trans, root, key->objectid, key->offset,
2005                               name, name_len, &log_key);
2006         if (ret && ret != -ENOENT && ret != -EEXIST)
2007                 goto out;
2008         if (!ret)
2009                 name_added = true;
2010         update_size = false;
2011         ret = 0;
2012         goto out;
2013 }
2014
2015 /*
2016  * find all the names in a directory item and reconcile them into
2017  * the subvolume.  Only BTRFS_DIR_ITEM_KEY types will have more than
2018  * one name in a directory item, but the same code gets used for
2019  * both directory index types
2020  */
2021 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2022                                         struct btrfs_root *root,
2023                                         struct btrfs_path *path,
2024                                         struct extent_buffer *eb, int slot,
2025                                         struct btrfs_key *key)
2026 {
2027         int ret = 0;
2028         u32 item_size = btrfs_item_size_nr(eb, slot);
2029         struct btrfs_dir_item *di;
2030         int name_len;
2031         unsigned long ptr;
2032         unsigned long ptr_end;
2033         struct btrfs_path *fixup_path = NULL;
2034
2035         ptr = btrfs_item_ptr_offset(eb, slot);
2036         ptr_end = ptr + item_size;
2037         while (ptr < ptr_end) {
2038                 di = (struct btrfs_dir_item *)ptr;
2039                 name_len = btrfs_dir_name_len(eb, di);
2040                 ret = replay_one_name(trans, root, path, eb, di, key);
2041                 if (ret < 0)
2042                         break;
2043                 ptr = (unsigned long)(di + 1);
2044                 ptr += name_len;
2045
2046                 /*
2047                  * If this entry refers to a non-directory (directories can not
2048                  * have a link count > 1) and it was added in the transaction
2049                  * that was not committed, make sure we fixup the link count of
2050                  * the inode it the entry points to. Otherwise something like
2051                  * the following would result in a directory pointing to an
2052                  * inode with a wrong link that does not account for this dir
2053                  * entry:
2054                  *
2055                  * mkdir testdir
2056                  * touch testdir/foo
2057                  * touch testdir/bar
2058                  * sync
2059                  *
2060                  * ln testdir/bar testdir/bar_link
2061                  * ln testdir/foo testdir/foo_link
2062                  * xfs_io -c "fsync" testdir/bar
2063                  *
2064                  * <power failure>
2065                  *
2066                  * mount fs, log replay happens
2067                  *
2068                  * File foo would remain with a link count of 1 when it has two
2069                  * entries pointing to it in the directory testdir. This would
2070                  * make it impossible to ever delete the parent directory has
2071                  * it would result in stale dentries that can never be deleted.
2072                  */
2073                 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2074                         struct btrfs_key di_key;
2075
2076                         if (!fixup_path) {
2077                                 fixup_path = btrfs_alloc_path();
2078                                 if (!fixup_path) {
2079                                         ret = -ENOMEM;
2080                                         break;
2081                                 }
2082                         }
2083
2084                         btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2085                         ret = link_to_fixup_dir(trans, root, fixup_path,
2086                                                 di_key.objectid);
2087                         if (ret)
2088                                 break;
2089                 }
2090                 ret = 0;
2091         }
2092         btrfs_free_path(fixup_path);
2093         return ret;
2094 }
2095
2096 /*
2097  * directory replay has two parts.  There are the standard directory
2098  * items in the log copied from the subvolume, and range items
2099  * created in the log while the subvolume was logged.
2100  *
2101  * The range items tell us which parts of the key space the log
2102  * is authoritative for.  During replay, if a key in the subvolume
2103  * directory is in a logged range item, but not actually in the log
2104  * that means it was deleted from the directory before the fsync
2105  * and should be removed.
2106  */
2107 static noinline int find_dir_range(struct btrfs_root *root,
2108                                    struct btrfs_path *path,
2109                                    u64 dirid, int key_type,
2110                                    u64 *start_ret, u64 *end_ret)
2111 {
2112         struct btrfs_key key;
2113         u64 found_end;
2114         struct btrfs_dir_log_item *item;
2115         int ret;
2116         int nritems;
2117
2118         if (*start_ret == (u64)-1)
2119                 return 1;
2120
2121         key.objectid = dirid;
2122         key.type = key_type;
2123         key.offset = *start_ret;
2124
2125         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2126         if (ret < 0)
2127                 goto out;
2128         if (ret > 0) {
2129                 if (path->slots[0] == 0)
2130                         goto out;
2131                 path->slots[0]--;
2132         }
2133         if (ret != 0)
2134                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2135
2136         if (key.type != key_type || key.objectid != dirid) {
2137                 ret = 1;
2138                 goto next;
2139         }
2140         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2141                               struct btrfs_dir_log_item);
2142         found_end = btrfs_dir_log_end(path->nodes[0], item);
2143
2144         if (*start_ret >= key.offset && *start_ret <= found_end) {
2145                 ret = 0;
2146                 *start_ret = key.offset;
2147                 *end_ret = found_end;
2148                 goto out;
2149         }
2150         ret = 1;
2151 next:
2152         /* check the next slot in the tree to see if it is a valid item */
2153         nritems = btrfs_header_nritems(path->nodes[0]);
2154         path->slots[0]++;
2155         if (path->slots[0] >= nritems) {
2156                 ret = btrfs_next_leaf(root, path);
2157                 if (ret)
2158                         goto out;
2159         }
2160
2161         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2162
2163         if (key.type != key_type || key.objectid != dirid) {
2164                 ret = 1;
2165                 goto out;
2166         }
2167         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2168                               struct btrfs_dir_log_item);
2169         found_end = btrfs_dir_log_end(path->nodes[0], item);
2170         *start_ret = key.offset;
2171         *end_ret = found_end;
2172         ret = 0;
2173 out:
2174         btrfs_release_path(path);
2175         return ret;
2176 }
2177
2178 /*
2179  * this looks for a given directory item in the log.  If the directory
2180  * item is not in the log, the item is removed and the inode it points
2181  * to is unlinked
2182  */
2183 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2184                                       struct btrfs_root *root,
2185                                       struct btrfs_root *log,
2186                                       struct btrfs_path *path,
2187                                       struct btrfs_path *log_path,
2188                                       struct inode *dir,
2189                                       struct btrfs_key *dir_key)
2190 {
2191         int ret;
2192         struct extent_buffer *eb;
2193         int slot;
2194         u32 item_size;
2195         struct btrfs_dir_item *di;
2196         struct btrfs_dir_item *log_di;
2197         int name_len;
2198         unsigned long ptr;
2199         unsigned long ptr_end;
2200         char *name;
2201         struct inode *inode;
2202         struct btrfs_key location;
2203
2204 again:
2205         eb = path->nodes[0];
2206         slot = path->slots[0];
2207         item_size = btrfs_item_size_nr(eb, slot);
2208         ptr = btrfs_item_ptr_offset(eb, slot);
2209         ptr_end = ptr + item_size;
2210         while (ptr < ptr_end) {
2211                 di = (struct btrfs_dir_item *)ptr;
2212                 name_len = btrfs_dir_name_len(eb, di);
2213                 name = kmalloc(name_len, GFP_NOFS);
2214                 if (!name) {
2215                         ret = -ENOMEM;
2216                         goto out;
2217                 }
2218                 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2219                                   name_len);
2220                 log_di = NULL;
2221                 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
2222                         log_di = btrfs_lookup_dir_item(trans, log, log_path,
2223                                                        dir_key->objectid,
2224                                                        name, name_len, 0);
2225                 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
2226                         log_di = btrfs_lookup_dir_index_item(trans, log,
2227                                                      log_path,
2228                                                      dir_key->objectid,
2229                                                      dir_key->offset,
2230                                                      name, name_len, 0);
2231                 }
2232                 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
2233                         btrfs_dir_item_key_to_cpu(eb, di, &location);
2234                         btrfs_release_path(path);
2235                         btrfs_release_path(log_path);
2236                         inode = read_one_inode(root, location.objectid);
2237                         if (!inode) {
2238                                 kfree(name);
2239                                 return -EIO;
2240                         }
2241
2242                         ret = link_to_fixup_dir(trans, root,
2243                                                 path, location.objectid);
2244                         if (ret) {
2245                                 kfree(name);
2246                                 iput(inode);
2247                                 goto out;
2248                         }
2249
2250                         inc_nlink(inode);
2251                         ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2252                                         BTRFS_I(inode), name, name_len);
2253                         if (!ret)
2254                                 ret = btrfs_run_delayed_items(trans);
2255                         kfree(name);
2256                         iput(inode);
2257                         if (ret)
2258                                 goto out;
2259
2260                         /* there might still be more names under this key
2261                          * check and repeat if required
2262                          */
2263                         ret = btrfs_search_slot(NULL, root, dir_key, path,
2264                                                 0, 0);
2265                         if (ret == 0)
2266                                 goto again;
2267                         ret = 0;
2268                         goto out;
2269                 } else if (IS_ERR(log_di)) {
2270                         kfree(name);
2271                         return PTR_ERR(log_di);
2272                 }
2273                 btrfs_release_path(log_path);
2274                 kfree(name);
2275
2276                 ptr = (unsigned long)(di + 1);
2277                 ptr += name_len;
2278         }
2279         ret = 0;
2280 out:
2281         btrfs_release_path(path);
2282         btrfs_release_path(log_path);
2283         return ret;
2284 }
2285
2286 static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2287                               struct btrfs_root *root,
2288                               struct btrfs_root *log,
2289                               struct btrfs_path *path,
2290                               const u64 ino)
2291 {
2292         struct btrfs_key search_key;
2293         struct btrfs_path *log_path;
2294         int i;
2295         int nritems;
2296         int ret;
2297
2298         log_path = btrfs_alloc_path();
2299         if (!log_path)
2300                 return -ENOMEM;
2301
2302         search_key.objectid = ino;
2303         search_key.type = BTRFS_XATTR_ITEM_KEY;
2304         search_key.offset = 0;
2305 again:
2306         ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2307         if (ret < 0)
2308                 goto out;
2309 process_leaf:
2310         nritems = btrfs_header_nritems(path->nodes[0]);
2311         for (i = path->slots[0]; i < nritems; i++) {
2312                 struct btrfs_key key;
2313                 struct btrfs_dir_item *di;
2314                 struct btrfs_dir_item *log_di;
2315                 u32 total_size;
2316                 u32 cur;
2317
2318                 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2319                 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2320                         ret = 0;
2321                         goto out;
2322                 }
2323
2324                 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2325                 total_size = btrfs_item_size_nr(path->nodes[0], i);
2326                 cur = 0;
2327                 while (cur < total_size) {
2328                         u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2329                         u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2330                         u32 this_len = sizeof(*di) + name_len + data_len;
2331                         char *name;
2332
2333                         name = kmalloc(name_len, GFP_NOFS);
2334                         if (!name) {
2335                                 ret = -ENOMEM;
2336                                 goto out;
2337                         }
2338                         read_extent_buffer(path->nodes[0], name,
2339                                            (unsigned long)(di + 1), name_len);
2340
2341                         log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2342                                                     name, name_len, 0);
2343                         btrfs_release_path(log_path);
2344                         if (!log_di) {
2345                                 /* Doesn't exist in log tree, so delete it. */
2346                                 btrfs_release_path(path);
2347                                 di = btrfs_lookup_xattr(trans, root, path, ino,
2348                                                         name, name_len, -1);
2349                                 kfree(name);
2350                                 if (IS_ERR(di)) {
2351                                         ret = PTR_ERR(di);
2352                                         goto out;
2353                                 }
2354                                 ASSERT(di);
2355                                 ret = btrfs_delete_one_dir_name(trans, root,
2356                                                                 path, di);
2357                                 if (ret)
2358                                         goto out;
2359                                 btrfs_release_path(path);
2360                                 search_key = key;
2361                                 goto again;
2362                         }
2363                         kfree(name);
2364                         if (IS_ERR(log_di)) {
2365                                 ret = PTR_ERR(log_di);
2366                                 goto out;
2367                         }
2368                         cur += this_len;
2369                         di = (struct btrfs_dir_item *)((char *)di + this_len);
2370                 }
2371         }
2372         ret = btrfs_next_leaf(root, path);
2373         if (ret > 0)
2374                 ret = 0;
2375         else if (ret == 0)
2376                 goto process_leaf;
2377 out:
2378         btrfs_free_path(log_path);
2379         btrfs_release_path(path);
2380         return ret;
2381 }
2382
2383
2384 /*
2385  * deletion replay happens before we copy any new directory items
2386  * out of the log or out of backreferences from inodes.  It
2387  * scans the log to find ranges of keys that log is authoritative for,
2388  * and then scans the directory to find items in those ranges that are
2389  * not present in the log.
2390  *
2391  * Anything we don't find in the log is unlinked and removed from the
2392  * directory.
2393  */
2394 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2395                                        struct btrfs_root *root,
2396                                        struct btrfs_root *log,
2397                                        struct btrfs_path *path,
2398                                        u64 dirid, int del_all)
2399 {
2400         u64 range_start;
2401         u64 range_end;
2402         int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2403         int ret = 0;
2404         struct btrfs_key dir_key;
2405         struct btrfs_key found_key;
2406         struct btrfs_path *log_path;
2407         struct inode *dir;
2408
2409         dir_key.objectid = dirid;
2410         dir_key.type = BTRFS_DIR_ITEM_KEY;
2411         log_path = btrfs_alloc_path();
2412         if (!log_path)
2413                 return -ENOMEM;
2414
2415         dir = read_one_inode(root, dirid);
2416         /* it isn't an error if the inode isn't there, that can happen
2417          * because we replay the deletes before we copy in the inode item
2418          * from the log
2419          */
2420         if (!dir) {
2421                 btrfs_free_path(log_path);
2422                 return 0;
2423         }
2424 again:
2425         range_start = 0;
2426         range_end = 0;
2427         while (1) {
2428                 if (del_all)
2429                         range_end = (u64)-1;
2430                 else {
2431                         ret = find_dir_range(log, path, dirid, key_type,
2432                                              &range_start, &range_end);
2433                         if (ret < 0)
2434                                 goto out;
2435                         else if (ret > 0)
2436                                 break;
2437                 }
2438
2439                 dir_key.offset = range_start;
2440                 while (1) {
2441                         int nritems;
2442                         ret = btrfs_search_slot(NULL, root, &dir_key, path,
2443                                                 0, 0);
2444                         if (ret < 0)
2445                                 goto out;
2446
2447                         nritems = btrfs_header_nritems(path->nodes[0]);
2448                         if (path->slots[0] >= nritems) {
2449                                 ret = btrfs_next_leaf(root, path);
2450                                 if (ret == 1)
2451                                         break;
2452                                 else if (ret < 0)
2453                                         goto out;
2454                         }
2455                         btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2456                                               path->slots[0]);
2457                         if (found_key.objectid != dirid ||
2458                             found_key.type != dir_key.type)
2459                                 goto next_type;
2460
2461                         if (found_key.offset > range_end)
2462                                 break;
2463
2464                         ret = check_item_in_log(trans, root, log, path,
2465                                                 log_path, dir,
2466                                                 &found_key);
2467                         if (ret)
2468                                 goto out;
2469                         if (found_key.offset == (u64)-1)
2470                                 break;
2471                         dir_key.offset = found_key.offset + 1;
2472                 }
2473                 btrfs_release_path(path);
2474                 if (range_end == (u64)-1)
2475                         break;
2476                 range_start = range_end + 1;
2477         }
2478
2479 next_type:
2480         ret = 0;
2481         if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2482                 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2483                 dir_key.type = BTRFS_DIR_INDEX_KEY;
2484                 btrfs_release_path(path);
2485                 goto again;
2486         }
2487 out:
2488         btrfs_release_path(path);
2489         btrfs_free_path(log_path);
2490         iput(dir);
2491         return ret;
2492 }
2493
2494 /*
2495  * the process_func used to replay items from the log tree.  This
2496  * gets called in two different stages.  The first stage just looks
2497  * for inodes and makes sure they are all copied into the subvolume.
2498  *
2499  * The second stage copies all the other item types from the log into
2500  * the subvolume.  The two stage approach is slower, but gets rid of
2501  * lots of complexity around inodes referencing other inodes that exist
2502  * only in the log (references come from either directory items or inode
2503  * back refs).
2504  */
2505 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2506                              struct walk_control *wc, u64 gen, int level)
2507 {
2508         int nritems;
2509         struct btrfs_path *path;
2510         struct btrfs_root *root = wc->replay_dest;
2511         struct btrfs_key key;
2512         int i;
2513         int ret;
2514
2515         ret = btrfs_read_buffer(eb, gen, level, NULL);
2516         if (ret)
2517                 return ret;
2518
2519         level = btrfs_header_level(eb);
2520
2521         if (level != 0)
2522                 return 0;
2523
2524         path = btrfs_alloc_path();
2525         if (!path)
2526                 return -ENOMEM;
2527
2528         nritems = btrfs_header_nritems(eb);
2529         for (i = 0; i < nritems; i++) {
2530                 btrfs_item_key_to_cpu(eb, &key, i);
2531
2532                 /* inode keys are done during the first stage */
2533                 if (key.type == BTRFS_INODE_ITEM_KEY &&
2534                     wc->stage == LOG_WALK_REPLAY_INODES) {
2535                         struct btrfs_inode_item *inode_item;
2536                         u32 mode;
2537
2538                         inode_item = btrfs_item_ptr(eb, i,
2539                                             struct btrfs_inode_item);
2540                         /*
2541                          * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2542                          * and never got linked before the fsync, skip it, as
2543                          * replaying it is pointless since it would be deleted
2544                          * later. We skip logging tmpfiles, but it's always
2545                          * possible we are replaying a log created with a kernel
2546                          * that used to log tmpfiles.
2547                          */
2548                         if (btrfs_inode_nlink(eb, inode_item) == 0) {
2549                                 wc->ignore_cur_inode = true;
2550                                 continue;
2551                         } else {
2552                                 wc->ignore_cur_inode = false;
2553                         }
2554                         ret = replay_xattr_deletes(wc->trans, root, log,
2555                                                    path, key.objectid);
2556                         if (ret)
2557                                 break;
2558                         mode = btrfs_inode_mode(eb, inode_item);
2559                         if (S_ISDIR(mode)) {
2560                                 ret = replay_dir_deletes(wc->trans,
2561                                          root, log, path, key.objectid, 0);
2562                                 if (ret)
2563                                         break;
2564                         }
2565                         ret = overwrite_item(wc->trans, root, path,
2566                                              eb, i, &key);
2567                         if (ret)
2568                                 break;
2569
2570                         /*
2571                          * Before replaying extents, truncate the inode to its
2572                          * size. We need to do it now and not after log replay
2573                          * because before an fsync we can have prealloc extents
2574                          * added beyond the inode's i_size. If we did it after,
2575                          * through orphan cleanup for example, we would drop
2576                          * those prealloc extents just after replaying them.
2577                          */
2578                         if (S_ISREG(mode)) {
2579                                 struct inode *inode;
2580                                 u64 from;
2581
2582                                 inode = read_one_inode(root, key.objectid);
2583                                 if (!inode) {
2584                                         ret = -EIO;
2585                                         break;
2586                                 }
2587                                 from = ALIGN(i_size_read(inode),
2588                                              root->fs_info->sectorsize);
2589                                 ret = btrfs_drop_extents(wc->trans, root, inode,
2590                                                          from, (u64)-1, 1);
2591                                 if (!ret) {
2592                                         /* Update the inode's nbytes. */
2593                                         ret = btrfs_update_inode(wc->trans,
2594                                                                  root, inode);
2595                                 }
2596                                 iput(inode);
2597                                 if (ret)
2598                                         break;
2599                         }
2600
2601                         ret = link_to_fixup_dir(wc->trans, root,
2602                                                 path, key.objectid);
2603                         if (ret)
2604                                 break;
2605                 }
2606
2607                 if (wc->ignore_cur_inode)
2608                         continue;
2609
2610                 if (key.type == BTRFS_DIR_INDEX_KEY &&
2611                     wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2612                         ret = replay_one_dir_item(wc->trans, root, path,
2613                                                   eb, i, &key);
2614                         if (ret)
2615                                 break;
2616                 }
2617
2618                 if (wc->stage < LOG_WALK_REPLAY_ALL)
2619                         continue;
2620
2621                 /* these keys are simply copied */
2622                 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2623                         ret = overwrite_item(wc->trans, root, path,
2624                                              eb, i, &key);
2625                         if (ret)
2626                                 break;
2627                 } else if (key.type == BTRFS_INODE_REF_KEY ||
2628                            key.type == BTRFS_INODE_EXTREF_KEY) {
2629                         ret = add_inode_ref(wc->trans, root, log, path,
2630                                             eb, i, &key);
2631                         if (ret && ret != -ENOENT)
2632                                 break;
2633                         ret = 0;
2634                 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2635                         ret = replay_one_extent(wc->trans, root, path,
2636                                                 eb, i, &key);
2637                         if (ret)
2638                                 break;
2639                 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
2640                         ret = replay_one_dir_item(wc->trans, root, path,
2641                                                   eb, i, &key);
2642                         if (ret)
2643                                 break;
2644                 }
2645         }
2646         btrfs_free_path(path);
2647         return ret;
2648 }
2649
2650 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2651                                    struct btrfs_root *root,
2652                                    struct btrfs_path *path, int *level,
2653                                    struct walk_control *wc)
2654 {
2655         struct btrfs_fs_info *fs_info = root->fs_info;
2656         u64 root_owner;
2657         u64 bytenr;
2658         u64 ptr_gen;
2659         struct extent_buffer *next;
2660         struct extent_buffer *cur;
2661         struct extent_buffer *parent;
2662         u32 blocksize;
2663         int ret = 0;
2664
2665         WARN_ON(*level < 0);
2666         WARN_ON(*level >= BTRFS_MAX_LEVEL);
2667
2668         while (*level > 0) {
2669                 struct btrfs_key first_key;
2670
2671                 WARN_ON(*level < 0);
2672                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2673                 cur = path->nodes[*level];
2674
2675                 WARN_ON(btrfs_header_level(cur) != *level);
2676
2677                 if (path->slots[*level] >=
2678                     btrfs_header_nritems(cur))
2679                         break;
2680
2681                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2682                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2683                 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
2684                 blocksize = fs_info->nodesize;
2685
2686                 parent = path->nodes[*level];
2687                 root_owner = btrfs_header_owner(parent);
2688
2689                 next = btrfs_find_create_tree_block(fs_info, bytenr);
2690                 if (IS_ERR(next))
2691                         return PTR_ERR(next);
2692
2693                 if (*level == 1) {
2694                         ret = wc->process_func(root, next, wc, ptr_gen,
2695                                                *level - 1);
2696                         if (ret) {
2697                                 free_extent_buffer(next);
2698                                 return ret;
2699                         }
2700
2701                         path->slots[*level]++;
2702                         if (wc->free) {
2703                                 ret = btrfs_read_buffer(next, ptr_gen,
2704                                                         *level - 1, &first_key);
2705                                 if (ret) {
2706                                         free_extent_buffer(next);
2707                                         return ret;
2708                                 }
2709
2710                                 if (trans) {
2711                                         btrfs_tree_lock(next);
2712                                         btrfs_set_lock_blocking(next);
2713                                         clean_tree_block(fs_info, next);
2714                                         btrfs_wait_tree_block_writeback(next);
2715                                         btrfs_tree_unlock(next);
2716                                 } else {
2717                                         if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2718                                                 clear_extent_buffer_dirty(next);
2719                                 }
2720
2721                                 WARN_ON(root_owner !=
2722                                         BTRFS_TREE_LOG_OBJECTID);
2723                                 ret = btrfs_free_and_pin_reserved_extent(
2724                                                         fs_info, bytenr,
2725                                                         blocksize);
2726                                 if (ret) {
2727                                         free_extent_buffer(next);
2728                                         return ret;
2729                                 }
2730                         }
2731                         free_extent_buffer(next);
2732                         continue;
2733                 }
2734                 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
2735                 if (ret) {
2736                         free_extent_buffer(next);
2737                         return ret;
2738                 }
2739
2740                 WARN_ON(*level <= 0);
2741                 if (path->nodes[*level-1])
2742                         free_extent_buffer(path->nodes[*level-1]);
2743                 path->nodes[*level-1] = next;
2744                 *level = btrfs_header_level(next);
2745                 path->slots[*level] = 0;
2746                 cond_resched();
2747         }
2748         WARN_ON(*level < 0);
2749         WARN_ON(*level >= BTRFS_MAX_LEVEL);
2750
2751         path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2752
2753         cond_resched();
2754         return 0;
2755 }
2756
2757 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2758                                  struct btrfs_root *root,
2759                                  struct btrfs_path *path, int *level,
2760                                  struct walk_control *wc)
2761 {
2762         struct btrfs_fs_info *fs_info = root->fs_info;
2763         u64 root_owner;
2764         int i;
2765         int slot;
2766         int ret;
2767
2768         for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2769                 slot = path->slots[i];
2770                 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2771                         path->slots[i]++;
2772                         *level = i;
2773                         WARN_ON(*level == 0);
2774                         return 0;
2775                 } else {
2776                         struct extent_buffer *parent;
2777                         if (path->nodes[*level] == root->node)
2778                                 parent = path->nodes[*level];
2779                         else
2780                                 parent = path->nodes[*level + 1];
2781
2782                         root_owner = btrfs_header_owner(parent);
2783                         ret = wc->process_func(root, path->nodes[*level], wc,
2784                                  btrfs_header_generation(path->nodes[*level]),
2785                                  *level);
2786                         if (ret)
2787                                 return ret;
2788
2789                         if (wc->free) {
2790                                 struct extent_buffer *next;
2791
2792                                 next = path->nodes[*level];
2793
2794                                 if (trans) {
2795                                         btrfs_tree_lock(next);
2796                                         btrfs_set_lock_blocking(next);
2797                                         clean_tree_block(fs_info, next);
2798                                         btrfs_wait_tree_block_writeback(next);
2799                                         btrfs_tree_unlock(next);
2800                                 } else {
2801                                         if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2802                                                 clear_extent_buffer_dirty(next);
2803                                 }
2804
2805                                 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
2806                                 ret = btrfs_free_and_pin_reserved_extent(
2807                                                 fs_info,
2808                                                 path->nodes[*level]->start,
2809                                                 path->nodes[*level]->len);
2810                                 if (ret)
2811                                         return ret;
2812                         }
2813                         free_extent_buffer(path->nodes[*level]);
2814                         path->nodes[*level] = NULL;
2815                         *level = i + 1;
2816                 }
2817         }
2818         return 1;
2819 }
2820
2821 /*
2822  * drop the reference count on the tree rooted at 'snap'.  This traverses
2823  * the tree freeing any blocks that have a ref count of zero after being
2824  * decremented.
2825  */
2826 static int walk_log_tree(struct btrfs_trans_handle *trans,
2827                          struct btrfs_root *log, struct walk_control *wc)
2828 {
2829         struct btrfs_fs_info *fs_info = log->fs_info;
2830         int ret = 0;
2831         int wret;
2832         int level;
2833         struct btrfs_path *path;
2834         int orig_level;
2835
2836         path = btrfs_alloc_path();
2837         if (!path)
2838                 return -ENOMEM;
2839
2840         level = btrfs_header_level(log->node);
2841         orig_level = level;
2842         path->nodes[level] = log->node;
2843         extent_buffer_get(log->node);
2844         path->slots[level] = 0;
2845
2846         while (1) {
2847                 wret = walk_down_log_tree(trans, log, path, &level, wc);
2848                 if (wret > 0)
2849                         break;
2850                 if (wret < 0) {
2851                         ret = wret;
2852                         goto out;
2853                 }
2854
2855                 wret = walk_up_log_tree(trans, log, path, &level, wc);
2856                 if (wret > 0)
2857                         break;
2858                 if (wret < 0) {
2859                         ret = wret;
2860                         goto out;
2861                 }
2862         }
2863
2864         /* was the root node processed? if not, catch it here */
2865         if (path->nodes[orig_level]) {
2866                 ret = wc->process_func(log, path->nodes[orig_level], wc,
2867                          btrfs_header_generation(path->nodes[orig_level]),
2868                          orig_level);
2869                 if (ret)
2870                         goto out;
2871                 if (wc->free) {
2872                         struct extent_buffer *next;
2873
2874                         next = path->nodes[orig_level];
2875
2876                         if (trans) {
2877                                 btrfs_tree_lock(next);
2878                                 btrfs_set_lock_blocking(next);
2879                                 clean_tree_block(fs_info, next);
2880                                 btrfs_wait_tree_block_writeback(next);
2881                                 btrfs_tree_unlock(next);
2882                         } else {
2883                                 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2884                                         clear_extent_buffer_dirty(next);
2885                         }
2886
2887                         WARN_ON(log->root_key.objectid !=
2888                                 BTRFS_TREE_LOG_OBJECTID);
2889                         ret = btrfs_free_and_pin_reserved_extent(fs_info,
2890                                                         next->start, next->len);
2891                         if (ret)
2892                                 goto out;
2893                 }
2894         }
2895
2896 out:
2897         btrfs_free_path(path);
2898         return ret;
2899 }
2900
2901 /*
2902  * helper function to update the item for a given subvolumes log root
2903  * in the tree of log roots
2904  */
2905 static int update_log_root(struct btrfs_trans_handle *trans,
2906                            struct btrfs_root *log,
2907                            struct btrfs_root_item *root_item)
2908 {
2909         struct btrfs_fs_info *fs_info = log->fs_info;
2910         int ret;
2911
2912         if (log->log_transid == 1) {
2913                 /* insert root item on the first sync */
2914                 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
2915                                 &log->root_key, root_item);
2916         } else {
2917                 ret = btrfs_update_root(trans, fs_info->log_root_tree,
2918                                 &log->root_key, root_item);
2919         }
2920         return ret;
2921 }
2922
2923 static void wait_log_commit(struct btrfs_root *root, int transid)
2924 {
2925         DEFINE_WAIT(wait);
2926         int index = transid % 2;
2927
2928         /*
2929          * we only allow two pending log transactions at a time,
2930          * so we know that if ours is more than 2 older than the
2931          * current transaction, we're done
2932          */
2933         for (;;) {
2934                 prepare_to_wait(&root->log_commit_wait[index],
2935                                 &wait, TASK_UNINTERRUPTIBLE);
2936
2937                 if (!(root->log_transid_committed < transid &&
2938                       atomic_read(&root->log_commit[index])))
2939                         break;
2940
2941                 mutex_unlock(&root->log_mutex);
2942                 schedule();
2943                 mutex_lock(&root->log_mutex);
2944         }
2945         finish_wait(&root->log_commit_wait[index], &wait);
2946 }
2947
2948 static void wait_for_writer(struct btrfs_root *root)
2949 {
2950         DEFINE_WAIT(wait);
2951
2952         for (;;) {
2953                 prepare_to_wait(&root->log_writer_wait, &wait,
2954                                 TASK_UNINTERRUPTIBLE);
2955                 if (!atomic_read(&root->log_writers))
2956                         break;
2957
2958                 mutex_unlock(&root->log_mutex);
2959                 schedule();
2960                 mutex_lock(&root->log_mutex);
2961         }
2962         finish_wait(&root->log_writer_wait, &wait);
2963 }
2964
2965 static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2966                                         struct btrfs_log_ctx *ctx)
2967 {
2968         if (!ctx)
2969                 return;
2970
2971         mutex_lock(&root->log_mutex);
2972         list_del_init(&ctx->list);
2973         mutex_unlock(&root->log_mutex);
2974 }
2975
2976 /* 
2977  * Invoked in log mutex context, or be sure there is no other task which
2978  * can access the list.
2979  */
2980 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2981                                              int index, int error)
2982 {
2983         struct btrfs_log_ctx *ctx;
2984         struct btrfs_log_ctx *safe;
2985
2986         list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2987                 list_del_init(&ctx->list);
2988                 ctx->log_ret = error;
2989         }
2990
2991         INIT_LIST_HEAD(&root->log_ctxs[index]);
2992 }
2993
2994 /*
2995  * btrfs_sync_log does sends a given tree log down to the disk and
2996  * updates the super blocks to record it.  When this call is done,
2997  * you know that any inodes previously logged are safely on disk only
2998  * if it returns 0.
2999  *
3000  * Any other return value means you need to call btrfs_commit_transaction.
3001  * Some of the edge cases for fsyncing directories that have had unlinks
3002  * or renames done in the past mean that sometimes the only safe
3003  * fsync is to commit the whole FS.  When btrfs_sync_log returns -EAGAIN,
3004  * that has happened.
3005  */
3006 int btrfs_sync_log(struct btrfs_trans_handle *trans,
3007                    struct btrfs_root *root, struct btrfs_log_ctx *ctx)
3008 {
3009         int index1;
3010         int index2;
3011         int mark;
3012         int ret;
3013         struct btrfs_fs_info *fs_info = root->fs_info;
3014         struct btrfs_root *log = root->log_root;
3015         struct btrfs_root *log_root_tree = fs_info->log_root_tree;
3016         struct btrfs_root_item new_root_item;
3017         int log_transid = 0;
3018         struct btrfs_log_ctx root_log_ctx;
3019         struct blk_plug plug;
3020
3021         mutex_lock(&root->log_mutex);
3022         log_transid = ctx->log_transid;
3023         if (root->log_transid_committed >= log_transid) {
3024                 mutex_unlock(&root->log_mutex);
3025                 return ctx->log_ret;
3026         }
3027
3028         index1 = log_transid % 2;
3029         if (atomic_read(&root->log_commit[index1])) {
3030                 wait_log_commit(root, log_transid);
3031                 mutex_unlock(&root->log_mutex);
3032                 return ctx->log_ret;
3033         }
3034         ASSERT(log_transid == root->log_transid);
3035         atomic_set(&root->log_commit[index1], 1);
3036
3037         /* wait for previous tree log sync to complete */
3038         if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
3039                 wait_log_commit(root, log_transid - 1);
3040
3041         while (1) {
3042                 int batch = atomic_read(&root->log_batch);
3043                 /* when we're on an ssd, just kick the log commit out */
3044                 if (!btrfs_test_opt(fs_info, SSD) &&
3045                     test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
3046                         mutex_unlock(&root->log_mutex);
3047                         schedule_timeout_uninterruptible(1);
3048                         mutex_lock(&root->log_mutex);
3049                 }
3050                 wait_for_writer(root);
3051                 if (batch == atomic_read(&root->log_batch))
3052                         break;
3053         }
3054
3055         /* bail out if we need to do a full commit */
3056         if (btrfs_need_log_full_commit(fs_info, trans)) {
3057                 ret = -EAGAIN;
3058                 mutex_unlock(&root->log_mutex);
3059                 goto out;
3060         }
3061
3062         if (log_transid % 2 == 0)
3063                 mark = EXTENT_DIRTY;
3064         else
3065                 mark = EXTENT_NEW;
3066
3067         /* we start IO on  all the marked extents here, but we don't actually
3068          * wait for them until later.
3069          */
3070         blk_start_plug(&plug);
3071         ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
3072         if (ret) {
3073                 blk_finish_plug(&plug);
3074                 btrfs_abort_transaction(trans, ret);
3075                 btrfs_set_log_full_commit(fs_info, trans);
3076                 mutex_unlock(&root->log_mutex);
3077                 goto out;
3078         }
3079
3080         /*
3081          * We _must_ update under the root->log_mutex in order to make sure we
3082          * have a consistent view of the log root we are trying to commit at
3083          * this moment.
3084          *
3085          * We _must_ copy this into a local copy, because we are not holding the
3086          * log_root_tree->log_mutex yet.  This is important because when we
3087          * commit the log_root_tree we must have a consistent view of the
3088          * log_root_tree when we update the super block to point at the
3089          * log_root_tree bytenr.  If we update the log_root_tree here we'll race
3090          * with the commit and possibly point at the new block which we may not
3091          * have written out.
3092          */
3093         btrfs_set_root_node(&log->root_item, log->node);
3094         memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
3095
3096         root->log_transid++;
3097         log->log_transid = root->log_transid;
3098         root->log_start_pid = 0;
3099         /*
3100          * IO has been started, blocks of the log tree have WRITTEN flag set
3101          * in their headers. new modifications of the log will be written to
3102          * new positions. so it's safe to allow log writers to go in.
3103          */
3104         mutex_unlock(&root->log_mutex);
3105
3106         btrfs_init_log_ctx(&root_log_ctx, NULL);
3107
3108         mutex_lock(&log_root_tree->log_mutex);
3109         atomic_inc(&log_root_tree->log_batch);
3110         atomic_inc(&log_root_tree->log_writers);
3111
3112         index2 = log_root_tree->log_transid % 2;
3113         list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3114         root_log_ctx.log_transid = log_root_tree->log_transid;
3115
3116         mutex_unlock(&log_root_tree->log_mutex);
3117
3118         mutex_lock(&log_root_tree->log_mutex);
3119
3120         /*
3121          * Now we are safe to update the log_root_tree because we're under the
3122          * log_mutex, and we're a current writer so we're holding the commit
3123          * open until we drop the log_mutex.
3124          */
3125         ret = update_log_root(trans, log, &new_root_item);
3126
3127         if (atomic_dec_and_test(&log_root_tree->log_writers)) {
3128                 /* atomic_dec_and_test implies a barrier */
3129                 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
3130         }
3131
3132         if (ret) {
3133                 if (!list_empty(&root_log_ctx.list))
3134                         list_del_init(&root_log_ctx.list);
3135
3136                 blk_finish_plug(&plug);
3137                 btrfs_set_log_full_commit(fs_info, trans);
3138
3139                 if (ret != -ENOSPC) {
3140                         btrfs_abort_transaction(trans, ret);
3141                         mutex_unlock(&log_root_tree->log_mutex);
3142                         goto out;
3143                 }
3144                 btrfs_wait_tree_log_extents(log, mark);
3145                 mutex_unlock(&log_root_tree->log_mutex);
3146                 ret = -EAGAIN;
3147                 goto out;
3148         }
3149
3150         if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
3151                 blk_finish_plug(&plug);
3152                 list_del_init(&root_log_ctx.list);
3153                 mutex_unlock(&log_root_tree->log_mutex);
3154                 ret = root_log_ctx.log_ret;
3155                 goto out;
3156         }
3157
3158         index2 = root_log_ctx.log_transid % 2;
3159         if (atomic_read(&log_root_tree->log_commit[index2])) {
3160                 blk_finish_plug(&plug);
3161                 ret = btrfs_wait_tree_log_extents(log, mark);
3162                 wait_log_commit(log_root_tree,
3163                                 root_log_ctx.log_transid);
3164                 mutex_unlock(&log_root_tree->log_mutex);
3165                 if (!ret)
3166                         ret = root_log_ctx.log_ret;
3167                 goto out;
3168         }
3169         ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
3170         atomic_set(&log_root_tree->log_commit[index2], 1);
3171
3172         if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
3173                 wait_log_commit(log_root_tree,
3174                                 root_log_ctx.log_transid - 1);
3175         }
3176
3177         wait_for_writer(log_root_tree);
3178
3179         /*
3180          * now that we've moved on to the tree of log tree roots,
3181          * check the full commit flag again
3182          */
3183         if (btrfs_need_log_full_commit(fs_info, trans)) {
3184                 blk_finish_plug(&plug);
3185                 btrfs_wait_tree_log_extents(log, mark);
3186                 mutex_unlock(&log_root_tree->log_mutex);
3187                 ret = -EAGAIN;
3188                 goto out_wake_log_root;
3189         }
3190
3191         ret = btrfs_write_marked_extents(fs_info,
3192                                          &log_root_tree->dirty_log_pages,
3193                                          EXTENT_DIRTY | EXTENT_NEW);
3194         blk_finish_plug(&plug);
3195         if (ret) {
3196                 btrfs_set_log_full_commit(fs_info, trans);
3197                 btrfs_abort_transaction(trans, ret);
3198                 mutex_unlock(&log_root_tree->log_mutex);
3199                 goto out_wake_log_root;
3200         }
3201         ret = btrfs_wait_tree_log_extents(log, mark);
3202         if (!ret)
3203                 ret = btrfs_wait_tree_log_extents(log_root_tree,
3204                                                   EXTENT_NEW | EXTENT_DIRTY);
3205         if (ret) {
3206                 btrfs_set_log_full_commit(fs_info, trans);
3207                 mutex_unlock(&log_root_tree->log_mutex);
3208                 goto out_wake_log_root;
3209         }
3210
3211         btrfs_set_super_log_root(fs_info->super_for_commit,
3212                                  log_root_tree->node->start);
3213         btrfs_set_super_log_root_level(fs_info->super_for_commit,
3214                                        btrfs_header_level(log_root_tree->node));
3215
3216         log_root_tree->log_transid++;
3217         mutex_unlock(&log_root_tree->log_mutex);
3218
3219         /*
3220          * nobody else is going to jump in and write the the ctree
3221          * super here because the log_commit atomic below is protecting
3222          * us.  We must be called with a transaction handle pinning
3223          * the running transaction open, so a full commit can't hop
3224          * in and cause problems either.
3225          */
3226         ret = write_all_supers(fs_info, 1);
3227         if (ret) {
3228                 btrfs_set_log_full_commit(fs_info, trans);
3229                 btrfs_abort_transaction(trans, ret);
3230                 goto out_wake_log_root;
3231         }
3232
3233         mutex_lock(&root->log_mutex);
3234         if (root->last_log_commit < log_transid)
3235                 root->last_log_commit = log_transid;
3236         mutex_unlock(&root->log_mutex);
3237
3238 out_wake_log_root:
3239         mutex_lock(&log_root_tree->log_mutex);
3240         btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3241
3242         log_root_tree->log_transid_committed++;
3243         atomic_set(&log_root_tree->log_commit[index2], 0);
3244         mutex_unlock(&log_root_tree->log_mutex);
3245
3246         /*
3247          * The barrier before waitqueue_active (in cond_wake_up) is needed so
3248          * all the updates above are seen by the woken threads. It might not be
3249          * necessary, but proving that seems to be hard.
3250          */
3251         cond_wake_up(&log_root_tree->log_commit_wait[index2]);
3252 out:
3253         mutex_lock(&root->log_mutex);
3254         btrfs_remove_all_log_ctxs(root, index1, ret);
3255         root->log_transid_committed++;
3256         atomic_set(&root->log_commit[index1], 0);
3257         mutex_unlock(&root->log_mutex);
3258
3259         /*
3260          * The barrier before waitqueue_active (in cond_wake_up) is needed so
3261          * all the updates above are seen by the woken threads. It might not be
3262          * necessary, but proving that seems to be hard.
3263          */
3264         cond_wake_up(&root->log_commit_wait[index1]);
3265         return ret;
3266 }
3267
3268 static void free_log_tree(struct btrfs_trans_handle *trans,
3269                           struct btrfs_root *log)
3270 {
3271         int ret;
3272         u64 start;
3273         u64 end;
3274         struct walk_control wc = {
3275                 .free = 1,
3276                 .process_func = process_one_buffer
3277         };
3278
3279         ret = walk_log_tree(trans, log, &wc);
3280         if (ret) {
3281                 if (trans)
3282                         btrfs_abort_transaction(trans, ret);
3283                 else
3284                         btrfs_handle_fs_error(log->fs_info, ret, NULL);
3285         }
3286
3287         while (1) {
3288                 ret = find_first_extent_bit(&log->dirty_log_pages,
3289                                 0, &start, &end,
3290                                 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
3291                                 NULL);
3292                 if (ret)
3293                         break;
3294
3295                 clear_extent_bits(&log->dirty_log_pages, start, end,
3296                                   EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3297         }
3298
3299         free_extent_buffer(log->node);
3300         kfree(log);
3301 }
3302
3303 /*
3304  * free all the extents used by the tree log.  This should be called
3305  * at commit time of the full transaction
3306  */
3307 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3308 {
3309         if (root->log_root) {
3310                 free_log_tree(trans, root->log_root);
3311                 root->log_root = NULL;
3312         }
3313         return 0;
3314 }
3315
3316 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3317                              struct btrfs_fs_info *fs_info)
3318 {
3319         if (fs_info->log_root_tree) {
3320                 free_log_tree(trans, fs_info->log_root_tree);
3321                 fs_info->log_root_tree = NULL;
3322         }
3323         return 0;
3324 }
3325
3326 /*
3327  * Check if an inode was logged in the current transaction. We can't always rely
3328  * on an inode's logged_trans value, because it's an in-memory only field and
3329  * therefore not persisted. This means that its value is lost if the inode gets
3330  * evicted and loaded again from disk (in which case it has a value of 0, and
3331  * certainly it is smaller then any possible transaction ID), when that happens
3332  * the full_sync flag is set in the inode's runtime flags, so on that case we
3333  * assume eviction happened and ignore the logged_trans value, assuming the
3334  * worst case, that the inode was logged before in the current transaction.
3335  */
3336 static bool inode_logged(struct btrfs_trans_handle *trans,
3337                          struct btrfs_inode *inode)
3338 {
3339         if (inode->logged_trans == trans->transid)
3340                 return true;
3341
3342         if (inode->last_trans == trans->transid &&
3343             test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
3344             !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3345                 return true;
3346
3347         return false;
3348 }
3349
3350 /*
3351  * If both a file and directory are logged, and unlinks or renames are
3352  * mixed in, we have a few interesting corners:
3353  *
3354  * create file X in dir Y
3355  * link file X to X.link in dir Y
3356  * fsync file X
3357  * unlink file X but leave X.link
3358  * fsync dir Y
3359  *
3360  * After a crash we would expect only X.link to exist.  But file X
3361  * didn't get fsync'd again so the log has back refs for X and X.link.
3362  *
3363  * We solve this by removing directory entries and inode backrefs from the
3364  * log when a file that was logged in the current transaction is
3365  * unlinked.  Any later fsync will include the updated log entries, and
3366  * we'll be able to reconstruct the proper directory items from backrefs.
3367  *
3368  * This optimizations allows us to avoid relogging the entire inode
3369  * or the entire directory.
3370  */
3371 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3372                                  struct btrfs_root *root,
3373                                  const char *name, int name_len,
3374                                  struct btrfs_inode *dir, u64 index)
3375 {
3376         struct btrfs_root *log;
3377         struct btrfs_dir_item *di;
3378         struct btrfs_path *path;
3379         int ret;
3380         int err = 0;
3381         int bytes_del = 0;
3382         u64 dir_ino = btrfs_ino(dir);
3383
3384         if (!inode_logged(trans, dir))
3385                 return 0;
3386
3387         ret = join_running_log_trans(root);
3388         if (ret)
3389                 return 0;
3390
3391         mutex_lock(&dir->log_mutex);
3392
3393         log = root->log_root;
3394         path = btrfs_alloc_path();
3395         if (!path) {
3396                 err = -ENOMEM;
3397                 goto out_unlock;
3398         }
3399
3400         di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
3401                                    name, name_len, -1);
3402         if (IS_ERR(di)) {
3403                 err = PTR_ERR(di);
3404                 goto fail;
3405         }
3406         if (di) {
3407                 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3408                 bytes_del += name_len;
3409                 if (ret) {
3410                         err = ret;
3411                         goto fail;
3412                 }
3413         }
3414         btrfs_release_path(path);
3415         di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3416                                          index, name, name_len, -1);
3417         if (IS_ERR(di)) {
3418                 err = PTR_ERR(di);
3419                 goto fail;
3420         }
3421         if (di) {
3422                 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3423                 bytes_del += name_len;
3424                 if (ret) {
3425                         err = ret;
3426                         goto fail;
3427                 }
3428         }
3429
3430         /* update the directory size in the log to reflect the names
3431          * we have removed
3432          */
3433         if (bytes_del) {
3434                 struct btrfs_key key;
3435
3436                 key.objectid = dir_ino;
3437                 key.offset = 0;
3438                 key.type = BTRFS_INODE_ITEM_KEY;
3439                 btrfs_release_path(path);
3440
3441                 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
3442                 if (ret < 0) {
3443                         err = ret;
3444                         goto fail;
3445                 }
3446                 if (ret == 0) {
3447                         struct btrfs_inode_item *item;
3448                         u64 i_size;
3449
3450                         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3451                                               struct btrfs_inode_item);
3452                         i_size = btrfs_inode_size(path->nodes[0], item);
3453                         if (i_size > bytes_del)
3454                                 i_size -= bytes_del;
3455                         else
3456                                 i_size = 0;
3457                         btrfs_set_inode_size(path->nodes[0], item, i_size);
3458                         btrfs_mark_buffer_dirty(path->nodes[0]);
3459                 } else
3460                         ret = 0;
3461                 btrfs_release_path(path);
3462         }
3463 fail:
3464         btrfs_free_path(path);
3465 out_unlock:
3466         mutex_unlock(&dir->log_mutex);
3467         if (err == -ENOSPC) {
3468                 btrfs_set_log_full_commit(root->fs_info, trans);
3469                 err = 0;
3470         } else if (err < 0 && err != -ENOENT) {
3471                 /* ENOENT can be returned if the entry hasn't been fsynced yet */
3472                 btrfs_abort_transaction(trans, err);
3473         }
3474
3475         btrfs_end_log_trans(root);
3476
3477         return err;
3478 }
3479
3480 /* see comments for btrfs_del_dir_entries_in_log */
3481 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3482                                struct btrfs_root *root,
3483                                const char *name, int name_len,
3484                                struct btrfs_inode *inode, u64 dirid)
3485 {
3486         struct btrfs_fs_info *fs_info = root->fs_info;
3487         struct btrfs_root *log;
3488         u64 index;
3489         int ret;
3490
3491         if (!inode_logged(trans, inode))
3492                 return 0;
3493
3494         ret = join_running_log_trans(root);
3495         if (ret)
3496                 return 0;
3497         log = root->log_root;
3498         mutex_lock(&inode->log_mutex);
3499
3500         ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
3501                                   dirid, &index);
3502         mutex_unlock(&inode->log_mutex);
3503         if (ret == -ENOSPC) {
3504                 btrfs_set_log_full_commit(fs_info, trans);
3505                 ret = 0;
3506         } else if (ret < 0 && ret != -ENOENT)
3507                 btrfs_abort_transaction(trans, ret);
3508         btrfs_end_log_trans(root);
3509
3510         return ret;
3511 }
3512
3513 /*
3514  * creates a range item in the log for 'dirid'.  first_offset and
3515  * last_offset tell us which parts of the key space the log should
3516  * be considered authoritative for.
3517  */
3518 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3519                                        struct btrfs_root *log,
3520                                        struct btrfs_path *path,
3521                                        int key_type, u64 dirid,
3522                                        u64 first_offset, u64 last_offset)
3523 {
3524         int ret;
3525         struct btrfs_key key;
3526         struct btrfs_dir_log_item *item;
3527
3528         key.objectid = dirid;
3529         key.offset = first_offset;
3530         if (key_type == BTRFS_DIR_ITEM_KEY)
3531                 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3532         else
3533                 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3534         ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3535         if (ret)
3536                 return ret;
3537
3538         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3539                               struct btrfs_dir_log_item);
3540         btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3541         btrfs_mark_buffer_dirty(path->nodes[0]);
3542         btrfs_release_path(path);
3543         return 0;
3544 }
3545
3546 /*
3547  * log all the items included in the current transaction for a given
3548  * directory.  This also creates the range items in the log tree required
3549  * to replay anything deleted before the fsync
3550  */
3551 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3552                           struct btrfs_root *root, struct btrfs_inode *inode,
3553                           struct btrfs_path *path,
3554                           struct btrfs_path *dst_path, int key_type,
3555                           struct btrfs_log_ctx *ctx,
3556                           u64 min_offset, u64 *last_offset_ret)
3557 {
3558         struct btrfs_key min_key;
3559         struct btrfs_root *log = root->log_root;
3560         struct extent_buffer *src;
3561         int err = 0;
3562         int ret;
3563         int i;
3564         int nritems;
3565         u64 first_offset = min_offset;
3566         u64 last_offset = (u64)-1;
3567         u64 ino = btrfs_ino(inode);
3568
3569         log = root->log_root;
3570
3571         min_key.objectid = ino;
3572         min_key.type = key_type;
3573         min_key.offset = min_offset;
3574
3575         ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3576
3577         /*
3578          * we didn't find anything from this transaction, see if there
3579          * is anything at all
3580          */
3581         if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3582                 min_key.objectid = ino;
3583                 min_key.type = key_type;
3584                 min_key.offset = (u64)-1;
3585                 btrfs_release_path(path);
3586                 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3587                 if (ret < 0) {
3588                         btrfs_release_path(path);
3589                         return ret;
3590                 }
3591                 ret = btrfs_previous_item(root, path, ino, key_type);
3592
3593                 /* if ret == 0 there are items for this type,
3594                  * create a range to tell us the last key of this type.
3595                  * otherwise, there are no items in this directory after
3596                  * *min_offset, and we create a range to indicate that.
3597                  */
3598                 if (ret == 0) {
3599                         struct btrfs_key tmp;
3600                         btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3601                                               path->slots[0]);
3602                         if (key_type == tmp.type)
3603                                 first_offset = max(min_offset, tmp.offset) + 1;
3604                 }
3605                 goto done;
3606         }
3607
3608         /* go backward to find any previous key */
3609         ret = btrfs_previous_item(root, path, ino, key_type);
3610         if (ret == 0) {
3611                 struct btrfs_key tmp;
3612                 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3613                 if (key_type == tmp.type) {
3614                         first_offset = tmp.offset;
3615                         ret = overwrite_item(trans, log, dst_path,
3616                                              path->nodes[0], path->slots[0],
3617                                              &tmp);
3618                         if (ret) {
3619                                 err = ret;
3620                                 goto done;
3621                         }
3622                 }
3623         }
3624         btrfs_release_path(path);
3625
3626         /*
3627          * Find the first key from this transaction again.  See the note for
3628          * log_new_dir_dentries, if we're logging a directory recursively we
3629          * won't be holding its i_mutex, which means we can modify the directory
3630          * while we're logging it.  If we remove an entry between our first
3631          * search and this search we'll not find the key again and can just
3632          * bail.
3633          */
3634 search:
3635         ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3636         if (ret != 0)
3637                 goto done;
3638
3639         /*
3640          * we have a block from this transaction, log every item in it
3641          * from our directory
3642          */
3643         while (1) {
3644                 struct btrfs_key tmp;
3645                 src = path->nodes[0];
3646                 nritems = btrfs_header_nritems(src);
3647                 for (i = path->slots[0]; i < nritems; i++) {
3648                         struct btrfs_dir_item *di;
3649
3650                         btrfs_item_key_to_cpu(src, &min_key, i);
3651
3652                         if (min_key.objectid != ino || min_key.type != key_type)
3653                                 goto done;
3654
3655                         if (need_resched()) {
3656                                 btrfs_release_path(path);
3657                                 cond_resched();
3658                                 goto search;
3659                         }
3660
3661                         ret = overwrite_item(trans, log, dst_path, src, i,
3662                                              &min_key);
3663                         if (ret) {
3664                                 err = ret;
3665                                 goto done;
3666                         }
3667
3668                         /*
3669                          * We must make sure that when we log a directory entry,
3670                          * the corresponding inode, after log replay, has a
3671                          * matching link count. For example:
3672                          *
3673                          * touch foo
3674                          * mkdir mydir
3675                          * sync
3676                          * ln foo mydir/bar
3677                          * xfs_io -c "fsync" mydir
3678                          * <crash>
3679                          * <mount fs and log replay>
3680                          *
3681                          * Would result in a fsync log that when replayed, our
3682                          * file inode would have a link count of 1, but we get
3683                          * two directory entries pointing to the same inode.
3684                          * After removing one of the names, it would not be
3685                          * possible to remove the other name, which resulted
3686                          * always in stale file handle errors, and would not
3687                          * be possible to rmdir the parent directory, since
3688                          * its i_size could never decrement to the value
3689                          * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3690                          */
3691                         di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3692                         btrfs_dir_item_key_to_cpu(src, di, &tmp);
3693                         if (ctx &&
3694                             (btrfs_dir_transid(src, di) == trans->transid ||
3695                              btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3696                             tmp.type != BTRFS_ROOT_ITEM_KEY)
3697                                 ctx->log_new_dentries = true;
3698                 }
3699                 path->slots[0] = nritems;
3700
3701                 /*
3702                  * look ahead to the next item and see if it is also
3703                  * from this directory and from this transaction
3704                  */
3705                 ret = btrfs_next_leaf(root, path);
3706                 if (ret) {
3707                         if (ret == 1)
3708                                 last_offset = (u64)-1;
3709                         else
3710                                 err = ret;
3711                         goto done;
3712                 }
3713                 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3714                 if (tmp.objectid != ino || tmp.type != key_type) {
3715                         last_offset = (u64)-1;
3716                         goto done;
3717                 }
3718                 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3719                         ret = overwrite_item(trans, log, dst_path,
3720                                              path->nodes[0], path->slots[0],
3721                                              &tmp);
3722                         if (ret)
3723                                 err = ret;
3724                         else
3725                                 last_offset = tmp.offset;
3726                         goto done;
3727                 }
3728         }
3729 done:
3730         btrfs_release_path(path);
3731         btrfs_release_path(dst_path);
3732
3733         if (err == 0) {
3734                 *last_offset_ret = last_offset;
3735                 /*
3736                  * insert the log range keys to indicate where the log
3737                  * is valid
3738                  */
3739                 ret = insert_dir_log_key(trans, log, path, key_type,
3740                                          ino, first_offset, last_offset);
3741                 if (ret)
3742                         err = ret;
3743         }
3744         return err;
3745 }
3746
3747 /*
3748  * logging directories is very similar to logging inodes, We find all the items
3749  * from the current transaction and write them to the log.
3750  *
3751  * The recovery code scans the directory in the subvolume, and if it finds a
3752  * key in the range logged that is not present in the log tree, then it means
3753  * that dir entry was unlinked during the transaction.
3754  *
3755  * In order for that scan to work, we must include one key smaller than
3756  * the smallest logged by this transaction and one key larger than the largest
3757  * key logged by this transaction.
3758  */
3759 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3760                           struct btrfs_root *root, struct btrfs_inode *inode,
3761                           struct btrfs_path *path,
3762                           struct btrfs_path *dst_path,
3763                           struct btrfs_log_ctx *ctx)
3764 {
3765         u64 min_key;
3766         u64 max_key;
3767         int ret;
3768         int key_type = BTRFS_DIR_ITEM_KEY;
3769
3770 again:
3771         min_key = 0;
3772         max_key = 0;
3773         while (1) {
3774                 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3775                                 ctx, min_key, &max_key);
3776                 if (ret)
3777                         return ret;
3778                 if (max_key == (u64)-1)
3779                         break;
3780                 min_key = max_key + 1;
3781         }
3782
3783         if (key_type == BTRFS_DIR_ITEM_KEY) {
3784                 key_type = BTRFS_DIR_INDEX_KEY;
3785                 goto again;
3786         }
3787         return 0;
3788 }
3789
3790 /*
3791  * a helper function to drop items from the log before we relog an
3792  * inode.  max_key_type indicates the highest item type to remove.
3793  * This cannot be run for file data extents because it does not
3794  * free the extents they point to.
3795  */
3796 static int drop_objectid_items(struct btrfs_trans_handle *trans,
3797                                   struct btrfs_root *log,
3798                                   struct btrfs_path *path,
3799                                   u64 objectid, int max_key_type)
3800 {
3801         int ret;
3802         struct btrfs_key key;
3803         struct btrfs_key found_key;
3804         int start_slot;
3805
3806         key.objectid = objectid;
3807         key.type = max_key_type;
3808         key.offset = (u64)-1;
3809
3810         while (1) {
3811                 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3812                 BUG_ON(ret == 0); /* Logic error */
3813                 if (ret < 0)
3814                         break;
3815
3816                 if (path->slots[0] == 0)
3817                         break;
3818
3819                 path->slots[0]--;
3820                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3821                                       path->slots[0]);
3822
3823                 if (found_key.objectid != objectid)
3824                         break;
3825
3826                 found_key.offset = 0;
3827                 found_key.type = 0;
3828                 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3829                                        &start_slot);
3830
3831                 ret = btrfs_del_items(trans, log, path, start_slot,
3832                                       path->slots[0] - start_slot + 1);
3833                 /*
3834                  * If start slot isn't 0 then we don't need to re-search, we've
3835                  * found the last guy with the objectid in this tree.
3836                  */
3837                 if (ret || start_slot != 0)
3838                         break;
3839                 btrfs_release_path(path);
3840         }
3841         btrfs_release_path(path);
3842         if (ret > 0)
3843                 ret = 0;
3844         return ret;
3845 }
3846
3847 static void fill_inode_item(struct btrfs_trans_handle *trans,
3848                             struct extent_buffer *leaf,
3849                             struct btrfs_inode_item *item,
3850                             struct inode *inode, int log_inode_only,
3851                             u64 logged_isize)
3852 {
3853         struct btrfs_map_token token;
3854
3855         btrfs_init_map_token(&token);
3856
3857         if (log_inode_only) {
3858                 /* set the generation to zero so the recover code
3859                  * can tell the difference between an logging
3860                  * just to say 'this inode exists' and a logging
3861                  * to say 'update this inode with these values'
3862                  */
3863                 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3864                 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
3865         } else {
3866                 btrfs_set_token_inode_generation(leaf, item,
3867                                                  BTRFS_I(inode)->generation,
3868                                                  &token);
3869                 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
3870         }
3871
3872         btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3873         btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3874         btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3875         btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3876
3877         btrfs_set_token_timespec_sec(leaf, &item->atime,
3878                                      inode->i_atime.tv_sec, &token);
3879         btrfs_set_token_timespec_nsec(leaf, &item->atime,
3880                                       inode->i_atime.tv_nsec, &token);
3881
3882         btrfs_set_token_timespec_sec(leaf, &item->mtime,
3883                                      inode->i_mtime.tv_sec, &token);
3884         btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3885                                       inode->i_mtime.tv_nsec, &token);
3886
3887         btrfs_set_token_timespec_sec(leaf, &item->ctime,
3888                                      inode->i_ctime.tv_sec, &token);
3889         btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3890                                       inode->i_ctime.tv_nsec, &token);
3891
3892         btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3893                                      &token);
3894
3895         btrfs_set_token_inode_sequence(leaf, item,
3896                                        inode_peek_iversion(inode), &token);
3897         btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3898         btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3899         btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3900         btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3901 }
3902
3903 static int log_inode_item(struct btrfs_trans_handle *trans,
3904                           struct btrfs_root *log, struct btrfs_path *path,
3905                           struct btrfs_inode *inode)
3906 {
3907         struct btrfs_inode_item *inode_item;
3908         int ret;
3909
3910         ret = btrfs_insert_empty_item(trans, log, path,
3911                                       &inode->location, sizeof(*inode_item));
3912         if (ret && ret != -EEXIST)
3913                 return ret;
3914         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3915                                     struct btrfs_inode_item);
3916         fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3917                         0, 0);
3918         btrfs_release_path(path);
3919         return 0;
3920 }
3921
3922 static int log_csums(struct btrfs_trans_handle *trans,
3923                      struct btrfs_root *log_root,
3924                      struct btrfs_ordered_sum *sums)
3925 {
3926         int ret;
3927
3928         /*
3929          * Due to extent cloning, we might have logged a csum item that covers a
3930          * subrange of a cloned extent, and later we can end up logging a csum
3931          * item for a larger subrange of the same extent or the entire range.
3932          * This would leave csum items in the log tree that cover the same range
3933          * and break the searches for checksums in the log tree, resulting in
3934          * some checksums missing in the fs/subvolume tree. So just delete (or
3935          * trim and adjust) any existing csum items in the log for this range.
3936          */
3937         ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
3938         if (ret)
3939                 return ret;
3940
3941         return btrfs_csum_file_blocks(trans, log_root, sums);
3942 }
3943
3944 static noinline int copy_items(struct btrfs_trans_handle *trans,
3945                                struct btrfs_inode *inode,
3946                                struct btrfs_path *dst_path,
3947                                struct btrfs_path *src_path,
3948                                int start_slot, int nr, int inode_only,
3949                                u64 logged_isize)
3950 {
3951         struct btrfs_fs_info *fs_info = trans->fs_info;
3952         unsigned long src_offset;
3953         unsigned long dst_offset;
3954         struct btrfs_root *log = inode->root->log_root;
3955         struct btrfs_file_extent_item *extent;
3956         struct btrfs_inode_item *inode_item;
3957         struct extent_buffer *src = src_path->nodes[0];
3958         int ret;
3959         struct btrfs_key *ins_keys;
3960         u32 *ins_sizes;
3961         char *ins_data;
3962         int i;
3963         struct list_head ordered_sums;
3964         int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
3965
3966         INIT_LIST_HEAD(&ordered_sums);
3967
3968         ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3969                            nr * sizeof(u32), GFP_NOFS);
3970         if (!ins_data)
3971                 return -ENOMEM;
3972
3973         ins_sizes = (u32 *)ins_data;
3974         ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3975
3976         for (i = 0; i < nr; i++) {
3977                 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3978                 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3979         }
3980         ret = btrfs_insert_empty_items(trans, log, dst_path,
3981                                        ins_keys, ins_sizes, nr);
3982         if (ret) {
3983                 kfree(ins_data);
3984                 return ret;
3985         }
3986
3987         for (i = 0; i < nr; i++, dst_path->slots[0]++) {
3988                 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3989                                                    dst_path->slots[0]);
3990
3991                 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3992
3993                 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
3994                         inode_item = btrfs_item_ptr(dst_path->nodes[0],
3995                                                     dst_path->slots[0],
3996                                                     struct btrfs_inode_item);
3997                         fill_inode_item(trans, dst_path->nodes[0], inode_item,
3998                                         &inode->vfs_inode,
3999                                         inode_only == LOG_INODE_EXISTS,
4000                                         logged_isize);
4001                 } else {
4002                         copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
4003                                            src_offset, ins_sizes[i]);
4004                 }
4005
4006                 /* take a reference on file data extents so that truncates
4007                  * or deletes of this inode don't have to relog the inode
4008                  * again
4009                  */
4010                 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
4011                     !skip_csum) {
4012                         int found_type;
4013                         extent = btrfs_item_ptr(src, start_slot + i,
4014                                                 struct btrfs_file_extent_item);
4015
4016                         if (btrfs_file_extent_generation(src, extent) < trans->transid)
4017                                 continue;
4018
4019                         found_type = btrfs_file_extent_type(src, extent);
4020                         if (found_type == BTRFS_FILE_EXTENT_REG) {
4021                                 u64 ds, dl, cs, cl;
4022                                 ds = btrfs_file_extent_disk_bytenr(src,
4023                                                                 extent);
4024                                 /* ds == 0 is a hole */
4025                                 if (ds == 0)
4026                                         continue;
4027
4028                                 dl = btrfs_file_extent_disk_num_bytes(src,
4029                                                                 extent);
4030                                 cs = btrfs_file_extent_offset(src, extent);
4031                                 cl = btrfs_file_extent_num_bytes(src,
4032                                                                 extent);
4033                                 if (btrfs_file_extent_compression(src,
4034                                                                   extent)) {
4035                                         cs = 0;
4036                                         cl = dl;
4037                                 }
4038
4039                                 ret = btrfs_lookup_csums_range(
4040                                                 fs_info->csum_root,
4041                                                 ds + cs, ds + cs + cl - 1,
4042                                                 &ordered_sums, 0);
4043                                 if (ret)
4044                                         break;
4045                         }
4046                 }
4047         }
4048
4049         btrfs_mark_buffer_dirty(dst_path->nodes[0]);
4050         btrfs_release_path(dst_path);
4051         kfree(ins_data);
4052
4053         /*
4054          * we have to do this after the loop above to avoid changing the
4055          * log tree while trying to change the log tree.
4056          */
4057         while (!list_empty(&ordered_sums)) {
4058                 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4059                                                    struct btrfs_ordered_sum,
4060                                                    list);
4061                 if (!ret)
4062                         ret = log_csums(trans, log, sums);
4063                 list_del(&sums->list);
4064                 kfree(sums);
4065         }
4066
4067         return ret;
4068 }
4069
4070 static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4071 {
4072         struct extent_map *em1, *em2;
4073
4074         em1 = list_entry(a, struct extent_map, list);
4075         em2 = list_entry(b, struct extent_map, list);
4076
4077         if (em1->start < em2->start)
4078                 return -1;
4079         else if (em1->start > em2->start)
4080                 return 1;
4081         return 0;
4082 }
4083
4084 static int log_extent_csums(struct btrfs_trans_handle *trans,
4085                             struct btrfs_inode *inode,
4086                             struct btrfs_root *log_root,
4087                             const struct extent_map *em)
4088 {
4089         u64 csum_offset;
4090         u64 csum_len;
4091         LIST_HEAD(ordered_sums);
4092         int ret = 0;
4093
4094         if (inode->flags & BTRFS_INODE_NODATASUM ||
4095             test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4096             em->block_start == EXTENT_MAP_HOLE)
4097                 return 0;
4098
4099         /* If we're compressed we have to save the entire range of csums. */
4100         if (em->compress_type) {
4101                 csum_offset = 0;
4102                 csum_len = max(em->block_len, em->orig_block_len);
4103         } else {
4104                 csum_offset = em->mod_start - em->start;
4105                 csum_len = em->mod_len;
4106         }
4107
4108         /* block start is already adjusted for the file extent offset. */
4109         ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
4110                                        em->block_start + csum_offset,
4111                                        em->block_start + csum_offset +
4112                                        csum_len - 1, &ordered_sums, 0);
4113         if (ret)
4114                 return ret;
4115
4116         while (!list_empty(&ordered_sums)) {
4117                 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4118                                                    struct btrfs_ordered_sum,
4119                                                    list);
4120                 if (!ret)
4121                         ret = log_csums(trans, log_root, sums);
4122                 list_del(&sums->list);
4123                 kfree(sums);
4124         }
4125
4126         return ret;
4127 }
4128
4129 static int log_one_extent(struct btrfs_trans_handle *trans,
4130                           struct btrfs_inode *inode, struct btrfs_root *root,
4131                           const struct extent_map *em,
4132                           struct btrfs_path *path,
4133                           struct btrfs_log_ctx *ctx)
4134 {
4135         struct btrfs_root *log = root->log_root;
4136         struct btrfs_file_extent_item *fi;
4137         struct extent_buffer *leaf;
4138         struct btrfs_map_token token;
4139         struct btrfs_key key;
4140         u64 extent_offset = em->start - em->orig_start;
4141         u64 block_len;
4142         int ret;
4143         int extent_inserted = 0;
4144
4145         ret = log_extent_csums(trans, inode, log, em);
4146         if (ret)
4147                 return ret;
4148
4149         btrfs_init_map_token(&token);
4150
4151         ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
4152                                    em->start + em->len, NULL, 0, 1,
4153                                    sizeof(*fi), &extent_inserted);
4154         if (ret)
4155                 return ret;
4156
4157         if (!extent_inserted) {
4158                 key.objectid = btrfs_ino(inode);
4159                 key.type = BTRFS_EXTENT_DATA_KEY;
4160                 key.offset = em->start;
4161
4162                 ret = btrfs_insert_empty_item(trans, log, path, &key,
4163                                               sizeof(*fi));
4164                 if (ret)
4165                         return ret;
4166         }
4167         leaf = path->nodes[0];
4168         fi = btrfs_item_ptr(leaf, path->slots[0],
4169                             struct btrfs_file_extent_item);
4170
4171         btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
4172                                                &token);
4173         if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4174                 btrfs_set_token_file_extent_type(leaf, fi,
4175                                                  BTRFS_FILE_EXTENT_PREALLOC,
4176                                                  &token);
4177         else
4178                 btrfs_set_token_file_extent_type(leaf, fi,
4179                                                  BTRFS_FILE_EXTENT_REG,
4180                                                  &token);
4181
4182         block_len = max(em->block_len, em->orig_block_len);
4183         if (em->compress_type != BTRFS_COMPRESS_NONE) {
4184                 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4185                                                         em->block_start,
4186                                                         &token);
4187                 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4188                                                            &token);
4189         } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4190                 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4191                                                         em->block_start -
4192                                                         extent_offset, &token);
4193                 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4194                                                            &token);
4195         } else {
4196                 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4197                 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4198                                                            &token);
4199         }
4200
4201         btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4202         btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4203         btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4204         btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4205                                                 &token);
4206         btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4207         btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4208         btrfs_mark_buffer_dirty(leaf);
4209
4210         btrfs_release_path(path);
4211
4212         return ret;
4213 }
4214
4215 /*
4216  * Log all prealloc extents beyond the inode's i_size to make sure we do not
4217  * lose them after doing a fast fsync and replaying the log. We scan the
4218  * subvolume's root instead of iterating the inode's extent map tree because
4219  * otherwise we can log incorrect extent items based on extent map conversion.
4220  * That can happen due to the fact that extent maps are merged when they
4221  * are not in the extent map tree's list of modified extents.
4222  */
4223 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4224                                       struct btrfs_inode *inode,
4225                                       struct btrfs_path *path)
4226 {
4227         struct btrfs_root *root = inode->root;
4228         struct btrfs_key key;
4229         const u64 i_size = i_size_read(&inode->vfs_inode);
4230         const u64 ino = btrfs_ino(inode);
4231         struct btrfs_path *dst_path = NULL;
4232         bool dropped_extents = false;
4233         u64 truncate_offset = i_size;
4234         struct extent_buffer *leaf;
4235         int slot;
4236         int ins_nr = 0;
4237         int start_slot;
4238         int ret;
4239
4240         if (!(inode->flags & BTRFS_INODE_PREALLOC))
4241                 return 0;
4242
4243         key.objectid = ino;
4244         key.type = BTRFS_EXTENT_DATA_KEY;
4245         key.offset = i_size;
4246         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4247         if (ret < 0)
4248                 goto out;
4249
4250         /*
4251          * We must check if there is a prealloc extent that starts before the
4252          * i_size and crosses the i_size boundary. This is to ensure later we
4253          * truncate down to the end of that extent and not to the i_size, as
4254          * otherwise we end up losing part of the prealloc extent after a log
4255          * replay and with an implicit hole if there is another prealloc extent
4256          * that starts at an offset beyond i_size.
4257          */
4258         ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4259         if (ret < 0)
4260                 goto out;
4261
4262         if (ret == 0) {
4263                 struct btrfs_file_extent_item *ei;
4264
4265                 leaf = path->nodes[0];
4266                 slot = path->slots[0];
4267                 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4268
4269                 if (btrfs_file_extent_type(leaf, ei) ==
4270                     BTRFS_FILE_EXTENT_PREALLOC) {
4271                         u64 extent_end;
4272
4273                         btrfs_item_key_to_cpu(leaf, &key, slot);
4274                         extent_end = key.offset +
4275                                 btrfs_file_extent_num_bytes(leaf, ei);
4276
4277                         if (extent_end > i_size)
4278                                 truncate_offset = extent_end;
4279                 }
4280         } else {
4281                 ret = 0;
4282         }
4283
4284         while (true) {
4285                 leaf = path->nodes[0];
4286                 slot = path->slots[0];
4287
4288                 if (slot >= btrfs_header_nritems(leaf)) {
4289                         if (ins_nr > 0) {
4290                                 ret = copy_items(trans, inode, dst_path, path,
4291                                                  start_slot, ins_nr, 1, 0);
4292                                 if (ret < 0)
4293                                         goto out;
4294                                 ins_nr = 0;
4295                         }
4296                         ret = btrfs_next_leaf(root, path);
4297                         if (ret < 0)
4298                                 goto out;
4299                         if (ret > 0) {
4300                                 ret = 0;
4301                                 break;
4302                         }
4303                         continue;
4304                 }
4305
4306                 btrfs_item_key_to_cpu(leaf, &key, slot);
4307                 if (key.objectid > ino)
4308                         break;
4309                 if (WARN_ON_ONCE(key.objectid < ino) ||
4310                     key.type < BTRFS_EXTENT_DATA_KEY ||
4311                     key.offset < i_size) {
4312                         path->slots[0]++;
4313                         continue;
4314                 }
4315                 if (!dropped_extents) {
4316                         /*
4317                          * Avoid logging extent items logged in past fsync calls
4318                          * and leading to duplicate keys in the log tree.
4319                          */
4320                         do {
4321                                 ret = btrfs_truncate_inode_items(trans,
4322                                                          root->log_root,
4323                                                          &inode->vfs_inode,
4324                                                          truncate_offset,
4325                                                          BTRFS_EXTENT_DATA_KEY);
4326                         } while (ret == -EAGAIN);
4327                         if (ret)
4328                                 goto out;
4329                         dropped_extents = true;
4330                 }
4331                 if (ins_nr == 0)
4332                         start_slot = slot;
4333                 ins_nr++;
4334                 path->slots[0]++;
4335                 if (!dst_path) {
4336                         dst_path = btrfs_alloc_path();
4337                         if (!dst_path) {
4338                                 ret = -ENOMEM;
4339                                 goto out;
4340                         }
4341                 }
4342         }
4343         if (ins_nr > 0) {
4344                 ret = copy_items(trans, inode, dst_path, path,
4345                                  start_slot, ins_nr, 1, 0);
4346                 if (ret > 0)
4347                         ret = 0;
4348         }
4349 out:
4350         btrfs_release_path(path);
4351         btrfs_free_path(dst_path);
4352         return ret;
4353 }
4354
4355 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4356                                      struct btrfs_root *root,
4357                                      struct btrfs_inode *inode,
4358                                      struct btrfs_path *path,
4359                                      struct btrfs_log_ctx *ctx,
4360                                      const u64 start,
4361                                      const u64 end)
4362 {
4363         struct extent_map *em, *n;
4364         struct list_head extents;
4365         struct extent_map_tree *tree = &inode->extent_tree;
4366         u64 logged_start, logged_end;
4367         u64 test_gen;
4368         int ret = 0;
4369         int num = 0;
4370
4371         INIT_LIST_HEAD(&extents);
4372
4373         write_lock(&tree->lock);
4374         test_gen = root->fs_info->last_trans_committed;
4375         logged_start = start;
4376         logged_end = end;
4377
4378         list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4379                 /*
4380                  * Skip extents outside our logging range. It's important to do
4381                  * it for correctness because if we don't ignore them, we may
4382                  * log them before their ordered extent completes, and therefore
4383                  * we could log them without logging their respective checksums
4384                  * (the checksum items are added to the csum tree at the very
4385                  * end of btrfs_finish_ordered_io()). Also leave such extents
4386                  * outside of our range in the list, since we may have another
4387                  * ranged fsync in the near future that needs them. If an extent
4388                  * outside our range corresponds to a hole, log it to avoid
4389                  * leaving gaps between extents (fsck will complain when we are
4390                  * not using the NO_HOLES feature).
4391                  */
4392                 if ((em->start > end || em->start + em->len <= start) &&
4393                     em->block_start != EXTENT_MAP_HOLE)
4394                         continue;
4395
4396                 list_del_init(&em->list);
4397                 /*
4398                  * Just an arbitrary number, this can be really CPU intensive
4399                  * once we start getting a lot of extents, and really once we
4400                  * have a bunch of extents we just want to commit since it will
4401                  * be faster.
4402                  */
4403                 if (++num > 32768) {
4404                         list_del_init(&tree->modified_extents);
4405                         ret = -EFBIG;
4406                         goto process;
4407                 }
4408
4409                 if (em->generation <= test_gen)
4410                         continue;
4411
4412                 /* We log prealloc extents beyond eof later. */
4413                 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4414                     em->start >= i_size_read(&inode->vfs_inode))
4415                         continue;
4416
4417                 if (em->start < logged_start)
4418                         logged_start = em->start;
4419                 if ((em->start + em->len - 1) > logged_end)
4420                         logged_end = em->start + em->len - 1;
4421
4422                 /* Need a ref to keep it from getting evicted from cache */
4423                 refcount_inc(&em->refs);
4424                 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4425                 list_add_tail(&em->list, &extents);
4426                 num++;
4427         }
4428
4429         list_sort(NULL, &extents, extent_cmp);
4430 process:
4431         while (!list_empty(&extents)) {
4432                 em = list_entry(extents.next, struct extent_map, list);
4433
4434                 list_del_init(&em->list);
4435
4436                 /*
4437                  * If we had an error we just need to delete everybody from our
4438                  * private list.
4439                  */
4440                 if (ret) {
4441                         clear_em_logging(tree, em);
4442                         free_extent_map(em);
4443                         continue;
4444                 }
4445
4446                 write_unlock(&tree->lock);
4447
4448                 ret = log_one_extent(trans, inode, root, em, path, ctx);
4449                 write_lock(&tree->lock);
4450                 clear_em_logging(tree, em);
4451                 free_extent_map(em);
4452         }
4453         WARN_ON(!list_empty(&extents));
4454         write_unlock(&tree->lock);
4455
4456         btrfs_release_path(path);
4457         if (!ret)
4458                 ret = btrfs_log_prealloc_extents(trans, inode, path);
4459
4460         return ret;
4461 }
4462
4463 static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
4464                              struct btrfs_path *path, u64 *size_ret)
4465 {
4466         struct btrfs_key key;
4467         int ret;
4468
4469         key.objectid = btrfs_ino(inode);
4470         key.type = BTRFS_INODE_ITEM_KEY;
4471         key.offset = 0;
4472
4473         ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4474         if (ret < 0) {
4475                 return ret;
4476         } else if (ret > 0) {
4477                 *size_ret = 0;
4478         } else {
4479                 struct btrfs_inode_item *item;
4480
4481                 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4482                                       struct btrfs_inode_item);
4483                 *size_ret = btrfs_inode_size(path->nodes[0], item);
4484                 /*
4485                  * If the in-memory inode's i_size is smaller then the inode
4486                  * size stored in the btree, return the inode's i_size, so
4487                  * that we get a correct inode size after replaying the log
4488                  * when before a power failure we had a shrinking truncate
4489                  * followed by addition of a new name (rename / new hard link).
4490                  * Otherwise return the inode size from the btree, to avoid
4491                  * data loss when replaying a log due to previously doing a
4492                  * write that expands the inode's size and logging a new name
4493                  * immediately after.
4494                  */
4495                 if (*size_ret > inode->vfs_inode.i_size)
4496                         *size_ret = inode->vfs_inode.i_size;
4497         }
4498
4499         btrfs_release_path(path);
4500         return 0;
4501 }
4502
4503 /*
4504  * At the moment we always log all xattrs. This is to figure out at log replay
4505  * time which xattrs must have their deletion replayed. If a xattr is missing
4506  * in the log tree and exists in the fs/subvol tree, we delete it. This is
4507  * because if a xattr is deleted, the inode is fsynced and a power failure
4508  * happens, causing the log to be replayed the next time the fs is mounted,
4509  * we want the xattr to not exist anymore (same behaviour as other filesystems
4510  * with a journal, ext3/4, xfs, f2fs, etc).
4511  */
4512 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4513                                 struct btrfs_root *root,
4514                                 struct btrfs_inode *inode,
4515                                 struct btrfs_path *path,
4516                                 struct btrfs_path *dst_path)
4517 {
4518         int ret;
4519         struct btrfs_key key;
4520         const u64 ino = btrfs_ino(inode);
4521         int ins_nr = 0;
4522         int start_slot = 0;
4523
4524         key.objectid = ino;
4525         key.type = BTRFS_XATTR_ITEM_KEY;
4526         key.offset = 0;
4527
4528         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4529         if (ret < 0)
4530                 return ret;
4531
4532         while (true) {
4533                 int slot = path->slots[0];
4534                 struct extent_buffer *leaf = path->nodes[0];
4535                 int nritems = btrfs_header_nritems(leaf);
4536
4537                 if (slot >= nritems) {
4538                         if (ins_nr > 0) {
4539                                 ret = copy_items(trans, inode, dst_path, path,
4540                                                  start_slot, ins_nr, 1, 0);
4541                                 if (ret < 0)
4542                                         return ret;
4543                                 ins_nr = 0;
4544                         }
4545                         ret = btrfs_next_leaf(root, path);
4546                         if (ret < 0)
4547                                 return ret;
4548                         else if (ret > 0)
4549                                 break;
4550                         continue;
4551                 }
4552
4553                 btrfs_item_key_to_cpu(leaf, &key, slot);
4554                 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4555                         break;
4556
4557                 if (ins_nr == 0)
4558                         start_slot = slot;
4559                 ins_nr++;
4560                 path->slots[0]++;
4561                 cond_resched();
4562         }
4563         if (ins_nr > 0) {
4564                 ret = copy_items(trans, inode, dst_path, path,
4565                                  start_slot, ins_nr, 1, 0);
4566                 if (ret < 0)
4567                         return ret;
4568         }
4569
4570         return 0;
4571 }
4572
4573 /*
4574  * When using the NO_HOLES feature if we punched a hole that causes the
4575  * deletion of entire leafs or all the extent items of the first leaf (the one
4576  * that contains the inode item and references) we may end up not processing
4577  * any extents, because there are no leafs with a generation matching the
4578  * current transaction that have extent items for our inode. So we need to find
4579  * if any holes exist and then log them. We also need to log holes after any
4580  * truncate operation that changes the inode's size.
4581  */
4582 static int btrfs_log_holes(struct btrfs_trans_handle *trans,
4583                            struct btrfs_root *root,
4584                            struct btrfs_inode *inode,
4585                            struct btrfs_path *path)
4586 {
4587         struct btrfs_fs_info *fs_info = root->fs_info;
4588         struct btrfs_key key;
4589         const u64 ino = btrfs_ino(inode);
4590         const u64 i_size = i_size_read(&inode->vfs_inode);
4591         u64 prev_extent_end = 0;
4592         int ret;
4593
4594         if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
4595                 return 0;
4596
4597         key.objectid = ino;
4598         key.type = BTRFS_EXTENT_DATA_KEY;
4599         key.offset = 0;
4600
4601         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4602         if (ret < 0)
4603                 return ret;
4604
4605         while (true) {
4606                 struct btrfs_file_extent_item *extent;
4607                 struct extent_buffer *leaf = path->nodes[0];
4608                 u64 len;
4609
4610                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4611                         ret = btrfs_next_leaf(root, path);
4612                         if (ret < 0)
4613                                 return ret;
4614                         if (ret > 0) {
4615                                 ret = 0;
4616                                 break;
4617                         }
4618                         leaf = path->nodes[0];
4619                 }
4620
4621                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4622                 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
4623                         break;
4624
4625                 /* We have a hole, log it. */
4626                 if (prev_extent_end < key.offset) {
4627                         const u64 hole_len = key.offset - prev_extent_end;
4628
4629                         /*
4630                          * Release the path to avoid deadlocks with other code
4631                          * paths that search the root while holding locks on
4632                          * leafs from the log root.
4633                          */
4634                         btrfs_release_path(path);
4635                         ret = btrfs_insert_file_extent(trans, root->log_root,
4636                                                        ino, prev_extent_end, 0,
4637                                                        0, hole_len, 0, hole_len,
4638                                                        0, 0, 0);
4639                         if (ret < 0)
4640                                 return ret;
4641
4642                         /*
4643                          * Search for the same key again in the root. Since it's
4644                          * an extent item and we are holding the inode lock, the
4645                          * key must still exist. If it doesn't just emit warning
4646                          * and return an error to fall back to a transaction
4647                          * commit.
4648                          */
4649                         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4650                         if (ret < 0)
4651                                 return ret;
4652                         if (WARN_ON(ret > 0))
4653                                 return -ENOENT;
4654                         leaf = path->nodes[0];
4655                 }
4656
4657                 extent = btrfs_item_ptr(leaf, path->slots[0],
4658                                         struct btrfs_file_extent_item);
4659                 if (btrfs_file_extent_type(leaf, extent) ==
4660                     BTRFS_FILE_EXTENT_INLINE) {
4661                         len = btrfs_file_extent_ram_bytes(leaf, extent);
4662                         prev_extent_end = ALIGN(key.offset + len,
4663                                                 fs_info->sectorsize);
4664                 } else {
4665                         len = btrfs_file_extent_num_bytes(leaf, extent);
4666                         prev_extent_end = key.offset + len;
4667                 }
4668
4669                 path->slots[0]++;
4670                 cond_resched();
4671         }
4672
4673         if (prev_extent_end < i_size) {
4674                 u64 hole_len;
4675
4676                 btrfs_release_path(path);
4677                 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
4678                 ret = btrfs_insert_file_extent(trans, root->log_root,
4679                                                ino, prev_extent_end, 0, 0,
4680                                                hole_len, 0, hole_len,
4681                                                0, 0, 0);
4682                 if (ret < 0)
4683                         return ret;
4684         }
4685
4686         return 0;
4687 }
4688
4689 /*
4690  * When we are logging a new inode X, check if it doesn't have a reference that
4691  * matches the reference from some other inode Y created in a past transaction
4692  * and that was renamed in the current transaction. If we don't do this, then at
4693  * log replay time we can lose inode Y (and all its files if it's a directory):
4694  *
4695  * mkdir /mnt/x
4696  * echo "hello world" > /mnt/x/foobar
4697  * sync
4698  * mv /mnt/x /mnt/y
4699  * mkdir /mnt/x                 # or touch /mnt/x
4700  * xfs_io -c fsync /mnt/x
4701  * <power fail>
4702  * mount fs, trigger log replay
4703  *
4704  * After the log replay procedure, we would lose the first directory and all its
4705  * files (file foobar).
4706  * For the case where inode Y is not a directory we simply end up losing it:
4707  *
4708  * echo "123" > /mnt/foo
4709  * sync
4710  * mv /mnt/foo /mnt/bar
4711  * echo "abc" > /mnt/foo
4712  * xfs_io -c fsync /mnt/foo
4713  * <power fail>
4714  *
4715  * We also need this for cases where a snapshot entry is replaced by some other
4716  * entry (file or directory) otherwise we end up with an unreplayable log due to
4717  * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4718  * if it were a regular entry:
4719  *
4720  * mkdir /mnt/x
4721  * btrfs subvolume snapshot /mnt /mnt/x/snap
4722  * btrfs subvolume delete /mnt/x/snap
4723  * rmdir /mnt/x
4724  * mkdir /mnt/x
4725  * fsync /mnt/x or fsync some new file inside it
4726  * <power fail>
4727  *
4728  * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4729  * the same transaction.
4730  */
4731 static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4732                                          const int slot,
4733                                          const struct btrfs_key *key,
4734                                          struct btrfs_inode *inode,
4735                                          u64 *other_ino)
4736 {
4737         int ret;
4738         struct btrfs_path *search_path;
4739         char *name = NULL;
4740         u32 name_len = 0;
4741         u32 item_size = btrfs_item_size_nr(eb, slot);
4742         u32 cur_offset = 0;
4743         unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4744
4745         search_path = btrfs_alloc_path();
4746         if (!search_path)
4747                 return -ENOMEM;
4748         search_path->search_commit_root = 1;
4749         search_path->skip_locking = 1;
4750
4751         while (cur_offset < item_size) {
4752                 u64 parent;
4753                 u32 this_name_len;
4754                 u32 this_len;
4755                 unsigned long name_ptr;
4756                 struct btrfs_dir_item *di;
4757
4758                 if (key->type == BTRFS_INODE_REF_KEY) {
4759                         struct btrfs_inode_ref *iref;
4760
4761                         iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4762                         parent = key->offset;
4763                         this_name_len = btrfs_inode_ref_name_len(eb, iref);
4764                         name_ptr = (unsigned long)(iref + 1);
4765                         this_len = sizeof(*iref) + this_name_len;
4766                 } else {
4767                         struct btrfs_inode_extref *extref;
4768
4769                         extref = (struct btrfs_inode_extref *)(ptr +
4770                                                                cur_offset);
4771                         parent = btrfs_inode_extref_parent(eb, extref);
4772                         this_name_len = btrfs_inode_extref_name_len(eb, extref);
4773                         name_ptr = (unsigned long)&extref->name;
4774                         this_len = sizeof(*extref) + this_name_len;
4775                 }
4776
4777                 if (this_name_len > name_len) {
4778                         char *new_name;
4779
4780                         new_name = krealloc(name, this_name_len, GFP_NOFS);
4781                         if (!new_name) {
4782                                 ret = -ENOMEM;
4783                                 goto out;
4784                         }
4785                         name_len = this_name_len;
4786                         name = new_name;
4787                 }
4788
4789                 read_extent_buffer(eb, name, name_ptr, this_name_len);
4790                 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4791                                 parent, name, this_name_len, 0);
4792                 if (di && !IS_ERR(di)) {
4793                         struct btrfs_key di_key;
4794
4795                         btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4796                                                   di, &di_key);
4797                         if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4798                                 ret = 1;
4799                                 *other_ino = di_key.objectid;
4800                         } else {
4801                                 ret = -EAGAIN;
4802                         }
4803                         goto out;
4804                 } else if (IS_ERR(di)) {
4805                         ret = PTR_ERR(di);
4806                         goto out;
4807                 }
4808                 btrfs_release_path(search_path);
4809
4810                 cur_offset += this_len;
4811         }
4812         ret = 0;
4813 out:
4814         btrfs_free_path(search_path);
4815         kfree(name);
4816         return ret;
4817 }
4818
4819 /* log a single inode in the tree log.
4820  * At least one parent directory for this inode must exist in the tree
4821  * or be logged already.
4822  *
4823  * Any items from this inode changed by the current transaction are copied
4824  * to the log tree.  An extra reference is taken on any extents in this
4825  * file, allowing us to avoid a whole pile of corner cases around logging
4826  * blocks that have been removed from the tree.
4827  *
4828  * See LOG_INODE_ALL and related defines for a description of what inode_only
4829  * does.
4830  *
4831  * This handles both files and directories.
4832  */
4833 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4834                            struct btrfs_root *root, struct btrfs_inode *inode,
4835                            int inode_only,
4836                            const loff_t start,
4837                            const loff_t end,
4838                            struct btrfs_log_ctx *ctx)
4839 {
4840         struct btrfs_fs_info *fs_info = root->fs_info;
4841         struct btrfs_path *path;
4842         struct btrfs_path *dst_path;
4843         struct btrfs_key min_key;
4844         struct btrfs_key max_key;
4845         struct btrfs_root *log = root->log_root;
4846         int err = 0;
4847         int ret;
4848         int nritems;
4849         int ins_start_slot = 0;
4850         int ins_nr;
4851         bool fast_search = false;
4852         u64 ino = btrfs_ino(inode);
4853         struct extent_map_tree *em_tree = &inode->extent_tree;
4854         u64 logged_isize = 0;
4855         bool need_log_inode_item = true;
4856         bool xattrs_logged = false;
4857
4858         path = btrfs_alloc_path();
4859         if (!path)
4860                 return -ENOMEM;
4861         dst_path = btrfs_alloc_path();
4862         if (!dst_path) {
4863                 btrfs_free_path(path);
4864                 return -ENOMEM;
4865         }
4866
4867         min_key.objectid = ino;
4868         min_key.type = BTRFS_INODE_ITEM_KEY;
4869         min_key.offset = 0;
4870
4871         max_key.objectid = ino;
4872
4873
4874         /* today the code can only do partial logging of directories */
4875         if (S_ISDIR(inode->vfs_inode.i_mode) ||
4876             (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4877                        &inode->runtime_flags) &&
4878              inode_only >= LOG_INODE_EXISTS))
4879                 max_key.type = BTRFS_XATTR_ITEM_KEY;
4880         else
4881                 max_key.type = (u8)-1;
4882         max_key.offset = (u64)-1;
4883
4884         /*
4885          * Only run delayed items if we are a dir or a new file.
4886          * Otherwise commit the delayed inode only, which is needed in
4887          * order for the log replay code to mark inodes for link count
4888          * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4889          */
4890         if (S_ISDIR(inode->vfs_inode.i_mode) ||
4891             inode->generation > fs_info->last_trans_committed)
4892                 ret = btrfs_commit_inode_delayed_items(trans, inode);
4893         else
4894                 ret = btrfs_commit_inode_delayed_inode(inode);
4895
4896         if (ret) {
4897                 btrfs_free_path(path);
4898                 btrfs_free_path(dst_path);
4899                 return ret;
4900         }
4901
4902         if (inode_only == LOG_OTHER_INODE) {
4903                 inode_only = LOG_INODE_EXISTS;
4904                 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
4905         } else {
4906                 mutex_lock(&inode->log_mutex);
4907         }
4908
4909         /*
4910          * For symlinks, we must always log their content, which is stored in an
4911          * inline extent, otherwise we could end up with an empty symlink after
4912          * log replay, which is invalid on linux (symlink(2) returns -ENOENT if
4913          * one attempts to create an empty symlink).
4914          * We don't need to worry about flushing delalloc, because when we create
4915          * the inline extent when the symlink is created (we never have delalloc
4916          * for symlinks).
4917          */
4918         if (S_ISLNK(inode->vfs_inode.i_mode))
4919                 inode_only = LOG_INODE_ALL;
4920
4921         /*
4922          * a brute force approach to making sure we get the most uptodate
4923          * copies of everything.
4924          */
4925         if (S_ISDIR(inode->vfs_inode.i_mode)) {
4926                 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4927
4928                 if (inode_only == LOG_INODE_EXISTS)
4929                         max_key_type = BTRFS_XATTR_ITEM_KEY;
4930                 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
4931         } else {
4932                 if (inode_only == LOG_INODE_EXISTS) {
4933                         /*
4934                          * Make sure the new inode item we write to the log has
4935                          * the same isize as the current one (if it exists).
4936                          * This is necessary to prevent data loss after log
4937                          * replay, and also to prevent doing a wrong expanding
4938                          * truncate - for e.g. create file, write 4K into offset
4939                          * 0, fsync, write 4K into offset 4096, add hard link,
4940                          * fsync some other file (to sync log), power fail - if
4941                          * we use the inode's current i_size, after log replay
4942                          * we get a 8Kb file, with the last 4Kb extent as a hole
4943                          * (zeroes), as if an expanding truncate happened,
4944                          * instead of getting a file of 4Kb only.
4945                          */
4946                         err = logged_inode_size(log, inode, path, &logged_isize);
4947                         if (err)
4948                                 goto out_unlock;
4949                 }
4950                 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4951                              &inode->runtime_flags)) {
4952                         if (inode_only == LOG_INODE_EXISTS) {
4953                                 max_key.type = BTRFS_XATTR_ITEM_KEY;
4954                                 ret = drop_objectid_items(trans, log, path, ino,
4955                                                           max_key.type);
4956                         } else {
4957                                 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4958                                           &inode->runtime_flags);
4959                                 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4960                                           &inode->runtime_flags);
4961                                 while(1) {
4962                                         ret = btrfs_truncate_inode_items(trans,
4963                                                 log, &inode->vfs_inode, 0, 0);
4964                                         if (ret != -EAGAIN)
4965                                                 break;
4966                                 }
4967                         }
4968                 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4969                                               &inode->runtime_flags) ||
4970                            inode_only == LOG_INODE_EXISTS) {
4971                         if (inode_only == LOG_INODE_ALL)
4972                                 fast_search = true;
4973                         max_key.type = BTRFS_XATTR_ITEM_KEY;
4974                         ret = drop_objectid_items(trans, log, path, ino,
4975                                                   max_key.type);
4976                 } else {
4977                         if (inode_only == LOG_INODE_ALL)
4978                                 fast_search = true;
4979                         goto log_extents;
4980                 }
4981
4982         }
4983         if (ret) {
4984                 err = ret;
4985                 goto out_unlock;
4986         }
4987
4988         while (1) {
4989                 ins_nr = 0;
4990                 ret = btrfs_search_forward(root, &min_key,
4991                                            path, trans->transid);
4992                 if (ret < 0) {
4993                         err = ret;
4994                         goto out_unlock;
4995                 }
4996                 if (ret != 0)
4997                         break;
4998 again:
4999                 /* note, ins_nr might be > 0 here, cleanup outside the loop */
5000                 if (min_key.objectid != ino)
5001                         break;
5002                 if (min_key.type > max_key.type)
5003                         break;
5004
5005                 if (min_key.type == BTRFS_INODE_ITEM_KEY)
5006                         need_log_inode_item = false;
5007
5008                 if ((min_key.type == BTRFS_INODE_REF_KEY ||
5009                      min_key.type == BTRFS_INODE_EXTREF_KEY) &&
5010                     inode->generation == trans->transid) {
5011                         u64 other_ino = 0;
5012
5013                         ret = btrfs_check_ref_name_override(path->nodes[0],
5014                                         path->slots[0], &min_key, inode,
5015                                         &other_ino);
5016                         if (ret < 0) {
5017                                 err = ret;
5018                                 goto out_unlock;
5019                         } else if (ret > 0 && ctx &&
5020                                    other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5021                                 struct btrfs_key inode_key;
5022                                 struct inode *other_inode;
5023
5024                                 if (ins_nr > 0) {
5025                                         ins_nr++;
5026                                 } else {
5027                                         ins_nr = 1;
5028                                         ins_start_slot = path->slots[0];
5029                                 }
5030                                 ret = copy_items(trans, inode, dst_path, path,
5031                                                  ins_start_slot,
5032                                                  ins_nr, inode_only,
5033                                                  logged_isize);
5034                                 if (ret < 0) {
5035                                         err = ret;
5036                                         goto out_unlock;
5037                                 }
5038                                 ins_nr = 0;
5039                                 btrfs_release_path(path);
5040                                 inode_key.objectid = other_ino;
5041                                 inode_key.type = BTRFS_INODE_ITEM_KEY;
5042                                 inode_key.offset = 0;
5043                                 other_inode = btrfs_iget(fs_info->sb,
5044                                                          &inode_key, root,
5045                                                          NULL);
5046                                 /*
5047                                  * If the other inode that had a conflicting dir
5048                                  * entry was deleted in the current transaction,
5049                                  * we don't need to do more work nor fallback to
5050                                  * a transaction commit.
5051                                  */
5052                                 if (other_inode == ERR_PTR(-ENOENT)) {
5053                                         goto next_key;
5054                                 } else if (IS_ERR(other_inode)) {
5055                                         err = PTR_ERR(other_inode);
5056                                         goto out_unlock;
5057                                 }
5058                                 /*
5059                                  * We are safe logging the other inode without
5060                                  * acquiring its i_mutex as long as we log with
5061                                  * the LOG_INODE_EXISTS mode. We're safe against
5062                                  * concurrent renames of the other inode as well
5063                                  * because during a rename we pin the log and
5064                                  * update the log with the new name before we
5065                                  * unpin it.
5066                                  */
5067                                 err = btrfs_log_inode(trans, root,
5068                                                 BTRFS_I(other_inode),
5069                                                 LOG_OTHER_INODE, 0, LLONG_MAX,
5070                                                 ctx);
5071                                 btrfs_add_delayed_iput(other_inode);
5072                                 if (err)
5073                                         goto out_unlock;
5074                                 else
5075                                         goto next_key;
5076                         }
5077                 }
5078
5079                 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5080                 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5081                         if (ins_nr == 0)
5082                                 goto next_slot;
5083                         ret = copy_items(trans, inode, dst_path, path,
5084                                          ins_start_slot,
5085                                          ins_nr, inode_only, logged_isize);
5086                         if (ret < 0) {
5087                                 err = ret;
5088                                 goto out_unlock;
5089                         }
5090                         ins_nr = 0;
5091                         goto next_slot;
5092                 }
5093
5094                 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5095                         ins_nr++;
5096                         goto next_slot;
5097                 } else if (!ins_nr) {
5098                         ins_start_slot = path->slots[0];
5099                         ins_nr = 1;
5100                         goto next_slot;
5101                 }
5102
5103                 ret = copy_items(trans, inode, dst_path, path,
5104                                  ins_start_slot, ins_nr, inode_only,
5105                                  logged_isize);
5106                 if (ret < 0) {
5107                         err = ret;
5108                         goto out_unlock;
5109                 }
5110                 ins_nr = 1;
5111                 ins_start_slot = path->slots[0];
5112 next_slot:
5113
5114                 nritems = btrfs_header_nritems(path->nodes[0]);
5115                 path->slots[0]++;
5116                 if (path->slots[0] < nritems) {
5117                         btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5118                                               path->slots[0]);
5119                         goto again;
5120                 }
5121                 if (ins_nr) {
5122                         ret = copy_items(trans, inode, dst_path, path,
5123                                          ins_start_slot,
5124                                          ins_nr, inode_only, logged_isize);
5125                         if (ret < 0) {
5126                                 err = ret;
5127                                 goto out_unlock;
5128                         }
5129                         ins_nr = 0;
5130                 }
5131                 btrfs_release_path(path);
5132 next_key:
5133                 if (min_key.offset < (u64)-1) {
5134                         min_key.offset++;
5135                 } else if (min_key.type < max_key.type) {
5136                         min_key.type++;
5137                         min_key.offset = 0;
5138                 } else {
5139                         break;
5140                 }
5141         }
5142         if (ins_nr) {
5143                 ret = copy_items(trans, inode, dst_path, path,
5144                                  ins_start_slot, ins_nr, inode_only,
5145                                  logged_isize);
5146                 if (ret < 0) {
5147                         err = ret;
5148                         goto out_unlock;
5149                 }
5150                 ins_nr = 0;
5151         }
5152
5153         btrfs_release_path(path);
5154         btrfs_release_path(dst_path);
5155         err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
5156         if (err)
5157                 goto out_unlock;
5158         xattrs_logged = true;
5159         if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5160                 btrfs_release_path(path);
5161                 btrfs_release_path(dst_path);
5162                 err = btrfs_log_holes(trans, root, inode, path);
5163                 if (err)
5164                         goto out_unlock;
5165         }
5166 log_extents:
5167         btrfs_release_path(path);
5168         btrfs_release_path(dst_path);
5169         if (need_log_inode_item) {
5170                 err = log_inode_item(trans, log, dst_path, inode);
5171                 if (!err && !xattrs_logged) {
5172                         err = btrfs_log_all_xattrs(trans, root, inode, path,
5173                                                    dst_path);
5174                         btrfs_release_path(path);
5175                 }
5176                 if (err)
5177                         goto out_unlock;
5178         }
5179         if (fast_search) {
5180                 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
5181                                                 ctx, start, end);
5182                 if (ret) {
5183                         err = ret;
5184                         goto out_unlock;
5185                 }
5186         } else if (inode_only == LOG_INODE_ALL) {
5187                 struct extent_map *em, *n;
5188
5189                 write_lock(&em_tree->lock);
5190                 /*
5191                  * We can't just remove every em if we're called for a ranged
5192                  * fsync - that is, one that doesn't cover the whole possible
5193                  * file range (0 to LLONG_MAX). This is because we can have
5194                  * em's that fall outside the range we're logging and therefore
5195                  * their ordered operations haven't completed yet
5196                  * (btrfs_finish_ordered_io() not invoked yet). This means we
5197                  * didn't get their respective file extent item in the fs/subvol
5198                  * tree yet, and need to let the next fast fsync (one which
5199                  * consults the list of modified extent maps) find the em so
5200                  * that it logs a matching file extent item and waits for the
5201                  * respective ordered operation to complete (if it's still
5202                  * running).
5203                  *
5204                  * Removing every em outside the range we're logging would make
5205                  * the next fast fsync not log their matching file extent items,
5206                  * therefore making us lose data after a log replay.
5207                  */
5208                 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5209                                          list) {
5210                         const u64 mod_end = em->mod_start + em->mod_len - 1;
5211
5212                         if (em->mod_start >= start && mod_end <= end)
5213                                 list_del_init(&em->list);
5214                 }
5215                 write_unlock(&em_tree->lock);
5216         }
5217
5218         if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5219                 ret = log_directory_changes(trans, root, inode, path, dst_path,
5220                                         ctx);
5221                 if (ret) {
5222                         err = ret;
5223                         goto out_unlock;
5224                 }
5225         }
5226
5227         /*
5228          * Don't update last_log_commit if we logged that an inode exists after
5229          * it was loaded to memory (full_sync bit set).
5230          * This is to prevent data loss when we do a write to the inode, then
5231          * the inode gets evicted after all delalloc was flushed, then we log
5232          * it exists (due to a rename for example) and then fsync it. This last
5233          * fsync would do nothing (not logging the extents previously written).
5234          */
5235         spin_lock(&inode->lock);
5236         inode->logged_trans = trans->transid;
5237         if (inode_only != LOG_INODE_EXISTS ||
5238             !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5239                 inode->last_log_commit = inode->last_sub_trans;
5240         spin_unlock(&inode->lock);
5241 out_unlock:
5242         mutex_unlock(&inode->log_mutex);
5243
5244         btrfs_free_path(path);
5245         btrfs_free_path(dst_path);
5246         return err;
5247 }
5248
5249 /*
5250  * Check if we must fallback to a transaction commit when logging an inode.
5251  * This must be called after logging the inode and is used only in the context
5252  * when fsyncing an inode requires the need to log some other inode - in which
5253  * case we can't lock the i_mutex of each other inode we need to log as that
5254  * can lead to deadlocks with concurrent fsync against other inodes (as we can
5255  * log inodes up or down in the hierarchy) or rename operations for example. So
5256  * we take the log_mutex of the inode after we have logged it and then check for
5257  * its last_unlink_trans value - this is safe because any task setting
5258  * last_unlink_trans must take the log_mutex and it must do this before it does
5259  * the actual unlink operation, so if we do this check before a concurrent task
5260  * sets last_unlink_trans it means we've logged a consistent version/state of
5261  * all the inode items, otherwise we are not sure and must do a transaction
5262  * commit (the concurrent task might have only updated last_unlink_trans before
5263  * we logged the inode or it might have also done the unlink).
5264  */
5265 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5266                                           struct btrfs_inode *inode)
5267 {
5268         struct btrfs_fs_info *fs_info = inode->root->fs_info;
5269         bool ret = false;
5270
5271         mutex_lock(&inode->log_mutex);
5272         if (inode->last_unlink_trans > fs_info->last_trans_committed) {
5273                 /*
5274                  * Make sure any commits to the log are forced to be full
5275                  * commits.
5276                  */
5277                 btrfs_set_log_full_commit(fs_info, trans);
5278                 ret = true;
5279         }
5280         mutex_unlock(&inode->log_mutex);
5281
5282         return ret;
5283 }
5284
5285 /*
5286  * follow the dentry parent pointers up the chain and see if any
5287  * of the directories in it require a full commit before they can
5288  * be logged.  Returns zero if nothing special needs to be done or 1 if
5289  * a full commit is required.
5290  */
5291 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5292                                                struct btrfs_inode *inode,
5293                                                struct dentry *parent,
5294                                                struct super_block *sb,
5295                                                u64 last_committed)
5296 {
5297         int ret = 0;
5298         struct dentry *old_parent = NULL;
5299
5300         /*
5301          * for regular files, if its inode is already on disk, we don't
5302          * have to worry about the parents at all.  This is because
5303          * we can use the last_unlink_trans field to record renames
5304          * and other fun in this file.
5305          */
5306         if (S_ISREG(inode->vfs_inode.i_mode) &&
5307             inode->generation <= last_committed &&
5308             inode->last_unlink_trans <= last_committed)
5309                 goto out;
5310
5311         if (!S_ISDIR(inode->vfs_inode.i_mode)) {
5312                 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5313                         goto out;
5314                 inode = BTRFS_I(d_inode(parent));
5315         }
5316
5317         while (1) {
5318                 if (btrfs_must_commit_transaction(trans, inode)) {
5319                         ret = 1;
5320                         break;
5321                 }
5322
5323                 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5324                         break;
5325
5326                 if (IS_ROOT(parent)) {
5327                         inode = BTRFS_I(d_inode(parent));
5328                         if (btrfs_must_commit_transaction(trans, inode))
5329                                 ret = 1;
5330                         break;
5331                 }
5332
5333                 parent = dget_parent(parent);
5334                 dput(old_parent);
5335                 old_parent = parent;
5336                 inode = BTRFS_I(d_inode(parent));
5337
5338         }
5339         dput(old_parent);
5340 out:
5341         return ret;
5342 }
5343
5344 struct btrfs_dir_list {
5345         u64 ino;
5346         struct list_head list;
5347 };
5348
5349 /*
5350  * Log the inodes of the new dentries of a directory. See log_dir_items() for
5351  * details about the why it is needed.
5352  * This is a recursive operation - if an existing dentry corresponds to a
5353  * directory, that directory's new entries are logged too (same behaviour as
5354  * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5355  * the dentries point to we do not lock their i_mutex, otherwise lockdep
5356  * complains about the following circular lock dependency / possible deadlock:
5357  *
5358  *        CPU0                                        CPU1
5359  *        ----                                        ----
5360  * lock(&type->i_mutex_dir_key#3/2);
5361  *                                            lock(sb_internal#2);
5362  *                                            lock(&type->i_mutex_dir_key#3/2);
5363  * lock(&sb->s_type->i_mutex_key#14);
5364  *
5365  * Where sb_internal is the lock (a counter that works as a lock) acquired by
5366  * sb_start_intwrite() in btrfs_start_transaction().
5367  * Not locking i_mutex of the inodes is still safe because:
5368  *
5369  * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5370  *    that while logging the inode new references (names) are added or removed
5371  *    from the inode, leaving the logged inode item with a link count that does
5372  *    not match the number of logged inode reference items. This is fine because
5373  *    at log replay time we compute the real number of links and correct the
5374  *    link count in the inode item (see replay_one_buffer() and
5375  *    link_to_fixup_dir());
5376  *
5377  * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5378  *    while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5379  *    BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5380  *    has a size that doesn't match the sum of the lengths of all the logged
5381  *    names. This does not result in a problem because if a dir_item key is
5382  *    logged but its matching dir_index key is not logged, at log replay time we
5383  *    don't use it to replay the respective name (see replay_one_name()). On the
5384  *    other hand if only the dir_index key ends up being logged, the respective
5385  *    name is added to the fs/subvol tree with both the dir_item and dir_index
5386  *    keys created (see replay_one_name()).
5387  *    The directory's inode item with a wrong i_size is not a problem as well,
5388  *    since we don't use it at log replay time to set the i_size in the inode
5389  *    item of the fs/subvol tree (see overwrite_item()).
5390  */
5391 static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5392                                 struct btrfs_root *root,
5393                                 struct btrfs_inode *start_inode,
5394                                 struct btrfs_log_ctx *ctx)
5395 {
5396         struct btrfs_fs_info *fs_info = root->fs_info;
5397         struct btrfs_root *log = root->log_root;
5398         struct btrfs_path *path;
5399         LIST_HEAD(dir_list);
5400         struct btrfs_dir_list *dir_elem;
5401         int ret = 0;
5402
5403         path = btrfs_alloc_path();
5404         if (!path)
5405                 return -ENOMEM;
5406
5407         dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5408         if (!dir_elem) {
5409                 btrfs_free_path(path);
5410                 return -ENOMEM;
5411         }
5412         dir_elem->ino = btrfs_ino(start_inode);
5413         list_add_tail(&dir_elem->list, &dir_list);
5414
5415         while (!list_empty(&dir_list)) {
5416                 struct extent_buffer *leaf;
5417                 struct btrfs_key min_key;
5418                 int nritems;
5419                 int i;
5420
5421                 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5422                                             list);
5423                 if (ret)
5424                         goto next_dir_inode;
5425
5426                 min_key.objectid = dir_elem->ino;
5427                 min_key.type = BTRFS_DIR_ITEM_KEY;
5428                 min_key.offset = 0;
5429 again:
5430                 btrfs_release_path(path);
5431                 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5432                 if (ret < 0) {
5433                         goto next_dir_inode;
5434                 } else if (ret > 0) {
5435                         ret = 0;
5436                         goto next_dir_inode;
5437                 }
5438
5439 process_leaf:
5440                 leaf = path->nodes[0];
5441                 nritems = btrfs_header_nritems(leaf);
5442                 for (i = path->slots[0]; i < nritems; i++) {
5443                         struct btrfs_dir_item *di;
5444                         struct btrfs_key di_key;
5445                         struct inode *di_inode;
5446                         struct btrfs_dir_list *new_dir_elem;
5447                         int log_mode = LOG_INODE_EXISTS;
5448                         int type;
5449
5450                         btrfs_item_key_to_cpu(leaf, &min_key, i);
5451                         if (min_key.objectid != dir_elem->ino ||
5452                             min_key.type != BTRFS_DIR_ITEM_KEY)
5453                                 goto next_dir_inode;
5454
5455                         di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5456                         type = btrfs_dir_type(leaf, di);
5457                         if (btrfs_dir_transid(leaf, di) < trans->transid &&
5458                             type != BTRFS_FT_DIR)
5459                                 continue;
5460                         btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5461                         if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5462                                 continue;
5463
5464                         btrfs_release_path(path);
5465                         di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
5466                         if (IS_ERR(di_inode)) {
5467                                 ret = PTR_ERR(di_inode);
5468                                 goto next_dir_inode;
5469                         }
5470
5471                         if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
5472                                 btrfs_add_delayed_iput(di_inode);
5473                                 break;
5474                         }
5475
5476                         ctx->log_new_dentries = false;
5477                         if (type == BTRFS_FT_DIR)
5478                                 log_mode = LOG_INODE_ALL;
5479                         ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
5480                                               log_mode, 0, LLONG_MAX, ctx);
5481                         if (!ret &&
5482                             btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
5483                                 ret = 1;
5484                         btrfs_add_delayed_iput(di_inode);
5485                         if (ret)
5486                                 goto next_dir_inode;
5487                         if (ctx->log_new_dentries) {
5488                                 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5489                                                        GFP_NOFS);
5490                                 if (!new_dir_elem) {
5491                                         ret = -ENOMEM;
5492                                         goto next_dir_inode;
5493                                 }
5494                                 new_dir_elem->ino = di_key.objectid;
5495                                 list_add_tail(&new_dir_elem->list, &dir_list);
5496                         }
5497                         break;
5498                 }
5499                 if (i == nritems) {
5500                         ret = btrfs_next_leaf(log, path);
5501                         if (ret < 0) {
5502                                 goto next_dir_inode;
5503                         } else if (ret > 0) {
5504                                 ret = 0;
5505                                 goto next_dir_inode;
5506                         }
5507                         goto process_leaf;
5508                 }
5509                 if (min_key.offset < (u64)-1) {
5510                         min_key.offset++;
5511                         goto again;
5512                 }
5513 next_dir_inode:
5514                 list_del(&dir_elem->list);
5515                 kfree(dir_elem);
5516         }
5517
5518         btrfs_free_path(path);
5519         return ret;
5520 }
5521
5522 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5523                                  struct btrfs_inode *inode,
5524                                  struct btrfs_log_ctx *ctx)
5525 {
5526         struct btrfs_fs_info *fs_info = trans->fs_info;
5527         int ret;
5528         struct btrfs_path *path;
5529         struct btrfs_key key;
5530         struct btrfs_root *root = inode->root;
5531         const u64 ino = btrfs_ino(inode);
5532
5533         path = btrfs_alloc_path();
5534         if (!path)
5535                 return -ENOMEM;
5536         path->skip_locking = 1;
5537         path->search_commit_root = 1;
5538
5539         key.objectid = ino;
5540         key.type = BTRFS_INODE_REF_KEY;
5541         key.offset = 0;
5542         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5543         if (ret < 0)
5544                 goto out;
5545
5546         while (true) {
5547                 struct extent_buffer *leaf = path->nodes[0];
5548                 int slot = path->slots[0];
5549                 u32 cur_offset = 0;
5550                 u32 item_size;
5551                 unsigned long ptr;
5552
5553                 if (slot >= btrfs_header_nritems(leaf)) {
5554                         ret = btrfs_next_leaf(root, path);
5555                         if (ret < 0)
5556                                 goto out;
5557                         else if (ret > 0)
5558                                 break;
5559                         continue;
5560                 }
5561
5562                 btrfs_item_key_to_cpu(leaf, &key, slot);
5563                 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5564                 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5565                         break;
5566
5567                 item_size = btrfs_item_size_nr(leaf, slot);
5568                 ptr = btrfs_item_ptr_offset(leaf, slot);
5569                 while (cur_offset < item_size) {
5570                         struct btrfs_key inode_key;
5571                         struct inode *dir_inode;
5572
5573                         inode_key.type = BTRFS_INODE_ITEM_KEY;
5574                         inode_key.offset = 0;
5575
5576                         if (key.type == BTRFS_INODE_EXTREF_KEY) {
5577                                 struct btrfs_inode_extref *extref;
5578
5579                                 extref = (struct btrfs_inode_extref *)
5580                                         (ptr + cur_offset);
5581                                 inode_key.objectid = btrfs_inode_extref_parent(
5582                                         leaf, extref);
5583                                 cur_offset += sizeof(*extref);
5584                                 cur_offset += btrfs_inode_extref_name_len(leaf,
5585                                         extref);
5586                         } else {
5587                                 inode_key.objectid = key.offset;
5588                                 cur_offset = item_size;
5589                         }
5590
5591                         dir_inode = btrfs_iget(fs_info->sb, &inode_key,
5592                                                root, NULL);
5593                         /*
5594                          * If the parent inode was deleted, return an error to
5595                          * fallback to a transaction commit. This is to prevent
5596                          * getting an inode that was moved from one parent A to
5597                          * a parent B, got its former parent A deleted and then
5598                          * it got fsync'ed, from existing at both parents after
5599                          * a log replay (and the old parent still existing).
5600                          * Example:
5601                          *
5602                          * mkdir /mnt/A
5603                          * mkdir /mnt/B
5604                          * touch /mnt/B/bar
5605                          * sync
5606                          * mv /mnt/B/bar /mnt/A/bar
5607                          * mv -T /mnt/A /mnt/B
5608                          * fsync /mnt/B/bar
5609                          * <power fail>
5610                          *
5611                          * If we ignore the old parent B which got deleted,
5612                          * after a log replay we would have file bar linked
5613                          * at both parents and the old parent B would still
5614                          * exist.
5615                          */
5616                         if (IS_ERR(dir_inode)) {
5617                                 ret = PTR_ERR(dir_inode);
5618                                 goto out;
5619                         }
5620
5621                         if (ctx)
5622                                 ctx->log_new_dentries = false;
5623                         ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
5624                                               LOG_INODE_ALL, 0, LLONG_MAX, ctx);
5625                         if (!ret &&
5626                             btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
5627                                 ret = 1;
5628                         if (!ret && ctx && ctx->log_new_dentries)
5629                                 ret = log_new_dir_dentries(trans, root,
5630                                                    BTRFS_I(dir_inode), ctx);
5631                         btrfs_add_delayed_iput(dir_inode);
5632                         if (ret)
5633                                 goto out;
5634                 }
5635                 path->slots[0]++;
5636         }
5637         ret = 0;
5638 out:
5639         btrfs_free_path(path);
5640         return ret;
5641 }
5642
5643 /*
5644  * helper function around btrfs_log_inode to make sure newly created
5645  * parent directories also end up in the log.  A minimal inode and backref
5646  * only logging is done of any parent directories that are older than
5647  * the last committed transaction
5648  */
5649 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5650                                   struct btrfs_inode *inode,
5651                                   struct dentry *parent,
5652                                   const loff_t start,
5653                                   const loff_t end,
5654                                   int inode_only,
5655                                   struct btrfs_log_ctx *ctx)
5656 {
5657         struct btrfs_root *root = inode->root;
5658         struct btrfs_fs_info *fs_info = root->fs_info;
5659         struct super_block *sb;
5660         struct dentry *old_parent = NULL;
5661         int ret = 0;
5662         u64 last_committed = fs_info->last_trans_committed;
5663         bool log_dentries = false;
5664         struct btrfs_inode *orig_inode = inode;
5665
5666         sb = inode->vfs_inode.i_sb;
5667
5668         if (btrfs_test_opt(fs_info, NOTREELOG)) {
5669                 ret = 1;
5670                 goto end_no_trans;
5671         }
5672
5673         /*
5674          * The prev transaction commit doesn't complete, we need do
5675          * full commit by ourselves.
5676          */
5677         if (fs_info->last_trans_log_full_commit >
5678             fs_info->last_trans_committed) {
5679                 ret = 1;
5680                 goto end_no_trans;
5681         }
5682
5683         if (btrfs_root_refs(&root->root_item) == 0) {
5684                 ret = 1;
5685                 goto end_no_trans;
5686         }
5687
5688         ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5689                         last_committed);
5690         if (ret)
5691                 goto end_no_trans;
5692
5693         /*
5694          * Skip already logged inodes or inodes corresponding to tmpfiles
5695          * (since logging them is pointless, a link count of 0 means they
5696          * will never be accessible).
5697          */
5698         if (btrfs_inode_in_log(inode, trans->transid) ||
5699             inode->vfs_inode.i_nlink == 0) {
5700                 ret = BTRFS_NO_LOG_SYNC;
5701                 goto end_no_trans;
5702         }
5703
5704         ret = start_log_trans(trans, root, ctx);
5705         if (ret)
5706                 goto end_no_trans;
5707
5708         ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
5709         if (ret)
5710                 goto end_trans;
5711
5712         /*
5713          * for regular files, if its inode is already on disk, we don't
5714          * have to worry about the parents at all.  This is because
5715          * we can use the last_unlink_trans field to record renames
5716          * and other fun in this file.
5717          */
5718         if (S_ISREG(inode->vfs_inode.i_mode) &&
5719             inode->generation <= last_committed &&
5720             inode->last_unlink_trans <= last_committed) {
5721                 ret = 0;
5722                 goto end_trans;
5723         }
5724
5725         if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
5726                 log_dentries = true;
5727
5728         /*
5729          * On unlink we must make sure all our current and old parent directory
5730          * inodes are fully logged. This is to prevent leaving dangling
5731          * directory index entries in directories that were our parents but are
5732          * not anymore. Not doing this results in old parent directory being
5733          * impossible to delete after log replay (rmdir will always fail with
5734          * error -ENOTEMPTY).
5735          *
5736          * Example 1:
5737          *
5738          * mkdir testdir
5739          * touch testdir/foo
5740          * ln testdir/foo testdir/bar
5741          * sync
5742          * unlink testdir/bar
5743          * xfs_io -c fsync testdir/foo
5744          * <power failure>
5745          * mount fs, triggers log replay
5746          *
5747          * If we don't log the parent directory (testdir), after log replay the
5748          * directory still has an entry pointing to the file inode using the bar
5749          * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5750          * the file inode has a link count of 1.
5751          *
5752          * Example 2:
5753          *
5754          * mkdir testdir
5755          * touch foo
5756          * ln foo testdir/foo2
5757          * ln foo testdir/foo3
5758          * sync
5759          * unlink testdir/foo3
5760          * xfs_io -c fsync foo
5761          * <power failure>
5762          * mount fs, triggers log replay
5763          *
5764          * Similar as the first example, after log replay the parent directory
5765          * testdir still has an entry pointing to the inode file with name foo3
5766          * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5767          * and has a link count of 2.
5768          */
5769         if (inode->last_unlink_trans > last_committed) {
5770                 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5771                 if (ret)
5772                         goto end_trans;
5773         }
5774
5775         /*
5776          * If a new hard link was added to the inode in the current transaction
5777          * and its link count is now greater than 1, we need to fallback to a
5778          * transaction commit, otherwise we can end up not logging all its new
5779          * parents for all the hard links. Here just from the dentry used to
5780          * fsync, we can not visit the ancestor inodes for all the other hard
5781          * links to figure out if any is new, so we fallback to a transaction
5782          * commit (instead of adding a lot of complexity of scanning a btree,
5783          * since this scenario is not a common use case).
5784          */
5785         if (inode->vfs_inode.i_nlink > 1 &&
5786             inode->last_link_trans > last_committed) {
5787                 ret = -EMLINK;
5788                 goto end_trans;
5789         }
5790
5791         while (1) {
5792                 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5793                         break;
5794
5795                 inode = BTRFS_I(d_inode(parent));
5796                 if (root != inode->root)
5797                         break;
5798
5799                 if (inode->generation > last_committed) {
5800                         ret = btrfs_log_inode(trans, root, inode,
5801                                         LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
5802                         if (ret)
5803                                 goto end_trans;
5804                 }
5805                 if (IS_ROOT(parent))
5806                         break;
5807
5808                 parent = dget_parent(parent);
5809                 dput(old_parent);
5810                 old_parent = parent;
5811         }
5812         if (log_dentries)
5813                 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5814         else
5815                 ret = 0;
5816 end_trans:
5817         dput(old_parent);
5818         if (ret < 0) {
5819                 btrfs_set_log_full_commit(fs_info, trans);
5820                 ret = 1;
5821         }
5822
5823         if (ret)
5824                 btrfs_remove_log_ctx(root, ctx);
5825         btrfs_end_log_trans(root);
5826 end_no_trans:
5827         return ret;
5828 }
5829
5830 /*
5831  * it is not safe to log dentry if the chunk root has added new
5832  * chunks.  This returns 0 if the dentry was logged, and 1 otherwise.
5833  * If this returns 1, you must commit the transaction to safely get your
5834  * data on disk.
5835  */
5836 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
5837                           struct dentry *dentry,
5838                           const loff_t start,
5839                           const loff_t end,
5840                           struct btrfs_log_ctx *ctx)
5841 {
5842         struct dentry *parent = dget_parent(dentry);
5843         int ret;
5844
5845         ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5846                                      start, end, LOG_INODE_ALL, ctx);
5847         dput(parent);
5848
5849         return ret;
5850 }
5851
5852 /*
5853  * should be called during mount to recover any replay any log trees
5854  * from the FS
5855  */
5856 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5857 {
5858         int ret;
5859         struct btrfs_path *path;
5860         struct btrfs_trans_handle *trans;
5861         struct btrfs_key key;
5862         struct btrfs_key found_key;
5863         struct btrfs_key tmp_key;
5864         struct btrfs_root *log;
5865         struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5866         struct walk_control wc = {
5867                 .process_func = process_one_buffer,
5868                 .stage = 0,
5869         };
5870
5871         path = btrfs_alloc_path();
5872         if (!path)
5873                 return -ENOMEM;
5874
5875         set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
5876
5877         trans = btrfs_start_transaction(fs_info->tree_root, 0);
5878         if (IS_ERR(trans)) {
5879                 ret = PTR_ERR(trans);
5880                 goto error;
5881         }
5882
5883         wc.trans = trans;
5884         wc.pin = 1;
5885
5886         ret = walk_log_tree(trans, log_root_tree, &wc);
5887         if (ret) {
5888                 btrfs_handle_fs_error(fs_info, ret,
5889                         "Failed to pin buffers while recovering log root tree.");
5890                 goto error;
5891         }
5892
5893 again:
5894         key.objectid = BTRFS_TREE_LOG_OBJECTID;
5895         key.offset = (u64)-1;
5896         key.type = BTRFS_ROOT_ITEM_KEY;
5897
5898         while (1) {
5899                 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
5900
5901                 if (ret < 0) {
5902                         btrfs_handle_fs_error(fs_info, ret,
5903                                     "Couldn't find tree log root.");
5904                         goto error;
5905                 }
5906                 if (ret > 0) {
5907                         if (path->slots[0] == 0)
5908                                 break;
5909                         path->slots[0]--;
5910                 }
5911                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5912                                       path->slots[0]);
5913                 btrfs_release_path(path);
5914                 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5915                         break;
5916
5917                 log = btrfs_read_fs_root(log_root_tree, &found_key);
5918                 if (IS_ERR(log)) {
5919                         ret = PTR_ERR(log);
5920                         btrfs_handle_fs_error(fs_info, ret,
5921                                     "Couldn't read tree log root.");
5922                         goto error;
5923                 }
5924
5925                 tmp_key.objectid = found_key.offset;
5926                 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5927                 tmp_key.offset = (u64)-1;
5928
5929                 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
5930                 if (IS_ERR(wc.replay_dest)) {
5931                         ret = PTR_ERR(wc.replay_dest);
5932
5933                         /*
5934                          * We didn't find the subvol, likely because it was
5935                          * deleted.  This is ok, simply skip this log and go to
5936                          * the next one.
5937                          *
5938                          * We need to exclude the root because we can't have
5939                          * other log replays overwriting this log as we'll read
5940                          * it back in a few more times.  This will keep our
5941                          * block from being modified, and we'll just bail for
5942                          * each subsequent pass.
5943                          */
5944                         if (ret == -ENOENT)
5945                                 ret = btrfs_pin_extent_for_log_replay(fs_info,
5946                                                         log->node->start,
5947                                                         log->node->len);
5948                         free_extent_buffer(log->node);
5949                         free_extent_buffer(log->commit_root);
5950                         kfree(log);
5951
5952                         if (!ret)
5953                                 goto next;
5954                         btrfs_handle_fs_error(fs_info, ret,
5955                                 "Couldn't read target root for tree log recovery.");
5956                         goto error;
5957                 }
5958
5959                 wc.replay_dest->log_root = log;
5960                 btrfs_record_root_in_trans(trans, wc.replay_dest);
5961                 ret = walk_log_tree(trans, log, &wc);
5962
5963                 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5964                         ret = fixup_inode_link_counts(trans, wc.replay_dest,
5965                                                       path);
5966                 }
5967
5968                 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5969                         struct btrfs_root *root = wc.replay_dest;
5970
5971                         btrfs_release_path(path);
5972
5973                         /*
5974                          * We have just replayed everything, and the highest
5975                          * objectid of fs roots probably has changed in case
5976                          * some inode_item's got replayed.
5977                          *
5978                          * root->objectid_mutex is not acquired as log replay
5979                          * could only happen during mount.
5980                          */
5981                         ret = btrfs_find_highest_objectid(root,
5982                                                   &root->highest_objectid);
5983                 }
5984
5985                 wc.replay_dest->log_root = NULL;
5986                 free_extent_buffer(log->node);
5987                 free_extent_buffer(log->commit_root);
5988                 kfree(log);
5989
5990                 if (ret)
5991                         goto error;
5992 next:
5993                 if (found_key.offset == 0)
5994                         break;
5995                 key.offset = found_key.offset - 1;
5996         }
5997         btrfs_release_path(path);
5998
5999         /* step one is to pin it all, step two is to replay just inodes */
6000         if (wc.pin) {
6001                 wc.pin = 0;
6002                 wc.process_func = replay_one_buffer;
6003                 wc.stage = LOG_WALK_REPLAY_INODES;
6004                 goto again;
6005         }
6006         /* step three is to replay everything */
6007         if (wc.stage < LOG_WALK_REPLAY_ALL) {
6008                 wc.stage++;
6009                 goto again;
6010         }
6011
6012         btrfs_free_path(path);
6013
6014         /* step 4: commit the transaction, which also unpins the blocks */
6015         ret = btrfs_commit_transaction(trans);
6016         if (ret)
6017                 return ret;
6018
6019         free_extent_buffer(log_root_tree->node);
6020         log_root_tree->log_root = NULL;
6021         clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
6022         kfree(log_root_tree);
6023
6024         return 0;
6025 error:
6026         if (wc.trans)
6027                 btrfs_end_transaction(wc.trans);
6028         clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
6029         btrfs_free_path(path);
6030         return ret;
6031 }
6032
6033 /*
6034  * there are some corner cases where we want to force a full
6035  * commit instead of allowing a directory to be logged.
6036  *
6037  * They revolve around files there were unlinked from the directory, and
6038  * this function updates the parent directory so that a full commit is
6039  * properly done if it is fsync'd later after the unlinks are done.
6040  *
6041  * Must be called before the unlink operations (updates to the subvolume tree,
6042  * inodes, etc) are done.
6043  */
6044 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
6045                              struct btrfs_inode *dir, struct btrfs_inode *inode,
6046                              int for_rename)
6047 {
6048         /*
6049          * when we're logging a file, if it hasn't been renamed
6050          * or unlinked, and its inode is fully committed on disk,
6051          * we don't have to worry about walking up the directory chain
6052          * to log its parents.
6053          *
6054          * So, we use the last_unlink_trans field to put this transid
6055          * into the file.  When the file is logged we check it and
6056          * don't log the parents if the file is fully on disk.
6057          */
6058         mutex_lock(&inode->log_mutex);
6059         inode->last_unlink_trans = trans->transid;
6060         mutex_unlock(&inode->log_mutex);
6061
6062         /*
6063          * if this directory was already logged any new
6064          * names for this file/dir will get recorded
6065          */
6066         if (dir->logged_trans == trans->transid)
6067                 return;
6068
6069         /*
6070          * if the inode we're about to unlink was logged,
6071          * the log will be properly updated for any new names
6072          */
6073         if (inode->logged_trans == trans->transid)
6074                 return;
6075
6076         /*
6077          * when renaming files across directories, if the directory
6078          * there we're unlinking from gets fsync'd later on, there's
6079          * no way to find the destination directory later and fsync it
6080          * properly.  So, we have to be conservative and force commits
6081          * so the new name gets discovered.
6082          */
6083         if (for_rename)
6084                 goto record;
6085
6086         /* we can safely do the unlink without any special recording */
6087         return;
6088
6089 record:
6090         mutex_lock(&dir->log_mutex);
6091         dir->last_unlink_trans = trans->transid;
6092         mutex_unlock(&dir->log_mutex);
6093 }
6094
6095 /*
6096  * Make sure that if someone attempts to fsync the parent directory of a deleted
6097  * snapshot, it ends up triggering a transaction commit. This is to guarantee
6098  * that after replaying the log tree of the parent directory's root we will not
6099  * see the snapshot anymore and at log replay time we will not see any log tree
6100  * corresponding to the deleted snapshot's root, which could lead to replaying
6101  * it after replaying the log tree of the parent directory (which would replay
6102  * the snapshot delete operation).
6103  *
6104  * Must be called before the actual snapshot destroy operation (updates to the
6105  * parent root and tree of tree roots trees, etc) are done.
6106  */
6107 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
6108                                    struct btrfs_inode *dir)
6109 {
6110         mutex_lock(&dir->log_mutex);
6111         dir->last_unlink_trans = trans->transid;
6112         mutex_unlock(&dir->log_mutex);
6113 }
6114
6115 /*
6116  * Call this after adding a new name for a file and it will properly
6117  * update the log to reflect the new name.
6118  *
6119  * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6120  * true (because it's not used).
6121  *
6122  * Return value depends on whether @sync_log is true or false.
6123  * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6124  *            committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6125  *            otherwise.
6126  * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6127  *             to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6128  *             or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6129  *             committed (without attempting to sync the log).
6130  */
6131 int btrfs_log_new_name(struct btrfs_trans_handle *trans,
6132                         struct btrfs_inode *inode, struct btrfs_inode *old_dir,
6133                         struct dentry *parent,
6134                         bool sync_log, struct btrfs_log_ctx *ctx)
6135 {
6136         struct btrfs_fs_info *fs_info = trans->fs_info;
6137         int ret;
6138
6139         /*
6140          * this will force the logging code to walk the dentry chain
6141          * up for the file
6142          */
6143         if (!S_ISDIR(inode->vfs_inode.i_mode))
6144                 inode->last_unlink_trans = trans->transid;
6145
6146         /*
6147          * if this inode hasn't been logged and directory we're renaming it
6148          * from hasn't been logged, we don't need to log it
6149          */
6150         if (inode->logged_trans <= fs_info->last_trans_committed &&
6151             (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
6152                 return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
6153                         BTRFS_DONT_NEED_LOG_SYNC;
6154
6155         if (sync_log) {
6156                 struct btrfs_log_ctx ctx2;
6157
6158                 btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
6159                 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6160                                              LOG_INODE_EXISTS, &ctx2);
6161                 if (ret == BTRFS_NO_LOG_SYNC)
6162                         return BTRFS_DONT_NEED_TRANS_COMMIT;
6163                 else if (ret)
6164                         return BTRFS_NEED_TRANS_COMMIT;
6165
6166                 ret = btrfs_sync_log(trans, inode->root, &ctx2);
6167                 if (ret)
6168                         return BTRFS_NEED_TRANS_COMMIT;
6169                 return BTRFS_DONT_NEED_TRANS_COMMIT;
6170         }
6171
6172         ASSERT(ctx);
6173         ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6174                                      LOG_INODE_EXISTS, ctx);
6175         if (ret == BTRFS_NO_LOG_SYNC)
6176                 return BTRFS_DONT_NEED_LOG_SYNC;
6177         else if (ret)
6178                 return BTRFS_NEED_TRANS_COMMIT;
6179
6180         return BTRFS_NEED_LOG_SYNC;
6181 }
6182