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> 13b293f02eSChris Mason #include <linux/writeback.h> 143ab2fb5aSChris Mason #include <linux/pagevec.h> 15a52d9a80SChris Mason #include "extent_map.h" 16a52d9a80SChris Mason 1786479a04SChris Mason /* temporary define until extent_map moves out of btrfs */ 1886479a04SChris Mason struct kmem_cache *btrfs_cache_create(const char *name, size_t size, 1986479a04SChris Mason unsigned long extra_flags, 2086479a04SChris Mason void (*ctor)(void *, struct kmem_cache *, 2186479a04SChris Mason unsigned long)); 2286479a04SChris Mason 23a52d9a80SChris Mason static struct kmem_cache *extent_map_cache; 24a52d9a80SChris Mason static struct kmem_cache *extent_state_cache; 256d36dcd4SChris Mason static struct kmem_cache *extent_buffer_cache; 26f510cfecSChris Mason 27f510cfecSChris Mason static LIST_HEAD(buffers); 28f510cfecSChris Mason static LIST_HEAD(states); 29f510cfecSChris Mason 30f510cfecSChris Mason static spinlock_t state_lock = SPIN_LOCK_UNLOCKED; 314dc11904SChris Mason #define BUFFER_LRU_MAX 64 32a52d9a80SChris Mason 33a52d9a80SChris Mason struct tree_entry { 34a52d9a80SChris Mason u64 start; 35a52d9a80SChris Mason u64 end; 36a52d9a80SChris Mason int in_tree; 37a52d9a80SChris Mason struct rb_node rb_node; 38a52d9a80SChris Mason }; 39a52d9a80SChris Mason 40b293f02eSChris Mason struct extent_page_data { 41b293f02eSChris Mason struct bio *bio; 42b293f02eSChris Mason struct extent_map_tree *tree; 43b293f02eSChris Mason get_extent_t *get_extent; 44b293f02eSChris Mason }; 45ca664626SChris Mason 462f4cbe64SWyatt Banks int __init extent_map_init(void) 47a52d9a80SChris Mason { 4886479a04SChris Mason extent_map_cache = btrfs_cache_create("extent_map", 496d36dcd4SChris Mason sizeof(struct extent_map), 0, 50a52d9a80SChris Mason NULL); 512f4cbe64SWyatt Banks if (!extent_map_cache) 522f4cbe64SWyatt Banks return -ENOMEM; 5386479a04SChris Mason extent_state_cache = btrfs_cache_create("extent_state", 546d36dcd4SChris Mason sizeof(struct extent_state), 0, 55a52d9a80SChris Mason NULL); 562f4cbe64SWyatt Banks if (!extent_state_cache) 572f4cbe64SWyatt Banks goto free_map_cache; 586d36dcd4SChris Mason extent_buffer_cache = btrfs_cache_create("extent_buffers", 596d36dcd4SChris Mason sizeof(struct extent_buffer), 0, 606d36dcd4SChris Mason NULL); 612f4cbe64SWyatt Banks if (!extent_buffer_cache) 622f4cbe64SWyatt Banks goto free_state_cache; 632f4cbe64SWyatt Banks return 0; 642f4cbe64SWyatt Banks 652f4cbe64SWyatt Banks free_state_cache: 662f4cbe64SWyatt Banks kmem_cache_destroy(extent_state_cache); 672f4cbe64SWyatt Banks free_map_cache: 682f4cbe64SWyatt Banks kmem_cache_destroy(extent_map_cache); 692f4cbe64SWyatt Banks return -ENOMEM; 70a52d9a80SChris Mason } 71a52d9a80SChris Mason 7217636e03SChristian Hesse void extent_map_exit(void) 73a52d9a80SChris Mason { 74f510cfecSChris Mason struct extent_state *state; 756d36dcd4SChris Mason 76f510cfecSChris Mason while (!list_empty(&states)) { 77f510cfecSChris Mason state = list_entry(states.next, struct extent_state, list); 78f510cfecSChris 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)); 79f510cfecSChris Mason list_del(&state->list); 80f510cfecSChris Mason kmem_cache_free(extent_state_cache, state); 81f510cfecSChris Mason 82f510cfecSChris Mason } 83f510cfecSChris Mason 84a52d9a80SChris Mason if (extent_map_cache) 85a52d9a80SChris Mason kmem_cache_destroy(extent_map_cache); 86a52d9a80SChris Mason if (extent_state_cache) 87a52d9a80SChris Mason kmem_cache_destroy(extent_state_cache); 886d36dcd4SChris Mason if (extent_buffer_cache) 896d36dcd4SChris Mason kmem_cache_destroy(extent_buffer_cache); 90a52d9a80SChris Mason } 91a52d9a80SChris Mason 92a52d9a80SChris Mason void extent_map_tree_init(struct extent_map_tree *tree, 93a52d9a80SChris Mason struct address_space *mapping, gfp_t mask) 94a52d9a80SChris Mason { 95a52d9a80SChris Mason tree->map.rb_node = NULL; 96a52d9a80SChris Mason tree->state.rb_node = NULL; 9707157aacSChris Mason tree->ops = NULL; 98ca664626SChris Mason tree->dirty_bytes = 0; 99a52d9a80SChris Mason rwlock_init(&tree->lock); 1004dc11904SChris Mason spin_lock_init(&tree->lru_lock); 101a52d9a80SChris Mason tree->mapping = mapping; 1024dc11904SChris Mason INIT_LIST_HEAD(&tree->buffer_lru); 1034dc11904SChris Mason tree->lru_size = 0; 104a52d9a80SChris Mason } 105a52d9a80SChris Mason EXPORT_SYMBOL(extent_map_tree_init); 106a52d9a80SChris Mason 10719c00ddcSChris Mason void extent_map_tree_empty_lru(struct extent_map_tree *tree) 1084dc11904SChris Mason { 1094dc11904SChris Mason struct extent_buffer *eb; 1104dc11904SChris Mason while(!list_empty(&tree->buffer_lru)) { 1114dc11904SChris Mason eb = list_entry(tree->buffer_lru.next, struct extent_buffer, 1124dc11904SChris Mason lru); 1130591fb56SChris Mason list_del_init(&eb->lru); 1144dc11904SChris Mason free_extent_buffer(eb); 1154dc11904SChris Mason } 1164dc11904SChris Mason } 11719c00ddcSChris Mason EXPORT_SYMBOL(extent_map_tree_empty_lru); 1184dc11904SChris Mason 119a52d9a80SChris Mason struct extent_map *alloc_extent_map(gfp_t mask) 120a52d9a80SChris Mason { 121a52d9a80SChris Mason struct extent_map *em; 122a52d9a80SChris Mason em = kmem_cache_alloc(extent_map_cache, mask); 123a52d9a80SChris Mason if (!em || IS_ERR(em)) 124a52d9a80SChris Mason return em; 125a52d9a80SChris Mason em->in_tree = 0; 126a52d9a80SChris Mason atomic_set(&em->refs, 1); 127a52d9a80SChris Mason return em; 128a52d9a80SChris Mason } 129a52d9a80SChris Mason EXPORT_SYMBOL(alloc_extent_map); 130a52d9a80SChris Mason 131a52d9a80SChris Mason void free_extent_map(struct extent_map *em) 132a52d9a80SChris Mason { 1332bf5a725SChris Mason if (!em) 1342bf5a725SChris Mason return; 135a52d9a80SChris Mason if (atomic_dec_and_test(&em->refs)) { 136a52d9a80SChris Mason WARN_ON(em->in_tree); 137a52d9a80SChris Mason kmem_cache_free(extent_map_cache, em); 138a52d9a80SChris Mason } 139a52d9a80SChris Mason } 140a52d9a80SChris Mason EXPORT_SYMBOL(free_extent_map); 141a52d9a80SChris Mason 142a52d9a80SChris Mason 143a52d9a80SChris Mason struct extent_state *alloc_extent_state(gfp_t mask) 144a52d9a80SChris Mason { 145a52d9a80SChris Mason struct extent_state *state; 146f510cfecSChris Mason unsigned long flags; 147f510cfecSChris Mason 148a52d9a80SChris Mason state = kmem_cache_alloc(extent_state_cache, mask); 149a52d9a80SChris Mason if (!state || IS_ERR(state)) 150a52d9a80SChris Mason return state; 151a52d9a80SChris Mason state->state = 0; 152a52d9a80SChris Mason state->in_tree = 0; 15307157aacSChris Mason state->private = 0; 154f510cfecSChris Mason 155f510cfecSChris Mason spin_lock_irqsave(&state_lock, flags); 156f510cfecSChris Mason list_add(&state->list, &states); 157f510cfecSChris Mason spin_unlock_irqrestore(&state_lock, flags); 158f510cfecSChris Mason 159a52d9a80SChris Mason atomic_set(&state->refs, 1); 160a52d9a80SChris Mason init_waitqueue_head(&state->wq); 161a52d9a80SChris Mason return state; 162a52d9a80SChris Mason } 163a52d9a80SChris Mason EXPORT_SYMBOL(alloc_extent_state); 164a52d9a80SChris Mason 165a52d9a80SChris Mason void free_extent_state(struct extent_state *state) 166a52d9a80SChris Mason { 167f510cfecSChris Mason unsigned long flags; 1682bf5a725SChris Mason if (!state) 1692bf5a725SChris Mason return; 170a52d9a80SChris Mason if (atomic_dec_and_test(&state->refs)) { 171a52d9a80SChris Mason WARN_ON(state->in_tree); 172f510cfecSChris Mason spin_lock_irqsave(&state_lock, flags); 173f510cfecSChris Mason list_del(&state->list); 174f510cfecSChris Mason spin_unlock_irqrestore(&state_lock, flags); 175a52d9a80SChris Mason kmem_cache_free(extent_state_cache, state); 176a52d9a80SChris Mason } 177a52d9a80SChris Mason } 178a52d9a80SChris Mason EXPORT_SYMBOL(free_extent_state); 179a52d9a80SChris Mason 180a52d9a80SChris Mason static struct rb_node *tree_insert(struct rb_root *root, u64 offset, 181a52d9a80SChris Mason struct rb_node *node) 182a52d9a80SChris Mason { 183a52d9a80SChris Mason struct rb_node ** p = &root->rb_node; 184a52d9a80SChris Mason struct rb_node * parent = NULL; 185a52d9a80SChris Mason struct tree_entry *entry; 186a52d9a80SChris Mason 187a52d9a80SChris Mason while(*p) { 188a52d9a80SChris Mason parent = *p; 189a52d9a80SChris Mason entry = rb_entry(parent, struct tree_entry, rb_node); 190a52d9a80SChris Mason 191a52d9a80SChris Mason if (offset < entry->start) 192a52d9a80SChris Mason p = &(*p)->rb_left; 193a52d9a80SChris Mason else if (offset > entry->end) 194a52d9a80SChris Mason p = &(*p)->rb_right; 195a52d9a80SChris Mason else 196a52d9a80SChris Mason return parent; 197a52d9a80SChris Mason } 198a52d9a80SChris Mason 199a52d9a80SChris Mason entry = rb_entry(node, struct tree_entry, rb_node); 200a52d9a80SChris Mason entry->in_tree = 1; 201a52d9a80SChris Mason rb_link_node(node, parent, p); 202a52d9a80SChris Mason rb_insert_color(node, root); 203a52d9a80SChris Mason return NULL; 204a52d9a80SChris Mason } 205a52d9a80SChris Mason 206a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset, 207*5f56406aSChris Mason struct rb_node **prev_ret, 208*5f56406aSChris Mason struct rb_node **next_ret) 209a52d9a80SChris Mason { 210a52d9a80SChris Mason struct rb_node * n = root->rb_node; 211a52d9a80SChris Mason struct rb_node *prev = NULL; 212*5f56406aSChris Mason struct rb_node *orig_prev = NULL; 213a52d9a80SChris Mason struct tree_entry *entry; 214a52d9a80SChris Mason struct tree_entry *prev_entry = NULL; 215a52d9a80SChris Mason 216a52d9a80SChris Mason while(n) { 217a52d9a80SChris Mason entry = rb_entry(n, struct tree_entry, rb_node); 218a52d9a80SChris Mason prev = n; 219a52d9a80SChris Mason prev_entry = entry; 220a52d9a80SChris Mason 221a52d9a80SChris Mason if (offset < entry->start) 222a52d9a80SChris Mason n = n->rb_left; 223a52d9a80SChris Mason else if (offset > entry->end) 224a52d9a80SChris Mason n = n->rb_right; 225a52d9a80SChris Mason else 226a52d9a80SChris Mason return n; 227a52d9a80SChris Mason } 228*5f56406aSChris Mason 229*5f56406aSChris Mason if (prev_ret) { 230*5f56406aSChris Mason orig_prev = prev; 231a52d9a80SChris Mason while(prev && offset > prev_entry->end) { 232a52d9a80SChris Mason prev = rb_next(prev); 233a52d9a80SChris Mason prev_entry = rb_entry(prev, struct tree_entry, rb_node); 234a52d9a80SChris Mason } 235a52d9a80SChris Mason *prev_ret = prev; 236*5f56406aSChris Mason prev = orig_prev; 237*5f56406aSChris Mason } 238*5f56406aSChris Mason 239*5f56406aSChris Mason if (next_ret) { 240*5f56406aSChris Mason prev_entry = rb_entry(prev, struct tree_entry, rb_node); 241*5f56406aSChris Mason while(prev && offset < prev_entry->start) { 242*5f56406aSChris Mason prev = rb_prev(prev); 243*5f56406aSChris Mason prev_entry = rb_entry(prev, struct tree_entry, rb_node); 244*5f56406aSChris Mason } 245*5f56406aSChris Mason *next_ret = prev; 246*5f56406aSChris Mason } 247a52d9a80SChris Mason return NULL; 248a52d9a80SChris Mason } 249a52d9a80SChris Mason 250a52d9a80SChris Mason static inline struct rb_node *tree_search(struct rb_root *root, u64 offset) 251a52d9a80SChris Mason { 252a52d9a80SChris Mason struct rb_node *prev; 253a52d9a80SChris Mason struct rb_node *ret; 254*5f56406aSChris Mason ret = __tree_search(root, offset, &prev, NULL); 255a52d9a80SChris Mason if (!ret) 256a52d9a80SChris Mason return prev; 257a52d9a80SChris Mason return ret; 258a52d9a80SChris Mason } 259a52d9a80SChris Mason 260a52d9a80SChris Mason static int tree_delete(struct rb_root *root, u64 offset) 261a52d9a80SChris Mason { 262a52d9a80SChris Mason struct rb_node *node; 263a52d9a80SChris Mason struct tree_entry *entry; 264a52d9a80SChris Mason 265*5f56406aSChris Mason node = __tree_search(root, offset, NULL, NULL); 266a52d9a80SChris Mason if (!node) 267a52d9a80SChris Mason return -ENOENT; 268a52d9a80SChris Mason entry = rb_entry(node, struct tree_entry, rb_node); 269a52d9a80SChris Mason entry->in_tree = 0; 270a52d9a80SChris Mason rb_erase(node, root); 271a52d9a80SChris Mason return 0; 272a52d9a80SChris Mason } 273a52d9a80SChris Mason 274a52d9a80SChris Mason /* 275a52d9a80SChris Mason * add_extent_mapping tries a simple backward merge with existing 276a52d9a80SChris Mason * mappings. The extent_map struct passed in will be inserted into 277a52d9a80SChris Mason * the tree directly (no copies made, just a reference taken). 278a52d9a80SChris Mason */ 279a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree, 280a52d9a80SChris Mason struct extent_map *em) 281a52d9a80SChris Mason { 282a52d9a80SChris Mason int ret = 0; 283a52d9a80SChris Mason struct extent_map *prev = NULL; 284a52d9a80SChris Mason struct rb_node *rb; 285a52d9a80SChris Mason 286a52d9a80SChris Mason write_lock_irq(&tree->lock); 287a52d9a80SChris Mason rb = tree_insert(&tree->map, em->end, &em->rb_node); 288a52d9a80SChris Mason if (rb) { 289a52d9a80SChris Mason prev = rb_entry(rb, struct extent_map, rb_node); 290a52d9a80SChris Mason ret = -EEXIST; 291a52d9a80SChris Mason goto out; 292a52d9a80SChris Mason } 293a52d9a80SChris Mason atomic_inc(&em->refs); 294a52d9a80SChris Mason if (em->start != 0) { 295a52d9a80SChris Mason rb = rb_prev(&em->rb_node); 296a52d9a80SChris Mason if (rb) 297a52d9a80SChris Mason prev = rb_entry(rb, struct extent_map, rb_node); 298a52d9a80SChris Mason if (prev && prev->end + 1 == em->start && 2995f39d397SChris Mason ((em->block_start == EXTENT_MAP_HOLE && 3005f39d397SChris Mason prev->block_start == EXTENT_MAP_HOLE) || 301179e29e4SChris Mason (em->block_start == EXTENT_MAP_INLINE && 302179e29e4SChris Mason prev->block_start == EXTENT_MAP_INLINE) || 303179e29e4SChris Mason (em->block_start == EXTENT_MAP_DELALLOC && 304179e29e4SChris Mason prev->block_start == EXTENT_MAP_DELALLOC) || 305179e29e4SChris Mason (em->block_start < EXTENT_MAP_DELALLOC - 1 && 306179e29e4SChris Mason em->block_start == prev->block_end + 1))) { 307a52d9a80SChris Mason em->start = prev->start; 308a52d9a80SChris Mason em->block_start = prev->block_start; 309a52d9a80SChris Mason rb_erase(&prev->rb_node, &tree->map); 310a52d9a80SChris Mason prev->in_tree = 0; 311a52d9a80SChris Mason free_extent_map(prev); 312a52d9a80SChris Mason } 313a52d9a80SChris Mason } 314a52d9a80SChris Mason out: 315a52d9a80SChris Mason write_unlock_irq(&tree->lock); 316a52d9a80SChris Mason return ret; 317a52d9a80SChris Mason } 318a52d9a80SChris Mason EXPORT_SYMBOL(add_extent_mapping); 319a52d9a80SChris Mason 320a52d9a80SChris Mason /* 321a52d9a80SChris Mason * lookup_extent_mapping returns the first extent_map struct in the 322a52d9a80SChris Mason * tree that intersects the [start, end] (inclusive) range. There may 323a52d9a80SChris Mason * be additional objects in the tree that intersect, so check the object 324a52d9a80SChris Mason * returned carefully to make sure you don't need additional lookups. 325a52d9a80SChris Mason */ 326a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, 327a52d9a80SChris Mason u64 start, u64 end) 328a52d9a80SChris Mason { 329a52d9a80SChris Mason struct extent_map *em; 330a52d9a80SChris Mason struct rb_node *rb_node; 331*5f56406aSChris Mason struct rb_node *prev = NULL; 332*5f56406aSChris Mason struct rb_node *next = NULL; 333a52d9a80SChris Mason 334a52d9a80SChris Mason read_lock_irq(&tree->lock); 335*5f56406aSChris Mason rb_node = __tree_search(&tree->map, start, &prev, &next); 336*5f56406aSChris Mason if (!rb_node && prev) { 337*5f56406aSChris Mason em = rb_entry(prev, struct extent_map, rb_node); 338*5f56406aSChris Mason if (em->start <= end && em->end >= start) 339*5f56406aSChris Mason goto found; 340*5f56406aSChris Mason } 341*5f56406aSChris Mason if (!rb_node && next) { 342*5f56406aSChris Mason em = rb_entry(next, struct extent_map, rb_node); 343*5f56406aSChris Mason if (em->start <= end && em->end >= start) 344*5f56406aSChris Mason goto found; 345*5f56406aSChris Mason } 346a52d9a80SChris Mason if (!rb_node) { 347a52d9a80SChris Mason em = NULL; 348a52d9a80SChris Mason goto out; 349a52d9a80SChris Mason } 350a52d9a80SChris Mason if (IS_ERR(rb_node)) { 351a52d9a80SChris Mason em = ERR_PTR(PTR_ERR(rb_node)); 352a52d9a80SChris Mason goto out; 353a52d9a80SChris Mason } 354a52d9a80SChris Mason em = rb_entry(rb_node, struct extent_map, rb_node); 355a52d9a80SChris Mason if (em->end < start || em->start > end) { 356a52d9a80SChris Mason em = NULL; 357a52d9a80SChris Mason goto out; 358a52d9a80SChris Mason } 359*5f56406aSChris Mason found: 360a52d9a80SChris Mason atomic_inc(&em->refs); 361a52d9a80SChris Mason out: 362a52d9a80SChris Mason read_unlock_irq(&tree->lock); 363a52d9a80SChris Mason return em; 364a52d9a80SChris Mason } 365a52d9a80SChris Mason EXPORT_SYMBOL(lookup_extent_mapping); 366a52d9a80SChris Mason 367a52d9a80SChris Mason /* 368a52d9a80SChris Mason * removes an extent_map struct from the tree. No reference counts are 369a52d9a80SChris Mason * dropped, and no checks are done to see if the range is in use 370a52d9a80SChris Mason */ 371a52d9a80SChris Mason int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) 372a52d9a80SChris Mason { 373a52d9a80SChris Mason int ret; 374a52d9a80SChris Mason 375a52d9a80SChris Mason write_lock_irq(&tree->lock); 376a52d9a80SChris Mason ret = tree_delete(&tree->map, em->end); 377a52d9a80SChris Mason write_unlock_irq(&tree->lock); 378a52d9a80SChris Mason return ret; 379a52d9a80SChris Mason } 380a52d9a80SChris Mason EXPORT_SYMBOL(remove_extent_mapping); 381a52d9a80SChris Mason 382a52d9a80SChris Mason /* 383a52d9a80SChris Mason * utility function to look for merge candidates inside a given range. 384a52d9a80SChris Mason * Any extents with matching state are merged together into a single 385a52d9a80SChris Mason * extent in the tree. Extents with EXTENT_IO in their state field 386a52d9a80SChris Mason * are not merged because the end_io handlers need to be able to do 387a52d9a80SChris Mason * operations on them without sleeping (or doing allocations/splits). 388a52d9a80SChris Mason * 389a52d9a80SChris Mason * This should be called with the tree lock held. 390a52d9a80SChris Mason */ 391a52d9a80SChris Mason static int merge_state(struct extent_map_tree *tree, 392a52d9a80SChris Mason struct extent_state *state) 393a52d9a80SChris Mason { 394a52d9a80SChris Mason struct extent_state *other; 395a52d9a80SChris Mason struct rb_node *other_node; 396a52d9a80SChris Mason 397a52d9a80SChris Mason if (state->state & EXTENT_IOBITS) 398a52d9a80SChris Mason return 0; 399a52d9a80SChris Mason 400a52d9a80SChris Mason other_node = rb_prev(&state->rb_node); 401a52d9a80SChris Mason if (other_node) { 402a52d9a80SChris Mason other = rb_entry(other_node, struct extent_state, rb_node); 403a52d9a80SChris Mason if (other->end == state->start - 1 && 404a52d9a80SChris Mason other->state == state->state) { 405a52d9a80SChris Mason state->start = other->start; 406a52d9a80SChris Mason other->in_tree = 0; 407a52d9a80SChris Mason rb_erase(&other->rb_node, &tree->state); 408a52d9a80SChris Mason free_extent_state(other); 409a52d9a80SChris Mason } 410a52d9a80SChris Mason } 411a52d9a80SChris Mason other_node = rb_next(&state->rb_node); 412a52d9a80SChris Mason if (other_node) { 413a52d9a80SChris Mason other = rb_entry(other_node, struct extent_state, rb_node); 414a52d9a80SChris Mason if (other->start == state->end + 1 && 415a52d9a80SChris Mason other->state == state->state) { 416a52d9a80SChris Mason other->start = state->start; 417a52d9a80SChris Mason state->in_tree = 0; 418a52d9a80SChris Mason rb_erase(&state->rb_node, &tree->state); 419a52d9a80SChris Mason free_extent_state(state); 420a52d9a80SChris Mason } 421a52d9a80SChris Mason } 422a52d9a80SChris Mason return 0; 423a52d9a80SChris Mason } 424a52d9a80SChris Mason 425a52d9a80SChris Mason /* 426a52d9a80SChris Mason * insert an extent_state struct into the tree. 'bits' are set on the 427a52d9a80SChris Mason * struct before it is inserted. 428a52d9a80SChris Mason * 429a52d9a80SChris Mason * This may return -EEXIST if the extent is already there, in which case the 430a52d9a80SChris Mason * state struct is freed. 431a52d9a80SChris Mason * 432a52d9a80SChris Mason * The tree lock is not taken internally. This is a utility function and 433a52d9a80SChris Mason * probably isn't what you want to call (see set/clear_extent_bit). 434a52d9a80SChris Mason */ 435a52d9a80SChris Mason static int insert_state(struct extent_map_tree *tree, 436a52d9a80SChris Mason struct extent_state *state, u64 start, u64 end, 437a52d9a80SChris Mason int bits) 438a52d9a80SChris Mason { 439a52d9a80SChris Mason struct rb_node *node; 440a52d9a80SChris Mason 441a52d9a80SChris Mason if (end < start) { 442a52d9a80SChris Mason printk("end < start %Lu %Lu\n", end, start); 443a52d9a80SChris Mason WARN_ON(1); 444a52d9a80SChris Mason } 445ca664626SChris Mason if (bits & EXTENT_DIRTY) 446ca664626SChris Mason tree->dirty_bytes += end - start + 1; 447a52d9a80SChris Mason state->state |= bits; 448a52d9a80SChris Mason state->start = start; 449a52d9a80SChris Mason state->end = end; 450a52d9a80SChris Mason node = tree_insert(&tree->state, end, &state->rb_node); 451a52d9a80SChris Mason if (node) { 452a52d9a80SChris Mason struct extent_state *found; 453a52d9a80SChris Mason found = rb_entry(node, struct extent_state, rb_node); 454a52d9a80SChris Mason printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end); 455a52d9a80SChris Mason free_extent_state(state); 456a52d9a80SChris Mason return -EEXIST; 457a52d9a80SChris Mason } 458a52d9a80SChris Mason merge_state(tree, state); 459a52d9a80SChris Mason return 0; 460a52d9a80SChris Mason } 461a52d9a80SChris Mason 462a52d9a80SChris Mason /* 463a52d9a80SChris Mason * split a given extent state struct in two, inserting the preallocated 464a52d9a80SChris Mason * struct 'prealloc' as the newly created second half. 'split' indicates an 465a52d9a80SChris Mason * offset inside 'orig' where it should be split. 466a52d9a80SChris Mason * 467a52d9a80SChris Mason * Before calling, 468a52d9a80SChris Mason * the tree has 'orig' at [orig->start, orig->end]. After calling, there 469a52d9a80SChris Mason * are two extent state structs in the tree: 470a52d9a80SChris Mason * prealloc: [orig->start, split - 1] 471a52d9a80SChris Mason * orig: [ split, orig->end ] 472a52d9a80SChris Mason * 473a52d9a80SChris Mason * The tree locks are not taken by this function. They need to be held 474a52d9a80SChris Mason * by the caller. 475a52d9a80SChris Mason */ 476a52d9a80SChris Mason static int split_state(struct extent_map_tree *tree, struct extent_state *orig, 477a52d9a80SChris Mason struct extent_state *prealloc, u64 split) 478a52d9a80SChris Mason { 479a52d9a80SChris Mason struct rb_node *node; 480a52d9a80SChris Mason prealloc->start = orig->start; 481a52d9a80SChris Mason prealloc->end = split - 1; 482a52d9a80SChris Mason prealloc->state = orig->state; 483a52d9a80SChris Mason orig->start = split; 484f510cfecSChris Mason 485a52d9a80SChris Mason node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node); 486a52d9a80SChris Mason if (node) { 487a52d9a80SChris Mason struct extent_state *found; 488a52d9a80SChris Mason found = rb_entry(node, struct extent_state, rb_node); 489a52d9a80SChris Mason printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end); 490a52d9a80SChris Mason free_extent_state(prealloc); 491a52d9a80SChris Mason return -EEXIST; 492a52d9a80SChris Mason } 493a52d9a80SChris Mason return 0; 494a52d9a80SChris Mason } 495a52d9a80SChris Mason 496a52d9a80SChris Mason /* 497a52d9a80SChris Mason * utility function to clear some bits in an extent state struct. 498a52d9a80SChris Mason * it will optionally wake up any one waiting on this state (wake == 1), or 499a52d9a80SChris Mason * forcibly remove the state from the tree (delete == 1). 500a52d9a80SChris Mason * 501a52d9a80SChris Mason * If no bits are set on the state struct after clearing things, the 502a52d9a80SChris Mason * struct is freed and removed from the tree 503a52d9a80SChris Mason */ 504a52d9a80SChris Mason static int clear_state_bit(struct extent_map_tree *tree, 505a52d9a80SChris Mason struct extent_state *state, int bits, int wake, 506a52d9a80SChris Mason int delete) 507a52d9a80SChris Mason { 508a52d9a80SChris Mason int ret = state->state & bits; 509ca664626SChris Mason 510ca664626SChris Mason if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { 511ca664626SChris Mason u64 range = state->end - state->start + 1; 512ca664626SChris Mason WARN_ON(range > tree->dirty_bytes); 513ca664626SChris Mason tree->dirty_bytes -= range; 514ca664626SChris Mason } 515a52d9a80SChris Mason state->state &= ~bits; 516a52d9a80SChris Mason if (wake) 517a52d9a80SChris Mason wake_up(&state->wq); 518a52d9a80SChris Mason if (delete || state->state == 0) { 519a52d9a80SChris Mason if (state->in_tree) { 520a52d9a80SChris Mason rb_erase(&state->rb_node, &tree->state); 521a52d9a80SChris Mason state->in_tree = 0; 522a52d9a80SChris Mason free_extent_state(state); 523a52d9a80SChris Mason } else { 524a52d9a80SChris Mason WARN_ON(1); 525a52d9a80SChris Mason } 526a52d9a80SChris Mason } else { 527a52d9a80SChris Mason merge_state(tree, state); 528a52d9a80SChris Mason } 529a52d9a80SChris Mason return ret; 530a52d9a80SChris Mason } 531a52d9a80SChris Mason 532a52d9a80SChris Mason /* 533a52d9a80SChris Mason * clear some bits on a range in the tree. This may require splitting 534a52d9a80SChris Mason * or inserting elements in the tree, so the gfp mask is used to 535a52d9a80SChris Mason * indicate which allocations or sleeping are allowed. 536a52d9a80SChris Mason * 537a52d9a80SChris Mason * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove 538a52d9a80SChris Mason * the given range from the tree regardless of state (ie for truncate). 539a52d9a80SChris Mason * 540a52d9a80SChris Mason * the range [start, end] is inclusive. 541a52d9a80SChris Mason * 542a52d9a80SChris Mason * This takes the tree lock, and returns < 0 on error, > 0 if any of the 543a52d9a80SChris Mason * bits were already set, or zero if none of the bits were already set. 544a52d9a80SChris Mason */ 545a52d9a80SChris Mason int clear_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, 546a52d9a80SChris Mason int bits, int wake, int delete, gfp_t mask) 547a52d9a80SChris Mason { 548a52d9a80SChris Mason struct extent_state *state; 549a52d9a80SChris Mason struct extent_state *prealloc = NULL; 550a52d9a80SChris Mason struct rb_node *node; 55190f1c19aSChristoph Hellwig unsigned long flags; 552a52d9a80SChris Mason int err; 553a52d9a80SChris Mason int set = 0; 554a52d9a80SChris Mason 555a52d9a80SChris Mason again: 556a52d9a80SChris Mason if (!prealloc && (mask & __GFP_WAIT)) { 557a52d9a80SChris Mason prealloc = alloc_extent_state(mask); 558a52d9a80SChris Mason if (!prealloc) 559a52d9a80SChris Mason return -ENOMEM; 560a52d9a80SChris Mason } 561a52d9a80SChris Mason 56290f1c19aSChristoph Hellwig write_lock_irqsave(&tree->lock, flags); 563a52d9a80SChris Mason /* 564a52d9a80SChris Mason * this search will find the extents that end after 565a52d9a80SChris Mason * our range starts 566a52d9a80SChris Mason */ 567a52d9a80SChris Mason node = tree_search(&tree->state, start); 568a52d9a80SChris Mason if (!node) 569a52d9a80SChris Mason goto out; 570a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 571a52d9a80SChris Mason if (state->start > end) 572a52d9a80SChris Mason goto out; 573a52d9a80SChris Mason WARN_ON(state->end < start); 574a52d9a80SChris Mason 575a52d9a80SChris Mason /* 576a52d9a80SChris Mason * | ---- desired range ---- | 577a52d9a80SChris Mason * | state | or 578a52d9a80SChris Mason * | ------------- state -------------- | 579a52d9a80SChris Mason * 580a52d9a80SChris Mason * We need to split the extent we found, and may flip 581a52d9a80SChris Mason * bits on second half. 582a52d9a80SChris Mason * 583a52d9a80SChris Mason * If the extent we found extends past our range, we 584a52d9a80SChris Mason * just split and search again. It'll get split again 585a52d9a80SChris Mason * the next time though. 586a52d9a80SChris Mason * 587a52d9a80SChris Mason * If the extent we found is inside our range, we clear 588a52d9a80SChris Mason * the desired bit on it. 589a52d9a80SChris Mason */ 590a52d9a80SChris Mason 591a52d9a80SChris Mason if (state->start < start) { 592a52d9a80SChris Mason err = split_state(tree, state, prealloc, start); 593a52d9a80SChris Mason BUG_ON(err == -EEXIST); 594a52d9a80SChris Mason prealloc = NULL; 595a52d9a80SChris Mason if (err) 596a52d9a80SChris Mason goto out; 597a52d9a80SChris Mason if (state->end <= end) { 598a52d9a80SChris Mason start = state->end + 1; 599a52d9a80SChris Mason set |= clear_state_bit(tree, state, bits, 600a52d9a80SChris Mason wake, delete); 601a52d9a80SChris Mason } else { 602a52d9a80SChris Mason start = state->start; 603a52d9a80SChris Mason } 604a52d9a80SChris Mason goto search_again; 605a52d9a80SChris Mason } 606a52d9a80SChris Mason /* 607a52d9a80SChris Mason * | ---- desired range ---- | 608a52d9a80SChris Mason * | state | 609a52d9a80SChris Mason * We need to split the extent, and clear the bit 610a52d9a80SChris Mason * on the first half 611a52d9a80SChris Mason */ 612a52d9a80SChris Mason if (state->start <= end && state->end > end) { 613a52d9a80SChris Mason err = split_state(tree, state, prealloc, end + 1); 614a52d9a80SChris Mason BUG_ON(err == -EEXIST); 615a52d9a80SChris Mason 616a52d9a80SChris Mason if (wake) 617a52d9a80SChris Mason wake_up(&state->wq); 618a52d9a80SChris Mason set |= clear_state_bit(tree, prealloc, bits, 619a52d9a80SChris Mason wake, delete); 620a52d9a80SChris Mason prealloc = NULL; 621a52d9a80SChris Mason goto out; 622a52d9a80SChris Mason } 623a52d9a80SChris Mason 624a52d9a80SChris Mason start = state->end + 1; 625a52d9a80SChris Mason set |= clear_state_bit(tree, state, bits, wake, delete); 626a52d9a80SChris Mason goto search_again; 627a52d9a80SChris Mason 628a52d9a80SChris Mason out: 62990f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 630a52d9a80SChris Mason if (prealloc) 631a52d9a80SChris Mason free_extent_state(prealloc); 632a52d9a80SChris Mason 633a52d9a80SChris Mason return set; 634a52d9a80SChris Mason 635a52d9a80SChris Mason search_again: 63696b5179dSChris Mason if (start > end) 637a52d9a80SChris Mason goto out; 63890f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 639a52d9a80SChris Mason if (mask & __GFP_WAIT) 640a52d9a80SChris Mason cond_resched(); 641a52d9a80SChris Mason goto again; 642a52d9a80SChris Mason } 643a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_bit); 644a52d9a80SChris Mason 645a52d9a80SChris Mason static int wait_on_state(struct extent_map_tree *tree, 646a52d9a80SChris Mason struct extent_state *state) 647a52d9a80SChris Mason { 648a52d9a80SChris Mason DEFINE_WAIT(wait); 649a52d9a80SChris Mason prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); 650a52d9a80SChris Mason read_unlock_irq(&tree->lock); 651a52d9a80SChris Mason schedule(); 652a52d9a80SChris Mason read_lock_irq(&tree->lock); 653a52d9a80SChris Mason finish_wait(&state->wq, &wait); 654a52d9a80SChris Mason return 0; 655a52d9a80SChris Mason } 656a52d9a80SChris Mason 657a52d9a80SChris Mason /* 658a52d9a80SChris Mason * waits for one or more bits to clear on a range in the state tree. 659a52d9a80SChris Mason * The range [start, end] is inclusive. 660a52d9a80SChris Mason * The tree lock is taken by this function 661a52d9a80SChris Mason */ 662a52d9a80SChris Mason int wait_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits) 663a52d9a80SChris Mason { 664a52d9a80SChris Mason struct extent_state *state; 665a52d9a80SChris Mason struct rb_node *node; 666a52d9a80SChris Mason 667a52d9a80SChris Mason read_lock_irq(&tree->lock); 668a52d9a80SChris Mason again: 669a52d9a80SChris Mason while (1) { 670a52d9a80SChris Mason /* 671a52d9a80SChris Mason * this search will find all the extents that end after 672a52d9a80SChris Mason * our range starts 673a52d9a80SChris Mason */ 674a52d9a80SChris Mason node = tree_search(&tree->state, start); 675a52d9a80SChris Mason if (!node) 676a52d9a80SChris Mason break; 677a52d9a80SChris Mason 678a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 679a52d9a80SChris Mason 680a52d9a80SChris Mason if (state->start > end) 681a52d9a80SChris Mason goto out; 682a52d9a80SChris Mason 683a52d9a80SChris Mason if (state->state & bits) { 684a52d9a80SChris Mason start = state->start; 685a52d9a80SChris Mason atomic_inc(&state->refs); 686a52d9a80SChris Mason wait_on_state(tree, state); 687a52d9a80SChris Mason free_extent_state(state); 688a52d9a80SChris Mason goto again; 689a52d9a80SChris Mason } 690a52d9a80SChris Mason start = state->end + 1; 691a52d9a80SChris Mason 692a52d9a80SChris Mason if (start > end) 693a52d9a80SChris Mason break; 694a52d9a80SChris Mason 695a52d9a80SChris Mason if (need_resched()) { 696a52d9a80SChris Mason read_unlock_irq(&tree->lock); 697a52d9a80SChris Mason cond_resched(); 698a52d9a80SChris Mason read_lock_irq(&tree->lock); 699a52d9a80SChris Mason } 700a52d9a80SChris Mason } 701a52d9a80SChris Mason out: 702a52d9a80SChris Mason read_unlock_irq(&tree->lock); 703a52d9a80SChris Mason return 0; 704a52d9a80SChris Mason } 705a52d9a80SChris Mason EXPORT_SYMBOL(wait_extent_bit); 706a52d9a80SChris Mason 707ca664626SChris Mason static void set_state_bits(struct extent_map_tree *tree, 708ca664626SChris Mason struct extent_state *state, 709ca664626SChris Mason int bits) 710ca664626SChris Mason { 711ca664626SChris Mason if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { 712ca664626SChris Mason u64 range = state->end - state->start + 1; 713ca664626SChris Mason tree->dirty_bytes += range; 714ca664626SChris Mason } 715ca664626SChris Mason state->state |= bits; 716ca664626SChris Mason } 717ca664626SChris Mason 718a52d9a80SChris Mason /* 719a52d9a80SChris Mason * set some bits on a range in the tree. This may require allocations 720a52d9a80SChris Mason * or sleeping, so the gfp mask is used to indicate what is allowed. 721a52d9a80SChris Mason * 722a52d9a80SChris Mason * If 'exclusive' == 1, this will fail with -EEXIST if some part of the 723a52d9a80SChris Mason * range already has the desired bits set. The start of the existing 724a52d9a80SChris Mason * range is returned in failed_start in this case. 725a52d9a80SChris Mason * 726a52d9a80SChris Mason * [start, end] is inclusive 727a52d9a80SChris Mason * This takes the tree lock. 728a52d9a80SChris Mason */ 729a52d9a80SChris Mason int set_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits, 730a52d9a80SChris Mason int exclusive, u64 *failed_start, gfp_t mask) 731a52d9a80SChris Mason { 732a52d9a80SChris Mason struct extent_state *state; 733a52d9a80SChris Mason struct extent_state *prealloc = NULL; 734a52d9a80SChris Mason struct rb_node *node; 73590f1c19aSChristoph Hellwig unsigned long flags; 736a52d9a80SChris Mason int err = 0; 737a52d9a80SChris Mason int set; 738a52d9a80SChris Mason u64 last_start; 739a52d9a80SChris Mason u64 last_end; 740a52d9a80SChris Mason again: 741a52d9a80SChris Mason if (!prealloc && (mask & __GFP_WAIT)) { 742a52d9a80SChris Mason prealloc = alloc_extent_state(mask); 743a52d9a80SChris Mason if (!prealloc) 744a52d9a80SChris Mason return -ENOMEM; 745a52d9a80SChris Mason } 746a52d9a80SChris Mason 74790f1c19aSChristoph Hellwig write_lock_irqsave(&tree->lock, flags); 748a52d9a80SChris Mason /* 749a52d9a80SChris Mason * this search will find all the extents that end after 750a52d9a80SChris Mason * our range starts. 751a52d9a80SChris Mason */ 752a52d9a80SChris Mason node = tree_search(&tree->state, start); 753a52d9a80SChris Mason if (!node) { 754a52d9a80SChris Mason err = insert_state(tree, prealloc, start, end, bits); 755a52d9a80SChris Mason prealloc = NULL; 756a52d9a80SChris Mason BUG_ON(err == -EEXIST); 757a52d9a80SChris Mason goto out; 758a52d9a80SChris Mason } 759a52d9a80SChris Mason 760a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 761a52d9a80SChris Mason last_start = state->start; 762a52d9a80SChris Mason last_end = state->end; 763a52d9a80SChris Mason 764a52d9a80SChris Mason /* 765a52d9a80SChris Mason * | ---- desired range ---- | 766a52d9a80SChris Mason * | state | 767a52d9a80SChris Mason * 768a52d9a80SChris Mason * Just lock what we found and keep going 769a52d9a80SChris Mason */ 770a52d9a80SChris Mason if (state->start == start && state->end <= end) { 771a52d9a80SChris Mason set = state->state & bits; 772a52d9a80SChris Mason if (set && exclusive) { 773a52d9a80SChris Mason *failed_start = state->start; 774a52d9a80SChris Mason err = -EEXIST; 775a52d9a80SChris Mason goto out; 776a52d9a80SChris Mason } 777ca664626SChris Mason set_state_bits(tree, state, bits); 778a52d9a80SChris Mason start = state->end + 1; 779a52d9a80SChris Mason merge_state(tree, state); 780a52d9a80SChris Mason goto search_again; 781a52d9a80SChris Mason } 782a52d9a80SChris Mason 783a52d9a80SChris Mason /* 784a52d9a80SChris Mason * | ---- desired range ---- | 785a52d9a80SChris Mason * | state | 786a52d9a80SChris Mason * or 787a52d9a80SChris Mason * | ------------- state -------------- | 788a52d9a80SChris Mason * 789a52d9a80SChris Mason * We need to split the extent we found, and may flip bits on 790a52d9a80SChris Mason * second half. 791a52d9a80SChris Mason * 792a52d9a80SChris Mason * If the extent we found extends past our 793a52d9a80SChris Mason * range, we just split and search again. It'll get split 794a52d9a80SChris Mason * again the next time though. 795a52d9a80SChris Mason * 796a52d9a80SChris Mason * If the extent we found is inside our range, we set the 797a52d9a80SChris Mason * desired bit on it. 798a52d9a80SChris Mason */ 799a52d9a80SChris Mason if (state->start < start) { 800a52d9a80SChris Mason set = state->state & bits; 801a52d9a80SChris Mason if (exclusive && set) { 802a52d9a80SChris Mason *failed_start = start; 803a52d9a80SChris Mason err = -EEXIST; 804a52d9a80SChris Mason goto out; 805a52d9a80SChris Mason } 806a52d9a80SChris Mason err = split_state(tree, state, prealloc, start); 807a52d9a80SChris Mason BUG_ON(err == -EEXIST); 808a52d9a80SChris Mason prealloc = NULL; 809a52d9a80SChris Mason if (err) 810a52d9a80SChris Mason goto out; 811a52d9a80SChris Mason if (state->end <= end) { 812ca664626SChris Mason set_state_bits(tree, state, bits); 813a52d9a80SChris Mason start = state->end + 1; 814a52d9a80SChris Mason merge_state(tree, state); 815a52d9a80SChris Mason } else { 816a52d9a80SChris Mason start = state->start; 817a52d9a80SChris Mason } 818a52d9a80SChris Mason goto search_again; 819a52d9a80SChris Mason } 820a52d9a80SChris Mason /* 821a52d9a80SChris Mason * | ---- desired range ---- | 822a52d9a80SChris Mason * | state | or | state | 823a52d9a80SChris Mason * 824a52d9a80SChris Mason * There's a hole, we need to insert something in it and 825a52d9a80SChris Mason * ignore the extent we found. 826a52d9a80SChris Mason */ 827a52d9a80SChris Mason if (state->start > start) { 828a52d9a80SChris Mason u64 this_end; 829a52d9a80SChris Mason if (end < last_start) 830a52d9a80SChris Mason this_end = end; 831a52d9a80SChris Mason else 832a52d9a80SChris Mason this_end = last_start -1; 833a52d9a80SChris Mason err = insert_state(tree, prealloc, start, this_end, 834a52d9a80SChris Mason bits); 835a52d9a80SChris Mason prealloc = NULL; 836a52d9a80SChris Mason BUG_ON(err == -EEXIST); 837a52d9a80SChris Mason if (err) 838a52d9a80SChris Mason goto out; 839a52d9a80SChris Mason start = this_end + 1; 840a52d9a80SChris Mason goto search_again; 841a52d9a80SChris Mason } 842a8c450b2SChris Mason /* 843a8c450b2SChris Mason * | ---- desired range ---- | 844a8c450b2SChris Mason * | state | 845a8c450b2SChris Mason * We need to split the extent, and set the bit 846a8c450b2SChris Mason * on the first half 847a8c450b2SChris Mason */ 848a8c450b2SChris Mason if (state->start <= end && state->end > end) { 849a8c450b2SChris Mason set = state->state & bits; 850a8c450b2SChris Mason if (exclusive && set) { 851a8c450b2SChris Mason *failed_start = start; 852a8c450b2SChris Mason err = -EEXIST; 853a8c450b2SChris Mason goto out; 854a8c450b2SChris Mason } 855a8c450b2SChris Mason err = split_state(tree, state, prealloc, end + 1); 856a8c450b2SChris Mason BUG_ON(err == -EEXIST); 857a8c450b2SChris Mason 858ca664626SChris Mason set_state_bits(tree, prealloc, bits); 859a8c450b2SChris Mason merge_state(tree, prealloc); 860a8c450b2SChris Mason prealloc = NULL; 861a8c450b2SChris Mason goto out; 862a8c450b2SChris Mason } 863a8c450b2SChris Mason 864a52d9a80SChris Mason goto search_again; 865a52d9a80SChris Mason 866a52d9a80SChris Mason out: 86790f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 868a52d9a80SChris Mason if (prealloc) 869a52d9a80SChris Mason free_extent_state(prealloc); 870a52d9a80SChris Mason 871a52d9a80SChris Mason return err; 872a52d9a80SChris Mason 873a52d9a80SChris Mason search_again: 874a52d9a80SChris Mason if (start > end) 875a52d9a80SChris Mason goto out; 87690f1c19aSChristoph Hellwig write_unlock_irqrestore(&tree->lock, flags); 877a52d9a80SChris Mason if (mask & __GFP_WAIT) 878a52d9a80SChris Mason cond_resched(); 879a52d9a80SChris Mason goto again; 880a52d9a80SChris Mason } 881a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_bit); 882a52d9a80SChris Mason 883a52d9a80SChris Mason /* wrappers around set/clear extent bit */ 884a52d9a80SChris Mason int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end, 885a52d9a80SChris Mason gfp_t mask) 886a52d9a80SChris Mason { 887a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL, 888a52d9a80SChris Mason mask); 889a52d9a80SChris Mason } 890a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_dirty); 891a52d9a80SChris Mason 89296b5179dSChris Mason int set_extent_bits(struct extent_map_tree *tree, u64 start, u64 end, 89396b5179dSChris Mason int bits, gfp_t mask) 89496b5179dSChris Mason { 89596b5179dSChris Mason return set_extent_bit(tree, start, end, bits, 0, NULL, 89696b5179dSChris Mason mask); 89796b5179dSChris Mason } 89896b5179dSChris Mason EXPORT_SYMBOL(set_extent_bits); 89996b5179dSChris Mason 90096b5179dSChris Mason int clear_extent_bits(struct extent_map_tree *tree, u64 start, u64 end, 90196b5179dSChris Mason int bits, gfp_t mask) 90296b5179dSChris Mason { 90396b5179dSChris Mason return clear_extent_bit(tree, start, end, bits, 0, 0, mask); 90496b5179dSChris Mason } 90596b5179dSChris Mason EXPORT_SYMBOL(clear_extent_bits); 90696b5179dSChris Mason 907b888db2bSChris Mason int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end, 908b888db2bSChris Mason gfp_t mask) 909b888db2bSChris Mason { 910b888db2bSChris Mason return set_extent_bit(tree, start, end, 911b888db2bSChris Mason EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL, 912b888db2bSChris Mason mask); 913b888db2bSChris Mason } 914b888db2bSChris Mason EXPORT_SYMBOL(set_extent_delalloc); 915b888db2bSChris Mason 916a52d9a80SChris Mason int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end, 917a52d9a80SChris Mason gfp_t mask) 918a52d9a80SChris Mason { 919b888db2bSChris Mason return clear_extent_bit(tree, start, end, 920b888db2bSChris Mason EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask); 921a52d9a80SChris Mason } 922a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_dirty); 923a52d9a80SChris Mason 924a52d9a80SChris Mason int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end, 925a52d9a80SChris Mason gfp_t mask) 926a52d9a80SChris Mason { 927a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL, 928a52d9a80SChris Mason mask); 929a52d9a80SChris Mason } 930a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_new); 931a52d9a80SChris Mason 932a52d9a80SChris Mason int clear_extent_new(struct extent_map_tree *tree, u64 start, u64 end, 933a52d9a80SChris Mason gfp_t mask) 934a52d9a80SChris Mason { 935a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask); 936a52d9a80SChris Mason } 937a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_new); 938a52d9a80SChris Mason 939a52d9a80SChris Mason int set_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end, 940a52d9a80SChris Mason gfp_t mask) 941a52d9a80SChris Mason { 942a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL, 943a52d9a80SChris Mason mask); 944a52d9a80SChris Mason } 945a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_uptodate); 946a52d9a80SChris Mason 947a52d9a80SChris Mason int clear_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end, 948a52d9a80SChris Mason gfp_t mask) 949a52d9a80SChris Mason { 950a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask); 951a52d9a80SChris Mason } 952a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_uptodate); 953a52d9a80SChris Mason 954a52d9a80SChris Mason int set_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end, 955a52d9a80SChris Mason gfp_t mask) 956a52d9a80SChris Mason { 957a52d9a80SChris Mason return set_extent_bit(tree, start, end, EXTENT_WRITEBACK, 958a52d9a80SChris Mason 0, NULL, mask); 959a52d9a80SChris Mason } 960a52d9a80SChris Mason EXPORT_SYMBOL(set_extent_writeback); 961a52d9a80SChris Mason 962a52d9a80SChris Mason int clear_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end, 963a52d9a80SChris Mason gfp_t mask) 964a52d9a80SChris Mason { 965a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask); 966a52d9a80SChris Mason } 967a52d9a80SChris Mason EXPORT_SYMBOL(clear_extent_writeback); 968a52d9a80SChris Mason 969a52d9a80SChris Mason int wait_on_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end) 970a52d9a80SChris Mason { 971a52d9a80SChris Mason return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK); 972a52d9a80SChris Mason } 973a52d9a80SChris Mason EXPORT_SYMBOL(wait_on_extent_writeback); 974a52d9a80SChris Mason 975a52d9a80SChris Mason /* 976a52d9a80SChris Mason * locks a range in ascending order, waiting for any locked regions 977a52d9a80SChris Mason * it hits on the way. [start,end] are inclusive, and this will sleep. 978a52d9a80SChris Mason */ 979a52d9a80SChris Mason int lock_extent(struct extent_map_tree *tree, u64 start, u64 end, gfp_t mask) 980a52d9a80SChris Mason { 981a52d9a80SChris Mason int err; 982a52d9a80SChris Mason u64 failed_start; 983a52d9a80SChris Mason while (1) { 984a52d9a80SChris Mason err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 985a52d9a80SChris Mason &failed_start, mask); 986a52d9a80SChris Mason if (err == -EEXIST && (mask & __GFP_WAIT)) { 987a52d9a80SChris Mason wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); 988a52d9a80SChris Mason start = failed_start; 989a52d9a80SChris Mason } else { 990a52d9a80SChris Mason break; 991a52d9a80SChris Mason } 992a52d9a80SChris Mason WARN_ON(start > end); 993a52d9a80SChris Mason } 994a52d9a80SChris Mason return err; 995a52d9a80SChris Mason } 996a52d9a80SChris Mason EXPORT_SYMBOL(lock_extent); 997a52d9a80SChris Mason 998a52d9a80SChris Mason int unlock_extent(struct extent_map_tree *tree, u64 start, u64 end, 999a52d9a80SChris Mason gfp_t mask) 1000a52d9a80SChris Mason { 1001a52d9a80SChris Mason return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask); 1002a52d9a80SChris Mason } 1003a52d9a80SChris Mason EXPORT_SYMBOL(unlock_extent); 1004a52d9a80SChris Mason 1005a52d9a80SChris Mason /* 1006a52d9a80SChris Mason * helper function to set pages and extents in the tree dirty 1007a52d9a80SChris Mason */ 1008a52d9a80SChris Mason int set_range_dirty(struct extent_map_tree *tree, u64 start, u64 end) 1009a52d9a80SChris Mason { 1010a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 1011a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 1012a52d9a80SChris Mason struct page *page; 1013a52d9a80SChris Mason 1014a52d9a80SChris Mason while (index <= end_index) { 1015a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 1016a52d9a80SChris Mason BUG_ON(!page); 1017a52d9a80SChris Mason __set_page_dirty_nobuffers(page); 1018a52d9a80SChris Mason page_cache_release(page); 1019a52d9a80SChris Mason index++; 1020a52d9a80SChris Mason } 1021a52d9a80SChris Mason set_extent_dirty(tree, start, end, GFP_NOFS); 1022a52d9a80SChris Mason return 0; 1023a52d9a80SChris Mason } 1024a52d9a80SChris Mason EXPORT_SYMBOL(set_range_dirty); 1025a52d9a80SChris Mason 1026a52d9a80SChris Mason /* 1027a52d9a80SChris Mason * helper function to set both pages and extents in the tree writeback 1028a52d9a80SChris Mason */ 1029a52d9a80SChris Mason int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end) 1030a52d9a80SChris Mason { 1031a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 1032a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 1033a52d9a80SChris Mason struct page *page; 1034a52d9a80SChris Mason 1035a52d9a80SChris Mason while (index <= end_index) { 1036a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 1037a52d9a80SChris Mason BUG_ON(!page); 1038a52d9a80SChris Mason set_page_writeback(page); 1039a52d9a80SChris Mason page_cache_release(page); 1040a52d9a80SChris Mason index++; 1041a52d9a80SChris Mason } 1042a52d9a80SChris Mason set_extent_writeback(tree, start, end, GFP_NOFS); 1043a52d9a80SChris Mason return 0; 1044a52d9a80SChris Mason } 1045a52d9a80SChris Mason EXPORT_SYMBOL(set_range_writeback); 1046a52d9a80SChris Mason 10475f39d397SChris Mason int find_first_extent_bit(struct extent_map_tree *tree, u64 start, 10485f39d397SChris Mason u64 *start_ret, u64 *end_ret, int bits) 10495f39d397SChris Mason { 10505f39d397SChris Mason struct rb_node *node; 10515f39d397SChris Mason struct extent_state *state; 10525f39d397SChris Mason int ret = 1; 10535f39d397SChris Mason 1054e19caa5fSChris Mason read_lock_irq(&tree->lock); 10555f39d397SChris Mason /* 10565f39d397SChris Mason * this search will find all the extents that end after 10575f39d397SChris Mason * our range starts. 10585f39d397SChris Mason */ 10595f39d397SChris Mason node = tree_search(&tree->state, start); 10605f39d397SChris Mason if (!node || IS_ERR(node)) { 10615f39d397SChris Mason goto out; 10625f39d397SChris Mason } 10635f39d397SChris Mason 10645f39d397SChris Mason while(1) { 10655f39d397SChris Mason state = rb_entry(node, struct extent_state, rb_node); 1066e19caa5fSChris Mason if (state->end >= start && (state->state & bits)) { 10675f39d397SChris Mason *start_ret = state->start; 10685f39d397SChris Mason *end_ret = state->end; 10695f39d397SChris Mason ret = 0; 1070f510cfecSChris Mason break; 10715f39d397SChris Mason } 10725f39d397SChris Mason node = rb_next(node); 10735f39d397SChris Mason if (!node) 10745f39d397SChris Mason break; 10755f39d397SChris Mason } 10765f39d397SChris Mason out: 1077e19caa5fSChris Mason read_unlock_irq(&tree->lock); 10785f39d397SChris Mason return ret; 10795f39d397SChris Mason } 10805f39d397SChris Mason EXPORT_SYMBOL(find_first_extent_bit); 10815f39d397SChris Mason 1082b888db2bSChris Mason u64 find_lock_delalloc_range(struct extent_map_tree *tree, 10833e9fd94fSChris Mason u64 *start, u64 *end, u64 max_bytes) 1084b888db2bSChris Mason { 1085b888db2bSChris Mason struct rb_node *node; 1086b888db2bSChris Mason struct extent_state *state; 10873e9fd94fSChris Mason u64 cur_start = *start; 1088b888db2bSChris Mason u64 found = 0; 1089b888db2bSChris Mason u64 total_bytes = 0; 1090b888db2bSChris Mason 1091b888db2bSChris Mason write_lock_irq(&tree->lock); 1092b888db2bSChris Mason /* 1093b888db2bSChris Mason * this search will find all the extents that end after 1094b888db2bSChris Mason * our range starts. 1095b888db2bSChris Mason */ 1096b888db2bSChris Mason search_again: 1097b888db2bSChris Mason node = tree_search(&tree->state, cur_start); 1098b888db2bSChris Mason if (!node || IS_ERR(node)) { 1099190662b2SChris Mason *end = (u64)-1; 1100b888db2bSChris Mason goto out; 1101b888db2bSChris Mason } 1102b888db2bSChris Mason 1103b888db2bSChris Mason while(1) { 1104b888db2bSChris Mason state = rb_entry(node, struct extent_state, rb_node); 11053e9fd94fSChris Mason if (found && state->start != cur_start) { 1106b888db2bSChris Mason goto out; 1107b888db2bSChris Mason } 1108b888db2bSChris Mason if (!(state->state & EXTENT_DELALLOC)) { 1109190662b2SChris Mason if (!found) 1110190662b2SChris Mason *end = state->end; 1111b888db2bSChris Mason goto out; 1112b888db2bSChris Mason } 11133e9fd94fSChris Mason if (!found) { 11143e9fd94fSChris Mason struct extent_state *prev_state; 11153e9fd94fSChris Mason struct rb_node *prev_node = node; 11163e9fd94fSChris Mason while(1) { 11173e9fd94fSChris Mason prev_node = rb_prev(prev_node); 11183e9fd94fSChris Mason if (!prev_node) 11193e9fd94fSChris Mason break; 11203e9fd94fSChris Mason prev_state = rb_entry(prev_node, 11213e9fd94fSChris Mason struct extent_state, 11223e9fd94fSChris Mason rb_node); 11233e9fd94fSChris Mason if (!(prev_state->state & EXTENT_DELALLOC)) 11243e9fd94fSChris Mason break; 11253e9fd94fSChris Mason state = prev_state; 11263e9fd94fSChris Mason node = prev_node; 11273e9fd94fSChris Mason } 11283e9fd94fSChris Mason } 1129b888db2bSChris Mason if (state->state & EXTENT_LOCKED) { 1130b888db2bSChris Mason DEFINE_WAIT(wait); 1131b888db2bSChris Mason atomic_inc(&state->refs); 1132944746ecSYan prepare_to_wait(&state->wq, &wait, 1133944746ecSYan TASK_UNINTERRUPTIBLE); 1134b888db2bSChris Mason write_unlock_irq(&tree->lock); 1135b888db2bSChris Mason schedule(); 1136b888db2bSChris Mason write_lock_irq(&tree->lock); 1137b888db2bSChris Mason finish_wait(&state->wq, &wait); 1138b888db2bSChris Mason free_extent_state(state); 1139b888db2bSChris Mason goto search_again; 1140b888db2bSChris Mason } 1141b888db2bSChris Mason state->state |= EXTENT_LOCKED; 11423e9fd94fSChris Mason if (!found) 11433e9fd94fSChris Mason *start = state->start; 1144b888db2bSChris Mason found++; 1145b888db2bSChris Mason *end = state->end; 1146b888db2bSChris Mason cur_start = state->end + 1; 1147b888db2bSChris Mason node = rb_next(node); 1148b888db2bSChris Mason if (!node) 1149b888db2bSChris Mason break; 1150944746ecSYan total_bytes += state->end - state->start + 1; 1151b888db2bSChris Mason if (total_bytes >= max_bytes) 1152b888db2bSChris Mason break; 1153b888db2bSChris Mason } 1154b888db2bSChris Mason out: 1155b888db2bSChris Mason write_unlock_irq(&tree->lock); 1156b888db2bSChris Mason return found; 1157b888db2bSChris Mason } 1158b888db2bSChris Mason 1159793955bcSChris Mason u64 count_range_bits(struct extent_map_tree *tree, 11601832a6d5SChris Mason u64 *start, u64 search_end, u64 max_bytes, 11611832a6d5SChris Mason unsigned long bits) 1162793955bcSChris Mason { 1163793955bcSChris Mason struct rb_node *node; 1164793955bcSChris Mason struct extent_state *state; 1165793955bcSChris Mason u64 cur_start = *start; 1166793955bcSChris Mason u64 total_bytes = 0; 1167793955bcSChris Mason int found = 0; 1168793955bcSChris Mason 11691832a6d5SChris Mason if (search_end <= cur_start) { 11701832a6d5SChris Mason printk("search_end %Lu start %Lu\n", search_end, cur_start); 11711832a6d5SChris Mason WARN_ON(1); 11721832a6d5SChris Mason return 0; 11731832a6d5SChris Mason } 11741832a6d5SChris Mason 1175793955bcSChris Mason write_lock_irq(&tree->lock); 11761832a6d5SChris Mason if (cur_start == 0 && bits == EXTENT_DIRTY) { 1177ca664626SChris Mason total_bytes = tree->dirty_bytes; 1178ca664626SChris Mason goto out; 1179ca664626SChris Mason } 1180793955bcSChris Mason /* 1181793955bcSChris Mason * this search will find all the extents that end after 1182793955bcSChris Mason * our range starts. 1183793955bcSChris Mason */ 1184793955bcSChris Mason node = tree_search(&tree->state, cur_start); 1185793955bcSChris Mason if (!node || IS_ERR(node)) { 1186793955bcSChris Mason goto out; 1187793955bcSChris Mason } 1188793955bcSChris Mason 1189793955bcSChris Mason while(1) { 1190793955bcSChris Mason state = rb_entry(node, struct extent_state, rb_node); 11911832a6d5SChris Mason if (state->start > search_end) 11921832a6d5SChris Mason break; 11931832a6d5SChris Mason if (state->end >= cur_start && (state->state & bits)) { 11941832a6d5SChris Mason total_bytes += min(search_end, state->end) + 1 - 11951832a6d5SChris Mason max(cur_start, state->start); 1196793955bcSChris Mason if (total_bytes >= max_bytes) 1197793955bcSChris Mason break; 1198793955bcSChris Mason if (!found) { 1199793955bcSChris Mason *start = state->start; 1200793955bcSChris Mason found = 1; 1201793955bcSChris Mason } 1202793955bcSChris Mason } 1203793955bcSChris Mason node = rb_next(node); 1204793955bcSChris Mason if (!node) 1205793955bcSChris Mason break; 1206793955bcSChris Mason } 1207793955bcSChris Mason out: 1208793955bcSChris Mason write_unlock_irq(&tree->lock); 1209793955bcSChris Mason return total_bytes; 1210793955bcSChris Mason } 1211a52d9a80SChris Mason /* 1212a52d9a80SChris Mason * helper function to lock both pages and extents in the tree. 1213a52d9a80SChris Mason * pages must be locked first. 1214a52d9a80SChris Mason */ 1215a52d9a80SChris Mason int lock_range(struct extent_map_tree *tree, u64 start, u64 end) 1216a52d9a80SChris Mason { 1217a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 1218a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 1219a52d9a80SChris Mason struct page *page; 1220a52d9a80SChris Mason int err; 1221a52d9a80SChris Mason 1222a52d9a80SChris Mason while (index <= end_index) { 1223a52d9a80SChris Mason page = grab_cache_page(tree->mapping, index); 1224a52d9a80SChris Mason if (!page) { 1225a52d9a80SChris Mason err = -ENOMEM; 1226a52d9a80SChris Mason goto failed; 1227a52d9a80SChris Mason } 1228a52d9a80SChris Mason if (IS_ERR(page)) { 1229a52d9a80SChris Mason err = PTR_ERR(page); 1230a52d9a80SChris Mason goto failed; 1231a52d9a80SChris Mason } 1232a52d9a80SChris Mason index++; 1233a52d9a80SChris Mason } 1234a52d9a80SChris Mason lock_extent(tree, start, end, GFP_NOFS); 1235a52d9a80SChris Mason return 0; 1236a52d9a80SChris Mason 1237a52d9a80SChris Mason failed: 1238a52d9a80SChris Mason /* 1239a52d9a80SChris Mason * we failed above in getting the page at 'index', so we undo here 1240a52d9a80SChris Mason * up to but not including the page at 'index' 1241a52d9a80SChris Mason */ 1242a52d9a80SChris Mason end_index = index; 1243a52d9a80SChris Mason index = start >> PAGE_CACHE_SHIFT; 1244a52d9a80SChris Mason while (index < end_index) { 1245a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 1246a52d9a80SChris Mason unlock_page(page); 1247a52d9a80SChris Mason page_cache_release(page); 1248a52d9a80SChris Mason index++; 1249a52d9a80SChris Mason } 1250a52d9a80SChris Mason return err; 1251a52d9a80SChris Mason } 1252a52d9a80SChris Mason EXPORT_SYMBOL(lock_range); 1253a52d9a80SChris Mason 1254a52d9a80SChris Mason /* 1255a52d9a80SChris Mason * helper function to unlock both pages and extents in the tree. 1256a52d9a80SChris Mason */ 1257a52d9a80SChris Mason int unlock_range(struct extent_map_tree *tree, u64 start, u64 end) 1258a52d9a80SChris Mason { 1259a52d9a80SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 1260a52d9a80SChris Mason unsigned long end_index = end >> PAGE_CACHE_SHIFT; 1261a52d9a80SChris Mason struct page *page; 1262a52d9a80SChris Mason 1263a52d9a80SChris Mason while (index <= end_index) { 1264a52d9a80SChris Mason page = find_get_page(tree->mapping, index); 1265a52d9a80SChris Mason unlock_page(page); 1266a52d9a80SChris Mason page_cache_release(page); 1267a52d9a80SChris Mason index++; 1268a52d9a80SChris Mason } 1269a52d9a80SChris Mason unlock_extent(tree, start, end, GFP_NOFS); 1270a52d9a80SChris Mason return 0; 1271a52d9a80SChris Mason } 1272a52d9a80SChris Mason EXPORT_SYMBOL(unlock_range); 1273a52d9a80SChris Mason 127407157aacSChris Mason int set_state_private(struct extent_map_tree *tree, u64 start, u64 private) 127507157aacSChris Mason { 127607157aacSChris Mason struct rb_node *node; 127707157aacSChris Mason struct extent_state *state; 127807157aacSChris Mason int ret = 0; 127907157aacSChris Mason 128007157aacSChris Mason write_lock_irq(&tree->lock); 128107157aacSChris Mason /* 128207157aacSChris Mason * this search will find all the extents that end after 128307157aacSChris Mason * our range starts. 128407157aacSChris Mason */ 128507157aacSChris Mason node = tree_search(&tree->state, start); 128607157aacSChris Mason if (!node || IS_ERR(node)) { 128707157aacSChris Mason ret = -ENOENT; 128807157aacSChris Mason goto out; 128907157aacSChris Mason } 129007157aacSChris Mason state = rb_entry(node, struct extent_state, rb_node); 129107157aacSChris Mason if (state->start != start) { 129207157aacSChris Mason ret = -ENOENT; 129307157aacSChris Mason goto out; 129407157aacSChris Mason } 129507157aacSChris Mason state->private = private; 129607157aacSChris Mason out: 129707157aacSChris Mason write_unlock_irq(&tree->lock); 129807157aacSChris Mason return ret; 129907157aacSChris Mason } 130007157aacSChris Mason 130107157aacSChris Mason int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private) 130207157aacSChris Mason { 130307157aacSChris Mason struct rb_node *node; 130407157aacSChris Mason struct extent_state *state; 130507157aacSChris Mason int ret = 0; 130607157aacSChris Mason 130707157aacSChris Mason read_lock_irq(&tree->lock); 130807157aacSChris Mason /* 130907157aacSChris Mason * this search will find all the extents that end after 131007157aacSChris Mason * our range starts. 131107157aacSChris Mason */ 131207157aacSChris Mason node = tree_search(&tree->state, start); 131307157aacSChris Mason if (!node || IS_ERR(node)) { 131407157aacSChris Mason ret = -ENOENT; 131507157aacSChris Mason goto out; 131607157aacSChris Mason } 131707157aacSChris Mason state = rb_entry(node, struct extent_state, rb_node); 131807157aacSChris Mason if (state->start != start) { 131907157aacSChris Mason ret = -ENOENT; 132007157aacSChris Mason goto out; 132107157aacSChris Mason } 132207157aacSChris Mason *private = state->private; 132307157aacSChris Mason out: 132407157aacSChris Mason read_unlock_irq(&tree->lock); 132507157aacSChris Mason return ret; 132607157aacSChris Mason } 132707157aacSChris Mason 1328a52d9a80SChris Mason /* 1329a52d9a80SChris Mason * searches a range in the state tree for a given mask. 1330a52d9a80SChris Mason * If 'filled' == 1, this returns 1 only if ever extent in the tree 1331a52d9a80SChris Mason * has the bits set. Otherwise, 1 is returned if any bit in the 1332a52d9a80SChris Mason * range is found set. 1333a52d9a80SChris Mason */ 13341a5bc167SChris Mason int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end, 1335a52d9a80SChris Mason int bits, int filled) 1336a52d9a80SChris Mason { 1337a52d9a80SChris Mason struct extent_state *state = NULL; 1338a52d9a80SChris Mason struct rb_node *node; 1339a52d9a80SChris Mason int bitset = 0; 1340a52d9a80SChris Mason 1341a52d9a80SChris Mason read_lock_irq(&tree->lock); 1342a52d9a80SChris Mason node = tree_search(&tree->state, start); 1343a52d9a80SChris Mason while (node && start <= end) { 1344a52d9a80SChris Mason state = rb_entry(node, struct extent_state, rb_node); 1345a52d9a80SChris Mason 1346a52d9a80SChris Mason if (filled && state->start > start) { 1347a52d9a80SChris Mason bitset = 0; 1348a52d9a80SChris Mason break; 1349a52d9a80SChris Mason } 13500591fb56SChris Mason 13510591fb56SChris Mason if (state->start > end) 13520591fb56SChris Mason break; 13530591fb56SChris Mason 1354a52d9a80SChris Mason if (state->state & bits) { 1355a52d9a80SChris Mason bitset = 1; 1356a52d9a80SChris Mason if (!filled) 1357a52d9a80SChris Mason break; 1358a52d9a80SChris Mason } else if (filled) { 1359a52d9a80SChris Mason bitset = 0; 1360a52d9a80SChris Mason break; 1361a52d9a80SChris Mason } 1362a52d9a80SChris Mason start = state->end + 1; 1363a52d9a80SChris Mason if (start > end) 1364a52d9a80SChris Mason break; 1365a52d9a80SChris Mason node = rb_next(node); 1366f0c5da14SYan if (!node) { 1367f0c5da14SYan if (filled) 1368f0c5da14SYan bitset = 0; 1369f0c5da14SYan break; 1370f0c5da14SYan } 1371a52d9a80SChris Mason } 1372a52d9a80SChris Mason read_unlock_irq(&tree->lock); 1373a52d9a80SChris Mason return bitset; 1374a52d9a80SChris Mason } 13751a5bc167SChris Mason EXPORT_SYMBOL(test_range_bit); 1376a52d9a80SChris Mason 1377a52d9a80SChris Mason /* 1378a52d9a80SChris Mason * helper function to set a given page up to date if all the 1379a52d9a80SChris Mason * extents in the tree for that page are up to date 1380a52d9a80SChris Mason */ 1381a52d9a80SChris Mason static int check_page_uptodate(struct extent_map_tree *tree, 1382a52d9a80SChris Mason struct page *page) 1383a52d9a80SChris Mason { 138435ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1385a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1386a52d9a80SChris Mason if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1)) 1387a52d9a80SChris Mason SetPageUptodate(page); 1388a52d9a80SChris Mason return 0; 1389a52d9a80SChris Mason } 1390a52d9a80SChris Mason 1391a52d9a80SChris Mason /* 1392a52d9a80SChris Mason * helper function to unlock a page if all the extents in the tree 1393a52d9a80SChris Mason * for that page are unlocked 1394a52d9a80SChris Mason */ 1395a52d9a80SChris Mason static int check_page_locked(struct extent_map_tree *tree, 1396a52d9a80SChris Mason struct page *page) 1397a52d9a80SChris Mason { 139835ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1399a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1400a52d9a80SChris Mason if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0)) 1401a52d9a80SChris Mason unlock_page(page); 1402a52d9a80SChris Mason return 0; 1403a52d9a80SChris Mason } 1404a52d9a80SChris Mason 1405a52d9a80SChris Mason /* 1406a52d9a80SChris Mason * helper function to end page writeback if all the extents 1407a52d9a80SChris Mason * in the tree for that page are done with writeback 1408a52d9a80SChris Mason */ 1409a52d9a80SChris Mason static int check_page_writeback(struct extent_map_tree *tree, 1410a52d9a80SChris Mason struct page *page) 1411a52d9a80SChris Mason { 141235ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1413a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 1414a52d9a80SChris Mason if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0)) 1415a52d9a80SChris Mason end_page_writeback(page); 1416a52d9a80SChris Mason return 0; 1417a52d9a80SChris Mason } 1418a52d9a80SChris Mason 1419a52d9a80SChris Mason /* lots and lots of room for performance fixes in the end_bio funcs */ 1420a52d9a80SChris Mason 1421a52d9a80SChris Mason /* 1422a52d9a80SChris Mason * after a writepage IO is done, we need to: 1423a52d9a80SChris Mason * clear the uptodate bits on error 1424a52d9a80SChris Mason * clear the writeback bits in the extent tree for this IO 1425a52d9a80SChris Mason * end_page_writeback if the page has no more pending IO 1426a52d9a80SChris Mason * 1427a52d9a80SChris Mason * Scheduling is not allowed, so the extent state tree is expected 1428a52d9a80SChris Mason * to have one and only one object corresponding to this IO. 1429a52d9a80SChris Mason */ 14300a2118dfSJens Axboe #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) 14310a2118dfSJens Axboe static void end_bio_extent_writepage(struct bio *bio, int err) 14320a2118dfSJens Axboe #else 1433a52d9a80SChris Mason static int end_bio_extent_writepage(struct bio *bio, 1434a52d9a80SChris Mason unsigned int bytes_done, int err) 14350a2118dfSJens Axboe #endif 1436a52d9a80SChris Mason { 1437a52d9a80SChris Mason const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1438a52d9a80SChris Mason struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; 1439a52d9a80SChris Mason struct extent_map_tree *tree = bio->bi_private; 1440a52d9a80SChris Mason u64 start; 1441a52d9a80SChris Mason u64 end; 1442a52d9a80SChris Mason int whole_page; 1443a52d9a80SChris Mason 14440a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1445a52d9a80SChris Mason if (bio->bi_size) 1446a52d9a80SChris Mason return 1; 14470a2118dfSJens Axboe #endif 1448a52d9a80SChris Mason 1449a52d9a80SChris Mason do { 1450a52d9a80SChris Mason struct page *page = bvec->bv_page; 145135ebb934SChris Mason start = ((u64)page->index << PAGE_CACHE_SHIFT) + 145235ebb934SChris Mason bvec->bv_offset; 1453a52d9a80SChris Mason end = start + bvec->bv_len - 1; 1454a52d9a80SChris Mason 1455a52d9a80SChris Mason if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) 1456a52d9a80SChris Mason whole_page = 1; 1457a52d9a80SChris Mason else 1458a52d9a80SChris Mason whole_page = 0; 1459a52d9a80SChris Mason 1460a52d9a80SChris Mason if (--bvec >= bio->bi_io_vec) 1461a52d9a80SChris Mason prefetchw(&bvec->bv_page->flags); 1462a52d9a80SChris Mason 1463a52d9a80SChris Mason if (!uptodate) { 1464a52d9a80SChris Mason clear_extent_uptodate(tree, start, end, GFP_ATOMIC); 1465a52d9a80SChris Mason ClearPageUptodate(page); 1466a52d9a80SChris Mason SetPageError(page); 1467a52d9a80SChris Mason } 1468a52d9a80SChris Mason clear_extent_writeback(tree, start, end, GFP_ATOMIC); 1469a52d9a80SChris Mason 1470a52d9a80SChris Mason if (whole_page) 1471a52d9a80SChris Mason end_page_writeback(page); 1472a52d9a80SChris Mason else 1473a52d9a80SChris Mason check_page_writeback(tree, page); 14740e2752a7SChristoph Hellwig if (tree->ops && tree->ops->writepage_end_io_hook) 14750e2752a7SChristoph Hellwig tree->ops->writepage_end_io_hook(page, start, end); 1476a52d9a80SChris Mason } while (bvec >= bio->bi_io_vec); 1477a52d9a80SChris Mason 1478a52d9a80SChris Mason bio_put(bio); 14790a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1480a52d9a80SChris Mason return 0; 14810a2118dfSJens Axboe #endif 1482a52d9a80SChris Mason } 1483a52d9a80SChris Mason 1484a52d9a80SChris Mason /* 1485a52d9a80SChris Mason * after a readpage IO is done, we need to: 1486a52d9a80SChris Mason * clear the uptodate bits on error 1487a52d9a80SChris Mason * set the uptodate bits if things worked 1488a52d9a80SChris Mason * set the page up to date if all extents in the tree are uptodate 1489a52d9a80SChris Mason * clear the lock bit in the extent tree 1490a52d9a80SChris Mason * unlock the page if there are no other extents locked for it 1491a52d9a80SChris Mason * 1492a52d9a80SChris Mason * Scheduling is not allowed, so the extent state tree is expected 1493a52d9a80SChris Mason * to have one and only one object corresponding to this IO. 1494a52d9a80SChris Mason */ 14950a2118dfSJens Axboe #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) 14960a2118dfSJens Axboe static void end_bio_extent_readpage(struct bio *bio, int err) 14970a2118dfSJens Axboe #else 1498a52d9a80SChris Mason static int end_bio_extent_readpage(struct bio *bio, 1499a52d9a80SChris Mason unsigned int bytes_done, int err) 15000a2118dfSJens Axboe #endif 1501a52d9a80SChris Mason { 150207157aacSChris Mason int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1503a52d9a80SChris Mason struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; 1504a52d9a80SChris Mason struct extent_map_tree *tree = bio->bi_private; 1505a52d9a80SChris Mason u64 start; 1506a52d9a80SChris Mason u64 end; 1507a52d9a80SChris Mason int whole_page; 150807157aacSChris Mason int ret; 1509a52d9a80SChris Mason 15100a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1511a52d9a80SChris Mason if (bio->bi_size) 1512a52d9a80SChris Mason return 1; 15130a2118dfSJens Axboe #endif 1514a52d9a80SChris Mason 1515a52d9a80SChris Mason do { 1516a52d9a80SChris Mason struct page *page = bvec->bv_page; 151735ebb934SChris Mason start = ((u64)page->index << PAGE_CACHE_SHIFT) + 151835ebb934SChris Mason bvec->bv_offset; 1519a52d9a80SChris Mason end = start + bvec->bv_len - 1; 1520a52d9a80SChris Mason 1521a52d9a80SChris Mason if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) 1522a52d9a80SChris Mason whole_page = 1; 1523a52d9a80SChris Mason else 1524a52d9a80SChris Mason whole_page = 0; 1525a52d9a80SChris Mason 1526a52d9a80SChris Mason if (--bvec >= bio->bi_io_vec) 1527a52d9a80SChris Mason prefetchw(&bvec->bv_page->flags); 1528a52d9a80SChris Mason 152907157aacSChris Mason if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { 153007157aacSChris Mason ret = tree->ops->readpage_end_io_hook(page, start, end); 153107157aacSChris Mason if (ret) 153207157aacSChris Mason uptodate = 0; 153307157aacSChris Mason } 1534a52d9a80SChris Mason if (uptodate) { 1535a52d9a80SChris Mason set_extent_uptodate(tree, start, end, GFP_ATOMIC); 1536a52d9a80SChris Mason if (whole_page) 1537a52d9a80SChris Mason SetPageUptodate(page); 1538a52d9a80SChris Mason else 1539a52d9a80SChris Mason check_page_uptodate(tree, page); 1540a52d9a80SChris Mason } else { 1541a52d9a80SChris Mason ClearPageUptodate(page); 1542a52d9a80SChris Mason SetPageError(page); 1543a52d9a80SChris Mason } 1544a52d9a80SChris Mason 1545a52d9a80SChris Mason unlock_extent(tree, start, end, GFP_ATOMIC); 1546a52d9a80SChris Mason 1547a52d9a80SChris Mason if (whole_page) 1548a52d9a80SChris Mason unlock_page(page); 1549a52d9a80SChris Mason else 1550a52d9a80SChris Mason check_page_locked(tree, page); 1551a52d9a80SChris Mason } while (bvec >= bio->bi_io_vec); 1552a52d9a80SChris Mason 1553a52d9a80SChris Mason bio_put(bio); 15540a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1555a52d9a80SChris Mason return 0; 15560a2118dfSJens Axboe #endif 1557a52d9a80SChris Mason } 1558a52d9a80SChris Mason 1559a52d9a80SChris Mason /* 1560a52d9a80SChris Mason * IO done from prepare_write is pretty simple, we just unlock 1561a52d9a80SChris Mason * the structs in the extent tree when done, and set the uptodate bits 1562a52d9a80SChris Mason * as appropriate. 1563a52d9a80SChris Mason */ 15640a2118dfSJens Axboe #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) 15650a2118dfSJens Axboe static void end_bio_extent_preparewrite(struct bio *bio, int err) 15660a2118dfSJens Axboe #else 1567a52d9a80SChris Mason static int end_bio_extent_preparewrite(struct bio *bio, 1568a52d9a80SChris Mason unsigned int bytes_done, int err) 15690a2118dfSJens Axboe #endif 1570a52d9a80SChris Mason { 1571a52d9a80SChris Mason const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1572a52d9a80SChris Mason struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; 1573a52d9a80SChris Mason struct extent_map_tree *tree = bio->bi_private; 1574a52d9a80SChris Mason u64 start; 1575a52d9a80SChris Mason u64 end; 1576a52d9a80SChris Mason 15770a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1578a52d9a80SChris Mason if (bio->bi_size) 1579a52d9a80SChris Mason return 1; 15800a2118dfSJens Axboe #endif 1581a52d9a80SChris Mason 1582a52d9a80SChris Mason do { 1583a52d9a80SChris Mason struct page *page = bvec->bv_page; 158435ebb934SChris Mason start = ((u64)page->index << PAGE_CACHE_SHIFT) + 158535ebb934SChris Mason bvec->bv_offset; 1586a52d9a80SChris Mason end = start + bvec->bv_len - 1; 1587a52d9a80SChris Mason 1588a52d9a80SChris Mason if (--bvec >= bio->bi_io_vec) 1589a52d9a80SChris Mason prefetchw(&bvec->bv_page->flags); 1590a52d9a80SChris Mason 1591a52d9a80SChris Mason if (uptodate) { 1592a52d9a80SChris Mason set_extent_uptodate(tree, start, end, GFP_ATOMIC); 1593a52d9a80SChris Mason } else { 1594a52d9a80SChris Mason ClearPageUptodate(page); 1595a52d9a80SChris Mason SetPageError(page); 1596a52d9a80SChris Mason } 1597a52d9a80SChris Mason 1598a52d9a80SChris Mason unlock_extent(tree, start, end, GFP_ATOMIC); 1599a52d9a80SChris Mason 1600a52d9a80SChris Mason } while (bvec >= bio->bi_io_vec); 1601a52d9a80SChris Mason 1602a52d9a80SChris Mason bio_put(bio); 16030a2118dfSJens Axboe #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1604a52d9a80SChris Mason return 0; 16050a2118dfSJens Axboe #endif 1606a52d9a80SChris Mason } 1607a52d9a80SChris Mason 1608b293f02eSChris Mason static struct bio * 1609b293f02eSChris Mason extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, 1610b293f02eSChris Mason gfp_t gfp_flags) 1611b293f02eSChris Mason { 1612b293f02eSChris Mason struct bio *bio; 1613b293f02eSChris Mason 1614b293f02eSChris Mason bio = bio_alloc(gfp_flags, nr_vecs); 1615b293f02eSChris Mason 1616b293f02eSChris Mason if (bio == NULL && (current->flags & PF_MEMALLOC)) { 1617b293f02eSChris Mason while (!bio && (nr_vecs /= 2)) 1618b293f02eSChris Mason bio = bio_alloc(gfp_flags, nr_vecs); 1619b293f02eSChris Mason } 1620b293f02eSChris Mason 1621b293f02eSChris Mason if (bio) { 1622b293f02eSChris Mason bio->bi_bdev = bdev; 1623b293f02eSChris Mason bio->bi_sector = first_sector; 1624b293f02eSChris Mason } 1625b293f02eSChris Mason return bio; 1626b293f02eSChris Mason } 1627b293f02eSChris Mason 1628b293f02eSChris Mason static int submit_one_bio(int rw, struct bio *bio) 1629b293f02eSChris Mason { 16306da6abaeSChris Mason u64 maxsector; 1631b293f02eSChris Mason int ret = 0; 16326da6abaeSChris Mason 1633b293f02eSChris Mason bio_get(bio); 16346da6abaeSChris Mason 16356da6abaeSChris Mason maxsector = bio->bi_bdev->bd_inode->i_size >> 9; 16366da6abaeSChris Mason if (maxsector < bio->bi_sector) { 16376da6abaeSChris Mason printk("sector too large max %Lu got %llu\n", maxsector, 16386da6abaeSChris Mason (unsigned long long)bio->bi_sector); 16396da6abaeSChris Mason WARN_ON(1); 16406da6abaeSChris Mason } 16416da6abaeSChris Mason 1642b293f02eSChris Mason submit_bio(rw, bio); 1643b293f02eSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) 1644b293f02eSChris Mason ret = -EOPNOTSUPP; 1645b293f02eSChris Mason bio_put(bio); 1646b293f02eSChris Mason return ret; 1647b293f02eSChris Mason } 1648b293f02eSChris Mason 1649a52d9a80SChris Mason static int submit_extent_page(int rw, struct extent_map_tree *tree, 1650a52d9a80SChris Mason struct page *page, sector_t sector, 1651a52d9a80SChris Mason size_t size, unsigned long offset, 1652a52d9a80SChris Mason struct block_device *bdev, 1653b293f02eSChris Mason struct bio **bio_ret, 16543ab2fb5aSChris Mason unsigned long max_pages, 1655a52d9a80SChris Mason bio_end_io_t end_io_func) 1656a52d9a80SChris Mason { 1657a52d9a80SChris Mason int ret = 0; 1658b293f02eSChris Mason struct bio *bio; 1659b293f02eSChris Mason int nr; 1660a52d9a80SChris Mason 1661b293f02eSChris Mason if (bio_ret && *bio_ret) { 1662b293f02eSChris Mason bio = *bio_ret; 1663b293f02eSChris Mason if (bio->bi_sector + (bio->bi_size >> 9) != sector || 1664b293f02eSChris Mason bio_add_page(bio, page, size, offset) < size) { 1665b293f02eSChris Mason ret = submit_one_bio(rw, bio); 1666b293f02eSChris Mason bio = NULL; 1667b293f02eSChris Mason } else { 1668b293f02eSChris Mason return 0; 1669b293f02eSChris Mason } 1670b293f02eSChris Mason } 16713ab2fb5aSChris Mason nr = min_t(int, max_pages, bio_get_nr_vecs(bdev)); 1672b293f02eSChris Mason bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH); 1673b293f02eSChris Mason if (!bio) { 1674b293f02eSChris Mason printk("failed to allocate bio nr %d\n", nr); 1675b293f02eSChris Mason } 1676b293f02eSChris Mason bio_add_page(bio, page, size, offset); 1677a52d9a80SChris Mason bio->bi_end_io = end_io_func; 1678a52d9a80SChris Mason bio->bi_private = tree; 1679b293f02eSChris Mason if (bio_ret) { 1680b293f02eSChris Mason *bio_ret = bio; 1681b293f02eSChris Mason } else { 1682b293f02eSChris Mason ret = submit_one_bio(rw, bio); 1683b293f02eSChris Mason } 1684a52d9a80SChris Mason 1685a52d9a80SChris Mason return ret; 1686a52d9a80SChris Mason } 1687a52d9a80SChris Mason 1688b3cfa35aSChristoph Hellwig void set_page_extent_mapped(struct page *page) 1689b3cfa35aSChristoph Hellwig { 1690b3cfa35aSChristoph Hellwig if (!PagePrivate(page)) { 1691b3cfa35aSChristoph Hellwig SetPagePrivate(page); 1692b3cfa35aSChristoph Hellwig WARN_ON(!page->mapping->a_ops->invalidatepage); 169319c00ddcSChris Mason set_page_private(page, EXTENT_PAGE_PRIVATE); 1694b3cfa35aSChristoph Hellwig page_cache_get(page); 1695b3cfa35aSChristoph Hellwig } 1696b3cfa35aSChristoph Hellwig } 1697b3cfa35aSChristoph Hellwig 169855c69072SChris Mason void set_page_extent_head(struct page *page, unsigned long len) 169955c69072SChris Mason { 170055c69072SChris Mason set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2); 170155c69072SChris Mason } 170255c69072SChris Mason 1703a52d9a80SChris Mason /* 1704a52d9a80SChris Mason * basic readpage implementation. Locked extent state structs are inserted 1705a52d9a80SChris Mason * into the tree that are removed when the IO is done (by the end_io 1706a52d9a80SChris Mason * handlers) 1707a52d9a80SChris Mason */ 17083ab2fb5aSChris Mason static int __extent_read_full_page(struct extent_map_tree *tree, 17093ab2fb5aSChris Mason struct page *page, 17103ab2fb5aSChris Mason get_extent_t *get_extent, 17113ab2fb5aSChris Mason struct bio **bio) 1712a52d9a80SChris Mason { 1713a52d9a80SChris Mason struct inode *inode = page->mapping->host; 171435ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 1715a52d9a80SChris Mason u64 page_end = start + PAGE_CACHE_SIZE - 1; 1716a52d9a80SChris Mason u64 end; 1717a52d9a80SChris Mason u64 cur = start; 1718a52d9a80SChris Mason u64 extent_offset; 1719a52d9a80SChris Mason u64 last_byte = i_size_read(inode); 1720a52d9a80SChris Mason u64 block_start; 1721a52d9a80SChris Mason u64 cur_end; 1722a52d9a80SChris Mason sector_t sector; 1723a52d9a80SChris Mason struct extent_map *em; 1724a52d9a80SChris Mason struct block_device *bdev; 1725a52d9a80SChris Mason int ret; 1726a52d9a80SChris Mason int nr = 0; 1727a52d9a80SChris Mason size_t page_offset = 0; 1728a52d9a80SChris Mason size_t iosize; 1729a52d9a80SChris Mason size_t blocksize = inode->i_sb->s_blocksize; 1730a52d9a80SChris Mason 1731b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 1732a52d9a80SChris Mason 1733a52d9a80SChris Mason end = page_end; 1734a52d9a80SChris Mason lock_extent(tree, start, end, GFP_NOFS); 1735a52d9a80SChris Mason 1736a52d9a80SChris Mason while (cur <= end) { 1737a52d9a80SChris Mason if (cur >= last_byte) { 17386da6abaeSChris Mason char *userpage; 1739a52d9a80SChris Mason iosize = PAGE_CACHE_SIZE - page_offset; 17406da6abaeSChris Mason userpage = kmap_atomic(page, KM_USER0); 17416da6abaeSChris Mason memset(userpage + page_offset, 0, iosize); 17426da6abaeSChris Mason flush_dcache_page(page); 17436da6abaeSChris Mason kunmap_atomic(userpage, KM_USER0); 1744a52d9a80SChris Mason set_extent_uptodate(tree, cur, cur + iosize - 1, 1745a52d9a80SChris Mason GFP_NOFS); 1746a52d9a80SChris Mason unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); 1747a52d9a80SChris Mason break; 1748a52d9a80SChris Mason } 1749a52d9a80SChris Mason em = get_extent(inode, page, page_offset, cur, end, 0); 1750a52d9a80SChris Mason if (IS_ERR(em) || !em) { 1751a52d9a80SChris Mason SetPageError(page); 1752a52d9a80SChris Mason unlock_extent(tree, cur, end, GFP_NOFS); 1753a52d9a80SChris Mason break; 1754a52d9a80SChris Mason } 1755a52d9a80SChris Mason 1756a52d9a80SChris Mason extent_offset = cur - em->start; 1757a52d9a80SChris Mason BUG_ON(em->end < cur); 1758a52d9a80SChris Mason BUG_ON(end < cur); 1759a52d9a80SChris Mason 1760a52d9a80SChris Mason iosize = min(em->end - cur, end - cur) + 1; 1761a52d9a80SChris Mason cur_end = min(em->end, end); 1762a52d9a80SChris Mason iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); 1763a52d9a80SChris Mason sector = (em->block_start + extent_offset) >> 9; 1764a52d9a80SChris Mason bdev = em->bdev; 1765a52d9a80SChris Mason block_start = em->block_start; 1766a52d9a80SChris Mason free_extent_map(em); 1767a52d9a80SChris Mason em = NULL; 1768a52d9a80SChris Mason 1769a52d9a80SChris Mason /* we've found a hole, just zero and go on */ 17705f39d397SChris Mason if (block_start == EXTENT_MAP_HOLE) { 17716da6abaeSChris Mason char *userpage; 17726da6abaeSChris Mason userpage = kmap_atomic(page, KM_USER0); 17736da6abaeSChris Mason memset(userpage + page_offset, 0, iosize); 17746da6abaeSChris Mason flush_dcache_page(page); 17756da6abaeSChris Mason kunmap_atomic(userpage, KM_USER0); 17766da6abaeSChris Mason 1777a52d9a80SChris Mason set_extent_uptodate(tree, cur, cur + iosize - 1, 1778a52d9a80SChris Mason GFP_NOFS); 1779a52d9a80SChris Mason unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); 1780a52d9a80SChris Mason cur = cur + iosize; 1781a52d9a80SChris Mason page_offset += iosize; 1782a52d9a80SChris Mason continue; 1783a52d9a80SChris Mason } 1784a52d9a80SChris Mason /* the get_extent function already copied into the page */ 1785a52d9a80SChris Mason if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) { 1786a52d9a80SChris Mason unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); 1787a52d9a80SChris Mason cur = cur + iosize; 1788a52d9a80SChris Mason page_offset += iosize; 1789a52d9a80SChris Mason continue; 1790a52d9a80SChris Mason } 1791a52d9a80SChris Mason 179207157aacSChris Mason ret = 0; 179307157aacSChris Mason if (tree->ops && tree->ops->readpage_io_hook) { 179407157aacSChris Mason ret = tree->ops->readpage_io_hook(page, cur, 179507157aacSChris Mason cur + iosize - 1); 179607157aacSChris Mason } 179707157aacSChris Mason if (!ret) { 17983ab2fb5aSChris Mason unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1; 17993ab2fb5aSChris Mason nr -= page->index; 1800a52d9a80SChris Mason ret = submit_extent_page(READ, tree, page, 180107157aacSChris Mason sector, iosize, page_offset, 18023ab2fb5aSChris Mason bdev, bio, nr, 1803b293f02eSChris Mason end_bio_extent_readpage); 180407157aacSChris Mason } 1805a52d9a80SChris Mason if (ret) 1806a52d9a80SChris Mason SetPageError(page); 1807a52d9a80SChris Mason cur = cur + iosize; 1808a52d9a80SChris Mason page_offset += iosize; 1809a52d9a80SChris Mason nr++; 1810a52d9a80SChris Mason } 1811a52d9a80SChris Mason if (!nr) { 1812a52d9a80SChris Mason if (!PageError(page)) 1813a52d9a80SChris Mason SetPageUptodate(page); 1814a52d9a80SChris Mason unlock_page(page); 1815a52d9a80SChris Mason } 1816a52d9a80SChris Mason return 0; 1817a52d9a80SChris Mason } 18183ab2fb5aSChris Mason 18193ab2fb5aSChris Mason int extent_read_full_page(struct extent_map_tree *tree, struct page *page, 18203ab2fb5aSChris Mason get_extent_t *get_extent) 18213ab2fb5aSChris Mason { 18223ab2fb5aSChris Mason struct bio *bio = NULL; 18233ab2fb5aSChris Mason int ret; 18243ab2fb5aSChris Mason 18253ab2fb5aSChris Mason ret = __extent_read_full_page(tree, page, get_extent, &bio); 18263ab2fb5aSChris Mason if (bio) 18273ab2fb5aSChris Mason submit_one_bio(READ, bio); 18283ab2fb5aSChris Mason return ret; 18293ab2fb5aSChris Mason } 1830a52d9a80SChris Mason EXPORT_SYMBOL(extent_read_full_page); 1831a52d9a80SChris Mason 1832a52d9a80SChris Mason /* 1833a52d9a80SChris Mason * the writepage semantics are similar to regular writepage. extent 1834a52d9a80SChris Mason * records are inserted to lock ranges in the tree, and as dirty areas 1835a52d9a80SChris Mason * are found, they are marked writeback. Then the lock bits are removed 1836a52d9a80SChris Mason * and the end_io handler clears the writeback ranges 1837a52d9a80SChris Mason */ 1838b293f02eSChris Mason static int __extent_writepage(struct page *page, struct writeback_control *wbc, 1839b293f02eSChris Mason void *data) 1840a52d9a80SChris Mason { 1841a52d9a80SChris Mason struct inode *inode = page->mapping->host; 1842b293f02eSChris Mason struct extent_page_data *epd = data; 1843b293f02eSChris Mason struct extent_map_tree *tree = epd->tree; 184435ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 18453e9fd94fSChris Mason u64 delalloc_start; 1846a52d9a80SChris Mason u64 page_end = start + PAGE_CACHE_SIZE - 1; 1847a52d9a80SChris Mason u64 end; 1848a52d9a80SChris Mason u64 cur = start; 1849a52d9a80SChris Mason u64 extent_offset; 1850a52d9a80SChris Mason u64 last_byte = i_size_read(inode); 1851a52d9a80SChris Mason u64 block_start; 1852179e29e4SChris Mason u64 iosize; 1853a52d9a80SChris Mason sector_t sector; 1854a52d9a80SChris Mason struct extent_map *em; 1855a52d9a80SChris Mason struct block_device *bdev; 1856a52d9a80SChris Mason int ret; 1857a52d9a80SChris Mason int nr = 0; 1858a52d9a80SChris Mason size_t page_offset = 0; 1859a52d9a80SChris Mason size_t blocksize; 1860a52d9a80SChris Mason loff_t i_size = i_size_read(inode); 1861a52d9a80SChris Mason unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; 1862b888db2bSChris Mason u64 nr_delalloc; 1863b888db2bSChris Mason u64 delalloc_end; 1864a52d9a80SChris Mason 1865b888db2bSChris Mason WARN_ON(!PageLocked(page)); 1866a52d9a80SChris Mason if (page->index > end_index) { 1867a52d9a80SChris Mason clear_extent_dirty(tree, start, page_end, GFP_NOFS); 1868a52d9a80SChris Mason unlock_page(page); 1869a52d9a80SChris Mason return 0; 1870a52d9a80SChris Mason } 1871a52d9a80SChris Mason 1872a52d9a80SChris Mason if (page->index == end_index) { 18736da6abaeSChris Mason char *userpage; 18746da6abaeSChris Mason 1875a52d9a80SChris Mason size_t offset = i_size & (PAGE_CACHE_SIZE - 1); 18766da6abaeSChris Mason 18776da6abaeSChris Mason userpage = kmap_atomic(page, KM_USER0); 18786da6abaeSChris Mason memset(userpage + offset, 0, PAGE_CACHE_SIZE - offset); 18796da6abaeSChris Mason flush_dcache_page(page); 18806da6abaeSChris Mason kunmap_atomic(userpage, KM_USER0); 1881a52d9a80SChris Mason } 1882a52d9a80SChris Mason 1883b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 1884a52d9a80SChris Mason 18853e9fd94fSChris Mason delalloc_start = start; 18863e9fd94fSChris Mason delalloc_end = 0; 18873e9fd94fSChris Mason while(delalloc_end < page_end) { 18883e9fd94fSChris Mason nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start, 1889b888db2bSChris Mason &delalloc_end, 1890b888db2bSChris Mason 128 * 1024 * 1024); 1891190662b2SChris Mason if (nr_delalloc == 0) { 1892190662b2SChris Mason delalloc_start = delalloc_end + 1; 1893190662b2SChris Mason continue; 1894190662b2SChris Mason } 18953e9fd94fSChris Mason tree->ops->fill_delalloc(inode, delalloc_start, 18963e9fd94fSChris Mason delalloc_end); 18973e9fd94fSChris Mason clear_extent_bit(tree, delalloc_start, 18983e9fd94fSChris Mason delalloc_end, 1899b888db2bSChris Mason EXTENT_LOCKED | EXTENT_DELALLOC, 1900b888db2bSChris Mason 1, 0, GFP_NOFS); 19013e9fd94fSChris Mason delalloc_start = delalloc_end + 1; 1902b888db2bSChris Mason } 19033e9fd94fSChris Mason lock_extent(tree, start, page_end, GFP_NOFS); 1904b888db2bSChris Mason 1905b888db2bSChris Mason end = page_end; 1906b888db2bSChris Mason if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) { 1907b888db2bSChris Mason printk("found delalloc bits after lock_extent\n"); 1908b888db2bSChris Mason } 1909a52d9a80SChris Mason 1910a52d9a80SChris Mason if (last_byte <= start) { 1911a52d9a80SChris Mason clear_extent_dirty(tree, start, page_end, GFP_NOFS); 1912a52d9a80SChris Mason goto done; 1913a52d9a80SChris Mason } 1914a52d9a80SChris Mason 1915a52d9a80SChris Mason set_extent_uptodate(tree, start, page_end, GFP_NOFS); 1916a52d9a80SChris Mason blocksize = inode->i_sb->s_blocksize; 1917a52d9a80SChris Mason 1918a52d9a80SChris Mason while (cur <= end) { 1919a52d9a80SChris Mason if (cur >= last_byte) { 1920a52d9a80SChris Mason clear_extent_dirty(tree, cur, page_end, GFP_NOFS); 1921a52d9a80SChris Mason break; 1922a52d9a80SChris Mason } 1923b293f02eSChris Mason em = epd->get_extent(inode, page, page_offset, cur, end, 1); 1924a52d9a80SChris Mason if (IS_ERR(em) || !em) { 1925a52d9a80SChris Mason SetPageError(page); 1926a52d9a80SChris Mason break; 1927a52d9a80SChris Mason } 1928a52d9a80SChris Mason 1929a52d9a80SChris Mason extent_offset = cur - em->start; 1930a52d9a80SChris Mason BUG_ON(em->end < cur); 1931a52d9a80SChris Mason BUG_ON(end < cur); 1932a52d9a80SChris Mason iosize = min(em->end - cur, end - cur) + 1; 1933a52d9a80SChris Mason iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); 1934a52d9a80SChris Mason sector = (em->block_start + extent_offset) >> 9; 1935a52d9a80SChris Mason bdev = em->bdev; 1936a52d9a80SChris Mason block_start = em->block_start; 1937a52d9a80SChris Mason free_extent_map(em); 1938a52d9a80SChris Mason em = NULL; 1939a52d9a80SChris Mason 19405f39d397SChris Mason if (block_start == EXTENT_MAP_HOLE || 19415f39d397SChris Mason block_start == EXTENT_MAP_INLINE) { 1942a52d9a80SChris Mason clear_extent_dirty(tree, cur, 1943a52d9a80SChris Mason cur + iosize - 1, GFP_NOFS); 1944a52d9a80SChris Mason cur = cur + iosize; 1945a52d9a80SChris Mason page_offset += iosize; 1946a52d9a80SChris Mason continue; 1947a52d9a80SChris Mason } 1948a52d9a80SChris Mason 1949a52d9a80SChris Mason /* leave this out until we have a page_mkwrite call */ 1950a52d9a80SChris Mason if (0 && !test_range_bit(tree, cur, cur + iosize - 1, 1951a52d9a80SChris Mason EXTENT_DIRTY, 0)) { 1952a52d9a80SChris Mason cur = cur + iosize; 1953a52d9a80SChris Mason page_offset += iosize; 1954a52d9a80SChris Mason continue; 1955a52d9a80SChris Mason } 1956a52d9a80SChris Mason clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS); 1957b06355f0SChristoph Hellwig if (tree->ops && tree->ops->writepage_io_hook) { 1958b06355f0SChristoph Hellwig ret = tree->ops->writepage_io_hook(page, cur, 1959b06355f0SChristoph Hellwig cur + iosize - 1); 1960b06355f0SChristoph Hellwig } else { 1961b06355f0SChristoph Hellwig ret = 0; 1962b06355f0SChristoph Hellwig } 196307157aacSChris Mason if (ret) 196407157aacSChris Mason SetPageError(page); 196507157aacSChris Mason else { 19667073c8e8SChris Mason unsigned long max_nr = end_index + 1; 1967a52d9a80SChris Mason set_range_writeback(tree, cur, cur + iosize - 1); 19687073c8e8SChris Mason if (!PageWriteback(page)) { 19697073c8e8SChris Mason printk("warning page %lu not writeback, " 19707073c8e8SChris Mason "cur %llu end %llu\n", page->index, 19717073c8e8SChris Mason (unsigned long long)cur, 19727073c8e8SChris Mason (unsigned long long)end); 19737073c8e8SChris Mason } 1974b293f02eSChris Mason 197507157aacSChris Mason ret = submit_extent_page(WRITE, tree, page, sector, 197607157aacSChris Mason iosize, page_offset, bdev, 19777073c8e8SChris Mason &epd->bio, max_nr, 1978a52d9a80SChris Mason end_bio_extent_writepage); 1979a52d9a80SChris Mason if (ret) 1980a52d9a80SChris Mason SetPageError(page); 198107157aacSChris Mason } 1982a52d9a80SChris Mason cur = cur + iosize; 1983a52d9a80SChris Mason page_offset += iosize; 1984a52d9a80SChris Mason nr++; 1985a52d9a80SChris Mason } 1986a52d9a80SChris Mason done: 19877073c8e8SChris Mason if (nr == 0) { 19887073c8e8SChris Mason /* make sure the mapping tag for page dirty gets cleared */ 19897073c8e8SChris Mason set_page_writeback(page); 19907073c8e8SChris Mason end_page_writeback(page); 19917073c8e8SChris Mason } 1992a52d9a80SChris Mason unlock_extent(tree, start, page_end, GFP_NOFS); 1993a52d9a80SChris Mason unlock_page(page); 1994a52d9a80SChris Mason return 0; 1995a52d9a80SChris Mason } 1996b293f02eSChris Mason 19976da6abaeSChris Mason #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 19986da6abaeSChris Mason 19996da6abaeSChris Mason /* Taken directly from 2.6.23 for 2.6.18 back port */ 20006da6abaeSChris Mason typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc, 20016da6abaeSChris Mason void *data); 20026da6abaeSChris Mason 20036da6abaeSChris Mason /** 20046da6abaeSChris Mason * write_cache_pages - walk the list of dirty pages of the given address space 20056da6abaeSChris Mason * and write all of them. 20066da6abaeSChris Mason * @mapping: address space structure to write 20076da6abaeSChris Mason * @wbc: subtract the number of written pages from *@wbc->nr_to_write 20086da6abaeSChris Mason * @writepage: function called for each page 20096da6abaeSChris Mason * @data: data passed to writepage function 20106da6abaeSChris Mason * 20116da6abaeSChris Mason * If a page is already under I/O, write_cache_pages() skips it, even 20126da6abaeSChris Mason * if it's dirty. This is desirable behaviour for memory-cleaning writeback, 20136da6abaeSChris Mason * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() 20146da6abaeSChris Mason * and msync() need to guarantee that all the data which was dirty at the time 20156da6abaeSChris Mason * the call was made get new I/O started against them. If wbc->sync_mode is 20166da6abaeSChris Mason * WB_SYNC_ALL then we were called for data integrity and we must wait for 20176da6abaeSChris Mason * existing IO to complete. 20186da6abaeSChris Mason */ 20196da6abaeSChris Mason static int write_cache_pages(struct address_space *mapping, 20206da6abaeSChris Mason struct writeback_control *wbc, writepage_t writepage, 20216da6abaeSChris Mason void *data) 20226da6abaeSChris Mason { 20236da6abaeSChris Mason struct backing_dev_info *bdi = mapping->backing_dev_info; 20246da6abaeSChris Mason int ret = 0; 20256da6abaeSChris Mason int done = 0; 20266da6abaeSChris Mason struct pagevec pvec; 20276da6abaeSChris Mason int nr_pages; 20286da6abaeSChris Mason pgoff_t index; 20296da6abaeSChris Mason pgoff_t end; /* Inclusive */ 20306da6abaeSChris Mason int scanned = 0; 20316da6abaeSChris Mason int range_whole = 0; 20326da6abaeSChris Mason 20336da6abaeSChris Mason if (wbc->nonblocking && bdi_write_congested(bdi)) { 20346da6abaeSChris Mason wbc->encountered_congestion = 1; 20356da6abaeSChris Mason return 0; 20366da6abaeSChris Mason } 20376da6abaeSChris Mason 20386da6abaeSChris Mason pagevec_init(&pvec, 0); 20396da6abaeSChris Mason if (wbc->range_cyclic) { 20406da6abaeSChris Mason index = mapping->writeback_index; /* Start from prev offset */ 20416da6abaeSChris Mason end = -1; 20426da6abaeSChris Mason } else { 20436da6abaeSChris Mason index = wbc->range_start >> PAGE_CACHE_SHIFT; 20446da6abaeSChris Mason end = wbc->range_end >> PAGE_CACHE_SHIFT; 20456da6abaeSChris Mason if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 20466da6abaeSChris Mason range_whole = 1; 20476da6abaeSChris Mason scanned = 1; 20486da6abaeSChris Mason } 20496da6abaeSChris Mason retry: 20506da6abaeSChris Mason while (!done && (index <= end) && 20516da6abaeSChris Mason (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 20526da6abaeSChris Mason PAGECACHE_TAG_DIRTY, 20536da6abaeSChris Mason min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { 20546da6abaeSChris Mason unsigned i; 20556da6abaeSChris Mason 20566da6abaeSChris Mason scanned = 1; 20576da6abaeSChris Mason for (i = 0; i < nr_pages; i++) { 20586da6abaeSChris Mason struct page *page = pvec.pages[i]; 20596da6abaeSChris Mason 20606da6abaeSChris Mason /* 20616da6abaeSChris Mason * At this point we hold neither mapping->tree_lock nor 20626da6abaeSChris Mason * lock on the page itself: the page may be truncated or 20636da6abaeSChris Mason * invalidated (changing page->mapping to NULL), or even 20646da6abaeSChris Mason * swizzled back from swapper_space to tmpfs file 20656da6abaeSChris Mason * mapping 20666da6abaeSChris Mason */ 20676da6abaeSChris Mason lock_page(page); 20686da6abaeSChris Mason 20696da6abaeSChris Mason if (unlikely(page->mapping != mapping)) { 20706da6abaeSChris Mason unlock_page(page); 20716da6abaeSChris Mason continue; 20726da6abaeSChris Mason } 20736da6abaeSChris Mason 20746da6abaeSChris Mason if (!wbc->range_cyclic && page->index > end) { 20756da6abaeSChris Mason done = 1; 20766da6abaeSChris Mason unlock_page(page); 20776da6abaeSChris Mason continue; 20786da6abaeSChris Mason } 20796da6abaeSChris Mason 20806da6abaeSChris Mason if (wbc->sync_mode != WB_SYNC_NONE) 20816da6abaeSChris Mason wait_on_page_writeback(page); 20826da6abaeSChris Mason 20836da6abaeSChris Mason if (PageWriteback(page) || 20846da6abaeSChris Mason !clear_page_dirty_for_io(page)) { 20856da6abaeSChris Mason unlock_page(page); 20866da6abaeSChris Mason continue; 20876da6abaeSChris Mason } 20886da6abaeSChris Mason 20896da6abaeSChris Mason ret = (*writepage)(page, wbc, data); 20906da6abaeSChris Mason 20916da6abaeSChris Mason if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { 20926da6abaeSChris Mason unlock_page(page); 20936da6abaeSChris Mason ret = 0; 20946da6abaeSChris Mason } 20956da6abaeSChris Mason if (ret || (--(wbc->nr_to_write) <= 0)) 20966da6abaeSChris Mason done = 1; 20976da6abaeSChris Mason if (wbc->nonblocking && bdi_write_congested(bdi)) { 20986da6abaeSChris Mason wbc->encountered_congestion = 1; 20996da6abaeSChris Mason done = 1; 21006da6abaeSChris Mason } 21016da6abaeSChris Mason } 21026da6abaeSChris Mason pagevec_release(&pvec); 21036da6abaeSChris Mason cond_resched(); 21046da6abaeSChris Mason } 21056da6abaeSChris Mason if (!scanned && !done) { 21066da6abaeSChris Mason /* 21076da6abaeSChris Mason * We hit the last page and there is more work to be done: wrap 21086da6abaeSChris Mason * back to the start of the file 21096da6abaeSChris Mason */ 21106da6abaeSChris Mason scanned = 1; 21116da6abaeSChris Mason index = 0; 21126da6abaeSChris Mason goto retry; 21136da6abaeSChris Mason } 21146da6abaeSChris Mason if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 21156da6abaeSChris Mason mapping->writeback_index = index; 21166da6abaeSChris Mason return ret; 21176da6abaeSChris Mason } 21186da6abaeSChris Mason #endif 21196da6abaeSChris Mason 2120b293f02eSChris Mason int extent_write_full_page(struct extent_map_tree *tree, struct page *page, 2121b293f02eSChris Mason get_extent_t *get_extent, 2122b293f02eSChris Mason struct writeback_control *wbc) 2123b293f02eSChris Mason { 2124b293f02eSChris Mason int ret; 2125015a739cSChris Mason struct address_space *mapping = page->mapping; 2126b293f02eSChris Mason struct extent_page_data epd = { 2127b293f02eSChris Mason .bio = NULL, 2128b293f02eSChris Mason .tree = tree, 2129b293f02eSChris Mason .get_extent = get_extent, 2130b293f02eSChris Mason }; 2131015a739cSChris Mason struct writeback_control wbc_writepages = { 2132015a739cSChris Mason .bdi = wbc->bdi, 2133015a739cSChris Mason .sync_mode = WB_SYNC_NONE, 2134015a739cSChris Mason .older_than_this = NULL, 2135015a739cSChris Mason .nr_to_write = 64, 2136015a739cSChris Mason .range_start = page_offset(page) + PAGE_CACHE_SIZE, 2137015a739cSChris Mason .range_end = (loff_t)-1, 2138015a739cSChris Mason }; 2139015a739cSChris Mason 2140b293f02eSChris Mason 2141b293f02eSChris Mason ret = __extent_writepage(page, wbc, &epd); 2142015a739cSChris Mason 2143015a739cSChris Mason write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd); 21446da6abaeSChris Mason if (epd.bio) { 2145b293f02eSChris Mason submit_one_bio(WRITE, epd.bio); 21466da6abaeSChris Mason } 2147b293f02eSChris Mason return ret; 2148b293f02eSChris Mason } 2149a52d9a80SChris Mason EXPORT_SYMBOL(extent_write_full_page); 2150a52d9a80SChris Mason 21516da6abaeSChris Mason 2152b293f02eSChris Mason int extent_writepages(struct extent_map_tree *tree, 2153b293f02eSChris Mason struct address_space *mapping, 2154b293f02eSChris Mason get_extent_t *get_extent, 2155b293f02eSChris Mason struct writeback_control *wbc) 2156b293f02eSChris Mason { 21576da6abaeSChris Mason int ret = 0; 2158b293f02eSChris Mason struct extent_page_data epd = { 2159b293f02eSChris Mason .bio = NULL, 2160b293f02eSChris Mason .tree = tree, 2161b293f02eSChris Mason .get_extent = get_extent, 2162b293f02eSChris Mason }; 2163b293f02eSChris Mason 2164b293f02eSChris Mason ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd); 21656da6abaeSChris Mason if (epd.bio) { 2166b293f02eSChris Mason submit_one_bio(WRITE, epd.bio); 21676da6abaeSChris Mason } 2168b293f02eSChris Mason return ret; 2169b293f02eSChris Mason } 2170b293f02eSChris Mason EXPORT_SYMBOL(extent_writepages); 2171b293f02eSChris Mason 21723ab2fb5aSChris Mason int extent_readpages(struct extent_map_tree *tree, 21733ab2fb5aSChris Mason struct address_space *mapping, 21743ab2fb5aSChris Mason struct list_head *pages, unsigned nr_pages, 21753ab2fb5aSChris Mason get_extent_t get_extent) 21763ab2fb5aSChris Mason { 21773ab2fb5aSChris Mason struct bio *bio = NULL; 21783ab2fb5aSChris Mason unsigned page_idx; 21793ab2fb5aSChris Mason struct pagevec pvec; 21803ab2fb5aSChris Mason 21813ab2fb5aSChris Mason pagevec_init(&pvec, 0); 21823ab2fb5aSChris Mason for (page_idx = 0; page_idx < nr_pages; page_idx++) { 21833ab2fb5aSChris Mason struct page *page = list_entry(pages->prev, struct page, lru); 21843ab2fb5aSChris Mason 21853ab2fb5aSChris Mason prefetchw(&page->flags); 21863ab2fb5aSChris Mason list_del(&page->lru); 21873ab2fb5aSChris Mason /* 21883ab2fb5aSChris Mason * what we want to do here is call add_to_page_cache_lru, 21893ab2fb5aSChris Mason * but that isn't exported, so we reproduce it here 21903ab2fb5aSChris Mason */ 21913ab2fb5aSChris Mason if (!add_to_page_cache(page, mapping, 21923ab2fb5aSChris Mason page->index, GFP_KERNEL)) { 21933ab2fb5aSChris Mason 21943ab2fb5aSChris Mason /* open coding of lru_cache_add, also not exported */ 21953ab2fb5aSChris Mason page_cache_get(page); 21963ab2fb5aSChris Mason if (!pagevec_add(&pvec, page)) 21973ab2fb5aSChris Mason __pagevec_lru_add(&pvec); 21983ab2fb5aSChris Mason __extent_read_full_page(tree, page, get_extent, &bio); 21993ab2fb5aSChris Mason } 22003ab2fb5aSChris Mason page_cache_release(page); 22013ab2fb5aSChris Mason } 22023ab2fb5aSChris Mason if (pagevec_count(&pvec)) 22033ab2fb5aSChris Mason __pagevec_lru_add(&pvec); 22043ab2fb5aSChris Mason BUG_ON(!list_empty(pages)); 22053ab2fb5aSChris Mason if (bio) 22063ab2fb5aSChris Mason submit_one_bio(READ, bio); 22073ab2fb5aSChris Mason return 0; 22083ab2fb5aSChris Mason } 22093ab2fb5aSChris Mason EXPORT_SYMBOL(extent_readpages); 22103ab2fb5aSChris Mason 2211a52d9a80SChris Mason /* 2212a52d9a80SChris Mason * basic invalidatepage code, this waits on any locked or writeback 2213a52d9a80SChris Mason * ranges corresponding to the page, and then deletes any extent state 2214a52d9a80SChris Mason * records from the tree 2215a52d9a80SChris Mason */ 2216a52d9a80SChris Mason int extent_invalidatepage(struct extent_map_tree *tree, 2217a52d9a80SChris Mason struct page *page, unsigned long offset) 2218a52d9a80SChris Mason { 221935ebb934SChris Mason u64 start = ((u64)page->index << PAGE_CACHE_SHIFT); 2220a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 2221a52d9a80SChris Mason size_t blocksize = page->mapping->host->i_sb->s_blocksize; 2222a52d9a80SChris Mason 2223a52d9a80SChris Mason start += (offset + blocksize -1) & ~(blocksize - 1); 2224a52d9a80SChris Mason if (start > end) 2225a52d9a80SChris Mason return 0; 2226a52d9a80SChris Mason 2227a52d9a80SChris Mason lock_extent(tree, start, end, GFP_NOFS); 2228a52d9a80SChris Mason wait_on_extent_writeback(tree, start, end); 22292bf5a725SChris Mason clear_extent_bit(tree, start, end, 22302bf5a725SChris Mason EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, 2231a52d9a80SChris Mason 1, 1, GFP_NOFS); 2232a52d9a80SChris Mason return 0; 2233a52d9a80SChris Mason } 2234a52d9a80SChris Mason EXPORT_SYMBOL(extent_invalidatepage); 2235a52d9a80SChris Mason 2236a52d9a80SChris Mason /* 2237a52d9a80SChris Mason * simple commit_write call, set_range_dirty is used to mark both 2238a52d9a80SChris Mason * the pages and the extent records as dirty 2239a52d9a80SChris Mason */ 2240a52d9a80SChris Mason int extent_commit_write(struct extent_map_tree *tree, 2241a52d9a80SChris Mason struct inode *inode, struct page *page, 2242a52d9a80SChris Mason unsigned from, unsigned to) 2243a52d9a80SChris Mason { 2244a52d9a80SChris Mason loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; 2245a52d9a80SChris Mason 2246b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 2247a52d9a80SChris Mason set_page_dirty(page); 2248a52d9a80SChris Mason 2249a52d9a80SChris Mason if (pos > inode->i_size) { 2250a52d9a80SChris Mason i_size_write(inode, pos); 2251a52d9a80SChris Mason mark_inode_dirty(inode); 2252a52d9a80SChris Mason } 2253a52d9a80SChris Mason return 0; 2254a52d9a80SChris Mason } 2255a52d9a80SChris Mason EXPORT_SYMBOL(extent_commit_write); 2256a52d9a80SChris Mason 2257a52d9a80SChris Mason int extent_prepare_write(struct extent_map_tree *tree, 2258a52d9a80SChris Mason struct inode *inode, struct page *page, 2259a52d9a80SChris Mason unsigned from, unsigned to, get_extent_t *get_extent) 2260a52d9a80SChris Mason { 226135ebb934SChris Mason u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; 2262a52d9a80SChris Mason u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 2263a52d9a80SChris Mason u64 block_start; 2264a52d9a80SChris Mason u64 orig_block_start; 2265a52d9a80SChris Mason u64 block_end; 2266a52d9a80SChris Mason u64 cur_end; 2267a52d9a80SChris Mason struct extent_map *em; 2268a52d9a80SChris Mason unsigned blocksize = 1 << inode->i_blkbits; 2269a52d9a80SChris Mason size_t page_offset = 0; 2270a52d9a80SChris Mason size_t block_off_start; 2271a52d9a80SChris Mason size_t block_off_end; 2272a52d9a80SChris Mason int err = 0; 2273a52d9a80SChris Mason int iocount = 0; 2274a52d9a80SChris Mason int ret = 0; 2275a52d9a80SChris Mason int isnew; 2276a52d9a80SChris Mason 2277b3cfa35aSChristoph Hellwig set_page_extent_mapped(page); 2278b3cfa35aSChristoph Hellwig 2279a52d9a80SChris Mason block_start = (page_start + from) & ~((u64)blocksize - 1); 2280a52d9a80SChris Mason block_end = (page_start + to - 1) | (blocksize - 1); 2281a52d9a80SChris Mason orig_block_start = block_start; 2282a52d9a80SChris Mason 2283a52d9a80SChris Mason lock_extent(tree, page_start, page_end, GFP_NOFS); 2284a52d9a80SChris Mason while(block_start <= block_end) { 2285a52d9a80SChris Mason em = get_extent(inode, page, page_offset, block_start, 2286a52d9a80SChris Mason block_end, 1); 2287a52d9a80SChris Mason if (IS_ERR(em) || !em) { 2288a52d9a80SChris Mason goto err; 2289a52d9a80SChris Mason } 2290a52d9a80SChris Mason cur_end = min(block_end, em->end); 2291a52d9a80SChris Mason block_off_start = block_start & (PAGE_CACHE_SIZE - 1); 2292a52d9a80SChris Mason block_off_end = block_off_start + blocksize; 2293a52d9a80SChris Mason isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS); 2294a52d9a80SChris Mason 2295a52d9a80SChris Mason if (!PageUptodate(page) && isnew && 2296a52d9a80SChris Mason (block_off_end > to || block_off_start < from)) { 2297a52d9a80SChris Mason void *kaddr; 2298a52d9a80SChris Mason 2299a52d9a80SChris Mason kaddr = kmap_atomic(page, KM_USER0); 2300a52d9a80SChris Mason if (block_off_end > to) 2301a52d9a80SChris Mason memset(kaddr + to, 0, block_off_end - to); 2302a52d9a80SChris Mason if (block_off_start < from) 2303a52d9a80SChris Mason memset(kaddr + block_off_start, 0, 2304a52d9a80SChris Mason from - block_off_start); 2305a52d9a80SChris Mason flush_dcache_page(page); 2306a52d9a80SChris Mason kunmap_atomic(kaddr, KM_USER0); 2307a52d9a80SChris Mason } 23086da6abaeSChris Mason if ((em->block_start != EXTENT_MAP_HOLE && 23096da6abaeSChris Mason em->block_start != EXTENT_MAP_INLINE) && 23106da6abaeSChris Mason !isnew && !PageUptodate(page) && 2311a52d9a80SChris Mason (block_off_end > to || block_off_start < from) && 2312a52d9a80SChris Mason !test_range_bit(tree, block_start, cur_end, 2313a52d9a80SChris Mason EXTENT_UPTODATE, 1)) { 2314a52d9a80SChris Mason u64 sector; 2315a52d9a80SChris Mason u64 extent_offset = block_start - em->start; 2316a52d9a80SChris Mason size_t iosize; 2317a52d9a80SChris Mason sector = (em->block_start + extent_offset) >> 9; 23185d4fb734SChris Mason iosize = (cur_end - block_start + blocksize) & 2319a52d9a80SChris Mason ~((u64)blocksize - 1); 2320a52d9a80SChris Mason /* 2321a52d9a80SChris Mason * we've already got the extent locked, but we 2322a52d9a80SChris Mason * need to split the state such that our end_bio 2323a52d9a80SChris Mason * handler can clear the lock. 2324a52d9a80SChris Mason */ 2325a52d9a80SChris Mason set_extent_bit(tree, block_start, 2326a52d9a80SChris Mason block_start + iosize - 1, 2327a52d9a80SChris Mason EXTENT_LOCKED, 0, NULL, GFP_NOFS); 2328a52d9a80SChris Mason ret = submit_extent_page(READ, tree, page, 2329a52d9a80SChris Mason sector, iosize, page_offset, em->bdev, 2330b293f02eSChris Mason NULL, 1, 2331a52d9a80SChris Mason end_bio_extent_preparewrite); 2332a52d9a80SChris Mason iocount++; 2333a52d9a80SChris Mason block_start = block_start + iosize; 2334a52d9a80SChris Mason } else { 2335a52d9a80SChris Mason set_extent_uptodate(tree, block_start, cur_end, 2336a52d9a80SChris Mason GFP_NOFS); 2337a52d9a80SChris Mason unlock_extent(tree, block_start, cur_end, GFP_NOFS); 2338a52d9a80SChris Mason block_start = cur_end + 1; 2339a52d9a80SChris Mason } 2340a52d9a80SChris Mason page_offset = block_start & (PAGE_CACHE_SIZE - 1); 2341a52d9a80SChris Mason free_extent_map(em); 2342a52d9a80SChris Mason } 2343a52d9a80SChris Mason if (iocount) { 2344a52d9a80SChris Mason wait_extent_bit(tree, orig_block_start, 2345a52d9a80SChris Mason block_end, EXTENT_LOCKED); 2346a52d9a80SChris Mason } 2347a52d9a80SChris Mason check_page_uptodate(tree, page); 2348a52d9a80SChris Mason err: 2349a52d9a80SChris Mason /* FIXME, zero out newly allocated blocks on error */ 2350a52d9a80SChris Mason return err; 2351a52d9a80SChris Mason } 2352a52d9a80SChris Mason EXPORT_SYMBOL(extent_prepare_write); 2353a52d9a80SChris Mason 2354a52d9a80SChris Mason /* 2355a52d9a80SChris Mason * a helper for releasepage. As long as there are no locked extents 2356a52d9a80SChris Mason * in the range corresponding to the page, both state records and extent 2357a52d9a80SChris Mason * map records are removed 2358a52d9a80SChris Mason */ 2359a52d9a80SChris Mason int try_release_extent_mapping(struct extent_map_tree *tree, struct page *page) 2360a52d9a80SChris Mason { 2361a52d9a80SChris Mason struct extent_map *em; 236235ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 2363a52d9a80SChris Mason u64 end = start + PAGE_CACHE_SIZE - 1; 2364a52d9a80SChris Mason u64 orig_start = start; 2365b888db2bSChris Mason int ret = 1; 2366a52d9a80SChris Mason 2367a52d9a80SChris Mason while (start <= end) { 2368a52d9a80SChris Mason em = lookup_extent_mapping(tree, start, end); 2369a52d9a80SChris Mason if (!em || IS_ERR(em)) 2370a52d9a80SChris Mason break; 2371b888db2bSChris Mason if (!test_range_bit(tree, em->start, em->end, 2372a52d9a80SChris Mason EXTENT_LOCKED, 0)) { 2373a52d9a80SChris Mason remove_extent_mapping(tree, em); 2374a52d9a80SChris Mason /* once for the rb tree */ 2375a52d9a80SChris Mason free_extent_map(em); 2376b888db2bSChris Mason } 2377b888db2bSChris Mason start = em->end + 1; 2378a52d9a80SChris Mason /* once for us */ 2379a52d9a80SChris Mason free_extent_map(em); 2380a52d9a80SChris Mason } 2381b888db2bSChris Mason if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0)) 2382b888db2bSChris Mason ret = 0; 2383b888db2bSChris Mason else 2384a52d9a80SChris Mason clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE, 2385a52d9a80SChris Mason 1, 1, GFP_NOFS); 2386b888db2bSChris Mason return ret; 2387a52d9a80SChris Mason } 2388a52d9a80SChris Mason EXPORT_SYMBOL(try_release_extent_mapping); 2389a52d9a80SChris Mason 2390d396c6f5SChristoph Hellwig sector_t extent_bmap(struct address_space *mapping, sector_t iblock, 2391d396c6f5SChristoph Hellwig get_extent_t *get_extent) 2392d396c6f5SChristoph Hellwig { 2393d396c6f5SChristoph Hellwig struct inode *inode = mapping->host; 2394d396c6f5SChristoph Hellwig u64 start = iblock << inode->i_blkbits; 2395d396c6f5SChristoph Hellwig u64 end = start + (1 << inode->i_blkbits) - 1; 2396c67cda17SYan sector_t sector = 0; 2397d396c6f5SChristoph Hellwig struct extent_map *em; 2398d396c6f5SChristoph Hellwig 2399d396c6f5SChristoph Hellwig em = get_extent(inode, NULL, 0, start, end, 0); 2400d396c6f5SChristoph Hellwig if (!em || IS_ERR(em)) 2401d396c6f5SChristoph Hellwig return 0; 2402d396c6f5SChristoph Hellwig 2403d396c6f5SChristoph Hellwig if (em->block_start == EXTENT_MAP_INLINE || 24045f39d397SChris Mason em->block_start == EXTENT_MAP_HOLE) 2405c67cda17SYan goto out; 2406d396c6f5SChristoph Hellwig 2407c67cda17SYan sector = (em->block_start + start - em->start) >> inode->i_blkbits; 2408c67cda17SYan out: 2409c67cda17SYan free_extent_map(em); 2410c67cda17SYan return sector; 2411d396c6f5SChristoph Hellwig } 24125f39d397SChris Mason 24134dc11904SChris Mason static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb) 24146d36dcd4SChris Mason { 24154dc11904SChris Mason if (list_empty(&eb->lru)) { 24164dc11904SChris Mason extent_buffer_get(eb); 24174dc11904SChris Mason list_add(&eb->lru, &tree->buffer_lru); 24184dc11904SChris Mason tree->lru_size++; 24194dc11904SChris Mason if (tree->lru_size >= BUFFER_LRU_MAX) { 24204dc11904SChris Mason struct extent_buffer *rm; 24214dc11904SChris Mason rm = list_entry(tree->buffer_lru.prev, 24224dc11904SChris Mason struct extent_buffer, lru); 24234dc11904SChris Mason tree->lru_size--; 2424856bf3e5SChris Mason list_del_init(&rm->lru); 24254dc11904SChris Mason free_extent_buffer(rm); 24266d36dcd4SChris Mason } 24274dc11904SChris Mason } else 24284dc11904SChris Mason list_move(&eb->lru, &tree->buffer_lru); 24294dc11904SChris Mason return 0; 24306d36dcd4SChris Mason } 24314dc11904SChris Mason static struct extent_buffer *find_lru(struct extent_map_tree *tree, 24324dc11904SChris Mason u64 start, unsigned long len) 24334dc11904SChris Mason { 24344dc11904SChris Mason struct list_head *lru = &tree->buffer_lru; 24354dc11904SChris Mason struct list_head *cur = lru->next; 24364dc11904SChris Mason struct extent_buffer *eb; 2437f510cfecSChris Mason 24384dc11904SChris Mason if (list_empty(lru)) 24394dc11904SChris Mason return NULL; 24404dc11904SChris Mason 24414dc11904SChris Mason do { 24424dc11904SChris Mason eb = list_entry(cur, struct extent_buffer, lru); 24434dc11904SChris Mason if (eb->start == start && eb->len == len) { 24444dc11904SChris Mason extent_buffer_get(eb); 2445f510cfecSChris Mason return eb; 24466d36dcd4SChris Mason } 24474dc11904SChris Mason cur = cur->next; 24484dc11904SChris Mason } while (cur != lru); 24494dc11904SChris Mason return NULL; 24506d36dcd4SChris Mason } 24516d36dcd4SChris Mason 2452db94535dSChris Mason static inline unsigned long num_extent_pages(u64 start, u64 len) 2453db94535dSChris Mason { 2454db94535dSChris Mason return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - 2455db94535dSChris Mason (start >> PAGE_CACHE_SHIFT); 2456db94535dSChris Mason } 24574dc11904SChris Mason 24584dc11904SChris Mason static inline struct page *extent_buffer_page(struct extent_buffer *eb, 24594dc11904SChris Mason unsigned long i) 24604dc11904SChris Mason { 24614dc11904SChris Mason struct page *p; 24623685f791SChris Mason struct address_space *mapping; 24634dc11904SChris Mason 24644dc11904SChris Mason if (i == 0) 2465810191ffSChris Mason return eb->first_page; 24664dc11904SChris Mason i += eb->start >> PAGE_CACHE_SHIFT; 24673685f791SChris Mason mapping = eb->first_page->mapping; 24683685f791SChris Mason read_lock_irq(&mapping->tree_lock); 24693685f791SChris Mason p = radix_tree_lookup(&mapping->page_tree, i); 24703685f791SChris Mason read_unlock_irq(&mapping->tree_lock); 24714dc11904SChris Mason return p; 24724dc11904SChris Mason } 24734dc11904SChris Mason 24744dc11904SChris Mason static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree, 24754dc11904SChris Mason u64 start, 24764dc11904SChris Mason unsigned long len, 24774dc11904SChris Mason gfp_t mask) 24784dc11904SChris Mason { 24794dc11904SChris Mason struct extent_buffer *eb = NULL; 24804dc11904SChris Mason 24814dc11904SChris Mason spin_lock(&tree->lru_lock); 24824dc11904SChris Mason eb = find_lru(tree, start, len); 24834dc11904SChris Mason spin_unlock(&tree->lru_lock); 24844dc11904SChris Mason if (eb) { 248509be207dSChris Mason return eb; 24864dc11904SChris Mason } 248709be207dSChris Mason 248809be207dSChris Mason eb = kmem_cache_zalloc(extent_buffer_cache, mask); 24894dc11904SChris Mason INIT_LIST_HEAD(&eb->lru); 24904dc11904SChris Mason eb->start = start; 24914dc11904SChris Mason eb->len = len; 24924dc11904SChris Mason atomic_set(&eb->refs, 1); 24934dc11904SChris Mason 24944dc11904SChris Mason return eb; 24954dc11904SChris Mason } 24964dc11904SChris Mason 24974dc11904SChris Mason static void __free_extent_buffer(struct extent_buffer *eb) 24984dc11904SChris Mason { 24994dc11904SChris Mason kmem_cache_free(extent_buffer_cache, eb); 25004dc11904SChris Mason } 25014dc11904SChris Mason 25025f39d397SChris Mason struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree, 25035f39d397SChris Mason u64 start, unsigned long len, 250419c00ddcSChris Mason struct page *page0, 25055f39d397SChris Mason gfp_t mask) 25065f39d397SChris Mason { 2507db94535dSChris Mason unsigned long num_pages = num_extent_pages(start, len); 25085f39d397SChris Mason unsigned long i; 25095f39d397SChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 25105f39d397SChris Mason struct extent_buffer *eb; 25115f39d397SChris Mason struct page *p; 25125f39d397SChris Mason struct address_space *mapping = tree->mapping; 251365555a06SYan int uptodate = 1; 25145f39d397SChris Mason 25154dc11904SChris Mason eb = __alloc_extent_buffer(tree, start, len, mask); 25165f39d397SChris Mason if (!eb || IS_ERR(eb)) 25175f39d397SChris Mason return NULL; 25185f39d397SChris Mason 25194dc11904SChris Mason if (eb->flags & EXTENT_BUFFER_FILLED) 252009be207dSChris Mason goto lru_add; 25215f39d397SChris Mason 252219c00ddcSChris Mason if (page0) { 252319c00ddcSChris Mason eb->first_page = page0; 252419c00ddcSChris Mason i = 1; 252519c00ddcSChris Mason index++; 252619c00ddcSChris Mason page_cache_get(page0); 2527ff79f819SChris Mason mark_page_accessed(page0); 252819c00ddcSChris Mason set_page_extent_mapped(page0); 25290591fb56SChris Mason WARN_ON(!PageUptodate(page0)); 253055c69072SChris Mason set_page_extent_head(page0, len); 253119c00ddcSChris Mason } else { 253219c00ddcSChris Mason i = 0; 253319c00ddcSChris Mason } 253419c00ddcSChris Mason for (; i < num_pages; i++, index++) { 25355f39d397SChris Mason p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM); 25366d36dcd4SChris Mason if (!p) { 2537db94535dSChris Mason WARN_ON(1); 25385f39d397SChris Mason goto fail; 25396d36dcd4SChris Mason } 2540f510cfecSChris Mason set_page_extent_mapped(p); 2541ff79f819SChris Mason mark_page_accessed(p); 254219c00ddcSChris Mason if (i == 0) { 2543810191ffSChris Mason eb->first_page = p; 254455c69072SChris Mason set_page_extent_head(p, len); 254519c00ddcSChris Mason } else { 254619c00ddcSChris Mason set_page_private(p, EXTENT_PAGE_PRIVATE); 254719c00ddcSChris Mason } 25485f39d397SChris Mason if (!PageUptodate(p)) 25495f39d397SChris Mason uptodate = 0; 25505f39d397SChris Mason unlock_page(p); 25515f39d397SChris Mason } 25525f39d397SChris Mason if (uptodate) 25535f39d397SChris Mason eb->flags |= EXTENT_UPTODATE; 25544dc11904SChris Mason eb->flags |= EXTENT_BUFFER_FILLED; 255509be207dSChris Mason 255609be207dSChris Mason lru_add: 255709be207dSChris Mason spin_lock(&tree->lru_lock); 255809be207dSChris Mason add_lru(tree, eb); 255909be207dSChris Mason spin_unlock(&tree->lru_lock); 25605f39d397SChris Mason return eb; 256109be207dSChris Mason 25625f39d397SChris Mason fail: 2563856bf3e5SChris Mason spin_lock(&tree->lru_lock); 2564856bf3e5SChris Mason list_del_init(&eb->lru); 2565856bf3e5SChris Mason spin_unlock(&tree->lru_lock); 256609be207dSChris Mason if (!atomic_dec_and_test(&eb->refs)) 256709be207dSChris Mason return NULL; 25680591fb56SChris Mason for (index = 1; index < i; index++) { 256909be207dSChris Mason page_cache_release(extent_buffer_page(eb, index)); 257009be207dSChris Mason } 25710591fb56SChris Mason if (i > 0) 25720591fb56SChris Mason page_cache_release(extent_buffer_page(eb, 0)); 257309be207dSChris Mason __free_extent_buffer(eb); 25745f39d397SChris Mason return NULL; 25755f39d397SChris Mason } 25765f39d397SChris Mason EXPORT_SYMBOL(alloc_extent_buffer); 25775f39d397SChris Mason 25785f39d397SChris Mason struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree, 25795f39d397SChris Mason u64 start, unsigned long len, 25805f39d397SChris Mason gfp_t mask) 25815f39d397SChris Mason { 2582db94535dSChris Mason unsigned long num_pages = num_extent_pages(start, len); 258309be207dSChris Mason unsigned long i; 258409be207dSChris Mason unsigned long index = start >> PAGE_CACHE_SHIFT; 25855f39d397SChris Mason struct extent_buffer *eb; 25865f39d397SChris Mason struct page *p; 25875f39d397SChris Mason struct address_space *mapping = tree->mapping; 258814048ed0SChris Mason int uptodate = 1; 25895f39d397SChris Mason 25904dc11904SChris Mason eb = __alloc_extent_buffer(tree, start, len, mask); 25915f39d397SChris Mason if (!eb || IS_ERR(eb)) 25925f39d397SChris Mason return NULL; 25935f39d397SChris Mason 25944dc11904SChris Mason if (eb->flags & EXTENT_BUFFER_FILLED) 259509be207dSChris Mason goto lru_add; 25965f39d397SChris Mason 25975f39d397SChris Mason for (i = 0; i < num_pages; i++, index++) { 259814048ed0SChris Mason p = find_lock_page(mapping, index); 25996d36dcd4SChris Mason if (!p) { 26005f39d397SChris Mason goto fail; 26016d36dcd4SChris Mason } 2602f510cfecSChris Mason set_page_extent_mapped(p); 2603ff79f819SChris Mason mark_page_accessed(p); 260419c00ddcSChris Mason 260519c00ddcSChris Mason if (i == 0) { 2606810191ffSChris Mason eb->first_page = p; 260755c69072SChris Mason set_page_extent_head(p, len); 260819c00ddcSChris Mason } else { 260919c00ddcSChris Mason set_page_private(p, EXTENT_PAGE_PRIVATE); 261019c00ddcSChris Mason } 261119c00ddcSChris Mason 261214048ed0SChris Mason if (!PageUptodate(p)) 261314048ed0SChris Mason uptodate = 0; 261414048ed0SChris Mason unlock_page(p); 26155f39d397SChris Mason } 261614048ed0SChris Mason if (uptodate) 261714048ed0SChris Mason eb->flags |= EXTENT_UPTODATE; 26184dc11904SChris Mason eb->flags |= EXTENT_BUFFER_FILLED; 261909be207dSChris Mason 262009be207dSChris Mason lru_add: 262109be207dSChris Mason spin_lock(&tree->lru_lock); 262209be207dSChris Mason add_lru(tree, eb); 262309be207dSChris Mason spin_unlock(&tree->lru_lock); 26245f39d397SChris Mason return eb; 26255f39d397SChris Mason fail: 2626856bf3e5SChris Mason spin_lock(&tree->lru_lock); 2627856bf3e5SChris Mason list_del_init(&eb->lru); 2628856bf3e5SChris Mason spin_unlock(&tree->lru_lock); 262909be207dSChris Mason if (!atomic_dec_and_test(&eb->refs)) 263009be207dSChris Mason return NULL; 26310591fb56SChris Mason for (index = 1; index < i; index++) { 263209be207dSChris Mason page_cache_release(extent_buffer_page(eb, index)); 263309be207dSChris Mason } 26340591fb56SChris Mason if (i > 0) 26350591fb56SChris Mason page_cache_release(extent_buffer_page(eb, 0)); 263609be207dSChris Mason __free_extent_buffer(eb); 26375f39d397SChris Mason return NULL; 26385f39d397SChris Mason } 26395f39d397SChris Mason EXPORT_SYMBOL(find_extent_buffer); 26405f39d397SChris Mason 26415f39d397SChris Mason void free_extent_buffer(struct extent_buffer *eb) 26425f39d397SChris Mason { 26435f39d397SChris Mason unsigned long i; 26445f39d397SChris Mason unsigned long num_pages; 26455f39d397SChris Mason 26465f39d397SChris Mason if (!eb) 26475f39d397SChris Mason return; 26485f39d397SChris Mason 26495f39d397SChris Mason if (!atomic_dec_and_test(&eb->refs)) 26505f39d397SChris Mason return; 26515f39d397SChris Mason 26520591fb56SChris Mason WARN_ON(!list_empty(&eb->lru)); 2653db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 26545f39d397SChris Mason 26550591fb56SChris Mason for (i = 1; i < num_pages; i++) { 26566d36dcd4SChris Mason page_cache_release(extent_buffer_page(eb, i)); 26575f39d397SChris Mason } 26580591fb56SChris Mason page_cache_release(extent_buffer_page(eb, 0)); 26596d36dcd4SChris Mason __free_extent_buffer(eb); 26605f39d397SChris Mason } 26615f39d397SChris Mason EXPORT_SYMBOL(free_extent_buffer); 26625f39d397SChris Mason 26635f39d397SChris Mason int clear_extent_buffer_dirty(struct extent_map_tree *tree, 26645f39d397SChris Mason struct extent_buffer *eb) 26655f39d397SChris Mason { 26665f39d397SChris Mason int set; 26675f39d397SChris Mason unsigned long i; 26685f39d397SChris Mason unsigned long num_pages; 26695f39d397SChris Mason struct page *page; 26705f39d397SChris Mason 26715f39d397SChris Mason u64 start = eb->start; 26725f39d397SChris Mason u64 end = start + eb->len - 1; 26735f39d397SChris Mason 26745f39d397SChris Mason set = clear_extent_dirty(tree, start, end, GFP_NOFS); 2675db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 26765f39d397SChris Mason 26775f39d397SChris Mason for (i = 0; i < num_pages; i++) { 26786d36dcd4SChris Mason page = extent_buffer_page(eb, i); 26795f39d397SChris Mason lock_page(page); 268055c69072SChris Mason if (i == 0) 268155c69072SChris Mason set_page_extent_head(page, eb->len); 268255c69072SChris Mason else 268355c69072SChris Mason set_page_private(page, EXTENT_PAGE_PRIVATE); 268455c69072SChris Mason 26855f39d397SChris Mason /* 26865f39d397SChris Mason * if we're on the last page or the first page and the 26875f39d397SChris Mason * block isn't aligned on a page boundary, do extra checks 26885f39d397SChris Mason * to make sure we don't clean page that is partially dirty 26895f39d397SChris Mason */ 26905f39d397SChris Mason if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || 26915f39d397SChris Mason ((i == num_pages - 1) && 269265555a06SYan ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { 269335ebb934SChris Mason start = (u64)page->index << PAGE_CACHE_SHIFT; 26945f39d397SChris Mason end = start + PAGE_CACHE_SIZE - 1; 26955f39d397SChris Mason if (test_range_bit(tree, start, end, 26965f39d397SChris Mason EXTENT_DIRTY, 0)) { 26975f39d397SChris Mason unlock_page(page); 26985f39d397SChris Mason continue; 26995f39d397SChris Mason } 27005f39d397SChris Mason } 27015f39d397SChris Mason clear_page_dirty_for_io(page); 27027073c8e8SChris Mason write_lock_irq(&page->mapping->tree_lock); 27037073c8e8SChris Mason if (!PageDirty(page)) { 27047073c8e8SChris Mason radix_tree_tag_clear(&page->mapping->page_tree, 27057073c8e8SChris Mason page_index(page), 27067073c8e8SChris Mason PAGECACHE_TAG_DIRTY); 27077073c8e8SChris Mason } 27087073c8e8SChris Mason write_unlock_irq(&page->mapping->tree_lock); 27095f39d397SChris Mason unlock_page(page); 27105f39d397SChris Mason } 27115f39d397SChris Mason return 0; 27125f39d397SChris Mason } 27135f39d397SChris Mason EXPORT_SYMBOL(clear_extent_buffer_dirty); 27145f39d397SChris Mason 27155f39d397SChris Mason int wait_on_extent_buffer_writeback(struct extent_map_tree *tree, 27165f39d397SChris Mason struct extent_buffer *eb) 27175f39d397SChris Mason { 27185f39d397SChris Mason return wait_on_extent_writeback(tree, eb->start, 27195f39d397SChris Mason eb->start + eb->len - 1); 27205f39d397SChris Mason } 27215f39d397SChris Mason EXPORT_SYMBOL(wait_on_extent_buffer_writeback); 27225f39d397SChris Mason 27235f39d397SChris Mason int set_extent_buffer_dirty(struct extent_map_tree *tree, 27245f39d397SChris Mason struct extent_buffer *eb) 27255f39d397SChris Mason { 2726810191ffSChris Mason unsigned long i; 2727810191ffSChris Mason unsigned long num_pages; 2728810191ffSChris Mason 2729810191ffSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 2730810191ffSChris Mason for (i = 0; i < num_pages; i++) { 273119c00ddcSChris Mason struct page *page = extent_buffer_page(eb, i); 273219c00ddcSChris Mason /* writepage may need to do something special for the 273319c00ddcSChris Mason * first page, we have to make sure page->private is 273419c00ddcSChris Mason * properly set. releasepage may drop page->private 273519c00ddcSChris Mason * on us if the page isn't already dirty. 273619c00ddcSChris Mason */ 273719c00ddcSChris Mason if (i == 0) { 273819c00ddcSChris Mason lock_page(page); 273955c69072SChris Mason set_page_extent_head(page, eb->len); 274055c69072SChris Mason } else if (PagePrivate(page) && 274155c69072SChris Mason page->private != EXTENT_PAGE_PRIVATE) { 274255c69072SChris Mason lock_page(page); 274355c69072SChris Mason set_page_extent_mapped(page); 274455c69072SChris Mason unlock_page(page); 274519c00ddcSChris Mason } 2746810191ffSChris Mason __set_page_dirty_nobuffers(extent_buffer_page(eb, i)); 274719c00ddcSChris Mason if (i == 0) 274819c00ddcSChris Mason unlock_page(page); 2749810191ffSChris Mason } 2750810191ffSChris Mason return set_extent_dirty(tree, eb->start, 2751810191ffSChris Mason eb->start + eb->len - 1, GFP_NOFS); 27525f39d397SChris Mason } 27535f39d397SChris Mason EXPORT_SYMBOL(set_extent_buffer_dirty); 27545f39d397SChris Mason 27555f39d397SChris Mason int set_extent_buffer_uptodate(struct extent_map_tree *tree, 27565f39d397SChris Mason struct extent_buffer *eb) 27575f39d397SChris Mason { 27585f39d397SChris Mason unsigned long i; 27595f39d397SChris Mason struct page *page; 27605f39d397SChris Mason unsigned long num_pages; 27615f39d397SChris Mason 2762db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 27635f39d397SChris Mason 27645f39d397SChris Mason set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, 27655f39d397SChris Mason GFP_NOFS); 27665f39d397SChris Mason for (i = 0; i < num_pages; i++) { 27676d36dcd4SChris Mason page = extent_buffer_page(eb, i); 27685f39d397SChris Mason if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || 27695f39d397SChris Mason ((i == num_pages - 1) && 277065555a06SYan ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { 27715f39d397SChris Mason check_page_uptodate(tree, page); 27725f39d397SChris Mason continue; 27735f39d397SChris Mason } 27745f39d397SChris Mason SetPageUptodate(page); 27755f39d397SChris Mason } 27765f39d397SChris Mason return 0; 27775f39d397SChris Mason } 27785f39d397SChris Mason EXPORT_SYMBOL(set_extent_buffer_uptodate); 27795f39d397SChris Mason 27805f39d397SChris Mason int extent_buffer_uptodate(struct extent_map_tree *tree, 27815f39d397SChris Mason struct extent_buffer *eb) 27825f39d397SChris Mason { 27835f39d397SChris Mason if (eb->flags & EXTENT_UPTODATE) 27845f39d397SChris Mason return 1; 27855f39d397SChris Mason return test_range_bit(tree, eb->start, eb->start + eb->len - 1, 27865f39d397SChris Mason EXTENT_UPTODATE, 1); 27875f39d397SChris Mason } 27885f39d397SChris Mason EXPORT_SYMBOL(extent_buffer_uptodate); 27895f39d397SChris Mason 27905f39d397SChris Mason int read_extent_buffer_pages(struct extent_map_tree *tree, 279119c00ddcSChris Mason struct extent_buffer *eb, 279219c00ddcSChris Mason u64 start, 279319c00ddcSChris Mason int wait) 27945f39d397SChris Mason { 27955f39d397SChris Mason unsigned long i; 279619c00ddcSChris Mason unsigned long start_i; 27975f39d397SChris Mason struct page *page; 27985f39d397SChris Mason int err; 27995f39d397SChris Mason int ret = 0; 28005f39d397SChris Mason unsigned long num_pages; 28015f39d397SChris Mason 28025f39d397SChris Mason if (eb->flags & EXTENT_UPTODATE) 28035f39d397SChris Mason return 0; 28045f39d397SChris Mason 280514048ed0SChris Mason if (0 && test_range_bit(tree, eb->start, eb->start + eb->len - 1, 28065f39d397SChris Mason EXTENT_UPTODATE, 1)) { 28075f39d397SChris Mason return 0; 28085f39d397SChris Mason } 28090591fb56SChris Mason 281019c00ddcSChris Mason if (start) { 281119c00ddcSChris Mason WARN_ON(start < eb->start); 281219c00ddcSChris Mason start_i = (start >> PAGE_CACHE_SHIFT) - 281319c00ddcSChris Mason (eb->start >> PAGE_CACHE_SHIFT); 281419c00ddcSChris Mason } else { 281519c00ddcSChris Mason start_i = 0; 281619c00ddcSChris Mason } 28175f39d397SChris Mason 2818db94535dSChris Mason num_pages = num_extent_pages(eb->start, eb->len); 281919c00ddcSChris Mason for (i = start_i; i < num_pages; i++) { 28206d36dcd4SChris Mason page = extent_buffer_page(eb, i); 28215f39d397SChris Mason if (PageUptodate(page)) { 28225f39d397SChris Mason continue; 28235f39d397SChris Mason } 28245f39d397SChris Mason if (!wait) { 28255f39d397SChris Mason if (TestSetPageLocked(page)) { 28265f39d397SChris Mason continue; 28275f39d397SChris Mason } 28285f39d397SChris Mason } else { 28295f39d397SChris Mason lock_page(page); 28305f39d397SChris Mason } 28315f39d397SChris Mason if (!PageUptodate(page)) { 28325f39d397SChris Mason err = page->mapping->a_ops->readpage(NULL, page); 28335f39d397SChris Mason if (err) { 28345f39d397SChris Mason ret = err; 28355f39d397SChris Mason } 28365f39d397SChris Mason } else { 28375f39d397SChris Mason unlock_page(page); 28385f39d397SChris Mason } 28395f39d397SChris Mason } 28405f39d397SChris Mason 28415f39d397SChris Mason if (ret || !wait) { 28425f39d397SChris Mason return ret; 28435f39d397SChris Mason } 28445f39d397SChris Mason 284519c00ddcSChris Mason for (i = start_i; i < num_pages; i++) { 28466d36dcd4SChris Mason page = extent_buffer_page(eb, i); 28475f39d397SChris Mason wait_on_page_locked(page); 28485f39d397SChris Mason if (!PageUptodate(page)) { 28495f39d397SChris Mason ret = -EIO; 28505f39d397SChris Mason } 28515f39d397SChris Mason } 28524dc11904SChris Mason if (!ret) 28535f39d397SChris Mason eb->flags |= EXTENT_UPTODATE; 28545f39d397SChris Mason return ret; 28555f39d397SChris Mason } 28565f39d397SChris Mason EXPORT_SYMBOL(read_extent_buffer_pages); 28575f39d397SChris Mason 28585f39d397SChris Mason void read_extent_buffer(struct extent_buffer *eb, void *dstv, 28595f39d397SChris Mason unsigned long start, 28605f39d397SChris Mason unsigned long len) 28615f39d397SChris Mason { 28625f39d397SChris Mason size_t cur; 28635f39d397SChris Mason size_t offset; 28645f39d397SChris Mason struct page *page; 28655f39d397SChris Mason char *kaddr; 28665f39d397SChris Mason char *dst = (char *)dstv; 28675f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 28685f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 286914048ed0SChris Mason unsigned long num_pages = num_extent_pages(eb->start, eb->len); 28705f39d397SChris Mason 28715f39d397SChris Mason WARN_ON(start > eb->len); 28725f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 28735f39d397SChris Mason 28743685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 28755f39d397SChris Mason 28765f39d397SChris Mason while(len > 0) { 28776d36dcd4SChris Mason page = extent_buffer_page(eb, i); 287814048ed0SChris Mason if (!PageUptodate(page)) { 287914048ed0SChris Mason printk("page %lu not up to date i %lu, total %lu, len %lu\n", page->index, i, num_pages, eb->len); 288014048ed0SChris Mason WARN_ON(1); 288114048ed0SChris Mason } 28825f39d397SChris Mason WARN_ON(!PageUptodate(page)); 28835f39d397SChris Mason 28845f39d397SChris Mason cur = min(len, (PAGE_CACHE_SIZE - offset)); 288559d169e2SChris Mason kaddr = kmap_atomic(page, KM_USER1); 28865f39d397SChris Mason memcpy(dst, kaddr + offset, cur); 288759d169e2SChris Mason kunmap_atomic(kaddr, KM_USER1); 28885f39d397SChris Mason 28895f39d397SChris Mason dst += cur; 28905f39d397SChris Mason len -= cur; 28915f39d397SChris Mason offset = 0; 28925f39d397SChris Mason i++; 28935f39d397SChris Mason } 28945f39d397SChris Mason } 28955f39d397SChris Mason EXPORT_SYMBOL(read_extent_buffer); 28965f39d397SChris Mason 289719c00ddcSChris Mason int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, 2898db94535dSChris Mason unsigned long min_len, char **token, char **map, 28995f39d397SChris Mason unsigned long *map_start, 29005f39d397SChris Mason unsigned long *map_len, int km) 29015f39d397SChris Mason { 2902479965d6SChris Mason size_t offset = start & (PAGE_CACHE_SIZE - 1); 29035f39d397SChris Mason char *kaddr; 2904db94535dSChris Mason struct page *p; 29055f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 29065f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 290765555a06SYan unsigned long end_i = (start_offset + start + min_len - 1) >> 2908479965d6SChris Mason PAGE_CACHE_SHIFT; 2909479965d6SChris Mason 2910479965d6SChris Mason if (i != end_i) 2911479965d6SChris Mason return -EINVAL; 29125f39d397SChris Mason 29135f39d397SChris Mason if (i == 0) { 29145f39d397SChris Mason offset = start_offset; 29155f39d397SChris Mason *map_start = 0; 29165f39d397SChris Mason } else { 2917db94535dSChris Mason offset = 0; 29180591fb56SChris Mason *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; 29195f39d397SChris Mason } 292065555a06SYan if (start + min_len > eb->len) { 292119c00ddcSChris Mason printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len); 292219c00ddcSChris Mason WARN_ON(1); 292319c00ddcSChris Mason } 29245f39d397SChris Mason 2925db94535dSChris Mason p = extent_buffer_page(eb, i); 2926db94535dSChris Mason WARN_ON(!PageUptodate(p)); 2927db94535dSChris Mason kaddr = kmap_atomic(p, km); 29285f39d397SChris Mason *token = kaddr; 29295f39d397SChris Mason *map = kaddr + offset; 29305f39d397SChris Mason *map_len = PAGE_CACHE_SIZE - offset; 29315f39d397SChris Mason return 0; 29325f39d397SChris Mason } 293319c00ddcSChris Mason EXPORT_SYMBOL(map_private_extent_buffer); 2934db94535dSChris Mason 2935db94535dSChris Mason int map_extent_buffer(struct extent_buffer *eb, unsigned long start, 2936db94535dSChris Mason unsigned long min_len, 2937db94535dSChris Mason char **token, char **map, 2938db94535dSChris Mason unsigned long *map_start, 2939db94535dSChris Mason unsigned long *map_len, int km) 2940db94535dSChris Mason { 2941db94535dSChris Mason int err; 2942db94535dSChris Mason int save = 0; 2943db94535dSChris Mason if (eb->map_token) { 2944db94535dSChris Mason unmap_extent_buffer(eb, eb->map_token, km); 2945db94535dSChris Mason eb->map_token = NULL; 2946db94535dSChris Mason save = 1; 2947db94535dSChris Mason } 294819c00ddcSChris Mason err = map_private_extent_buffer(eb, start, min_len, token, map, 2949db94535dSChris Mason map_start, map_len, km); 2950db94535dSChris Mason if (!err && save) { 2951db94535dSChris Mason eb->map_token = *token; 2952db94535dSChris Mason eb->kaddr = *map; 2953db94535dSChris Mason eb->map_start = *map_start; 2954db94535dSChris Mason eb->map_len = *map_len; 2955db94535dSChris Mason } 2956db94535dSChris Mason return err; 2957db94535dSChris Mason } 29585f39d397SChris Mason EXPORT_SYMBOL(map_extent_buffer); 29595f39d397SChris Mason 29605f39d397SChris Mason void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km) 29615f39d397SChris Mason { 2962ae5252bdSChris Mason kunmap_atomic(token, km); 29635f39d397SChris Mason } 29645f39d397SChris Mason EXPORT_SYMBOL(unmap_extent_buffer); 29655f39d397SChris Mason 29665f39d397SChris Mason int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, 29675f39d397SChris Mason unsigned long start, 29685f39d397SChris Mason unsigned long len) 29695f39d397SChris Mason { 29705f39d397SChris Mason size_t cur; 29715f39d397SChris Mason size_t offset; 29725f39d397SChris Mason struct page *page; 29735f39d397SChris Mason char *kaddr; 29745f39d397SChris Mason char *ptr = (char *)ptrv; 29755f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 29765f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 29775f39d397SChris Mason int ret = 0; 29785f39d397SChris Mason 29795f39d397SChris Mason WARN_ON(start > eb->len); 29805f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 29815f39d397SChris Mason 29823685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 29835f39d397SChris Mason 29845f39d397SChris Mason while(len > 0) { 29856d36dcd4SChris Mason page = extent_buffer_page(eb, i); 29865f39d397SChris Mason WARN_ON(!PageUptodate(page)); 29875f39d397SChris Mason 29885f39d397SChris Mason cur = min(len, (PAGE_CACHE_SIZE - offset)); 29895f39d397SChris Mason 2990ae5252bdSChris Mason kaddr = kmap_atomic(page, KM_USER0); 29915f39d397SChris Mason ret = memcmp(ptr, kaddr + offset, cur); 2992ae5252bdSChris Mason kunmap_atomic(kaddr, KM_USER0); 29935f39d397SChris Mason if (ret) 29945f39d397SChris Mason break; 29955f39d397SChris Mason 29965f39d397SChris Mason ptr += cur; 29975f39d397SChris Mason len -= cur; 29985f39d397SChris Mason offset = 0; 29995f39d397SChris Mason i++; 30005f39d397SChris Mason } 30015f39d397SChris Mason return ret; 30025f39d397SChris Mason } 30035f39d397SChris Mason EXPORT_SYMBOL(memcmp_extent_buffer); 30045f39d397SChris Mason 30055f39d397SChris Mason void write_extent_buffer(struct extent_buffer *eb, const void *srcv, 30065f39d397SChris Mason unsigned long start, unsigned long len) 30075f39d397SChris Mason { 30085f39d397SChris Mason size_t cur; 30095f39d397SChris Mason size_t offset; 30105f39d397SChris Mason struct page *page; 30115f39d397SChris Mason char *kaddr; 30125f39d397SChris Mason char *src = (char *)srcv; 30135f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 30145f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 30155f39d397SChris Mason 30165f39d397SChris Mason WARN_ON(start > eb->len); 30175f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 30185f39d397SChris Mason 30193685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 30205f39d397SChris Mason 30215f39d397SChris Mason while(len > 0) { 30226d36dcd4SChris Mason page = extent_buffer_page(eb, i); 30235f39d397SChris Mason WARN_ON(!PageUptodate(page)); 30245f39d397SChris Mason 30255f39d397SChris Mason cur = min(len, PAGE_CACHE_SIZE - offset); 302659d169e2SChris Mason kaddr = kmap_atomic(page, KM_USER1); 30275f39d397SChris Mason memcpy(kaddr + offset, src, cur); 302859d169e2SChris Mason kunmap_atomic(kaddr, KM_USER1); 30295f39d397SChris Mason 30305f39d397SChris Mason src += cur; 30315f39d397SChris Mason len -= cur; 30325f39d397SChris Mason offset = 0; 30335f39d397SChris Mason i++; 30345f39d397SChris Mason } 30355f39d397SChris Mason } 30365f39d397SChris Mason EXPORT_SYMBOL(write_extent_buffer); 30375f39d397SChris Mason 30385f39d397SChris Mason void memset_extent_buffer(struct extent_buffer *eb, char c, 30395f39d397SChris Mason unsigned long start, unsigned long len) 30405f39d397SChris Mason { 30415f39d397SChris Mason size_t cur; 30425f39d397SChris Mason size_t offset; 30435f39d397SChris Mason struct page *page; 30445f39d397SChris Mason char *kaddr; 30455f39d397SChris Mason size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); 30465f39d397SChris Mason unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; 30475f39d397SChris Mason 30485f39d397SChris Mason WARN_ON(start > eb->len); 30495f39d397SChris Mason WARN_ON(start + len > eb->start + eb->len); 30505f39d397SChris Mason 30513685f791SChris Mason offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); 30525f39d397SChris Mason 30535f39d397SChris Mason while(len > 0) { 30546d36dcd4SChris Mason page = extent_buffer_page(eb, i); 30555f39d397SChris Mason WARN_ON(!PageUptodate(page)); 30565f39d397SChris Mason 30575f39d397SChris Mason cur = min(len, PAGE_CACHE_SIZE - offset); 3058ae5252bdSChris Mason kaddr = kmap_atomic(page, KM_USER0); 30595f39d397SChris Mason memset(kaddr + offset, c, cur); 3060ae5252bdSChris Mason kunmap_atomic(kaddr, KM_USER0); 30615f39d397SChris Mason 30625f39d397SChris Mason len -= cur; 30635f39d397SChris Mason offset = 0; 30645f39d397SChris Mason i++; 30655f39d397SChris Mason } 30665f39d397SChris Mason } 30675f39d397SChris Mason EXPORT_SYMBOL(memset_extent_buffer); 30685f39d397SChris Mason 30695f39d397SChris Mason void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, 30705f39d397SChris Mason unsigned long dst_offset, unsigned long src_offset, 30715f39d397SChris Mason unsigned long len) 30725f39d397SChris Mason { 30735f39d397SChris Mason u64 dst_len = dst->len; 30745f39d397SChris Mason size_t cur; 30755f39d397SChris Mason size_t offset; 30765f39d397SChris Mason struct page *page; 30775f39d397SChris Mason char *kaddr; 30785f39d397SChris Mason size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); 30795f39d397SChris Mason unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; 30805f39d397SChris Mason 30815f39d397SChris Mason WARN_ON(src->len != dst_len); 30825f39d397SChris Mason 30833685f791SChris Mason offset = (start_offset + dst_offset) & 30843685f791SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 30855f39d397SChris Mason 30865f39d397SChris Mason while(len > 0) { 30876d36dcd4SChris Mason page = extent_buffer_page(dst, i); 30885f39d397SChris Mason WARN_ON(!PageUptodate(page)); 30895f39d397SChris Mason 30905f39d397SChris Mason cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); 30915f39d397SChris Mason 3092ff190c0cSChris Mason kaddr = kmap_atomic(page, KM_USER0); 30935f39d397SChris Mason read_extent_buffer(src, kaddr + offset, src_offset, cur); 3094ff190c0cSChris Mason kunmap_atomic(kaddr, KM_USER0); 30955f39d397SChris Mason 30965f39d397SChris Mason src_offset += cur; 30975f39d397SChris Mason len -= cur; 30985f39d397SChris Mason offset = 0; 30995f39d397SChris Mason i++; 31005f39d397SChris Mason } 31015f39d397SChris Mason } 31025f39d397SChris Mason EXPORT_SYMBOL(copy_extent_buffer); 31035f39d397SChris Mason 31045f39d397SChris Mason static void move_pages(struct page *dst_page, struct page *src_page, 31055f39d397SChris Mason unsigned long dst_off, unsigned long src_off, 31065f39d397SChris Mason unsigned long len) 31075f39d397SChris Mason { 3108ae5252bdSChris Mason char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); 31095f39d397SChris Mason if (dst_page == src_page) { 31105f39d397SChris Mason memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len); 31115f39d397SChris Mason } else { 3112ae5252bdSChris Mason char *src_kaddr = kmap_atomic(src_page, KM_USER1); 31135f39d397SChris Mason char *p = dst_kaddr + dst_off + len; 31145f39d397SChris Mason char *s = src_kaddr + src_off + len; 31155f39d397SChris Mason 31165f39d397SChris Mason while (len--) 31175f39d397SChris Mason *--p = *--s; 31185f39d397SChris Mason 3119ae5252bdSChris Mason kunmap_atomic(src_kaddr, KM_USER1); 31205f39d397SChris Mason } 3121ae5252bdSChris Mason kunmap_atomic(dst_kaddr, KM_USER0); 31225f39d397SChris Mason } 31235f39d397SChris Mason 31245f39d397SChris Mason static void copy_pages(struct page *dst_page, struct page *src_page, 31255f39d397SChris Mason unsigned long dst_off, unsigned long src_off, 31265f39d397SChris Mason unsigned long len) 31275f39d397SChris Mason { 3128ae5252bdSChris Mason char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); 31295f39d397SChris Mason char *src_kaddr; 31305f39d397SChris Mason 31315f39d397SChris Mason if (dst_page != src_page) 3132ae5252bdSChris Mason src_kaddr = kmap_atomic(src_page, KM_USER1); 31335f39d397SChris Mason else 31345f39d397SChris Mason src_kaddr = dst_kaddr; 31355f39d397SChris Mason 31365f39d397SChris Mason memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); 31375f39d397SChris Mason kunmap_atomic(dst_kaddr, KM_USER0); 31385f39d397SChris Mason if (dst_page != src_page) 31395f39d397SChris Mason kunmap_atomic(src_kaddr, KM_USER1); 31405f39d397SChris Mason } 31415f39d397SChris Mason 31425f39d397SChris Mason void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, 31435f39d397SChris Mason unsigned long src_offset, unsigned long len) 31445f39d397SChris Mason { 31455f39d397SChris Mason size_t cur; 31465f39d397SChris Mason size_t dst_off_in_page; 31475f39d397SChris Mason size_t src_off_in_page; 31485f39d397SChris Mason size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); 31495f39d397SChris Mason unsigned long dst_i; 31505f39d397SChris Mason unsigned long src_i; 31515f39d397SChris Mason 31525f39d397SChris Mason if (src_offset + len > dst->len) { 31535f39d397SChris Mason printk("memmove bogus src_offset %lu move len %lu len %lu\n", 31545f39d397SChris Mason src_offset, len, dst->len); 31555f39d397SChris Mason BUG_ON(1); 31565f39d397SChris Mason } 31575f39d397SChris Mason if (dst_offset + len > dst->len) { 31585f39d397SChris Mason printk("memmove bogus dst_offset %lu move len %lu len %lu\n", 31595f39d397SChris Mason dst_offset, len, dst->len); 31605f39d397SChris Mason BUG_ON(1); 31615f39d397SChris Mason } 31625f39d397SChris Mason 31635f39d397SChris Mason while(len > 0) { 31643685f791SChris Mason dst_off_in_page = (start_offset + dst_offset) & 31655f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 31663685f791SChris Mason src_off_in_page = (start_offset + src_offset) & 31675f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 31685f39d397SChris Mason 31695f39d397SChris Mason dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; 31705f39d397SChris Mason src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; 31715f39d397SChris Mason 31725f39d397SChris Mason cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - 31735f39d397SChris Mason src_off_in_page)); 3174ae2f5411SJens Axboe cur = min_t(unsigned long, cur, 3175ae2f5411SJens Axboe (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); 31765f39d397SChris Mason 31776d36dcd4SChris Mason copy_pages(extent_buffer_page(dst, dst_i), 31786d36dcd4SChris Mason extent_buffer_page(dst, src_i), 31795f39d397SChris Mason dst_off_in_page, src_off_in_page, cur); 31805f39d397SChris Mason 31815f39d397SChris Mason src_offset += cur; 31825f39d397SChris Mason dst_offset += cur; 31835f39d397SChris Mason len -= cur; 31845f39d397SChris Mason } 31855f39d397SChris Mason } 31865f39d397SChris Mason EXPORT_SYMBOL(memcpy_extent_buffer); 31875f39d397SChris Mason 31885f39d397SChris Mason void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, 31895f39d397SChris Mason unsigned long src_offset, unsigned long len) 31905f39d397SChris Mason { 31915f39d397SChris Mason size_t cur; 31925f39d397SChris Mason size_t dst_off_in_page; 31935f39d397SChris Mason size_t src_off_in_page; 31945f39d397SChris Mason unsigned long dst_end = dst_offset + len - 1; 31955f39d397SChris Mason unsigned long src_end = src_offset + len - 1; 31965f39d397SChris Mason size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); 31975f39d397SChris Mason unsigned long dst_i; 31985f39d397SChris Mason unsigned long src_i; 31995f39d397SChris Mason 32005f39d397SChris Mason if (src_offset + len > dst->len) { 32015f39d397SChris Mason printk("memmove bogus src_offset %lu move len %lu len %lu\n", 32025f39d397SChris Mason src_offset, len, dst->len); 32035f39d397SChris Mason BUG_ON(1); 32045f39d397SChris Mason } 32055f39d397SChris Mason if (dst_offset + len > dst->len) { 32065f39d397SChris Mason printk("memmove bogus dst_offset %lu move len %lu len %lu\n", 32075f39d397SChris Mason dst_offset, len, dst->len); 32085f39d397SChris Mason BUG_ON(1); 32095f39d397SChris Mason } 32105f39d397SChris Mason if (dst_offset < src_offset) { 32115f39d397SChris Mason memcpy_extent_buffer(dst, dst_offset, src_offset, len); 32125f39d397SChris Mason return; 32135f39d397SChris Mason } 32145f39d397SChris Mason while(len > 0) { 32155f39d397SChris Mason dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; 32165f39d397SChris Mason src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; 32175f39d397SChris Mason 32183685f791SChris Mason dst_off_in_page = (start_offset + dst_end) & 32195f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 32203685f791SChris Mason src_off_in_page = (start_offset + src_end) & 32215f39d397SChris Mason ((unsigned long)PAGE_CACHE_SIZE - 1); 32225f39d397SChris Mason 3223ae2f5411SJens Axboe cur = min_t(unsigned long, len, src_off_in_page + 1); 32245f39d397SChris Mason cur = min(cur, dst_off_in_page + 1); 32256d36dcd4SChris Mason move_pages(extent_buffer_page(dst, dst_i), 32266d36dcd4SChris Mason extent_buffer_page(dst, src_i), 32275f39d397SChris Mason dst_off_in_page - cur + 1, 32285f39d397SChris Mason src_off_in_page - cur + 1, cur); 32295f39d397SChris Mason 3230db94535dSChris Mason dst_end -= cur; 3231db94535dSChris Mason src_end -= cur; 32325f39d397SChris Mason len -= cur; 32335f39d397SChris Mason } 32345f39d397SChris Mason } 32355f39d397SChris Mason EXPORT_SYMBOL(memmove_extent_buffer); 3236