blob: 769f2c5d9e9e53040418c31a2d6fa279c74f271b [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 Mason4b4e25f2008-11-20 10:22:27 -050023#include <linux/version.h>
Chris Mason593060d2008-03-25 16:50:33 -040024#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040026#include "ctree.h"
27#include "extent_map.h"
28#include "disk-io.h"
29#include "transaction.h"
30#include "print-tree.h"
31#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040032#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033
Chris Mason593060d2008-03-25 16:50:33 -040034struct map_lookup {
35 u64 type;
36 int io_align;
37 int io_width;
38 int stripe_len;
39 int sector_size;
40 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040041 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040042 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040043};
44
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49
50
Chris Mason593060d2008-03-25 16:50:33 -040051#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040052 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040053
Chris Mason8a4b83c2008-03-24 15:02:07 -040054static DEFINE_MUTEX(uuid_mutex);
55static LIST_HEAD(fs_uuids);
56
Chris Masona061fc82008-05-07 11:43:44 -040057void btrfs_lock_volumes(void)
58{
59 mutex_lock(&uuid_mutex);
60}
61
62void btrfs_unlock_volumes(void)
63{
64 mutex_unlock(&uuid_mutex);
65}
66
Chris Mason7d9eb122008-07-08 14:19:17 -040067static void lock_chunks(struct btrfs_root *root)
68{
Chris Mason7d9eb122008-07-08 14:19:17 -040069 mutex_lock(&root->fs_info->chunk_mutex);
70}
71
72static void unlock_chunks(struct btrfs_root *root)
73{
Chris Mason7d9eb122008-07-08 14:19:17 -040074 mutex_unlock(&root->fs_info->chunk_mutex);
75}
76
Chris Mason8a4b83c2008-03-24 15:02:07 -040077int btrfs_cleanup_fs_uuids(void)
78{
79 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040080 struct btrfs_device *dev;
81
Yan Zheng2b820322008-11-17 21:11:30 -050082 while (!list_empty(&fs_uuids)) {
83 fs_devices = list_entry(fs_uuids.next,
84 struct btrfs_fs_devices, list);
85 list_del(&fs_devices->list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040086 while(!list_empty(&fs_devices->devices)) {
Yan Zheng2b820322008-11-17 21:11:30 -050087 dev = list_entry(fs_devices->devices.next,
88 struct btrfs_device, dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040089 if (dev->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -050090 close_bdev_exclusive(dev->bdev, dev->mode);
Chris Masona0af4692008-05-13 16:03:06 -040091 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -040092 }
Yan Zheng2b820322008-11-17 21:11:30 -050093 fs_devices->num_devices--;
94 if (dev->writeable)
95 fs_devices->rw_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -040096 list_del(&dev->dev_list);
Yan Zheng2b820322008-11-17 21:11:30 -050097 list_del(&dev->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -040098 kfree(dev->name);
Chris Mason8a4b83c2008-03-24 15:02:07 -040099 kfree(dev);
100 }
Yan Zheng2b820322008-11-17 21:11:30 -0500101 WARN_ON(fs_devices->num_devices);
102 WARN_ON(fs_devices->open_devices);
103 WARN_ON(fs_devices->rw_devices);
104 kfree(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105 }
106 return 0;
107}
108
Chris Masona1b32a52008-09-05 16:09:51 -0400109static noinline struct btrfs_device *__find_device(struct list_head *head,
110 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400111{
112 struct btrfs_device *dev;
113 struct list_head *cur;
114
115 list_for_each(cur, head) {
116 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masona4437552008-04-18 10:29:38 -0400117 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400118 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400120 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400121 }
122 return NULL;
123}
124
Chris Masona1b32a52008-09-05 16:09:51 -0400125static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400126{
127 struct list_head *cur;
128 struct btrfs_fs_devices *fs_devices;
129
130 list_for_each(cur, &fs_uuids) {
131 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
132 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
133 return fs_devices;
134 }
135 return NULL;
136}
137
Chris Mason8b712842008-06-11 16:50:36 -0400138/*
139 * we try to collect pending bios for a device so we don't get a large
140 * number of procs sending bios down to the same device. This greatly
141 * improves the schedulers ability to collect and merge the bios.
142 *
143 * But, it also turns into a long list of bios to process and that is sure
144 * to eventually make the worker thread block. The solution here is to
145 * make some progress and then put this work struct back at the end of
146 * the list if the block device is congested. This way, multiple devices
147 * can make progress from a single worker thread.
148 */
Chris Masona1b32a52008-09-05 16:09:51 -0400149static int noinline run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400150{
151 struct bio *pending;
152 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400153 struct btrfs_fs_info *fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400154 struct bio *tail;
155 struct bio *cur;
156 int again = 0;
157 unsigned long num_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400158 unsigned long limit;
Chris Mason8b712842008-06-11 16:50:36 -0400159
160 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
Chris Masonb64a2852008-08-20 13:39:41 -0400161 fs_info = device->dev_root->fs_info;
162 limit = btrfs_async_submit_limit(fs_info);
163 limit = limit * 2 / 3;
164
Chris Mason8b712842008-06-11 16:50:36 -0400165loop:
166 spin_lock(&device->io_lock);
167
168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
172 */
173 pending = device->pending_bios;
174 tail = device->pending_bio_tail;
175 WARN_ON(pending && !tail);
176 device->pending_bios = NULL;
177 device->pending_bio_tail = NULL;
178
179 /*
180 * if pending was null this time around, no bios need processing
181 * at all and we can stop. Otherwise it'll loop back up again
182 * and do an additional check so no bios are missed.
183 *
184 * device->running_pending is used to synchronize with the
185 * schedule_bio code.
186 */
187 if (pending) {
188 again = 1;
189 device->running_pending = 1;
190 } else {
191 again = 0;
192 device->running_pending = 0;
193 }
194 spin_unlock(&device->io_lock);
195
196 while(pending) {
197 cur = pending;
198 pending = pending->bi_next;
199 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400200 atomic_dec(&fs_info->nr_async_bios);
201
202 if (atomic_read(&fs_info->nr_async_bios) < limit &&
203 waitqueue_active(&fs_info->async_submit_wait))
204 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400205
206 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
207 bio_get(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400208 submit_bio(cur->bi_rw, cur);
Chris Mason492bb6de2008-07-31 16:29:02 -0400209 bio_put(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400210 num_run++;
211
212 /*
213 * we made progress, there is more work to do and the bdi
214 * is now congested. Back off and let other work structs
215 * run instead
216 */
Chris Mason5f2cc082008-11-07 18:22:45 -0500217 if (pending && bdi_write_congested(bdi) &&
218 fs_info->fs_devices->open_devices > 1) {
Chris Mason8b712842008-06-11 16:50:36 -0400219 struct bio *old_head;
220
221 spin_lock(&device->io_lock);
Chris Mason492bb6de2008-07-31 16:29:02 -0400222
Chris Mason8b712842008-06-11 16:50:36 -0400223 old_head = device->pending_bios;
224 device->pending_bios = pending;
225 if (device->pending_bio_tail)
226 tail->bi_next = old_head;
227 else
228 device->pending_bio_tail = tail;
229
230 spin_unlock(&device->io_lock);
231 btrfs_requeue_work(&device->work);
232 goto done;
233 }
234 }
235 if (again)
236 goto loop;
237done:
238 return 0;
239}
240
Christoph Hellwigb2950862008-12-02 09:54:17 -0500241static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400242{
243 struct btrfs_device *device;
244
245 device = container_of(work, struct btrfs_device, work);
246 run_scheduled_bios(device);
247}
248
Chris Masona1b32a52008-09-05 16:09:51 -0400249static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400250 struct btrfs_super_block *disk_super,
251 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
252{
253 struct btrfs_device *device;
254 struct btrfs_fs_devices *fs_devices;
255 u64 found_transid = btrfs_super_generation(disk_super);
256
257 fs_devices = find_fsid(disk_super->fsid);
258 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400259 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400260 if (!fs_devices)
261 return -ENOMEM;
262 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400263 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400264 list_add(&fs_devices->list, &fs_uuids);
265 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
266 fs_devices->latest_devid = devid;
267 fs_devices->latest_trans = found_transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400268 device = NULL;
269 } else {
Chris Masona4437552008-04-18 10:29:38 -0400270 device = __find_device(&fs_devices->devices, devid,
271 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400272 }
273 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500274 if (fs_devices->opened)
275 return -EBUSY;
276
Chris Mason8a4b83c2008-03-24 15:02:07 -0400277 device = kzalloc(sizeof(*device), GFP_NOFS);
278 if (!device) {
279 /* we can safely leave the fs_devices entry around */
280 return -ENOMEM;
281 }
282 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400283 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400284 memcpy(device->uuid, disk_super->dev_item.uuid,
285 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400286 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400287 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400288 device->name = kstrdup(path, GFP_NOFS);
289 if (!device->name) {
290 kfree(device);
291 return -ENOMEM;
292 }
Yan Zheng2b820322008-11-17 21:11:30 -0500293 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400294 list_add(&device->dev_list, &fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500295 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400296 fs_devices->num_devices++;
297 }
298
299 if (found_transid > fs_devices->latest_trans) {
300 fs_devices->latest_devid = devid;
301 fs_devices->latest_trans = found_transid;
302 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400303 *fs_devices_ret = fs_devices;
304 return 0;
305}
306
Chris Masondfe25022008-05-13 13:46:40 -0400307int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
308{
Yan Zheng2b820322008-11-17 21:11:30 -0500309 struct list_head *tmp;
Chris Masondfe25022008-05-13 13:46:40 -0400310 struct list_head *cur;
311 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500312 int seed_devices = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400313
314 mutex_lock(&uuid_mutex);
315again:
Yan Zheng2b820322008-11-17 21:11:30 -0500316 list_for_each_safe(cur, tmp, &fs_devices->devices) {
Chris Masondfe25022008-05-13 13:46:40 -0400317 device = list_entry(cur, struct btrfs_device, dev_list);
Yan Zheng2b820322008-11-17 21:11:30 -0500318 if (device->in_fs_metadata)
319 continue;
320
321 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500322 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500323 device->bdev = NULL;
324 fs_devices->open_devices--;
325 }
326 if (device->writeable) {
327 list_del_init(&device->dev_alloc_list);
328 device->writeable = 0;
329 fs_devices->rw_devices--;
330 }
331 if (!seed_devices) {
332 list_del_init(&device->dev_list);
Chris Masondfe25022008-05-13 13:46:40 -0400333 fs_devices->num_devices--;
334 kfree(device->name);
335 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400336 }
337 }
Yan Zheng2b820322008-11-17 21:11:30 -0500338
339 if (fs_devices->seed) {
340 fs_devices = fs_devices->seed;
341 seed_devices = 1;
342 goto again;
343 }
344
Chris Masondfe25022008-05-13 13:46:40 -0400345 mutex_unlock(&uuid_mutex);
346 return 0;
347}
Chris Masona0af4692008-05-13 16:03:06 -0400348
Yan Zheng2b820322008-11-17 21:11:30 -0500349static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400350{
Yan Zheng2b820322008-11-17 21:11:30 -0500351 struct btrfs_fs_devices *seed_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400352 struct list_head *cur;
353 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500354again:
355 if (--fs_devices->opened > 0)
356 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400357
Yan Zheng2b820322008-11-17 21:11:30 -0500358 list_for_each(cur, &fs_devices->devices) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359 device = list_entry(cur, struct btrfs_device, dev_list);
360 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500361 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400362 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400363 }
Yan Zheng2b820322008-11-17 21:11:30 -0500364 if (device->writeable) {
365 list_del_init(&device->dev_alloc_list);
366 fs_devices->rw_devices--;
367 }
368
Chris Mason8a4b83c2008-03-24 15:02:07 -0400369 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500370 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400371 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400372 }
Yan Zheng2b820322008-11-17 21:11:30 -0500373 fs_devices->opened = 0;
374 fs_devices->seeding = 0;
375 fs_devices->sprouted = 0;
376
377 seed_devices = fs_devices->seed;
378 fs_devices->seed = NULL;
379 if (seed_devices) {
380 fs_devices = seed_devices;
381 goto again;
382 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400383 return 0;
384}
385
Yan Zheng2b820322008-11-17 21:11:30 -0500386int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
387{
388 int ret;
389
390 mutex_lock(&uuid_mutex);
391 ret = __btrfs_close_devices(fs_devices);
392 mutex_unlock(&uuid_mutex);
393 return ret;
394}
395
Chris Mason15916de2008-11-19 21:17:22 -0500396int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
397 int flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400398{
399 struct block_device *bdev;
400 struct list_head *head = &fs_devices->devices;
401 struct list_head *cur;
402 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400403 struct block_device *latest_bdev = NULL;
404 struct buffer_head *bh;
405 struct btrfs_super_block *disk_super;
406 u64 latest_devid = 0;
407 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400408 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500409 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400410 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411
Chris Mason8a4b83c2008-03-24 15:02:07 -0400412 list_for_each(cur, head) {
413 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Masonc1c4d912008-05-08 15:05:58 -0400414 if (device->bdev)
415 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400416 if (!device->name)
417 continue;
418
Chris Mason15916de2008-11-19 21:17:22 -0500419 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400420 if (IS_ERR(bdev)) {
421 printk("open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400422 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400423 }
Chris Masona061fc82008-05-07 11:43:44 -0400424 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400425
426 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
427 if (!bh)
428 goto error_close;
429
430 disk_super = (struct btrfs_super_block *)bh->b_data;
431 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
432 sizeof(disk_super->magic)))
433 goto error_brelse;
434
435 devid = le64_to_cpu(disk_super->dev_item.devid);
436 if (devid != device->devid)
437 goto error_brelse;
438
Yan Zheng2b820322008-11-17 21:11:30 -0500439 if (memcmp(device->uuid, disk_super->dev_item.uuid,
440 BTRFS_UUID_SIZE))
441 goto error_brelse;
442
443 device->generation = btrfs_super_generation(disk_super);
444 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400445 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500446 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400447 latest_bdev = bdev;
448 }
449
Yan Zheng2b820322008-11-17 21:11:30 -0500450 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
451 device->writeable = 0;
452 } else {
453 device->writeable = !bdev_read_only(bdev);
454 seeding = 0;
455 }
456
Chris Mason8a4b83c2008-03-24 15:02:07 -0400457 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400458 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500459 device->mode = flags;
460
Chris Masona0af4692008-05-13 16:03:06 -0400461 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500462 if (device->writeable) {
463 fs_devices->rw_devices++;
464 list_add(&device->dev_alloc_list,
465 &fs_devices->alloc_list);
466 }
Chris Masona0af4692008-05-13 16:03:06 -0400467 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400468
Chris Masona0af4692008-05-13 16:03:06 -0400469error_brelse:
470 brelse(bh);
471error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500472 close_bdev_exclusive(bdev, MS_RDONLY);
Chris Masona0af4692008-05-13 16:03:06 -0400473error:
474 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400475 }
Chris Masona0af4692008-05-13 16:03:06 -0400476 if (fs_devices->open_devices == 0) {
477 ret = -EIO;
478 goto out;
479 }
Yan Zheng2b820322008-11-17 21:11:30 -0500480 fs_devices->seeding = seeding;
481 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400482 fs_devices->latest_bdev = latest_bdev;
483 fs_devices->latest_devid = latest_devid;
484 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500485 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400486out:
Yan Zheng2b820322008-11-17 21:11:30 -0500487 return ret;
488}
489
490int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
491 int flags, void *holder)
492{
493 int ret;
494
495 mutex_lock(&uuid_mutex);
496 if (fs_devices->opened) {
497 if (fs_devices->sprouted) {
498 ret = -EBUSY;
499 } else {
500 fs_devices->opened++;
501 ret = 0;
502 }
503 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500504 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500505 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400506 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400507 return ret;
508}
509
510int btrfs_scan_one_device(const char *path, int flags, void *holder,
511 struct btrfs_fs_devices **fs_devices_ret)
512{
513 struct btrfs_super_block *disk_super;
514 struct block_device *bdev;
515 struct buffer_head *bh;
516 int ret;
517 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400518 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519
520 mutex_lock(&uuid_mutex);
521
Chris Mason15916de2008-11-19 21:17:22 -0500522 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400523
524 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400525 ret = PTR_ERR(bdev);
526 goto error;
527 }
528
529 ret = set_blocksize(bdev, 4096);
530 if (ret)
531 goto error_close;
532 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
533 if (!bh) {
534 ret = -EIO;
535 goto error_close;
536 }
537 disk_super = (struct btrfs_super_block *)bh->b_data;
538 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
539 sizeof(disk_super->magic))) {
Yane58ca022008-04-01 11:21:34 -0400540 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400541 goto error_brelse;
542 }
543 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400544 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400545 if (disk_super->label[0])
546 printk("device label %s ", disk_super->label);
547 else {
548 /* FIXME, make a readl uuid parser */
549 printk("device fsid %llx-%llx ",
550 *(unsigned long long *)disk_super->fsid,
551 *(unsigned long long *)(disk_super->fsid + 8));
552 }
553 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400554 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
555
556error_brelse:
557 brelse(bh);
558error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500559 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400560error:
561 mutex_unlock(&uuid_mutex);
562 return ret;
563}
Chris Mason0b86a832008-03-24 15:01:56 -0400564
565/*
566 * this uses a pretty simple search, the expectation is that it is
567 * called very infrequently and that a given device has a small number
568 * of extents
569 */
Chris Masona1b32a52008-09-05 16:09:51 -0400570static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
571 struct btrfs_device *device,
Chris Masona1b32a52008-09-05 16:09:51 -0400572 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400573{
574 struct btrfs_key key;
575 struct btrfs_root *root = device->dev_root;
576 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500577 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400578 u64 hole_size = 0;
579 u64 last_byte = 0;
580 u64 search_start = 0;
581 u64 search_end = device->total_bytes;
582 int ret;
583 int slot = 0;
584 int start_found;
585 struct extent_buffer *l;
586
Yan Zheng2b820322008-11-17 21:11:30 -0500587 path = btrfs_alloc_path();
588 if (!path)
589 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400590 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500591 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400592
593 /* FIXME use last free of some kind */
594
Chris Mason8a4b83c2008-03-24 15:02:07 -0400595 /* we don't want to overwrite the superblock on the drive,
596 * so we make sure to start at an offset of at least 1MB
597 */
598 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400599
600 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
601 search_start = max(root->fs_info->alloc_start, search_start);
602
Chris Mason0b86a832008-03-24 15:01:56 -0400603 key.objectid = device->devid;
604 key.offset = search_start;
605 key.type = BTRFS_DEV_EXTENT_KEY;
606 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
607 if (ret < 0)
608 goto error;
609 ret = btrfs_previous_item(root, path, 0, key.type);
610 if (ret < 0)
611 goto error;
612 l = path->nodes[0];
613 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
614 while (1) {
615 l = path->nodes[0];
616 slot = path->slots[0];
617 if (slot >= btrfs_header_nritems(l)) {
618 ret = btrfs_next_leaf(root, path);
619 if (ret == 0)
620 continue;
621 if (ret < 0)
622 goto error;
623no_more_items:
624 if (!start_found) {
625 if (search_start >= search_end) {
626 ret = -ENOSPC;
627 goto error;
628 }
629 *start = search_start;
630 start_found = 1;
631 goto check_pending;
632 }
633 *start = last_byte > search_start ?
634 last_byte : search_start;
635 if (search_end <= *start) {
636 ret = -ENOSPC;
637 goto error;
638 }
639 goto check_pending;
640 }
641 btrfs_item_key_to_cpu(l, &key, slot);
642
643 if (key.objectid < device->devid)
644 goto next;
645
646 if (key.objectid > device->devid)
647 goto no_more_items;
648
649 if (key.offset >= search_start && key.offset > last_byte &&
650 start_found) {
651 if (last_byte < search_start)
652 last_byte = search_start;
653 hole_size = key.offset - last_byte;
654 if (key.offset > last_byte &&
655 hole_size >= num_bytes) {
656 *start = last_byte;
657 goto check_pending;
658 }
659 }
660 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
661 goto next;
662 }
663
664 start_found = 1;
665 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
666 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
667next:
668 path->slots[0]++;
669 cond_resched();
670 }
671check_pending:
672 /* we have to make sure we didn't find an extent that has already
673 * been allocated by the map tree or the original allocation
674 */
Chris Mason0b86a832008-03-24 15:01:56 -0400675 BUG_ON(*start < search_start);
676
Chris Mason6324fbf2008-03-24 15:01:59 -0400677 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400678 ret = -ENOSPC;
679 goto error;
680 }
681 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500682 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400683
684error:
Yan Zheng2b820322008-11-17 21:11:30 -0500685 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400686 return ret;
687}
688
Christoph Hellwigb2950862008-12-02 09:54:17 -0500689static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400690 struct btrfs_device *device,
691 u64 start)
692{
693 int ret;
694 struct btrfs_path *path;
695 struct btrfs_root *root = device->dev_root;
696 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400697 struct btrfs_key found_key;
698 struct extent_buffer *leaf = NULL;
699 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400700
701 path = btrfs_alloc_path();
702 if (!path)
703 return -ENOMEM;
704
705 key.objectid = device->devid;
706 key.offset = start;
707 key.type = BTRFS_DEV_EXTENT_KEY;
708
709 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400710 if (ret > 0) {
711 ret = btrfs_previous_item(root, path, key.objectid,
712 BTRFS_DEV_EXTENT_KEY);
713 BUG_ON(ret);
714 leaf = path->nodes[0];
715 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
716 extent = btrfs_item_ptr(leaf, path->slots[0],
717 struct btrfs_dev_extent);
718 BUG_ON(found_key.offset > start || found_key.offset +
719 btrfs_dev_extent_length(leaf, extent) < start);
720 ret = 0;
721 } else if (ret == 0) {
722 leaf = path->nodes[0];
723 extent = btrfs_item_ptr(leaf, path->slots[0],
724 struct btrfs_dev_extent);
725 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400726 BUG_ON(ret);
727
Chris Masondfe25022008-05-13 13:46:40 -0400728 if (device->bytes_used > 0)
729 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400730 ret = btrfs_del_item(trans, root, path);
731 BUG_ON(ret);
732
733 btrfs_free_path(path);
734 return ret;
735}
736
Yan Zheng2b820322008-11-17 21:11:30 -0500737int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400738 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400739 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500740 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400741{
742 int ret;
743 struct btrfs_path *path;
744 struct btrfs_root *root = device->dev_root;
745 struct btrfs_dev_extent *extent;
746 struct extent_buffer *leaf;
747 struct btrfs_key key;
748
Chris Masondfe25022008-05-13 13:46:40 -0400749 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400750 path = btrfs_alloc_path();
751 if (!path)
752 return -ENOMEM;
753
Chris Mason0b86a832008-03-24 15:01:56 -0400754 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500755 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400756 key.type = BTRFS_DEV_EXTENT_KEY;
757 ret = btrfs_insert_empty_item(trans, root, path, &key,
758 sizeof(*extent));
759 BUG_ON(ret);
760
761 leaf = path->nodes[0];
762 extent = btrfs_item_ptr(leaf, path->slots[0],
763 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400764 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
765 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
766 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
767
768 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
769 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
770 BTRFS_UUID_SIZE);
771
Chris Mason0b86a832008-03-24 15:01:56 -0400772 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
773 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400774 btrfs_free_path(path);
775 return ret;
776}
777
Chris Masona1b32a52008-09-05 16:09:51 -0400778static noinline int find_next_chunk(struct btrfs_root *root,
779 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400780{
781 struct btrfs_path *path;
782 int ret;
783 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400784 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400785 struct btrfs_key found_key;
786
787 path = btrfs_alloc_path();
788 BUG_ON(!path);
789
Chris Masone17cade2008-04-15 15:41:47 -0400790 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400791 key.offset = (u64)-1;
792 key.type = BTRFS_CHUNK_ITEM_KEY;
793
794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
795 if (ret < 0)
796 goto error;
797
798 BUG_ON(ret == 0);
799
800 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
801 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400802 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400803 } else {
804 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
805 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400806 if (found_key.objectid != objectid)
807 *offset = 0;
808 else {
809 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
810 struct btrfs_chunk);
811 *offset = found_key.offset +
812 btrfs_chunk_length(path->nodes[0], chunk);
813 }
Chris Mason0b86a832008-03-24 15:01:56 -0400814 }
815 ret = 0;
816error:
817 btrfs_free_path(path);
818 return ret;
819}
820
Yan Zheng2b820322008-11-17 21:11:30 -0500821static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400822{
823 int ret;
824 struct btrfs_key key;
825 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500826 struct btrfs_path *path;
827
828 root = root->fs_info->chunk_root;
829
830 path = btrfs_alloc_path();
831 if (!path)
832 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400833
834 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
835 key.type = BTRFS_DEV_ITEM_KEY;
836 key.offset = (u64)-1;
837
838 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
839 if (ret < 0)
840 goto error;
841
842 BUG_ON(ret == 0);
843
844 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
845 BTRFS_DEV_ITEM_KEY);
846 if (ret) {
847 *objectid = 1;
848 } else {
849 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
850 path->slots[0]);
851 *objectid = found_key.offset + 1;
852 }
853 ret = 0;
854error:
Yan Zheng2b820322008-11-17 21:11:30 -0500855 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400856 return ret;
857}
858
859/*
860 * the device information is stored in the chunk root
861 * the btrfs_device struct should be fully filled in
862 */
863int btrfs_add_device(struct btrfs_trans_handle *trans,
864 struct btrfs_root *root,
865 struct btrfs_device *device)
866{
867 int ret;
868 struct btrfs_path *path;
869 struct btrfs_dev_item *dev_item;
870 struct extent_buffer *leaf;
871 struct btrfs_key key;
872 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -0400873
874 root = root->fs_info->chunk_root;
875
876 path = btrfs_alloc_path();
877 if (!path)
878 return -ENOMEM;
879
Chris Mason0b86a832008-03-24 15:01:56 -0400880 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
881 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -0500882 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400883
884 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400885 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400886 if (ret)
887 goto out;
888
889 leaf = path->nodes[0];
890 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
891
892 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -0500893 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400894 btrfs_set_device_type(leaf, dev_item, device->type);
895 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
896 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
897 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400898 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
899 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400900 btrfs_set_device_group(leaf, dev_item, 0);
901 btrfs_set_device_seek_speed(leaf, dev_item, 0);
902 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400903
Chris Mason0b86a832008-03-24 15:01:56 -0400904 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400905 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500906 ptr = (unsigned long)btrfs_device_fsid(dev_item);
907 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400908 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400909
Yan Zheng2b820322008-11-17 21:11:30 -0500910 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400911out:
912 btrfs_free_path(path);
913 return ret;
914}
Chris Mason8f18cf12008-04-25 16:53:30 -0400915
Chris Masona061fc82008-05-07 11:43:44 -0400916static int btrfs_rm_dev_item(struct btrfs_root *root,
917 struct btrfs_device *device)
918{
919 int ret;
920 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -0400921 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400922 struct btrfs_trans_handle *trans;
923
924 root = root->fs_info->chunk_root;
925
926 path = btrfs_alloc_path();
927 if (!path)
928 return -ENOMEM;
929
930 trans = btrfs_start_transaction(root, 1);
931 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
932 key.type = BTRFS_DEV_ITEM_KEY;
933 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400934 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400935
936 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
937 if (ret < 0)
938 goto out;
939
940 if (ret > 0) {
941 ret = -ENOENT;
942 goto out;
943 }
944
945 ret = btrfs_del_item(trans, root, path);
946 if (ret)
947 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400948out:
949 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -0400950 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400951 btrfs_commit_transaction(trans, root);
952 return ret;
953}
954
955int btrfs_rm_device(struct btrfs_root *root, char *device_path)
956{
957 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500958 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -0400959 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400960 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -0400961 struct btrfs_super_block *disk_super;
962 u64 all_avail;
963 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500964 u64 num_devices;
965 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -0400966 int ret = 0;
967
Chris Masona061fc82008-05-07 11:43:44 -0400968 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -0400969 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -0400970
971 all_avail = root->fs_info->avail_data_alloc_bits |
972 root->fs_info->avail_system_alloc_bits |
973 root->fs_info->avail_metadata_alloc_bits;
974
975 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -0500976 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masona061fc82008-05-07 11:43:44 -0400977 printk("btrfs: unable to go below four devices on raid10\n");
978 ret = -EINVAL;
979 goto out;
980 }
981
982 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -0500983 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masona061fc82008-05-07 11:43:44 -0400984 printk("btrfs: unable to go below two devices on raid1\n");
985 ret = -EINVAL;
986 goto out;
987 }
988
Chris Masondfe25022008-05-13 13:46:40 -0400989 if (strcmp(device_path, "missing") == 0) {
990 struct list_head *cur;
991 struct list_head *devices;
992 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -0400993
Chris Masondfe25022008-05-13 13:46:40 -0400994 device = NULL;
995 devices = &root->fs_info->fs_devices->devices;
996 list_for_each(cur, devices) {
997 tmp = list_entry(cur, struct btrfs_device, dev_list);
998 if (tmp->in_fs_metadata && !tmp->bdev) {
999 device = tmp;
1000 break;
1001 }
1002 }
1003 bdev = NULL;
1004 bh = NULL;
1005 disk_super = NULL;
1006 if (!device) {
1007 printk("btrfs: no missing devices found to remove\n");
1008 goto out;
1009 }
Chris Masondfe25022008-05-13 13:46:40 -04001010 } else {
Chris Mason15916de2008-11-19 21:17:22 -05001011 bdev = open_bdev_exclusive(device_path, MS_RDONLY,
Chris Masondfe25022008-05-13 13:46:40 -04001012 root->fs_info->bdev_holder);
1013 if (IS_ERR(bdev)) {
1014 ret = PTR_ERR(bdev);
1015 goto out;
1016 }
1017
Yan Zheng2b820322008-11-17 21:11:30 -05001018 set_blocksize(bdev, 4096);
Chris Masondfe25022008-05-13 13:46:40 -04001019 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
1020 if (!bh) {
1021 ret = -EIO;
1022 goto error_close;
1023 }
1024 disk_super = (struct btrfs_super_block *)bh->b_data;
1025 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
Yan Zheng2b820322008-11-17 21:11:30 -05001026 sizeof(disk_super->magic))) {
Chris Masondfe25022008-05-13 13:46:40 -04001027 ret = -ENOENT;
1028 goto error_brelse;
1029 }
1030 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001031 dev_uuid = disk_super->dev_item.uuid;
1032 device = btrfs_find_device(root, devid, dev_uuid,
1033 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001034 if (!device) {
1035 ret = -ENOENT;
1036 goto error_brelse;
1037 }
Chris Masondfe25022008-05-13 13:46:40 -04001038 }
Yan Zheng2b820322008-11-17 21:11:30 -05001039
1040 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1041 printk("btrfs: unable to remove the only writeable device\n");
1042 ret = -EINVAL;
1043 goto error_brelse;
1044 }
1045
1046 if (device->writeable) {
1047 list_del_init(&device->dev_alloc_list);
1048 root->fs_info->fs_devices->rw_devices--;
1049 }
Chris Masona061fc82008-05-07 11:43:44 -04001050
1051 ret = btrfs_shrink_device(device, 0);
1052 if (ret)
1053 goto error_brelse;
1054
Chris Masona061fc82008-05-07 11:43:44 -04001055 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1056 if (ret)
1057 goto error_brelse;
1058
Yan Zheng2b820322008-11-17 21:11:30 -05001059 device->in_fs_metadata = 0;
1060 if (device->fs_devices == root->fs_info->fs_devices) {
1061 list_del_init(&device->dev_list);
1062 root->fs_info->fs_devices->num_devices--;
1063 if (device->bdev)
1064 device->fs_devices->open_devices--;
1065 }
1066
1067 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1068 struct btrfs_device, dev_list);
1069 if (device->bdev == root->fs_info->sb->s_bdev)
1070 root->fs_info->sb->s_bdev = next_device->bdev;
1071 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1072 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1073
1074 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1075 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1076
1077 if (device->fs_devices != root->fs_info->fs_devices) {
1078 BUG_ON(device->writeable);
1079 brelse(bh);
1080 if (bdev)
Chris Mason15916de2008-11-19 21:17:22 -05001081 close_bdev_exclusive(bdev, MS_RDONLY);
Yan Zheng2b820322008-11-17 21:11:30 -05001082
1083 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -05001084 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001085 device->bdev = NULL;
1086 device->fs_devices->open_devices--;
1087 }
1088 if (device->fs_devices->open_devices == 0) {
1089 struct btrfs_fs_devices *fs_devices;
1090 fs_devices = root->fs_info->fs_devices;
1091 while (fs_devices) {
1092 if (fs_devices->seed == device->fs_devices)
1093 break;
1094 fs_devices = fs_devices->seed;
1095 }
1096 fs_devices->seed = device->fs_devices->seed;
1097 device->fs_devices->seed = NULL;
1098 __btrfs_close_devices(device->fs_devices);
1099 }
1100 ret = 0;
1101 goto out;
1102 }
1103
1104 /*
1105 * at this point, the device is zero sized. We want to
1106 * remove it from the devices list and zero out the old super
1107 */
1108 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001109 /* make sure this device isn't detected as part of
1110 * the FS anymore
1111 */
1112 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1113 set_buffer_dirty(bh);
1114 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001115 }
Yan Zheng2b820322008-11-17 21:11:30 -05001116 brelse(bh);
Chris Masona061fc82008-05-07 11:43:44 -04001117
Chris Masondfe25022008-05-13 13:46:40 -04001118 if (device->bdev) {
1119 /* one close for the device struct or super_block */
Chris Mason15916de2008-11-19 21:17:22 -05001120 close_bdev_exclusive(device->bdev, device->mode);
Chris Masondfe25022008-05-13 13:46:40 -04001121 }
1122 if (bdev) {
1123 /* one close for us */
Chris Mason15916de2008-11-19 21:17:22 -05001124 close_bdev_exclusive(bdev, MS_RDONLY);
Chris Masondfe25022008-05-13 13:46:40 -04001125 }
Chris Masona061fc82008-05-07 11:43:44 -04001126 kfree(device->name);
1127 kfree(device);
1128 ret = 0;
1129 goto out;
1130
1131error_brelse:
1132 brelse(bh);
1133error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001134 if (bdev)
Chris Mason15916de2008-11-19 21:17:22 -05001135 close_bdev_exclusive(bdev, MS_RDONLY);
Chris Masona061fc82008-05-07 11:43:44 -04001136out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001137 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001138 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001139 return ret;
1140}
1141
Yan Zheng2b820322008-11-17 21:11:30 -05001142/*
1143 * does all the dirty work required for changing file system's UUID.
1144 */
1145static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1146 struct btrfs_root *root)
1147{
1148 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1149 struct btrfs_fs_devices *old_devices;
1150 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1151 struct btrfs_device *device;
1152 u64 super_flags;
1153
1154 BUG_ON(!mutex_is_locked(&uuid_mutex));
1155 if (!fs_devices->seeding || fs_devices->opened != 1)
1156 return -EINVAL;
1157
1158 old_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1159 if (!old_devices)
1160 return -ENOMEM;
1161
1162 memcpy(old_devices, fs_devices, sizeof(*old_devices));
1163 old_devices->opened = 1;
1164 old_devices->sprouted = 1;
1165 INIT_LIST_HEAD(&old_devices->devices);
1166 INIT_LIST_HEAD(&old_devices->alloc_list);
1167 list_splice_init(&fs_devices->devices, &old_devices->devices);
1168 list_splice_init(&fs_devices->alloc_list, &old_devices->alloc_list);
1169 list_for_each_entry(device, &old_devices->devices, dev_list) {
1170 device->fs_devices = old_devices;
1171 }
1172 list_add(&old_devices->list, &fs_uuids);
1173
1174 fs_devices->seeding = 0;
1175 fs_devices->num_devices = 0;
1176 fs_devices->open_devices = 0;
1177 fs_devices->seed = old_devices;
1178
1179 generate_random_uuid(fs_devices->fsid);
1180 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1181 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1182 super_flags = btrfs_super_flags(disk_super) &
1183 ~BTRFS_SUPER_FLAG_SEEDING;
1184 btrfs_set_super_flags(disk_super, super_flags);
1185
1186 return 0;
1187}
1188
1189/*
1190 * strore the expected generation for seed devices in device items.
1191 */
1192static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root)
1194{
1195 struct btrfs_path *path;
1196 struct extent_buffer *leaf;
1197 struct btrfs_dev_item *dev_item;
1198 struct btrfs_device *device;
1199 struct btrfs_key key;
1200 u8 fs_uuid[BTRFS_UUID_SIZE];
1201 u8 dev_uuid[BTRFS_UUID_SIZE];
1202 u64 devid;
1203 int ret;
1204
1205 path = btrfs_alloc_path();
1206 if (!path)
1207 return -ENOMEM;
1208
1209 root = root->fs_info->chunk_root;
1210 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1211 key.offset = 0;
1212 key.type = BTRFS_DEV_ITEM_KEY;
1213
1214 while (1) {
1215 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1216 if (ret < 0)
1217 goto error;
1218
1219 leaf = path->nodes[0];
1220next_slot:
1221 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1222 ret = btrfs_next_leaf(root, path);
1223 if (ret > 0)
1224 break;
1225 if (ret < 0)
1226 goto error;
1227 leaf = path->nodes[0];
1228 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1229 btrfs_release_path(root, path);
1230 continue;
1231 }
1232
1233 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1234 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1235 key.type != BTRFS_DEV_ITEM_KEY)
1236 break;
1237
1238 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1239 struct btrfs_dev_item);
1240 devid = btrfs_device_id(leaf, dev_item);
1241 read_extent_buffer(leaf, dev_uuid,
1242 (unsigned long)btrfs_device_uuid(dev_item),
1243 BTRFS_UUID_SIZE);
1244 read_extent_buffer(leaf, fs_uuid,
1245 (unsigned long)btrfs_device_fsid(dev_item),
1246 BTRFS_UUID_SIZE);
1247 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1248 BUG_ON(!device);
1249
1250 if (device->fs_devices->seeding) {
1251 btrfs_set_device_generation(leaf, dev_item,
1252 device->generation);
1253 btrfs_mark_buffer_dirty(leaf);
1254 }
1255
1256 path->slots[0]++;
1257 goto next_slot;
1258 }
1259 ret = 0;
1260error:
1261 btrfs_free_path(path);
1262 return ret;
1263}
1264
Chris Mason788f20e2008-04-28 15:29:42 -04001265int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1266{
1267 struct btrfs_trans_handle *trans;
1268 struct btrfs_device *device;
1269 struct block_device *bdev;
1270 struct list_head *cur;
1271 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001272 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001273 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001274 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001275 int ret = 0;
1276
Yan Zheng2b820322008-11-17 21:11:30 -05001277 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1278 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001279
Chris Mason15916de2008-11-19 21:17:22 -05001280 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Chris Mason788f20e2008-04-28 15:29:42 -04001281 if (!bdev) {
1282 return -EIO;
1283 }
Chris Masona2135012008-06-25 16:01:30 -04001284
Yan Zheng2b820322008-11-17 21:11:30 -05001285 if (root->fs_info->fs_devices->seeding) {
1286 seeding_dev = 1;
1287 down_write(&sb->s_umount);
1288 mutex_lock(&uuid_mutex);
1289 }
1290
Chris Mason8c8bee12008-09-29 11:19:10 -04001291 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001292 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001293
Chris Mason788f20e2008-04-28 15:29:42 -04001294 devices = &root->fs_info->fs_devices->devices;
1295 list_for_each(cur, devices) {
1296 device = list_entry(cur, struct btrfs_device, dev_list);
1297 if (device->bdev == bdev) {
1298 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001299 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001300 }
1301 }
1302
1303 device = kzalloc(sizeof(*device), GFP_NOFS);
1304 if (!device) {
1305 /* we can safely leave the fs_devices entry around */
1306 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001307 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001308 }
1309
Chris Mason788f20e2008-04-28 15:29:42 -04001310 device->name = kstrdup(device_path, GFP_NOFS);
1311 if (!device->name) {
1312 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001313 ret = -ENOMEM;
1314 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001315 }
Yan Zheng2b820322008-11-17 21:11:30 -05001316
1317 ret = find_next_devid(root, &device->devid);
1318 if (ret) {
1319 kfree(device);
1320 goto error;
1321 }
1322
1323 trans = btrfs_start_transaction(root, 1);
1324 lock_chunks(root);
1325
1326 device->barriers = 1;
1327 device->writeable = 1;
1328 device->work.func = pending_bios_fn;
1329 generate_random_uuid(device->uuid);
1330 spin_lock_init(&device->io_lock);
1331 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001332 device->io_width = root->sectorsize;
1333 device->io_align = root->sectorsize;
1334 device->sector_size = root->sectorsize;
1335 device->total_bytes = i_size_read(bdev->bd_inode);
1336 device->dev_root = root->fs_info->dev_root;
1337 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001338 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001339 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001340 set_blocksize(device->bdev, 4096);
1341
Yan Zheng2b820322008-11-17 21:11:30 -05001342 if (seeding_dev) {
1343 sb->s_flags &= ~MS_RDONLY;
1344 ret = btrfs_prepare_sprout(trans, root);
1345 BUG_ON(ret);
1346 }
1347
1348 device->fs_devices = root->fs_info->fs_devices;
1349 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1350 list_add(&device->dev_alloc_list,
1351 &root->fs_info->fs_devices->alloc_list);
1352 root->fs_info->fs_devices->num_devices++;
1353 root->fs_info->fs_devices->open_devices++;
1354 root->fs_info->fs_devices->rw_devices++;
1355 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1356
Chris Mason788f20e2008-04-28 15:29:42 -04001357 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1358 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1359 total_bytes + device->total_bytes);
1360
1361 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1362 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1363 total_bytes + 1);
1364
Yan Zheng2b820322008-11-17 21:11:30 -05001365 if (seeding_dev) {
1366 ret = init_first_rw_device(trans, root, device);
1367 BUG_ON(ret);
1368 ret = btrfs_finish_sprout(trans, root);
1369 BUG_ON(ret);
1370 } else {
1371 ret = btrfs_add_device(trans, root, device);
1372 }
1373
Chris Mason7d9eb122008-07-08 14:19:17 -04001374 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001375 btrfs_commit_transaction(trans, root);
1376
1377 if (seeding_dev) {
1378 mutex_unlock(&uuid_mutex);
1379 up_write(&sb->s_umount);
1380
1381 ret = btrfs_relocate_sys_chunks(root);
1382 BUG_ON(ret);
1383 }
1384out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001385 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001386 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001387error:
Chris Mason15916de2008-11-19 21:17:22 -05001388 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001389 if (seeding_dev) {
1390 mutex_unlock(&uuid_mutex);
1391 up_write(&sb->s_umount);
1392 }
Chris Mason788f20e2008-04-28 15:29:42 -04001393 goto out;
1394}
1395
Christoph Hellwigb2950862008-12-02 09:54:17 -05001396static int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
Chris Masona1b32a52008-09-05 16:09:51 -04001397 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001398{
1399 int ret;
1400 struct btrfs_path *path;
1401 struct btrfs_root *root;
1402 struct btrfs_dev_item *dev_item;
1403 struct extent_buffer *leaf;
1404 struct btrfs_key key;
1405
1406 root = device->dev_root->fs_info->chunk_root;
1407
1408 path = btrfs_alloc_path();
1409 if (!path)
1410 return -ENOMEM;
1411
1412 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1413 key.type = BTRFS_DEV_ITEM_KEY;
1414 key.offset = device->devid;
1415
1416 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1417 if (ret < 0)
1418 goto out;
1419
1420 if (ret > 0) {
1421 ret = -ENOENT;
1422 goto out;
1423 }
1424
1425 leaf = path->nodes[0];
1426 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1427
1428 btrfs_set_device_id(leaf, dev_item, device->devid);
1429 btrfs_set_device_type(leaf, dev_item, device->type);
1430 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1431 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1432 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001433 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1434 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1435 btrfs_mark_buffer_dirty(leaf);
1436
1437out:
1438 btrfs_free_path(path);
1439 return ret;
1440}
1441
Chris Mason7d9eb122008-07-08 14:19:17 -04001442static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001443 struct btrfs_device *device, u64 new_size)
1444{
1445 struct btrfs_super_block *super_copy =
1446 &device->dev_root->fs_info->super_copy;
1447 u64 old_total = btrfs_super_total_bytes(super_copy);
1448 u64 diff = new_size - device->total_bytes;
1449
Yan Zheng2b820322008-11-17 21:11:30 -05001450 if (!device->writeable)
1451 return -EACCES;
1452 if (new_size <= device->total_bytes)
1453 return -EINVAL;
1454
Chris Mason8f18cf12008-04-25 16:53:30 -04001455 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001456 device->fs_devices->total_rw_bytes += diff;
1457
1458 device->total_bytes = new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04001459 return btrfs_update_device(trans, device);
1460}
1461
Chris Mason7d9eb122008-07-08 14:19:17 -04001462int btrfs_grow_device(struct btrfs_trans_handle *trans,
1463 struct btrfs_device *device, u64 new_size)
1464{
1465 int ret;
1466 lock_chunks(device->dev_root);
1467 ret = __btrfs_grow_device(trans, device, new_size);
1468 unlock_chunks(device->dev_root);
1469 return ret;
1470}
1471
Chris Mason8f18cf12008-04-25 16:53:30 -04001472static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1473 struct btrfs_root *root,
1474 u64 chunk_tree, u64 chunk_objectid,
1475 u64 chunk_offset)
1476{
1477 int ret;
1478 struct btrfs_path *path;
1479 struct btrfs_key key;
1480
1481 root = root->fs_info->chunk_root;
1482 path = btrfs_alloc_path();
1483 if (!path)
1484 return -ENOMEM;
1485
1486 key.objectid = chunk_objectid;
1487 key.offset = chunk_offset;
1488 key.type = BTRFS_CHUNK_ITEM_KEY;
1489
1490 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1491 BUG_ON(ret);
1492
1493 ret = btrfs_del_item(trans, root, path);
1494 BUG_ON(ret);
1495
1496 btrfs_free_path(path);
1497 return 0;
1498}
1499
Christoph Hellwigb2950862008-12-02 09:54:17 -05001500static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001501 chunk_offset)
1502{
1503 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1504 struct btrfs_disk_key *disk_key;
1505 struct btrfs_chunk *chunk;
1506 u8 *ptr;
1507 int ret = 0;
1508 u32 num_stripes;
1509 u32 array_size;
1510 u32 len = 0;
1511 u32 cur;
1512 struct btrfs_key key;
1513
1514 array_size = btrfs_super_sys_array_size(super_copy);
1515
1516 ptr = super_copy->sys_chunk_array;
1517 cur = 0;
1518
1519 while (cur < array_size) {
1520 disk_key = (struct btrfs_disk_key *)ptr;
1521 btrfs_disk_key_to_cpu(&key, disk_key);
1522
1523 len = sizeof(*disk_key);
1524
1525 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1526 chunk = (struct btrfs_chunk *)(ptr + len);
1527 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1528 len += btrfs_chunk_item_size(num_stripes);
1529 } else {
1530 ret = -EIO;
1531 break;
1532 }
1533 if (key.objectid == chunk_objectid &&
1534 key.offset == chunk_offset) {
1535 memmove(ptr, ptr + len, array_size - (cur + len));
1536 array_size -= len;
1537 btrfs_set_super_sys_array_size(super_copy, array_size);
1538 } else {
1539 ptr += len;
1540 cur += len;
1541 }
1542 }
1543 return ret;
1544}
1545
Christoph Hellwigb2950862008-12-02 09:54:17 -05001546static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001547 u64 chunk_tree, u64 chunk_objectid,
1548 u64 chunk_offset)
1549{
1550 struct extent_map_tree *em_tree;
1551 struct btrfs_root *extent_root;
1552 struct btrfs_trans_handle *trans;
1553 struct extent_map *em;
1554 struct map_lookup *map;
1555 int ret;
1556 int i;
1557
Chris Mason323da792008-05-09 11:46:48 -04001558 printk("btrfs relocating chunk %llu\n",
1559 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001560 root = root->fs_info->chunk_root;
1561 extent_root = root->fs_info->extent_root;
1562 em_tree = &root->fs_info->mapping_tree.map_tree;
1563
1564 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001565 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001566 BUG_ON(ret);
1567
1568 trans = btrfs_start_transaction(root, 1);
1569 BUG_ON(!trans);
1570
Chris Mason7d9eb122008-07-08 14:19:17 -04001571 lock_chunks(root);
1572
Chris Mason8f18cf12008-04-25 16:53:30 -04001573 /*
1574 * step two, delete the device extents and the
1575 * chunk tree entries
1576 */
1577 spin_lock(&em_tree->lock);
1578 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1579 spin_unlock(&em_tree->lock);
1580
Chris Masona061fc82008-05-07 11:43:44 -04001581 BUG_ON(em->start > chunk_offset ||
1582 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001583 map = (struct map_lookup *)em->bdev;
1584
1585 for (i = 0; i < map->num_stripes; i++) {
1586 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1587 map->stripes[i].physical);
1588 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001589
Chris Masondfe25022008-05-13 13:46:40 -04001590 if (map->stripes[i].dev) {
1591 ret = btrfs_update_device(trans, map->stripes[i].dev);
1592 BUG_ON(ret);
1593 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001594 }
1595 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1596 chunk_offset);
1597
1598 BUG_ON(ret);
1599
1600 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1601 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1602 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001603 }
1604
Zheng Yan1a40e232008-09-26 10:09:34 -04001605 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1606 BUG_ON(ret);
1607
Chris Mason8f18cf12008-04-25 16:53:30 -04001608 spin_lock(&em_tree->lock);
1609 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001610 spin_unlock(&em_tree->lock);
1611
Chris Mason8f18cf12008-04-25 16:53:30 -04001612 kfree(map);
1613 em->bdev = NULL;
1614
1615 /* once for the tree */
1616 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001617 /* once for us */
1618 free_extent_map(em);
1619
Chris Mason7d9eb122008-07-08 14:19:17 -04001620 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001621 btrfs_end_transaction(trans, root);
1622 return 0;
1623}
1624
Yan Zheng2b820322008-11-17 21:11:30 -05001625static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1626{
1627 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1628 struct btrfs_path *path;
1629 struct extent_buffer *leaf;
1630 struct btrfs_chunk *chunk;
1631 struct btrfs_key key;
1632 struct btrfs_key found_key;
1633 u64 chunk_tree = chunk_root->root_key.objectid;
1634 u64 chunk_type;
1635 int ret;
1636
1637 path = btrfs_alloc_path();
1638 if (!path)
1639 return -ENOMEM;
1640
1641 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1642 key.offset = (u64)-1;
1643 key.type = BTRFS_CHUNK_ITEM_KEY;
1644
1645 while (1) {
1646 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1647 if (ret < 0)
1648 goto error;
1649 BUG_ON(ret == 0);
1650
1651 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1652 key.type);
1653 if (ret < 0)
1654 goto error;
1655 if (ret > 0)
1656 break;
1657
1658 leaf = path->nodes[0];
1659 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1660
1661 chunk = btrfs_item_ptr(leaf, path->slots[0],
1662 struct btrfs_chunk);
1663 chunk_type = btrfs_chunk_type(leaf, chunk);
1664 btrfs_release_path(chunk_root, path);
1665
1666 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1667 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1668 found_key.objectid,
1669 found_key.offset);
1670 BUG_ON(ret);
1671 }
1672
1673 if (found_key.offset == 0)
1674 break;
1675 key.offset = found_key.offset - 1;
1676 }
1677 ret = 0;
1678error:
1679 btrfs_free_path(path);
1680 return ret;
1681}
1682
Chris Masonec44a352008-04-28 15:29:52 -04001683static u64 div_factor(u64 num, int factor)
1684{
1685 if (factor == 10)
1686 return num;
1687 num *= factor;
1688 do_div(num, 10);
1689 return num;
1690}
1691
Chris Masonec44a352008-04-28 15:29:52 -04001692int btrfs_balance(struct btrfs_root *dev_root)
1693{
1694 int ret;
1695 struct list_head *cur;
1696 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1697 struct btrfs_device *device;
1698 u64 old_size;
1699 u64 size_to_free;
1700 struct btrfs_path *path;
1701 struct btrfs_key key;
1702 struct btrfs_chunk *chunk;
1703 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1704 struct btrfs_trans_handle *trans;
1705 struct btrfs_key found_key;
1706
Yan Zheng2b820322008-11-17 21:11:30 -05001707 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1708 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001709
Chris Mason7d9eb122008-07-08 14:19:17 -04001710 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001711 dev_root = dev_root->fs_info->dev_root;
1712
Chris Masonec44a352008-04-28 15:29:52 -04001713 /* step one make some room on all the devices */
1714 list_for_each(cur, devices) {
1715 device = list_entry(cur, struct btrfs_device, dev_list);
1716 old_size = device->total_bytes;
1717 size_to_free = div_factor(old_size, 1);
1718 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001719 if (!device->writeable ||
1720 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001721 continue;
1722
1723 ret = btrfs_shrink_device(device, old_size - size_to_free);
1724 BUG_ON(ret);
1725
1726 trans = btrfs_start_transaction(dev_root, 1);
1727 BUG_ON(!trans);
1728
1729 ret = btrfs_grow_device(trans, device, old_size);
1730 BUG_ON(ret);
1731
1732 btrfs_end_transaction(trans, dev_root);
1733 }
1734
1735 /* step two, relocate all the chunks */
1736 path = btrfs_alloc_path();
1737 BUG_ON(!path);
1738
1739 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1740 key.offset = (u64)-1;
1741 key.type = BTRFS_CHUNK_ITEM_KEY;
1742
1743 while(1) {
1744 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1745 if (ret < 0)
1746 goto error;
1747
1748 /*
1749 * this shouldn't happen, it means the last relocate
1750 * failed
1751 */
1752 if (ret == 0)
1753 break;
1754
1755 ret = btrfs_previous_item(chunk_root, path, 0,
1756 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001757 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001758 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001759
Chris Masonec44a352008-04-28 15:29:52 -04001760 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1761 path->slots[0]);
1762 if (found_key.objectid != key.objectid)
1763 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001764
Chris Masonec44a352008-04-28 15:29:52 -04001765 chunk = btrfs_item_ptr(path->nodes[0],
1766 path->slots[0],
1767 struct btrfs_chunk);
1768 key.offset = found_key.offset;
1769 /* chunk zero is special */
1770 if (key.offset == 0)
1771 break;
1772
Chris Mason7d9eb122008-07-08 14:19:17 -04001773 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001774 ret = btrfs_relocate_chunk(chunk_root,
1775 chunk_root->root_key.objectid,
1776 found_key.objectid,
1777 found_key.offset);
1778 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001779 }
1780 ret = 0;
1781error:
1782 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001783 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001784 return ret;
1785}
1786
Chris Mason8f18cf12008-04-25 16:53:30 -04001787/*
1788 * shrinking a device means finding all of the device extents past
1789 * the new size, and then following the back refs to the chunks.
1790 * The chunk relocation code actually frees the device extent
1791 */
1792int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1793{
1794 struct btrfs_trans_handle *trans;
1795 struct btrfs_root *root = device->dev_root;
1796 struct btrfs_dev_extent *dev_extent = NULL;
1797 struct btrfs_path *path;
1798 u64 length;
1799 u64 chunk_tree;
1800 u64 chunk_objectid;
1801 u64 chunk_offset;
1802 int ret;
1803 int slot;
1804 struct extent_buffer *l;
1805 struct btrfs_key key;
1806 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1807 u64 old_total = btrfs_super_total_bytes(super_copy);
1808 u64 diff = device->total_bytes - new_size;
1809
Yan Zheng2b820322008-11-17 21:11:30 -05001810 if (new_size >= device->total_bytes)
1811 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001812
1813 path = btrfs_alloc_path();
1814 if (!path)
1815 return -ENOMEM;
1816
1817 trans = btrfs_start_transaction(root, 1);
1818 if (!trans) {
1819 ret = -ENOMEM;
1820 goto done;
1821 }
1822
1823 path->reada = 2;
1824
Chris Mason7d9eb122008-07-08 14:19:17 -04001825 lock_chunks(root);
1826
Chris Mason8f18cf12008-04-25 16:53:30 -04001827 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001828 if (device->writeable)
1829 device->fs_devices->total_rw_bytes -= diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04001830 ret = btrfs_update_device(trans, device);
1831 if (ret) {
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 goto done;
1835 }
1836 WARN_ON(diff > old_total);
1837 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001838 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001839 btrfs_end_transaction(trans, root);
1840
1841 key.objectid = device->devid;
1842 key.offset = (u64)-1;
1843 key.type = BTRFS_DEV_EXTENT_KEY;
1844
1845 while (1) {
1846 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1847 if (ret < 0)
1848 goto done;
1849
1850 ret = btrfs_previous_item(root, path, 0, key.type);
1851 if (ret < 0)
1852 goto done;
1853 if (ret) {
1854 ret = 0;
1855 goto done;
1856 }
1857
1858 l = path->nodes[0];
1859 slot = path->slots[0];
1860 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1861
1862 if (key.objectid != device->devid)
1863 goto done;
1864
1865 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1866 length = btrfs_dev_extent_length(l, dev_extent);
1867
1868 if (key.offset + length <= new_size)
1869 goto done;
1870
1871 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1872 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1873 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1874 btrfs_release_path(root, path);
1875
1876 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1877 chunk_offset);
1878 if (ret)
1879 goto done;
1880 }
1881
1882done:
1883 btrfs_free_path(path);
1884 return ret;
1885}
1886
Christoph Hellwigb2950862008-12-02 09:54:17 -05001887static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001888 struct btrfs_root *root,
1889 struct btrfs_key *key,
1890 struct btrfs_chunk *chunk, int item_size)
1891{
1892 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1893 struct btrfs_disk_key disk_key;
1894 u32 array_size;
1895 u8 *ptr;
1896
1897 array_size = btrfs_super_sys_array_size(super_copy);
1898 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1899 return -EFBIG;
1900
1901 ptr = super_copy->sys_chunk_array + array_size;
1902 btrfs_cpu_key_to_disk(&disk_key, key);
1903 memcpy(ptr, &disk_key, sizeof(disk_key));
1904 ptr += sizeof(disk_key);
1905 memcpy(ptr, chunk, item_size);
1906 item_size += sizeof(disk_key);
1907 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1908 return 0;
1909}
1910
Chris Masona1b32a52008-09-05 16:09:51 -04001911static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
1912 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04001913{
1914 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1915 return calc_size;
1916 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1917 return calc_size * (num_stripes / sub_stripes);
1918 else
1919 return calc_size * num_stripes;
1920}
1921
Yan Zheng2b820322008-11-17 21:11:30 -05001922static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1923 struct btrfs_root *extent_root,
1924 struct map_lookup **map_ret,
1925 u64 *num_bytes, u64 *stripe_size,
1926 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001927{
Chris Mason593060d2008-03-25 16:50:33 -04001928 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001929 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001930 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04001931 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05001932 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001933 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04001934 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05001935 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04001936 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001937 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001938 u64 max_chunk_size = calc_size;
1939 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001940 u64 avail;
1941 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001942 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04001943 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001944 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001945 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001946 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001947 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001948 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001949 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001950
Chris Masonec44a352008-04-28 15:29:52 -04001951 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1952 (type & BTRFS_BLOCK_GROUP_DUP)) {
1953 WARN_ON(1);
1954 type &= ~BTRFS_BLOCK_GROUP_DUP;
1955 }
Yan Zheng2b820322008-11-17 21:11:30 -05001956 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04001957 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001958
Chris Masona40a90a2008-04-18 11:55:51 -04001959 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001960 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04001961 min_stripes = 2;
1962 }
1963 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04001964 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001965 min_stripes = 2;
1966 }
Chris Mason8790d502008-04-03 16:29:03 -04001967 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001968 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001969 if (num_stripes < 2)
1970 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04001971 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04001972 }
Chris Mason321aecc2008-04-16 10:49:51 -04001973 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001974 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04001975 if (num_stripes < 4)
1976 return -ENOSPC;
1977 num_stripes &= ~(u32)1;
1978 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001979 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04001980 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04001981
1982 if (type & BTRFS_BLOCK_GROUP_DATA) {
1983 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001984 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001985 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1986 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001987 min_stripe_size = 32 * 1024 * 1024;
1988 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1989 calc_size = 8 * 1024 * 1024;
1990 max_chunk_size = calc_size * 2;
1991 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001992 }
1993
Yan Zheng2b820322008-11-17 21:11:30 -05001994 /* we don't want a chunk larger than 10% of writeable space */
1995 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
1996 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001997
Chris Masona40a90a2008-04-18 11:55:51 -04001998again:
Yan Zheng2b820322008-11-17 21:11:30 -05001999 if (!map || map->num_stripes != num_stripes) {
2000 kfree(map);
2001 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2002 if (!map)
2003 return -ENOMEM;
2004 map->num_stripes = num_stripes;
2005 }
2006
Chris Mason9b3f68b2008-04-18 10:29:51 -04002007 if (calc_size * num_stripes > max_chunk_size) {
2008 calc_size = max_chunk_size;
2009 do_div(calc_size, num_stripes);
2010 do_div(calc_size, stripe_len);
2011 calc_size *= stripe_len;
2012 }
2013 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002014 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002015
Chris Mason9b3f68b2008-04-18 10:29:51 -04002016 do_div(calc_size, stripe_len);
2017 calc_size *= stripe_len;
2018
Yan Zheng2b820322008-11-17 21:11:30 -05002019 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002020 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002021
2022 if (type & BTRFS_BLOCK_GROUP_DUP)
2023 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002024 else
2025 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002026
Josef Bacik0f9dd462008-09-23 13:14:11 -04002027 /*
2028 * we add 1MB because we never use the first 1MB of the device, unless
2029 * we've looped, then we are likely allocating the maximum amount of
2030 * space left already
2031 */
2032 if (!looped)
2033 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002034
Yan Zheng2b820322008-11-17 21:11:30 -05002035 INIT_LIST_HEAD(&private_devs);
Chris Mason6324fbf2008-03-24 15:01:59 -04002036 while(index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002037 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002038 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002039 if (device->total_bytes > device->bytes_used)
2040 avail = device->total_bytes - device->bytes_used;
2041 else
2042 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002043 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002044
Chris Masondfe25022008-05-13 13:46:40 -04002045 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002046 ret = find_free_dev_extent(trans, device,
2047 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002048 if (ret == 0) {
2049 list_move_tail(&device->dev_alloc_list,
2050 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002051 map->stripes[index].dev = device;
2052 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002053 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002054 if (type & BTRFS_BLOCK_GROUP_DUP) {
2055 map->stripes[index].dev = device;
2056 map->stripes[index].physical =
2057 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002058 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002059 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002060 }
Chris Masondfe25022008-05-13 13:46:40 -04002061 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002062 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002063 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002064 break;
2065 }
Yan Zheng2b820322008-11-17 21:11:30 -05002066 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002067 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002068 if (index >= min_stripes) {
2069 num_stripes = index;
2070 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2071 num_stripes /= sub_stripes;
2072 num_stripes *= sub_stripes;
2073 }
2074 looped = 1;
2075 goto again;
2076 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002077 if (!looped && max_avail > 0) {
2078 looped = 1;
2079 calc_size = max_avail;
2080 goto again;
2081 }
Yan Zheng2b820322008-11-17 21:11:30 -05002082 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002083 return -ENOSPC;
2084 }
Chris Mason593060d2008-03-25 16:50:33 -04002085 map->sector_size = extent_root->sectorsize;
2086 map->stripe_len = stripe_len;
2087 map->io_align = stripe_len;
2088 map->io_width = stripe_len;
2089 map->type = type;
2090 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002091 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002092
Yan Zheng2b820322008-11-17 21:11:30 -05002093 *map_ret = map;
2094 *stripe_size = calc_size;
2095 *num_bytes = chunk_bytes_by_type(type, calc_size,
2096 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002097
2098 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002099 if (!em) {
2100 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002101 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002102 }
Chris Mason0b86a832008-03-24 15:01:56 -04002103 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002104 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002105 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002106 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002107 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002108
Chris Mason0b86a832008-03-24 15:01:56 -04002109 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2110 spin_lock(&em_tree->lock);
2111 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002112 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002113 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002114 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002115
2116 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2117 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2118 start, *num_bytes);
2119 BUG_ON(ret);
2120
2121 index = 0;
2122 while (index < map->num_stripes) {
2123 device = map->stripes[index].dev;
2124 dev_offset = map->stripes[index].physical;
2125
2126 ret = btrfs_alloc_dev_extent(trans, device,
2127 info->chunk_root->root_key.objectid,
2128 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2129 start, dev_offset, calc_size);
2130 BUG_ON(ret);
2131 index++;
2132 }
2133
2134 return 0;
2135}
2136
2137static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *extent_root,
2139 struct map_lookup *map, u64 chunk_offset,
2140 u64 chunk_size, u64 stripe_size)
2141{
2142 u64 dev_offset;
2143 struct btrfs_key key;
2144 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2145 struct btrfs_device *device;
2146 struct btrfs_chunk *chunk;
2147 struct btrfs_stripe *stripe;
2148 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2149 int index = 0;
2150 int ret;
2151
2152 chunk = kzalloc(item_size, GFP_NOFS);
2153 if (!chunk)
2154 return -ENOMEM;
2155
2156 index = 0;
2157 while (index < map->num_stripes) {
2158 device = map->stripes[index].dev;
2159 device->bytes_used += stripe_size;
2160 ret = btrfs_update_device(trans, device);
2161 BUG_ON(ret);
2162 index++;
2163 }
2164
2165 index = 0;
2166 stripe = &chunk->stripe;
2167 while (index < map->num_stripes) {
2168 device = map->stripes[index].dev;
2169 dev_offset = map->stripes[index].physical;
2170
2171 btrfs_set_stack_stripe_devid(stripe, device->devid);
2172 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2173 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2174 stripe++;
2175 index++;
2176 }
2177
2178 btrfs_set_stack_chunk_length(chunk, chunk_size);
2179 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2180 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2181 btrfs_set_stack_chunk_type(chunk, map->type);
2182 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2183 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2184 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2185 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2186 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2187
2188 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2189 key.type = BTRFS_CHUNK_ITEM_KEY;
2190 key.offset = chunk_offset;
2191
2192 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2193 BUG_ON(ret);
2194
2195 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2196 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2197 item_size);
2198 BUG_ON(ret);
2199 }
2200 kfree(chunk);
2201 return 0;
2202}
2203
2204/*
2205 * Chunk allocation falls into two parts. The first part does works
2206 * that make the new allocated chunk useable, but not do any operation
2207 * that modifies the chunk tree. The second part does the works that
2208 * require modifying the chunk tree. This division is important for the
2209 * bootstrap process of adding storage to a seed btrfs.
2210 */
2211int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2212 struct btrfs_root *extent_root, u64 type)
2213{
2214 u64 chunk_offset;
2215 u64 chunk_size;
2216 u64 stripe_size;
2217 struct map_lookup *map;
2218 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2219 int ret;
2220
2221 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2222 &chunk_offset);
2223 if (ret)
2224 return ret;
2225
2226 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2227 &stripe_size, chunk_offset, type);
2228 if (ret)
2229 return ret;
2230
2231 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2232 chunk_size, stripe_size);
2233 BUG_ON(ret);
2234 return 0;
2235}
2236
2237static int noinline init_first_rw_device(struct btrfs_trans_handle *trans,
2238 struct btrfs_root *root,
2239 struct btrfs_device *device)
2240{
2241 u64 chunk_offset;
2242 u64 sys_chunk_offset;
2243 u64 chunk_size;
2244 u64 sys_chunk_size;
2245 u64 stripe_size;
2246 u64 sys_stripe_size;
2247 u64 alloc_profile;
2248 struct map_lookup *map;
2249 struct map_lookup *sys_map;
2250 struct btrfs_fs_info *fs_info = root->fs_info;
2251 struct btrfs_root *extent_root = fs_info->extent_root;
2252 int ret;
2253
2254 ret = find_next_chunk(fs_info->chunk_root,
2255 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2256 BUG_ON(ret);
2257
2258 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2259 (fs_info->metadata_alloc_profile &
2260 fs_info->avail_metadata_alloc_bits);
2261 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2262
2263 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2264 &stripe_size, chunk_offset, alloc_profile);
2265 BUG_ON(ret);
2266
2267 sys_chunk_offset = chunk_offset + chunk_size;
2268
2269 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2270 (fs_info->system_alloc_profile &
2271 fs_info->avail_system_alloc_bits);
2272 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2273
2274 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2275 &sys_chunk_size, &sys_stripe_size,
2276 sys_chunk_offset, alloc_profile);
2277 BUG_ON(ret);
2278
2279 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2280 BUG_ON(ret);
2281
2282 /*
2283 * Modifying chunk tree needs allocating new blocks from both
2284 * system block group and metadata block group. So we only can
2285 * do operations require modifying the chunk tree after both
2286 * block groups were created.
2287 */
2288 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2289 chunk_size, stripe_size);
2290 BUG_ON(ret);
2291
2292 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2293 sys_chunk_offset, sys_chunk_size,
2294 sys_stripe_size);
2295 BUG_ON(ret);
2296 return 0;
2297}
2298
2299int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2300{
2301 struct extent_map *em;
2302 struct map_lookup *map;
2303 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2304 int readonly = 0;
2305 int i;
2306
2307 spin_lock(&map_tree->map_tree.lock);
2308 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2309 spin_unlock(&map_tree->map_tree.lock);
2310 if (!em)
2311 return 1;
2312
2313 map = (struct map_lookup *)em->bdev;
2314 for (i = 0; i < map->num_stripes; i++) {
2315 if (!map->stripes[i].dev->writeable) {
2316 readonly = 1;
2317 break;
2318 }
2319 }
2320 free_extent_map(em);
2321 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002322}
2323
2324void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2325{
2326 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2327}
2328
2329void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2330{
2331 struct extent_map *em;
2332
2333 while(1) {
2334 spin_lock(&tree->map_tree.lock);
2335 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2336 if (em)
2337 remove_extent_mapping(&tree->map_tree, em);
2338 spin_unlock(&tree->map_tree.lock);
2339 if (!em)
2340 break;
2341 kfree(em->bdev);
2342 /* once for us */
2343 free_extent_map(em);
2344 /* once for the tree */
2345 free_extent_map(em);
2346 }
2347}
2348
Chris Masonf1885912008-04-09 16:28:12 -04002349int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2350{
2351 struct extent_map *em;
2352 struct map_lookup *map;
2353 struct extent_map_tree *em_tree = &map_tree->map_tree;
2354 int ret;
2355
2356 spin_lock(&em_tree->lock);
2357 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002358 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002359 BUG_ON(!em);
2360
2361 BUG_ON(em->start > logical || em->start + em->len < logical);
2362 map = (struct map_lookup *)em->bdev;
2363 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2364 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002365 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2366 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002367 else
2368 ret = 1;
2369 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002370 return ret;
2371}
2372
Chris Masondfe25022008-05-13 13:46:40 -04002373static int find_live_mirror(struct map_lookup *map, int first, int num,
2374 int optimal)
2375{
2376 int i;
2377 if (map->stripes[optimal].dev->bdev)
2378 return optimal;
2379 for (i = first; i < first + num; i++) {
2380 if (map->stripes[i].dev->bdev)
2381 return i;
2382 }
2383 /* we couldn't find one that doesn't fail. Just return something
2384 * and the io error handling code will clean up eventually
2385 */
2386 return optimal;
2387}
2388
Chris Masonf2d8d742008-04-21 10:03:05 -04002389static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2390 u64 logical, u64 *length,
2391 struct btrfs_multi_bio **multi_ret,
2392 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002393{
2394 struct extent_map *em;
2395 struct map_lookup *map;
2396 struct extent_map_tree *em_tree = &map_tree->map_tree;
2397 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002398 u64 stripe_offset;
2399 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002400 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002401 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002402 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002403 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002404 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002405 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002406 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002407
Chris Masoncea9e442008-04-09 16:28:12 -04002408 if (multi_ret && !(rw & (1 << BIO_RW))) {
2409 stripes_allocated = 1;
2410 }
2411again:
2412 if (multi_ret) {
2413 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2414 GFP_NOFS);
2415 if (!multi)
2416 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002417
2418 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002419 }
Chris Mason0b86a832008-03-24 15:01:56 -04002420
2421 spin_lock(&em_tree->lock);
2422 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002423 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002424
2425 if (!em && unplug_page)
2426 return 0;
2427
Chris Mason3b951512008-04-17 11:29:12 -04002428 if (!em) {
Chris Masona061fc82008-05-07 11:43:44 -04002429 printk("unable to find logical %Lu len %Lu\n", logical, *length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002430 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002431 }
Chris Mason0b86a832008-03-24 15:01:56 -04002432
2433 BUG_ON(em->start > logical || em->start + em->len < logical);
2434 map = (struct map_lookup *)em->bdev;
2435 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002436
Chris Masonf1885912008-04-09 16:28:12 -04002437 if (mirror_num > map->num_stripes)
2438 mirror_num = 0;
2439
Chris Masoncea9e442008-04-09 16:28:12 -04002440 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002441 if (rw & (1 << BIO_RW)) {
2442 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2443 BTRFS_BLOCK_GROUP_DUP)) {
2444 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002445 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002446 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2447 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002448 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002449 }
2450 }
2451 if (multi_ret && rw == WRITE &&
2452 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002453 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002454 free_extent_map(em);
2455 kfree(multi);
2456 goto again;
2457 }
Chris Mason593060d2008-03-25 16:50:33 -04002458 stripe_nr = offset;
2459 /*
2460 * stripe_nr counts the total number of stripes we have to stride
2461 * to get to this block
2462 */
2463 do_div(stripe_nr, map->stripe_len);
2464
2465 stripe_offset = stripe_nr * map->stripe_len;
2466 BUG_ON(offset < stripe_offset);
2467
2468 /* stripe_offset is the offset of this block in its stripe*/
2469 stripe_offset = offset - stripe_offset;
2470
Chris Masoncea9e442008-04-09 16:28:12 -04002471 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002472 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002473 BTRFS_BLOCK_GROUP_DUP)) {
2474 /* we limit the length of each bio to what fits in a stripe */
2475 *length = min_t(u64, em->len - offset,
2476 map->stripe_len - stripe_offset);
2477 } else {
2478 *length = em->len - offset;
2479 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002480
2481 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002482 goto out;
2483
Chris Masonf2d8d742008-04-21 10:03:05 -04002484 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002485 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002486 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002487 if (unplug_page || (rw & (1 << BIO_RW)))
2488 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002489 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002490 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002491 else {
2492 stripe_index = find_live_mirror(map, 0,
2493 map->num_stripes,
2494 current->pid % map->num_stripes);
2495 }
Chris Mason2fff7342008-04-29 14:12:09 -04002496
Chris Mason611f0e02008-04-03 16:29:03 -04002497 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002498 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002499 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002500 else if (mirror_num)
2501 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002502
Chris Mason321aecc2008-04-16 10:49:51 -04002503 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2504 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002505
2506 stripe_index = do_div(stripe_nr, factor);
2507 stripe_index *= map->sub_stripes;
2508
Chris Masonf2d8d742008-04-21 10:03:05 -04002509 if (unplug_page || (rw & (1 << BIO_RW)))
2510 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002511 else if (mirror_num)
2512 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002513 else {
2514 stripe_index = find_live_mirror(map, stripe_index,
2515 map->sub_stripes, stripe_index +
2516 current->pid % map->sub_stripes);
2517 }
Chris Mason8790d502008-04-03 16:29:03 -04002518 } else {
2519 /*
2520 * after this do_div call, stripe_nr is the number of stripes
2521 * on this device we have to walk to find the data, and
2522 * stripe_index is the number of our device in the stripe array
2523 */
2524 stripe_index = do_div(stripe_nr, map->num_stripes);
2525 }
Chris Mason593060d2008-03-25 16:50:33 -04002526 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002527
Chris Masonf2d8d742008-04-21 10:03:05 -04002528 for (i = 0; i < num_stripes; i++) {
2529 if (unplug_page) {
2530 struct btrfs_device *device;
2531 struct backing_dev_info *bdi;
2532
2533 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002534 if (device->bdev) {
2535 bdi = blk_get_backing_dev_info(device->bdev);
2536 if (bdi->unplug_io_fn) {
2537 bdi->unplug_io_fn(bdi, unplug_page);
2538 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002539 }
2540 } else {
2541 multi->stripes[i].physical =
2542 map->stripes[stripe_index].physical +
2543 stripe_offset + stripe_nr * map->stripe_len;
2544 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2545 }
Chris Masoncea9e442008-04-09 16:28:12 -04002546 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002547 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002548 if (multi_ret) {
2549 *multi_ret = multi;
2550 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002551 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002552 }
Chris Masoncea9e442008-04-09 16:28:12 -04002553out:
Chris Mason0b86a832008-03-24 15:01:56 -04002554 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002555 return 0;
2556}
2557
Chris Masonf2d8d742008-04-21 10:03:05 -04002558int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2559 u64 logical, u64 *length,
2560 struct btrfs_multi_bio **multi_ret, int mirror_num)
2561{
2562 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2563 mirror_num, NULL);
2564}
2565
2566int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2567 u64 logical, struct page *page)
2568{
2569 u64 length = PAGE_CACHE_SIZE;
2570 return __btrfs_map_block(map_tree, READ, logical, &length,
2571 NULL, 0, page);
2572}
2573
2574
Chris Mason8790d502008-04-03 16:29:03 -04002575static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002576{
Chris Masoncea9e442008-04-09 16:28:12 -04002577 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002578 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002579
Chris Mason8790d502008-04-03 16:29:03 -04002580 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002581 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002582
Chris Mason7d2b4da2008-08-05 10:13:57 -04002583 if (bio == multi->orig_bio)
2584 is_orig_bio = 1;
2585
Chris Masoncea9e442008-04-09 16:28:12 -04002586 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002587 if (!is_orig_bio) {
2588 bio_put(bio);
2589 bio = multi->orig_bio;
2590 }
Chris Mason8790d502008-04-03 16:29:03 -04002591 bio->bi_private = multi->private;
2592 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002593 /* only send an error to the higher layers if it is
2594 * beyond the tolerance of the multi-bio
2595 */
Chris Mason1259ab72008-05-12 13:39:03 -04002596 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002597 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002598 } else if (err) {
2599 /*
2600 * this bio is actually up to date, we didn't
2601 * go over the max number of errors
2602 */
2603 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002604 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002605 }
Chris Mason8790d502008-04-03 16:29:03 -04002606 kfree(multi);
2607
2608 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002609 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002610 bio_put(bio);
2611 }
Chris Mason8790d502008-04-03 16:29:03 -04002612}
2613
Chris Mason8b712842008-06-11 16:50:36 -04002614struct async_sched {
2615 struct bio *bio;
2616 int rw;
2617 struct btrfs_fs_info *info;
2618 struct btrfs_work work;
2619};
2620
2621/*
2622 * see run_scheduled_bios for a description of why bios are collected for
2623 * async submit.
2624 *
2625 * This will add one bio to the pending list for a device and make sure
2626 * the work struct is scheduled.
2627 */
Chris Masona1b32a52008-09-05 16:09:51 -04002628static int noinline schedule_bio(struct btrfs_root *root,
2629 struct btrfs_device *device,
2630 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002631{
2632 int should_queue = 1;
2633
2634 /* don't bother with additional async steps for reads, right now */
2635 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002636 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002637 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002638 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002639 return 0;
2640 }
2641
2642 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04002643 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002644 * higher layers. Otherwise, the async bio makes it appear we have
2645 * made progress against dirty pages when we've really just put it
2646 * on a queue for later
2647 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04002648 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002649 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002650 bio->bi_next = NULL;
2651 bio->bi_rw |= rw;
2652
2653 spin_lock(&device->io_lock);
2654
2655 if (device->pending_bio_tail)
2656 device->pending_bio_tail->bi_next = bio;
2657
2658 device->pending_bio_tail = bio;
2659 if (!device->pending_bios)
2660 device->pending_bios = bio;
2661 if (device->running_pending)
2662 should_queue = 0;
2663
2664 spin_unlock(&device->io_lock);
2665
2666 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002667 btrfs_queue_worker(&root->fs_info->submit_workers,
2668 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002669 return 0;
2670}
2671
Chris Masonf1885912008-04-09 16:28:12 -04002672int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002673 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002674{
2675 struct btrfs_mapping_tree *map_tree;
2676 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002677 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002678 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002679 u64 length = 0;
2680 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002681 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002682 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002683 int dev_nr = 0;
2684 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002685
Chris Masonf2d8d742008-04-21 10:03:05 -04002686 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002687 map_tree = &root->fs_info->mapping_tree;
2688 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002689
Chris Masonf1885912008-04-09 16:28:12 -04002690 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2691 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002692 BUG_ON(ret);
2693
2694 total_devs = multi->num_stripes;
2695 if (map_length < length) {
2696 printk("mapping failed logical %Lu bio len %Lu "
2697 "len %Lu\n", logical, length, map_length);
2698 BUG();
2699 }
2700 multi->end_io = first_bio->bi_end_io;
2701 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002702 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002703 atomic_set(&multi->stripes_pending, multi->num_stripes);
2704
Chris Mason8790d502008-04-03 16:29:03 -04002705 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002706 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002707 if (dev_nr < total_devs - 1) {
2708 bio = bio_clone(first_bio, GFP_NOFS);
2709 BUG_ON(!bio);
2710 } else {
2711 bio = first_bio;
2712 }
2713 bio->bi_private = multi;
2714 bio->bi_end_io = end_bio_multi_stripe;
2715 }
Chris Masoncea9e442008-04-09 16:28:12 -04002716 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2717 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002718 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002719 if (dev && dev->bdev) {
2720 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002721 if (async_submit)
2722 schedule_bio(root, dev, rw, bio);
2723 else
2724 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002725 } else {
2726 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2727 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002728 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002729 }
Chris Mason8790d502008-04-03 16:29:03 -04002730 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002731 }
Chris Masoncea9e442008-04-09 16:28:12 -04002732 if (total_devs == 1)
2733 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002734 return 0;
2735}
2736
Chris Masona4437552008-04-18 10:29:38 -04002737struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05002738 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04002739{
Yan Zheng2b820322008-11-17 21:11:30 -05002740 struct btrfs_device *device;
2741 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002742
Yan Zheng2b820322008-11-17 21:11:30 -05002743 cur_devices = root->fs_info->fs_devices;
2744 while (cur_devices) {
2745 if (!fsid ||
2746 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2747 device = __find_device(&cur_devices->devices,
2748 devid, uuid);
2749 if (device)
2750 return device;
2751 }
2752 cur_devices = cur_devices->seed;
2753 }
2754 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002755}
2756
Chris Masondfe25022008-05-13 13:46:40 -04002757static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2758 u64 devid, u8 *dev_uuid)
2759{
2760 struct btrfs_device *device;
2761 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2762
2763 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05002764 if (!device)
2765 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04002766 list_add(&device->dev_list,
2767 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04002768 device->barriers = 1;
2769 device->dev_root = root->fs_info->dev_root;
2770 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002771 device->work.func = pending_bios_fn;
Chris Masondfe25022008-05-13 13:46:40 -04002772 fs_devices->num_devices++;
2773 spin_lock_init(&device->io_lock);
2774 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2775 return device;
2776}
2777
Chris Mason0b86a832008-03-24 15:01:56 -04002778static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2779 struct extent_buffer *leaf,
2780 struct btrfs_chunk *chunk)
2781{
2782 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2783 struct map_lookup *map;
2784 struct extent_map *em;
2785 u64 logical;
2786 u64 length;
2787 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002788 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002789 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002790 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002791 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002792
Chris Masone17cade2008-04-15 15:41:47 -04002793 logical = key->offset;
2794 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002795
Chris Mason0b86a832008-03-24 15:01:56 -04002796 spin_lock(&map_tree->map_tree.lock);
2797 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002798 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002799
2800 /* already mapped? */
2801 if (em && em->start <= logical && em->start + em->len > logical) {
2802 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002803 return 0;
2804 } else if (em) {
2805 free_extent_map(em);
2806 }
Chris Mason0b86a832008-03-24 15:01:56 -04002807
2808 map = kzalloc(sizeof(*map), GFP_NOFS);
2809 if (!map)
2810 return -ENOMEM;
2811
2812 em = alloc_extent_map(GFP_NOFS);
2813 if (!em)
2814 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002815 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2816 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002817 if (!map) {
2818 free_extent_map(em);
2819 return -ENOMEM;
2820 }
2821
2822 em->bdev = (struct block_device *)map;
2823 em->start = logical;
2824 em->len = length;
2825 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002826 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002827
Chris Mason593060d2008-03-25 16:50:33 -04002828 map->num_stripes = num_stripes;
2829 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2830 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2831 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2832 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2833 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002834 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002835 for (i = 0; i < num_stripes; i++) {
2836 map->stripes[i].physical =
2837 btrfs_stripe_offset_nr(leaf, chunk, i);
2838 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002839 read_extent_buffer(leaf, uuid, (unsigned long)
2840 btrfs_stripe_dev_uuid_nr(chunk, i),
2841 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002842 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2843 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04002844 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002845 kfree(map);
2846 free_extent_map(em);
2847 return -EIO;
2848 }
Chris Masondfe25022008-05-13 13:46:40 -04002849 if (!map->stripes[i].dev) {
2850 map->stripes[i].dev =
2851 add_missing_dev(root, devid, uuid);
2852 if (!map->stripes[i].dev) {
2853 kfree(map);
2854 free_extent_map(em);
2855 return -EIO;
2856 }
2857 }
2858 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002859 }
2860
2861 spin_lock(&map_tree->map_tree.lock);
2862 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002863 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002864 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002865 free_extent_map(em);
2866
2867 return 0;
2868}
2869
2870static int fill_device_from_item(struct extent_buffer *leaf,
2871 struct btrfs_dev_item *dev_item,
2872 struct btrfs_device *device)
2873{
2874 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04002875
2876 device->devid = btrfs_device_id(leaf, dev_item);
2877 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2878 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2879 device->type = btrfs_device_type(leaf, dev_item);
2880 device->io_align = btrfs_device_io_align(leaf, dev_item);
2881 device->io_width = btrfs_device_io_width(leaf, dev_item);
2882 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002883
2884 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04002885 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002886
Chris Mason0b86a832008-03-24 15:01:56 -04002887 return 0;
2888}
2889
Yan Zheng2b820322008-11-17 21:11:30 -05002890static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
2891{
2892 struct btrfs_fs_devices *fs_devices;
2893 int ret;
2894
2895 mutex_lock(&uuid_mutex);
2896
2897 fs_devices = root->fs_info->fs_devices->seed;
2898 while (fs_devices) {
2899 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2900 ret = 0;
2901 goto out;
2902 }
2903 fs_devices = fs_devices->seed;
2904 }
2905
2906 fs_devices = find_fsid(fsid);
2907 if (!fs_devices) {
2908 ret = -ENOENT;
2909 goto out;
2910 }
2911 if (fs_devices->opened) {
2912 ret = -EBUSY;
2913 goto out;
2914 }
2915
Chris Mason15916de2008-11-19 21:17:22 -05002916 ret = __btrfs_open_devices(fs_devices, MS_RDONLY,
2917 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05002918 if (ret)
2919 goto out;
2920
2921 if (!fs_devices->seeding) {
2922 __btrfs_close_devices(fs_devices);
2923 ret = -EINVAL;
2924 goto out;
2925 }
2926
2927 fs_devices->seed = root->fs_info->fs_devices->seed;
2928 root->fs_info->fs_devices->seed = fs_devices;
2929 fs_devices->sprouted = 1;
2930out:
2931 mutex_unlock(&uuid_mutex);
2932 return ret;
2933}
2934
Chris Mason0d81ba52008-03-24 15:02:07 -04002935static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04002936 struct extent_buffer *leaf,
2937 struct btrfs_dev_item *dev_item)
2938{
2939 struct btrfs_device *device;
2940 u64 devid;
2941 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002942 int seed_devices = 0;
2943 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04002944 u8 dev_uuid[BTRFS_UUID_SIZE];
2945
Chris Mason0b86a832008-03-24 15:01:56 -04002946 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04002947 read_extent_buffer(leaf, dev_uuid,
2948 (unsigned long)btrfs_device_uuid(dev_item),
2949 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002950 read_extent_buffer(leaf, fs_uuid,
2951 (unsigned long)btrfs_device_fsid(dev_item),
2952 BTRFS_UUID_SIZE);
2953
2954 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
2955 ret = open_seed_devices(root, fs_uuid);
2956 if (ret)
2957 return ret;
2958 seed_devices = 1;
2959 }
2960
2961 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
2962 if (!device || !device->bdev) {
2963 if (!btrfs_test_opt(root, DEGRADED) || seed_devices)
2964 return -EIO;
2965
2966 if (!device) {
2967 printk("warning devid %Lu missing\n", devid);
2968 device = add_missing_dev(root, devid, dev_uuid);
2969 if (!device)
2970 return -ENOMEM;
2971 }
2972 }
2973
2974 if (device->fs_devices != root->fs_info->fs_devices) {
2975 BUG_ON(device->writeable);
2976 if (device->generation !=
2977 btrfs_device_generation(leaf, dev_item))
2978 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04002979 }
Chris Mason0b86a832008-03-24 15:01:56 -04002980
2981 fill_device_from_item(leaf, dev_item, device);
2982 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04002983 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002984 if (device->writeable)
2985 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002986 ret = 0;
2987#if 0
2988 ret = btrfs_open_device(device);
2989 if (ret) {
2990 kfree(device);
2991 }
2992#endif
2993 return ret;
2994}
2995
Chris Mason0d81ba52008-03-24 15:02:07 -04002996int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
2997{
2998 struct btrfs_dev_item *dev_item;
2999
3000 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3001 dev_item);
3002 return read_one_dev(root, buf, dev_item);
3003}
3004
Chris Mason0b86a832008-03-24 15:01:56 -04003005int btrfs_read_sys_array(struct btrfs_root *root)
3006{
3007 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003008 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003009 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003010 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003011 u8 *ptr;
3012 unsigned long sb_ptr;
3013 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003014 u32 num_stripes;
3015 u32 array_size;
3016 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003017 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003018 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003019
Chris Masona061fc82008-05-07 11:43:44 -04003020 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3021 BTRFS_SUPER_INFO_SIZE);
3022 if (!sb)
3023 return -ENOMEM;
3024 btrfs_set_buffer_uptodate(sb);
3025 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003026 array_size = btrfs_super_sys_array_size(super_copy);
3027
Chris Mason0b86a832008-03-24 15:01:56 -04003028 ptr = super_copy->sys_chunk_array;
3029 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3030 cur = 0;
3031
3032 while (cur < array_size) {
3033 disk_key = (struct btrfs_disk_key *)ptr;
3034 btrfs_disk_key_to_cpu(&key, disk_key);
3035
Chris Masona061fc82008-05-07 11:43:44 -04003036 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003037 sb_ptr += len;
3038 cur += len;
3039
Chris Mason0d81ba52008-03-24 15:02:07 -04003040 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003041 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003042 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003043 if (ret)
3044 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003045 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3046 len = btrfs_chunk_item_size(num_stripes);
3047 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003048 ret = -EIO;
3049 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003050 }
3051 ptr += len;
3052 sb_ptr += len;
3053 cur += len;
3054 }
Chris Masona061fc82008-05-07 11:43:44 -04003055 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003056 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003057}
3058
3059int btrfs_read_chunk_tree(struct btrfs_root *root)
3060{
3061 struct btrfs_path *path;
3062 struct extent_buffer *leaf;
3063 struct btrfs_key key;
3064 struct btrfs_key found_key;
3065 int ret;
3066 int slot;
3067
3068 root = root->fs_info->chunk_root;
3069
3070 path = btrfs_alloc_path();
3071 if (!path)
3072 return -ENOMEM;
3073
3074 /* first we search for all of the device items, and then we
3075 * read in all of the chunk items. This way we can create chunk
3076 * mappings that reference all of the devices that are afound
3077 */
3078 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3079 key.offset = 0;
3080 key.type = 0;
3081again:
3082 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3083 while(1) {
3084 leaf = path->nodes[0];
3085 slot = path->slots[0];
3086 if (slot >= btrfs_header_nritems(leaf)) {
3087 ret = btrfs_next_leaf(root, path);
3088 if (ret == 0)
3089 continue;
3090 if (ret < 0)
3091 goto error;
3092 break;
3093 }
3094 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3095 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3096 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3097 break;
3098 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3099 struct btrfs_dev_item *dev_item;
3100 dev_item = btrfs_item_ptr(leaf, slot,
3101 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003102 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003103 if (ret)
3104 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003105 }
3106 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3107 struct btrfs_chunk *chunk;
3108 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3109 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003110 if (ret)
3111 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003112 }
3113 path->slots[0]++;
3114 }
3115 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3116 key.objectid = 0;
3117 btrfs_release_path(root, path);
3118 goto again;
3119 }
Chris Mason0b86a832008-03-24 15:01:56 -04003120 ret = 0;
3121error:
Yan Zheng2b820322008-11-17 21:11:30 -05003122 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003123 return ret;
3124}