blob: 43baaf0c6749d9d03a61863f5f233c2b5a1b90cc [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
Josef Bacika2ea1862011-08-04 14:52:27 +0000503 if (device->can_discard)
504 fs_devices->num_can_discard--;
505
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
507 BUG_ON(!new_device);
508 memcpy(new_device, device, sizeof(*new_device));
509 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000510 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000511 new_device->bdev = NULL;
512 new_device->writeable = 0;
513 new_device->in_fs_metadata = 0;
Josef Bacika2ea1862011-08-04 14:52:27 +0000514 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000515 list_replace_rcu(&device->dev_list, &new_device->dev_list);
516
517 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400518 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000519 mutex_unlock(&fs_devices->device_list_mutex);
520
Yan Zhenge4404d62008-12-12 10:03:26 -0500521 WARN_ON(fs_devices->open_devices);
522 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500523 fs_devices->opened = 0;
524 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500525
Chris Mason8a4b83c2008-03-24 15:02:07 -0400526 return 0;
527}
528
Yan Zheng2b820322008-11-17 21:11:30 -0500529int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
530{
Yan Zhenge4404d62008-12-12 10:03:26 -0500531 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500532 int ret;
533
534 mutex_lock(&uuid_mutex);
535 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500536 if (!fs_devices->opened) {
537 seed_devices = fs_devices->seed;
538 fs_devices->seed = NULL;
539 }
Yan Zheng2b820322008-11-17 21:11:30 -0500540 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500541
542 while (seed_devices) {
543 fs_devices = seed_devices;
544 seed_devices = fs_devices->seed;
545 __btrfs_close_devices(fs_devices);
546 free_fs_devices(fs_devices);
547 }
Yan Zheng2b820322008-11-17 21:11:30 -0500548 return ret;
549}
550
Yan Zhenge4404d62008-12-12 10:03:26 -0500551static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
552 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400553{
Josef Bacika2ea1862011-08-04 14:52:27 +0000554 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400555 struct block_device *bdev;
556 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400557 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400558 struct block_device *latest_bdev = NULL;
559 struct buffer_head *bh;
560 struct btrfs_super_block *disk_super;
561 u64 latest_devid = 0;
562 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400563 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500564 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400565 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400566
Tejun Heod4d77622010-11-13 11:55:18 +0100567 flags |= FMODE_EXCL;
568
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500569 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400570 if (device->bdev)
571 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400572 if (!device->name)
573 continue;
574
Tejun Heod4d77622010-11-13 11:55:18 +0100575 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400576 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500577 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400578 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400579 }
Chris Masona061fc82008-05-07 11:43:44 -0400580 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400581
Yan Zhenga512bbf2008-12-08 16:46:26 -0500582 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000583 if (!bh) {
584 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400585 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000586 }
Chris Masona0af4692008-05-13 16:03:06 -0400587
588 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000589 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400590 if (devid != device->devid)
591 goto error_brelse;
592
Yan Zheng2b820322008-11-17 21:11:30 -0500593 if (memcmp(device->uuid, disk_super->dev_item.uuid,
594 BTRFS_UUID_SIZE))
595 goto error_brelse;
596
597 device->generation = btrfs_super_generation(disk_super);
598 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400599 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500600 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400601 latest_bdev = bdev;
602 }
603
Yan Zheng2b820322008-11-17 21:11:30 -0500604 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
605 device->writeable = 0;
606 } else {
607 device->writeable = !bdev_read_only(bdev);
608 seeding = 0;
609 }
610
Josef Bacika2ea1862011-08-04 14:52:27 +0000611 q = bdev_get_queue(bdev);
612 if (blk_queue_discard(q)) {
613 device->can_discard = 1;
614 fs_devices->num_can_discard++;
615 }
616
Chris Mason8a4b83c2008-03-24 15:02:07 -0400617 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400618 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500619 device->mode = flags;
620
Chris Masonc2898112009-06-10 09:51:32 -0400621 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
622 fs_devices->rotating = 1;
623
Chris Masona0af4692008-05-13 16:03:06 -0400624 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500625 if (device->writeable) {
626 fs_devices->rw_devices++;
627 list_add(&device->dev_alloc_list,
628 &fs_devices->alloc_list);
629 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000630 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400631 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400632
Chris Masona0af4692008-05-13 16:03:06 -0400633error_brelse:
634 brelse(bh);
635error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100636 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400637error:
638 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400639 }
Chris Masona0af4692008-05-13 16:03:06 -0400640 if (fs_devices->open_devices == 0) {
641 ret = -EIO;
642 goto out;
643 }
Yan Zheng2b820322008-11-17 21:11:30 -0500644 fs_devices->seeding = seeding;
645 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400646 fs_devices->latest_bdev = latest_bdev;
647 fs_devices->latest_devid = latest_devid;
648 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500649 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400650out:
Yan Zheng2b820322008-11-17 21:11:30 -0500651 return ret;
652}
653
654int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500655 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500656{
657 int ret;
658
659 mutex_lock(&uuid_mutex);
660 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500661 fs_devices->opened++;
662 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500663 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500664 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500665 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400666 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400667 return ret;
668}
669
Christoph Hellwig97288f22008-12-02 06:36:09 -0500670int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400671 struct btrfs_fs_devices **fs_devices_ret)
672{
673 struct btrfs_super_block *disk_super;
674 struct block_device *bdev;
675 struct buffer_head *bh;
676 int ret;
677 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400678 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400679
680 mutex_lock(&uuid_mutex);
681
Tejun Heod4d77622010-11-13 11:55:18 +0100682 flags |= FMODE_EXCL;
683 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400684
685 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400686 ret = PTR_ERR(bdev);
687 goto error;
688 }
689
690 ret = set_blocksize(bdev, 4096);
691 if (ret)
692 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500693 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400694 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000695 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 goto error_close;
697 }
698 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000699 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400700 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400701 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500702 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200703 else
704 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500705 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500706 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400707 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
708
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709 brelse(bh);
710error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100711 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712error:
713 mutex_unlock(&uuid_mutex);
714 return ret;
715}
Chris Mason0b86a832008-03-24 15:01:56 -0400716
Miao Xie6d07bce2011-01-05 10:07:31 +0000717/* helper to account the used device space in the range */
718int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
719 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400720{
721 struct btrfs_key key;
722 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000723 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500724 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000725 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400726 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000727 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400728 struct extent_buffer *l;
729
Miao Xie6d07bce2011-01-05 10:07:31 +0000730 *length = 0;
731
732 if (start >= device->total_bytes)
733 return 0;
734
Yan Zheng2b820322008-11-17 21:11:30 -0500735 path = btrfs_alloc_path();
736 if (!path)
737 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400738 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400739
Chris Mason0b86a832008-03-24 15:01:56 -0400740 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000741 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400742 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000743
744 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400745 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000746 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400747 if (ret > 0) {
748 ret = btrfs_previous_item(root, path, key.objectid, key.type);
749 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000750 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400751 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000752
Chris Mason0b86a832008-03-24 15:01:56 -0400753 while (1) {
754 l = path->nodes[0];
755 slot = path->slots[0];
756 if (slot >= btrfs_header_nritems(l)) {
757 ret = btrfs_next_leaf(root, path);
758 if (ret == 0)
759 continue;
760 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000761 goto out;
762
763 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400764 }
765 btrfs_item_key_to_cpu(l, &key, slot);
766
767 if (key.objectid < device->devid)
768 goto next;
769
770 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000771 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400772
Chris Masond3977122009-01-05 21:25:51 -0500773 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400774 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400775
Chris Mason0b86a832008-03-24 15:01:56 -0400776 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000777 extent_end = key.offset + btrfs_dev_extent_length(l,
778 dev_extent);
779 if (key.offset <= start && extent_end > end) {
780 *length = end - start + 1;
781 break;
782 } else if (key.offset <= start && extent_end > start)
783 *length += extent_end - start;
784 else if (key.offset > start && extent_end <= end)
785 *length += extent_end - key.offset;
786 else if (key.offset > start && key.offset <= end) {
787 *length += end - key.offset + 1;
788 break;
789 } else if (key.offset > end)
790 break;
791
792next:
793 path->slots[0]++;
794 }
795 ret = 0;
796out:
797 btrfs_free_path(path);
798 return ret;
799}
800
Chris Mason0b86a832008-03-24 15:01:56 -0400801/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000802 * find_free_dev_extent - find free space in the specified device
803 * @trans: transaction handler
804 * @device: the device which we search the free space in
805 * @num_bytes: the size of the free space that we need
806 * @start: store the start of the free space.
807 * @len: the size of the free space. that we find, or the size of the max
808 * free space if we don't find suitable free space
809 *
Chris Mason0b86a832008-03-24 15:01:56 -0400810 * this uses a pretty simple search, the expectation is that it is
811 * called very infrequently and that a given device has a small number
812 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000813 *
814 * @start is used to store the start of the free space if we find. But if we
815 * don't find suitable free space, it will be used to store the start position
816 * of the max free space.
817 *
818 * @len is used to store the size of the free space that we find.
819 * But if we don't find suitable free space, it is used to store the size of
820 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400821 */
822int find_free_dev_extent(struct btrfs_trans_handle *trans,
823 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000824 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400825{
826 struct btrfs_key key;
827 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000828 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400829 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000830 u64 hole_size;
831 u64 max_hole_start;
832 u64 max_hole_size;
833 u64 extent_end;
834 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400835 u64 search_end = device->total_bytes;
836 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000837 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400838 struct extent_buffer *l;
839
Chris Mason0b86a832008-03-24 15:01:56 -0400840 /* FIXME use last free of some kind */
841
842 /* we don't want to overwrite the superblock on the drive,
843 * so we make sure to start at an offset of at least 1MB
844 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200845 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400846
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 max_hole_start = search_start;
848 max_hole_size = 0;
849
850 if (search_start >= search_end) {
851 ret = -ENOSPC;
852 goto error;
853 }
854
855 path = btrfs_alloc_path();
856 if (!path) {
857 ret = -ENOMEM;
858 goto error;
859 }
860 path->reada = 2;
861
Chris Mason0b86a832008-03-24 15:01:56 -0400862 key.objectid = device->devid;
863 key.offset = search_start;
864 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000865
Chris Mason0b86a832008-03-24 15:01:56 -0400866 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
867 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000868 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400869 if (ret > 0) {
870 ret = btrfs_previous_item(root, path, key.objectid, key.type);
871 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000872 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400873 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000874
Chris Mason0b86a832008-03-24 15:01:56 -0400875 while (1) {
876 l = path->nodes[0];
877 slot = path->slots[0];
878 if (slot >= btrfs_header_nritems(l)) {
879 ret = btrfs_next_leaf(root, path);
880 if (ret == 0)
881 continue;
882 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000883 goto out;
884
885 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400886 }
887 btrfs_item_key_to_cpu(l, &key, slot);
888
889 if (key.objectid < device->devid)
890 goto next;
891
892 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000893 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400894
Chris Mason0b86a832008-03-24 15:01:56 -0400895 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
896 goto next;
897
Miao Xie7bfc8372011-01-05 10:07:26 +0000898 if (key.offset > search_start) {
899 hole_size = key.offset - search_start;
900
901 if (hole_size > max_hole_size) {
902 max_hole_start = search_start;
903 max_hole_size = hole_size;
904 }
905
906 /*
907 * If this free space is greater than which we need,
908 * it must be the max free space that we have found
909 * until now, so max_hole_start must point to the start
910 * of this free space and the length of this free space
911 * is stored in max_hole_size. Thus, we return
912 * max_hole_start and max_hole_size and go back to the
913 * caller.
914 */
915 if (hole_size >= num_bytes) {
916 ret = 0;
917 goto out;
918 }
919 }
920
Chris Mason0b86a832008-03-24 15:01:56 -0400921 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000922 extent_end = key.offset + btrfs_dev_extent_length(l,
923 dev_extent);
924 if (extent_end > search_start)
925 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400926next:
927 path->slots[0]++;
928 cond_resched();
929 }
Chris Mason0b86a832008-03-24 15:01:56 -0400930
Miao Xie7bfc8372011-01-05 10:07:26 +0000931 hole_size = search_end- search_start;
932 if (hole_size > max_hole_size) {
933 max_hole_start = search_start;
934 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400935 }
Chris Mason0b86a832008-03-24 15:01:56 -0400936
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 /* See above. */
938 if (hole_size < num_bytes)
939 ret = -ENOSPC;
940 else
941 ret = 0;
942
943out:
Yan Zheng2b820322008-11-17 21:11:30 -0500944 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000945error:
946 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000947 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000948 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400949 return ret;
950}
951
Christoph Hellwigb2950862008-12-02 09:54:17 -0500952static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400953 struct btrfs_device *device,
954 u64 start)
955{
956 int ret;
957 struct btrfs_path *path;
958 struct btrfs_root *root = device->dev_root;
959 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400960 struct btrfs_key found_key;
961 struct extent_buffer *leaf = NULL;
962 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400963
964 path = btrfs_alloc_path();
965 if (!path)
966 return -ENOMEM;
967
968 key.objectid = device->devid;
969 key.offset = start;
970 key.type = BTRFS_DEV_EXTENT_KEY;
971
972 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400973 if (ret > 0) {
974 ret = btrfs_previous_item(root, path, key.objectid,
975 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000976 if (ret)
977 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400978 leaf = path->nodes[0];
979 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
980 extent = btrfs_item_ptr(leaf, path->slots[0],
981 struct btrfs_dev_extent);
982 BUG_ON(found_key.offset > start || found_key.offset +
983 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400984 } else if (ret == 0) {
985 leaf = path->nodes[0];
986 extent = btrfs_item_ptr(leaf, path->slots[0],
987 struct btrfs_dev_extent);
988 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400989 BUG_ON(ret);
990
Chris Masondfe25022008-05-13 13:46:40 -0400991 if (device->bytes_used > 0)
992 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400993 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -0400994
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000995out:
Chris Mason8f18cf12008-04-25 16:53:30 -0400996 btrfs_free_path(path);
997 return ret;
998}
999
Yan Zheng2b820322008-11-17 21:11:30 -05001000int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001001 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001002 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001003 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001004{
1005 int ret;
1006 struct btrfs_path *path;
1007 struct btrfs_root *root = device->dev_root;
1008 struct btrfs_dev_extent *extent;
1009 struct extent_buffer *leaf;
1010 struct btrfs_key key;
1011
Chris Masondfe25022008-05-13 13:46:40 -04001012 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001013 path = btrfs_alloc_path();
1014 if (!path)
1015 return -ENOMEM;
1016
Chris Mason0b86a832008-03-24 15:01:56 -04001017 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001018 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001019 key.type = BTRFS_DEV_EXTENT_KEY;
1020 ret = btrfs_insert_empty_item(trans, root, path, &key,
1021 sizeof(*extent));
1022 BUG_ON(ret);
1023
1024 leaf = path->nodes[0];
1025 extent = btrfs_item_ptr(leaf, path->slots[0],
1026 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001027 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1028 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1029 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1030
1031 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1032 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1033 BTRFS_UUID_SIZE);
1034
Chris Mason0b86a832008-03-24 15:01:56 -04001035 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1036 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001037 btrfs_free_path(path);
1038 return ret;
1039}
1040
Chris Masona1b32a52008-09-05 16:09:51 -04001041static noinline int find_next_chunk(struct btrfs_root *root,
1042 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001043{
1044 struct btrfs_path *path;
1045 int ret;
1046 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001047 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001048 struct btrfs_key found_key;
1049
1050 path = btrfs_alloc_path();
1051 BUG_ON(!path);
1052
Chris Masone17cade2008-04-15 15:41:47 -04001053 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001054 key.offset = (u64)-1;
1055 key.type = BTRFS_CHUNK_ITEM_KEY;
1056
1057 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1058 if (ret < 0)
1059 goto error;
1060
1061 BUG_ON(ret == 0);
1062
1063 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1064 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001065 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001066 } else {
1067 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1068 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001069 if (found_key.objectid != objectid)
1070 *offset = 0;
1071 else {
1072 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1073 struct btrfs_chunk);
1074 *offset = found_key.offset +
1075 btrfs_chunk_length(path->nodes[0], chunk);
1076 }
Chris Mason0b86a832008-03-24 15:01:56 -04001077 }
1078 ret = 0;
1079error:
1080 btrfs_free_path(path);
1081 return ret;
1082}
1083
Yan Zheng2b820322008-11-17 21:11:30 -05001084static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001085{
1086 int ret;
1087 struct btrfs_key key;
1088 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001089 struct btrfs_path *path;
1090
1091 root = root->fs_info->chunk_root;
1092
1093 path = btrfs_alloc_path();
1094 if (!path)
1095 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001096
1097 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1098 key.type = BTRFS_DEV_ITEM_KEY;
1099 key.offset = (u64)-1;
1100
1101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1102 if (ret < 0)
1103 goto error;
1104
1105 BUG_ON(ret == 0);
1106
1107 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1108 BTRFS_DEV_ITEM_KEY);
1109 if (ret) {
1110 *objectid = 1;
1111 } else {
1112 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1113 path->slots[0]);
1114 *objectid = found_key.offset + 1;
1115 }
1116 ret = 0;
1117error:
Yan Zheng2b820322008-11-17 21:11:30 -05001118 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001119 return ret;
1120}
1121
1122/*
1123 * the device information is stored in the chunk root
1124 * the btrfs_device struct should be fully filled in
1125 */
1126int btrfs_add_device(struct btrfs_trans_handle *trans,
1127 struct btrfs_root *root,
1128 struct btrfs_device *device)
1129{
1130 int ret;
1131 struct btrfs_path *path;
1132 struct btrfs_dev_item *dev_item;
1133 struct extent_buffer *leaf;
1134 struct btrfs_key key;
1135 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001136
1137 root = root->fs_info->chunk_root;
1138
1139 path = btrfs_alloc_path();
1140 if (!path)
1141 return -ENOMEM;
1142
Chris Mason0b86a832008-03-24 15:01:56 -04001143 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1144 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001145 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001146
1147 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001148 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001149 if (ret)
1150 goto out;
1151
1152 leaf = path->nodes[0];
1153 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1154
1155 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001156 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001157 btrfs_set_device_type(leaf, dev_item, device->type);
1158 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1159 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1160 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001161 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1162 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001163 btrfs_set_device_group(leaf, dev_item, 0);
1164 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1165 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001166 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001167
Chris Mason0b86a832008-03-24 15:01:56 -04001168 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001169 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001170 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1171 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001172 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001173
Yan Zheng2b820322008-11-17 21:11:30 -05001174 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001175out:
1176 btrfs_free_path(path);
1177 return ret;
1178}
Chris Mason8f18cf12008-04-25 16:53:30 -04001179
Chris Masona061fc82008-05-07 11:43:44 -04001180static int btrfs_rm_dev_item(struct btrfs_root *root,
1181 struct btrfs_device *device)
1182{
1183 int ret;
1184 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001185 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001186 struct btrfs_trans_handle *trans;
1187
1188 root = root->fs_info->chunk_root;
1189
1190 path = btrfs_alloc_path();
1191 if (!path)
1192 return -ENOMEM;
1193
Yan, Zhenga22285a2010-05-16 10:48:46 -04001194 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001195 if (IS_ERR(trans)) {
1196 btrfs_free_path(path);
1197 return PTR_ERR(trans);
1198 }
Chris Masona061fc82008-05-07 11:43:44 -04001199 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1200 key.type = BTRFS_DEV_ITEM_KEY;
1201 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001202 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001203
1204 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1205 if (ret < 0)
1206 goto out;
1207
1208 if (ret > 0) {
1209 ret = -ENOENT;
1210 goto out;
1211 }
1212
1213 ret = btrfs_del_item(trans, root, path);
1214 if (ret)
1215 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001216out:
1217 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001218 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001219 btrfs_commit_transaction(trans, root);
1220 return ret;
1221}
1222
1223int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1224{
1225 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001226 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001227 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001228 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001229 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001230 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001231 u64 all_avail;
1232 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001233 u64 num_devices;
1234 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001235 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001236 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001237
Chris Masona061fc82008-05-07 11:43:44 -04001238 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001239 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001240
1241 all_avail = root->fs_info->avail_data_alloc_bits |
1242 root->fs_info->avail_system_alloc_bits |
1243 root->fs_info->avail_metadata_alloc_bits;
1244
1245 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001246 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001247 printk(KERN_ERR "btrfs: unable to go below four devices "
1248 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001249 ret = -EINVAL;
1250 goto out;
1251 }
1252
1253 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001254 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001255 printk(KERN_ERR "btrfs: unable to go below two "
1256 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001257 ret = -EINVAL;
1258 goto out;
1259 }
1260
Chris Masondfe25022008-05-13 13:46:40 -04001261 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001262 struct list_head *devices;
1263 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001264
Chris Masondfe25022008-05-13 13:46:40 -04001265 device = NULL;
1266 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001267 /*
1268 * It is safe to read the devices since the volume_mutex
1269 * is held.
1270 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001271 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001272 if (tmp->in_fs_metadata && !tmp->bdev) {
1273 device = tmp;
1274 break;
1275 }
1276 }
1277 bdev = NULL;
1278 bh = NULL;
1279 disk_super = NULL;
1280 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001281 printk(KERN_ERR "btrfs: no missing devices found to "
1282 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001283 goto out;
1284 }
Chris Masondfe25022008-05-13 13:46:40 -04001285 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001286 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1287 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001288 if (IS_ERR(bdev)) {
1289 ret = PTR_ERR(bdev);
1290 goto out;
1291 }
1292
Yan Zheng2b820322008-11-17 21:11:30 -05001293 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001294 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001295 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001296 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001297 goto error_close;
1298 }
1299 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001300 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001301 dev_uuid = disk_super->dev_item.uuid;
1302 device = btrfs_find_device(root, devid, dev_uuid,
1303 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001304 if (!device) {
1305 ret = -ENOENT;
1306 goto error_brelse;
1307 }
Chris Masondfe25022008-05-13 13:46:40 -04001308 }
Yan Zheng2b820322008-11-17 21:11:30 -05001309
1310 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001311 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1312 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001313 ret = -EINVAL;
1314 goto error_brelse;
1315 }
1316
1317 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001318 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001319 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001320 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001321 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001322 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001323 }
Chris Masona061fc82008-05-07 11:43:44 -04001324
1325 ret = btrfs_shrink_device(device, 0);
1326 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001327 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001328
Chris Masona061fc82008-05-07 11:43:44 -04001329 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1330 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001331 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001332
Yan Zheng2b820322008-11-17 21:11:30 -05001333 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001334 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001335
1336 /*
1337 * the device list mutex makes sure that we don't change
1338 * the device list while someone else is writing out all
1339 * the device supers.
1340 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001341
1342 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001343 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001344 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001345
Yan Zhenge4404d62008-12-12 10:03:26 -05001346 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001347
Chris Masoncd02dca2010-12-13 14:56:23 -05001348 if (device->missing)
1349 root->fs_info->fs_devices->missing_devices--;
1350
Yan Zheng2b820322008-11-17 21:11:30 -05001351 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1352 struct btrfs_device, dev_list);
1353 if (device->bdev == root->fs_info->sb->s_bdev)
1354 root->fs_info->sb->s_bdev = next_device->bdev;
1355 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1356 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1357
Xiao Guangrong1f781602011-04-20 10:09:16 +00001358 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001359 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001360
1361 call_rcu(&device->rcu, free_device);
1362 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001363
Yan Zheng2b820322008-11-17 21:11:30 -05001364 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1365 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1366
Xiao Guangrong1f781602011-04-20 10:09:16 +00001367 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001368 struct btrfs_fs_devices *fs_devices;
1369 fs_devices = root->fs_info->fs_devices;
1370 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001371 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001372 break;
1373 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001374 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001375 fs_devices->seed = cur_devices->seed;
1376 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001377 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001378 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001379 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001380 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001381 }
1382
1383 /*
1384 * at this point, the device is zero sized. We want to
1385 * remove it from the devices list and zero out the old super
1386 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001387 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001388 /* make sure this device isn't detected as part of
1389 * the FS anymore
1390 */
1391 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1392 set_buffer_dirty(bh);
1393 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001394 }
Chris Masona061fc82008-05-07 11:43:44 -04001395
Chris Masona061fc82008-05-07 11:43:44 -04001396 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001397
1398error_brelse:
1399 brelse(bh);
1400error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001401 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001402 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001403out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001404 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001405 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001406 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001407error_undo:
1408 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001409 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001410 list_add(&device->dev_alloc_list,
1411 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001412 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001413 root->fs_info->fs_devices->rw_devices++;
1414 }
1415 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001416}
1417
Yan Zheng2b820322008-11-17 21:11:30 -05001418/*
1419 * does all the dirty work required for changing file system's UUID.
1420 */
1421static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1422 struct btrfs_root *root)
1423{
1424 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1425 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001426 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001427 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1428 struct btrfs_device *device;
1429 u64 super_flags;
1430
1431 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001432 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001433 return -EINVAL;
1434
Yan Zhenge4404d62008-12-12 10:03:26 -05001435 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1436 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001437 return -ENOMEM;
1438
Yan Zhenge4404d62008-12-12 10:03:26 -05001439 old_devices = clone_fs_devices(fs_devices);
1440 if (IS_ERR(old_devices)) {
1441 kfree(seed_devices);
1442 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001443 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001444
Yan Zheng2b820322008-11-17 21:11:30 -05001445 list_add(&old_devices->list, &fs_uuids);
1446
Yan Zhenge4404d62008-12-12 10:03:26 -05001447 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1448 seed_devices->opened = 1;
1449 INIT_LIST_HEAD(&seed_devices->devices);
1450 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001451 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001452
1453 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001454 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1455 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001456 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1457
Yan Zhenge4404d62008-12-12 10:03:26 -05001458 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1459 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1460 device->fs_devices = seed_devices;
1461 }
1462
Yan Zheng2b820322008-11-17 21:11:30 -05001463 fs_devices->seeding = 0;
1464 fs_devices->num_devices = 0;
1465 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001466 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001467
1468 generate_random_uuid(fs_devices->fsid);
1469 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1470 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1471 super_flags = btrfs_super_flags(disk_super) &
1472 ~BTRFS_SUPER_FLAG_SEEDING;
1473 btrfs_set_super_flags(disk_super, super_flags);
1474
1475 return 0;
1476}
1477
1478/*
1479 * strore the expected generation for seed devices in device items.
1480 */
1481static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root)
1483{
1484 struct btrfs_path *path;
1485 struct extent_buffer *leaf;
1486 struct btrfs_dev_item *dev_item;
1487 struct btrfs_device *device;
1488 struct btrfs_key key;
1489 u8 fs_uuid[BTRFS_UUID_SIZE];
1490 u8 dev_uuid[BTRFS_UUID_SIZE];
1491 u64 devid;
1492 int ret;
1493
1494 path = btrfs_alloc_path();
1495 if (!path)
1496 return -ENOMEM;
1497
1498 root = root->fs_info->chunk_root;
1499 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1500 key.offset = 0;
1501 key.type = BTRFS_DEV_ITEM_KEY;
1502
1503 while (1) {
1504 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1505 if (ret < 0)
1506 goto error;
1507
1508 leaf = path->nodes[0];
1509next_slot:
1510 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1511 ret = btrfs_next_leaf(root, path);
1512 if (ret > 0)
1513 break;
1514 if (ret < 0)
1515 goto error;
1516 leaf = path->nodes[0];
1517 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001518 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001519 continue;
1520 }
1521
1522 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1523 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1524 key.type != BTRFS_DEV_ITEM_KEY)
1525 break;
1526
1527 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1528 struct btrfs_dev_item);
1529 devid = btrfs_device_id(leaf, dev_item);
1530 read_extent_buffer(leaf, dev_uuid,
1531 (unsigned long)btrfs_device_uuid(dev_item),
1532 BTRFS_UUID_SIZE);
1533 read_extent_buffer(leaf, fs_uuid,
1534 (unsigned long)btrfs_device_fsid(dev_item),
1535 BTRFS_UUID_SIZE);
1536 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1537 BUG_ON(!device);
1538
1539 if (device->fs_devices->seeding) {
1540 btrfs_set_device_generation(leaf, dev_item,
1541 device->generation);
1542 btrfs_mark_buffer_dirty(leaf);
1543 }
1544
1545 path->slots[0]++;
1546 goto next_slot;
1547 }
1548 ret = 0;
1549error:
1550 btrfs_free_path(path);
1551 return ret;
1552}
1553
Chris Mason788f20e2008-04-28 15:29:42 -04001554int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1555{
Josef Bacika2ea1862011-08-04 14:52:27 +00001556 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001557 struct btrfs_trans_handle *trans;
1558 struct btrfs_device *device;
1559 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001560 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001561 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001562 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001563 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001564 int ret = 0;
1565
Yan Zheng2b820322008-11-17 21:11:30 -05001566 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1567 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001568
Tejun Heod4d77622010-11-13 11:55:18 +01001569 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1570 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001571 if (IS_ERR(bdev))
1572 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001573
Yan Zheng2b820322008-11-17 21:11:30 -05001574 if (root->fs_info->fs_devices->seeding) {
1575 seeding_dev = 1;
1576 down_write(&sb->s_umount);
1577 mutex_lock(&uuid_mutex);
1578 }
1579
Chris Mason8c8bee12008-09-29 11:19:10 -04001580 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001581 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001582
Chris Mason788f20e2008-04-28 15:29:42 -04001583 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001584 /*
1585 * we have the volume lock, so we don't need the extra
1586 * device list mutex while reading the list here.
1587 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001588 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001589 if (device->bdev == bdev) {
1590 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001591 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001592 }
1593 }
1594
1595 device = kzalloc(sizeof(*device), GFP_NOFS);
1596 if (!device) {
1597 /* we can safely leave the fs_devices entry around */
1598 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001599 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001600 }
1601
Chris Mason788f20e2008-04-28 15:29:42 -04001602 device->name = kstrdup(device_path, GFP_NOFS);
1603 if (!device->name) {
1604 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001605 ret = -ENOMEM;
1606 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001607 }
Yan Zheng2b820322008-11-17 21:11:30 -05001608
1609 ret = find_next_devid(root, &device->devid);
1610 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001611 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001612 kfree(device);
1613 goto error;
1614 }
1615
Yan, Zhenga22285a2010-05-16 10:48:46 -04001616 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001617 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001618 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001619 kfree(device);
1620 ret = PTR_ERR(trans);
1621 goto error;
1622 }
1623
Yan Zheng2b820322008-11-17 21:11:30 -05001624 lock_chunks(root);
1625
Josef Bacika2ea1862011-08-04 14:52:27 +00001626 q = bdev_get_queue(bdev);
1627 if (blk_queue_discard(q))
1628 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001629 device->writeable = 1;
1630 device->work.func = pending_bios_fn;
1631 generate_random_uuid(device->uuid);
1632 spin_lock_init(&device->io_lock);
1633 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001634 device->io_width = root->sectorsize;
1635 device->io_align = root->sectorsize;
1636 device->sector_size = root->sectorsize;
1637 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001638 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001639 device->dev_root = root->fs_info->dev_root;
1640 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001641 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001642 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001643 set_blocksize(device->bdev, 4096);
1644
Yan Zheng2b820322008-11-17 21:11:30 -05001645 if (seeding_dev) {
1646 sb->s_flags &= ~MS_RDONLY;
1647 ret = btrfs_prepare_sprout(trans, root);
1648 BUG_ON(ret);
1649 }
1650
1651 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001652
1653 /*
1654 * we don't want write_supers to jump in here with our device
1655 * half setup
1656 */
1657 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001658 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001659 list_add(&device->dev_alloc_list,
1660 &root->fs_info->fs_devices->alloc_list);
1661 root->fs_info->fs_devices->num_devices++;
1662 root->fs_info->fs_devices->open_devices++;
1663 root->fs_info->fs_devices->rw_devices++;
Josef Bacika2ea1862011-08-04 14:52:27 +00001664 if (device->can_discard)
1665 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001666 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1667
Chris Masonc2898112009-06-10 09:51:32 -04001668 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1669 root->fs_info->fs_devices->rotating = 1;
1670
Chris Mason788f20e2008-04-28 15:29:42 -04001671 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1672 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1673 total_bytes + device->total_bytes);
1674
1675 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1676 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1677 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001678 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001679
Yan Zheng2b820322008-11-17 21:11:30 -05001680 if (seeding_dev) {
1681 ret = init_first_rw_device(trans, root, device);
1682 BUG_ON(ret);
1683 ret = btrfs_finish_sprout(trans, root);
1684 BUG_ON(ret);
1685 } else {
1686 ret = btrfs_add_device(trans, root, device);
1687 }
1688
Chris Mason913d9522009-03-10 13:17:18 -04001689 /*
1690 * we've got more storage, clear any full flags on the space
1691 * infos
1692 */
1693 btrfs_clear_space_info_full(root->fs_info);
1694
Chris Mason7d9eb122008-07-08 14:19:17 -04001695 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001696 btrfs_commit_transaction(trans, root);
1697
1698 if (seeding_dev) {
1699 mutex_unlock(&uuid_mutex);
1700 up_write(&sb->s_umount);
1701
1702 ret = btrfs_relocate_sys_chunks(root);
1703 BUG_ON(ret);
1704 }
1705out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001706 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001707 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001708error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001709 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001710 if (seeding_dev) {
1711 mutex_unlock(&uuid_mutex);
1712 up_write(&sb->s_umount);
1713 }
Chris Mason788f20e2008-04-28 15:29:42 -04001714 goto out;
1715}
1716
Chris Masond3977122009-01-05 21:25:51 -05001717static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1718 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001719{
1720 int ret;
1721 struct btrfs_path *path;
1722 struct btrfs_root *root;
1723 struct btrfs_dev_item *dev_item;
1724 struct extent_buffer *leaf;
1725 struct btrfs_key key;
1726
1727 root = device->dev_root->fs_info->chunk_root;
1728
1729 path = btrfs_alloc_path();
1730 if (!path)
1731 return -ENOMEM;
1732
1733 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1734 key.type = BTRFS_DEV_ITEM_KEY;
1735 key.offset = device->devid;
1736
1737 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1738 if (ret < 0)
1739 goto out;
1740
1741 if (ret > 0) {
1742 ret = -ENOENT;
1743 goto out;
1744 }
1745
1746 leaf = path->nodes[0];
1747 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1748
1749 btrfs_set_device_id(leaf, dev_item, device->devid);
1750 btrfs_set_device_type(leaf, dev_item, device->type);
1751 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1752 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1753 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001754 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001755 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1756 btrfs_mark_buffer_dirty(leaf);
1757
1758out:
1759 btrfs_free_path(path);
1760 return ret;
1761}
1762
Chris Mason7d9eb122008-07-08 14:19:17 -04001763static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001764 struct btrfs_device *device, u64 new_size)
1765{
1766 struct btrfs_super_block *super_copy =
1767 &device->dev_root->fs_info->super_copy;
1768 u64 old_total = btrfs_super_total_bytes(super_copy);
1769 u64 diff = new_size - device->total_bytes;
1770
Yan Zheng2b820322008-11-17 21:11:30 -05001771 if (!device->writeable)
1772 return -EACCES;
1773 if (new_size <= device->total_bytes)
1774 return -EINVAL;
1775
Chris Mason8f18cf12008-04-25 16:53:30 -04001776 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001777 device->fs_devices->total_rw_bytes += diff;
1778
1779 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001780 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001781 btrfs_clear_space_info_full(device->dev_root->fs_info);
1782
Chris Mason8f18cf12008-04-25 16:53:30 -04001783 return btrfs_update_device(trans, device);
1784}
1785
Chris Mason7d9eb122008-07-08 14:19:17 -04001786int btrfs_grow_device(struct btrfs_trans_handle *trans,
1787 struct btrfs_device *device, u64 new_size)
1788{
1789 int ret;
1790 lock_chunks(device->dev_root);
1791 ret = __btrfs_grow_device(trans, device, new_size);
1792 unlock_chunks(device->dev_root);
1793 return ret;
1794}
1795
Chris Mason8f18cf12008-04-25 16:53:30 -04001796static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1797 struct btrfs_root *root,
1798 u64 chunk_tree, u64 chunk_objectid,
1799 u64 chunk_offset)
1800{
1801 int ret;
1802 struct btrfs_path *path;
1803 struct btrfs_key key;
1804
1805 root = root->fs_info->chunk_root;
1806 path = btrfs_alloc_path();
1807 if (!path)
1808 return -ENOMEM;
1809
1810 key.objectid = chunk_objectid;
1811 key.offset = chunk_offset;
1812 key.type = BTRFS_CHUNK_ITEM_KEY;
1813
1814 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1815 BUG_ON(ret);
1816
1817 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001818
1819 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001820 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001821}
1822
Christoph Hellwigb2950862008-12-02 09:54:17 -05001823static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001824 chunk_offset)
1825{
1826 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1827 struct btrfs_disk_key *disk_key;
1828 struct btrfs_chunk *chunk;
1829 u8 *ptr;
1830 int ret = 0;
1831 u32 num_stripes;
1832 u32 array_size;
1833 u32 len = 0;
1834 u32 cur;
1835 struct btrfs_key key;
1836
1837 array_size = btrfs_super_sys_array_size(super_copy);
1838
1839 ptr = super_copy->sys_chunk_array;
1840 cur = 0;
1841
1842 while (cur < array_size) {
1843 disk_key = (struct btrfs_disk_key *)ptr;
1844 btrfs_disk_key_to_cpu(&key, disk_key);
1845
1846 len = sizeof(*disk_key);
1847
1848 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1849 chunk = (struct btrfs_chunk *)(ptr + len);
1850 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1851 len += btrfs_chunk_item_size(num_stripes);
1852 } else {
1853 ret = -EIO;
1854 break;
1855 }
1856 if (key.objectid == chunk_objectid &&
1857 key.offset == chunk_offset) {
1858 memmove(ptr, ptr + len, array_size - (cur + len));
1859 array_size -= len;
1860 btrfs_set_super_sys_array_size(super_copy, array_size);
1861 } else {
1862 ptr += len;
1863 cur += len;
1864 }
1865 }
1866 return ret;
1867}
1868
Christoph Hellwigb2950862008-12-02 09:54:17 -05001869static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001870 u64 chunk_tree, u64 chunk_objectid,
1871 u64 chunk_offset)
1872{
1873 struct extent_map_tree *em_tree;
1874 struct btrfs_root *extent_root;
1875 struct btrfs_trans_handle *trans;
1876 struct extent_map *em;
1877 struct map_lookup *map;
1878 int ret;
1879 int i;
1880
1881 root = root->fs_info->chunk_root;
1882 extent_root = root->fs_info->extent_root;
1883 em_tree = &root->fs_info->mapping_tree.map_tree;
1884
Josef Bacikba1bf482009-09-11 16:11:19 -04001885 ret = btrfs_can_relocate(extent_root, chunk_offset);
1886 if (ret)
1887 return -ENOSPC;
1888
Chris Mason8f18cf12008-04-25 16:53:30 -04001889 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001890 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001891 if (ret)
1892 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001893
Yan, Zhenga22285a2010-05-16 10:48:46 -04001894 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001895 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001896
Chris Mason7d9eb122008-07-08 14:19:17 -04001897 lock_chunks(root);
1898
Chris Mason8f18cf12008-04-25 16:53:30 -04001899 /*
1900 * step two, delete the device extents and the
1901 * chunk tree entries
1902 */
Chris Mason890871b2009-09-02 16:24:52 -04001903 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001904 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001905 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001906
Chris Masona061fc82008-05-07 11:43:44 -04001907 BUG_ON(em->start > chunk_offset ||
1908 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001909 map = (struct map_lookup *)em->bdev;
1910
1911 for (i = 0; i < map->num_stripes; i++) {
1912 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1913 map->stripes[i].physical);
1914 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001915
Chris Masondfe25022008-05-13 13:46:40 -04001916 if (map->stripes[i].dev) {
1917 ret = btrfs_update_device(trans, map->stripes[i].dev);
1918 BUG_ON(ret);
1919 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001920 }
1921 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1922 chunk_offset);
1923
1924 BUG_ON(ret);
1925
liubo1abe9b82011-03-24 11:18:59 +00001926 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1927
Chris Mason8f18cf12008-04-25 16:53:30 -04001928 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1929 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1930 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001931 }
1932
Zheng Yan1a40e232008-09-26 10:09:34 -04001933 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1934 BUG_ON(ret);
1935
Chris Mason890871b2009-09-02 16:24:52 -04001936 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001937 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001938 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001939
Chris Mason8f18cf12008-04-25 16:53:30 -04001940 kfree(map);
1941 em->bdev = NULL;
1942
1943 /* once for the tree */
1944 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001945 /* once for us */
1946 free_extent_map(em);
1947
Chris Mason7d9eb122008-07-08 14:19:17 -04001948 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001949 btrfs_end_transaction(trans, root);
1950 return 0;
1951}
1952
Yan Zheng2b820322008-11-17 21:11:30 -05001953static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1954{
1955 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1956 struct btrfs_path *path;
1957 struct extent_buffer *leaf;
1958 struct btrfs_chunk *chunk;
1959 struct btrfs_key key;
1960 struct btrfs_key found_key;
1961 u64 chunk_tree = chunk_root->root_key.objectid;
1962 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001963 bool retried = false;
1964 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001965 int ret;
1966
1967 path = btrfs_alloc_path();
1968 if (!path)
1969 return -ENOMEM;
1970
Josef Bacikba1bf482009-09-11 16:11:19 -04001971again:
Yan Zheng2b820322008-11-17 21:11:30 -05001972 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1973 key.offset = (u64)-1;
1974 key.type = BTRFS_CHUNK_ITEM_KEY;
1975
1976 while (1) {
1977 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1978 if (ret < 0)
1979 goto error;
1980 BUG_ON(ret == 0);
1981
1982 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1983 key.type);
1984 if (ret < 0)
1985 goto error;
1986 if (ret > 0)
1987 break;
1988
1989 leaf = path->nodes[0];
1990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1991
1992 chunk = btrfs_item_ptr(leaf, path->slots[0],
1993 struct btrfs_chunk);
1994 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02001995 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001996
1997 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1998 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1999 found_key.objectid,
2000 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002001 if (ret == -ENOSPC)
2002 failed++;
2003 else if (ret)
2004 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002005 }
2006
2007 if (found_key.offset == 0)
2008 break;
2009 key.offset = found_key.offset - 1;
2010 }
2011 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002012 if (failed && !retried) {
2013 failed = 0;
2014 retried = true;
2015 goto again;
2016 } else if (failed && retried) {
2017 WARN_ON(1);
2018 ret = -ENOSPC;
2019 }
Yan Zheng2b820322008-11-17 21:11:30 -05002020error:
2021 btrfs_free_path(path);
2022 return ret;
2023}
2024
Chris Masonec44a352008-04-28 15:29:52 -04002025static u64 div_factor(u64 num, int factor)
2026{
2027 if (factor == 10)
2028 return num;
2029 num *= factor;
2030 do_div(num, 10);
2031 return num;
2032}
2033
Chris Masonec44a352008-04-28 15:29:52 -04002034int btrfs_balance(struct btrfs_root *dev_root)
2035{
2036 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002037 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2038 struct btrfs_device *device;
2039 u64 old_size;
2040 u64 size_to_free;
2041 struct btrfs_path *path;
2042 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002043 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2044 struct btrfs_trans_handle *trans;
2045 struct btrfs_key found_key;
2046
Yan Zheng2b820322008-11-17 21:11:30 -05002047 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2048 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002049
Ben Hutchings6f88a442010-12-29 14:55:03 +00002050 if (!capable(CAP_SYS_ADMIN))
2051 return -EPERM;
2052
Chris Mason7d9eb122008-07-08 14:19:17 -04002053 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002054 dev_root = dev_root->fs_info->dev_root;
2055
Chris Masonec44a352008-04-28 15:29:52 -04002056 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002057 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002058 old_size = device->total_bytes;
2059 size_to_free = div_factor(old_size, 1);
2060 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002061 if (!device->writeable ||
2062 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002063 continue;
2064
2065 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002066 if (ret == -ENOSPC)
2067 break;
Chris Masonec44a352008-04-28 15:29:52 -04002068 BUG_ON(ret);
2069
Yan, Zhenga22285a2010-05-16 10:48:46 -04002070 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002071 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002072
2073 ret = btrfs_grow_device(trans, device, old_size);
2074 BUG_ON(ret);
2075
2076 btrfs_end_transaction(trans, dev_root);
2077 }
2078
2079 /* step two, relocate all the chunks */
2080 path = btrfs_alloc_path();
2081 BUG_ON(!path);
2082
2083 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2084 key.offset = (u64)-1;
2085 key.type = BTRFS_CHUNK_ITEM_KEY;
2086
Chris Masond3977122009-01-05 21:25:51 -05002087 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002088 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2089 if (ret < 0)
2090 goto error;
2091
2092 /*
2093 * this shouldn't happen, it means the last relocate
2094 * failed
2095 */
2096 if (ret == 0)
2097 break;
2098
2099 ret = btrfs_previous_item(chunk_root, path, 0,
2100 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002101 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002102 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002103
Chris Masonec44a352008-04-28 15:29:52 -04002104 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2105 path->slots[0]);
2106 if (found_key.objectid != key.objectid)
2107 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002108
Chris Masonec44a352008-04-28 15:29:52 -04002109 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002110 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002111 break;
2112
David Sterbab3b4aa72011-04-21 01:20:15 +02002113 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002114 ret = btrfs_relocate_chunk(chunk_root,
2115 chunk_root->root_key.objectid,
2116 found_key.objectid,
2117 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002118 if (ret && ret != -ENOSPC)
2119 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002120 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002121 }
2122 ret = 0;
2123error:
2124 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002125 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002126 return ret;
2127}
2128
Chris Mason8f18cf12008-04-25 16:53:30 -04002129/*
2130 * shrinking a device means finding all of the device extents past
2131 * the new size, and then following the back refs to the chunks.
2132 * The chunk relocation code actually frees the device extent
2133 */
2134int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2135{
2136 struct btrfs_trans_handle *trans;
2137 struct btrfs_root *root = device->dev_root;
2138 struct btrfs_dev_extent *dev_extent = NULL;
2139 struct btrfs_path *path;
2140 u64 length;
2141 u64 chunk_tree;
2142 u64 chunk_objectid;
2143 u64 chunk_offset;
2144 int ret;
2145 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002146 int failed = 0;
2147 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002148 struct extent_buffer *l;
2149 struct btrfs_key key;
2150 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2151 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002152 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002153 u64 diff = device->total_bytes - new_size;
2154
Yan Zheng2b820322008-11-17 21:11:30 -05002155 if (new_size >= device->total_bytes)
2156 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002157
2158 path = btrfs_alloc_path();
2159 if (!path)
2160 return -ENOMEM;
2161
Chris Mason8f18cf12008-04-25 16:53:30 -04002162 path->reada = 2;
2163
Chris Mason7d9eb122008-07-08 14:19:17 -04002164 lock_chunks(root);
2165
Chris Mason8f18cf12008-04-25 16:53:30 -04002166 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002167 if (device->writeable)
2168 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002169 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002170
Josef Bacikba1bf482009-09-11 16:11:19 -04002171again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002172 key.objectid = device->devid;
2173 key.offset = (u64)-1;
2174 key.type = BTRFS_DEV_EXTENT_KEY;
2175
2176 while (1) {
2177 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2178 if (ret < 0)
2179 goto done;
2180
2181 ret = btrfs_previous_item(root, path, 0, key.type);
2182 if (ret < 0)
2183 goto done;
2184 if (ret) {
2185 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002186 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002187 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002188 }
2189
2190 l = path->nodes[0];
2191 slot = path->slots[0];
2192 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2193
Josef Bacikba1bf482009-09-11 16:11:19 -04002194 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002195 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002196 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002197 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002198
2199 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2200 length = btrfs_dev_extent_length(l, dev_extent);
2201
Josef Bacikba1bf482009-09-11 16:11:19 -04002202 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002203 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002204 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002205 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002206
2207 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2208 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2209 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002210 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002211
2212 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2213 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002214 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002215 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002216 if (ret == -ENOSPC)
2217 failed++;
2218 key.offset -= 1;
2219 }
2220
2221 if (failed && !retried) {
2222 failed = 0;
2223 retried = true;
2224 goto again;
2225 } else if (failed && retried) {
2226 ret = -ENOSPC;
2227 lock_chunks(root);
2228
2229 device->total_bytes = old_size;
2230 if (device->writeable)
2231 device->fs_devices->total_rw_bytes += diff;
2232 unlock_chunks(root);
2233 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002234 }
2235
Chris Balld6397ba2009-04-27 07:29:03 -04002236 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002237 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002238 if (IS_ERR(trans)) {
2239 ret = PTR_ERR(trans);
2240 goto done;
2241 }
2242
Chris Balld6397ba2009-04-27 07:29:03 -04002243 lock_chunks(root);
2244
2245 device->disk_total_bytes = new_size;
2246 /* Now btrfs_update_device() will change the on-disk size. */
2247 ret = btrfs_update_device(trans, device);
2248 if (ret) {
2249 unlock_chunks(root);
2250 btrfs_end_transaction(trans, root);
2251 goto done;
2252 }
2253 WARN_ON(diff > old_total);
2254 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2255 unlock_chunks(root);
2256 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002257done:
2258 btrfs_free_path(path);
2259 return ret;
2260}
2261
Christoph Hellwigb2950862008-12-02 09:54:17 -05002262static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002263 struct btrfs_root *root,
2264 struct btrfs_key *key,
2265 struct btrfs_chunk *chunk, int item_size)
2266{
2267 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2268 struct btrfs_disk_key disk_key;
2269 u32 array_size;
2270 u8 *ptr;
2271
2272 array_size = btrfs_super_sys_array_size(super_copy);
2273 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2274 return -EFBIG;
2275
2276 ptr = super_copy->sys_chunk_array + array_size;
2277 btrfs_cpu_key_to_disk(&disk_key, key);
2278 memcpy(ptr, &disk_key, sizeof(disk_key));
2279 ptr += sizeof(disk_key);
2280 memcpy(ptr, chunk, item_size);
2281 item_size += sizeof(disk_key);
2282 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2283 return 0;
2284}
2285
Miao Xieb2117a32011-01-05 10:07:28 +00002286/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002287 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002288 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002289static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002290{
Arne Jansen73c5de02011-04-12 12:07:57 +02002291 const struct btrfs_device_info *di_a = a;
2292 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002293
Arne Jansen73c5de02011-04-12 12:07:57 +02002294 if (di_a->max_avail > di_b->max_avail)
2295 return -1;
2296 if (di_a->max_avail < di_b->max_avail)
2297 return 1;
2298 if (di_a->total_avail > di_b->total_avail)
2299 return -1;
2300 if (di_a->total_avail < di_b->total_avail)
2301 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002302 return 0;
2303}
2304
2305static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2306 struct btrfs_root *extent_root,
2307 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002308 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002309 u64 start, u64 type)
2310{
2311 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002312 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2313 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002314 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002315 struct extent_map_tree *em_tree;
2316 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002317 struct btrfs_device_info *devices_info = NULL;
2318 u64 total_avail;
2319 int num_stripes; /* total number of stripes to allocate */
2320 int sub_stripes; /* sub_stripes info for map */
2321 int dev_stripes; /* stripes per dev */
2322 int devs_max; /* max devs to use */
2323 int devs_min; /* min devs needed */
2324 int devs_increment; /* ndevs has to be a multiple of this */
2325 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002326 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002327 u64 max_stripe_size;
2328 u64 max_chunk_size;
2329 u64 stripe_size;
2330 u64 num_bytes;
2331 int ndevs;
2332 int i;
2333 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002334
2335 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2336 (type & BTRFS_BLOCK_GROUP_DUP)) {
2337 WARN_ON(1);
2338 type &= ~BTRFS_BLOCK_GROUP_DUP;
2339 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002340
Miao Xieb2117a32011-01-05 10:07:28 +00002341 if (list_empty(&fs_devices->alloc_list))
2342 return -ENOSPC;
2343
Arne Jansen73c5de02011-04-12 12:07:57 +02002344 sub_stripes = 1;
2345 dev_stripes = 1;
2346 devs_increment = 1;
2347 ncopies = 1;
2348 devs_max = 0; /* 0 == as many as possible */
2349 devs_min = 1;
2350
2351 /*
2352 * define the properties of each RAID type.
2353 * FIXME: move this to a global table and use it in all RAID
2354 * calculation code
2355 */
2356 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2357 dev_stripes = 2;
2358 ncopies = 2;
2359 devs_max = 1;
2360 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2361 devs_min = 2;
2362 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2363 devs_increment = 2;
2364 ncopies = 2;
2365 devs_max = 2;
2366 devs_min = 2;
2367 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2368 sub_stripes = 2;
2369 devs_increment = 2;
2370 ncopies = 2;
2371 devs_min = 4;
2372 } else {
2373 devs_max = 1;
2374 }
2375
2376 if (type & BTRFS_BLOCK_GROUP_DATA) {
2377 max_stripe_size = 1024 * 1024 * 1024;
2378 max_chunk_size = 10 * max_stripe_size;
2379 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2380 max_stripe_size = 256 * 1024 * 1024;
2381 max_chunk_size = max_stripe_size;
2382 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2383 max_stripe_size = 8 * 1024 * 1024;
2384 max_chunk_size = 2 * max_stripe_size;
2385 } else {
2386 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2387 type);
2388 BUG_ON(1);
2389 }
2390
2391 /* we don't want a chunk larger than 10% of writeable space */
2392 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2393 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002394
2395 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2396 GFP_NOFS);
2397 if (!devices_info)
2398 return -ENOMEM;
2399
Arne Jansen73c5de02011-04-12 12:07:57 +02002400 cur = fs_devices->alloc_list.next;
2401
2402 /*
2403 * in the first pass through the devices list, we gather information
2404 * about the available holes on each device.
2405 */
2406 ndevs = 0;
2407 while (cur != &fs_devices->alloc_list) {
2408 struct btrfs_device *device;
2409 u64 max_avail;
2410 u64 dev_offset;
2411
2412 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2413
2414 cur = cur->next;
2415
2416 if (!device->writeable) {
2417 printk(KERN_ERR
2418 "btrfs: read-only device in alloc_list\n");
2419 WARN_ON(1);
2420 continue;
2421 }
2422
2423 if (!device->in_fs_metadata)
2424 continue;
2425
2426 if (device->total_bytes > device->bytes_used)
2427 total_avail = device->total_bytes - device->bytes_used;
2428 else
2429 total_avail = 0;
2430 /* avail is off by max(alloc_start, 1MB), but that is the same
2431 * for all devices, so it doesn't hurt the sorting later on
2432 */
2433
2434 ret = find_free_dev_extent(trans, device,
2435 max_stripe_size * dev_stripes,
2436 &dev_offset, &max_avail);
2437 if (ret && ret != -ENOSPC)
2438 goto error;
2439
2440 if (ret == 0)
2441 max_avail = max_stripe_size * dev_stripes;
2442
2443 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2444 continue;
2445
2446 devices_info[ndevs].dev_offset = dev_offset;
2447 devices_info[ndevs].max_avail = max_avail;
2448 devices_info[ndevs].total_avail = total_avail;
2449 devices_info[ndevs].dev = device;
2450 ++ndevs;
2451 }
2452
2453 /*
2454 * now sort the devices by hole size / available space
2455 */
2456 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2457 btrfs_cmp_device_info, NULL);
2458
2459 /* round down to number of usable stripes */
2460 ndevs -= ndevs % devs_increment;
2461
2462 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2463 ret = -ENOSPC;
2464 goto error;
2465 }
2466
2467 if (devs_max && ndevs > devs_max)
2468 ndevs = devs_max;
2469 /*
2470 * the primary goal is to maximize the number of stripes, so use as many
2471 * devices as possible, even if the stripes are not maximum sized.
2472 */
2473 stripe_size = devices_info[ndevs-1].max_avail;
2474 num_stripes = ndevs * dev_stripes;
2475
2476 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2477 stripe_size = max_chunk_size * ncopies;
2478 do_div(stripe_size, num_stripes);
2479 }
2480
2481 do_div(stripe_size, dev_stripes);
2482 do_div(stripe_size, BTRFS_STRIPE_LEN);
2483 stripe_size *= BTRFS_STRIPE_LEN;
2484
Miao Xieb2117a32011-01-05 10:07:28 +00002485 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2486 if (!map) {
2487 ret = -ENOMEM;
2488 goto error;
2489 }
2490 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002491
Arne Jansen73c5de02011-04-12 12:07:57 +02002492 for (i = 0; i < ndevs; ++i) {
2493 for (j = 0; j < dev_stripes; ++j) {
2494 int s = i * dev_stripes + j;
2495 map->stripes[s].dev = devices_info[i].dev;
2496 map->stripes[s].physical = devices_info[i].dev_offset +
2497 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002498 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002499 }
Chris Mason593060d2008-03-25 16:50:33 -04002500 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002501 map->stripe_len = BTRFS_STRIPE_LEN;
2502 map->io_align = BTRFS_STRIPE_LEN;
2503 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002504 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002505 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002506
Yan Zheng2b820322008-11-17 21:11:30 -05002507 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002508 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002509
Arne Jansen73c5de02011-04-12 12:07:57 +02002510 *stripe_size_out = stripe_size;
2511 *num_bytes_out = num_bytes;
2512
2513 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002514
David Sterba172ddd62011-04-21 00:48:27 +02002515 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002516 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002517 ret = -ENOMEM;
2518 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002519 }
Chris Mason0b86a832008-03-24 15:01:56 -04002520 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002521 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002522 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002523 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002524 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002525
Chris Mason0b86a832008-03-24 15:01:56 -04002526 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002527 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002528 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002529 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002530 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002531 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002532
2533 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2534 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002535 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002536 BUG_ON(ret);
2537
Arne Jansen73c5de02011-04-12 12:07:57 +02002538 for (i = 0; i < map->num_stripes; ++i) {
2539 struct btrfs_device *device;
2540 u64 dev_offset;
2541
2542 device = map->stripes[i].dev;
2543 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002544
2545 ret = btrfs_alloc_dev_extent(trans, device,
2546 info->chunk_root->root_key.objectid,
2547 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002548 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002549 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002550 }
2551
Miao Xieb2117a32011-01-05 10:07:28 +00002552 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002553 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002554
2555error:
2556 kfree(map);
2557 kfree(devices_info);
2558 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002559}
2560
2561static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2562 struct btrfs_root *extent_root,
2563 struct map_lookup *map, u64 chunk_offset,
2564 u64 chunk_size, u64 stripe_size)
2565{
2566 u64 dev_offset;
2567 struct btrfs_key key;
2568 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2569 struct btrfs_device *device;
2570 struct btrfs_chunk *chunk;
2571 struct btrfs_stripe *stripe;
2572 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2573 int index = 0;
2574 int ret;
2575
2576 chunk = kzalloc(item_size, GFP_NOFS);
2577 if (!chunk)
2578 return -ENOMEM;
2579
2580 index = 0;
2581 while (index < map->num_stripes) {
2582 device = map->stripes[index].dev;
2583 device->bytes_used += stripe_size;
2584 ret = btrfs_update_device(trans, device);
2585 BUG_ON(ret);
2586 index++;
2587 }
2588
2589 index = 0;
2590 stripe = &chunk->stripe;
2591 while (index < map->num_stripes) {
2592 device = map->stripes[index].dev;
2593 dev_offset = map->stripes[index].physical;
2594
2595 btrfs_set_stack_stripe_devid(stripe, device->devid);
2596 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2597 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2598 stripe++;
2599 index++;
2600 }
2601
2602 btrfs_set_stack_chunk_length(chunk, chunk_size);
2603 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2604 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2605 btrfs_set_stack_chunk_type(chunk, map->type);
2606 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2607 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2608 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2609 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2610 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2611
2612 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2613 key.type = BTRFS_CHUNK_ITEM_KEY;
2614 key.offset = chunk_offset;
2615
2616 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2617 BUG_ON(ret);
2618
2619 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2620 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2621 item_size);
2622 BUG_ON(ret);
2623 }
liubo1abe9b82011-03-24 11:18:59 +00002624
Yan Zheng2b820322008-11-17 21:11:30 -05002625 kfree(chunk);
2626 return 0;
2627}
2628
2629/*
2630 * Chunk allocation falls into two parts. The first part does works
2631 * that make the new allocated chunk useable, but not do any operation
2632 * that modifies the chunk tree. The second part does the works that
2633 * require modifying the chunk tree. This division is important for the
2634 * bootstrap process of adding storage to a seed btrfs.
2635 */
2636int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2637 struct btrfs_root *extent_root, u64 type)
2638{
2639 u64 chunk_offset;
2640 u64 chunk_size;
2641 u64 stripe_size;
2642 struct map_lookup *map;
2643 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2644 int ret;
2645
2646 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2647 &chunk_offset);
2648 if (ret)
2649 return ret;
2650
2651 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2652 &stripe_size, chunk_offset, type);
2653 if (ret)
2654 return ret;
2655
2656 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2657 chunk_size, stripe_size);
2658 BUG_ON(ret);
2659 return 0;
2660}
2661
Chris Masond3977122009-01-05 21:25:51 -05002662static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002663 struct btrfs_root *root,
2664 struct btrfs_device *device)
2665{
2666 u64 chunk_offset;
2667 u64 sys_chunk_offset;
2668 u64 chunk_size;
2669 u64 sys_chunk_size;
2670 u64 stripe_size;
2671 u64 sys_stripe_size;
2672 u64 alloc_profile;
2673 struct map_lookup *map;
2674 struct map_lookup *sys_map;
2675 struct btrfs_fs_info *fs_info = root->fs_info;
2676 struct btrfs_root *extent_root = fs_info->extent_root;
2677 int ret;
2678
2679 ret = find_next_chunk(fs_info->chunk_root,
2680 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2681 BUG_ON(ret);
2682
2683 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2684 (fs_info->metadata_alloc_profile &
2685 fs_info->avail_metadata_alloc_bits);
2686 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2687
2688 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2689 &stripe_size, chunk_offset, alloc_profile);
2690 BUG_ON(ret);
2691
2692 sys_chunk_offset = chunk_offset + chunk_size;
2693
2694 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2695 (fs_info->system_alloc_profile &
2696 fs_info->avail_system_alloc_bits);
2697 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2698
2699 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2700 &sys_chunk_size, &sys_stripe_size,
2701 sys_chunk_offset, alloc_profile);
2702 BUG_ON(ret);
2703
2704 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2705 BUG_ON(ret);
2706
2707 /*
2708 * Modifying chunk tree needs allocating new blocks from both
2709 * system block group and metadata block group. So we only can
2710 * do operations require modifying the chunk tree after both
2711 * block groups were created.
2712 */
2713 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2714 chunk_size, stripe_size);
2715 BUG_ON(ret);
2716
2717 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2718 sys_chunk_offset, sys_chunk_size,
2719 sys_stripe_size);
2720 BUG_ON(ret);
2721 return 0;
2722}
2723
2724int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2725{
2726 struct extent_map *em;
2727 struct map_lookup *map;
2728 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2729 int readonly = 0;
2730 int i;
2731
Chris Mason890871b2009-09-02 16:24:52 -04002732 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002733 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002734 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002735 if (!em)
2736 return 1;
2737
Josef Bacikf48b9072010-01-27 02:07:59 +00002738 if (btrfs_test_opt(root, DEGRADED)) {
2739 free_extent_map(em);
2740 return 0;
2741 }
2742
Yan Zheng2b820322008-11-17 21:11:30 -05002743 map = (struct map_lookup *)em->bdev;
2744 for (i = 0; i < map->num_stripes; i++) {
2745 if (!map->stripes[i].dev->writeable) {
2746 readonly = 1;
2747 break;
2748 }
2749 }
2750 free_extent_map(em);
2751 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002752}
2753
2754void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2755{
David Sterbaa8067e02011-04-21 00:34:43 +02002756 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002757}
2758
2759void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2760{
2761 struct extent_map *em;
2762
Chris Masond3977122009-01-05 21:25:51 -05002763 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002764 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002765 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2766 if (em)
2767 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002768 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002769 if (!em)
2770 break;
2771 kfree(em->bdev);
2772 /* once for us */
2773 free_extent_map(em);
2774 /* once for the tree */
2775 free_extent_map(em);
2776 }
2777}
2778
Chris Masonf1885912008-04-09 16:28:12 -04002779int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2780{
2781 struct extent_map *em;
2782 struct map_lookup *map;
2783 struct extent_map_tree *em_tree = &map_tree->map_tree;
2784 int ret;
2785
Chris Mason890871b2009-09-02 16:24:52 -04002786 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002787 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002788 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002789 BUG_ON(!em);
2790
2791 BUG_ON(em->start > logical || em->start + em->len < logical);
2792 map = (struct map_lookup *)em->bdev;
2793 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2794 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002795 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2796 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002797 else
2798 ret = 1;
2799 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002800 return ret;
2801}
2802
Chris Masondfe25022008-05-13 13:46:40 -04002803static int find_live_mirror(struct map_lookup *map, int first, int num,
2804 int optimal)
2805{
2806 int i;
2807 if (map->stripes[optimal].dev->bdev)
2808 return optimal;
2809 for (i = first; i < first + num; i++) {
2810 if (map->stripes[i].dev->bdev)
2811 return i;
2812 }
2813 /* we couldn't find one that doesn't fail. Just return something
2814 * and the io error handling code will clean up eventually
2815 */
2816 return optimal;
2817}
2818
Chris Masonf2d8d742008-04-21 10:03:05 -04002819static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2820 u64 logical, u64 *length,
2821 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002822 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002823{
2824 struct extent_map *em;
2825 struct map_lookup *map;
2826 struct extent_map_tree *em_tree = &map_tree->map_tree;
2827 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002828 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002829 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002830 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002831 u64 stripe_nr_orig;
2832 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002833 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002834 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002835 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002836 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002837 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002838 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002839 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002840
Li Dongyangfce3bb92011-03-24 10:24:26 +00002841 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002842 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002843again:
2844 if (multi_ret) {
2845 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2846 GFP_NOFS);
2847 if (!multi)
2848 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002849
2850 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002851 }
Chris Mason0b86a832008-03-24 15:01:56 -04002852
Chris Mason890871b2009-09-02 16:24:52 -04002853 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002854 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002855 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002856
Chris Mason3b951512008-04-17 11:29:12 -04002857 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002858 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2859 (unsigned long long)logical,
2860 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002861 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002862 }
Chris Mason0b86a832008-03-24 15:01:56 -04002863
2864 BUG_ON(em->start > logical || em->start + em->len < logical);
2865 map = (struct map_lookup *)em->bdev;
2866 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002867
Chris Masonf1885912008-04-09 16:28:12 -04002868 if (mirror_num > map->num_stripes)
2869 mirror_num = 0;
2870
Chris Masoncea9e442008-04-09 16:28:12 -04002871 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002872 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002873 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2874 BTRFS_BLOCK_GROUP_DUP)) {
2875 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002876 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002877 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2878 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002879 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002880 }
2881 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002882 if (rw & REQ_DISCARD) {
2883 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2884 BTRFS_BLOCK_GROUP_RAID1 |
2885 BTRFS_BLOCK_GROUP_DUP |
2886 BTRFS_BLOCK_GROUP_RAID10)) {
2887 stripes_required = map->num_stripes;
2888 }
2889 }
2890 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002891 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002892 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002893 free_extent_map(em);
2894 kfree(multi);
2895 goto again;
2896 }
Chris Mason593060d2008-03-25 16:50:33 -04002897 stripe_nr = offset;
2898 /*
2899 * stripe_nr counts the total number of stripes we have to stride
2900 * to get to this block
2901 */
2902 do_div(stripe_nr, map->stripe_len);
2903
2904 stripe_offset = stripe_nr * map->stripe_len;
2905 BUG_ON(offset < stripe_offset);
2906
2907 /* stripe_offset is the offset of this block in its stripe*/
2908 stripe_offset = offset - stripe_offset;
2909
Li Dongyangfce3bb92011-03-24 10:24:26 +00002910 if (rw & REQ_DISCARD)
2911 *length = min_t(u64, em->len - offset, *length);
2912 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2913 BTRFS_BLOCK_GROUP_RAID1 |
2914 BTRFS_BLOCK_GROUP_RAID10 |
2915 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002916 /* we limit the length of each bio to what fits in a stripe */
2917 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002918 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002919 } else {
2920 *length = em->len - offset;
2921 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002922
Jens Axboe7eaceac2011-03-10 08:52:07 +01002923 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002924 goto out;
2925
Chris Masonf2d8d742008-04-21 10:03:05 -04002926 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002927 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002928 stripe_nr_orig = stripe_nr;
2929 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2930 (~(map->stripe_len - 1));
2931 do_div(stripe_nr_end, map->stripe_len);
2932 stripe_end_offset = stripe_nr_end * map->stripe_len -
2933 (offset + *length);
2934 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2935 if (rw & REQ_DISCARD)
2936 num_stripes = min_t(u64, map->num_stripes,
2937 stripe_nr_end - stripe_nr_orig);
2938 stripe_index = do_div(stripe_nr, map->num_stripes);
2939 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07002940 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002941 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002942 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002943 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002944 else {
2945 stripe_index = find_live_mirror(map, 0,
2946 map->num_stripes,
2947 current->pid % map->num_stripes);
2948 }
Chris Mason2fff7342008-04-29 14:12:09 -04002949
Chris Mason611f0e02008-04-03 16:29:03 -04002950 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00002951 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002952 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002953 else if (mirror_num)
2954 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002955
Chris Mason321aecc2008-04-16 10:49:51 -04002956 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2957 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002958
2959 stripe_index = do_div(stripe_nr, factor);
2960 stripe_index *= map->sub_stripes;
2961
Jens Axboe7eaceac2011-03-10 08:52:07 +01002962 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04002963 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002964 else if (rw & REQ_DISCARD)
2965 num_stripes = min_t(u64, map->sub_stripes *
2966 (stripe_nr_end - stripe_nr_orig),
2967 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04002968 else if (mirror_num)
2969 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002970 else {
2971 stripe_index = find_live_mirror(map, stripe_index,
2972 map->sub_stripes, stripe_index +
2973 current->pid % map->sub_stripes);
2974 }
Chris Mason8790d502008-04-03 16:29:03 -04002975 } else {
2976 /*
2977 * after this do_div call, stripe_nr is the number of stripes
2978 * on this device we have to walk to find the data, and
2979 * stripe_index is the number of our device in the stripe array
2980 */
2981 stripe_index = do_div(stripe_nr, map->num_stripes);
2982 }
Chris Mason593060d2008-03-25 16:50:33 -04002983 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002984
Li Dongyangfce3bb92011-03-24 10:24:26 +00002985 if (rw & REQ_DISCARD) {
2986 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002987 multi->stripes[i].physical =
2988 map->stripes[stripe_index].physical +
2989 stripe_offset + stripe_nr * map->stripe_len;
2990 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002991
2992 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2993 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04002994 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002995 int j;
2996
Chris Masond9d04872011-03-27 21:23:21 -04002997 div_u64_rem(stripe_nr_end - 1,
2998 map->num_stripes,
2999 &last_stripe);
3000
Li Dongyangfce3bb92011-03-24 10:24:26 +00003001 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003002 u32 test;
3003
3004 div_u64_rem(stripe_nr_end - 1 - j,
3005 map->num_stripes, &test);
3006 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003007 break;
3008 }
3009 stripes = stripe_nr_end - 1 - j;
3010 do_div(stripes, map->num_stripes);
3011 multi->stripes[i].length = map->stripe_len *
3012 (stripes - stripe_nr + 1);
3013
3014 if (i == 0) {
3015 multi->stripes[i].length -=
3016 stripe_offset;
3017 stripe_offset = 0;
3018 }
3019 if (stripe_index == last_stripe)
3020 multi->stripes[i].length -=
3021 stripe_end_offset;
3022 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3023 u64 stripes;
3024 int j;
3025 int factor = map->num_stripes /
3026 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003027 u32 last_stripe = 0;
3028
3029 div_u64_rem(stripe_nr_end - 1,
3030 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003031 last_stripe *= map->sub_stripes;
3032
3033 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003034 u32 test;
3035
3036 div_u64_rem(stripe_nr_end - 1 - j,
3037 factor, &test);
3038
3039 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003040 stripe_index / map->sub_stripes)
3041 break;
3042 }
3043 stripes = stripe_nr_end - 1 - j;
3044 do_div(stripes, factor);
3045 multi->stripes[i].length = map->stripe_len *
3046 (stripes - stripe_nr + 1);
3047
3048 if (i < map->sub_stripes) {
3049 multi->stripes[i].length -=
3050 stripe_offset;
3051 if (i == map->sub_stripes - 1)
3052 stripe_offset = 0;
3053 }
3054 if (stripe_index >= last_stripe &&
3055 stripe_index <= (last_stripe +
3056 map->sub_stripes - 1)) {
3057 multi->stripes[i].length -=
3058 stripe_end_offset;
3059 }
3060 } else
3061 multi->stripes[i].length = *length;
3062
3063 stripe_index++;
3064 if (stripe_index == map->num_stripes) {
3065 /* This could only happen for RAID0/10 */
3066 stripe_index = 0;
3067 stripe_nr++;
3068 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003069 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003070 } else {
3071 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003072 multi->stripes[i].physical =
3073 map->stripes[stripe_index].physical +
3074 stripe_offset +
3075 stripe_nr * map->stripe_len;
3076 multi->stripes[i].dev =
3077 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003078 stripe_index++;
3079 }
Chris Mason593060d2008-03-25 16:50:33 -04003080 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003081 if (multi_ret) {
3082 *multi_ret = multi;
3083 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003084 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003085 }
Chris Masoncea9e442008-04-09 16:28:12 -04003086out:
Chris Mason0b86a832008-03-24 15:01:56 -04003087 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003088 return 0;
3089}
3090
Chris Masonf2d8d742008-04-21 10:03:05 -04003091int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3092 u64 logical, u64 *length,
3093 struct btrfs_multi_bio **multi_ret, int mirror_num)
3094{
3095 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003096 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003097}
3098
Yan Zhenga512bbf2008-12-08 16:46:26 -05003099int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3100 u64 chunk_start, u64 physical, u64 devid,
3101 u64 **logical, int *naddrs, int *stripe_len)
3102{
3103 struct extent_map_tree *em_tree = &map_tree->map_tree;
3104 struct extent_map *em;
3105 struct map_lookup *map;
3106 u64 *buf;
3107 u64 bytenr;
3108 u64 length;
3109 u64 stripe_nr;
3110 int i, j, nr = 0;
3111
Chris Mason890871b2009-09-02 16:24:52 -04003112 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003113 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003114 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003115
3116 BUG_ON(!em || em->start != chunk_start);
3117 map = (struct map_lookup *)em->bdev;
3118
3119 length = em->len;
3120 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3121 do_div(length, map->num_stripes / map->sub_stripes);
3122 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3123 do_div(length, map->num_stripes);
3124
3125 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3126 BUG_ON(!buf);
3127
3128 for (i = 0; i < map->num_stripes; i++) {
3129 if (devid && map->stripes[i].dev->devid != devid)
3130 continue;
3131 if (map->stripes[i].physical > physical ||
3132 map->stripes[i].physical + length <= physical)
3133 continue;
3134
3135 stripe_nr = physical - map->stripes[i].physical;
3136 do_div(stripe_nr, map->stripe_len);
3137
3138 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3139 stripe_nr = stripe_nr * map->num_stripes + i;
3140 do_div(stripe_nr, map->sub_stripes);
3141 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3142 stripe_nr = stripe_nr * map->num_stripes + i;
3143 }
3144 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003145 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003146 for (j = 0; j < nr; j++) {
3147 if (buf[j] == bytenr)
3148 break;
3149 }
Chris Mason934d3752008-12-08 16:43:10 -05003150 if (j == nr) {
3151 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003152 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003153 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003154 }
3155
Yan Zhenga512bbf2008-12-08 16:46:26 -05003156 *logical = buf;
3157 *naddrs = nr;
3158 *stripe_len = map->stripe_len;
3159
3160 free_extent_map(em);
3161 return 0;
3162}
3163
Chris Mason8790d502008-04-03 16:29:03 -04003164static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003165{
Chris Masoncea9e442008-04-09 16:28:12 -04003166 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003167 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003168
Chris Mason8790d502008-04-03 16:29:03 -04003169 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003170 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003171
Chris Mason7d2b4da2008-08-05 10:13:57 -04003172 if (bio == multi->orig_bio)
3173 is_orig_bio = 1;
3174
Chris Masoncea9e442008-04-09 16:28:12 -04003175 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003176 if (!is_orig_bio) {
3177 bio_put(bio);
3178 bio = multi->orig_bio;
3179 }
Chris Mason8790d502008-04-03 16:29:03 -04003180 bio->bi_private = multi->private;
3181 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003182 /* only send an error to the higher layers if it is
3183 * beyond the tolerance of the multi-bio
3184 */
Chris Mason1259ab72008-05-12 13:39:03 -04003185 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003186 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003187 } else if (err) {
3188 /*
3189 * this bio is actually up to date, we didn't
3190 * go over the max number of errors
3191 */
3192 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003193 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003194 }
Chris Mason8790d502008-04-03 16:29:03 -04003195 kfree(multi);
3196
3197 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003198 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003199 bio_put(bio);
3200 }
Chris Mason8790d502008-04-03 16:29:03 -04003201}
3202
Chris Mason8b712842008-06-11 16:50:36 -04003203struct async_sched {
3204 struct bio *bio;
3205 int rw;
3206 struct btrfs_fs_info *info;
3207 struct btrfs_work work;
3208};
3209
3210/*
3211 * see run_scheduled_bios for a description of why bios are collected for
3212 * async submit.
3213 *
3214 * This will add one bio to the pending list for a device and make sure
3215 * the work struct is scheduled.
3216 */
Chris Masond3977122009-01-05 21:25:51 -05003217static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003218 struct btrfs_device *device,
3219 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003220{
3221 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003222 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003223
3224 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003225 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04003226 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003227 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04003228 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003229 return 0;
3230 }
3231
3232 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003233 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003234 * higher layers. Otherwise, the async bio makes it appear we have
3235 * made progress against dirty pages when we've really just put it
3236 * on a queue for later
3237 */
Chris Mason0986fe92008-08-15 15:34:15 -04003238 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04003239 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003240 bio->bi_next = NULL;
3241 bio->bi_rw |= rw;
3242
3243 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003244 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003245 pending_bios = &device->pending_sync_bios;
3246 else
3247 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003248
Chris Masonffbd5172009-04-20 15:50:09 -04003249 if (pending_bios->tail)
3250 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003251
Chris Masonffbd5172009-04-20 15:50:09 -04003252 pending_bios->tail = bio;
3253 if (!pending_bios->head)
3254 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003255 if (device->running_pending)
3256 should_queue = 0;
3257
3258 spin_unlock(&device->io_lock);
3259
3260 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003261 btrfs_queue_worker(&root->fs_info->submit_workers,
3262 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003263 return 0;
3264}
3265
Chris Masonf1885912008-04-09 16:28:12 -04003266int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003267 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003268{
3269 struct btrfs_mapping_tree *map_tree;
3270 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003271 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003272 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003273 u64 length = 0;
3274 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003275 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003276 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003277 int dev_nr = 0;
3278 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003279
Chris Masonf2d8d742008-04-21 10:03:05 -04003280 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003281 map_tree = &root->fs_info->mapping_tree;
3282 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003283
Chris Masonf1885912008-04-09 16:28:12 -04003284 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3285 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003286 BUG_ON(ret);
3287
3288 total_devs = multi->num_stripes;
3289 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003290 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3291 "len %llu\n", (unsigned long long)logical,
3292 (unsigned long long)length,
3293 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003294 BUG();
3295 }
3296 multi->end_io = first_bio->bi_end_io;
3297 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003298 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003299 atomic_set(&multi->stripes_pending, multi->num_stripes);
3300
Chris Masond3977122009-01-05 21:25:51 -05003301 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003302 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003303 if (dev_nr < total_devs - 1) {
3304 bio = bio_clone(first_bio, GFP_NOFS);
3305 BUG_ON(!bio);
3306 } else {
3307 bio = first_bio;
3308 }
3309 bio->bi_private = multi;
3310 bio->bi_end_io = end_bio_multi_stripe;
3311 }
Chris Masoncea9e442008-04-09 16:28:12 -04003312 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3313 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003314 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003315 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003316 if (async_submit)
3317 schedule_bio(root, dev, rw, bio);
3318 else
3319 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003320 } else {
3321 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3322 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003323 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003324 }
Chris Mason8790d502008-04-03 16:29:03 -04003325 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003326 }
Chris Masoncea9e442008-04-09 16:28:12 -04003327 if (total_devs == 1)
3328 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003329 return 0;
3330}
3331
Chris Masona4437552008-04-18 10:29:38 -04003332struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003333 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003334{
Yan Zheng2b820322008-11-17 21:11:30 -05003335 struct btrfs_device *device;
3336 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003337
Yan Zheng2b820322008-11-17 21:11:30 -05003338 cur_devices = root->fs_info->fs_devices;
3339 while (cur_devices) {
3340 if (!fsid ||
3341 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3342 device = __find_device(&cur_devices->devices,
3343 devid, uuid);
3344 if (device)
3345 return device;
3346 }
3347 cur_devices = cur_devices->seed;
3348 }
3349 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003350}
3351
Chris Masondfe25022008-05-13 13:46:40 -04003352static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3353 u64 devid, u8 *dev_uuid)
3354{
3355 struct btrfs_device *device;
3356 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3357
3358 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003359 if (!device)
3360 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003361 list_add(&device->dev_list,
3362 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003363 device->dev_root = root->fs_info->dev_root;
3364 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003365 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003366 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003367 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003368 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003369 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003370 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003371 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003372 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3373 return device;
3374}
3375
Chris Mason0b86a832008-03-24 15:01:56 -04003376static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3377 struct extent_buffer *leaf,
3378 struct btrfs_chunk *chunk)
3379{
3380 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3381 struct map_lookup *map;
3382 struct extent_map *em;
3383 u64 logical;
3384 u64 length;
3385 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003386 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003387 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003388 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003389 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003390
Chris Masone17cade2008-04-15 15:41:47 -04003391 logical = key->offset;
3392 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003393
Chris Mason890871b2009-09-02 16:24:52 -04003394 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003395 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003396 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003397
3398 /* already mapped? */
3399 if (em && em->start <= logical && em->start + em->len > logical) {
3400 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003401 return 0;
3402 } else if (em) {
3403 free_extent_map(em);
3404 }
Chris Mason0b86a832008-03-24 15:01:56 -04003405
David Sterba172ddd62011-04-21 00:48:27 +02003406 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003407 if (!em)
3408 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003409 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3410 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003411 if (!map) {
3412 free_extent_map(em);
3413 return -ENOMEM;
3414 }
3415
3416 em->bdev = (struct block_device *)map;
3417 em->start = logical;
3418 em->len = length;
3419 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003420 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003421
Chris Mason593060d2008-03-25 16:50:33 -04003422 map->num_stripes = num_stripes;
3423 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3424 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3425 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3426 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3427 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003428 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003429 for (i = 0; i < num_stripes; i++) {
3430 map->stripes[i].physical =
3431 btrfs_stripe_offset_nr(leaf, chunk, i);
3432 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003433 read_extent_buffer(leaf, uuid, (unsigned long)
3434 btrfs_stripe_dev_uuid_nr(chunk, i),
3435 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003436 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3437 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003438 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003439 kfree(map);
3440 free_extent_map(em);
3441 return -EIO;
3442 }
Chris Masondfe25022008-05-13 13:46:40 -04003443 if (!map->stripes[i].dev) {
3444 map->stripes[i].dev =
3445 add_missing_dev(root, devid, uuid);
3446 if (!map->stripes[i].dev) {
3447 kfree(map);
3448 free_extent_map(em);
3449 return -EIO;
3450 }
3451 }
3452 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003453 }
3454
Chris Mason890871b2009-09-02 16:24:52 -04003455 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003456 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003457 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003458 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003459 free_extent_map(em);
3460
3461 return 0;
3462}
3463
3464static int fill_device_from_item(struct extent_buffer *leaf,
3465 struct btrfs_dev_item *dev_item,
3466 struct btrfs_device *device)
3467{
3468 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003469
3470 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003471 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3472 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003473 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3474 device->type = btrfs_device_type(leaf, dev_item);
3475 device->io_align = btrfs_device_io_align(leaf, dev_item);
3476 device->io_width = btrfs_device_io_width(leaf, dev_item);
3477 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003478
3479 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003480 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003481
Chris Mason0b86a832008-03-24 15:01:56 -04003482 return 0;
3483}
3484
Yan Zheng2b820322008-11-17 21:11:30 -05003485static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3486{
3487 struct btrfs_fs_devices *fs_devices;
3488 int ret;
3489
3490 mutex_lock(&uuid_mutex);
3491
3492 fs_devices = root->fs_info->fs_devices->seed;
3493 while (fs_devices) {
3494 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3495 ret = 0;
3496 goto out;
3497 }
3498 fs_devices = fs_devices->seed;
3499 }
3500
3501 fs_devices = find_fsid(fsid);
3502 if (!fs_devices) {
3503 ret = -ENOENT;
3504 goto out;
3505 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003506
3507 fs_devices = clone_fs_devices(fs_devices);
3508 if (IS_ERR(fs_devices)) {
3509 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003510 goto out;
3511 }
3512
Christoph Hellwig97288f22008-12-02 06:36:09 -05003513 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003514 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003515 if (ret)
3516 goto out;
3517
3518 if (!fs_devices->seeding) {
3519 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003520 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003521 ret = -EINVAL;
3522 goto out;
3523 }
3524
3525 fs_devices->seed = root->fs_info->fs_devices->seed;
3526 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003527out:
3528 mutex_unlock(&uuid_mutex);
3529 return ret;
3530}
3531
Chris Mason0d81ba52008-03-24 15:02:07 -04003532static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003533 struct extent_buffer *leaf,
3534 struct btrfs_dev_item *dev_item)
3535{
3536 struct btrfs_device *device;
3537 u64 devid;
3538 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003539 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003540 u8 dev_uuid[BTRFS_UUID_SIZE];
3541
Chris Mason0b86a832008-03-24 15:01:56 -04003542 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003543 read_extent_buffer(leaf, dev_uuid,
3544 (unsigned long)btrfs_device_uuid(dev_item),
3545 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003546 read_extent_buffer(leaf, fs_uuid,
3547 (unsigned long)btrfs_device_fsid(dev_item),
3548 BTRFS_UUID_SIZE);
3549
3550 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3551 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003552 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003553 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003554 }
3555
3556 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3557 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003558 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003559 return -EIO;
3560
3561 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003562 printk(KERN_WARNING "warning devid %llu missing\n",
3563 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003564 device = add_missing_dev(root, devid, dev_uuid);
3565 if (!device)
3566 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003567 } else if (!device->missing) {
3568 /*
3569 * this happens when a device that was properly setup
3570 * in the device info lists suddenly goes bad.
3571 * device->bdev is NULL, and so we have to set
3572 * device->missing to one here
3573 */
3574 root->fs_info->fs_devices->missing_devices++;
3575 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003576 }
3577 }
3578
3579 if (device->fs_devices != root->fs_info->fs_devices) {
3580 BUG_ON(device->writeable);
3581 if (device->generation !=
3582 btrfs_device_generation(leaf, dev_item))
3583 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003584 }
Chris Mason0b86a832008-03-24 15:01:56 -04003585
3586 fill_device_from_item(leaf, dev_item, device);
3587 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003588 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003589 if (device->writeable)
3590 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003591 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003592 return ret;
3593}
3594
Yan Zhenge4404d62008-12-12 10:03:26 -05003595int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003596{
3597 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003598 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003599 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003600 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003601 u8 *ptr;
3602 unsigned long sb_ptr;
3603 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003604 u32 num_stripes;
3605 u32 array_size;
3606 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003607 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003608 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003609
Yan Zhenge4404d62008-12-12 10:03:26 -05003610 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003611 BTRFS_SUPER_INFO_SIZE);
3612 if (!sb)
3613 return -ENOMEM;
3614 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003615 btrfs_set_buffer_lockdep_class(sb, 0);
3616
Chris Masona061fc82008-05-07 11:43:44 -04003617 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003618 array_size = btrfs_super_sys_array_size(super_copy);
3619
Chris Mason0b86a832008-03-24 15:01:56 -04003620 ptr = super_copy->sys_chunk_array;
3621 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3622 cur = 0;
3623
3624 while (cur < array_size) {
3625 disk_key = (struct btrfs_disk_key *)ptr;
3626 btrfs_disk_key_to_cpu(&key, disk_key);
3627
Chris Masona061fc82008-05-07 11:43:44 -04003628 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003629 sb_ptr += len;
3630 cur += len;
3631
Chris Mason0d81ba52008-03-24 15:02:07 -04003632 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003633 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003634 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003635 if (ret)
3636 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003637 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3638 len = btrfs_chunk_item_size(num_stripes);
3639 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003640 ret = -EIO;
3641 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003642 }
3643 ptr += len;
3644 sb_ptr += len;
3645 cur += len;
3646 }
Chris Masona061fc82008-05-07 11:43:44 -04003647 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003648 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003649}
3650
3651int btrfs_read_chunk_tree(struct btrfs_root *root)
3652{
3653 struct btrfs_path *path;
3654 struct extent_buffer *leaf;
3655 struct btrfs_key key;
3656 struct btrfs_key found_key;
3657 int ret;
3658 int slot;
3659
3660 root = root->fs_info->chunk_root;
3661
3662 path = btrfs_alloc_path();
3663 if (!path)
3664 return -ENOMEM;
3665
3666 /* first we search for all of the device items, and then we
3667 * read in all of the chunk items. This way we can create chunk
3668 * mappings that reference all of the devices that are afound
3669 */
3670 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3671 key.offset = 0;
3672 key.type = 0;
3673again:
3674 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003675 if (ret < 0)
3676 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003677 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003678 leaf = path->nodes[0];
3679 slot = path->slots[0];
3680 if (slot >= btrfs_header_nritems(leaf)) {
3681 ret = btrfs_next_leaf(root, path);
3682 if (ret == 0)
3683 continue;
3684 if (ret < 0)
3685 goto error;
3686 break;
3687 }
3688 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3689 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3690 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3691 break;
3692 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3693 struct btrfs_dev_item *dev_item;
3694 dev_item = btrfs_item_ptr(leaf, slot,
3695 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003696 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003697 if (ret)
3698 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003699 }
3700 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3701 struct btrfs_chunk *chunk;
3702 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3703 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003704 if (ret)
3705 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003706 }
3707 path->slots[0]++;
3708 }
3709 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3710 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003711 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003712 goto again;
3713 }
Chris Mason0b86a832008-03-24 15:01:56 -04003714 ret = 0;
3715error:
Yan Zheng2b820322008-11-17 21:11:30 -05003716 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003717 return ret;
3718}