blob: 7aa3810d7f69523c81b5b6b45b08681f58dac848 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040020#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040021#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040022#include <linux/random.h>
Chris Mason593060d2008-03-25 16:50:33 -040023#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050024#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040025#include "ctree.h"
26#include "extent_map.h"
27#include "disk-io.h"
28#include "transaction.h"
29#include "print-tree.h"
30#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040031#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032
Chris Mason593060d2008-03-25 16:50:33 -040033struct map_lookup {
34 u64 type;
35 int io_align;
36 int io_width;
37 int stripe_len;
38 int sector_size;
39 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040040 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040041 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040042};
43
Yan Zheng2b820322008-11-17 21:11:30 -050044static int init_first_rw_device(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_device *device);
47static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
48
Chris Mason593060d2008-03-25 16:50:33 -040049#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040050 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040051
Chris Mason8a4b83c2008-03-24 15:02:07 -040052static DEFINE_MUTEX(uuid_mutex);
53static LIST_HEAD(fs_uuids);
54
Chris Masona061fc82008-05-07 11:43:44 -040055void btrfs_lock_volumes(void)
56{
57 mutex_lock(&uuid_mutex);
58}
59
60void btrfs_unlock_volumes(void)
61{
62 mutex_unlock(&uuid_mutex);
63}
64
Chris Mason7d9eb122008-07-08 14:19:17 -040065static void lock_chunks(struct btrfs_root *root)
66{
Chris Mason7d9eb122008-07-08 14:19:17 -040067 mutex_lock(&root->fs_info->chunk_mutex);
68}
69
70static void unlock_chunks(struct btrfs_root *root)
71{
Chris Mason7d9eb122008-07-08 14:19:17 -040072 mutex_unlock(&root->fs_info->chunk_mutex);
73}
74
Yan Zhenge4404d62008-12-12 10:03:26 -050075static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
76{
77 struct btrfs_device *device;
78 WARN_ON(fs_devices->opened);
79 while (!list_empty(&fs_devices->devices)) {
80 device = list_entry(fs_devices->devices.next,
81 struct btrfs_device, dev_list);
82 list_del(&device->dev_list);
83 kfree(device->name);
84 kfree(device);
85 }
86 kfree(fs_devices);
87}
88
Chris Mason8a4b83c2008-03-24 15:02:07 -040089int btrfs_cleanup_fs_uuids(void)
90{
91 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040092
Yan Zheng2b820322008-11-17 21:11:30 -050093 while (!list_empty(&fs_uuids)) {
94 fs_devices = list_entry(fs_uuids.next,
95 struct btrfs_fs_devices, list);
96 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050097 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040098 }
99 return 0;
100}
101
Chris Masona1b32a52008-09-05 16:09:51 -0400102static noinline struct btrfs_device *__find_device(struct list_head *head,
103 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400104{
105 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400106
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500107 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400108 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400109 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400110 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400111 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400112 }
113 return NULL;
114}
115
Chris Masona1b32a52008-09-05 16:09:51 -0400116static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400117{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400118 struct btrfs_fs_devices *fs_devices;
119
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500120 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400121 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
122 return fs_devices;
123 }
124 return NULL;
125}
126
Chris Mason8b712842008-06-11 16:50:36 -0400127/*
128 * we try to collect pending bios for a device so we don't get a large
129 * number of procs sending bios down to the same device. This greatly
130 * improves the schedulers ability to collect and merge the bios.
131 *
132 * But, it also turns into a long list of bios to process and that is sure
133 * to eventually make the worker thread block. The solution here is to
134 * make some progress and then put this work struct back at the end of
135 * the list if the block device is congested. This way, multiple devices
136 * can make progress from a single worker thread.
137 */
Chris Masond3977122009-01-05 21:25:51 -0500138static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400139{
140 struct bio *pending;
141 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400142 struct btrfs_fs_info *fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400143 struct bio *tail;
144 struct bio *cur;
145 int again = 0;
146 unsigned long num_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400147 unsigned long limit;
Chris Mason8b712842008-06-11 16:50:36 -0400148
149 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
Chris Masonb64a2852008-08-20 13:39:41 -0400150 fs_info = device->dev_root->fs_info;
151 limit = btrfs_async_submit_limit(fs_info);
152 limit = limit * 2 / 3;
153
Chris Mason8b712842008-06-11 16:50:36 -0400154loop:
155 spin_lock(&device->io_lock);
156
Chris Masona6837052009-02-04 09:19:41 -0500157loop_lock:
Chris Mason8b712842008-06-11 16:50:36 -0400158 /* take all the bios off the list at once and process them
159 * later on (without the lock held). But, remember the
160 * tail and other pointers so the bios can be properly reinserted
161 * into the list if we hit congestion
162 */
163 pending = device->pending_bios;
164 tail = device->pending_bio_tail;
165 WARN_ON(pending && !tail);
166 device->pending_bios = NULL;
167 device->pending_bio_tail = NULL;
168
169 /*
170 * if pending was null this time around, no bios need processing
171 * at all and we can stop. Otherwise it'll loop back up again
172 * and do an additional check so no bios are missed.
173 *
174 * device->running_pending is used to synchronize with the
175 * schedule_bio code.
176 */
177 if (pending) {
178 again = 1;
179 device->running_pending = 1;
180 } else {
181 again = 0;
182 device->running_pending = 0;
183 }
184 spin_unlock(&device->io_lock);
185
Chris Masond3977122009-01-05 21:25:51 -0500186 while (pending) {
Chris Mason8b712842008-06-11 16:50:36 -0400187 cur = pending;
188 pending = pending->bi_next;
189 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400190 atomic_dec(&fs_info->nr_async_bios);
191
192 if (atomic_read(&fs_info->nr_async_bios) < limit &&
193 waitqueue_active(&fs_info->async_submit_wait))
194 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400195
196 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
197 bio_get(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400198 submit_bio(cur->bi_rw, cur);
Chris Mason492bb6de2008-07-31 16:29:02 -0400199 bio_put(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400200 num_run++;
201
202 /*
203 * we made progress, there is more work to do and the bdi
204 * is now congested. Back off and let other work structs
205 * run instead
206 */
Chris Masona6837052009-02-04 09:19:41 -0500207 if (pending && bdi_write_congested(bdi) && num_run > 16 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500208 fs_info->fs_devices->open_devices > 1) {
Chris Mason8b712842008-06-11 16:50:36 -0400209 struct bio *old_head;
210
211 spin_lock(&device->io_lock);
Chris Mason492bb6de2008-07-31 16:29:02 -0400212
Chris Mason8b712842008-06-11 16:50:36 -0400213 old_head = device->pending_bios;
214 device->pending_bios = pending;
215 if (device->pending_bio_tail)
216 tail->bi_next = old_head;
217 else
218 device->pending_bio_tail = tail;
Chris Masona6837052009-02-04 09:19:41 -0500219
220 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400221
222 spin_unlock(&device->io_lock);
223 btrfs_requeue_work(&device->work);
224 goto done;
225 }
226 }
227 if (again)
228 goto loop;
Chris Masona6837052009-02-04 09:19:41 -0500229
230 spin_lock(&device->io_lock);
231 if (device->pending_bios)
232 goto loop_lock;
233 spin_unlock(&device->io_lock);
Chris Mason8b712842008-06-11 16:50:36 -0400234done:
235 return 0;
236}
237
Christoph Hellwigb2950862008-12-02 09:54:17 -0500238static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400239{
240 struct btrfs_device *device;
241
242 device = container_of(work, struct btrfs_device, work);
243 run_scheduled_bios(device);
244}
245
Chris Masona1b32a52008-09-05 16:09:51 -0400246static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400247 struct btrfs_super_block *disk_super,
248 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
249{
250 struct btrfs_device *device;
251 struct btrfs_fs_devices *fs_devices;
252 u64 found_transid = btrfs_super_generation(disk_super);
253
254 fs_devices = find_fsid(disk_super->fsid);
255 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400256 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400257 if (!fs_devices)
258 return -ENOMEM;
259 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400260 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400261 list_add(&fs_devices->list, &fs_uuids);
262 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
263 fs_devices->latest_devid = devid;
264 fs_devices->latest_trans = found_transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400265 device = NULL;
266 } else {
Chris Masona4437552008-04-18 10:29:38 -0400267 device = __find_device(&fs_devices->devices, devid,
268 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400269 }
270 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500271 if (fs_devices->opened)
272 return -EBUSY;
273
Chris Mason8a4b83c2008-03-24 15:02:07 -0400274 device = kzalloc(sizeof(*device), GFP_NOFS);
275 if (!device) {
276 /* we can safely leave the fs_devices entry around */
277 return -ENOMEM;
278 }
279 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400280 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400281 memcpy(device->uuid, disk_super->dev_item.uuid,
282 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400283 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400284 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400285 device->name = kstrdup(path, GFP_NOFS);
286 if (!device->name) {
287 kfree(device);
288 return -ENOMEM;
289 }
Yan Zheng2b820322008-11-17 21:11:30 -0500290 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400291 list_add(&device->dev_list, &fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500292 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400293 fs_devices->num_devices++;
294 }
295
296 if (found_transid > fs_devices->latest_trans) {
297 fs_devices->latest_devid = devid;
298 fs_devices->latest_trans = found_transid;
299 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400300 *fs_devices_ret = fs_devices;
301 return 0;
302}
303
Yan Zhenge4404d62008-12-12 10:03:26 -0500304static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
305{
306 struct btrfs_fs_devices *fs_devices;
307 struct btrfs_device *device;
308 struct btrfs_device *orig_dev;
309
310 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
311 if (!fs_devices)
312 return ERR_PTR(-ENOMEM);
313
314 INIT_LIST_HEAD(&fs_devices->devices);
315 INIT_LIST_HEAD(&fs_devices->alloc_list);
316 INIT_LIST_HEAD(&fs_devices->list);
317 fs_devices->latest_devid = orig->latest_devid;
318 fs_devices->latest_trans = orig->latest_trans;
319 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
320
321 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
322 device = kzalloc(sizeof(*device), GFP_NOFS);
323 if (!device)
324 goto error;
325
326 device->name = kstrdup(orig_dev->name, GFP_NOFS);
327 if (!device->name)
328 goto error;
329
330 device->devid = orig_dev->devid;
331 device->work.func = pending_bios_fn;
332 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
333 device->barriers = 1;
334 spin_lock_init(&device->io_lock);
335 INIT_LIST_HEAD(&device->dev_list);
336 INIT_LIST_HEAD(&device->dev_alloc_list);
337
338 list_add(&device->dev_list, &fs_devices->devices);
339 device->fs_devices = fs_devices;
340 fs_devices->num_devices++;
341 }
342 return fs_devices;
343error:
344 free_fs_devices(fs_devices);
345 return ERR_PTR(-ENOMEM);
346}
347
Chris Masondfe25022008-05-13 13:46:40 -0400348int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
349{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500350 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400351
352 mutex_lock(&uuid_mutex);
353again:
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500354 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500355 if (device->in_fs_metadata)
356 continue;
357
358 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500359 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500360 device->bdev = NULL;
361 fs_devices->open_devices--;
362 }
363 if (device->writeable) {
364 list_del_init(&device->dev_alloc_list);
365 device->writeable = 0;
366 fs_devices->rw_devices--;
367 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500368 list_del_init(&device->dev_list);
369 fs_devices->num_devices--;
370 kfree(device->name);
371 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400372 }
Yan Zheng2b820322008-11-17 21:11:30 -0500373
374 if (fs_devices->seed) {
375 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500376 goto again;
377 }
378
Chris Masondfe25022008-05-13 13:46:40 -0400379 mutex_unlock(&uuid_mutex);
380 return 0;
381}
Chris Masona0af4692008-05-13 16:03:06 -0400382
Yan Zheng2b820322008-11-17 21:11:30 -0500383static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400384{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400385 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500386
Yan Zheng2b820322008-11-17 21:11:30 -0500387 if (--fs_devices->opened > 0)
388 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400389
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500390 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400391 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500392 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400393 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394 }
Yan Zheng2b820322008-11-17 21:11:30 -0500395 if (device->writeable) {
396 list_del_init(&device->dev_alloc_list);
397 fs_devices->rw_devices--;
398 }
399
Chris Mason8a4b83c2008-03-24 15:02:07 -0400400 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500401 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400402 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400403 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500404 WARN_ON(fs_devices->open_devices);
405 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500406 fs_devices->opened = 0;
407 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500408
Chris Mason8a4b83c2008-03-24 15:02:07 -0400409 return 0;
410}
411
Yan Zheng2b820322008-11-17 21:11:30 -0500412int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
413{
Yan Zhenge4404d62008-12-12 10:03:26 -0500414 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500415 int ret;
416
417 mutex_lock(&uuid_mutex);
418 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500419 if (!fs_devices->opened) {
420 seed_devices = fs_devices->seed;
421 fs_devices->seed = NULL;
422 }
Yan Zheng2b820322008-11-17 21:11:30 -0500423 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500424
425 while (seed_devices) {
426 fs_devices = seed_devices;
427 seed_devices = fs_devices->seed;
428 __btrfs_close_devices(fs_devices);
429 free_fs_devices(fs_devices);
430 }
Yan Zheng2b820322008-11-17 21:11:30 -0500431 return ret;
432}
433
Yan Zhenge4404d62008-12-12 10:03:26 -0500434static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
435 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400436{
437 struct block_device *bdev;
438 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400439 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400440 struct block_device *latest_bdev = NULL;
441 struct buffer_head *bh;
442 struct btrfs_super_block *disk_super;
443 u64 latest_devid = 0;
444 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400445 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500446 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400447 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400448
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500449 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400450 if (device->bdev)
451 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400452 if (!device->name)
453 continue;
454
Chris Mason15916de2008-11-19 21:17:22 -0500455 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400456 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500457 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400458 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400459 }
Chris Masona061fc82008-05-07 11:43:44 -0400460 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400461
Yan Zhenga512bbf2008-12-08 16:46:26 -0500462 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400463 if (!bh)
464 goto error_close;
465
466 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masona0af4692008-05-13 16:03:06 -0400467 devid = le64_to_cpu(disk_super->dev_item.devid);
468 if (devid != device->devid)
469 goto error_brelse;
470
Yan Zheng2b820322008-11-17 21:11:30 -0500471 if (memcmp(device->uuid, disk_super->dev_item.uuid,
472 BTRFS_UUID_SIZE))
473 goto error_brelse;
474
475 device->generation = btrfs_super_generation(disk_super);
476 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400477 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500478 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400479 latest_bdev = bdev;
480 }
481
Yan Zheng2b820322008-11-17 21:11:30 -0500482 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
483 device->writeable = 0;
484 } else {
485 device->writeable = !bdev_read_only(bdev);
486 seeding = 0;
487 }
488
Chris Mason8a4b83c2008-03-24 15:02:07 -0400489 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400490 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500491 device->mode = flags;
492
Chris Masona0af4692008-05-13 16:03:06 -0400493 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500494 if (device->writeable) {
495 fs_devices->rw_devices++;
496 list_add(&device->dev_alloc_list,
497 &fs_devices->alloc_list);
498 }
Chris Masona0af4692008-05-13 16:03:06 -0400499 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400500
Chris Masona0af4692008-05-13 16:03:06 -0400501error_brelse:
502 brelse(bh);
503error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500504 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400505error:
506 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400507 }
Chris Masona0af4692008-05-13 16:03:06 -0400508 if (fs_devices->open_devices == 0) {
509 ret = -EIO;
510 goto out;
511 }
Yan Zheng2b820322008-11-17 21:11:30 -0500512 fs_devices->seeding = seeding;
513 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400514 fs_devices->latest_bdev = latest_bdev;
515 fs_devices->latest_devid = latest_devid;
516 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500517 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400518out:
Yan Zheng2b820322008-11-17 21:11:30 -0500519 return ret;
520}
521
522int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500523 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500524{
525 int ret;
526
527 mutex_lock(&uuid_mutex);
528 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500529 fs_devices->opened++;
530 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500531 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500532 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500533 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 return ret;
536}
537
Christoph Hellwig97288f22008-12-02 06:36:09 -0500538int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400539 struct btrfs_fs_devices **fs_devices_ret)
540{
541 struct btrfs_super_block *disk_super;
542 struct block_device *bdev;
543 struct buffer_head *bh;
544 int ret;
545 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400546 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400547
548 mutex_lock(&uuid_mutex);
549
Chris Mason15916de2008-11-19 21:17:22 -0500550 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400551
552 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400553 ret = PTR_ERR(bdev);
554 goto error;
555 }
556
557 ret = set_blocksize(bdev, 4096);
558 if (ret)
559 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500560 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400561 if (!bh) {
562 ret = -EIO;
563 goto error_close;
564 }
565 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400566 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400567 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400568 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500569 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400570 else {
571 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500572 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400573 *(unsigned long long *)disk_super->fsid,
574 *(unsigned long long *)(disk_super->fsid + 8));
575 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500576 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500577 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
579
Chris Mason8a4b83c2008-03-24 15:02:07 -0400580 brelse(bh);
581error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500582 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583error:
584 mutex_unlock(&uuid_mutex);
585 return ret;
586}
Chris Mason0b86a832008-03-24 15:01:56 -0400587
588/*
589 * this uses a pretty simple search, the expectation is that it is
590 * called very infrequently and that a given device has a small number
591 * of extents
592 */
Chris Masona1b32a52008-09-05 16:09:51 -0400593static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
594 struct btrfs_device *device,
Chris Masona1b32a52008-09-05 16:09:51 -0400595 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400596{
597 struct btrfs_key key;
598 struct btrfs_root *root = device->dev_root;
599 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500600 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400601 u64 hole_size = 0;
602 u64 last_byte = 0;
603 u64 search_start = 0;
604 u64 search_end = device->total_bytes;
605 int ret;
606 int slot = 0;
607 int start_found;
608 struct extent_buffer *l;
609
Yan Zheng2b820322008-11-17 21:11:30 -0500610 path = btrfs_alloc_path();
611 if (!path)
612 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400613 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500614 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400615
616 /* FIXME use last free of some kind */
617
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618 /* we don't want to overwrite the superblock on the drive,
619 * so we make sure to start at an offset of at least 1MB
620 */
621 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400622
623 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
624 search_start = max(root->fs_info->alloc_start, search_start);
625
Chris Mason0b86a832008-03-24 15:01:56 -0400626 key.objectid = device->devid;
627 key.offset = search_start;
628 key.type = BTRFS_DEV_EXTENT_KEY;
629 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
630 if (ret < 0)
631 goto error;
632 ret = btrfs_previous_item(root, path, 0, key.type);
633 if (ret < 0)
634 goto error;
635 l = path->nodes[0];
636 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
637 while (1) {
638 l = path->nodes[0];
639 slot = path->slots[0];
640 if (slot >= btrfs_header_nritems(l)) {
641 ret = btrfs_next_leaf(root, path);
642 if (ret == 0)
643 continue;
644 if (ret < 0)
645 goto error;
646no_more_items:
647 if (!start_found) {
648 if (search_start >= search_end) {
649 ret = -ENOSPC;
650 goto error;
651 }
652 *start = search_start;
653 start_found = 1;
654 goto check_pending;
655 }
656 *start = last_byte > search_start ?
657 last_byte : search_start;
658 if (search_end <= *start) {
659 ret = -ENOSPC;
660 goto error;
661 }
662 goto check_pending;
663 }
664 btrfs_item_key_to_cpu(l, &key, slot);
665
666 if (key.objectid < device->devid)
667 goto next;
668
669 if (key.objectid > device->devid)
670 goto no_more_items;
671
672 if (key.offset >= search_start && key.offset > last_byte &&
673 start_found) {
674 if (last_byte < search_start)
675 last_byte = search_start;
676 hole_size = key.offset - last_byte;
677 if (key.offset > last_byte &&
678 hole_size >= num_bytes) {
679 *start = last_byte;
680 goto check_pending;
681 }
682 }
Chris Masond3977122009-01-05 21:25:51 -0500683 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400684 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400685
686 start_found = 1;
687 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
688 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
689next:
690 path->slots[0]++;
691 cond_resched();
692 }
693check_pending:
694 /* we have to make sure we didn't find an extent that has already
695 * been allocated by the map tree or the original allocation
696 */
Chris Mason0b86a832008-03-24 15:01:56 -0400697 BUG_ON(*start < search_start);
698
Chris Mason6324fbf2008-03-24 15:01:59 -0400699 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400700 ret = -ENOSPC;
701 goto error;
702 }
703 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500704 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400705
706error:
Yan Zheng2b820322008-11-17 21:11:30 -0500707 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400708 return ret;
709}
710
Christoph Hellwigb2950862008-12-02 09:54:17 -0500711static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400712 struct btrfs_device *device,
713 u64 start)
714{
715 int ret;
716 struct btrfs_path *path;
717 struct btrfs_root *root = device->dev_root;
718 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400719 struct btrfs_key found_key;
720 struct extent_buffer *leaf = NULL;
721 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400722
723 path = btrfs_alloc_path();
724 if (!path)
725 return -ENOMEM;
726
727 key.objectid = device->devid;
728 key.offset = start;
729 key.type = BTRFS_DEV_EXTENT_KEY;
730
731 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400732 if (ret > 0) {
733 ret = btrfs_previous_item(root, path, key.objectid,
734 BTRFS_DEV_EXTENT_KEY);
735 BUG_ON(ret);
736 leaf = path->nodes[0];
737 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
738 extent = btrfs_item_ptr(leaf, path->slots[0],
739 struct btrfs_dev_extent);
740 BUG_ON(found_key.offset > start || found_key.offset +
741 btrfs_dev_extent_length(leaf, extent) < start);
742 ret = 0;
743 } else if (ret == 0) {
744 leaf = path->nodes[0];
745 extent = btrfs_item_ptr(leaf, path->slots[0],
746 struct btrfs_dev_extent);
747 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400748 BUG_ON(ret);
749
Chris Masondfe25022008-05-13 13:46:40 -0400750 if (device->bytes_used > 0)
751 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400752 ret = btrfs_del_item(trans, root, path);
753 BUG_ON(ret);
754
755 btrfs_free_path(path);
756 return ret;
757}
758
Yan Zheng2b820322008-11-17 21:11:30 -0500759int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400760 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400761 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500762 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400763{
764 int ret;
765 struct btrfs_path *path;
766 struct btrfs_root *root = device->dev_root;
767 struct btrfs_dev_extent *extent;
768 struct extent_buffer *leaf;
769 struct btrfs_key key;
770
Chris Masondfe25022008-05-13 13:46:40 -0400771 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400772 path = btrfs_alloc_path();
773 if (!path)
774 return -ENOMEM;
775
Chris Mason0b86a832008-03-24 15:01:56 -0400776 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500777 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400778 key.type = BTRFS_DEV_EXTENT_KEY;
779 ret = btrfs_insert_empty_item(trans, root, path, &key,
780 sizeof(*extent));
781 BUG_ON(ret);
782
783 leaf = path->nodes[0];
784 extent = btrfs_item_ptr(leaf, path->slots[0],
785 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400786 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
787 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
788 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
789
790 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
791 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
792 BTRFS_UUID_SIZE);
793
Chris Mason0b86a832008-03-24 15:01:56 -0400794 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
795 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400796 btrfs_free_path(path);
797 return ret;
798}
799
Chris Masona1b32a52008-09-05 16:09:51 -0400800static noinline int find_next_chunk(struct btrfs_root *root,
801 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400802{
803 struct btrfs_path *path;
804 int ret;
805 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400806 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400807 struct btrfs_key found_key;
808
809 path = btrfs_alloc_path();
810 BUG_ON(!path);
811
Chris Masone17cade2008-04-15 15:41:47 -0400812 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400813 key.offset = (u64)-1;
814 key.type = BTRFS_CHUNK_ITEM_KEY;
815
816 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
817 if (ret < 0)
818 goto error;
819
820 BUG_ON(ret == 0);
821
822 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
823 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400824 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400825 } else {
826 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
827 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400828 if (found_key.objectid != objectid)
829 *offset = 0;
830 else {
831 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
832 struct btrfs_chunk);
833 *offset = found_key.offset +
834 btrfs_chunk_length(path->nodes[0], chunk);
835 }
Chris Mason0b86a832008-03-24 15:01:56 -0400836 }
837 ret = 0;
838error:
839 btrfs_free_path(path);
840 return ret;
841}
842
Yan Zheng2b820322008-11-17 21:11:30 -0500843static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400844{
845 int ret;
846 struct btrfs_key key;
847 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500848 struct btrfs_path *path;
849
850 root = root->fs_info->chunk_root;
851
852 path = btrfs_alloc_path();
853 if (!path)
854 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400855
856 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
857 key.type = BTRFS_DEV_ITEM_KEY;
858 key.offset = (u64)-1;
859
860 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
861 if (ret < 0)
862 goto error;
863
864 BUG_ON(ret == 0);
865
866 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
867 BTRFS_DEV_ITEM_KEY);
868 if (ret) {
869 *objectid = 1;
870 } else {
871 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
872 path->slots[0]);
873 *objectid = found_key.offset + 1;
874 }
875 ret = 0;
876error:
Yan Zheng2b820322008-11-17 21:11:30 -0500877 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400878 return ret;
879}
880
881/*
882 * the device information is stored in the chunk root
883 * the btrfs_device struct should be fully filled in
884 */
885int btrfs_add_device(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root,
887 struct btrfs_device *device)
888{
889 int ret;
890 struct btrfs_path *path;
891 struct btrfs_dev_item *dev_item;
892 struct extent_buffer *leaf;
893 struct btrfs_key key;
894 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -0400895
896 root = root->fs_info->chunk_root;
897
898 path = btrfs_alloc_path();
899 if (!path)
900 return -ENOMEM;
901
Chris Mason0b86a832008-03-24 15:01:56 -0400902 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
903 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -0500904 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400905
906 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400907 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400908 if (ret)
909 goto out;
910
911 leaf = path->nodes[0];
912 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
913
914 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -0500915 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400916 btrfs_set_device_type(leaf, dev_item, device->type);
917 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
918 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
919 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400920 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
921 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400922 btrfs_set_device_group(leaf, dev_item, 0);
923 btrfs_set_device_seek_speed(leaf, dev_item, 0);
924 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -0500925 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400926
Chris Mason0b86a832008-03-24 15:01:56 -0400927 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400928 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500929 ptr = (unsigned long)btrfs_device_fsid(dev_item);
930 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400931 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400932
Yan Zheng2b820322008-11-17 21:11:30 -0500933 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400934out:
935 btrfs_free_path(path);
936 return ret;
937}
Chris Mason8f18cf12008-04-25 16:53:30 -0400938
Chris Masona061fc82008-05-07 11:43:44 -0400939static int btrfs_rm_dev_item(struct btrfs_root *root,
940 struct btrfs_device *device)
941{
942 int ret;
943 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -0400944 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400945 struct btrfs_trans_handle *trans;
946
947 root = root->fs_info->chunk_root;
948
949 path = btrfs_alloc_path();
950 if (!path)
951 return -ENOMEM;
952
953 trans = btrfs_start_transaction(root, 1);
954 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
955 key.type = BTRFS_DEV_ITEM_KEY;
956 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400957 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400958
959 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
960 if (ret < 0)
961 goto out;
962
963 if (ret > 0) {
964 ret = -ENOENT;
965 goto out;
966 }
967
968 ret = btrfs_del_item(trans, root, path);
969 if (ret)
970 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400971out:
972 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -0400973 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400974 btrfs_commit_transaction(trans, root);
975 return ret;
976}
977
978int btrfs_rm_device(struct btrfs_root *root, char *device_path)
979{
980 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -0500981 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -0400982 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400983 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -0400984 struct btrfs_super_block *disk_super;
985 u64 all_avail;
986 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500987 u64 num_devices;
988 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -0400989 int ret = 0;
990
Chris Masona061fc82008-05-07 11:43:44 -0400991 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -0400992 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -0400993
994 all_avail = root->fs_info->avail_data_alloc_bits |
995 root->fs_info->avail_system_alloc_bits |
996 root->fs_info->avail_metadata_alloc_bits;
997
998 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -0500999 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001000 printk(KERN_ERR "btrfs: unable to go below four devices "
1001 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001002 ret = -EINVAL;
1003 goto out;
1004 }
1005
1006 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001007 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001008 printk(KERN_ERR "btrfs: unable to go below two "
1009 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001010 ret = -EINVAL;
1011 goto out;
1012 }
1013
Chris Masondfe25022008-05-13 13:46:40 -04001014 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001015 struct list_head *devices;
1016 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001017
Chris Masondfe25022008-05-13 13:46:40 -04001018 device = NULL;
1019 devices = &root->fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001020 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001021 if (tmp->in_fs_metadata && !tmp->bdev) {
1022 device = tmp;
1023 break;
1024 }
1025 }
1026 bdev = NULL;
1027 bh = NULL;
1028 disk_super = NULL;
1029 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001030 printk(KERN_ERR "btrfs: no missing devices found to "
1031 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001032 goto out;
1033 }
Chris Masondfe25022008-05-13 13:46:40 -04001034 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001035 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001036 root->fs_info->bdev_holder);
1037 if (IS_ERR(bdev)) {
1038 ret = PTR_ERR(bdev);
1039 goto out;
1040 }
1041
Yan Zheng2b820322008-11-17 21:11:30 -05001042 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001043 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001044 if (!bh) {
1045 ret = -EIO;
1046 goto error_close;
1047 }
1048 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masondfe25022008-05-13 13:46:40 -04001049 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001050 dev_uuid = disk_super->dev_item.uuid;
1051 device = btrfs_find_device(root, devid, dev_uuid,
1052 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001053 if (!device) {
1054 ret = -ENOENT;
1055 goto error_brelse;
1056 }
Chris Masondfe25022008-05-13 13:46:40 -04001057 }
Yan Zheng2b820322008-11-17 21:11:30 -05001058
1059 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001060 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1061 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001062 ret = -EINVAL;
1063 goto error_brelse;
1064 }
1065
1066 if (device->writeable) {
1067 list_del_init(&device->dev_alloc_list);
1068 root->fs_info->fs_devices->rw_devices--;
1069 }
Chris Masona061fc82008-05-07 11:43:44 -04001070
1071 ret = btrfs_shrink_device(device, 0);
1072 if (ret)
1073 goto error_brelse;
1074
Chris Masona061fc82008-05-07 11:43:44 -04001075 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1076 if (ret)
1077 goto error_brelse;
1078
Yan Zheng2b820322008-11-17 21:11:30 -05001079 device->in_fs_metadata = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001080 list_del_init(&device->dev_list);
1081 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001082
1083 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1084 struct btrfs_device, dev_list);
1085 if (device->bdev == root->fs_info->sb->s_bdev)
1086 root->fs_info->sb->s_bdev = next_device->bdev;
1087 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1088 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1089
Yan Zhenge4404d62008-12-12 10:03:26 -05001090 if (device->bdev) {
1091 close_bdev_exclusive(device->bdev, device->mode);
1092 device->bdev = NULL;
1093 device->fs_devices->open_devices--;
1094 }
1095
Yan Zheng2b820322008-11-17 21:11:30 -05001096 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1097 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1098
Yan Zhenge4404d62008-12-12 10:03:26 -05001099 if (device->fs_devices->open_devices == 0) {
1100 struct btrfs_fs_devices *fs_devices;
1101 fs_devices = root->fs_info->fs_devices;
1102 while (fs_devices) {
1103 if (fs_devices->seed == device->fs_devices)
1104 break;
1105 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001106 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001107 fs_devices->seed = device->fs_devices->seed;
1108 device->fs_devices->seed = NULL;
1109 __btrfs_close_devices(device->fs_devices);
1110 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001111 }
1112
1113 /*
1114 * at this point, the device is zero sized. We want to
1115 * remove it from the devices list and zero out the old super
1116 */
1117 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001118 /* make sure this device isn't detected as part of
1119 * the FS anymore
1120 */
1121 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1122 set_buffer_dirty(bh);
1123 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001124 }
Chris Masona061fc82008-05-07 11:43:44 -04001125
Chris Masona061fc82008-05-07 11:43:44 -04001126 kfree(device->name);
1127 kfree(device);
1128 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001129
1130error_brelse:
1131 brelse(bh);
1132error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001133 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001134 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001135out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001136 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001137 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001138 return ret;
1139}
1140
Yan Zheng2b820322008-11-17 21:11:30 -05001141/*
1142 * does all the dirty work required for changing file system's UUID.
1143 */
1144static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1145 struct btrfs_root *root)
1146{
1147 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1148 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001149 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001150 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1151 struct btrfs_device *device;
1152 u64 super_flags;
1153
1154 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001155 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001156 return -EINVAL;
1157
Yan Zhenge4404d62008-12-12 10:03:26 -05001158 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1159 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001160 return -ENOMEM;
1161
Yan Zhenge4404d62008-12-12 10:03:26 -05001162 old_devices = clone_fs_devices(fs_devices);
1163 if (IS_ERR(old_devices)) {
1164 kfree(seed_devices);
1165 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001166 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001167
Yan Zheng2b820322008-11-17 21:11:30 -05001168 list_add(&old_devices->list, &fs_uuids);
1169
Yan Zhenge4404d62008-12-12 10:03:26 -05001170 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1171 seed_devices->opened = 1;
1172 INIT_LIST_HEAD(&seed_devices->devices);
1173 INIT_LIST_HEAD(&seed_devices->alloc_list);
1174 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1175 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1176 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1177 device->fs_devices = seed_devices;
1178 }
1179
Yan Zheng2b820322008-11-17 21:11:30 -05001180 fs_devices->seeding = 0;
1181 fs_devices->num_devices = 0;
1182 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001183 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001184
1185 generate_random_uuid(fs_devices->fsid);
1186 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1187 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1188 super_flags = btrfs_super_flags(disk_super) &
1189 ~BTRFS_SUPER_FLAG_SEEDING;
1190 btrfs_set_super_flags(disk_super, super_flags);
1191
1192 return 0;
1193}
1194
1195/*
1196 * strore the expected generation for seed devices in device items.
1197 */
1198static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1199 struct btrfs_root *root)
1200{
1201 struct btrfs_path *path;
1202 struct extent_buffer *leaf;
1203 struct btrfs_dev_item *dev_item;
1204 struct btrfs_device *device;
1205 struct btrfs_key key;
1206 u8 fs_uuid[BTRFS_UUID_SIZE];
1207 u8 dev_uuid[BTRFS_UUID_SIZE];
1208 u64 devid;
1209 int ret;
1210
1211 path = btrfs_alloc_path();
1212 if (!path)
1213 return -ENOMEM;
1214
1215 root = root->fs_info->chunk_root;
1216 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1217 key.offset = 0;
1218 key.type = BTRFS_DEV_ITEM_KEY;
1219
1220 while (1) {
1221 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1222 if (ret < 0)
1223 goto error;
1224
1225 leaf = path->nodes[0];
1226next_slot:
1227 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1228 ret = btrfs_next_leaf(root, path);
1229 if (ret > 0)
1230 break;
1231 if (ret < 0)
1232 goto error;
1233 leaf = path->nodes[0];
1234 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1235 btrfs_release_path(root, path);
1236 continue;
1237 }
1238
1239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1240 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1241 key.type != BTRFS_DEV_ITEM_KEY)
1242 break;
1243
1244 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1245 struct btrfs_dev_item);
1246 devid = btrfs_device_id(leaf, dev_item);
1247 read_extent_buffer(leaf, dev_uuid,
1248 (unsigned long)btrfs_device_uuid(dev_item),
1249 BTRFS_UUID_SIZE);
1250 read_extent_buffer(leaf, fs_uuid,
1251 (unsigned long)btrfs_device_fsid(dev_item),
1252 BTRFS_UUID_SIZE);
1253 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1254 BUG_ON(!device);
1255
1256 if (device->fs_devices->seeding) {
1257 btrfs_set_device_generation(leaf, dev_item,
1258 device->generation);
1259 btrfs_mark_buffer_dirty(leaf);
1260 }
1261
1262 path->slots[0]++;
1263 goto next_slot;
1264 }
1265 ret = 0;
1266error:
1267 btrfs_free_path(path);
1268 return ret;
1269}
1270
Chris Mason788f20e2008-04-28 15:29:42 -04001271int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1272{
1273 struct btrfs_trans_handle *trans;
1274 struct btrfs_device *device;
1275 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001276 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001277 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001278 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001279 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001280 int ret = 0;
1281
Yan Zheng2b820322008-11-17 21:11:30 -05001282 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1283 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001284
Chris Mason15916de2008-11-19 21:17:22 -05001285 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Chris Masond3977122009-01-05 21:25:51 -05001286 if (!bdev)
Chris Mason788f20e2008-04-28 15:29:42 -04001287 return -EIO;
Chris Masona2135012008-06-25 16:01:30 -04001288
Yan Zheng2b820322008-11-17 21:11:30 -05001289 if (root->fs_info->fs_devices->seeding) {
1290 seeding_dev = 1;
1291 down_write(&sb->s_umount);
1292 mutex_lock(&uuid_mutex);
1293 }
1294
Chris Mason8c8bee12008-09-29 11:19:10 -04001295 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001296 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001297
Chris Mason788f20e2008-04-28 15:29:42 -04001298 devices = &root->fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001299 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001300 if (device->bdev == bdev) {
1301 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001302 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001303 }
1304 }
1305
1306 device = kzalloc(sizeof(*device), GFP_NOFS);
1307 if (!device) {
1308 /* we can safely leave the fs_devices entry around */
1309 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001310 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001311 }
1312
Chris Mason788f20e2008-04-28 15:29:42 -04001313 device->name = kstrdup(device_path, GFP_NOFS);
1314 if (!device->name) {
1315 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001316 ret = -ENOMEM;
1317 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001318 }
Yan Zheng2b820322008-11-17 21:11:30 -05001319
1320 ret = find_next_devid(root, &device->devid);
1321 if (ret) {
1322 kfree(device);
1323 goto error;
1324 }
1325
1326 trans = btrfs_start_transaction(root, 1);
1327 lock_chunks(root);
1328
1329 device->barriers = 1;
1330 device->writeable = 1;
1331 device->work.func = pending_bios_fn;
1332 generate_random_uuid(device->uuid);
1333 spin_lock_init(&device->io_lock);
1334 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001335 device->io_width = root->sectorsize;
1336 device->io_align = root->sectorsize;
1337 device->sector_size = root->sectorsize;
1338 device->total_bytes = i_size_read(bdev->bd_inode);
1339 device->dev_root = root->fs_info->dev_root;
1340 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001341 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001342 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001343 set_blocksize(device->bdev, 4096);
1344
Yan Zheng2b820322008-11-17 21:11:30 -05001345 if (seeding_dev) {
1346 sb->s_flags &= ~MS_RDONLY;
1347 ret = btrfs_prepare_sprout(trans, root);
1348 BUG_ON(ret);
1349 }
1350
1351 device->fs_devices = root->fs_info->fs_devices;
1352 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1353 list_add(&device->dev_alloc_list,
1354 &root->fs_info->fs_devices->alloc_list);
1355 root->fs_info->fs_devices->num_devices++;
1356 root->fs_info->fs_devices->open_devices++;
1357 root->fs_info->fs_devices->rw_devices++;
1358 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1359
Chris Mason788f20e2008-04-28 15:29:42 -04001360 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1361 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1362 total_bytes + device->total_bytes);
1363
1364 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1365 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1366 total_bytes + 1);
1367
Yan Zheng2b820322008-11-17 21:11:30 -05001368 if (seeding_dev) {
1369 ret = init_first_rw_device(trans, root, device);
1370 BUG_ON(ret);
1371 ret = btrfs_finish_sprout(trans, root);
1372 BUG_ON(ret);
1373 } else {
1374 ret = btrfs_add_device(trans, root, device);
1375 }
1376
Chris Mason7d9eb122008-07-08 14:19:17 -04001377 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001378 btrfs_commit_transaction(trans, root);
1379
1380 if (seeding_dev) {
1381 mutex_unlock(&uuid_mutex);
1382 up_write(&sb->s_umount);
1383
1384 ret = btrfs_relocate_sys_chunks(root);
1385 BUG_ON(ret);
1386 }
1387out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001388 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001389 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001390error:
Chris Mason15916de2008-11-19 21:17:22 -05001391 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001392 if (seeding_dev) {
1393 mutex_unlock(&uuid_mutex);
1394 up_write(&sb->s_umount);
1395 }
Chris Mason788f20e2008-04-28 15:29:42 -04001396 goto out;
1397}
1398
Chris Masond3977122009-01-05 21:25:51 -05001399static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1400 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001401{
1402 int ret;
1403 struct btrfs_path *path;
1404 struct btrfs_root *root;
1405 struct btrfs_dev_item *dev_item;
1406 struct extent_buffer *leaf;
1407 struct btrfs_key key;
1408
1409 root = device->dev_root->fs_info->chunk_root;
1410
1411 path = btrfs_alloc_path();
1412 if (!path)
1413 return -ENOMEM;
1414
1415 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1416 key.type = BTRFS_DEV_ITEM_KEY;
1417 key.offset = device->devid;
1418
1419 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1420 if (ret < 0)
1421 goto out;
1422
1423 if (ret > 0) {
1424 ret = -ENOENT;
1425 goto out;
1426 }
1427
1428 leaf = path->nodes[0];
1429 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1430
1431 btrfs_set_device_id(leaf, dev_item, device->devid);
1432 btrfs_set_device_type(leaf, dev_item, device->type);
1433 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1434 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1435 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001436 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1437 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1438 btrfs_mark_buffer_dirty(leaf);
1439
1440out:
1441 btrfs_free_path(path);
1442 return ret;
1443}
1444
Chris Mason7d9eb122008-07-08 14:19:17 -04001445static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001446 struct btrfs_device *device, u64 new_size)
1447{
1448 struct btrfs_super_block *super_copy =
1449 &device->dev_root->fs_info->super_copy;
1450 u64 old_total = btrfs_super_total_bytes(super_copy);
1451 u64 diff = new_size - device->total_bytes;
1452
Yan Zheng2b820322008-11-17 21:11:30 -05001453 if (!device->writeable)
1454 return -EACCES;
1455 if (new_size <= device->total_bytes)
1456 return -EINVAL;
1457
Chris Mason8f18cf12008-04-25 16:53:30 -04001458 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001459 device->fs_devices->total_rw_bytes += diff;
1460
1461 device->total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001462 btrfs_clear_space_info_full(device->dev_root->fs_info);
1463
Chris Mason8f18cf12008-04-25 16:53:30 -04001464 return btrfs_update_device(trans, device);
1465}
1466
Chris Mason7d9eb122008-07-08 14:19:17 -04001467int btrfs_grow_device(struct btrfs_trans_handle *trans,
1468 struct btrfs_device *device, u64 new_size)
1469{
1470 int ret;
1471 lock_chunks(device->dev_root);
1472 ret = __btrfs_grow_device(trans, device, new_size);
1473 unlock_chunks(device->dev_root);
1474 return ret;
1475}
1476
Chris Mason8f18cf12008-04-25 16:53:30 -04001477static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1478 struct btrfs_root *root,
1479 u64 chunk_tree, u64 chunk_objectid,
1480 u64 chunk_offset)
1481{
1482 int ret;
1483 struct btrfs_path *path;
1484 struct btrfs_key key;
1485
1486 root = root->fs_info->chunk_root;
1487 path = btrfs_alloc_path();
1488 if (!path)
1489 return -ENOMEM;
1490
1491 key.objectid = chunk_objectid;
1492 key.offset = chunk_offset;
1493 key.type = BTRFS_CHUNK_ITEM_KEY;
1494
1495 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1496 BUG_ON(ret);
1497
1498 ret = btrfs_del_item(trans, root, path);
1499 BUG_ON(ret);
1500
1501 btrfs_free_path(path);
1502 return 0;
1503}
1504
Christoph Hellwigb2950862008-12-02 09:54:17 -05001505static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001506 chunk_offset)
1507{
1508 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1509 struct btrfs_disk_key *disk_key;
1510 struct btrfs_chunk *chunk;
1511 u8 *ptr;
1512 int ret = 0;
1513 u32 num_stripes;
1514 u32 array_size;
1515 u32 len = 0;
1516 u32 cur;
1517 struct btrfs_key key;
1518
1519 array_size = btrfs_super_sys_array_size(super_copy);
1520
1521 ptr = super_copy->sys_chunk_array;
1522 cur = 0;
1523
1524 while (cur < array_size) {
1525 disk_key = (struct btrfs_disk_key *)ptr;
1526 btrfs_disk_key_to_cpu(&key, disk_key);
1527
1528 len = sizeof(*disk_key);
1529
1530 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1531 chunk = (struct btrfs_chunk *)(ptr + len);
1532 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1533 len += btrfs_chunk_item_size(num_stripes);
1534 } else {
1535 ret = -EIO;
1536 break;
1537 }
1538 if (key.objectid == chunk_objectid &&
1539 key.offset == chunk_offset) {
1540 memmove(ptr, ptr + len, array_size - (cur + len));
1541 array_size -= len;
1542 btrfs_set_super_sys_array_size(super_copy, array_size);
1543 } else {
1544 ptr += len;
1545 cur += len;
1546 }
1547 }
1548 return ret;
1549}
1550
Christoph Hellwigb2950862008-12-02 09:54:17 -05001551static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001552 u64 chunk_tree, u64 chunk_objectid,
1553 u64 chunk_offset)
1554{
1555 struct extent_map_tree *em_tree;
1556 struct btrfs_root *extent_root;
1557 struct btrfs_trans_handle *trans;
1558 struct extent_map *em;
1559 struct map_lookup *map;
1560 int ret;
1561 int i;
1562
Chris Masond3977122009-01-05 21:25:51 -05001563 printk(KERN_INFO "btrfs relocating chunk %llu\n",
Chris Mason323da792008-05-09 11:46:48 -04001564 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001565 root = root->fs_info->chunk_root;
1566 extent_root = root->fs_info->extent_root;
1567 em_tree = &root->fs_info->mapping_tree.map_tree;
1568
1569 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001570 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001571 BUG_ON(ret);
1572
1573 trans = btrfs_start_transaction(root, 1);
1574 BUG_ON(!trans);
1575
Chris Mason7d9eb122008-07-08 14:19:17 -04001576 lock_chunks(root);
1577
Chris Mason8f18cf12008-04-25 16:53:30 -04001578 /*
1579 * step two, delete the device extents and the
1580 * chunk tree entries
1581 */
1582 spin_lock(&em_tree->lock);
1583 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1584 spin_unlock(&em_tree->lock);
1585
Chris Masona061fc82008-05-07 11:43:44 -04001586 BUG_ON(em->start > chunk_offset ||
1587 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001588 map = (struct map_lookup *)em->bdev;
1589
1590 for (i = 0; i < map->num_stripes; i++) {
1591 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1592 map->stripes[i].physical);
1593 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001594
Chris Masondfe25022008-05-13 13:46:40 -04001595 if (map->stripes[i].dev) {
1596 ret = btrfs_update_device(trans, map->stripes[i].dev);
1597 BUG_ON(ret);
1598 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001599 }
1600 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1601 chunk_offset);
1602
1603 BUG_ON(ret);
1604
1605 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1606 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1607 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001608 }
1609
Zheng Yan1a40e232008-09-26 10:09:34 -04001610 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1611 BUG_ON(ret);
1612
Chris Mason8f18cf12008-04-25 16:53:30 -04001613 spin_lock(&em_tree->lock);
1614 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001615 spin_unlock(&em_tree->lock);
1616
Chris Mason8f18cf12008-04-25 16:53:30 -04001617 kfree(map);
1618 em->bdev = NULL;
1619
1620 /* once for the tree */
1621 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001622 /* once for us */
1623 free_extent_map(em);
1624
Chris Mason7d9eb122008-07-08 14:19:17 -04001625 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001626 btrfs_end_transaction(trans, root);
1627 return 0;
1628}
1629
Yan Zheng2b820322008-11-17 21:11:30 -05001630static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1631{
1632 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1633 struct btrfs_path *path;
1634 struct extent_buffer *leaf;
1635 struct btrfs_chunk *chunk;
1636 struct btrfs_key key;
1637 struct btrfs_key found_key;
1638 u64 chunk_tree = chunk_root->root_key.objectid;
1639 u64 chunk_type;
1640 int ret;
1641
1642 path = btrfs_alloc_path();
1643 if (!path)
1644 return -ENOMEM;
1645
1646 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1647 key.offset = (u64)-1;
1648 key.type = BTRFS_CHUNK_ITEM_KEY;
1649
1650 while (1) {
1651 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1652 if (ret < 0)
1653 goto error;
1654 BUG_ON(ret == 0);
1655
1656 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1657 key.type);
1658 if (ret < 0)
1659 goto error;
1660 if (ret > 0)
1661 break;
1662
1663 leaf = path->nodes[0];
1664 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1665
1666 chunk = btrfs_item_ptr(leaf, path->slots[0],
1667 struct btrfs_chunk);
1668 chunk_type = btrfs_chunk_type(leaf, chunk);
1669 btrfs_release_path(chunk_root, path);
1670
1671 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1672 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1673 found_key.objectid,
1674 found_key.offset);
1675 BUG_ON(ret);
1676 }
1677
1678 if (found_key.offset == 0)
1679 break;
1680 key.offset = found_key.offset - 1;
1681 }
1682 ret = 0;
1683error:
1684 btrfs_free_path(path);
1685 return ret;
1686}
1687
Chris Masonec44a352008-04-28 15:29:52 -04001688static u64 div_factor(u64 num, int factor)
1689{
1690 if (factor == 10)
1691 return num;
1692 num *= factor;
1693 do_div(num, 10);
1694 return num;
1695}
1696
Chris Masonec44a352008-04-28 15:29:52 -04001697int btrfs_balance(struct btrfs_root *dev_root)
1698{
1699 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001700 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1701 struct btrfs_device *device;
1702 u64 old_size;
1703 u64 size_to_free;
1704 struct btrfs_path *path;
1705 struct btrfs_key key;
1706 struct btrfs_chunk *chunk;
1707 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1708 struct btrfs_trans_handle *trans;
1709 struct btrfs_key found_key;
1710
Yan Zheng2b820322008-11-17 21:11:30 -05001711 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1712 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001713
Chris Mason7d9eb122008-07-08 14:19:17 -04001714 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001715 dev_root = dev_root->fs_info->dev_root;
1716
Chris Masonec44a352008-04-28 15:29:52 -04001717 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001718 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001719 old_size = device->total_bytes;
1720 size_to_free = div_factor(old_size, 1);
1721 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001722 if (!device->writeable ||
1723 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001724 continue;
1725
1726 ret = btrfs_shrink_device(device, old_size - size_to_free);
1727 BUG_ON(ret);
1728
1729 trans = btrfs_start_transaction(dev_root, 1);
1730 BUG_ON(!trans);
1731
1732 ret = btrfs_grow_device(trans, device, old_size);
1733 BUG_ON(ret);
1734
1735 btrfs_end_transaction(trans, dev_root);
1736 }
1737
1738 /* step two, relocate all the chunks */
1739 path = btrfs_alloc_path();
1740 BUG_ON(!path);
1741
1742 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1743 key.offset = (u64)-1;
1744 key.type = BTRFS_CHUNK_ITEM_KEY;
1745
Chris Masond3977122009-01-05 21:25:51 -05001746 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001747 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1748 if (ret < 0)
1749 goto error;
1750
1751 /*
1752 * this shouldn't happen, it means the last relocate
1753 * failed
1754 */
1755 if (ret == 0)
1756 break;
1757
1758 ret = btrfs_previous_item(chunk_root, path, 0,
1759 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001760 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001761 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001762
Chris Masonec44a352008-04-28 15:29:52 -04001763 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1764 path->slots[0]);
1765 if (found_key.objectid != key.objectid)
1766 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001767
Chris Masonec44a352008-04-28 15:29:52 -04001768 chunk = btrfs_item_ptr(path->nodes[0],
1769 path->slots[0],
1770 struct btrfs_chunk);
1771 key.offset = found_key.offset;
1772 /* chunk zero is special */
1773 if (key.offset == 0)
1774 break;
1775
Chris Mason7d9eb122008-07-08 14:19:17 -04001776 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001777 ret = btrfs_relocate_chunk(chunk_root,
1778 chunk_root->root_key.objectid,
1779 found_key.objectid,
1780 found_key.offset);
1781 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001782 }
1783 ret = 0;
1784error:
1785 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001786 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001787 return ret;
1788}
1789
Chris Mason8f18cf12008-04-25 16:53:30 -04001790/*
1791 * shrinking a device means finding all of the device extents past
1792 * the new size, and then following the back refs to the chunks.
1793 * The chunk relocation code actually frees the device extent
1794 */
1795int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1796{
1797 struct btrfs_trans_handle *trans;
1798 struct btrfs_root *root = device->dev_root;
1799 struct btrfs_dev_extent *dev_extent = NULL;
1800 struct btrfs_path *path;
1801 u64 length;
1802 u64 chunk_tree;
1803 u64 chunk_objectid;
1804 u64 chunk_offset;
1805 int ret;
1806 int slot;
1807 struct extent_buffer *l;
1808 struct btrfs_key key;
1809 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1810 u64 old_total = btrfs_super_total_bytes(super_copy);
1811 u64 diff = device->total_bytes - new_size;
1812
Yan Zheng2b820322008-11-17 21:11:30 -05001813 if (new_size >= device->total_bytes)
1814 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001815
1816 path = btrfs_alloc_path();
1817 if (!path)
1818 return -ENOMEM;
1819
1820 trans = btrfs_start_transaction(root, 1);
1821 if (!trans) {
1822 ret = -ENOMEM;
1823 goto done;
1824 }
1825
1826 path->reada = 2;
1827
Chris Mason7d9eb122008-07-08 14:19:17 -04001828 lock_chunks(root);
1829
Chris Mason8f18cf12008-04-25 16:53:30 -04001830 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001831 if (device->writeable)
1832 device->fs_devices->total_rw_bytes -= diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04001833 ret = btrfs_update_device(trans, device);
1834 if (ret) {
Chris Mason7d9eb122008-07-08 14:19:17 -04001835 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001836 btrfs_end_transaction(trans, root);
1837 goto done;
1838 }
1839 WARN_ON(diff > old_total);
1840 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001841 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001842 btrfs_end_transaction(trans, root);
1843
1844 key.objectid = device->devid;
1845 key.offset = (u64)-1;
1846 key.type = BTRFS_DEV_EXTENT_KEY;
1847
1848 while (1) {
1849 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1850 if (ret < 0)
1851 goto done;
1852
1853 ret = btrfs_previous_item(root, path, 0, key.type);
1854 if (ret < 0)
1855 goto done;
1856 if (ret) {
1857 ret = 0;
1858 goto done;
1859 }
1860
1861 l = path->nodes[0];
1862 slot = path->slots[0];
1863 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1864
1865 if (key.objectid != device->devid)
1866 goto done;
1867
1868 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1869 length = btrfs_dev_extent_length(l, dev_extent);
1870
1871 if (key.offset + length <= new_size)
1872 goto done;
1873
1874 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1875 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1876 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1877 btrfs_release_path(root, path);
1878
1879 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1880 chunk_offset);
1881 if (ret)
1882 goto done;
1883 }
1884
1885done:
1886 btrfs_free_path(path);
1887 return ret;
1888}
1889
Christoph Hellwigb2950862008-12-02 09:54:17 -05001890static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001891 struct btrfs_root *root,
1892 struct btrfs_key *key,
1893 struct btrfs_chunk *chunk, int item_size)
1894{
1895 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1896 struct btrfs_disk_key disk_key;
1897 u32 array_size;
1898 u8 *ptr;
1899
1900 array_size = btrfs_super_sys_array_size(super_copy);
1901 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1902 return -EFBIG;
1903
1904 ptr = super_copy->sys_chunk_array + array_size;
1905 btrfs_cpu_key_to_disk(&disk_key, key);
1906 memcpy(ptr, &disk_key, sizeof(disk_key));
1907 ptr += sizeof(disk_key);
1908 memcpy(ptr, chunk, item_size);
1909 item_size += sizeof(disk_key);
1910 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1911 return 0;
1912}
1913
Chris Masond3977122009-01-05 21:25:51 -05001914static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04001915 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04001916{
1917 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1918 return calc_size;
1919 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1920 return calc_size * (num_stripes / sub_stripes);
1921 else
1922 return calc_size * num_stripes;
1923}
1924
Yan Zheng2b820322008-11-17 21:11:30 -05001925static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *extent_root,
1927 struct map_lookup **map_ret,
1928 u64 *num_bytes, u64 *stripe_size,
1929 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001930{
Chris Mason593060d2008-03-25 16:50:33 -04001931 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001932 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001933 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04001934 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05001935 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001936 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04001937 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05001938 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04001939 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001940 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001941 u64 max_chunk_size = calc_size;
1942 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001943 u64 avail;
1944 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001945 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04001946 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001947 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001948 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001949 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001950 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001951 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001952 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001953
Chris Masonec44a352008-04-28 15:29:52 -04001954 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1955 (type & BTRFS_BLOCK_GROUP_DUP)) {
1956 WARN_ON(1);
1957 type &= ~BTRFS_BLOCK_GROUP_DUP;
1958 }
Yan Zheng2b820322008-11-17 21:11:30 -05001959 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04001960 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001961
Chris Masona40a90a2008-04-18 11:55:51 -04001962 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001963 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04001964 min_stripes = 2;
1965 }
1966 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04001967 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001968 min_stripes = 2;
1969 }
Chris Mason8790d502008-04-03 16:29:03 -04001970 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001971 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001972 if (num_stripes < 2)
1973 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04001974 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04001975 }
Chris Mason321aecc2008-04-16 10:49:51 -04001976 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001977 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04001978 if (num_stripes < 4)
1979 return -ENOSPC;
1980 num_stripes &= ~(u32)1;
1981 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001982 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04001983 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04001984
1985 if (type & BTRFS_BLOCK_GROUP_DATA) {
1986 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001987 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001988 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1989 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001990 min_stripe_size = 32 * 1024 * 1024;
1991 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1992 calc_size = 8 * 1024 * 1024;
1993 max_chunk_size = calc_size * 2;
1994 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001995 }
1996
Yan Zheng2b820322008-11-17 21:11:30 -05001997 /* we don't want a chunk larger than 10% of writeable space */
1998 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
1999 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002000
Chris Masona40a90a2008-04-18 11:55:51 -04002001again:
Yan Zheng2b820322008-11-17 21:11:30 -05002002 if (!map || map->num_stripes != num_stripes) {
2003 kfree(map);
2004 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2005 if (!map)
2006 return -ENOMEM;
2007 map->num_stripes = num_stripes;
2008 }
2009
Chris Mason9b3f68b2008-04-18 10:29:51 -04002010 if (calc_size * num_stripes > max_chunk_size) {
2011 calc_size = max_chunk_size;
2012 do_div(calc_size, num_stripes);
2013 do_div(calc_size, stripe_len);
2014 calc_size *= stripe_len;
2015 }
2016 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002017 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002018
Chris Mason9b3f68b2008-04-18 10:29:51 -04002019 do_div(calc_size, stripe_len);
2020 calc_size *= stripe_len;
2021
Yan Zheng2b820322008-11-17 21:11:30 -05002022 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002023 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002024
2025 if (type & BTRFS_BLOCK_GROUP_DUP)
2026 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002027 else
2028 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002029
Josef Bacik0f9dd462008-09-23 13:14:11 -04002030 /*
2031 * we add 1MB because we never use the first 1MB of the device, unless
2032 * we've looped, then we are likely allocating the maximum amount of
2033 * space left already
2034 */
2035 if (!looped)
2036 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002037
Yan Zheng2b820322008-11-17 21:11:30 -05002038 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002039 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002040 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002041 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002042 if (device->total_bytes > device->bytes_used)
2043 avail = device->total_bytes - device->bytes_used;
2044 else
2045 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002046 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002047
Chris Masondfe25022008-05-13 13:46:40 -04002048 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002049 ret = find_free_dev_extent(trans, device,
2050 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002051 if (ret == 0) {
2052 list_move_tail(&device->dev_alloc_list,
2053 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002054 map->stripes[index].dev = device;
2055 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002056 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002057 if (type & BTRFS_BLOCK_GROUP_DUP) {
2058 map->stripes[index].dev = device;
2059 map->stripes[index].physical =
2060 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002061 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002062 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002063 }
Chris Masondfe25022008-05-13 13:46:40 -04002064 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002065 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002066 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002067 break;
2068 }
Yan Zheng2b820322008-11-17 21:11:30 -05002069 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002070 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002071 if (index >= min_stripes) {
2072 num_stripes = index;
2073 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2074 num_stripes /= sub_stripes;
2075 num_stripes *= sub_stripes;
2076 }
2077 looped = 1;
2078 goto again;
2079 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002080 if (!looped && max_avail > 0) {
2081 looped = 1;
2082 calc_size = max_avail;
2083 goto again;
2084 }
Yan Zheng2b820322008-11-17 21:11:30 -05002085 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002086 return -ENOSPC;
2087 }
Chris Mason593060d2008-03-25 16:50:33 -04002088 map->sector_size = extent_root->sectorsize;
2089 map->stripe_len = stripe_len;
2090 map->io_align = stripe_len;
2091 map->io_width = stripe_len;
2092 map->type = type;
2093 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002094 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002095
Yan Zheng2b820322008-11-17 21:11:30 -05002096 *map_ret = map;
2097 *stripe_size = calc_size;
2098 *num_bytes = chunk_bytes_by_type(type, calc_size,
2099 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002100
2101 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002102 if (!em) {
2103 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002104 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002105 }
Chris Mason0b86a832008-03-24 15:01:56 -04002106 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002107 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002108 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002109 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002110 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002111
Chris Mason0b86a832008-03-24 15:01:56 -04002112 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2113 spin_lock(&em_tree->lock);
2114 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002115 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002116 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002117 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002118
2119 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2120 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2121 start, *num_bytes);
2122 BUG_ON(ret);
2123
2124 index = 0;
2125 while (index < map->num_stripes) {
2126 device = map->stripes[index].dev;
2127 dev_offset = map->stripes[index].physical;
2128
2129 ret = btrfs_alloc_dev_extent(trans, device,
2130 info->chunk_root->root_key.objectid,
2131 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2132 start, dev_offset, calc_size);
2133 BUG_ON(ret);
2134 index++;
2135 }
2136
2137 return 0;
2138}
2139
2140static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2141 struct btrfs_root *extent_root,
2142 struct map_lookup *map, u64 chunk_offset,
2143 u64 chunk_size, u64 stripe_size)
2144{
2145 u64 dev_offset;
2146 struct btrfs_key key;
2147 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2148 struct btrfs_device *device;
2149 struct btrfs_chunk *chunk;
2150 struct btrfs_stripe *stripe;
2151 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2152 int index = 0;
2153 int ret;
2154
2155 chunk = kzalloc(item_size, GFP_NOFS);
2156 if (!chunk)
2157 return -ENOMEM;
2158
2159 index = 0;
2160 while (index < map->num_stripes) {
2161 device = map->stripes[index].dev;
2162 device->bytes_used += stripe_size;
2163 ret = btrfs_update_device(trans, device);
2164 BUG_ON(ret);
2165 index++;
2166 }
2167
2168 index = 0;
2169 stripe = &chunk->stripe;
2170 while (index < map->num_stripes) {
2171 device = map->stripes[index].dev;
2172 dev_offset = map->stripes[index].physical;
2173
2174 btrfs_set_stack_stripe_devid(stripe, device->devid);
2175 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2176 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2177 stripe++;
2178 index++;
2179 }
2180
2181 btrfs_set_stack_chunk_length(chunk, chunk_size);
2182 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2183 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2184 btrfs_set_stack_chunk_type(chunk, map->type);
2185 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2186 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2187 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2188 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2189 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2190
2191 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2192 key.type = BTRFS_CHUNK_ITEM_KEY;
2193 key.offset = chunk_offset;
2194
2195 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2196 BUG_ON(ret);
2197
2198 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2199 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2200 item_size);
2201 BUG_ON(ret);
2202 }
2203 kfree(chunk);
2204 return 0;
2205}
2206
2207/*
2208 * Chunk allocation falls into two parts. The first part does works
2209 * that make the new allocated chunk useable, but not do any operation
2210 * that modifies the chunk tree. The second part does the works that
2211 * require modifying the chunk tree. This division is important for the
2212 * bootstrap process of adding storage to a seed btrfs.
2213 */
2214int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2215 struct btrfs_root *extent_root, u64 type)
2216{
2217 u64 chunk_offset;
2218 u64 chunk_size;
2219 u64 stripe_size;
2220 struct map_lookup *map;
2221 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2222 int ret;
2223
2224 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2225 &chunk_offset);
2226 if (ret)
2227 return ret;
2228
2229 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2230 &stripe_size, chunk_offset, type);
2231 if (ret)
2232 return ret;
2233
2234 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2235 chunk_size, stripe_size);
2236 BUG_ON(ret);
2237 return 0;
2238}
2239
Chris Masond3977122009-01-05 21:25:51 -05002240static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002241 struct btrfs_root *root,
2242 struct btrfs_device *device)
2243{
2244 u64 chunk_offset;
2245 u64 sys_chunk_offset;
2246 u64 chunk_size;
2247 u64 sys_chunk_size;
2248 u64 stripe_size;
2249 u64 sys_stripe_size;
2250 u64 alloc_profile;
2251 struct map_lookup *map;
2252 struct map_lookup *sys_map;
2253 struct btrfs_fs_info *fs_info = root->fs_info;
2254 struct btrfs_root *extent_root = fs_info->extent_root;
2255 int ret;
2256
2257 ret = find_next_chunk(fs_info->chunk_root,
2258 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2259 BUG_ON(ret);
2260
2261 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2262 (fs_info->metadata_alloc_profile &
2263 fs_info->avail_metadata_alloc_bits);
2264 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2265
2266 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2267 &stripe_size, chunk_offset, alloc_profile);
2268 BUG_ON(ret);
2269
2270 sys_chunk_offset = chunk_offset + chunk_size;
2271
2272 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2273 (fs_info->system_alloc_profile &
2274 fs_info->avail_system_alloc_bits);
2275 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2276
2277 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2278 &sys_chunk_size, &sys_stripe_size,
2279 sys_chunk_offset, alloc_profile);
2280 BUG_ON(ret);
2281
2282 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2283 BUG_ON(ret);
2284
2285 /*
2286 * Modifying chunk tree needs allocating new blocks from both
2287 * system block group and metadata block group. So we only can
2288 * do operations require modifying the chunk tree after both
2289 * block groups were created.
2290 */
2291 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2292 chunk_size, stripe_size);
2293 BUG_ON(ret);
2294
2295 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2296 sys_chunk_offset, sys_chunk_size,
2297 sys_stripe_size);
2298 BUG_ON(ret);
2299 return 0;
2300}
2301
2302int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2303{
2304 struct extent_map *em;
2305 struct map_lookup *map;
2306 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2307 int readonly = 0;
2308 int i;
2309
2310 spin_lock(&map_tree->map_tree.lock);
2311 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2312 spin_unlock(&map_tree->map_tree.lock);
2313 if (!em)
2314 return 1;
2315
2316 map = (struct map_lookup *)em->bdev;
2317 for (i = 0; i < map->num_stripes; i++) {
2318 if (!map->stripes[i].dev->writeable) {
2319 readonly = 1;
2320 break;
2321 }
2322 }
2323 free_extent_map(em);
2324 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002325}
2326
2327void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2328{
2329 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2330}
2331
2332void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2333{
2334 struct extent_map *em;
2335
Chris Masond3977122009-01-05 21:25:51 -05002336 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002337 spin_lock(&tree->map_tree.lock);
2338 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2339 if (em)
2340 remove_extent_mapping(&tree->map_tree, em);
2341 spin_unlock(&tree->map_tree.lock);
2342 if (!em)
2343 break;
2344 kfree(em->bdev);
2345 /* once for us */
2346 free_extent_map(em);
2347 /* once for the tree */
2348 free_extent_map(em);
2349 }
2350}
2351
Chris Masonf1885912008-04-09 16:28:12 -04002352int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2353{
2354 struct extent_map *em;
2355 struct map_lookup *map;
2356 struct extent_map_tree *em_tree = &map_tree->map_tree;
2357 int ret;
2358
2359 spin_lock(&em_tree->lock);
2360 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002361 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002362 BUG_ON(!em);
2363
2364 BUG_ON(em->start > logical || em->start + em->len < logical);
2365 map = (struct map_lookup *)em->bdev;
2366 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2367 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002368 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2369 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002370 else
2371 ret = 1;
2372 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002373 return ret;
2374}
2375
Chris Masondfe25022008-05-13 13:46:40 -04002376static int find_live_mirror(struct map_lookup *map, int first, int num,
2377 int optimal)
2378{
2379 int i;
2380 if (map->stripes[optimal].dev->bdev)
2381 return optimal;
2382 for (i = first; i < first + num; i++) {
2383 if (map->stripes[i].dev->bdev)
2384 return i;
2385 }
2386 /* we couldn't find one that doesn't fail. Just return something
2387 * and the io error handling code will clean up eventually
2388 */
2389 return optimal;
2390}
2391
Chris Masonf2d8d742008-04-21 10:03:05 -04002392static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2393 u64 logical, u64 *length,
2394 struct btrfs_multi_bio **multi_ret,
2395 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002396{
2397 struct extent_map *em;
2398 struct map_lookup *map;
2399 struct extent_map_tree *em_tree = &map_tree->map_tree;
2400 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002401 u64 stripe_offset;
2402 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002403 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002404 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002405 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002406 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002407 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002408 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002409 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002410
Chris Masond3977122009-01-05 21:25:51 -05002411 if (multi_ret && !(rw & (1 << BIO_RW)))
Chris Masoncea9e442008-04-09 16:28:12 -04002412 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002413again:
2414 if (multi_ret) {
2415 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2416 GFP_NOFS);
2417 if (!multi)
2418 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002419
2420 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002421 }
Chris Mason0b86a832008-03-24 15:01:56 -04002422
2423 spin_lock(&em_tree->lock);
2424 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002425 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002426
2427 if (!em && unplug_page)
2428 return 0;
2429
Chris Mason3b951512008-04-17 11:29:12 -04002430 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002431 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2432 (unsigned long long)logical,
2433 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002434 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002435 }
Chris Mason0b86a832008-03-24 15:01:56 -04002436
2437 BUG_ON(em->start > logical || em->start + em->len < logical);
2438 map = (struct map_lookup *)em->bdev;
2439 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002440
Chris Masonf1885912008-04-09 16:28:12 -04002441 if (mirror_num > map->num_stripes)
2442 mirror_num = 0;
2443
Chris Masoncea9e442008-04-09 16:28:12 -04002444 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002445 if (rw & (1 << BIO_RW)) {
2446 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2447 BTRFS_BLOCK_GROUP_DUP)) {
2448 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002449 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002450 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2451 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002452 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002453 }
2454 }
2455 if (multi_ret && rw == WRITE &&
2456 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002457 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002458 free_extent_map(em);
2459 kfree(multi);
2460 goto again;
2461 }
Chris Mason593060d2008-03-25 16:50:33 -04002462 stripe_nr = offset;
2463 /*
2464 * stripe_nr counts the total number of stripes we have to stride
2465 * to get to this block
2466 */
2467 do_div(stripe_nr, map->stripe_len);
2468
2469 stripe_offset = stripe_nr * map->stripe_len;
2470 BUG_ON(offset < stripe_offset);
2471
2472 /* stripe_offset is the offset of this block in its stripe*/
2473 stripe_offset = offset - stripe_offset;
2474
Chris Masoncea9e442008-04-09 16:28:12 -04002475 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002476 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002477 BTRFS_BLOCK_GROUP_DUP)) {
2478 /* we limit the length of each bio to what fits in a stripe */
2479 *length = min_t(u64, em->len - offset,
2480 map->stripe_len - stripe_offset);
2481 } else {
2482 *length = em->len - offset;
2483 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002484
2485 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002486 goto out;
2487
Chris Masonf2d8d742008-04-21 10:03:05 -04002488 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002489 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002490 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002491 if (unplug_page || (rw & (1 << BIO_RW)))
2492 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002493 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002494 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002495 else {
2496 stripe_index = find_live_mirror(map, 0,
2497 map->num_stripes,
2498 current->pid % map->num_stripes);
2499 }
Chris Mason2fff7342008-04-29 14:12:09 -04002500
Chris Mason611f0e02008-04-03 16:29:03 -04002501 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002502 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002503 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002504 else if (mirror_num)
2505 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002506
Chris Mason321aecc2008-04-16 10:49:51 -04002507 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2508 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002509
2510 stripe_index = do_div(stripe_nr, factor);
2511 stripe_index *= map->sub_stripes;
2512
Chris Masonf2d8d742008-04-21 10:03:05 -04002513 if (unplug_page || (rw & (1 << BIO_RW)))
2514 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002515 else if (mirror_num)
2516 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002517 else {
2518 stripe_index = find_live_mirror(map, stripe_index,
2519 map->sub_stripes, stripe_index +
2520 current->pid % map->sub_stripes);
2521 }
Chris Mason8790d502008-04-03 16:29:03 -04002522 } else {
2523 /*
2524 * after this do_div call, stripe_nr is the number of stripes
2525 * on this device we have to walk to find the data, and
2526 * stripe_index is the number of our device in the stripe array
2527 */
2528 stripe_index = do_div(stripe_nr, map->num_stripes);
2529 }
Chris Mason593060d2008-03-25 16:50:33 -04002530 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002531
Chris Masonf2d8d742008-04-21 10:03:05 -04002532 for (i = 0; i < num_stripes; i++) {
2533 if (unplug_page) {
2534 struct btrfs_device *device;
2535 struct backing_dev_info *bdi;
2536
2537 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002538 if (device->bdev) {
2539 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002540 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002541 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002542 }
2543 } else {
2544 multi->stripes[i].physical =
2545 map->stripes[stripe_index].physical +
2546 stripe_offset + stripe_nr * map->stripe_len;
2547 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2548 }
Chris Masoncea9e442008-04-09 16:28:12 -04002549 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002550 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002551 if (multi_ret) {
2552 *multi_ret = multi;
2553 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002554 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002555 }
Chris Masoncea9e442008-04-09 16:28:12 -04002556out:
Chris Mason0b86a832008-03-24 15:01:56 -04002557 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002558 return 0;
2559}
2560
Chris Masonf2d8d742008-04-21 10:03:05 -04002561int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2562 u64 logical, u64 *length,
2563 struct btrfs_multi_bio **multi_ret, int mirror_num)
2564{
2565 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2566 mirror_num, NULL);
2567}
2568
Yan Zhenga512bbf2008-12-08 16:46:26 -05002569int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2570 u64 chunk_start, u64 physical, u64 devid,
2571 u64 **logical, int *naddrs, int *stripe_len)
2572{
2573 struct extent_map_tree *em_tree = &map_tree->map_tree;
2574 struct extent_map *em;
2575 struct map_lookup *map;
2576 u64 *buf;
2577 u64 bytenr;
2578 u64 length;
2579 u64 stripe_nr;
2580 int i, j, nr = 0;
2581
2582 spin_lock(&em_tree->lock);
2583 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2584 spin_unlock(&em_tree->lock);
2585
2586 BUG_ON(!em || em->start != chunk_start);
2587 map = (struct map_lookup *)em->bdev;
2588
2589 length = em->len;
2590 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2591 do_div(length, map->num_stripes / map->sub_stripes);
2592 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2593 do_div(length, map->num_stripes);
2594
2595 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2596 BUG_ON(!buf);
2597
2598 for (i = 0; i < map->num_stripes; i++) {
2599 if (devid && map->stripes[i].dev->devid != devid)
2600 continue;
2601 if (map->stripes[i].physical > physical ||
2602 map->stripes[i].physical + length <= physical)
2603 continue;
2604
2605 stripe_nr = physical - map->stripes[i].physical;
2606 do_div(stripe_nr, map->stripe_len);
2607
2608 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2609 stripe_nr = stripe_nr * map->num_stripes + i;
2610 do_div(stripe_nr, map->sub_stripes);
2611 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2612 stripe_nr = stripe_nr * map->num_stripes + i;
2613 }
2614 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002615 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002616 for (j = 0; j < nr; j++) {
2617 if (buf[j] == bytenr)
2618 break;
2619 }
Chris Mason934d3752008-12-08 16:43:10 -05002620 if (j == nr) {
2621 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002622 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002623 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002624 }
2625
2626 for (i = 0; i > nr; i++) {
2627 struct btrfs_multi_bio *multi;
2628 struct btrfs_bio_stripe *stripe;
2629 int ret;
2630
2631 length = 1;
2632 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2633 &length, &multi, 0);
2634 BUG_ON(ret);
2635
2636 stripe = multi->stripes;
2637 for (j = 0; j < multi->num_stripes; j++) {
2638 if (stripe->physical >= physical &&
2639 physical < stripe->physical + length)
2640 break;
2641 }
2642 BUG_ON(j >= multi->num_stripes);
2643 kfree(multi);
2644 }
2645
2646 *logical = buf;
2647 *naddrs = nr;
2648 *stripe_len = map->stripe_len;
2649
2650 free_extent_map(em);
2651 return 0;
2652}
2653
Chris Masonf2d8d742008-04-21 10:03:05 -04002654int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2655 u64 logical, struct page *page)
2656{
2657 u64 length = PAGE_CACHE_SIZE;
2658 return __btrfs_map_block(map_tree, READ, logical, &length,
2659 NULL, 0, page);
2660}
2661
Chris Mason8790d502008-04-03 16:29:03 -04002662static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002663{
Chris Masoncea9e442008-04-09 16:28:12 -04002664 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002665 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002666
Chris Mason8790d502008-04-03 16:29:03 -04002667 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002668 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002669
Chris Mason7d2b4da2008-08-05 10:13:57 -04002670 if (bio == multi->orig_bio)
2671 is_orig_bio = 1;
2672
Chris Masoncea9e442008-04-09 16:28:12 -04002673 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002674 if (!is_orig_bio) {
2675 bio_put(bio);
2676 bio = multi->orig_bio;
2677 }
Chris Mason8790d502008-04-03 16:29:03 -04002678 bio->bi_private = multi->private;
2679 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002680 /* only send an error to the higher layers if it is
2681 * beyond the tolerance of the multi-bio
2682 */
Chris Mason1259ab72008-05-12 13:39:03 -04002683 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002684 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002685 } else if (err) {
2686 /*
2687 * this bio is actually up to date, we didn't
2688 * go over the max number of errors
2689 */
2690 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002691 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002692 }
Chris Mason8790d502008-04-03 16:29:03 -04002693 kfree(multi);
2694
2695 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002696 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002697 bio_put(bio);
2698 }
Chris Mason8790d502008-04-03 16:29:03 -04002699}
2700
Chris Mason8b712842008-06-11 16:50:36 -04002701struct async_sched {
2702 struct bio *bio;
2703 int rw;
2704 struct btrfs_fs_info *info;
2705 struct btrfs_work work;
2706};
2707
2708/*
2709 * see run_scheduled_bios for a description of why bios are collected for
2710 * async submit.
2711 *
2712 * This will add one bio to the pending list for a device and make sure
2713 * the work struct is scheduled.
2714 */
Chris Masond3977122009-01-05 21:25:51 -05002715static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002716 struct btrfs_device *device,
2717 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002718{
2719 int should_queue = 1;
2720
2721 /* don't bother with additional async steps for reads, right now */
2722 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002723 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002724 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002725 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002726 return 0;
2727 }
2728
2729 /*
Chris Mason0986fe92008-08-15 15:34:15 -04002730 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002731 * higher layers. Otherwise, the async bio makes it appear we have
2732 * made progress against dirty pages when we've really just put it
2733 * on a queue for later
2734 */
Chris Mason0986fe92008-08-15 15:34:15 -04002735 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002736 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002737 bio->bi_next = NULL;
2738 bio->bi_rw |= rw;
2739
2740 spin_lock(&device->io_lock);
2741
2742 if (device->pending_bio_tail)
2743 device->pending_bio_tail->bi_next = bio;
2744
2745 device->pending_bio_tail = bio;
2746 if (!device->pending_bios)
2747 device->pending_bios = bio;
2748 if (device->running_pending)
2749 should_queue = 0;
2750
2751 spin_unlock(&device->io_lock);
2752
2753 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002754 btrfs_queue_worker(&root->fs_info->submit_workers,
2755 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002756 return 0;
2757}
2758
Chris Masonf1885912008-04-09 16:28:12 -04002759int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002760 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002761{
2762 struct btrfs_mapping_tree *map_tree;
2763 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002764 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002765 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002766 u64 length = 0;
2767 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002768 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002769 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002770 int dev_nr = 0;
2771 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002772
Chris Masonf2d8d742008-04-21 10:03:05 -04002773 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002774 map_tree = &root->fs_info->mapping_tree;
2775 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002776
Chris Masonf1885912008-04-09 16:28:12 -04002777 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2778 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002779 BUG_ON(ret);
2780
2781 total_devs = multi->num_stripes;
2782 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05002783 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2784 "len %llu\n", (unsigned long long)logical,
2785 (unsigned long long)length,
2786 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04002787 BUG();
2788 }
2789 multi->end_io = first_bio->bi_end_io;
2790 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002791 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002792 atomic_set(&multi->stripes_pending, multi->num_stripes);
2793
Chris Masond3977122009-01-05 21:25:51 -05002794 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002795 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002796 if (dev_nr < total_devs - 1) {
2797 bio = bio_clone(first_bio, GFP_NOFS);
2798 BUG_ON(!bio);
2799 } else {
2800 bio = first_bio;
2801 }
2802 bio->bi_private = multi;
2803 bio->bi_end_io = end_bio_multi_stripe;
2804 }
Chris Masoncea9e442008-04-09 16:28:12 -04002805 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2806 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002807 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002808 if (dev && dev->bdev) {
2809 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002810 if (async_submit)
2811 schedule_bio(root, dev, rw, bio);
2812 else
2813 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002814 } else {
2815 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2816 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002817 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002818 }
Chris Mason8790d502008-04-03 16:29:03 -04002819 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002820 }
Chris Masoncea9e442008-04-09 16:28:12 -04002821 if (total_devs == 1)
2822 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002823 return 0;
2824}
2825
Chris Masona4437552008-04-18 10:29:38 -04002826struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05002827 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04002828{
Yan Zheng2b820322008-11-17 21:11:30 -05002829 struct btrfs_device *device;
2830 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002831
Yan Zheng2b820322008-11-17 21:11:30 -05002832 cur_devices = root->fs_info->fs_devices;
2833 while (cur_devices) {
2834 if (!fsid ||
2835 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2836 device = __find_device(&cur_devices->devices,
2837 devid, uuid);
2838 if (device)
2839 return device;
2840 }
2841 cur_devices = cur_devices->seed;
2842 }
2843 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002844}
2845
Chris Masondfe25022008-05-13 13:46:40 -04002846static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2847 u64 devid, u8 *dev_uuid)
2848{
2849 struct btrfs_device *device;
2850 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2851
2852 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05002853 if (!device)
2854 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04002855 list_add(&device->dev_list,
2856 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04002857 device->barriers = 1;
2858 device->dev_root = root->fs_info->dev_root;
2859 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002860 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05002861 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04002862 fs_devices->num_devices++;
2863 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05002864 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04002865 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2866 return device;
2867}
2868
Chris Mason0b86a832008-03-24 15:01:56 -04002869static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2870 struct extent_buffer *leaf,
2871 struct btrfs_chunk *chunk)
2872{
2873 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2874 struct map_lookup *map;
2875 struct extent_map *em;
2876 u64 logical;
2877 u64 length;
2878 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002879 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002880 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002881 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002882 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002883
Chris Masone17cade2008-04-15 15:41:47 -04002884 logical = key->offset;
2885 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002886
Chris Mason0b86a832008-03-24 15:01:56 -04002887 spin_lock(&map_tree->map_tree.lock);
2888 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002889 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002890
2891 /* already mapped? */
2892 if (em && em->start <= logical && em->start + em->len > logical) {
2893 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002894 return 0;
2895 } else if (em) {
2896 free_extent_map(em);
2897 }
Chris Mason0b86a832008-03-24 15:01:56 -04002898
Chris Mason0b86a832008-03-24 15:01:56 -04002899 em = alloc_extent_map(GFP_NOFS);
2900 if (!em)
2901 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002902 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2903 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002904 if (!map) {
2905 free_extent_map(em);
2906 return -ENOMEM;
2907 }
2908
2909 em->bdev = (struct block_device *)map;
2910 em->start = logical;
2911 em->len = length;
2912 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002913 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002914
Chris Mason593060d2008-03-25 16:50:33 -04002915 map->num_stripes = num_stripes;
2916 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2917 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2918 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2919 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2920 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002921 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002922 for (i = 0; i < num_stripes; i++) {
2923 map->stripes[i].physical =
2924 btrfs_stripe_offset_nr(leaf, chunk, i);
2925 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002926 read_extent_buffer(leaf, uuid, (unsigned long)
2927 btrfs_stripe_dev_uuid_nr(chunk, i),
2928 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002929 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2930 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04002931 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002932 kfree(map);
2933 free_extent_map(em);
2934 return -EIO;
2935 }
Chris Masondfe25022008-05-13 13:46:40 -04002936 if (!map->stripes[i].dev) {
2937 map->stripes[i].dev =
2938 add_missing_dev(root, devid, uuid);
2939 if (!map->stripes[i].dev) {
2940 kfree(map);
2941 free_extent_map(em);
2942 return -EIO;
2943 }
2944 }
2945 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002946 }
2947
2948 spin_lock(&map_tree->map_tree.lock);
2949 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002950 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002951 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002952 free_extent_map(em);
2953
2954 return 0;
2955}
2956
2957static int fill_device_from_item(struct extent_buffer *leaf,
2958 struct btrfs_dev_item *dev_item,
2959 struct btrfs_device *device)
2960{
2961 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04002962
2963 device->devid = btrfs_device_id(leaf, dev_item);
2964 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2965 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2966 device->type = btrfs_device_type(leaf, dev_item);
2967 device->io_align = btrfs_device_io_align(leaf, dev_item);
2968 device->io_width = btrfs_device_io_width(leaf, dev_item);
2969 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002970
2971 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04002972 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002973
Chris Mason0b86a832008-03-24 15:01:56 -04002974 return 0;
2975}
2976
Yan Zheng2b820322008-11-17 21:11:30 -05002977static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
2978{
2979 struct btrfs_fs_devices *fs_devices;
2980 int ret;
2981
2982 mutex_lock(&uuid_mutex);
2983
2984 fs_devices = root->fs_info->fs_devices->seed;
2985 while (fs_devices) {
2986 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2987 ret = 0;
2988 goto out;
2989 }
2990 fs_devices = fs_devices->seed;
2991 }
2992
2993 fs_devices = find_fsid(fsid);
2994 if (!fs_devices) {
2995 ret = -ENOENT;
2996 goto out;
2997 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002998
2999 fs_devices = clone_fs_devices(fs_devices);
3000 if (IS_ERR(fs_devices)) {
3001 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003002 goto out;
3003 }
3004
Christoph Hellwig97288f22008-12-02 06:36:09 -05003005 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003006 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003007 if (ret)
3008 goto out;
3009
3010 if (!fs_devices->seeding) {
3011 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003012 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003013 ret = -EINVAL;
3014 goto out;
3015 }
3016
3017 fs_devices->seed = root->fs_info->fs_devices->seed;
3018 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003019out:
3020 mutex_unlock(&uuid_mutex);
3021 return ret;
3022}
3023
Chris Mason0d81ba52008-03-24 15:02:07 -04003024static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003025 struct extent_buffer *leaf,
3026 struct btrfs_dev_item *dev_item)
3027{
3028 struct btrfs_device *device;
3029 u64 devid;
3030 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003031 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003032 u8 dev_uuid[BTRFS_UUID_SIZE];
3033
Chris Mason0b86a832008-03-24 15:01:56 -04003034 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003035 read_extent_buffer(leaf, dev_uuid,
3036 (unsigned long)btrfs_device_uuid(dev_item),
3037 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003038 read_extent_buffer(leaf, fs_uuid,
3039 (unsigned long)btrfs_device_fsid(dev_item),
3040 BTRFS_UUID_SIZE);
3041
3042 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3043 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003044 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003045 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003046 }
3047
3048 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3049 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003050 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003051 return -EIO;
3052
3053 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003054 printk(KERN_WARNING "warning devid %llu missing\n",
3055 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003056 device = add_missing_dev(root, devid, dev_uuid);
3057 if (!device)
3058 return -ENOMEM;
3059 }
3060 }
3061
3062 if (device->fs_devices != root->fs_info->fs_devices) {
3063 BUG_ON(device->writeable);
3064 if (device->generation !=
3065 btrfs_device_generation(leaf, dev_item))
3066 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003067 }
Chris Mason0b86a832008-03-24 15:01:56 -04003068
3069 fill_device_from_item(leaf, dev_item, device);
3070 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003071 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003072 if (device->writeable)
3073 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003074 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003075 return ret;
3076}
3077
Chris Mason0d81ba52008-03-24 15:02:07 -04003078int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3079{
3080 struct btrfs_dev_item *dev_item;
3081
3082 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3083 dev_item);
3084 return read_one_dev(root, buf, dev_item);
3085}
3086
Yan Zhenge4404d62008-12-12 10:03:26 -05003087int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003088{
3089 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003090 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003091 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003092 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003093 u8 *ptr;
3094 unsigned long sb_ptr;
3095 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003096 u32 num_stripes;
3097 u32 array_size;
3098 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003099 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003100 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003101
Yan Zhenge4404d62008-12-12 10:03:26 -05003102 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003103 BTRFS_SUPER_INFO_SIZE);
3104 if (!sb)
3105 return -ENOMEM;
3106 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003107 btrfs_set_buffer_lockdep_class(sb, 0);
3108
Chris Masona061fc82008-05-07 11:43:44 -04003109 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003110 array_size = btrfs_super_sys_array_size(super_copy);
3111
Chris Mason0b86a832008-03-24 15:01:56 -04003112 ptr = super_copy->sys_chunk_array;
3113 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3114 cur = 0;
3115
3116 while (cur < array_size) {
3117 disk_key = (struct btrfs_disk_key *)ptr;
3118 btrfs_disk_key_to_cpu(&key, disk_key);
3119
Chris Masona061fc82008-05-07 11:43:44 -04003120 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003121 sb_ptr += len;
3122 cur += len;
3123
Chris Mason0d81ba52008-03-24 15:02:07 -04003124 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003125 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003126 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003127 if (ret)
3128 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003129 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3130 len = btrfs_chunk_item_size(num_stripes);
3131 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003132 ret = -EIO;
3133 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003134 }
3135 ptr += len;
3136 sb_ptr += len;
3137 cur += len;
3138 }
Chris Masona061fc82008-05-07 11:43:44 -04003139 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003140 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003141}
3142
3143int btrfs_read_chunk_tree(struct btrfs_root *root)
3144{
3145 struct btrfs_path *path;
3146 struct extent_buffer *leaf;
3147 struct btrfs_key key;
3148 struct btrfs_key found_key;
3149 int ret;
3150 int slot;
3151
3152 root = root->fs_info->chunk_root;
3153
3154 path = btrfs_alloc_path();
3155 if (!path)
3156 return -ENOMEM;
3157
3158 /* first we search for all of the device items, and then we
3159 * read in all of the chunk items. This way we can create chunk
3160 * mappings that reference all of the devices that are afound
3161 */
3162 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3163 key.offset = 0;
3164 key.type = 0;
3165again:
3166 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masond3977122009-01-05 21:25:51 -05003167 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003168 leaf = path->nodes[0];
3169 slot = path->slots[0];
3170 if (slot >= btrfs_header_nritems(leaf)) {
3171 ret = btrfs_next_leaf(root, path);
3172 if (ret == 0)
3173 continue;
3174 if (ret < 0)
3175 goto error;
3176 break;
3177 }
3178 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3179 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3180 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3181 break;
3182 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3183 struct btrfs_dev_item *dev_item;
3184 dev_item = btrfs_item_ptr(leaf, slot,
3185 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003186 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003187 if (ret)
3188 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003189 }
3190 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3191 struct btrfs_chunk *chunk;
3192 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3193 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003194 if (ret)
3195 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003196 }
3197 path->slots[0]++;
3198 }
3199 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3200 key.objectid = 0;
3201 btrfs_release_path(root, path);
3202 goto again;
3203 }
Chris Mason0b86a832008-03-24 15:01:56 -04003204 ret = 0;
3205error:
Yan Zheng2b820322008-11-17 21:11:30 -05003206 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003207 return ret;
3208}