blob: 074c1c56d8c4a7c34a75147d63358250c0be6dbc [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 Mason492bb6de2008-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;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400761 if (ret > 0) {
762 ret = btrfs_previous_item(root, path, key.objectid, key.type);
763 if (ret < 0)
764 goto error;
765 if (ret > 0)
766 start_found = 1;
767 }
Chris Mason0b86a832008-03-24 15:01:56 -0400768 l = path->nodes[0];
769 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
770 while (1) {
771 l = path->nodes[0];
772 slot = path->slots[0];
773 if (slot >= btrfs_header_nritems(l)) {
774 ret = btrfs_next_leaf(root, path);
775 if (ret == 0)
776 continue;
777 if (ret < 0)
778 goto error;
779no_more_items:
780 if (!start_found) {
781 if (search_start >= search_end) {
782 ret = -ENOSPC;
783 goto error;
784 }
785 *start = search_start;
786 start_found = 1;
787 goto check_pending;
788 }
789 *start = last_byte > search_start ?
790 last_byte : search_start;
791 if (search_end <= *start) {
792 ret = -ENOSPC;
793 goto error;
794 }
795 goto check_pending;
796 }
797 btrfs_item_key_to_cpu(l, &key, slot);
798
799 if (key.objectid < device->devid)
800 goto next;
801
802 if (key.objectid > device->devid)
803 goto no_more_items;
804
805 if (key.offset >= search_start && key.offset > last_byte &&
806 start_found) {
807 if (last_byte < search_start)
808 last_byte = search_start;
809 hole_size = key.offset - last_byte;
810 if (key.offset > last_byte &&
811 hole_size >= num_bytes) {
812 *start = last_byte;
813 goto check_pending;
814 }
815 }
Chris Masond3977122009-01-05 21:25:51 -0500816 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400817 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400818
819 start_found = 1;
820 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
821 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
822next:
823 path->slots[0]++;
824 cond_resched();
825 }
826check_pending:
827 /* we have to make sure we didn't find an extent that has already
828 * been allocated by the map tree or the original allocation
829 */
Chris Mason0b86a832008-03-24 15:01:56 -0400830 BUG_ON(*start < search_start);
831
Chris Mason6324fbf2008-03-24 15:01:59 -0400832 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400833 ret = -ENOSPC;
834 goto error;
835 }
836 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500837 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400838
839error:
Yan Zheng2b820322008-11-17 21:11:30 -0500840 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400841 return ret;
842}
843
Christoph Hellwigb2950862008-12-02 09:54:17 -0500844static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400845 struct btrfs_device *device,
846 u64 start)
847{
848 int ret;
849 struct btrfs_path *path;
850 struct btrfs_root *root = device->dev_root;
851 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400852 struct btrfs_key found_key;
853 struct extent_buffer *leaf = NULL;
854 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400855
856 path = btrfs_alloc_path();
857 if (!path)
858 return -ENOMEM;
859
860 key.objectid = device->devid;
861 key.offset = start;
862 key.type = BTRFS_DEV_EXTENT_KEY;
863
864 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400865 if (ret > 0) {
866 ret = btrfs_previous_item(root, path, key.objectid,
867 BTRFS_DEV_EXTENT_KEY);
868 BUG_ON(ret);
869 leaf = path->nodes[0];
870 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
871 extent = btrfs_item_ptr(leaf, path->slots[0],
872 struct btrfs_dev_extent);
873 BUG_ON(found_key.offset > start || found_key.offset +
874 btrfs_dev_extent_length(leaf, extent) < start);
875 ret = 0;
876 } else if (ret == 0) {
877 leaf = path->nodes[0];
878 extent = btrfs_item_ptr(leaf, path->slots[0],
879 struct btrfs_dev_extent);
880 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400881 BUG_ON(ret);
882
Chris Masondfe25022008-05-13 13:46:40 -0400883 if (device->bytes_used > 0)
884 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400885 ret = btrfs_del_item(trans, root, path);
886 BUG_ON(ret);
887
888 btrfs_free_path(path);
889 return ret;
890}
891
Yan Zheng2b820322008-11-17 21:11:30 -0500892int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400893 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400894 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500895 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400896{
897 int ret;
898 struct btrfs_path *path;
899 struct btrfs_root *root = device->dev_root;
900 struct btrfs_dev_extent *extent;
901 struct extent_buffer *leaf;
902 struct btrfs_key key;
903
Chris Masondfe25022008-05-13 13:46:40 -0400904 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400905 path = btrfs_alloc_path();
906 if (!path)
907 return -ENOMEM;
908
Chris Mason0b86a832008-03-24 15:01:56 -0400909 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500910 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400911 key.type = BTRFS_DEV_EXTENT_KEY;
912 ret = btrfs_insert_empty_item(trans, root, path, &key,
913 sizeof(*extent));
914 BUG_ON(ret);
915
916 leaf = path->nodes[0];
917 extent = btrfs_item_ptr(leaf, path->slots[0],
918 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400919 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
920 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
921 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
922
923 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
924 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
925 BTRFS_UUID_SIZE);
926
Chris Mason0b86a832008-03-24 15:01:56 -0400927 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
928 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400929 btrfs_free_path(path);
930 return ret;
931}
932
Chris Masona1b32a52008-09-05 16:09:51 -0400933static noinline int find_next_chunk(struct btrfs_root *root,
934 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400935{
936 struct btrfs_path *path;
937 int ret;
938 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400939 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400940 struct btrfs_key found_key;
941
942 path = btrfs_alloc_path();
943 BUG_ON(!path);
944
Chris Masone17cade2008-04-15 15:41:47 -0400945 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400946 key.offset = (u64)-1;
947 key.type = BTRFS_CHUNK_ITEM_KEY;
948
949 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
950 if (ret < 0)
951 goto error;
952
953 BUG_ON(ret == 0);
954
955 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
956 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400957 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400958 } else {
959 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
960 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400961 if (found_key.objectid != objectid)
962 *offset = 0;
963 else {
964 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
965 struct btrfs_chunk);
966 *offset = found_key.offset +
967 btrfs_chunk_length(path->nodes[0], chunk);
968 }
Chris Mason0b86a832008-03-24 15:01:56 -0400969 }
970 ret = 0;
971error:
972 btrfs_free_path(path);
973 return ret;
974}
975
Yan Zheng2b820322008-11-17 21:11:30 -0500976static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400977{
978 int ret;
979 struct btrfs_key key;
980 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500981 struct btrfs_path *path;
982
983 root = root->fs_info->chunk_root;
984
985 path = btrfs_alloc_path();
986 if (!path)
987 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400988
989 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
990 key.type = BTRFS_DEV_ITEM_KEY;
991 key.offset = (u64)-1;
992
993 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
994 if (ret < 0)
995 goto error;
996
997 BUG_ON(ret == 0);
998
999 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1000 BTRFS_DEV_ITEM_KEY);
1001 if (ret) {
1002 *objectid = 1;
1003 } else {
1004 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1005 path->slots[0]);
1006 *objectid = found_key.offset + 1;
1007 }
1008 ret = 0;
1009error:
Yan Zheng2b820322008-11-17 21:11:30 -05001010 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001011 return ret;
1012}
1013
1014/*
1015 * the device information is stored in the chunk root
1016 * the btrfs_device struct should be fully filled in
1017 */
1018int btrfs_add_device(struct btrfs_trans_handle *trans,
1019 struct btrfs_root *root,
1020 struct btrfs_device *device)
1021{
1022 int ret;
1023 struct btrfs_path *path;
1024 struct btrfs_dev_item *dev_item;
1025 struct extent_buffer *leaf;
1026 struct btrfs_key key;
1027 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001028
1029 root = root->fs_info->chunk_root;
1030
1031 path = btrfs_alloc_path();
1032 if (!path)
1033 return -ENOMEM;
1034
Chris Mason0b86a832008-03-24 15:01:56 -04001035 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1036 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001037 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001038
1039 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001040 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001041 if (ret)
1042 goto out;
1043
1044 leaf = path->nodes[0];
1045 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1046
1047 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001048 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001049 btrfs_set_device_type(leaf, dev_item, device->type);
1050 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1051 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1052 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001053 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1054 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001055 btrfs_set_device_group(leaf, dev_item, 0);
1056 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1057 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001058 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001059
Chris Mason0b86a832008-03-24 15:01:56 -04001060 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001061 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001062 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1063 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001064 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001065
Yan Zheng2b820322008-11-17 21:11:30 -05001066 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001067out:
1068 btrfs_free_path(path);
1069 return ret;
1070}
Chris Mason8f18cf12008-04-25 16:53:30 -04001071
Chris Masona061fc82008-05-07 11:43:44 -04001072static int btrfs_rm_dev_item(struct btrfs_root *root,
1073 struct btrfs_device *device)
1074{
1075 int ret;
1076 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001077 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001078 struct btrfs_trans_handle *trans;
1079
1080 root = root->fs_info->chunk_root;
1081
1082 path = btrfs_alloc_path();
1083 if (!path)
1084 return -ENOMEM;
1085
1086 trans = btrfs_start_transaction(root, 1);
1087 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1088 key.type = BTRFS_DEV_ITEM_KEY;
1089 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001090 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001091
1092 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1093 if (ret < 0)
1094 goto out;
1095
1096 if (ret > 0) {
1097 ret = -ENOENT;
1098 goto out;
1099 }
1100
1101 ret = btrfs_del_item(trans, root, path);
1102 if (ret)
1103 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001104out:
1105 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001106 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001107 btrfs_commit_transaction(trans, root);
1108 return ret;
1109}
1110
1111int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1112{
1113 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001114 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001115 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001116 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001117 struct btrfs_super_block *disk_super;
1118 u64 all_avail;
1119 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001120 u64 num_devices;
1121 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001122 int ret = 0;
1123
Chris Masona061fc82008-05-07 11:43:44 -04001124 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001125 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001126
1127 all_avail = root->fs_info->avail_data_alloc_bits |
1128 root->fs_info->avail_system_alloc_bits |
1129 root->fs_info->avail_metadata_alloc_bits;
1130
1131 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001132 root->fs_info->fs_devices->rw_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001133 printk(KERN_ERR "btrfs: unable to go below four devices "
1134 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001135 ret = -EINVAL;
1136 goto out;
1137 }
1138
1139 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Yan Zheng2b820322008-11-17 21:11:30 -05001140 root->fs_info->fs_devices->rw_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001141 printk(KERN_ERR "btrfs: unable to go below two "
1142 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001143 ret = -EINVAL;
1144 goto out;
1145 }
1146
Chris Masondfe25022008-05-13 13:46:40 -04001147 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001148 struct list_head *devices;
1149 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001150
Chris Masondfe25022008-05-13 13:46:40 -04001151 device = NULL;
1152 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001153 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001154 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001155 if (tmp->in_fs_metadata && !tmp->bdev) {
1156 device = tmp;
1157 break;
1158 }
1159 }
Chris Masone5e9a522009-06-10 15:17:02 -04001160 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001161 bdev = NULL;
1162 bh = NULL;
1163 disk_super = NULL;
1164 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001165 printk(KERN_ERR "btrfs: no missing devices found to "
1166 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001167 goto out;
1168 }
Chris Masondfe25022008-05-13 13:46:40 -04001169 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001170 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001171 root->fs_info->bdev_holder);
1172 if (IS_ERR(bdev)) {
1173 ret = PTR_ERR(bdev);
1174 goto out;
1175 }
1176
Yan Zheng2b820322008-11-17 21:11:30 -05001177 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001178 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001179 if (!bh) {
1180 ret = -EIO;
1181 goto error_close;
1182 }
1183 disk_super = (struct btrfs_super_block *)bh->b_data;
Chris Masondfe25022008-05-13 13:46:40 -04001184 devid = le64_to_cpu(disk_super->dev_item.devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001185 dev_uuid = disk_super->dev_item.uuid;
1186 device = btrfs_find_device(root, devid, dev_uuid,
1187 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001188 if (!device) {
1189 ret = -ENOENT;
1190 goto error_brelse;
1191 }
Chris Masondfe25022008-05-13 13:46:40 -04001192 }
Yan Zheng2b820322008-11-17 21:11:30 -05001193
1194 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001195 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1196 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001197 ret = -EINVAL;
1198 goto error_brelse;
1199 }
1200
1201 if (device->writeable) {
1202 list_del_init(&device->dev_alloc_list);
1203 root->fs_info->fs_devices->rw_devices--;
1204 }
Chris Masona061fc82008-05-07 11:43:44 -04001205
1206 ret = btrfs_shrink_device(device, 0);
1207 if (ret)
1208 goto error_brelse;
1209
Chris Masona061fc82008-05-07 11:43:44 -04001210 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1211 if (ret)
1212 goto error_brelse;
1213
Yan Zheng2b820322008-11-17 21:11:30 -05001214 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001215
1216 /*
1217 * the device list mutex makes sure that we don't change
1218 * the device list while someone else is writing out all
1219 * the device supers.
1220 */
1221 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001222 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001223 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1224
Yan Zhenge4404d62008-12-12 10:03:26 -05001225 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001226
1227 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1228 struct btrfs_device, dev_list);
1229 if (device->bdev == root->fs_info->sb->s_bdev)
1230 root->fs_info->sb->s_bdev = next_device->bdev;
1231 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1232 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1233
Yan Zhenge4404d62008-12-12 10:03:26 -05001234 if (device->bdev) {
1235 close_bdev_exclusive(device->bdev, device->mode);
1236 device->bdev = NULL;
1237 device->fs_devices->open_devices--;
1238 }
1239
Yan Zheng2b820322008-11-17 21:11:30 -05001240 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1241 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1242
Yan Zhenge4404d62008-12-12 10:03:26 -05001243 if (device->fs_devices->open_devices == 0) {
1244 struct btrfs_fs_devices *fs_devices;
1245 fs_devices = root->fs_info->fs_devices;
1246 while (fs_devices) {
1247 if (fs_devices->seed == device->fs_devices)
1248 break;
1249 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001250 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001251 fs_devices->seed = device->fs_devices->seed;
1252 device->fs_devices->seed = NULL;
1253 __btrfs_close_devices(device->fs_devices);
1254 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001255 }
1256
1257 /*
1258 * at this point, the device is zero sized. We want to
1259 * remove it from the devices list and zero out the old super
1260 */
1261 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001262 /* make sure this device isn't detected as part of
1263 * the FS anymore
1264 */
1265 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1266 set_buffer_dirty(bh);
1267 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001268 }
Chris Masona061fc82008-05-07 11:43:44 -04001269
Chris Masona061fc82008-05-07 11:43:44 -04001270 kfree(device->name);
1271 kfree(device);
1272 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001273
1274error_brelse:
1275 brelse(bh);
1276error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001277 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001278 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001279out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001280 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001281 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001282 return ret;
1283}
1284
Yan Zheng2b820322008-11-17 21:11:30 -05001285/*
1286 * does all the dirty work required for changing file system's UUID.
1287 */
1288static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1289 struct btrfs_root *root)
1290{
1291 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1292 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001293 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001294 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1295 struct btrfs_device *device;
1296 u64 super_flags;
1297
1298 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001299 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001300 return -EINVAL;
1301
Yan Zhenge4404d62008-12-12 10:03:26 -05001302 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1303 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001304 return -ENOMEM;
1305
Yan Zhenge4404d62008-12-12 10:03:26 -05001306 old_devices = clone_fs_devices(fs_devices);
1307 if (IS_ERR(old_devices)) {
1308 kfree(seed_devices);
1309 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001310 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001311
Yan Zheng2b820322008-11-17 21:11:30 -05001312 list_add(&old_devices->list, &fs_uuids);
1313
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1315 seed_devices->opened = 1;
1316 INIT_LIST_HEAD(&seed_devices->devices);
1317 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001318 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001319 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1320 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1321 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1322 device->fs_devices = seed_devices;
1323 }
1324
Yan Zheng2b820322008-11-17 21:11:30 -05001325 fs_devices->seeding = 0;
1326 fs_devices->num_devices = 0;
1327 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001328 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001329
1330 generate_random_uuid(fs_devices->fsid);
1331 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1332 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1333 super_flags = btrfs_super_flags(disk_super) &
1334 ~BTRFS_SUPER_FLAG_SEEDING;
1335 btrfs_set_super_flags(disk_super, super_flags);
1336
1337 return 0;
1338}
1339
1340/*
1341 * strore the expected generation for seed devices in device items.
1342 */
1343static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root)
1345{
1346 struct btrfs_path *path;
1347 struct extent_buffer *leaf;
1348 struct btrfs_dev_item *dev_item;
1349 struct btrfs_device *device;
1350 struct btrfs_key key;
1351 u8 fs_uuid[BTRFS_UUID_SIZE];
1352 u8 dev_uuid[BTRFS_UUID_SIZE];
1353 u64 devid;
1354 int ret;
1355
1356 path = btrfs_alloc_path();
1357 if (!path)
1358 return -ENOMEM;
1359
1360 root = root->fs_info->chunk_root;
1361 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1362 key.offset = 0;
1363 key.type = BTRFS_DEV_ITEM_KEY;
1364
1365 while (1) {
1366 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1367 if (ret < 0)
1368 goto error;
1369
1370 leaf = path->nodes[0];
1371next_slot:
1372 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1373 ret = btrfs_next_leaf(root, path);
1374 if (ret > 0)
1375 break;
1376 if (ret < 0)
1377 goto error;
1378 leaf = path->nodes[0];
1379 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1380 btrfs_release_path(root, path);
1381 continue;
1382 }
1383
1384 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1385 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1386 key.type != BTRFS_DEV_ITEM_KEY)
1387 break;
1388
1389 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1390 struct btrfs_dev_item);
1391 devid = btrfs_device_id(leaf, dev_item);
1392 read_extent_buffer(leaf, dev_uuid,
1393 (unsigned long)btrfs_device_uuid(dev_item),
1394 BTRFS_UUID_SIZE);
1395 read_extent_buffer(leaf, fs_uuid,
1396 (unsigned long)btrfs_device_fsid(dev_item),
1397 BTRFS_UUID_SIZE);
1398 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1399 BUG_ON(!device);
1400
1401 if (device->fs_devices->seeding) {
1402 btrfs_set_device_generation(leaf, dev_item,
1403 device->generation);
1404 btrfs_mark_buffer_dirty(leaf);
1405 }
1406
1407 path->slots[0]++;
1408 goto next_slot;
1409 }
1410 ret = 0;
1411error:
1412 btrfs_free_path(path);
1413 return ret;
1414}
1415
Chris Mason788f20e2008-04-28 15:29:42 -04001416int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1417{
1418 struct btrfs_trans_handle *trans;
1419 struct btrfs_device *device;
1420 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001421 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001422 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001423 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001424 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001425 int ret = 0;
1426
Yan Zheng2b820322008-11-17 21:11:30 -05001427 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1428 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001429
Chris Mason15916de2008-11-19 21:17:22 -05001430 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Chris Masond3977122009-01-05 21:25:51 -05001431 if (!bdev)
Chris Mason788f20e2008-04-28 15:29:42 -04001432 return -EIO;
Chris Masona2135012008-06-25 16:01:30 -04001433
Yan Zheng2b820322008-11-17 21:11:30 -05001434 if (root->fs_info->fs_devices->seeding) {
1435 seeding_dev = 1;
1436 down_write(&sb->s_umount);
1437 mutex_lock(&uuid_mutex);
1438 }
1439
Chris Mason8c8bee12008-09-29 11:19:10 -04001440 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001441 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001442
Chris Mason788f20e2008-04-28 15:29:42 -04001443 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001444 /*
1445 * we have the volume lock, so we don't need the extra
1446 * device list mutex while reading the list here.
1447 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001448 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001449 if (device->bdev == bdev) {
1450 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001451 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001452 }
1453 }
1454
1455 device = kzalloc(sizeof(*device), GFP_NOFS);
1456 if (!device) {
1457 /* we can safely leave the fs_devices entry around */
1458 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001459 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001460 }
1461
Chris Mason788f20e2008-04-28 15:29:42 -04001462 device->name = kstrdup(device_path, GFP_NOFS);
1463 if (!device->name) {
1464 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001465 ret = -ENOMEM;
1466 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001467 }
Yan Zheng2b820322008-11-17 21:11:30 -05001468
1469 ret = find_next_devid(root, &device->devid);
1470 if (ret) {
1471 kfree(device);
1472 goto error;
1473 }
1474
1475 trans = btrfs_start_transaction(root, 1);
1476 lock_chunks(root);
1477
1478 device->barriers = 1;
1479 device->writeable = 1;
1480 device->work.func = pending_bios_fn;
1481 generate_random_uuid(device->uuid);
1482 spin_lock_init(&device->io_lock);
1483 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001484 device->io_width = root->sectorsize;
1485 device->io_align = root->sectorsize;
1486 device->sector_size = root->sectorsize;
1487 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001488 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001489 device->dev_root = root->fs_info->dev_root;
1490 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001491 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001492 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001493 set_blocksize(device->bdev, 4096);
1494
Yan Zheng2b820322008-11-17 21:11:30 -05001495 if (seeding_dev) {
1496 sb->s_flags &= ~MS_RDONLY;
1497 ret = btrfs_prepare_sprout(trans, root);
1498 BUG_ON(ret);
1499 }
1500
1501 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001502
1503 /*
1504 * we don't want write_supers to jump in here with our device
1505 * half setup
1506 */
1507 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001508 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1509 list_add(&device->dev_alloc_list,
1510 &root->fs_info->fs_devices->alloc_list);
1511 root->fs_info->fs_devices->num_devices++;
1512 root->fs_info->fs_devices->open_devices++;
1513 root->fs_info->fs_devices->rw_devices++;
1514 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1515
Chris Masonc2898112009-06-10 09:51:32 -04001516 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1517 root->fs_info->fs_devices->rotating = 1;
1518
Chris Mason788f20e2008-04-28 15:29:42 -04001519 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1520 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1521 total_bytes + device->total_bytes);
1522
1523 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1524 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1525 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001526 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001527
Yan Zheng2b820322008-11-17 21:11:30 -05001528 if (seeding_dev) {
1529 ret = init_first_rw_device(trans, root, device);
1530 BUG_ON(ret);
1531 ret = btrfs_finish_sprout(trans, root);
1532 BUG_ON(ret);
1533 } else {
1534 ret = btrfs_add_device(trans, root, device);
1535 }
1536
Chris Mason913d9522009-03-10 13:17:18 -04001537 /*
1538 * we've got more storage, clear any full flags on the space
1539 * infos
1540 */
1541 btrfs_clear_space_info_full(root->fs_info);
1542
Chris Mason7d9eb122008-07-08 14:19:17 -04001543 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001544 btrfs_commit_transaction(trans, root);
1545
1546 if (seeding_dev) {
1547 mutex_unlock(&uuid_mutex);
1548 up_write(&sb->s_umount);
1549
1550 ret = btrfs_relocate_sys_chunks(root);
1551 BUG_ON(ret);
1552 }
1553out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001554 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001555 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001556error:
Chris Mason15916de2008-11-19 21:17:22 -05001557 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001558 if (seeding_dev) {
1559 mutex_unlock(&uuid_mutex);
1560 up_write(&sb->s_umount);
1561 }
Chris Mason788f20e2008-04-28 15:29:42 -04001562 goto out;
1563}
1564
Chris Masond3977122009-01-05 21:25:51 -05001565static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1566 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001567{
1568 int ret;
1569 struct btrfs_path *path;
1570 struct btrfs_root *root;
1571 struct btrfs_dev_item *dev_item;
1572 struct extent_buffer *leaf;
1573 struct btrfs_key key;
1574
1575 root = device->dev_root->fs_info->chunk_root;
1576
1577 path = btrfs_alloc_path();
1578 if (!path)
1579 return -ENOMEM;
1580
1581 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1582 key.type = BTRFS_DEV_ITEM_KEY;
1583 key.offset = device->devid;
1584
1585 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1586 if (ret < 0)
1587 goto out;
1588
1589 if (ret > 0) {
1590 ret = -ENOENT;
1591 goto out;
1592 }
1593
1594 leaf = path->nodes[0];
1595 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1596
1597 btrfs_set_device_id(leaf, dev_item, device->devid);
1598 btrfs_set_device_type(leaf, dev_item, device->type);
1599 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1600 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1601 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001602 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001603 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1604 btrfs_mark_buffer_dirty(leaf);
1605
1606out:
1607 btrfs_free_path(path);
1608 return ret;
1609}
1610
Chris Mason7d9eb122008-07-08 14:19:17 -04001611static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001612 struct btrfs_device *device, u64 new_size)
1613{
1614 struct btrfs_super_block *super_copy =
1615 &device->dev_root->fs_info->super_copy;
1616 u64 old_total = btrfs_super_total_bytes(super_copy);
1617 u64 diff = new_size - device->total_bytes;
1618
Yan Zheng2b820322008-11-17 21:11:30 -05001619 if (!device->writeable)
1620 return -EACCES;
1621 if (new_size <= device->total_bytes)
1622 return -EINVAL;
1623
Chris Mason8f18cf12008-04-25 16:53:30 -04001624 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001625 device->fs_devices->total_rw_bytes += diff;
1626
1627 device->total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001628 btrfs_clear_space_info_full(device->dev_root->fs_info);
1629
Chris Mason8f18cf12008-04-25 16:53:30 -04001630 return btrfs_update_device(trans, device);
1631}
1632
Chris Mason7d9eb122008-07-08 14:19:17 -04001633int btrfs_grow_device(struct btrfs_trans_handle *trans,
1634 struct btrfs_device *device, u64 new_size)
1635{
1636 int ret;
1637 lock_chunks(device->dev_root);
1638 ret = __btrfs_grow_device(trans, device, new_size);
1639 unlock_chunks(device->dev_root);
1640 return ret;
1641}
1642
Chris Mason8f18cf12008-04-25 16:53:30 -04001643static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1644 struct btrfs_root *root,
1645 u64 chunk_tree, u64 chunk_objectid,
1646 u64 chunk_offset)
1647{
1648 int ret;
1649 struct btrfs_path *path;
1650 struct btrfs_key key;
1651
1652 root = root->fs_info->chunk_root;
1653 path = btrfs_alloc_path();
1654 if (!path)
1655 return -ENOMEM;
1656
1657 key.objectid = chunk_objectid;
1658 key.offset = chunk_offset;
1659 key.type = BTRFS_CHUNK_ITEM_KEY;
1660
1661 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1662 BUG_ON(ret);
1663
1664 ret = btrfs_del_item(trans, root, path);
1665 BUG_ON(ret);
1666
1667 btrfs_free_path(path);
1668 return 0;
1669}
1670
Christoph Hellwigb2950862008-12-02 09:54:17 -05001671static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001672 chunk_offset)
1673{
1674 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1675 struct btrfs_disk_key *disk_key;
1676 struct btrfs_chunk *chunk;
1677 u8 *ptr;
1678 int ret = 0;
1679 u32 num_stripes;
1680 u32 array_size;
1681 u32 len = 0;
1682 u32 cur;
1683 struct btrfs_key key;
1684
1685 array_size = btrfs_super_sys_array_size(super_copy);
1686
1687 ptr = super_copy->sys_chunk_array;
1688 cur = 0;
1689
1690 while (cur < array_size) {
1691 disk_key = (struct btrfs_disk_key *)ptr;
1692 btrfs_disk_key_to_cpu(&key, disk_key);
1693
1694 len = sizeof(*disk_key);
1695
1696 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1697 chunk = (struct btrfs_chunk *)(ptr + len);
1698 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1699 len += btrfs_chunk_item_size(num_stripes);
1700 } else {
1701 ret = -EIO;
1702 break;
1703 }
1704 if (key.objectid == chunk_objectid &&
1705 key.offset == chunk_offset) {
1706 memmove(ptr, ptr + len, array_size - (cur + len));
1707 array_size -= len;
1708 btrfs_set_super_sys_array_size(super_copy, array_size);
1709 } else {
1710 ptr += len;
1711 cur += len;
1712 }
1713 }
1714 return ret;
1715}
1716
Christoph Hellwigb2950862008-12-02 09:54:17 -05001717static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001718 u64 chunk_tree, u64 chunk_objectid,
1719 u64 chunk_offset)
1720{
1721 struct extent_map_tree *em_tree;
1722 struct btrfs_root *extent_root;
1723 struct btrfs_trans_handle *trans;
1724 struct extent_map *em;
1725 struct map_lookup *map;
1726 int ret;
1727 int i;
1728
1729 root = root->fs_info->chunk_root;
1730 extent_root = root->fs_info->extent_root;
1731 em_tree = &root->fs_info->mapping_tree.map_tree;
1732
1733 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001734 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001735 BUG_ON(ret);
1736
1737 trans = btrfs_start_transaction(root, 1);
1738 BUG_ON(!trans);
1739
Chris Mason7d9eb122008-07-08 14:19:17 -04001740 lock_chunks(root);
1741
Chris Mason8f18cf12008-04-25 16:53:30 -04001742 /*
1743 * step two, delete the device extents and the
1744 * chunk tree entries
1745 */
1746 spin_lock(&em_tree->lock);
1747 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1748 spin_unlock(&em_tree->lock);
1749
Chris Masona061fc82008-05-07 11:43:44 -04001750 BUG_ON(em->start > chunk_offset ||
1751 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001752 map = (struct map_lookup *)em->bdev;
1753
1754 for (i = 0; i < map->num_stripes; i++) {
1755 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1756 map->stripes[i].physical);
1757 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001758
Chris Masondfe25022008-05-13 13:46:40 -04001759 if (map->stripes[i].dev) {
1760 ret = btrfs_update_device(trans, map->stripes[i].dev);
1761 BUG_ON(ret);
1762 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001763 }
1764 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1765 chunk_offset);
1766
1767 BUG_ON(ret);
1768
1769 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1770 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1771 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001772 }
1773
Zheng Yan1a40e232008-09-26 10:09:34 -04001774 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1775 BUG_ON(ret);
1776
Chris Mason8f18cf12008-04-25 16:53:30 -04001777 spin_lock(&em_tree->lock);
1778 remove_extent_mapping(em_tree, em);
Zheng Yan1a40e232008-09-26 10:09:34 -04001779 spin_unlock(&em_tree->lock);
1780
Chris Mason8f18cf12008-04-25 16:53:30 -04001781 kfree(map);
1782 em->bdev = NULL;
1783
1784 /* once for the tree */
1785 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001786 /* once for us */
1787 free_extent_map(em);
1788
Chris Mason7d9eb122008-07-08 14:19:17 -04001789 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001790 btrfs_end_transaction(trans, root);
1791 return 0;
1792}
1793
Yan Zheng2b820322008-11-17 21:11:30 -05001794static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1795{
1796 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1797 struct btrfs_path *path;
1798 struct extent_buffer *leaf;
1799 struct btrfs_chunk *chunk;
1800 struct btrfs_key key;
1801 struct btrfs_key found_key;
1802 u64 chunk_tree = chunk_root->root_key.objectid;
1803 u64 chunk_type;
1804 int ret;
1805
1806 path = btrfs_alloc_path();
1807 if (!path)
1808 return -ENOMEM;
1809
1810 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1811 key.offset = (u64)-1;
1812 key.type = BTRFS_CHUNK_ITEM_KEY;
1813
1814 while (1) {
1815 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1816 if (ret < 0)
1817 goto error;
1818 BUG_ON(ret == 0);
1819
1820 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1821 key.type);
1822 if (ret < 0)
1823 goto error;
1824 if (ret > 0)
1825 break;
1826
1827 leaf = path->nodes[0];
1828 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1829
1830 chunk = btrfs_item_ptr(leaf, path->slots[0],
1831 struct btrfs_chunk);
1832 chunk_type = btrfs_chunk_type(leaf, chunk);
1833 btrfs_release_path(chunk_root, path);
1834
1835 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1836 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1837 found_key.objectid,
1838 found_key.offset);
1839 BUG_ON(ret);
1840 }
1841
1842 if (found_key.offset == 0)
1843 break;
1844 key.offset = found_key.offset - 1;
1845 }
1846 ret = 0;
1847error:
1848 btrfs_free_path(path);
1849 return ret;
1850}
1851
Chris Masonec44a352008-04-28 15:29:52 -04001852static u64 div_factor(u64 num, int factor)
1853{
1854 if (factor == 10)
1855 return num;
1856 num *= factor;
1857 do_div(num, 10);
1858 return num;
1859}
1860
Chris Masonec44a352008-04-28 15:29:52 -04001861int btrfs_balance(struct btrfs_root *dev_root)
1862{
1863 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001864 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1865 struct btrfs_device *device;
1866 u64 old_size;
1867 u64 size_to_free;
1868 struct btrfs_path *path;
1869 struct btrfs_key key;
1870 struct btrfs_chunk *chunk;
1871 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1872 struct btrfs_trans_handle *trans;
1873 struct btrfs_key found_key;
1874
Yan Zheng2b820322008-11-17 21:11:30 -05001875 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1876 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001877
Chris Mason7d9eb122008-07-08 14:19:17 -04001878 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001879 dev_root = dev_root->fs_info->dev_root;
1880
Chris Masonec44a352008-04-28 15:29:52 -04001881 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001882 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001883 old_size = device->total_bytes;
1884 size_to_free = div_factor(old_size, 1);
1885 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001886 if (!device->writeable ||
1887 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001888 continue;
1889
1890 ret = btrfs_shrink_device(device, old_size - size_to_free);
1891 BUG_ON(ret);
1892
1893 trans = btrfs_start_transaction(dev_root, 1);
1894 BUG_ON(!trans);
1895
1896 ret = btrfs_grow_device(trans, device, old_size);
1897 BUG_ON(ret);
1898
1899 btrfs_end_transaction(trans, dev_root);
1900 }
1901
1902 /* step two, relocate all the chunks */
1903 path = btrfs_alloc_path();
1904 BUG_ON(!path);
1905
1906 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1907 key.offset = (u64)-1;
1908 key.type = BTRFS_CHUNK_ITEM_KEY;
1909
Chris Masond3977122009-01-05 21:25:51 -05001910 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001911 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1912 if (ret < 0)
1913 goto error;
1914
1915 /*
1916 * this shouldn't happen, it means the last relocate
1917 * failed
1918 */
1919 if (ret == 0)
1920 break;
1921
1922 ret = btrfs_previous_item(chunk_root, path, 0,
1923 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001924 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001925 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001926
Chris Masonec44a352008-04-28 15:29:52 -04001927 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1928 path->slots[0]);
1929 if (found_key.objectid != key.objectid)
1930 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001931
Chris Masonec44a352008-04-28 15:29:52 -04001932 chunk = btrfs_item_ptr(path->nodes[0],
1933 path->slots[0],
1934 struct btrfs_chunk);
1935 key.offset = found_key.offset;
1936 /* chunk zero is special */
1937 if (key.offset == 0)
1938 break;
1939
Chris Mason7d9eb122008-07-08 14:19:17 -04001940 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001941 ret = btrfs_relocate_chunk(chunk_root,
1942 chunk_root->root_key.objectid,
1943 found_key.objectid,
1944 found_key.offset);
1945 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001946 }
1947 ret = 0;
1948error:
1949 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001950 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001951 return ret;
1952}
1953
Chris Mason8f18cf12008-04-25 16:53:30 -04001954/*
1955 * shrinking a device means finding all of the device extents past
1956 * the new size, and then following the back refs to the chunks.
1957 * The chunk relocation code actually frees the device extent
1958 */
1959int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1960{
1961 struct btrfs_trans_handle *trans;
1962 struct btrfs_root *root = device->dev_root;
1963 struct btrfs_dev_extent *dev_extent = NULL;
1964 struct btrfs_path *path;
1965 u64 length;
1966 u64 chunk_tree;
1967 u64 chunk_objectid;
1968 u64 chunk_offset;
1969 int ret;
1970 int slot;
1971 struct extent_buffer *l;
1972 struct btrfs_key key;
1973 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1974 u64 old_total = btrfs_super_total_bytes(super_copy);
1975 u64 diff = device->total_bytes - new_size;
1976
Yan Zheng2b820322008-11-17 21:11:30 -05001977 if (new_size >= device->total_bytes)
1978 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001979
1980 path = btrfs_alloc_path();
1981 if (!path)
1982 return -ENOMEM;
1983
1984 trans = btrfs_start_transaction(root, 1);
1985 if (!trans) {
1986 ret = -ENOMEM;
1987 goto done;
1988 }
1989
1990 path->reada = 2;
1991
Chris Mason7d9eb122008-07-08 14:19:17 -04001992 lock_chunks(root);
1993
Chris Mason8f18cf12008-04-25 16:53:30 -04001994 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05001995 if (device->writeable)
1996 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04001997 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001998 btrfs_end_transaction(trans, root);
1999
2000 key.objectid = device->devid;
2001 key.offset = (u64)-1;
2002 key.type = BTRFS_DEV_EXTENT_KEY;
2003
2004 while (1) {
2005 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2006 if (ret < 0)
2007 goto done;
2008
2009 ret = btrfs_previous_item(root, path, 0, key.type);
2010 if (ret < 0)
2011 goto done;
2012 if (ret) {
2013 ret = 0;
Yan Zhengbf1fb512009-07-22 09:59:00 -04002014 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002015 }
2016
2017 l = path->nodes[0];
2018 slot = path->slots[0];
2019 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2020
2021 if (key.objectid != device->devid)
Yan Zhengbf1fb512009-07-22 09:59:00 -04002022 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002023
2024 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2025 length = btrfs_dev_extent_length(l, dev_extent);
2026
2027 if (key.offset + length <= new_size)
Chris Balld6397ba2009-04-27 07:29:03 -04002028 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002029
2030 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2031 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2032 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2033 btrfs_release_path(root, path);
2034
2035 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2036 chunk_offset);
2037 if (ret)
2038 goto done;
2039 }
2040
Chris Balld6397ba2009-04-27 07:29:03 -04002041 /* Shrinking succeeded, else we would be at "done". */
2042 trans = btrfs_start_transaction(root, 1);
2043 if (!trans) {
2044 ret = -ENOMEM;
2045 goto done;
2046 }
2047 lock_chunks(root);
2048
2049 device->disk_total_bytes = new_size;
2050 /* Now btrfs_update_device() will change the on-disk size. */
2051 ret = btrfs_update_device(trans, device);
2052 if (ret) {
2053 unlock_chunks(root);
2054 btrfs_end_transaction(trans, root);
2055 goto done;
2056 }
2057 WARN_ON(diff > old_total);
2058 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2059 unlock_chunks(root);
2060 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002061done:
2062 btrfs_free_path(path);
2063 return ret;
2064}
2065
Christoph Hellwigb2950862008-12-02 09:54:17 -05002066static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002067 struct btrfs_root *root,
2068 struct btrfs_key *key,
2069 struct btrfs_chunk *chunk, int item_size)
2070{
2071 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2072 struct btrfs_disk_key disk_key;
2073 u32 array_size;
2074 u8 *ptr;
2075
2076 array_size = btrfs_super_sys_array_size(super_copy);
2077 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2078 return -EFBIG;
2079
2080 ptr = super_copy->sys_chunk_array + array_size;
2081 btrfs_cpu_key_to_disk(&disk_key, key);
2082 memcpy(ptr, &disk_key, sizeof(disk_key));
2083 ptr += sizeof(disk_key);
2084 memcpy(ptr, chunk, item_size);
2085 item_size += sizeof(disk_key);
2086 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2087 return 0;
2088}
2089
Chris Masond3977122009-01-05 21:25:51 -05002090static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002091 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002092{
2093 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2094 return calc_size;
2095 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2096 return calc_size * (num_stripes / sub_stripes);
2097 else
2098 return calc_size * num_stripes;
2099}
2100
Yan Zheng2b820322008-11-17 21:11:30 -05002101static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2102 struct btrfs_root *extent_root,
2103 struct map_lookup **map_ret,
2104 u64 *num_bytes, u64 *stripe_size,
2105 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04002106{
Chris Mason593060d2008-03-25 16:50:33 -04002107 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04002108 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05002109 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04002110 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05002111 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002112 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04002113 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05002114 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04002115 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002116 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002117 u64 max_chunk_size = calc_size;
2118 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04002119 u64 avail;
2120 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002121 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04002122 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04002123 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002124 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002125 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002126 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002127 int index;
Chris Mason593060d2008-03-25 16:50:33 -04002128 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002129
Chris Masonec44a352008-04-28 15:29:52 -04002130 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2131 (type & BTRFS_BLOCK_GROUP_DUP)) {
2132 WARN_ON(1);
2133 type &= ~BTRFS_BLOCK_GROUP_DUP;
2134 }
Yan Zheng2b820322008-11-17 21:11:30 -05002135 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04002136 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04002137
Chris Masona40a90a2008-04-18 11:55:51 -04002138 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002139 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04002140 min_stripes = 2;
2141 }
2142 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04002143 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002144 min_stripes = 2;
2145 }
Chris Mason8790d502008-04-03 16:29:03 -04002146 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002147 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002148 if (num_stripes < 2)
2149 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04002150 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002151 }
Chris Mason321aecc2008-04-16 10:49:51 -04002152 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002153 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04002154 if (num_stripes < 4)
2155 return -ENOSPC;
2156 num_stripes &= ~(u32)1;
2157 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002158 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002159 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002160
2161 if (type & BTRFS_BLOCK_GROUP_DATA) {
2162 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002163 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002164 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2165 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002166 min_stripe_size = 32 * 1024 * 1024;
2167 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2168 calc_size = 8 * 1024 * 1024;
2169 max_chunk_size = calc_size * 2;
2170 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002171 }
2172
Yan Zheng2b820322008-11-17 21:11:30 -05002173 /* we don't want a chunk larger than 10% of writeable space */
2174 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2175 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002176
Chris Masona40a90a2008-04-18 11:55:51 -04002177again:
Yan Zheng2b820322008-11-17 21:11:30 -05002178 if (!map || map->num_stripes != num_stripes) {
2179 kfree(map);
2180 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2181 if (!map)
2182 return -ENOMEM;
2183 map->num_stripes = num_stripes;
2184 }
2185
Chris Mason9b3f68b2008-04-18 10:29:51 -04002186 if (calc_size * num_stripes > max_chunk_size) {
2187 calc_size = max_chunk_size;
2188 do_div(calc_size, num_stripes);
2189 do_div(calc_size, stripe_len);
2190 calc_size *= stripe_len;
2191 }
2192 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04002193 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002194
Chris Mason9b3f68b2008-04-18 10:29:51 -04002195 do_div(calc_size, stripe_len);
2196 calc_size *= stripe_len;
2197
Yan Zheng2b820322008-11-17 21:11:30 -05002198 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002199 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002200
2201 if (type & BTRFS_BLOCK_GROUP_DUP)
2202 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002203 else
2204 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002205
Josef Bacik0f9dd462008-09-23 13:14:11 -04002206 /*
2207 * we add 1MB because we never use the first 1MB of the device, unless
2208 * we've looped, then we are likely allocating the maximum amount of
2209 * space left already
2210 */
2211 if (!looped)
2212 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002213
Yan Zheng2b820322008-11-17 21:11:30 -05002214 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002215 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002216 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002217 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002218 if (device->total_bytes > device->bytes_used)
2219 avail = device->total_bytes - device->bytes_used;
2220 else
2221 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002222 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002223
Chris Masondfe25022008-05-13 13:46:40 -04002224 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002225 ret = find_free_dev_extent(trans, device,
2226 min_free, &dev_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002227 if (ret == 0) {
2228 list_move_tail(&device->dev_alloc_list,
2229 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002230 map->stripes[index].dev = device;
2231 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002232 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002233 if (type & BTRFS_BLOCK_GROUP_DUP) {
2234 map->stripes[index].dev = device;
2235 map->stripes[index].physical =
2236 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002237 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002238 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002239 }
Chris Masondfe25022008-05-13 13:46:40 -04002240 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002241 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002242 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002243 break;
2244 }
Yan Zheng2b820322008-11-17 21:11:30 -05002245 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002246 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002247 if (index >= min_stripes) {
2248 num_stripes = index;
2249 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2250 num_stripes /= sub_stripes;
2251 num_stripes *= sub_stripes;
2252 }
2253 looped = 1;
2254 goto again;
2255 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002256 if (!looped && max_avail > 0) {
2257 looped = 1;
2258 calc_size = max_avail;
2259 goto again;
2260 }
Yan Zheng2b820322008-11-17 21:11:30 -05002261 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002262 return -ENOSPC;
2263 }
Chris Mason593060d2008-03-25 16:50:33 -04002264 map->sector_size = extent_root->sectorsize;
2265 map->stripe_len = stripe_len;
2266 map->io_align = stripe_len;
2267 map->io_width = stripe_len;
2268 map->type = type;
2269 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002270 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002271
Yan Zheng2b820322008-11-17 21:11:30 -05002272 *map_ret = map;
2273 *stripe_size = calc_size;
2274 *num_bytes = chunk_bytes_by_type(type, calc_size,
2275 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002276
2277 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002278 if (!em) {
2279 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002280 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002281 }
Chris Mason0b86a832008-03-24 15:01:56 -04002282 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002283 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002284 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002285 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002286 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002287
Chris Mason0b86a832008-03-24 15:01:56 -04002288 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2289 spin_lock(&em_tree->lock);
2290 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002291 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002292 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002293 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002294
2295 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2296 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2297 start, *num_bytes);
2298 BUG_ON(ret);
2299
2300 index = 0;
2301 while (index < map->num_stripes) {
2302 device = map->stripes[index].dev;
2303 dev_offset = map->stripes[index].physical;
2304
2305 ret = btrfs_alloc_dev_extent(trans, device,
2306 info->chunk_root->root_key.objectid,
2307 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2308 start, dev_offset, calc_size);
2309 BUG_ON(ret);
2310 index++;
2311 }
2312
2313 return 0;
2314}
2315
2316static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2317 struct btrfs_root *extent_root,
2318 struct map_lookup *map, u64 chunk_offset,
2319 u64 chunk_size, u64 stripe_size)
2320{
2321 u64 dev_offset;
2322 struct btrfs_key key;
2323 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2324 struct btrfs_device *device;
2325 struct btrfs_chunk *chunk;
2326 struct btrfs_stripe *stripe;
2327 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2328 int index = 0;
2329 int ret;
2330
2331 chunk = kzalloc(item_size, GFP_NOFS);
2332 if (!chunk)
2333 return -ENOMEM;
2334
2335 index = 0;
2336 while (index < map->num_stripes) {
2337 device = map->stripes[index].dev;
2338 device->bytes_used += stripe_size;
2339 ret = btrfs_update_device(trans, device);
2340 BUG_ON(ret);
2341 index++;
2342 }
2343
2344 index = 0;
2345 stripe = &chunk->stripe;
2346 while (index < map->num_stripes) {
2347 device = map->stripes[index].dev;
2348 dev_offset = map->stripes[index].physical;
2349
2350 btrfs_set_stack_stripe_devid(stripe, device->devid);
2351 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2352 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2353 stripe++;
2354 index++;
2355 }
2356
2357 btrfs_set_stack_chunk_length(chunk, chunk_size);
2358 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2359 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2360 btrfs_set_stack_chunk_type(chunk, map->type);
2361 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2362 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2363 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2364 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2365 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2366
2367 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2368 key.type = BTRFS_CHUNK_ITEM_KEY;
2369 key.offset = chunk_offset;
2370
2371 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2372 BUG_ON(ret);
2373
2374 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2375 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2376 item_size);
2377 BUG_ON(ret);
2378 }
2379 kfree(chunk);
2380 return 0;
2381}
2382
2383/*
2384 * Chunk allocation falls into two parts. The first part does works
2385 * that make the new allocated chunk useable, but not do any operation
2386 * that modifies the chunk tree. The second part does the works that
2387 * require modifying the chunk tree. This division is important for the
2388 * bootstrap process of adding storage to a seed btrfs.
2389 */
2390int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2391 struct btrfs_root *extent_root, u64 type)
2392{
2393 u64 chunk_offset;
2394 u64 chunk_size;
2395 u64 stripe_size;
2396 struct map_lookup *map;
2397 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2398 int ret;
2399
2400 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2401 &chunk_offset);
2402 if (ret)
2403 return ret;
2404
2405 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2406 &stripe_size, chunk_offset, type);
2407 if (ret)
2408 return ret;
2409
2410 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2411 chunk_size, stripe_size);
2412 BUG_ON(ret);
2413 return 0;
2414}
2415
Chris Masond3977122009-01-05 21:25:51 -05002416static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002417 struct btrfs_root *root,
2418 struct btrfs_device *device)
2419{
2420 u64 chunk_offset;
2421 u64 sys_chunk_offset;
2422 u64 chunk_size;
2423 u64 sys_chunk_size;
2424 u64 stripe_size;
2425 u64 sys_stripe_size;
2426 u64 alloc_profile;
2427 struct map_lookup *map;
2428 struct map_lookup *sys_map;
2429 struct btrfs_fs_info *fs_info = root->fs_info;
2430 struct btrfs_root *extent_root = fs_info->extent_root;
2431 int ret;
2432
2433 ret = find_next_chunk(fs_info->chunk_root,
2434 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2435 BUG_ON(ret);
2436
2437 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2438 (fs_info->metadata_alloc_profile &
2439 fs_info->avail_metadata_alloc_bits);
2440 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2441
2442 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2443 &stripe_size, chunk_offset, alloc_profile);
2444 BUG_ON(ret);
2445
2446 sys_chunk_offset = chunk_offset + chunk_size;
2447
2448 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2449 (fs_info->system_alloc_profile &
2450 fs_info->avail_system_alloc_bits);
2451 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2452
2453 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2454 &sys_chunk_size, &sys_stripe_size,
2455 sys_chunk_offset, alloc_profile);
2456 BUG_ON(ret);
2457
2458 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2459 BUG_ON(ret);
2460
2461 /*
2462 * Modifying chunk tree needs allocating new blocks from both
2463 * system block group and metadata block group. So we only can
2464 * do operations require modifying the chunk tree after both
2465 * block groups were created.
2466 */
2467 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2468 chunk_size, stripe_size);
2469 BUG_ON(ret);
2470
2471 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2472 sys_chunk_offset, sys_chunk_size,
2473 sys_stripe_size);
2474 BUG_ON(ret);
2475 return 0;
2476}
2477
2478int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2479{
2480 struct extent_map *em;
2481 struct map_lookup *map;
2482 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2483 int readonly = 0;
2484 int i;
2485
2486 spin_lock(&map_tree->map_tree.lock);
2487 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2488 spin_unlock(&map_tree->map_tree.lock);
2489 if (!em)
2490 return 1;
2491
2492 map = (struct map_lookup *)em->bdev;
2493 for (i = 0; i < map->num_stripes; i++) {
2494 if (!map->stripes[i].dev->writeable) {
2495 readonly = 1;
2496 break;
2497 }
2498 }
2499 free_extent_map(em);
2500 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002501}
2502
2503void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2504{
2505 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2506}
2507
2508void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2509{
2510 struct extent_map *em;
2511
Chris Masond3977122009-01-05 21:25:51 -05002512 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002513 spin_lock(&tree->map_tree.lock);
2514 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2515 if (em)
2516 remove_extent_mapping(&tree->map_tree, em);
2517 spin_unlock(&tree->map_tree.lock);
2518 if (!em)
2519 break;
2520 kfree(em->bdev);
2521 /* once for us */
2522 free_extent_map(em);
2523 /* once for the tree */
2524 free_extent_map(em);
2525 }
2526}
2527
Chris Masonf1885912008-04-09 16:28:12 -04002528int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2529{
2530 struct extent_map *em;
2531 struct map_lookup *map;
2532 struct extent_map_tree *em_tree = &map_tree->map_tree;
2533 int ret;
2534
2535 spin_lock(&em_tree->lock);
2536 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04002537 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002538 BUG_ON(!em);
2539
2540 BUG_ON(em->start > logical || em->start + em->len < logical);
2541 map = (struct map_lookup *)em->bdev;
2542 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2543 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002544 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2545 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002546 else
2547 ret = 1;
2548 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002549 return ret;
2550}
2551
Chris Masondfe25022008-05-13 13:46:40 -04002552static int find_live_mirror(struct map_lookup *map, int first, int num,
2553 int optimal)
2554{
2555 int i;
2556 if (map->stripes[optimal].dev->bdev)
2557 return optimal;
2558 for (i = first; i < first + num; i++) {
2559 if (map->stripes[i].dev->bdev)
2560 return i;
2561 }
2562 /* we couldn't find one that doesn't fail. Just return something
2563 * and the io error handling code will clean up eventually
2564 */
2565 return optimal;
2566}
2567
Chris Masonf2d8d742008-04-21 10:03:05 -04002568static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2569 u64 logical, u64 *length,
2570 struct btrfs_multi_bio **multi_ret,
2571 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002572{
2573 struct extent_map *em;
2574 struct map_lookup *map;
2575 struct extent_map_tree *em_tree = &map_tree->map_tree;
2576 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002577 u64 stripe_offset;
2578 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002579 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002580 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002581 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002582 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002583 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002584 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002585 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002586
Chris Masond3977122009-01-05 21:25:51 -05002587 if (multi_ret && !(rw & (1 << BIO_RW)))
Chris Masoncea9e442008-04-09 16:28:12 -04002588 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002589again:
2590 if (multi_ret) {
2591 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2592 GFP_NOFS);
2593 if (!multi)
2594 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002595
2596 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002597 }
Chris Mason0b86a832008-03-24 15:01:56 -04002598
2599 spin_lock(&em_tree->lock);
2600 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04002601 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002602
2603 if (!em && unplug_page)
2604 return 0;
2605
Chris Mason3b951512008-04-17 11:29:12 -04002606 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002607 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2608 (unsigned long long)logical,
2609 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002610 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002611 }
Chris Mason0b86a832008-03-24 15:01:56 -04002612
2613 BUG_ON(em->start > logical || em->start + em->len < logical);
2614 map = (struct map_lookup *)em->bdev;
2615 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002616
Chris Masonf1885912008-04-09 16:28:12 -04002617 if (mirror_num > map->num_stripes)
2618 mirror_num = 0;
2619
Chris Masoncea9e442008-04-09 16:28:12 -04002620 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04002621 if (rw & (1 << BIO_RW)) {
2622 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2623 BTRFS_BLOCK_GROUP_DUP)) {
2624 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002625 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002626 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2627 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002628 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002629 }
2630 }
Chris Masonffbd5172009-04-20 15:50:09 -04002631 if (multi_ret && (rw & (1 << BIO_RW)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002632 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002633 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002634 free_extent_map(em);
2635 kfree(multi);
2636 goto again;
2637 }
Chris Mason593060d2008-03-25 16:50:33 -04002638 stripe_nr = offset;
2639 /*
2640 * stripe_nr counts the total number of stripes we have to stride
2641 * to get to this block
2642 */
2643 do_div(stripe_nr, map->stripe_len);
2644
2645 stripe_offset = stripe_nr * map->stripe_len;
2646 BUG_ON(offset < stripe_offset);
2647
2648 /* stripe_offset is the offset of this block in its stripe*/
2649 stripe_offset = offset - stripe_offset;
2650
Chris Masoncea9e442008-04-09 16:28:12 -04002651 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002652 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002653 BTRFS_BLOCK_GROUP_DUP)) {
2654 /* we limit the length of each bio to what fits in a stripe */
2655 *length = min_t(u64, em->len - offset,
2656 map->stripe_len - stripe_offset);
2657 } else {
2658 *length = em->len - offset;
2659 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002660
2661 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002662 goto out;
2663
Chris Masonf2d8d742008-04-21 10:03:05 -04002664 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002665 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002666 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002667 if (unplug_page || (rw & (1 << BIO_RW)))
2668 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002669 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002670 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002671 else {
2672 stripe_index = find_live_mirror(map, 0,
2673 map->num_stripes,
2674 current->pid % map->num_stripes);
2675 }
Chris Mason2fff7342008-04-29 14:12:09 -04002676
Chris Mason611f0e02008-04-03 16:29:03 -04002677 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04002678 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002679 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002680 else if (mirror_num)
2681 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002682
Chris Mason321aecc2008-04-16 10:49:51 -04002683 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2684 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002685
2686 stripe_index = do_div(stripe_nr, factor);
2687 stripe_index *= map->sub_stripes;
2688
Chris Masonf2d8d742008-04-21 10:03:05 -04002689 if (unplug_page || (rw & (1 << BIO_RW)))
2690 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002691 else if (mirror_num)
2692 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002693 else {
2694 stripe_index = find_live_mirror(map, stripe_index,
2695 map->sub_stripes, stripe_index +
2696 current->pid % map->sub_stripes);
2697 }
Chris Mason8790d502008-04-03 16:29:03 -04002698 } else {
2699 /*
2700 * after this do_div call, stripe_nr is the number of stripes
2701 * on this device we have to walk to find the data, and
2702 * stripe_index is the number of our device in the stripe array
2703 */
2704 stripe_index = do_div(stripe_nr, map->num_stripes);
2705 }
Chris Mason593060d2008-03-25 16:50:33 -04002706 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002707
Chris Masonf2d8d742008-04-21 10:03:05 -04002708 for (i = 0; i < num_stripes; i++) {
2709 if (unplug_page) {
2710 struct btrfs_device *device;
2711 struct backing_dev_info *bdi;
2712
2713 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002714 if (device->bdev) {
2715 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002716 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002717 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002718 }
2719 } else {
2720 multi->stripes[i].physical =
2721 map->stripes[stripe_index].physical +
2722 stripe_offset + stripe_nr * map->stripe_len;
2723 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2724 }
Chris Masoncea9e442008-04-09 16:28:12 -04002725 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002726 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002727 if (multi_ret) {
2728 *multi_ret = multi;
2729 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002730 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002731 }
Chris Masoncea9e442008-04-09 16:28:12 -04002732out:
Chris Mason0b86a832008-03-24 15:01:56 -04002733 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002734 return 0;
2735}
2736
Chris Masonf2d8d742008-04-21 10:03:05 -04002737int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2738 u64 logical, u64 *length,
2739 struct btrfs_multi_bio **multi_ret, int mirror_num)
2740{
2741 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2742 mirror_num, NULL);
2743}
2744
Yan Zhenga512bbf2008-12-08 16:46:26 -05002745int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2746 u64 chunk_start, u64 physical, u64 devid,
2747 u64 **logical, int *naddrs, int *stripe_len)
2748{
2749 struct extent_map_tree *em_tree = &map_tree->map_tree;
2750 struct extent_map *em;
2751 struct map_lookup *map;
2752 u64 *buf;
2753 u64 bytenr;
2754 u64 length;
2755 u64 stripe_nr;
2756 int i, j, nr = 0;
2757
2758 spin_lock(&em_tree->lock);
2759 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2760 spin_unlock(&em_tree->lock);
2761
2762 BUG_ON(!em || em->start != chunk_start);
2763 map = (struct map_lookup *)em->bdev;
2764
2765 length = em->len;
2766 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2767 do_div(length, map->num_stripes / map->sub_stripes);
2768 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2769 do_div(length, map->num_stripes);
2770
2771 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2772 BUG_ON(!buf);
2773
2774 for (i = 0; i < map->num_stripes; i++) {
2775 if (devid && map->stripes[i].dev->devid != devid)
2776 continue;
2777 if (map->stripes[i].physical > physical ||
2778 map->stripes[i].physical + length <= physical)
2779 continue;
2780
2781 stripe_nr = physical - map->stripes[i].physical;
2782 do_div(stripe_nr, map->stripe_len);
2783
2784 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2785 stripe_nr = stripe_nr * map->num_stripes + i;
2786 do_div(stripe_nr, map->sub_stripes);
2787 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2788 stripe_nr = stripe_nr * map->num_stripes + i;
2789 }
2790 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002791 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002792 for (j = 0; j < nr; j++) {
2793 if (buf[j] == bytenr)
2794 break;
2795 }
Chris Mason934d3752008-12-08 16:43:10 -05002796 if (j == nr) {
2797 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002798 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002799 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002800 }
2801
Yan Zhenga512bbf2008-12-08 16:46:26 -05002802 *logical = buf;
2803 *naddrs = nr;
2804 *stripe_len = map->stripe_len;
2805
2806 free_extent_map(em);
2807 return 0;
2808}
2809
Chris Masonf2d8d742008-04-21 10:03:05 -04002810int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2811 u64 logical, struct page *page)
2812{
2813 u64 length = PAGE_CACHE_SIZE;
2814 return __btrfs_map_block(map_tree, READ, logical, &length,
2815 NULL, 0, page);
2816}
2817
Chris Mason8790d502008-04-03 16:29:03 -04002818static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002819{
Chris Masoncea9e442008-04-09 16:28:12 -04002820 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002821 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002822
Chris Mason8790d502008-04-03 16:29:03 -04002823 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002824 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002825
Chris Mason7d2b4da2008-08-05 10:13:57 -04002826 if (bio == multi->orig_bio)
2827 is_orig_bio = 1;
2828
Chris Masoncea9e442008-04-09 16:28:12 -04002829 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002830 if (!is_orig_bio) {
2831 bio_put(bio);
2832 bio = multi->orig_bio;
2833 }
Chris Mason8790d502008-04-03 16:29:03 -04002834 bio->bi_private = multi->private;
2835 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002836 /* only send an error to the higher layers if it is
2837 * beyond the tolerance of the multi-bio
2838 */
Chris Mason1259ab72008-05-12 13:39:03 -04002839 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002840 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002841 } else if (err) {
2842 /*
2843 * this bio is actually up to date, we didn't
2844 * go over the max number of errors
2845 */
2846 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002847 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002848 }
Chris Mason8790d502008-04-03 16:29:03 -04002849 kfree(multi);
2850
2851 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002852 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002853 bio_put(bio);
2854 }
Chris Mason8790d502008-04-03 16:29:03 -04002855}
2856
Chris Mason8b712842008-06-11 16:50:36 -04002857struct async_sched {
2858 struct bio *bio;
2859 int rw;
2860 struct btrfs_fs_info *info;
2861 struct btrfs_work work;
2862};
2863
2864/*
2865 * see run_scheduled_bios for a description of why bios are collected for
2866 * async submit.
2867 *
2868 * This will add one bio to the pending list for a device and make sure
2869 * the work struct is scheduled.
2870 */
Chris Masond3977122009-01-05 21:25:51 -05002871static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002872 struct btrfs_device *device,
2873 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002874{
2875 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04002876 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002877
2878 /* don't bother with additional async steps for reads, right now */
2879 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002880 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002881 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002882 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002883 return 0;
2884 }
2885
2886 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04002887 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002888 * higher layers. Otherwise, the async bio makes it appear we have
2889 * made progress against dirty pages when we've really just put it
2890 * on a queue for later
2891 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04002892 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002893 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002894 bio->bi_next = NULL;
2895 bio->bi_rw |= rw;
2896
2897 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -04002898 if (bio_sync(bio))
2899 pending_bios = &device->pending_sync_bios;
2900 else
2901 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002902
Chris Masonffbd5172009-04-20 15:50:09 -04002903 if (pending_bios->tail)
2904 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002905
Chris Masonffbd5172009-04-20 15:50:09 -04002906 pending_bios->tail = bio;
2907 if (!pending_bios->head)
2908 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002909 if (device->running_pending)
2910 should_queue = 0;
2911
2912 spin_unlock(&device->io_lock);
2913
2914 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002915 btrfs_queue_worker(&root->fs_info->submit_workers,
2916 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002917 return 0;
2918}
2919
Chris Masonf1885912008-04-09 16:28:12 -04002920int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002921 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002922{
2923 struct btrfs_mapping_tree *map_tree;
2924 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002925 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002926 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002927 u64 length = 0;
2928 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002929 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002930 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002931 int dev_nr = 0;
2932 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002933
Chris Masonf2d8d742008-04-21 10:03:05 -04002934 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002935 map_tree = &root->fs_info->mapping_tree;
2936 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002937
Chris Masonf1885912008-04-09 16:28:12 -04002938 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2939 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002940 BUG_ON(ret);
2941
2942 total_devs = multi->num_stripes;
2943 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05002944 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2945 "len %llu\n", (unsigned long long)logical,
2946 (unsigned long long)length,
2947 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04002948 BUG();
2949 }
2950 multi->end_io = first_bio->bi_end_io;
2951 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002952 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002953 atomic_set(&multi->stripes_pending, multi->num_stripes);
2954
Chris Masond3977122009-01-05 21:25:51 -05002955 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002956 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002957 if (dev_nr < total_devs - 1) {
2958 bio = bio_clone(first_bio, GFP_NOFS);
2959 BUG_ON(!bio);
2960 } else {
2961 bio = first_bio;
2962 }
2963 bio->bi_private = multi;
2964 bio->bi_end_io = end_bio_multi_stripe;
2965 }
Chris Masoncea9e442008-04-09 16:28:12 -04002966 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2967 dev = multi->stripes[dev_nr].dev;
Yan Zheng2b820322008-11-17 21:11:30 -05002968 BUG_ON(rw == WRITE && !dev->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002969 if (dev && dev->bdev) {
2970 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002971 if (async_submit)
2972 schedule_bio(root, dev, rw, bio);
2973 else
2974 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002975 } else {
2976 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2977 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04002978 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04002979 }
Chris Mason8790d502008-04-03 16:29:03 -04002980 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002981 }
Chris Masoncea9e442008-04-09 16:28:12 -04002982 if (total_devs == 1)
2983 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002984 return 0;
2985}
2986
Chris Masona4437552008-04-18 10:29:38 -04002987struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05002988 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04002989{
Yan Zheng2b820322008-11-17 21:11:30 -05002990 struct btrfs_device *device;
2991 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002992
Yan Zheng2b820322008-11-17 21:11:30 -05002993 cur_devices = root->fs_info->fs_devices;
2994 while (cur_devices) {
2995 if (!fsid ||
2996 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2997 device = __find_device(&cur_devices->devices,
2998 devid, uuid);
2999 if (device)
3000 return device;
3001 }
3002 cur_devices = cur_devices->seed;
3003 }
3004 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003005}
3006
Chris Masondfe25022008-05-13 13:46:40 -04003007static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3008 u64 devid, u8 *dev_uuid)
3009{
3010 struct btrfs_device *device;
3011 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3012
3013 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003014 if (!device)
3015 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003016 list_add(&device->dev_list,
3017 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003018 device->barriers = 1;
3019 device->dev_root = root->fs_info->dev_root;
3020 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003021 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003022 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04003023 fs_devices->num_devices++;
3024 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003025 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003026 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3027 return device;
3028}
3029
Chris Mason0b86a832008-03-24 15:01:56 -04003030static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3031 struct extent_buffer *leaf,
3032 struct btrfs_chunk *chunk)
3033{
3034 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3035 struct map_lookup *map;
3036 struct extent_map *em;
3037 u64 logical;
3038 u64 length;
3039 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003040 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003041 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003042 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003043 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003044
Chris Masone17cade2008-04-15 15:41:47 -04003045 logical = key->offset;
3046 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003047
Chris Mason0b86a832008-03-24 15:01:56 -04003048 spin_lock(&map_tree->map_tree.lock);
3049 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04003050 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003051
3052 /* already mapped? */
3053 if (em && em->start <= logical && em->start + em->len > logical) {
3054 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003055 return 0;
3056 } else if (em) {
3057 free_extent_map(em);
3058 }
Chris Mason0b86a832008-03-24 15:01:56 -04003059
Chris Mason0b86a832008-03-24 15:01:56 -04003060 em = alloc_extent_map(GFP_NOFS);
3061 if (!em)
3062 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003063 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3064 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003065 if (!map) {
3066 free_extent_map(em);
3067 return -ENOMEM;
3068 }
3069
3070 em->bdev = (struct block_device *)map;
3071 em->start = logical;
3072 em->len = length;
3073 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003074 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003075
Chris Mason593060d2008-03-25 16:50:33 -04003076 map->num_stripes = num_stripes;
3077 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3078 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3079 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3080 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3081 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003082 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003083 for (i = 0; i < num_stripes; i++) {
3084 map->stripes[i].physical =
3085 btrfs_stripe_offset_nr(leaf, chunk, i);
3086 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003087 read_extent_buffer(leaf, uuid, (unsigned long)
3088 btrfs_stripe_dev_uuid_nr(chunk, i),
3089 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003090 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3091 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003092 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003093 kfree(map);
3094 free_extent_map(em);
3095 return -EIO;
3096 }
Chris Masondfe25022008-05-13 13:46:40 -04003097 if (!map->stripes[i].dev) {
3098 map->stripes[i].dev =
3099 add_missing_dev(root, devid, uuid);
3100 if (!map->stripes[i].dev) {
3101 kfree(map);
3102 free_extent_map(em);
3103 return -EIO;
3104 }
3105 }
3106 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003107 }
3108
3109 spin_lock(&map_tree->map_tree.lock);
3110 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04003111 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003112 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003113 free_extent_map(em);
3114
3115 return 0;
3116}
3117
3118static int fill_device_from_item(struct extent_buffer *leaf,
3119 struct btrfs_dev_item *dev_item,
3120 struct btrfs_device *device)
3121{
3122 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003123
3124 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003125 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3126 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003127 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3128 device->type = btrfs_device_type(leaf, dev_item);
3129 device->io_align = btrfs_device_io_align(leaf, dev_item);
3130 device->io_width = btrfs_device_io_width(leaf, dev_item);
3131 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003132
3133 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003134 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003135
Chris Mason0b86a832008-03-24 15:01:56 -04003136 return 0;
3137}
3138
Yan Zheng2b820322008-11-17 21:11:30 -05003139static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3140{
3141 struct btrfs_fs_devices *fs_devices;
3142 int ret;
3143
3144 mutex_lock(&uuid_mutex);
3145
3146 fs_devices = root->fs_info->fs_devices->seed;
3147 while (fs_devices) {
3148 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3149 ret = 0;
3150 goto out;
3151 }
3152 fs_devices = fs_devices->seed;
3153 }
3154
3155 fs_devices = find_fsid(fsid);
3156 if (!fs_devices) {
3157 ret = -ENOENT;
3158 goto out;
3159 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003160
3161 fs_devices = clone_fs_devices(fs_devices);
3162 if (IS_ERR(fs_devices)) {
3163 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003164 goto out;
3165 }
3166
Christoph Hellwig97288f22008-12-02 06:36:09 -05003167 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003168 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003169 if (ret)
3170 goto out;
3171
3172 if (!fs_devices->seeding) {
3173 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003174 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003175 ret = -EINVAL;
3176 goto out;
3177 }
3178
3179 fs_devices->seed = root->fs_info->fs_devices->seed;
3180 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003181out:
3182 mutex_unlock(&uuid_mutex);
3183 return ret;
3184}
3185
Chris Mason0d81ba52008-03-24 15:02:07 -04003186static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003187 struct extent_buffer *leaf,
3188 struct btrfs_dev_item *dev_item)
3189{
3190 struct btrfs_device *device;
3191 u64 devid;
3192 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003193 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003194 u8 dev_uuid[BTRFS_UUID_SIZE];
3195
Chris Mason0b86a832008-03-24 15:01:56 -04003196 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003197 read_extent_buffer(leaf, dev_uuid,
3198 (unsigned long)btrfs_device_uuid(dev_item),
3199 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003200 read_extent_buffer(leaf, fs_uuid,
3201 (unsigned long)btrfs_device_fsid(dev_item),
3202 BTRFS_UUID_SIZE);
3203
3204 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3205 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003206 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003207 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003208 }
3209
3210 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3211 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003212 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003213 return -EIO;
3214
3215 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003216 printk(KERN_WARNING "warning devid %llu missing\n",
3217 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003218 device = add_missing_dev(root, devid, dev_uuid);
3219 if (!device)
3220 return -ENOMEM;
3221 }
3222 }
3223
3224 if (device->fs_devices != root->fs_info->fs_devices) {
3225 BUG_ON(device->writeable);
3226 if (device->generation !=
3227 btrfs_device_generation(leaf, dev_item))
3228 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003229 }
Chris Mason0b86a832008-03-24 15:01:56 -04003230
3231 fill_device_from_item(leaf, dev_item, device);
3232 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003233 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003234 if (device->writeable)
3235 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003236 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003237 return ret;
3238}
3239
Chris Mason0d81ba52008-03-24 15:02:07 -04003240int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3241{
3242 struct btrfs_dev_item *dev_item;
3243
3244 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3245 dev_item);
3246 return read_one_dev(root, buf, dev_item);
3247}
3248
Yan Zhenge4404d62008-12-12 10:03:26 -05003249int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003250{
3251 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003252 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003253 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003254 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003255 u8 *ptr;
3256 unsigned long sb_ptr;
3257 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003258 u32 num_stripes;
3259 u32 array_size;
3260 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003261 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003262 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003263
Yan Zhenge4404d62008-12-12 10:03:26 -05003264 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003265 BTRFS_SUPER_INFO_SIZE);
3266 if (!sb)
3267 return -ENOMEM;
3268 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003269 btrfs_set_buffer_lockdep_class(sb, 0);
3270
Chris Masona061fc82008-05-07 11:43:44 -04003271 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003272 array_size = btrfs_super_sys_array_size(super_copy);
3273
Chris Mason0b86a832008-03-24 15:01:56 -04003274 ptr = super_copy->sys_chunk_array;
3275 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3276 cur = 0;
3277
3278 while (cur < array_size) {
3279 disk_key = (struct btrfs_disk_key *)ptr;
3280 btrfs_disk_key_to_cpu(&key, disk_key);
3281
Chris Masona061fc82008-05-07 11:43:44 -04003282 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003283 sb_ptr += len;
3284 cur += len;
3285
Chris Mason0d81ba52008-03-24 15:02:07 -04003286 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003287 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003288 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003289 if (ret)
3290 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003291 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3292 len = btrfs_chunk_item_size(num_stripes);
3293 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003294 ret = -EIO;
3295 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003296 }
3297 ptr += len;
3298 sb_ptr += len;
3299 cur += len;
3300 }
Chris Masona061fc82008-05-07 11:43:44 -04003301 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003302 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003303}
3304
3305int btrfs_read_chunk_tree(struct btrfs_root *root)
3306{
3307 struct btrfs_path *path;
3308 struct extent_buffer *leaf;
3309 struct btrfs_key key;
3310 struct btrfs_key found_key;
3311 int ret;
3312 int slot;
3313
3314 root = root->fs_info->chunk_root;
3315
3316 path = btrfs_alloc_path();
3317 if (!path)
3318 return -ENOMEM;
3319
3320 /* first we search for all of the device items, and then we
3321 * read in all of the chunk items. This way we can create chunk
3322 * mappings that reference all of the devices that are afound
3323 */
3324 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3325 key.offset = 0;
3326 key.type = 0;
3327again:
3328 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masond3977122009-01-05 21:25:51 -05003329 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003330 leaf = path->nodes[0];
3331 slot = path->slots[0];
3332 if (slot >= btrfs_header_nritems(leaf)) {
3333 ret = btrfs_next_leaf(root, path);
3334 if (ret == 0)
3335 continue;
3336 if (ret < 0)
3337 goto error;
3338 break;
3339 }
3340 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3341 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3342 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3343 break;
3344 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3345 struct btrfs_dev_item *dev_item;
3346 dev_item = btrfs_item_ptr(leaf, slot,
3347 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003348 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003349 if (ret)
3350 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003351 }
3352 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3353 struct btrfs_chunk *chunk;
3354 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3355 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003356 if (ret)
3357 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003358 }
3359 path->slots[0]++;
3360 }
3361 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3362 key.objectid = 0;
3363 btrfs_release_path(root, path);
3364 goto again;
3365 }
Chris Mason0b86a832008-03-24 15:01:56 -04003366 ret = 0;
3367error:
Yan Zheng2b820322008-11-17 21:11:30 -05003368 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003369 return ret;
3370}