blob: 7cad59353b09c4fdf0cb80072e109bf0f9db7165 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Chris Mason593060d2008-03-25 16:50:33 -040026#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "ctree.h"
29#include "extent_map.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "print-tree.h"
33#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Chris Mason593060d2008-03-25 16:50:33 -040036struct map_lookup {
37 u64 type;
38 int io_align;
39 int io_width;
40 int stripe_len;
41 int sector_size;
42 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040043 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040044 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040045};
46
Yan Zheng2b820322008-11-17 21:11:30 -050047static int init_first_rw_device(struct btrfs_trans_handle *trans,
48 struct btrfs_root *root,
49 struct btrfs_device *device);
50static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
51
Chris Mason593060d2008-03-25 16:50:33 -040052#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040053 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040054
Chris Mason8a4b83c2008-03-24 15:02:07 -040055static DEFINE_MUTEX(uuid_mutex);
56static LIST_HEAD(fs_uuids);
57
Chris Masona061fc82008-05-07 11:43:44 -040058void btrfs_lock_volumes(void)
59{
60 mutex_lock(&uuid_mutex);
61}
62
63void btrfs_unlock_volumes(void)
64{
65 mutex_unlock(&uuid_mutex);
66}
67
Chris Mason7d9eb122008-07-08 14:19:17 -040068static void lock_chunks(struct btrfs_root *root)
69{
Chris Mason7d9eb122008-07-08 14:19:17 -040070 mutex_lock(&root->fs_info->chunk_mutex);
71}
72
73static void unlock_chunks(struct btrfs_root *root)
74{
Chris Mason7d9eb122008-07-08 14:19:17 -040075 mutex_unlock(&root->fs_info->chunk_mutex);
76}
77
Yan Zhenge4404d62008-12-12 10:03:26 -050078static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
79{
80 struct btrfs_device *device;
81 WARN_ON(fs_devices->opened);
82 while (!list_empty(&fs_devices->devices)) {
83 device = list_entry(fs_devices->devices.next,
84 struct btrfs_device, dev_list);
85 list_del(&device->dev_list);
86 kfree(device->name);
87 kfree(device);
88 }
89 kfree(fs_devices);
90}
91
Chris Mason8a4b83c2008-03-24 15:02:07 -040092int btrfs_cleanup_fs_uuids(void)
93{
94 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040095
Yan Zheng2b820322008-11-17 21:11:30 -050096 while (!list_empty(&fs_uuids)) {
97 fs_devices = list_entry(fs_uuids.next,
98 struct btrfs_fs_devices, list);
99 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500100 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 }
102 return 0;
103}
104
Chris Masona1b32a52008-09-05 16:09:51 -0400105static noinline struct btrfs_device *__find_device(struct list_head *head,
106 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107{
108 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400109
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500110 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400111 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400112 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400114 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400115 }
116 return NULL;
117}
118
Chris Masona1b32a52008-09-05 16:09:51 -0400119static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400120{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400121 struct btrfs_fs_devices *fs_devices;
122
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500123 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400124 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
125 return fs_devices;
126 }
127 return NULL;
128}
129
Chris Masonffbd5172009-04-20 15:50:09 -0400130static void requeue_list(struct btrfs_pending_bios *pending_bios,
131 struct bio *head, struct bio *tail)
132{
133
134 struct bio *old_head;
135
136 old_head = pending_bios->head;
137 pending_bios->head = head;
138 if (pending_bios->tail)
139 tail->bi_next = old_head;
140 else
141 pending_bios->tail = tail;
142}
143
Chris Mason8b712842008-06-11 16:50:36 -0400144/*
145 * we try to collect pending bios for a device so we don't get a large
146 * number of procs sending bios down to the same device. This greatly
147 * improves the schedulers ability to collect and merge the bios.
148 *
149 * But, it also turns into a long list of bios to process and that is sure
150 * to eventually make the worker thread block. The solution here is to
151 * make some progress and then put this work struct back at the end of
152 * the list if the block device is congested. This way, multiple devices
153 * can make progress from a single worker thread.
154 */
Chris Masond3977122009-01-05 21:25:51 -0500155static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400156{
157 struct bio *pending;
158 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400159 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400160 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400161 struct bio *tail;
162 struct bio *cur;
163 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400164 unsigned long num_run;
165 unsigned long num_sync_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400166 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400167 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400168 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400169 int force_reg = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400170
Chris Masonbedf7622009-04-03 10:32:58 -0400171 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400172 fs_info = device->dev_root->fs_info;
173 limit = btrfs_async_submit_limit(fs_info);
174 limit = limit * 2 / 3;
175
Chris Masonffbd5172009-04-20 15:50:09 -0400176 /* we want to make sure that every time we switch from the sync
177 * list to the normal list, we unplug
178 */
179 num_sync_run = 0;
180
Chris Mason8b712842008-06-11 16:50:36 -0400181loop:
182 spin_lock(&device->io_lock);
183
Chris Masona6837052009-02-04 09:19:41 -0500184loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400185 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400186
Chris Mason8b712842008-06-11 16:50:36 -0400187 /* take all the bios off the list at once and process them
188 * later on (without the lock held). But, remember the
189 * tail and other pointers so the bios can be properly reinserted
190 * into the list if we hit congestion
191 */
Chris Masond84275c2009-06-09 15:39:08 -0400192 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400193 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400194 force_reg = 1;
195 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400196 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400197 force_reg = 0;
198 }
Chris Masonffbd5172009-04-20 15:50:09 -0400199
200 pending = pending_bios->head;
201 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400202 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400203
204 /*
205 * if pending was null this time around, no bios need processing
206 * at all and we can stop. Otherwise it'll loop back up again
207 * and do an additional check so no bios are missed.
208 *
209 * device->running_pending is used to synchronize with the
210 * schedule_bio code.
211 */
Chris Masonffbd5172009-04-20 15:50:09 -0400212 if (device->pending_sync_bios.head == NULL &&
213 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400214 again = 0;
215 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400216 } else {
217 again = 1;
218 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400219 }
Chris Masonffbd5172009-04-20 15:50:09 -0400220
221 pending_bios->head = NULL;
222 pending_bios->tail = NULL;
223
Chris Mason8b712842008-06-11 16:50:36 -0400224 spin_unlock(&device->io_lock);
225
Chris Masonffbd5172009-04-20 15:50:09 -0400226 /*
227 * if we're doing the regular priority list, make sure we unplug
228 * for any high prio bios we've sent down
229 */
230 if (pending_bios == &device->pending_bios && num_sync_run > 0) {
231 num_sync_run = 0;
232 blk_run_backing_dev(bdi, NULL);
233 }
234
Chris Masond3977122009-01-05 21:25:51 -0500235 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400236
237 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400238 /* we want to work on both lists, but do more bios on the
239 * sync list than the regular list
240 */
241 if ((num_run > 32 &&
242 pending_bios != &device->pending_sync_bios &&
243 device->pending_sync_bios.head) ||
244 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
245 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400246 spin_lock(&device->io_lock);
247 requeue_list(pending_bios, pending, tail);
248 goto loop_lock;
249 }
250
Chris Mason8b712842008-06-11 16:50:36 -0400251 cur = pending;
252 pending = pending->bi_next;
253 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400254 atomic_dec(&fs_info->nr_async_bios);
255
256 if (atomic_read(&fs_info->nr_async_bios) < limit &&
257 waitqueue_active(&fs_info->async_submit_wait))
258 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400259
260 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400261
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200262 if (cur->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -0400263 num_sync_run++;
264
Chris Mason5ff7ba32010-03-15 10:21:30 -0400265 submit_bio(cur->bi_rw, cur);
266 num_run++;
267 batch_run++;
Chris Masonffbd5172009-04-20 15:50:09 -0400268 if (need_resched()) {
269 if (num_sync_run) {
270 blk_run_backing_dev(bdi, NULL);
271 num_sync_run = 0;
272 }
273 cond_resched();
274 }
Chris Mason8b712842008-06-11 16:50:36 -0400275
276 /*
277 * we made progress, there is more work to do and the bdi
278 * is now congested. Back off and let other work structs
279 * run instead
280 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400281 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500282 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400283 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400284
Chris Masonb765ead2009-04-03 10:27:10 -0400285 ioc = current->io_context;
286
287 /*
288 * the main goal here is that we don't want to
289 * block if we're going to be able to submit
290 * more requests without blocking.
291 *
292 * This code does two great things, it pokes into
293 * the elevator code from a filesystem _and_
294 * it makes assumptions about how batching works.
295 */
296 if (ioc && ioc->nr_batch_requests > 0 &&
297 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
298 (last_waited == 0 ||
299 ioc->last_waited == last_waited)) {
300 /*
301 * we want to go through our batch of
302 * requests and stop. So, we copy out
303 * the ioc->last_waited time and test
304 * against it before looping
305 */
306 last_waited = ioc->last_waited;
Chris Masonffbd5172009-04-20 15:50:09 -0400307 if (need_resched()) {
308 if (num_sync_run) {
309 blk_run_backing_dev(bdi, NULL);
310 num_sync_run = 0;
311 }
312 cond_resched();
313 }
Chris Masonb765ead2009-04-03 10:27:10 -0400314 continue;
315 }
Chris Mason8b712842008-06-11 16:50:36 -0400316 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400317 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500318 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400319
320 spin_unlock(&device->io_lock);
321 btrfs_requeue_work(&device->work);
322 goto done;
323 }
324 }
Chris Masonffbd5172009-04-20 15:50:09 -0400325
326 if (num_sync_run) {
327 num_sync_run = 0;
328 blk_run_backing_dev(bdi, NULL);
329 }
Chris Masonbedf7622009-04-03 10:32:58 -0400330 /*
331 * IO has already been through a long path to get here. Checksumming,
332 * async helper threads, perhaps compression. We've done a pretty
333 * good job of collecting a batch of IO and should just unplug
334 * the device right away.
335 *
336 * This will help anyone who is waiting on the IO, they might have
337 * already unplugged, but managed to do so before the bio they
338 * cared about found its way down here.
339 */
340 blk_run_backing_dev(bdi, NULL);
Chris Mason51684082010-03-10 15:33:32 -0500341
342 cond_resched();
343 if (again)
344 goto loop;
345
346 spin_lock(&device->io_lock);
347 if (device->pending_bios.head || device->pending_sync_bios.head)
348 goto loop_lock;
349 spin_unlock(&device->io_lock);
350
Chris Mason8b712842008-06-11 16:50:36 -0400351done:
352 return 0;
353}
354
Christoph Hellwigb2950862008-12-02 09:54:17 -0500355static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400356{
357 struct btrfs_device *device;
358
359 device = container_of(work, struct btrfs_device, work);
360 run_scheduled_bios(device);
361}
362
Chris Masona1b32a52008-09-05 16:09:51 -0400363static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400364 struct btrfs_super_block *disk_super,
365 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
366{
367 struct btrfs_device *device;
368 struct btrfs_fs_devices *fs_devices;
369 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000370 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400371
372 fs_devices = find_fsid(disk_super->fsid);
373 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400374 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375 if (!fs_devices)
376 return -ENOMEM;
377 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400378 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400379 list_add(&fs_devices->list, &fs_uuids);
380 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
381 fs_devices->latest_devid = devid;
382 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400383 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400384 device = NULL;
385 } else {
Chris Masona4437552008-04-18 10:29:38 -0400386 device = __find_device(&fs_devices->devices, devid,
387 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 }
389 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500390 if (fs_devices->opened)
391 return -EBUSY;
392
Chris Mason8a4b83c2008-03-24 15:02:07 -0400393 device = kzalloc(sizeof(*device), GFP_NOFS);
394 if (!device) {
395 /* we can safely leave the fs_devices entry around */
396 return -ENOMEM;
397 }
398 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400399 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400400 memcpy(device->uuid, disk_super->dev_item.uuid,
401 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400402 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400403 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400404 device->name = kstrdup(path, GFP_NOFS);
405 if (!device->name) {
406 kfree(device);
407 return -ENOMEM;
408 }
Yan Zheng2b820322008-11-17 21:11:30 -0500409 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400410
411 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400412 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400413 mutex_unlock(&fs_devices->device_list_mutex);
414
Yan Zheng2b820322008-11-17 21:11:30 -0500415 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400416 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500417 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000418 name = kstrdup(path, GFP_NOFS);
419 if (!name)
420 return -ENOMEM;
421 kfree(device->name);
422 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500423 if (device->missing) {
424 fs_devices->missing_devices--;
425 device->missing = 0;
426 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427 }
428
429 if (found_transid > fs_devices->latest_trans) {
430 fs_devices->latest_devid = devid;
431 fs_devices->latest_trans = found_transid;
432 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400433 *fs_devices_ret = fs_devices;
434 return 0;
435}
436
Yan Zhenge4404d62008-12-12 10:03:26 -0500437static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
438{
439 struct btrfs_fs_devices *fs_devices;
440 struct btrfs_device *device;
441 struct btrfs_device *orig_dev;
442
443 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
444 if (!fs_devices)
445 return ERR_PTR(-ENOMEM);
446
447 INIT_LIST_HEAD(&fs_devices->devices);
448 INIT_LIST_HEAD(&fs_devices->alloc_list);
449 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400450 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500451 fs_devices->latest_devid = orig->latest_devid;
452 fs_devices->latest_trans = orig->latest_trans;
453 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
454
Chris Masone5e9a522009-06-10 15:17:02 -0400455 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500456 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
457 device = kzalloc(sizeof(*device), GFP_NOFS);
458 if (!device)
459 goto error;
460
461 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400462 if (!device->name) {
463 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500464 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400465 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500466
467 device->devid = orig_dev->devid;
468 device->work.func = pending_bios_fn;
469 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
470 device->barriers = 1;
471 spin_lock_init(&device->io_lock);
472 INIT_LIST_HEAD(&device->dev_list);
473 INIT_LIST_HEAD(&device->dev_alloc_list);
474
475 list_add(&device->dev_list, &fs_devices->devices);
476 device->fs_devices = fs_devices;
477 fs_devices->num_devices++;
478 }
Chris Masone5e9a522009-06-10 15:17:02 -0400479 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500480 return fs_devices;
481error:
Chris Masone5e9a522009-06-10 15:17:02 -0400482 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500483 free_fs_devices(fs_devices);
484 return ERR_PTR(-ENOMEM);
485}
486
Chris Masondfe25022008-05-13 13:46:40 -0400487int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
488{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500489 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400490
491 mutex_lock(&uuid_mutex);
492again:
Chris Masone5e9a522009-06-10 15:17:02 -0400493 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500494 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500495 if (device->in_fs_metadata)
496 continue;
497
498 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500499 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500500 device->bdev = NULL;
501 fs_devices->open_devices--;
502 }
503 if (device->writeable) {
504 list_del_init(&device->dev_alloc_list);
505 device->writeable = 0;
506 fs_devices->rw_devices--;
507 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500508 list_del_init(&device->dev_list);
509 fs_devices->num_devices--;
510 kfree(device->name);
511 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400512 }
Chris Masone5e9a522009-06-10 15:17:02 -0400513 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500514
515 if (fs_devices->seed) {
516 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500517 goto again;
518 }
519
Chris Masondfe25022008-05-13 13:46:40 -0400520 mutex_unlock(&uuid_mutex);
521 return 0;
522}
Chris Masona0af4692008-05-13 16:03:06 -0400523
Yan Zheng2b820322008-11-17 21:11:30 -0500524static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400525{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400526 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500527
Yan Zheng2b820322008-11-17 21:11:30 -0500528 if (--fs_devices->opened > 0)
529 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400530
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500531 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400532 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500533 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400534 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 }
Yan Zheng2b820322008-11-17 21:11:30 -0500536 if (device->writeable) {
537 list_del_init(&device->dev_alloc_list);
538 fs_devices->rw_devices--;
539 }
540
Chris Mason8a4b83c2008-03-24 15:02:07 -0400541 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500542 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400543 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400544 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500545 WARN_ON(fs_devices->open_devices);
546 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500547 fs_devices->opened = 0;
548 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500549
Chris Mason8a4b83c2008-03-24 15:02:07 -0400550 return 0;
551}
552
Yan Zheng2b820322008-11-17 21:11:30 -0500553int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
554{
Yan Zhenge4404d62008-12-12 10:03:26 -0500555 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500556 int ret;
557
558 mutex_lock(&uuid_mutex);
559 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500560 if (!fs_devices->opened) {
561 seed_devices = fs_devices->seed;
562 fs_devices->seed = NULL;
563 }
Yan Zheng2b820322008-11-17 21:11:30 -0500564 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500565
566 while (seed_devices) {
567 fs_devices = seed_devices;
568 seed_devices = fs_devices->seed;
569 __btrfs_close_devices(fs_devices);
570 free_fs_devices(fs_devices);
571 }
Yan Zheng2b820322008-11-17 21:11:30 -0500572 return ret;
573}
574
Yan Zhenge4404d62008-12-12 10:03:26 -0500575static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
576 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400577{
578 struct block_device *bdev;
579 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400580 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400581 struct block_device *latest_bdev = NULL;
582 struct buffer_head *bh;
583 struct btrfs_super_block *disk_super;
584 u64 latest_devid = 0;
585 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400586 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500587 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400588 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400589
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500590 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400591 if (device->bdev)
592 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400593 if (!device->name)
594 continue;
595
Chris Mason15916de2008-11-19 21:17:22 -0500596 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400597 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500598 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400599 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400600 }
Chris Masona061fc82008-05-07 11:43:44 -0400601 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400602
Yan Zhenga512bbf2008-12-08 16:46:26 -0500603 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000604 if (!bh) {
605 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400606 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000607 }
Chris Masona0af4692008-05-13 16:03:06 -0400608
609 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000610 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400611 if (devid != device->devid)
612 goto error_brelse;
613
Yan Zheng2b820322008-11-17 21:11:30 -0500614 if (memcmp(device->uuid, disk_super->dev_item.uuid,
615 BTRFS_UUID_SIZE))
616 goto error_brelse;
617
618 device->generation = btrfs_super_generation(disk_super);
619 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400620 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500621 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400622 latest_bdev = bdev;
623 }
624
Yan Zheng2b820322008-11-17 21:11:30 -0500625 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
626 device->writeable = 0;
627 } else {
628 device->writeable = !bdev_read_only(bdev);
629 seeding = 0;
630 }
631
Chris Mason8a4b83c2008-03-24 15:02:07 -0400632 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400633 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500634 device->mode = flags;
635
Chris Masonc2898112009-06-10 09:51:32 -0400636 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
637 fs_devices->rotating = 1;
638
Chris Masona0af4692008-05-13 16:03:06 -0400639 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500640 if (device->writeable) {
641 fs_devices->rw_devices++;
642 list_add(&device->dev_alloc_list,
643 &fs_devices->alloc_list);
644 }
Chris Masona0af4692008-05-13 16:03:06 -0400645 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400646
Chris Masona0af4692008-05-13 16:03:06 -0400647error_brelse:
648 brelse(bh);
649error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500650 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400651error:
652 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400653 }
Chris Masona0af4692008-05-13 16:03:06 -0400654 if (fs_devices->open_devices == 0) {
655 ret = -EIO;
656 goto out;
657 }
Yan Zheng2b820322008-11-17 21:11:30 -0500658 fs_devices->seeding = seeding;
659 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400660 fs_devices->latest_bdev = latest_bdev;
661 fs_devices->latest_devid = latest_devid;
662 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500663 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400664out:
Yan Zheng2b820322008-11-17 21:11:30 -0500665 return ret;
666}
667
668int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500669 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500670{
671 int ret;
672
673 mutex_lock(&uuid_mutex);
674 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500675 fs_devices->opened++;
676 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500677 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500678 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500679 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400680 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400681 return ret;
682}
683
Christoph Hellwig97288f22008-12-02 06:36:09 -0500684int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685 struct btrfs_fs_devices **fs_devices_ret)
686{
687 struct btrfs_super_block *disk_super;
688 struct block_device *bdev;
689 struct buffer_head *bh;
690 int ret;
691 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400692 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400693
694 mutex_lock(&uuid_mutex);
695
Chris Mason15916de2008-11-19 21:17:22 -0500696 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400697
698 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400699 ret = PTR_ERR(bdev);
700 goto error;
701 }
702
703 ret = set_blocksize(bdev, 4096);
704 if (ret)
705 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500706 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400707 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000708 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709 goto error_close;
710 }
711 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000712 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400713 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400714 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500715 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400716 else {
717 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500718 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400719 *(unsigned long long *)disk_super->fsid,
720 *(unsigned long long *)(disk_super->fsid + 8));
721 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500722 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500723 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400724 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
725
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726 brelse(bh);
727error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500728 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400729error:
730 mutex_unlock(&uuid_mutex);
731 return ret;
732}
Chris Mason0b86a832008-03-24 15:01:56 -0400733
Miao Xie6d07bce2011-01-05 10:07:31 +0000734/* helper to account the used device space in the range */
735int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
736 u64 end, u64 *length)
737{
738 struct btrfs_key key;
739 struct btrfs_root *root = device->dev_root;
740 struct btrfs_dev_extent *dev_extent;
741 struct btrfs_path *path;
742 u64 extent_end;
743 int ret;
744 int slot;
745 struct extent_buffer *l;
746
747 *length = 0;
748
749 if (start >= device->total_bytes)
750 return 0;
751
752 path = btrfs_alloc_path();
753 if (!path)
754 return -ENOMEM;
755 path->reada = 2;
756
757 key.objectid = device->devid;
758 key.offset = start;
759 key.type = BTRFS_DEV_EXTENT_KEY;
760
761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
762 if (ret < 0)
763 goto out;
764 if (ret > 0) {
765 ret = btrfs_previous_item(root, path, key.objectid, key.type);
766 if (ret < 0)
767 goto out;
768 }
769
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 out;
779
780 break;
781 }
782 btrfs_item_key_to_cpu(l, &key, slot);
783
784 if (key.objectid < device->devid)
785 goto next;
786
787 if (key.objectid > device->devid)
788 break;
789
790 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
791 goto next;
792
793 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
794 extent_end = key.offset + btrfs_dev_extent_length(l,
795 dev_extent);
796 if (key.offset <= start && extent_end > end) {
797 *length = end - start + 1;
798 break;
799 } else if (key.offset <= start && extent_end > start)
800 *length += extent_end - start;
801 else if (key.offset > start && extent_end <= end)
802 *length += extent_end - key.offset;
803 else if (key.offset > start && key.offset <= end) {
804 *length += end - key.offset + 1;
805 break;
806 } else if (key.offset > end)
807 break;
808
809next:
810 path->slots[0]++;
811 }
812 ret = 0;
813out:
814 btrfs_free_path(path);
815 return ret;
816}
817
Chris Mason0b86a832008-03-24 15:01:56 -0400818/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000819 * find_free_dev_extent - find free space in the specified device
820 * @trans: transaction handler
821 * @device: the device which we search the free space in
822 * @num_bytes: the size of the free space that we need
823 * @start: store the start of the free space.
824 * @len: the size of the free space. that we find, or the size of the max
825 * free space if we don't find suitable free space
826 *
Chris Mason0b86a832008-03-24 15:01:56 -0400827 * this uses a pretty simple search, the expectation is that it is
828 * called very infrequently and that a given device has a small number
829 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000830 *
831 * @start is used to store the start of the free space if we find. But if we
832 * don't find suitable free space, it will be used to store the start position
833 * of the max free space.
834 *
835 * @len is used to store the size of the free space that we find.
836 * But if we don't find suitable free space, it is used to store the size of
837 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400838 */
Josef Bacikba1bf482009-09-11 16:11:19 -0400839int find_free_dev_extent(struct btrfs_trans_handle *trans,
840 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000841 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400842{
843 struct btrfs_key key;
844 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000845 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500846 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 u64 hole_size;
848 u64 max_hole_start;
849 u64 max_hole_size;
850 u64 extent_end;
851 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400852 u64 search_end = device->total_bytes;
853 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400855 struct extent_buffer *l;
856
Chris Mason0b86a832008-03-24 15:01:56 -0400857 /* FIXME use last free of some kind */
858
Chris Mason8a4b83c2008-03-24 15:02:07 -0400859 /* we don't want to overwrite the superblock on the drive,
860 * so we make sure to start at an offset of at least 1MB
861 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000862 search_start = 1024 * 1024;
Chris Mason8f18cf12008-04-25 16:53:30 -0400863
Miao Xie7bfc8372011-01-05 10:07:26 +0000864 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason8f18cf12008-04-25 16:53:30 -0400865 search_start = max(root->fs_info->alloc_start, search_start);
866
Miao Xie7bfc8372011-01-05 10:07:26 +0000867 max_hole_start = search_start;
868 max_hole_size = 0;
869
870 if (search_start >= search_end) {
871 ret = -ENOSPC;
872 goto error;
873 }
874
875 path = btrfs_alloc_path();
876 if (!path) {
877 ret = -ENOMEM;
878 goto error;
879 }
880 path->reada = 2;
881
Chris Mason0b86a832008-03-24 15:01:56 -0400882 key.objectid = device->devid;
883 key.offset = search_start;
884 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000885
Chris Mason0b86a832008-03-24 15:01:56 -0400886 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
887 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000888 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400889 if (ret > 0) {
890 ret = btrfs_previous_item(root, path, key.objectid, key.type);
891 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000892 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400893 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000894
Chris Mason0b86a832008-03-24 15:01:56 -0400895 while (1) {
896 l = path->nodes[0];
897 slot = path->slots[0];
898 if (slot >= btrfs_header_nritems(l)) {
899 ret = btrfs_next_leaf(root, path);
900 if (ret == 0)
901 continue;
902 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000903 goto out;
904
905 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400906 }
907 btrfs_item_key_to_cpu(l, &key, slot);
908
909 if (key.objectid < device->devid)
910 goto next;
911
912 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400914
Chris Masond3977122009-01-05 21:25:51 -0500915 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400916 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400917
Miao Xie7bfc8372011-01-05 10:07:26 +0000918 if (key.offset > search_start) {
919 hole_size = key.offset - search_start;
920
921 if (hole_size > max_hole_size) {
922 max_hole_start = search_start;
923 max_hole_size = hole_size;
924 }
925
926 /*
927 * If this free space is greater than which we need,
928 * it must be the max free space that we have found
929 * until now, so max_hole_start must point to the start
930 * of this free space and the length of this free space
931 * is stored in max_hole_size. Thus, we return
932 * max_hole_start and max_hole_size and go back to the
933 * caller.
934 */
935 if (hole_size >= num_bytes) {
936 ret = 0;
937 goto out;
938 }
939 }
940
Chris Mason0b86a832008-03-24 15:01:56 -0400941 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000942 extent_end = key.offset + btrfs_dev_extent_length(l,
943 dev_extent);
944 if (extent_end > search_start)
945 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400946next:
947 path->slots[0]++;
948 cond_resched();
949 }
Chris Mason0b86a832008-03-24 15:01:56 -0400950
Miao Xie7bfc8372011-01-05 10:07:26 +0000951 hole_size = search_end- search_start;
952 if (hole_size > max_hole_size) {
953 max_hole_start = search_start;
954 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400955 }
Chris Mason0b86a832008-03-24 15:01:56 -0400956
Miao Xie7bfc8372011-01-05 10:07:26 +0000957 /* See above. */
958 if (hole_size < num_bytes)
959 ret = -ENOSPC;
960 else
961 ret = 0;
962
963out:
Yan Zheng2b820322008-11-17 21:11:30 -0500964 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000965error:
966 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000967 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000968 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400969 return ret;
970}
971
Christoph Hellwigb2950862008-12-02 09:54:17 -0500972static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400973 struct btrfs_device *device,
974 u64 start)
975{
976 int ret;
977 struct btrfs_path *path;
978 struct btrfs_root *root = device->dev_root;
979 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400980 struct btrfs_key found_key;
981 struct extent_buffer *leaf = NULL;
982 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400983
984 path = btrfs_alloc_path();
985 if (!path)
986 return -ENOMEM;
987
988 key.objectid = device->devid;
989 key.offset = start;
990 key.type = BTRFS_DEV_EXTENT_KEY;
991
992 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400993 if (ret > 0) {
994 ret = btrfs_previous_item(root, path, key.objectid,
995 BTRFS_DEV_EXTENT_KEY);
996 BUG_ON(ret);
997 leaf = path->nodes[0];
998 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
999 extent = btrfs_item_ptr(leaf, path->slots[0],
1000 struct btrfs_dev_extent);
1001 BUG_ON(found_key.offset > start || found_key.offset +
1002 btrfs_dev_extent_length(leaf, extent) < start);
1003 ret = 0;
1004 } else if (ret == 0) {
1005 leaf = path->nodes[0];
1006 extent = btrfs_item_ptr(leaf, path->slots[0],
1007 struct btrfs_dev_extent);
1008 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001009 BUG_ON(ret);
1010
Chris Masondfe25022008-05-13 13:46:40 -04001011 if (device->bytes_used > 0)
1012 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -04001013 ret = btrfs_del_item(trans, root, path);
1014 BUG_ON(ret);
1015
1016 btrfs_free_path(path);
1017 return ret;
1018}
1019
Yan Zheng2b820322008-11-17 21:11:30 -05001020int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001021 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001022 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001023 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001024{
1025 int ret;
1026 struct btrfs_path *path;
1027 struct btrfs_root *root = device->dev_root;
1028 struct btrfs_dev_extent *extent;
1029 struct extent_buffer *leaf;
1030 struct btrfs_key key;
1031
Chris Masondfe25022008-05-13 13:46:40 -04001032 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001033 path = btrfs_alloc_path();
1034 if (!path)
1035 return -ENOMEM;
1036
Chris Mason0b86a832008-03-24 15:01:56 -04001037 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001038 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001039 key.type = BTRFS_DEV_EXTENT_KEY;
1040 ret = btrfs_insert_empty_item(trans, root, path, &key,
1041 sizeof(*extent));
1042 BUG_ON(ret);
1043
1044 leaf = path->nodes[0];
1045 extent = btrfs_item_ptr(leaf, path->slots[0],
1046 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001047 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1048 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1049 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1050
1051 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1052 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1053 BTRFS_UUID_SIZE);
1054
Chris Mason0b86a832008-03-24 15:01:56 -04001055 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1056 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001057 btrfs_free_path(path);
1058 return ret;
1059}
1060
Chris Masona1b32a52008-09-05 16:09:51 -04001061static noinline int find_next_chunk(struct btrfs_root *root,
1062 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001063{
1064 struct btrfs_path *path;
1065 int ret;
1066 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001067 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001068 struct btrfs_key found_key;
1069
1070 path = btrfs_alloc_path();
1071 BUG_ON(!path);
1072
Chris Masone17cade2008-04-15 15:41:47 -04001073 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001074 key.offset = (u64)-1;
1075 key.type = BTRFS_CHUNK_ITEM_KEY;
1076
1077 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1078 if (ret < 0)
1079 goto error;
1080
1081 BUG_ON(ret == 0);
1082
1083 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1084 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001085 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001086 } else {
1087 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1088 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001089 if (found_key.objectid != objectid)
1090 *offset = 0;
1091 else {
1092 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1093 struct btrfs_chunk);
1094 *offset = found_key.offset +
1095 btrfs_chunk_length(path->nodes[0], chunk);
1096 }
Chris Mason0b86a832008-03-24 15:01:56 -04001097 }
1098 ret = 0;
1099error:
1100 btrfs_free_path(path);
1101 return ret;
1102}
1103
Yan Zheng2b820322008-11-17 21:11:30 -05001104static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001105{
1106 int ret;
1107 struct btrfs_key key;
1108 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001109 struct btrfs_path *path;
1110
1111 root = root->fs_info->chunk_root;
1112
1113 path = btrfs_alloc_path();
1114 if (!path)
1115 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001116
1117 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1118 key.type = BTRFS_DEV_ITEM_KEY;
1119 key.offset = (u64)-1;
1120
1121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1122 if (ret < 0)
1123 goto error;
1124
1125 BUG_ON(ret == 0);
1126
1127 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1128 BTRFS_DEV_ITEM_KEY);
1129 if (ret) {
1130 *objectid = 1;
1131 } else {
1132 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1133 path->slots[0]);
1134 *objectid = found_key.offset + 1;
1135 }
1136 ret = 0;
1137error:
Yan Zheng2b820322008-11-17 21:11:30 -05001138 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001139 return ret;
1140}
1141
1142/*
1143 * the device information is stored in the chunk root
1144 * the btrfs_device struct should be fully filled in
1145 */
1146int btrfs_add_device(struct btrfs_trans_handle *trans,
1147 struct btrfs_root *root,
1148 struct btrfs_device *device)
1149{
1150 int ret;
1151 struct btrfs_path *path;
1152 struct btrfs_dev_item *dev_item;
1153 struct extent_buffer *leaf;
1154 struct btrfs_key key;
1155 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001156
1157 root = root->fs_info->chunk_root;
1158
1159 path = btrfs_alloc_path();
1160 if (!path)
1161 return -ENOMEM;
1162
Chris Mason0b86a832008-03-24 15:01:56 -04001163 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1164 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001165 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001166
1167 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001168 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001169 if (ret)
1170 goto out;
1171
1172 leaf = path->nodes[0];
1173 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1174
1175 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001176 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001177 btrfs_set_device_type(leaf, dev_item, device->type);
1178 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1179 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1180 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001181 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1182 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001183 btrfs_set_device_group(leaf, dev_item, 0);
1184 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1185 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001186 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001187
Chris Mason0b86a832008-03-24 15:01:56 -04001188 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001189 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001190 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1191 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001192 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001193
Yan Zheng2b820322008-11-17 21:11:30 -05001194 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001195out:
1196 btrfs_free_path(path);
1197 return ret;
1198}
Chris Mason8f18cf12008-04-25 16:53:30 -04001199
Chris Masona061fc82008-05-07 11:43:44 -04001200static int btrfs_rm_dev_item(struct btrfs_root *root,
1201 struct btrfs_device *device)
1202{
1203 int ret;
1204 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001205 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001206 struct btrfs_trans_handle *trans;
1207
1208 root = root->fs_info->chunk_root;
1209
1210 path = btrfs_alloc_path();
1211 if (!path)
1212 return -ENOMEM;
1213
Yan, Zhenga22285a2010-05-16 10:48:46 -04001214 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001215 if (IS_ERR(trans)) {
1216 btrfs_free_path(path);
1217 return PTR_ERR(trans);
1218 }
Chris Masona061fc82008-05-07 11:43:44 -04001219 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1220 key.type = BTRFS_DEV_ITEM_KEY;
1221 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001222 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001223
1224 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1225 if (ret < 0)
1226 goto out;
1227
1228 if (ret > 0) {
1229 ret = -ENOENT;
1230 goto out;
1231 }
1232
1233 ret = btrfs_del_item(trans, root, path);
1234 if (ret)
1235 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001236out:
1237 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001238 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001239 btrfs_commit_transaction(trans, root);
1240 return ret;
1241}
1242
1243int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1244{
1245 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001246 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001247 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001248 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001249 struct btrfs_super_block *disk_super;
1250 u64 all_avail;
1251 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001252 u64 num_devices;
1253 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001254 int ret = 0;
1255
Chris Masona061fc82008-05-07 11:43:44 -04001256 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001257 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001258
1259 all_avail = root->fs_info->avail_data_alloc_bits |
1260 root->fs_info->avail_system_alloc_bits |
1261 root->fs_info->avail_metadata_alloc_bits;
1262
1263 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001264 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001265 printk(KERN_ERR "btrfs: unable to go below four devices "
1266 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001267 ret = -EINVAL;
1268 goto out;
1269 }
1270
1271 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001272 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001273 printk(KERN_ERR "btrfs: unable to go below two "
1274 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001275 ret = -EINVAL;
1276 goto out;
1277 }
1278
Chris Masondfe25022008-05-13 13:46:40 -04001279 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001280 struct list_head *devices;
1281 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001282
Chris Masondfe25022008-05-13 13:46:40 -04001283 device = NULL;
1284 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001285 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001286 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001287 if (tmp->in_fs_metadata && !tmp->bdev) {
1288 device = tmp;
1289 break;
1290 }
1291 }
Chris Masone5e9a522009-06-10 15:17:02 -04001292 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001293 bdev = NULL;
1294 bh = NULL;
1295 disk_super = NULL;
1296 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001297 printk(KERN_ERR "btrfs: no missing devices found to "
1298 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001299 goto out;
1300 }
Chris Masondfe25022008-05-13 13:46:40 -04001301 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001302 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001303 root->fs_info->bdev_holder);
1304 if (IS_ERR(bdev)) {
1305 ret = PTR_ERR(bdev);
1306 goto out;
1307 }
1308
Yan Zheng2b820322008-11-17 21:11:30 -05001309 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001310 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001311 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001312 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001313 goto error_close;
1314 }
1315 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001316 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001317 dev_uuid = disk_super->dev_item.uuid;
1318 device = btrfs_find_device(root, devid, dev_uuid,
1319 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001320 if (!device) {
1321 ret = -ENOENT;
1322 goto error_brelse;
1323 }
Chris Masondfe25022008-05-13 13:46:40 -04001324 }
Yan Zheng2b820322008-11-17 21:11:30 -05001325
1326 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001327 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1328 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001329 ret = -EINVAL;
1330 goto error_brelse;
1331 }
1332
1333 if (device->writeable) {
1334 list_del_init(&device->dev_alloc_list);
1335 root->fs_info->fs_devices->rw_devices--;
1336 }
Chris Masona061fc82008-05-07 11:43:44 -04001337
1338 ret = btrfs_shrink_device(device, 0);
1339 if (ret)
1340 goto error_brelse;
1341
Chris Masona061fc82008-05-07 11:43:44 -04001342 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1343 if (ret)
1344 goto error_brelse;
1345
Yan Zheng2b820322008-11-17 21:11:30 -05001346 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001347
1348 /*
1349 * the device list mutex makes sure that we don't change
1350 * the device list while someone else is writing out all
1351 * the device supers.
1352 */
1353 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001354 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001355 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1356
Yan Zhenge4404d62008-12-12 10:03:26 -05001357 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001358
Chris Masoncd02dca2010-12-13 14:56:23 -05001359 if (device->missing)
1360 root->fs_info->fs_devices->missing_devices--;
1361
Yan Zheng2b820322008-11-17 21:11:30 -05001362 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1363 struct btrfs_device, dev_list);
1364 if (device->bdev == root->fs_info->sb->s_bdev)
1365 root->fs_info->sb->s_bdev = next_device->bdev;
1366 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1367 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1368
Yan Zhenge4404d62008-12-12 10:03:26 -05001369 if (device->bdev) {
1370 close_bdev_exclusive(device->bdev, device->mode);
1371 device->bdev = NULL;
1372 device->fs_devices->open_devices--;
1373 }
1374
Yan Zheng2b820322008-11-17 21:11:30 -05001375 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1376 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1377
Yan Zhenge4404d62008-12-12 10:03:26 -05001378 if (device->fs_devices->open_devices == 0) {
1379 struct btrfs_fs_devices *fs_devices;
1380 fs_devices = root->fs_info->fs_devices;
1381 while (fs_devices) {
1382 if (fs_devices->seed == device->fs_devices)
1383 break;
1384 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001385 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001386 fs_devices->seed = device->fs_devices->seed;
1387 device->fs_devices->seed = NULL;
1388 __btrfs_close_devices(device->fs_devices);
1389 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001390 }
1391
1392 /*
1393 * at this point, the device is zero sized. We want to
1394 * remove it from the devices list and zero out the old super
1395 */
1396 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001397 /* make sure this device isn't detected as part of
1398 * the FS anymore
1399 */
1400 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1401 set_buffer_dirty(bh);
1402 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001403 }
Chris Masona061fc82008-05-07 11:43:44 -04001404
Chris Masona061fc82008-05-07 11:43:44 -04001405 kfree(device->name);
1406 kfree(device);
1407 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001408
1409error_brelse:
1410 brelse(bh);
1411error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001412 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001413 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001414out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001415 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001416 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001417 return ret;
1418}
1419
Yan Zheng2b820322008-11-17 21:11:30 -05001420/*
1421 * does all the dirty work required for changing file system's UUID.
1422 */
1423static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1424 struct btrfs_root *root)
1425{
1426 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1427 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001428 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001429 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1430 struct btrfs_device *device;
1431 u64 super_flags;
1432
1433 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001434 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001435 return -EINVAL;
1436
Yan Zhenge4404d62008-12-12 10:03:26 -05001437 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1438 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001439 return -ENOMEM;
1440
Yan Zhenge4404d62008-12-12 10:03:26 -05001441 old_devices = clone_fs_devices(fs_devices);
1442 if (IS_ERR(old_devices)) {
1443 kfree(seed_devices);
1444 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001445 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001446
Yan Zheng2b820322008-11-17 21:11:30 -05001447 list_add(&old_devices->list, &fs_uuids);
1448
Yan Zhenge4404d62008-12-12 10:03:26 -05001449 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1450 seed_devices->opened = 1;
1451 INIT_LIST_HEAD(&seed_devices->devices);
1452 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001453 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001454 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1455 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1456 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1457 device->fs_devices = seed_devices;
1458 }
1459
Yan Zheng2b820322008-11-17 21:11:30 -05001460 fs_devices->seeding = 0;
1461 fs_devices->num_devices = 0;
1462 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001463 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001464
1465 generate_random_uuid(fs_devices->fsid);
1466 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1467 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1468 super_flags = btrfs_super_flags(disk_super) &
1469 ~BTRFS_SUPER_FLAG_SEEDING;
1470 btrfs_set_super_flags(disk_super, super_flags);
1471
1472 return 0;
1473}
1474
1475/*
1476 * strore the expected generation for seed devices in device items.
1477 */
1478static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1479 struct btrfs_root *root)
1480{
1481 struct btrfs_path *path;
1482 struct extent_buffer *leaf;
1483 struct btrfs_dev_item *dev_item;
1484 struct btrfs_device *device;
1485 struct btrfs_key key;
1486 u8 fs_uuid[BTRFS_UUID_SIZE];
1487 u8 dev_uuid[BTRFS_UUID_SIZE];
1488 u64 devid;
1489 int ret;
1490
1491 path = btrfs_alloc_path();
1492 if (!path)
1493 return -ENOMEM;
1494
1495 root = root->fs_info->chunk_root;
1496 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1497 key.offset = 0;
1498 key.type = BTRFS_DEV_ITEM_KEY;
1499
1500 while (1) {
1501 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1502 if (ret < 0)
1503 goto error;
1504
1505 leaf = path->nodes[0];
1506next_slot:
1507 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1508 ret = btrfs_next_leaf(root, path);
1509 if (ret > 0)
1510 break;
1511 if (ret < 0)
1512 goto error;
1513 leaf = path->nodes[0];
1514 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1515 btrfs_release_path(root, path);
1516 continue;
1517 }
1518
1519 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1520 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1521 key.type != BTRFS_DEV_ITEM_KEY)
1522 break;
1523
1524 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1525 struct btrfs_dev_item);
1526 devid = btrfs_device_id(leaf, dev_item);
1527 read_extent_buffer(leaf, dev_uuid,
1528 (unsigned long)btrfs_device_uuid(dev_item),
1529 BTRFS_UUID_SIZE);
1530 read_extent_buffer(leaf, fs_uuid,
1531 (unsigned long)btrfs_device_fsid(dev_item),
1532 BTRFS_UUID_SIZE);
1533 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1534 BUG_ON(!device);
1535
1536 if (device->fs_devices->seeding) {
1537 btrfs_set_device_generation(leaf, dev_item,
1538 device->generation);
1539 btrfs_mark_buffer_dirty(leaf);
1540 }
1541
1542 path->slots[0]++;
1543 goto next_slot;
1544 }
1545 ret = 0;
1546error:
1547 btrfs_free_path(path);
1548 return ret;
1549}
1550
Chris Mason788f20e2008-04-28 15:29:42 -04001551int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1552{
1553 struct btrfs_trans_handle *trans;
1554 struct btrfs_device *device;
1555 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001556 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001557 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001558 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001559 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001560 int ret = 0;
1561
Yan Zheng2b820322008-11-17 21:11:30 -05001562 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1563 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001564
Chris Mason15916de2008-11-19 21:17:22 -05001565 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001566 if (IS_ERR(bdev))
1567 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001568
Yan Zheng2b820322008-11-17 21:11:30 -05001569 if (root->fs_info->fs_devices->seeding) {
1570 seeding_dev = 1;
1571 down_write(&sb->s_umount);
1572 mutex_lock(&uuid_mutex);
1573 }
1574
Chris Mason8c8bee12008-09-29 11:19:10 -04001575 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001576 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001577
Chris Mason788f20e2008-04-28 15:29:42 -04001578 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001579 /*
1580 * we have the volume lock, so we don't need the extra
1581 * device list mutex while reading the list here.
1582 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001583 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001584 if (device->bdev == bdev) {
1585 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001586 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001587 }
1588 }
1589
1590 device = kzalloc(sizeof(*device), GFP_NOFS);
1591 if (!device) {
1592 /* we can safely leave the fs_devices entry around */
1593 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001594 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001595 }
1596
Chris Mason788f20e2008-04-28 15:29:42 -04001597 device->name = kstrdup(device_path, GFP_NOFS);
1598 if (!device->name) {
1599 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001600 ret = -ENOMEM;
1601 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001602 }
Yan Zheng2b820322008-11-17 21:11:30 -05001603
1604 ret = find_next_devid(root, &device->devid);
1605 if (ret) {
1606 kfree(device);
1607 goto error;
1608 }
1609
Yan, Zhenga22285a2010-05-16 10:48:46 -04001610 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001611 if (IS_ERR(trans)) {
1612 kfree(device);
1613 ret = PTR_ERR(trans);
1614 goto error;
1615 }
1616
Yan Zheng2b820322008-11-17 21:11:30 -05001617 lock_chunks(root);
1618
1619 device->barriers = 1;
1620 device->writeable = 1;
1621 device->work.func = pending_bios_fn;
1622 generate_random_uuid(device->uuid);
1623 spin_lock_init(&device->io_lock);
1624 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001625 device->io_width = root->sectorsize;
1626 device->io_align = root->sectorsize;
1627 device->sector_size = root->sectorsize;
1628 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001629 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001630 device->dev_root = root->fs_info->dev_root;
1631 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001632 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001633 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001634 set_blocksize(device->bdev, 4096);
1635
Yan Zheng2b820322008-11-17 21:11:30 -05001636 if (seeding_dev) {
1637 sb->s_flags &= ~MS_RDONLY;
1638 ret = btrfs_prepare_sprout(trans, root);
1639 BUG_ON(ret);
1640 }
1641
1642 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001643
1644 /*
1645 * we don't want write_supers to jump in here with our device
1646 * half setup
1647 */
1648 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001649 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1650 list_add(&device->dev_alloc_list,
1651 &root->fs_info->fs_devices->alloc_list);
1652 root->fs_info->fs_devices->num_devices++;
1653 root->fs_info->fs_devices->open_devices++;
1654 root->fs_info->fs_devices->rw_devices++;
1655 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1656
Chris Masonc2898112009-06-10 09:51:32 -04001657 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1658 root->fs_info->fs_devices->rotating = 1;
1659
Chris Mason788f20e2008-04-28 15:29:42 -04001660 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1661 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1662 total_bytes + device->total_bytes);
1663
1664 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1665 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1666 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001667 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001668
Yan Zheng2b820322008-11-17 21:11:30 -05001669 if (seeding_dev) {
1670 ret = init_first_rw_device(trans, root, device);
1671 BUG_ON(ret);
1672 ret = btrfs_finish_sprout(trans, root);
1673 BUG_ON(ret);
1674 } else {
1675 ret = btrfs_add_device(trans, root, device);
1676 }
1677
Chris Mason913d9522009-03-10 13:17:18 -04001678 /*
1679 * we've got more storage, clear any full flags on the space
1680 * infos
1681 */
1682 btrfs_clear_space_info_full(root->fs_info);
1683
Chris Mason7d9eb122008-07-08 14:19:17 -04001684 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001685 btrfs_commit_transaction(trans, root);
1686
1687 if (seeding_dev) {
1688 mutex_unlock(&uuid_mutex);
1689 up_write(&sb->s_umount);
1690
1691 ret = btrfs_relocate_sys_chunks(root);
1692 BUG_ON(ret);
1693 }
1694out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001695 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001696 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001697error:
Chris Mason15916de2008-11-19 21:17:22 -05001698 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001699 if (seeding_dev) {
1700 mutex_unlock(&uuid_mutex);
1701 up_write(&sb->s_umount);
1702 }
Chris Mason788f20e2008-04-28 15:29:42 -04001703 goto out;
1704}
1705
Chris Masond3977122009-01-05 21:25:51 -05001706static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1707 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001708{
1709 int ret;
1710 struct btrfs_path *path;
1711 struct btrfs_root *root;
1712 struct btrfs_dev_item *dev_item;
1713 struct extent_buffer *leaf;
1714 struct btrfs_key key;
1715
1716 root = device->dev_root->fs_info->chunk_root;
1717
1718 path = btrfs_alloc_path();
1719 if (!path)
1720 return -ENOMEM;
1721
1722 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1723 key.type = BTRFS_DEV_ITEM_KEY;
1724 key.offset = device->devid;
1725
1726 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1727 if (ret < 0)
1728 goto out;
1729
1730 if (ret > 0) {
1731 ret = -ENOENT;
1732 goto out;
1733 }
1734
1735 leaf = path->nodes[0];
1736 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1737
1738 btrfs_set_device_id(leaf, dev_item, device->devid);
1739 btrfs_set_device_type(leaf, dev_item, device->type);
1740 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1741 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1742 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001743 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001744 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1745 btrfs_mark_buffer_dirty(leaf);
1746
1747out:
1748 btrfs_free_path(path);
1749 return ret;
1750}
1751
Chris Mason7d9eb122008-07-08 14:19:17 -04001752static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001753 struct btrfs_device *device, u64 new_size)
1754{
1755 struct btrfs_super_block *super_copy =
1756 &device->dev_root->fs_info->super_copy;
1757 u64 old_total = btrfs_super_total_bytes(super_copy);
1758 u64 diff = new_size - device->total_bytes;
1759
Yan Zheng2b820322008-11-17 21:11:30 -05001760 if (!device->writeable)
1761 return -EACCES;
1762 if (new_size <= device->total_bytes)
1763 return -EINVAL;
1764
Chris Mason8f18cf12008-04-25 16:53:30 -04001765 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001766 device->fs_devices->total_rw_bytes += diff;
1767
1768 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001769 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001770 btrfs_clear_space_info_full(device->dev_root->fs_info);
1771
Chris Mason8f18cf12008-04-25 16:53:30 -04001772 return btrfs_update_device(trans, device);
1773}
1774
Chris Mason7d9eb122008-07-08 14:19:17 -04001775int btrfs_grow_device(struct btrfs_trans_handle *trans,
1776 struct btrfs_device *device, u64 new_size)
1777{
1778 int ret;
1779 lock_chunks(device->dev_root);
1780 ret = __btrfs_grow_device(trans, device, new_size);
1781 unlock_chunks(device->dev_root);
1782 return ret;
1783}
1784
Chris Mason8f18cf12008-04-25 16:53:30 -04001785static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1786 struct btrfs_root *root,
1787 u64 chunk_tree, u64 chunk_objectid,
1788 u64 chunk_offset)
1789{
1790 int ret;
1791 struct btrfs_path *path;
1792 struct btrfs_key key;
1793
1794 root = root->fs_info->chunk_root;
1795 path = btrfs_alloc_path();
1796 if (!path)
1797 return -ENOMEM;
1798
1799 key.objectid = chunk_objectid;
1800 key.offset = chunk_offset;
1801 key.type = BTRFS_CHUNK_ITEM_KEY;
1802
1803 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1804 BUG_ON(ret);
1805
1806 ret = btrfs_del_item(trans, root, path);
1807 BUG_ON(ret);
1808
1809 btrfs_free_path(path);
1810 return 0;
1811}
1812
Christoph Hellwigb2950862008-12-02 09:54:17 -05001813static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001814 chunk_offset)
1815{
1816 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1817 struct btrfs_disk_key *disk_key;
1818 struct btrfs_chunk *chunk;
1819 u8 *ptr;
1820 int ret = 0;
1821 u32 num_stripes;
1822 u32 array_size;
1823 u32 len = 0;
1824 u32 cur;
1825 struct btrfs_key key;
1826
1827 array_size = btrfs_super_sys_array_size(super_copy);
1828
1829 ptr = super_copy->sys_chunk_array;
1830 cur = 0;
1831
1832 while (cur < array_size) {
1833 disk_key = (struct btrfs_disk_key *)ptr;
1834 btrfs_disk_key_to_cpu(&key, disk_key);
1835
1836 len = sizeof(*disk_key);
1837
1838 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1839 chunk = (struct btrfs_chunk *)(ptr + len);
1840 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1841 len += btrfs_chunk_item_size(num_stripes);
1842 } else {
1843 ret = -EIO;
1844 break;
1845 }
1846 if (key.objectid == chunk_objectid &&
1847 key.offset == chunk_offset) {
1848 memmove(ptr, ptr + len, array_size - (cur + len));
1849 array_size -= len;
1850 btrfs_set_super_sys_array_size(super_copy, array_size);
1851 } else {
1852 ptr += len;
1853 cur += len;
1854 }
1855 }
1856 return ret;
1857}
1858
Christoph Hellwigb2950862008-12-02 09:54:17 -05001859static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001860 u64 chunk_tree, u64 chunk_objectid,
1861 u64 chunk_offset)
1862{
1863 struct extent_map_tree *em_tree;
1864 struct btrfs_root *extent_root;
1865 struct btrfs_trans_handle *trans;
1866 struct extent_map *em;
1867 struct map_lookup *map;
1868 int ret;
1869 int i;
1870
1871 root = root->fs_info->chunk_root;
1872 extent_root = root->fs_info->extent_root;
1873 em_tree = &root->fs_info->mapping_tree.map_tree;
1874
Josef Bacikba1bf482009-09-11 16:11:19 -04001875 ret = btrfs_can_relocate(extent_root, chunk_offset);
1876 if (ret)
1877 return -ENOSPC;
1878
Chris Mason8f18cf12008-04-25 16:53:30 -04001879 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001880 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001881 if (ret)
1882 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001883
Yan, Zhenga22285a2010-05-16 10:48:46 -04001884 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001885 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001886
Chris Mason7d9eb122008-07-08 14:19:17 -04001887 lock_chunks(root);
1888
Chris Mason8f18cf12008-04-25 16:53:30 -04001889 /*
1890 * step two, delete the device extents and the
1891 * chunk tree entries
1892 */
Chris Mason890871b2009-09-02 16:24:52 -04001893 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001894 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001895 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001896
Chris Masona061fc82008-05-07 11:43:44 -04001897 BUG_ON(em->start > chunk_offset ||
1898 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001899 map = (struct map_lookup *)em->bdev;
1900
1901 for (i = 0; i < map->num_stripes; i++) {
1902 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1903 map->stripes[i].physical);
1904 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001905
Chris Masondfe25022008-05-13 13:46:40 -04001906 if (map->stripes[i].dev) {
1907 ret = btrfs_update_device(trans, map->stripes[i].dev);
1908 BUG_ON(ret);
1909 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001910 }
1911 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1912 chunk_offset);
1913
1914 BUG_ON(ret);
1915
1916 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1917 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1918 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001919 }
1920
Zheng Yan1a40e232008-09-26 10:09:34 -04001921 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1922 BUG_ON(ret);
1923
Chris Mason890871b2009-09-02 16:24:52 -04001924 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001925 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001926 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001927
Chris Mason8f18cf12008-04-25 16:53:30 -04001928 kfree(map);
1929 em->bdev = NULL;
1930
1931 /* once for the tree */
1932 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001933 /* once for us */
1934 free_extent_map(em);
1935
Chris Mason7d9eb122008-07-08 14:19:17 -04001936 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001937 btrfs_end_transaction(trans, root);
1938 return 0;
1939}
1940
Yan Zheng2b820322008-11-17 21:11:30 -05001941static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1942{
1943 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1944 struct btrfs_path *path;
1945 struct extent_buffer *leaf;
1946 struct btrfs_chunk *chunk;
1947 struct btrfs_key key;
1948 struct btrfs_key found_key;
1949 u64 chunk_tree = chunk_root->root_key.objectid;
1950 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001951 bool retried = false;
1952 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001953 int ret;
1954
1955 path = btrfs_alloc_path();
1956 if (!path)
1957 return -ENOMEM;
1958
Josef Bacikba1bf482009-09-11 16:11:19 -04001959again:
Yan Zheng2b820322008-11-17 21:11:30 -05001960 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1961 key.offset = (u64)-1;
1962 key.type = BTRFS_CHUNK_ITEM_KEY;
1963
1964 while (1) {
1965 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1966 if (ret < 0)
1967 goto error;
1968 BUG_ON(ret == 0);
1969
1970 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1971 key.type);
1972 if (ret < 0)
1973 goto error;
1974 if (ret > 0)
1975 break;
1976
1977 leaf = path->nodes[0];
1978 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1979
1980 chunk = btrfs_item_ptr(leaf, path->slots[0],
1981 struct btrfs_chunk);
1982 chunk_type = btrfs_chunk_type(leaf, chunk);
1983 btrfs_release_path(chunk_root, path);
1984
1985 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1986 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1987 found_key.objectid,
1988 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001989 if (ret == -ENOSPC)
1990 failed++;
1991 else if (ret)
1992 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001993 }
1994
1995 if (found_key.offset == 0)
1996 break;
1997 key.offset = found_key.offset - 1;
1998 }
1999 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002000 if (failed && !retried) {
2001 failed = 0;
2002 retried = true;
2003 goto again;
2004 } else if (failed && retried) {
2005 WARN_ON(1);
2006 ret = -ENOSPC;
2007 }
Yan Zheng2b820322008-11-17 21:11:30 -05002008error:
2009 btrfs_free_path(path);
2010 return ret;
2011}
2012
Chris Masonec44a352008-04-28 15:29:52 -04002013static u64 div_factor(u64 num, int factor)
2014{
2015 if (factor == 10)
2016 return num;
2017 num *= factor;
2018 do_div(num, 10);
2019 return num;
2020}
2021
Chris Masonec44a352008-04-28 15:29:52 -04002022int btrfs_balance(struct btrfs_root *dev_root)
2023{
2024 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002025 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2026 struct btrfs_device *device;
2027 u64 old_size;
2028 u64 size_to_free;
2029 struct btrfs_path *path;
2030 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002031 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2032 struct btrfs_trans_handle *trans;
2033 struct btrfs_key found_key;
2034
Yan Zheng2b820322008-11-17 21:11:30 -05002035 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2036 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002037
Ben Hutchings6f88a442010-12-29 14:55:03 +00002038 if (!capable(CAP_SYS_ADMIN))
2039 return -EPERM;
2040
Chris Mason7d9eb122008-07-08 14:19:17 -04002041 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002042 dev_root = dev_root->fs_info->dev_root;
2043
Chris Masonec44a352008-04-28 15:29:52 -04002044 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002045 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002046 old_size = device->total_bytes;
2047 size_to_free = div_factor(old_size, 1);
2048 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002049 if (!device->writeable ||
2050 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002051 continue;
2052
2053 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002054 if (ret == -ENOSPC)
2055 break;
Chris Masonec44a352008-04-28 15:29:52 -04002056 BUG_ON(ret);
2057
Yan, Zhenga22285a2010-05-16 10:48:46 -04002058 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002059 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002060
2061 ret = btrfs_grow_device(trans, device, old_size);
2062 BUG_ON(ret);
2063
2064 btrfs_end_transaction(trans, dev_root);
2065 }
2066
2067 /* step two, relocate all the chunks */
2068 path = btrfs_alloc_path();
2069 BUG_ON(!path);
2070
2071 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2072 key.offset = (u64)-1;
2073 key.type = BTRFS_CHUNK_ITEM_KEY;
2074
Chris Masond3977122009-01-05 21:25:51 -05002075 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002076 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2077 if (ret < 0)
2078 goto error;
2079
2080 /*
2081 * this shouldn't happen, it means the last relocate
2082 * failed
2083 */
2084 if (ret == 0)
2085 break;
2086
2087 ret = btrfs_previous_item(chunk_root, path, 0,
2088 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002089 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002090 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002091
Chris Masonec44a352008-04-28 15:29:52 -04002092 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2093 path->slots[0]);
2094 if (found_key.objectid != key.objectid)
2095 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002096
Chris Masonec44a352008-04-28 15:29:52 -04002097 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002098 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002099 break;
2100
Chris Mason7d9eb122008-07-08 14:19:17 -04002101 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002102 ret = btrfs_relocate_chunk(chunk_root,
2103 chunk_root->root_key.objectid,
2104 found_key.objectid,
2105 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002106 BUG_ON(ret && ret != -ENOSPC);
2107 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002108 }
2109 ret = 0;
2110error:
2111 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002112 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002113 return ret;
2114}
2115
Chris Mason8f18cf12008-04-25 16:53:30 -04002116/*
2117 * shrinking a device means finding all of the device extents past
2118 * the new size, and then following the back refs to the chunks.
2119 * The chunk relocation code actually frees the device extent
2120 */
2121int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2122{
2123 struct btrfs_trans_handle *trans;
2124 struct btrfs_root *root = device->dev_root;
2125 struct btrfs_dev_extent *dev_extent = NULL;
2126 struct btrfs_path *path;
2127 u64 length;
2128 u64 chunk_tree;
2129 u64 chunk_objectid;
2130 u64 chunk_offset;
2131 int ret;
2132 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002133 int failed = 0;
2134 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002135 struct extent_buffer *l;
2136 struct btrfs_key key;
2137 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2138 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002139 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002140 u64 diff = device->total_bytes - new_size;
2141
Yan Zheng2b820322008-11-17 21:11:30 -05002142 if (new_size >= device->total_bytes)
2143 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002144
2145 path = btrfs_alloc_path();
2146 if (!path)
2147 return -ENOMEM;
2148
Chris Mason8f18cf12008-04-25 16:53:30 -04002149 path->reada = 2;
2150
Chris Mason7d9eb122008-07-08 14:19:17 -04002151 lock_chunks(root);
2152
Chris Mason8f18cf12008-04-25 16:53:30 -04002153 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002154 if (device->writeable)
2155 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002156 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002157
Josef Bacikba1bf482009-09-11 16:11:19 -04002158again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002159 key.objectid = device->devid;
2160 key.offset = (u64)-1;
2161 key.type = BTRFS_DEV_EXTENT_KEY;
2162
2163 while (1) {
2164 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2165 if (ret < 0)
2166 goto done;
2167
2168 ret = btrfs_previous_item(root, path, 0, key.type);
2169 if (ret < 0)
2170 goto done;
2171 if (ret) {
2172 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002173 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002174 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002175 }
2176
2177 l = path->nodes[0];
2178 slot = path->slots[0];
2179 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2180
Josef Bacikba1bf482009-09-11 16:11:19 -04002181 if (key.objectid != device->devid) {
2182 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002183 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002184 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002185
2186 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2187 length = btrfs_dev_extent_length(l, dev_extent);
2188
Josef Bacikba1bf482009-09-11 16:11:19 -04002189 if (key.offset + length <= new_size) {
2190 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002191 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002192 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002193
2194 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2195 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2196 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2197 btrfs_release_path(root, path);
2198
2199 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2200 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002201 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002202 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002203 if (ret == -ENOSPC)
2204 failed++;
2205 key.offset -= 1;
2206 }
2207
2208 if (failed && !retried) {
2209 failed = 0;
2210 retried = true;
2211 goto again;
2212 } else if (failed && retried) {
2213 ret = -ENOSPC;
2214 lock_chunks(root);
2215
2216 device->total_bytes = old_size;
2217 if (device->writeable)
2218 device->fs_devices->total_rw_bytes += diff;
2219 unlock_chunks(root);
2220 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002221 }
2222
Chris Balld6397ba2009-04-27 07:29:03 -04002223 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002224 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002225 if (IS_ERR(trans)) {
2226 ret = PTR_ERR(trans);
2227 goto done;
2228 }
2229
Chris Balld6397ba2009-04-27 07:29:03 -04002230 lock_chunks(root);
2231
2232 device->disk_total_bytes = new_size;
2233 /* Now btrfs_update_device() will change the on-disk size. */
2234 ret = btrfs_update_device(trans, device);
2235 if (ret) {
2236 unlock_chunks(root);
2237 btrfs_end_transaction(trans, root);
2238 goto done;
2239 }
2240 WARN_ON(diff > old_total);
2241 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2242 unlock_chunks(root);
2243 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002244done:
2245 btrfs_free_path(path);
2246 return ret;
2247}
2248
Christoph Hellwigb2950862008-12-02 09:54:17 -05002249static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002250 struct btrfs_root *root,
2251 struct btrfs_key *key,
2252 struct btrfs_chunk *chunk, int item_size)
2253{
2254 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2255 struct btrfs_disk_key disk_key;
2256 u32 array_size;
2257 u8 *ptr;
2258
2259 array_size = btrfs_super_sys_array_size(super_copy);
2260 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2261 return -EFBIG;
2262
2263 ptr = super_copy->sys_chunk_array + array_size;
2264 btrfs_cpu_key_to_disk(&disk_key, key);
2265 memcpy(ptr, &disk_key, sizeof(disk_key));
2266 ptr += sizeof(disk_key);
2267 memcpy(ptr, chunk, item_size);
2268 item_size += sizeof(disk_key);
2269 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2270 return 0;
2271}
2272
Chris Masond3977122009-01-05 21:25:51 -05002273static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002274 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002275{
2276 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2277 return calc_size;
2278 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2279 return calc_size * (num_stripes / sub_stripes);
2280 else
2281 return calc_size * num_stripes;
2282}
2283
Miao Xieb2117a32011-01-05 10:07:28 +00002284/* Used to sort the devices by max_avail(descending sort) */
2285int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
Chris Mason0b86a832008-03-24 15:01:56 -04002286{
Miao Xieb2117a32011-01-05 10:07:28 +00002287 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2288 ((struct btrfs_device_info *)dev_info2)->max_avail)
2289 return -1;
2290 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2291 ((struct btrfs_device_info *)dev_info2)->max_avail)
2292 return 1;
2293 else
2294 return 0;
2295}
Chris Mason0b86a832008-03-24 15:01:56 -04002296
Miao Xieb2117a32011-01-05 10:07:28 +00002297static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2298 int *num_stripes, int *min_stripes,
2299 int *sub_stripes)
2300{
2301 *num_stripes = 1;
2302 *min_stripes = 1;
2303 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002304
Chris Masona40a90a2008-04-18 11:55:51 -04002305 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002306 *num_stripes = fs_devices->rw_devices;
2307 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002308 }
2309 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002310 *num_stripes = 2;
2311 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002312 }
Chris Mason8790d502008-04-03 16:29:03 -04002313 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002314 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002315 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002316 *num_stripes = 2;
2317 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002318 }
Chris Mason321aecc2008-04-16 10:49:51 -04002319 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002320 *num_stripes = fs_devices->rw_devices;
2321 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002322 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002323 *num_stripes &= ~(u32)1;
2324 *sub_stripes = 2;
2325 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002326 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002327
Miao Xieb2117a32011-01-05 10:07:28 +00002328 return 0;
2329}
2330
2331static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2332 u64 proposed_size, u64 type,
2333 int num_stripes, int small_stripe)
2334{
2335 int min_stripe_size = 1 * 1024 * 1024;
2336 u64 calc_size = proposed_size;
2337 u64 max_chunk_size = calc_size;
2338 int ncopies = 1;
2339
2340 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2341 BTRFS_BLOCK_GROUP_DUP |
2342 BTRFS_BLOCK_GROUP_RAID10))
2343 ncopies = 2;
2344
Chris Mason9b3f68b2008-04-18 10:29:51 -04002345 if (type & BTRFS_BLOCK_GROUP_DATA) {
2346 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002347 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002348 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002349 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002350 min_stripe_size = 32 * 1024 * 1024;
2351 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2352 calc_size = 8 * 1024 * 1024;
2353 max_chunk_size = calc_size * 2;
2354 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002355 }
2356
Yan Zheng2b820322008-11-17 21:11:30 -05002357 /* we don't want a chunk larger than 10% of writeable space */
2358 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2359 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002360
Miao Xie1974a3b2011-01-05 10:07:24 +00002361 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2362 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002363 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002364 do_div(calc_size, BTRFS_STRIPE_LEN);
2365 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002366 }
Josef Bacik0cad8a12010-03-17 20:45:56 +00002367
Chris Mason9b3f68b2008-04-18 10:29:51 -04002368 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002369 if (!small_stripe)
Josef Bacik0cad8a12010-03-17 20:45:56 +00002370 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002371
Chris Mason9f680ce2010-04-06 09:37:47 -04002372 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002373 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002374 * we end up with something bigger than a stripe
2375 */
Miao Xieb2117a32011-01-05 10:07:28 +00002376 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002377
Miao Xieb2117a32011-01-05 10:07:28 +00002378 do_div(calc_size, BTRFS_STRIPE_LEN);
2379 calc_size *= BTRFS_STRIPE_LEN;
2380
2381 return calc_size;
2382}
2383
2384static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2385 int num_stripes)
2386{
2387 struct map_lookup *new;
2388 size_t len = map_lookup_size(num_stripes);
2389
2390 BUG_ON(map->num_stripes < num_stripes);
2391
2392 if (map->num_stripes == num_stripes)
2393 return map;
2394
2395 new = kmalloc(len, GFP_NOFS);
2396 if (!new) {
2397 /* just change map->num_stripes */
2398 map->num_stripes = num_stripes;
2399 return map;
2400 }
2401
2402 memcpy(new, map, len);
2403 new->num_stripes = num_stripes;
2404 kfree(map);
2405 return new;
2406}
2407
2408/*
2409 * helper to allocate device space from btrfs_device_info, in which we stored
2410 * max free space information of every device. It is used when we can not
2411 * allocate chunks by default size.
2412 *
2413 * By this helper, we can allocate a new chunk as larger as possible.
2414 */
2415static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2416 struct btrfs_fs_devices *fs_devices,
2417 struct btrfs_device_info *devices,
2418 int nr_device, u64 type,
2419 struct map_lookup **map_lookup,
2420 int min_stripes, u64 *stripe_size)
2421{
2422 int i, index, sort_again = 0;
2423 int min_devices = min_stripes;
2424 u64 max_avail, min_free;
2425 struct map_lookup *map = *map_lookup;
2426 int ret;
2427
2428 if (nr_device < min_stripes)
2429 return -ENOSPC;
2430
2431 btrfs_descending_sort_devices(devices, nr_device);
2432
2433 max_avail = devices[0].max_avail;
2434 if (!max_avail)
2435 return -ENOSPC;
2436
2437 for (i = 0; i < nr_device; i++) {
2438 /*
2439 * if dev_offset = 0, it means the free space of this device
2440 * is less than what we need, and we didn't search max avail
2441 * extent on this device, so do it now.
2442 */
2443 if (!devices[i].dev_offset) {
2444 ret = find_free_dev_extent(trans, devices[i].dev,
2445 max_avail,
2446 &devices[i].dev_offset,
2447 &devices[i].max_avail);
2448 if (ret != 0 && ret != -ENOSPC)
2449 return ret;
2450 sort_again = 1;
2451 }
2452 }
2453
2454 /* we update the max avail free extent of each devices, sort again */
2455 if (sort_again)
2456 btrfs_descending_sort_devices(devices, nr_device);
2457
2458 if (type & BTRFS_BLOCK_GROUP_DUP)
2459 min_devices = 1;
2460
2461 if (!devices[min_devices - 1].max_avail)
2462 return -ENOSPC;
2463
2464 max_avail = devices[min_devices - 1].max_avail;
2465 if (type & BTRFS_BLOCK_GROUP_DUP)
2466 do_div(max_avail, 2);
2467
2468 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2469 min_stripes, 1);
2470 if (type & BTRFS_BLOCK_GROUP_DUP)
2471 min_free = max_avail * 2;
2472 else
2473 min_free = max_avail;
2474
2475 if (min_free > devices[min_devices - 1].max_avail)
2476 return -ENOSPC;
2477
2478 map = __shrink_map_lookup_stripes(map, min_stripes);
2479 *stripe_size = max_avail;
2480
2481 index = 0;
2482 for (i = 0; i < min_stripes; i++) {
2483 map->stripes[i].dev = devices[index].dev;
2484 map->stripes[i].physical = devices[index].dev_offset;
2485 if (type & BTRFS_BLOCK_GROUP_DUP) {
2486 i++;
2487 map->stripes[i].dev = devices[index].dev;
2488 map->stripes[i].physical = devices[index].dev_offset +
2489 max_avail;
2490 }
2491 index++;
2492 }
2493 *map_lookup = map;
2494
2495 return 0;
2496}
2497
2498static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2499 struct btrfs_root *extent_root,
2500 struct map_lookup **map_ret,
2501 u64 *num_bytes, u64 *stripe_size,
2502 u64 start, u64 type)
2503{
2504 struct btrfs_fs_info *info = extent_root->fs_info;
2505 struct btrfs_device *device = NULL;
2506 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2507 struct list_head *cur;
2508 struct map_lookup *map;
2509 struct extent_map_tree *em_tree;
2510 struct extent_map *em;
2511 struct btrfs_device_info *devices_info;
2512 struct list_head private_devs;
2513 u64 calc_size = 1024 * 1024 * 1024;
2514 u64 min_free;
2515 u64 avail;
2516 u64 dev_offset;
2517 int num_stripes;
2518 int min_stripes;
2519 int sub_stripes;
2520 int min_devices; /* the min number of devices we need */
2521 int i;
2522 int ret;
2523 int index;
2524
2525 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2526 (type & BTRFS_BLOCK_GROUP_DUP)) {
2527 WARN_ON(1);
2528 type &= ~BTRFS_BLOCK_GROUP_DUP;
2529 }
2530 if (list_empty(&fs_devices->alloc_list))
2531 return -ENOSPC;
2532
2533 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2534 &min_stripes, &sub_stripes);
2535 if (ret)
2536 return ret;
2537
2538 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2539 GFP_NOFS);
2540 if (!devices_info)
2541 return -ENOMEM;
2542
2543 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2544 if (!map) {
2545 ret = -ENOMEM;
2546 goto error;
2547 }
2548 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002549
Yan Zheng2b820322008-11-17 21:11:30 -05002550 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002551 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002552 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002553
Miao Xieb2117a32011-01-05 10:07:28 +00002554 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2555 num_stripes, 0);
2556
2557 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002558 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002559 min_devices = 1;
2560 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002561 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002562 min_devices = min_stripes;
2563 }
Chris Masonad5bd912008-04-21 08:28:10 -04002564
Yan Zheng2b820322008-11-17 21:11:30 -05002565 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002566 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002567 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002568 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002569 if (device->total_bytes > device->bytes_used)
2570 avail = device->total_bytes - device->bytes_used;
2571 else
2572 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002573 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002574
Chris Masondfe25022008-05-13 13:46:40 -04002575 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002576 ret = find_free_dev_extent(trans, device, min_free,
2577 &devices_info[i].dev_offset,
2578 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002579 if (ret == 0) {
2580 list_move_tail(&device->dev_alloc_list,
2581 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002582 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002583 map->stripes[index].physical =
2584 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002585 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002586 if (type & BTRFS_BLOCK_GROUP_DUP) {
2587 map->stripes[index].dev = device;
2588 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002589 devices_info[i].dev_offset +
2590 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002591 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002592 }
Miao Xieb2117a32011-01-05 10:07:28 +00002593 } else if (ret != -ENOSPC)
2594 goto error;
2595
2596 devices_info[i].dev = device;
2597 i++;
2598 } else if (device->in_fs_metadata &&
2599 avail >= BTRFS_STRIPE_LEN) {
2600 devices_info[i].dev = device;
2601 devices_info[i].max_avail = avail;
2602 i++;
2603 }
2604
Yan Zheng2b820322008-11-17 21:11:30 -05002605 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002606 break;
2607 }
Miao Xieb2117a32011-01-05 10:07:28 +00002608
Yan Zheng2b820322008-11-17 21:11:30 -05002609 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002610 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002611 if (index >= min_stripes) {
2612 num_stripes = index;
2613 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2614 num_stripes /= sub_stripes;
2615 num_stripes *= sub_stripes;
2616 }
Miao Xieb2117a32011-01-05 10:07:28 +00002617
2618 map = __shrink_map_lookup_stripes(map, num_stripes);
2619 } else if (i >= min_devices) {
2620 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2621 devices_info, i, type,
2622 &map, min_stripes,
2623 &calc_size);
2624 if (ret)
2625 goto error;
2626 } else {
2627 ret = -ENOSPC;
2628 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002629 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002630 }
Chris Mason593060d2008-03-25 16:50:33 -04002631 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002632 map->stripe_len = BTRFS_STRIPE_LEN;
2633 map->io_align = BTRFS_STRIPE_LEN;
2634 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002635 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002636 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002637
Yan Zheng2b820322008-11-17 21:11:30 -05002638 *map_ret = map;
2639 *stripe_size = calc_size;
2640 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002641 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002642
2643 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002644 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002645 ret = -ENOMEM;
2646 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002647 }
Chris Mason0b86a832008-03-24 15:01:56 -04002648 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002649 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002650 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002651 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002652 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002653
Chris Mason0b86a832008-03-24 15:01:56 -04002654 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002655 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002656 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002657 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002658 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002659 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002660
2661 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2662 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2663 start, *num_bytes);
2664 BUG_ON(ret);
2665
2666 index = 0;
2667 while (index < map->num_stripes) {
2668 device = map->stripes[index].dev;
2669 dev_offset = map->stripes[index].physical;
2670
2671 ret = btrfs_alloc_dev_extent(trans, device,
2672 info->chunk_root->root_key.objectid,
2673 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2674 start, dev_offset, calc_size);
2675 BUG_ON(ret);
2676 index++;
2677 }
2678
Miao Xieb2117a32011-01-05 10:07:28 +00002679 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002680 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002681
2682error:
2683 kfree(map);
2684 kfree(devices_info);
2685 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002686}
2687
2688static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2689 struct btrfs_root *extent_root,
2690 struct map_lookup *map, u64 chunk_offset,
2691 u64 chunk_size, u64 stripe_size)
2692{
2693 u64 dev_offset;
2694 struct btrfs_key key;
2695 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2696 struct btrfs_device *device;
2697 struct btrfs_chunk *chunk;
2698 struct btrfs_stripe *stripe;
2699 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2700 int index = 0;
2701 int ret;
2702
2703 chunk = kzalloc(item_size, GFP_NOFS);
2704 if (!chunk)
2705 return -ENOMEM;
2706
2707 index = 0;
2708 while (index < map->num_stripes) {
2709 device = map->stripes[index].dev;
2710 device->bytes_used += stripe_size;
2711 ret = btrfs_update_device(trans, device);
2712 BUG_ON(ret);
2713 index++;
2714 }
2715
2716 index = 0;
2717 stripe = &chunk->stripe;
2718 while (index < map->num_stripes) {
2719 device = map->stripes[index].dev;
2720 dev_offset = map->stripes[index].physical;
2721
2722 btrfs_set_stack_stripe_devid(stripe, device->devid);
2723 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2724 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2725 stripe++;
2726 index++;
2727 }
2728
2729 btrfs_set_stack_chunk_length(chunk, chunk_size);
2730 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2731 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2732 btrfs_set_stack_chunk_type(chunk, map->type);
2733 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2734 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2735 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2736 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2737 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2738
2739 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2740 key.type = BTRFS_CHUNK_ITEM_KEY;
2741 key.offset = chunk_offset;
2742
2743 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2744 BUG_ON(ret);
2745
2746 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2747 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2748 item_size);
2749 BUG_ON(ret);
2750 }
2751 kfree(chunk);
2752 return 0;
2753}
2754
2755/*
2756 * Chunk allocation falls into two parts. The first part does works
2757 * that make the new allocated chunk useable, but not do any operation
2758 * that modifies the chunk tree. The second part does the works that
2759 * require modifying the chunk tree. This division is important for the
2760 * bootstrap process of adding storage to a seed btrfs.
2761 */
2762int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2763 struct btrfs_root *extent_root, u64 type)
2764{
2765 u64 chunk_offset;
2766 u64 chunk_size;
2767 u64 stripe_size;
2768 struct map_lookup *map;
2769 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2770 int ret;
2771
2772 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2773 &chunk_offset);
2774 if (ret)
2775 return ret;
2776
2777 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2778 &stripe_size, chunk_offset, type);
2779 if (ret)
2780 return ret;
2781
2782 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2783 chunk_size, stripe_size);
2784 BUG_ON(ret);
2785 return 0;
2786}
2787
Chris Masond3977122009-01-05 21:25:51 -05002788static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002789 struct btrfs_root *root,
2790 struct btrfs_device *device)
2791{
2792 u64 chunk_offset;
2793 u64 sys_chunk_offset;
2794 u64 chunk_size;
2795 u64 sys_chunk_size;
2796 u64 stripe_size;
2797 u64 sys_stripe_size;
2798 u64 alloc_profile;
2799 struct map_lookup *map;
2800 struct map_lookup *sys_map;
2801 struct btrfs_fs_info *fs_info = root->fs_info;
2802 struct btrfs_root *extent_root = fs_info->extent_root;
2803 int ret;
2804
2805 ret = find_next_chunk(fs_info->chunk_root,
2806 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2807 BUG_ON(ret);
2808
2809 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2810 (fs_info->metadata_alloc_profile &
2811 fs_info->avail_metadata_alloc_bits);
2812 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2813
2814 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2815 &stripe_size, chunk_offset, alloc_profile);
2816 BUG_ON(ret);
2817
2818 sys_chunk_offset = chunk_offset + chunk_size;
2819
2820 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2821 (fs_info->system_alloc_profile &
2822 fs_info->avail_system_alloc_bits);
2823 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2824
2825 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2826 &sys_chunk_size, &sys_stripe_size,
2827 sys_chunk_offset, alloc_profile);
2828 BUG_ON(ret);
2829
2830 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2831 BUG_ON(ret);
2832
2833 /*
2834 * Modifying chunk tree needs allocating new blocks from both
2835 * system block group and metadata block group. So we only can
2836 * do operations require modifying the chunk tree after both
2837 * block groups were created.
2838 */
2839 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2840 chunk_size, stripe_size);
2841 BUG_ON(ret);
2842
2843 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2844 sys_chunk_offset, sys_chunk_size,
2845 sys_stripe_size);
2846 BUG_ON(ret);
2847 return 0;
2848}
2849
2850int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2851{
2852 struct extent_map *em;
2853 struct map_lookup *map;
2854 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2855 int readonly = 0;
2856 int i;
2857
Chris Mason890871b2009-09-02 16:24:52 -04002858 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002859 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002860 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002861 if (!em)
2862 return 1;
2863
Josef Bacikf48b9072010-01-27 02:07:59 +00002864 if (btrfs_test_opt(root, DEGRADED)) {
2865 free_extent_map(em);
2866 return 0;
2867 }
2868
Yan Zheng2b820322008-11-17 21:11:30 -05002869 map = (struct map_lookup *)em->bdev;
2870 for (i = 0; i < map->num_stripes; i++) {
2871 if (!map->stripes[i].dev->writeable) {
2872 readonly = 1;
2873 break;
2874 }
2875 }
2876 free_extent_map(em);
2877 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002878}
2879
2880void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2881{
2882 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2883}
2884
2885void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2886{
2887 struct extent_map *em;
2888
Chris Masond3977122009-01-05 21:25:51 -05002889 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002890 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002891 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2892 if (em)
2893 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002894 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002895 if (!em)
2896 break;
2897 kfree(em->bdev);
2898 /* once for us */
2899 free_extent_map(em);
2900 /* once for the tree */
2901 free_extent_map(em);
2902 }
2903}
2904
Chris Masonf1885912008-04-09 16:28:12 -04002905int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2906{
2907 struct extent_map *em;
2908 struct map_lookup *map;
2909 struct extent_map_tree *em_tree = &map_tree->map_tree;
2910 int ret;
2911
Chris Mason890871b2009-09-02 16:24:52 -04002912 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002913 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002914 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002915 BUG_ON(!em);
2916
2917 BUG_ON(em->start > logical || em->start + em->len < logical);
2918 map = (struct map_lookup *)em->bdev;
2919 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2920 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002921 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2922 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002923 else
2924 ret = 1;
2925 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002926 return ret;
2927}
2928
Chris Masondfe25022008-05-13 13:46:40 -04002929static int find_live_mirror(struct map_lookup *map, int first, int num,
2930 int optimal)
2931{
2932 int i;
2933 if (map->stripes[optimal].dev->bdev)
2934 return optimal;
2935 for (i = first; i < first + num; i++) {
2936 if (map->stripes[i].dev->bdev)
2937 return i;
2938 }
2939 /* we couldn't find one that doesn't fail. Just return something
2940 * and the io error handling code will clean up eventually
2941 */
2942 return optimal;
2943}
2944
Chris Masonf2d8d742008-04-21 10:03:05 -04002945static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2946 u64 logical, u64 *length,
2947 struct btrfs_multi_bio **multi_ret,
2948 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002949{
2950 struct extent_map *em;
2951 struct map_lookup *map;
2952 struct extent_map_tree *em_tree = &map_tree->map_tree;
2953 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002954 u64 stripe_offset;
2955 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002956 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002957 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002958 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002959 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002960 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002961 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002962 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002963
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002964 if (multi_ret && !(rw & REQ_WRITE))
Chris Masoncea9e442008-04-09 16:28:12 -04002965 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002966again:
2967 if (multi_ret) {
2968 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2969 GFP_NOFS);
2970 if (!multi)
2971 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002972
2973 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002974 }
Chris Mason0b86a832008-03-24 15:01:56 -04002975
Chris Mason890871b2009-09-02 16:24:52 -04002976 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002977 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002978 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002979
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002980 if (!em && unplug_page) {
2981 kfree(multi);
Chris Masonf2d8d742008-04-21 10:03:05 -04002982 return 0;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002983 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002984
Chris Mason3b951512008-04-17 11:29:12 -04002985 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002986 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2987 (unsigned long long)logical,
2988 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002989 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002990 }
Chris Mason0b86a832008-03-24 15:01:56 -04002991
2992 BUG_ON(em->start > logical || em->start + em->len < logical);
2993 map = (struct map_lookup *)em->bdev;
2994 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002995
Chris Masonf1885912008-04-09 16:28:12 -04002996 if (mirror_num > map->num_stripes)
2997 mirror_num = 0;
2998
Chris Masoncea9e442008-04-09 16:28:12 -04002999 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003000 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04003001 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3002 BTRFS_BLOCK_GROUP_DUP)) {
3003 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003004 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003005 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3006 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003007 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003008 }
3009 }
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003010 if (multi_ret && (rw & REQ_WRITE) &&
Chris Mason321aecc2008-04-16 10:49:51 -04003011 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04003012 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003013 free_extent_map(em);
3014 kfree(multi);
3015 goto again;
3016 }
Chris Mason593060d2008-03-25 16:50:33 -04003017 stripe_nr = offset;
3018 /*
3019 * stripe_nr counts the total number of stripes we have to stride
3020 * to get to this block
3021 */
3022 do_div(stripe_nr, map->stripe_len);
3023
3024 stripe_offset = stripe_nr * map->stripe_len;
3025 BUG_ON(offset < stripe_offset);
3026
3027 /* stripe_offset is the offset of this block in its stripe*/
3028 stripe_offset = offset - stripe_offset;
3029
Chris Masoncea9e442008-04-09 16:28:12 -04003030 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003031 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04003032 BTRFS_BLOCK_GROUP_DUP)) {
3033 /* we limit the length of each bio to what fits in a stripe */
3034 *length = min_t(u64, em->len - offset,
3035 map->stripe_len - stripe_offset);
3036 } else {
3037 *length = em->len - offset;
3038 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003039
3040 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04003041 goto out;
3042
Chris Masonf2d8d742008-04-21 10:03:05 -04003043 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003044 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003045 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003046 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04003047 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003048 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003049 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003050 else {
3051 stripe_index = find_live_mirror(map, 0,
3052 map->num_stripes,
3053 current->pid % map->num_stripes);
3054 }
Chris Mason2fff7342008-04-29 14:12:09 -04003055
Chris Mason611f0e02008-04-03 16:29:03 -04003056 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003057 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003058 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003059 else if (mirror_num)
3060 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003061
Chris Mason321aecc2008-04-16 10:49:51 -04003062 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3063 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003064
3065 stripe_index = do_div(stripe_nr, factor);
3066 stripe_index *= map->sub_stripes;
3067
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003068 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04003069 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003070 else if (mirror_num)
3071 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003072 else {
3073 stripe_index = find_live_mirror(map, stripe_index,
3074 map->sub_stripes, stripe_index +
3075 current->pid % map->sub_stripes);
3076 }
Chris Mason8790d502008-04-03 16:29:03 -04003077 } else {
3078 /*
3079 * after this do_div call, stripe_nr is the number of stripes
3080 * on this device we have to walk to find the data, and
3081 * stripe_index is the number of our device in the stripe array
3082 */
3083 stripe_index = do_div(stripe_nr, map->num_stripes);
3084 }
Chris Mason593060d2008-03-25 16:50:33 -04003085 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003086
Chris Masonf2d8d742008-04-21 10:03:05 -04003087 for (i = 0; i < num_stripes; i++) {
3088 if (unplug_page) {
3089 struct btrfs_device *device;
3090 struct backing_dev_info *bdi;
3091
3092 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04003093 if (device->bdev) {
3094 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05003095 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04003096 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04003097 }
3098 } else {
3099 multi->stripes[i].physical =
3100 map->stripes[stripe_index].physical +
3101 stripe_offset + stripe_nr * map->stripe_len;
3102 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3103 }
Chris Masoncea9e442008-04-09 16:28:12 -04003104 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04003105 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003106 if (multi_ret) {
3107 *multi_ret = multi;
3108 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003109 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003110 }
Chris Masoncea9e442008-04-09 16:28:12 -04003111out:
Chris Mason0b86a832008-03-24 15:01:56 -04003112 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003113 return 0;
3114}
3115
Chris Masonf2d8d742008-04-21 10:03:05 -04003116int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3117 u64 logical, u64 *length,
3118 struct btrfs_multi_bio **multi_ret, int mirror_num)
3119{
3120 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3121 mirror_num, NULL);
3122}
3123
Yan Zhenga512bbf2008-12-08 16:46:26 -05003124int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3125 u64 chunk_start, u64 physical, u64 devid,
3126 u64 **logical, int *naddrs, int *stripe_len)
3127{
3128 struct extent_map_tree *em_tree = &map_tree->map_tree;
3129 struct extent_map *em;
3130 struct map_lookup *map;
3131 u64 *buf;
3132 u64 bytenr;
3133 u64 length;
3134 u64 stripe_nr;
3135 int i, j, nr = 0;
3136
Chris Mason890871b2009-09-02 16:24:52 -04003137 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003138 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003139 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003140
3141 BUG_ON(!em || em->start != chunk_start);
3142 map = (struct map_lookup *)em->bdev;
3143
3144 length = em->len;
3145 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3146 do_div(length, map->num_stripes / map->sub_stripes);
3147 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3148 do_div(length, map->num_stripes);
3149
3150 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3151 BUG_ON(!buf);
3152
3153 for (i = 0; i < map->num_stripes; i++) {
3154 if (devid && map->stripes[i].dev->devid != devid)
3155 continue;
3156 if (map->stripes[i].physical > physical ||
3157 map->stripes[i].physical + length <= physical)
3158 continue;
3159
3160 stripe_nr = physical - map->stripes[i].physical;
3161 do_div(stripe_nr, map->stripe_len);
3162
3163 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3164 stripe_nr = stripe_nr * map->num_stripes + i;
3165 do_div(stripe_nr, map->sub_stripes);
3166 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3167 stripe_nr = stripe_nr * map->num_stripes + i;
3168 }
3169 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003170 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003171 for (j = 0; j < nr; j++) {
3172 if (buf[j] == bytenr)
3173 break;
3174 }
Chris Mason934d3752008-12-08 16:43:10 -05003175 if (j == nr) {
3176 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003177 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003178 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003179 }
3180
Yan Zhenga512bbf2008-12-08 16:46:26 -05003181 *logical = buf;
3182 *naddrs = nr;
3183 *stripe_len = map->stripe_len;
3184
3185 free_extent_map(em);
3186 return 0;
3187}
3188
Chris Masonf2d8d742008-04-21 10:03:05 -04003189int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
3190 u64 logical, struct page *page)
3191{
3192 u64 length = PAGE_CACHE_SIZE;
3193 return __btrfs_map_block(map_tree, READ, logical, &length,
3194 NULL, 0, page);
3195}
3196
Chris Mason8790d502008-04-03 16:29:03 -04003197static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003198{
Chris Masoncea9e442008-04-09 16:28:12 -04003199 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003200 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003201
Chris Mason8790d502008-04-03 16:29:03 -04003202 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003203 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003204
Chris Mason7d2b4da2008-08-05 10:13:57 -04003205 if (bio == multi->orig_bio)
3206 is_orig_bio = 1;
3207
Chris Masoncea9e442008-04-09 16:28:12 -04003208 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003209 if (!is_orig_bio) {
3210 bio_put(bio);
3211 bio = multi->orig_bio;
3212 }
Chris Mason8790d502008-04-03 16:29:03 -04003213 bio->bi_private = multi->private;
3214 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003215 /* only send an error to the higher layers if it is
3216 * beyond the tolerance of the multi-bio
3217 */
Chris Mason1259ab72008-05-12 13:39:03 -04003218 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003219 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003220 } else if (err) {
3221 /*
3222 * this bio is actually up to date, we didn't
3223 * go over the max number of errors
3224 */
3225 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003226 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003227 }
Chris Mason8790d502008-04-03 16:29:03 -04003228 kfree(multi);
3229
3230 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003231 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003232 bio_put(bio);
3233 }
Chris Mason8790d502008-04-03 16:29:03 -04003234}
3235
Chris Mason8b712842008-06-11 16:50:36 -04003236struct async_sched {
3237 struct bio *bio;
3238 int rw;
3239 struct btrfs_fs_info *info;
3240 struct btrfs_work work;
3241};
3242
3243/*
3244 * see run_scheduled_bios for a description of why bios are collected for
3245 * async submit.
3246 *
3247 * This will add one bio to the pending list for a device and make sure
3248 * the work struct is scheduled.
3249 */
Chris Masond3977122009-01-05 21:25:51 -05003250static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003251 struct btrfs_device *device,
3252 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003253{
3254 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003255 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003256
3257 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003258 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04003259 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003260 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04003261 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003262 return 0;
3263 }
3264
3265 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003266 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003267 * higher layers. Otherwise, the async bio makes it appear we have
3268 * made progress against dirty pages when we've really just put it
3269 * on a queue for later
3270 */
Chris Mason0986fe92008-08-15 15:34:15 -04003271 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04003272 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003273 bio->bi_next = NULL;
3274 bio->bi_rw |= rw;
3275
3276 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003277 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003278 pending_bios = &device->pending_sync_bios;
3279 else
3280 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003281
Chris Masonffbd5172009-04-20 15:50:09 -04003282 if (pending_bios->tail)
3283 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003284
Chris Masonffbd5172009-04-20 15:50:09 -04003285 pending_bios->tail = bio;
3286 if (!pending_bios->head)
3287 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003288 if (device->running_pending)
3289 should_queue = 0;
3290
3291 spin_unlock(&device->io_lock);
3292
3293 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003294 btrfs_queue_worker(&root->fs_info->submit_workers,
3295 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003296 return 0;
3297}
3298
Chris Masonf1885912008-04-09 16:28:12 -04003299int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003300 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003301{
3302 struct btrfs_mapping_tree *map_tree;
3303 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003304 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003305 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003306 u64 length = 0;
3307 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003308 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003309 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003310 int dev_nr = 0;
3311 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003312
Chris Masonf2d8d742008-04-21 10:03:05 -04003313 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003314 map_tree = &root->fs_info->mapping_tree;
3315 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003316
Chris Masonf1885912008-04-09 16:28:12 -04003317 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3318 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003319 BUG_ON(ret);
3320
3321 total_devs = multi->num_stripes;
3322 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003323 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3324 "len %llu\n", (unsigned long long)logical,
3325 (unsigned long long)length,
3326 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003327 BUG();
3328 }
3329 multi->end_io = first_bio->bi_end_io;
3330 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003331 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003332 atomic_set(&multi->stripes_pending, multi->num_stripes);
3333
Chris Masond3977122009-01-05 21:25:51 -05003334 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003335 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003336 if (dev_nr < total_devs - 1) {
3337 bio = bio_clone(first_bio, GFP_NOFS);
3338 BUG_ON(!bio);
3339 } else {
3340 bio = first_bio;
3341 }
3342 bio->bi_private = multi;
3343 bio->bi_end_io = end_bio_multi_stripe;
3344 }
Chris Masoncea9e442008-04-09 16:28:12 -04003345 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3346 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003347 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003348 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003349 if (async_submit)
3350 schedule_bio(root, dev, rw, bio);
3351 else
3352 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003353 } else {
3354 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3355 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003356 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003357 }
Chris Mason8790d502008-04-03 16:29:03 -04003358 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003359 }
Chris Masoncea9e442008-04-09 16:28:12 -04003360 if (total_devs == 1)
3361 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003362 return 0;
3363}
3364
Chris Masona4437552008-04-18 10:29:38 -04003365struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003366 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003367{
Yan Zheng2b820322008-11-17 21:11:30 -05003368 struct btrfs_device *device;
3369 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003370
Yan Zheng2b820322008-11-17 21:11:30 -05003371 cur_devices = root->fs_info->fs_devices;
3372 while (cur_devices) {
3373 if (!fsid ||
3374 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3375 device = __find_device(&cur_devices->devices,
3376 devid, uuid);
3377 if (device)
3378 return device;
3379 }
3380 cur_devices = cur_devices->seed;
3381 }
3382 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003383}
3384
Chris Masondfe25022008-05-13 13:46:40 -04003385static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3386 u64 devid, u8 *dev_uuid)
3387{
3388 struct btrfs_device *device;
3389 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3390
3391 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003392 if (!device)
3393 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003394 list_add(&device->dev_list,
3395 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003396 device->barriers = 1;
3397 device->dev_root = root->fs_info->dev_root;
3398 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003399 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003400 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003401 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003402 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003403 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003404 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003405 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003406 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3407 return device;
3408}
3409
Chris Mason0b86a832008-03-24 15:01:56 -04003410static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3411 struct extent_buffer *leaf,
3412 struct btrfs_chunk *chunk)
3413{
3414 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3415 struct map_lookup *map;
3416 struct extent_map *em;
3417 u64 logical;
3418 u64 length;
3419 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003420 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003421 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003422 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003423 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003424
Chris Masone17cade2008-04-15 15:41:47 -04003425 logical = key->offset;
3426 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003427
Chris Mason890871b2009-09-02 16:24:52 -04003428 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003429 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003430 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003431
3432 /* already mapped? */
3433 if (em && em->start <= logical && em->start + em->len > logical) {
3434 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003435 return 0;
3436 } else if (em) {
3437 free_extent_map(em);
3438 }
Chris Mason0b86a832008-03-24 15:01:56 -04003439
Chris Mason0b86a832008-03-24 15:01:56 -04003440 em = alloc_extent_map(GFP_NOFS);
3441 if (!em)
3442 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003443 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3444 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003445 if (!map) {
3446 free_extent_map(em);
3447 return -ENOMEM;
3448 }
3449
3450 em->bdev = (struct block_device *)map;
3451 em->start = logical;
3452 em->len = length;
3453 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003454 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003455
Chris Mason593060d2008-03-25 16:50:33 -04003456 map->num_stripes = num_stripes;
3457 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3458 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3459 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3460 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3461 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003462 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003463 for (i = 0; i < num_stripes; i++) {
3464 map->stripes[i].physical =
3465 btrfs_stripe_offset_nr(leaf, chunk, i);
3466 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003467 read_extent_buffer(leaf, uuid, (unsigned long)
3468 btrfs_stripe_dev_uuid_nr(chunk, i),
3469 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003470 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3471 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003472 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003473 kfree(map);
3474 free_extent_map(em);
3475 return -EIO;
3476 }
Chris Masondfe25022008-05-13 13:46:40 -04003477 if (!map->stripes[i].dev) {
3478 map->stripes[i].dev =
3479 add_missing_dev(root, devid, uuid);
3480 if (!map->stripes[i].dev) {
3481 kfree(map);
3482 free_extent_map(em);
3483 return -EIO;
3484 }
3485 }
3486 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003487 }
3488
Chris Mason890871b2009-09-02 16:24:52 -04003489 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003490 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003491 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003492 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003493 free_extent_map(em);
3494
3495 return 0;
3496}
3497
3498static int fill_device_from_item(struct extent_buffer *leaf,
3499 struct btrfs_dev_item *dev_item,
3500 struct btrfs_device *device)
3501{
3502 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003503
3504 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003505 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3506 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003507 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3508 device->type = btrfs_device_type(leaf, dev_item);
3509 device->io_align = btrfs_device_io_align(leaf, dev_item);
3510 device->io_width = btrfs_device_io_width(leaf, dev_item);
3511 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003512
3513 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003514 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003515
Chris Mason0b86a832008-03-24 15:01:56 -04003516 return 0;
3517}
3518
Yan Zheng2b820322008-11-17 21:11:30 -05003519static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3520{
3521 struct btrfs_fs_devices *fs_devices;
3522 int ret;
3523
3524 mutex_lock(&uuid_mutex);
3525
3526 fs_devices = root->fs_info->fs_devices->seed;
3527 while (fs_devices) {
3528 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3529 ret = 0;
3530 goto out;
3531 }
3532 fs_devices = fs_devices->seed;
3533 }
3534
3535 fs_devices = find_fsid(fsid);
3536 if (!fs_devices) {
3537 ret = -ENOENT;
3538 goto out;
3539 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003540
3541 fs_devices = clone_fs_devices(fs_devices);
3542 if (IS_ERR(fs_devices)) {
3543 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003544 goto out;
3545 }
3546
Christoph Hellwig97288f22008-12-02 06:36:09 -05003547 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003548 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003549 if (ret)
3550 goto out;
3551
3552 if (!fs_devices->seeding) {
3553 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003554 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003555 ret = -EINVAL;
3556 goto out;
3557 }
3558
3559 fs_devices->seed = root->fs_info->fs_devices->seed;
3560 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003561out:
3562 mutex_unlock(&uuid_mutex);
3563 return ret;
3564}
3565
Chris Mason0d81ba52008-03-24 15:02:07 -04003566static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003567 struct extent_buffer *leaf,
3568 struct btrfs_dev_item *dev_item)
3569{
3570 struct btrfs_device *device;
3571 u64 devid;
3572 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003573 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003574 u8 dev_uuid[BTRFS_UUID_SIZE];
3575
Chris Mason0b86a832008-03-24 15:01:56 -04003576 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003577 read_extent_buffer(leaf, dev_uuid,
3578 (unsigned long)btrfs_device_uuid(dev_item),
3579 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003580 read_extent_buffer(leaf, fs_uuid,
3581 (unsigned long)btrfs_device_fsid(dev_item),
3582 BTRFS_UUID_SIZE);
3583
3584 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3585 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003586 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003587 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003588 }
3589
3590 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3591 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003592 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003593 return -EIO;
3594
3595 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003596 printk(KERN_WARNING "warning devid %llu missing\n",
3597 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003598 device = add_missing_dev(root, devid, dev_uuid);
3599 if (!device)
3600 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003601 } else if (!device->missing) {
3602 /*
3603 * this happens when a device that was properly setup
3604 * in the device info lists suddenly goes bad.
3605 * device->bdev is NULL, and so we have to set
3606 * device->missing to one here
3607 */
3608 root->fs_info->fs_devices->missing_devices++;
3609 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003610 }
3611 }
3612
3613 if (device->fs_devices != root->fs_info->fs_devices) {
3614 BUG_ON(device->writeable);
3615 if (device->generation !=
3616 btrfs_device_generation(leaf, dev_item))
3617 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003618 }
Chris Mason0b86a832008-03-24 15:01:56 -04003619
3620 fill_device_from_item(leaf, dev_item, device);
3621 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003622 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003623 if (device->writeable)
3624 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003625 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003626 return ret;
3627}
3628
Chris Mason0d81ba52008-03-24 15:02:07 -04003629int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3630{
3631 struct btrfs_dev_item *dev_item;
3632
3633 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3634 dev_item);
3635 return read_one_dev(root, buf, dev_item);
3636}
3637
Yan Zhenge4404d62008-12-12 10:03:26 -05003638int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003639{
3640 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003641 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003642 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003643 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003644 u8 *ptr;
3645 unsigned long sb_ptr;
3646 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003647 u32 num_stripes;
3648 u32 array_size;
3649 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003650 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003651 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003652
Yan Zhenge4404d62008-12-12 10:03:26 -05003653 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003654 BTRFS_SUPER_INFO_SIZE);
3655 if (!sb)
3656 return -ENOMEM;
3657 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003658 btrfs_set_buffer_lockdep_class(sb, 0);
3659
Chris Masona061fc82008-05-07 11:43:44 -04003660 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003661 array_size = btrfs_super_sys_array_size(super_copy);
3662
Chris Mason0b86a832008-03-24 15:01:56 -04003663 ptr = super_copy->sys_chunk_array;
3664 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3665 cur = 0;
3666
3667 while (cur < array_size) {
3668 disk_key = (struct btrfs_disk_key *)ptr;
3669 btrfs_disk_key_to_cpu(&key, disk_key);
3670
Chris Masona061fc82008-05-07 11:43:44 -04003671 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003672 sb_ptr += len;
3673 cur += len;
3674
Chris Mason0d81ba52008-03-24 15:02:07 -04003675 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003676 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003677 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003678 if (ret)
3679 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003680 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3681 len = btrfs_chunk_item_size(num_stripes);
3682 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003683 ret = -EIO;
3684 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003685 }
3686 ptr += len;
3687 sb_ptr += len;
3688 cur += len;
3689 }
Chris Masona061fc82008-05-07 11:43:44 -04003690 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003691 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003692}
3693
3694int btrfs_read_chunk_tree(struct btrfs_root *root)
3695{
3696 struct btrfs_path *path;
3697 struct extent_buffer *leaf;
3698 struct btrfs_key key;
3699 struct btrfs_key found_key;
3700 int ret;
3701 int slot;
3702
3703 root = root->fs_info->chunk_root;
3704
3705 path = btrfs_alloc_path();
3706 if (!path)
3707 return -ENOMEM;
3708
3709 /* first we search for all of the device items, and then we
3710 * read in all of the chunk items. This way we can create chunk
3711 * mappings that reference all of the devices that are afound
3712 */
3713 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3714 key.offset = 0;
3715 key.type = 0;
3716again:
3717 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003718 if (ret < 0)
3719 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003720 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003721 leaf = path->nodes[0];
3722 slot = path->slots[0];
3723 if (slot >= btrfs_header_nritems(leaf)) {
3724 ret = btrfs_next_leaf(root, path);
3725 if (ret == 0)
3726 continue;
3727 if (ret < 0)
3728 goto error;
3729 break;
3730 }
3731 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3732 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3733 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3734 break;
3735 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3736 struct btrfs_dev_item *dev_item;
3737 dev_item = btrfs_item_ptr(leaf, slot,
3738 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003739 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003740 if (ret)
3741 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003742 }
3743 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3744 struct btrfs_chunk *chunk;
3745 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3746 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003747 if (ret)
3748 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003749 }
3750 path->slots[0]++;
3751 }
3752 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3753 key.objectid = 0;
3754 btrfs_release_path(root, path);
3755 goto again;
3756 }
Chris Mason0b86a832008-03-24 15:01:56 -04003757 ret = 0;
3758error:
Yan Zheng2b820322008-11-17 21:11:30 -05003759 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003760 return ret;
3761}