blob: cb79b8975c9f114cf73057f32570f82a933bf56c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. 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
Li Zefan581bb052011-04-20 10:06:11 +080019#include <linux/delay.h>
20#include <linux/kthread.h>
21#include <linux/pagemap.h>
22
Chris Mason9f5fae22007-03-20 14:38:32 -040023#include "ctree.h"
24#include "disk-io.h"
Li Zefan581bb052011-04-20 10:06:11 +080025#include "free-space-cache.h"
26#include "inode-map.h"
Chris Mason9f5fae22007-03-20 14:38:32 -040027#include "transaction.h"
28
Li Zefan581bb052011-04-20 10:06:11 +080029static int caching_kthread(void *data)
30{
31 struct btrfs_root *root = data;
32 struct btrfs_fs_info *fs_info = root->fs_info;
33 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
34 struct btrfs_key key;
35 struct btrfs_path *path;
36 struct extent_buffer *leaf;
37 u64 last = (u64)-1;
38 int slot;
39 int ret;
40
Chris Mason4b9465c2011-06-03 09:36:29 -040041 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
42 return 0;
43
Li Zefan581bb052011-04-20 10:06:11 +080044 path = btrfs_alloc_path();
45 if (!path)
46 return -ENOMEM;
47
48 /* Since the commit root is read-only, we can safely skip locking. */
49 path->skip_locking = 1;
50 path->search_commit_root = 1;
51 path->reada = 2;
52
53 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
54 key.offset = 0;
55 key.type = BTRFS_INODE_ITEM_KEY;
56again:
57 /* need to make sure the commit_root doesn't disappear */
58 mutex_lock(&root->fs_commit_mutex);
59
60 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
61 if (ret < 0)
62 goto out;
63
64 while (1) {
65 smp_mb();
Li Zefana47d6b72011-05-26 06:38:30 +000066 if (fs_info->closing)
Li Zefan581bb052011-04-20 10:06:11 +080067 goto out;
68
69 leaf = path->nodes[0];
70 slot = path->slots[0];
Li Zefana47d6b72011-05-26 06:38:30 +000071 if (slot >= btrfs_header_nritems(leaf)) {
Li Zefan581bb052011-04-20 10:06:11 +080072 ret = btrfs_next_leaf(root, path);
73 if (ret < 0)
74 goto out;
75 else if (ret > 0)
76 break;
77
78 if (need_resched() ||
79 btrfs_transaction_in_commit(fs_info)) {
80 leaf = path->nodes[0];
81
82 if (btrfs_header_nritems(leaf) == 0) {
83 WARN_ON(1);
84 break;
85 }
86
87 /*
88 * Save the key so we can advances forward
89 * in the next search.
90 */
91 btrfs_item_key_to_cpu(leaf, &key, 0);
Chris Mason945d8962011-05-22 12:33:42 -040092 btrfs_release_path(path);
Li Zefan581bb052011-04-20 10:06:11 +080093 root->cache_progress = last;
94 mutex_unlock(&root->fs_commit_mutex);
95 schedule_timeout(1);
96 goto again;
97 } else
98 continue;
99 }
100
101 btrfs_item_key_to_cpu(leaf, &key, slot);
102
103 if (key.type != BTRFS_INODE_ITEM_KEY)
104 goto next;
105
Li Zefana47d6b72011-05-26 06:38:30 +0000106 if (key.objectid >= root->highest_objectid)
Li Zefan581bb052011-04-20 10:06:11 +0800107 break;
108
109 if (last != (u64)-1 && last + 1 != key.objectid) {
110 __btrfs_add_free_space(ctl, last + 1,
111 key.objectid - last - 1);
112 wake_up(&root->cache_wait);
113 }
114
115 last = key.objectid;
116next:
117 path->slots[0]++;
118 }
119
Li Zefana47d6b72011-05-26 06:38:30 +0000120 if (last < root->highest_objectid - 1) {
Li Zefan581bb052011-04-20 10:06:11 +0800121 __btrfs_add_free_space(ctl, last + 1,
Li Zefana47d6b72011-05-26 06:38:30 +0000122 root->highest_objectid - last - 1);
Li Zefan581bb052011-04-20 10:06:11 +0800123 }
124
125 spin_lock(&root->cache_lock);
126 root->cached = BTRFS_CACHE_FINISHED;
127 spin_unlock(&root->cache_lock);
128
129 root->cache_progress = (u64)-1;
130 btrfs_unpin_free_ino(root);
131out:
132 wake_up(&root->cache_wait);
133 mutex_unlock(&root->fs_commit_mutex);
134
135 btrfs_free_path(path);
136
137 return ret;
138}
139
140static void start_caching(struct btrfs_root *root)
141{
Li Zefana47d6b72011-05-26 06:38:30 +0000142 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan581bb052011-04-20 10:06:11 +0800143 struct task_struct *tsk;
Li Zefan82d59022011-04-20 10:33:24 +0800144 int ret;
Li Zefana47d6b72011-05-26 06:38:30 +0000145 u64 objectid;
Li Zefan581bb052011-04-20 10:06:11 +0800146
Chris Mason4b9465c2011-06-03 09:36:29 -0400147 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
148 return;
149
Li Zefan581bb052011-04-20 10:06:11 +0800150 spin_lock(&root->cache_lock);
151 if (root->cached != BTRFS_CACHE_NO) {
152 spin_unlock(&root->cache_lock);
153 return;
154 }
155
156 root->cached = BTRFS_CACHE_STARTED;
157 spin_unlock(&root->cache_lock);
158
Li Zefan82d59022011-04-20 10:33:24 +0800159 ret = load_free_ino_cache(root->fs_info, root);
160 if (ret == 1) {
161 spin_lock(&root->cache_lock);
162 root->cached = BTRFS_CACHE_FINISHED;
163 spin_unlock(&root->cache_lock);
164 return;
165 }
166
Li Zefana47d6b72011-05-26 06:38:30 +0000167 /*
168 * It can be quite time-consuming to fill the cache by searching
169 * through the extent tree, and this can keep ino allocation path
170 * waiting. Therefore at start we quickly find out the highest
171 * inode number and we know we can use inode numbers which fall in
172 * [highest_ino + 1, BTRFS_LAST_FREE_OBJECTID].
173 */
174 ret = btrfs_find_free_objectid(root, &objectid);
175 if (!ret && objectid <= BTRFS_LAST_FREE_OBJECTID) {
176 __btrfs_add_free_space(ctl, objectid,
177 BTRFS_LAST_FREE_OBJECTID - objectid + 1);
178 }
179
Li Zefan581bb052011-04-20 10:06:11 +0800180 tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu\n",
181 root->root_key.objectid);
182 BUG_ON(IS_ERR(tsk));
183}
184
185int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid)
186{
Chris Mason4b9465c2011-06-03 09:36:29 -0400187 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
188 return btrfs_find_free_objectid(root, objectid);
189
Li Zefan581bb052011-04-20 10:06:11 +0800190again:
191 *objectid = btrfs_find_ino_for_alloc(root);
192
193 if (*objectid != 0)
194 return 0;
195
196 start_caching(root);
197
198 wait_event(root->cache_wait,
199 root->cached == BTRFS_CACHE_FINISHED ||
200 root->free_ino_ctl->free_space > 0);
201
202 if (root->cached == BTRFS_CACHE_FINISHED &&
203 root->free_ino_ctl->free_space == 0)
204 return -ENOSPC;
205 else
206 goto again;
207}
208
209void btrfs_return_ino(struct btrfs_root *root, u64 objectid)
210{
211 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
212 struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
Chris Mason4b9465c2011-06-03 09:36:29 -0400213
214 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
215 return;
216
Li Zefan581bb052011-04-20 10:06:11 +0800217again:
218 if (root->cached == BTRFS_CACHE_FINISHED) {
219 __btrfs_add_free_space(ctl, objectid, 1);
220 } else {
221 /*
222 * If we are in the process of caching free ino chunks,
223 * to avoid adding the same inode number to the free_ino
224 * tree twice due to cross transaction, we'll leave it
225 * in the pinned tree until a transaction is committed
226 * or the caching work is done.
227 */
228
229 mutex_lock(&root->fs_commit_mutex);
230 spin_lock(&root->cache_lock);
231 if (root->cached == BTRFS_CACHE_FINISHED) {
232 spin_unlock(&root->cache_lock);
233 mutex_unlock(&root->fs_commit_mutex);
234 goto again;
235 }
236 spin_unlock(&root->cache_lock);
237
238 start_caching(root);
239
Li Zefana47d6b72011-05-26 06:38:30 +0000240 if (objectid <= root->cache_progress ||
241 objectid > root->highest_objectid)
Li Zefan581bb052011-04-20 10:06:11 +0800242 __btrfs_add_free_space(ctl, objectid, 1);
243 else
244 __btrfs_add_free_space(pinned, objectid, 1);
245
246 mutex_unlock(&root->fs_commit_mutex);
247 }
248}
249
250/*
251 * When a transaction is committed, we'll move those inode numbers which
252 * are smaller than root->cache_progress from pinned tree to free_ino tree,
253 * and others will just be dropped, because the commit root we were
254 * searching has changed.
255 *
256 * Must be called with root->fs_commit_mutex held
257 */
258void btrfs_unpin_free_ino(struct btrfs_root *root)
259{
260 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
261 struct rb_root *rbroot = &root->free_ino_pinned->free_space_offset;
262 struct btrfs_free_space *info;
263 struct rb_node *n;
264 u64 count;
265
Chris Mason4b9465c2011-06-03 09:36:29 -0400266 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
267 return;
268
Li Zefan581bb052011-04-20 10:06:11 +0800269 while (1) {
270 n = rb_first(rbroot);
271 if (!n)
272 break;
273
274 info = rb_entry(n, struct btrfs_free_space, offset_index);
275 BUG_ON(info->bitmap);
276
277 if (info->offset > root->cache_progress)
278 goto free;
279 else if (info->offset + info->bytes > root->cache_progress)
280 count = root->cache_progress - info->offset + 1;
281 else
282 count = info->bytes;
283
284 __btrfs_add_free_space(ctl, info->offset, count);
285free:
286 rb_erase(&info->offset_index, rbroot);
287 kfree(info);
288 }
289}
290
291#define INIT_THRESHOLD (((1024 * 32) / 2) / sizeof(struct btrfs_free_space))
292#define INODES_PER_BITMAP (PAGE_CACHE_SIZE * 8)
293
294/*
295 * The goal is to keep the memory used by the free_ino tree won't
296 * exceed the memory if we use bitmaps only.
297 */
298static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
299{
300 struct btrfs_free_space *info;
301 struct rb_node *n;
302 int max_ino;
303 int max_bitmaps;
304
305 n = rb_last(&ctl->free_space_offset);
306 if (!n) {
307 ctl->extents_thresh = INIT_THRESHOLD;
308 return;
309 }
310 info = rb_entry(n, struct btrfs_free_space, offset_index);
311
312 /*
313 * Find the maximum inode number in the filesystem. Note we
314 * ignore the fact that this can be a bitmap, because we are
315 * not doing precise calculation.
316 */
317 max_ino = info->bytes - 1;
318
319 max_bitmaps = ALIGN(max_ino, INODES_PER_BITMAP) / INODES_PER_BITMAP;
320 if (max_bitmaps <= ctl->total_bitmaps) {
321 ctl->extents_thresh = 0;
322 return;
323 }
324
325 ctl->extents_thresh = (max_bitmaps - ctl->total_bitmaps) *
326 PAGE_CACHE_SIZE / sizeof(*info);
327}
328
329/*
330 * We don't fall back to bitmap, if we are below the extents threshold
331 * or this chunk of inode numbers is a big one.
332 */
333static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
334 struct btrfs_free_space *info)
335{
336 if (ctl->free_extents < ctl->extents_thresh ||
337 info->bytes > INODES_PER_BITMAP / 10)
338 return false;
339
340 return true;
341}
342
343static struct btrfs_free_space_op free_ino_op = {
344 .recalc_thresholds = recalculate_thresholds,
345 .use_bitmap = use_bitmap,
346};
347
348static void pinned_recalc_thresholds(struct btrfs_free_space_ctl *ctl)
349{
350}
351
352static bool pinned_use_bitmap(struct btrfs_free_space_ctl *ctl,
353 struct btrfs_free_space *info)
354{
355 /*
356 * We always use extents for two reasons:
357 *
358 * - The pinned tree is only used during the process of caching
359 * work.
360 * - Make code simpler. See btrfs_unpin_free_ino().
361 */
362 return false;
363}
364
365static struct btrfs_free_space_op pinned_free_ino_op = {
366 .recalc_thresholds = pinned_recalc_thresholds,
367 .use_bitmap = pinned_use_bitmap,
368};
369
370void btrfs_init_free_ino_ctl(struct btrfs_root *root)
371{
372 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
373 struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
374
375 spin_lock_init(&ctl->tree_lock);
376 ctl->unit = 1;
377 ctl->start = 0;
378 ctl->private = NULL;
379 ctl->op = &free_ino_op;
380
381 /*
382 * Initially we allow to use 16K of ram to cache chunks of
383 * inode numbers before we resort to bitmaps. This is somewhat
384 * arbitrary, but it will be adjusted in runtime.
385 */
386 ctl->extents_thresh = INIT_THRESHOLD;
387
388 spin_lock_init(&pinned->tree_lock);
389 pinned->unit = 1;
390 pinned->start = 0;
391 pinned->private = NULL;
392 pinned->extents_thresh = 0;
393 pinned->op = &pinned_free_ino_op;
394}
395
Li Zefan82d59022011-04-20 10:33:24 +0800396int btrfs_save_ino_cache(struct btrfs_root *root,
397 struct btrfs_trans_handle *trans)
398{
399 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
400 struct btrfs_path *path;
401 struct inode *inode;
402 u64 alloc_hint = 0;
403 int ret;
404 int prealloc;
405 bool retry = false;
406
liuboca456ae2011-06-01 09:42:49 +0000407 /* only fs tree and subvol/snap needs ino cache */
408 if (root->root_key.objectid != BTRFS_FS_TREE_OBJECTID &&
409 (root->root_key.objectid < BTRFS_FIRST_FREE_OBJECTID ||
410 root->root_key.objectid > BTRFS_LAST_FREE_OBJECTID))
411 return 0;
412
Josef Bacikd132a532011-05-31 19:33:33 +0000413 /* Don't save inode cache if we are deleting this root */
414 if (btrfs_root_refs(&root->root_item) == 0 &&
415 root != root->fs_info->tree_root)
416 return 0;
417
Chris Mason4b9465c2011-06-03 09:36:29 -0400418 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
419 return 0;
420
Li Zefan82d59022011-04-20 10:33:24 +0800421 path = btrfs_alloc_path();
422 if (!path)
423 return -ENOMEM;
Chris Mason4b9465c2011-06-03 09:36:29 -0400424
Li Zefan82d59022011-04-20 10:33:24 +0800425again:
426 inode = lookup_free_ino_inode(root, path);
427 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
428 ret = PTR_ERR(inode);
429 goto out;
430 }
431
432 if (IS_ERR(inode)) {
433 BUG_ON(retry);
434 retry = true;
435
436 ret = create_free_ino_inode(root, trans, path);
437 if (ret)
438 goto out;
439 goto again;
440 }
441
442 BTRFS_I(inode)->generation = 0;
443 ret = btrfs_update_inode(trans, root, inode);
444 WARN_ON(ret);
445
446 if (i_size_read(inode) > 0) {
447 ret = btrfs_truncate_free_space_cache(root, trans, path, inode);
448 if (ret)
449 goto out_put;
450 }
451
452 spin_lock(&root->cache_lock);
453 if (root->cached != BTRFS_CACHE_FINISHED) {
454 ret = -1;
455 spin_unlock(&root->cache_lock);
456 goto out_put;
457 }
458 spin_unlock(&root->cache_lock);
459
460 spin_lock(&ctl->tree_lock);
461 prealloc = sizeof(struct btrfs_free_space) * ctl->free_extents;
462 prealloc = ALIGN(prealloc, PAGE_CACHE_SIZE);
463 prealloc += ctl->total_bitmaps * PAGE_CACHE_SIZE;
464 spin_unlock(&ctl->tree_lock);
465
466 /* Just to make sure we have enough space */
467 prealloc += 8 * PAGE_CACHE_SIZE;
468
469 ret = btrfs_check_data_free_space(inode, prealloc);
470 if (ret)
471 goto out_put;
472
473 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
474 prealloc, prealloc, &alloc_hint);
475 if (ret)
476 goto out_put;
477 btrfs_free_reserved_data_space(inode, prealloc);
478
479out_put:
480 iput(inode);
481out:
482 if (ret == 0)
483 ret = btrfs_write_out_ino_cache(root, trans, path);
484
485 btrfs_free_path(path);
486 return ret;
487}
488
Li Zefan581bb052011-04-20 10:06:11 +0800489static int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
Chris Mason5be6f7f2007-04-05 13:35:25 -0400490{
491 struct btrfs_path *path;
492 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400493 struct extent_buffer *l;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400494 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400495 struct btrfs_key found_key;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400496 int slot;
497
498 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000499 if (!path)
500 return -ENOMEM;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400501
Zheng Yan6527cdb2008-09-05 16:43:53 -0400502 search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
503 search_key.type = -1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400504 search_key.offset = (u64)-1;
505 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
506 if (ret < 0)
507 goto error;
508 BUG_ON(ret == 0);
509 if (path->slots[0] > 0) {
510 slot = path->slots[0] - 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400511 l = path->nodes[0];
512 btrfs_item_key_to_cpu(l, &found_key, slot);
Yan, Zheng13a8a7c2009-09-21 15:56:00 -0400513 *objectid = max_t(u64, found_key.objectid,
514 BTRFS_FIRST_FREE_OBJECTID - 1);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400515 } else {
Yan, Zheng13a8a7c2009-09-21 15:56:00 -0400516 *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400517 }
518 ret = 0;
519error:
520 btrfs_free_path(path);
521 return ret;
522}
523
Li Zefan581bb052011-04-20 10:06:11 +0800524int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
Chris Mason9f5fae22007-03-20 14:38:32 -0400525{
Chris Mason9f5fae22007-03-20 14:38:32 -0400526 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400527 mutex_lock(&root->objectid_mutex);
Yan, Zheng13a8a7c2009-09-21 15:56:00 -0400528
529 if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
Li Zefan581bb052011-04-20 10:06:11 +0800530 ret = btrfs_find_highest_objectid(root,
531 &root->highest_objectid);
Yan, Zheng13a8a7c2009-09-21 15:56:00 -0400532 if (ret)
533 goto out;
Chris Masona2135012008-06-25 16:01:30 -0400534 }
Chris Masonb1a4d962007-04-04 15:27:52 -0400535
Yan, Zheng13a8a7c2009-09-21 15:56:00 -0400536 if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
537 ret = -ENOSPC;
538 goto out;
Chris Mason9f5fae22007-03-20 14:38:32 -0400539 }
Yan, Zheng13a8a7c2009-09-21 15:56:00 -0400540
541 *objectid = ++root->highest_objectid;
542 ret = 0;
543out:
Chris Masona2135012008-06-25 16:01:30 -0400544 mutex_unlock(&root->objectid_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400545 return ret;
546}