^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2007 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "extent_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "delalloc-space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct kmem_cache *btrfs_ordered_extent_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static u64 entry_end(struct btrfs_ordered_extent *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (entry->file_offset + entry->num_bytes < entry->file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return entry->file_offset + entry->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* returns NULL if the insertion worked, or it returns the node it did find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct rb_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct rb_node **p = &root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct btrfs_ordered_extent *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (file_offset < entry->file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) else if (file_offset >= entry_end(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) rb_link_node(node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) rb_insert_color(node, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * look for a given offset in the tree, and if it can't be found return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * first lesser offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct rb_node **prev_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct rb_node *n = root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct rb_node *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct rb_node *test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct btrfs_ordered_extent *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct btrfs_ordered_extent *prev_entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) prev = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) prev_entry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (file_offset < entry->file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) else if (file_offset >= entry_end(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!prev_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) while (prev && file_offset >= entry_end(prev_entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) test = rb_next(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) prev_entry = rb_entry(test, struct btrfs_ordered_extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (file_offset < entry_end(prev_entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) prev = test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) while (prev && file_offset < entry_end(prev_entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) test = rb_prev(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) prev_entry = rb_entry(test, struct btrfs_ordered_extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) prev = test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *prev_ret = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * helper to check if a given offset is inside a given entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (file_offset < entry->file_offset ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) entry->file_offset + entry->num_bytes <= file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (file_offset + len <= entry->file_offset ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) entry->file_offset + entry->num_bytes <= file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * look find the first ordered struct that has this offset, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * the first one less than this offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) u64 file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct rb_root *root = &tree->tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct rb_node *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct rb_node *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct btrfs_ordered_extent *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (tree->last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) entry = rb_entry(tree->last, struct btrfs_ordered_extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (offset_in_entry(entry, file_offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return tree->last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret = __tree_search(root, file_offset, &prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) tree->last = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Allocate and add a new ordered_extent into the per-inode tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * The tree is given a single reference on the ordered extent that was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * inserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int __btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u64 disk_bytenr, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u64 disk_num_bytes, int type, int dio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int compress_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct btrfs_ordered_extent *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (type == BTRFS_ORDERED_NOCOW || type == BTRFS_ORDERED_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /* For nocow write, we can release the qgroup rsv right now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * The ordered extent has reserved qgroup space, release now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * and pass the reserved number for qgroup_record to free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = btrfs_qgroup_release_data(inode, file_offset, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) entry->file_offset = file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) entry->disk_bytenr = disk_bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) entry->num_bytes = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) entry->disk_num_bytes = disk_num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) entry->bytes_left = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) entry->inode = igrab(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) entry->compress_type = compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) entry->truncated_len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) entry->qgroup_rsv = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) set_bit(type, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (dio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) percpu_counter_add_batch(&fs_info->dio_bytes, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) fs_info->delalloc_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* one ref for the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) refcount_set(&entry->refs, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) init_waitqueue_head(&entry->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) INIT_LIST_HEAD(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) INIT_LIST_HEAD(&entry->log_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) INIT_LIST_HEAD(&entry->root_extent_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) INIT_LIST_HEAD(&entry->work_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) init_completion(&entry->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) trace_btrfs_ordered_extent_add(inode, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) node = tree_insert(&tree->tree, file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) &entry->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) btrfs_panic(fs_info, -EEXIST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) "inconsistency in ordered tree at offset %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) spin_lock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) list_add_tail(&entry->root_extent_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) &root->ordered_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) root->nr_ordered_extents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (root->nr_ordered_extents == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) spin_lock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) BUG_ON(!list_empty(&root->ordered_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) spin_unlock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) spin_unlock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * We don't need the count_max_extents here, we can assume that all of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * that work has been done at higher layers, so this is truly the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * smallest the extent is going to get.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) spin_lock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) btrfs_mod_outstanding_extents(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) spin_unlock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) num_bytes, disk_num_bytes, type, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) BTRFS_COMPRESS_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) int btrfs_add_ordered_extent_dio(struct btrfs_inode *inode, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) u64 disk_bytenr, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) u64 disk_num_bytes, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) num_bytes, disk_num_bytes, type, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) BTRFS_COMPRESS_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int btrfs_add_ordered_extent_compress(struct btrfs_inode *inode, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) u64 disk_bytenr, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) u64 disk_num_bytes, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int compress_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) num_bytes, disk_num_bytes, type, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * when an ordered extent is finished. If the list covers more than one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * ordered extent, it is split across multiples.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct btrfs_ordered_sum *sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct btrfs_ordered_inode_tree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) tree = &BTRFS_I(entry->inode)->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) list_add_tail(&sum->list, &entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * this is used to account for finished IO across a given range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * of the file. The IO may span ordered extents. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * a given ordered_extent is completely done, 1 is returned, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * to make sure this function only returns 1 once for a given ordered extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * file_offset is updated to one byte past the range that is recorded as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * complete. This allows you to walk forward in the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct btrfs_ordered_extent **cached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) u64 *file_offset, u64 io_size, int uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct btrfs_ordered_extent *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) u64 dec_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) u64 dec_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) u64 to_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) spin_lock_irqsave(&tree->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) node = tree_search(tree, *file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!offset_in_entry(entry, *file_offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) dec_start = max(*file_offset, entry->file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) dec_end = min(*file_offset + io_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) entry->file_offset + entry->num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) *file_offset = dec_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (dec_start > dec_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) btrfs_crit(fs_info, "bad ordering dec_start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) dec_start, dec_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) to_dec = dec_end - dec_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (to_dec > entry->bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) "bad ordered accounting left %llu size %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) entry->bytes_left, to_dec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) entry->bytes_left -= to_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (entry->bytes_left == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* test_and_set_bit implies a barrier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) cond_wake_up_nomb(&entry->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (!ret && cached && entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) *cached = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) refcount_inc(&entry->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) spin_unlock_irqrestore(&tree->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return ret == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * this is used to account for finished IO across a given range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * of the file. The IO should not span ordered extents. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * a given ordered_extent is completely done, 1 is returned, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * to make sure this function only returns 1 once for a given ordered extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) int btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct btrfs_ordered_extent **cached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) u64 file_offset, u64 io_size, int uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct btrfs_ordered_extent *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) spin_lock_irqsave(&tree->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (cached && *cached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) entry = *cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) goto have_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) node = tree_search(tree, file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) have_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (!offset_in_entry(entry, file_offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (io_size > entry->bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) btrfs_crit(inode->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) "bad ordered accounting left %llu size %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) entry->bytes_left, io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) entry->bytes_left -= io_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (!uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (entry->bytes_left == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* test_and_set_bit implies a barrier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) cond_wake_up_nomb(&entry->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (!ret && cached && entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) *cached = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) refcount_inc(&entry->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) spin_unlock_irqrestore(&tree->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return ret == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * used to drop a reference on an ordered extent. This will free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * the extent if the last reference is dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) struct list_head *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct btrfs_ordered_sum *sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) trace_btrfs_ordered_extent_put(BTRFS_I(entry->inode), entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (refcount_dec_and_test(&entry->refs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) ASSERT(list_empty(&entry->root_extent_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) ASSERT(list_empty(&entry->log_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ASSERT(RB_EMPTY_NODE(&entry->rb_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (entry->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) btrfs_add_delayed_iput(entry->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) while (!list_empty(&entry->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) cur = entry->list.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) sum = list_entry(cur, struct btrfs_ordered_sum, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) list_del(&sum->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) kvfree(sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) kmem_cache_free(btrfs_ordered_extent_cache, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * remove an ordered extent from the tree. No references are dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * and waiters are woken up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct btrfs_ordered_extent *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) struct btrfs_ordered_inode_tree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct btrfs_root *root = btrfs_inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) bool pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* This is paired with btrfs_add_ordered_extent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) spin_lock(&btrfs_inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) btrfs_mod_outstanding_extents(btrfs_inode, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) spin_unlock(&btrfs_inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (root != fs_info->tree_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) btrfs_delalloc_release_metadata(btrfs_inode, entry->num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) percpu_counter_add_batch(&fs_info->dio_bytes, -entry->num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) fs_info->delalloc_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) tree = &btrfs_inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) node = &entry->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) rb_erase(node, &tree->tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) RB_CLEAR_NODE(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (tree->last == node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) tree->last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * The current running transaction is waiting on us, we need to let it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * know that we're complete and wake it up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) struct btrfs_transaction *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * The checks for trans are just a formality, it should be set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * but if it isn't we don't want to deref/assert under the spin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * lock, so be nice and check if trans is set, but ASSERT() so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * if it isn't set a developer will notice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) spin_lock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) trans = fs_info->running_transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) refcount_inc(&trans->use_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) spin_unlock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) ASSERT(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (atomic_dec_and_test(&trans->pending_ordered))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) wake_up(&trans->pending_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) btrfs_put_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) spin_lock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) list_del_init(&entry->root_extent_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) root->nr_ordered_extents--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) trace_btrfs_ordered_extent_remove(btrfs_inode, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (!root->nr_ordered_extents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) spin_lock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) BUG_ON(list_empty(&root->ordered_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) list_del_init(&root->ordered_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) spin_unlock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) spin_unlock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) wake_up(&entry->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) complete(&ordered->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * wait for all the ordered extents in a root. This is done when balancing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * space between drives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) const u64 range_start, const u64 range_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) LIST_HEAD(splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) LIST_HEAD(skipped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) LIST_HEAD(works);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct btrfs_ordered_extent *ordered, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) u64 count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) const u64 range_end = range_start + range_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) mutex_lock(&root->ordered_extent_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) spin_lock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) list_splice_init(&root->ordered_extents, &splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) while (!list_empty(&splice) && nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) root_extent_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (range_end <= ordered->disk_bytenr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) list_move_tail(&ordered->root_extent_list, &skipped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) cond_resched_lock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) list_move_tail(&ordered->root_extent_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) &root->ordered_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) refcount_inc(&ordered->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) spin_unlock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) btrfs_init_work(&ordered->flush_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) btrfs_run_ordered_extent_work, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) list_add_tail(&ordered->work_list, &works);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) spin_lock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (nr != U64_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) list_splice_tail(&skipped, &root->ordered_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) list_splice_tail(&splice, &root->ordered_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) spin_unlock(&root->ordered_extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) list_for_each_entry_safe(ordered, next, &works, work_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) list_del_init(&ordered->work_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) wait_for_completion(&ordered->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) mutex_unlock(&root->ordered_extent_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) const u64 range_start, const u64 range_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct list_head splice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) u64 done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) INIT_LIST_HEAD(&splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) mutex_lock(&fs_info->ordered_operations_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) spin_lock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) list_splice_init(&fs_info->ordered_roots, &splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) while (!list_empty(&splice) && nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) root = list_first_entry(&splice, struct btrfs_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) ordered_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) root = btrfs_grab_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) BUG_ON(!root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) list_move_tail(&root->ordered_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) &fs_info->ordered_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) spin_unlock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) done = btrfs_wait_ordered_extents(root, nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) range_start, range_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) spin_lock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (nr != U64_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) nr -= done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) list_splice_tail(&splice, &fs_info->ordered_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) spin_unlock(&fs_info->ordered_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) mutex_unlock(&fs_info->ordered_operations_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * Used to start IO or wait for a given ordered extent to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * If wait is one, this effectively waits on page writeback for all the pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * in the extent, and it waits on the io completion code to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * metadata into the btree corresponding to the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) u64 start = entry->file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) u64 end = start + entry->num_bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) struct btrfs_inode *inode = BTRFS_I(entry->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) trace_btrfs_ordered_extent_start(inode, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * pages in the range can be dirty, clean or writeback. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * start IO on any dirty ones so the wait doesn't stall waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * for the flusher thread to find them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) &entry->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Used to wait on ordered extents across a large range of bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) int ret_wb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) u64 orig_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (start + len < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) orig_end = INT_LIMIT(loff_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) orig_end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (orig_end > INT_LIMIT(loff_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) orig_end = INT_LIMIT(loff_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) /* start IO across the range first to instantiate any delalloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) ret = btrfs_fdatawrite_range(inode, start, orig_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * If we have a writeback error don't return immediately. Wait first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * for any ordered extents that haven't completed yet. This is to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * sure no one can dirty the same page ranges and call writepages()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * before the ordered extents complete - to avoid failures (-EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * when adding the new ordered extents to the ordered tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) end = orig_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (!ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (ordered->file_offset > orig_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) if (ordered->file_offset + ordered->num_bytes <= start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) end = ordered->file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * If the ordered extent had an error save the error but don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * exit without waiting first for all other ordered extents in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * the range to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (end == 0 || end == start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) end--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return ret_wb ? ret_wb : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * find an ordered extent corresponding to file_offset. return NULL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * nothing is found, otherwise take a reference on the extent and return it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) u64 file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) struct btrfs_ordered_inode_tree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) struct btrfs_ordered_extent *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) node = tree_search(tree, file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (!offset_in_entry(entry, file_offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) refcount_inc(&entry->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) /* Since the DIO code tries to lock a wide area we need to look for any ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * extents that exist in the range, rather than just the start of the range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct btrfs_inode *inode, u64 file_offset, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct btrfs_ordered_inode_tree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct btrfs_ordered_extent *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) node = tree_search(tree, file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) node = tree_search(tree, file_offset + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (range_overlaps(entry, file_offset, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (entry->file_offset >= file_offset + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) refcount_inc(&entry->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * Adds all ordered extents to the given list. The list ends up sorted by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * file_offset of the ordered extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) ASSERT(inode_is_locked(&inode->vfs_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (test_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) ASSERT(list_empty(&ordered->log_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) list_add_tail(&ordered->log_list, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) refcount_inc(&ordered->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * lookup and return any extent before 'file_offset'. NULL is returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * if none is found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) struct btrfs_ordered_extent *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) struct btrfs_ordered_inode_tree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) struct btrfs_ordered_extent *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) node = tree_search(tree, file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) refcount_inc(&entry->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * search the ordered extents for one corresponding to 'offset' and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * try to find a checksum. This is used because we allow pages to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * be reclaimed before their checksum is actually put into the btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) int btrfs_find_ordered_sum(struct btrfs_inode *inode, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) u64 disk_bytenr, u8 *sum, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) struct btrfs_ordered_sum *ordered_sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) unsigned long num_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) u32 sectorsize = btrfs_inode_sectorsize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) const u8 blocksize_bits = inode->vfs_inode.i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) int index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) ordered = btrfs_lookup_ordered_extent(inode, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (!ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (disk_bytenr >= ordered_sum->bytenr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) i = (disk_bytenr - ordered_sum->bytenr) >> blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) num_sectors = ordered_sum->len >> blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) num_sectors = min_t(int, len - index, num_sectors - i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) memcpy(sum + index, ordered_sum->sums + i * csum_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) num_sectors * csum_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) index += (int)num_sectors * csum_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (index == len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) disk_bytenr += num_sectors * sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * btrfs_flush_ordered_range - Lock the passed range and ensures all pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) * ordered extents in it are run to completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) * @inode: Inode whose ordered tree is to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) * @start: Beginning of range to flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * @end: Last byte of range to lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * @cached_state: If passed, will return the extent state responsible for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) * locked range. It's the caller's responsibility to free the cached state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * This function always returns with the given range locked, ensuring after it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) * called no order extent can be pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) struct extent_state *cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) struct extent_state **cachedp = &cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) cachedp = cached_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) lock_extent_bits(&inode->io_tree, start, end, cachedp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) ordered = btrfs_lookup_ordered_range(inode, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) end - start + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (!ordered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * If no external cached_state has been passed then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * decrement the extra ref taken for cachedp since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * aren't exposing it outside of this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) if (!cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) refcount_dec(&cache->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) unlock_extent_cached(&inode->io_tree, start, end, cachedp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) int __init ordered_data_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) sizeof(struct btrfs_ordered_extent), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) SLAB_MEM_SPREAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (!btrfs_ordered_extent_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) void __cold ordered_data_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) kmem_cache_destroy(btrfs_ordered_extent_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }