blob: 43c4f09e441cd9da56f40e0ba3395cf1b3bc2273 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Chris Mason593060d2008-03-25 16:50:33 -040026#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "ctree.h"
29#include "extent_map.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "print-tree.h"
33#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Yan Zheng2b820322008-11-17 21:11:30 -050036static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
40
Chris Mason593060d2008-03-25 16:50:33 -040041#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040042 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040043
Chris Mason8a4b83c2008-03-24 15:02:07 -040044static DEFINE_MUTEX(uuid_mutex);
45static LIST_HEAD(fs_uuids);
46
Chris Masona061fc82008-05-07 11:43:44 -040047void btrfs_lock_volumes(void)
48{
49 mutex_lock(&uuid_mutex);
50}
51
52void btrfs_unlock_volumes(void)
53{
54 mutex_unlock(&uuid_mutex);
55}
56
Chris Mason7d9eb122008-07-08 14:19:17 -040057static void lock_chunks(struct btrfs_root *root)
58{
Chris Mason7d9eb122008-07-08 14:19:17 -040059 mutex_lock(&root->fs_info->chunk_mutex);
60}
61
62static void unlock_chunks(struct btrfs_root *root)
63{
Chris Mason7d9eb122008-07-08 14:19:17 -040064 mutex_unlock(&root->fs_info->chunk_mutex);
65}
66
Yan Zhenge4404d62008-12-12 10:03:26 -050067static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
68{
69 struct btrfs_device *device;
70 WARN_ON(fs_devices->opened);
71 while (!list_empty(&fs_devices->devices)) {
72 device = list_entry(fs_devices->devices.next,
73 struct btrfs_device, dev_list);
74 list_del(&device->dev_list);
75 kfree(device->name);
76 kfree(device);
77 }
78 kfree(fs_devices);
79}
80
Chris Mason8a4b83c2008-03-24 15:02:07 -040081int btrfs_cleanup_fs_uuids(void)
82{
83 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040084
Yan Zheng2b820322008-11-17 21:11:30 -050085 while (!list_empty(&fs_uuids)) {
86 fs_devices = list_entry(fs_uuids.next,
87 struct btrfs_fs_devices, list);
88 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050089 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 }
91 return 0;
92}
93
Chris Masona1b32a52008-09-05 16:09:51 -040094static noinline struct btrfs_device *__find_device(struct list_head *head,
95 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040096{
97 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040098
Qinghuang Fengc6e30872009-01-21 10:59:08 -050099 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400100 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400101 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400103 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400104 }
105 return NULL;
106}
107
Chris Masona1b32a52008-09-05 16:09:51 -0400108static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400109{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400110 struct btrfs_fs_devices *fs_devices;
111
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500112 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
114 return fs_devices;
115 }
116 return NULL;
117}
118
Chris Masonffbd5172009-04-20 15:50:09 -0400119static void requeue_list(struct btrfs_pending_bios *pending_bios,
120 struct bio *head, struct bio *tail)
121{
122
123 struct bio *old_head;
124
125 old_head = pending_bios->head;
126 pending_bios->head = head;
127 if (pending_bios->tail)
128 tail->bi_next = old_head;
129 else
130 pending_bios->tail = tail;
131}
132
Chris Mason8b712842008-06-11 16:50:36 -0400133/*
134 * we try to collect pending bios for a device so we don't get a large
135 * number of procs sending bios down to the same device. This greatly
136 * improves the schedulers ability to collect and merge the bios.
137 *
138 * But, it also turns into a long list of bios to process and that is sure
139 * to eventually make the worker thread block. The solution here is to
140 * make some progress and then put this work struct back at the end of
141 * the list if the block device is congested. This way, multiple devices
142 * can make progress from a single worker thread.
143 */
Chris Masond3977122009-01-05 21:25:51 -0500144static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400145{
146 struct bio *pending;
147 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400148 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400149 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400150 struct bio *tail;
151 struct bio *cur;
152 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400153 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400154 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400155 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400156 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400157 int force_reg = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400158 struct blk_plug plug;
159
160 /*
161 * this function runs all the bios we've collected for
162 * a particular device. We don't want to wander off to
163 * another device without first sending all of these down.
164 * So, setup a plug here and finish it off before we return
165 */
166 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400167
Chris Masonbedf7622009-04-03 10:32:58 -0400168 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400169 fs_info = device->dev_root->fs_info;
170 limit = btrfs_async_submit_limit(fs_info);
171 limit = limit * 2 / 3;
172
Chris Mason8b712842008-06-11 16:50:36 -0400173loop:
174 spin_lock(&device->io_lock);
175
Chris Masona6837052009-02-04 09:19:41 -0500176loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400177 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400178
Chris Mason8b712842008-06-11 16:50:36 -0400179 /* take all the bios off the list at once and process them
180 * later on (without the lock held). But, remember the
181 * tail and other pointers so the bios can be properly reinserted
182 * into the list if we hit congestion
183 */
Chris Masond84275c2009-06-09 15:39:08 -0400184 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400185 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400186 force_reg = 1;
187 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400188 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400189 force_reg = 0;
190 }
Chris Masonffbd5172009-04-20 15:50:09 -0400191
192 pending = pending_bios->head;
193 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400194 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400195
196 /*
197 * if pending was null this time around, no bios need processing
198 * at all and we can stop. Otherwise it'll loop back up again
199 * and do an additional check so no bios are missed.
200 *
201 * device->running_pending is used to synchronize with the
202 * schedule_bio code.
203 */
Chris Masonffbd5172009-04-20 15:50:09 -0400204 if (device->pending_sync_bios.head == NULL &&
205 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400206 again = 0;
207 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400208 } else {
209 again = 1;
210 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400211 }
Chris Masonffbd5172009-04-20 15:50:09 -0400212
213 pending_bios->head = NULL;
214 pending_bios->tail = NULL;
215
Chris Mason8b712842008-06-11 16:50:36 -0400216 spin_unlock(&device->io_lock);
217
Chris Masond3977122009-01-05 21:25:51 -0500218 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400219
220 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400221 /* we want to work on both lists, but do more bios on the
222 * sync list than the regular list
223 */
224 if ((num_run > 32 &&
225 pending_bios != &device->pending_sync_bios &&
226 device->pending_sync_bios.head) ||
227 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
228 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400229 spin_lock(&device->io_lock);
230 requeue_list(pending_bios, pending, tail);
231 goto loop_lock;
232 }
233
Chris Mason8b712842008-06-11 16:50:36 -0400234 cur = pending;
235 pending = pending->bi_next;
236 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400237 atomic_dec(&fs_info->nr_async_bios);
238
239 if (atomic_read(&fs_info->nr_async_bios) < limit &&
240 waitqueue_active(&fs_info->async_submit_wait))
241 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400242
243 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400244
Chris Mason5ff7ba32010-03-15 10:21:30 -0400245 submit_bio(cur->bi_rw, cur);
246 num_run++;
247 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100248 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400249 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400250
251 /*
252 * we made progress, there is more work to do and the bdi
253 * is now congested. Back off and let other work structs
254 * run instead
255 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400256 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500257 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400258 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400259
Chris Masonb765ead2009-04-03 10:27:10 -0400260 ioc = current->io_context;
261
262 /*
263 * the main goal here is that we don't want to
264 * block if we're going to be able to submit
265 * more requests without blocking.
266 *
267 * This code does two great things, it pokes into
268 * the elevator code from a filesystem _and_
269 * it makes assumptions about how batching works.
270 */
271 if (ioc && ioc->nr_batch_requests > 0 &&
272 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
273 (last_waited == 0 ||
274 ioc->last_waited == last_waited)) {
275 /*
276 * we want to go through our batch of
277 * requests and stop. So, we copy out
278 * the ioc->last_waited time and test
279 * against it before looping
280 */
281 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100282 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400283 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400284 continue;
285 }
Chris Mason8b712842008-06-11 16:50:36 -0400286 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400287 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500288 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400289
290 spin_unlock(&device->io_lock);
291 btrfs_requeue_work(&device->work);
292 goto done;
293 }
294 }
Chris Masonffbd5172009-04-20 15:50:09 -0400295
Chris Mason51684082010-03-10 15:33:32 -0500296 cond_resched();
297 if (again)
298 goto loop;
299
300 spin_lock(&device->io_lock);
301 if (device->pending_bios.head || device->pending_sync_bios.head)
302 goto loop_lock;
303 spin_unlock(&device->io_lock);
304
Chris Mason8b712842008-06-11 16:50:36 -0400305done:
Chris Mason211588a2011-04-19 20:12:40 -0400306 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400307 return 0;
308}
309
Christoph Hellwigb2950862008-12-02 09:54:17 -0500310static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400311{
312 struct btrfs_device *device;
313
314 device = container_of(work, struct btrfs_device, work);
315 run_scheduled_bios(device);
316}
317
Chris Masona1b32a52008-09-05 16:09:51 -0400318static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400319 struct btrfs_super_block *disk_super,
320 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
321{
322 struct btrfs_device *device;
323 struct btrfs_fs_devices *fs_devices;
324 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000325 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400326
327 fs_devices = find_fsid(disk_super->fsid);
328 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400329 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 if (!fs_devices)
331 return -ENOMEM;
332 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400333 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400334 list_add(&fs_devices->list, &fs_uuids);
335 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
336 fs_devices->latest_devid = devid;
337 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400338 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400339 device = NULL;
340 } else {
Chris Masona4437552008-04-18 10:29:38 -0400341 device = __find_device(&fs_devices->devices, devid,
342 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 }
344 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500345 if (fs_devices->opened)
346 return -EBUSY;
347
Chris Mason8a4b83c2008-03-24 15:02:07 -0400348 device = kzalloc(sizeof(*device), GFP_NOFS);
349 if (!device) {
350 /* we can safely leave the fs_devices entry around */
351 return -ENOMEM;
352 }
353 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400354 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400355 memcpy(device->uuid, disk_super->dev_item.uuid,
356 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400357 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358 device->name = kstrdup(path, GFP_NOFS);
359 if (!device->name) {
360 kfree(device);
361 return -ENOMEM;
362 }
Yan Zheng2b820322008-11-17 21:11:30 -0500363 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400364
365 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400366 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400367 mutex_unlock(&fs_devices->device_list_mutex);
368
Yan Zheng2b820322008-11-17 21:11:30 -0500369 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500371 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000372 name = kstrdup(path, GFP_NOFS);
373 if (!name)
374 return -ENOMEM;
375 kfree(device->name);
376 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500377 if (device->missing) {
378 fs_devices->missing_devices--;
379 device->missing = 0;
380 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400381 }
382
383 if (found_transid > fs_devices->latest_trans) {
384 fs_devices->latest_devid = devid;
385 fs_devices->latest_trans = found_transid;
386 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400387 *fs_devices_ret = fs_devices;
388 return 0;
389}
390
Yan Zhenge4404d62008-12-12 10:03:26 -0500391static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
392{
393 struct btrfs_fs_devices *fs_devices;
394 struct btrfs_device *device;
395 struct btrfs_device *orig_dev;
396
397 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
398 if (!fs_devices)
399 return ERR_PTR(-ENOMEM);
400
401 INIT_LIST_HEAD(&fs_devices->devices);
402 INIT_LIST_HEAD(&fs_devices->alloc_list);
403 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400404 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500405 fs_devices->latest_devid = orig->latest_devid;
406 fs_devices->latest_trans = orig->latest_trans;
407 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
408
Chris Masone5e9a522009-06-10 15:17:02 -0400409 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500410 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
411 device = kzalloc(sizeof(*device), GFP_NOFS);
412 if (!device)
413 goto error;
414
415 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400416 if (!device->name) {
417 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500418 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400419 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500420
421 device->devid = orig_dev->devid;
422 device->work.func = pending_bios_fn;
423 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500424 spin_lock_init(&device->io_lock);
425 INIT_LIST_HEAD(&device->dev_list);
426 INIT_LIST_HEAD(&device->dev_alloc_list);
427
428 list_add(&device->dev_list, &fs_devices->devices);
429 device->fs_devices = fs_devices;
430 fs_devices->num_devices++;
431 }
Chris Masone5e9a522009-06-10 15:17:02 -0400432 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500433 return fs_devices;
434error:
Chris Masone5e9a522009-06-10 15:17:02 -0400435 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 free_fs_devices(fs_devices);
437 return ERR_PTR(-ENOMEM);
438}
439
Chris Masondfe25022008-05-13 13:46:40 -0400440int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
441{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500442 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400443
444 mutex_lock(&uuid_mutex);
445again:
Chris Masone5e9a522009-06-10 15:17:02 -0400446 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500447 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500448 if (device->in_fs_metadata)
449 continue;
450
451 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100452 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500453 device->bdev = NULL;
454 fs_devices->open_devices--;
455 }
456 if (device->writeable) {
457 list_del_init(&device->dev_alloc_list);
458 device->writeable = 0;
459 fs_devices->rw_devices--;
460 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500461 list_del_init(&device->dev_list);
462 fs_devices->num_devices--;
463 kfree(device->name);
464 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400465 }
Chris Masone5e9a522009-06-10 15:17:02 -0400466 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500467
468 if (fs_devices->seed) {
469 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500470 goto again;
471 }
472
Chris Masondfe25022008-05-13 13:46:40 -0400473 mutex_unlock(&uuid_mutex);
474 return 0;
475}
Chris Masona0af4692008-05-13 16:03:06 -0400476
Yan Zheng2b820322008-11-17 21:11:30 -0500477static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400478{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500480
Yan Zheng2b820322008-11-17 21:11:30 -0500481 if (--fs_devices->opened > 0)
482 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400483
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000484 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500485 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400486 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100487 blkdev_put(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400488 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400489 }
Yan Zheng2b820322008-11-17 21:11:30 -0500490 if (device->writeable) {
491 list_del_init(&device->dev_alloc_list);
492 fs_devices->rw_devices--;
493 }
494
Chris Mason8a4b83c2008-03-24 15:02:07 -0400495 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500496 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400497 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000499 mutex_unlock(&fs_devices->device_list_mutex);
500
Yan Zhenge4404d62008-12-12 10:03:26 -0500501 WARN_ON(fs_devices->open_devices);
502 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500503 fs_devices->opened = 0;
504 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500505
Chris Mason8a4b83c2008-03-24 15:02:07 -0400506 return 0;
507}
508
Yan Zheng2b820322008-11-17 21:11:30 -0500509int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
510{
Yan Zhenge4404d62008-12-12 10:03:26 -0500511 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500512 int ret;
513
514 mutex_lock(&uuid_mutex);
515 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500516 if (!fs_devices->opened) {
517 seed_devices = fs_devices->seed;
518 fs_devices->seed = NULL;
519 }
Yan Zheng2b820322008-11-17 21:11:30 -0500520 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500521
522 while (seed_devices) {
523 fs_devices = seed_devices;
524 seed_devices = fs_devices->seed;
525 __btrfs_close_devices(fs_devices);
526 free_fs_devices(fs_devices);
527 }
Yan Zheng2b820322008-11-17 21:11:30 -0500528 return ret;
529}
530
Yan Zhenge4404d62008-12-12 10:03:26 -0500531static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
532 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533{
534 struct block_device *bdev;
535 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400536 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400537 struct block_device *latest_bdev = NULL;
538 struct buffer_head *bh;
539 struct btrfs_super_block *disk_super;
540 u64 latest_devid = 0;
541 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400542 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500543 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400544 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400545
Tejun Heod4d77622010-11-13 11:55:18 +0100546 flags |= FMODE_EXCL;
547
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500548 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400549 if (device->bdev)
550 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400551 if (!device->name)
552 continue;
553
Tejun Heod4d77622010-11-13 11:55:18 +0100554 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400555 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500556 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400557 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 }
Chris Masona061fc82008-05-07 11:43:44 -0400559 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400560
Yan Zhenga512bbf2008-12-08 16:46:26 -0500561 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000562 if (!bh) {
563 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400564 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000565 }
Chris Masona0af4692008-05-13 16:03:06 -0400566
567 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000568 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400569 if (devid != device->devid)
570 goto error_brelse;
571
Yan Zheng2b820322008-11-17 21:11:30 -0500572 if (memcmp(device->uuid, disk_super->dev_item.uuid,
573 BTRFS_UUID_SIZE))
574 goto error_brelse;
575
576 device->generation = btrfs_super_generation(disk_super);
577 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400578 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500579 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400580 latest_bdev = bdev;
581 }
582
Yan Zheng2b820322008-11-17 21:11:30 -0500583 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
584 device->writeable = 0;
585 } else {
586 device->writeable = !bdev_read_only(bdev);
587 seeding = 0;
588 }
589
Chris Mason8a4b83c2008-03-24 15:02:07 -0400590 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400591 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500592 device->mode = flags;
593
Chris Masonc289811c2009-06-10 09:51:32 -0400594 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
595 fs_devices->rotating = 1;
596
Chris Masona0af4692008-05-13 16:03:06 -0400597 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500598 if (device->writeable) {
599 fs_devices->rw_devices++;
600 list_add(&device->dev_alloc_list,
601 &fs_devices->alloc_list);
602 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000603 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400604 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400605
Chris Masona0af4692008-05-13 16:03:06 -0400606error_brelse:
607 brelse(bh);
608error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100609 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400610error:
611 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400612 }
Chris Masona0af4692008-05-13 16:03:06 -0400613 if (fs_devices->open_devices == 0) {
614 ret = -EIO;
615 goto out;
616 }
Yan Zheng2b820322008-11-17 21:11:30 -0500617 fs_devices->seeding = seeding;
618 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400619 fs_devices->latest_bdev = latest_bdev;
620 fs_devices->latest_devid = latest_devid;
621 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500622 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400623out:
Yan Zheng2b820322008-11-17 21:11:30 -0500624 return ret;
625}
626
627int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500628 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500629{
630 int ret;
631
632 mutex_lock(&uuid_mutex);
633 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500634 fs_devices->opened++;
635 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500636 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500637 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500638 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400639 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400640 return ret;
641}
642
Christoph Hellwig97288f22008-12-02 06:36:09 -0500643int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400644 struct btrfs_fs_devices **fs_devices_ret)
645{
646 struct btrfs_super_block *disk_super;
647 struct block_device *bdev;
648 struct buffer_head *bh;
649 int ret;
650 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400651 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400652
653 mutex_lock(&uuid_mutex);
654
Tejun Heod4d77622010-11-13 11:55:18 +0100655 flags |= FMODE_EXCL;
656 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400657
658 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400659 ret = PTR_ERR(bdev);
660 goto error;
661 }
662
663 ret = set_blocksize(bdev, 4096);
664 if (ret)
665 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500666 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400667 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000668 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 goto error_close;
670 }
671 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000672 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400673 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400674 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500675 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400676 else {
677 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500678 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400679 *(unsigned long long *)disk_super->fsid,
680 *(unsigned long long *)(disk_super->fsid + 8));
681 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500682 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500683 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400684 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
685
Chris Mason8a4b83c2008-03-24 15:02:07 -0400686 brelse(bh);
687error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100688 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400689error:
690 mutex_unlock(&uuid_mutex);
691 return ret;
692}
Chris Mason0b86a832008-03-24 15:01:56 -0400693
Miao Xie6d07bce2011-01-05 10:07:31 +0000694/* helper to account the used device space in the range */
695int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
696 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400697{
698 struct btrfs_key key;
699 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000700 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500701 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000702 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400703 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000704 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400705 struct extent_buffer *l;
706
Miao Xie6d07bce2011-01-05 10:07:31 +0000707 *length = 0;
708
709 if (start >= device->total_bytes)
710 return 0;
711
Yan Zheng2b820322008-11-17 21:11:30 -0500712 path = btrfs_alloc_path();
713 if (!path)
714 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400715 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400716
Chris Mason0b86a832008-03-24 15:01:56 -0400717 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000718 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400719 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000720
721 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400722 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000723 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400724 if (ret > 0) {
725 ret = btrfs_previous_item(root, path, key.objectid, key.type);
726 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000727 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400728 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000729
Chris Mason0b86a832008-03-24 15:01:56 -0400730 while (1) {
731 l = path->nodes[0];
732 slot = path->slots[0];
733 if (slot >= btrfs_header_nritems(l)) {
734 ret = btrfs_next_leaf(root, path);
735 if (ret == 0)
736 continue;
737 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000738 goto out;
739
740 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400741 }
742 btrfs_item_key_to_cpu(l, &key, slot);
743
744 if (key.objectid < device->devid)
745 goto next;
746
747 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000748 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400749
Chris Masond3977122009-01-05 21:25:51 -0500750 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400751 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400752
Chris Mason0b86a832008-03-24 15:01:56 -0400753 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000754 extent_end = key.offset + btrfs_dev_extent_length(l,
755 dev_extent);
756 if (key.offset <= start && extent_end > end) {
757 *length = end - start + 1;
758 break;
759 } else if (key.offset <= start && extent_end > start)
760 *length += extent_end - start;
761 else if (key.offset > start && extent_end <= end)
762 *length += extent_end - key.offset;
763 else if (key.offset > start && key.offset <= end) {
764 *length += end - key.offset + 1;
765 break;
766 } else if (key.offset > end)
767 break;
768
769next:
770 path->slots[0]++;
771 }
772 ret = 0;
773out:
774 btrfs_free_path(path);
775 return ret;
776}
777
Chris Mason0b86a832008-03-24 15:01:56 -0400778/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000779 * find_free_dev_extent - find free space in the specified device
780 * @trans: transaction handler
781 * @device: the device which we search the free space in
782 * @num_bytes: the size of the free space that we need
783 * @start: store the start of the free space.
784 * @len: the size of the free space. that we find, or the size of the max
785 * free space if we don't find suitable free space
786 *
Chris Mason0b86a832008-03-24 15:01:56 -0400787 * this uses a pretty simple search, the expectation is that it is
788 * called very infrequently and that a given device has a small number
789 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000790 *
791 * @start is used to store the start of the free space if we find. But if we
792 * don't find suitable free space, it will be used to store the start position
793 * of the max free space.
794 *
795 * @len is used to store the size of the free space that we find.
796 * But if we don't find suitable free space, it is used to store the size of
797 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400798 */
799int find_free_dev_extent(struct btrfs_trans_handle *trans,
800 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000801 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400802{
803 struct btrfs_key key;
804 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000805 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400806 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000807 u64 hole_size;
808 u64 max_hole_start;
809 u64 max_hole_size;
810 u64 extent_end;
811 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400812 u64 search_end = device->total_bytes;
813 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000814 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400815 struct extent_buffer *l;
816
Chris Mason0b86a832008-03-24 15:01:56 -0400817 /* FIXME use last free of some kind */
818
819 /* we don't want to overwrite the superblock on the drive,
820 * so we make sure to start at an offset of at least 1MB
821 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000822 search_start = 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400823
Miao Xie7bfc8372011-01-05 10:07:26 +0000824 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason0b86a832008-03-24 15:01:56 -0400825 search_start = max(root->fs_info->alloc_start, search_start);
826
Miao Xie7bfc8372011-01-05 10:07:26 +0000827 max_hole_start = search_start;
828 max_hole_size = 0;
829
830 if (search_start >= search_end) {
831 ret = -ENOSPC;
832 goto error;
833 }
834
835 path = btrfs_alloc_path();
836 if (!path) {
837 ret = -ENOMEM;
838 goto error;
839 }
840 path->reada = 2;
841
Chris Mason0b86a832008-03-24 15:01:56 -0400842 key.objectid = device->devid;
843 key.offset = search_start;
844 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000845
Chris Mason0b86a832008-03-24 15:01:56 -0400846 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
847 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400849 if (ret > 0) {
850 ret = btrfs_previous_item(root, path, key.objectid, key.type);
851 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000852 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400853 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000854
Chris Mason0b86a832008-03-24 15:01:56 -0400855 while (1) {
856 l = path->nodes[0];
857 slot = path->slots[0];
858 if (slot >= btrfs_header_nritems(l)) {
859 ret = btrfs_next_leaf(root, path);
860 if (ret == 0)
861 continue;
862 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000863 goto out;
864
865 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400866 }
867 btrfs_item_key_to_cpu(l, &key, slot);
868
869 if (key.objectid < device->devid)
870 goto next;
871
872 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000873 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400874
Chris Mason0b86a832008-03-24 15:01:56 -0400875 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
876 goto next;
877
Miao Xie7bfc8372011-01-05 10:07:26 +0000878 if (key.offset > search_start) {
879 hole_size = key.offset - search_start;
880
881 if (hole_size > max_hole_size) {
882 max_hole_start = search_start;
883 max_hole_size = hole_size;
884 }
885
886 /*
887 * If this free space is greater than which we need,
888 * it must be the max free space that we have found
889 * until now, so max_hole_start must point to the start
890 * of this free space and the length of this free space
891 * is stored in max_hole_size. Thus, we return
892 * max_hole_start and max_hole_size and go back to the
893 * caller.
894 */
895 if (hole_size >= num_bytes) {
896 ret = 0;
897 goto out;
898 }
899 }
900
Chris Mason0b86a832008-03-24 15:01:56 -0400901 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000902 extent_end = key.offset + btrfs_dev_extent_length(l,
903 dev_extent);
904 if (extent_end > search_start)
905 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400906next:
907 path->slots[0]++;
908 cond_resched();
909 }
Chris Mason0b86a832008-03-24 15:01:56 -0400910
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 hole_size = search_end- search_start;
912 if (hole_size > max_hole_size) {
913 max_hole_start = search_start;
914 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400915 }
Chris Mason0b86a832008-03-24 15:01:56 -0400916
Miao Xie7bfc8372011-01-05 10:07:26 +0000917 /* See above. */
918 if (hole_size < num_bytes)
919 ret = -ENOSPC;
920 else
921 ret = 0;
922
923out:
Yan Zheng2b820322008-11-17 21:11:30 -0500924 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000925error:
926 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000927 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000928 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400929 return ret;
930}
931
Christoph Hellwigb2950862008-12-02 09:54:17 -0500932static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400933 struct btrfs_device *device,
934 u64 start)
935{
936 int ret;
937 struct btrfs_path *path;
938 struct btrfs_root *root = device->dev_root;
939 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400940 struct btrfs_key found_key;
941 struct extent_buffer *leaf = NULL;
942 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400943
944 path = btrfs_alloc_path();
945 if (!path)
946 return -ENOMEM;
947
948 key.objectid = device->devid;
949 key.offset = start;
950 key.type = BTRFS_DEV_EXTENT_KEY;
951
952 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400953 if (ret > 0) {
954 ret = btrfs_previous_item(root, path, key.objectid,
955 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000956 if (ret)
957 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400958 leaf = path->nodes[0];
959 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
960 extent = btrfs_item_ptr(leaf, path->slots[0],
961 struct btrfs_dev_extent);
962 BUG_ON(found_key.offset > start || found_key.offset +
963 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400964 } else if (ret == 0) {
965 leaf = path->nodes[0];
966 extent = btrfs_item_ptr(leaf, path->slots[0],
967 struct btrfs_dev_extent);
968 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400969 BUG_ON(ret);
970
Chris Masondfe25022008-05-13 13:46:40 -0400971 if (device->bytes_used > 0)
972 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400973 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -0400974
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000975out:
Chris Mason8f18cf12008-04-25 16:53:30 -0400976 btrfs_free_path(path);
977 return ret;
978}
979
Yan Zheng2b820322008-11-17 21:11:30 -0500980int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400981 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400982 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500983 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400984{
985 int ret;
986 struct btrfs_path *path;
987 struct btrfs_root *root = device->dev_root;
988 struct btrfs_dev_extent *extent;
989 struct extent_buffer *leaf;
990 struct btrfs_key key;
991
Chris Masondfe25022008-05-13 13:46:40 -0400992 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400993 path = btrfs_alloc_path();
994 if (!path)
995 return -ENOMEM;
996
Chris Mason0b86a832008-03-24 15:01:56 -0400997 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500998 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400999 key.type = BTRFS_DEV_EXTENT_KEY;
1000 ret = btrfs_insert_empty_item(trans, root, path, &key,
1001 sizeof(*extent));
1002 BUG_ON(ret);
1003
1004 leaf = path->nodes[0];
1005 extent = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001007 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1008 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1009 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1010
1011 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1012 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1013 BTRFS_UUID_SIZE);
1014
Chris Mason0b86a832008-03-24 15:01:56 -04001015 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1016 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001017 btrfs_free_path(path);
1018 return ret;
1019}
1020
Chris Masona1b32a52008-09-05 16:09:51 -04001021static noinline int find_next_chunk(struct btrfs_root *root,
1022 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001023{
1024 struct btrfs_path *path;
1025 int ret;
1026 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001027 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001028 struct btrfs_key found_key;
1029
1030 path = btrfs_alloc_path();
1031 BUG_ON(!path);
1032
Chris Masone17cade2008-04-15 15:41:47 -04001033 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001034 key.offset = (u64)-1;
1035 key.type = BTRFS_CHUNK_ITEM_KEY;
1036
1037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1038 if (ret < 0)
1039 goto error;
1040
1041 BUG_ON(ret == 0);
1042
1043 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1044 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001045 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001046 } else {
1047 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1048 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001049 if (found_key.objectid != objectid)
1050 *offset = 0;
1051 else {
1052 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1053 struct btrfs_chunk);
1054 *offset = found_key.offset +
1055 btrfs_chunk_length(path->nodes[0], chunk);
1056 }
Chris Mason0b86a832008-03-24 15:01:56 -04001057 }
1058 ret = 0;
1059error:
1060 btrfs_free_path(path);
1061 return ret;
1062}
1063
Yan Zheng2b820322008-11-17 21:11:30 -05001064static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001065{
1066 int ret;
1067 struct btrfs_key key;
1068 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001069 struct btrfs_path *path;
1070
1071 root = root->fs_info->chunk_root;
1072
1073 path = btrfs_alloc_path();
1074 if (!path)
1075 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001076
1077 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1078 key.type = BTRFS_DEV_ITEM_KEY;
1079 key.offset = (u64)-1;
1080
1081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1082 if (ret < 0)
1083 goto error;
1084
1085 BUG_ON(ret == 0);
1086
1087 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1088 BTRFS_DEV_ITEM_KEY);
1089 if (ret) {
1090 *objectid = 1;
1091 } else {
1092 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1093 path->slots[0]);
1094 *objectid = found_key.offset + 1;
1095 }
1096 ret = 0;
1097error:
Yan Zheng2b820322008-11-17 21:11:30 -05001098 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001099 return ret;
1100}
1101
1102/*
1103 * the device information is stored in the chunk root
1104 * the btrfs_device struct should be fully filled in
1105 */
1106int btrfs_add_device(struct btrfs_trans_handle *trans,
1107 struct btrfs_root *root,
1108 struct btrfs_device *device)
1109{
1110 int ret;
1111 struct btrfs_path *path;
1112 struct btrfs_dev_item *dev_item;
1113 struct extent_buffer *leaf;
1114 struct btrfs_key key;
1115 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001116
1117 root = root->fs_info->chunk_root;
1118
1119 path = btrfs_alloc_path();
1120 if (!path)
1121 return -ENOMEM;
1122
Chris Mason0b86a832008-03-24 15:01:56 -04001123 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1124 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001125 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001126
1127 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001128 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001129 if (ret)
1130 goto out;
1131
1132 leaf = path->nodes[0];
1133 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1134
1135 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001136 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001137 btrfs_set_device_type(leaf, dev_item, device->type);
1138 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1139 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1140 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001141 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1142 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001143 btrfs_set_device_group(leaf, dev_item, 0);
1144 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1145 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001146 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001147
Chris Mason0b86a832008-03-24 15:01:56 -04001148 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001149 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001150 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1151 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001152 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001153
Yan Zheng2b820322008-11-17 21:11:30 -05001154 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001155out:
1156 btrfs_free_path(path);
1157 return ret;
1158}
Chris Mason8f18cf12008-04-25 16:53:30 -04001159
Chris Masona061fc82008-05-07 11:43:44 -04001160static int btrfs_rm_dev_item(struct btrfs_root *root,
1161 struct btrfs_device *device)
1162{
1163 int ret;
1164 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001165 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001166 struct btrfs_trans_handle *trans;
1167
1168 root = root->fs_info->chunk_root;
1169
1170 path = btrfs_alloc_path();
1171 if (!path)
1172 return -ENOMEM;
1173
Yan, Zhenga22285a2010-05-16 10:48:46 -04001174 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001175 if (IS_ERR(trans)) {
1176 btrfs_free_path(path);
1177 return PTR_ERR(trans);
1178 }
Chris Masona061fc82008-05-07 11:43:44 -04001179 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1180 key.type = BTRFS_DEV_ITEM_KEY;
1181 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001182 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001183
1184 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1185 if (ret < 0)
1186 goto out;
1187
1188 if (ret > 0) {
1189 ret = -ENOENT;
1190 goto out;
1191 }
1192
1193 ret = btrfs_del_item(trans, root, path);
1194 if (ret)
1195 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001196out:
1197 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001198 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001199 btrfs_commit_transaction(trans, root);
1200 return ret;
1201}
1202
1203int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1204{
1205 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001206 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001207 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001208 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001209 struct btrfs_super_block *disk_super;
1210 u64 all_avail;
1211 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001212 u64 num_devices;
1213 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001214 int ret = 0;
1215
Chris Masona061fc82008-05-07 11:43:44 -04001216 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001217 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001218
1219 all_avail = root->fs_info->avail_data_alloc_bits |
1220 root->fs_info->avail_system_alloc_bits |
1221 root->fs_info->avail_metadata_alloc_bits;
1222
1223 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001224 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001225 printk(KERN_ERR "btrfs: unable to go below four devices "
1226 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001227 ret = -EINVAL;
1228 goto out;
1229 }
1230
1231 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001232 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001233 printk(KERN_ERR "btrfs: unable to go below two "
1234 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001235 ret = -EINVAL;
1236 goto out;
1237 }
1238
Chris Masondfe25022008-05-13 13:46:40 -04001239 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001240 struct list_head *devices;
1241 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001242
Chris Masondfe25022008-05-13 13:46:40 -04001243 device = NULL;
1244 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001245 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001246 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001247 if (tmp->in_fs_metadata && !tmp->bdev) {
1248 device = tmp;
1249 break;
1250 }
1251 }
Chris Masone5e9a522009-06-10 15:17:02 -04001252 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001253 bdev = NULL;
1254 bh = NULL;
1255 disk_super = NULL;
1256 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001257 printk(KERN_ERR "btrfs: no missing devices found to "
1258 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001259 goto out;
1260 }
Chris Masondfe25022008-05-13 13:46:40 -04001261 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001262 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1263 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001264 if (IS_ERR(bdev)) {
1265 ret = PTR_ERR(bdev);
1266 goto out;
1267 }
1268
Yan Zheng2b820322008-11-17 21:11:30 -05001269 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001270 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001271 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001272 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001273 goto error_close;
1274 }
1275 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001276 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001277 dev_uuid = disk_super->dev_item.uuid;
1278 device = btrfs_find_device(root, devid, dev_uuid,
1279 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001280 if (!device) {
1281 ret = -ENOENT;
1282 goto error_brelse;
1283 }
Chris Masondfe25022008-05-13 13:46:40 -04001284 }
Yan Zheng2b820322008-11-17 21:11:30 -05001285
1286 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001287 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1288 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001289 ret = -EINVAL;
1290 goto error_brelse;
1291 }
1292
1293 if (device->writeable) {
1294 list_del_init(&device->dev_alloc_list);
1295 root->fs_info->fs_devices->rw_devices--;
1296 }
Chris Masona061fc82008-05-07 11:43:44 -04001297
1298 ret = btrfs_shrink_device(device, 0);
1299 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001300 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001301
Chris Masona061fc82008-05-07 11:43:44 -04001302 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1303 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001304 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001305
Yan Zheng2b820322008-11-17 21:11:30 -05001306 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001307
1308 /*
1309 * the device list mutex makes sure that we don't change
1310 * the device list while someone else is writing out all
1311 * the device supers.
1312 */
1313 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001315 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1316
Yan Zhenge4404d62008-12-12 10:03:26 -05001317 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001318
Chris Masoncd02dca2010-12-13 14:56:23 -05001319 if (device->missing)
1320 root->fs_info->fs_devices->missing_devices--;
1321
Yan Zheng2b820322008-11-17 21:11:30 -05001322 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1323 struct btrfs_device, dev_list);
1324 if (device->bdev == root->fs_info->sb->s_bdev)
1325 root->fs_info->sb->s_bdev = next_device->bdev;
1326 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1327 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1328
Yan Zhenge4404d62008-12-12 10:03:26 -05001329 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001330 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001331 device->bdev = NULL;
1332 device->fs_devices->open_devices--;
1333 }
1334
Yan Zheng2b820322008-11-17 21:11:30 -05001335 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1336 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1337
Yan Zhenge4404d62008-12-12 10:03:26 -05001338 if (device->fs_devices->open_devices == 0) {
1339 struct btrfs_fs_devices *fs_devices;
1340 fs_devices = root->fs_info->fs_devices;
1341 while (fs_devices) {
1342 if (fs_devices->seed == device->fs_devices)
1343 break;
1344 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001345 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001346 fs_devices->seed = device->fs_devices->seed;
1347 device->fs_devices->seed = NULL;
1348 __btrfs_close_devices(device->fs_devices);
1349 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001350 }
1351
1352 /*
1353 * at this point, the device is zero sized. We want to
1354 * remove it from the devices list and zero out the old super
1355 */
1356 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001357 /* make sure this device isn't detected as part of
1358 * the FS anymore
1359 */
1360 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1361 set_buffer_dirty(bh);
1362 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001363 }
Chris Masona061fc82008-05-07 11:43:44 -04001364
Chris Masona061fc82008-05-07 11:43:44 -04001365 kfree(device->name);
1366 kfree(device);
1367 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001368
1369error_brelse:
1370 brelse(bh);
1371error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001372 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001373 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001374out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001375 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001376 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001377 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001378error_undo:
1379 if (device->writeable) {
1380 list_add(&device->dev_alloc_list,
1381 &root->fs_info->fs_devices->alloc_list);
1382 root->fs_info->fs_devices->rw_devices++;
1383 }
1384 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001385}
1386
Yan Zheng2b820322008-11-17 21:11:30 -05001387/*
1388 * does all the dirty work required for changing file system's UUID.
1389 */
1390static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1391 struct btrfs_root *root)
1392{
1393 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1394 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001395 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001396 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1397 struct btrfs_device *device;
1398 u64 super_flags;
1399
1400 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001401 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001402 return -EINVAL;
1403
Yan Zhenge4404d62008-12-12 10:03:26 -05001404 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1405 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001406 return -ENOMEM;
1407
Yan Zhenge4404d62008-12-12 10:03:26 -05001408 old_devices = clone_fs_devices(fs_devices);
1409 if (IS_ERR(old_devices)) {
1410 kfree(seed_devices);
1411 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001412 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001413
Yan Zheng2b820322008-11-17 21:11:30 -05001414 list_add(&old_devices->list, &fs_uuids);
1415
Yan Zhenge4404d62008-12-12 10:03:26 -05001416 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1417 seed_devices->opened = 1;
1418 INIT_LIST_HEAD(&seed_devices->devices);
1419 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001420 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001421
1422 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001423 list_splice_init(&fs_devices->devices, &seed_devices->devices);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001424 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1425
Yan Zhenge4404d62008-12-12 10:03:26 -05001426 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1427 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1428 device->fs_devices = seed_devices;
1429 }
1430
Yan Zheng2b820322008-11-17 21:11:30 -05001431 fs_devices->seeding = 0;
1432 fs_devices->num_devices = 0;
1433 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001434 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001435
1436 generate_random_uuid(fs_devices->fsid);
1437 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1438 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1439 super_flags = btrfs_super_flags(disk_super) &
1440 ~BTRFS_SUPER_FLAG_SEEDING;
1441 btrfs_set_super_flags(disk_super, super_flags);
1442
1443 return 0;
1444}
1445
1446/*
1447 * strore the expected generation for seed devices in device items.
1448 */
1449static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1450 struct btrfs_root *root)
1451{
1452 struct btrfs_path *path;
1453 struct extent_buffer *leaf;
1454 struct btrfs_dev_item *dev_item;
1455 struct btrfs_device *device;
1456 struct btrfs_key key;
1457 u8 fs_uuid[BTRFS_UUID_SIZE];
1458 u8 dev_uuid[BTRFS_UUID_SIZE];
1459 u64 devid;
1460 int ret;
1461
1462 path = btrfs_alloc_path();
1463 if (!path)
1464 return -ENOMEM;
1465
1466 root = root->fs_info->chunk_root;
1467 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1468 key.offset = 0;
1469 key.type = BTRFS_DEV_ITEM_KEY;
1470
1471 while (1) {
1472 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1473 if (ret < 0)
1474 goto error;
1475
1476 leaf = path->nodes[0];
1477next_slot:
1478 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1479 ret = btrfs_next_leaf(root, path);
1480 if (ret > 0)
1481 break;
1482 if (ret < 0)
1483 goto error;
1484 leaf = path->nodes[0];
1485 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1486 btrfs_release_path(root, path);
1487 continue;
1488 }
1489
1490 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1491 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1492 key.type != BTRFS_DEV_ITEM_KEY)
1493 break;
1494
1495 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1496 struct btrfs_dev_item);
1497 devid = btrfs_device_id(leaf, dev_item);
1498 read_extent_buffer(leaf, dev_uuid,
1499 (unsigned long)btrfs_device_uuid(dev_item),
1500 BTRFS_UUID_SIZE);
1501 read_extent_buffer(leaf, fs_uuid,
1502 (unsigned long)btrfs_device_fsid(dev_item),
1503 BTRFS_UUID_SIZE);
1504 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1505 BUG_ON(!device);
1506
1507 if (device->fs_devices->seeding) {
1508 btrfs_set_device_generation(leaf, dev_item,
1509 device->generation);
1510 btrfs_mark_buffer_dirty(leaf);
1511 }
1512
1513 path->slots[0]++;
1514 goto next_slot;
1515 }
1516 ret = 0;
1517error:
1518 btrfs_free_path(path);
1519 return ret;
1520}
1521
Chris Mason788f20e2008-04-28 15:29:42 -04001522int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1523{
1524 struct btrfs_trans_handle *trans;
1525 struct btrfs_device *device;
1526 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001527 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001528 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001529 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001530 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001531 int ret = 0;
1532
Yan Zheng2b820322008-11-17 21:11:30 -05001533 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1534 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001535
Tejun Heod4d77622010-11-13 11:55:18 +01001536 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1537 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001538 if (IS_ERR(bdev))
1539 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001540
Yan Zheng2b820322008-11-17 21:11:30 -05001541 if (root->fs_info->fs_devices->seeding) {
1542 seeding_dev = 1;
1543 down_write(&sb->s_umount);
1544 mutex_lock(&uuid_mutex);
1545 }
1546
Chris Mason8c8bee12008-09-29 11:19:10 -04001547 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001548 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001549
Chris Mason788f20e2008-04-28 15:29:42 -04001550 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001551 /*
1552 * we have the volume lock, so we don't need the extra
1553 * device list mutex while reading the list here.
1554 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001555 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001556 if (device->bdev == bdev) {
1557 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001558 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001559 }
1560 }
1561
1562 device = kzalloc(sizeof(*device), GFP_NOFS);
1563 if (!device) {
1564 /* we can safely leave the fs_devices entry around */
1565 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001566 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001567 }
1568
Chris Mason788f20e2008-04-28 15:29:42 -04001569 device->name = kstrdup(device_path, GFP_NOFS);
1570 if (!device->name) {
1571 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001572 ret = -ENOMEM;
1573 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001574 }
Yan Zheng2b820322008-11-17 21:11:30 -05001575
1576 ret = find_next_devid(root, &device->devid);
1577 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001578 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001579 kfree(device);
1580 goto error;
1581 }
1582
Yan, Zhenga22285a2010-05-16 10:48:46 -04001583 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001584 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001585 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001586 kfree(device);
1587 ret = PTR_ERR(trans);
1588 goto error;
1589 }
1590
Yan Zheng2b820322008-11-17 21:11:30 -05001591 lock_chunks(root);
1592
Yan Zheng2b820322008-11-17 21:11:30 -05001593 device->writeable = 1;
1594 device->work.func = pending_bios_fn;
1595 generate_random_uuid(device->uuid);
1596 spin_lock_init(&device->io_lock);
1597 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001598 device->io_width = root->sectorsize;
1599 device->io_align = root->sectorsize;
1600 device->sector_size = root->sectorsize;
1601 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001602 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001603 device->dev_root = root->fs_info->dev_root;
1604 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001605 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001606 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001607 set_blocksize(device->bdev, 4096);
1608
Yan Zheng2b820322008-11-17 21:11:30 -05001609 if (seeding_dev) {
1610 sb->s_flags &= ~MS_RDONLY;
1611 ret = btrfs_prepare_sprout(trans, root);
1612 BUG_ON(ret);
1613 }
1614
1615 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001616
1617 /*
1618 * we don't want write_supers to jump in here with our device
1619 * half setup
1620 */
1621 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001622 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1623 list_add(&device->dev_alloc_list,
1624 &root->fs_info->fs_devices->alloc_list);
1625 root->fs_info->fs_devices->num_devices++;
1626 root->fs_info->fs_devices->open_devices++;
1627 root->fs_info->fs_devices->rw_devices++;
1628 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1629
Chris Masonc289811c2009-06-10 09:51:32 -04001630 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1631 root->fs_info->fs_devices->rotating = 1;
1632
Chris Mason788f20e2008-04-28 15:29:42 -04001633 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1634 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1635 total_bytes + device->total_bytes);
1636
1637 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1638 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1639 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001640 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001641
Yan Zheng2b820322008-11-17 21:11:30 -05001642 if (seeding_dev) {
1643 ret = init_first_rw_device(trans, root, device);
1644 BUG_ON(ret);
1645 ret = btrfs_finish_sprout(trans, root);
1646 BUG_ON(ret);
1647 } else {
1648 ret = btrfs_add_device(trans, root, device);
1649 }
1650
Chris Mason913d9522009-03-10 13:17:18 -04001651 /*
1652 * we've got more storage, clear any full flags on the space
1653 * infos
1654 */
1655 btrfs_clear_space_info_full(root->fs_info);
1656
Chris Mason7d9eb122008-07-08 14:19:17 -04001657 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001658 btrfs_commit_transaction(trans, root);
1659
1660 if (seeding_dev) {
1661 mutex_unlock(&uuid_mutex);
1662 up_write(&sb->s_umount);
1663
1664 ret = btrfs_relocate_sys_chunks(root);
1665 BUG_ON(ret);
1666 }
1667out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001668 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001669 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001670error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001671 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001672 if (seeding_dev) {
1673 mutex_unlock(&uuid_mutex);
1674 up_write(&sb->s_umount);
1675 }
Chris Mason788f20e2008-04-28 15:29:42 -04001676 goto out;
1677}
1678
Chris Masond3977122009-01-05 21:25:51 -05001679static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1680 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001681{
1682 int ret;
1683 struct btrfs_path *path;
1684 struct btrfs_root *root;
1685 struct btrfs_dev_item *dev_item;
1686 struct extent_buffer *leaf;
1687 struct btrfs_key key;
1688
1689 root = device->dev_root->fs_info->chunk_root;
1690
1691 path = btrfs_alloc_path();
1692 if (!path)
1693 return -ENOMEM;
1694
1695 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1696 key.type = BTRFS_DEV_ITEM_KEY;
1697 key.offset = device->devid;
1698
1699 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1700 if (ret < 0)
1701 goto out;
1702
1703 if (ret > 0) {
1704 ret = -ENOENT;
1705 goto out;
1706 }
1707
1708 leaf = path->nodes[0];
1709 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1710
1711 btrfs_set_device_id(leaf, dev_item, device->devid);
1712 btrfs_set_device_type(leaf, dev_item, device->type);
1713 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1714 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1715 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001716 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001717 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1718 btrfs_mark_buffer_dirty(leaf);
1719
1720out:
1721 btrfs_free_path(path);
1722 return ret;
1723}
1724
Chris Mason7d9eb122008-07-08 14:19:17 -04001725static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001726 struct btrfs_device *device, u64 new_size)
1727{
1728 struct btrfs_super_block *super_copy =
1729 &device->dev_root->fs_info->super_copy;
1730 u64 old_total = btrfs_super_total_bytes(super_copy);
1731 u64 diff = new_size - device->total_bytes;
1732
Yan Zheng2b820322008-11-17 21:11:30 -05001733 if (!device->writeable)
1734 return -EACCES;
1735 if (new_size <= device->total_bytes)
1736 return -EINVAL;
1737
Chris Mason8f18cf12008-04-25 16:53:30 -04001738 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001739 device->fs_devices->total_rw_bytes += diff;
1740
1741 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001742 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001743 btrfs_clear_space_info_full(device->dev_root->fs_info);
1744
Chris Mason8f18cf12008-04-25 16:53:30 -04001745 return btrfs_update_device(trans, device);
1746}
1747
Chris Mason7d9eb122008-07-08 14:19:17 -04001748int btrfs_grow_device(struct btrfs_trans_handle *trans,
1749 struct btrfs_device *device, u64 new_size)
1750{
1751 int ret;
1752 lock_chunks(device->dev_root);
1753 ret = __btrfs_grow_device(trans, device, new_size);
1754 unlock_chunks(device->dev_root);
1755 return ret;
1756}
1757
Chris Mason8f18cf12008-04-25 16:53:30 -04001758static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1759 struct btrfs_root *root,
1760 u64 chunk_tree, u64 chunk_objectid,
1761 u64 chunk_offset)
1762{
1763 int ret;
1764 struct btrfs_path *path;
1765 struct btrfs_key key;
1766
1767 root = root->fs_info->chunk_root;
1768 path = btrfs_alloc_path();
1769 if (!path)
1770 return -ENOMEM;
1771
1772 key.objectid = chunk_objectid;
1773 key.offset = chunk_offset;
1774 key.type = BTRFS_CHUNK_ITEM_KEY;
1775
1776 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1777 BUG_ON(ret);
1778
1779 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001780
1781 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001782 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001783}
1784
Christoph Hellwigb2950862008-12-02 09:54:17 -05001785static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001786 chunk_offset)
1787{
1788 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1789 struct btrfs_disk_key *disk_key;
1790 struct btrfs_chunk *chunk;
1791 u8 *ptr;
1792 int ret = 0;
1793 u32 num_stripes;
1794 u32 array_size;
1795 u32 len = 0;
1796 u32 cur;
1797 struct btrfs_key key;
1798
1799 array_size = btrfs_super_sys_array_size(super_copy);
1800
1801 ptr = super_copy->sys_chunk_array;
1802 cur = 0;
1803
1804 while (cur < array_size) {
1805 disk_key = (struct btrfs_disk_key *)ptr;
1806 btrfs_disk_key_to_cpu(&key, disk_key);
1807
1808 len = sizeof(*disk_key);
1809
1810 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1811 chunk = (struct btrfs_chunk *)(ptr + len);
1812 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1813 len += btrfs_chunk_item_size(num_stripes);
1814 } else {
1815 ret = -EIO;
1816 break;
1817 }
1818 if (key.objectid == chunk_objectid &&
1819 key.offset == chunk_offset) {
1820 memmove(ptr, ptr + len, array_size - (cur + len));
1821 array_size -= len;
1822 btrfs_set_super_sys_array_size(super_copy, array_size);
1823 } else {
1824 ptr += len;
1825 cur += len;
1826 }
1827 }
1828 return ret;
1829}
1830
Christoph Hellwigb2950862008-12-02 09:54:17 -05001831static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001832 u64 chunk_tree, u64 chunk_objectid,
1833 u64 chunk_offset)
1834{
1835 struct extent_map_tree *em_tree;
1836 struct btrfs_root *extent_root;
1837 struct btrfs_trans_handle *trans;
1838 struct extent_map *em;
1839 struct map_lookup *map;
1840 int ret;
1841 int i;
1842
1843 root = root->fs_info->chunk_root;
1844 extent_root = root->fs_info->extent_root;
1845 em_tree = &root->fs_info->mapping_tree.map_tree;
1846
Josef Bacikba1bf482009-09-11 16:11:19 -04001847 ret = btrfs_can_relocate(extent_root, chunk_offset);
1848 if (ret)
1849 return -ENOSPC;
1850
Chris Mason8f18cf12008-04-25 16:53:30 -04001851 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001852 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001853 if (ret)
1854 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001855
Yan, Zhenga22285a2010-05-16 10:48:46 -04001856 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001857 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001858
Chris Mason7d9eb122008-07-08 14:19:17 -04001859 lock_chunks(root);
1860
Chris Mason8f18cf12008-04-25 16:53:30 -04001861 /*
1862 * step two, delete the device extents and the
1863 * chunk tree entries
1864 */
Chris Mason890871b2009-09-02 16:24:52 -04001865 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001866 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001867 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001868
Chris Masona061fc82008-05-07 11:43:44 -04001869 BUG_ON(em->start > chunk_offset ||
1870 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001871 map = (struct map_lookup *)em->bdev;
1872
1873 for (i = 0; i < map->num_stripes; i++) {
1874 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1875 map->stripes[i].physical);
1876 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001877
Chris Masondfe25022008-05-13 13:46:40 -04001878 if (map->stripes[i].dev) {
1879 ret = btrfs_update_device(trans, map->stripes[i].dev);
1880 BUG_ON(ret);
1881 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001882 }
1883 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1884 chunk_offset);
1885
1886 BUG_ON(ret);
1887
liubo1abe9b82011-03-24 11:18:59 +00001888 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1889
Chris Mason8f18cf12008-04-25 16:53:30 -04001890 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1891 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1892 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001893 }
1894
Zheng Yan1a40e232008-09-26 10:09:34 -04001895 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1896 BUG_ON(ret);
1897
Chris Mason890871b2009-09-02 16:24:52 -04001898 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001899 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001900 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001901
Chris Mason8f18cf12008-04-25 16:53:30 -04001902 kfree(map);
1903 em->bdev = NULL;
1904
1905 /* once for the tree */
1906 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001907 /* once for us */
1908 free_extent_map(em);
1909
Chris Mason7d9eb122008-07-08 14:19:17 -04001910 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001911 btrfs_end_transaction(trans, root);
1912 return 0;
1913}
1914
Yan Zheng2b820322008-11-17 21:11:30 -05001915static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1916{
1917 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1918 struct btrfs_path *path;
1919 struct extent_buffer *leaf;
1920 struct btrfs_chunk *chunk;
1921 struct btrfs_key key;
1922 struct btrfs_key found_key;
1923 u64 chunk_tree = chunk_root->root_key.objectid;
1924 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001925 bool retried = false;
1926 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001927 int ret;
1928
1929 path = btrfs_alloc_path();
1930 if (!path)
1931 return -ENOMEM;
1932
Josef Bacikba1bf482009-09-11 16:11:19 -04001933again:
Yan Zheng2b820322008-11-17 21:11:30 -05001934 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1935 key.offset = (u64)-1;
1936 key.type = BTRFS_CHUNK_ITEM_KEY;
1937
1938 while (1) {
1939 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1940 if (ret < 0)
1941 goto error;
1942 BUG_ON(ret == 0);
1943
1944 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1945 key.type);
1946 if (ret < 0)
1947 goto error;
1948 if (ret > 0)
1949 break;
1950
1951 leaf = path->nodes[0];
1952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1953
1954 chunk = btrfs_item_ptr(leaf, path->slots[0],
1955 struct btrfs_chunk);
1956 chunk_type = btrfs_chunk_type(leaf, chunk);
1957 btrfs_release_path(chunk_root, path);
1958
1959 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1960 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1961 found_key.objectid,
1962 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001963 if (ret == -ENOSPC)
1964 failed++;
1965 else if (ret)
1966 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001967 }
1968
1969 if (found_key.offset == 0)
1970 break;
1971 key.offset = found_key.offset - 1;
1972 }
1973 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001974 if (failed && !retried) {
1975 failed = 0;
1976 retried = true;
1977 goto again;
1978 } else if (failed && retried) {
1979 WARN_ON(1);
1980 ret = -ENOSPC;
1981 }
Yan Zheng2b820322008-11-17 21:11:30 -05001982error:
1983 btrfs_free_path(path);
1984 return ret;
1985}
1986
Chris Masonec44a352008-04-28 15:29:52 -04001987static u64 div_factor(u64 num, int factor)
1988{
1989 if (factor == 10)
1990 return num;
1991 num *= factor;
1992 do_div(num, 10);
1993 return num;
1994}
1995
Chris Masonec44a352008-04-28 15:29:52 -04001996int btrfs_balance(struct btrfs_root *dev_root)
1997{
1998 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001999 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2000 struct btrfs_device *device;
2001 u64 old_size;
2002 u64 size_to_free;
2003 struct btrfs_path *path;
2004 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002005 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2006 struct btrfs_trans_handle *trans;
2007 struct btrfs_key found_key;
2008
Yan Zheng2b820322008-11-17 21:11:30 -05002009 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2010 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002011
Ben Hutchings6f88a442010-12-29 14:55:03 +00002012 if (!capable(CAP_SYS_ADMIN))
2013 return -EPERM;
2014
Chris Mason7d9eb122008-07-08 14:19:17 -04002015 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002016 dev_root = dev_root->fs_info->dev_root;
2017
Chris Masonec44a352008-04-28 15:29:52 -04002018 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002019 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002020 old_size = device->total_bytes;
2021 size_to_free = div_factor(old_size, 1);
2022 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002023 if (!device->writeable ||
2024 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002025 continue;
2026
2027 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002028 if (ret == -ENOSPC)
2029 break;
Chris Masonec44a352008-04-28 15:29:52 -04002030 BUG_ON(ret);
2031
Yan, Zhenga22285a2010-05-16 10:48:46 -04002032 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002033 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002034
2035 ret = btrfs_grow_device(trans, device, old_size);
2036 BUG_ON(ret);
2037
2038 btrfs_end_transaction(trans, dev_root);
2039 }
2040
2041 /* step two, relocate all the chunks */
2042 path = btrfs_alloc_path();
2043 BUG_ON(!path);
2044
2045 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2046 key.offset = (u64)-1;
2047 key.type = BTRFS_CHUNK_ITEM_KEY;
2048
Chris Masond3977122009-01-05 21:25:51 -05002049 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002050 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2051 if (ret < 0)
2052 goto error;
2053
2054 /*
2055 * this shouldn't happen, it means the last relocate
2056 * failed
2057 */
2058 if (ret == 0)
2059 break;
2060
2061 ret = btrfs_previous_item(chunk_root, path, 0,
2062 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002063 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002064 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002065
Chris Masonec44a352008-04-28 15:29:52 -04002066 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2067 path->slots[0]);
2068 if (found_key.objectid != key.objectid)
2069 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002070
Chris Masonec44a352008-04-28 15:29:52 -04002071 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002072 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002073 break;
2074
Chris Mason7d9eb122008-07-08 14:19:17 -04002075 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002076 ret = btrfs_relocate_chunk(chunk_root,
2077 chunk_root->root_key.objectid,
2078 found_key.objectid,
2079 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002080 BUG_ON(ret && ret != -ENOSPC);
2081 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002082 }
2083 ret = 0;
2084error:
2085 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002086 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002087 return ret;
2088}
2089
Chris Mason8f18cf12008-04-25 16:53:30 -04002090/*
2091 * shrinking a device means finding all of the device extents past
2092 * the new size, and then following the back refs to the chunks.
2093 * The chunk relocation code actually frees the device extent
2094 */
2095int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2096{
2097 struct btrfs_trans_handle *trans;
2098 struct btrfs_root *root = device->dev_root;
2099 struct btrfs_dev_extent *dev_extent = NULL;
2100 struct btrfs_path *path;
2101 u64 length;
2102 u64 chunk_tree;
2103 u64 chunk_objectid;
2104 u64 chunk_offset;
2105 int ret;
2106 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002107 int failed = 0;
2108 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002109 struct extent_buffer *l;
2110 struct btrfs_key key;
2111 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2112 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002113 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002114 u64 diff = device->total_bytes - new_size;
2115
Yan Zheng2b820322008-11-17 21:11:30 -05002116 if (new_size >= device->total_bytes)
2117 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002118
2119 path = btrfs_alloc_path();
2120 if (!path)
2121 return -ENOMEM;
2122
Chris Mason8f18cf12008-04-25 16:53:30 -04002123 path->reada = 2;
2124
Chris Mason7d9eb122008-07-08 14:19:17 -04002125 lock_chunks(root);
2126
Chris Mason8f18cf12008-04-25 16:53:30 -04002127 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002128 if (device->writeable)
2129 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002130 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002131
Josef Bacikba1bf482009-09-11 16:11:19 -04002132again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002133 key.objectid = device->devid;
2134 key.offset = (u64)-1;
2135 key.type = BTRFS_DEV_EXTENT_KEY;
2136
2137 while (1) {
2138 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2139 if (ret < 0)
2140 goto done;
2141
2142 ret = btrfs_previous_item(root, path, 0, key.type);
2143 if (ret < 0)
2144 goto done;
2145 if (ret) {
2146 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002147 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002148 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002149 }
2150
2151 l = path->nodes[0];
2152 slot = path->slots[0];
2153 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2154
Josef Bacikba1bf482009-09-11 16:11:19 -04002155 if (key.objectid != device->devid) {
2156 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002157 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002158 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002159
2160 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2161 length = btrfs_dev_extent_length(l, dev_extent);
2162
Josef Bacikba1bf482009-09-11 16:11:19 -04002163 if (key.offset + length <= new_size) {
2164 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002165 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002166 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002167
2168 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2169 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2170 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2171 btrfs_release_path(root, path);
2172
2173 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2174 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002175 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002176 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002177 if (ret == -ENOSPC)
2178 failed++;
2179 key.offset -= 1;
2180 }
2181
2182 if (failed && !retried) {
2183 failed = 0;
2184 retried = true;
2185 goto again;
2186 } else if (failed && retried) {
2187 ret = -ENOSPC;
2188 lock_chunks(root);
2189
2190 device->total_bytes = old_size;
2191 if (device->writeable)
2192 device->fs_devices->total_rw_bytes += diff;
2193 unlock_chunks(root);
2194 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002195 }
2196
Chris Balld6397ba2009-04-27 07:29:03 -04002197 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002198 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002199 if (IS_ERR(trans)) {
2200 ret = PTR_ERR(trans);
2201 goto done;
2202 }
2203
Chris Balld6397ba2009-04-27 07:29:03 -04002204 lock_chunks(root);
2205
2206 device->disk_total_bytes = new_size;
2207 /* Now btrfs_update_device() will change the on-disk size. */
2208 ret = btrfs_update_device(trans, device);
2209 if (ret) {
2210 unlock_chunks(root);
2211 btrfs_end_transaction(trans, root);
2212 goto done;
2213 }
2214 WARN_ON(diff > old_total);
2215 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2216 unlock_chunks(root);
2217 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002218done:
2219 btrfs_free_path(path);
2220 return ret;
2221}
2222
Christoph Hellwigb2950862008-12-02 09:54:17 -05002223static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002224 struct btrfs_root *root,
2225 struct btrfs_key *key,
2226 struct btrfs_chunk *chunk, int item_size)
2227{
2228 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2229 struct btrfs_disk_key disk_key;
2230 u32 array_size;
2231 u8 *ptr;
2232
2233 array_size = btrfs_super_sys_array_size(super_copy);
2234 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2235 return -EFBIG;
2236
2237 ptr = super_copy->sys_chunk_array + array_size;
2238 btrfs_cpu_key_to_disk(&disk_key, key);
2239 memcpy(ptr, &disk_key, sizeof(disk_key));
2240 ptr += sizeof(disk_key);
2241 memcpy(ptr, chunk, item_size);
2242 item_size += sizeof(disk_key);
2243 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2244 return 0;
2245}
2246
Chris Masond3977122009-01-05 21:25:51 -05002247static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002248 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002249{
2250 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2251 return calc_size;
2252 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2253 return calc_size * (num_stripes / sub_stripes);
2254 else
2255 return calc_size * num_stripes;
2256}
2257
Miao Xieb2117a32011-01-05 10:07:28 +00002258/* Used to sort the devices by max_avail(descending sort) */
2259int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
Chris Mason0b86a832008-03-24 15:01:56 -04002260{
Miao Xieb2117a32011-01-05 10:07:28 +00002261 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2262 ((struct btrfs_device_info *)dev_info2)->max_avail)
2263 return -1;
2264 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2265 ((struct btrfs_device_info *)dev_info2)->max_avail)
2266 return 1;
2267 else
2268 return 0;
2269}
Chris Mason0b86a832008-03-24 15:01:56 -04002270
Miao Xieb2117a32011-01-05 10:07:28 +00002271static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2272 int *num_stripes, int *min_stripes,
2273 int *sub_stripes)
2274{
2275 *num_stripes = 1;
2276 *min_stripes = 1;
2277 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002278
Chris Masona40a90a2008-04-18 11:55:51 -04002279 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002280 *num_stripes = fs_devices->rw_devices;
2281 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002282 }
2283 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002284 *num_stripes = 2;
2285 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002286 }
Chris Mason8790d502008-04-03 16:29:03 -04002287 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002288 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002289 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002290 *num_stripes = 2;
2291 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002292 }
Chris Mason321aecc2008-04-16 10:49:51 -04002293 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002294 *num_stripes = fs_devices->rw_devices;
2295 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002296 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002297 *num_stripes &= ~(u32)1;
2298 *sub_stripes = 2;
2299 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002300 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002301
Miao Xieb2117a32011-01-05 10:07:28 +00002302 return 0;
2303}
2304
2305static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2306 u64 proposed_size, u64 type,
2307 int num_stripes, int small_stripe)
2308{
2309 int min_stripe_size = 1 * 1024 * 1024;
2310 u64 calc_size = proposed_size;
2311 u64 max_chunk_size = calc_size;
2312 int ncopies = 1;
2313
2314 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2315 BTRFS_BLOCK_GROUP_DUP |
2316 BTRFS_BLOCK_GROUP_RAID10))
2317 ncopies = 2;
2318
Chris Mason9b3f68b2008-04-18 10:29:51 -04002319 if (type & BTRFS_BLOCK_GROUP_DATA) {
2320 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002321 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002322 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002323 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002324 min_stripe_size = 32 * 1024 * 1024;
2325 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2326 calc_size = 8 * 1024 * 1024;
2327 max_chunk_size = calc_size * 2;
2328 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002329 }
2330
Yan Zheng2b820322008-11-17 21:11:30 -05002331 /* we don't want a chunk larger than 10% of writeable space */
2332 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2333 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002334
Miao Xie1974a3b2011-01-05 10:07:24 +00002335 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2336 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002337 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002338 do_div(calc_size, BTRFS_STRIPE_LEN);
2339 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002340 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002341
Chris Mason9b3f68b2008-04-18 10:29:51 -04002342 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002343 if (!small_stripe)
Josef Bacik0cad8a112010-03-17 20:45:56 +00002344 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002345
Chris Mason9f680ce2010-04-06 09:37:47 -04002346 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002347 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002348 * we end up with something bigger than a stripe
2349 */
Miao Xieb2117a32011-01-05 10:07:28 +00002350 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002351
Miao Xieb2117a32011-01-05 10:07:28 +00002352 do_div(calc_size, BTRFS_STRIPE_LEN);
2353 calc_size *= BTRFS_STRIPE_LEN;
2354
2355 return calc_size;
2356}
2357
2358static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2359 int num_stripes)
2360{
2361 struct map_lookup *new;
2362 size_t len = map_lookup_size(num_stripes);
2363
2364 BUG_ON(map->num_stripes < num_stripes);
2365
2366 if (map->num_stripes == num_stripes)
2367 return map;
2368
2369 new = kmalloc(len, GFP_NOFS);
2370 if (!new) {
2371 /* just change map->num_stripes */
2372 map->num_stripes = num_stripes;
2373 return map;
2374 }
2375
2376 memcpy(new, map, len);
2377 new->num_stripes = num_stripes;
2378 kfree(map);
2379 return new;
2380}
2381
2382/*
2383 * helper to allocate device space from btrfs_device_info, in which we stored
2384 * max free space information of every device. It is used when we can not
2385 * allocate chunks by default size.
2386 *
2387 * By this helper, we can allocate a new chunk as larger as possible.
2388 */
2389static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2390 struct btrfs_fs_devices *fs_devices,
2391 struct btrfs_device_info *devices,
2392 int nr_device, u64 type,
2393 struct map_lookup **map_lookup,
2394 int min_stripes, u64 *stripe_size)
2395{
2396 int i, index, sort_again = 0;
2397 int min_devices = min_stripes;
2398 u64 max_avail, min_free;
2399 struct map_lookup *map = *map_lookup;
2400 int ret;
2401
2402 if (nr_device < min_stripes)
2403 return -ENOSPC;
2404
2405 btrfs_descending_sort_devices(devices, nr_device);
2406
2407 max_avail = devices[0].max_avail;
2408 if (!max_avail)
2409 return -ENOSPC;
2410
2411 for (i = 0; i < nr_device; i++) {
2412 /*
2413 * if dev_offset = 0, it means the free space of this device
2414 * is less than what we need, and we didn't search max avail
2415 * extent on this device, so do it now.
2416 */
2417 if (!devices[i].dev_offset) {
2418 ret = find_free_dev_extent(trans, devices[i].dev,
2419 max_avail,
2420 &devices[i].dev_offset,
2421 &devices[i].max_avail);
2422 if (ret != 0 && ret != -ENOSPC)
2423 return ret;
2424 sort_again = 1;
2425 }
2426 }
2427
2428 /* we update the max avail free extent of each devices, sort again */
2429 if (sort_again)
2430 btrfs_descending_sort_devices(devices, nr_device);
2431
2432 if (type & BTRFS_BLOCK_GROUP_DUP)
2433 min_devices = 1;
2434
2435 if (!devices[min_devices - 1].max_avail)
2436 return -ENOSPC;
2437
2438 max_avail = devices[min_devices - 1].max_avail;
2439 if (type & BTRFS_BLOCK_GROUP_DUP)
2440 do_div(max_avail, 2);
2441
2442 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2443 min_stripes, 1);
2444 if (type & BTRFS_BLOCK_GROUP_DUP)
2445 min_free = max_avail * 2;
2446 else
2447 min_free = max_avail;
2448
2449 if (min_free > devices[min_devices - 1].max_avail)
2450 return -ENOSPC;
2451
2452 map = __shrink_map_lookup_stripes(map, min_stripes);
2453 *stripe_size = max_avail;
2454
2455 index = 0;
2456 for (i = 0; i < min_stripes; i++) {
2457 map->stripes[i].dev = devices[index].dev;
2458 map->stripes[i].physical = devices[index].dev_offset;
2459 if (type & BTRFS_BLOCK_GROUP_DUP) {
2460 i++;
2461 map->stripes[i].dev = devices[index].dev;
2462 map->stripes[i].physical = devices[index].dev_offset +
2463 max_avail;
2464 }
2465 index++;
2466 }
2467 *map_lookup = map;
2468
2469 return 0;
2470}
2471
2472static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2473 struct btrfs_root *extent_root,
2474 struct map_lookup **map_ret,
2475 u64 *num_bytes, u64 *stripe_size,
2476 u64 start, u64 type)
2477{
2478 struct btrfs_fs_info *info = extent_root->fs_info;
2479 struct btrfs_device *device = NULL;
2480 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2481 struct list_head *cur;
2482 struct map_lookup *map;
2483 struct extent_map_tree *em_tree;
2484 struct extent_map *em;
2485 struct btrfs_device_info *devices_info;
2486 struct list_head private_devs;
2487 u64 calc_size = 1024 * 1024 * 1024;
2488 u64 min_free;
2489 u64 avail;
2490 u64 dev_offset;
2491 int num_stripes;
2492 int min_stripes;
2493 int sub_stripes;
2494 int min_devices; /* the min number of devices we need */
2495 int i;
2496 int ret;
2497 int index;
2498
2499 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2500 (type & BTRFS_BLOCK_GROUP_DUP)) {
2501 WARN_ON(1);
2502 type &= ~BTRFS_BLOCK_GROUP_DUP;
2503 }
2504 if (list_empty(&fs_devices->alloc_list))
2505 return -ENOSPC;
2506
2507 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2508 &min_stripes, &sub_stripes);
2509 if (ret)
2510 return ret;
2511
2512 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2513 GFP_NOFS);
2514 if (!devices_info)
2515 return -ENOMEM;
2516
2517 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2518 if (!map) {
2519 ret = -ENOMEM;
2520 goto error;
2521 }
2522 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002523
Yan Zheng2b820322008-11-17 21:11:30 -05002524 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002525 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002526 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002527
Miao Xieb2117a32011-01-05 10:07:28 +00002528 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2529 num_stripes, 0);
2530
2531 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002532 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002533 min_devices = 1;
2534 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002535 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002536 min_devices = min_stripes;
2537 }
Chris Masonad5bd912008-04-21 08:28:10 -04002538
Yan Zheng2b820322008-11-17 21:11:30 -05002539 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002540 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002541 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002542 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002543 if (device->total_bytes > device->bytes_used)
2544 avail = device->total_bytes - device->bytes_used;
2545 else
2546 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002547 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002548
Chris Masondfe25022008-05-13 13:46:40 -04002549 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002550 ret = find_free_dev_extent(trans, device, min_free,
2551 &devices_info[i].dev_offset,
2552 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002553 if (ret == 0) {
2554 list_move_tail(&device->dev_alloc_list,
2555 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002556 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002557 map->stripes[index].physical =
2558 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002559 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002560 if (type & BTRFS_BLOCK_GROUP_DUP) {
2561 map->stripes[index].dev = device;
2562 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002563 devices_info[i].dev_offset +
2564 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002565 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002566 }
Miao Xieb2117a32011-01-05 10:07:28 +00002567 } else if (ret != -ENOSPC)
2568 goto error;
2569
2570 devices_info[i].dev = device;
2571 i++;
2572 } else if (device->in_fs_metadata &&
2573 avail >= BTRFS_STRIPE_LEN) {
2574 devices_info[i].dev = device;
2575 devices_info[i].max_avail = avail;
2576 i++;
2577 }
2578
Yan Zheng2b820322008-11-17 21:11:30 -05002579 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002580 break;
2581 }
Miao Xieb2117a32011-01-05 10:07:28 +00002582
Yan Zheng2b820322008-11-17 21:11:30 -05002583 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002584 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002585 if (index >= min_stripes) {
2586 num_stripes = index;
2587 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2588 num_stripes /= sub_stripes;
2589 num_stripes *= sub_stripes;
2590 }
Miao Xieb2117a32011-01-05 10:07:28 +00002591
2592 map = __shrink_map_lookup_stripes(map, num_stripes);
2593 } else if (i >= min_devices) {
2594 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2595 devices_info, i, type,
2596 &map, min_stripes,
2597 &calc_size);
2598 if (ret)
2599 goto error;
2600 } else {
2601 ret = -ENOSPC;
2602 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002603 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002604 }
Chris Mason593060d2008-03-25 16:50:33 -04002605 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002606 map->stripe_len = BTRFS_STRIPE_LEN;
2607 map->io_align = BTRFS_STRIPE_LEN;
2608 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002609 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002610 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002611
Yan Zheng2b820322008-11-17 21:11:30 -05002612 *map_ret = map;
2613 *stripe_size = calc_size;
2614 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002615 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002616
liubo1abe9b82011-03-24 11:18:59 +00002617 trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes);
2618
Chris Mason0b86a832008-03-24 15:01:56 -04002619 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002620 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002621 ret = -ENOMEM;
2622 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002623 }
Chris Mason0b86a832008-03-24 15:01:56 -04002624 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002625 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002626 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002627 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002628 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002629
Chris Mason0b86a832008-03-24 15:01:56 -04002630 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002631 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002632 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002633 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002634 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002635 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002636
2637 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2638 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2639 start, *num_bytes);
2640 BUG_ON(ret);
2641
2642 index = 0;
2643 while (index < map->num_stripes) {
2644 device = map->stripes[index].dev;
2645 dev_offset = map->stripes[index].physical;
2646
2647 ret = btrfs_alloc_dev_extent(trans, device,
2648 info->chunk_root->root_key.objectid,
2649 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2650 start, dev_offset, calc_size);
2651 BUG_ON(ret);
2652 index++;
2653 }
2654
Miao Xieb2117a32011-01-05 10:07:28 +00002655 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002656 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002657
2658error:
2659 kfree(map);
2660 kfree(devices_info);
2661 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002662}
2663
2664static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2665 struct btrfs_root *extent_root,
2666 struct map_lookup *map, u64 chunk_offset,
2667 u64 chunk_size, u64 stripe_size)
2668{
2669 u64 dev_offset;
2670 struct btrfs_key key;
2671 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2672 struct btrfs_device *device;
2673 struct btrfs_chunk *chunk;
2674 struct btrfs_stripe *stripe;
2675 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2676 int index = 0;
2677 int ret;
2678
2679 chunk = kzalloc(item_size, GFP_NOFS);
2680 if (!chunk)
2681 return -ENOMEM;
2682
2683 index = 0;
2684 while (index < map->num_stripes) {
2685 device = map->stripes[index].dev;
2686 device->bytes_used += stripe_size;
2687 ret = btrfs_update_device(trans, device);
2688 BUG_ON(ret);
2689 index++;
2690 }
2691
2692 index = 0;
2693 stripe = &chunk->stripe;
2694 while (index < map->num_stripes) {
2695 device = map->stripes[index].dev;
2696 dev_offset = map->stripes[index].physical;
2697
2698 btrfs_set_stack_stripe_devid(stripe, device->devid);
2699 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2700 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2701 stripe++;
2702 index++;
2703 }
2704
2705 btrfs_set_stack_chunk_length(chunk, chunk_size);
2706 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2707 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2708 btrfs_set_stack_chunk_type(chunk, map->type);
2709 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2710 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2711 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2712 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2713 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2714
2715 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2716 key.type = BTRFS_CHUNK_ITEM_KEY;
2717 key.offset = chunk_offset;
2718
2719 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2720 BUG_ON(ret);
2721
2722 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2723 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2724 item_size);
2725 BUG_ON(ret);
2726 }
liubo1abe9b82011-03-24 11:18:59 +00002727
Yan Zheng2b820322008-11-17 21:11:30 -05002728 kfree(chunk);
2729 return 0;
2730}
2731
2732/*
2733 * Chunk allocation falls into two parts. The first part does works
2734 * that make the new allocated chunk useable, but not do any operation
2735 * that modifies the chunk tree. The second part does the works that
2736 * require modifying the chunk tree. This division is important for the
2737 * bootstrap process of adding storage to a seed btrfs.
2738 */
2739int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2740 struct btrfs_root *extent_root, u64 type)
2741{
2742 u64 chunk_offset;
2743 u64 chunk_size;
2744 u64 stripe_size;
2745 struct map_lookup *map;
2746 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2747 int ret;
2748
2749 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2750 &chunk_offset);
2751 if (ret)
2752 return ret;
2753
2754 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2755 &stripe_size, chunk_offset, type);
2756 if (ret)
2757 return ret;
2758
2759 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2760 chunk_size, stripe_size);
2761 BUG_ON(ret);
2762 return 0;
2763}
2764
Chris Masond3977122009-01-05 21:25:51 -05002765static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002766 struct btrfs_root *root,
2767 struct btrfs_device *device)
2768{
2769 u64 chunk_offset;
2770 u64 sys_chunk_offset;
2771 u64 chunk_size;
2772 u64 sys_chunk_size;
2773 u64 stripe_size;
2774 u64 sys_stripe_size;
2775 u64 alloc_profile;
2776 struct map_lookup *map;
2777 struct map_lookup *sys_map;
2778 struct btrfs_fs_info *fs_info = root->fs_info;
2779 struct btrfs_root *extent_root = fs_info->extent_root;
2780 int ret;
2781
2782 ret = find_next_chunk(fs_info->chunk_root,
2783 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2784 BUG_ON(ret);
2785
2786 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2787 (fs_info->metadata_alloc_profile &
2788 fs_info->avail_metadata_alloc_bits);
2789 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2790
2791 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2792 &stripe_size, chunk_offset, alloc_profile);
2793 BUG_ON(ret);
2794
2795 sys_chunk_offset = chunk_offset + chunk_size;
2796
2797 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2798 (fs_info->system_alloc_profile &
2799 fs_info->avail_system_alloc_bits);
2800 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2801
2802 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2803 &sys_chunk_size, &sys_stripe_size,
2804 sys_chunk_offset, alloc_profile);
2805 BUG_ON(ret);
2806
2807 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2808 BUG_ON(ret);
2809
2810 /*
2811 * Modifying chunk tree needs allocating new blocks from both
2812 * system block group and metadata block group. So we only can
2813 * do operations require modifying the chunk tree after both
2814 * block groups were created.
2815 */
2816 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2817 chunk_size, stripe_size);
2818 BUG_ON(ret);
2819
2820 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2821 sys_chunk_offset, sys_chunk_size,
2822 sys_stripe_size);
2823 BUG_ON(ret);
2824 return 0;
2825}
2826
2827int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2828{
2829 struct extent_map *em;
2830 struct map_lookup *map;
2831 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2832 int readonly = 0;
2833 int i;
2834
Chris Mason890871b2009-09-02 16:24:52 -04002835 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002836 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002837 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002838 if (!em)
2839 return 1;
2840
Josef Bacikf48b9072010-01-27 02:07:59 +00002841 if (btrfs_test_opt(root, DEGRADED)) {
2842 free_extent_map(em);
2843 return 0;
2844 }
2845
Yan Zheng2b820322008-11-17 21:11:30 -05002846 map = (struct map_lookup *)em->bdev;
2847 for (i = 0; i < map->num_stripes; i++) {
2848 if (!map->stripes[i].dev->writeable) {
2849 readonly = 1;
2850 break;
2851 }
2852 }
2853 free_extent_map(em);
2854 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002855}
2856
2857void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2858{
2859 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2860}
2861
2862void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2863{
2864 struct extent_map *em;
2865
Chris Masond3977122009-01-05 21:25:51 -05002866 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002867 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002868 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2869 if (em)
2870 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002871 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002872 if (!em)
2873 break;
2874 kfree(em->bdev);
2875 /* once for us */
2876 free_extent_map(em);
2877 /* once for the tree */
2878 free_extent_map(em);
2879 }
2880}
2881
Chris Masonf1885912008-04-09 16:28:12 -04002882int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2883{
2884 struct extent_map *em;
2885 struct map_lookup *map;
2886 struct extent_map_tree *em_tree = &map_tree->map_tree;
2887 int ret;
2888
Chris Mason890871b2009-09-02 16:24:52 -04002889 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002890 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002891 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002892 BUG_ON(!em);
2893
2894 BUG_ON(em->start > logical || em->start + em->len < logical);
2895 map = (struct map_lookup *)em->bdev;
2896 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2897 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002898 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2899 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002900 else
2901 ret = 1;
2902 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002903 return ret;
2904}
2905
Chris Masondfe25022008-05-13 13:46:40 -04002906static int find_live_mirror(struct map_lookup *map, int first, int num,
2907 int optimal)
2908{
2909 int i;
2910 if (map->stripes[optimal].dev->bdev)
2911 return optimal;
2912 for (i = first; i < first + num; i++) {
2913 if (map->stripes[i].dev->bdev)
2914 return i;
2915 }
2916 /* we couldn't find one that doesn't fail. Just return something
2917 * and the io error handling code will clean up eventually
2918 */
2919 return optimal;
2920}
2921
Chris Masonf2d8d742008-04-21 10:03:05 -04002922static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2923 u64 logical, u64 *length,
2924 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002925 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002926{
2927 struct extent_map *em;
2928 struct map_lookup *map;
2929 struct extent_map_tree *em_tree = &map_tree->map_tree;
2930 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002931 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002932 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002933 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002934 u64 stripe_nr_orig;
2935 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002936 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002937 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002938 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002939 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002940 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002941 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002942 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002943
Li Dongyangfce3bb92011-03-24 10:24:26 +00002944 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002945 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002946again:
2947 if (multi_ret) {
2948 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2949 GFP_NOFS);
2950 if (!multi)
2951 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002952
2953 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002954 }
Chris Mason0b86a832008-03-24 15:01:56 -04002955
Chris Mason890871b2009-09-02 16:24:52 -04002956 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002957 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002958 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002959
Chris Mason3b951512008-04-17 11:29:12 -04002960 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002961 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2962 (unsigned long long)logical,
2963 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002964 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002965 }
Chris Mason0b86a832008-03-24 15:01:56 -04002966
2967 BUG_ON(em->start > logical || em->start + em->len < logical);
2968 map = (struct map_lookup *)em->bdev;
2969 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002970
Chris Masonf1885912008-04-09 16:28:12 -04002971 if (mirror_num > map->num_stripes)
2972 mirror_num = 0;
2973
Chris Masoncea9e442008-04-09 16:28:12 -04002974 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002975 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002976 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2977 BTRFS_BLOCK_GROUP_DUP)) {
2978 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002979 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002980 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2981 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002982 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002983 }
2984 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002985 if (rw & REQ_DISCARD) {
2986 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2987 BTRFS_BLOCK_GROUP_RAID1 |
2988 BTRFS_BLOCK_GROUP_DUP |
2989 BTRFS_BLOCK_GROUP_RAID10)) {
2990 stripes_required = map->num_stripes;
2991 }
2992 }
2993 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002994 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002995 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002996 free_extent_map(em);
2997 kfree(multi);
2998 goto again;
2999 }
Chris Mason593060d2008-03-25 16:50:33 -04003000 stripe_nr = offset;
3001 /*
3002 * stripe_nr counts the total number of stripes we have to stride
3003 * to get to this block
3004 */
3005 do_div(stripe_nr, map->stripe_len);
3006
3007 stripe_offset = stripe_nr * map->stripe_len;
3008 BUG_ON(offset < stripe_offset);
3009
3010 /* stripe_offset is the offset of this block in its stripe*/
3011 stripe_offset = offset - stripe_offset;
3012
Li Dongyangfce3bb92011-03-24 10:24:26 +00003013 if (rw & REQ_DISCARD)
3014 *length = min_t(u64, em->len - offset, *length);
3015 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3016 BTRFS_BLOCK_GROUP_RAID1 |
3017 BTRFS_BLOCK_GROUP_RAID10 |
3018 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04003019 /* we limit the length of each bio to what fits in a stripe */
3020 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003021 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003022 } else {
3023 *length = em->len - offset;
3024 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003025
Jens Axboe7eaceac2011-03-10 08:52:07 +01003026 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003027 goto out;
3028
Chris Masonf2d8d742008-04-21 10:03:05 -04003029 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003030 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003031 stripe_nr_orig = stripe_nr;
3032 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3033 (~(map->stripe_len - 1));
3034 do_div(stripe_nr_end, map->stripe_len);
3035 stripe_end_offset = stripe_nr_end * map->stripe_len -
3036 (offset + *length);
3037 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3038 if (rw & REQ_DISCARD)
3039 num_stripes = min_t(u64, map->num_stripes,
3040 stripe_nr_end - stripe_nr_orig);
3041 stripe_index = do_div(stripe_nr, map->num_stripes);
3042 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003043 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003044 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003045 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003046 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003047 else {
3048 stripe_index = find_live_mirror(map, 0,
3049 map->num_stripes,
3050 current->pid % map->num_stripes);
3051 }
Chris Mason2fff7342008-04-29 14:12:09 -04003052
Chris Mason611f0e02008-04-03 16:29:03 -04003053 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00003054 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003055 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003056 else if (mirror_num)
3057 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003058
Chris Mason321aecc2008-04-16 10:49:51 -04003059 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3060 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003061
3062 stripe_index = do_div(stripe_nr, factor);
3063 stripe_index *= map->sub_stripes;
3064
Jens Axboe7eaceac2011-03-10 08:52:07 +01003065 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003066 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003067 else if (rw & REQ_DISCARD)
3068 num_stripes = min_t(u64, map->sub_stripes *
3069 (stripe_nr_end - stripe_nr_orig),
3070 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003071 else if (mirror_num)
3072 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003073 else {
3074 stripe_index = find_live_mirror(map, stripe_index,
3075 map->sub_stripes, stripe_index +
3076 current->pid % map->sub_stripes);
3077 }
Chris Mason8790d502008-04-03 16:29:03 -04003078 } else {
3079 /*
3080 * after this do_div call, stripe_nr is the number of stripes
3081 * on this device we have to walk to find the data, and
3082 * stripe_index is the number of our device in the stripe array
3083 */
3084 stripe_index = do_div(stripe_nr, map->num_stripes);
3085 }
Chris Mason593060d2008-03-25 16:50:33 -04003086 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003087
Li Dongyangfce3bb92011-03-24 10:24:26 +00003088 if (rw & REQ_DISCARD) {
3089 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003090 multi->stripes[i].physical =
3091 map->stripes[stripe_index].physical +
3092 stripe_offset + stripe_nr * map->stripe_len;
3093 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003094
3095 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3096 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003097 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003098 int j;
3099
Chris Masond9d04872011-03-27 21:23:21 -04003100 div_u64_rem(stripe_nr_end - 1,
3101 map->num_stripes,
3102 &last_stripe);
3103
Li Dongyangfce3bb92011-03-24 10:24:26 +00003104 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003105 u32 test;
3106
3107 div_u64_rem(stripe_nr_end - 1 - j,
3108 map->num_stripes, &test);
3109 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003110 break;
3111 }
3112 stripes = stripe_nr_end - 1 - j;
3113 do_div(stripes, map->num_stripes);
3114 multi->stripes[i].length = map->stripe_len *
3115 (stripes - stripe_nr + 1);
3116
3117 if (i == 0) {
3118 multi->stripes[i].length -=
3119 stripe_offset;
3120 stripe_offset = 0;
3121 }
3122 if (stripe_index == last_stripe)
3123 multi->stripes[i].length -=
3124 stripe_end_offset;
3125 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3126 u64 stripes;
3127 int j;
3128 int factor = map->num_stripes /
3129 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003130 u32 last_stripe = 0;
3131
3132 div_u64_rem(stripe_nr_end - 1,
3133 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003134 last_stripe *= map->sub_stripes;
3135
3136 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003137 u32 test;
3138
3139 div_u64_rem(stripe_nr_end - 1 - j,
3140 factor, &test);
3141
3142 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003143 stripe_index / map->sub_stripes)
3144 break;
3145 }
3146 stripes = stripe_nr_end - 1 - j;
3147 do_div(stripes, factor);
3148 multi->stripes[i].length = map->stripe_len *
3149 (stripes - stripe_nr + 1);
3150
3151 if (i < map->sub_stripes) {
3152 multi->stripes[i].length -=
3153 stripe_offset;
3154 if (i == map->sub_stripes - 1)
3155 stripe_offset = 0;
3156 }
3157 if (stripe_index >= last_stripe &&
3158 stripe_index <= (last_stripe +
3159 map->sub_stripes - 1)) {
3160 multi->stripes[i].length -=
3161 stripe_end_offset;
3162 }
3163 } else
3164 multi->stripes[i].length = *length;
3165
3166 stripe_index++;
3167 if (stripe_index == map->num_stripes) {
3168 /* This could only happen for RAID0/10 */
3169 stripe_index = 0;
3170 stripe_nr++;
3171 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003172 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003173 } else {
3174 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003175 multi->stripes[i].physical =
3176 map->stripes[stripe_index].physical +
3177 stripe_offset +
3178 stripe_nr * map->stripe_len;
3179 multi->stripes[i].dev =
3180 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003181 stripe_index++;
3182 }
Chris Mason593060d2008-03-25 16:50:33 -04003183 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003184 if (multi_ret) {
3185 *multi_ret = multi;
3186 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003187 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003188 }
Chris Masoncea9e442008-04-09 16:28:12 -04003189out:
Chris Mason0b86a832008-03-24 15:01:56 -04003190 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003191 return 0;
3192}
3193
Chris Masonf2d8d742008-04-21 10:03:05 -04003194int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3195 u64 logical, u64 *length,
3196 struct btrfs_multi_bio **multi_ret, int mirror_num)
3197{
3198 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003199 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003200}
3201
Yan Zhenga512bbf2008-12-08 16:46:26 -05003202int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3203 u64 chunk_start, u64 physical, u64 devid,
3204 u64 **logical, int *naddrs, int *stripe_len)
3205{
3206 struct extent_map_tree *em_tree = &map_tree->map_tree;
3207 struct extent_map *em;
3208 struct map_lookup *map;
3209 u64 *buf;
3210 u64 bytenr;
3211 u64 length;
3212 u64 stripe_nr;
3213 int i, j, nr = 0;
3214
Chris Mason890871b2009-09-02 16:24:52 -04003215 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003216 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003217 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003218
3219 BUG_ON(!em || em->start != chunk_start);
3220 map = (struct map_lookup *)em->bdev;
3221
3222 length = em->len;
3223 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3224 do_div(length, map->num_stripes / map->sub_stripes);
3225 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3226 do_div(length, map->num_stripes);
3227
3228 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3229 BUG_ON(!buf);
3230
3231 for (i = 0; i < map->num_stripes; i++) {
3232 if (devid && map->stripes[i].dev->devid != devid)
3233 continue;
3234 if (map->stripes[i].physical > physical ||
3235 map->stripes[i].physical + length <= physical)
3236 continue;
3237
3238 stripe_nr = physical - map->stripes[i].physical;
3239 do_div(stripe_nr, map->stripe_len);
3240
3241 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3242 stripe_nr = stripe_nr * map->num_stripes + i;
3243 do_div(stripe_nr, map->sub_stripes);
3244 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3245 stripe_nr = stripe_nr * map->num_stripes + i;
3246 }
3247 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003248 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003249 for (j = 0; j < nr; j++) {
3250 if (buf[j] == bytenr)
3251 break;
3252 }
Chris Mason934d3752008-12-08 16:43:10 -05003253 if (j == nr) {
3254 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003255 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003256 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003257 }
3258
Yan Zhenga512bbf2008-12-08 16:46:26 -05003259 *logical = buf;
3260 *naddrs = nr;
3261 *stripe_len = map->stripe_len;
3262
3263 free_extent_map(em);
3264 return 0;
3265}
3266
Chris Mason8790d502008-04-03 16:29:03 -04003267static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003268{
Chris Masoncea9e442008-04-09 16:28:12 -04003269 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003270 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003271
Chris Mason8790d502008-04-03 16:29:03 -04003272 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003273 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003274
Chris Mason7d2b4da2008-08-05 10:13:57 -04003275 if (bio == multi->orig_bio)
3276 is_orig_bio = 1;
3277
Chris Masoncea9e442008-04-09 16:28:12 -04003278 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003279 if (!is_orig_bio) {
3280 bio_put(bio);
3281 bio = multi->orig_bio;
3282 }
Chris Mason8790d502008-04-03 16:29:03 -04003283 bio->bi_private = multi->private;
3284 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003285 /* only send an error to the higher layers if it is
3286 * beyond the tolerance of the multi-bio
3287 */
Chris Mason1259ab72008-05-12 13:39:03 -04003288 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003289 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003290 } else if (err) {
3291 /*
3292 * this bio is actually up to date, we didn't
3293 * go over the max number of errors
3294 */
3295 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003296 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003297 }
Chris Mason8790d502008-04-03 16:29:03 -04003298 kfree(multi);
3299
3300 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003301 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003302 bio_put(bio);
3303 }
Chris Mason8790d502008-04-03 16:29:03 -04003304}
3305
Chris Mason8b712842008-06-11 16:50:36 -04003306struct async_sched {
3307 struct bio *bio;
3308 int rw;
3309 struct btrfs_fs_info *info;
3310 struct btrfs_work work;
3311};
3312
3313/*
3314 * see run_scheduled_bios for a description of why bios are collected for
3315 * async submit.
3316 *
3317 * This will add one bio to the pending list for a device and make sure
3318 * the work struct is scheduled.
3319 */
Chris Masond3977122009-01-05 21:25:51 -05003320static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003321 struct btrfs_device *device,
3322 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003323{
3324 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003325 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003326
3327 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003328 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003329 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003330 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003331 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003332 return 0;
3333 }
3334
3335 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003336 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003337 * higher layers. Otherwise, the async bio makes it appear we have
3338 * made progress against dirty pages when we've really just put it
3339 * on a queue for later
3340 */
Chris Mason0986fe92008-08-15 15:34:15 -04003341 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003342 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003343 bio->bi_next = NULL;
3344 bio->bi_rw |= rw;
3345
3346 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003347 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003348 pending_bios = &device->pending_sync_bios;
3349 else
3350 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003351
Chris Masonffbd5172009-04-20 15:50:09 -04003352 if (pending_bios->tail)
3353 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003354
Chris Masonffbd5172009-04-20 15:50:09 -04003355 pending_bios->tail = bio;
3356 if (!pending_bios->head)
3357 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003358 if (device->running_pending)
3359 should_queue = 0;
3360
3361 spin_unlock(&device->io_lock);
3362
3363 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003364 btrfs_queue_worker(&root->fs_info->submit_workers,
3365 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003366 return 0;
3367}
3368
Chris Masonf1885912008-04-09 16:28:12 -04003369int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003370 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003371{
3372 struct btrfs_mapping_tree *map_tree;
3373 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003374 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003375 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003376 u64 length = 0;
3377 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003378 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003379 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003380 int dev_nr = 0;
3381 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003382
Chris Masonf2d8d742008-04-21 10:03:05 -04003383 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003384 map_tree = &root->fs_info->mapping_tree;
3385 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003386
Chris Masonf1885912008-04-09 16:28:12 -04003387 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3388 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003389 BUG_ON(ret);
3390
3391 total_devs = multi->num_stripes;
3392 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003393 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3394 "len %llu\n", (unsigned long long)logical,
3395 (unsigned long long)length,
3396 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003397 BUG();
3398 }
3399 multi->end_io = first_bio->bi_end_io;
3400 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003401 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003402 atomic_set(&multi->stripes_pending, multi->num_stripes);
3403
Chris Masond3977122009-01-05 21:25:51 -05003404 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003405 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003406 if (dev_nr < total_devs - 1) {
3407 bio = bio_clone(first_bio, GFP_NOFS);
3408 BUG_ON(!bio);
3409 } else {
3410 bio = first_bio;
3411 }
3412 bio->bi_private = multi;
3413 bio->bi_end_io = end_bio_multi_stripe;
3414 }
Chris Masoncea9e442008-04-09 16:28:12 -04003415 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3416 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003417 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003418 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003419 if (async_submit)
3420 schedule_bio(root, dev, rw, bio);
3421 else
3422 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003423 } else {
3424 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3425 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003426 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003427 }
Chris Mason8790d502008-04-03 16:29:03 -04003428 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003429 }
Chris Masoncea9e442008-04-09 16:28:12 -04003430 if (total_devs == 1)
3431 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003432 return 0;
3433}
3434
Chris Masona4437552008-04-18 10:29:38 -04003435struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003436 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003437{
Yan Zheng2b820322008-11-17 21:11:30 -05003438 struct btrfs_device *device;
3439 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003440
Yan Zheng2b820322008-11-17 21:11:30 -05003441 cur_devices = root->fs_info->fs_devices;
3442 while (cur_devices) {
3443 if (!fsid ||
3444 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3445 device = __find_device(&cur_devices->devices,
3446 devid, uuid);
3447 if (device)
3448 return device;
3449 }
3450 cur_devices = cur_devices->seed;
3451 }
3452 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003453}
3454
Chris Masondfe25022008-05-13 13:46:40 -04003455static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3456 u64 devid, u8 *dev_uuid)
3457{
3458 struct btrfs_device *device;
3459 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3460
3461 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003462 if (!device)
3463 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003464 list_add(&device->dev_list,
3465 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003466 device->dev_root = root->fs_info->dev_root;
3467 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003468 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003469 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003470 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003471 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003472 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003473 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003474 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003475 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3476 return device;
3477}
3478
Chris Mason0b86a832008-03-24 15:01:56 -04003479static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3480 struct extent_buffer *leaf,
3481 struct btrfs_chunk *chunk)
3482{
3483 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3484 struct map_lookup *map;
3485 struct extent_map *em;
3486 u64 logical;
3487 u64 length;
3488 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003489 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003490 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003491 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003492 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003493
Chris Masone17cade2008-04-15 15:41:47 -04003494 logical = key->offset;
3495 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003496
Chris Mason890871b2009-09-02 16:24:52 -04003497 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003498 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003499 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003500
3501 /* already mapped? */
3502 if (em && em->start <= logical && em->start + em->len > logical) {
3503 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003504 return 0;
3505 } else if (em) {
3506 free_extent_map(em);
3507 }
Chris Mason0b86a832008-03-24 15:01:56 -04003508
Chris Mason0b86a832008-03-24 15:01:56 -04003509 em = alloc_extent_map(GFP_NOFS);
3510 if (!em)
3511 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003512 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3513 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003514 if (!map) {
3515 free_extent_map(em);
3516 return -ENOMEM;
3517 }
3518
3519 em->bdev = (struct block_device *)map;
3520 em->start = logical;
3521 em->len = length;
3522 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003523 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003524
Chris Mason593060d2008-03-25 16:50:33 -04003525 map->num_stripes = num_stripes;
3526 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3527 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3528 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3529 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3530 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003531 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003532 for (i = 0; i < num_stripes; i++) {
3533 map->stripes[i].physical =
3534 btrfs_stripe_offset_nr(leaf, chunk, i);
3535 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003536 read_extent_buffer(leaf, uuid, (unsigned long)
3537 btrfs_stripe_dev_uuid_nr(chunk, i),
3538 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003539 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3540 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003541 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003542 kfree(map);
3543 free_extent_map(em);
3544 return -EIO;
3545 }
Chris Masondfe25022008-05-13 13:46:40 -04003546 if (!map->stripes[i].dev) {
3547 map->stripes[i].dev =
3548 add_missing_dev(root, devid, uuid);
3549 if (!map->stripes[i].dev) {
3550 kfree(map);
3551 free_extent_map(em);
3552 return -EIO;
3553 }
3554 }
3555 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003556 }
3557
Chris Mason890871b2009-09-02 16:24:52 -04003558 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003559 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003560 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003561 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003562 free_extent_map(em);
3563
3564 return 0;
3565}
3566
3567static int fill_device_from_item(struct extent_buffer *leaf,
3568 struct btrfs_dev_item *dev_item,
3569 struct btrfs_device *device)
3570{
3571 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003572
3573 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003574 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3575 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003576 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3577 device->type = btrfs_device_type(leaf, dev_item);
3578 device->io_align = btrfs_device_io_align(leaf, dev_item);
3579 device->io_width = btrfs_device_io_width(leaf, dev_item);
3580 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003581
3582 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003583 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003584
Chris Mason0b86a832008-03-24 15:01:56 -04003585 return 0;
3586}
3587
Yan Zheng2b820322008-11-17 21:11:30 -05003588static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3589{
3590 struct btrfs_fs_devices *fs_devices;
3591 int ret;
3592
3593 mutex_lock(&uuid_mutex);
3594
3595 fs_devices = root->fs_info->fs_devices->seed;
3596 while (fs_devices) {
3597 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3598 ret = 0;
3599 goto out;
3600 }
3601 fs_devices = fs_devices->seed;
3602 }
3603
3604 fs_devices = find_fsid(fsid);
3605 if (!fs_devices) {
3606 ret = -ENOENT;
3607 goto out;
3608 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003609
3610 fs_devices = clone_fs_devices(fs_devices);
3611 if (IS_ERR(fs_devices)) {
3612 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003613 goto out;
3614 }
3615
Christoph Hellwig97288f22008-12-02 06:36:09 -05003616 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003617 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003618 if (ret)
3619 goto out;
3620
3621 if (!fs_devices->seeding) {
3622 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003623 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003624 ret = -EINVAL;
3625 goto out;
3626 }
3627
3628 fs_devices->seed = root->fs_info->fs_devices->seed;
3629 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003630out:
3631 mutex_unlock(&uuid_mutex);
3632 return ret;
3633}
3634
Chris Mason0d81ba52008-03-24 15:02:07 -04003635static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003636 struct extent_buffer *leaf,
3637 struct btrfs_dev_item *dev_item)
3638{
3639 struct btrfs_device *device;
3640 u64 devid;
3641 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003642 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003643 u8 dev_uuid[BTRFS_UUID_SIZE];
3644
Chris Mason0b86a832008-03-24 15:01:56 -04003645 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003646 read_extent_buffer(leaf, dev_uuid,
3647 (unsigned long)btrfs_device_uuid(dev_item),
3648 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003649 read_extent_buffer(leaf, fs_uuid,
3650 (unsigned long)btrfs_device_fsid(dev_item),
3651 BTRFS_UUID_SIZE);
3652
3653 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3654 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003655 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003656 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003657 }
3658
3659 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3660 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003661 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003662 return -EIO;
3663
3664 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003665 printk(KERN_WARNING "warning devid %llu missing\n",
3666 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003667 device = add_missing_dev(root, devid, dev_uuid);
3668 if (!device)
3669 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003670 } else if (!device->missing) {
3671 /*
3672 * this happens when a device that was properly setup
3673 * in the device info lists suddenly goes bad.
3674 * device->bdev is NULL, and so we have to set
3675 * device->missing to one here
3676 */
3677 root->fs_info->fs_devices->missing_devices++;
3678 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003679 }
3680 }
3681
3682 if (device->fs_devices != root->fs_info->fs_devices) {
3683 BUG_ON(device->writeable);
3684 if (device->generation !=
3685 btrfs_device_generation(leaf, dev_item))
3686 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003687 }
Chris Mason0b86a832008-03-24 15:01:56 -04003688
3689 fill_device_from_item(leaf, dev_item, device);
3690 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003691 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003692 if (device->writeable)
3693 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003694 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003695 return ret;
3696}
3697
Chris Mason0d81ba52008-03-24 15:02:07 -04003698int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3699{
3700 struct btrfs_dev_item *dev_item;
3701
3702 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3703 dev_item);
3704 return read_one_dev(root, buf, dev_item);
3705}
3706
Yan Zhenge4404d62008-12-12 10:03:26 -05003707int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003708{
3709 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003710 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003711 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003712 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003713 u8 *ptr;
3714 unsigned long sb_ptr;
3715 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003716 u32 num_stripes;
3717 u32 array_size;
3718 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003719 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003720 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003721
Yan Zhenge4404d62008-12-12 10:03:26 -05003722 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003723 BTRFS_SUPER_INFO_SIZE);
3724 if (!sb)
3725 return -ENOMEM;
3726 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003727 btrfs_set_buffer_lockdep_class(sb, 0);
3728
Chris Masona061fc82008-05-07 11:43:44 -04003729 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003730 array_size = btrfs_super_sys_array_size(super_copy);
3731
Chris Mason0b86a832008-03-24 15:01:56 -04003732 ptr = super_copy->sys_chunk_array;
3733 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3734 cur = 0;
3735
3736 while (cur < array_size) {
3737 disk_key = (struct btrfs_disk_key *)ptr;
3738 btrfs_disk_key_to_cpu(&key, disk_key);
3739
Chris Masona061fc82008-05-07 11:43:44 -04003740 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003741 sb_ptr += len;
3742 cur += len;
3743
Chris Mason0d81ba52008-03-24 15:02:07 -04003744 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003745 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003746 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003747 if (ret)
3748 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003749 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3750 len = btrfs_chunk_item_size(num_stripes);
3751 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003752 ret = -EIO;
3753 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003754 }
3755 ptr += len;
3756 sb_ptr += len;
3757 cur += len;
3758 }
Chris Masona061fc82008-05-07 11:43:44 -04003759 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003760 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003761}
3762
3763int btrfs_read_chunk_tree(struct btrfs_root *root)
3764{
3765 struct btrfs_path *path;
3766 struct extent_buffer *leaf;
3767 struct btrfs_key key;
3768 struct btrfs_key found_key;
3769 int ret;
3770 int slot;
3771
3772 root = root->fs_info->chunk_root;
3773
3774 path = btrfs_alloc_path();
3775 if (!path)
3776 return -ENOMEM;
3777
3778 /* first we search for all of the device items, and then we
3779 * read in all of the chunk items. This way we can create chunk
3780 * mappings that reference all of the devices that are afound
3781 */
3782 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3783 key.offset = 0;
3784 key.type = 0;
3785again:
3786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003787 if (ret < 0)
3788 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003789 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003790 leaf = path->nodes[0];
3791 slot = path->slots[0];
3792 if (slot >= btrfs_header_nritems(leaf)) {
3793 ret = btrfs_next_leaf(root, path);
3794 if (ret == 0)
3795 continue;
3796 if (ret < 0)
3797 goto error;
3798 break;
3799 }
3800 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3801 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3802 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3803 break;
3804 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3805 struct btrfs_dev_item *dev_item;
3806 dev_item = btrfs_item_ptr(leaf, slot,
3807 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003808 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003809 if (ret)
3810 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003811 }
3812 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3813 struct btrfs_chunk *chunk;
3814 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3815 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003816 if (ret)
3817 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003818 }
3819 path->slots[0]++;
3820 }
3821 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3822 key.objectid = 0;
3823 btrfs_release_path(root, path);
3824 goto again;
3825 }
Chris Mason0b86a832008-03-24 15:01:56 -04003826 ret = 0;
3827error:
Yan Zheng2b820322008-11-17 21:11:30 -05003828 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003829 return ret;
3830}