blob: 3ab80e9cd76767a674e3005185ee99f3afa53433 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040020#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040021#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040022#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040023#include <linux/iocontext.h>
Chris Mason593060d2008-03-25 16:50:33 -040024#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040026#include "ctree.h"
27#include "extent_map.h"
28#include "disk-io.h"
29#include "transaction.h"
30#include "print-tree.h"
31#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040032#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033
Chris Mason593060d2008-03-25 16:50:33 -040034struct map_lookup {
35 u64 type;
36 int io_align;
37 int io_width;
38 int stripe_len;
39 int sector_size;
40 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040041 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040042 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040043};
44
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49
Chris Mason593060d2008-03-25 16:50:33 -040050#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040051 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Masona061fc82008-05-07 11:43:44 -040056void btrfs_lock_volumes(void)
57{
58 mutex_lock(&uuid_mutex);
59}
60
61void btrfs_unlock_volumes(void)
62{
63 mutex_unlock(&uuid_mutex);
64}
65
Chris Mason7d9eb122008-07-08 14:19:17 -040066static void lock_chunks(struct btrfs_root *root)
67{
Chris Mason7d9eb122008-07-08 14:19:17 -040068 mutex_lock(&root->fs_info->chunk_mutex);
69}
70
71static void unlock_chunks(struct btrfs_root *root)
72{
Chris Mason7d9eb122008-07-08 14:19:17 -040073 mutex_unlock(&root->fs_info->chunk_mutex);
74}
75
Yan Zhenge4404d62008-12-12 10:03:26 -050076static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
77{
78 struct btrfs_device *device;
79 WARN_ON(fs_devices->opened);
80 while (!list_empty(&fs_devices->devices)) {
81 device = list_entry(fs_devices->devices.next,
82 struct btrfs_device, dev_list);
83 list_del(&device->dev_list);
84 kfree(device->name);
85 kfree(device);
86 }
87 kfree(fs_devices);
88}
89
Chris Mason8a4b83c2008-03-24 15:02:07 -040090int btrfs_cleanup_fs_uuids(void)
91{
92 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040093
Yan Zheng2b820322008-11-17 21:11:30 -050094 while (!list_empty(&fs_uuids)) {
95 fs_devices = list_entry(fs_uuids.next,
96 struct btrfs_fs_devices, list);
97 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050098 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040099 }
100 return 0;
101}
102
Chris Masona1b32a52008-09-05 16:09:51 -0400103static noinline struct btrfs_device *__find_device(struct list_head *head,
104 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105{
106 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500108 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400109 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400110 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400111 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400112 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 }
114 return NULL;
115}
116
Chris Masona1b32a52008-09-05 16:09:51 -0400117static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400118{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 struct btrfs_fs_devices *fs_devices;
120
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500121 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400122 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
123 return fs_devices;
124 }
125 return NULL;
126}
127
Chris Masonffbd5172009-04-20 15:50:09 -0400128static void requeue_list(struct btrfs_pending_bios *pending_bios,
129 struct bio *head, struct bio *tail)
130{
131
132 struct bio *old_head;
133
134 old_head = pending_bios->head;
135 pending_bios->head = head;
136 if (pending_bios->tail)
137 tail->bi_next = old_head;
138 else
139 pending_bios->tail = tail;
140}
141
Chris Mason8b712842008-06-11 16:50:36 -0400142/*
143 * we try to collect pending bios for a device so we don't get a large
144 * number of procs sending bios down to the same device. This greatly
145 * improves the schedulers ability to collect and merge the bios.
146 *
147 * But, it also turns into a long list of bios to process and that is sure
148 * to eventually make the worker thread block. The solution here is to
149 * make some progress and then put this work struct back at the end of
150 * the list if the block device is congested. This way, multiple devices
151 * can make progress from a single worker thread.
152 */
Chris Masond3977122009-01-05 21:25:51 -0500153static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400154{
155 struct bio *pending;
156 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400157 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400158 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400159 struct bio *tail;
160 struct bio *cur;
161 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400162 unsigned long num_run;
163 unsigned long num_sync_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400164 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400165 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400166 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400167 int force_reg = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400168
Chris Masonbedf7622009-04-03 10:32:58 -0400169 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400170 fs_info = device->dev_root->fs_info;
171 limit = btrfs_async_submit_limit(fs_info);
172 limit = limit * 2 / 3;
173
Chris Masonffbd5172009-04-20 15:50:09 -0400174 /* we want to make sure that every time we switch from the sync
175 * list to the normal list, we unplug
176 */
177 num_sync_run = 0;
178
Chris Mason8b712842008-06-11 16:50:36 -0400179loop:
180 spin_lock(&device->io_lock);
181
Chris Masona6837052009-02-04 09:19:41 -0500182loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400183 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400184
Chris Mason8b712842008-06-11 16:50:36 -0400185 /* take all the bios off the list at once and process them
186 * later on (without the lock held). But, remember the
187 * tail and other pointers so the bios can be properly reinserted
188 * into the list if we hit congestion
189 */
Chris Masond84275c2009-06-09 15:39:08 -0400190 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400191 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400192 force_reg = 1;
193 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400194 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400195 force_reg = 0;
196 }
Chris Masonffbd5172009-04-20 15:50:09 -0400197
198 pending = pending_bios->head;
199 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400200 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400201
202 /*
203 * if pending was null this time around, no bios need processing
204 * at all and we can stop. Otherwise it'll loop back up again
205 * and do an additional check so no bios are missed.
206 *
207 * device->running_pending is used to synchronize with the
208 * schedule_bio code.
209 */
Chris Masonffbd5172009-04-20 15:50:09 -0400210 if (device->pending_sync_bios.head == NULL &&
211 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400212 again = 0;
213 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400214 } else {
215 again = 1;
216 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400217 }
Chris Masonffbd5172009-04-20 15:50:09 -0400218
219 pending_bios->head = NULL;
220 pending_bios->tail = NULL;
221
Chris Mason8b712842008-06-11 16:50:36 -0400222 spin_unlock(&device->io_lock);
223
Chris Masonffbd5172009-04-20 15:50:09 -0400224 /*
225 * if we're doing the regular priority list, make sure we unplug
226 * for any high prio bios we've sent down
227 */
228 if (pending_bios == &device->pending_bios && num_sync_run > 0) {
229 num_sync_run = 0;
230 blk_run_backing_dev(bdi, NULL);
231 }
232
Chris Masond3977122009-01-05 21:25:51 -0500233 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400234
235 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400236 /* we want to work on both lists, but do more bios on the
237 * sync list than the regular list
238 */
239 if ((num_run > 32 &&
240 pending_bios != &device->pending_sync_bios &&
241 device->pending_sync_bios.head) ||
242 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
243 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400244 spin_lock(&device->io_lock);
245 requeue_list(pending_bios, pending, tail);
246 goto loop_lock;
247 }
248
Chris Mason8b712842008-06-11 16:50:36 -0400249 cur = pending;
250 pending = pending->bi_next;
251 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400252 atomic_dec(&fs_info->nr_async_bios);
253
254 if (atomic_read(&fs_info->nr_async_bios) < limit &&
255 waitqueue_active(&fs_info->async_submit_wait))
256 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400257
258 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Mason8b712842008-06-11 16:50:36 -0400259 submit_bio(cur->bi_rw, cur);
260 num_run++;
Chris Masond644d8a2009-06-09 15:59:22 -0400261 batch_run++;
262
Chris Masonffbd5172009-04-20 15:50:09 -0400263 if (bio_sync(cur))
264 num_sync_run++;
265
266 if (need_resched()) {
267 if (num_sync_run) {
268 blk_run_backing_dev(bdi, NULL);
269 num_sync_run = 0;
270 }
271 cond_resched();
272 }
Chris Mason8b712842008-06-11 16:50:36 -0400273
274 /*
275 * we made progress, there is more work to do and the bdi
276 * is now congested. Back off and let other work structs
277 * run instead
278 */
Chris Masond644d8a2009-06-09 15:59:22 -0400279 if (pending && bdi_write_congested(bdi) && batch_run > 32 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500280 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400281 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400282
Chris Masonb765ead2009-04-03 10:27:10 -0400283 ioc = current->io_context;
284
285 /*
286 * the main goal here is that we don't want to
287 * block if we're going to be able to submit
288 * more requests without blocking.
289 *
290 * This code does two great things, it pokes into
291 * the elevator code from a filesystem _and_
292 * it makes assumptions about how batching works.
293 */
294 if (ioc && ioc->nr_batch_requests > 0 &&
295 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
296 (last_waited == 0 ||
297 ioc->last_waited == last_waited)) {
298 /*
299 * we want to go through our batch of
300 * requests and stop. So, we copy out
301 * the ioc->last_waited time and test
302 * against it before looping
303 */
304 last_waited = ioc->last_waited;
Chris Masonffbd5172009-04-20 15:50:09 -0400305 if (need_resched()) {
306 if (num_sync_run) {
307 blk_run_backing_dev(bdi, NULL);
308 num_sync_run = 0;
309 }
310 cond_resched();
311 }
Chris Masonb765ead2009-04-03 10:27:10 -0400312 continue;
313 }
Chris Mason8b712842008-06-11 16:50:36 -0400314 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400315 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500316 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400317
318 spin_unlock(&device->io_lock);
319 btrfs_requeue_work(&device->work);
320 goto done;
321 }
322 }
Chris Masonffbd5172009-04-20 15:50:09 -0400323
324 if (num_sync_run) {
325 num_sync_run = 0;
326 blk_run_backing_dev(bdi, NULL);
327 }
328
329 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400330 if (again)
331 goto loop;
Chris Masona6837052009-02-04 09:19:41 -0500332
333 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400334 if (device->pending_bios.head || device->pending_sync_bios.head)
Chris Masona6837052009-02-04 09:19:41 -0500335 goto loop_lock;
336 spin_unlock(&device->io_lock);
Chris Masonbedf7622009-04-03 10:32:58 -0400337
338 /*
339 * IO has already been through a long path to get here. Checksumming,
340 * async helper threads, perhaps compression. We've done a pretty
341 * good job of collecting a batch of IO and should just unplug
342 * the device right away.
343 *
344 * This will help anyone who is waiting on the IO, they might have
345 * already unplugged, but managed to do so before the bio they
346 * cared about found its way down here.
347 */
348 blk_run_backing_dev(bdi, NULL);
Chris Mason8b712842008-06-11 16:50:36 -0400349done:
350 return 0;
351}
352
Christoph Hellwigb2950862008-12-02 09:54:17 -0500353static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400354{
355 struct btrfs_device *device;
356
357 device = container_of(work, struct btrfs_device, work);
358 run_scheduled_bios(device);
359}
360
Chris Masona1b32a52008-09-05 16:09:51 -0400361static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400362 struct btrfs_super_block *disk_super,
363 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
364{
365 struct btrfs_device *device;
366 struct btrfs_fs_devices *fs_devices;
367 u64 found_transid = btrfs_super_generation(disk_super);
368
369 fs_devices = find_fsid(disk_super->fsid);
370 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400371 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400372 if (!fs_devices)
373 return -ENOMEM;
374 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400375 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400376 list_add(&fs_devices->list, &fs_uuids);
377 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
378 fs_devices->latest_devid = devid;
379 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400380 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400381 device = NULL;
382 } else {
Chris Masona4437552008-04-18 10:29:38 -0400383 device = __find_device(&fs_devices->devices, devid,
384 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400385 }
386 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500387 if (fs_devices->opened)
388 return -EBUSY;
389
Chris Mason8a4b83c2008-03-24 15:02:07 -0400390 device = kzalloc(sizeof(*device), GFP_NOFS);
391 if (!device) {
392 /* we can safely leave the fs_devices entry around */
393 return -ENOMEM;
394 }
395 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400396 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400397 memcpy(device->uuid, disk_super->dev_item.uuid,
398 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400399 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400400 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400401 device->name = kstrdup(path, GFP_NOFS);
402 if (!device->name) {
403 kfree(device);
404 return -ENOMEM;
405 }
Yan Zheng2b820322008-11-17 21:11:30 -0500406 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400407
408 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400409 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400410 mutex_unlock(&fs_devices->device_list_mutex);
411
Yan Zheng2b820322008-11-17 21:11:30 -0500412 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400413 fs_devices->num_devices++;
414 }
415
416 if (found_transid > fs_devices->latest_trans) {
417 fs_devices->latest_devid = devid;
418 fs_devices->latest_trans = found_transid;
419 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400420 *fs_devices_ret = fs_devices;
421 return 0;
422}
423
Yan Zhenge4404d62008-12-12 10:03:26 -0500424static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
425{
426 struct btrfs_fs_devices *fs_devices;
427 struct btrfs_device *device;
428 struct btrfs_device *orig_dev;
429
430 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
431 if (!fs_devices)
432 return ERR_PTR(-ENOMEM);
433
434 INIT_LIST_HEAD(&fs_devices->devices);
435 INIT_LIST_HEAD(&fs_devices->alloc_list);
436 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400437 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500438 fs_devices->latest_devid = orig->latest_devid;
439 fs_devices->latest_trans = orig->latest_trans;
440 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
441
Chris Masone5e9a522009-06-10 15:17:02 -0400442 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500443 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
444 device = kzalloc(sizeof(*device), GFP_NOFS);
445 if (!device)
446 goto error;
447
448 device->name = kstrdup(orig_dev->name, GFP_NOFS);
449 if (!device->name)
450 goto error;
451
452 device->devid = orig_dev->devid;
453 device->work.func = pending_bios_fn;
454 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
455 device->barriers = 1;
456 spin_lock_init(&device->io_lock);
457 INIT_LIST_HEAD(&device->dev_list);
458 INIT_LIST_HEAD(&device->dev_alloc_list);
459
460 list_add(&device->dev_list, &fs_devices->devices);
461 device->fs_devices = fs_devices;
462 fs_devices->num_devices++;
463 }
Chris Masone5e9a522009-06-10 15:17:02 -0400464 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500465 return fs_devices;
466error:
Chris Masone5e9a522009-06-10 15:17:02 -0400467 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500468 free_fs_devices(fs_devices);
469 return ERR_PTR(-ENOMEM);
470}
471
Chris Masondfe25022008-05-13 13:46:40 -0400472int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
473{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500474 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400475
476 mutex_lock(&uuid_mutex);
477again:
Chris Masone5e9a522009-06-10 15:17:02 -0400478 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500479 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500480 if (device->in_fs_metadata)
481 continue;
482
483 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500484 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500485 device->bdev = NULL;
486 fs_devices->open_devices--;
487 }
488 if (device->writeable) {
489 list_del_init(&device->dev_alloc_list);
490 device->writeable = 0;
491 fs_devices->rw_devices--;
492 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500493 list_del_init(&device->dev_list);
494 fs_devices->num_devices--;
495 kfree(device->name);
496 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400497 }
Chris Masone5e9a522009-06-10 15:17:02 -0400498 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500499
500 if (fs_devices->seed) {
501 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500502 goto again;
503 }
504
Chris Masondfe25022008-05-13 13:46:40 -0400505 mutex_unlock(&uuid_mutex);
506 return 0;
507}
Chris Masona0af4692008-05-13 16:03:06 -0400508
Yan Zheng2b820322008-11-17 21:11:30 -0500509static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400510{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400511 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500512
Yan Zheng2b820322008-11-17 21:11:30 -0500513 if (--fs_devices->opened > 0)
514 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400515
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500516 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400517 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500518 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400519 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400520 }
Yan Zheng2b820322008-11-17 21:11:30 -0500521 if (device->writeable) {
522 list_del_init(&device->dev_alloc_list);
523 fs_devices->rw_devices--;
524 }
525
Chris Mason8a4b83c2008-03-24 15:02:07 -0400526 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500527 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400528 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400529 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500530 WARN_ON(fs_devices->open_devices);
531 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500532 fs_devices->opened = 0;
533 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500534
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 return 0;
536}
537
Yan Zheng2b820322008-11-17 21:11:30 -0500538int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
539{
Yan Zhenge4404d62008-12-12 10:03:26 -0500540 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500541 int ret;
542
543 mutex_lock(&uuid_mutex);
544 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500545 if (!fs_devices->opened) {
546 seed_devices = fs_devices->seed;
547 fs_devices->seed = NULL;
548 }
Yan Zheng2b820322008-11-17 21:11:30 -0500549 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500550
551 while (seed_devices) {
552 fs_devices = seed_devices;
553 seed_devices = fs_devices->seed;
554 __btrfs_close_devices(fs_devices);
555 free_fs_devices(fs_devices);
556 }
Yan Zheng2b820322008-11-17 21:11:30 -0500557 return ret;
558}
559
Yan Zhenge4404d62008-12-12 10:03:26 -0500560static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
561 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400562{
563 struct block_device *bdev;
564 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400565 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400566 struct block_device *latest_bdev = NULL;
567 struct buffer_head *bh;
568 struct btrfs_super_block *disk_super;
569 u64 latest_devid = 0;
570 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400571 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500572 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400573 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500575 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400576 if (device->bdev)
577 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400578 if (!device->name)
579 continue;
580
Chris Mason15916de2008-11-19 21:17:22 -0500581 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400582 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500583 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400584 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400585 }
Chris Masona061fc82008-05-07 11:43:44 -0400586 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400587
Yan Zhenga512bbf2008-12-08 16:46:26 -0500588 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400589 if (!bh)
590 goto error_close;
591
592 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masona0af4692008-05-13 16:03:06 -0400593 devid = le64_to_cpu(disk_super->dev_item.devid);
594 if (devid != device->devid)
595 goto error_brelse;
596
Yan Zheng2b820322008-11-17 21:11:30 -0500597 if (memcmp(device->uuid, disk_super->dev_item.uuid,
598 BTRFS_UUID_SIZE))
599 goto error_brelse;
600
601 device->generation = btrfs_super_generation(disk_super);
602 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400603 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500604 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400605 latest_bdev = bdev;
606 }
607
Yan Zheng2b820322008-11-17 21:11:30 -0500608 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
609 device->writeable = 0;
610 } else {
611 device->writeable = !bdev_read_only(bdev);
612 seeding = 0;
613 }
614
Chris Mason8a4b83c2008-03-24 15:02:07 -0400615 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400616 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500617 device->mode = flags;
618
Chris Masonc2898112009-06-10 09:51:32 -0400619 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
620 fs_devices->rotating = 1;
621
Chris Masona0af4692008-05-13 16:03:06 -0400622 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500623 if (device->writeable) {
624 fs_devices->rw_devices++;
625 list_add(&device->dev_alloc_list,
626 &fs_devices->alloc_list);
627 }
Chris Masona0af4692008-05-13 16:03:06 -0400628 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400629
Chris Masona0af4692008-05-13 16:03:06 -0400630error_brelse:
631 brelse(bh);
632error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500633 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400634error:
635 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400636 }
Chris Masona0af4692008-05-13 16:03:06 -0400637 if (fs_devices->open_devices == 0) {
638 ret = -EIO;
639 goto out;
640 }
Yan Zheng2b820322008-11-17 21:11:30 -0500641 fs_devices->seeding = seeding;
642 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400643 fs_devices->latest_bdev = latest_bdev;
644 fs_devices->latest_devid = latest_devid;
645 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500646 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400647out:
Yan Zheng2b820322008-11-17 21:11:30 -0500648 return ret;
649}
650
651int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500652 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500653{
654 int ret;
655
656 mutex_lock(&uuid_mutex);
657 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500658 fs_devices->opened++;
659 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500660 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500661 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500662 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400663 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400664 return ret;
665}
666
Christoph Hellwig97288f22008-12-02 06:36:09 -0500667int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400668 struct btrfs_fs_devices **fs_devices_ret)
669{
670 struct btrfs_super_block *disk_super;
671 struct block_device *bdev;
672 struct buffer_head *bh;
673 int ret;
674 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400675 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400676
677 mutex_lock(&uuid_mutex);
678
Chris Mason15916de2008-11-19 21:17:22 -0500679 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400680
681 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682 ret = PTR_ERR(bdev);
683 goto error;
684 }
685
686 ret = set_blocksize(bdev, 4096);
687 if (ret)
688 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500689 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400690 if (!bh) {
691 ret = -EIO;
692 goto error_close;
693 }
694 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400695 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400696 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400697 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500698 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400699 else {
700 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500701 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400702 *(unsigned long long *)disk_super->fsid,
703 *(unsigned long long *)(disk_super->fsid + 8));
704 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500705 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500706 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400707 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
708
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709 brelse(bh);
710error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500711 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712error:
713 mutex_unlock(&uuid_mutex);
714 return ret;
715}
Chris Mason0b86a832008-03-24 15:01:56 -0400716
717/*
718 * this uses a pretty simple search, the expectation is that it is
719 * called very infrequently and that a given device has a small number
720 * of extents
721 */
Chris Masona1b32a52008-09-05 16:09:51 -0400722static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
723 struct btrfs_device *device,
Chris Masona1b32a52008-09-05 16:09:51 -0400724 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400725{
726 struct btrfs_key key;
727 struct btrfs_root *root = device->dev_root;
728 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500729 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400730 u64 hole_size = 0;
731 u64 last_byte = 0;
732 u64 search_start = 0;
733 u64 search_end = device->total_bytes;
734 int ret;
735 int slot = 0;
736 int start_found;
737 struct extent_buffer *l;
738
Yan Zheng2b820322008-11-17 21:11:30 -0500739 path = btrfs_alloc_path();
740 if (!path)
741 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400742 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500743 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400744
745 /* FIXME use last free of some kind */
746
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747 /* we don't want to overwrite the superblock on the drive,
748 * so we make sure to start at an offset of at least 1MB
749 */
750 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400751
752 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
753 search_start = max(root->fs_info->alloc_start, search_start);
754
Chris Mason0b86a832008-03-24 15:01:56 -0400755 key.objectid = device->devid;
756 key.offset = search_start;
757 key.type = BTRFS_DEV_EXTENT_KEY;
758 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
759 if (ret < 0)
760 goto error;
761 ret = btrfs_previous_item(root, path, 0, key.type);
762 if (ret < 0)
763 goto error;
764 l = path->nodes[0];
765 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
766 while (1) {
767 l = path->nodes[0];
768 slot = path->slots[0];
769 if (slot >= btrfs_header_nritems(l)) {
770 ret = btrfs_next_leaf(root, path);
771 if (ret == 0)
772 continue;
773 if (ret < 0)
774 goto error;
775no_more_items:
776 if (!start_found) {
777 if (search_start >= search_end) {
778 ret = -ENOSPC;
779 goto error;
780 }
781 *start = search_start;
782 start_found = 1;
783 goto check_pending;
784 }
785 *start = last_byte > search_start ?
786 last_byte : search_start;
787 if (search_end <= *start) {
788 ret = -ENOSPC;
789 goto error;
790 }
791 goto check_pending;
792 }
793 btrfs_item_key_to_cpu(l, &key, slot);
794
795 if (key.objectid < device->devid)
796 goto next;
797
798 if (key.objectid > device->devid)
799 goto no_more_items;
800
801 if (key.offset >= search_start && key.offset > last_byte &&
802 start_found) {
803 if (last_byte < search_start)
804 last_byte = search_start;
805 hole_size = key.offset - last_byte;
806 if (key.offset > last_byte &&
807 hole_size >= num_bytes) {
808 *start = last_byte;
809 goto check_pending;
810 }
811 }
Chris Masond3977122009-01-05 21:25:51 -0500812 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400813 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400814
815 start_found = 1;
816 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
817 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
818next:
819 path->slots[0]++;
820 cond_resched();
821 }
822check_pending:
823 /* we have to make sure we didn't find an extent that has already
824 * been allocated by the map tree or the original allocation
825 */
Chris Mason0b86a832008-03-24 15:01:56 -0400826 BUG_ON(*start < search_start);
827
Chris Mason6324fbf2008-03-24 15:01:59 -0400828 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400829 ret = -ENOSPC;
830 goto error;
831 }
832 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500833 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400834
835error:
Yan Zheng2b820322008-11-17 21:11:30 -0500836 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400837 return ret;
838}
839
Christoph Hellwigb2950862008-12-02 09:54:17 -0500840static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400841 struct btrfs_device *device,
842 u64 start)
843{
844 int ret;
845 struct btrfs_path *path;
846 struct btrfs_root *root = device->dev_root;
847 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400848 struct btrfs_key found_key;
849 struct extent_buffer *leaf = NULL;
850 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400851
852 path = btrfs_alloc_path();
853 if (!path)
854 return -ENOMEM;
855
856 key.objectid = device->devid;
857 key.offset = start;
858 key.type = BTRFS_DEV_EXTENT_KEY;
859
860 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400861 if (ret > 0) {
862 ret = btrfs_previous_item(root, path, key.objectid,
863 BTRFS_DEV_EXTENT_KEY);
864 BUG_ON(ret);
865 leaf = path->nodes[0];
866 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
867 extent = btrfs_item_ptr(leaf, path->slots[0],
868 struct btrfs_dev_extent);
869 BUG_ON(found_key.offset > start || found_key.offset +
870 btrfs_dev_extent_length(leaf, extent) < start);
871 ret = 0;
872 } else if (ret == 0) {
873 leaf = path->nodes[0];
874 extent = btrfs_item_ptr(leaf, path->slots[0],
875 struct btrfs_dev_extent);
876 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400877 BUG_ON(ret);
878
Chris Masondfe25022008-05-13 13:46:40 -0400879 if (device->bytes_used > 0)
880 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400881 ret = btrfs_del_item(trans, root, path);
882 BUG_ON(ret);
883
884 btrfs_free_path(path);
885 return ret;
886}
887
Yan Zheng2b820322008-11-17 21:11:30 -0500888int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400889 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400890 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500891 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400892{
893 int ret;
894 struct btrfs_path *path;
895 struct btrfs_root *root = device->dev_root;
896 struct btrfs_dev_extent *extent;
897 struct extent_buffer *leaf;
898 struct btrfs_key key;
899
Chris Masondfe25022008-05-13 13:46:40 -0400900 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400901 path = btrfs_alloc_path();
902 if (!path)
903 return -ENOMEM;
904
Chris Mason0b86a832008-03-24 15:01:56 -0400905 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500906 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400907 key.type = BTRFS_DEV_EXTENT_KEY;
908 ret = btrfs_insert_empty_item(trans, root, path, &key,
909 sizeof(*extent));
910 BUG_ON(ret);
911
912 leaf = path->nodes[0];
913 extent = btrfs_item_ptr(leaf, path->slots[0],
914 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400915 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
916 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
917 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
918
919 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
920 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
921 BTRFS_UUID_SIZE);
922
Chris Mason0b86a832008-03-24 15:01:56 -0400923 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
924 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400925 btrfs_free_path(path);
926 return ret;
927}
928
Chris Masona1b32a52008-09-05 16:09:51 -0400929static noinline int find_next_chunk(struct btrfs_root *root,
930 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400931{
932 struct btrfs_path *path;
933 int ret;
934 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400935 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400936 struct btrfs_key found_key;
937
938 path = btrfs_alloc_path();
939 BUG_ON(!path);
940
Chris Masone17cade2008-04-15 15:41:47 -0400941 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400942 key.offset = (u64)-1;
943 key.type = BTRFS_CHUNK_ITEM_KEY;
944
945 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
946 if (ret < 0)
947 goto error;
948
949 BUG_ON(ret == 0);
950
951 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
952 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400953 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400954 } else {
955 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
956 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400957 if (found_key.objectid != objectid)
958 *offset = 0;
959 else {
960 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
961 struct btrfs_chunk);
962 *offset = found_key.offset +
963 btrfs_chunk_length(path->nodes[0], chunk);
964 }
Chris Mason0b86a832008-03-24 15:01:56 -0400965 }
966 ret = 0;
967error:
968 btrfs_free_path(path);
969 return ret;
970}
971
Yan Zheng2b820322008-11-17 21:11:30 -0500972static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400973{
974 int ret;
975 struct btrfs_key key;
976 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500977 struct btrfs_path *path;
978
979 root = root->fs_info->chunk_root;
980
981 path = btrfs_alloc_path();
982 if (!path)
983 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400984
985 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
986 key.type = BTRFS_DEV_ITEM_KEY;
987 key.offset = (u64)-1;
988
989 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
990 if (ret < 0)
991 goto error;
992
993 BUG_ON(ret == 0);
994
995 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
996 BTRFS_DEV_ITEM_KEY);
997 if (ret) {
998 *objectid = 1;
999 } else {
1000 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1001 path->slots[0]);
1002 *objectid = found_key.offset + 1;
1003 }
1004 ret = 0;
1005error:
Yan Zheng2b820322008-11-17 21:11:30 -05001006 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001007 return ret;
1008}
1009
1010/*
1011 * the device information is stored in the chunk root
1012 * the btrfs_device struct should be fully filled in
1013 */
1014int btrfs_add_device(struct btrfs_trans_handle *trans,
1015 struct btrfs_root *root,
1016 struct btrfs_device *device)
1017{
1018 int ret;
1019 struct btrfs_path *path;
1020 struct btrfs_dev_item *dev_item;
1021 struct extent_buffer *leaf;
1022 struct btrfs_key key;
1023 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001024
1025 root = root->fs_info->chunk_root;
1026
1027 path = btrfs_alloc_path();
1028 if (!path)
1029 return -ENOMEM;
1030
Chris Mason0b86a832008-03-24 15:01:56 -04001031 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1032 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001033 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001034
1035 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001036 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001037 if (ret)
1038 goto out;
1039
1040 leaf = path->nodes[0];
1041 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1042
1043 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001044 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001045 btrfs_set_device_type(leaf, dev_item, device->type);
1046 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1047 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1048 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001049 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1050 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001051 btrfs_set_device_group(leaf, dev_item, 0);
1052 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1053 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001054 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001055
Chris Mason0b86a832008-03-24 15:01:56 -04001056 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001057 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001058 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1059 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001060 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001061
Yan Zheng2b820322008-11-17 21:11:30 -05001062 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001063out:
1064 btrfs_free_path(path);
1065 return ret;
1066}
Chris Mason8f18cf12008-04-25 16:53:30 -04001067
Chris Masona061fc82008-05-07 11:43:44 -04001068static int btrfs_rm_dev_item(struct btrfs_root *root,
1069 struct btrfs_device *device)
1070{
1071 int ret;
1072 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001073 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001074 struct btrfs_trans_handle *trans;
1075
1076 root = root->fs_info->chunk_root;
1077
1078 path = btrfs_alloc_path();
1079 if (!path)
1080 return -ENOMEM;
1081
1082 trans = btrfs_start_transaction(root, 1);
1083 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1084 key.type = BTRFS_DEV_ITEM_KEY;
1085 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001086 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001087
1088 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1089 if (ret < 0)
1090 goto out;
1091
1092 if (ret > 0) {
1093 ret = -ENOENT;
1094 goto out;
1095 }
1096
1097 ret = btrfs_del_item(trans, root, path);
1098 if (ret)
1099 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001100out:
1101 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001102 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001103 btrfs_commit_transaction(trans, root);
1104 return ret;
1105}
1106
1107int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1108{
1109 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001110 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001111 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001112 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001113 struct btrfs_super_block *disk_super;
1114 u64 all_avail;
1115 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001116 u64 num_devices;
1117 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001118 int ret = 0;
1119
Chris Masona061fc82008-05-07 11:43:44 -04001120 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001121 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001122
1123 all_avail = root->fs_info->avail_data_alloc_bits |
1124 root->fs_info->avail_system_alloc_bits |
1125 root->fs_info->avail_metadata_alloc_bits;
1126
1127 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001128 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001129 printk(KERN_ERR "btrfs: unable to go below four devices "
1130 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001131 ret = -EINVAL;
1132 goto out;
1133 }
1134
1135 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001136 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001137 printk(KERN_ERR "btrfs: unable to go below two "
1138 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001139 ret = -EINVAL;
1140 goto out;
1141 }
1142
Chris Masondfe25022008-05-13 13:46:40 -04001143 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001144 struct list_head *devices;
1145 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001146
Chris Masondfe25022008-05-13 13:46:40 -04001147 device = NULL;
1148 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001149 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001150 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001151 if (tmp->in_fs_metadata && !tmp->bdev) {
1152 device = tmp;
1153 break;
1154 }
1155 }
Chris Masone5e9a522009-06-10 15:17:02 -04001156 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001157 bdev = NULL;
1158 bh = NULL;
1159 disk_super = NULL;
1160 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001161 printk(KERN_ERR "btrfs: no missing devices found to "
1162 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001163 goto out;
1164 }
Chris Masondfe25022008-05-13 13:46:40 -04001165 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001166 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001167 root->fs_info->bdev_holder);
1168 if (IS_ERR(bdev)) {
1169 ret = PTR_ERR(bdev);
1170 goto out;
1171 }
1172
Yan Zheng2b820322008-11-17 21:11:30 -05001173 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001174 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001175 if (!bh) {
1176 ret = -EIO;
1177 goto error_close;
1178 }
1179 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masondfe25022008-05-13 13:46:40 -04001180 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001181 dev_uuid = disk_super->dev_item.uuid;
1182 device = btrfs_find_device(root, devid, dev_uuid,
1183 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001184 if (!device) {
1185 ret = -ENOENT;
1186 goto error_brelse;
1187 }
Chris Masondfe25022008-05-13 13:46:40 -04001188 }
Yan Zheng2b820322008-11-17 21:11:30 -05001189
1190 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001191 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1192 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001193 ret = -EINVAL;
1194 goto error_brelse;
1195 }
1196
1197 if (device->writeable) {
1198 list_del_init(&device->dev_alloc_list);
1199 root->fs_info->fs_devices->rw_devices--;
1200 }
Chris Masona061fc82008-05-07 11:43:44 -04001201
1202 ret = btrfs_shrink_device(device, 0);
1203 if (ret)
1204 goto error_brelse;
1205
Chris Masona061fc82008-05-07 11:43:44 -04001206 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1207 if (ret)
1208 goto error_brelse;
1209
Yan Zheng2b820322008-11-17 21:11:30 -05001210 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001211
1212 /*
1213 * the device list mutex makes sure that we don't change
1214 * the device list while someone else is writing out all
1215 * the device supers.
1216 */
1217 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001218 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001219 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1220
Yan Zhenge4404d62008-12-12 10:03:26 -05001221 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001222
1223 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1224 struct btrfs_device, dev_list);
1225 if (device->bdev == root->fs_info->sb->s_bdev)
1226 root->fs_info->sb->s_bdev = next_device->bdev;
1227 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1228 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1229
Yan Zhenge4404d62008-12-12 10:03:26 -05001230 if (device->bdev) {
1231 close_bdev_exclusive(device->bdev, device->mode);
1232 device->bdev = NULL;
1233 device->fs_devices->open_devices--;
1234 }
1235
Yan Zheng2b820322008-11-17 21:11:30 -05001236 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1237 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1238
Yan Zhenge4404d62008-12-12 10:03:26 -05001239 if (device->fs_devices->open_devices == 0) {
1240 struct btrfs_fs_devices *fs_devices;
1241 fs_devices = root->fs_info->fs_devices;
1242 while (fs_devices) {
1243 if (fs_devices->seed == device->fs_devices)
1244 break;
1245 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001246 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001247 fs_devices->seed = device->fs_devices->seed;
1248 device->fs_devices->seed = NULL;
1249 __btrfs_close_devices(device->fs_devices);
1250 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001251 }
1252
1253 /*
1254 * at this point, the device is zero sized. We want to
1255 * remove it from the devices list and zero out the old super
1256 */
1257 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001258 /* make sure this device isn't detected as part of
1259 * the FS anymore
1260 */
1261 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1262 set_buffer_dirty(bh);
1263 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001264 }
Chris Masona061fc82008-05-07 11:43:44 -04001265
Chris Masona061fc82008-05-07 11:43:44 -04001266 kfree(device->name);
1267 kfree(device);
1268 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001269
1270error_brelse:
1271 brelse(bh);
1272error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001273 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001274 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001275out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001276 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001277 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001278 return ret;
1279}
1280
Yan Zheng2b820322008-11-17 21:11:30 -05001281/*
1282 * does all the dirty work required for changing file system's UUID.
1283 */
1284static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1285 struct btrfs_root *root)
1286{
1287 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1288 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001289 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001290 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1291 struct btrfs_device *device;
1292 u64 super_flags;
1293
1294 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001295 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001296 return -EINVAL;
1297
Yan Zhenge4404d62008-12-12 10:03:26 -05001298 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1299 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001300 return -ENOMEM;
1301
Yan Zhenge4404d62008-12-12 10:03:26 -05001302 old_devices = clone_fs_devices(fs_devices);
1303 if (IS_ERR(old_devices)) {
1304 kfree(seed_devices);
1305 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001306 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001307
Yan Zheng2b820322008-11-17 21:11:30 -05001308 list_add(&old_devices->list, &fs_uuids);
1309
Yan Zhenge4404d62008-12-12 10:03:26 -05001310 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1311 seed_devices->opened = 1;
1312 INIT_LIST_HEAD(&seed_devices->devices);
1313 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001314 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001315 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1316 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1317 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1318 device->fs_devices = seed_devices;
1319 }
1320
Yan Zheng2b820322008-11-17 21:11:30 -05001321 fs_devices->seeding = 0;
1322 fs_devices->num_devices = 0;
1323 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001324 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001325
1326 generate_random_uuid(fs_devices->fsid);
1327 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1328 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1329 super_flags = btrfs_super_flags(disk_super) &
1330 ~BTRFS_SUPER_FLAG_SEEDING;
1331 btrfs_set_super_flags(disk_super, super_flags);
1332
1333 return 0;
1334}
1335
1336/*
1337 * strore the expected generation for seed devices in device items.
1338 */
1339static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root)
1341{
1342 struct btrfs_path *path;
1343 struct extent_buffer *leaf;
1344 struct btrfs_dev_item *dev_item;
1345 struct btrfs_device *device;
1346 struct btrfs_key key;
1347 u8 fs_uuid[BTRFS_UUID_SIZE];
1348 u8 dev_uuid[BTRFS_UUID_SIZE];
1349 u64 devid;
1350 int ret;
1351
1352 path = btrfs_alloc_path();
1353 if (!path)
1354 return -ENOMEM;
1355
1356 root = root->fs_info->chunk_root;
1357 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1358 key.offset = 0;
1359 key.type = BTRFS_DEV_ITEM_KEY;
1360
1361 while (1) {
1362 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1363 if (ret < 0)
1364 goto error;
1365
1366 leaf = path->nodes[0];
1367next_slot:
1368 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1369 ret = btrfs_next_leaf(root, path);
1370 if (ret > 0)
1371 break;
1372 if (ret < 0)
1373 goto error;
1374 leaf = path->nodes[0];
1375 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1376 btrfs_release_path(root, path);
1377 continue;
1378 }
1379
1380 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1381 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1382 key.type != BTRFS_DEV_ITEM_KEY)
1383 break;
1384
1385 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1386 struct btrfs_dev_item);
1387 devid = btrfs_device_id(leaf, dev_item);
1388 read_extent_buffer(leaf, dev_uuid,
1389 (unsigned long)btrfs_device_uuid(dev_item),
1390 BTRFS_UUID_SIZE);
1391 read_extent_buffer(leaf, fs_uuid,
1392 (unsigned long)btrfs_device_fsid(dev_item),
1393 BTRFS_UUID_SIZE);
1394 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1395 BUG_ON(!device);
1396
1397 if (device->fs_devices->seeding) {
1398 btrfs_set_device_generation(leaf, dev_item,
1399 device->generation);
1400 btrfs_mark_buffer_dirty(leaf);
1401 }
1402
1403 path->slots[0]++;
1404 goto next_slot;
1405 }
1406 ret = 0;
1407error:
1408 btrfs_free_path(path);
1409 return ret;
1410}
1411
Chris Mason788f20e2008-04-28 15:29:42 -04001412int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1413{
1414 struct btrfs_trans_handle *trans;
1415 struct btrfs_device *device;
1416 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001417 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001418 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001419 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001420 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001421 int ret = 0;
1422
Yan Zheng2b820322008-11-17 21:11:30 -05001423 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1424 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001425
Chris Mason15916de2008-11-19 21:17:22 -05001426 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Chris Masond3977122009-01-05 21:25:51 -05001427 if (!bdev)
Chris Mason788f20e2008-04-28 15:29:42 -04001428 return -EIO;
Chris Masona2135012008-06-25 16:01:30 -04001429
Yan Zheng2b820322008-11-17 21:11:30 -05001430 if (root->fs_info->fs_devices->seeding) {
1431 seeding_dev = 1;
1432 down_write(&sb->s_umount);
1433 mutex_lock(&uuid_mutex);
1434 }
1435
Chris Mason8c8bee12008-09-29 11:19:10 -04001436 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001437 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001438
Chris Mason788f20e2008-04-28 15:29:42 -04001439 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001440 /*
1441 * we have the volume lock, so we don't need the extra
1442 * device list mutex while reading the list here.
1443 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001444 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001445 if (device->bdev == bdev) {
1446 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001447 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001448 }
1449 }
1450
1451 device = kzalloc(sizeof(*device), GFP_NOFS);
1452 if (!device) {
1453 /* we can safely leave the fs_devices entry around */
1454 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001455 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001456 }
1457
Chris Mason788f20e2008-04-28 15:29:42 -04001458 device->name = kstrdup(device_path, GFP_NOFS);
1459 if (!device->name) {
1460 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001461 ret = -ENOMEM;
1462 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001463 }
Yan Zheng2b820322008-11-17 21:11:30 -05001464
1465 ret = find_next_devid(root, &device->devid);
1466 if (ret) {
1467 kfree(device);
1468 goto error;
1469 }
1470
1471 trans = btrfs_start_transaction(root, 1);
1472 lock_chunks(root);
1473
1474 device->barriers = 1;
1475 device->writeable = 1;
1476 device->work.func = pending_bios_fn;
1477 generate_random_uuid(device->uuid);
1478 spin_lock_init(&device->io_lock);
1479 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001480 device->io_width = root->sectorsize;
1481 device->io_align = root->sectorsize;
1482 device->sector_size = root->sectorsize;
1483 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001484 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001485 device->dev_root = root->fs_info->dev_root;
1486 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001487 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001488 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001489 set_blocksize(device->bdev, 4096);
1490
Yan Zheng2b820322008-11-17 21:11:30 -05001491 if (seeding_dev) {
1492 sb->s_flags &= ~MS_RDONLY;
1493 ret = btrfs_prepare_sprout(trans, root);
1494 BUG_ON(ret);
1495 }
1496
1497 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001498
1499 /*
1500 * we don't want write_supers to jump in here with our device
1501 * half setup
1502 */
1503 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001504 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1505 list_add(&device->dev_alloc_list,
1506 &root->fs_info->fs_devices->alloc_list);
1507 root->fs_info->fs_devices->num_devices++;
1508 root->fs_info->fs_devices->open_devices++;
1509 root->fs_info->fs_devices->rw_devices++;
1510 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1511
Chris Masonc2898112009-06-10 09:51:32 -04001512 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1513 root->fs_info->fs_devices->rotating = 1;
1514
Chris Mason788f20e2008-04-28 15:29:42 -04001515 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1516 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1517 total_bytes + device->total_bytes);
1518
1519 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1520 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1521 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001522 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001523
Yan Zheng2b820322008-11-17 21:11:30 -05001524 if (seeding_dev) {
1525 ret = init_first_rw_device(trans, root, device);
1526 BUG_ON(ret);
1527 ret = btrfs_finish_sprout(trans, root);
1528 BUG_ON(ret);
1529 } else {
1530 ret = btrfs_add_device(trans, root, device);
1531 }
1532
Chris Mason913d9522009-03-10 13:17:18 -04001533 /*
1534 * we've got more storage, clear any full flags on the space
1535 * infos
1536 */
1537 btrfs_clear_space_info_full(root->fs_info);
1538
Chris Mason7d9eb122008-07-08 14:19:17 -04001539 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001540 btrfs_commit_transaction(trans, root);
1541
1542 if (seeding_dev) {
1543 mutex_unlock(&uuid_mutex);
1544 up_write(&sb->s_umount);
1545
1546 ret = btrfs_relocate_sys_chunks(root);
1547 BUG_ON(ret);
1548 }
1549out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001550 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001551 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001552error:
Chris Mason15916de2008-11-19 21:17:22 -05001553 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001554 if (seeding_dev) {
1555 mutex_unlock(&uuid_mutex);
1556 up_write(&sb->s_umount);
1557 }
Chris Mason788f20e2008-04-28 15:29:42 -04001558 goto out;
1559}
1560
Chris Masond3977122009-01-05 21:25:51 -05001561static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1562 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001563{
1564 int ret;
1565 struct btrfs_path *path;
1566 struct btrfs_root *root;
1567 struct btrfs_dev_item *dev_item;
1568 struct extent_buffer *leaf;
1569 struct btrfs_key key;
1570
1571 root = device->dev_root->fs_info->chunk_root;
1572
1573 path = btrfs_alloc_path();
1574 if (!path)
1575 return -ENOMEM;
1576
1577 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1578 key.type = BTRFS_DEV_ITEM_KEY;
1579 key.offset = device->devid;
1580
1581 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1582 if (ret < 0)
1583 goto out;
1584
1585 if (ret > 0) {
1586 ret = -ENOENT;
1587 goto out;
1588 }
1589
1590 leaf = path->nodes[0];
1591 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1592
1593 btrfs_set_device_id(leaf, dev_item, device->devid);
1594 btrfs_set_device_type(leaf, dev_item, device->type);
1595 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1596 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1597 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001598 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001599 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1600 btrfs_mark_buffer_dirty(leaf);
1601
1602out:
1603 btrfs_free_path(path);
1604 return ret;
1605}
1606
Chris Mason7d9eb122008-07-08 14:19:17 -04001607static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001608 struct btrfs_device *device, u64 new_size)
1609{
1610 struct btrfs_super_block *super_copy =
1611 &device->dev_root->fs_info->super_copy;
1612 u64 old_total = btrfs_super_total_bytes(super_copy);
1613 u64 diff = new_size - device->total_bytes;
1614
Yan Zheng2b820322008-11-17 21:11:30 -05001615 if (!device->writeable)
1616 return -EACCES;
1617 if (new_size <= device->total_bytes)
1618 return -EINVAL;
1619
Chris Mason8f18cf12008-04-25 16:53:30 -04001620 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001621 device->fs_devices->total_rw_bytes += diff;
1622
1623 device->total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001624 btrfs_clear_space_info_full(device->dev_root->fs_info);
1625
Chris Mason8f18cf12008-04-25 16:53:30 -04001626 return btrfs_update_device(trans, device);
1627}
1628
Chris Mason7d9eb122008-07-08 14:19:17 -04001629int btrfs_grow_device(struct btrfs_trans_handle *trans,
1630 struct btrfs_device *device, u64 new_size)
1631{
1632 int ret;
1633 lock_chunks(device->dev_root);
1634 ret = __btrfs_grow_device(trans, device, new_size);
1635 unlock_chunks(device->dev_root);
1636 return ret;
1637}
1638
Chris Mason8f18cf12008-04-25 16:53:30 -04001639static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1640 struct btrfs_root *root,
1641 u64 chunk_tree, u64 chunk_objectid,
1642 u64 chunk_offset)
1643{
1644 int ret;
1645 struct btrfs_path *path;
1646 struct btrfs_key key;
1647
1648 root = root->fs_info->chunk_root;
1649 path = btrfs_alloc_path();
1650 if (!path)
1651 return -ENOMEM;
1652
1653 key.objectid = chunk_objectid;
1654 key.offset = chunk_offset;
1655 key.type = BTRFS_CHUNK_ITEM_KEY;
1656
1657 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1658 BUG_ON(ret);
1659
1660 ret = btrfs_del_item(trans, root, path);
1661 BUG_ON(ret);
1662
1663 btrfs_free_path(path);
1664 return 0;
1665}
1666
Christoph Hellwigb2950862008-12-02 09:54:17 -05001667static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001668 chunk_offset)
1669{
1670 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1671 struct btrfs_disk_key *disk_key;
1672 struct btrfs_chunk *chunk;
1673 u8 *ptr;
1674 int ret = 0;
1675 u32 num_stripes;
1676 u32 array_size;
1677 u32 len = 0;
1678 u32 cur;
1679 struct btrfs_key key;
1680
1681 array_size = btrfs_super_sys_array_size(super_copy);
1682
1683 ptr = super_copy->sys_chunk_array;
1684 cur = 0;
1685
1686 while (cur < array_size) {
1687 disk_key = (struct btrfs_disk_key *)ptr;
1688 btrfs_disk_key_to_cpu(&key, disk_key);
1689
1690 len = sizeof(*disk_key);
1691
1692 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1693 chunk = (struct btrfs_chunk *)(ptr + len);
1694 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1695 len += btrfs_chunk_item_size(num_stripes);
1696 } else {
1697 ret = -EIO;
1698 break;
1699 }
1700 if (key.objectid == chunk_objectid &&
1701 key.offset == chunk_offset) {
1702 memmove(ptr, ptr + len, array_size - (cur + len));
1703 array_size -= len;
1704 btrfs_set_super_sys_array_size(super_copy, array_size);
1705 } else {
1706 ptr += len;
1707 cur += len;
1708 }
1709 }
1710 return ret;
1711}
1712
Christoph Hellwigb2950862008-12-02 09:54:17 -05001713static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001714 u64 chunk_tree, u64 chunk_objectid,
1715 u64 chunk_offset)
1716{
1717 struct extent_map_tree *em_tree;
1718 struct btrfs_root *extent_root;
1719 struct btrfs_trans_handle *trans;
1720 struct extent_map *em;
1721 struct map_lookup *map;
1722 int ret;
1723 int i;
1724
1725 root = root->fs_info->chunk_root;
1726 extent_root = root->fs_info->extent_root;
1727 em_tree = &root->fs_info->mapping_tree.map_tree;
1728
1729 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001730 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001731 BUG_ON(ret);
1732
1733 trans = btrfs_start_transaction(root, 1);
1734 BUG_ON(!trans);
1735
Chris Mason7d9eb122008-07-08 14:19:17 -04001736 lock_chunks(root);
1737
Chris Mason8f18cf12008-04-25 16:53:30 -04001738 /*
1739 * step two, delete the device extents and the
1740 * chunk tree entries
1741 */
1742 spin_lock(&em_tree->lock);
1743 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1744 spin_unlock(&em_tree->lock);
1745
Chris Masona061fc82008-05-07 11:43:44 -04001746 BUG_ON(em->start > chunk_offset ||
1747 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001748 map = (struct map_lookup *)em->bdev;
1749
1750 for (i = 0; i < map->num_stripes; i++) {
1751 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1752 map->stripes[i].physical);
1753 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001754
Chris Masondfe25022008-05-13 13:46:40 -04001755 if (map->stripes[i].dev) {
1756 ret = btrfs_update_device(trans, map->stripes[i].dev);
1757 BUG_ON(ret);
1758 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001759 }
1760 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1761 chunk_offset);
1762
1763 BUG_ON(ret);
1764
1765 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1766 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1767 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001768 }
1769
Zheng Yan1a40e232008-09-26 10:09:34 -04001770 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1771 BUG_ON(ret);
1772
Chris Mason8f18cf12008-04-25 16:53:30 -04001773 spin_lock(&em_tree->lock);
1774 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001775 spin_unlock(&em_tree->lock);
1776
Chris Mason8f18cf12008-04-25 16:53:30 -04001777 kfree(map);
1778 em->bdev = NULL;
1779
1780 /* once for the tree */
1781 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001782 /* once for us */
1783 free_extent_map(em);
1784
Chris Mason7d9eb122008-07-08 14:19:17 -04001785 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001786 btrfs_end_transaction(trans, root);
1787 return 0;
1788}
1789
Yan Zheng2b820322008-11-17 21:11:30 -05001790static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1791{
1792 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1793 struct btrfs_path *path;
1794 struct extent_buffer *leaf;
1795 struct btrfs_chunk *chunk;
1796 struct btrfs_key key;
1797 struct btrfs_key found_key;
1798 u64 chunk_tree = chunk_root->root_key.objectid;
1799 u64 chunk_type;
1800 int ret;
1801
1802 path = btrfs_alloc_path();
1803 if (!path)
1804 return -ENOMEM;
1805
1806 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1807 key.offset = (u64)-1;
1808 key.type = BTRFS_CHUNK_ITEM_KEY;
1809
1810 while (1) {
1811 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1812 if (ret < 0)
1813 goto error;
1814 BUG_ON(ret == 0);
1815
1816 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1817 key.type);
1818 if (ret < 0)
1819 goto error;
1820 if (ret > 0)
1821 break;
1822
1823 leaf = path->nodes[0];
1824 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1825
1826 chunk = btrfs_item_ptr(leaf, path->slots[0],
1827 struct btrfs_chunk);
1828 chunk_type = btrfs_chunk_type(leaf, chunk);
1829 btrfs_release_path(chunk_root, path);
1830
1831 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1832 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1833 found_key.objectid,
1834 found_key.offset);
1835 BUG_ON(ret);
1836 }
1837
1838 if (found_key.offset == 0)
1839 break;
1840 key.offset = found_key.offset - 1;
1841 }
1842 ret = 0;
1843error:
1844 btrfs_free_path(path);
1845 return ret;
1846}
1847
Chris Masonec44a352008-04-28 15:29:52 -04001848static u64 div_factor(u64 num, int factor)
1849{
1850 if (factor == 10)
1851 return num;
1852 num *= factor;
1853 do_div(num, 10);
1854 return num;
1855}
1856
Chris Masonec44a352008-04-28 15:29:52 -04001857int btrfs_balance(struct btrfs_root *dev_root)
1858{
1859 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001860 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1861 struct btrfs_device *device;
1862 u64 old_size;
1863 u64 size_to_free;
1864 struct btrfs_path *path;
1865 struct btrfs_key key;
1866 struct btrfs_chunk *chunk;
1867 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1868 struct btrfs_trans_handle *trans;
1869 struct btrfs_key found_key;
1870
Yan Zheng2b820322008-11-17 21:11:30 -05001871 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1872 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001873
Chris Mason7d9eb122008-07-08 14:19:17 -04001874 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001875 dev_root = dev_root->fs_info->dev_root;
1876
Chris Masonec44a352008-04-28 15:29:52 -04001877 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001878 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001879 old_size = device->total_bytes;
1880 size_to_free = div_factor(old_size, 1);
1881 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001882 if (!device->writeable ||
1883 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001884 continue;
1885
1886 ret = btrfs_shrink_device(device, old_size - size_to_free);
1887 BUG_ON(ret);
1888
1889 trans = btrfs_start_transaction(dev_root, 1);
1890 BUG_ON(!trans);
1891
1892 ret = btrfs_grow_device(trans, device, old_size);
1893 BUG_ON(ret);
1894
1895 btrfs_end_transaction(trans, dev_root);
1896 }
1897
1898 /* step two, relocate all the chunks */
1899 path = btrfs_alloc_path();
1900 BUG_ON(!path);
1901
1902 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1903 key.offset = (u64)-1;
1904 key.type = BTRFS_CHUNK_ITEM_KEY;
1905
Chris Masond3977122009-01-05 21:25:51 -05001906 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001907 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1908 if (ret < 0)
1909 goto error;
1910
1911 /*
1912 * this shouldn't happen, it means the last relocate
1913 * failed
1914 */
1915 if (ret == 0)
1916 break;
1917
1918 ret = btrfs_previous_item(chunk_root, path, 0,
1919 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001920 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001921 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001922
Chris Masonec44a352008-04-28 15:29:52 -04001923 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1924 path->slots[0]);
1925 if (found_key.objectid != key.objectid)
1926 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001927
Chris Masonec44a352008-04-28 15:29:52 -04001928 chunk = btrfs_item_ptr(path->nodes[0],
1929 path->slots[0],
1930 struct btrfs_chunk);
1931 key.offset = found_key.offset;
1932 /* chunk zero is special */
1933 if (key.offset == 0)
1934 break;
1935
Chris Mason7d9eb122008-07-08 14:19:17 -04001936 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001937 ret = btrfs_relocate_chunk(chunk_root,
1938 chunk_root->root_key.objectid,
1939 found_key.objectid,
1940 found_key.offset);
1941 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001942 }
1943 ret = 0;
1944error:
1945 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001946 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001947 return ret;
1948}
1949
Chris Mason8f18cf12008-04-25 16:53:30 -04001950/*
1951 * shrinking a device means finding all of the device extents past
1952 * the new size, and then following the back refs to the chunks.
1953 * The chunk relocation code actually frees the device extent
1954 */
1955int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1956{
1957 struct btrfs_trans_handle *trans;
1958 struct btrfs_root *root = device->dev_root;
1959 struct btrfs_dev_extent *dev_extent = NULL;
1960 struct btrfs_path *path;
1961 u64 length;
1962 u64 chunk_tree;
1963 u64 chunk_objectid;
1964 u64 chunk_offset;
1965 int ret;
1966 int slot;
1967 struct extent_buffer *l;
1968 struct btrfs_key key;
1969 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1970 u64 old_total = btrfs_super_total_bytes(super_copy);
1971 u64 diff = device->total_bytes - new_size;
1972
Yan Zheng2b820322008-11-17 21:11:30 -05001973 if (new_size >= device->total_bytes)
1974 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001975
1976 path = btrfs_alloc_path();
1977 if (!path)
1978 return -ENOMEM;
1979
1980 trans = btrfs_start_transaction(root, 1);
1981 if (!trans) {
1982 ret = -ENOMEM;
1983 goto done;
1984 }
1985
1986 path->reada = 2;
1987
Chris Mason7d9eb122008-07-08 14:19:17 -04001988 lock_chunks(root);
1989
Chris Mason8f18cf12008-04-25 16:53:30 -04001990 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001991 if (device->writeable)
1992 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04001993 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001994 btrfs_end_transaction(trans, root);
1995
1996 key.objectid = device->devid;
1997 key.offset = (u64)-1;
1998 key.type = BTRFS_DEV_EXTENT_KEY;
1999
2000 while (1) {
2001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2002 if (ret < 0)
2003 goto done;
2004
2005 ret = btrfs_previous_item(root, path, 0, key.type);
2006 if (ret < 0)
2007 goto done;
2008 if (ret) {
2009 ret = 0;
2010 goto done;
2011 }
2012
2013 l = path->nodes[0];
2014 slot = path->slots[0];
2015 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2016
2017 if (key.objectid != device->devid)
2018 goto done;
2019
2020 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2021 length = btrfs_dev_extent_length(l, dev_extent);
2022
2023 if (key.offset + length <= new_size)
Chris Balld6397ba2009-04-27 07:29:03 -04002024 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002025
2026 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2027 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2028 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2029 btrfs_release_path(root, path);
2030
2031 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2032 chunk_offset);
2033 if (ret)
2034 goto done;
2035 }
2036
Chris Balld6397ba2009-04-27 07:29:03 -04002037 /* Shrinking succeeded, else we would be at "done". */
2038 trans = btrfs_start_transaction(root, 1);
2039 if (!trans) {
2040 ret = -ENOMEM;
2041 goto done;
2042 }
2043 lock_chunks(root);
2044
2045 device->disk_total_bytes = new_size;
2046 /* Now btrfs_update_device() will change the on-disk size. */
2047 ret = btrfs_update_device(trans, device);
2048 if (ret) {
2049 unlock_chunks(root);
2050 btrfs_end_transaction(trans, root);
2051 goto done;
2052 }
2053 WARN_ON(diff > old_total);
2054 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2055 unlock_chunks(root);
2056 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002057done:
2058 btrfs_free_path(path);
2059 return ret;
2060}
2061
Christoph Hellwigb2950862008-12-02 09:54:17 -05002062static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002063 struct btrfs_root *root,
2064 struct btrfs_key *key,
2065 struct btrfs_chunk *chunk, int item_size)
2066{
2067 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2068 struct btrfs_disk_key disk_key;
2069 u32 array_size;
2070 u8 *ptr;
2071
2072 array_size = btrfs_super_sys_array_size(super_copy);
2073 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2074 return -EFBIG;
2075
2076 ptr = super_copy->sys_chunk_array + array_size;
2077 btrfs_cpu_key_to_disk(&disk_key, key);
2078 memcpy(ptr, &disk_key, sizeof(disk_key));
2079 ptr += sizeof(disk_key);
2080 memcpy(ptr, chunk, item_size);
2081 item_size += sizeof(disk_key);
2082 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2083 return 0;
2084}
2085
Chris Masond3977122009-01-05 21:25:51 -05002086static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002087 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002088{
2089 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2090 return calc_size;
2091 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2092 return calc_size * (num_stripes / sub_stripes);
2093 else
2094 return calc_size * num_stripes;
2095}
2096
Yan Zheng2b820322008-11-17 21:11:30 -05002097static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2098 struct btrfs_root *extent_root,
2099 struct map_lookup **map_ret,
2100 u64 *num_bytes, u64 *stripe_size,
2101 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04002102{
Chris Mason593060d2008-03-25 16:50:33 -04002103 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04002104 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05002105 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04002106 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05002107 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002108 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04002109 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05002110 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04002111 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002112 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002113 u64 max_chunk_size = calc_size;
2114 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04002115 u64 avail;
2116 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002117 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04002118 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04002119 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002120 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002121 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002122 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002123 int index;
Chris Mason593060d2008-03-25 16:50:33 -04002124 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002125
Chris Masonec44a352008-04-28 15:29:52 -04002126 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2127 (type & BTRFS_BLOCK_GROUP_DUP)) {
2128 WARN_ON(1);
2129 type &= ~BTRFS_BLOCK_GROUP_DUP;
2130 }
Yan Zheng2b820322008-11-17 21:11:30 -05002131 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04002132 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04002133
Chris Masona40a90a2008-04-18 11:55:51 -04002134 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002135 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04002136 min_stripes = 2;
2137 }
2138 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04002139 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002140 min_stripes = 2;
2141 }
Chris Mason8790d502008-04-03 16:29:03 -04002142 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002143 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002144 if (num_stripes < 2)
2145 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04002146 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002147 }
Chris Mason321aecc2008-04-16 10:49:51 -04002148 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002149 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04002150 if (num_stripes < 4)
2151 return -ENOSPC;
2152 num_stripes &= ~(u32)1;
2153 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002154 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002155 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002156
2157 if (type & BTRFS_BLOCK_GROUP_DATA) {
2158 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002159 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002160 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2161 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002162 min_stripe_size = 32 * 1024 * 1024;
2163 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2164 calc_size = 8 * 1024 * 1024;
2165 max_chunk_size = calc_size * 2;
2166 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002167 }
2168
Yan Zheng2b820322008-11-17 21:11:30 -05002169 /* we don't want a chunk larger than 10% of writeable space */
2170 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2171 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002172
Chris Masona40a90a2008-04-18 11:55:51 -04002173again:
Yan Zheng2b820322008-11-17 21:11:30 -05002174 if (!map || map->num_stripes != num_stripes) {
2175 kfree(map);
2176 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2177 if (!map)
2178 return -ENOMEM;
2179 map->num_stripes = num_stripes;
2180 }
2181
Chris Mason9b3f68b2008-04-18 10:29:51 -04002182 if (calc_size * num_stripes > max_chunk_size) {
2183 calc_size = max_chunk_size;
2184 do_div(calc_size, num_stripes);
2185 do_div(calc_size, stripe_len);
2186 calc_size *= stripe_len;
2187 }
2188 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002189 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002190
Chris Mason9b3f68b2008-04-18 10:29:51 -04002191 do_div(calc_size, stripe_len);
2192 calc_size *= stripe_len;
2193
Yan Zheng2b820322008-11-17 21:11:30 -05002194 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002195 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002196
2197 if (type & BTRFS_BLOCK_GROUP_DUP)
2198 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002199 else
2200 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002201
Josef Bacik0f9dd462008-09-23 13:14:11 -04002202 /*
2203 * we add 1MB because we never use the first 1MB of the device, unless
2204 * we've looped, then we are likely allocating the maximum amount of
2205 * space left already
2206 */
2207 if (!looped)
2208 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002209
Yan Zheng2b820322008-11-17 21:11:30 -05002210 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002211 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002212 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002213 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002214 if (device->total_bytes > device->bytes_used)
2215 avail = device->total_bytes - device->bytes_used;
2216 else
2217 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002218 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002219
Chris Masondfe25022008-05-13 13:46:40 -04002220 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002221 ret = find_free_dev_extent(trans, device,
2222 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002223 if (ret == 0) {
2224 list_move_tail(&device->dev_alloc_list,
2225 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002226 map->stripes[index].dev = device;
2227 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002228 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002229 if (type & BTRFS_BLOCK_GROUP_DUP) {
2230 map->stripes[index].dev = device;
2231 map->stripes[index].physical =
2232 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002233 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002234 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002235 }
Chris Masondfe25022008-05-13 13:46:40 -04002236 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002237 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002238 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002239 break;
2240 }
Yan Zheng2b820322008-11-17 21:11:30 -05002241 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002242 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002243 if (index >= min_stripes) {
2244 num_stripes = index;
2245 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2246 num_stripes /= sub_stripes;
2247 num_stripes *= sub_stripes;
2248 }
2249 looped = 1;
2250 goto again;
2251 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002252 if (!looped && max_avail > 0) {
2253 looped = 1;
2254 calc_size = max_avail;
2255 goto again;
2256 }
Yan Zheng2b820322008-11-17 21:11:30 -05002257 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002258 return -ENOSPC;
2259 }
Chris Mason593060d2008-03-25 16:50:33 -04002260 map->sector_size = extent_root->sectorsize;
2261 map->stripe_len = stripe_len;
2262 map->io_align = stripe_len;
2263 map->io_width = stripe_len;
2264 map->type = type;
2265 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002266 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002267
Yan Zheng2b820322008-11-17 21:11:30 -05002268 *map_ret = map;
2269 *stripe_size = calc_size;
2270 *num_bytes = chunk_bytes_by_type(type, calc_size,
2271 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002272
2273 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002274 if (!em) {
2275 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002276 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002277 }
Chris Mason0b86a832008-03-24 15:01:56 -04002278 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002279 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002280 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002281 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002282 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002283
Chris Mason0b86a832008-03-24 15:01:56 -04002284 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2285 spin_lock(&em_tree->lock);
2286 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002287 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002288 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002289 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002290
2291 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2292 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2293 start, *num_bytes);
2294 BUG_ON(ret);
2295
2296 index = 0;
2297 while (index < map->num_stripes) {
2298 device = map->stripes[index].dev;
2299 dev_offset = map->stripes[index].physical;
2300
2301 ret = btrfs_alloc_dev_extent(trans, device,
2302 info->chunk_root->root_key.objectid,
2303 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2304 start, dev_offset, calc_size);
2305 BUG_ON(ret);
2306 index++;
2307 }
2308
2309 return 0;
2310}
2311
2312static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2313 struct btrfs_root *extent_root,
2314 struct map_lookup *map, u64 chunk_offset,
2315 u64 chunk_size, u64 stripe_size)
2316{
2317 u64 dev_offset;
2318 struct btrfs_key key;
2319 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2320 struct btrfs_device *device;
2321 struct btrfs_chunk *chunk;
2322 struct btrfs_stripe *stripe;
2323 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2324 int index = 0;
2325 int ret;
2326
2327 chunk = kzalloc(item_size, GFP_NOFS);
2328 if (!chunk)
2329 return -ENOMEM;
2330
2331 index = 0;
2332 while (index < map->num_stripes) {
2333 device = map->stripes[index].dev;
2334 device->bytes_used += stripe_size;
2335 ret = btrfs_update_device(trans, device);
2336 BUG_ON(ret);
2337 index++;
2338 }
2339
2340 index = 0;
2341 stripe = &chunk->stripe;
2342 while (index < map->num_stripes) {
2343 device = map->stripes[index].dev;
2344 dev_offset = map->stripes[index].physical;
2345
2346 btrfs_set_stack_stripe_devid(stripe, device->devid);
2347 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2348 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2349 stripe++;
2350 index++;
2351 }
2352
2353 btrfs_set_stack_chunk_length(chunk, chunk_size);
2354 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2355 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2356 btrfs_set_stack_chunk_type(chunk, map->type);
2357 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2358 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2359 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2360 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2361 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2362
2363 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2364 key.type = BTRFS_CHUNK_ITEM_KEY;
2365 key.offset = chunk_offset;
2366
2367 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2368 BUG_ON(ret);
2369
2370 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2371 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2372 item_size);
2373 BUG_ON(ret);
2374 }
2375 kfree(chunk);
2376 return 0;
2377}
2378
2379/*
2380 * Chunk allocation falls into two parts. The first part does works
2381 * that make the new allocated chunk useable, but not do any operation
2382 * that modifies the chunk tree. The second part does the works that
2383 * require modifying the chunk tree. This division is important for the
2384 * bootstrap process of adding storage to a seed btrfs.
2385 */
2386int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2387 struct btrfs_root *extent_root, u64 type)
2388{
2389 u64 chunk_offset;
2390 u64 chunk_size;
2391 u64 stripe_size;
2392 struct map_lookup *map;
2393 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2394 int ret;
2395
2396 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2397 &chunk_offset);
2398 if (ret)
2399 return ret;
2400
2401 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2402 &stripe_size, chunk_offset, type);
2403 if (ret)
2404 return ret;
2405
2406 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2407 chunk_size, stripe_size);
2408 BUG_ON(ret);
2409 return 0;
2410}
2411
Chris Masond3977122009-01-05 21:25:51 -05002412static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002413 struct btrfs_root *root,
2414 struct btrfs_device *device)
2415{
2416 u64 chunk_offset;
2417 u64 sys_chunk_offset;
2418 u64 chunk_size;
2419 u64 sys_chunk_size;
2420 u64 stripe_size;
2421 u64 sys_stripe_size;
2422 u64 alloc_profile;
2423 struct map_lookup *map;
2424 struct map_lookup *sys_map;
2425 struct btrfs_fs_info *fs_info = root->fs_info;
2426 struct btrfs_root *extent_root = fs_info->extent_root;
2427 int ret;
2428
2429 ret = find_next_chunk(fs_info->chunk_root,
2430 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2431 BUG_ON(ret);
2432
2433 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2434 (fs_info->metadata_alloc_profile &
2435 fs_info->avail_metadata_alloc_bits);
2436 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2437
2438 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2439 &stripe_size, chunk_offset, alloc_profile);
2440 BUG_ON(ret);
2441
2442 sys_chunk_offset = chunk_offset + chunk_size;
2443
2444 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2445 (fs_info->system_alloc_profile &
2446 fs_info->avail_system_alloc_bits);
2447 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2448
2449 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2450 &sys_chunk_size, &sys_stripe_size,
2451 sys_chunk_offset, alloc_profile);
2452 BUG_ON(ret);
2453
2454 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2455 BUG_ON(ret);
2456
2457 /*
2458 * Modifying chunk tree needs allocating new blocks from both
2459 * system block group and metadata block group. So we only can
2460 * do operations require modifying the chunk tree after both
2461 * block groups were created.
2462 */
2463 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2464 chunk_size, stripe_size);
2465 BUG_ON(ret);
2466
2467 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2468 sys_chunk_offset, sys_chunk_size,
2469 sys_stripe_size);
2470 BUG_ON(ret);
2471 return 0;
2472}
2473
2474int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2475{
2476 struct extent_map *em;
2477 struct map_lookup *map;
2478 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2479 int readonly = 0;
2480 int i;
2481
2482 spin_lock(&map_tree->map_tree.lock);
2483 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2484 spin_unlock(&map_tree->map_tree.lock);
2485 if (!em)
2486 return 1;
2487
2488 map = (struct map_lookup *)em->bdev;
2489 for (i = 0; i < map->num_stripes; i++) {
2490 if (!map->stripes[i].dev->writeable) {
2491 readonly = 1;
2492 break;
2493 }
2494 }
2495 free_extent_map(em);
2496 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002497}
2498
2499void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2500{
2501 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2502}
2503
2504void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2505{
2506 struct extent_map *em;
2507
Chris Masond3977122009-01-05 21:25:51 -05002508 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002509 spin_lock(&tree->map_tree.lock);
2510 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2511 if (em)
2512 remove_extent_mapping(&tree->map_tree, em);
2513 spin_unlock(&tree->map_tree.lock);
2514 if (!em)
2515 break;
2516 kfree(em->bdev);
2517 /* once for us */
2518 free_extent_map(em);
2519 /* once for the tree */
2520 free_extent_map(em);
2521 }
2522}
2523
Chris Masonf1885912008-04-09 16:28:12 -04002524int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2525{
2526 struct extent_map *em;
2527 struct map_lookup *map;
2528 struct extent_map_tree *em_tree = &map_tree->map_tree;
2529 int ret;
2530
2531 spin_lock(&em_tree->lock);
2532 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002533 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002534 BUG_ON(!em);
2535
2536 BUG_ON(em->start > logical || em->start + em->len < logical);
2537 map = (struct map_lookup *)em->bdev;
2538 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2539 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002540 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2541 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002542 else
2543 ret = 1;
2544 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002545 return ret;
2546}
2547
Chris Masondfe25022008-05-13 13:46:40 -04002548static int find_live_mirror(struct map_lookup *map, int first, int num,
2549 int optimal)
2550{
2551 int i;
2552 if (map->stripes[optimal].dev->bdev)
2553 return optimal;
2554 for (i = first; i < first + num; i++) {
2555 if (map->stripes[i].dev->bdev)
2556 return i;
2557 }
2558 /* we couldn't find one that doesn't fail. Just return something
2559 * and the io error handling code will clean up eventually
2560 */
2561 return optimal;
2562}
2563
Chris Masonf2d8d742008-04-21 10:03:05 -04002564static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2565 u64 logical, u64 *length,
2566 struct btrfs_multi_bio **multi_ret,
2567 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002568{
2569 struct extent_map *em;
2570 struct map_lookup *map;
2571 struct extent_map_tree *em_tree = &map_tree->map_tree;
2572 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002573 u64 stripe_offset;
2574 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002575 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002576 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002577 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002578 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002579 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002580 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002581 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002582
Chris Masond3977122009-01-05 21:25:51 -05002583 if (multi_ret && !(rw & (1 << BIO_RW)))
Chris Masoncea9e442008-04-09 16:28:12 -04002584 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002585again:
2586 if (multi_ret) {
2587 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2588 GFP_NOFS);
2589 if (!multi)
2590 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002591
2592 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002593 }
Chris Mason0b86a832008-03-24 15:01:56 -04002594
2595 spin_lock(&em_tree->lock);
2596 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002597 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002598
2599 if (!em && unplug_page)
2600 return 0;
2601
Chris Mason3b951512008-04-17 11:29:12 -04002602 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002603 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2604 (unsigned long long)logical,
2605 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002606 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002607 }
Chris Mason0b86a832008-03-24 15:01:56 -04002608
2609 BUG_ON(em->start > logical || em->start + em->len < logical);
2610 map = (struct map_lookup *)em->bdev;
2611 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002612
Chris Masonf1885912008-04-09 16:28:12 -04002613 if (mirror_num > map->num_stripes)
2614 mirror_num = 0;
2615
Chris Masoncea9e442008-04-09 16:28:12 -04002616 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002617 if (rw & (1 << BIO_RW)) {
2618 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2619 BTRFS_BLOCK_GROUP_DUP)) {
2620 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002621 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002622 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2623 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002624 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002625 }
2626 }
Chris Masonffbd5172009-04-20 15:50:09 -04002627 if (multi_ret && (rw & (1 << BIO_RW)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002628 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002629 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002630 free_extent_map(em);
2631 kfree(multi);
2632 goto again;
2633 }
Chris Mason593060d2008-03-25 16:50:33 -04002634 stripe_nr = offset;
2635 /*
2636 * stripe_nr counts the total number of stripes we have to stride
2637 * to get to this block
2638 */
2639 do_div(stripe_nr, map->stripe_len);
2640
2641 stripe_offset = stripe_nr * map->stripe_len;
2642 BUG_ON(offset < stripe_offset);
2643
2644 /* stripe_offset is the offset of this block in its stripe*/
2645 stripe_offset = offset - stripe_offset;
2646
Chris Masoncea9e442008-04-09 16:28:12 -04002647 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002648 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002649 BTRFS_BLOCK_GROUP_DUP)) {
2650 /* we limit the length of each bio to what fits in a stripe */
2651 *length = min_t(u64, em->len - offset,
2652 map->stripe_len - stripe_offset);
2653 } else {
2654 *length = em->len - offset;
2655 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002656
2657 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002658 goto out;
2659
Chris Masonf2d8d742008-04-21 10:03:05 -04002660 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002661 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002662 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002663 if (unplug_page || (rw & (1 << BIO_RW)))
2664 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002665 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002666 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002667 else {
2668 stripe_index = find_live_mirror(map, 0,
2669 map->num_stripes,
2670 current->pid % map->num_stripes);
2671 }
Chris Mason2fff7342008-04-29 14:12:09 -04002672
Chris Mason611f0e02008-04-03 16:29:03 -04002673 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002674 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002675 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002676 else if (mirror_num)
2677 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002678
Chris Mason321aecc2008-04-16 10:49:51 -04002679 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2680 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002681
2682 stripe_index = do_div(stripe_nr, factor);
2683 stripe_index *= map->sub_stripes;
2684
Chris Masonf2d8d742008-04-21 10:03:05 -04002685 if (unplug_page || (rw & (1 << BIO_RW)))
2686 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002687 else if (mirror_num)
2688 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002689 else {
2690 stripe_index = find_live_mirror(map, stripe_index,
2691 map->sub_stripes, stripe_index +
2692 current->pid % map->sub_stripes);
2693 }
Chris Mason8790d502008-04-03 16:29:03 -04002694 } else {
2695 /*
2696 * after this do_div call, stripe_nr is the number of stripes
2697 * on this device we have to walk to find the data, and
2698 * stripe_index is the number of our device in the stripe array
2699 */
2700 stripe_index = do_div(stripe_nr, map->num_stripes);
2701 }
Chris Mason593060d2008-03-25 16:50:33 -04002702 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002703
Chris Masonf2d8d742008-04-21 10:03:05 -04002704 for (i = 0; i < num_stripes; i++) {
2705 if (unplug_page) {
2706 struct btrfs_device *device;
2707 struct backing_dev_info *bdi;
2708
2709 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002710 if (device->bdev) {
2711 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002712 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002713 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002714 }
2715 } else {
2716 multi->stripes[i].physical =
2717 map->stripes[stripe_index].physical +
2718 stripe_offset + stripe_nr * map->stripe_len;
2719 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2720 }
Chris Masoncea9e442008-04-09 16:28:12 -04002721 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002722 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002723 if (multi_ret) {
2724 *multi_ret = multi;
2725 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002726 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002727 }
Chris Masoncea9e442008-04-09 16:28:12 -04002728out:
Chris Mason0b86a832008-03-24 15:01:56 -04002729 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002730 return 0;
2731}
2732
Chris Masonf2d8d742008-04-21 10:03:05 -04002733int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2734 u64 logical, u64 *length,
2735 struct btrfs_multi_bio **multi_ret, int mirror_num)
2736{
2737 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2738 mirror_num, NULL);
2739}
2740
Yan Zhenga512bbf2008-12-08 16:46:26 -05002741int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2742 u64 chunk_start, u64 physical, u64 devid,
2743 u64 **logical, int *naddrs, int *stripe_len)
2744{
2745 struct extent_map_tree *em_tree = &map_tree->map_tree;
2746 struct extent_map *em;
2747 struct map_lookup *map;
2748 u64 *buf;
2749 u64 bytenr;
2750 u64 length;
2751 u64 stripe_nr;
2752 int i, j, nr = 0;
2753
2754 spin_lock(&em_tree->lock);
2755 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2756 spin_unlock(&em_tree->lock);
2757
2758 BUG_ON(!em || em->start != chunk_start);
2759 map = (struct map_lookup *)em->bdev;
2760
2761 length = em->len;
2762 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2763 do_div(length, map->num_stripes / map->sub_stripes);
2764 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2765 do_div(length, map->num_stripes);
2766
2767 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2768 BUG_ON(!buf);
2769
2770 for (i = 0; i < map->num_stripes; i++) {
2771 if (devid && map->stripes[i].dev->devid != devid)
2772 continue;
2773 if (map->stripes[i].physical > physical ||
2774 map->stripes[i].physical + length <= physical)
2775 continue;
2776
2777 stripe_nr = physical - map->stripes[i].physical;
2778 do_div(stripe_nr, map->stripe_len);
2779
2780 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2781 stripe_nr = stripe_nr * map->num_stripes + i;
2782 do_div(stripe_nr, map->sub_stripes);
2783 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2784 stripe_nr = stripe_nr * map->num_stripes + i;
2785 }
2786 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002787 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002788 for (j = 0; j < nr; j++) {
2789 if (buf[j] == bytenr)
2790 break;
2791 }
Chris Mason934d3752008-12-08 16:43:10 -05002792 if (j == nr) {
2793 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002794 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002795 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002796 }
2797
2798 for (i = 0; i > nr; i++) {
2799 struct btrfs_multi_bio *multi;
2800 struct btrfs_bio_stripe *stripe;
2801 int ret;
2802
2803 length = 1;
2804 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2805 &length, &multi, 0);
2806 BUG_ON(ret);
2807
2808 stripe = multi->stripes;
2809 for (j = 0; j < multi->num_stripes; j++) {
2810 if (stripe->physical >= physical &&
2811 physical < stripe->physical + length)
2812 break;
2813 }
2814 BUG_ON(j >= multi->num_stripes);
2815 kfree(multi);
2816 }
2817
2818 *logical = buf;
2819 *naddrs = nr;
2820 *stripe_len = map->stripe_len;
2821
2822 free_extent_map(em);
2823 return 0;
2824}
2825
Chris Masonf2d8d742008-04-21 10:03:05 -04002826int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2827 u64 logical, struct page *page)
2828{
2829 u64 length = PAGE_CACHE_SIZE;
2830 return __btrfs_map_block(map_tree, READ, logical, &length,
2831 NULL, 0, page);
2832}
2833
Chris Mason8790d502008-04-03 16:29:03 -04002834static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002835{
Chris Masoncea9e442008-04-09 16:28:12 -04002836 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002837 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002838
Chris Mason8790d502008-04-03 16:29:03 -04002839 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002840 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002841
Chris Mason7d2b4da2008-08-05 10:13:57 -04002842 if (bio == multi->orig_bio)
2843 is_orig_bio = 1;
2844
Chris Masoncea9e442008-04-09 16:28:12 -04002845 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002846 if (!is_orig_bio) {
2847 bio_put(bio);
2848 bio = multi->orig_bio;
2849 }
Chris Mason8790d502008-04-03 16:29:03 -04002850 bio->bi_private = multi->private;
2851 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002852 /* only send an error to the higher layers if it is
2853 * beyond the tolerance of the multi-bio
2854 */
Chris Mason1259ab72008-05-12 13:39:03 -04002855 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002856 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002857 } else if (err) {
2858 /*
2859 * this bio is actually up to date, we didn't
2860 * go over the max number of errors
2861 */
2862 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002863 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002864 }
Chris Mason8790d502008-04-03 16:29:03 -04002865 kfree(multi);
2866
2867 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002868 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002869 bio_put(bio);
2870 }
Chris Mason8790d502008-04-03 16:29:03 -04002871}
2872
Chris Mason8b712842008-06-11 16:50:36 -04002873struct async_sched {
2874 struct bio *bio;
2875 int rw;
2876 struct btrfs_fs_info *info;
2877 struct btrfs_work work;
2878};
2879
2880/*
2881 * see run_scheduled_bios for a description of why bios are collected for
2882 * async submit.
2883 *
2884 * This will add one bio to the pending list for a device and make sure
2885 * the work struct is scheduled.
2886 */
Chris Masond3977122009-01-05 21:25:51 -05002887static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002888 struct btrfs_device *device,
2889 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002890{
2891 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04002892 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002893
2894 /* don't bother with additional async steps for reads, right now */
2895 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6d2008-07-31 16:29:02 -04002896 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002897 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04002898 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002899 return 0;
2900 }
2901
2902 /*
Chris Mason0986fe92008-08-15 15:34:15 -04002903 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002904 * higher layers. Otherwise, the async bio makes it appear we have
2905 * made progress against dirty pages when we've really just put it
2906 * on a queue for later
2907 */
Chris Mason0986fe92008-08-15 15:34:15 -04002908 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04002909 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002910 bio->bi_next = NULL;
2911 bio->bi_rw |= rw;
2912
2913 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -04002914 if (bio_sync(bio))
2915 pending_bios = &device->pending_sync_bios;
2916 else
2917 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002918
Chris Masonffbd5172009-04-20 15:50:09 -04002919 if (pending_bios->tail)
2920 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002921
Chris Masonffbd5172009-04-20 15:50:09 -04002922 pending_bios->tail = bio;
2923 if (!pending_bios->head)
2924 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002925 if (device->running_pending)
2926 should_queue = 0;
2927
2928 spin_unlock(&device->io_lock);
2929
2930 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002931 btrfs_queue_worker(&root->fs_info->submit_workers,
2932 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002933 return 0;
2934}
2935
Chris Masonf1885912008-04-09 16:28:12 -04002936int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002937 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002938{
2939 struct btrfs_mapping_tree *map_tree;
2940 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002941 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002942 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002943 u64 length = 0;
2944 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002945 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002946 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002947 int dev_nr = 0;
2948 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002949
Chris Masonf2d8d742008-04-21 10:03:05 -04002950 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002951 map_tree = &root->fs_info->mapping_tree;
2952 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002953
Chris Masonf1885912008-04-09 16:28:12 -04002954 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2955 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002956 BUG_ON(ret);
2957
2958 total_devs = multi->num_stripes;
2959 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05002960 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2961 "len %llu\n", (unsigned long long)logical,
2962 (unsigned long long)length,
2963 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04002964 BUG();
2965 }
2966 multi->end_io = first_bio->bi_end_io;
2967 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002968 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002969 atomic_set(&multi->stripes_pending, multi->num_stripes);
2970
Chris Masond3977122009-01-05 21:25:51 -05002971 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002972 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002973 if (dev_nr < total_devs - 1) {
2974 bio = bio_clone(first_bio, GFP_NOFS);
2975 BUG_ON(!bio);
2976 } else {
2977 bio = first_bio;
2978 }
2979 bio->bi_private = multi;
2980 bio->bi_end_io = end_bio_multi_stripe;
2981 }
Chris Masoncea9e442008-04-09 16:28:12 -04002982 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2983 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002984 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002985 if (dev && dev->bdev) {
2986 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002987 if (async_submit)
2988 schedule_bio(root, dev, rw, bio);
2989 else
2990 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002991 } else {
2992 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2993 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002994 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002995 }
Chris Mason8790d502008-04-03 16:29:03 -04002996 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002997 }
Chris Masoncea9e442008-04-09 16:28:12 -04002998 if (total_devs == 1)
2999 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003000 return 0;
3001}
3002
Chris Masona4437552008-04-18 10:29:38 -04003003struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003004 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003005{
Yan Zheng2b820322008-11-17 21:11:30 -05003006 struct btrfs_device *device;
3007 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003008
Yan Zheng2b820322008-11-17 21:11:30 -05003009 cur_devices = root->fs_info->fs_devices;
3010 while (cur_devices) {
3011 if (!fsid ||
3012 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3013 device = __find_device(&cur_devices->devices,
3014 devid, uuid);
3015 if (device)
3016 return device;
3017 }
3018 cur_devices = cur_devices->seed;
3019 }
3020 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003021}
3022
Chris Masondfe25022008-05-13 13:46:40 -04003023static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3024 u64 devid, u8 *dev_uuid)
3025{
3026 struct btrfs_device *device;
3027 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3028
3029 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003030 if (!device)
3031 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003032 list_add(&device->dev_list,
3033 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003034 device->barriers = 1;
3035 device->dev_root = root->fs_info->dev_root;
3036 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003037 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003038 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04003039 fs_devices->num_devices++;
3040 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003041 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003042 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3043 return device;
3044}
3045
Chris Mason0b86a832008-03-24 15:01:56 -04003046static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3047 struct extent_buffer *leaf,
3048 struct btrfs_chunk *chunk)
3049{
3050 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3051 struct map_lookup *map;
3052 struct extent_map *em;
3053 u64 logical;
3054 u64 length;
3055 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003056 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003057 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003058 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003059 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003060
Chris Masone17cade2008-04-15 15:41:47 -04003061 logical = key->offset;
3062 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003063
Chris Mason0b86a832008-03-24 15:01:56 -04003064 spin_lock(&map_tree->map_tree.lock);
3065 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04003066 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003067
3068 /* already mapped? */
3069 if (em && em->start <= logical && em->start + em->len > logical) {
3070 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003071 return 0;
3072 } else if (em) {
3073 free_extent_map(em);
3074 }
Chris Mason0b86a832008-03-24 15:01:56 -04003075
Chris Mason0b86a832008-03-24 15:01:56 -04003076 em = alloc_extent_map(GFP_NOFS);
3077 if (!em)
3078 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003079 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3080 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003081 if (!map) {
3082 free_extent_map(em);
3083 return -ENOMEM;
3084 }
3085
3086 em->bdev = (struct block_device *)map;
3087 em->start = logical;
3088 em->len = length;
3089 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003090 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003091
Chris Mason593060d2008-03-25 16:50:33 -04003092 map->num_stripes = num_stripes;
3093 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3094 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3095 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3096 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3097 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003098 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003099 for (i = 0; i < num_stripes; i++) {
3100 map->stripes[i].physical =
3101 btrfs_stripe_offset_nr(leaf, chunk, i);
3102 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003103 read_extent_buffer(leaf, uuid, (unsigned long)
3104 btrfs_stripe_dev_uuid_nr(chunk, i),
3105 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003106 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3107 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003108 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003109 kfree(map);
3110 free_extent_map(em);
3111 return -EIO;
3112 }
Chris Masondfe25022008-05-13 13:46:40 -04003113 if (!map->stripes[i].dev) {
3114 map->stripes[i].dev =
3115 add_missing_dev(root, devid, uuid);
3116 if (!map->stripes[i].dev) {
3117 kfree(map);
3118 free_extent_map(em);
3119 return -EIO;
3120 }
3121 }
3122 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003123 }
3124
3125 spin_lock(&map_tree->map_tree.lock);
3126 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04003127 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003128 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003129 free_extent_map(em);
3130
3131 return 0;
3132}
3133
3134static int fill_device_from_item(struct extent_buffer *leaf,
3135 struct btrfs_dev_item *dev_item,
3136 struct btrfs_device *device)
3137{
3138 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003139
3140 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003141 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3142 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003143 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3144 device->type = btrfs_device_type(leaf, dev_item);
3145 device->io_align = btrfs_device_io_align(leaf, dev_item);
3146 device->io_width = btrfs_device_io_width(leaf, dev_item);
3147 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003148
3149 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003150 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003151
Chris Mason0b86a832008-03-24 15:01:56 -04003152 return 0;
3153}
3154
Yan Zheng2b820322008-11-17 21:11:30 -05003155static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3156{
3157 struct btrfs_fs_devices *fs_devices;
3158 int ret;
3159
3160 mutex_lock(&uuid_mutex);
3161
3162 fs_devices = root->fs_info->fs_devices->seed;
3163 while (fs_devices) {
3164 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3165 ret = 0;
3166 goto out;
3167 }
3168 fs_devices = fs_devices->seed;
3169 }
3170
3171 fs_devices = find_fsid(fsid);
3172 if (!fs_devices) {
3173 ret = -ENOENT;
3174 goto out;
3175 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003176
3177 fs_devices = clone_fs_devices(fs_devices);
3178 if (IS_ERR(fs_devices)) {
3179 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003180 goto out;
3181 }
3182
Christoph Hellwig97288f22008-12-02 06:36:09 -05003183 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003184 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003185 if (ret)
3186 goto out;
3187
3188 if (!fs_devices->seeding) {
3189 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003190 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003191 ret = -EINVAL;
3192 goto out;
3193 }
3194
3195 fs_devices->seed = root->fs_info->fs_devices->seed;
3196 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003197out:
3198 mutex_unlock(&uuid_mutex);
3199 return ret;
3200}
3201
Chris Mason0d81ba52008-03-24 15:02:07 -04003202static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003203 struct extent_buffer *leaf,
3204 struct btrfs_dev_item *dev_item)
3205{
3206 struct btrfs_device *device;
3207 u64 devid;
3208 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003209 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003210 u8 dev_uuid[BTRFS_UUID_SIZE];
3211
Chris Mason0b86a832008-03-24 15:01:56 -04003212 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003213 read_extent_buffer(leaf, dev_uuid,
3214 (unsigned long)btrfs_device_uuid(dev_item),
3215 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003216 read_extent_buffer(leaf, fs_uuid,
3217 (unsigned long)btrfs_device_fsid(dev_item),
3218 BTRFS_UUID_SIZE);
3219
3220 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3221 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003222 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003223 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003224 }
3225
3226 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3227 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003228 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003229 return -EIO;
3230
3231 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003232 printk(KERN_WARNING "warning devid %llu missing\n",
3233 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003234 device = add_missing_dev(root, devid, dev_uuid);
3235 if (!device)
3236 return -ENOMEM;
3237 }
3238 }
3239
3240 if (device->fs_devices != root->fs_info->fs_devices) {
3241 BUG_ON(device->writeable);
3242 if (device->generation !=
3243 btrfs_device_generation(leaf, dev_item))
3244 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003245 }
Chris Mason0b86a832008-03-24 15:01:56 -04003246
3247 fill_device_from_item(leaf, dev_item, device);
3248 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003249 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003250 if (device->writeable)
3251 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003252 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003253 return ret;
3254}
3255
Chris Mason0d81ba52008-03-24 15:02:07 -04003256int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3257{
3258 struct btrfs_dev_item *dev_item;
3259
3260 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3261 dev_item);
3262 return read_one_dev(root, buf, dev_item);
3263}
3264
Yan Zhenge4404d62008-12-12 10:03:26 -05003265int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003266{
3267 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003268 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003269 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003270 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003271 u8 *ptr;
3272 unsigned long sb_ptr;
3273 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003274 u32 num_stripes;
3275 u32 array_size;
3276 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003277 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003278 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003279
Yan Zhenge4404d62008-12-12 10:03:26 -05003280 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003281 BTRFS_SUPER_INFO_SIZE);
3282 if (!sb)
3283 return -ENOMEM;
3284 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003285 btrfs_set_buffer_lockdep_class(sb, 0);
3286
Chris Masona061fc82008-05-07 11:43:44 -04003287 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003288 array_size = btrfs_super_sys_array_size(super_copy);
3289
Chris Mason0b86a832008-03-24 15:01:56 -04003290 ptr = super_copy->sys_chunk_array;
3291 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3292 cur = 0;
3293
3294 while (cur < array_size) {
3295 disk_key = (struct btrfs_disk_key *)ptr;
3296 btrfs_disk_key_to_cpu(&key, disk_key);
3297
Chris Masona061fc82008-05-07 11:43:44 -04003298 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003299 sb_ptr += len;
3300 cur += len;
3301
Chris Mason0d81ba52008-03-24 15:02:07 -04003302 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003303 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003304 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003305 if (ret)
3306 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003307 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3308 len = btrfs_chunk_item_size(num_stripes);
3309 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003310 ret = -EIO;
3311 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003312 }
3313 ptr += len;
3314 sb_ptr += len;
3315 cur += len;
3316 }
Chris Masona061fc82008-05-07 11:43:44 -04003317 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003318 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003319}
3320
3321int btrfs_read_chunk_tree(struct btrfs_root *root)
3322{
3323 struct btrfs_path *path;
3324 struct extent_buffer *leaf;
3325 struct btrfs_key key;
3326 struct btrfs_key found_key;
3327 int ret;
3328 int slot;
3329
3330 root = root->fs_info->chunk_root;
3331
3332 path = btrfs_alloc_path();
3333 if (!path)
3334 return -ENOMEM;
3335
3336 /* first we search for all of the device items, and then we
3337 * read in all of the chunk items. This way we can create chunk
3338 * mappings that reference all of the devices that are afound
3339 */
3340 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3341 key.offset = 0;
3342 key.type = 0;
3343again:
3344 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masond3977122009-01-05 21:25:51 -05003345 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003346 leaf = path->nodes[0];
3347 slot = path->slots[0];
3348 if (slot >= btrfs_header_nritems(leaf)) {
3349 ret = btrfs_next_leaf(root, path);
3350 if (ret == 0)
3351 continue;
3352 if (ret < 0)
3353 goto error;
3354 break;
3355 }
3356 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3357 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3358 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3359 break;
3360 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3361 struct btrfs_dev_item *dev_item;
3362 dev_item = btrfs_item_ptr(leaf, slot,
3363 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003364 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003365 if (ret)
3366 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003367 }
3368 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3369 struct btrfs_chunk *chunk;
3370 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3371 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003372 if (ret)
3373 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003374 }
3375 path->slots[0]++;
3376 }
3377 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3378 key.objectid = 0;
3379 btrfs_release_path(root, path);
3380 goto again;
3381 }
Chris Mason0b86a832008-03-24 15:01:56 -04003382 ret = 0;
3383error:
Yan Zheng2b820322008-11-17 21:11:30 -05003384 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003385 return ret;
3386}