Btrfs: leave spinning on lookup and map the leaf
[pandora-kernel.git] / fs / btrfs / free-space-cache.c
1 /*
2  * Copyright (C) 2008 Red Hat.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/math64.h>
23 #include "ctree.h"
24 #include "free-space-cache.h"
25 #include "transaction.h"
26 #include "disk-io.h"
27 #include "extent_io.h"
28
29 #define BITS_PER_BITMAP         (PAGE_CACHE_SIZE * 8)
30 #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
31
32 static void recalculate_thresholds(struct btrfs_block_group_cache
33                                    *block_group);
34 static int link_free_space(struct btrfs_block_group_cache *block_group,
35                            struct btrfs_free_space *info);
36
37 struct inode *lookup_free_space_inode(struct btrfs_root *root,
38                                       struct btrfs_block_group_cache
39                                       *block_group, struct btrfs_path *path)
40 {
41         struct btrfs_key key;
42         struct btrfs_key location;
43         struct btrfs_disk_key disk_key;
44         struct btrfs_free_space_header *header;
45         struct extent_buffer *leaf;
46         struct inode *inode = NULL;
47         int ret;
48
49         spin_lock(&block_group->lock);
50         if (block_group->inode)
51                 inode = igrab(block_group->inode);
52         spin_unlock(&block_group->lock);
53         if (inode)
54                 return inode;
55
56         key.objectid = BTRFS_FREE_SPACE_OBJECTID;
57         key.offset = block_group->key.objectid;
58         key.type = 0;
59
60         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
61         if (ret < 0)
62                 return ERR_PTR(ret);
63         if (ret > 0) {
64                 btrfs_release_path(root, path);
65                 return ERR_PTR(-ENOENT);
66         }
67
68         leaf = path->nodes[0];
69         header = btrfs_item_ptr(leaf, path->slots[0],
70                                 struct btrfs_free_space_header);
71         btrfs_free_space_key(leaf, header, &disk_key);
72         btrfs_disk_key_to_cpu(&location, &disk_key);
73         btrfs_release_path(root, path);
74
75         inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
76         if (!inode)
77                 return ERR_PTR(-ENOENT);
78         if (IS_ERR(inode))
79                 return inode;
80         if (is_bad_inode(inode)) {
81                 iput(inode);
82                 return ERR_PTR(-ENOENT);
83         }
84
85         inode->i_mapping->flags &= ~__GFP_FS;
86
87         spin_lock(&block_group->lock);
88         if (!root->fs_info->closing) {
89                 block_group->inode = igrab(inode);
90                 block_group->iref = 1;
91         }
92         spin_unlock(&block_group->lock);
93
94         return inode;
95 }
96
97 int create_free_space_inode(struct btrfs_root *root,
98                             struct btrfs_trans_handle *trans,
99                             struct btrfs_block_group_cache *block_group,
100                             struct btrfs_path *path)
101 {
102         struct btrfs_key key;
103         struct btrfs_disk_key disk_key;
104         struct btrfs_free_space_header *header;
105         struct btrfs_inode_item *inode_item;
106         struct extent_buffer *leaf;
107         u64 objectid;
108         int ret;
109
110         ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
111         if (ret < 0)
112                 return ret;
113
114         ret = btrfs_insert_empty_inode(trans, root, path, objectid);
115         if (ret)
116                 return ret;
117
118         leaf = path->nodes[0];
119         inode_item = btrfs_item_ptr(leaf, path->slots[0],
120                                     struct btrfs_inode_item);
121         btrfs_item_key(leaf, &disk_key, path->slots[0]);
122         memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
123                              sizeof(*inode_item));
124         btrfs_set_inode_generation(leaf, inode_item, trans->transid);
125         btrfs_set_inode_size(leaf, inode_item, 0);
126         btrfs_set_inode_nbytes(leaf, inode_item, 0);
127         btrfs_set_inode_uid(leaf, inode_item, 0);
128         btrfs_set_inode_gid(leaf, inode_item, 0);
129         btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
130         btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
131                               BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
132         btrfs_set_inode_nlink(leaf, inode_item, 1);
133         btrfs_set_inode_transid(leaf, inode_item, trans->transid);
134         btrfs_set_inode_block_group(leaf, inode_item,
135                                     block_group->key.objectid);
136         btrfs_mark_buffer_dirty(leaf);
137         btrfs_release_path(root, path);
138
139         key.objectid = BTRFS_FREE_SPACE_OBJECTID;
140         key.offset = block_group->key.objectid;
141         key.type = 0;
142
143         ret = btrfs_insert_empty_item(trans, root, path, &key,
144                                       sizeof(struct btrfs_free_space_header));
145         if (ret < 0) {
146                 btrfs_release_path(root, path);
147                 return ret;
148         }
149         leaf = path->nodes[0];
150         header = btrfs_item_ptr(leaf, path->slots[0],
151                                 struct btrfs_free_space_header);
152         memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
153         btrfs_set_free_space_key(leaf, header, &disk_key);
154         btrfs_mark_buffer_dirty(leaf);
155         btrfs_release_path(root, path);
156
157         return 0;
158 }
159
160 int btrfs_truncate_free_space_cache(struct btrfs_root *root,
161                                     struct btrfs_trans_handle *trans,
162                                     struct btrfs_path *path,
163                                     struct inode *inode)
164 {
165         loff_t oldsize;
166         int ret = 0;
167
168         trans->block_rsv = root->orphan_block_rsv;
169         ret = btrfs_block_rsv_check(trans, root,
170                                     root->orphan_block_rsv,
171                                     0, 5);
172         if (ret)
173                 return ret;
174
175         oldsize = i_size_read(inode);
176         btrfs_i_size_write(inode, 0);
177         truncate_pagecache(inode, oldsize, 0);
178
179         /*
180          * We don't need an orphan item because truncating the free space cache
181          * will never be split across transactions.
182          */
183         ret = btrfs_truncate_inode_items(trans, root, inode,
184                                          0, BTRFS_EXTENT_DATA_KEY);
185         if (ret) {
186                 WARN_ON(1);
187                 return ret;
188         }
189
190         return btrfs_update_inode(trans, root, inode);
191 }
192
193 static int readahead_cache(struct inode *inode)
194 {
195         struct file_ra_state *ra;
196         unsigned long last_index;
197
198         ra = kzalloc(sizeof(*ra), GFP_NOFS);
199         if (!ra)
200                 return -ENOMEM;
201
202         file_ra_state_init(ra, inode->i_mapping);
203         last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
204
205         page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
206
207         kfree(ra);
208
209         return 0;
210 }
211
212 int load_free_space_cache(struct btrfs_fs_info *fs_info,
213                           struct btrfs_block_group_cache *block_group)
214 {
215         struct btrfs_root *root = fs_info->tree_root;
216         struct inode *inode;
217         struct btrfs_free_space_header *header;
218         struct extent_buffer *leaf;
219         struct page *page;
220         struct btrfs_path *path;
221         u32 *checksums = NULL, *crc;
222         char *disk_crcs = NULL;
223         struct btrfs_key key;
224         struct list_head bitmaps;
225         u64 num_entries;
226         u64 num_bitmaps;
227         u64 generation;
228         u64 used = btrfs_block_group_used(&block_group->item);
229         u32 cur_crc = ~(u32)0;
230         pgoff_t index = 0;
231         unsigned long first_page_offset;
232         int num_checksums;
233         int ret = 0;
234
235         /*
236          * If we're unmounting then just return, since this does a search on the
237          * normal root and not the commit root and we could deadlock.
238          */
239         smp_mb();
240         if (fs_info->closing)
241                 return 0;
242
243         /*
244          * If this block group has been marked to be cleared for one reason or
245          * another then we can't trust the on disk cache, so just return.
246          */
247         spin_lock(&block_group->lock);
248         if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
249                 spin_unlock(&block_group->lock);
250                 return 0;
251         }
252         spin_unlock(&block_group->lock);
253
254         INIT_LIST_HEAD(&bitmaps);
255
256         path = btrfs_alloc_path();
257         if (!path)
258                 return 0;
259
260         inode = lookup_free_space_inode(root, block_group, path);
261         if (IS_ERR(inode)) {
262                 btrfs_free_path(path);
263                 return 0;
264         }
265
266         /* Nothing in the space cache, goodbye */
267         if (!i_size_read(inode)) {
268                 btrfs_free_path(path);
269                 goto out;
270         }
271
272         key.objectid = BTRFS_FREE_SPACE_OBJECTID;
273         key.offset = block_group->key.objectid;
274         key.type = 0;
275
276         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
277         if (ret) {
278                 btrfs_free_path(path);
279                 goto out;
280         }
281
282         leaf = path->nodes[0];
283         header = btrfs_item_ptr(leaf, path->slots[0],
284                                 struct btrfs_free_space_header);
285         num_entries = btrfs_free_space_entries(leaf, header);
286         num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
287         generation = btrfs_free_space_generation(leaf, header);
288         btrfs_free_path(path);
289
290         if (BTRFS_I(inode)->generation != generation) {
291                 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
292                        " not match free space cache generation (%llu) for "
293                        "block group %llu\n",
294                        (unsigned long long)BTRFS_I(inode)->generation,
295                        (unsigned long long)generation,
296                        (unsigned long long)block_group->key.objectid);
297                 goto free_cache;
298         }
299
300         if (!num_entries)
301                 goto out;
302
303         /* Setup everything for doing checksumming */
304         num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
305         checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
306         if (!checksums)
307                 goto out;
308         first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
309         disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
310         if (!disk_crcs)
311                 goto out;
312
313         ret = readahead_cache(inode);
314         if (ret) {
315                 ret = 0;
316                 goto out;
317         }
318
319         while (1) {
320                 struct btrfs_free_space_entry *entry;
321                 struct btrfs_free_space *e;
322                 void *addr;
323                 unsigned long offset = 0;
324                 unsigned long start_offset = 0;
325                 int need_loop = 0;
326
327                 if (!num_entries && !num_bitmaps)
328                         break;
329
330                 if (index == 0) {
331                         start_offset = first_page_offset;
332                         offset = start_offset;
333                 }
334
335                 page = grab_cache_page(inode->i_mapping, index);
336                 if (!page) {
337                         ret = 0;
338                         goto free_cache;
339                 }
340
341                 if (!PageUptodate(page)) {
342                         btrfs_readpage(NULL, page);
343                         lock_page(page);
344                         if (!PageUptodate(page)) {
345                                 unlock_page(page);
346                                 page_cache_release(page);
347                                 printk(KERN_ERR "btrfs: error reading free "
348                                        "space cache: %llu\n",
349                                        (unsigned long long)
350                                        block_group->key.objectid);
351                                 goto free_cache;
352                         }
353                 }
354                 addr = kmap(page);
355
356                 if (index == 0) {
357                         u64 *gen;
358
359                         memcpy(disk_crcs, addr, first_page_offset);
360                         gen = addr + (sizeof(u32) * num_checksums);
361                         if (*gen != BTRFS_I(inode)->generation) {
362                                 printk(KERN_ERR "btrfs: space cache generation"
363                                        " (%llu) does not match inode (%llu) "
364                                        "for block group %llu\n",
365                                        (unsigned long long)*gen,
366                                        (unsigned long long)
367                                        BTRFS_I(inode)->generation,
368                                        (unsigned long long)
369                                        block_group->key.objectid);
370                                 kunmap(page);
371                                 unlock_page(page);
372                                 page_cache_release(page);
373                                 goto free_cache;
374                         }
375                         crc = (u32 *)disk_crcs;
376                 }
377                 entry = addr + start_offset;
378
379                 /* First lets check our crc before we do anything fun */
380                 cur_crc = ~(u32)0;
381                 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
382                                           PAGE_CACHE_SIZE - start_offset);
383                 btrfs_csum_final(cur_crc, (char *)&cur_crc);
384                 if (cur_crc != *crc) {
385                         printk(KERN_ERR "btrfs: crc mismatch for page %lu in "
386                                "block group %llu\n", index,
387                                (unsigned long long)block_group->key.objectid);
388                         kunmap(page);
389                         unlock_page(page);
390                         page_cache_release(page);
391                         goto free_cache;
392                 }
393                 crc++;
394
395                 while (1) {
396                         if (!num_entries)
397                                 break;
398
399                         need_loop = 1;
400                         e = kmem_cache_zalloc(btrfs_free_space_cachep,
401                                               GFP_NOFS);
402                         if (!e) {
403                                 kunmap(page);
404                                 unlock_page(page);
405                                 page_cache_release(page);
406                                 goto free_cache;
407                         }
408
409                         e->offset = le64_to_cpu(entry->offset);
410                         e->bytes = le64_to_cpu(entry->bytes);
411                         if (!e->bytes) {
412                                 kunmap(page);
413                                 kmem_cache_free(btrfs_free_space_cachep, e);
414                                 unlock_page(page);
415                                 page_cache_release(page);
416                                 goto free_cache;
417                         }
418
419                         if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
420                                 spin_lock(&block_group->tree_lock);
421                                 ret = link_free_space(block_group, e);
422                                 spin_unlock(&block_group->tree_lock);
423                                 if (ret) {
424                                         printk(KERN_ERR "Duplicate entries in "
425                                                "free space cache, dumping\n");
426                                         kunmap(page);
427                                         unlock_page(page);
428                                         page_cache_release(page);
429                                         goto free_cache;
430                                 }
431                         } else {
432                                 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
433                                 if (!e->bitmap) {
434                                         kunmap(page);
435                                         kmem_cache_free(
436                                                 btrfs_free_space_cachep, e);
437                                         unlock_page(page);
438                                         page_cache_release(page);
439                                         goto free_cache;
440                                 }
441                                 spin_lock(&block_group->tree_lock);
442                                 ret = link_free_space(block_group, e);
443                                 block_group->total_bitmaps++;
444                                 recalculate_thresholds(block_group);
445                                 spin_unlock(&block_group->tree_lock);
446                                 list_add_tail(&e->list, &bitmaps);
447                                 if (ret) {
448                                         printk(KERN_ERR "Duplicate entries in "
449                                                "free space cache, dumping\n");
450                                         kunmap(page);
451                                         unlock_page(page);
452                                         page_cache_release(page);
453                                         goto free_cache;
454                                 }
455                         }
456
457                         num_entries--;
458                         offset += sizeof(struct btrfs_free_space_entry);
459                         if (offset + sizeof(struct btrfs_free_space_entry) >=
460                             PAGE_CACHE_SIZE)
461                                 break;
462                         entry++;
463                 }
464
465                 /*
466                  * We read an entry out of this page, we need to move on to the
467                  * next page.
468                  */
469                 if (need_loop) {
470                         kunmap(page);
471                         goto next;
472                 }
473
474                 /*
475                  * We add the bitmaps at the end of the entries in order that
476                  * the bitmap entries are added to the cache.
477                  */
478                 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
479                 list_del_init(&e->list);
480                 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
481                 kunmap(page);
482                 num_bitmaps--;
483 next:
484                 unlock_page(page);
485                 page_cache_release(page);
486                 index++;
487         }
488
489         spin_lock(&block_group->tree_lock);
490         if (block_group->free_space != (block_group->key.offset - used -
491                                         block_group->bytes_super)) {
492                 spin_unlock(&block_group->tree_lock);
493                 printk(KERN_ERR "block group %llu has an wrong amount of free "
494                        "space\n", block_group->key.objectid);
495                 ret = 0;
496                 goto free_cache;
497         }
498         spin_unlock(&block_group->tree_lock);
499
500         ret = 1;
501 out:
502         kfree(checksums);
503         kfree(disk_crcs);
504         iput(inode);
505         return ret;
506
507 free_cache:
508         /* This cache is bogus, make sure it gets cleared */
509         spin_lock(&block_group->lock);
510         block_group->disk_cache_state = BTRFS_DC_CLEAR;
511         spin_unlock(&block_group->lock);
512         btrfs_remove_free_space_cache(block_group);
513         goto out;
514 }
515
516 int btrfs_write_out_cache(struct btrfs_root *root,
517                           struct btrfs_trans_handle *trans,
518                           struct btrfs_block_group_cache *block_group,
519                           struct btrfs_path *path)
520 {
521         struct btrfs_free_space_header *header;
522         struct extent_buffer *leaf;
523         struct inode *inode;
524         struct rb_node *node;
525         struct list_head *pos, *n;
526         struct page **pages;
527         struct page *page;
528         struct extent_state *cached_state = NULL;
529         struct btrfs_free_cluster *cluster = NULL;
530         struct extent_io_tree *unpin = NULL;
531         struct list_head bitmap_list;
532         struct btrfs_key key;
533         u64 start, end, len;
534         u64 bytes = 0;
535         u32 *crc, *checksums;
536         unsigned long first_page_offset;
537         int index = 0, num_pages = 0;
538         int entries = 0;
539         int bitmaps = 0;
540         int ret = 0;
541         bool next_page = false;
542         bool out_of_space = false;
543
544         root = root->fs_info->tree_root;
545
546         INIT_LIST_HEAD(&bitmap_list);
547
548         spin_lock(&block_group->lock);
549         if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
550                 spin_unlock(&block_group->lock);
551                 return 0;
552         }
553         spin_unlock(&block_group->lock);
554
555         inode = lookup_free_space_inode(root, block_group, path);
556         if (IS_ERR(inode))
557                 return 0;
558
559         if (!i_size_read(inode)) {
560                 iput(inode);
561                 return 0;
562         }
563
564         node = rb_first(&block_group->free_space_offset);
565         if (!node) {
566                 iput(inode);
567                 return 0;
568         }
569
570         num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
571                 PAGE_CACHE_SHIFT;
572         filemap_write_and_wait(inode->i_mapping);
573         btrfs_wait_ordered_range(inode, inode->i_size &
574                                  ~(root->sectorsize - 1), (u64)-1);
575
576         /* We need a checksum per page. */
577         crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS);
578         if (!crc) {
579                 iput(inode);
580                 return 0;
581         }
582
583         pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
584         if (!pages) {
585                 kfree(crc);
586                 iput(inode);
587                 return 0;
588         }
589
590         /* Since the first page has all of our checksums and our generation we
591          * need to calculate the offset into the page that we can start writing
592          * our entries.
593          */
594         first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64);
595
596         /* Get the cluster for this block_group if it exists */
597         if (!list_empty(&block_group->cluster_list))
598                 cluster = list_entry(block_group->cluster_list.next,
599                                      struct btrfs_free_cluster,
600                                      block_group_list);
601
602         /*
603          * We shouldn't have switched the pinned extents yet so this is the
604          * right one
605          */
606         unpin = root->fs_info->pinned_extents;
607
608         /*
609          * Lock all pages first so we can lock the extent safely.
610          *
611          * NOTE: Because we hold the ref the entire time we're going to write to
612          * the page find_get_page should never fail, so we don't do a check
613          * after find_get_page at this point.  Just putting this here so people
614          * know and don't freak out.
615          */
616         while (index < num_pages) {
617                 page = grab_cache_page(inode->i_mapping, index);
618                 if (!page) {
619                         int i;
620
621                         for (i = 0; i < num_pages; i++) {
622                                 unlock_page(pages[i]);
623                                 page_cache_release(pages[i]);
624                         }
625                         goto out_free;
626                 }
627                 pages[index] = page;
628                 index++;
629         }
630
631         index = 0;
632         lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
633                          0, &cached_state, GFP_NOFS);
634
635         /*
636          * When searching for pinned extents, we need to start at our start
637          * offset.
638          */
639         start = block_group->key.objectid;
640
641         /* Write out the extent entries */
642         do {
643                 struct btrfs_free_space_entry *entry;
644                 void *addr;
645                 unsigned long offset = 0;
646                 unsigned long start_offset = 0;
647
648                 next_page = false;
649
650                 if (index == 0) {
651                         start_offset = first_page_offset;
652                         offset = start_offset;
653                 }
654
655                 if (index >= num_pages) {
656                         out_of_space = true;
657                         break;
658                 }
659
660                 page = pages[index];
661
662                 addr = kmap(page);
663                 entry = addr + start_offset;
664
665                 memset(addr, 0, PAGE_CACHE_SIZE);
666                 while (node && !next_page) {
667                         struct btrfs_free_space *e;
668
669                         e = rb_entry(node, struct btrfs_free_space, offset_index);
670                         entries++;
671
672                         entry->offset = cpu_to_le64(e->offset);
673                         entry->bytes = cpu_to_le64(e->bytes);
674                         if (e->bitmap) {
675                                 entry->type = BTRFS_FREE_SPACE_BITMAP;
676                                 list_add_tail(&e->list, &bitmap_list);
677                                 bitmaps++;
678                         } else {
679                                 entry->type = BTRFS_FREE_SPACE_EXTENT;
680                         }
681                         node = rb_next(node);
682                         if (!node && cluster) {
683                                 node = rb_first(&cluster->root);
684                                 cluster = NULL;
685                         }
686                         offset += sizeof(struct btrfs_free_space_entry);
687                         if (offset + sizeof(struct btrfs_free_space_entry) >=
688                             PAGE_CACHE_SIZE)
689                                 next_page = true;
690                         entry++;
691                 }
692
693                 /*
694                  * We want to add any pinned extents to our free space cache
695                  * so we don't leak the space
696                  */
697                 while (!next_page && (start < block_group->key.objectid +
698                                       block_group->key.offset)) {
699                         ret = find_first_extent_bit(unpin, start, &start, &end,
700                                                     EXTENT_DIRTY);
701                         if (ret) {
702                                 ret = 0;
703                                 break;
704                         }
705
706                         /* This pinned extent is out of our range */
707                         if (start >= block_group->key.objectid +
708                             block_group->key.offset)
709                                 break;
710
711                         len = block_group->key.objectid +
712                                 block_group->key.offset - start;
713                         len = min(len, end + 1 - start);
714
715                         entries++;
716                         entry->offset = cpu_to_le64(start);
717                         entry->bytes = cpu_to_le64(len);
718                         entry->type = BTRFS_FREE_SPACE_EXTENT;
719
720                         start = end + 1;
721                         offset += sizeof(struct btrfs_free_space_entry);
722                         if (offset + sizeof(struct btrfs_free_space_entry) >=
723                             PAGE_CACHE_SIZE)
724                                 next_page = true;
725                         entry++;
726                 }
727                 *crc = ~(u32)0;
728                 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
729                                        PAGE_CACHE_SIZE - start_offset);
730                 kunmap(page);
731
732                 btrfs_csum_final(*crc, (char *)crc);
733                 crc++;
734
735                 bytes += PAGE_CACHE_SIZE;
736
737                 index++;
738         } while (node || next_page);
739
740         /* Write out the bitmaps */
741         list_for_each_safe(pos, n, &bitmap_list) {
742                 void *addr;
743                 struct btrfs_free_space *entry =
744                         list_entry(pos, struct btrfs_free_space, list);
745
746                 if (index >= num_pages) {
747                         out_of_space = true;
748                         break;
749                 }
750                 page = pages[index];
751
752                 addr = kmap(page);
753                 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
754                 *crc = ~(u32)0;
755                 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
756                 kunmap(page);
757                 btrfs_csum_final(*crc, (char *)crc);
758                 crc++;
759                 bytes += PAGE_CACHE_SIZE;
760
761                 list_del_init(&entry->list);
762                 index++;
763         }
764
765         if (out_of_space) {
766                 btrfs_drop_pages(pages, num_pages);
767                 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
768                                      i_size_read(inode) - 1, &cached_state,
769                                      GFP_NOFS);
770                 ret = 0;
771                 goto out_free;
772         }
773
774         /* Zero out the rest of the pages just to make sure */
775         while (index < num_pages) {
776                 void *addr;
777
778                 page = pages[index];
779                 addr = kmap(page);
780                 memset(addr, 0, PAGE_CACHE_SIZE);
781                 kunmap(page);
782                 bytes += PAGE_CACHE_SIZE;
783                 index++;
784         }
785
786         /* Write the checksums and trans id to the first page */
787         {
788                 void *addr;
789                 u64 *gen;
790
791                 page = pages[0];
792
793                 addr = kmap(page);
794                 memcpy(addr, checksums, sizeof(u32) * num_pages);
795                 gen = addr + (sizeof(u32) * num_pages);
796                 *gen = trans->transid;
797                 kunmap(page);
798         }
799
800         ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
801                                             bytes, &cached_state);
802         btrfs_drop_pages(pages, num_pages);
803         unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
804                              i_size_read(inode) - 1, &cached_state, GFP_NOFS);
805
806         if (ret) {
807                 ret = 0;
808                 goto out_free;
809         }
810
811         BTRFS_I(inode)->generation = trans->transid;
812
813         filemap_write_and_wait(inode->i_mapping);
814
815         key.objectid = BTRFS_FREE_SPACE_OBJECTID;
816         key.offset = block_group->key.objectid;
817         key.type = 0;
818
819         ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
820         if (ret < 0) {
821                 ret = 0;
822                 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
823                                  EXTENT_DIRTY | EXTENT_DELALLOC |
824                                  EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
825                 goto out_free;
826         }
827         leaf = path->nodes[0];
828         if (ret > 0) {
829                 struct btrfs_key found_key;
830                 BUG_ON(!path->slots[0]);
831                 path->slots[0]--;
832                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
833                 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
834                     found_key.offset != block_group->key.objectid) {
835                         ret = 0;
836                         clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
837                                          EXTENT_DIRTY | EXTENT_DELALLOC |
838                                          EXTENT_DO_ACCOUNTING, 0, 0, NULL,
839                                          GFP_NOFS);
840                         btrfs_release_path(root, path);
841                         goto out_free;
842                 }
843         }
844         header = btrfs_item_ptr(leaf, path->slots[0],
845                                 struct btrfs_free_space_header);
846         btrfs_set_free_space_entries(leaf, header, entries);
847         btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
848         btrfs_set_free_space_generation(leaf, header, trans->transid);
849         btrfs_mark_buffer_dirty(leaf);
850         btrfs_release_path(root, path);
851
852         ret = 1;
853
854 out_free:
855         if (ret == 0) {
856                 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
857                 spin_lock(&block_group->lock);
858                 block_group->disk_cache_state = BTRFS_DC_ERROR;
859                 spin_unlock(&block_group->lock);
860                 BTRFS_I(inode)->generation = 0;
861         }
862         kfree(checksums);
863         kfree(pages);
864         btrfs_update_inode(trans, root, inode);
865         iput(inode);
866         return ret;
867 }
868
869 static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
870                                           u64 offset)
871 {
872         BUG_ON(offset < bitmap_start);
873         offset -= bitmap_start;
874         return (unsigned long)(div64_u64(offset, sectorsize));
875 }
876
877 static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
878 {
879         return (unsigned long)(div64_u64(bytes, sectorsize));
880 }
881
882 static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
883                                    u64 offset)
884 {
885         u64 bitmap_start;
886         u64 bytes_per_bitmap;
887
888         bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
889         bitmap_start = offset - block_group->key.objectid;
890         bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
891         bitmap_start *= bytes_per_bitmap;
892         bitmap_start += block_group->key.objectid;
893
894         return bitmap_start;
895 }
896
897 static int tree_insert_offset(struct rb_root *root, u64 offset,
898                               struct rb_node *node, int bitmap)
899 {
900         struct rb_node **p = &root->rb_node;
901         struct rb_node *parent = NULL;
902         struct btrfs_free_space *info;
903
904         while (*p) {
905                 parent = *p;
906                 info = rb_entry(parent, struct btrfs_free_space, offset_index);
907
908                 if (offset < info->offset) {
909                         p = &(*p)->rb_left;
910                 } else if (offset > info->offset) {
911                         p = &(*p)->rb_right;
912                 } else {
913                         /*
914                          * we could have a bitmap entry and an extent entry
915                          * share the same offset.  If this is the case, we want
916                          * the extent entry to always be found first if we do a
917                          * linear search through the tree, since we want to have
918                          * the quickest allocation time, and allocating from an
919                          * extent is faster than allocating from a bitmap.  So
920                          * if we're inserting a bitmap and we find an entry at
921                          * this offset, we want to go right, or after this entry
922                          * logically.  If we are inserting an extent and we've
923                          * found a bitmap, we want to go left, or before
924                          * logically.
925                          */
926                         if (bitmap) {
927                                 if (info->bitmap) {
928                                         WARN_ON_ONCE(1);
929                                         return -EEXIST;
930                                 }
931                                 p = &(*p)->rb_right;
932                         } else {
933                                 if (!info->bitmap) {
934                                         WARN_ON_ONCE(1);
935                                         return -EEXIST;
936                                 }
937                                 p = &(*p)->rb_left;
938                         }
939                 }
940         }
941
942         rb_link_node(node, parent, p);
943         rb_insert_color(node, root);
944
945         return 0;
946 }
947
948 /*
949  * searches the tree for the given offset.
950  *
951  * fuzzy - If this is set, then we are trying to make an allocation, and we just
952  * want a section that has at least bytes size and comes at or after the given
953  * offset.
954  */
955 static struct btrfs_free_space *
956 tree_search_offset(struct btrfs_block_group_cache *block_group,
957                    u64 offset, int bitmap_only, int fuzzy)
958 {
959         struct rb_node *n = block_group->free_space_offset.rb_node;
960         struct btrfs_free_space *entry, *prev = NULL;
961
962         /* find entry that is closest to the 'offset' */
963         while (1) {
964                 if (!n) {
965                         entry = NULL;
966                         break;
967                 }
968
969                 entry = rb_entry(n, struct btrfs_free_space, offset_index);
970                 prev = entry;
971
972                 if (offset < entry->offset)
973                         n = n->rb_left;
974                 else if (offset > entry->offset)
975                         n = n->rb_right;
976                 else
977                         break;
978         }
979
980         if (bitmap_only) {
981                 if (!entry)
982                         return NULL;
983                 if (entry->bitmap)
984                         return entry;
985
986                 /*
987                  * bitmap entry and extent entry may share same offset,
988                  * in that case, bitmap entry comes after extent entry.
989                  */
990                 n = rb_next(n);
991                 if (!n)
992                         return NULL;
993                 entry = rb_entry(n, struct btrfs_free_space, offset_index);
994                 if (entry->offset != offset)
995                         return NULL;
996
997                 WARN_ON(!entry->bitmap);
998                 return entry;
999         } else if (entry) {
1000                 if (entry->bitmap) {
1001                         /*
1002                          * if previous extent entry covers the offset,
1003                          * we should return it instead of the bitmap entry
1004                          */
1005                         n = &entry->offset_index;
1006                         while (1) {
1007                                 n = rb_prev(n);
1008                                 if (!n)
1009                                         break;
1010                                 prev = rb_entry(n, struct btrfs_free_space,
1011                                                 offset_index);
1012                                 if (!prev->bitmap) {
1013                                         if (prev->offset + prev->bytes > offset)
1014                                                 entry = prev;
1015                                         break;
1016                                 }
1017                         }
1018                 }
1019                 return entry;
1020         }
1021
1022         if (!prev)
1023                 return NULL;
1024
1025         /* find last entry before the 'offset' */
1026         entry = prev;
1027         if (entry->offset > offset) {
1028                 n = rb_prev(&entry->offset_index);
1029                 if (n) {
1030                         entry = rb_entry(n, struct btrfs_free_space,
1031                                         offset_index);
1032                         BUG_ON(entry->offset > offset);
1033                 } else {
1034                         if (fuzzy)
1035                                 return entry;
1036                         else
1037                                 return NULL;
1038                 }
1039         }
1040
1041         if (entry->bitmap) {
1042                 n = &entry->offset_index;
1043                 while (1) {
1044                         n = rb_prev(n);
1045                         if (!n)
1046                                 break;
1047                         prev = rb_entry(n, struct btrfs_free_space,
1048                                         offset_index);
1049                         if (!prev->bitmap) {
1050                                 if (prev->offset + prev->bytes > offset)
1051                                         return prev;
1052                                 break;
1053                         }
1054                 }
1055                 if (entry->offset + BITS_PER_BITMAP *
1056                     block_group->sectorsize > offset)
1057                         return entry;
1058         } else if (entry->offset + entry->bytes > offset)
1059                 return entry;
1060
1061         if (!fuzzy)
1062                 return NULL;
1063
1064         while (1) {
1065                 if (entry->bitmap) {
1066                         if (entry->offset + BITS_PER_BITMAP *
1067                             block_group->sectorsize > offset)
1068                                 break;
1069                 } else {
1070                         if (entry->offset + entry->bytes > offset)
1071                                 break;
1072                 }
1073
1074                 n = rb_next(&entry->offset_index);
1075                 if (!n)
1076                         return NULL;
1077                 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1078         }
1079         return entry;
1080 }
1081
1082 static inline void
1083 __unlink_free_space(struct btrfs_block_group_cache *block_group,
1084                     struct btrfs_free_space *info)
1085 {
1086         rb_erase(&info->offset_index, &block_group->free_space_offset);
1087         block_group->free_extents--;
1088 }
1089
1090 static void unlink_free_space(struct btrfs_block_group_cache *block_group,
1091                               struct btrfs_free_space *info)
1092 {
1093         __unlink_free_space(block_group, info);
1094         block_group->free_space -= info->bytes;
1095 }
1096
1097 static int link_free_space(struct btrfs_block_group_cache *block_group,
1098                            struct btrfs_free_space *info)
1099 {
1100         int ret = 0;
1101
1102         BUG_ON(!info->bitmap && !info->bytes);
1103         ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
1104                                  &info->offset_index, (info->bitmap != NULL));
1105         if (ret)
1106                 return ret;
1107
1108         block_group->free_space += info->bytes;
1109         block_group->free_extents++;
1110         return ret;
1111 }
1112
1113 static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
1114 {
1115         u64 max_bytes;
1116         u64 bitmap_bytes;
1117         u64 extent_bytes;
1118         u64 size = block_group->key.offset;
1119
1120         /*
1121          * The goal is to keep the total amount of memory used per 1gb of space
1122          * at or below 32k, so we need to adjust how much memory we allow to be
1123          * used by extent based free space tracking
1124          */
1125         if (size < 1024 * 1024 * 1024)
1126                 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1127         else
1128                 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1129                         div64_u64(size, 1024 * 1024 * 1024);
1130
1131         /*
1132          * we want to account for 1 more bitmap than what we have so we can make
1133          * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1134          * we add more bitmaps.
1135          */
1136         bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
1137
1138         if (bitmap_bytes >= max_bytes) {
1139                 block_group->extents_thresh = 0;
1140                 return;
1141         }
1142
1143         /*
1144          * we want the extent entry threshold to always be at most 1/2 the maxw
1145          * bytes we can have, or whatever is less than that.
1146          */
1147         extent_bytes = max_bytes - bitmap_bytes;
1148         extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1149
1150         block_group->extents_thresh =
1151                 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
1152 }
1153
1154 static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
1155                               struct btrfs_free_space *info, u64 offset,
1156                               u64 bytes)
1157 {
1158         unsigned long start, end;
1159         unsigned long i;
1160
1161         start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1162         end = start + bytes_to_bits(bytes, block_group->sectorsize);
1163         BUG_ON(end > BITS_PER_BITMAP);
1164
1165         for (i = start; i < end; i++)
1166                 clear_bit(i, info->bitmap);
1167
1168         info->bytes -= bytes;
1169         block_group->free_space -= bytes;
1170 }
1171
1172 static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
1173                             struct btrfs_free_space *info, u64 offset,
1174                             u64 bytes)
1175 {
1176         unsigned long start, end;
1177         unsigned long i;
1178
1179         start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1180         end = start + bytes_to_bits(bytes, block_group->sectorsize);
1181         BUG_ON(end > BITS_PER_BITMAP);
1182
1183         for (i = start; i < end; i++)
1184                 set_bit(i, info->bitmap);
1185
1186         info->bytes += bytes;
1187         block_group->free_space += bytes;
1188 }
1189
1190 static int search_bitmap(struct btrfs_block_group_cache *block_group,
1191                          struct btrfs_free_space *bitmap_info, u64 *offset,
1192                          u64 *bytes)
1193 {
1194         unsigned long found_bits = 0;
1195         unsigned long bits, i;
1196         unsigned long next_zero;
1197
1198         i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
1199                           max_t(u64, *offset, bitmap_info->offset));
1200         bits = bytes_to_bits(*bytes, block_group->sectorsize);
1201
1202         for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1203              i < BITS_PER_BITMAP;
1204              i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1205                 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1206                                                BITS_PER_BITMAP, i);
1207                 if ((next_zero - i) >= bits) {
1208                         found_bits = next_zero - i;
1209                         break;
1210                 }
1211                 i = next_zero;
1212         }
1213
1214         if (found_bits) {
1215                 *offset = (u64)(i * block_group->sectorsize) +
1216                         bitmap_info->offset;
1217                 *bytes = (u64)(found_bits) * block_group->sectorsize;
1218                 return 0;
1219         }
1220
1221         return -1;
1222 }
1223
1224 static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
1225                                                 *block_group, u64 *offset,
1226                                                 u64 *bytes, int debug)
1227 {
1228         struct btrfs_free_space *entry;
1229         struct rb_node *node;
1230         int ret;
1231
1232         if (!block_group->free_space_offset.rb_node)
1233                 return NULL;
1234
1235         entry = tree_search_offset(block_group,
1236                                    offset_to_bitmap(block_group, *offset),
1237                                    0, 1);
1238         if (!entry)
1239                 return NULL;
1240
1241         for (node = &entry->offset_index; node; node = rb_next(node)) {
1242                 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1243                 if (entry->bytes < *bytes)
1244                         continue;
1245
1246                 if (entry->bitmap) {
1247                         ret = search_bitmap(block_group, entry, offset, bytes);
1248                         if (!ret)
1249                                 return entry;
1250                         continue;
1251                 }
1252
1253                 *offset = entry->offset;
1254                 *bytes = entry->bytes;
1255                 return entry;
1256         }
1257
1258         return NULL;
1259 }
1260
1261 static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
1262                            struct btrfs_free_space *info, u64 offset)
1263 {
1264         u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1265         int max_bitmaps = (int)div64_u64(block_group->key.offset +
1266                                          bytes_per_bg - 1, bytes_per_bg);
1267         BUG_ON(block_group->total_bitmaps >= max_bitmaps);
1268
1269         info->offset = offset_to_bitmap(block_group, offset);
1270         info->bytes = 0;
1271         link_free_space(block_group, info);
1272         block_group->total_bitmaps++;
1273
1274         recalculate_thresholds(block_group);
1275 }
1276
1277 static void free_bitmap(struct btrfs_block_group_cache *block_group,
1278                         struct btrfs_free_space *bitmap_info)
1279 {
1280         unlink_free_space(block_group, bitmap_info);
1281         kfree(bitmap_info->bitmap);
1282         kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1283         block_group->total_bitmaps--;
1284         recalculate_thresholds(block_group);
1285 }
1286
1287 static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
1288                               struct btrfs_free_space *bitmap_info,
1289                               u64 *offset, u64 *bytes)
1290 {
1291         u64 end;
1292         u64 search_start, search_bytes;
1293         int ret;
1294
1295 again:
1296         end = bitmap_info->offset +
1297                 (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
1298
1299         /*
1300          * XXX - this can go away after a few releases.
1301          *
1302          * since the only user of btrfs_remove_free_space is the tree logging
1303          * stuff, and the only way to test that is under crash conditions, we
1304          * want to have this debug stuff here just in case somethings not
1305          * working.  Search the bitmap for the space we are trying to use to
1306          * make sure its actually there.  If its not there then we need to stop
1307          * because something has gone wrong.
1308          */
1309         search_start = *offset;
1310         search_bytes = *bytes;
1311         search_bytes = min(search_bytes, end - search_start + 1);
1312         ret = search_bitmap(block_group, bitmap_info, &search_start,
1313                             &search_bytes);
1314         BUG_ON(ret < 0 || search_start != *offset);
1315
1316         if (*offset > bitmap_info->offset && *offset + *bytes > end) {
1317                 bitmap_clear_bits(block_group, bitmap_info, *offset,
1318                                   end - *offset + 1);
1319                 *bytes -= end - *offset + 1;
1320                 *offset = end + 1;
1321         } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
1322                 bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
1323                 *bytes = 0;
1324         }
1325
1326         if (*bytes) {
1327                 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1328                 if (!bitmap_info->bytes)
1329                         free_bitmap(block_group, bitmap_info);
1330
1331                 /*
1332                  * no entry after this bitmap, but we still have bytes to
1333                  * remove, so something has gone wrong.
1334                  */
1335                 if (!next)
1336                         return -EINVAL;
1337
1338                 bitmap_info = rb_entry(next, struct btrfs_free_space,
1339                                        offset_index);
1340
1341                 /*
1342                  * if the next entry isn't a bitmap we need to return to let the
1343                  * extent stuff do its work.
1344                  */
1345                 if (!bitmap_info->bitmap)
1346                         return -EAGAIN;
1347
1348                 /*
1349                  * Ok the next item is a bitmap, but it may not actually hold
1350                  * the information for the rest of this free space stuff, so
1351                  * look for it, and if we don't find it return so we can try
1352                  * everything over again.
1353                  */
1354                 search_start = *offset;
1355                 search_bytes = *bytes;
1356                 ret = search_bitmap(block_group, bitmap_info, &search_start,
1357                                     &search_bytes);
1358                 if (ret < 0 || search_start != *offset)
1359                         return -EAGAIN;
1360
1361                 goto again;
1362         } else if (!bitmap_info->bytes)
1363                 free_bitmap(block_group, bitmap_info);
1364
1365         return 0;
1366 }
1367
1368 static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
1369                               struct btrfs_free_space *info)
1370 {
1371         struct btrfs_free_space *bitmap_info;
1372         int added = 0;
1373         u64 bytes, offset, end;
1374         int ret;
1375
1376         /*
1377          * If we are below the extents threshold then we can add this as an
1378          * extent, and don't have to deal with the bitmap
1379          */
1380         if (block_group->free_extents < block_group->extents_thresh) {
1381                 /*
1382                  * If this block group has some small extents we don't want to
1383                  * use up all of our free slots in the cache with them, we want
1384                  * to reserve them to larger extents, however if we have plent
1385                  * of cache left then go ahead an dadd them, no sense in adding
1386                  * the overhead of a bitmap if we don't have to.
1387                  */
1388                 if (info->bytes <= block_group->sectorsize * 4) {
1389                         if (block_group->free_extents * 2 <=
1390                             block_group->extents_thresh)
1391                                 return 0;
1392                 } else {
1393                         return 0;
1394                 }
1395         }
1396
1397         /*
1398          * some block groups are so tiny they can't be enveloped by a bitmap, so
1399          * don't even bother to create a bitmap for this
1400          */
1401         if (BITS_PER_BITMAP * block_group->sectorsize >
1402             block_group->key.offset)
1403                 return 0;
1404
1405         bytes = info->bytes;
1406         offset = info->offset;
1407
1408 again:
1409         bitmap_info = tree_search_offset(block_group,
1410                                          offset_to_bitmap(block_group, offset),
1411                                          1, 0);
1412         if (!bitmap_info) {
1413                 BUG_ON(added);
1414                 goto new_bitmap;
1415         }
1416
1417         end = bitmap_info->offset +
1418                 (u64)(BITS_PER_BITMAP * block_group->sectorsize);
1419
1420         if (offset >= bitmap_info->offset && offset + bytes > end) {
1421                 bitmap_set_bits(block_group, bitmap_info, offset,
1422                                 end - offset);
1423                 bytes -= end - offset;
1424                 offset = end;
1425                 added = 0;
1426         } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
1427                 bitmap_set_bits(block_group, bitmap_info, offset, bytes);
1428                 bytes = 0;
1429         } else {
1430                 BUG();
1431         }
1432
1433         if (!bytes) {
1434                 ret = 1;
1435                 goto out;
1436         } else
1437                 goto again;
1438
1439 new_bitmap:
1440         if (info && info->bitmap) {
1441                 add_new_bitmap(block_group, info, offset);
1442                 added = 1;
1443                 info = NULL;
1444                 goto again;
1445         } else {
1446                 spin_unlock(&block_group->tree_lock);
1447
1448                 /* no pre-allocated info, allocate a new one */
1449                 if (!info) {
1450                         info = kmem_cache_zalloc(btrfs_free_space_cachep,
1451                                                  GFP_NOFS);
1452                         if (!info) {
1453                                 spin_lock(&block_group->tree_lock);
1454                                 ret = -ENOMEM;
1455                                 goto out;
1456                         }
1457                 }
1458
1459                 /* allocate the bitmap */
1460                 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
1461                 spin_lock(&block_group->tree_lock);
1462                 if (!info->bitmap) {
1463                         ret = -ENOMEM;
1464                         goto out;
1465                 }
1466                 goto again;
1467         }
1468
1469 out:
1470         if (info) {
1471                 if (info->bitmap)
1472                         kfree(info->bitmap);
1473                 kmem_cache_free(btrfs_free_space_cachep, info);
1474         }
1475
1476         return ret;
1477 }
1478
1479 bool try_merge_free_space(struct btrfs_block_group_cache *block_group,
1480                           struct btrfs_free_space *info, bool update_stat)
1481 {
1482         struct btrfs_free_space *left_info;
1483         struct btrfs_free_space *right_info;
1484         bool merged = false;
1485         u64 offset = info->offset;
1486         u64 bytes = info->bytes;
1487
1488         /*
1489          * first we want to see if there is free space adjacent to the range we
1490          * are adding, if there is remove that struct and add a new one to
1491          * cover the entire range
1492          */
1493         right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
1494         if (right_info && rb_prev(&right_info->offset_index))
1495                 left_info = rb_entry(rb_prev(&right_info->offset_index),
1496                                      struct btrfs_free_space, offset_index);
1497         else
1498                 left_info = tree_search_offset(block_group, offset - 1, 0, 0);
1499
1500         if (right_info && !right_info->bitmap) {
1501                 if (update_stat)
1502                         unlink_free_space(block_group, right_info);
1503                 else
1504                         __unlink_free_space(block_group, right_info);
1505                 info->bytes += right_info->bytes;
1506                 kmem_cache_free(btrfs_free_space_cachep, right_info);
1507                 merged = true;
1508         }
1509
1510         if (left_info && !left_info->bitmap &&
1511             left_info->offset + left_info->bytes == offset) {
1512                 if (update_stat)
1513                         unlink_free_space(block_group, left_info);
1514                 else
1515                         __unlink_free_space(block_group, left_info);
1516                 info->offset = left_info->offset;
1517                 info->bytes += left_info->bytes;
1518                 kmem_cache_free(btrfs_free_space_cachep, left_info);
1519                 merged = true;
1520         }
1521
1522         return merged;
1523 }
1524
1525 int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
1526                          u64 offset, u64 bytes)
1527 {
1528         struct btrfs_free_space *info;
1529         int ret = 0;
1530
1531         info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
1532         if (!info)
1533                 return -ENOMEM;
1534
1535         info->offset = offset;
1536         info->bytes = bytes;
1537
1538         spin_lock(&block_group->tree_lock);
1539
1540         if (try_merge_free_space(block_group, info, true))
1541                 goto link;
1542
1543         /*
1544          * There was no extent directly to the left or right of this new
1545          * extent then we know we're going to have to allocate a new extent, so
1546          * before we do that see if we need to drop this into a bitmap
1547          */
1548         ret = insert_into_bitmap(block_group, info);
1549         if (ret < 0) {
1550                 goto out;
1551         } else if (ret) {
1552                 ret = 0;
1553                 goto out;
1554         }
1555 link:
1556         ret = link_free_space(block_group, info);
1557         if (ret)
1558                 kmem_cache_free(btrfs_free_space_cachep, info);
1559 out:
1560         spin_unlock(&block_group->tree_lock);
1561
1562         if (ret) {
1563                 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
1564                 BUG_ON(ret == -EEXIST);
1565         }
1566
1567         return ret;
1568 }
1569
1570 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1571                             u64 offset, u64 bytes)
1572 {
1573         struct btrfs_free_space *info;
1574         struct btrfs_free_space *next_info = NULL;
1575         int ret = 0;
1576
1577         spin_lock(&block_group->tree_lock);
1578
1579 again:
1580         info = tree_search_offset(block_group, offset, 0, 0);
1581         if (!info) {
1582                 /*
1583                  * oops didn't find an extent that matched the space we wanted
1584                  * to remove, look for a bitmap instead
1585                  */
1586                 info = tree_search_offset(block_group,
1587                                           offset_to_bitmap(block_group, offset),
1588                                           1, 0);
1589                 if (!info) {
1590                         WARN_ON(1);
1591                         goto out_lock;
1592                 }
1593         }
1594
1595         if (info->bytes < bytes && rb_next(&info->offset_index)) {
1596                 u64 end;
1597                 next_info = rb_entry(rb_next(&info->offset_index),
1598                                              struct btrfs_free_space,
1599                                              offset_index);
1600
1601                 if (next_info->bitmap)
1602                         end = next_info->offset + BITS_PER_BITMAP *
1603                                 block_group->sectorsize - 1;
1604                 else
1605                         end = next_info->offset + next_info->bytes;
1606
1607                 if (next_info->bytes < bytes ||
1608                     next_info->offset > offset || offset > end) {
1609                         printk(KERN_CRIT "Found free space at %llu, size %llu,"
1610                               " trying to use %llu\n",
1611                               (unsigned long long)info->offset,
1612                               (unsigned long long)info->bytes,
1613                               (unsigned long long)bytes);
1614                         WARN_ON(1);
1615                         ret = -EINVAL;
1616                         goto out_lock;
1617                 }
1618
1619                 info = next_info;
1620         }
1621
1622         if (info->bytes == bytes) {
1623                 unlink_free_space(block_group, info);
1624                 if (info->bitmap) {
1625                         kfree(info->bitmap);
1626                         block_group->total_bitmaps--;
1627                 }
1628                 kmem_cache_free(btrfs_free_space_cachep, info);
1629                 goto out_lock;
1630         }
1631
1632         if (!info->bitmap && info->offset == offset) {
1633                 unlink_free_space(block_group, info);
1634                 info->offset += bytes;
1635                 info->bytes -= bytes;
1636                 link_free_space(block_group, info);
1637                 goto out_lock;
1638         }
1639
1640         if (!info->bitmap && info->offset <= offset &&
1641             info->offset + info->bytes >= offset + bytes) {
1642                 u64 old_start = info->offset;
1643                 /*
1644                  * we're freeing space in the middle of the info,
1645                  * this can happen during tree log replay
1646                  *
1647                  * first unlink the old info and then
1648                  * insert it again after the hole we're creating
1649                  */
1650                 unlink_free_space(block_group, info);
1651                 if (offset + bytes < info->offset + info->bytes) {
1652                         u64 old_end = info->offset + info->bytes;
1653
1654                         info->offset = offset + bytes;
1655                         info->bytes = old_end - info->offset;
1656                         ret = link_free_space(block_group, info);
1657                         WARN_ON(ret);
1658                         if (ret)
1659                                 goto out_lock;
1660                 } else {
1661                         /* the hole we're creating ends at the end
1662                          * of the info struct, just free the info
1663                          */
1664                         kmem_cache_free(btrfs_free_space_cachep, info);
1665                 }
1666                 spin_unlock(&block_group->tree_lock);
1667
1668                 /* step two, insert a new info struct to cover
1669                  * anything before the hole
1670                  */
1671                 ret = btrfs_add_free_space(block_group, old_start,
1672                                            offset - old_start);
1673                 WARN_ON(ret);
1674                 goto out;
1675         }
1676
1677         ret = remove_from_bitmap(block_group, info, &offset, &bytes);
1678         if (ret == -EAGAIN)
1679                 goto again;
1680         BUG_ON(ret);
1681 out_lock:
1682         spin_unlock(&block_group->tree_lock);
1683 out:
1684         return ret;
1685 }
1686
1687 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1688                            u64 bytes)
1689 {
1690         struct btrfs_free_space *info;
1691         struct rb_node *n;
1692         int count = 0;
1693
1694         for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
1695                 info = rb_entry(n, struct btrfs_free_space, offset_index);
1696                 if (info->bytes >= bytes)
1697                         count++;
1698                 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
1699                        (unsigned long long)info->offset,
1700                        (unsigned long long)info->bytes,
1701                        (info->bitmap) ? "yes" : "no");
1702         }
1703         printk(KERN_INFO "block group has cluster?: %s\n",
1704                list_empty(&block_group->cluster_list) ? "no" : "yes");
1705         printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1706                "\n", count);
1707 }
1708
1709 u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
1710 {
1711         struct btrfs_free_space *info;
1712         struct rb_node *n;
1713         u64 ret = 0;
1714
1715         for (n = rb_first(&block_group->free_space_offset); n;
1716              n = rb_next(n)) {
1717                 info = rb_entry(n, struct btrfs_free_space, offset_index);
1718                 ret += info->bytes;
1719         }
1720
1721         return ret;
1722 }
1723
1724 /*
1725  * for a given cluster, put all of its extents back into the free
1726  * space cache.  If the block group passed doesn't match the block group
1727  * pointed to by the cluster, someone else raced in and freed the
1728  * cluster already.  In that case, we just return without changing anything
1729  */
1730 static int
1731 __btrfs_return_cluster_to_free_space(
1732                              struct btrfs_block_group_cache *block_group,
1733                              struct btrfs_free_cluster *cluster)
1734 {
1735         struct btrfs_free_space *entry;
1736         struct rb_node *node;
1737
1738         spin_lock(&cluster->lock);
1739         if (cluster->block_group != block_group)
1740                 goto out;
1741
1742         cluster->block_group = NULL;
1743         cluster->window_start = 0;
1744         list_del_init(&cluster->block_group_list);
1745
1746         node = rb_first(&cluster->root);
1747         while (node) {
1748                 bool bitmap;
1749
1750                 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1751                 node = rb_next(&entry->offset_index);
1752                 rb_erase(&entry->offset_index, &cluster->root);
1753
1754                 bitmap = (entry->bitmap != NULL);
1755                 if (!bitmap)
1756                         try_merge_free_space(block_group, entry, false);
1757                 tree_insert_offset(&block_group->free_space_offset,
1758                                    entry->offset, &entry->offset_index, bitmap);
1759         }
1760         cluster->root = RB_ROOT;
1761
1762 out:
1763         spin_unlock(&cluster->lock);
1764         btrfs_put_block_group(block_group);
1765         return 0;
1766 }
1767
1768 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1769 {
1770         struct btrfs_free_space *info;
1771         struct rb_node *node;
1772         struct btrfs_free_cluster *cluster;
1773         struct list_head *head;
1774
1775         spin_lock(&block_group->tree_lock);
1776         while ((head = block_group->cluster_list.next) !=
1777                &block_group->cluster_list) {
1778                 cluster = list_entry(head, struct btrfs_free_cluster,
1779                                      block_group_list);
1780
1781                 WARN_ON(cluster->block_group != block_group);
1782                 __btrfs_return_cluster_to_free_space(block_group, cluster);
1783                 if (need_resched()) {
1784                         spin_unlock(&block_group->tree_lock);
1785                         cond_resched();
1786                         spin_lock(&block_group->tree_lock);
1787                 }
1788         }
1789
1790         while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
1791                 info = rb_entry(node, struct btrfs_free_space, offset_index);
1792                 if (!info->bitmap) {
1793                         unlink_free_space(block_group, info);
1794                         kmem_cache_free(btrfs_free_space_cachep, info);
1795                 } else {
1796                         free_bitmap(block_group, info);
1797                 }
1798
1799                 if (need_resched()) {
1800                         spin_unlock(&block_group->tree_lock);
1801                         cond_resched();
1802                         spin_lock(&block_group->tree_lock);
1803                 }
1804         }
1805
1806         spin_unlock(&block_group->tree_lock);
1807 }
1808
1809 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1810                                u64 offset, u64 bytes, u64 empty_size)
1811 {
1812         struct btrfs_free_space *entry = NULL;
1813         u64 bytes_search = bytes + empty_size;
1814         u64 ret = 0;
1815
1816         spin_lock(&block_group->tree_lock);
1817         entry = find_free_space(block_group, &offset, &bytes_search, 0);
1818         if (!entry)
1819                 goto out;
1820
1821         ret = offset;
1822         if (entry->bitmap) {
1823                 bitmap_clear_bits(block_group, entry, offset, bytes);
1824                 if (!entry->bytes)
1825                         free_bitmap(block_group, entry);
1826         } else {
1827                 unlink_free_space(block_group, entry);
1828                 entry->offset += bytes;
1829                 entry->bytes -= bytes;
1830                 if (!entry->bytes)
1831                         kmem_cache_free(btrfs_free_space_cachep, entry);
1832                 else
1833                         link_free_space(block_group, entry);
1834         }
1835
1836 out:
1837         spin_unlock(&block_group->tree_lock);
1838
1839         return ret;
1840 }
1841
1842 /*
1843  * given a cluster, put all of its extents back into the free space
1844  * cache.  If a block group is passed, this function will only free
1845  * a cluster that belongs to the passed block group.
1846  *
1847  * Otherwise, it'll get a reference on the block group pointed to by the
1848  * cluster and remove the cluster from it.
1849  */
1850 int btrfs_return_cluster_to_free_space(
1851                                struct btrfs_block_group_cache *block_group,
1852                                struct btrfs_free_cluster *cluster)
1853 {
1854         int ret;
1855
1856         /* first, get a safe pointer to the block group */
1857         spin_lock(&cluster->lock);
1858         if (!block_group) {
1859                 block_group = cluster->block_group;
1860                 if (!block_group) {
1861                         spin_unlock(&cluster->lock);
1862                         return 0;
1863                 }
1864         } else if (cluster->block_group != block_group) {
1865                 /* someone else has already freed it don't redo their work */
1866                 spin_unlock(&cluster->lock);
1867                 return 0;
1868         }
1869         atomic_inc(&block_group->count);
1870         spin_unlock(&cluster->lock);
1871
1872         /* now return any extents the cluster had on it */
1873         spin_lock(&block_group->tree_lock);
1874         ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
1875         spin_unlock(&block_group->tree_lock);
1876
1877         /* finally drop our ref */
1878         btrfs_put_block_group(block_group);
1879         return ret;
1880 }
1881
1882 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1883                                    struct btrfs_free_cluster *cluster,
1884                                    struct btrfs_free_space *entry,
1885                                    u64 bytes, u64 min_start)
1886 {
1887         int err;
1888         u64 search_start = cluster->window_start;
1889         u64 search_bytes = bytes;
1890         u64 ret = 0;
1891
1892         search_start = min_start;
1893         search_bytes = bytes;
1894
1895         err = search_bitmap(block_group, entry, &search_start,
1896                             &search_bytes);
1897         if (err)
1898                 return 0;
1899
1900         ret = search_start;
1901         bitmap_clear_bits(block_group, entry, ret, bytes);
1902
1903         return ret;
1904 }
1905
1906 /*
1907  * given a cluster, try to allocate 'bytes' from it, returns 0
1908  * if it couldn't find anything suitably large, or a logical disk offset
1909  * if things worked out
1910  */
1911 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1912                              struct btrfs_free_cluster *cluster, u64 bytes,
1913                              u64 min_start)
1914 {
1915         struct btrfs_free_space *entry = NULL;
1916         struct rb_node *node;
1917         u64 ret = 0;
1918
1919         spin_lock(&cluster->lock);
1920         if (bytes > cluster->max_size)
1921                 goto out;
1922
1923         if (cluster->block_group != block_group)
1924                 goto out;
1925
1926         node = rb_first(&cluster->root);
1927         if (!node)
1928                 goto out;
1929
1930         entry = rb_entry(node, struct btrfs_free_space, offset_index);
1931         while(1) {
1932                 if (entry->bytes < bytes ||
1933                     (!entry->bitmap && entry->offset < min_start)) {
1934                         struct rb_node *node;
1935
1936                         node = rb_next(&entry->offset_index);
1937                         if (!node)
1938                                 break;
1939                         entry = rb_entry(node, struct btrfs_free_space,
1940                                          offset_index);
1941                         continue;
1942                 }
1943
1944                 if (entry->bitmap) {
1945                         ret = btrfs_alloc_from_bitmap(block_group,
1946                                                       cluster, entry, bytes,
1947                                                       min_start);
1948                         if (ret == 0) {
1949                                 struct rb_node *node;
1950                                 node = rb_next(&entry->offset_index);
1951                                 if (!node)
1952                                         break;
1953                                 entry = rb_entry(node, struct btrfs_free_space,
1954                                                  offset_index);
1955                                 continue;
1956                         }
1957                 } else {
1958
1959                         ret = entry->offset;
1960
1961                         entry->offset += bytes;
1962                         entry->bytes -= bytes;
1963                 }
1964
1965                 if (entry->bytes == 0)
1966                         rb_erase(&entry->offset_index, &cluster->root);
1967                 break;
1968         }
1969 out:
1970         spin_unlock(&cluster->lock);
1971
1972         if (!ret)
1973                 return 0;
1974
1975         spin_lock(&block_group->tree_lock);
1976
1977         block_group->free_space -= bytes;
1978         if (entry->bytes == 0) {
1979                 block_group->free_extents--;
1980                 if (entry->bitmap) {
1981                         kfree(entry->bitmap);
1982                         block_group->total_bitmaps--;
1983                         recalculate_thresholds(block_group);
1984                 }
1985                 kmem_cache_free(btrfs_free_space_cachep, entry);
1986         }
1987
1988         spin_unlock(&block_group->tree_lock);
1989
1990         return ret;
1991 }
1992
1993 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1994                                 struct btrfs_free_space *entry,
1995                                 struct btrfs_free_cluster *cluster,
1996                                 u64 offset, u64 bytes, u64 min_bytes)
1997 {
1998         unsigned long next_zero;
1999         unsigned long i;
2000         unsigned long search_bits;
2001         unsigned long total_bits;
2002         unsigned long found_bits;
2003         unsigned long start = 0;
2004         unsigned long total_found = 0;
2005         int ret;
2006         bool found = false;
2007
2008         i = offset_to_bit(entry->offset, block_group->sectorsize,
2009                           max_t(u64, offset, entry->offset));
2010         search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2011         total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
2012
2013 again:
2014         found_bits = 0;
2015         for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2016              i < BITS_PER_BITMAP;
2017              i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2018                 next_zero = find_next_zero_bit(entry->bitmap,
2019                                                BITS_PER_BITMAP, i);
2020                 if (next_zero - i >= search_bits) {
2021                         found_bits = next_zero - i;
2022                         break;
2023                 }
2024                 i = next_zero;
2025         }
2026
2027         if (!found_bits)
2028                 return -ENOSPC;
2029
2030         if (!found) {
2031                 start = i;
2032                 found = true;
2033         }
2034
2035         total_found += found_bits;
2036
2037         if (cluster->max_size < found_bits * block_group->sectorsize)
2038                 cluster->max_size = found_bits * block_group->sectorsize;
2039
2040         if (total_found < total_bits) {
2041                 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2042                 if (i - start > total_bits * 2) {
2043                         total_found = 0;
2044                         cluster->max_size = 0;
2045                         found = false;
2046                 }
2047                 goto again;
2048         }
2049
2050         cluster->window_start = start * block_group->sectorsize +
2051                 entry->offset;
2052         rb_erase(&entry->offset_index, &block_group->free_space_offset);
2053         ret = tree_insert_offset(&cluster->root, entry->offset,
2054                                  &entry->offset_index, 1);
2055         BUG_ON(ret);
2056
2057         return 0;
2058 }
2059
2060 /*
2061  * This searches the block group for just extents to fill the cluster with.
2062  */
2063 static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2064                                    struct btrfs_free_cluster *cluster,
2065                                    u64 offset, u64 bytes, u64 min_bytes)
2066 {
2067         struct btrfs_free_space *first = NULL;
2068         struct btrfs_free_space *entry = NULL;
2069         struct btrfs_free_space *prev = NULL;
2070         struct btrfs_free_space *last;
2071         struct rb_node *node;
2072         u64 window_start;
2073         u64 window_free;
2074         u64 max_extent;
2075         u64 max_gap = 128 * 1024;
2076
2077         entry = tree_search_offset(block_group, offset, 0, 1);
2078         if (!entry)
2079                 return -ENOSPC;
2080
2081         /*
2082          * We don't want bitmaps, so just move along until we find a normal
2083          * extent entry.
2084          */
2085         while (entry->bitmap) {
2086                 node = rb_next(&entry->offset_index);
2087                 if (!node)
2088                         return -ENOSPC;
2089                 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2090         }
2091
2092         window_start = entry->offset;
2093         window_free = entry->bytes;
2094         max_extent = entry->bytes;
2095         first = entry;
2096         last = entry;
2097         prev = entry;
2098
2099         while (window_free <= min_bytes) {
2100                 node = rb_next(&entry->offset_index);
2101                 if (!node)
2102                         return -ENOSPC;
2103                 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2104
2105                 if (entry->bitmap)
2106                         continue;
2107                 /*
2108                  * we haven't filled the empty size and the window is
2109                  * very large.  reset and try again
2110                  */
2111                 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2112                     entry->offset - window_start > (min_bytes * 2)) {
2113                         first = entry;
2114                         window_start = entry->offset;
2115                         window_free = entry->bytes;
2116                         last = entry;
2117                         max_extent = entry->bytes;
2118                 } else {
2119                         last = entry;
2120                         window_free += entry->bytes;
2121                         if (entry->bytes > max_extent)
2122                                 max_extent = entry->bytes;
2123                 }
2124                 prev = entry;
2125         }
2126
2127         cluster->window_start = first->offset;
2128
2129         node = &first->offset_index;
2130
2131         /*
2132          * now we've found our entries, pull them out of the free space
2133          * cache and put them into the cluster rbtree
2134          */
2135         do {
2136                 int ret;
2137
2138                 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2139                 node = rb_next(&entry->offset_index);
2140                 if (entry->bitmap)
2141                         continue;
2142
2143                 rb_erase(&entry->offset_index, &block_group->free_space_offset);
2144                 ret = tree_insert_offset(&cluster->root, entry->offset,
2145                                          &entry->offset_index, 0);
2146                 BUG_ON(ret);
2147         } while (node && entry != last);
2148
2149         cluster->max_size = max_extent;
2150
2151         return 0;
2152 }
2153
2154 /*
2155  * This specifically looks for bitmaps that may work in the cluster, we assume
2156  * that we have already failed to find extents that will work.
2157  */
2158 static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2159                                 struct btrfs_free_cluster *cluster,
2160                                 u64 offset, u64 bytes, u64 min_bytes)
2161 {
2162         struct btrfs_free_space *entry;
2163         struct rb_node *node;
2164         int ret = -ENOSPC;
2165
2166         if (block_group->total_bitmaps == 0)
2167                 return -ENOSPC;
2168
2169         entry = tree_search_offset(block_group,
2170                                    offset_to_bitmap(block_group, offset),
2171                                    0, 1);
2172         if (!entry)
2173                 return -ENOSPC;
2174
2175         node = &entry->offset_index;
2176         do {
2177                 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2178                 node = rb_next(&entry->offset_index);
2179                 if (!entry->bitmap)
2180                         continue;
2181                 if (entry->bytes < min_bytes)
2182                         continue;
2183                 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2184                                            bytes, min_bytes);
2185         } while (ret && node);
2186
2187         return ret;
2188 }
2189
2190 /*
2191  * here we try to find a cluster of blocks in a block group.  The goal
2192  * is to find at least bytes free and up to empty_size + bytes free.
2193  * We might not find them all in one contiguous area.
2194  *
2195  * returns zero and sets up cluster if things worked out, otherwise
2196  * it returns -enospc
2197  */
2198 int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2199                              struct btrfs_root *root,
2200                              struct btrfs_block_group_cache *block_group,
2201                              struct btrfs_free_cluster *cluster,
2202                              u64 offset, u64 bytes, u64 empty_size)
2203 {
2204         u64 min_bytes;
2205         int ret;
2206
2207         /* for metadata, allow allocates with more holes */
2208         if (btrfs_test_opt(root, SSD_SPREAD)) {
2209                 min_bytes = bytes + empty_size;
2210         } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
2211                 /*
2212                  * we want to do larger allocations when we are
2213                  * flushing out the delayed refs, it helps prevent
2214                  * making more work as we go along.
2215                  */
2216                 if (trans->transaction->delayed_refs.flushing)
2217                         min_bytes = max(bytes, (bytes + empty_size) >> 1);
2218                 else
2219                         min_bytes = max(bytes, (bytes + empty_size) >> 4);
2220         } else
2221                 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2222
2223         spin_lock(&block_group->tree_lock);
2224
2225         /*
2226          * If we know we don't have enough space to make a cluster don't even
2227          * bother doing all the work to try and find one.
2228          */
2229         if (block_group->free_space < min_bytes) {
2230                 spin_unlock(&block_group->tree_lock);
2231                 return -ENOSPC;
2232         }
2233
2234         spin_lock(&cluster->lock);
2235
2236         /* someone already found a cluster, hooray */
2237         if (cluster->block_group) {
2238                 ret = 0;
2239                 goto out;
2240         }
2241
2242         ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes,
2243                                       min_bytes);
2244         if (ret)
2245                 ret = setup_cluster_bitmap(block_group, cluster, offset,
2246                                            bytes, min_bytes);
2247
2248         if (!ret) {
2249                 atomic_inc(&block_group->count);
2250                 list_add_tail(&cluster->block_group_list,
2251                               &block_group->cluster_list);
2252                 cluster->block_group = block_group;
2253         }
2254 out:
2255         spin_unlock(&cluster->lock);
2256         spin_unlock(&block_group->tree_lock);
2257
2258         return ret;
2259 }
2260
2261 /*
2262  * simple code to zero out a cluster
2263  */
2264 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2265 {
2266         spin_lock_init(&cluster->lock);
2267         spin_lock_init(&cluster->refill_lock);
2268         cluster->root = RB_ROOT;
2269         cluster->max_size = 0;
2270         INIT_LIST_HEAD(&cluster->block_group_list);
2271         cluster->block_group = NULL;
2272 }
2273
2274 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2275                            u64 *trimmed, u64 start, u64 end, u64 minlen)
2276 {
2277         struct btrfs_free_space *entry = NULL;
2278         struct btrfs_fs_info *fs_info = block_group->fs_info;
2279         u64 bytes = 0;
2280         u64 actually_trimmed;
2281         int ret = 0;
2282
2283         *trimmed = 0;
2284
2285         while (start < end) {
2286                 spin_lock(&block_group->tree_lock);
2287
2288                 if (block_group->free_space < minlen) {
2289                         spin_unlock(&block_group->tree_lock);
2290                         break;
2291                 }
2292
2293                 entry = tree_search_offset(block_group, start, 0, 1);
2294                 if (!entry)
2295                         entry = tree_search_offset(block_group,
2296                                                    offset_to_bitmap(block_group,
2297                                                                     start),
2298                                                    1, 1);
2299
2300                 if (!entry || entry->offset >= end) {
2301                         spin_unlock(&block_group->tree_lock);
2302                         break;
2303                 }
2304
2305                 if (entry->bitmap) {
2306                         ret = search_bitmap(block_group, entry, &start, &bytes);
2307                         if (!ret) {
2308                                 if (start >= end) {
2309                                         spin_unlock(&block_group->tree_lock);
2310                                         break;
2311                                 }
2312                                 bytes = min(bytes, end - start);
2313                                 bitmap_clear_bits(block_group, entry,
2314                                                   start, bytes);
2315                                 if (entry->bytes == 0)
2316                                         free_bitmap(block_group, entry);
2317                         } else {
2318                                 start = entry->offset + BITS_PER_BITMAP *
2319                                         block_group->sectorsize;
2320                                 spin_unlock(&block_group->tree_lock);
2321                                 ret = 0;
2322                                 continue;
2323                         }
2324                 } else {
2325                         start = entry->offset;
2326                         bytes = min(entry->bytes, end - start);
2327                         unlink_free_space(block_group, entry);
2328                         kmem_cache_free(btrfs_free_space_cachep, entry);
2329                 }
2330
2331                 spin_unlock(&block_group->tree_lock);
2332
2333                 if (bytes >= minlen) {
2334                         int update_ret;
2335                         update_ret = btrfs_update_reserved_bytes(block_group,
2336                                                                  bytes, 1, 1);
2337
2338                         ret = btrfs_error_discard_extent(fs_info->extent_root,
2339                                                          start,
2340                                                          bytes,
2341                                                          &actually_trimmed);
2342
2343                         btrfs_add_free_space(block_group,
2344                                              start, bytes);
2345                         if (!update_ret)
2346                                 btrfs_update_reserved_bytes(block_group,
2347                                                             bytes, 0, 1);
2348
2349                         if (ret)
2350                                 break;
2351                         *trimmed += actually_trimmed;
2352                 }
2353                 start += bytes;
2354                 bytes = 0;
2355
2356                 if (fatal_signal_pending(current)) {
2357                         ret = -ERESTARTSYS;
2358                         break;
2359                 }
2360
2361                 cond_resched();
2362         }
2363
2364         return ret;
2365 }