blob: 83532a2459473b42ba2d7d763cd0ea382f269295 [file] [log] [blame]
Josef Bacik0f9dd462008-09-23 13:14:11 -04001/*
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
Josef Bacik96303082009-07-13 21:29:25 -040019#include <linux/pagemap.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040022#include <linux/math64.h>
Josef Bacik6ab60602011-08-08 08:24:46 -040023#include <linux/ratelimit.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040024#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040025#include "free-space-cache.h"
26#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040027#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000028#include "extent_io.h"
Li Zefan581bb052011-04-20 10:06:11 +080029#include "inode-map.h"
Filipe Manana04216822014-11-27 21:14:15 +000030#include "volumes.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040031
Josef Bacik96303082009-07-13 21:29:25 -040032#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
33#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
34
Filipe Manana55507ce2014-12-01 17:04:09 +000035struct btrfs_trim_range {
36 u64 start;
37 u64 bytes;
38 struct list_head list;
39};
40
Li Zefan34d52cb2011-03-29 13:46:06 +080041static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040042 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040043static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
44 struct btrfs_free_space *info);
Josef Bacik0cb59c92010-07-02 12:14:14 -040045
Li Zefan0414efa2011-04-20 10:20:14 +080046static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
47 struct btrfs_path *path,
48 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040049{
50 struct btrfs_key key;
51 struct btrfs_key location;
52 struct btrfs_disk_key disk_key;
53 struct btrfs_free_space_header *header;
54 struct extent_buffer *leaf;
55 struct inode *inode = NULL;
56 int ret;
57
Josef Bacik0af3d002010-06-21 14:48:16 -040058 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080059 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040060 key.type = 0;
61
62 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
63 if (ret < 0)
64 return ERR_PTR(ret);
65 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020066 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040067 return ERR_PTR(-ENOENT);
68 }
69
70 leaf = path->nodes[0];
71 header = btrfs_item_ptr(leaf, path->slots[0],
72 struct btrfs_free_space_header);
73 btrfs_free_space_key(leaf, header, &disk_key);
74 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020075 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040076
77 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
78 if (!inode)
79 return ERR_PTR(-ENOENT);
80 if (IS_ERR(inode))
81 return inode;
82 if (is_bad_inode(inode)) {
83 iput(inode);
84 return ERR_PTR(-ENOENT);
85 }
86
Al Viro528c0322012-04-13 11:03:55 -040087 mapping_set_gfp_mask(inode->i_mapping,
Chris Mason2b108262015-04-06 07:48:20 -070088 mapping_gfp_mask(inode->i_mapping) &
89 ~(GFP_NOFS & ~__GFP_HIGHMEM));
Miao Xieadae52b2011-03-31 09:43:23 +000090
Li Zefan0414efa2011-04-20 10:20:14 +080091 return inode;
92}
93
94struct inode *lookup_free_space_inode(struct btrfs_root *root,
95 struct btrfs_block_group_cache
96 *block_group, struct btrfs_path *path)
97{
98 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040099 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800100
101 spin_lock(&block_group->lock);
102 if (block_group->inode)
103 inode = igrab(block_group->inode);
104 spin_unlock(&block_group->lock);
105 if (inode)
106 return inode;
107
108 inode = __lookup_free_space_inode(root, path,
109 block_group->key.objectid);
110 if (IS_ERR(inode))
111 return inode;
112
Josef Bacik0af3d002010-06-21 14:48:16 -0400113 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400114 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000115 btrfs_info(root->fs_info,
116 "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400117 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
118 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400119 block_group->disk_cache_state = BTRFS_DC_CLEAR;
120 }
121
Josef Bacik300e4f82011-08-29 14:06:00 -0400122 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400123 block_group->inode = igrab(inode);
124 block_group->iref = 1;
125 }
126 spin_unlock(&block_group->lock);
127
128 return inode;
129}
130
Eric Sandeen48a3b632013-04-25 20:41:01 +0000131static int __create_free_space_inode(struct btrfs_root *root,
132 struct btrfs_trans_handle *trans,
133 struct btrfs_path *path,
134 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400135{
136 struct btrfs_key key;
137 struct btrfs_disk_key disk_key;
138 struct btrfs_free_space_header *header;
139 struct btrfs_inode_item *inode_item;
140 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400141 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400142 int ret;
143
Li Zefan0414efa2011-04-20 10:20:14 +0800144 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400145 if (ret)
146 return ret;
147
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400148 /* We inline crc's for the free disk space cache */
149 if (ino != BTRFS_FREE_INO_OBJECTID)
150 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
151
Josef Bacik0af3d002010-06-21 14:48:16 -0400152 leaf = path->nodes[0];
153 inode_item = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_inode_item);
155 btrfs_item_key(leaf, &disk_key, path->slots[0]);
156 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
157 sizeof(*inode_item));
158 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
159 btrfs_set_inode_size(leaf, inode_item, 0);
160 btrfs_set_inode_nbytes(leaf, inode_item, 0);
161 btrfs_set_inode_uid(leaf, inode_item, 0);
162 btrfs_set_inode_gid(leaf, inode_item, 0);
163 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400164 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400165 btrfs_set_inode_nlink(leaf, inode_item, 1);
166 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800167 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400168 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200169 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400170
171 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800172 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400173 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400174 ret = btrfs_insert_empty_item(trans, root, path, &key,
175 sizeof(struct btrfs_free_space_header));
176 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200177 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400178 return ret;
179 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700180
Josef Bacik0af3d002010-06-21 14:48:16 -0400181 leaf = path->nodes[0];
182 header = btrfs_item_ptr(leaf, path->slots[0],
183 struct btrfs_free_space_header);
184 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
185 btrfs_set_free_space_key(leaf, header, &disk_key);
186 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200187 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400188
189 return 0;
190}
191
Li Zefan0414efa2011-04-20 10:20:14 +0800192int create_free_space_inode(struct btrfs_root *root,
193 struct btrfs_trans_handle *trans,
194 struct btrfs_block_group_cache *block_group,
195 struct btrfs_path *path)
196{
197 int ret;
198 u64 ino;
199
200 ret = btrfs_find_free_objectid(root, &ino);
201 if (ret < 0)
202 return ret;
203
204 return __create_free_space_inode(root, trans, path, ino,
205 block_group->key.objectid);
206}
207
Miao Xie7b61cd92013-05-13 13:55:09 +0000208int btrfs_check_trunc_cache_free_space(struct btrfs_root *root,
209 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400210{
Josef Bacikc8174312011-11-02 09:29:35 -0400211 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000212 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400213
214 /* 1 for slack space, 1 for updating the inode */
215 needed_bytes = btrfs_calc_trunc_metadata_size(root, 1) +
216 btrfs_calc_trans_metadata_size(root, 1);
217
Miao Xie7b61cd92013-05-13 13:55:09 +0000218 spin_lock(&rsv->lock);
219 if (rsv->reserved < needed_bytes)
220 ret = -ENOSPC;
221 else
222 ret = 0;
223 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000224 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000225}
226
227int btrfs_truncate_free_space_cache(struct btrfs_root *root,
228 struct btrfs_trans_handle *trans,
Miao Xie7b61cd92013-05-13 13:55:09 +0000229 struct inode *inode)
230{
Miao Xie7b61cd92013-05-13 13:55:09 +0000231 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400232
Josef Bacik0af3d002010-06-21 14:48:16 -0400233 btrfs_i_size_write(inode, 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700234 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400235
236 /*
237 * We don't need an orphan item because truncating the free space cache
238 * will never be split across transactions.
Chris Mason28ed1342014-12-17 09:41:04 -0800239 * We don't need to check for -EAGAIN because we're a free space
240 * cache inode
Josef Bacik0af3d002010-06-21 14:48:16 -0400241 */
242 ret = btrfs_truncate_inode_items(trans, root, inode,
243 0, BTRFS_EXTENT_DATA_KEY);
244 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100245 btrfs_abort_transaction(trans, root, ret);
Josef Bacik0af3d002010-06-21 14:48:16 -0400246 return ret;
247 }
248
Li Zefan82d59022011-04-20 10:33:24 +0800249 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100250 if (ret)
251 btrfs_abort_transaction(trans, root, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400252
Li Zefan82d59022011-04-20 10:33:24 +0800253 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400254}
255
Josef Bacik9d66e232010-08-25 16:54:15 -0400256static int readahead_cache(struct inode *inode)
257{
258 struct file_ra_state *ra;
259 unsigned long last_index;
260
261 ra = kzalloc(sizeof(*ra), GFP_NOFS);
262 if (!ra)
263 return -ENOMEM;
264
265 file_ra_state_init(ra, inode->i_mapping);
266 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
267
268 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
269
270 kfree(ra);
271
272 return 0;
273}
274
Chris Mason4c6d1d82015-04-06 13:17:20 -0700275static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Miao Xie5349d6c2014-06-19 10:42:49 +0800276 struct btrfs_root *root, int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400277{
Miao Xie5349d6c2014-06-19 10:42:49 +0800278 int num_pages;
279 int check_crcs = 0;
280
David Sterbaed6078f2014-06-05 01:59:57 +0200281 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800282
283 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
284 check_crcs = 1;
285
286 /* Make sure we can fit our crcs into the first page */
287 if (write && check_crcs &&
288 (num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE)
289 return -ENOSPC;
290
Chris Mason4c6d1d82015-04-06 13:17:20 -0700291 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800292
David Sterba31e818f2015-02-20 18:00:26 +0100293 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400294 if (!io_ctl->pages)
295 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800296
297 io_ctl->num_pages = num_pages;
Josef Bacika67509c2011-10-05 15:18:58 -0400298 io_ctl->root = root;
Miao Xie5349d6c2014-06-19 10:42:49 +0800299 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700300 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800301
Josef Bacika67509c2011-10-05 15:18:58 -0400302 return 0;
303}
304
Chris Mason4c6d1d82015-04-06 13:17:20 -0700305static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400306{
307 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700308 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400309}
310
Chris Mason4c6d1d82015-04-06 13:17:20 -0700311static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400312{
313 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400314 io_ctl->cur = NULL;
315 io_ctl->orig = NULL;
316 }
317}
318
Chris Mason4c6d1d82015-04-06 13:17:20 -0700319static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400320{
Josef Bacikb12d6862013-08-26 17:14:08 -0400321 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400322 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700323 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400324 io_ctl->orig = io_ctl->cur;
325 io_ctl->size = PAGE_CACHE_SIZE;
326 if (clear)
327 memset(io_ctl->cur, 0, PAGE_CACHE_SIZE);
328}
329
Chris Mason4c6d1d82015-04-06 13:17:20 -0700330static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400331{
332 int i;
333
334 io_ctl_unmap_page(io_ctl);
335
336 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800337 if (io_ctl->pages[i]) {
338 ClearPageChecked(io_ctl->pages[i]);
339 unlock_page(io_ctl->pages[i]);
340 page_cache_release(io_ctl->pages[i]);
341 }
Josef Bacika67509c2011-10-05 15:18:58 -0400342 }
343}
344
Chris Mason4c6d1d82015-04-06 13:17:20 -0700345static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400346 int uptodate)
347{
348 struct page *page;
349 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
350 int i;
351
352 for (i = 0; i < io_ctl->num_pages; i++) {
353 page = find_or_create_page(inode->i_mapping, i, mask);
354 if (!page) {
355 io_ctl_drop_pages(io_ctl);
356 return -ENOMEM;
357 }
358 io_ctl->pages[i] = page;
359 if (uptodate && !PageUptodate(page)) {
360 btrfs_readpage(NULL, page);
361 lock_page(page);
362 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500363 btrfs_err(BTRFS_I(inode)->root->fs_info,
364 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400365 io_ctl_drop_pages(io_ctl);
366 return -EIO;
367 }
368 }
369 }
370
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500371 for (i = 0; i < io_ctl->num_pages; i++) {
372 clear_page_dirty_for_io(io_ctl->pages[i]);
373 set_page_extent_mapped(io_ctl->pages[i]);
374 }
375
Josef Bacika67509c2011-10-05 15:18:58 -0400376 return 0;
377}
378
Chris Mason4c6d1d82015-04-06 13:17:20 -0700379static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400380{
Al Viro528c0322012-04-13 11:03:55 -0400381 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400382
383 io_ctl_map_page(io_ctl, 1);
384
385 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400386 * Skip the csum areas. If we don't check crcs then we just have a
387 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400388 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400389 if (io_ctl->check_crcs) {
390 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
391 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
392 } else {
393 io_ctl->cur += sizeof(u64);
394 io_ctl->size -= sizeof(u64) * 2;
395 }
Josef Bacika67509c2011-10-05 15:18:58 -0400396
397 val = io_ctl->cur;
398 *val = cpu_to_le64(generation);
399 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400400}
401
Chris Mason4c6d1d82015-04-06 13:17:20 -0700402static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400403{
Al Viro528c0322012-04-13 11:03:55 -0400404 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400405
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400406 /*
407 * Skip the crc area. If we don't check crcs then we just have a 64bit
408 * chunk at the front of the first page.
409 */
410 if (io_ctl->check_crcs) {
411 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
412 io_ctl->size -= sizeof(u64) +
413 (sizeof(u32) * io_ctl->num_pages);
414 } else {
415 io_ctl->cur += sizeof(u64);
416 io_ctl->size -= sizeof(u64) * 2;
417 }
Josef Bacika67509c2011-10-05 15:18:58 -0400418
Josef Bacika67509c2011-10-05 15:18:58 -0400419 gen = io_ctl->cur;
420 if (le64_to_cpu(*gen) != generation) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500421 printk_ratelimited(KERN_ERR "BTRFS: space cache generation "
Josef Bacika67509c2011-10-05 15:18:58 -0400422 "(%Lu) does not match inode (%Lu)\n", *gen,
423 generation);
424 io_ctl_unmap_page(io_ctl);
425 return -EIO;
426 }
427 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400428 return 0;
429}
430
Chris Mason4c6d1d82015-04-06 13:17:20 -0700431static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400432{
433 u32 *tmp;
434 u32 crc = ~(u32)0;
435 unsigned offset = 0;
436
437 if (!io_ctl->check_crcs) {
438 io_ctl_unmap_page(io_ctl);
439 return;
440 }
441
442 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800443 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400444
Liu Bob0496682013-03-14 14:57:45 +0000445 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400446 PAGE_CACHE_SIZE - offset);
447 btrfs_csum_final(crc, (char *)&crc);
448 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700449 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400450 tmp += index;
451 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400452}
453
Chris Mason4c6d1d82015-04-06 13:17:20 -0700454static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400455{
456 u32 *tmp, val;
457 u32 crc = ~(u32)0;
458 unsigned offset = 0;
459
460 if (!io_ctl->check_crcs) {
461 io_ctl_map_page(io_ctl, 0);
462 return 0;
463 }
464
465 if (index == 0)
466 offset = sizeof(u32) * io_ctl->num_pages;
467
Chris Mason2b108262015-04-06 07:48:20 -0700468 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400469 tmp += index;
470 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400471
472 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000473 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400474 PAGE_CACHE_SIZE - offset);
475 btrfs_csum_final(crc, (char *)&crc);
476 if (val != crc) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500477 printk_ratelimited(KERN_ERR "BTRFS: csum mismatch on free "
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400478 "space cache\n");
479 io_ctl_unmap_page(io_ctl);
480 return -EIO;
481 }
482
Josef Bacika67509c2011-10-05 15:18:58 -0400483 return 0;
484}
485
Chris Mason4c6d1d82015-04-06 13:17:20 -0700486static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400487 void *bitmap)
488{
489 struct btrfs_free_space_entry *entry;
490
491 if (!io_ctl->cur)
492 return -ENOSPC;
493
494 entry = io_ctl->cur;
495 entry->offset = cpu_to_le64(offset);
496 entry->bytes = cpu_to_le64(bytes);
497 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
498 BTRFS_FREE_SPACE_EXTENT;
499 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
500 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
501
502 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
503 return 0;
504
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400505 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400506
507 /* No more pages to map */
508 if (io_ctl->index >= io_ctl->num_pages)
509 return 0;
510
511 /* map the next page */
512 io_ctl_map_page(io_ctl, 1);
513 return 0;
514}
515
Chris Mason4c6d1d82015-04-06 13:17:20 -0700516static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400517{
518 if (!io_ctl->cur)
519 return -ENOSPC;
520
521 /*
522 * If we aren't at the start of the current page, unmap this one and
523 * map the next one if there is any left.
524 */
525 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400526 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400527 if (io_ctl->index >= io_ctl->num_pages)
528 return -ENOSPC;
529 io_ctl_map_page(io_ctl, 0);
530 }
531
532 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400533 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400534 if (io_ctl->index < io_ctl->num_pages)
535 io_ctl_map_page(io_ctl, 0);
536 return 0;
537}
538
Chris Mason4c6d1d82015-04-06 13:17:20 -0700539static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400540{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400541 /*
542 * If we're not on the boundary we know we've modified the page and we
543 * need to crc the page.
544 */
545 if (io_ctl->cur != io_ctl->orig)
546 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
547 else
548 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400549
550 while (io_ctl->index < io_ctl->num_pages) {
551 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400552 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400553 }
554}
555
Chris Mason4c6d1d82015-04-06 13:17:20 -0700556static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400557 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400558{
559 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500560 int ret;
561
562 if (!io_ctl->cur) {
563 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
564 if (ret)
565 return ret;
566 }
Josef Bacika67509c2011-10-05 15:18:58 -0400567
568 e = io_ctl->cur;
569 entry->offset = le64_to_cpu(e->offset);
570 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400571 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400572 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
573 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
574
575 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400576 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400577
578 io_ctl_unmap_page(io_ctl);
579
Josef Bacik2f120c02011-11-10 20:45:05 -0500580 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400581}
582
Chris Mason4c6d1d82015-04-06 13:17:20 -0700583static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400584 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400585{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400586 int ret;
587
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400588 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
589 if (ret)
590 return ret;
591
Josef Bacika67509c2011-10-05 15:18:58 -0400592 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
593 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400594
595 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400596}
597
Josef Bacikcd023e72012-05-14 10:06:40 -0400598/*
599 * Since we attach pinned extents after the fact we can have contiguous sections
600 * of free space that are split up in entries. This poses a problem with the
601 * tree logging stuff since it could have allocated across what appears to be 2
602 * entries since we would have merged the entries when adding the pinned extents
603 * back to the free space cache. So run through the space cache that we just
604 * loaded and merge contiguous entries. This will make the log replay stuff not
605 * blow up and it will make for nicer allocator behavior.
606 */
607static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
608{
609 struct btrfs_free_space *e, *prev = NULL;
610 struct rb_node *n;
611
612again:
613 spin_lock(&ctl->tree_lock);
614 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
615 e = rb_entry(n, struct btrfs_free_space, offset_index);
616 if (!prev)
617 goto next;
618 if (e->bitmap || prev->bitmap)
619 goto next;
620 if (prev->offset + prev->bytes == e->offset) {
621 unlink_free_space(ctl, prev);
622 unlink_free_space(ctl, e);
623 prev->bytes += e->bytes;
624 kmem_cache_free(btrfs_free_space_cachep, e);
625 link_free_space(ctl, prev);
626 prev = NULL;
627 spin_unlock(&ctl->tree_lock);
628 goto again;
629 }
630next:
631 prev = e;
632 }
633 spin_unlock(&ctl->tree_lock);
634}
635
Eric Sandeen48a3b632013-04-25 20:41:01 +0000636static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
637 struct btrfs_free_space_ctl *ctl,
638 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400639{
Josef Bacik9d66e232010-08-25 16:54:15 -0400640 struct btrfs_free_space_header *header;
641 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700642 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400643 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400644 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800645 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400646 u64 num_entries;
647 u64 num_bitmaps;
648 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400649 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400650 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400651
Josef Bacik9d66e232010-08-25 16:54:15 -0400652 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800653 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400654 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400655
656 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800657 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400658 key.type = 0;
659
660 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800661 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400662 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800663 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400664 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400665 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400666 }
667
Li Zefan0414efa2011-04-20 10:20:14 +0800668 ret = -1;
669
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 leaf = path->nodes[0];
671 header = btrfs_item_ptr(leaf, path->slots[0],
672 struct btrfs_free_space_header);
673 num_entries = btrfs_free_space_entries(leaf, header);
674 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
675 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400676 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400677
Miao Xiee570fd22014-06-19 10:42:50 +0800678 if (!BTRFS_I(inode)->generation) {
679 btrfs_info(root->fs_info,
680 "The free space cache file (%llu) is invalid. skip it\n",
681 offset);
682 return 0;
683 }
684
Josef Bacik9d66e232010-08-25 16:54:15 -0400685 if (BTRFS_I(inode)->generation != generation) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000686 btrfs_err(root->fs_info,
687 "free space inode generation (%llu) "
688 "did not match free space cache generation (%llu)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200689 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400690 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400691 }
692
693 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400694 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400695
Miao Xie5349d6c2014-06-19 10:42:49 +0800696 ret = io_ctl_init(&io_ctl, inode, root, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800697 if (ret)
698 return ret;
699
Josef Bacik9d66e232010-08-25 16:54:15 -0400700 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800701 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400702 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400703
Josef Bacika67509c2011-10-05 15:18:58 -0400704 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
705 if (ret)
706 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400707
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400708 ret = io_ctl_check_crc(&io_ctl, 0);
709 if (ret)
710 goto free_cache;
711
Josef Bacika67509c2011-10-05 15:18:58 -0400712 ret = io_ctl_check_generation(&io_ctl, generation);
713 if (ret)
714 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400715
Josef Bacika67509c2011-10-05 15:18:58 -0400716 while (num_entries) {
717 e = kmem_cache_zalloc(btrfs_free_space_cachep,
718 GFP_NOFS);
719 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400720 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400721
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400722 ret = io_ctl_read_entry(&io_ctl, e, &type);
723 if (ret) {
724 kmem_cache_free(btrfs_free_space_cachep, e);
725 goto free_cache;
726 }
727
Josef Bacika67509c2011-10-05 15:18:58 -0400728 if (!e->bytes) {
729 kmem_cache_free(btrfs_free_space_cachep, e);
730 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400731 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400732
Josef Bacika67509c2011-10-05 15:18:58 -0400733 if (type == BTRFS_FREE_SPACE_EXTENT) {
734 spin_lock(&ctl->tree_lock);
735 ret = link_free_space(ctl, e);
736 spin_unlock(&ctl->tree_lock);
737 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000738 btrfs_err(root->fs_info,
739 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500740 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400741 goto free_cache;
742 }
Josef Bacika67509c2011-10-05 15:18:58 -0400743 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400744 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400745 num_bitmaps--;
746 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
747 if (!e->bitmap) {
748 kmem_cache_free(
749 btrfs_free_space_cachep, e);
750 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400751 }
Josef Bacika67509c2011-10-05 15:18:58 -0400752 spin_lock(&ctl->tree_lock);
753 ret = link_free_space(ctl, e);
754 ctl->total_bitmaps++;
755 ctl->op->recalc_thresholds(ctl);
756 spin_unlock(&ctl->tree_lock);
757 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000758 btrfs_err(root->fs_info,
759 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400760 kmem_cache_free(btrfs_free_space_cachep, e);
761 goto free_cache;
762 }
763 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400764 }
765
Josef Bacika67509c2011-10-05 15:18:58 -0400766 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400767 }
768
Josef Bacik2f120c02011-11-10 20:45:05 -0500769 io_ctl_unmap_page(&io_ctl);
770
Josef Bacika67509c2011-10-05 15:18:58 -0400771 /*
772 * We add the bitmaps at the end of the entries in order that
773 * the bitmap entries are added to the cache.
774 */
775 list_for_each_entry_safe(e, n, &bitmaps, list) {
776 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400777 ret = io_ctl_read_bitmap(&io_ctl, e);
778 if (ret)
779 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400780 }
781
782 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400783 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400784 ret = 1;
785out:
Josef Bacika67509c2011-10-05 15:18:58 -0400786 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400787 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400788free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400789 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800790 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400791 goto out;
792}
793
Li Zefan0414efa2011-04-20 10:20:14 +0800794int load_free_space_cache(struct btrfs_fs_info *fs_info,
795 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400796{
Li Zefan34d52cb2011-03-29 13:46:06 +0800797 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800798 struct btrfs_root *root = fs_info->tree_root;
799 struct inode *inode;
800 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400801 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800802 bool matched;
803 u64 used = btrfs_block_group_used(&block_group->item);
804
805 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800806 * If this block group has been marked to be cleared for one reason or
807 * another then we can't trust the on disk cache, so just return.
808 */
809 spin_lock(&block_group->lock);
810 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
811 spin_unlock(&block_group->lock);
812 return 0;
813 }
814 spin_unlock(&block_group->lock);
815
816 path = btrfs_alloc_path();
817 if (!path)
818 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400819 path->search_commit_root = 1;
820 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800821
822 inode = lookup_free_space_inode(root, block_group, path);
823 if (IS_ERR(inode)) {
824 btrfs_free_path(path);
825 return 0;
826 }
827
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400828 /* We may have converted the inode and made the cache invalid. */
829 spin_lock(&block_group->lock);
830 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
831 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900832 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400833 goto out;
834 }
835 spin_unlock(&block_group->lock);
836
Li Zefan0414efa2011-04-20 10:20:14 +0800837 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
838 path, block_group->key.objectid);
839 btrfs_free_path(path);
840 if (ret <= 0)
841 goto out;
842
843 spin_lock(&ctl->tree_lock);
844 matched = (ctl->free_space == (block_group->key.offset - used -
845 block_group->bytes_super));
846 spin_unlock(&ctl->tree_lock);
847
848 if (!matched) {
849 __btrfs_remove_free_space_cache(ctl);
Miao Xie32d6b472014-04-24 13:31:55 +0800850 btrfs_warn(fs_info, "block group %llu has wrong amount of free space",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000851 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800852 ret = -1;
853 }
854out:
855 if (ret < 0) {
856 /* This cache is bogus, make sure it gets cleared */
857 spin_lock(&block_group->lock);
858 block_group->disk_cache_state = BTRFS_DC_CLEAR;
859 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800860 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800861
Miao Xie32d6b472014-04-24 13:31:55 +0800862 btrfs_warn(fs_info, "failed to load free space cache for block group %llu, rebuild it now",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000863 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800864 }
865
866 iput(inode);
867 return ret;
868}
869
Chris Masond4452bc2014-05-19 20:47:56 -0700870static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700871int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700872 struct btrfs_free_space_ctl *ctl,
873 struct btrfs_block_group_cache *block_group,
874 int *entries, int *bitmaps,
875 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400876{
Josef Bacikc09544e2011-08-30 10:19:10 -0400877 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700878 struct btrfs_free_cluster *cluster = NULL;
879 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000880 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400881
Josef Bacik43be2142011-04-01 14:55:00 +0000882 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700883 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000884 cluster = list_entry(block_group->cluster_list.next,
885 struct btrfs_free_cluster,
886 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700887 }
Josef Bacik43be2142011-04-01 14:55:00 +0000888
Josef Bacikf75b1302011-10-05 10:00:18 -0400889 if (!node && cluster) {
890 node = rb_first(&cluster->root);
891 cluster = NULL;
892 }
893
Josef Bacik0cb59c92010-07-02 12:14:14 -0400894 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400895 while (node) {
896 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400897
Josef Bacika67509c2011-10-05 15:18:58 -0400898 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700899 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000900
Chris Masond4452bc2014-05-19 20:47:56 -0700901 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400902 e->bitmap);
903 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700904 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400905
906 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700907 list_add_tail(&e->list, bitmap_list);
908 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400909 }
910 node = rb_next(node);
911 if (!node && cluster) {
912 node = rb_first(&cluster->root);
913 cluster = NULL;
914 }
915 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000916
917 /*
918 * Make sure we don't miss any range that was removed from our rbtree
919 * because trimming is running. Otherwise after a umount+mount (or crash
920 * after committing the transaction) we would leak free space and get
921 * an inconsistent free space cache report from fsck.
922 */
923 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
924 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
925 trim_entry->bytes, NULL);
926 if (ret)
927 goto fail;
928 *entries += 1;
929 }
930
Chris Masond4452bc2014-05-19 20:47:56 -0700931 return 0;
932fail:
933 return -ENOSPC;
934}
935
936static noinline_for_stack int
937update_cache_item(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root,
939 struct inode *inode,
940 struct btrfs_path *path, u64 offset,
941 int entries, int bitmaps)
942{
943 struct btrfs_key key;
944 struct btrfs_free_space_header *header;
945 struct extent_buffer *leaf;
946 int ret;
947
948 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
949 key.offset = offset;
950 key.type = 0;
951
952 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
953 if (ret < 0) {
954 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
955 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
956 GFP_NOFS);
957 goto fail;
958 }
959 leaf = path->nodes[0];
960 if (ret > 0) {
961 struct btrfs_key found_key;
962 ASSERT(path->slots[0]);
963 path->slots[0]--;
964 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
965 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
966 found_key.offset != offset) {
967 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
968 inode->i_size - 1,
969 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
970 NULL, GFP_NOFS);
971 btrfs_release_path(path);
972 goto fail;
973 }
974 }
975
976 BTRFS_I(inode)->generation = trans->transid;
977 header = btrfs_item_ptr(leaf, path->slots[0],
978 struct btrfs_free_space_header);
979 btrfs_set_free_space_entries(leaf, header, entries);
980 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
981 btrfs_set_free_space_generation(leaf, header, trans->transid);
982 btrfs_mark_buffer_dirty(leaf);
983 btrfs_release_path(path);
984
985 return 0;
986
987fail:
988 return -1;
989}
990
991static noinline_for_stack int
Miao Xie5349d6c2014-06-19 10:42:49 +0800992write_pinned_extent_entries(struct btrfs_root *root,
993 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -0700994 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +0800995 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -0700996{
997 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -0700998 struct extent_io_tree *unpin = NULL;
999 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001000
Miao Xie5349d6c2014-06-19 10:42:49 +08001001 if (!block_group)
1002 return 0;
1003
Josef Bacika67509c2011-10-05 15:18:58 -04001004 /*
1005 * We want to add any pinned extents to our free space cache
1006 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001007 *
Li Zefandb804f22012-01-10 16:41:01 +08001008 * We shouldn't have switched the pinned extents yet so this is the
1009 * right one
1010 */
1011 unpin = root->fs_info->pinned_extents;
1012
Miao Xie5349d6c2014-06-19 10:42:49 +08001013 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001014
Miao Xie5349d6c2014-06-19 10:42:49 +08001015 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001016 ret = find_first_extent_bit(unpin, start,
1017 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001018 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001019 if (ret)
1020 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001021
Josef Bacika67509c2011-10-05 15:18:58 -04001022 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001023 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001024 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001025 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001026
Li Zefandb804f22012-01-10 16:41:01 +08001027 extent_start = max(extent_start, start);
1028 extent_end = min(block_group->key.objectid +
1029 block_group->key.offset, extent_end + 1);
1030 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001031
Chris Masond4452bc2014-05-19 20:47:56 -07001032 *entries += 1;
1033 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001034 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001035 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001036
Li Zefandb804f22012-01-10 16:41:01 +08001037 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001038 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001039
Miao Xie5349d6c2014-06-19 10:42:49 +08001040 return 0;
1041}
1042
1043static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001044write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001045{
1046 struct list_head *pos, *n;
1047 int ret;
1048
Josef Bacik0cb59c92010-07-02 12:14:14 -04001049 /* Write out the bitmaps */
Chris Masond4452bc2014-05-19 20:47:56 -07001050 list_for_each_safe(pos, n, bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001051 struct btrfs_free_space *entry =
1052 list_entry(pos, struct btrfs_free_space, list);
1053
Chris Masond4452bc2014-05-19 20:47:56 -07001054 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001055 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001056 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001057 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001058 }
1059
Miao Xie5349d6c2014-06-19 10:42:49 +08001060 return 0;
1061}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001062
Miao Xie5349d6c2014-06-19 10:42:49 +08001063static int flush_dirty_cache(struct inode *inode)
1064{
1065 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001066
Josef Bacik0ef8b722013-10-25 16:13:35 -04001067 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001068 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001069 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1070 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1071 GFP_NOFS);
Chris Masond4452bc2014-05-19 20:47:56 -07001072
Miao Xie5349d6c2014-06-19 10:42:49 +08001073 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001074}
1075
1076static void noinline_for_stack
1077cleanup_write_cache_enospc(struct inode *inode,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001078 struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001079 struct extent_state **cached_state,
1080 struct list_head *bitmap_list)
1081{
1082 struct list_head *pos, *n;
Miao Xie5349d6c2014-06-19 10:42:49 +08001083
Chris Masond4452bc2014-05-19 20:47:56 -07001084 list_for_each_safe(pos, n, bitmap_list) {
1085 struct btrfs_free_space *entry =
1086 list_entry(pos, struct btrfs_free_space, list);
1087 list_del_init(&entry->list);
1088 }
1089 io_ctl_drop_pages(io_ctl);
1090 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1091 i_size_read(inode) - 1, cached_state,
1092 GFP_NOFS);
1093}
1094
Chris Masonc9dc4c62015-04-04 17:14:42 -07001095int btrfs_wait_cache_io(struct btrfs_root *root,
1096 struct btrfs_trans_handle *trans,
1097 struct btrfs_block_group_cache *block_group,
1098 struct btrfs_io_ctl *io_ctl,
1099 struct btrfs_path *path, u64 offset)
1100{
1101 int ret;
1102 struct inode *inode = io_ctl->inode;
1103
1104 root = root->fs_info->tree_root;
1105
1106 /* Flush the dirty pages in the cache file. */
1107 ret = flush_dirty_cache(inode);
1108 if (ret)
1109 goto out;
1110
1111 /* Update the cache item to tell everyone this cache file is valid. */
1112 ret = update_cache_item(trans, root, inode, path, offset,
1113 io_ctl->entries, io_ctl->bitmaps);
1114out:
1115 io_ctl_free(io_ctl);
1116 if (ret) {
1117 invalidate_inode_pages2(inode->i_mapping);
1118 BTRFS_I(inode)->generation = 0;
1119 if (block_group) {
1120#ifdef DEBUG
1121 btrfs_err(root->fs_info,
1122 "failed to write free space cache for block group %llu",
1123 block_group->key.objectid);
1124#endif
1125 }
1126 }
1127 btrfs_update_inode(trans, root, inode);
1128
1129 if (block_group) {
1130 spin_lock(&block_group->lock);
1131
1132 /*
1133 * only mark this as written if we didn't get put back on
1134 * the dirty list while waiting for IO.
1135 */
1136 if (!ret && list_empty(&block_group->dirty_list))
1137 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1138 else if (ret)
1139 block_group->disk_cache_state = BTRFS_DC_ERROR;
1140
1141 spin_unlock(&block_group->lock);
1142 io_ctl->inode = NULL;
1143 iput(inode);
1144 }
1145
1146 return ret;
1147
1148}
1149
Chris Masond4452bc2014-05-19 20:47:56 -07001150/**
1151 * __btrfs_write_out_cache - write out cached info to an inode
1152 * @root - the root the inode belongs to
1153 * @ctl - the free space cache we are going to write out
1154 * @block_group - the block_group for this cache if it belongs to a block_group
1155 * @trans - the trans handle
1156 * @path - the path to use
1157 * @offset - the offset for the key we'll insert
1158 *
1159 * This function writes out a free space cache struct to disk for quick recovery
1160 * on mount. This will return 0 if it was successfull in writing the cache out,
1161 * and -1 if it was not.
1162 */
1163static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1164 struct btrfs_free_space_ctl *ctl,
1165 struct btrfs_block_group_cache *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001166 struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001167 struct btrfs_trans_handle *trans,
1168 struct btrfs_path *path, u64 offset)
1169{
1170 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001171 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001172 int entries = 0;
1173 int bitmaps = 0;
1174 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001175 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001176
1177 if (!i_size_read(inode))
1178 return -1;
1179
Chris Masonc9dc4c62015-04-04 17:14:42 -07001180 WARN_ON(io_ctl->pages);
1181 ret = io_ctl_init(io_ctl, inode, root, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001182 if (ret)
1183 return -1;
1184
Miao Xiee570fd22014-06-19 10:42:50 +08001185 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1186 down_write(&block_group->data_rwsem);
1187 spin_lock(&block_group->lock);
1188 if (block_group->delalloc_bytes) {
1189 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1190 spin_unlock(&block_group->lock);
1191 up_write(&block_group->data_rwsem);
1192 BTRFS_I(inode)->generation = 0;
1193 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001194 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001195 goto out;
1196 }
1197 spin_unlock(&block_group->lock);
1198 }
1199
Chris Masond4452bc2014-05-19 20:47:56 -07001200 /* Lock all pages first so we can lock the extent safely. */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001201 io_ctl_prepare_pages(io_ctl, inode, 0);
Chris Masond4452bc2014-05-19 20:47:56 -07001202
1203 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1204 0, &cached_state);
1205
Chris Masonc9dc4c62015-04-04 17:14:42 -07001206 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001207
Filipe Manana55507ce2014-12-01 17:04:09 +00001208 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001209 /* Write out the extent entries in the free space cache */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001210 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001211 block_group, &entries, &bitmaps,
1212 &bitmap_list);
Filipe Manana55507ce2014-12-01 17:04:09 +00001213 if (ret) {
1214 mutex_unlock(&ctl->cache_writeout_mutex);
Chris Masond4452bc2014-05-19 20:47:56 -07001215 goto out_nospc;
Filipe Manana55507ce2014-12-01 17:04:09 +00001216 }
Chris Masond4452bc2014-05-19 20:47:56 -07001217
Miao Xie5349d6c2014-06-19 10:42:49 +08001218 /*
1219 * Some spaces that are freed in the current transaction are pinned,
1220 * they will be added into free space cache after the transaction is
1221 * committed, we shouldn't lose them.
1222 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001223 ret = write_pinned_extent_entries(root, block_group, io_ctl, &entries);
Filipe Manana55507ce2014-12-01 17:04:09 +00001224 if (ret) {
1225 mutex_unlock(&ctl->cache_writeout_mutex);
Chris Masond4452bc2014-05-19 20:47:56 -07001226 goto out_nospc;
Filipe Manana55507ce2014-12-01 17:04:09 +00001227 }
Miao Xie5349d6c2014-06-19 10:42:49 +08001228
Filipe Manana55507ce2014-12-01 17:04:09 +00001229 /*
1230 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1231 * locked while doing it because a concurrent trim can be manipulating
1232 * or freeing the bitmap.
1233 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001234 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Filipe Manana55507ce2014-12-01 17:04:09 +00001235 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001236 if (ret)
1237 goto out_nospc;
1238
1239 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001240 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001241
1242 /* Everything is written out, now we dirty the pages in the file. */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001243 ret = btrfs_dirty_pages(root, inode, io_ctl->pages, io_ctl->num_pages,
Miao Xie5349d6c2014-06-19 10:42:49 +08001244 0, i_size_read(inode), &cached_state);
1245 if (ret)
1246 goto out_nospc;
1247
Miao Xiee570fd22014-06-19 10:42:50 +08001248 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1249 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001250 /*
1251 * Release the pages and unlock the extent, we will flush
1252 * them out later
1253 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001254 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001255
1256 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1257 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1258
Chris Masonc9dc4c62015-04-04 17:14:42 -07001259 /*
1260 * at this point the pages are under IO and we're happy,
1261 * The caller is responsible for waiting on them and updating the
1262 * the cache and the inode
1263 */
1264 io_ctl->entries = entries;
1265 io_ctl->bitmaps = bitmaps;
1266
1267 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001268 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001269 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001270
Chris Masonc9dc4c62015-04-04 17:14:42 -07001271 return 0;
1272
Josef Bacik2f356122011-06-10 15:31:13 -04001273out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001274 io_ctl->inode = NULL;
1275 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001276 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001277 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001278 BTRFS_I(inode)->generation = 0;
1279 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001280 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001281 if (must_iput)
1282 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001283 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001284
1285out_nospc:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001286 cleanup_write_cache_enospc(inode, io_ctl, &cached_state, &bitmap_list);
Miao Xiee570fd22014-06-19 10:42:50 +08001287
1288 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1289 up_write(&block_group->data_rwsem);
1290
Josef Bacika67509c2011-10-05 15:18:58 -04001291 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001292}
1293
1294int btrfs_write_out_cache(struct btrfs_root *root,
1295 struct btrfs_trans_handle *trans,
1296 struct btrfs_block_group_cache *block_group,
1297 struct btrfs_path *path)
1298{
1299 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1300 struct inode *inode;
1301 int ret = 0;
1302
1303 root = root->fs_info->tree_root;
1304
1305 spin_lock(&block_group->lock);
1306 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1307 spin_unlock(&block_group->lock);
1308 return 0;
1309 }
Miao Xiee570fd22014-06-19 10:42:50 +08001310
1311 if (block_group->delalloc_bytes) {
1312 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1313 spin_unlock(&block_group->lock);
1314 return 0;
1315 }
Li Zefan0414efa2011-04-20 10:20:14 +08001316 spin_unlock(&block_group->lock);
1317
1318 inode = lookup_free_space_inode(root, block_group, path);
1319 if (IS_ERR(inode))
1320 return 0;
1321
Chris Masonc9dc4c62015-04-04 17:14:42 -07001322 ret = __btrfs_write_out_cache(root, inode, ctl, block_group,
1323 &block_group->io_ctl, trans,
Li Zefan0414efa2011-04-20 10:20:14 +08001324 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001325 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001326#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00001327 btrfs_err(root->fs_info,
1328 "failed to write free space cache for block group %llu",
1329 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001330#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001331 spin_lock(&block_group->lock);
1332 block_group->disk_cache_state = BTRFS_DC_ERROR;
1333 spin_unlock(&block_group->lock);
1334
1335 block_group->io_ctl.inode = NULL;
1336 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001337 }
1338
Chris Masonc9dc4c62015-04-04 17:14:42 -07001339 /*
1340 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1341 * to wait for IO and put the inode
1342 */
1343
Josef Bacik0cb59c92010-07-02 12:14:14 -04001344 return ret;
1345}
1346
Li Zefan34d52cb2011-03-29 13:46:06 +08001347static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001348 u64 offset)
1349{
Josef Bacikb12d6862013-08-26 17:14:08 -04001350 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001351 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001352 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001353}
1354
Li Zefan34d52cb2011-03-29 13:46:06 +08001355static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001356{
Li Zefan34d52cb2011-03-29 13:46:06 +08001357 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001358}
1359
Li Zefan34d52cb2011-03-29 13:46:06 +08001360static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001361 u64 offset)
1362{
1363 u64 bitmap_start;
David Sterbab8b93ad2015-01-16 17:26:13 +01001364 u32 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001365
Li Zefan34d52cb2011-03-29 13:46:06 +08001366 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1367 bitmap_start = offset - ctl->start;
David Sterbab8b93ad2015-01-16 17:26:13 +01001368 bitmap_start = div_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001369 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001370 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001371
1372 return bitmap_start;
1373}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001374
1375static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001376 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001377{
1378 struct rb_node **p = &root->rb_node;
1379 struct rb_node *parent = NULL;
1380 struct btrfs_free_space *info;
1381
1382 while (*p) {
1383 parent = *p;
1384 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1385
Josef Bacik96303082009-07-13 21:29:25 -04001386 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001387 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001388 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001389 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001390 } else {
1391 /*
1392 * we could have a bitmap entry and an extent entry
1393 * share the same offset. If this is the case, we want
1394 * the extent entry to always be found first if we do a
1395 * linear search through the tree, since we want to have
1396 * the quickest allocation time, and allocating from an
1397 * extent is faster than allocating from a bitmap. So
1398 * if we're inserting a bitmap and we find an entry at
1399 * this offset, we want to go right, or after this entry
1400 * logically. If we are inserting an extent and we've
1401 * found a bitmap, we want to go left, or before
1402 * logically.
1403 */
1404 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001405 if (info->bitmap) {
1406 WARN_ON_ONCE(1);
1407 return -EEXIST;
1408 }
Josef Bacik96303082009-07-13 21:29:25 -04001409 p = &(*p)->rb_right;
1410 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001411 if (!info->bitmap) {
1412 WARN_ON_ONCE(1);
1413 return -EEXIST;
1414 }
Josef Bacik96303082009-07-13 21:29:25 -04001415 p = &(*p)->rb_left;
1416 }
1417 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001418 }
1419
1420 rb_link_node(node, parent, p);
1421 rb_insert_color(node, root);
1422
1423 return 0;
1424}
1425
1426/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001427 * searches the tree for the given offset.
1428 *
Josef Bacik96303082009-07-13 21:29:25 -04001429 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1430 * want a section that has at least bytes size and comes at or after the given
1431 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001432 */
Josef Bacik96303082009-07-13 21:29:25 -04001433static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001434tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001435 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001436{
Li Zefan34d52cb2011-03-29 13:46:06 +08001437 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001438 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001439
Josef Bacik96303082009-07-13 21:29:25 -04001440 /* find entry that is closest to the 'offset' */
1441 while (1) {
1442 if (!n) {
1443 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001444 break;
1445 }
Josef Bacik96303082009-07-13 21:29:25 -04001446
1447 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1448 prev = entry;
1449
1450 if (offset < entry->offset)
1451 n = n->rb_left;
1452 else if (offset > entry->offset)
1453 n = n->rb_right;
1454 else
1455 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001456 }
1457
Josef Bacik96303082009-07-13 21:29:25 -04001458 if (bitmap_only) {
1459 if (!entry)
1460 return NULL;
1461 if (entry->bitmap)
1462 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001463
Josef Bacik96303082009-07-13 21:29:25 -04001464 /*
1465 * bitmap entry and extent entry may share same offset,
1466 * in that case, bitmap entry comes after extent entry.
1467 */
1468 n = rb_next(n);
1469 if (!n)
1470 return NULL;
1471 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1472 if (entry->offset != offset)
1473 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001474
Josef Bacik96303082009-07-13 21:29:25 -04001475 WARN_ON(!entry->bitmap);
1476 return entry;
1477 } else if (entry) {
1478 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001479 /*
Josef Bacik96303082009-07-13 21:29:25 -04001480 * if previous extent entry covers the offset,
1481 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001482 */
Miao Xiede6c4112012-10-18 08:18:01 +00001483 n = rb_prev(&entry->offset_index);
1484 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001485 prev = rb_entry(n, struct btrfs_free_space,
1486 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001487 if (!prev->bitmap &&
1488 prev->offset + prev->bytes > offset)
1489 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001490 }
Josef Bacik96303082009-07-13 21:29:25 -04001491 }
1492 return entry;
1493 }
1494
1495 if (!prev)
1496 return NULL;
1497
1498 /* find last entry before the 'offset' */
1499 entry = prev;
1500 if (entry->offset > offset) {
1501 n = rb_prev(&entry->offset_index);
1502 if (n) {
1503 entry = rb_entry(n, struct btrfs_free_space,
1504 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001505 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001506 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001507 if (fuzzy)
1508 return entry;
1509 else
1510 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001511 }
1512 }
1513
Josef Bacik96303082009-07-13 21:29:25 -04001514 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001515 n = rb_prev(&entry->offset_index);
1516 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001517 prev = rb_entry(n, struct btrfs_free_space,
1518 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001519 if (!prev->bitmap &&
1520 prev->offset + prev->bytes > offset)
1521 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001522 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001523 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001524 return entry;
1525 } else if (entry->offset + entry->bytes > offset)
1526 return entry;
1527
1528 if (!fuzzy)
1529 return NULL;
1530
1531 while (1) {
1532 if (entry->bitmap) {
1533 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001534 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001535 break;
1536 } else {
1537 if (entry->offset + entry->bytes > offset)
1538 break;
1539 }
1540
1541 n = rb_next(&entry->offset_index);
1542 if (!n)
1543 return NULL;
1544 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1545 }
1546 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001547}
1548
Li Zefanf333adb2010-11-09 14:57:39 +08001549static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001550__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001551 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001552{
Li Zefan34d52cb2011-03-29 13:46:06 +08001553 rb_erase(&info->offset_index, &ctl->free_space_offset);
1554 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001555}
1556
Li Zefan34d52cb2011-03-29 13:46:06 +08001557static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001558 struct btrfs_free_space *info)
1559{
Li Zefan34d52cb2011-03-29 13:46:06 +08001560 __unlink_free_space(ctl, info);
1561 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001562}
1563
Li Zefan34d52cb2011-03-29 13:46:06 +08001564static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001565 struct btrfs_free_space *info)
1566{
1567 int ret = 0;
1568
Josef Bacikb12d6862013-08-26 17:14:08 -04001569 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001570 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001571 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001572 if (ret)
1573 return ret;
1574
Li Zefan34d52cb2011-03-29 13:46:06 +08001575 ctl->free_space += info->bytes;
1576 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001577 return ret;
1578}
1579
Li Zefan34d52cb2011-03-29 13:46:06 +08001580static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001581{
Li Zefan34d52cb2011-03-29 13:46:06 +08001582 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001583 u64 max_bytes;
1584 u64 bitmap_bytes;
1585 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001586 u64 size = block_group->key.offset;
David Sterbab8b93ad2015-01-16 17:26:13 +01001587 u32 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1588 u32 max_bitmaps = div_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001589
David Sterbab8b93ad2015-01-16 17:26:13 +01001590 max_bitmaps = max_t(u32, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001591
Josef Bacikb12d6862013-08-26 17:14:08 -04001592 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001593
1594 /*
1595 * The goal is to keep the total amount of memory used per 1gb of space
1596 * at or below 32k, so we need to adjust how much memory we allow to be
1597 * used by extent based free space tracking
1598 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001599 if (size < 1024 * 1024 * 1024)
1600 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1601 else
1602 max_bytes = MAX_CACHE_BYTES_PER_GIG *
David Sterbaf8c269d2015-01-16 17:21:12 +01001603 div_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001604
Josef Bacik25891f72009-09-11 16:11:20 -04001605 /*
1606 * we want to account for 1 more bitmap than what we have so we can make
1607 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1608 * we add more bitmaps.
1609 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001610 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001611
Josef Bacik25891f72009-09-11 16:11:20 -04001612 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001613 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001614 return;
Josef Bacik96303082009-07-13 21:29:25 -04001615 }
Josef Bacik25891f72009-09-11 16:11:20 -04001616
1617 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001618 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001619 * bytes we can have, or whatever is less than that.
1620 */
1621 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001622 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001623
Li Zefan34d52cb2011-03-29 13:46:06 +08001624 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001625 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001626}
1627
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001628static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1629 struct btrfs_free_space *info,
1630 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001631{
Li Zefanf38b6e72011-03-14 13:40:51 +08001632 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001633
Li Zefan34d52cb2011-03-29 13:46:06 +08001634 start = offset_to_bit(info->offset, ctl->unit, offset);
1635 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001636 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001637
Li Zefanf38b6e72011-03-14 13:40:51 +08001638 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001639
1640 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001641}
1642
1643static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1644 struct btrfs_free_space *info, u64 offset,
1645 u64 bytes)
1646{
1647 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001648 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001649}
1650
Li Zefan34d52cb2011-03-29 13:46:06 +08001651static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001652 struct btrfs_free_space *info, u64 offset,
1653 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001654{
Li Zefanf38b6e72011-03-14 13:40:51 +08001655 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001656
Li Zefan34d52cb2011-03-29 13:46:06 +08001657 start = offset_to_bit(info->offset, ctl->unit, offset);
1658 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001659 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001660
Li Zefanf38b6e72011-03-14 13:40:51 +08001661 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001662
1663 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001664 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001665}
1666
Miao Xiea4820392013-09-09 13:19:42 +08001667/*
1668 * If we can not find suitable extent, we will use bytes to record
1669 * the size of the max extent.
1670 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001671static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001672 struct btrfs_free_space *bitmap_info, u64 *offset,
1673 u64 *bytes)
1674{
1675 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001676 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001677 unsigned long bits, i;
1678 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001679 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001680
Li Zefan34d52cb2011-03-29 13:46:06 +08001681 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001682 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001683 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001684
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001685 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04001686 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1687 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001688 extent_bits = next_zero - i;
1689 if (extent_bits >= bits) {
1690 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001691 break;
Miao Xiea4820392013-09-09 13:19:42 +08001692 } else if (extent_bits > max_bits) {
1693 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001694 }
1695 i = next_zero;
1696 }
1697
1698 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001699 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1700 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001701 return 0;
1702 }
1703
Miao Xiea4820392013-09-09 13:19:42 +08001704 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001705 return -1;
1706}
1707
Miao Xiea4820392013-09-09 13:19:42 +08001708/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001709static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001710find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001711 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001712{
1713 struct btrfs_free_space *entry;
1714 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001715 u64 tmp;
1716 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001717 int ret;
1718
Li Zefan34d52cb2011-03-29 13:46:06 +08001719 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001720 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001721
Li Zefan34d52cb2011-03-29 13:46:06 +08001722 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001723 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001724 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001725
1726 for (node = &entry->offset_index; node; node = rb_next(node)) {
1727 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001728 if (entry->bytes < *bytes) {
1729 if (entry->bytes > *max_extent_size)
1730 *max_extent_size = entry->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001731 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001732 }
Josef Bacik96303082009-07-13 21:29:25 -04001733
David Woodhouse53b381b2013-01-29 18:40:14 -05001734 /* make sure the space returned is big enough
1735 * to match our requested alignment
1736 */
1737 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001738 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001739 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001740 tmp = tmp * align + ctl->start;
1741 align_off = tmp - entry->offset;
1742 } else {
1743 align_off = 0;
1744 tmp = entry->offset;
1745 }
1746
Miao Xiea4820392013-09-09 13:19:42 +08001747 if (entry->bytes < *bytes + align_off) {
1748 if (entry->bytes > *max_extent_size)
1749 *max_extent_size = entry->bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05001750 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001751 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001752
Josef Bacik96303082009-07-13 21:29:25 -04001753 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001754 u64 size = *bytes;
1755
1756 ret = search_bitmap(ctl, entry, &tmp, &size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001757 if (!ret) {
1758 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001759 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001760 return entry;
Miao Xiea4820392013-09-09 13:19:42 +08001761 } else if (size > *max_extent_size) {
1762 *max_extent_size = size;
David Woodhouse53b381b2013-01-29 18:40:14 -05001763 }
Josef Bacik96303082009-07-13 21:29:25 -04001764 continue;
1765 }
1766
David Woodhouse53b381b2013-01-29 18:40:14 -05001767 *offset = tmp;
1768 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001769 return entry;
1770 }
Miao Xiea4820392013-09-09 13:19:42 +08001771out:
Josef Bacik96303082009-07-13 21:29:25 -04001772 return NULL;
1773}
1774
Li Zefan34d52cb2011-03-29 13:46:06 +08001775static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001776 struct btrfs_free_space *info, u64 offset)
1777{
Li Zefan34d52cb2011-03-29 13:46:06 +08001778 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001779 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001780 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001781 link_free_space(ctl, info);
1782 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001783
Li Zefan34d52cb2011-03-29 13:46:06 +08001784 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001785}
1786
Li Zefan34d52cb2011-03-29 13:46:06 +08001787static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001788 struct btrfs_free_space *bitmap_info)
1789{
Li Zefan34d52cb2011-03-29 13:46:06 +08001790 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001791 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001792 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001793 ctl->total_bitmaps--;
1794 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001795}
1796
Li Zefan34d52cb2011-03-29 13:46:06 +08001797static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001798 struct btrfs_free_space *bitmap_info,
1799 u64 *offset, u64 *bytes)
1800{
1801 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001802 u64 search_start, search_bytes;
1803 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001804
1805again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001806 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001807
Josef Bacik6606bb92009-07-31 11:03:58 -04001808 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001809 * We need to search for bits in this bitmap. We could only cover some
1810 * of the extent in this bitmap thanks to how we add space, so we need
1811 * to search for as much as it as we can and clear that amount, and then
1812 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001813 */
1814 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001815 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001816 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001817 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001818 if (ret < 0 || search_start != *offset)
1819 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001820
Josef Bacikbdb7d302012-06-27 15:10:56 -04001821 /* We may have found more bits than what we need */
1822 search_bytes = min(search_bytes, *bytes);
1823
1824 /* Cannot clear past the end of the bitmap */
1825 search_bytes = min(search_bytes, end - search_start + 1);
1826
1827 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1828 *offset += search_bytes;
1829 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001830
1831 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001832 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001833 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001834 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001835
Josef Bacik6606bb92009-07-31 11:03:58 -04001836 /*
1837 * no entry after this bitmap, but we still have bytes to
1838 * remove, so something has gone wrong.
1839 */
1840 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001841 return -EINVAL;
1842
Josef Bacik6606bb92009-07-31 11:03:58 -04001843 bitmap_info = rb_entry(next, struct btrfs_free_space,
1844 offset_index);
1845
1846 /*
1847 * if the next entry isn't a bitmap we need to return to let the
1848 * extent stuff do its work.
1849 */
Josef Bacik96303082009-07-13 21:29:25 -04001850 if (!bitmap_info->bitmap)
1851 return -EAGAIN;
1852
Josef Bacik6606bb92009-07-31 11:03:58 -04001853 /*
1854 * Ok the next item is a bitmap, but it may not actually hold
1855 * the information for the rest of this free space stuff, so
1856 * look for it, and if we don't find it return so we can try
1857 * everything over again.
1858 */
1859 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001860 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001861 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001862 &search_bytes);
1863 if (ret < 0 || search_start != *offset)
1864 return -EAGAIN;
1865
Josef Bacik96303082009-07-13 21:29:25 -04001866 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001867 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001868 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001869
1870 return 0;
1871}
1872
Josef Bacik2cdc3422011-05-27 14:07:49 -04001873static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1874 struct btrfs_free_space *info, u64 offset,
1875 u64 bytes)
1876{
1877 u64 bytes_to_set = 0;
1878 u64 end;
1879
1880 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1881
1882 bytes_to_set = min(end - offset, bytes);
1883
1884 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1885
1886 return bytes_to_set;
1887
1888}
1889
Li Zefan34d52cb2011-03-29 13:46:06 +08001890static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1891 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001892{
Li Zefan34d52cb2011-03-29 13:46:06 +08001893 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001894
1895 /*
1896 * If we are below the extents threshold then we can add this as an
1897 * extent, and don't have to deal with the bitmap
1898 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001899 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001900 /*
1901 * If this block group has some small extents we don't want to
1902 * use up all of our free slots in the cache with them, we want
1903 * to reserve them to larger extents, however if we have plent
1904 * of cache left then go ahead an dadd them, no sense in adding
1905 * the overhead of a bitmap if we don't have to.
1906 */
1907 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001908 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1909 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001910 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001911 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001912 }
1913 }
Josef Bacik96303082009-07-13 21:29:25 -04001914
1915 /*
Josef Bacikdde57402013-02-12 14:07:51 -05001916 * The original block groups from mkfs can be really small, like 8
1917 * megabytes, so don't bother with a bitmap for those entries. However
1918 * some block groups can be smaller than what a bitmap would cover but
1919 * are still large enough that they could overflow the 32k memory limit,
1920 * so allow those block groups to still be allowed to have a bitmap
1921 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04001922 */
Josef Bacikdde57402013-02-12 14:07:51 -05001923 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001924 return false;
1925
1926 return true;
1927}
1928
Josef Bacik2cdc3422011-05-27 14:07:49 -04001929static struct btrfs_free_space_op free_space_op = {
1930 .recalc_thresholds = recalculate_thresholds,
1931 .use_bitmap = use_bitmap,
1932};
1933
Li Zefan34d52cb2011-03-29 13:46:06 +08001934static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1935 struct btrfs_free_space *info)
1936{
1937 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001938 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001939 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001940 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001941 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001942
1943 bytes = info->bytes;
1944 offset = info->offset;
1945
Li Zefan34d52cb2011-03-29 13:46:06 +08001946 if (!ctl->op->use_bitmap(ctl, info))
1947 return 0;
1948
Josef Bacik2cdc3422011-05-27 14:07:49 -04001949 if (ctl->op == &free_space_op)
1950 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001951again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001952 /*
1953 * Since we link bitmaps right into the cluster we need to see if we
1954 * have a cluster here, and if so and it has our bitmap we need to add
1955 * the free space to that bitmap.
1956 */
1957 if (block_group && !list_empty(&block_group->cluster_list)) {
1958 struct btrfs_free_cluster *cluster;
1959 struct rb_node *node;
1960 struct btrfs_free_space *entry;
1961
1962 cluster = list_entry(block_group->cluster_list.next,
1963 struct btrfs_free_cluster,
1964 block_group_list);
1965 spin_lock(&cluster->lock);
1966 node = rb_first(&cluster->root);
1967 if (!node) {
1968 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001969 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001970 }
1971
1972 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1973 if (!entry->bitmap) {
1974 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001975 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001976 }
1977
1978 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1979 bytes_added = add_bytes_to_bitmap(ctl, entry,
1980 offset, bytes);
1981 bytes -= bytes_added;
1982 offset += bytes_added;
1983 }
1984 spin_unlock(&cluster->lock);
1985 if (!bytes) {
1986 ret = 1;
1987 goto out;
1988 }
1989 }
Chris Mason38e87882011-06-10 16:36:57 -04001990
1991no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001992 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001993 1, 0);
1994 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04001995 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04001996 goto new_bitmap;
1997 }
1998
Josef Bacik2cdc3422011-05-27 14:07:49 -04001999 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2000 bytes -= bytes_added;
2001 offset += bytes_added;
2002 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002003
2004 if (!bytes) {
2005 ret = 1;
2006 goto out;
2007 } else
2008 goto again;
2009
2010new_bitmap:
2011 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002012 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002013 added = 1;
2014 info = NULL;
2015 goto again;
2016 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002017 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002018
2019 /* no pre-allocated info, allocate a new one */
2020 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002021 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2022 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002023 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002024 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002025 ret = -ENOMEM;
2026 goto out;
2027 }
2028 }
2029
2030 /* allocate the bitmap */
2031 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002032 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002033 if (!info->bitmap) {
2034 ret = -ENOMEM;
2035 goto out;
2036 }
2037 goto again;
2038 }
2039
2040out:
2041 if (info) {
2042 if (info->bitmap)
2043 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002044 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002045 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002046
2047 return ret;
2048}
2049
Chris Mason945d8962011-05-22 12:33:42 -04002050static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002051 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002052{
Li Zefan120d66e2010-11-09 14:56:50 +08002053 struct btrfs_free_space *left_info;
2054 struct btrfs_free_space *right_info;
2055 bool merged = false;
2056 u64 offset = info->offset;
2057 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002058
Josef Bacik0f9dd462008-09-23 13:14:11 -04002059 /*
2060 * first we want to see if there is free space adjacent to the range we
2061 * are adding, if there is remove that struct and add a new one to
2062 * cover the entire range
2063 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002064 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002065 if (right_info && rb_prev(&right_info->offset_index))
2066 left_info = rb_entry(rb_prev(&right_info->offset_index),
2067 struct btrfs_free_space, offset_index);
2068 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002069 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002070
Josef Bacik96303082009-07-13 21:29:25 -04002071 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002072 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002073 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002074 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002075 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002076 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002077 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002078 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002079 }
2080
Josef Bacik96303082009-07-13 21:29:25 -04002081 if (left_info && !left_info->bitmap &&
2082 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002083 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002084 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002085 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002086 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002087 info->offset = left_info->offset;
2088 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002089 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002090 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002091 }
2092
Li Zefan120d66e2010-11-09 14:56:50 +08002093 return merged;
2094}
2095
Filipe Manana20005522014-08-29 13:35:13 +01002096static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2097 struct btrfs_free_space *info,
2098 bool update_stat)
2099{
2100 struct btrfs_free_space *bitmap;
2101 unsigned long i;
2102 unsigned long j;
2103 const u64 end = info->offset + info->bytes;
2104 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2105 u64 bytes;
2106
2107 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2108 if (!bitmap)
2109 return false;
2110
2111 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2112 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2113 if (j == i)
2114 return false;
2115 bytes = (j - i) * ctl->unit;
2116 info->bytes += bytes;
2117
2118 if (update_stat)
2119 bitmap_clear_bits(ctl, bitmap, end, bytes);
2120 else
2121 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2122
2123 if (!bitmap->bytes)
2124 free_bitmap(ctl, bitmap);
2125
2126 return true;
2127}
2128
2129static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2130 struct btrfs_free_space *info,
2131 bool update_stat)
2132{
2133 struct btrfs_free_space *bitmap;
2134 u64 bitmap_offset;
2135 unsigned long i;
2136 unsigned long j;
2137 unsigned long prev_j;
2138 u64 bytes;
2139
2140 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2141 /* If we're on a boundary, try the previous logical bitmap. */
2142 if (bitmap_offset == info->offset) {
2143 if (info->offset == 0)
2144 return false;
2145 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2146 }
2147
2148 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2149 if (!bitmap)
2150 return false;
2151
2152 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2153 j = 0;
2154 prev_j = (unsigned long)-1;
2155 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2156 if (j > i)
2157 break;
2158 prev_j = j;
2159 }
2160 if (prev_j == i)
2161 return false;
2162
2163 if (prev_j == (unsigned long)-1)
2164 bytes = (i + 1) * ctl->unit;
2165 else
2166 bytes = (i - prev_j) * ctl->unit;
2167
2168 info->offset -= bytes;
2169 info->bytes += bytes;
2170
2171 if (update_stat)
2172 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2173 else
2174 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2175
2176 if (!bitmap->bytes)
2177 free_bitmap(ctl, bitmap);
2178
2179 return true;
2180}
2181
2182/*
2183 * We prefer always to allocate from extent entries, both for clustered and
2184 * non-clustered allocation requests. So when attempting to add a new extent
2185 * entry, try to see if there's adjacent free space in bitmap entries, and if
2186 * there is, migrate that space from the bitmaps to the extent.
2187 * Like this we get better chances of satisfying space allocation requests
2188 * because we attempt to satisfy them based on a single cache entry, and never
2189 * on 2 or more entries - even if the entries represent a contiguous free space
2190 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2191 * ends).
2192 */
2193static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2194 struct btrfs_free_space *info,
2195 bool update_stat)
2196{
2197 /*
2198 * Only work with disconnected entries, as we can change their offset,
2199 * and must be extent entries.
2200 */
2201 ASSERT(!info->bitmap);
2202 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2203
2204 if (ctl->total_bitmaps > 0) {
2205 bool stole_end;
2206 bool stole_front = false;
2207
2208 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2209 if (ctl->total_bitmaps > 0)
2210 stole_front = steal_from_bitmap_to_front(ctl, info,
2211 update_stat);
2212
2213 if (stole_end || stole_front)
2214 try_merge_free_space(ctl, info, update_stat);
2215 }
2216}
2217
Li Zefan581bb052011-04-20 10:06:11 +08002218int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
2219 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002220{
2221 struct btrfs_free_space *info;
2222 int ret = 0;
2223
Josef Bacikdc89e982011-01-28 17:05:48 -05002224 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002225 if (!info)
2226 return -ENOMEM;
2227
2228 info->offset = offset;
2229 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002230 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002231
Li Zefan34d52cb2011-03-29 13:46:06 +08002232 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002233
Li Zefan34d52cb2011-03-29 13:46:06 +08002234 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002235 goto link;
2236
2237 /*
2238 * There was no extent directly to the left or right of this new
2239 * extent then we know we're going to have to allocate a new extent, so
2240 * before we do that see if we need to drop this into a bitmap
2241 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002242 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002243 if (ret < 0) {
2244 goto out;
2245 } else if (ret) {
2246 ret = 0;
2247 goto out;
2248 }
2249link:
Filipe Manana20005522014-08-29 13:35:13 +01002250 /*
2251 * Only steal free space from adjacent bitmaps if we're sure we're not
2252 * going to add the new free space to existing bitmap entries - because
2253 * that would mean unnecessary work that would be reverted. Therefore
2254 * attempt to steal space from bitmaps if we're adding an extent entry.
2255 */
2256 steal_from_bitmap(ctl, info, true);
2257
Li Zefan34d52cb2011-03-29 13:46:06 +08002258 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002259 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002260 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002261out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002262 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002263
Josef Bacik0f9dd462008-09-23 13:14:11 -04002264 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002265 printk(KERN_CRIT "BTRFS: unable to add free space :%d\n", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002266 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002267 }
2268
Josef Bacik0f9dd462008-09-23 13:14:11 -04002269 return ret;
2270}
2271
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002272int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2273 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002274{
Li Zefan34d52cb2011-03-29 13:46:06 +08002275 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002276 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002277 int ret;
2278 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002279
Li Zefan34d52cb2011-03-29 13:46:06 +08002280 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002281
Josef Bacik96303082009-07-13 21:29:25 -04002282again:
Josef Bacikb0175112012-12-18 11:39:19 -05002283 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002284 if (!bytes)
2285 goto out_lock;
2286
Li Zefan34d52cb2011-03-29 13:46:06 +08002287 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002288 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002289 /*
2290 * oops didn't find an extent that matched the space we wanted
2291 * to remove, look for a bitmap instead
2292 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002293 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002294 1, 0);
2295 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002296 /*
2297 * If we found a partial bit of our free space in a
2298 * bitmap but then couldn't find the other part this may
2299 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002300 */
Josef Bacikb0175112012-12-18 11:39:19 -05002301 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002302 goto out_lock;
2303 }
Josef Bacik96303082009-07-13 21:29:25 -04002304 }
2305
Josef Bacikb0175112012-12-18 11:39:19 -05002306 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002307 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002308 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002309 if (offset == info->offset) {
2310 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002311
Josef Bacikbdb7d302012-06-27 15:10:56 -04002312 info->bytes -= to_free;
2313 info->offset += to_free;
2314 if (info->bytes) {
2315 ret = link_free_space(ctl, info);
2316 WARN_ON(ret);
2317 } else {
2318 kmem_cache_free(btrfs_free_space_cachep, info);
2319 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002320
Josef Bacikbdb7d302012-06-27 15:10:56 -04002321 offset += to_free;
2322 bytes -= to_free;
2323 goto again;
2324 } else {
2325 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002326
Josef Bacikbdb7d302012-06-27 15:10:56 -04002327 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002328 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002329 WARN_ON(ret);
2330 if (ret)
2331 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002332
Josef Bacikbdb7d302012-06-27 15:10:56 -04002333 /* Not enough bytes in this entry to satisfy us */
2334 if (old_end < offset + bytes) {
2335 bytes -= old_end - offset;
2336 offset = old_end;
2337 goto again;
2338 } else if (old_end == offset + bytes) {
2339 /* all done */
2340 goto out_lock;
2341 }
2342 spin_unlock(&ctl->tree_lock);
2343
2344 ret = btrfs_add_free_space(block_group, offset + bytes,
2345 old_end - (offset + bytes));
2346 WARN_ON(ret);
2347 goto out;
2348 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002349 }
Josef Bacik96303082009-07-13 21:29:25 -04002350
Li Zefan34d52cb2011-03-29 13:46:06 +08002351 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002352 if (ret == -EAGAIN) {
2353 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002354 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002355 }
Josef Bacik96303082009-07-13 21:29:25 -04002356out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002357 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002358out:
Josef Bacik25179202008-10-29 14:49:05 -04002359 return ret;
2360}
2361
Josef Bacik0f9dd462008-09-23 13:14:11 -04002362void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2363 u64 bytes)
2364{
Li Zefan34d52cb2011-03-29 13:46:06 +08002365 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002366 struct btrfs_free_space *info;
2367 struct rb_node *n;
2368 int count = 0;
2369
Li Zefan34d52cb2011-03-29 13:46:06 +08002370 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002371 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002372 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002373 count++;
Frank Holtonefe120a2013-12-20 11:37:06 -05002374 btrfs_crit(block_group->fs_info,
2375 "entry offset %llu, bytes %llu, bitmap %s",
2376 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002377 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002378 }
Frank Holtonefe120a2013-12-20 11:37:06 -05002379 btrfs_info(block_group->fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002380 list_empty(&block_group->cluster_list) ? "no" : "yes");
Frank Holtonefe120a2013-12-20 11:37:06 -05002381 btrfs_info(block_group->fs_info,
2382 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002383}
2384
Li Zefan34d52cb2011-03-29 13:46:06 +08002385void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002386{
Li Zefan34d52cb2011-03-29 13:46:06 +08002387 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002388
Li Zefan34d52cb2011-03-29 13:46:06 +08002389 spin_lock_init(&ctl->tree_lock);
2390 ctl->unit = block_group->sectorsize;
2391 ctl->start = block_group->key.objectid;
2392 ctl->private = block_group;
2393 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002394 INIT_LIST_HEAD(&ctl->trimming_ranges);
2395 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002396
Li Zefan34d52cb2011-03-29 13:46:06 +08002397 /*
2398 * we only want to have 32k of ram per block group for keeping
2399 * track of free space, and if we pass 1/2 of that we want to
2400 * start converting things over to using bitmaps
2401 */
2402 ctl->extents_thresh = ((1024 * 32) / 2) /
2403 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002404}
2405
Chris Masonfa9c0d792009-04-03 09:47:43 -04002406/*
2407 * for a given cluster, put all of its extents back into the free
2408 * space cache. If the block group passed doesn't match the block group
2409 * pointed to by the cluster, someone else raced in and freed the
2410 * cluster already. In that case, we just return without changing anything
2411 */
2412static int
2413__btrfs_return_cluster_to_free_space(
2414 struct btrfs_block_group_cache *block_group,
2415 struct btrfs_free_cluster *cluster)
2416{
Li Zefan34d52cb2011-03-29 13:46:06 +08002417 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002418 struct btrfs_free_space *entry;
2419 struct rb_node *node;
2420
2421 spin_lock(&cluster->lock);
2422 if (cluster->block_group != block_group)
2423 goto out;
2424
Josef Bacik96303082009-07-13 21:29:25 -04002425 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002426 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002427 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002428
Chris Masonfa9c0d792009-04-03 09:47:43 -04002429 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002430 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002431 bool bitmap;
2432
Chris Masonfa9c0d792009-04-03 09:47:43 -04002433 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2434 node = rb_next(&entry->offset_index);
2435 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002436 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002437
2438 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002439 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002440 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002441 steal_from_bitmap(ctl, entry, false);
2442 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002443 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002444 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002445 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002446 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002447
Chris Masonfa9c0d792009-04-03 09:47:43 -04002448out:
2449 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002450 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002451 return 0;
2452}
2453
Eric Sandeen48a3b632013-04-25 20:41:01 +00002454static void __btrfs_remove_free_space_cache_locked(
2455 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002456{
2457 struct btrfs_free_space *info;
2458 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002459
Li Zefan581bb052011-04-20 10:06:11 +08002460 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2461 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002462 if (!info->bitmap) {
2463 unlink_free_space(ctl, info);
2464 kmem_cache_free(btrfs_free_space_cachep, info);
2465 } else {
2466 free_bitmap(ctl, info);
2467 }
David Sterba351810c2015-01-08 15:20:54 +01002468
2469 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002470 }
Chris Mason09655372011-05-21 09:27:38 -04002471}
2472
2473void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2474{
2475 spin_lock(&ctl->tree_lock);
2476 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002477 spin_unlock(&ctl->tree_lock);
2478}
2479
Josef Bacik0f9dd462008-09-23 13:14:11 -04002480void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2481{
Li Zefan34d52cb2011-03-29 13:46:06 +08002482 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002483 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002484 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002485
Li Zefan34d52cb2011-03-29 13:46:06 +08002486 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002487 while ((head = block_group->cluster_list.next) !=
2488 &block_group->cluster_list) {
2489 cluster = list_entry(head, struct btrfs_free_cluster,
2490 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002491
2492 WARN_ON(cluster->block_group != block_group);
2493 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002494
2495 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002496 }
Chris Mason09655372011-05-21 09:27:38 -04002497 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002498 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002499
Josef Bacik0f9dd462008-09-23 13:14:11 -04002500}
2501
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002502u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002503 u64 offset, u64 bytes, u64 empty_size,
2504 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002505{
Li Zefan34d52cb2011-03-29 13:46:06 +08002506 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002507 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002508 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002509 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002510 u64 align_gap = 0;
2511 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002512
Li Zefan34d52cb2011-03-29 13:46:06 +08002513 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002514 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002515 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002516 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002517 goto out;
2518
2519 ret = offset;
2520 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002521 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002522 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002523 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002524 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002525 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002526 align_gap_len = offset - entry->offset;
2527 align_gap = entry->offset;
2528
2529 entry->offset = offset + bytes;
2530 WARN_ON(entry->bytes < bytes + align_gap_len);
2531
2532 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002533 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002534 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002535 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002536 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002537 }
Josef Bacik96303082009-07-13 21:29:25 -04002538out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002539 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002540
David Woodhouse53b381b2013-01-29 18:40:14 -05002541 if (align_gap_len)
2542 __btrfs_add_free_space(ctl, align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002543 return ret;
2544}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002545
2546/*
2547 * given a cluster, put all of its extents back into the free space
2548 * cache. If a block group is passed, this function will only free
2549 * a cluster that belongs to the passed block group.
2550 *
2551 * Otherwise, it'll get a reference on the block group pointed to by the
2552 * cluster and remove the cluster from it.
2553 */
2554int btrfs_return_cluster_to_free_space(
2555 struct btrfs_block_group_cache *block_group,
2556 struct btrfs_free_cluster *cluster)
2557{
Li Zefan34d52cb2011-03-29 13:46:06 +08002558 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002559 int ret;
2560
2561 /* first, get a safe pointer to the block group */
2562 spin_lock(&cluster->lock);
2563 if (!block_group) {
2564 block_group = cluster->block_group;
2565 if (!block_group) {
2566 spin_unlock(&cluster->lock);
2567 return 0;
2568 }
2569 } else if (cluster->block_group != block_group) {
2570 /* someone else has already freed it don't redo their work */
2571 spin_unlock(&cluster->lock);
2572 return 0;
2573 }
2574 atomic_inc(&block_group->count);
2575 spin_unlock(&cluster->lock);
2576
Li Zefan34d52cb2011-03-29 13:46:06 +08002577 ctl = block_group->free_space_ctl;
2578
Chris Masonfa9c0d792009-04-03 09:47:43 -04002579 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002580 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002581 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002582 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002583
2584 /* finally drop our ref */
2585 btrfs_put_block_group(block_group);
2586 return ret;
2587}
2588
Josef Bacik96303082009-07-13 21:29:25 -04002589static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2590 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002591 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002592 u64 bytes, u64 min_start,
2593 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002594{
Li Zefan34d52cb2011-03-29 13:46:06 +08002595 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002596 int err;
2597 u64 search_start = cluster->window_start;
2598 u64 search_bytes = bytes;
2599 u64 ret = 0;
2600
Josef Bacik96303082009-07-13 21:29:25 -04002601 search_start = min_start;
2602 search_bytes = bytes;
2603
Li Zefan34d52cb2011-03-29 13:46:06 +08002604 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Miao Xiea4820392013-09-09 13:19:42 +08002605 if (err) {
2606 if (search_bytes > *max_extent_size)
2607 *max_extent_size = search_bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002608 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002609 }
Josef Bacik96303082009-07-13 21:29:25 -04002610
2611 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002612 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002613
2614 return ret;
2615}
2616
Chris Masonfa9c0d792009-04-03 09:47:43 -04002617/*
2618 * given a cluster, try to allocate 'bytes' from it, returns 0
2619 * if it couldn't find anything suitably large, or a logical disk offset
2620 * if things worked out
2621 */
2622u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2623 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002624 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002625{
Li Zefan34d52cb2011-03-29 13:46:06 +08002626 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002627 struct btrfs_free_space *entry = NULL;
2628 struct rb_node *node;
2629 u64 ret = 0;
2630
2631 spin_lock(&cluster->lock);
2632 if (bytes > cluster->max_size)
2633 goto out;
2634
2635 if (cluster->block_group != block_group)
2636 goto out;
2637
2638 node = rb_first(&cluster->root);
2639 if (!node)
2640 goto out;
2641
2642 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302643 while (1) {
Miao Xiea4820392013-09-09 13:19:42 +08002644 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2645 *max_extent_size = entry->bytes;
2646
Josef Bacik4e69b592011-03-21 10:11:24 -04002647 if (entry->bytes < bytes ||
2648 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002649 node = rb_next(&entry->offset_index);
2650 if (!node)
2651 break;
2652 entry = rb_entry(node, struct btrfs_free_space,
2653 offset_index);
2654 continue;
2655 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002656
Josef Bacik4e69b592011-03-21 10:11:24 -04002657 if (entry->bitmap) {
2658 ret = btrfs_alloc_from_bitmap(block_group,
2659 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002660 cluster->window_start,
2661 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002662 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002663 node = rb_next(&entry->offset_index);
2664 if (!node)
2665 break;
2666 entry = rb_entry(node, struct btrfs_free_space,
2667 offset_index);
2668 continue;
2669 }
Josef Bacik9b230622012-01-26 15:01:12 -05002670 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002671 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002672 ret = entry->offset;
2673
2674 entry->offset += bytes;
2675 entry->bytes -= bytes;
2676 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002677
Li Zefan5e71b5d2010-11-09 14:55:34 +08002678 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002679 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002680 break;
2681 }
2682out:
2683 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002684
Li Zefan5e71b5d2010-11-09 14:55:34 +08002685 if (!ret)
2686 return 0;
2687
Li Zefan34d52cb2011-03-29 13:46:06 +08002688 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002689
Li Zefan34d52cb2011-03-29 13:46:06 +08002690 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002691 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002692 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002693 if (entry->bitmap) {
2694 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002695 ctl->total_bitmaps--;
2696 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002697 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002698 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002699 }
2700
Li Zefan34d52cb2011-03-29 13:46:06 +08002701 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002702
Chris Masonfa9c0d792009-04-03 09:47:43 -04002703 return ret;
2704}
2705
Josef Bacik96303082009-07-13 21:29:25 -04002706static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2707 struct btrfs_free_space *entry,
2708 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002709 u64 offset, u64 bytes,
2710 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002711{
Li Zefan34d52cb2011-03-29 13:46:06 +08002712 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002713 unsigned long next_zero;
2714 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002715 unsigned long want_bits;
2716 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002717 unsigned long found_bits;
2718 unsigned long start = 0;
2719 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002720 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002721
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002722 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002723 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002724 want_bits = bytes_to_bits(bytes, ctl->unit);
2725 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002726
2727again:
2728 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002729 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002730 next_zero = find_next_zero_bit(entry->bitmap,
2731 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002732 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002733 found_bits = next_zero - i;
2734 break;
2735 }
2736 i = next_zero;
2737 }
2738
2739 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002740 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002741
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002742 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002743 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002744 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002745 }
2746
2747 total_found += found_bits;
2748
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002749 if (cluster->max_size < found_bits * ctl->unit)
2750 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002751
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002752 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2753 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002754 goto again;
2755 }
2756
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002757 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002758 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002759 ret = tree_insert_offset(&cluster->root, entry->offset,
2760 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002761 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002762
Josef Bacik3f7de032011-11-10 08:29:20 -05002763 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002764 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002765 return 0;
2766}
2767
Chris Masonfa9c0d792009-04-03 09:47:43 -04002768/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002769 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002770 * Try to find a cluster with at least bytes total bytes, at least one
2771 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002772 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002773static noinline int
2774setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2775 struct btrfs_free_cluster *cluster,
2776 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002777 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002778{
Li Zefan34d52cb2011-03-29 13:46:06 +08002779 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002780 struct btrfs_free_space *first = NULL;
2781 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002782 struct btrfs_free_space *last;
2783 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002784 u64 window_free;
2785 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002786 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002787
Li Zefan34d52cb2011-03-29 13:46:06 +08002788 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002789 if (!entry)
2790 return -ENOSPC;
2791
2792 /*
2793 * We don't want bitmaps, so just move along until we find a normal
2794 * extent entry.
2795 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002796 while (entry->bitmap || entry->bytes < min_bytes) {
2797 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002798 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002799 node = rb_next(&entry->offset_index);
2800 if (!node)
2801 return -ENOSPC;
2802 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2803 }
2804
Josef Bacik4e69b592011-03-21 10:11:24 -04002805 window_free = entry->bytes;
2806 max_extent = entry->bytes;
2807 first = entry;
2808 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002809
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002810 for (node = rb_next(&entry->offset_index); node;
2811 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002812 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2813
Josef Bacik86d4a772011-05-25 13:03:16 -04002814 if (entry->bitmap) {
2815 if (list_empty(&entry->list))
2816 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002817 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002818 }
2819
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002820 if (entry->bytes < min_bytes)
2821 continue;
2822
2823 last = entry;
2824 window_free += entry->bytes;
2825 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002826 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002827 }
2828
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002829 if (window_free < bytes || max_extent < cont1_bytes)
2830 return -ENOSPC;
2831
Josef Bacik4e69b592011-03-21 10:11:24 -04002832 cluster->window_start = first->offset;
2833
2834 node = &first->offset_index;
2835
2836 /*
2837 * now we've found our entries, pull them out of the free space
2838 * cache and put them into the cluster rbtree
2839 */
2840 do {
2841 int ret;
2842
2843 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2844 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002845 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002846 continue;
2847
Li Zefan34d52cb2011-03-29 13:46:06 +08002848 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002849 ret = tree_insert_offset(&cluster->root, entry->offset,
2850 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002851 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002852 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002853 } while (node && entry != last);
2854
2855 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002856 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002857 return 0;
2858}
2859
2860/*
2861 * This specifically looks for bitmaps that may work in the cluster, we assume
2862 * that we have already failed to find extents that will work.
2863 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002864static noinline int
2865setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2866 struct btrfs_free_cluster *cluster,
2867 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002868 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002869{
Li Zefan34d52cb2011-03-29 13:46:06 +08002870 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002871 struct btrfs_free_space *entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002872 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002873 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002874
Li Zefan34d52cb2011-03-29 13:46:06 +08002875 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002876 return -ENOSPC;
2877
Josef Bacik86d4a772011-05-25 13:03:16 -04002878 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002879 * The bitmap that covers offset won't be in the list unless offset
2880 * is just its start offset.
2881 */
2882 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2883 if (entry->offset != bitmap_offset) {
2884 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2885 if (entry && list_empty(&entry->list))
2886 list_add(&entry->list, bitmaps);
2887 }
2888
Josef Bacik86d4a772011-05-25 13:03:16 -04002889 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05002890 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04002891 continue;
2892 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002893 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002894 if (!ret)
2895 return 0;
2896 }
2897
2898 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002899 * The bitmaps list has all the bitmaps that record free space
2900 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002901 */
Li Zefan52621cb2011-11-20 07:33:38 -05002902 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002903}
2904
2905/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002906 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002907 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04002908 * We might not find them all in one contiguous area.
2909 *
2910 * returns zero and sets up cluster if things worked out, otherwise
2911 * it returns -enospc
2912 */
Josef Bacik00361582013-08-14 14:02:47 -04002913int btrfs_find_space_cluster(struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002914 struct btrfs_block_group_cache *block_group,
2915 struct btrfs_free_cluster *cluster,
2916 u64 offset, u64 bytes, u64 empty_size)
2917{
Li Zefan34d52cb2011-03-29 13:46:06 +08002918 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002919 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05002920 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002921 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002922 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002923 int ret;
2924
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002925 /*
2926 * Choose the minimum extent size we'll require for this
2927 * cluster. For SSD_SPREAD, don't allow any fragmentation.
2928 * For metadata, allow allocates with smaller extents. For
2929 * data, keep it dense.
2930 */
Chris Mason451d7582009-06-09 20:28:34 -04002931 if (btrfs_test_opt(root, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002932 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04002933 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002934 cont1_bytes = bytes;
2935 min_bytes = block_group->sectorsize;
2936 } else {
2937 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
2938 min_bytes = block_group->sectorsize;
2939 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002940
Li Zefan34d52cb2011-03-29 13:46:06 +08002941 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002942
2943 /*
2944 * If we know we don't have enough space to make a cluster don't even
2945 * bother doing all the work to try and find one.
2946 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002947 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002948 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002949 return -ENOSPC;
2950 }
2951
Chris Masonfa9c0d792009-04-03 09:47:43 -04002952 spin_lock(&cluster->lock);
2953
2954 /* someone already found a cluster, hooray */
2955 if (cluster->block_group) {
2956 ret = 0;
2957 goto out;
2958 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002959
Josef Bacik3f7de032011-11-10 08:29:20 -05002960 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
2961 min_bytes);
2962
Josef Bacik86d4a772011-05-25 13:03:16 -04002963 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002964 bytes + empty_size,
2965 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002966 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002967 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002968 offset, bytes + empty_size,
2969 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002970
2971 /* Clear our temporary list */
2972 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2973 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002974
2975 if (!ret) {
2976 atomic_inc(&block_group->count);
2977 list_add_tail(&cluster->block_group_list,
2978 &block_group->cluster_list);
2979 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05002980 } else {
2981 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002982 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002983out:
2984 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002985 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002986
2987 return ret;
2988}
2989
2990/*
2991 * simple code to zero out a cluster
2992 */
2993void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2994{
2995 spin_lock_init(&cluster->lock);
2996 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002997 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002998 cluster->max_size = 0;
2999 INIT_LIST_HEAD(&cluster->block_group_list);
3000 cluster->block_group = NULL;
3001}
3002
Li Zefan7fe1e642011-12-29 14:47:27 +08003003static int do_trimming(struct btrfs_block_group_cache *block_group,
3004 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003005 u64 reserved_start, u64 reserved_bytes,
3006 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003007{
3008 struct btrfs_space_info *space_info = block_group->space_info;
3009 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003010 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003011 int ret;
3012 int update = 0;
3013 u64 trimmed = 0;
3014
3015 spin_lock(&space_info->lock);
3016 spin_lock(&block_group->lock);
3017 if (!block_group->ro) {
3018 block_group->reserved += reserved_bytes;
3019 space_info->bytes_reserved += reserved_bytes;
3020 update = 1;
3021 }
3022 spin_unlock(&block_group->lock);
3023 spin_unlock(&space_info->lock);
3024
Filipe Manana1edb647b2014-12-08 14:01:12 +00003025 ret = btrfs_discard_extent(fs_info->extent_root,
3026 start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003027 if (!ret)
3028 *total_trimmed += trimmed;
3029
Filipe Manana55507ce2014-12-01 17:04:09 +00003030 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003031 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003032 list_del(&trim_entry->list);
3033 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003034
3035 if (update) {
3036 spin_lock(&space_info->lock);
3037 spin_lock(&block_group->lock);
3038 if (block_group->ro)
3039 space_info->bytes_readonly += reserved_bytes;
3040 block_group->reserved -= reserved_bytes;
3041 space_info->bytes_reserved -= reserved_bytes;
3042 spin_unlock(&space_info->lock);
3043 spin_unlock(&block_group->lock);
3044 }
3045
3046 return ret;
3047}
3048
3049static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3050 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003051{
Li Zefan34d52cb2011-03-29 13:46:06 +08003052 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003053 struct btrfs_free_space *entry;
3054 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003055 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003056 u64 extent_start;
3057 u64 extent_bytes;
3058 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003059
3060 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003061 struct btrfs_trim_range trim_entry;
3062
3063 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003064 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003065
Li Zefan34d52cb2011-03-29 13:46:06 +08003066 if (ctl->free_space < minlen) {
3067 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003068 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003069 break;
3070 }
3071
Li Zefan34d52cb2011-03-29 13:46:06 +08003072 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003073 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003074 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003075 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003076 break;
3077 }
3078
Li Zefan7fe1e642011-12-29 14:47:27 +08003079 /* skip bitmaps */
3080 while (entry->bitmap) {
3081 node = rb_next(&entry->offset_index);
3082 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003083 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003084 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003085 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003086 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003087 entry = rb_entry(node, struct btrfs_free_space,
3088 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003089 }
3090
Li Zefan7fe1e642011-12-29 14:47:27 +08003091 if (entry->offset >= end) {
3092 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003093 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003094 break;
3095 }
3096
3097 extent_start = entry->offset;
3098 extent_bytes = entry->bytes;
3099 start = max(start, extent_start);
3100 bytes = min(extent_start + extent_bytes, end) - start;
3101 if (bytes < minlen) {
3102 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003103 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003104 goto next;
3105 }
3106
3107 unlink_free_space(ctl, entry);
3108 kmem_cache_free(btrfs_free_space_cachep, entry);
3109
Li Zefan34d52cb2011-03-29 13:46:06 +08003110 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003111 trim_entry.start = extent_start;
3112 trim_entry.bytes = extent_bytes;
3113 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3114 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003115
Li Zefan7fe1e642011-12-29 14:47:27 +08003116 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003117 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003118 if (ret)
3119 break;
3120next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003121 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003122
3123 if (fatal_signal_pending(current)) {
3124 ret = -ERESTARTSYS;
3125 break;
3126 }
3127
3128 cond_resched();
3129 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003130out:
3131 return ret;
3132}
3133
3134static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3135 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3136{
3137 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3138 struct btrfs_free_space *entry;
3139 int ret = 0;
3140 int ret2;
3141 u64 bytes;
3142 u64 offset = offset_to_bitmap(ctl, start);
3143
3144 while (offset < end) {
3145 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003146 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003147
Filipe Manana55507ce2014-12-01 17:04:09 +00003148 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003149 spin_lock(&ctl->tree_lock);
3150
3151 if (ctl->free_space < minlen) {
3152 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003153 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003154 break;
3155 }
3156
3157 entry = tree_search_offset(ctl, offset, 1, 0);
3158 if (!entry) {
3159 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003160 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003161 next_bitmap = true;
3162 goto next;
3163 }
3164
3165 bytes = minlen;
3166 ret2 = search_bitmap(ctl, entry, &start, &bytes);
3167 if (ret2 || start >= end) {
3168 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003169 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003170 next_bitmap = true;
3171 goto next;
3172 }
3173
3174 bytes = min(bytes, end - start);
3175 if (bytes < minlen) {
3176 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003177 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003178 goto next;
3179 }
3180
3181 bitmap_clear_bits(ctl, entry, start, bytes);
3182 if (entry->bytes == 0)
3183 free_bitmap(ctl, entry);
3184
3185 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003186 trim_entry.start = start;
3187 trim_entry.bytes = bytes;
3188 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3189 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003190
3191 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003192 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003193 if (ret)
3194 break;
3195next:
3196 if (next_bitmap) {
3197 offset += BITS_PER_BITMAP * ctl->unit;
3198 } else {
3199 start += bytes;
3200 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3201 offset += BITS_PER_BITMAP * ctl->unit;
3202 }
3203
3204 if (fatal_signal_pending(current)) {
3205 ret = -ERESTARTSYS;
3206 break;
3207 }
3208
3209 cond_resched();
3210 }
3211
3212 return ret;
3213}
3214
3215int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3216 u64 *trimmed, u64 start, u64 end, u64 minlen)
3217{
3218 int ret;
3219
3220 *trimmed = 0;
3221
Filipe Manana04216822014-11-27 21:14:15 +00003222 spin_lock(&block_group->lock);
3223 if (block_group->removed) {
3224 spin_unlock(&block_group->lock);
3225 return 0;
3226 }
3227 atomic_inc(&block_group->trimming);
3228 spin_unlock(&block_group->lock);
3229
Li Zefan7fe1e642011-12-29 14:47:27 +08003230 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3231 if (ret)
Filipe Manana04216822014-11-27 21:14:15 +00003232 goto out;
Li Zefan7fe1e642011-12-29 14:47:27 +08003233
3234 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
Filipe Manana04216822014-11-27 21:14:15 +00003235out:
3236 spin_lock(&block_group->lock);
3237 if (atomic_dec_and_test(&block_group->trimming) &&
3238 block_group->removed) {
3239 struct extent_map_tree *em_tree;
3240 struct extent_map *em;
3241
3242 spin_unlock(&block_group->lock);
3243
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003244 lock_chunks(block_group->fs_info->chunk_root);
Filipe Manana04216822014-11-27 21:14:15 +00003245 em_tree = &block_group->fs_info->mapping_tree.map_tree;
3246 write_lock(&em_tree->lock);
3247 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3248 1);
3249 BUG_ON(!em); /* logic error, can't happen */
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003250 /*
3251 * remove_extent_mapping() will delete us from the pinned_chunks
3252 * list, which is protected by the chunk mutex.
3253 */
Filipe Manana04216822014-11-27 21:14:15 +00003254 remove_extent_mapping(em_tree, em);
3255 write_unlock(&em_tree->lock);
Filipe Manana04216822014-11-27 21:14:15 +00003256 unlock_chunks(block_group->fs_info->chunk_root);
3257
3258 /* once for us and once for the tree */
3259 free_extent_map(em);
3260 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003261
3262 /*
3263 * We've left one free space entry and other tasks trimming
3264 * this block group have left 1 entry each one. Free them.
3265 */
3266 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003267 } else {
3268 spin_unlock(&block_group->lock);
3269 }
Li Dongyangf7039b12011-03-24 10:24:28 +00003270
3271 return ret;
3272}
Li Zefan581bb052011-04-20 10:06:11 +08003273
3274/*
3275 * Find the left-most item in the cache tree, and then return the
3276 * smallest inode number in the item.
3277 *
3278 * Note: the returned inode number may not be the smallest one in
3279 * the tree, if the left-most item is a bitmap.
3280 */
3281u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3282{
3283 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3284 struct btrfs_free_space *entry = NULL;
3285 u64 ino = 0;
3286
3287 spin_lock(&ctl->tree_lock);
3288
3289 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3290 goto out;
3291
3292 entry = rb_entry(rb_first(&ctl->free_space_offset),
3293 struct btrfs_free_space, offset_index);
3294
3295 if (!entry->bitmap) {
3296 ino = entry->offset;
3297
3298 unlink_free_space(ctl, entry);
3299 entry->offset++;
3300 entry->bytes--;
3301 if (!entry->bytes)
3302 kmem_cache_free(btrfs_free_space_cachep, entry);
3303 else
3304 link_free_space(ctl, entry);
3305 } else {
3306 u64 offset = 0;
3307 u64 count = 1;
3308 int ret;
3309
3310 ret = search_bitmap(ctl, entry, &offset, &count);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003311 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003312 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003313
3314 ino = offset;
3315 bitmap_clear_bits(ctl, entry, offset, 1);
3316 if (entry->bytes == 0)
3317 free_bitmap(ctl, entry);
3318 }
3319out:
3320 spin_unlock(&ctl->tree_lock);
3321
3322 return ino;
3323}
Li Zefan82d59022011-04-20 10:33:24 +08003324
3325struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3326 struct btrfs_path *path)
3327{
3328 struct inode *inode = NULL;
3329
David Sterba57cdc8d2014-02-05 02:37:48 +01003330 spin_lock(&root->ino_cache_lock);
3331 if (root->ino_cache_inode)
3332 inode = igrab(root->ino_cache_inode);
3333 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003334 if (inode)
3335 return inode;
3336
3337 inode = __lookup_free_space_inode(root, path, 0);
3338 if (IS_ERR(inode))
3339 return inode;
3340
David Sterba57cdc8d2014-02-05 02:37:48 +01003341 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003342 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003343 root->ino_cache_inode = igrab(inode);
3344 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003345
3346 return inode;
3347}
3348
3349int create_free_ino_inode(struct btrfs_root *root,
3350 struct btrfs_trans_handle *trans,
3351 struct btrfs_path *path)
3352{
3353 return __create_free_space_inode(root, trans, path,
3354 BTRFS_FREE_INO_OBJECTID, 0);
3355}
3356
3357int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3358{
3359 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3360 struct btrfs_path *path;
3361 struct inode *inode;
3362 int ret = 0;
3363 u64 root_gen = btrfs_root_generation(&root->root_item);
3364
Chris Mason4b9465c2011-06-03 09:36:29 -04003365 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3366 return 0;
3367
Li Zefan82d59022011-04-20 10:33:24 +08003368 /*
3369 * If we're unmounting then just return, since this does a search on the
3370 * normal root and not the commit root and we could deadlock.
3371 */
David Sterba7841cb22011-05-31 18:07:27 +02003372 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003373 return 0;
3374
3375 path = btrfs_alloc_path();
3376 if (!path)
3377 return 0;
3378
3379 inode = lookup_free_ino_inode(root, path);
3380 if (IS_ERR(inode))
3381 goto out;
3382
3383 if (root_gen != BTRFS_I(inode)->generation)
3384 goto out_put;
3385
3386 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3387
3388 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003389 btrfs_err(fs_info,
3390 "failed to load free ino cache for root %llu",
3391 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003392out_put:
3393 iput(inode);
3394out:
3395 btrfs_free_path(path);
3396 return ret;
3397}
3398
3399int btrfs_write_out_ino_cache(struct btrfs_root *root,
3400 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003401 struct btrfs_path *path,
3402 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003403{
3404 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003405 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003406 struct btrfs_io_ctl io_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003407
Chris Mason4b9465c2011-06-03 09:36:29 -04003408 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3409 return 0;
3410
Chris Masonc9dc4c62015-04-04 17:14:42 -07003411 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl,
3412 trans, path, 0) ||
3413 btrfs_wait_cache_io(root, trans, NULL, &io_ctl, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04003414 if (ret) {
3415 btrfs_delalloc_release_metadata(inode, inode->i_size);
3416#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003417 btrfs_err(root->fs_info,
3418 "failed to write free ino cache for root %llu",
3419 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003420#endif
3421 }
Li Zefan82d59022011-04-20 10:33:24 +08003422
Li Zefan82d59022011-04-20 10:33:24 +08003423 return ret;
3424}
Josef Bacik74255aa2013-03-15 09:47:08 -04003425
3426#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003427/*
3428 * Use this if you need to make a bitmap or extent entry specifically, it
3429 * doesn't do any of the merging that add_free_space does, this acts a lot like
3430 * how the free space cache loading stuff works, so you can get really weird
3431 * configurations.
3432 */
3433int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3434 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003435{
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003436 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3437 struct btrfs_free_space *info = NULL, *bitmap_info;
3438 void *map = NULL;
3439 u64 bytes_added;
3440 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003441
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003442again:
3443 if (!info) {
3444 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3445 if (!info)
3446 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003447 }
3448
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003449 if (!bitmap) {
3450 spin_lock(&ctl->tree_lock);
3451 info->offset = offset;
3452 info->bytes = bytes;
3453 ret = link_free_space(ctl, info);
3454 spin_unlock(&ctl->tree_lock);
3455 if (ret)
3456 kmem_cache_free(btrfs_free_space_cachep, info);
3457 return ret;
3458 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003459
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003460 if (!map) {
3461 map = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
3462 if (!map) {
3463 kmem_cache_free(btrfs_free_space_cachep, info);
3464 return -ENOMEM;
3465 }
3466 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003467
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003468 spin_lock(&ctl->tree_lock);
3469 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3470 1, 0);
3471 if (!bitmap_info) {
3472 info->bitmap = map;
3473 map = NULL;
3474 add_new_bitmap(ctl, info, offset);
3475 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003476 info = NULL;
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003477 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003478
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003479 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3480 bytes -= bytes_added;
3481 offset += bytes_added;
3482 spin_unlock(&ctl->tree_lock);
3483
3484 if (bytes)
3485 goto again;
3486
Filipe Manana20005522014-08-29 13:35:13 +01003487 if (info)
3488 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003489 if (map)
3490 kfree(map);
3491 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003492}
3493
3494/*
3495 * Checks to see if the given range is in the free space cache. This is really
3496 * just used to check the absence of space, so if there is free space in the
3497 * range at all we will return 1.
3498 */
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003499int test_check_exists(struct btrfs_block_group_cache *cache,
3500 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003501{
3502 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3503 struct btrfs_free_space *info;
3504 int ret = 0;
3505
3506 spin_lock(&ctl->tree_lock);
3507 info = tree_search_offset(ctl, offset, 0, 0);
3508 if (!info) {
3509 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3510 1, 0);
3511 if (!info)
3512 goto out;
3513 }
3514
3515have_info:
3516 if (info->bitmap) {
3517 u64 bit_off, bit_bytes;
3518 struct rb_node *n;
3519 struct btrfs_free_space *tmp;
3520
3521 bit_off = offset;
3522 bit_bytes = ctl->unit;
3523 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes);
3524 if (!ret) {
3525 if (bit_off == offset) {
3526 ret = 1;
3527 goto out;
3528 } else if (bit_off > offset &&
3529 offset + bytes > bit_off) {
3530 ret = 1;
3531 goto out;
3532 }
3533 }
3534
3535 n = rb_prev(&info->offset_index);
3536 while (n) {
3537 tmp = rb_entry(n, struct btrfs_free_space,
3538 offset_index);
3539 if (tmp->offset + tmp->bytes < offset)
3540 break;
3541 if (offset + bytes < tmp->offset) {
3542 n = rb_prev(&info->offset_index);
3543 continue;
3544 }
3545 info = tmp;
3546 goto have_info;
3547 }
3548
3549 n = rb_next(&info->offset_index);
3550 while (n) {
3551 tmp = rb_entry(n, struct btrfs_free_space,
3552 offset_index);
3553 if (offset + bytes < tmp->offset)
3554 break;
3555 if (tmp->offset + tmp->bytes < offset) {
3556 n = rb_next(&info->offset_index);
3557 continue;
3558 }
3559 info = tmp;
3560 goto have_info;
3561 }
3562
Filipe Manana20005522014-08-29 13:35:13 +01003563 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003564 goto out;
3565 }
3566
3567 if (info->offset == offset) {
3568 ret = 1;
3569 goto out;
3570 }
3571
3572 if (offset > info->offset && offset < info->offset + info->bytes)
3573 ret = 1;
3574out:
3575 spin_unlock(&ctl->tree_lock);
3576 return ret;
3577}
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003578#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */