blob: e0913e4697284673b4c00cd58bf911fc44fe4019 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040020#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040021#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040022#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040023#include <linux/iocontext.h>
Chris Mason593060d2008-03-25 16:50:33 -040024#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040026#include "ctree.h"
27#include "extent_map.h"
28#include "disk-io.h"
29#include "transaction.h"
30#include "print-tree.h"
31#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040032#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033
Chris Mason593060d2008-03-25 16:50:33 -040034struct map_lookup {
35 u64 type;
36 int io_align;
37 int io_width;
38 int stripe_len;
39 int sector_size;
40 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040041 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040042 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040043};
44
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49
Chris Mason593060d2008-03-25 16:50:33 -040050#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040051 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Masona061fc82008-05-07 11:43:44 -040056void btrfs_lock_volumes(void)
57{
58 mutex_lock(&uuid_mutex);
59}
60
61void btrfs_unlock_volumes(void)
62{
63 mutex_unlock(&uuid_mutex);
64}
65
Chris Mason7d9eb122008-07-08 14:19:17 -040066static void lock_chunks(struct btrfs_root *root)
67{
Chris Mason7d9eb122008-07-08 14:19:17 -040068 mutex_lock(&root->fs_info->chunk_mutex);
69}
70
71static void unlock_chunks(struct btrfs_root *root)
72{
Chris Mason7d9eb122008-07-08 14:19:17 -040073 mutex_unlock(&root->fs_info->chunk_mutex);
74}
75
Yan Zhenge4404d62008-12-12 10:03:26 -050076static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
77{
78 struct btrfs_device *device;
79 WARN_ON(fs_devices->opened);
80 while (!list_empty(&fs_devices->devices)) {
81 device = list_entry(fs_devices->devices.next,
82 struct btrfs_device, dev_list);
83 list_del(&device->dev_list);
84 kfree(device->name);
85 kfree(device);
86 }
87 kfree(fs_devices);
88}
89
Chris Mason8a4b83c2008-03-24 15:02:07 -040090int btrfs_cleanup_fs_uuids(void)
91{
92 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040093
Yan Zheng2b820322008-11-17 21:11:30 -050094 while (!list_empty(&fs_uuids)) {
95 fs_devices = list_entry(fs_uuids.next,
96 struct btrfs_fs_devices, list);
97 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050098 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040099 }
100 return 0;
101}
102
Chris Masona1b32a52008-09-05 16:09:51 -0400103static noinline struct btrfs_device *__find_device(struct list_head *head,
104 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105{
106 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500108 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400109 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400110 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400111 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400112 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 }
114 return NULL;
115}
116
Chris Masona1b32a52008-09-05 16:09:51 -0400117static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400118{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 struct btrfs_fs_devices *fs_devices;
120
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500121 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400122 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
123 return fs_devices;
124 }
125 return NULL;
126}
127
Chris Mason8b712842008-06-11 16:50:36 -0400128/*
129 * we try to collect pending bios for a device so we don't get a large
130 * number of procs sending bios down to the same device. This greatly
131 * improves the schedulers ability to collect and merge the bios.
132 *
133 * But, it also turns into a long list of bios to process and that is sure
134 * to eventually make the worker thread block. The solution here is to
135 * make some progress and then put this work struct back at the end of
136 * the list if the block device is congested. This way, multiple devices
137 * can make progress from a single worker thread.
138 */
Chris Masond3977122009-01-05 21:25:51 -0500139static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400140{
141 struct bio *pending;
142 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400143 struct btrfs_fs_info *fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400144 struct bio *tail;
145 struct bio *cur;
146 int again = 0;
147 unsigned long num_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400148 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400149 unsigned long last_waited = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400150
Chris Masonbedf7622009-04-03 10:32:58 -0400151 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400152 fs_info = device->dev_root->fs_info;
153 limit = btrfs_async_submit_limit(fs_info);
154 limit = limit * 2 / 3;
155
Chris Mason8b712842008-06-11 16:50:36 -0400156loop:
157 spin_lock(&device->io_lock);
158
Chris Masona6837052009-02-04 09:19:41 -0500159loop_lock:
Chris Mason8b712842008-06-11 16:50:36 -0400160 /* take all the bios off the list at once and process them
161 * later on (without the lock held). But, remember the
162 * tail and other pointers so the bios can be properly reinserted
163 * into the list if we hit congestion
164 */
165 pending = device->pending_bios;
166 tail = device->pending_bio_tail;
167 WARN_ON(pending && !tail);
168 device->pending_bios = NULL;
169 device->pending_bio_tail = NULL;
170
171 /*
172 * if pending was null this time around, no bios need processing
173 * at all and we can stop. Otherwise it'll loop back up again
174 * and do an additional check so no bios are missed.
175 *
176 * device->running_pending is used to synchronize with the
177 * schedule_bio code.
178 */
179 if (pending) {
180 again = 1;
181 device->running_pending = 1;
182 } else {
183 again = 0;
184 device->running_pending = 0;
185 }
186 spin_unlock(&device->io_lock);
187
Chris Masond3977122009-01-05 21:25:51 -0500188 while (pending) {
Chris Mason8b712842008-06-11 16:50:36 -0400189 cur = pending;
190 pending = pending->bi_next;
191 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400192 atomic_dec(&fs_info->nr_async_bios);
193
194 if (atomic_read(&fs_info->nr_async_bios) < limit &&
195 waitqueue_active(&fs_info->async_submit_wait))
196 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400197
198 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
199 bio_get(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400200 submit_bio(cur->bi_rw, cur);
Chris Mason492bb6d2008-07-31 16:29:02 -0400201 bio_put(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400202 num_run++;
203
204 /*
205 * we made progress, there is more work to do and the bdi
206 * is now congested. Back off and let other work structs
207 * run instead
208 */
Chris Masona6837052009-02-04 09:19:41 -0500209 if (pending && bdi_write_congested(bdi) && num_run > 16 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500210 fs_info->fs_devices->open_devices > 1) {
Chris Mason8b712842008-06-11 16:50:36 -0400211 struct bio *old_head;
Chris Masonb765ead2009-04-03 10:27:10 -0400212 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400213
Chris Masonb765ead2009-04-03 10:27:10 -0400214 ioc = current->io_context;
215
216 /*
217 * the main goal here is that we don't want to
218 * block if we're going to be able to submit
219 * more requests without blocking.
220 *
221 * This code does two great things, it pokes into
222 * the elevator code from a filesystem _and_
223 * it makes assumptions about how batching works.
224 */
225 if (ioc && ioc->nr_batch_requests > 0 &&
226 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
227 (last_waited == 0 ||
228 ioc->last_waited == last_waited)) {
229 /*
230 * we want to go through our batch of
231 * requests and stop. So, we copy out
232 * the ioc->last_waited time and test
233 * against it before looping
234 */
235 last_waited = ioc->last_waited;
236 continue;
237 }
Chris Mason8b712842008-06-11 16:50:36 -0400238 spin_lock(&device->io_lock);
Chris Mason492bb6d2008-07-31 16:29:02 -0400239
Chris Mason8b712842008-06-11 16:50:36 -0400240 old_head = device->pending_bios;
241 device->pending_bios = pending;
242 if (device->pending_bio_tail)
243 tail->bi_next = old_head;
244 else
245 device->pending_bio_tail = tail;
Chris Masona6837052009-02-04 09:19:41 -0500246
247 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400248
249 spin_unlock(&device->io_lock);
250 btrfs_requeue_work(&device->work);
251 goto done;
252 }
253 }
254 if (again)
255 goto loop;
Chris Masona6837052009-02-04 09:19:41 -0500256
257 spin_lock(&device->io_lock);
258 if (device->pending_bios)
259 goto loop_lock;
260 spin_unlock(&device->io_lock);
Chris Masonbedf7622009-04-03 10:32:58 -0400261
262 /*
263 * IO has already been through a long path to get here. Checksumming,
264 * async helper threads, perhaps compression. We've done a pretty
265 * good job of collecting a batch of IO and should just unplug
266 * the device right away.
267 *
268 * This will help anyone who is waiting on the IO, they might have
269 * already unplugged, but managed to do so before the bio they
270 * cared about found its way down here.
271 */
272 blk_run_backing_dev(bdi, NULL);
Chris Mason8b712842008-06-11 16:50:36 -0400273done:
274 return 0;
275}
276
Christoph Hellwigb2950862008-12-02 09:54:17 -0500277static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400278{
279 struct btrfs_device *device;
280
281 device = container_of(work, struct btrfs_device, work);
282 run_scheduled_bios(device);
283}
284
Chris Masona1b32a52008-09-05 16:09:51 -0400285static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400286 struct btrfs_super_block *disk_super,
287 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
288{
289 struct btrfs_device *device;
290 struct btrfs_fs_devices *fs_devices;
291 u64 found_transid = btrfs_super_generation(disk_super);
292
293 fs_devices = find_fsid(disk_super->fsid);
294 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400295 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400296 if (!fs_devices)
297 return -ENOMEM;
298 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400299 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400300 list_add(&fs_devices->list, &fs_uuids);
301 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
302 fs_devices->latest_devid = devid;
303 fs_devices->latest_trans = found_transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400304 device = NULL;
305 } else {
Chris Masona4437552008-04-18 10:29:38 -0400306 device = __find_device(&fs_devices->devices, devid,
307 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400308 }
309 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500310 if (fs_devices->opened)
311 return -EBUSY;
312
Chris Mason8a4b83c2008-03-24 15:02:07 -0400313 device = kzalloc(sizeof(*device), GFP_NOFS);
314 if (!device) {
315 /* we can safely leave the fs_devices entry around */
316 return -ENOMEM;
317 }
318 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400319 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400320 memcpy(device->uuid, disk_super->dev_item.uuid,
321 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400322 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400323 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400324 device->name = kstrdup(path, GFP_NOFS);
325 if (!device->name) {
326 kfree(device);
327 return -ENOMEM;
328 }
Yan Zheng2b820322008-11-17 21:11:30 -0500329 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 list_add(&device->dev_list, &fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500331 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400332 fs_devices->num_devices++;
333 }
334
335 if (found_transid > fs_devices->latest_trans) {
336 fs_devices->latest_devid = devid;
337 fs_devices->latest_trans = found_transid;
338 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400339 *fs_devices_ret = fs_devices;
340 return 0;
341}
342
Yan Zhenge4404d62008-12-12 10:03:26 -0500343static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
344{
345 struct btrfs_fs_devices *fs_devices;
346 struct btrfs_device *device;
347 struct btrfs_device *orig_dev;
348
349 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
350 if (!fs_devices)
351 return ERR_PTR(-ENOMEM);
352
353 INIT_LIST_HEAD(&fs_devices->devices);
354 INIT_LIST_HEAD(&fs_devices->alloc_list);
355 INIT_LIST_HEAD(&fs_devices->list);
356 fs_devices->latest_devid = orig->latest_devid;
357 fs_devices->latest_trans = orig->latest_trans;
358 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
359
360 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
361 device = kzalloc(sizeof(*device), GFP_NOFS);
362 if (!device)
363 goto error;
364
365 device->name = kstrdup(orig_dev->name, GFP_NOFS);
366 if (!device->name)
367 goto error;
368
369 device->devid = orig_dev->devid;
370 device->work.func = pending_bios_fn;
371 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
372 device->barriers = 1;
373 spin_lock_init(&device->io_lock);
374 INIT_LIST_HEAD(&device->dev_list);
375 INIT_LIST_HEAD(&device->dev_alloc_list);
376
377 list_add(&device->dev_list, &fs_devices->devices);
378 device->fs_devices = fs_devices;
379 fs_devices->num_devices++;
380 }
381 return fs_devices;
382error:
383 free_fs_devices(fs_devices);
384 return ERR_PTR(-ENOMEM);
385}
386
Chris Masondfe25022008-05-13 13:46:40 -0400387int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
388{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500389 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400390
391 mutex_lock(&uuid_mutex);
392again:
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500393 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500394 if (device->in_fs_metadata)
395 continue;
396
397 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500398 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500399 device->bdev = NULL;
400 fs_devices->open_devices--;
401 }
402 if (device->writeable) {
403 list_del_init(&device->dev_alloc_list);
404 device->writeable = 0;
405 fs_devices->rw_devices--;
406 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500407 list_del_init(&device->dev_list);
408 fs_devices->num_devices--;
409 kfree(device->name);
410 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400411 }
Yan Zheng2b820322008-11-17 21:11:30 -0500412
413 if (fs_devices->seed) {
414 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500415 goto again;
416 }
417
Chris Masondfe25022008-05-13 13:46:40 -0400418 mutex_unlock(&uuid_mutex);
419 return 0;
420}
Chris Masona0af4692008-05-13 16:03:06 -0400421
Yan Zheng2b820322008-11-17 21:11:30 -0500422static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400423{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400424 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500425
Yan Zheng2b820322008-11-17 21:11:30 -0500426 if (--fs_devices->opened > 0)
427 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400428
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500429 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400430 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500431 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400432 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400433 }
Yan Zheng2b820322008-11-17 21:11:30 -0500434 if (device->writeable) {
435 list_del_init(&device->dev_alloc_list);
436 fs_devices->rw_devices--;
437 }
438
Chris Mason8a4b83c2008-03-24 15:02:07 -0400439 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500440 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400441 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400442 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500443 WARN_ON(fs_devices->open_devices);
444 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500445 fs_devices->opened = 0;
446 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500447
Chris Mason8a4b83c2008-03-24 15:02:07 -0400448 return 0;
449}
450
Yan Zheng2b820322008-11-17 21:11:30 -0500451int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
452{
Yan Zhenge4404d62008-12-12 10:03:26 -0500453 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500454 int ret;
455
456 mutex_lock(&uuid_mutex);
457 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500458 if (!fs_devices->opened) {
459 seed_devices = fs_devices->seed;
460 fs_devices->seed = NULL;
461 }
Yan Zheng2b820322008-11-17 21:11:30 -0500462 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500463
464 while (seed_devices) {
465 fs_devices = seed_devices;
466 seed_devices = fs_devices->seed;
467 __btrfs_close_devices(fs_devices);
468 free_fs_devices(fs_devices);
469 }
Yan Zheng2b820322008-11-17 21:11:30 -0500470 return ret;
471}
472
Yan Zhenge4404d62008-12-12 10:03:26 -0500473static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
474 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400475{
476 struct block_device *bdev;
477 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400478 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400479 struct block_device *latest_bdev = NULL;
480 struct buffer_head *bh;
481 struct btrfs_super_block *disk_super;
482 u64 latest_devid = 0;
483 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400484 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500485 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400486 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400487
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500488 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400489 if (device->bdev)
490 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400491 if (!device->name)
492 continue;
493
Chris Mason15916de2008-11-19 21:17:22 -0500494 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400495 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500496 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400497 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 }
Chris Masona061fc82008-05-07 11:43:44 -0400499 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400500
Yan Zhenga512bbf2008-12-08 16:46:26 -0500501 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400502 if (!bh)
503 goto error_close;
504
505 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masona0af4692008-05-13 16:03:06 -0400506 devid = le64_to_cpu(disk_super->dev_item.devid);
507 if (devid != device->devid)
508 goto error_brelse;
509
Yan Zheng2b820322008-11-17 21:11:30 -0500510 if (memcmp(device->uuid, disk_super->dev_item.uuid,
511 BTRFS_UUID_SIZE))
512 goto error_brelse;
513
514 device->generation = btrfs_super_generation(disk_super);
515 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400516 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500517 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400518 latest_bdev = bdev;
519 }
520
Yan Zheng2b820322008-11-17 21:11:30 -0500521 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
522 device->writeable = 0;
523 } else {
524 device->writeable = !bdev_read_only(bdev);
525 seeding = 0;
526 }
527
Chris Mason8a4b83c2008-03-24 15:02:07 -0400528 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400529 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500530 device->mode = flags;
531
Chris Masona0af4692008-05-13 16:03:06 -0400532 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500533 if (device->writeable) {
534 fs_devices->rw_devices++;
535 list_add(&device->dev_alloc_list,
536 &fs_devices->alloc_list);
537 }
Chris Masona0af4692008-05-13 16:03:06 -0400538 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400539
Chris Masona0af4692008-05-13 16:03:06 -0400540error_brelse:
541 brelse(bh);
542error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500543 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400544error:
545 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400546 }
Chris Masona0af4692008-05-13 16:03:06 -0400547 if (fs_devices->open_devices == 0) {
548 ret = -EIO;
549 goto out;
550 }
Yan Zheng2b820322008-11-17 21:11:30 -0500551 fs_devices->seeding = seeding;
552 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400553 fs_devices->latest_bdev = latest_bdev;
554 fs_devices->latest_devid = latest_devid;
555 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500556 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400557out:
Yan Zheng2b820322008-11-17 21:11:30 -0500558 return ret;
559}
560
561int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500562 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500563{
564 int ret;
565
566 mutex_lock(&uuid_mutex);
567 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500568 fs_devices->opened++;
569 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500570 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500571 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500572 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400573 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 return ret;
575}
576
Christoph Hellwig97288f22008-12-02 06:36:09 -0500577int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578 struct btrfs_fs_devices **fs_devices_ret)
579{
580 struct btrfs_super_block *disk_super;
581 struct block_device *bdev;
582 struct buffer_head *bh;
583 int ret;
584 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400585 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400586
587 mutex_lock(&uuid_mutex);
588
Chris Mason15916de2008-11-19 21:17:22 -0500589 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400590
591 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400592 ret = PTR_ERR(bdev);
593 goto error;
594 }
595
596 ret = set_blocksize(bdev, 4096);
597 if (ret)
598 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500599 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400600 if (!bh) {
601 ret = -EIO;
602 goto error_close;
603 }
604 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400605 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400606 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400607 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500608 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400609 else {
610 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500611 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400612 *(unsigned long long *)disk_super->fsid,
613 *(unsigned long long *)(disk_super->fsid + 8));
614 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500615 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500616 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400617 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
618
Chris Mason8a4b83c2008-03-24 15:02:07 -0400619 brelse(bh);
620error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500621 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622error:
623 mutex_unlock(&uuid_mutex);
624 return ret;
625}
Chris Mason0b86a832008-03-24 15:01:56 -0400626
627/*
628 * this uses a pretty simple search, the expectation is that it is
629 * called very infrequently and that a given device has a small number
630 * of extents
631 */
Chris Masona1b32a52008-09-05 16:09:51 -0400632static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
633 struct btrfs_device *device,
Chris Masona1b32a52008-09-05 16:09:51 -0400634 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400635{
636 struct btrfs_key key;
637 struct btrfs_root *root = device->dev_root;
638 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500639 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400640 u64 hole_size = 0;
641 u64 last_byte = 0;
642 u64 search_start = 0;
643 u64 search_end = device->total_bytes;
644 int ret;
645 int slot = 0;
646 int start_found;
647 struct extent_buffer *l;
648
Yan Zheng2b820322008-11-17 21:11:30 -0500649 path = btrfs_alloc_path();
650 if (!path)
651 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400652 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500653 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400654
655 /* FIXME use last free of some kind */
656
Chris Mason8a4b83c2008-03-24 15:02:07 -0400657 /* we don't want to overwrite the superblock on the drive,
658 * so we make sure to start at an offset of at least 1MB
659 */
660 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400661
662 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
663 search_start = max(root->fs_info->alloc_start, search_start);
664
Chris Mason0b86a832008-03-24 15:01:56 -0400665 key.objectid = device->devid;
666 key.offset = search_start;
667 key.type = BTRFS_DEV_EXTENT_KEY;
668 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
669 if (ret < 0)
670 goto error;
671 ret = btrfs_previous_item(root, path, 0, key.type);
672 if (ret < 0)
673 goto error;
674 l = path->nodes[0];
675 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
676 while (1) {
677 l = path->nodes[0];
678 slot = path->slots[0];
679 if (slot >= btrfs_header_nritems(l)) {
680 ret = btrfs_next_leaf(root, path);
681 if (ret == 0)
682 continue;
683 if (ret < 0)
684 goto error;
685no_more_items:
686 if (!start_found) {
687 if (search_start >= search_end) {
688 ret = -ENOSPC;
689 goto error;
690 }
691 *start = search_start;
692 start_found = 1;
693 goto check_pending;
694 }
695 *start = last_byte > search_start ?
696 last_byte : search_start;
697 if (search_end <= *start) {
698 ret = -ENOSPC;
699 goto error;
700 }
701 goto check_pending;
702 }
703 btrfs_item_key_to_cpu(l, &key, slot);
704
705 if (key.objectid < device->devid)
706 goto next;
707
708 if (key.objectid > device->devid)
709 goto no_more_items;
710
711 if (key.offset >= search_start && key.offset > last_byte &&
712 start_found) {
713 if (last_byte < search_start)
714 last_byte = search_start;
715 hole_size = key.offset - last_byte;
716 if (key.offset > last_byte &&
717 hole_size >= num_bytes) {
718 *start = last_byte;
719 goto check_pending;
720 }
721 }
Chris Masond3977122009-01-05 21:25:51 -0500722 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400723 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400724
725 start_found = 1;
726 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
727 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
728next:
729 path->slots[0]++;
730 cond_resched();
731 }
732check_pending:
733 /* we have to make sure we didn't find an extent that has already
734 * been allocated by the map tree or the original allocation
735 */
Chris Mason0b86a832008-03-24 15:01:56 -0400736 BUG_ON(*start < search_start);
737
Chris Mason6324fbf2008-03-24 15:01:59 -0400738 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400739 ret = -ENOSPC;
740 goto error;
741 }
742 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500743 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400744
745error:
Yan Zheng2b820322008-11-17 21:11:30 -0500746 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400747 return ret;
748}
749
Christoph Hellwigb2950862008-12-02 09:54:17 -0500750static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400751 struct btrfs_device *device,
752 u64 start)
753{
754 int ret;
755 struct btrfs_path *path;
756 struct btrfs_root *root = device->dev_root;
757 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400758 struct btrfs_key found_key;
759 struct extent_buffer *leaf = NULL;
760 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400761
762 path = btrfs_alloc_path();
763 if (!path)
764 return -ENOMEM;
765
766 key.objectid = device->devid;
767 key.offset = start;
768 key.type = BTRFS_DEV_EXTENT_KEY;
769
770 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400771 if (ret > 0) {
772 ret = btrfs_previous_item(root, path, key.objectid,
773 BTRFS_DEV_EXTENT_KEY);
774 BUG_ON(ret);
775 leaf = path->nodes[0];
776 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
777 extent = btrfs_item_ptr(leaf, path->slots[0],
778 struct btrfs_dev_extent);
779 BUG_ON(found_key.offset > start || found_key.offset +
780 btrfs_dev_extent_length(leaf, extent) < start);
781 ret = 0;
782 } else if (ret == 0) {
783 leaf = path->nodes[0];
784 extent = btrfs_item_ptr(leaf, path->slots[0],
785 struct btrfs_dev_extent);
786 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400787 BUG_ON(ret);
788
Chris Masondfe25022008-05-13 13:46:40 -0400789 if (device->bytes_used > 0)
790 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400791 ret = btrfs_del_item(trans, root, path);
792 BUG_ON(ret);
793
794 btrfs_free_path(path);
795 return ret;
796}
797
Yan Zheng2b820322008-11-17 21:11:30 -0500798int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400799 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400800 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500801 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400802{
803 int ret;
804 struct btrfs_path *path;
805 struct btrfs_root *root = device->dev_root;
806 struct btrfs_dev_extent *extent;
807 struct extent_buffer *leaf;
808 struct btrfs_key key;
809
Chris Masondfe25022008-05-13 13:46:40 -0400810 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400811 path = btrfs_alloc_path();
812 if (!path)
813 return -ENOMEM;
814
Chris Mason0b86a832008-03-24 15:01:56 -0400815 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500816 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400817 key.type = BTRFS_DEV_EXTENT_KEY;
818 ret = btrfs_insert_empty_item(trans, root, path, &key,
819 sizeof(*extent));
820 BUG_ON(ret);
821
822 leaf = path->nodes[0];
823 extent = btrfs_item_ptr(leaf, path->slots[0],
824 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400825 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
826 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
827 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
828
829 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
830 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
831 BTRFS_UUID_SIZE);
832
Chris Mason0b86a832008-03-24 15:01:56 -0400833 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
834 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400835 btrfs_free_path(path);
836 return ret;
837}
838
Chris Masona1b32a52008-09-05 16:09:51 -0400839static noinline int find_next_chunk(struct btrfs_root *root,
840 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400841{
842 struct btrfs_path *path;
843 int ret;
844 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400845 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400846 struct btrfs_key found_key;
847
848 path = btrfs_alloc_path();
849 BUG_ON(!path);
850
Chris Masone17cade2008-04-15 15:41:47 -0400851 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400852 key.offset = (u64)-1;
853 key.type = BTRFS_CHUNK_ITEM_KEY;
854
855 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
856 if (ret < 0)
857 goto error;
858
859 BUG_ON(ret == 0);
860
861 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
862 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400863 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400864 } else {
865 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
866 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400867 if (found_key.objectid != objectid)
868 *offset = 0;
869 else {
870 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
871 struct btrfs_chunk);
872 *offset = found_key.offset +
873 btrfs_chunk_length(path->nodes[0], chunk);
874 }
Chris Mason0b86a832008-03-24 15:01:56 -0400875 }
876 ret = 0;
877error:
878 btrfs_free_path(path);
879 return ret;
880}
881
Yan Zheng2b820322008-11-17 21:11:30 -0500882static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400883{
884 int ret;
885 struct btrfs_key key;
886 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500887 struct btrfs_path *path;
888
889 root = root->fs_info->chunk_root;
890
891 path = btrfs_alloc_path();
892 if (!path)
893 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400894
895 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
896 key.type = BTRFS_DEV_ITEM_KEY;
897 key.offset = (u64)-1;
898
899 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
900 if (ret < 0)
901 goto error;
902
903 BUG_ON(ret == 0);
904
905 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
906 BTRFS_DEV_ITEM_KEY);
907 if (ret) {
908 *objectid = 1;
909 } else {
910 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
911 path->slots[0]);
912 *objectid = found_key.offset + 1;
913 }
914 ret = 0;
915error:
Yan Zheng2b820322008-11-17 21:11:30 -0500916 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400917 return ret;
918}
919
920/*
921 * the device information is stored in the chunk root
922 * the btrfs_device struct should be fully filled in
923 */
924int btrfs_add_device(struct btrfs_trans_handle *trans,
925 struct btrfs_root *root,
926 struct btrfs_device *device)
927{
928 int ret;
929 struct btrfs_path *path;
930 struct btrfs_dev_item *dev_item;
931 struct extent_buffer *leaf;
932 struct btrfs_key key;
933 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -0400934
935 root = root->fs_info->chunk_root;
936
937 path = btrfs_alloc_path();
938 if (!path)
939 return -ENOMEM;
940
Chris Mason0b86a832008-03-24 15:01:56 -0400941 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
942 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -0500943 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400944
945 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400946 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400947 if (ret)
948 goto out;
949
950 leaf = path->nodes[0];
951 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
952
953 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -0500954 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400955 btrfs_set_device_type(leaf, dev_item, device->type);
956 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
957 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
958 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400959 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
960 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400961 btrfs_set_device_group(leaf, dev_item, 0);
962 btrfs_set_device_seek_speed(leaf, dev_item, 0);
963 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -0500964 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400965
Chris Mason0b86a832008-03-24 15:01:56 -0400966 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400967 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500968 ptr = (unsigned long)btrfs_device_fsid(dev_item);
969 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400970 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400971
Yan Zheng2b820322008-11-17 21:11:30 -0500972 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400973out:
974 btrfs_free_path(path);
975 return ret;
976}
Chris Mason8f18cf12008-04-25 16:53:30 -0400977
Chris Masona061fc82008-05-07 11:43:44 -0400978static int btrfs_rm_dev_item(struct btrfs_root *root,
979 struct btrfs_device *device)
980{
981 int ret;
982 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -0400983 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400984 struct btrfs_trans_handle *trans;
985
986 root = root->fs_info->chunk_root;
987
988 path = btrfs_alloc_path();
989 if (!path)
990 return -ENOMEM;
991
992 trans = btrfs_start_transaction(root, 1);
993 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
994 key.type = BTRFS_DEV_ITEM_KEY;
995 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400996 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400997
998 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
999 if (ret < 0)
1000 goto out;
1001
1002 if (ret > 0) {
1003 ret = -ENOENT;
1004 goto out;
1005 }
1006
1007 ret = btrfs_del_item(trans, root, path);
1008 if (ret)
1009 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001010out:
1011 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001012 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001013 btrfs_commit_transaction(trans, root);
1014 return ret;
1015}
1016
1017int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1018{
1019 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001020 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001021 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001022 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001023 struct btrfs_super_block *disk_super;
1024 u64 all_avail;
1025 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001026 u64 num_devices;
1027 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001028 int ret = 0;
1029
Chris Masona061fc82008-05-07 11:43:44 -04001030 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001031 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001032
1033 all_avail = root->fs_info->avail_data_alloc_bits |
1034 root->fs_info->avail_system_alloc_bits |
1035 root->fs_info->avail_metadata_alloc_bits;
1036
1037 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001038 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001039 printk(KERN_ERR "btrfs: unable to go below four devices "
1040 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001041 ret = -EINVAL;
1042 goto out;
1043 }
1044
1045 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001046 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001047 printk(KERN_ERR "btrfs: unable to go below two "
1048 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001049 ret = -EINVAL;
1050 goto out;
1051 }
1052
Chris Masondfe25022008-05-13 13:46:40 -04001053 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001054 struct list_head *devices;
1055 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001056
Chris Masondfe25022008-05-13 13:46:40 -04001057 device = NULL;
1058 devices = &root->fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001059 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001060 if (tmp->in_fs_metadata && !tmp->bdev) {
1061 device = tmp;
1062 break;
1063 }
1064 }
1065 bdev = NULL;
1066 bh = NULL;
1067 disk_super = NULL;
1068 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001069 printk(KERN_ERR "btrfs: no missing devices found to "
1070 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001071 goto out;
1072 }
Chris Masondfe25022008-05-13 13:46:40 -04001073 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001074 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001075 root->fs_info->bdev_holder);
1076 if (IS_ERR(bdev)) {
1077 ret = PTR_ERR(bdev);
1078 goto out;
1079 }
1080
Yan Zheng2b820322008-11-17 21:11:30 -05001081 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001082 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001083 if (!bh) {
1084 ret = -EIO;
1085 goto error_close;
1086 }
1087 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masondfe25022008-05-13 13:46:40 -04001088 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001089 dev_uuid = disk_super->dev_item.uuid;
1090 device = btrfs_find_device(root, devid, dev_uuid,
1091 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001092 if (!device) {
1093 ret = -ENOENT;
1094 goto error_brelse;
1095 }
Chris Masondfe25022008-05-13 13:46:40 -04001096 }
Yan Zheng2b820322008-11-17 21:11:30 -05001097
1098 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001099 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1100 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001101 ret = -EINVAL;
1102 goto error_brelse;
1103 }
1104
1105 if (device->writeable) {
1106 list_del_init(&device->dev_alloc_list);
1107 root->fs_info->fs_devices->rw_devices--;
1108 }
Chris Masona061fc82008-05-07 11:43:44 -04001109
1110 ret = btrfs_shrink_device(device, 0);
1111 if (ret)
1112 goto error_brelse;
1113
Chris Masona061fc82008-05-07 11:43:44 -04001114 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1115 if (ret)
1116 goto error_brelse;
1117
Yan Zheng2b820322008-11-17 21:11:30 -05001118 device->in_fs_metadata = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001119 list_del_init(&device->dev_list);
1120 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001121
1122 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1123 struct btrfs_device, dev_list);
1124 if (device->bdev == root->fs_info->sb->s_bdev)
1125 root->fs_info->sb->s_bdev = next_device->bdev;
1126 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1127 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1128
Yan Zhenge4404d62008-12-12 10:03:26 -05001129 if (device->bdev) {
1130 close_bdev_exclusive(device->bdev, device->mode);
1131 device->bdev = NULL;
1132 device->fs_devices->open_devices--;
1133 }
1134
Yan Zheng2b820322008-11-17 21:11:30 -05001135 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1136 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1137
Yan Zhenge4404d62008-12-12 10:03:26 -05001138 if (device->fs_devices->open_devices == 0) {
1139 struct btrfs_fs_devices *fs_devices;
1140 fs_devices = root->fs_info->fs_devices;
1141 while (fs_devices) {
1142 if (fs_devices->seed == device->fs_devices)
1143 break;
1144 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001145 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001146 fs_devices->seed = device->fs_devices->seed;
1147 device->fs_devices->seed = NULL;
1148 __btrfs_close_devices(device->fs_devices);
1149 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001150 }
1151
1152 /*
1153 * at this point, the device is zero sized. We want to
1154 * remove it from the devices list and zero out the old super
1155 */
1156 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001157 /* make sure this device isn't detected as part of
1158 * the FS anymore
1159 */
1160 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1161 set_buffer_dirty(bh);
1162 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001163 }
Chris Masona061fc82008-05-07 11:43:44 -04001164
Chris Masona061fc82008-05-07 11:43:44 -04001165 kfree(device->name);
1166 kfree(device);
1167 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001168
1169error_brelse:
1170 brelse(bh);
1171error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001172 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001173 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001174out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001175 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001176 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001177 return ret;
1178}
1179
Yan Zheng2b820322008-11-17 21:11:30 -05001180/*
1181 * does all the dirty work required for changing file system's UUID.
1182 */
1183static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root)
1185{
1186 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1187 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001188 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001189 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1190 struct btrfs_device *device;
1191 u64 super_flags;
1192
1193 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001194 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001195 return -EINVAL;
1196
Yan Zhenge4404d62008-12-12 10:03:26 -05001197 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1198 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001199 return -ENOMEM;
1200
Yan Zhenge4404d62008-12-12 10:03:26 -05001201 old_devices = clone_fs_devices(fs_devices);
1202 if (IS_ERR(old_devices)) {
1203 kfree(seed_devices);
1204 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001205 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001206
Yan Zheng2b820322008-11-17 21:11:30 -05001207 list_add(&old_devices->list, &fs_uuids);
1208
Yan Zhenge4404d62008-12-12 10:03:26 -05001209 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1210 seed_devices->opened = 1;
1211 INIT_LIST_HEAD(&seed_devices->devices);
1212 INIT_LIST_HEAD(&seed_devices->alloc_list);
1213 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1214 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1215 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1216 device->fs_devices = seed_devices;
1217 }
1218
Yan Zheng2b820322008-11-17 21:11:30 -05001219 fs_devices->seeding = 0;
1220 fs_devices->num_devices = 0;
1221 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001222 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001223
1224 generate_random_uuid(fs_devices->fsid);
1225 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1226 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1227 super_flags = btrfs_super_flags(disk_super) &
1228 ~BTRFS_SUPER_FLAG_SEEDING;
1229 btrfs_set_super_flags(disk_super, super_flags);
1230
1231 return 0;
1232}
1233
1234/*
1235 * strore the expected generation for seed devices in device items.
1236 */
1237static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1238 struct btrfs_root *root)
1239{
1240 struct btrfs_path *path;
1241 struct extent_buffer *leaf;
1242 struct btrfs_dev_item *dev_item;
1243 struct btrfs_device *device;
1244 struct btrfs_key key;
1245 u8 fs_uuid[BTRFS_UUID_SIZE];
1246 u8 dev_uuid[BTRFS_UUID_SIZE];
1247 u64 devid;
1248 int ret;
1249
1250 path = btrfs_alloc_path();
1251 if (!path)
1252 return -ENOMEM;
1253
1254 root = root->fs_info->chunk_root;
1255 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1256 key.offset = 0;
1257 key.type = BTRFS_DEV_ITEM_KEY;
1258
1259 while (1) {
1260 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1261 if (ret < 0)
1262 goto error;
1263
1264 leaf = path->nodes[0];
1265next_slot:
1266 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1267 ret = btrfs_next_leaf(root, path);
1268 if (ret > 0)
1269 break;
1270 if (ret < 0)
1271 goto error;
1272 leaf = path->nodes[0];
1273 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1274 btrfs_release_path(root, path);
1275 continue;
1276 }
1277
1278 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1279 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1280 key.type != BTRFS_DEV_ITEM_KEY)
1281 break;
1282
1283 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1284 struct btrfs_dev_item);
1285 devid = btrfs_device_id(leaf, dev_item);
1286 read_extent_buffer(leaf, dev_uuid,
1287 (unsigned long)btrfs_device_uuid(dev_item),
1288 BTRFS_UUID_SIZE);
1289 read_extent_buffer(leaf, fs_uuid,
1290 (unsigned long)btrfs_device_fsid(dev_item),
1291 BTRFS_UUID_SIZE);
1292 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1293 BUG_ON(!device);
1294
1295 if (device->fs_devices->seeding) {
1296 btrfs_set_device_generation(leaf, dev_item,
1297 device->generation);
1298 btrfs_mark_buffer_dirty(leaf);
1299 }
1300
1301 path->slots[0]++;
1302 goto next_slot;
1303 }
1304 ret = 0;
1305error:
1306 btrfs_free_path(path);
1307 return ret;
1308}
1309
Chris Mason788f20e2008-04-28 15:29:42 -04001310int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1311{
1312 struct btrfs_trans_handle *trans;
1313 struct btrfs_device *device;
1314 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001315 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001316 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001317 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001318 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001319 int ret = 0;
1320
Yan Zheng2b820322008-11-17 21:11:30 -05001321 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1322 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001323
Chris Mason15916de2008-11-19 21:17:22 -05001324 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Chris Masond3977122009-01-05 21:25:51 -05001325 if (!bdev)
Chris Mason788f20e2008-04-28 15:29:42 -04001326 return -EIO;
Chris Masona2135012008-06-25 16:01:30 -04001327
Yan Zheng2b820322008-11-17 21:11:30 -05001328 if (root->fs_info->fs_devices->seeding) {
1329 seeding_dev = 1;
1330 down_write(&sb->s_umount);
1331 mutex_lock(&uuid_mutex);
1332 }
1333
Chris Mason8c8bee12008-09-29 11:19:10 -04001334 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001335 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001336
Chris Mason788f20e2008-04-28 15:29:42 -04001337 devices = &root->fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001338 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001339 if (device->bdev == bdev) {
1340 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001341 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001342 }
1343 }
1344
1345 device = kzalloc(sizeof(*device), GFP_NOFS);
1346 if (!device) {
1347 /* we can safely leave the fs_devices entry around */
1348 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001349 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001350 }
1351
Chris Mason788f20e2008-04-28 15:29:42 -04001352 device->name = kstrdup(device_path, GFP_NOFS);
1353 if (!device->name) {
1354 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001355 ret = -ENOMEM;
1356 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001357 }
Yan Zheng2b820322008-11-17 21:11:30 -05001358
1359 ret = find_next_devid(root, &device->devid);
1360 if (ret) {
1361 kfree(device);
1362 goto error;
1363 }
1364
1365 trans = btrfs_start_transaction(root, 1);
1366 lock_chunks(root);
1367
1368 device->barriers = 1;
1369 device->writeable = 1;
1370 device->work.func = pending_bios_fn;
1371 generate_random_uuid(device->uuid);
1372 spin_lock_init(&device->io_lock);
1373 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001374 device->io_width = root->sectorsize;
1375 device->io_align = root->sectorsize;
1376 device->sector_size = root->sectorsize;
1377 device->total_bytes = i_size_read(bdev->bd_inode);
1378 device->dev_root = root->fs_info->dev_root;
1379 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001380 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001381 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001382 set_blocksize(device->bdev, 4096);
1383
Yan Zheng2b820322008-11-17 21:11:30 -05001384 if (seeding_dev) {
1385 sb->s_flags &= ~MS_RDONLY;
1386 ret = btrfs_prepare_sprout(trans, root);
1387 BUG_ON(ret);
1388 }
1389
1390 device->fs_devices = root->fs_info->fs_devices;
1391 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1392 list_add(&device->dev_alloc_list,
1393 &root->fs_info->fs_devices->alloc_list);
1394 root->fs_info->fs_devices->num_devices++;
1395 root->fs_info->fs_devices->open_devices++;
1396 root->fs_info->fs_devices->rw_devices++;
1397 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1398
Chris Mason788f20e2008-04-28 15:29:42 -04001399 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1400 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1401 total_bytes + device->total_bytes);
1402
1403 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1404 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1405 total_bytes + 1);
1406
Yan Zheng2b820322008-11-17 21:11:30 -05001407 if (seeding_dev) {
1408 ret = init_first_rw_device(trans, root, device);
1409 BUG_ON(ret);
1410 ret = btrfs_finish_sprout(trans, root);
1411 BUG_ON(ret);
1412 } else {
1413 ret = btrfs_add_device(trans, root, device);
1414 }
1415
Chris Mason913d9522009-03-10 13:17:18 -04001416 /*
1417 * we've got more storage, clear any full flags on the space
1418 * infos
1419 */
1420 btrfs_clear_space_info_full(root->fs_info);
1421
Chris Mason7d9eb122008-07-08 14:19:17 -04001422 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001423 btrfs_commit_transaction(trans, root);
1424
1425 if (seeding_dev) {
1426 mutex_unlock(&uuid_mutex);
1427 up_write(&sb->s_umount);
1428
1429 ret = btrfs_relocate_sys_chunks(root);
1430 BUG_ON(ret);
1431 }
1432out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001433 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001434 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001435error:
Chris Mason15916de2008-11-19 21:17:22 -05001436 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001437 if (seeding_dev) {
1438 mutex_unlock(&uuid_mutex);
1439 up_write(&sb->s_umount);
1440 }
Chris Mason788f20e2008-04-28 15:29:42 -04001441 goto out;
1442}
1443
Chris Masond3977122009-01-05 21:25:51 -05001444static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1445 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001446{
1447 int ret;
1448 struct btrfs_path *path;
1449 struct btrfs_root *root;
1450 struct btrfs_dev_item *dev_item;
1451 struct extent_buffer *leaf;
1452 struct btrfs_key key;
1453
1454 root = device->dev_root->fs_info->chunk_root;
1455
1456 path = btrfs_alloc_path();
1457 if (!path)
1458 return -ENOMEM;
1459
1460 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1461 key.type = BTRFS_DEV_ITEM_KEY;
1462 key.offset = device->devid;
1463
1464 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1465 if (ret < 0)
1466 goto out;
1467
1468 if (ret > 0) {
1469 ret = -ENOENT;
1470 goto out;
1471 }
1472
1473 leaf = path->nodes[0];
1474 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1475
1476 btrfs_set_device_id(leaf, dev_item, device->devid);
1477 btrfs_set_device_type(leaf, dev_item, device->type);
1478 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1479 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1480 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001481 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1482 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1483 btrfs_mark_buffer_dirty(leaf);
1484
1485out:
1486 btrfs_free_path(path);
1487 return ret;
1488}
1489
Chris Mason7d9eb122008-07-08 14:19:17 -04001490static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001491 struct btrfs_device *device, u64 new_size)
1492{
1493 struct btrfs_super_block *super_copy =
1494 &device->dev_root->fs_info->super_copy;
1495 u64 old_total = btrfs_super_total_bytes(super_copy);
1496 u64 diff = new_size - device->total_bytes;
1497
Yan Zheng2b820322008-11-17 21:11:30 -05001498 if (!device->writeable)
1499 return -EACCES;
1500 if (new_size <= device->total_bytes)
1501 return -EINVAL;
1502
Chris Mason8f18cf12008-04-25 16:53:30 -04001503 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001504 device->fs_devices->total_rw_bytes += diff;
1505
1506 device->total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001507 btrfs_clear_space_info_full(device->dev_root->fs_info);
1508
Chris Mason8f18cf12008-04-25 16:53:30 -04001509 return btrfs_update_device(trans, device);
1510}
1511
Chris Mason7d9eb122008-07-08 14:19:17 -04001512int btrfs_grow_device(struct btrfs_trans_handle *trans,
1513 struct btrfs_device *device, u64 new_size)
1514{
1515 int ret;
1516 lock_chunks(device->dev_root);
1517 ret = __btrfs_grow_device(trans, device, new_size);
1518 unlock_chunks(device->dev_root);
1519 return ret;
1520}
1521
Chris Mason8f18cf12008-04-25 16:53:30 -04001522static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1523 struct btrfs_root *root,
1524 u64 chunk_tree, u64 chunk_objectid,
1525 u64 chunk_offset)
1526{
1527 int ret;
1528 struct btrfs_path *path;
1529 struct btrfs_key key;
1530
1531 root = root->fs_info->chunk_root;
1532 path = btrfs_alloc_path();
1533 if (!path)
1534 return -ENOMEM;
1535
1536 key.objectid = chunk_objectid;
1537 key.offset = chunk_offset;
1538 key.type = BTRFS_CHUNK_ITEM_KEY;
1539
1540 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1541 BUG_ON(ret);
1542
1543 ret = btrfs_del_item(trans, root, path);
1544 BUG_ON(ret);
1545
1546 btrfs_free_path(path);
1547 return 0;
1548}
1549
Christoph Hellwigb2950862008-12-02 09:54:17 -05001550static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001551 chunk_offset)
1552{
1553 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1554 struct btrfs_disk_key *disk_key;
1555 struct btrfs_chunk *chunk;
1556 u8 *ptr;
1557 int ret = 0;
1558 u32 num_stripes;
1559 u32 array_size;
1560 u32 len = 0;
1561 u32 cur;
1562 struct btrfs_key key;
1563
1564 array_size = btrfs_super_sys_array_size(super_copy);
1565
1566 ptr = super_copy->sys_chunk_array;
1567 cur = 0;
1568
1569 while (cur < array_size) {
1570 disk_key = (struct btrfs_disk_key *)ptr;
1571 btrfs_disk_key_to_cpu(&key, disk_key);
1572
1573 len = sizeof(*disk_key);
1574
1575 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1576 chunk = (struct btrfs_chunk *)(ptr + len);
1577 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1578 len += btrfs_chunk_item_size(num_stripes);
1579 } else {
1580 ret = -EIO;
1581 break;
1582 }
1583 if (key.objectid == chunk_objectid &&
1584 key.offset == chunk_offset) {
1585 memmove(ptr, ptr + len, array_size - (cur + len));
1586 array_size -= len;
1587 btrfs_set_super_sys_array_size(super_copy, array_size);
1588 } else {
1589 ptr += len;
1590 cur += len;
1591 }
1592 }
1593 return ret;
1594}
1595
Christoph Hellwigb2950862008-12-02 09:54:17 -05001596static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001597 u64 chunk_tree, u64 chunk_objectid,
1598 u64 chunk_offset)
1599{
1600 struct extent_map_tree *em_tree;
1601 struct btrfs_root *extent_root;
1602 struct btrfs_trans_handle *trans;
1603 struct extent_map *em;
1604 struct map_lookup *map;
1605 int ret;
1606 int i;
1607
Chris Masond3977122009-01-05 21:25:51 -05001608 printk(KERN_INFO "btrfs relocating chunk %llu\n",
Chris Mason323da792008-05-09 11:46:48 -04001609 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001610 root = root->fs_info->chunk_root;
1611 extent_root = root->fs_info->extent_root;
1612 em_tree = &root->fs_info->mapping_tree.map_tree;
1613
1614 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001615 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001616 BUG_ON(ret);
1617
1618 trans = btrfs_start_transaction(root, 1);
1619 BUG_ON(!trans);
1620
Chris Mason7d9eb122008-07-08 14:19:17 -04001621 lock_chunks(root);
1622
Chris Mason8f18cf12008-04-25 16:53:30 -04001623 /*
1624 * step two, delete the device extents and the
1625 * chunk tree entries
1626 */
1627 spin_lock(&em_tree->lock);
1628 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1629 spin_unlock(&em_tree->lock);
1630
Chris Masona061fc82008-05-07 11:43:44 -04001631 BUG_ON(em->start > chunk_offset ||
1632 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001633 map = (struct map_lookup *)em->bdev;
1634
1635 for (i = 0; i < map->num_stripes; i++) {
1636 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1637 map->stripes[i].physical);
1638 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001639
Chris Masondfe25022008-05-13 13:46:40 -04001640 if (map->stripes[i].dev) {
1641 ret = btrfs_update_device(trans, map->stripes[i].dev);
1642 BUG_ON(ret);
1643 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001644 }
1645 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1646 chunk_offset);
1647
1648 BUG_ON(ret);
1649
1650 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1651 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1652 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001653 }
1654
Zheng Yan1a40e232008-09-26 10:09:34 -04001655 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1656 BUG_ON(ret);
1657
Chris Mason8f18cf12008-04-25 16:53:30 -04001658 spin_lock(&em_tree->lock);
1659 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001660 spin_unlock(&em_tree->lock);
1661
Chris Mason8f18cf12008-04-25 16:53:30 -04001662 kfree(map);
1663 em->bdev = NULL;
1664
1665 /* once for the tree */
1666 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001667 /* once for us */
1668 free_extent_map(em);
1669
Chris Mason7d9eb122008-07-08 14:19:17 -04001670 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001671 btrfs_end_transaction(trans, root);
1672 return 0;
1673}
1674
Yan Zheng2b820322008-11-17 21:11:30 -05001675static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1676{
1677 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1678 struct btrfs_path *path;
1679 struct extent_buffer *leaf;
1680 struct btrfs_chunk *chunk;
1681 struct btrfs_key key;
1682 struct btrfs_key found_key;
1683 u64 chunk_tree = chunk_root->root_key.objectid;
1684 u64 chunk_type;
1685 int ret;
1686
1687 path = btrfs_alloc_path();
1688 if (!path)
1689 return -ENOMEM;
1690
1691 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1692 key.offset = (u64)-1;
1693 key.type = BTRFS_CHUNK_ITEM_KEY;
1694
1695 while (1) {
1696 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1697 if (ret < 0)
1698 goto error;
1699 BUG_ON(ret == 0);
1700
1701 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1702 key.type);
1703 if (ret < 0)
1704 goto error;
1705 if (ret > 0)
1706 break;
1707
1708 leaf = path->nodes[0];
1709 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1710
1711 chunk = btrfs_item_ptr(leaf, path->slots[0],
1712 struct btrfs_chunk);
1713 chunk_type = btrfs_chunk_type(leaf, chunk);
1714 btrfs_release_path(chunk_root, path);
1715
1716 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1717 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1718 found_key.objectid,
1719 found_key.offset);
1720 BUG_ON(ret);
1721 }
1722
1723 if (found_key.offset == 0)
1724 break;
1725 key.offset = found_key.offset - 1;
1726 }
1727 ret = 0;
1728error:
1729 btrfs_free_path(path);
1730 return ret;
1731}
1732
Chris Masonec44a352008-04-28 15:29:52 -04001733static u64 div_factor(u64 num, int factor)
1734{
1735 if (factor == 10)
1736 return num;
1737 num *= factor;
1738 do_div(num, 10);
1739 return num;
1740}
1741
Chris Masonec44a352008-04-28 15:29:52 -04001742int btrfs_balance(struct btrfs_root *dev_root)
1743{
1744 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001745 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1746 struct btrfs_device *device;
1747 u64 old_size;
1748 u64 size_to_free;
1749 struct btrfs_path *path;
1750 struct btrfs_key key;
1751 struct btrfs_chunk *chunk;
1752 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1753 struct btrfs_trans_handle *trans;
1754 struct btrfs_key found_key;
1755
Yan Zheng2b820322008-11-17 21:11:30 -05001756 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1757 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001758
Chris Mason7d9eb122008-07-08 14:19:17 -04001759 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001760 dev_root = dev_root->fs_info->dev_root;
1761
Chris Masonec44a352008-04-28 15:29:52 -04001762 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001763 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001764 old_size = device->total_bytes;
1765 size_to_free = div_factor(old_size, 1);
1766 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001767 if (!device->writeable ||
1768 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001769 continue;
1770
1771 ret = btrfs_shrink_device(device, old_size - size_to_free);
1772 BUG_ON(ret);
1773
1774 trans = btrfs_start_transaction(dev_root, 1);
1775 BUG_ON(!trans);
1776
1777 ret = btrfs_grow_device(trans, device, old_size);
1778 BUG_ON(ret);
1779
1780 btrfs_end_transaction(trans, dev_root);
1781 }
1782
1783 /* step two, relocate all the chunks */
1784 path = btrfs_alloc_path();
1785 BUG_ON(!path);
1786
1787 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1788 key.offset = (u64)-1;
1789 key.type = BTRFS_CHUNK_ITEM_KEY;
1790
Chris Masond3977122009-01-05 21:25:51 -05001791 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001792 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1793 if (ret < 0)
1794 goto error;
1795
1796 /*
1797 * this shouldn't happen, it means the last relocate
1798 * failed
1799 */
1800 if (ret == 0)
1801 break;
1802
1803 ret = btrfs_previous_item(chunk_root, path, 0,
1804 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001805 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001806 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001807
Chris Masonec44a352008-04-28 15:29:52 -04001808 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1809 path->slots[0]);
1810 if (found_key.objectid != key.objectid)
1811 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001812
Chris Masonec44a352008-04-28 15:29:52 -04001813 chunk = btrfs_item_ptr(path->nodes[0],
1814 path->slots[0],
1815 struct btrfs_chunk);
1816 key.offset = found_key.offset;
1817 /* chunk zero is special */
1818 if (key.offset == 0)
1819 break;
1820
Chris Mason7d9eb122008-07-08 14:19:17 -04001821 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001822 ret = btrfs_relocate_chunk(chunk_root,
1823 chunk_root->root_key.objectid,
1824 found_key.objectid,
1825 found_key.offset);
1826 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001827 }
1828 ret = 0;
1829error:
1830 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001831 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001832 return ret;
1833}
1834
Chris Mason8f18cf12008-04-25 16:53:30 -04001835/*
1836 * shrinking a device means finding all of the device extents past
1837 * the new size, and then following the back refs to the chunks.
1838 * The chunk relocation code actually frees the device extent
1839 */
1840int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1841{
1842 struct btrfs_trans_handle *trans;
1843 struct btrfs_root *root = device->dev_root;
1844 struct btrfs_dev_extent *dev_extent = NULL;
1845 struct btrfs_path *path;
1846 u64 length;
1847 u64 chunk_tree;
1848 u64 chunk_objectid;
1849 u64 chunk_offset;
1850 int ret;
1851 int slot;
1852 struct extent_buffer *l;
1853 struct btrfs_key key;
1854 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1855 u64 old_total = btrfs_super_total_bytes(super_copy);
1856 u64 diff = device->total_bytes - new_size;
1857
Yan Zheng2b820322008-11-17 21:11:30 -05001858 if (new_size >= device->total_bytes)
1859 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001860
1861 path = btrfs_alloc_path();
1862 if (!path)
1863 return -ENOMEM;
1864
1865 trans = btrfs_start_transaction(root, 1);
1866 if (!trans) {
1867 ret = -ENOMEM;
1868 goto done;
1869 }
1870
1871 path->reada = 2;
1872
Chris Mason7d9eb122008-07-08 14:19:17 -04001873 lock_chunks(root);
1874
Chris Mason8f18cf12008-04-25 16:53:30 -04001875 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001876 if (device->writeable)
1877 device->fs_devices->total_rw_bytes -= diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04001878 ret = btrfs_update_device(trans, device);
1879 if (ret) {
Chris Mason7d9eb122008-07-08 14:19:17 -04001880 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001881 btrfs_end_transaction(trans, root);
1882 goto done;
1883 }
1884 WARN_ON(diff > old_total);
1885 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001886 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001887 btrfs_end_transaction(trans, root);
1888
1889 key.objectid = device->devid;
1890 key.offset = (u64)-1;
1891 key.type = BTRFS_DEV_EXTENT_KEY;
1892
1893 while (1) {
1894 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1895 if (ret < 0)
1896 goto done;
1897
1898 ret = btrfs_previous_item(root, path, 0, key.type);
1899 if (ret < 0)
1900 goto done;
1901 if (ret) {
1902 ret = 0;
1903 goto done;
1904 }
1905
1906 l = path->nodes[0];
1907 slot = path->slots[0];
1908 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1909
1910 if (key.objectid != device->devid)
1911 goto done;
1912
1913 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1914 length = btrfs_dev_extent_length(l, dev_extent);
1915
1916 if (key.offset + length <= new_size)
1917 goto done;
1918
1919 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1920 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1921 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1922 btrfs_release_path(root, path);
1923
1924 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1925 chunk_offset);
1926 if (ret)
1927 goto done;
1928 }
1929
1930done:
1931 btrfs_free_path(path);
1932 return ret;
1933}
1934
Christoph Hellwigb2950862008-12-02 09:54:17 -05001935static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001936 struct btrfs_root *root,
1937 struct btrfs_key *key,
1938 struct btrfs_chunk *chunk, int item_size)
1939{
1940 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1941 struct btrfs_disk_key disk_key;
1942 u32 array_size;
1943 u8 *ptr;
1944
1945 array_size = btrfs_super_sys_array_size(super_copy);
1946 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1947 return -EFBIG;
1948
1949 ptr = super_copy->sys_chunk_array + array_size;
1950 btrfs_cpu_key_to_disk(&disk_key, key);
1951 memcpy(ptr, &disk_key, sizeof(disk_key));
1952 ptr += sizeof(disk_key);
1953 memcpy(ptr, chunk, item_size);
1954 item_size += sizeof(disk_key);
1955 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1956 return 0;
1957}
1958
Chris Masond3977122009-01-05 21:25:51 -05001959static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04001960 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04001961{
1962 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1963 return calc_size;
1964 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1965 return calc_size * (num_stripes / sub_stripes);
1966 else
1967 return calc_size * num_stripes;
1968}
1969
Yan Zheng2b820322008-11-17 21:11:30 -05001970static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1971 struct btrfs_root *extent_root,
1972 struct map_lookup **map_ret,
1973 u64 *num_bytes, u64 *stripe_size,
1974 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001975{
Chris Mason593060d2008-03-25 16:50:33 -04001976 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001977 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001978 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04001979 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05001980 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001981 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04001982 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05001983 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04001984 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001985 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001986 u64 max_chunk_size = calc_size;
1987 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001988 u64 avail;
1989 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001990 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04001991 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001992 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001993 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001994 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001995 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001996 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001997 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001998
Chris Masonec44a352008-04-28 15:29:52 -04001999 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2000 (type & BTRFS_BLOCK_GROUP_DUP)) {
2001 WARN_ON(1);
2002 type &= ~BTRFS_BLOCK_GROUP_DUP;
2003 }
Yan Zheng2b820322008-11-17 21:11:30 -05002004 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04002005 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04002006
Chris Masona40a90a2008-04-18 11:55:51 -04002007 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002008 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04002009 min_stripes = 2;
2010 }
2011 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04002012 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002013 min_stripes = 2;
2014 }
Chris Mason8790d502008-04-03 16:29:03 -04002015 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002016 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002017 if (num_stripes < 2)
2018 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04002019 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002020 }
Chris Mason321aecc2008-04-16 10:49:51 -04002021 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002022 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04002023 if (num_stripes < 4)
2024 return -ENOSPC;
2025 num_stripes &= ~(u32)1;
2026 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002027 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002028 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002029
2030 if (type & BTRFS_BLOCK_GROUP_DATA) {
2031 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002032 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002033 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2034 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002035 min_stripe_size = 32 * 1024 * 1024;
2036 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2037 calc_size = 8 * 1024 * 1024;
2038 max_chunk_size = calc_size * 2;
2039 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002040 }
2041
Yan Zheng2b820322008-11-17 21:11:30 -05002042 /* we don't want a chunk larger than 10% of writeable space */
2043 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2044 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002045
Chris Masona40a90a2008-04-18 11:55:51 -04002046again:
Yan Zheng2b820322008-11-17 21:11:30 -05002047 if (!map || map->num_stripes != num_stripes) {
2048 kfree(map);
2049 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2050 if (!map)
2051 return -ENOMEM;
2052 map->num_stripes = num_stripes;
2053 }
2054
Chris Mason9b3f68b2008-04-18 10:29:51 -04002055 if (calc_size * num_stripes > max_chunk_size) {
2056 calc_size = max_chunk_size;
2057 do_div(calc_size, num_stripes);
2058 do_div(calc_size, stripe_len);
2059 calc_size *= stripe_len;
2060 }
2061 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002062 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002063
Chris Mason9b3f68b2008-04-18 10:29:51 -04002064 do_div(calc_size, stripe_len);
2065 calc_size *= stripe_len;
2066
Yan Zheng2b820322008-11-17 21:11:30 -05002067 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002068 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002069
2070 if (type & BTRFS_BLOCK_GROUP_DUP)
2071 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002072 else
2073 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002074
Josef Bacik0f9dd462008-09-23 13:14:11 -04002075 /*
2076 * we add 1MB because we never use the first 1MB of the device, unless
2077 * we've looped, then we are likely allocating the maximum amount of
2078 * space left already
2079 */
2080 if (!looped)
2081 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002082
Yan Zheng2b820322008-11-17 21:11:30 -05002083 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002084 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002085 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002086 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002087 if (device->total_bytes > device->bytes_used)
2088 avail = device->total_bytes - device->bytes_used;
2089 else
2090 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002091 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002092
Chris Masondfe25022008-05-13 13:46:40 -04002093 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002094 ret = find_free_dev_extent(trans, device,
2095 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002096 if (ret == 0) {
2097 list_move_tail(&device->dev_alloc_list,
2098 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002099 map->stripes[index].dev = device;
2100 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002101 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002102 if (type & BTRFS_BLOCK_GROUP_DUP) {
2103 map->stripes[index].dev = device;
2104 map->stripes[index].physical =
2105 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002106 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002107 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002108 }
Chris Masondfe25022008-05-13 13:46:40 -04002109 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002110 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002111 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002112 break;
2113 }
Yan Zheng2b820322008-11-17 21:11:30 -05002114 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002115 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002116 if (index >= min_stripes) {
2117 num_stripes = index;
2118 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2119 num_stripes /= sub_stripes;
2120 num_stripes *= sub_stripes;
2121 }
2122 looped = 1;
2123 goto again;
2124 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002125 if (!looped && max_avail > 0) {
2126 looped = 1;
2127 calc_size = max_avail;
2128 goto again;
2129 }
Yan Zheng2b820322008-11-17 21:11:30 -05002130 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002131 return -ENOSPC;
2132 }
Chris Mason593060d2008-03-25 16:50:33 -04002133 map->sector_size = extent_root->sectorsize;
2134 map->stripe_len = stripe_len;
2135 map->io_align = stripe_len;
2136 map->io_width = stripe_len;
2137 map->type = type;
2138 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002139 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002140
Yan Zheng2b820322008-11-17 21:11:30 -05002141 *map_ret = map;
2142 *stripe_size = calc_size;
2143 *num_bytes = chunk_bytes_by_type(type, calc_size,
2144 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002145
2146 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002147 if (!em) {
2148 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002149 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002150 }
Chris Mason0b86a832008-03-24 15:01:56 -04002151 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002152 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002153 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002154 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002155 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002156
Chris Mason0b86a832008-03-24 15:01:56 -04002157 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2158 spin_lock(&em_tree->lock);
2159 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002160 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002161 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002162 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002163
2164 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2165 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2166 start, *num_bytes);
2167 BUG_ON(ret);
2168
2169 index = 0;
2170 while (index < map->num_stripes) {
2171 device = map->stripes[index].dev;
2172 dev_offset = map->stripes[index].physical;
2173
2174 ret = btrfs_alloc_dev_extent(trans, device,
2175 info->chunk_root->root_key.objectid,
2176 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2177 start, dev_offset, calc_size);
2178 BUG_ON(ret);
2179 index++;
2180 }
2181
2182 return 0;
2183}
2184
2185static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2186 struct btrfs_root *extent_root,
2187 struct map_lookup *map, u64 chunk_offset,
2188 u64 chunk_size, u64 stripe_size)
2189{
2190 u64 dev_offset;
2191 struct btrfs_key key;
2192 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2193 struct btrfs_device *device;
2194 struct btrfs_chunk *chunk;
2195 struct btrfs_stripe *stripe;
2196 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2197 int index = 0;
2198 int ret;
2199
2200 chunk = kzalloc(item_size, GFP_NOFS);
2201 if (!chunk)
2202 return -ENOMEM;
2203
2204 index = 0;
2205 while (index < map->num_stripes) {
2206 device = map->stripes[index].dev;
2207 device->bytes_used += stripe_size;
2208 ret = btrfs_update_device(trans, device);
2209 BUG_ON(ret);
2210 index++;
2211 }
2212
2213 index = 0;
2214 stripe = &chunk->stripe;
2215 while (index < map->num_stripes) {
2216 device = map->stripes[index].dev;
2217 dev_offset = map->stripes[index].physical;
2218
2219 btrfs_set_stack_stripe_devid(stripe, device->devid);
2220 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2221 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2222 stripe++;
2223 index++;
2224 }
2225
2226 btrfs_set_stack_chunk_length(chunk, chunk_size);
2227 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2228 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2229 btrfs_set_stack_chunk_type(chunk, map->type);
2230 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2231 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2232 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2233 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2234 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2235
2236 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2237 key.type = BTRFS_CHUNK_ITEM_KEY;
2238 key.offset = chunk_offset;
2239
2240 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2241 BUG_ON(ret);
2242
2243 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2244 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2245 item_size);
2246 BUG_ON(ret);
2247 }
2248 kfree(chunk);
2249 return 0;
2250}
2251
2252/*
2253 * Chunk allocation falls into two parts. The first part does works
2254 * that make the new allocated chunk useable, but not do any operation
2255 * that modifies the chunk tree. The second part does the works that
2256 * require modifying the chunk tree. This division is important for the
2257 * bootstrap process of adding storage to a seed btrfs.
2258 */
2259int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2260 struct btrfs_root *extent_root, u64 type)
2261{
2262 u64 chunk_offset;
2263 u64 chunk_size;
2264 u64 stripe_size;
2265 struct map_lookup *map;
2266 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2267 int ret;
2268
2269 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2270 &chunk_offset);
2271 if (ret)
2272 return ret;
2273
2274 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2275 &stripe_size, chunk_offset, type);
2276 if (ret)
2277 return ret;
2278
2279 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2280 chunk_size, stripe_size);
2281 BUG_ON(ret);
2282 return 0;
2283}
2284
Chris Masond3977122009-01-05 21:25:51 -05002285static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002286 struct btrfs_root *root,
2287 struct btrfs_device *device)
2288{
2289 u64 chunk_offset;
2290 u64 sys_chunk_offset;
2291 u64 chunk_size;
2292 u64 sys_chunk_size;
2293 u64 stripe_size;
2294 u64 sys_stripe_size;
2295 u64 alloc_profile;
2296 struct map_lookup *map;
2297 struct map_lookup *sys_map;
2298 struct btrfs_fs_info *fs_info = root->fs_info;
2299 struct btrfs_root *extent_root = fs_info->extent_root;
2300 int ret;
2301
2302 ret = find_next_chunk(fs_info->chunk_root,
2303 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2304 BUG_ON(ret);
2305
2306 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2307 (fs_info->metadata_alloc_profile &
2308 fs_info->avail_metadata_alloc_bits);
2309 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2310
2311 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2312 &stripe_size, chunk_offset, alloc_profile);
2313 BUG_ON(ret);
2314
2315 sys_chunk_offset = chunk_offset + chunk_size;
2316
2317 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2318 (fs_info->system_alloc_profile &
2319 fs_info->avail_system_alloc_bits);
2320 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2321
2322 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2323 &sys_chunk_size, &sys_stripe_size,
2324 sys_chunk_offset, alloc_profile);
2325 BUG_ON(ret);
2326
2327 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2328 BUG_ON(ret);
2329
2330 /*
2331 * Modifying chunk tree needs allocating new blocks from both
2332 * system block group and metadata block group. So we only can
2333 * do operations require modifying the chunk tree after both
2334 * block groups were created.
2335 */
2336 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2337 chunk_size, stripe_size);
2338 BUG_ON(ret);
2339
2340 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2341 sys_chunk_offset, sys_chunk_size,
2342 sys_stripe_size);
2343 BUG_ON(ret);
2344 return 0;
2345}
2346
2347int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2348{
2349 struct extent_map *em;
2350 struct map_lookup *map;
2351 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2352 int readonly = 0;
2353 int i;
2354
2355 spin_lock(&map_tree->map_tree.lock);
2356 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2357 spin_unlock(&map_tree->map_tree.lock);
2358 if (!em)
2359 return 1;
2360
2361 map = (struct map_lookup *)em->bdev;
2362 for (i = 0; i < map->num_stripes; i++) {
2363 if (!map->stripes[i].dev->writeable) {
2364 readonly = 1;
2365 break;
2366 }
2367 }
2368 free_extent_map(em);
2369 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002370}
2371
2372void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2373{
2374 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2375}
2376
2377void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2378{
2379 struct extent_map *em;
2380
Chris Masond3977122009-01-05 21:25:51 -05002381 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002382 spin_lock(&tree->map_tree.lock);
2383 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2384 if (em)
2385 remove_extent_mapping(&tree->map_tree, em);
2386 spin_unlock(&tree->map_tree.lock);
2387 if (!em)
2388 break;
2389 kfree(em->bdev);
2390 /* once for us */
2391 free_extent_map(em);
2392 /* once for the tree */
2393 free_extent_map(em);
2394 }
2395}
2396
Chris Masonf1885912008-04-09 16:28:12 -04002397int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2398{
2399 struct extent_map *em;
2400 struct map_lookup *map;
2401 struct extent_map_tree *em_tree = &map_tree->map_tree;
2402 int ret;
2403
2404 spin_lock(&em_tree->lock);
2405 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002406 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002407 BUG_ON(!em);
2408
2409 BUG_ON(em->start > logical || em->start + em->len < logical);
2410 map = (struct map_lookup *)em->bdev;
2411 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2412 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002413 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2414 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002415 else
2416 ret = 1;
2417 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002418 return ret;
2419}
2420
Chris Masondfe25022008-05-13 13:46:40 -04002421static int find_live_mirror(struct map_lookup *map, int first, int num,
2422 int optimal)
2423{
2424 int i;
2425 if (map->stripes[optimal].dev->bdev)
2426 return optimal;
2427 for (i = first; i < first + num; i++) {
2428 if (map->stripes[i].dev->bdev)
2429 return i;
2430 }
2431 /* we couldn't find one that doesn't fail. Just return something
2432 * and the io error handling code will clean up eventually
2433 */
2434 return optimal;
2435}
2436
Chris Masonf2d8d742008-04-21 10:03:05 -04002437static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2438 u64 logical, u64 *length,
2439 struct btrfs_multi_bio **multi_ret,
2440 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002441{
2442 struct extent_map *em;
2443 struct map_lookup *map;
2444 struct extent_map_tree *em_tree = &map_tree->map_tree;
2445 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002446 u64 stripe_offset;
2447 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002448 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002449 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002450 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002451 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002452 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002453 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002454 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002455
Chris Masond3977122009-01-05 21:25:51 -05002456 if (multi_ret && !(rw & (1 << BIO_RW)))
Chris Masoncea9e442008-04-09 16:28:12 -04002457 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002458again:
2459 if (multi_ret) {
2460 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2461 GFP_NOFS);
2462 if (!multi)
2463 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002464
2465 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002466 }
Chris Mason0b86a832008-03-24 15:01:56 -04002467
2468 spin_lock(&em_tree->lock);
2469 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002470 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002471
2472 if (!em && unplug_page)
2473 return 0;
2474
Chris Mason3b951512008-04-17 11:29:12 -04002475 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002476 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2477 (unsigned long long)logical,
2478 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002479 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002480 }
Chris Mason0b86a832008-03-24 15:01:56 -04002481
2482 BUG_ON(em->start > logical || em->start + em->len < logical);
2483 map = (struct map_lookup *)em->bdev;
2484 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002485
Chris Masonf1885912008-04-09 16:28:12 -04002486 if (mirror_num > map->num_stripes)
2487 mirror_num = 0;
2488
Chris Masoncea9e442008-04-09 16:28:12 -04002489 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002490 if (rw & (1 << BIO_RW)) {
2491 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2492 BTRFS_BLOCK_GROUP_DUP)) {
2493 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002494 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002495 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2496 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002497 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002498 }
2499 }
2500 if (multi_ret && rw == WRITE &&
2501 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002502 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002503 free_extent_map(em);
2504 kfree(multi);
2505 goto again;
2506 }
Chris Mason593060d2008-03-25 16:50:33 -04002507 stripe_nr = offset;
2508 /*
2509 * stripe_nr counts the total number of stripes we have to stride
2510 * to get to this block
2511 */
2512 do_div(stripe_nr, map->stripe_len);
2513
2514 stripe_offset = stripe_nr * map->stripe_len;
2515 BUG_ON(offset < stripe_offset);
2516
2517 /* stripe_offset is the offset of this block in its stripe*/
2518 stripe_offset = offset - stripe_offset;
2519
Chris Masoncea9e442008-04-09 16:28:12 -04002520 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002521 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002522 BTRFS_BLOCK_GROUP_DUP)) {
2523 /* we limit the length of each bio to what fits in a stripe */
2524 *length = min_t(u64, em->len - offset,
2525 map->stripe_len - stripe_offset);
2526 } else {
2527 *length = em->len - offset;
2528 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002529
2530 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002531 goto out;
2532
Chris Masonf2d8d742008-04-21 10:03:05 -04002533 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002534 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002535 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002536 if (unplug_page || (rw & (1 << BIO_RW)))
2537 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002538 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002539 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002540 else {
2541 stripe_index = find_live_mirror(map, 0,
2542 map->num_stripes,
2543 current->pid % map->num_stripes);
2544 }
Chris Mason2fff7342008-04-29 14:12:09 -04002545
Chris Mason611f0e02008-04-03 16:29:03 -04002546 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002547 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002548 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002549 else if (mirror_num)
2550 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002551
Chris Mason321aecc2008-04-16 10:49:51 -04002552 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2553 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002554
2555 stripe_index = do_div(stripe_nr, factor);
2556 stripe_index *= map->sub_stripes;
2557
Chris Masonf2d8d742008-04-21 10:03:05 -04002558 if (unplug_page || (rw & (1 << BIO_RW)))
2559 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002560 else if (mirror_num)
2561 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002562 else {
2563 stripe_index = find_live_mirror(map, stripe_index,
2564 map->sub_stripes, stripe_index +
2565 current->pid % map->sub_stripes);
2566 }
Chris Mason8790d502008-04-03 16:29:03 -04002567 } else {
2568 /*
2569 * after this do_div call, stripe_nr is the number of stripes
2570 * on this device we have to walk to find the data, and
2571 * stripe_index is the number of our device in the stripe array
2572 */
2573 stripe_index = do_div(stripe_nr, map->num_stripes);
2574 }
Chris Mason593060d2008-03-25 16:50:33 -04002575 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002576
Chris Masonf2d8d742008-04-21 10:03:05 -04002577 for (i = 0; i < num_stripes; i++) {
2578 if (unplug_page) {
2579 struct btrfs_device *device;
2580 struct backing_dev_info *bdi;
2581
2582 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002583 if (device->bdev) {
2584 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002585 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002586 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002587 }
2588 } else {
2589 multi->stripes[i].physical =
2590 map->stripes[stripe_index].physical +
2591 stripe_offset + stripe_nr * map->stripe_len;
2592 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2593 }
Chris Masoncea9e442008-04-09 16:28:12 -04002594 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002595 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002596 if (multi_ret) {
2597 *multi_ret = multi;
2598 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002599 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002600 }
Chris Masoncea9e442008-04-09 16:28:12 -04002601out:
Chris Mason0b86a832008-03-24 15:01:56 -04002602 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002603 return 0;
2604}
2605
Chris Masonf2d8d742008-04-21 10:03:05 -04002606int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2607 u64 logical, u64 *length,
2608 struct btrfs_multi_bio **multi_ret, int mirror_num)
2609{
2610 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2611 mirror_num, NULL);
2612}
2613
Yan Zhenga512bbf2008-12-08 16:46:26 -05002614int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2615 u64 chunk_start, u64 physical, u64 devid,
2616 u64 **logical, int *naddrs, int *stripe_len)
2617{
2618 struct extent_map_tree *em_tree = &map_tree->map_tree;
2619 struct extent_map *em;
2620 struct map_lookup *map;
2621 u64 *buf;
2622 u64 bytenr;
2623 u64 length;
2624 u64 stripe_nr;
2625 int i, j, nr = 0;
2626
2627 spin_lock(&em_tree->lock);
2628 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2629 spin_unlock(&em_tree->lock);
2630
2631 BUG_ON(!em || em->start != chunk_start);
2632 map = (struct map_lookup *)em->bdev;
2633
2634 length = em->len;
2635 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2636 do_div(length, map->num_stripes / map->sub_stripes);
2637 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2638 do_div(length, map->num_stripes);
2639
2640 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2641 BUG_ON(!buf);
2642
2643 for (i = 0; i < map->num_stripes; i++) {
2644 if (devid && map->stripes[i].dev->devid != devid)
2645 continue;
2646 if (map->stripes[i].physical > physical ||
2647 map->stripes[i].physical + length <= physical)
2648 continue;
2649
2650 stripe_nr = physical - map->stripes[i].physical;
2651 do_div(stripe_nr, map->stripe_len);
2652
2653 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2654 stripe_nr = stripe_nr * map->num_stripes + i;
2655 do_div(stripe_nr, map->sub_stripes);
2656 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2657 stripe_nr = stripe_nr * map->num_stripes + i;
2658 }
2659 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002660 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002661 for (j = 0; j < nr; j++) {
2662 if (buf[j] == bytenr)
2663 break;
2664 }
Chris Mason934d3752008-12-08 16:43:10 -05002665 if (j == nr) {
2666 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002667 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002668 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002669 }
2670
2671 for (i = 0; i > nr; i++) {
2672 struct btrfs_multi_bio *multi;
2673 struct btrfs_bio_stripe *stripe;
2674 int ret;
2675
2676 length = 1;
2677 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2678 &length, &multi, 0);
2679 BUG_ON(ret);
2680
2681 stripe = multi->stripes;
2682 for (j = 0; j < multi->num_stripes; j++) {
2683 if (stripe->physical >= physical &&
2684 physical < stripe->physical + length)
2685 break;
2686 }
2687 BUG_ON(j >= multi->num_stripes);
2688 kfree(multi);
2689 }
2690
2691 *logical = buf;
2692 *naddrs = nr;
2693 *stripe_len = map->stripe_len;
2694
2695 free_extent_map(em);
2696 return 0;
2697}
2698
Chris Masonf2d8d742008-04-21 10:03:05 -04002699int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2700 u64 logical, struct page *page)
2701{
2702 u64 length = PAGE_CACHE_SIZE;
2703 return __btrfs_map_block(map_tree, READ, logical, &length,
2704 NULL, 0, page);
2705}
2706
Chris Mason8790d502008-04-03 16:29:03 -04002707static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002708{
Chris Masoncea9e442008-04-09 16:28:12 -04002709 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002710 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002711
Chris Mason8790d502008-04-03 16:29:03 -04002712 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002713 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002714
Chris Mason7d2b4da2008-08-05 10:13:57 -04002715 if (bio == multi->orig_bio)
2716 is_orig_bio = 1;
2717
Chris Masoncea9e442008-04-09 16:28:12 -04002718 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002719 if (!is_orig_bio) {
2720 bio_put(bio);
2721 bio = multi->orig_bio;
2722 }
Chris Mason8790d502008-04-03 16:29:03 -04002723 bio->bi_private = multi->private;
2724 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002725 /* only send an error to the higher layers if it is
2726 * beyond the tolerance of the multi-bio
2727 */
Chris Mason1259ab72008-05-12 13:39:03 -04002728 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002729 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002730 } else if (err) {
2731 /*
2732 * this bio is actually up to date, we didn't
2733 * go over the max number of errors
2734 */
2735 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002736 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002737 }
Chris Mason8790d502008-04-03 16:29:03 -04002738 kfree(multi);
2739
2740 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002741 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002742 bio_put(bio);
2743 }
Chris Mason8790d502008-04-03 16:29:03 -04002744}
2745
Chris Mason8b712842008-06-11 16:50:36 -04002746struct async_sched {
2747 struct bio *bio;
2748 int rw;
2749 struct btrfs_fs_info *info;
2750 struct btrfs_work work;
2751};
2752
2753/*
2754 * see run_scheduled_bios for a description of why bios are collected for
2755 * async submit.
2756 *
2757 * This will add one bio to the pending list for a device and make sure
2758 * the work struct is scheduled.
2759 */
Chris Masond3977122009-01-05 21:25:51 -05002760static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002761 struct btrfs_device *device,
2762 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002763{
2764 int should_queue = 1;
2765
2766 /* don't bother with additional async steps for reads, right now */
2767 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6d2008-07-31 16:29:02 -04002768 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002769 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04002770 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002771 return 0;
2772 }
2773
2774 /*
Chris Mason0986fe92008-08-15 15:34:15 -04002775 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002776 * higher layers. Otherwise, the async bio makes it appear we have
2777 * made progress against dirty pages when we've really just put it
2778 * on a queue for later
2779 */
Chris Mason0986fe92008-08-15 15:34:15 -04002780 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04002781 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002782 bio->bi_next = NULL;
2783 bio->bi_rw |= rw;
2784
2785 spin_lock(&device->io_lock);
2786
2787 if (device->pending_bio_tail)
2788 device->pending_bio_tail->bi_next = bio;
2789
2790 device->pending_bio_tail = bio;
2791 if (!device->pending_bios)
2792 device->pending_bios = bio;
2793 if (device->running_pending)
2794 should_queue = 0;
2795
2796 spin_unlock(&device->io_lock);
2797
2798 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002799 btrfs_queue_worker(&root->fs_info->submit_workers,
2800 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002801 return 0;
2802}
2803
Chris Masonf1885912008-04-09 16:28:12 -04002804int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002805 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002806{
2807 struct btrfs_mapping_tree *map_tree;
2808 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002809 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002810 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002811 u64 length = 0;
2812 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002813 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002814 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002815 int dev_nr = 0;
2816 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002817
Chris Masonf2d8d742008-04-21 10:03:05 -04002818 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002819 map_tree = &root->fs_info->mapping_tree;
2820 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002821
Chris Masonf1885912008-04-09 16:28:12 -04002822 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2823 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002824 BUG_ON(ret);
2825
2826 total_devs = multi->num_stripes;
2827 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05002828 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2829 "len %llu\n", (unsigned long long)logical,
2830 (unsigned long long)length,
2831 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04002832 BUG();
2833 }
2834 multi->end_io = first_bio->bi_end_io;
2835 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002836 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002837 atomic_set(&multi->stripes_pending, multi->num_stripes);
2838
Chris Masond3977122009-01-05 21:25:51 -05002839 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002840 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002841 if (dev_nr < total_devs - 1) {
2842 bio = bio_clone(first_bio, GFP_NOFS);
2843 BUG_ON(!bio);
2844 } else {
2845 bio = first_bio;
2846 }
2847 bio->bi_private = multi;
2848 bio->bi_end_io = end_bio_multi_stripe;
2849 }
Chris Masoncea9e442008-04-09 16:28:12 -04002850 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2851 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002852 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002853 if (dev && dev->bdev) {
2854 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002855 if (async_submit)
2856 schedule_bio(root, dev, rw, bio);
2857 else
2858 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002859 } else {
2860 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2861 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002862 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002863 }
Chris Mason8790d502008-04-03 16:29:03 -04002864 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002865 }
Chris Masoncea9e442008-04-09 16:28:12 -04002866 if (total_devs == 1)
2867 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002868 return 0;
2869}
2870
Chris Masona4437552008-04-18 10:29:38 -04002871struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05002872 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04002873{
Yan Zheng2b820322008-11-17 21:11:30 -05002874 struct btrfs_device *device;
2875 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002876
Yan Zheng2b820322008-11-17 21:11:30 -05002877 cur_devices = root->fs_info->fs_devices;
2878 while (cur_devices) {
2879 if (!fsid ||
2880 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2881 device = __find_device(&cur_devices->devices,
2882 devid, uuid);
2883 if (device)
2884 return device;
2885 }
2886 cur_devices = cur_devices->seed;
2887 }
2888 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002889}
2890
Chris Masondfe25022008-05-13 13:46:40 -04002891static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2892 u64 devid, u8 *dev_uuid)
2893{
2894 struct btrfs_device *device;
2895 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2896
2897 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05002898 if (!device)
2899 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04002900 list_add(&device->dev_list,
2901 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04002902 device->barriers = 1;
2903 device->dev_root = root->fs_info->dev_root;
2904 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002905 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05002906 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04002907 fs_devices->num_devices++;
2908 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05002909 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04002910 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2911 return device;
2912}
2913
Chris Mason0b86a832008-03-24 15:01:56 -04002914static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2915 struct extent_buffer *leaf,
2916 struct btrfs_chunk *chunk)
2917{
2918 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2919 struct map_lookup *map;
2920 struct extent_map *em;
2921 u64 logical;
2922 u64 length;
2923 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002924 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002925 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002926 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002927 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002928
Chris Masone17cade2008-04-15 15:41:47 -04002929 logical = key->offset;
2930 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002931
Chris Mason0b86a832008-03-24 15:01:56 -04002932 spin_lock(&map_tree->map_tree.lock);
2933 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002934 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002935
2936 /* already mapped? */
2937 if (em && em->start <= logical && em->start + em->len > logical) {
2938 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002939 return 0;
2940 } else if (em) {
2941 free_extent_map(em);
2942 }
Chris Mason0b86a832008-03-24 15:01:56 -04002943
Chris Mason0b86a832008-03-24 15:01:56 -04002944 em = alloc_extent_map(GFP_NOFS);
2945 if (!em)
2946 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002947 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2948 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002949 if (!map) {
2950 free_extent_map(em);
2951 return -ENOMEM;
2952 }
2953
2954 em->bdev = (struct block_device *)map;
2955 em->start = logical;
2956 em->len = length;
2957 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002958 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002959
Chris Mason593060d2008-03-25 16:50:33 -04002960 map->num_stripes = num_stripes;
2961 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2962 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2963 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2964 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2965 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002966 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002967 for (i = 0; i < num_stripes; i++) {
2968 map->stripes[i].physical =
2969 btrfs_stripe_offset_nr(leaf, chunk, i);
2970 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002971 read_extent_buffer(leaf, uuid, (unsigned long)
2972 btrfs_stripe_dev_uuid_nr(chunk, i),
2973 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002974 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2975 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04002976 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002977 kfree(map);
2978 free_extent_map(em);
2979 return -EIO;
2980 }
Chris Masondfe25022008-05-13 13:46:40 -04002981 if (!map->stripes[i].dev) {
2982 map->stripes[i].dev =
2983 add_missing_dev(root, devid, uuid);
2984 if (!map->stripes[i].dev) {
2985 kfree(map);
2986 free_extent_map(em);
2987 return -EIO;
2988 }
2989 }
2990 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002991 }
2992
2993 spin_lock(&map_tree->map_tree.lock);
2994 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002995 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002996 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002997 free_extent_map(em);
2998
2999 return 0;
3000}
3001
3002static int fill_device_from_item(struct extent_buffer *leaf,
3003 struct btrfs_dev_item *dev_item,
3004 struct btrfs_device *device)
3005{
3006 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003007
3008 device->devid = btrfs_device_id(leaf, dev_item);
3009 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3010 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3011 device->type = btrfs_device_type(leaf, dev_item);
3012 device->io_align = btrfs_device_io_align(leaf, dev_item);
3013 device->io_width = btrfs_device_io_width(leaf, dev_item);
3014 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003015
3016 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003017 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003018
Chris Mason0b86a832008-03-24 15:01:56 -04003019 return 0;
3020}
3021
Yan Zheng2b820322008-11-17 21:11:30 -05003022static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3023{
3024 struct btrfs_fs_devices *fs_devices;
3025 int ret;
3026
3027 mutex_lock(&uuid_mutex);
3028
3029 fs_devices = root->fs_info->fs_devices->seed;
3030 while (fs_devices) {
3031 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3032 ret = 0;
3033 goto out;
3034 }
3035 fs_devices = fs_devices->seed;
3036 }
3037
3038 fs_devices = find_fsid(fsid);
3039 if (!fs_devices) {
3040 ret = -ENOENT;
3041 goto out;
3042 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003043
3044 fs_devices = clone_fs_devices(fs_devices);
3045 if (IS_ERR(fs_devices)) {
3046 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003047 goto out;
3048 }
3049
Christoph Hellwig97288f22008-12-02 06:36:09 -05003050 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003051 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003052 if (ret)
3053 goto out;
3054
3055 if (!fs_devices->seeding) {
3056 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003057 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003058 ret = -EINVAL;
3059 goto out;
3060 }
3061
3062 fs_devices->seed = root->fs_info->fs_devices->seed;
3063 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003064out:
3065 mutex_unlock(&uuid_mutex);
3066 return ret;
3067}
3068
Chris Mason0d81ba52008-03-24 15:02:07 -04003069static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003070 struct extent_buffer *leaf,
3071 struct btrfs_dev_item *dev_item)
3072{
3073 struct btrfs_device *device;
3074 u64 devid;
3075 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003076 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003077 u8 dev_uuid[BTRFS_UUID_SIZE];
3078
Chris Mason0b86a832008-03-24 15:01:56 -04003079 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003080 read_extent_buffer(leaf, dev_uuid,
3081 (unsigned long)btrfs_device_uuid(dev_item),
3082 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003083 read_extent_buffer(leaf, fs_uuid,
3084 (unsigned long)btrfs_device_fsid(dev_item),
3085 BTRFS_UUID_SIZE);
3086
3087 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3088 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003089 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003090 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003091 }
3092
3093 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3094 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003095 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003096 return -EIO;
3097
3098 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003099 printk(KERN_WARNING "warning devid %llu missing\n",
3100 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003101 device = add_missing_dev(root, devid, dev_uuid);
3102 if (!device)
3103 return -ENOMEM;
3104 }
3105 }
3106
3107 if (device->fs_devices != root->fs_info->fs_devices) {
3108 BUG_ON(device->writeable);
3109 if (device->generation !=
3110 btrfs_device_generation(leaf, dev_item))
3111 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003112 }
Chris Mason0b86a832008-03-24 15:01:56 -04003113
3114 fill_device_from_item(leaf, dev_item, device);
3115 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003116 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003117 if (device->writeable)
3118 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003119 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003120 return ret;
3121}
3122
Chris Mason0d81ba52008-03-24 15:02:07 -04003123int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3124{
3125 struct btrfs_dev_item *dev_item;
3126
3127 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3128 dev_item);
3129 return read_one_dev(root, buf, dev_item);
3130}
3131
Yan Zhenge4404d62008-12-12 10:03:26 -05003132int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003133{
3134 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003135 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003136 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003137 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003138 u8 *ptr;
3139 unsigned long sb_ptr;
3140 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003141 u32 num_stripes;
3142 u32 array_size;
3143 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003144 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003145 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003146
Yan Zhenge4404d62008-12-12 10:03:26 -05003147 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003148 BTRFS_SUPER_INFO_SIZE);
3149 if (!sb)
3150 return -ENOMEM;
3151 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003152 btrfs_set_buffer_lockdep_class(sb, 0);
3153
Chris Masona061fc82008-05-07 11:43:44 -04003154 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003155 array_size = btrfs_super_sys_array_size(super_copy);
3156
Chris Mason0b86a832008-03-24 15:01:56 -04003157 ptr = super_copy->sys_chunk_array;
3158 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3159 cur = 0;
3160
3161 while (cur < array_size) {
3162 disk_key = (struct btrfs_disk_key *)ptr;
3163 btrfs_disk_key_to_cpu(&key, disk_key);
3164
Chris Masona061fc82008-05-07 11:43:44 -04003165 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003166 sb_ptr += len;
3167 cur += len;
3168
Chris Mason0d81ba52008-03-24 15:02:07 -04003169 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003170 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003171 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003172 if (ret)
3173 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003174 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3175 len = btrfs_chunk_item_size(num_stripes);
3176 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003177 ret = -EIO;
3178 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003179 }
3180 ptr += len;
3181 sb_ptr += len;
3182 cur += len;
3183 }
Chris Masona061fc82008-05-07 11:43:44 -04003184 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003185 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003186}
3187
3188int btrfs_read_chunk_tree(struct btrfs_root *root)
3189{
3190 struct btrfs_path *path;
3191 struct extent_buffer *leaf;
3192 struct btrfs_key key;
3193 struct btrfs_key found_key;
3194 int ret;
3195 int slot;
3196
3197 root = root->fs_info->chunk_root;
3198
3199 path = btrfs_alloc_path();
3200 if (!path)
3201 return -ENOMEM;
3202
3203 /* first we search for all of the device items, and then we
3204 * read in all of the chunk items. This way we can create chunk
3205 * mappings that reference all of the devices that are afound
3206 */
3207 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3208 key.offset = 0;
3209 key.type = 0;
3210again:
3211 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masond3977122009-01-05 21:25:51 -05003212 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003213 leaf = path->nodes[0];
3214 slot = path->slots[0];
3215 if (slot >= btrfs_header_nritems(leaf)) {
3216 ret = btrfs_next_leaf(root, path);
3217 if (ret == 0)
3218 continue;
3219 if (ret < 0)
3220 goto error;
3221 break;
3222 }
3223 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3224 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3225 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3226 break;
3227 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3228 struct btrfs_dev_item *dev_item;
3229 dev_item = btrfs_item_ptr(leaf, slot,
3230 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003231 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003232 if (ret)
3233 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003234 }
3235 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3236 struct btrfs_chunk *chunk;
3237 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3238 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003239 if (ret)
3240 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003241 }
3242 path->slots[0]++;
3243 }
3244 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3245 key.objectid = 0;
3246 btrfs_release_path(root, path);
3247 goto again;
3248 }
Chris Mason0b86a832008-03-24 15:01:56 -04003249 ret = 0;
3250error:
Yan Zheng2b820322008-11-17 21:11:30 -05003251 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003252 return ret;
3253}