blob: ee197ec285475fc9c9ec914b5c4923b29a51c04e [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Chris Mason593060d2008-03-25 16:50:33 -040026#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "ctree.h"
29#include "extent_map.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "print-tree.h"
33#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Yan Zheng2b820322008-11-17 21:11:30 -050036static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
40
Chris Mason593060d2008-03-25 16:50:33 -040041#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040042 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040043
Chris Mason8a4b83c2008-03-24 15:02:07 -040044static DEFINE_MUTEX(uuid_mutex);
45static LIST_HEAD(fs_uuids);
46
Chris Masona061fc82008-05-07 11:43:44 -040047void btrfs_lock_volumes(void)
48{
49 mutex_lock(&uuid_mutex);
50}
51
52void btrfs_unlock_volumes(void)
53{
54 mutex_unlock(&uuid_mutex);
55}
56
Chris Mason7d9eb122008-07-08 14:19:17 -040057static void lock_chunks(struct btrfs_root *root)
58{
Chris Mason7d9eb122008-07-08 14:19:17 -040059 mutex_lock(&root->fs_info->chunk_mutex);
60}
61
62static void unlock_chunks(struct btrfs_root *root)
63{
Chris Mason7d9eb122008-07-08 14:19:17 -040064 mutex_unlock(&root->fs_info->chunk_mutex);
65}
66
Yan Zhenge4404d62008-12-12 10:03:26 -050067static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
68{
69 struct btrfs_device *device;
70 WARN_ON(fs_devices->opened);
71 while (!list_empty(&fs_devices->devices)) {
72 device = list_entry(fs_devices->devices.next,
73 struct btrfs_device, dev_list);
74 list_del(&device->dev_list);
75 kfree(device->name);
76 kfree(device);
77 }
78 kfree(fs_devices);
79}
80
Chris Mason8a4b83c2008-03-24 15:02:07 -040081int btrfs_cleanup_fs_uuids(void)
82{
83 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040084
Yan Zheng2b820322008-11-17 21:11:30 -050085 while (!list_empty(&fs_uuids)) {
86 fs_devices = list_entry(fs_uuids.next,
87 struct btrfs_fs_devices, list);
88 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050089 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 }
91 return 0;
92}
93
Chris Masona1b32a52008-09-05 16:09:51 -040094static noinline struct btrfs_device *__find_device(struct list_head *head,
95 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040096{
97 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040098
Qinghuang Fengc6e30872009-01-21 10:59:08 -050099 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400100 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400101 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400103 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400104 }
105 return NULL;
106}
107
Chris Masona1b32a52008-09-05 16:09:51 -0400108static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400109{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400110 struct btrfs_fs_devices *fs_devices;
111
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500112 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
114 return fs_devices;
115 }
116 return NULL;
117}
118
Chris Masonffbd5172009-04-20 15:50:09 -0400119static void requeue_list(struct btrfs_pending_bios *pending_bios,
120 struct bio *head, struct bio *tail)
121{
122
123 struct bio *old_head;
124
125 old_head = pending_bios->head;
126 pending_bios->head = head;
127 if (pending_bios->tail)
128 tail->bi_next = old_head;
129 else
130 pending_bios->tail = tail;
131}
132
Chris Mason8b712842008-06-11 16:50:36 -0400133/*
134 * we try to collect pending bios for a device so we don't get a large
135 * number of procs sending bios down to the same device. This greatly
136 * improves the schedulers ability to collect and merge the bios.
137 *
138 * But, it also turns into a long list of bios to process and that is sure
139 * to eventually make the worker thread block. The solution here is to
140 * make some progress and then put this work struct back at the end of
141 * the list if the block device is congested. This way, multiple devices
142 * can make progress from a single worker thread.
143 */
Chris Masond3977122009-01-05 21:25:51 -0500144static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400145{
146 struct bio *pending;
147 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400148 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400149 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400150 struct bio *tail;
151 struct bio *cur;
152 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400153 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400154 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400155 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400156 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400157 int force_reg = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400158 struct blk_plug plug;
159
160 /*
161 * this function runs all the bios we've collected for
162 * a particular device. We don't want to wander off to
163 * another device without first sending all of these down.
164 * So, setup a plug here and finish it off before we return
165 */
166 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400167
Chris Masonbedf7622009-04-03 10:32:58 -0400168 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400169 fs_info = device->dev_root->fs_info;
170 limit = btrfs_async_submit_limit(fs_info);
171 limit = limit * 2 / 3;
172
Chris Mason8b712842008-06-11 16:50:36 -0400173loop:
174 spin_lock(&device->io_lock);
175
Chris Masona6837052009-02-04 09:19:41 -0500176loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400177 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400178
Chris Mason8b712842008-06-11 16:50:36 -0400179 /* take all the bios off the list at once and process them
180 * later on (without the lock held). But, remember the
181 * tail and other pointers so the bios can be properly reinserted
182 * into the list if we hit congestion
183 */
Chris Masond84275c2009-06-09 15:39:08 -0400184 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400185 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400186 force_reg = 1;
187 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400188 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400189 force_reg = 0;
190 }
Chris Masonffbd5172009-04-20 15:50:09 -0400191
192 pending = pending_bios->head;
193 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400194 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400195
196 /*
197 * if pending was null this time around, no bios need processing
198 * at all and we can stop. Otherwise it'll loop back up again
199 * and do an additional check so no bios are missed.
200 *
201 * device->running_pending is used to synchronize with the
202 * schedule_bio code.
203 */
Chris Masonffbd5172009-04-20 15:50:09 -0400204 if (device->pending_sync_bios.head == NULL &&
205 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400206 again = 0;
207 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400208 } else {
209 again = 1;
210 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400211 }
Chris Masonffbd5172009-04-20 15:50:09 -0400212
213 pending_bios->head = NULL;
214 pending_bios->tail = NULL;
215
Chris Mason8b712842008-06-11 16:50:36 -0400216 spin_unlock(&device->io_lock);
217
Chris Masond3977122009-01-05 21:25:51 -0500218 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400219
220 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400221 /* we want to work on both lists, but do more bios on the
222 * sync list than the regular list
223 */
224 if ((num_run > 32 &&
225 pending_bios != &device->pending_sync_bios &&
226 device->pending_sync_bios.head) ||
227 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
228 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400229 spin_lock(&device->io_lock);
230 requeue_list(pending_bios, pending, tail);
231 goto loop_lock;
232 }
233
Chris Mason8b712842008-06-11 16:50:36 -0400234 cur = pending;
235 pending = pending->bi_next;
236 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400237 atomic_dec(&fs_info->nr_async_bios);
238
239 if (atomic_read(&fs_info->nr_async_bios) < limit &&
240 waitqueue_active(&fs_info->async_submit_wait))
241 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400242
243 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400244
Chris Mason5ff7ba32010-03-15 10:21:30 -0400245 submit_bio(cur->bi_rw, cur);
246 num_run++;
247 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100248 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400249 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400250
251 /*
252 * we made progress, there is more work to do and the bdi
253 * is now congested. Back off and let other work structs
254 * run instead
255 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400256 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500257 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400258 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400259
Chris Masonb765ead2009-04-03 10:27:10 -0400260 ioc = current->io_context;
261
262 /*
263 * the main goal here is that we don't want to
264 * block if we're going to be able to submit
265 * more requests without blocking.
266 *
267 * This code does two great things, it pokes into
268 * the elevator code from a filesystem _and_
269 * it makes assumptions about how batching works.
270 */
271 if (ioc && ioc->nr_batch_requests > 0 &&
272 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
273 (last_waited == 0 ||
274 ioc->last_waited == last_waited)) {
275 /*
276 * we want to go through our batch of
277 * requests and stop. So, we copy out
278 * the ioc->last_waited time and test
279 * against it before looping
280 */
281 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100282 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400283 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400284 continue;
285 }
Chris Mason8b712842008-06-11 16:50:36 -0400286 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400287 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500288 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400289
290 spin_unlock(&device->io_lock);
291 btrfs_requeue_work(&device->work);
292 goto done;
293 }
294 }
Chris Masonffbd5172009-04-20 15:50:09 -0400295
Chris Mason51684082010-03-10 15:33:32 -0500296 cond_resched();
297 if (again)
298 goto loop;
299
300 spin_lock(&device->io_lock);
301 if (device->pending_bios.head || device->pending_sync_bios.head)
302 goto loop_lock;
303 spin_unlock(&device->io_lock);
304
Chris Mason8b712842008-06-11 16:50:36 -0400305done:
Chris Mason211588a2011-04-19 20:12:40 -0400306 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400307 return 0;
308}
309
Christoph Hellwigb2950862008-12-02 09:54:17 -0500310static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400311{
312 struct btrfs_device *device;
313
314 device = container_of(work, struct btrfs_device, work);
315 run_scheduled_bios(device);
316}
317
Chris Masona1b32a52008-09-05 16:09:51 -0400318static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400319 struct btrfs_super_block *disk_super,
320 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
321{
322 struct btrfs_device *device;
323 struct btrfs_fs_devices *fs_devices;
324 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000325 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400326
327 fs_devices = find_fsid(disk_super->fsid);
328 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400329 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 if (!fs_devices)
331 return -ENOMEM;
332 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400333 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400334 list_add(&fs_devices->list, &fs_uuids);
335 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
336 fs_devices->latest_devid = devid;
337 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400338 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400339 device = NULL;
340 } else {
Chris Masona4437552008-04-18 10:29:38 -0400341 device = __find_device(&fs_devices->devices, devid,
342 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 }
344 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500345 if (fs_devices->opened)
346 return -EBUSY;
347
Chris Mason8a4b83c2008-03-24 15:02:07 -0400348 device = kzalloc(sizeof(*device), GFP_NOFS);
349 if (!device) {
350 /* we can safely leave the fs_devices entry around */
351 return -ENOMEM;
352 }
353 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400354 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400355 memcpy(device->uuid, disk_super->dev_item.uuid,
356 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400357 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358 device->name = kstrdup(path, GFP_NOFS);
359 if (!device->name) {
360 kfree(device);
361 return -ENOMEM;
362 }
Yan Zheng2b820322008-11-17 21:11:30 -0500363 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400364
365 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400366 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400367 mutex_unlock(&fs_devices->device_list_mutex);
368
Yan Zheng2b820322008-11-17 21:11:30 -0500369 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500371 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000372 name = kstrdup(path, GFP_NOFS);
373 if (!name)
374 return -ENOMEM;
375 kfree(device->name);
376 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500377 if (device->missing) {
378 fs_devices->missing_devices--;
379 device->missing = 0;
380 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400381 }
382
383 if (found_transid > fs_devices->latest_trans) {
384 fs_devices->latest_devid = devid;
385 fs_devices->latest_trans = found_transid;
386 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400387 *fs_devices_ret = fs_devices;
388 return 0;
389}
390
Yan Zhenge4404d62008-12-12 10:03:26 -0500391static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
392{
393 struct btrfs_fs_devices *fs_devices;
394 struct btrfs_device *device;
395 struct btrfs_device *orig_dev;
396
397 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
398 if (!fs_devices)
399 return ERR_PTR(-ENOMEM);
400
401 INIT_LIST_HEAD(&fs_devices->devices);
402 INIT_LIST_HEAD(&fs_devices->alloc_list);
403 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400404 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500405 fs_devices->latest_devid = orig->latest_devid;
406 fs_devices->latest_trans = orig->latest_trans;
407 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
408
Chris Masone5e9a522009-06-10 15:17:02 -0400409 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500410 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
411 device = kzalloc(sizeof(*device), GFP_NOFS);
412 if (!device)
413 goto error;
414
415 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400416 if (!device->name) {
417 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500418 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400419 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500420
421 device->devid = orig_dev->devid;
422 device->work.func = pending_bios_fn;
423 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500424 spin_lock_init(&device->io_lock);
425 INIT_LIST_HEAD(&device->dev_list);
426 INIT_LIST_HEAD(&device->dev_alloc_list);
427
428 list_add(&device->dev_list, &fs_devices->devices);
429 device->fs_devices = fs_devices;
430 fs_devices->num_devices++;
431 }
Chris Masone5e9a522009-06-10 15:17:02 -0400432 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500433 return fs_devices;
434error:
Chris Masone5e9a522009-06-10 15:17:02 -0400435 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 free_fs_devices(fs_devices);
437 return ERR_PTR(-ENOMEM);
438}
439
Chris Masondfe25022008-05-13 13:46:40 -0400440int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
441{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500442 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400443
444 mutex_lock(&uuid_mutex);
445again:
Chris Masone5e9a522009-06-10 15:17:02 -0400446 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500447 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500448 if (device->in_fs_metadata)
449 continue;
450
451 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100452 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500453 device->bdev = NULL;
454 fs_devices->open_devices--;
455 }
456 if (device->writeable) {
457 list_del_init(&device->dev_alloc_list);
458 device->writeable = 0;
459 fs_devices->rw_devices--;
460 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500461 list_del_init(&device->dev_list);
462 fs_devices->num_devices--;
463 kfree(device->name);
464 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400465 }
Chris Masone5e9a522009-06-10 15:17:02 -0400466 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500467
468 if (fs_devices->seed) {
469 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500470 goto again;
471 }
472
Chris Masondfe25022008-05-13 13:46:40 -0400473 mutex_unlock(&uuid_mutex);
474 return 0;
475}
Chris Masona0af4692008-05-13 16:03:06 -0400476
Yan Zheng2b820322008-11-17 21:11:30 -0500477static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400478{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500480
Yan Zheng2b820322008-11-17 21:11:30 -0500481 if (--fs_devices->opened > 0)
482 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400483
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000484 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500485 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400486 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100487 blkdev_put(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400488 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400489 }
Yan Zheng2b820322008-11-17 21:11:30 -0500490 if (device->writeable) {
491 list_del_init(&device->dev_alloc_list);
492 fs_devices->rw_devices--;
493 }
494
Chris Mason8a4b83c2008-03-24 15:02:07 -0400495 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500496 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400497 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000499 mutex_unlock(&fs_devices->device_list_mutex);
500
Yan Zhenge4404d62008-12-12 10:03:26 -0500501 WARN_ON(fs_devices->open_devices);
502 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500503 fs_devices->opened = 0;
504 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500505
Chris Mason8a4b83c2008-03-24 15:02:07 -0400506 return 0;
507}
508
Yan Zheng2b820322008-11-17 21:11:30 -0500509int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
510{
Yan Zhenge4404d62008-12-12 10:03:26 -0500511 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500512 int ret;
513
514 mutex_lock(&uuid_mutex);
515 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500516 if (!fs_devices->opened) {
517 seed_devices = fs_devices->seed;
518 fs_devices->seed = NULL;
519 }
Yan Zheng2b820322008-11-17 21:11:30 -0500520 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500521
522 while (seed_devices) {
523 fs_devices = seed_devices;
524 seed_devices = fs_devices->seed;
525 __btrfs_close_devices(fs_devices);
526 free_fs_devices(fs_devices);
527 }
Yan Zheng2b820322008-11-17 21:11:30 -0500528 return ret;
529}
530
Yan Zhenge4404d62008-12-12 10:03:26 -0500531static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
532 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533{
534 struct block_device *bdev;
535 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400536 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400537 struct block_device *latest_bdev = NULL;
538 struct buffer_head *bh;
539 struct btrfs_super_block *disk_super;
540 u64 latest_devid = 0;
541 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400542 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500543 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400544 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400545
Tejun Heod4d77622010-11-13 11:55:18 +0100546 flags |= FMODE_EXCL;
547
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500548 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400549 if (device->bdev)
550 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400551 if (!device->name)
552 continue;
553
Tejun Heod4d77622010-11-13 11:55:18 +0100554 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400555 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500556 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400557 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 }
Chris Masona061fc82008-05-07 11:43:44 -0400559 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400560
Yan Zhenga512bbf2008-12-08 16:46:26 -0500561 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000562 if (!bh) {
563 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400564 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000565 }
Chris Masona0af4692008-05-13 16:03:06 -0400566
567 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000568 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400569 if (devid != device->devid)
570 goto error_brelse;
571
Yan Zheng2b820322008-11-17 21:11:30 -0500572 if (memcmp(device->uuid, disk_super->dev_item.uuid,
573 BTRFS_UUID_SIZE))
574 goto error_brelse;
575
576 device->generation = btrfs_super_generation(disk_super);
577 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400578 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500579 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400580 latest_bdev = bdev;
581 }
582
Yan Zheng2b820322008-11-17 21:11:30 -0500583 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
584 device->writeable = 0;
585 } else {
586 device->writeable = !bdev_read_only(bdev);
587 seeding = 0;
588 }
589
Chris Mason8a4b83c2008-03-24 15:02:07 -0400590 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400591 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500592 device->mode = flags;
593
Chris Masonc2898112009-06-10 09:51:32 -0400594 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
595 fs_devices->rotating = 1;
596
Chris Masona0af4692008-05-13 16:03:06 -0400597 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500598 if (device->writeable) {
599 fs_devices->rw_devices++;
600 list_add(&device->dev_alloc_list,
601 &fs_devices->alloc_list);
602 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000603 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400604 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400605
Chris Masona0af4692008-05-13 16:03:06 -0400606error_brelse:
607 brelse(bh);
608error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100609 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400610error:
611 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400612 }
Chris Masona0af4692008-05-13 16:03:06 -0400613 if (fs_devices->open_devices == 0) {
614 ret = -EIO;
615 goto out;
616 }
Yan Zheng2b820322008-11-17 21:11:30 -0500617 fs_devices->seeding = seeding;
618 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400619 fs_devices->latest_bdev = latest_bdev;
620 fs_devices->latest_devid = latest_devid;
621 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500622 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400623out:
Yan Zheng2b820322008-11-17 21:11:30 -0500624 return ret;
625}
626
627int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500628 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500629{
630 int ret;
631
632 mutex_lock(&uuid_mutex);
633 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500634 fs_devices->opened++;
635 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500636 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500637 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500638 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400639 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400640 return ret;
641}
642
Christoph Hellwig97288f22008-12-02 06:36:09 -0500643int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400644 struct btrfs_fs_devices **fs_devices_ret)
645{
646 struct btrfs_super_block *disk_super;
647 struct block_device *bdev;
648 struct buffer_head *bh;
649 int ret;
650 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400651 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400652
653 mutex_lock(&uuid_mutex);
654
Tejun Heod4d77622010-11-13 11:55:18 +0100655 flags |= FMODE_EXCL;
656 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400657
658 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400659 ret = PTR_ERR(bdev);
660 goto error;
661 }
662
663 ret = set_blocksize(bdev, 4096);
664 if (ret)
665 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500666 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400667 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000668 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 goto error_close;
670 }
671 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000672 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400673 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400674 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500675 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400676 else {
677 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500678 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400679 *(unsigned long long *)disk_super->fsid,
680 *(unsigned long long *)(disk_super->fsid + 8));
681 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500682 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500683 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400684 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
685
Chris Mason8a4b83c2008-03-24 15:02:07 -0400686 brelse(bh);
687error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100688 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400689error:
690 mutex_unlock(&uuid_mutex);
691 return ret;
692}
Chris Mason0b86a832008-03-24 15:01:56 -0400693
Miao Xie6d07bce2011-01-05 10:07:31 +0000694/* helper to account the used device space in the range */
695int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
696 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400697{
698 struct btrfs_key key;
699 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000700 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500701 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000702 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400703 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000704 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400705 struct extent_buffer *l;
706
Miao Xie6d07bce2011-01-05 10:07:31 +0000707 *length = 0;
708
709 if (start >= device->total_bytes)
710 return 0;
711
Yan Zheng2b820322008-11-17 21:11:30 -0500712 path = btrfs_alloc_path();
713 if (!path)
714 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400715 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400716
Chris Mason0b86a832008-03-24 15:01:56 -0400717 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000718 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400719 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000720
721 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400722 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000723 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400724 if (ret > 0) {
725 ret = btrfs_previous_item(root, path, key.objectid, key.type);
726 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000727 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400728 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000729
Chris Mason0b86a832008-03-24 15:01:56 -0400730 while (1) {
731 l = path->nodes[0];
732 slot = path->slots[0];
733 if (slot >= btrfs_header_nritems(l)) {
734 ret = btrfs_next_leaf(root, path);
735 if (ret == 0)
736 continue;
737 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000738 goto out;
739
740 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400741 }
742 btrfs_item_key_to_cpu(l, &key, slot);
743
744 if (key.objectid < device->devid)
745 goto next;
746
747 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000748 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400749
Chris Masond3977122009-01-05 21:25:51 -0500750 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400751 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400752
Chris Mason0b86a832008-03-24 15:01:56 -0400753 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000754 extent_end = key.offset + btrfs_dev_extent_length(l,
755 dev_extent);
756 if (key.offset <= start && extent_end > end) {
757 *length = end - start + 1;
758 break;
759 } else if (key.offset <= start && extent_end > start)
760 *length += extent_end - start;
761 else if (key.offset > start && extent_end <= end)
762 *length += extent_end - key.offset;
763 else if (key.offset > start && key.offset <= end) {
764 *length += end - key.offset + 1;
765 break;
766 } else if (key.offset > end)
767 break;
768
769next:
770 path->slots[0]++;
771 }
772 ret = 0;
773out:
774 btrfs_free_path(path);
775 return ret;
776}
777
Chris Mason0b86a832008-03-24 15:01:56 -0400778/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000779 * find_free_dev_extent - find free space in the specified device
780 * @trans: transaction handler
781 * @device: the device which we search the free space in
782 * @num_bytes: the size of the free space that we need
783 * @start: store the start of the free space.
784 * @len: the size of the free space. that we find, or the size of the max
785 * free space if we don't find suitable free space
786 *
Chris Mason0b86a832008-03-24 15:01:56 -0400787 * this uses a pretty simple search, the expectation is that it is
788 * called very infrequently and that a given device has a small number
789 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000790 *
791 * @start is used to store the start of the free space if we find. But if we
792 * don't find suitable free space, it will be used to store the start position
793 * of the max free space.
794 *
795 * @len is used to store the size of the free space that we find.
796 * But if we don't find suitable free space, it is used to store the size of
797 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400798 */
799int find_free_dev_extent(struct btrfs_trans_handle *trans,
800 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000801 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400802{
803 struct btrfs_key key;
804 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000805 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400806 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000807 u64 hole_size;
808 u64 max_hole_start;
809 u64 max_hole_size;
810 u64 extent_end;
811 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400812 u64 search_end = device->total_bytes;
813 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000814 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400815 struct extent_buffer *l;
816
Chris Mason0b86a832008-03-24 15:01:56 -0400817 /* FIXME use last free of some kind */
818
819 /* we don't want to overwrite the superblock on the drive,
820 * so we make sure to start at an offset of at least 1MB
821 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000822 search_start = 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400823
Miao Xie7bfc8372011-01-05 10:07:26 +0000824 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason0b86a832008-03-24 15:01:56 -0400825 search_start = max(root->fs_info->alloc_start, search_start);
826
Miao Xie7bfc8372011-01-05 10:07:26 +0000827 max_hole_start = search_start;
828 max_hole_size = 0;
829
830 if (search_start >= search_end) {
831 ret = -ENOSPC;
832 goto error;
833 }
834
835 path = btrfs_alloc_path();
836 if (!path) {
837 ret = -ENOMEM;
838 goto error;
839 }
840 path->reada = 2;
841
Chris Mason0b86a832008-03-24 15:01:56 -0400842 key.objectid = device->devid;
843 key.offset = search_start;
844 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000845
Chris Mason0b86a832008-03-24 15:01:56 -0400846 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
847 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400849 if (ret > 0) {
850 ret = btrfs_previous_item(root, path, key.objectid, key.type);
851 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000852 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400853 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000854
Chris Mason0b86a832008-03-24 15:01:56 -0400855 while (1) {
856 l = path->nodes[0];
857 slot = path->slots[0];
858 if (slot >= btrfs_header_nritems(l)) {
859 ret = btrfs_next_leaf(root, path);
860 if (ret == 0)
861 continue;
862 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000863 goto out;
864
865 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400866 }
867 btrfs_item_key_to_cpu(l, &key, slot);
868
869 if (key.objectid < device->devid)
870 goto next;
871
872 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000873 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400874
Chris Mason0b86a832008-03-24 15:01:56 -0400875 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
876 goto next;
877
Miao Xie7bfc8372011-01-05 10:07:26 +0000878 if (key.offset > search_start) {
879 hole_size = key.offset - search_start;
880
881 if (hole_size > max_hole_size) {
882 max_hole_start = search_start;
883 max_hole_size = hole_size;
884 }
885
886 /*
887 * If this free space is greater than which we need,
888 * it must be the max free space that we have found
889 * until now, so max_hole_start must point to the start
890 * of this free space and the length of this free space
891 * is stored in max_hole_size. Thus, we return
892 * max_hole_start and max_hole_size and go back to the
893 * caller.
894 */
895 if (hole_size >= num_bytes) {
896 ret = 0;
897 goto out;
898 }
899 }
900
Chris Mason0b86a832008-03-24 15:01:56 -0400901 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000902 extent_end = key.offset + btrfs_dev_extent_length(l,
903 dev_extent);
904 if (extent_end > search_start)
905 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400906next:
907 path->slots[0]++;
908 cond_resched();
909 }
Chris Mason0b86a832008-03-24 15:01:56 -0400910
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 hole_size = search_end- search_start;
912 if (hole_size > max_hole_size) {
913 max_hole_start = search_start;
914 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400915 }
Chris Mason0b86a832008-03-24 15:01:56 -0400916
Miao Xie7bfc8372011-01-05 10:07:26 +0000917 /* See above. */
918 if (hole_size < num_bytes)
919 ret = -ENOSPC;
920 else
921 ret = 0;
922
923out:
Yan Zheng2b820322008-11-17 21:11:30 -0500924 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000925error:
926 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000927 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000928 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400929 return ret;
930}
931
Christoph Hellwigb2950862008-12-02 09:54:17 -0500932static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400933 struct btrfs_device *device,
934 u64 start)
935{
936 int ret;
937 struct btrfs_path *path;
938 struct btrfs_root *root = device->dev_root;
939 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400940 struct btrfs_key found_key;
941 struct extent_buffer *leaf = NULL;
942 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400943
944 path = btrfs_alloc_path();
945 if (!path)
946 return -ENOMEM;
947
948 key.objectid = device->devid;
949 key.offset = start;
950 key.type = BTRFS_DEV_EXTENT_KEY;
951
952 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400953 if (ret > 0) {
954 ret = btrfs_previous_item(root, path, key.objectid,
955 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000956 if (ret)
957 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400958 leaf = path->nodes[0];
959 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
960 extent = btrfs_item_ptr(leaf, path->slots[0],
961 struct btrfs_dev_extent);
962 BUG_ON(found_key.offset > start || found_key.offset +
963 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400964 } else if (ret == 0) {
965 leaf = path->nodes[0];
966 extent = btrfs_item_ptr(leaf, path->slots[0],
967 struct btrfs_dev_extent);
968 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400969 BUG_ON(ret);
970
Chris Masondfe25022008-05-13 13:46:40 -0400971 if (device->bytes_used > 0)
972 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400973 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -0400974
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000975out:
Chris Mason8f18cf12008-04-25 16:53:30 -0400976 btrfs_free_path(path);
977 return ret;
978}
979
Yan Zheng2b820322008-11-17 21:11:30 -0500980int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400981 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400982 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500983 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400984{
985 int ret;
986 struct btrfs_path *path;
987 struct btrfs_root *root = device->dev_root;
988 struct btrfs_dev_extent *extent;
989 struct extent_buffer *leaf;
990 struct btrfs_key key;
991
Chris Masondfe25022008-05-13 13:46:40 -0400992 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400993 path = btrfs_alloc_path();
994 if (!path)
995 return -ENOMEM;
996
Chris Mason0b86a832008-03-24 15:01:56 -0400997 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500998 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400999 key.type = BTRFS_DEV_EXTENT_KEY;
1000 ret = btrfs_insert_empty_item(trans, root, path, &key,
1001 sizeof(*extent));
1002 BUG_ON(ret);
1003
1004 leaf = path->nodes[0];
1005 extent = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001007 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1008 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1009 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1010
1011 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1012 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1013 BTRFS_UUID_SIZE);
1014
Chris Mason0b86a832008-03-24 15:01:56 -04001015 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1016 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001017 btrfs_free_path(path);
1018 return ret;
1019}
1020
Chris Masona1b32a52008-09-05 16:09:51 -04001021static noinline int find_next_chunk(struct btrfs_root *root,
1022 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001023{
1024 struct btrfs_path *path;
1025 int ret;
1026 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001027 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001028 struct btrfs_key found_key;
1029
1030 path = btrfs_alloc_path();
1031 BUG_ON(!path);
1032
Chris Masone17cade2008-04-15 15:41:47 -04001033 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001034 key.offset = (u64)-1;
1035 key.type = BTRFS_CHUNK_ITEM_KEY;
1036
1037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1038 if (ret < 0)
1039 goto error;
1040
1041 BUG_ON(ret == 0);
1042
1043 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1044 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001045 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001046 } else {
1047 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1048 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001049 if (found_key.objectid != objectid)
1050 *offset = 0;
1051 else {
1052 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1053 struct btrfs_chunk);
1054 *offset = found_key.offset +
1055 btrfs_chunk_length(path->nodes[0], chunk);
1056 }
Chris Mason0b86a832008-03-24 15:01:56 -04001057 }
1058 ret = 0;
1059error:
1060 btrfs_free_path(path);
1061 return ret;
1062}
1063
Yan Zheng2b820322008-11-17 21:11:30 -05001064static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001065{
1066 int ret;
1067 struct btrfs_key key;
1068 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001069 struct btrfs_path *path;
1070
1071 root = root->fs_info->chunk_root;
1072
1073 path = btrfs_alloc_path();
1074 if (!path)
1075 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001076
1077 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1078 key.type = BTRFS_DEV_ITEM_KEY;
1079 key.offset = (u64)-1;
1080
1081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1082 if (ret < 0)
1083 goto error;
1084
1085 BUG_ON(ret == 0);
1086
1087 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1088 BTRFS_DEV_ITEM_KEY);
1089 if (ret) {
1090 *objectid = 1;
1091 } else {
1092 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1093 path->slots[0]);
1094 *objectid = found_key.offset + 1;
1095 }
1096 ret = 0;
1097error:
Yan Zheng2b820322008-11-17 21:11:30 -05001098 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001099 return ret;
1100}
1101
1102/*
1103 * the device information is stored in the chunk root
1104 * the btrfs_device struct should be fully filled in
1105 */
1106int btrfs_add_device(struct btrfs_trans_handle *trans,
1107 struct btrfs_root *root,
1108 struct btrfs_device *device)
1109{
1110 int ret;
1111 struct btrfs_path *path;
1112 struct btrfs_dev_item *dev_item;
1113 struct extent_buffer *leaf;
1114 struct btrfs_key key;
1115 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001116
1117 root = root->fs_info->chunk_root;
1118
1119 path = btrfs_alloc_path();
1120 if (!path)
1121 return -ENOMEM;
1122
Chris Mason0b86a832008-03-24 15:01:56 -04001123 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1124 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001125 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001126
1127 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001128 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001129 if (ret)
1130 goto out;
1131
1132 leaf = path->nodes[0];
1133 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1134
1135 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001136 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001137 btrfs_set_device_type(leaf, dev_item, device->type);
1138 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1139 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1140 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001141 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1142 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001143 btrfs_set_device_group(leaf, dev_item, 0);
1144 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1145 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001146 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001147
Chris Mason0b86a832008-03-24 15:01:56 -04001148 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001149 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001150 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1151 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001152 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001153
Yan Zheng2b820322008-11-17 21:11:30 -05001154 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001155out:
1156 btrfs_free_path(path);
1157 return ret;
1158}
Chris Mason8f18cf12008-04-25 16:53:30 -04001159
Chris Masona061fc82008-05-07 11:43:44 -04001160static int btrfs_rm_dev_item(struct btrfs_root *root,
1161 struct btrfs_device *device)
1162{
1163 int ret;
1164 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001165 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001166 struct btrfs_trans_handle *trans;
1167
1168 root = root->fs_info->chunk_root;
1169
1170 path = btrfs_alloc_path();
1171 if (!path)
1172 return -ENOMEM;
1173
Yan, Zhenga22285a2010-05-16 10:48:46 -04001174 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001175 if (IS_ERR(trans)) {
1176 btrfs_free_path(path);
1177 return PTR_ERR(trans);
1178 }
Chris Masona061fc82008-05-07 11:43:44 -04001179 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1180 key.type = BTRFS_DEV_ITEM_KEY;
1181 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001182 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001183
1184 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1185 if (ret < 0)
1186 goto out;
1187
1188 if (ret > 0) {
1189 ret = -ENOENT;
1190 goto out;
1191 }
1192
1193 ret = btrfs_del_item(trans, root, path);
1194 if (ret)
1195 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001196out:
1197 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001198 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001199 btrfs_commit_transaction(trans, root);
1200 return ret;
1201}
1202
1203int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1204{
1205 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001206 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001207 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001208 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001209 struct btrfs_super_block *disk_super;
1210 u64 all_avail;
1211 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001212 u64 num_devices;
1213 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001214 int ret = 0;
1215
Chris Masona061fc82008-05-07 11:43:44 -04001216 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001217 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001218
1219 all_avail = root->fs_info->avail_data_alloc_bits |
1220 root->fs_info->avail_system_alloc_bits |
1221 root->fs_info->avail_metadata_alloc_bits;
1222
1223 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001224 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001225 printk(KERN_ERR "btrfs: unable to go below four devices "
1226 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001227 ret = -EINVAL;
1228 goto out;
1229 }
1230
1231 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001232 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001233 printk(KERN_ERR "btrfs: unable to go below two "
1234 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001235 ret = -EINVAL;
1236 goto out;
1237 }
1238
Chris Masondfe25022008-05-13 13:46:40 -04001239 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001240 struct list_head *devices;
1241 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001242
Chris Masondfe25022008-05-13 13:46:40 -04001243 device = NULL;
1244 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001245 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001246 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001247 if (tmp->in_fs_metadata && !tmp->bdev) {
1248 device = tmp;
1249 break;
1250 }
1251 }
Chris Masone5e9a522009-06-10 15:17:02 -04001252 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001253 bdev = NULL;
1254 bh = NULL;
1255 disk_super = NULL;
1256 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001257 printk(KERN_ERR "btrfs: no missing devices found to "
1258 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001259 goto out;
1260 }
Chris Masondfe25022008-05-13 13:46:40 -04001261 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001262 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1263 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001264 if (IS_ERR(bdev)) {
1265 ret = PTR_ERR(bdev);
1266 goto out;
1267 }
1268
Yan Zheng2b820322008-11-17 21:11:30 -05001269 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001270 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001271 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001272 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001273 goto error_close;
1274 }
1275 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001276 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001277 dev_uuid = disk_super->dev_item.uuid;
1278 device = btrfs_find_device(root, devid, dev_uuid,
1279 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001280 if (!device) {
1281 ret = -ENOENT;
1282 goto error_brelse;
1283 }
Chris Masondfe25022008-05-13 13:46:40 -04001284 }
Yan Zheng2b820322008-11-17 21:11:30 -05001285
1286 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001287 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1288 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001289 ret = -EINVAL;
1290 goto error_brelse;
1291 }
1292
1293 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001294 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001295 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001296 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001297 root->fs_info->fs_devices->rw_devices--;
1298 }
Chris Masona061fc82008-05-07 11:43:44 -04001299
1300 ret = btrfs_shrink_device(device, 0);
1301 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001302 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001303
Chris Masona061fc82008-05-07 11:43:44 -04001304 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1305 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001306 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001307
Yan Zheng2b820322008-11-17 21:11:30 -05001308 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001309
1310 /*
1311 * the device list mutex makes sure that we don't change
1312 * the device list while someone else is writing out all
1313 * the device supers.
1314 */
1315 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001316 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001317 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1318
Yan Zhenge4404d62008-12-12 10:03:26 -05001319 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001320
Chris Masoncd02dca2010-12-13 14:56:23 -05001321 if (device->missing)
1322 root->fs_info->fs_devices->missing_devices--;
1323
Yan Zheng2b820322008-11-17 21:11:30 -05001324 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1325 struct btrfs_device, dev_list);
1326 if (device->bdev == root->fs_info->sb->s_bdev)
1327 root->fs_info->sb->s_bdev = next_device->bdev;
1328 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1329 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1330
Yan Zhenge4404d62008-12-12 10:03:26 -05001331 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001332 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001333 device->bdev = NULL;
1334 device->fs_devices->open_devices--;
1335 }
1336
Yan Zheng2b820322008-11-17 21:11:30 -05001337 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1338 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1339
Yan Zhenge4404d62008-12-12 10:03:26 -05001340 if (device->fs_devices->open_devices == 0) {
1341 struct btrfs_fs_devices *fs_devices;
1342 fs_devices = root->fs_info->fs_devices;
1343 while (fs_devices) {
1344 if (fs_devices->seed == device->fs_devices)
1345 break;
1346 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001347 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001348 fs_devices->seed = device->fs_devices->seed;
1349 device->fs_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001350 lock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05001351 __btrfs_close_devices(device->fs_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001352 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05001353 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001354 }
1355
1356 /*
1357 * at this point, the device is zero sized. We want to
1358 * remove it from the devices list and zero out the old super
1359 */
1360 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001361 /* make sure this device isn't detected as part of
1362 * the FS anymore
1363 */
1364 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1365 set_buffer_dirty(bh);
1366 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001367 }
Chris Masona061fc82008-05-07 11:43:44 -04001368
Chris Masona061fc82008-05-07 11:43:44 -04001369 kfree(device->name);
1370 kfree(device);
1371 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001372
1373error_brelse:
1374 brelse(bh);
1375error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001376 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001377 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001378out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001379 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001380 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001381 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001382error_undo:
1383 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001384 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001385 list_add(&device->dev_alloc_list,
1386 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001387 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001388 root->fs_info->fs_devices->rw_devices++;
1389 }
1390 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001391}
1392
Yan Zheng2b820322008-11-17 21:11:30 -05001393/*
1394 * does all the dirty work required for changing file system's UUID.
1395 */
1396static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1397 struct btrfs_root *root)
1398{
1399 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1400 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001401 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001402 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1403 struct btrfs_device *device;
1404 u64 super_flags;
1405
1406 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001407 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001408 return -EINVAL;
1409
Yan Zhenge4404d62008-12-12 10:03:26 -05001410 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1411 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001412 return -ENOMEM;
1413
Yan Zhenge4404d62008-12-12 10:03:26 -05001414 old_devices = clone_fs_devices(fs_devices);
1415 if (IS_ERR(old_devices)) {
1416 kfree(seed_devices);
1417 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001418 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001419
Yan Zheng2b820322008-11-17 21:11:30 -05001420 list_add(&old_devices->list, &fs_uuids);
1421
Yan Zhenge4404d62008-12-12 10:03:26 -05001422 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1423 seed_devices->opened = 1;
1424 INIT_LIST_HEAD(&seed_devices->devices);
1425 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001426 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001427
1428 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001429 list_splice_init(&fs_devices->devices, &seed_devices->devices);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001430 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1431
Yan Zhenge4404d62008-12-12 10:03:26 -05001432 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1433 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1434 device->fs_devices = seed_devices;
1435 }
1436
Yan Zheng2b820322008-11-17 21:11:30 -05001437 fs_devices->seeding = 0;
1438 fs_devices->num_devices = 0;
1439 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001440 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001441
1442 generate_random_uuid(fs_devices->fsid);
1443 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1444 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1445 super_flags = btrfs_super_flags(disk_super) &
1446 ~BTRFS_SUPER_FLAG_SEEDING;
1447 btrfs_set_super_flags(disk_super, super_flags);
1448
1449 return 0;
1450}
1451
1452/*
1453 * strore the expected generation for seed devices in device items.
1454 */
1455static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root)
1457{
1458 struct btrfs_path *path;
1459 struct extent_buffer *leaf;
1460 struct btrfs_dev_item *dev_item;
1461 struct btrfs_device *device;
1462 struct btrfs_key key;
1463 u8 fs_uuid[BTRFS_UUID_SIZE];
1464 u8 dev_uuid[BTRFS_UUID_SIZE];
1465 u64 devid;
1466 int ret;
1467
1468 path = btrfs_alloc_path();
1469 if (!path)
1470 return -ENOMEM;
1471
1472 root = root->fs_info->chunk_root;
1473 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1474 key.offset = 0;
1475 key.type = BTRFS_DEV_ITEM_KEY;
1476
1477 while (1) {
1478 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1479 if (ret < 0)
1480 goto error;
1481
1482 leaf = path->nodes[0];
1483next_slot:
1484 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1485 ret = btrfs_next_leaf(root, path);
1486 if (ret > 0)
1487 break;
1488 if (ret < 0)
1489 goto error;
1490 leaf = path->nodes[0];
1491 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1492 btrfs_release_path(root, path);
1493 continue;
1494 }
1495
1496 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1497 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1498 key.type != BTRFS_DEV_ITEM_KEY)
1499 break;
1500
1501 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1502 struct btrfs_dev_item);
1503 devid = btrfs_device_id(leaf, dev_item);
1504 read_extent_buffer(leaf, dev_uuid,
1505 (unsigned long)btrfs_device_uuid(dev_item),
1506 BTRFS_UUID_SIZE);
1507 read_extent_buffer(leaf, fs_uuid,
1508 (unsigned long)btrfs_device_fsid(dev_item),
1509 BTRFS_UUID_SIZE);
1510 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1511 BUG_ON(!device);
1512
1513 if (device->fs_devices->seeding) {
1514 btrfs_set_device_generation(leaf, dev_item,
1515 device->generation);
1516 btrfs_mark_buffer_dirty(leaf);
1517 }
1518
1519 path->slots[0]++;
1520 goto next_slot;
1521 }
1522 ret = 0;
1523error:
1524 btrfs_free_path(path);
1525 return ret;
1526}
1527
Chris Mason788f20e2008-04-28 15:29:42 -04001528int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1529{
1530 struct btrfs_trans_handle *trans;
1531 struct btrfs_device *device;
1532 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001533 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001534 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001535 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001536 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001537 int ret = 0;
1538
Yan Zheng2b820322008-11-17 21:11:30 -05001539 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1540 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001541
Tejun Heod4d77622010-11-13 11:55:18 +01001542 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1543 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001544 if (IS_ERR(bdev))
1545 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001546
Yan Zheng2b820322008-11-17 21:11:30 -05001547 if (root->fs_info->fs_devices->seeding) {
1548 seeding_dev = 1;
1549 down_write(&sb->s_umount);
1550 mutex_lock(&uuid_mutex);
1551 }
1552
Chris Mason8c8bee1d2008-09-29 11:19:10 -04001553 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001554 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001555
Chris Mason788f20e2008-04-28 15:29:42 -04001556 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001557 /*
1558 * we have the volume lock, so we don't need the extra
1559 * device list mutex while reading the list here.
1560 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001561 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001562 if (device->bdev == bdev) {
1563 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001564 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001565 }
1566 }
1567
1568 device = kzalloc(sizeof(*device), GFP_NOFS);
1569 if (!device) {
1570 /* we can safely leave the fs_devices entry around */
1571 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001572 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001573 }
1574
Chris Mason788f20e2008-04-28 15:29:42 -04001575 device->name = kstrdup(device_path, GFP_NOFS);
1576 if (!device->name) {
1577 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001578 ret = -ENOMEM;
1579 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001580 }
Yan Zheng2b820322008-11-17 21:11:30 -05001581
1582 ret = find_next_devid(root, &device->devid);
1583 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001584 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001585 kfree(device);
1586 goto error;
1587 }
1588
Yan, Zhenga22285a2010-05-16 10:48:46 -04001589 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001590 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001591 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001592 kfree(device);
1593 ret = PTR_ERR(trans);
1594 goto error;
1595 }
1596
Yan Zheng2b820322008-11-17 21:11:30 -05001597 lock_chunks(root);
1598
Yan Zheng2b820322008-11-17 21:11:30 -05001599 device->writeable = 1;
1600 device->work.func = pending_bios_fn;
1601 generate_random_uuid(device->uuid);
1602 spin_lock_init(&device->io_lock);
1603 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001604 device->io_width = root->sectorsize;
1605 device->io_align = root->sectorsize;
1606 device->sector_size = root->sectorsize;
1607 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001608 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001609 device->dev_root = root->fs_info->dev_root;
1610 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001611 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001612 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001613 set_blocksize(device->bdev, 4096);
1614
Yan Zheng2b820322008-11-17 21:11:30 -05001615 if (seeding_dev) {
1616 sb->s_flags &= ~MS_RDONLY;
1617 ret = btrfs_prepare_sprout(trans, root);
1618 BUG_ON(ret);
1619 }
1620
1621 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001622
1623 /*
1624 * we don't want write_supers to jump in here with our device
1625 * half setup
1626 */
1627 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001628 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1629 list_add(&device->dev_alloc_list,
1630 &root->fs_info->fs_devices->alloc_list);
1631 root->fs_info->fs_devices->num_devices++;
1632 root->fs_info->fs_devices->open_devices++;
1633 root->fs_info->fs_devices->rw_devices++;
1634 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1635
Chris Masonc2898112009-06-10 09:51:32 -04001636 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1637 root->fs_info->fs_devices->rotating = 1;
1638
Chris Mason788f20e2008-04-28 15:29:42 -04001639 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1640 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1641 total_bytes + device->total_bytes);
1642
1643 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1644 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1645 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001646 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001647
Yan Zheng2b820322008-11-17 21:11:30 -05001648 if (seeding_dev) {
1649 ret = init_first_rw_device(trans, root, device);
1650 BUG_ON(ret);
1651 ret = btrfs_finish_sprout(trans, root);
1652 BUG_ON(ret);
1653 } else {
1654 ret = btrfs_add_device(trans, root, device);
1655 }
1656
Chris Mason913d9522009-03-10 13:17:18 -04001657 /*
1658 * we've got more storage, clear any full flags on the space
1659 * infos
1660 */
1661 btrfs_clear_space_info_full(root->fs_info);
1662
Chris Mason7d9eb122008-07-08 14:19:17 -04001663 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001664 btrfs_commit_transaction(trans, root);
1665
1666 if (seeding_dev) {
1667 mutex_unlock(&uuid_mutex);
1668 up_write(&sb->s_umount);
1669
1670 ret = btrfs_relocate_sys_chunks(root);
1671 BUG_ON(ret);
1672 }
1673out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001674 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001675 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001676error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001677 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001678 if (seeding_dev) {
1679 mutex_unlock(&uuid_mutex);
1680 up_write(&sb->s_umount);
1681 }
Chris Mason788f20e2008-04-28 15:29:42 -04001682 goto out;
1683}
1684
Chris Masond3977122009-01-05 21:25:51 -05001685static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1686 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001687{
1688 int ret;
1689 struct btrfs_path *path;
1690 struct btrfs_root *root;
1691 struct btrfs_dev_item *dev_item;
1692 struct extent_buffer *leaf;
1693 struct btrfs_key key;
1694
1695 root = device->dev_root->fs_info->chunk_root;
1696
1697 path = btrfs_alloc_path();
1698 if (!path)
1699 return -ENOMEM;
1700
1701 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1702 key.type = BTRFS_DEV_ITEM_KEY;
1703 key.offset = device->devid;
1704
1705 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1706 if (ret < 0)
1707 goto out;
1708
1709 if (ret > 0) {
1710 ret = -ENOENT;
1711 goto out;
1712 }
1713
1714 leaf = path->nodes[0];
1715 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1716
1717 btrfs_set_device_id(leaf, dev_item, device->devid);
1718 btrfs_set_device_type(leaf, dev_item, device->type);
1719 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1720 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1721 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001722 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001723 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1724 btrfs_mark_buffer_dirty(leaf);
1725
1726out:
1727 btrfs_free_path(path);
1728 return ret;
1729}
1730
Chris Mason7d9eb122008-07-08 14:19:17 -04001731static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001732 struct btrfs_device *device, u64 new_size)
1733{
1734 struct btrfs_super_block *super_copy =
1735 &device->dev_root->fs_info->super_copy;
1736 u64 old_total = btrfs_super_total_bytes(super_copy);
1737 u64 diff = new_size - device->total_bytes;
1738
Yan Zheng2b820322008-11-17 21:11:30 -05001739 if (!device->writeable)
1740 return -EACCES;
1741 if (new_size <= device->total_bytes)
1742 return -EINVAL;
1743
Chris Mason8f18cf12008-04-25 16:53:30 -04001744 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001745 device->fs_devices->total_rw_bytes += diff;
1746
1747 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001748 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001749 btrfs_clear_space_info_full(device->dev_root->fs_info);
1750
Chris Mason8f18cf12008-04-25 16:53:30 -04001751 return btrfs_update_device(trans, device);
1752}
1753
Chris Mason7d9eb122008-07-08 14:19:17 -04001754int btrfs_grow_device(struct btrfs_trans_handle *trans,
1755 struct btrfs_device *device, u64 new_size)
1756{
1757 int ret;
1758 lock_chunks(device->dev_root);
1759 ret = __btrfs_grow_device(trans, device, new_size);
1760 unlock_chunks(device->dev_root);
1761 return ret;
1762}
1763
Chris Mason8f18cf12008-04-25 16:53:30 -04001764static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1765 struct btrfs_root *root,
1766 u64 chunk_tree, u64 chunk_objectid,
1767 u64 chunk_offset)
1768{
1769 int ret;
1770 struct btrfs_path *path;
1771 struct btrfs_key key;
1772
1773 root = root->fs_info->chunk_root;
1774 path = btrfs_alloc_path();
1775 if (!path)
1776 return -ENOMEM;
1777
1778 key.objectid = chunk_objectid;
1779 key.offset = chunk_offset;
1780 key.type = BTRFS_CHUNK_ITEM_KEY;
1781
1782 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1783 BUG_ON(ret);
1784
1785 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001786
1787 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001788 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001789}
1790
Christoph Hellwigb2950862008-12-02 09:54:17 -05001791static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001792 chunk_offset)
1793{
1794 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1795 struct btrfs_disk_key *disk_key;
1796 struct btrfs_chunk *chunk;
1797 u8 *ptr;
1798 int ret = 0;
1799 u32 num_stripes;
1800 u32 array_size;
1801 u32 len = 0;
1802 u32 cur;
1803 struct btrfs_key key;
1804
1805 array_size = btrfs_super_sys_array_size(super_copy);
1806
1807 ptr = super_copy->sys_chunk_array;
1808 cur = 0;
1809
1810 while (cur < array_size) {
1811 disk_key = (struct btrfs_disk_key *)ptr;
1812 btrfs_disk_key_to_cpu(&key, disk_key);
1813
1814 len = sizeof(*disk_key);
1815
1816 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1817 chunk = (struct btrfs_chunk *)(ptr + len);
1818 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1819 len += btrfs_chunk_item_size(num_stripes);
1820 } else {
1821 ret = -EIO;
1822 break;
1823 }
1824 if (key.objectid == chunk_objectid &&
1825 key.offset == chunk_offset) {
1826 memmove(ptr, ptr + len, array_size - (cur + len));
1827 array_size -= len;
1828 btrfs_set_super_sys_array_size(super_copy, array_size);
1829 } else {
1830 ptr += len;
1831 cur += len;
1832 }
1833 }
1834 return ret;
1835}
1836
Christoph Hellwigb2950862008-12-02 09:54:17 -05001837static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001838 u64 chunk_tree, u64 chunk_objectid,
1839 u64 chunk_offset)
1840{
1841 struct extent_map_tree *em_tree;
1842 struct btrfs_root *extent_root;
1843 struct btrfs_trans_handle *trans;
1844 struct extent_map *em;
1845 struct map_lookup *map;
1846 int ret;
1847 int i;
1848
1849 root = root->fs_info->chunk_root;
1850 extent_root = root->fs_info->extent_root;
1851 em_tree = &root->fs_info->mapping_tree.map_tree;
1852
Josef Bacikba1bf482009-09-11 16:11:19 -04001853 ret = btrfs_can_relocate(extent_root, chunk_offset);
1854 if (ret)
1855 return -ENOSPC;
1856
Chris Mason8f18cf12008-04-25 16:53:30 -04001857 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001858 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001859 if (ret)
1860 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001861
Yan, Zhenga22285a2010-05-16 10:48:46 -04001862 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001863 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001864
Chris Mason7d9eb122008-07-08 14:19:17 -04001865 lock_chunks(root);
1866
Chris Mason8f18cf12008-04-25 16:53:30 -04001867 /*
1868 * step two, delete the device extents and the
1869 * chunk tree entries
1870 */
Chris Mason890871b2009-09-02 16:24:52 -04001871 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001872 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001873 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001874
Chris Masona061fc82008-05-07 11:43:44 -04001875 BUG_ON(em->start > chunk_offset ||
1876 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001877 map = (struct map_lookup *)em->bdev;
1878
1879 for (i = 0; i < map->num_stripes; i++) {
1880 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1881 map->stripes[i].physical);
1882 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001883
Chris Masondfe25022008-05-13 13:46:40 -04001884 if (map->stripes[i].dev) {
1885 ret = btrfs_update_device(trans, map->stripes[i].dev);
1886 BUG_ON(ret);
1887 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001888 }
1889 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1890 chunk_offset);
1891
1892 BUG_ON(ret);
1893
liubo1abe9b82011-03-24 11:18:59 +00001894 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1895
Chris Mason8f18cf12008-04-25 16:53:30 -04001896 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1897 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1898 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001899 }
1900
Zheng Yan1a40e232008-09-26 10:09:34 -04001901 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1902 BUG_ON(ret);
1903
Chris Mason890871b2009-09-02 16:24:52 -04001904 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001905 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001906 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001907
Chris Mason8f18cf12008-04-25 16:53:30 -04001908 kfree(map);
1909 em->bdev = NULL;
1910
1911 /* once for the tree */
1912 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001913 /* once for us */
1914 free_extent_map(em);
1915
Chris Mason7d9eb122008-07-08 14:19:17 -04001916 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001917 btrfs_end_transaction(trans, root);
1918 return 0;
1919}
1920
Yan Zheng2b820322008-11-17 21:11:30 -05001921static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1922{
1923 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1924 struct btrfs_path *path;
1925 struct extent_buffer *leaf;
1926 struct btrfs_chunk *chunk;
1927 struct btrfs_key key;
1928 struct btrfs_key found_key;
1929 u64 chunk_tree = chunk_root->root_key.objectid;
1930 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001931 bool retried = false;
1932 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001933 int ret;
1934
1935 path = btrfs_alloc_path();
1936 if (!path)
1937 return -ENOMEM;
1938
Josef Bacikba1bf482009-09-11 16:11:19 -04001939again:
Yan Zheng2b820322008-11-17 21:11:30 -05001940 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1941 key.offset = (u64)-1;
1942 key.type = BTRFS_CHUNK_ITEM_KEY;
1943
1944 while (1) {
1945 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1946 if (ret < 0)
1947 goto error;
1948 BUG_ON(ret == 0);
1949
1950 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1951 key.type);
1952 if (ret < 0)
1953 goto error;
1954 if (ret > 0)
1955 break;
1956
1957 leaf = path->nodes[0];
1958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1959
1960 chunk = btrfs_item_ptr(leaf, path->slots[0],
1961 struct btrfs_chunk);
1962 chunk_type = btrfs_chunk_type(leaf, chunk);
1963 btrfs_release_path(chunk_root, path);
1964
1965 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1966 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1967 found_key.objectid,
1968 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001969 if (ret == -ENOSPC)
1970 failed++;
1971 else if (ret)
1972 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001973 }
1974
1975 if (found_key.offset == 0)
1976 break;
1977 key.offset = found_key.offset - 1;
1978 }
1979 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001980 if (failed && !retried) {
1981 failed = 0;
1982 retried = true;
1983 goto again;
1984 } else if (failed && retried) {
1985 WARN_ON(1);
1986 ret = -ENOSPC;
1987 }
Yan Zheng2b820322008-11-17 21:11:30 -05001988error:
1989 btrfs_free_path(path);
1990 return ret;
1991}
1992
Chris Masonec44a352008-04-28 15:29:52 -04001993static u64 div_factor(u64 num, int factor)
1994{
1995 if (factor == 10)
1996 return num;
1997 num *= factor;
1998 do_div(num, 10);
1999 return num;
2000}
2001
Chris Masonec44a352008-04-28 15:29:52 -04002002int btrfs_balance(struct btrfs_root *dev_root)
2003{
2004 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002005 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2006 struct btrfs_device *device;
2007 u64 old_size;
2008 u64 size_to_free;
2009 struct btrfs_path *path;
2010 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002011 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2012 struct btrfs_trans_handle *trans;
2013 struct btrfs_key found_key;
2014
Yan Zheng2b820322008-11-17 21:11:30 -05002015 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2016 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002017
Ben Hutchings6f88a442010-12-29 14:55:03 +00002018 if (!capable(CAP_SYS_ADMIN))
2019 return -EPERM;
2020
Chris Mason7d9eb122008-07-08 14:19:17 -04002021 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002022 dev_root = dev_root->fs_info->dev_root;
2023
Chris Masonec44a352008-04-28 15:29:52 -04002024 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002025 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002026 old_size = device->total_bytes;
2027 size_to_free = div_factor(old_size, 1);
2028 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002029 if (!device->writeable ||
2030 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002031 continue;
2032
2033 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002034 if (ret == -ENOSPC)
2035 break;
Chris Masonec44a352008-04-28 15:29:52 -04002036 BUG_ON(ret);
2037
Yan, Zhenga22285a2010-05-16 10:48:46 -04002038 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002039 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002040
2041 ret = btrfs_grow_device(trans, device, old_size);
2042 BUG_ON(ret);
2043
2044 btrfs_end_transaction(trans, dev_root);
2045 }
2046
2047 /* step two, relocate all the chunks */
2048 path = btrfs_alloc_path();
2049 BUG_ON(!path);
2050
2051 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2052 key.offset = (u64)-1;
2053 key.type = BTRFS_CHUNK_ITEM_KEY;
2054
Chris Masond3977122009-01-05 21:25:51 -05002055 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002056 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2057 if (ret < 0)
2058 goto error;
2059
2060 /*
2061 * this shouldn't happen, it means the last relocate
2062 * failed
2063 */
2064 if (ret == 0)
2065 break;
2066
2067 ret = btrfs_previous_item(chunk_root, path, 0,
2068 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002069 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002070 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002071
Chris Masonec44a352008-04-28 15:29:52 -04002072 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2073 path->slots[0]);
2074 if (found_key.objectid != key.objectid)
2075 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002076
Chris Masonec44a352008-04-28 15:29:52 -04002077 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002078 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002079 break;
2080
Chris Mason7d9eb122008-07-08 14:19:17 -04002081 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002082 ret = btrfs_relocate_chunk(chunk_root,
2083 chunk_root->root_key.objectid,
2084 found_key.objectid,
2085 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002086 BUG_ON(ret && ret != -ENOSPC);
2087 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002088 }
2089 ret = 0;
2090error:
2091 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002092 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002093 return ret;
2094}
2095
Chris Mason8f18cf12008-04-25 16:53:30 -04002096/*
2097 * shrinking a device means finding all of the device extents past
2098 * the new size, and then following the back refs to the chunks.
2099 * The chunk relocation code actually frees the device extent
2100 */
2101int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2102{
2103 struct btrfs_trans_handle *trans;
2104 struct btrfs_root *root = device->dev_root;
2105 struct btrfs_dev_extent *dev_extent = NULL;
2106 struct btrfs_path *path;
2107 u64 length;
2108 u64 chunk_tree;
2109 u64 chunk_objectid;
2110 u64 chunk_offset;
2111 int ret;
2112 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002113 int failed = 0;
2114 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002115 struct extent_buffer *l;
2116 struct btrfs_key key;
2117 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2118 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002119 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002120 u64 diff = device->total_bytes - new_size;
2121
Yan Zheng2b820322008-11-17 21:11:30 -05002122 if (new_size >= device->total_bytes)
2123 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002124
2125 path = btrfs_alloc_path();
2126 if (!path)
2127 return -ENOMEM;
2128
Chris Mason8f18cf12008-04-25 16:53:30 -04002129 path->reada = 2;
2130
Chris Mason7d9eb122008-07-08 14:19:17 -04002131 lock_chunks(root);
2132
Chris Mason8f18cf12008-04-25 16:53:30 -04002133 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002134 if (device->writeable)
2135 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002136 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002137
Josef Bacikba1bf482009-09-11 16:11:19 -04002138again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002139 key.objectid = device->devid;
2140 key.offset = (u64)-1;
2141 key.type = BTRFS_DEV_EXTENT_KEY;
2142
2143 while (1) {
2144 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2145 if (ret < 0)
2146 goto done;
2147
2148 ret = btrfs_previous_item(root, path, 0, key.type);
2149 if (ret < 0)
2150 goto done;
2151 if (ret) {
2152 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002153 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002154 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002155 }
2156
2157 l = path->nodes[0];
2158 slot = path->slots[0];
2159 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2160
Josef Bacikba1bf482009-09-11 16:11:19 -04002161 if (key.objectid != device->devid) {
2162 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002163 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002164 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002165
2166 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2167 length = btrfs_dev_extent_length(l, dev_extent);
2168
Josef Bacikba1bf482009-09-11 16:11:19 -04002169 if (key.offset + length <= new_size) {
2170 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002171 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002172 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002173
2174 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2175 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2176 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2177 btrfs_release_path(root, path);
2178
2179 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2180 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002181 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002182 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002183 if (ret == -ENOSPC)
2184 failed++;
2185 key.offset -= 1;
2186 }
2187
2188 if (failed && !retried) {
2189 failed = 0;
2190 retried = true;
2191 goto again;
2192 } else if (failed && retried) {
2193 ret = -ENOSPC;
2194 lock_chunks(root);
2195
2196 device->total_bytes = old_size;
2197 if (device->writeable)
2198 device->fs_devices->total_rw_bytes += diff;
2199 unlock_chunks(root);
2200 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002201 }
2202
Chris Balld6397ba2009-04-27 07:29:03 -04002203 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002204 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002205 if (IS_ERR(trans)) {
2206 ret = PTR_ERR(trans);
2207 goto done;
2208 }
2209
Chris Balld6397ba2009-04-27 07:29:03 -04002210 lock_chunks(root);
2211
2212 device->disk_total_bytes = new_size;
2213 /* Now btrfs_update_device() will change the on-disk size. */
2214 ret = btrfs_update_device(trans, device);
2215 if (ret) {
2216 unlock_chunks(root);
2217 btrfs_end_transaction(trans, root);
2218 goto done;
2219 }
2220 WARN_ON(diff > old_total);
2221 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2222 unlock_chunks(root);
2223 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002224done:
2225 btrfs_free_path(path);
2226 return ret;
2227}
2228
Christoph Hellwigb2950862008-12-02 09:54:17 -05002229static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002230 struct btrfs_root *root,
2231 struct btrfs_key *key,
2232 struct btrfs_chunk *chunk, int item_size)
2233{
2234 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2235 struct btrfs_disk_key disk_key;
2236 u32 array_size;
2237 u8 *ptr;
2238
2239 array_size = btrfs_super_sys_array_size(super_copy);
2240 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2241 return -EFBIG;
2242
2243 ptr = super_copy->sys_chunk_array + array_size;
2244 btrfs_cpu_key_to_disk(&disk_key, key);
2245 memcpy(ptr, &disk_key, sizeof(disk_key));
2246 ptr += sizeof(disk_key);
2247 memcpy(ptr, chunk, item_size);
2248 item_size += sizeof(disk_key);
2249 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2250 return 0;
2251}
2252
Chris Masond3977122009-01-05 21:25:51 -05002253static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002254 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002255{
2256 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2257 return calc_size;
2258 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2259 return calc_size * (num_stripes / sub_stripes);
2260 else
2261 return calc_size * num_stripes;
2262}
2263
Miao Xieb2117a32011-01-05 10:07:28 +00002264/* Used to sort the devices by max_avail(descending sort) */
2265int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
Chris Mason0b86a832008-03-24 15:01:56 -04002266{
Miao Xieb2117a32011-01-05 10:07:28 +00002267 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2268 ((struct btrfs_device_info *)dev_info2)->max_avail)
2269 return -1;
2270 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2271 ((struct btrfs_device_info *)dev_info2)->max_avail)
2272 return 1;
2273 else
2274 return 0;
2275}
Chris Mason0b86a832008-03-24 15:01:56 -04002276
Miao Xieb2117a32011-01-05 10:07:28 +00002277static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2278 int *num_stripes, int *min_stripes,
2279 int *sub_stripes)
2280{
2281 *num_stripes = 1;
2282 *min_stripes = 1;
2283 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002284
Chris Masona40a90a2008-04-18 11:55:51 -04002285 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002286 *num_stripes = fs_devices->rw_devices;
2287 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002288 }
2289 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002290 *num_stripes = 2;
2291 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002292 }
Chris Mason8790d502008-04-03 16:29:03 -04002293 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002294 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002295 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002296 *num_stripes = 2;
2297 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002298 }
Chris Mason321aecc2008-04-16 10:49:51 -04002299 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002300 *num_stripes = fs_devices->rw_devices;
2301 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002302 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002303 *num_stripes &= ~(u32)1;
2304 *sub_stripes = 2;
2305 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002306 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002307
Miao Xieb2117a32011-01-05 10:07:28 +00002308 return 0;
2309}
2310
2311static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2312 u64 proposed_size, u64 type,
2313 int num_stripes, int small_stripe)
2314{
2315 int min_stripe_size = 1 * 1024 * 1024;
2316 u64 calc_size = proposed_size;
2317 u64 max_chunk_size = calc_size;
2318 int ncopies = 1;
2319
2320 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2321 BTRFS_BLOCK_GROUP_DUP |
2322 BTRFS_BLOCK_GROUP_RAID10))
2323 ncopies = 2;
2324
Chris Mason9b3f68b2008-04-18 10:29:51 -04002325 if (type & BTRFS_BLOCK_GROUP_DATA) {
2326 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002327 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002328 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002329 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002330 min_stripe_size = 32 * 1024 * 1024;
2331 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2332 calc_size = 8 * 1024 * 1024;
2333 max_chunk_size = calc_size * 2;
2334 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002335 }
2336
Yan Zheng2b820322008-11-17 21:11:30 -05002337 /* we don't want a chunk larger than 10% of writeable space */
2338 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2339 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002340
Miao Xie1974a3b2011-01-05 10:07:24 +00002341 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2342 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002343 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002344 do_div(calc_size, BTRFS_STRIPE_LEN);
2345 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002346 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002347
Chris Mason9b3f68b2008-04-18 10:29:51 -04002348 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002349 if (!small_stripe)
Josef Bacik0cad8a112010-03-17 20:45:56 +00002350 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002351
Chris Mason9f680ce2010-04-06 09:37:47 -04002352 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002353 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002354 * we end up with something bigger than a stripe
2355 */
Miao Xieb2117a32011-01-05 10:07:28 +00002356 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002357
Miao Xieb2117a32011-01-05 10:07:28 +00002358 do_div(calc_size, BTRFS_STRIPE_LEN);
2359 calc_size *= BTRFS_STRIPE_LEN;
2360
2361 return calc_size;
2362}
2363
2364static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2365 int num_stripes)
2366{
2367 struct map_lookup *new;
2368 size_t len = map_lookup_size(num_stripes);
2369
2370 BUG_ON(map->num_stripes < num_stripes);
2371
2372 if (map->num_stripes == num_stripes)
2373 return map;
2374
2375 new = kmalloc(len, GFP_NOFS);
2376 if (!new) {
2377 /* just change map->num_stripes */
2378 map->num_stripes = num_stripes;
2379 return map;
2380 }
2381
2382 memcpy(new, map, len);
2383 new->num_stripes = num_stripes;
2384 kfree(map);
2385 return new;
2386}
2387
2388/*
2389 * helper to allocate device space from btrfs_device_info, in which we stored
2390 * max free space information of every device. It is used when we can not
2391 * allocate chunks by default size.
2392 *
2393 * By this helper, we can allocate a new chunk as larger as possible.
2394 */
2395static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2396 struct btrfs_fs_devices *fs_devices,
2397 struct btrfs_device_info *devices,
2398 int nr_device, u64 type,
2399 struct map_lookup **map_lookup,
2400 int min_stripes, u64 *stripe_size)
2401{
2402 int i, index, sort_again = 0;
2403 int min_devices = min_stripes;
2404 u64 max_avail, min_free;
2405 struct map_lookup *map = *map_lookup;
2406 int ret;
2407
2408 if (nr_device < min_stripes)
2409 return -ENOSPC;
2410
2411 btrfs_descending_sort_devices(devices, nr_device);
2412
2413 max_avail = devices[0].max_avail;
2414 if (!max_avail)
2415 return -ENOSPC;
2416
2417 for (i = 0; i < nr_device; i++) {
2418 /*
2419 * if dev_offset = 0, it means the free space of this device
2420 * is less than what we need, and we didn't search max avail
2421 * extent on this device, so do it now.
2422 */
2423 if (!devices[i].dev_offset) {
2424 ret = find_free_dev_extent(trans, devices[i].dev,
2425 max_avail,
2426 &devices[i].dev_offset,
2427 &devices[i].max_avail);
2428 if (ret != 0 && ret != -ENOSPC)
2429 return ret;
2430 sort_again = 1;
2431 }
2432 }
2433
2434 /* we update the max avail free extent of each devices, sort again */
2435 if (sort_again)
2436 btrfs_descending_sort_devices(devices, nr_device);
2437
2438 if (type & BTRFS_BLOCK_GROUP_DUP)
2439 min_devices = 1;
2440
2441 if (!devices[min_devices - 1].max_avail)
2442 return -ENOSPC;
2443
2444 max_avail = devices[min_devices - 1].max_avail;
2445 if (type & BTRFS_BLOCK_GROUP_DUP)
2446 do_div(max_avail, 2);
2447
2448 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2449 min_stripes, 1);
2450 if (type & BTRFS_BLOCK_GROUP_DUP)
2451 min_free = max_avail * 2;
2452 else
2453 min_free = max_avail;
2454
2455 if (min_free > devices[min_devices - 1].max_avail)
2456 return -ENOSPC;
2457
2458 map = __shrink_map_lookup_stripes(map, min_stripes);
2459 *stripe_size = max_avail;
2460
2461 index = 0;
2462 for (i = 0; i < min_stripes; i++) {
2463 map->stripes[i].dev = devices[index].dev;
2464 map->stripes[i].physical = devices[index].dev_offset;
2465 if (type & BTRFS_BLOCK_GROUP_DUP) {
2466 i++;
2467 map->stripes[i].dev = devices[index].dev;
2468 map->stripes[i].physical = devices[index].dev_offset +
2469 max_avail;
2470 }
2471 index++;
2472 }
2473 *map_lookup = map;
2474
2475 return 0;
2476}
2477
2478static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2479 struct btrfs_root *extent_root,
2480 struct map_lookup **map_ret,
2481 u64 *num_bytes, u64 *stripe_size,
2482 u64 start, u64 type)
2483{
2484 struct btrfs_fs_info *info = extent_root->fs_info;
2485 struct btrfs_device *device = NULL;
2486 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2487 struct list_head *cur;
2488 struct map_lookup *map;
2489 struct extent_map_tree *em_tree;
2490 struct extent_map *em;
2491 struct btrfs_device_info *devices_info;
2492 struct list_head private_devs;
2493 u64 calc_size = 1024 * 1024 * 1024;
2494 u64 min_free;
2495 u64 avail;
2496 u64 dev_offset;
2497 int num_stripes;
2498 int min_stripes;
2499 int sub_stripes;
2500 int min_devices; /* the min number of devices we need */
2501 int i;
2502 int ret;
2503 int index;
2504
2505 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2506 (type & BTRFS_BLOCK_GROUP_DUP)) {
2507 WARN_ON(1);
2508 type &= ~BTRFS_BLOCK_GROUP_DUP;
2509 }
2510 if (list_empty(&fs_devices->alloc_list))
2511 return -ENOSPC;
2512
2513 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2514 &min_stripes, &sub_stripes);
2515 if (ret)
2516 return ret;
2517
2518 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2519 GFP_NOFS);
2520 if (!devices_info)
2521 return -ENOMEM;
2522
2523 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2524 if (!map) {
2525 ret = -ENOMEM;
2526 goto error;
2527 }
2528 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002529
Yan Zheng2b820322008-11-17 21:11:30 -05002530 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002531 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002532 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002533
Miao Xieb2117a32011-01-05 10:07:28 +00002534 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2535 num_stripes, 0);
2536
2537 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002538 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002539 min_devices = 1;
2540 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002541 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002542 min_devices = min_stripes;
2543 }
Chris Masonad5bd912008-04-21 08:28:10 -04002544
Yan Zheng2b820322008-11-17 21:11:30 -05002545 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002546 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002547 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002548 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002549 if (device->total_bytes > device->bytes_used)
2550 avail = device->total_bytes - device->bytes_used;
2551 else
2552 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002553 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002554
Chris Masondfe25022008-05-13 13:46:40 -04002555 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002556 ret = find_free_dev_extent(trans, device, min_free,
2557 &devices_info[i].dev_offset,
2558 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002559 if (ret == 0) {
2560 list_move_tail(&device->dev_alloc_list,
2561 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002562 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002563 map->stripes[index].physical =
2564 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002565 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002566 if (type & BTRFS_BLOCK_GROUP_DUP) {
2567 map->stripes[index].dev = device;
2568 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002569 devices_info[i].dev_offset +
2570 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002571 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002572 }
Miao Xieb2117a32011-01-05 10:07:28 +00002573 } else if (ret != -ENOSPC)
2574 goto error;
2575
2576 devices_info[i].dev = device;
2577 i++;
2578 } else if (device->in_fs_metadata &&
2579 avail >= BTRFS_STRIPE_LEN) {
2580 devices_info[i].dev = device;
2581 devices_info[i].max_avail = avail;
2582 i++;
2583 }
2584
Yan Zheng2b820322008-11-17 21:11:30 -05002585 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002586 break;
2587 }
Miao Xieb2117a32011-01-05 10:07:28 +00002588
Yan Zheng2b820322008-11-17 21:11:30 -05002589 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002590 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002591 if (index >= min_stripes) {
2592 num_stripes = index;
2593 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2594 num_stripes /= sub_stripes;
2595 num_stripes *= sub_stripes;
2596 }
Miao Xieb2117a32011-01-05 10:07:28 +00002597
2598 map = __shrink_map_lookup_stripes(map, num_stripes);
2599 } else if (i >= min_devices) {
2600 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2601 devices_info, i, type,
2602 &map, min_stripes,
2603 &calc_size);
2604 if (ret)
2605 goto error;
2606 } else {
2607 ret = -ENOSPC;
2608 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002609 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002610 }
Chris Mason593060d2008-03-25 16:50:33 -04002611 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002612 map->stripe_len = BTRFS_STRIPE_LEN;
2613 map->io_align = BTRFS_STRIPE_LEN;
2614 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002615 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002616 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002617
Yan Zheng2b820322008-11-17 21:11:30 -05002618 *map_ret = map;
2619 *stripe_size = calc_size;
2620 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002621 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002622
liubo1abe9b82011-03-24 11:18:59 +00002623 trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes);
2624
Chris Mason0b86a832008-03-24 15:01:56 -04002625 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002626 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002627 ret = -ENOMEM;
2628 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002629 }
Chris Mason0b86a832008-03-24 15:01:56 -04002630 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002631 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002632 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002633 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002634 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002635
Chris Mason0b86a832008-03-24 15:01:56 -04002636 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002637 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002638 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002639 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002640 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002641 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002642
2643 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2644 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2645 start, *num_bytes);
2646 BUG_ON(ret);
2647
2648 index = 0;
2649 while (index < map->num_stripes) {
2650 device = map->stripes[index].dev;
2651 dev_offset = map->stripes[index].physical;
2652
2653 ret = btrfs_alloc_dev_extent(trans, device,
2654 info->chunk_root->root_key.objectid,
2655 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2656 start, dev_offset, calc_size);
2657 BUG_ON(ret);
2658 index++;
2659 }
2660
Miao Xieb2117a32011-01-05 10:07:28 +00002661 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002662 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002663
2664error:
2665 kfree(map);
2666 kfree(devices_info);
2667 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002668}
2669
2670static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2671 struct btrfs_root *extent_root,
2672 struct map_lookup *map, u64 chunk_offset,
2673 u64 chunk_size, u64 stripe_size)
2674{
2675 u64 dev_offset;
2676 struct btrfs_key key;
2677 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2678 struct btrfs_device *device;
2679 struct btrfs_chunk *chunk;
2680 struct btrfs_stripe *stripe;
2681 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2682 int index = 0;
2683 int ret;
2684
2685 chunk = kzalloc(item_size, GFP_NOFS);
2686 if (!chunk)
2687 return -ENOMEM;
2688
2689 index = 0;
2690 while (index < map->num_stripes) {
2691 device = map->stripes[index].dev;
2692 device->bytes_used += stripe_size;
2693 ret = btrfs_update_device(trans, device);
2694 BUG_ON(ret);
2695 index++;
2696 }
2697
2698 index = 0;
2699 stripe = &chunk->stripe;
2700 while (index < map->num_stripes) {
2701 device = map->stripes[index].dev;
2702 dev_offset = map->stripes[index].physical;
2703
2704 btrfs_set_stack_stripe_devid(stripe, device->devid);
2705 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2706 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2707 stripe++;
2708 index++;
2709 }
2710
2711 btrfs_set_stack_chunk_length(chunk, chunk_size);
2712 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2713 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2714 btrfs_set_stack_chunk_type(chunk, map->type);
2715 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2716 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2717 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2718 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2719 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2720
2721 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2722 key.type = BTRFS_CHUNK_ITEM_KEY;
2723 key.offset = chunk_offset;
2724
2725 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2726 BUG_ON(ret);
2727
2728 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2729 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2730 item_size);
2731 BUG_ON(ret);
2732 }
liubo1abe9b82011-03-24 11:18:59 +00002733
Yan Zheng2b820322008-11-17 21:11:30 -05002734 kfree(chunk);
2735 return 0;
2736}
2737
2738/*
2739 * Chunk allocation falls into two parts. The first part does works
2740 * that make the new allocated chunk useable, but not do any operation
2741 * that modifies the chunk tree. The second part does the works that
2742 * require modifying the chunk tree. This division is important for the
2743 * bootstrap process of adding storage to a seed btrfs.
2744 */
2745int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2746 struct btrfs_root *extent_root, u64 type)
2747{
2748 u64 chunk_offset;
2749 u64 chunk_size;
2750 u64 stripe_size;
2751 struct map_lookup *map;
2752 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2753 int ret;
2754
2755 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2756 &chunk_offset);
2757 if (ret)
2758 return ret;
2759
2760 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2761 &stripe_size, chunk_offset, type);
2762 if (ret)
2763 return ret;
2764
2765 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2766 chunk_size, stripe_size);
2767 BUG_ON(ret);
2768 return 0;
2769}
2770
Chris Masond3977122009-01-05 21:25:51 -05002771static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002772 struct btrfs_root *root,
2773 struct btrfs_device *device)
2774{
2775 u64 chunk_offset;
2776 u64 sys_chunk_offset;
2777 u64 chunk_size;
2778 u64 sys_chunk_size;
2779 u64 stripe_size;
2780 u64 sys_stripe_size;
2781 u64 alloc_profile;
2782 struct map_lookup *map;
2783 struct map_lookup *sys_map;
2784 struct btrfs_fs_info *fs_info = root->fs_info;
2785 struct btrfs_root *extent_root = fs_info->extent_root;
2786 int ret;
2787
2788 ret = find_next_chunk(fs_info->chunk_root,
2789 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2790 BUG_ON(ret);
2791
2792 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2793 (fs_info->metadata_alloc_profile &
2794 fs_info->avail_metadata_alloc_bits);
2795 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2796
2797 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2798 &stripe_size, chunk_offset, alloc_profile);
2799 BUG_ON(ret);
2800
2801 sys_chunk_offset = chunk_offset + chunk_size;
2802
2803 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2804 (fs_info->system_alloc_profile &
2805 fs_info->avail_system_alloc_bits);
2806 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2807
2808 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2809 &sys_chunk_size, &sys_stripe_size,
2810 sys_chunk_offset, alloc_profile);
2811 BUG_ON(ret);
2812
2813 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2814 BUG_ON(ret);
2815
2816 /*
2817 * Modifying chunk tree needs allocating new blocks from both
2818 * system block group and metadata block group. So we only can
2819 * do operations require modifying the chunk tree after both
2820 * block groups were created.
2821 */
2822 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2823 chunk_size, stripe_size);
2824 BUG_ON(ret);
2825
2826 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2827 sys_chunk_offset, sys_chunk_size,
2828 sys_stripe_size);
2829 BUG_ON(ret);
2830 return 0;
2831}
2832
2833int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2834{
2835 struct extent_map *em;
2836 struct map_lookup *map;
2837 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2838 int readonly = 0;
2839 int i;
2840
Chris Mason890871b2009-09-02 16:24:52 -04002841 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002842 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002843 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002844 if (!em)
2845 return 1;
2846
Josef Bacikf48b9072010-01-27 02:07:59 +00002847 if (btrfs_test_opt(root, DEGRADED)) {
2848 free_extent_map(em);
2849 return 0;
2850 }
2851
Yan Zheng2b820322008-11-17 21:11:30 -05002852 map = (struct map_lookup *)em->bdev;
2853 for (i = 0; i < map->num_stripes; i++) {
2854 if (!map->stripes[i].dev->writeable) {
2855 readonly = 1;
2856 break;
2857 }
2858 }
2859 free_extent_map(em);
2860 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002861}
2862
2863void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2864{
2865 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2866}
2867
2868void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2869{
2870 struct extent_map *em;
2871
Chris Masond3977122009-01-05 21:25:51 -05002872 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002873 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002874 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2875 if (em)
2876 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002877 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002878 if (!em)
2879 break;
2880 kfree(em->bdev);
2881 /* once for us */
2882 free_extent_map(em);
2883 /* once for the tree */
2884 free_extent_map(em);
2885 }
2886}
2887
Chris Masonf1885912008-04-09 16:28:12 -04002888int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2889{
2890 struct extent_map *em;
2891 struct map_lookup *map;
2892 struct extent_map_tree *em_tree = &map_tree->map_tree;
2893 int ret;
2894
Chris Mason890871b2009-09-02 16:24:52 -04002895 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002896 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002897 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002898 BUG_ON(!em);
2899
2900 BUG_ON(em->start > logical || em->start + em->len < logical);
2901 map = (struct map_lookup *)em->bdev;
2902 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2903 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002904 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2905 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002906 else
2907 ret = 1;
2908 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002909 return ret;
2910}
2911
Chris Masondfe25022008-05-13 13:46:40 -04002912static int find_live_mirror(struct map_lookup *map, int first, int num,
2913 int optimal)
2914{
2915 int i;
2916 if (map->stripes[optimal].dev->bdev)
2917 return optimal;
2918 for (i = first; i < first + num; i++) {
2919 if (map->stripes[i].dev->bdev)
2920 return i;
2921 }
2922 /* we couldn't find one that doesn't fail. Just return something
2923 * and the io error handling code will clean up eventually
2924 */
2925 return optimal;
2926}
2927
Chris Masonf2d8d742008-04-21 10:03:05 -04002928static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2929 u64 logical, u64 *length,
2930 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002931 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002932{
2933 struct extent_map *em;
2934 struct map_lookup *map;
2935 struct extent_map_tree *em_tree = &map_tree->map_tree;
2936 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002937 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002938 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002939 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002940 u64 stripe_nr_orig;
2941 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002942 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002943 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002944 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002945 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002946 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002947 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002948 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002949
Li Dongyangfce3bb92011-03-24 10:24:26 +00002950 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002951 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002952again:
2953 if (multi_ret) {
2954 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2955 GFP_NOFS);
2956 if (!multi)
2957 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002958
2959 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002960 }
Chris Mason0b86a832008-03-24 15:01:56 -04002961
Chris Mason890871b2009-09-02 16:24:52 -04002962 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002963 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002964 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002965
Chris Mason3b951512008-04-17 11:29:12 -04002966 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002967 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2968 (unsigned long long)logical,
2969 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002970 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002971 }
Chris Mason0b86a832008-03-24 15:01:56 -04002972
2973 BUG_ON(em->start > logical || em->start + em->len < logical);
2974 map = (struct map_lookup *)em->bdev;
2975 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002976
Chris Masonf1885912008-04-09 16:28:12 -04002977 if (mirror_num > map->num_stripes)
2978 mirror_num = 0;
2979
Chris Masoncea9e442008-04-09 16:28:12 -04002980 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002981 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002982 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2983 BTRFS_BLOCK_GROUP_DUP)) {
2984 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002985 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002986 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2987 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002988 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002989 }
2990 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002991 if (rw & REQ_DISCARD) {
2992 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2993 BTRFS_BLOCK_GROUP_RAID1 |
2994 BTRFS_BLOCK_GROUP_DUP |
2995 BTRFS_BLOCK_GROUP_RAID10)) {
2996 stripes_required = map->num_stripes;
2997 }
2998 }
2999 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04003000 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04003001 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003002 free_extent_map(em);
3003 kfree(multi);
3004 goto again;
3005 }
Chris Mason593060d2008-03-25 16:50:33 -04003006 stripe_nr = offset;
3007 /*
3008 * stripe_nr counts the total number of stripes we have to stride
3009 * to get to this block
3010 */
3011 do_div(stripe_nr, map->stripe_len);
3012
3013 stripe_offset = stripe_nr * map->stripe_len;
3014 BUG_ON(offset < stripe_offset);
3015
3016 /* stripe_offset is the offset of this block in its stripe*/
3017 stripe_offset = offset - stripe_offset;
3018
Li Dongyangfce3bb92011-03-24 10:24:26 +00003019 if (rw & REQ_DISCARD)
3020 *length = min_t(u64, em->len - offset, *length);
3021 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3022 BTRFS_BLOCK_GROUP_RAID1 |
3023 BTRFS_BLOCK_GROUP_RAID10 |
3024 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04003025 /* we limit the length of each bio to what fits in a stripe */
3026 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003027 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003028 } else {
3029 *length = em->len - offset;
3030 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003031
Jens Axboe7eaceac2011-03-10 08:52:07 +01003032 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003033 goto out;
3034
Chris Masonf2d8d742008-04-21 10:03:05 -04003035 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003036 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003037 stripe_nr_orig = stripe_nr;
3038 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3039 (~(map->stripe_len - 1));
3040 do_div(stripe_nr_end, map->stripe_len);
3041 stripe_end_offset = stripe_nr_end * map->stripe_len -
3042 (offset + *length);
3043 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3044 if (rw & REQ_DISCARD)
3045 num_stripes = min_t(u64, map->num_stripes,
3046 stripe_nr_end - stripe_nr_orig);
3047 stripe_index = do_div(stripe_nr, map->num_stripes);
3048 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003049 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003050 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003051 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003052 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003053 else {
3054 stripe_index = find_live_mirror(map, 0,
3055 map->num_stripes,
3056 current->pid % map->num_stripes);
3057 }
Chris Mason2fff7342008-04-29 14:12:09 -04003058
Chris Mason611f0e02008-04-03 16:29:03 -04003059 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00003060 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003061 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003062 else if (mirror_num)
3063 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003064
Chris Mason321aecc2008-04-16 10:49:51 -04003065 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3066 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003067
3068 stripe_index = do_div(stripe_nr, factor);
3069 stripe_index *= map->sub_stripes;
3070
Jens Axboe7eaceac2011-03-10 08:52:07 +01003071 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003072 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003073 else if (rw & REQ_DISCARD)
3074 num_stripes = min_t(u64, map->sub_stripes *
3075 (stripe_nr_end - stripe_nr_orig),
3076 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003077 else if (mirror_num)
3078 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003079 else {
3080 stripe_index = find_live_mirror(map, stripe_index,
3081 map->sub_stripes, stripe_index +
3082 current->pid % map->sub_stripes);
3083 }
Chris Mason8790d502008-04-03 16:29:03 -04003084 } else {
3085 /*
3086 * after this do_div call, stripe_nr is the number of stripes
3087 * on this device we have to walk to find the data, and
3088 * stripe_index is the number of our device in the stripe array
3089 */
3090 stripe_index = do_div(stripe_nr, map->num_stripes);
3091 }
Chris Mason593060d2008-03-25 16:50:33 -04003092 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003093
Li Dongyangfce3bb92011-03-24 10:24:26 +00003094 if (rw & REQ_DISCARD) {
3095 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003096 multi->stripes[i].physical =
3097 map->stripes[stripe_index].physical +
3098 stripe_offset + stripe_nr * map->stripe_len;
3099 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003100
3101 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3102 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003103 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003104 int j;
3105
Chris Masond9d04872011-03-27 21:23:21 -04003106 div_u64_rem(stripe_nr_end - 1,
3107 map->num_stripes,
3108 &last_stripe);
3109
Li Dongyangfce3bb92011-03-24 10:24:26 +00003110 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003111 u32 test;
3112
3113 div_u64_rem(stripe_nr_end - 1 - j,
3114 map->num_stripes, &test);
3115 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003116 break;
3117 }
3118 stripes = stripe_nr_end - 1 - j;
3119 do_div(stripes, map->num_stripes);
3120 multi->stripes[i].length = map->stripe_len *
3121 (stripes - stripe_nr + 1);
3122
3123 if (i == 0) {
3124 multi->stripes[i].length -=
3125 stripe_offset;
3126 stripe_offset = 0;
3127 }
3128 if (stripe_index == last_stripe)
3129 multi->stripes[i].length -=
3130 stripe_end_offset;
3131 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3132 u64 stripes;
3133 int j;
3134 int factor = map->num_stripes /
3135 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003136 u32 last_stripe = 0;
3137
3138 div_u64_rem(stripe_nr_end - 1,
3139 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003140 last_stripe *= map->sub_stripes;
3141
3142 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003143 u32 test;
3144
3145 div_u64_rem(stripe_nr_end - 1 - j,
3146 factor, &test);
3147
3148 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003149 stripe_index / map->sub_stripes)
3150 break;
3151 }
3152 stripes = stripe_nr_end - 1 - j;
3153 do_div(stripes, factor);
3154 multi->stripes[i].length = map->stripe_len *
3155 (stripes - stripe_nr + 1);
3156
3157 if (i < map->sub_stripes) {
3158 multi->stripes[i].length -=
3159 stripe_offset;
3160 if (i == map->sub_stripes - 1)
3161 stripe_offset = 0;
3162 }
3163 if (stripe_index >= last_stripe &&
3164 stripe_index <= (last_stripe +
3165 map->sub_stripes - 1)) {
3166 multi->stripes[i].length -=
3167 stripe_end_offset;
3168 }
3169 } else
3170 multi->stripes[i].length = *length;
3171
3172 stripe_index++;
3173 if (stripe_index == map->num_stripes) {
3174 /* This could only happen for RAID0/10 */
3175 stripe_index = 0;
3176 stripe_nr++;
3177 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003178 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003179 } else {
3180 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003181 multi->stripes[i].physical =
3182 map->stripes[stripe_index].physical +
3183 stripe_offset +
3184 stripe_nr * map->stripe_len;
3185 multi->stripes[i].dev =
3186 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003187 stripe_index++;
3188 }
Chris Mason593060d2008-03-25 16:50:33 -04003189 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003190 if (multi_ret) {
3191 *multi_ret = multi;
3192 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003193 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003194 }
Chris Masoncea9e442008-04-09 16:28:12 -04003195out:
Chris Mason0b86a832008-03-24 15:01:56 -04003196 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003197 return 0;
3198}
3199
Chris Masonf2d8d742008-04-21 10:03:05 -04003200int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3201 u64 logical, u64 *length,
3202 struct btrfs_multi_bio **multi_ret, int mirror_num)
3203{
3204 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003205 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003206}
3207
Yan Zhenga512bbf2008-12-08 16:46:26 -05003208int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3209 u64 chunk_start, u64 physical, u64 devid,
3210 u64 **logical, int *naddrs, int *stripe_len)
3211{
3212 struct extent_map_tree *em_tree = &map_tree->map_tree;
3213 struct extent_map *em;
3214 struct map_lookup *map;
3215 u64 *buf;
3216 u64 bytenr;
3217 u64 length;
3218 u64 stripe_nr;
3219 int i, j, nr = 0;
3220
Chris Mason890871b2009-09-02 16:24:52 -04003221 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003222 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003223 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003224
3225 BUG_ON(!em || em->start != chunk_start);
3226 map = (struct map_lookup *)em->bdev;
3227
3228 length = em->len;
3229 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3230 do_div(length, map->num_stripes / map->sub_stripes);
3231 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3232 do_div(length, map->num_stripes);
3233
3234 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3235 BUG_ON(!buf);
3236
3237 for (i = 0; i < map->num_stripes; i++) {
3238 if (devid && map->stripes[i].dev->devid != devid)
3239 continue;
3240 if (map->stripes[i].physical > physical ||
3241 map->stripes[i].physical + length <= physical)
3242 continue;
3243
3244 stripe_nr = physical - map->stripes[i].physical;
3245 do_div(stripe_nr, map->stripe_len);
3246
3247 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3248 stripe_nr = stripe_nr * map->num_stripes + i;
3249 do_div(stripe_nr, map->sub_stripes);
3250 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3251 stripe_nr = stripe_nr * map->num_stripes + i;
3252 }
3253 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003254 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003255 for (j = 0; j < nr; j++) {
3256 if (buf[j] == bytenr)
3257 break;
3258 }
Chris Mason934d3752008-12-08 16:43:10 -05003259 if (j == nr) {
3260 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003261 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003262 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003263 }
3264
Yan Zhenga512bbf2008-12-08 16:46:26 -05003265 *logical = buf;
3266 *naddrs = nr;
3267 *stripe_len = map->stripe_len;
3268
3269 free_extent_map(em);
3270 return 0;
3271}
3272
Chris Mason8790d502008-04-03 16:29:03 -04003273static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003274{
Chris Masoncea9e442008-04-09 16:28:12 -04003275 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003276 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003277
Chris Mason8790d502008-04-03 16:29:03 -04003278 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003279 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003280
Chris Mason7d2b4da2008-08-05 10:13:57 -04003281 if (bio == multi->orig_bio)
3282 is_orig_bio = 1;
3283
Chris Masoncea9e442008-04-09 16:28:12 -04003284 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003285 if (!is_orig_bio) {
3286 bio_put(bio);
3287 bio = multi->orig_bio;
3288 }
Chris Mason8790d502008-04-03 16:29:03 -04003289 bio->bi_private = multi->private;
3290 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003291 /* only send an error to the higher layers if it is
3292 * beyond the tolerance of the multi-bio
3293 */
Chris Mason1259ab72008-05-12 13:39:03 -04003294 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003295 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003296 } else if (err) {
3297 /*
3298 * this bio is actually up to date, we didn't
3299 * go over the max number of errors
3300 */
3301 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003302 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003303 }
Chris Mason8790d502008-04-03 16:29:03 -04003304 kfree(multi);
3305
3306 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003307 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003308 bio_put(bio);
3309 }
Chris Mason8790d502008-04-03 16:29:03 -04003310}
3311
Chris Mason8b712842008-06-11 16:50:36 -04003312struct async_sched {
3313 struct bio *bio;
3314 int rw;
3315 struct btrfs_fs_info *info;
3316 struct btrfs_work work;
3317};
3318
3319/*
3320 * see run_scheduled_bios for a description of why bios are collected for
3321 * async submit.
3322 *
3323 * This will add one bio to the pending list for a device and make sure
3324 * the work struct is scheduled.
3325 */
Chris Masond3977122009-01-05 21:25:51 -05003326static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003327 struct btrfs_device *device,
3328 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003329{
3330 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003331 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003332
3333 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003334 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003335 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003336 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003337 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003338 return 0;
3339 }
3340
3341 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003342 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003343 * higher layers. Otherwise, the async bio makes it appear we have
3344 * made progress against dirty pages when we've really just put it
3345 * on a queue for later
3346 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003347 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003348 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003349 bio->bi_next = NULL;
3350 bio->bi_rw |= rw;
3351
3352 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003353 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003354 pending_bios = &device->pending_sync_bios;
3355 else
3356 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003357
Chris Masonffbd5172009-04-20 15:50:09 -04003358 if (pending_bios->tail)
3359 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003360
Chris Masonffbd5172009-04-20 15:50:09 -04003361 pending_bios->tail = bio;
3362 if (!pending_bios->head)
3363 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003364 if (device->running_pending)
3365 should_queue = 0;
3366
3367 spin_unlock(&device->io_lock);
3368
3369 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003370 btrfs_queue_worker(&root->fs_info->submit_workers,
3371 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003372 return 0;
3373}
3374
Chris Masonf1885912008-04-09 16:28:12 -04003375int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003376 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003377{
3378 struct btrfs_mapping_tree *map_tree;
3379 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003380 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003381 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003382 u64 length = 0;
3383 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003384 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003385 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003386 int dev_nr = 0;
3387 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003388
Chris Masonf2d8d742008-04-21 10:03:05 -04003389 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003390 map_tree = &root->fs_info->mapping_tree;
3391 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003392
Chris Masonf1885912008-04-09 16:28:12 -04003393 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3394 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003395 BUG_ON(ret);
3396
3397 total_devs = multi->num_stripes;
3398 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003399 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3400 "len %llu\n", (unsigned long long)logical,
3401 (unsigned long long)length,
3402 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003403 BUG();
3404 }
3405 multi->end_io = first_bio->bi_end_io;
3406 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003407 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003408 atomic_set(&multi->stripes_pending, multi->num_stripes);
3409
Chris Masond3977122009-01-05 21:25:51 -05003410 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003411 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003412 if (dev_nr < total_devs - 1) {
3413 bio = bio_clone(first_bio, GFP_NOFS);
3414 BUG_ON(!bio);
3415 } else {
3416 bio = first_bio;
3417 }
3418 bio->bi_private = multi;
3419 bio->bi_end_io = end_bio_multi_stripe;
3420 }
Chris Masoncea9e442008-04-09 16:28:12 -04003421 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3422 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003423 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003424 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003425 if (async_submit)
3426 schedule_bio(root, dev, rw, bio);
3427 else
3428 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003429 } else {
3430 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3431 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003432 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003433 }
Chris Mason8790d502008-04-03 16:29:03 -04003434 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003435 }
Chris Masoncea9e442008-04-09 16:28:12 -04003436 if (total_devs == 1)
3437 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003438 return 0;
3439}
3440
Chris Masona4437552008-04-18 10:29:38 -04003441struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003442 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003443{
Yan Zheng2b820322008-11-17 21:11:30 -05003444 struct btrfs_device *device;
3445 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003446
Yan Zheng2b820322008-11-17 21:11:30 -05003447 cur_devices = root->fs_info->fs_devices;
3448 while (cur_devices) {
3449 if (!fsid ||
3450 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3451 device = __find_device(&cur_devices->devices,
3452 devid, uuid);
3453 if (device)
3454 return device;
3455 }
3456 cur_devices = cur_devices->seed;
3457 }
3458 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003459}
3460
Chris Masondfe25022008-05-13 13:46:40 -04003461static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3462 u64 devid, u8 *dev_uuid)
3463{
3464 struct btrfs_device *device;
3465 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3466
3467 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003468 if (!device)
3469 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003470 list_add(&device->dev_list,
3471 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003472 device->dev_root = root->fs_info->dev_root;
3473 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003474 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003475 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003476 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003477 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003478 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003479 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003480 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003481 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3482 return device;
3483}
3484
Chris Mason0b86a832008-03-24 15:01:56 -04003485static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3486 struct extent_buffer *leaf,
3487 struct btrfs_chunk *chunk)
3488{
3489 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3490 struct map_lookup *map;
3491 struct extent_map *em;
3492 u64 logical;
3493 u64 length;
3494 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003495 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003496 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003497 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003498 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003499
Chris Masone17cade2008-04-15 15:41:47 -04003500 logical = key->offset;
3501 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003502
Chris Mason890871b2009-09-02 16:24:52 -04003503 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003504 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003505 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003506
3507 /* already mapped? */
3508 if (em && em->start <= logical && em->start + em->len > logical) {
3509 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003510 return 0;
3511 } else if (em) {
3512 free_extent_map(em);
3513 }
Chris Mason0b86a832008-03-24 15:01:56 -04003514
Chris Mason0b86a832008-03-24 15:01:56 -04003515 em = alloc_extent_map(GFP_NOFS);
3516 if (!em)
3517 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003518 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3519 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003520 if (!map) {
3521 free_extent_map(em);
3522 return -ENOMEM;
3523 }
3524
3525 em->bdev = (struct block_device *)map;
3526 em->start = logical;
3527 em->len = length;
3528 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003529 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003530
Chris Mason593060d2008-03-25 16:50:33 -04003531 map->num_stripes = num_stripes;
3532 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3533 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3534 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3535 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3536 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003537 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003538 for (i = 0; i < num_stripes; i++) {
3539 map->stripes[i].physical =
3540 btrfs_stripe_offset_nr(leaf, chunk, i);
3541 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003542 read_extent_buffer(leaf, uuid, (unsigned long)
3543 btrfs_stripe_dev_uuid_nr(chunk, i),
3544 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003545 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3546 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003547 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003548 kfree(map);
3549 free_extent_map(em);
3550 return -EIO;
3551 }
Chris Masondfe25022008-05-13 13:46:40 -04003552 if (!map->stripes[i].dev) {
3553 map->stripes[i].dev =
3554 add_missing_dev(root, devid, uuid);
3555 if (!map->stripes[i].dev) {
3556 kfree(map);
3557 free_extent_map(em);
3558 return -EIO;
3559 }
3560 }
3561 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003562 }
3563
Chris Mason890871b2009-09-02 16:24:52 -04003564 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003565 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003566 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003567 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003568 free_extent_map(em);
3569
3570 return 0;
3571}
3572
3573static int fill_device_from_item(struct extent_buffer *leaf,
3574 struct btrfs_dev_item *dev_item,
3575 struct btrfs_device *device)
3576{
3577 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003578
3579 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003580 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3581 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003582 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3583 device->type = btrfs_device_type(leaf, dev_item);
3584 device->io_align = btrfs_device_io_align(leaf, dev_item);
3585 device->io_width = btrfs_device_io_width(leaf, dev_item);
3586 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003587
3588 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003589 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003590
Chris Mason0b86a832008-03-24 15:01:56 -04003591 return 0;
3592}
3593
Yan Zheng2b820322008-11-17 21:11:30 -05003594static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3595{
3596 struct btrfs_fs_devices *fs_devices;
3597 int ret;
3598
3599 mutex_lock(&uuid_mutex);
3600
3601 fs_devices = root->fs_info->fs_devices->seed;
3602 while (fs_devices) {
3603 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3604 ret = 0;
3605 goto out;
3606 }
3607 fs_devices = fs_devices->seed;
3608 }
3609
3610 fs_devices = find_fsid(fsid);
3611 if (!fs_devices) {
3612 ret = -ENOENT;
3613 goto out;
3614 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003615
3616 fs_devices = clone_fs_devices(fs_devices);
3617 if (IS_ERR(fs_devices)) {
3618 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003619 goto out;
3620 }
3621
Christoph Hellwig97288f22008-12-02 06:36:09 -05003622 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003623 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003624 if (ret)
3625 goto out;
3626
3627 if (!fs_devices->seeding) {
3628 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003629 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003630 ret = -EINVAL;
3631 goto out;
3632 }
3633
3634 fs_devices->seed = root->fs_info->fs_devices->seed;
3635 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003636out:
3637 mutex_unlock(&uuid_mutex);
3638 return ret;
3639}
3640
Chris Mason0d81ba52008-03-24 15:02:07 -04003641static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003642 struct extent_buffer *leaf,
3643 struct btrfs_dev_item *dev_item)
3644{
3645 struct btrfs_device *device;
3646 u64 devid;
3647 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003648 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003649 u8 dev_uuid[BTRFS_UUID_SIZE];
3650
Chris Mason0b86a832008-03-24 15:01:56 -04003651 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003652 read_extent_buffer(leaf, dev_uuid,
3653 (unsigned long)btrfs_device_uuid(dev_item),
3654 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003655 read_extent_buffer(leaf, fs_uuid,
3656 (unsigned long)btrfs_device_fsid(dev_item),
3657 BTRFS_UUID_SIZE);
3658
3659 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3660 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003661 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003662 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003663 }
3664
3665 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3666 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003667 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003668 return -EIO;
3669
3670 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003671 printk(KERN_WARNING "warning devid %llu missing\n",
3672 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003673 device = add_missing_dev(root, devid, dev_uuid);
3674 if (!device)
3675 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003676 } else if (!device->missing) {
3677 /*
3678 * this happens when a device that was properly setup
3679 * in the device info lists suddenly goes bad.
3680 * device->bdev is NULL, and so we have to set
3681 * device->missing to one here
3682 */
3683 root->fs_info->fs_devices->missing_devices++;
3684 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003685 }
3686 }
3687
3688 if (device->fs_devices != root->fs_info->fs_devices) {
3689 BUG_ON(device->writeable);
3690 if (device->generation !=
3691 btrfs_device_generation(leaf, dev_item))
3692 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003693 }
Chris Mason0b86a832008-03-24 15:01:56 -04003694
3695 fill_device_from_item(leaf, dev_item, device);
3696 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003697 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003698 if (device->writeable)
3699 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003700 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003701 return ret;
3702}
3703
Chris Mason0d81ba52008-03-24 15:02:07 -04003704int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3705{
3706 struct btrfs_dev_item *dev_item;
3707
3708 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3709 dev_item);
3710 return read_one_dev(root, buf, dev_item);
3711}
3712
Yan Zhenge4404d62008-12-12 10:03:26 -05003713int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003714{
3715 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003716 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003717 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003718 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003719 u8 *ptr;
3720 unsigned long sb_ptr;
3721 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003722 u32 num_stripes;
3723 u32 array_size;
3724 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003725 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003726 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003727
Yan Zhenge4404d62008-12-12 10:03:26 -05003728 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003729 BTRFS_SUPER_INFO_SIZE);
3730 if (!sb)
3731 return -ENOMEM;
3732 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003733 btrfs_set_buffer_lockdep_class(sb, 0);
3734
Chris Masona061fc82008-05-07 11:43:44 -04003735 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003736 array_size = btrfs_super_sys_array_size(super_copy);
3737
Chris Mason0b86a832008-03-24 15:01:56 -04003738 ptr = super_copy->sys_chunk_array;
3739 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3740 cur = 0;
3741
3742 while (cur < array_size) {
3743 disk_key = (struct btrfs_disk_key *)ptr;
3744 btrfs_disk_key_to_cpu(&key, disk_key);
3745
Chris Masona061fc82008-05-07 11:43:44 -04003746 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003747 sb_ptr += len;
3748 cur += len;
3749
Chris Mason0d81ba52008-03-24 15:02:07 -04003750 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003751 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003752 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003753 if (ret)
3754 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003755 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3756 len = btrfs_chunk_item_size(num_stripes);
3757 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003758 ret = -EIO;
3759 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003760 }
3761 ptr += len;
3762 sb_ptr += len;
3763 cur += len;
3764 }
Chris Masona061fc82008-05-07 11:43:44 -04003765 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003766 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003767}
3768
3769int btrfs_read_chunk_tree(struct btrfs_root *root)
3770{
3771 struct btrfs_path *path;
3772 struct extent_buffer *leaf;
3773 struct btrfs_key key;
3774 struct btrfs_key found_key;
3775 int ret;
3776 int slot;
3777
3778 root = root->fs_info->chunk_root;
3779
3780 path = btrfs_alloc_path();
3781 if (!path)
3782 return -ENOMEM;
3783
3784 /* first we search for all of the device items, and then we
3785 * read in all of the chunk items. This way we can create chunk
3786 * mappings that reference all of the devices that are afound
3787 */
3788 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3789 key.offset = 0;
3790 key.type = 0;
3791again:
3792 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003793 if (ret < 0)
3794 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003795 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003796 leaf = path->nodes[0];
3797 slot = path->slots[0];
3798 if (slot >= btrfs_header_nritems(leaf)) {
3799 ret = btrfs_next_leaf(root, path);
3800 if (ret == 0)
3801 continue;
3802 if (ret < 0)
3803 goto error;
3804 break;
3805 }
3806 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3807 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3808 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3809 break;
3810 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3811 struct btrfs_dev_item *dev_item;
3812 dev_item = btrfs_item_ptr(leaf, slot,
3813 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003814 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003815 if (ret)
3816 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003817 }
3818 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3819 struct btrfs_chunk *chunk;
3820 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3821 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003822 if (ret)
3823 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003824 }
3825 path->slots[0]++;
3826 }
3827 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3828 key.objectid = 0;
3829 btrfs_release_path(root, path);
3830 goto again;
3831 }
Chris Mason0b86a832008-03-24 15:01:56 -04003832 ret = 0;
3833error:
Yan Zheng2b820322008-11-17 21:11:30 -05003834 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003835 return ret;
3836}