blob: c6c80093eac00d6ae57a84a220c97f42f0a85c23 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040020#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040021#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040022#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040023#include <linux/iocontext.h>
Chris Mason593060d2008-03-25 16:50:33 -040024#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040026#include "ctree.h"
27#include "extent_map.h"
28#include "disk-io.h"
29#include "transaction.h"
30#include "print-tree.h"
31#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040032#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033
Chris Mason593060d2008-03-25 16:50:33 -040034struct map_lookup {
35 u64 type;
36 int io_align;
37 int io_width;
38 int stripe_len;
39 int sector_size;
40 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040041 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040042 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040043};
44
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49
Chris Mason593060d2008-03-25 16:50:33 -040050#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040051 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Masona061fc82008-05-07 11:43:44 -040056void btrfs_lock_volumes(void)
57{
58 mutex_lock(&uuid_mutex);
59}
60
61void btrfs_unlock_volumes(void)
62{
63 mutex_unlock(&uuid_mutex);
64}
65
Chris Mason7d9eb122008-07-08 14:19:17 -040066static void lock_chunks(struct btrfs_root *root)
67{
Chris Mason7d9eb122008-07-08 14:19:17 -040068 mutex_lock(&root->fs_info->chunk_mutex);
69}
70
71static void unlock_chunks(struct btrfs_root *root)
72{
Chris Mason7d9eb122008-07-08 14:19:17 -040073 mutex_unlock(&root->fs_info->chunk_mutex);
74}
75
Yan Zhenge4404d62008-12-12 10:03:26 -050076static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
77{
78 struct btrfs_device *device;
79 WARN_ON(fs_devices->opened);
80 while (!list_empty(&fs_devices->devices)) {
81 device = list_entry(fs_devices->devices.next,
82 struct btrfs_device, dev_list);
83 list_del(&device->dev_list);
84 kfree(device->name);
85 kfree(device);
86 }
87 kfree(fs_devices);
88}
89
Chris Mason8a4b83c2008-03-24 15:02:07 -040090int btrfs_cleanup_fs_uuids(void)
91{
92 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040093
Yan Zheng2b820322008-11-17 21:11:30 -050094 while (!list_empty(&fs_uuids)) {
95 fs_devices = list_entry(fs_uuids.next,
96 struct btrfs_fs_devices, list);
97 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050098 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040099 }
100 return 0;
101}
102
Chris Masona1b32a52008-09-05 16:09:51 -0400103static noinline struct btrfs_device *__find_device(struct list_head *head,
104 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105{
106 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500108 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400109 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400110 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400111 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400112 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 }
114 return NULL;
115}
116
Chris Masona1b32a52008-09-05 16:09:51 -0400117static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400118{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 struct btrfs_fs_devices *fs_devices;
120
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500121 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400122 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
123 return fs_devices;
124 }
125 return NULL;
126}
127
Chris Masonffbd5172009-04-20 15:50:09 -0400128static void requeue_list(struct btrfs_pending_bios *pending_bios,
129 struct bio *head, struct bio *tail)
130{
131
132 struct bio *old_head;
133
134 old_head = pending_bios->head;
135 pending_bios->head = head;
136 if (pending_bios->tail)
137 tail->bi_next = old_head;
138 else
139 pending_bios->tail = tail;
140}
141
Chris Mason8b712842008-06-11 16:50:36 -0400142/*
143 * we try to collect pending bios for a device so we don't get a large
144 * number of procs sending bios down to the same device. This greatly
145 * improves the schedulers ability to collect and merge the bios.
146 *
147 * But, it also turns into a long list of bios to process and that is sure
148 * to eventually make the worker thread block. The solution here is to
149 * make some progress and then put this work struct back at the end of
150 * the list if the block device is congested. This way, multiple devices
151 * can make progress from a single worker thread.
152 */
Chris Masond3977122009-01-05 21:25:51 -0500153static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400154{
155 struct bio *pending;
156 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400157 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400158 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400159 struct bio *tail;
160 struct bio *cur;
161 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400162 unsigned long num_run;
163 unsigned long num_sync_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400164 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400165 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400166 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400167 int force_reg = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400168
Chris Masonbedf7622009-04-03 10:32:58 -0400169 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400170 fs_info = device->dev_root->fs_info;
171 limit = btrfs_async_submit_limit(fs_info);
172 limit = limit * 2 / 3;
173
Chris Masonffbd5172009-04-20 15:50:09 -0400174 /* we want to make sure that every time we switch from the sync
175 * list to the normal list, we unplug
176 */
177 num_sync_run = 0;
178
Chris Mason8b712842008-06-11 16:50:36 -0400179loop:
180 spin_lock(&device->io_lock);
181
Chris Masona6837052009-02-04 09:19:41 -0500182loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400183 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400184
Chris Mason8b712842008-06-11 16:50:36 -0400185 /* take all the bios off the list at once and process them
186 * later on (without the lock held). But, remember the
187 * tail and other pointers so the bios can be properly reinserted
188 * into the list if we hit congestion
189 */
Chris Masond84275c2009-06-09 15:39:08 -0400190 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400191 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400192 force_reg = 1;
193 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400194 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400195 force_reg = 0;
196 }
Chris Masonffbd5172009-04-20 15:50:09 -0400197
198 pending = pending_bios->head;
199 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400200 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400201
202 /*
203 * if pending was null this time around, no bios need processing
204 * at all and we can stop. Otherwise it'll loop back up again
205 * and do an additional check so no bios are missed.
206 *
207 * device->running_pending is used to synchronize with the
208 * schedule_bio code.
209 */
Chris Masonffbd5172009-04-20 15:50:09 -0400210 if (device->pending_sync_bios.head == NULL &&
211 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400212 again = 0;
213 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400214 } else {
215 again = 1;
216 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400217 }
Chris Masonffbd5172009-04-20 15:50:09 -0400218
219 pending_bios->head = NULL;
220 pending_bios->tail = NULL;
221
Chris Mason8b712842008-06-11 16:50:36 -0400222 spin_unlock(&device->io_lock);
223
Chris Masonffbd5172009-04-20 15:50:09 -0400224 /*
225 * if we're doing the regular priority list, make sure we unplug
226 * for any high prio bios we've sent down
227 */
228 if (pending_bios == &device->pending_bios && num_sync_run > 0) {
229 num_sync_run = 0;
230 blk_run_backing_dev(bdi, NULL);
231 }
232
Chris Masond3977122009-01-05 21:25:51 -0500233 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400234
235 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400236 /* we want to work on both lists, but do more bios on the
237 * sync list than the regular list
238 */
239 if ((num_run > 32 &&
240 pending_bios != &device->pending_sync_bios &&
241 device->pending_sync_bios.head) ||
242 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
243 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400244 spin_lock(&device->io_lock);
245 requeue_list(pending_bios, pending, tail);
246 goto loop_lock;
247 }
248
Chris Mason8b712842008-06-11 16:50:36 -0400249 cur = pending;
250 pending = pending->bi_next;
251 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400252 atomic_dec(&fs_info->nr_async_bios);
253
254 if (atomic_read(&fs_info->nr_async_bios) < limit &&
255 waitqueue_active(&fs_info->async_submit_wait))
256 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400257
258 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400259
Jens Axboe1f98a132009-09-11 14:32:04 +0200260 if (bio_rw_flagged(cur, BIO_RW_SYNCIO))
Chris Masonffbd5172009-04-20 15:50:09 -0400261 num_sync_run++;
262
Chris Mason5ff7ba32010-03-15 10:21:30 -0400263 submit_bio(cur->bi_rw, cur);
264 num_run++;
265 batch_run++;
Chris Masonffbd5172009-04-20 15:50:09 -0400266 if (need_resched()) {
267 if (num_sync_run) {
268 blk_run_backing_dev(bdi, NULL);
269 num_sync_run = 0;
270 }
271 cond_resched();
272 }
Chris Mason8b712842008-06-11 16:50:36 -0400273
274 /*
275 * we made progress, there is more work to do and the bdi
276 * is now congested. Back off and let other work structs
277 * run instead
278 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400279 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500280 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400281 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400282
Chris Masonb765ead2009-04-03 10:27:10 -0400283 ioc = current->io_context;
284
285 /*
286 * the main goal here is that we don't want to
287 * block if we're going to be able to submit
288 * more requests without blocking.
289 *
290 * This code does two great things, it pokes into
291 * the elevator code from a filesystem _and_
292 * it makes assumptions about how batching works.
293 */
294 if (ioc && ioc->nr_batch_requests > 0 &&
295 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
296 (last_waited == 0 ||
297 ioc->last_waited == last_waited)) {
298 /*
299 * we want to go through our batch of
300 * requests and stop. So, we copy out
301 * the ioc->last_waited time and test
302 * against it before looping
303 */
304 last_waited = ioc->last_waited;
Chris Masonffbd5172009-04-20 15:50:09 -0400305 if (need_resched()) {
306 if (num_sync_run) {
307 blk_run_backing_dev(bdi, NULL);
308 num_sync_run = 0;
309 }
310 cond_resched();
311 }
Chris Masonb765ead2009-04-03 10:27:10 -0400312 continue;
313 }
Chris Mason8b712842008-06-11 16:50:36 -0400314 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400315 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500316 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400317
318 spin_unlock(&device->io_lock);
319 btrfs_requeue_work(&device->work);
320 goto done;
321 }
322 }
Chris Masonffbd5172009-04-20 15:50:09 -0400323
324 if (num_sync_run) {
325 num_sync_run = 0;
326 blk_run_backing_dev(bdi, NULL);
327 }
Chris Masonbedf7622009-04-03 10:32:58 -0400328 /*
329 * IO has already been through a long path to get here. Checksumming,
330 * async helper threads, perhaps compression. We've done a pretty
331 * good job of collecting a batch of IO and should just unplug
332 * the device right away.
333 *
334 * This will help anyone who is waiting on the IO, they might have
335 * already unplugged, but managed to do so before the bio they
336 * cared about found its way down here.
337 */
338 blk_run_backing_dev(bdi, NULL);
Chris Mason51684082010-03-10 15:33:32 -0500339
340 cond_resched();
341 if (again)
342 goto loop;
343
344 spin_lock(&device->io_lock);
345 if (device->pending_bios.head || device->pending_sync_bios.head)
346 goto loop_lock;
347 spin_unlock(&device->io_lock);
348
Chris Mason8b712842008-06-11 16:50:36 -0400349done:
350 return 0;
351}
352
Christoph Hellwigb2950862008-12-02 09:54:17 -0500353static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400354{
355 struct btrfs_device *device;
356
357 device = container_of(work, struct btrfs_device, work);
358 run_scheduled_bios(device);
359}
360
Chris Masona1b32a52008-09-05 16:09:51 -0400361static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400362 struct btrfs_super_block *disk_super,
363 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
364{
365 struct btrfs_device *device;
366 struct btrfs_fs_devices *fs_devices;
367 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000368 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400369
370 fs_devices = find_fsid(disk_super->fsid);
371 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400372 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400373 if (!fs_devices)
374 return -ENOMEM;
375 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400376 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400377 list_add(&fs_devices->list, &fs_uuids);
378 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
379 fs_devices->latest_devid = devid;
380 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400381 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400382 device = NULL;
383 } else {
Chris Masona4437552008-04-18 10:29:38 -0400384 device = __find_device(&fs_devices->devices, devid,
385 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400386 }
387 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500388 if (fs_devices->opened)
389 return -EBUSY;
390
Chris Mason8a4b83c2008-03-24 15:02:07 -0400391 device = kzalloc(sizeof(*device), GFP_NOFS);
392 if (!device) {
393 /* we can safely leave the fs_devices entry around */
394 return -ENOMEM;
395 }
396 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400397 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400398 memcpy(device->uuid, disk_super->dev_item.uuid,
399 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400400 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400401 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402 device->name = kstrdup(path, GFP_NOFS);
403 if (!device->name) {
404 kfree(device);
405 return -ENOMEM;
406 }
Yan Zheng2b820322008-11-17 21:11:30 -0500407 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400408
409 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400410 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400411 mutex_unlock(&fs_devices->device_list_mutex);
412
Yan Zheng2b820322008-11-17 21:11:30 -0500413 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400414 fs_devices->num_devices++;
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000415 } else if (strcmp(device->name, path)) {
416 name = kstrdup(path, GFP_NOFS);
417 if (!name)
418 return -ENOMEM;
419 kfree(device->name);
420 device->name = name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400421 }
422
423 if (found_transid > fs_devices->latest_trans) {
424 fs_devices->latest_devid = devid;
425 fs_devices->latest_trans = found_transid;
426 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427 *fs_devices_ret = fs_devices;
428 return 0;
429}
430
Yan Zhenge4404d62008-12-12 10:03:26 -0500431static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
432{
433 struct btrfs_fs_devices *fs_devices;
434 struct btrfs_device *device;
435 struct btrfs_device *orig_dev;
436
437 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
438 if (!fs_devices)
439 return ERR_PTR(-ENOMEM);
440
441 INIT_LIST_HEAD(&fs_devices->devices);
442 INIT_LIST_HEAD(&fs_devices->alloc_list);
443 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400444 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500445 fs_devices->latest_devid = orig->latest_devid;
446 fs_devices->latest_trans = orig->latest_trans;
447 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
448
Chris Masone5e9a522009-06-10 15:17:02 -0400449 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500450 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
451 device = kzalloc(sizeof(*device), GFP_NOFS);
452 if (!device)
453 goto error;
454
455 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400456 if (!device->name) {
457 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500458 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400459 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500460
461 device->devid = orig_dev->devid;
462 device->work.func = pending_bios_fn;
463 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
464 device->barriers = 1;
465 spin_lock_init(&device->io_lock);
466 INIT_LIST_HEAD(&device->dev_list);
467 INIT_LIST_HEAD(&device->dev_alloc_list);
468
469 list_add(&device->dev_list, &fs_devices->devices);
470 device->fs_devices = fs_devices;
471 fs_devices->num_devices++;
472 }
Chris Masone5e9a522009-06-10 15:17:02 -0400473 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500474 return fs_devices;
475error:
Chris Masone5e9a522009-06-10 15:17:02 -0400476 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500477 free_fs_devices(fs_devices);
478 return ERR_PTR(-ENOMEM);
479}
480
Chris Masondfe25022008-05-13 13:46:40 -0400481int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
482{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500483 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400484
485 mutex_lock(&uuid_mutex);
486again:
Chris Masone5e9a522009-06-10 15:17:02 -0400487 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500488 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500489 if (device->in_fs_metadata)
490 continue;
491
492 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500493 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500494 device->bdev = NULL;
495 fs_devices->open_devices--;
496 }
497 if (device->writeable) {
498 list_del_init(&device->dev_alloc_list);
499 device->writeable = 0;
500 fs_devices->rw_devices--;
501 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500502 list_del_init(&device->dev_list);
503 fs_devices->num_devices--;
504 kfree(device->name);
505 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400506 }
Chris Masone5e9a522009-06-10 15:17:02 -0400507 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500508
509 if (fs_devices->seed) {
510 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500511 goto again;
512 }
513
Chris Masondfe25022008-05-13 13:46:40 -0400514 mutex_unlock(&uuid_mutex);
515 return 0;
516}
Chris Masona0af4692008-05-13 16:03:06 -0400517
Yan Zheng2b820322008-11-17 21:11:30 -0500518static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400520 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500521
Yan Zheng2b820322008-11-17 21:11:30 -0500522 if (--fs_devices->opened > 0)
523 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400524
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500525 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400526 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500527 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400528 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400529 }
Yan Zheng2b820322008-11-17 21:11:30 -0500530 if (device->writeable) {
531 list_del_init(&device->dev_alloc_list);
532 fs_devices->rw_devices--;
533 }
534
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500536 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400537 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400538 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500539 WARN_ON(fs_devices->open_devices);
540 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500541 fs_devices->opened = 0;
542 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500543
Chris Mason8a4b83c2008-03-24 15:02:07 -0400544 return 0;
545}
546
Yan Zheng2b820322008-11-17 21:11:30 -0500547int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
548{
Yan Zhenge4404d62008-12-12 10:03:26 -0500549 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500550 int ret;
551
552 mutex_lock(&uuid_mutex);
553 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500554 if (!fs_devices->opened) {
555 seed_devices = fs_devices->seed;
556 fs_devices->seed = NULL;
557 }
Yan Zheng2b820322008-11-17 21:11:30 -0500558 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500559
560 while (seed_devices) {
561 fs_devices = seed_devices;
562 seed_devices = fs_devices->seed;
563 __btrfs_close_devices(fs_devices);
564 free_fs_devices(fs_devices);
565 }
Yan Zheng2b820322008-11-17 21:11:30 -0500566 return ret;
567}
568
Yan Zhenge4404d62008-12-12 10:03:26 -0500569static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
570 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571{
572 struct block_device *bdev;
573 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400575 struct block_device *latest_bdev = NULL;
576 struct buffer_head *bh;
577 struct btrfs_super_block *disk_super;
578 u64 latest_devid = 0;
579 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400580 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500581 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400582 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500584 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400585 if (device->bdev)
586 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400587 if (!device->name)
588 continue;
589
Chris Mason15916de2008-11-19 21:17:22 -0500590 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400591 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500592 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400593 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400594 }
Chris Masona061fc82008-05-07 11:43:44 -0400595 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400596
Yan Zhenga512bbf2008-12-08 16:46:26 -0500597 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400598 if (!bh)
599 goto error_close;
600
601 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000602 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400603 if (devid != device->devid)
604 goto error_brelse;
605
Yan Zheng2b820322008-11-17 21:11:30 -0500606 if (memcmp(device->uuid, disk_super->dev_item.uuid,
607 BTRFS_UUID_SIZE))
608 goto error_brelse;
609
610 device->generation = btrfs_super_generation(disk_super);
611 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400612 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500613 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400614 latest_bdev = bdev;
615 }
616
Yan Zheng2b820322008-11-17 21:11:30 -0500617 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
618 device->writeable = 0;
619 } else {
620 device->writeable = !bdev_read_only(bdev);
621 seeding = 0;
622 }
623
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400625 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500626 device->mode = flags;
627
Chris Masonc2898112009-06-10 09:51:32 -0400628 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
629 fs_devices->rotating = 1;
630
Chris Masona0af4692008-05-13 16:03:06 -0400631 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500632 if (device->writeable) {
633 fs_devices->rw_devices++;
634 list_add(&device->dev_alloc_list,
635 &fs_devices->alloc_list);
636 }
Chris Masona0af4692008-05-13 16:03:06 -0400637 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400638
Chris Masona0af4692008-05-13 16:03:06 -0400639error_brelse:
640 brelse(bh);
641error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500642 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400643error:
644 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400645 }
Chris Masona0af4692008-05-13 16:03:06 -0400646 if (fs_devices->open_devices == 0) {
647 ret = -EIO;
648 goto out;
649 }
Yan Zheng2b820322008-11-17 21:11:30 -0500650 fs_devices->seeding = seeding;
651 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400652 fs_devices->latest_bdev = latest_bdev;
653 fs_devices->latest_devid = latest_devid;
654 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500655 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400656out:
Yan Zheng2b820322008-11-17 21:11:30 -0500657 return ret;
658}
659
660int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500661 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500662{
663 int ret;
664
665 mutex_lock(&uuid_mutex);
666 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500667 fs_devices->opened++;
668 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500669 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500670 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500671 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400672 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400673 return ret;
674}
675
Christoph Hellwig97288f22008-12-02 06:36:09 -0500676int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400677 struct btrfs_fs_devices **fs_devices_ret)
678{
679 struct btrfs_super_block *disk_super;
680 struct block_device *bdev;
681 struct buffer_head *bh;
682 int ret;
683 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400684 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685
686 mutex_lock(&uuid_mutex);
687
Chris Mason15916de2008-11-19 21:17:22 -0500688 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400689
690 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400691 ret = PTR_ERR(bdev);
692 goto error;
693 }
694
695 ret = set_blocksize(bdev, 4096);
696 if (ret)
697 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500698 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400699 if (!bh) {
700 ret = -EIO;
701 goto error_close;
702 }
703 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000704 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400705 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400706 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500707 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400708 else {
709 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500710 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400711 *(unsigned long long *)disk_super->fsid,
712 *(unsigned long long *)(disk_super->fsid + 8));
713 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500714 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500715 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
717
Chris Mason8a4b83c2008-03-24 15:02:07 -0400718 brelse(bh);
719error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500720 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400721error:
722 mutex_unlock(&uuid_mutex);
723 return ret;
724}
Chris Mason0b86a832008-03-24 15:01:56 -0400725
726/*
727 * this uses a pretty simple search, the expectation is that it is
728 * called very infrequently and that a given device has a small number
729 * of extents
730 */
Josef Bacikba1bf482009-09-11 16:11:19 -0400731int find_free_dev_extent(struct btrfs_trans_handle *trans,
732 struct btrfs_device *device, u64 num_bytes,
733 u64 *start, u64 *max_avail)
Chris Mason0b86a832008-03-24 15:01:56 -0400734{
735 struct btrfs_key key;
736 struct btrfs_root *root = device->dev_root;
737 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500738 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400739 u64 hole_size = 0;
740 u64 last_byte = 0;
741 u64 search_start = 0;
742 u64 search_end = device->total_bytes;
743 int ret;
744 int slot = 0;
745 int start_found;
746 struct extent_buffer *l;
747
Yan Zheng2b820322008-11-17 21:11:30 -0500748 path = btrfs_alloc_path();
749 if (!path)
750 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400751 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500752 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400753
754 /* FIXME use last free of some kind */
755
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 /* we don't want to overwrite the superblock on the drive,
757 * so we make sure to start at an offset of at least 1MB
758 */
759 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400760
761 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
762 search_start = max(root->fs_info->alloc_start, search_start);
763
Chris Mason0b86a832008-03-24 15:01:56 -0400764 key.objectid = device->devid;
765 key.offset = search_start;
766 key.type = BTRFS_DEV_EXTENT_KEY;
767 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
768 if (ret < 0)
769 goto error;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400770 if (ret > 0) {
771 ret = btrfs_previous_item(root, path, key.objectid, key.type);
772 if (ret < 0)
773 goto error;
774 if (ret > 0)
775 start_found = 1;
776 }
Chris Mason0b86a832008-03-24 15:01:56 -0400777 l = path->nodes[0];
778 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
779 while (1) {
780 l = path->nodes[0];
781 slot = path->slots[0];
782 if (slot >= btrfs_header_nritems(l)) {
783 ret = btrfs_next_leaf(root, path);
784 if (ret == 0)
785 continue;
786 if (ret < 0)
787 goto error;
788no_more_items:
789 if (!start_found) {
790 if (search_start >= search_end) {
791 ret = -ENOSPC;
792 goto error;
793 }
794 *start = search_start;
795 start_found = 1;
796 goto check_pending;
797 }
798 *start = last_byte > search_start ?
799 last_byte : search_start;
800 if (search_end <= *start) {
801 ret = -ENOSPC;
802 goto error;
803 }
804 goto check_pending;
805 }
806 btrfs_item_key_to_cpu(l, &key, slot);
807
808 if (key.objectid < device->devid)
809 goto next;
810
811 if (key.objectid > device->devid)
812 goto no_more_items;
813
814 if (key.offset >= search_start && key.offset > last_byte &&
815 start_found) {
816 if (last_byte < search_start)
817 last_byte = search_start;
818 hole_size = key.offset - last_byte;
Chris Mason9779b722009-07-24 16:41:41 -0400819
820 if (hole_size > *max_avail)
821 *max_avail = hole_size;
822
Chris Mason0b86a832008-03-24 15:01:56 -0400823 if (key.offset > last_byte &&
824 hole_size >= num_bytes) {
825 *start = last_byte;
826 goto check_pending;
827 }
828 }
Chris Masond3977122009-01-05 21:25:51 -0500829 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400830 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400831
832 start_found = 1;
833 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
834 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
835next:
836 path->slots[0]++;
837 cond_resched();
838 }
839check_pending:
840 /* we have to make sure we didn't find an extent that has already
841 * been allocated by the map tree or the original allocation
842 */
Chris Mason0b86a832008-03-24 15:01:56 -0400843 BUG_ON(*start < search_start);
844
Chris Mason6324fbf2008-03-24 15:01:59 -0400845 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400846 ret = -ENOSPC;
847 goto error;
848 }
849 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500850 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400851
852error:
Yan Zheng2b820322008-11-17 21:11:30 -0500853 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400854 return ret;
855}
856
Christoph Hellwigb2950862008-12-02 09:54:17 -0500857static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400858 struct btrfs_device *device,
859 u64 start)
860{
861 int ret;
862 struct btrfs_path *path;
863 struct btrfs_root *root = device->dev_root;
864 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400865 struct btrfs_key found_key;
866 struct extent_buffer *leaf = NULL;
867 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400868
869 path = btrfs_alloc_path();
870 if (!path)
871 return -ENOMEM;
872
873 key.objectid = device->devid;
874 key.offset = start;
875 key.type = BTRFS_DEV_EXTENT_KEY;
876
877 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400878 if (ret > 0) {
879 ret = btrfs_previous_item(root, path, key.objectid,
880 BTRFS_DEV_EXTENT_KEY);
881 BUG_ON(ret);
882 leaf = path->nodes[0];
883 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
884 extent = btrfs_item_ptr(leaf, path->slots[0],
885 struct btrfs_dev_extent);
886 BUG_ON(found_key.offset > start || found_key.offset +
887 btrfs_dev_extent_length(leaf, extent) < start);
888 ret = 0;
889 } else if (ret == 0) {
890 leaf = path->nodes[0];
891 extent = btrfs_item_ptr(leaf, path->slots[0],
892 struct btrfs_dev_extent);
893 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400894 BUG_ON(ret);
895
Chris Masondfe25022008-05-13 13:46:40 -0400896 if (device->bytes_used > 0)
897 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400898 ret = btrfs_del_item(trans, root, path);
899 BUG_ON(ret);
900
901 btrfs_free_path(path);
902 return ret;
903}
904
Yan Zheng2b820322008-11-17 21:11:30 -0500905int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400906 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400907 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500908 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400909{
910 int ret;
911 struct btrfs_path *path;
912 struct btrfs_root *root = device->dev_root;
913 struct btrfs_dev_extent *extent;
914 struct extent_buffer *leaf;
915 struct btrfs_key key;
916
Chris Masondfe25022008-05-13 13:46:40 -0400917 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400918 path = btrfs_alloc_path();
919 if (!path)
920 return -ENOMEM;
921
Chris Mason0b86a832008-03-24 15:01:56 -0400922 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500923 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400924 key.type = BTRFS_DEV_EXTENT_KEY;
925 ret = btrfs_insert_empty_item(trans, root, path, &key,
926 sizeof(*extent));
927 BUG_ON(ret);
928
929 leaf = path->nodes[0];
930 extent = btrfs_item_ptr(leaf, path->slots[0],
931 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400932 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
933 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
934 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
935
936 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
937 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
938 BTRFS_UUID_SIZE);
939
Chris Mason0b86a832008-03-24 15:01:56 -0400940 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
941 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400942 btrfs_free_path(path);
943 return ret;
944}
945
Chris Masona1b32a52008-09-05 16:09:51 -0400946static noinline int find_next_chunk(struct btrfs_root *root,
947 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400948{
949 struct btrfs_path *path;
950 int ret;
951 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400952 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400953 struct btrfs_key found_key;
954
955 path = btrfs_alloc_path();
956 BUG_ON(!path);
957
Chris Masone17cade2008-04-15 15:41:47 -0400958 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400959 key.offset = (u64)-1;
960 key.type = BTRFS_CHUNK_ITEM_KEY;
961
962 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
963 if (ret < 0)
964 goto error;
965
966 BUG_ON(ret == 0);
967
968 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
969 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400970 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400971 } else {
972 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
973 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400974 if (found_key.objectid != objectid)
975 *offset = 0;
976 else {
977 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
978 struct btrfs_chunk);
979 *offset = found_key.offset +
980 btrfs_chunk_length(path->nodes[0], chunk);
981 }
Chris Mason0b86a832008-03-24 15:01:56 -0400982 }
983 ret = 0;
984error:
985 btrfs_free_path(path);
986 return ret;
987}
988
Yan Zheng2b820322008-11-17 21:11:30 -0500989static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400990{
991 int ret;
992 struct btrfs_key key;
993 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500994 struct btrfs_path *path;
995
996 root = root->fs_info->chunk_root;
997
998 path = btrfs_alloc_path();
999 if (!path)
1000 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001001
1002 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1003 key.type = BTRFS_DEV_ITEM_KEY;
1004 key.offset = (u64)-1;
1005
1006 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1007 if (ret < 0)
1008 goto error;
1009
1010 BUG_ON(ret == 0);
1011
1012 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1013 BTRFS_DEV_ITEM_KEY);
1014 if (ret) {
1015 *objectid = 1;
1016 } else {
1017 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1018 path->slots[0]);
1019 *objectid = found_key.offset + 1;
1020 }
1021 ret = 0;
1022error:
Yan Zheng2b820322008-11-17 21:11:30 -05001023 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001024 return ret;
1025}
1026
1027/*
1028 * the device information is stored in the chunk root
1029 * the btrfs_device struct should be fully filled in
1030 */
1031int btrfs_add_device(struct btrfs_trans_handle *trans,
1032 struct btrfs_root *root,
1033 struct btrfs_device *device)
1034{
1035 int ret;
1036 struct btrfs_path *path;
1037 struct btrfs_dev_item *dev_item;
1038 struct extent_buffer *leaf;
1039 struct btrfs_key key;
1040 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001041
1042 root = root->fs_info->chunk_root;
1043
1044 path = btrfs_alloc_path();
1045 if (!path)
1046 return -ENOMEM;
1047
Chris Mason0b86a832008-03-24 15:01:56 -04001048 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1049 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001050 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001051
1052 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001053 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001054 if (ret)
1055 goto out;
1056
1057 leaf = path->nodes[0];
1058 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1059
1060 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001061 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001062 btrfs_set_device_type(leaf, dev_item, device->type);
1063 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1064 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1065 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001066 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1067 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001068 btrfs_set_device_group(leaf, dev_item, 0);
1069 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1070 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001071 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001072
Chris Mason0b86a832008-03-24 15:01:56 -04001073 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001074 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001075 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1076 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001077 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001078
Yan Zheng2b820322008-11-17 21:11:30 -05001079 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001080out:
1081 btrfs_free_path(path);
1082 return ret;
1083}
Chris Mason8f18cf12008-04-25 16:53:30 -04001084
Chris Masona061fc82008-05-07 11:43:44 -04001085static int btrfs_rm_dev_item(struct btrfs_root *root,
1086 struct btrfs_device *device)
1087{
1088 int ret;
1089 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001090 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001091 struct btrfs_trans_handle *trans;
1092
1093 root = root->fs_info->chunk_root;
1094
1095 path = btrfs_alloc_path();
1096 if (!path)
1097 return -ENOMEM;
1098
1099 trans = btrfs_start_transaction(root, 1);
1100 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1101 key.type = BTRFS_DEV_ITEM_KEY;
1102 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001103 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001104
1105 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1106 if (ret < 0)
1107 goto out;
1108
1109 if (ret > 0) {
1110 ret = -ENOENT;
1111 goto out;
1112 }
1113
1114 ret = btrfs_del_item(trans, root, path);
1115 if (ret)
1116 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001117out:
1118 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001119 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001120 btrfs_commit_transaction(trans, root);
1121 return ret;
1122}
1123
1124int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1125{
1126 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001127 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001128 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001129 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001130 struct btrfs_super_block *disk_super;
1131 u64 all_avail;
1132 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001133 u64 num_devices;
1134 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001135 int ret = 0;
1136
Chris Masona061fc82008-05-07 11:43:44 -04001137 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001138 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001139
1140 all_avail = root->fs_info->avail_data_alloc_bits |
1141 root->fs_info->avail_system_alloc_bits |
1142 root->fs_info->avail_metadata_alloc_bits;
1143
1144 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001145 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001146 printk(KERN_ERR "btrfs: unable to go below four devices "
1147 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001148 ret = -EINVAL;
1149 goto out;
1150 }
1151
1152 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001153 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001154 printk(KERN_ERR "btrfs: unable to go below two "
1155 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001156 ret = -EINVAL;
1157 goto out;
1158 }
1159
Chris Masondfe25022008-05-13 13:46:40 -04001160 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001161 struct list_head *devices;
1162 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001163
Chris Masondfe25022008-05-13 13:46:40 -04001164 device = NULL;
1165 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001166 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001167 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001168 if (tmp->in_fs_metadata && !tmp->bdev) {
1169 device = tmp;
1170 break;
1171 }
1172 }
Chris Masone5e9a522009-06-10 15:17:02 -04001173 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001174 bdev = NULL;
1175 bh = NULL;
1176 disk_super = NULL;
1177 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001178 printk(KERN_ERR "btrfs: no missing devices found to "
1179 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001180 goto out;
1181 }
Chris Masondfe25022008-05-13 13:46:40 -04001182 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001183 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001184 root->fs_info->bdev_holder);
1185 if (IS_ERR(bdev)) {
1186 ret = PTR_ERR(bdev);
1187 goto out;
1188 }
1189
Yan Zheng2b820322008-11-17 21:11:30 -05001190 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001191 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001192 if (!bh) {
1193 ret = -EIO;
1194 goto error_close;
1195 }
1196 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001197 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001198 dev_uuid = disk_super->dev_item.uuid;
1199 device = btrfs_find_device(root, devid, dev_uuid,
1200 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001201 if (!device) {
1202 ret = -ENOENT;
1203 goto error_brelse;
1204 }
Chris Masondfe25022008-05-13 13:46:40 -04001205 }
Yan Zheng2b820322008-11-17 21:11:30 -05001206
1207 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001208 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1209 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001210 ret = -EINVAL;
1211 goto error_brelse;
1212 }
1213
1214 if (device->writeable) {
1215 list_del_init(&device->dev_alloc_list);
1216 root->fs_info->fs_devices->rw_devices--;
1217 }
Chris Masona061fc82008-05-07 11:43:44 -04001218
1219 ret = btrfs_shrink_device(device, 0);
1220 if (ret)
1221 goto error_brelse;
1222
Chris Masona061fc82008-05-07 11:43:44 -04001223 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1224 if (ret)
1225 goto error_brelse;
1226
Yan Zheng2b820322008-11-17 21:11:30 -05001227 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001228
1229 /*
1230 * the device list mutex makes sure that we don't change
1231 * the device list while someone else is writing out all
1232 * the device supers.
1233 */
1234 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001235 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001236 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1237
Yan Zhenge4404d62008-12-12 10:03:26 -05001238 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001239
1240 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1241 struct btrfs_device, dev_list);
1242 if (device->bdev == root->fs_info->sb->s_bdev)
1243 root->fs_info->sb->s_bdev = next_device->bdev;
1244 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1245 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1246
Yan Zhenge4404d62008-12-12 10:03:26 -05001247 if (device->bdev) {
1248 close_bdev_exclusive(device->bdev, device->mode);
1249 device->bdev = NULL;
1250 device->fs_devices->open_devices--;
1251 }
1252
Yan Zheng2b820322008-11-17 21:11:30 -05001253 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1254 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1255
Yan Zhenge4404d62008-12-12 10:03:26 -05001256 if (device->fs_devices->open_devices == 0) {
1257 struct btrfs_fs_devices *fs_devices;
1258 fs_devices = root->fs_info->fs_devices;
1259 while (fs_devices) {
1260 if (fs_devices->seed == device->fs_devices)
1261 break;
1262 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001263 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001264 fs_devices->seed = device->fs_devices->seed;
1265 device->fs_devices->seed = NULL;
1266 __btrfs_close_devices(device->fs_devices);
1267 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001268 }
1269
1270 /*
1271 * at this point, the device is zero sized. We want to
1272 * remove it from the devices list and zero out the old super
1273 */
1274 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001275 /* make sure this device isn't detected as part of
1276 * the FS anymore
1277 */
1278 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1279 set_buffer_dirty(bh);
1280 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001281 }
Chris Masona061fc82008-05-07 11:43:44 -04001282
Chris Masona061fc82008-05-07 11:43:44 -04001283 kfree(device->name);
1284 kfree(device);
1285 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001286
1287error_brelse:
1288 brelse(bh);
1289error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001290 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001291 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001292out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001293 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001294 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001295 return ret;
1296}
1297
Yan Zheng2b820322008-11-17 21:11:30 -05001298/*
1299 * does all the dirty work required for changing file system's UUID.
1300 */
1301static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1302 struct btrfs_root *root)
1303{
1304 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1305 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001306 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001307 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1308 struct btrfs_device *device;
1309 u64 super_flags;
1310
1311 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001312 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001313 return -EINVAL;
1314
Yan Zhenge4404d62008-12-12 10:03:26 -05001315 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1316 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001317 return -ENOMEM;
1318
Yan Zhenge4404d62008-12-12 10:03:26 -05001319 old_devices = clone_fs_devices(fs_devices);
1320 if (IS_ERR(old_devices)) {
1321 kfree(seed_devices);
1322 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001323 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001324
Yan Zheng2b820322008-11-17 21:11:30 -05001325 list_add(&old_devices->list, &fs_uuids);
1326
Yan Zhenge4404d62008-12-12 10:03:26 -05001327 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1328 seed_devices->opened = 1;
1329 INIT_LIST_HEAD(&seed_devices->devices);
1330 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001331 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001332 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1333 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1334 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1335 device->fs_devices = seed_devices;
1336 }
1337
Yan Zheng2b820322008-11-17 21:11:30 -05001338 fs_devices->seeding = 0;
1339 fs_devices->num_devices = 0;
1340 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001341 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001342
1343 generate_random_uuid(fs_devices->fsid);
1344 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1345 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1346 super_flags = btrfs_super_flags(disk_super) &
1347 ~BTRFS_SUPER_FLAG_SEEDING;
1348 btrfs_set_super_flags(disk_super, super_flags);
1349
1350 return 0;
1351}
1352
1353/*
1354 * strore the expected generation for seed devices in device items.
1355 */
1356static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1357 struct btrfs_root *root)
1358{
1359 struct btrfs_path *path;
1360 struct extent_buffer *leaf;
1361 struct btrfs_dev_item *dev_item;
1362 struct btrfs_device *device;
1363 struct btrfs_key key;
1364 u8 fs_uuid[BTRFS_UUID_SIZE];
1365 u8 dev_uuid[BTRFS_UUID_SIZE];
1366 u64 devid;
1367 int ret;
1368
1369 path = btrfs_alloc_path();
1370 if (!path)
1371 return -ENOMEM;
1372
1373 root = root->fs_info->chunk_root;
1374 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1375 key.offset = 0;
1376 key.type = BTRFS_DEV_ITEM_KEY;
1377
1378 while (1) {
1379 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1380 if (ret < 0)
1381 goto error;
1382
1383 leaf = path->nodes[0];
1384next_slot:
1385 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1386 ret = btrfs_next_leaf(root, path);
1387 if (ret > 0)
1388 break;
1389 if (ret < 0)
1390 goto error;
1391 leaf = path->nodes[0];
1392 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1393 btrfs_release_path(root, path);
1394 continue;
1395 }
1396
1397 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1398 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1399 key.type != BTRFS_DEV_ITEM_KEY)
1400 break;
1401
1402 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1403 struct btrfs_dev_item);
1404 devid = btrfs_device_id(leaf, dev_item);
1405 read_extent_buffer(leaf, dev_uuid,
1406 (unsigned long)btrfs_device_uuid(dev_item),
1407 BTRFS_UUID_SIZE);
1408 read_extent_buffer(leaf, fs_uuid,
1409 (unsigned long)btrfs_device_fsid(dev_item),
1410 BTRFS_UUID_SIZE);
1411 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1412 BUG_ON(!device);
1413
1414 if (device->fs_devices->seeding) {
1415 btrfs_set_device_generation(leaf, dev_item,
1416 device->generation);
1417 btrfs_mark_buffer_dirty(leaf);
1418 }
1419
1420 path->slots[0]++;
1421 goto next_slot;
1422 }
1423 ret = 0;
1424error:
1425 btrfs_free_path(path);
1426 return ret;
1427}
1428
Chris Mason788f20e2008-04-28 15:29:42 -04001429int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1430{
1431 struct btrfs_trans_handle *trans;
1432 struct btrfs_device *device;
1433 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001434 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001435 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001436 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001437 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001438 int ret = 0;
1439
Yan Zheng2b820322008-11-17 21:11:30 -05001440 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1441 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001442
Chris Mason15916de2008-11-19 21:17:22 -05001443 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001444 if (IS_ERR(bdev))
1445 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001446
Yan Zheng2b820322008-11-17 21:11:30 -05001447 if (root->fs_info->fs_devices->seeding) {
1448 seeding_dev = 1;
1449 down_write(&sb->s_umount);
1450 mutex_lock(&uuid_mutex);
1451 }
1452
Chris Mason8c8bee12008-09-29 11:19:10 -04001453 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001454 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001455
Chris Mason788f20e2008-04-28 15:29:42 -04001456 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001457 /*
1458 * we have the volume lock, so we don't need the extra
1459 * device list mutex while reading the list here.
1460 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001461 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001462 if (device->bdev == bdev) {
1463 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001464 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001465 }
1466 }
1467
1468 device = kzalloc(sizeof(*device), GFP_NOFS);
1469 if (!device) {
1470 /* we can safely leave the fs_devices entry around */
1471 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001472 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001473 }
1474
Chris Mason788f20e2008-04-28 15:29:42 -04001475 device->name = kstrdup(device_path, GFP_NOFS);
1476 if (!device->name) {
1477 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001478 ret = -ENOMEM;
1479 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001480 }
Yan Zheng2b820322008-11-17 21:11:30 -05001481
1482 ret = find_next_devid(root, &device->devid);
1483 if (ret) {
1484 kfree(device);
1485 goto error;
1486 }
1487
1488 trans = btrfs_start_transaction(root, 1);
1489 lock_chunks(root);
1490
1491 device->barriers = 1;
1492 device->writeable = 1;
1493 device->work.func = pending_bios_fn;
1494 generate_random_uuid(device->uuid);
1495 spin_lock_init(&device->io_lock);
1496 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001497 device->io_width = root->sectorsize;
1498 device->io_align = root->sectorsize;
1499 device->sector_size = root->sectorsize;
1500 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001501 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001502 device->dev_root = root->fs_info->dev_root;
1503 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001504 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001505 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001506 set_blocksize(device->bdev, 4096);
1507
Yan Zheng2b820322008-11-17 21:11:30 -05001508 if (seeding_dev) {
1509 sb->s_flags &= ~MS_RDONLY;
1510 ret = btrfs_prepare_sprout(trans, root);
1511 BUG_ON(ret);
1512 }
1513
1514 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001515
1516 /*
1517 * we don't want write_supers to jump in here with our device
1518 * half setup
1519 */
1520 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001521 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1522 list_add(&device->dev_alloc_list,
1523 &root->fs_info->fs_devices->alloc_list);
1524 root->fs_info->fs_devices->num_devices++;
1525 root->fs_info->fs_devices->open_devices++;
1526 root->fs_info->fs_devices->rw_devices++;
1527 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1528
Chris Masonc2898112009-06-10 09:51:32 -04001529 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1530 root->fs_info->fs_devices->rotating = 1;
1531
Chris Mason788f20e2008-04-28 15:29:42 -04001532 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1533 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1534 total_bytes + device->total_bytes);
1535
1536 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1537 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1538 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001539 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001540
Yan Zheng2b820322008-11-17 21:11:30 -05001541 if (seeding_dev) {
1542 ret = init_first_rw_device(trans, root, device);
1543 BUG_ON(ret);
1544 ret = btrfs_finish_sprout(trans, root);
1545 BUG_ON(ret);
1546 } else {
1547 ret = btrfs_add_device(trans, root, device);
1548 }
1549
Chris Mason913d9522009-03-10 13:17:18 -04001550 /*
1551 * we've got more storage, clear any full flags on the space
1552 * infos
1553 */
1554 btrfs_clear_space_info_full(root->fs_info);
1555
Chris Mason7d9eb122008-07-08 14:19:17 -04001556 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001557 btrfs_commit_transaction(trans, root);
1558
1559 if (seeding_dev) {
1560 mutex_unlock(&uuid_mutex);
1561 up_write(&sb->s_umount);
1562
1563 ret = btrfs_relocate_sys_chunks(root);
1564 BUG_ON(ret);
1565 }
1566out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001567 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001568 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001569error:
Chris Mason15916de2008-11-19 21:17:22 -05001570 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001571 if (seeding_dev) {
1572 mutex_unlock(&uuid_mutex);
1573 up_write(&sb->s_umount);
1574 }
Chris Mason788f20e2008-04-28 15:29:42 -04001575 goto out;
1576}
1577
Chris Masond3977122009-01-05 21:25:51 -05001578static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1579 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001580{
1581 int ret;
1582 struct btrfs_path *path;
1583 struct btrfs_root *root;
1584 struct btrfs_dev_item *dev_item;
1585 struct extent_buffer *leaf;
1586 struct btrfs_key key;
1587
1588 root = device->dev_root->fs_info->chunk_root;
1589
1590 path = btrfs_alloc_path();
1591 if (!path)
1592 return -ENOMEM;
1593
1594 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1595 key.type = BTRFS_DEV_ITEM_KEY;
1596 key.offset = device->devid;
1597
1598 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1599 if (ret < 0)
1600 goto out;
1601
1602 if (ret > 0) {
1603 ret = -ENOENT;
1604 goto out;
1605 }
1606
1607 leaf = path->nodes[0];
1608 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1609
1610 btrfs_set_device_id(leaf, dev_item, device->devid);
1611 btrfs_set_device_type(leaf, dev_item, device->type);
1612 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1613 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1614 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001615 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001616 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1617 btrfs_mark_buffer_dirty(leaf);
1618
1619out:
1620 btrfs_free_path(path);
1621 return ret;
1622}
1623
Chris Mason7d9eb122008-07-08 14:19:17 -04001624static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001625 struct btrfs_device *device, u64 new_size)
1626{
1627 struct btrfs_super_block *super_copy =
1628 &device->dev_root->fs_info->super_copy;
1629 u64 old_total = btrfs_super_total_bytes(super_copy);
1630 u64 diff = new_size - device->total_bytes;
1631
Yan Zheng2b820322008-11-17 21:11:30 -05001632 if (!device->writeable)
1633 return -EACCES;
1634 if (new_size <= device->total_bytes)
1635 return -EINVAL;
1636
Chris Mason8f18cf12008-04-25 16:53:30 -04001637 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001638 device->fs_devices->total_rw_bytes += diff;
1639
1640 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001641 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001642 btrfs_clear_space_info_full(device->dev_root->fs_info);
1643
Chris Mason8f18cf12008-04-25 16:53:30 -04001644 return btrfs_update_device(trans, device);
1645}
1646
Chris Mason7d9eb122008-07-08 14:19:17 -04001647int btrfs_grow_device(struct btrfs_trans_handle *trans,
1648 struct btrfs_device *device, u64 new_size)
1649{
1650 int ret;
1651 lock_chunks(device->dev_root);
1652 ret = __btrfs_grow_device(trans, device, new_size);
1653 unlock_chunks(device->dev_root);
1654 return ret;
1655}
1656
Chris Mason8f18cf12008-04-25 16:53:30 -04001657static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1658 struct btrfs_root *root,
1659 u64 chunk_tree, u64 chunk_objectid,
1660 u64 chunk_offset)
1661{
1662 int ret;
1663 struct btrfs_path *path;
1664 struct btrfs_key key;
1665
1666 root = root->fs_info->chunk_root;
1667 path = btrfs_alloc_path();
1668 if (!path)
1669 return -ENOMEM;
1670
1671 key.objectid = chunk_objectid;
1672 key.offset = chunk_offset;
1673 key.type = BTRFS_CHUNK_ITEM_KEY;
1674
1675 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1676 BUG_ON(ret);
1677
1678 ret = btrfs_del_item(trans, root, path);
1679 BUG_ON(ret);
1680
1681 btrfs_free_path(path);
1682 return 0;
1683}
1684
Christoph Hellwigb2950862008-12-02 09:54:17 -05001685static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001686 chunk_offset)
1687{
1688 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1689 struct btrfs_disk_key *disk_key;
1690 struct btrfs_chunk *chunk;
1691 u8 *ptr;
1692 int ret = 0;
1693 u32 num_stripes;
1694 u32 array_size;
1695 u32 len = 0;
1696 u32 cur;
1697 struct btrfs_key key;
1698
1699 array_size = btrfs_super_sys_array_size(super_copy);
1700
1701 ptr = super_copy->sys_chunk_array;
1702 cur = 0;
1703
1704 while (cur < array_size) {
1705 disk_key = (struct btrfs_disk_key *)ptr;
1706 btrfs_disk_key_to_cpu(&key, disk_key);
1707
1708 len = sizeof(*disk_key);
1709
1710 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1711 chunk = (struct btrfs_chunk *)(ptr + len);
1712 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1713 len += btrfs_chunk_item_size(num_stripes);
1714 } else {
1715 ret = -EIO;
1716 break;
1717 }
1718 if (key.objectid == chunk_objectid &&
1719 key.offset == chunk_offset) {
1720 memmove(ptr, ptr + len, array_size - (cur + len));
1721 array_size -= len;
1722 btrfs_set_super_sys_array_size(super_copy, array_size);
1723 } else {
1724 ptr += len;
1725 cur += len;
1726 }
1727 }
1728 return ret;
1729}
1730
Christoph Hellwigb2950862008-12-02 09:54:17 -05001731static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001732 u64 chunk_tree, u64 chunk_objectid,
1733 u64 chunk_offset)
1734{
1735 struct extent_map_tree *em_tree;
1736 struct btrfs_root *extent_root;
1737 struct btrfs_trans_handle *trans;
1738 struct extent_map *em;
1739 struct map_lookup *map;
1740 int ret;
1741 int i;
1742
1743 root = root->fs_info->chunk_root;
1744 extent_root = root->fs_info->extent_root;
1745 em_tree = &root->fs_info->mapping_tree.map_tree;
1746
Josef Bacikba1bf482009-09-11 16:11:19 -04001747 ret = btrfs_can_relocate(extent_root, chunk_offset);
1748 if (ret)
1749 return -ENOSPC;
1750
Chris Mason8f18cf12008-04-25 16:53:30 -04001751 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001752 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001753 BUG_ON(ret);
1754
1755 trans = btrfs_start_transaction(root, 1);
1756 BUG_ON(!trans);
1757
Chris Mason7d9eb122008-07-08 14:19:17 -04001758 lock_chunks(root);
1759
Chris Mason8f18cf12008-04-25 16:53:30 -04001760 /*
1761 * step two, delete the device extents and the
1762 * chunk tree entries
1763 */
Chris Mason890871b2009-09-02 16:24:52 -04001764 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001765 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001766 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001767
Chris Masona061fc82008-05-07 11:43:44 -04001768 BUG_ON(em->start > chunk_offset ||
1769 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001770 map = (struct map_lookup *)em->bdev;
1771
1772 for (i = 0; i < map->num_stripes; i++) {
1773 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1774 map->stripes[i].physical);
1775 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001776
Chris Masondfe25022008-05-13 13:46:40 -04001777 if (map->stripes[i].dev) {
1778 ret = btrfs_update_device(trans, map->stripes[i].dev);
1779 BUG_ON(ret);
1780 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001781 }
1782 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1783 chunk_offset);
1784
1785 BUG_ON(ret);
1786
1787 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1788 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1789 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001790 }
1791
Zheng Yan1a40e232008-09-26 10:09:34 -04001792 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1793 BUG_ON(ret);
1794
Chris Mason890871b2009-09-02 16:24:52 -04001795 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001796 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001797 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001798
Chris Mason8f18cf12008-04-25 16:53:30 -04001799 kfree(map);
1800 em->bdev = NULL;
1801
1802 /* once for the tree */
1803 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001804 /* once for us */
1805 free_extent_map(em);
1806
Chris Mason7d9eb122008-07-08 14:19:17 -04001807 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001808 btrfs_end_transaction(trans, root);
1809 return 0;
1810}
1811
Yan Zheng2b820322008-11-17 21:11:30 -05001812static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1813{
1814 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1815 struct btrfs_path *path;
1816 struct extent_buffer *leaf;
1817 struct btrfs_chunk *chunk;
1818 struct btrfs_key key;
1819 struct btrfs_key found_key;
1820 u64 chunk_tree = chunk_root->root_key.objectid;
1821 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001822 bool retried = false;
1823 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001824 int ret;
1825
1826 path = btrfs_alloc_path();
1827 if (!path)
1828 return -ENOMEM;
1829
Josef Bacikba1bf482009-09-11 16:11:19 -04001830again:
Yan Zheng2b820322008-11-17 21:11:30 -05001831 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1832 key.offset = (u64)-1;
1833 key.type = BTRFS_CHUNK_ITEM_KEY;
1834
1835 while (1) {
1836 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1837 if (ret < 0)
1838 goto error;
1839 BUG_ON(ret == 0);
1840
1841 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1842 key.type);
1843 if (ret < 0)
1844 goto error;
1845 if (ret > 0)
1846 break;
1847
1848 leaf = path->nodes[0];
1849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1850
1851 chunk = btrfs_item_ptr(leaf, path->slots[0],
1852 struct btrfs_chunk);
1853 chunk_type = btrfs_chunk_type(leaf, chunk);
1854 btrfs_release_path(chunk_root, path);
1855
1856 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1857 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1858 found_key.objectid,
1859 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001860 if (ret == -ENOSPC)
1861 failed++;
1862 else if (ret)
1863 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001864 }
1865
1866 if (found_key.offset == 0)
1867 break;
1868 key.offset = found_key.offset - 1;
1869 }
1870 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001871 if (failed && !retried) {
1872 failed = 0;
1873 retried = true;
1874 goto again;
1875 } else if (failed && retried) {
1876 WARN_ON(1);
1877 ret = -ENOSPC;
1878 }
Yan Zheng2b820322008-11-17 21:11:30 -05001879error:
1880 btrfs_free_path(path);
1881 return ret;
1882}
1883
Chris Masonec44a352008-04-28 15:29:52 -04001884static u64 div_factor(u64 num, int factor)
1885{
1886 if (factor == 10)
1887 return num;
1888 num *= factor;
1889 do_div(num, 10);
1890 return num;
1891}
1892
Chris Masonec44a352008-04-28 15:29:52 -04001893int btrfs_balance(struct btrfs_root *dev_root)
1894{
1895 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001896 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1897 struct btrfs_device *device;
1898 u64 old_size;
1899 u64 size_to_free;
1900 struct btrfs_path *path;
1901 struct btrfs_key key;
1902 struct btrfs_chunk *chunk;
1903 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1904 struct btrfs_trans_handle *trans;
1905 struct btrfs_key found_key;
1906
Yan Zheng2b820322008-11-17 21:11:30 -05001907 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1908 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001909
Chris Mason7d9eb122008-07-08 14:19:17 -04001910 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001911 dev_root = dev_root->fs_info->dev_root;
1912
Chris Masonec44a352008-04-28 15:29:52 -04001913 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001914 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001915 old_size = device->total_bytes;
1916 size_to_free = div_factor(old_size, 1);
1917 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001918 if (!device->writeable ||
1919 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001920 continue;
1921
1922 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04001923 if (ret == -ENOSPC)
1924 break;
Chris Masonec44a352008-04-28 15:29:52 -04001925 BUG_ON(ret);
1926
1927 trans = btrfs_start_transaction(dev_root, 1);
1928 BUG_ON(!trans);
1929
1930 ret = btrfs_grow_device(trans, device, old_size);
1931 BUG_ON(ret);
1932
1933 btrfs_end_transaction(trans, dev_root);
1934 }
1935
1936 /* step two, relocate all the chunks */
1937 path = btrfs_alloc_path();
1938 BUG_ON(!path);
1939
1940 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1941 key.offset = (u64)-1;
1942 key.type = BTRFS_CHUNK_ITEM_KEY;
1943
Chris Masond3977122009-01-05 21:25:51 -05001944 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001945 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1946 if (ret < 0)
1947 goto error;
1948
1949 /*
1950 * this shouldn't happen, it means the last relocate
1951 * failed
1952 */
1953 if (ret == 0)
1954 break;
1955
1956 ret = btrfs_previous_item(chunk_root, path, 0,
1957 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001958 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001959 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001960
Chris Masonec44a352008-04-28 15:29:52 -04001961 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1962 path->slots[0]);
1963 if (found_key.objectid != key.objectid)
1964 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001965
Chris Masonec44a352008-04-28 15:29:52 -04001966 chunk = btrfs_item_ptr(path->nodes[0],
1967 path->slots[0],
1968 struct btrfs_chunk);
Chris Masonec44a352008-04-28 15:29:52 -04001969 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04001970 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04001971 break;
1972
Chris Mason7d9eb122008-07-08 14:19:17 -04001973 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001974 ret = btrfs_relocate_chunk(chunk_root,
1975 chunk_root->root_key.objectid,
1976 found_key.objectid,
1977 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001978 BUG_ON(ret && ret != -ENOSPC);
1979 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04001980 }
1981 ret = 0;
1982error:
1983 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001984 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001985 return ret;
1986}
1987
Chris Mason8f18cf12008-04-25 16:53:30 -04001988/*
1989 * shrinking a device means finding all of the device extents past
1990 * the new size, and then following the back refs to the chunks.
1991 * The chunk relocation code actually frees the device extent
1992 */
1993int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1994{
1995 struct btrfs_trans_handle *trans;
1996 struct btrfs_root *root = device->dev_root;
1997 struct btrfs_dev_extent *dev_extent = NULL;
1998 struct btrfs_path *path;
1999 u64 length;
2000 u64 chunk_tree;
2001 u64 chunk_objectid;
2002 u64 chunk_offset;
2003 int ret;
2004 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002005 int failed = 0;
2006 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002007 struct extent_buffer *l;
2008 struct btrfs_key key;
2009 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2010 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002011 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002012 u64 diff = device->total_bytes - new_size;
2013
Yan Zheng2b820322008-11-17 21:11:30 -05002014 if (new_size >= device->total_bytes)
2015 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002016
2017 path = btrfs_alloc_path();
2018 if (!path)
2019 return -ENOMEM;
2020
Chris Mason8f18cf12008-04-25 16:53:30 -04002021 path->reada = 2;
2022
Chris Mason7d9eb122008-07-08 14:19:17 -04002023 lock_chunks(root);
2024
Chris Mason8f18cf12008-04-25 16:53:30 -04002025 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002026 if (device->writeable)
2027 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002028 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002029
Josef Bacikba1bf482009-09-11 16:11:19 -04002030again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002031 key.objectid = device->devid;
2032 key.offset = (u64)-1;
2033 key.type = BTRFS_DEV_EXTENT_KEY;
2034
2035 while (1) {
2036 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2037 if (ret < 0)
2038 goto done;
2039
2040 ret = btrfs_previous_item(root, path, 0, key.type);
2041 if (ret < 0)
2042 goto done;
2043 if (ret) {
2044 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002045 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002046 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002047 }
2048
2049 l = path->nodes[0];
2050 slot = path->slots[0];
2051 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2052
Josef Bacikba1bf482009-09-11 16:11:19 -04002053 if (key.objectid != device->devid) {
2054 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002055 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002056 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002057
2058 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2059 length = btrfs_dev_extent_length(l, dev_extent);
2060
Josef Bacikba1bf482009-09-11 16:11:19 -04002061 if (key.offset + length <= new_size) {
2062 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002063 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002064 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002065
2066 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2067 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2068 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2069 btrfs_release_path(root, path);
2070
2071 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2072 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002073 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002074 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002075 if (ret == -ENOSPC)
2076 failed++;
2077 key.offset -= 1;
2078 }
2079
2080 if (failed && !retried) {
2081 failed = 0;
2082 retried = true;
2083 goto again;
2084 } else if (failed && retried) {
2085 ret = -ENOSPC;
2086 lock_chunks(root);
2087
2088 device->total_bytes = old_size;
2089 if (device->writeable)
2090 device->fs_devices->total_rw_bytes += diff;
2091 unlock_chunks(root);
2092 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002093 }
2094
Chris Balld6397ba2009-04-27 07:29:03 -04002095 /* Shrinking succeeded, else we would be at "done". */
2096 trans = btrfs_start_transaction(root, 1);
2097 if (!trans) {
2098 ret = -ENOMEM;
2099 goto done;
2100 }
2101 lock_chunks(root);
2102
2103 device->disk_total_bytes = new_size;
2104 /* Now btrfs_update_device() will change the on-disk size. */
2105 ret = btrfs_update_device(trans, device);
2106 if (ret) {
2107 unlock_chunks(root);
2108 btrfs_end_transaction(trans, root);
2109 goto done;
2110 }
2111 WARN_ON(diff > old_total);
2112 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2113 unlock_chunks(root);
2114 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002115done:
2116 btrfs_free_path(path);
2117 return ret;
2118}
2119
Christoph Hellwigb2950862008-12-02 09:54:17 -05002120static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002121 struct btrfs_root *root,
2122 struct btrfs_key *key,
2123 struct btrfs_chunk *chunk, int item_size)
2124{
2125 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2126 struct btrfs_disk_key disk_key;
2127 u32 array_size;
2128 u8 *ptr;
2129
2130 array_size = btrfs_super_sys_array_size(super_copy);
2131 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2132 return -EFBIG;
2133
2134 ptr = super_copy->sys_chunk_array + array_size;
2135 btrfs_cpu_key_to_disk(&disk_key, key);
2136 memcpy(ptr, &disk_key, sizeof(disk_key));
2137 ptr += sizeof(disk_key);
2138 memcpy(ptr, chunk, item_size);
2139 item_size += sizeof(disk_key);
2140 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2141 return 0;
2142}
2143
Chris Masond3977122009-01-05 21:25:51 -05002144static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002145 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002146{
2147 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2148 return calc_size;
2149 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2150 return calc_size * (num_stripes / sub_stripes);
2151 else
2152 return calc_size * num_stripes;
2153}
2154
Yan Zheng2b820322008-11-17 21:11:30 -05002155static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2156 struct btrfs_root *extent_root,
2157 struct map_lookup **map_ret,
2158 u64 *num_bytes, u64 *stripe_size,
2159 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04002160{
Chris Mason593060d2008-03-25 16:50:33 -04002161 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04002162 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05002163 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04002164 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05002165 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002166 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04002167 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05002168 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04002169 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002170 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002171 u64 max_chunk_size = calc_size;
2172 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04002173 u64 avail;
2174 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002175 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04002176 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04002177 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002178 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002179 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002180 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002181 int index;
Chris Mason593060d2008-03-25 16:50:33 -04002182 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002183
Chris Masonec44a352008-04-28 15:29:52 -04002184 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2185 (type & BTRFS_BLOCK_GROUP_DUP)) {
2186 WARN_ON(1);
2187 type &= ~BTRFS_BLOCK_GROUP_DUP;
2188 }
Yan Zheng2b820322008-11-17 21:11:30 -05002189 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04002190 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04002191
Chris Masona40a90a2008-04-18 11:55:51 -04002192 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002193 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04002194 min_stripes = 2;
2195 }
2196 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04002197 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002198 min_stripes = 2;
2199 }
Chris Mason8790d502008-04-03 16:29:03 -04002200 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002201 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002202 if (num_stripes < 2)
2203 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04002204 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002205 }
Chris Mason321aecc2008-04-16 10:49:51 -04002206 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002207 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04002208 if (num_stripes < 4)
2209 return -ENOSPC;
2210 num_stripes &= ~(u32)1;
2211 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002212 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002213 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002214
2215 if (type & BTRFS_BLOCK_GROUP_DATA) {
2216 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002217 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002218 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002219 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002220 min_stripe_size = 32 * 1024 * 1024;
2221 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2222 calc_size = 8 * 1024 * 1024;
2223 max_chunk_size = calc_size * 2;
2224 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002225 }
2226
Yan Zheng2b820322008-11-17 21:11:30 -05002227 /* we don't want a chunk larger than 10% of writeable space */
2228 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2229 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002230
Chris Masona40a90a2008-04-18 11:55:51 -04002231again:
Chris Mason9779b722009-07-24 16:41:41 -04002232 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002233 if (!map || map->num_stripes != num_stripes) {
2234 kfree(map);
2235 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2236 if (!map)
2237 return -ENOMEM;
2238 map->num_stripes = num_stripes;
2239 }
2240
Chris Mason9b3f68b2008-04-18 10:29:51 -04002241 if (calc_size * num_stripes > max_chunk_size) {
2242 calc_size = max_chunk_size;
2243 do_div(calc_size, num_stripes);
2244 do_div(calc_size, stripe_len);
2245 calc_size *= stripe_len;
2246 }
2247 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002248 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002249
Chris Mason9b3f68b2008-04-18 10:29:51 -04002250 do_div(calc_size, stripe_len);
2251 calc_size *= stripe_len;
2252
Yan Zheng2b820322008-11-17 21:11:30 -05002253 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002254 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002255
2256 if (type & BTRFS_BLOCK_GROUP_DUP)
2257 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002258 else
2259 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002260
Josef Bacik0f9dd462008-09-23 13:14:11 -04002261 /*
2262 * we add 1MB because we never use the first 1MB of the device, unless
2263 * we've looped, then we are likely allocating the maximum amount of
2264 * space left already
2265 */
2266 if (!looped)
2267 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002268
Yan Zheng2b820322008-11-17 21:11:30 -05002269 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002270 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002271 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002272 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002273 if (device->total_bytes > device->bytes_used)
2274 avail = device->total_bytes - device->bytes_used;
2275 else
2276 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002277 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002278
Chris Masondfe25022008-05-13 13:46:40 -04002279 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002280 ret = find_free_dev_extent(trans, device,
Chris Mason9779b722009-07-24 16:41:41 -04002281 min_free, &dev_offset,
2282 &max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002283 if (ret == 0) {
2284 list_move_tail(&device->dev_alloc_list,
2285 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002286 map->stripes[index].dev = device;
2287 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002288 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002289 if (type & BTRFS_BLOCK_GROUP_DUP) {
2290 map->stripes[index].dev = device;
2291 map->stripes[index].physical =
2292 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002293 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002294 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002295 }
Chris Masondfe25022008-05-13 13:46:40 -04002296 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002297 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002298 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002299 break;
2300 }
Yan Zheng2b820322008-11-17 21:11:30 -05002301 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002302 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002303 if (index >= min_stripes) {
2304 num_stripes = index;
2305 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2306 num_stripes /= sub_stripes;
2307 num_stripes *= sub_stripes;
2308 }
2309 looped = 1;
2310 goto again;
2311 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002312 if (!looped && max_avail > 0) {
2313 looped = 1;
2314 calc_size = max_avail;
2315 goto again;
2316 }
Yan Zheng2b820322008-11-17 21:11:30 -05002317 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002318 return -ENOSPC;
2319 }
Chris Mason593060d2008-03-25 16:50:33 -04002320 map->sector_size = extent_root->sectorsize;
2321 map->stripe_len = stripe_len;
2322 map->io_align = stripe_len;
2323 map->io_width = stripe_len;
2324 map->type = type;
2325 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002326 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002327
Yan Zheng2b820322008-11-17 21:11:30 -05002328 *map_ret = map;
2329 *stripe_size = calc_size;
2330 *num_bytes = chunk_bytes_by_type(type, calc_size,
2331 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002332
2333 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002334 if (!em) {
2335 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002336 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002337 }
Chris Mason0b86a832008-03-24 15:01:56 -04002338 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002339 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002340 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002341 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002342 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002343
Chris Mason0b86a832008-03-24 15:01:56 -04002344 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002345 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002346 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002347 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002348 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002349 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002350
2351 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2352 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2353 start, *num_bytes);
2354 BUG_ON(ret);
2355
2356 index = 0;
2357 while (index < map->num_stripes) {
2358 device = map->stripes[index].dev;
2359 dev_offset = map->stripes[index].physical;
2360
2361 ret = btrfs_alloc_dev_extent(trans, device,
2362 info->chunk_root->root_key.objectid,
2363 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2364 start, dev_offset, calc_size);
2365 BUG_ON(ret);
2366 index++;
2367 }
2368
2369 return 0;
2370}
2371
2372static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *extent_root,
2374 struct map_lookup *map, u64 chunk_offset,
2375 u64 chunk_size, u64 stripe_size)
2376{
2377 u64 dev_offset;
2378 struct btrfs_key key;
2379 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2380 struct btrfs_device *device;
2381 struct btrfs_chunk *chunk;
2382 struct btrfs_stripe *stripe;
2383 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2384 int index = 0;
2385 int ret;
2386
2387 chunk = kzalloc(item_size, GFP_NOFS);
2388 if (!chunk)
2389 return -ENOMEM;
2390
2391 index = 0;
2392 while (index < map->num_stripes) {
2393 device = map->stripes[index].dev;
2394 device->bytes_used += stripe_size;
2395 ret = btrfs_update_device(trans, device);
2396 BUG_ON(ret);
2397 index++;
2398 }
2399
2400 index = 0;
2401 stripe = &chunk->stripe;
2402 while (index < map->num_stripes) {
2403 device = map->stripes[index].dev;
2404 dev_offset = map->stripes[index].physical;
2405
2406 btrfs_set_stack_stripe_devid(stripe, device->devid);
2407 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2408 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2409 stripe++;
2410 index++;
2411 }
2412
2413 btrfs_set_stack_chunk_length(chunk, chunk_size);
2414 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2415 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2416 btrfs_set_stack_chunk_type(chunk, map->type);
2417 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2418 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2419 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2420 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2421 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2422
2423 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2424 key.type = BTRFS_CHUNK_ITEM_KEY;
2425 key.offset = chunk_offset;
2426
2427 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2428 BUG_ON(ret);
2429
2430 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2431 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2432 item_size);
2433 BUG_ON(ret);
2434 }
2435 kfree(chunk);
2436 return 0;
2437}
2438
2439/*
2440 * Chunk allocation falls into two parts. The first part does works
2441 * that make the new allocated chunk useable, but not do any operation
2442 * that modifies the chunk tree. The second part does the works that
2443 * require modifying the chunk tree. This division is important for the
2444 * bootstrap process of adding storage to a seed btrfs.
2445 */
2446int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2447 struct btrfs_root *extent_root, u64 type)
2448{
2449 u64 chunk_offset;
2450 u64 chunk_size;
2451 u64 stripe_size;
2452 struct map_lookup *map;
2453 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2454 int ret;
2455
2456 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2457 &chunk_offset);
2458 if (ret)
2459 return ret;
2460
2461 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2462 &stripe_size, chunk_offset, type);
2463 if (ret)
2464 return ret;
2465
2466 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2467 chunk_size, stripe_size);
2468 BUG_ON(ret);
2469 return 0;
2470}
2471
Chris Masond3977122009-01-05 21:25:51 -05002472static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002473 struct btrfs_root *root,
2474 struct btrfs_device *device)
2475{
2476 u64 chunk_offset;
2477 u64 sys_chunk_offset;
2478 u64 chunk_size;
2479 u64 sys_chunk_size;
2480 u64 stripe_size;
2481 u64 sys_stripe_size;
2482 u64 alloc_profile;
2483 struct map_lookup *map;
2484 struct map_lookup *sys_map;
2485 struct btrfs_fs_info *fs_info = root->fs_info;
2486 struct btrfs_root *extent_root = fs_info->extent_root;
2487 int ret;
2488
2489 ret = find_next_chunk(fs_info->chunk_root,
2490 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2491 BUG_ON(ret);
2492
2493 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2494 (fs_info->metadata_alloc_profile &
2495 fs_info->avail_metadata_alloc_bits);
2496 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2497
2498 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2499 &stripe_size, chunk_offset, alloc_profile);
2500 BUG_ON(ret);
2501
2502 sys_chunk_offset = chunk_offset + chunk_size;
2503
2504 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2505 (fs_info->system_alloc_profile &
2506 fs_info->avail_system_alloc_bits);
2507 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2508
2509 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2510 &sys_chunk_size, &sys_stripe_size,
2511 sys_chunk_offset, alloc_profile);
2512 BUG_ON(ret);
2513
2514 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2515 BUG_ON(ret);
2516
2517 /*
2518 * Modifying chunk tree needs allocating new blocks from both
2519 * system block group and metadata block group. So we only can
2520 * do operations require modifying the chunk tree after both
2521 * block groups were created.
2522 */
2523 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2524 chunk_size, stripe_size);
2525 BUG_ON(ret);
2526
2527 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2528 sys_chunk_offset, sys_chunk_size,
2529 sys_stripe_size);
2530 BUG_ON(ret);
2531 return 0;
2532}
2533
2534int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2535{
2536 struct extent_map *em;
2537 struct map_lookup *map;
2538 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2539 int readonly = 0;
2540 int i;
2541
Chris Mason890871b2009-09-02 16:24:52 -04002542 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002543 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002544 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002545 if (!em)
2546 return 1;
2547
Josef Bacikf48b9072010-01-27 02:07:59 +00002548 if (btrfs_test_opt(root, DEGRADED)) {
2549 free_extent_map(em);
2550 return 0;
2551 }
2552
Yan Zheng2b820322008-11-17 21:11:30 -05002553 map = (struct map_lookup *)em->bdev;
2554 for (i = 0; i < map->num_stripes; i++) {
2555 if (!map->stripes[i].dev->writeable) {
2556 readonly = 1;
2557 break;
2558 }
2559 }
2560 free_extent_map(em);
2561 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002562}
2563
2564void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2565{
2566 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2567}
2568
2569void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2570{
2571 struct extent_map *em;
2572
Chris Masond3977122009-01-05 21:25:51 -05002573 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002574 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002575 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2576 if (em)
2577 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002578 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002579 if (!em)
2580 break;
2581 kfree(em->bdev);
2582 /* once for us */
2583 free_extent_map(em);
2584 /* once for the tree */
2585 free_extent_map(em);
2586 }
2587}
2588
Chris Masonf1885912008-04-09 16:28:12 -04002589int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2590{
2591 struct extent_map *em;
2592 struct map_lookup *map;
2593 struct extent_map_tree *em_tree = &map_tree->map_tree;
2594 int ret;
2595
Chris Mason890871b2009-09-02 16:24:52 -04002596 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002597 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002598 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002599 BUG_ON(!em);
2600
2601 BUG_ON(em->start > logical || em->start + em->len < logical);
2602 map = (struct map_lookup *)em->bdev;
2603 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2604 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002605 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2606 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002607 else
2608 ret = 1;
2609 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002610 return ret;
2611}
2612
Chris Masondfe25022008-05-13 13:46:40 -04002613static int find_live_mirror(struct map_lookup *map, int first, int num,
2614 int optimal)
2615{
2616 int i;
2617 if (map->stripes[optimal].dev->bdev)
2618 return optimal;
2619 for (i = first; i < first + num; i++) {
2620 if (map->stripes[i].dev->bdev)
2621 return i;
2622 }
2623 /* we couldn't find one that doesn't fail. Just return something
2624 * and the io error handling code will clean up eventually
2625 */
2626 return optimal;
2627}
2628
Chris Masonf2d8d742008-04-21 10:03:05 -04002629static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2630 u64 logical, u64 *length,
2631 struct btrfs_multi_bio **multi_ret,
2632 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002633{
2634 struct extent_map *em;
2635 struct map_lookup *map;
2636 struct extent_map_tree *em_tree = &map_tree->map_tree;
2637 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002638 u64 stripe_offset;
2639 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002640 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002641 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002642 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002643 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002644 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002645 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002646 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002647
Chris Masond3977122009-01-05 21:25:51 -05002648 if (multi_ret && !(rw & (1 << BIO_RW)))
Chris Masoncea9e442008-04-09 16:28:12 -04002649 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002650again:
2651 if (multi_ret) {
2652 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2653 GFP_NOFS);
2654 if (!multi)
2655 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002656
2657 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002658 }
Chris Mason0b86a832008-03-24 15:01:56 -04002659
Chris Mason890871b2009-09-02 16:24:52 -04002660 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002661 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002662 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002663
Jiri Slaby2423fdfb2010-01-06 16:57:22 +00002664 if (!em && unplug_page) {
2665 kfree(multi);
Chris Masonf2d8d742008-04-21 10:03:05 -04002666 return 0;
Jiri Slaby2423fdfb2010-01-06 16:57:22 +00002667 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002668
Chris Mason3b951512008-04-17 11:29:12 -04002669 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002670 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2671 (unsigned long long)logical,
2672 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002673 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002674 }
Chris Mason0b86a832008-03-24 15:01:56 -04002675
2676 BUG_ON(em->start > logical || em->start + em->len < logical);
2677 map = (struct map_lookup *)em->bdev;
2678 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002679
Chris Masonf1885912008-04-09 16:28:12 -04002680 if (mirror_num > map->num_stripes)
2681 mirror_num = 0;
2682
Chris Masoncea9e442008-04-09 16:28:12 -04002683 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002684 if (rw & (1 << BIO_RW)) {
2685 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2686 BTRFS_BLOCK_GROUP_DUP)) {
2687 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002688 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002689 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2690 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002691 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002692 }
2693 }
Chris Masonffbd5172009-04-20 15:50:09 -04002694 if (multi_ret && (rw & (1 << BIO_RW)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002695 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002696 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002697 free_extent_map(em);
2698 kfree(multi);
2699 goto again;
2700 }
Chris Mason593060d2008-03-25 16:50:33 -04002701 stripe_nr = offset;
2702 /*
2703 * stripe_nr counts the total number of stripes we have to stride
2704 * to get to this block
2705 */
2706 do_div(stripe_nr, map->stripe_len);
2707
2708 stripe_offset = stripe_nr * map->stripe_len;
2709 BUG_ON(offset < stripe_offset);
2710
2711 /* stripe_offset is the offset of this block in its stripe*/
2712 stripe_offset = offset - stripe_offset;
2713
Chris Masoncea9e442008-04-09 16:28:12 -04002714 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002715 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002716 BTRFS_BLOCK_GROUP_DUP)) {
2717 /* we limit the length of each bio to what fits in a stripe */
2718 *length = min_t(u64, em->len - offset,
2719 map->stripe_len - stripe_offset);
2720 } else {
2721 *length = em->len - offset;
2722 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002723
2724 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002725 goto out;
2726
Chris Masonf2d8d742008-04-21 10:03:05 -04002727 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002728 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002729 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002730 if (unplug_page || (rw & (1 << BIO_RW)))
2731 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002732 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002733 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002734 else {
2735 stripe_index = find_live_mirror(map, 0,
2736 map->num_stripes,
2737 current->pid % map->num_stripes);
2738 }
Chris Mason2fff7342008-04-29 14:12:09 -04002739
Chris Mason611f0e02008-04-03 16:29:03 -04002740 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002741 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002742 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002743 else if (mirror_num)
2744 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002745
Chris Mason321aecc2008-04-16 10:49:51 -04002746 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2747 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002748
2749 stripe_index = do_div(stripe_nr, factor);
2750 stripe_index *= map->sub_stripes;
2751
Chris Masonf2d8d742008-04-21 10:03:05 -04002752 if (unplug_page || (rw & (1 << BIO_RW)))
2753 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002754 else if (mirror_num)
2755 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002756 else {
2757 stripe_index = find_live_mirror(map, stripe_index,
2758 map->sub_stripes, stripe_index +
2759 current->pid % map->sub_stripes);
2760 }
Chris Mason8790d502008-04-03 16:29:03 -04002761 } else {
2762 /*
2763 * after this do_div call, stripe_nr is the number of stripes
2764 * on this device we have to walk to find the data, and
2765 * stripe_index is the number of our device in the stripe array
2766 */
2767 stripe_index = do_div(stripe_nr, map->num_stripes);
2768 }
Chris Mason593060d2008-03-25 16:50:33 -04002769 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002770
Chris Masonf2d8d742008-04-21 10:03:05 -04002771 for (i = 0; i < num_stripes; i++) {
2772 if (unplug_page) {
2773 struct btrfs_device *device;
2774 struct backing_dev_info *bdi;
2775
2776 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002777 if (device->bdev) {
2778 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002779 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002780 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002781 }
2782 } else {
2783 multi->stripes[i].physical =
2784 map->stripes[stripe_index].physical +
2785 stripe_offset + stripe_nr * map->stripe_len;
2786 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2787 }
Chris Masoncea9e442008-04-09 16:28:12 -04002788 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002789 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002790 if (multi_ret) {
2791 *multi_ret = multi;
2792 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002793 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002794 }
Chris Masoncea9e442008-04-09 16:28:12 -04002795out:
Chris Mason0b86a832008-03-24 15:01:56 -04002796 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002797 return 0;
2798}
2799
Chris Masonf2d8d742008-04-21 10:03:05 -04002800int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2801 u64 logical, u64 *length,
2802 struct btrfs_multi_bio **multi_ret, int mirror_num)
2803{
2804 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2805 mirror_num, NULL);
2806}
2807
Yan Zhenga512bbf2008-12-08 16:46:26 -05002808int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2809 u64 chunk_start, u64 physical, u64 devid,
2810 u64 **logical, int *naddrs, int *stripe_len)
2811{
2812 struct extent_map_tree *em_tree = &map_tree->map_tree;
2813 struct extent_map *em;
2814 struct map_lookup *map;
2815 u64 *buf;
2816 u64 bytenr;
2817 u64 length;
2818 u64 stripe_nr;
2819 int i, j, nr = 0;
2820
Chris Mason890871b2009-09-02 16:24:52 -04002821 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002822 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002823 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002824
2825 BUG_ON(!em || em->start != chunk_start);
2826 map = (struct map_lookup *)em->bdev;
2827
2828 length = em->len;
2829 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2830 do_div(length, map->num_stripes / map->sub_stripes);
2831 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2832 do_div(length, map->num_stripes);
2833
2834 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2835 BUG_ON(!buf);
2836
2837 for (i = 0; i < map->num_stripes; i++) {
2838 if (devid && map->stripes[i].dev->devid != devid)
2839 continue;
2840 if (map->stripes[i].physical > physical ||
2841 map->stripes[i].physical + length <= physical)
2842 continue;
2843
2844 stripe_nr = physical - map->stripes[i].physical;
2845 do_div(stripe_nr, map->stripe_len);
2846
2847 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2848 stripe_nr = stripe_nr * map->num_stripes + i;
2849 do_div(stripe_nr, map->sub_stripes);
2850 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2851 stripe_nr = stripe_nr * map->num_stripes + i;
2852 }
2853 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002854 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002855 for (j = 0; j < nr; j++) {
2856 if (buf[j] == bytenr)
2857 break;
2858 }
Chris Mason934d3752008-12-08 16:43:10 -05002859 if (j == nr) {
2860 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002861 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002862 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002863 }
2864
Yan Zhenga512bbf2008-12-08 16:46:26 -05002865 *logical = buf;
2866 *naddrs = nr;
2867 *stripe_len = map->stripe_len;
2868
2869 free_extent_map(em);
2870 return 0;
2871}
2872
Chris Masonf2d8d742008-04-21 10:03:05 -04002873int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2874 u64 logical, struct page *page)
2875{
2876 u64 length = PAGE_CACHE_SIZE;
2877 return __btrfs_map_block(map_tree, READ, logical, &length,
2878 NULL, 0, page);
2879}
2880
Chris Mason8790d502008-04-03 16:29:03 -04002881static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002882{
Chris Masoncea9e442008-04-09 16:28:12 -04002883 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002884 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002885
Chris Mason8790d502008-04-03 16:29:03 -04002886 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002887 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002888
Chris Mason7d2b4da2008-08-05 10:13:57 -04002889 if (bio == multi->orig_bio)
2890 is_orig_bio = 1;
2891
Chris Masoncea9e442008-04-09 16:28:12 -04002892 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002893 if (!is_orig_bio) {
2894 bio_put(bio);
2895 bio = multi->orig_bio;
2896 }
Chris Mason8790d502008-04-03 16:29:03 -04002897 bio->bi_private = multi->private;
2898 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002899 /* only send an error to the higher layers if it is
2900 * beyond the tolerance of the multi-bio
2901 */
Chris Mason1259ab72008-05-12 13:39:03 -04002902 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002903 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002904 } else if (err) {
2905 /*
2906 * this bio is actually up to date, we didn't
2907 * go over the max number of errors
2908 */
2909 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002910 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002911 }
Chris Mason8790d502008-04-03 16:29:03 -04002912 kfree(multi);
2913
2914 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002915 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002916 bio_put(bio);
2917 }
Chris Mason8790d502008-04-03 16:29:03 -04002918}
2919
Chris Mason8b712842008-06-11 16:50:36 -04002920struct async_sched {
2921 struct bio *bio;
2922 int rw;
2923 struct btrfs_fs_info *info;
2924 struct btrfs_work work;
2925};
2926
2927/*
2928 * see run_scheduled_bios for a description of why bios are collected for
2929 * async submit.
2930 *
2931 * This will add one bio to the pending list for a device and make sure
2932 * the work struct is scheduled.
2933 */
Chris Masond3977122009-01-05 21:25:51 -05002934static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002935 struct btrfs_device *device,
2936 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002937{
2938 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04002939 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002940
2941 /* don't bother with additional async steps for reads, right now */
2942 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002943 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002944 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002945 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002946 return 0;
2947 }
2948
2949 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04002950 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002951 * higher layers. Otherwise, the async bio makes it appear we have
2952 * made progress against dirty pages when we've really just put it
2953 * on a queue for later
2954 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04002955 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002956 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002957 bio->bi_next = NULL;
2958 bio->bi_rw |= rw;
2959
2960 spin_lock(&device->io_lock);
Jens Axboe1f98a132009-09-11 14:32:04 +02002961 if (bio_rw_flagged(bio, BIO_RW_SYNCIO))
Chris Masonffbd5172009-04-20 15:50:09 -04002962 pending_bios = &device->pending_sync_bios;
2963 else
2964 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002965
Chris Masonffbd5172009-04-20 15:50:09 -04002966 if (pending_bios->tail)
2967 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002968
Chris Masonffbd5172009-04-20 15:50:09 -04002969 pending_bios->tail = bio;
2970 if (!pending_bios->head)
2971 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002972 if (device->running_pending)
2973 should_queue = 0;
2974
2975 spin_unlock(&device->io_lock);
2976
2977 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002978 btrfs_queue_worker(&root->fs_info->submit_workers,
2979 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002980 return 0;
2981}
2982
Chris Masonf1885912008-04-09 16:28:12 -04002983int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002984 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002985{
2986 struct btrfs_mapping_tree *map_tree;
2987 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002988 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002989 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002990 u64 length = 0;
2991 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002992 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002993 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002994 int dev_nr = 0;
2995 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002996
Chris Masonf2d8d742008-04-21 10:03:05 -04002997 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002998 map_tree = &root->fs_info->mapping_tree;
2999 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003000
Chris Masonf1885912008-04-09 16:28:12 -04003001 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3002 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003003 BUG_ON(ret);
3004
3005 total_devs = multi->num_stripes;
3006 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003007 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3008 "len %llu\n", (unsigned long long)logical,
3009 (unsigned long long)length,
3010 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003011 BUG();
3012 }
3013 multi->end_io = first_bio->bi_end_io;
3014 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003015 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003016 atomic_set(&multi->stripes_pending, multi->num_stripes);
3017
Chris Masond3977122009-01-05 21:25:51 -05003018 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003019 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003020 if (dev_nr < total_devs - 1) {
3021 bio = bio_clone(first_bio, GFP_NOFS);
3022 BUG_ON(!bio);
3023 } else {
3024 bio = first_bio;
3025 }
3026 bio->bi_private = multi;
3027 bio->bi_end_io = end_bio_multi_stripe;
3028 }
Chris Masoncea9e442008-04-09 16:28:12 -04003029 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3030 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05003031 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04003032 if (dev && dev->bdev) {
3033 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003034 if (async_submit)
3035 schedule_bio(root, dev, rw, bio);
3036 else
3037 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003038 } else {
3039 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3040 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003041 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003042 }
Chris Mason8790d502008-04-03 16:29:03 -04003043 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003044 }
Chris Masoncea9e442008-04-09 16:28:12 -04003045 if (total_devs == 1)
3046 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003047 return 0;
3048}
3049
Chris Masona4437552008-04-18 10:29:38 -04003050struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003051 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003052{
Yan Zheng2b820322008-11-17 21:11:30 -05003053 struct btrfs_device *device;
3054 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003055
Yan Zheng2b820322008-11-17 21:11:30 -05003056 cur_devices = root->fs_info->fs_devices;
3057 while (cur_devices) {
3058 if (!fsid ||
3059 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3060 device = __find_device(&cur_devices->devices,
3061 devid, uuid);
3062 if (device)
3063 return device;
3064 }
3065 cur_devices = cur_devices->seed;
3066 }
3067 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003068}
3069
Chris Masondfe25022008-05-13 13:46:40 -04003070static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3071 u64 devid, u8 *dev_uuid)
3072{
3073 struct btrfs_device *device;
3074 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3075
3076 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003077 if (!device)
3078 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003079 list_add(&device->dev_list,
3080 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003081 device->barriers = 1;
3082 device->dev_root = root->fs_info->dev_root;
3083 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003084 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003085 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04003086 fs_devices->num_devices++;
3087 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003088 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003089 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3090 return device;
3091}
3092
Chris Mason0b86a832008-03-24 15:01:56 -04003093static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3094 struct extent_buffer *leaf,
3095 struct btrfs_chunk *chunk)
3096{
3097 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3098 struct map_lookup *map;
3099 struct extent_map *em;
3100 u64 logical;
3101 u64 length;
3102 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003103 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003104 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003105 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003106 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003107
Chris Masone17cade2008-04-15 15:41:47 -04003108 logical = key->offset;
3109 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003110
Chris Mason890871b2009-09-02 16:24:52 -04003111 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003112 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003113 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003114
3115 /* already mapped? */
3116 if (em && em->start <= logical && em->start + em->len > logical) {
3117 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003118 return 0;
3119 } else if (em) {
3120 free_extent_map(em);
3121 }
Chris Mason0b86a832008-03-24 15:01:56 -04003122
Chris Mason0b86a832008-03-24 15:01:56 -04003123 em = alloc_extent_map(GFP_NOFS);
3124 if (!em)
3125 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003126 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3127 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003128 if (!map) {
3129 free_extent_map(em);
3130 return -ENOMEM;
3131 }
3132
3133 em->bdev = (struct block_device *)map;
3134 em->start = logical;
3135 em->len = length;
3136 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003137 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003138
Chris Mason593060d2008-03-25 16:50:33 -04003139 map->num_stripes = num_stripes;
3140 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3141 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3142 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3143 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3144 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003145 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003146 for (i = 0; i < num_stripes; i++) {
3147 map->stripes[i].physical =
3148 btrfs_stripe_offset_nr(leaf, chunk, i);
3149 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003150 read_extent_buffer(leaf, uuid, (unsigned long)
3151 btrfs_stripe_dev_uuid_nr(chunk, i),
3152 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003153 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3154 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003155 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003156 kfree(map);
3157 free_extent_map(em);
3158 return -EIO;
3159 }
Chris Masondfe25022008-05-13 13:46:40 -04003160 if (!map->stripes[i].dev) {
3161 map->stripes[i].dev =
3162 add_missing_dev(root, devid, uuid);
3163 if (!map->stripes[i].dev) {
3164 kfree(map);
3165 free_extent_map(em);
3166 return -EIO;
3167 }
3168 }
3169 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003170 }
3171
Chris Mason890871b2009-09-02 16:24:52 -04003172 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003173 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003174 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003175 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003176 free_extent_map(em);
3177
3178 return 0;
3179}
3180
3181static int fill_device_from_item(struct extent_buffer *leaf,
3182 struct btrfs_dev_item *dev_item,
3183 struct btrfs_device *device)
3184{
3185 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003186
3187 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003188 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3189 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003190 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3191 device->type = btrfs_device_type(leaf, dev_item);
3192 device->io_align = btrfs_device_io_align(leaf, dev_item);
3193 device->io_width = btrfs_device_io_width(leaf, dev_item);
3194 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003195
3196 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003197 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003198
Chris Mason0b86a832008-03-24 15:01:56 -04003199 return 0;
3200}
3201
Yan Zheng2b820322008-11-17 21:11:30 -05003202static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3203{
3204 struct btrfs_fs_devices *fs_devices;
3205 int ret;
3206
3207 mutex_lock(&uuid_mutex);
3208
3209 fs_devices = root->fs_info->fs_devices->seed;
3210 while (fs_devices) {
3211 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3212 ret = 0;
3213 goto out;
3214 }
3215 fs_devices = fs_devices->seed;
3216 }
3217
3218 fs_devices = find_fsid(fsid);
3219 if (!fs_devices) {
3220 ret = -ENOENT;
3221 goto out;
3222 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003223
3224 fs_devices = clone_fs_devices(fs_devices);
3225 if (IS_ERR(fs_devices)) {
3226 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003227 goto out;
3228 }
3229
Christoph Hellwig97288f22008-12-02 06:36:09 -05003230 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003231 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003232 if (ret)
3233 goto out;
3234
3235 if (!fs_devices->seeding) {
3236 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003237 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003238 ret = -EINVAL;
3239 goto out;
3240 }
3241
3242 fs_devices->seed = root->fs_info->fs_devices->seed;
3243 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003244out:
3245 mutex_unlock(&uuid_mutex);
3246 return ret;
3247}
3248
Chris Mason0d81ba52008-03-24 15:02:07 -04003249static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003250 struct extent_buffer *leaf,
3251 struct btrfs_dev_item *dev_item)
3252{
3253 struct btrfs_device *device;
3254 u64 devid;
3255 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003256 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003257 u8 dev_uuid[BTRFS_UUID_SIZE];
3258
Chris Mason0b86a832008-03-24 15:01:56 -04003259 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003260 read_extent_buffer(leaf, dev_uuid,
3261 (unsigned long)btrfs_device_uuid(dev_item),
3262 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003263 read_extent_buffer(leaf, fs_uuid,
3264 (unsigned long)btrfs_device_fsid(dev_item),
3265 BTRFS_UUID_SIZE);
3266
3267 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3268 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003269 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003270 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003271 }
3272
3273 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3274 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003275 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003276 return -EIO;
3277
3278 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003279 printk(KERN_WARNING "warning devid %llu missing\n",
3280 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003281 device = add_missing_dev(root, devid, dev_uuid);
3282 if (!device)
3283 return -ENOMEM;
3284 }
3285 }
3286
3287 if (device->fs_devices != root->fs_info->fs_devices) {
3288 BUG_ON(device->writeable);
3289 if (device->generation !=
3290 btrfs_device_generation(leaf, dev_item))
3291 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003292 }
Chris Mason0b86a832008-03-24 15:01:56 -04003293
3294 fill_device_from_item(leaf, dev_item, device);
3295 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003296 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003297 if (device->writeable)
3298 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003299 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003300 return ret;
3301}
3302
Chris Mason0d81ba52008-03-24 15:02:07 -04003303int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3304{
3305 struct btrfs_dev_item *dev_item;
3306
3307 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3308 dev_item);
3309 return read_one_dev(root, buf, dev_item);
3310}
3311
Yan Zhenge4404d62008-12-12 10:03:26 -05003312int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003313{
3314 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003315 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003316 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003317 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003318 u8 *ptr;
3319 unsigned long sb_ptr;
3320 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003321 u32 num_stripes;
3322 u32 array_size;
3323 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003324 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003325 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003326
Yan Zhenge4404d62008-12-12 10:03:26 -05003327 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003328 BTRFS_SUPER_INFO_SIZE);
3329 if (!sb)
3330 return -ENOMEM;
3331 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003332 btrfs_set_buffer_lockdep_class(sb, 0);
3333
Chris Masona061fc82008-05-07 11:43:44 -04003334 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003335 array_size = btrfs_super_sys_array_size(super_copy);
3336
Chris Mason0b86a832008-03-24 15:01:56 -04003337 ptr = super_copy->sys_chunk_array;
3338 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3339 cur = 0;
3340
3341 while (cur < array_size) {
3342 disk_key = (struct btrfs_disk_key *)ptr;
3343 btrfs_disk_key_to_cpu(&key, disk_key);
3344
Chris Masona061fc82008-05-07 11:43:44 -04003345 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003346 sb_ptr += len;
3347 cur += len;
3348
Chris Mason0d81ba52008-03-24 15:02:07 -04003349 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003350 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003351 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003352 if (ret)
3353 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003354 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3355 len = btrfs_chunk_item_size(num_stripes);
3356 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003357 ret = -EIO;
3358 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003359 }
3360 ptr += len;
3361 sb_ptr += len;
3362 cur += len;
3363 }
Chris Masona061fc82008-05-07 11:43:44 -04003364 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003365 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003366}
3367
3368int btrfs_read_chunk_tree(struct btrfs_root *root)
3369{
3370 struct btrfs_path *path;
3371 struct extent_buffer *leaf;
3372 struct btrfs_key key;
3373 struct btrfs_key found_key;
3374 int ret;
3375 int slot;
3376
3377 root = root->fs_info->chunk_root;
3378
3379 path = btrfs_alloc_path();
3380 if (!path)
3381 return -ENOMEM;
3382
3383 /* first we search for all of the device items, and then we
3384 * read in all of the chunk items. This way we can create chunk
3385 * mappings that reference all of the devices that are afound
3386 */
3387 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3388 key.offset = 0;
3389 key.type = 0;
3390again:
3391 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003392 if (ret < 0)
3393 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003394 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003395 leaf = path->nodes[0];
3396 slot = path->slots[0];
3397 if (slot >= btrfs_header_nritems(leaf)) {
3398 ret = btrfs_next_leaf(root, path);
3399 if (ret == 0)
3400 continue;
3401 if (ret < 0)
3402 goto error;
3403 break;
3404 }
3405 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3406 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3407 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3408 break;
3409 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3410 struct btrfs_dev_item *dev_item;
3411 dev_item = btrfs_item_ptr(leaf, slot,
3412 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003413 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003414 if (ret)
3415 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003416 }
3417 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3418 struct btrfs_chunk *chunk;
3419 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3420 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003421 if (ret)
3422 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003423 }
3424 path->slots[0]++;
3425 }
3426 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3427 key.objectid = 0;
3428 btrfs_release_path(root, path);
3429 goto again;
3430 }
Chris Mason0b86a832008-03-24 15:01:56 -04003431 ret = 0;
3432error:
Yan Zheng2b820322008-11-17 21:11:30 -05003433 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003434 return ret;
3435}