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