blob: 33848196550e4984eff6a577242e7710f70b0c82 [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"
Chris Masonfa9c0d792009-04-03 09:47:43 -040030
Josef Bacik96303082009-07-13 21:29:25 -040031#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
32#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
33
Li Zefan34d52cb2011-03-29 13:46:06 +080034static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040035 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040036static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
37 struct btrfs_free_space *info);
Josef Bacik0cb59c92010-07-02 12:14:14 -040038
Li Zefan0414efa2011-04-20 10:20:14 +080039static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
40 struct btrfs_path *path,
41 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040042{
43 struct btrfs_key key;
44 struct btrfs_key location;
45 struct btrfs_disk_key disk_key;
46 struct btrfs_free_space_header *header;
47 struct extent_buffer *leaf;
48 struct inode *inode = NULL;
49 int ret;
50
Josef Bacik0af3d002010-06-21 14:48:16 -040051 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080052 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040053 key.type = 0;
54
55 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
56 if (ret < 0)
57 return ERR_PTR(ret);
58 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020059 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040060 return ERR_PTR(-ENOENT);
61 }
62
63 leaf = path->nodes[0];
64 header = btrfs_item_ptr(leaf, path->slots[0],
65 struct btrfs_free_space_header);
66 btrfs_free_space_key(leaf, header, &disk_key);
67 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020068 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040069
70 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
71 if (!inode)
72 return ERR_PTR(-ENOENT);
73 if (IS_ERR(inode))
74 return inode;
75 if (is_bad_inode(inode)) {
76 iput(inode);
77 return ERR_PTR(-ENOENT);
78 }
79
Al Viro528c0322012-04-13 11:03:55 -040080 mapping_set_gfp_mask(inode->i_mapping,
81 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
Miao Xieadae52b2011-03-31 09:43:23 +000082
Li Zefan0414efa2011-04-20 10:20:14 +080083 return inode;
84}
85
86struct inode *lookup_free_space_inode(struct btrfs_root *root,
87 struct btrfs_block_group_cache
88 *block_group, struct btrfs_path *path)
89{
90 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040091 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +080092
93 spin_lock(&block_group->lock);
94 if (block_group->inode)
95 inode = igrab(block_group->inode);
96 spin_unlock(&block_group->lock);
97 if (inode)
98 return inode;
99
100 inode = __lookup_free_space_inode(root, path,
101 block_group->key.objectid);
102 if (IS_ERR(inode))
103 return inode;
104
Josef Bacik0af3d002010-06-21 14:48:16 -0400105 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400106 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000107 btrfs_info(root->fs_info,
108 "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400109 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
110 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400111 block_group->disk_cache_state = BTRFS_DC_CLEAR;
112 }
113
Josef Bacik300e4f82011-08-29 14:06:00 -0400114 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400115 block_group->inode = igrab(inode);
116 block_group->iref = 1;
117 }
118 spin_unlock(&block_group->lock);
119
120 return inode;
121}
122
Eric Sandeen48a3b632013-04-25 20:41:01 +0000123static int __create_free_space_inode(struct btrfs_root *root,
124 struct btrfs_trans_handle *trans,
125 struct btrfs_path *path,
126 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400127{
128 struct btrfs_key key;
129 struct btrfs_disk_key disk_key;
130 struct btrfs_free_space_header *header;
131 struct btrfs_inode_item *inode_item;
132 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400133 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400134 int ret;
135
Li Zefan0414efa2011-04-20 10:20:14 +0800136 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400137 if (ret)
138 return ret;
139
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400140 /* We inline crc's for the free disk space cache */
141 if (ino != BTRFS_FREE_INO_OBJECTID)
142 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
143
Josef Bacik0af3d002010-06-21 14:48:16 -0400144 leaf = path->nodes[0];
145 inode_item = btrfs_item_ptr(leaf, path->slots[0],
146 struct btrfs_inode_item);
147 btrfs_item_key(leaf, &disk_key, path->slots[0]);
148 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
149 sizeof(*inode_item));
150 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
151 btrfs_set_inode_size(leaf, inode_item, 0);
152 btrfs_set_inode_nbytes(leaf, inode_item, 0);
153 btrfs_set_inode_uid(leaf, inode_item, 0);
154 btrfs_set_inode_gid(leaf, inode_item, 0);
155 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400156 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400157 btrfs_set_inode_nlink(leaf, inode_item, 1);
158 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800159 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400160 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200161 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400162
163 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800164 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400165 key.type = 0;
166
167 ret = btrfs_insert_empty_item(trans, root, path, &key,
168 sizeof(struct btrfs_free_space_header));
169 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200170 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400171 return ret;
172 }
173 leaf = path->nodes[0];
174 header = btrfs_item_ptr(leaf, path->slots[0],
175 struct btrfs_free_space_header);
176 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
177 btrfs_set_free_space_key(leaf, header, &disk_key);
178 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200179 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400180
181 return 0;
182}
183
Li Zefan0414efa2011-04-20 10:20:14 +0800184int create_free_space_inode(struct btrfs_root *root,
185 struct btrfs_trans_handle *trans,
186 struct btrfs_block_group_cache *block_group,
187 struct btrfs_path *path)
188{
189 int ret;
190 u64 ino;
191
192 ret = btrfs_find_free_objectid(root, &ino);
193 if (ret < 0)
194 return ret;
195
196 return __create_free_space_inode(root, trans, path, ino,
197 block_group->key.objectid);
198}
199
Miao Xie7b61cd92013-05-13 13:55:09 +0000200int btrfs_check_trunc_cache_free_space(struct btrfs_root *root,
201 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400202{
Josef Bacikc8174312011-11-02 09:29:35 -0400203 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000204 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400205
206 /* 1 for slack space, 1 for updating the inode */
207 needed_bytes = btrfs_calc_trunc_metadata_size(root, 1) +
208 btrfs_calc_trans_metadata_size(root, 1);
209
Miao Xie7b61cd92013-05-13 13:55:09 +0000210 spin_lock(&rsv->lock);
211 if (rsv->reserved < needed_bytes)
212 ret = -ENOSPC;
213 else
214 ret = 0;
215 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000216 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000217}
218
219int btrfs_truncate_free_space_cache(struct btrfs_root *root,
220 struct btrfs_trans_handle *trans,
Miao Xie7b61cd92013-05-13 13:55:09 +0000221 struct inode *inode)
222{
Miao Xie7b61cd92013-05-13 13:55:09 +0000223 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400224
Josef Bacik0af3d002010-06-21 14:48:16 -0400225 btrfs_i_size_write(inode, 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700226 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400227
228 /*
229 * We don't need an orphan item because truncating the free space cache
230 * will never be split across transactions.
231 */
232 ret = btrfs_truncate_inode_items(trans, root, inode,
233 0, BTRFS_EXTENT_DATA_KEY);
234 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100235 btrfs_abort_transaction(trans, root, ret);
Josef Bacik0af3d002010-06-21 14:48:16 -0400236 return ret;
237 }
238
Li Zefan82d59022011-04-20 10:33:24 +0800239 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100240 if (ret)
241 btrfs_abort_transaction(trans, root, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400242
Li Zefan82d59022011-04-20 10:33:24 +0800243 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400244}
245
Josef Bacik9d66e232010-08-25 16:54:15 -0400246static int readahead_cache(struct inode *inode)
247{
248 struct file_ra_state *ra;
249 unsigned long last_index;
250
251 ra = kzalloc(sizeof(*ra), GFP_NOFS);
252 if (!ra)
253 return -ENOMEM;
254
255 file_ra_state_init(ra, inode->i_mapping);
256 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
257
258 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
259
260 kfree(ra);
261
262 return 0;
263}
264
Josef Bacika67509c2011-10-05 15:18:58 -0400265struct io_ctl {
266 void *cur, *orig;
267 struct page *page;
268 struct page **pages;
269 struct btrfs_root *root;
270 unsigned long size;
271 int index;
272 int num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400273 unsigned check_crcs:1;
Josef Bacika67509c2011-10-05 15:18:58 -0400274};
275
276static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
Miao Xie5349d6c2014-06-19 10:42:49 +0800277 struct btrfs_root *root, int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400278{
Miao Xie5349d6c2014-06-19 10:42:49 +0800279 int num_pages;
280 int check_crcs = 0;
281
David Sterbaed6078f2014-06-05 01:59:57 +0200282 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800283
284 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
285 check_crcs = 1;
286
287 /* Make sure we can fit our crcs into the first page */
288 if (write && check_crcs &&
289 (num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE)
290 return -ENOSPC;
291
Josef Bacika67509c2011-10-05 15:18:58 -0400292 memset(io_ctl, 0, sizeof(struct io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800293
294 io_ctl->pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400295 if (!io_ctl->pages)
296 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800297
298 io_ctl->num_pages = num_pages;
Josef Bacika67509c2011-10-05 15:18:58 -0400299 io_ctl->root = root;
Miao Xie5349d6c2014-06-19 10:42:49 +0800300 io_ctl->check_crcs = check_crcs;
301
Josef Bacika67509c2011-10-05 15:18:58 -0400302 return 0;
303}
304
305static void io_ctl_free(struct io_ctl *io_ctl)
306{
307 kfree(io_ctl->pages);
308}
309
310static void io_ctl_unmap_page(struct io_ctl *io_ctl)
311{
312 if (io_ctl->cur) {
313 kunmap(io_ctl->page);
314 io_ctl->cur = NULL;
315 io_ctl->orig = NULL;
316 }
317}
318
319static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
320{
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++];
323 io_ctl->cur = kmap(io_ctl->page);
324 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
330static void io_ctl_drop_pages(struct io_ctl *io_ctl)
331{
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
345static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode,
346 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
379static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation)
380{
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
402static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
403{
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
431static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
432{
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);
449 tmp = kmap(io_ctl->pages[0]);
450 tmp += index;
451 *tmp = crc;
452 kunmap(io_ctl->pages[0]);
453}
454
455static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
456{
457 u32 *tmp, val;
458 u32 crc = ~(u32)0;
459 unsigned offset = 0;
460
461 if (!io_ctl->check_crcs) {
462 io_ctl_map_page(io_ctl, 0);
463 return 0;
464 }
465
466 if (index == 0)
467 offset = sizeof(u32) * io_ctl->num_pages;
468
469 tmp = kmap(io_ctl->pages[0]);
470 tmp += index;
471 val = *tmp;
472 kunmap(io_ctl->pages[0]);
473
474 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000475 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400476 PAGE_CACHE_SIZE - offset);
477 btrfs_csum_final(crc, (char *)&crc);
478 if (val != crc) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500479 printk_ratelimited(KERN_ERR "BTRFS: csum mismatch on free "
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400480 "space cache\n");
481 io_ctl_unmap_page(io_ctl);
482 return -EIO;
483 }
484
Josef Bacika67509c2011-10-05 15:18:58 -0400485 return 0;
486}
487
488static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes,
489 void *bitmap)
490{
491 struct btrfs_free_space_entry *entry;
492
493 if (!io_ctl->cur)
494 return -ENOSPC;
495
496 entry = io_ctl->cur;
497 entry->offset = cpu_to_le64(offset);
498 entry->bytes = cpu_to_le64(bytes);
499 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
500 BTRFS_FREE_SPACE_EXTENT;
501 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
502 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
503
504 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
505 return 0;
506
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400507 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400508
509 /* No more pages to map */
510 if (io_ctl->index >= io_ctl->num_pages)
511 return 0;
512
513 /* map the next page */
514 io_ctl_map_page(io_ctl, 1);
515 return 0;
516}
517
518static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap)
519{
520 if (!io_ctl->cur)
521 return -ENOSPC;
522
523 /*
524 * If we aren't at the start of the current page, unmap this one and
525 * map the next one if there is any left.
526 */
527 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400528 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400529 if (io_ctl->index >= io_ctl->num_pages)
530 return -ENOSPC;
531 io_ctl_map_page(io_ctl, 0);
532 }
533
534 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400535 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400536 if (io_ctl->index < io_ctl->num_pages)
537 io_ctl_map_page(io_ctl, 0);
538 return 0;
539}
540
541static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl)
542{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400543 /*
544 * If we're not on the boundary we know we've modified the page and we
545 * need to crc the page.
546 */
547 if (io_ctl->cur != io_ctl->orig)
548 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
549 else
550 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400551
552 while (io_ctl->index < io_ctl->num_pages) {
553 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400554 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400555 }
556}
557
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400558static int io_ctl_read_entry(struct io_ctl *io_ctl,
559 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400560{
561 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500562 int ret;
563
564 if (!io_ctl->cur) {
565 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
566 if (ret)
567 return ret;
568 }
Josef Bacika67509c2011-10-05 15:18:58 -0400569
570 e = io_ctl->cur;
571 entry->offset = le64_to_cpu(e->offset);
572 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400573 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400574 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
575 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
576
577 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400578 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400579
580 io_ctl_unmap_page(io_ctl);
581
Josef Bacik2f120c02011-11-10 20:45:05 -0500582 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400583}
584
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400585static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
586 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400587{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400588 int ret;
589
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400590 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
591 if (ret)
592 return ret;
593
Josef Bacika67509c2011-10-05 15:18:58 -0400594 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
595 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400596
597 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400598}
599
Josef Bacikcd023e72012-05-14 10:06:40 -0400600/*
601 * Since we attach pinned extents after the fact we can have contiguous sections
602 * of free space that are split up in entries. This poses a problem with the
603 * tree logging stuff since it could have allocated across what appears to be 2
604 * entries since we would have merged the entries when adding the pinned extents
605 * back to the free space cache. So run through the space cache that we just
606 * loaded and merge contiguous entries. This will make the log replay stuff not
607 * blow up and it will make for nicer allocator behavior.
608 */
609static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
610{
611 struct btrfs_free_space *e, *prev = NULL;
612 struct rb_node *n;
613
614again:
615 spin_lock(&ctl->tree_lock);
616 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
617 e = rb_entry(n, struct btrfs_free_space, offset_index);
618 if (!prev)
619 goto next;
620 if (e->bitmap || prev->bitmap)
621 goto next;
622 if (prev->offset + prev->bytes == e->offset) {
623 unlink_free_space(ctl, prev);
624 unlink_free_space(ctl, e);
625 prev->bytes += e->bytes;
626 kmem_cache_free(btrfs_free_space_cachep, e);
627 link_free_space(ctl, prev);
628 prev = NULL;
629 spin_unlock(&ctl->tree_lock);
630 goto again;
631 }
632next:
633 prev = e;
634 }
635 spin_unlock(&ctl->tree_lock);
636}
637
Eric Sandeen48a3b632013-04-25 20:41:01 +0000638static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
639 struct btrfs_free_space_ctl *ctl,
640 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400641{
Josef Bacik9d66e232010-08-25 16:54:15 -0400642 struct btrfs_free_space_header *header;
643 struct extent_buffer *leaf;
Josef Bacika67509c2011-10-05 15:18:58 -0400644 struct io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400645 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400646 struct btrfs_free_space *e, *n;
Josef Bacik9d66e232010-08-25 16:54:15 -0400647 struct list_head bitmaps;
648 u64 num_entries;
649 u64 num_bitmaps;
650 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400651 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400652 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400653
654 INIT_LIST_HEAD(&bitmaps);
655
Josef Bacik9d66e232010-08-25 16:54:15 -0400656 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800657 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400658 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400659
660 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800661 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400662 key.type = 0;
663
664 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800665 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400666 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800667 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400668 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400669 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 }
671
Li Zefan0414efa2011-04-20 10:20:14 +0800672 ret = -1;
673
Josef Bacik9d66e232010-08-25 16:54:15 -0400674 leaf = path->nodes[0];
675 header = btrfs_item_ptr(leaf, path->slots[0],
676 struct btrfs_free_space_header);
677 num_entries = btrfs_free_space_entries(leaf, header);
678 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
679 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400680 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400681
Miao Xiee570fd22014-06-19 10:42:50 +0800682 if (!BTRFS_I(inode)->generation) {
683 btrfs_info(root->fs_info,
684 "The free space cache file (%llu) is invalid. skip it\n",
685 offset);
686 return 0;
687 }
688
Josef Bacik9d66e232010-08-25 16:54:15 -0400689 if (BTRFS_I(inode)->generation != generation) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000690 btrfs_err(root->fs_info,
691 "free space inode generation (%llu) "
692 "did not match free space cache generation (%llu)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200693 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400694 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400695 }
696
697 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400698 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400699
Miao Xie5349d6c2014-06-19 10:42:49 +0800700 ret = io_ctl_init(&io_ctl, inode, root, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800701 if (ret)
702 return ret;
703
Josef Bacik9d66e232010-08-25 16:54:15 -0400704 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800705 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400706 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400707
Josef Bacika67509c2011-10-05 15:18:58 -0400708 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
709 if (ret)
710 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400711
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400712 ret = io_ctl_check_crc(&io_ctl, 0);
713 if (ret)
714 goto free_cache;
715
Josef Bacika67509c2011-10-05 15:18:58 -0400716 ret = io_ctl_check_generation(&io_ctl, generation);
717 if (ret)
718 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400719
Josef Bacika67509c2011-10-05 15:18:58 -0400720 while (num_entries) {
721 e = kmem_cache_zalloc(btrfs_free_space_cachep,
722 GFP_NOFS);
723 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400724 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400725
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400726 ret = io_ctl_read_entry(&io_ctl, e, &type);
727 if (ret) {
728 kmem_cache_free(btrfs_free_space_cachep, e);
729 goto free_cache;
730 }
731
Josef Bacika67509c2011-10-05 15:18:58 -0400732 if (!e->bytes) {
733 kmem_cache_free(btrfs_free_space_cachep, e);
734 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400735 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400736
Josef Bacika67509c2011-10-05 15:18:58 -0400737 if (type == BTRFS_FREE_SPACE_EXTENT) {
738 spin_lock(&ctl->tree_lock);
739 ret = link_free_space(ctl, e);
740 spin_unlock(&ctl->tree_lock);
741 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000742 btrfs_err(root->fs_info,
743 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500744 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400745 goto free_cache;
746 }
Josef Bacika67509c2011-10-05 15:18:58 -0400747 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400748 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400749 num_bitmaps--;
750 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
751 if (!e->bitmap) {
752 kmem_cache_free(
753 btrfs_free_space_cachep, e);
754 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400755 }
Josef Bacika67509c2011-10-05 15:18:58 -0400756 spin_lock(&ctl->tree_lock);
757 ret = link_free_space(ctl, e);
758 ctl->total_bitmaps++;
759 ctl->op->recalc_thresholds(ctl);
760 spin_unlock(&ctl->tree_lock);
761 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000762 btrfs_err(root->fs_info,
763 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400764 kmem_cache_free(btrfs_free_space_cachep, e);
765 goto free_cache;
766 }
767 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400768 }
769
Josef Bacika67509c2011-10-05 15:18:58 -0400770 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400771 }
772
Josef Bacik2f120c02011-11-10 20:45:05 -0500773 io_ctl_unmap_page(&io_ctl);
774
Josef Bacika67509c2011-10-05 15:18:58 -0400775 /*
776 * We add the bitmaps at the end of the entries in order that
777 * the bitmap entries are added to the cache.
778 */
779 list_for_each_entry_safe(e, n, &bitmaps, list) {
780 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400781 ret = io_ctl_read_bitmap(&io_ctl, e);
782 if (ret)
783 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400784 }
785
786 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400787 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400788 ret = 1;
789out:
Josef Bacika67509c2011-10-05 15:18:58 -0400790 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400791 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400792free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400793 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800794 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400795 goto out;
796}
797
Li Zefan0414efa2011-04-20 10:20:14 +0800798int load_free_space_cache(struct btrfs_fs_info *fs_info,
799 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400800{
Li Zefan34d52cb2011-03-29 13:46:06 +0800801 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800802 struct btrfs_root *root = fs_info->tree_root;
803 struct inode *inode;
804 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400805 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800806 bool matched;
807 u64 used = btrfs_block_group_used(&block_group->item);
808
809 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800810 * If this block group has been marked to be cleared for one reason or
811 * another then we can't trust the on disk cache, so just return.
812 */
813 spin_lock(&block_group->lock);
814 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
815 spin_unlock(&block_group->lock);
816 return 0;
817 }
818 spin_unlock(&block_group->lock);
819
820 path = btrfs_alloc_path();
821 if (!path)
822 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400823 path->search_commit_root = 1;
824 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800825
826 inode = lookup_free_space_inode(root, block_group, path);
827 if (IS_ERR(inode)) {
828 btrfs_free_path(path);
829 return 0;
830 }
831
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400832 /* We may have converted the inode and made the cache invalid. */
833 spin_lock(&block_group->lock);
834 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
835 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900836 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400837 goto out;
838 }
839 spin_unlock(&block_group->lock);
840
Li Zefan0414efa2011-04-20 10:20:14 +0800841 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
842 path, block_group->key.objectid);
843 btrfs_free_path(path);
844 if (ret <= 0)
845 goto out;
846
847 spin_lock(&ctl->tree_lock);
848 matched = (ctl->free_space == (block_group->key.offset - used -
849 block_group->bytes_super));
850 spin_unlock(&ctl->tree_lock);
851
852 if (!matched) {
853 __btrfs_remove_free_space_cache(ctl);
Miao Xie32d6b472014-04-24 13:31:55 +0800854 btrfs_warn(fs_info, "block group %llu has wrong amount of free space",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000855 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800856 ret = -1;
857 }
858out:
859 if (ret < 0) {
860 /* This cache is bogus, make sure it gets cleared */
861 spin_lock(&block_group->lock);
862 block_group->disk_cache_state = BTRFS_DC_CLEAR;
863 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800864 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800865
Miao Xie32d6b472014-04-24 13:31:55 +0800866 btrfs_warn(fs_info, "failed to load free space cache for block group %llu, rebuild it now",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000867 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800868 }
869
870 iput(inode);
871 return ret;
872}
873
Chris Masond4452bc2014-05-19 20:47:56 -0700874static noinline_for_stack
875int write_cache_extent_entries(struct io_ctl *io_ctl,
876 struct btrfs_free_space_ctl *ctl,
877 struct btrfs_block_group_cache *block_group,
878 int *entries, int *bitmaps,
879 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400880{
Josef Bacikc09544e2011-08-30 10:19:10 -0400881 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700882 struct btrfs_free_cluster *cluster = NULL;
883 struct rb_node *node = rb_first(&ctl->free_space_offset);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400884
Josef Bacik43be2142011-04-01 14:55:00 +0000885 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700886 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000887 cluster = list_entry(block_group->cluster_list.next,
888 struct btrfs_free_cluster,
889 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700890 }
Josef Bacik43be2142011-04-01 14:55:00 +0000891
Josef Bacikf75b1302011-10-05 10:00:18 -0400892 if (!node && cluster) {
893 node = rb_first(&cluster->root);
894 cluster = NULL;
895 }
896
Josef Bacik0cb59c92010-07-02 12:14:14 -0400897 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400898 while (node) {
899 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400900
Josef Bacika67509c2011-10-05 15:18:58 -0400901 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700902 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000903
Chris Masond4452bc2014-05-19 20:47:56 -0700904 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400905 e->bitmap);
906 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700907 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400908
909 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700910 list_add_tail(&e->list, bitmap_list);
911 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400912 }
913 node = rb_next(node);
914 if (!node && cluster) {
915 node = rb_first(&cluster->root);
916 cluster = NULL;
917 }
918 }
Chris Masond4452bc2014-05-19 20:47:56 -0700919 return 0;
920fail:
921 return -ENOSPC;
922}
923
924static noinline_for_stack int
925update_cache_item(struct btrfs_trans_handle *trans,
926 struct btrfs_root *root,
927 struct inode *inode,
928 struct btrfs_path *path, u64 offset,
929 int entries, int bitmaps)
930{
931 struct btrfs_key key;
932 struct btrfs_free_space_header *header;
933 struct extent_buffer *leaf;
934 int ret;
935
936 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
937 key.offset = offset;
938 key.type = 0;
939
940 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
941 if (ret < 0) {
942 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
943 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
944 GFP_NOFS);
945 goto fail;
946 }
947 leaf = path->nodes[0];
948 if (ret > 0) {
949 struct btrfs_key found_key;
950 ASSERT(path->slots[0]);
951 path->slots[0]--;
952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
953 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
954 found_key.offset != offset) {
955 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
956 inode->i_size - 1,
957 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
958 NULL, GFP_NOFS);
959 btrfs_release_path(path);
960 goto fail;
961 }
962 }
963
964 BTRFS_I(inode)->generation = trans->transid;
965 header = btrfs_item_ptr(leaf, path->slots[0],
966 struct btrfs_free_space_header);
967 btrfs_set_free_space_entries(leaf, header, entries);
968 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
969 btrfs_set_free_space_generation(leaf, header, trans->transid);
970 btrfs_mark_buffer_dirty(leaf);
971 btrfs_release_path(path);
972
973 return 0;
974
975fail:
976 return -1;
977}
978
979static noinline_for_stack int
Miao Xie5349d6c2014-06-19 10:42:49 +0800980write_pinned_extent_entries(struct btrfs_root *root,
981 struct btrfs_block_group_cache *block_group,
982 struct io_ctl *io_ctl,
983 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -0700984{
985 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -0700986 struct extent_io_tree *unpin = NULL;
987 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -0400988
Miao Xie5349d6c2014-06-19 10:42:49 +0800989 if (!block_group)
990 return 0;
991
Josef Bacika67509c2011-10-05 15:18:58 -0400992 /*
993 * We want to add any pinned extents to our free space cache
994 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -0700995 *
Li Zefandb804f22012-01-10 16:41:01 +0800996 * We shouldn't have switched the pinned extents yet so this is the
997 * right one
998 */
999 unpin = root->fs_info->pinned_extents;
1000
Miao Xie5349d6c2014-06-19 10:42:49 +08001001 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001002
Miao Xie5349d6c2014-06-19 10:42:49 +08001003 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001004 ret = find_first_extent_bit(unpin, start,
1005 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001006 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001007 if (ret)
1008 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001009
Josef Bacika67509c2011-10-05 15:18:58 -04001010 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001011 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001012 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001013 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001014
Li Zefandb804f22012-01-10 16:41:01 +08001015 extent_start = max(extent_start, start);
1016 extent_end = min(block_group->key.objectid +
1017 block_group->key.offset, extent_end + 1);
1018 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001019
Chris Masond4452bc2014-05-19 20:47:56 -07001020 *entries += 1;
1021 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001022 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001023 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001024
Li Zefandb804f22012-01-10 16:41:01 +08001025 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001026 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001027
Miao Xie5349d6c2014-06-19 10:42:49 +08001028 return 0;
1029}
1030
1031static noinline_for_stack int
1032write_bitmap_entries(struct io_ctl *io_ctl, struct list_head *bitmap_list)
1033{
1034 struct list_head *pos, *n;
1035 int ret;
1036
Josef Bacik0cb59c92010-07-02 12:14:14 -04001037 /* Write out the bitmaps */
Chris Masond4452bc2014-05-19 20:47:56 -07001038 list_for_each_safe(pos, n, bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001039 struct btrfs_free_space *entry =
1040 list_entry(pos, struct btrfs_free_space, list);
1041
Chris Masond4452bc2014-05-19 20:47:56 -07001042 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001043 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001044 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001045 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001046 }
1047
Miao Xie5349d6c2014-06-19 10:42:49 +08001048 return 0;
1049}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001050
Miao Xie5349d6c2014-06-19 10:42:49 +08001051static int flush_dirty_cache(struct inode *inode)
1052{
1053 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001054
Josef Bacik0ef8b722013-10-25 16:13:35 -04001055 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001056 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001057 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1058 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1059 GFP_NOFS);
Chris Masond4452bc2014-05-19 20:47:56 -07001060
Miao Xie5349d6c2014-06-19 10:42:49 +08001061 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001062}
1063
1064static void noinline_for_stack
1065cleanup_write_cache_enospc(struct inode *inode,
1066 struct io_ctl *io_ctl,
1067 struct extent_state **cached_state,
1068 struct list_head *bitmap_list)
1069{
1070 struct list_head *pos, *n;
Miao Xie5349d6c2014-06-19 10:42:49 +08001071
Chris Masond4452bc2014-05-19 20:47:56 -07001072 list_for_each_safe(pos, n, bitmap_list) {
1073 struct btrfs_free_space *entry =
1074 list_entry(pos, struct btrfs_free_space, list);
1075 list_del_init(&entry->list);
1076 }
1077 io_ctl_drop_pages(io_ctl);
1078 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1079 i_size_read(inode) - 1, cached_state,
1080 GFP_NOFS);
1081}
1082
1083/**
1084 * __btrfs_write_out_cache - write out cached info to an inode
1085 * @root - the root the inode belongs to
1086 * @ctl - the free space cache we are going to write out
1087 * @block_group - the block_group for this cache if it belongs to a block_group
1088 * @trans - the trans handle
1089 * @path - the path to use
1090 * @offset - the offset for the key we'll insert
1091 *
1092 * This function writes out a free space cache struct to disk for quick recovery
1093 * on mount. This will return 0 if it was successfull in writing the cache out,
1094 * and -1 if it was not.
1095 */
1096static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1097 struct btrfs_free_space_ctl *ctl,
1098 struct btrfs_block_group_cache *block_group,
1099 struct btrfs_trans_handle *trans,
1100 struct btrfs_path *path, u64 offset)
1101{
1102 struct extent_state *cached_state = NULL;
1103 struct io_ctl io_ctl;
Miao Xie5349d6c2014-06-19 10:42:49 +08001104 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001105 int entries = 0;
1106 int bitmaps = 0;
1107 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001108
1109 if (!i_size_read(inode))
1110 return -1;
1111
Miao Xie5349d6c2014-06-19 10:42:49 +08001112 ret = io_ctl_init(&io_ctl, inode, root, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001113 if (ret)
1114 return -1;
1115
Miao Xiee570fd22014-06-19 10:42:50 +08001116 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1117 down_write(&block_group->data_rwsem);
1118 spin_lock(&block_group->lock);
1119 if (block_group->delalloc_bytes) {
1120 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1121 spin_unlock(&block_group->lock);
1122 up_write(&block_group->data_rwsem);
1123 BTRFS_I(inode)->generation = 0;
1124 ret = 0;
1125 goto out;
1126 }
1127 spin_unlock(&block_group->lock);
1128 }
1129
Chris Masond4452bc2014-05-19 20:47:56 -07001130 /* Lock all pages first so we can lock the extent safely. */
1131 io_ctl_prepare_pages(&io_ctl, inode, 0);
1132
1133 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1134 0, &cached_state);
1135
Chris Masond4452bc2014-05-19 20:47:56 -07001136 io_ctl_set_generation(&io_ctl, trans->transid);
1137
Miao Xie5349d6c2014-06-19 10:42:49 +08001138 /* Write out the extent entries in the free space cache */
Chris Masond4452bc2014-05-19 20:47:56 -07001139 ret = write_cache_extent_entries(&io_ctl, ctl,
1140 block_group, &entries, &bitmaps,
1141 &bitmap_list);
1142 if (ret)
1143 goto out_nospc;
1144
Miao Xie5349d6c2014-06-19 10:42:49 +08001145 /*
1146 * Some spaces that are freed in the current transaction are pinned,
1147 * they will be added into free space cache after the transaction is
1148 * committed, we shouldn't lose them.
1149 */
1150 ret = write_pinned_extent_entries(root, block_group, &io_ctl, &entries);
1151 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -07001152 goto out_nospc;
Miao Xie5349d6c2014-06-19 10:42:49 +08001153
1154 /* At last, we write out all the bitmaps. */
1155 ret = write_bitmap_entries(&io_ctl, &bitmap_list);
1156 if (ret)
1157 goto out_nospc;
1158
1159 /* Zero out the rest of the pages just to make sure */
1160 io_ctl_zero_remaining_pages(&io_ctl);
1161
1162 /* Everything is written out, now we dirty the pages in the file. */
1163 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
1164 0, i_size_read(inode), &cached_state);
1165 if (ret)
1166 goto out_nospc;
1167
Miao Xiee570fd22014-06-19 10:42:50 +08001168 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1169 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001170 /*
1171 * Release the pages and unlock the extent, we will flush
1172 * them out later
1173 */
1174 io_ctl_drop_pages(&io_ctl);
1175
1176 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1177 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1178
1179 /* Flush the dirty pages in the cache file. */
1180 ret = flush_dirty_cache(inode);
1181 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001182 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001183
Miao Xie5349d6c2014-06-19 10:42:49 +08001184 /* Update the cache item to tell everyone this cache file is valid. */
1185 ret = update_cache_item(trans, root, inode, path, offset,
Chris Masond4452bc2014-05-19 20:47:56 -07001186 entries, bitmaps);
Josef Bacik2f356122011-06-10 15:31:13 -04001187out:
Josef Bacika67509c2011-10-05 15:18:58 -04001188 io_ctl_free(&io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001189 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001190 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001191 BTRFS_I(inode)->generation = 0;
1192 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001193 btrfs_update_inode(trans, root, inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001194 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001195
1196out_nospc:
Chris Masond4452bc2014-05-19 20:47:56 -07001197 cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list);
Miao Xiee570fd22014-06-19 10:42:50 +08001198
1199 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1200 up_write(&block_group->data_rwsem);
1201
Josef Bacika67509c2011-10-05 15:18:58 -04001202 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001203}
1204
1205int btrfs_write_out_cache(struct btrfs_root *root,
1206 struct btrfs_trans_handle *trans,
1207 struct btrfs_block_group_cache *block_group,
1208 struct btrfs_path *path)
1209{
1210 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1211 struct inode *inode;
1212 int ret = 0;
1213
1214 root = root->fs_info->tree_root;
1215
1216 spin_lock(&block_group->lock);
1217 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1218 spin_unlock(&block_group->lock);
1219 return 0;
1220 }
Miao Xiee570fd22014-06-19 10:42:50 +08001221
1222 if (block_group->delalloc_bytes) {
1223 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1224 spin_unlock(&block_group->lock);
1225 return 0;
1226 }
Li Zefan0414efa2011-04-20 10:20:14 +08001227 spin_unlock(&block_group->lock);
1228
1229 inode = lookup_free_space_inode(root, block_group, path);
1230 if (IS_ERR(inode))
1231 return 0;
1232
1233 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1234 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001235 if (ret) {
Li Zefan0414efa2011-04-20 10:20:14 +08001236 spin_lock(&block_group->lock);
1237 block_group->disk_cache_state = BTRFS_DC_ERROR;
1238 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +08001239 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04001240#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00001241 btrfs_err(root->fs_info,
1242 "failed to write free space cache for block group %llu",
1243 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001244#endif
Li Zefan0414efa2011-04-20 10:20:14 +08001245 }
1246
Josef Bacik0cb59c92010-07-02 12:14:14 -04001247 iput(inode);
1248 return ret;
1249}
1250
Li Zefan34d52cb2011-03-29 13:46:06 +08001251static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001252 u64 offset)
1253{
Josef Bacikb12d6862013-08-26 17:14:08 -04001254 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001255 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001256 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001257}
1258
Li Zefan34d52cb2011-03-29 13:46:06 +08001259static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001260{
Li Zefan34d52cb2011-03-29 13:46:06 +08001261 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001262}
1263
Li Zefan34d52cb2011-03-29 13:46:06 +08001264static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001265 u64 offset)
1266{
1267 u64 bitmap_start;
1268 u64 bytes_per_bitmap;
1269
Li Zefan34d52cb2011-03-29 13:46:06 +08001270 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1271 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001272 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1273 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001274 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001275
1276 return bitmap_start;
1277}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001278
1279static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001280 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001281{
1282 struct rb_node **p = &root->rb_node;
1283 struct rb_node *parent = NULL;
1284 struct btrfs_free_space *info;
1285
1286 while (*p) {
1287 parent = *p;
1288 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1289
Josef Bacik96303082009-07-13 21:29:25 -04001290 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001291 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001292 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001293 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001294 } else {
1295 /*
1296 * we could have a bitmap entry and an extent entry
1297 * share the same offset. If this is the case, we want
1298 * the extent entry to always be found first if we do a
1299 * linear search through the tree, since we want to have
1300 * the quickest allocation time, and allocating from an
1301 * extent is faster than allocating from a bitmap. So
1302 * if we're inserting a bitmap and we find an entry at
1303 * this offset, we want to go right, or after this entry
1304 * logically. If we are inserting an extent and we've
1305 * found a bitmap, we want to go left, or before
1306 * logically.
1307 */
1308 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001309 if (info->bitmap) {
1310 WARN_ON_ONCE(1);
1311 return -EEXIST;
1312 }
Josef Bacik96303082009-07-13 21:29:25 -04001313 p = &(*p)->rb_right;
1314 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001315 if (!info->bitmap) {
1316 WARN_ON_ONCE(1);
1317 return -EEXIST;
1318 }
Josef Bacik96303082009-07-13 21:29:25 -04001319 p = &(*p)->rb_left;
1320 }
1321 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001322 }
1323
1324 rb_link_node(node, parent, p);
1325 rb_insert_color(node, root);
1326
1327 return 0;
1328}
1329
1330/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001331 * searches the tree for the given offset.
1332 *
Josef Bacik96303082009-07-13 21:29:25 -04001333 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1334 * want a section that has at least bytes size and comes at or after the given
1335 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001336 */
Josef Bacik96303082009-07-13 21:29:25 -04001337static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001338tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001339 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001340{
Li Zefan34d52cb2011-03-29 13:46:06 +08001341 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001342 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001343
Josef Bacik96303082009-07-13 21:29:25 -04001344 /* find entry that is closest to the 'offset' */
1345 while (1) {
1346 if (!n) {
1347 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001348 break;
1349 }
Josef Bacik96303082009-07-13 21:29:25 -04001350
1351 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1352 prev = entry;
1353
1354 if (offset < entry->offset)
1355 n = n->rb_left;
1356 else if (offset > entry->offset)
1357 n = n->rb_right;
1358 else
1359 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001360 }
1361
Josef Bacik96303082009-07-13 21:29:25 -04001362 if (bitmap_only) {
1363 if (!entry)
1364 return NULL;
1365 if (entry->bitmap)
1366 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001367
Josef Bacik96303082009-07-13 21:29:25 -04001368 /*
1369 * bitmap entry and extent entry may share same offset,
1370 * in that case, bitmap entry comes after extent entry.
1371 */
1372 n = rb_next(n);
1373 if (!n)
1374 return NULL;
1375 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1376 if (entry->offset != offset)
1377 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001378
Josef Bacik96303082009-07-13 21:29:25 -04001379 WARN_ON(!entry->bitmap);
1380 return entry;
1381 } else if (entry) {
1382 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001383 /*
Josef Bacik96303082009-07-13 21:29:25 -04001384 * if previous extent entry covers the offset,
1385 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001386 */
Miao Xiede6c4112012-10-18 08:18:01 +00001387 n = rb_prev(&entry->offset_index);
1388 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001389 prev = rb_entry(n, struct btrfs_free_space,
1390 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001391 if (!prev->bitmap &&
1392 prev->offset + prev->bytes > offset)
1393 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001394 }
Josef Bacik96303082009-07-13 21:29:25 -04001395 }
1396 return entry;
1397 }
1398
1399 if (!prev)
1400 return NULL;
1401
1402 /* find last entry before the 'offset' */
1403 entry = prev;
1404 if (entry->offset > offset) {
1405 n = rb_prev(&entry->offset_index);
1406 if (n) {
1407 entry = rb_entry(n, struct btrfs_free_space,
1408 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001409 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001410 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001411 if (fuzzy)
1412 return entry;
1413 else
1414 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001415 }
1416 }
1417
Josef Bacik96303082009-07-13 21:29:25 -04001418 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001419 n = rb_prev(&entry->offset_index);
1420 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001421 prev = rb_entry(n, struct btrfs_free_space,
1422 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001423 if (!prev->bitmap &&
1424 prev->offset + prev->bytes > offset)
1425 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001426 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001427 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001428 return entry;
1429 } else if (entry->offset + entry->bytes > offset)
1430 return entry;
1431
1432 if (!fuzzy)
1433 return NULL;
1434
1435 while (1) {
1436 if (entry->bitmap) {
1437 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001438 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001439 break;
1440 } else {
1441 if (entry->offset + entry->bytes > offset)
1442 break;
1443 }
1444
1445 n = rb_next(&entry->offset_index);
1446 if (!n)
1447 return NULL;
1448 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1449 }
1450 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001451}
1452
Li Zefanf333adb2010-11-09 14:57:39 +08001453static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001454__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001455 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001456{
Li Zefan34d52cb2011-03-29 13:46:06 +08001457 rb_erase(&info->offset_index, &ctl->free_space_offset);
1458 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001459}
1460
Li Zefan34d52cb2011-03-29 13:46:06 +08001461static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001462 struct btrfs_free_space *info)
1463{
Li Zefan34d52cb2011-03-29 13:46:06 +08001464 __unlink_free_space(ctl, info);
1465 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001466}
1467
Li Zefan34d52cb2011-03-29 13:46:06 +08001468static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001469 struct btrfs_free_space *info)
1470{
1471 int ret = 0;
1472
Josef Bacikb12d6862013-08-26 17:14:08 -04001473 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001474 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001475 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001476 if (ret)
1477 return ret;
1478
Li Zefan34d52cb2011-03-29 13:46:06 +08001479 ctl->free_space += info->bytes;
1480 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001481 return ret;
1482}
1483
Li Zefan34d52cb2011-03-29 13:46:06 +08001484static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001485{
Li Zefan34d52cb2011-03-29 13:46:06 +08001486 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001487 u64 max_bytes;
1488 u64 bitmap_bytes;
1489 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001490 u64 size = block_group->key.offset;
Wang Sheng-Hui96009762012-11-30 06:30:14 +00001491 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001492 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1493
Josef Bacikdde57402013-02-12 14:07:51 -05001494 max_bitmaps = max(max_bitmaps, 1);
1495
Josef Bacikb12d6862013-08-26 17:14:08 -04001496 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001497
1498 /*
1499 * The goal is to keep the total amount of memory used per 1gb of space
1500 * at or below 32k, so we need to adjust how much memory we allow to be
1501 * used by extent based free space tracking
1502 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001503 if (size < 1024 * 1024 * 1024)
1504 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1505 else
1506 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1507 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001508
Josef Bacik25891f72009-09-11 16:11:20 -04001509 /*
1510 * we want to account for 1 more bitmap than what we have so we can make
1511 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1512 * we add more bitmaps.
1513 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001514 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001515
Josef Bacik25891f72009-09-11 16:11:20 -04001516 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001517 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001518 return;
Josef Bacik96303082009-07-13 21:29:25 -04001519 }
Josef Bacik25891f72009-09-11 16:11:20 -04001520
1521 /*
1522 * we want the extent entry threshold to always be at most 1/2 the maxw
1523 * bytes we can have, or whatever is less than that.
1524 */
1525 extent_bytes = max_bytes - bitmap_bytes;
1526 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1527
Li Zefan34d52cb2011-03-29 13:46:06 +08001528 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001529 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001530}
1531
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001532static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1533 struct btrfs_free_space *info,
1534 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001535{
Li Zefanf38b6e72011-03-14 13:40:51 +08001536 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001537
Li Zefan34d52cb2011-03-29 13:46:06 +08001538 start = offset_to_bit(info->offset, ctl->unit, offset);
1539 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001540 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001541
Li Zefanf38b6e72011-03-14 13:40:51 +08001542 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001543
1544 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001545}
1546
1547static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1548 struct btrfs_free_space *info, u64 offset,
1549 u64 bytes)
1550{
1551 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001552 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001553}
1554
Li Zefan34d52cb2011-03-29 13:46:06 +08001555static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001556 struct btrfs_free_space *info, u64 offset,
1557 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001558{
Li Zefanf38b6e72011-03-14 13:40:51 +08001559 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001560
Li Zefan34d52cb2011-03-29 13:46:06 +08001561 start = offset_to_bit(info->offset, ctl->unit, offset);
1562 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001563 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001564
Li Zefanf38b6e72011-03-14 13:40:51 +08001565 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001566
1567 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001568 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001569}
1570
Miao Xiea4820392013-09-09 13:19:42 +08001571/*
1572 * If we can not find suitable extent, we will use bytes to record
1573 * the size of the max extent.
1574 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001575static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001576 struct btrfs_free_space *bitmap_info, u64 *offset,
1577 u64 *bytes)
1578{
1579 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001580 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001581 unsigned long bits, i;
1582 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001583 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001584
Li Zefan34d52cb2011-03-29 13:46:06 +08001585 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001586 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001587 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001588
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001589 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04001590 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1591 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001592 extent_bits = next_zero - i;
1593 if (extent_bits >= bits) {
1594 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001595 break;
Miao Xiea4820392013-09-09 13:19:42 +08001596 } else if (extent_bits > max_bits) {
1597 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001598 }
1599 i = next_zero;
1600 }
1601
1602 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001603 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1604 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001605 return 0;
1606 }
1607
Miao Xiea4820392013-09-09 13:19:42 +08001608 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001609 return -1;
1610}
1611
Miao Xiea4820392013-09-09 13:19:42 +08001612/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001613static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001614find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001615 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001616{
1617 struct btrfs_free_space *entry;
1618 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001619 u64 tmp;
1620 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001621 int ret;
1622
Li Zefan34d52cb2011-03-29 13:46:06 +08001623 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001624 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001625
Li Zefan34d52cb2011-03-29 13:46:06 +08001626 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001627 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001628 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001629
1630 for (node = &entry->offset_index; node; node = rb_next(node)) {
1631 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001632 if (entry->bytes < *bytes) {
1633 if (entry->bytes > *max_extent_size)
1634 *max_extent_size = entry->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001635 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001636 }
Josef Bacik96303082009-07-13 21:29:25 -04001637
David Woodhouse53b381b2013-01-29 18:40:14 -05001638 /* make sure the space returned is big enough
1639 * to match our requested alignment
1640 */
1641 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001642 tmp = entry->offset - ctl->start + align - 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05001643 do_div(tmp, align);
1644 tmp = tmp * align + ctl->start;
1645 align_off = tmp - entry->offset;
1646 } else {
1647 align_off = 0;
1648 tmp = entry->offset;
1649 }
1650
Miao Xiea4820392013-09-09 13:19:42 +08001651 if (entry->bytes < *bytes + align_off) {
1652 if (entry->bytes > *max_extent_size)
1653 *max_extent_size = entry->bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05001654 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001655 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001656
Josef Bacik96303082009-07-13 21:29:25 -04001657 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001658 u64 size = *bytes;
1659
1660 ret = search_bitmap(ctl, entry, &tmp, &size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001661 if (!ret) {
1662 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001663 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001664 return entry;
Miao Xiea4820392013-09-09 13:19:42 +08001665 } else if (size > *max_extent_size) {
1666 *max_extent_size = size;
David Woodhouse53b381b2013-01-29 18:40:14 -05001667 }
Josef Bacik96303082009-07-13 21:29:25 -04001668 continue;
1669 }
1670
David Woodhouse53b381b2013-01-29 18:40:14 -05001671 *offset = tmp;
1672 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001673 return entry;
1674 }
Miao Xiea4820392013-09-09 13:19:42 +08001675out:
Josef Bacik96303082009-07-13 21:29:25 -04001676 return NULL;
1677}
1678
Li Zefan34d52cb2011-03-29 13:46:06 +08001679static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001680 struct btrfs_free_space *info, u64 offset)
1681{
Li Zefan34d52cb2011-03-29 13:46:06 +08001682 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001683 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001684 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001685 link_free_space(ctl, info);
1686 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001687
Li Zefan34d52cb2011-03-29 13:46:06 +08001688 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001689}
1690
Li Zefan34d52cb2011-03-29 13:46:06 +08001691static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001692 struct btrfs_free_space *bitmap_info)
1693{
Li Zefan34d52cb2011-03-29 13:46:06 +08001694 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001695 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001696 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001697 ctl->total_bitmaps--;
1698 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001699}
1700
Li Zefan34d52cb2011-03-29 13:46:06 +08001701static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001702 struct btrfs_free_space *bitmap_info,
1703 u64 *offset, u64 *bytes)
1704{
1705 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001706 u64 search_start, search_bytes;
1707 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001708
1709again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001710 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001711
Josef Bacik6606bb92009-07-31 11:03:58 -04001712 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001713 * We need to search for bits in this bitmap. We could only cover some
1714 * of the extent in this bitmap thanks to how we add space, so we need
1715 * to search for as much as it as we can and clear that amount, and then
1716 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001717 */
1718 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001719 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001720 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001721 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001722 if (ret < 0 || search_start != *offset)
1723 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001724
Josef Bacikbdb7d302012-06-27 15:10:56 -04001725 /* We may have found more bits than what we need */
1726 search_bytes = min(search_bytes, *bytes);
1727
1728 /* Cannot clear past the end of the bitmap */
1729 search_bytes = min(search_bytes, end - search_start + 1);
1730
1731 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1732 *offset += search_bytes;
1733 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001734
1735 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001736 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001737 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001738 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001739
Josef Bacik6606bb92009-07-31 11:03:58 -04001740 /*
1741 * no entry after this bitmap, but we still have bytes to
1742 * remove, so something has gone wrong.
1743 */
1744 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001745 return -EINVAL;
1746
Josef Bacik6606bb92009-07-31 11:03:58 -04001747 bitmap_info = rb_entry(next, struct btrfs_free_space,
1748 offset_index);
1749
1750 /*
1751 * if the next entry isn't a bitmap we need to return to let the
1752 * extent stuff do its work.
1753 */
Josef Bacik96303082009-07-13 21:29:25 -04001754 if (!bitmap_info->bitmap)
1755 return -EAGAIN;
1756
Josef Bacik6606bb92009-07-31 11:03:58 -04001757 /*
1758 * Ok the next item is a bitmap, but it may not actually hold
1759 * the information for the rest of this free space stuff, so
1760 * look for it, and if we don't find it return so we can try
1761 * everything over again.
1762 */
1763 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001764 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001765 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001766 &search_bytes);
1767 if (ret < 0 || search_start != *offset)
1768 return -EAGAIN;
1769
Josef Bacik96303082009-07-13 21:29:25 -04001770 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001771 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001772 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001773
1774 return 0;
1775}
1776
Josef Bacik2cdc3422011-05-27 14:07:49 -04001777static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1778 struct btrfs_free_space *info, u64 offset,
1779 u64 bytes)
1780{
1781 u64 bytes_to_set = 0;
1782 u64 end;
1783
1784 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1785
1786 bytes_to_set = min(end - offset, bytes);
1787
1788 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1789
1790 return bytes_to_set;
1791
1792}
1793
Li Zefan34d52cb2011-03-29 13:46:06 +08001794static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1795 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001796{
Li Zefan34d52cb2011-03-29 13:46:06 +08001797 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001798
1799 /*
1800 * If we are below the extents threshold then we can add this as an
1801 * extent, and don't have to deal with the bitmap
1802 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001803 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001804 /*
1805 * If this block group has some small extents we don't want to
1806 * use up all of our free slots in the cache with them, we want
1807 * to reserve them to larger extents, however if we have plent
1808 * of cache left then go ahead an dadd them, no sense in adding
1809 * the overhead of a bitmap if we don't have to.
1810 */
1811 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001812 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1813 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001814 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001815 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001816 }
1817 }
Josef Bacik96303082009-07-13 21:29:25 -04001818
1819 /*
Josef Bacikdde57402013-02-12 14:07:51 -05001820 * The original block groups from mkfs can be really small, like 8
1821 * megabytes, so don't bother with a bitmap for those entries. However
1822 * some block groups can be smaller than what a bitmap would cover but
1823 * are still large enough that they could overflow the 32k memory limit,
1824 * so allow those block groups to still be allowed to have a bitmap
1825 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04001826 */
Josef Bacikdde57402013-02-12 14:07:51 -05001827 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001828 return false;
1829
1830 return true;
1831}
1832
Josef Bacik2cdc3422011-05-27 14:07:49 -04001833static struct btrfs_free_space_op free_space_op = {
1834 .recalc_thresholds = recalculate_thresholds,
1835 .use_bitmap = use_bitmap,
1836};
1837
Li Zefan34d52cb2011-03-29 13:46:06 +08001838static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1839 struct btrfs_free_space *info)
1840{
1841 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001842 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001843 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001844 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001845 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001846
1847 bytes = info->bytes;
1848 offset = info->offset;
1849
Li Zefan34d52cb2011-03-29 13:46:06 +08001850 if (!ctl->op->use_bitmap(ctl, info))
1851 return 0;
1852
Josef Bacik2cdc3422011-05-27 14:07:49 -04001853 if (ctl->op == &free_space_op)
1854 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001855again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001856 /*
1857 * Since we link bitmaps right into the cluster we need to see if we
1858 * have a cluster here, and if so and it has our bitmap we need to add
1859 * the free space to that bitmap.
1860 */
1861 if (block_group && !list_empty(&block_group->cluster_list)) {
1862 struct btrfs_free_cluster *cluster;
1863 struct rb_node *node;
1864 struct btrfs_free_space *entry;
1865
1866 cluster = list_entry(block_group->cluster_list.next,
1867 struct btrfs_free_cluster,
1868 block_group_list);
1869 spin_lock(&cluster->lock);
1870 node = rb_first(&cluster->root);
1871 if (!node) {
1872 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001873 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001874 }
1875
1876 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1877 if (!entry->bitmap) {
1878 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001879 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001880 }
1881
1882 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1883 bytes_added = add_bytes_to_bitmap(ctl, entry,
1884 offset, bytes);
1885 bytes -= bytes_added;
1886 offset += bytes_added;
1887 }
1888 spin_unlock(&cluster->lock);
1889 if (!bytes) {
1890 ret = 1;
1891 goto out;
1892 }
1893 }
Chris Mason38e87882011-06-10 16:36:57 -04001894
1895no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001896 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001897 1, 0);
1898 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04001899 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04001900 goto new_bitmap;
1901 }
1902
Josef Bacik2cdc3422011-05-27 14:07:49 -04001903 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1904 bytes -= bytes_added;
1905 offset += bytes_added;
1906 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001907
1908 if (!bytes) {
1909 ret = 1;
1910 goto out;
1911 } else
1912 goto again;
1913
1914new_bitmap:
1915 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001916 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001917 added = 1;
1918 info = NULL;
1919 goto again;
1920 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001921 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001922
1923 /* no pre-allocated info, allocate a new one */
1924 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001925 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1926 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001927 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001928 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001929 ret = -ENOMEM;
1930 goto out;
1931 }
1932 }
1933
1934 /* allocate the bitmap */
1935 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001936 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001937 if (!info->bitmap) {
1938 ret = -ENOMEM;
1939 goto out;
1940 }
1941 goto again;
1942 }
1943
1944out:
1945 if (info) {
1946 if (info->bitmap)
1947 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001948 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001949 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001950
1951 return ret;
1952}
1953
Chris Mason945d8962011-05-22 12:33:42 -04001954static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001955 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001956{
Li Zefan120d66e2010-11-09 14:56:50 +08001957 struct btrfs_free_space *left_info;
1958 struct btrfs_free_space *right_info;
1959 bool merged = false;
1960 u64 offset = info->offset;
1961 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001962
Josef Bacik0f9dd462008-09-23 13:14:11 -04001963 /*
1964 * first we want to see if there is free space adjacent to the range we
1965 * are adding, if there is remove that struct and add a new one to
1966 * cover the entire range
1967 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001968 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001969 if (right_info && rb_prev(&right_info->offset_index))
1970 left_info = rb_entry(rb_prev(&right_info->offset_index),
1971 struct btrfs_free_space, offset_index);
1972 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001973 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001974
Josef Bacik96303082009-07-13 21:29:25 -04001975 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001976 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001977 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001978 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001979 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001980 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001981 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001982 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001983 }
1984
Josef Bacik96303082009-07-13 21:29:25 -04001985 if (left_info && !left_info->bitmap &&
1986 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001987 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001988 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001989 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001990 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001991 info->offset = left_info->offset;
1992 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001993 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001994 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001995 }
1996
Li Zefan120d66e2010-11-09 14:56:50 +08001997 return merged;
1998}
1999
Filipe Manana20005522014-08-29 13:35:13 +01002000static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2001 struct btrfs_free_space *info,
2002 bool update_stat)
2003{
2004 struct btrfs_free_space *bitmap;
2005 unsigned long i;
2006 unsigned long j;
2007 const u64 end = info->offset + info->bytes;
2008 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2009 u64 bytes;
2010
2011 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2012 if (!bitmap)
2013 return false;
2014
2015 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2016 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2017 if (j == i)
2018 return false;
2019 bytes = (j - i) * ctl->unit;
2020 info->bytes += bytes;
2021
2022 if (update_stat)
2023 bitmap_clear_bits(ctl, bitmap, end, bytes);
2024 else
2025 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2026
2027 if (!bitmap->bytes)
2028 free_bitmap(ctl, bitmap);
2029
2030 return true;
2031}
2032
2033static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2034 struct btrfs_free_space *info,
2035 bool update_stat)
2036{
2037 struct btrfs_free_space *bitmap;
2038 u64 bitmap_offset;
2039 unsigned long i;
2040 unsigned long j;
2041 unsigned long prev_j;
2042 u64 bytes;
2043
2044 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2045 /* If we're on a boundary, try the previous logical bitmap. */
2046 if (bitmap_offset == info->offset) {
2047 if (info->offset == 0)
2048 return false;
2049 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2050 }
2051
2052 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2053 if (!bitmap)
2054 return false;
2055
2056 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2057 j = 0;
2058 prev_j = (unsigned long)-1;
2059 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2060 if (j > i)
2061 break;
2062 prev_j = j;
2063 }
2064 if (prev_j == i)
2065 return false;
2066
2067 if (prev_j == (unsigned long)-1)
2068 bytes = (i + 1) * ctl->unit;
2069 else
2070 bytes = (i - prev_j) * ctl->unit;
2071
2072 info->offset -= bytes;
2073 info->bytes += bytes;
2074
2075 if (update_stat)
2076 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2077 else
2078 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2079
2080 if (!bitmap->bytes)
2081 free_bitmap(ctl, bitmap);
2082
2083 return true;
2084}
2085
2086/*
2087 * We prefer always to allocate from extent entries, both for clustered and
2088 * non-clustered allocation requests. So when attempting to add a new extent
2089 * entry, try to see if there's adjacent free space in bitmap entries, and if
2090 * there is, migrate that space from the bitmaps to the extent.
2091 * Like this we get better chances of satisfying space allocation requests
2092 * because we attempt to satisfy them based on a single cache entry, and never
2093 * on 2 or more entries - even if the entries represent a contiguous free space
2094 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2095 * ends).
2096 */
2097static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2098 struct btrfs_free_space *info,
2099 bool update_stat)
2100{
2101 /*
2102 * Only work with disconnected entries, as we can change their offset,
2103 * and must be extent entries.
2104 */
2105 ASSERT(!info->bitmap);
2106 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2107
2108 if (ctl->total_bitmaps > 0) {
2109 bool stole_end;
2110 bool stole_front = false;
2111
2112 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2113 if (ctl->total_bitmaps > 0)
2114 stole_front = steal_from_bitmap_to_front(ctl, info,
2115 update_stat);
2116
2117 if (stole_end || stole_front)
2118 try_merge_free_space(ctl, info, update_stat);
2119 }
2120}
2121
Li Zefan581bb052011-04-20 10:06:11 +08002122int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
2123 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002124{
2125 struct btrfs_free_space *info;
2126 int ret = 0;
2127
Josef Bacikdc89e982011-01-28 17:05:48 -05002128 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002129 if (!info)
2130 return -ENOMEM;
2131
2132 info->offset = offset;
2133 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002134 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002135
Li Zefan34d52cb2011-03-29 13:46:06 +08002136 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002137
Li Zefan34d52cb2011-03-29 13:46:06 +08002138 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002139 goto link;
2140
2141 /*
2142 * There was no extent directly to the left or right of this new
2143 * extent then we know we're going to have to allocate a new extent, so
2144 * before we do that see if we need to drop this into a bitmap
2145 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002146 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002147 if (ret < 0) {
2148 goto out;
2149 } else if (ret) {
2150 ret = 0;
2151 goto out;
2152 }
2153link:
Filipe Manana20005522014-08-29 13:35:13 +01002154 /*
2155 * Only steal free space from adjacent bitmaps if we're sure we're not
2156 * going to add the new free space to existing bitmap entries - because
2157 * that would mean unnecessary work that would be reverted. Therefore
2158 * attempt to steal space from bitmaps if we're adding an extent entry.
2159 */
2160 steal_from_bitmap(ctl, info, true);
2161
Li Zefan34d52cb2011-03-29 13:46:06 +08002162 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002163 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002164 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002165out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002166 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002167
Josef Bacik0f9dd462008-09-23 13:14:11 -04002168 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002169 printk(KERN_CRIT "BTRFS: unable to add free space :%d\n", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002170 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002171 }
2172
Josef Bacik0f9dd462008-09-23 13:14:11 -04002173 return ret;
2174}
2175
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002176int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2177 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002178{
Li Zefan34d52cb2011-03-29 13:46:06 +08002179 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002180 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002181 int ret;
2182 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002183
Li Zefan34d52cb2011-03-29 13:46:06 +08002184 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002185
Josef Bacik96303082009-07-13 21:29:25 -04002186again:
Josef Bacikb0175112012-12-18 11:39:19 -05002187 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002188 if (!bytes)
2189 goto out_lock;
2190
Li Zefan34d52cb2011-03-29 13:46:06 +08002191 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002192 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002193 /*
2194 * oops didn't find an extent that matched the space we wanted
2195 * to remove, look for a bitmap instead
2196 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002197 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002198 1, 0);
2199 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002200 /*
2201 * If we found a partial bit of our free space in a
2202 * bitmap but then couldn't find the other part this may
2203 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002204 */
Josef Bacikb0175112012-12-18 11:39:19 -05002205 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002206 goto out_lock;
2207 }
Josef Bacik96303082009-07-13 21:29:25 -04002208 }
2209
Josef Bacikb0175112012-12-18 11:39:19 -05002210 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002211 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002212 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002213 if (offset == info->offset) {
2214 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002215
Josef Bacikbdb7d302012-06-27 15:10:56 -04002216 info->bytes -= to_free;
2217 info->offset += to_free;
2218 if (info->bytes) {
2219 ret = link_free_space(ctl, info);
2220 WARN_ON(ret);
2221 } else {
2222 kmem_cache_free(btrfs_free_space_cachep, info);
2223 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002224
Josef Bacikbdb7d302012-06-27 15:10:56 -04002225 offset += to_free;
2226 bytes -= to_free;
2227 goto again;
2228 } else {
2229 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002230
Josef Bacikbdb7d302012-06-27 15:10:56 -04002231 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002232 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002233 WARN_ON(ret);
2234 if (ret)
2235 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002236
Josef Bacikbdb7d302012-06-27 15:10:56 -04002237 /* Not enough bytes in this entry to satisfy us */
2238 if (old_end < offset + bytes) {
2239 bytes -= old_end - offset;
2240 offset = old_end;
2241 goto again;
2242 } else if (old_end == offset + bytes) {
2243 /* all done */
2244 goto out_lock;
2245 }
2246 spin_unlock(&ctl->tree_lock);
2247
2248 ret = btrfs_add_free_space(block_group, offset + bytes,
2249 old_end - (offset + bytes));
2250 WARN_ON(ret);
2251 goto out;
2252 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002253 }
Josef Bacik96303082009-07-13 21:29:25 -04002254
Li Zefan34d52cb2011-03-29 13:46:06 +08002255 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002256 if (ret == -EAGAIN) {
2257 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002258 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002259 }
Josef Bacik96303082009-07-13 21:29:25 -04002260out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002261 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002262out:
Josef Bacik25179202008-10-29 14:49:05 -04002263 return ret;
2264}
2265
Josef Bacik0f9dd462008-09-23 13:14:11 -04002266void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2267 u64 bytes)
2268{
Li Zefan34d52cb2011-03-29 13:46:06 +08002269 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002270 struct btrfs_free_space *info;
2271 struct rb_node *n;
2272 int count = 0;
2273
Li Zefan34d52cb2011-03-29 13:46:06 +08002274 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002275 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002276 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002277 count++;
Frank Holtonefe120a2013-12-20 11:37:06 -05002278 btrfs_crit(block_group->fs_info,
2279 "entry offset %llu, bytes %llu, bitmap %s",
2280 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002281 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002282 }
Frank Holtonefe120a2013-12-20 11:37:06 -05002283 btrfs_info(block_group->fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002284 list_empty(&block_group->cluster_list) ? "no" : "yes");
Frank Holtonefe120a2013-12-20 11:37:06 -05002285 btrfs_info(block_group->fs_info,
2286 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002287}
2288
Li Zefan34d52cb2011-03-29 13:46:06 +08002289void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002290{
Li Zefan34d52cb2011-03-29 13:46:06 +08002291 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002292
Li Zefan34d52cb2011-03-29 13:46:06 +08002293 spin_lock_init(&ctl->tree_lock);
2294 ctl->unit = block_group->sectorsize;
2295 ctl->start = block_group->key.objectid;
2296 ctl->private = block_group;
2297 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002298
Li Zefan34d52cb2011-03-29 13:46:06 +08002299 /*
2300 * we only want to have 32k of ram per block group for keeping
2301 * track of free space, and if we pass 1/2 of that we want to
2302 * start converting things over to using bitmaps
2303 */
2304 ctl->extents_thresh = ((1024 * 32) / 2) /
2305 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002306}
2307
Chris Masonfa9c0d792009-04-03 09:47:43 -04002308/*
2309 * for a given cluster, put all of its extents back into the free
2310 * space cache. If the block group passed doesn't match the block group
2311 * pointed to by the cluster, someone else raced in and freed the
2312 * cluster already. In that case, we just return without changing anything
2313 */
2314static int
2315__btrfs_return_cluster_to_free_space(
2316 struct btrfs_block_group_cache *block_group,
2317 struct btrfs_free_cluster *cluster)
2318{
Li Zefan34d52cb2011-03-29 13:46:06 +08002319 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002320 struct btrfs_free_space *entry;
2321 struct rb_node *node;
2322
2323 spin_lock(&cluster->lock);
2324 if (cluster->block_group != block_group)
2325 goto out;
2326
Josef Bacik96303082009-07-13 21:29:25 -04002327 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002328 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002329 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002330
Chris Masonfa9c0d792009-04-03 09:47:43 -04002331 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002332 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002333 bool bitmap;
2334
Chris Masonfa9c0d792009-04-03 09:47:43 -04002335 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2336 node = rb_next(&entry->offset_index);
2337 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002338 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002339
2340 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002341 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002342 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002343 steal_from_bitmap(ctl, entry, false);
2344 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002345 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002346 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002347 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002348 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002349
Chris Masonfa9c0d792009-04-03 09:47:43 -04002350out:
2351 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002352 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002353 return 0;
2354}
2355
Eric Sandeen48a3b632013-04-25 20:41:01 +00002356static void __btrfs_remove_free_space_cache_locked(
2357 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002358{
2359 struct btrfs_free_space *info;
2360 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002361
Li Zefan581bb052011-04-20 10:06:11 +08002362 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2363 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002364 if (!info->bitmap) {
2365 unlink_free_space(ctl, info);
2366 kmem_cache_free(btrfs_free_space_cachep, info);
2367 } else {
2368 free_bitmap(ctl, info);
2369 }
Li Zefan581bb052011-04-20 10:06:11 +08002370 if (need_resched()) {
2371 spin_unlock(&ctl->tree_lock);
2372 cond_resched();
2373 spin_lock(&ctl->tree_lock);
2374 }
2375 }
Chris Mason09655372011-05-21 09:27:38 -04002376}
2377
2378void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2379{
2380 spin_lock(&ctl->tree_lock);
2381 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002382 spin_unlock(&ctl->tree_lock);
2383}
2384
Josef Bacik0f9dd462008-09-23 13:14:11 -04002385void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2386{
Li Zefan34d52cb2011-03-29 13:46:06 +08002387 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002388 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002389 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002390
Li Zefan34d52cb2011-03-29 13:46:06 +08002391 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002392 while ((head = block_group->cluster_list.next) !=
2393 &block_group->cluster_list) {
2394 cluster = list_entry(head, struct btrfs_free_cluster,
2395 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002396
2397 WARN_ON(cluster->block_group != block_group);
2398 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04002399 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002400 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002401 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08002402 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002403 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002404 }
Chris Mason09655372011-05-21 09:27:38 -04002405 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002406 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002407
Josef Bacik0f9dd462008-09-23 13:14:11 -04002408}
2409
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002410u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002411 u64 offset, u64 bytes, u64 empty_size,
2412 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002413{
Li Zefan34d52cb2011-03-29 13:46:06 +08002414 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002415 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002416 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002417 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002418 u64 align_gap = 0;
2419 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002420
Li Zefan34d52cb2011-03-29 13:46:06 +08002421 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002422 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002423 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002424 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002425 goto out;
2426
2427 ret = offset;
2428 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002429 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002430 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002431 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002432 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002433 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002434 align_gap_len = offset - entry->offset;
2435 align_gap = entry->offset;
2436
2437 entry->offset = offset + bytes;
2438 WARN_ON(entry->bytes < bytes + align_gap_len);
2439
2440 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002441 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002442 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002443 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002444 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002445 }
Josef Bacik96303082009-07-13 21:29:25 -04002446out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002447 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002448
David Woodhouse53b381b2013-01-29 18:40:14 -05002449 if (align_gap_len)
2450 __btrfs_add_free_space(ctl, align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002451 return ret;
2452}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002453
2454/*
2455 * given a cluster, put all of its extents back into the free space
2456 * cache. If a block group is passed, this function will only free
2457 * a cluster that belongs to the passed block group.
2458 *
2459 * Otherwise, it'll get a reference on the block group pointed to by the
2460 * cluster and remove the cluster from it.
2461 */
2462int btrfs_return_cluster_to_free_space(
2463 struct btrfs_block_group_cache *block_group,
2464 struct btrfs_free_cluster *cluster)
2465{
Li Zefan34d52cb2011-03-29 13:46:06 +08002466 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002467 int ret;
2468
2469 /* first, get a safe pointer to the block group */
2470 spin_lock(&cluster->lock);
2471 if (!block_group) {
2472 block_group = cluster->block_group;
2473 if (!block_group) {
2474 spin_unlock(&cluster->lock);
2475 return 0;
2476 }
2477 } else if (cluster->block_group != block_group) {
2478 /* someone else has already freed it don't redo their work */
2479 spin_unlock(&cluster->lock);
2480 return 0;
2481 }
2482 atomic_inc(&block_group->count);
2483 spin_unlock(&cluster->lock);
2484
Li Zefan34d52cb2011-03-29 13:46:06 +08002485 ctl = block_group->free_space_ctl;
2486
Chris Masonfa9c0d792009-04-03 09:47:43 -04002487 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002488 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002489 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002490 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002491
2492 /* finally drop our ref */
2493 btrfs_put_block_group(block_group);
2494 return ret;
2495}
2496
Josef Bacik96303082009-07-13 21:29:25 -04002497static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2498 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002499 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002500 u64 bytes, u64 min_start,
2501 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002502{
Li Zefan34d52cb2011-03-29 13:46:06 +08002503 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002504 int err;
2505 u64 search_start = cluster->window_start;
2506 u64 search_bytes = bytes;
2507 u64 ret = 0;
2508
Josef Bacik96303082009-07-13 21:29:25 -04002509 search_start = min_start;
2510 search_bytes = bytes;
2511
Li Zefan34d52cb2011-03-29 13:46:06 +08002512 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Miao Xiea4820392013-09-09 13:19:42 +08002513 if (err) {
2514 if (search_bytes > *max_extent_size)
2515 *max_extent_size = search_bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002516 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002517 }
Josef Bacik96303082009-07-13 21:29:25 -04002518
2519 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002520 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002521
2522 return ret;
2523}
2524
Chris Masonfa9c0d792009-04-03 09:47:43 -04002525/*
2526 * given a cluster, try to allocate 'bytes' from it, returns 0
2527 * if it couldn't find anything suitably large, or a logical disk offset
2528 * if things worked out
2529 */
2530u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2531 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002532 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002533{
Li Zefan34d52cb2011-03-29 13:46:06 +08002534 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002535 struct btrfs_free_space *entry = NULL;
2536 struct rb_node *node;
2537 u64 ret = 0;
2538
2539 spin_lock(&cluster->lock);
2540 if (bytes > cluster->max_size)
2541 goto out;
2542
2543 if (cluster->block_group != block_group)
2544 goto out;
2545
2546 node = rb_first(&cluster->root);
2547 if (!node)
2548 goto out;
2549
2550 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302551 while (1) {
Miao Xiea4820392013-09-09 13:19:42 +08002552 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2553 *max_extent_size = entry->bytes;
2554
Josef Bacik4e69b592011-03-21 10:11:24 -04002555 if (entry->bytes < bytes ||
2556 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002557 node = rb_next(&entry->offset_index);
2558 if (!node)
2559 break;
2560 entry = rb_entry(node, struct btrfs_free_space,
2561 offset_index);
2562 continue;
2563 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002564
Josef Bacik4e69b592011-03-21 10:11:24 -04002565 if (entry->bitmap) {
2566 ret = btrfs_alloc_from_bitmap(block_group,
2567 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002568 cluster->window_start,
2569 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002570 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002571 node = rb_next(&entry->offset_index);
2572 if (!node)
2573 break;
2574 entry = rb_entry(node, struct btrfs_free_space,
2575 offset_index);
2576 continue;
2577 }
Josef Bacik9b230622012-01-26 15:01:12 -05002578 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002579 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002580 ret = entry->offset;
2581
2582 entry->offset += bytes;
2583 entry->bytes -= bytes;
2584 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002585
Li Zefan5e71b5d2010-11-09 14:55:34 +08002586 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002587 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002588 break;
2589 }
2590out:
2591 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002592
Li Zefan5e71b5d2010-11-09 14:55:34 +08002593 if (!ret)
2594 return 0;
2595
Li Zefan34d52cb2011-03-29 13:46:06 +08002596 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002597
Li Zefan34d52cb2011-03-29 13:46:06 +08002598 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002599 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002600 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002601 if (entry->bitmap) {
2602 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002603 ctl->total_bitmaps--;
2604 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002605 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002606 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002607 }
2608
Li Zefan34d52cb2011-03-29 13:46:06 +08002609 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002610
Chris Masonfa9c0d792009-04-03 09:47:43 -04002611 return ret;
2612}
2613
Josef Bacik96303082009-07-13 21:29:25 -04002614static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2615 struct btrfs_free_space *entry,
2616 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002617 u64 offset, u64 bytes,
2618 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002619{
Li Zefan34d52cb2011-03-29 13:46:06 +08002620 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002621 unsigned long next_zero;
2622 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002623 unsigned long want_bits;
2624 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002625 unsigned long found_bits;
2626 unsigned long start = 0;
2627 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002628 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002629
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002630 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002631 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002632 want_bits = bytes_to_bits(bytes, ctl->unit);
2633 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002634
2635again:
2636 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002637 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002638 next_zero = find_next_zero_bit(entry->bitmap,
2639 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002640 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002641 found_bits = next_zero - i;
2642 break;
2643 }
2644 i = next_zero;
2645 }
2646
2647 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002648 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002649
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002650 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002651 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002652 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002653 }
2654
2655 total_found += found_bits;
2656
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002657 if (cluster->max_size < found_bits * ctl->unit)
2658 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002659
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002660 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2661 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002662 goto again;
2663 }
2664
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002665 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002666 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002667 ret = tree_insert_offset(&cluster->root, entry->offset,
2668 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002669 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002670
Josef Bacik3f7de032011-11-10 08:29:20 -05002671 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002672 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002673 return 0;
2674}
2675
Chris Masonfa9c0d792009-04-03 09:47:43 -04002676/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002677 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002678 * Try to find a cluster with at least bytes total bytes, at least one
2679 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002680 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002681static noinline int
2682setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2683 struct btrfs_free_cluster *cluster,
2684 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002685 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002686{
Li Zefan34d52cb2011-03-29 13:46:06 +08002687 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002688 struct btrfs_free_space *first = NULL;
2689 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002690 struct btrfs_free_space *last;
2691 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002692 u64 window_free;
2693 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002694 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002695
Li Zefan34d52cb2011-03-29 13:46:06 +08002696 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002697 if (!entry)
2698 return -ENOSPC;
2699
2700 /*
2701 * We don't want bitmaps, so just move along until we find a normal
2702 * extent entry.
2703 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002704 while (entry->bitmap || entry->bytes < min_bytes) {
2705 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002706 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002707 node = rb_next(&entry->offset_index);
2708 if (!node)
2709 return -ENOSPC;
2710 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2711 }
2712
Josef Bacik4e69b592011-03-21 10:11:24 -04002713 window_free = entry->bytes;
2714 max_extent = entry->bytes;
2715 first = entry;
2716 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002717
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002718 for (node = rb_next(&entry->offset_index); node;
2719 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002720 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2721
Josef Bacik86d4a772011-05-25 13:03:16 -04002722 if (entry->bitmap) {
2723 if (list_empty(&entry->list))
2724 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002725 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002726 }
2727
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002728 if (entry->bytes < min_bytes)
2729 continue;
2730
2731 last = entry;
2732 window_free += entry->bytes;
2733 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002734 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002735 }
2736
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002737 if (window_free < bytes || max_extent < cont1_bytes)
2738 return -ENOSPC;
2739
Josef Bacik4e69b592011-03-21 10:11:24 -04002740 cluster->window_start = first->offset;
2741
2742 node = &first->offset_index;
2743
2744 /*
2745 * now we've found our entries, pull them out of the free space
2746 * cache and put them into the cluster rbtree
2747 */
2748 do {
2749 int ret;
2750
2751 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2752 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002753 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002754 continue;
2755
Li Zefan34d52cb2011-03-29 13:46:06 +08002756 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002757 ret = tree_insert_offset(&cluster->root, entry->offset,
2758 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002759 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002760 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002761 } while (node && entry != last);
2762
2763 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002764 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002765 return 0;
2766}
2767
2768/*
2769 * This specifically looks for bitmaps that may work in the cluster, we assume
2770 * that we have already failed to find extents that will work.
2771 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002772static noinline int
2773setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2774 struct btrfs_free_cluster *cluster,
2775 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002776 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002777{
Li Zefan34d52cb2011-03-29 13:46:06 +08002778 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002779 struct btrfs_free_space *entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002780 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002781 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002782
Li Zefan34d52cb2011-03-29 13:46:06 +08002783 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002784 return -ENOSPC;
2785
Josef Bacik86d4a772011-05-25 13:03:16 -04002786 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002787 * The bitmap that covers offset won't be in the list unless offset
2788 * is just its start offset.
2789 */
2790 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2791 if (entry->offset != bitmap_offset) {
2792 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2793 if (entry && list_empty(&entry->list))
2794 list_add(&entry->list, bitmaps);
2795 }
2796
Josef Bacik86d4a772011-05-25 13:03:16 -04002797 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05002798 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04002799 continue;
2800 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002801 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002802 if (!ret)
2803 return 0;
2804 }
2805
2806 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002807 * The bitmaps list has all the bitmaps that record free space
2808 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002809 */
Li Zefan52621cb2011-11-20 07:33:38 -05002810 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002811}
2812
2813/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002814 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002815 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04002816 * We might not find them all in one contiguous area.
2817 *
2818 * returns zero and sets up cluster if things worked out, otherwise
2819 * it returns -enospc
2820 */
Josef Bacik00361582013-08-14 14:02:47 -04002821int btrfs_find_space_cluster(struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002822 struct btrfs_block_group_cache *block_group,
2823 struct btrfs_free_cluster *cluster,
2824 u64 offset, u64 bytes, u64 empty_size)
2825{
Li Zefan34d52cb2011-03-29 13:46:06 +08002826 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002827 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05002828 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002829 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002830 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002831 int ret;
2832
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002833 /*
2834 * Choose the minimum extent size we'll require for this
2835 * cluster. For SSD_SPREAD, don't allow any fragmentation.
2836 * For metadata, allow allocates with smaller extents. For
2837 * data, keep it dense.
2838 */
Chris Mason451d7582009-06-09 20:28:34 -04002839 if (btrfs_test_opt(root, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002840 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04002841 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002842 cont1_bytes = bytes;
2843 min_bytes = block_group->sectorsize;
2844 } else {
2845 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
2846 min_bytes = block_group->sectorsize;
2847 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002848
Li Zefan34d52cb2011-03-29 13:46:06 +08002849 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002850
2851 /*
2852 * If we know we don't have enough space to make a cluster don't even
2853 * bother doing all the work to try and find one.
2854 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002855 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002856 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002857 return -ENOSPC;
2858 }
2859
Chris Masonfa9c0d792009-04-03 09:47:43 -04002860 spin_lock(&cluster->lock);
2861
2862 /* someone already found a cluster, hooray */
2863 if (cluster->block_group) {
2864 ret = 0;
2865 goto out;
2866 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002867
Josef Bacik3f7de032011-11-10 08:29:20 -05002868 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
2869 min_bytes);
2870
2871 INIT_LIST_HEAD(&bitmaps);
Josef Bacik86d4a772011-05-25 13:03:16 -04002872 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002873 bytes + empty_size,
2874 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002875 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002876 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002877 offset, bytes + empty_size,
2878 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002879
2880 /* Clear our temporary list */
2881 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2882 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002883
2884 if (!ret) {
2885 atomic_inc(&block_group->count);
2886 list_add_tail(&cluster->block_group_list,
2887 &block_group->cluster_list);
2888 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05002889 } else {
2890 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002891 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002892out:
2893 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002894 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002895
2896 return ret;
2897}
2898
2899/*
2900 * simple code to zero out a cluster
2901 */
2902void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2903{
2904 spin_lock_init(&cluster->lock);
2905 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002906 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002907 cluster->max_size = 0;
2908 INIT_LIST_HEAD(&cluster->block_group_list);
2909 cluster->block_group = NULL;
2910}
2911
Li Zefan7fe1e642011-12-29 14:47:27 +08002912static int do_trimming(struct btrfs_block_group_cache *block_group,
2913 u64 *total_trimmed, u64 start, u64 bytes,
2914 u64 reserved_start, u64 reserved_bytes)
2915{
2916 struct btrfs_space_info *space_info = block_group->space_info;
2917 struct btrfs_fs_info *fs_info = block_group->fs_info;
2918 int ret;
2919 int update = 0;
2920 u64 trimmed = 0;
2921
2922 spin_lock(&space_info->lock);
2923 spin_lock(&block_group->lock);
2924 if (!block_group->ro) {
2925 block_group->reserved += reserved_bytes;
2926 space_info->bytes_reserved += reserved_bytes;
2927 update = 1;
2928 }
2929 spin_unlock(&block_group->lock);
2930 spin_unlock(&space_info->lock);
2931
2932 ret = btrfs_error_discard_extent(fs_info->extent_root,
2933 start, bytes, &trimmed);
2934 if (!ret)
2935 *total_trimmed += trimmed;
2936
2937 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
2938
2939 if (update) {
2940 spin_lock(&space_info->lock);
2941 spin_lock(&block_group->lock);
2942 if (block_group->ro)
2943 space_info->bytes_readonly += reserved_bytes;
2944 block_group->reserved -= reserved_bytes;
2945 space_info->bytes_reserved -= reserved_bytes;
2946 spin_unlock(&space_info->lock);
2947 spin_unlock(&block_group->lock);
2948 }
2949
2950 return ret;
2951}
2952
2953static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
2954 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00002955{
Li Zefan34d52cb2011-03-29 13:46:06 +08002956 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08002957 struct btrfs_free_space *entry;
2958 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00002959 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08002960 u64 extent_start;
2961 u64 extent_bytes;
2962 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00002963
2964 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002965 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002966
Li Zefan34d52cb2011-03-29 13:46:06 +08002967 if (ctl->free_space < minlen) {
2968 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002969 break;
2970 }
2971
Li Zefan34d52cb2011-03-29 13:46:06 +08002972 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08002973 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002974 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002975 break;
2976 }
2977
Li Zefan7fe1e642011-12-29 14:47:27 +08002978 /* skip bitmaps */
2979 while (entry->bitmap) {
2980 node = rb_next(&entry->offset_index);
2981 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002982 spin_unlock(&ctl->tree_lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08002983 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00002984 }
Li Zefan7fe1e642011-12-29 14:47:27 +08002985 entry = rb_entry(node, struct btrfs_free_space,
2986 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00002987 }
2988
Li Zefan7fe1e642011-12-29 14:47:27 +08002989 if (entry->offset >= end) {
2990 spin_unlock(&ctl->tree_lock);
2991 break;
2992 }
2993
2994 extent_start = entry->offset;
2995 extent_bytes = entry->bytes;
2996 start = max(start, extent_start);
2997 bytes = min(extent_start + extent_bytes, end) - start;
2998 if (bytes < minlen) {
2999 spin_unlock(&ctl->tree_lock);
3000 goto next;
3001 }
3002
3003 unlink_free_space(ctl, entry);
3004 kmem_cache_free(btrfs_free_space_cachep, entry);
3005
Li Zefan34d52cb2011-03-29 13:46:06 +08003006 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003007
Li Zefan7fe1e642011-12-29 14:47:27 +08003008 ret = do_trimming(block_group, total_trimmed, start, bytes,
3009 extent_start, extent_bytes);
3010 if (ret)
3011 break;
3012next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003013 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003014
3015 if (fatal_signal_pending(current)) {
3016 ret = -ERESTARTSYS;
3017 break;
3018 }
3019
3020 cond_resched();
3021 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003022out:
3023 return ret;
3024}
3025
3026static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3027 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3028{
3029 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3030 struct btrfs_free_space *entry;
3031 int ret = 0;
3032 int ret2;
3033 u64 bytes;
3034 u64 offset = offset_to_bitmap(ctl, start);
3035
3036 while (offset < end) {
3037 bool next_bitmap = false;
3038
3039 spin_lock(&ctl->tree_lock);
3040
3041 if (ctl->free_space < minlen) {
3042 spin_unlock(&ctl->tree_lock);
3043 break;
3044 }
3045
3046 entry = tree_search_offset(ctl, offset, 1, 0);
3047 if (!entry) {
3048 spin_unlock(&ctl->tree_lock);
3049 next_bitmap = true;
3050 goto next;
3051 }
3052
3053 bytes = minlen;
3054 ret2 = search_bitmap(ctl, entry, &start, &bytes);
3055 if (ret2 || start >= end) {
3056 spin_unlock(&ctl->tree_lock);
3057 next_bitmap = true;
3058 goto next;
3059 }
3060
3061 bytes = min(bytes, end - start);
3062 if (bytes < minlen) {
3063 spin_unlock(&ctl->tree_lock);
3064 goto next;
3065 }
3066
3067 bitmap_clear_bits(ctl, entry, start, bytes);
3068 if (entry->bytes == 0)
3069 free_bitmap(ctl, entry);
3070
3071 spin_unlock(&ctl->tree_lock);
3072
3073 ret = do_trimming(block_group, total_trimmed, start, bytes,
3074 start, bytes);
3075 if (ret)
3076 break;
3077next:
3078 if (next_bitmap) {
3079 offset += BITS_PER_BITMAP * ctl->unit;
3080 } else {
3081 start += bytes;
3082 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3083 offset += BITS_PER_BITMAP * ctl->unit;
3084 }
3085
3086 if (fatal_signal_pending(current)) {
3087 ret = -ERESTARTSYS;
3088 break;
3089 }
3090
3091 cond_resched();
3092 }
3093
3094 return ret;
3095}
3096
3097int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3098 u64 *trimmed, u64 start, u64 end, u64 minlen)
3099{
3100 int ret;
3101
3102 *trimmed = 0;
3103
3104 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3105 if (ret)
3106 return ret;
3107
3108 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
Li Dongyangf7039b12011-03-24 10:24:28 +00003109
3110 return ret;
3111}
Li Zefan581bb052011-04-20 10:06:11 +08003112
3113/*
3114 * Find the left-most item in the cache tree, and then return the
3115 * smallest inode number in the item.
3116 *
3117 * Note: the returned inode number may not be the smallest one in
3118 * the tree, if the left-most item is a bitmap.
3119 */
3120u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3121{
3122 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3123 struct btrfs_free_space *entry = NULL;
3124 u64 ino = 0;
3125
3126 spin_lock(&ctl->tree_lock);
3127
3128 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3129 goto out;
3130
3131 entry = rb_entry(rb_first(&ctl->free_space_offset),
3132 struct btrfs_free_space, offset_index);
3133
3134 if (!entry->bitmap) {
3135 ino = entry->offset;
3136
3137 unlink_free_space(ctl, entry);
3138 entry->offset++;
3139 entry->bytes--;
3140 if (!entry->bytes)
3141 kmem_cache_free(btrfs_free_space_cachep, entry);
3142 else
3143 link_free_space(ctl, entry);
3144 } else {
3145 u64 offset = 0;
3146 u64 count = 1;
3147 int ret;
3148
3149 ret = search_bitmap(ctl, entry, &offset, &count);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003150 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003151 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003152
3153 ino = offset;
3154 bitmap_clear_bits(ctl, entry, offset, 1);
3155 if (entry->bytes == 0)
3156 free_bitmap(ctl, entry);
3157 }
3158out:
3159 spin_unlock(&ctl->tree_lock);
3160
3161 return ino;
3162}
Li Zefan82d59022011-04-20 10:33:24 +08003163
3164struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3165 struct btrfs_path *path)
3166{
3167 struct inode *inode = NULL;
3168
David Sterba57cdc8d2014-02-05 02:37:48 +01003169 spin_lock(&root->ino_cache_lock);
3170 if (root->ino_cache_inode)
3171 inode = igrab(root->ino_cache_inode);
3172 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003173 if (inode)
3174 return inode;
3175
3176 inode = __lookup_free_space_inode(root, path, 0);
3177 if (IS_ERR(inode))
3178 return inode;
3179
David Sterba57cdc8d2014-02-05 02:37:48 +01003180 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003181 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003182 root->ino_cache_inode = igrab(inode);
3183 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003184
3185 return inode;
3186}
3187
3188int create_free_ino_inode(struct btrfs_root *root,
3189 struct btrfs_trans_handle *trans,
3190 struct btrfs_path *path)
3191{
3192 return __create_free_space_inode(root, trans, path,
3193 BTRFS_FREE_INO_OBJECTID, 0);
3194}
3195
3196int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3197{
3198 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3199 struct btrfs_path *path;
3200 struct inode *inode;
3201 int ret = 0;
3202 u64 root_gen = btrfs_root_generation(&root->root_item);
3203
Chris Mason4b9465c2011-06-03 09:36:29 -04003204 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3205 return 0;
3206
Li Zefan82d59022011-04-20 10:33:24 +08003207 /*
3208 * If we're unmounting then just return, since this does a search on the
3209 * normal root and not the commit root and we could deadlock.
3210 */
David Sterba7841cb22011-05-31 18:07:27 +02003211 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003212 return 0;
3213
3214 path = btrfs_alloc_path();
3215 if (!path)
3216 return 0;
3217
3218 inode = lookup_free_ino_inode(root, path);
3219 if (IS_ERR(inode))
3220 goto out;
3221
3222 if (root_gen != BTRFS_I(inode)->generation)
3223 goto out_put;
3224
3225 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3226
3227 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003228 btrfs_err(fs_info,
3229 "failed to load free ino cache for root %llu",
3230 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003231out_put:
3232 iput(inode);
3233out:
3234 btrfs_free_path(path);
3235 return ret;
3236}
3237
3238int btrfs_write_out_ino_cache(struct btrfs_root *root,
3239 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003240 struct btrfs_path *path,
3241 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003242{
3243 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003244 int ret;
3245
Chris Mason4b9465c2011-06-03 09:36:29 -04003246 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3247 return 0;
3248
Li Zefan82d59022011-04-20 10:33:24 +08003249 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04003250 if (ret) {
3251 btrfs_delalloc_release_metadata(inode, inode->i_size);
3252#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003253 btrfs_err(root->fs_info,
3254 "failed to write free ino cache for root %llu",
3255 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003256#endif
3257 }
Li Zefan82d59022011-04-20 10:33:24 +08003258
Li Zefan82d59022011-04-20 10:33:24 +08003259 return ret;
3260}
Josef Bacik74255aa2013-03-15 09:47:08 -04003261
3262#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003263/*
3264 * Use this if you need to make a bitmap or extent entry specifically, it
3265 * doesn't do any of the merging that add_free_space does, this acts a lot like
3266 * how the free space cache loading stuff works, so you can get really weird
3267 * configurations.
3268 */
3269int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3270 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003271{
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003272 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3273 struct btrfs_free_space *info = NULL, *bitmap_info;
3274 void *map = NULL;
3275 u64 bytes_added;
3276 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003277
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003278again:
3279 if (!info) {
3280 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3281 if (!info)
3282 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003283 }
3284
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003285 if (!bitmap) {
3286 spin_lock(&ctl->tree_lock);
3287 info->offset = offset;
3288 info->bytes = bytes;
3289 ret = link_free_space(ctl, info);
3290 spin_unlock(&ctl->tree_lock);
3291 if (ret)
3292 kmem_cache_free(btrfs_free_space_cachep, info);
3293 return ret;
3294 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003295
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003296 if (!map) {
3297 map = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
3298 if (!map) {
3299 kmem_cache_free(btrfs_free_space_cachep, info);
3300 return -ENOMEM;
3301 }
3302 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003303
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003304 spin_lock(&ctl->tree_lock);
3305 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3306 1, 0);
3307 if (!bitmap_info) {
3308 info->bitmap = map;
3309 map = NULL;
3310 add_new_bitmap(ctl, info, offset);
3311 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003312 info = NULL;
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003313 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003314
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003315 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3316 bytes -= bytes_added;
3317 offset += bytes_added;
3318 spin_unlock(&ctl->tree_lock);
3319
3320 if (bytes)
3321 goto again;
3322
Filipe Manana20005522014-08-29 13:35:13 +01003323 if (info)
3324 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003325 if (map)
3326 kfree(map);
3327 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003328}
3329
3330/*
3331 * Checks to see if the given range is in the free space cache. This is really
3332 * just used to check the absence of space, so if there is free space in the
3333 * range at all we will return 1.
3334 */
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003335int test_check_exists(struct btrfs_block_group_cache *cache,
3336 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003337{
3338 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3339 struct btrfs_free_space *info;
3340 int ret = 0;
3341
3342 spin_lock(&ctl->tree_lock);
3343 info = tree_search_offset(ctl, offset, 0, 0);
3344 if (!info) {
3345 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3346 1, 0);
3347 if (!info)
3348 goto out;
3349 }
3350
3351have_info:
3352 if (info->bitmap) {
3353 u64 bit_off, bit_bytes;
3354 struct rb_node *n;
3355 struct btrfs_free_space *tmp;
3356
3357 bit_off = offset;
3358 bit_bytes = ctl->unit;
3359 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes);
3360 if (!ret) {
3361 if (bit_off == offset) {
3362 ret = 1;
3363 goto out;
3364 } else if (bit_off > offset &&
3365 offset + bytes > bit_off) {
3366 ret = 1;
3367 goto out;
3368 }
3369 }
3370
3371 n = rb_prev(&info->offset_index);
3372 while (n) {
3373 tmp = rb_entry(n, struct btrfs_free_space,
3374 offset_index);
3375 if (tmp->offset + tmp->bytes < offset)
3376 break;
3377 if (offset + bytes < tmp->offset) {
3378 n = rb_prev(&info->offset_index);
3379 continue;
3380 }
3381 info = tmp;
3382 goto have_info;
3383 }
3384
3385 n = rb_next(&info->offset_index);
3386 while (n) {
3387 tmp = rb_entry(n, struct btrfs_free_space,
3388 offset_index);
3389 if (offset + bytes < tmp->offset)
3390 break;
3391 if (tmp->offset + tmp->bytes < offset) {
3392 n = rb_next(&info->offset_index);
3393 continue;
3394 }
3395 info = tmp;
3396 goto have_info;
3397 }
3398
Filipe Manana20005522014-08-29 13:35:13 +01003399 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003400 goto out;
3401 }
3402
3403 if (info->offset == offset) {
3404 ret = 1;
3405 goto out;
3406 }
3407
3408 if (offset > info->offset && offset < info->offset + info->bytes)
3409 ret = 1;
3410out:
3411 spin_unlock(&ctl->tree_lock);
3412 return ret;
3413}
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003414#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */