Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include "tree-mod-log.h"
0004 #include "disk-io.h"
0005 
0006 struct tree_mod_root {
0007     u64 logical;
0008     u8 level;
0009 };
0010 
0011 struct tree_mod_elem {
0012     struct rb_node node;
0013     u64 logical;
0014     u64 seq;
0015     enum btrfs_mod_log_op op;
0016 
0017     /*
0018      * This is used for BTRFS_MOD_LOG_KEY_* and BTRFS_MOD_LOG_MOVE_KEYS
0019      * operations.
0020      */
0021     int slot;
0022 
0023     /* This is used for BTRFS_MOD_LOG_KEY* and BTRFS_MOD_LOG_ROOT_REPLACE. */
0024     u64 generation;
0025 
0026     /* Those are used for op == BTRFS_MOD_LOG_KEY_{REPLACE,REMOVE}. */
0027     struct btrfs_disk_key key;
0028     u64 blockptr;
0029 
0030     /* This is used for op == BTRFS_MOD_LOG_MOVE_KEYS. */
0031     struct {
0032         int dst_slot;
0033         int nr_items;
0034     } move;
0035 
0036     /* This is used for op == BTRFS_MOD_LOG_ROOT_REPLACE. */
0037     struct tree_mod_root old_root;
0038 };
0039 
0040 /*
0041  * Pull a new tree mod seq number for our operation.
0042  */
0043 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
0044 {
0045     return atomic64_inc_return(&fs_info->tree_mod_seq);
0046 }
0047 
0048 /*
0049  * This adds a new blocker to the tree mod log's blocker list if the @elem
0050  * passed does not already have a sequence number set. So when a caller expects
0051  * to record tree modifications, it should ensure to set elem->seq to zero
0052  * before calling btrfs_get_tree_mod_seq.
0053  * Returns a fresh, unused tree log modification sequence number, even if no new
0054  * blocker was added.
0055  */
0056 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
0057                struct btrfs_seq_list *elem)
0058 {
0059     write_lock(&fs_info->tree_mod_log_lock);
0060     if (!elem->seq) {
0061         elem->seq = btrfs_inc_tree_mod_seq(fs_info);
0062         list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
0063         set_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
0064     }
0065     write_unlock(&fs_info->tree_mod_log_lock);
0066 
0067     return elem->seq;
0068 }
0069 
0070 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
0071                 struct btrfs_seq_list *elem)
0072 {
0073     struct rb_root *tm_root;
0074     struct rb_node *node;
0075     struct rb_node *next;
0076     struct tree_mod_elem *tm;
0077     u64 min_seq = BTRFS_SEQ_LAST;
0078     u64 seq_putting = elem->seq;
0079 
0080     if (!seq_putting)
0081         return;
0082 
0083     write_lock(&fs_info->tree_mod_log_lock);
0084     list_del(&elem->list);
0085     elem->seq = 0;
0086 
0087     if (list_empty(&fs_info->tree_mod_seq_list)) {
0088         clear_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
0089     } else {
0090         struct btrfs_seq_list *first;
0091 
0092         first = list_first_entry(&fs_info->tree_mod_seq_list,
0093                      struct btrfs_seq_list, list);
0094         if (seq_putting > first->seq) {
0095             /*
0096              * Blocker with lower sequence number exists, we cannot
0097              * remove anything from the log.
0098              */
0099             write_unlock(&fs_info->tree_mod_log_lock);
0100             return;
0101         }
0102         min_seq = first->seq;
0103     }
0104 
0105     /*
0106      * Anything that's lower than the lowest existing (read: blocked)
0107      * sequence number can be removed from the tree.
0108      */
0109     tm_root = &fs_info->tree_mod_log;
0110     for (node = rb_first(tm_root); node; node = next) {
0111         next = rb_next(node);
0112         tm = rb_entry(node, struct tree_mod_elem, node);
0113         if (tm->seq >= min_seq)
0114             continue;
0115         rb_erase(node, tm_root);
0116         kfree(tm);
0117     }
0118     write_unlock(&fs_info->tree_mod_log_lock);
0119 }
0120 
0121 /*
0122  * Key order of the log:
0123  *       node/leaf start address -> sequence
0124  *
0125  * The 'start address' is the logical address of the *new* root node for root
0126  * replace operations, or the logical address of the affected block for all
0127  * other operations.
0128  */
0129 static noinline int tree_mod_log_insert(struct btrfs_fs_info *fs_info,
0130                     struct tree_mod_elem *tm)
0131 {
0132     struct rb_root *tm_root;
0133     struct rb_node **new;
0134     struct rb_node *parent = NULL;
0135     struct tree_mod_elem *cur;
0136 
0137     lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
0138 
0139     tm->seq = btrfs_inc_tree_mod_seq(fs_info);
0140 
0141     tm_root = &fs_info->tree_mod_log;
0142     new = &tm_root->rb_node;
0143     while (*new) {
0144         cur = rb_entry(*new, struct tree_mod_elem, node);
0145         parent = *new;
0146         if (cur->logical < tm->logical)
0147             new = &((*new)->rb_left);
0148         else if (cur->logical > tm->logical)
0149             new = &((*new)->rb_right);
0150         else if (cur->seq < tm->seq)
0151             new = &((*new)->rb_left);
0152         else if (cur->seq > tm->seq)
0153             new = &((*new)->rb_right);
0154         else
0155             return -EEXIST;
0156     }
0157 
0158     rb_link_node(&tm->node, parent, new);
0159     rb_insert_color(&tm->node, tm_root);
0160     return 0;
0161 }
0162 
0163 /*
0164  * Determines if logging can be omitted. Returns true if it can. Otherwise, it
0165  * returns false with the tree_mod_log_lock acquired. The caller must hold
0166  * this until all tree mod log insertions are recorded in the rb tree and then
0167  * write unlock fs_info::tree_mod_log_lock.
0168  */
0169 static inline bool tree_mod_dont_log(struct btrfs_fs_info *fs_info,
0170                     struct extent_buffer *eb)
0171 {
0172     if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
0173         return true;
0174     if (eb && btrfs_header_level(eb) == 0)
0175         return true;
0176 
0177     write_lock(&fs_info->tree_mod_log_lock);
0178     if (list_empty(&(fs_info)->tree_mod_seq_list)) {
0179         write_unlock(&fs_info->tree_mod_log_lock);
0180         return true;
0181     }
0182 
0183     return false;
0184 }
0185 
0186 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
0187 static inline bool tree_mod_need_log(const struct btrfs_fs_info *fs_info,
0188                     struct extent_buffer *eb)
0189 {
0190     if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
0191         return false;
0192     if (eb && btrfs_header_level(eb) == 0)
0193         return false;
0194 
0195     return true;
0196 }
0197 
0198 static struct tree_mod_elem *alloc_tree_mod_elem(struct extent_buffer *eb,
0199                          int slot,
0200                          enum btrfs_mod_log_op op,
0201                          gfp_t flags)
0202 {
0203     struct tree_mod_elem *tm;
0204 
0205     tm = kzalloc(sizeof(*tm), flags);
0206     if (!tm)
0207         return NULL;
0208 
0209     tm->logical = eb->start;
0210     if (op != BTRFS_MOD_LOG_KEY_ADD) {
0211         btrfs_node_key(eb, &tm->key, slot);
0212         tm->blockptr = btrfs_node_blockptr(eb, slot);
0213     }
0214     tm->op = op;
0215     tm->slot = slot;
0216     tm->generation = btrfs_node_ptr_generation(eb, slot);
0217     RB_CLEAR_NODE(&tm->node);
0218 
0219     return tm;
0220 }
0221 
0222 int btrfs_tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
0223                   enum btrfs_mod_log_op op, gfp_t flags)
0224 {
0225     struct tree_mod_elem *tm;
0226     int ret;
0227 
0228     if (!tree_mod_need_log(eb->fs_info, eb))
0229         return 0;
0230 
0231     tm = alloc_tree_mod_elem(eb, slot, op, flags);
0232     if (!tm)
0233         return -ENOMEM;
0234 
0235     if (tree_mod_dont_log(eb->fs_info, eb)) {
0236         kfree(tm);
0237         return 0;
0238     }
0239 
0240     ret = tree_mod_log_insert(eb->fs_info, tm);
0241     write_unlock(&eb->fs_info->tree_mod_log_lock);
0242     if (ret)
0243         kfree(tm);
0244 
0245     return ret;
0246 }
0247 
0248 int btrfs_tree_mod_log_insert_move(struct extent_buffer *eb,
0249                    int dst_slot, int src_slot,
0250                    int nr_items)
0251 {
0252     struct tree_mod_elem *tm = NULL;
0253     struct tree_mod_elem **tm_list = NULL;
0254     int ret = 0;
0255     int i;
0256     bool locked = false;
0257 
0258     if (!tree_mod_need_log(eb->fs_info, eb))
0259         return 0;
0260 
0261     tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
0262     if (!tm_list)
0263         return -ENOMEM;
0264 
0265     tm = kzalloc(sizeof(*tm), GFP_NOFS);
0266     if (!tm) {
0267         ret = -ENOMEM;
0268         goto free_tms;
0269     }
0270 
0271     tm->logical = eb->start;
0272     tm->slot = src_slot;
0273     tm->move.dst_slot = dst_slot;
0274     tm->move.nr_items = nr_items;
0275     tm->op = BTRFS_MOD_LOG_MOVE_KEYS;
0276 
0277     for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
0278         tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
0279                 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
0280         if (!tm_list[i]) {
0281             ret = -ENOMEM;
0282             goto free_tms;
0283         }
0284     }
0285 
0286     if (tree_mod_dont_log(eb->fs_info, eb))
0287         goto free_tms;
0288     locked = true;
0289 
0290     /*
0291      * When we override something during the move, we log these removals.
0292      * This can only happen when we move towards the beginning of the
0293      * buffer, i.e. dst_slot < src_slot.
0294      */
0295     for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
0296         ret = tree_mod_log_insert(eb->fs_info, tm_list[i]);
0297         if (ret)
0298             goto free_tms;
0299     }
0300 
0301     ret = tree_mod_log_insert(eb->fs_info, tm);
0302     if (ret)
0303         goto free_tms;
0304     write_unlock(&eb->fs_info->tree_mod_log_lock);
0305     kfree(tm_list);
0306 
0307     return 0;
0308 
0309 free_tms:
0310     for (i = 0; i < nr_items; i++) {
0311         if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
0312             rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
0313         kfree(tm_list[i]);
0314     }
0315     if (locked)
0316         write_unlock(&eb->fs_info->tree_mod_log_lock);
0317     kfree(tm_list);
0318     kfree(tm);
0319 
0320     return ret;
0321 }
0322 
0323 static inline int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
0324                        struct tree_mod_elem **tm_list,
0325                        int nritems)
0326 {
0327     int i, j;
0328     int ret;
0329 
0330     for (i = nritems - 1; i >= 0; i--) {
0331         ret = tree_mod_log_insert(fs_info, tm_list[i]);
0332         if (ret) {
0333             for (j = nritems - 1; j > i; j--)
0334                 rb_erase(&tm_list[j]->node,
0335                      &fs_info->tree_mod_log);
0336             return ret;
0337         }
0338     }
0339 
0340     return 0;
0341 }
0342 
0343 int btrfs_tree_mod_log_insert_root(struct extent_buffer *old_root,
0344                    struct extent_buffer *new_root,
0345                    bool log_removal)
0346 {
0347     struct btrfs_fs_info *fs_info = old_root->fs_info;
0348     struct tree_mod_elem *tm = NULL;
0349     struct tree_mod_elem **tm_list = NULL;
0350     int nritems = 0;
0351     int ret = 0;
0352     int i;
0353 
0354     if (!tree_mod_need_log(fs_info, NULL))
0355         return 0;
0356 
0357     if (log_removal && btrfs_header_level(old_root) > 0) {
0358         nritems = btrfs_header_nritems(old_root);
0359         tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
0360                   GFP_NOFS);
0361         if (!tm_list) {
0362             ret = -ENOMEM;
0363             goto free_tms;
0364         }
0365         for (i = 0; i < nritems; i++) {
0366             tm_list[i] = alloc_tree_mod_elem(old_root, i,
0367                 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
0368             if (!tm_list[i]) {
0369                 ret = -ENOMEM;
0370                 goto free_tms;
0371             }
0372         }
0373     }
0374 
0375     tm = kzalloc(sizeof(*tm), GFP_NOFS);
0376     if (!tm) {
0377         ret = -ENOMEM;
0378         goto free_tms;
0379     }
0380 
0381     tm->logical = new_root->start;
0382     tm->old_root.logical = old_root->start;
0383     tm->old_root.level = btrfs_header_level(old_root);
0384     tm->generation = btrfs_header_generation(old_root);
0385     tm->op = BTRFS_MOD_LOG_ROOT_REPLACE;
0386 
0387     if (tree_mod_dont_log(fs_info, NULL))
0388         goto free_tms;
0389 
0390     if (tm_list)
0391         ret = tree_mod_log_free_eb(fs_info, tm_list, nritems);
0392     if (!ret)
0393         ret = tree_mod_log_insert(fs_info, tm);
0394 
0395     write_unlock(&fs_info->tree_mod_log_lock);
0396     if (ret)
0397         goto free_tms;
0398     kfree(tm_list);
0399 
0400     return ret;
0401 
0402 free_tms:
0403     if (tm_list) {
0404         for (i = 0; i < nritems; i++)
0405             kfree(tm_list[i]);
0406         kfree(tm_list);
0407     }
0408     kfree(tm);
0409 
0410     return ret;
0411 }
0412 
0413 static struct tree_mod_elem *__tree_mod_log_search(struct btrfs_fs_info *fs_info,
0414                            u64 start, u64 min_seq,
0415                            bool smallest)
0416 {
0417     struct rb_root *tm_root;
0418     struct rb_node *node;
0419     struct tree_mod_elem *cur = NULL;
0420     struct tree_mod_elem *found = NULL;
0421 
0422     read_lock(&fs_info->tree_mod_log_lock);
0423     tm_root = &fs_info->tree_mod_log;
0424     node = tm_root->rb_node;
0425     while (node) {
0426         cur = rb_entry(node, struct tree_mod_elem, node);
0427         if (cur->logical < start) {
0428             node = node->rb_left;
0429         } else if (cur->logical > start) {
0430             node = node->rb_right;
0431         } else if (cur->seq < min_seq) {
0432             node = node->rb_left;
0433         } else if (!smallest) {
0434             /* We want the node with the highest seq */
0435             if (found)
0436                 BUG_ON(found->seq > cur->seq);
0437             found = cur;
0438             node = node->rb_left;
0439         } else if (cur->seq > min_seq) {
0440             /* We want the node with the smallest seq */
0441             if (found)
0442                 BUG_ON(found->seq < cur->seq);
0443             found = cur;
0444             node = node->rb_right;
0445         } else {
0446             found = cur;
0447             break;
0448         }
0449     }
0450     read_unlock(&fs_info->tree_mod_log_lock);
0451 
0452     return found;
0453 }
0454 
0455 /*
0456  * This returns the element from the log with the smallest time sequence
0457  * value that's in the log (the oldest log item). Any element with a time
0458  * sequence lower than min_seq will be ignored.
0459  */
0460 static struct tree_mod_elem *tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info,
0461                             u64 start, u64 min_seq)
0462 {
0463     return __tree_mod_log_search(fs_info, start, min_seq, true);
0464 }
0465 
0466 /*
0467  * This returns the element from the log with the largest time sequence
0468  * value that's in the log (the most recent log item). Any element with
0469  * a time sequence lower than min_seq will be ignored.
0470  */
0471 static struct tree_mod_elem *tree_mod_log_search(struct btrfs_fs_info *fs_info,
0472                          u64 start, u64 min_seq)
0473 {
0474     return __tree_mod_log_search(fs_info, start, min_seq, false);
0475 }
0476 
0477 int btrfs_tree_mod_log_eb_copy(struct extent_buffer *dst,
0478                    struct extent_buffer *src,
0479                    unsigned long dst_offset,
0480                    unsigned long src_offset,
0481                    int nr_items)
0482 {
0483     struct btrfs_fs_info *fs_info = dst->fs_info;
0484     int ret = 0;
0485     struct tree_mod_elem **tm_list = NULL;
0486     struct tree_mod_elem **tm_list_add, **tm_list_rem;
0487     int i;
0488     bool locked = false;
0489 
0490     if (!tree_mod_need_log(fs_info, NULL))
0491         return 0;
0492 
0493     if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
0494         return 0;
0495 
0496     tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
0497               GFP_NOFS);
0498     if (!tm_list)
0499         return -ENOMEM;
0500 
0501     tm_list_add = tm_list;
0502     tm_list_rem = tm_list + nr_items;
0503     for (i = 0; i < nr_items; i++) {
0504         tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
0505             BTRFS_MOD_LOG_KEY_REMOVE, GFP_NOFS);
0506         if (!tm_list_rem[i]) {
0507             ret = -ENOMEM;
0508             goto free_tms;
0509         }
0510 
0511         tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
0512                         BTRFS_MOD_LOG_KEY_ADD, GFP_NOFS);
0513         if (!tm_list_add[i]) {
0514             ret = -ENOMEM;
0515             goto free_tms;
0516         }
0517     }
0518 
0519     if (tree_mod_dont_log(fs_info, NULL))
0520         goto free_tms;
0521     locked = true;
0522 
0523     for (i = 0; i < nr_items; i++) {
0524         ret = tree_mod_log_insert(fs_info, tm_list_rem[i]);
0525         if (ret)
0526             goto free_tms;
0527         ret = tree_mod_log_insert(fs_info, tm_list_add[i]);
0528         if (ret)
0529             goto free_tms;
0530     }
0531 
0532     write_unlock(&fs_info->tree_mod_log_lock);
0533     kfree(tm_list);
0534 
0535     return 0;
0536 
0537 free_tms:
0538     for (i = 0; i < nr_items * 2; i++) {
0539         if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
0540             rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
0541         kfree(tm_list[i]);
0542     }
0543     if (locked)
0544         write_unlock(&fs_info->tree_mod_log_lock);
0545     kfree(tm_list);
0546 
0547     return ret;
0548 }
0549 
0550 int btrfs_tree_mod_log_free_eb(struct extent_buffer *eb)
0551 {
0552     struct tree_mod_elem **tm_list = NULL;
0553     int nritems = 0;
0554     int i;
0555     int ret = 0;
0556 
0557     if (!tree_mod_need_log(eb->fs_info, eb))
0558         return 0;
0559 
0560     nritems = btrfs_header_nritems(eb);
0561     tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
0562     if (!tm_list)
0563         return -ENOMEM;
0564 
0565     for (i = 0; i < nritems; i++) {
0566         tm_list[i] = alloc_tree_mod_elem(eb, i,
0567             BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
0568         if (!tm_list[i]) {
0569             ret = -ENOMEM;
0570             goto free_tms;
0571         }
0572     }
0573 
0574     if (tree_mod_dont_log(eb->fs_info, eb))
0575         goto free_tms;
0576 
0577     ret = tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
0578     write_unlock(&eb->fs_info->tree_mod_log_lock);
0579     if (ret)
0580         goto free_tms;
0581     kfree(tm_list);
0582 
0583     return 0;
0584 
0585 free_tms:
0586     for (i = 0; i < nritems; i++)
0587         kfree(tm_list[i]);
0588     kfree(tm_list);
0589 
0590     return ret;
0591 }
0592 
0593 /*
0594  * Returns the logical address of the oldest predecessor of the given root.
0595  * Entries older than time_seq are ignored.
0596  */
0597 static struct tree_mod_elem *tree_mod_log_oldest_root(struct extent_buffer *eb_root,
0598                               u64 time_seq)
0599 {
0600     struct tree_mod_elem *tm;
0601     struct tree_mod_elem *found = NULL;
0602     u64 root_logical = eb_root->start;
0603     bool looped = false;
0604 
0605     if (!time_seq)
0606         return NULL;
0607 
0608     /*
0609      * The very last operation that's logged for a root is the replacement
0610      * operation (if it is replaced at all). This has the logical address
0611      * of the *new* root, making it the very first operation that's logged
0612      * for this root.
0613      */
0614     while (1) {
0615         tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
0616                         time_seq);
0617         if (!looped && !tm)
0618             return NULL;
0619         /*
0620          * If there are no tree operation for the oldest root, we simply
0621          * return it. This should only happen if that (old) root is at
0622          * level 0.
0623          */
0624         if (!tm)
0625             break;
0626 
0627         /*
0628          * If there's an operation that's not a root replacement, we
0629          * found the oldest version of our root. Normally, we'll find a
0630          * BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
0631          */
0632         if (tm->op != BTRFS_MOD_LOG_ROOT_REPLACE)
0633             break;
0634 
0635         found = tm;
0636         root_logical = tm->old_root.logical;
0637         looped = true;
0638     }
0639 
0640     /* If there's no old root to return, return what we found instead */
0641     if (!found)
0642         found = tm;
0643 
0644     return found;
0645 }
0646 
0647 
0648 /*
0649  * tm is a pointer to the first operation to rewind within eb. Then, all
0650  * previous operations will be rewound (until we reach something older than
0651  * time_seq).
0652  */
0653 static void tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
0654                 struct extent_buffer *eb,
0655                 u64 time_seq,
0656                 struct tree_mod_elem *first_tm)
0657 {
0658     u32 n;
0659     struct rb_node *next;
0660     struct tree_mod_elem *tm = first_tm;
0661     unsigned long o_dst;
0662     unsigned long o_src;
0663     unsigned long p_size = sizeof(struct btrfs_key_ptr);
0664 
0665     n = btrfs_header_nritems(eb);
0666     read_lock(&fs_info->tree_mod_log_lock);
0667     while (tm && tm->seq >= time_seq) {
0668         /*
0669          * All the operations are recorded with the operator used for
0670          * the modification. As we're going backwards, we do the
0671          * opposite of each operation here.
0672          */
0673         switch (tm->op) {
0674         case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING:
0675             BUG_ON(tm->slot < n);
0676             fallthrough;
0677         case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING:
0678         case BTRFS_MOD_LOG_KEY_REMOVE:
0679             btrfs_set_node_key(eb, &tm->key, tm->slot);
0680             btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
0681             btrfs_set_node_ptr_generation(eb, tm->slot,
0682                               tm->generation);
0683             n++;
0684             break;
0685         case BTRFS_MOD_LOG_KEY_REPLACE:
0686             BUG_ON(tm->slot >= n);
0687             btrfs_set_node_key(eb, &tm->key, tm->slot);
0688             btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
0689             btrfs_set_node_ptr_generation(eb, tm->slot,
0690                               tm->generation);
0691             break;
0692         case BTRFS_MOD_LOG_KEY_ADD:
0693             /* if a move operation is needed it's in the log */
0694             n--;
0695             break;
0696         case BTRFS_MOD_LOG_MOVE_KEYS:
0697             o_dst = btrfs_node_key_ptr_offset(tm->slot);
0698             o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
0699             memmove_extent_buffer(eb, o_dst, o_src,
0700                           tm->move.nr_items * p_size);
0701             break;
0702         case BTRFS_MOD_LOG_ROOT_REPLACE:
0703             /*
0704              * This operation is special. For roots, this must be
0705              * handled explicitly before rewinding.
0706              * For non-roots, this operation may exist if the node
0707              * was a root: root A -> child B; then A gets empty and
0708              * B is promoted to the new root. In the mod log, we'll
0709              * have a root-replace operation for B, a tree block
0710              * that is no root. We simply ignore that operation.
0711              */
0712             break;
0713         }
0714         next = rb_next(&tm->node);
0715         if (!next)
0716             break;
0717         tm = rb_entry(next, struct tree_mod_elem, node);
0718         if (tm->logical != first_tm->logical)
0719             break;
0720     }
0721     read_unlock(&fs_info->tree_mod_log_lock);
0722     btrfs_set_header_nritems(eb, n);
0723 }
0724 
0725 /*
0726  * Called with eb read locked. If the buffer cannot be rewound, the same buffer
0727  * is returned. If rewind operations happen, a fresh buffer is returned. The
0728  * returned buffer is always read-locked. If the returned buffer is not the
0729  * input buffer, the lock on the input buffer is released and the input buffer
0730  * is freed (its refcount is decremented).
0731  */
0732 struct extent_buffer *btrfs_tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
0733                         struct btrfs_path *path,
0734                         struct extent_buffer *eb,
0735                         u64 time_seq)
0736 {
0737     struct extent_buffer *eb_rewin;
0738     struct tree_mod_elem *tm;
0739 
0740     if (!time_seq)
0741         return eb;
0742 
0743     if (btrfs_header_level(eb) == 0)
0744         return eb;
0745 
0746     tm = tree_mod_log_search(fs_info, eb->start, time_seq);
0747     if (!tm)
0748         return eb;
0749 
0750     if (tm->op == BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
0751         BUG_ON(tm->slot != 0);
0752         eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
0753         if (!eb_rewin) {
0754             btrfs_tree_read_unlock(eb);
0755             free_extent_buffer(eb);
0756             return NULL;
0757         }
0758         btrfs_set_header_bytenr(eb_rewin, eb->start);
0759         btrfs_set_header_backref_rev(eb_rewin,
0760                          btrfs_header_backref_rev(eb));
0761         btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
0762         btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
0763     } else {
0764         eb_rewin = btrfs_clone_extent_buffer(eb);
0765         if (!eb_rewin) {
0766             btrfs_tree_read_unlock(eb);
0767             free_extent_buffer(eb);
0768             return NULL;
0769         }
0770     }
0771 
0772     btrfs_tree_read_unlock(eb);
0773     free_extent_buffer(eb);
0774 
0775     btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
0776                        eb_rewin, btrfs_header_level(eb_rewin));
0777     btrfs_tree_read_lock(eb_rewin);
0778     tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
0779     WARN_ON(btrfs_header_nritems(eb_rewin) >
0780         BTRFS_NODEPTRS_PER_BLOCK(fs_info));
0781 
0782     return eb_rewin;
0783 }
0784 
0785 /*
0786  * Rewind the state of @root's root node to the given @time_seq value.
0787  * If there are no changes, the current root->root_node is returned. If anything
0788  * changed in between, there's a fresh buffer allocated on which the rewind
0789  * operations are done. In any case, the returned buffer is read locked.
0790  * Returns NULL on error (with no locks held).
0791  */
0792 struct extent_buffer *btrfs_get_old_root(struct btrfs_root *root, u64 time_seq)
0793 {
0794     struct btrfs_fs_info *fs_info = root->fs_info;
0795     struct tree_mod_elem *tm;
0796     struct extent_buffer *eb = NULL;
0797     struct extent_buffer *eb_root;
0798     u64 eb_root_owner = 0;
0799     struct extent_buffer *old;
0800     struct tree_mod_root *old_root = NULL;
0801     u64 old_generation = 0;
0802     u64 logical;
0803     int level;
0804 
0805     eb_root = btrfs_read_lock_root_node(root);
0806     tm = tree_mod_log_oldest_root(eb_root, time_seq);
0807     if (!tm)
0808         return eb_root;
0809 
0810     if (tm->op == BTRFS_MOD_LOG_ROOT_REPLACE) {
0811         old_root = &tm->old_root;
0812         old_generation = tm->generation;
0813         logical = old_root->logical;
0814         level = old_root->level;
0815     } else {
0816         logical = eb_root->start;
0817         level = btrfs_header_level(eb_root);
0818     }
0819 
0820     tm = tree_mod_log_search(fs_info, logical, time_seq);
0821     if (old_root && tm && tm->op != BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
0822         btrfs_tree_read_unlock(eb_root);
0823         free_extent_buffer(eb_root);
0824         old = read_tree_block(fs_info, logical, root->root_key.objectid,
0825                       0, level, NULL);
0826         if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
0827             if (!IS_ERR(old))
0828                 free_extent_buffer(old);
0829             btrfs_warn(fs_info,
0830                    "failed to read tree block %llu from get_old_root",
0831                    logical);
0832         } else {
0833             struct tree_mod_elem *tm2;
0834 
0835             btrfs_tree_read_lock(old);
0836             eb = btrfs_clone_extent_buffer(old);
0837             /*
0838              * After the lookup for the most recent tree mod operation
0839              * above and before we locked and cloned the extent buffer
0840              * 'old', a new tree mod log operation may have been added.
0841              * So lookup for a more recent one to make sure the number
0842              * of mod log operations we replay is consistent with the
0843              * number of items we have in the cloned extent buffer,
0844              * otherwise we can hit a BUG_ON when rewinding the extent
0845              * buffer.
0846              */
0847             tm2 = tree_mod_log_search(fs_info, logical, time_seq);
0848             btrfs_tree_read_unlock(old);
0849             free_extent_buffer(old);
0850             ASSERT(tm2);
0851             ASSERT(tm2 == tm || tm2->seq > tm->seq);
0852             if (!tm2 || tm2->seq < tm->seq) {
0853                 free_extent_buffer(eb);
0854                 return NULL;
0855             }
0856             tm = tm2;
0857         }
0858     } else if (old_root) {
0859         eb_root_owner = btrfs_header_owner(eb_root);
0860         btrfs_tree_read_unlock(eb_root);
0861         free_extent_buffer(eb_root);
0862         eb = alloc_dummy_extent_buffer(fs_info, logical);
0863     } else {
0864         eb = btrfs_clone_extent_buffer(eb_root);
0865         btrfs_tree_read_unlock(eb_root);
0866         free_extent_buffer(eb_root);
0867     }
0868 
0869     if (!eb)
0870         return NULL;
0871     if (old_root) {
0872         btrfs_set_header_bytenr(eb, eb->start);
0873         btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
0874         btrfs_set_header_owner(eb, eb_root_owner);
0875         btrfs_set_header_level(eb, old_root->level);
0876         btrfs_set_header_generation(eb, old_generation);
0877     }
0878     btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
0879                        btrfs_header_level(eb));
0880     btrfs_tree_read_lock(eb);
0881     if (tm)
0882         tree_mod_log_rewind(fs_info, eb, time_seq, tm);
0883     else
0884         WARN_ON(btrfs_header_level(eb) != 0);
0885     WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
0886 
0887     return eb;
0888 }
0889 
0890 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
0891 {
0892     struct tree_mod_elem *tm;
0893     int level;
0894     struct extent_buffer *eb_root = btrfs_root_node(root);
0895 
0896     tm = tree_mod_log_oldest_root(eb_root, time_seq);
0897     if (tm && tm->op == BTRFS_MOD_LOG_ROOT_REPLACE)
0898         level = tm->old_root.level;
0899     else
0900         level = btrfs_header_level(eb_root);
0901 
0902     free_extent_buffer(eb_root);
0903 
0904     return level;
0905 }
0906 
0907 /*
0908  * Return the lowest sequence number in the tree modification log.
0909  *
0910  * Return the sequence number of the oldest tree modification log user, which
0911  * corresponds to the lowest sequence number of all existing users. If there are
0912  * no users it returns 0.
0913  */
0914 u64 btrfs_tree_mod_log_lowest_seq(struct btrfs_fs_info *fs_info)
0915 {
0916     u64 ret = 0;
0917 
0918     read_lock(&fs_info->tree_mod_log_lock);
0919     if (!list_empty(&fs_info->tree_mod_seq_list)) {
0920         struct btrfs_seq_list *elem;
0921 
0922         elem = list_first_entry(&fs_info->tree_mod_seq_list,
0923                     struct btrfs_seq_list, list);
0924         ret = elem->seq;
0925     }
0926     read_unlock(&fs_info->tree_mod_log_lock);
0927 
0928     return ret;
0929 }