blob: cc01abff03d963af529027941bb2bb4f16f53f3c [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
151 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
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 Mason492bb6de2008-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 Mason492bb6de2008-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 Mason492bb6de2008-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 Mason8b712842008-06-11 16:50:36 -0400261done:
262 return 0;
263}
264
Christoph Hellwigb2950862008-12-02 09:54:17 -0500265static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400266{
267 struct btrfs_device *device;
268
269 device = container_of(work, struct btrfs_device, work);
270 run_scheduled_bios(device);
271}
272
Chris Masona1b32a52008-09-05 16:09:51 -0400273static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400274 struct btrfs_super_block *disk_super,
275 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
276{
277 struct btrfs_device *device;
278 struct btrfs_fs_devices *fs_devices;
279 u64 found_transid = btrfs_super_generation(disk_super);
280
281 fs_devices = find_fsid(disk_super->fsid);
282 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400283 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400284 if (!fs_devices)
285 return -ENOMEM;
286 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400287 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400288 list_add(&fs_devices->list, &fs_uuids);
289 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
290 fs_devices->latest_devid = devid;
291 fs_devices->latest_trans = found_transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400292 device = NULL;
293 } else {
Chris Masona4437552008-04-18 10:29:38 -0400294 device = __find_device(&fs_devices->devices, devid,
295 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400296 }
297 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500298 if (fs_devices->opened)
299 return -EBUSY;
300
Chris Mason8a4b83c2008-03-24 15:02:07 -0400301 device = kzalloc(sizeof(*device), GFP_NOFS);
302 if (!device) {
303 /* we can safely leave the fs_devices entry around */
304 return -ENOMEM;
305 }
306 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400307 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400308 memcpy(device->uuid, disk_super->dev_item.uuid,
309 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400310 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400311 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400312 device->name = kstrdup(path, GFP_NOFS);
313 if (!device->name) {
314 kfree(device);
315 return -ENOMEM;
316 }
Yan Zheng2b820322008-11-17 21:11:30 -0500317 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400318 list_add(&device->dev_list, &fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500319 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400320 fs_devices->num_devices++;
321 }
322
323 if (found_transid > fs_devices->latest_trans) {
324 fs_devices->latest_devid = devid;
325 fs_devices->latest_trans = found_transid;
326 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400327 *fs_devices_ret = fs_devices;
328 return 0;
329}
330
Yan Zhenge4404d62008-12-12 10:03:26 -0500331static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
332{
333 struct btrfs_fs_devices *fs_devices;
334 struct btrfs_device *device;
335 struct btrfs_device *orig_dev;
336
337 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
338 if (!fs_devices)
339 return ERR_PTR(-ENOMEM);
340
341 INIT_LIST_HEAD(&fs_devices->devices);
342 INIT_LIST_HEAD(&fs_devices->alloc_list);
343 INIT_LIST_HEAD(&fs_devices->list);
344 fs_devices->latest_devid = orig->latest_devid;
345 fs_devices->latest_trans = orig->latest_trans;
346 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
347
348 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
349 device = kzalloc(sizeof(*device), GFP_NOFS);
350 if (!device)
351 goto error;
352
353 device->name = kstrdup(orig_dev->name, GFP_NOFS);
354 if (!device->name)
355 goto error;
356
357 device->devid = orig_dev->devid;
358 device->work.func = pending_bios_fn;
359 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
360 device->barriers = 1;
361 spin_lock_init(&device->io_lock);
362 INIT_LIST_HEAD(&device->dev_list);
363 INIT_LIST_HEAD(&device->dev_alloc_list);
364
365 list_add(&device->dev_list, &fs_devices->devices);
366 device->fs_devices = fs_devices;
367 fs_devices->num_devices++;
368 }
369 return fs_devices;
370error:
371 free_fs_devices(fs_devices);
372 return ERR_PTR(-ENOMEM);
373}
374
Chris Masondfe25022008-05-13 13:46:40 -0400375int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
376{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500377 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400378
379 mutex_lock(&uuid_mutex);
380again:
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500381 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500382 if (device->in_fs_metadata)
383 continue;
384
385 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500386 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500387 device->bdev = NULL;
388 fs_devices->open_devices--;
389 }
390 if (device->writeable) {
391 list_del_init(&device->dev_alloc_list);
392 device->writeable = 0;
393 fs_devices->rw_devices--;
394 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500395 list_del_init(&device->dev_list);
396 fs_devices->num_devices--;
397 kfree(device->name);
398 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400399 }
Yan Zheng2b820322008-11-17 21:11:30 -0500400
401 if (fs_devices->seed) {
402 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500403 goto again;
404 }
405
Chris Masondfe25022008-05-13 13:46:40 -0400406 mutex_unlock(&uuid_mutex);
407 return 0;
408}
Chris Masona0af4692008-05-13 16:03:06 -0400409
Yan Zheng2b820322008-11-17 21:11:30 -0500410static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400412 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500413
Yan Zheng2b820322008-11-17 21:11:30 -0500414 if (--fs_devices->opened > 0)
415 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400416
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500417 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400418 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500419 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400420 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400421 }
Yan Zheng2b820322008-11-17 21:11:30 -0500422 if (device->writeable) {
423 list_del_init(&device->dev_alloc_list);
424 fs_devices->rw_devices--;
425 }
426
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500428 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400429 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400430 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500431 WARN_ON(fs_devices->open_devices);
432 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500433 fs_devices->opened = 0;
434 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500435
Chris Mason8a4b83c2008-03-24 15:02:07 -0400436 return 0;
437}
438
Yan Zheng2b820322008-11-17 21:11:30 -0500439int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
440{
Yan Zhenge4404d62008-12-12 10:03:26 -0500441 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500442 int ret;
443
444 mutex_lock(&uuid_mutex);
445 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500446 if (!fs_devices->opened) {
447 seed_devices = fs_devices->seed;
448 fs_devices->seed = NULL;
449 }
Yan Zheng2b820322008-11-17 21:11:30 -0500450 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500451
452 while (seed_devices) {
453 fs_devices = seed_devices;
454 seed_devices = fs_devices->seed;
455 __btrfs_close_devices(fs_devices);
456 free_fs_devices(fs_devices);
457 }
Yan Zheng2b820322008-11-17 21:11:30 -0500458 return ret;
459}
460
Yan Zhenge4404d62008-12-12 10:03:26 -0500461static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
462 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400463{
464 struct block_device *bdev;
465 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400466 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400467 struct block_device *latest_bdev = NULL;
468 struct buffer_head *bh;
469 struct btrfs_super_block *disk_super;
470 u64 latest_devid = 0;
471 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400472 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500473 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400474 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400475
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500476 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400477 if (device->bdev)
478 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400479 if (!device->name)
480 continue;
481
Chris Mason15916de2008-11-19 21:17:22 -0500482 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400483 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500484 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400485 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400486 }
Chris Masona061fc82008-05-07 11:43:44 -0400487 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400488
Yan Zhenga512bbf2008-12-08 16:46:26 -0500489 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400490 if (!bh)
491 goto error_close;
492
493 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masona0af4692008-05-13 16:03:06 -0400494 devid = le64_to_cpu(disk_super->dev_item.devid);
495 if (devid != device->devid)
496 goto error_brelse;
497
Yan Zheng2b820322008-11-17 21:11:30 -0500498 if (memcmp(device->uuid, disk_super->dev_item.uuid,
499 BTRFS_UUID_SIZE))
500 goto error_brelse;
501
502 device->generation = btrfs_super_generation(disk_super);
503 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400504 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500505 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400506 latest_bdev = bdev;
507 }
508
Yan Zheng2b820322008-11-17 21:11:30 -0500509 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
510 device->writeable = 0;
511 } else {
512 device->writeable = !bdev_read_only(bdev);
513 seeding = 0;
514 }
515
Chris Mason8a4b83c2008-03-24 15:02:07 -0400516 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400517 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500518 device->mode = flags;
519
Chris Masona0af4692008-05-13 16:03:06 -0400520 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500521 if (device->writeable) {
522 fs_devices->rw_devices++;
523 list_add(&device->dev_alloc_list,
524 &fs_devices->alloc_list);
525 }
Chris Masona0af4692008-05-13 16:03:06 -0400526 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400527
Chris Masona0af4692008-05-13 16:03:06 -0400528error_brelse:
529 brelse(bh);
530error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500531 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400532error:
533 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 }
Chris Masona0af4692008-05-13 16:03:06 -0400535 if (fs_devices->open_devices == 0) {
536 ret = -EIO;
537 goto out;
538 }
Yan Zheng2b820322008-11-17 21:11:30 -0500539 fs_devices->seeding = seeding;
540 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400541 fs_devices->latest_bdev = latest_bdev;
542 fs_devices->latest_devid = latest_devid;
543 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500544 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400545out:
Yan Zheng2b820322008-11-17 21:11:30 -0500546 return ret;
547}
548
549int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500550 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500551{
552 int ret;
553
554 mutex_lock(&uuid_mutex);
555 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500556 fs_devices->opened++;
557 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500558 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500559 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500560 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400561 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400562 return ret;
563}
564
Christoph Hellwig97288f22008-12-02 06:36:09 -0500565int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400566 struct btrfs_fs_devices **fs_devices_ret)
567{
568 struct btrfs_super_block *disk_super;
569 struct block_device *bdev;
570 struct buffer_head *bh;
571 int ret;
572 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400573 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574
575 mutex_lock(&uuid_mutex);
576
Chris Mason15916de2008-11-19 21:17:22 -0500577 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578
579 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400580 ret = PTR_ERR(bdev);
581 goto error;
582 }
583
584 ret = set_blocksize(bdev, 4096);
585 if (ret)
586 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500587 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588 if (!bh) {
589 ret = -EIO;
590 goto error_close;
591 }
592 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400593 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400594 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400595 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500596 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400597 else {
598 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500599 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400600 *(unsigned long long *)disk_super->fsid,
601 *(unsigned long long *)(disk_super->fsid + 8));
602 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500603 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500604 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400605 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
606
Chris Mason8a4b83c2008-03-24 15:02:07 -0400607 brelse(bh);
608error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500609 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400610error:
611 mutex_unlock(&uuid_mutex);
612 return ret;
613}
Chris Mason0b86a832008-03-24 15:01:56 -0400614
615/*
616 * this uses a pretty simple search, the expectation is that it is
617 * called very infrequently and that a given device has a small number
618 * of extents
619 */
Chris Masona1b32a52008-09-05 16:09:51 -0400620static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
621 struct btrfs_device *device,
Chris Masona1b32a52008-09-05 16:09:51 -0400622 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400623{
624 struct btrfs_key key;
625 struct btrfs_root *root = device->dev_root;
626 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500627 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400628 u64 hole_size = 0;
629 u64 last_byte = 0;
630 u64 search_start = 0;
631 u64 search_end = device->total_bytes;
632 int ret;
633 int slot = 0;
634 int start_found;
635 struct extent_buffer *l;
636
Yan Zheng2b820322008-11-17 21:11:30 -0500637 path = btrfs_alloc_path();
638 if (!path)
639 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400640 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500641 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400642
643 /* FIXME use last free of some kind */
644
Chris Mason8a4b83c2008-03-24 15:02:07 -0400645 /* we don't want to overwrite the superblock on the drive,
646 * so we make sure to start at an offset of at least 1MB
647 */
648 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400649
650 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
651 search_start = max(root->fs_info->alloc_start, search_start);
652
Chris Mason0b86a832008-03-24 15:01:56 -0400653 key.objectid = device->devid;
654 key.offset = search_start;
655 key.type = BTRFS_DEV_EXTENT_KEY;
656 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
657 if (ret < 0)
658 goto error;
659 ret = btrfs_previous_item(root, path, 0, key.type);
660 if (ret < 0)
661 goto error;
662 l = path->nodes[0];
663 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
664 while (1) {
665 l = path->nodes[0];
666 slot = path->slots[0];
667 if (slot >= btrfs_header_nritems(l)) {
668 ret = btrfs_next_leaf(root, path);
669 if (ret == 0)
670 continue;
671 if (ret < 0)
672 goto error;
673no_more_items:
674 if (!start_found) {
675 if (search_start >= search_end) {
676 ret = -ENOSPC;
677 goto error;
678 }
679 *start = search_start;
680 start_found = 1;
681 goto check_pending;
682 }
683 *start = last_byte > search_start ?
684 last_byte : search_start;
685 if (search_end <= *start) {
686 ret = -ENOSPC;
687 goto error;
688 }
689 goto check_pending;
690 }
691 btrfs_item_key_to_cpu(l, &key, slot);
692
693 if (key.objectid < device->devid)
694 goto next;
695
696 if (key.objectid > device->devid)
697 goto no_more_items;
698
699 if (key.offset >= search_start && key.offset > last_byte &&
700 start_found) {
701 if (last_byte < search_start)
702 last_byte = search_start;
703 hole_size = key.offset - last_byte;
704 if (key.offset > last_byte &&
705 hole_size >= num_bytes) {
706 *start = last_byte;
707 goto check_pending;
708 }
709 }
Chris Masond3977122009-01-05 21:25:51 -0500710 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400711 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400712
713 start_found = 1;
714 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
715 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
716next:
717 path->slots[0]++;
718 cond_resched();
719 }
720check_pending:
721 /* we have to make sure we didn't find an extent that has already
722 * been allocated by the map tree or the original allocation
723 */
Chris Mason0b86a832008-03-24 15:01:56 -0400724 BUG_ON(*start < search_start);
725
Chris Mason6324fbf2008-03-24 15:01:59 -0400726 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400727 ret = -ENOSPC;
728 goto error;
729 }
730 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500731 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400732
733error:
Yan Zheng2b820322008-11-17 21:11:30 -0500734 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400735 return ret;
736}
737
Christoph Hellwigb2950862008-12-02 09:54:17 -0500738static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400739 struct btrfs_device *device,
740 u64 start)
741{
742 int ret;
743 struct btrfs_path *path;
744 struct btrfs_root *root = device->dev_root;
745 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400746 struct btrfs_key found_key;
747 struct extent_buffer *leaf = NULL;
748 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400749
750 path = btrfs_alloc_path();
751 if (!path)
752 return -ENOMEM;
753
754 key.objectid = device->devid;
755 key.offset = start;
756 key.type = BTRFS_DEV_EXTENT_KEY;
757
758 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400759 if (ret > 0) {
760 ret = btrfs_previous_item(root, path, key.objectid,
761 BTRFS_DEV_EXTENT_KEY);
762 BUG_ON(ret);
763 leaf = path->nodes[0];
764 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
765 extent = btrfs_item_ptr(leaf, path->slots[0],
766 struct btrfs_dev_extent);
767 BUG_ON(found_key.offset > start || found_key.offset +
768 btrfs_dev_extent_length(leaf, extent) < start);
769 ret = 0;
770 } else if (ret == 0) {
771 leaf = path->nodes[0];
772 extent = btrfs_item_ptr(leaf, path->slots[0],
773 struct btrfs_dev_extent);
774 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400775 BUG_ON(ret);
776
Chris Masondfe25022008-05-13 13:46:40 -0400777 if (device->bytes_used > 0)
778 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400779 ret = btrfs_del_item(trans, root, path);
780 BUG_ON(ret);
781
782 btrfs_free_path(path);
783 return ret;
784}
785
Yan Zheng2b820322008-11-17 21:11:30 -0500786int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400787 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400788 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500789 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400790{
791 int ret;
792 struct btrfs_path *path;
793 struct btrfs_root *root = device->dev_root;
794 struct btrfs_dev_extent *extent;
795 struct extent_buffer *leaf;
796 struct btrfs_key key;
797
Chris Masondfe25022008-05-13 13:46:40 -0400798 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400799 path = btrfs_alloc_path();
800 if (!path)
801 return -ENOMEM;
802
Chris Mason0b86a832008-03-24 15:01:56 -0400803 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500804 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400805 key.type = BTRFS_DEV_EXTENT_KEY;
806 ret = btrfs_insert_empty_item(trans, root, path, &key,
807 sizeof(*extent));
808 BUG_ON(ret);
809
810 leaf = path->nodes[0];
811 extent = btrfs_item_ptr(leaf, path->slots[0],
812 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400813 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
814 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
815 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
816
817 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
818 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
819 BTRFS_UUID_SIZE);
820
Chris Mason0b86a832008-03-24 15:01:56 -0400821 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
822 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400823 btrfs_free_path(path);
824 return ret;
825}
826
Chris Masona1b32a52008-09-05 16:09:51 -0400827static noinline int find_next_chunk(struct btrfs_root *root,
828 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400829{
830 struct btrfs_path *path;
831 int ret;
832 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400833 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400834 struct btrfs_key found_key;
835
836 path = btrfs_alloc_path();
837 BUG_ON(!path);
838
Chris Masone17cade2008-04-15 15:41:47 -0400839 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400840 key.offset = (u64)-1;
841 key.type = BTRFS_CHUNK_ITEM_KEY;
842
843 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
844 if (ret < 0)
845 goto error;
846
847 BUG_ON(ret == 0);
848
849 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
850 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400851 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400852 } else {
853 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
854 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400855 if (found_key.objectid != objectid)
856 *offset = 0;
857 else {
858 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
859 struct btrfs_chunk);
860 *offset = found_key.offset +
861 btrfs_chunk_length(path->nodes[0], chunk);
862 }
Chris Mason0b86a832008-03-24 15:01:56 -0400863 }
864 ret = 0;
865error:
866 btrfs_free_path(path);
867 return ret;
868}
869
Yan Zheng2b820322008-11-17 21:11:30 -0500870static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400871{
872 int ret;
873 struct btrfs_key key;
874 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500875 struct btrfs_path *path;
876
877 root = root->fs_info->chunk_root;
878
879 path = btrfs_alloc_path();
880 if (!path)
881 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400882
883 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
884 key.type = BTRFS_DEV_ITEM_KEY;
885 key.offset = (u64)-1;
886
887 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
888 if (ret < 0)
889 goto error;
890
891 BUG_ON(ret == 0);
892
893 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
894 BTRFS_DEV_ITEM_KEY);
895 if (ret) {
896 *objectid = 1;
897 } else {
898 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
899 path->slots[0]);
900 *objectid = found_key.offset + 1;
901 }
902 ret = 0;
903error:
Yan Zheng2b820322008-11-17 21:11:30 -0500904 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400905 return ret;
906}
907
908/*
909 * the device information is stored in the chunk root
910 * the btrfs_device struct should be fully filled in
911 */
912int btrfs_add_device(struct btrfs_trans_handle *trans,
913 struct btrfs_root *root,
914 struct btrfs_device *device)
915{
916 int ret;
917 struct btrfs_path *path;
918 struct btrfs_dev_item *dev_item;
919 struct extent_buffer *leaf;
920 struct btrfs_key key;
921 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -0400922
923 root = root->fs_info->chunk_root;
924
925 path = btrfs_alloc_path();
926 if (!path)
927 return -ENOMEM;
928
Chris Mason0b86a832008-03-24 15:01:56 -0400929 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
930 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -0500931 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400932
933 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400934 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400935 if (ret)
936 goto out;
937
938 leaf = path->nodes[0];
939 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
940
941 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -0500942 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400943 btrfs_set_device_type(leaf, dev_item, device->type);
944 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
945 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
946 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400947 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
948 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400949 btrfs_set_device_group(leaf, dev_item, 0);
950 btrfs_set_device_seek_speed(leaf, dev_item, 0);
951 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -0500952 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400953
Chris Mason0b86a832008-03-24 15:01:56 -0400954 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400955 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500956 ptr = (unsigned long)btrfs_device_fsid(dev_item);
957 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400958 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400959
Yan Zheng2b820322008-11-17 21:11:30 -0500960 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400961out:
962 btrfs_free_path(path);
963 return ret;
964}
Chris Mason8f18cf12008-04-25 16:53:30 -0400965
Chris Masona061fc82008-05-07 11:43:44 -0400966static int btrfs_rm_dev_item(struct btrfs_root *root,
967 struct btrfs_device *device)
968{
969 int ret;
970 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -0400971 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400972 struct btrfs_trans_handle *trans;
973
974 root = root->fs_info->chunk_root;
975
976 path = btrfs_alloc_path();
977 if (!path)
978 return -ENOMEM;
979
980 trans = btrfs_start_transaction(root, 1);
981 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
982 key.type = BTRFS_DEV_ITEM_KEY;
983 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400984 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400985
986 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
987 if (ret < 0)
988 goto out;
989
990 if (ret > 0) {
991 ret = -ENOENT;
992 goto out;
993 }
994
995 ret = btrfs_del_item(trans, root, path);
996 if (ret)
997 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400998out:
999 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001000 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001001 btrfs_commit_transaction(trans, root);
1002 return ret;
1003}
1004
1005int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1006{
1007 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001008 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001009 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001010 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001011 struct btrfs_super_block *disk_super;
1012 u64 all_avail;
1013 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001014 u64 num_devices;
1015 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001016 int ret = 0;
1017
Chris Masona061fc82008-05-07 11:43:44 -04001018 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001019 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001020
1021 all_avail = root->fs_info->avail_data_alloc_bits |
1022 root->fs_info->avail_system_alloc_bits |
1023 root->fs_info->avail_metadata_alloc_bits;
1024
1025 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001026 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001027 printk(KERN_ERR "btrfs: unable to go below four devices "
1028 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001029 ret = -EINVAL;
1030 goto out;
1031 }
1032
1033 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001034 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001035 printk(KERN_ERR "btrfs: unable to go below two "
1036 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001037 ret = -EINVAL;
1038 goto out;
1039 }
1040
Chris Masondfe25022008-05-13 13:46:40 -04001041 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001042 struct list_head *devices;
1043 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001044
Chris Masondfe25022008-05-13 13:46:40 -04001045 device = NULL;
1046 devices = &root->fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001047 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001048 if (tmp->in_fs_metadata && !tmp->bdev) {
1049 device = tmp;
1050 break;
1051 }
1052 }
1053 bdev = NULL;
1054 bh = NULL;
1055 disk_super = NULL;
1056 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001057 printk(KERN_ERR "btrfs: no missing devices found to "
1058 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001059 goto out;
1060 }
Chris Masondfe25022008-05-13 13:46:40 -04001061 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001062 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001063 root->fs_info->bdev_holder);
1064 if (IS_ERR(bdev)) {
1065 ret = PTR_ERR(bdev);
1066 goto out;
1067 }
1068
Yan Zheng2b820322008-11-17 21:11:30 -05001069 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001070 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001071 if (!bh) {
1072 ret = -EIO;
1073 goto error_close;
1074 }
1075 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masondfe25022008-05-13 13:46:40 -04001076 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001077 dev_uuid = disk_super->dev_item.uuid;
1078 device = btrfs_find_device(root, devid, dev_uuid,
1079 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001080 if (!device) {
1081 ret = -ENOENT;
1082 goto error_brelse;
1083 }
Chris Masondfe25022008-05-13 13:46:40 -04001084 }
Yan Zheng2b820322008-11-17 21:11:30 -05001085
1086 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001087 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1088 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001089 ret = -EINVAL;
1090 goto error_brelse;
1091 }
1092
1093 if (device->writeable) {
1094 list_del_init(&device->dev_alloc_list);
1095 root->fs_info->fs_devices->rw_devices--;
1096 }
Chris Masona061fc82008-05-07 11:43:44 -04001097
1098 ret = btrfs_shrink_device(device, 0);
1099 if (ret)
1100 goto error_brelse;
1101
Chris Masona061fc82008-05-07 11:43:44 -04001102 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1103 if (ret)
1104 goto error_brelse;
1105
Yan Zheng2b820322008-11-17 21:11:30 -05001106 device->in_fs_metadata = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001107 list_del_init(&device->dev_list);
1108 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001109
1110 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1111 struct btrfs_device, dev_list);
1112 if (device->bdev == root->fs_info->sb->s_bdev)
1113 root->fs_info->sb->s_bdev = next_device->bdev;
1114 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1115 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1116
Yan Zhenge4404d62008-12-12 10:03:26 -05001117 if (device->bdev) {
1118 close_bdev_exclusive(device->bdev, device->mode);
1119 device->bdev = NULL;
1120 device->fs_devices->open_devices--;
1121 }
1122
Yan Zheng2b820322008-11-17 21:11:30 -05001123 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1124 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1125
Yan Zhenge4404d62008-12-12 10:03:26 -05001126 if (device->fs_devices->open_devices == 0) {
1127 struct btrfs_fs_devices *fs_devices;
1128 fs_devices = root->fs_info->fs_devices;
1129 while (fs_devices) {
1130 if (fs_devices->seed == device->fs_devices)
1131 break;
1132 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001133 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001134 fs_devices->seed = device->fs_devices->seed;
1135 device->fs_devices->seed = NULL;
1136 __btrfs_close_devices(device->fs_devices);
1137 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001138 }
1139
1140 /*
1141 * at this point, the device is zero sized. We want to
1142 * remove it from the devices list and zero out the old super
1143 */
1144 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001145 /* make sure this device isn't detected as part of
1146 * the FS anymore
1147 */
1148 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1149 set_buffer_dirty(bh);
1150 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001151 }
Chris Masona061fc82008-05-07 11:43:44 -04001152
Chris Masona061fc82008-05-07 11:43:44 -04001153 kfree(device->name);
1154 kfree(device);
1155 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001156
1157error_brelse:
1158 brelse(bh);
1159error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001160 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001161 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001162out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001163 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001164 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001165 return ret;
1166}
1167
Yan Zheng2b820322008-11-17 21:11:30 -05001168/*
1169 * does all the dirty work required for changing file system's UUID.
1170 */
1171static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root)
1173{
1174 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1175 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001176 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001177 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1178 struct btrfs_device *device;
1179 u64 super_flags;
1180
1181 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001182 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001183 return -EINVAL;
1184
Yan Zhenge4404d62008-12-12 10:03:26 -05001185 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1186 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001187 return -ENOMEM;
1188
Yan Zhenge4404d62008-12-12 10:03:26 -05001189 old_devices = clone_fs_devices(fs_devices);
1190 if (IS_ERR(old_devices)) {
1191 kfree(seed_devices);
1192 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001193 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001194
Yan Zheng2b820322008-11-17 21:11:30 -05001195 list_add(&old_devices->list, &fs_uuids);
1196
Yan Zhenge4404d62008-12-12 10:03:26 -05001197 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1198 seed_devices->opened = 1;
1199 INIT_LIST_HEAD(&seed_devices->devices);
1200 INIT_LIST_HEAD(&seed_devices->alloc_list);
1201 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1202 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1203 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1204 device->fs_devices = seed_devices;
1205 }
1206
Yan Zheng2b820322008-11-17 21:11:30 -05001207 fs_devices->seeding = 0;
1208 fs_devices->num_devices = 0;
1209 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001210 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001211
1212 generate_random_uuid(fs_devices->fsid);
1213 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1214 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1215 super_flags = btrfs_super_flags(disk_super) &
1216 ~BTRFS_SUPER_FLAG_SEEDING;
1217 btrfs_set_super_flags(disk_super, super_flags);
1218
1219 return 0;
1220}
1221
1222/*
1223 * strore the expected generation for seed devices in device items.
1224 */
1225static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1226 struct btrfs_root *root)
1227{
1228 struct btrfs_path *path;
1229 struct extent_buffer *leaf;
1230 struct btrfs_dev_item *dev_item;
1231 struct btrfs_device *device;
1232 struct btrfs_key key;
1233 u8 fs_uuid[BTRFS_UUID_SIZE];
1234 u8 dev_uuid[BTRFS_UUID_SIZE];
1235 u64 devid;
1236 int ret;
1237
1238 path = btrfs_alloc_path();
1239 if (!path)
1240 return -ENOMEM;
1241
1242 root = root->fs_info->chunk_root;
1243 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1244 key.offset = 0;
1245 key.type = BTRFS_DEV_ITEM_KEY;
1246
1247 while (1) {
1248 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1249 if (ret < 0)
1250 goto error;
1251
1252 leaf = path->nodes[0];
1253next_slot:
1254 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1255 ret = btrfs_next_leaf(root, path);
1256 if (ret > 0)
1257 break;
1258 if (ret < 0)
1259 goto error;
1260 leaf = path->nodes[0];
1261 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1262 btrfs_release_path(root, path);
1263 continue;
1264 }
1265
1266 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1267 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1268 key.type != BTRFS_DEV_ITEM_KEY)
1269 break;
1270
1271 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1272 struct btrfs_dev_item);
1273 devid = btrfs_device_id(leaf, dev_item);
1274 read_extent_buffer(leaf, dev_uuid,
1275 (unsigned long)btrfs_device_uuid(dev_item),
1276 BTRFS_UUID_SIZE);
1277 read_extent_buffer(leaf, fs_uuid,
1278 (unsigned long)btrfs_device_fsid(dev_item),
1279 BTRFS_UUID_SIZE);
1280 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1281 BUG_ON(!device);
1282
1283 if (device->fs_devices->seeding) {
1284 btrfs_set_device_generation(leaf, dev_item,
1285 device->generation);
1286 btrfs_mark_buffer_dirty(leaf);
1287 }
1288
1289 path->slots[0]++;
1290 goto next_slot;
1291 }
1292 ret = 0;
1293error:
1294 btrfs_free_path(path);
1295 return ret;
1296}
1297
Chris Mason788f20e2008-04-28 15:29:42 -04001298int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1299{
1300 struct btrfs_trans_handle *trans;
1301 struct btrfs_device *device;
1302 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001303 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001304 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001305 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001306 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001307 int ret = 0;
1308
Yan Zheng2b820322008-11-17 21:11:30 -05001309 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1310 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001311
Chris Mason15916de2008-11-19 21:17:22 -05001312 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Chris Masond3977122009-01-05 21:25:51 -05001313 if (!bdev)
Chris Mason788f20e2008-04-28 15:29:42 -04001314 return -EIO;
Chris Masona2135012008-06-25 16:01:30 -04001315
Yan Zheng2b820322008-11-17 21:11:30 -05001316 if (root->fs_info->fs_devices->seeding) {
1317 seeding_dev = 1;
1318 down_write(&sb->s_umount);
1319 mutex_lock(&uuid_mutex);
1320 }
1321
Chris Mason8c8bee12008-09-29 11:19:10 -04001322 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001323 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001324
Chris Mason788f20e2008-04-28 15:29:42 -04001325 devices = &root->fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001326 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001327 if (device->bdev == bdev) {
1328 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001329 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001330 }
1331 }
1332
1333 device = kzalloc(sizeof(*device), GFP_NOFS);
1334 if (!device) {
1335 /* we can safely leave the fs_devices entry around */
1336 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001337 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001338 }
1339
Chris Mason788f20e2008-04-28 15:29:42 -04001340 device->name = kstrdup(device_path, GFP_NOFS);
1341 if (!device->name) {
1342 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001343 ret = -ENOMEM;
1344 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001345 }
Yan Zheng2b820322008-11-17 21:11:30 -05001346
1347 ret = find_next_devid(root, &device->devid);
1348 if (ret) {
1349 kfree(device);
1350 goto error;
1351 }
1352
1353 trans = btrfs_start_transaction(root, 1);
1354 lock_chunks(root);
1355
1356 device->barriers = 1;
1357 device->writeable = 1;
1358 device->work.func = pending_bios_fn;
1359 generate_random_uuid(device->uuid);
1360 spin_lock_init(&device->io_lock);
1361 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001362 device->io_width = root->sectorsize;
1363 device->io_align = root->sectorsize;
1364 device->sector_size = root->sectorsize;
1365 device->total_bytes = i_size_read(bdev->bd_inode);
1366 device->dev_root = root->fs_info->dev_root;
1367 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001368 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001369 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001370 set_blocksize(device->bdev, 4096);
1371
Yan Zheng2b820322008-11-17 21:11:30 -05001372 if (seeding_dev) {
1373 sb->s_flags &= ~MS_RDONLY;
1374 ret = btrfs_prepare_sprout(trans, root);
1375 BUG_ON(ret);
1376 }
1377
1378 device->fs_devices = root->fs_info->fs_devices;
1379 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1380 list_add(&device->dev_alloc_list,
1381 &root->fs_info->fs_devices->alloc_list);
1382 root->fs_info->fs_devices->num_devices++;
1383 root->fs_info->fs_devices->open_devices++;
1384 root->fs_info->fs_devices->rw_devices++;
1385 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1386
Chris Mason788f20e2008-04-28 15:29:42 -04001387 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1388 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1389 total_bytes + device->total_bytes);
1390
1391 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1392 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1393 total_bytes + 1);
1394
Yan Zheng2b820322008-11-17 21:11:30 -05001395 if (seeding_dev) {
1396 ret = init_first_rw_device(trans, root, device);
1397 BUG_ON(ret);
1398 ret = btrfs_finish_sprout(trans, root);
1399 BUG_ON(ret);
1400 } else {
1401 ret = btrfs_add_device(trans, root, device);
1402 }
1403
Chris Mason913d9522009-03-10 13:17:18 -04001404 /*
1405 * we've got more storage, clear any full flags on the space
1406 * infos
1407 */
1408 btrfs_clear_space_info_full(root->fs_info);
1409
Chris Mason7d9eb122008-07-08 14:19:17 -04001410 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001411 btrfs_commit_transaction(trans, root);
1412
1413 if (seeding_dev) {
1414 mutex_unlock(&uuid_mutex);
1415 up_write(&sb->s_umount);
1416
1417 ret = btrfs_relocate_sys_chunks(root);
1418 BUG_ON(ret);
1419 }
1420out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001421 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001422 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001423error:
Chris Mason15916de2008-11-19 21:17:22 -05001424 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001425 if (seeding_dev) {
1426 mutex_unlock(&uuid_mutex);
1427 up_write(&sb->s_umount);
1428 }
Chris Mason788f20e2008-04-28 15:29:42 -04001429 goto out;
1430}
1431
Chris Masond3977122009-01-05 21:25:51 -05001432static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1433 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001434{
1435 int ret;
1436 struct btrfs_path *path;
1437 struct btrfs_root *root;
1438 struct btrfs_dev_item *dev_item;
1439 struct extent_buffer *leaf;
1440 struct btrfs_key key;
1441
1442 root = device->dev_root->fs_info->chunk_root;
1443
1444 path = btrfs_alloc_path();
1445 if (!path)
1446 return -ENOMEM;
1447
1448 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1449 key.type = BTRFS_DEV_ITEM_KEY;
1450 key.offset = device->devid;
1451
1452 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1453 if (ret < 0)
1454 goto out;
1455
1456 if (ret > 0) {
1457 ret = -ENOENT;
1458 goto out;
1459 }
1460
1461 leaf = path->nodes[0];
1462 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1463
1464 btrfs_set_device_id(leaf, dev_item, device->devid);
1465 btrfs_set_device_type(leaf, dev_item, device->type);
1466 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1467 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1468 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001469 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1470 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1471 btrfs_mark_buffer_dirty(leaf);
1472
1473out:
1474 btrfs_free_path(path);
1475 return ret;
1476}
1477
Chris Mason7d9eb122008-07-08 14:19:17 -04001478static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001479 struct btrfs_device *device, u64 new_size)
1480{
1481 struct btrfs_super_block *super_copy =
1482 &device->dev_root->fs_info->super_copy;
1483 u64 old_total = btrfs_super_total_bytes(super_copy);
1484 u64 diff = new_size - device->total_bytes;
1485
Yan Zheng2b820322008-11-17 21:11:30 -05001486 if (!device->writeable)
1487 return -EACCES;
1488 if (new_size <= device->total_bytes)
1489 return -EINVAL;
1490
Chris Mason8f18cf12008-04-25 16:53:30 -04001491 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001492 device->fs_devices->total_rw_bytes += diff;
1493
1494 device->total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001495 btrfs_clear_space_info_full(device->dev_root->fs_info);
1496
Chris Mason8f18cf12008-04-25 16:53:30 -04001497 return btrfs_update_device(trans, device);
1498}
1499
Chris Mason7d9eb122008-07-08 14:19:17 -04001500int btrfs_grow_device(struct btrfs_trans_handle *trans,
1501 struct btrfs_device *device, u64 new_size)
1502{
1503 int ret;
1504 lock_chunks(device->dev_root);
1505 ret = __btrfs_grow_device(trans, device, new_size);
1506 unlock_chunks(device->dev_root);
1507 return ret;
1508}
1509
Chris Mason8f18cf12008-04-25 16:53:30 -04001510static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1511 struct btrfs_root *root,
1512 u64 chunk_tree, u64 chunk_objectid,
1513 u64 chunk_offset)
1514{
1515 int ret;
1516 struct btrfs_path *path;
1517 struct btrfs_key key;
1518
1519 root = root->fs_info->chunk_root;
1520 path = btrfs_alloc_path();
1521 if (!path)
1522 return -ENOMEM;
1523
1524 key.objectid = chunk_objectid;
1525 key.offset = chunk_offset;
1526 key.type = BTRFS_CHUNK_ITEM_KEY;
1527
1528 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1529 BUG_ON(ret);
1530
1531 ret = btrfs_del_item(trans, root, path);
1532 BUG_ON(ret);
1533
1534 btrfs_free_path(path);
1535 return 0;
1536}
1537
Christoph Hellwigb2950862008-12-02 09:54:17 -05001538static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001539 chunk_offset)
1540{
1541 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1542 struct btrfs_disk_key *disk_key;
1543 struct btrfs_chunk *chunk;
1544 u8 *ptr;
1545 int ret = 0;
1546 u32 num_stripes;
1547 u32 array_size;
1548 u32 len = 0;
1549 u32 cur;
1550 struct btrfs_key key;
1551
1552 array_size = btrfs_super_sys_array_size(super_copy);
1553
1554 ptr = super_copy->sys_chunk_array;
1555 cur = 0;
1556
1557 while (cur < array_size) {
1558 disk_key = (struct btrfs_disk_key *)ptr;
1559 btrfs_disk_key_to_cpu(&key, disk_key);
1560
1561 len = sizeof(*disk_key);
1562
1563 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1564 chunk = (struct btrfs_chunk *)(ptr + len);
1565 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1566 len += btrfs_chunk_item_size(num_stripes);
1567 } else {
1568 ret = -EIO;
1569 break;
1570 }
1571 if (key.objectid == chunk_objectid &&
1572 key.offset == chunk_offset) {
1573 memmove(ptr, ptr + len, array_size - (cur + len));
1574 array_size -= len;
1575 btrfs_set_super_sys_array_size(super_copy, array_size);
1576 } else {
1577 ptr += len;
1578 cur += len;
1579 }
1580 }
1581 return ret;
1582}
1583
Christoph Hellwigb2950862008-12-02 09:54:17 -05001584static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001585 u64 chunk_tree, u64 chunk_objectid,
1586 u64 chunk_offset)
1587{
1588 struct extent_map_tree *em_tree;
1589 struct btrfs_root *extent_root;
1590 struct btrfs_trans_handle *trans;
1591 struct extent_map *em;
1592 struct map_lookup *map;
1593 int ret;
1594 int i;
1595
Chris Masond3977122009-01-05 21:25:51 -05001596 printk(KERN_INFO "btrfs relocating chunk %llu\n",
Chris Mason323da792008-05-09 11:46:48 -04001597 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001598 root = root->fs_info->chunk_root;
1599 extent_root = root->fs_info->extent_root;
1600 em_tree = &root->fs_info->mapping_tree.map_tree;
1601
1602 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001603 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001604 BUG_ON(ret);
1605
1606 trans = btrfs_start_transaction(root, 1);
1607 BUG_ON(!trans);
1608
Chris Mason7d9eb122008-07-08 14:19:17 -04001609 lock_chunks(root);
1610
Chris Mason8f18cf12008-04-25 16:53:30 -04001611 /*
1612 * step two, delete the device extents and the
1613 * chunk tree entries
1614 */
1615 spin_lock(&em_tree->lock);
1616 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1617 spin_unlock(&em_tree->lock);
1618
Chris Masona061fc82008-05-07 11:43:44 -04001619 BUG_ON(em->start > chunk_offset ||
1620 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001621 map = (struct map_lookup *)em->bdev;
1622
1623 for (i = 0; i < map->num_stripes; i++) {
1624 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1625 map->stripes[i].physical);
1626 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001627
Chris Masondfe25022008-05-13 13:46:40 -04001628 if (map->stripes[i].dev) {
1629 ret = btrfs_update_device(trans, map->stripes[i].dev);
1630 BUG_ON(ret);
1631 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001632 }
1633 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1634 chunk_offset);
1635
1636 BUG_ON(ret);
1637
1638 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1639 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1640 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001641 }
1642
Zheng Yan1a40e232008-09-26 10:09:34 -04001643 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1644 BUG_ON(ret);
1645
Chris Mason8f18cf12008-04-25 16:53:30 -04001646 spin_lock(&em_tree->lock);
1647 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001648 spin_unlock(&em_tree->lock);
1649
Chris Mason8f18cf12008-04-25 16:53:30 -04001650 kfree(map);
1651 em->bdev = NULL;
1652
1653 /* once for the tree */
1654 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001655 /* once for us */
1656 free_extent_map(em);
1657
Chris Mason7d9eb122008-07-08 14:19:17 -04001658 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001659 btrfs_end_transaction(trans, root);
1660 return 0;
1661}
1662
Yan Zheng2b820322008-11-17 21:11:30 -05001663static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1664{
1665 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1666 struct btrfs_path *path;
1667 struct extent_buffer *leaf;
1668 struct btrfs_chunk *chunk;
1669 struct btrfs_key key;
1670 struct btrfs_key found_key;
1671 u64 chunk_tree = chunk_root->root_key.objectid;
1672 u64 chunk_type;
1673 int ret;
1674
1675 path = btrfs_alloc_path();
1676 if (!path)
1677 return -ENOMEM;
1678
1679 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1680 key.offset = (u64)-1;
1681 key.type = BTRFS_CHUNK_ITEM_KEY;
1682
1683 while (1) {
1684 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1685 if (ret < 0)
1686 goto error;
1687 BUG_ON(ret == 0);
1688
1689 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1690 key.type);
1691 if (ret < 0)
1692 goto error;
1693 if (ret > 0)
1694 break;
1695
1696 leaf = path->nodes[0];
1697 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1698
1699 chunk = btrfs_item_ptr(leaf, path->slots[0],
1700 struct btrfs_chunk);
1701 chunk_type = btrfs_chunk_type(leaf, chunk);
1702 btrfs_release_path(chunk_root, path);
1703
1704 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1705 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1706 found_key.objectid,
1707 found_key.offset);
1708 BUG_ON(ret);
1709 }
1710
1711 if (found_key.offset == 0)
1712 break;
1713 key.offset = found_key.offset - 1;
1714 }
1715 ret = 0;
1716error:
1717 btrfs_free_path(path);
1718 return ret;
1719}
1720
Chris Masonec44a352008-04-28 15:29:52 -04001721static u64 div_factor(u64 num, int factor)
1722{
1723 if (factor == 10)
1724 return num;
1725 num *= factor;
1726 do_div(num, 10);
1727 return num;
1728}
1729
Chris Masonec44a352008-04-28 15:29:52 -04001730int btrfs_balance(struct btrfs_root *dev_root)
1731{
1732 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001733 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1734 struct btrfs_device *device;
1735 u64 old_size;
1736 u64 size_to_free;
1737 struct btrfs_path *path;
1738 struct btrfs_key key;
1739 struct btrfs_chunk *chunk;
1740 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1741 struct btrfs_trans_handle *trans;
1742 struct btrfs_key found_key;
1743
Yan Zheng2b820322008-11-17 21:11:30 -05001744 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1745 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001746
Chris Mason7d9eb122008-07-08 14:19:17 -04001747 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001748 dev_root = dev_root->fs_info->dev_root;
1749
Chris Masonec44a352008-04-28 15:29:52 -04001750 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001751 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001752 old_size = device->total_bytes;
1753 size_to_free = div_factor(old_size, 1);
1754 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001755 if (!device->writeable ||
1756 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001757 continue;
1758
1759 ret = btrfs_shrink_device(device, old_size - size_to_free);
1760 BUG_ON(ret);
1761
1762 trans = btrfs_start_transaction(dev_root, 1);
1763 BUG_ON(!trans);
1764
1765 ret = btrfs_grow_device(trans, device, old_size);
1766 BUG_ON(ret);
1767
1768 btrfs_end_transaction(trans, dev_root);
1769 }
1770
1771 /* step two, relocate all the chunks */
1772 path = btrfs_alloc_path();
1773 BUG_ON(!path);
1774
1775 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1776 key.offset = (u64)-1;
1777 key.type = BTRFS_CHUNK_ITEM_KEY;
1778
Chris Masond3977122009-01-05 21:25:51 -05001779 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001780 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1781 if (ret < 0)
1782 goto error;
1783
1784 /*
1785 * this shouldn't happen, it means the last relocate
1786 * failed
1787 */
1788 if (ret == 0)
1789 break;
1790
1791 ret = btrfs_previous_item(chunk_root, path, 0,
1792 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001793 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001794 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001795
Chris Masonec44a352008-04-28 15:29:52 -04001796 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1797 path->slots[0]);
1798 if (found_key.objectid != key.objectid)
1799 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001800
Chris Masonec44a352008-04-28 15:29:52 -04001801 chunk = btrfs_item_ptr(path->nodes[0],
1802 path->slots[0],
1803 struct btrfs_chunk);
1804 key.offset = found_key.offset;
1805 /* chunk zero is special */
1806 if (key.offset == 0)
1807 break;
1808
Chris Mason7d9eb122008-07-08 14:19:17 -04001809 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001810 ret = btrfs_relocate_chunk(chunk_root,
1811 chunk_root->root_key.objectid,
1812 found_key.objectid,
1813 found_key.offset);
1814 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001815 }
1816 ret = 0;
1817error:
1818 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001819 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001820 return ret;
1821}
1822
Chris Mason8f18cf12008-04-25 16:53:30 -04001823/*
1824 * shrinking a device means finding all of the device extents past
1825 * the new size, and then following the back refs to the chunks.
1826 * The chunk relocation code actually frees the device extent
1827 */
1828int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1829{
1830 struct btrfs_trans_handle *trans;
1831 struct btrfs_root *root = device->dev_root;
1832 struct btrfs_dev_extent *dev_extent = NULL;
1833 struct btrfs_path *path;
1834 u64 length;
1835 u64 chunk_tree;
1836 u64 chunk_objectid;
1837 u64 chunk_offset;
1838 int ret;
1839 int slot;
1840 struct extent_buffer *l;
1841 struct btrfs_key key;
1842 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1843 u64 old_total = btrfs_super_total_bytes(super_copy);
1844 u64 diff = device->total_bytes - new_size;
1845
Yan Zheng2b820322008-11-17 21:11:30 -05001846 if (new_size >= device->total_bytes)
1847 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001848
1849 path = btrfs_alloc_path();
1850 if (!path)
1851 return -ENOMEM;
1852
1853 trans = btrfs_start_transaction(root, 1);
1854 if (!trans) {
1855 ret = -ENOMEM;
1856 goto done;
1857 }
1858
1859 path->reada = 2;
1860
Chris Mason7d9eb122008-07-08 14:19:17 -04001861 lock_chunks(root);
1862
Chris Mason8f18cf12008-04-25 16:53:30 -04001863 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001864 if (device->writeable)
1865 device->fs_devices->total_rw_bytes -= diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04001866 ret = btrfs_update_device(trans, device);
1867 if (ret) {
Chris Mason7d9eb122008-07-08 14:19:17 -04001868 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001869 btrfs_end_transaction(trans, root);
1870 goto done;
1871 }
1872 WARN_ON(diff > old_total);
1873 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001874 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001875 btrfs_end_transaction(trans, root);
1876
1877 key.objectid = device->devid;
1878 key.offset = (u64)-1;
1879 key.type = BTRFS_DEV_EXTENT_KEY;
1880
1881 while (1) {
1882 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1883 if (ret < 0)
1884 goto done;
1885
1886 ret = btrfs_previous_item(root, path, 0, key.type);
1887 if (ret < 0)
1888 goto done;
1889 if (ret) {
1890 ret = 0;
1891 goto done;
1892 }
1893
1894 l = path->nodes[0];
1895 slot = path->slots[0];
1896 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1897
1898 if (key.objectid != device->devid)
1899 goto done;
1900
1901 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1902 length = btrfs_dev_extent_length(l, dev_extent);
1903
1904 if (key.offset + length <= new_size)
1905 goto done;
1906
1907 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1908 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1909 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1910 btrfs_release_path(root, path);
1911
1912 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1913 chunk_offset);
1914 if (ret)
1915 goto done;
1916 }
1917
1918done:
1919 btrfs_free_path(path);
1920 return ret;
1921}
1922
Christoph Hellwigb2950862008-12-02 09:54:17 -05001923static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001924 struct btrfs_root *root,
1925 struct btrfs_key *key,
1926 struct btrfs_chunk *chunk, int item_size)
1927{
1928 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1929 struct btrfs_disk_key disk_key;
1930 u32 array_size;
1931 u8 *ptr;
1932
1933 array_size = btrfs_super_sys_array_size(super_copy);
1934 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1935 return -EFBIG;
1936
1937 ptr = super_copy->sys_chunk_array + array_size;
1938 btrfs_cpu_key_to_disk(&disk_key, key);
1939 memcpy(ptr, &disk_key, sizeof(disk_key));
1940 ptr += sizeof(disk_key);
1941 memcpy(ptr, chunk, item_size);
1942 item_size += sizeof(disk_key);
1943 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1944 return 0;
1945}
1946
Chris Masond3977122009-01-05 21:25:51 -05001947static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04001948 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04001949{
1950 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1951 return calc_size;
1952 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1953 return calc_size * (num_stripes / sub_stripes);
1954 else
1955 return calc_size * num_stripes;
1956}
1957
Yan Zheng2b820322008-11-17 21:11:30 -05001958static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1959 struct btrfs_root *extent_root,
1960 struct map_lookup **map_ret,
1961 u64 *num_bytes, u64 *stripe_size,
1962 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001963{
Chris Mason593060d2008-03-25 16:50:33 -04001964 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001965 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001966 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04001967 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05001968 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001969 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04001970 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05001971 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04001972 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001973 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001974 u64 max_chunk_size = calc_size;
1975 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001976 u64 avail;
1977 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001978 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04001979 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001980 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001981 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001982 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001983 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001984 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001985 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001986
Chris Masonec44a352008-04-28 15:29:52 -04001987 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1988 (type & BTRFS_BLOCK_GROUP_DUP)) {
1989 WARN_ON(1);
1990 type &= ~BTRFS_BLOCK_GROUP_DUP;
1991 }
Yan Zheng2b820322008-11-17 21:11:30 -05001992 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04001993 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001994
Chris Masona40a90a2008-04-18 11:55:51 -04001995 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001996 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04001997 min_stripes = 2;
1998 }
1999 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04002000 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002001 min_stripes = 2;
2002 }
Chris Mason8790d502008-04-03 16:29:03 -04002003 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002004 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002005 if (num_stripes < 2)
2006 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04002007 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002008 }
Chris Mason321aecc2008-04-16 10:49:51 -04002009 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002010 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04002011 if (num_stripes < 4)
2012 return -ENOSPC;
2013 num_stripes &= ~(u32)1;
2014 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002015 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002016 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002017
2018 if (type & BTRFS_BLOCK_GROUP_DATA) {
2019 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002020 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002021 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2022 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002023 min_stripe_size = 32 * 1024 * 1024;
2024 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2025 calc_size = 8 * 1024 * 1024;
2026 max_chunk_size = calc_size * 2;
2027 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002028 }
2029
Yan Zheng2b820322008-11-17 21:11:30 -05002030 /* we don't want a chunk larger than 10% of writeable space */
2031 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2032 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002033
Chris Masona40a90a2008-04-18 11:55:51 -04002034again:
Yan Zheng2b820322008-11-17 21:11:30 -05002035 if (!map || map->num_stripes != num_stripes) {
2036 kfree(map);
2037 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2038 if (!map)
2039 return -ENOMEM;
2040 map->num_stripes = num_stripes;
2041 }
2042
Chris Mason9b3f68b2008-04-18 10:29:51 -04002043 if (calc_size * num_stripes > max_chunk_size) {
2044 calc_size = max_chunk_size;
2045 do_div(calc_size, num_stripes);
2046 do_div(calc_size, stripe_len);
2047 calc_size *= stripe_len;
2048 }
2049 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002050 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002051
Chris Mason9b3f68b2008-04-18 10:29:51 -04002052 do_div(calc_size, stripe_len);
2053 calc_size *= stripe_len;
2054
Yan Zheng2b820322008-11-17 21:11:30 -05002055 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002056 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002057
2058 if (type & BTRFS_BLOCK_GROUP_DUP)
2059 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002060 else
2061 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002062
Josef Bacik0f9dd462008-09-23 13:14:11 -04002063 /*
2064 * we add 1MB because we never use the first 1MB of the device, unless
2065 * we've looped, then we are likely allocating the maximum amount of
2066 * space left already
2067 */
2068 if (!looped)
2069 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002070
Yan Zheng2b820322008-11-17 21:11:30 -05002071 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002072 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002073 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002074 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002075 if (device->total_bytes > device->bytes_used)
2076 avail = device->total_bytes - device->bytes_used;
2077 else
2078 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002079 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002080
Chris Masondfe25022008-05-13 13:46:40 -04002081 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002082 ret = find_free_dev_extent(trans, device,
2083 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002084 if (ret == 0) {
2085 list_move_tail(&device->dev_alloc_list,
2086 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002087 map->stripes[index].dev = device;
2088 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002089 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002090 if (type & BTRFS_BLOCK_GROUP_DUP) {
2091 map->stripes[index].dev = device;
2092 map->stripes[index].physical =
2093 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002094 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002095 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002096 }
Chris Masondfe25022008-05-13 13:46:40 -04002097 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002098 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002099 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002100 break;
2101 }
Yan Zheng2b820322008-11-17 21:11:30 -05002102 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002103 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002104 if (index >= min_stripes) {
2105 num_stripes = index;
2106 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2107 num_stripes /= sub_stripes;
2108 num_stripes *= sub_stripes;
2109 }
2110 looped = 1;
2111 goto again;
2112 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002113 if (!looped && max_avail > 0) {
2114 looped = 1;
2115 calc_size = max_avail;
2116 goto again;
2117 }
Yan Zheng2b820322008-11-17 21:11:30 -05002118 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002119 return -ENOSPC;
2120 }
Chris Mason593060d2008-03-25 16:50:33 -04002121 map->sector_size = extent_root->sectorsize;
2122 map->stripe_len = stripe_len;
2123 map->io_align = stripe_len;
2124 map->io_width = stripe_len;
2125 map->type = type;
2126 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002127 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002128
Yan Zheng2b820322008-11-17 21:11:30 -05002129 *map_ret = map;
2130 *stripe_size = calc_size;
2131 *num_bytes = chunk_bytes_by_type(type, calc_size,
2132 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002133
2134 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002135 if (!em) {
2136 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002137 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002138 }
Chris Mason0b86a832008-03-24 15:01:56 -04002139 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002140 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002141 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002142 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002143 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002144
Chris Mason0b86a832008-03-24 15:01:56 -04002145 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2146 spin_lock(&em_tree->lock);
2147 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002148 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002149 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002150 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002151
2152 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2153 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2154 start, *num_bytes);
2155 BUG_ON(ret);
2156
2157 index = 0;
2158 while (index < map->num_stripes) {
2159 device = map->stripes[index].dev;
2160 dev_offset = map->stripes[index].physical;
2161
2162 ret = btrfs_alloc_dev_extent(trans, device,
2163 info->chunk_root->root_key.objectid,
2164 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2165 start, dev_offset, calc_size);
2166 BUG_ON(ret);
2167 index++;
2168 }
2169
2170 return 0;
2171}
2172
2173static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2174 struct btrfs_root *extent_root,
2175 struct map_lookup *map, u64 chunk_offset,
2176 u64 chunk_size, u64 stripe_size)
2177{
2178 u64 dev_offset;
2179 struct btrfs_key key;
2180 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2181 struct btrfs_device *device;
2182 struct btrfs_chunk *chunk;
2183 struct btrfs_stripe *stripe;
2184 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2185 int index = 0;
2186 int ret;
2187
2188 chunk = kzalloc(item_size, GFP_NOFS);
2189 if (!chunk)
2190 return -ENOMEM;
2191
2192 index = 0;
2193 while (index < map->num_stripes) {
2194 device = map->stripes[index].dev;
2195 device->bytes_used += stripe_size;
2196 ret = btrfs_update_device(trans, device);
2197 BUG_ON(ret);
2198 index++;
2199 }
2200
2201 index = 0;
2202 stripe = &chunk->stripe;
2203 while (index < map->num_stripes) {
2204 device = map->stripes[index].dev;
2205 dev_offset = map->stripes[index].physical;
2206
2207 btrfs_set_stack_stripe_devid(stripe, device->devid);
2208 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2209 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2210 stripe++;
2211 index++;
2212 }
2213
2214 btrfs_set_stack_chunk_length(chunk, chunk_size);
2215 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2216 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2217 btrfs_set_stack_chunk_type(chunk, map->type);
2218 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2219 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2220 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2221 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2222 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2223
2224 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2225 key.type = BTRFS_CHUNK_ITEM_KEY;
2226 key.offset = chunk_offset;
2227
2228 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2229 BUG_ON(ret);
2230
2231 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2232 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2233 item_size);
2234 BUG_ON(ret);
2235 }
2236 kfree(chunk);
2237 return 0;
2238}
2239
2240/*
2241 * Chunk allocation falls into two parts. The first part does works
2242 * that make the new allocated chunk useable, but not do any operation
2243 * that modifies the chunk tree. The second part does the works that
2244 * require modifying the chunk tree. This division is important for the
2245 * bootstrap process of adding storage to a seed btrfs.
2246 */
2247int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2248 struct btrfs_root *extent_root, u64 type)
2249{
2250 u64 chunk_offset;
2251 u64 chunk_size;
2252 u64 stripe_size;
2253 struct map_lookup *map;
2254 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2255 int ret;
2256
2257 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2258 &chunk_offset);
2259 if (ret)
2260 return ret;
2261
2262 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2263 &stripe_size, chunk_offset, type);
2264 if (ret)
2265 return ret;
2266
2267 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2268 chunk_size, stripe_size);
2269 BUG_ON(ret);
2270 return 0;
2271}
2272
Chris Masond3977122009-01-05 21:25:51 -05002273static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002274 struct btrfs_root *root,
2275 struct btrfs_device *device)
2276{
2277 u64 chunk_offset;
2278 u64 sys_chunk_offset;
2279 u64 chunk_size;
2280 u64 sys_chunk_size;
2281 u64 stripe_size;
2282 u64 sys_stripe_size;
2283 u64 alloc_profile;
2284 struct map_lookup *map;
2285 struct map_lookup *sys_map;
2286 struct btrfs_fs_info *fs_info = root->fs_info;
2287 struct btrfs_root *extent_root = fs_info->extent_root;
2288 int ret;
2289
2290 ret = find_next_chunk(fs_info->chunk_root,
2291 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2292 BUG_ON(ret);
2293
2294 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2295 (fs_info->metadata_alloc_profile &
2296 fs_info->avail_metadata_alloc_bits);
2297 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2298
2299 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2300 &stripe_size, chunk_offset, alloc_profile);
2301 BUG_ON(ret);
2302
2303 sys_chunk_offset = chunk_offset + chunk_size;
2304
2305 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2306 (fs_info->system_alloc_profile &
2307 fs_info->avail_system_alloc_bits);
2308 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2309
2310 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2311 &sys_chunk_size, &sys_stripe_size,
2312 sys_chunk_offset, alloc_profile);
2313 BUG_ON(ret);
2314
2315 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2316 BUG_ON(ret);
2317
2318 /*
2319 * Modifying chunk tree needs allocating new blocks from both
2320 * system block group and metadata block group. So we only can
2321 * do operations require modifying the chunk tree after both
2322 * block groups were created.
2323 */
2324 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2325 chunk_size, stripe_size);
2326 BUG_ON(ret);
2327
2328 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2329 sys_chunk_offset, sys_chunk_size,
2330 sys_stripe_size);
2331 BUG_ON(ret);
2332 return 0;
2333}
2334
2335int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2336{
2337 struct extent_map *em;
2338 struct map_lookup *map;
2339 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2340 int readonly = 0;
2341 int i;
2342
2343 spin_lock(&map_tree->map_tree.lock);
2344 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2345 spin_unlock(&map_tree->map_tree.lock);
2346 if (!em)
2347 return 1;
2348
2349 map = (struct map_lookup *)em->bdev;
2350 for (i = 0; i < map->num_stripes; i++) {
2351 if (!map->stripes[i].dev->writeable) {
2352 readonly = 1;
2353 break;
2354 }
2355 }
2356 free_extent_map(em);
2357 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002358}
2359
2360void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2361{
2362 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2363}
2364
2365void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2366{
2367 struct extent_map *em;
2368
Chris Masond3977122009-01-05 21:25:51 -05002369 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002370 spin_lock(&tree->map_tree.lock);
2371 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2372 if (em)
2373 remove_extent_mapping(&tree->map_tree, em);
2374 spin_unlock(&tree->map_tree.lock);
2375 if (!em)
2376 break;
2377 kfree(em->bdev);
2378 /* once for us */
2379 free_extent_map(em);
2380 /* once for the tree */
2381 free_extent_map(em);
2382 }
2383}
2384
Chris Masonf1885912008-04-09 16:28:12 -04002385int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2386{
2387 struct extent_map *em;
2388 struct map_lookup *map;
2389 struct extent_map_tree *em_tree = &map_tree->map_tree;
2390 int ret;
2391
2392 spin_lock(&em_tree->lock);
2393 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002394 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002395 BUG_ON(!em);
2396
2397 BUG_ON(em->start > logical || em->start + em->len < logical);
2398 map = (struct map_lookup *)em->bdev;
2399 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2400 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002401 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2402 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002403 else
2404 ret = 1;
2405 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002406 return ret;
2407}
2408
Chris Masondfe25022008-05-13 13:46:40 -04002409static int find_live_mirror(struct map_lookup *map, int first, int num,
2410 int optimal)
2411{
2412 int i;
2413 if (map->stripes[optimal].dev->bdev)
2414 return optimal;
2415 for (i = first; i < first + num; i++) {
2416 if (map->stripes[i].dev->bdev)
2417 return i;
2418 }
2419 /* we couldn't find one that doesn't fail. Just return something
2420 * and the io error handling code will clean up eventually
2421 */
2422 return optimal;
2423}
2424
Chris Masonf2d8d742008-04-21 10:03:05 -04002425static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2426 u64 logical, u64 *length,
2427 struct btrfs_multi_bio **multi_ret,
2428 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002429{
2430 struct extent_map *em;
2431 struct map_lookup *map;
2432 struct extent_map_tree *em_tree = &map_tree->map_tree;
2433 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002434 u64 stripe_offset;
2435 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002436 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002437 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002438 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002439 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002440 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002441 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002442 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002443
Chris Masond3977122009-01-05 21:25:51 -05002444 if (multi_ret && !(rw & (1 << BIO_RW)))
Chris Masoncea9e442008-04-09 16:28:12 -04002445 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002446again:
2447 if (multi_ret) {
2448 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2449 GFP_NOFS);
2450 if (!multi)
2451 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002452
2453 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002454 }
Chris Mason0b86a832008-03-24 15:01:56 -04002455
2456 spin_lock(&em_tree->lock);
2457 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002458 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002459
2460 if (!em && unplug_page)
2461 return 0;
2462
Chris Mason3b951512008-04-17 11:29:12 -04002463 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002464 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2465 (unsigned long long)logical,
2466 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002467 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002468 }
Chris Mason0b86a832008-03-24 15:01:56 -04002469
2470 BUG_ON(em->start > logical || em->start + em->len < logical);
2471 map = (struct map_lookup *)em->bdev;
2472 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002473
Chris Masonf1885912008-04-09 16:28:12 -04002474 if (mirror_num > map->num_stripes)
2475 mirror_num = 0;
2476
Chris Masoncea9e442008-04-09 16:28:12 -04002477 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002478 if (rw & (1 << BIO_RW)) {
2479 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2480 BTRFS_BLOCK_GROUP_DUP)) {
2481 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002482 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002483 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2484 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002485 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002486 }
2487 }
2488 if (multi_ret && rw == WRITE &&
2489 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002490 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002491 free_extent_map(em);
2492 kfree(multi);
2493 goto again;
2494 }
Chris Mason593060d2008-03-25 16:50:33 -04002495 stripe_nr = offset;
2496 /*
2497 * stripe_nr counts the total number of stripes we have to stride
2498 * to get to this block
2499 */
2500 do_div(stripe_nr, map->stripe_len);
2501
2502 stripe_offset = stripe_nr * map->stripe_len;
2503 BUG_ON(offset < stripe_offset);
2504
2505 /* stripe_offset is the offset of this block in its stripe*/
2506 stripe_offset = offset - stripe_offset;
2507
Chris Masoncea9e442008-04-09 16:28:12 -04002508 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002509 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002510 BTRFS_BLOCK_GROUP_DUP)) {
2511 /* we limit the length of each bio to what fits in a stripe */
2512 *length = min_t(u64, em->len - offset,
2513 map->stripe_len - stripe_offset);
2514 } else {
2515 *length = em->len - offset;
2516 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002517
2518 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002519 goto out;
2520
Chris Masonf2d8d742008-04-21 10:03:05 -04002521 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002522 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002523 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002524 if (unplug_page || (rw & (1 << BIO_RW)))
2525 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002526 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002527 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002528 else {
2529 stripe_index = find_live_mirror(map, 0,
2530 map->num_stripes,
2531 current->pid % map->num_stripes);
2532 }
Chris Mason2fff7342008-04-29 14:12:09 -04002533
Chris Mason611f0e02008-04-03 16:29:03 -04002534 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002535 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002536 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002537 else if (mirror_num)
2538 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002539
Chris Mason321aecc2008-04-16 10:49:51 -04002540 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2541 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002542
2543 stripe_index = do_div(stripe_nr, factor);
2544 stripe_index *= map->sub_stripes;
2545
Chris Masonf2d8d742008-04-21 10:03:05 -04002546 if (unplug_page || (rw & (1 << BIO_RW)))
2547 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002548 else if (mirror_num)
2549 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002550 else {
2551 stripe_index = find_live_mirror(map, stripe_index,
2552 map->sub_stripes, stripe_index +
2553 current->pid % map->sub_stripes);
2554 }
Chris Mason8790d502008-04-03 16:29:03 -04002555 } else {
2556 /*
2557 * after this do_div call, stripe_nr is the number of stripes
2558 * on this device we have to walk to find the data, and
2559 * stripe_index is the number of our device in the stripe array
2560 */
2561 stripe_index = do_div(stripe_nr, map->num_stripes);
2562 }
Chris Mason593060d2008-03-25 16:50:33 -04002563 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002564
Chris Masonf2d8d742008-04-21 10:03:05 -04002565 for (i = 0; i < num_stripes; i++) {
2566 if (unplug_page) {
2567 struct btrfs_device *device;
2568 struct backing_dev_info *bdi;
2569
2570 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002571 if (device->bdev) {
2572 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002573 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002574 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002575 }
2576 } else {
2577 multi->stripes[i].physical =
2578 map->stripes[stripe_index].physical +
2579 stripe_offset + stripe_nr * map->stripe_len;
2580 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2581 }
Chris Masoncea9e442008-04-09 16:28:12 -04002582 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002583 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002584 if (multi_ret) {
2585 *multi_ret = multi;
2586 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002587 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002588 }
Chris Masoncea9e442008-04-09 16:28:12 -04002589out:
Chris Mason0b86a832008-03-24 15:01:56 -04002590 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002591 return 0;
2592}
2593
Chris Masonf2d8d742008-04-21 10:03:05 -04002594int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2595 u64 logical, u64 *length,
2596 struct btrfs_multi_bio **multi_ret, int mirror_num)
2597{
2598 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2599 mirror_num, NULL);
2600}
2601
Yan Zhenga512bbf2008-12-08 16:46:26 -05002602int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2603 u64 chunk_start, u64 physical, u64 devid,
2604 u64 **logical, int *naddrs, int *stripe_len)
2605{
2606 struct extent_map_tree *em_tree = &map_tree->map_tree;
2607 struct extent_map *em;
2608 struct map_lookup *map;
2609 u64 *buf;
2610 u64 bytenr;
2611 u64 length;
2612 u64 stripe_nr;
2613 int i, j, nr = 0;
2614
2615 spin_lock(&em_tree->lock);
2616 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2617 spin_unlock(&em_tree->lock);
2618
2619 BUG_ON(!em || em->start != chunk_start);
2620 map = (struct map_lookup *)em->bdev;
2621
2622 length = em->len;
2623 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2624 do_div(length, map->num_stripes / map->sub_stripes);
2625 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2626 do_div(length, map->num_stripes);
2627
2628 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2629 BUG_ON(!buf);
2630
2631 for (i = 0; i < map->num_stripes; i++) {
2632 if (devid && map->stripes[i].dev->devid != devid)
2633 continue;
2634 if (map->stripes[i].physical > physical ||
2635 map->stripes[i].physical + length <= physical)
2636 continue;
2637
2638 stripe_nr = physical - map->stripes[i].physical;
2639 do_div(stripe_nr, map->stripe_len);
2640
2641 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2642 stripe_nr = stripe_nr * map->num_stripes + i;
2643 do_div(stripe_nr, map->sub_stripes);
2644 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2645 stripe_nr = stripe_nr * map->num_stripes + i;
2646 }
2647 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002648 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002649 for (j = 0; j < nr; j++) {
2650 if (buf[j] == bytenr)
2651 break;
2652 }
Chris Mason934d3752008-12-08 16:43:10 -05002653 if (j == nr) {
2654 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002655 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002656 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002657 }
2658
2659 for (i = 0; i > nr; i++) {
2660 struct btrfs_multi_bio *multi;
2661 struct btrfs_bio_stripe *stripe;
2662 int ret;
2663
2664 length = 1;
2665 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2666 &length, &multi, 0);
2667 BUG_ON(ret);
2668
2669 stripe = multi->stripes;
2670 for (j = 0; j < multi->num_stripes; j++) {
2671 if (stripe->physical >= physical &&
2672 physical < stripe->physical + length)
2673 break;
2674 }
2675 BUG_ON(j >= multi->num_stripes);
2676 kfree(multi);
2677 }
2678
2679 *logical = buf;
2680 *naddrs = nr;
2681 *stripe_len = map->stripe_len;
2682
2683 free_extent_map(em);
2684 return 0;
2685}
2686
Chris Masonf2d8d742008-04-21 10:03:05 -04002687int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2688 u64 logical, struct page *page)
2689{
2690 u64 length = PAGE_CACHE_SIZE;
2691 return __btrfs_map_block(map_tree, READ, logical, &length,
2692 NULL, 0, page);
2693}
2694
Chris Mason8790d502008-04-03 16:29:03 -04002695static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002696{
Chris Masoncea9e442008-04-09 16:28:12 -04002697 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002698 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002699
Chris Mason8790d502008-04-03 16:29:03 -04002700 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002701 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002702
Chris Mason7d2b4da2008-08-05 10:13:57 -04002703 if (bio == multi->orig_bio)
2704 is_orig_bio = 1;
2705
Chris Masoncea9e442008-04-09 16:28:12 -04002706 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002707 if (!is_orig_bio) {
2708 bio_put(bio);
2709 bio = multi->orig_bio;
2710 }
Chris Mason8790d502008-04-03 16:29:03 -04002711 bio->bi_private = multi->private;
2712 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002713 /* only send an error to the higher layers if it is
2714 * beyond the tolerance of the multi-bio
2715 */
Chris Mason1259ab72008-05-12 13:39:03 -04002716 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002717 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002718 } else if (err) {
2719 /*
2720 * this bio is actually up to date, we didn't
2721 * go over the max number of errors
2722 */
2723 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002724 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002725 }
Chris Mason8790d502008-04-03 16:29:03 -04002726 kfree(multi);
2727
2728 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002729 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002730 bio_put(bio);
2731 }
Chris Mason8790d502008-04-03 16:29:03 -04002732}
2733
Chris Mason8b712842008-06-11 16:50:36 -04002734struct async_sched {
2735 struct bio *bio;
2736 int rw;
2737 struct btrfs_fs_info *info;
2738 struct btrfs_work work;
2739};
2740
2741/*
2742 * see run_scheduled_bios for a description of why bios are collected for
2743 * async submit.
2744 *
2745 * This will add one bio to the pending list for a device and make sure
2746 * the work struct is scheduled.
2747 */
Chris Masond3977122009-01-05 21:25:51 -05002748static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002749 struct btrfs_device *device,
2750 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002751{
2752 int should_queue = 1;
2753
2754 /* don't bother with additional async steps for reads, right now */
2755 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002756 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002757 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002758 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002759 return 0;
2760 }
2761
2762 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04002763 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002764 * higher layers. Otherwise, the async bio makes it appear we have
2765 * made progress against dirty pages when we've really just put it
2766 * on a queue for later
2767 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04002768 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002769 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002770 bio->bi_next = NULL;
2771 bio->bi_rw |= rw;
2772
2773 spin_lock(&device->io_lock);
2774
2775 if (device->pending_bio_tail)
2776 device->pending_bio_tail->bi_next = bio;
2777
2778 device->pending_bio_tail = bio;
2779 if (!device->pending_bios)
2780 device->pending_bios = bio;
2781 if (device->running_pending)
2782 should_queue = 0;
2783
2784 spin_unlock(&device->io_lock);
2785
2786 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002787 btrfs_queue_worker(&root->fs_info->submit_workers,
2788 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002789 return 0;
2790}
2791
Chris Masonf1885912008-04-09 16:28:12 -04002792int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002793 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002794{
2795 struct btrfs_mapping_tree *map_tree;
2796 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002797 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002798 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002799 u64 length = 0;
2800 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002801 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002802 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002803 int dev_nr = 0;
2804 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002805
Chris Masonf2d8d742008-04-21 10:03:05 -04002806 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002807 map_tree = &root->fs_info->mapping_tree;
2808 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002809
Chris Masonf1885912008-04-09 16:28:12 -04002810 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2811 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002812 BUG_ON(ret);
2813
2814 total_devs = multi->num_stripes;
2815 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05002816 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2817 "len %llu\n", (unsigned long long)logical,
2818 (unsigned long long)length,
2819 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04002820 BUG();
2821 }
2822 multi->end_io = first_bio->bi_end_io;
2823 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002824 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002825 atomic_set(&multi->stripes_pending, multi->num_stripes);
2826
Chris Masond3977122009-01-05 21:25:51 -05002827 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002828 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002829 if (dev_nr < total_devs - 1) {
2830 bio = bio_clone(first_bio, GFP_NOFS);
2831 BUG_ON(!bio);
2832 } else {
2833 bio = first_bio;
2834 }
2835 bio->bi_private = multi;
2836 bio->bi_end_io = end_bio_multi_stripe;
2837 }
Chris Masoncea9e442008-04-09 16:28:12 -04002838 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2839 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002840 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002841 if (dev && dev->bdev) {
2842 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002843 if (async_submit)
2844 schedule_bio(root, dev, rw, bio);
2845 else
2846 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002847 } else {
2848 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2849 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002850 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002851 }
Chris Mason8790d502008-04-03 16:29:03 -04002852 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002853 }
Chris Masoncea9e442008-04-09 16:28:12 -04002854 if (total_devs == 1)
2855 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002856 return 0;
2857}
2858
Chris Masona4437552008-04-18 10:29:38 -04002859struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05002860 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04002861{
Yan Zheng2b820322008-11-17 21:11:30 -05002862 struct btrfs_device *device;
2863 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002864
Yan Zheng2b820322008-11-17 21:11:30 -05002865 cur_devices = root->fs_info->fs_devices;
2866 while (cur_devices) {
2867 if (!fsid ||
2868 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2869 device = __find_device(&cur_devices->devices,
2870 devid, uuid);
2871 if (device)
2872 return device;
2873 }
2874 cur_devices = cur_devices->seed;
2875 }
2876 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002877}
2878
Chris Masondfe25022008-05-13 13:46:40 -04002879static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2880 u64 devid, u8 *dev_uuid)
2881{
2882 struct btrfs_device *device;
2883 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2884
2885 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05002886 if (!device)
2887 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04002888 list_add(&device->dev_list,
2889 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04002890 device->barriers = 1;
2891 device->dev_root = root->fs_info->dev_root;
2892 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002893 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05002894 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04002895 fs_devices->num_devices++;
2896 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05002897 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04002898 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2899 return device;
2900}
2901
Chris Mason0b86a832008-03-24 15:01:56 -04002902static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2903 struct extent_buffer *leaf,
2904 struct btrfs_chunk *chunk)
2905{
2906 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2907 struct map_lookup *map;
2908 struct extent_map *em;
2909 u64 logical;
2910 u64 length;
2911 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002912 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002913 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002914 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002915 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002916
Chris Masone17cade2008-04-15 15:41:47 -04002917 logical = key->offset;
2918 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002919
Chris Mason0b86a832008-03-24 15:01:56 -04002920 spin_lock(&map_tree->map_tree.lock);
2921 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002922 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002923
2924 /* already mapped? */
2925 if (em && em->start <= logical && em->start + em->len > logical) {
2926 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002927 return 0;
2928 } else if (em) {
2929 free_extent_map(em);
2930 }
Chris Mason0b86a832008-03-24 15:01:56 -04002931
Chris Mason0b86a832008-03-24 15:01:56 -04002932 em = alloc_extent_map(GFP_NOFS);
2933 if (!em)
2934 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002935 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2936 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002937 if (!map) {
2938 free_extent_map(em);
2939 return -ENOMEM;
2940 }
2941
2942 em->bdev = (struct block_device *)map;
2943 em->start = logical;
2944 em->len = length;
2945 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002946 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002947
Chris Mason593060d2008-03-25 16:50:33 -04002948 map->num_stripes = num_stripes;
2949 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2950 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2951 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2952 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2953 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002954 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002955 for (i = 0; i < num_stripes; i++) {
2956 map->stripes[i].physical =
2957 btrfs_stripe_offset_nr(leaf, chunk, i);
2958 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002959 read_extent_buffer(leaf, uuid, (unsigned long)
2960 btrfs_stripe_dev_uuid_nr(chunk, i),
2961 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002962 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2963 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04002964 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002965 kfree(map);
2966 free_extent_map(em);
2967 return -EIO;
2968 }
Chris Masondfe25022008-05-13 13:46:40 -04002969 if (!map->stripes[i].dev) {
2970 map->stripes[i].dev =
2971 add_missing_dev(root, devid, uuid);
2972 if (!map->stripes[i].dev) {
2973 kfree(map);
2974 free_extent_map(em);
2975 return -EIO;
2976 }
2977 }
2978 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002979 }
2980
2981 spin_lock(&map_tree->map_tree.lock);
2982 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002983 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002984 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002985 free_extent_map(em);
2986
2987 return 0;
2988}
2989
2990static int fill_device_from_item(struct extent_buffer *leaf,
2991 struct btrfs_dev_item *dev_item,
2992 struct btrfs_device *device)
2993{
2994 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04002995
2996 device->devid = btrfs_device_id(leaf, dev_item);
2997 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2998 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2999 device->type = btrfs_device_type(leaf, dev_item);
3000 device->io_align = btrfs_device_io_align(leaf, dev_item);
3001 device->io_width = btrfs_device_io_width(leaf, dev_item);
3002 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003003
3004 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003005 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003006
Chris Mason0b86a832008-03-24 15:01:56 -04003007 return 0;
3008}
3009
Yan Zheng2b820322008-11-17 21:11:30 -05003010static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3011{
3012 struct btrfs_fs_devices *fs_devices;
3013 int ret;
3014
3015 mutex_lock(&uuid_mutex);
3016
3017 fs_devices = root->fs_info->fs_devices->seed;
3018 while (fs_devices) {
3019 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3020 ret = 0;
3021 goto out;
3022 }
3023 fs_devices = fs_devices->seed;
3024 }
3025
3026 fs_devices = find_fsid(fsid);
3027 if (!fs_devices) {
3028 ret = -ENOENT;
3029 goto out;
3030 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003031
3032 fs_devices = clone_fs_devices(fs_devices);
3033 if (IS_ERR(fs_devices)) {
3034 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003035 goto out;
3036 }
3037
Christoph Hellwig97288f22008-12-02 06:36:09 -05003038 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003039 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003040 if (ret)
3041 goto out;
3042
3043 if (!fs_devices->seeding) {
3044 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003045 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003046 ret = -EINVAL;
3047 goto out;
3048 }
3049
3050 fs_devices->seed = root->fs_info->fs_devices->seed;
3051 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003052out:
3053 mutex_unlock(&uuid_mutex);
3054 return ret;
3055}
3056
Chris Mason0d81ba52008-03-24 15:02:07 -04003057static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003058 struct extent_buffer *leaf,
3059 struct btrfs_dev_item *dev_item)
3060{
3061 struct btrfs_device *device;
3062 u64 devid;
3063 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003064 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003065 u8 dev_uuid[BTRFS_UUID_SIZE];
3066
Chris Mason0b86a832008-03-24 15:01:56 -04003067 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003068 read_extent_buffer(leaf, dev_uuid,
3069 (unsigned long)btrfs_device_uuid(dev_item),
3070 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003071 read_extent_buffer(leaf, fs_uuid,
3072 (unsigned long)btrfs_device_fsid(dev_item),
3073 BTRFS_UUID_SIZE);
3074
3075 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3076 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003077 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003078 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003079 }
3080
3081 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3082 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003083 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003084 return -EIO;
3085
3086 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003087 printk(KERN_WARNING "warning devid %llu missing\n",
3088 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003089 device = add_missing_dev(root, devid, dev_uuid);
3090 if (!device)
3091 return -ENOMEM;
3092 }
3093 }
3094
3095 if (device->fs_devices != root->fs_info->fs_devices) {
3096 BUG_ON(device->writeable);
3097 if (device->generation !=
3098 btrfs_device_generation(leaf, dev_item))
3099 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003100 }
Chris Mason0b86a832008-03-24 15:01:56 -04003101
3102 fill_device_from_item(leaf, dev_item, device);
3103 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003104 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003105 if (device->writeable)
3106 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003107 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003108 return ret;
3109}
3110
Chris Mason0d81ba52008-03-24 15:02:07 -04003111int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3112{
3113 struct btrfs_dev_item *dev_item;
3114
3115 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3116 dev_item);
3117 return read_one_dev(root, buf, dev_item);
3118}
3119
Yan Zhenge4404d62008-12-12 10:03:26 -05003120int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003121{
3122 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003123 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003124 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003125 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003126 u8 *ptr;
3127 unsigned long sb_ptr;
3128 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003129 u32 num_stripes;
3130 u32 array_size;
3131 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003132 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003133 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003134
Yan Zhenge4404d62008-12-12 10:03:26 -05003135 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003136 BTRFS_SUPER_INFO_SIZE);
3137 if (!sb)
3138 return -ENOMEM;
3139 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003140 btrfs_set_buffer_lockdep_class(sb, 0);
3141
Chris Masona061fc82008-05-07 11:43:44 -04003142 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003143 array_size = btrfs_super_sys_array_size(super_copy);
3144
Chris Mason0b86a832008-03-24 15:01:56 -04003145 ptr = super_copy->sys_chunk_array;
3146 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3147 cur = 0;
3148
3149 while (cur < array_size) {
3150 disk_key = (struct btrfs_disk_key *)ptr;
3151 btrfs_disk_key_to_cpu(&key, disk_key);
3152
Chris Masona061fc82008-05-07 11:43:44 -04003153 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003154 sb_ptr += len;
3155 cur += len;
3156
Chris Mason0d81ba52008-03-24 15:02:07 -04003157 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003158 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003159 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003160 if (ret)
3161 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003162 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3163 len = btrfs_chunk_item_size(num_stripes);
3164 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003165 ret = -EIO;
3166 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003167 }
3168 ptr += len;
3169 sb_ptr += len;
3170 cur += len;
3171 }
Chris Masona061fc82008-05-07 11:43:44 -04003172 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003173 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003174}
3175
3176int btrfs_read_chunk_tree(struct btrfs_root *root)
3177{
3178 struct btrfs_path *path;
3179 struct extent_buffer *leaf;
3180 struct btrfs_key key;
3181 struct btrfs_key found_key;
3182 int ret;
3183 int slot;
3184
3185 root = root->fs_info->chunk_root;
3186
3187 path = btrfs_alloc_path();
3188 if (!path)
3189 return -ENOMEM;
3190
3191 /* first we search for all of the device items, and then we
3192 * read in all of the chunk items. This way we can create chunk
3193 * mappings that reference all of the devices that are afound
3194 */
3195 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3196 key.offset = 0;
3197 key.type = 0;
3198again:
3199 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masond3977122009-01-05 21:25:51 -05003200 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003201 leaf = path->nodes[0];
3202 slot = path->slots[0];
3203 if (slot >= btrfs_header_nritems(leaf)) {
3204 ret = btrfs_next_leaf(root, path);
3205 if (ret == 0)
3206 continue;
3207 if (ret < 0)
3208 goto error;
3209 break;
3210 }
3211 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3212 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3213 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3214 break;
3215 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3216 struct btrfs_dev_item *dev_item;
3217 dev_item = btrfs_item_ptr(leaf, slot,
3218 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003219 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003220 if (ret)
3221 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003222 }
3223 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3224 struct btrfs_chunk *chunk;
3225 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3226 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003227 if (ret)
3228 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003229 }
3230 path->slots[0]++;
3231 }
3232 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3233 key.objectid = 0;
3234 btrfs_release_path(root, path);
3235 goto again;
3236 }
Chris Mason0b86a832008-03-24 15:01:56 -04003237 ret = 0;
3238error:
Yan Zheng2b820322008-11-17 21:11:30 -05003239 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003240 return ret;
3241}