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