blob: d158530233b701397360fa13be8265274589b896 [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 Mason492bb6de2008-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 Masonb248a412008-04-14 09:48:18 -0400402 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400403 device->name = kstrdup(path, GFP_NOFS);
404 if (!device->name) {
405 kfree(device);
406 return -ENOMEM;
407 }
Yan Zheng2b820322008-11-17 21:11:30 -0500408 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400409
410 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400412 mutex_unlock(&fs_devices->device_list_mutex);
413
Yan Zheng2b820322008-11-17 21:11:30 -0500414 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400415 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500416 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000417 name = kstrdup(path, GFP_NOFS);
418 if (!name)
419 return -ENOMEM;
420 kfree(device->name);
421 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500422 if (device->missing) {
423 fs_devices->missing_devices--;
424 device->missing = 0;
425 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400426 }
427
428 if (found_transid > fs_devices->latest_trans) {
429 fs_devices->latest_devid = devid;
430 fs_devices->latest_trans = found_transid;
431 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400432 *fs_devices_ret = fs_devices;
433 return 0;
434}
435
Yan Zhenge4404d62008-12-12 10:03:26 -0500436static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
437{
438 struct btrfs_fs_devices *fs_devices;
439 struct btrfs_device *device;
440 struct btrfs_device *orig_dev;
441
442 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
443 if (!fs_devices)
444 return ERR_PTR(-ENOMEM);
445
446 INIT_LIST_HEAD(&fs_devices->devices);
447 INIT_LIST_HEAD(&fs_devices->alloc_list);
448 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400449 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500450 fs_devices->latest_devid = orig->latest_devid;
451 fs_devices->latest_trans = orig->latest_trans;
452 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
453
Chris Masone5e9a522009-06-10 15:17:02 -0400454 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500455 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
456 device = kzalloc(sizeof(*device), GFP_NOFS);
457 if (!device)
458 goto error;
459
460 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400461 if (!device->name) {
462 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500463 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400464 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500465
466 device->devid = orig_dev->devid;
467 device->work.func = pending_bios_fn;
468 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500469 spin_lock_init(&device->io_lock);
470 INIT_LIST_HEAD(&device->dev_list);
471 INIT_LIST_HEAD(&device->dev_alloc_list);
472
473 list_add(&device->dev_list, &fs_devices->devices);
474 device->fs_devices = fs_devices;
475 fs_devices->num_devices++;
476 }
Chris Masone5e9a522009-06-10 15:17:02 -0400477 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500478 return fs_devices;
479error:
Chris Masone5e9a522009-06-10 15:17:02 -0400480 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500481 free_fs_devices(fs_devices);
482 return ERR_PTR(-ENOMEM);
483}
484
Chris Masondfe25022008-05-13 13:46:40 -0400485int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
486{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500487 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400488
489 mutex_lock(&uuid_mutex);
490again:
Chris Masone5e9a522009-06-10 15:17:02 -0400491 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500492 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500493 if (device->in_fs_metadata)
494 continue;
495
496 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100497 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500498 device->bdev = NULL;
499 fs_devices->open_devices--;
500 }
501 if (device->writeable) {
502 list_del_init(&device->dev_alloc_list);
503 device->writeable = 0;
504 fs_devices->rw_devices--;
505 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500506 list_del_init(&device->dev_list);
507 fs_devices->num_devices--;
508 kfree(device->name);
509 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400510 }
Chris Masone5e9a522009-06-10 15:17:02 -0400511 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500512
513 if (fs_devices->seed) {
514 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500515 goto again;
516 }
517
Chris Masondfe25022008-05-13 13:46:40 -0400518 mutex_unlock(&uuid_mutex);
519 return 0;
520}
Chris Masona0af4692008-05-13 16:03:06 -0400521
Yan Zheng2b820322008-11-17 21:11:30 -0500522static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400523{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400524 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500525
Yan Zheng2b820322008-11-17 21:11:30 -0500526 if (--fs_devices->opened > 0)
527 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400528
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500529 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400530 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100531 blkdev_put(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400532 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533 }
Yan Zheng2b820322008-11-17 21:11:30 -0500534 if (device->writeable) {
535 list_del_init(&device->dev_alloc_list);
536 fs_devices->rw_devices--;
537 }
538
Chris Mason8a4b83c2008-03-24 15:02:07 -0400539 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500540 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400541 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400542 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500543 WARN_ON(fs_devices->open_devices);
544 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500545 fs_devices->opened = 0;
546 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500547
Chris Mason8a4b83c2008-03-24 15:02:07 -0400548 return 0;
549}
550
Yan Zheng2b820322008-11-17 21:11:30 -0500551int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
552{
Yan Zhenge4404d62008-12-12 10:03:26 -0500553 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500554 int ret;
555
556 mutex_lock(&uuid_mutex);
557 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500558 if (!fs_devices->opened) {
559 seed_devices = fs_devices->seed;
560 fs_devices->seed = NULL;
561 }
Yan Zheng2b820322008-11-17 21:11:30 -0500562 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500563
564 while (seed_devices) {
565 fs_devices = seed_devices;
566 seed_devices = fs_devices->seed;
567 __btrfs_close_devices(fs_devices);
568 free_fs_devices(fs_devices);
569 }
Yan Zheng2b820322008-11-17 21:11:30 -0500570 return ret;
571}
572
Yan Zhenge4404d62008-12-12 10:03:26 -0500573static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
574 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400575{
576 struct block_device *bdev;
577 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400579 struct block_device *latest_bdev = NULL;
580 struct buffer_head *bh;
581 struct btrfs_super_block *disk_super;
582 u64 latest_devid = 0;
583 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400584 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500585 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400586 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400587
Tejun Heod4d77622010-11-13 11:55:18 +0100588 flags |= FMODE_EXCL;
589
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
Tejun Heod4d77622010-11-13 11:55:18 +0100596 bdev = blkdev_get_by_path(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:
Tejun Heod4d77622010-11-13 11:55:18 +0100650 blkdev_put(bdev, flags);
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
Tejun Heod4d77622010-11-13 11:55:18 +0100696 flags |= FMODE_EXCL;
697 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400698
699 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400700 ret = PTR_ERR(bdev);
701 goto error;
702 }
703
704 ret = set_blocksize(bdev, 4096);
705 if (ret)
706 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500707 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000709 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400710 goto error_close;
711 }
712 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000713 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400714 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400715 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500716 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400717 else {
718 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500719 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400720 *(unsigned long long *)disk_super->fsid,
721 *(unsigned long long *)(disk_super->fsid + 8));
722 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500723 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500724 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400725 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
726
Chris Mason8a4b83c2008-03-24 15:02:07 -0400727 brelse(bh);
728error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100729 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730error:
731 mutex_unlock(&uuid_mutex);
732 return ret;
733}
Chris Mason0b86a832008-03-24 15:01:56 -0400734
Miao Xie6d07bce2011-01-05 10:07:31 +0000735/* helper to account the used device space in the range */
736int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
737 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400738{
739 struct btrfs_key key;
740 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000741 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500742 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000743 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400744 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000745 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400746 struct extent_buffer *l;
747
Miao Xie6d07bce2011-01-05 10:07:31 +0000748 *length = 0;
749
750 if (start >= device->total_bytes)
751 return 0;
752
Yan Zheng2b820322008-11-17 21:11:30 -0500753 path = btrfs_alloc_path();
754 if (!path)
755 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400756 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400757
Chris Mason0b86a832008-03-24 15:01:56 -0400758 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000759 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400760 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000761
762 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400763 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000764 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400765 if (ret > 0) {
766 ret = btrfs_previous_item(root, path, key.objectid, key.type);
767 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000768 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400769 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000770
Chris Mason0b86a832008-03-24 15:01:56 -0400771 while (1) {
772 l = path->nodes[0];
773 slot = path->slots[0];
774 if (slot >= btrfs_header_nritems(l)) {
775 ret = btrfs_next_leaf(root, path);
776 if (ret == 0)
777 continue;
778 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000779 goto out;
780
781 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400782 }
783 btrfs_item_key_to_cpu(l, &key, slot);
784
785 if (key.objectid < device->devid)
786 goto next;
787
788 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000789 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400790
Chris Masond3977122009-01-05 21:25:51 -0500791 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400792 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400793
Chris Mason0b86a832008-03-24 15:01:56 -0400794 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000795 extent_end = key.offset + btrfs_dev_extent_length(l,
796 dev_extent);
797 if (key.offset <= start && extent_end > end) {
798 *length = end - start + 1;
799 break;
800 } else if (key.offset <= start && extent_end > start)
801 *length += extent_end - start;
802 else if (key.offset > start && extent_end <= end)
803 *length += extent_end - key.offset;
804 else if (key.offset > start && key.offset <= end) {
805 *length += end - key.offset + 1;
806 break;
807 } else if (key.offset > end)
808 break;
809
810next:
811 path->slots[0]++;
812 }
813 ret = 0;
814out:
815 btrfs_free_path(path);
816 return ret;
817}
818
Chris Mason0b86a832008-03-24 15:01:56 -0400819/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000820 * find_free_dev_extent - find free space in the specified device
821 * @trans: transaction handler
822 * @device: the device which we search the free space in
823 * @num_bytes: the size of the free space that we need
824 * @start: store the start of the free space.
825 * @len: the size of the free space. that we find, or the size of the max
826 * free space if we don't find suitable free space
827 *
Chris Mason0b86a832008-03-24 15:01:56 -0400828 * this uses a pretty simple search, the expectation is that it is
829 * called very infrequently and that a given device has a small number
830 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000831 *
832 * @start is used to store the start of the free space if we find. But if we
833 * don't find suitable free space, it will be used to store the start position
834 * of the max free space.
835 *
836 * @len is used to store the size of the free space that we find.
837 * But if we don't find suitable free space, it is used to store the size of
838 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400839 */
840int find_free_dev_extent(struct btrfs_trans_handle *trans,
841 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000842 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400843{
844 struct btrfs_key key;
845 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000846 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400847 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 u64 hole_size;
849 u64 max_hole_start;
850 u64 max_hole_size;
851 u64 extent_end;
852 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400853 u64 search_end = device->total_bytes;
854 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000855 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400856 struct extent_buffer *l;
857
Chris Mason0b86a832008-03-24 15:01:56 -0400858 /* FIXME use last free of some kind */
859
860 /* we don't want to overwrite the superblock on the drive,
861 * so we make sure to start at an offset of at least 1MB
862 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000863 search_start = 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400864
Miao Xie7bfc8372011-01-05 10:07:26 +0000865 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason0b86a832008-03-24 15:01:56 -0400866 search_start = max(root->fs_info->alloc_start, search_start);
867
Miao Xie7bfc8372011-01-05 10:07:26 +0000868 max_hole_start = search_start;
869 max_hole_size = 0;
870
871 if (search_start >= search_end) {
872 ret = -ENOSPC;
873 goto error;
874 }
875
876 path = btrfs_alloc_path();
877 if (!path) {
878 ret = -ENOMEM;
879 goto error;
880 }
881 path->reada = 2;
882
Chris Mason0b86a832008-03-24 15:01:56 -0400883 key.objectid = device->devid;
884 key.offset = search_start;
885 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000886
Chris Mason0b86a832008-03-24 15:01:56 -0400887 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
888 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000889 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400890 if (ret > 0) {
891 ret = btrfs_previous_item(root, path, key.objectid, key.type);
892 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000893 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400894 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000895
Chris Mason0b86a832008-03-24 15:01:56 -0400896 while (1) {
897 l = path->nodes[0];
898 slot = path->slots[0];
899 if (slot >= btrfs_header_nritems(l)) {
900 ret = btrfs_next_leaf(root, path);
901 if (ret == 0)
902 continue;
903 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000904 goto out;
905
906 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400907 }
908 btrfs_item_key_to_cpu(l, &key, slot);
909
910 if (key.objectid < device->devid)
911 goto next;
912
913 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000914 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400915
Chris Mason0b86a832008-03-24 15:01:56 -0400916 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
917 goto next;
918
Miao Xie7bfc8372011-01-05 10:07:26 +0000919 if (key.offset > search_start) {
920 hole_size = key.offset - search_start;
921
922 if (hole_size > max_hole_size) {
923 max_hole_start = search_start;
924 max_hole_size = hole_size;
925 }
926
927 /*
928 * If this free space is greater than which we need,
929 * it must be the max free space that we have found
930 * until now, so max_hole_start must point to the start
931 * of this free space and the length of this free space
932 * is stored in max_hole_size. Thus, we return
933 * max_hole_start and max_hole_size and go back to the
934 * caller.
935 */
936 if (hole_size >= num_bytes) {
937 ret = 0;
938 goto out;
939 }
940 }
941
Chris Mason0b86a832008-03-24 15:01:56 -0400942 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000943 extent_end = key.offset + btrfs_dev_extent_length(l,
944 dev_extent);
945 if (extent_end > search_start)
946 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400947next:
948 path->slots[0]++;
949 cond_resched();
950 }
Chris Mason0b86a832008-03-24 15:01:56 -0400951
Miao Xie7bfc8372011-01-05 10:07:26 +0000952 hole_size = search_end- search_start;
953 if (hole_size > max_hole_size) {
954 max_hole_start = search_start;
955 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400956 }
Chris Mason0b86a832008-03-24 15:01:56 -0400957
Miao Xie7bfc8372011-01-05 10:07:26 +0000958 /* See above. */
959 if (hole_size < num_bytes)
960 ret = -ENOSPC;
961 else
962 ret = 0;
963
964out:
Yan Zheng2b820322008-11-17 21:11:30 -0500965 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000966error:
967 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000968 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000969 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400970 return ret;
971}
972
Christoph Hellwigb2950862008-12-02 09:54:17 -0500973static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400974 struct btrfs_device *device,
975 u64 start)
976{
977 int ret;
978 struct btrfs_path *path;
979 struct btrfs_root *root = device->dev_root;
980 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400981 struct btrfs_key found_key;
982 struct extent_buffer *leaf = NULL;
983 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400984
985 path = btrfs_alloc_path();
986 if (!path)
987 return -ENOMEM;
988
989 key.objectid = device->devid;
990 key.offset = start;
991 key.type = BTRFS_DEV_EXTENT_KEY;
992
993 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400994 if (ret > 0) {
995 ret = btrfs_previous_item(root, path, key.objectid,
996 BTRFS_DEV_EXTENT_KEY);
997 BUG_ON(ret);
998 leaf = path->nodes[0];
999 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1000 extent = btrfs_item_ptr(leaf, path->slots[0],
1001 struct btrfs_dev_extent);
1002 BUG_ON(found_key.offset > start || found_key.offset +
1003 btrfs_dev_extent_length(leaf, extent) < start);
1004 ret = 0;
1005 } else if (ret == 0) {
1006 leaf = path->nodes[0];
1007 extent = btrfs_item_ptr(leaf, path->slots[0],
1008 struct btrfs_dev_extent);
1009 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001010 BUG_ON(ret);
1011
Chris Masondfe25022008-05-13 13:46:40 -04001012 if (device->bytes_used > 0)
1013 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -04001014 ret = btrfs_del_item(trans, root, path);
1015 BUG_ON(ret);
1016
1017 btrfs_free_path(path);
1018 return ret;
1019}
1020
Yan Zheng2b820322008-11-17 21:11:30 -05001021int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001022 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001023 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001024 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001025{
1026 int ret;
1027 struct btrfs_path *path;
1028 struct btrfs_root *root = device->dev_root;
1029 struct btrfs_dev_extent *extent;
1030 struct extent_buffer *leaf;
1031 struct btrfs_key key;
1032
Chris Masondfe25022008-05-13 13:46:40 -04001033 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001034 path = btrfs_alloc_path();
1035 if (!path)
1036 return -ENOMEM;
1037
Chris Mason0b86a832008-03-24 15:01:56 -04001038 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001039 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001040 key.type = BTRFS_DEV_EXTENT_KEY;
1041 ret = btrfs_insert_empty_item(trans, root, path, &key,
1042 sizeof(*extent));
1043 BUG_ON(ret);
1044
1045 leaf = path->nodes[0];
1046 extent = btrfs_item_ptr(leaf, path->slots[0],
1047 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001048 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1049 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1050 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1051
1052 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1053 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1054 BTRFS_UUID_SIZE);
1055
Chris Mason0b86a832008-03-24 15:01:56 -04001056 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1057 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001058 btrfs_free_path(path);
1059 return ret;
1060}
1061
Chris Masona1b32a52008-09-05 16:09:51 -04001062static noinline int find_next_chunk(struct btrfs_root *root,
1063 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001064{
1065 struct btrfs_path *path;
1066 int ret;
1067 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001068 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001069 struct btrfs_key found_key;
1070
1071 path = btrfs_alloc_path();
1072 BUG_ON(!path);
1073
Chris Masone17cade2008-04-15 15:41:47 -04001074 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001075 key.offset = (u64)-1;
1076 key.type = BTRFS_CHUNK_ITEM_KEY;
1077
1078 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1079 if (ret < 0)
1080 goto error;
1081
1082 BUG_ON(ret == 0);
1083
1084 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1085 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001086 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001087 } else {
1088 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1089 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001090 if (found_key.objectid != objectid)
1091 *offset = 0;
1092 else {
1093 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1094 struct btrfs_chunk);
1095 *offset = found_key.offset +
1096 btrfs_chunk_length(path->nodes[0], chunk);
1097 }
Chris Mason0b86a832008-03-24 15:01:56 -04001098 }
1099 ret = 0;
1100error:
1101 btrfs_free_path(path);
1102 return ret;
1103}
1104
Yan Zheng2b820322008-11-17 21:11:30 -05001105static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001106{
1107 int ret;
1108 struct btrfs_key key;
1109 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001110 struct btrfs_path *path;
1111
1112 root = root->fs_info->chunk_root;
1113
1114 path = btrfs_alloc_path();
1115 if (!path)
1116 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001117
1118 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1119 key.type = BTRFS_DEV_ITEM_KEY;
1120 key.offset = (u64)-1;
1121
1122 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1123 if (ret < 0)
1124 goto error;
1125
1126 BUG_ON(ret == 0);
1127
1128 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1129 BTRFS_DEV_ITEM_KEY);
1130 if (ret) {
1131 *objectid = 1;
1132 } else {
1133 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1134 path->slots[0]);
1135 *objectid = found_key.offset + 1;
1136 }
1137 ret = 0;
1138error:
Yan Zheng2b820322008-11-17 21:11:30 -05001139 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001140 return ret;
1141}
1142
1143/*
1144 * the device information is stored in the chunk root
1145 * the btrfs_device struct should be fully filled in
1146 */
1147int btrfs_add_device(struct btrfs_trans_handle *trans,
1148 struct btrfs_root *root,
1149 struct btrfs_device *device)
1150{
1151 int ret;
1152 struct btrfs_path *path;
1153 struct btrfs_dev_item *dev_item;
1154 struct extent_buffer *leaf;
1155 struct btrfs_key key;
1156 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001157
1158 root = root->fs_info->chunk_root;
1159
1160 path = btrfs_alloc_path();
1161 if (!path)
1162 return -ENOMEM;
1163
Chris Mason0b86a832008-03-24 15:01:56 -04001164 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1165 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001166 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001167
1168 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001169 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001170 if (ret)
1171 goto out;
1172
1173 leaf = path->nodes[0];
1174 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1175
1176 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001177 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001178 btrfs_set_device_type(leaf, dev_item, device->type);
1179 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1180 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1181 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001182 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1183 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001184 btrfs_set_device_group(leaf, dev_item, 0);
1185 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1186 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001187 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001188
Chris Mason0b86a832008-03-24 15:01:56 -04001189 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001190 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001191 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1192 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001193 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001194
Yan Zheng2b820322008-11-17 21:11:30 -05001195 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001196out:
1197 btrfs_free_path(path);
1198 return ret;
1199}
Chris Mason8f18cf12008-04-25 16:53:30 -04001200
Chris Masona061fc82008-05-07 11:43:44 -04001201static int btrfs_rm_dev_item(struct btrfs_root *root,
1202 struct btrfs_device *device)
1203{
1204 int ret;
1205 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001206 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001207 struct btrfs_trans_handle *trans;
1208
1209 root = root->fs_info->chunk_root;
1210
1211 path = btrfs_alloc_path();
1212 if (!path)
1213 return -ENOMEM;
1214
Yan, Zhenga22285a2010-05-16 10:48:46 -04001215 trans = btrfs_start_transaction(root, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001216 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1217 key.type = BTRFS_DEV_ITEM_KEY;
1218 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001219 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001220
1221 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1222 if (ret < 0)
1223 goto out;
1224
1225 if (ret > 0) {
1226 ret = -ENOENT;
1227 goto out;
1228 }
1229
1230 ret = btrfs_del_item(trans, root, path);
1231 if (ret)
1232 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001233out:
1234 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001235 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001236 btrfs_commit_transaction(trans, root);
1237 return ret;
1238}
1239
1240int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1241{
1242 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001243 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001244 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001245 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001246 struct btrfs_super_block *disk_super;
1247 u64 all_avail;
1248 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001249 u64 num_devices;
1250 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001251 int ret = 0;
1252
Chris Masona061fc82008-05-07 11:43:44 -04001253 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001254 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001255
1256 all_avail = root->fs_info->avail_data_alloc_bits |
1257 root->fs_info->avail_system_alloc_bits |
1258 root->fs_info->avail_metadata_alloc_bits;
1259
1260 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001261 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001262 printk(KERN_ERR "btrfs: unable to go below four devices "
1263 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001264 ret = -EINVAL;
1265 goto out;
1266 }
1267
1268 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001269 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001270 printk(KERN_ERR "btrfs: unable to go below two "
1271 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001272 ret = -EINVAL;
1273 goto out;
1274 }
1275
Chris Masondfe25022008-05-13 13:46:40 -04001276 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001277 struct list_head *devices;
1278 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001279
Chris Masondfe25022008-05-13 13:46:40 -04001280 device = NULL;
1281 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001282 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001283 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001284 if (tmp->in_fs_metadata && !tmp->bdev) {
1285 device = tmp;
1286 break;
1287 }
1288 }
Chris Masone5e9a522009-06-10 15:17:02 -04001289 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001290 bdev = NULL;
1291 bh = NULL;
1292 disk_super = NULL;
1293 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001294 printk(KERN_ERR "btrfs: no missing devices found to "
1295 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001296 goto out;
1297 }
Chris Masondfe25022008-05-13 13:46:40 -04001298 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001299 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1300 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001301 if (IS_ERR(bdev)) {
1302 ret = PTR_ERR(bdev);
1303 goto out;
1304 }
1305
Yan Zheng2b820322008-11-17 21:11:30 -05001306 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001307 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001308 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001309 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001310 goto error_close;
1311 }
1312 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001313 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001314 dev_uuid = disk_super->dev_item.uuid;
1315 device = btrfs_find_device(root, devid, dev_uuid,
1316 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001317 if (!device) {
1318 ret = -ENOENT;
1319 goto error_brelse;
1320 }
Chris Masondfe25022008-05-13 13:46:40 -04001321 }
Yan Zheng2b820322008-11-17 21:11:30 -05001322
1323 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001324 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1325 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001326 ret = -EINVAL;
1327 goto error_brelse;
1328 }
1329
1330 if (device->writeable) {
1331 list_del_init(&device->dev_alloc_list);
1332 root->fs_info->fs_devices->rw_devices--;
1333 }
Chris Masona061fc82008-05-07 11:43:44 -04001334
1335 ret = btrfs_shrink_device(device, 0);
1336 if (ret)
1337 goto error_brelse;
1338
Chris Masona061fc82008-05-07 11:43:44 -04001339 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1340 if (ret)
1341 goto error_brelse;
1342
Yan Zheng2b820322008-11-17 21:11:30 -05001343 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001344
1345 /*
1346 * the device list mutex makes sure that we don't change
1347 * the device list while someone else is writing out all
1348 * the device supers.
1349 */
1350 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001351 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001352 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1353
Yan Zhenge4404d62008-12-12 10:03:26 -05001354 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001355
Chris Masoncd02dca2010-12-13 14:56:23 -05001356 if (device->missing)
1357 root->fs_info->fs_devices->missing_devices--;
1358
Yan Zheng2b820322008-11-17 21:11:30 -05001359 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1360 struct btrfs_device, dev_list);
1361 if (device->bdev == root->fs_info->sb->s_bdev)
1362 root->fs_info->sb->s_bdev = next_device->bdev;
1363 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1364 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1365
Yan Zhenge4404d62008-12-12 10:03:26 -05001366 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001367 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001368 device->bdev = NULL;
1369 device->fs_devices->open_devices--;
1370 }
1371
Yan Zheng2b820322008-11-17 21:11:30 -05001372 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1373 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1374
Yan Zhenge4404d62008-12-12 10:03:26 -05001375 if (device->fs_devices->open_devices == 0) {
1376 struct btrfs_fs_devices *fs_devices;
1377 fs_devices = root->fs_info->fs_devices;
1378 while (fs_devices) {
1379 if (fs_devices->seed == device->fs_devices)
1380 break;
1381 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001382 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001383 fs_devices->seed = device->fs_devices->seed;
1384 device->fs_devices->seed = NULL;
1385 __btrfs_close_devices(device->fs_devices);
1386 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001387 }
1388
1389 /*
1390 * at this point, the device is zero sized. We want to
1391 * remove it from the devices list and zero out the old super
1392 */
1393 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001394 /* make sure this device isn't detected as part of
1395 * the FS anymore
1396 */
1397 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1398 set_buffer_dirty(bh);
1399 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001400 }
Chris Masona061fc82008-05-07 11:43:44 -04001401
Chris Masona061fc82008-05-07 11:43:44 -04001402 kfree(device->name);
1403 kfree(device);
1404 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001405
1406error_brelse:
1407 brelse(bh);
1408error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001409 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001410 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001411out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001412 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001413 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001414 return ret;
1415}
1416
Yan Zheng2b820322008-11-17 21:11:30 -05001417/*
1418 * does all the dirty work required for changing file system's UUID.
1419 */
1420static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1421 struct btrfs_root *root)
1422{
1423 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1424 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001425 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001426 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1427 struct btrfs_device *device;
1428 u64 super_flags;
1429
1430 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001431 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001432 return -EINVAL;
1433
Yan Zhenge4404d62008-12-12 10:03:26 -05001434 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1435 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001436 return -ENOMEM;
1437
Yan Zhenge4404d62008-12-12 10:03:26 -05001438 old_devices = clone_fs_devices(fs_devices);
1439 if (IS_ERR(old_devices)) {
1440 kfree(seed_devices);
1441 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001442 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001443
Yan Zheng2b820322008-11-17 21:11:30 -05001444 list_add(&old_devices->list, &fs_uuids);
1445
Yan Zhenge4404d62008-12-12 10:03:26 -05001446 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1447 seed_devices->opened = 1;
1448 INIT_LIST_HEAD(&seed_devices->devices);
1449 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001450 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001451 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1452 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1453 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1454 device->fs_devices = seed_devices;
1455 }
1456
Yan Zheng2b820322008-11-17 21:11:30 -05001457 fs_devices->seeding = 0;
1458 fs_devices->num_devices = 0;
1459 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001460 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001461
1462 generate_random_uuid(fs_devices->fsid);
1463 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1464 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1465 super_flags = btrfs_super_flags(disk_super) &
1466 ~BTRFS_SUPER_FLAG_SEEDING;
1467 btrfs_set_super_flags(disk_super, super_flags);
1468
1469 return 0;
1470}
1471
1472/*
1473 * strore the expected generation for seed devices in device items.
1474 */
1475static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1476 struct btrfs_root *root)
1477{
1478 struct btrfs_path *path;
1479 struct extent_buffer *leaf;
1480 struct btrfs_dev_item *dev_item;
1481 struct btrfs_device *device;
1482 struct btrfs_key key;
1483 u8 fs_uuid[BTRFS_UUID_SIZE];
1484 u8 dev_uuid[BTRFS_UUID_SIZE];
1485 u64 devid;
1486 int ret;
1487
1488 path = btrfs_alloc_path();
1489 if (!path)
1490 return -ENOMEM;
1491
1492 root = root->fs_info->chunk_root;
1493 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1494 key.offset = 0;
1495 key.type = BTRFS_DEV_ITEM_KEY;
1496
1497 while (1) {
1498 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1499 if (ret < 0)
1500 goto error;
1501
1502 leaf = path->nodes[0];
1503next_slot:
1504 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1505 ret = btrfs_next_leaf(root, path);
1506 if (ret > 0)
1507 break;
1508 if (ret < 0)
1509 goto error;
1510 leaf = path->nodes[0];
1511 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1512 btrfs_release_path(root, path);
1513 continue;
1514 }
1515
1516 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1517 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1518 key.type != BTRFS_DEV_ITEM_KEY)
1519 break;
1520
1521 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1522 struct btrfs_dev_item);
1523 devid = btrfs_device_id(leaf, dev_item);
1524 read_extent_buffer(leaf, dev_uuid,
1525 (unsigned long)btrfs_device_uuid(dev_item),
1526 BTRFS_UUID_SIZE);
1527 read_extent_buffer(leaf, fs_uuid,
1528 (unsigned long)btrfs_device_fsid(dev_item),
1529 BTRFS_UUID_SIZE);
1530 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1531 BUG_ON(!device);
1532
1533 if (device->fs_devices->seeding) {
1534 btrfs_set_device_generation(leaf, dev_item,
1535 device->generation);
1536 btrfs_mark_buffer_dirty(leaf);
1537 }
1538
1539 path->slots[0]++;
1540 goto next_slot;
1541 }
1542 ret = 0;
1543error:
1544 btrfs_free_path(path);
1545 return ret;
1546}
1547
Chris Mason788f20e2008-04-28 15:29:42 -04001548int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1549{
1550 struct btrfs_trans_handle *trans;
1551 struct btrfs_device *device;
1552 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001553 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001554 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001555 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001556 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001557 int ret = 0;
1558
Yan Zheng2b820322008-11-17 21:11:30 -05001559 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1560 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001561
Tejun Heod4d77622010-11-13 11:55:18 +01001562 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1563 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001564 if (IS_ERR(bdev))
1565 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001566
Yan Zheng2b820322008-11-17 21:11:30 -05001567 if (root->fs_info->fs_devices->seeding) {
1568 seeding_dev = 1;
1569 down_write(&sb->s_umount);
1570 mutex_lock(&uuid_mutex);
1571 }
1572
Chris Mason8c8bee12008-09-29 11:19:10 -04001573 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001574 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001575
Chris Mason788f20e2008-04-28 15:29:42 -04001576 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001577 /*
1578 * we have the volume lock, so we don't need the extra
1579 * device list mutex while reading the list here.
1580 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001581 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001582 if (device->bdev == bdev) {
1583 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001584 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001585 }
1586 }
1587
1588 device = kzalloc(sizeof(*device), GFP_NOFS);
1589 if (!device) {
1590 /* we can safely leave the fs_devices entry around */
1591 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001592 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001593 }
1594
Chris Mason788f20e2008-04-28 15:29:42 -04001595 device->name = kstrdup(device_path, GFP_NOFS);
1596 if (!device->name) {
1597 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001598 ret = -ENOMEM;
1599 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001600 }
Yan Zheng2b820322008-11-17 21:11:30 -05001601
1602 ret = find_next_devid(root, &device->devid);
1603 if (ret) {
1604 kfree(device);
1605 goto error;
1606 }
1607
Yan, Zhenga22285a2010-05-16 10:48:46 -04001608 trans = btrfs_start_transaction(root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001609 lock_chunks(root);
1610
Yan Zheng2b820322008-11-17 21:11:30 -05001611 device->writeable = 1;
1612 device->work.func = pending_bios_fn;
1613 generate_random_uuid(device->uuid);
1614 spin_lock_init(&device->io_lock);
1615 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001616 device->io_width = root->sectorsize;
1617 device->io_align = root->sectorsize;
1618 device->sector_size = root->sectorsize;
1619 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001620 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001621 device->dev_root = root->fs_info->dev_root;
1622 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001623 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001624 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001625 set_blocksize(device->bdev, 4096);
1626
Yan Zheng2b820322008-11-17 21:11:30 -05001627 if (seeding_dev) {
1628 sb->s_flags &= ~MS_RDONLY;
1629 ret = btrfs_prepare_sprout(trans, root);
1630 BUG_ON(ret);
1631 }
1632
1633 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001634
1635 /*
1636 * we don't want write_supers to jump in here with our device
1637 * half setup
1638 */
1639 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001640 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1641 list_add(&device->dev_alloc_list,
1642 &root->fs_info->fs_devices->alloc_list);
1643 root->fs_info->fs_devices->num_devices++;
1644 root->fs_info->fs_devices->open_devices++;
1645 root->fs_info->fs_devices->rw_devices++;
1646 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1647
Chris Masonc2898112009-06-10 09:51:32 -04001648 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1649 root->fs_info->fs_devices->rotating = 1;
1650
Chris Mason788f20e2008-04-28 15:29:42 -04001651 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1652 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1653 total_bytes + device->total_bytes);
1654
1655 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1656 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1657 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001658 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001659
Yan Zheng2b820322008-11-17 21:11:30 -05001660 if (seeding_dev) {
1661 ret = init_first_rw_device(trans, root, device);
1662 BUG_ON(ret);
1663 ret = btrfs_finish_sprout(trans, root);
1664 BUG_ON(ret);
1665 } else {
1666 ret = btrfs_add_device(trans, root, device);
1667 }
1668
Chris Mason913d9522009-03-10 13:17:18 -04001669 /*
1670 * we've got more storage, clear any full flags on the space
1671 * infos
1672 */
1673 btrfs_clear_space_info_full(root->fs_info);
1674
Chris Mason7d9eb122008-07-08 14:19:17 -04001675 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001676 btrfs_commit_transaction(trans, root);
1677
1678 if (seeding_dev) {
1679 mutex_unlock(&uuid_mutex);
1680 up_write(&sb->s_umount);
1681
1682 ret = btrfs_relocate_sys_chunks(root);
1683 BUG_ON(ret);
1684 }
1685out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001686 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001687 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001688error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001689 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001690 if (seeding_dev) {
1691 mutex_unlock(&uuid_mutex);
1692 up_write(&sb->s_umount);
1693 }
Chris Mason788f20e2008-04-28 15:29:42 -04001694 goto out;
1695}
1696
Chris Masond3977122009-01-05 21:25:51 -05001697static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1698 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001699{
1700 int ret;
1701 struct btrfs_path *path;
1702 struct btrfs_root *root;
1703 struct btrfs_dev_item *dev_item;
1704 struct extent_buffer *leaf;
1705 struct btrfs_key key;
1706
1707 root = device->dev_root->fs_info->chunk_root;
1708
1709 path = btrfs_alloc_path();
1710 if (!path)
1711 return -ENOMEM;
1712
1713 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1714 key.type = BTRFS_DEV_ITEM_KEY;
1715 key.offset = device->devid;
1716
1717 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1718 if (ret < 0)
1719 goto out;
1720
1721 if (ret > 0) {
1722 ret = -ENOENT;
1723 goto out;
1724 }
1725
1726 leaf = path->nodes[0];
1727 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1728
1729 btrfs_set_device_id(leaf, dev_item, device->devid);
1730 btrfs_set_device_type(leaf, dev_item, device->type);
1731 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1732 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1733 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001734 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001735 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1736 btrfs_mark_buffer_dirty(leaf);
1737
1738out:
1739 btrfs_free_path(path);
1740 return ret;
1741}
1742
Chris Mason7d9eb122008-07-08 14:19:17 -04001743static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001744 struct btrfs_device *device, u64 new_size)
1745{
1746 struct btrfs_super_block *super_copy =
1747 &device->dev_root->fs_info->super_copy;
1748 u64 old_total = btrfs_super_total_bytes(super_copy);
1749 u64 diff = new_size - device->total_bytes;
1750
Yan Zheng2b820322008-11-17 21:11:30 -05001751 if (!device->writeable)
1752 return -EACCES;
1753 if (new_size <= device->total_bytes)
1754 return -EINVAL;
1755
Chris Mason8f18cf12008-04-25 16:53:30 -04001756 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001757 device->fs_devices->total_rw_bytes += diff;
1758
1759 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001760 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001761 btrfs_clear_space_info_full(device->dev_root->fs_info);
1762
Chris Mason8f18cf12008-04-25 16:53:30 -04001763 return btrfs_update_device(trans, device);
1764}
1765
Chris Mason7d9eb122008-07-08 14:19:17 -04001766int btrfs_grow_device(struct btrfs_trans_handle *trans,
1767 struct btrfs_device *device, u64 new_size)
1768{
1769 int ret;
1770 lock_chunks(device->dev_root);
1771 ret = __btrfs_grow_device(trans, device, new_size);
1772 unlock_chunks(device->dev_root);
1773 return ret;
1774}
1775
Chris Mason8f18cf12008-04-25 16:53:30 -04001776static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1777 struct btrfs_root *root,
1778 u64 chunk_tree, u64 chunk_objectid,
1779 u64 chunk_offset)
1780{
1781 int ret;
1782 struct btrfs_path *path;
1783 struct btrfs_key key;
1784
1785 root = root->fs_info->chunk_root;
1786 path = btrfs_alloc_path();
1787 if (!path)
1788 return -ENOMEM;
1789
1790 key.objectid = chunk_objectid;
1791 key.offset = chunk_offset;
1792 key.type = BTRFS_CHUNK_ITEM_KEY;
1793
1794 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1795 BUG_ON(ret);
1796
1797 ret = btrfs_del_item(trans, root, path);
1798 BUG_ON(ret);
1799
1800 btrfs_free_path(path);
1801 return 0;
1802}
1803
Christoph Hellwigb2950862008-12-02 09:54:17 -05001804static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001805 chunk_offset)
1806{
1807 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1808 struct btrfs_disk_key *disk_key;
1809 struct btrfs_chunk *chunk;
1810 u8 *ptr;
1811 int ret = 0;
1812 u32 num_stripes;
1813 u32 array_size;
1814 u32 len = 0;
1815 u32 cur;
1816 struct btrfs_key key;
1817
1818 array_size = btrfs_super_sys_array_size(super_copy);
1819
1820 ptr = super_copy->sys_chunk_array;
1821 cur = 0;
1822
1823 while (cur < array_size) {
1824 disk_key = (struct btrfs_disk_key *)ptr;
1825 btrfs_disk_key_to_cpu(&key, disk_key);
1826
1827 len = sizeof(*disk_key);
1828
1829 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1830 chunk = (struct btrfs_chunk *)(ptr + len);
1831 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1832 len += btrfs_chunk_item_size(num_stripes);
1833 } else {
1834 ret = -EIO;
1835 break;
1836 }
1837 if (key.objectid == chunk_objectid &&
1838 key.offset == chunk_offset) {
1839 memmove(ptr, ptr + len, array_size - (cur + len));
1840 array_size -= len;
1841 btrfs_set_super_sys_array_size(super_copy, array_size);
1842 } else {
1843 ptr += len;
1844 cur += len;
1845 }
1846 }
1847 return ret;
1848}
1849
Christoph Hellwigb2950862008-12-02 09:54:17 -05001850static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001851 u64 chunk_tree, u64 chunk_objectid,
1852 u64 chunk_offset)
1853{
1854 struct extent_map_tree *em_tree;
1855 struct btrfs_root *extent_root;
1856 struct btrfs_trans_handle *trans;
1857 struct extent_map *em;
1858 struct map_lookup *map;
1859 int ret;
1860 int i;
1861
1862 root = root->fs_info->chunk_root;
1863 extent_root = root->fs_info->extent_root;
1864 em_tree = &root->fs_info->mapping_tree.map_tree;
1865
Josef Bacikba1bf482009-09-11 16:11:19 -04001866 ret = btrfs_can_relocate(extent_root, chunk_offset);
1867 if (ret)
1868 return -ENOSPC;
1869
Chris Mason8f18cf12008-04-25 16:53:30 -04001870 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001871 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001872 if (ret)
1873 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001874
Yan, Zhenga22285a2010-05-16 10:48:46 -04001875 trans = btrfs_start_transaction(root, 0);
Chris Mason8f18cf12008-04-25 16:53:30 -04001876 BUG_ON(!trans);
1877
Chris Mason7d9eb122008-07-08 14:19:17 -04001878 lock_chunks(root);
1879
Chris Mason8f18cf12008-04-25 16:53:30 -04001880 /*
1881 * step two, delete the device extents and the
1882 * chunk tree entries
1883 */
Chris Mason890871b2009-09-02 16:24:52 -04001884 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001885 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001886 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001887
Chris Masona061fc82008-05-07 11:43:44 -04001888 BUG_ON(em->start > chunk_offset ||
1889 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001890 map = (struct map_lookup *)em->bdev;
1891
1892 for (i = 0; i < map->num_stripes; i++) {
1893 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1894 map->stripes[i].physical);
1895 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001896
Chris Masondfe25022008-05-13 13:46:40 -04001897 if (map->stripes[i].dev) {
1898 ret = btrfs_update_device(trans, map->stripes[i].dev);
1899 BUG_ON(ret);
1900 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001901 }
1902 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1903 chunk_offset);
1904
1905 BUG_ON(ret);
1906
1907 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1908 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1909 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001910 }
1911
Zheng Yan1a40e232008-09-26 10:09:34 -04001912 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1913 BUG_ON(ret);
1914
Chris Mason890871b2009-09-02 16:24:52 -04001915 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001916 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001917 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001918
Chris Mason8f18cf12008-04-25 16:53:30 -04001919 kfree(map);
1920 em->bdev = NULL;
1921
1922 /* once for the tree */
1923 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001924 /* once for us */
1925 free_extent_map(em);
1926
Chris Mason7d9eb122008-07-08 14:19:17 -04001927 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001928 btrfs_end_transaction(trans, root);
1929 return 0;
1930}
1931
Yan Zheng2b820322008-11-17 21:11:30 -05001932static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1933{
1934 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1935 struct btrfs_path *path;
1936 struct extent_buffer *leaf;
1937 struct btrfs_chunk *chunk;
1938 struct btrfs_key key;
1939 struct btrfs_key found_key;
1940 u64 chunk_tree = chunk_root->root_key.objectid;
1941 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001942 bool retried = false;
1943 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001944 int ret;
1945
1946 path = btrfs_alloc_path();
1947 if (!path)
1948 return -ENOMEM;
1949
Josef Bacikba1bf482009-09-11 16:11:19 -04001950again:
Yan Zheng2b820322008-11-17 21:11:30 -05001951 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1952 key.offset = (u64)-1;
1953 key.type = BTRFS_CHUNK_ITEM_KEY;
1954
1955 while (1) {
1956 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1957 if (ret < 0)
1958 goto error;
1959 BUG_ON(ret == 0);
1960
1961 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1962 key.type);
1963 if (ret < 0)
1964 goto error;
1965 if (ret > 0)
1966 break;
1967
1968 leaf = path->nodes[0];
1969 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1970
1971 chunk = btrfs_item_ptr(leaf, path->slots[0],
1972 struct btrfs_chunk);
1973 chunk_type = btrfs_chunk_type(leaf, chunk);
1974 btrfs_release_path(chunk_root, path);
1975
1976 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1977 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1978 found_key.objectid,
1979 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001980 if (ret == -ENOSPC)
1981 failed++;
1982 else if (ret)
1983 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001984 }
1985
1986 if (found_key.offset == 0)
1987 break;
1988 key.offset = found_key.offset - 1;
1989 }
1990 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001991 if (failed && !retried) {
1992 failed = 0;
1993 retried = true;
1994 goto again;
1995 } else if (failed && retried) {
1996 WARN_ON(1);
1997 ret = -ENOSPC;
1998 }
Yan Zheng2b820322008-11-17 21:11:30 -05001999error:
2000 btrfs_free_path(path);
2001 return ret;
2002}
2003
Chris Masonec44a352008-04-28 15:29:52 -04002004static u64 div_factor(u64 num, int factor)
2005{
2006 if (factor == 10)
2007 return num;
2008 num *= factor;
2009 do_div(num, 10);
2010 return num;
2011}
2012
Chris Masonec44a352008-04-28 15:29:52 -04002013int btrfs_balance(struct btrfs_root *dev_root)
2014{
2015 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002016 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2017 struct btrfs_device *device;
2018 u64 old_size;
2019 u64 size_to_free;
2020 struct btrfs_path *path;
2021 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002022 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2023 struct btrfs_trans_handle *trans;
2024 struct btrfs_key found_key;
2025
Yan Zheng2b820322008-11-17 21:11:30 -05002026 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2027 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002028
Ben Hutchings6f88a442010-12-29 14:55:03 +00002029 if (!capable(CAP_SYS_ADMIN))
2030 return -EPERM;
2031
Chris Mason7d9eb122008-07-08 14:19:17 -04002032 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002033 dev_root = dev_root->fs_info->dev_root;
2034
Chris Masonec44a352008-04-28 15:29:52 -04002035 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002036 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002037 old_size = device->total_bytes;
2038 size_to_free = div_factor(old_size, 1);
2039 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002040 if (!device->writeable ||
2041 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002042 continue;
2043
2044 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002045 if (ret == -ENOSPC)
2046 break;
Chris Masonec44a352008-04-28 15:29:52 -04002047 BUG_ON(ret);
2048
Yan, Zhenga22285a2010-05-16 10:48:46 -04002049 trans = btrfs_start_transaction(dev_root, 0);
Chris Masonec44a352008-04-28 15:29:52 -04002050 BUG_ON(!trans);
2051
2052 ret = btrfs_grow_device(trans, device, old_size);
2053 BUG_ON(ret);
2054
2055 btrfs_end_transaction(trans, dev_root);
2056 }
2057
2058 /* step two, relocate all the chunks */
2059 path = btrfs_alloc_path();
2060 BUG_ON(!path);
2061
2062 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2063 key.offset = (u64)-1;
2064 key.type = BTRFS_CHUNK_ITEM_KEY;
2065
Chris Masond3977122009-01-05 21:25:51 -05002066 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002067 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2068 if (ret < 0)
2069 goto error;
2070
2071 /*
2072 * this shouldn't happen, it means the last relocate
2073 * failed
2074 */
2075 if (ret == 0)
2076 break;
2077
2078 ret = btrfs_previous_item(chunk_root, path, 0,
2079 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002080 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002081 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002082
Chris Masonec44a352008-04-28 15:29:52 -04002083 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2084 path->slots[0]);
2085 if (found_key.objectid != key.objectid)
2086 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002087
Chris Masonec44a352008-04-28 15:29:52 -04002088 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002089 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002090 break;
2091
Chris Mason7d9eb122008-07-08 14:19:17 -04002092 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002093 ret = btrfs_relocate_chunk(chunk_root,
2094 chunk_root->root_key.objectid,
2095 found_key.objectid,
2096 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002097 BUG_ON(ret && ret != -ENOSPC);
2098 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002099 }
2100 ret = 0;
2101error:
2102 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002103 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002104 return ret;
2105}
2106
Chris Mason8f18cf12008-04-25 16:53:30 -04002107/*
2108 * shrinking a device means finding all of the device extents past
2109 * the new size, and then following the back refs to the chunks.
2110 * The chunk relocation code actually frees the device extent
2111 */
2112int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2113{
2114 struct btrfs_trans_handle *trans;
2115 struct btrfs_root *root = device->dev_root;
2116 struct btrfs_dev_extent *dev_extent = NULL;
2117 struct btrfs_path *path;
2118 u64 length;
2119 u64 chunk_tree;
2120 u64 chunk_objectid;
2121 u64 chunk_offset;
2122 int ret;
2123 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002124 int failed = 0;
2125 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002126 struct extent_buffer *l;
2127 struct btrfs_key key;
2128 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2129 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002130 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002131 u64 diff = device->total_bytes - new_size;
2132
Yan Zheng2b820322008-11-17 21:11:30 -05002133 if (new_size >= device->total_bytes)
2134 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002135
2136 path = btrfs_alloc_path();
2137 if (!path)
2138 return -ENOMEM;
2139
Chris Mason8f18cf12008-04-25 16:53:30 -04002140 path->reada = 2;
2141
Chris Mason7d9eb122008-07-08 14:19:17 -04002142 lock_chunks(root);
2143
Chris Mason8f18cf12008-04-25 16:53:30 -04002144 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002145 if (device->writeable)
2146 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002147 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002148
Josef Bacikba1bf482009-09-11 16:11:19 -04002149again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002150 key.objectid = device->devid;
2151 key.offset = (u64)-1;
2152 key.type = BTRFS_DEV_EXTENT_KEY;
2153
2154 while (1) {
2155 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2156 if (ret < 0)
2157 goto done;
2158
2159 ret = btrfs_previous_item(root, path, 0, key.type);
2160 if (ret < 0)
2161 goto done;
2162 if (ret) {
2163 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002164 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002165 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002166 }
2167
2168 l = path->nodes[0];
2169 slot = path->slots[0];
2170 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2171
Josef Bacikba1bf482009-09-11 16:11:19 -04002172 if (key.objectid != device->devid) {
2173 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002174 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002175 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002176
2177 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2178 length = btrfs_dev_extent_length(l, dev_extent);
2179
Josef Bacikba1bf482009-09-11 16:11:19 -04002180 if (key.offset + length <= new_size) {
2181 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002182 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002183 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002184
2185 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2186 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2187 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2188 btrfs_release_path(root, path);
2189
2190 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2191 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002192 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002193 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002194 if (ret == -ENOSPC)
2195 failed++;
2196 key.offset -= 1;
2197 }
2198
2199 if (failed && !retried) {
2200 failed = 0;
2201 retried = true;
2202 goto again;
2203 } else if (failed && retried) {
2204 ret = -ENOSPC;
2205 lock_chunks(root);
2206
2207 device->total_bytes = old_size;
2208 if (device->writeable)
2209 device->fs_devices->total_rw_bytes += diff;
2210 unlock_chunks(root);
2211 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002212 }
2213
Chris Balld6397ba2009-04-27 07:29:03 -04002214 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002215 trans = btrfs_start_transaction(root, 0);
Chris Balld6397ba2009-04-27 07:29:03 -04002216 lock_chunks(root);
2217
2218 device->disk_total_bytes = new_size;
2219 /* Now btrfs_update_device() will change the on-disk size. */
2220 ret = btrfs_update_device(trans, device);
2221 if (ret) {
2222 unlock_chunks(root);
2223 btrfs_end_transaction(trans, root);
2224 goto done;
2225 }
2226 WARN_ON(diff > old_total);
2227 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2228 unlock_chunks(root);
2229 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002230done:
2231 btrfs_free_path(path);
2232 return ret;
2233}
2234
Christoph Hellwigb2950862008-12-02 09:54:17 -05002235static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002236 struct btrfs_root *root,
2237 struct btrfs_key *key,
2238 struct btrfs_chunk *chunk, int item_size)
2239{
2240 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2241 struct btrfs_disk_key disk_key;
2242 u32 array_size;
2243 u8 *ptr;
2244
2245 array_size = btrfs_super_sys_array_size(super_copy);
2246 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2247 return -EFBIG;
2248
2249 ptr = super_copy->sys_chunk_array + array_size;
2250 btrfs_cpu_key_to_disk(&disk_key, key);
2251 memcpy(ptr, &disk_key, sizeof(disk_key));
2252 ptr += sizeof(disk_key);
2253 memcpy(ptr, chunk, item_size);
2254 item_size += sizeof(disk_key);
2255 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2256 return 0;
2257}
2258
Chris Masond3977122009-01-05 21:25:51 -05002259static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002260 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002261{
2262 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2263 return calc_size;
2264 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2265 return calc_size * (num_stripes / sub_stripes);
2266 else
2267 return calc_size * num_stripes;
2268}
2269
Miao Xieb2117a32011-01-05 10:07:28 +00002270/* Used to sort the devices by max_avail(descending sort) */
2271int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
Chris Mason0b86a832008-03-24 15:01:56 -04002272{
Miao Xieb2117a32011-01-05 10:07:28 +00002273 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2274 ((struct btrfs_device_info *)dev_info2)->max_avail)
2275 return -1;
2276 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2277 ((struct btrfs_device_info *)dev_info2)->max_avail)
2278 return 1;
2279 else
2280 return 0;
2281}
Chris Mason0b86a832008-03-24 15:01:56 -04002282
Miao Xieb2117a32011-01-05 10:07:28 +00002283static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2284 int *num_stripes, int *min_stripes,
2285 int *sub_stripes)
2286{
2287 *num_stripes = 1;
2288 *min_stripes = 1;
2289 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002290
Chris Masona40a90a2008-04-18 11:55:51 -04002291 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002292 *num_stripes = fs_devices->rw_devices;
2293 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002294 }
2295 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002296 *num_stripes = 2;
2297 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002298 }
Chris Mason8790d502008-04-03 16:29:03 -04002299 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002300 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002301 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002302 *num_stripes = 2;
2303 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002304 }
Chris Mason321aecc2008-04-16 10:49:51 -04002305 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002306 *num_stripes = fs_devices->rw_devices;
2307 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002308 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002309 *num_stripes &= ~(u32)1;
2310 *sub_stripes = 2;
2311 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002312 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002313
Miao Xieb2117a32011-01-05 10:07:28 +00002314 return 0;
2315}
2316
2317static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2318 u64 proposed_size, u64 type,
2319 int num_stripes, int small_stripe)
2320{
2321 int min_stripe_size = 1 * 1024 * 1024;
2322 u64 calc_size = proposed_size;
2323 u64 max_chunk_size = calc_size;
2324 int ncopies = 1;
2325
2326 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2327 BTRFS_BLOCK_GROUP_DUP |
2328 BTRFS_BLOCK_GROUP_RAID10))
2329 ncopies = 2;
2330
Chris Mason9b3f68b2008-04-18 10:29:51 -04002331 if (type & BTRFS_BLOCK_GROUP_DATA) {
2332 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002333 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002334 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002335 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002336 min_stripe_size = 32 * 1024 * 1024;
2337 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2338 calc_size = 8 * 1024 * 1024;
2339 max_chunk_size = calc_size * 2;
2340 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002341 }
2342
Yan Zheng2b820322008-11-17 21:11:30 -05002343 /* we don't want a chunk larger than 10% of writeable space */
2344 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2345 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002346
Miao Xie1974a3b2011-01-05 10:07:24 +00002347 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2348 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002349 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002350 do_div(calc_size, BTRFS_STRIPE_LEN);
2351 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002352 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002353
Chris Mason9b3f68b2008-04-18 10:29:51 -04002354 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002355 if (!small_stripe)
Josef Bacik0cad8a112010-03-17 20:45:56 +00002356 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002357
Chris Mason9f680ce2010-04-06 09:37:47 -04002358 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002359 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002360 * we end up with something bigger than a stripe
2361 */
Miao Xieb2117a32011-01-05 10:07:28 +00002362 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002363
Miao Xieb2117a32011-01-05 10:07:28 +00002364 do_div(calc_size, BTRFS_STRIPE_LEN);
2365 calc_size *= BTRFS_STRIPE_LEN;
2366
2367 return calc_size;
2368}
2369
2370static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2371 int num_stripes)
2372{
2373 struct map_lookup *new;
2374 size_t len = map_lookup_size(num_stripes);
2375
2376 BUG_ON(map->num_stripes < num_stripes);
2377
2378 if (map->num_stripes == num_stripes)
2379 return map;
2380
2381 new = kmalloc(len, GFP_NOFS);
2382 if (!new) {
2383 /* just change map->num_stripes */
2384 map->num_stripes = num_stripes;
2385 return map;
2386 }
2387
2388 memcpy(new, map, len);
2389 new->num_stripes = num_stripes;
2390 kfree(map);
2391 return new;
2392}
2393
2394/*
2395 * helper to allocate device space from btrfs_device_info, in which we stored
2396 * max free space information of every device. It is used when we can not
2397 * allocate chunks by default size.
2398 *
2399 * By this helper, we can allocate a new chunk as larger as possible.
2400 */
2401static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2402 struct btrfs_fs_devices *fs_devices,
2403 struct btrfs_device_info *devices,
2404 int nr_device, u64 type,
2405 struct map_lookup **map_lookup,
2406 int min_stripes, u64 *stripe_size)
2407{
2408 int i, index, sort_again = 0;
2409 int min_devices = min_stripes;
2410 u64 max_avail, min_free;
2411 struct map_lookup *map = *map_lookup;
2412 int ret;
2413
2414 if (nr_device < min_stripes)
2415 return -ENOSPC;
2416
2417 btrfs_descending_sort_devices(devices, nr_device);
2418
2419 max_avail = devices[0].max_avail;
2420 if (!max_avail)
2421 return -ENOSPC;
2422
2423 for (i = 0; i < nr_device; i++) {
2424 /*
2425 * if dev_offset = 0, it means the free space of this device
2426 * is less than what we need, and we didn't search max avail
2427 * extent on this device, so do it now.
2428 */
2429 if (!devices[i].dev_offset) {
2430 ret = find_free_dev_extent(trans, devices[i].dev,
2431 max_avail,
2432 &devices[i].dev_offset,
2433 &devices[i].max_avail);
2434 if (ret != 0 && ret != -ENOSPC)
2435 return ret;
2436 sort_again = 1;
2437 }
2438 }
2439
2440 /* we update the max avail free extent of each devices, sort again */
2441 if (sort_again)
2442 btrfs_descending_sort_devices(devices, nr_device);
2443
2444 if (type & BTRFS_BLOCK_GROUP_DUP)
2445 min_devices = 1;
2446
2447 if (!devices[min_devices - 1].max_avail)
2448 return -ENOSPC;
2449
2450 max_avail = devices[min_devices - 1].max_avail;
2451 if (type & BTRFS_BLOCK_GROUP_DUP)
2452 do_div(max_avail, 2);
2453
2454 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2455 min_stripes, 1);
2456 if (type & BTRFS_BLOCK_GROUP_DUP)
2457 min_free = max_avail * 2;
2458 else
2459 min_free = max_avail;
2460
2461 if (min_free > devices[min_devices - 1].max_avail)
2462 return -ENOSPC;
2463
2464 map = __shrink_map_lookup_stripes(map, min_stripes);
2465 *stripe_size = max_avail;
2466
2467 index = 0;
2468 for (i = 0; i < min_stripes; i++) {
2469 map->stripes[i].dev = devices[index].dev;
2470 map->stripes[i].physical = devices[index].dev_offset;
2471 if (type & BTRFS_BLOCK_GROUP_DUP) {
2472 i++;
2473 map->stripes[i].dev = devices[index].dev;
2474 map->stripes[i].physical = devices[index].dev_offset +
2475 max_avail;
2476 }
2477 index++;
2478 }
2479 *map_lookup = map;
2480
2481 return 0;
2482}
2483
2484static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2485 struct btrfs_root *extent_root,
2486 struct map_lookup **map_ret,
2487 u64 *num_bytes, u64 *stripe_size,
2488 u64 start, u64 type)
2489{
2490 struct btrfs_fs_info *info = extent_root->fs_info;
2491 struct btrfs_device *device = NULL;
2492 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2493 struct list_head *cur;
2494 struct map_lookup *map;
2495 struct extent_map_tree *em_tree;
2496 struct extent_map *em;
2497 struct btrfs_device_info *devices_info;
2498 struct list_head private_devs;
2499 u64 calc_size = 1024 * 1024 * 1024;
2500 u64 min_free;
2501 u64 avail;
2502 u64 dev_offset;
2503 int num_stripes;
2504 int min_stripes;
2505 int sub_stripes;
2506 int min_devices; /* the min number of devices we need */
2507 int i;
2508 int ret;
2509 int index;
2510
2511 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2512 (type & BTRFS_BLOCK_GROUP_DUP)) {
2513 WARN_ON(1);
2514 type &= ~BTRFS_BLOCK_GROUP_DUP;
2515 }
2516 if (list_empty(&fs_devices->alloc_list))
2517 return -ENOSPC;
2518
2519 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2520 &min_stripes, &sub_stripes);
2521 if (ret)
2522 return ret;
2523
2524 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2525 GFP_NOFS);
2526 if (!devices_info)
2527 return -ENOMEM;
2528
2529 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2530 if (!map) {
2531 ret = -ENOMEM;
2532 goto error;
2533 }
2534 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002535
Yan Zheng2b820322008-11-17 21:11:30 -05002536 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002537 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002538 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002539
Miao Xieb2117a32011-01-05 10:07:28 +00002540 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2541 num_stripes, 0);
2542
2543 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002544 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002545 min_devices = 1;
2546 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002547 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002548 min_devices = min_stripes;
2549 }
Chris Masonad5bd912008-04-21 08:28:10 -04002550
Yan Zheng2b820322008-11-17 21:11:30 -05002551 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002552 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002553 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002554 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002555 if (device->total_bytes > device->bytes_used)
2556 avail = device->total_bytes - device->bytes_used;
2557 else
2558 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002559 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002560
Chris Masondfe25022008-05-13 13:46:40 -04002561 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002562 ret = find_free_dev_extent(trans, device, min_free,
2563 &devices_info[i].dev_offset,
2564 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002565 if (ret == 0) {
2566 list_move_tail(&device->dev_alloc_list,
2567 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002568 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002569 map->stripes[index].physical =
2570 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002571 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002572 if (type & BTRFS_BLOCK_GROUP_DUP) {
2573 map->stripes[index].dev = device;
2574 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002575 devices_info[i].dev_offset +
2576 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002577 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002578 }
Miao Xieb2117a32011-01-05 10:07:28 +00002579 } else if (ret != -ENOSPC)
2580 goto error;
2581
2582 devices_info[i].dev = device;
2583 i++;
2584 } else if (device->in_fs_metadata &&
2585 avail >= BTRFS_STRIPE_LEN) {
2586 devices_info[i].dev = device;
2587 devices_info[i].max_avail = avail;
2588 i++;
2589 }
2590
Yan Zheng2b820322008-11-17 21:11:30 -05002591 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002592 break;
2593 }
Miao Xieb2117a32011-01-05 10:07:28 +00002594
Yan Zheng2b820322008-11-17 21:11:30 -05002595 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002596 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002597 if (index >= min_stripes) {
2598 num_stripes = index;
2599 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2600 num_stripes /= sub_stripes;
2601 num_stripes *= sub_stripes;
2602 }
Miao Xieb2117a32011-01-05 10:07:28 +00002603
2604 map = __shrink_map_lookup_stripes(map, num_stripes);
2605 } else if (i >= min_devices) {
2606 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2607 devices_info, i, type,
2608 &map, min_stripes,
2609 &calc_size);
2610 if (ret)
2611 goto error;
2612 } else {
2613 ret = -ENOSPC;
2614 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002615 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002616 }
Chris Mason593060d2008-03-25 16:50:33 -04002617 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002618 map->stripe_len = BTRFS_STRIPE_LEN;
2619 map->io_align = BTRFS_STRIPE_LEN;
2620 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002621 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002622 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002623
Yan Zheng2b820322008-11-17 21:11:30 -05002624 *map_ret = map;
2625 *stripe_size = calc_size;
2626 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002627 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002628
2629 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002630 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002631 ret = -ENOMEM;
2632 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002633 }
Chris Mason0b86a832008-03-24 15:01:56 -04002634 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002635 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002636 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002637 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002638 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002639
Chris Mason0b86a832008-03-24 15:01:56 -04002640 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002641 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002642 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002643 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002644 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002645 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002646
2647 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2648 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2649 start, *num_bytes);
2650 BUG_ON(ret);
2651
2652 index = 0;
2653 while (index < map->num_stripes) {
2654 device = map->stripes[index].dev;
2655 dev_offset = map->stripes[index].physical;
2656
2657 ret = btrfs_alloc_dev_extent(trans, device,
2658 info->chunk_root->root_key.objectid,
2659 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2660 start, dev_offset, calc_size);
2661 BUG_ON(ret);
2662 index++;
2663 }
2664
Miao Xieb2117a32011-01-05 10:07:28 +00002665 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002666 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002667
2668error:
2669 kfree(map);
2670 kfree(devices_info);
2671 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002672}
2673
2674static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2675 struct btrfs_root *extent_root,
2676 struct map_lookup *map, u64 chunk_offset,
2677 u64 chunk_size, u64 stripe_size)
2678{
2679 u64 dev_offset;
2680 struct btrfs_key key;
2681 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2682 struct btrfs_device *device;
2683 struct btrfs_chunk *chunk;
2684 struct btrfs_stripe *stripe;
2685 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2686 int index = 0;
2687 int ret;
2688
2689 chunk = kzalloc(item_size, GFP_NOFS);
2690 if (!chunk)
2691 return -ENOMEM;
2692
2693 index = 0;
2694 while (index < map->num_stripes) {
2695 device = map->stripes[index].dev;
2696 device->bytes_used += stripe_size;
2697 ret = btrfs_update_device(trans, device);
2698 BUG_ON(ret);
2699 index++;
2700 }
2701
2702 index = 0;
2703 stripe = &chunk->stripe;
2704 while (index < map->num_stripes) {
2705 device = map->stripes[index].dev;
2706 dev_offset = map->stripes[index].physical;
2707
2708 btrfs_set_stack_stripe_devid(stripe, device->devid);
2709 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2710 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2711 stripe++;
2712 index++;
2713 }
2714
2715 btrfs_set_stack_chunk_length(chunk, chunk_size);
2716 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2717 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2718 btrfs_set_stack_chunk_type(chunk, map->type);
2719 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2720 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2721 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2722 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2723 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2724
2725 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2726 key.type = BTRFS_CHUNK_ITEM_KEY;
2727 key.offset = chunk_offset;
2728
2729 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2730 BUG_ON(ret);
2731
2732 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2733 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2734 item_size);
2735 BUG_ON(ret);
2736 }
2737 kfree(chunk);
2738 return 0;
2739}
2740
2741/*
2742 * Chunk allocation falls into two parts. The first part does works
2743 * that make the new allocated chunk useable, but not do any operation
2744 * that modifies the chunk tree. The second part does the works that
2745 * require modifying the chunk tree. This division is important for the
2746 * bootstrap process of adding storage to a seed btrfs.
2747 */
2748int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2749 struct btrfs_root *extent_root, u64 type)
2750{
2751 u64 chunk_offset;
2752 u64 chunk_size;
2753 u64 stripe_size;
2754 struct map_lookup *map;
2755 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2756 int ret;
2757
2758 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2759 &chunk_offset);
2760 if (ret)
2761 return ret;
2762
2763 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2764 &stripe_size, chunk_offset, type);
2765 if (ret)
2766 return ret;
2767
2768 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2769 chunk_size, stripe_size);
2770 BUG_ON(ret);
2771 return 0;
2772}
2773
Chris Masond3977122009-01-05 21:25:51 -05002774static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002775 struct btrfs_root *root,
2776 struct btrfs_device *device)
2777{
2778 u64 chunk_offset;
2779 u64 sys_chunk_offset;
2780 u64 chunk_size;
2781 u64 sys_chunk_size;
2782 u64 stripe_size;
2783 u64 sys_stripe_size;
2784 u64 alloc_profile;
2785 struct map_lookup *map;
2786 struct map_lookup *sys_map;
2787 struct btrfs_fs_info *fs_info = root->fs_info;
2788 struct btrfs_root *extent_root = fs_info->extent_root;
2789 int ret;
2790
2791 ret = find_next_chunk(fs_info->chunk_root,
2792 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2793 BUG_ON(ret);
2794
2795 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2796 (fs_info->metadata_alloc_profile &
2797 fs_info->avail_metadata_alloc_bits);
2798 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2799
2800 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2801 &stripe_size, chunk_offset, alloc_profile);
2802 BUG_ON(ret);
2803
2804 sys_chunk_offset = chunk_offset + chunk_size;
2805
2806 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2807 (fs_info->system_alloc_profile &
2808 fs_info->avail_system_alloc_bits);
2809 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2810
2811 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2812 &sys_chunk_size, &sys_stripe_size,
2813 sys_chunk_offset, alloc_profile);
2814 BUG_ON(ret);
2815
2816 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2817 BUG_ON(ret);
2818
2819 /*
2820 * Modifying chunk tree needs allocating new blocks from both
2821 * system block group and metadata block group. So we only can
2822 * do operations require modifying the chunk tree after both
2823 * block groups were created.
2824 */
2825 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2826 chunk_size, stripe_size);
2827 BUG_ON(ret);
2828
2829 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2830 sys_chunk_offset, sys_chunk_size,
2831 sys_stripe_size);
2832 BUG_ON(ret);
2833 return 0;
2834}
2835
2836int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2837{
2838 struct extent_map *em;
2839 struct map_lookup *map;
2840 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2841 int readonly = 0;
2842 int i;
2843
Chris Mason890871b2009-09-02 16:24:52 -04002844 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002845 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002846 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002847 if (!em)
2848 return 1;
2849
Josef Bacikf48b9072010-01-27 02:07:59 +00002850 if (btrfs_test_opt(root, DEGRADED)) {
2851 free_extent_map(em);
2852 return 0;
2853 }
2854
Yan Zheng2b820322008-11-17 21:11:30 -05002855 map = (struct map_lookup *)em->bdev;
2856 for (i = 0; i < map->num_stripes; i++) {
2857 if (!map->stripes[i].dev->writeable) {
2858 readonly = 1;
2859 break;
2860 }
2861 }
2862 free_extent_map(em);
2863 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002864}
2865
2866void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2867{
2868 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2869}
2870
2871void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2872{
2873 struct extent_map *em;
2874
Chris Masond3977122009-01-05 21:25:51 -05002875 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002876 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002877 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2878 if (em)
2879 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002880 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002881 if (!em)
2882 break;
2883 kfree(em->bdev);
2884 /* once for us */
2885 free_extent_map(em);
2886 /* once for the tree */
2887 free_extent_map(em);
2888 }
2889}
2890
Chris Masonf1885912008-04-09 16:28:12 -04002891int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2892{
2893 struct extent_map *em;
2894 struct map_lookup *map;
2895 struct extent_map_tree *em_tree = &map_tree->map_tree;
2896 int ret;
2897
Chris Mason890871b2009-09-02 16:24:52 -04002898 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002899 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002900 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002901 BUG_ON(!em);
2902
2903 BUG_ON(em->start > logical || em->start + em->len < logical);
2904 map = (struct map_lookup *)em->bdev;
2905 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2906 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002907 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2908 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002909 else
2910 ret = 1;
2911 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002912 return ret;
2913}
2914
Chris Masondfe25022008-05-13 13:46:40 -04002915static int find_live_mirror(struct map_lookup *map, int first, int num,
2916 int optimal)
2917{
2918 int i;
2919 if (map->stripes[optimal].dev->bdev)
2920 return optimal;
2921 for (i = first; i < first + num; i++) {
2922 if (map->stripes[i].dev->bdev)
2923 return i;
2924 }
2925 /* we couldn't find one that doesn't fail. Just return something
2926 * and the io error handling code will clean up eventually
2927 */
2928 return optimal;
2929}
2930
Chris Masonf2d8d742008-04-21 10:03:05 -04002931static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2932 u64 logical, u64 *length,
2933 struct btrfs_multi_bio **multi_ret,
2934 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002935{
2936 struct extent_map *em;
2937 struct map_lookup *map;
2938 struct extent_map_tree *em_tree = &map_tree->map_tree;
2939 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002940 u64 stripe_offset;
2941 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002942 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002943 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002944 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002945 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002946 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002947 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002948 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002949
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002950 if (multi_ret && !(rw & REQ_WRITE))
Chris Masoncea9e442008-04-09 16:28:12 -04002951 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002952again:
2953 if (multi_ret) {
2954 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2955 GFP_NOFS);
2956 if (!multi)
2957 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002958
2959 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002960 }
Chris Mason0b86a832008-03-24 15:01:56 -04002961
Chris Mason890871b2009-09-02 16:24:52 -04002962 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002963 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002964 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002965
Jiri Slaby2423fdfb2010-01-06 16:57:22 +00002966 if (!em && unplug_page) {
2967 kfree(multi);
Chris Masonf2d8d742008-04-21 10:03:05 -04002968 return 0;
Jiri Slaby2423fdfb2010-01-06 16:57:22 +00002969 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002970
Chris Mason3b951512008-04-17 11:29:12 -04002971 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002972 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2973 (unsigned long long)logical,
2974 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002975 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002976 }
Chris Mason0b86a832008-03-24 15:01:56 -04002977
2978 BUG_ON(em->start > logical || em->start + em->len < logical);
2979 map = (struct map_lookup *)em->bdev;
2980 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002981
Chris Masonf1885912008-04-09 16:28:12 -04002982 if (mirror_num > map->num_stripes)
2983 mirror_num = 0;
2984
Chris Masoncea9e442008-04-09 16:28:12 -04002985 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002986 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002987 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2988 BTRFS_BLOCK_GROUP_DUP)) {
2989 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002990 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002991 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2992 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002993 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002994 }
2995 }
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002996 if (multi_ret && (rw & REQ_WRITE) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002997 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002998 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002999 free_extent_map(em);
3000 kfree(multi);
3001 goto again;
3002 }
Chris Mason593060d2008-03-25 16:50:33 -04003003 stripe_nr = offset;
3004 /*
3005 * stripe_nr counts the total number of stripes we have to stride
3006 * to get to this block
3007 */
3008 do_div(stripe_nr, map->stripe_len);
3009
3010 stripe_offset = stripe_nr * map->stripe_len;
3011 BUG_ON(offset < stripe_offset);
3012
3013 /* stripe_offset is the offset of this block in its stripe*/
3014 stripe_offset = offset - stripe_offset;
3015
Chris Masoncea9e442008-04-09 16:28:12 -04003016 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003017 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04003018 BTRFS_BLOCK_GROUP_DUP)) {
3019 /* we limit the length of each bio to what fits in a stripe */
3020 *length = min_t(u64, em->len - offset,
3021 map->stripe_len - stripe_offset);
3022 } else {
3023 *length = em->len - offset;
3024 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003025
3026 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04003027 goto out;
3028
Chris Masonf2d8d742008-04-21 10:03:05 -04003029 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003030 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003031 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003032 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04003033 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003034 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003035 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003036 else {
3037 stripe_index = find_live_mirror(map, 0,
3038 map->num_stripes,
3039 current->pid % map->num_stripes);
3040 }
Chris Mason2fff7342008-04-29 14:12:09 -04003041
Chris Mason611f0e02008-04-03 16:29:03 -04003042 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003043 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003044 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003045 else if (mirror_num)
3046 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003047
Chris Mason321aecc2008-04-16 10:49:51 -04003048 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3049 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003050
3051 stripe_index = do_div(stripe_nr, factor);
3052 stripe_index *= map->sub_stripes;
3053
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003054 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04003055 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003056 else if (mirror_num)
3057 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003058 else {
3059 stripe_index = find_live_mirror(map, stripe_index,
3060 map->sub_stripes, stripe_index +
3061 current->pid % map->sub_stripes);
3062 }
Chris Mason8790d502008-04-03 16:29:03 -04003063 } else {
3064 /*
3065 * after this do_div call, stripe_nr is the number of stripes
3066 * on this device we have to walk to find the data, and
3067 * stripe_index is the number of our device in the stripe array
3068 */
3069 stripe_index = do_div(stripe_nr, map->num_stripes);
3070 }
Chris Mason593060d2008-03-25 16:50:33 -04003071 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003072
Chris Masonf2d8d742008-04-21 10:03:05 -04003073 for (i = 0; i < num_stripes; i++) {
3074 if (unplug_page) {
3075 struct btrfs_device *device;
3076 struct backing_dev_info *bdi;
3077
3078 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04003079 if (device->bdev) {
3080 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05003081 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04003082 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04003083 }
3084 } else {
3085 multi->stripes[i].physical =
3086 map->stripes[stripe_index].physical +
3087 stripe_offset + stripe_nr * map->stripe_len;
3088 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3089 }
Chris Masoncea9e442008-04-09 16:28:12 -04003090 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04003091 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003092 if (multi_ret) {
3093 *multi_ret = multi;
3094 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003095 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003096 }
Chris Masoncea9e442008-04-09 16:28:12 -04003097out:
Chris Mason0b86a832008-03-24 15:01:56 -04003098 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003099 return 0;
3100}
3101
Chris Masonf2d8d742008-04-21 10:03:05 -04003102int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3103 u64 logical, u64 *length,
3104 struct btrfs_multi_bio **multi_ret, int mirror_num)
3105{
3106 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3107 mirror_num, NULL);
3108}
3109
Yan Zhenga512bbf2008-12-08 16:46:26 -05003110int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3111 u64 chunk_start, u64 physical, u64 devid,
3112 u64 **logical, int *naddrs, int *stripe_len)
3113{
3114 struct extent_map_tree *em_tree = &map_tree->map_tree;
3115 struct extent_map *em;
3116 struct map_lookup *map;
3117 u64 *buf;
3118 u64 bytenr;
3119 u64 length;
3120 u64 stripe_nr;
3121 int i, j, nr = 0;
3122
Chris Mason890871b2009-09-02 16:24:52 -04003123 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003124 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003125 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003126
3127 BUG_ON(!em || em->start != chunk_start);
3128 map = (struct map_lookup *)em->bdev;
3129
3130 length = em->len;
3131 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3132 do_div(length, map->num_stripes / map->sub_stripes);
3133 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3134 do_div(length, map->num_stripes);
3135
3136 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3137 BUG_ON(!buf);
3138
3139 for (i = 0; i < map->num_stripes; i++) {
3140 if (devid && map->stripes[i].dev->devid != devid)
3141 continue;
3142 if (map->stripes[i].physical > physical ||
3143 map->stripes[i].physical + length <= physical)
3144 continue;
3145
3146 stripe_nr = physical - map->stripes[i].physical;
3147 do_div(stripe_nr, map->stripe_len);
3148
3149 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3150 stripe_nr = stripe_nr * map->num_stripes + i;
3151 do_div(stripe_nr, map->sub_stripes);
3152 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3153 stripe_nr = stripe_nr * map->num_stripes + i;
3154 }
3155 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003156 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003157 for (j = 0; j < nr; j++) {
3158 if (buf[j] == bytenr)
3159 break;
3160 }
Chris Mason934d3752008-12-08 16:43:10 -05003161 if (j == nr) {
3162 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003163 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003164 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003165 }
3166
Yan Zhenga512bbf2008-12-08 16:46:26 -05003167 *logical = buf;
3168 *naddrs = nr;
3169 *stripe_len = map->stripe_len;
3170
3171 free_extent_map(em);
3172 return 0;
3173}
3174
Chris Masonf2d8d742008-04-21 10:03:05 -04003175int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
3176 u64 logical, struct page *page)
3177{
3178 u64 length = PAGE_CACHE_SIZE;
3179 return __btrfs_map_block(map_tree, READ, logical, &length,
3180 NULL, 0, page);
3181}
3182
Chris Mason8790d502008-04-03 16:29:03 -04003183static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003184{
Chris Masoncea9e442008-04-09 16:28:12 -04003185 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003186 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003187
Chris Mason8790d502008-04-03 16:29:03 -04003188 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003189 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003190
Chris Mason7d2b4da2008-08-05 10:13:57 -04003191 if (bio == multi->orig_bio)
3192 is_orig_bio = 1;
3193
Chris Masoncea9e442008-04-09 16:28:12 -04003194 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003195 if (!is_orig_bio) {
3196 bio_put(bio);
3197 bio = multi->orig_bio;
3198 }
Chris Mason8790d502008-04-03 16:29:03 -04003199 bio->bi_private = multi->private;
3200 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003201 /* only send an error to the higher layers if it is
3202 * beyond the tolerance of the multi-bio
3203 */
Chris Mason1259ab72008-05-12 13:39:03 -04003204 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003205 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003206 } else if (err) {
3207 /*
3208 * this bio is actually up to date, we didn't
3209 * go over the max number of errors
3210 */
3211 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003212 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003213 }
Chris Mason8790d502008-04-03 16:29:03 -04003214 kfree(multi);
3215
3216 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003217 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003218 bio_put(bio);
3219 }
Chris Mason8790d502008-04-03 16:29:03 -04003220}
3221
Chris Mason8b712842008-06-11 16:50:36 -04003222struct async_sched {
3223 struct bio *bio;
3224 int rw;
3225 struct btrfs_fs_info *info;
3226 struct btrfs_work work;
3227};
3228
3229/*
3230 * see run_scheduled_bios for a description of why bios are collected for
3231 * async submit.
3232 *
3233 * This will add one bio to the pending list for a device and make sure
3234 * the work struct is scheduled.
3235 */
Chris Masond3977122009-01-05 21:25:51 -05003236static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003237 struct btrfs_device *device,
3238 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003239{
3240 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003241 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003242
3243 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003244 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003245 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003246 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003247 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003248 return 0;
3249 }
3250
3251 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003252 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003253 * higher layers. Otherwise, the async bio makes it appear we have
3254 * made progress against dirty pages when we've really just put it
3255 * on a queue for later
3256 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003257 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003258 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003259 bio->bi_next = NULL;
3260 bio->bi_rw |= rw;
3261
3262 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003263 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003264 pending_bios = &device->pending_sync_bios;
3265 else
3266 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003267
Chris Masonffbd5172009-04-20 15:50:09 -04003268 if (pending_bios->tail)
3269 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003270
Chris Masonffbd5172009-04-20 15:50:09 -04003271 pending_bios->tail = bio;
3272 if (!pending_bios->head)
3273 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003274 if (device->running_pending)
3275 should_queue = 0;
3276
3277 spin_unlock(&device->io_lock);
3278
3279 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003280 btrfs_queue_worker(&root->fs_info->submit_workers,
3281 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003282 return 0;
3283}
3284
Chris Masonf1885912008-04-09 16:28:12 -04003285int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003286 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003287{
3288 struct btrfs_mapping_tree *map_tree;
3289 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003290 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003291 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003292 u64 length = 0;
3293 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003294 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003295 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003296 int dev_nr = 0;
3297 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003298
Chris Masonf2d8d742008-04-21 10:03:05 -04003299 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003300 map_tree = &root->fs_info->mapping_tree;
3301 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003302
Chris Masonf1885912008-04-09 16:28:12 -04003303 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3304 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003305 BUG_ON(ret);
3306
3307 total_devs = multi->num_stripes;
3308 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003309 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3310 "len %llu\n", (unsigned long long)logical,
3311 (unsigned long long)length,
3312 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003313 BUG();
3314 }
3315 multi->end_io = first_bio->bi_end_io;
3316 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003317 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003318 atomic_set(&multi->stripes_pending, multi->num_stripes);
3319
Chris Masond3977122009-01-05 21:25:51 -05003320 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003321 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003322 if (dev_nr < total_devs - 1) {
3323 bio = bio_clone(first_bio, GFP_NOFS);
3324 BUG_ON(!bio);
3325 } else {
3326 bio = first_bio;
3327 }
3328 bio->bi_private = multi;
3329 bio->bi_end_io = end_bio_multi_stripe;
3330 }
Chris Masoncea9e442008-04-09 16:28:12 -04003331 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3332 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003333 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003334 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003335 if (async_submit)
3336 schedule_bio(root, dev, rw, bio);
3337 else
3338 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003339 } else {
3340 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3341 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003342 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003343 }
Chris Mason8790d502008-04-03 16:29:03 -04003344 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003345 }
Chris Masoncea9e442008-04-09 16:28:12 -04003346 if (total_devs == 1)
3347 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003348 return 0;
3349}
3350
Chris Masona4437552008-04-18 10:29:38 -04003351struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003352 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003353{
Yan Zheng2b820322008-11-17 21:11:30 -05003354 struct btrfs_device *device;
3355 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003356
Yan Zheng2b820322008-11-17 21:11:30 -05003357 cur_devices = root->fs_info->fs_devices;
3358 while (cur_devices) {
3359 if (!fsid ||
3360 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3361 device = __find_device(&cur_devices->devices,
3362 devid, uuid);
3363 if (device)
3364 return device;
3365 }
3366 cur_devices = cur_devices->seed;
3367 }
3368 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003369}
3370
Chris Masondfe25022008-05-13 13:46:40 -04003371static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3372 u64 devid, u8 *dev_uuid)
3373{
3374 struct btrfs_device *device;
3375 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3376
3377 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003378 if (!device)
3379 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003380 list_add(&device->dev_list,
3381 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003382 device->dev_root = root->fs_info->dev_root;
3383 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003384 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003385 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003386 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003387 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003388 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003389 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003390 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003391 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3392 return device;
3393}
3394
Chris Mason0b86a832008-03-24 15:01:56 -04003395static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3396 struct extent_buffer *leaf,
3397 struct btrfs_chunk *chunk)
3398{
3399 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3400 struct map_lookup *map;
3401 struct extent_map *em;
3402 u64 logical;
3403 u64 length;
3404 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003405 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003406 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003407 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003408 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003409
Chris Masone17cade2008-04-15 15:41:47 -04003410 logical = key->offset;
3411 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003412
Chris Mason890871b2009-09-02 16:24:52 -04003413 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003414 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003415 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003416
3417 /* already mapped? */
3418 if (em && em->start <= logical && em->start + em->len > logical) {
3419 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003420 return 0;
3421 } else if (em) {
3422 free_extent_map(em);
3423 }
Chris Mason0b86a832008-03-24 15:01:56 -04003424
Chris Mason0b86a832008-03-24 15:01:56 -04003425 em = alloc_extent_map(GFP_NOFS);
3426 if (!em)
3427 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003428 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3429 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003430 if (!map) {
3431 free_extent_map(em);
3432 return -ENOMEM;
3433 }
3434
3435 em->bdev = (struct block_device *)map;
3436 em->start = logical;
3437 em->len = length;
3438 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003439 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003440
Chris Mason593060d2008-03-25 16:50:33 -04003441 map->num_stripes = num_stripes;
3442 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3443 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3444 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3445 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3446 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003447 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003448 for (i = 0; i < num_stripes; i++) {
3449 map->stripes[i].physical =
3450 btrfs_stripe_offset_nr(leaf, chunk, i);
3451 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003452 read_extent_buffer(leaf, uuid, (unsigned long)
3453 btrfs_stripe_dev_uuid_nr(chunk, i),
3454 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003455 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3456 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003457 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003458 kfree(map);
3459 free_extent_map(em);
3460 return -EIO;
3461 }
Chris Masondfe25022008-05-13 13:46:40 -04003462 if (!map->stripes[i].dev) {
3463 map->stripes[i].dev =
3464 add_missing_dev(root, devid, uuid);
3465 if (!map->stripes[i].dev) {
3466 kfree(map);
3467 free_extent_map(em);
3468 return -EIO;
3469 }
3470 }
3471 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003472 }
3473
Chris Mason890871b2009-09-02 16:24:52 -04003474 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003475 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003476 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003477 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003478 free_extent_map(em);
3479
3480 return 0;
3481}
3482
3483static int fill_device_from_item(struct extent_buffer *leaf,
3484 struct btrfs_dev_item *dev_item,
3485 struct btrfs_device *device)
3486{
3487 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003488
3489 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003490 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3491 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003492 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3493 device->type = btrfs_device_type(leaf, dev_item);
3494 device->io_align = btrfs_device_io_align(leaf, dev_item);
3495 device->io_width = btrfs_device_io_width(leaf, dev_item);
3496 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003497
3498 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003499 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003500
Chris Mason0b86a832008-03-24 15:01:56 -04003501 return 0;
3502}
3503
Yan Zheng2b820322008-11-17 21:11:30 -05003504static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3505{
3506 struct btrfs_fs_devices *fs_devices;
3507 int ret;
3508
3509 mutex_lock(&uuid_mutex);
3510
3511 fs_devices = root->fs_info->fs_devices->seed;
3512 while (fs_devices) {
3513 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3514 ret = 0;
3515 goto out;
3516 }
3517 fs_devices = fs_devices->seed;
3518 }
3519
3520 fs_devices = find_fsid(fsid);
3521 if (!fs_devices) {
3522 ret = -ENOENT;
3523 goto out;
3524 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003525
3526 fs_devices = clone_fs_devices(fs_devices);
3527 if (IS_ERR(fs_devices)) {
3528 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003529 goto out;
3530 }
3531
Christoph Hellwig97288f22008-12-02 06:36:09 -05003532 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003533 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003534 if (ret)
3535 goto out;
3536
3537 if (!fs_devices->seeding) {
3538 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003539 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003540 ret = -EINVAL;
3541 goto out;
3542 }
3543
3544 fs_devices->seed = root->fs_info->fs_devices->seed;
3545 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003546out:
3547 mutex_unlock(&uuid_mutex);
3548 return ret;
3549}
3550
Chris Mason0d81ba52008-03-24 15:02:07 -04003551static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003552 struct extent_buffer *leaf,
3553 struct btrfs_dev_item *dev_item)
3554{
3555 struct btrfs_device *device;
3556 u64 devid;
3557 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003558 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003559 u8 dev_uuid[BTRFS_UUID_SIZE];
3560
Chris Mason0b86a832008-03-24 15:01:56 -04003561 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003562 read_extent_buffer(leaf, dev_uuid,
3563 (unsigned long)btrfs_device_uuid(dev_item),
3564 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003565 read_extent_buffer(leaf, fs_uuid,
3566 (unsigned long)btrfs_device_fsid(dev_item),
3567 BTRFS_UUID_SIZE);
3568
3569 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3570 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003571 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003572 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003573 }
3574
3575 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3576 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003577 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003578 return -EIO;
3579
3580 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003581 printk(KERN_WARNING "warning devid %llu missing\n",
3582 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003583 device = add_missing_dev(root, devid, dev_uuid);
3584 if (!device)
3585 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003586 } else if (!device->missing) {
3587 /*
3588 * this happens when a device that was properly setup
3589 * in the device info lists suddenly goes bad.
3590 * device->bdev is NULL, and so we have to set
3591 * device->missing to one here
3592 */
3593 root->fs_info->fs_devices->missing_devices++;
3594 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003595 }
3596 }
3597
3598 if (device->fs_devices != root->fs_info->fs_devices) {
3599 BUG_ON(device->writeable);
3600 if (device->generation !=
3601 btrfs_device_generation(leaf, dev_item))
3602 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003603 }
Chris Mason0b86a832008-03-24 15:01:56 -04003604
3605 fill_device_from_item(leaf, dev_item, device);
3606 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003607 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003608 if (device->writeable)
3609 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003610 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003611 return ret;
3612}
3613
Chris Mason0d81ba52008-03-24 15:02:07 -04003614int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3615{
3616 struct btrfs_dev_item *dev_item;
3617
3618 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3619 dev_item);
3620 return read_one_dev(root, buf, dev_item);
3621}
3622
Yan Zhenge4404d62008-12-12 10:03:26 -05003623int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003624{
3625 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003626 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003627 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003628 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003629 u8 *ptr;
3630 unsigned long sb_ptr;
3631 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003632 u32 num_stripes;
3633 u32 array_size;
3634 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003635 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003636 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003637
Yan Zhenge4404d62008-12-12 10:03:26 -05003638 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003639 BTRFS_SUPER_INFO_SIZE);
3640 if (!sb)
3641 return -ENOMEM;
3642 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003643 btrfs_set_buffer_lockdep_class(sb, 0);
3644
Chris Masona061fc82008-05-07 11:43:44 -04003645 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003646 array_size = btrfs_super_sys_array_size(super_copy);
3647
Chris Mason0b86a832008-03-24 15:01:56 -04003648 ptr = super_copy->sys_chunk_array;
3649 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3650 cur = 0;
3651
3652 while (cur < array_size) {
3653 disk_key = (struct btrfs_disk_key *)ptr;
3654 btrfs_disk_key_to_cpu(&key, disk_key);
3655
Chris Masona061fc82008-05-07 11:43:44 -04003656 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003657 sb_ptr += len;
3658 cur += len;
3659
Chris Mason0d81ba52008-03-24 15:02:07 -04003660 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003661 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003662 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003663 if (ret)
3664 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003665 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3666 len = btrfs_chunk_item_size(num_stripes);
3667 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003668 ret = -EIO;
3669 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003670 }
3671 ptr += len;
3672 sb_ptr += len;
3673 cur += len;
3674 }
Chris Masona061fc82008-05-07 11:43:44 -04003675 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003676 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003677}
3678
3679int btrfs_read_chunk_tree(struct btrfs_root *root)
3680{
3681 struct btrfs_path *path;
3682 struct extent_buffer *leaf;
3683 struct btrfs_key key;
3684 struct btrfs_key found_key;
3685 int ret;
3686 int slot;
3687
3688 root = root->fs_info->chunk_root;
3689
3690 path = btrfs_alloc_path();
3691 if (!path)
3692 return -ENOMEM;
3693
3694 /* first we search for all of the device items, and then we
3695 * read in all of the chunk items. This way we can create chunk
3696 * mappings that reference all of the devices that are afound
3697 */
3698 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3699 key.offset = 0;
3700 key.type = 0;
3701again:
3702 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003703 if (ret < 0)
3704 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003705 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003706 leaf = path->nodes[0];
3707 slot = path->slots[0];
3708 if (slot >= btrfs_header_nritems(leaf)) {
3709 ret = btrfs_next_leaf(root, path);
3710 if (ret == 0)
3711 continue;
3712 if (ret < 0)
3713 goto error;
3714 break;
3715 }
3716 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3717 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3718 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3719 break;
3720 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3721 struct btrfs_dev_item *dev_item;
3722 dev_item = btrfs_item_ptr(leaf, slot,
3723 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003724 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003725 if (ret)
3726 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003727 }
3728 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3729 struct btrfs_chunk *chunk;
3730 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3731 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003732 if (ret)
3733 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003734 }
3735 path->slots[0]++;
3736 }
3737 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3738 key.objectid = 0;
3739 btrfs_release_path(root, path);
3740 goto again;
3741 }
Chris Mason0b86a832008-03-24 15:01:56 -04003742 ret = 0;
3743error:
Yan Zheng2b820322008-11-17 21:11:30 -05003744 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003745 return ret;
3746}