blob: 530a2fcea1eff677514c2e7964f322825c516205 [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
Yan Zheng2b820322008-11-17 21:11:30 -050036static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
40
Chris Mason8a4b83c2008-03-24 15:02:07 -040041static DEFINE_MUTEX(uuid_mutex);
42static LIST_HEAD(fs_uuids);
43
Chris Mason7d9eb122008-07-08 14:19:17 -040044static void lock_chunks(struct btrfs_root *root)
45{
Chris Mason7d9eb122008-07-08 14:19:17 -040046 mutex_lock(&root->fs_info->chunk_mutex);
47}
48
49static void unlock_chunks(struct btrfs_root *root)
50{
Chris Mason7d9eb122008-07-08 14:19:17 -040051 mutex_unlock(&root->fs_info->chunk_mutex);
52}
53
Yan Zhenge4404d62008-12-12 10:03:26 -050054static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
55{
56 struct btrfs_device *device;
57 WARN_ON(fs_devices->opened);
58 while (!list_empty(&fs_devices->devices)) {
59 device = list_entry(fs_devices->devices.next,
60 struct btrfs_device, dev_list);
61 list_del(&device->dev_list);
62 kfree(device->name);
63 kfree(device);
64 }
65 kfree(fs_devices);
66}
67
Chris Mason8a4b83c2008-03-24 15:02:07 -040068int btrfs_cleanup_fs_uuids(void)
69{
70 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040071
Yan Zheng2b820322008-11-17 21:11:30 -050072 while (!list_empty(&fs_uuids)) {
73 fs_devices = list_entry(fs_uuids.next,
74 struct btrfs_fs_devices, list);
75 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050076 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040077 }
78 return 0;
79}
80
Chris Masona1b32a52008-09-05 16:09:51 -040081static noinline struct btrfs_device *__find_device(struct list_head *head,
82 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040083{
84 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040085
Qinghuang Fengc6e30872009-01-21 10:59:08 -050086 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040087 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040088 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040089 return dev;
Chris Masona4437552008-04-18 10:29:38 -040090 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 }
92 return NULL;
93}
94
Chris Masona1b32a52008-09-05 16:09:51 -040095static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040096{
Chris Mason8a4b83c2008-03-24 15:02:07 -040097 struct btrfs_fs_devices *fs_devices;
98
Qinghuang Fengc6e30872009-01-21 10:59:08 -050099 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400100 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
101 return fs_devices;
102 }
103 return NULL;
104}
105
Chris Masonffbd5172009-04-20 15:50:09 -0400106static void requeue_list(struct btrfs_pending_bios *pending_bios,
107 struct bio *head, struct bio *tail)
108{
109
110 struct bio *old_head;
111
112 old_head = pending_bios->head;
113 pending_bios->head = head;
114 if (pending_bios->tail)
115 tail->bi_next = old_head;
116 else
117 pending_bios->tail = tail;
118}
119
Chris Mason8b712842008-06-11 16:50:36 -0400120/*
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
124 *
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
130 */
Chris Masond3977122009-01-05 21:25:51 -0500131static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400132{
133 struct bio *pending;
134 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400135 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400136 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400137 struct bio *tail;
138 struct bio *cur;
139 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400140 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400141 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400142 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400143 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400144 int force_reg = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400145 struct blk_plug plug;
146
147 /*
148 * this function runs all the bios we've collected for
149 * a particular device. We don't want to wander off to
150 * another device without first sending all of these down.
151 * So, setup a plug here and finish it off before we return
152 */
153 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400154
Chris Masonbedf7622009-04-03 10:32:58 -0400155 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400156 fs_info = device->dev_root->fs_info;
157 limit = btrfs_async_submit_limit(fs_info);
158 limit = limit * 2 / 3;
159
Chris Mason8b712842008-06-11 16:50:36 -0400160loop:
161 spin_lock(&device->io_lock);
162
Chris Masona6837052009-02-04 09:19:41 -0500163loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400164 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400165
Chris Mason8b712842008-06-11 16:50:36 -0400166 /* take all the bios off the list at once and process them
167 * later on (without the lock held). But, remember the
168 * tail and other pointers so the bios can be properly reinserted
169 * into the list if we hit congestion
170 */
Chris Masond84275c2009-06-09 15:39:08 -0400171 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400172 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400173 force_reg = 1;
174 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400175 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400176 force_reg = 0;
177 }
Chris Masonffbd5172009-04-20 15:50:09 -0400178
179 pending = pending_bios->head;
180 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400181 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400182
183 /*
184 * if pending was null this time around, no bios need processing
185 * at all and we can stop. Otherwise it'll loop back up again
186 * and do an additional check so no bios are missed.
187 *
188 * device->running_pending is used to synchronize with the
189 * schedule_bio code.
190 */
Chris Masonffbd5172009-04-20 15:50:09 -0400191 if (device->pending_sync_bios.head == NULL &&
192 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400193 again = 0;
194 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400195 } else {
196 again = 1;
197 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400198 }
Chris Masonffbd5172009-04-20 15:50:09 -0400199
200 pending_bios->head = NULL;
201 pending_bios->tail = NULL;
202
Chris Mason8b712842008-06-11 16:50:36 -0400203 spin_unlock(&device->io_lock);
204
Chris Masond3977122009-01-05 21:25:51 -0500205 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400206
207 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400208 /* we want to work on both lists, but do more bios on the
209 * sync list than the regular list
210 */
211 if ((num_run > 32 &&
212 pending_bios != &device->pending_sync_bios &&
213 device->pending_sync_bios.head) ||
214 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
215 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400216 spin_lock(&device->io_lock);
217 requeue_list(pending_bios, pending, tail);
218 goto loop_lock;
219 }
220
Chris Mason8b712842008-06-11 16:50:36 -0400221 cur = pending;
222 pending = pending->bi_next;
223 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400224 atomic_dec(&fs_info->nr_async_bios);
225
226 if (atomic_read(&fs_info->nr_async_bios) < limit &&
227 waitqueue_active(&fs_info->async_submit_wait))
228 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400229
230 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400231
Chris Mason5ff7ba32010-03-15 10:21:30 -0400232 submit_bio(cur->bi_rw, cur);
233 num_run++;
234 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100235 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400236 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400237
238 /*
239 * we made progress, there is more work to do and the bdi
240 * is now congested. Back off and let other work structs
241 * run instead
242 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400243 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500244 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400245 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400246
Chris Masonb765ead2009-04-03 10:27:10 -0400247 ioc = current->io_context;
248
249 /*
250 * the main goal here is that we don't want to
251 * block if we're going to be able to submit
252 * more requests without blocking.
253 *
254 * This code does two great things, it pokes into
255 * the elevator code from a filesystem _and_
256 * it makes assumptions about how batching works.
257 */
258 if (ioc && ioc->nr_batch_requests > 0 &&
259 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
260 (last_waited == 0 ||
261 ioc->last_waited == last_waited)) {
262 /*
263 * we want to go through our batch of
264 * requests and stop. So, we copy out
265 * the ioc->last_waited time and test
266 * against it before looping
267 */
268 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100269 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400270 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400271 continue;
272 }
Chris Mason8b712842008-06-11 16:50:36 -0400273 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400274 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500275 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400276
277 spin_unlock(&device->io_lock);
278 btrfs_requeue_work(&device->work);
279 goto done;
280 }
281 }
Chris Masonffbd5172009-04-20 15:50:09 -0400282
Chris Mason51684082010-03-10 15:33:32 -0500283 cond_resched();
284 if (again)
285 goto loop;
286
287 spin_lock(&device->io_lock);
288 if (device->pending_bios.head || device->pending_sync_bios.head)
289 goto loop_lock;
290 spin_unlock(&device->io_lock);
291
Chris Mason8b712842008-06-11 16:50:36 -0400292done:
Chris Mason211588a2011-04-19 20:12:40 -0400293 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400294 return 0;
295}
296
Christoph Hellwigb2950862008-12-02 09:54:17 -0500297static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400298{
299 struct btrfs_device *device;
300
301 device = container_of(work, struct btrfs_device, work);
302 run_scheduled_bios(device);
303}
304
Chris Masona1b32a52008-09-05 16:09:51 -0400305static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400306 struct btrfs_super_block *disk_super,
307 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
308{
309 struct btrfs_device *device;
310 struct btrfs_fs_devices *fs_devices;
311 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000312 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400313
314 fs_devices = find_fsid(disk_super->fsid);
315 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400316 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400317 if (!fs_devices)
318 return -ENOMEM;
319 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400320 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400321 list_add(&fs_devices->list, &fs_uuids);
322 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
323 fs_devices->latest_devid = devid;
324 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400325 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400326 device = NULL;
327 } else {
Chris Masona4437552008-04-18 10:29:38 -0400328 device = __find_device(&fs_devices->devices, devid,
329 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 }
331 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500332 if (fs_devices->opened)
333 return -EBUSY;
334
Chris Mason8a4b83c2008-03-24 15:02:07 -0400335 device = kzalloc(sizeof(*device), GFP_NOFS);
336 if (!device) {
337 /* we can safely leave the fs_devices entry around */
338 return -ENOMEM;
339 }
340 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400341 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400342 memcpy(device->uuid, disk_super->dev_item.uuid,
343 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400344 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400345 device->name = kstrdup(path, GFP_NOFS);
346 if (!device->name) {
347 kfree(device);
348 return -ENOMEM;
349 }
Yan Zheng2b820322008-11-17 21:11:30 -0500350 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400351
352 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000353 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400354 mutex_unlock(&fs_devices->device_list_mutex);
355
Yan Zheng2b820322008-11-17 21:11:30 -0500356 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400357 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500358 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000359 name = kstrdup(path, GFP_NOFS);
360 if (!name)
361 return -ENOMEM;
362 kfree(device->name);
363 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500364 if (device->missing) {
365 fs_devices->missing_devices--;
366 device->missing = 0;
367 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 }
369
370 if (found_transid > fs_devices->latest_trans) {
371 fs_devices->latest_devid = devid;
372 fs_devices->latest_trans = found_transid;
373 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400374 *fs_devices_ret = fs_devices;
375 return 0;
376}
377
Yan Zhenge4404d62008-12-12 10:03:26 -0500378static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
379{
380 struct btrfs_fs_devices *fs_devices;
381 struct btrfs_device *device;
382 struct btrfs_device *orig_dev;
383
384 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
385 if (!fs_devices)
386 return ERR_PTR(-ENOMEM);
387
388 INIT_LIST_HEAD(&fs_devices->devices);
389 INIT_LIST_HEAD(&fs_devices->alloc_list);
390 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400391 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500392 fs_devices->latest_devid = orig->latest_devid;
393 fs_devices->latest_trans = orig->latest_trans;
394 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
395
Xiao Guangrong46224702011-04-20 10:08:47 +0000396 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500397 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
398 device = kzalloc(sizeof(*device), GFP_NOFS);
399 if (!device)
400 goto error;
401
402 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400403 if (!device->name) {
404 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500405 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400406 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500407
408 device->devid = orig_dev->devid;
409 device->work.func = pending_bios_fn;
410 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500411 spin_lock_init(&device->io_lock);
412 INIT_LIST_HEAD(&device->dev_list);
413 INIT_LIST_HEAD(&device->dev_alloc_list);
414
415 list_add(&device->dev_list, &fs_devices->devices);
416 device->fs_devices = fs_devices;
417 fs_devices->num_devices++;
418 }
419 return fs_devices;
420error:
421 free_fs_devices(fs_devices);
422 return ERR_PTR(-ENOMEM);
423}
424
Chris Masondfe25022008-05-13 13:46:40 -0400425int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
426{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500427 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400428
429 mutex_lock(&uuid_mutex);
430again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000431 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500432 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500433 if (device->in_fs_metadata)
434 continue;
435
436 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100437 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500438 device->bdev = NULL;
439 fs_devices->open_devices--;
440 }
441 if (device->writeable) {
442 list_del_init(&device->dev_alloc_list);
443 device->writeable = 0;
444 fs_devices->rw_devices--;
445 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500446 list_del_init(&device->dev_list);
447 fs_devices->num_devices--;
448 kfree(device->name);
449 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400450 }
Yan Zheng2b820322008-11-17 21:11:30 -0500451
452 if (fs_devices->seed) {
453 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500454 goto again;
455 }
456
Chris Masondfe25022008-05-13 13:46:40 -0400457 mutex_unlock(&uuid_mutex);
458 return 0;
459}
Chris Masona0af4692008-05-13 16:03:06 -0400460
Xiao Guangrong1f781602011-04-20 10:09:16 +0000461static void __free_device(struct work_struct *work)
462{
463 struct btrfs_device *device;
464
465 device = container_of(work, struct btrfs_device, rcu_work);
466
467 if (device->bdev)
468 blkdev_put(device->bdev, device->mode);
469
470 kfree(device->name);
471 kfree(device);
472}
473
474static void free_device(struct rcu_head *head)
475{
476 struct btrfs_device *device;
477
478 device = container_of(head, struct btrfs_device, rcu);
479
480 INIT_WORK(&device->rcu_work, __free_device);
481 schedule_work(&device->rcu_work);
482}
483
Yan Zheng2b820322008-11-17 21:11:30 -0500484static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400486 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500487
Yan Zheng2b820322008-11-17 21:11:30 -0500488 if (--fs_devices->opened > 0)
489 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400490
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000491 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500492 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000493 struct btrfs_device *new_device;
494
495 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400496 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000497
Yan Zheng2b820322008-11-17 21:11:30 -0500498 if (device->writeable) {
499 list_del_init(&device->dev_alloc_list);
500 fs_devices->rw_devices--;
501 }
502
Xiao Guangrong1f781602011-04-20 10:09:16 +0000503 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
504 BUG_ON(!new_device);
505 memcpy(new_device, device, sizeof(*new_device));
506 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000507 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000508 new_device->bdev = NULL;
509 new_device->writeable = 0;
510 new_device->in_fs_metadata = 0;
511 list_replace_rcu(&device->dev_list, &new_device->dev_list);
512
513 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400514 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000515 mutex_unlock(&fs_devices->device_list_mutex);
516
Yan Zhenge4404d62008-12-12 10:03:26 -0500517 WARN_ON(fs_devices->open_devices);
518 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500519 fs_devices->opened = 0;
520 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500521
Chris Mason8a4b83c2008-03-24 15:02:07 -0400522 return 0;
523}
524
Yan Zheng2b820322008-11-17 21:11:30 -0500525int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
526{
Yan Zhenge4404d62008-12-12 10:03:26 -0500527 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500528 int ret;
529
530 mutex_lock(&uuid_mutex);
531 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500532 if (!fs_devices->opened) {
533 seed_devices = fs_devices->seed;
534 fs_devices->seed = NULL;
535 }
Yan Zheng2b820322008-11-17 21:11:30 -0500536 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500537
538 while (seed_devices) {
539 fs_devices = seed_devices;
540 seed_devices = fs_devices->seed;
541 __btrfs_close_devices(fs_devices);
542 free_fs_devices(fs_devices);
543 }
Yan Zheng2b820322008-11-17 21:11:30 -0500544 return ret;
545}
546
Yan Zhenge4404d62008-12-12 10:03:26 -0500547static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
548 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549{
550 struct block_device *bdev;
551 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400552 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400553 struct block_device *latest_bdev = NULL;
554 struct buffer_head *bh;
555 struct btrfs_super_block *disk_super;
556 u64 latest_devid = 0;
557 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400558 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500559 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400560 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400561
Tejun Heod4d77622010-11-13 11:55:18 +0100562 flags |= FMODE_EXCL;
563
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500564 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400565 if (device->bdev)
566 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400567 if (!device->name)
568 continue;
569
Tejun Heod4d77622010-11-13 11:55:18 +0100570 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500572 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400573 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 }
Chris Masona061fc82008-05-07 11:43:44 -0400575 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400576
Yan Zhenga512bbf2008-12-08 16:46:26 -0500577 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000578 if (!bh) {
579 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400580 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000581 }
Chris Masona0af4692008-05-13 16:03:06 -0400582
583 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000584 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400585 if (devid != device->devid)
586 goto error_brelse;
587
Yan Zheng2b820322008-11-17 21:11:30 -0500588 if (memcmp(device->uuid, disk_super->dev_item.uuid,
589 BTRFS_UUID_SIZE))
590 goto error_brelse;
591
592 device->generation = btrfs_super_generation(disk_super);
593 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400594 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500595 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400596 latest_bdev = bdev;
597 }
598
Yan Zheng2b820322008-11-17 21:11:30 -0500599 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
600 device->writeable = 0;
601 } else {
602 device->writeable = !bdev_read_only(bdev);
603 seeding = 0;
604 }
605
Chris Mason8a4b83c2008-03-24 15:02:07 -0400606 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400607 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500608 device->mode = flags;
609
Chris Masonc2898112009-06-10 09:51:32 -0400610 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
611 fs_devices->rotating = 1;
612
Chris Masona0af4692008-05-13 16:03:06 -0400613 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500614 if (device->writeable) {
615 fs_devices->rw_devices++;
616 list_add(&device->dev_alloc_list,
617 &fs_devices->alloc_list);
618 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000619 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400620 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400621
Chris Masona0af4692008-05-13 16:03:06 -0400622error_brelse:
623 brelse(bh);
624error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100625 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400626error:
627 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400628 }
Chris Masona0af4692008-05-13 16:03:06 -0400629 if (fs_devices->open_devices == 0) {
630 ret = -EIO;
631 goto out;
632 }
Yan Zheng2b820322008-11-17 21:11:30 -0500633 fs_devices->seeding = seeding;
634 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400635 fs_devices->latest_bdev = latest_bdev;
636 fs_devices->latest_devid = latest_devid;
637 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500638 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400639out:
Yan Zheng2b820322008-11-17 21:11:30 -0500640 return ret;
641}
642
643int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500644 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500645{
646 int ret;
647
648 mutex_lock(&uuid_mutex);
649 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500650 fs_devices->opened++;
651 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500652 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500653 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500654 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400655 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400656 return ret;
657}
658
Christoph Hellwig97288f22008-12-02 06:36:09 -0500659int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400660 struct btrfs_fs_devices **fs_devices_ret)
661{
662 struct btrfs_super_block *disk_super;
663 struct block_device *bdev;
664 struct buffer_head *bh;
665 int ret;
666 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400667 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400668
669 mutex_lock(&uuid_mutex);
670
Tejun Heod4d77622010-11-13 11:55:18 +0100671 flags |= FMODE_EXCL;
672 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400673
674 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400675 ret = PTR_ERR(bdev);
676 goto error;
677 }
678
679 ret = set_blocksize(bdev, 4096);
680 if (ret)
681 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500682 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400683 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000684 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685 goto error_close;
686 }
687 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000688 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400689 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400690 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500691 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200692 else
693 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500694 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500695 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
697
Chris Mason8a4b83c2008-03-24 15:02:07 -0400698 brelse(bh);
699error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100700 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701error:
702 mutex_unlock(&uuid_mutex);
703 return ret;
704}
Chris Mason0b86a832008-03-24 15:01:56 -0400705
Miao Xie6d07bce2011-01-05 10:07:31 +0000706/* helper to account the used device space in the range */
707int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
708 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400709{
710 struct btrfs_key key;
711 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000712 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500713 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000714 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400715 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000716 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400717 struct extent_buffer *l;
718
Miao Xie6d07bce2011-01-05 10:07:31 +0000719 *length = 0;
720
721 if (start >= device->total_bytes)
722 return 0;
723
Yan Zheng2b820322008-11-17 21:11:30 -0500724 path = btrfs_alloc_path();
725 if (!path)
726 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400727 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400728
Chris Mason0b86a832008-03-24 15:01:56 -0400729 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000730 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400731 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000732
733 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400734 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000735 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400736 if (ret > 0) {
737 ret = btrfs_previous_item(root, path, key.objectid, key.type);
738 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000739 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400740 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000741
Chris Mason0b86a832008-03-24 15:01:56 -0400742 while (1) {
743 l = path->nodes[0];
744 slot = path->slots[0];
745 if (slot >= btrfs_header_nritems(l)) {
746 ret = btrfs_next_leaf(root, path);
747 if (ret == 0)
748 continue;
749 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000750 goto out;
751
752 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400753 }
754 btrfs_item_key_to_cpu(l, &key, slot);
755
756 if (key.objectid < device->devid)
757 goto next;
758
759 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000760 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400761
Chris Masond3977122009-01-05 21:25:51 -0500762 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400763 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400764
Chris Mason0b86a832008-03-24 15:01:56 -0400765 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000766 extent_end = key.offset + btrfs_dev_extent_length(l,
767 dev_extent);
768 if (key.offset <= start && extent_end > end) {
769 *length = end - start + 1;
770 break;
771 } else if (key.offset <= start && extent_end > start)
772 *length += extent_end - start;
773 else if (key.offset > start && extent_end <= end)
774 *length += extent_end - key.offset;
775 else if (key.offset > start && key.offset <= end) {
776 *length += end - key.offset + 1;
777 break;
778 } else if (key.offset > end)
779 break;
780
781next:
782 path->slots[0]++;
783 }
784 ret = 0;
785out:
786 btrfs_free_path(path);
787 return ret;
788}
789
Chris Mason0b86a832008-03-24 15:01:56 -0400790/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000791 * find_free_dev_extent - find free space in the specified device
792 * @trans: transaction handler
793 * @device: the device which we search the free space in
794 * @num_bytes: the size of the free space that we need
795 * @start: store the start of the free space.
796 * @len: the size of the free space. that we find, or the size of the max
797 * free space if we don't find suitable free space
798 *
Chris Mason0b86a832008-03-24 15:01:56 -0400799 * this uses a pretty simple search, the expectation is that it is
800 * called very infrequently and that a given device has a small number
801 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000802 *
803 * @start is used to store the start of the free space if we find. But if we
804 * don't find suitable free space, it will be used to store the start position
805 * of the max free space.
806 *
807 * @len is used to store the size of the free space that we find.
808 * But if we don't find suitable free space, it is used to store the size of
809 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400810 */
811int find_free_dev_extent(struct btrfs_trans_handle *trans,
812 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000813 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400814{
815 struct btrfs_key key;
816 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000817 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400818 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000819 u64 hole_size;
820 u64 max_hole_start;
821 u64 max_hole_size;
822 u64 extent_end;
823 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400824 u64 search_end = device->total_bytes;
825 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000826 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400827 struct extent_buffer *l;
828
Chris Mason0b86a832008-03-24 15:01:56 -0400829 /* FIXME use last free of some kind */
830
831 /* we don't want to overwrite the superblock on the drive,
832 * so we make sure to start at an offset of at least 1MB
833 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200834 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400835
Miao Xie7bfc8372011-01-05 10:07:26 +0000836 max_hole_start = search_start;
837 max_hole_size = 0;
838
839 if (search_start >= search_end) {
840 ret = -ENOSPC;
841 goto error;
842 }
843
844 path = btrfs_alloc_path();
845 if (!path) {
846 ret = -ENOMEM;
847 goto error;
848 }
849 path->reada = 2;
850
Chris Mason0b86a832008-03-24 15:01:56 -0400851 key.objectid = device->devid;
852 key.offset = search_start;
853 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000854
Chris Mason0b86a832008-03-24 15:01:56 -0400855 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
856 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400858 if (ret > 0) {
859 ret = btrfs_previous_item(root, path, key.objectid, key.type);
860 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000861 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400862 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000863
Chris Mason0b86a832008-03-24 15:01:56 -0400864 while (1) {
865 l = path->nodes[0];
866 slot = path->slots[0];
867 if (slot >= btrfs_header_nritems(l)) {
868 ret = btrfs_next_leaf(root, path);
869 if (ret == 0)
870 continue;
871 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000872 goto out;
873
874 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400875 }
876 btrfs_item_key_to_cpu(l, &key, slot);
877
878 if (key.objectid < device->devid)
879 goto next;
880
881 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000882 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400883
Chris Mason0b86a832008-03-24 15:01:56 -0400884 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
885 goto next;
886
Miao Xie7bfc8372011-01-05 10:07:26 +0000887 if (key.offset > search_start) {
888 hole_size = key.offset - search_start;
889
890 if (hole_size > max_hole_size) {
891 max_hole_start = search_start;
892 max_hole_size = hole_size;
893 }
894
895 /*
896 * If this free space is greater than which we need,
897 * it must be the max free space that we have found
898 * until now, so max_hole_start must point to the start
899 * of this free space and the length of this free space
900 * is stored in max_hole_size. Thus, we return
901 * max_hole_start and max_hole_size and go back to the
902 * caller.
903 */
904 if (hole_size >= num_bytes) {
905 ret = 0;
906 goto out;
907 }
908 }
909
Chris Mason0b86a832008-03-24 15:01:56 -0400910 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 extent_end = key.offset + btrfs_dev_extent_length(l,
912 dev_extent);
913 if (extent_end > search_start)
914 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400915next:
916 path->slots[0]++;
917 cond_resched();
918 }
Chris Mason0b86a832008-03-24 15:01:56 -0400919
Miao Xie7bfc8372011-01-05 10:07:26 +0000920 hole_size = search_end- search_start;
921 if (hole_size > max_hole_size) {
922 max_hole_start = search_start;
923 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400924 }
Chris Mason0b86a832008-03-24 15:01:56 -0400925
Miao Xie7bfc8372011-01-05 10:07:26 +0000926 /* See above. */
927 if (hole_size < num_bytes)
928 ret = -ENOSPC;
929 else
930 ret = 0;
931
932out:
Yan Zheng2b820322008-11-17 21:11:30 -0500933 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000934error:
935 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000936 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400938 return ret;
939}
940
Christoph Hellwigb2950862008-12-02 09:54:17 -0500941static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400942 struct btrfs_device *device,
943 u64 start)
944{
945 int ret;
946 struct btrfs_path *path;
947 struct btrfs_root *root = device->dev_root;
948 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400949 struct btrfs_key found_key;
950 struct extent_buffer *leaf = NULL;
951 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400952
953 path = btrfs_alloc_path();
954 if (!path)
955 return -ENOMEM;
956
957 key.objectid = device->devid;
958 key.offset = start;
959 key.type = BTRFS_DEV_EXTENT_KEY;
960
961 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400962 if (ret > 0) {
963 ret = btrfs_previous_item(root, path, key.objectid,
964 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000965 if (ret)
966 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400967 leaf = path->nodes[0];
968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
969 extent = btrfs_item_ptr(leaf, path->slots[0],
970 struct btrfs_dev_extent);
971 BUG_ON(found_key.offset > start || found_key.offset +
972 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400973 } else if (ret == 0) {
974 leaf = path->nodes[0];
975 extent = btrfs_item_ptr(leaf, path->slots[0],
976 struct btrfs_dev_extent);
977 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400978 BUG_ON(ret);
979
Chris Masondfe25022008-05-13 13:46:40 -0400980 if (device->bytes_used > 0)
981 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400982 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -0400983
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000984out:
Chris Mason8f18cf12008-04-25 16:53:30 -0400985 btrfs_free_path(path);
986 return ret;
987}
988
Yan Zheng2b820322008-11-17 21:11:30 -0500989int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400990 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400991 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500992 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400993{
994 int ret;
995 struct btrfs_path *path;
996 struct btrfs_root *root = device->dev_root;
997 struct btrfs_dev_extent *extent;
998 struct extent_buffer *leaf;
999 struct btrfs_key key;
1000
Chris Masondfe25022008-05-13 13:46:40 -04001001 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001002 path = btrfs_alloc_path();
1003 if (!path)
1004 return -ENOMEM;
1005
Chris Mason0b86a832008-03-24 15:01:56 -04001006 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001007 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001008 key.type = BTRFS_DEV_EXTENT_KEY;
1009 ret = btrfs_insert_empty_item(trans, root, path, &key,
1010 sizeof(*extent));
1011 BUG_ON(ret);
1012
1013 leaf = path->nodes[0];
1014 extent = btrfs_item_ptr(leaf, path->slots[0],
1015 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001016 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1017 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1018 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1019
1020 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1021 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1022 BTRFS_UUID_SIZE);
1023
Chris Mason0b86a832008-03-24 15:01:56 -04001024 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1025 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001026 btrfs_free_path(path);
1027 return ret;
1028}
1029
Chris Masona1b32a52008-09-05 16:09:51 -04001030static noinline int find_next_chunk(struct btrfs_root *root,
1031 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001032{
1033 struct btrfs_path *path;
1034 int ret;
1035 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001036 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001037 struct btrfs_key found_key;
1038
1039 path = btrfs_alloc_path();
1040 BUG_ON(!path);
1041
Chris Masone17cade2008-04-15 15:41:47 -04001042 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001043 key.offset = (u64)-1;
1044 key.type = BTRFS_CHUNK_ITEM_KEY;
1045
1046 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1047 if (ret < 0)
1048 goto error;
1049
1050 BUG_ON(ret == 0);
1051
1052 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1053 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001054 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001055 } else {
1056 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1057 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001058 if (found_key.objectid != objectid)
1059 *offset = 0;
1060 else {
1061 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1062 struct btrfs_chunk);
1063 *offset = found_key.offset +
1064 btrfs_chunk_length(path->nodes[0], chunk);
1065 }
Chris Mason0b86a832008-03-24 15:01:56 -04001066 }
1067 ret = 0;
1068error:
1069 btrfs_free_path(path);
1070 return ret;
1071}
1072
Yan Zheng2b820322008-11-17 21:11:30 -05001073static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001074{
1075 int ret;
1076 struct btrfs_key key;
1077 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001078 struct btrfs_path *path;
1079
1080 root = root->fs_info->chunk_root;
1081
1082 path = btrfs_alloc_path();
1083 if (!path)
1084 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001085
1086 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1087 key.type = BTRFS_DEV_ITEM_KEY;
1088 key.offset = (u64)-1;
1089
1090 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1091 if (ret < 0)
1092 goto error;
1093
1094 BUG_ON(ret == 0);
1095
1096 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1097 BTRFS_DEV_ITEM_KEY);
1098 if (ret) {
1099 *objectid = 1;
1100 } else {
1101 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1102 path->slots[0]);
1103 *objectid = found_key.offset + 1;
1104 }
1105 ret = 0;
1106error:
Yan Zheng2b820322008-11-17 21:11:30 -05001107 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001108 return ret;
1109}
1110
1111/*
1112 * the device information is stored in the chunk root
1113 * the btrfs_device struct should be fully filled in
1114 */
1115int btrfs_add_device(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_device *device)
1118{
1119 int ret;
1120 struct btrfs_path *path;
1121 struct btrfs_dev_item *dev_item;
1122 struct extent_buffer *leaf;
1123 struct btrfs_key key;
1124 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001125
1126 root = root->fs_info->chunk_root;
1127
1128 path = btrfs_alloc_path();
1129 if (!path)
1130 return -ENOMEM;
1131
Chris Mason0b86a832008-03-24 15:01:56 -04001132 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1133 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001134 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001135
1136 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001137 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001138 if (ret)
1139 goto out;
1140
1141 leaf = path->nodes[0];
1142 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1143
1144 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001145 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001146 btrfs_set_device_type(leaf, dev_item, device->type);
1147 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1148 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1149 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001150 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1151 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001152 btrfs_set_device_group(leaf, dev_item, 0);
1153 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1154 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001155 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001156
Chris Mason0b86a832008-03-24 15:01:56 -04001157 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001158 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001159 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1160 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001161 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001162
Yan Zheng2b820322008-11-17 21:11:30 -05001163 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001164out:
1165 btrfs_free_path(path);
1166 return ret;
1167}
Chris Mason8f18cf12008-04-25 16:53:30 -04001168
Chris Masona061fc82008-05-07 11:43:44 -04001169static int btrfs_rm_dev_item(struct btrfs_root *root,
1170 struct btrfs_device *device)
1171{
1172 int ret;
1173 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001174 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001175 struct btrfs_trans_handle *trans;
1176
1177 root = root->fs_info->chunk_root;
1178
1179 path = btrfs_alloc_path();
1180 if (!path)
1181 return -ENOMEM;
1182
Yan, Zhenga22285a2010-05-16 10:48:46 -04001183 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001184 if (IS_ERR(trans)) {
1185 btrfs_free_path(path);
1186 return PTR_ERR(trans);
1187 }
Chris Masona061fc82008-05-07 11:43:44 -04001188 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1189 key.type = BTRFS_DEV_ITEM_KEY;
1190 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001191 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001192
1193 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1194 if (ret < 0)
1195 goto out;
1196
1197 if (ret > 0) {
1198 ret = -ENOENT;
1199 goto out;
1200 }
1201
1202 ret = btrfs_del_item(trans, root, path);
1203 if (ret)
1204 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001205out:
1206 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001207 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001208 btrfs_commit_transaction(trans, root);
1209 return ret;
1210}
1211
1212int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1213{
1214 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001215 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001216 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001217 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001218 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001219 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001220 u64 all_avail;
1221 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001222 u64 num_devices;
1223 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001224 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001225 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001226
Chris Masona061fc82008-05-07 11:43:44 -04001227 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001228 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001229
1230 all_avail = root->fs_info->avail_data_alloc_bits |
1231 root->fs_info->avail_system_alloc_bits |
1232 root->fs_info->avail_metadata_alloc_bits;
1233
1234 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001235 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001236 printk(KERN_ERR "btrfs: unable to go below four devices "
1237 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001238 ret = -EINVAL;
1239 goto out;
1240 }
1241
1242 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001243 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001244 printk(KERN_ERR "btrfs: unable to go below two "
1245 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001246 ret = -EINVAL;
1247 goto out;
1248 }
1249
Chris Masondfe25022008-05-13 13:46:40 -04001250 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001251 struct list_head *devices;
1252 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001253
Chris Masondfe25022008-05-13 13:46:40 -04001254 device = NULL;
1255 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001256 /*
1257 * It is safe to read the devices since the volume_mutex
1258 * is held.
1259 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001260 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001261 if (tmp->in_fs_metadata && !tmp->bdev) {
1262 device = tmp;
1263 break;
1264 }
1265 }
1266 bdev = NULL;
1267 bh = NULL;
1268 disk_super = NULL;
1269 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001270 printk(KERN_ERR "btrfs: no missing devices found to "
1271 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001272 goto out;
1273 }
Chris Masondfe25022008-05-13 13:46:40 -04001274 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001275 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1276 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001277 if (IS_ERR(bdev)) {
1278 ret = PTR_ERR(bdev);
1279 goto out;
1280 }
1281
Yan Zheng2b820322008-11-17 21:11:30 -05001282 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001283 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001284 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001285 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001286 goto error_close;
1287 }
1288 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001289 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001290 dev_uuid = disk_super->dev_item.uuid;
1291 device = btrfs_find_device(root, devid, dev_uuid,
1292 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001293 if (!device) {
1294 ret = -ENOENT;
1295 goto error_brelse;
1296 }
Chris Masondfe25022008-05-13 13:46:40 -04001297 }
Yan Zheng2b820322008-11-17 21:11:30 -05001298
1299 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001300 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1301 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001302 ret = -EINVAL;
1303 goto error_brelse;
1304 }
1305
1306 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001307 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001308 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001309 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001310 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001311 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001312 }
Chris Masona061fc82008-05-07 11:43:44 -04001313
1314 ret = btrfs_shrink_device(device, 0);
1315 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001316 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001317
Chris Masona061fc82008-05-07 11:43:44 -04001318 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1319 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001320 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001321
Yan Zheng2b820322008-11-17 21:11:30 -05001322 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001323 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001324
1325 /*
1326 * the device list mutex makes sure that we don't change
1327 * the device list while someone else is writing out all
1328 * the device supers.
1329 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001330
1331 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001332 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001333 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001334
Yan Zhenge4404d62008-12-12 10:03:26 -05001335 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001336
Chris Masoncd02dca2010-12-13 14:56:23 -05001337 if (device->missing)
1338 root->fs_info->fs_devices->missing_devices--;
1339
Yan Zheng2b820322008-11-17 21:11:30 -05001340 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1341 struct btrfs_device, dev_list);
1342 if (device->bdev == root->fs_info->sb->s_bdev)
1343 root->fs_info->sb->s_bdev = next_device->bdev;
1344 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1345 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1346
Xiao Guangrong1f781602011-04-20 10:09:16 +00001347 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001348 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001349
1350 call_rcu(&device->rcu, free_device);
1351 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001352
Yan Zheng2b820322008-11-17 21:11:30 -05001353 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1354 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1355
Xiao Guangrong1f781602011-04-20 10:09:16 +00001356 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001357 struct btrfs_fs_devices *fs_devices;
1358 fs_devices = root->fs_info->fs_devices;
1359 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001360 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001361 break;
1362 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001363 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001364 fs_devices->seed = cur_devices->seed;
1365 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001366 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001367 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001368 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001369 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001370 }
1371
1372 /*
1373 * at this point, the device is zero sized. We want to
1374 * remove it from the devices list and zero out the old super
1375 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001376 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001377 /* make sure this device isn't detected as part of
1378 * the FS anymore
1379 */
1380 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1381 set_buffer_dirty(bh);
1382 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001383 }
Chris Masona061fc82008-05-07 11:43:44 -04001384
Chris Masona061fc82008-05-07 11:43:44 -04001385 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001386
1387error_brelse:
1388 brelse(bh);
1389error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001390 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001391 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001392out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001393 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001394 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001395 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001396error_undo:
1397 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001398 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001399 list_add(&device->dev_alloc_list,
1400 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001401 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001402 root->fs_info->fs_devices->rw_devices++;
1403 }
1404 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001405}
1406
Yan Zheng2b820322008-11-17 21:11:30 -05001407/*
1408 * does all the dirty work required for changing file system's UUID.
1409 */
1410static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1411 struct btrfs_root *root)
1412{
1413 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1414 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001415 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001416 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1417 struct btrfs_device *device;
1418 u64 super_flags;
1419
1420 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001421 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001422 return -EINVAL;
1423
Yan Zhenge4404d62008-12-12 10:03:26 -05001424 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1425 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001426 return -ENOMEM;
1427
Yan Zhenge4404d62008-12-12 10:03:26 -05001428 old_devices = clone_fs_devices(fs_devices);
1429 if (IS_ERR(old_devices)) {
1430 kfree(seed_devices);
1431 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001432 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001433
Yan Zheng2b820322008-11-17 21:11:30 -05001434 list_add(&old_devices->list, &fs_uuids);
1435
Yan Zhenge4404d62008-12-12 10:03:26 -05001436 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1437 seed_devices->opened = 1;
1438 INIT_LIST_HEAD(&seed_devices->devices);
1439 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001440 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001441
1442 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1444 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001445 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1446
Yan Zhenge4404d62008-12-12 10:03:26 -05001447 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1448 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1449 device->fs_devices = seed_devices;
1450 }
1451
Yan Zheng2b820322008-11-17 21:11:30 -05001452 fs_devices->seeding = 0;
1453 fs_devices->num_devices = 0;
1454 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001455 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001456
1457 generate_random_uuid(fs_devices->fsid);
1458 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1459 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1460 super_flags = btrfs_super_flags(disk_super) &
1461 ~BTRFS_SUPER_FLAG_SEEDING;
1462 btrfs_set_super_flags(disk_super, super_flags);
1463
1464 return 0;
1465}
1466
1467/*
1468 * strore the expected generation for seed devices in device items.
1469 */
1470static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1471 struct btrfs_root *root)
1472{
1473 struct btrfs_path *path;
1474 struct extent_buffer *leaf;
1475 struct btrfs_dev_item *dev_item;
1476 struct btrfs_device *device;
1477 struct btrfs_key key;
1478 u8 fs_uuid[BTRFS_UUID_SIZE];
1479 u8 dev_uuid[BTRFS_UUID_SIZE];
1480 u64 devid;
1481 int ret;
1482
1483 path = btrfs_alloc_path();
1484 if (!path)
1485 return -ENOMEM;
1486
1487 root = root->fs_info->chunk_root;
1488 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1489 key.offset = 0;
1490 key.type = BTRFS_DEV_ITEM_KEY;
1491
1492 while (1) {
1493 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1494 if (ret < 0)
1495 goto error;
1496
1497 leaf = path->nodes[0];
1498next_slot:
1499 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1500 ret = btrfs_next_leaf(root, path);
1501 if (ret > 0)
1502 break;
1503 if (ret < 0)
1504 goto error;
1505 leaf = path->nodes[0];
1506 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001507 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001508 continue;
1509 }
1510
1511 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1512 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1513 key.type != BTRFS_DEV_ITEM_KEY)
1514 break;
1515
1516 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1517 struct btrfs_dev_item);
1518 devid = btrfs_device_id(leaf, dev_item);
1519 read_extent_buffer(leaf, dev_uuid,
1520 (unsigned long)btrfs_device_uuid(dev_item),
1521 BTRFS_UUID_SIZE);
1522 read_extent_buffer(leaf, fs_uuid,
1523 (unsigned long)btrfs_device_fsid(dev_item),
1524 BTRFS_UUID_SIZE);
1525 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1526 BUG_ON(!device);
1527
1528 if (device->fs_devices->seeding) {
1529 btrfs_set_device_generation(leaf, dev_item,
1530 device->generation);
1531 btrfs_mark_buffer_dirty(leaf);
1532 }
1533
1534 path->slots[0]++;
1535 goto next_slot;
1536 }
1537 ret = 0;
1538error:
1539 btrfs_free_path(path);
1540 return ret;
1541}
1542
Chris Mason788f20e2008-04-28 15:29:42 -04001543int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1544{
1545 struct btrfs_trans_handle *trans;
1546 struct btrfs_device *device;
1547 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001548 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001549 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001550 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001551 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001552 int ret = 0;
1553
Yan Zheng2b820322008-11-17 21:11:30 -05001554 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1555 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001556
Tejun Heod4d77622010-11-13 11:55:18 +01001557 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1558 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001559 if (IS_ERR(bdev))
1560 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001561
Yan Zheng2b820322008-11-17 21:11:30 -05001562 if (root->fs_info->fs_devices->seeding) {
1563 seeding_dev = 1;
1564 down_write(&sb->s_umount);
1565 mutex_lock(&uuid_mutex);
1566 }
1567
Chris Mason8c8bee12008-09-29 11:19:10 -04001568 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001569 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001570
Chris Mason788f20e2008-04-28 15:29:42 -04001571 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001572 /*
1573 * we have the volume lock, so we don't need the extra
1574 * device list mutex while reading the list here.
1575 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001576 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001577 if (device->bdev == bdev) {
1578 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001579 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001580 }
1581 }
1582
1583 device = kzalloc(sizeof(*device), GFP_NOFS);
1584 if (!device) {
1585 /* we can safely leave the fs_devices entry around */
1586 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001587 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001588 }
1589
Chris Mason788f20e2008-04-28 15:29:42 -04001590 device->name = kstrdup(device_path, GFP_NOFS);
1591 if (!device->name) {
1592 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001593 ret = -ENOMEM;
1594 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001595 }
Yan Zheng2b820322008-11-17 21:11:30 -05001596
1597 ret = find_next_devid(root, &device->devid);
1598 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001599 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001600 kfree(device);
1601 goto error;
1602 }
1603
Yan, Zhenga22285a2010-05-16 10:48:46 -04001604 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001605 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001606 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001607 kfree(device);
1608 ret = PTR_ERR(trans);
1609 goto error;
1610 }
1611
Yan Zheng2b820322008-11-17 21:11:30 -05001612 lock_chunks(root);
1613
Yan Zheng2b820322008-11-17 21:11:30 -05001614 device->writeable = 1;
1615 device->work.func = pending_bios_fn;
1616 generate_random_uuid(device->uuid);
1617 spin_lock_init(&device->io_lock);
1618 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001619 device->io_width = root->sectorsize;
1620 device->io_align = root->sectorsize;
1621 device->sector_size = root->sectorsize;
1622 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001623 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001624 device->dev_root = root->fs_info->dev_root;
1625 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001626 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001627 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001628 set_blocksize(device->bdev, 4096);
1629
Yan Zheng2b820322008-11-17 21:11:30 -05001630 if (seeding_dev) {
1631 sb->s_flags &= ~MS_RDONLY;
1632 ret = btrfs_prepare_sprout(trans, root);
1633 BUG_ON(ret);
1634 }
1635
1636 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001637
1638 /*
1639 * we don't want write_supers to jump in here with our device
1640 * half setup
1641 */
1642 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001643 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001644 list_add(&device->dev_alloc_list,
1645 &root->fs_info->fs_devices->alloc_list);
1646 root->fs_info->fs_devices->num_devices++;
1647 root->fs_info->fs_devices->open_devices++;
1648 root->fs_info->fs_devices->rw_devices++;
1649 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1650
Chris Masonc2898112009-06-10 09:51:32 -04001651 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1652 root->fs_info->fs_devices->rotating = 1;
1653
Chris Mason788f20e2008-04-28 15:29:42 -04001654 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1655 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1656 total_bytes + device->total_bytes);
1657
1658 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1659 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1660 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001661 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001662
Yan Zheng2b820322008-11-17 21:11:30 -05001663 if (seeding_dev) {
1664 ret = init_first_rw_device(trans, root, device);
1665 BUG_ON(ret);
1666 ret = btrfs_finish_sprout(trans, root);
1667 BUG_ON(ret);
1668 } else {
1669 ret = btrfs_add_device(trans, root, device);
1670 }
1671
Chris Mason913d9522009-03-10 13:17:18 -04001672 /*
1673 * we've got more storage, clear any full flags on the space
1674 * infos
1675 */
1676 btrfs_clear_space_info_full(root->fs_info);
1677
Chris Mason7d9eb122008-07-08 14:19:17 -04001678 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001679 btrfs_commit_transaction(trans, root);
1680
1681 if (seeding_dev) {
1682 mutex_unlock(&uuid_mutex);
1683 up_write(&sb->s_umount);
1684
1685 ret = btrfs_relocate_sys_chunks(root);
1686 BUG_ON(ret);
1687 }
1688out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001689 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001690 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001691error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001692 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001693 if (seeding_dev) {
1694 mutex_unlock(&uuid_mutex);
1695 up_write(&sb->s_umount);
1696 }
Chris Mason788f20e2008-04-28 15:29:42 -04001697 goto out;
1698}
1699
Chris Masond3977122009-01-05 21:25:51 -05001700static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1701 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001702{
1703 int ret;
1704 struct btrfs_path *path;
1705 struct btrfs_root *root;
1706 struct btrfs_dev_item *dev_item;
1707 struct extent_buffer *leaf;
1708 struct btrfs_key key;
1709
1710 root = device->dev_root->fs_info->chunk_root;
1711
1712 path = btrfs_alloc_path();
1713 if (!path)
1714 return -ENOMEM;
1715
1716 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1717 key.type = BTRFS_DEV_ITEM_KEY;
1718 key.offset = device->devid;
1719
1720 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1721 if (ret < 0)
1722 goto out;
1723
1724 if (ret > 0) {
1725 ret = -ENOENT;
1726 goto out;
1727 }
1728
1729 leaf = path->nodes[0];
1730 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1731
1732 btrfs_set_device_id(leaf, dev_item, device->devid);
1733 btrfs_set_device_type(leaf, dev_item, device->type);
1734 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1735 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1736 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001737 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001738 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1739 btrfs_mark_buffer_dirty(leaf);
1740
1741out:
1742 btrfs_free_path(path);
1743 return ret;
1744}
1745
Chris Mason7d9eb122008-07-08 14:19:17 -04001746static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001747 struct btrfs_device *device, u64 new_size)
1748{
1749 struct btrfs_super_block *super_copy =
1750 &device->dev_root->fs_info->super_copy;
1751 u64 old_total = btrfs_super_total_bytes(super_copy);
1752 u64 diff = new_size - device->total_bytes;
1753
Yan Zheng2b820322008-11-17 21:11:30 -05001754 if (!device->writeable)
1755 return -EACCES;
1756 if (new_size <= device->total_bytes)
1757 return -EINVAL;
1758
Chris Mason8f18cf12008-04-25 16:53:30 -04001759 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001760 device->fs_devices->total_rw_bytes += diff;
1761
1762 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001763 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001764 btrfs_clear_space_info_full(device->dev_root->fs_info);
1765
Chris Mason8f18cf12008-04-25 16:53:30 -04001766 return btrfs_update_device(trans, device);
1767}
1768
Chris Mason7d9eb122008-07-08 14:19:17 -04001769int btrfs_grow_device(struct btrfs_trans_handle *trans,
1770 struct btrfs_device *device, u64 new_size)
1771{
1772 int ret;
1773 lock_chunks(device->dev_root);
1774 ret = __btrfs_grow_device(trans, device, new_size);
1775 unlock_chunks(device->dev_root);
1776 return ret;
1777}
1778
Chris Mason8f18cf12008-04-25 16:53:30 -04001779static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1780 struct btrfs_root *root,
1781 u64 chunk_tree, u64 chunk_objectid,
1782 u64 chunk_offset)
1783{
1784 int ret;
1785 struct btrfs_path *path;
1786 struct btrfs_key key;
1787
1788 root = root->fs_info->chunk_root;
1789 path = btrfs_alloc_path();
1790 if (!path)
1791 return -ENOMEM;
1792
1793 key.objectid = chunk_objectid;
1794 key.offset = chunk_offset;
1795 key.type = BTRFS_CHUNK_ITEM_KEY;
1796
1797 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1798 BUG_ON(ret);
1799
1800 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001801
1802 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001803 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001804}
1805
Christoph Hellwigb2950862008-12-02 09:54:17 -05001806static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001807 chunk_offset)
1808{
1809 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1810 struct btrfs_disk_key *disk_key;
1811 struct btrfs_chunk *chunk;
1812 u8 *ptr;
1813 int ret = 0;
1814 u32 num_stripes;
1815 u32 array_size;
1816 u32 len = 0;
1817 u32 cur;
1818 struct btrfs_key key;
1819
1820 array_size = btrfs_super_sys_array_size(super_copy);
1821
1822 ptr = super_copy->sys_chunk_array;
1823 cur = 0;
1824
1825 while (cur < array_size) {
1826 disk_key = (struct btrfs_disk_key *)ptr;
1827 btrfs_disk_key_to_cpu(&key, disk_key);
1828
1829 len = sizeof(*disk_key);
1830
1831 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1832 chunk = (struct btrfs_chunk *)(ptr + len);
1833 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1834 len += btrfs_chunk_item_size(num_stripes);
1835 } else {
1836 ret = -EIO;
1837 break;
1838 }
1839 if (key.objectid == chunk_objectid &&
1840 key.offset == chunk_offset) {
1841 memmove(ptr, ptr + len, array_size - (cur + len));
1842 array_size -= len;
1843 btrfs_set_super_sys_array_size(super_copy, array_size);
1844 } else {
1845 ptr += len;
1846 cur += len;
1847 }
1848 }
1849 return ret;
1850}
1851
Christoph Hellwigb2950862008-12-02 09:54:17 -05001852static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001853 u64 chunk_tree, u64 chunk_objectid,
1854 u64 chunk_offset)
1855{
1856 struct extent_map_tree *em_tree;
1857 struct btrfs_root *extent_root;
1858 struct btrfs_trans_handle *trans;
1859 struct extent_map *em;
1860 struct map_lookup *map;
1861 int ret;
1862 int i;
1863
1864 root = root->fs_info->chunk_root;
1865 extent_root = root->fs_info->extent_root;
1866 em_tree = &root->fs_info->mapping_tree.map_tree;
1867
Josef Bacikba1bf482009-09-11 16:11:19 -04001868 ret = btrfs_can_relocate(extent_root, chunk_offset);
1869 if (ret)
1870 return -ENOSPC;
1871
Chris Mason8f18cf12008-04-25 16:53:30 -04001872 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001873 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001874 if (ret)
1875 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001876
Yan, Zhenga22285a2010-05-16 10:48:46 -04001877 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001878 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001879
Chris Mason7d9eb122008-07-08 14:19:17 -04001880 lock_chunks(root);
1881
Chris Mason8f18cf12008-04-25 16:53:30 -04001882 /*
1883 * step two, delete the device extents and the
1884 * chunk tree entries
1885 */
Chris Mason890871b2009-09-02 16:24:52 -04001886 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001887 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001888 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001889
Chris Masona061fc82008-05-07 11:43:44 -04001890 BUG_ON(em->start > chunk_offset ||
1891 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001892 map = (struct map_lookup *)em->bdev;
1893
1894 for (i = 0; i < map->num_stripes; i++) {
1895 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1896 map->stripes[i].physical);
1897 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001898
Chris Masondfe25022008-05-13 13:46:40 -04001899 if (map->stripes[i].dev) {
1900 ret = btrfs_update_device(trans, map->stripes[i].dev);
1901 BUG_ON(ret);
1902 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001903 }
1904 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1905 chunk_offset);
1906
1907 BUG_ON(ret);
1908
liubo1abe9b82011-03-24 11:18:59 +00001909 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1910
Chris Mason8f18cf12008-04-25 16:53:30 -04001911 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1912 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1913 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001914 }
1915
Zheng Yan1a40e232008-09-26 10:09:34 -04001916 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1917 BUG_ON(ret);
1918
Chris Mason890871b2009-09-02 16:24:52 -04001919 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001920 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001921 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001922
Chris Mason8f18cf12008-04-25 16:53:30 -04001923 kfree(map);
1924 em->bdev = NULL;
1925
1926 /* once for the tree */
1927 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001928 /* once for us */
1929 free_extent_map(em);
1930
Chris Mason7d9eb122008-07-08 14:19:17 -04001931 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001932 btrfs_end_transaction(trans, root);
1933 return 0;
1934}
1935
Yan Zheng2b820322008-11-17 21:11:30 -05001936static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1937{
1938 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1939 struct btrfs_path *path;
1940 struct extent_buffer *leaf;
1941 struct btrfs_chunk *chunk;
1942 struct btrfs_key key;
1943 struct btrfs_key found_key;
1944 u64 chunk_tree = chunk_root->root_key.objectid;
1945 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001946 bool retried = false;
1947 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001948 int ret;
1949
1950 path = btrfs_alloc_path();
1951 if (!path)
1952 return -ENOMEM;
1953
Josef Bacikba1bf482009-09-11 16:11:19 -04001954again:
Yan Zheng2b820322008-11-17 21:11:30 -05001955 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1956 key.offset = (u64)-1;
1957 key.type = BTRFS_CHUNK_ITEM_KEY;
1958
1959 while (1) {
1960 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1961 if (ret < 0)
1962 goto error;
1963 BUG_ON(ret == 0);
1964
1965 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1966 key.type);
1967 if (ret < 0)
1968 goto error;
1969 if (ret > 0)
1970 break;
1971
1972 leaf = path->nodes[0];
1973 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1974
1975 chunk = btrfs_item_ptr(leaf, path->slots[0],
1976 struct btrfs_chunk);
1977 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02001978 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001979
1980 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1981 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1982 found_key.objectid,
1983 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001984 if (ret == -ENOSPC)
1985 failed++;
1986 else if (ret)
1987 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001988 }
1989
1990 if (found_key.offset == 0)
1991 break;
1992 key.offset = found_key.offset - 1;
1993 }
1994 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001995 if (failed && !retried) {
1996 failed = 0;
1997 retried = true;
1998 goto again;
1999 } else if (failed && retried) {
2000 WARN_ON(1);
2001 ret = -ENOSPC;
2002 }
Yan Zheng2b820322008-11-17 21:11:30 -05002003error:
2004 btrfs_free_path(path);
2005 return ret;
2006}
2007
Chris Masonec44a352008-04-28 15:29:52 -04002008static u64 div_factor(u64 num, int factor)
2009{
2010 if (factor == 10)
2011 return num;
2012 num *= factor;
2013 do_div(num, 10);
2014 return num;
2015}
2016
Chris Masonec44a352008-04-28 15:29:52 -04002017int btrfs_balance(struct btrfs_root *dev_root)
2018{
2019 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002020 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2021 struct btrfs_device *device;
2022 u64 old_size;
2023 u64 size_to_free;
2024 struct btrfs_path *path;
2025 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002026 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2027 struct btrfs_trans_handle *trans;
2028 struct btrfs_key found_key;
2029
Yan Zheng2b820322008-11-17 21:11:30 -05002030 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2031 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002032
Ben Hutchings6f88a442010-12-29 14:55:03 +00002033 if (!capable(CAP_SYS_ADMIN))
2034 return -EPERM;
2035
Chris Mason7d9eb122008-07-08 14:19:17 -04002036 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002037 dev_root = dev_root->fs_info->dev_root;
2038
Chris Masonec44a352008-04-28 15:29:52 -04002039 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002040 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002041 old_size = device->total_bytes;
2042 size_to_free = div_factor(old_size, 1);
2043 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002044 if (!device->writeable ||
2045 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002046 continue;
2047
2048 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002049 if (ret == -ENOSPC)
2050 break;
Chris Masonec44a352008-04-28 15:29:52 -04002051 BUG_ON(ret);
2052
Yan, Zhenga22285a2010-05-16 10:48:46 -04002053 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002054 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002055
2056 ret = btrfs_grow_device(trans, device, old_size);
2057 BUG_ON(ret);
2058
2059 btrfs_end_transaction(trans, dev_root);
2060 }
2061
2062 /* step two, relocate all the chunks */
2063 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002064 if (!path) {
2065 ret = -ENOMEM;
2066 goto error;
2067 }
Chris Masonec44a352008-04-28 15:29:52 -04002068 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2069 key.offset = (u64)-1;
2070 key.type = BTRFS_CHUNK_ITEM_KEY;
2071
Chris Masond3977122009-01-05 21:25:51 -05002072 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002073 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2074 if (ret < 0)
2075 goto error;
2076
2077 /*
2078 * this shouldn't happen, it means the last relocate
2079 * failed
2080 */
2081 if (ret == 0)
2082 break;
2083
2084 ret = btrfs_previous_item(chunk_root, path, 0,
2085 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002086 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002087 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002088
Chris Masonec44a352008-04-28 15:29:52 -04002089 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2090 path->slots[0]);
2091 if (found_key.objectid != key.objectid)
2092 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002093
Chris Masonec44a352008-04-28 15:29:52 -04002094 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002095 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002096 break;
2097
David Sterbab3b4aa72011-04-21 01:20:15 +02002098 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002099 ret = btrfs_relocate_chunk(chunk_root,
2100 chunk_root->root_key.objectid,
2101 found_key.objectid,
2102 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002103 if (ret && ret != -ENOSPC)
2104 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002105 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002106 }
2107 ret = 0;
2108error:
2109 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002110 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002111 return ret;
2112}
2113
Chris Mason8f18cf12008-04-25 16:53:30 -04002114/*
2115 * shrinking a device means finding all of the device extents past
2116 * the new size, and then following the back refs to the chunks.
2117 * The chunk relocation code actually frees the device extent
2118 */
2119int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2120{
2121 struct btrfs_trans_handle *trans;
2122 struct btrfs_root *root = device->dev_root;
2123 struct btrfs_dev_extent *dev_extent = NULL;
2124 struct btrfs_path *path;
2125 u64 length;
2126 u64 chunk_tree;
2127 u64 chunk_objectid;
2128 u64 chunk_offset;
2129 int ret;
2130 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002131 int failed = 0;
2132 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002133 struct extent_buffer *l;
2134 struct btrfs_key key;
2135 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2136 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002137 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002138 u64 diff = device->total_bytes - new_size;
2139
Yan Zheng2b820322008-11-17 21:11:30 -05002140 if (new_size >= device->total_bytes)
2141 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002142
2143 path = btrfs_alloc_path();
2144 if (!path)
2145 return -ENOMEM;
2146
Chris Mason8f18cf12008-04-25 16:53:30 -04002147 path->reada = 2;
2148
Chris Mason7d9eb122008-07-08 14:19:17 -04002149 lock_chunks(root);
2150
Chris Mason8f18cf12008-04-25 16:53:30 -04002151 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002152 if (device->writeable)
2153 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002154 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002155
Josef Bacikba1bf482009-09-11 16:11:19 -04002156again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002157 key.objectid = device->devid;
2158 key.offset = (u64)-1;
2159 key.type = BTRFS_DEV_EXTENT_KEY;
2160
2161 while (1) {
2162 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2163 if (ret < 0)
2164 goto done;
2165
2166 ret = btrfs_previous_item(root, path, 0, key.type);
2167 if (ret < 0)
2168 goto done;
2169 if (ret) {
2170 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002171 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002172 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002173 }
2174
2175 l = path->nodes[0];
2176 slot = path->slots[0];
2177 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2178
Josef Bacikba1bf482009-09-11 16:11:19 -04002179 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002180 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002181 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002182 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002183
2184 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2185 length = btrfs_dev_extent_length(l, dev_extent);
2186
Josef Bacikba1bf482009-09-11 16:11:19 -04002187 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002188 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002189 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002190 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002191
2192 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2193 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2194 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002195 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002196
2197 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2198 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002199 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002200 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002201 if (ret == -ENOSPC)
2202 failed++;
2203 key.offset -= 1;
2204 }
2205
2206 if (failed && !retried) {
2207 failed = 0;
2208 retried = true;
2209 goto again;
2210 } else if (failed && retried) {
2211 ret = -ENOSPC;
2212 lock_chunks(root);
2213
2214 device->total_bytes = old_size;
2215 if (device->writeable)
2216 device->fs_devices->total_rw_bytes += diff;
2217 unlock_chunks(root);
2218 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002219 }
2220
Chris Balld6397ba2009-04-27 07:29:03 -04002221 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002222 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002223 if (IS_ERR(trans)) {
2224 ret = PTR_ERR(trans);
2225 goto done;
2226 }
2227
Chris Balld6397ba2009-04-27 07:29:03 -04002228 lock_chunks(root);
2229
2230 device->disk_total_bytes = new_size;
2231 /* Now btrfs_update_device() will change the on-disk size. */
2232 ret = btrfs_update_device(trans, device);
2233 if (ret) {
2234 unlock_chunks(root);
2235 btrfs_end_transaction(trans, root);
2236 goto done;
2237 }
2238 WARN_ON(diff > old_total);
2239 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2240 unlock_chunks(root);
2241 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002242done:
2243 btrfs_free_path(path);
2244 return ret;
2245}
2246
Christoph Hellwigb2950862008-12-02 09:54:17 -05002247static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002248 struct btrfs_root *root,
2249 struct btrfs_key *key,
2250 struct btrfs_chunk *chunk, int item_size)
2251{
2252 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2253 struct btrfs_disk_key disk_key;
2254 u32 array_size;
2255 u8 *ptr;
2256
2257 array_size = btrfs_super_sys_array_size(super_copy);
2258 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2259 return -EFBIG;
2260
2261 ptr = super_copy->sys_chunk_array + array_size;
2262 btrfs_cpu_key_to_disk(&disk_key, key);
2263 memcpy(ptr, &disk_key, sizeof(disk_key));
2264 ptr += sizeof(disk_key);
2265 memcpy(ptr, chunk, item_size);
2266 item_size += sizeof(disk_key);
2267 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2268 return 0;
2269}
2270
Miao Xieb2117a32011-01-05 10:07:28 +00002271/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002272 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002273 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002274static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002275{
Arne Jansen73c5de02011-04-12 12:07:57 +02002276 const struct btrfs_device_info *di_a = a;
2277 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002278
Arne Jansen73c5de02011-04-12 12:07:57 +02002279 if (di_a->max_avail > di_b->max_avail)
2280 return -1;
2281 if (di_a->max_avail < di_b->max_avail)
2282 return 1;
2283 if (di_a->total_avail > di_b->total_avail)
2284 return -1;
2285 if (di_a->total_avail < di_b->total_avail)
2286 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002287 return 0;
2288}
2289
2290static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2291 struct btrfs_root *extent_root,
2292 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002293 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002294 u64 start, u64 type)
2295{
2296 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002297 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2298 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002299 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002300 struct extent_map_tree *em_tree;
2301 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002302 struct btrfs_device_info *devices_info = NULL;
2303 u64 total_avail;
2304 int num_stripes; /* total number of stripes to allocate */
2305 int sub_stripes; /* sub_stripes info for map */
2306 int dev_stripes; /* stripes per dev */
2307 int devs_max; /* max devs to use */
2308 int devs_min; /* min devs needed */
2309 int devs_increment; /* ndevs has to be a multiple of this */
2310 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002311 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002312 u64 max_stripe_size;
2313 u64 max_chunk_size;
2314 u64 stripe_size;
2315 u64 num_bytes;
2316 int ndevs;
2317 int i;
2318 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002319
2320 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2321 (type & BTRFS_BLOCK_GROUP_DUP)) {
2322 WARN_ON(1);
2323 type &= ~BTRFS_BLOCK_GROUP_DUP;
2324 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002325
Miao Xieb2117a32011-01-05 10:07:28 +00002326 if (list_empty(&fs_devices->alloc_list))
2327 return -ENOSPC;
2328
Arne Jansen73c5de02011-04-12 12:07:57 +02002329 sub_stripes = 1;
2330 dev_stripes = 1;
2331 devs_increment = 1;
2332 ncopies = 1;
2333 devs_max = 0; /* 0 == as many as possible */
2334 devs_min = 1;
2335
2336 /*
2337 * define the properties of each RAID type.
2338 * FIXME: move this to a global table and use it in all RAID
2339 * calculation code
2340 */
2341 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2342 dev_stripes = 2;
2343 ncopies = 2;
2344 devs_max = 1;
2345 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2346 devs_min = 2;
2347 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2348 devs_increment = 2;
2349 ncopies = 2;
2350 devs_max = 2;
2351 devs_min = 2;
2352 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2353 sub_stripes = 2;
2354 devs_increment = 2;
2355 ncopies = 2;
2356 devs_min = 4;
2357 } else {
2358 devs_max = 1;
2359 }
2360
2361 if (type & BTRFS_BLOCK_GROUP_DATA) {
2362 max_stripe_size = 1024 * 1024 * 1024;
2363 max_chunk_size = 10 * max_stripe_size;
2364 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2365 max_stripe_size = 256 * 1024 * 1024;
2366 max_chunk_size = max_stripe_size;
2367 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2368 max_stripe_size = 8 * 1024 * 1024;
2369 max_chunk_size = 2 * max_stripe_size;
2370 } else {
2371 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2372 type);
2373 BUG_ON(1);
2374 }
2375
2376 /* we don't want a chunk larger than 10% of writeable space */
2377 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2378 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002379
2380 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2381 GFP_NOFS);
2382 if (!devices_info)
2383 return -ENOMEM;
2384
Arne Jansen73c5de02011-04-12 12:07:57 +02002385 cur = fs_devices->alloc_list.next;
2386
2387 /*
2388 * in the first pass through the devices list, we gather information
2389 * about the available holes on each device.
2390 */
2391 ndevs = 0;
2392 while (cur != &fs_devices->alloc_list) {
2393 struct btrfs_device *device;
2394 u64 max_avail;
2395 u64 dev_offset;
2396
2397 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2398
2399 cur = cur->next;
2400
2401 if (!device->writeable) {
2402 printk(KERN_ERR
2403 "btrfs: read-only device in alloc_list\n");
2404 WARN_ON(1);
2405 continue;
2406 }
2407
2408 if (!device->in_fs_metadata)
2409 continue;
2410
2411 if (device->total_bytes > device->bytes_used)
2412 total_avail = device->total_bytes - device->bytes_used;
2413 else
2414 total_avail = 0;
2415 /* avail is off by max(alloc_start, 1MB), but that is the same
2416 * for all devices, so it doesn't hurt the sorting later on
2417 */
2418
2419 ret = find_free_dev_extent(trans, device,
2420 max_stripe_size * dev_stripes,
2421 &dev_offset, &max_avail);
2422 if (ret && ret != -ENOSPC)
2423 goto error;
2424
2425 if (ret == 0)
2426 max_avail = max_stripe_size * dev_stripes;
2427
2428 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2429 continue;
2430
2431 devices_info[ndevs].dev_offset = dev_offset;
2432 devices_info[ndevs].max_avail = max_avail;
2433 devices_info[ndevs].total_avail = total_avail;
2434 devices_info[ndevs].dev = device;
2435 ++ndevs;
2436 }
2437
2438 /*
2439 * now sort the devices by hole size / available space
2440 */
2441 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2442 btrfs_cmp_device_info, NULL);
2443
2444 /* round down to number of usable stripes */
2445 ndevs -= ndevs % devs_increment;
2446
2447 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2448 ret = -ENOSPC;
2449 goto error;
2450 }
2451
2452 if (devs_max && ndevs > devs_max)
2453 ndevs = devs_max;
2454 /*
2455 * the primary goal is to maximize the number of stripes, so use as many
2456 * devices as possible, even if the stripes are not maximum sized.
2457 */
2458 stripe_size = devices_info[ndevs-1].max_avail;
2459 num_stripes = ndevs * dev_stripes;
2460
2461 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2462 stripe_size = max_chunk_size * ncopies;
2463 do_div(stripe_size, num_stripes);
2464 }
2465
2466 do_div(stripe_size, dev_stripes);
2467 do_div(stripe_size, BTRFS_STRIPE_LEN);
2468 stripe_size *= BTRFS_STRIPE_LEN;
2469
Miao Xieb2117a32011-01-05 10:07:28 +00002470 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2471 if (!map) {
2472 ret = -ENOMEM;
2473 goto error;
2474 }
2475 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002476
Arne Jansen73c5de02011-04-12 12:07:57 +02002477 for (i = 0; i < ndevs; ++i) {
2478 for (j = 0; j < dev_stripes; ++j) {
2479 int s = i * dev_stripes + j;
2480 map->stripes[s].dev = devices_info[i].dev;
2481 map->stripes[s].physical = devices_info[i].dev_offset +
2482 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002483 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002484 }
Chris Mason593060d2008-03-25 16:50:33 -04002485 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002486 map->stripe_len = BTRFS_STRIPE_LEN;
2487 map->io_align = BTRFS_STRIPE_LEN;
2488 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002489 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002490 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002491
Yan Zheng2b820322008-11-17 21:11:30 -05002492 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002493 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002494
Arne Jansen73c5de02011-04-12 12:07:57 +02002495 *stripe_size_out = stripe_size;
2496 *num_bytes_out = num_bytes;
2497
2498 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002499
David Sterba172ddd62011-04-21 00:48:27 +02002500 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002501 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002502 ret = -ENOMEM;
2503 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002504 }
Chris Mason0b86a832008-03-24 15:01:56 -04002505 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002506 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002507 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002508 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002509 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002510
Chris Mason0b86a832008-03-24 15:01:56 -04002511 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002512 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002513 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002514 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002515 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002516 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002517
2518 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2519 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002520 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002521 BUG_ON(ret);
2522
Arne Jansen73c5de02011-04-12 12:07:57 +02002523 for (i = 0; i < map->num_stripes; ++i) {
2524 struct btrfs_device *device;
2525 u64 dev_offset;
2526
2527 device = map->stripes[i].dev;
2528 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002529
2530 ret = btrfs_alloc_dev_extent(trans, device,
2531 info->chunk_root->root_key.objectid,
2532 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002533 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002534 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002535 }
2536
Miao Xieb2117a32011-01-05 10:07:28 +00002537 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002538 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002539
2540error:
2541 kfree(map);
2542 kfree(devices_info);
2543 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002544}
2545
2546static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2547 struct btrfs_root *extent_root,
2548 struct map_lookup *map, u64 chunk_offset,
2549 u64 chunk_size, u64 stripe_size)
2550{
2551 u64 dev_offset;
2552 struct btrfs_key key;
2553 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2554 struct btrfs_device *device;
2555 struct btrfs_chunk *chunk;
2556 struct btrfs_stripe *stripe;
2557 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2558 int index = 0;
2559 int ret;
2560
2561 chunk = kzalloc(item_size, GFP_NOFS);
2562 if (!chunk)
2563 return -ENOMEM;
2564
2565 index = 0;
2566 while (index < map->num_stripes) {
2567 device = map->stripes[index].dev;
2568 device->bytes_used += stripe_size;
2569 ret = btrfs_update_device(trans, device);
2570 BUG_ON(ret);
2571 index++;
2572 }
2573
2574 index = 0;
2575 stripe = &chunk->stripe;
2576 while (index < map->num_stripes) {
2577 device = map->stripes[index].dev;
2578 dev_offset = map->stripes[index].physical;
2579
2580 btrfs_set_stack_stripe_devid(stripe, device->devid);
2581 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2582 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2583 stripe++;
2584 index++;
2585 }
2586
2587 btrfs_set_stack_chunk_length(chunk, chunk_size);
2588 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2589 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2590 btrfs_set_stack_chunk_type(chunk, map->type);
2591 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2592 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2593 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2594 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2595 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2596
2597 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2598 key.type = BTRFS_CHUNK_ITEM_KEY;
2599 key.offset = chunk_offset;
2600
2601 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2602 BUG_ON(ret);
2603
2604 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2605 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2606 item_size);
2607 BUG_ON(ret);
2608 }
liubo1abe9b82011-03-24 11:18:59 +00002609
Yan Zheng2b820322008-11-17 21:11:30 -05002610 kfree(chunk);
2611 return 0;
2612}
2613
2614/*
2615 * Chunk allocation falls into two parts. The first part does works
2616 * that make the new allocated chunk useable, but not do any operation
2617 * that modifies the chunk tree. The second part does the works that
2618 * require modifying the chunk tree. This division is important for the
2619 * bootstrap process of adding storage to a seed btrfs.
2620 */
2621int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2622 struct btrfs_root *extent_root, u64 type)
2623{
2624 u64 chunk_offset;
2625 u64 chunk_size;
2626 u64 stripe_size;
2627 struct map_lookup *map;
2628 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2629 int ret;
2630
2631 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2632 &chunk_offset);
2633 if (ret)
2634 return ret;
2635
2636 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2637 &stripe_size, chunk_offset, type);
2638 if (ret)
2639 return ret;
2640
2641 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2642 chunk_size, stripe_size);
2643 BUG_ON(ret);
2644 return 0;
2645}
2646
Chris Masond3977122009-01-05 21:25:51 -05002647static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002648 struct btrfs_root *root,
2649 struct btrfs_device *device)
2650{
2651 u64 chunk_offset;
2652 u64 sys_chunk_offset;
2653 u64 chunk_size;
2654 u64 sys_chunk_size;
2655 u64 stripe_size;
2656 u64 sys_stripe_size;
2657 u64 alloc_profile;
2658 struct map_lookup *map;
2659 struct map_lookup *sys_map;
2660 struct btrfs_fs_info *fs_info = root->fs_info;
2661 struct btrfs_root *extent_root = fs_info->extent_root;
2662 int ret;
2663
2664 ret = find_next_chunk(fs_info->chunk_root,
2665 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2666 BUG_ON(ret);
2667
2668 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2669 (fs_info->metadata_alloc_profile &
2670 fs_info->avail_metadata_alloc_bits);
2671 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2672
2673 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2674 &stripe_size, chunk_offset, alloc_profile);
2675 BUG_ON(ret);
2676
2677 sys_chunk_offset = chunk_offset + chunk_size;
2678
2679 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2680 (fs_info->system_alloc_profile &
2681 fs_info->avail_system_alloc_bits);
2682 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2683
2684 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2685 &sys_chunk_size, &sys_stripe_size,
2686 sys_chunk_offset, alloc_profile);
2687 BUG_ON(ret);
2688
2689 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2690 BUG_ON(ret);
2691
2692 /*
2693 * Modifying chunk tree needs allocating new blocks from both
2694 * system block group and metadata block group. So we only can
2695 * do operations require modifying the chunk tree after both
2696 * block groups were created.
2697 */
2698 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2699 chunk_size, stripe_size);
2700 BUG_ON(ret);
2701
2702 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2703 sys_chunk_offset, sys_chunk_size,
2704 sys_stripe_size);
2705 BUG_ON(ret);
2706 return 0;
2707}
2708
2709int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2710{
2711 struct extent_map *em;
2712 struct map_lookup *map;
2713 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2714 int readonly = 0;
2715 int i;
2716
Chris Mason890871b2009-09-02 16:24:52 -04002717 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002718 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002719 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002720 if (!em)
2721 return 1;
2722
Josef Bacikf48b9072010-01-27 02:07:59 +00002723 if (btrfs_test_opt(root, DEGRADED)) {
2724 free_extent_map(em);
2725 return 0;
2726 }
2727
Yan Zheng2b820322008-11-17 21:11:30 -05002728 map = (struct map_lookup *)em->bdev;
2729 for (i = 0; i < map->num_stripes; i++) {
2730 if (!map->stripes[i].dev->writeable) {
2731 readonly = 1;
2732 break;
2733 }
2734 }
2735 free_extent_map(em);
2736 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002737}
2738
2739void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2740{
David Sterbaa8067e02011-04-21 00:34:43 +02002741 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002742}
2743
2744void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2745{
2746 struct extent_map *em;
2747
Chris Masond3977122009-01-05 21:25:51 -05002748 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002749 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002750 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2751 if (em)
2752 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002753 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002754 if (!em)
2755 break;
2756 kfree(em->bdev);
2757 /* once for us */
2758 free_extent_map(em);
2759 /* once for the tree */
2760 free_extent_map(em);
2761 }
2762}
2763
Chris Masonf1885912008-04-09 16:28:12 -04002764int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2765{
2766 struct extent_map *em;
2767 struct map_lookup *map;
2768 struct extent_map_tree *em_tree = &map_tree->map_tree;
2769 int ret;
2770
Chris Mason890871b2009-09-02 16:24:52 -04002771 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002772 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002773 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002774 BUG_ON(!em);
2775
2776 BUG_ON(em->start > logical || em->start + em->len < logical);
2777 map = (struct map_lookup *)em->bdev;
2778 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2779 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002780 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2781 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002782 else
2783 ret = 1;
2784 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002785 return ret;
2786}
2787
Chris Masondfe25022008-05-13 13:46:40 -04002788static int find_live_mirror(struct map_lookup *map, int first, int num,
2789 int optimal)
2790{
2791 int i;
2792 if (map->stripes[optimal].dev->bdev)
2793 return optimal;
2794 for (i = first; i < first + num; i++) {
2795 if (map->stripes[i].dev->bdev)
2796 return i;
2797 }
2798 /* we couldn't find one that doesn't fail. Just return something
2799 * and the io error handling code will clean up eventually
2800 */
2801 return optimal;
2802}
2803
Chris Masonf2d8d742008-04-21 10:03:05 -04002804static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2805 u64 logical, u64 *length,
2806 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002807 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002808{
2809 struct extent_map *em;
2810 struct map_lookup *map;
2811 struct extent_map_tree *em_tree = &map_tree->map_tree;
2812 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002813 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002814 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002815 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002816 u64 stripe_nr_orig;
2817 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002818 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002819 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002820 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002821 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002822 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002823 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002824 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002825
Li Dongyangfce3bb92011-03-24 10:24:26 +00002826 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002827 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002828again:
2829 if (multi_ret) {
2830 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2831 GFP_NOFS);
2832 if (!multi)
2833 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002834
2835 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002836 }
Chris Mason0b86a832008-03-24 15:01:56 -04002837
Chris Mason890871b2009-09-02 16:24:52 -04002838 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002839 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002840 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002841
Chris Mason3b951512008-04-17 11:29:12 -04002842 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002843 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2844 (unsigned long long)logical,
2845 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002846 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002847 }
Chris Mason0b86a832008-03-24 15:01:56 -04002848
2849 BUG_ON(em->start > logical || em->start + em->len < logical);
2850 map = (struct map_lookup *)em->bdev;
2851 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002852
Chris Masonf1885912008-04-09 16:28:12 -04002853 if (mirror_num > map->num_stripes)
2854 mirror_num = 0;
2855
Chris Masoncea9e442008-04-09 16:28:12 -04002856 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002857 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002858 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2859 BTRFS_BLOCK_GROUP_DUP)) {
2860 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002861 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002862 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2863 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002864 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002865 }
2866 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002867 if (rw & REQ_DISCARD) {
2868 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2869 BTRFS_BLOCK_GROUP_RAID1 |
2870 BTRFS_BLOCK_GROUP_DUP |
2871 BTRFS_BLOCK_GROUP_RAID10)) {
2872 stripes_required = map->num_stripes;
2873 }
2874 }
2875 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002876 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002877 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002878 free_extent_map(em);
2879 kfree(multi);
2880 goto again;
2881 }
Chris Mason593060d2008-03-25 16:50:33 -04002882 stripe_nr = offset;
2883 /*
2884 * stripe_nr counts the total number of stripes we have to stride
2885 * to get to this block
2886 */
2887 do_div(stripe_nr, map->stripe_len);
2888
2889 stripe_offset = stripe_nr * map->stripe_len;
2890 BUG_ON(offset < stripe_offset);
2891
2892 /* stripe_offset is the offset of this block in its stripe*/
2893 stripe_offset = offset - stripe_offset;
2894
Li Dongyangfce3bb92011-03-24 10:24:26 +00002895 if (rw & REQ_DISCARD)
2896 *length = min_t(u64, em->len - offset, *length);
2897 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2898 BTRFS_BLOCK_GROUP_RAID1 |
2899 BTRFS_BLOCK_GROUP_RAID10 |
2900 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002901 /* we limit the length of each bio to what fits in a stripe */
2902 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002903 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002904 } else {
2905 *length = em->len - offset;
2906 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002907
Jens Axboe7eaceac2011-03-10 08:52:07 +01002908 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002909 goto out;
2910
Chris Masonf2d8d742008-04-21 10:03:05 -04002911 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002912 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002913 stripe_nr_orig = stripe_nr;
2914 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2915 (~(map->stripe_len - 1));
2916 do_div(stripe_nr_end, map->stripe_len);
2917 stripe_end_offset = stripe_nr_end * map->stripe_len -
2918 (offset + *length);
2919 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2920 if (rw & REQ_DISCARD)
2921 num_stripes = min_t(u64, map->num_stripes,
2922 stripe_nr_end - stripe_nr_orig);
2923 stripe_index = do_div(stripe_nr, map->num_stripes);
2924 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07002925 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002926 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002927 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002928 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002929 else {
2930 stripe_index = find_live_mirror(map, 0,
2931 map->num_stripes,
2932 current->pid % map->num_stripes);
2933 }
Chris Mason2fff7342008-04-29 14:12:09 -04002934
Chris Mason611f0e02008-04-03 16:29:03 -04002935 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00002936 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002937 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002938 else if (mirror_num)
2939 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002940
Chris Mason321aecc2008-04-16 10:49:51 -04002941 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2942 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002943
2944 stripe_index = do_div(stripe_nr, factor);
2945 stripe_index *= map->sub_stripes;
2946
Jens Axboe7eaceac2011-03-10 08:52:07 +01002947 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04002948 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002949 else if (rw & REQ_DISCARD)
2950 num_stripes = min_t(u64, map->sub_stripes *
2951 (stripe_nr_end - stripe_nr_orig),
2952 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04002953 else if (mirror_num)
2954 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002955 else {
2956 stripe_index = find_live_mirror(map, stripe_index,
2957 map->sub_stripes, stripe_index +
2958 current->pid % map->sub_stripes);
2959 }
Chris Mason8790d502008-04-03 16:29:03 -04002960 } else {
2961 /*
2962 * after this do_div call, stripe_nr is the number of stripes
2963 * on this device we have to walk to find the data, and
2964 * stripe_index is the number of our device in the stripe array
2965 */
2966 stripe_index = do_div(stripe_nr, map->num_stripes);
2967 }
Chris Mason593060d2008-03-25 16:50:33 -04002968 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002969
Li Dongyangfce3bb92011-03-24 10:24:26 +00002970 if (rw & REQ_DISCARD) {
2971 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002972 multi->stripes[i].physical =
2973 map->stripes[stripe_index].physical +
2974 stripe_offset + stripe_nr * map->stripe_len;
2975 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002976
2977 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2978 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04002979 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002980 int j;
2981
Chris Masond9d04872011-03-27 21:23:21 -04002982 div_u64_rem(stripe_nr_end - 1,
2983 map->num_stripes,
2984 &last_stripe);
2985
Li Dongyangfce3bb92011-03-24 10:24:26 +00002986 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04002987 u32 test;
2988
2989 div_u64_rem(stripe_nr_end - 1 - j,
2990 map->num_stripes, &test);
2991 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00002992 break;
2993 }
2994 stripes = stripe_nr_end - 1 - j;
2995 do_div(stripes, map->num_stripes);
2996 multi->stripes[i].length = map->stripe_len *
2997 (stripes - stripe_nr + 1);
2998
2999 if (i == 0) {
3000 multi->stripes[i].length -=
3001 stripe_offset;
3002 stripe_offset = 0;
3003 }
3004 if (stripe_index == last_stripe)
3005 multi->stripes[i].length -=
3006 stripe_end_offset;
3007 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3008 u64 stripes;
3009 int j;
3010 int factor = map->num_stripes /
3011 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003012 u32 last_stripe = 0;
3013
3014 div_u64_rem(stripe_nr_end - 1,
3015 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003016 last_stripe *= map->sub_stripes;
3017
3018 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003019 u32 test;
3020
3021 div_u64_rem(stripe_nr_end - 1 - j,
3022 factor, &test);
3023
3024 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003025 stripe_index / map->sub_stripes)
3026 break;
3027 }
3028 stripes = stripe_nr_end - 1 - j;
3029 do_div(stripes, factor);
3030 multi->stripes[i].length = map->stripe_len *
3031 (stripes - stripe_nr + 1);
3032
3033 if (i < map->sub_stripes) {
3034 multi->stripes[i].length -=
3035 stripe_offset;
3036 if (i == map->sub_stripes - 1)
3037 stripe_offset = 0;
3038 }
3039 if (stripe_index >= last_stripe &&
3040 stripe_index <= (last_stripe +
3041 map->sub_stripes - 1)) {
3042 multi->stripes[i].length -=
3043 stripe_end_offset;
3044 }
3045 } else
3046 multi->stripes[i].length = *length;
3047
3048 stripe_index++;
3049 if (stripe_index == map->num_stripes) {
3050 /* This could only happen for RAID0/10 */
3051 stripe_index = 0;
3052 stripe_nr++;
3053 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003054 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003055 } else {
3056 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003057 multi->stripes[i].physical =
3058 map->stripes[stripe_index].physical +
3059 stripe_offset +
3060 stripe_nr * map->stripe_len;
3061 multi->stripes[i].dev =
3062 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003063 stripe_index++;
3064 }
Chris Mason593060d2008-03-25 16:50:33 -04003065 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003066 if (multi_ret) {
3067 *multi_ret = multi;
3068 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003069 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003070 }
Chris Masoncea9e442008-04-09 16:28:12 -04003071out:
Chris Mason0b86a832008-03-24 15:01:56 -04003072 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003073 return 0;
3074}
3075
Chris Masonf2d8d742008-04-21 10:03:05 -04003076int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3077 u64 logical, u64 *length,
3078 struct btrfs_multi_bio **multi_ret, int mirror_num)
3079{
3080 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003081 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003082}
3083
Yan Zhenga512bbf2008-12-08 16:46:26 -05003084int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3085 u64 chunk_start, u64 physical, u64 devid,
3086 u64 **logical, int *naddrs, int *stripe_len)
3087{
3088 struct extent_map_tree *em_tree = &map_tree->map_tree;
3089 struct extent_map *em;
3090 struct map_lookup *map;
3091 u64 *buf;
3092 u64 bytenr;
3093 u64 length;
3094 u64 stripe_nr;
3095 int i, j, nr = 0;
3096
Chris Mason890871b2009-09-02 16:24:52 -04003097 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003098 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003099 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003100
3101 BUG_ON(!em || em->start != chunk_start);
3102 map = (struct map_lookup *)em->bdev;
3103
3104 length = em->len;
3105 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3106 do_div(length, map->num_stripes / map->sub_stripes);
3107 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3108 do_div(length, map->num_stripes);
3109
3110 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3111 BUG_ON(!buf);
3112
3113 for (i = 0; i < map->num_stripes; i++) {
3114 if (devid && map->stripes[i].dev->devid != devid)
3115 continue;
3116 if (map->stripes[i].physical > physical ||
3117 map->stripes[i].physical + length <= physical)
3118 continue;
3119
3120 stripe_nr = physical - map->stripes[i].physical;
3121 do_div(stripe_nr, map->stripe_len);
3122
3123 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3124 stripe_nr = stripe_nr * map->num_stripes + i;
3125 do_div(stripe_nr, map->sub_stripes);
3126 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3127 stripe_nr = stripe_nr * map->num_stripes + i;
3128 }
3129 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003130 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003131 for (j = 0; j < nr; j++) {
3132 if (buf[j] == bytenr)
3133 break;
3134 }
Chris Mason934d3752008-12-08 16:43:10 -05003135 if (j == nr) {
3136 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003137 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003138 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003139 }
3140
Yan Zhenga512bbf2008-12-08 16:46:26 -05003141 *logical = buf;
3142 *naddrs = nr;
3143 *stripe_len = map->stripe_len;
3144
3145 free_extent_map(em);
3146 return 0;
3147}
3148
Chris Mason8790d502008-04-03 16:29:03 -04003149static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003150{
Chris Masoncea9e442008-04-09 16:28:12 -04003151 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003152 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003153
Chris Mason8790d502008-04-03 16:29:03 -04003154 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003155 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003156
Chris Mason7d2b4da2008-08-05 10:13:57 -04003157 if (bio == multi->orig_bio)
3158 is_orig_bio = 1;
3159
Chris Masoncea9e442008-04-09 16:28:12 -04003160 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003161 if (!is_orig_bio) {
3162 bio_put(bio);
3163 bio = multi->orig_bio;
3164 }
Chris Mason8790d502008-04-03 16:29:03 -04003165 bio->bi_private = multi->private;
3166 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003167 /* only send an error to the higher layers if it is
3168 * beyond the tolerance of the multi-bio
3169 */
Chris Mason1259ab72008-05-12 13:39:03 -04003170 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003171 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003172 } else if (err) {
3173 /*
3174 * this bio is actually up to date, we didn't
3175 * go over the max number of errors
3176 */
3177 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003178 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003179 }
Chris Mason8790d502008-04-03 16:29:03 -04003180 kfree(multi);
3181
3182 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003183 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003184 bio_put(bio);
3185 }
Chris Mason8790d502008-04-03 16:29:03 -04003186}
3187
Chris Mason8b712842008-06-11 16:50:36 -04003188struct async_sched {
3189 struct bio *bio;
3190 int rw;
3191 struct btrfs_fs_info *info;
3192 struct btrfs_work work;
3193};
3194
3195/*
3196 * see run_scheduled_bios for a description of why bios are collected for
3197 * async submit.
3198 *
3199 * This will add one bio to the pending list for a device and make sure
3200 * the work struct is scheduled.
3201 */
Chris Masond3977122009-01-05 21:25:51 -05003202static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003203 struct btrfs_device *device,
3204 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003205{
3206 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003207 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003208
3209 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003210 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04003211 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003212 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04003213 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003214 return 0;
3215 }
3216
3217 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003218 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003219 * higher layers. Otherwise, the async bio makes it appear we have
3220 * made progress against dirty pages when we've really just put it
3221 * on a queue for later
3222 */
Chris Mason0986fe92008-08-15 15:34:15 -04003223 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04003224 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003225 bio->bi_next = NULL;
3226 bio->bi_rw |= rw;
3227
3228 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003229 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003230 pending_bios = &device->pending_sync_bios;
3231 else
3232 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003233
Chris Masonffbd5172009-04-20 15:50:09 -04003234 if (pending_bios->tail)
3235 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003236
Chris Masonffbd5172009-04-20 15:50:09 -04003237 pending_bios->tail = bio;
3238 if (!pending_bios->head)
3239 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003240 if (device->running_pending)
3241 should_queue = 0;
3242
3243 spin_unlock(&device->io_lock);
3244
3245 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003246 btrfs_queue_worker(&root->fs_info->submit_workers,
3247 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003248 return 0;
3249}
3250
Chris Masonf1885912008-04-09 16:28:12 -04003251int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003252 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003253{
3254 struct btrfs_mapping_tree *map_tree;
3255 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003256 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003257 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003258 u64 length = 0;
3259 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003260 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003261 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003262 int dev_nr = 0;
3263 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003264
Chris Masonf2d8d742008-04-21 10:03:05 -04003265 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003266 map_tree = &root->fs_info->mapping_tree;
3267 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003268
Chris Masonf1885912008-04-09 16:28:12 -04003269 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3270 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003271 BUG_ON(ret);
3272
3273 total_devs = multi->num_stripes;
3274 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003275 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3276 "len %llu\n", (unsigned long long)logical,
3277 (unsigned long long)length,
3278 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003279 BUG();
3280 }
3281 multi->end_io = first_bio->bi_end_io;
3282 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003283 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003284 atomic_set(&multi->stripes_pending, multi->num_stripes);
3285
Chris Masond3977122009-01-05 21:25:51 -05003286 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003287 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003288 if (dev_nr < total_devs - 1) {
3289 bio = bio_clone(first_bio, GFP_NOFS);
3290 BUG_ON(!bio);
3291 } else {
3292 bio = first_bio;
3293 }
3294 bio->bi_private = multi;
3295 bio->bi_end_io = end_bio_multi_stripe;
3296 }
Chris Masoncea9e442008-04-09 16:28:12 -04003297 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3298 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003299 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003300 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003301 if (async_submit)
3302 schedule_bio(root, dev, rw, bio);
3303 else
3304 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003305 } else {
3306 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3307 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003308 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003309 }
Chris Mason8790d502008-04-03 16:29:03 -04003310 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003311 }
Chris Masoncea9e442008-04-09 16:28:12 -04003312 if (total_devs == 1)
3313 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003314 return 0;
3315}
3316
Chris Masona4437552008-04-18 10:29:38 -04003317struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003318 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003319{
Yan Zheng2b820322008-11-17 21:11:30 -05003320 struct btrfs_device *device;
3321 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003322
Yan Zheng2b820322008-11-17 21:11:30 -05003323 cur_devices = root->fs_info->fs_devices;
3324 while (cur_devices) {
3325 if (!fsid ||
3326 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3327 device = __find_device(&cur_devices->devices,
3328 devid, uuid);
3329 if (device)
3330 return device;
3331 }
3332 cur_devices = cur_devices->seed;
3333 }
3334 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003335}
3336
Chris Masondfe25022008-05-13 13:46:40 -04003337static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3338 u64 devid, u8 *dev_uuid)
3339{
3340 struct btrfs_device *device;
3341 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3342
3343 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003344 if (!device)
3345 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003346 list_add(&device->dev_list,
3347 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003348 device->dev_root = root->fs_info->dev_root;
3349 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003350 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003351 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003352 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003353 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003354 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003355 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003356 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003357 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3358 return device;
3359}
3360
Chris Mason0b86a832008-03-24 15:01:56 -04003361static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3362 struct extent_buffer *leaf,
3363 struct btrfs_chunk *chunk)
3364{
3365 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3366 struct map_lookup *map;
3367 struct extent_map *em;
3368 u64 logical;
3369 u64 length;
3370 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003371 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003372 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003373 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003374 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003375
Chris Masone17cade2008-04-15 15:41:47 -04003376 logical = key->offset;
3377 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003378
Chris Mason890871b2009-09-02 16:24:52 -04003379 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003380 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003381 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003382
3383 /* already mapped? */
3384 if (em && em->start <= logical && em->start + em->len > logical) {
3385 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003386 return 0;
3387 } else if (em) {
3388 free_extent_map(em);
3389 }
Chris Mason0b86a832008-03-24 15:01:56 -04003390
David Sterba172ddd62011-04-21 00:48:27 +02003391 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003392 if (!em)
3393 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003394 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3395 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003396 if (!map) {
3397 free_extent_map(em);
3398 return -ENOMEM;
3399 }
3400
3401 em->bdev = (struct block_device *)map;
3402 em->start = logical;
3403 em->len = length;
3404 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003405 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003406
Chris Mason593060d2008-03-25 16:50:33 -04003407 map->num_stripes = num_stripes;
3408 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3409 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3410 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3411 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3412 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003413 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003414 for (i = 0; i < num_stripes; i++) {
3415 map->stripes[i].physical =
3416 btrfs_stripe_offset_nr(leaf, chunk, i);
3417 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003418 read_extent_buffer(leaf, uuid, (unsigned long)
3419 btrfs_stripe_dev_uuid_nr(chunk, i),
3420 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003421 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3422 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003423 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003424 kfree(map);
3425 free_extent_map(em);
3426 return -EIO;
3427 }
Chris Masondfe25022008-05-13 13:46:40 -04003428 if (!map->stripes[i].dev) {
3429 map->stripes[i].dev =
3430 add_missing_dev(root, devid, uuid);
3431 if (!map->stripes[i].dev) {
3432 kfree(map);
3433 free_extent_map(em);
3434 return -EIO;
3435 }
3436 }
3437 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003438 }
3439
Chris Mason890871b2009-09-02 16:24:52 -04003440 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003441 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003442 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003443 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003444 free_extent_map(em);
3445
3446 return 0;
3447}
3448
3449static int fill_device_from_item(struct extent_buffer *leaf,
3450 struct btrfs_dev_item *dev_item,
3451 struct btrfs_device *device)
3452{
3453 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003454
3455 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003456 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3457 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003458 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3459 device->type = btrfs_device_type(leaf, dev_item);
3460 device->io_align = btrfs_device_io_align(leaf, dev_item);
3461 device->io_width = btrfs_device_io_width(leaf, dev_item);
3462 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003463
3464 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003465 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003466
Chris Mason0b86a832008-03-24 15:01:56 -04003467 return 0;
3468}
3469
Yan Zheng2b820322008-11-17 21:11:30 -05003470static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3471{
3472 struct btrfs_fs_devices *fs_devices;
3473 int ret;
3474
3475 mutex_lock(&uuid_mutex);
3476
3477 fs_devices = root->fs_info->fs_devices->seed;
3478 while (fs_devices) {
3479 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3480 ret = 0;
3481 goto out;
3482 }
3483 fs_devices = fs_devices->seed;
3484 }
3485
3486 fs_devices = find_fsid(fsid);
3487 if (!fs_devices) {
3488 ret = -ENOENT;
3489 goto out;
3490 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003491
3492 fs_devices = clone_fs_devices(fs_devices);
3493 if (IS_ERR(fs_devices)) {
3494 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003495 goto out;
3496 }
3497
Christoph Hellwig97288f22008-12-02 06:36:09 -05003498 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003499 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003500 if (ret)
3501 goto out;
3502
3503 if (!fs_devices->seeding) {
3504 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003505 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003506 ret = -EINVAL;
3507 goto out;
3508 }
3509
3510 fs_devices->seed = root->fs_info->fs_devices->seed;
3511 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003512out:
3513 mutex_unlock(&uuid_mutex);
3514 return ret;
3515}
3516
Chris Mason0d81ba52008-03-24 15:02:07 -04003517static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003518 struct extent_buffer *leaf,
3519 struct btrfs_dev_item *dev_item)
3520{
3521 struct btrfs_device *device;
3522 u64 devid;
3523 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003524 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003525 u8 dev_uuid[BTRFS_UUID_SIZE];
3526
Chris Mason0b86a832008-03-24 15:01:56 -04003527 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003528 read_extent_buffer(leaf, dev_uuid,
3529 (unsigned long)btrfs_device_uuid(dev_item),
3530 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003531 read_extent_buffer(leaf, fs_uuid,
3532 (unsigned long)btrfs_device_fsid(dev_item),
3533 BTRFS_UUID_SIZE);
3534
3535 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3536 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003537 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003538 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003539 }
3540
3541 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3542 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003543 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003544 return -EIO;
3545
3546 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003547 printk(KERN_WARNING "warning devid %llu missing\n",
3548 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003549 device = add_missing_dev(root, devid, dev_uuid);
3550 if (!device)
3551 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003552 } else if (!device->missing) {
3553 /*
3554 * this happens when a device that was properly setup
3555 * in the device info lists suddenly goes bad.
3556 * device->bdev is NULL, and so we have to set
3557 * device->missing to one here
3558 */
3559 root->fs_info->fs_devices->missing_devices++;
3560 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003561 }
3562 }
3563
3564 if (device->fs_devices != root->fs_info->fs_devices) {
3565 BUG_ON(device->writeable);
3566 if (device->generation !=
3567 btrfs_device_generation(leaf, dev_item))
3568 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003569 }
Chris Mason0b86a832008-03-24 15:01:56 -04003570
3571 fill_device_from_item(leaf, dev_item, device);
3572 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003573 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003574 if (device->writeable)
3575 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003576 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003577 return ret;
3578}
3579
Yan Zhenge4404d62008-12-12 10:03:26 -05003580int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003581{
3582 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003583 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003584 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003585 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003586 u8 *ptr;
3587 unsigned long sb_ptr;
3588 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003589 u32 num_stripes;
3590 u32 array_size;
3591 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003592 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003593 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003594
Yan Zhenge4404d62008-12-12 10:03:26 -05003595 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003596 BTRFS_SUPER_INFO_SIZE);
3597 if (!sb)
3598 return -ENOMEM;
3599 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003600 btrfs_set_buffer_lockdep_class(sb, 0);
3601
Chris Masona061fc82008-05-07 11:43:44 -04003602 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003603 array_size = btrfs_super_sys_array_size(super_copy);
3604
Chris Mason0b86a832008-03-24 15:01:56 -04003605 ptr = super_copy->sys_chunk_array;
3606 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3607 cur = 0;
3608
3609 while (cur < array_size) {
3610 disk_key = (struct btrfs_disk_key *)ptr;
3611 btrfs_disk_key_to_cpu(&key, disk_key);
3612
Chris Masona061fc82008-05-07 11:43:44 -04003613 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003614 sb_ptr += len;
3615 cur += len;
3616
Chris Mason0d81ba52008-03-24 15:02:07 -04003617 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003618 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003619 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003620 if (ret)
3621 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003622 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3623 len = btrfs_chunk_item_size(num_stripes);
3624 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003625 ret = -EIO;
3626 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003627 }
3628 ptr += len;
3629 sb_ptr += len;
3630 cur += len;
3631 }
Chris Masona061fc82008-05-07 11:43:44 -04003632 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003633 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003634}
3635
3636int btrfs_read_chunk_tree(struct btrfs_root *root)
3637{
3638 struct btrfs_path *path;
3639 struct extent_buffer *leaf;
3640 struct btrfs_key key;
3641 struct btrfs_key found_key;
3642 int ret;
3643 int slot;
3644
3645 root = root->fs_info->chunk_root;
3646
3647 path = btrfs_alloc_path();
3648 if (!path)
3649 return -ENOMEM;
3650
3651 /* first we search for all of the device items, and then we
3652 * read in all of the chunk items. This way we can create chunk
3653 * mappings that reference all of the devices that are afound
3654 */
3655 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3656 key.offset = 0;
3657 key.type = 0;
3658again:
3659 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003660 if (ret < 0)
3661 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003662 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003663 leaf = path->nodes[0];
3664 slot = path->slots[0];
3665 if (slot >= btrfs_header_nritems(leaf)) {
3666 ret = btrfs_next_leaf(root, path);
3667 if (ret == 0)
3668 continue;
3669 if (ret < 0)
3670 goto error;
3671 break;
3672 }
3673 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3674 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3675 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3676 break;
3677 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3678 struct btrfs_dev_item *dev_item;
3679 dev_item = btrfs_item_ptr(leaf, slot,
3680 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003681 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003682 if (ret)
3683 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003684 }
3685 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3686 struct btrfs_chunk *chunk;
3687 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3688 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003689 if (ret)
3690 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003691 }
3692 path->slots[0]++;
3693 }
3694 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3695 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003696 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003697 goto again;
3698 }
Chris Mason0b86a832008-03-24 15:01:56 -04003699 ret = 0;
3700error:
Yan Zheng2b820322008-11-17 21:11:30 -05003701 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003702 return ret;
3703}