blob: ecf0633ab8cc9093cb6e48beb034dfc1274c5a24 [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 Mason593060d2008-03-25 16:50:33 -040023#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040024#include "ctree.h"
25#include "extent_map.h"
26#include "disk-io.h"
27#include "transaction.h"
28#include "print-tree.h"
29#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040030#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031
Chris Mason593060d2008-03-25 16:50:33 -040032struct map_lookup {
33 u64 type;
34 int io_align;
35 int io_width;
36 int stripe_len;
37 int sector_size;
38 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040039 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040040 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040041};
42
Yan Zheng2b820322008-11-17 21:11:30 -050043static int init_first_rw_device(struct btrfs_trans_handle *trans,
44 struct btrfs_root *root,
45 struct btrfs_device *device);
46static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
47
48
Chris Mason593060d2008-03-25 16:50:33 -040049#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040050 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040051
Chris Mason8a4b83c2008-03-24 15:02:07 -040052static DEFINE_MUTEX(uuid_mutex);
53static LIST_HEAD(fs_uuids);
54
Chris Masona061fc82008-05-07 11:43:44 -040055void btrfs_lock_volumes(void)
56{
57 mutex_lock(&uuid_mutex);
58}
59
60void btrfs_unlock_volumes(void)
61{
62 mutex_unlock(&uuid_mutex);
63}
64
Chris Mason7d9eb122008-07-08 14:19:17 -040065static void lock_chunks(struct btrfs_root *root)
66{
Chris Mason7d9eb122008-07-08 14:19:17 -040067 mutex_lock(&root->fs_info->chunk_mutex);
68}
69
70static void unlock_chunks(struct btrfs_root *root)
71{
Chris Mason7d9eb122008-07-08 14:19:17 -040072 mutex_unlock(&root->fs_info->chunk_mutex);
73}
74
Chris Mason8a4b83c2008-03-24 15:02:07 -040075int btrfs_cleanup_fs_uuids(void)
76{
77 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040078 struct btrfs_device *dev;
79
Yan Zheng2b820322008-11-17 21:11:30 -050080 while (!list_empty(&fs_uuids)) {
81 fs_devices = list_entry(fs_uuids.next,
82 struct btrfs_fs_devices, list);
83 list_del(&fs_devices->list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040084 while(!list_empty(&fs_devices->devices)) {
Yan Zheng2b820322008-11-17 21:11:30 -050085 dev = list_entry(fs_devices->devices.next,
86 struct btrfs_device, dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040087 if (dev->bdev) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040088 close_bdev_excl(dev->bdev);
Chris Masona0af4692008-05-13 16:03:06 -040089 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 }
Yan Zheng2b820322008-11-17 21:11:30 -050091 fs_devices->num_devices--;
92 if (dev->writeable)
93 fs_devices->rw_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -040094 list_del(&dev->dev_list);
Yan Zheng2b820322008-11-17 21:11:30 -050095 list_del(&dev->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -040096 kfree(dev->name);
Chris Mason8a4b83c2008-03-24 15:02:07 -040097 kfree(dev);
98 }
Yan Zheng2b820322008-11-17 21:11:30 -050099 WARN_ON(fs_devices->num_devices);
100 WARN_ON(fs_devices->open_devices);
101 WARN_ON(fs_devices->rw_devices);
102 kfree(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400103 }
104 return 0;
105}
106
Chris Masona1b32a52008-09-05 16:09:51 -0400107static noinline struct btrfs_device *__find_device(struct list_head *head,
108 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400109{
110 struct btrfs_device *dev;
111 struct list_head *cur;
112
113 list_for_each(cur, head) {
114 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masona4437552008-04-18 10:29:38 -0400115 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400116 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400117 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400118 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 }
120 return NULL;
121}
122
Chris Masona1b32a52008-09-05 16:09:51 -0400123static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400124{
125 struct list_head *cur;
126 struct btrfs_fs_devices *fs_devices;
127
128 list_for_each(cur, &fs_uuids) {
129 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
130 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
131 return fs_devices;
132 }
133 return NULL;
134}
135
Chris Mason8b712842008-06-11 16:50:36 -0400136/*
137 * we try to collect pending bios for a device so we don't get a large
138 * number of procs sending bios down to the same device. This greatly
139 * improves the schedulers ability to collect and merge the bios.
140 *
141 * But, it also turns into a long list of bios to process and that is sure
142 * to eventually make the worker thread block. The solution here is to
143 * make some progress and then put this work struct back at the end of
144 * the list if the block device is congested. This way, multiple devices
145 * can make progress from a single worker thread.
146 */
Chris Masona1b32a52008-09-05 16:09:51 -0400147static int noinline run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400148{
149 struct bio *pending;
150 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400151 struct btrfs_fs_info *fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400152 struct bio *tail;
153 struct bio *cur;
154 int again = 0;
155 unsigned long num_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400156 unsigned long limit;
Chris Mason8b712842008-06-11 16:50:36 -0400157
158 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
Chris Masonb64a2852008-08-20 13:39:41 -0400159 fs_info = device->dev_root->fs_info;
160 limit = btrfs_async_submit_limit(fs_info);
161 limit = limit * 2 / 3;
162
Chris Mason8b712842008-06-11 16:50:36 -0400163loop:
164 spin_lock(&device->io_lock);
165
166 /* take all the bios off the list at once and process them
167 * later on (without the lock held). But, remember the
168 * tail and other pointers so the bios can be properly reinserted
169 * into the list if we hit congestion
170 */
171 pending = device->pending_bios;
172 tail = device->pending_bio_tail;
173 WARN_ON(pending && !tail);
174 device->pending_bios = NULL;
175 device->pending_bio_tail = NULL;
176
177 /*
178 * if pending was null this time around, no bios need processing
179 * at all and we can stop. Otherwise it'll loop back up again
180 * and do an additional check so no bios are missed.
181 *
182 * device->running_pending is used to synchronize with the
183 * schedule_bio code.
184 */
185 if (pending) {
186 again = 1;
187 device->running_pending = 1;
188 } else {
189 again = 0;
190 device->running_pending = 0;
191 }
192 spin_unlock(&device->io_lock);
193
194 while(pending) {
195 cur = pending;
196 pending = pending->bi_next;
197 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400198 atomic_dec(&fs_info->nr_async_bios);
199
200 if (atomic_read(&fs_info->nr_async_bios) < limit &&
201 waitqueue_active(&fs_info->async_submit_wait))
202 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400203
204 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
205 bio_get(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400206 submit_bio(cur->bi_rw, cur);
Chris Mason492bb6de2008-07-31 16:29:02 -0400207 bio_put(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400208 num_run++;
209
210 /*
211 * we made progress, there is more work to do and the bdi
212 * is now congested. Back off and let other work structs
213 * run instead
214 */
Chris Mason5f2cc082008-11-07 18:22:45 -0500215 if (pending && bdi_write_congested(bdi) &&
216 fs_info->fs_devices->open_devices > 1) {
Chris Mason8b712842008-06-11 16:50:36 -0400217 struct bio *old_head;
218
219 spin_lock(&device->io_lock);
Chris Mason492bb6de2008-07-31 16:29:02 -0400220
Chris Mason8b712842008-06-11 16:50:36 -0400221 old_head = device->pending_bios;
222 device->pending_bios = pending;
223 if (device->pending_bio_tail)
224 tail->bi_next = old_head;
225 else
226 device->pending_bio_tail = tail;
227
228 spin_unlock(&device->io_lock);
229 btrfs_requeue_work(&device->work);
230 goto done;
231 }
232 }
233 if (again)
234 goto loop;
235done:
236 return 0;
237}
238
239void pending_bios_fn(struct btrfs_work *work)
240{
241 struct btrfs_device *device;
242
243 device = container_of(work, struct btrfs_device, work);
244 run_scheduled_bios(device);
245}
246
Chris Masona1b32a52008-09-05 16:09:51 -0400247static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400248 struct btrfs_super_block *disk_super,
249 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
250{
251 struct btrfs_device *device;
252 struct btrfs_fs_devices *fs_devices;
253 u64 found_transid = btrfs_super_generation(disk_super);
254
255 fs_devices = find_fsid(disk_super->fsid);
256 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400257 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400258 if (!fs_devices)
259 return -ENOMEM;
260 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400261 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400262 list_add(&fs_devices->list, &fs_uuids);
263 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
264 fs_devices->latest_devid = devid;
265 fs_devices->latest_trans = found_transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400266 device = NULL;
267 } else {
Chris Masona4437552008-04-18 10:29:38 -0400268 device = __find_device(&fs_devices->devices, devid,
269 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400270 }
271 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500272 if (fs_devices->opened)
273 return -EBUSY;
274
Chris Mason8a4b83c2008-03-24 15:02:07 -0400275 device = kzalloc(sizeof(*device), GFP_NOFS);
276 if (!device) {
277 /* we can safely leave the fs_devices entry around */
278 return -ENOMEM;
279 }
280 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400281 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400282 memcpy(device->uuid, disk_super->dev_item.uuid,
283 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400284 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400285 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400286 device->name = kstrdup(path, GFP_NOFS);
287 if (!device->name) {
288 kfree(device);
289 return -ENOMEM;
290 }
Yan Zheng2b820322008-11-17 21:11:30 -0500291 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400292 list_add(&device->dev_list, &fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500293 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400294 fs_devices->num_devices++;
295 }
296
297 if (found_transid > fs_devices->latest_trans) {
298 fs_devices->latest_devid = devid;
299 fs_devices->latest_trans = found_transid;
300 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400301 *fs_devices_ret = fs_devices;
302 return 0;
303}
304
Chris Masondfe25022008-05-13 13:46:40 -0400305int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
306{
Yan Zheng2b820322008-11-17 21:11:30 -0500307 struct list_head *tmp;
Chris Masondfe25022008-05-13 13:46:40 -0400308 struct list_head *cur;
309 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500310 int seed_devices = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400311
312 mutex_lock(&uuid_mutex);
313again:
Yan Zheng2b820322008-11-17 21:11:30 -0500314 list_for_each_safe(cur, tmp, &fs_devices->devices) {
Chris Masondfe25022008-05-13 13:46:40 -0400315 device = list_entry(cur, struct btrfs_device, dev_list);
Yan Zheng2b820322008-11-17 21:11:30 -0500316 if (device->in_fs_metadata)
317 continue;
318
319 if (device->bdev) {
320 close_bdev_excl(device->bdev);
321 device->bdev = NULL;
322 fs_devices->open_devices--;
323 }
324 if (device->writeable) {
325 list_del_init(&device->dev_alloc_list);
326 device->writeable = 0;
327 fs_devices->rw_devices--;
328 }
329 if (!seed_devices) {
330 list_del_init(&device->dev_list);
Chris Masondfe25022008-05-13 13:46:40 -0400331 fs_devices->num_devices--;
332 kfree(device->name);
333 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400334 }
335 }
Yan Zheng2b820322008-11-17 21:11:30 -0500336
337 if (fs_devices->seed) {
338 fs_devices = fs_devices->seed;
339 seed_devices = 1;
340 goto again;
341 }
342
Chris Masondfe25022008-05-13 13:46:40 -0400343 mutex_unlock(&uuid_mutex);
344 return 0;
345}
Chris Masona0af4692008-05-13 16:03:06 -0400346
Yan Zheng2b820322008-11-17 21:11:30 -0500347static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400348{
Yan Zheng2b820322008-11-17 21:11:30 -0500349 struct btrfs_fs_devices *seed_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400350 struct list_head *cur;
351 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500352again:
353 if (--fs_devices->opened > 0)
354 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355
Yan Zheng2b820322008-11-17 21:11:30 -0500356 list_for_each(cur, &fs_devices->devices) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400357 device = list_entry(cur, struct btrfs_device, dev_list);
358 if (device->bdev) {
359 close_bdev_excl(device->bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400360 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400361 }
Yan Zheng2b820322008-11-17 21:11:30 -0500362 if (device->writeable) {
363 list_del_init(&device->dev_alloc_list);
364 fs_devices->rw_devices--;
365 }
366
Chris Mason8a4b83c2008-03-24 15:02:07 -0400367 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500368 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400369 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 }
Yan Zheng2b820322008-11-17 21:11:30 -0500371 fs_devices->opened = 0;
372 fs_devices->seeding = 0;
373 fs_devices->sprouted = 0;
374
375 seed_devices = fs_devices->seed;
376 fs_devices->seed = NULL;
377 if (seed_devices) {
378 fs_devices = seed_devices;
379 goto again;
380 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400381 return 0;
382}
383
Yan Zheng2b820322008-11-17 21:11:30 -0500384int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
385{
386 int ret;
387
388 mutex_lock(&uuid_mutex);
389 ret = __btrfs_close_devices(fs_devices);
390 mutex_unlock(&uuid_mutex);
391 return ret;
392}
393
394int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400395{
396 struct block_device *bdev;
397 struct list_head *head = &fs_devices->devices;
398 struct list_head *cur;
399 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400400 struct block_device *latest_bdev = NULL;
401 struct buffer_head *bh;
402 struct btrfs_super_block *disk_super;
403 u64 latest_devid = 0;
404 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400405 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500406 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400407 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400408
Chris Mason8a4b83c2008-03-24 15:02:07 -0400409 list_for_each(cur, head) {
410 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Masonc1c4d912008-05-08 15:05:58 -0400411 if (device->bdev)
412 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400413 if (!device->name)
414 continue;
415
Yan Zheng2b820322008-11-17 21:11:30 -0500416 bdev = open_bdev_excl(device->name, MS_RDONLY, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400417 if (IS_ERR(bdev)) {
418 printk("open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400419 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400420 }
Chris Masona061fc82008-05-07 11:43:44 -0400421 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400422
423 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
424 if (!bh)
425 goto error_close;
426
427 disk_super = (struct btrfs_super_block *)bh->b_data;
428 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
429 sizeof(disk_super->magic)))
430 goto error_brelse;
431
432 devid = le64_to_cpu(disk_super->dev_item.devid);
433 if (devid != device->devid)
434 goto error_brelse;
435
Yan Zheng2b820322008-11-17 21:11:30 -0500436 if (memcmp(device->uuid, disk_super->dev_item.uuid,
437 BTRFS_UUID_SIZE))
438 goto error_brelse;
439
440 device->generation = btrfs_super_generation(disk_super);
441 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400442 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500443 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400444 latest_bdev = bdev;
445 }
446
Yan Zheng2b820322008-11-17 21:11:30 -0500447 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
448 device->writeable = 0;
449 } else {
450 device->writeable = !bdev_read_only(bdev);
451 seeding = 0;
452 }
453
Chris Mason8a4b83c2008-03-24 15:02:07 -0400454 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400455 device->in_fs_metadata = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400456 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500457 if (device->writeable) {
458 fs_devices->rw_devices++;
459 list_add(&device->dev_alloc_list,
460 &fs_devices->alloc_list);
461 }
Chris Masona0af4692008-05-13 16:03:06 -0400462 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400463
Chris Masona0af4692008-05-13 16:03:06 -0400464error_brelse:
465 brelse(bh);
466error_close:
467 close_bdev_excl(bdev);
468error:
469 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400470 }
Chris Masona0af4692008-05-13 16:03:06 -0400471 if (fs_devices->open_devices == 0) {
472 ret = -EIO;
473 goto out;
474 }
Yan Zheng2b820322008-11-17 21:11:30 -0500475 fs_devices->seeding = seeding;
476 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400477 fs_devices->latest_bdev = latest_bdev;
478 fs_devices->latest_devid = latest_devid;
479 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500480 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400481out:
Yan Zheng2b820322008-11-17 21:11:30 -0500482 return ret;
483}
484
485int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
486 int flags, void *holder)
487{
488 int ret;
489
490 mutex_lock(&uuid_mutex);
491 if (fs_devices->opened) {
492 if (fs_devices->sprouted) {
493 ret = -EBUSY;
494 } else {
495 fs_devices->opened++;
496 ret = 0;
497 }
498 } else {
499 ret = __btrfs_open_devices(fs_devices, holder);
500 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400501 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400502 return ret;
503}
504
505int btrfs_scan_one_device(const char *path, int flags, void *holder,
506 struct btrfs_fs_devices **fs_devices_ret)
507{
508 struct btrfs_super_block *disk_super;
509 struct block_device *bdev;
510 struct buffer_head *bh;
511 int ret;
512 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400513 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400514
515 mutex_lock(&uuid_mutex);
516
Chris Mason8a4b83c2008-03-24 15:02:07 -0400517 bdev = open_bdev_excl(path, flags, holder);
518
519 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400520 ret = PTR_ERR(bdev);
521 goto error;
522 }
523
524 ret = set_blocksize(bdev, 4096);
525 if (ret)
526 goto error_close;
527 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
528 if (!bh) {
529 ret = -EIO;
530 goto error_close;
531 }
532 disk_super = (struct btrfs_super_block *)bh->b_data;
533 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
534 sizeof(disk_super->magic))) {
Yane58ca022008-04-01 11:21:34 -0400535 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400536 goto error_brelse;
537 }
538 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400539 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400540 if (disk_super->label[0])
541 printk("device label %s ", disk_super->label);
542 else {
543 /* FIXME, make a readl uuid parser */
544 printk("device fsid %llx-%llx ",
545 *(unsigned long long *)disk_super->fsid,
546 *(unsigned long long *)(disk_super->fsid + 8));
547 }
548 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
550
551error_brelse:
552 brelse(bh);
553error_close:
554 close_bdev_excl(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400555error:
556 mutex_unlock(&uuid_mutex);
557 return ret;
558}
Chris Mason0b86a832008-03-24 15:01:56 -0400559
560/*
561 * this uses a pretty simple search, the expectation is that it is
562 * called very infrequently and that a given device has a small number
563 * of extents
564 */
Chris Masona1b32a52008-09-05 16:09:51 -0400565static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
566 struct btrfs_device *device,
Chris Masona1b32a52008-09-05 16:09:51 -0400567 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400568{
569 struct btrfs_key key;
570 struct btrfs_root *root = device->dev_root;
571 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500572 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400573 u64 hole_size = 0;
574 u64 last_byte = 0;
575 u64 search_start = 0;
576 u64 search_end = device->total_bytes;
577 int ret;
578 int slot = 0;
579 int start_found;
580 struct extent_buffer *l;
581
Yan Zheng2b820322008-11-17 21:11:30 -0500582 path = btrfs_alloc_path();
583 if (!path)
584 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400585 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500586 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400587
588 /* FIXME use last free of some kind */
589
Chris Mason8a4b83c2008-03-24 15:02:07 -0400590 /* we don't want to overwrite the superblock on the drive,
591 * so we make sure to start at an offset of at least 1MB
592 */
593 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400594
595 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
596 search_start = max(root->fs_info->alloc_start, search_start);
597
Chris Mason0b86a832008-03-24 15:01:56 -0400598 key.objectid = device->devid;
599 key.offset = search_start;
600 key.type = BTRFS_DEV_EXTENT_KEY;
601 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
602 if (ret < 0)
603 goto error;
604 ret = btrfs_previous_item(root, path, 0, key.type);
605 if (ret < 0)
606 goto error;
607 l = path->nodes[0];
608 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
609 while (1) {
610 l = path->nodes[0];
611 slot = path->slots[0];
612 if (slot >= btrfs_header_nritems(l)) {
613 ret = btrfs_next_leaf(root, path);
614 if (ret == 0)
615 continue;
616 if (ret < 0)
617 goto error;
618no_more_items:
619 if (!start_found) {
620 if (search_start >= search_end) {
621 ret = -ENOSPC;
622 goto error;
623 }
624 *start = search_start;
625 start_found = 1;
626 goto check_pending;
627 }
628 *start = last_byte > search_start ?
629 last_byte : search_start;
630 if (search_end <= *start) {
631 ret = -ENOSPC;
632 goto error;
633 }
634 goto check_pending;
635 }
636 btrfs_item_key_to_cpu(l, &key, slot);
637
638 if (key.objectid < device->devid)
639 goto next;
640
641 if (key.objectid > device->devid)
642 goto no_more_items;
643
644 if (key.offset >= search_start && key.offset > last_byte &&
645 start_found) {
646 if (last_byte < search_start)
647 last_byte = search_start;
648 hole_size = key.offset - last_byte;
649 if (key.offset > last_byte &&
650 hole_size >= num_bytes) {
651 *start = last_byte;
652 goto check_pending;
653 }
654 }
655 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
656 goto next;
657 }
658
659 start_found = 1;
660 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
661 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
662next:
663 path->slots[0]++;
664 cond_resched();
665 }
666check_pending:
667 /* we have to make sure we didn't find an extent that has already
668 * been allocated by the map tree or the original allocation
669 */
Chris Mason0b86a832008-03-24 15:01:56 -0400670 BUG_ON(*start < search_start);
671
Chris Mason6324fbf2008-03-24 15:01:59 -0400672 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400673 ret = -ENOSPC;
674 goto error;
675 }
676 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500677 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400678
679error:
Yan Zheng2b820322008-11-17 21:11:30 -0500680 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400681 return ret;
682}
683
Chris Mason8f18cf12008-04-25 16:53:30 -0400684int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
685 struct btrfs_device *device,
686 u64 start)
687{
688 int ret;
689 struct btrfs_path *path;
690 struct btrfs_root *root = device->dev_root;
691 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400692 struct btrfs_key found_key;
693 struct extent_buffer *leaf = NULL;
694 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400695
696 path = btrfs_alloc_path();
697 if (!path)
698 return -ENOMEM;
699
700 key.objectid = device->devid;
701 key.offset = start;
702 key.type = BTRFS_DEV_EXTENT_KEY;
703
704 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400705 if (ret > 0) {
706 ret = btrfs_previous_item(root, path, key.objectid,
707 BTRFS_DEV_EXTENT_KEY);
708 BUG_ON(ret);
709 leaf = path->nodes[0];
710 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
711 extent = btrfs_item_ptr(leaf, path->slots[0],
712 struct btrfs_dev_extent);
713 BUG_ON(found_key.offset > start || found_key.offset +
714 btrfs_dev_extent_length(leaf, extent) < start);
715 ret = 0;
716 } else if (ret == 0) {
717 leaf = path->nodes[0];
718 extent = btrfs_item_ptr(leaf, path->slots[0],
719 struct btrfs_dev_extent);
720 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400721 BUG_ON(ret);
722
Chris Masondfe25022008-05-13 13:46:40 -0400723 if (device->bytes_used > 0)
724 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400725 ret = btrfs_del_item(trans, root, path);
726 BUG_ON(ret);
727
728 btrfs_free_path(path);
729 return ret;
730}
731
Yan Zheng2b820322008-11-17 21:11:30 -0500732int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400733 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400734 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500735 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400736{
737 int ret;
738 struct btrfs_path *path;
739 struct btrfs_root *root = device->dev_root;
740 struct btrfs_dev_extent *extent;
741 struct extent_buffer *leaf;
742 struct btrfs_key key;
743
Chris Masondfe25022008-05-13 13:46:40 -0400744 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400745 path = btrfs_alloc_path();
746 if (!path)
747 return -ENOMEM;
748
Chris Mason0b86a832008-03-24 15:01:56 -0400749 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500750 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400751 key.type = BTRFS_DEV_EXTENT_KEY;
752 ret = btrfs_insert_empty_item(trans, root, path, &key,
753 sizeof(*extent));
754 BUG_ON(ret);
755
756 leaf = path->nodes[0];
757 extent = btrfs_item_ptr(leaf, path->slots[0],
758 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400759 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
760 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
761 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
762
763 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
764 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
765 BTRFS_UUID_SIZE);
766
Chris Mason0b86a832008-03-24 15:01:56 -0400767 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
768 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400769 btrfs_free_path(path);
770 return ret;
771}
772
Chris Masona1b32a52008-09-05 16:09:51 -0400773static noinline int find_next_chunk(struct btrfs_root *root,
774 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400775{
776 struct btrfs_path *path;
777 int ret;
778 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400779 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400780 struct btrfs_key found_key;
781
782 path = btrfs_alloc_path();
783 BUG_ON(!path);
784
Chris Masone17cade2008-04-15 15:41:47 -0400785 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400786 key.offset = (u64)-1;
787 key.type = BTRFS_CHUNK_ITEM_KEY;
788
789 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
790 if (ret < 0)
791 goto error;
792
793 BUG_ON(ret == 0);
794
795 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
796 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400797 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400798 } else {
799 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
800 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400801 if (found_key.objectid != objectid)
802 *offset = 0;
803 else {
804 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
805 struct btrfs_chunk);
806 *offset = found_key.offset +
807 btrfs_chunk_length(path->nodes[0], chunk);
808 }
Chris Mason0b86a832008-03-24 15:01:56 -0400809 }
810 ret = 0;
811error:
812 btrfs_free_path(path);
813 return ret;
814}
815
Yan Zheng2b820322008-11-17 21:11:30 -0500816static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400817{
818 int ret;
819 struct btrfs_key key;
820 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500821 struct btrfs_path *path;
822
823 root = root->fs_info->chunk_root;
824
825 path = btrfs_alloc_path();
826 if (!path)
827 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400828
829 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
830 key.type = BTRFS_DEV_ITEM_KEY;
831 key.offset = (u64)-1;
832
833 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
834 if (ret < 0)
835 goto error;
836
837 BUG_ON(ret == 0);
838
839 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
840 BTRFS_DEV_ITEM_KEY);
841 if (ret) {
842 *objectid = 1;
843 } else {
844 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
845 path->slots[0]);
846 *objectid = found_key.offset + 1;
847 }
848 ret = 0;
849error:
Yan Zheng2b820322008-11-17 21:11:30 -0500850 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400851 return ret;
852}
853
854/*
855 * the device information is stored in the chunk root
856 * the btrfs_device struct should be fully filled in
857 */
858int btrfs_add_device(struct btrfs_trans_handle *trans,
859 struct btrfs_root *root,
860 struct btrfs_device *device)
861{
862 int ret;
863 struct btrfs_path *path;
864 struct btrfs_dev_item *dev_item;
865 struct extent_buffer *leaf;
866 struct btrfs_key key;
867 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -0400868
869 root = root->fs_info->chunk_root;
870
871 path = btrfs_alloc_path();
872 if (!path)
873 return -ENOMEM;
874
Chris Mason0b86a832008-03-24 15:01:56 -0400875 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
876 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -0500877 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400878
879 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400880 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400881 if (ret)
882 goto out;
883
884 leaf = path->nodes[0];
885 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
886
887 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -0500888 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400889 btrfs_set_device_type(leaf, dev_item, device->type);
890 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
891 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
892 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400893 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
894 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400895 btrfs_set_device_group(leaf, dev_item, 0);
896 btrfs_set_device_seek_speed(leaf, dev_item, 0);
897 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400898
Chris Mason0b86a832008-03-24 15:01:56 -0400899 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400900 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500901 ptr = (unsigned long)btrfs_device_fsid(dev_item);
902 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400903 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400904
Yan Zheng2b820322008-11-17 21:11:30 -0500905 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400906out:
907 btrfs_free_path(path);
908 return ret;
909}
Chris Mason8f18cf12008-04-25 16:53:30 -0400910
Chris Masona061fc82008-05-07 11:43:44 -0400911static int btrfs_rm_dev_item(struct btrfs_root *root,
912 struct btrfs_device *device)
913{
914 int ret;
915 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -0400916 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400917 struct btrfs_trans_handle *trans;
918
919 root = root->fs_info->chunk_root;
920
921 path = btrfs_alloc_path();
922 if (!path)
923 return -ENOMEM;
924
925 trans = btrfs_start_transaction(root, 1);
926 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
927 key.type = BTRFS_DEV_ITEM_KEY;
928 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400929 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400930
931 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
932 if (ret < 0)
933 goto out;
934
935 if (ret > 0) {
936 ret = -ENOENT;
937 goto out;
938 }
939
940 ret = btrfs_del_item(trans, root, path);
941 if (ret)
942 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400943out:
944 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -0400945 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400946 btrfs_commit_transaction(trans, root);
947 return ret;
948}
949
950int btrfs_rm_device(struct btrfs_root *root, char *device_path)
951{
952 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500953 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -0400954 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400955 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -0400956 struct btrfs_super_block *disk_super;
957 u64 all_avail;
958 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500959 u64 num_devices;
960 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -0400961 int ret = 0;
962
Chris Masona061fc82008-05-07 11:43:44 -0400963 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -0400964 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -0400965
966 all_avail = root->fs_info->avail_data_alloc_bits |
967 root->fs_info->avail_system_alloc_bits |
968 root->fs_info->avail_metadata_alloc_bits;
969
970 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -0500971 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masona061fc82008-05-07 11:43:44 -0400972 printk("btrfs: unable to go below four devices on raid10\n");
973 ret = -EINVAL;
974 goto out;
975 }
976
977 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -0500978 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masona061fc82008-05-07 11:43:44 -0400979 printk("btrfs: unable to go below two devices on raid1\n");
980 ret = -EINVAL;
981 goto out;
982 }
983
Chris Masondfe25022008-05-13 13:46:40 -0400984 if (strcmp(device_path, "missing") == 0) {
985 struct list_head *cur;
986 struct list_head *devices;
987 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -0400988
Chris Masondfe25022008-05-13 13:46:40 -0400989 device = NULL;
990 devices = &root->fs_info->fs_devices->devices;
991 list_for_each(cur, devices) {
992 tmp = list_entry(cur, struct btrfs_device, dev_list);
993 if (tmp->in_fs_metadata && !tmp->bdev) {
994 device = tmp;
995 break;
996 }
997 }
998 bdev = NULL;
999 bh = NULL;
1000 disk_super = NULL;
1001 if (!device) {
1002 printk("btrfs: no missing devices found to remove\n");
1003 goto out;
1004 }
Chris Masondfe25022008-05-13 13:46:40 -04001005 } else {
Yan Zheng2b820322008-11-17 21:11:30 -05001006 bdev = open_bdev_excl(device_path, MS_RDONLY,
Chris Masondfe25022008-05-13 13:46:40 -04001007 root->fs_info->bdev_holder);
1008 if (IS_ERR(bdev)) {
1009 ret = PTR_ERR(bdev);
1010 goto out;
1011 }
1012
Yan Zheng2b820322008-11-17 21:11:30 -05001013 set_blocksize(bdev, 4096);
Chris Masondfe25022008-05-13 13:46:40 -04001014 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
1015 if (!bh) {
1016 ret = -EIO;
1017 goto error_close;
1018 }
1019 disk_super = (struct btrfs_super_block *)bh->b_data;
1020 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
Yan Zheng2b820322008-11-17 21:11:30 -05001021 sizeof(disk_super->magic))) {
Chris Masondfe25022008-05-13 13:46:40 -04001022 ret = -ENOENT;
1023 goto error_brelse;
1024 }
1025 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001026 dev_uuid = disk_super->dev_item.uuid;
1027 device = btrfs_find_device(root, devid, dev_uuid,
1028 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001029 if (!device) {
1030 ret = -ENOENT;
1031 goto error_brelse;
1032 }
Chris Masondfe25022008-05-13 13:46:40 -04001033 }
Yan Zheng2b820322008-11-17 21:11:30 -05001034
1035 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1036 printk("btrfs: unable to remove the only writeable device\n");
1037 ret = -EINVAL;
1038 goto error_brelse;
1039 }
1040
1041 if (device->writeable) {
1042 list_del_init(&device->dev_alloc_list);
1043 root->fs_info->fs_devices->rw_devices--;
1044 }
Chris Masona061fc82008-05-07 11:43:44 -04001045
1046 ret = btrfs_shrink_device(device, 0);
1047 if (ret)
1048 goto error_brelse;
1049
Chris Masona061fc82008-05-07 11:43:44 -04001050 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1051 if (ret)
1052 goto error_brelse;
1053
Yan Zheng2b820322008-11-17 21:11:30 -05001054 device->in_fs_metadata = 0;
1055 if (device->fs_devices == root->fs_info->fs_devices) {
1056 list_del_init(&device->dev_list);
1057 root->fs_info->fs_devices->num_devices--;
1058 if (device->bdev)
1059 device->fs_devices->open_devices--;
1060 }
1061
1062 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1063 struct btrfs_device, dev_list);
1064 if (device->bdev == root->fs_info->sb->s_bdev)
1065 root->fs_info->sb->s_bdev = next_device->bdev;
1066 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1067 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1068
1069 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1070 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1071
1072 if (device->fs_devices != root->fs_info->fs_devices) {
1073 BUG_ON(device->writeable);
1074 brelse(bh);
1075 if (bdev)
1076 close_bdev_excl(bdev);
1077
1078 if (device->bdev) {
1079 close_bdev_excl(device->bdev);
1080 device->bdev = NULL;
1081 device->fs_devices->open_devices--;
1082 }
1083 if (device->fs_devices->open_devices == 0) {
1084 struct btrfs_fs_devices *fs_devices;
1085 fs_devices = root->fs_info->fs_devices;
1086 while (fs_devices) {
1087 if (fs_devices->seed == device->fs_devices)
1088 break;
1089 fs_devices = fs_devices->seed;
1090 }
1091 fs_devices->seed = device->fs_devices->seed;
1092 device->fs_devices->seed = NULL;
1093 __btrfs_close_devices(device->fs_devices);
1094 }
1095 ret = 0;
1096 goto out;
1097 }
1098
1099 /*
1100 * at this point, the device is zero sized. We want to
1101 * remove it from the devices list and zero out the old super
1102 */
1103 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001104 /* make sure this device isn't detected as part of
1105 * the FS anymore
1106 */
1107 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1108 set_buffer_dirty(bh);
1109 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001110 }
Yan Zheng2b820322008-11-17 21:11:30 -05001111 brelse(bh);
Chris Masona061fc82008-05-07 11:43:44 -04001112
Chris Masondfe25022008-05-13 13:46:40 -04001113 if (device->bdev) {
1114 /* one close for the device struct or super_block */
1115 close_bdev_excl(device->bdev);
1116 }
1117 if (bdev) {
1118 /* one close for us */
1119 close_bdev_excl(bdev);
1120 }
Chris Masona061fc82008-05-07 11:43:44 -04001121 kfree(device->name);
1122 kfree(device);
1123 ret = 0;
1124 goto out;
1125
1126error_brelse:
1127 brelse(bh);
1128error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001129 if (bdev)
1130 close_bdev_excl(bdev);
Chris Masona061fc82008-05-07 11:43:44 -04001131out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001132 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001133 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001134 return ret;
1135}
1136
Yan Zheng2b820322008-11-17 21:11:30 -05001137/*
1138 * does all the dirty work required for changing file system's UUID.
1139 */
1140static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *root)
1142{
1143 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1144 struct btrfs_fs_devices *old_devices;
1145 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1146 struct btrfs_device *device;
1147 u64 super_flags;
1148
1149 BUG_ON(!mutex_is_locked(&uuid_mutex));
1150 if (!fs_devices->seeding || fs_devices->opened != 1)
1151 return -EINVAL;
1152
1153 old_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1154 if (!old_devices)
1155 return -ENOMEM;
1156
1157 memcpy(old_devices, fs_devices, sizeof(*old_devices));
1158 old_devices->opened = 1;
1159 old_devices->sprouted = 1;
1160 INIT_LIST_HEAD(&old_devices->devices);
1161 INIT_LIST_HEAD(&old_devices->alloc_list);
1162 list_splice_init(&fs_devices->devices, &old_devices->devices);
1163 list_splice_init(&fs_devices->alloc_list, &old_devices->alloc_list);
1164 list_for_each_entry(device, &old_devices->devices, dev_list) {
1165 device->fs_devices = old_devices;
1166 }
1167 list_add(&old_devices->list, &fs_uuids);
1168
1169 fs_devices->seeding = 0;
1170 fs_devices->num_devices = 0;
1171 fs_devices->open_devices = 0;
1172 fs_devices->seed = old_devices;
1173
1174 generate_random_uuid(fs_devices->fsid);
1175 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1176 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1177 super_flags = btrfs_super_flags(disk_super) &
1178 ~BTRFS_SUPER_FLAG_SEEDING;
1179 btrfs_set_super_flags(disk_super, super_flags);
1180
1181 return 0;
1182}
1183
1184/*
1185 * strore the expected generation for seed devices in device items.
1186 */
1187static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1188 struct btrfs_root *root)
1189{
1190 struct btrfs_path *path;
1191 struct extent_buffer *leaf;
1192 struct btrfs_dev_item *dev_item;
1193 struct btrfs_device *device;
1194 struct btrfs_key key;
1195 u8 fs_uuid[BTRFS_UUID_SIZE];
1196 u8 dev_uuid[BTRFS_UUID_SIZE];
1197 u64 devid;
1198 int ret;
1199
1200 path = btrfs_alloc_path();
1201 if (!path)
1202 return -ENOMEM;
1203
1204 root = root->fs_info->chunk_root;
1205 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1206 key.offset = 0;
1207 key.type = BTRFS_DEV_ITEM_KEY;
1208
1209 while (1) {
1210 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1211 if (ret < 0)
1212 goto error;
1213
1214 leaf = path->nodes[0];
1215next_slot:
1216 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1217 ret = btrfs_next_leaf(root, path);
1218 if (ret > 0)
1219 break;
1220 if (ret < 0)
1221 goto error;
1222 leaf = path->nodes[0];
1223 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1224 btrfs_release_path(root, path);
1225 continue;
1226 }
1227
1228 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1229 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1230 key.type != BTRFS_DEV_ITEM_KEY)
1231 break;
1232
1233 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1234 struct btrfs_dev_item);
1235 devid = btrfs_device_id(leaf, dev_item);
1236 read_extent_buffer(leaf, dev_uuid,
1237 (unsigned long)btrfs_device_uuid(dev_item),
1238 BTRFS_UUID_SIZE);
1239 read_extent_buffer(leaf, fs_uuid,
1240 (unsigned long)btrfs_device_fsid(dev_item),
1241 BTRFS_UUID_SIZE);
1242 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1243 BUG_ON(!device);
1244
1245 if (device->fs_devices->seeding) {
1246 btrfs_set_device_generation(leaf, dev_item,
1247 device->generation);
1248 btrfs_mark_buffer_dirty(leaf);
1249 }
1250
1251 path->slots[0]++;
1252 goto next_slot;
1253 }
1254 ret = 0;
1255error:
1256 btrfs_free_path(path);
1257 return ret;
1258}
1259
Chris Mason788f20e2008-04-28 15:29:42 -04001260int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1261{
1262 struct btrfs_trans_handle *trans;
1263 struct btrfs_device *device;
1264 struct block_device *bdev;
1265 struct list_head *cur;
1266 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001267 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001268 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001269 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001270 int ret = 0;
1271
Yan Zheng2b820322008-11-17 21:11:30 -05001272 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1273 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001274
1275 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
1276 if (!bdev) {
1277 return -EIO;
1278 }
Chris Masona2135012008-06-25 16:01:30 -04001279
Yan Zheng2b820322008-11-17 21:11:30 -05001280 if (root->fs_info->fs_devices->seeding) {
1281 seeding_dev = 1;
1282 down_write(&sb->s_umount);
1283 mutex_lock(&uuid_mutex);
1284 }
1285
Chris Mason8c8bee12008-09-29 11:19:10 -04001286 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001287 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001288
Chris Mason788f20e2008-04-28 15:29:42 -04001289 devices = &root->fs_info->fs_devices->devices;
1290 list_for_each(cur, devices) {
1291 device = list_entry(cur, struct btrfs_device, dev_list);
1292 if (device->bdev == bdev) {
1293 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001294 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001295 }
1296 }
1297
1298 device = kzalloc(sizeof(*device), GFP_NOFS);
1299 if (!device) {
1300 /* we can safely leave the fs_devices entry around */
1301 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001302 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001303 }
1304
Chris Mason788f20e2008-04-28 15:29:42 -04001305 device->name = kstrdup(device_path, GFP_NOFS);
1306 if (!device->name) {
1307 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001308 ret = -ENOMEM;
1309 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001310 }
Yan Zheng2b820322008-11-17 21:11:30 -05001311
1312 ret = find_next_devid(root, &device->devid);
1313 if (ret) {
1314 kfree(device);
1315 goto error;
1316 }
1317
1318 trans = btrfs_start_transaction(root, 1);
1319 lock_chunks(root);
1320
1321 device->barriers = 1;
1322 device->writeable = 1;
1323 device->work.func = pending_bios_fn;
1324 generate_random_uuid(device->uuid);
1325 spin_lock_init(&device->io_lock);
1326 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001327 device->io_width = root->sectorsize;
1328 device->io_align = root->sectorsize;
1329 device->sector_size = root->sectorsize;
1330 device->total_bytes = i_size_read(bdev->bd_inode);
1331 device->dev_root = root->fs_info->dev_root;
1332 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001333 device->in_fs_metadata = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001334 set_blocksize(device->bdev, 4096);
1335
Yan Zheng2b820322008-11-17 21:11:30 -05001336 if (seeding_dev) {
1337 sb->s_flags &= ~MS_RDONLY;
1338 ret = btrfs_prepare_sprout(trans, root);
1339 BUG_ON(ret);
1340 }
1341
1342 device->fs_devices = root->fs_info->fs_devices;
1343 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1344 list_add(&device->dev_alloc_list,
1345 &root->fs_info->fs_devices->alloc_list);
1346 root->fs_info->fs_devices->num_devices++;
1347 root->fs_info->fs_devices->open_devices++;
1348 root->fs_info->fs_devices->rw_devices++;
1349 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1350
Chris Mason788f20e2008-04-28 15:29:42 -04001351 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1352 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1353 total_bytes + device->total_bytes);
1354
1355 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1356 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1357 total_bytes + 1);
1358
Yan Zheng2b820322008-11-17 21:11:30 -05001359 if (seeding_dev) {
1360 ret = init_first_rw_device(trans, root, device);
1361 BUG_ON(ret);
1362 ret = btrfs_finish_sprout(trans, root);
1363 BUG_ON(ret);
1364 } else {
1365 ret = btrfs_add_device(trans, root, device);
1366 }
1367
Chris Mason7d9eb122008-07-08 14:19:17 -04001368 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001369 btrfs_commit_transaction(trans, root);
1370
1371 if (seeding_dev) {
1372 mutex_unlock(&uuid_mutex);
1373 up_write(&sb->s_umount);
1374
1375 ret = btrfs_relocate_sys_chunks(root);
1376 BUG_ON(ret);
1377 }
1378out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001379 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001380 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001381error:
Chris Mason788f20e2008-04-28 15:29:42 -04001382 close_bdev_excl(bdev);
Yan Zheng2b820322008-11-17 21:11:30 -05001383 if (seeding_dev) {
1384 mutex_unlock(&uuid_mutex);
1385 up_write(&sb->s_umount);
1386 }
Chris Mason788f20e2008-04-28 15:29:42 -04001387 goto out;
1388}
1389
Chris Masona1b32a52008-09-05 16:09:51 -04001390int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
1391 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001392{
1393 int ret;
1394 struct btrfs_path *path;
1395 struct btrfs_root *root;
1396 struct btrfs_dev_item *dev_item;
1397 struct extent_buffer *leaf;
1398 struct btrfs_key key;
1399
1400 root = device->dev_root->fs_info->chunk_root;
1401
1402 path = btrfs_alloc_path();
1403 if (!path)
1404 return -ENOMEM;
1405
1406 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1407 key.type = BTRFS_DEV_ITEM_KEY;
1408 key.offset = device->devid;
1409
1410 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1411 if (ret < 0)
1412 goto out;
1413
1414 if (ret > 0) {
1415 ret = -ENOENT;
1416 goto out;
1417 }
1418
1419 leaf = path->nodes[0];
1420 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1421
1422 btrfs_set_device_id(leaf, dev_item, device->devid);
1423 btrfs_set_device_type(leaf, dev_item, device->type);
1424 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1425 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1426 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001427 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1428 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1429 btrfs_mark_buffer_dirty(leaf);
1430
1431out:
1432 btrfs_free_path(path);
1433 return ret;
1434}
1435
Chris Mason7d9eb122008-07-08 14:19:17 -04001436static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001437 struct btrfs_device *device, u64 new_size)
1438{
1439 struct btrfs_super_block *super_copy =
1440 &device->dev_root->fs_info->super_copy;
1441 u64 old_total = btrfs_super_total_bytes(super_copy);
1442 u64 diff = new_size - device->total_bytes;
1443
Yan Zheng2b820322008-11-17 21:11:30 -05001444 if (!device->writeable)
1445 return -EACCES;
1446 if (new_size <= device->total_bytes)
1447 return -EINVAL;
1448
Chris Mason8f18cf12008-04-25 16:53:30 -04001449 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001450 device->fs_devices->total_rw_bytes += diff;
1451
1452 device->total_bytes = new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04001453 return btrfs_update_device(trans, device);
1454}
1455
Chris Mason7d9eb122008-07-08 14:19:17 -04001456int btrfs_grow_device(struct btrfs_trans_handle *trans,
1457 struct btrfs_device *device, u64 new_size)
1458{
1459 int ret;
1460 lock_chunks(device->dev_root);
1461 ret = __btrfs_grow_device(trans, device, new_size);
1462 unlock_chunks(device->dev_root);
1463 return ret;
1464}
1465
Chris Mason8f18cf12008-04-25 16:53:30 -04001466static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1467 struct btrfs_root *root,
1468 u64 chunk_tree, u64 chunk_objectid,
1469 u64 chunk_offset)
1470{
1471 int ret;
1472 struct btrfs_path *path;
1473 struct btrfs_key key;
1474
1475 root = root->fs_info->chunk_root;
1476 path = btrfs_alloc_path();
1477 if (!path)
1478 return -ENOMEM;
1479
1480 key.objectid = chunk_objectid;
1481 key.offset = chunk_offset;
1482 key.type = BTRFS_CHUNK_ITEM_KEY;
1483
1484 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1485 BUG_ON(ret);
1486
1487 ret = btrfs_del_item(trans, root, path);
1488 BUG_ON(ret);
1489
1490 btrfs_free_path(path);
1491 return 0;
1492}
1493
1494int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1495 chunk_offset)
1496{
1497 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1498 struct btrfs_disk_key *disk_key;
1499 struct btrfs_chunk *chunk;
1500 u8 *ptr;
1501 int ret = 0;
1502 u32 num_stripes;
1503 u32 array_size;
1504 u32 len = 0;
1505 u32 cur;
1506 struct btrfs_key key;
1507
1508 array_size = btrfs_super_sys_array_size(super_copy);
1509
1510 ptr = super_copy->sys_chunk_array;
1511 cur = 0;
1512
1513 while (cur < array_size) {
1514 disk_key = (struct btrfs_disk_key *)ptr;
1515 btrfs_disk_key_to_cpu(&key, disk_key);
1516
1517 len = sizeof(*disk_key);
1518
1519 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1520 chunk = (struct btrfs_chunk *)(ptr + len);
1521 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1522 len += btrfs_chunk_item_size(num_stripes);
1523 } else {
1524 ret = -EIO;
1525 break;
1526 }
1527 if (key.objectid == chunk_objectid &&
1528 key.offset == chunk_offset) {
1529 memmove(ptr, ptr + len, array_size - (cur + len));
1530 array_size -= len;
1531 btrfs_set_super_sys_array_size(super_copy, array_size);
1532 } else {
1533 ptr += len;
1534 cur += len;
1535 }
1536 }
1537 return ret;
1538}
1539
Chris Mason8f18cf12008-04-25 16:53:30 -04001540int btrfs_relocate_chunk(struct btrfs_root *root,
1541 u64 chunk_tree, u64 chunk_objectid,
1542 u64 chunk_offset)
1543{
1544 struct extent_map_tree *em_tree;
1545 struct btrfs_root *extent_root;
1546 struct btrfs_trans_handle *trans;
1547 struct extent_map *em;
1548 struct map_lookup *map;
1549 int ret;
1550 int i;
1551
Chris Mason323da792008-05-09 11:46:48 -04001552 printk("btrfs relocating chunk %llu\n",
1553 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001554 root = root->fs_info->chunk_root;
1555 extent_root = root->fs_info->extent_root;
1556 em_tree = &root->fs_info->mapping_tree.map_tree;
1557
1558 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001559 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001560 BUG_ON(ret);
1561
1562 trans = btrfs_start_transaction(root, 1);
1563 BUG_ON(!trans);
1564
Chris Mason7d9eb122008-07-08 14:19:17 -04001565 lock_chunks(root);
1566
Chris Mason8f18cf12008-04-25 16:53:30 -04001567 /*
1568 * step two, delete the device extents and the
1569 * chunk tree entries
1570 */
1571 spin_lock(&em_tree->lock);
1572 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1573 spin_unlock(&em_tree->lock);
1574
Chris Masona061fc82008-05-07 11:43:44 -04001575 BUG_ON(em->start > chunk_offset ||
1576 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001577 map = (struct map_lookup *)em->bdev;
1578
1579 for (i = 0; i < map->num_stripes; i++) {
1580 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1581 map->stripes[i].physical);
1582 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001583
Chris Masondfe25022008-05-13 13:46:40 -04001584 if (map->stripes[i].dev) {
1585 ret = btrfs_update_device(trans, map->stripes[i].dev);
1586 BUG_ON(ret);
1587 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001588 }
1589 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1590 chunk_offset);
1591
1592 BUG_ON(ret);
1593
1594 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1595 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1596 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001597 }
1598
Zheng Yan1a40e232008-09-26 10:09:34 -04001599 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1600 BUG_ON(ret);
1601
Chris Mason8f18cf12008-04-25 16:53:30 -04001602 spin_lock(&em_tree->lock);
1603 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001604 spin_unlock(&em_tree->lock);
1605
Chris Mason8f18cf12008-04-25 16:53:30 -04001606 kfree(map);
1607 em->bdev = NULL;
1608
1609 /* once for the tree */
1610 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001611 /* once for us */
1612 free_extent_map(em);
1613
Chris Mason7d9eb122008-07-08 14:19:17 -04001614 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001615 btrfs_end_transaction(trans, root);
1616 return 0;
1617}
1618
Yan Zheng2b820322008-11-17 21:11:30 -05001619static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1620{
1621 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1622 struct btrfs_path *path;
1623 struct extent_buffer *leaf;
1624 struct btrfs_chunk *chunk;
1625 struct btrfs_key key;
1626 struct btrfs_key found_key;
1627 u64 chunk_tree = chunk_root->root_key.objectid;
1628 u64 chunk_type;
1629 int ret;
1630
1631 path = btrfs_alloc_path();
1632 if (!path)
1633 return -ENOMEM;
1634
1635 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1636 key.offset = (u64)-1;
1637 key.type = BTRFS_CHUNK_ITEM_KEY;
1638
1639 while (1) {
1640 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1641 if (ret < 0)
1642 goto error;
1643 BUG_ON(ret == 0);
1644
1645 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1646 key.type);
1647 if (ret < 0)
1648 goto error;
1649 if (ret > 0)
1650 break;
1651
1652 leaf = path->nodes[0];
1653 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1654
1655 chunk = btrfs_item_ptr(leaf, path->slots[0],
1656 struct btrfs_chunk);
1657 chunk_type = btrfs_chunk_type(leaf, chunk);
1658 btrfs_release_path(chunk_root, path);
1659
1660 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1661 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1662 found_key.objectid,
1663 found_key.offset);
1664 BUG_ON(ret);
1665 }
1666
1667 if (found_key.offset == 0)
1668 break;
1669 key.offset = found_key.offset - 1;
1670 }
1671 ret = 0;
1672error:
1673 btrfs_free_path(path);
1674 return ret;
1675}
1676
Chris Masonec44a352008-04-28 15:29:52 -04001677static u64 div_factor(u64 num, int factor)
1678{
1679 if (factor == 10)
1680 return num;
1681 num *= factor;
1682 do_div(num, 10);
1683 return num;
1684}
1685
Chris Masonec44a352008-04-28 15:29:52 -04001686int btrfs_balance(struct btrfs_root *dev_root)
1687{
1688 int ret;
1689 struct list_head *cur;
1690 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1691 struct btrfs_device *device;
1692 u64 old_size;
1693 u64 size_to_free;
1694 struct btrfs_path *path;
1695 struct btrfs_key key;
1696 struct btrfs_chunk *chunk;
1697 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1698 struct btrfs_trans_handle *trans;
1699 struct btrfs_key found_key;
1700
Yan Zheng2b820322008-11-17 21:11:30 -05001701 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1702 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001703
Chris Mason7d9eb122008-07-08 14:19:17 -04001704 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001705 dev_root = dev_root->fs_info->dev_root;
1706
Chris Masonec44a352008-04-28 15:29:52 -04001707 /* step one make some room on all the devices */
1708 list_for_each(cur, devices) {
1709 device = list_entry(cur, struct btrfs_device, dev_list);
1710 old_size = device->total_bytes;
1711 size_to_free = div_factor(old_size, 1);
1712 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001713 if (!device->writeable ||
1714 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001715 continue;
1716
1717 ret = btrfs_shrink_device(device, old_size - size_to_free);
1718 BUG_ON(ret);
1719
1720 trans = btrfs_start_transaction(dev_root, 1);
1721 BUG_ON(!trans);
1722
1723 ret = btrfs_grow_device(trans, device, old_size);
1724 BUG_ON(ret);
1725
1726 btrfs_end_transaction(trans, dev_root);
1727 }
1728
1729 /* step two, relocate all the chunks */
1730 path = btrfs_alloc_path();
1731 BUG_ON(!path);
1732
1733 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1734 key.offset = (u64)-1;
1735 key.type = BTRFS_CHUNK_ITEM_KEY;
1736
1737 while(1) {
1738 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1739 if (ret < 0)
1740 goto error;
1741
1742 /*
1743 * this shouldn't happen, it means the last relocate
1744 * failed
1745 */
1746 if (ret == 0)
1747 break;
1748
1749 ret = btrfs_previous_item(chunk_root, path, 0,
1750 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001751 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001752 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001753
Chris Masonec44a352008-04-28 15:29:52 -04001754 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1755 path->slots[0]);
1756 if (found_key.objectid != key.objectid)
1757 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001758
Chris Masonec44a352008-04-28 15:29:52 -04001759 chunk = btrfs_item_ptr(path->nodes[0],
1760 path->slots[0],
1761 struct btrfs_chunk);
1762 key.offset = found_key.offset;
1763 /* chunk zero is special */
1764 if (key.offset == 0)
1765 break;
1766
Chris Mason7d9eb122008-07-08 14:19:17 -04001767 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001768 ret = btrfs_relocate_chunk(chunk_root,
1769 chunk_root->root_key.objectid,
1770 found_key.objectid,
1771 found_key.offset);
1772 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001773 }
1774 ret = 0;
1775error:
1776 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001777 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001778 return ret;
1779}
1780
Chris Mason8f18cf12008-04-25 16:53:30 -04001781/*
1782 * shrinking a device means finding all of the device extents past
1783 * the new size, and then following the back refs to the chunks.
1784 * The chunk relocation code actually frees the device extent
1785 */
1786int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1787{
1788 struct btrfs_trans_handle *trans;
1789 struct btrfs_root *root = device->dev_root;
1790 struct btrfs_dev_extent *dev_extent = NULL;
1791 struct btrfs_path *path;
1792 u64 length;
1793 u64 chunk_tree;
1794 u64 chunk_objectid;
1795 u64 chunk_offset;
1796 int ret;
1797 int slot;
1798 struct extent_buffer *l;
1799 struct btrfs_key key;
1800 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1801 u64 old_total = btrfs_super_total_bytes(super_copy);
1802 u64 diff = device->total_bytes - new_size;
1803
Yan Zheng2b820322008-11-17 21:11:30 -05001804 if (new_size >= device->total_bytes)
1805 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001806
1807 path = btrfs_alloc_path();
1808 if (!path)
1809 return -ENOMEM;
1810
1811 trans = btrfs_start_transaction(root, 1);
1812 if (!trans) {
1813 ret = -ENOMEM;
1814 goto done;
1815 }
1816
1817 path->reada = 2;
1818
Chris Mason7d9eb122008-07-08 14:19:17 -04001819 lock_chunks(root);
1820
Chris Mason8f18cf12008-04-25 16:53:30 -04001821 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001822 if (device->writeable)
1823 device->fs_devices->total_rw_bytes -= diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04001824 ret = btrfs_update_device(trans, device);
1825 if (ret) {
Chris Mason7d9eb122008-07-08 14:19:17 -04001826 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001827 btrfs_end_transaction(trans, root);
1828 goto done;
1829 }
1830 WARN_ON(diff > old_total);
1831 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001832 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001833 btrfs_end_transaction(trans, root);
1834
1835 key.objectid = device->devid;
1836 key.offset = (u64)-1;
1837 key.type = BTRFS_DEV_EXTENT_KEY;
1838
1839 while (1) {
1840 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1841 if (ret < 0)
1842 goto done;
1843
1844 ret = btrfs_previous_item(root, path, 0, key.type);
1845 if (ret < 0)
1846 goto done;
1847 if (ret) {
1848 ret = 0;
1849 goto done;
1850 }
1851
1852 l = path->nodes[0];
1853 slot = path->slots[0];
1854 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1855
1856 if (key.objectid != device->devid)
1857 goto done;
1858
1859 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1860 length = btrfs_dev_extent_length(l, dev_extent);
1861
1862 if (key.offset + length <= new_size)
1863 goto done;
1864
1865 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1866 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1867 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1868 btrfs_release_path(root, path);
1869
1870 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1871 chunk_offset);
1872 if (ret)
1873 goto done;
1874 }
1875
1876done:
1877 btrfs_free_path(path);
1878 return ret;
1879}
1880
Chris Mason0b86a832008-03-24 15:01:56 -04001881int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1882 struct btrfs_root *root,
1883 struct btrfs_key *key,
1884 struct btrfs_chunk *chunk, int item_size)
1885{
1886 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1887 struct btrfs_disk_key disk_key;
1888 u32 array_size;
1889 u8 *ptr;
1890
1891 array_size = btrfs_super_sys_array_size(super_copy);
1892 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1893 return -EFBIG;
1894
1895 ptr = super_copy->sys_chunk_array + array_size;
1896 btrfs_cpu_key_to_disk(&disk_key, key);
1897 memcpy(ptr, &disk_key, sizeof(disk_key));
1898 ptr += sizeof(disk_key);
1899 memcpy(ptr, chunk, item_size);
1900 item_size += sizeof(disk_key);
1901 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1902 return 0;
1903}
1904
Chris Masona1b32a52008-09-05 16:09:51 -04001905static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
1906 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04001907{
1908 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1909 return calc_size;
1910 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1911 return calc_size * (num_stripes / sub_stripes);
1912 else
1913 return calc_size * num_stripes;
1914}
1915
Yan Zheng2b820322008-11-17 21:11:30 -05001916static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1917 struct btrfs_root *extent_root,
1918 struct map_lookup **map_ret,
1919 u64 *num_bytes, u64 *stripe_size,
1920 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001921{
Chris Mason593060d2008-03-25 16:50:33 -04001922 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001923 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001924 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04001925 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05001926 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001927 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04001928 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05001929 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04001930 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001931 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001932 u64 max_chunk_size = calc_size;
1933 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001934 u64 avail;
1935 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001936 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04001937 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001938 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001939 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001940 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001941 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001942 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001943 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001944
Chris Masonec44a352008-04-28 15:29:52 -04001945 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1946 (type & BTRFS_BLOCK_GROUP_DUP)) {
1947 WARN_ON(1);
1948 type &= ~BTRFS_BLOCK_GROUP_DUP;
1949 }
Yan Zheng2b820322008-11-17 21:11:30 -05001950 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04001951 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001952
Chris Masona40a90a2008-04-18 11:55:51 -04001953 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001954 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04001955 min_stripes = 2;
1956 }
1957 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04001958 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001959 min_stripes = 2;
1960 }
Chris Mason8790d502008-04-03 16:29:03 -04001961 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001962 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001963 if (num_stripes < 2)
1964 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04001965 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04001966 }
Chris Mason321aecc2008-04-16 10:49:51 -04001967 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001968 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04001969 if (num_stripes < 4)
1970 return -ENOSPC;
1971 num_stripes &= ~(u32)1;
1972 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001973 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04001974 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04001975
1976 if (type & BTRFS_BLOCK_GROUP_DATA) {
1977 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001978 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001979 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1980 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001981 min_stripe_size = 32 * 1024 * 1024;
1982 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1983 calc_size = 8 * 1024 * 1024;
1984 max_chunk_size = calc_size * 2;
1985 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001986 }
1987
Yan Zheng2b820322008-11-17 21:11:30 -05001988 /* we don't want a chunk larger than 10% of writeable space */
1989 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
1990 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001991
Chris Masona40a90a2008-04-18 11:55:51 -04001992again:
Yan Zheng2b820322008-11-17 21:11:30 -05001993 if (!map || map->num_stripes != num_stripes) {
1994 kfree(map);
1995 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1996 if (!map)
1997 return -ENOMEM;
1998 map->num_stripes = num_stripes;
1999 }
2000
Chris Mason9b3f68b2008-04-18 10:29:51 -04002001 if (calc_size * num_stripes > max_chunk_size) {
2002 calc_size = max_chunk_size;
2003 do_div(calc_size, num_stripes);
2004 do_div(calc_size, stripe_len);
2005 calc_size *= stripe_len;
2006 }
2007 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002008 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002009
Chris Mason9b3f68b2008-04-18 10:29:51 -04002010 do_div(calc_size, stripe_len);
2011 calc_size *= stripe_len;
2012
Yan Zheng2b820322008-11-17 21:11:30 -05002013 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002014 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002015
2016 if (type & BTRFS_BLOCK_GROUP_DUP)
2017 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002018 else
2019 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002020
Josef Bacik0f9dd462008-09-23 13:14:11 -04002021 /*
2022 * we add 1MB because we never use the first 1MB of the device, unless
2023 * we've looped, then we are likely allocating the maximum amount of
2024 * space left already
2025 */
2026 if (!looped)
2027 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002028
Yan Zheng2b820322008-11-17 21:11:30 -05002029 INIT_LIST_HEAD(&private_devs);
Chris Mason6324fbf2008-03-24 15:01:59 -04002030 while(index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002031 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002032 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002033 if (device->total_bytes > device->bytes_used)
2034 avail = device->total_bytes - device->bytes_used;
2035 else
2036 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002037 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002038
Chris Masondfe25022008-05-13 13:46:40 -04002039 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002040 ret = find_free_dev_extent(trans, device,
2041 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002042 if (ret == 0) {
2043 list_move_tail(&device->dev_alloc_list,
2044 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002045 map->stripes[index].dev = device;
2046 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002047 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002048 if (type & BTRFS_BLOCK_GROUP_DUP) {
2049 map->stripes[index].dev = device;
2050 map->stripes[index].physical =
2051 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002052 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002053 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002054 }
Chris Masondfe25022008-05-13 13:46:40 -04002055 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002056 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002057 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002058 break;
2059 }
Yan Zheng2b820322008-11-17 21:11:30 -05002060 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002061 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002062 if (index >= min_stripes) {
2063 num_stripes = index;
2064 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2065 num_stripes /= sub_stripes;
2066 num_stripes *= sub_stripes;
2067 }
2068 looped = 1;
2069 goto again;
2070 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002071 if (!looped && max_avail > 0) {
2072 looped = 1;
2073 calc_size = max_avail;
2074 goto again;
2075 }
Yan Zheng2b820322008-11-17 21:11:30 -05002076 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002077 return -ENOSPC;
2078 }
Chris Mason593060d2008-03-25 16:50:33 -04002079 map->sector_size = extent_root->sectorsize;
2080 map->stripe_len = stripe_len;
2081 map->io_align = stripe_len;
2082 map->io_width = stripe_len;
2083 map->type = type;
2084 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002085 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002086
Yan Zheng2b820322008-11-17 21:11:30 -05002087 *map_ret = map;
2088 *stripe_size = calc_size;
2089 *num_bytes = chunk_bytes_by_type(type, calc_size,
2090 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002091
2092 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002093 if (!em) {
2094 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002095 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002096 }
Chris Mason0b86a832008-03-24 15:01:56 -04002097 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002098 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002099 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002100 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002101 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002102
Chris Mason0b86a832008-03-24 15:01:56 -04002103 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2104 spin_lock(&em_tree->lock);
2105 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002106 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002107 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002108 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002109
2110 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2111 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2112 start, *num_bytes);
2113 BUG_ON(ret);
2114
2115 index = 0;
2116 while (index < map->num_stripes) {
2117 device = map->stripes[index].dev;
2118 dev_offset = map->stripes[index].physical;
2119
2120 ret = btrfs_alloc_dev_extent(trans, device,
2121 info->chunk_root->root_key.objectid,
2122 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2123 start, dev_offset, calc_size);
2124 BUG_ON(ret);
2125 index++;
2126 }
2127
2128 return 0;
2129}
2130
2131static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *extent_root,
2133 struct map_lookup *map, u64 chunk_offset,
2134 u64 chunk_size, u64 stripe_size)
2135{
2136 u64 dev_offset;
2137 struct btrfs_key key;
2138 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2139 struct btrfs_device *device;
2140 struct btrfs_chunk *chunk;
2141 struct btrfs_stripe *stripe;
2142 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2143 int index = 0;
2144 int ret;
2145
2146 chunk = kzalloc(item_size, GFP_NOFS);
2147 if (!chunk)
2148 return -ENOMEM;
2149
2150 index = 0;
2151 while (index < map->num_stripes) {
2152 device = map->stripes[index].dev;
2153 device->bytes_used += stripe_size;
2154 ret = btrfs_update_device(trans, device);
2155 BUG_ON(ret);
2156 index++;
2157 }
2158
2159 index = 0;
2160 stripe = &chunk->stripe;
2161 while (index < map->num_stripes) {
2162 device = map->stripes[index].dev;
2163 dev_offset = map->stripes[index].physical;
2164
2165 btrfs_set_stack_stripe_devid(stripe, device->devid);
2166 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2167 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2168 stripe++;
2169 index++;
2170 }
2171
2172 btrfs_set_stack_chunk_length(chunk, chunk_size);
2173 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2174 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2175 btrfs_set_stack_chunk_type(chunk, map->type);
2176 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2177 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2178 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2179 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2180 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2181
2182 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2183 key.type = BTRFS_CHUNK_ITEM_KEY;
2184 key.offset = chunk_offset;
2185
2186 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2187 BUG_ON(ret);
2188
2189 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2190 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2191 item_size);
2192 BUG_ON(ret);
2193 }
2194 kfree(chunk);
2195 return 0;
2196}
2197
2198/*
2199 * Chunk allocation falls into two parts. The first part does works
2200 * that make the new allocated chunk useable, but not do any operation
2201 * that modifies the chunk tree. The second part does the works that
2202 * require modifying the chunk tree. This division is important for the
2203 * bootstrap process of adding storage to a seed btrfs.
2204 */
2205int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2206 struct btrfs_root *extent_root, u64 type)
2207{
2208 u64 chunk_offset;
2209 u64 chunk_size;
2210 u64 stripe_size;
2211 struct map_lookup *map;
2212 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2213 int ret;
2214
2215 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2216 &chunk_offset);
2217 if (ret)
2218 return ret;
2219
2220 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2221 &stripe_size, chunk_offset, type);
2222 if (ret)
2223 return ret;
2224
2225 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2226 chunk_size, stripe_size);
2227 BUG_ON(ret);
2228 return 0;
2229}
2230
2231static int noinline init_first_rw_device(struct btrfs_trans_handle *trans,
2232 struct btrfs_root *root,
2233 struct btrfs_device *device)
2234{
2235 u64 chunk_offset;
2236 u64 sys_chunk_offset;
2237 u64 chunk_size;
2238 u64 sys_chunk_size;
2239 u64 stripe_size;
2240 u64 sys_stripe_size;
2241 u64 alloc_profile;
2242 struct map_lookup *map;
2243 struct map_lookup *sys_map;
2244 struct btrfs_fs_info *fs_info = root->fs_info;
2245 struct btrfs_root *extent_root = fs_info->extent_root;
2246 int ret;
2247
2248 ret = find_next_chunk(fs_info->chunk_root,
2249 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2250 BUG_ON(ret);
2251
2252 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2253 (fs_info->metadata_alloc_profile &
2254 fs_info->avail_metadata_alloc_bits);
2255 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2256
2257 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2258 &stripe_size, chunk_offset, alloc_profile);
2259 BUG_ON(ret);
2260
2261 sys_chunk_offset = chunk_offset + chunk_size;
2262
2263 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2264 (fs_info->system_alloc_profile &
2265 fs_info->avail_system_alloc_bits);
2266 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2267
2268 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2269 &sys_chunk_size, &sys_stripe_size,
2270 sys_chunk_offset, alloc_profile);
2271 BUG_ON(ret);
2272
2273 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2274 BUG_ON(ret);
2275
2276 /*
2277 * Modifying chunk tree needs allocating new blocks from both
2278 * system block group and metadata block group. So we only can
2279 * do operations require modifying the chunk tree after both
2280 * block groups were created.
2281 */
2282 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2283 chunk_size, stripe_size);
2284 BUG_ON(ret);
2285
2286 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2287 sys_chunk_offset, sys_chunk_size,
2288 sys_stripe_size);
2289 BUG_ON(ret);
2290 return 0;
2291}
2292
2293int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2294{
2295 struct extent_map *em;
2296 struct map_lookup *map;
2297 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2298 int readonly = 0;
2299 int i;
2300
2301 spin_lock(&map_tree->map_tree.lock);
2302 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2303 spin_unlock(&map_tree->map_tree.lock);
2304 if (!em)
2305 return 1;
2306
2307 map = (struct map_lookup *)em->bdev;
2308 for (i = 0; i < map->num_stripes; i++) {
2309 if (!map->stripes[i].dev->writeable) {
2310 readonly = 1;
2311 break;
2312 }
2313 }
2314 free_extent_map(em);
2315 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002316}
2317
2318void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2319{
2320 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2321}
2322
2323void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2324{
2325 struct extent_map *em;
2326
2327 while(1) {
2328 spin_lock(&tree->map_tree.lock);
2329 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2330 if (em)
2331 remove_extent_mapping(&tree->map_tree, em);
2332 spin_unlock(&tree->map_tree.lock);
2333 if (!em)
2334 break;
2335 kfree(em->bdev);
2336 /* once for us */
2337 free_extent_map(em);
2338 /* once for the tree */
2339 free_extent_map(em);
2340 }
2341}
2342
Chris Masonf1885912008-04-09 16:28:12 -04002343int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2344{
2345 struct extent_map *em;
2346 struct map_lookup *map;
2347 struct extent_map_tree *em_tree = &map_tree->map_tree;
2348 int ret;
2349
2350 spin_lock(&em_tree->lock);
2351 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002352 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002353 BUG_ON(!em);
2354
2355 BUG_ON(em->start > logical || em->start + em->len < logical);
2356 map = (struct map_lookup *)em->bdev;
2357 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2358 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002359 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2360 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002361 else
2362 ret = 1;
2363 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002364 return ret;
2365}
2366
Chris Masondfe25022008-05-13 13:46:40 -04002367static int find_live_mirror(struct map_lookup *map, int first, int num,
2368 int optimal)
2369{
2370 int i;
2371 if (map->stripes[optimal].dev->bdev)
2372 return optimal;
2373 for (i = first; i < first + num; i++) {
2374 if (map->stripes[i].dev->bdev)
2375 return i;
2376 }
2377 /* we couldn't find one that doesn't fail. Just return something
2378 * and the io error handling code will clean up eventually
2379 */
2380 return optimal;
2381}
2382
Chris Masonf2d8d742008-04-21 10:03:05 -04002383static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2384 u64 logical, u64 *length,
2385 struct btrfs_multi_bio **multi_ret,
2386 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002387{
2388 struct extent_map *em;
2389 struct map_lookup *map;
2390 struct extent_map_tree *em_tree = &map_tree->map_tree;
2391 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002392 u64 stripe_offset;
2393 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002394 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002395 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002396 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002397 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002398 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002399 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002400 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002401
Chris Masoncea9e442008-04-09 16:28:12 -04002402 if (multi_ret && !(rw & (1 << BIO_RW))) {
2403 stripes_allocated = 1;
2404 }
2405again:
2406 if (multi_ret) {
2407 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2408 GFP_NOFS);
2409 if (!multi)
2410 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002411
2412 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002413 }
Chris Mason0b86a832008-03-24 15:01:56 -04002414
2415 spin_lock(&em_tree->lock);
2416 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002417 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002418
2419 if (!em && unplug_page)
2420 return 0;
2421
Chris Mason3b951512008-04-17 11:29:12 -04002422 if (!em) {
Chris Masona061fc82008-05-07 11:43:44 -04002423 printk("unable to find logical %Lu len %Lu\n", logical, *length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002424 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002425 }
Chris Mason0b86a832008-03-24 15:01:56 -04002426
2427 BUG_ON(em->start > logical || em->start + em->len < logical);
2428 map = (struct map_lookup *)em->bdev;
2429 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002430
Chris Masonf1885912008-04-09 16:28:12 -04002431 if (mirror_num > map->num_stripes)
2432 mirror_num = 0;
2433
Chris Masoncea9e442008-04-09 16:28:12 -04002434 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002435 if (rw & (1 << BIO_RW)) {
2436 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2437 BTRFS_BLOCK_GROUP_DUP)) {
2438 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002439 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002440 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2441 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002442 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002443 }
2444 }
2445 if (multi_ret && rw == WRITE &&
2446 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002447 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002448 free_extent_map(em);
2449 kfree(multi);
2450 goto again;
2451 }
Chris Mason593060d2008-03-25 16:50:33 -04002452 stripe_nr = offset;
2453 /*
2454 * stripe_nr counts the total number of stripes we have to stride
2455 * to get to this block
2456 */
2457 do_div(stripe_nr, map->stripe_len);
2458
2459 stripe_offset = stripe_nr * map->stripe_len;
2460 BUG_ON(offset < stripe_offset);
2461
2462 /* stripe_offset is the offset of this block in its stripe*/
2463 stripe_offset = offset - stripe_offset;
2464
Chris Masoncea9e442008-04-09 16:28:12 -04002465 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002466 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002467 BTRFS_BLOCK_GROUP_DUP)) {
2468 /* we limit the length of each bio to what fits in a stripe */
2469 *length = min_t(u64, em->len - offset,
2470 map->stripe_len - stripe_offset);
2471 } else {
2472 *length = em->len - offset;
2473 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002474
2475 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002476 goto out;
2477
Chris Masonf2d8d742008-04-21 10:03:05 -04002478 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002479 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002480 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002481 if (unplug_page || (rw & (1 << BIO_RW)))
2482 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002483 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002484 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002485 else {
2486 stripe_index = find_live_mirror(map, 0,
2487 map->num_stripes,
2488 current->pid % map->num_stripes);
2489 }
Chris Mason2fff7342008-04-29 14:12:09 -04002490
Chris Mason611f0e02008-04-03 16:29:03 -04002491 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002492 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002493 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002494 else if (mirror_num)
2495 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002496
Chris Mason321aecc2008-04-16 10:49:51 -04002497 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2498 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002499
2500 stripe_index = do_div(stripe_nr, factor);
2501 stripe_index *= map->sub_stripes;
2502
Chris Masonf2d8d742008-04-21 10:03:05 -04002503 if (unplug_page || (rw & (1 << BIO_RW)))
2504 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002505 else if (mirror_num)
2506 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002507 else {
2508 stripe_index = find_live_mirror(map, stripe_index,
2509 map->sub_stripes, stripe_index +
2510 current->pid % map->sub_stripes);
2511 }
Chris Mason8790d502008-04-03 16:29:03 -04002512 } else {
2513 /*
2514 * after this do_div call, stripe_nr is the number of stripes
2515 * on this device we have to walk to find the data, and
2516 * stripe_index is the number of our device in the stripe array
2517 */
2518 stripe_index = do_div(stripe_nr, map->num_stripes);
2519 }
Chris Mason593060d2008-03-25 16:50:33 -04002520 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002521
Chris Masonf2d8d742008-04-21 10:03:05 -04002522 for (i = 0; i < num_stripes; i++) {
2523 if (unplug_page) {
2524 struct btrfs_device *device;
2525 struct backing_dev_info *bdi;
2526
2527 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002528 if (device->bdev) {
2529 bdi = blk_get_backing_dev_info(device->bdev);
2530 if (bdi->unplug_io_fn) {
2531 bdi->unplug_io_fn(bdi, unplug_page);
2532 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002533 }
2534 } else {
2535 multi->stripes[i].physical =
2536 map->stripes[stripe_index].physical +
2537 stripe_offset + stripe_nr * map->stripe_len;
2538 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2539 }
Chris Masoncea9e442008-04-09 16:28:12 -04002540 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002541 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002542 if (multi_ret) {
2543 *multi_ret = multi;
2544 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002545 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002546 }
Chris Masoncea9e442008-04-09 16:28:12 -04002547out:
Chris Mason0b86a832008-03-24 15:01:56 -04002548 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002549 return 0;
2550}
2551
Chris Masonf2d8d742008-04-21 10:03:05 -04002552int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2553 u64 logical, u64 *length,
2554 struct btrfs_multi_bio **multi_ret, int mirror_num)
2555{
2556 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2557 mirror_num, NULL);
2558}
2559
2560int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2561 u64 logical, struct page *page)
2562{
2563 u64 length = PAGE_CACHE_SIZE;
2564 return __btrfs_map_block(map_tree, READ, logical, &length,
2565 NULL, 0, page);
2566}
2567
2568
Chris Mason8790d502008-04-03 16:29:03 -04002569static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002570{
Chris Masoncea9e442008-04-09 16:28:12 -04002571 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002572 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002573
Chris Mason8790d502008-04-03 16:29:03 -04002574 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002575 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002576
Chris Mason7d2b4da2008-08-05 10:13:57 -04002577 if (bio == multi->orig_bio)
2578 is_orig_bio = 1;
2579
Chris Masoncea9e442008-04-09 16:28:12 -04002580 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002581 if (!is_orig_bio) {
2582 bio_put(bio);
2583 bio = multi->orig_bio;
2584 }
Chris Mason8790d502008-04-03 16:29:03 -04002585 bio->bi_private = multi->private;
2586 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002587 /* only send an error to the higher layers if it is
2588 * beyond the tolerance of the multi-bio
2589 */
Chris Mason1259ab72008-05-12 13:39:03 -04002590 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002591 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002592 } else if (err) {
2593 /*
2594 * this bio is actually up to date, we didn't
2595 * go over the max number of errors
2596 */
2597 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002598 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002599 }
Chris Mason8790d502008-04-03 16:29:03 -04002600 kfree(multi);
2601
2602 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002603 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002604 bio_put(bio);
2605 }
Chris Mason8790d502008-04-03 16:29:03 -04002606}
2607
Chris Mason8b712842008-06-11 16:50:36 -04002608struct async_sched {
2609 struct bio *bio;
2610 int rw;
2611 struct btrfs_fs_info *info;
2612 struct btrfs_work work;
2613};
2614
2615/*
2616 * see run_scheduled_bios for a description of why bios are collected for
2617 * async submit.
2618 *
2619 * This will add one bio to the pending list for a device and make sure
2620 * the work struct is scheduled.
2621 */
Chris Masona1b32a52008-09-05 16:09:51 -04002622static int noinline schedule_bio(struct btrfs_root *root,
2623 struct btrfs_device *device,
2624 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002625{
2626 int should_queue = 1;
2627
2628 /* don't bother with additional async steps for reads, right now */
2629 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002630 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002631 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002632 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002633 return 0;
2634 }
2635
2636 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04002637 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002638 * higher layers. Otherwise, the async bio makes it appear we have
2639 * made progress against dirty pages when we've really just put it
2640 * on a queue for later
2641 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04002642 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002643 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002644 bio->bi_next = NULL;
2645 bio->bi_rw |= rw;
2646
2647 spin_lock(&device->io_lock);
2648
2649 if (device->pending_bio_tail)
2650 device->pending_bio_tail->bi_next = bio;
2651
2652 device->pending_bio_tail = bio;
2653 if (!device->pending_bios)
2654 device->pending_bios = bio;
2655 if (device->running_pending)
2656 should_queue = 0;
2657
2658 spin_unlock(&device->io_lock);
2659
2660 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002661 btrfs_queue_worker(&root->fs_info->submit_workers,
2662 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002663 return 0;
2664}
2665
Chris Masonf1885912008-04-09 16:28:12 -04002666int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002667 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002668{
2669 struct btrfs_mapping_tree *map_tree;
2670 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002671 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002672 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002673 u64 length = 0;
2674 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002675 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002676 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002677 int dev_nr = 0;
2678 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002679
Chris Masonf2d8d742008-04-21 10:03:05 -04002680 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002681 map_tree = &root->fs_info->mapping_tree;
2682 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002683
Chris Masonf1885912008-04-09 16:28:12 -04002684 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2685 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002686 BUG_ON(ret);
2687
2688 total_devs = multi->num_stripes;
2689 if (map_length < length) {
2690 printk("mapping failed logical %Lu bio len %Lu "
2691 "len %Lu\n", logical, length, map_length);
2692 BUG();
2693 }
2694 multi->end_io = first_bio->bi_end_io;
2695 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002696 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002697 atomic_set(&multi->stripes_pending, multi->num_stripes);
2698
Chris Mason8790d502008-04-03 16:29:03 -04002699 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002700 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002701 if (dev_nr < total_devs - 1) {
2702 bio = bio_clone(first_bio, GFP_NOFS);
2703 BUG_ON(!bio);
2704 } else {
2705 bio = first_bio;
2706 }
2707 bio->bi_private = multi;
2708 bio->bi_end_io = end_bio_multi_stripe;
2709 }
Chris Masoncea9e442008-04-09 16:28:12 -04002710 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2711 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002712 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002713 if (dev && dev->bdev) {
2714 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002715 if (async_submit)
2716 schedule_bio(root, dev, rw, bio);
2717 else
2718 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002719 } else {
2720 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2721 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002722 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002723 }
Chris Mason8790d502008-04-03 16:29:03 -04002724 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002725 }
Chris Masoncea9e442008-04-09 16:28:12 -04002726 if (total_devs == 1)
2727 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002728 return 0;
2729}
2730
Chris Masona4437552008-04-18 10:29:38 -04002731struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05002732 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04002733{
Yan Zheng2b820322008-11-17 21:11:30 -05002734 struct btrfs_device *device;
2735 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002736
Yan Zheng2b820322008-11-17 21:11:30 -05002737 cur_devices = root->fs_info->fs_devices;
2738 while (cur_devices) {
2739 if (!fsid ||
2740 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2741 device = __find_device(&cur_devices->devices,
2742 devid, uuid);
2743 if (device)
2744 return device;
2745 }
2746 cur_devices = cur_devices->seed;
2747 }
2748 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002749}
2750
Chris Masondfe25022008-05-13 13:46:40 -04002751static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2752 u64 devid, u8 *dev_uuid)
2753{
2754 struct btrfs_device *device;
2755 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2756
2757 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05002758 if (!device)
2759 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04002760 list_add(&device->dev_list,
2761 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04002762 device->barriers = 1;
2763 device->dev_root = root->fs_info->dev_root;
2764 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002765 device->work.func = pending_bios_fn;
Chris Masondfe25022008-05-13 13:46:40 -04002766 fs_devices->num_devices++;
2767 spin_lock_init(&device->io_lock);
2768 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2769 return device;
2770}
2771
Chris Mason0b86a832008-03-24 15:01:56 -04002772static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2773 struct extent_buffer *leaf,
2774 struct btrfs_chunk *chunk)
2775{
2776 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2777 struct map_lookup *map;
2778 struct extent_map *em;
2779 u64 logical;
2780 u64 length;
2781 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002782 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002783 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002784 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002785 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002786
Chris Masone17cade2008-04-15 15:41:47 -04002787 logical = key->offset;
2788 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002789
Chris Mason0b86a832008-03-24 15:01:56 -04002790 spin_lock(&map_tree->map_tree.lock);
2791 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002792 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002793
2794 /* already mapped? */
2795 if (em && em->start <= logical && em->start + em->len > logical) {
2796 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002797 return 0;
2798 } else if (em) {
2799 free_extent_map(em);
2800 }
Chris Mason0b86a832008-03-24 15:01:56 -04002801
2802 map = kzalloc(sizeof(*map), GFP_NOFS);
2803 if (!map)
2804 return -ENOMEM;
2805
2806 em = alloc_extent_map(GFP_NOFS);
2807 if (!em)
2808 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002809 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2810 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002811 if (!map) {
2812 free_extent_map(em);
2813 return -ENOMEM;
2814 }
2815
2816 em->bdev = (struct block_device *)map;
2817 em->start = logical;
2818 em->len = length;
2819 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002820 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002821
Chris Mason593060d2008-03-25 16:50:33 -04002822 map->num_stripes = num_stripes;
2823 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2824 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2825 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2826 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2827 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002828 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002829 for (i = 0; i < num_stripes; i++) {
2830 map->stripes[i].physical =
2831 btrfs_stripe_offset_nr(leaf, chunk, i);
2832 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002833 read_extent_buffer(leaf, uuid, (unsigned long)
2834 btrfs_stripe_dev_uuid_nr(chunk, i),
2835 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002836 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2837 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04002838 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002839 kfree(map);
2840 free_extent_map(em);
2841 return -EIO;
2842 }
Chris Masondfe25022008-05-13 13:46:40 -04002843 if (!map->stripes[i].dev) {
2844 map->stripes[i].dev =
2845 add_missing_dev(root, devid, uuid);
2846 if (!map->stripes[i].dev) {
2847 kfree(map);
2848 free_extent_map(em);
2849 return -EIO;
2850 }
2851 }
2852 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002853 }
2854
2855 spin_lock(&map_tree->map_tree.lock);
2856 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002857 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002858 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002859 free_extent_map(em);
2860
2861 return 0;
2862}
2863
2864static int fill_device_from_item(struct extent_buffer *leaf,
2865 struct btrfs_dev_item *dev_item,
2866 struct btrfs_device *device)
2867{
2868 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04002869
2870 device->devid = btrfs_device_id(leaf, dev_item);
2871 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2872 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2873 device->type = btrfs_device_type(leaf, dev_item);
2874 device->io_align = btrfs_device_io_align(leaf, dev_item);
2875 device->io_width = btrfs_device_io_width(leaf, dev_item);
2876 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002877
2878 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04002879 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002880
Chris Mason0b86a832008-03-24 15:01:56 -04002881 return 0;
2882}
2883
Yan Zheng2b820322008-11-17 21:11:30 -05002884static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
2885{
2886 struct btrfs_fs_devices *fs_devices;
2887 int ret;
2888
2889 mutex_lock(&uuid_mutex);
2890
2891 fs_devices = root->fs_info->fs_devices->seed;
2892 while (fs_devices) {
2893 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2894 ret = 0;
2895 goto out;
2896 }
2897 fs_devices = fs_devices->seed;
2898 }
2899
2900 fs_devices = find_fsid(fsid);
2901 if (!fs_devices) {
2902 ret = -ENOENT;
2903 goto out;
2904 }
2905 if (fs_devices->opened) {
2906 ret = -EBUSY;
2907 goto out;
2908 }
2909
2910 ret = __btrfs_open_devices(fs_devices, root->fs_info->bdev_holder);
2911 if (ret)
2912 goto out;
2913
2914 if (!fs_devices->seeding) {
2915 __btrfs_close_devices(fs_devices);
2916 ret = -EINVAL;
2917 goto out;
2918 }
2919
2920 fs_devices->seed = root->fs_info->fs_devices->seed;
2921 root->fs_info->fs_devices->seed = fs_devices;
2922 fs_devices->sprouted = 1;
2923out:
2924 mutex_unlock(&uuid_mutex);
2925 return ret;
2926}
2927
Chris Mason0d81ba52008-03-24 15:02:07 -04002928static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04002929 struct extent_buffer *leaf,
2930 struct btrfs_dev_item *dev_item)
2931{
2932 struct btrfs_device *device;
2933 u64 devid;
2934 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002935 int seed_devices = 0;
2936 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04002937 u8 dev_uuid[BTRFS_UUID_SIZE];
2938
Chris Mason0b86a832008-03-24 15:01:56 -04002939 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04002940 read_extent_buffer(leaf, dev_uuid,
2941 (unsigned long)btrfs_device_uuid(dev_item),
2942 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002943 read_extent_buffer(leaf, fs_uuid,
2944 (unsigned long)btrfs_device_fsid(dev_item),
2945 BTRFS_UUID_SIZE);
2946
2947 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
2948 ret = open_seed_devices(root, fs_uuid);
2949 if (ret)
2950 return ret;
2951 seed_devices = 1;
2952 }
2953
2954 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
2955 if (!device || !device->bdev) {
2956 if (!btrfs_test_opt(root, DEGRADED) || seed_devices)
2957 return -EIO;
2958
2959 if (!device) {
2960 printk("warning devid %Lu missing\n", devid);
2961 device = add_missing_dev(root, devid, dev_uuid);
2962 if (!device)
2963 return -ENOMEM;
2964 }
2965 }
2966
2967 if (device->fs_devices != root->fs_info->fs_devices) {
2968 BUG_ON(device->writeable);
2969 if (device->generation !=
2970 btrfs_device_generation(leaf, dev_item))
2971 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04002972 }
Chris Mason0b86a832008-03-24 15:01:56 -04002973
2974 fill_device_from_item(leaf, dev_item, device);
2975 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04002976 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002977 if (device->writeable)
2978 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002979 ret = 0;
2980#if 0
2981 ret = btrfs_open_device(device);
2982 if (ret) {
2983 kfree(device);
2984 }
2985#endif
2986 return ret;
2987}
2988
Chris Mason0d81ba52008-03-24 15:02:07 -04002989int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
2990{
2991 struct btrfs_dev_item *dev_item;
2992
2993 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
2994 dev_item);
2995 return read_one_dev(root, buf, dev_item);
2996}
2997
Chris Mason0b86a832008-03-24 15:01:56 -04002998int btrfs_read_sys_array(struct btrfs_root *root)
2999{
3000 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003001 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003002 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003003 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003004 u8 *ptr;
3005 unsigned long sb_ptr;
3006 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003007 u32 num_stripes;
3008 u32 array_size;
3009 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003010 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003011 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003012
Chris Masona061fc82008-05-07 11:43:44 -04003013 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3014 BTRFS_SUPER_INFO_SIZE);
3015 if (!sb)
3016 return -ENOMEM;
3017 btrfs_set_buffer_uptodate(sb);
3018 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003019 array_size = btrfs_super_sys_array_size(super_copy);
3020
Chris Mason0b86a832008-03-24 15:01:56 -04003021 ptr = super_copy->sys_chunk_array;
3022 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3023 cur = 0;
3024
3025 while (cur < array_size) {
3026 disk_key = (struct btrfs_disk_key *)ptr;
3027 btrfs_disk_key_to_cpu(&key, disk_key);
3028
Chris Masona061fc82008-05-07 11:43:44 -04003029 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003030 sb_ptr += len;
3031 cur += len;
3032
Chris Mason0d81ba52008-03-24 15:02:07 -04003033 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003034 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003035 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003036 if (ret)
3037 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003038 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3039 len = btrfs_chunk_item_size(num_stripes);
3040 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003041 ret = -EIO;
3042 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003043 }
3044 ptr += len;
3045 sb_ptr += len;
3046 cur += len;
3047 }
Chris Masona061fc82008-05-07 11:43:44 -04003048 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003049 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003050}
3051
3052int btrfs_read_chunk_tree(struct btrfs_root *root)
3053{
3054 struct btrfs_path *path;
3055 struct extent_buffer *leaf;
3056 struct btrfs_key key;
3057 struct btrfs_key found_key;
3058 int ret;
3059 int slot;
3060
3061 root = root->fs_info->chunk_root;
3062
3063 path = btrfs_alloc_path();
3064 if (!path)
3065 return -ENOMEM;
3066
3067 /* first we search for all of the device items, and then we
3068 * read in all of the chunk items. This way we can create chunk
3069 * mappings that reference all of the devices that are afound
3070 */
3071 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3072 key.offset = 0;
3073 key.type = 0;
3074again:
3075 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3076 while(1) {
3077 leaf = path->nodes[0];
3078 slot = path->slots[0];
3079 if (slot >= btrfs_header_nritems(leaf)) {
3080 ret = btrfs_next_leaf(root, path);
3081 if (ret == 0)
3082 continue;
3083 if (ret < 0)
3084 goto error;
3085 break;
3086 }
3087 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3088 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3089 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3090 break;
3091 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3092 struct btrfs_dev_item *dev_item;
3093 dev_item = btrfs_item_ptr(leaf, slot,
3094 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003095 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003096 if (ret)
3097 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003098 }
3099 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3100 struct btrfs_chunk *chunk;
3101 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3102 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003103 if (ret)
3104 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003105 }
3106 path->slots[0]++;
3107 }
3108 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3109 key.objectid = 0;
3110 btrfs_release_path(root, path);
3111 goto again;
3112 }
Chris Mason0b86a832008-03-24 15:01:56 -04003113 ret = 0;
3114error:
Yan Zheng2b820322008-11-17 21:11:30 -05003115 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003116 return ret;
3117}