blob: e40cdd5b46699a0325b0b1865880b2aba98e7288 [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
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500484 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100486 blkdev_put(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400487 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400488 }
Yan Zheng2b820322008-11-17 21:11:30 -0500489 if (device->writeable) {
490 list_del_init(&device->dev_alloc_list);
491 fs_devices->rw_devices--;
492 }
493
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500495 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400496 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400497 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500498 WARN_ON(fs_devices->open_devices);
499 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500500 fs_devices->opened = 0;
501 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500502
Chris Mason8a4b83c2008-03-24 15:02:07 -0400503 return 0;
504}
505
Yan Zheng2b820322008-11-17 21:11:30 -0500506int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
507{
Yan Zhenge4404d62008-12-12 10:03:26 -0500508 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500509 int ret;
510
511 mutex_lock(&uuid_mutex);
512 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500513 if (!fs_devices->opened) {
514 seed_devices = fs_devices->seed;
515 fs_devices->seed = NULL;
516 }
Yan Zheng2b820322008-11-17 21:11:30 -0500517 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500518
519 while (seed_devices) {
520 fs_devices = seed_devices;
521 seed_devices = fs_devices->seed;
522 __btrfs_close_devices(fs_devices);
523 free_fs_devices(fs_devices);
524 }
Yan Zheng2b820322008-11-17 21:11:30 -0500525 return ret;
526}
527
Yan Zhenge4404d62008-12-12 10:03:26 -0500528static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
529 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400530{
531 struct block_device *bdev;
532 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400534 struct block_device *latest_bdev = NULL;
535 struct buffer_head *bh;
536 struct btrfs_super_block *disk_super;
537 u64 latest_devid = 0;
538 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400539 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500540 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400541 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400542
Tejun Heod4d77622010-11-13 11:55:18 +0100543 flags |= FMODE_EXCL;
544
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500545 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400546 if (device->bdev)
547 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400548 if (!device->name)
549 continue;
550
Tejun Heod4d77622010-11-13 11:55:18 +0100551 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400552 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500553 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400554 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400555 }
Chris Masona061fc82008-05-07 11:43:44 -0400556 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400557
Yan Zhenga512bbf2008-12-08 16:46:26 -0500558 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000559 if (!bh) {
560 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400561 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000562 }
Chris Masona0af4692008-05-13 16:03:06 -0400563
564 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000565 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400566 if (devid != device->devid)
567 goto error_brelse;
568
Yan Zheng2b820322008-11-17 21:11:30 -0500569 if (memcmp(device->uuid, disk_super->dev_item.uuid,
570 BTRFS_UUID_SIZE))
571 goto error_brelse;
572
573 device->generation = btrfs_super_generation(disk_super);
574 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400575 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500576 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400577 latest_bdev = bdev;
578 }
579
Yan Zheng2b820322008-11-17 21:11:30 -0500580 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
581 device->writeable = 0;
582 } else {
583 device->writeable = !bdev_read_only(bdev);
584 seeding = 0;
585 }
586
Chris Mason8a4b83c2008-03-24 15:02:07 -0400587 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400588 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500589 device->mode = flags;
590
Chris Masonc2898112009-06-10 09:51:32 -0400591 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
592 fs_devices->rotating = 1;
593
Chris Masona0af4692008-05-13 16:03:06 -0400594 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500595 if (device->writeable) {
596 fs_devices->rw_devices++;
597 list_add(&device->dev_alloc_list,
598 &fs_devices->alloc_list);
599 }
Chris Masona0af4692008-05-13 16:03:06 -0400600 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400601
Chris Masona0af4692008-05-13 16:03:06 -0400602error_brelse:
603 brelse(bh);
604error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100605 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400606error:
607 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400608 }
Chris Masona0af4692008-05-13 16:03:06 -0400609 if (fs_devices->open_devices == 0) {
610 ret = -EIO;
611 goto out;
612 }
Yan Zheng2b820322008-11-17 21:11:30 -0500613 fs_devices->seeding = seeding;
614 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400615 fs_devices->latest_bdev = latest_bdev;
616 fs_devices->latest_devid = latest_devid;
617 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500618 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400619out:
Yan Zheng2b820322008-11-17 21:11:30 -0500620 return ret;
621}
622
623int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500624 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500625{
626 int ret;
627
628 mutex_lock(&uuid_mutex);
629 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500630 fs_devices->opened++;
631 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500632 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500633 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500634 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400635 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400636 return ret;
637}
638
Christoph Hellwig97288f22008-12-02 06:36:09 -0500639int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400640 struct btrfs_fs_devices **fs_devices_ret)
641{
642 struct btrfs_super_block *disk_super;
643 struct block_device *bdev;
644 struct buffer_head *bh;
645 int ret;
646 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400647 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400648
649 mutex_lock(&uuid_mutex);
650
Tejun Heod4d77622010-11-13 11:55:18 +0100651 flags |= FMODE_EXCL;
652 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400653
654 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400655 ret = PTR_ERR(bdev);
656 goto error;
657 }
658
659 ret = set_blocksize(bdev, 4096);
660 if (ret)
661 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500662 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400663 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000664 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400665 goto error_close;
666 }
667 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000668 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400669 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400670 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500671 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400672 else {
673 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500674 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400675 *(unsigned long long *)disk_super->fsid,
676 *(unsigned long long *)(disk_super->fsid + 8));
677 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500678 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500679 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400680 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
681
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682 brelse(bh);
683error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100684 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685error:
686 mutex_unlock(&uuid_mutex);
687 return ret;
688}
Chris Mason0b86a832008-03-24 15:01:56 -0400689
Miao Xie6d07bce2011-01-05 10:07:31 +0000690/* helper to account the used device space in the range */
691int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
692 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400693{
694 struct btrfs_key key;
695 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000696 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500697 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000698 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400699 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000700 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400701 struct extent_buffer *l;
702
Miao Xie6d07bce2011-01-05 10:07:31 +0000703 *length = 0;
704
705 if (start >= device->total_bytes)
706 return 0;
707
Yan Zheng2b820322008-11-17 21:11:30 -0500708 path = btrfs_alloc_path();
709 if (!path)
710 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400711 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400712
Chris Mason0b86a832008-03-24 15:01:56 -0400713 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000714 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400715 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000716
717 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400718 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000719 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400720 if (ret > 0) {
721 ret = btrfs_previous_item(root, path, key.objectid, key.type);
722 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000723 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400724 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000725
Chris Mason0b86a832008-03-24 15:01:56 -0400726 while (1) {
727 l = path->nodes[0];
728 slot = path->slots[0];
729 if (slot >= btrfs_header_nritems(l)) {
730 ret = btrfs_next_leaf(root, path);
731 if (ret == 0)
732 continue;
733 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000734 goto out;
735
736 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400737 }
738 btrfs_item_key_to_cpu(l, &key, slot);
739
740 if (key.objectid < device->devid)
741 goto next;
742
743 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000744 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400745
Chris Masond3977122009-01-05 21:25:51 -0500746 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400747 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400748
Chris Mason0b86a832008-03-24 15:01:56 -0400749 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000750 extent_end = key.offset + btrfs_dev_extent_length(l,
751 dev_extent);
752 if (key.offset <= start && extent_end > end) {
753 *length = end - start + 1;
754 break;
755 } else if (key.offset <= start && extent_end > start)
756 *length += extent_end - start;
757 else if (key.offset > start && extent_end <= end)
758 *length += extent_end - key.offset;
759 else if (key.offset > start && key.offset <= end) {
760 *length += end - key.offset + 1;
761 break;
762 } else if (key.offset > end)
763 break;
764
765next:
766 path->slots[0]++;
767 }
768 ret = 0;
769out:
770 btrfs_free_path(path);
771 return ret;
772}
773
Chris Mason0b86a832008-03-24 15:01:56 -0400774/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000775 * find_free_dev_extent - find free space in the specified device
776 * @trans: transaction handler
777 * @device: the device which we search the free space in
778 * @num_bytes: the size of the free space that we need
779 * @start: store the start of the free space.
780 * @len: the size of the free space. that we find, or the size of the max
781 * free space if we don't find suitable free space
782 *
Chris Mason0b86a832008-03-24 15:01:56 -0400783 * this uses a pretty simple search, the expectation is that it is
784 * called very infrequently and that a given device has a small number
785 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000786 *
787 * @start is used to store the start of the free space if we find. But if we
788 * don't find suitable free space, it will be used to store the start position
789 * of the max free space.
790 *
791 * @len is used to store the size of the free space that we find.
792 * But if we don't find suitable free space, it is used to store the size of
793 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400794 */
795int find_free_dev_extent(struct btrfs_trans_handle *trans,
796 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000797 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400798{
799 struct btrfs_key key;
800 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000801 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400802 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000803 u64 hole_size;
804 u64 max_hole_start;
805 u64 max_hole_size;
806 u64 extent_end;
807 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400808 u64 search_end = device->total_bytes;
809 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000810 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400811 struct extent_buffer *l;
812
Chris Mason0b86a832008-03-24 15:01:56 -0400813 /* FIXME use last free of some kind */
814
815 /* we don't want to overwrite the superblock on the drive,
816 * so we make sure to start at an offset of at least 1MB
817 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000818 search_start = 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400819
Miao Xie7bfc8372011-01-05 10:07:26 +0000820 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason0b86a832008-03-24 15:01:56 -0400821 search_start = max(root->fs_info->alloc_start, search_start);
822
Miao Xie7bfc8372011-01-05 10:07:26 +0000823 max_hole_start = search_start;
824 max_hole_size = 0;
825
826 if (search_start >= search_end) {
827 ret = -ENOSPC;
828 goto error;
829 }
830
831 path = btrfs_alloc_path();
832 if (!path) {
833 ret = -ENOMEM;
834 goto error;
835 }
836 path->reada = 2;
837
Chris Mason0b86a832008-03-24 15:01:56 -0400838 key.objectid = device->devid;
839 key.offset = search_start;
840 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000841
Chris Mason0b86a832008-03-24 15:01:56 -0400842 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
843 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000844 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400845 if (ret > 0) {
846 ret = btrfs_previous_item(root, path, key.objectid, key.type);
847 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400849 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000850
Chris Mason0b86a832008-03-24 15:01:56 -0400851 while (1) {
852 l = path->nodes[0];
853 slot = path->slots[0];
854 if (slot >= btrfs_header_nritems(l)) {
855 ret = btrfs_next_leaf(root, path);
856 if (ret == 0)
857 continue;
858 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000859 goto out;
860
861 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400862 }
863 btrfs_item_key_to_cpu(l, &key, slot);
864
865 if (key.objectid < device->devid)
866 goto next;
867
868 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000869 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400870
Chris Mason0b86a832008-03-24 15:01:56 -0400871 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
872 goto next;
873
Miao Xie7bfc8372011-01-05 10:07:26 +0000874 if (key.offset > search_start) {
875 hole_size = key.offset - search_start;
876
877 if (hole_size > max_hole_size) {
878 max_hole_start = search_start;
879 max_hole_size = hole_size;
880 }
881
882 /*
883 * If this free space is greater than which we need,
884 * it must be the max free space that we have found
885 * until now, so max_hole_start must point to the start
886 * of this free space and the length of this free space
887 * is stored in max_hole_size. Thus, we return
888 * max_hole_start and max_hole_size and go back to the
889 * caller.
890 */
891 if (hole_size >= num_bytes) {
892 ret = 0;
893 goto out;
894 }
895 }
896
Chris Mason0b86a832008-03-24 15:01:56 -0400897 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000898 extent_end = key.offset + btrfs_dev_extent_length(l,
899 dev_extent);
900 if (extent_end > search_start)
901 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400902next:
903 path->slots[0]++;
904 cond_resched();
905 }
Chris Mason0b86a832008-03-24 15:01:56 -0400906
Miao Xie7bfc8372011-01-05 10:07:26 +0000907 hole_size = search_end- search_start;
908 if (hole_size > max_hole_size) {
909 max_hole_start = search_start;
910 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400911 }
Chris Mason0b86a832008-03-24 15:01:56 -0400912
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 /* See above. */
914 if (hole_size < num_bytes)
915 ret = -ENOSPC;
916 else
917 ret = 0;
918
919out:
Yan Zheng2b820322008-11-17 21:11:30 -0500920 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000921error:
922 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000923 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000924 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400925 return ret;
926}
927
Christoph Hellwigb2950862008-12-02 09:54:17 -0500928static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400929 struct btrfs_device *device,
930 u64 start)
931{
932 int ret;
933 struct btrfs_path *path;
934 struct btrfs_root *root = device->dev_root;
935 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400936 struct btrfs_key found_key;
937 struct extent_buffer *leaf = NULL;
938 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400939
940 path = btrfs_alloc_path();
941 if (!path)
942 return -ENOMEM;
943
944 key.objectid = device->devid;
945 key.offset = start;
946 key.type = BTRFS_DEV_EXTENT_KEY;
947
948 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400949 if (ret > 0) {
950 ret = btrfs_previous_item(root, path, key.objectid,
951 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000952 if (ret)
953 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400954 leaf = path->nodes[0];
955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
956 extent = btrfs_item_ptr(leaf, path->slots[0],
957 struct btrfs_dev_extent);
958 BUG_ON(found_key.offset > start || found_key.offset +
959 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400960 } else if (ret == 0) {
961 leaf = path->nodes[0];
962 extent = btrfs_item_ptr(leaf, path->slots[0],
963 struct btrfs_dev_extent);
964 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400965 BUG_ON(ret);
966
Chris Masondfe25022008-05-13 13:46:40 -0400967 if (device->bytes_used > 0)
968 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400969 ret = btrfs_del_item(trans, root, path);
970 BUG_ON(ret);
971
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000972out:
Chris Mason8f18cf12008-04-25 16:53:30 -0400973 btrfs_free_path(path);
974 return ret;
975}
976
Yan Zheng2b820322008-11-17 21:11:30 -0500977int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400978 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400979 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500980 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400981{
982 int ret;
983 struct btrfs_path *path;
984 struct btrfs_root *root = device->dev_root;
985 struct btrfs_dev_extent *extent;
986 struct extent_buffer *leaf;
987 struct btrfs_key key;
988
Chris Masondfe25022008-05-13 13:46:40 -0400989 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400990 path = btrfs_alloc_path();
991 if (!path)
992 return -ENOMEM;
993
Chris Mason0b86a832008-03-24 15:01:56 -0400994 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500995 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400996 key.type = BTRFS_DEV_EXTENT_KEY;
997 ret = btrfs_insert_empty_item(trans, root, path, &key,
998 sizeof(*extent));
999 BUG_ON(ret);
1000
1001 leaf = path->nodes[0];
1002 extent = btrfs_item_ptr(leaf, path->slots[0],
1003 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001004 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1005 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1006 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1007
1008 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1009 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1010 BTRFS_UUID_SIZE);
1011
Chris Mason0b86a832008-03-24 15:01:56 -04001012 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1013 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001014 btrfs_free_path(path);
1015 return ret;
1016}
1017
Chris Masona1b32a52008-09-05 16:09:51 -04001018static noinline int find_next_chunk(struct btrfs_root *root,
1019 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001020{
1021 struct btrfs_path *path;
1022 int ret;
1023 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001024 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001025 struct btrfs_key found_key;
1026
1027 path = btrfs_alloc_path();
1028 BUG_ON(!path);
1029
Chris Masone17cade2008-04-15 15:41:47 -04001030 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001031 key.offset = (u64)-1;
1032 key.type = BTRFS_CHUNK_ITEM_KEY;
1033
1034 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1035 if (ret < 0)
1036 goto error;
1037
1038 BUG_ON(ret == 0);
1039
1040 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1041 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001042 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001043 } else {
1044 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1045 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001046 if (found_key.objectid != objectid)
1047 *offset = 0;
1048 else {
1049 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1050 struct btrfs_chunk);
1051 *offset = found_key.offset +
1052 btrfs_chunk_length(path->nodes[0], chunk);
1053 }
Chris Mason0b86a832008-03-24 15:01:56 -04001054 }
1055 ret = 0;
1056error:
1057 btrfs_free_path(path);
1058 return ret;
1059}
1060
Yan Zheng2b820322008-11-17 21:11:30 -05001061static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001062{
1063 int ret;
1064 struct btrfs_key key;
1065 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001066 struct btrfs_path *path;
1067
1068 root = root->fs_info->chunk_root;
1069
1070 path = btrfs_alloc_path();
1071 if (!path)
1072 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001073
1074 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1075 key.type = BTRFS_DEV_ITEM_KEY;
1076 key.offset = (u64)-1;
1077
1078 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1079 if (ret < 0)
1080 goto error;
1081
1082 BUG_ON(ret == 0);
1083
1084 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1085 BTRFS_DEV_ITEM_KEY);
1086 if (ret) {
1087 *objectid = 1;
1088 } else {
1089 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1090 path->slots[0]);
1091 *objectid = found_key.offset + 1;
1092 }
1093 ret = 0;
1094error:
Yan Zheng2b820322008-11-17 21:11:30 -05001095 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001096 return ret;
1097}
1098
1099/*
1100 * the device information is stored in the chunk root
1101 * the btrfs_device struct should be fully filled in
1102 */
1103int btrfs_add_device(struct btrfs_trans_handle *trans,
1104 struct btrfs_root *root,
1105 struct btrfs_device *device)
1106{
1107 int ret;
1108 struct btrfs_path *path;
1109 struct btrfs_dev_item *dev_item;
1110 struct extent_buffer *leaf;
1111 struct btrfs_key key;
1112 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001113
1114 root = root->fs_info->chunk_root;
1115
1116 path = btrfs_alloc_path();
1117 if (!path)
1118 return -ENOMEM;
1119
Chris Mason0b86a832008-03-24 15:01:56 -04001120 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1121 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001122 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001123
1124 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001125 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001126 if (ret)
1127 goto out;
1128
1129 leaf = path->nodes[0];
1130 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1131
1132 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001133 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001134 btrfs_set_device_type(leaf, dev_item, device->type);
1135 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1136 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1137 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001138 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1139 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001140 btrfs_set_device_group(leaf, dev_item, 0);
1141 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1142 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001143 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001144
Chris Mason0b86a832008-03-24 15:01:56 -04001145 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001146 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001147 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1148 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001149 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001150
Yan Zheng2b820322008-11-17 21:11:30 -05001151 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001152out:
1153 btrfs_free_path(path);
1154 return ret;
1155}
Chris Mason8f18cf12008-04-25 16:53:30 -04001156
Chris Masona061fc82008-05-07 11:43:44 -04001157static int btrfs_rm_dev_item(struct btrfs_root *root,
1158 struct btrfs_device *device)
1159{
1160 int ret;
1161 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001162 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001163 struct btrfs_trans_handle *trans;
1164
1165 root = root->fs_info->chunk_root;
1166
1167 path = btrfs_alloc_path();
1168 if (!path)
1169 return -ENOMEM;
1170
Yan, Zhenga22285a2010-05-16 10:48:46 -04001171 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001172 if (IS_ERR(trans)) {
1173 btrfs_free_path(path);
1174 return PTR_ERR(trans);
1175 }
Chris Masona061fc82008-05-07 11:43:44 -04001176 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1177 key.type = BTRFS_DEV_ITEM_KEY;
1178 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001179 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001180
1181 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1182 if (ret < 0)
1183 goto out;
1184
1185 if (ret > 0) {
1186 ret = -ENOENT;
1187 goto out;
1188 }
1189
1190 ret = btrfs_del_item(trans, root, path);
1191 if (ret)
1192 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001193out:
1194 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001195 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001196 btrfs_commit_transaction(trans, root);
1197 return ret;
1198}
1199
1200int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1201{
1202 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001203 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001204 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001205 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001206 struct btrfs_super_block *disk_super;
1207 u64 all_avail;
1208 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001209 u64 num_devices;
1210 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001211 int ret = 0;
1212
Chris Masona061fc82008-05-07 11:43:44 -04001213 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001214 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001215
1216 all_avail = root->fs_info->avail_data_alloc_bits |
1217 root->fs_info->avail_system_alloc_bits |
1218 root->fs_info->avail_metadata_alloc_bits;
1219
1220 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001221 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001222 printk(KERN_ERR "btrfs: unable to go below four devices "
1223 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001224 ret = -EINVAL;
1225 goto out;
1226 }
1227
1228 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001229 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001230 printk(KERN_ERR "btrfs: unable to go below two "
1231 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001232 ret = -EINVAL;
1233 goto out;
1234 }
1235
Chris Masondfe25022008-05-13 13:46:40 -04001236 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001237 struct list_head *devices;
1238 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001239
Chris Masondfe25022008-05-13 13:46:40 -04001240 device = NULL;
1241 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001242 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001243 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001244 if (tmp->in_fs_metadata && !tmp->bdev) {
1245 device = tmp;
1246 break;
1247 }
1248 }
Chris Masone5e9a522009-06-10 15:17:02 -04001249 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001250 bdev = NULL;
1251 bh = NULL;
1252 disk_super = NULL;
1253 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001254 printk(KERN_ERR "btrfs: no missing devices found to "
1255 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001256 goto out;
1257 }
Chris Masondfe25022008-05-13 13:46:40 -04001258 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001259 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1260 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001261 if (IS_ERR(bdev)) {
1262 ret = PTR_ERR(bdev);
1263 goto out;
1264 }
1265
Yan Zheng2b820322008-11-17 21:11:30 -05001266 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001267 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001268 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001269 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001270 goto error_close;
1271 }
1272 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001273 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001274 dev_uuid = disk_super->dev_item.uuid;
1275 device = btrfs_find_device(root, devid, dev_uuid,
1276 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001277 if (!device) {
1278 ret = -ENOENT;
1279 goto error_brelse;
1280 }
Chris Masondfe25022008-05-13 13:46:40 -04001281 }
Yan Zheng2b820322008-11-17 21:11:30 -05001282
1283 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001284 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1285 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001286 ret = -EINVAL;
1287 goto error_brelse;
1288 }
1289
1290 if (device->writeable) {
1291 list_del_init(&device->dev_alloc_list);
1292 root->fs_info->fs_devices->rw_devices--;
1293 }
Chris Masona061fc82008-05-07 11:43:44 -04001294
1295 ret = btrfs_shrink_device(device, 0);
1296 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001297 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001298
Chris Masona061fc82008-05-07 11:43:44 -04001299 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1300 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001301 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001302
Yan Zheng2b820322008-11-17 21:11:30 -05001303 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001304
1305 /*
1306 * the device list mutex makes sure that we don't change
1307 * the device list while someone else is writing out all
1308 * the device supers.
1309 */
1310 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001311 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001312 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1313
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001315
Chris Masoncd02dca2010-12-13 14:56:23 -05001316 if (device->missing)
1317 root->fs_info->fs_devices->missing_devices--;
1318
Yan Zheng2b820322008-11-17 21:11:30 -05001319 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1320 struct btrfs_device, dev_list);
1321 if (device->bdev == root->fs_info->sb->s_bdev)
1322 root->fs_info->sb->s_bdev = next_device->bdev;
1323 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1324 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1325
Yan Zhenge4404d62008-12-12 10:03:26 -05001326 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001327 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001328 device->bdev = NULL;
1329 device->fs_devices->open_devices--;
1330 }
1331
Yan Zheng2b820322008-11-17 21:11:30 -05001332 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1333 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1334
Yan Zhenge4404d62008-12-12 10:03:26 -05001335 if (device->fs_devices->open_devices == 0) {
1336 struct btrfs_fs_devices *fs_devices;
1337 fs_devices = root->fs_info->fs_devices;
1338 while (fs_devices) {
1339 if (fs_devices->seed == device->fs_devices)
1340 break;
1341 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001342 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001343 fs_devices->seed = device->fs_devices->seed;
1344 device->fs_devices->seed = NULL;
1345 __btrfs_close_devices(device->fs_devices);
1346 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001347 }
1348
1349 /*
1350 * at this point, the device is zero sized. We want to
1351 * remove it from the devices list and zero out the old super
1352 */
1353 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001354 /* make sure this device isn't detected as part of
1355 * the FS anymore
1356 */
1357 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1358 set_buffer_dirty(bh);
1359 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001360 }
Chris Masona061fc82008-05-07 11:43:44 -04001361
Chris Masona061fc82008-05-07 11:43:44 -04001362 kfree(device->name);
1363 kfree(device);
1364 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001365
1366error_brelse:
1367 brelse(bh);
1368error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001369 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001370 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001371out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001372 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001373 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001374 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001375error_undo:
1376 if (device->writeable) {
1377 list_add(&device->dev_alloc_list,
1378 &root->fs_info->fs_devices->alloc_list);
1379 root->fs_info->fs_devices->rw_devices++;
1380 }
1381 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001382}
1383
Yan Zheng2b820322008-11-17 21:11:30 -05001384/*
1385 * does all the dirty work required for changing file system's UUID.
1386 */
1387static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *root)
1389{
1390 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1391 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001392 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001393 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1394 struct btrfs_device *device;
1395 u64 super_flags;
1396
1397 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001398 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001399 return -EINVAL;
1400
Yan Zhenge4404d62008-12-12 10:03:26 -05001401 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1402 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001403 return -ENOMEM;
1404
Yan Zhenge4404d62008-12-12 10:03:26 -05001405 old_devices = clone_fs_devices(fs_devices);
1406 if (IS_ERR(old_devices)) {
1407 kfree(seed_devices);
1408 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001409 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001410
Yan Zheng2b820322008-11-17 21:11:30 -05001411 list_add(&old_devices->list, &fs_uuids);
1412
Yan Zhenge4404d62008-12-12 10:03:26 -05001413 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1414 seed_devices->opened = 1;
1415 INIT_LIST_HEAD(&seed_devices->devices);
1416 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001417 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001418 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1419 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1420 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1421 device->fs_devices = seed_devices;
1422 }
1423
Yan Zheng2b820322008-11-17 21:11:30 -05001424 fs_devices->seeding = 0;
1425 fs_devices->num_devices = 0;
1426 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001427 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001428
1429 generate_random_uuid(fs_devices->fsid);
1430 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1431 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1432 super_flags = btrfs_super_flags(disk_super) &
1433 ~BTRFS_SUPER_FLAG_SEEDING;
1434 btrfs_set_super_flags(disk_super, super_flags);
1435
1436 return 0;
1437}
1438
1439/*
1440 * strore the expected generation for seed devices in device items.
1441 */
1442static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1443 struct btrfs_root *root)
1444{
1445 struct btrfs_path *path;
1446 struct extent_buffer *leaf;
1447 struct btrfs_dev_item *dev_item;
1448 struct btrfs_device *device;
1449 struct btrfs_key key;
1450 u8 fs_uuid[BTRFS_UUID_SIZE];
1451 u8 dev_uuid[BTRFS_UUID_SIZE];
1452 u64 devid;
1453 int ret;
1454
1455 path = btrfs_alloc_path();
1456 if (!path)
1457 return -ENOMEM;
1458
1459 root = root->fs_info->chunk_root;
1460 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1461 key.offset = 0;
1462 key.type = BTRFS_DEV_ITEM_KEY;
1463
1464 while (1) {
1465 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1466 if (ret < 0)
1467 goto error;
1468
1469 leaf = path->nodes[0];
1470next_slot:
1471 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1472 ret = btrfs_next_leaf(root, path);
1473 if (ret > 0)
1474 break;
1475 if (ret < 0)
1476 goto error;
1477 leaf = path->nodes[0];
1478 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1479 btrfs_release_path(root, path);
1480 continue;
1481 }
1482
1483 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1484 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1485 key.type != BTRFS_DEV_ITEM_KEY)
1486 break;
1487
1488 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1489 struct btrfs_dev_item);
1490 devid = btrfs_device_id(leaf, dev_item);
1491 read_extent_buffer(leaf, dev_uuid,
1492 (unsigned long)btrfs_device_uuid(dev_item),
1493 BTRFS_UUID_SIZE);
1494 read_extent_buffer(leaf, fs_uuid,
1495 (unsigned long)btrfs_device_fsid(dev_item),
1496 BTRFS_UUID_SIZE);
1497 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1498 BUG_ON(!device);
1499
1500 if (device->fs_devices->seeding) {
1501 btrfs_set_device_generation(leaf, dev_item,
1502 device->generation);
1503 btrfs_mark_buffer_dirty(leaf);
1504 }
1505
1506 path->slots[0]++;
1507 goto next_slot;
1508 }
1509 ret = 0;
1510error:
1511 btrfs_free_path(path);
1512 return ret;
1513}
1514
Chris Mason788f20e2008-04-28 15:29:42 -04001515int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1516{
1517 struct btrfs_trans_handle *trans;
1518 struct btrfs_device *device;
1519 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001520 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001521 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001522 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001523 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001524 int ret = 0;
1525
Yan Zheng2b820322008-11-17 21:11:30 -05001526 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1527 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001528
Tejun Heod4d77622010-11-13 11:55:18 +01001529 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1530 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001531 if (IS_ERR(bdev))
1532 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001533
Yan Zheng2b820322008-11-17 21:11:30 -05001534 if (root->fs_info->fs_devices->seeding) {
1535 seeding_dev = 1;
1536 down_write(&sb->s_umount);
1537 mutex_lock(&uuid_mutex);
1538 }
1539
Chris Mason8c8bee12008-09-29 11:19:10 -04001540 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001541 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001542
Chris Mason788f20e2008-04-28 15:29:42 -04001543 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001544 /*
1545 * we have the volume lock, so we don't need the extra
1546 * device list mutex while reading the list here.
1547 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001548 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001549 if (device->bdev == bdev) {
1550 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001551 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001552 }
1553 }
1554
1555 device = kzalloc(sizeof(*device), GFP_NOFS);
1556 if (!device) {
1557 /* we can safely leave the fs_devices entry around */
1558 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001559 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001560 }
1561
Chris Mason788f20e2008-04-28 15:29:42 -04001562 device->name = kstrdup(device_path, GFP_NOFS);
1563 if (!device->name) {
1564 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001565 ret = -ENOMEM;
1566 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001567 }
Yan Zheng2b820322008-11-17 21:11:30 -05001568
1569 ret = find_next_devid(root, &device->devid);
1570 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001571 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001572 kfree(device);
1573 goto error;
1574 }
1575
Yan, Zhenga22285a2010-05-16 10:48:46 -04001576 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001577 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001578 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001579 kfree(device);
1580 ret = PTR_ERR(trans);
1581 goto error;
1582 }
1583
Yan Zheng2b820322008-11-17 21:11:30 -05001584 lock_chunks(root);
1585
Yan Zheng2b820322008-11-17 21:11:30 -05001586 device->writeable = 1;
1587 device->work.func = pending_bios_fn;
1588 generate_random_uuid(device->uuid);
1589 spin_lock_init(&device->io_lock);
1590 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001591 device->io_width = root->sectorsize;
1592 device->io_align = root->sectorsize;
1593 device->sector_size = root->sectorsize;
1594 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001595 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001596 device->dev_root = root->fs_info->dev_root;
1597 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001598 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001599 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001600 set_blocksize(device->bdev, 4096);
1601
Yan Zheng2b820322008-11-17 21:11:30 -05001602 if (seeding_dev) {
1603 sb->s_flags &= ~MS_RDONLY;
1604 ret = btrfs_prepare_sprout(trans, root);
1605 BUG_ON(ret);
1606 }
1607
1608 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001609
1610 /*
1611 * we don't want write_supers to jump in here with our device
1612 * half setup
1613 */
1614 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001615 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1616 list_add(&device->dev_alloc_list,
1617 &root->fs_info->fs_devices->alloc_list);
1618 root->fs_info->fs_devices->num_devices++;
1619 root->fs_info->fs_devices->open_devices++;
1620 root->fs_info->fs_devices->rw_devices++;
1621 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1622
Chris Masonc2898112009-06-10 09:51:32 -04001623 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1624 root->fs_info->fs_devices->rotating = 1;
1625
Chris Mason788f20e2008-04-28 15:29:42 -04001626 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1627 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1628 total_bytes + device->total_bytes);
1629
1630 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1631 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1632 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001633 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001634
Yan Zheng2b820322008-11-17 21:11:30 -05001635 if (seeding_dev) {
1636 ret = init_first_rw_device(trans, root, device);
1637 BUG_ON(ret);
1638 ret = btrfs_finish_sprout(trans, root);
1639 BUG_ON(ret);
1640 } else {
1641 ret = btrfs_add_device(trans, root, device);
1642 }
1643
Chris Mason913d9522009-03-10 13:17:18 -04001644 /*
1645 * we've got more storage, clear any full flags on the space
1646 * infos
1647 */
1648 btrfs_clear_space_info_full(root->fs_info);
1649
Chris Mason7d9eb122008-07-08 14:19:17 -04001650 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001651 btrfs_commit_transaction(trans, root);
1652
1653 if (seeding_dev) {
1654 mutex_unlock(&uuid_mutex);
1655 up_write(&sb->s_umount);
1656
1657 ret = btrfs_relocate_sys_chunks(root);
1658 BUG_ON(ret);
1659 }
1660out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001661 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001662 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001663error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001664 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001665 if (seeding_dev) {
1666 mutex_unlock(&uuid_mutex);
1667 up_write(&sb->s_umount);
1668 }
Chris Mason788f20e2008-04-28 15:29:42 -04001669 goto out;
1670}
1671
Chris Masond3977122009-01-05 21:25:51 -05001672static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1673 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001674{
1675 int ret;
1676 struct btrfs_path *path;
1677 struct btrfs_root *root;
1678 struct btrfs_dev_item *dev_item;
1679 struct extent_buffer *leaf;
1680 struct btrfs_key key;
1681
1682 root = device->dev_root->fs_info->chunk_root;
1683
1684 path = btrfs_alloc_path();
1685 if (!path)
1686 return -ENOMEM;
1687
1688 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1689 key.type = BTRFS_DEV_ITEM_KEY;
1690 key.offset = device->devid;
1691
1692 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1693 if (ret < 0)
1694 goto out;
1695
1696 if (ret > 0) {
1697 ret = -ENOENT;
1698 goto out;
1699 }
1700
1701 leaf = path->nodes[0];
1702 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1703
1704 btrfs_set_device_id(leaf, dev_item, device->devid);
1705 btrfs_set_device_type(leaf, dev_item, device->type);
1706 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1707 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1708 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001709 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001710 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1711 btrfs_mark_buffer_dirty(leaf);
1712
1713out:
1714 btrfs_free_path(path);
1715 return ret;
1716}
1717
Chris Mason7d9eb122008-07-08 14:19:17 -04001718static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001719 struct btrfs_device *device, u64 new_size)
1720{
1721 struct btrfs_super_block *super_copy =
1722 &device->dev_root->fs_info->super_copy;
1723 u64 old_total = btrfs_super_total_bytes(super_copy);
1724 u64 diff = new_size - device->total_bytes;
1725
Yan Zheng2b820322008-11-17 21:11:30 -05001726 if (!device->writeable)
1727 return -EACCES;
1728 if (new_size <= device->total_bytes)
1729 return -EINVAL;
1730
Chris Mason8f18cf12008-04-25 16:53:30 -04001731 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001732 device->fs_devices->total_rw_bytes += diff;
1733
1734 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001735 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001736 btrfs_clear_space_info_full(device->dev_root->fs_info);
1737
Chris Mason8f18cf12008-04-25 16:53:30 -04001738 return btrfs_update_device(trans, device);
1739}
1740
Chris Mason7d9eb122008-07-08 14:19:17 -04001741int btrfs_grow_device(struct btrfs_trans_handle *trans,
1742 struct btrfs_device *device, u64 new_size)
1743{
1744 int ret;
1745 lock_chunks(device->dev_root);
1746 ret = __btrfs_grow_device(trans, device, new_size);
1747 unlock_chunks(device->dev_root);
1748 return ret;
1749}
1750
Chris Mason8f18cf12008-04-25 16:53:30 -04001751static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1752 struct btrfs_root *root,
1753 u64 chunk_tree, u64 chunk_objectid,
1754 u64 chunk_offset)
1755{
1756 int ret;
1757 struct btrfs_path *path;
1758 struct btrfs_key key;
1759
1760 root = root->fs_info->chunk_root;
1761 path = btrfs_alloc_path();
1762 if (!path)
1763 return -ENOMEM;
1764
1765 key.objectid = chunk_objectid;
1766 key.offset = chunk_offset;
1767 key.type = BTRFS_CHUNK_ITEM_KEY;
1768
1769 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1770 BUG_ON(ret);
1771
1772 ret = btrfs_del_item(trans, root, path);
1773 BUG_ON(ret);
1774
1775 btrfs_free_path(path);
1776 return 0;
1777}
1778
Christoph Hellwigb2950862008-12-02 09:54:17 -05001779static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001780 chunk_offset)
1781{
1782 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1783 struct btrfs_disk_key *disk_key;
1784 struct btrfs_chunk *chunk;
1785 u8 *ptr;
1786 int ret = 0;
1787 u32 num_stripes;
1788 u32 array_size;
1789 u32 len = 0;
1790 u32 cur;
1791 struct btrfs_key key;
1792
1793 array_size = btrfs_super_sys_array_size(super_copy);
1794
1795 ptr = super_copy->sys_chunk_array;
1796 cur = 0;
1797
1798 while (cur < array_size) {
1799 disk_key = (struct btrfs_disk_key *)ptr;
1800 btrfs_disk_key_to_cpu(&key, disk_key);
1801
1802 len = sizeof(*disk_key);
1803
1804 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1805 chunk = (struct btrfs_chunk *)(ptr + len);
1806 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1807 len += btrfs_chunk_item_size(num_stripes);
1808 } else {
1809 ret = -EIO;
1810 break;
1811 }
1812 if (key.objectid == chunk_objectid &&
1813 key.offset == chunk_offset) {
1814 memmove(ptr, ptr + len, array_size - (cur + len));
1815 array_size -= len;
1816 btrfs_set_super_sys_array_size(super_copy, array_size);
1817 } else {
1818 ptr += len;
1819 cur += len;
1820 }
1821 }
1822 return ret;
1823}
1824
Christoph Hellwigb2950862008-12-02 09:54:17 -05001825static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001826 u64 chunk_tree, u64 chunk_objectid,
1827 u64 chunk_offset)
1828{
1829 struct extent_map_tree *em_tree;
1830 struct btrfs_root *extent_root;
1831 struct btrfs_trans_handle *trans;
1832 struct extent_map *em;
1833 struct map_lookup *map;
1834 int ret;
1835 int i;
1836
1837 root = root->fs_info->chunk_root;
1838 extent_root = root->fs_info->extent_root;
1839 em_tree = &root->fs_info->mapping_tree.map_tree;
1840
Josef Bacikba1bf482009-09-11 16:11:19 -04001841 ret = btrfs_can_relocate(extent_root, chunk_offset);
1842 if (ret)
1843 return -ENOSPC;
1844
Chris Mason8f18cf12008-04-25 16:53:30 -04001845 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001846 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001847 if (ret)
1848 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001849
Yan, Zhenga22285a2010-05-16 10:48:46 -04001850 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001851 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001852
Chris Mason7d9eb122008-07-08 14:19:17 -04001853 lock_chunks(root);
1854
Chris Mason8f18cf12008-04-25 16:53:30 -04001855 /*
1856 * step two, delete the device extents and the
1857 * chunk tree entries
1858 */
Chris Mason890871b2009-09-02 16:24:52 -04001859 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001860 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001861 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001862
Chris Masona061fc82008-05-07 11:43:44 -04001863 BUG_ON(em->start > chunk_offset ||
1864 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001865 map = (struct map_lookup *)em->bdev;
1866
1867 for (i = 0; i < map->num_stripes; i++) {
1868 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1869 map->stripes[i].physical);
1870 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001871
Chris Masondfe25022008-05-13 13:46:40 -04001872 if (map->stripes[i].dev) {
1873 ret = btrfs_update_device(trans, map->stripes[i].dev);
1874 BUG_ON(ret);
1875 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001876 }
1877 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1878 chunk_offset);
1879
1880 BUG_ON(ret);
1881
liubo1abe9b82011-03-24 11:18:59 +00001882 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1883
Chris Mason8f18cf12008-04-25 16:53:30 -04001884 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1885 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1886 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001887 }
1888
Zheng Yan1a40e232008-09-26 10:09:34 -04001889 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1890 BUG_ON(ret);
1891
Chris Mason890871b2009-09-02 16:24:52 -04001892 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001893 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001894 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001895
Chris Mason8f18cf12008-04-25 16:53:30 -04001896 kfree(map);
1897 em->bdev = NULL;
1898
1899 /* once for the tree */
1900 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001901 /* once for us */
1902 free_extent_map(em);
1903
Chris Mason7d9eb122008-07-08 14:19:17 -04001904 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001905 btrfs_end_transaction(trans, root);
1906 return 0;
1907}
1908
Yan Zheng2b820322008-11-17 21:11:30 -05001909static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1910{
1911 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1912 struct btrfs_path *path;
1913 struct extent_buffer *leaf;
1914 struct btrfs_chunk *chunk;
1915 struct btrfs_key key;
1916 struct btrfs_key found_key;
1917 u64 chunk_tree = chunk_root->root_key.objectid;
1918 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001919 bool retried = false;
1920 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001921 int ret;
1922
1923 path = btrfs_alloc_path();
1924 if (!path)
1925 return -ENOMEM;
1926
Josef Bacikba1bf482009-09-11 16:11:19 -04001927again:
Yan Zheng2b820322008-11-17 21:11:30 -05001928 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1929 key.offset = (u64)-1;
1930 key.type = BTRFS_CHUNK_ITEM_KEY;
1931
1932 while (1) {
1933 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1934 if (ret < 0)
1935 goto error;
1936 BUG_ON(ret == 0);
1937
1938 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1939 key.type);
1940 if (ret < 0)
1941 goto error;
1942 if (ret > 0)
1943 break;
1944
1945 leaf = path->nodes[0];
1946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1947
1948 chunk = btrfs_item_ptr(leaf, path->slots[0],
1949 struct btrfs_chunk);
1950 chunk_type = btrfs_chunk_type(leaf, chunk);
1951 btrfs_release_path(chunk_root, path);
1952
1953 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1954 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1955 found_key.objectid,
1956 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001957 if (ret == -ENOSPC)
1958 failed++;
1959 else if (ret)
1960 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001961 }
1962
1963 if (found_key.offset == 0)
1964 break;
1965 key.offset = found_key.offset - 1;
1966 }
1967 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001968 if (failed && !retried) {
1969 failed = 0;
1970 retried = true;
1971 goto again;
1972 } else if (failed && retried) {
1973 WARN_ON(1);
1974 ret = -ENOSPC;
1975 }
Yan Zheng2b820322008-11-17 21:11:30 -05001976error:
1977 btrfs_free_path(path);
1978 return ret;
1979}
1980
Chris Masonec44a352008-04-28 15:29:52 -04001981static u64 div_factor(u64 num, int factor)
1982{
1983 if (factor == 10)
1984 return num;
1985 num *= factor;
1986 do_div(num, 10);
1987 return num;
1988}
1989
Chris Masonec44a352008-04-28 15:29:52 -04001990int btrfs_balance(struct btrfs_root *dev_root)
1991{
1992 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001993 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1994 struct btrfs_device *device;
1995 u64 old_size;
1996 u64 size_to_free;
1997 struct btrfs_path *path;
1998 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04001999 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2000 struct btrfs_trans_handle *trans;
2001 struct btrfs_key found_key;
2002
Yan Zheng2b820322008-11-17 21:11:30 -05002003 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2004 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002005
Ben Hutchings6f88a442010-12-29 14:55:03 +00002006 if (!capable(CAP_SYS_ADMIN))
2007 return -EPERM;
2008
Chris Mason7d9eb122008-07-08 14:19:17 -04002009 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002010 dev_root = dev_root->fs_info->dev_root;
2011
Chris Masonec44a352008-04-28 15:29:52 -04002012 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002013 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002014 old_size = device->total_bytes;
2015 size_to_free = div_factor(old_size, 1);
2016 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002017 if (!device->writeable ||
2018 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002019 continue;
2020
2021 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002022 if (ret == -ENOSPC)
2023 break;
Chris Masonec44a352008-04-28 15:29:52 -04002024 BUG_ON(ret);
2025
Yan, Zhenga22285a2010-05-16 10:48:46 -04002026 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002027 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002028
2029 ret = btrfs_grow_device(trans, device, old_size);
2030 BUG_ON(ret);
2031
2032 btrfs_end_transaction(trans, dev_root);
2033 }
2034
2035 /* step two, relocate all the chunks */
2036 path = btrfs_alloc_path();
2037 BUG_ON(!path);
2038
2039 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2040 key.offset = (u64)-1;
2041 key.type = BTRFS_CHUNK_ITEM_KEY;
2042
Chris Masond3977122009-01-05 21:25:51 -05002043 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002044 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2045 if (ret < 0)
2046 goto error;
2047
2048 /*
2049 * this shouldn't happen, it means the last relocate
2050 * failed
2051 */
2052 if (ret == 0)
2053 break;
2054
2055 ret = btrfs_previous_item(chunk_root, path, 0,
2056 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002057 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002058 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002059
Chris Masonec44a352008-04-28 15:29:52 -04002060 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2061 path->slots[0]);
2062 if (found_key.objectid != key.objectid)
2063 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002064
Chris Masonec44a352008-04-28 15:29:52 -04002065 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002066 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002067 break;
2068
Chris Mason7d9eb122008-07-08 14:19:17 -04002069 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002070 ret = btrfs_relocate_chunk(chunk_root,
2071 chunk_root->root_key.objectid,
2072 found_key.objectid,
2073 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002074 BUG_ON(ret && ret != -ENOSPC);
2075 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002076 }
2077 ret = 0;
2078error:
2079 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002080 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002081 return ret;
2082}
2083
Chris Mason8f18cf12008-04-25 16:53:30 -04002084/*
2085 * shrinking a device means finding all of the device extents past
2086 * the new size, and then following the back refs to the chunks.
2087 * The chunk relocation code actually frees the device extent
2088 */
2089int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2090{
2091 struct btrfs_trans_handle *trans;
2092 struct btrfs_root *root = device->dev_root;
2093 struct btrfs_dev_extent *dev_extent = NULL;
2094 struct btrfs_path *path;
2095 u64 length;
2096 u64 chunk_tree;
2097 u64 chunk_objectid;
2098 u64 chunk_offset;
2099 int ret;
2100 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002101 int failed = 0;
2102 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002103 struct extent_buffer *l;
2104 struct btrfs_key key;
2105 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2106 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002107 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002108 u64 diff = device->total_bytes - new_size;
2109
Yan Zheng2b820322008-11-17 21:11:30 -05002110 if (new_size >= device->total_bytes)
2111 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002112
2113 path = btrfs_alloc_path();
2114 if (!path)
2115 return -ENOMEM;
2116
Chris Mason8f18cf12008-04-25 16:53:30 -04002117 path->reada = 2;
2118
Chris Mason7d9eb122008-07-08 14:19:17 -04002119 lock_chunks(root);
2120
Chris Mason8f18cf12008-04-25 16:53:30 -04002121 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002122 if (device->writeable)
2123 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002124 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002125
Josef Bacikba1bf482009-09-11 16:11:19 -04002126again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002127 key.objectid = device->devid;
2128 key.offset = (u64)-1;
2129 key.type = BTRFS_DEV_EXTENT_KEY;
2130
2131 while (1) {
2132 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2133 if (ret < 0)
2134 goto done;
2135
2136 ret = btrfs_previous_item(root, path, 0, key.type);
2137 if (ret < 0)
2138 goto done;
2139 if (ret) {
2140 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002141 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002142 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002143 }
2144
2145 l = path->nodes[0];
2146 slot = path->slots[0];
2147 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2148
Josef Bacikba1bf482009-09-11 16:11:19 -04002149 if (key.objectid != device->devid) {
2150 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002151 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002152 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002153
2154 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2155 length = btrfs_dev_extent_length(l, dev_extent);
2156
Josef Bacikba1bf482009-09-11 16:11:19 -04002157 if (key.offset + length <= new_size) {
2158 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002159 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002160 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002161
2162 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2163 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2164 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2165 btrfs_release_path(root, path);
2166
2167 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2168 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002169 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002170 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002171 if (ret == -ENOSPC)
2172 failed++;
2173 key.offset -= 1;
2174 }
2175
2176 if (failed && !retried) {
2177 failed = 0;
2178 retried = true;
2179 goto again;
2180 } else if (failed && retried) {
2181 ret = -ENOSPC;
2182 lock_chunks(root);
2183
2184 device->total_bytes = old_size;
2185 if (device->writeable)
2186 device->fs_devices->total_rw_bytes += diff;
2187 unlock_chunks(root);
2188 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002189 }
2190
Chris Balld6397ba2009-04-27 07:29:03 -04002191 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002192 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002193 if (IS_ERR(trans)) {
2194 ret = PTR_ERR(trans);
2195 goto done;
2196 }
2197
Chris Balld6397ba2009-04-27 07:29:03 -04002198 lock_chunks(root);
2199
2200 device->disk_total_bytes = new_size;
2201 /* Now btrfs_update_device() will change the on-disk size. */
2202 ret = btrfs_update_device(trans, device);
2203 if (ret) {
2204 unlock_chunks(root);
2205 btrfs_end_transaction(trans, root);
2206 goto done;
2207 }
2208 WARN_ON(diff > old_total);
2209 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2210 unlock_chunks(root);
2211 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002212done:
2213 btrfs_free_path(path);
2214 return ret;
2215}
2216
Christoph Hellwigb2950862008-12-02 09:54:17 -05002217static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002218 struct btrfs_root *root,
2219 struct btrfs_key *key,
2220 struct btrfs_chunk *chunk, int item_size)
2221{
2222 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2223 struct btrfs_disk_key disk_key;
2224 u32 array_size;
2225 u8 *ptr;
2226
2227 array_size = btrfs_super_sys_array_size(super_copy);
2228 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2229 return -EFBIG;
2230
2231 ptr = super_copy->sys_chunk_array + array_size;
2232 btrfs_cpu_key_to_disk(&disk_key, key);
2233 memcpy(ptr, &disk_key, sizeof(disk_key));
2234 ptr += sizeof(disk_key);
2235 memcpy(ptr, chunk, item_size);
2236 item_size += sizeof(disk_key);
2237 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2238 return 0;
2239}
2240
Chris Masond3977122009-01-05 21:25:51 -05002241static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002242 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002243{
2244 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2245 return calc_size;
2246 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2247 return calc_size * (num_stripes / sub_stripes);
2248 else
2249 return calc_size * num_stripes;
2250}
2251
Miao Xieb2117a32011-01-05 10:07:28 +00002252/* Used to sort the devices by max_avail(descending sort) */
2253int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
Chris Mason0b86a832008-03-24 15:01:56 -04002254{
Miao Xieb2117a32011-01-05 10:07:28 +00002255 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2256 ((struct btrfs_device_info *)dev_info2)->max_avail)
2257 return -1;
2258 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2259 ((struct btrfs_device_info *)dev_info2)->max_avail)
2260 return 1;
2261 else
2262 return 0;
2263}
Chris Mason0b86a832008-03-24 15:01:56 -04002264
Miao Xieb2117a32011-01-05 10:07:28 +00002265static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2266 int *num_stripes, int *min_stripes,
2267 int *sub_stripes)
2268{
2269 *num_stripes = 1;
2270 *min_stripes = 1;
2271 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002272
Chris Masona40a90a2008-04-18 11:55:51 -04002273 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002274 *num_stripes = fs_devices->rw_devices;
2275 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002276 }
2277 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002278 *num_stripes = 2;
2279 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002280 }
Chris Mason8790d502008-04-03 16:29:03 -04002281 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002282 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002283 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002284 *num_stripes = 2;
2285 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002286 }
Chris Mason321aecc2008-04-16 10:49:51 -04002287 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002288 *num_stripes = fs_devices->rw_devices;
2289 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002290 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002291 *num_stripes &= ~(u32)1;
2292 *sub_stripes = 2;
2293 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002294 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002295
Miao Xieb2117a32011-01-05 10:07:28 +00002296 return 0;
2297}
2298
2299static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2300 u64 proposed_size, u64 type,
2301 int num_stripes, int small_stripe)
2302{
2303 int min_stripe_size = 1 * 1024 * 1024;
2304 u64 calc_size = proposed_size;
2305 u64 max_chunk_size = calc_size;
2306 int ncopies = 1;
2307
2308 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2309 BTRFS_BLOCK_GROUP_DUP |
2310 BTRFS_BLOCK_GROUP_RAID10))
2311 ncopies = 2;
2312
Chris Mason9b3f68b2008-04-18 10:29:51 -04002313 if (type & BTRFS_BLOCK_GROUP_DATA) {
2314 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002315 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002316 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002317 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002318 min_stripe_size = 32 * 1024 * 1024;
2319 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2320 calc_size = 8 * 1024 * 1024;
2321 max_chunk_size = calc_size * 2;
2322 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002323 }
2324
Yan Zheng2b820322008-11-17 21:11:30 -05002325 /* we don't want a chunk larger than 10% of writeable space */
2326 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2327 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002328
Miao Xie1974a3b2011-01-05 10:07:24 +00002329 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2330 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002331 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002332 do_div(calc_size, BTRFS_STRIPE_LEN);
2333 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002334 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002335
Chris Mason9b3f68b2008-04-18 10:29:51 -04002336 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002337 if (!small_stripe)
Josef Bacik0cad8a112010-03-17 20:45:56 +00002338 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002339
Chris Mason9f680ce2010-04-06 09:37:47 -04002340 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002341 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002342 * we end up with something bigger than a stripe
2343 */
Miao Xieb2117a32011-01-05 10:07:28 +00002344 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002345
Miao Xieb2117a32011-01-05 10:07:28 +00002346 do_div(calc_size, BTRFS_STRIPE_LEN);
2347 calc_size *= BTRFS_STRIPE_LEN;
2348
2349 return calc_size;
2350}
2351
2352static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2353 int num_stripes)
2354{
2355 struct map_lookup *new;
2356 size_t len = map_lookup_size(num_stripes);
2357
2358 BUG_ON(map->num_stripes < num_stripes);
2359
2360 if (map->num_stripes == num_stripes)
2361 return map;
2362
2363 new = kmalloc(len, GFP_NOFS);
2364 if (!new) {
2365 /* just change map->num_stripes */
2366 map->num_stripes = num_stripes;
2367 return map;
2368 }
2369
2370 memcpy(new, map, len);
2371 new->num_stripes = num_stripes;
2372 kfree(map);
2373 return new;
2374}
2375
2376/*
2377 * helper to allocate device space from btrfs_device_info, in which we stored
2378 * max free space information of every device. It is used when we can not
2379 * allocate chunks by default size.
2380 *
2381 * By this helper, we can allocate a new chunk as larger as possible.
2382 */
2383static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2384 struct btrfs_fs_devices *fs_devices,
2385 struct btrfs_device_info *devices,
2386 int nr_device, u64 type,
2387 struct map_lookup **map_lookup,
2388 int min_stripes, u64 *stripe_size)
2389{
2390 int i, index, sort_again = 0;
2391 int min_devices = min_stripes;
2392 u64 max_avail, min_free;
2393 struct map_lookup *map = *map_lookup;
2394 int ret;
2395
2396 if (nr_device < min_stripes)
2397 return -ENOSPC;
2398
2399 btrfs_descending_sort_devices(devices, nr_device);
2400
2401 max_avail = devices[0].max_avail;
2402 if (!max_avail)
2403 return -ENOSPC;
2404
2405 for (i = 0; i < nr_device; i++) {
2406 /*
2407 * if dev_offset = 0, it means the free space of this device
2408 * is less than what we need, and we didn't search max avail
2409 * extent on this device, so do it now.
2410 */
2411 if (!devices[i].dev_offset) {
2412 ret = find_free_dev_extent(trans, devices[i].dev,
2413 max_avail,
2414 &devices[i].dev_offset,
2415 &devices[i].max_avail);
2416 if (ret != 0 && ret != -ENOSPC)
2417 return ret;
2418 sort_again = 1;
2419 }
2420 }
2421
2422 /* we update the max avail free extent of each devices, sort again */
2423 if (sort_again)
2424 btrfs_descending_sort_devices(devices, nr_device);
2425
2426 if (type & BTRFS_BLOCK_GROUP_DUP)
2427 min_devices = 1;
2428
2429 if (!devices[min_devices - 1].max_avail)
2430 return -ENOSPC;
2431
2432 max_avail = devices[min_devices - 1].max_avail;
2433 if (type & BTRFS_BLOCK_GROUP_DUP)
2434 do_div(max_avail, 2);
2435
2436 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2437 min_stripes, 1);
2438 if (type & BTRFS_BLOCK_GROUP_DUP)
2439 min_free = max_avail * 2;
2440 else
2441 min_free = max_avail;
2442
2443 if (min_free > devices[min_devices - 1].max_avail)
2444 return -ENOSPC;
2445
2446 map = __shrink_map_lookup_stripes(map, min_stripes);
2447 *stripe_size = max_avail;
2448
2449 index = 0;
2450 for (i = 0; i < min_stripes; i++) {
2451 map->stripes[i].dev = devices[index].dev;
2452 map->stripes[i].physical = devices[index].dev_offset;
2453 if (type & BTRFS_BLOCK_GROUP_DUP) {
2454 i++;
2455 map->stripes[i].dev = devices[index].dev;
2456 map->stripes[i].physical = devices[index].dev_offset +
2457 max_avail;
2458 }
2459 index++;
2460 }
2461 *map_lookup = map;
2462
2463 return 0;
2464}
2465
2466static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2467 struct btrfs_root *extent_root,
2468 struct map_lookup **map_ret,
2469 u64 *num_bytes, u64 *stripe_size,
2470 u64 start, u64 type)
2471{
2472 struct btrfs_fs_info *info = extent_root->fs_info;
2473 struct btrfs_device *device = NULL;
2474 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2475 struct list_head *cur;
2476 struct map_lookup *map;
2477 struct extent_map_tree *em_tree;
2478 struct extent_map *em;
2479 struct btrfs_device_info *devices_info;
2480 struct list_head private_devs;
2481 u64 calc_size = 1024 * 1024 * 1024;
2482 u64 min_free;
2483 u64 avail;
2484 u64 dev_offset;
2485 int num_stripes;
2486 int min_stripes;
2487 int sub_stripes;
2488 int min_devices; /* the min number of devices we need */
2489 int i;
2490 int ret;
2491 int index;
2492
2493 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2494 (type & BTRFS_BLOCK_GROUP_DUP)) {
2495 WARN_ON(1);
2496 type &= ~BTRFS_BLOCK_GROUP_DUP;
2497 }
2498 if (list_empty(&fs_devices->alloc_list))
2499 return -ENOSPC;
2500
2501 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2502 &min_stripes, &sub_stripes);
2503 if (ret)
2504 return ret;
2505
2506 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2507 GFP_NOFS);
2508 if (!devices_info)
2509 return -ENOMEM;
2510
2511 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2512 if (!map) {
2513 ret = -ENOMEM;
2514 goto error;
2515 }
2516 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002517
Yan Zheng2b820322008-11-17 21:11:30 -05002518 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002519 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002520 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002521
Miao Xieb2117a32011-01-05 10:07:28 +00002522 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2523 num_stripes, 0);
2524
2525 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002526 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002527 min_devices = 1;
2528 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002529 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002530 min_devices = min_stripes;
2531 }
Chris Masonad5bd912008-04-21 08:28:10 -04002532
Yan Zheng2b820322008-11-17 21:11:30 -05002533 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002534 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002535 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002536 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002537 if (device->total_bytes > device->bytes_used)
2538 avail = device->total_bytes - device->bytes_used;
2539 else
2540 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002541 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002542
Chris Masondfe25022008-05-13 13:46:40 -04002543 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002544 ret = find_free_dev_extent(trans, device, min_free,
2545 &devices_info[i].dev_offset,
2546 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002547 if (ret == 0) {
2548 list_move_tail(&device->dev_alloc_list,
2549 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002550 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002551 map->stripes[index].physical =
2552 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002553 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002554 if (type & BTRFS_BLOCK_GROUP_DUP) {
2555 map->stripes[index].dev = device;
2556 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002557 devices_info[i].dev_offset +
2558 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002559 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002560 }
Miao Xieb2117a32011-01-05 10:07:28 +00002561 } else if (ret != -ENOSPC)
2562 goto error;
2563
2564 devices_info[i].dev = device;
2565 i++;
2566 } else if (device->in_fs_metadata &&
2567 avail >= BTRFS_STRIPE_LEN) {
2568 devices_info[i].dev = device;
2569 devices_info[i].max_avail = avail;
2570 i++;
2571 }
2572
Yan Zheng2b820322008-11-17 21:11:30 -05002573 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002574 break;
2575 }
Miao Xieb2117a32011-01-05 10:07:28 +00002576
Yan Zheng2b820322008-11-17 21:11:30 -05002577 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002578 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002579 if (index >= min_stripes) {
2580 num_stripes = index;
2581 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2582 num_stripes /= sub_stripes;
2583 num_stripes *= sub_stripes;
2584 }
Miao Xieb2117a32011-01-05 10:07:28 +00002585
2586 map = __shrink_map_lookup_stripes(map, num_stripes);
2587 } else if (i >= min_devices) {
2588 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2589 devices_info, i, type,
2590 &map, min_stripes,
2591 &calc_size);
2592 if (ret)
2593 goto error;
2594 } else {
2595 ret = -ENOSPC;
2596 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002597 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002598 }
Chris Mason593060d2008-03-25 16:50:33 -04002599 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002600 map->stripe_len = BTRFS_STRIPE_LEN;
2601 map->io_align = BTRFS_STRIPE_LEN;
2602 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002603 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002604 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002605
Yan Zheng2b820322008-11-17 21:11:30 -05002606 *map_ret = map;
2607 *stripe_size = calc_size;
2608 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002609 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002610
liubo1abe9b82011-03-24 11:18:59 +00002611 trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes);
2612
Chris Mason0b86a832008-03-24 15:01:56 -04002613 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002614 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002615 ret = -ENOMEM;
2616 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002617 }
Chris Mason0b86a832008-03-24 15:01:56 -04002618 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002619 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002620 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002621 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002622 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002623
Chris Mason0b86a832008-03-24 15:01:56 -04002624 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002625 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002626 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002627 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002628 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002629 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002630
2631 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2632 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2633 start, *num_bytes);
2634 BUG_ON(ret);
2635
2636 index = 0;
2637 while (index < map->num_stripes) {
2638 device = map->stripes[index].dev;
2639 dev_offset = map->stripes[index].physical;
2640
2641 ret = btrfs_alloc_dev_extent(trans, device,
2642 info->chunk_root->root_key.objectid,
2643 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2644 start, dev_offset, calc_size);
2645 BUG_ON(ret);
2646 index++;
2647 }
2648
Miao Xieb2117a32011-01-05 10:07:28 +00002649 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002650 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002651
2652error:
2653 kfree(map);
2654 kfree(devices_info);
2655 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002656}
2657
2658static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2659 struct btrfs_root *extent_root,
2660 struct map_lookup *map, u64 chunk_offset,
2661 u64 chunk_size, u64 stripe_size)
2662{
2663 u64 dev_offset;
2664 struct btrfs_key key;
2665 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2666 struct btrfs_device *device;
2667 struct btrfs_chunk *chunk;
2668 struct btrfs_stripe *stripe;
2669 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2670 int index = 0;
2671 int ret;
2672
2673 chunk = kzalloc(item_size, GFP_NOFS);
2674 if (!chunk)
2675 return -ENOMEM;
2676
2677 index = 0;
2678 while (index < map->num_stripes) {
2679 device = map->stripes[index].dev;
2680 device->bytes_used += stripe_size;
2681 ret = btrfs_update_device(trans, device);
2682 BUG_ON(ret);
2683 index++;
2684 }
2685
2686 index = 0;
2687 stripe = &chunk->stripe;
2688 while (index < map->num_stripes) {
2689 device = map->stripes[index].dev;
2690 dev_offset = map->stripes[index].physical;
2691
2692 btrfs_set_stack_stripe_devid(stripe, device->devid);
2693 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2694 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2695 stripe++;
2696 index++;
2697 }
2698
2699 btrfs_set_stack_chunk_length(chunk, chunk_size);
2700 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2701 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2702 btrfs_set_stack_chunk_type(chunk, map->type);
2703 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2704 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2705 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2706 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2707 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2708
2709 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2710 key.type = BTRFS_CHUNK_ITEM_KEY;
2711 key.offset = chunk_offset;
2712
2713 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2714 BUG_ON(ret);
2715
2716 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2717 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2718 item_size);
2719 BUG_ON(ret);
2720 }
liubo1abe9b82011-03-24 11:18:59 +00002721
Yan Zheng2b820322008-11-17 21:11:30 -05002722 kfree(chunk);
2723 return 0;
2724}
2725
2726/*
2727 * Chunk allocation falls into two parts. The first part does works
2728 * that make the new allocated chunk useable, but not do any operation
2729 * that modifies the chunk tree. The second part does the works that
2730 * require modifying the chunk tree. This division is important for the
2731 * bootstrap process of adding storage to a seed btrfs.
2732 */
2733int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2734 struct btrfs_root *extent_root, u64 type)
2735{
2736 u64 chunk_offset;
2737 u64 chunk_size;
2738 u64 stripe_size;
2739 struct map_lookup *map;
2740 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2741 int ret;
2742
2743 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2744 &chunk_offset);
2745 if (ret)
2746 return ret;
2747
2748 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2749 &stripe_size, chunk_offset, type);
2750 if (ret)
2751 return ret;
2752
2753 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2754 chunk_size, stripe_size);
2755 BUG_ON(ret);
2756 return 0;
2757}
2758
Chris Masond3977122009-01-05 21:25:51 -05002759static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002760 struct btrfs_root *root,
2761 struct btrfs_device *device)
2762{
2763 u64 chunk_offset;
2764 u64 sys_chunk_offset;
2765 u64 chunk_size;
2766 u64 sys_chunk_size;
2767 u64 stripe_size;
2768 u64 sys_stripe_size;
2769 u64 alloc_profile;
2770 struct map_lookup *map;
2771 struct map_lookup *sys_map;
2772 struct btrfs_fs_info *fs_info = root->fs_info;
2773 struct btrfs_root *extent_root = fs_info->extent_root;
2774 int ret;
2775
2776 ret = find_next_chunk(fs_info->chunk_root,
2777 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2778 BUG_ON(ret);
2779
2780 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2781 (fs_info->metadata_alloc_profile &
2782 fs_info->avail_metadata_alloc_bits);
2783 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2784
2785 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2786 &stripe_size, chunk_offset, alloc_profile);
2787 BUG_ON(ret);
2788
2789 sys_chunk_offset = chunk_offset + chunk_size;
2790
2791 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2792 (fs_info->system_alloc_profile &
2793 fs_info->avail_system_alloc_bits);
2794 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2795
2796 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2797 &sys_chunk_size, &sys_stripe_size,
2798 sys_chunk_offset, alloc_profile);
2799 BUG_ON(ret);
2800
2801 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2802 BUG_ON(ret);
2803
2804 /*
2805 * Modifying chunk tree needs allocating new blocks from both
2806 * system block group and metadata block group. So we only can
2807 * do operations require modifying the chunk tree after both
2808 * block groups were created.
2809 */
2810 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2811 chunk_size, stripe_size);
2812 BUG_ON(ret);
2813
2814 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2815 sys_chunk_offset, sys_chunk_size,
2816 sys_stripe_size);
2817 BUG_ON(ret);
2818 return 0;
2819}
2820
2821int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2822{
2823 struct extent_map *em;
2824 struct map_lookup *map;
2825 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2826 int readonly = 0;
2827 int i;
2828
Chris Mason890871b2009-09-02 16:24:52 -04002829 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002830 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002831 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002832 if (!em)
2833 return 1;
2834
Josef Bacikf48b9072010-01-27 02:07:59 +00002835 if (btrfs_test_opt(root, DEGRADED)) {
2836 free_extent_map(em);
2837 return 0;
2838 }
2839
Yan Zheng2b820322008-11-17 21:11:30 -05002840 map = (struct map_lookup *)em->bdev;
2841 for (i = 0; i < map->num_stripes; i++) {
2842 if (!map->stripes[i].dev->writeable) {
2843 readonly = 1;
2844 break;
2845 }
2846 }
2847 free_extent_map(em);
2848 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002849}
2850
2851void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2852{
2853 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2854}
2855
2856void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2857{
2858 struct extent_map *em;
2859
Chris Masond3977122009-01-05 21:25:51 -05002860 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002861 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002862 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2863 if (em)
2864 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002865 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002866 if (!em)
2867 break;
2868 kfree(em->bdev);
2869 /* once for us */
2870 free_extent_map(em);
2871 /* once for the tree */
2872 free_extent_map(em);
2873 }
2874}
2875
Chris Masonf1885912008-04-09 16:28:12 -04002876int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2877{
2878 struct extent_map *em;
2879 struct map_lookup *map;
2880 struct extent_map_tree *em_tree = &map_tree->map_tree;
2881 int ret;
2882
Chris Mason890871b2009-09-02 16:24:52 -04002883 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002884 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002885 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002886 BUG_ON(!em);
2887
2888 BUG_ON(em->start > logical || em->start + em->len < logical);
2889 map = (struct map_lookup *)em->bdev;
2890 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2891 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002892 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2893 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002894 else
2895 ret = 1;
2896 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002897 return ret;
2898}
2899
Chris Masondfe25022008-05-13 13:46:40 -04002900static int find_live_mirror(struct map_lookup *map, int first, int num,
2901 int optimal)
2902{
2903 int i;
2904 if (map->stripes[optimal].dev->bdev)
2905 return optimal;
2906 for (i = first; i < first + num; i++) {
2907 if (map->stripes[i].dev->bdev)
2908 return i;
2909 }
2910 /* we couldn't find one that doesn't fail. Just return something
2911 * and the io error handling code will clean up eventually
2912 */
2913 return optimal;
2914}
2915
Chris Masonf2d8d742008-04-21 10:03:05 -04002916static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2917 u64 logical, u64 *length,
2918 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002919 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002920{
2921 struct extent_map *em;
2922 struct map_lookup *map;
2923 struct extent_map_tree *em_tree = &map_tree->map_tree;
2924 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002925 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002926 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002927 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002928 u64 stripe_nr_orig;
2929 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002930 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002931 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002932 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002933 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002934 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002935 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002936 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002937
Li Dongyangfce3bb92011-03-24 10:24:26 +00002938 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002939 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002940again:
2941 if (multi_ret) {
2942 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2943 GFP_NOFS);
2944 if (!multi)
2945 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002946
2947 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002948 }
Chris Mason0b86a832008-03-24 15:01:56 -04002949
Chris Mason890871b2009-09-02 16:24:52 -04002950 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002951 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002952 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002953
Chris Mason3b951512008-04-17 11:29:12 -04002954 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002955 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2956 (unsigned long long)logical,
2957 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002958 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002959 }
Chris Mason0b86a832008-03-24 15:01:56 -04002960
2961 BUG_ON(em->start > logical || em->start + em->len < logical);
2962 map = (struct map_lookup *)em->bdev;
2963 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002964
Chris Masonf1885912008-04-09 16:28:12 -04002965 if (mirror_num > map->num_stripes)
2966 mirror_num = 0;
2967
Chris Masoncea9e442008-04-09 16:28:12 -04002968 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002969 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002970 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2971 BTRFS_BLOCK_GROUP_DUP)) {
2972 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002973 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002974 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2975 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002976 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002977 }
2978 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002979 if (rw & REQ_DISCARD) {
2980 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2981 BTRFS_BLOCK_GROUP_RAID1 |
2982 BTRFS_BLOCK_GROUP_DUP |
2983 BTRFS_BLOCK_GROUP_RAID10)) {
2984 stripes_required = map->num_stripes;
2985 }
2986 }
2987 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002988 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002989 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002990 free_extent_map(em);
2991 kfree(multi);
2992 goto again;
2993 }
Chris Mason593060d2008-03-25 16:50:33 -04002994 stripe_nr = offset;
2995 /*
2996 * stripe_nr counts the total number of stripes we have to stride
2997 * to get to this block
2998 */
2999 do_div(stripe_nr, map->stripe_len);
3000
3001 stripe_offset = stripe_nr * map->stripe_len;
3002 BUG_ON(offset < stripe_offset);
3003
3004 /* stripe_offset is the offset of this block in its stripe*/
3005 stripe_offset = offset - stripe_offset;
3006
Li Dongyangfce3bb92011-03-24 10:24:26 +00003007 if (rw & REQ_DISCARD)
3008 *length = min_t(u64, em->len - offset, *length);
3009 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3010 BTRFS_BLOCK_GROUP_RAID1 |
3011 BTRFS_BLOCK_GROUP_RAID10 |
3012 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04003013 /* we limit the length of each bio to what fits in a stripe */
3014 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003015 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003016 } else {
3017 *length = em->len - offset;
3018 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003019
Jens Axboe7eaceac2011-03-10 08:52:07 +01003020 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003021 goto out;
3022
Chris Masonf2d8d742008-04-21 10:03:05 -04003023 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003024 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003025 stripe_nr_orig = stripe_nr;
3026 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3027 (~(map->stripe_len - 1));
3028 do_div(stripe_nr_end, map->stripe_len);
3029 stripe_end_offset = stripe_nr_end * map->stripe_len -
3030 (offset + *length);
3031 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3032 if (rw & REQ_DISCARD)
3033 num_stripes = min_t(u64, map->num_stripes,
3034 stripe_nr_end - stripe_nr_orig);
3035 stripe_index = do_div(stripe_nr, map->num_stripes);
3036 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003037 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003038 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003039 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003040 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003041 else {
3042 stripe_index = find_live_mirror(map, 0,
3043 map->num_stripes,
3044 current->pid % map->num_stripes);
3045 }
Chris Mason2fff7342008-04-29 14:12:09 -04003046
Chris Mason611f0e02008-04-03 16:29:03 -04003047 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00003048 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003049 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003050 else if (mirror_num)
3051 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003052
Chris Mason321aecc2008-04-16 10:49:51 -04003053 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3054 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003055
3056 stripe_index = do_div(stripe_nr, factor);
3057 stripe_index *= map->sub_stripes;
3058
Jens Axboe7eaceac2011-03-10 08:52:07 +01003059 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003060 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003061 else if (rw & REQ_DISCARD)
3062 num_stripes = min_t(u64, map->sub_stripes *
3063 (stripe_nr_end - stripe_nr_orig),
3064 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003065 else if (mirror_num)
3066 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003067 else {
3068 stripe_index = find_live_mirror(map, stripe_index,
3069 map->sub_stripes, stripe_index +
3070 current->pid % map->sub_stripes);
3071 }
Chris Mason8790d502008-04-03 16:29:03 -04003072 } else {
3073 /*
3074 * after this do_div call, stripe_nr is the number of stripes
3075 * on this device we have to walk to find the data, and
3076 * stripe_index is the number of our device in the stripe array
3077 */
3078 stripe_index = do_div(stripe_nr, map->num_stripes);
3079 }
Chris Mason593060d2008-03-25 16:50:33 -04003080 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003081
Li Dongyangfce3bb92011-03-24 10:24:26 +00003082 if (rw & REQ_DISCARD) {
3083 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003084 multi->stripes[i].physical =
3085 map->stripes[stripe_index].physical +
3086 stripe_offset + stripe_nr * map->stripe_len;
3087 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003088
3089 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3090 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003091 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003092 int j;
3093
Chris Masond9d04872011-03-27 21:23:21 -04003094 div_u64_rem(stripe_nr_end - 1,
3095 map->num_stripes,
3096 &last_stripe);
3097
Li Dongyangfce3bb92011-03-24 10:24:26 +00003098 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003099 u32 test;
3100
3101 div_u64_rem(stripe_nr_end - 1 - j,
3102 map->num_stripes, &test);
3103 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003104 break;
3105 }
3106 stripes = stripe_nr_end - 1 - j;
3107 do_div(stripes, map->num_stripes);
3108 multi->stripes[i].length = map->stripe_len *
3109 (stripes - stripe_nr + 1);
3110
3111 if (i == 0) {
3112 multi->stripes[i].length -=
3113 stripe_offset;
3114 stripe_offset = 0;
3115 }
3116 if (stripe_index == last_stripe)
3117 multi->stripes[i].length -=
3118 stripe_end_offset;
3119 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3120 u64 stripes;
3121 int j;
3122 int factor = map->num_stripes /
3123 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003124 u32 last_stripe = 0;
3125
3126 div_u64_rem(stripe_nr_end - 1,
3127 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003128 last_stripe *= map->sub_stripes;
3129
3130 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003131 u32 test;
3132
3133 div_u64_rem(stripe_nr_end - 1 - j,
3134 factor, &test);
3135
3136 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003137 stripe_index / map->sub_stripes)
3138 break;
3139 }
3140 stripes = stripe_nr_end - 1 - j;
3141 do_div(stripes, factor);
3142 multi->stripes[i].length = map->stripe_len *
3143 (stripes - stripe_nr + 1);
3144
3145 if (i < map->sub_stripes) {
3146 multi->stripes[i].length -=
3147 stripe_offset;
3148 if (i == map->sub_stripes - 1)
3149 stripe_offset = 0;
3150 }
3151 if (stripe_index >= last_stripe &&
3152 stripe_index <= (last_stripe +
3153 map->sub_stripes - 1)) {
3154 multi->stripes[i].length -=
3155 stripe_end_offset;
3156 }
3157 } else
3158 multi->stripes[i].length = *length;
3159
3160 stripe_index++;
3161 if (stripe_index == map->num_stripes) {
3162 /* This could only happen for RAID0/10 */
3163 stripe_index = 0;
3164 stripe_nr++;
3165 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003166 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003167 } else {
3168 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003169 multi->stripes[i].physical =
3170 map->stripes[stripe_index].physical +
3171 stripe_offset +
3172 stripe_nr * map->stripe_len;
3173 multi->stripes[i].dev =
3174 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003175 stripe_index++;
3176 }
Chris Mason593060d2008-03-25 16:50:33 -04003177 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003178 if (multi_ret) {
3179 *multi_ret = multi;
3180 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003181 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003182 }
Chris Masoncea9e442008-04-09 16:28:12 -04003183out:
Chris Mason0b86a832008-03-24 15:01:56 -04003184 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003185 return 0;
3186}
3187
Chris Masonf2d8d742008-04-21 10:03:05 -04003188int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3189 u64 logical, u64 *length,
3190 struct btrfs_multi_bio **multi_ret, int mirror_num)
3191{
3192 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003193 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003194}
3195
Yan Zhenga512bbf2008-12-08 16:46:26 -05003196int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3197 u64 chunk_start, u64 physical, u64 devid,
3198 u64 **logical, int *naddrs, int *stripe_len)
3199{
3200 struct extent_map_tree *em_tree = &map_tree->map_tree;
3201 struct extent_map *em;
3202 struct map_lookup *map;
3203 u64 *buf;
3204 u64 bytenr;
3205 u64 length;
3206 u64 stripe_nr;
3207 int i, j, nr = 0;
3208
Chris Mason890871b2009-09-02 16:24:52 -04003209 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003210 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003211 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003212
3213 BUG_ON(!em || em->start != chunk_start);
3214 map = (struct map_lookup *)em->bdev;
3215
3216 length = em->len;
3217 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3218 do_div(length, map->num_stripes / map->sub_stripes);
3219 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3220 do_div(length, map->num_stripes);
3221
3222 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3223 BUG_ON(!buf);
3224
3225 for (i = 0; i < map->num_stripes; i++) {
3226 if (devid && map->stripes[i].dev->devid != devid)
3227 continue;
3228 if (map->stripes[i].physical > physical ||
3229 map->stripes[i].physical + length <= physical)
3230 continue;
3231
3232 stripe_nr = physical - map->stripes[i].physical;
3233 do_div(stripe_nr, map->stripe_len);
3234
3235 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3236 stripe_nr = stripe_nr * map->num_stripes + i;
3237 do_div(stripe_nr, map->sub_stripes);
3238 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3239 stripe_nr = stripe_nr * map->num_stripes + i;
3240 }
3241 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003242 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003243 for (j = 0; j < nr; j++) {
3244 if (buf[j] == bytenr)
3245 break;
3246 }
Chris Mason934d3752008-12-08 16:43:10 -05003247 if (j == nr) {
3248 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003249 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003250 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003251 }
3252
Yan Zhenga512bbf2008-12-08 16:46:26 -05003253 *logical = buf;
3254 *naddrs = nr;
3255 *stripe_len = map->stripe_len;
3256
3257 free_extent_map(em);
3258 return 0;
3259}
3260
Chris Mason8790d502008-04-03 16:29:03 -04003261static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003262{
Chris Masoncea9e442008-04-09 16:28:12 -04003263 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003264 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003265
Chris Mason8790d502008-04-03 16:29:03 -04003266 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003267 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003268
Chris Mason7d2b4da2008-08-05 10:13:57 -04003269 if (bio == multi->orig_bio)
3270 is_orig_bio = 1;
3271
Chris Masoncea9e442008-04-09 16:28:12 -04003272 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003273 if (!is_orig_bio) {
3274 bio_put(bio);
3275 bio = multi->orig_bio;
3276 }
Chris Mason8790d502008-04-03 16:29:03 -04003277 bio->bi_private = multi->private;
3278 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003279 /* only send an error to the higher layers if it is
3280 * beyond the tolerance of the multi-bio
3281 */
Chris Mason1259ab72008-05-12 13:39:03 -04003282 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003283 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003284 } else if (err) {
3285 /*
3286 * this bio is actually up to date, we didn't
3287 * go over the max number of errors
3288 */
3289 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003290 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003291 }
Chris Mason8790d502008-04-03 16:29:03 -04003292 kfree(multi);
3293
3294 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003295 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003296 bio_put(bio);
3297 }
Chris Mason8790d502008-04-03 16:29:03 -04003298}
3299
Chris Mason8b712842008-06-11 16:50:36 -04003300struct async_sched {
3301 struct bio *bio;
3302 int rw;
3303 struct btrfs_fs_info *info;
3304 struct btrfs_work work;
3305};
3306
3307/*
3308 * see run_scheduled_bios for a description of why bios are collected for
3309 * async submit.
3310 *
3311 * This will add one bio to the pending list for a device and make sure
3312 * the work struct is scheduled.
3313 */
Chris Masond3977122009-01-05 21:25:51 -05003314static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003315 struct btrfs_device *device,
3316 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003317{
3318 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003319 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003320
3321 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003322 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003323 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003324 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003325 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003326 return 0;
3327 }
3328
3329 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003330 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003331 * higher layers. Otherwise, the async bio makes it appear we have
3332 * made progress against dirty pages when we've really just put it
3333 * on a queue for later
3334 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003335 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003336 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003337 bio->bi_next = NULL;
3338 bio->bi_rw |= rw;
3339
3340 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003341 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003342 pending_bios = &device->pending_sync_bios;
3343 else
3344 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003345
Chris Masonffbd5172009-04-20 15:50:09 -04003346 if (pending_bios->tail)
3347 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003348
Chris Masonffbd5172009-04-20 15:50:09 -04003349 pending_bios->tail = bio;
3350 if (!pending_bios->head)
3351 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003352 if (device->running_pending)
3353 should_queue = 0;
3354
3355 spin_unlock(&device->io_lock);
3356
3357 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003358 btrfs_queue_worker(&root->fs_info->submit_workers,
3359 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003360 return 0;
3361}
3362
Chris Masonf1885912008-04-09 16:28:12 -04003363int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003364 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003365{
3366 struct btrfs_mapping_tree *map_tree;
3367 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003368 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003369 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003370 u64 length = 0;
3371 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003372 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003373 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003374 int dev_nr = 0;
3375 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003376
Chris Masonf2d8d742008-04-21 10:03:05 -04003377 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003378 map_tree = &root->fs_info->mapping_tree;
3379 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003380
Chris Masonf1885912008-04-09 16:28:12 -04003381 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3382 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003383 BUG_ON(ret);
3384
3385 total_devs = multi->num_stripes;
3386 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003387 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3388 "len %llu\n", (unsigned long long)logical,
3389 (unsigned long long)length,
3390 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003391 BUG();
3392 }
3393 multi->end_io = first_bio->bi_end_io;
3394 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003395 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003396 atomic_set(&multi->stripes_pending, multi->num_stripes);
3397
Chris Masond3977122009-01-05 21:25:51 -05003398 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003399 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003400 if (dev_nr < total_devs - 1) {
3401 bio = bio_clone(first_bio, GFP_NOFS);
3402 BUG_ON(!bio);
3403 } else {
3404 bio = first_bio;
3405 }
3406 bio->bi_private = multi;
3407 bio->bi_end_io = end_bio_multi_stripe;
3408 }
Chris Masoncea9e442008-04-09 16:28:12 -04003409 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3410 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003411 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003412 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003413 if (async_submit)
3414 schedule_bio(root, dev, rw, bio);
3415 else
3416 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003417 } else {
3418 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3419 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003420 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003421 }
Chris Mason8790d502008-04-03 16:29:03 -04003422 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003423 }
Chris Masoncea9e442008-04-09 16:28:12 -04003424 if (total_devs == 1)
3425 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003426 return 0;
3427}
3428
Chris Masona4437552008-04-18 10:29:38 -04003429struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003430 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003431{
Yan Zheng2b820322008-11-17 21:11:30 -05003432 struct btrfs_device *device;
3433 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003434
Yan Zheng2b820322008-11-17 21:11:30 -05003435 cur_devices = root->fs_info->fs_devices;
3436 while (cur_devices) {
3437 if (!fsid ||
3438 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3439 device = __find_device(&cur_devices->devices,
3440 devid, uuid);
3441 if (device)
3442 return device;
3443 }
3444 cur_devices = cur_devices->seed;
3445 }
3446 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003447}
3448
Chris Masondfe25022008-05-13 13:46:40 -04003449static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3450 u64 devid, u8 *dev_uuid)
3451{
3452 struct btrfs_device *device;
3453 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3454
3455 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003456 if (!device)
3457 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003458 list_add(&device->dev_list,
3459 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003460 device->dev_root = root->fs_info->dev_root;
3461 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003462 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003463 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003464 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003465 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003466 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003467 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003468 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003469 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3470 return device;
3471}
3472
Chris Mason0b86a832008-03-24 15:01:56 -04003473static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3474 struct extent_buffer *leaf,
3475 struct btrfs_chunk *chunk)
3476{
3477 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3478 struct map_lookup *map;
3479 struct extent_map *em;
3480 u64 logical;
3481 u64 length;
3482 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003483 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003484 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003485 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003486 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003487
Chris Masone17cade2008-04-15 15:41:47 -04003488 logical = key->offset;
3489 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003490
Chris Mason890871b2009-09-02 16:24:52 -04003491 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003492 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003493 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003494
3495 /* already mapped? */
3496 if (em && em->start <= logical && em->start + em->len > logical) {
3497 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003498 return 0;
3499 } else if (em) {
3500 free_extent_map(em);
3501 }
Chris Mason0b86a832008-03-24 15:01:56 -04003502
Chris Mason0b86a832008-03-24 15:01:56 -04003503 em = alloc_extent_map(GFP_NOFS);
3504 if (!em)
3505 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003506 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3507 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003508 if (!map) {
3509 free_extent_map(em);
3510 return -ENOMEM;
3511 }
3512
3513 em->bdev = (struct block_device *)map;
3514 em->start = logical;
3515 em->len = length;
3516 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003517 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003518
Chris Mason593060d2008-03-25 16:50:33 -04003519 map->num_stripes = num_stripes;
3520 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3521 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3522 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3523 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3524 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003525 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003526 for (i = 0; i < num_stripes; i++) {
3527 map->stripes[i].physical =
3528 btrfs_stripe_offset_nr(leaf, chunk, i);
3529 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003530 read_extent_buffer(leaf, uuid, (unsigned long)
3531 btrfs_stripe_dev_uuid_nr(chunk, i),
3532 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003533 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3534 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003535 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003536 kfree(map);
3537 free_extent_map(em);
3538 return -EIO;
3539 }
Chris Masondfe25022008-05-13 13:46:40 -04003540 if (!map->stripes[i].dev) {
3541 map->stripes[i].dev =
3542 add_missing_dev(root, devid, uuid);
3543 if (!map->stripes[i].dev) {
3544 kfree(map);
3545 free_extent_map(em);
3546 return -EIO;
3547 }
3548 }
3549 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003550 }
3551
Chris Mason890871b2009-09-02 16:24:52 -04003552 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003553 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003554 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003555 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003556 free_extent_map(em);
3557
3558 return 0;
3559}
3560
3561static int fill_device_from_item(struct extent_buffer *leaf,
3562 struct btrfs_dev_item *dev_item,
3563 struct btrfs_device *device)
3564{
3565 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003566
3567 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003568 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3569 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003570 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3571 device->type = btrfs_device_type(leaf, dev_item);
3572 device->io_align = btrfs_device_io_align(leaf, dev_item);
3573 device->io_width = btrfs_device_io_width(leaf, dev_item);
3574 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003575
3576 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003577 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003578
Chris Mason0b86a832008-03-24 15:01:56 -04003579 return 0;
3580}
3581
Yan Zheng2b820322008-11-17 21:11:30 -05003582static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3583{
3584 struct btrfs_fs_devices *fs_devices;
3585 int ret;
3586
3587 mutex_lock(&uuid_mutex);
3588
3589 fs_devices = root->fs_info->fs_devices->seed;
3590 while (fs_devices) {
3591 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3592 ret = 0;
3593 goto out;
3594 }
3595 fs_devices = fs_devices->seed;
3596 }
3597
3598 fs_devices = find_fsid(fsid);
3599 if (!fs_devices) {
3600 ret = -ENOENT;
3601 goto out;
3602 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003603
3604 fs_devices = clone_fs_devices(fs_devices);
3605 if (IS_ERR(fs_devices)) {
3606 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003607 goto out;
3608 }
3609
Christoph Hellwig97288f22008-12-02 06:36:09 -05003610 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003611 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003612 if (ret)
3613 goto out;
3614
3615 if (!fs_devices->seeding) {
3616 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003617 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003618 ret = -EINVAL;
3619 goto out;
3620 }
3621
3622 fs_devices->seed = root->fs_info->fs_devices->seed;
3623 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003624out:
3625 mutex_unlock(&uuid_mutex);
3626 return ret;
3627}
3628
Chris Mason0d81ba52008-03-24 15:02:07 -04003629static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003630 struct extent_buffer *leaf,
3631 struct btrfs_dev_item *dev_item)
3632{
3633 struct btrfs_device *device;
3634 u64 devid;
3635 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003636 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003637 u8 dev_uuid[BTRFS_UUID_SIZE];
3638
Chris Mason0b86a832008-03-24 15:01:56 -04003639 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003640 read_extent_buffer(leaf, dev_uuid,
3641 (unsigned long)btrfs_device_uuid(dev_item),
3642 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003643 read_extent_buffer(leaf, fs_uuid,
3644 (unsigned long)btrfs_device_fsid(dev_item),
3645 BTRFS_UUID_SIZE);
3646
3647 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3648 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003649 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003650 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003651 }
3652
3653 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3654 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003655 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003656 return -EIO;
3657
3658 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003659 printk(KERN_WARNING "warning devid %llu missing\n",
3660 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003661 device = add_missing_dev(root, devid, dev_uuid);
3662 if (!device)
3663 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003664 } else if (!device->missing) {
3665 /*
3666 * this happens when a device that was properly setup
3667 * in the device info lists suddenly goes bad.
3668 * device->bdev is NULL, and so we have to set
3669 * device->missing to one here
3670 */
3671 root->fs_info->fs_devices->missing_devices++;
3672 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003673 }
3674 }
3675
3676 if (device->fs_devices != root->fs_info->fs_devices) {
3677 BUG_ON(device->writeable);
3678 if (device->generation !=
3679 btrfs_device_generation(leaf, dev_item))
3680 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003681 }
Chris Mason0b86a832008-03-24 15:01:56 -04003682
3683 fill_device_from_item(leaf, dev_item, device);
3684 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003685 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003686 if (device->writeable)
3687 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003688 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003689 return ret;
3690}
3691
Chris Mason0d81ba52008-03-24 15:02:07 -04003692int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3693{
3694 struct btrfs_dev_item *dev_item;
3695
3696 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3697 dev_item);
3698 return read_one_dev(root, buf, dev_item);
3699}
3700
Yan Zhenge4404d62008-12-12 10:03:26 -05003701int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003702{
3703 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003704 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003705 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003706 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003707 u8 *ptr;
3708 unsigned long sb_ptr;
3709 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003710 u32 num_stripes;
3711 u32 array_size;
3712 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003713 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003714 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003715
Yan Zhenge4404d62008-12-12 10:03:26 -05003716 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003717 BTRFS_SUPER_INFO_SIZE);
3718 if (!sb)
3719 return -ENOMEM;
3720 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003721 btrfs_set_buffer_lockdep_class(sb, 0);
3722
Chris Masona061fc82008-05-07 11:43:44 -04003723 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003724 array_size = btrfs_super_sys_array_size(super_copy);
3725
Chris Mason0b86a832008-03-24 15:01:56 -04003726 ptr = super_copy->sys_chunk_array;
3727 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3728 cur = 0;
3729
3730 while (cur < array_size) {
3731 disk_key = (struct btrfs_disk_key *)ptr;
3732 btrfs_disk_key_to_cpu(&key, disk_key);
3733
Chris Masona061fc82008-05-07 11:43:44 -04003734 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003735 sb_ptr += len;
3736 cur += len;
3737
Chris Mason0d81ba52008-03-24 15:02:07 -04003738 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003739 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003740 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003741 if (ret)
3742 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003743 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3744 len = btrfs_chunk_item_size(num_stripes);
3745 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003746 ret = -EIO;
3747 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003748 }
3749 ptr += len;
3750 sb_ptr += len;
3751 cur += len;
3752 }
Chris Masona061fc82008-05-07 11:43:44 -04003753 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003754 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003755}
3756
3757int btrfs_read_chunk_tree(struct btrfs_root *root)
3758{
3759 struct btrfs_path *path;
3760 struct extent_buffer *leaf;
3761 struct btrfs_key key;
3762 struct btrfs_key found_key;
3763 int ret;
3764 int slot;
3765
3766 root = root->fs_info->chunk_root;
3767
3768 path = btrfs_alloc_path();
3769 if (!path)
3770 return -ENOMEM;
3771
3772 /* first we search for all of the device items, and then we
3773 * read in all of the chunk items. This way we can create chunk
3774 * mappings that reference all of the devices that are afound
3775 */
3776 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3777 key.offset = 0;
3778 key.type = 0;
3779again:
3780 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003781 if (ret < 0)
3782 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003783 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003784 leaf = path->nodes[0];
3785 slot = path->slots[0];
3786 if (slot >= btrfs_header_nritems(leaf)) {
3787 ret = btrfs_next_leaf(root, path);
3788 if (ret == 0)
3789 continue;
3790 if (ret < 0)
3791 goto error;
3792 break;
3793 }
3794 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3795 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3796 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3797 break;
3798 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3799 struct btrfs_dev_item *dev_item;
3800 dev_item = btrfs_item_ptr(leaf, slot,
3801 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003802 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003803 if (ret)
3804 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003805 }
3806 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3807 struct btrfs_chunk *chunk;
3808 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3809 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003810 if (ret)
3811 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003812 }
3813 path->slots[0]++;
3814 }
3815 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3816 key.objectid = 0;
3817 btrfs_release_path(root, path);
3818 goto again;
3819 }
Chris Mason0b86a832008-03-24 15:01:56 -04003820 ret = 0;
3821error:
Yan Zheng2b820322008-11-17 21:11:30 -05003822 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003823 return ret;
3824}