1a52d9a80SChris Mason #include <linux/bitops.h> 2a52d9a80SChris Mason #include <linux/slab.h> 3a52d9a80SChris Mason #include <linux/bio.h> 4a52d9a80SChris Mason #include <linux/mm.h> 5a52d9a80SChris Mason #include <linux/gfp.h> 6a52d9a80SChris Mason #include <linux/pagemap.h> 7a52d9a80SChris Mason #include <linux/page-flags.h> 8a52d9a80SChris Mason #include <linux/module.h> 9a52d9a80SChris Mason #include <linux/spinlock.h> 10a52d9a80SChris Mason #include <linux/blkdev.h> 114dc11904SChris Mason #include <linux/swap.h> 120a2118dfSJens Axboe #include <linux/version.h> 13a52d9a80SChris Mason #include "extent_map.h" 14a52d9a80SChris Mason 1586479a04SChris Mason /* temporary define until extent_map moves out of btrfs */ 1686479a04SChris Mason struct kmem_cache *btrfs_cache_create(const char *name, size_t size, 1786479a04SChris Mason unsigned long extra_flags, 1886479a04SChris Mason void (*ctor)(void *, struct kmem_cache *, 1986479a04SChris Mason unsigned long)); 2086479a04SChris Mason 21a52d9a80SChris Mason static struct kmem_cache *extent_map_cache; 22a52d9a80SChris Mason static struct kmem_cache *extent_state_cache; 236d36dcd4SChris Mason static struct kmem_cache *extent_buffer_cache; 24f510cfecSChris Mason 25f510cfecSChris Mason static LIST_HEAD(buffers); 26f510cfecSChris Mason static LIST_HEAD(states); 27f510cfecSChris Mason 28f510cfecSChris Mason static spinlock_t state_lock = SPIN_LOCK_UNLOCKED; 294dc11904SChris Mason #define BUFFER_LRU_MAX 64 30a52d9a80SChris Mason 31a52d9a80SChris Mason struct tree_entry { 32a52d9a80SChris Mason u64 start; 33a52d9a80SChris Mason u64 end; 34a52d9a80SChris Mason int in_tree; 35a52d9a80SChris Mason struct rb_node rb_node; 36a52d9a80SChris Mason }; 37a52d9a80SChris Mason 38a52d9a80SChris Mason void __init extent_map_init(void) 39a52d9a80SChris Mason { 4086479a04SChris Mason extent_map_cache = btrfs_cache_create("extent_map", 416d36dcd4SChris Mason sizeof(struct extent_map), 0, 42a52d9a80SChris Mason NULL); 4386479a04SChris Mason extent_state_cache = btrfs_cache_create("extent_state", 446d36dcd4SChris Mason sizeof(struct extent_state), 0, 45a52d9a80SChris Mason NULL); 466d36dcd4SChris Mason extent_buffer_cache = btrfs_cache_create("extent_buffers", 476d36dcd4SChris Mason sizeof(struct extent_buffer), 0, 486d36dcd4SChris Mason NULL); 49a52d9a80SChris Mason } 50a52d9a80SChris Mason 51a52d9a80SChris Mason void __exit extent_map_exit(void) 52a52d9a80SChris Mason { 53f510cfecSChris Mason struct extent_state *state; 546d36dcd4SChris Mason 55f510cfecSChris Mason while (!list_empty(&states)) { 56f510cfecSChris Mason state = list_entry(states.next, struct extent_state, list); 57f510cfecSChris Mason printk("state leak: start %Lu end %Lu state %lu in tree %d refs %d\n", state->start, state->end, state->state, state->in_tree, atomic_read(&state->refs)); 58f510cfecSChris Mason list_del(&state->list); 59f510cfecSChris Mason kmem_cache_free(extent_state_cache, state); 60f510cfecSChris Mason 61f510cfecSChris Mason } 62f510cfecSChris Mason 63a52d9a80SChris Mason if (extent_map_cache) 64a52d9a80SChris Mason kmem_cache_destroy(extent_map_cache); 65a52d9a80SChris Mason if (extent_state_cache) 66a52d9a80SChris Mason kmem_cache_destroy(extent_state_cache); 676d36dcd4SChris Mason if (extent_buffer_cache) 686d36dcd4SChris Mason kmem_cache_destroy(extent_buffer_cache); 69a52d9a80SChris Mason } 70a52d9a80SChris Mason 71a52d9a80SChris Mason void extent_map_tree_init(struct extent_map_tree *tree, 72a52d9a80SChris Mason struct address_space *mapping, gfp_t mask) 73a52d9a80SChris Mason { 74a52d9a80SChris Mason tree->map.rb_node = NULL; 75a52d9a80SChris Mason tree->state.rb_node = NULL; 7607157aacSChris Mason tree->ops = NULL; 77a52d9a80SChris Mason rwlock_init(&tree->lock); 784dc11904SChris Mason spin_lock_init(&tree->lru_lock); 79a52d9a80SChris Mason tree->mapping = mapping; 804dc11904SChris Mason INIT_LIST_HEAD(&tree->buffer_lru); 814dc11904SChris Mason tree->lru_size = 0; 82a52d9a80SChris Mason } 83a52d9a80SChris Mason EXPORT_SYMBOL(extent_map_tree_init); 84a52d9a80SChris Mason 8519c00ddcSChris Mason void extent_map_tree_empty_lru(struct extent_map_tree *tree) 864dc11904SChris Mason { 874dc11904SChris Mason struct extent_buffer *eb; 884dc11904SChris Mason while(!list_empty(&tree->buffer_lru)) { 894dc11904SChris Mason eb = list_entry(tree->buffer_lru.next, struct extent_buffer, 904dc11904SChris Mason lru); 914dc11904SChris Mason list_del(&eb->lru); 924dc11904SChris Mason free_extent_buffer(eb); 934dc11904SChris Mason } 944dc11904SChris Mason } 9519c00ddcSChris Mason EXPORT_SYMBOL(extent_map_tree_empty_lru); 964dc11904SChris Mason 97a52d9a80SChris Mason struct extent_map *alloc_extent_map(gfp_t mask) 98a52d9a80SChris Mason { 99a52d9a80SChris Mason struct extent_map *em; 100a52d9a80SChris Mason em = kmem_cache_alloc(extent_map_cache, mask); 101a52d9a80SChris Mason if (!em || IS_ERR(em)) 102a52d9a80SChris Mason return em; 103a52d9a80SChris Mason em->in_tree = 0; 104a52d9a80SChris Mason atomic_set(&em->refs, 1); 105a52d9a80SChris Mason return em; 106a52d9a80SChris Mason } 107a52d9a80SChris Mason EXPORT_SYMBOL(alloc_extent_map); 108a52d9a80SChris Mason 109a52d9a80SChris Mason void free_extent_map(struct extent_map *em) 110a52d9a80SChris Mason { 1112bf5a725SChris Mason if (!em) 1122bf5a725SChris Mason return; 113a52d9a80SChris Mason if (atomic_dec_and_test(&em->refs)) { 114a52d9a80SChris Mason WARN_ON(em->in_tree); 115a52d9a80SChris Mason kmem_cache_free(extent_map_cache, em); 116a52d9a80SChris Mason } 117a52d9a80SChris Mason } 118a52d9a80SChris Mason EXPORT_SYMBOL(free_extent_map); 119a52d9a80SChris Mason 120a52d9a80SChris Mason 121a52d9a80SChris Mason struct extent_state *alloc_extent_state(gfp_t mask) 122a52d9a80SChris Mason { 123a52d9a80SChris Mason struct extent_state *state; 124f510cfecSChris Mason unsigned long flags; 125f510cfecSChris Mason 126a52d9a80SChris Mason state = kmem_cache_alloc(extent_state_cache, mask); 127a52d9a80SChris Mason if (!state || IS_ERR(state)) 128a52d9a80SChris Mason return state; 129a52d9a80SChris Mason state->state = 0; 130a52d9a80SChris Mason state->in_tree = 0; 13107157aacSChris Mason state->private = 0; 132f510cfecSChris Mason 133f510cfecSChris Mason spin_lock_irqsave(&state_lock, flags); 134f510cfecSChris Mason list_add(&state->list, &states); 135f510cfecSChris Mason spin_unlock_irqrestore(&state_lock, flags); 136f510cfecSChris Mason 137a52d9a80SChris Mason atomic_set(&state->refs, 1); 138a52d9a80SChris Mason init_waitqueue_head(&state->wq); 139a52d9a80SChris Mason return state; 140a52d9a80SChris Mason } 141a52d9a80SChris Mason EXPORT_SYMBOL(alloc_extent_state); 142a52d9a80SChris Mason 143a52d9a80SChris Mason void free_extent_state(struct extent_state *state) 144a52d9a80SChris Mason { 145f510cfecSChris Mason unsigned long flags; 1462bf5a725SChris Mason if (!state) 1472bf5a725SChris Mason return; 148a52d9a80SChris Mason if (atomic_dec_and_test(&state->refs)) { 149a52d9a80SChris Mason WARN_ON(state->in_tree); 150f510cfecSChris Mason spin_lock_irqsave(&state_lock, flags); 151f510cfecSChris Mason list_del(&state->list); 152f510cfecSChris Mason spin_unlock_irqrestore(&state_lock, flags); 153a52d9a80SChris Mason kmem_cache_free(extent_state_cache, state); 154a52d9a80SChris Mason } 155a52d9a80SChris Mason } 156a52d9a80SChris Mason EXPORT_SYMBOL(free_extent_state); 157a52d9a80SChris Mason 158a52d9a80SChris Mason static struct rb_node *tree_insert(struct rb_root *root, u64 offset, 159a52d9a80SChris Mason struct rb_node *node) 160a52d9a80SChris Mason { 161a52d9a80SChris Mason struct rb_node ** p = &root->rb_node; 162a52d9a80SChris Mason struct rb_node * parent = NULL; 163a52d9a80SChris Mason struct tree_entry *entry; 164a52d9a80SChris Mason 165a52d9a80SChris Mason while(*p) { 166a52d9a80SChris Mason parent = *p; 167a52d9a80SChris Mason entry = rb_entry(parent, struct tree_entry, rb_node); 168a52d9a80SChris Mason 169a52d9a80SChris Mason if (offset < entry->start) 170a52d9a80SChris Mason p = &(*p)->rb_left; 171a52d9a80SChris Mason else if (offset > entry->end) 172a52d9a80SChris Mason p = &(*p)->rb_right; 173a52d9a80SChris Mason else 174a52d9a80SChris Mason return parent; 175a52d9a80SChris Mason } 176a52d9a80SChris Mason 177a52d9a80SChris Mason entry = rb_entry(node, struct tree_entry, rb_node); 178a52d9a80SChris Mason entry->in_tree = 1; 179a52d9a80SChris Mason rb_link_node(node, parent, p); 180a52d9a80SChris Mason rb_insert_color(node, root); 181a52d9a80SChris Mason return NULL; 182a52d9a80SChris Mason } 183a52d9a80SChris Mason 184a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset, 185a52d9a80SChris Mason struct rb_node **prev_ret) 186a52d9a80SChris Mason { 187a52d9a80SChris Mason struct rb_node * n = root->rb_node; 188a52d9a80SChris Mason struct rb_node *prev = NULL; 189a52d9a80SChris Mason struct tree_entry *entry; 190a52d9a80SChris Mason struct tree_entry *prev_entry = NULL; 191a52d9a80SChris Mason 192a52d9a80SChris Mason while(n) { 193a52d9a80SChris Mason entry = rb_entry(n, struct tree_entry, rb_node); 194a52d9a80SChris Mason prev = n; 195a52d9a80SChris Mason prev_entry = entry; 196a52d9a80SChris Mason 197a52d9a80SChris Mason if (offset < entry->start) 198a52d9a80SChris Mason n = n->rb_left; 199a52d9a80SChris Mason else if (offset > entry->end) 200a52d9a80SChris Mason n = n->rb_right; 201a52d9a80SChris Mason else 202a52d9a80SChris Mason return n; 203a52d9a80SChris Mason } 204a52d9a80SChris Mason if (!prev_ret) 205a52d9a80SChris Mason return NULL; 206a52d9a80SChris Mason while(prev && offset > prev_entry->end) { 207a52d9a80SChris Mason prev = rb_next(prev); 208a52d9a80SChris Mason prev_entry = rb_entry(prev, struct tree_entry, rb_node); 209a52d9a80SChris Mason } 210a52d9a80SChris Mason *prev_ret = prev; 211a52d9a80SChris Mason return NULL; 212a52d9a80SChris Mason } 213a52d9a80SChris Mason 214a52d9a80SChris Mason static inline struct rb_node *tree_search(struct rb_root *root, u64 offset) 215a52d9a80SChris Mason { 216a52d9a80SChris Mason struct rb_node *prev; 217a52d9a80SChris Mason struct rb_node *ret; 218a52d9a80SChris Mason ret = __tree_search(root, offset, &prev); 219a52d9a80SChris Mason if (!ret) 220a52d9a80SChris Mason return prev; 221a52d9a80SChris Mason return ret; 222a52d9a80SChris Mason } 223a52d9a80SChris Mason 224a52d9a80SChris Mason static int tree_delete(struct rb_root *root, u64 offset) 225a52d9a80SChris Mason { 226a52d9a80SChris Mason struct rb_node *node; 227a52d9a80SChris Mason struct tree_entry *entry; 228a52d9a80SChris Mason 229a52d9a80SChris Mason node = __tree_search(root, offset, NULL); 230a52d9a80SChris Mason if (!node) 231a52d9a80SChris Mason return -ENOENT; 232a52d9a80SChris Mason entry = rb_entry(node, struct tree_entry, rb_node); 233a52d9a80SChris Mason entry->in_tree = 0; 234a52d9a80SChris Mason rb_erase(node, root); 235a52d9a80SChris Mason return 0; 236a52d9a80SChris Mason } 237a52d9a80SChris Mason 238a52d9a80SChris Mason /* 239a52d9a80SChris Mason * add_extent_mapping tries a simple backward merge with existing 240a52d9a80SChris Mason * mappings. The extent_map struct passed in will be inserted into 241a52d9a80SChris Mason * the tree directly (no copies made, just a reference taken). 242a52d9a80SChris Mason */ 243a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree, 244a52d9a80SChris Mason struct extent_map *em) 245a52d9a80SChris Mason { 246a52d9a80SChris Mason int ret = 0; 247a52d9a80SChris Mason struct extent_map *prev = NULL; 248a52d9a80SChris Mason struct rb_node *rb; 249a52d9a80SChris Mason 250a52d9a80SChris Mason write_lock_irq(&tree->lock); 251a52d9a80SChris Mason rb = tree_insert(&tree->map, em->end, &em->rb_node); 252a52d9a80SChris Mason if (rb) { 253a52d9a80SChris Mason prev = rb_entry(rb, struct extent_map, rb_node); 254a52d9a80SChris Mason printk("found extent map %Lu %Lu on insert of %Lu %Lu\n", prev->start, prev->end, em->start, em->end); 255a52d9a80SChris Mason ret = -EEXIST; 256a52d9a80SChris Mason goto out; 257a52d9a80SChris Mason } 258a52d9a80SChris Mason atomic_inc(&em->refs); 259a52d9a80SChris Mason if (em->start != 0) { 260a52d9a80SChris Mason rb = rb_prev(&em->rb_node); 261a52d9a80SChris Mason if (rb) 262a52d9a80SChris Mason prev = rb_entry(rb, struct extent_map, rb_node); 263a52d9a80SChris Mason if (prev && prev->end + 1 == em->start && 2645f39d397SChris Mason ((em->block_start == EXTENT_MAP_HOLE && 2655f39d397SChris Mason prev->block_start == EXTENT_MAP_HOLE) || 266179e29e4SChris Mason (em->block_start == EXTENT_MAP_INLINE && 267179e29e4SChris Mason prev->block_start == EXTENT_MAP_INLINE) || 268179e29e4SChris Mason (em->block_start == EXTENT_MAP_DELALLOC && 269179e29e4SChris Mason prev->block_start == EXTENT_MAP_DELALLOC) || 270179e29e4SChris Mason (em->block_start < EXTENT_MAP_DELALLOC - 1 && 271179e29e4SChris Mason em->block_start == prev->block_end + 1))) { 272a52d9a80SChris Mason em->start = prev->start; 273a52d9a80SChris Mason em->block_start = prev->block_start; 274a52d9a80SChris Mason rb_erase(&prev->rb_node, &tree->map); 275a52d9a80SChris Mason prev->in_tree = 0; 276a52d9a80SChris Mason free_extent_map(prev); 277a52d9a80SChris Mason } 278a52d9a80SChris Mason } 279a52d9a80SChris Mason out: 280a52d9a80SChris Mason write_unlock_irq(&tree->lock); 281a52d9a80SChris Mason return ret; 282a52d9a80SChris Mason } 283a52d9a80SChris Mason EXPORT_SYMBOL(add_extent_mapping); 284a52d9a80SChris Mason 285a52d9a80SChris Mason /* 286a52d9a80SChris Mason * lookup_extent_mapping returns the first extent_map struct in the 287a52d9a80SChris Mason * tree that intersects the [start, end] (inclusive) range. There may 288a52d9a80SChris Mason * be additional objects in the tree that intersect, so check the object 289a52d9a80SChris Mason * returned carefully to make sure you don't need additional lookups. 290a52d9a80SChris Mason */ 291a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, 292a52d9a80SChris Mason u64 start, u64 end) 293a52d9a80SChris Mason { 294a52d9a80SChris Mason struct extent_map *em; 295a52d9a80SChris Mason struct rb_node *rb_node; 296a52d9a80SChris Mason 297a52d9a80SChris Mason read_lock_irq(&tree->lock); 298a52d9a80SChris Mason rb_node = tree_search(&tree->map, start); 299a52d9a80SChris Mason if (!rb_node) { 300a52d9a80SChris Mason em = NULL; 301a52d9a80SChris Mason goto out; 302a52d9a80SChris Mason } 303a52d9a80SChris Mason if (IS_ERR(rb_node)) { 304a52d9a80SChris Mason em = ERR_PTR(PTR_ERR(rb_node)); 305a52d9a80SChris Mason goto out; 306a52d9a80SChris Mason } 307a52d9a80SChris Mason em = rb_entry(rb_node, struct extent_map, rb_node); 308a52d9a80SChris Mason if (em->end < start || em->start > end) { 309a52d9a80SChris Mason em = NULL; 310a52d9a80SChris Mason goto out; 311a52d9a80SChris Mason } 312a52d9a80SChris Mason atomic_inc(&em->refs); 313a52d9a80SChris Mason out: 314a52d9a80SChris Mason read_unlock_irq(&tree->lock); 315a52d9a80SChris Mason return em; 316a52d9a80SChris Mason } 317a52d9a80SChris Mason EXPORT_SYMBOL(lookup_extent_mapping); 318a52d9a80SChris Mason 319a52d9a80SChris Mason /* 320a52d9a80SChris Mason * removes an extent_map struct from the tree. No reference counts are 321a52d9a80SChris Mason * dropped, and no checks are done to see if the range is in use 322a52d9a80SChris Mason */ 323a52d9a80SChris Mason int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) 324a52d9a80SChris Mason { 325a52d9a80SChris Mason int ret; 326a52d9a80SChris Mason 327a52d9a80SChris Mason write_lock_irq(&tree->lock); 328a52d9a80SChris Mason ret = tree_delete(&tree->map, em->end); 329a52d9a80SChris Mason write_unlock_irq(&tree->lock); 330a52d9a80SChris Mason return ret; 331a52d9a80SChris Mason } 332a52d9a80SChris Mason EXPORT_SYMBOL(remove_extent_mapping); 333a52d9a80SChris Mason 334a52d9a80SChris Mason /* 335a52d9a80SChris Mason * utility function to look for merge candidates inside a given range. 336a52d9a80SChris Mason * Any extents with matching state are merged together into a single 337a52d9a80SChris Mason * extent in the tree. Extents with EXTENT_IO in their state field 338a52d9a80SChris Mason * are not merged because the end_io handlers need to be able to do 339a52d9a80SChris Mason * operations on them without sleeping (or doing allocations/splits). 340a52d9a80SChris Mason * 341a52d9a80SChris Mason * This should be called with the tree lock held. 342a52d9a80SChris Mason */ 343a52d9a80SChris Mason static int merge_state(struct extent_map_tree *tree, 344a52d9a80SChris Mason struct extent_state *state) 345a52d9a80SChris Mason { 346a52d9a80SChris Mason struct extent_state *other; 347a52d9a80SChris Mason struct rb_node *other_node; 348a52d9a80SChris Mason 349a52d9a80SChris Mason if (state->state & EXTENT_IOBITS) 350a52d9a80SChris Mason return 0; 351a52d9a80SChris Mason 352a52d9a80SChris Mason other_node = rb_prev(&state->rb_node); 353a52d9a80SChris Mason if (other_node) { 354a52d9a80SChris Mason other = rb_entry(other_node, struct extent_state, rb_node); 355a52d9a80SChris Mason if (other->end == state->start - 1 && 356a52d9a80SChris Mason other->state == state->state) { 357a52d9a80SChris Mason state->start = other->start; 358a52d9a80SChris Mason other->in_tree = 0; 359a52d9a80SChris Mason rb_erase(&other->rb_node, &tree->state); 360a52d9a80SChris Mason free_extent_state(other); 361a52d9a80SChris Mason } 362a52d9a80SChris Mason } 363a52d9a80SChris Mason other_node = rb_next(&state->rb_node); 364a52d9a80SChris Mason if (other_node) { 365a52d9a80SChris Mason other = rb_entry(other_node, struct extent_state, rb_node); 366a52d9a80SChris Mason if (other->start == state->end + 1 && 367a52d9a80SChris Mason other->state == state->state) { 368a52d9a80SChris Mason other->start = state->start; 369a52d9a80SChris Mason state->in_tree = 0; 370a52d9a80SChris Mason rb_erase(&state->rb_node, &tree->state); 371a52d9a80SChris Mason free_extent_state(state); 372a52d9a80SChris Mason } 373a52d9a80SChris Mason } 374a52d9a80SChris Mason return 0; 375a52d9a80SChris Mason } 376a52d9a80SChris Mason 377a52d9a80SChris Mason /* 378a52d9a80SChris Mason * insert an extent_state struct into the tree. 'bits' are set on the 379a52d9a80SChris Mason * struct before it is inserted. 380a52d9a80SChris Mason * 381a52d9a80SChris Mason * This may return -EEXIST if the extent is already there, in which case the 382a52d9a80SChris Mason * state struct is freed. 383a52d9a80SChris Mason * 384a52d9a80SChris Mason * The tree lock is not taken internally. This is a utility function and 385a52d9a80SChris Mason * probably isn't what you want to call (see set/clear_extent_bit). 386a52d9a80SChris Mason */ 387a52d9a80SChris Mason static int insert_state(struct extent_map_tree *tree, 388a52d9a80SChris Mason struct extent_state *state, u64 start, u64 end, 389a52d9a80SChris Mason int bits) 390a52d9a80SChris Mason { 391a52d9a80SChris Mason struct rb_node *node; 392a52d9a80SChris Mason 393a52d9a80SChris Mason if (end < start) { 394a52d9a80SChris Mason printk("end < start %Lu %Lu\n", end, start); 395a52d9a80SChris Mason WARN_ON(1); 396a52d9a80SChris Mason } 397a52d9a80SChris Mason state->state |= bits; 398a52d9a80SChris Mason state->start = start; 399a52d9a80SChris Mason state->end = end; 400a52d9a80SChris Mason node = tree_insert(&tree->state, end, &state->rb_node); 401a52d9a80SChris Mason if (node) { 402a52d9a80SChris Mason struct extent_state *found; 403a52d9a80SChris Mason found = rb_entry(node, struct extent_state, rb_node); 404a52d9a80SChris Mason printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end); 405a52d9a80SChris Mason free_extent_state(state); 406a52d9a80SChris Mason return -EEXIST; 407a52d9a80SChris Mason } 408a52d9a80SChris Mason merge_state(tree, state); 409a52d9a80SChris Mason return 0; 410a52d9a80SChris Mason } 411a52d9a80SChris Mason 412a52d9a80SChris Mason /* 413a52d9a80SChris Mason * split a given extent state struct in two, inserting the preallocated 414a52d9a80SChris Mason * struct 'prealloc' as the newly created second half. 'split' indicates an 415a52d9a80SChris Mason * offset inside 'orig' where it should be split. 416a52d9a80SChris Mason * 417a52d9a80SChris Mason * Before calling, 418a52d9a80SChris Mason * the tree has 'orig' at [orig->start, orig->end]. After calling, there 419a52d9a80SChris Mason * are two extent state structs in the tree: 420a52d9a80SChris Mason * prealloc: [orig->start, split - 1] 421a52d9a80SChris Mason * orig: [ split, orig->end ] 422a52d9a80SChris Mason * 423a52d9a80SChris Mason * The tree locks are not taken by this function. They need to be held 424a52d9a80SChris Mason * by the caller. 425a52d9a80SChris Mason */ 426a52d9a80SChris Mason static int split_state(struct extent_map_tree *tree, struct extent_state *orig, 427a52d9a80SChris Mason struct extent_state *prealloc, u64 split) 428a52d9a80SChris Mason { 429a52d9a80SChris Mason struct rb_node *node; 430a52d9a80SChris Mason prealloc->start = orig->start; 431a52d9a80SChris Mason prealloc->end = split - 1; 432a52d9a80SChris Mason prealloc->state = orig->state; 433a52d9a80SChris Mason orig->start = split; 434f510cfecSChris Mason 435a52d9a80SChris Mason node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node); 436a52d9a80SChris Mason if (node) { 437a52d9a80SChris Mason struct extent_state *found; 438a52d9a80SChris Mason found = rb_entry(node, struct extent_state, rb_node); 439a52d9a80SChris Mason printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end); 440a52d9a80SChris Mason free_extent_state(prealloc); 441a52d9a80SChris Mason return -EEXIST; 442a52d9a80SChris Mason } 443a52d9a80SChris Mason return 0; 444a52d9a80SChris Mason } 445a52d9a80SChris Mason 446a52d9a80SChris Mason /* 447a52d9a80SChris Mason * utility function to clear some bits in an extent state struct. 448a52d9a80SChris Mason * it will optionally wake up any one waiting on this state (wake == 1), or 449a52d9a80SChris Mason * forcibly remove the state from the tree (delete == 1). 450a52d9a80SChris Mason * 451a52d9a80SChris Mason * If no bits are set on the state struct after clearing things, the 452a52d9a80SChris Mason * struct is freed and removed from the tree 453a52d9a80SChris Mason */ 454a52d9a80SChris Mason static int clear_state_bit(struct extent_map_tree *tree, 455a52d9a80SChris Mason struct extent_state *state, int bits, int wake, 456a52d9a80SChris Mason int delete) 457a52d9a80SChris Mason { 458a52d9a80SChris Mason int ret = state->state & bits; 459a52d9a80SChris Mason state->state &= ~bits; 460a52d9a80SChris Mason if (wake) 461a52d9a80SChris Mason wake_up(&state->wq); 462a52d9a80SChris Mason if (delete || state->state == 0) { 463a52d9a80SChris Mason if (state->in_tree) { 464a52d9a80SChris Mason rb_erase(&state->rb_node, &tree->state); 465a52d9a80SChris Mason state->in_tree = 0; 466a52d9a80SChris Mason free_extent_state(state); 467a52d9a80SChris Mason } else { 468a52d9a80SChris Mason WARN_ON(1); 469a52d9a80SChris Mason } 470a52d9a80SChris Mason } else { 471a52d9a80SChris Mason merge_state(tree, state); 472a52d9a80SChris Mason } 473a52d9a80SChris Mason return ret; 474a52d9a80SChris Mason } 475a52d9a80SChris Mason 476a52d9a80SChris Mason /* 477a52d9a80SChris Mason * clear some bits on a range in the tree. This may require splitting 478a52d9a80SChris Mason * or inserting elements in the tree, so the gfp mask is used to 479a52d9a80SChris Mason * indicate which allocations or sleeping are allowed. 480a52d9a80SChris Mason * 481a52d9a80SChris Mason * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove 482a52d9a80SChris Mason * the given range from the tree regardless of state (ie for truncate). 483a52d9a80SChris Mason * 484a52d9a80SChris Mason * the range [start, end] is inclusive. 485a52d9a80SChris Mason * 486a52d9a80SChris Mason * This takes the tree lock, and returns < 0 on error, > 0 if any of the 487a52d9a80SChris Mason * bits were already set, or zero if none of the bits were already set. 488a52d9a80SChris Mason */ 489a52d9a80SChris Mason int clear_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, 490a52d9a80SChris Mason int bits, int wake, int delete, gfp_t mask) 491a52d9a80SChris Mason { 492a52d9a80SChris Mason struct extent_state *state; 493a52d9a80SChris Mason struct extent_state *prealloc = NULL; 494a52d9a80SChris Mason struct rb_node *node; 49590f1c19aSChristoph Hellwig unsigned long flags; 496a52d9a80SChris Mason int err; 497a52d9a80SChris Mason int set = 0; 498a52d9a80SChris Mason 499a52d9a80SChris Mason again: 500a52d9a80SChris Mason if (!prealloc && (mask & __GFP_WAIT)) { 501a52d9a80SChris Mason prealloc = alloc_extent_state(mask); 502a52d9a80SChris Mason if (!prealloc) 503a52d9a80SChris Mason return -ENOMEM; 504a52d9a80SChris Mason } 505a52d9a80SChris Mason 50690f1c19aSChristoph Hellwig write_lock_irqsave(&tree->lock, flags); 507a52d9a80SChris Mason /* 508a52d9a80SChris Mason * this search will find the extents that end after 509a52d9a80SChris Mason * our range starts 510a52d9a80SChris Mason */ 511a52d9a80SChris Mason node = tree_search(&tree->state, start); 512a52d9a80SChris Mason if (!node) 513a52d9a80SChris Mason goto out; 514a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 515a52d9a80SChris Mason if (state->start > end) 516a52d9a80SChris Mason goto out; 517a52d9a80SChris Mason WARN_ON(state->end < start); 518a52d9a80SChris Mason 519a52d9a80SChris Mason /* 520a52d9a80SChris Mason * | ---- desired range ---- | 521a52d9a80SChris Mason * | state | or 522a52d9a80SChris Mason * | ------------- state -------------- | 523a52d9a80SChris Mason * 524a52d9a80SChris Mason * We need to split the extent we found, and may flip 525a52d9a80SChris Mason * bits on second half. 526a52d9a80SChris Mason * 527a52d9a80SChris Mason * If the extent we found extends past our range, we 528a52d9a80SChris Mason * just split and search again. It'll get split again 529a52d9a80SChris Mason * the next time though. 530a52d9a80SChris Mason * 531a52d9a80SChris Mason * If the extent we found is inside our range, we clear 532a52d9a80SChris Mason * the desired bit on it. 533a52d9a80SChris Mason */ 534a52d9a80SChris Mason 535a52d9a80SChris Mason if (state->start < start) { 536a52d9a80SChris Mason err = split_state(tree, state, prealloc, start); 537a52d9a80SChris Mason BUG_ON(err == -EEXIST); 538a52d9a80SChris Mason prealloc = NULL; 539a52d9a80SChris Mason if (err) 540a52d9a80SChris Mason goto out; 541a52d9a80SChris Mason if (state->end <= end) { 542a52d9a80SChris Mason start = state->end + 1; 543a52d9a80SChris Mason set |= clear_state_bit(tree, state, bits, 544a52d9a80SChris Mason wake, delete); 545a52d9a80SChris Mason } else { 546a52d9a80SChris Mason start = state->start; 547a52d9a80SChris Mason } 548a52d9a80SChris Mason goto search_again; 549a52d9a80SChris Mason } 550a52d9a80SChris Mason /* 551a52d9a80SChris Mason * | ---- desired range ---- | 552a52d9a80SChris Mason * | state | 553a52d9a80SChris Mason * We need to split the extent, and clear the bit 554a52d9a80SChris Mason * on the first half 555a52d9a80SChris Mason */ 556a52d9a80SChris Mason if (state->start <= end && state->end > end) { 557a52d9a80SChris Mason err = split_state(tree, state, prealloc, end + 1); 558a52d9a80SChris Mason BUG_ON(err == -EEXIST); 559a52d9a80SChris Mason 560a52d9a80SChris Mason if (wake) 561a52d9a80SChris Mason wake_up(&state->wq); 562a52d9a80SChris Mason set |= clear_state_bit(tree, prealloc, bits, 563a52d9a80SChris Mason wake, delete); 564a52d9a80SChris Mason prealloc = NULL; 565a52d9a80SChris Mason goto out; 566a52d9a80SChris Mason } 567a52d9a80SChris Mason 568a52d9a80SChris Mason start = state->end + 1; 569a52d9a80SChris Mason set |= clear_state_bit(tree, state, bits, wake, delete); 570a52d9a80SChris Mason goto search_again; 571a52d9a80SChris Mason 572a52d9a80SChris Mason out: 57390f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 574a52d9a80SChris Mason if (prealloc) 575a52d9a80SChris Mason free_extent_state(prealloc); 576a52d9a80SChris Mason 577a52d9a80SChris Mason return set; 578a52d9a80SChris Mason 579a52d9a80SChris Mason search_again: 58096b5179dSChris Mason if (start > end) 581a52d9a80SChris Mason goto out; 58290f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 583a52d9a80SChris Mason if (mask & __GFP_WAIT) 584a52d9a80SChris Mason cond_resched(); 585a52d9a80SChris Mason goto again; 586a52d9a80SChris Mason } 587a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_bit); 588a52d9a80SChris Mason 589a52d9a80SChris Mason static int wait_on_state(struct extent_map_tree *tree, 590a52d9a80SChris Mason struct extent_state *state) 591a52d9a80SChris Mason { 592a52d9a80SChris Mason DEFINE_WAIT(wait); 593a52d9a80SChris Mason prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); 594a52d9a80SChris Mason read_unlock_irq(&tree->lock); 595a52d9a80SChris Mason schedule(); 596a52d9a80SChris Mason read_lock_irq(&tree->lock); 597a52d9a80SChris Mason finish_wait(&state->wq, &wait); 598a52d9a80SChris Mason return 0; 599a52d9a80SChris Mason } 600a52d9a80SChris Mason 601a52d9a80SChris Mason /* 602a52d9a80SChris Mason * waits for one or more bits to clear on a range in the state tree. 603a52d9a80SChris Mason * The range [start, end] is inclusive. 604a52d9a80SChris Mason * The tree lock is taken by this function 605a52d9a80SChris Mason */ 606a52d9a80SChris Mason int wait_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits) 607a52d9a80SChris Mason { 608a52d9a80SChris Mason struct extent_state *state; 609a52d9a80SChris Mason struct rb_node *node; 610a52d9a80SChris Mason 611a52d9a80SChris Mason read_lock_irq(&tree->lock); 612a52d9a80SChris Mason again: 613a52d9a80SChris Mason while (1) { 614a52d9a80SChris Mason /* 615a52d9a80SChris Mason * this search will find all the extents that end after 616a52d9a80SChris Mason * our range starts 617a52d9a80SChris Mason */ 618a52d9a80SChris Mason node = tree_search(&tree->state, start); 619a52d9a80SChris Mason if (!node) 620a52d9a80SChris Mason break; 621a52d9a80SChris Mason 622a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 623a52d9a80SChris Mason 624a52d9a80SChris Mason if (state->start > end) 625a52d9a80SChris Mason goto out; 626a52d9a80SChris Mason 627a52d9a80SChris Mason if (state->state & bits) { 628a52d9a80SChris Mason start = state->start; 629a52d9a80SChris Mason atomic_inc(&state->refs); 630a52d9a80SChris Mason wait_on_state(tree, state); 631a52d9a80SChris Mason free_extent_state(state); 632a52d9a80SChris Mason goto again; 633a52d9a80SChris Mason } 634a52d9a80SChris Mason start = state->end + 1; 635a52d9a80SChris Mason 636a52d9a80SChris Mason if (start > end) 637a52d9a80SChris Mason break; 638a52d9a80SChris Mason 639a52d9a80SChris Mason if (need_resched()) { 640a52d9a80SChris Mason read_unlock_irq(&tree->lock); 641a52d9a80SChris Mason cond_resched(); 642a52d9a80SChris Mason read_lock_irq(&tree->lock); 643a52d9a80SChris Mason } 644a52d9a80SChris Mason } 645a52d9a80SChris Mason out: 646a52d9a80SChris Mason read_unlock_irq(&tree->lock); 647a52d9a80SChris Mason return 0; 648a52d9a80SChris Mason } 649a52d9a80SChris Mason EXPORT_SYMBOL(wait_extent_bit); 650a52d9a80SChris Mason 651a52d9a80SChris Mason /* 652a52d9a80SChris Mason * set some bits on a range in the tree. This may require allocations 653a52d9a80SChris Mason * or sleeping, so the gfp mask is used to indicate what is allowed. 654a52d9a80SChris Mason * 655a52d9a80SChris Mason * If 'exclusive' == 1, this will fail with -EEXIST if some part of the 656a52d9a80SChris Mason * range already has the desired bits set. The start of the existing 657a52d9a80SChris Mason * range is returned in failed_start in this case. 658a52d9a80SChris Mason * 659a52d9a80SChris Mason * [start, end] is inclusive 660a52d9a80SChris Mason * This takes the tree lock. 661a52d9a80SChris Mason */ 662a52d9a80SChris Mason int set_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits, 663a52d9a80SChris Mason int exclusive, u64 *failed_start, gfp_t mask) 664a52d9a80SChris Mason { 665a52d9a80SChris Mason struct extent_state *state; 666a52d9a80SChris Mason struct extent_state *prealloc = NULL; 667a52d9a80SChris Mason struct rb_node *node; 66890f1c19aSChristoph Hellwig unsigned long flags; 669a52d9a80SChris Mason int err = 0; 670a52d9a80SChris Mason int set; 671a52d9a80SChris Mason u64 last_start; 672a52d9a80SChris Mason u64 last_end; 673a52d9a80SChris Mason again: 674a52d9a80SChris Mason if (!prealloc && (mask & __GFP_WAIT)) { 675a52d9a80SChris Mason prealloc = alloc_extent_state(mask); 676a52d9a80SChris Mason if (!prealloc) 677a52d9a80SChris Mason return -ENOMEM; 678a52d9a80SChris Mason } 679a52d9a80SChris Mason 68090f1c19aSChristoph Hellwig write_lock_irqsave(&tree->lock, flags); 681a52d9a80SChris Mason /* 682a52d9a80SChris Mason * this search will find all the extents that end after 683a52d9a80SChris Mason * our range starts. 684a52d9a80SChris Mason */ 685a52d9a80SChris Mason node = tree_search(&tree->state, start); 686a52d9a80SChris Mason if (!node) { 687a52d9a80SChris Mason err = insert_state(tree, prealloc, start, end, bits); 688a52d9a80SChris Mason prealloc = NULL; 689a52d9a80SChris Mason BUG_ON(err == -EEXIST); 690a52d9a80SChris Mason goto out; 691a52d9a80SChris Mason } 692a52d9a80SChris Mason 693a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 694a52d9a80SChris Mason last_start = state->start; 695a52d9a80SChris Mason last_end = state->end; 696a52d9a80SChris Mason 697a52d9a80SChris Mason /* 698a52d9a80SChris Mason * | ---- desired range ---- | 699a52d9a80SChris Mason * | state | 700a52d9a80SChris Mason * 701a52d9a80SChris Mason * Just lock what we found and keep going 702a52d9a80SChris Mason */ 703a52d9a80SChris Mason if (state->start == start && state->end <= end) { 704a52d9a80SChris Mason set = state->state & bits; 705a52d9a80SChris Mason if (set && exclusive) { 706a52d9a80SChris Mason *failed_start = state->start; 707a52d9a80SChris Mason err = -EEXIST; 708a52d9a80SChris Mason goto out; 709a52d9a80SChris Mason } 710a52d9a80SChris Mason state->state |= bits; 711a52d9a80SChris Mason start = state->end + 1; 712a52d9a80SChris Mason merge_state(tree, state); 713a52d9a80SChris Mason goto search_again; 714a52d9a80SChris Mason } 715a52d9a80SChris Mason 716a52d9a80SChris Mason /* 717a52d9a80SChris Mason * | ---- desired range ---- | 718a52d9a80SChris Mason * | state | 719a52d9a80SChris Mason * or 720a52d9a80SChris Mason * | ------------- state -------------- | 721a52d9a80SChris Mason * 722a52d9a80SChris Mason * We need to split the extent we found, and may flip bits on 723a52d9a80SChris Mason * second half. 724a52d9a80SChris Mason * 725a52d9a80SChris Mason * If the extent we found extends past our 726a52d9a80SChris Mason * range, we just split and search again. It'll get split 727a52d9a80SChris Mason * again the next time though. 728a52d9a80SChris Mason * 729a52d9a80SChris Mason * If the extent we found is inside our range, we set the 730a52d9a80SChris Mason * desired bit on it. 731a52d9a80SChris Mason */ 732a52d9a80SChris Mason if (state->start < start) { 733a52d9a80SChris Mason set = state->state & bits; 734a52d9a80SChris Mason if (exclusive && set) { 735a52d9a80SChris Mason *failed_start = start; 736a52d9a80SChris Mason err = -EEXIST; 737a52d9a80SChris Mason goto out; 738a52d9a80SChris Mason } 739a52d9a80SChris Mason err = split_state(tree, state, prealloc, start); 740a52d9a80SChris Mason BUG_ON(err == -EEXIST); 741a52d9a80SChris Mason prealloc = NULL; 742a52d9a80SChris Mason if (err) 743a52d9a80SChris Mason goto out; 744a52d9a80SChris Mason if (state->end <= end) { 745a52d9a80SChris Mason state->state |= bits; 746a52d9a80SChris Mason start = state->end + 1; 747a52d9a80SChris Mason merge_state(tree, state); 748a52d9a80SChris Mason } else { 749a52d9a80SChris Mason start = state->start; 750a52d9a80SChris Mason } 751a52d9a80SChris Mason goto search_again; 752a52d9a80SChris Mason } 753a52d9a80SChris Mason /* 754a52d9a80SChris Mason * | ---- desired range ---- | 755a52d9a80SChris Mason * | state | or | state | 756a52d9a80SChris Mason * 757a52d9a80SChris Mason * There's a hole, we need to insert something in it and 758a52d9a80SChris Mason * ignore the extent we found. 759a52d9a80SChris Mason */ 760a52d9a80SChris Mason if (state->start > start) { 761a52d9a80SChris Mason u64 this_end; 762a52d9a80SChris Mason if (end < last_start) 763a52d9a80SChris Mason this_end = end; 764a52d9a80SChris Mason else 765a52d9a80SChris Mason this_end = last_start -1; 766a52d9a80SChris Mason err = insert_state(tree, prealloc, start, this_end, 767a52d9a80SChris Mason bits); 768a52d9a80SChris Mason prealloc = NULL; 769a52d9a80SChris Mason BUG_ON(err == -EEXIST); 770a52d9a80SChris Mason if (err) 771a52d9a80SChris Mason goto out; 772a52d9a80SChris Mason start = this_end + 1; 773a52d9a80SChris Mason goto search_again; 774a52d9a80SChris Mason } 775a8c450b2SChris Mason /* 776a8c450b2SChris Mason * | ---- desired range ---- | 777a8c450b2SChris Mason * | state | 778a8c450b2SChris Mason * We need to split the extent, and set the bit 779a8c450b2SChris Mason * on the first half 780a8c450b2SChris Mason */ 781a8c450b2SChris Mason if (state->start <= end && state->end > end) { 782a8c450b2SChris Mason set = state->state & bits; 783a8c450b2SChris Mason if (exclusive && set) { 784a8c450b2SChris Mason *failed_start = start; 785a8c450b2SChris Mason err = -EEXIST; 786a8c450b2SChris Mason goto out; 787a8c450b2SChris Mason } 788a8c450b2SChris Mason err = split_state(tree, state, prealloc, end + 1); 789a8c450b2SChris Mason BUG_ON(err == -EEXIST); 790a8c450b2SChris Mason 791a8c450b2SChris Mason prealloc->state |= bits; 792a8c450b2SChris Mason merge_state(tree, prealloc); 793a8c450b2SChris Mason prealloc = NULL; 794a8c450b2SChris Mason goto out; 795a8c450b2SChris Mason } 796a8c450b2SChris Mason 797a52d9a80SChris Mason goto search_again; 798a52d9a80SChris Mason 799a52d9a80SChris Mason out: 80090f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 801a52d9a80SChris Mason if (prealloc) 802a52d9a80SChris Mason free_extent_state(prealloc); 803a52d9a80SChris Mason 804a52d9a80SChris Mason return err; 805a52d9a80SChris Mason 806a52d9a80SChris Mason search_again: 807a52d9a80SChris Mason if (start > end) 808a52d9a80SChris Mason goto out; 80990f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 810a52d9a80SChris Mason if (mask & __GFP_WAIT) 811a52d9a80SChris Mason cond_resched(); 812a52d9a80SChris Mason goto again; 813a52d9a80SChris Mason } 814a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_bit); 815a52d9a80SChris Mason 816a52d9a80SChris Mason /* wrappers around set/clear extent bit */ 817a52d9a80SChris Mason int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end, 818a52d9a80SChris Mason gfp_t mask) 819a52d9a80SChris Mason { 820a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL, 821a52d9a80SChris Mason mask); 822a52d9a80SChris Mason } 823a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_dirty); 824a52d9a80SChris Mason 82596b5179dSChris Mason int set_extent_bits(struct extent_map_tree *tree, u64 start, u64 end, 82696b5179dSChris Mason int bits, gfp_t mask) 82796b5179dSChris Mason { 82896b5179dSChris Mason return set_extent_bit(tree, start, end, bits, 0, NULL, 82996b5179dSChris Mason mask); 83096b5179dSChris Mason } 83196b5179dSChris Mason EXPORT_SYMBOL(set_extent_bits); 83296b5179dSChris Mason 83396b5179dSChris Mason int clear_extent_bits(struct extent_map_tree *tree, u64 start, u64 end, 83496b5179dSChris Mason int bits, gfp_t mask) 83596b5179dSChris Mason { 83696b5179dSChris Mason return clear_extent_bit(tree, start, end, bits, 0, 0, mask); 83796b5179dSChris Mason } 83896b5179dSChris Mason EXPORT_SYMBOL(clear_extent_bits); 83996b5179dSChris Mason 840b888db2bSChris Mason int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end, 841b888db2bSChris Mason gfp_t mask) 842b888db2bSChris Mason { 843b888db2bSChris Mason return set_extent_bit(tree, start, end, 844b888db2bSChris Mason EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL, 845b888db2bSChris Mason mask); 846b888db2bSChris Mason } 847b888db2bSChris Mason EXPORT_SYMBOL(set_extent_delalloc); 848b888db2bSChris Mason 849a52d9a80SChris Mason int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end, 850a52d9a80SChris Mason gfp_t mask) 851a52d9a80SChris Mason { 852b888db2bSChris Mason return clear_extent_bit(tree, start, end, 853b888db2bSChris Mason EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask); 854a52d9a80SChris Mason } 855a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_dirty); 856a52d9a80SChris Mason 857a52d9a80SChris Mason int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end, 858a52d9a80SChris Mason gfp_t mask) 859a52d9a80SChris Mason { 860a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL, 861a52d9a80SChris Mason mask); 862a52d9a80SChris Mason } 863a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_new); 864a52d9a80SChris Mason 865a52d9a80SChris Mason int clear_extent_new(struct extent_map_tree *tree, u64 start, u64 end, 866a52d9a80SChris Mason gfp_t mask) 867a52d9a80SChris Mason { 868a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask); 869a52d9a80SChris Mason } 870a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_new); 871a52d9a80SChris Mason 872a52d9a80SChris Mason int set_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end, 873a52d9a80SChris Mason gfp_t mask) 874a52d9a80SChris Mason { 875a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL, 876a52d9a80SChris Mason mask); 877a52d9a80SChris Mason } 878a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_uptodate); 879a52d9a80SChris Mason 880a52d9a80SChris Mason int clear_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end, 881a52d9a80SChris Mason gfp_t mask) 882a52d9a80SChris Mason { 883a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask); 884a52d9a80SChris Mason } 885a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_uptodate); 886a52d9a80SChris Mason 887a52d9a80SChris Mason int set_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end, 888a52d9a80SChris Mason gfp_t mask) 889a52d9a80SChris Mason { 890a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_WRITEBACK, 891a52d9a80SChris Mason 0, NULL, mask); 892a52d9a80SChris Mason } 893a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_writeback); 894a52d9a80SChris Mason 895a52d9a80SChris Mason int clear_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end, 896a52d9a80SChris Mason gfp_t mask) 897a52d9a80SChris Mason { 898a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask); 899a52d9a80SChris Mason } 900a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_writeback); 901a52d9a80SChris Mason 902a52d9a80SChris Mason int wait_on_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end) 903a52d9a80SChris Mason { 904a52d9a80SChris Mason return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK); 905a52d9a80SChris Mason } 906a52d9a80SChris Mason EXPORT_SYMBOL(wait_on_extent_writeback); 907a52d9a80SChris Mason 908a52d9a80SChris Mason /* 909a52d9a80SChris Mason * locks a range in ascending order, waiting for any locked regions 910a52d9a80SChris Mason * it hits on the way. [start,end] are inclusive, and this will sleep. 911a52d9a80SChris Mason */ 912a52d9a80SChris Mason int lock_extent(struct extent_map_tree *tree, u64 start, u64 end, gfp_t mask) 913a52d9a80SChris Mason { 914a52d9a80SChris Mason int err; 915a52d9a80SChris Mason u64 failed_start; 916a52d9a80SChris Mason while (1) { 917a52d9a80SChris Mason err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 918a52d9a80SChris Mason &failed_start, mask); 919a52d9a80SChris Mason if (err == -EEXIST && (mask & __GFP_WAIT)) { 920a52d9a80SChris Mason wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); 921a52d9a80SChris Mason start = failed_start; 922a52d9a80SChris Mason } else { 923a52d9a80SChris Mason break; 924a52d9a80SChris Mason } 925a52d9a80SChris Mason WARN_ON(start > end); 926a52d9a80SChris Mason } 927a52d9a80SChris Mason return err; 928a52d9a80SChris Mason } 929a52d9a80SChris Mason EXPORT_SYMBOL(lock_extent); 930a52d9a80SChris Mason 931a52d9a80SChris Mason int unlock_extent(struct extent_map_tree *tree, u64 start, u64 end, 932a52d9a80SChris Mason gfp_t mask) 933a52d9a80SChris Mason { 934a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask); 935a52d9a80SChris Mason } 936a52d9a80SChris Mason EXPORT_SYMBOL(unlock_extent); 937a52d9a80SChris Mason 938a52d9a80SChris Mason /* 939a52d9a80SChris Mason * helper function to set pages and extents in the tree dirty 940a52d9a80SChris Mason */ 941a52d9a80SChris Mason int set_range_dirty(struct extent_map_tree *tree, u64 start, u64 end) 942a52d9a80SChris Mason { 943a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 944a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 945a52d9a80SChris Mason struct page *page; 946a52d9a80SChris Mason 947a52d9a80SChris Mason while (index <= end_index) { 948a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 949a52d9a80SChris Mason BUG_ON(!page); 950a52d9a80SChris Mason __set_page_dirty_nobuffers(page); 951a52d9a80SChris Mason page_cache_release(page); 952a52d9a80SChris Mason index++; 953a52d9a80SChris Mason } 954a52d9a80SChris Mason set_extent_dirty(tree, start, end, GFP_NOFS); 955a52d9a80SChris Mason return 0; 956a52d9a80SChris Mason } 957a52d9a80SChris Mason EXPORT_SYMBOL(set_range_dirty); 958a52d9a80SChris Mason 959a52d9a80SChris Mason /* 960a52d9a80SChris Mason * helper function to set both pages and extents in the tree writeback 961a52d9a80SChris Mason */ 962a52d9a80SChris Mason int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end) 963a52d9a80SChris Mason { 964a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 965a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 966a52d9a80SChris Mason struct page *page; 967a52d9a80SChris Mason 968a52d9a80SChris Mason while (index <= end_index) { 969a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 970a52d9a80SChris Mason BUG_ON(!page); 971a52d9a80SChris Mason set_page_writeback(page); 972a52d9a80SChris Mason page_cache_release(page); 973a52d9a80SChris Mason index++; 974a52d9a80SChris Mason } 975a52d9a80SChris Mason set_extent_writeback(tree, start, end, GFP_NOFS); 976a52d9a80SChris Mason return 0; 977a52d9a80SChris Mason } 978a52d9a80SChris Mason EXPORT_SYMBOL(set_range_writeback); 979a52d9a80SChris Mason 9805f39d397SChris Mason int find_first_extent_bit(struct extent_map_tree *tree, u64 start, 9815f39d397SChris Mason u64 *start_ret, u64 *end_ret, int bits) 9825f39d397SChris Mason { 9835f39d397SChris Mason struct rb_node *node; 9845f39d397SChris Mason struct extent_state *state; 9855f39d397SChris Mason int ret = 1; 9865f39d397SChris Mason 987e19caa5fSChris Mason read_lock_irq(&tree->lock); 9885f39d397SChris Mason /* 9895f39d397SChris Mason * this search will find all the extents that end after 9905f39d397SChris Mason * our range starts. 9915f39d397SChris Mason */ 9925f39d397SChris Mason node = tree_search(&tree->state, start); 9935f39d397SChris Mason if (!node || IS_ERR(node)) { 9945f39d397SChris Mason goto out; 9955f39d397SChris Mason } 9965f39d397SChris Mason 9975f39d397SChris Mason while(1) { 9985f39d397SChris Mason state = rb_entry(node, struct extent_state, rb_node); 999e19caa5fSChris Mason if (state->end >= start && (state->state & bits)) { 10005f39d397SChris Mason *start_ret = state->start; 10015f39d397SChris Mason *end_ret = state->end; 10025f39d397SChris Mason ret = 0; 1003f510cfecSChris Mason break; 10045f39d397SChris Mason } 10055f39d397SChris Mason node = rb_next(node); 10065f39d397SChris Mason if (!node) 10075f39d397SChris Mason break; 10085f39d397SChris Mason } 10095f39d397SChris Mason out: 1010e19caa5fSChris Mason read_unlock_irq(&tree->lock); 10115f39d397SChris Mason return ret; 10125f39d397SChris Mason } 10135f39d397SChris Mason EXPORT_SYMBOL(find_first_extent_bit); 10145f39d397SChris Mason 1015b888db2bSChris Mason u64 find_lock_delalloc_range(struct extent_map_tree *tree, 1016b888db2bSChris Mason u64 start, u64 lock_start, u64 *end, u64 max_bytes) 1017b888db2bSChris Mason { 1018b888db2bSChris Mason struct rb_node *node; 1019b888db2bSChris Mason struct extent_state *state; 1020b888db2bSChris Mason u64 cur_start = start; 1021b888db2bSChris Mason u64 found = 0; 1022b888db2bSChris Mason u64 total_bytes = 0; 1023b888db2bSChris Mason 1024b888db2bSChris Mason write_lock_irq(&tree->lock); 1025b888db2bSChris Mason /* 1026b888db2bSChris Mason * this search will find all the extents that end after 1027b888db2bSChris Mason * our range starts. 1028b888db2bSChris Mason */ 1029b888db2bSChris Mason search_again: 1030b888db2bSChris Mason node = tree_search(&tree->state, cur_start); 1031b888db2bSChris Mason if (!node || IS_ERR(node)) { 1032b888db2bSChris Mason goto out; 1033b888db2bSChris Mason } 1034b888db2bSChris Mason 1035b888db2bSChris Mason while(1) { 1036b888db2bSChris Mason state = rb_entry(node, struct extent_state, rb_node); 1037b888db2bSChris Mason if (state->start != cur_start) { 1038b888db2bSChris Mason goto out; 1039b888db2bSChris Mason } 1040b888db2bSChris Mason if (!(state->state & EXTENT_DELALLOC)) { 1041b888db2bSChris Mason goto out; 1042b888db2bSChris Mason } 1043b888db2bSChris Mason if (state->start >= lock_start) { 1044b888db2bSChris Mason if (state->state & EXTENT_LOCKED) { 1045b888db2bSChris Mason DEFINE_WAIT(wait); 1046b888db2bSChris Mason atomic_inc(&state->refs); 1047*944746ecSYan prepare_to_wait(&state->wq, &wait, 1048*944746ecSYan TASK_UNINTERRUPTIBLE); 1049b888db2bSChris Mason write_unlock_irq(&tree->lock); 1050b888db2bSChris Mason schedule(); 1051b888db2bSChris Mason write_lock_irq(&tree->lock); 1052b888db2bSChris Mason finish_wait(&state->wq, &wait); 1053b888db2bSChris Mason free_extent_state(state); 1054b888db2bSChris Mason goto search_again; 1055b888db2bSChris Mason } 1056b888db2bSChris Mason state->state |= EXTENT_LOCKED; 1057b888db2bSChris Mason } 1058b888db2bSChris Mason found++; 1059b888db2bSChris Mason *end = state->end; 1060b888db2bSChris Mason cur_start = state->end + 1; 1061b888db2bSChris Mason node = rb_next(node); 1062b888db2bSChris Mason if (!node) 1063b888db2bSChris Mason break; 1064*944746ecSYan total_bytes += state->end - state->start + 1; 1065b888db2bSChris Mason if (total_bytes >= max_bytes) 1066b888db2bSChris Mason break; 1067b888db2bSChris Mason } 1068b888db2bSChris Mason out: 1069b888db2bSChris Mason write_unlock_irq(&tree->lock); 1070b888db2bSChris Mason return found; 1071b888db2bSChris Mason } 1072b888db2bSChris Mason 1073a52d9a80SChris Mason /* 1074a52d9a80SChris Mason * helper function to lock both pages and extents in the tree. 1075a52d9a80SChris Mason * pages must be locked first. 1076a52d9a80SChris Mason */ 1077a52d9a80SChris Mason int lock_range(struct extent_map_tree *tree, u64 start, u64 end) 1078a52d9a80SChris Mason { 1079a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 1080a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 1081a52d9a80SChris Mason struct page *page; 1082a52d9a80SChris Mason int err; 1083a52d9a80SChris Mason 1084a52d9a80SChris Mason while (index <= end_index) { 1085a52d9a80SChris Mason page = grab_cache_page(tree->mapping, index); 1086a52d9a80SChris Mason if (!page) { 1087a52d9a80SChris Mason err = -ENOMEM; 1088a52d9a80SChris Mason goto failed; 1089a52d9a80SChris Mason } 1090a52d9a80SChris Mason if (IS_ERR(page)) { 1091a52d9a80SChris Mason err = PTR_ERR(page); 1092a52d9a80SChris Mason goto failed; 1093a52d9a80SChris Mason } 1094a52d9a80SChris Mason index++; 1095a52d9a80SChris Mason } 1096a52d9a80SChris Mason lock_extent(tree, start, end, GFP_NOFS); 1097a52d9a80SChris Mason return 0; 1098a52d9a80SChris Mason 1099a52d9a80SChris Mason failed: 1100a52d9a80SChris Mason /* 1101a52d9a80SChris Mason * we failed above in getting the page at 'index', so we undo here 1102a52d9a80SChris Mason * up to but not including the page at 'index' 1103a52d9a80SChris Mason */ 1104a52d9a80SChris Mason end_index = index; 1105a52d9a80SChris Mason index = start >> PAGE_CACHE_SHIFT; 1106a52d9a80SChris Mason while (index < end_index) { 1107a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 1108a52d9a80SChris Mason unlock_page(page); 1109a52d9a80SChris Mason page_cache_release(page); 1110a52d9a80SChris Mason index++; 1111a52d9a80SChris Mason } 1112a52d9a80SChris Mason return err; 1113a52d9a80SChris Mason } 1114a52d9a80SChris Mason EXPORT_SYMBOL(lock_range); 1115a52d9a80SChris Mason 1116a52d9a80SChris Mason /* 1117a52d9a80SChris Mason * helper function to unlock both pages and extents in the tree. 1118a52d9a80SChris Mason */ 1119a52d9a80SChris Mason int unlock_range(struct extent_map_tree *tree, u64 start, u64 end) 1120a52d9a80SChris Mason { 1121a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 1122a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 1123a52d9a80SChris Mason struct page *page; 1124a52d9a80SChris Mason 1125a52d9a80SChris Mason while (index <= end_index) { 1126a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 1127a52d9a80SChris Mason unlock_page(page); 1128a52d9a80SChris Mason page_cache_release(page); 1129a52d9a80SChris Mason index++; 1130a52d9a80SChris Mason } 1131a52d9a80SChris Mason unlock_extent(tree, start, end, GFP_NOFS); 1132a52d9a80SChris Mason return 0; 1133a52d9a80SChris Mason } 1134a52d9a80SChris Mason EXPORT_SYMBOL(unlock_range); 1135a52d9a80SChris Mason 113607157aacSChris Mason int set_state_private(struct extent_map_tree *tree, u64 start, u64 private) 113707157aacSChris Mason { 113807157aacSChris Mason struct rb_node *node; 113907157aacSChris Mason struct extent_state *state; 114007157aacSChris Mason int ret = 0; 114107157aacSChris Mason 114207157aacSChris Mason write_lock_irq(&tree->lock); 114307157aacSChris Mason /* 114407157aacSChris Mason * this search will find all the extents that end after 114507157aacSChris Mason * our range starts. 114607157aacSChris Mason */ 114707157aacSChris Mason node = tree_search(&tree->state, start); 114807157aacSChris Mason if (!node || IS_ERR(node)) { 114907157aacSChris Mason ret = -ENOENT; 115007157aacSChris Mason goto out; 115107157aacSChris Mason } 115207157aacSChris Mason state = rb_entry(node, struct extent_state, rb_node); 115307157aacSChris Mason if (state->start != start) { 115407157aacSChris Mason ret = -ENOENT; 115507157aacSChris Mason goto out; 115607157aacSChris Mason } 115707157aacSChris Mason state->private = private; 115807157aacSChris Mason out: 115907157aacSChris Mason write_unlock_irq(&tree->lock); 116007157aacSChris Mason return ret; 116107157aacSChris Mason } 116207157aacSChris Mason 116307157aacSChris Mason int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private) 116407157aacSChris Mason { 116507157aacSChris Mason struct rb_node *node; 116607157aacSChris Mason struct extent_state *state; 116707157aacSChris Mason int ret = 0; 116807157aacSChris Mason 116907157aacSChris Mason read_lock_irq(&tree->lock); 117007157aacSChris Mason /* 117107157aacSChris Mason * this search will find all the extents that end after 117207157aacSChris Mason * our range starts. 117307157aacSChris Mason */ 117407157aacSChris Mason node = tree_search(&tree->state, start); 117507157aacSChris Mason if (!node || IS_ERR(node)) { 117607157aacSChris Mason ret = -ENOENT; 117707157aacSChris Mason goto out; 117807157aacSChris Mason } 117907157aacSChris Mason state = rb_entry(node, struct extent_state, rb_node); 118007157aacSChris Mason if (state->start != start) { 118107157aacSChris Mason ret = -ENOENT; 118207157aacSChris Mason goto out; 118307157aacSChris Mason } 118407157aacSChris Mason *private = state->private; 118507157aacSChris Mason out: 118607157aacSChris Mason read_unlock_irq(&tree->lock); 118707157aacSChris Mason return ret; 118807157aacSChris Mason } 118907157aacSChris Mason 1190a52d9a80SChris Mason /* 1191a52d9a80SChris Mason * searches a range in the state tree for a given mask. 1192a52d9a80SChris Mason * If 'filled' == 1, this returns 1 only if ever extent in the tree 1193a52d9a80SChris Mason * has the bits set. Otherwise, 1 is returned if any bit in the 1194a52d9a80SChris Mason * range is found set. 1195a52d9a80SChris Mason */ 11961a5bc167SChris Mason int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end, 1197a52d9a80SChris Mason int bits, int filled) 1198a52d9a80SChris Mason { 1199a52d9a80SChris Mason struct extent_state *state = NULL; 1200a52d9a80SChris Mason struct rb_node *node; 1201a52d9a80SChris Mason int bitset = 0; 1202a52d9a80SChris Mason 1203a52d9a80SChris Mason read_lock_irq(&tree->lock); 1204a52d9a80SChris Mason node = tree_search(&tree->state, start); 1205a52d9a80SChris Mason while (node && start <= end) { 1206a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 1207a52d9a80SChris Mason if (state->start > end) 1208a52d9a80SChris Mason break; 1209a52d9a80SChris Mason 1210a52d9a80SChris Mason if (filled && state->start > start) { 1211a52d9a80SChris Mason bitset = 0; 1212a52d9a80SChris Mason break; 1213a52d9a80SChris Mason } 1214a52d9a80SChris Mason if (state->state & bits) { 1215a52d9a80SChris Mason bitset = 1; 1216a52d9a80SChris Mason if (!filled) 1217a52d9a80SChris Mason break; 1218a52d9a80SChris Mason } else if (filled) { 1219a52d9a80SChris Mason bitset = 0; 1220a52d9a80SChris Mason break; 1221a52d9a80SChris Mason } 1222a52d9a80SChris Mason start = state->end + 1; 1223a52d9a80SChris Mason if (start > end) 1224a52d9a80SChris Mason break; 1225a52d9a80SChris Mason node = rb_next(node); 1226a52d9a80SChris Mason } 1227a52d9a80SChris Mason read_unlock_irq(&tree->lock); 1228a52d9a80SChris Mason return bitset; 1229a52d9a80SChris Mason } 12301a5bc167SChris Mason EXPORT_SYMBOL(test_range_bit); 1231a52d9a80SChris Mason 1232a52d9a80SChris Mason /* 1233a52d9a80SChris Mason * helper function to set a given page up to date if all the 1234a52d9a80SChris Mason * extents in the tree for that page are up to date 1235a52d9a80SChris Mason */ 1236a52d9a80SChris Mason static int check_page_uptodate(struct extent_map_tree *tree, 1237a52d9a80SChris Mason struct page *page) 1238a52d9a80SChris Mason { 123935ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1240a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1241a52d9a80SChris Mason if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1)) 1242a52d9a80SChris Mason SetPageUptodate(page); 1243a52d9a80SChris Mason return 0; 1244a52d9a80SChris Mason } 1245a52d9a80SChris Mason 1246a52d9a80SChris Mason /* 1247a52d9a80SChris Mason * helper function to unlock a page if all the extents in the tree 1248a52d9a80SChris Mason * for that page are unlocked 1249a52d9a80SChris Mason */ 1250a52d9a80SChris Mason static int check_page_locked(struct extent_map_tree *tree, 1251a52d9a80SChris Mason struct page *page) 1252a52d9a80SChris Mason { 125335ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1254a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1255a52d9a80SChris Mason if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0)) 1256a52d9a80SChris Mason unlock_page(page); 1257a52d9a80SChris Mason return 0; 1258a52d9a80SChris Mason } 1259a52d9a80SChris Mason 1260a52d9a80SChris Mason /* 1261a52d9a80SChris Mason * helper function to end page writeback if all the extents 1262a52d9a80SChris Mason * in the tree for that page are done with writeback 1263a52d9a80SChris Mason */ 1264a52d9a80SChris Mason static int check_page_writeback(struct extent_map_tree *tree, 1265a52d9a80SChris Mason struct page *page) 1266a52d9a80SChris Mason { 126735ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1268a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1269a52d9a80SChris Mason if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0)) 1270a52d9a80SChris Mason end_page_writeback(page); 1271a52d9a80SChris Mason return 0; 1272a52d9a80SChris Mason } 1273a52d9a80SChris Mason 1274a52d9a80SChris Mason /* lots and lots of room for performance fixes in the end_bio funcs */ 1275a52d9a80SChris Mason 1276a52d9a80SChris Mason /* 1277a52d9a80SChris Mason * after a writepage IO is done, we need to: 1278a52d9a80SChris Mason * clear the uptodate bits on error 1279a52d9a80SChris Mason * clear the writeback bits in the extent tree for this IO 1280a52d9a80SChris Mason * end_page_writeback if the page has no more pending IO 1281a52d9a80SChris Mason * 1282a52d9a80SChris Mason * Scheduling is not allowed, so the extent state tree is expected 1283a52d9a80SChris Mason * to have one and only one object corresponding to this IO. 1284a52d9a80SChris Mason */ 12850a2118dfSJens Axboe #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) 12860a2118dfSJens Axboe static void end_bio_extent_writepage(struct bio *bio, int err) 12870a2118dfSJens Axboe #else 1288a52d9a80SChris Mason static int end_bio_extent_writepage(struct bio *bio, 1289a52d9a80SChris Mason unsigned int bytes_done, int err) 12900a2118dfSJens Axboe #endif 1291a52d9a80SChris Mason { 1292a52d9a80SChris Mason const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1293a52d9a80SChris Mason struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; 1294a52d9a80SChris Mason struct extent_map_tree *tree = bio->bi_private; 1295a52d9a80SChris Mason u64 start; 1296a52d9a80SChris Mason u64 end; 1297a52d9a80SChris Mason int whole_page; 1298a52d9a80SChris Mason 12990a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1300a52d9a80SChris Mason if (bio->bi_size) 1301a52d9a80SChris Mason return 1; 13020a2118dfSJens Axboe #endif 1303a52d9a80SChris Mason 1304a52d9a80SChris Mason do { 1305a52d9a80SChris Mason struct page *page = bvec->bv_page; 130635ebb934SChris Mason start = ((u64)page->index << PAGE_CACHE_SHIFT) + 130735ebb934SChris Mason bvec->bv_offset; 1308a52d9a80SChris Mason end = start + bvec->bv_len - 1; 1309a52d9a80SChris Mason 1310a52d9a80SChris Mason if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) 1311a52d9a80SChris Mason whole_page = 1; 1312a52d9a80SChris Mason else 1313a52d9a80SChris Mason whole_page = 0; 1314a52d9a80SChris Mason 1315a52d9a80SChris Mason if (--bvec >= bio->bi_io_vec) 1316a52d9a80SChris Mason prefetchw(&bvec->bv_page->flags); 1317a52d9a80SChris Mason 1318a52d9a80SChris Mason if (!uptodate) { 1319a52d9a80SChris Mason clear_extent_uptodate(tree, start, end, GFP_ATOMIC); 1320a52d9a80SChris Mason ClearPageUptodate(page); 1321a52d9a80SChris Mason SetPageError(page); 1322a52d9a80SChris Mason } 1323a52d9a80SChris Mason clear_extent_writeback(tree, start, end, GFP_ATOMIC); 1324a52d9a80SChris Mason 1325a52d9a80SChris Mason if (whole_page) 1326a52d9a80SChris Mason end_page_writeback(page); 1327a52d9a80SChris Mason else 1328a52d9a80SChris Mason check_page_writeback(tree, page); 13290e2752a7SChristoph Hellwig if (tree->ops && tree->ops->writepage_end_io_hook) 13300e2752a7SChristoph Hellwig tree->ops->writepage_end_io_hook(page, start, end); 1331a52d9a80SChris Mason } while (bvec >= bio->bi_io_vec); 1332a52d9a80SChris Mason 1333a52d9a80SChris Mason bio_put(bio); 13340a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1335a52d9a80SChris Mason return 0; 13360a2118dfSJens Axboe #endif 1337a52d9a80SChris Mason } 1338a52d9a80SChris Mason 1339a52d9a80SChris Mason /* 1340a52d9a80SChris Mason * after a readpage IO is done, we need to: 1341a52d9a80SChris Mason * clear the uptodate bits on error 1342a52d9a80SChris Mason * set the uptodate bits if things worked 1343a52d9a80SChris Mason * set the page up to date if all extents in the tree are uptodate 1344a52d9a80SChris Mason * clear the lock bit in the extent tree 1345a52d9a80SChris Mason * unlock the page if there are no other extents locked for it 1346a52d9a80SChris Mason * 1347a52d9a80SChris Mason * Scheduling is not allowed, so the extent state tree is expected 1348a52d9a80SChris Mason * to have one and only one object corresponding to this IO. 1349a52d9a80SChris Mason */ 13500a2118dfSJens Axboe #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) 13510a2118dfSJens Axboe static void end_bio_extent_readpage(struct bio *bio, int err) 13520a2118dfSJens Axboe #else 1353a52d9a80SChris Mason static int end_bio_extent_readpage(struct bio *bio, 1354a52d9a80SChris Mason unsigned int bytes_done, int err) 13550a2118dfSJens Axboe #endif 1356a52d9a80SChris Mason { 135707157aacSChris Mason int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1358a52d9a80SChris Mason struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; 1359a52d9a80SChris Mason struct extent_map_tree *tree = bio->bi_private; 1360a52d9a80SChris Mason u64 start; 1361a52d9a80SChris Mason u64 end; 1362a52d9a80SChris Mason int whole_page; 136307157aacSChris Mason int ret; 1364a52d9a80SChris Mason 13650a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1366a52d9a80SChris Mason if (bio->bi_size) 1367a52d9a80SChris Mason return 1; 13680a2118dfSJens Axboe #endif 1369a52d9a80SChris Mason 1370a52d9a80SChris Mason do { 1371a52d9a80SChris Mason struct page *page = bvec->bv_page; 137235ebb934SChris Mason start = ((u64)page->index << PAGE_CACHE_SHIFT) + 137335ebb934SChris Mason bvec->bv_offset; 1374a52d9a80SChris Mason end = start + bvec->bv_len - 1; 1375a52d9a80SChris Mason 1376a52d9a80SChris Mason if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) 1377a52d9a80SChris Mason whole_page = 1; 1378a52d9a80SChris Mason else 1379a52d9a80SChris Mason whole_page = 0; 1380a52d9a80SChris Mason 1381a52d9a80SChris Mason if (--bvec >= bio->bi_io_vec) 1382a52d9a80SChris Mason prefetchw(&bvec->bv_page->flags); 1383a52d9a80SChris Mason 138407157aacSChris Mason if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { 138507157aacSChris Mason ret = tree->ops->readpage_end_io_hook(page, start, end); 138607157aacSChris Mason if (ret) 138707157aacSChris Mason uptodate = 0; 138807157aacSChris Mason } 1389a52d9a80SChris Mason if (uptodate) { 1390a52d9a80SChris Mason set_extent_uptodate(tree, start, end, GFP_ATOMIC); 1391a52d9a80SChris Mason if (whole_page) 1392a52d9a80SChris Mason SetPageUptodate(page); 1393a52d9a80SChris Mason else 1394a52d9a80SChris Mason check_page_uptodate(tree, page); 1395a52d9a80SChris Mason } else { 1396a52d9a80SChris Mason ClearPageUptodate(page); 1397a52d9a80SChris Mason SetPageError(page); 1398a52d9a80SChris Mason } 1399a52d9a80SChris Mason 1400a52d9a80SChris Mason unlock_extent(tree, start, end, GFP_ATOMIC); 1401a52d9a80SChris Mason 1402a52d9a80SChris Mason if (whole_page) 1403a52d9a80SChris Mason unlock_page(page); 1404a52d9a80SChris Mason else 1405a52d9a80SChris Mason check_page_locked(tree, page); 1406a52d9a80SChris Mason } while (bvec >= bio->bi_io_vec); 1407a52d9a80SChris Mason 1408a52d9a80SChris Mason bio_put(bio); 14090a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1410a52d9a80SChris Mason return 0; 14110a2118dfSJens Axboe #endif 1412a52d9a80SChris Mason } 1413a52d9a80SChris Mason 1414a52d9a80SChris Mason /* 1415a52d9a80SChris Mason * IO done from prepare_write is pretty simple, we just unlock 1416a52d9a80SChris Mason * the structs in the extent tree when done, and set the uptodate bits 1417a52d9a80SChris Mason * as appropriate. 1418a52d9a80SChris Mason */ 14190a2118dfSJens Axboe #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) 14200a2118dfSJens Axboe static void end_bio_extent_preparewrite(struct bio *bio, int err) 14210a2118dfSJens Axboe #else 1422a52d9a80SChris Mason static int end_bio_extent_preparewrite(struct bio *bio, 1423a52d9a80SChris Mason unsigned int bytes_done, int err) 14240a2118dfSJens Axboe #endif 1425a52d9a80SChris Mason { 1426a52d9a80SChris Mason const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1427a52d9a80SChris Mason struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; 1428a52d9a80SChris Mason struct extent_map_tree *tree = bio->bi_private; 1429a52d9a80SChris Mason u64 start; 1430a52d9a80SChris Mason u64 end; 1431a52d9a80SChris Mason 14320a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1433a52d9a80SChris Mason if (bio->bi_size) 1434a52d9a80SChris Mason return 1; 14350a2118dfSJens Axboe #endif 1436a52d9a80SChris Mason 1437a52d9a80SChris Mason do { 1438a52d9a80SChris Mason struct page *page = bvec->bv_page; 143935ebb934SChris Mason start = ((u64)page->index << PAGE_CACHE_SHIFT) + 144035ebb934SChris Mason bvec->bv_offset; 1441a52d9a80SChris Mason end = start + bvec->bv_len - 1; 1442a52d9a80SChris Mason 1443a52d9a80SChris Mason if (--bvec >= bio->bi_io_vec) 1444a52d9a80SChris Mason prefetchw(&bvec->bv_page->flags); 1445a52d9a80SChris Mason 1446a52d9a80SChris Mason if (uptodate) { 1447a52d9a80SChris Mason set_extent_uptodate(tree, start, end, GFP_ATOMIC); 1448a52d9a80SChris Mason } else { 1449a52d9a80SChris Mason ClearPageUptodate(page); 1450a52d9a80SChris Mason SetPageError(page); 1451a52d9a80SChris Mason } 1452a52d9a80SChris Mason 1453a52d9a80SChris Mason unlock_extent(tree, start, end, GFP_ATOMIC); 1454a52d9a80SChris Mason 1455a52d9a80SChris Mason } while (bvec >= bio->bi_io_vec); 1456a52d9a80SChris Mason 1457a52d9a80SChris Mason bio_put(bio); 14580a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1459a52d9a80SChris Mason return 0; 14600a2118dfSJens Axboe #endif 1461a52d9a80SChris Mason } 1462a52d9a80SChris Mason 1463a52d9a80SChris Mason static int submit_extent_page(int rw, struct extent_map_tree *tree, 1464a52d9a80SChris Mason struct page *page, sector_t sector, 1465a52d9a80SChris Mason size_t size, unsigned long offset, 1466a52d9a80SChris Mason struct block_device *bdev, 1467a52d9a80SChris Mason bio_end_io_t end_io_func) 1468a52d9a80SChris Mason { 1469a52d9a80SChris Mason struct bio *bio; 1470a52d9a80SChris Mason int ret = 0; 1471a52d9a80SChris Mason 1472a52d9a80SChris Mason bio = bio_alloc(GFP_NOIO, 1); 1473a52d9a80SChris Mason 1474a52d9a80SChris Mason bio->bi_sector = sector; 1475a52d9a80SChris Mason bio->bi_bdev = bdev; 1476a52d9a80SChris Mason bio->bi_io_vec[0].bv_page = page; 1477a52d9a80SChris Mason bio->bi_io_vec[0].bv_len = size; 1478a52d9a80SChris Mason bio->bi_io_vec[0].bv_offset = offset; 1479a52d9a80SChris Mason 1480a52d9a80SChris Mason bio->bi_vcnt = 1; 1481a52d9a80SChris Mason bio->bi_idx = 0; 1482a52d9a80SChris Mason bio->bi_size = size; 1483a52d9a80SChris Mason 1484a52d9a80SChris Mason bio->bi_end_io = end_io_func; 1485a52d9a80SChris Mason bio->bi_private = tree; 1486a52d9a80SChris Mason 1487a52d9a80SChris Mason bio_get(bio); 1488a52d9a80SChris Mason submit_bio(rw, bio); 1489a52d9a80SChris Mason 1490a52d9a80SChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) 1491a52d9a80SChris Mason ret = -EOPNOTSUPP; 1492a52d9a80SChris Mason 1493a52d9a80SChris Mason bio_put(bio); 1494a52d9a80SChris Mason return ret; 1495a52d9a80SChris Mason } 1496a52d9a80SChris Mason 1497b3cfa35aSChristoph Hellwig void set_page_extent_mapped(struct page *page) 1498b3cfa35aSChristoph Hellwig { 1499b3cfa35aSChristoph Hellwig if (!PagePrivate(page)) { 1500b3cfa35aSChristoph Hellwig SetPagePrivate(page); 1501b3cfa35aSChristoph Hellwig WARN_ON(!page->mapping->a_ops->invalidatepage); 150219c00ddcSChris Mason set_page_private(page, EXTENT_PAGE_PRIVATE); 1503b3cfa35aSChristoph Hellwig page_cache_get(page); 1504b3cfa35aSChristoph Hellwig } 1505b3cfa35aSChristoph Hellwig } 1506b3cfa35aSChristoph Hellwig 1507a52d9a80SChris Mason /* 1508a52d9a80SChris Mason * basic readpage implementation. Locked extent state structs are inserted 1509a52d9a80SChris Mason * into the tree that are removed when the IO is done (by the end_io 1510a52d9a80SChris Mason * handlers) 1511a52d9a80SChris Mason */ 1512a52d9a80SChris Mason int extent_read_full_page(struct extent_map_tree *tree, struct page *page, 1513a52d9a80SChris Mason get_extent_t *get_extent) 1514a52d9a80SChris Mason { 1515a52d9a80SChris Mason struct inode *inode = page->mapping->host; 151635ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1517a52d9a80SChris Mason u64 page_end = start + PAGE_CACHE_SIZE - 1; 1518a52d9a80SChris Mason u64 end; 1519a52d9a80SChris Mason u64 cur = start; 1520a52d9a80SChris Mason u64 extent_offset; 1521a52d9a80SChris Mason u64 last_byte = i_size_read(inode); 1522a52d9a80SChris Mason u64 block_start; 1523a52d9a80SChris Mason u64 cur_end; 1524a52d9a80SChris Mason sector_t sector; 1525a52d9a80SChris Mason struct extent_map *em; 1526a52d9a80SChris Mason struct block_device *bdev; 1527a52d9a80SChris Mason int ret; 1528a52d9a80SChris Mason int nr = 0; 1529a52d9a80SChris Mason size_t page_offset = 0; 1530a52d9a80SChris Mason size_t iosize; 1531a52d9a80SChris Mason size_t blocksize = inode->i_sb->s_blocksize; 1532a52d9a80SChris Mason 1533b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 1534a52d9a80SChris Mason 1535a52d9a80SChris Mason end = page_end; 1536a52d9a80SChris Mason lock_extent(tree, start, end, GFP_NOFS); 1537a52d9a80SChris Mason 1538a52d9a80SChris Mason while (cur <= end) { 1539a52d9a80SChris Mason if (cur >= last_byte) { 1540a52d9a80SChris Mason iosize = PAGE_CACHE_SIZE - page_offset; 1541a52d9a80SChris Mason zero_user_page(page, page_offset, iosize, KM_USER0); 1542a52d9a80SChris Mason set_extent_uptodate(tree, cur, cur + iosize - 1, 1543a52d9a80SChris Mason GFP_NOFS); 1544a52d9a80SChris Mason unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); 1545a52d9a80SChris Mason break; 1546a52d9a80SChris Mason } 1547a52d9a80SChris Mason em = get_extent(inode, page, page_offset, cur, end, 0); 1548a52d9a80SChris Mason if (IS_ERR(em) || !em) { 1549a52d9a80SChris Mason SetPageError(page); 1550a52d9a80SChris Mason unlock_extent(tree, cur, end, GFP_NOFS); 1551a52d9a80SChris Mason break; 1552a52d9a80SChris Mason } 1553a52d9a80SChris Mason 1554a52d9a80SChris Mason extent_offset = cur - em->start; 1555a52d9a80SChris Mason BUG_ON(em->end < cur); 1556a52d9a80SChris Mason BUG_ON(end < cur); 1557a52d9a80SChris Mason 1558a52d9a80SChris Mason iosize = min(em->end - cur, end - cur) + 1; 1559a52d9a80SChris Mason cur_end = min(em->end, end); 1560a52d9a80SChris Mason iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); 1561a52d9a80SChris Mason sector = (em->block_start + extent_offset) >> 9; 1562a52d9a80SChris Mason bdev = em->bdev; 1563a52d9a80SChris Mason block_start = em->block_start; 1564a52d9a80SChris Mason free_extent_map(em); 1565a52d9a80SChris Mason em = NULL; 1566a52d9a80SChris Mason 1567a52d9a80SChris Mason /* we've found a hole, just zero and go on */ 15685f39d397SChris Mason if (block_start == EXTENT_MAP_HOLE) { 1569a52d9a80SChris Mason zero_user_page(page, page_offset, iosize, KM_USER0); 1570a52d9a80SChris Mason set_extent_uptodate(tree, cur, cur + iosize - 1, 1571a52d9a80SChris Mason GFP_NOFS); 1572a52d9a80SChris Mason unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); 1573a52d9a80SChris Mason cur = cur + iosize; 1574a52d9a80SChris Mason page_offset += iosize; 1575a52d9a80SChris Mason continue; 1576a52d9a80SChris Mason } 1577a52d9a80SChris Mason /* the get_extent function already copied into the page */ 1578a52d9a80SChris Mason if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) { 1579a52d9a80SChris Mason unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); 1580a52d9a80SChris Mason cur = cur + iosize; 1581a52d9a80SChris Mason page_offset += iosize; 1582a52d9a80SChris Mason continue; 1583a52d9a80SChris Mason } 1584a52d9a80SChris Mason 158507157aacSChris Mason ret = 0; 158607157aacSChris Mason if (tree->ops && tree->ops->readpage_io_hook) { 158707157aacSChris Mason ret = tree->ops->readpage_io_hook(page, cur, 158807157aacSChris Mason cur + iosize - 1); 158907157aacSChris Mason } 159007157aacSChris Mason if (!ret) { 1591a52d9a80SChris Mason ret = submit_extent_page(READ, tree, page, 159207157aacSChris Mason sector, iosize, page_offset, 159307157aacSChris Mason bdev, end_bio_extent_readpage); 159407157aacSChris Mason } 1595a52d9a80SChris Mason if (ret) 1596a52d9a80SChris Mason SetPageError(page); 1597a52d9a80SChris Mason cur = cur + iosize; 1598a52d9a80SChris Mason page_offset += iosize; 1599a52d9a80SChris Mason nr++; 1600a52d9a80SChris Mason } 1601a52d9a80SChris Mason if (!nr) { 1602a52d9a80SChris Mason if (!PageError(page)) 1603a52d9a80SChris Mason SetPageUptodate(page); 1604a52d9a80SChris Mason unlock_page(page); 1605a52d9a80SChris Mason } 1606a52d9a80SChris Mason return 0; 1607a52d9a80SChris Mason } 1608a52d9a80SChris Mason EXPORT_SYMBOL(extent_read_full_page); 1609a52d9a80SChris Mason 1610a52d9a80SChris Mason /* 1611a52d9a80SChris Mason * the writepage semantics are similar to regular writepage. extent 1612a52d9a80SChris Mason * records are inserted to lock ranges in the tree, and as dirty areas 1613a52d9a80SChris Mason * are found, they are marked writeback. Then the lock bits are removed 1614a52d9a80SChris Mason * and the end_io handler clears the writeback ranges 1615a52d9a80SChris Mason */ 1616a52d9a80SChris Mason int extent_write_full_page(struct extent_map_tree *tree, struct page *page, 1617a52d9a80SChris Mason get_extent_t *get_extent, 1618a52d9a80SChris Mason struct writeback_control *wbc) 1619a52d9a80SChris Mason { 1620a52d9a80SChris Mason struct inode *inode = page->mapping->host; 162135ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1622a52d9a80SChris Mason u64 page_end = start + PAGE_CACHE_SIZE - 1; 1623a52d9a80SChris Mason u64 end; 1624a52d9a80SChris Mason u64 cur = start; 1625a52d9a80SChris Mason u64 extent_offset; 1626a52d9a80SChris Mason u64 last_byte = i_size_read(inode); 1627a52d9a80SChris Mason u64 block_start; 1628179e29e4SChris Mason u64 iosize; 1629a52d9a80SChris Mason sector_t sector; 1630a52d9a80SChris Mason struct extent_map *em; 1631a52d9a80SChris Mason struct block_device *bdev; 1632a52d9a80SChris Mason int ret; 1633a52d9a80SChris Mason int nr = 0; 1634a52d9a80SChris Mason size_t page_offset = 0; 1635a52d9a80SChris Mason size_t blocksize; 1636a52d9a80SChris Mason loff_t i_size = i_size_read(inode); 1637a52d9a80SChris Mason unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; 1638b888db2bSChris Mason u64 nr_delalloc; 1639b888db2bSChris Mason u64 delalloc_end; 1640a52d9a80SChris Mason 1641b888db2bSChris Mason WARN_ON(!PageLocked(page)); 1642a52d9a80SChris Mason if (page->index > end_index) { 1643a52d9a80SChris Mason clear_extent_dirty(tree, start, page_end, GFP_NOFS); 1644a52d9a80SChris Mason unlock_page(page); 1645a52d9a80SChris Mason return 0; 1646a52d9a80SChris Mason } 1647a52d9a80SChris Mason 1648a52d9a80SChris Mason if (page->index == end_index) { 1649a52d9a80SChris Mason size_t offset = i_size & (PAGE_CACHE_SIZE - 1); 1650a52d9a80SChris Mason zero_user_page(page, offset, 1651a52d9a80SChris Mason PAGE_CACHE_SIZE - offset, KM_USER0); 1652a52d9a80SChris Mason } 1653a52d9a80SChris Mason 1654b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 1655a52d9a80SChris Mason 1656a52d9a80SChris Mason lock_extent(tree, start, page_end, GFP_NOFS); 1657b888db2bSChris Mason nr_delalloc = find_lock_delalloc_range(tree, start, page_end + 1, 1658b888db2bSChris Mason &delalloc_end, 1659b888db2bSChris Mason 128 * 1024 * 1024); 1660b888db2bSChris Mason if (nr_delalloc) { 166107157aacSChris Mason tree->ops->fill_delalloc(inode, start, delalloc_end); 1662b888db2bSChris Mason if (delalloc_end >= page_end + 1) { 1663b888db2bSChris Mason clear_extent_bit(tree, page_end + 1, delalloc_end, 1664b888db2bSChris Mason EXTENT_LOCKED | EXTENT_DELALLOC, 1665b888db2bSChris Mason 1, 0, GFP_NOFS); 1666b888db2bSChris Mason } 1667b888db2bSChris Mason clear_extent_bit(tree, start, page_end, EXTENT_DELALLOC, 1668b888db2bSChris Mason 0, 0, GFP_NOFS); 1669b888db2bSChris Mason if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) { 1670b888db2bSChris Mason printk("found delalloc bits after clear extent_bit\n"); 1671b888db2bSChris Mason } 1672b888db2bSChris Mason } else if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) { 1673b888db2bSChris Mason printk("found delalloc bits after find_delalloc_range returns 0\n"); 1674b888db2bSChris Mason } 1675b888db2bSChris Mason 1676b888db2bSChris Mason end = page_end; 1677b888db2bSChris Mason if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) { 1678b888db2bSChris Mason printk("found delalloc bits after lock_extent\n"); 1679b888db2bSChris Mason } 1680a52d9a80SChris Mason 1681a52d9a80SChris Mason if (last_byte <= start) { 1682a52d9a80SChris Mason clear_extent_dirty(tree, start, page_end, GFP_NOFS); 1683a52d9a80SChris Mason goto done; 1684a52d9a80SChris Mason } 1685a52d9a80SChris Mason 1686a52d9a80SChris Mason set_extent_uptodate(tree, start, page_end, GFP_NOFS); 1687a52d9a80SChris Mason blocksize = inode->i_sb->s_blocksize; 1688a52d9a80SChris Mason 1689a52d9a80SChris Mason while (cur <= end) { 1690a52d9a80SChris Mason if (cur >= last_byte) { 1691a52d9a80SChris Mason clear_extent_dirty(tree, cur, page_end, GFP_NOFS); 1692a52d9a80SChris Mason break; 1693a52d9a80SChris Mason } 1694179e29e4SChris Mason em = get_extent(inode, page, page_offset, cur, end, 1); 1695a52d9a80SChris Mason if (IS_ERR(em) || !em) { 1696a52d9a80SChris Mason SetPageError(page); 1697a52d9a80SChris Mason break; 1698a52d9a80SChris Mason } 1699a52d9a80SChris Mason 1700a52d9a80SChris Mason extent_offset = cur - em->start; 1701a52d9a80SChris Mason BUG_ON(em->end < cur); 1702a52d9a80SChris Mason BUG_ON(end < cur); 1703a52d9a80SChris Mason iosize = min(em->end - cur, end - cur) + 1; 1704a52d9a80SChris Mason iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); 1705a52d9a80SChris Mason sector = (em->block_start + extent_offset) >> 9; 1706a52d9a80SChris Mason bdev = em->bdev; 1707a52d9a80SChris Mason block_start = em->block_start; 1708a52d9a80SChris Mason free_extent_map(em); 1709a52d9a80SChris Mason em = NULL; 1710a52d9a80SChris Mason 17115f39d397SChris Mason if (block_start == EXTENT_MAP_HOLE || 17125f39d397SChris Mason block_start == EXTENT_MAP_INLINE) { 1713a52d9a80SChris Mason clear_extent_dirty(tree, cur, 1714a52d9a80SChris Mason cur + iosize - 1, GFP_NOFS); 1715a52d9a80SChris Mason cur = cur + iosize; 1716a52d9a80SChris Mason page_offset += iosize; 1717a52d9a80SChris Mason continue; 1718a52d9a80SChris Mason } 1719a52d9a80SChris Mason 1720a52d9a80SChris Mason /* leave this out until we have a page_mkwrite call */ 1721a52d9a80SChris Mason if (0 && !test_range_bit(tree, cur, cur + iosize - 1, 1722a52d9a80SChris Mason EXTENT_DIRTY, 0)) { 1723a52d9a80SChris Mason cur = cur + iosize; 1724a52d9a80SChris Mason page_offset += iosize; 1725a52d9a80SChris Mason continue; 1726a52d9a80SChris Mason } 1727a52d9a80SChris Mason clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS); 1728b06355f0SChristoph Hellwig if (tree->ops && tree->ops->writepage_io_hook) { 1729b06355f0SChristoph Hellwig ret = tree->ops->writepage_io_hook(page, cur, 1730b06355f0SChristoph Hellwig cur + iosize - 1); 1731b06355f0SChristoph Hellwig } else { 1732b06355f0SChristoph Hellwig ret = 0; 1733b06355f0SChristoph Hellwig } 173407157aacSChris Mason if (ret) 173507157aacSChris Mason SetPageError(page); 173607157aacSChris Mason else { 1737a52d9a80SChris Mason set_range_writeback(tree, cur, cur + iosize - 1); 173807157aacSChris Mason ret = submit_extent_page(WRITE, tree, page, sector, 173907157aacSChris Mason iosize, page_offset, bdev, 1740a52d9a80SChris Mason end_bio_extent_writepage); 1741a52d9a80SChris Mason if (ret) 1742a52d9a80SChris Mason SetPageError(page); 174307157aacSChris Mason } 1744a52d9a80SChris Mason cur = cur + iosize; 1745a52d9a80SChris Mason page_offset += iosize; 1746a52d9a80SChris Mason nr++; 1747a52d9a80SChris Mason } 1748a52d9a80SChris Mason done: 1749a52d9a80SChris Mason unlock_extent(tree, start, page_end, GFP_NOFS); 1750a52d9a80SChris Mason unlock_page(page); 1751a52d9a80SChris Mason return 0; 1752a52d9a80SChris Mason } 1753a52d9a80SChris Mason EXPORT_SYMBOL(extent_write_full_page); 1754a52d9a80SChris Mason 1755a52d9a80SChris Mason /* 1756a52d9a80SChris Mason * basic invalidatepage code, this waits on any locked or writeback 1757a52d9a80SChris Mason * ranges corresponding to the page, and then deletes any extent state 1758a52d9a80SChris Mason * records from the tree 1759a52d9a80SChris Mason */ 1760a52d9a80SChris Mason int extent_invalidatepage(struct extent_map_tree *tree, 1761a52d9a80SChris Mason struct page *page, unsigned long offset) 1762a52d9a80SChris Mason { 176335ebb934SChris Mason u64 start = ((u64)page->index << PAGE_CACHE_SHIFT); 1764a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1765a52d9a80SChris Mason size_t blocksize = page->mapping->host->i_sb->s_blocksize; 1766a52d9a80SChris Mason 1767a52d9a80SChris Mason start += (offset + blocksize -1) & ~(blocksize - 1); 1768a52d9a80SChris Mason if (start > end) 1769a52d9a80SChris Mason return 0; 1770a52d9a80SChris Mason 1771a52d9a80SChris Mason lock_extent(tree, start, end, GFP_NOFS); 1772a52d9a80SChris Mason wait_on_extent_writeback(tree, start, end); 17732bf5a725SChris Mason clear_extent_bit(tree, start, end, 17742bf5a725SChris Mason EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, 1775a52d9a80SChris Mason 1, 1, GFP_NOFS); 1776a52d9a80SChris Mason return 0; 1777a52d9a80SChris Mason } 1778a52d9a80SChris Mason EXPORT_SYMBOL(extent_invalidatepage); 1779a52d9a80SChris Mason 1780a52d9a80SChris Mason /* 1781a52d9a80SChris Mason * simple commit_write call, set_range_dirty is used to mark both 1782a52d9a80SChris Mason * the pages and the extent records as dirty 1783a52d9a80SChris Mason */ 1784a52d9a80SChris Mason int extent_commit_write(struct extent_map_tree *tree, 1785a52d9a80SChris Mason struct inode *inode, struct page *page, 1786a52d9a80SChris Mason unsigned from, unsigned to) 1787a52d9a80SChris Mason { 1788a52d9a80SChris Mason loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; 1789a52d9a80SChris Mason 1790b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 1791a52d9a80SChris Mason set_page_dirty(page); 1792a52d9a80SChris Mason 1793a52d9a80SChris Mason if (pos > inode->i_size) { 1794a52d9a80SChris Mason i_size_write(inode, pos); 1795a52d9a80SChris Mason mark_inode_dirty(inode); 1796a52d9a80SChris Mason } 1797a52d9a80SChris Mason return 0; 1798a52d9a80SChris Mason } 1799a52d9a80SChris Mason EXPORT_SYMBOL(extent_commit_write); 1800a52d9a80SChris Mason 1801a52d9a80SChris Mason int extent_prepare_write(struct extent_map_tree *tree, 1802a52d9a80SChris Mason struct inode *inode, struct page *page, 1803a52d9a80SChris Mason unsigned from, unsigned to, get_extent_t *get_extent) 1804a52d9a80SChris Mason { 180535ebb934SChris Mason u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; 1806a52d9a80SChris Mason u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 1807a52d9a80SChris Mason u64 block_start; 1808a52d9a80SChris Mason u64 orig_block_start; 1809a52d9a80SChris Mason u64 block_end; 1810a52d9a80SChris Mason u64 cur_end; 1811a52d9a80SChris Mason struct extent_map *em; 1812a52d9a80SChris Mason unsigned blocksize = 1 << inode->i_blkbits; 1813a52d9a80SChris Mason size_t page_offset = 0; 1814a52d9a80SChris Mason size_t block_off_start; 1815a52d9a80SChris Mason size_t block_off_end; 1816a52d9a80SChris Mason int err = 0; 1817a52d9a80SChris Mason int iocount = 0; 1818a52d9a80SChris Mason int ret = 0; 1819a52d9a80SChris Mason int isnew; 1820a52d9a80SChris Mason 1821b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 1822b3cfa35aSChristoph Hellwig 1823a52d9a80SChris Mason block_start = (page_start + from) & ~((u64)blocksize - 1); 1824a52d9a80SChris Mason block_end = (page_start + to - 1) | (blocksize - 1); 1825a52d9a80SChris Mason orig_block_start = block_start; 1826a52d9a80SChris Mason 1827a52d9a80SChris Mason lock_extent(tree, page_start, page_end, GFP_NOFS); 1828a52d9a80SChris Mason while(block_start <= block_end) { 1829a52d9a80SChris Mason em = get_extent(inode, page, page_offset, block_start, 1830a52d9a80SChris Mason block_end, 1); 1831a52d9a80SChris Mason if (IS_ERR(em) || !em) { 1832a52d9a80SChris Mason goto err; 1833a52d9a80SChris Mason } 1834a52d9a80SChris Mason cur_end = min(block_end, em->end); 1835a52d9a80SChris Mason block_off_start = block_start & (PAGE_CACHE_SIZE - 1); 1836a52d9a80SChris Mason block_off_end = block_off_start + blocksize; 1837a52d9a80SChris Mason isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS); 1838a52d9a80SChris Mason 1839a52d9a80SChris Mason if (!PageUptodate(page) && isnew && 1840a52d9a80SChris Mason (block_off_end > to || block_off_start < from)) { 1841a52d9a80SChris Mason void *kaddr; 1842a52d9a80SChris Mason 1843a52d9a80SChris Mason kaddr = kmap_atomic(page, KM_USER0); 1844a52d9a80SChris Mason if (block_off_end > to) 1845a52d9a80SChris Mason memset(kaddr + to, 0, block_off_end - to); 1846a52d9a80SChris Mason if (block_off_start < from) 1847a52d9a80SChris Mason memset(kaddr + block_off_start, 0, 1848a52d9a80SChris Mason from - block_off_start); 1849a52d9a80SChris Mason flush_dcache_page(page); 1850a52d9a80SChris Mason kunmap_atomic(kaddr, KM_USER0); 1851a52d9a80SChris Mason } 1852a52d9a80SChris Mason if (!isnew && !PageUptodate(page) && 1853a52d9a80SChris Mason (block_off_end > to || block_off_start < from) && 1854a52d9a80SChris Mason !test_range_bit(tree, block_start, cur_end, 1855a52d9a80SChris Mason EXTENT_UPTODATE, 1)) { 1856a52d9a80SChris Mason u64 sector; 1857a52d9a80SChris Mason u64 extent_offset = block_start - em->start; 1858a52d9a80SChris Mason size_t iosize; 1859a52d9a80SChris Mason sector = (em->block_start + extent_offset) >> 9; 1860a52d9a80SChris Mason iosize = (cur_end - block_start + blocksize - 1) & 1861a52d9a80SChris Mason ~((u64)blocksize - 1); 1862a52d9a80SChris Mason /* 1863a52d9a80SChris Mason * we've already got the extent locked, but we 1864a52d9a80SChris Mason * need to split the state such that our end_bio 1865a52d9a80SChris Mason * handler can clear the lock. 1866a52d9a80SChris Mason */ 1867a52d9a80SChris Mason set_extent_bit(tree, block_start, 1868a52d9a80SChris Mason block_start + iosize - 1, 1869a52d9a80SChris Mason EXTENT_LOCKED, 0, NULL, GFP_NOFS); 1870a52d9a80SChris Mason ret = submit_extent_page(READ, tree, page, 1871a52d9a80SChris Mason sector, iosize, page_offset, em->bdev, 1872a52d9a80SChris Mason end_bio_extent_preparewrite); 1873a52d9a80SChris Mason iocount++; 1874a52d9a80SChris Mason block_start = block_start + iosize; 1875a52d9a80SChris Mason } else { 1876a52d9a80SChris Mason set_extent_uptodate(tree, block_start, cur_end, 1877a52d9a80SChris Mason GFP_NOFS); 1878a52d9a80SChris Mason unlock_extent(tree, block_start, cur_end, GFP_NOFS); 1879a52d9a80SChris Mason block_start = cur_end + 1; 1880a52d9a80SChris Mason } 1881a52d9a80SChris Mason page_offset = block_start & (PAGE_CACHE_SIZE - 1); 1882a52d9a80SChris Mason free_extent_map(em); 1883a52d9a80SChris Mason } 1884a52d9a80SChris Mason if (iocount) { 1885a52d9a80SChris Mason wait_extent_bit(tree, orig_block_start, 1886a52d9a80SChris Mason block_end, EXTENT_LOCKED); 1887a52d9a80SChris Mason } 1888a52d9a80SChris Mason check_page_uptodate(tree, page); 1889a52d9a80SChris Mason err: 1890a52d9a80SChris Mason /* FIXME, zero out newly allocated blocks on error */ 1891a52d9a80SChris Mason return err; 1892a52d9a80SChris Mason } 1893a52d9a80SChris Mason EXPORT_SYMBOL(extent_prepare_write); 1894a52d9a80SChris Mason 1895a52d9a80SChris Mason /* 1896a52d9a80SChris Mason * a helper for releasepage. As long as there are no locked extents 1897a52d9a80SChris Mason * in the range corresponding to the page, both state records and extent 1898a52d9a80SChris Mason * map records are removed 1899a52d9a80SChris Mason */ 1900a52d9a80SChris Mason int try_release_extent_mapping(struct extent_map_tree *tree, struct page *page) 1901a52d9a80SChris Mason { 1902a52d9a80SChris Mason struct extent_map *em; 190335ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1904a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1905a52d9a80SChris Mason u64 orig_start = start; 1906b888db2bSChris Mason int ret = 1; 1907a52d9a80SChris Mason 1908a52d9a80SChris Mason while (start <= end) { 1909a52d9a80SChris Mason em = lookup_extent_mapping(tree, start, end); 1910a52d9a80SChris Mason if (!em || IS_ERR(em)) 1911a52d9a80SChris Mason break; 1912b888db2bSChris Mason if (!test_range_bit(tree, em->start, em->end, 1913a52d9a80SChris Mason EXTENT_LOCKED, 0)) { 1914a52d9a80SChris Mason remove_extent_mapping(tree, em); 1915a52d9a80SChris Mason /* once for the rb tree */ 1916a52d9a80SChris Mason free_extent_map(em); 1917b888db2bSChris Mason } 1918b888db2bSChris Mason start = em->end + 1; 1919a52d9a80SChris Mason /* once for us */ 1920a52d9a80SChris Mason free_extent_map(em); 1921a52d9a80SChris Mason } 1922b888db2bSChris Mason if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0)) 1923b888db2bSChris Mason ret = 0; 1924b888db2bSChris Mason else 1925a52d9a80SChris Mason clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE, 1926a52d9a80SChris Mason 1, 1, GFP_NOFS); 1927b888db2bSChris Mason return ret; 1928a52d9a80SChris Mason } 1929a52d9a80SChris Mason EXPORT_SYMBOL(try_release_extent_mapping); 1930a52d9a80SChris Mason 1931d396c6f5SChristoph Hellwig sector_t extent_bmap(struct address_space *mapping, sector_t iblock, 1932d396c6f5SChristoph Hellwig get_extent_t *get_extent) 1933d396c6f5SChristoph Hellwig { 1934d396c6f5SChristoph Hellwig struct inode *inode = mapping->host; 1935d396c6f5SChristoph Hellwig u64 start = iblock << inode->i_blkbits; 1936d396c6f5SChristoph Hellwig u64 end = start + (1 << inode->i_blkbits) - 1; 1937c67cda17SYan sector_t sector = 0; 1938d396c6f5SChristoph Hellwig struct extent_map *em; 1939d396c6f5SChristoph Hellwig 1940d396c6f5SChristoph Hellwig em = get_extent(inode, NULL, 0, start, end, 0); 1941d396c6f5SChristoph Hellwig if (!em || IS_ERR(em)) 1942d396c6f5SChristoph Hellwig return 0; 1943d396c6f5SChristoph Hellwig 1944d396c6f5SChristoph Hellwig if (em->block_start == EXTENT_MAP_INLINE || 19455f39d397SChris Mason em->block_start == EXTENT_MAP_HOLE) 1946c67cda17SYan goto out; 1947d396c6f5SChristoph Hellwig 1948c67cda17SYan sector = (em->block_start + start - em->start) >> inode->i_blkbits; 1949c67cda17SYan out: 1950c67cda17SYan free_extent_map(em); 1951c67cda17SYan return sector; 1952d396c6f5SChristoph Hellwig } 19535f39d397SChris Mason 19544dc11904SChris Mason static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb) 19556d36dcd4SChris Mason { 19564dc11904SChris Mason if (list_empty(&eb->lru)) { 19574dc11904SChris Mason extent_buffer_get(eb); 19584dc11904SChris Mason list_add(&eb->lru, &tree->buffer_lru); 19594dc11904SChris Mason tree->lru_size++; 19604dc11904SChris Mason if (tree->lru_size >= BUFFER_LRU_MAX) { 19614dc11904SChris Mason struct extent_buffer *rm; 19624dc11904SChris Mason rm = list_entry(tree->buffer_lru.prev, 19634dc11904SChris Mason struct extent_buffer, lru); 19644dc11904SChris Mason tree->lru_size--; 19654dc11904SChris Mason list_del(&rm->lru); 19664dc11904SChris Mason free_extent_buffer(rm); 19676d36dcd4SChris Mason } 19684dc11904SChris Mason } else 19694dc11904SChris Mason list_move(&eb->lru, &tree->buffer_lru); 19704dc11904SChris Mason return 0; 19716d36dcd4SChris Mason } 19724dc11904SChris Mason static struct extent_buffer *find_lru(struct extent_map_tree *tree, 19734dc11904SChris Mason u64 start, unsigned long len) 19744dc11904SChris Mason { 19754dc11904SChris Mason struct list_head *lru = &tree->buffer_lru; 19764dc11904SChris Mason struct list_head *cur = lru->next; 19774dc11904SChris Mason struct extent_buffer *eb; 1978f510cfecSChris Mason 19794dc11904SChris Mason if (list_empty(lru)) 19804dc11904SChris Mason return NULL; 19814dc11904SChris Mason 19824dc11904SChris Mason do { 19834dc11904SChris Mason eb = list_entry(cur, struct extent_buffer, lru); 19844dc11904SChris Mason if (eb->start == start && eb->len == len) { 19854dc11904SChris Mason extent_buffer_get(eb); 1986f510cfecSChris Mason return eb; 19876d36dcd4SChris Mason } 19884dc11904SChris Mason cur = cur->next; 19894dc11904SChris Mason } while (cur != lru); 19904dc11904SChris Mason return NULL; 19916d36dcd4SChris Mason } 19926d36dcd4SChris Mason 1993db94535dSChris Mason static inline unsigned long num_extent_pages(u64 start, u64 len) 1994db94535dSChris Mason { 1995db94535dSChris Mason return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - 1996db94535dSChris Mason (start >> PAGE_CACHE_SHIFT); 1997db94535dSChris Mason } 19984dc11904SChris Mason 19994dc11904SChris Mason static inline struct page *extent_buffer_page(struct extent_buffer *eb, 20004dc11904SChris Mason unsigned long i) 20014dc11904SChris Mason { 20024dc11904SChris Mason struct page *p; 20033685f791SChris Mason struct address_space *mapping; 20044dc11904SChris Mason 20054dc11904SChris Mason if (i == 0) 2006810191ffSChris Mason return eb->first_page; 20074dc11904SChris Mason i += eb->start >> PAGE_CACHE_SHIFT; 20083685f791SChris Mason mapping = eb->first_page->mapping; 20093685f791SChris Mason read_lock_irq(&mapping->tree_lock); 20103685f791SChris Mason p = radix_tree_lookup(&mapping->page_tree, i); 20113685f791SChris Mason read_unlock_irq(&mapping->tree_lock); 20124dc11904SChris Mason return p; 20134dc11904SChris Mason } 20144dc11904SChris Mason 20154dc11904SChris Mason static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree, 20164dc11904SChris Mason u64 start, 20174dc11904SChris Mason unsigned long len, 20184dc11904SChris Mason gfp_t mask) 20194dc11904SChris Mason { 20204dc11904SChris Mason struct extent_buffer *eb = NULL; 20214dc11904SChris Mason 20224dc11904SChris Mason spin_lock(&tree->lru_lock); 20234dc11904SChris Mason eb = find_lru(tree, start, len); 202419c00ddcSChris Mason if (eb) { 20254dc11904SChris Mason goto lru_add; 202619c00ddcSChris Mason } 20274dc11904SChris Mason spin_unlock(&tree->lru_lock); 20284dc11904SChris Mason 20294dc11904SChris Mason if (eb) { 20304dc11904SChris Mason memset(eb, 0, sizeof(*eb)); 20314dc11904SChris Mason } else { 20324dc11904SChris Mason eb = kmem_cache_zalloc(extent_buffer_cache, mask); 20334dc11904SChris Mason } 20344dc11904SChris Mason INIT_LIST_HEAD(&eb->lru); 20354dc11904SChris Mason eb->start = start; 20364dc11904SChris Mason eb->len = len; 20374dc11904SChris Mason atomic_set(&eb->refs, 1); 20384dc11904SChris Mason 20394dc11904SChris Mason spin_lock(&tree->lru_lock); 20404dc11904SChris Mason lru_add: 20414dc11904SChris Mason add_lru(tree, eb); 20424dc11904SChris Mason spin_unlock(&tree->lru_lock); 20434dc11904SChris Mason return eb; 20444dc11904SChris Mason } 20454dc11904SChris Mason 20464dc11904SChris Mason static void __free_extent_buffer(struct extent_buffer *eb) 20474dc11904SChris Mason { 20484dc11904SChris Mason kmem_cache_free(extent_buffer_cache, eb); 20494dc11904SChris Mason } 20504dc11904SChris Mason 20515f39d397SChris Mason struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree, 20525f39d397SChris Mason u64 start, unsigned long len, 205319c00ddcSChris Mason struct page *page0, 20545f39d397SChris Mason gfp_t mask) 20555f39d397SChris Mason { 2056db94535dSChris Mason unsigned long num_pages = num_extent_pages(start, len); 20575f39d397SChris Mason unsigned long i; 20585f39d397SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 20595f39d397SChris Mason struct extent_buffer *eb; 20605f39d397SChris Mason struct page *p; 20615f39d397SChris Mason struct address_space *mapping = tree->mapping; 206265555a06SYan int uptodate = 1; 20635f39d397SChris Mason 20644dc11904SChris Mason eb = __alloc_extent_buffer(tree, start, len, mask); 20655f39d397SChris Mason if (!eb || IS_ERR(eb)) 20665f39d397SChris Mason return NULL; 20675f39d397SChris Mason 20684dc11904SChris Mason if (eb->flags & EXTENT_BUFFER_FILLED) 20694dc11904SChris Mason return eb; 20705f39d397SChris Mason 207119c00ddcSChris Mason if (page0) { 207219c00ddcSChris Mason eb->first_page = page0; 207319c00ddcSChris Mason i = 1; 207419c00ddcSChris Mason index++; 207519c00ddcSChris Mason page_cache_get(page0); 2076ff79f819SChris Mason mark_page_accessed(page0); 207719c00ddcSChris Mason set_page_extent_mapped(page0); 207819c00ddcSChris Mason set_page_private(page0, EXTENT_PAGE_PRIVATE_FIRST_PAGE | 207919c00ddcSChris Mason len << 2); 208019c00ddcSChris Mason } else { 208119c00ddcSChris Mason i = 0; 208219c00ddcSChris Mason } 208319c00ddcSChris Mason for (; i < num_pages; i++, index++) { 20845f39d397SChris Mason p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM); 20856d36dcd4SChris Mason if (!p) { 2086db94535dSChris Mason WARN_ON(1); 20876d36dcd4SChris Mason /* make sure the free only frees the pages we've 20886d36dcd4SChris Mason * grabbed a reference on 20896d36dcd4SChris Mason */ 20906d36dcd4SChris Mason eb->len = i << PAGE_CACHE_SHIFT; 20916d36dcd4SChris Mason eb->start &= ~((u64)PAGE_CACHE_SIZE - 1); 20925f39d397SChris Mason goto fail; 20936d36dcd4SChris Mason } 2094f510cfecSChris Mason set_page_extent_mapped(p); 2095ff79f819SChris Mason mark_page_accessed(p); 209619c00ddcSChris Mason if (i == 0) { 2097810191ffSChris Mason eb->first_page = p; 209819c00ddcSChris Mason set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE | 209919c00ddcSChris Mason len << 2); 210019c00ddcSChris Mason } else { 210119c00ddcSChris Mason set_page_private(p, EXTENT_PAGE_PRIVATE); 210219c00ddcSChris Mason } 21035f39d397SChris Mason if (!PageUptodate(p)) 21045f39d397SChris Mason uptodate = 0; 21055f39d397SChris Mason unlock_page(p); 21065f39d397SChris Mason } 21075f39d397SChris Mason if (uptodate) 21085f39d397SChris Mason eb->flags |= EXTENT_UPTODATE; 21094dc11904SChris Mason eb->flags |= EXTENT_BUFFER_FILLED; 21105f39d397SChris Mason return eb; 21115f39d397SChris Mason fail: 21125f39d397SChris Mason free_extent_buffer(eb); 21135f39d397SChris Mason return NULL; 21145f39d397SChris Mason } 21155f39d397SChris Mason EXPORT_SYMBOL(alloc_extent_buffer); 21165f39d397SChris Mason 21175f39d397SChris Mason struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree, 21185f39d397SChris Mason u64 start, unsigned long len, 21195f39d397SChris Mason gfp_t mask) 21205f39d397SChris Mason { 2121db94535dSChris Mason unsigned long num_pages = num_extent_pages(start, len); 212219c00ddcSChris Mason unsigned long i; unsigned long index = start >> PAGE_CACHE_SHIFT; 21235f39d397SChris Mason struct extent_buffer *eb; 21245f39d397SChris Mason struct page *p; 21255f39d397SChris Mason struct address_space *mapping = tree->mapping; 212614048ed0SChris Mason int uptodate = 1; 21275f39d397SChris Mason 21284dc11904SChris Mason eb = __alloc_extent_buffer(tree, start, len, mask); 21295f39d397SChris Mason if (!eb || IS_ERR(eb)) 21305f39d397SChris Mason return NULL; 21315f39d397SChris Mason 21324dc11904SChris Mason if (eb->flags & EXTENT_BUFFER_FILLED) 21334dc11904SChris Mason return eb; 21345f39d397SChris Mason 21355f39d397SChris Mason for (i = 0; i < num_pages; i++, index++) { 213614048ed0SChris Mason p = find_lock_page(mapping, index); 21376d36dcd4SChris Mason if (!p) { 21386d36dcd4SChris Mason /* make sure the free only frees the pages we've 21396d36dcd4SChris Mason * grabbed a reference on 21406d36dcd4SChris Mason */ 21416d36dcd4SChris Mason eb->len = i << PAGE_CACHE_SHIFT; 21426d36dcd4SChris Mason eb->start &= ~((u64)PAGE_CACHE_SIZE - 1); 21435f39d397SChris Mason goto fail; 21446d36dcd4SChris Mason } 2145f510cfecSChris Mason set_page_extent_mapped(p); 2146ff79f819SChris Mason mark_page_accessed(p); 214719c00ddcSChris Mason 214819c00ddcSChris Mason if (i == 0) { 2149810191ffSChris Mason eb->first_page = p; 215019c00ddcSChris Mason set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE | 215119c00ddcSChris Mason len << 2); 215219c00ddcSChris Mason } else { 215319c00ddcSChris Mason set_page_private(p, EXTENT_PAGE_PRIVATE); 215419c00ddcSChris Mason } 215519c00ddcSChris Mason 215614048ed0SChris Mason if (!PageUptodate(p)) 215714048ed0SChris Mason uptodate = 0; 215814048ed0SChris Mason unlock_page(p); 21595f39d397SChris Mason } 216014048ed0SChris Mason if (uptodate) 216114048ed0SChris Mason eb->flags |= EXTENT_UPTODATE; 21624dc11904SChris Mason eb->flags |= EXTENT_BUFFER_FILLED; 21635f39d397SChris Mason return eb; 21645f39d397SChris Mason fail: 21655f39d397SChris Mason free_extent_buffer(eb); 21665f39d397SChris Mason return NULL; 21675f39d397SChris Mason } 21685f39d397SChris Mason EXPORT_SYMBOL(find_extent_buffer); 21695f39d397SChris Mason 21705f39d397SChris Mason void free_extent_buffer(struct extent_buffer *eb) 21715f39d397SChris Mason { 21725f39d397SChris Mason unsigned long i; 21735f39d397SChris Mason unsigned long num_pages; 21745f39d397SChris Mason 21755f39d397SChris Mason if (!eb) 21765f39d397SChris Mason return; 21775f39d397SChris Mason 21785f39d397SChris Mason if (!atomic_dec_and_test(&eb->refs)) 21795f39d397SChris Mason return; 21805f39d397SChris Mason 2181db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 21825f39d397SChris Mason 218309e71a32SChris Mason for (i = 0; i < num_pages; i++) { 21846d36dcd4SChris Mason page_cache_release(extent_buffer_page(eb, i)); 21855f39d397SChris Mason } 21866d36dcd4SChris Mason __free_extent_buffer(eb); 21875f39d397SChris Mason } 21885f39d397SChris Mason EXPORT_SYMBOL(free_extent_buffer); 21895f39d397SChris Mason 21905f39d397SChris Mason int clear_extent_buffer_dirty(struct extent_map_tree *tree, 21915f39d397SChris Mason struct extent_buffer *eb) 21925f39d397SChris Mason { 21935f39d397SChris Mason int set; 21945f39d397SChris Mason unsigned long i; 21955f39d397SChris Mason unsigned long num_pages; 21965f39d397SChris Mason struct page *page; 21975f39d397SChris Mason 21985f39d397SChris Mason u64 start = eb->start; 21995f39d397SChris Mason u64 end = start + eb->len - 1; 22005f39d397SChris Mason 22015f39d397SChris Mason set = clear_extent_dirty(tree, start, end, GFP_NOFS); 2202db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 22035f39d397SChris Mason 22045f39d397SChris Mason for (i = 0; i < num_pages; i++) { 22056d36dcd4SChris Mason page = extent_buffer_page(eb, i); 22065f39d397SChris Mason lock_page(page); 22075f39d397SChris Mason /* 22085f39d397SChris Mason * if we're on the last page or the first page and the 22095f39d397SChris Mason * block isn't aligned on a page boundary, do extra checks 22105f39d397SChris Mason * to make sure we don't clean page that is partially dirty 22115f39d397SChris Mason */ 22125f39d397SChris Mason if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || 22135f39d397SChris Mason ((i == num_pages - 1) && 221465555a06SYan ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { 221535ebb934SChris Mason start = (u64)page->index << PAGE_CACHE_SHIFT; 22165f39d397SChris Mason end = start + PAGE_CACHE_SIZE - 1; 22175f39d397SChris Mason if (test_range_bit(tree, start, end, 22185f39d397SChris Mason EXTENT_DIRTY, 0)) { 22195f39d397SChris Mason unlock_page(page); 22205f39d397SChris Mason continue; 22215f39d397SChris Mason } 22225f39d397SChris Mason } 22235f39d397SChris Mason clear_page_dirty_for_io(page); 22245f39d397SChris Mason unlock_page(page); 22255f39d397SChris Mason } 22265f39d397SChris Mason return 0; 22275f39d397SChris Mason } 22285f39d397SChris Mason EXPORT_SYMBOL(clear_extent_buffer_dirty); 22295f39d397SChris Mason 22305f39d397SChris Mason int wait_on_extent_buffer_writeback(struct extent_map_tree *tree, 22315f39d397SChris Mason struct extent_buffer *eb) 22325f39d397SChris Mason { 22335f39d397SChris Mason return wait_on_extent_writeback(tree, eb->start, 22345f39d397SChris Mason eb->start + eb->len - 1); 22355f39d397SChris Mason } 22365f39d397SChris Mason EXPORT_SYMBOL(wait_on_extent_buffer_writeback); 22375f39d397SChris Mason 22385f39d397SChris Mason int set_extent_buffer_dirty(struct extent_map_tree *tree, 22395f39d397SChris Mason struct extent_buffer *eb) 22405f39d397SChris Mason { 2241810191ffSChris Mason unsigned long i; 2242810191ffSChris Mason unsigned long num_pages; 2243810191ffSChris Mason 2244810191ffSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 2245810191ffSChris Mason for (i = 0; i < num_pages; i++) { 224619c00ddcSChris Mason struct page *page = extent_buffer_page(eb, i); 224719c00ddcSChris Mason /* writepage may need to do something special for the 224819c00ddcSChris Mason * first page, we have to make sure page->private is 224919c00ddcSChris Mason * properly set. releasepage may drop page->private 225019c00ddcSChris Mason * on us if the page isn't already dirty. 225119c00ddcSChris Mason */ 225219c00ddcSChris Mason if (i == 0) { 225319c00ddcSChris Mason lock_page(page); 225419c00ddcSChris Mason set_page_private(page, 225519c00ddcSChris Mason EXTENT_PAGE_PRIVATE_FIRST_PAGE | 225619c00ddcSChris Mason eb->len << 2); 225719c00ddcSChris Mason } 2258810191ffSChris Mason __set_page_dirty_nobuffers(extent_buffer_page(eb, i)); 225919c00ddcSChris Mason if (i == 0) 226019c00ddcSChris Mason unlock_page(page); 2261810191ffSChris Mason } 2262810191ffSChris Mason return set_extent_dirty(tree, eb->start, 2263810191ffSChris Mason eb->start + eb->len - 1, GFP_NOFS); 22645f39d397SChris Mason } 22655f39d397SChris Mason EXPORT_SYMBOL(set_extent_buffer_dirty); 22665f39d397SChris Mason 22675f39d397SChris Mason int set_extent_buffer_uptodate(struct extent_map_tree *tree, 22685f39d397SChris Mason struct extent_buffer *eb) 22695f39d397SChris Mason { 22705f39d397SChris Mason unsigned long i; 22715f39d397SChris Mason struct page *page; 22725f39d397SChris Mason unsigned long num_pages; 22735f39d397SChris Mason 2274db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 22755f39d397SChris Mason 22765f39d397SChris Mason set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, 22775f39d397SChris Mason GFP_NOFS); 22785f39d397SChris Mason for (i = 0; i < num_pages; i++) { 22796d36dcd4SChris Mason page = extent_buffer_page(eb, i); 22805f39d397SChris Mason if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || 22815f39d397SChris Mason ((i == num_pages - 1) && 228265555a06SYan ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { 22835f39d397SChris Mason check_page_uptodate(tree, page); 22845f39d397SChris Mason continue; 22855f39d397SChris Mason } 22865f39d397SChris Mason SetPageUptodate(page); 22875f39d397SChris Mason } 22885f39d397SChris Mason return 0; 22895f39d397SChris Mason } 22905f39d397SChris Mason EXPORT_SYMBOL(set_extent_buffer_uptodate); 22915f39d397SChris Mason 22925f39d397SChris Mason int extent_buffer_uptodate(struct extent_map_tree *tree, 22935f39d397SChris Mason struct extent_buffer *eb) 22945f39d397SChris Mason { 22955f39d397SChris Mason if (eb->flags & EXTENT_UPTODATE) 22965f39d397SChris Mason return 1; 22975f39d397SChris Mason return test_range_bit(tree, eb->start, eb->start + eb->len - 1, 22985f39d397SChris Mason EXTENT_UPTODATE, 1); 22995f39d397SChris Mason } 23005f39d397SChris Mason EXPORT_SYMBOL(extent_buffer_uptodate); 23015f39d397SChris Mason 23025f39d397SChris Mason int read_extent_buffer_pages(struct extent_map_tree *tree, 230319c00ddcSChris Mason struct extent_buffer *eb, 230419c00ddcSChris Mason u64 start, 230519c00ddcSChris Mason int wait) 23065f39d397SChris Mason { 23075f39d397SChris Mason unsigned long i; 230819c00ddcSChris Mason unsigned long start_i; 23095f39d397SChris Mason struct page *page; 23105f39d397SChris Mason int err; 23115f39d397SChris Mason int ret = 0; 23125f39d397SChris Mason unsigned long num_pages; 23135f39d397SChris Mason 23145f39d397SChris Mason if (eb->flags & EXTENT_UPTODATE) 23155f39d397SChris Mason return 0; 23165f39d397SChris Mason 231714048ed0SChris Mason if (0 && test_range_bit(tree, eb->start, eb->start + eb->len - 1, 23185f39d397SChris Mason EXTENT_UPTODATE, 1)) { 23195f39d397SChris Mason return 0; 23205f39d397SChris Mason } 232119c00ddcSChris Mason if (start) { 232219c00ddcSChris Mason WARN_ON(start < eb->start); 232319c00ddcSChris Mason start_i = (start >> PAGE_CACHE_SHIFT) - 232419c00ddcSChris Mason (eb->start >> PAGE_CACHE_SHIFT); 232519c00ddcSChris Mason } else { 232619c00ddcSChris Mason start_i = 0; 232719c00ddcSChris Mason } 23285f39d397SChris Mason 2329db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 233019c00ddcSChris Mason for (i = start_i; i < num_pages; i++) { 23316d36dcd4SChris Mason page = extent_buffer_page(eb, i); 23325f39d397SChris Mason if (PageUptodate(page)) { 23335f39d397SChris Mason continue; 23345f39d397SChris Mason } 23355f39d397SChris Mason if (!wait) { 23365f39d397SChris Mason if (TestSetPageLocked(page)) { 23375f39d397SChris Mason continue; 23385f39d397SChris Mason } 23395f39d397SChris Mason } else { 23405f39d397SChris Mason lock_page(page); 23415f39d397SChris Mason } 23425f39d397SChris Mason if (!PageUptodate(page)) { 23435f39d397SChris Mason err = page->mapping->a_ops->readpage(NULL, page); 23445f39d397SChris Mason if (err) { 23455f39d397SChris Mason ret = err; 23465f39d397SChris Mason } 23475f39d397SChris Mason } else { 23485f39d397SChris Mason unlock_page(page); 23495f39d397SChris Mason } 23505f39d397SChris Mason } 23515f39d397SChris Mason 23525f39d397SChris Mason if (ret || !wait) { 23535f39d397SChris Mason return ret; 23545f39d397SChris Mason } 23555f39d397SChris Mason 235619c00ddcSChris Mason for (i = start_i; i < num_pages; i++) { 23576d36dcd4SChris Mason page = extent_buffer_page(eb, i); 23585f39d397SChris Mason wait_on_page_locked(page); 23595f39d397SChris Mason if (!PageUptodate(page)) { 23605f39d397SChris Mason ret = -EIO; 23615f39d397SChris Mason } 23625f39d397SChris Mason } 23634dc11904SChris Mason if (!ret) 23645f39d397SChris Mason eb->flags |= EXTENT_UPTODATE; 23655f39d397SChris Mason return ret; 23665f39d397SChris Mason } 23675f39d397SChris Mason EXPORT_SYMBOL(read_extent_buffer_pages); 23685f39d397SChris Mason 23695f39d397SChris Mason void read_extent_buffer(struct extent_buffer *eb, void *dstv, 23705f39d397SChris Mason unsigned long start, 23715f39d397SChris Mason unsigned long len) 23725f39d397SChris Mason { 23735f39d397SChris Mason size_t cur; 23745f39d397SChris Mason size_t offset; 23755f39d397SChris Mason struct page *page; 23765f39d397SChris Mason char *kaddr; 23775f39d397SChris Mason char *dst = (char *)dstv; 23785f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 23795f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 238014048ed0SChris Mason unsigned long num_pages = num_extent_pages(eb->start, eb->len); 23815f39d397SChris Mason 23825f39d397SChris Mason WARN_ON(start > eb->len); 23835f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 23845f39d397SChris Mason 23853685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 23865f39d397SChris Mason 23875f39d397SChris Mason while(len > 0) { 23886d36dcd4SChris Mason page = extent_buffer_page(eb, i); 238914048ed0SChris Mason if (!PageUptodate(page)) { 239014048ed0SChris Mason printk("page %lu not up to date i %lu, total %lu, len %lu\n", page->index, i, num_pages, eb->len); 239114048ed0SChris Mason WARN_ON(1); 239214048ed0SChris Mason } 23935f39d397SChris Mason WARN_ON(!PageUptodate(page)); 23945f39d397SChris Mason 23955f39d397SChris Mason cur = min(len, (PAGE_CACHE_SIZE - offset)); 239659d169e2SChris Mason kaddr = kmap_atomic(page, KM_USER1); 23975f39d397SChris Mason memcpy(dst, kaddr + offset, cur); 239859d169e2SChris Mason kunmap_atomic(kaddr, KM_USER1); 23995f39d397SChris Mason 24005f39d397SChris Mason dst += cur; 24015f39d397SChris Mason len -= cur; 24025f39d397SChris Mason offset = 0; 24035f39d397SChris Mason i++; 24045f39d397SChris Mason } 24055f39d397SChris Mason } 24065f39d397SChris Mason EXPORT_SYMBOL(read_extent_buffer); 24075f39d397SChris Mason 240819c00ddcSChris Mason int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, 2409db94535dSChris Mason unsigned long min_len, char **token, char **map, 24105f39d397SChris Mason unsigned long *map_start, 24115f39d397SChris Mason unsigned long *map_len, int km) 24125f39d397SChris Mason { 2413479965d6SChris Mason size_t offset = start & (PAGE_CACHE_SIZE - 1); 24145f39d397SChris Mason char *kaddr; 2415db94535dSChris Mason struct page *p; 24165f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 24175f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 241865555a06SYan unsigned long end_i = (start_offset + start + min_len - 1) >> 2419479965d6SChris Mason PAGE_CACHE_SHIFT; 2420479965d6SChris Mason 2421479965d6SChris Mason if (i != end_i) 2422479965d6SChris Mason return -EINVAL; 24235f39d397SChris Mason 24245f39d397SChris Mason if (i == 0) { 24255f39d397SChris Mason offset = start_offset; 24265f39d397SChris Mason *map_start = 0; 24275f39d397SChris Mason } else { 2428db94535dSChris Mason offset = 0; 2429479965d6SChris Mason *map_start = (i << PAGE_CACHE_SHIFT) - start_offset; 24305f39d397SChris Mason } 243165555a06SYan if (start + min_len > eb->len) { 243219c00ddcSChris Mason printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len); 243319c00ddcSChris Mason WARN_ON(1); 243419c00ddcSChris Mason } 24355f39d397SChris Mason 2436db94535dSChris Mason p = extent_buffer_page(eb, i); 2437db94535dSChris Mason WARN_ON(!PageUptodate(p)); 2438db94535dSChris Mason kaddr = kmap_atomic(p, km); 24395f39d397SChris Mason *token = kaddr; 24405f39d397SChris Mason *map = kaddr + offset; 24415f39d397SChris Mason *map_len = PAGE_CACHE_SIZE - offset; 24425f39d397SChris Mason return 0; 24435f39d397SChris Mason } 244419c00ddcSChris Mason EXPORT_SYMBOL(map_private_extent_buffer); 2445db94535dSChris Mason 2446db94535dSChris Mason int map_extent_buffer(struct extent_buffer *eb, unsigned long start, 2447db94535dSChris Mason unsigned long min_len, 2448db94535dSChris Mason char **token, char **map, 2449db94535dSChris Mason unsigned long *map_start, 2450db94535dSChris Mason unsigned long *map_len, int km) 2451db94535dSChris Mason { 2452db94535dSChris Mason int err; 2453db94535dSChris Mason int save = 0; 2454db94535dSChris Mason if (eb->map_token) { 2455db94535dSChris Mason unmap_extent_buffer(eb, eb->map_token, km); 2456db94535dSChris Mason eb->map_token = NULL; 2457db94535dSChris Mason save = 1; 2458db94535dSChris Mason } 245919c00ddcSChris Mason err = map_private_extent_buffer(eb, start, min_len, token, map, 2460db94535dSChris Mason map_start, map_len, km); 2461db94535dSChris Mason if (!err && save) { 2462db94535dSChris Mason eb->map_token = *token; 2463db94535dSChris Mason eb->kaddr = *map; 2464db94535dSChris Mason eb->map_start = *map_start; 2465db94535dSChris Mason eb->map_len = *map_len; 2466db94535dSChris Mason } 2467db94535dSChris Mason return err; 2468db94535dSChris Mason } 24695f39d397SChris Mason EXPORT_SYMBOL(map_extent_buffer); 24705f39d397SChris Mason 24715f39d397SChris Mason void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km) 24725f39d397SChris Mason { 2473ae5252bdSChris Mason kunmap_atomic(token, km); 24745f39d397SChris Mason } 24755f39d397SChris Mason EXPORT_SYMBOL(unmap_extent_buffer); 24765f39d397SChris Mason 24775f39d397SChris Mason int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, 24785f39d397SChris Mason unsigned long start, 24795f39d397SChris Mason unsigned long len) 24805f39d397SChris Mason { 24815f39d397SChris Mason size_t cur; 24825f39d397SChris Mason size_t offset; 24835f39d397SChris Mason struct page *page; 24845f39d397SChris Mason char *kaddr; 24855f39d397SChris Mason char *ptr = (char *)ptrv; 24865f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 24875f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 24885f39d397SChris Mason int ret = 0; 24895f39d397SChris Mason 24905f39d397SChris Mason WARN_ON(start > eb->len); 24915f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 24925f39d397SChris Mason 24933685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 24945f39d397SChris Mason 24955f39d397SChris Mason while(len > 0) { 24966d36dcd4SChris Mason page = extent_buffer_page(eb, i); 24975f39d397SChris Mason WARN_ON(!PageUptodate(page)); 24985f39d397SChris Mason 24995f39d397SChris Mason cur = min(len, (PAGE_CACHE_SIZE - offset)); 25005f39d397SChris Mason 2501ae5252bdSChris Mason kaddr = kmap_atomic(page, KM_USER0); 25025f39d397SChris Mason ret = memcmp(ptr, kaddr + offset, cur); 2503ae5252bdSChris Mason kunmap_atomic(kaddr, KM_USER0); 25045f39d397SChris Mason if (ret) 25055f39d397SChris Mason break; 25065f39d397SChris Mason 25075f39d397SChris Mason ptr += cur; 25085f39d397SChris Mason len -= cur; 25095f39d397SChris Mason offset = 0; 25105f39d397SChris Mason i++; 25115f39d397SChris Mason } 25125f39d397SChris Mason return ret; 25135f39d397SChris Mason } 25145f39d397SChris Mason EXPORT_SYMBOL(memcmp_extent_buffer); 25155f39d397SChris Mason 25165f39d397SChris Mason void write_extent_buffer(struct extent_buffer *eb, const void *srcv, 25175f39d397SChris Mason unsigned long start, unsigned long len) 25185f39d397SChris Mason { 25195f39d397SChris Mason size_t cur; 25205f39d397SChris Mason size_t offset; 25215f39d397SChris Mason struct page *page; 25225f39d397SChris Mason char *kaddr; 25235f39d397SChris Mason char *src = (char *)srcv; 25245f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 25255f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 25265f39d397SChris Mason 25275f39d397SChris Mason WARN_ON(start > eb->len); 25285f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 25295f39d397SChris Mason 25303685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 25315f39d397SChris Mason 25325f39d397SChris Mason while(len > 0) { 25336d36dcd4SChris Mason page = extent_buffer_page(eb, i); 25345f39d397SChris Mason WARN_ON(!PageUptodate(page)); 25355f39d397SChris Mason 25365f39d397SChris Mason cur = min(len, PAGE_CACHE_SIZE - offset); 253759d169e2SChris Mason kaddr = kmap_atomic(page, KM_USER1); 25385f39d397SChris Mason memcpy(kaddr + offset, src, cur); 253959d169e2SChris Mason kunmap_atomic(kaddr, KM_USER1); 25405f39d397SChris Mason 25415f39d397SChris Mason src += cur; 25425f39d397SChris Mason len -= cur; 25435f39d397SChris Mason offset = 0; 25445f39d397SChris Mason i++; 25455f39d397SChris Mason } 25465f39d397SChris Mason } 25475f39d397SChris Mason EXPORT_SYMBOL(write_extent_buffer); 25485f39d397SChris Mason 25495f39d397SChris Mason void memset_extent_buffer(struct extent_buffer *eb, char c, 25505f39d397SChris Mason unsigned long start, unsigned long len) 25515f39d397SChris Mason { 25525f39d397SChris Mason size_t cur; 25535f39d397SChris Mason size_t offset; 25545f39d397SChris Mason struct page *page; 25555f39d397SChris Mason char *kaddr; 25565f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 25575f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 25585f39d397SChris Mason 25595f39d397SChris Mason WARN_ON(start > eb->len); 25605f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 25615f39d397SChris Mason 25623685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 25635f39d397SChris Mason 25645f39d397SChris Mason while(len > 0) { 25656d36dcd4SChris Mason page = extent_buffer_page(eb, i); 25665f39d397SChris Mason WARN_ON(!PageUptodate(page)); 25675f39d397SChris Mason 25685f39d397SChris Mason cur = min(len, PAGE_CACHE_SIZE - offset); 2569ae5252bdSChris Mason kaddr = kmap_atomic(page, KM_USER0); 25705f39d397SChris Mason memset(kaddr + offset, c, cur); 2571ae5252bdSChris Mason kunmap_atomic(kaddr, KM_USER0); 25725f39d397SChris Mason 25735f39d397SChris Mason len -= cur; 25745f39d397SChris Mason offset = 0; 25755f39d397SChris Mason i++; 25765f39d397SChris Mason } 25775f39d397SChris Mason } 25785f39d397SChris Mason EXPORT_SYMBOL(memset_extent_buffer); 25795f39d397SChris Mason 25805f39d397SChris Mason void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, 25815f39d397SChris Mason unsigned long dst_offset, unsigned long src_offset, 25825f39d397SChris Mason unsigned long len) 25835f39d397SChris Mason { 25845f39d397SChris Mason u64 dst_len = dst->len; 25855f39d397SChris Mason size_t cur; 25865f39d397SChris Mason size_t offset; 25875f39d397SChris Mason struct page *page; 25885f39d397SChris Mason char *kaddr; 25895f39d397SChris Mason size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); 25905f39d397SChris Mason unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; 25915f39d397SChris Mason 25925f39d397SChris Mason WARN_ON(src->len != dst_len); 25935f39d397SChris Mason 25943685f791SChris Mason offset = (start_offset + dst_offset) & 25953685f791SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 25965f39d397SChris Mason 25975f39d397SChris Mason while(len > 0) { 25986d36dcd4SChris Mason page = extent_buffer_page(dst, i); 25995f39d397SChris Mason WARN_ON(!PageUptodate(page)); 26005f39d397SChris Mason 26015f39d397SChris Mason cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); 26025f39d397SChris Mason 2603ff190c0cSChris Mason kaddr = kmap_atomic(page, KM_USER0); 26045f39d397SChris Mason read_extent_buffer(src, kaddr + offset, src_offset, cur); 2605ff190c0cSChris Mason kunmap_atomic(kaddr, KM_USER0); 26065f39d397SChris Mason 26075f39d397SChris Mason src_offset += cur; 26085f39d397SChris Mason len -= cur; 26095f39d397SChris Mason offset = 0; 26105f39d397SChris Mason i++; 26115f39d397SChris Mason } 26125f39d397SChris Mason } 26135f39d397SChris Mason EXPORT_SYMBOL(copy_extent_buffer); 26145f39d397SChris Mason 26155f39d397SChris Mason static void move_pages(struct page *dst_page, struct page *src_page, 26165f39d397SChris Mason unsigned long dst_off, unsigned long src_off, 26175f39d397SChris Mason unsigned long len) 26185f39d397SChris Mason { 2619ae5252bdSChris Mason char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); 26205f39d397SChris Mason if (dst_page == src_page) { 26215f39d397SChris Mason memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len); 26225f39d397SChris Mason } else { 2623ae5252bdSChris Mason char *src_kaddr = kmap_atomic(src_page, KM_USER1); 26245f39d397SChris Mason char *p = dst_kaddr + dst_off + len; 26255f39d397SChris Mason char *s = src_kaddr + src_off + len; 26265f39d397SChris Mason 26275f39d397SChris Mason while (len--) 26285f39d397SChris Mason *--p = *--s; 26295f39d397SChris Mason 2630ae5252bdSChris Mason kunmap_atomic(src_kaddr, KM_USER1); 26315f39d397SChris Mason } 2632ae5252bdSChris Mason kunmap_atomic(dst_kaddr, KM_USER0); 26335f39d397SChris Mason } 26345f39d397SChris Mason 26355f39d397SChris Mason static void copy_pages(struct page *dst_page, struct page *src_page, 26365f39d397SChris Mason unsigned long dst_off, unsigned long src_off, 26375f39d397SChris Mason unsigned long len) 26385f39d397SChris Mason { 2639ae5252bdSChris Mason char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); 26405f39d397SChris Mason char *src_kaddr; 26415f39d397SChris Mason 26425f39d397SChris Mason if (dst_page != src_page) 2643ae5252bdSChris Mason src_kaddr = kmap_atomic(src_page, KM_USER1); 26445f39d397SChris Mason else 26455f39d397SChris Mason src_kaddr = dst_kaddr; 26465f39d397SChris Mason 26475f39d397SChris Mason memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); 26485f39d397SChris Mason kunmap_atomic(dst_kaddr, KM_USER0); 26495f39d397SChris Mason if (dst_page != src_page) 26505f39d397SChris Mason kunmap_atomic(src_kaddr, KM_USER1); 26515f39d397SChris Mason } 26525f39d397SChris Mason 26535f39d397SChris Mason void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, 26545f39d397SChris Mason unsigned long src_offset, unsigned long len) 26555f39d397SChris Mason { 26565f39d397SChris Mason size_t cur; 26575f39d397SChris Mason size_t dst_off_in_page; 26585f39d397SChris Mason size_t src_off_in_page; 26595f39d397SChris Mason size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); 26605f39d397SChris Mason unsigned long dst_i; 26615f39d397SChris Mason unsigned long src_i; 26625f39d397SChris Mason 26635f39d397SChris Mason if (src_offset + len > dst->len) { 26645f39d397SChris Mason printk("memmove bogus src_offset %lu move len %lu len %lu\n", 26655f39d397SChris Mason src_offset, len, dst->len); 26665f39d397SChris Mason BUG_ON(1); 26675f39d397SChris Mason } 26685f39d397SChris Mason if (dst_offset + len > dst->len) { 26695f39d397SChris Mason printk("memmove bogus dst_offset %lu move len %lu len %lu\n", 26705f39d397SChris Mason dst_offset, len, dst->len); 26715f39d397SChris Mason BUG_ON(1); 26725f39d397SChris Mason } 26735f39d397SChris Mason 26745f39d397SChris Mason while(len > 0) { 26753685f791SChris Mason dst_off_in_page = (start_offset + dst_offset) & 26765f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 26773685f791SChris Mason src_off_in_page = (start_offset + src_offset) & 26785f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 26795f39d397SChris Mason 26805f39d397SChris Mason dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; 26815f39d397SChris Mason src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; 26825f39d397SChris Mason 26835f39d397SChris Mason cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - 26845f39d397SChris Mason src_off_in_page)); 2685ae2f5411SJens Axboe cur = min_t(unsigned long, cur, 2686ae2f5411SJens Axboe (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); 26875f39d397SChris Mason 26886d36dcd4SChris Mason copy_pages(extent_buffer_page(dst, dst_i), 26896d36dcd4SChris Mason extent_buffer_page(dst, src_i), 26905f39d397SChris Mason dst_off_in_page, src_off_in_page, cur); 26915f39d397SChris Mason 26925f39d397SChris Mason src_offset += cur; 26935f39d397SChris Mason dst_offset += cur; 26945f39d397SChris Mason len -= cur; 26955f39d397SChris Mason } 26965f39d397SChris Mason } 26975f39d397SChris Mason EXPORT_SYMBOL(memcpy_extent_buffer); 26985f39d397SChris Mason 26995f39d397SChris Mason void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, 27005f39d397SChris Mason unsigned long src_offset, unsigned long len) 27015f39d397SChris Mason { 27025f39d397SChris Mason size_t cur; 27035f39d397SChris Mason size_t dst_off_in_page; 27045f39d397SChris Mason size_t src_off_in_page; 27055f39d397SChris Mason unsigned long dst_end = dst_offset + len - 1; 27065f39d397SChris Mason unsigned long src_end = src_offset + len - 1; 27075f39d397SChris Mason size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); 27085f39d397SChris Mason unsigned long dst_i; 27095f39d397SChris Mason unsigned long src_i; 27105f39d397SChris Mason 27115f39d397SChris Mason if (src_offset + len > dst->len) { 27125f39d397SChris Mason printk("memmove bogus src_offset %lu move len %lu len %lu\n", 27135f39d397SChris Mason src_offset, len, dst->len); 27145f39d397SChris Mason BUG_ON(1); 27155f39d397SChris Mason } 27165f39d397SChris Mason if (dst_offset + len > dst->len) { 27175f39d397SChris Mason printk("memmove bogus dst_offset %lu move len %lu len %lu\n", 27185f39d397SChris Mason dst_offset, len, dst->len); 27195f39d397SChris Mason BUG_ON(1); 27205f39d397SChris Mason } 27215f39d397SChris Mason if (dst_offset < src_offset) { 27225f39d397SChris Mason memcpy_extent_buffer(dst, dst_offset, src_offset, len); 27235f39d397SChris Mason return; 27245f39d397SChris Mason } 27255f39d397SChris Mason while(len > 0) { 27265f39d397SChris Mason dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; 27275f39d397SChris Mason src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; 27285f39d397SChris Mason 27293685f791SChris Mason dst_off_in_page = (start_offset + dst_end) & 27305f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 27313685f791SChris Mason src_off_in_page = (start_offset + src_end) & 27325f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 27335f39d397SChris Mason 2734ae2f5411SJens Axboe cur = min_t(unsigned long, len, src_off_in_page + 1); 27355f39d397SChris Mason cur = min(cur, dst_off_in_page + 1); 27366d36dcd4SChris Mason move_pages(extent_buffer_page(dst, dst_i), 27376d36dcd4SChris Mason extent_buffer_page(dst, src_i), 27385f39d397SChris Mason dst_off_in_page - cur + 1, 27395f39d397SChris Mason src_off_in_page - cur + 1, cur); 27405f39d397SChris Mason 2741db94535dSChris Mason dst_end -= cur; 2742db94535dSChris Mason src_end -= cur; 27435f39d397SChris Mason len -= cur; 27445f39d397SChris Mason } 27455f39d397SChris Mason } 27465f39d397SChris Mason EXPORT_SYMBOL(memmove_extent_buffer); 2747