blob: 2f0fe1028e51f3170fd7affe63b6f8797dfd0131 [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
Li Zefan581bb052011-04-20 10:06:11 +08002000int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
2001 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002002{
2003 struct btrfs_free_space *info;
2004 int ret = 0;
2005
Josef Bacikdc89e982011-01-28 17:05:48 -05002006 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002007 if (!info)
2008 return -ENOMEM;
2009
2010 info->offset = offset;
2011 info->bytes = bytes;
2012
Li Zefan34d52cb2011-03-29 13:46:06 +08002013 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002014
Li Zefan34d52cb2011-03-29 13:46:06 +08002015 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002016 goto link;
2017
2018 /*
2019 * There was no extent directly to the left or right of this new
2020 * extent then we know we're going to have to allocate a new extent, so
2021 * before we do that see if we need to drop this into a bitmap
2022 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002023 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002024 if (ret < 0) {
2025 goto out;
2026 } else if (ret) {
2027 ret = 0;
2028 goto out;
2029 }
2030link:
Li Zefan34d52cb2011-03-29 13:46:06 +08002031 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002032 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002033 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002034out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002035 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002036
Josef Bacik0f9dd462008-09-23 13:14:11 -04002037 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002038 printk(KERN_CRIT "BTRFS: unable to add free space :%d\n", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002039 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002040 }
2041
Josef Bacik0f9dd462008-09-23 13:14:11 -04002042 return ret;
2043}
2044
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002045int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2046 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002047{
Li Zefan34d52cb2011-03-29 13:46:06 +08002048 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002049 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002050 int ret;
2051 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002052
Li Zefan34d52cb2011-03-29 13:46:06 +08002053 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002054
Josef Bacik96303082009-07-13 21:29:25 -04002055again:
Josef Bacikb0175112012-12-18 11:39:19 -05002056 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002057 if (!bytes)
2058 goto out_lock;
2059
Li Zefan34d52cb2011-03-29 13:46:06 +08002060 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002061 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002062 /*
2063 * oops didn't find an extent that matched the space we wanted
2064 * to remove, look for a bitmap instead
2065 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002066 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002067 1, 0);
2068 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002069 /*
2070 * If we found a partial bit of our free space in a
2071 * bitmap but then couldn't find the other part this may
2072 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002073 */
Josef Bacikb0175112012-12-18 11:39:19 -05002074 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002075 goto out_lock;
2076 }
Josef Bacik96303082009-07-13 21:29:25 -04002077 }
2078
Josef Bacikb0175112012-12-18 11:39:19 -05002079 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002080 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002081 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002082 if (offset == info->offset) {
2083 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002084
Josef Bacikbdb7d302012-06-27 15:10:56 -04002085 info->bytes -= to_free;
2086 info->offset += to_free;
2087 if (info->bytes) {
2088 ret = link_free_space(ctl, info);
2089 WARN_ON(ret);
2090 } else {
2091 kmem_cache_free(btrfs_free_space_cachep, info);
2092 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002093
Josef Bacikbdb7d302012-06-27 15:10:56 -04002094 offset += to_free;
2095 bytes -= to_free;
2096 goto again;
2097 } else {
2098 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002099
Josef Bacikbdb7d302012-06-27 15:10:56 -04002100 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002101 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002102 WARN_ON(ret);
2103 if (ret)
2104 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002105
Josef Bacikbdb7d302012-06-27 15:10:56 -04002106 /* Not enough bytes in this entry to satisfy us */
2107 if (old_end < offset + bytes) {
2108 bytes -= old_end - offset;
2109 offset = old_end;
2110 goto again;
2111 } else if (old_end == offset + bytes) {
2112 /* all done */
2113 goto out_lock;
2114 }
2115 spin_unlock(&ctl->tree_lock);
2116
2117 ret = btrfs_add_free_space(block_group, offset + bytes,
2118 old_end - (offset + bytes));
2119 WARN_ON(ret);
2120 goto out;
2121 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002122 }
Josef Bacik96303082009-07-13 21:29:25 -04002123
Li Zefan34d52cb2011-03-29 13:46:06 +08002124 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002125 if (ret == -EAGAIN) {
2126 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002127 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002128 }
Josef Bacik96303082009-07-13 21:29:25 -04002129out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002130 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002131out:
Josef Bacik25179202008-10-29 14:49:05 -04002132 return ret;
2133}
2134
Josef Bacik0f9dd462008-09-23 13:14:11 -04002135void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2136 u64 bytes)
2137{
Li Zefan34d52cb2011-03-29 13:46:06 +08002138 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002139 struct btrfs_free_space *info;
2140 struct rb_node *n;
2141 int count = 0;
2142
Li Zefan34d52cb2011-03-29 13:46:06 +08002143 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002144 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002145 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002146 count++;
Frank Holtonefe120a2013-12-20 11:37:06 -05002147 btrfs_crit(block_group->fs_info,
2148 "entry offset %llu, bytes %llu, bitmap %s",
2149 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002150 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002151 }
Frank Holtonefe120a2013-12-20 11:37:06 -05002152 btrfs_info(block_group->fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002153 list_empty(&block_group->cluster_list) ? "no" : "yes");
Frank Holtonefe120a2013-12-20 11:37:06 -05002154 btrfs_info(block_group->fs_info,
2155 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002156}
2157
Li Zefan34d52cb2011-03-29 13:46:06 +08002158void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002159{
Li Zefan34d52cb2011-03-29 13:46:06 +08002160 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002161
Li Zefan34d52cb2011-03-29 13:46:06 +08002162 spin_lock_init(&ctl->tree_lock);
2163 ctl->unit = block_group->sectorsize;
2164 ctl->start = block_group->key.objectid;
2165 ctl->private = block_group;
2166 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002167
Li Zefan34d52cb2011-03-29 13:46:06 +08002168 /*
2169 * we only want to have 32k of ram per block group for keeping
2170 * track of free space, and if we pass 1/2 of that we want to
2171 * start converting things over to using bitmaps
2172 */
2173 ctl->extents_thresh = ((1024 * 32) / 2) /
2174 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002175}
2176
Chris Masonfa9c0d792009-04-03 09:47:43 -04002177/*
2178 * for a given cluster, put all of its extents back into the free
2179 * space cache. If the block group passed doesn't match the block group
2180 * pointed to by the cluster, someone else raced in and freed the
2181 * cluster already. In that case, we just return without changing anything
2182 */
2183static int
2184__btrfs_return_cluster_to_free_space(
2185 struct btrfs_block_group_cache *block_group,
2186 struct btrfs_free_cluster *cluster)
2187{
Li Zefan34d52cb2011-03-29 13:46:06 +08002188 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002189 struct btrfs_free_space *entry;
2190 struct rb_node *node;
2191
2192 spin_lock(&cluster->lock);
2193 if (cluster->block_group != block_group)
2194 goto out;
2195
Josef Bacik96303082009-07-13 21:29:25 -04002196 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002197 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002198 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002199
Chris Masonfa9c0d792009-04-03 09:47:43 -04002200 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002201 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002202 bool bitmap;
2203
Chris Masonfa9c0d792009-04-03 09:47:43 -04002204 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2205 node = rb_next(&entry->offset_index);
2206 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04002207
2208 bitmap = (entry->bitmap != NULL);
2209 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08002210 try_merge_free_space(ctl, entry, false);
2211 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002212 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002213 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002214 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002215
Chris Masonfa9c0d792009-04-03 09:47:43 -04002216out:
2217 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002218 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002219 return 0;
2220}
2221
Eric Sandeen48a3b632013-04-25 20:41:01 +00002222static void __btrfs_remove_free_space_cache_locked(
2223 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002224{
2225 struct btrfs_free_space *info;
2226 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002227
Li Zefan581bb052011-04-20 10:06:11 +08002228 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2229 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002230 if (!info->bitmap) {
2231 unlink_free_space(ctl, info);
2232 kmem_cache_free(btrfs_free_space_cachep, info);
2233 } else {
2234 free_bitmap(ctl, info);
2235 }
Li Zefan581bb052011-04-20 10:06:11 +08002236 if (need_resched()) {
2237 spin_unlock(&ctl->tree_lock);
2238 cond_resched();
2239 spin_lock(&ctl->tree_lock);
2240 }
2241 }
Chris Mason09655372011-05-21 09:27:38 -04002242}
2243
2244void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2245{
2246 spin_lock(&ctl->tree_lock);
2247 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002248 spin_unlock(&ctl->tree_lock);
2249}
2250
Josef Bacik0f9dd462008-09-23 13:14:11 -04002251void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2252{
Li Zefan34d52cb2011-03-29 13:46:06 +08002253 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002254 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002255 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002256
Li Zefan34d52cb2011-03-29 13:46:06 +08002257 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002258 while ((head = block_group->cluster_list.next) !=
2259 &block_group->cluster_list) {
2260 cluster = list_entry(head, struct btrfs_free_cluster,
2261 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002262
2263 WARN_ON(cluster->block_group != block_group);
2264 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04002265 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002266 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002267 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08002268 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002269 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002270 }
Chris Mason09655372011-05-21 09:27:38 -04002271 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002272 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002273
Josef Bacik0f9dd462008-09-23 13:14:11 -04002274}
2275
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002276u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002277 u64 offset, u64 bytes, u64 empty_size,
2278 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002279{
Li Zefan34d52cb2011-03-29 13:46:06 +08002280 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002281 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002282 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002283 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002284 u64 align_gap = 0;
2285 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002286
Li Zefan34d52cb2011-03-29 13:46:06 +08002287 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002288 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002289 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002290 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002291 goto out;
2292
2293 ret = offset;
2294 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002295 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002296 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002297 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002298 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002299 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002300 align_gap_len = offset - entry->offset;
2301 align_gap = entry->offset;
2302
2303 entry->offset = offset + bytes;
2304 WARN_ON(entry->bytes < bytes + align_gap_len);
2305
2306 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002307 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002308 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002309 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002310 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002311 }
Josef Bacik96303082009-07-13 21:29:25 -04002312out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002313 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002314
David Woodhouse53b381b2013-01-29 18:40:14 -05002315 if (align_gap_len)
2316 __btrfs_add_free_space(ctl, align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002317 return ret;
2318}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002319
2320/*
2321 * given a cluster, put all of its extents back into the free space
2322 * cache. If a block group is passed, this function will only free
2323 * a cluster that belongs to the passed block group.
2324 *
2325 * Otherwise, it'll get a reference on the block group pointed to by the
2326 * cluster and remove the cluster from it.
2327 */
2328int btrfs_return_cluster_to_free_space(
2329 struct btrfs_block_group_cache *block_group,
2330 struct btrfs_free_cluster *cluster)
2331{
Li Zefan34d52cb2011-03-29 13:46:06 +08002332 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002333 int ret;
2334
2335 /* first, get a safe pointer to the block group */
2336 spin_lock(&cluster->lock);
2337 if (!block_group) {
2338 block_group = cluster->block_group;
2339 if (!block_group) {
2340 spin_unlock(&cluster->lock);
2341 return 0;
2342 }
2343 } else if (cluster->block_group != block_group) {
2344 /* someone else has already freed it don't redo their work */
2345 spin_unlock(&cluster->lock);
2346 return 0;
2347 }
2348 atomic_inc(&block_group->count);
2349 spin_unlock(&cluster->lock);
2350
Li Zefan34d52cb2011-03-29 13:46:06 +08002351 ctl = block_group->free_space_ctl;
2352
Chris Masonfa9c0d792009-04-03 09:47:43 -04002353 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002354 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002355 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002356 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002357
2358 /* finally drop our ref */
2359 btrfs_put_block_group(block_group);
2360 return ret;
2361}
2362
Josef Bacik96303082009-07-13 21:29:25 -04002363static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2364 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002365 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002366 u64 bytes, u64 min_start,
2367 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002368{
Li Zefan34d52cb2011-03-29 13:46:06 +08002369 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002370 int err;
2371 u64 search_start = cluster->window_start;
2372 u64 search_bytes = bytes;
2373 u64 ret = 0;
2374
Josef Bacik96303082009-07-13 21:29:25 -04002375 search_start = min_start;
2376 search_bytes = bytes;
2377
Li Zefan34d52cb2011-03-29 13:46:06 +08002378 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Miao Xiea4820392013-09-09 13:19:42 +08002379 if (err) {
2380 if (search_bytes > *max_extent_size)
2381 *max_extent_size = search_bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002382 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002383 }
Josef Bacik96303082009-07-13 21:29:25 -04002384
2385 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002386 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002387
2388 return ret;
2389}
2390
Chris Masonfa9c0d792009-04-03 09:47:43 -04002391/*
2392 * given a cluster, try to allocate 'bytes' from it, returns 0
2393 * if it couldn't find anything suitably large, or a logical disk offset
2394 * if things worked out
2395 */
2396u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2397 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002398 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002399{
Li Zefan34d52cb2011-03-29 13:46:06 +08002400 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002401 struct btrfs_free_space *entry = NULL;
2402 struct rb_node *node;
2403 u64 ret = 0;
2404
2405 spin_lock(&cluster->lock);
2406 if (bytes > cluster->max_size)
2407 goto out;
2408
2409 if (cluster->block_group != block_group)
2410 goto out;
2411
2412 node = rb_first(&cluster->root);
2413 if (!node)
2414 goto out;
2415
2416 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302417 while (1) {
Miao Xiea4820392013-09-09 13:19:42 +08002418 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2419 *max_extent_size = entry->bytes;
2420
Josef Bacik4e69b592011-03-21 10:11:24 -04002421 if (entry->bytes < bytes ||
2422 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002423 node = rb_next(&entry->offset_index);
2424 if (!node)
2425 break;
2426 entry = rb_entry(node, struct btrfs_free_space,
2427 offset_index);
2428 continue;
2429 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002430
Josef Bacik4e69b592011-03-21 10:11:24 -04002431 if (entry->bitmap) {
2432 ret = btrfs_alloc_from_bitmap(block_group,
2433 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002434 cluster->window_start,
2435 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002436 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002437 node = rb_next(&entry->offset_index);
2438 if (!node)
2439 break;
2440 entry = rb_entry(node, struct btrfs_free_space,
2441 offset_index);
2442 continue;
2443 }
Josef Bacik9b230622012-01-26 15:01:12 -05002444 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002445 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002446 ret = entry->offset;
2447
2448 entry->offset += bytes;
2449 entry->bytes -= bytes;
2450 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002451
Li Zefan5e71b5d2010-11-09 14:55:34 +08002452 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002453 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002454 break;
2455 }
2456out:
2457 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002458
Li Zefan5e71b5d2010-11-09 14:55:34 +08002459 if (!ret)
2460 return 0;
2461
Li Zefan34d52cb2011-03-29 13:46:06 +08002462 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002463
Li Zefan34d52cb2011-03-29 13:46:06 +08002464 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002465 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002466 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002467 if (entry->bitmap) {
2468 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002469 ctl->total_bitmaps--;
2470 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002471 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002472 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002473 }
2474
Li Zefan34d52cb2011-03-29 13:46:06 +08002475 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002476
Chris Masonfa9c0d792009-04-03 09:47:43 -04002477 return ret;
2478}
2479
Josef Bacik96303082009-07-13 21:29:25 -04002480static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2481 struct btrfs_free_space *entry,
2482 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002483 u64 offset, u64 bytes,
2484 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002485{
Li Zefan34d52cb2011-03-29 13:46:06 +08002486 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002487 unsigned long next_zero;
2488 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002489 unsigned long want_bits;
2490 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002491 unsigned long found_bits;
2492 unsigned long start = 0;
2493 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002494 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002495
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002496 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002497 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002498 want_bits = bytes_to_bits(bytes, ctl->unit);
2499 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002500
2501again:
2502 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002503 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002504 next_zero = find_next_zero_bit(entry->bitmap,
2505 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002506 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002507 found_bits = next_zero - i;
2508 break;
2509 }
2510 i = next_zero;
2511 }
2512
2513 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002514 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002515
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002516 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002517 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002518 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002519 }
2520
2521 total_found += found_bits;
2522
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002523 if (cluster->max_size < found_bits * ctl->unit)
2524 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002525
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002526 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2527 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002528 goto again;
2529 }
2530
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002531 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002532 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002533 ret = tree_insert_offset(&cluster->root, entry->offset,
2534 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002535 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002536
Josef Bacik3f7de032011-11-10 08:29:20 -05002537 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002538 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002539 return 0;
2540}
2541
Chris Masonfa9c0d792009-04-03 09:47:43 -04002542/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002543 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002544 * Try to find a cluster with at least bytes total bytes, at least one
2545 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002546 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002547static noinline int
2548setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2549 struct btrfs_free_cluster *cluster,
2550 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002551 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002552{
Li Zefan34d52cb2011-03-29 13:46:06 +08002553 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002554 struct btrfs_free_space *first = NULL;
2555 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002556 struct btrfs_free_space *last;
2557 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002558 u64 window_free;
2559 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002560 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002561
Li Zefan34d52cb2011-03-29 13:46:06 +08002562 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002563 if (!entry)
2564 return -ENOSPC;
2565
2566 /*
2567 * We don't want bitmaps, so just move along until we find a normal
2568 * extent entry.
2569 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002570 while (entry->bitmap || entry->bytes < min_bytes) {
2571 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002572 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002573 node = rb_next(&entry->offset_index);
2574 if (!node)
2575 return -ENOSPC;
2576 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2577 }
2578
Josef Bacik4e69b592011-03-21 10:11:24 -04002579 window_free = entry->bytes;
2580 max_extent = entry->bytes;
2581 first = entry;
2582 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002583
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002584 for (node = rb_next(&entry->offset_index); node;
2585 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002586 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2587
Josef Bacik86d4a772011-05-25 13:03:16 -04002588 if (entry->bitmap) {
2589 if (list_empty(&entry->list))
2590 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002591 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002592 }
2593
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002594 if (entry->bytes < min_bytes)
2595 continue;
2596
2597 last = entry;
2598 window_free += entry->bytes;
2599 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002600 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002601 }
2602
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002603 if (window_free < bytes || max_extent < cont1_bytes)
2604 return -ENOSPC;
2605
Josef Bacik4e69b592011-03-21 10:11:24 -04002606 cluster->window_start = first->offset;
2607
2608 node = &first->offset_index;
2609
2610 /*
2611 * now we've found our entries, pull them out of the free space
2612 * cache and put them into the cluster rbtree
2613 */
2614 do {
2615 int ret;
2616
2617 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2618 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002619 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002620 continue;
2621
Li Zefan34d52cb2011-03-29 13:46:06 +08002622 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002623 ret = tree_insert_offset(&cluster->root, entry->offset,
2624 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002625 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002626 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002627 } while (node && entry != last);
2628
2629 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002630 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002631 return 0;
2632}
2633
2634/*
2635 * This specifically looks for bitmaps that may work in the cluster, we assume
2636 * that we have already failed to find extents that will work.
2637 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002638static noinline int
2639setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2640 struct btrfs_free_cluster *cluster,
2641 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002642 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002643{
Li Zefan34d52cb2011-03-29 13:46:06 +08002644 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002645 struct btrfs_free_space *entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002646 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002647 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002648
Li Zefan34d52cb2011-03-29 13:46:06 +08002649 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002650 return -ENOSPC;
2651
Josef Bacik86d4a772011-05-25 13:03:16 -04002652 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002653 * The bitmap that covers offset won't be in the list unless offset
2654 * is just its start offset.
2655 */
2656 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2657 if (entry->offset != bitmap_offset) {
2658 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2659 if (entry && list_empty(&entry->list))
2660 list_add(&entry->list, bitmaps);
2661 }
2662
Josef Bacik86d4a772011-05-25 13:03:16 -04002663 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05002664 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04002665 continue;
2666 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002667 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002668 if (!ret)
2669 return 0;
2670 }
2671
2672 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002673 * The bitmaps list has all the bitmaps that record free space
2674 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002675 */
Li Zefan52621cb2011-11-20 07:33:38 -05002676 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002677}
2678
2679/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002680 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002681 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04002682 * We might not find them all in one contiguous area.
2683 *
2684 * returns zero and sets up cluster if things worked out, otherwise
2685 * it returns -enospc
2686 */
Josef Bacik00361582013-08-14 14:02:47 -04002687int btrfs_find_space_cluster(struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002688 struct btrfs_block_group_cache *block_group,
2689 struct btrfs_free_cluster *cluster,
2690 u64 offset, u64 bytes, u64 empty_size)
2691{
Li Zefan34d52cb2011-03-29 13:46:06 +08002692 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002693 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05002694 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002695 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002696 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002697 int ret;
2698
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002699 /*
2700 * Choose the minimum extent size we'll require for this
2701 * cluster. For SSD_SPREAD, don't allow any fragmentation.
2702 * For metadata, allow allocates with smaller extents. For
2703 * data, keep it dense.
2704 */
Chris Mason451d7582009-06-09 20:28:34 -04002705 if (btrfs_test_opt(root, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002706 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04002707 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002708 cont1_bytes = bytes;
2709 min_bytes = block_group->sectorsize;
2710 } else {
2711 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
2712 min_bytes = block_group->sectorsize;
2713 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002714
Li Zefan34d52cb2011-03-29 13:46:06 +08002715 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002716
2717 /*
2718 * If we know we don't have enough space to make a cluster don't even
2719 * bother doing all the work to try and find one.
2720 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002721 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002722 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002723 return -ENOSPC;
2724 }
2725
Chris Masonfa9c0d792009-04-03 09:47:43 -04002726 spin_lock(&cluster->lock);
2727
2728 /* someone already found a cluster, hooray */
2729 if (cluster->block_group) {
2730 ret = 0;
2731 goto out;
2732 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002733
Josef Bacik3f7de032011-11-10 08:29:20 -05002734 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
2735 min_bytes);
2736
2737 INIT_LIST_HEAD(&bitmaps);
Josef Bacik86d4a772011-05-25 13:03:16 -04002738 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002739 bytes + empty_size,
2740 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002741 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002742 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002743 offset, bytes + empty_size,
2744 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002745
2746 /* Clear our temporary list */
2747 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2748 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002749
2750 if (!ret) {
2751 atomic_inc(&block_group->count);
2752 list_add_tail(&cluster->block_group_list,
2753 &block_group->cluster_list);
2754 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05002755 } else {
2756 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002757 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002758out:
2759 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002760 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002761
2762 return ret;
2763}
2764
2765/*
2766 * simple code to zero out a cluster
2767 */
2768void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2769{
2770 spin_lock_init(&cluster->lock);
2771 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002772 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002773 cluster->max_size = 0;
2774 INIT_LIST_HEAD(&cluster->block_group_list);
2775 cluster->block_group = NULL;
2776}
2777
Li Zefan7fe1e642011-12-29 14:47:27 +08002778static int do_trimming(struct btrfs_block_group_cache *block_group,
2779 u64 *total_trimmed, u64 start, u64 bytes,
2780 u64 reserved_start, u64 reserved_bytes)
2781{
2782 struct btrfs_space_info *space_info = block_group->space_info;
2783 struct btrfs_fs_info *fs_info = block_group->fs_info;
2784 int ret;
2785 int update = 0;
2786 u64 trimmed = 0;
2787
2788 spin_lock(&space_info->lock);
2789 spin_lock(&block_group->lock);
2790 if (!block_group->ro) {
2791 block_group->reserved += reserved_bytes;
2792 space_info->bytes_reserved += reserved_bytes;
2793 update = 1;
2794 }
2795 spin_unlock(&block_group->lock);
2796 spin_unlock(&space_info->lock);
2797
2798 ret = btrfs_error_discard_extent(fs_info->extent_root,
2799 start, bytes, &trimmed);
2800 if (!ret)
2801 *total_trimmed += trimmed;
2802
2803 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
2804
2805 if (update) {
2806 spin_lock(&space_info->lock);
2807 spin_lock(&block_group->lock);
2808 if (block_group->ro)
2809 space_info->bytes_readonly += reserved_bytes;
2810 block_group->reserved -= reserved_bytes;
2811 space_info->bytes_reserved -= reserved_bytes;
2812 spin_unlock(&space_info->lock);
2813 spin_unlock(&block_group->lock);
2814 }
2815
2816 return ret;
2817}
2818
2819static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
2820 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00002821{
Li Zefan34d52cb2011-03-29 13:46:06 +08002822 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08002823 struct btrfs_free_space *entry;
2824 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00002825 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08002826 u64 extent_start;
2827 u64 extent_bytes;
2828 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00002829
2830 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002831 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002832
Li Zefan34d52cb2011-03-29 13:46:06 +08002833 if (ctl->free_space < minlen) {
2834 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002835 break;
2836 }
2837
Li Zefan34d52cb2011-03-29 13:46:06 +08002838 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08002839 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002840 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002841 break;
2842 }
2843
Li Zefan7fe1e642011-12-29 14:47:27 +08002844 /* skip bitmaps */
2845 while (entry->bitmap) {
2846 node = rb_next(&entry->offset_index);
2847 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002848 spin_unlock(&ctl->tree_lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08002849 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00002850 }
Li Zefan7fe1e642011-12-29 14:47:27 +08002851 entry = rb_entry(node, struct btrfs_free_space,
2852 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00002853 }
2854
Li Zefan7fe1e642011-12-29 14:47:27 +08002855 if (entry->offset >= end) {
2856 spin_unlock(&ctl->tree_lock);
2857 break;
2858 }
2859
2860 extent_start = entry->offset;
2861 extent_bytes = entry->bytes;
2862 start = max(start, extent_start);
2863 bytes = min(extent_start + extent_bytes, end) - start;
2864 if (bytes < minlen) {
2865 spin_unlock(&ctl->tree_lock);
2866 goto next;
2867 }
2868
2869 unlink_free_space(ctl, entry);
2870 kmem_cache_free(btrfs_free_space_cachep, entry);
2871
Li Zefan34d52cb2011-03-29 13:46:06 +08002872 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002873
Li Zefan7fe1e642011-12-29 14:47:27 +08002874 ret = do_trimming(block_group, total_trimmed, start, bytes,
2875 extent_start, extent_bytes);
2876 if (ret)
2877 break;
2878next:
Li Dongyangf7039b12011-03-24 10:24:28 +00002879 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00002880
2881 if (fatal_signal_pending(current)) {
2882 ret = -ERESTARTSYS;
2883 break;
2884 }
2885
2886 cond_resched();
2887 }
Li Zefan7fe1e642011-12-29 14:47:27 +08002888out:
2889 return ret;
2890}
2891
2892static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
2893 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
2894{
2895 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2896 struct btrfs_free_space *entry;
2897 int ret = 0;
2898 int ret2;
2899 u64 bytes;
2900 u64 offset = offset_to_bitmap(ctl, start);
2901
2902 while (offset < end) {
2903 bool next_bitmap = false;
2904
2905 spin_lock(&ctl->tree_lock);
2906
2907 if (ctl->free_space < minlen) {
2908 spin_unlock(&ctl->tree_lock);
2909 break;
2910 }
2911
2912 entry = tree_search_offset(ctl, offset, 1, 0);
2913 if (!entry) {
2914 spin_unlock(&ctl->tree_lock);
2915 next_bitmap = true;
2916 goto next;
2917 }
2918
2919 bytes = minlen;
2920 ret2 = search_bitmap(ctl, entry, &start, &bytes);
2921 if (ret2 || start >= end) {
2922 spin_unlock(&ctl->tree_lock);
2923 next_bitmap = true;
2924 goto next;
2925 }
2926
2927 bytes = min(bytes, end - start);
2928 if (bytes < minlen) {
2929 spin_unlock(&ctl->tree_lock);
2930 goto next;
2931 }
2932
2933 bitmap_clear_bits(ctl, entry, start, bytes);
2934 if (entry->bytes == 0)
2935 free_bitmap(ctl, entry);
2936
2937 spin_unlock(&ctl->tree_lock);
2938
2939 ret = do_trimming(block_group, total_trimmed, start, bytes,
2940 start, bytes);
2941 if (ret)
2942 break;
2943next:
2944 if (next_bitmap) {
2945 offset += BITS_PER_BITMAP * ctl->unit;
2946 } else {
2947 start += bytes;
2948 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
2949 offset += BITS_PER_BITMAP * ctl->unit;
2950 }
2951
2952 if (fatal_signal_pending(current)) {
2953 ret = -ERESTARTSYS;
2954 break;
2955 }
2956
2957 cond_resched();
2958 }
2959
2960 return ret;
2961}
2962
2963int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2964 u64 *trimmed, u64 start, u64 end, u64 minlen)
2965{
2966 int ret;
2967
2968 *trimmed = 0;
2969
2970 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
2971 if (ret)
2972 return ret;
2973
2974 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
Li Dongyangf7039b12011-03-24 10:24:28 +00002975
2976 return ret;
2977}
Li Zefan581bb052011-04-20 10:06:11 +08002978
2979/*
2980 * Find the left-most item in the cache tree, and then return the
2981 * smallest inode number in the item.
2982 *
2983 * Note: the returned inode number may not be the smallest one in
2984 * the tree, if the left-most item is a bitmap.
2985 */
2986u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2987{
2988 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2989 struct btrfs_free_space *entry = NULL;
2990 u64 ino = 0;
2991
2992 spin_lock(&ctl->tree_lock);
2993
2994 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2995 goto out;
2996
2997 entry = rb_entry(rb_first(&ctl->free_space_offset),
2998 struct btrfs_free_space, offset_index);
2999
3000 if (!entry->bitmap) {
3001 ino = entry->offset;
3002
3003 unlink_free_space(ctl, entry);
3004 entry->offset++;
3005 entry->bytes--;
3006 if (!entry->bytes)
3007 kmem_cache_free(btrfs_free_space_cachep, entry);
3008 else
3009 link_free_space(ctl, entry);
3010 } else {
3011 u64 offset = 0;
3012 u64 count = 1;
3013 int ret;
3014
3015 ret = search_bitmap(ctl, entry, &offset, &count);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003016 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003017 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003018
3019 ino = offset;
3020 bitmap_clear_bits(ctl, entry, offset, 1);
3021 if (entry->bytes == 0)
3022 free_bitmap(ctl, entry);
3023 }
3024out:
3025 spin_unlock(&ctl->tree_lock);
3026
3027 return ino;
3028}
Li Zefan82d59022011-04-20 10:33:24 +08003029
3030struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3031 struct btrfs_path *path)
3032{
3033 struct inode *inode = NULL;
3034
David Sterba57cdc8d2014-02-05 02:37:48 +01003035 spin_lock(&root->ino_cache_lock);
3036 if (root->ino_cache_inode)
3037 inode = igrab(root->ino_cache_inode);
3038 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003039 if (inode)
3040 return inode;
3041
3042 inode = __lookup_free_space_inode(root, path, 0);
3043 if (IS_ERR(inode))
3044 return inode;
3045
David Sterba57cdc8d2014-02-05 02:37:48 +01003046 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003047 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003048 root->ino_cache_inode = igrab(inode);
3049 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003050
3051 return inode;
3052}
3053
3054int create_free_ino_inode(struct btrfs_root *root,
3055 struct btrfs_trans_handle *trans,
3056 struct btrfs_path *path)
3057{
3058 return __create_free_space_inode(root, trans, path,
3059 BTRFS_FREE_INO_OBJECTID, 0);
3060}
3061
3062int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3063{
3064 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3065 struct btrfs_path *path;
3066 struct inode *inode;
3067 int ret = 0;
3068 u64 root_gen = btrfs_root_generation(&root->root_item);
3069
Chris Mason4b9465c2011-06-03 09:36:29 -04003070 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3071 return 0;
3072
Li Zefan82d59022011-04-20 10:33:24 +08003073 /*
3074 * If we're unmounting then just return, since this does a search on the
3075 * normal root and not the commit root and we could deadlock.
3076 */
David Sterba7841cb22011-05-31 18:07:27 +02003077 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003078 return 0;
3079
3080 path = btrfs_alloc_path();
3081 if (!path)
3082 return 0;
3083
3084 inode = lookup_free_ino_inode(root, path);
3085 if (IS_ERR(inode))
3086 goto out;
3087
3088 if (root_gen != BTRFS_I(inode)->generation)
3089 goto out_put;
3090
3091 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3092
3093 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003094 btrfs_err(fs_info,
3095 "failed to load free ino cache for root %llu",
3096 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003097out_put:
3098 iput(inode);
3099out:
3100 btrfs_free_path(path);
3101 return ret;
3102}
3103
3104int btrfs_write_out_ino_cache(struct btrfs_root *root,
3105 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003106 struct btrfs_path *path,
3107 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003108{
3109 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003110 int ret;
3111
Chris Mason4b9465c2011-06-03 09:36:29 -04003112 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3113 return 0;
3114
Li Zefan82d59022011-04-20 10:33:24 +08003115 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04003116 if (ret) {
3117 btrfs_delalloc_release_metadata(inode, inode->i_size);
3118#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003119 btrfs_err(root->fs_info,
3120 "failed to write free ino cache for root %llu",
3121 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003122#endif
3123 }
Li Zefan82d59022011-04-20 10:33:24 +08003124
Li Zefan82d59022011-04-20 10:33:24 +08003125 return ret;
3126}
Josef Bacik74255aa2013-03-15 09:47:08 -04003127
3128#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003129/*
3130 * Use this if you need to make a bitmap or extent entry specifically, it
3131 * doesn't do any of the merging that add_free_space does, this acts a lot like
3132 * how the free space cache loading stuff works, so you can get really weird
3133 * configurations.
3134 */
3135int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3136 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003137{
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003138 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3139 struct btrfs_free_space *info = NULL, *bitmap_info;
3140 void *map = NULL;
3141 u64 bytes_added;
3142 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003143
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003144again:
3145 if (!info) {
3146 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3147 if (!info)
3148 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003149 }
3150
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003151 if (!bitmap) {
3152 spin_lock(&ctl->tree_lock);
3153 info->offset = offset;
3154 info->bytes = bytes;
3155 ret = link_free_space(ctl, info);
3156 spin_unlock(&ctl->tree_lock);
3157 if (ret)
3158 kmem_cache_free(btrfs_free_space_cachep, info);
3159 return ret;
3160 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003161
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003162 if (!map) {
3163 map = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
3164 if (!map) {
3165 kmem_cache_free(btrfs_free_space_cachep, info);
3166 return -ENOMEM;
3167 }
3168 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003169
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003170 spin_lock(&ctl->tree_lock);
3171 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3172 1, 0);
3173 if (!bitmap_info) {
3174 info->bitmap = map;
3175 map = NULL;
3176 add_new_bitmap(ctl, info, offset);
3177 bitmap_info = info;
3178 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003179
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003180 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3181 bytes -= bytes_added;
3182 offset += bytes_added;
3183 spin_unlock(&ctl->tree_lock);
3184
3185 if (bytes)
3186 goto again;
3187
3188 if (map)
3189 kfree(map);
3190 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003191}
3192
3193/*
3194 * Checks to see if the given range is in the free space cache. This is really
3195 * just used to check the absence of space, so if there is free space in the
3196 * range at all we will return 1.
3197 */
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003198int test_check_exists(struct btrfs_block_group_cache *cache,
3199 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003200{
3201 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3202 struct btrfs_free_space *info;
3203 int ret = 0;
3204
3205 spin_lock(&ctl->tree_lock);
3206 info = tree_search_offset(ctl, offset, 0, 0);
3207 if (!info) {
3208 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3209 1, 0);
3210 if (!info)
3211 goto out;
3212 }
3213
3214have_info:
3215 if (info->bitmap) {
3216 u64 bit_off, bit_bytes;
3217 struct rb_node *n;
3218 struct btrfs_free_space *tmp;
3219
3220 bit_off = offset;
3221 bit_bytes = ctl->unit;
3222 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes);
3223 if (!ret) {
3224 if (bit_off == offset) {
3225 ret = 1;
3226 goto out;
3227 } else if (bit_off > offset &&
3228 offset + bytes > bit_off) {
3229 ret = 1;
3230 goto out;
3231 }
3232 }
3233
3234 n = rb_prev(&info->offset_index);
3235 while (n) {
3236 tmp = rb_entry(n, struct btrfs_free_space,
3237 offset_index);
3238 if (tmp->offset + tmp->bytes < offset)
3239 break;
3240 if (offset + bytes < tmp->offset) {
3241 n = rb_prev(&info->offset_index);
3242 continue;
3243 }
3244 info = tmp;
3245 goto have_info;
3246 }
3247
3248 n = rb_next(&info->offset_index);
3249 while (n) {
3250 tmp = rb_entry(n, struct btrfs_free_space,
3251 offset_index);
3252 if (offset + bytes < tmp->offset)
3253 break;
3254 if (tmp->offset + tmp->bytes < offset) {
3255 n = rb_next(&info->offset_index);
3256 continue;
3257 }
3258 info = tmp;
3259 goto have_info;
3260 }
3261
3262 goto out;
3263 }
3264
3265 if (info->offset == offset) {
3266 ret = 1;
3267 goto out;
3268 }
3269
3270 if (offset > info->offset && offset < info->offset + info->bytes)
3271 ret = 1;
3272out:
3273 spin_unlock(&ctl->tree_lock);
3274 return ret;
3275}
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003276#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */