blob: e7844f8a347a17fcd78a0e713c3ba8e6f05c1e65 [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);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000366 list_add_rcu(&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
Xiao Guangrong46224702011-04-20 10:08:47 +0000409 /* We have held the volume lock, it is safe to get the devices. */
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 }
432 return fs_devices;
433error:
434 free_fs_devices(fs_devices);
435 return ERR_PTR(-ENOMEM);
436}
437
Chris Masondfe25022008-05-13 13:46:40 -0400438int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
439{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500440 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400441
442 mutex_lock(&uuid_mutex);
443again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000444 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500445 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500446 if (device->in_fs_metadata)
447 continue;
448
449 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100450 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500451 device->bdev = NULL;
452 fs_devices->open_devices--;
453 }
454 if (device->writeable) {
455 list_del_init(&device->dev_alloc_list);
456 device->writeable = 0;
457 fs_devices->rw_devices--;
458 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500459 list_del_init(&device->dev_list);
460 fs_devices->num_devices--;
461 kfree(device->name);
462 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400463 }
Yan Zheng2b820322008-11-17 21:11:30 -0500464
465 if (fs_devices->seed) {
466 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500467 goto again;
468 }
469
Chris Masondfe25022008-05-13 13:46:40 -0400470 mutex_unlock(&uuid_mutex);
471 return 0;
472}
Chris Masona0af4692008-05-13 16:03:06 -0400473
Xiao Guangrong1f781602011-04-20 10:09:16 +0000474static void __free_device(struct work_struct *work)
475{
476 struct btrfs_device *device;
477
478 device = container_of(work, struct btrfs_device, rcu_work);
479
480 if (device->bdev)
481 blkdev_put(device->bdev, device->mode);
482
483 kfree(device->name);
484 kfree(device);
485}
486
487static void free_device(struct rcu_head *head)
488{
489 struct btrfs_device *device;
490
491 device = container_of(head, struct btrfs_device, rcu);
492
493 INIT_WORK(&device->rcu_work, __free_device);
494 schedule_work(&device->rcu_work);
495}
496
Yan Zheng2b820322008-11-17 21:11:30 -0500497static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400499 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500500
Yan Zheng2b820322008-11-17 21:11:30 -0500501 if (--fs_devices->opened > 0)
502 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400503
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000504 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500505 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506 struct btrfs_device *new_device;
507
508 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400509 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000510
Yan Zheng2b820322008-11-17 21:11:30 -0500511 if (device->writeable) {
512 list_del_init(&device->dev_alloc_list);
513 fs_devices->rw_devices--;
514 }
515
Xiao Guangrong1f781602011-04-20 10:09:16 +0000516 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
517 BUG_ON(!new_device);
518 memcpy(new_device, device, sizeof(*new_device));
519 new_device->name = kstrdup(device->name, GFP_NOFS);
520 BUG_ON(!new_device->name);
521 new_device->bdev = NULL;
522 new_device->writeable = 0;
523 new_device->in_fs_metadata = 0;
524 list_replace_rcu(&device->dev_list, &new_device->dev_list);
525
526 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400527 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000528 mutex_unlock(&fs_devices->device_list_mutex);
529
Yan Zhenge4404d62008-12-12 10:03:26 -0500530 WARN_ON(fs_devices->open_devices);
531 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500532 fs_devices->opened = 0;
533 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500534
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 return 0;
536}
537
Yan Zheng2b820322008-11-17 21:11:30 -0500538int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
539{
Yan Zhenge4404d62008-12-12 10:03:26 -0500540 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500541 int ret;
542
543 mutex_lock(&uuid_mutex);
544 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500545 if (!fs_devices->opened) {
546 seed_devices = fs_devices->seed;
547 fs_devices->seed = NULL;
548 }
Yan Zheng2b820322008-11-17 21:11:30 -0500549 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500550
551 while (seed_devices) {
552 fs_devices = seed_devices;
553 seed_devices = fs_devices->seed;
554 __btrfs_close_devices(fs_devices);
555 free_fs_devices(fs_devices);
556 }
Yan Zheng2b820322008-11-17 21:11:30 -0500557 return ret;
558}
559
Yan Zhenge4404d62008-12-12 10:03:26 -0500560static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
561 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400562{
563 struct block_device *bdev;
564 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400565 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400566 struct block_device *latest_bdev = NULL;
567 struct buffer_head *bh;
568 struct btrfs_super_block *disk_super;
569 u64 latest_devid = 0;
570 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400571 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500572 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400573 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574
Tejun Heod4d77622010-11-13 11:55:18 +0100575 flags |= FMODE_EXCL;
576
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500577 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400578 if (device->bdev)
579 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400580 if (!device->name)
581 continue;
582
Tejun Heod4d77622010-11-13 11:55:18 +0100583 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400584 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500585 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400586 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400587 }
Chris Masona061fc82008-05-07 11:43:44 -0400588 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400589
Yan Zhenga512bbf2008-12-08 16:46:26 -0500590 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000591 if (!bh) {
592 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400593 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000594 }
Chris Masona0af4692008-05-13 16:03:06 -0400595
596 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000597 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400598 if (devid != device->devid)
599 goto error_brelse;
600
Yan Zheng2b820322008-11-17 21:11:30 -0500601 if (memcmp(device->uuid, disk_super->dev_item.uuid,
602 BTRFS_UUID_SIZE))
603 goto error_brelse;
604
605 device->generation = btrfs_super_generation(disk_super);
606 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400607 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500608 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400609 latest_bdev = bdev;
610 }
611
Yan Zheng2b820322008-11-17 21:11:30 -0500612 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
613 device->writeable = 0;
614 } else {
615 device->writeable = !bdev_read_only(bdev);
616 seeding = 0;
617 }
618
Chris Mason8a4b83c2008-03-24 15:02:07 -0400619 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400620 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500621 device->mode = flags;
622
Chris Masonc289811c2009-06-10 09:51:32 -0400623 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
624 fs_devices->rotating = 1;
625
Chris Masona0af4692008-05-13 16:03:06 -0400626 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500627 if (device->writeable) {
628 fs_devices->rw_devices++;
629 list_add(&device->dev_alloc_list,
630 &fs_devices->alloc_list);
631 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000632 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400633 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400634
Chris Masona0af4692008-05-13 16:03:06 -0400635error_brelse:
636 brelse(bh);
637error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100638 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400639error:
640 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400641 }
Chris Masona0af4692008-05-13 16:03:06 -0400642 if (fs_devices->open_devices == 0) {
643 ret = -EIO;
644 goto out;
645 }
Yan Zheng2b820322008-11-17 21:11:30 -0500646 fs_devices->seeding = seeding;
647 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400648 fs_devices->latest_bdev = latest_bdev;
649 fs_devices->latest_devid = latest_devid;
650 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500651 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400652out:
Yan Zheng2b820322008-11-17 21:11:30 -0500653 return ret;
654}
655
656int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500657 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500658{
659 int ret;
660
661 mutex_lock(&uuid_mutex);
662 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500663 fs_devices->opened++;
664 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500665 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500666 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500667 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400668 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 return ret;
670}
671
Christoph Hellwig97288f22008-12-02 06:36:09 -0500672int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400673 struct btrfs_fs_devices **fs_devices_ret)
674{
675 struct btrfs_super_block *disk_super;
676 struct block_device *bdev;
677 struct buffer_head *bh;
678 int ret;
679 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400680 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400681
682 mutex_lock(&uuid_mutex);
683
Tejun Heod4d77622010-11-13 11:55:18 +0100684 flags |= FMODE_EXCL;
685 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400686
687 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400688 ret = PTR_ERR(bdev);
689 goto error;
690 }
691
692 ret = set_blocksize(bdev, 4096);
693 if (ret)
694 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500695 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000697 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400698 goto error_close;
699 }
700 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000701 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400702 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400703 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500704 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400705 else {
706 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500707 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400708 *(unsigned long long *)disk_super->fsid,
709 *(unsigned long long *)(disk_super->fsid + 8));
710 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500711 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500712 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
714
Chris Mason8a4b83c2008-03-24 15:02:07 -0400715 brelse(bh);
716error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100717 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400718error:
719 mutex_unlock(&uuid_mutex);
720 return ret;
721}
Chris Mason0b86a832008-03-24 15:01:56 -0400722
Miao Xie6d07bce2011-01-05 10:07:31 +0000723/* helper to account the used device space in the range */
724int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
725 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400726{
727 struct btrfs_key key;
728 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000729 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500730 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000731 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400732 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000733 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400734 struct extent_buffer *l;
735
Miao Xie6d07bce2011-01-05 10:07:31 +0000736 *length = 0;
737
738 if (start >= device->total_bytes)
739 return 0;
740
Yan Zheng2b820322008-11-17 21:11:30 -0500741 path = btrfs_alloc_path();
742 if (!path)
743 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400744 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400745
Chris Mason0b86a832008-03-24 15:01:56 -0400746 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000747 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400748 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000749
750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400751 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000752 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400753 if (ret > 0) {
754 ret = btrfs_previous_item(root, path, key.objectid, key.type);
755 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000756 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400757 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000758
Chris Mason0b86a832008-03-24 15:01:56 -0400759 while (1) {
760 l = path->nodes[0];
761 slot = path->slots[0];
762 if (slot >= btrfs_header_nritems(l)) {
763 ret = btrfs_next_leaf(root, path);
764 if (ret == 0)
765 continue;
766 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000767 goto out;
768
769 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400770 }
771 btrfs_item_key_to_cpu(l, &key, slot);
772
773 if (key.objectid < device->devid)
774 goto next;
775
776 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000777 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400778
Chris Masond3977122009-01-05 21:25:51 -0500779 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400780 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400781
Chris Mason0b86a832008-03-24 15:01:56 -0400782 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000783 extent_end = key.offset + btrfs_dev_extent_length(l,
784 dev_extent);
785 if (key.offset <= start && extent_end > end) {
786 *length = end - start + 1;
787 break;
788 } else if (key.offset <= start && extent_end > start)
789 *length += extent_end - start;
790 else if (key.offset > start && extent_end <= end)
791 *length += extent_end - key.offset;
792 else if (key.offset > start && key.offset <= end) {
793 *length += end - key.offset + 1;
794 break;
795 } else if (key.offset > end)
796 break;
797
798next:
799 path->slots[0]++;
800 }
801 ret = 0;
802out:
803 btrfs_free_path(path);
804 return ret;
805}
806
Chris Mason0b86a832008-03-24 15:01:56 -0400807/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000808 * find_free_dev_extent - find free space in the specified device
809 * @trans: transaction handler
810 * @device: the device which we search the free space in
811 * @num_bytes: the size of the free space that we need
812 * @start: store the start of the free space.
813 * @len: the size of the free space. that we find, or the size of the max
814 * free space if we don't find suitable free space
815 *
Chris Mason0b86a832008-03-24 15:01:56 -0400816 * this uses a pretty simple search, the expectation is that it is
817 * called very infrequently and that a given device has a small number
818 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000819 *
820 * @start is used to store the start of the free space if we find. But if we
821 * don't find suitable free space, it will be used to store the start position
822 * of the max free space.
823 *
824 * @len is used to store the size of the free space that we find.
825 * But if we don't find suitable free space, it is used to store the size of
826 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400827 */
828int find_free_dev_extent(struct btrfs_trans_handle *trans,
829 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000830 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400831{
832 struct btrfs_key key;
833 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000834 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400835 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000836 u64 hole_size;
837 u64 max_hole_start;
838 u64 max_hole_size;
839 u64 extent_end;
840 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400841 u64 search_end = device->total_bytes;
842 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000843 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400844 struct extent_buffer *l;
845
Chris Mason0b86a832008-03-24 15:01:56 -0400846 /* FIXME use last free of some kind */
847
848 /* we don't want to overwrite the superblock on the drive,
849 * so we make sure to start at an offset of at least 1MB
850 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000851 search_start = 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400852
Miao Xie7bfc8372011-01-05 10:07:26 +0000853 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason0b86a832008-03-24 15:01:56 -0400854 search_start = max(root->fs_info->alloc_start, search_start);
855
Miao Xie7bfc8372011-01-05 10:07:26 +0000856 max_hole_start = search_start;
857 max_hole_size = 0;
858
859 if (search_start >= search_end) {
860 ret = -ENOSPC;
861 goto error;
862 }
863
864 path = btrfs_alloc_path();
865 if (!path) {
866 ret = -ENOMEM;
867 goto error;
868 }
869 path->reada = 2;
870
Chris Mason0b86a832008-03-24 15:01:56 -0400871 key.objectid = device->devid;
872 key.offset = search_start;
873 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000874
Chris Mason0b86a832008-03-24 15:01:56 -0400875 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
876 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000877 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 if (ret > 0) {
879 ret = btrfs_previous_item(root, path, key.objectid, key.type);
880 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000881 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400882 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000883
Chris Mason0b86a832008-03-24 15:01:56 -0400884 while (1) {
885 l = path->nodes[0];
886 slot = path->slots[0];
887 if (slot >= btrfs_header_nritems(l)) {
888 ret = btrfs_next_leaf(root, path);
889 if (ret == 0)
890 continue;
891 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000892 goto out;
893
894 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400895 }
896 btrfs_item_key_to_cpu(l, &key, slot);
897
898 if (key.objectid < device->devid)
899 goto next;
900
901 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000902 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400903
Chris Mason0b86a832008-03-24 15:01:56 -0400904 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
905 goto next;
906
Miao Xie7bfc8372011-01-05 10:07:26 +0000907 if (key.offset > search_start) {
908 hole_size = key.offset - search_start;
909
910 if (hole_size > max_hole_size) {
911 max_hole_start = search_start;
912 max_hole_size = hole_size;
913 }
914
915 /*
916 * If this free space is greater than which we need,
917 * it must be the max free space that we have found
918 * until now, so max_hole_start must point to the start
919 * of this free space and the length of this free space
920 * is stored in max_hole_size. Thus, we return
921 * max_hole_start and max_hole_size and go back to the
922 * caller.
923 */
924 if (hole_size >= num_bytes) {
925 ret = 0;
926 goto out;
927 }
928 }
929
Chris Mason0b86a832008-03-24 15:01:56 -0400930 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000931 extent_end = key.offset + btrfs_dev_extent_length(l,
932 dev_extent);
933 if (extent_end > search_start)
934 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400935next:
936 path->slots[0]++;
937 cond_resched();
938 }
Chris Mason0b86a832008-03-24 15:01:56 -0400939
Miao Xie7bfc8372011-01-05 10:07:26 +0000940 hole_size = search_end- search_start;
941 if (hole_size > max_hole_size) {
942 max_hole_start = search_start;
943 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400944 }
Chris Mason0b86a832008-03-24 15:01:56 -0400945
Miao Xie7bfc8372011-01-05 10:07:26 +0000946 /* See above. */
947 if (hole_size < num_bytes)
948 ret = -ENOSPC;
949 else
950 ret = 0;
951
952out:
Yan Zheng2b820322008-11-17 21:11:30 -0500953 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000954error:
955 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000956 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000957 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400958 return ret;
959}
960
Christoph Hellwigb2950862008-12-02 09:54:17 -0500961static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400962 struct btrfs_device *device,
963 u64 start)
964{
965 int ret;
966 struct btrfs_path *path;
967 struct btrfs_root *root = device->dev_root;
968 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400969 struct btrfs_key found_key;
970 struct extent_buffer *leaf = NULL;
971 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400972
973 path = btrfs_alloc_path();
974 if (!path)
975 return -ENOMEM;
976
977 key.objectid = device->devid;
978 key.offset = start;
979 key.type = BTRFS_DEV_EXTENT_KEY;
980
981 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400982 if (ret > 0) {
983 ret = btrfs_previous_item(root, path, key.objectid,
984 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000985 if (ret)
986 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400987 leaf = path->nodes[0];
988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
989 extent = btrfs_item_ptr(leaf, path->slots[0],
990 struct btrfs_dev_extent);
991 BUG_ON(found_key.offset > start || found_key.offset +
992 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400993 } else if (ret == 0) {
994 leaf = path->nodes[0];
995 extent = btrfs_item_ptr(leaf, path->slots[0],
996 struct btrfs_dev_extent);
997 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400998 BUG_ON(ret);
999
Chris Masondfe25022008-05-13 13:46:40 -04001000 if (device->bytes_used > 0)
1001 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -04001002 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001003
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001004out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001005 btrfs_free_path(path);
1006 return ret;
1007}
1008
Yan Zheng2b820322008-11-17 21:11:30 -05001009int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001010 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001011 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001012 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001013{
1014 int ret;
1015 struct btrfs_path *path;
1016 struct btrfs_root *root = device->dev_root;
1017 struct btrfs_dev_extent *extent;
1018 struct extent_buffer *leaf;
1019 struct btrfs_key key;
1020
Chris Masondfe25022008-05-13 13:46:40 -04001021 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001022 path = btrfs_alloc_path();
1023 if (!path)
1024 return -ENOMEM;
1025
Chris Mason0b86a832008-03-24 15:01:56 -04001026 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001027 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001028 key.type = BTRFS_DEV_EXTENT_KEY;
1029 ret = btrfs_insert_empty_item(trans, root, path, &key,
1030 sizeof(*extent));
1031 BUG_ON(ret);
1032
1033 leaf = path->nodes[0];
1034 extent = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001036 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1037 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1038 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1039
1040 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1041 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1042 BTRFS_UUID_SIZE);
1043
Chris Mason0b86a832008-03-24 15:01:56 -04001044 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1045 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001046 btrfs_free_path(path);
1047 return ret;
1048}
1049
Chris Masona1b32a52008-09-05 16:09:51 -04001050static noinline int find_next_chunk(struct btrfs_root *root,
1051 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001052{
1053 struct btrfs_path *path;
1054 int ret;
1055 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001056 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001057 struct btrfs_key found_key;
1058
1059 path = btrfs_alloc_path();
1060 BUG_ON(!path);
1061
Chris Masone17cade2008-04-15 15:41:47 -04001062 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001063 key.offset = (u64)-1;
1064 key.type = BTRFS_CHUNK_ITEM_KEY;
1065
1066 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1067 if (ret < 0)
1068 goto error;
1069
1070 BUG_ON(ret == 0);
1071
1072 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1073 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001074 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001075 } else {
1076 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1077 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001078 if (found_key.objectid != objectid)
1079 *offset = 0;
1080 else {
1081 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1082 struct btrfs_chunk);
1083 *offset = found_key.offset +
1084 btrfs_chunk_length(path->nodes[0], chunk);
1085 }
Chris Mason0b86a832008-03-24 15:01:56 -04001086 }
1087 ret = 0;
1088error:
1089 btrfs_free_path(path);
1090 return ret;
1091}
1092
Yan Zheng2b820322008-11-17 21:11:30 -05001093static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001094{
1095 int ret;
1096 struct btrfs_key key;
1097 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001098 struct btrfs_path *path;
1099
1100 root = root->fs_info->chunk_root;
1101
1102 path = btrfs_alloc_path();
1103 if (!path)
1104 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001105
1106 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1107 key.type = BTRFS_DEV_ITEM_KEY;
1108 key.offset = (u64)-1;
1109
1110 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1111 if (ret < 0)
1112 goto error;
1113
1114 BUG_ON(ret == 0);
1115
1116 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1117 BTRFS_DEV_ITEM_KEY);
1118 if (ret) {
1119 *objectid = 1;
1120 } else {
1121 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1122 path->slots[0]);
1123 *objectid = found_key.offset + 1;
1124 }
1125 ret = 0;
1126error:
Yan Zheng2b820322008-11-17 21:11:30 -05001127 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001128 return ret;
1129}
1130
1131/*
1132 * the device information is stored in the chunk root
1133 * the btrfs_device struct should be fully filled in
1134 */
1135int btrfs_add_device(struct btrfs_trans_handle *trans,
1136 struct btrfs_root *root,
1137 struct btrfs_device *device)
1138{
1139 int ret;
1140 struct btrfs_path *path;
1141 struct btrfs_dev_item *dev_item;
1142 struct extent_buffer *leaf;
1143 struct btrfs_key key;
1144 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001145
1146 root = root->fs_info->chunk_root;
1147
1148 path = btrfs_alloc_path();
1149 if (!path)
1150 return -ENOMEM;
1151
Chris Mason0b86a832008-03-24 15:01:56 -04001152 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1153 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001154 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001155
1156 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001157 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001158 if (ret)
1159 goto out;
1160
1161 leaf = path->nodes[0];
1162 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1163
1164 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001165 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001166 btrfs_set_device_type(leaf, dev_item, device->type);
1167 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1168 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1169 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001170 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1171 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001172 btrfs_set_device_group(leaf, dev_item, 0);
1173 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1174 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001175 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001176
Chris Mason0b86a832008-03-24 15:01:56 -04001177 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001178 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001179 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1180 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001181 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001182
Yan Zheng2b820322008-11-17 21:11:30 -05001183 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001184out:
1185 btrfs_free_path(path);
1186 return ret;
1187}
Chris Mason8f18cf12008-04-25 16:53:30 -04001188
Chris Masona061fc82008-05-07 11:43:44 -04001189static int btrfs_rm_dev_item(struct btrfs_root *root,
1190 struct btrfs_device *device)
1191{
1192 int ret;
1193 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001194 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001195 struct btrfs_trans_handle *trans;
1196
1197 root = root->fs_info->chunk_root;
1198
1199 path = btrfs_alloc_path();
1200 if (!path)
1201 return -ENOMEM;
1202
Yan, Zhenga22285a2010-05-16 10:48:46 -04001203 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001204 if (IS_ERR(trans)) {
1205 btrfs_free_path(path);
1206 return PTR_ERR(trans);
1207 }
Chris Masona061fc82008-05-07 11:43:44 -04001208 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1209 key.type = BTRFS_DEV_ITEM_KEY;
1210 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001211 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001212
1213 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1214 if (ret < 0)
1215 goto out;
1216
1217 if (ret > 0) {
1218 ret = -ENOENT;
1219 goto out;
1220 }
1221
1222 ret = btrfs_del_item(trans, root, path);
1223 if (ret)
1224 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001225out:
1226 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001227 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001228 btrfs_commit_transaction(trans, root);
1229 return ret;
1230}
1231
1232int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1233{
1234 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001235 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001236 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001237 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001238 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001239 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001240 u64 all_avail;
1241 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001242 u64 num_devices;
1243 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001244 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001245 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001246
Chris Masona061fc82008-05-07 11:43:44 -04001247 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001248 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001249
1250 all_avail = root->fs_info->avail_data_alloc_bits |
1251 root->fs_info->avail_system_alloc_bits |
1252 root->fs_info->avail_metadata_alloc_bits;
1253
1254 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001255 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001256 printk(KERN_ERR "btrfs: unable to go below four devices "
1257 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001258 ret = -EINVAL;
1259 goto out;
1260 }
1261
1262 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001263 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001264 printk(KERN_ERR "btrfs: unable to go below two "
1265 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001266 ret = -EINVAL;
1267 goto out;
1268 }
1269
Chris Masondfe25022008-05-13 13:46:40 -04001270 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001271 struct list_head *devices;
1272 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001273
Chris Masondfe25022008-05-13 13:46:40 -04001274 device = NULL;
1275 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001276 /*
1277 * It is safe to read the devices since the volume_mutex
1278 * is held.
1279 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001280 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001281 if (tmp->in_fs_metadata && !tmp->bdev) {
1282 device = tmp;
1283 break;
1284 }
1285 }
1286 bdev = NULL;
1287 bh = NULL;
1288 disk_super = NULL;
1289 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001290 printk(KERN_ERR "btrfs: no missing devices found to "
1291 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001292 goto out;
1293 }
Chris Masondfe25022008-05-13 13:46:40 -04001294 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001295 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1296 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001297 if (IS_ERR(bdev)) {
1298 ret = PTR_ERR(bdev);
1299 goto out;
1300 }
1301
Yan Zheng2b820322008-11-17 21:11:30 -05001302 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001303 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001304 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001305 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001306 goto error_close;
1307 }
1308 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001309 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001310 dev_uuid = disk_super->dev_item.uuid;
1311 device = btrfs_find_device(root, devid, dev_uuid,
1312 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001313 if (!device) {
1314 ret = -ENOENT;
1315 goto error_brelse;
1316 }
Chris Masondfe25022008-05-13 13:46:40 -04001317 }
Yan Zheng2b820322008-11-17 21:11:30 -05001318
1319 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001320 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1321 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001322 ret = -EINVAL;
1323 goto error_brelse;
1324 }
1325
1326 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001327 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001328 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001329 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001330 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001331 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001332 }
Chris Masona061fc82008-05-07 11:43:44 -04001333
1334 ret = btrfs_shrink_device(device, 0);
1335 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001336 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001337
Chris Masona061fc82008-05-07 11:43:44 -04001338 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1339 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001340 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001341
Chris Masone5e9a522009-06-10 15:17:02 -04001342 /*
1343 * the device list mutex makes sure that we don't change
1344 * the device list while someone else is writing out all
1345 * the device supers.
1346 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001347
1348 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001349 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001350 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001351
Yan Zhenge4404d62008-12-12 10:03:26 -05001352 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001353
Chris Masoncd02dca2010-12-13 14:56:23 -05001354 if (device->missing)
1355 root->fs_info->fs_devices->missing_devices--;
1356
Yan Zheng2b820322008-11-17 21:11:30 -05001357 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1358 struct btrfs_device, dev_list);
1359 if (device->bdev == root->fs_info->sb->s_bdev)
1360 root->fs_info->sb->s_bdev = next_device->bdev;
1361 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1362 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1363
Xiao Guangrong1f781602011-04-20 10:09:16 +00001364 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001365 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001366
1367 call_rcu(&device->rcu, free_device);
1368 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001369
Yan Zheng2b820322008-11-17 21:11:30 -05001370 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1371 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1372
Xiao Guangrong1f781602011-04-20 10:09:16 +00001373 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001374 struct btrfs_fs_devices *fs_devices;
1375 fs_devices = root->fs_info->fs_devices;
1376 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001377 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001378 break;
1379 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001380 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001381 fs_devices->seed = cur_devices->seed;
1382 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001383 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001384 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001385 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001386 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001387 }
1388
1389 /*
1390 * at this point, the device is zero sized. We want to
1391 * remove it from the devices list and zero out the old super
1392 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001393 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001394 /* make sure this device isn't detected as part of
1395 * the FS anymore
1396 */
1397 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1398 set_buffer_dirty(bh);
1399 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001400 }
Chris Masona061fc82008-05-07 11:43:44 -04001401
Chris Masona061fc82008-05-07 11:43:44 -04001402 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001403
1404error_brelse:
1405 brelse(bh);
1406error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001407 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001408 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001409out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001410 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001411 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001412 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001413error_undo:
1414 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001415 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001416 list_add(&device->dev_alloc_list,
1417 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001418 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001419 root->fs_info->fs_devices->rw_devices++;
1420 }
1421 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001422}
1423
Yan Zheng2b820322008-11-17 21:11:30 -05001424/*
1425 * does all the dirty work required for changing file system's UUID.
1426 */
1427static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1428 struct btrfs_root *root)
1429{
1430 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1431 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001432 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001433 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1434 struct btrfs_device *device;
1435 u64 super_flags;
1436
1437 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001438 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001439 return -EINVAL;
1440
Yan Zhenge4404d62008-12-12 10:03:26 -05001441 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1442 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001443 return -ENOMEM;
1444
Yan Zhenge4404d62008-12-12 10:03:26 -05001445 old_devices = clone_fs_devices(fs_devices);
1446 if (IS_ERR(old_devices)) {
1447 kfree(seed_devices);
1448 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001449 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001450
Yan Zheng2b820322008-11-17 21:11:30 -05001451 list_add(&old_devices->list, &fs_uuids);
1452
Yan Zhenge4404d62008-12-12 10:03:26 -05001453 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1454 seed_devices->opened = 1;
1455 INIT_LIST_HEAD(&seed_devices->devices);
1456 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001457 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001458
1459 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001460 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1461 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001462 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1463
Yan Zhenge4404d62008-12-12 10:03:26 -05001464 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1465 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1466 device->fs_devices = seed_devices;
1467 }
1468
Yan Zheng2b820322008-11-17 21:11:30 -05001469 fs_devices->seeding = 0;
1470 fs_devices->num_devices = 0;
1471 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001472 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001473
1474 generate_random_uuid(fs_devices->fsid);
1475 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1476 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1477 super_flags = btrfs_super_flags(disk_super) &
1478 ~BTRFS_SUPER_FLAG_SEEDING;
1479 btrfs_set_super_flags(disk_super, super_flags);
1480
1481 return 0;
1482}
1483
1484/*
1485 * strore the expected generation for seed devices in device items.
1486 */
1487static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1488 struct btrfs_root *root)
1489{
1490 struct btrfs_path *path;
1491 struct extent_buffer *leaf;
1492 struct btrfs_dev_item *dev_item;
1493 struct btrfs_device *device;
1494 struct btrfs_key key;
1495 u8 fs_uuid[BTRFS_UUID_SIZE];
1496 u8 dev_uuid[BTRFS_UUID_SIZE];
1497 u64 devid;
1498 int ret;
1499
1500 path = btrfs_alloc_path();
1501 if (!path)
1502 return -ENOMEM;
1503
1504 root = root->fs_info->chunk_root;
1505 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1506 key.offset = 0;
1507 key.type = BTRFS_DEV_ITEM_KEY;
1508
1509 while (1) {
1510 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1511 if (ret < 0)
1512 goto error;
1513
1514 leaf = path->nodes[0];
1515next_slot:
1516 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1517 ret = btrfs_next_leaf(root, path);
1518 if (ret > 0)
1519 break;
1520 if (ret < 0)
1521 goto error;
1522 leaf = path->nodes[0];
1523 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1524 btrfs_release_path(root, path);
1525 continue;
1526 }
1527
1528 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1529 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1530 key.type != BTRFS_DEV_ITEM_KEY)
1531 break;
1532
1533 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1534 struct btrfs_dev_item);
1535 devid = btrfs_device_id(leaf, dev_item);
1536 read_extent_buffer(leaf, dev_uuid,
1537 (unsigned long)btrfs_device_uuid(dev_item),
1538 BTRFS_UUID_SIZE);
1539 read_extent_buffer(leaf, fs_uuid,
1540 (unsigned long)btrfs_device_fsid(dev_item),
1541 BTRFS_UUID_SIZE);
1542 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1543 BUG_ON(!device);
1544
1545 if (device->fs_devices->seeding) {
1546 btrfs_set_device_generation(leaf, dev_item,
1547 device->generation);
1548 btrfs_mark_buffer_dirty(leaf);
1549 }
1550
1551 path->slots[0]++;
1552 goto next_slot;
1553 }
1554 ret = 0;
1555error:
1556 btrfs_free_path(path);
1557 return ret;
1558}
1559
Chris Mason788f20e2008-04-28 15:29:42 -04001560int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1561{
1562 struct btrfs_trans_handle *trans;
1563 struct btrfs_device *device;
1564 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001565 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001566 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001567 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001568 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001569 int ret = 0;
1570
Yan Zheng2b820322008-11-17 21:11:30 -05001571 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1572 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001573
Tejun Heod4d77622010-11-13 11:55:18 +01001574 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1575 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001576 if (IS_ERR(bdev))
1577 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001578
Yan Zheng2b820322008-11-17 21:11:30 -05001579 if (root->fs_info->fs_devices->seeding) {
1580 seeding_dev = 1;
1581 down_write(&sb->s_umount);
1582 mutex_lock(&uuid_mutex);
1583 }
1584
Chris Mason8c8bee12008-09-29 11:19:10 -04001585 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001586 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001587
Chris Mason788f20e2008-04-28 15:29:42 -04001588 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001589 /*
1590 * we have the volume lock, so we don't need the extra
1591 * device list mutex while reading the list here.
1592 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001593 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001594 if (device->bdev == bdev) {
1595 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001596 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001597 }
1598 }
1599
1600 device = kzalloc(sizeof(*device), GFP_NOFS);
1601 if (!device) {
1602 /* we can safely leave the fs_devices entry around */
1603 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001604 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001605 }
1606
Chris Mason788f20e2008-04-28 15:29:42 -04001607 device->name = kstrdup(device_path, GFP_NOFS);
1608 if (!device->name) {
1609 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001610 ret = -ENOMEM;
1611 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001612 }
Yan Zheng2b820322008-11-17 21:11:30 -05001613
1614 ret = find_next_devid(root, &device->devid);
1615 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001616 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001617 kfree(device);
1618 goto error;
1619 }
1620
Yan, Zhenga22285a2010-05-16 10:48:46 -04001621 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001622 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001623 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001624 kfree(device);
1625 ret = PTR_ERR(trans);
1626 goto error;
1627 }
1628
Yan Zheng2b820322008-11-17 21:11:30 -05001629 lock_chunks(root);
1630
Yan Zheng2b820322008-11-17 21:11:30 -05001631 device->writeable = 1;
1632 device->work.func = pending_bios_fn;
1633 generate_random_uuid(device->uuid);
1634 spin_lock_init(&device->io_lock);
1635 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001636 device->io_width = root->sectorsize;
1637 device->io_align = root->sectorsize;
1638 device->sector_size = root->sectorsize;
1639 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001640 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001641 device->dev_root = root->fs_info->dev_root;
1642 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001643 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001644 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001645 set_blocksize(device->bdev, 4096);
1646
Yan Zheng2b820322008-11-17 21:11:30 -05001647 if (seeding_dev) {
1648 sb->s_flags &= ~MS_RDONLY;
1649 ret = btrfs_prepare_sprout(trans, root);
1650 BUG_ON(ret);
1651 }
1652
1653 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001654
1655 /*
1656 * we don't want write_supers to jump in here with our device
1657 * half setup
1658 */
1659 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001660 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001661 list_add(&device->dev_alloc_list,
1662 &root->fs_info->fs_devices->alloc_list);
1663 root->fs_info->fs_devices->num_devices++;
1664 root->fs_info->fs_devices->open_devices++;
1665 root->fs_info->fs_devices->rw_devices++;
1666 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1667
Chris Masonc289811c2009-06-10 09:51:32 -04001668 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1669 root->fs_info->fs_devices->rotating = 1;
1670
Chris Mason788f20e2008-04-28 15:29:42 -04001671 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1672 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1673 total_bytes + device->total_bytes);
1674
1675 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1676 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1677 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001678 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001679
Yan Zheng2b820322008-11-17 21:11:30 -05001680 if (seeding_dev) {
1681 ret = init_first_rw_device(trans, root, device);
1682 BUG_ON(ret);
1683 ret = btrfs_finish_sprout(trans, root);
1684 BUG_ON(ret);
1685 } else {
1686 ret = btrfs_add_device(trans, root, device);
1687 }
1688
Chris Mason913d9522009-03-10 13:17:18 -04001689 /*
1690 * we've got more storage, clear any full flags on the space
1691 * infos
1692 */
1693 btrfs_clear_space_info_full(root->fs_info);
1694
Chris Mason7d9eb122008-07-08 14:19:17 -04001695 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001696 btrfs_commit_transaction(trans, root);
1697
1698 if (seeding_dev) {
1699 mutex_unlock(&uuid_mutex);
1700 up_write(&sb->s_umount);
1701
1702 ret = btrfs_relocate_sys_chunks(root);
1703 BUG_ON(ret);
1704 }
1705out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001706 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001707 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001708error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001709 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001710 if (seeding_dev) {
1711 mutex_unlock(&uuid_mutex);
1712 up_write(&sb->s_umount);
1713 }
Chris Mason788f20e2008-04-28 15:29:42 -04001714 goto out;
1715}
1716
Chris Masond3977122009-01-05 21:25:51 -05001717static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1718 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001719{
1720 int ret;
1721 struct btrfs_path *path;
1722 struct btrfs_root *root;
1723 struct btrfs_dev_item *dev_item;
1724 struct extent_buffer *leaf;
1725 struct btrfs_key key;
1726
1727 root = device->dev_root->fs_info->chunk_root;
1728
1729 path = btrfs_alloc_path();
1730 if (!path)
1731 return -ENOMEM;
1732
1733 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1734 key.type = BTRFS_DEV_ITEM_KEY;
1735 key.offset = device->devid;
1736
1737 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1738 if (ret < 0)
1739 goto out;
1740
1741 if (ret > 0) {
1742 ret = -ENOENT;
1743 goto out;
1744 }
1745
1746 leaf = path->nodes[0];
1747 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1748
1749 btrfs_set_device_id(leaf, dev_item, device->devid);
1750 btrfs_set_device_type(leaf, dev_item, device->type);
1751 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1752 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1753 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001754 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001755 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1756 btrfs_mark_buffer_dirty(leaf);
1757
1758out:
1759 btrfs_free_path(path);
1760 return ret;
1761}
1762
Chris Mason7d9eb122008-07-08 14:19:17 -04001763static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001764 struct btrfs_device *device, u64 new_size)
1765{
1766 struct btrfs_super_block *super_copy =
1767 &device->dev_root->fs_info->super_copy;
1768 u64 old_total = btrfs_super_total_bytes(super_copy);
1769 u64 diff = new_size - device->total_bytes;
1770
Yan Zheng2b820322008-11-17 21:11:30 -05001771 if (!device->writeable)
1772 return -EACCES;
1773 if (new_size <= device->total_bytes)
1774 return -EINVAL;
1775
Chris Mason8f18cf12008-04-25 16:53:30 -04001776 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001777 device->fs_devices->total_rw_bytes += diff;
1778
1779 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001780 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001781 btrfs_clear_space_info_full(device->dev_root->fs_info);
1782
Chris Mason8f18cf12008-04-25 16:53:30 -04001783 return btrfs_update_device(trans, device);
1784}
1785
Chris Mason7d9eb122008-07-08 14:19:17 -04001786int btrfs_grow_device(struct btrfs_trans_handle *trans,
1787 struct btrfs_device *device, u64 new_size)
1788{
1789 int ret;
1790 lock_chunks(device->dev_root);
1791 ret = __btrfs_grow_device(trans, device, new_size);
1792 unlock_chunks(device->dev_root);
1793 return ret;
1794}
1795
Chris Mason8f18cf12008-04-25 16:53:30 -04001796static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1797 struct btrfs_root *root,
1798 u64 chunk_tree, u64 chunk_objectid,
1799 u64 chunk_offset)
1800{
1801 int ret;
1802 struct btrfs_path *path;
1803 struct btrfs_key key;
1804
1805 root = root->fs_info->chunk_root;
1806 path = btrfs_alloc_path();
1807 if (!path)
1808 return -ENOMEM;
1809
1810 key.objectid = chunk_objectid;
1811 key.offset = chunk_offset;
1812 key.type = BTRFS_CHUNK_ITEM_KEY;
1813
1814 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1815 BUG_ON(ret);
1816
1817 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001818
1819 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001820 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001821}
1822
Christoph Hellwigb2950862008-12-02 09:54:17 -05001823static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001824 chunk_offset)
1825{
1826 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1827 struct btrfs_disk_key *disk_key;
1828 struct btrfs_chunk *chunk;
1829 u8 *ptr;
1830 int ret = 0;
1831 u32 num_stripes;
1832 u32 array_size;
1833 u32 len = 0;
1834 u32 cur;
1835 struct btrfs_key key;
1836
1837 array_size = btrfs_super_sys_array_size(super_copy);
1838
1839 ptr = super_copy->sys_chunk_array;
1840 cur = 0;
1841
1842 while (cur < array_size) {
1843 disk_key = (struct btrfs_disk_key *)ptr;
1844 btrfs_disk_key_to_cpu(&key, disk_key);
1845
1846 len = sizeof(*disk_key);
1847
1848 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1849 chunk = (struct btrfs_chunk *)(ptr + len);
1850 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1851 len += btrfs_chunk_item_size(num_stripes);
1852 } else {
1853 ret = -EIO;
1854 break;
1855 }
1856 if (key.objectid == chunk_objectid &&
1857 key.offset == chunk_offset) {
1858 memmove(ptr, ptr + len, array_size - (cur + len));
1859 array_size -= len;
1860 btrfs_set_super_sys_array_size(super_copy, array_size);
1861 } else {
1862 ptr += len;
1863 cur += len;
1864 }
1865 }
1866 return ret;
1867}
1868
Christoph Hellwigb2950862008-12-02 09:54:17 -05001869static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001870 u64 chunk_tree, u64 chunk_objectid,
1871 u64 chunk_offset)
1872{
1873 struct extent_map_tree *em_tree;
1874 struct btrfs_root *extent_root;
1875 struct btrfs_trans_handle *trans;
1876 struct extent_map *em;
1877 struct map_lookup *map;
1878 int ret;
1879 int i;
1880
1881 root = root->fs_info->chunk_root;
1882 extent_root = root->fs_info->extent_root;
1883 em_tree = &root->fs_info->mapping_tree.map_tree;
1884
Josef Bacikba1bf482009-09-11 16:11:19 -04001885 ret = btrfs_can_relocate(extent_root, chunk_offset);
1886 if (ret)
1887 return -ENOSPC;
1888
Chris Mason8f18cf12008-04-25 16:53:30 -04001889 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001890 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001891 if (ret)
1892 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001893
Yan, Zhenga22285a2010-05-16 10:48:46 -04001894 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001895 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001896
Chris Mason7d9eb122008-07-08 14:19:17 -04001897 lock_chunks(root);
1898
Chris Mason8f18cf12008-04-25 16:53:30 -04001899 /*
1900 * step two, delete the device extents and the
1901 * chunk tree entries
1902 */
Chris Mason890871b2009-09-02 16:24:52 -04001903 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001904 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001905 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001906
Chris Masona061fc82008-05-07 11:43:44 -04001907 BUG_ON(em->start > chunk_offset ||
1908 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001909 map = (struct map_lookup *)em->bdev;
1910
1911 for (i = 0; i < map->num_stripes; i++) {
1912 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1913 map->stripes[i].physical);
1914 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001915
Chris Masondfe25022008-05-13 13:46:40 -04001916 if (map->stripes[i].dev) {
1917 ret = btrfs_update_device(trans, map->stripes[i].dev);
1918 BUG_ON(ret);
1919 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001920 }
1921 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1922 chunk_offset);
1923
1924 BUG_ON(ret);
1925
liubo1abe9b82011-03-24 11:18:59 +00001926 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1927
Chris Mason8f18cf12008-04-25 16:53:30 -04001928 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1929 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1930 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001931 }
1932
Zheng Yan1a40e232008-09-26 10:09:34 -04001933 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1934 BUG_ON(ret);
1935
Chris Mason890871b2009-09-02 16:24:52 -04001936 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001937 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001938 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001939
Chris Mason8f18cf12008-04-25 16:53:30 -04001940 kfree(map);
1941 em->bdev = NULL;
1942
1943 /* once for the tree */
1944 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001945 /* once for us */
1946 free_extent_map(em);
1947
Chris Mason7d9eb122008-07-08 14:19:17 -04001948 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001949 btrfs_end_transaction(trans, root);
1950 return 0;
1951}
1952
Yan Zheng2b820322008-11-17 21:11:30 -05001953static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1954{
1955 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1956 struct btrfs_path *path;
1957 struct extent_buffer *leaf;
1958 struct btrfs_chunk *chunk;
1959 struct btrfs_key key;
1960 struct btrfs_key found_key;
1961 u64 chunk_tree = chunk_root->root_key.objectid;
1962 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001963 bool retried = false;
1964 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001965 int ret;
1966
1967 path = btrfs_alloc_path();
1968 if (!path)
1969 return -ENOMEM;
1970
Josef Bacikba1bf482009-09-11 16:11:19 -04001971again:
Yan Zheng2b820322008-11-17 21:11:30 -05001972 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1973 key.offset = (u64)-1;
1974 key.type = BTRFS_CHUNK_ITEM_KEY;
1975
1976 while (1) {
1977 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1978 if (ret < 0)
1979 goto error;
1980 BUG_ON(ret == 0);
1981
1982 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1983 key.type);
1984 if (ret < 0)
1985 goto error;
1986 if (ret > 0)
1987 break;
1988
1989 leaf = path->nodes[0];
1990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1991
1992 chunk = btrfs_item_ptr(leaf, path->slots[0],
1993 struct btrfs_chunk);
1994 chunk_type = btrfs_chunk_type(leaf, chunk);
1995 btrfs_release_path(chunk_root, path);
1996
1997 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1998 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1999 found_key.objectid,
2000 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002001 if (ret == -ENOSPC)
2002 failed++;
2003 else if (ret)
2004 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002005 }
2006
2007 if (found_key.offset == 0)
2008 break;
2009 key.offset = found_key.offset - 1;
2010 }
2011 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002012 if (failed && !retried) {
2013 failed = 0;
2014 retried = true;
2015 goto again;
2016 } else if (failed && retried) {
2017 WARN_ON(1);
2018 ret = -ENOSPC;
2019 }
Yan Zheng2b820322008-11-17 21:11:30 -05002020error:
2021 btrfs_free_path(path);
2022 return ret;
2023}
2024
Chris Masonec44a352008-04-28 15:29:52 -04002025static u64 div_factor(u64 num, int factor)
2026{
2027 if (factor == 10)
2028 return num;
2029 num *= factor;
2030 do_div(num, 10);
2031 return num;
2032}
2033
Chris Masonec44a352008-04-28 15:29:52 -04002034int btrfs_balance(struct btrfs_root *dev_root)
2035{
2036 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002037 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2038 struct btrfs_device *device;
2039 u64 old_size;
2040 u64 size_to_free;
2041 struct btrfs_path *path;
2042 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002043 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2044 struct btrfs_trans_handle *trans;
2045 struct btrfs_key found_key;
2046
Yan Zheng2b820322008-11-17 21:11:30 -05002047 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2048 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002049
Ben Hutchings6f88a442010-12-29 14:55:03 +00002050 if (!capable(CAP_SYS_ADMIN))
2051 return -EPERM;
2052
Chris Mason7d9eb122008-07-08 14:19:17 -04002053 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002054 dev_root = dev_root->fs_info->dev_root;
2055
Chris Masonec44a352008-04-28 15:29:52 -04002056 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002057 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002058 old_size = device->total_bytes;
2059 size_to_free = div_factor(old_size, 1);
2060 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002061 if (!device->writeable ||
2062 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002063 continue;
2064
2065 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002066 if (ret == -ENOSPC)
2067 break;
Chris Masonec44a352008-04-28 15:29:52 -04002068 BUG_ON(ret);
2069
Yan, Zhenga22285a2010-05-16 10:48:46 -04002070 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002071 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002072
2073 ret = btrfs_grow_device(trans, device, old_size);
2074 BUG_ON(ret);
2075
2076 btrfs_end_transaction(trans, dev_root);
2077 }
2078
2079 /* step two, relocate all the chunks */
2080 path = btrfs_alloc_path();
2081 BUG_ON(!path);
2082
2083 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2084 key.offset = (u64)-1;
2085 key.type = BTRFS_CHUNK_ITEM_KEY;
2086
Chris Masond3977122009-01-05 21:25:51 -05002087 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002088 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2089 if (ret < 0)
2090 goto error;
2091
2092 /*
2093 * this shouldn't happen, it means the last relocate
2094 * failed
2095 */
2096 if (ret == 0)
2097 break;
2098
2099 ret = btrfs_previous_item(chunk_root, path, 0,
2100 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002101 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002102 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002103
Chris Masonec44a352008-04-28 15:29:52 -04002104 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2105 path->slots[0]);
2106 if (found_key.objectid != key.objectid)
2107 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002108
Chris Masonec44a352008-04-28 15:29:52 -04002109 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002110 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002111 break;
2112
Chris Mason7d9eb122008-07-08 14:19:17 -04002113 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002114 ret = btrfs_relocate_chunk(chunk_root,
2115 chunk_root->root_key.objectid,
2116 found_key.objectid,
2117 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002118 BUG_ON(ret && ret != -ENOSPC);
2119 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002120 }
2121 ret = 0;
2122error:
2123 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002124 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002125 return ret;
2126}
2127
Chris Mason8f18cf12008-04-25 16:53:30 -04002128/*
2129 * shrinking a device means finding all of the device extents past
2130 * the new size, and then following the back refs to the chunks.
2131 * The chunk relocation code actually frees the device extent
2132 */
2133int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2134{
2135 struct btrfs_trans_handle *trans;
2136 struct btrfs_root *root = device->dev_root;
2137 struct btrfs_dev_extent *dev_extent = NULL;
2138 struct btrfs_path *path;
2139 u64 length;
2140 u64 chunk_tree;
2141 u64 chunk_objectid;
2142 u64 chunk_offset;
2143 int ret;
2144 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002145 int failed = 0;
2146 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002147 struct extent_buffer *l;
2148 struct btrfs_key key;
2149 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2150 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002151 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002152 u64 diff = device->total_bytes - new_size;
2153
Yan Zheng2b820322008-11-17 21:11:30 -05002154 if (new_size >= device->total_bytes)
2155 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002156
2157 path = btrfs_alloc_path();
2158 if (!path)
2159 return -ENOMEM;
2160
Chris Mason8f18cf12008-04-25 16:53:30 -04002161 path->reada = 2;
2162
Chris Mason7d9eb122008-07-08 14:19:17 -04002163 lock_chunks(root);
2164
Chris Mason8f18cf12008-04-25 16:53:30 -04002165 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002166 if (device->writeable)
2167 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002168 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002169
Josef Bacikba1bf482009-09-11 16:11:19 -04002170again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002171 key.objectid = device->devid;
2172 key.offset = (u64)-1;
2173 key.type = BTRFS_DEV_EXTENT_KEY;
2174
2175 while (1) {
2176 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2177 if (ret < 0)
2178 goto done;
2179
2180 ret = btrfs_previous_item(root, path, 0, key.type);
2181 if (ret < 0)
2182 goto done;
2183 if (ret) {
2184 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002185 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002186 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002187 }
2188
2189 l = path->nodes[0];
2190 slot = path->slots[0];
2191 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2192
Josef Bacikba1bf482009-09-11 16:11:19 -04002193 if (key.objectid != device->devid) {
2194 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002195 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002196 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002197
2198 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2199 length = btrfs_dev_extent_length(l, dev_extent);
2200
Josef Bacikba1bf482009-09-11 16:11:19 -04002201 if (key.offset + length <= new_size) {
2202 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002203 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002204 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002205
2206 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2207 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2208 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2209 btrfs_release_path(root, path);
2210
2211 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2212 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002213 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002214 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002215 if (ret == -ENOSPC)
2216 failed++;
2217 key.offset -= 1;
2218 }
2219
2220 if (failed && !retried) {
2221 failed = 0;
2222 retried = true;
2223 goto again;
2224 } else if (failed && retried) {
2225 ret = -ENOSPC;
2226 lock_chunks(root);
2227
2228 device->total_bytes = old_size;
2229 if (device->writeable)
2230 device->fs_devices->total_rw_bytes += diff;
2231 unlock_chunks(root);
2232 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002233 }
2234
Chris Balld6397ba2009-04-27 07:29:03 -04002235 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002236 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002237 if (IS_ERR(trans)) {
2238 ret = PTR_ERR(trans);
2239 goto done;
2240 }
2241
Chris Balld6397ba2009-04-27 07:29:03 -04002242 lock_chunks(root);
2243
2244 device->disk_total_bytes = new_size;
2245 /* Now btrfs_update_device() will change the on-disk size. */
2246 ret = btrfs_update_device(trans, device);
2247 if (ret) {
2248 unlock_chunks(root);
2249 btrfs_end_transaction(trans, root);
2250 goto done;
2251 }
2252 WARN_ON(diff > old_total);
2253 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2254 unlock_chunks(root);
2255 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002256done:
2257 btrfs_free_path(path);
2258 return ret;
2259}
2260
Christoph Hellwigb2950862008-12-02 09:54:17 -05002261static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002262 struct btrfs_root *root,
2263 struct btrfs_key *key,
2264 struct btrfs_chunk *chunk, int item_size)
2265{
2266 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2267 struct btrfs_disk_key disk_key;
2268 u32 array_size;
2269 u8 *ptr;
2270
2271 array_size = btrfs_super_sys_array_size(super_copy);
2272 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2273 return -EFBIG;
2274
2275 ptr = super_copy->sys_chunk_array + array_size;
2276 btrfs_cpu_key_to_disk(&disk_key, key);
2277 memcpy(ptr, &disk_key, sizeof(disk_key));
2278 ptr += sizeof(disk_key);
2279 memcpy(ptr, chunk, item_size);
2280 item_size += sizeof(disk_key);
2281 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2282 return 0;
2283}
2284
Chris Masond3977122009-01-05 21:25:51 -05002285static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002286 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002287{
2288 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2289 return calc_size;
2290 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2291 return calc_size * (num_stripes / sub_stripes);
2292 else
2293 return calc_size * num_stripes;
2294}
2295
Miao Xieb2117a32011-01-05 10:07:28 +00002296/* Used to sort the devices by max_avail(descending sort) */
2297int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
Chris Mason0b86a832008-03-24 15:01:56 -04002298{
Miao Xieb2117a32011-01-05 10:07:28 +00002299 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2300 ((struct btrfs_device_info *)dev_info2)->max_avail)
2301 return -1;
2302 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2303 ((struct btrfs_device_info *)dev_info2)->max_avail)
2304 return 1;
2305 else
2306 return 0;
2307}
Chris Mason0b86a832008-03-24 15:01:56 -04002308
Miao Xieb2117a32011-01-05 10:07:28 +00002309static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2310 int *num_stripes, int *min_stripes,
2311 int *sub_stripes)
2312{
2313 *num_stripes = 1;
2314 *min_stripes = 1;
2315 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002316
Chris Masona40a90a2008-04-18 11:55:51 -04002317 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002318 *num_stripes = fs_devices->rw_devices;
2319 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002320 }
2321 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002322 *num_stripes = 2;
2323 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002324 }
Chris Mason8790d502008-04-03 16:29:03 -04002325 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002326 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002327 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002328 *num_stripes = 2;
2329 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002330 }
Chris Mason321aecc2008-04-16 10:49:51 -04002331 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002332 *num_stripes = fs_devices->rw_devices;
2333 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002334 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002335 *num_stripes &= ~(u32)1;
2336 *sub_stripes = 2;
2337 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002338 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002339
Miao Xieb2117a32011-01-05 10:07:28 +00002340 return 0;
2341}
2342
2343static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2344 u64 proposed_size, u64 type,
2345 int num_stripes, int small_stripe)
2346{
2347 int min_stripe_size = 1 * 1024 * 1024;
2348 u64 calc_size = proposed_size;
2349 u64 max_chunk_size = calc_size;
2350 int ncopies = 1;
2351
2352 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2353 BTRFS_BLOCK_GROUP_DUP |
2354 BTRFS_BLOCK_GROUP_RAID10))
2355 ncopies = 2;
2356
Chris Mason9b3f68b2008-04-18 10:29:51 -04002357 if (type & BTRFS_BLOCK_GROUP_DATA) {
2358 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002359 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002360 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002361 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002362 min_stripe_size = 32 * 1024 * 1024;
2363 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2364 calc_size = 8 * 1024 * 1024;
2365 max_chunk_size = calc_size * 2;
2366 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002367 }
2368
Yan Zheng2b820322008-11-17 21:11:30 -05002369 /* we don't want a chunk larger than 10% of writeable space */
2370 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2371 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002372
Miao Xie1974a3b2011-01-05 10:07:24 +00002373 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2374 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002375 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002376 do_div(calc_size, BTRFS_STRIPE_LEN);
2377 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002378 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002379
Chris Mason9b3f68b2008-04-18 10:29:51 -04002380 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002381 if (!small_stripe)
Josef Bacik0cad8a112010-03-17 20:45:56 +00002382 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002383
Chris Mason9f680ce2010-04-06 09:37:47 -04002384 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002385 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002386 * we end up with something bigger than a stripe
2387 */
Miao Xieb2117a32011-01-05 10:07:28 +00002388 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002389
Miao Xieb2117a32011-01-05 10:07:28 +00002390 do_div(calc_size, BTRFS_STRIPE_LEN);
2391 calc_size *= BTRFS_STRIPE_LEN;
2392
2393 return calc_size;
2394}
2395
2396static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2397 int num_stripes)
2398{
2399 struct map_lookup *new;
2400 size_t len = map_lookup_size(num_stripes);
2401
2402 BUG_ON(map->num_stripes < num_stripes);
2403
2404 if (map->num_stripes == num_stripes)
2405 return map;
2406
2407 new = kmalloc(len, GFP_NOFS);
2408 if (!new) {
2409 /* just change map->num_stripes */
2410 map->num_stripes = num_stripes;
2411 return map;
2412 }
2413
2414 memcpy(new, map, len);
2415 new->num_stripes = num_stripes;
2416 kfree(map);
2417 return new;
2418}
2419
2420/*
2421 * helper to allocate device space from btrfs_device_info, in which we stored
2422 * max free space information of every device. It is used when we can not
2423 * allocate chunks by default size.
2424 *
2425 * By this helper, we can allocate a new chunk as larger as possible.
2426 */
2427static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2428 struct btrfs_fs_devices *fs_devices,
2429 struct btrfs_device_info *devices,
2430 int nr_device, u64 type,
2431 struct map_lookup **map_lookup,
2432 int min_stripes, u64 *stripe_size)
2433{
2434 int i, index, sort_again = 0;
2435 int min_devices = min_stripes;
2436 u64 max_avail, min_free;
2437 struct map_lookup *map = *map_lookup;
2438 int ret;
2439
2440 if (nr_device < min_stripes)
2441 return -ENOSPC;
2442
2443 btrfs_descending_sort_devices(devices, nr_device);
2444
2445 max_avail = devices[0].max_avail;
2446 if (!max_avail)
2447 return -ENOSPC;
2448
2449 for (i = 0; i < nr_device; i++) {
2450 /*
2451 * if dev_offset = 0, it means the free space of this device
2452 * is less than what we need, and we didn't search max avail
2453 * extent on this device, so do it now.
2454 */
2455 if (!devices[i].dev_offset) {
2456 ret = find_free_dev_extent(trans, devices[i].dev,
2457 max_avail,
2458 &devices[i].dev_offset,
2459 &devices[i].max_avail);
2460 if (ret != 0 && ret != -ENOSPC)
2461 return ret;
2462 sort_again = 1;
2463 }
2464 }
2465
2466 /* we update the max avail free extent of each devices, sort again */
2467 if (sort_again)
2468 btrfs_descending_sort_devices(devices, nr_device);
2469
2470 if (type & BTRFS_BLOCK_GROUP_DUP)
2471 min_devices = 1;
2472
2473 if (!devices[min_devices - 1].max_avail)
2474 return -ENOSPC;
2475
2476 max_avail = devices[min_devices - 1].max_avail;
2477 if (type & BTRFS_BLOCK_GROUP_DUP)
2478 do_div(max_avail, 2);
2479
2480 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2481 min_stripes, 1);
2482 if (type & BTRFS_BLOCK_GROUP_DUP)
2483 min_free = max_avail * 2;
2484 else
2485 min_free = max_avail;
2486
2487 if (min_free > devices[min_devices - 1].max_avail)
2488 return -ENOSPC;
2489
2490 map = __shrink_map_lookup_stripes(map, min_stripes);
2491 *stripe_size = max_avail;
2492
2493 index = 0;
2494 for (i = 0; i < min_stripes; i++) {
2495 map->stripes[i].dev = devices[index].dev;
2496 map->stripes[i].physical = devices[index].dev_offset;
2497 if (type & BTRFS_BLOCK_GROUP_DUP) {
2498 i++;
2499 map->stripes[i].dev = devices[index].dev;
2500 map->stripes[i].physical = devices[index].dev_offset +
2501 max_avail;
2502 }
2503 index++;
2504 }
2505 *map_lookup = map;
2506
2507 return 0;
2508}
2509
2510static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2511 struct btrfs_root *extent_root,
2512 struct map_lookup **map_ret,
2513 u64 *num_bytes, u64 *stripe_size,
2514 u64 start, u64 type)
2515{
2516 struct btrfs_fs_info *info = extent_root->fs_info;
2517 struct btrfs_device *device = NULL;
2518 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2519 struct list_head *cur;
2520 struct map_lookup *map;
2521 struct extent_map_tree *em_tree;
2522 struct extent_map *em;
2523 struct btrfs_device_info *devices_info;
2524 struct list_head private_devs;
2525 u64 calc_size = 1024 * 1024 * 1024;
2526 u64 min_free;
2527 u64 avail;
2528 u64 dev_offset;
2529 int num_stripes;
2530 int min_stripes;
2531 int sub_stripes;
2532 int min_devices; /* the min number of devices we need */
2533 int i;
2534 int ret;
2535 int index;
2536
2537 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2538 (type & BTRFS_BLOCK_GROUP_DUP)) {
2539 WARN_ON(1);
2540 type &= ~BTRFS_BLOCK_GROUP_DUP;
2541 }
2542 if (list_empty(&fs_devices->alloc_list))
2543 return -ENOSPC;
2544
2545 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2546 &min_stripes, &sub_stripes);
2547 if (ret)
2548 return ret;
2549
2550 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2551 GFP_NOFS);
2552 if (!devices_info)
2553 return -ENOMEM;
2554
2555 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2556 if (!map) {
2557 ret = -ENOMEM;
2558 goto error;
2559 }
2560 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002561
Yan Zheng2b820322008-11-17 21:11:30 -05002562 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002563 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002564 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002565
Miao Xieb2117a32011-01-05 10:07:28 +00002566 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2567 num_stripes, 0);
2568
2569 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002570 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002571 min_devices = 1;
2572 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002573 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002574 min_devices = min_stripes;
2575 }
Chris Masonad5bd912008-04-21 08:28:10 -04002576
Yan Zheng2b820322008-11-17 21:11:30 -05002577 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002578 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002579 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002580 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002581 if (device->total_bytes > device->bytes_used)
2582 avail = device->total_bytes - device->bytes_used;
2583 else
2584 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002585 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002586
Chris Masondfe25022008-05-13 13:46:40 -04002587 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002588 ret = find_free_dev_extent(trans, device, min_free,
2589 &devices_info[i].dev_offset,
2590 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002591 if (ret == 0) {
2592 list_move_tail(&device->dev_alloc_list,
2593 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002594 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002595 map->stripes[index].physical =
2596 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002597 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002598 if (type & BTRFS_BLOCK_GROUP_DUP) {
2599 map->stripes[index].dev = device;
2600 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002601 devices_info[i].dev_offset +
2602 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002603 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002604 }
Miao Xieb2117a32011-01-05 10:07:28 +00002605 } else if (ret != -ENOSPC)
2606 goto error;
2607
2608 devices_info[i].dev = device;
2609 i++;
2610 } else if (device->in_fs_metadata &&
2611 avail >= BTRFS_STRIPE_LEN) {
2612 devices_info[i].dev = device;
2613 devices_info[i].max_avail = avail;
2614 i++;
2615 }
2616
Yan Zheng2b820322008-11-17 21:11:30 -05002617 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002618 break;
2619 }
Miao Xieb2117a32011-01-05 10:07:28 +00002620
Yan Zheng2b820322008-11-17 21:11:30 -05002621 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002622 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002623 if (index >= min_stripes) {
2624 num_stripes = index;
2625 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2626 num_stripes /= sub_stripes;
2627 num_stripes *= sub_stripes;
2628 }
Miao Xieb2117a32011-01-05 10:07:28 +00002629
2630 map = __shrink_map_lookup_stripes(map, num_stripes);
2631 } else if (i >= min_devices) {
2632 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2633 devices_info, i, type,
2634 &map, min_stripes,
2635 &calc_size);
2636 if (ret)
2637 goto error;
2638 } else {
2639 ret = -ENOSPC;
2640 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002641 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002642 }
Chris Mason593060d2008-03-25 16:50:33 -04002643 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002644 map->stripe_len = BTRFS_STRIPE_LEN;
2645 map->io_align = BTRFS_STRIPE_LEN;
2646 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002647 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002648 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002649
Yan Zheng2b820322008-11-17 21:11:30 -05002650 *map_ret = map;
2651 *stripe_size = calc_size;
2652 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002653 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002654
liubo1abe9b82011-03-24 11:18:59 +00002655 trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes);
2656
Chris Mason0b86a832008-03-24 15:01:56 -04002657 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002658 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002659 ret = -ENOMEM;
2660 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002661 }
Chris Mason0b86a832008-03-24 15:01:56 -04002662 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002663 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002664 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002665 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002666 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002667
Chris Mason0b86a832008-03-24 15:01:56 -04002668 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002669 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002670 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002671 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002672 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002673 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002674
2675 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2676 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2677 start, *num_bytes);
2678 BUG_ON(ret);
2679
2680 index = 0;
2681 while (index < map->num_stripes) {
2682 device = map->stripes[index].dev;
2683 dev_offset = map->stripes[index].physical;
2684
2685 ret = btrfs_alloc_dev_extent(trans, device,
2686 info->chunk_root->root_key.objectid,
2687 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2688 start, dev_offset, calc_size);
2689 BUG_ON(ret);
2690 index++;
2691 }
2692
Miao Xieb2117a32011-01-05 10:07:28 +00002693 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002694 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002695
2696error:
2697 kfree(map);
2698 kfree(devices_info);
2699 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002700}
2701
2702static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2703 struct btrfs_root *extent_root,
2704 struct map_lookup *map, u64 chunk_offset,
2705 u64 chunk_size, u64 stripe_size)
2706{
2707 u64 dev_offset;
2708 struct btrfs_key key;
2709 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2710 struct btrfs_device *device;
2711 struct btrfs_chunk *chunk;
2712 struct btrfs_stripe *stripe;
2713 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2714 int index = 0;
2715 int ret;
2716
2717 chunk = kzalloc(item_size, GFP_NOFS);
2718 if (!chunk)
2719 return -ENOMEM;
2720
2721 index = 0;
2722 while (index < map->num_stripes) {
2723 device = map->stripes[index].dev;
2724 device->bytes_used += stripe_size;
2725 ret = btrfs_update_device(trans, device);
2726 BUG_ON(ret);
2727 index++;
2728 }
2729
2730 index = 0;
2731 stripe = &chunk->stripe;
2732 while (index < map->num_stripes) {
2733 device = map->stripes[index].dev;
2734 dev_offset = map->stripes[index].physical;
2735
2736 btrfs_set_stack_stripe_devid(stripe, device->devid);
2737 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2738 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2739 stripe++;
2740 index++;
2741 }
2742
2743 btrfs_set_stack_chunk_length(chunk, chunk_size);
2744 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2745 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2746 btrfs_set_stack_chunk_type(chunk, map->type);
2747 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2748 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2749 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2750 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2751 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2752
2753 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2754 key.type = BTRFS_CHUNK_ITEM_KEY;
2755 key.offset = chunk_offset;
2756
2757 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2758 BUG_ON(ret);
2759
2760 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2761 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2762 item_size);
2763 BUG_ON(ret);
2764 }
liubo1abe9b82011-03-24 11:18:59 +00002765
Yan Zheng2b820322008-11-17 21:11:30 -05002766 kfree(chunk);
2767 return 0;
2768}
2769
2770/*
2771 * Chunk allocation falls into two parts. The first part does works
2772 * that make the new allocated chunk useable, but not do any operation
2773 * that modifies the chunk tree. The second part does the works that
2774 * require modifying the chunk tree. This division is important for the
2775 * bootstrap process of adding storage to a seed btrfs.
2776 */
2777int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2778 struct btrfs_root *extent_root, u64 type)
2779{
2780 u64 chunk_offset;
2781 u64 chunk_size;
2782 u64 stripe_size;
2783 struct map_lookup *map;
2784 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2785 int ret;
2786
2787 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2788 &chunk_offset);
2789 if (ret)
2790 return ret;
2791
2792 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2793 &stripe_size, chunk_offset, type);
2794 if (ret)
2795 return ret;
2796
2797 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2798 chunk_size, stripe_size);
2799 BUG_ON(ret);
2800 return 0;
2801}
2802
Chris Masond3977122009-01-05 21:25:51 -05002803static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002804 struct btrfs_root *root,
2805 struct btrfs_device *device)
2806{
2807 u64 chunk_offset;
2808 u64 sys_chunk_offset;
2809 u64 chunk_size;
2810 u64 sys_chunk_size;
2811 u64 stripe_size;
2812 u64 sys_stripe_size;
2813 u64 alloc_profile;
2814 struct map_lookup *map;
2815 struct map_lookup *sys_map;
2816 struct btrfs_fs_info *fs_info = root->fs_info;
2817 struct btrfs_root *extent_root = fs_info->extent_root;
2818 int ret;
2819
2820 ret = find_next_chunk(fs_info->chunk_root,
2821 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2822 BUG_ON(ret);
2823
2824 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2825 (fs_info->metadata_alloc_profile &
2826 fs_info->avail_metadata_alloc_bits);
2827 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2828
2829 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2830 &stripe_size, chunk_offset, alloc_profile);
2831 BUG_ON(ret);
2832
2833 sys_chunk_offset = chunk_offset + chunk_size;
2834
2835 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2836 (fs_info->system_alloc_profile &
2837 fs_info->avail_system_alloc_bits);
2838 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2839
2840 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2841 &sys_chunk_size, &sys_stripe_size,
2842 sys_chunk_offset, alloc_profile);
2843 BUG_ON(ret);
2844
2845 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2846 BUG_ON(ret);
2847
2848 /*
2849 * Modifying chunk tree needs allocating new blocks from both
2850 * system block group and metadata block group. So we only can
2851 * do operations require modifying the chunk tree after both
2852 * block groups were created.
2853 */
2854 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2855 chunk_size, stripe_size);
2856 BUG_ON(ret);
2857
2858 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2859 sys_chunk_offset, sys_chunk_size,
2860 sys_stripe_size);
2861 BUG_ON(ret);
2862 return 0;
2863}
2864
2865int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2866{
2867 struct extent_map *em;
2868 struct map_lookup *map;
2869 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2870 int readonly = 0;
2871 int i;
2872
Chris Mason890871b2009-09-02 16:24:52 -04002873 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002874 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002875 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002876 if (!em)
2877 return 1;
2878
Josef Bacikf48b9072010-01-27 02:07:59 +00002879 if (btrfs_test_opt(root, DEGRADED)) {
2880 free_extent_map(em);
2881 return 0;
2882 }
2883
Yan Zheng2b820322008-11-17 21:11:30 -05002884 map = (struct map_lookup *)em->bdev;
2885 for (i = 0; i < map->num_stripes; i++) {
2886 if (!map->stripes[i].dev->writeable) {
2887 readonly = 1;
2888 break;
2889 }
2890 }
2891 free_extent_map(em);
2892 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002893}
2894
2895void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2896{
2897 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2898}
2899
2900void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2901{
2902 struct extent_map *em;
2903
Chris Masond3977122009-01-05 21:25:51 -05002904 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002905 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002906 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2907 if (em)
2908 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002909 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002910 if (!em)
2911 break;
2912 kfree(em->bdev);
2913 /* once for us */
2914 free_extent_map(em);
2915 /* once for the tree */
2916 free_extent_map(em);
2917 }
2918}
2919
Chris Masonf1885912008-04-09 16:28:12 -04002920int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2921{
2922 struct extent_map *em;
2923 struct map_lookup *map;
2924 struct extent_map_tree *em_tree = &map_tree->map_tree;
2925 int ret;
2926
Chris Mason890871b2009-09-02 16:24:52 -04002927 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002928 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002929 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002930 BUG_ON(!em);
2931
2932 BUG_ON(em->start > logical || em->start + em->len < logical);
2933 map = (struct map_lookup *)em->bdev;
2934 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2935 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002936 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2937 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002938 else
2939 ret = 1;
2940 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002941 return ret;
2942}
2943
Chris Masondfe25022008-05-13 13:46:40 -04002944static int find_live_mirror(struct map_lookup *map, int first, int num,
2945 int optimal)
2946{
2947 int i;
2948 if (map->stripes[optimal].dev->bdev)
2949 return optimal;
2950 for (i = first; i < first + num; i++) {
2951 if (map->stripes[i].dev->bdev)
2952 return i;
2953 }
2954 /* we couldn't find one that doesn't fail. Just return something
2955 * and the io error handling code will clean up eventually
2956 */
2957 return optimal;
2958}
2959
Chris Masonf2d8d742008-04-21 10:03:05 -04002960static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2961 u64 logical, u64 *length,
2962 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002963 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002964{
2965 struct extent_map *em;
2966 struct map_lookup *map;
2967 struct extent_map_tree *em_tree = &map_tree->map_tree;
2968 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002969 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002970 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002971 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002972 u64 stripe_nr_orig;
2973 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002974 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002975 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002976 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002977 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002978 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002979 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002980 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002981
Li Dongyangfce3bb92011-03-24 10:24:26 +00002982 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002983 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002984again:
2985 if (multi_ret) {
2986 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2987 GFP_NOFS);
2988 if (!multi)
2989 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002990
2991 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002992 }
Chris Mason0b86a832008-03-24 15:01:56 -04002993
Chris Mason890871b2009-09-02 16:24:52 -04002994 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002995 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002996 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002997
Chris Mason3b951512008-04-17 11:29:12 -04002998 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002999 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3000 (unsigned long long)logical,
3001 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003002 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003003 }
Chris Mason0b86a832008-03-24 15:01:56 -04003004
3005 BUG_ON(em->start > logical || em->start + em->len < logical);
3006 map = (struct map_lookup *)em->bdev;
3007 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003008
Chris Masonf1885912008-04-09 16:28:12 -04003009 if (mirror_num > map->num_stripes)
3010 mirror_num = 0;
3011
Chris Masoncea9e442008-04-09 16:28:12 -04003012 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003013 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04003014 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3015 BTRFS_BLOCK_GROUP_DUP)) {
3016 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003017 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003018 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3019 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003020 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003021 }
3022 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003023 if (rw & REQ_DISCARD) {
3024 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3025 BTRFS_BLOCK_GROUP_RAID1 |
3026 BTRFS_BLOCK_GROUP_DUP |
3027 BTRFS_BLOCK_GROUP_RAID10)) {
3028 stripes_required = map->num_stripes;
3029 }
3030 }
3031 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04003032 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04003033 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003034 free_extent_map(em);
3035 kfree(multi);
3036 goto again;
3037 }
Chris Mason593060d2008-03-25 16:50:33 -04003038 stripe_nr = offset;
3039 /*
3040 * stripe_nr counts the total number of stripes we have to stride
3041 * to get to this block
3042 */
3043 do_div(stripe_nr, map->stripe_len);
3044
3045 stripe_offset = stripe_nr * map->stripe_len;
3046 BUG_ON(offset < stripe_offset);
3047
3048 /* stripe_offset is the offset of this block in its stripe*/
3049 stripe_offset = offset - stripe_offset;
3050
Li Dongyangfce3bb92011-03-24 10:24:26 +00003051 if (rw & REQ_DISCARD)
3052 *length = min_t(u64, em->len - offset, *length);
3053 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3054 BTRFS_BLOCK_GROUP_RAID1 |
3055 BTRFS_BLOCK_GROUP_RAID10 |
3056 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04003057 /* we limit the length of each bio to what fits in a stripe */
3058 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003059 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003060 } else {
3061 *length = em->len - offset;
3062 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003063
Jens Axboe7eaceac2011-03-10 08:52:07 +01003064 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003065 goto out;
3066
Chris Masonf2d8d742008-04-21 10:03:05 -04003067 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003068 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003069 stripe_nr_orig = stripe_nr;
3070 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3071 (~(map->stripe_len - 1));
3072 do_div(stripe_nr_end, map->stripe_len);
3073 stripe_end_offset = stripe_nr_end * map->stripe_len -
3074 (offset + *length);
3075 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3076 if (rw & REQ_DISCARD)
3077 num_stripes = min_t(u64, map->num_stripes,
3078 stripe_nr_end - stripe_nr_orig);
3079 stripe_index = do_div(stripe_nr, map->num_stripes);
3080 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003081 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003082 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003083 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003084 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003085 else {
3086 stripe_index = find_live_mirror(map, 0,
3087 map->num_stripes,
3088 current->pid % map->num_stripes);
3089 }
Chris Mason2fff7342008-04-29 14:12:09 -04003090
Chris Mason611f0e02008-04-03 16:29:03 -04003091 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00003092 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003093 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003094 else if (mirror_num)
3095 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003096
Chris Mason321aecc2008-04-16 10:49:51 -04003097 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3098 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003099
3100 stripe_index = do_div(stripe_nr, factor);
3101 stripe_index *= map->sub_stripes;
3102
Jens Axboe7eaceac2011-03-10 08:52:07 +01003103 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003104 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003105 else if (rw & REQ_DISCARD)
3106 num_stripes = min_t(u64, map->sub_stripes *
3107 (stripe_nr_end - stripe_nr_orig),
3108 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003109 else if (mirror_num)
3110 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003111 else {
3112 stripe_index = find_live_mirror(map, stripe_index,
3113 map->sub_stripes, stripe_index +
3114 current->pid % map->sub_stripes);
3115 }
Chris Mason8790d502008-04-03 16:29:03 -04003116 } else {
3117 /*
3118 * after this do_div call, stripe_nr is the number of stripes
3119 * on this device we have to walk to find the data, and
3120 * stripe_index is the number of our device in the stripe array
3121 */
3122 stripe_index = do_div(stripe_nr, map->num_stripes);
3123 }
Chris Mason593060d2008-03-25 16:50:33 -04003124 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003125
Li Dongyangfce3bb92011-03-24 10:24:26 +00003126 if (rw & REQ_DISCARD) {
3127 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003128 multi->stripes[i].physical =
3129 map->stripes[stripe_index].physical +
3130 stripe_offset + stripe_nr * map->stripe_len;
3131 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003132
3133 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3134 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003135 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003136 int j;
3137
Chris Masond9d04872011-03-27 21:23:21 -04003138 div_u64_rem(stripe_nr_end - 1,
3139 map->num_stripes,
3140 &last_stripe);
3141
Li Dongyangfce3bb92011-03-24 10:24:26 +00003142 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003143 u32 test;
3144
3145 div_u64_rem(stripe_nr_end - 1 - j,
3146 map->num_stripes, &test);
3147 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003148 break;
3149 }
3150 stripes = stripe_nr_end - 1 - j;
3151 do_div(stripes, map->num_stripes);
3152 multi->stripes[i].length = map->stripe_len *
3153 (stripes - stripe_nr + 1);
3154
3155 if (i == 0) {
3156 multi->stripes[i].length -=
3157 stripe_offset;
3158 stripe_offset = 0;
3159 }
3160 if (stripe_index == last_stripe)
3161 multi->stripes[i].length -=
3162 stripe_end_offset;
3163 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3164 u64 stripes;
3165 int j;
3166 int factor = map->num_stripes /
3167 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003168 u32 last_stripe = 0;
3169
3170 div_u64_rem(stripe_nr_end - 1,
3171 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003172 last_stripe *= map->sub_stripes;
3173
3174 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003175 u32 test;
3176
3177 div_u64_rem(stripe_nr_end - 1 - j,
3178 factor, &test);
3179
3180 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003181 stripe_index / map->sub_stripes)
3182 break;
3183 }
3184 stripes = stripe_nr_end - 1 - j;
3185 do_div(stripes, factor);
3186 multi->stripes[i].length = map->stripe_len *
3187 (stripes - stripe_nr + 1);
3188
3189 if (i < map->sub_stripes) {
3190 multi->stripes[i].length -=
3191 stripe_offset;
3192 if (i == map->sub_stripes - 1)
3193 stripe_offset = 0;
3194 }
3195 if (stripe_index >= last_stripe &&
3196 stripe_index <= (last_stripe +
3197 map->sub_stripes - 1)) {
3198 multi->stripes[i].length -=
3199 stripe_end_offset;
3200 }
3201 } else
3202 multi->stripes[i].length = *length;
3203
3204 stripe_index++;
3205 if (stripe_index == map->num_stripes) {
3206 /* This could only happen for RAID0/10 */
3207 stripe_index = 0;
3208 stripe_nr++;
3209 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003210 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003211 } else {
3212 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003213 multi->stripes[i].physical =
3214 map->stripes[stripe_index].physical +
3215 stripe_offset +
3216 stripe_nr * map->stripe_len;
3217 multi->stripes[i].dev =
3218 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003219 stripe_index++;
3220 }
Chris Mason593060d2008-03-25 16:50:33 -04003221 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003222 if (multi_ret) {
3223 *multi_ret = multi;
3224 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003225 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003226 }
Chris Masoncea9e442008-04-09 16:28:12 -04003227out:
Chris Mason0b86a832008-03-24 15:01:56 -04003228 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003229 return 0;
3230}
3231
Chris Masonf2d8d742008-04-21 10:03:05 -04003232int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3233 u64 logical, u64 *length,
3234 struct btrfs_multi_bio **multi_ret, int mirror_num)
3235{
3236 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003237 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003238}
3239
Yan Zhenga512bbf2008-12-08 16:46:26 -05003240int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3241 u64 chunk_start, u64 physical, u64 devid,
3242 u64 **logical, int *naddrs, int *stripe_len)
3243{
3244 struct extent_map_tree *em_tree = &map_tree->map_tree;
3245 struct extent_map *em;
3246 struct map_lookup *map;
3247 u64 *buf;
3248 u64 bytenr;
3249 u64 length;
3250 u64 stripe_nr;
3251 int i, j, nr = 0;
3252
Chris Mason890871b2009-09-02 16:24:52 -04003253 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003254 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003255 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003256
3257 BUG_ON(!em || em->start != chunk_start);
3258 map = (struct map_lookup *)em->bdev;
3259
3260 length = em->len;
3261 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3262 do_div(length, map->num_stripes / map->sub_stripes);
3263 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3264 do_div(length, map->num_stripes);
3265
3266 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3267 BUG_ON(!buf);
3268
3269 for (i = 0; i < map->num_stripes; i++) {
3270 if (devid && map->stripes[i].dev->devid != devid)
3271 continue;
3272 if (map->stripes[i].physical > physical ||
3273 map->stripes[i].physical + length <= physical)
3274 continue;
3275
3276 stripe_nr = physical - map->stripes[i].physical;
3277 do_div(stripe_nr, map->stripe_len);
3278
3279 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3280 stripe_nr = stripe_nr * map->num_stripes + i;
3281 do_div(stripe_nr, map->sub_stripes);
3282 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3283 stripe_nr = stripe_nr * map->num_stripes + i;
3284 }
3285 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003286 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003287 for (j = 0; j < nr; j++) {
3288 if (buf[j] == bytenr)
3289 break;
3290 }
Chris Mason934d3752008-12-08 16:43:10 -05003291 if (j == nr) {
3292 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003293 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003294 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003295 }
3296
Yan Zhenga512bbf2008-12-08 16:46:26 -05003297 *logical = buf;
3298 *naddrs = nr;
3299 *stripe_len = map->stripe_len;
3300
3301 free_extent_map(em);
3302 return 0;
3303}
3304
Chris Mason8790d502008-04-03 16:29:03 -04003305static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003306{
Chris Masoncea9e442008-04-09 16:28:12 -04003307 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003308 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003309
Chris Mason8790d502008-04-03 16:29:03 -04003310 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003311 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003312
Chris Mason7d2b4da2008-08-05 10:13:57 -04003313 if (bio == multi->orig_bio)
3314 is_orig_bio = 1;
3315
Chris Masoncea9e442008-04-09 16:28:12 -04003316 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003317 if (!is_orig_bio) {
3318 bio_put(bio);
3319 bio = multi->orig_bio;
3320 }
Chris Mason8790d502008-04-03 16:29:03 -04003321 bio->bi_private = multi->private;
3322 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003323 /* only send an error to the higher layers if it is
3324 * beyond the tolerance of the multi-bio
3325 */
Chris Mason1259ab72008-05-12 13:39:03 -04003326 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003327 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003328 } else if (err) {
3329 /*
3330 * this bio is actually up to date, we didn't
3331 * go over the max number of errors
3332 */
3333 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003334 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003335 }
Chris Mason8790d502008-04-03 16:29:03 -04003336 kfree(multi);
3337
3338 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003339 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003340 bio_put(bio);
3341 }
Chris Mason8790d502008-04-03 16:29:03 -04003342}
3343
Chris Mason8b712842008-06-11 16:50:36 -04003344struct async_sched {
3345 struct bio *bio;
3346 int rw;
3347 struct btrfs_fs_info *info;
3348 struct btrfs_work work;
3349};
3350
3351/*
3352 * see run_scheduled_bios for a description of why bios are collected for
3353 * async submit.
3354 *
3355 * This will add one bio to the pending list for a device and make sure
3356 * the work struct is scheduled.
3357 */
Chris Masond3977122009-01-05 21:25:51 -05003358static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003359 struct btrfs_device *device,
3360 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003361{
3362 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003363 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003364
3365 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003366 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003367 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003368 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003369 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003370 return 0;
3371 }
3372
3373 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003374 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003375 * higher layers. Otherwise, the async bio makes it appear we have
3376 * made progress against dirty pages when we've really just put it
3377 * on a queue for later
3378 */
Chris Mason0986fe92008-08-15 15:34:15 -04003379 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003380 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003381 bio->bi_next = NULL;
3382 bio->bi_rw |= rw;
3383
3384 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003385 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003386 pending_bios = &device->pending_sync_bios;
3387 else
3388 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003389
Chris Masonffbd5172009-04-20 15:50:09 -04003390 if (pending_bios->tail)
3391 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003392
Chris Masonffbd5172009-04-20 15:50:09 -04003393 pending_bios->tail = bio;
3394 if (!pending_bios->head)
3395 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003396 if (device->running_pending)
3397 should_queue = 0;
3398
3399 spin_unlock(&device->io_lock);
3400
3401 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003402 btrfs_queue_worker(&root->fs_info->submit_workers,
3403 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003404 return 0;
3405}
3406
Chris Masonf1885912008-04-09 16:28:12 -04003407int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003408 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003409{
3410 struct btrfs_mapping_tree *map_tree;
3411 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003412 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003413 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003414 u64 length = 0;
3415 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003416 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003417 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003418 int dev_nr = 0;
3419 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003420
Chris Masonf2d8d742008-04-21 10:03:05 -04003421 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003422 map_tree = &root->fs_info->mapping_tree;
3423 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003424
Chris Masonf1885912008-04-09 16:28:12 -04003425 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3426 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003427 BUG_ON(ret);
3428
3429 total_devs = multi->num_stripes;
3430 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003431 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3432 "len %llu\n", (unsigned long long)logical,
3433 (unsigned long long)length,
3434 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003435 BUG();
3436 }
3437 multi->end_io = first_bio->bi_end_io;
3438 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003439 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003440 atomic_set(&multi->stripes_pending, multi->num_stripes);
3441
Chris Masond3977122009-01-05 21:25:51 -05003442 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003443 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003444 if (dev_nr < total_devs - 1) {
3445 bio = bio_clone(first_bio, GFP_NOFS);
3446 BUG_ON(!bio);
3447 } else {
3448 bio = first_bio;
3449 }
3450 bio->bi_private = multi;
3451 bio->bi_end_io = end_bio_multi_stripe;
3452 }
Chris Masoncea9e442008-04-09 16:28:12 -04003453 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3454 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003455 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003456 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003457 if (async_submit)
3458 schedule_bio(root, dev, rw, bio);
3459 else
3460 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003461 } else {
3462 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3463 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003464 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003465 }
Chris Mason8790d502008-04-03 16:29:03 -04003466 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003467 }
Chris Masoncea9e442008-04-09 16:28:12 -04003468 if (total_devs == 1)
3469 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003470 return 0;
3471}
3472
Chris Masona4437552008-04-18 10:29:38 -04003473struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003474 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003475{
Yan Zheng2b820322008-11-17 21:11:30 -05003476 struct btrfs_device *device;
3477 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003478
Yan Zheng2b820322008-11-17 21:11:30 -05003479 cur_devices = root->fs_info->fs_devices;
3480 while (cur_devices) {
3481 if (!fsid ||
3482 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3483 device = __find_device(&cur_devices->devices,
3484 devid, uuid);
3485 if (device)
3486 return device;
3487 }
3488 cur_devices = cur_devices->seed;
3489 }
3490 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003491}
3492
Chris Masondfe25022008-05-13 13:46:40 -04003493static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3494 u64 devid, u8 *dev_uuid)
3495{
3496 struct btrfs_device *device;
3497 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3498
3499 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003500 if (!device)
3501 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003502 list_add(&device->dev_list,
3503 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003504 device->dev_root = root->fs_info->dev_root;
3505 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003506 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003507 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003508 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003509 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003510 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003511 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003512 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003513 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3514 return device;
3515}
3516
Chris Mason0b86a832008-03-24 15:01:56 -04003517static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3518 struct extent_buffer *leaf,
3519 struct btrfs_chunk *chunk)
3520{
3521 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3522 struct map_lookup *map;
3523 struct extent_map *em;
3524 u64 logical;
3525 u64 length;
3526 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003527 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003528 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003529 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003530 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003531
Chris Masone17cade2008-04-15 15:41:47 -04003532 logical = key->offset;
3533 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003534
Chris Mason890871b2009-09-02 16:24:52 -04003535 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003536 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003537 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003538
3539 /* already mapped? */
3540 if (em && em->start <= logical && em->start + em->len > logical) {
3541 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003542 return 0;
3543 } else if (em) {
3544 free_extent_map(em);
3545 }
Chris Mason0b86a832008-03-24 15:01:56 -04003546
Chris Mason0b86a832008-03-24 15:01:56 -04003547 em = alloc_extent_map(GFP_NOFS);
3548 if (!em)
3549 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003550 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3551 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003552 if (!map) {
3553 free_extent_map(em);
3554 return -ENOMEM;
3555 }
3556
3557 em->bdev = (struct block_device *)map;
3558 em->start = logical;
3559 em->len = length;
3560 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003561 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003562
Chris Mason593060d2008-03-25 16:50:33 -04003563 map->num_stripes = num_stripes;
3564 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3565 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3566 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3567 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3568 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003569 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003570 for (i = 0; i < num_stripes; i++) {
3571 map->stripes[i].physical =
3572 btrfs_stripe_offset_nr(leaf, chunk, i);
3573 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003574 read_extent_buffer(leaf, uuid, (unsigned long)
3575 btrfs_stripe_dev_uuid_nr(chunk, i),
3576 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003577 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3578 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003579 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003580 kfree(map);
3581 free_extent_map(em);
3582 return -EIO;
3583 }
Chris Masondfe25022008-05-13 13:46:40 -04003584 if (!map->stripes[i].dev) {
3585 map->stripes[i].dev =
3586 add_missing_dev(root, devid, uuid);
3587 if (!map->stripes[i].dev) {
3588 kfree(map);
3589 free_extent_map(em);
3590 return -EIO;
3591 }
3592 }
3593 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003594 }
3595
Chris Mason890871b2009-09-02 16:24:52 -04003596 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003597 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003598 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003599 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003600 free_extent_map(em);
3601
3602 return 0;
3603}
3604
3605static int fill_device_from_item(struct extent_buffer *leaf,
3606 struct btrfs_dev_item *dev_item,
3607 struct btrfs_device *device)
3608{
3609 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003610
3611 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003612 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3613 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003614 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3615 device->type = btrfs_device_type(leaf, dev_item);
3616 device->io_align = btrfs_device_io_align(leaf, dev_item);
3617 device->io_width = btrfs_device_io_width(leaf, dev_item);
3618 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003619
3620 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003621 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003622
Chris Mason0b86a832008-03-24 15:01:56 -04003623 return 0;
3624}
3625
Yan Zheng2b820322008-11-17 21:11:30 -05003626static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3627{
3628 struct btrfs_fs_devices *fs_devices;
3629 int ret;
3630
3631 mutex_lock(&uuid_mutex);
3632
3633 fs_devices = root->fs_info->fs_devices->seed;
3634 while (fs_devices) {
3635 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3636 ret = 0;
3637 goto out;
3638 }
3639 fs_devices = fs_devices->seed;
3640 }
3641
3642 fs_devices = find_fsid(fsid);
3643 if (!fs_devices) {
3644 ret = -ENOENT;
3645 goto out;
3646 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003647
3648 fs_devices = clone_fs_devices(fs_devices);
3649 if (IS_ERR(fs_devices)) {
3650 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003651 goto out;
3652 }
3653
Christoph Hellwig97288f22008-12-02 06:36:09 -05003654 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003655 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003656 if (ret)
3657 goto out;
3658
3659 if (!fs_devices->seeding) {
3660 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003661 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003662 ret = -EINVAL;
3663 goto out;
3664 }
3665
3666 fs_devices->seed = root->fs_info->fs_devices->seed;
3667 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003668out:
3669 mutex_unlock(&uuid_mutex);
3670 return ret;
3671}
3672
Chris Mason0d81ba52008-03-24 15:02:07 -04003673static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003674 struct extent_buffer *leaf,
3675 struct btrfs_dev_item *dev_item)
3676{
3677 struct btrfs_device *device;
3678 u64 devid;
3679 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003680 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003681 u8 dev_uuid[BTRFS_UUID_SIZE];
3682
Chris Mason0b86a832008-03-24 15:01:56 -04003683 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003684 read_extent_buffer(leaf, dev_uuid,
3685 (unsigned long)btrfs_device_uuid(dev_item),
3686 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003687 read_extent_buffer(leaf, fs_uuid,
3688 (unsigned long)btrfs_device_fsid(dev_item),
3689 BTRFS_UUID_SIZE);
3690
3691 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3692 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003693 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003694 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003695 }
3696
3697 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3698 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003699 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003700 return -EIO;
3701
3702 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003703 printk(KERN_WARNING "warning devid %llu missing\n",
3704 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003705 device = add_missing_dev(root, devid, dev_uuid);
3706 if (!device)
3707 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003708 } else if (!device->missing) {
3709 /*
3710 * this happens when a device that was properly setup
3711 * in the device info lists suddenly goes bad.
3712 * device->bdev is NULL, and so we have to set
3713 * device->missing to one here
3714 */
3715 root->fs_info->fs_devices->missing_devices++;
3716 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003717 }
3718 }
3719
3720 if (device->fs_devices != root->fs_info->fs_devices) {
3721 BUG_ON(device->writeable);
3722 if (device->generation !=
3723 btrfs_device_generation(leaf, dev_item))
3724 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003725 }
Chris Mason0b86a832008-03-24 15:01:56 -04003726
3727 fill_device_from_item(leaf, dev_item, device);
3728 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003729 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003730 if (device->writeable)
3731 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003732 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003733 return ret;
3734}
3735
Chris Mason0d81ba52008-03-24 15:02:07 -04003736int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3737{
3738 struct btrfs_dev_item *dev_item;
3739
3740 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3741 dev_item);
3742 return read_one_dev(root, buf, dev_item);
3743}
3744
Yan Zhenge4404d62008-12-12 10:03:26 -05003745int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003746{
3747 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003748 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003749 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003750 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003751 u8 *ptr;
3752 unsigned long sb_ptr;
3753 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003754 u32 num_stripes;
3755 u32 array_size;
3756 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003757 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003758 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003759
Yan Zhenge4404d62008-12-12 10:03:26 -05003760 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003761 BTRFS_SUPER_INFO_SIZE);
3762 if (!sb)
3763 return -ENOMEM;
3764 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003765 btrfs_set_buffer_lockdep_class(sb, 0);
3766
Chris Masona061fc82008-05-07 11:43:44 -04003767 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003768 array_size = btrfs_super_sys_array_size(super_copy);
3769
Chris Mason0b86a832008-03-24 15:01:56 -04003770 ptr = super_copy->sys_chunk_array;
3771 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3772 cur = 0;
3773
3774 while (cur < array_size) {
3775 disk_key = (struct btrfs_disk_key *)ptr;
3776 btrfs_disk_key_to_cpu(&key, disk_key);
3777
Chris Masona061fc82008-05-07 11:43:44 -04003778 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003779 sb_ptr += len;
3780 cur += len;
3781
Chris Mason0d81ba52008-03-24 15:02:07 -04003782 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003783 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003784 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003785 if (ret)
3786 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003787 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3788 len = btrfs_chunk_item_size(num_stripes);
3789 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003790 ret = -EIO;
3791 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003792 }
3793 ptr += len;
3794 sb_ptr += len;
3795 cur += len;
3796 }
Chris Masona061fc82008-05-07 11:43:44 -04003797 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003798 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003799}
3800
3801int btrfs_read_chunk_tree(struct btrfs_root *root)
3802{
3803 struct btrfs_path *path;
3804 struct extent_buffer *leaf;
3805 struct btrfs_key key;
3806 struct btrfs_key found_key;
3807 int ret;
3808 int slot;
3809
3810 root = root->fs_info->chunk_root;
3811
3812 path = btrfs_alloc_path();
3813 if (!path)
3814 return -ENOMEM;
3815
3816 /* first we search for all of the device items, and then we
3817 * read in all of the chunk items. This way we can create chunk
3818 * mappings that reference all of the devices that are afound
3819 */
3820 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3821 key.offset = 0;
3822 key.type = 0;
3823again:
3824 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003825 if (ret < 0)
3826 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003827 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003828 leaf = path->nodes[0];
3829 slot = path->slots[0];
3830 if (slot >= btrfs_header_nritems(leaf)) {
3831 ret = btrfs_next_leaf(root, path);
3832 if (ret == 0)
3833 continue;
3834 if (ret < 0)
3835 goto error;
3836 break;
3837 }
3838 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3839 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3840 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3841 break;
3842 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3843 struct btrfs_dev_item *dev_item;
3844 dev_item = btrfs_item_ptr(leaf, slot,
3845 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003846 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003847 if (ret)
3848 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003849 }
3850 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3851 struct btrfs_chunk *chunk;
3852 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3853 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003854 if (ret)
3855 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003856 }
3857 path->slots[0]++;
3858 }
3859 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3860 key.objectid = 0;
3861 btrfs_release_path(root, path);
3862 goto again;
3863 }
Chris Mason0b86a832008-03-24 15:01:56 -04003864 ret = 0;
3865error:
Yan Zheng2b820322008-11-17 21:11:30 -05003866 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003867 return ret;
3868}