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