]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/btrfs/volumes.c
btrfs: fix early abort in 'remount'
[mirror_ubuntu-zesty-kernel.git] / fs / btrfs / volumes.c
CommitLineData
0b86a832
CM
1/*
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#include <linux/sched.h>
19#include <linux/bio.h>
5a0e3ad6 20#include <linux/slab.h>
8a4b83cc 21#include <linux/buffer_head.h>
f2d8d74d 22#include <linux/blkdev.h>
788f20eb 23#include <linux/random.h>
b765ead5 24#include <linux/iocontext.h>
6f88a440 25#include <linux/capability.h>
59641015 26#include <linux/kthread.h>
593060d7 27#include <asm/div64.h>
4b4e25f2 28#include "compat.h"
0b86a832
CM
29#include "ctree.h"
30#include "extent_map.h"
31#include "disk-io.h"
32#include "transaction.h"
33#include "print-tree.h"
34#include "volumes.h"
8b712842 35#include "async-thread.h"
21adbd5c 36#include "check-integrity.h"
0b86a832 37
2b82032c
YZ
38static int init_first_rw_device(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 struct btrfs_device *device);
41static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
42
8a4b83cc
CM
43static DEFINE_MUTEX(uuid_mutex);
44static LIST_HEAD(fs_uuids);
45
7d9eb12c
CM
46static void lock_chunks(struct btrfs_root *root)
47{
7d9eb12c
CM
48 mutex_lock(&root->fs_info->chunk_mutex);
49}
50
51static void unlock_chunks(struct btrfs_root *root)
52{
7d9eb12c
CM
53 mutex_unlock(&root->fs_info->chunk_mutex);
54}
55
e4404d6e
YZ
56static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
57{
58 struct btrfs_device *device;
59 WARN_ON(fs_devices->opened);
60 while (!list_empty(&fs_devices->devices)) {
61 device = list_entry(fs_devices->devices.next,
62 struct btrfs_device, dev_list);
63 list_del(&device->dev_list);
64 kfree(device->name);
65 kfree(device);
66 }
67 kfree(fs_devices);
68}
69
143bede5 70void btrfs_cleanup_fs_uuids(void)
8a4b83cc
CM
71{
72 struct btrfs_fs_devices *fs_devices;
8a4b83cc 73
2b82032c
YZ
74 while (!list_empty(&fs_uuids)) {
75 fs_devices = list_entry(fs_uuids.next,
76 struct btrfs_fs_devices, list);
77 list_del(&fs_devices->list);
e4404d6e 78 free_fs_devices(fs_devices);
8a4b83cc 79 }
8a4b83cc
CM
80}
81
a1b32a59
CM
82static noinline struct btrfs_device *__find_device(struct list_head *head,
83 u64 devid, u8 *uuid)
8a4b83cc
CM
84{
85 struct btrfs_device *dev;
8a4b83cc 86
c6e30871 87 list_for_each_entry(dev, head, dev_list) {
a443755f 88 if (dev->devid == devid &&
8f18cf13 89 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
8a4b83cc 90 return dev;
a443755f 91 }
8a4b83cc
CM
92 }
93 return NULL;
94}
95
a1b32a59 96static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
8a4b83cc 97{
8a4b83cc
CM
98 struct btrfs_fs_devices *fs_devices;
99
c6e30871 100 list_for_each_entry(fs_devices, &fs_uuids, list) {
8a4b83cc
CM
101 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
102 return fs_devices;
103 }
104 return NULL;
105}
106
ffbd517d
CM
107static void requeue_list(struct btrfs_pending_bios *pending_bios,
108 struct bio *head, struct bio *tail)
109{
110
111 struct bio *old_head;
112
113 old_head = pending_bios->head;
114 pending_bios->head = head;
115 if (pending_bios->tail)
116 tail->bi_next = old_head;
117 else
118 pending_bios->tail = tail;
119}
120
8b712842
CM
121/*
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
125 *
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
131 */
143bede5 132static noinline void run_scheduled_bios(struct btrfs_device *device)
8b712842
CM
133{
134 struct bio *pending;
135 struct backing_dev_info *bdi;
b64a2851 136 struct btrfs_fs_info *fs_info;
ffbd517d 137 struct btrfs_pending_bios *pending_bios;
8b712842
CM
138 struct bio *tail;
139 struct bio *cur;
140 int again = 0;
ffbd517d 141 unsigned long num_run;
d644d8a1 142 unsigned long batch_run = 0;
b64a2851 143 unsigned long limit;
b765ead5 144 unsigned long last_waited = 0;
d84275c9 145 int force_reg = 0;
0e588859 146 int sync_pending = 0;
211588ad
CM
147 struct blk_plug plug;
148
149 /*
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
154 */
155 blk_start_plug(&plug);
8b712842 156
bedf762b 157 bdi = blk_get_backing_dev_info(device->bdev);
b64a2851
CM
158 fs_info = device->dev_root->fs_info;
159 limit = btrfs_async_submit_limit(fs_info);
160 limit = limit * 2 / 3;
161
8b712842
CM
162loop:
163 spin_lock(&device->io_lock);
164
a6837051 165loop_lock:
d84275c9 166 num_run = 0;
ffbd517d 167
8b712842
CM
168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
172 */
d84275c9 173 if (!force_reg && device->pending_sync_bios.head) {
ffbd517d 174 pending_bios = &device->pending_sync_bios;
d84275c9
CM
175 force_reg = 1;
176 } else {
ffbd517d 177 pending_bios = &device->pending_bios;
d84275c9
CM
178 force_reg = 0;
179 }
ffbd517d
CM
180
181 pending = pending_bios->head;
182 tail = pending_bios->tail;
8b712842 183 WARN_ON(pending && !tail);
8b712842
CM
184
185 /*
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
189 *
190 * device->running_pending is used to synchronize with the
191 * schedule_bio code.
192 */
ffbd517d
CM
193 if (device->pending_sync_bios.head == NULL &&
194 device->pending_bios.head == NULL) {
8b712842
CM
195 again = 0;
196 device->running_pending = 0;
ffbd517d
CM
197 } else {
198 again = 1;
199 device->running_pending = 1;
8b712842 200 }
ffbd517d
CM
201
202 pending_bios->head = NULL;
203 pending_bios->tail = NULL;
204
8b712842
CM
205 spin_unlock(&device->io_lock);
206
d397712b 207 while (pending) {
ffbd517d
CM
208
209 rmb();
d84275c9
CM
210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
212 */
213 if ((num_run > 32 &&
214 pending_bios != &device->pending_sync_bios &&
215 device->pending_sync_bios.head) ||
216 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
217 device->pending_bios.head)) {
ffbd517d
CM
218 spin_lock(&device->io_lock);
219 requeue_list(pending_bios, pending, tail);
220 goto loop_lock;
221 }
222
8b712842
CM
223 cur = pending;
224 pending = pending->bi_next;
225 cur->bi_next = NULL;
b64a2851
CM
226 atomic_dec(&fs_info->nr_async_bios);
227
228 if (atomic_read(&fs_info->nr_async_bios) < limit &&
229 waitqueue_active(&fs_info->async_submit_wait))
230 wake_up(&fs_info->async_submit_wait);
492bb6de
CM
231
232 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
d644d8a1 233
2ab1ba68
CM
234 /*
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
237 *
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
240 * we add more
241 */
242 if (pending_bios == &device->pending_sync_bios) {
243 sync_pending = 1;
244 } else if (sync_pending) {
245 blk_finish_plug(&plug);
246 blk_start_plug(&plug);
247 sync_pending = 0;
248 }
249
21adbd5c 250 btrfsic_submit_bio(cur->bi_rw, cur);
5ff7ba3a
CM
251 num_run++;
252 batch_run++;
7eaceacc 253 if (need_resched())
ffbd517d 254 cond_resched();
8b712842
CM
255
256 /*
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
259 * run instead
260 */
57fd5a5f 261 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
5f2cc086 262 fs_info->fs_devices->open_devices > 1) {
b765ead5 263 struct io_context *ioc;
8b712842 264
b765ead5
CM
265 ioc = current->io_context;
266
267 /*
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
271 *
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
275 */
276 if (ioc && ioc->nr_batch_requests > 0 &&
277 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 (last_waited == 0 ||
279 ioc->last_waited == last_waited)) {
280 /*
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
285 */
286 last_waited = ioc->last_waited;
7eaceacc 287 if (need_resched())
ffbd517d 288 cond_resched();
b765ead5
CM
289 continue;
290 }
8b712842 291 spin_lock(&device->io_lock);
ffbd517d 292 requeue_list(pending_bios, pending, tail);
a6837051 293 device->running_pending = 1;
8b712842
CM
294
295 spin_unlock(&device->io_lock);
296 btrfs_requeue_work(&device->work);
297 goto done;
298 }
d85c8a6f
CM
299 /* unplug every 64 requests just for good measure */
300 if (batch_run % 64 == 0) {
301 blk_finish_plug(&plug);
302 blk_start_plug(&plug);
303 sync_pending = 0;
304 }
8b712842 305 }
ffbd517d 306
51684082
CM
307 cond_resched();
308 if (again)
309 goto loop;
310
311 spin_lock(&device->io_lock);
312 if (device->pending_bios.head || device->pending_sync_bios.head)
313 goto loop_lock;
314 spin_unlock(&device->io_lock);
315
8b712842 316done:
211588ad 317 blk_finish_plug(&plug);
8b712842
CM
318}
319
b2950863 320static void pending_bios_fn(struct btrfs_work *work)
8b712842
CM
321{
322 struct btrfs_device *device;
323
324 device = container_of(work, struct btrfs_device, work);
325 run_scheduled_bios(device);
326}
327
a1b32a59 328static noinline int device_list_add(const char *path,
8a4b83cc
CM
329 struct btrfs_super_block *disk_super,
330 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
331{
332 struct btrfs_device *device;
333 struct btrfs_fs_devices *fs_devices;
334 u64 found_transid = btrfs_super_generation(disk_super);
3a0524dc 335 char *name;
8a4b83cc
CM
336
337 fs_devices = find_fsid(disk_super->fsid);
338 if (!fs_devices) {
515dc322 339 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
8a4b83cc
CM
340 if (!fs_devices)
341 return -ENOMEM;
342 INIT_LIST_HEAD(&fs_devices->devices);
b3075717 343 INIT_LIST_HEAD(&fs_devices->alloc_list);
8a4b83cc
CM
344 list_add(&fs_devices->list, &fs_uuids);
345 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
346 fs_devices->latest_devid = devid;
347 fs_devices->latest_trans = found_transid;
e5e9a520 348 mutex_init(&fs_devices->device_list_mutex);
8a4b83cc
CM
349 device = NULL;
350 } else {
a443755f
CM
351 device = __find_device(&fs_devices->devices, devid,
352 disk_super->dev_item.uuid);
8a4b83cc
CM
353 }
354 if (!device) {
2b82032c
YZ
355 if (fs_devices->opened)
356 return -EBUSY;
357
8a4b83cc
CM
358 device = kzalloc(sizeof(*device), GFP_NOFS);
359 if (!device) {
360 /* we can safely leave the fs_devices entry around */
361 return -ENOMEM;
362 }
363 device->devid = devid;
8b712842 364 device->work.func = pending_bios_fn;
a443755f
CM
365 memcpy(device->uuid, disk_super->dev_item.uuid,
366 BTRFS_UUID_SIZE);
b248a415 367 spin_lock_init(&device->io_lock);
8a4b83cc
CM
368 device->name = kstrdup(path, GFP_NOFS);
369 if (!device->name) {
370 kfree(device);
371 return -ENOMEM;
372 }
2b82032c 373 INIT_LIST_HEAD(&device->dev_alloc_list);
e5e9a520 374
90519d66
AJ
375 /* init readahead state */
376 spin_lock_init(&device->reada_lock);
377 device->reada_curr_zone = NULL;
378 atomic_set(&device->reada_in_flight, 0);
379 device->reada_next = 0;
380 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
381 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
382
e5e9a520 383 mutex_lock(&fs_devices->device_list_mutex);
1f78160c 384 list_add_rcu(&device->dev_list, &fs_devices->devices);
e5e9a520
CM
385 mutex_unlock(&fs_devices->device_list_mutex);
386
2b82032c 387 device->fs_devices = fs_devices;
8a4b83cc 388 fs_devices->num_devices++;
cd02dca5 389 } else if (!device->name || strcmp(device->name, path)) {
3a0524dc
TH
390 name = kstrdup(path, GFP_NOFS);
391 if (!name)
392 return -ENOMEM;
393 kfree(device->name);
394 device->name = name;
cd02dca5
CM
395 if (device->missing) {
396 fs_devices->missing_devices--;
397 device->missing = 0;
398 }
8a4b83cc
CM
399 }
400
401 if (found_transid > fs_devices->latest_trans) {
402 fs_devices->latest_devid = devid;
403 fs_devices->latest_trans = found_transid;
404 }
8a4b83cc
CM
405 *fs_devices_ret = fs_devices;
406 return 0;
407}
408
e4404d6e
YZ
409static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
410{
411 struct btrfs_fs_devices *fs_devices;
412 struct btrfs_device *device;
413 struct btrfs_device *orig_dev;
414
415 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
416 if (!fs_devices)
417 return ERR_PTR(-ENOMEM);
418
419 INIT_LIST_HEAD(&fs_devices->devices);
420 INIT_LIST_HEAD(&fs_devices->alloc_list);
421 INIT_LIST_HEAD(&fs_devices->list);
e5e9a520 422 mutex_init(&fs_devices->device_list_mutex);
e4404d6e
YZ
423 fs_devices->latest_devid = orig->latest_devid;
424 fs_devices->latest_trans = orig->latest_trans;
425 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
426
46224705 427 /* We have held the volume lock, it is safe to get the devices. */
e4404d6e
YZ
428 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
429 device = kzalloc(sizeof(*device), GFP_NOFS);
430 if (!device)
431 goto error;
432
433 device->name = kstrdup(orig_dev->name, GFP_NOFS);
fd2696f3
JL
434 if (!device->name) {
435 kfree(device);
e4404d6e 436 goto error;
fd2696f3 437 }
e4404d6e
YZ
438
439 device->devid = orig_dev->devid;
440 device->work.func = pending_bios_fn;
441 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
e4404d6e
YZ
442 spin_lock_init(&device->io_lock);
443 INIT_LIST_HEAD(&device->dev_list);
444 INIT_LIST_HEAD(&device->dev_alloc_list);
445
446 list_add(&device->dev_list, &fs_devices->devices);
447 device->fs_devices = fs_devices;
448 fs_devices->num_devices++;
449 }
450 return fs_devices;
451error:
452 free_fs_devices(fs_devices);
453 return ERR_PTR(-ENOMEM);
454}
455
143bede5 456void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
dfe25020 457{
c6e30871 458 struct btrfs_device *device, *next;
dfe25020 459
a6b0d5c8
CM
460 struct block_device *latest_bdev = NULL;
461 u64 latest_devid = 0;
462 u64 latest_transid = 0;
463
dfe25020
CM
464 mutex_lock(&uuid_mutex);
465again:
46224705 466 /* This is the initialized path, it is safe to release the devices. */
c6e30871 467 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
a6b0d5c8
CM
468 if (device->in_fs_metadata) {
469 if (!latest_transid ||
470 device->generation > latest_transid) {
471 latest_devid = device->devid;
472 latest_transid = device->generation;
473 latest_bdev = device->bdev;
474 }
2b82032c 475 continue;
a6b0d5c8 476 }
2b82032c
YZ
477
478 if (device->bdev) {
d4d77629 479 blkdev_put(device->bdev, device->mode);
2b82032c
YZ
480 device->bdev = NULL;
481 fs_devices->open_devices--;
482 }
483 if (device->writeable) {
484 list_del_init(&device->dev_alloc_list);
485 device->writeable = 0;
486 fs_devices->rw_devices--;
487 }
e4404d6e
YZ
488 list_del_init(&device->dev_list);
489 fs_devices->num_devices--;
490 kfree(device->name);
491 kfree(device);
dfe25020 492 }
2b82032c
YZ
493
494 if (fs_devices->seed) {
495 fs_devices = fs_devices->seed;
2b82032c
YZ
496 goto again;
497 }
498
a6b0d5c8
CM
499 fs_devices->latest_bdev = latest_bdev;
500 fs_devices->latest_devid = latest_devid;
501 fs_devices->latest_trans = latest_transid;
502
dfe25020 503 mutex_unlock(&uuid_mutex);
dfe25020 504}
a0af469b 505
1f78160c
XG
506static void __free_device(struct work_struct *work)
507{
508 struct btrfs_device *device;
509
510 device = container_of(work, struct btrfs_device, rcu_work);
511
512 if (device->bdev)
513 blkdev_put(device->bdev, device->mode);
514
515 kfree(device->name);
516 kfree(device);
517}
518
519static void free_device(struct rcu_head *head)
520{
521 struct btrfs_device *device;
522
523 device = container_of(head, struct btrfs_device, rcu);
524
525 INIT_WORK(&device->rcu_work, __free_device);
526 schedule_work(&device->rcu_work);
527}
528
2b82032c 529static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
8a4b83cc 530{
8a4b83cc 531 struct btrfs_device *device;
e4404d6e 532
2b82032c
YZ
533 if (--fs_devices->opened > 0)
534 return 0;
8a4b83cc 535
c9513edb 536 mutex_lock(&fs_devices->device_list_mutex);
c6e30871 537 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1f78160c
XG
538 struct btrfs_device *new_device;
539
540 if (device->bdev)
a0af469b 541 fs_devices->open_devices--;
1f78160c 542
2b82032c
YZ
543 if (device->writeable) {
544 list_del_init(&device->dev_alloc_list);
545 fs_devices->rw_devices--;
546 }
547
d5e2003c
JB
548 if (device->can_discard)
549 fs_devices->num_can_discard--;
550
1f78160c 551 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
79787eaa 552 BUG_ON(!new_device); /* -ENOMEM */
1f78160c
XG
553 memcpy(new_device, device, sizeof(*new_device));
554 new_device->name = kstrdup(device->name, GFP_NOFS);
79787eaa 555 BUG_ON(device->name && !new_device->name); /* -ENOMEM */
1f78160c
XG
556 new_device->bdev = NULL;
557 new_device->writeable = 0;
558 new_device->in_fs_metadata = 0;
d5e2003c 559 new_device->can_discard = 0;
1f78160c
XG
560 list_replace_rcu(&device->dev_list, &new_device->dev_list);
561
562 call_rcu(&device->rcu, free_device);
8a4b83cc 563 }
c9513edb
XG
564 mutex_unlock(&fs_devices->device_list_mutex);
565
e4404d6e
YZ
566 WARN_ON(fs_devices->open_devices);
567 WARN_ON(fs_devices->rw_devices);
2b82032c
YZ
568 fs_devices->opened = 0;
569 fs_devices->seeding = 0;
2b82032c 570
8a4b83cc
CM
571 return 0;
572}
573
2b82032c
YZ
574int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
575{
e4404d6e 576 struct btrfs_fs_devices *seed_devices = NULL;
2b82032c
YZ
577 int ret;
578
579 mutex_lock(&uuid_mutex);
580 ret = __btrfs_close_devices(fs_devices);
e4404d6e
YZ
581 if (!fs_devices->opened) {
582 seed_devices = fs_devices->seed;
583 fs_devices->seed = NULL;
584 }
2b82032c 585 mutex_unlock(&uuid_mutex);
e4404d6e
YZ
586
587 while (seed_devices) {
588 fs_devices = seed_devices;
589 seed_devices = fs_devices->seed;
590 __btrfs_close_devices(fs_devices);
591 free_fs_devices(fs_devices);
592 }
2b82032c
YZ
593 return ret;
594}
595
e4404d6e
YZ
596static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
597 fmode_t flags, void *holder)
8a4b83cc 598{
d5e2003c 599 struct request_queue *q;
8a4b83cc
CM
600 struct block_device *bdev;
601 struct list_head *head = &fs_devices->devices;
8a4b83cc 602 struct btrfs_device *device;
a0af469b
CM
603 struct block_device *latest_bdev = NULL;
604 struct buffer_head *bh;
605 struct btrfs_super_block *disk_super;
606 u64 latest_devid = 0;
607 u64 latest_transid = 0;
a0af469b 608 u64 devid;
2b82032c 609 int seeding = 1;
a0af469b 610 int ret = 0;
8a4b83cc 611
d4d77629
TH
612 flags |= FMODE_EXCL;
613
c6e30871 614 list_for_each_entry(device, head, dev_list) {
c1c4d91c
CM
615 if (device->bdev)
616 continue;
dfe25020
CM
617 if (!device->name)
618 continue;
619
d4d77629 620 bdev = blkdev_get_by_path(device->name, flags, holder);
8a4b83cc 621 if (IS_ERR(bdev)) {
d397712b 622 printk(KERN_INFO "open %s failed\n", device->name);
a0af469b 623 goto error;
8a4b83cc 624 }
3c4bb26b
CM
625 filemap_write_and_wait(bdev->bd_inode->i_mapping);
626 invalidate_bdev(bdev);
a061fc8d 627 set_blocksize(bdev, 4096);
a0af469b 628
a512bbf8 629 bh = btrfs_read_dev_super(bdev);
20bcd649 630 if (!bh)
a0af469b
CM
631 goto error_close;
632
633 disk_super = (struct btrfs_super_block *)bh->b_data;
a343832f 634 devid = btrfs_stack_device_id(&disk_super->dev_item);
a0af469b
CM
635 if (devid != device->devid)
636 goto error_brelse;
637
2b82032c
YZ
638 if (memcmp(device->uuid, disk_super->dev_item.uuid,
639 BTRFS_UUID_SIZE))
640 goto error_brelse;
641
642 device->generation = btrfs_super_generation(disk_super);
643 if (!latest_transid || device->generation > latest_transid) {
a0af469b 644 latest_devid = devid;
2b82032c 645 latest_transid = device->generation;
a0af469b
CM
646 latest_bdev = bdev;
647 }
648
2b82032c
YZ
649 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
650 device->writeable = 0;
651 } else {
652 device->writeable = !bdev_read_only(bdev);
653 seeding = 0;
654 }
655
d5e2003c
JB
656 q = bdev_get_queue(bdev);
657 if (blk_queue_discard(q)) {
658 device->can_discard = 1;
659 fs_devices->num_can_discard++;
660 }
661
8a4b83cc 662 device->bdev = bdev;
dfe25020 663 device->in_fs_metadata = 0;
15916de8
CM
664 device->mode = flags;
665
c289811c
CM
666 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
667 fs_devices->rotating = 1;
668
a0af469b 669 fs_devices->open_devices++;
2b82032c
YZ
670 if (device->writeable) {
671 fs_devices->rw_devices++;
672 list_add(&device->dev_alloc_list,
673 &fs_devices->alloc_list);
674 }
4f6c9328 675 brelse(bh);
a0af469b 676 continue;
a061fc8d 677
a0af469b
CM
678error_brelse:
679 brelse(bh);
680error_close:
d4d77629 681 blkdev_put(bdev, flags);
a0af469b
CM
682error:
683 continue;
8a4b83cc 684 }
a0af469b 685 if (fs_devices->open_devices == 0) {
20bcd649 686 ret = -EINVAL;
a0af469b
CM
687 goto out;
688 }
2b82032c
YZ
689 fs_devices->seeding = seeding;
690 fs_devices->opened = 1;
a0af469b
CM
691 fs_devices->latest_bdev = latest_bdev;
692 fs_devices->latest_devid = latest_devid;
693 fs_devices->latest_trans = latest_transid;
2b82032c 694 fs_devices->total_rw_bytes = 0;
a0af469b 695out:
2b82032c
YZ
696 return ret;
697}
698
699int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
97288f2c 700 fmode_t flags, void *holder)
2b82032c
YZ
701{
702 int ret;
703
704 mutex_lock(&uuid_mutex);
705 if (fs_devices->opened) {
e4404d6e
YZ
706 fs_devices->opened++;
707 ret = 0;
2b82032c 708 } else {
15916de8 709 ret = __btrfs_open_devices(fs_devices, flags, holder);
2b82032c 710 }
8a4b83cc 711 mutex_unlock(&uuid_mutex);
8a4b83cc
CM
712 return ret;
713}
714
97288f2c 715int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
8a4b83cc
CM
716 struct btrfs_fs_devices **fs_devices_ret)
717{
718 struct btrfs_super_block *disk_super;
719 struct block_device *bdev;
720 struct buffer_head *bh;
721 int ret;
722 u64 devid;
f2984462 723 u64 transid;
8a4b83cc 724
d4d77629
TH
725 flags |= FMODE_EXCL;
726 bdev = blkdev_get_by_path(path, flags, holder);
8a4b83cc
CM
727
728 if (IS_ERR(bdev)) {
8a4b83cc
CM
729 ret = PTR_ERR(bdev);
730 goto error;
731 }
732
10f6327b 733 mutex_lock(&uuid_mutex);
8a4b83cc
CM
734 ret = set_blocksize(bdev, 4096);
735 if (ret)
736 goto error_close;
a512bbf8 737 bh = btrfs_read_dev_super(bdev);
8a4b83cc 738 if (!bh) {
20b45077 739 ret = -EINVAL;
8a4b83cc
CM
740 goto error_close;
741 }
742 disk_super = (struct btrfs_super_block *)bh->b_data;
a343832f 743 devid = btrfs_stack_device_id(&disk_super->dev_item);
f2984462 744 transid = btrfs_super_generation(disk_super);
7ae9c09d 745 if (disk_super->label[0])
d397712b 746 printk(KERN_INFO "device label %s ", disk_super->label);
22b63a29
ID
747 else
748 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
119e10cf 749 printk(KERN_CONT "devid %llu transid %llu %s\n",
d397712b 750 (unsigned long long)devid, (unsigned long long)transid, path);
8a4b83cc
CM
751 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
752
8a4b83cc
CM
753 brelse(bh);
754error_close:
10f6327b 755 mutex_unlock(&uuid_mutex);
d4d77629 756 blkdev_put(bdev, flags);
8a4b83cc 757error:
8a4b83cc
CM
758 return ret;
759}
0b86a832 760
6d07bcec
MX
761/* helper to account the used device space in the range */
762int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
763 u64 end, u64 *length)
764{
765 struct btrfs_key key;
766 struct btrfs_root *root = device->dev_root;
767 struct btrfs_dev_extent *dev_extent;
768 struct btrfs_path *path;
769 u64 extent_end;
770 int ret;
771 int slot;
772 struct extent_buffer *l;
773
774 *length = 0;
775
776 if (start >= device->total_bytes)
777 return 0;
778
779 path = btrfs_alloc_path();
780 if (!path)
781 return -ENOMEM;
782 path->reada = 2;
783
784 key.objectid = device->devid;
785 key.offset = start;
786 key.type = BTRFS_DEV_EXTENT_KEY;
787
788 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
789 if (ret < 0)
790 goto out;
791 if (ret > 0) {
792 ret = btrfs_previous_item(root, path, key.objectid, key.type);
793 if (ret < 0)
794 goto out;
795 }
796
797 while (1) {
798 l = path->nodes[0];
799 slot = path->slots[0];
800 if (slot >= btrfs_header_nritems(l)) {
801 ret = btrfs_next_leaf(root, path);
802 if (ret == 0)
803 continue;
804 if (ret < 0)
805 goto out;
806
807 break;
808 }
809 btrfs_item_key_to_cpu(l, &key, slot);
810
811 if (key.objectid < device->devid)
812 goto next;
813
814 if (key.objectid > device->devid)
815 break;
816
817 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
818 goto next;
819
820 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
821 extent_end = key.offset + btrfs_dev_extent_length(l,
822 dev_extent);
823 if (key.offset <= start && extent_end > end) {
824 *length = end - start + 1;
825 break;
826 } else if (key.offset <= start && extent_end > start)
827 *length += extent_end - start;
828 else if (key.offset > start && extent_end <= end)
829 *length += extent_end - key.offset;
830 else if (key.offset > start && key.offset <= end) {
831 *length += end - key.offset + 1;
832 break;
833 } else if (key.offset > end)
834 break;
835
836next:
837 path->slots[0]++;
838 }
839 ret = 0;
840out:
841 btrfs_free_path(path);
842 return ret;
843}
844
0b86a832 845/*
7bfc837d 846 * find_free_dev_extent - find free space in the specified device
7bfc837d
MX
847 * @device: the device which we search the free space in
848 * @num_bytes: the size of the free space that we need
849 * @start: store the start of the free space.
850 * @len: the size of the free space. that we find, or the size of the max
851 * free space if we don't find suitable free space
852 *
0b86a832
CM
853 * this uses a pretty simple search, the expectation is that it is
854 * called very infrequently and that a given device has a small number
855 * of extents
7bfc837d
MX
856 *
857 * @start is used to store the start of the free space if we find. But if we
858 * don't find suitable free space, it will be used to store the start position
859 * of the max free space.
860 *
861 * @len is used to store the size of the free space that we find.
862 * But if we don't find suitable free space, it is used to store the size of
863 * the max free space.
0b86a832 864 */
125ccb0a 865int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
7bfc837d 866 u64 *start, u64 *len)
0b86a832
CM
867{
868 struct btrfs_key key;
869 struct btrfs_root *root = device->dev_root;
7bfc837d 870 struct btrfs_dev_extent *dev_extent;
2b82032c 871 struct btrfs_path *path;
7bfc837d
MX
872 u64 hole_size;
873 u64 max_hole_start;
874 u64 max_hole_size;
875 u64 extent_end;
876 u64 search_start;
0b86a832
CM
877 u64 search_end = device->total_bytes;
878 int ret;
7bfc837d 879 int slot;
0b86a832
CM
880 struct extent_buffer *l;
881
0b86a832
CM
882 /* FIXME use last free of some kind */
883
8a4b83cc
CM
884 /* we don't want to overwrite the superblock on the drive,
885 * so we make sure to start at an offset of at least 1MB
886 */
a9c9bf68 887 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
8f18cf13 888
7bfc837d
MX
889 max_hole_start = search_start;
890 max_hole_size = 0;
38c01b96 891 hole_size = 0;
7bfc837d
MX
892
893 if (search_start >= search_end) {
894 ret = -ENOSPC;
895 goto error;
896 }
897
898 path = btrfs_alloc_path();
899 if (!path) {
900 ret = -ENOMEM;
901 goto error;
902 }
903 path->reada = 2;
904
0b86a832
CM
905 key.objectid = device->devid;
906 key.offset = search_start;
907 key.type = BTRFS_DEV_EXTENT_KEY;
7bfc837d 908
125ccb0a 909 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
0b86a832 910 if (ret < 0)
7bfc837d 911 goto out;
1fcbac58
YZ
912 if (ret > 0) {
913 ret = btrfs_previous_item(root, path, key.objectid, key.type);
914 if (ret < 0)
7bfc837d 915 goto out;
1fcbac58 916 }
7bfc837d 917
0b86a832
CM
918 while (1) {
919 l = path->nodes[0];
920 slot = path->slots[0];
921 if (slot >= btrfs_header_nritems(l)) {
922 ret = btrfs_next_leaf(root, path);
923 if (ret == 0)
924 continue;
925 if (ret < 0)
7bfc837d
MX
926 goto out;
927
928 break;
0b86a832
CM
929 }
930 btrfs_item_key_to_cpu(l, &key, slot);
931
932 if (key.objectid < device->devid)
933 goto next;
934
935 if (key.objectid > device->devid)
7bfc837d 936 break;
0b86a832 937
7bfc837d
MX
938 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
939 goto next;
9779b72f 940
7bfc837d
MX
941 if (key.offset > search_start) {
942 hole_size = key.offset - search_start;
9779b72f 943
7bfc837d
MX
944 if (hole_size > max_hole_size) {
945 max_hole_start = search_start;
946 max_hole_size = hole_size;
947 }
9779b72f 948
7bfc837d
MX
949 /*
950 * If this free space is greater than which we need,
951 * it must be the max free space that we have found
952 * until now, so max_hole_start must point to the start
953 * of this free space and the length of this free space
954 * is stored in max_hole_size. Thus, we return
955 * max_hole_start and max_hole_size and go back to the
956 * caller.
957 */
958 if (hole_size >= num_bytes) {
959 ret = 0;
960 goto out;
0b86a832
CM
961 }
962 }
0b86a832 963
0b86a832 964 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
7bfc837d
MX
965 extent_end = key.offset + btrfs_dev_extent_length(l,
966 dev_extent);
967 if (extent_end > search_start)
968 search_start = extent_end;
0b86a832
CM
969next:
970 path->slots[0]++;
971 cond_resched();
972 }
0b86a832 973
38c01b96 974 /*
975 * At this point, search_start should be the end of
976 * allocated dev extents, and when shrinking the device,
977 * search_end may be smaller than search_start.
978 */
979 if (search_end > search_start)
980 hole_size = search_end - search_start;
981
7bfc837d
MX
982 if (hole_size > max_hole_size) {
983 max_hole_start = search_start;
984 max_hole_size = hole_size;
0b86a832 985 }
0b86a832 986
7bfc837d
MX
987 /* See above. */
988 if (hole_size < num_bytes)
989 ret = -ENOSPC;
990 else
991 ret = 0;
992
993out:
2b82032c 994 btrfs_free_path(path);
7bfc837d
MX
995error:
996 *start = max_hole_start;
b2117a39 997 if (len)
7bfc837d 998 *len = max_hole_size;
0b86a832
CM
999 return ret;
1000}
1001
b2950863 1002static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
8f18cf13
CM
1003 struct btrfs_device *device,
1004 u64 start)
1005{
1006 int ret;
1007 struct btrfs_path *path;
1008 struct btrfs_root *root = device->dev_root;
1009 struct btrfs_key key;
a061fc8d
CM
1010 struct btrfs_key found_key;
1011 struct extent_buffer *leaf = NULL;
1012 struct btrfs_dev_extent *extent = NULL;
8f18cf13
CM
1013
1014 path = btrfs_alloc_path();
1015 if (!path)
1016 return -ENOMEM;
1017
1018 key.objectid = device->devid;
1019 key.offset = start;
1020 key.type = BTRFS_DEV_EXTENT_KEY;
924cd8fb 1021again:
8f18cf13 1022 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
a061fc8d
CM
1023 if (ret > 0) {
1024 ret = btrfs_previous_item(root, path, key.objectid,
1025 BTRFS_DEV_EXTENT_KEY);
b0b802d7
TI
1026 if (ret)
1027 goto out;
a061fc8d
CM
1028 leaf = path->nodes[0];
1029 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1030 extent = btrfs_item_ptr(leaf, path->slots[0],
1031 struct btrfs_dev_extent);
1032 BUG_ON(found_key.offset > start || found_key.offset +
1033 btrfs_dev_extent_length(leaf, extent) < start);
924cd8fb
MX
1034 key = found_key;
1035 btrfs_release_path(path);
1036 goto again;
a061fc8d
CM
1037 } else if (ret == 0) {
1038 leaf = path->nodes[0];
1039 extent = btrfs_item_ptr(leaf, path->slots[0],
1040 struct btrfs_dev_extent);
79787eaa
JM
1041 } else {
1042 btrfs_error(root->fs_info, ret, "Slot search failed");
1043 goto out;
a061fc8d 1044 }
8f18cf13 1045
2bf64758
JB
1046 if (device->bytes_used > 0) {
1047 u64 len = btrfs_dev_extent_length(leaf, extent);
1048 device->bytes_used -= len;
1049 spin_lock(&root->fs_info->free_chunk_lock);
1050 root->fs_info->free_chunk_space += len;
1051 spin_unlock(&root->fs_info->free_chunk_lock);
1052 }
8f18cf13 1053 ret = btrfs_del_item(trans, root, path);
79787eaa
JM
1054 if (ret) {
1055 btrfs_error(root->fs_info, ret,
1056 "Failed to remove dev extent item");
1057 }
b0b802d7 1058out:
8f18cf13
CM
1059 btrfs_free_path(path);
1060 return ret;
1061}
1062
2b82032c 1063int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
0b86a832 1064 struct btrfs_device *device,
e17cade2 1065 u64 chunk_tree, u64 chunk_objectid,
2b82032c 1066 u64 chunk_offset, u64 start, u64 num_bytes)
0b86a832
CM
1067{
1068 int ret;
1069 struct btrfs_path *path;
1070 struct btrfs_root *root = device->dev_root;
1071 struct btrfs_dev_extent *extent;
1072 struct extent_buffer *leaf;
1073 struct btrfs_key key;
1074
dfe25020 1075 WARN_ON(!device->in_fs_metadata);
0b86a832
CM
1076 path = btrfs_alloc_path();
1077 if (!path)
1078 return -ENOMEM;
1079
0b86a832 1080 key.objectid = device->devid;
2b82032c 1081 key.offset = start;
0b86a832
CM
1082 key.type = BTRFS_DEV_EXTENT_KEY;
1083 ret = btrfs_insert_empty_item(trans, root, path, &key,
1084 sizeof(*extent));
2cdcecbc
MF
1085 if (ret)
1086 goto out;
0b86a832
CM
1087
1088 leaf = path->nodes[0];
1089 extent = btrfs_item_ptr(leaf, path->slots[0],
1090 struct btrfs_dev_extent);
e17cade2
CM
1091 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1092 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1093 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1094
1095 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1096 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1097 BTRFS_UUID_SIZE);
1098
0b86a832
CM
1099 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1100 btrfs_mark_buffer_dirty(leaf);
2cdcecbc 1101out:
0b86a832
CM
1102 btrfs_free_path(path);
1103 return ret;
1104}
1105
a1b32a59
CM
1106static noinline int find_next_chunk(struct btrfs_root *root,
1107 u64 objectid, u64 *offset)
0b86a832
CM
1108{
1109 struct btrfs_path *path;
1110 int ret;
1111 struct btrfs_key key;
e17cade2 1112 struct btrfs_chunk *chunk;
0b86a832
CM
1113 struct btrfs_key found_key;
1114
1115 path = btrfs_alloc_path();
92b8e897
MF
1116 if (!path)
1117 return -ENOMEM;
0b86a832 1118
e17cade2 1119 key.objectid = objectid;
0b86a832
CM
1120 key.offset = (u64)-1;
1121 key.type = BTRFS_CHUNK_ITEM_KEY;
1122
1123 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1124 if (ret < 0)
1125 goto error;
1126
79787eaa 1127 BUG_ON(ret == 0); /* Corruption */
0b86a832
CM
1128
1129 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1130 if (ret) {
e17cade2 1131 *offset = 0;
0b86a832
CM
1132 } else {
1133 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1134 path->slots[0]);
e17cade2
CM
1135 if (found_key.objectid != objectid)
1136 *offset = 0;
1137 else {
1138 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1139 struct btrfs_chunk);
1140 *offset = found_key.offset +
1141 btrfs_chunk_length(path->nodes[0], chunk);
1142 }
0b86a832
CM
1143 }
1144 ret = 0;
1145error:
1146 btrfs_free_path(path);
1147 return ret;
1148}
1149
2b82032c 1150static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
0b86a832
CM
1151{
1152 int ret;
1153 struct btrfs_key key;
1154 struct btrfs_key found_key;
2b82032c
YZ
1155 struct btrfs_path *path;
1156
1157 root = root->fs_info->chunk_root;
1158
1159 path = btrfs_alloc_path();
1160 if (!path)
1161 return -ENOMEM;
0b86a832
CM
1162
1163 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1164 key.type = BTRFS_DEV_ITEM_KEY;
1165 key.offset = (u64)-1;
1166
1167 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1168 if (ret < 0)
1169 goto error;
1170
79787eaa 1171 BUG_ON(ret == 0); /* Corruption */
0b86a832
CM
1172
1173 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1174 BTRFS_DEV_ITEM_KEY);
1175 if (ret) {
1176 *objectid = 1;
1177 } else {
1178 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1179 path->slots[0]);
1180 *objectid = found_key.offset + 1;
1181 }
1182 ret = 0;
1183error:
2b82032c 1184 btrfs_free_path(path);
0b86a832
CM
1185 return ret;
1186}
1187
1188/*
1189 * the device information is stored in the chunk root
1190 * the btrfs_device struct should be fully filled in
1191 */
1192int btrfs_add_device(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root,
1194 struct btrfs_device *device)
1195{
1196 int ret;
1197 struct btrfs_path *path;
1198 struct btrfs_dev_item *dev_item;
1199 struct extent_buffer *leaf;
1200 struct btrfs_key key;
1201 unsigned long ptr;
0b86a832
CM
1202
1203 root = root->fs_info->chunk_root;
1204
1205 path = btrfs_alloc_path();
1206 if (!path)
1207 return -ENOMEM;
1208
0b86a832
CM
1209 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1210 key.type = BTRFS_DEV_ITEM_KEY;
2b82032c 1211 key.offset = device->devid;
0b86a832
CM
1212
1213 ret = btrfs_insert_empty_item(trans, root, path, &key,
0d81ba5d 1214 sizeof(*dev_item));
0b86a832
CM
1215 if (ret)
1216 goto out;
1217
1218 leaf = path->nodes[0];
1219 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1220
1221 btrfs_set_device_id(leaf, dev_item, device->devid);
2b82032c 1222 btrfs_set_device_generation(leaf, dev_item, 0);
0b86a832
CM
1223 btrfs_set_device_type(leaf, dev_item, device->type);
1224 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1225 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1226 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
0b86a832
CM
1227 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1228 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
e17cade2
CM
1229 btrfs_set_device_group(leaf, dev_item, 0);
1230 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1231 btrfs_set_device_bandwidth(leaf, dev_item, 0);
c3027eb5 1232 btrfs_set_device_start_offset(leaf, dev_item, 0);
0b86a832 1233
0b86a832 1234 ptr = (unsigned long)btrfs_device_uuid(dev_item);
e17cade2 1235 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
2b82032c
YZ
1236 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1237 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
0b86a832 1238 btrfs_mark_buffer_dirty(leaf);
0b86a832 1239
2b82032c 1240 ret = 0;
0b86a832
CM
1241out:
1242 btrfs_free_path(path);
1243 return ret;
1244}
8f18cf13 1245
a061fc8d
CM
1246static int btrfs_rm_dev_item(struct btrfs_root *root,
1247 struct btrfs_device *device)
1248{
1249 int ret;
1250 struct btrfs_path *path;
a061fc8d 1251 struct btrfs_key key;
a061fc8d
CM
1252 struct btrfs_trans_handle *trans;
1253
1254 root = root->fs_info->chunk_root;
1255
1256 path = btrfs_alloc_path();
1257 if (!path)
1258 return -ENOMEM;
1259
a22285a6 1260 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
1261 if (IS_ERR(trans)) {
1262 btrfs_free_path(path);
1263 return PTR_ERR(trans);
1264 }
a061fc8d
CM
1265 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1266 key.type = BTRFS_DEV_ITEM_KEY;
1267 key.offset = device->devid;
7d9eb12c 1268 lock_chunks(root);
a061fc8d
CM
1269
1270 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1271 if (ret < 0)
1272 goto out;
1273
1274 if (ret > 0) {
1275 ret = -ENOENT;
1276 goto out;
1277 }
1278
1279 ret = btrfs_del_item(trans, root, path);
1280 if (ret)
1281 goto out;
a061fc8d
CM
1282out:
1283 btrfs_free_path(path);
7d9eb12c 1284 unlock_chunks(root);
a061fc8d
CM
1285 btrfs_commit_transaction(trans, root);
1286 return ret;
1287}
1288
1289int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1290{
1291 struct btrfs_device *device;
2b82032c 1292 struct btrfs_device *next_device;
a061fc8d 1293 struct block_device *bdev;
dfe25020 1294 struct buffer_head *bh = NULL;
a061fc8d 1295 struct btrfs_super_block *disk_super;
1f78160c 1296 struct btrfs_fs_devices *cur_devices;
a061fc8d
CM
1297 u64 all_avail;
1298 u64 devid;
2b82032c
YZ
1299 u64 num_devices;
1300 u8 *dev_uuid;
a061fc8d 1301 int ret = 0;
1f78160c 1302 bool clear_super = false;
a061fc8d 1303
a061fc8d
CM
1304 mutex_lock(&uuid_mutex);
1305
1306 all_avail = root->fs_info->avail_data_alloc_bits |
1307 root->fs_info->avail_system_alloc_bits |
1308 root->fs_info->avail_metadata_alloc_bits;
1309
1310 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
035fe03a 1311 root->fs_info->fs_devices->num_devices <= 4) {
d397712b
CM
1312 printk(KERN_ERR "btrfs: unable to go below four devices "
1313 "on raid10\n");
a061fc8d
CM
1314 ret = -EINVAL;
1315 goto out;
1316 }
1317
1318 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
035fe03a 1319 root->fs_info->fs_devices->num_devices <= 2) {
d397712b
CM
1320 printk(KERN_ERR "btrfs: unable to go below two "
1321 "devices on raid1\n");
a061fc8d
CM
1322 ret = -EINVAL;
1323 goto out;
1324 }
1325
dfe25020 1326 if (strcmp(device_path, "missing") == 0) {
dfe25020
CM
1327 struct list_head *devices;
1328 struct btrfs_device *tmp;
a061fc8d 1329
dfe25020
CM
1330 device = NULL;
1331 devices = &root->fs_info->fs_devices->devices;
46224705
XG
1332 /*
1333 * It is safe to read the devices since the volume_mutex
1334 * is held.
1335 */
c6e30871 1336 list_for_each_entry(tmp, devices, dev_list) {
dfe25020
CM
1337 if (tmp->in_fs_metadata && !tmp->bdev) {
1338 device = tmp;
1339 break;
1340 }
1341 }
1342 bdev = NULL;
1343 bh = NULL;
1344 disk_super = NULL;
1345 if (!device) {
d397712b
CM
1346 printk(KERN_ERR "btrfs: no missing devices found to "
1347 "remove\n");
dfe25020
CM
1348 goto out;
1349 }
dfe25020 1350 } else {
d4d77629
TH
1351 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1352 root->fs_info->bdev_holder);
dfe25020
CM
1353 if (IS_ERR(bdev)) {
1354 ret = PTR_ERR(bdev);
1355 goto out;
1356 }
a061fc8d 1357
2b82032c 1358 set_blocksize(bdev, 4096);
3c4bb26b 1359 invalidate_bdev(bdev);
a512bbf8 1360 bh = btrfs_read_dev_super(bdev);
dfe25020 1361 if (!bh) {
20b45077 1362 ret = -EINVAL;
dfe25020
CM
1363 goto error_close;
1364 }
1365 disk_super = (struct btrfs_super_block *)bh->b_data;
a343832f 1366 devid = btrfs_stack_device_id(&disk_super->dev_item);
2b82032c
YZ
1367 dev_uuid = disk_super->dev_item.uuid;
1368 device = btrfs_find_device(root, devid, dev_uuid,
1369 disk_super->fsid);
dfe25020
CM
1370 if (!device) {
1371 ret = -ENOENT;
1372 goto error_brelse;
1373 }
2b82032c 1374 }
dfe25020 1375
2b82032c 1376 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
d397712b
CM
1377 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1378 "device\n");
2b82032c
YZ
1379 ret = -EINVAL;
1380 goto error_brelse;
1381 }
1382
1383 if (device->writeable) {
0c1daee0 1384 lock_chunks(root);
2b82032c 1385 list_del_init(&device->dev_alloc_list);
0c1daee0 1386 unlock_chunks(root);
2b82032c 1387 root->fs_info->fs_devices->rw_devices--;
1f78160c 1388 clear_super = true;
dfe25020 1389 }
a061fc8d
CM
1390
1391 ret = btrfs_shrink_device(device, 0);
1392 if (ret)
9b3517e9 1393 goto error_undo;
a061fc8d 1394
a061fc8d
CM
1395 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1396 if (ret)
9b3517e9 1397 goto error_undo;
a061fc8d 1398
2bf64758
JB
1399 spin_lock(&root->fs_info->free_chunk_lock);
1400 root->fs_info->free_chunk_space = device->total_bytes -
1401 device->bytes_used;
1402 spin_unlock(&root->fs_info->free_chunk_lock);
1403
2b82032c 1404 device->in_fs_metadata = 0;
a2de733c 1405 btrfs_scrub_cancel_dev(root, device);
e5e9a520
CM
1406
1407 /*
1408 * the device list mutex makes sure that we don't change
1409 * the device list while someone else is writing out all
1410 * the device supers.
1411 */
1f78160c
XG
1412
1413 cur_devices = device->fs_devices;
e5e9a520 1414 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1f78160c 1415 list_del_rcu(&device->dev_list);
e5e9a520 1416
e4404d6e 1417 device->fs_devices->num_devices--;
2b82032c 1418
cd02dca5
CM
1419 if (device->missing)
1420 root->fs_info->fs_devices->missing_devices--;
1421
2b82032c
YZ
1422 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1423 struct btrfs_device, dev_list);
1424 if (device->bdev == root->fs_info->sb->s_bdev)
1425 root->fs_info->sb->s_bdev = next_device->bdev;
1426 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1427 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1428
1f78160c 1429 if (device->bdev)
e4404d6e 1430 device->fs_devices->open_devices--;
1f78160c
XG
1431
1432 call_rcu(&device->rcu, free_device);
1433 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
e4404d6e 1434
6c41761f
DS
1435 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1436 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
2b82032c 1437
1f78160c 1438 if (cur_devices->open_devices == 0) {
e4404d6e
YZ
1439 struct btrfs_fs_devices *fs_devices;
1440 fs_devices = root->fs_info->fs_devices;
1441 while (fs_devices) {
1f78160c 1442 if (fs_devices->seed == cur_devices)
e4404d6e
YZ
1443 break;
1444 fs_devices = fs_devices->seed;
2b82032c 1445 }
1f78160c
XG
1446 fs_devices->seed = cur_devices->seed;
1447 cur_devices->seed = NULL;
0c1daee0 1448 lock_chunks(root);
1f78160c 1449 __btrfs_close_devices(cur_devices);
0c1daee0 1450 unlock_chunks(root);
1f78160c 1451 free_fs_devices(cur_devices);
2b82032c
YZ
1452 }
1453
1454 /*
1455 * at this point, the device is zero sized. We want to
1456 * remove it from the devices list and zero out the old super
1457 */
1f78160c 1458 if (clear_super) {
dfe25020
CM
1459 /* make sure this device isn't detected as part of
1460 * the FS anymore
1461 */
1462 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1463 set_buffer_dirty(bh);
1464 sync_dirty_buffer(bh);
dfe25020 1465 }
a061fc8d 1466
a061fc8d 1467 ret = 0;
a061fc8d
CM
1468
1469error_brelse:
1470 brelse(bh);
1471error_close:
dfe25020 1472 if (bdev)
e525fd89 1473 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
a061fc8d
CM
1474out:
1475 mutex_unlock(&uuid_mutex);
a061fc8d 1476 return ret;
9b3517e9
ID
1477error_undo:
1478 if (device->writeable) {
0c1daee0 1479 lock_chunks(root);
9b3517e9
ID
1480 list_add(&device->dev_alloc_list,
1481 &root->fs_info->fs_devices->alloc_list);
0c1daee0 1482 unlock_chunks(root);
9b3517e9
ID
1483 root->fs_info->fs_devices->rw_devices++;
1484 }
1485 goto error_brelse;
a061fc8d
CM
1486}
1487
2b82032c
YZ
1488/*
1489 * does all the dirty work required for changing file system's UUID.
1490 */
125ccb0a 1491static int btrfs_prepare_sprout(struct btrfs_root *root)
2b82032c
YZ
1492{
1493 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1494 struct btrfs_fs_devices *old_devices;
e4404d6e 1495 struct btrfs_fs_devices *seed_devices;
6c41761f 1496 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
2b82032c
YZ
1497 struct btrfs_device *device;
1498 u64 super_flags;
1499
1500 BUG_ON(!mutex_is_locked(&uuid_mutex));
e4404d6e 1501 if (!fs_devices->seeding)
2b82032c
YZ
1502 return -EINVAL;
1503
e4404d6e
YZ
1504 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1505 if (!seed_devices)
2b82032c
YZ
1506 return -ENOMEM;
1507
e4404d6e
YZ
1508 old_devices = clone_fs_devices(fs_devices);
1509 if (IS_ERR(old_devices)) {
1510 kfree(seed_devices);
1511 return PTR_ERR(old_devices);
2b82032c 1512 }
e4404d6e 1513
2b82032c
YZ
1514 list_add(&old_devices->list, &fs_uuids);
1515
e4404d6e
YZ
1516 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1517 seed_devices->opened = 1;
1518 INIT_LIST_HEAD(&seed_devices->devices);
1519 INIT_LIST_HEAD(&seed_devices->alloc_list);
e5e9a520 1520 mutex_init(&seed_devices->device_list_mutex);
c9513edb
XG
1521
1522 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1f78160c
XG
1523 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1524 synchronize_rcu);
c9513edb
XG
1525 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1526
e4404d6e
YZ
1527 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1528 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1529 device->fs_devices = seed_devices;
1530 }
1531
2b82032c
YZ
1532 fs_devices->seeding = 0;
1533 fs_devices->num_devices = 0;
1534 fs_devices->open_devices = 0;
e4404d6e 1535 fs_devices->seed = seed_devices;
2b82032c
YZ
1536
1537 generate_random_uuid(fs_devices->fsid);
1538 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1539 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1540 super_flags = btrfs_super_flags(disk_super) &
1541 ~BTRFS_SUPER_FLAG_SEEDING;
1542 btrfs_set_super_flags(disk_super, super_flags);
1543
1544 return 0;
1545}
1546
1547/*
1548 * strore the expected generation for seed devices in device items.
1549 */
1550static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1551 struct btrfs_root *root)
1552{
1553 struct btrfs_path *path;
1554 struct extent_buffer *leaf;
1555 struct btrfs_dev_item *dev_item;
1556 struct btrfs_device *device;
1557 struct btrfs_key key;
1558 u8 fs_uuid[BTRFS_UUID_SIZE];
1559 u8 dev_uuid[BTRFS_UUID_SIZE];
1560 u64 devid;
1561 int ret;
1562
1563 path = btrfs_alloc_path();
1564 if (!path)
1565 return -ENOMEM;
1566
1567 root = root->fs_info->chunk_root;
1568 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1569 key.offset = 0;
1570 key.type = BTRFS_DEV_ITEM_KEY;
1571
1572 while (1) {
1573 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1574 if (ret < 0)
1575 goto error;
1576
1577 leaf = path->nodes[0];
1578next_slot:
1579 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1580 ret = btrfs_next_leaf(root, path);
1581 if (ret > 0)
1582 break;
1583 if (ret < 0)
1584 goto error;
1585 leaf = path->nodes[0];
1586 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
b3b4aa74 1587 btrfs_release_path(path);
2b82032c
YZ
1588 continue;
1589 }
1590
1591 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1592 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1593 key.type != BTRFS_DEV_ITEM_KEY)
1594 break;
1595
1596 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1597 struct btrfs_dev_item);
1598 devid = btrfs_device_id(leaf, dev_item);
1599 read_extent_buffer(leaf, dev_uuid,
1600 (unsigned long)btrfs_device_uuid(dev_item),
1601 BTRFS_UUID_SIZE);
1602 read_extent_buffer(leaf, fs_uuid,
1603 (unsigned long)btrfs_device_fsid(dev_item),
1604 BTRFS_UUID_SIZE);
1605 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
79787eaa 1606 BUG_ON(!device); /* Logic error */
2b82032c
YZ
1607
1608 if (device->fs_devices->seeding) {
1609 btrfs_set_device_generation(leaf, dev_item,
1610 device->generation);
1611 btrfs_mark_buffer_dirty(leaf);
1612 }
1613
1614 path->slots[0]++;
1615 goto next_slot;
1616 }
1617 ret = 0;
1618error:
1619 btrfs_free_path(path);
1620 return ret;
1621}
1622
788f20eb
CM
1623int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1624{
d5e2003c 1625 struct request_queue *q;
788f20eb
CM
1626 struct btrfs_trans_handle *trans;
1627 struct btrfs_device *device;
1628 struct block_device *bdev;
788f20eb 1629 struct list_head *devices;
2b82032c 1630 struct super_block *sb = root->fs_info->sb;
788f20eb 1631 u64 total_bytes;
2b82032c 1632 int seeding_dev = 0;
788f20eb
CM
1633 int ret = 0;
1634
2b82032c
YZ
1635 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1636 return -EINVAL;
788f20eb 1637
a5d16333 1638 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
d4d77629 1639 root->fs_info->bdev_holder);
7f59203a
JB
1640 if (IS_ERR(bdev))
1641 return PTR_ERR(bdev);
a2135011 1642
2b82032c
YZ
1643 if (root->fs_info->fs_devices->seeding) {
1644 seeding_dev = 1;
1645 down_write(&sb->s_umount);
1646 mutex_lock(&uuid_mutex);
1647 }
1648
8c8bee1d 1649 filemap_write_and_wait(bdev->bd_inode->i_mapping);
a2135011 1650
788f20eb 1651 devices = &root->fs_info->fs_devices->devices;
e5e9a520
CM
1652 /*
1653 * we have the volume lock, so we don't need the extra
1654 * device list mutex while reading the list here.
1655 */
c6e30871 1656 list_for_each_entry(device, devices, dev_list) {
788f20eb
CM
1657 if (device->bdev == bdev) {
1658 ret = -EEXIST;
2b82032c 1659 goto error;
788f20eb
CM
1660 }
1661 }
1662
1663 device = kzalloc(sizeof(*device), GFP_NOFS);
1664 if (!device) {
1665 /* we can safely leave the fs_devices entry around */
1666 ret = -ENOMEM;
2b82032c 1667 goto error;
788f20eb
CM
1668 }
1669
788f20eb
CM
1670 device->name = kstrdup(device_path, GFP_NOFS);
1671 if (!device->name) {
1672 kfree(device);
2b82032c
YZ
1673 ret = -ENOMEM;
1674 goto error;
788f20eb 1675 }
2b82032c
YZ
1676
1677 ret = find_next_devid(root, &device->devid);
1678 if (ret) {
67100f25 1679 kfree(device->name);
2b82032c
YZ
1680 kfree(device);
1681 goto error;
1682 }
1683
a22285a6 1684 trans = btrfs_start_transaction(root, 0);
98d5dc13 1685 if (IS_ERR(trans)) {
67100f25 1686 kfree(device->name);
98d5dc13
TI
1687 kfree(device);
1688 ret = PTR_ERR(trans);
1689 goto error;
1690 }
1691
2b82032c
YZ
1692 lock_chunks(root);
1693
d5e2003c
JB
1694 q = bdev_get_queue(bdev);
1695 if (blk_queue_discard(q))
1696 device->can_discard = 1;
2b82032c
YZ
1697 device->writeable = 1;
1698 device->work.func = pending_bios_fn;
1699 generate_random_uuid(device->uuid);
1700 spin_lock_init(&device->io_lock);
1701 device->generation = trans->transid;
788f20eb
CM
1702 device->io_width = root->sectorsize;
1703 device->io_align = root->sectorsize;
1704 device->sector_size = root->sectorsize;
1705 device->total_bytes = i_size_read(bdev->bd_inode);
2cc3c559 1706 device->disk_total_bytes = device->total_bytes;
788f20eb
CM
1707 device->dev_root = root->fs_info->dev_root;
1708 device->bdev = bdev;
dfe25020 1709 device->in_fs_metadata = 1;
fb01aa85 1710 device->mode = FMODE_EXCL;
2b82032c 1711 set_blocksize(device->bdev, 4096);
788f20eb 1712
2b82032c
YZ
1713 if (seeding_dev) {
1714 sb->s_flags &= ~MS_RDONLY;
125ccb0a 1715 ret = btrfs_prepare_sprout(root);
79787eaa 1716 BUG_ON(ret); /* -ENOMEM */
2b82032c 1717 }
788f20eb 1718
2b82032c 1719 device->fs_devices = root->fs_info->fs_devices;
e5e9a520
CM
1720
1721 /*
1722 * we don't want write_supers to jump in here with our device
1723 * half setup
1724 */
1725 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1f78160c 1726 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2b82032c
YZ
1727 list_add(&device->dev_alloc_list,
1728 &root->fs_info->fs_devices->alloc_list);
1729 root->fs_info->fs_devices->num_devices++;
1730 root->fs_info->fs_devices->open_devices++;
1731 root->fs_info->fs_devices->rw_devices++;
d5e2003c
JB
1732 if (device->can_discard)
1733 root->fs_info->fs_devices->num_can_discard++;
2b82032c 1734 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
325cd4ba 1735
2bf64758
JB
1736 spin_lock(&root->fs_info->free_chunk_lock);
1737 root->fs_info->free_chunk_space += device->total_bytes;
1738 spin_unlock(&root->fs_info->free_chunk_lock);
1739
c289811c
CM
1740 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1741 root->fs_info->fs_devices->rotating = 1;
1742
6c41761f
DS
1743 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1744 btrfs_set_super_total_bytes(root->fs_info->super_copy,
788f20eb
CM
1745 total_bytes + device->total_bytes);
1746
6c41761f
DS
1747 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1748 btrfs_set_super_num_devices(root->fs_info->super_copy,
788f20eb 1749 total_bytes + 1);
e5e9a520 1750 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
788f20eb 1751
2b82032c
YZ
1752 if (seeding_dev) {
1753 ret = init_first_rw_device(trans, root, device);
79787eaa
JM
1754 if (ret)
1755 goto error_trans;
2b82032c 1756 ret = btrfs_finish_sprout(trans, root);
79787eaa
JM
1757 if (ret)
1758 goto error_trans;
2b82032c
YZ
1759 } else {
1760 ret = btrfs_add_device(trans, root, device);
79787eaa
JM
1761 if (ret)
1762 goto error_trans;
2b82032c
YZ
1763 }
1764
913d952e
CM
1765 /*
1766 * we've got more storage, clear any full flags on the space
1767 * infos
1768 */
1769 btrfs_clear_space_info_full(root->fs_info);
1770
7d9eb12c 1771 unlock_chunks(root);
79787eaa 1772 ret = btrfs_commit_transaction(trans, root);
a2135011 1773
2b82032c
YZ
1774 if (seeding_dev) {
1775 mutex_unlock(&uuid_mutex);
1776 up_write(&sb->s_umount);
788f20eb 1777
79787eaa
JM
1778 if (ret) /* transaction commit */
1779 return ret;
1780
2b82032c 1781 ret = btrfs_relocate_sys_chunks(root);
79787eaa
JM
1782 if (ret < 0)
1783 btrfs_error(root->fs_info, ret,
1784 "Failed to relocate sys chunks after "
1785 "device initialization. This can be fixed "
1786 "using the \"btrfs balance\" command.");
2b82032c 1787 }
c9e9f97b 1788
2b82032c 1789 return ret;
79787eaa
JM
1790
1791error_trans:
1792 unlock_chunks(root);
1793 btrfs_abort_transaction(trans, root, ret);
1794 btrfs_end_transaction(trans, root);
1795 kfree(device->name);
1796 kfree(device);
2b82032c 1797error:
e525fd89 1798 blkdev_put(bdev, FMODE_EXCL);
2b82032c
YZ
1799 if (seeding_dev) {
1800 mutex_unlock(&uuid_mutex);
1801 up_write(&sb->s_umount);
1802 }
c9e9f97b 1803 return ret;
788f20eb
CM
1804}
1805
d397712b
CM
1806static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1807 struct btrfs_device *device)
0b86a832
CM
1808{
1809 int ret;
1810 struct btrfs_path *path;
1811 struct btrfs_root *root;
1812 struct btrfs_dev_item *dev_item;
1813 struct extent_buffer *leaf;
1814 struct btrfs_key key;
1815
1816 root = device->dev_root->fs_info->chunk_root;
1817
1818 path = btrfs_alloc_path();
1819 if (!path)
1820 return -ENOMEM;
1821
1822 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1823 key.type = BTRFS_DEV_ITEM_KEY;
1824 key.offset = device->devid;
1825
1826 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1827 if (ret < 0)
1828 goto out;
1829
1830 if (ret > 0) {
1831 ret = -ENOENT;
1832 goto out;
1833 }
1834
1835 leaf = path->nodes[0];
1836 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1837
1838 btrfs_set_device_id(leaf, dev_item, device->devid);
1839 btrfs_set_device_type(leaf, dev_item, device->type);
1840 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1841 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1842 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
d6397bae 1843 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
0b86a832
CM
1844 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1845 btrfs_mark_buffer_dirty(leaf);
1846
1847out:
1848 btrfs_free_path(path);
1849 return ret;
1850}
1851
7d9eb12c 1852static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
8f18cf13
CM
1853 struct btrfs_device *device, u64 new_size)
1854{
1855 struct btrfs_super_block *super_copy =
6c41761f 1856 device->dev_root->fs_info->super_copy;
8f18cf13
CM
1857 u64 old_total = btrfs_super_total_bytes(super_copy);
1858 u64 diff = new_size - device->total_bytes;
1859
2b82032c
YZ
1860 if (!device->writeable)
1861 return -EACCES;
1862 if (new_size <= device->total_bytes)
1863 return -EINVAL;
1864
8f18cf13 1865 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2b82032c
YZ
1866 device->fs_devices->total_rw_bytes += diff;
1867
1868 device->total_bytes = new_size;
9779b72f 1869 device->disk_total_bytes = new_size;
4184ea7f
CM
1870 btrfs_clear_space_info_full(device->dev_root->fs_info);
1871
8f18cf13
CM
1872 return btrfs_update_device(trans, device);
1873}
1874
7d9eb12c
CM
1875int btrfs_grow_device(struct btrfs_trans_handle *trans,
1876 struct btrfs_device *device, u64 new_size)
1877{
1878 int ret;
1879 lock_chunks(device->dev_root);
1880 ret = __btrfs_grow_device(trans, device, new_size);
1881 unlock_chunks(device->dev_root);
1882 return ret;
1883}
1884
8f18cf13
CM
1885static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 u64 chunk_tree, u64 chunk_objectid,
1888 u64 chunk_offset)
1889{
1890 int ret;
1891 struct btrfs_path *path;
1892 struct btrfs_key key;
1893
1894 root = root->fs_info->chunk_root;
1895 path = btrfs_alloc_path();
1896 if (!path)
1897 return -ENOMEM;
1898
1899 key.objectid = chunk_objectid;
1900 key.offset = chunk_offset;
1901 key.type = BTRFS_CHUNK_ITEM_KEY;
1902
1903 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
79787eaa
JM
1904 if (ret < 0)
1905 goto out;
1906 else if (ret > 0) { /* Logic error or corruption */
1907 btrfs_error(root->fs_info, -ENOENT,
1908 "Failed lookup while freeing chunk.");
1909 ret = -ENOENT;
1910 goto out;
1911 }
8f18cf13
CM
1912
1913 ret = btrfs_del_item(trans, root, path);
79787eaa
JM
1914 if (ret < 0)
1915 btrfs_error(root->fs_info, ret,
1916 "Failed to delete chunk item.");
1917out:
8f18cf13 1918 btrfs_free_path(path);
65a246c5 1919 return ret;
8f18cf13
CM
1920}
1921
b2950863 1922static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
8f18cf13
CM
1923 chunk_offset)
1924{
6c41761f 1925 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
8f18cf13
CM
1926 struct btrfs_disk_key *disk_key;
1927 struct btrfs_chunk *chunk;
1928 u8 *ptr;
1929 int ret = 0;
1930 u32 num_stripes;
1931 u32 array_size;
1932 u32 len = 0;
1933 u32 cur;
1934 struct btrfs_key key;
1935
1936 array_size = btrfs_super_sys_array_size(super_copy);
1937
1938 ptr = super_copy->sys_chunk_array;
1939 cur = 0;
1940
1941 while (cur < array_size) {
1942 disk_key = (struct btrfs_disk_key *)ptr;
1943 btrfs_disk_key_to_cpu(&key, disk_key);
1944
1945 len = sizeof(*disk_key);
1946
1947 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1948 chunk = (struct btrfs_chunk *)(ptr + len);
1949 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1950 len += btrfs_chunk_item_size(num_stripes);
1951 } else {
1952 ret = -EIO;
1953 break;
1954 }
1955 if (key.objectid == chunk_objectid &&
1956 key.offset == chunk_offset) {
1957 memmove(ptr, ptr + len, array_size - (cur + len));
1958 array_size -= len;
1959 btrfs_set_super_sys_array_size(super_copy, array_size);
1960 } else {
1961 ptr += len;
1962 cur += len;
1963 }
1964 }
1965 return ret;
1966}
1967
b2950863 1968static int btrfs_relocate_chunk(struct btrfs_root *root,
8f18cf13
CM
1969 u64 chunk_tree, u64 chunk_objectid,
1970 u64 chunk_offset)
1971{
1972 struct extent_map_tree *em_tree;
1973 struct btrfs_root *extent_root;
1974 struct btrfs_trans_handle *trans;
1975 struct extent_map *em;
1976 struct map_lookup *map;
1977 int ret;
1978 int i;
1979
1980 root = root->fs_info->chunk_root;
1981 extent_root = root->fs_info->extent_root;
1982 em_tree = &root->fs_info->mapping_tree.map_tree;
1983
ba1bf481
JB
1984 ret = btrfs_can_relocate(extent_root, chunk_offset);
1985 if (ret)
1986 return -ENOSPC;
1987
8f18cf13 1988 /* step one, relocate all the extents inside this chunk */
1a40e23b 1989 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
a22285a6
YZ
1990 if (ret)
1991 return ret;
8f18cf13 1992
a22285a6 1993 trans = btrfs_start_transaction(root, 0);
98d5dc13 1994 BUG_ON(IS_ERR(trans));
8f18cf13 1995
7d9eb12c
CM
1996 lock_chunks(root);
1997
8f18cf13
CM
1998 /*
1999 * step two, delete the device extents and the
2000 * chunk tree entries
2001 */
890871be 2002 read_lock(&em_tree->lock);
8f18cf13 2003 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
890871be 2004 read_unlock(&em_tree->lock);
8f18cf13 2005
285190d9 2006 BUG_ON(!em || em->start > chunk_offset ||
a061fc8d 2007 em->start + em->len < chunk_offset);
8f18cf13
CM
2008 map = (struct map_lookup *)em->bdev;
2009
2010 for (i = 0; i < map->num_stripes; i++) {
2011 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2012 map->stripes[i].physical);
2013 BUG_ON(ret);
a061fc8d 2014
dfe25020
CM
2015 if (map->stripes[i].dev) {
2016 ret = btrfs_update_device(trans, map->stripes[i].dev);
2017 BUG_ON(ret);
2018 }
8f18cf13
CM
2019 }
2020 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2021 chunk_offset);
2022
2023 BUG_ON(ret);
2024
1abe9b8a 2025 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2026
8f18cf13
CM
2027 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2028 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2029 BUG_ON(ret);
8f18cf13
CM
2030 }
2031
2b82032c
YZ
2032 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2033 BUG_ON(ret);
2034
890871be 2035 write_lock(&em_tree->lock);
2b82032c 2036 remove_extent_mapping(em_tree, em);
890871be 2037 write_unlock(&em_tree->lock);
2b82032c
YZ
2038
2039 kfree(map);
2040 em->bdev = NULL;
2041
2042 /* once for the tree */
2043 free_extent_map(em);
2044 /* once for us */
2045 free_extent_map(em);
2046
2047 unlock_chunks(root);
2048 btrfs_end_transaction(trans, root);
2049 return 0;
2050}
2051
2052static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2053{
2054 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2055 struct btrfs_path *path;
2056 struct extent_buffer *leaf;
2057 struct btrfs_chunk *chunk;
2058 struct btrfs_key key;
2059 struct btrfs_key found_key;
2060 u64 chunk_tree = chunk_root->root_key.objectid;
2061 u64 chunk_type;
ba1bf481
JB
2062 bool retried = false;
2063 int failed = 0;
2b82032c
YZ
2064 int ret;
2065
2066 path = btrfs_alloc_path();
2067 if (!path)
2068 return -ENOMEM;
2069
ba1bf481 2070again:
2b82032c
YZ
2071 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2072 key.offset = (u64)-1;
2073 key.type = BTRFS_CHUNK_ITEM_KEY;
2074
2075 while (1) {
2076 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2077 if (ret < 0)
2078 goto error;
79787eaa 2079 BUG_ON(ret == 0); /* Corruption */
2b82032c
YZ
2080
2081 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2082 key.type);
2083 if (ret < 0)
2084 goto error;
2085 if (ret > 0)
2086 break;
1a40e23b 2087
2b82032c
YZ
2088 leaf = path->nodes[0];
2089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1a40e23b 2090
2b82032c
YZ
2091 chunk = btrfs_item_ptr(leaf, path->slots[0],
2092 struct btrfs_chunk);
2093 chunk_type = btrfs_chunk_type(leaf, chunk);
b3b4aa74 2094 btrfs_release_path(path);
8f18cf13 2095
2b82032c
YZ
2096 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2097 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2098 found_key.objectid,
2099 found_key.offset);
ba1bf481
JB
2100 if (ret == -ENOSPC)
2101 failed++;
2102 else if (ret)
2103 BUG();
2b82032c 2104 }
8f18cf13 2105
2b82032c
YZ
2106 if (found_key.offset == 0)
2107 break;
2108 key.offset = found_key.offset - 1;
2109 }
2110 ret = 0;
ba1bf481
JB
2111 if (failed && !retried) {
2112 failed = 0;
2113 retried = true;
2114 goto again;
2115 } else if (failed && retried) {
2116 WARN_ON(1);
2117 ret = -ENOSPC;
2118 }
2b82032c
YZ
2119error:
2120 btrfs_free_path(path);
2121 return ret;
8f18cf13
CM
2122}
2123
0940ebf6
ID
2124static int insert_balance_item(struct btrfs_root *root,
2125 struct btrfs_balance_control *bctl)
2126{
2127 struct btrfs_trans_handle *trans;
2128 struct btrfs_balance_item *item;
2129 struct btrfs_disk_balance_args disk_bargs;
2130 struct btrfs_path *path;
2131 struct extent_buffer *leaf;
2132 struct btrfs_key key;
2133 int ret, err;
2134
2135 path = btrfs_alloc_path();
2136 if (!path)
2137 return -ENOMEM;
2138
2139 trans = btrfs_start_transaction(root, 0);
2140 if (IS_ERR(trans)) {
2141 btrfs_free_path(path);
2142 return PTR_ERR(trans);
2143 }
2144
2145 key.objectid = BTRFS_BALANCE_OBJECTID;
2146 key.type = BTRFS_BALANCE_ITEM_KEY;
2147 key.offset = 0;
2148
2149 ret = btrfs_insert_empty_item(trans, root, path, &key,
2150 sizeof(*item));
2151 if (ret)
2152 goto out;
2153
2154 leaf = path->nodes[0];
2155 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2156
2157 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2158
2159 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2160 btrfs_set_balance_data(leaf, item, &disk_bargs);
2161 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2162 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2163 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2164 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2165
2166 btrfs_set_balance_flags(leaf, item, bctl->flags);
2167
2168 btrfs_mark_buffer_dirty(leaf);
2169out:
2170 btrfs_free_path(path);
2171 err = btrfs_commit_transaction(trans, root);
2172 if (err && !ret)
2173 ret = err;
2174 return ret;
2175}
2176
2177static int del_balance_item(struct btrfs_root *root)
2178{
2179 struct btrfs_trans_handle *trans;
2180 struct btrfs_path *path;
2181 struct btrfs_key key;
2182 int ret, err;
2183
2184 path = btrfs_alloc_path();
2185 if (!path)
2186 return -ENOMEM;
2187
2188 trans = btrfs_start_transaction(root, 0);
2189 if (IS_ERR(trans)) {
2190 btrfs_free_path(path);
2191 return PTR_ERR(trans);
2192 }
2193
2194 key.objectid = BTRFS_BALANCE_OBJECTID;
2195 key.type = BTRFS_BALANCE_ITEM_KEY;
2196 key.offset = 0;
2197
2198 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2199 if (ret < 0)
2200 goto out;
2201 if (ret > 0) {
2202 ret = -ENOENT;
2203 goto out;
2204 }
2205
2206 ret = btrfs_del_item(trans, root, path);
2207out:
2208 btrfs_free_path(path);
2209 err = btrfs_commit_transaction(trans, root);
2210 if (err && !ret)
2211 ret = err;
2212 return ret;
2213}
2214
59641015
ID
2215/*
2216 * This is a heuristic used to reduce the number of chunks balanced on
2217 * resume after balance was interrupted.
2218 */
2219static void update_balance_args(struct btrfs_balance_control *bctl)
2220{
2221 /*
2222 * Turn on soft mode for chunk types that were being converted.
2223 */
2224 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2225 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2226 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2227 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2228 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2229 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2230
2231 /*
2232 * Turn on usage filter if is not already used. The idea is
2233 * that chunks that we have already balanced should be
2234 * reasonably full. Don't do it for chunks that are being
2235 * converted - that will keep us from relocating unconverted
2236 * (albeit full) chunks.
2237 */
2238 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2239 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2240 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2241 bctl->data.usage = 90;
2242 }
2243 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2244 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2245 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2246 bctl->sys.usage = 90;
2247 }
2248 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2249 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2250 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2251 bctl->meta.usage = 90;
2252 }
2253}
2254
c9e9f97b
ID
2255/*
2256 * Should be called with both balance and volume mutexes held to
2257 * serialize other volume operations (add_dev/rm_dev/resize) with
2258 * restriper. Same goes for unset_balance_control.
2259 */
2260static void set_balance_control(struct btrfs_balance_control *bctl)
2261{
2262 struct btrfs_fs_info *fs_info = bctl->fs_info;
2263
2264 BUG_ON(fs_info->balance_ctl);
2265
2266 spin_lock(&fs_info->balance_lock);
2267 fs_info->balance_ctl = bctl;
2268 spin_unlock(&fs_info->balance_lock);
2269}
2270
2271static void unset_balance_control(struct btrfs_fs_info *fs_info)
2272{
2273 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2274
2275 BUG_ON(!fs_info->balance_ctl);
2276
2277 spin_lock(&fs_info->balance_lock);
2278 fs_info->balance_ctl = NULL;
2279 spin_unlock(&fs_info->balance_lock);
2280
2281 kfree(bctl);
2282}
2283
ed25e9b2
ID
2284/*
2285 * Balance filters. Return 1 if chunk should be filtered out
2286 * (should not be balanced).
2287 */
899c81ea 2288static int chunk_profiles_filter(u64 chunk_type,
ed25e9b2
ID
2289 struct btrfs_balance_args *bargs)
2290{
899c81ea
ID
2291 chunk_type = chunk_to_extended(chunk_type) &
2292 BTRFS_EXTENDED_PROFILE_MASK;
ed25e9b2 2293
899c81ea 2294 if (bargs->profiles & chunk_type)
ed25e9b2
ID
2295 return 0;
2296
2297 return 1;
2298}
2299
5ce5b3c0
ID
2300static u64 div_factor_fine(u64 num, int factor)
2301{
2302 if (factor <= 0)
2303 return 0;
2304 if (factor >= 100)
2305 return num;
2306
2307 num *= factor;
2308 do_div(num, 100);
2309 return num;
2310}
2311
2312static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2313 struct btrfs_balance_args *bargs)
2314{
2315 struct btrfs_block_group_cache *cache;
2316 u64 chunk_used, user_thresh;
2317 int ret = 1;
2318
2319 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2320 chunk_used = btrfs_block_group_used(&cache->item);
2321
2322 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2323 if (chunk_used < user_thresh)
2324 ret = 0;
2325
2326 btrfs_put_block_group(cache);
2327 return ret;
2328}
2329
409d404b
ID
2330static int chunk_devid_filter(struct extent_buffer *leaf,
2331 struct btrfs_chunk *chunk,
2332 struct btrfs_balance_args *bargs)
2333{
2334 struct btrfs_stripe *stripe;
2335 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2336 int i;
2337
2338 for (i = 0; i < num_stripes; i++) {
2339 stripe = btrfs_stripe_nr(chunk, i);
2340 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2341 return 0;
2342 }
2343
2344 return 1;
2345}
2346
94e60d5a
ID
2347/* [pstart, pend) */
2348static int chunk_drange_filter(struct extent_buffer *leaf,
2349 struct btrfs_chunk *chunk,
2350 u64 chunk_offset,
2351 struct btrfs_balance_args *bargs)
2352{
2353 struct btrfs_stripe *stripe;
2354 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2355 u64 stripe_offset;
2356 u64 stripe_length;
2357 int factor;
2358 int i;
2359
2360 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2361 return 0;
2362
2363 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2364 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2365 factor = 2;
2366 else
2367 factor = 1;
2368 factor = num_stripes / factor;
2369
2370 for (i = 0; i < num_stripes; i++) {
2371 stripe = btrfs_stripe_nr(chunk, i);
2372 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2373 continue;
2374
2375 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2376 stripe_length = btrfs_chunk_length(leaf, chunk);
2377 do_div(stripe_length, factor);
2378
2379 if (stripe_offset < bargs->pend &&
2380 stripe_offset + stripe_length > bargs->pstart)
2381 return 0;
2382 }
2383
2384 return 1;
2385}
2386
ea67176a
ID
2387/* [vstart, vend) */
2388static int chunk_vrange_filter(struct extent_buffer *leaf,
2389 struct btrfs_chunk *chunk,
2390 u64 chunk_offset,
2391 struct btrfs_balance_args *bargs)
2392{
2393 if (chunk_offset < bargs->vend &&
2394 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2395 /* at least part of the chunk is inside this vrange */
2396 return 0;
2397
2398 return 1;
2399}
2400
899c81ea 2401static int chunk_soft_convert_filter(u64 chunk_type,
cfa4c961
ID
2402 struct btrfs_balance_args *bargs)
2403{
2404 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2405 return 0;
2406
899c81ea
ID
2407 chunk_type = chunk_to_extended(chunk_type) &
2408 BTRFS_EXTENDED_PROFILE_MASK;
cfa4c961 2409
899c81ea 2410 if (bargs->target == chunk_type)
cfa4c961
ID
2411 return 1;
2412
2413 return 0;
2414}
2415
f43ffb60
ID
2416static int should_balance_chunk(struct btrfs_root *root,
2417 struct extent_buffer *leaf,
2418 struct btrfs_chunk *chunk, u64 chunk_offset)
2419{
2420 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2421 struct btrfs_balance_args *bargs = NULL;
2422 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2423
2424 /* type filter */
2425 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2426 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2427 return 0;
2428 }
2429
2430 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2431 bargs = &bctl->data;
2432 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2433 bargs = &bctl->sys;
2434 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2435 bargs = &bctl->meta;
2436
ed25e9b2
ID
2437 /* profiles filter */
2438 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2439 chunk_profiles_filter(chunk_type, bargs)) {
2440 return 0;
5ce5b3c0
ID
2441 }
2442
2443 /* usage filter */
2444 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2445 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2446 return 0;
409d404b
ID
2447 }
2448
2449 /* devid filter */
2450 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2451 chunk_devid_filter(leaf, chunk, bargs)) {
2452 return 0;
94e60d5a
ID
2453 }
2454
2455 /* drange filter, makes sense only with devid filter */
2456 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2457 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2458 return 0;
ea67176a
ID
2459 }
2460
2461 /* vrange filter */
2462 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2463 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2464 return 0;
ed25e9b2
ID
2465 }
2466
cfa4c961
ID
2467 /* soft profile changing mode */
2468 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2469 chunk_soft_convert_filter(chunk_type, bargs)) {
2470 return 0;
2471 }
2472
f43ffb60
ID
2473 return 1;
2474}
2475
ec44a35c
CM
2476static u64 div_factor(u64 num, int factor)
2477{
2478 if (factor == 10)
2479 return num;
2480 num *= factor;
2481 do_div(num, 10);
2482 return num;
2483}
2484
c9e9f97b 2485static int __btrfs_balance(struct btrfs_fs_info *fs_info)
ec44a35c 2486{
19a39dce 2487 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
c9e9f97b
ID
2488 struct btrfs_root *chunk_root = fs_info->chunk_root;
2489 struct btrfs_root *dev_root = fs_info->dev_root;
2490 struct list_head *devices;
ec44a35c
CM
2491 struct btrfs_device *device;
2492 u64 old_size;
2493 u64 size_to_free;
f43ffb60 2494 struct btrfs_chunk *chunk;
ec44a35c
CM
2495 struct btrfs_path *path;
2496 struct btrfs_key key;
ec44a35c 2497 struct btrfs_key found_key;
c9e9f97b 2498 struct btrfs_trans_handle *trans;
f43ffb60
ID
2499 struct extent_buffer *leaf;
2500 int slot;
c9e9f97b
ID
2501 int ret;
2502 int enospc_errors = 0;
19a39dce 2503 bool counting = true;
ec44a35c 2504
ec44a35c 2505 /* step one make some room on all the devices */
c9e9f97b 2506 devices = &fs_info->fs_devices->devices;
c6e30871 2507 list_for_each_entry(device, devices, dev_list) {
ec44a35c
CM
2508 old_size = device->total_bytes;
2509 size_to_free = div_factor(old_size, 1);
2510 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2b82032c
YZ
2511 if (!device->writeable ||
2512 device->total_bytes - device->bytes_used > size_to_free)
ec44a35c
CM
2513 continue;
2514
2515 ret = btrfs_shrink_device(device, old_size - size_to_free);
ba1bf481
JB
2516 if (ret == -ENOSPC)
2517 break;
ec44a35c
CM
2518 BUG_ON(ret);
2519
a22285a6 2520 trans = btrfs_start_transaction(dev_root, 0);
98d5dc13 2521 BUG_ON(IS_ERR(trans));
ec44a35c
CM
2522
2523 ret = btrfs_grow_device(trans, device, old_size);
2524 BUG_ON(ret);
2525
2526 btrfs_end_transaction(trans, dev_root);
2527 }
2528
2529 /* step two, relocate all the chunks */
2530 path = btrfs_alloc_path();
17e9f796
MF
2531 if (!path) {
2532 ret = -ENOMEM;
2533 goto error;
2534 }
19a39dce
ID
2535
2536 /* zero out stat counters */
2537 spin_lock(&fs_info->balance_lock);
2538 memset(&bctl->stat, 0, sizeof(bctl->stat));
2539 spin_unlock(&fs_info->balance_lock);
2540again:
ec44a35c
CM
2541 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2542 key.offset = (u64)-1;
2543 key.type = BTRFS_CHUNK_ITEM_KEY;
2544
d397712b 2545 while (1) {
19a39dce 2546 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
a7e99c69 2547 atomic_read(&fs_info->balance_cancel_req)) {
837d5b6e
ID
2548 ret = -ECANCELED;
2549 goto error;
2550 }
2551
ec44a35c
CM
2552 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2553 if (ret < 0)
2554 goto error;
2555
2556 /*
2557 * this shouldn't happen, it means the last relocate
2558 * failed
2559 */
2560 if (ret == 0)
c9e9f97b 2561 BUG(); /* FIXME break ? */
ec44a35c
CM
2562
2563 ret = btrfs_previous_item(chunk_root, path, 0,
2564 BTRFS_CHUNK_ITEM_KEY);
c9e9f97b
ID
2565 if (ret) {
2566 ret = 0;
ec44a35c 2567 break;
c9e9f97b 2568 }
7d9eb12c 2569
f43ffb60
ID
2570 leaf = path->nodes[0];
2571 slot = path->slots[0];
2572 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7d9eb12c 2573
ec44a35c
CM
2574 if (found_key.objectid != key.objectid)
2575 break;
7d9eb12c 2576
ec44a35c 2577 /* chunk zero is special */
ba1bf481 2578 if (found_key.offset == 0)
ec44a35c
CM
2579 break;
2580
f43ffb60
ID
2581 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2582
19a39dce
ID
2583 if (!counting) {
2584 spin_lock(&fs_info->balance_lock);
2585 bctl->stat.considered++;
2586 spin_unlock(&fs_info->balance_lock);
2587 }
2588
f43ffb60
ID
2589 ret = should_balance_chunk(chunk_root, leaf, chunk,
2590 found_key.offset);
b3b4aa74 2591 btrfs_release_path(path);
f43ffb60
ID
2592 if (!ret)
2593 goto loop;
2594
19a39dce
ID
2595 if (counting) {
2596 spin_lock(&fs_info->balance_lock);
2597 bctl->stat.expected++;
2598 spin_unlock(&fs_info->balance_lock);
2599 goto loop;
2600 }
2601
ec44a35c
CM
2602 ret = btrfs_relocate_chunk(chunk_root,
2603 chunk_root->root_key.objectid,
2604 found_key.objectid,
2605 found_key.offset);
508794eb
JB
2606 if (ret && ret != -ENOSPC)
2607 goto error;
19a39dce 2608 if (ret == -ENOSPC) {
c9e9f97b 2609 enospc_errors++;
19a39dce
ID
2610 } else {
2611 spin_lock(&fs_info->balance_lock);
2612 bctl->stat.completed++;
2613 spin_unlock(&fs_info->balance_lock);
2614 }
f43ffb60 2615loop:
ba1bf481 2616 key.offset = found_key.offset - 1;
ec44a35c 2617 }
c9e9f97b 2618
19a39dce
ID
2619 if (counting) {
2620 btrfs_release_path(path);
2621 counting = false;
2622 goto again;
2623 }
ec44a35c
CM
2624error:
2625 btrfs_free_path(path);
c9e9f97b
ID
2626 if (enospc_errors) {
2627 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2628 enospc_errors);
2629 if (!ret)
2630 ret = -ENOSPC;
2631 }
2632
ec44a35c
CM
2633 return ret;
2634}
2635
0c460c0d
ID
2636/**
2637 * alloc_profile_is_valid - see if a given profile is valid and reduced
2638 * @flags: profile to validate
2639 * @extended: if true @flags is treated as an extended profile
2640 */
2641static int alloc_profile_is_valid(u64 flags, int extended)
2642{
2643 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2644 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2645
2646 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2647
2648 /* 1) check that all other bits are zeroed */
2649 if (flags & ~mask)
2650 return 0;
2651
2652 /* 2) see if profile is reduced */
2653 if (flags == 0)
2654 return !extended; /* "0" is valid for usual profiles */
2655
2656 /* true if exactly one bit set */
2657 return (flags & (flags - 1)) == 0;
2658}
2659
837d5b6e
ID
2660static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2661{
a7e99c69
ID
2662 /* cancel requested || normal exit path */
2663 return atomic_read(&fs_info->balance_cancel_req) ||
2664 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2665 atomic_read(&fs_info->balance_cancel_req) == 0);
837d5b6e
ID
2666}
2667
c9e9f97b
ID
2668static void __cancel_balance(struct btrfs_fs_info *fs_info)
2669{
0940ebf6
ID
2670 int ret;
2671
c9e9f97b 2672 unset_balance_control(fs_info);
0940ebf6
ID
2673 ret = del_balance_item(fs_info->tree_root);
2674 BUG_ON(ret);
c9e9f97b
ID
2675}
2676
19a39dce 2677void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
c9e9f97b
ID
2678 struct btrfs_ioctl_balance_args *bargs);
2679
2680/*
2681 * Should be called with both balance and volume mutexes held
2682 */
2683int btrfs_balance(struct btrfs_balance_control *bctl,
2684 struct btrfs_ioctl_balance_args *bargs)
2685{
2686 struct btrfs_fs_info *fs_info = bctl->fs_info;
f43ffb60 2687 u64 allowed;
e4837f8f 2688 int mixed = 0;
c9e9f97b
ID
2689 int ret;
2690
837d5b6e 2691 if (btrfs_fs_closing(fs_info) ||
a7e99c69
ID
2692 atomic_read(&fs_info->balance_pause_req) ||
2693 atomic_read(&fs_info->balance_cancel_req)) {
c9e9f97b
ID
2694 ret = -EINVAL;
2695 goto out;
2696 }
2697
e4837f8f
ID
2698 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2699 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2700 mixed = 1;
2701
f43ffb60
ID
2702 /*
2703 * In case of mixed groups both data and meta should be picked,
2704 * and identical options should be given for both of them.
2705 */
e4837f8f
ID
2706 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2707 if (mixed && (bctl->flags & allowed)) {
f43ffb60
ID
2708 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2709 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2710 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2711 printk(KERN_ERR "btrfs: with mixed groups data and "
2712 "metadata balance options must be the same\n");
2713 ret = -EINVAL;
2714 goto out;
2715 }
2716 }
2717
e4d8ec0f
ID
2718 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2719 if (fs_info->fs_devices->num_devices == 1)
2720 allowed |= BTRFS_BLOCK_GROUP_DUP;
2721 else if (fs_info->fs_devices->num_devices < 4)
2722 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2723 else
2724 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2725 BTRFS_BLOCK_GROUP_RAID10);
2726
6728b198
ID
2727 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2728 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2729 (bctl->data.target & ~allowed))) {
e4d8ec0f
ID
2730 printk(KERN_ERR "btrfs: unable to start balance with target "
2731 "data profile %llu\n",
2732 (unsigned long long)bctl->data.target);
2733 ret = -EINVAL;
2734 goto out;
2735 }
6728b198
ID
2736 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2737 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2738 (bctl->meta.target & ~allowed))) {
e4d8ec0f
ID
2739 printk(KERN_ERR "btrfs: unable to start balance with target "
2740 "metadata profile %llu\n",
2741 (unsigned long long)bctl->meta.target);
2742 ret = -EINVAL;
2743 goto out;
2744 }
6728b198
ID
2745 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2746 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2747 (bctl->sys.target & ~allowed))) {
e4d8ec0f
ID
2748 printk(KERN_ERR "btrfs: unable to start balance with target "
2749 "system profile %llu\n",
2750 (unsigned long long)bctl->sys.target);
2751 ret = -EINVAL;
2752 goto out;
2753 }
2754
e4837f8f
ID
2755 /* allow dup'ed data chunks only in mixed mode */
2756 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
6728b198 2757 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
e4d8ec0f
ID
2758 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2759 ret = -EINVAL;
2760 goto out;
2761 }
2762
2763 /* allow to reduce meta or sys integrity only if force set */
2764 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2765 BTRFS_BLOCK_GROUP_RAID10;
2766 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2767 (fs_info->avail_system_alloc_bits & allowed) &&
2768 !(bctl->sys.target & allowed)) ||
2769 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2770 (fs_info->avail_metadata_alloc_bits & allowed) &&
2771 !(bctl->meta.target & allowed))) {
2772 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2773 printk(KERN_INFO "btrfs: force reducing metadata "
2774 "integrity\n");
2775 } else {
2776 printk(KERN_ERR "btrfs: balance will reduce metadata "
2777 "integrity, use force if you want this\n");
2778 ret = -EINVAL;
2779 goto out;
2780 }
2781 }
2782
0940ebf6 2783 ret = insert_balance_item(fs_info->tree_root, bctl);
59641015 2784 if (ret && ret != -EEXIST)
0940ebf6
ID
2785 goto out;
2786
59641015
ID
2787 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2788 BUG_ON(ret == -EEXIST);
2789 set_balance_control(bctl);
2790 } else {
2791 BUG_ON(ret != -EEXIST);
2792 spin_lock(&fs_info->balance_lock);
2793 update_balance_args(bctl);
2794 spin_unlock(&fs_info->balance_lock);
2795 }
c9e9f97b 2796
837d5b6e 2797 atomic_inc(&fs_info->balance_running);
c9e9f97b
ID
2798 mutex_unlock(&fs_info->balance_mutex);
2799
2800 ret = __btrfs_balance(fs_info);
2801
2802 mutex_lock(&fs_info->balance_mutex);
837d5b6e 2803 atomic_dec(&fs_info->balance_running);
c9e9f97b
ID
2804
2805 if (bargs) {
2806 memset(bargs, 0, sizeof(*bargs));
19a39dce 2807 update_ioctl_balance_args(fs_info, 0, bargs);
c9e9f97b
ID
2808 }
2809
837d5b6e
ID
2810 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2811 balance_need_close(fs_info)) {
2812 __cancel_balance(fs_info);
2813 }
2814
2815 wake_up(&fs_info->balance_wait_q);
c9e9f97b
ID
2816
2817 return ret;
2818out:
59641015
ID
2819 if (bctl->flags & BTRFS_BALANCE_RESUME)
2820 __cancel_balance(fs_info);
2821 else
2822 kfree(bctl);
2823 return ret;
2824}
2825
2826static int balance_kthread(void *data)
2827{
2828 struct btrfs_balance_control *bctl =
2829 (struct btrfs_balance_control *)data;
2830 struct btrfs_fs_info *fs_info = bctl->fs_info;
9555c6c1 2831 int ret = 0;
59641015
ID
2832
2833 mutex_lock(&fs_info->volume_mutex);
2834 mutex_lock(&fs_info->balance_mutex);
2835
2836 set_balance_control(bctl);
2837
9555c6c1
ID
2838 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2839 printk(KERN_INFO "btrfs: force skipping balance\n");
2840 } else {
2841 printk(KERN_INFO "btrfs: continuing balance\n");
2842 ret = btrfs_balance(bctl, NULL);
2843 }
59641015
ID
2844
2845 mutex_unlock(&fs_info->balance_mutex);
2846 mutex_unlock(&fs_info->volume_mutex);
2847 return ret;
2848}
2849
2850int btrfs_recover_balance(struct btrfs_root *tree_root)
2851{
2852 struct task_struct *tsk;
2853 struct btrfs_balance_control *bctl;
2854 struct btrfs_balance_item *item;
2855 struct btrfs_disk_balance_args disk_bargs;
2856 struct btrfs_path *path;
2857 struct extent_buffer *leaf;
2858 struct btrfs_key key;
2859 int ret;
2860
2861 path = btrfs_alloc_path();
2862 if (!path)
2863 return -ENOMEM;
2864
2865 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2866 if (!bctl) {
2867 ret = -ENOMEM;
2868 goto out;
2869 }
2870
2871 key.objectid = BTRFS_BALANCE_OBJECTID;
2872 key.type = BTRFS_BALANCE_ITEM_KEY;
2873 key.offset = 0;
2874
2875 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2876 if (ret < 0)
2877 goto out_bctl;
2878 if (ret > 0) { /* ret = -ENOENT; */
2879 ret = 0;
2880 goto out_bctl;
2881 }
2882
2883 leaf = path->nodes[0];
2884 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2885
2886 bctl->fs_info = tree_root->fs_info;
2887 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2888
2889 btrfs_balance_data(leaf, item, &disk_bargs);
2890 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2891 btrfs_balance_meta(leaf, item, &disk_bargs);
2892 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2893 btrfs_balance_sys(leaf, item, &disk_bargs);
2894 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2895
2896 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2897 if (IS_ERR(tsk))
2898 ret = PTR_ERR(tsk);
2899 else
2900 goto out;
2901
2902out_bctl:
c9e9f97b 2903 kfree(bctl);
59641015
ID
2904out:
2905 btrfs_free_path(path);
ec44a35c
CM
2906 return ret;
2907}
2908
837d5b6e
ID
2909int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2910{
2911 int ret = 0;
2912
2913 mutex_lock(&fs_info->balance_mutex);
2914 if (!fs_info->balance_ctl) {
2915 mutex_unlock(&fs_info->balance_mutex);
2916 return -ENOTCONN;
2917 }
2918
2919 if (atomic_read(&fs_info->balance_running)) {
2920 atomic_inc(&fs_info->balance_pause_req);
2921 mutex_unlock(&fs_info->balance_mutex);
2922
2923 wait_event(fs_info->balance_wait_q,
2924 atomic_read(&fs_info->balance_running) == 0);
2925
2926 mutex_lock(&fs_info->balance_mutex);
2927 /* we are good with balance_ctl ripped off from under us */
2928 BUG_ON(atomic_read(&fs_info->balance_running));
2929 atomic_dec(&fs_info->balance_pause_req);
2930 } else {
2931 ret = -ENOTCONN;
2932 }
2933
2934 mutex_unlock(&fs_info->balance_mutex);
2935 return ret;
2936}
2937
a7e99c69
ID
2938int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2939{
2940 mutex_lock(&fs_info->balance_mutex);
2941 if (!fs_info->balance_ctl) {
2942 mutex_unlock(&fs_info->balance_mutex);
2943 return -ENOTCONN;
2944 }
2945
2946 atomic_inc(&fs_info->balance_cancel_req);
2947 /*
2948 * if we are running just wait and return, balance item is
2949 * deleted in btrfs_balance in this case
2950 */
2951 if (atomic_read(&fs_info->balance_running)) {
2952 mutex_unlock(&fs_info->balance_mutex);
2953 wait_event(fs_info->balance_wait_q,
2954 atomic_read(&fs_info->balance_running) == 0);
2955 mutex_lock(&fs_info->balance_mutex);
2956 } else {
2957 /* __cancel_balance needs volume_mutex */
2958 mutex_unlock(&fs_info->balance_mutex);
2959 mutex_lock(&fs_info->volume_mutex);
2960 mutex_lock(&fs_info->balance_mutex);
2961
2962 if (fs_info->balance_ctl)
2963 __cancel_balance(fs_info);
2964
2965 mutex_unlock(&fs_info->volume_mutex);
2966 }
2967
2968 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2969 atomic_dec(&fs_info->balance_cancel_req);
2970 mutex_unlock(&fs_info->balance_mutex);
2971 return 0;
2972}
2973
8f18cf13
CM
2974/*
2975 * shrinking a device means finding all of the device extents past
2976 * the new size, and then following the back refs to the chunks.
2977 * The chunk relocation code actually frees the device extent
2978 */
2979int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2980{
2981 struct btrfs_trans_handle *trans;
2982 struct btrfs_root *root = device->dev_root;
2983 struct btrfs_dev_extent *dev_extent = NULL;
2984 struct btrfs_path *path;
2985 u64 length;
2986 u64 chunk_tree;
2987 u64 chunk_objectid;
2988 u64 chunk_offset;
2989 int ret;
2990 int slot;
ba1bf481
JB
2991 int failed = 0;
2992 bool retried = false;
8f18cf13
CM
2993 struct extent_buffer *l;
2994 struct btrfs_key key;
6c41761f 2995 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
8f18cf13 2996 u64 old_total = btrfs_super_total_bytes(super_copy);
ba1bf481 2997 u64 old_size = device->total_bytes;
8f18cf13
CM
2998 u64 diff = device->total_bytes - new_size;
2999
2b82032c
YZ
3000 if (new_size >= device->total_bytes)
3001 return -EINVAL;
8f18cf13
CM
3002
3003 path = btrfs_alloc_path();
3004 if (!path)
3005 return -ENOMEM;
3006
8f18cf13
CM
3007 path->reada = 2;
3008
7d9eb12c
CM
3009 lock_chunks(root);
3010
8f18cf13 3011 device->total_bytes = new_size;
2bf64758 3012 if (device->writeable) {
2b82032c 3013 device->fs_devices->total_rw_bytes -= diff;
2bf64758
JB
3014 spin_lock(&root->fs_info->free_chunk_lock);
3015 root->fs_info->free_chunk_space -= diff;
3016 spin_unlock(&root->fs_info->free_chunk_lock);
3017 }
7d9eb12c 3018 unlock_chunks(root);
8f18cf13 3019
ba1bf481 3020again:
8f18cf13
CM
3021 key.objectid = device->devid;
3022 key.offset = (u64)-1;
3023 key.type = BTRFS_DEV_EXTENT_KEY;
3024
213e64da 3025 do {
8f18cf13
CM
3026 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3027 if (ret < 0)
3028 goto done;
3029
3030 ret = btrfs_previous_item(root, path, 0, key.type);
3031 if (ret < 0)
3032 goto done;
3033 if (ret) {
3034 ret = 0;
b3b4aa74 3035 btrfs_release_path(path);
bf1fb512 3036 break;
8f18cf13
CM
3037 }
3038
3039 l = path->nodes[0];
3040 slot = path->slots[0];
3041 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3042
ba1bf481 3043 if (key.objectid != device->devid) {
b3b4aa74 3044 btrfs_release_path(path);
bf1fb512 3045 break;
ba1bf481 3046 }
8f18cf13
CM
3047
3048 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3049 length = btrfs_dev_extent_length(l, dev_extent);
3050
ba1bf481 3051 if (key.offset + length <= new_size) {
b3b4aa74 3052 btrfs_release_path(path);
d6397bae 3053 break;
ba1bf481 3054 }
8f18cf13
CM
3055
3056 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3057 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3058 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
b3b4aa74 3059 btrfs_release_path(path);
8f18cf13
CM
3060
3061 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3062 chunk_offset);
ba1bf481 3063 if (ret && ret != -ENOSPC)
8f18cf13 3064 goto done;
ba1bf481
JB
3065 if (ret == -ENOSPC)
3066 failed++;
213e64da 3067 } while (key.offset-- > 0);
ba1bf481
JB
3068
3069 if (failed && !retried) {
3070 failed = 0;
3071 retried = true;
3072 goto again;
3073 } else if (failed && retried) {
3074 ret = -ENOSPC;
3075 lock_chunks(root);
3076
3077 device->total_bytes = old_size;
3078 if (device->writeable)
3079 device->fs_devices->total_rw_bytes += diff;
2bf64758
JB
3080 spin_lock(&root->fs_info->free_chunk_lock);
3081 root->fs_info->free_chunk_space += diff;
3082 spin_unlock(&root->fs_info->free_chunk_lock);
ba1bf481
JB
3083 unlock_chunks(root);
3084 goto done;
8f18cf13
CM
3085 }
3086
d6397bae 3087 /* Shrinking succeeded, else we would be at "done". */
a22285a6 3088 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
3089 if (IS_ERR(trans)) {
3090 ret = PTR_ERR(trans);
3091 goto done;
3092 }
3093
d6397bae
CB
3094 lock_chunks(root);
3095
3096 device->disk_total_bytes = new_size;
3097 /* Now btrfs_update_device() will change the on-disk size. */
3098 ret = btrfs_update_device(trans, device);
3099 if (ret) {
3100 unlock_chunks(root);
3101 btrfs_end_transaction(trans, root);
3102 goto done;
3103 }
3104 WARN_ON(diff > old_total);
3105 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3106 unlock_chunks(root);
3107 btrfs_end_transaction(trans, root);
8f18cf13
CM
3108done:
3109 btrfs_free_path(path);
3110 return ret;
3111}
3112
125ccb0a 3113static int btrfs_add_system_chunk(struct btrfs_root *root,
0b86a832
CM
3114 struct btrfs_key *key,
3115 struct btrfs_chunk *chunk, int item_size)
3116{
6c41761f 3117 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
0b86a832
CM
3118 struct btrfs_disk_key disk_key;
3119 u32 array_size;
3120 u8 *ptr;
3121
3122 array_size = btrfs_super_sys_array_size(super_copy);
3123 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3124 return -EFBIG;
3125
3126 ptr = super_copy->sys_chunk_array + array_size;
3127 btrfs_cpu_key_to_disk(&disk_key, key);
3128 memcpy(ptr, &disk_key, sizeof(disk_key));
3129 ptr += sizeof(disk_key);
3130 memcpy(ptr, chunk, item_size);
3131 item_size += sizeof(disk_key);
3132 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3133 return 0;
3134}
3135
73c5de00
AJ
3136/*
3137 * sort the devices in descending order by max_avail, total_avail
3138 */
3139static int btrfs_cmp_device_info(const void *a, const void *b)
9b3f68b9 3140{
73c5de00
AJ
3141 const struct btrfs_device_info *di_a = a;
3142 const struct btrfs_device_info *di_b = b;
9b3f68b9 3143
73c5de00 3144 if (di_a->max_avail > di_b->max_avail)
b2117a39 3145 return -1;
73c5de00 3146 if (di_a->max_avail < di_b->max_avail)
b2117a39 3147 return 1;
73c5de00
AJ
3148 if (di_a->total_avail > di_b->total_avail)
3149 return -1;
3150 if (di_a->total_avail < di_b->total_avail)
3151 return 1;
3152 return 0;
b2117a39 3153}
0b86a832 3154
73c5de00
AJ
3155static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3156 struct btrfs_root *extent_root,
3157 struct map_lookup **map_ret,
3158 u64 *num_bytes_out, u64 *stripe_size_out,
3159 u64 start, u64 type)
b2117a39 3160{
73c5de00
AJ
3161 struct btrfs_fs_info *info = extent_root->fs_info;
3162 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3163 struct list_head *cur;
3164 struct map_lookup *map = NULL;
3165 struct extent_map_tree *em_tree;
3166 struct extent_map *em;
3167 struct btrfs_device_info *devices_info = NULL;
3168 u64 total_avail;
3169 int num_stripes; /* total number of stripes to allocate */
3170 int sub_stripes; /* sub_stripes info for map */
3171 int dev_stripes; /* stripes per dev */
3172 int devs_max; /* max devs to use */
3173 int devs_min; /* min devs needed */
3174 int devs_increment; /* ndevs has to be a multiple of this */
3175 int ncopies; /* how many copies to data has */
3176 int ret;
3177 u64 max_stripe_size;
3178 u64 max_chunk_size;
3179 u64 stripe_size;
3180 u64 num_bytes;
3181 int ndevs;
3182 int i;
3183 int j;
593060d7 3184
0c460c0d 3185 BUG_ON(!alloc_profile_is_valid(type, 0));
9b3f68b9 3186
73c5de00
AJ
3187 if (list_empty(&fs_devices->alloc_list))
3188 return -ENOSPC;
b2117a39 3189
73c5de00
AJ
3190 sub_stripes = 1;
3191 dev_stripes = 1;
3192 devs_increment = 1;
3193 ncopies = 1;
3194 devs_max = 0; /* 0 == as many as possible */
3195 devs_min = 1;
3196
3197 /*
3198 * define the properties of each RAID type.
3199 * FIXME: move this to a global table and use it in all RAID
3200 * calculation code
3201 */
3202 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3203 dev_stripes = 2;
b2117a39 3204 ncopies = 2;
73c5de00
AJ
3205 devs_max = 1;
3206 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3207 devs_min = 2;
3208 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3209 devs_increment = 2;
b2117a39 3210 ncopies = 2;
73c5de00
AJ
3211 devs_max = 2;
3212 devs_min = 2;
3213 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3214 sub_stripes = 2;
3215 devs_increment = 2;
3216 ncopies = 2;
3217 devs_min = 4;
3218 } else {
3219 devs_max = 1;
3220 }
b2117a39 3221
9b3f68b9 3222 if (type & BTRFS_BLOCK_GROUP_DATA) {
73c5de00
AJ
3223 max_stripe_size = 1024 * 1024 * 1024;
3224 max_chunk_size = 10 * max_stripe_size;
9b3f68b9 3225 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1100373f
CM
3226 /* for larger filesystems, use larger metadata chunks */
3227 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3228 max_stripe_size = 1024 * 1024 * 1024;
3229 else
3230 max_stripe_size = 256 * 1024 * 1024;
73c5de00 3231 max_chunk_size = max_stripe_size;
a40a90a0 3232 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
96bdc7dc 3233 max_stripe_size = 32 * 1024 * 1024;
73c5de00
AJ
3234 max_chunk_size = 2 * max_stripe_size;
3235 } else {
3236 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3237 type);
3238 BUG_ON(1);
9b3f68b9
CM
3239 }
3240
2b82032c
YZ
3241 /* we don't want a chunk larger than 10% of writeable space */
3242 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3243 max_chunk_size);
9b3f68b9 3244
73c5de00
AJ
3245 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3246 GFP_NOFS);
3247 if (!devices_info)
3248 return -ENOMEM;
0cad8a11 3249
73c5de00 3250 cur = fs_devices->alloc_list.next;
9b3f68b9 3251
9f680ce0 3252 /*
73c5de00
AJ
3253 * in the first pass through the devices list, we gather information
3254 * about the available holes on each device.
9f680ce0 3255 */
73c5de00
AJ
3256 ndevs = 0;
3257 while (cur != &fs_devices->alloc_list) {
3258 struct btrfs_device *device;
3259 u64 max_avail;
3260 u64 dev_offset;
b2117a39 3261
73c5de00 3262 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
9f680ce0 3263
73c5de00 3264 cur = cur->next;
b2117a39 3265
73c5de00
AJ
3266 if (!device->writeable) {
3267 printk(KERN_ERR
3268 "btrfs: read-only device in alloc_list\n");
3269 WARN_ON(1);
3270 continue;
3271 }
b2117a39 3272
73c5de00
AJ
3273 if (!device->in_fs_metadata)
3274 continue;
b2117a39 3275
73c5de00
AJ
3276 if (device->total_bytes > device->bytes_used)
3277 total_avail = device->total_bytes - device->bytes_used;
3278 else
3279 total_avail = 0;
38c01b96 3280
3281 /* If there is no space on this device, skip it. */
3282 if (total_avail == 0)
3283 continue;
b2117a39 3284
125ccb0a 3285 ret = find_free_dev_extent(device,
73c5de00
AJ
3286 max_stripe_size * dev_stripes,
3287 &dev_offset, &max_avail);
3288 if (ret && ret != -ENOSPC)
3289 goto error;
b2117a39 3290
73c5de00
AJ
3291 if (ret == 0)
3292 max_avail = max_stripe_size * dev_stripes;
b2117a39 3293
73c5de00
AJ
3294 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3295 continue;
b2117a39 3296
73c5de00
AJ
3297 devices_info[ndevs].dev_offset = dev_offset;
3298 devices_info[ndevs].max_avail = max_avail;
3299 devices_info[ndevs].total_avail = total_avail;
3300 devices_info[ndevs].dev = device;
3301 ++ndevs;
3302 }
b2117a39 3303
73c5de00
AJ
3304 /*
3305 * now sort the devices by hole size / available space
3306 */
3307 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3308 btrfs_cmp_device_info, NULL);
b2117a39 3309
73c5de00
AJ
3310 /* round down to number of usable stripes */
3311 ndevs -= ndevs % devs_increment;
b2117a39 3312
73c5de00
AJ
3313 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3314 ret = -ENOSPC;
3315 goto error;
b2117a39 3316 }
9f680ce0 3317
73c5de00
AJ
3318 if (devs_max && ndevs > devs_max)
3319 ndevs = devs_max;
3320 /*
3321 * the primary goal is to maximize the number of stripes, so use as many
3322 * devices as possible, even if the stripes are not maximum sized.
3323 */
3324 stripe_size = devices_info[ndevs-1].max_avail;
3325 num_stripes = ndevs * dev_stripes;
b2117a39 3326
37db63a4 3327 if (stripe_size * ndevs > max_chunk_size * ncopies) {
73c5de00 3328 stripe_size = max_chunk_size * ncopies;
37db63a4 3329 do_div(stripe_size, ndevs);
b2117a39 3330 }
b2117a39 3331
73c5de00 3332 do_div(stripe_size, dev_stripes);
37db63a4
ID
3333
3334 /* align to BTRFS_STRIPE_LEN */
73c5de00
AJ
3335 do_div(stripe_size, BTRFS_STRIPE_LEN);
3336 stripe_size *= BTRFS_STRIPE_LEN;
b2117a39
MX
3337
3338 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3339 if (!map) {
3340 ret = -ENOMEM;
3341 goto error;
3342 }
3343 map->num_stripes = num_stripes;
9b3f68b9 3344
73c5de00
AJ
3345 for (i = 0; i < ndevs; ++i) {
3346 for (j = 0; j < dev_stripes; ++j) {
3347 int s = i * dev_stripes + j;
3348 map->stripes[s].dev = devices_info[i].dev;
3349 map->stripes[s].physical = devices_info[i].dev_offset +
3350 j * stripe_size;
6324fbf3 3351 }
6324fbf3 3352 }
2b82032c 3353 map->sector_size = extent_root->sectorsize;
b2117a39
MX
3354 map->stripe_len = BTRFS_STRIPE_LEN;
3355 map->io_align = BTRFS_STRIPE_LEN;
3356 map->io_width = BTRFS_STRIPE_LEN;
2b82032c 3357 map->type = type;
2b82032c 3358 map->sub_stripes = sub_stripes;
0b86a832 3359
2b82032c 3360 *map_ret = map;
73c5de00 3361 num_bytes = stripe_size * (num_stripes / ncopies);
0b86a832 3362
73c5de00
AJ
3363 *stripe_size_out = stripe_size;
3364 *num_bytes_out = num_bytes;
0b86a832 3365
73c5de00 3366 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
1abe9b8a 3367
172ddd60 3368 em = alloc_extent_map();
2b82032c 3369 if (!em) {
b2117a39
MX
3370 ret = -ENOMEM;
3371 goto error;
593060d7 3372 }
2b82032c
YZ
3373 em->bdev = (struct block_device *)map;
3374 em->start = start;
73c5de00 3375 em->len = num_bytes;
2b82032c
YZ
3376 em->block_start = 0;
3377 em->block_len = em->len;
593060d7 3378
2b82032c 3379 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
890871be 3380 write_lock(&em_tree->lock);
2b82032c 3381 ret = add_extent_mapping(em_tree, em);
890871be 3382 write_unlock(&em_tree->lock);
2b82032c 3383 free_extent_map(em);
1dd4602f
MF
3384 if (ret)
3385 goto error;
0b86a832 3386
2b82032c
YZ
3387 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3388 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
73c5de00 3389 start, num_bytes);
79787eaa
JM
3390 if (ret)
3391 goto error;
611f0e00 3392
73c5de00
AJ
3393 for (i = 0; i < map->num_stripes; ++i) {
3394 struct btrfs_device *device;
3395 u64 dev_offset;
3396
3397 device = map->stripes[i].dev;
3398 dev_offset = map->stripes[i].physical;
0b86a832
CM
3399
3400 ret = btrfs_alloc_dev_extent(trans, device,
2b82032c
YZ
3401 info->chunk_root->root_key.objectid,
3402 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
73c5de00 3403 start, dev_offset, stripe_size);
79787eaa
JM
3404 if (ret) {
3405 btrfs_abort_transaction(trans, extent_root, ret);
3406 goto error;
3407 }
2b82032c
YZ
3408 }
3409
b2117a39 3410 kfree(devices_info);
2b82032c 3411 return 0;
b2117a39
MX
3412
3413error:
3414 kfree(map);
3415 kfree(devices_info);
3416 return ret;
2b82032c
YZ
3417}
3418
3419static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3420 struct btrfs_root *extent_root,
3421 struct map_lookup *map, u64 chunk_offset,
3422 u64 chunk_size, u64 stripe_size)
3423{
3424 u64 dev_offset;
3425 struct btrfs_key key;
3426 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3427 struct btrfs_device *device;
3428 struct btrfs_chunk *chunk;
3429 struct btrfs_stripe *stripe;
3430 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3431 int index = 0;
3432 int ret;
3433
3434 chunk = kzalloc(item_size, GFP_NOFS);
3435 if (!chunk)
3436 return -ENOMEM;
3437
3438 index = 0;
3439 while (index < map->num_stripes) {
3440 device = map->stripes[index].dev;
3441 device->bytes_used += stripe_size;
0b86a832 3442 ret = btrfs_update_device(trans, device);
3acd3953
MF
3443 if (ret)
3444 goto out_free;
2b82032c
YZ
3445 index++;
3446 }
3447
2bf64758
JB
3448 spin_lock(&extent_root->fs_info->free_chunk_lock);
3449 extent_root->fs_info->free_chunk_space -= (stripe_size *
3450 map->num_stripes);
3451 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3452
2b82032c
YZ
3453 index = 0;
3454 stripe = &chunk->stripe;
3455 while (index < map->num_stripes) {
3456 device = map->stripes[index].dev;
3457 dev_offset = map->stripes[index].physical;
0b86a832 3458
e17cade2
CM
3459 btrfs_set_stack_stripe_devid(stripe, device->devid);
3460 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3461 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2b82032c 3462 stripe++;
0b86a832
CM
3463 index++;
3464 }
3465
2b82032c 3466 btrfs_set_stack_chunk_length(chunk, chunk_size);
0b86a832 3467 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2b82032c
YZ
3468 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3469 btrfs_set_stack_chunk_type(chunk, map->type);
3470 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3471 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3472 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
0b86a832 3473 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2b82032c 3474 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
0b86a832 3475
2b82032c
YZ
3476 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3477 key.type = BTRFS_CHUNK_ITEM_KEY;
3478 key.offset = chunk_offset;
0b86a832 3479
2b82032c 3480 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
0b86a832 3481
4ed1d16e
MF
3482 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3483 /*
3484 * TODO: Cleanup of inserted chunk root in case of
3485 * failure.
3486 */
125ccb0a 3487 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
2b82032c 3488 item_size);
8f18cf13 3489 }
1abe9b8a 3490
3acd3953 3491out_free:
0b86a832 3492 kfree(chunk);
4ed1d16e 3493 return ret;
2b82032c 3494}
0b86a832 3495
2b82032c
YZ
3496/*
3497 * Chunk allocation falls into two parts. The first part does works
3498 * that make the new allocated chunk useable, but not do any operation
3499 * that modifies the chunk tree. The second part does the works that
3500 * require modifying the chunk tree. This division is important for the
3501 * bootstrap process of adding storage to a seed btrfs.
3502 */
3503int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3504 struct btrfs_root *extent_root, u64 type)
3505{
3506 u64 chunk_offset;
3507 u64 chunk_size;
3508 u64 stripe_size;
3509 struct map_lookup *map;
3510 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3511 int ret;
3512
3513 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3514 &chunk_offset);
3515 if (ret)
3516 return ret;
3517
3518 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3519 &stripe_size, chunk_offset, type);
3520 if (ret)
3521 return ret;
3522
3523 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3524 chunk_size, stripe_size);
79787eaa
JM
3525 if (ret)
3526 return ret;
2b82032c
YZ
3527 return 0;
3528}
3529
d397712b 3530static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2b82032c
YZ
3531 struct btrfs_root *root,
3532 struct btrfs_device *device)
3533{
3534 u64 chunk_offset;
3535 u64 sys_chunk_offset;
3536 u64 chunk_size;
3537 u64 sys_chunk_size;
3538 u64 stripe_size;
3539 u64 sys_stripe_size;
3540 u64 alloc_profile;
3541 struct map_lookup *map;
3542 struct map_lookup *sys_map;
3543 struct btrfs_fs_info *fs_info = root->fs_info;
3544 struct btrfs_root *extent_root = fs_info->extent_root;
3545 int ret;
3546
3547 ret = find_next_chunk(fs_info->chunk_root,
3548 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
92b8e897
MF
3549 if (ret)
3550 return ret;
2b82032c
YZ
3551
3552 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
6fef8df1 3553 fs_info->avail_metadata_alloc_bits;
2b82032c
YZ
3554 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3555
3556 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3557 &stripe_size, chunk_offset, alloc_profile);
79787eaa
JM
3558 if (ret)
3559 return ret;
2b82032c
YZ
3560
3561 sys_chunk_offset = chunk_offset + chunk_size;
3562
3563 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
6fef8df1 3564 fs_info->avail_system_alloc_bits;
2b82032c
YZ
3565 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3566
3567 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3568 &sys_chunk_size, &sys_stripe_size,
3569 sys_chunk_offset, alloc_profile);
79787eaa
JM
3570 if (ret)
3571 goto abort;
2b82032c
YZ
3572
3573 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
79787eaa
JM
3574 if (ret)
3575 goto abort;
2b82032c
YZ
3576
3577 /*
3578 * Modifying chunk tree needs allocating new blocks from both
3579 * system block group and metadata block group. So we only can
3580 * do operations require modifying the chunk tree after both
3581 * block groups were created.
3582 */
3583 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3584 chunk_size, stripe_size);
79787eaa
JM
3585 if (ret)
3586 goto abort;
2b82032c
YZ
3587
3588 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3589 sys_chunk_offset, sys_chunk_size,
3590 sys_stripe_size);
79787eaa
JM
3591 if (ret)
3592 goto abort;
3593
2b82032c 3594 return 0;
79787eaa
JM
3595
3596abort:
3597 btrfs_abort_transaction(trans, root, ret);
3598 return ret;
2b82032c
YZ
3599}
3600
3601int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3602{
3603 struct extent_map *em;
3604 struct map_lookup *map;
3605 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3606 int readonly = 0;
3607 int i;
3608
890871be 3609 read_lock(&map_tree->map_tree.lock);
2b82032c 3610 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
890871be 3611 read_unlock(&map_tree->map_tree.lock);
2b82032c
YZ
3612 if (!em)
3613 return 1;
3614
f48b9075
JB
3615 if (btrfs_test_opt(root, DEGRADED)) {
3616 free_extent_map(em);
3617 return 0;
3618 }
3619
2b82032c
YZ
3620 map = (struct map_lookup *)em->bdev;
3621 for (i = 0; i < map->num_stripes; i++) {
3622 if (!map->stripes[i].dev->writeable) {
3623 readonly = 1;
3624 break;
3625 }
3626 }
0b86a832 3627 free_extent_map(em);
2b82032c 3628 return readonly;
0b86a832
CM
3629}
3630
3631void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3632{
a8067e02 3633 extent_map_tree_init(&tree->map_tree);
0b86a832
CM
3634}
3635
3636void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3637{
3638 struct extent_map *em;
3639
d397712b 3640 while (1) {
890871be 3641 write_lock(&tree->map_tree.lock);
0b86a832
CM
3642 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3643 if (em)
3644 remove_extent_mapping(&tree->map_tree, em);
890871be 3645 write_unlock(&tree->map_tree.lock);
0b86a832
CM
3646 if (!em)
3647 break;
3648 kfree(em->bdev);
3649 /* once for us */
3650 free_extent_map(em);
3651 /* once for the tree */
3652 free_extent_map(em);
3653 }
3654}
3655
f188591e
CM
3656int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3657{
3658 struct extent_map *em;
3659 struct map_lookup *map;
3660 struct extent_map_tree *em_tree = &map_tree->map_tree;
3661 int ret;
3662
890871be 3663 read_lock(&em_tree->lock);
f188591e 3664 em = lookup_extent_mapping(em_tree, logical, len);
890871be 3665 read_unlock(&em_tree->lock);
f188591e
CM
3666 BUG_ON(!em);
3667
3668 BUG_ON(em->start > logical || em->start + em->len < logical);
3669 map = (struct map_lookup *)em->bdev;
3670 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3671 ret = map->num_stripes;
321aecc6
CM
3672 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3673 ret = map->sub_stripes;
f188591e
CM
3674 else
3675 ret = 1;
3676 free_extent_map(em);
f188591e
CM
3677 return ret;
3678}
3679
dfe25020
CM
3680static int find_live_mirror(struct map_lookup *map, int first, int num,
3681 int optimal)
3682{
3683 int i;
3684 if (map->stripes[optimal].dev->bdev)
3685 return optimal;
3686 for (i = first; i < first + num; i++) {
3687 if (map->stripes[i].dev->bdev)
3688 return i;
3689 }
3690 /* we couldn't find one that doesn't fail. Just return something
3691 * and the io error handling code will clean up eventually
3692 */
3693 return optimal;
3694}
3695
f2d8d74d
CM
3696static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3697 u64 logical, u64 *length,
a1d3c478 3698 struct btrfs_bio **bbio_ret,
7eaceacc 3699 int mirror_num)
0b86a832
CM
3700{
3701 struct extent_map *em;
3702 struct map_lookup *map;
3703 struct extent_map_tree *em_tree = &map_tree->map_tree;
3704 u64 offset;
593060d7 3705 u64 stripe_offset;
fce3bb9a 3706 u64 stripe_end_offset;
593060d7 3707 u64 stripe_nr;
fce3bb9a
LD
3708 u64 stripe_nr_orig;
3709 u64 stripe_nr_end;
593060d7 3710 int stripe_index;
cea9e445 3711 int i;
de11cc12 3712 int ret = 0;
f2d8d74d 3713 int num_stripes;
a236aed1 3714 int max_errors = 0;
a1d3c478 3715 struct btrfs_bio *bbio = NULL;
0b86a832 3716
890871be 3717 read_lock(&em_tree->lock);
0b86a832 3718 em = lookup_extent_mapping(em_tree, logical, *length);
890871be 3719 read_unlock(&em_tree->lock);
f2d8d74d 3720
3b951516 3721 if (!em) {
d397712b
CM
3722 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3723 (unsigned long long)logical,
3724 (unsigned long long)*length);
f2d8d74d 3725 BUG();
3b951516 3726 }
0b86a832
CM
3727
3728 BUG_ON(em->start > logical || em->start + em->len < logical);
3729 map = (struct map_lookup *)em->bdev;
3730 offset = logical - em->start;
593060d7 3731
f188591e
CM
3732 if (mirror_num > map->num_stripes)
3733 mirror_num = 0;
3734
593060d7
CM
3735 stripe_nr = offset;
3736 /*
3737 * stripe_nr counts the total number of stripes we have to stride
3738 * to get to this block
3739 */
3740 do_div(stripe_nr, map->stripe_len);
3741
3742 stripe_offset = stripe_nr * map->stripe_len;
3743 BUG_ON(offset < stripe_offset);
3744
3745 /* stripe_offset is the offset of this block in its stripe*/
3746 stripe_offset = offset - stripe_offset;
3747
fce3bb9a
LD
3748 if (rw & REQ_DISCARD)
3749 *length = min_t(u64, em->len - offset, *length);
52ba6929 3750 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
cea9e445
CM
3751 /* we limit the length of each bio to what fits in a stripe */
3752 *length = min_t(u64, em->len - offset,
fce3bb9a 3753 map->stripe_len - stripe_offset);
cea9e445
CM
3754 } else {
3755 *length = em->len - offset;
3756 }
f2d8d74d 3757
a1d3c478 3758 if (!bbio_ret)
cea9e445
CM
3759 goto out;
3760
f2d8d74d 3761 num_stripes = 1;
cea9e445 3762 stripe_index = 0;
fce3bb9a
LD
3763 stripe_nr_orig = stripe_nr;
3764 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3765 (~(map->stripe_len - 1));
3766 do_div(stripe_nr_end, map->stripe_len);
3767 stripe_end_offset = stripe_nr_end * map->stripe_len -
3768 (offset + *length);
3769 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3770 if (rw & REQ_DISCARD)
3771 num_stripes = min_t(u64, map->num_stripes,
3772 stripe_nr_end - stripe_nr_orig);
3773 stripe_index = do_div(stripe_nr, map->num_stripes);
3774 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
212a17ab 3775 if (rw & (REQ_WRITE | REQ_DISCARD))
f2d8d74d 3776 num_stripes = map->num_stripes;
2fff734f 3777 else if (mirror_num)
f188591e 3778 stripe_index = mirror_num - 1;
dfe25020
CM
3779 else {
3780 stripe_index = find_live_mirror(map, 0,
3781 map->num_stripes,
3782 current->pid % map->num_stripes);
a1d3c478 3783 mirror_num = stripe_index + 1;
dfe25020 3784 }
2fff734f 3785
611f0e00 3786 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
a1d3c478 3787 if (rw & (REQ_WRITE | REQ_DISCARD)) {
f2d8d74d 3788 num_stripes = map->num_stripes;
a1d3c478 3789 } else if (mirror_num) {
f188591e 3790 stripe_index = mirror_num - 1;
a1d3c478
JS
3791 } else {
3792 mirror_num = 1;
3793 }
2fff734f 3794
321aecc6
CM
3795 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3796 int factor = map->num_stripes / map->sub_stripes;
321aecc6
CM
3797
3798 stripe_index = do_div(stripe_nr, factor);
3799 stripe_index *= map->sub_stripes;
3800
7eaceacc 3801 if (rw & REQ_WRITE)
f2d8d74d 3802 num_stripes = map->sub_stripes;
fce3bb9a
LD
3803 else if (rw & REQ_DISCARD)
3804 num_stripes = min_t(u64, map->sub_stripes *
3805 (stripe_nr_end - stripe_nr_orig),
3806 map->num_stripes);
321aecc6
CM
3807 else if (mirror_num)
3808 stripe_index += mirror_num - 1;
dfe25020
CM
3809 else {
3810 stripe_index = find_live_mirror(map, stripe_index,
3811 map->sub_stripes, stripe_index +
3812 current->pid % map->sub_stripes);
a1d3c478 3813 mirror_num = stripe_index + 1;
dfe25020 3814 }
8790d502
CM
3815 } else {
3816 /*
3817 * after this do_div call, stripe_nr is the number of stripes
3818 * on this device we have to walk to find the data, and
3819 * stripe_index is the number of our device in the stripe array
3820 */
3821 stripe_index = do_div(stripe_nr, map->num_stripes);
a1d3c478 3822 mirror_num = stripe_index + 1;
8790d502 3823 }
593060d7 3824 BUG_ON(stripe_index >= map->num_stripes);
cea9e445 3825
de11cc12
LZ
3826 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3827 if (!bbio) {
3828 ret = -ENOMEM;
3829 goto out;
3830 }
3831 atomic_set(&bbio->error, 0);
3832
fce3bb9a 3833 if (rw & REQ_DISCARD) {
ec9ef7a1
LZ
3834 int factor = 0;
3835 int sub_stripes = 0;
3836 u64 stripes_per_dev = 0;
3837 u32 remaining_stripes = 0;
b89203f7 3838 u32 last_stripe = 0;
ec9ef7a1
LZ
3839
3840 if (map->type &
3841 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3842 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3843 sub_stripes = 1;
3844 else
3845 sub_stripes = map->sub_stripes;
3846
3847 factor = map->num_stripes / sub_stripes;
3848 stripes_per_dev = div_u64_rem(stripe_nr_end -
3849 stripe_nr_orig,
3850 factor,
3851 &remaining_stripes);
b89203f7
LB
3852 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3853 last_stripe *= sub_stripes;
ec9ef7a1
LZ
3854 }
3855
fce3bb9a 3856 for (i = 0; i < num_stripes; i++) {
a1d3c478 3857 bbio->stripes[i].physical =
f2d8d74d
CM
3858 map->stripes[stripe_index].physical +
3859 stripe_offset + stripe_nr * map->stripe_len;
a1d3c478 3860 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
fce3bb9a 3861
ec9ef7a1
LZ
3862 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3863 BTRFS_BLOCK_GROUP_RAID10)) {
3864 bbio->stripes[i].length = stripes_per_dev *
3865 map->stripe_len;
b89203f7 3866
ec9ef7a1
LZ
3867 if (i / sub_stripes < remaining_stripes)
3868 bbio->stripes[i].length +=
3869 map->stripe_len;
b89203f7
LB
3870
3871 /*
3872 * Special for the first stripe and
3873 * the last stripe:
3874 *
3875 * |-------|...|-------|
3876 * |----------|
3877 * off end_off
3878 */
ec9ef7a1 3879 if (i < sub_stripes)
a1d3c478 3880 bbio->stripes[i].length -=
fce3bb9a 3881 stripe_offset;
b89203f7
LB
3882
3883 if (stripe_index >= last_stripe &&
3884 stripe_index <= (last_stripe +
3885 sub_stripes - 1))
a1d3c478 3886 bbio->stripes[i].length -=
fce3bb9a 3887 stripe_end_offset;
b89203f7 3888
ec9ef7a1
LZ
3889 if (i == sub_stripes - 1)
3890 stripe_offset = 0;
fce3bb9a 3891 } else
a1d3c478 3892 bbio->stripes[i].length = *length;
fce3bb9a
LD
3893
3894 stripe_index++;
3895 if (stripe_index == map->num_stripes) {
3896 /* This could only happen for RAID0/10 */
3897 stripe_index = 0;
3898 stripe_nr++;
3899 }
3900 }
3901 } else {
3902 for (i = 0; i < num_stripes; i++) {
a1d3c478 3903 bbio->stripes[i].physical =
212a17ab
LT
3904 map->stripes[stripe_index].physical +
3905 stripe_offset +
3906 stripe_nr * map->stripe_len;
a1d3c478 3907 bbio->stripes[i].dev =
212a17ab 3908 map->stripes[stripe_index].dev;
fce3bb9a 3909 stripe_index++;
f2d8d74d 3910 }
593060d7 3911 }
de11cc12
LZ
3912
3913 if (rw & REQ_WRITE) {
3914 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3915 BTRFS_BLOCK_GROUP_RAID10 |
3916 BTRFS_BLOCK_GROUP_DUP)) {
3917 max_errors = 1;
3918 }
f2d8d74d 3919 }
de11cc12
LZ
3920
3921 *bbio_ret = bbio;
3922 bbio->num_stripes = num_stripes;
3923 bbio->max_errors = max_errors;
3924 bbio->mirror_num = mirror_num;
cea9e445 3925out:
0b86a832 3926 free_extent_map(em);
de11cc12 3927 return ret;
0b86a832
CM
3928}
3929
f2d8d74d
CM
3930int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3931 u64 logical, u64 *length,
a1d3c478 3932 struct btrfs_bio **bbio_ret, int mirror_num)
f2d8d74d 3933{
a1d3c478 3934 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
7eaceacc 3935 mirror_num);
f2d8d74d
CM
3936}
3937
a512bbf8
YZ
3938int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3939 u64 chunk_start, u64 physical, u64 devid,
3940 u64 **logical, int *naddrs, int *stripe_len)
3941{
3942 struct extent_map_tree *em_tree = &map_tree->map_tree;
3943 struct extent_map *em;
3944 struct map_lookup *map;
3945 u64 *buf;
3946 u64 bytenr;
3947 u64 length;
3948 u64 stripe_nr;
3949 int i, j, nr = 0;
3950
890871be 3951 read_lock(&em_tree->lock);
a512bbf8 3952 em = lookup_extent_mapping(em_tree, chunk_start, 1);
890871be 3953 read_unlock(&em_tree->lock);
a512bbf8
YZ
3954
3955 BUG_ON(!em || em->start != chunk_start);
3956 map = (struct map_lookup *)em->bdev;
3957
3958 length = em->len;
3959 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3960 do_div(length, map->num_stripes / map->sub_stripes);
3961 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3962 do_div(length, map->num_stripes);
3963
3964 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
79787eaa 3965 BUG_ON(!buf); /* -ENOMEM */
a512bbf8
YZ
3966
3967 for (i = 0; i < map->num_stripes; i++) {
3968 if (devid && map->stripes[i].dev->devid != devid)
3969 continue;
3970 if (map->stripes[i].physical > physical ||
3971 map->stripes[i].physical + length <= physical)
3972 continue;
3973
3974 stripe_nr = physical - map->stripes[i].physical;
3975 do_div(stripe_nr, map->stripe_len);
3976
3977 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3978 stripe_nr = stripe_nr * map->num_stripes + i;
3979 do_div(stripe_nr, map->sub_stripes);
3980 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3981 stripe_nr = stripe_nr * map->num_stripes + i;
3982 }
3983 bytenr = chunk_start + stripe_nr * map->stripe_len;
934d375b 3984 WARN_ON(nr >= map->num_stripes);
a512bbf8
YZ
3985 for (j = 0; j < nr; j++) {
3986 if (buf[j] == bytenr)
3987 break;
3988 }
934d375b
CM
3989 if (j == nr) {
3990 WARN_ON(nr >= map->num_stripes);
a512bbf8 3991 buf[nr++] = bytenr;
934d375b 3992 }
a512bbf8
YZ
3993 }
3994
a512bbf8
YZ
3995 *logical = buf;
3996 *naddrs = nr;
3997 *stripe_len = map->stripe_len;
3998
3999 free_extent_map(em);
4000 return 0;
f2d8d74d
CM
4001}
4002
a1d3c478 4003static void btrfs_end_bio(struct bio *bio, int err)
8790d502 4004{
a1d3c478 4005 struct btrfs_bio *bbio = bio->bi_private;
7d2b4daa 4006 int is_orig_bio = 0;
8790d502 4007
8790d502 4008 if (err)
a1d3c478 4009 atomic_inc(&bbio->error);
8790d502 4010
a1d3c478 4011 if (bio == bbio->orig_bio)
7d2b4daa
CM
4012 is_orig_bio = 1;
4013
a1d3c478 4014 if (atomic_dec_and_test(&bbio->stripes_pending)) {
7d2b4daa
CM
4015 if (!is_orig_bio) {
4016 bio_put(bio);
a1d3c478 4017 bio = bbio->orig_bio;
7d2b4daa 4018 }
a1d3c478
JS
4019 bio->bi_private = bbio->private;
4020 bio->bi_end_io = bbio->end_io;
2774b2ca
JS
4021 bio->bi_bdev = (struct block_device *)
4022 (unsigned long)bbio->mirror_num;
a236aed1
CM
4023 /* only send an error to the higher layers if it is
4024 * beyond the tolerance of the multi-bio
4025 */
a1d3c478 4026 if (atomic_read(&bbio->error) > bbio->max_errors) {
a236aed1 4027 err = -EIO;
5dbc8fca 4028 } else {
1259ab75
CM
4029 /*
4030 * this bio is actually up to date, we didn't
4031 * go over the max number of errors
4032 */
4033 set_bit(BIO_UPTODATE, &bio->bi_flags);
a236aed1 4034 err = 0;
1259ab75 4035 }
a1d3c478 4036 kfree(bbio);
8790d502
CM
4037
4038 bio_endio(bio, err);
7d2b4daa 4039 } else if (!is_orig_bio) {
8790d502
CM
4040 bio_put(bio);
4041 }
8790d502
CM
4042}
4043
8b712842
CM
4044struct async_sched {
4045 struct bio *bio;
4046 int rw;
4047 struct btrfs_fs_info *info;
4048 struct btrfs_work work;
4049};
4050
4051/*
4052 * see run_scheduled_bios for a description of why bios are collected for
4053 * async submit.
4054 *
4055 * This will add one bio to the pending list for a device and make sure
4056 * the work struct is scheduled.
4057 */
143bede5 4058static noinline void schedule_bio(struct btrfs_root *root,
a1b32a59
CM
4059 struct btrfs_device *device,
4060 int rw, struct bio *bio)
8b712842
CM
4061{
4062 int should_queue = 1;
ffbd517d 4063 struct btrfs_pending_bios *pending_bios;
8b712842
CM
4064
4065 /* don't bother with additional async steps for reads, right now */
7b6d91da 4066 if (!(rw & REQ_WRITE)) {
492bb6de 4067 bio_get(bio);
21adbd5c 4068 btrfsic_submit_bio(rw, bio);
492bb6de 4069 bio_put(bio);
143bede5 4070 return;
8b712842
CM
4071 }
4072
4073 /*
0986fe9e 4074 * nr_async_bios allows us to reliably return congestion to the
8b712842
CM
4075 * higher layers. Otherwise, the async bio makes it appear we have
4076 * made progress against dirty pages when we've really just put it
4077 * on a queue for later
4078 */
0986fe9e 4079 atomic_inc(&root->fs_info->nr_async_bios);
492bb6de 4080 WARN_ON(bio->bi_next);
8b712842
CM
4081 bio->bi_next = NULL;
4082 bio->bi_rw |= rw;
4083
4084 spin_lock(&device->io_lock);
7b6d91da 4085 if (bio->bi_rw & REQ_SYNC)
ffbd517d
CM
4086 pending_bios = &device->pending_sync_bios;
4087 else
4088 pending_bios = &device->pending_bios;
8b712842 4089
ffbd517d
CM
4090 if (pending_bios->tail)
4091 pending_bios->tail->bi_next = bio;
8b712842 4092
ffbd517d
CM
4093 pending_bios->tail = bio;
4094 if (!pending_bios->head)
4095 pending_bios->head = bio;
8b712842
CM
4096 if (device->running_pending)
4097 should_queue = 0;
4098
4099 spin_unlock(&device->io_lock);
4100
4101 if (should_queue)
1cc127b5
CM
4102 btrfs_queue_worker(&root->fs_info->submit_workers,
4103 &device->work);
8b712842
CM
4104}
4105
f188591e 4106int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
8b712842 4107 int mirror_num, int async_submit)
0b86a832
CM
4108{
4109 struct btrfs_mapping_tree *map_tree;
4110 struct btrfs_device *dev;
8790d502 4111 struct bio *first_bio = bio;
a62b9401 4112 u64 logical = (u64)bio->bi_sector << 9;
0b86a832
CM
4113 u64 length = 0;
4114 u64 map_length;
0b86a832 4115 int ret;
8790d502
CM
4116 int dev_nr = 0;
4117 int total_devs = 1;
a1d3c478 4118 struct btrfs_bio *bbio = NULL;
0b86a832 4119
f2d8d74d 4120 length = bio->bi_size;
0b86a832
CM
4121 map_tree = &root->fs_info->mapping_tree;
4122 map_length = length;
cea9e445 4123
a1d3c478 4124 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
f188591e 4125 mirror_num);
79787eaa
JM
4126 if (ret) /* -ENOMEM */
4127 return ret;
cea9e445 4128
a1d3c478 4129 total_devs = bbio->num_stripes;
cea9e445 4130 if (map_length < length) {
d397712b
CM
4131 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4132 "len %llu\n", (unsigned long long)logical,
4133 (unsigned long long)length,
4134 (unsigned long long)map_length);
cea9e445
CM
4135 BUG();
4136 }
a1d3c478
JS
4137
4138 bbio->orig_bio = first_bio;
4139 bbio->private = first_bio->bi_private;
4140 bbio->end_io = first_bio->bi_end_io;
4141 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
cea9e445 4142
d397712b 4143 while (dev_nr < total_devs) {
a1d3c478
JS
4144 if (dev_nr < total_devs - 1) {
4145 bio = bio_clone(first_bio, GFP_NOFS);
79787eaa 4146 BUG_ON(!bio); /* -ENOMEM */
a1d3c478
JS
4147 } else {
4148 bio = first_bio;
8790d502 4149 }
a1d3c478
JS
4150 bio->bi_private = bbio;
4151 bio->bi_end_io = btrfs_end_bio;
4152 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4153 dev = bbio->stripes[dev_nr].dev;
18e503d6 4154 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
a1d3c478
JS
4155 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4156 "(%s id %llu), size=%u\n", rw,
4157 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4158 dev->name, dev->devid, bio->bi_size);
dfe25020 4159 bio->bi_bdev = dev->bdev;
8b712842
CM
4160 if (async_submit)
4161 schedule_bio(root, dev, rw, bio);
4162 else
21adbd5c 4163 btrfsic_submit_bio(rw, bio);
dfe25020
CM
4164 } else {
4165 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4166 bio->bi_sector = logical >> 9;
dfe25020 4167 bio_endio(bio, -EIO);
dfe25020 4168 }
8790d502
CM
4169 dev_nr++;
4170 }
0b86a832
CM
4171 return 0;
4172}
4173
a443755f 4174struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2b82032c 4175 u8 *uuid, u8 *fsid)
0b86a832 4176{
2b82032c
YZ
4177 struct btrfs_device *device;
4178 struct btrfs_fs_devices *cur_devices;
4179
4180 cur_devices = root->fs_info->fs_devices;
4181 while (cur_devices) {
4182 if (!fsid ||
4183 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4184 device = __find_device(&cur_devices->devices,
4185 devid, uuid);
4186 if (device)
4187 return device;
4188 }
4189 cur_devices = cur_devices->seed;
4190 }
4191 return NULL;
0b86a832
CM
4192}
4193
dfe25020
CM
4194static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4195 u64 devid, u8 *dev_uuid)
4196{
4197 struct btrfs_device *device;
4198 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4199
4200 device = kzalloc(sizeof(*device), GFP_NOFS);
7cbd8a83 4201 if (!device)
4202 return NULL;
dfe25020
CM
4203 list_add(&device->dev_list,
4204 &fs_devices->devices);
dfe25020
CM
4205 device->dev_root = root->fs_info->dev_root;
4206 device->devid = devid;
8b712842 4207 device->work.func = pending_bios_fn;
e4404d6e 4208 device->fs_devices = fs_devices;
cd02dca5 4209 device->missing = 1;
dfe25020 4210 fs_devices->num_devices++;
cd02dca5 4211 fs_devices->missing_devices++;
dfe25020 4212 spin_lock_init(&device->io_lock);
d20f7043 4213 INIT_LIST_HEAD(&device->dev_alloc_list);
dfe25020
CM
4214 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4215 return device;
4216}
4217
0b86a832
CM
4218static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4219 struct extent_buffer *leaf,
4220 struct btrfs_chunk *chunk)
4221{
4222 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4223 struct map_lookup *map;
4224 struct extent_map *em;
4225 u64 logical;
4226 u64 length;
4227 u64 devid;
a443755f 4228 u8 uuid[BTRFS_UUID_SIZE];
593060d7 4229 int num_stripes;
0b86a832 4230 int ret;
593060d7 4231 int i;
0b86a832 4232
e17cade2
CM
4233 logical = key->offset;
4234 length = btrfs_chunk_length(leaf, chunk);
a061fc8d 4235
890871be 4236 read_lock(&map_tree->map_tree.lock);
0b86a832 4237 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
890871be 4238 read_unlock(&map_tree->map_tree.lock);
0b86a832
CM
4239
4240 /* already mapped? */
4241 if (em && em->start <= logical && em->start + em->len > logical) {
4242 free_extent_map(em);
0b86a832
CM
4243 return 0;
4244 } else if (em) {
4245 free_extent_map(em);
4246 }
0b86a832 4247
172ddd60 4248 em = alloc_extent_map();
0b86a832
CM
4249 if (!em)
4250 return -ENOMEM;
593060d7
CM
4251 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4252 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
0b86a832
CM
4253 if (!map) {
4254 free_extent_map(em);
4255 return -ENOMEM;
4256 }
4257
4258 em->bdev = (struct block_device *)map;
4259 em->start = logical;
4260 em->len = length;
4261 em->block_start = 0;
c8b97818 4262 em->block_len = em->len;
0b86a832 4263
593060d7
CM
4264 map->num_stripes = num_stripes;
4265 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4266 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4267 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4268 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4269 map->type = btrfs_chunk_type(leaf, chunk);
321aecc6 4270 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
593060d7
CM
4271 for (i = 0; i < num_stripes; i++) {
4272 map->stripes[i].physical =
4273 btrfs_stripe_offset_nr(leaf, chunk, i);
4274 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
a443755f
CM
4275 read_extent_buffer(leaf, uuid, (unsigned long)
4276 btrfs_stripe_dev_uuid_nr(chunk, i),
4277 BTRFS_UUID_SIZE);
2b82032c
YZ
4278 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4279 NULL);
dfe25020 4280 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
593060d7
CM
4281 kfree(map);
4282 free_extent_map(em);
4283 return -EIO;
4284 }
dfe25020
CM
4285 if (!map->stripes[i].dev) {
4286 map->stripes[i].dev =
4287 add_missing_dev(root, devid, uuid);
4288 if (!map->stripes[i].dev) {
4289 kfree(map);
4290 free_extent_map(em);
4291 return -EIO;
4292 }
4293 }
4294 map->stripes[i].dev->in_fs_metadata = 1;
0b86a832
CM
4295 }
4296
890871be 4297 write_lock(&map_tree->map_tree.lock);
0b86a832 4298 ret = add_extent_mapping(&map_tree->map_tree, em);
890871be 4299 write_unlock(&map_tree->map_tree.lock);
79787eaa 4300 BUG_ON(ret); /* Tree corruption */
0b86a832
CM
4301 free_extent_map(em);
4302
4303 return 0;
4304}
4305
143bede5 4306static void fill_device_from_item(struct extent_buffer *leaf,
0b86a832
CM
4307 struct btrfs_dev_item *dev_item,
4308 struct btrfs_device *device)
4309{
4310 unsigned long ptr;
0b86a832
CM
4311
4312 device->devid = btrfs_device_id(leaf, dev_item);
d6397bae
CB
4313 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4314 device->total_bytes = device->disk_total_bytes;
0b86a832
CM
4315 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4316 device->type = btrfs_device_type(leaf, dev_item);
4317 device->io_align = btrfs_device_io_align(leaf, dev_item);
4318 device->io_width = btrfs_device_io_width(leaf, dev_item);
4319 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
0b86a832
CM
4320
4321 ptr = (unsigned long)btrfs_device_uuid(dev_item);
e17cade2 4322 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
0b86a832
CM
4323}
4324
2b82032c
YZ
4325static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4326{
4327 struct btrfs_fs_devices *fs_devices;
4328 int ret;
4329
b367e47f 4330 BUG_ON(!mutex_is_locked(&uuid_mutex));
2b82032c
YZ
4331
4332 fs_devices = root->fs_info->fs_devices->seed;
4333 while (fs_devices) {
4334 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4335 ret = 0;
4336 goto out;
4337 }
4338 fs_devices = fs_devices->seed;
4339 }
4340
4341 fs_devices = find_fsid(fsid);
4342 if (!fs_devices) {
4343 ret = -ENOENT;
4344 goto out;
4345 }
e4404d6e
YZ
4346
4347 fs_devices = clone_fs_devices(fs_devices);
4348 if (IS_ERR(fs_devices)) {
4349 ret = PTR_ERR(fs_devices);
2b82032c
YZ
4350 goto out;
4351 }
4352
97288f2c 4353 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
15916de8 4354 root->fs_info->bdev_holder);
2b82032c
YZ
4355 if (ret)
4356 goto out;
4357
4358 if (!fs_devices->seeding) {
4359 __btrfs_close_devices(fs_devices);
e4404d6e 4360 free_fs_devices(fs_devices);
2b82032c
YZ
4361 ret = -EINVAL;
4362 goto out;
4363 }
4364
4365 fs_devices->seed = root->fs_info->fs_devices->seed;
4366 root->fs_info->fs_devices->seed = fs_devices;
2b82032c 4367out:
2b82032c
YZ
4368 return ret;
4369}
4370
0d81ba5d 4371static int read_one_dev(struct btrfs_root *root,
0b86a832
CM
4372 struct extent_buffer *leaf,
4373 struct btrfs_dev_item *dev_item)
4374{
4375 struct btrfs_device *device;
4376 u64 devid;
4377 int ret;
2b82032c 4378 u8 fs_uuid[BTRFS_UUID_SIZE];
a443755f
CM
4379 u8 dev_uuid[BTRFS_UUID_SIZE];
4380
0b86a832 4381 devid = btrfs_device_id(leaf, dev_item);
a443755f
CM
4382 read_extent_buffer(leaf, dev_uuid,
4383 (unsigned long)btrfs_device_uuid(dev_item),
4384 BTRFS_UUID_SIZE);
2b82032c
YZ
4385 read_extent_buffer(leaf, fs_uuid,
4386 (unsigned long)btrfs_device_fsid(dev_item),
4387 BTRFS_UUID_SIZE);
4388
4389 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4390 ret = open_seed_devices(root, fs_uuid);
e4404d6e 4391 if (ret && !btrfs_test_opt(root, DEGRADED))
2b82032c 4392 return ret;
2b82032c
YZ
4393 }
4394
4395 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4396 if (!device || !device->bdev) {
e4404d6e 4397 if (!btrfs_test_opt(root, DEGRADED))
2b82032c
YZ
4398 return -EIO;
4399
4400 if (!device) {
d397712b
CM
4401 printk(KERN_WARNING "warning devid %llu missing\n",
4402 (unsigned long long)devid);
2b82032c
YZ
4403 device = add_missing_dev(root, devid, dev_uuid);
4404 if (!device)
4405 return -ENOMEM;
cd02dca5
CM
4406 } else if (!device->missing) {
4407 /*
4408 * this happens when a device that was properly setup
4409 * in the device info lists suddenly goes bad.
4410 * device->bdev is NULL, and so we have to set
4411 * device->missing to one here
4412 */
4413 root->fs_info->fs_devices->missing_devices++;
4414 device->missing = 1;
2b82032c
YZ
4415 }
4416 }
4417
4418 if (device->fs_devices != root->fs_info->fs_devices) {
4419 BUG_ON(device->writeable);
4420 if (device->generation !=
4421 btrfs_device_generation(leaf, dev_item))
4422 return -EINVAL;
6324fbf3 4423 }
0b86a832
CM
4424
4425 fill_device_from_item(leaf, dev_item, device);
4426 device->dev_root = root->fs_info->dev_root;
dfe25020 4427 device->in_fs_metadata = 1;
2bf64758 4428 if (device->writeable) {
2b82032c 4429 device->fs_devices->total_rw_bytes += device->total_bytes;
2bf64758
JB
4430 spin_lock(&root->fs_info->free_chunk_lock);
4431 root->fs_info->free_chunk_space += device->total_bytes -
4432 device->bytes_used;
4433 spin_unlock(&root->fs_info->free_chunk_lock);
4434 }
0b86a832 4435 ret = 0;
0b86a832
CM
4436 return ret;
4437}
4438
e4404d6e 4439int btrfs_read_sys_array(struct btrfs_root *root)
0b86a832 4440{
6c41761f 4441 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
a061fc8d 4442 struct extent_buffer *sb;
0b86a832 4443 struct btrfs_disk_key *disk_key;
0b86a832 4444 struct btrfs_chunk *chunk;
84eed90f
CM
4445 u8 *ptr;
4446 unsigned long sb_ptr;
4447 int ret = 0;
0b86a832
CM
4448 u32 num_stripes;
4449 u32 array_size;
4450 u32 len = 0;
0b86a832 4451 u32 cur;
84eed90f 4452 struct btrfs_key key;
0b86a832 4453
e4404d6e 4454 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
a061fc8d
CM
4455 BTRFS_SUPER_INFO_SIZE);
4456 if (!sb)
4457 return -ENOMEM;
4458 btrfs_set_buffer_uptodate(sb);
85d4e461 4459 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
8a334426
DS
4460 /*
4461 * The sb extent buffer is artifical and just used to read the system array.
4462 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4463 * pages up-to-date when the page is larger: extent does not cover the
4464 * whole page and consequently check_page_uptodate does not find all
4465 * the page's extents up-to-date (the hole beyond sb),
4466 * write_extent_buffer then triggers a WARN_ON.
4467 *
4468 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4469 * but sb spans only this function. Add an explicit SetPageUptodate call
4470 * to silence the warning eg. on PowerPC 64.
4471 */
4472 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
727011e0 4473 SetPageUptodate(sb->pages[0]);
4008c04a 4474
a061fc8d 4475 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
0b86a832
CM
4476 array_size = btrfs_super_sys_array_size(super_copy);
4477
0b86a832
CM
4478 ptr = super_copy->sys_chunk_array;
4479 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4480 cur = 0;
4481
4482 while (cur < array_size) {
4483 disk_key = (struct btrfs_disk_key *)ptr;
4484 btrfs_disk_key_to_cpu(&key, disk_key);
4485
a061fc8d 4486 len = sizeof(*disk_key); ptr += len;
0b86a832
CM
4487 sb_ptr += len;
4488 cur += len;
4489
0d81ba5d 4490 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
0b86a832 4491 chunk = (struct btrfs_chunk *)sb_ptr;
0d81ba5d 4492 ret = read_one_chunk(root, &key, sb, chunk);
84eed90f
CM
4493 if (ret)
4494 break;
0b86a832
CM
4495 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4496 len = btrfs_chunk_item_size(num_stripes);
4497 } else {
84eed90f
CM
4498 ret = -EIO;
4499 break;
0b86a832
CM
4500 }
4501 ptr += len;
4502 sb_ptr += len;
4503 cur += len;
4504 }
a061fc8d 4505 free_extent_buffer(sb);
84eed90f 4506 return ret;
0b86a832
CM
4507}
4508
4509int btrfs_read_chunk_tree(struct btrfs_root *root)
4510{
4511 struct btrfs_path *path;
4512 struct extent_buffer *leaf;
4513 struct btrfs_key key;
4514 struct btrfs_key found_key;
4515 int ret;
4516 int slot;
4517
4518 root = root->fs_info->chunk_root;
4519
4520 path = btrfs_alloc_path();
4521 if (!path)
4522 return -ENOMEM;
4523
b367e47f
LZ
4524 mutex_lock(&uuid_mutex);
4525 lock_chunks(root);
4526
0b86a832
CM
4527 /* first we search for all of the device items, and then we
4528 * read in all of the chunk items. This way we can create chunk
4529 * mappings that reference all of the devices that are afound
4530 */
4531 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4532 key.offset = 0;
4533 key.type = 0;
4534again:
4535 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
ab59381e
ZL
4536 if (ret < 0)
4537 goto error;
d397712b 4538 while (1) {
0b86a832
CM
4539 leaf = path->nodes[0];
4540 slot = path->slots[0];
4541 if (slot >= btrfs_header_nritems(leaf)) {
4542 ret = btrfs_next_leaf(root, path);
4543 if (ret == 0)
4544 continue;
4545 if (ret < 0)
4546 goto error;
4547 break;
4548 }
4549 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4550 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4551 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4552 break;
4553 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4554 struct btrfs_dev_item *dev_item;
4555 dev_item = btrfs_item_ptr(leaf, slot,
4556 struct btrfs_dev_item);
0d81ba5d 4557 ret = read_one_dev(root, leaf, dev_item);
2b82032c
YZ
4558 if (ret)
4559 goto error;
0b86a832
CM
4560 }
4561 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4562 struct btrfs_chunk *chunk;
4563 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4564 ret = read_one_chunk(root, &found_key, leaf, chunk);
2b82032c
YZ
4565 if (ret)
4566 goto error;
0b86a832
CM
4567 }
4568 path->slots[0]++;
4569 }
4570 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4571 key.objectid = 0;
b3b4aa74 4572 btrfs_release_path(path);
0b86a832
CM
4573 goto again;
4574 }
0b86a832
CM
4575 ret = 0;
4576error:
b367e47f
LZ
4577 unlock_chunks(root);
4578 mutex_unlock(&uuid_mutex);
4579
2b82032c 4580 btrfs_free_path(path);
0b86a832
CM
4581 return ret;
4582}