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