blob: 825364fae690001c0860ec5de1ae6c5fd9927743 [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,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500397 fmode_t 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
Yan Zhenga512bbf2008-12-08 16:46:26 -0500426 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400427 if (!bh)
428 goto error_close;
429
430 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masona0af4692008-05-13 16:03:06 -0400431 devid = le64_to_cpu(disk_super->dev_item.devid);
432 if (devid != device->devid)
433 goto error_brelse;
434
Yan Zheng2b820322008-11-17 21:11:30 -0500435 if (memcmp(device->uuid, disk_super->dev_item.uuid,
436 BTRFS_UUID_SIZE))
437 goto error_brelse;
438
439 device->generation = btrfs_super_generation(disk_super);
440 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400441 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500442 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400443 latest_bdev = bdev;
444 }
445
Yan Zheng2b820322008-11-17 21:11:30 -0500446 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
447 device->writeable = 0;
448 } else {
449 device->writeable = !bdev_read_only(bdev);
450 seeding = 0;
451 }
452
Chris Mason8a4b83c2008-03-24 15:02:07 -0400453 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400454 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500455 device->mode = flags;
456
Chris Masona0af4692008-05-13 16:03:06 -0400457 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500458 if (device->writeable) {
459 fs_devices->rw_devices++;
460 list_add(&device->dev_alloc_list,
461 &fs_devices->alloc_list);
462 }
Chris Masona0af4692008-05-13 16:03:06 -0400463 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400464
Chris Masona0af4692008-05-13 16:03:06 -0400465error_brelse:
466 brelse(bh);
467error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500468 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400469error:
470 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400471 }
Chris Masona0af4692008-05-13 16:03:06 -0400472 if (fs_devices->open_devices == 0) {
473 ret = -EIO;
474 goto out;
475 }
Yan Zheng2b820322008-11-17 21:11:30 -0500476 fs_devices->seeding = seeding;
477 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400478 fs_devices->latest_bdev = latest_bdev;
479 fs_devices->latest_devid = latest_devid;
480 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500481 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400482out:
Yan Zheng2b820322008-11-17 21:11:30 -0500483 return ret;
484}
485
486int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500487 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500488{
489 int ret;
490
491 mutex_lock(&uuid_mutex);
492 if (fs_devices->opened) {
493 if (fs_devices->sprouted) {
494 ret = -EBUSY;
495 } else {
496 fs_devices->opened++;
497 ret = 0;
498 }
499 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500500 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500501 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400502 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400503 return ret;
504}
505
Christoph Hellwig97288f22008-12-02 06:36:09 -0500506int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400507 struct btrfs_fs_devices **fs_devices_ret)
508{
509 struct btrfs_super_block *disk_super;
510 struct block_device *bdev;
511 struct buffer_head *bh;
512 int ret;
513 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400514 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400515
516 mutex_lock(&uuid_mutex);
517
Chris Mason15916de2008-11-19 21:17:22 -0500518 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519
520 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400521 ret = PTR_ERR(bdev);
522 goto error;
523 }
524
525 ret = set_blocksize(bdev, 4096);
526 if (ret)
527 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500528 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400529 if (!bh) {
530 ret = -EIO;
531 goto error_close;
532 }
533 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400535 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400536 if (disk_super->label[0])
537 printk("device label %s ", disk_super->label);
538 else {
539 /* FIXME, make a readl uuid parser */
540 printk("device fsid %llx-%llx ",
541 *(unsigned long long *)disk_super->fsid,
542 *(unsigned long long *)(disk_super->fsid + 8));
543 }
544 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400545 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
546
Chris Mason8a4b83c2008-03-24 15:02:07 -0400547 brelse(bh);
548error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500549 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400550error:
551 mutex_unlock(&uuid_mutex);
552 return ret;
553}
Chris Mason0b86a832008-03-24 15:01:56 -0400554
555/*
556 * this uses a pretty simple search, the expectation is that it is
557 * called very infrequently and that a given device has a small number
558 * of extents
559 */
Chris Masona1b32a52008-09-05 16:09:51 -0400560static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
561 struct btrfs_device *device,
Chris Masona1b32a52008-09-05 16:09:51 -0400562 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400563{
564 struct btrfs_key key;
565 struct btrfs_root *root = device->dev_root;
566 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500567 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400568 u64 hole_size = 0;
569 u64 last_byte = 0;
570 u64 search_start = 0;
571 u64 search_end = device->total_bytes;
572 int ret;
573 int slot = 0;
574 int start_found;
575 struct extent_buffer *l;
576
Yan Zheng2b820322008-11-17 21:11:30 -0500577 path = btrfs_alloc_path();
578 if (!path)
579 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400580 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500581 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400582
583 /* FIXME use last free of some kind */
584
Chris Mason8a4b83c2008-03-24 15:02:07 -0400585 /* we don't want to overwrite the superblock on the drive,
586 * so we make sure to start at an offset of at least 1MB
587 */
588 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400589
590 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
591 search_start = max(root->fs_info->alloc_start, search_start);
592
Chris Mason0b86a832008-03-24 15:01:56 -0400593 key.objectid = device->devid;
594 key.offset = search_start;
595 key.type = BTRFS_DEV_EXTENT_KEY;
596 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
597 if (ret < 0)
598 goto error;
599 ret = btrfs_previous_item(root, path, 0, key.type);
600 if (ret < 0)
601 goto error;
602 l = path->nodes[0];
603 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
604 while (1) {
605 l = path->nodes[0];
606 slot = path->slots[0];
607 if (slot >= btrfs_header_nritems(l)) {
608 ret = btrfs_next_leaf(root, path);
609 if (ret == 0)
610 continue;
611 if (ret < 0)
612 goto error;
613no_more_items:
614 if (!start_found) {
615 if (search_start >= search_end) {
616 ret = -ENOSPC;
617 goto error;
618 }
619 *start = search_start;
620 start_found = 1;
621 goto check_pending;
622 }
623 *start = last_byte > search_start ?
624 last_byte : search_start;
625 if (search_end <= *start) {
626 ret = -ENOSPC;
627 goto error;
628 }
629 goto check_pending;
630 }
631 btrfs_item_key_to_cpu(l, &key, slot);
632
633 if (key.objectid < device->devid)
634 goto next;
635
636 if (key.objectid > device->devid)
637 goto no_more_items;
638
639 if (key.offset >= search_start && key.offset > last_byte &&
640 start_found) {
641 if (last_byte < search_start)
642 last_byte = search_start;
643 hole_size = key.offset - last_byte;
644 if (key.offset > last_byte &&
645 hole_size >= num_bytes) {
646 *start = last_byte;
647 goto check_pending;
648 }
649 }
650 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
651 goto next;
652 }
653
654 start_found = 1;
655 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
656 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
657next:
658 path->slots[0]++;
659 cond_resched();
660 }
661check_pending:
662 /* we have to make sure we didn't find an extent that has already
663 * been allocated by the map tree or the original allocation
664 */
Chris Mason0b86a832008-03-24 15:01:56 -0400665 BUG_ON(*start < search_start);
666
Chris Mason6324fbf2008-03-24 15:01:59 -0400667 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400668 ret = -ENOSPC;
669 goto error;
670 }
671 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500672 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400673
674error:
Yan Zheng2b820322008-11-17 21:11:30 -0500675 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400676 return ret;
677}
678
Christoph Hellwigb2950862008-12-02 09:54:17 -0500679static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400680 struct btrfs_device *device,
681 u64 start)
682{
683 int ret;
684 struct btrfs_path *path;
685 struct btrfs_root *root = device->dev_root;
686 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400687 struct btrfs_key found_key;
688 struct extent_buffer *leaf = NULL;
689 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400690
691 path = btrfs_alloc_path();
692 if (!path)
693 return -ENOMEM;
694
695 key.objectid = device->devid;
696 key.offset = start;
697 key.type = BTRFS_DEV_EXTENT_KEY;
698
699 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400700 if (ret > 0) {
701 ret = btrfs_previous_item(root, path, key.objectid,
702 BTRFS_DEV_EXTENT_KEY);
703 BUG_ON(ret);
704 leaf = path->nodes[0];
705 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
706 extent = btrfs_item_ptr(leaf, path->slots[0],
707 struct btrfs_dev_extent);
708 BUG_ON(found_key.offset > start || found_key.offset +
709 btrfs_dev_extent_length(leaf, extent) < start);
710 ret = 0;
711 } else if (ret == 0) {
712 leaf = path->nodes[0];
713 extent = btrfs_item_ptr(leaf, path->slots[0],
714 struct btrfs_dev_extent);
715 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400716 BUG_ON(ret);
717
Chris Masondfe25022008-05-13 13:46:40 -0400718 if (device->bytes_used > 0)
719 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400720 ret = btrfs_del_item(trans, root, path);
721 BUG_ON(ret);
722
723 btrfs_free_path(path);
724 return ret;
725}
726
Yan Zheng2b820322008-11-17 21:11:30 -0500727int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400728 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400729 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500730 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400731{
732 int ret;
733 struct btrfs_path *path;
734 struct btrfs_root *root = device->dev_root;
735 struct btrfs_dev_extent *extent;
736 struct extent_buffer *leaf;
737 struct btrfs_key key;
738
Chris Masondfe25022008-05-13 13:46:40 -0400739 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400740 path = btrfs_alloc_path();
741 if (!path)
742 return -ENOMEM;
743
Chris Mason0b86a832008-03-24 15:01:56 -0400744 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500745 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400746 key.type = BTRFS_DEV_EXTENT_KEY;
747 ret = btrfs_insert_empty_item(trans, root, path, &key,
748 sizeof(*extent));
749 BUG_ON(ret);
750
751 leaf = path->nodes[0];
752 extent = btrfs_item_ptr(leaf, path->slots[0],
753 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400754 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
755 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
756 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
757
758 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
759 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
760 BTRFS_UUID_SIZE);
761
Chris Mason0b86a832008-03-24 15:01:56 -0400762 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
763 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400764 btrfs_free_path(path);
765 return ret;
766}
767
Chris Masona1b32a52008-09-05 16:09:51 -0400768static noinline int find_next_chunk(struct btrfs_root *root,
769 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400770{
771 struct btrfs_path *path;
772 int ret;
773 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400774 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400775 struct btrfs_key found_key;
776
777 path = btrfs_alloc_path();
778 BUG_ON(!path);
779
Chris Masone17cade2008-04-15 15:41:47 -0400780 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400781 key.offset = (u64)-1;
782 key.type = BTRFS_CHUNK_ITEM_KEY;
783
784 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
785 if (ret < 0)
786 goto error;
787
788 BUG_ON(ret == 0);
789
790 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
791 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400792 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400793 } else {
794 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
795 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400796 if (found_key.objectid != objectid)
797 *offset = 0;
798 else {
799 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
800 struct btrfs_chunk);
801 *offset = found_key.offset +
802 btrfs_chunk_length(path->nodes[0], chunk);
803 }
Chris Mason0b86a832008-03-24 15:01:56 -0400804 }
805 ret = 0;
806error:
807 btrfs_free_path(path);
808 return ret;
809}
810
Yan Zheng2b820322008-11-17 21:11:30 -0500811static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400812{
813 int ret;
814 struct btrfs_key key;
815 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500816 struct btrfs_path *path;
817
818 root = root->fs_info->chunk_root;
819
820 path = btrfs_alloc_path();
821 if (!path)
822 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400823
824 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
825 key.type = BTRFS_DEV_ITEM_KEY;
826 key.offset = (u64)-1;
827
828 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
829 if (ret < 0)
830 goto error;
831
832 BUG_ON(ret == 0);
833
834 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
835 BTRFS_DEV_ITEM_KEY);
836 if (ret) {
837 *objectid = 1;
838 } else {
839 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
840 path->slots[0]);
841 *objectid = found_key.offset + 1;
842 }
843 ret = 0;
844error:
Yan Zheng2b820322008-11-17 21:11:30 -0500845 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400846 return ret;
847}
848
849/*
850 * the device information is stored in the chunk root
851 * the btrfs_device struct should be fully filled in
852 */
853int btrfs_add_device(struct btrfs_trans_handle *trans,
854 struct btrfs_root *root,
855 struct btrfs_device *device)
856{
857 int ret;
858 struct btrfs_path *path;
859 struct btrfs_dev_item *dev_item;
860 struct extent_buffer *leaf;
861 struct btrfs_key key;
862 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -0400863
864 root = root->fs_info->chunk_root;
865
866 path = btrfs_alloc_path();
867 if (!path)
868 return -ENOMEM;
869
Chris Mason0b86a832008-03-24 15:01:56 -0400870 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
871 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -0500872 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400873
874 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400875 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400876 if (ret)
877 goto out;
878
879 leaf = path->nodes[0];
880 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
881
882 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -0500883 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400884 btrfs_set_device_type(leaf, dev_item, device->type);
885 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
886 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
887 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400888 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
889 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400890 btrfs_set_device_group(leaf, dev_item, 0);
891 btrfs_set_device_seek_speed(leaf, dev_item, 0);
892 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400893
Chris Mason0b86a832008-03-24 15:01:56 -0400894 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400895 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500896 ptr = (unsigned long)btrfs_device_fsid(dev_item);
897 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400898 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400899
Yan Zheng2b820322008-11-17 21:11:30 -0500900 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400901out:
902 btrfs_free_path(path);
903 return ret;
904}
Chris Mason8f18cf12008-04-25 16:53:30 -0400905
Chris Masona061fc82008-05-07 11:43:44 -0400906static int btrfs_rm_dev_item(struct btrfs_root *root,
907 struct btrfs_device *device)
908{
909 int ret;
910 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -0400911 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400912 struct btrfs_trans_handle *trans;
913
914 root = root->fs_info->chunk_root;
915
916 path = btrfs_alloc_path();
917 if (!path)
918 return -ENOMEM;
919
920 trans = btrfs_start_transaction(root, 1);
921 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
922 key.type = BTRFS_DEV_ITEM_KEY;
923 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400924 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400925
926 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
927 if (ret < 0)
928 goto out;
929
930 if (ret > 0) {
931 ret = -ENOENT;
932 goto out;
933 }
934
935 ret = btrfs_del_item(trans, root, path);
936 if (ret)
937 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400938out:
939 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -0400940 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400941 btrfs_commit_transaction(trans, root);
942 return ret;
943}
944
945int btrfs_rm_device(struct btrfs_root *root, char *device_path)
946{
947 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500948 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -0400949 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400950 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -0400951 struct btrfs_super_block *disk_super;
952 u64 all_avail;
953 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500954 u64 num_devices;
955 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -0400956 int ret = 0;
957
Chris Masona061fc82008-05-07 11:43:44 -0400958 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -0400959 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -0400960
961 all_avail = root->fs_info->avail_data_alloc_bits |
962 root->fs_info->avail_system_alloc_bits |
963 root->fs_info->avail_metadata_alloc_bits;
964
965 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -0500966 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masona061fc82008-05-07 11:43:44 -0400967 printk("btrfs: unable to go below four devices on raid10\n");
968 ret = -EINVAL;
969 goto out;
970 }
971
972 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -0500973 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masona061fc82008-05-07 11:43:44 -0400974 printk("btrfs: unable to go below two devices on raid1\n");
975 ret = -EINVAL;
976 goto out;
977 }
978
Chris Masondfe25022008-05-13 13:46:40 -0400979 if (strcmp(device_path, "missing") == 0) {
980 struct list_head *cur;
981 struct list_head *devices;
982 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -0400983
Chris Masondfe25022008-05-13 13:46:40 -0400984 device = NULL;
985 devices = &root->fs_info->fs_devices->devices;
986 list_for_each(cur, devices) {
987 tmp = list_entry(cur, struct btrfs_device, dev_list);
988 if (tmp->in_fs_metadata && !tmp->bdev) {
989 device = tmp;
990 break;
991 }
992 }
993 bdev = NULL;
994 bh = NULL;
995 disk_super = NULL;
996 if (!device) {
997 printk("btrfs: no missing devices found to remove\n");
998 goto out;
999 }
Chris Masondfe25022008-05-13 13:46:40 -04001000 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001001 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001002 root->fs_info->bdev_holder);
1003 if (IS_ERR(bdev)) {
1004 ret = PTR_ERR(bdev);
1005 goto out;
1006 }
1007
Yan Zheng2b820322008-11-17 21:11:30 -05001008 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001009 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001010 if (!bh) {
1011 ret = -EIO;
1012 goto error_close;
1013 }
1014 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masondfe25022008-05-13 13:46:40 -04001015 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001016 dev_uuid = disk_super->dev_item.uuid;
1017 device = btrfs_find_device(root, devid, dev_uuid,
1018 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001019 if (!device) {
1020 ret = -ENOENT;
1021 goto error_brelse;
1022 }
Chris Masondfe25022008-05-13 13:46:40 -04001023 }
Yan Zheng2b820322008-11-17 21:11:30 -05001024
1025 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1026 printk("btrfs: unable to remove the only writeable device\n");
1027 ret = -EINVAL;
1028 goto error_brelse;
1029 }
1030
1031 if (device->writeable) {
1032 list_del_init(&device->dev_alloc_list);
1033 root->fs_info->fs_devices->rw_devices--;
1034 }
Chris Masona061fc82008-05-07 11:43:44 -04001035
1036 ret = btrfs_shrink_device(device, 0);
1037 if (ret)
1038 goto error_brelse;
1039
Chris Masona061fc82008-05-07 11:43:44 -04001040 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1041 if (ret)
1042 goto error_brelse;
1043
Yan Zheng2b820322008-11-17 21:11:30 -05001044 device->in_fs_metadata = 0;
1045 if (device->fs_devices == root->fs_info->fs_devices) {
1046 list_del_init(&device->dev_list);
1047 root->fs_info->fs_devices->num_devices--;
1048 if (device->bdev)
1049 device->fs_devices->open_devices--;
1050 }
1051
1052 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1053 struct btrfs_device, dev_list);
1054 if (device->bdev == root->fs_info->sb->s_bdev)
1055 root->fs_info->sb->s_bdev = next_device->bdev;
1056 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1057 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1058
1059 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1060 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1061
1062 if (device->fs_devices != root->fs_info->fs_devices) {
1063 BUG_ON(device->writeable);
1064 brelse(bh);
1065 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001066 close_bdev_exclusive(bdev, FMODE_READ);
Yan Zheng2b820322008-11-17 21:11:30 -05001067
1068 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -05001069 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001070 device->bdev = NULL;
1071 device->fs_devices->open_devices--;
1072 }
1073 if (device->fs_devices->open_devices == 0) {
1074 struct btrfs_fs_devices *fs_devices;
1075 fs_devices = root->fs_info->fs_devices;
1076 while (fs_devices) {
1077 if (fs_devices->seed == device->fs_devices)
1078 break;
1079 fs_devices = fs_devices->seed;
1080 }
1081 fs_devices->seed = device->fs_devices->seed;
1082 device->fs_devices->seed = NULL;
1083 __btrfs_close_devices(device->fs_devices);
1084 }
1085 ret = 0;
1086 goto out;
1087 }
1088
1089 /*
1090 * at this point, the device is zero sized. We want to
1091 * remove it from the devices list and zero out the old super
1092 */
1093 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001094 /* make sure this device isn't detected as part of
1095 * the FS anymore
1096 */
1097 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1098 set_buffer_dirty(bh);
1099 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001100 }
Yan Zheng2b820322008-11-17 21:11:30 -05001101 brelse(bh);
Chris Masona061fc82008-05-07 11:43:44 -04001102
Chris Masondfe25022008-05-13 13:46:40 -04001103 if (device->bdev) {
1104 /* one close for the device struct or super_block */
Chris Mason15916de2008-11-19 21:17:22 -05001105 close_bdev_exclusive(device->bdev, device->mode);
Chris Masondfe25022008-05-13 13:46:40 -04001106 }
1107 if (bdev) {
1108 /* one close for us */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001109 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masondfe25022008-05-13 13:46:40 -04001110 }
Chris Masona061fc82008-05-07 11:43:44 -04001111 kfree(device->name);
1112 kfree(device);
1113 ret = 0;
1114 goto out;
1115
1116error_brelse:
1117 brelse(bh);
1118error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001119 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001120 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001121out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001122 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001123 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001124 return ret;
1125}
1126
Yan Zheng2b820322008-11-17 21:11:30 -05001127/*
1128 * does all the dirty work required for changing file system's UUID.
1129 */
1130static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1131 struct btrfs_root *root)
1132{
1133 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1134 struct btrfs_fs_devices *old_devices;
1135 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1136 struct btrfs_device *device;
1137 u64 super_flags;
1138
1139 BUG_ON(!mutex_is_locked(&uuid_mutex));
1140 if (!fs_devices->seeding || fs_devices->opened != 1)
1141 return -EINVAL;
1142
1143 old_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1144 if (!old_devices)
1145 return -ENOMEM;
1146
1147 memcpy(old_devices, fs_devices, sizeof(*old_devices));
1148 old_devices->opened = 1;
1149 old_devices->sprouted = 1;
1150 INIT_LIST_HEAD(&old_devices->devices);
1151 INIT_LIST_HEAD(&old_devices->alloc_list);
1152 list_splice_init(&fs_devices->devices, &old_devices->devices);
1153 list_splice_init(&fs_devices->alloc_list, &old_devices->alloc_list);
1154 list_for_each_entry(device, &old_devices->devices, dev_list) {
1155 device->fs_devices = old_devices;
1156 }
1157 list_add(&old_devices->list, &fs_uuids);
1158
1159 fs_devices->seeding = 0;
1160 fs_devices->num_devices = 0;
1161 fs_devices->open_devices = 0;
1162 fs_devices->seed = old_devices;
1163
1164 generate_random_uuid(fs_devices->fsid);
1165 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1166 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1167 super_flags = btrfs_super_flags(disk_super) &
1168 ~BTRFS_SUPER_FLAG_SEEDING;
1169 btrfs_set_super_flags(disk_super, super_flags);
1170
1171 return 0;
1172}
1173
1174/*
1175 * strore the expected generation for seed devices in device items.
1176 */
1177static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root)
1179{
1180 struct btrfs_path *path;
1181 struct extent_buffer *leaf;
1182 struct btrfs_dev_item *dev_item;
1183 struct btrfs_device *device;
1184 struct btrfs_key key;
1185 u8 fs_uuid[BTRFS_UUID_SIZE];
1186 u8 dev_uuid[BTRFS_UUID_SIZE];
1187 u64 devid;
1188 int ret;
1189
1190 path = btrfs_alloc_path();
1191 if (!path)
1192 return -ENOMEM;
1193
1194 root = root->fs_info->chunk_root;
1195 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1196 key.offset = 0;
1197 key.type = BTRFS_DEV_ITEM_KEY;
1198
1199 while (1) {
1200 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1201 if (ret < 0)
1202 goto error;
1203
1204 leaf = path->nodes[0];
1205next_slot:
1206 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1207 ret = btrfs_next_leaf(root, path);
1208 if (ret > 0)
1209 break;
1210 if (ret < 0)
1211 goto error;
1212 leaf = path->nodes[0];
1213 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1214 btrfs_release_path(root, path);
1215 continue;
1216 }
1217
1218 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1219 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1220 key.type != BTRFS_DEV_ITEM_KEY)
1221 break;
1222
1223 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1224 struct btrfs_dev_item);
1225 devid = btrfs_device_id(leaf, dev_item);
1226 read_extent_buffer(leaf, dev_uuid,
1227 (unsigned long)btrfs_device_uuid(dev_item),
1228 BTRFS_UUID_SIZE);
1229 read_extent_buffer(leaf, fs_uuid,
1230 (unsigned long)btrfs_device_fsid(dev_item),
1231 BTRFS_UUID_SIZE);
1232 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1233 BUG_ON(!device);
1234
1235 if (device->fs_devices->seeding) {
1236 btrfs_set_device_generation(leaf, dev_item,
1237 device->generation);
1238 btrfs_mark_buffer_dirty(leaf);
1239 }
1240
1241 path->slots[0]++;
1242 goto next_slot;
1243 }
1244 ret = 0;
1245error:
1246 btrfs_free_path(path);
1247 return ret;
1248}
1249
Chris Mason788f20e2008-04-28 15:29:42 -04001250int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1251{
1252 struct btrfs_trans_handle *trans;
1253 struct btrfs_device *device;
1254 struct block_device *bdev;
1255 struct list_head *cur;
1256 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001257 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001258 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001259 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001260 int ret = 0;
1261
Yan Zheng2b820322008-11-17 21:11:30 -05001262 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1263 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001264
Chris Mason15916de2008-11-19 21:17:22 -05001265 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Chris Mason788f20e2008-04-28 15:29:42 -04001266 if (!bdev) {
1267 return -EIO;
1268 }
Chris Masona2135012008-06-25 16:01:30 -04001269
Yan Zheng2b820322008-11-17 21:11:30 -05001270 if (root->fs_info->fs_devices->seeding) {
1271 seeding_dev = 1;
1272 down_write(&sb->s_umount);
1273 mutex_lock(&uuid_mutex);
1274 }
1275
Chris Mason8c8bee12008-09-29 11:19:10 -04001276 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001277 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001278
Chris Mason788f20e2008-04-28 15:29:42 -04001279 devices = &root->fs_info->fs_devices->devices;
1280 list_for_each(cur, devices) {
1281 device = list_entry(cur, struct btrfs_device, dev_list);
1282 if (device->bdev == bdev) {
1283 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001284 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001285 }
1286 }
1287
1288 device = kzalloc(sizeof(*device), GFP_NOFS);
1289 if (!device) {
1290 /* we can safely leave the fs_devices entry around */
1291 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001292 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001293 }
1294
Chris Mason788f20e2008-04-28 15:29:42 -04001295 device->name = kstrdup(device_path, GFP_NOFS);
1296 if (!device->name) {
1297 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001298 ret = -ENOMEM;
1299 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001300 }
Yan Zheng2b820322008-11-17 21:11:30 -05001301
1302 ret = find_next_devid(root, &device->devid);
1303 if (ret) {
1304 kfree(device);
1305 goto error;
1306 }
1307
1308 trans = btrfs_start_transaction(root, 1);
1309 lock_chunks(root);
1310
1311 device->barriers = 1;
1312 device->writeable = 1;
1313 device->work.func = pending_bios_fn;
1314 generate_random_uuid(device->uuid);
1315 spin_lock_init(&device->io_lock);
1316 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001317 device->io_width = root->sectorsize;
1318 device->io_align = root->sectorsize;
1319 device->sector_size = root->sectorsize;
1320 device->total_bytes = i_size_read(bdev->bd_inode);
1321 device->dev_root = root->fs_info->dev_root;
1322 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001323 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001324 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001325 set_blocksize(device->bdev, 4096);
1326
Yan Zheng2b820322008-11-17 21:11:30 -05001327 if (seeding_dev) {
1328 sb->s_flags &= ~MS_RDONLY;
1329 ret = btrfs_prepare_sprout(trans, root);
1330 BUG_ON(ret);
1331 }
1332
1333 device->fs_devices = root->fs_info->fs_devices;
1334 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1335 list_add(&device->dev_alloc_list,
1336 &root->fs_info->fs_devices->alloc_list);
1337 root->fs_info->fs_devices->num_devices++;
1338 root->fs_info->fs_devices->open_devices++;
1339 root->fs_info->fs_devices->rw_devices++;
1340 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1341
Chris Mason788f20e2008-04-28 15:29:42 -04001342 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1343 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1344 total_bytes + device->total_bytes);
1345
1346 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1347 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1348 total_bytes + 1);
1349
Yan Zheng2b820322008-11-17 21:11:30 -05001350 if (seeding_dev) {
1351 ret = init_first_rw_device(trans, root, device);
1352 BUG_ON(ret);
1353 ret = btrfs_finish_sprout(trans, root);
1354 BUG_ON(ret);
1355 } else {
1356 ret = btrfs_add_device(trans, root, device);
1357 }
1358
Chris Mason7d9eb122008-07-08 14:19:17 -04001359 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001360 btrfs_commit_transaction(trans, root);
1361
1362 if (seeding_dev) {
1363 mutex_unlock(&uuid_mutex);
1364 up_write(&sb->s_umount);
1365
1366 ret = btrfs_relocate_sys_chunks(root);
1367 BUG_ON(ret);
1368 }
1369out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001370 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001371 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001372error:
Chris Mason15916de2008-11-19 21:17:22 -05001373 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001374 if (seeding_dev) {
1375 mutex_unlock(&uuid_mutex);
1376 up_write(&sb->s_umount);
1377 }
Chris Mason788f20e2008-04-28 15:29:42 -04001378 goto out;
1379}
1380
Christoph Hellwigb2950862008-12-02 09:54:17 -05001381static int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
Chris Masona1b32a52008-09-05 16:09:51 -04001382 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001383{
1384 int ret;
1385 struct btrfs_path *path;
1386 struct btrfs_root *root;
1387 struct btrfs_dev_item *dev_item;
1388 struct extent_buffer *leaf;
1389 struct btrfs_key key;
1390
1391 root = device->dev_root->fs_info->chunk_root;
1392
1393 path = btrfs_alloc_path();
1394 if (!path)
1395 return -ENOMEM;
1396
1397 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1398 key.type = BTRFS_DEV_ITEM_KEY;
1399 key.offset = device->devid;
1400
1401 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1402 if (ret < 0)
1403 goto out;
1404
1405 if (ret > 0) {
1406 ret = -ENOENT;
1407 goto out;
1408 }
1409
1410 leaf = path->nodes[0];
1411 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1412
1413 btrfs_set_device_id(leaf, dev_item, device->devid);
1414 btrfs_set_device_type(leaf, dev_item, device->type);
1415 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1416 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1417 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001418 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1419 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1420 btrfs_mark_buffer_dirty(leaf);
1421
1422out:
1423 btrfs_free_path(path);
1424 return ret;
1425}
1426
Chris Mason7d9eb122008-07-08 14:19:17 -04001427static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001428 struct btrfs_device *device, u64 new_size)
1429{
1430 struct btrfs_super_block *super_copy =
1431 &device->dev_root->fs_info->super_copy;
1432 u64 old_total = btrfs_super_total_bytes(super_copy);
1433 u64 diff = new_size - device->total_bytes;
1434
Yan Zheng2b820322008-11-17 21:11:30 -05001435 if (!device->writeable)
1436 return -EACCES;
1437 if (new_size <= device->total_bytes)
1438 return -EINVAL;
1439
Chris Mason8f18cf12008-04-25 16:53:30 -04001440 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001441 device->fs_devices->total_rw_bytes += diff;
1442
1443 device->total_bytes = new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04001444 return btrfs_update_device(trans, device);
1445}
1446
Chris Mason7d9eb122008-07-08 14:19:17 -04001447int btrfs_grow_device(struct btrfs_trans_handle *trans,
1448 struct btrfs_device *device, u64 new_size)
1449{
1450 int ret;
1451 lock_chunks(device->dev_root);
1452 ret = __btrfs_grow_device(trans, device, new_size);
1453 unlock_chunks(device->dev_root);
1454 return ret;
1455}
1456
Chris Mason8f18cf12008-04-25 16:53:30 -04001457static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *root,
1459 u64 chunk_tree, u64 chunk_objectid,
1460 u64 chunk_offset)
1461{
1462 int ret;
1463 struct btrfs_path *path;
1464 struct btrfs_key key;
1465
1466 root = root->fs_info->chunk_root;
1467 path = btrfs_alloc_path();
1468 if (!path)
1469 return -ENOMEM;
1470
1471 key.objectid = chunk_objectid;
1472 key.offset = chunk_offset;
1473 key.type = BTRFS_CHUNK_ITEM_KEY;
1474
1475 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1476 BUG_ON(ret);
1477
1478 ret = btrfs_del_item(trans, root, path);
1479 BUG_ON(ret);
1480
1481 btrfs_free_path(path);
1482 return 0;
1483}
1484
Christoph Hellwigb2950862008-12-02 09:54:17 -05001485static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001486 chunk_offset)
1487{
1488 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1489 struct btrfs_disk_key *disk_key;
1490 struct btrfs_chunk *chunk;
1491 u8 *ptr;
1492 int ret = 0;
1493 u32 num_stripes;
1494 u32 array_size;
1495 u32 len = 0;
1496 u32 cur;
1497 struct btrfs_key key;
1498
1499 array_size = btrfs_super_sys_array_size(super_copy);
1500
1501 ptr = super_copy->sys_chunk_array;
1502 cur = 0;
1503
1504 while (cur < array_size) {
1505 disk_key = (struct btrfs_disk_key *)ptr;
1506 btrfs_disk_key_to_cpu(&key, disk_key);
1507
1508 len = sizeof(*disk_key);
1509
1510 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1511 chunk = (struct btrfs_chunk *)(ptr + len);
1512 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1513 len += btrfs_chunk_item_size(num_stripes);
1514 } else {
1515 ret = -EIO;
1516 break;
1517 }
1518 if (key.objectid == chunk_objectid &&
1519 key.offset == chunk_offset) {
1520 memmove(ptr, ptr + len, array_size - (cur + len));
1521 array_size -= len;
1522 btrfs_set_super_sys_array_size(super_copy, array_size);
1523 } else {
1524 ptr += len;
1525 cur += len;
1526 }
1527 }
1528 return ret;
1529}
1530
Christoph Hellwigb2950862008-12-02 09:54:17 -05001531static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001532 u64 chunk_tree, u64 chunk_objectid,
1533 u64 chunk_offset)
1534{
1535 struct extent_map_tree *em_tree;
1536 struct btrfs_root *extent_root;
1537 struct btrfs_trans_handle *trans;
1538 struct extent_map *em;
1539 struct map_lookup *map;
1540 int ret;
1541 int i;
1542
Chris Mason323da792008-05-09 11:46:48 -04001543 printk("btrfs relocating chunk %llu\n",
1544 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001545 root = root->fs_info->chunk_root;
1546 extent_root = root->fs_info->extent_root;
1547 em_tree = &root->fs_info->mapping_tree.map_tree;
1548
1549 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001550 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001551 BUG_ON(ret);
1552
1553 trans = btrfs_start_transaction(root, 1);
1554 BUG_ON(!trans);
1555
Chris Mason7d9eb122008-07-08 14:19:17 -04001556 lock_chunks(root);
1557
Chris Mason8f18cf12008-04-25 16:53:30 -04001558 /*
1559 * step two, delete the device extents and the
1560 * chunk tree entries
1561 */
1562 spin_lock(&em_tree->lock);
1563 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1564 spin_unlock(&em_tree->lock);
1565
Chris Masona061fc82008-05-07 11:43:44 -04001566 BUG_ON(em->start > chunk_offset ||
1567 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001568 map = (struct map_lookup *)em->bdev;
1569
1570 for (i = 0; i < map->num_stripes; i++) {
1571 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1572 map->stripes[i].physical);
1573 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001574
Chris Masondfe25022008-05-13 13:46:40 -04001575 if (map->stripes[i].dev) {
1576 ret = btrfs_update_device(trans, map->stripes[i].dev);
1577 BUG_ON(ret);
1578 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001579 }
1580 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1581 chunk_offset);
1582
1583 BUG_ON(ret);
1584
1585 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1586 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1587 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001588 }
1589
Zheng Yan1a40e232008-09-26 10:09:34 -04001590 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1591 BUG_ON(ret);
1592
Chris Mason8f18cf12008-04-25 16:53:30 -04001593 spin_lock(&em_tree->lock);
1594 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001595 spin_unlock(&em_tree->lock);
1596
Chris Mason8f18cf12008-04-25 16:53:30 -04001597 kfree(map);
1598 em->bdev = NULL;
1599
1600 /* once for the tree */
1601 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001602 /* once for us */
1603 free_extent_map(em);
1604
Chris Mason7d9eb122008-07-08 14:19:17 -04001605 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001606 btrfs_end_transaction(trans, root);
1607 return 0;
1608}
1609
Yan Zheng2b820322008-11-17 21:11:30 -05001610static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1611{
1612 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1613 struct btrfs_path *path;
1614 struct extent_buffer *leaf;
1615 struct btrfs_chunk *chunk;
1616 struct btrfs_key key;
1617 struct btrfs_key found_key;
1618 u64 chunk_tree = chunk_root->root_key.objectid;
1619 u64 chunk_type;
1620 int ret;
1621
1622 path = btrfs_alloc_path();
1623 if (!path)
1624 return -ENOMEM;
1625
1626 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1627 key.offset = (u64)-1;
1628 key.type = BTRFS_CHUNK_ITEM_KEY;
1629
1630 while (1) {
1631 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1632 if (ret < 0)
1633 goto error;
1634 BUG_ON(ret == 0);
1635
1636 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1637 key.type);
1638 if (ret < 0)
1639 goto error;
1640 if (ret > 0)
1641 break;
1642
1643 leaf = path->nodes[0];
1644 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1645
1646 chunk = btrfs_item_ptr(leaf, path->slots[0],
1647 struct btrfs_chunk);
1648 chunk_type = btrfs_chunk_type(leaf, chunk);
1649 btrfs_release_path(chunk_root, path);
1650
1651 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1652 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1653 found_key.objectid,
1654 found_key.offset);
1655 BUG_ON(ret);
1656 }
1657
1658 if (found_key.offset == 0)
1659 break;
1660 key.offset = found_key.offset - 1;
1661 }
1662 ret = 0;
1663error:
1664 btrfs_free_path(path);
1665 return ret;
1666}
1667
Chris Masonec44a352008-04-28 15:29:52 -04001668static u64 div_factor(u64 num, int factor)
1669{
1670 if (factor == 10)
1671 return num;
1672 num *= factor;
1673 do_div(num, 10);
1674 return num;
1675}
1676
Chris Masonec44a352008-04-28 15:29:52 -04001677int btrfs_balance(struct btrfs_root *dev_root)
1678{
1679 int ret;
1680 struct list_head *cur;
1681 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1682 struct btrfs_device *device;
1683 u64 old_size;
1684 u64 size_to_free;
1685 struct btrfs_path *path;
1686 struct btrfs_key key;
1687 struct btrfs_chunk *chunk;
1688 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1689 struct btrfs_trans_handle *trans;
1690 struct btrfs_key found_key;
1691
Yan Zheng2b820322008-11-17 21:11:30 -05001692 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1693 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001694
Chris Mason7d9eb122008-07-08 14:19:17 -04001695 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001696 dev_root = dev_root->fs_info->dev_root;
1697
Chris Masonec44a352008-04-28 15:29:52 -04001698 /* step one make some room on all the devices */
1699 list_for_each(cur, devices) {
1700 device = list_entry(cur, struct btrfs_device, dev_list);
1701 old_size = device->total_bytes;
1702 size_to_free = div_factor(old_size, 1);
1703 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001704 if (!device->writeable ||
1705 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001706 continue;
1707
1708 ret = btrfs_shrink_device(device, old_size - size_to_free);
1709 BUG_ON(ret);
1710
1711 trans = btrfs_start_transaction(dev_root, 1);
1712 BUG_ON(!trans);
1713
1714 ret = btrfs_grow_device(trans, device, old_size);
1715 BUG_ON(ret);
1716
1717 btrfs_end_transaction(trans, dev_root);
1718 }
1719
1720 /* step two, relocate all the chunks */
1721 path = btrfs_alloc_path();
1722 BUG_ON(!path);
1723
1724 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1725 key.offset = (u64)-1;
1726 key.type = BTRFS_CHUNK_ITEM_KEY;
1727
1728 while(1) {
1729 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1730 if (ret < 0)
1731 goto error;
1732
1733 /*
1734 * this shouldn't happen, it means the last relocate
1735 * failed
1736 */
1737 if (ret == 0)
1738 break;
1739
1740 ret = btrfs_previous_item(chunk_root, path, 0,
1741 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001742 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001743 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001744
Chris Masonec44a352008-04-28 15:29:52 -04001745 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1746 path->slots[0]);
1747 if (found_key.objectid != key.objectid)
1748 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001749
Chris Masonec44a352008-04-28 15:29:52 -04001750 chunk = btrfs_item_ptr(path->nodes[0],
1751 path->slots[0],
1752 struct btrfs_chunk);
1753 key.offset = found_key.offset;
1754 /* chunk zero is special */
1755 if (key.offset == 0)
1756 break;
1757
Chris Mason7d9eb122008-07-08 14:19:17 -04001758 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001759 ret = btrfs_relocate_chunk(chunk_root,
1760 chunk_root->root_key.objectid,
1761 found_key.objectid,
1762 found_key.offset);
1763 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001764 }
1765 ret = 0;
1766error:
1767 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001768 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001769 return ret;
1770}
1771
Chris Mason8f18cf12008-04-25 16:53:30 -04001772/*
1773 * shrinking a device means finding all of the device extents past
1774 * the new size, and then following the back refs to the chunks.
1775 * The chunk relocation code actually frees the device extent
1776 */
1777int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1778{
1779 struct btrfs_trans_handle *trans;
1780 struct btrfs_root *root = device->dev_root;
1781 struct btrfs_dev_extent *dev_extent = NULL;
1782 struct btrfs_path *path;
1783 u64 length;
1784 u64 chunk_tree;
1785 u64 chunk_objectid;
1786 u64 chunk_offset;
1787 int ret;
1788 int slot;
1789 struct extent_buffer *l;
1790 struct btrfs_key key;
1791 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1792 u64 old_total = btrfs_super_total_bytes(super_copy);
1793 u64 diff = device->total_bytes - new_size;
1794
Yan Zheng2b820322008-11-17 21:11:30 -05001795 if (new_size >= device->total_bytes)
1796 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001797
1798 path = btrfs_alloc_path();
1799 if (!path)
1800 return -ENOMEM;
1801
1802 trans = btrfs_start_transaction(root, 1);
1803 if (!trans) {
1804 ret = -ENOMEM;
1805 goto done;
1806 }
1807
1808 path->reada = 2;
1809
Chris Mason7d9eb122008-07-08 14:19:17 -04001810 lock_chunks(root);
1811
Chris Mason8f18cf12008-04-25 16:53:30 -04001812 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001813 if (device->writeable)
1814 device->fs_devices->total_rw_bytes -= diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04001815 ret = btrfs_update_device(trans, device);
1816 if (ret) {
Chris Mason7d9eb122008-07-08 14:19:17 -04001817 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001818 btrfs_end_transaction(trans, root);
1819 goto done;
1820 }
1821 WARN_ON(diff > old_total);
1822 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001823 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001824 btrfs_end_transaction(trans, root);
1825
1826 key.objectid = device->devid;
1827 key.offset = (u64)-1;
1828 key.type = BTRFS_DEV_EXTENT_KEY;
1829
1830 while (1) {
1831 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1832 if (ret < 0)
1833 goto done;
1834
1835 ret = btrfs_previous_item(root, path, 0, key.type);
1836 if (ret < 0)
1837 goto done;
1838 if (ret) {
1839 ret = 0;
1840 goto done;
1841 }
1842
1843 l = path->nodes[0];
1844 slot = path->slots[0];
1845 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1846
1847 if (key.objectid != device->devid)
1848 goto done;
1849
1850 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1851 length = btrfs_dev_extent_length(l, dev_extent);
1852
1853 if (key.offset + length <= new_size)
1854 goto done;
1855
1856 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1857 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1858 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1859 btrfs_release_path(root, path);
1860
1861 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1862 chunk_offset);
1863 if (ret)
1864 goto done;
1865 }
1866
1867done:
1868 btrfs_free_path(path);
1869 return ret;
1870}
1871
Christoph Hellwigb2950862008-12-02 09:54:17 -05001872static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001873 struct btrfs_root *root,
1874 struct btrfs_key *key,
1875 struct btrfs_chunk *chunk, int item_size)
1876{
1877 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1878 struct btrfs_disk_key disk_key;
1879 u32 array_size;
1880 u8 *ptr;
1881
1882 array_size = btrfs_super_sys_array_size(super_copy);
1883 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1884 return -EFBIG;
1885
1886 ptr = super_copy->sys_chunk_array + array_size;
1887 btrfs_cpu_key_to_disk(&disk_key, key);
1888 memcpy(ptr, &disk_key, sizeof(disk_key));
1889 ptr += sizeof(disk_key);
1890 memcpy(ptr, chunk, item_size);
1891 item_size += sizeof(disk_key);
1892 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1893 return 0;
1894}
1895
Chris Masona1b32a52008-09-05 16:09:51 -04001896static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
1897 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04001898{
1899 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1900 return calc_size;
1901 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1902 return calc_size * (num_stripes / sub_stripes);
1903 else
1904 return calc_size * num_stripes;
1905}
1906
Yan Zheng2b820322008-11-17 21:11:30 -05001907static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1908 struct btrfs_root *extent_root,
1909 struct map_lookup **map_ret,
1910 u64 *num_bytes, u64 *stripe_size,
1911 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001912{
Chris Mason593060d2008-03-25 16:50:33 -04001913 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001914 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001915 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04001916 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05001917 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001918 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04001919 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05001920 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04001921 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001922 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001923 u64 max_chunk_size = calc_size;
1924 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001925 u64 avail;
1926 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001927 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04001928 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001929 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001930 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001931 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001932 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001933 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001934 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001935
Chris Masonec44a352008-04-28 15:29:52 -04001936 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1937 (type & BTRFS_BLOCK_GROUP_DUP)) {
1938 WARN_ON(1);
1939 type &= ~BTRFS_BLOCK_GROUP_DUP;
1940 }
Yan Zheng2b820322008-11-17 21:11:30 -05001941 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04001942 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001943
Chris Masona40a90a2008-04-18 11:55:51 -04001944 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001945 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04001946 min_stripes = 2;
1947 }
1948 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04001949 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001950 min_stripes = 2;
1951 }
Chris Mason8790d502008-04-03 16:29:03 -04001952 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001953 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001954 if (num_stripes < 2)
1955 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04001956 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04001957 }
Chris Mason321aecc2008-04-16 10:49:51 -04001958 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001959 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04001960 if (num_stripes < 4)
1961 return -ENOSPC;
1962 num_stripes &= ~(u32)1;
1963 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001964 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04001965 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04001966
1967 if (type & BTRFS_BLOCK_GROUP_DATA) {
1968 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001969 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001970 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1971 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001972 min_stripe_size = 32 * 1024 * 1024;
1973 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1974 calc_size = 8 * 1024 * 1024;
1975 max_chunk_size = calc_size * 2;
1976 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001977 }
1978
Yan Zheng2b820322008-11-17 21:11:30 -05001979 /* we don't want a chunk larger than 10% of writeable space */
1980 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
1981 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001982
Chris Masona40a90a2008-04-18 11:55:51 -04001983again:
Yan Zheng2b820322008-11-17 21:11:30 -05001984 if (!map || map->num_stripes != num_stripes) {
1985 kfree(map);
1986 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1987 if (!map)
1988 return -ENOMEM;
1989 map->num_stripes = num_stripes;
1990 }
1991
Chris Mason9b3f68b2008-04-18 10:29:51 -04001992 if (calc_size * num_stripes > max_chunk_size) {
1993 calc_size = max_chunk_size;
1994 do_div(calc_size, num_stripes);
1995 do_div(calc_size, stripe_len);
1996 calc_size *= stripe_len;
1997 }
1998 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04001999 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002000
Chris Mason9b3f68b2008-04-18 10:29:51 -04002001 do_div(calc_size, stripe_len);
2002 calc_size *= stripe_len;
2003
Yan Zheng2b820322008-11-17 21:11:30 -05002004 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002005 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002006
2007 if (type & BTRFS_BLOCK_GROUP_DUP)
2008 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002009 else
2010 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002011
Josef Bacik0f9dd462008-09-23 13:14:11 -04002012 /*
2013 * we add 1MB because we never use the first 1MB of the device, unless
2014 * we've looped, then we are likely allocating the maximum amount of
2015 * space left already
2016 */
2017 if (!looped)
2018 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002019
Yan Zheng2b820322008-11-17 21:11:30 -05002020 INIT_LIST_HEAD(&private_devs);
Chris Mason6324fbf2008-03-24 15:01:59 -04002021 while(index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002022 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002023 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002024 if (device->total_bytes > device->bytes_used)
2025 avail = device->total_bytes - device->bytes_used;
2026 else
2027 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002028 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002029
Chris Masondfe25022008-05-13 13:46:40 -04002030 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002031 ret = find_free_dev_extent(trans, device,
2032 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002033 if (ret == 0) {
2034 list_move_tail(&device->dev_alloc_list,
2035 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002036 map->stripes[index].dev = device;
2037 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002038 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002039 if (type & BTRFS_BLOCK_GROUP_DUP) {
2040 map->stripes[index].dev = device;
2041 map->stripes[index].physical =
2042 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002043 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002044 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002045 }
Chris Masondfe25022008-05-13 13:46:40 -04002046 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002047 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002048 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002049 break;
2050 }
Yan Zheng2b820322008-11-17 21:11:30 -05002051 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002052 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002053 if (index >= min_stripes) {
2054 num_stripes = index;
2055 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2056 num_stripes /= sub_stripes;
2057 num_stripes *= sub_stripes;
2058 }
2059 looped = 1;
2060 goto again;
2061 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002062 if (!looped && max_avail > 0) {
2063 looped = 1;
2064 calc_size = max_avail;
2065 goto again;
2066 }
Yan Zheng2b820322008-11-17 21:11:30 -05002067 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002068 return -ENOSPC;
2069 }
Chris Mason593060d2008-03-25 16:50:33 -04002070 map->sector_size = extent_root->sectorsize;
2071 map->stripe_len = stripe_len;
2072 map->io_align = stripe_len;
2073 map->io_width = stripe_len;
2074 map->type = type;
2075 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002076 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002077
Yan Zheng2b820322008-11-17 21:11:30 -05002078 *map_ret = map;
2079 *stripe_size = calc_size;
2080 *num_bytes = chunk_bytes_by_type(type, calc_size,
2081 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002082
2083 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002084 if (!em) {
2085 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002086 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002087 }
Chris Mason0b86a832008-03-24 15:01:56 -04002088 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002089 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002090 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002091 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002092 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002093
Chris Mason0b86a832008-03-24 15:01:56 -04002094 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2095 spin_lock(&em_tree->lock);
2096 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002097 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002098 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002099 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002100
2101 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2102 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2103 start, *num_bytes);
2104 BUG_ON(ret);
2105
2106 index = 0;
2107 while (index < map->num_stripes) {
2108 device = map->stripes[index].dev;
2109 dev_offset = map->stripes[index].physical;
2110
2111 ret = btrfs_alloc_dev_extent(trans, device,
2112 info->chunk_root->root_key.objectid,
2113 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2114 start, dev_offset, calc_size);
2115 BUG_ON(ret);
2116 index++;
2117 }
2118
2119 return 0;
2120}
2121
2122static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2123 struct btrfs_root *extent_root,
2124 struct map_lookup *map, u64 chunk_offset,
2125 u64 chunk_size, u64 stripe_size)
2126{
2127 u64 dev_offset;
2128 struct btrfs_key key;
2129 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2130 struct btrfs_device *device;
2131 struct btrfs_chunk *chunk;
2132 struct btrfs_stripe *stripe;
2133 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2134 int index = 0;
2135 int ret;
2136
2137 chunk = kzalloc(item_size, GFP_NOFS);
2138 if (!chunk)
2139 return -ENOMEM;
2140
2141 index = 0;
2142 while (index < map->num_stripes) {
2143 device = map->stripes[index].dev;
2144 device->bytes_used += stripe_size;
2145 ret = btrfs_update_device(trans, device);
2146 BUG_ON(ret);
2147 index++;
2148 }
2149
2150 index = 0;
2151 stripe = &chunk->stripe;
2152 while (index < map->num_stripes) {
2153 device = map->stripes[index].dev;
2154 dev_offset = map->stripes[index].physical;
2155
2156 btrfs_set_stack_stripe_devid(stripe, device->devid);
2157 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2158 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2159 stripe++;
2160 index++;
2161 }
2162
2163 btrfs_set_stack_chunk_length(chunk, chunk_size);
2164 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2165 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2166 btrfs_set_stack_chunk_type(chunk, map->type);
2167 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2168 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2169 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2170 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2171 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2172
2173 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2174 key.type = BTRFS_CHUNK_ITEM_KEY;
2175 key.offset = chunk_offset;
2176
2177 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2178 BUG_ON(ret);
2179
2180 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2181 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2182 item_size);
2183 BUG_ON(ret);
2184 }
2185 kfree(chunk);
2186 return 0;
2187}
2188
2189/*
2190 * Chunk allocation falls into two parts. The first part does works
2191 * that make the new allocated chunk useable, but not do any operation
2192 * that modifies the chunk tree. The second part does the works that
2193 * require modifying the chunk tree. This division is important for the
2194 * bootstrap process of adding storage to a seed btrfs.
2195 */
2196int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2197 struct btrfs_root *extent_root, u64 type)
2198{
2199 u64 chunk_offset;
2200 u64 chunk_size;
2201 u64 stripe_size;
2202 struct map_lookup *map;
2203 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2204 int ret;
2205
2206 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2207 &chunk_offset);
2208 if (ret)
2209 return ret;
2210
2211 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2212 &stripe_size, chunk_offset, type);
2213 if (ret)
2214 return ret;
2215
2216 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2217 chunk_size, stripe_size);
2218 BUG_ON(ret);
2219 return 0;
2220}
2221
2222static int noinline init_first_rw_device(struct btrfs_trans_handle *trans,
2223 struct btrfs_root *root,
2224 struct btrfs_device *device)
2225{
2226 u64 chunk_offset;
2227 u64 sys_chunk_offset;
2228 u64 chunk_size;
2229 u64 sys_chunk_size;
2230 u64 stripe_size;
2231 u64 sys_stripe_size;
2232 u64 alloc_profile;
2233 struct map_lookup *map;
2234 struct map_lookup *sys_map;
2235 struct btrfs_fs_info *fs_info = root->fs_info;
2236 struct btrfs_root *extent_root = fs_info->extent_root;
2237 int ret;
2238
2239 ret = find_next_chunk(fs_info->chunk_root,
2240 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2241 BUG_ON(ret);
2242
2243 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2244 (fs_info->metadata_alloc_profile &
2245 fs_info->avail_metadata_alloc_bits);
2246 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2247
2248 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2249 &stripe_size, chunk_offset, alloc_profile);
2250 BUG_ON(ret);
2251
2252 sys_chunk_offset = chunk_offset + chunk_size;
2253
2254 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2255 (fs_info->system_alloc_profile &
2256 fs_info->avail_system_alloc_bits);
2257 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2258
2259 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2260 &sys_chunk_size, &sys_stripe_size,
2261 sys_chunk_offset, alloc_profile);
2262 BUG_ON(ret);
2263
2264 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2265 BUG_ON(ret);
2266
2267 /*
2268 * Modifying chunk tree needs allocating new blocks from both
2269 * system block group and metadata block group. So we only can
2270 * do operations require modifying the chunk tree after both
2271 * block groups were created.
2272 */
2273 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2274 chunk_size, stripe_size);
2275 BUG_ON(ret);
2276
2277 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2278 sys_chunk_offset, sys_chunk_size,
2279 sys_stripe_size);
2280 BUG_ON(ret);
2281 return 0;
2282}
2283
2284int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2285{
2286 struct extent_map *em;
2287 struct map_lookup *map;
2288 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2289 int readonly = 0;
2290 int i;
2291
2292 spin_lock(&map_tree->map_tree.lock);
2293 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2294 spin_unlock(&map_tree->map_tree.lock);
2295 if (!em)
2296 return 1;
2297
2298 map = (struct map_lookup *)em->bdev;
2299 for (i = 0; i < map->num_stripes; i++) {
2300 if (!map->stripes[i].dev->writeable) {
2301 readonly = 1;
2302 break;
2303 }
2304 }
2305 free_extent_map(em);
2306 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002307}
2308
2309void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2310{
2311 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2312}
2313
2314void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2315{
2316 struct extent_map *em;
2317
2318 while(1) {
2319 spin_lock(&tree->map_tree.lock);
2320 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2321 if (em)
2322 remove_extent_mapping(&tree->map_tree, em);
2323 spin_unlock(&tree->map_tree.lock);
2324 if (!em)
2325 break;
2326 kfree(em->bdev);
2327 /* once for us */
2328 free_extent_map(em);
2329 /* once for the tree */
2330 free_extent_map(em);
2331 }
2332}
2333
Chris Masonf1885912008-04-09 16:28:12 -04002334int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2335{
2336 struct extent_map *em;
2337 struct map_lookup *map;
2338 struct extent_map_tree *em_tree = &map_tree->map_tree;
2339 int ret;
2340
2341 spin_lock(&em_tree->lock);
2342 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002343 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002344 BUG_ON(!em);
2345
2346 BUG_ON(em->start > logical || em->start + em->len < logical);
2347 map = (struct map_lookup *)em->bdev;
2348 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2349 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002350 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2351 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002352 else
2353 ret = 1;
2354 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002355 return ret;
2356}
2357
Chris Masondfe25022008-05-13 13:46:40 -04002358static int find_live_mirror(struct map_lookup *map, int first, int num,
2359 int optimal)
2360{
2361 int i;
2362 if (map->stripes[optimal].dev->bdev)
2363 return optimal;
2364 for (i = first; i < first + num; i++) {
2365 if (map->stripes[i].dev->bdev)
2366 return i;
2367 }
2368 /* we couldn't find one that doesn't fail. Just return something
2369 * and the io error handling code will clean up eventually
2370 */
2371 return optimal;
2372}
2373
Chris Masonf2d8d742008-04-21 10:03:05 -04002374static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2375 u64 logical, u64 *length,
2376 struct btrfs_multi_bio **multi_ret,
2377 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002378{
2379 struct extent_map *em;
2380 struct map_lookup *map;
2381 struct extent_map_tree *em_tree = &map_tree->map_tree;
2382 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002383 u64 stripe_offset;
2384 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002385 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002386 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002387 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002388 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002389 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002390 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002391 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002392
Chris Masoncea9e442008-04-09 16:28:12 -04002393 if (multi_ret && !(rw & (1 << BIO_RW))) {
2394 stripes_allocated = 1;
2395 }
2396again:
2397 if (multi_ret) {
2398 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2399 GFP_NOFS);
2400 if (!multi)
2401 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002402
2403 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002404 }
Chris Mason0b86a832008-03-24 15:01:56 -04002405
2406 spin_lock(&em_tree->lock);
2407 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002408 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002409
2410 if (!em && unplug_page)
2411 return 0;
2412
Chris Mason3b951512008-04-17 11:29:12 -04002413 if (!em) {
Chris Masona061fc82008-05-07 11:43:44 -04002414 printk("unable to find logical %Lu len %Lu\n", logical, *length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002415 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002416 }
Chris Mason0b86a832008-03-24 15:01:56 -04002417
2418 BUG_ON(em->start > logical || em->start + em->len < logical);
2419 map = (struct map_lookup *)em->bdev;
2420 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002421
Chris Masonf1885912008-04-09 16:28:12 -04002422 if (mirror_num > map->num_stripes)
2423 mirror_num = 0;
2424
Chris Masoncea9e442008-04-09 16:28:12 -04002425 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002426 if (rw & (1 << BIO_RW)) {
2427 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2428 BTRFS_BLOCK_GROUP_DUP)) {
2429 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002430 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002431 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2432 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002433 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002434 }
2435 }
2436 if (multi_ret && rw == WRITE &&
2437 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002438 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002439 free_extent_map(em);
2440 kfree(multi);
2441 goto again;
2442 }
Chris Mason593060d2008-03-25 16:50:33 -04002443 stripe_nr = offset;
2444 /*
2445 * stripe_nr counts the total number of stripes we have to stride
2446 * to get to this block
2447 */
2448 do_div(stripe_nr, map->stripe_len);
2449
2450 stripe_offset = stripe_nr * map->stripe_len;
2451 BUG_ON(offset < stripe_offset);
2452
2453 /* stripe_offset is the offset of this block in its stripe*/
2454 stripe_offset = offset - stripe_offset;
2455
Chris Masoncea9e442008-04-09 16:28:12 -04002456 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002457 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002458 BTRFS_BLOCK_GROUP_DUP)) {
2459 /* we limit the length of each bio to what fits in a stripe */
2460 *length = min_t(u64, em->len - offset,
2461 map->stripe_len - stripe_offset);
2462 } else {
2463 *length = em->len - offset;
2464 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002465
2466 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002467 goto out;
2468
Chris Masonf2d8d742008-04-21 10:03:05 -04002469 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002470 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002471 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002472 if (unplug_page || (rw & (1 << BIO_RW)))
2473 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002474 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002475 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002476 else {
2477 stripe_index = find_live_mirror(map, 0,
2478 map->num_stripes,
2479 current->pid % map->num_stripes);
2480 }
Chris Mason2fff7342008-04-29 14:12:09 -04002481
Chris Mason611f0e02008-04-03 16:29:03 -04002482 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002483 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002484 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002485 else if (mirror_num)
2486 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002487
Chris Mason321aecc2008-04-16 10:49:51 -04002488 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2489 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002490
2491 stripe_index = do_div(stripe_nr, factor);
2492 stripe_index *= map->sub_stripes;
2493
Chris Masonf2d8d742008-04-21 10:03:05 -04002494 if (unplug_page || (rw & (1 << BIO_RW)))
2495 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002496 else if (mirror_num)
2497 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002498 else {
2499 stripe_index = find_live_mirror(map, stripe_index,
2500 map->sub_stripes, stripe_index +
2501 current->pid % map->sub_stripes);
2502 }
Chris Mason8790d502008-04-03 16:29:03 -04002503 } else {
2504 /*
2505 * after this do_div call, stripe_nr is the number of stripes
2506 * on this device we have to walk to find the data, and
2507 * stripe_index is the number of our device in the stripe array
2508 */
2509 stripe_index = do_div(stripe_nr, map->num_stripes);
2510 }
Chris Mason593060d2008-03-25 16:50:33 -04002511 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002512
Chris Masonf2d8d742008-04-21 10:03:05 -04002513 for (i = 0; i < num_stripes; i++) {
2514 if (unplug_page) {
2515 struct btrfs_device *device;
2516 struct backing_dev_info *bdi;
2517
2518 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002519 if (device->bdev) {
2520 bdi = blk_get_backing_dev_info(device->bdev);
2521 if (bdi->unplug_io_fn) {
2522 bdi->unplug_io_fn(bdi, unplug_page);
2523 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002524 }
2525 } else {
2526 multi->stripes[i].physical =
2527 map->stripes[stripe_index].physical +
2528 stripe_offset + stripe_nr * map->stripe_len;
2529 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2530 }
Chris Masoncea9e442008-04-09 16:28:12 -04002531 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002532 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002533 if (multi_ret) {
2534 *multi_ret = multi;
2535 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002536 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002537 }
Chris Masoncea9e442008-04-09 16:28:12 -04002538out:
Chris Mason0b86a832008-03-24 15:01:56 -04002539 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002540 return 0;
2541}
2542
Chris Masonf2d8d742008-04-21 10:03:05 -04002543int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2544 u64 logical, u64 *length,
2545 struct btrfs_multi_bio **multi_ret, int mirror_num)
2546{
2547 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2548 mirror_num, NULL);
2549}
2550
Yan Zhenga512bbf2008-12-08 16:46:26 -05002551int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2552 u64 chunk_start, u64 physical, u64 devid,
2553 u64 **logical, int *naddrs, int *stripe_len)
2554{
2555 struct extent_map_tree *em_tree = &map_tree->map_tree;
2556 struct extent_map *em;
2557 struct map_lookup *map;
2558 u64 *buf;
2559 u64 bytenr;
2560 u64 length;
2561 u64 stripe_nr;
2562 int i, j, nr = 0;
2563
2564 spin_lock(&em_tree->lock);
2565 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2566 spin_unlock(&em_tree->lock);
2567
2568 BUG_ON(!em || em->start != chunk_start);
2569 map = (struct map_lookup *)em->bdev;
2570
2571 length = em->len;
2572 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2573 do_div(length, map->num_stripes / map->sub_stripes);
2574 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2575 do_div(length, map->num_stripes);
2576
2577 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2578 BUG_ON(!buf);
2579
2580 for (i = 0; i < map->num_stripes; i++) {
2581 if (devid && map->stripes[i].dev->devid != devid)
2582 continue;
2583 if (map->stripes[i].physical > physical ||
2584 map->stripes[i].physical + length <= physical)
2585 continue;
2586
2587 stripe_nr = physical - map->stripes[i].physical;
2588 do_div(stripe_nr, map->stripe_len);
2589
2590 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2591 stripe_nr = stripe_nr * map->num_stripes + i;
2592 do_div(stripe_nr, map->sub_stripes);
2593 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2594 stripe_nr = stripe_nr * map->num_stripes + i;
2595 }
2596 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002597 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002598 for (j = 0; j < nr; j++) {
2599 if (buf[j] == bytenr)
2600 break;
2601 }
Chris Mason934d3752008-12-08 16:43:10 -05002602 if (j == nr) {
2603 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002604 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002605 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002606 }
2607
2608 for (i = 0; i > nr; i++) {
2609 struct btrfs_multi_bio *multi;
2610 struct btrfs_bio_stripe *stripe;
2611 int ret;
2612
2613 length = 1;
2614 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2615 &length, &multi, 0);
2616 BUG_ON(ret);
2617
2618 stripe = multi->stripes;
2619 for (j = 0; j < multi->num_stripes; j++) {
2620 if (stripe->physical >= physical &&
2621 physical < stripe->physical + length)
2622 break;
2623 }
2624 BUG_ON(j >= multi->num_stripes);
2625 kfree(multi);
2626 }
2627
2628 *logical = buf;
2629 *naddrs = nr;
2630 *stripe_len = map->stripe_len;
2631
2632 free_extent_map(em);
2633 return 0;
2634}
2635
Chris Masonf2d8d742008-04-21 10:03:05 -04002636int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2637 u64 logical, struct page *page)
2638{
2639 u64 length = PAGE_CACHE_SIZE;
2640 return __btrfs_map_block(map_tree, READ, logical, &length,
2641 NULL, 0, page);
2642}
2643
2644
Chris Mason8790d502008-04-03 16:29:03 -04002645static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002646{
Chris Masoncea9e442008-04-09 16:28:12 -04002647 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002648 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002649
Chris Mason8790d502008-04-03 16:29:03 -04002650 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002651 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002652
Chris Mason7d2b4da2008-08-05 10:13:57 -04002653 if (bio == multi->orig_bio)
2654 is_orig_bio = 1;
2655
Chris Masoncea9e442008-04-09 16:28:12 -04002656 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002657 if (!is_orig_bio) {
2658 bio_put(bio);
2659 bio = multi->orig_bio;
2660 }
Chris Mason8790d502008-04-03 16:29:03 -04002661 bio->bi_private = multi->private;
2662 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002663 /* only send an error to the higher layers if it is
2664 * beyond the tolerance of the multi-bio
2665 */
Chris Mason1259ab72008-05-12 13:39:03 -04002666 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002667 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002668 } else if (err) {
2669 /*
2670 * this bio is actually up to date, we didn't
2671 * go over the max number of errors
2672 */
2673 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002674 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002675 }
Chris Mason8790d502008-04-03 16:29:03 -04002676 kfree(multi);
2677
2678 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002679 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002680 bio_put(bio);
2681 }
Chris Mason8790d502008-04-03 16:29:03 -04002682}
2683
Chris Mason8b712842008-06-11 16:50:36 -04002684struct async_sched {
2685 struct bio *bio;
2686 int rw;
2687 struct btrfs_fs_info *info;
2688 struct btrfs_work work;
2689};
2690
2691/*
2692 * see run_scheduled_bios for a description of why bios are collected for
2693 * async submit.
2694 *
2695 * This will add one bio to the pending list for a device and make sure
2696 * the work struct is scheduled.
2697 */
Chris Masona1b32a52008-09-05 16:09:51 -04002698static int noinline schedule_bio(struct btrfs_root *root,
2699 struct btrfs_device *device,
2700 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002701{
2702 int should_queue = 1;
2703
2704 /* don't bother with additional async steps for reads, right now */
2705 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002706 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002707 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002708 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002709 return 0;
2710 }
2711
2712 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04002713 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002714 * higher layers. Otherwise, the async bio makes it appear we have
2715 * made progress against dirty pages when we've really just put it
2716 * on a queue for later
2717 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04002718 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002719 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002720 bio->bi_next = NULL;
2721 bio->bi_rw |= rw;
2722
2723 spin_lock(&device->io_lock);
2724
2725 if (device->pending_bio_tail)
2726 device->pending_bio_tail->bi_next = bio;
2727
2728 device->pending_bio_tail = bio;
2729 if (!device->pending_bios)
2730 device->pending_bios = bio;
2731 if (device->running_pending)
2732 should_queue = 0;
2733
2734 spin_unlock(&device->io_lock);
2735
2736 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002737 btrfs_queue_worker(&root->fs_info->submit_workers,
2738 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002739 return 0;
2740}
2741
Chris Masonf1885912008-04-09 16:28:12 -04002742int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002743 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002744{
2745 struct btrfs_mapping_tree *map_tree;
2746 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002747 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002748 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002749 u64 length = 0;
2750 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002751 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002752 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002753 int dev_nr = 0;
2754 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002755
Chris Masonf2d8d742008-04-21 10:03:05 -04002756 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002757 map_tree = &root->fs_info->mapping_tree;
2758 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002759
Chris Masonf1885912008-04-09 16:28:12 -04002760 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2761 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002762 BUG_ON(ret);
2763
2764 total_devs = multi->num_stripes;
2765 if (map_length < length) {
2766 printk("mapping failed logical %Lu bio len %Lu "
2767 "len %Lu\n", logical, length, map_length);
2768 BUG();
2769 }
2770 multi->end_io = first_bio->bi_end_io;
2771 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002772 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002773 atomic_set(&multi->stripes_pending, multi->num_stripes);
2774
Chris Mason8790d502008-04-03 16:29:03 -04002775 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002776 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002777 if (dev_nr < total_devs - 1) {
2778 bio = bio_clone(first_bio, GFP_NOFS);
2779 BUG_ON(!bio);
2780 } else {
2781 bio = first_bio;
2782 }
2783 bio->bi_private = multi;
2784 bio->bi_end_io = end_bio_multi_stripe;
2785 }
Chris Masoncea9e442008-04-09 16:28:12 -04002786 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2787 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002788 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002789 if (dev && dev->bdev) {
2790 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002791 if (async_submit)
2792 schedule_bio(root, dev, rw, bio);
2793 else
2794 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002795 } else {
2796 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2797 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002798 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002799 }
Chris Mason8790d502008-04-03 16:29:03 -04002800 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002801 }
Chris Masoncea9e442008-04-09 16:28:12 -04002802 if (total_devs == 1)
2803 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002804 return 0;
2805}
2806
Chris Masona4437552008-04-18 10:29:38 -04002807struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05002808 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04002809{
Yan Zheng2b820322008-11-17 21:11:30 -05002810 struct btrfs_device *device;
2811 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002812
Yan Zheng2b820322008-11-17 21:11:30 -05002813 cur_devices = root->fs_info->fs_devices;
2814 while (cur_devices) {
2815 if (!fsid ||
2816 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2817 device = __find_device(&cur_devices->devices,
2818 devid, uuid);
2819 if (device)
2820 return device;
2821 }
2822 cur_devices = cur_devices->seed;
2823 }
2824 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002825}
2826
Chris Masondfe25022008-05-13 13:46:40 -04002827static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2828 u64 devid, u8 *dev_uuid)
2829{
2830 struct btrfs_device *device;
2831 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2832
2833 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05002834 if (!device)
2835 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04002836 list_add(&device->dev_list,
2837 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04002838 device->barriers = 1;
2839 device->dev_root = root->fs_info->dev_root;
2840 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002841 device->work.func = pending_bios_fn;
Chris Masondfe25022008-05-13 13:46:40 -04002842 fs_devices->num_devices++;
2843 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05002844 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04002845 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2846 return device;
2847}
2848
Chris Mason0b86a832008-03-24 15:01:56 -04002849static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2850 struct extent_buffer *leaf,
2851 struct btrfs_chunk *chunk)
2852{
2853 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2854 struct map_lookup *map;
2855 struct extent_map *em;
2856 u64 logical;
2857 u64 length;
2858 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002859 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002860 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002861 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002862 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002863
Chris Masone17cade2008-04-15 15:41:47 -04002864 logical = key->offset;
2865 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002866
Chris Mason0b86a832008-03-24 15:01:56 -04002867 spin_lock(&map_tree->map_tree.lock);
2868 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002869 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002870
2871 /* already mapped? */
2872 if (em && em->start <= logical && em->start + em->len > logical) {
2873 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002874 return 0;
2875 } else if (em) {
2876 free_extent_map(em);
2877 }
Chris Mason0b86a832008-03-24 15:01:56 -04002878
2879 map = kzalloc(sizeof(*map), GFP_NOFS);
2880 if (!map)
2881 return -ENOMEM;
2882
2883 em = alloc_extent_map(GFP_NOFS);
2884 if (!em)
2885 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002886 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2887 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002888 if (!map) {
2889 free_extent_map(em);
2890 return -ENOMEM;
2891 }
2892
2893 em->bdev = (struct block_device *)map;
2894 em->start = logical;
2895 em->len = length;
2896 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002897 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002898
Chris Mason593060d2008-03-25 16:50:33 -04002899 map->num_stripes = num_stripes;
2900 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2901 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2902 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2903 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2904 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002905 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002906 for (i = 0; i < num_stripes; i++) {
2907 map->stripes[i].physical =
2908 btrfs_stripe_offset_nr(leaf, chunk, i);
2909 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002910 read_extent_buffer(leaf, uuid, (unsigned long)
2911 btrfs_stripe_dev_uuid_nr(chunk, i),
2912 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002913 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2914 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04002915 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002916 kfree(map);
2917 free_extent_map(em);
2918 return -EIO;
2919 }
Chris Masondfe25022008-05-13 13:46:40 -04002920 if (!map->stripes[i].dev) {
2921 map->stripes[i].dev =
2922 add_missing_dev(root, devid, uuid);
2923 if (!map->stripes[i].dev) {
2924 kfree(map);
2925 free_extent_map(em);
2926 return -EIO;
2927 }
2928 }
2929 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002930 }
2931
2932 spin_lock(&map_tree->map_tree.lock);
2933 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002934 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002935 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002936 free_extent_map(em);
2937
2938 return 0;
2939}
2940
2941static int fill_device_from_item(struct extent_buffer *leaf,
2942 struct btrfs_dev_item *dev_item,
2943 struct btrfs_device *device)
2944{
2945 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04002946
2947 device->devid = btrfs_device_id(leaf, dev_item);
2948 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2949 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2950 device->type = btrfs_device_type(leaf, dev_item);
2951 device->io_align = btrfs_device_io_align(leaf, dev_item);
2952 device->io_width = btrfs_device_io_width(leaf, dev_item);
2953 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002954
2955 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04002956 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002957
Chris Mason0b86a832008-03-24 15:01:56 -04002958 return 0;
2959}
2960
Yan Zheng2b820322008-11-17 21:11:30 -05002961static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
2962{
2963 struct btrfs_fs_devices *fs_devices;
2964 int ret;
2965
2966 mutex_lock(&uuid_mutex);
2967
2968 fs_devices = root->fs_info->fs_devices->seed;
2969 while (fs_devices) {
2970 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2971 ret = 0;
2972 goto out;
2973 }
2974 fs_devices = fs_devices->seed;
2975 }
2976
2977 fs_devices = find_fsid(fsid);
2978 if (!fs_devices) {
2979 ret = -ENOENT;
2980 goto out;
2981 }
2982 if (fs_devices->opened) {
2983 ret = -EBUSY;
2984 goto out;
2985 }
2986
Christoph Hellwig97288f22008-12-02 06:36:09 -05002987 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05002988 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05002989 if (ret)
2990 goto out;
2991
2992 if (!fs_devices->seeding) {
2993 __btrfs_close_devices(fs_devices);
2994 ret = -EINVAL;
2995 goto out;
2996 }
2997
2998 fs_devices->seed = root->fs_info->fs_devices->seed;
2999 root->fs_info->fs_devices->seed = fs_devices;
3000 fs_devices->sprouted = 1;
3001out:
3002 mutex_unlock(&uuid_mutex);
3003 return ret;
3004}
3005
Chris Mason0d81ba52008-03-24 15:02:07 -04003006static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003007 struct extent_buffer *leaf,
3008 struct btrfs_dev_item *dev_item)
3009{
3010 struct btrfs_device *device;
3011 u64 devid;
3012 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003013 int seed_devices = 0;
3014 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003015 u8 dev_uuid[BTRFS_UUID_SIZE];
3016
Chris Mason0b86a832008-03-24 15:01:56 -04003017 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003018 read_extent_buffer(leaf, dev_uuid,
3019 (unsigned long)btrfs_device_uuid(dev_item),
3020 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003021 read_extent_buffer(leaf, fs_uuid,
3022 (unsigned long)btrfs_device_fsid(dev_item),
3023 BTRFS_UUID_SIZE);
3024
3025 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3026 ret = open_seed_devices(root, fs_uuid);
3027 if (ret)
3028 return ret;
3029 seed_devices = 1;
3030 }
3031
3032 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3033 if (!device || !device->bdev) {
3034 if (!btrfs_test_opt(root, DEGRADED) || seed_devices)
3035 return -EIO;
3036
3037 if (!device) {
3038 printk("warning devid %Lu missing\n", devid);
3039 device = add_missing_dev(root, devid, dev_uuid);
3040 if (!device)
3041 return -ENOMEM;
3042 }
3043 }
3044
3045 if (device->fs_devices != root->fs_info->fs_devices) {
3046 BUG_ON(device->writeable);
3047 if (device->generation !=
3048 btrfs_device_generation(leaf, dev_item))
3049 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003050 }
Chris Mason0b86a832008-03-24 15:01:56 -04003051
3052 fill_device_from_item(leaf, dev_item, device);
3053 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003054 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003055 if (device->writeable)
3056 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003057 ret = 0;
3058#if 0
3059 ret = btrfs_open_device(device);
3060 if (ret) {
3061 kfree(device);
3062 }
3063#endif
3064 return ret;
3065}
3066
Chris Mason0d81ba52008-03-24 15:02:07 -04003067int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3068{
3069 struct btrfs_dev_item *dev_item;
3070
3071 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3072 dev_item);
3073 return read_one_dev(root, buf, dev_item);
3074}
3075
Yan Zhenga512bbf2008-12-08 16:46:26 -05003076int btrfs_read_sys_array(struct btrfs_root *root, u64 sb_bytenr)
Chris Mason0b86a832008-03-24 15:01:56 -04003077{
3078 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003079 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003080 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003081 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003082 u8 *ptr;
3083 unsigned long sb_ptr;
3084 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003085 u32 num_stripes;
3086 u32 array_size;
3087 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003088 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003089 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003090
Yan Zhenga512bbf2008-12-08 16:46:26 -05003091 sb = btrfs_find_create_tree_block(root, sb_bytenr,
Chris Masona061fc82008-05-07 11:43:44 -04003092 BTRFS_SUPER_INFO_SIZE);
3093 if (!sb)
3094 return -ENOMEM;
3095 btrfs_set_buffer_uptodate(sb);
3096 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003097 array_size = btrfs_super_sys_array_size(super_copy);
3098
Chris Mason0b86a832008-03-24 15:01:56 -04003099 ptr = super_copy->sys_chunk_array;
3100 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3101 cur = 0;
3102
3103 while (cur < array_size) {
3104 disk_key = (struct btrfs_disk_key *)ptr;
3105 btrfs_disk_key_to_cpu(&key, disk_key);
3106
Chris Masona061fc82008-05-07 11:43:44 -04003107 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003108 sb_ptr += len;
3109 cur += len;
3110
Chris Mason0d81ba52008-03-24 15:02:07 -04003111 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003112 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003113 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003114 if (ret)
3115 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003116 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3117 len = btrfs_chunk_item_size(num_stripes);
3118 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003119 ret = -EIO;
3120 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003121 }
3122 ptr += len;
3123 sb_ptr += len;
3124 cur += len;
3125 }
Chris Masona061fc82008-05-07 11:43:44 -04003126 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003127 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003128}
3129
3130int btrfs_read_chunk_tree(struct btrfs_root *root)
3131{
3132 struct btrfs_path *path;
3133 struct extent_buffer *leaf;
3134 struct btrfs_key key;
3135 struct btrfs_key found_key;
3136 int ret;
3137 int slot;
3138
3139 root = root->fs_info->chunk_root;
3140
3141 path = btrfs_alloc_path();
3142 if (!path)
3143 return -ENOMEM;
3144
3145 /* first we search for all of the device items, and then we
3146 * read in all of the chunk items. This way we can create chunk
3147 * mappings that reference all of the devices that are afound
3148 */
3149 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3150 key.offset = 0;
3151 key.type = 0;
3152again:
3153 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3154 while(1) {
3155 leaf = path->nodes[0];
3156 slot = path->slots[0];
3157 if (slot >= btrfs_header_nritems(leaf)) {
3158 ret = btrfs_next_leaf(root, path);
3159 if (ret == 0)
3160 continue;
3161 if (ret < 0)
3162 goto error;
3163 break;
3164 }
3165 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3166 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3167 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3168 break;
3169 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3170 struct btrfs_dev_item *dev_item;
3171 dev_item = btrfs_item_ptr(leaf, slot,
3172 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003173 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003174 if (ret)
3175 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003176 }
3177 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3178 struct btrfs_chunk *chunk;
3179 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3180 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003181 if (ret)
3182 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003183 }
3184 path->slots[0]++;
3185 }
3186 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3187 key.objectid = 0;
3188 btrfs_release_path(root, path);
3189 goto again;
3190 }
Chris Mason0b86a832008-03-24 15:01:56 -04003191 ret = 0;
3192error:
Yan Zheng2b820322008-11-17 21:11:30 -05003193 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003194 return ret;
3195}