blob: 53875ae73ad4f634548e8d1d315056822c9cbd4b [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 Mason492bb6de2008-07-31 16:29:02 -0400229
230 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400231
Chris Mason5ff7ba32010-03-15 10:21:30 -0400232 submit_bio(cur->bi_rw, cur);
233 num_run++;
234 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100235 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400236 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400237
238 /*
239 * we made progress, there is more work to do and the bdi
240 * is now congested. Back off and let other work structs
241 * run instead
242 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400243 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500244 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400245 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400246
Chris Masonb765ead2009-04-03 10:27:10 -0400247 ioc = current->io_context;
248
249 /*
250 * the main goal here is that we don't want to
251 * block if we're going to be able to submit
252 * more requests without blocking.
253 *
254 * This code does two great things, it pokes into
255 * the elevator code from a filesystem _and_
256 * it makes assumptions about how batching works.
257 */
258 if (ioc && ioc->nr_batch_requests > 0 &&
259 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
260 (last_waited == 0 ||
261 ioc->last_waited == last_waited)) {
262 /*
263 * we want to go through our batch of
264 * requests and stop. So, we copy out
265 * the ioc->last_waited time and test
266 * against it before looping
267 */
268 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100269 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400270 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400271 continue;
272 }
Chris Mason8b712842008-06-11 16:50:36 -0400273 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400274 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500275 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400276
277 spin_unlock(&device->io_lock);
278 btrfs_requeue_work(&device->work);
279 goto done;
280 }
281 }
Chris Masonffbd5172009-04-20 15:50:09 -0400282
Chris Mason51684082010-03-10 15:33:32 -0500283 cond_resched();
284 if (again)
285 goto loop;
286
287 spin_lock(&device->io_lock);
288 if (device->pending_bios.head || device->pending_sync_bios.head)
289 goto loop_lock;
290 spin_unlock(&device->io_lock);
291
Chris Mason8b712842008-06-11 16:50:36 -0400292done:
Chris Mason211588a2011-04-19 20:12:40 -0400293 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400294 return 0;
295}
296
Christoph Hellwigb2950862008-12-02 09:54:17 -0500297static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400298{
299 struct btrfs_device *device;
300
301 device = container_of(work, struct btrfs_device, work);
302 run_scheduled_bios(device);
303}
304
Chris Masona1b32a52008-09-05 16:09:51 -0400305static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400306 struct btrfs_super_block *disk_super,
307 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
308{
309 struct btrfs_device *device;
310 struct btrfs_fs_devices *fs_devices;
311 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000312 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400313
314 fs_devices = find_fsid(disk_super->fsid);
315 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400316 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400317 if (!fs_devices)
318 return -ENOMEM;
319 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400320 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400321 list_add(&fs_devices->list, &fs_uuids);
322 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
323 fs_devices->latest_devid = devid;
324 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400325 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400326 device = NULL;
327 } else {
Chris Masona4437552008-04-18 10:29:38 -0400328 device = __find_device(&fs_devices->devices, devid,
329 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 }
331 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500332 if (fs_devices->opened)
333 return -EBUSY;
334
Chris Mason8a4b83c2008-03-24 15:02:07 -0400335 device = kzalloc(sizeof(*device), GFP_NOFS);
336 if (!device) {
337 /* we can safely leave the fs_devices entry around */
338 return -ENOMEM;
339 }
340 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400341 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400342 memcpy(device->uuid, disk_super->dev_item.uuid,
343 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400344 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400345 device->name = kstrdup(path, GFP_NOFS);
346 if (!device->name) {
347 kfree(device);
348 return -ENOMEM;
349 }
Yan Zheng2b820322008-11-17 21:11:30 -0500350 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400351
352 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000353 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400354 mutex_unlock(&fs_devices->device_list_mutex);
355
Yan Zheng2b820322008-11-17 21:11:30 -0500356 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400357 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500358 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000359 name = kstrdup(path, GFP_NOFS);
360 if (!name)
361 return -ENOMEM;
362 kfree(device->name);
363 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500364 if (device->missing) {
365 fs_devices->missing_devices--;
366 device->missing = 0;
367 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 }
369
370 if (found_transid > fs_devices->latest_trans) {
371 fs_devices->latest_devid = devid;
372 fs_devices->latest_trans = found_transid;
373 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400374 *fs_devices_ret = fs_devices;
375 return 0;
376}
377
Yan Zhenge4404d62008-12-12 10:03:26 -0500378static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
379{
380 struct btrfs_fs_devices *fs_devices;
381 struct btrfs_device *device;
382 struct btrfs_device *orig_dev;
383
384 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
385 if (!fs_devices)
386 return ERR_PTR(-ENOMEM);
387
388 INIT_LIST_HEAD(&fs_devices->devices);
389 INIT_LIST_HEAD(&fs_devices->alloc_list);
390 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400391 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500392 fs_devices->latest_devid = orig->latest_devid;
393 fs_devices->latest_trans = orig->latest_trans;
394 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
395
Xiao Guangrong46224702011-04-20 10:08:47 +0000396 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500397 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
398 device = kzalloc(sizeof(*device), GFP_NOFS);
399 if (!device)
400 goto error;
401
402 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400403 if (!device->name) {
404 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500405 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400406 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500407
408 device->devid = orig_dev->devid;
409 device->work.func = pending_bios_fn;
410 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500411 spin_lock_init(&device->io_lock);
412 INIT_LIST_HEAD(&device->dev_list);
413 INIT_LIST_HEAD(&device->dev_alloc_list);
414
415 list_add(&device->dev_list, &fs_devices->devices);
416 device->fs_devices = fs_devices;
417 fs_devices->num_devices++;
418 }
419 return fs_devices;
420error:
421 free_fs_devices(fs_devices);
422 return ERR_PTR(-ENOMEM);
423}
424
Chris Masondfe25022008-05-13 13:46:40 -0400425int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
426{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500427 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400428
429 mutex_lock(&uuid_mutex);
430again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000431 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500432 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500433 if (device->in_fs_metadata)
434 continue;
435
436 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100437 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500438 device->bdev = NULL;
439 fs_devices->open_devices--;
440 }
441 if (device->writeable) {
442 list_del_init(&device->dev_alloc_list);
443 device->writeable = 0;
444 fs_devices->rw_devices--;
445 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500446 list_del_init(&device->dev_list);
447 fs_devices->num_devices--;
448 kfree(device->name);
449 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400450 }
Yan Zheng2b820322008-11-17 21:11:30 -0500451
452 if (fs_devices->seed) {
453 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500454 goto again;
455 }
456
Chris Masondfe25022008-05-13 13:46:40 -0400457 mutex_unlock(&uuid_mutex);
458 return 0;
459}
Chris Masona0af4692008-05-13 16:03:06 -0400460
Xiao Guangrong1f781602011-04-20 10:09:16 +0000461static void __free_device(struct work_struct *work)
462{
463 struct btrfs_device *device;
464
465 device = container_of(work, struct btrfs_device, rcu_work);
466
467 if (device->bdev)
468 blkdev_put(device->bdev, device->mode);
469
470 kfree(device->name);
471 kfree(device);
472}
473
474static void free_device(struct rcu_head *head)
475{
476 struct btrfs_device *device;
477
478 device = container_of(head, struct btrfs_device, rcu);
479
480 INIT_WORK(&device->rcu_work, __free_device);
481 schedule_work(&device->rcu_work);
482}
483
Yan Zheng2b820322008-11-17 21:11:30 -0500484static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400486 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500487
Yan Zheng2b820322008-11-17 21:11:30 -0500488 if (--fs_devices->opened > 0)
489 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400490
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000491 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500492 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000493 struct btrfs_device *new_device;
494
495 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400496 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000497
Yan Zheng2b820322008-11-17 21:11:30 -0500498 if (device->writeable) {
499 list_del_init(&device->dev_alloc_list);
500 fs_devices->rw_devices--;
501 }
502
Xiao Guangrong1f781602011-04-20 10:09:16 +0000503 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
504 BUG_ON(!new_device);
505 memcpy(new_device, device, sizeof(*new_device));
506 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000507 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000508 new_device->bdev = NULL;
509 new_device->writeable = 0;
510 new_device->in_fs_metadata = 0;
511 list_replace_rcu(&device->dev_list, &new_device->dev_list);
512
513 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400514 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000515 mutex_unlock(&fs_devices->device_list_mutex);
516
Yan Zhenge4404d62008-12-12 10:03:26 -0500517 WARN_ON(fs_devices->open_devices);
518 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500519 fs_devices->opened = 0;
520 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500521
Chris Mason8a4b83c2008-03-24 15:02:07 -0400522 return 0;
523}
524
Yan Zheng2b820322008-11-17 21:11:30 -0500525int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
526{
Yan Zhenge4404d62008-12-12 10:03:26 -0500527 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500528 int ret;
529
530 mutex_lock(&uuid_mutex);
531 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500532 if (!fs_devices->opened) {
533 seed_devices = fs_devices->seed;
534 fs_devices->seed = NULL;
535 }
Yan Zheng2b820322008-11-17 21:11:30 -0500536 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500537
538 while (seed_devices) {
539 fs_devices = seed_devices;
540 seed_devices = fs_devices->seed;
541 __btrfs_close_devices(fs_devices);
542 free_fs_devices(fs_devices);
543 }
Yan Zheng2b820322008-11-17 21:11:30 -0500544 return ret;
545}
546
Yan Zhenge4404d62008-12-12 10:03:26 -0500547static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
548 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549{
550 struct block_device *bdev;
551 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400552 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400553 struct block_device *latest_bdev = NULL;
554 struct buffer_head *bh;
555 struct btrfs_super_block *disk_super;
556 u64 latest_devid = 0;
557 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400558 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500559 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400560 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400561
Tejun Heod4d77622010-11-13 11:55:18 +0100562 flags |= FMODE_EXCL;
563
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500564 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400565 if (device->bdev)
566 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400567 if (!device->name)
568 continue;
569
Tejun Heod4d77622010-11-13 11:55:18 +0100570 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500572 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400573 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 }
Chris Masona061fc82008-05-07 11:43:44 -0400575 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400576
Yan Zhenga512bbf2008-12-08 16:46:26 -0500577 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000578 if (!bh) {
579 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400580 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000581 }
Chris Masona0af4692008-05-13 16:03:06 -0400582
583 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000584 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400585 if (devid != device->devid)
586 goto error_brelse;
587
Yan Zheng2b820322008-11-17 21:11:30 -0500588 if (memcmp(device->uuid, disk_super->dev_item.uuid,
589 BTRFS_UUID_SIZE))
590 goto error_brelse;
591
592 device->generation = btrfs_super_generation(disk_super);
593 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400594 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500595 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400596 latest_bdev = bdev;
597 }
598
Yan Zheng2b820322008-11-17 21:11:30 -0500599 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
600 device->writeable = 0;
601 } else {
602 device->writeable = !bdev_read_only(bdev);
603 seeding = 0;
604 }
605
Chris Mason8a4b83c2008-03-24 15:02:07 -0400606 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400607 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500608 device->mode = flags;
609
Chris Masonc2898112009-06-10 09:51:32 -0400610 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
611 fs_devices->rotating = 1;
612
Chris Masona0af4692008-05-13 16:03:06 -0400613 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500614 if (device->writeable) {
615 fs_devices->rw_devices++;
616 list_add(&device->dev_alloc_list,
617 &fs_devices->alloc_list);
618 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000619 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400620 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400621
Chris Masona0af4692008-05-13 16:03:06 -0400622error_brelse:
623 brelse(bh);
624error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100625 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400626error:
627 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400628 }
Chris Masona0af4692008-05-13 16:03:06 -0400629 if (fs_devices->open_devices == 0) {
630 ret = -EIO;
631 goto out;
632 }
Yan Zheng2b820322008-11-17 21:11:30 -0500633 fs_devices->seeding = seeding;
634 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400635 fs_devices->latest_bdev = latest_bdev;
636 fs_devices->latest_devid = latest_devid;
637 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500638 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400639out:
Yan Zheng2b820322008-11-17 21:11:30 -0500640 return ret;
641}
642
643int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500644 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500645{
646 int ret;
647
648 mutex_lock(&uuid_mutex);
649 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500650 fs_devices->opened++;
651 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500652 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500653 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500654 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400655 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400656 return ret;
657}
658
Christoph Hellwig97288f22008-12-02 06:36:09 -0500659int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400660 struct btrfs_fs_devices **fs_devices_ret)
661{
662 struct btrfs_super_block *disk_super;
663 struct block_device *bdev;
664 struct buffer_head *bh;
665 int ret;
666 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400667 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400668
669 mutex_lock(&uuid_mutex);
670
Tejun Heod4d77622010-11-13 11:55:18 +0100671 flags |= FMODE_EXCL;
672 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400673
674 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400675 ret = PTR_ERR(bdev);
676 goto error;
677 }
678
679 ret = set_blocksize(bdev, 4096);
680 if (ret)
681 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500682 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400683 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000684 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685 goto error_close;
686 }
687 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000688 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400689 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400690 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500691 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200692 else
693 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500694 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500695 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
697
Chris Mason8a4b83c2008-03-24 15:02:07 -0400698 brelse(bh);
699error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100700 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701error:
702 mutex_unlock(&uuid_mutex);
703 return ret;
704}
Chris Mason0b86a832008-03-24 15:01:56 -0400705
Miao Xie6d07bce2011-01-05 10:07:31 +0000706/* helper to account the used device space in the range */
707int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
708 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400709{
710 struct btrfs_key key;
711 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000712 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500713 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000714 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400715 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000716 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400717 struct extent_buffer *l;
718
Miao Xie6d07bce2011-01-05 10:07:31 +0000719 *length = 0;
720
721 if (start >= device->total_bytes)
722 return 0;
723
Yan Zheng2b820322008-11-17 21:11:30 -0500724 path = btrfs_alloc_path();
725 if (!path)
726 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400727 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400728
Chris Mason0b86a832008-03-24 15:01:56 -0400729 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000730 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400731 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000732
733 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400734 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000735 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400736 if (ret > 0) {
737 ret = btrfs_previous_item(root, path, key.objectid, key.type);
738 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000739 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400740 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000741
Chris Mason0b86a832008-03-24 15:01:56 -0400742 while (1) {
743 l = path->nodes[0];
744 slot = path->slots[0];
745 if (slot >= btrfs_header_nritems(l)) {
746 ret = btrfs_next_leaf(root, path);
747 if (ret == 0)
748 continue;
749 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000750 goto out;
751
752 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400753 }
754 btrfs_item_key_to_cpu(l, &key, slot);
755
756 if (key.objectid < device->devid)
757 goto next;
758
759 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000760 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400761
Chris Masond3977122009-01-05 21:25:51 -0500762 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400763 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400764
Chris Mason0b86a832008-03-24 15:01:56 -0400765 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000766 extent_end = key.offset + btrfs_dev_extent_length(l,
767 dev_extent);
768 if (key.offset <= start && extent_end > end) {
769 *length = end - start + 1;
770 break;
771 } else if (key.offset <= start && extent_end > start)
772 *length += extent_end - start;
773 else if (key.offset > start && extent_end <= end)
774 *length += extent_end - key.offset;
775 else if (key.offset > start && key.offset <= end) {
776 *length += end - key.offset + 1;
777 break;
778 } else if (key.offset > end)
779 break;
780
781next:
782 path->slots[0]++;
783 }
784 ret = 0;
785out:
786 btrfs_free_path(path);
787 return ret;
788}
789
Chris Mason0b86a832008-03-24 15:01:56 -0400790/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000791 * find_free_dev_extent - find free space in the specified device
792 * @trans: transaction handler
793 * @device: the device which we search the free space in
794 * @num_bytes: the size of the free space that we need
795 * @start: store the start of the free space.
796 * @len: the size of the free space. that we find, or the size of the max
797 * free space if we don't find suitable free space
798 *
Chris Mason0b86a832008-03-24 15:01:56 -0400799 * this uses a pretty simple search, the expectation is that it is
800 * called very infrequently and that a given device has a small number
801 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000802 *
803 * @start is used to store the start of the free space if we find. But if we
804 * don't find suitable free space, it will be used to store the start position
805 * of the max free space.
806 *
807 * @len is used to store the size of the free space that we find.
808 * But if we don't find suitable free space, it is used to store the size of
809 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400810 */
811int find_free_dev_extent(struct btrfs_trans_handle *trans,
812 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000813 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400814{
815 struct btrfs_key key;
816 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000817 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400818 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000819 u64 hole_size;
820 u64 max_hole_start;
821 u64 max_hole_size;
822 u64 extent_end;
823 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400824 u64 search_end = device->total_bytes;
825 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000826 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400827 struct extent_buffer *l;
828
Chris Mason0b86a832008-03-24 15:01:56 -0400829 /* FIXME use last free of some kind */
830
831 /* we don't want to overwrite the superblock on the drive,
832 * so we make sure to start at an offset of at least 1MB
833 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200834 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400835
Miao Xie7bfc8372011-01-05 10:07:26 +0000836 max_hole_start = search_start;
837 max_hole_size = 0;
838
839 if (search_start >= search_end) {
840 ret = -ENOSPC;
841 goto error;
842 }
843
844 path = btrfs_alloc_path();
845 if (!path) {
846 ret = -ENOMEM;
847 goto error;
848 }
849 path->reada = 2;
850
Chris Mason0b86a832008-03-24 15:01:56 -0400851 key.objectid = device->devid;
852 key.offset = search_start;
853 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000854
Chris Mason0b86a832008-03-24 15:01:56 -0400855 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
856 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400858 if (ret > 0) {
859 ret = btrfs_previous_item(root, path, key.objectid, key.type);
860 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000861 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400862 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000863
Chris Mason0b86a832008-03-24 15:01:56 -0400864 while (1) {
865 l = path->nodes[0];
866 slot = path->slots[0];
867 if (slot >= btrfs_header_nritems(l)) {
868 ret = btrfs_next_leaf(root, path);
869 if (ret == 0)
870 continue;
871 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000872 goto out;
873
874 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400875 }
876 btrfs_item_key_to_cpu(l, &key, slot);
877
878 if (key.objectid < device->devid)
879 goto next;
880
881 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000882 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400883
Chris Mason0b86a832008-03-24 15:01:56 -0400884 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
885 goto next;
886
Miao Xie7bfc8372011-01-05 10:07:26 +0000887 if (key.offset > search_start) {
888 hole_size = key.offset - search_start;
889
890 if (hole_size > max_hole_size) {
891 max_hole_start = search_start;
892 max_hole_size = hole_size;
893 }
894
895 /*
896 * If this free space is greater than which we need,
897 * it must be the max free space that we have found
898 * until now, so max_hole_start must point to the start
899 * of this free space and the length of this free space
900 * is stored in max_hole_size. Thus, we return
901 * max_hole_start and max_hole_size and go back to the
902 * caller.
903 */
904 if (hole_size >= num_bytes) {
905 ret = 0;
906 goto out;
907 }
908 }
909
Chris Mason0b86a832008-03-24 15:01:56 -0400910 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 extent_end = key.offset + btrfs_dev_extent_length(l,
912 dev_extent);
913 if (extent_end > search_start)
914 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400915next:
916 path->slots[0]++;
917 cond_resched();
918 }
Chris Mason0b86a832008-03-24 15:01:56 -0400919
Miao Xie7bfc8372011-01-05 10:07:26 +0000920 hole_size = search_end- search_start;
921 if (hole_size > max_hole_size) {
922 max_hole_start = search_start;
923 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400924 }
Chris Mason0b86a832008-03-24 15:01:56 -0400925
Miao Xie7bfc8372011-01-05 10:07:26 +0000926 /* See above. */
927 if (hole_size < num_bytes)
928 ret = -ENOSPC;
929 else
930 ret = 0;
931
932out:
Yan Zheng2b820322008-11-17 21:11:30 -0500933 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000934error:
935 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000936 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400938 return ret;
939}
940
Christoph Hellwigb2950862008-12-02 09:54:17 -0500941static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400942 struct btrfs_device *device,
943 u64 start)
944{
945 int ret;
946 struct btrfs_path *path;
947 struct btrfs_root *root = device->dev_root;
948 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400949 struct btrfs_key found_key;
950 struct extent_buffer *leaf = NULL;
951 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400952
953 path = btrfs_alloc_path();
954 if (!path)
955 return -ENOMEM;
956
957 key.objectid = device->devid;
958 key.offset = start;
959 key.type = BTRFS_DEV_EXTENT_KEY;
960
961 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400962 if (ret > 0) {
963 ret = btrfs_previous_item(root, path, key.objectid,
964 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000965 if (ret)
966 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400967 leaf = path->nodes[0];
968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
969 extent = btrfs_item_ptr(leaf, path->slots[0],
970 struct btrfs_dev_extent);
971 BUG_ON(found_key.offset > start || found_key.offset +
972 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400973 } else if (ret == 0) {
974 leaf = path->nodes[0];
975 extent = btrfs_item_ptr(leaf, path->slots[0],
976 struct btrfs_dev_extent);
977 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400978 BUG_ON(ret);
979
Chris Masondfe25022008-05-13 13:46:40 -0400980 if (device->bytes_used > 0)
981 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400982 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -0400983
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000984out:
Chris Mason8f18cf12008-04-25 16:53:30 -0400985 btrfs_free_path(path);
986 return ret;
987}
988
Yan Zheng2b820322008-11-17 21:11:30 -0500989int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400990 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400991 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500992 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400993{
994 int ret;
995 struct btrfs_path *path;
996 struct btrfs_root *root = device->dev_root;
997 struct btrfs_dev_extent *extent;
998 struct extent_buffer *leaf;
999 struct btrfs_key key;
1000
Chris Masondfe25022008-05-13 13:46:40 -04001001 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001002 path = btrfs_alloc_path();
1003 if (!path)
1004 return -ENOMEM;
1005
Chris Mason0b86a832008-03-24 15:01:56 -04001006 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001007 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001008 key.type = BTRFS_DEV_EXTENT_KEY;
1009 ret = btrfs_insert_empty_item(trans, root, path, &key,
1010 sizeof(*extent));
1011 BUG_ON(ret);
1012
1013 leaf = path->nodes[0];
1014 extent = btrfs_item_ptr(leaf, path->slots[0],
1015 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001016 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1017 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1018 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1019
1020 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1021 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1022 BTRFS_UUID_SIZE);
1023
Chris Mason0b86a832008-03-24 15:01:56 -04001024 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1025 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001026 btrfs_free_path(path);
1027 return ret;
1028}
1029
Chris Masona1b32a52008-09-05 16:09:51 -04001030static noinline int find_next_chunk(struct btrfs_root *root,
1031 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001032{
1033 struct btrfs_path *path;
1034 int ret;
1035 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001036 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001037 struct btrfs_key found_key;
1038
1039 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001040 if (!path)
1041 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001042
Chris Masone17cade2008-04-15 15:41:47 -04001043 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001044 key.offset = (u64)-1;
1045 key.type = BTRFS_CHUNK_ITEM_KEY;
1046
1047 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1048 if (ret < 0)
1049 goto error;
1050
1051 BUG_ON(ret == 0);
1052
1053 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1054 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001055 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001056 } else {
1057 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1058 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001059 if (found_key.objectid != objectid)
1060 *offset = 0;
1061 else {
1062 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1063 struct btrfs_chunk);
1064 *offset = found_key.offset +
1065 btrfs_chunk_length(path->nodes[0], chunk);
1066 }
Chris Mason0b86a832008-03-24 15:01:56 -04001067 }
1068 ret = 0;
1069error:
1070 btrfs_free_path(path);
1071 return ret;
1072}
1073
Yan Zheng2b820322008-11-17 21:11:30 -05001074static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001075{
1076 int ret;
1077 struct btrfs_key key;
1078 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001079 struct btrfs_path *path;
1080
1081 root = root->fs_info->chunk_root;
1082
1083 path = btrfs_alloc_path();
1084 if (!path)
1085 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001086
1087 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1088 key.type = BTRFS_DEV_ITEM_KEY;
1089 key.offset = (u64)-1;
1090
1091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1092 if (ret < 0)
1093 goto error;
1094
1095 BUG_ON(ret == 0);
1096
1097 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1098 BTRFS_DEV_ITEM_KEY);
1099 if (ret) {
1100 *objectid = 1;
1101 } else {
1102 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1103 path->slots[0]);
1104 *objectid = found_key.offset + 1;
1105 }
1106 ret = 0;
1107error:
Yan Zheng2b820322008-11-17 21:11:30 -05001108 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001109 return ret;
1110}
1111
1112/*
1113 * the device information is stored in the chunk root
1114 * the btrfs_device struct should be fully filled in
1115 */
1116int btrfs_add_device(struct btrfs_trans_handle *trans,
1117 struct btrfs_root *root,
1118 struct btrfs_device *device)
1119{
1120 int ret;
1121 struct btrfs_path *path;
1122 struct btrfs_dev_item *dev_item;
1123 struct extent_buffer *leaf;
1124 struct btrfs_key key;
1125 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001126
1127 root = root->fs_info->chunk_root;
1128
1129 path = btrfs_alloc_path();
1130 if (!path)
1131 return -ENOMEM;
1132
Chris Mason0b86a832008-03-24 15:01:56 -04001133 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1134 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001135 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001136
1137 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001138 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001139 if (ret)
1140 goto out;
1141
1142 leaf = path->nodes[0];
1143 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1144
1145 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001146 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001147 btrfs_set_device_type(leaf, dev_item, device->type);
1148 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1149 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1150 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001151 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1152 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001153 btrfs_set_device_group(leaf, dev_item, 0);
1154 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1155 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001156 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001157
Chris Mason0b86a832008-03-24 15:01:56 -04001158 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001159 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001160 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1161 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001162 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001163
Yan Zheng2b820322008-11-17 21:11:30 -05001164 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001165out:
1166 btrfs_free_path(path);
1167 return ret;
1168}
Chris Mason8f18cf12008-04-25 16:53:30 -04001169
Chris Masona061fc82008-05-07 11:43:44 -04001170static int btrfs_rm_dev_item(struct btrfs_root *root,
1171 struct btrfs_device *device)
1172{
1173 int ret;
1174 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001175 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001176 struct btrfs_trans_handle *trans;
1177
1178 root = root->fs_info->chunk_root;
1179
1180 path = btrfs_alloc_path();
1181 if (!path)
1182 return -ENOMEM;
1183
Yan, Zhenga22285a2010-05-16 10:48:46 -04001184 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001185 if (IS_ERR(trans)) {
1186 btrfs_free_path(path);
1187 return PTR_ERR(trans);
1188 }
Chris Masona061fc82008-05-07 11:43:44 -04001189 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1190 key.type = BTRFS_DEV_ITEM_KEY;
1191 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001192 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001193
1194 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1195 if (ret < 0)
1196 goto out;
1197
1198 if (ret > 0) {
1199 ret = -ENOENT;
1200 goto out;
1201 }
1202
1203 ret = btrfs_del_item(trans, root, path);
1204 if (ret)
1205 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001206out:
1207 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001208 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001209 btrfs_commit_transaction(trans, root);
1210 return ret;
1211}
1212
1213int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1214{
1215 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001216 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001217 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001218 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001219 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001220 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001221 u64 all_avail;
1222 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001223 u64 num_devices;
1224 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001225 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001226 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001227
Chris Masona061fc82008-05-07 11:43:44 -04001228 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001229 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001230
1231 all_avail = root->fs_info->avail_data_alloc_bits |
1232 root->fs_info->avail_system_alloc_bits |
1233 root->fs_info->avail_metadata_alloc_bits;
1234
1235 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001236 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001237 printk(KERN_ERR "btrfs: unable to go below four devices "
1238 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001239 ret = -EINVAL;
1240 goto out;
1241 }
1242
1243 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001244 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001245 printk(KERN_ERR "btrfs: unable to go below two "
1246 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001247 ret = -EINVAL;
1248 goto out;
1249 }
1250
Chris Masondfe25022008-05-13 13:46:40 -04001251 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001252 struct list_head *devices;
1253 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001254
Chris Masondfe25022008-05-13 13:46:40 -04001255 device = NULL;
1256 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001257 /*
1258 * It is safe to read the devices since the volume_mutex
1259 * is held.
1260 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001261 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001262 if (tmp->in_fs_metadata && !tmp->bdev) {
1263 device = tmp;
1264 break;
1265 }
1266 }
1267 bdev = NULL;
1268 bh = NULL;
1269 disk_super = NULL;
1270 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001271 printk(KERN_ERR "btrfs: no missing devices found to "
1272 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001273 goto out;
1274 }
Chris Masondfe25022008-05-13 13:46:40 -04001275 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001276 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1277 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001278 if (IS_ERR(bdev)) {
1279 ret = PTR_ERR(bdev);
1280 goto out;
1281 }
1282
Yan Zheng2b820322008-11-17 21:11:30 -05001283 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001284 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001285 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001286 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001287 goto error_close;
1288 }
1289 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001290 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001291 dev_uuid = disk_super->dev_item.uuid;
1292 device = btrfs_find_device(root, devid, dev_uuid,
1293 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001294 if (!device) {
1295 ret = -ENOENT;
1296 goto error_brelse;
1297 }
Chris Masondfe25022008-05-13 13:46:40 -04001298 }
Yan Zheng2b820322008-11-17 21:11:30 -05001299
1300 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001301 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1302 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001303 ret = -EINVAL;
1304 goto error_brelse;
1305 }
1306
1307 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001308 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001309 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001310 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001311 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001312 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001313 }
Chris Masona061fc82008-05-07 11:43:44 -04001314
1315 ret = btrfs_shrink_device(device, 0);
1316 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001317 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001318
Chris Masona061fc82008-05-07 11:43:44 -04001319 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1320 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001321 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001322
Yan Zheng2b820322008-11-17 21:11:30 -05001323 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001324 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001325
1326 /*
1327 * the device list mutex makes sure that we don't change
1328 * the device list while someone else is writing out all
1329 * the device supers.
1330 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001331
1332 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001333 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001334 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001335
Yan Zhenge4404d62008-12-12 10:03:26 -05001336 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001337
Chris Masoncd02dca2010-12-13 14:56:23 -05001338 if (device->missing)
1339 root->fs_info->fs_devices->missing_devices--;
1340
Yan Zheng2b820322008-11-17 21:11:30 -05001341 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1342 struct btrfs_device, dev_list);
1343 if (device->bdev == root->fs_info->sb->s_bdev)
1344 root->fs_info->sb->s_bdev = next_device->bdev;
1345 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1346 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1347
Xiao Guangrong1f781602011-04-20 10:09:16 +00001348 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001349 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001350
1351 call_rcu(&device->rcu, free_device);
1352 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001353
Yan Zheng2b820322008-11-17 21:11:30 -05001354 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1355 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1356
Xiao Guangrong1f781602011-04-20 10:09:16 +00001357 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001358 struct btrfs_fs_devices *fs_devices;
1359 fs_devices = root->fs_info->fs_devices;
1360 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001361 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001362 break;
1363 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001364 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001365 fs_devices->seed = cur_devices->seed;
1366 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001367 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001368 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001369 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001370 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001371 }
1372
1373 /*
1374 * at this point, the device is zero sized. We want to
1375 * remove it from the devices list and zero out the old super
1376 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001377 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001378 /* make sure this device isn't detected as part of
1379 * the FS anymore
1380 */
1381 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1382 set_buffer_dirty(bh);
1383 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001384 }
Chris Masona061fc82008-05-07 11:43:44 -04001385
Chris Masona061fc82008-05-07 11:43:44 -04001386 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001387
1388error_brelse:
1389 brelse(bh);
1390error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001391 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001392 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001393out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001394 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001395 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001396 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001397error_undo:
1398 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001399 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001400 list_add(&device->dev_alloc_list,
1401 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001402 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001403 root->fs_info->fs_devices->rw_devices++;
1404 }
1405 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001406}
1407
Yan Zheng2b820322008-11-17 21:11:30 -05001408/*
1409 * does all the dirty work required for changing file system's UUID.
1410 */
1411static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1412 struct btrfs_root *root)
1413{
1414 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1415 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001416 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001417 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1418 struct btrfs_device *device;
1419 u64 super_flags;
1420
1421 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001422 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001423 return -EINVAL;
1424
Yan Zhenge4404d62008-12-12 10:03:26 -05001425 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1426 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001427 return -ENOMEM;
1428
Yan Zhenge4404d62008-12-12 10:03:26 -05001429 old_devices = clone_fs_devices(fs_devices);
1430 if (IS_ERR(old_devices)) {
1431 kfree(seed_devices);
1432 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001433 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001434
Yan Zheng2b820322008-11-17 21:11:30 -05001435 list_add(&old_devices->list, &fs_uuids);
1436
Yan Zhenge4404d62008-12-12 10:03:26 -05001437 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1438 seed_devices->opened = 1;
1439 INIT_LIST_HEAD(&seed_devices->devices);
1440 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001441 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001442
1443 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001444 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1445 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001446 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1447
Yan Zhenge4404d62008-12-12 10:03:26 -05001448 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1449 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1450 device->fs_devices = seed_devices;
1451 }
1452
Yan Zheng2b820322008-11-17 21:11:30 -05001453 fs_devices->seeding = 0;
1454 fs_devices->num_devices = 0;
1455 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001456 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001457
1458 generate_random_uuid(fs_devices->fsid);
1459 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1460 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1461 super_flags = btrfs_super_flags(disk_super) &
1462 ~BTRFS_SUPER_FLAG_SEEDING;
1463 btrfs_set_super_flags(disk_super, super_flags);
1464
1465 return 0;
1466}
1467
1468/*
1469 * strore the expected generation for seed devices in device items.
1470 */
1471static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1472 struct btrfs_root *root)
1473{
1474 struct btrfs_path *path;
1475 struct extent_buffer *leaf;
1476 struct btrfs_dev_item *dev_item;
1477 struct btrfs_device *device;
1478 struct btrfs_key key;
1479 u8 fs_uuid[BTRFS_UUID_SIZE];
1480 u8 dev_uuid[BTRFS_UUID_SIZE];
1481 u64 devid;
1482 int ret;
1483
1484 path = btrfs_alloc_path();
1485 if (!path)
1486 return -ENOMEM;
1487
1488 root = root->fs_info->chunk_root;
1489 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1490 key.offset = 0;
1491 key.type = BTRFS_DEV_ITEM_KEY;
1492
1493 while (1) {
1494 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1495 if (ret < 0)
1496 goto error;
1497
1498 leaf = path->nodes[0];
1499next_slot:
1500 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1501 ret = btrfs_next_leaf(root, path);
1502 if (ret > 0)
1503 break;
1504 if (ret < 0)
1505 goto error;
1506 leaf = path->nodes[0];
1507 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001508 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001509 continue;
1510 }
1511
1512 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1513 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1514 key.type != BTRFS_DEV_ITEM_KEY)
1515 break;
1516
1517 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1518 struct btrfs_dev_item);
1519 devid = btrfs_device_id(leaf, dev_item);
1520 read_extent_buffer(leaf, dev_uuid,
1521 (unsigned long)btrfs_device_uuid(dev_item),
1522 BTRFS_UUID_SIZE);
1523 read_extent_buffer(leaf, fs_uuid,
1524 (unsigned long)btrfs_device_fsid(dev_item),
1525 BTRFS_UUID_SIZE);
1526 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1527 BUG_ON(!device);
1528
1529 if (device->fs_devices->seeding) {
1530 btrfs_set_device_generation(leaf, dev_item,
1531 device->generation);
1532 btrfs_mark_buffer_dirty(leaf);
1533 }
1534
1535 path->slots[0]++;
1536 goto next_slot;
1537 }
1538 ret = 0;
1539error:
1540 btrfs_free_path(path);
1541 return ret;
1542}
1543
Chris Mason788f20e2008-04-28 15:29:42 -04001544int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1545{
1546 struct btrfs_trans_handle *trans;
1547 struct btrfs_device *device;
1548 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001549 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001550 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001551 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001552 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001553 int ret = 0;
1554
Yan Zheng2b820322008-11-17 21:11:30 -05001555 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1556 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001557
Tejun Heod4d77622010-11-13 11:55:18 +01001558 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1559 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001560 if (IS_ERR(bdev))
1561 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001562
Yan Zheng2b820322008-11-17 21:11:30 -05001563 if (root->fs_info->fs_devices->seeding) {
1564 seeding_dev = 1;
1565 down_write(&sb->s_umount);
1566 mutex_lock(&uuid_mutex);
1567 }
1568
Chris Mason8c8bee12008-09-29 11:19:10 -04001569 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001570 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001571
Chris Mason788f20e2008-04-28 15:29:42 -04001572 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001573 /*
1574 * we have the volume lock, so we don't need the extra
1575 * device list mutex while reading the list here.
1576 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001577 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001578 if (device->bdev == bdev) {
1579 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001580 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001581 }
1582 }
1583
1584 device = kzalloc(sizeof(*device), GFP_NOFS);
1585 if (!device) {
1586 /* we can safely leave the fs_devices entry around */
1587 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001588 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001589 }
1590
Chris Mason788f20e2008-04-28 15:29:42 -04001591 device->name = kstrdup(device_path, GFP_NOFS);
1592 if (!device->name) {
1593 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001594 ret = -ENOMEM;
1595 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001596 }
Yan Zheng2b820322008-11-17 21:11:30 -05001597
1598 ret = find_next_devid(root, &device->devid);
1599 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001600 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001601 kfree(device);
1602 goto error;
1603 }
1604
Yan, Zhenga22285a2010-05-16 10:48:46 -04001605 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001606 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001607 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001608 kfree(device);
1609 ret = PTR_ERR(trans);
1610 goto error;
1611 }
1612
Yan Zheng2b820322008-11-17 21:11:30 -05001613 lock_chunks(root);
1614
Yan Zheng2b820322008-11-17 21:11:30 -05001615 device->writeable = 1;
1616 device->work.func = pending_bios_fn;
1617 generate_random_uuid(device->uuid);
1618 spin_lock_init(&device->io_lock);
1619 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001620 device->io_width = root->sectorsize;
1621 device->io_align = root->sectorsize;
1622 device->sector_size = root->sectorsize;
1623 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001624 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001625 device->dev_root = root->fs_info->dev_root;
1626 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001627 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001628 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001629 set_blocksize(device->bdev, 4096);
1630
Yan Zheng2b820322008-11-17 21:11:30 -05001631 if (seeding_dev) {
1632 sb->s_flags &= ~MS_RDONLY;
1633 ret = btrfs_prepare_sprout(trans, root);
1634 BUG_ON(ret);
1635 }
1636
1637 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001638
1639 /*
1640 * we don't want write_supers to jump in here with our device
1641 * half setup
1642 */
1643 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001644 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001645 list_add(&device->dev_alloc_list,
1646 &root->fs_info->fs_devices->alloc_list);
1647 root->fs_info->fs_devices->num_devices++;
1648 root->fs_info->fs_devices->open_devices++;
1649 root->fs_info->fs_devices->rw_devices++;
1650 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1651
Chris Masonc2898112009-06-10 09:51:32 -04001652 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1653 root->fs_info->fs_devices->rotating = 1;
1654
Chris Mason788f20e2008-04-28 15:29:42 -04001655 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1656 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1657 total_bytes + device->total_bytes);
1658
1659 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1660 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1661 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001662 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001663
Yan Zheng2b820322008-11-17 21:11:30 -05001664 if (seeding_dev) {
1665 ret = init_first_rw_device(trans, root, device);
1666 BUG_ON(ret);
1667 ret = btrfs_finish_sprout(trans, root);
1668 BUG_ON(ret);
1669 } else {
1670 ret = btrfs_add_device(trans, root, device);
1671 }
1672
Chris Mason913d9522009-03-10 13:17:18 -04001673 /*
1674 * we've got more storage, clear any full flags on the space
1675 * infos
1676 */
1677 btrfs_clear_space_info_full(root->fs_info);
1678
Chris Mason7d9eb122008-07-08 14:19:17 -04001679 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001680 btrfs_commit_transaction(trans, root);
1681
1682 if (seeding_dev) {
1683 mutex_unlock(&uuid_mutex);
1684 up_write(&sb->s_umount);
1685
1686 ret = btrfs_relocate_sys_chunks(root);
1687 BUG_ON(ret);
1688 }
1689out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001690 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001691 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001692error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001693 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001694 if (seeding_dev) {
1695 mutex_unlock(&uuid_mutex);
1696 up_write(&sb->s_umount);
1697 }
Chris Mason788f20e2008-04-28 15:29:42 -04001698 goto out;
1699}
1700
Chris Masond3977122009-01-05 21:25:51 -05001701static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1702 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001703{
1704 int ret;
1705 struct btrfs_path *path;
1706 struct btrfs_root *root;
1707 struct btrfs_dev_item *dev_item;
1708 struct extent_buffer *leaf;
1709 struct btrfs_key key;
1710
1711 root = device->dev_root->fs_info->chunk_root;
1712
1713 path = btrfs_alloc_path();
1714 if (!path)
1715 return -ENOMEM;
1716
1717 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1718 key.type = BTRFS_DEV_ITEM_KEY;
1719 key.offset = device->devid;
1720
1721 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1722 if (ret < 0)
1723 goto out;
1724
1725 if (ret > 0) {
1726 ret = -ENOENT;
1727 goto out;
1728 }
1729
1730 leaf = path->nodes[0];
1731 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1732
1733 btrfs_set_device_id(leaf, dev_item, device->devid);
1734 btrfs_set_device_type(leaf, dev_item, device->type);
1735 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1736 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1737 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001738 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001739 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1740 btrfs_mark_buffer_dirty(leaf);
1741
1742out:
1743 btrfs_free_path(path);
1744 return ret;
1745}
1746
Chris Mason7d9eb122008-07-08 14:19:17 -04001747static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001748 struct btrfs_device *device, u64 new_size)
1749{
1750 struct btrfs_super_block *super_copy =
1751 &device->dev_root->fs_info->super_copy;
1752 u64 old_total = btrfs_super_total_bytes(super_copy);
1753 u64 diff = new_size - device->total_bytes;
1754
Yan Zheng2b820322008-11-17 21:11:30 -05001755 if (!device->writeable)
1756 return -EACCES;
1757 if (new_size <= device->total_bytes)
1758 return -EINVAL;
1759
Chris Mason8f18cf12008-04-25 16:53:30 -04001760 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001761 device->fs_devices->total_rw_bytes += diff;
1762
1763 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001764 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001765 btrfs_clear_space_info_full(device->dev_root->fs_info);
1766
Chris Mason8f18cf12008-04-25 16:53:30 -04001767 return btrfs_update_device(trans, device);
1768}
1769
Chris Mason7d9eb122008-07-08 14:19:17 -04001770int btrfs_grow_device(struct btrfs_trans_handle *trans,
1771 struct btrfs_device *device, u64 new_size)
1772{
1773 int ret;
1774 lock_chunks(device->dev_root);
1775 ret = __btrfs_grow_device(trans, device, new_size);
1776 unlock_chunks(device->dev_root);
1777 return ret;
1778}
1779
Chris Mason8f18cf12008-04-25 16:53:30 -04001780static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1781 struct btrfs_root *root,
1782 u64 chunk_tree, u64 chunk_objectid,
1783 u64 chunk_offset)
1784{
1785 int ret;
1786 struct btrfs_path *path;
1787 struct btrfs_key key;
1788
1789 root = root->fs_info->chunk_root;
1790 path = btrfs_alloc_path();
1791 if (!path)
1792 return -ENOMEM;
1793
1794 key.objectid = chunk_objectid;
1795 key.offset = chunk_offset;
1796 key.type = BTRFS_CHUNK_ITEM_KEY;
1797
1798 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1799 BUG_ON(ret);
1800
1801 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001802
1803 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001804 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001805}
1806
Christoph Hellwigb2950862008-12-02 09:54:17 -05001807static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001808 chunk_offset)
1809{
1810 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1811 struct btrfs_disk_key *disk_key;
1812 struct btrfs_chunk *chunk;
1813 u8 *ptr;
1814 int ret = 0;
1815 u32 num_stripes;
1816 u32 array_size;
1817 u32 len = 0;
1818 u32 cur;
1819 struct btrfs_key key;
1820
1821 array_size = btrfs_super_sys_array_size(super_copy);
1822
1823 ptr = super_copy->sys_chunk_array;
1824 cur = 0;
1825
1826 while (cur < array_size) {
1827 disk_key = (struct btrfs_disk_key *)ptr;
1828 btrfs_disk_key_to_cpu(&key, disk_key);
1829
1830 len = sizeof(*disk_key);
1831
1832 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1833 chunk = (struct btrfs_chunk *)(ptr + len);
1834 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1835 len += btrfs_chunk_item_size(num_stripes);
1836 } else {
1837 ret = -EIO;
1838 break;
1839 }
1840 if (key.objectid == chunk_objectid &&
1841 key.offset == chunk_offset) {
1842 memmove(ptr, ptr + len, array_size - (cur + len));
1843 array_size -= len;
1844 btrfs_set_super_sys_array_size(super_copy, array_size);
1845 } else {
1846 ptr += len;
1847 cur += len;
1848 }
1849 }
1850 return ret;
1851}
1852
Christoph Hellwigb2950862008-12-02 09:54:17 -05001853static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001854 u64 chunk_tree, u64 chunk_objectid,
1855 u64 chunk_offset)
1856{
1857 struct extent_map_tree *em_tree;
1858 struct btrfs_root *extent_root;
1859 struct btrfs_trans_handle *trans;
1860 struct extent_map *em;
1861 struct map_lookup *map;
1862 int ret;
1863 int i;
1864
1865 root = root->fs_info->chunk_root;
1866 extent_root = root->fs_info->extent_root;
1867 em_tree = &root->fs_info->mapping_tree.map_tree;
1868
Josef Bacikba1bf482009-09-11 16:11:19 -04001869 ret = btrfs_can_relocate(extent_root, chunk_offset);
1870 if (ret)
1871 return -ENOSPC;
1872
Chris Mason8f18cf12008-04-25 16:53:30 -04001873 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001874 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001875 if (ret)
1876 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001877
Yan, Zhenga22285a2010-05-16 10:48:46 -04001878 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001879 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001880
Chris Mason7d9eb122008-07-08 14:19:17 -04001881 lock_chunks(root);
1882
Chris Mason8f18cf12008-04-25 16:53:30 -04001883 /*
1884 * step two, delete the device extents and the
1885 * chunk tree entries
1886 */
Chris Mason890871b2009-09-02 16:24:52 -04001887 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001888 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001889 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001890
Chris Masona061fc82008-05-07 11:43:44 -04001891 BUG_ON(em->start > chunk_offset ||
1892 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001893 map = (struct map_lookup *)em->bdev;
1894
1895 for (i = 0; i < map->num_stripes; i++) {
1896 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1897 map->stripes[i].physical);
1898 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001899
Chris Masondfe25022008-05-13 13:46:40 -04001900 if (map->stripes[i].dev) {
1901 ret = btrfs_update_device(trans, map->stripes[i].dev);
1902 BUG_ON(ret);
1903 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001904 }
1905 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1906 chunk_offset);
1907
1908 BUG_ON(ret);
1909
liubo1abe9b82011-03-24 11:18:59 +00001910 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1911
Chris Mason8f18cf12008-04-25 16:53:30 -04001912 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1913 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1914 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001915 }
1916
Zheng Yan1a40e232008-09-26 10:09:34 -04001917 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1918 BUG_ON(ret);
1919
Chris Mason890871b2009-09-02 16:24:52 -04001920 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001921 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001922 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001923
Chris Mason8f18cf12008-04-25 16:53:30 -04001924 kfree(map);
1925 em->bdev = NULL;
1926
1927 /* once for the tree */
1928 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001929 /* once for us */
1930 free_extent_map(em);
1931
Chris Mason7d9eb122008-07-08 14:19:17 -04001932 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001933 btrfs_end_transaction(trans, root);
1934 return 0;
1935}
1936
Yan Zheng2b820322008-11-17 21:11:30 -05001937static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1938{
1939 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1940 struct btrfs_path *path;
1941 struct extent_buffer *leaf;
1942 struct btrfs_chunk *chunk;
1943 struct btrfs_key key;
1944 struct btrfs_key found_key;
1945 u64 chunk_tree = chunk_root->root_key.objectid;
1946 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001947 bool retried = false;
1948 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001949 int ret;
1950
1951 path = btrfs_alloc_path();
1952 if (!path)
1953 return -ENOMEM;
1954
Josef Bacikba1bf482009-09-11 16:11:19 -04001955again:
Yan Zheng2b820322008-11-17 21:11:30 -05001956 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1957 key.offset = (u64)-1;
1958 key.type = BTRFS_CHUNK_ITEM_KEY;
1959
1960 while (1) {
1961 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1962 if (ret < 0)
1963 goto error;
1964 BUG_ON(ret == 0);
1965
1966 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1967 key.type);
1968 if (ret < 0)
1969 goto error;
1970 if (ret > 0)
1971 break;
1972
1973 leaf = path->nodes[0];
1974 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1975
1976 chunk = btrfs_item_ptr(leaf, path->slots[0],
1977 struct btrfs_chunk);
1978 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02001979 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001980
1981 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1982 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1983 found_key.objectid,
1984 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001985 if (ret == -ENOSPC)
1986 failed++;
1987 else if (ret)
1988 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001989 }
1990
1991 if (found_key.offset == 0)
1992 break;
1993 key.offset = found_key.offset - 1;
1994 }
1995 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001996 if (failed && !retried) {
1997 failed = 0;
1998 retried = true;
1999 goto again;
2000 } else if (failed && retried) {
2001 WARN_ON(1);
2002 ret = -ENOSPC;
2003 }
Yan Zheng2b820322008-11-17 21:11:30 -05002004error:
2005 btrfs_free_path(path);
2006 return ret;
2007}
2008
Chris Masonec44a352008-04-28 15:29:52 -04002009static u64 div_factor(u64 num, int factor)
2010{
2011 if (factor == 10)
2012 return num;
2013 num *= factor;
2014 do_div(num, 10);
2015 return num;
2016}
2017
Chris Masonec44a352008-04-28 15:29:52 -04002018int btrfs_balance(struct btrfs_root *dev_root)
2019{
2020 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002021 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2022 struct btrfs_device *device;
2023 u64 old_size;
2024 u64 size_to_free;
2025 struct btrfs_path *path;
2026 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002027 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2028 struct btrfs_trans_handle *trans;
2029 struct btrfs_key found_key;
2030
Yan Zheng2b820322008-11-17 21:11:30 -05002031 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2032 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002033
Ben Hutchings6f88a442010-12-29 14:55:03 +00002034 if (!capable(CAP_SYS_ADMIN))
2035 return -EPERM;
2036
Chris Mason7d9eb122008-07-08 14:19:17 -04002037 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002038 dev_root = dev_root->fs_info->dev_root;
2039
Chris Masonec44a352008-04-28 15:29:52 -04002040 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002041 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002042 old_size = device->total_bytes;
2043 size_to_free = div_factor(old_size, 1);
2044 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002045 if (!device->writeable ||
2046 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002047 continue;
2048
2049 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002050 if (ret == -ENOSPC)
2051 break;
Chris Masonec44a352008-04-28 15:29:52 -04002052 BUG_ON(ret);
2053
Yan, Zhenga22285a2010-05-16 10:48:46 -04002054 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002055 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002056
2057 ret = btrfs_grow_device(trans, device, old_size);
2058 BUG_ON(ret);
2059
2060 btrfs_end_transaction(trans, dev_root);
2061 }
2062
2063 /* step two, relocate all the chunks */
2064 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002065 if (!path) {
2066 ret = -ENOMEM;
2067 goto error;
2068 }
Chris Masonec44a352008-04-28 15:29:52 -04002069 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2070 key.offset = (u64)-1;
2071 key.type = BTRFS_CHUNK_ITEM_KEY;
2072
Chris Masond3977122009-01-05 21:25:51 -05002073 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002074 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2075 if (ret < 0)
2076 goto error;
2077
2078 /*
2079 * this shouldn't happen, it means the last relocate
2080 * failed
2081 */
2082 if (ret == 0)
2083 break;
2084
2085 ret = btrfs_previous_item(chunk_root, path, 0,
2086 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002087 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002088 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002089
Chris Masonec44a352008-04-28 15:29:52 -04002090 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2091 path->slots[0]);
2092 if (found_key.objectid != key.objectid)
2093 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002094
Chris Masonec44a352008-04-28 15:29:52 -04002095 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002096 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002097 break;
2098
David Sterbab3b4aa72011-04-21 01:20:15 +02002099 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002100 ret = btrfs_relocate_chunk(chunk_root,
2101 chunk_root->root_key.objectid,
2102 found_key.objectid,
2103 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002104 if (ret && ret != -ENOSPC)
2105 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002106 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002107 }
2108 ret = 0;
2109error:
2110 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002111 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002112 return ret;
2113}
2114
Chris Mason8f18cf12008-04-25 16:53:30 -04002115/*
2116 * shrinking a device means finding all of the device extents past
2117 * the new size, and then following the back refs to the chunks.
2118 * The chunk relocation code actually frees the device extent
2119 */
2120int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2121{
2122 struct btrfs_trans_handle *trans;
2123 struct btrfs_root *root = device->dev_root;
2124 struct btrfs_dev_extent *dev_extent = NULL;
2125 struct btrfs_path *path;
2126 u64 length;
2127 u64 chunk_tree;
2128 u64 chunk_objectid;
2129 u64 chunk_offset;
2130 int ret;
2131 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002132 int failed = 0;
2133 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002134 struct extent_buffer *l;
2135 struct btrfs_key key;
2136 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2137 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002138 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002139 u64 diff = device->total_bytes - new_size;
2140
Yan Zheng2b820322008-11-17 21:11:30 -05002141 if (new_size >= device->total_bytes)
2142 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002143
2144 path = btrfs_alloc_path();
2145 if (!path)
2146 return -ENOMEM;
2147
Chris Mason8f18cf12008-04-25 16:53:30 -04002148 path->reada = 2;
2149
Chris Mason7d9eb122008-07-08 14:19:17 -04002150 lock_chunks(root);
2151
Chris Mason8f18cf12008-04-25 16:53:30 -04002152 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002153 if (device->writeable)
2154 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002155 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002156
Josef Bacikba1bf482009-09-11 16:11:19 -04002157again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002158 key.objectid = device->devid;
2159 key.offset = (u64)-1;
2160 key.type = BTRFS_DEV_EXTENT_KEY;
2161
2162 while (1) {
2163 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2164 if (ret < 0)
2165 goto done;
2166
2167 ret = btrfs_previous_item(root, path, 0, key.type);
2168 if (ret < 0)
2169 goto done;
2170 if (ret) {
2171 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002172 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002173 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002174 }
2175
2176 l = path->nodes[0];
2177 slot = path->slots[0];
2178 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2179
Josef Bacikba1bf482009-09-11 16:11:19 -04002180 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002181 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002182 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002183 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002184
2185 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2186 length = btrfs_dev_extent_length(l, dev_extent);
2187
Josef Bacikba1bf482009-09-11 16:11:19 -04002188 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002189 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002190 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002191 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002192
2193 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2194 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2195 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002196 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002197
2198 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2199 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002200 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002201 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002202 if (ret == -ENOSPC)
2203 failed++;
2204 key.offset -= 1;
2205 }
2206
2207 if (failed && !retried) {
2208 failed = 0;
2209 retried = true;
2210 goto again;
2211 } else if (failed && retried) {
2212 ret = -ENOSPC;
2213 lock_chunks(root);
2214
2215 device->total_bytes = old_size;
2216 if (device->writeable)
2217 device->fs_devices->total_rw_bytes += diff;
2218 unlock_chunks(root);
2219 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002220 }
2221
Chris Balld6397ba2009-04-27 07:29:03 -04002222 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002223 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002224 if (IS_ERR(trans)) {
2225 ret = PTR_ERR(trans);
2226 goto done;
2227 }
2228
Chris Balld6397ba2009-04-27 07:29:03 -04002229 lock_chunks(root);
2230
2231 device->disk_total_bytes = new_size;
2232 /* Now btrfs_update_device() will change the on-disk size. */
2233 ret = btrfs_update_device(trans, device);
2234 if (ret) {
2235 unlock_chunks(root);
2236 btrfs_end_transaction(trans, root);
2237 goto done;
2238 }
2239 WARN_ON(diff > old_total);
2240 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2241 unlock_chunks(root);
2242 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002243done:
2244 btrfs_free_path(path);
2245 return ret;
2246}
2247
Christoph Hellwigb2950862008-12-02 09:54:17 -05002248static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002249 struct btrfs_root *root,
2250 struct btrfs_key *key,
2251 struct btrfs_chunk *chunk, int item_size)
2252{
2253 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2254 struct btrfs_disk_key disk_key;
2255 u32 array_size;
2256 u8 *ptr;
2257
2258 array_size = btrfs_super_sys_array_size(super_copy);
2259 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2260 return -EFBIG;
2261
2262 ptr = super_copy->sys_chunk_array + array_size;
2263 btrfs_cpu_key_to_disk(&disk_key, key);
2264 memcpy(ptr, &disk_key, sizeof(disk_key));
2265 ptr += sizeof(disk_key);
2266 memcpy(ptr, chunk, item_size);
2267 item_size += sizeof(disk_key);
2268 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2269 return 0;
2270}
2271
Miao Xieb2117a32011-01-05 10:07:28 +00002272/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002273 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002274 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002275static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002276{
Arne Jansen73c5de02011-04-12 12:07:57 +02002277 const struct btrfs_device_info *di_a = a;
2278 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002279
Arne Jansen73c5de02011-04-12 12:07:57 +02002280 if (di_a->max_avail > di_b->max_avail)
2281 return -1;
2282 if (di_a->max_avail < di_b->max_avail)
2283 return 1;
2284 if (di_a->total_avail > di_b->total_avail)
2285 return -1;
2286 if (di_a->total_avail < di_b->total_avail)
2287 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002288 return 0;
2289}
2290
2291static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2292 struct btrfs_root *extent_root,
2293 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002294 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002295 u64 start, u64 type)
2296{
2297 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002298 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2299 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002300 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002301 struct extent_map_tree *em_tree;
2302 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002303 struct btrfs_device_info *devices_info = NULL;
2304 u64 total_avail;
2305 int num_stripes; /* total number of stripes to allocate */
2306 int sub_stripes; /* sub_stripes info for map */
2307 int dev_stripes; /* stripes per dev */
2308 int devs_max; /* max devs to use */
2309 int devs_min; /* min devs needed */
2310 int devs_increment; /* ndevs has to be a multiple of this */
2311 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002312 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002313 u64 max_stripe_size;
2314 u64 max_chunk_size;
2315 u64 stripe_size;
2316 u64 num_bytes;
2317 int ndevs;
2318 int i;
2319 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002320
2321 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2322 (type & BTRFS_BLOCK_GROUP_DUP)) {
2323 WARN_ON(1);
2324 type &= ~BTRFS_BLOCK_GROUP_DUP;
2325 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002326
Miao Xieb2117a32011-01-05 10:07:28 +00002327 if (list_empty(&fs_devices->alloc_list))
2328 return -ENOSPC;
2329
Arne Jansen73c5de02011-04-12 12:07:57 +02002330 sub_stripes = 1;
2331 dev_stripes = 1;
2332 devs_increment = 1;
2333 ncopies = 1;
2334 devs_max = 0; /* 0 == as many as possible */
2335 devs_min = 1;
2336
2337 /*
2338 * define the properties of each RAID type.
2339 * FIXME: move this to a global table and use it in all RAID
2340 * calculation code
2341 */
2342 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2343 dev_stripes = 2;
2344 ncopies = 2;
2345 devs_max = 1;
2346 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2347 devs_min = 2;
2348 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2349 devs_increment = 2;
2350 ncopies = 2;
2351 devs_max = 2;
2352 devs_min = 2;
2353 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2354 sub_stripes = 2;
2355 devs_increment = 2;
2356 ncopies = 2;
2357 devs_min = 4;
2358 } else {
2359 devs_max = 1;
2360 }
2361
2362 if (type & BTRFS_BLOCK_GROUP_DATA) {
2363 max_stripe_size = 1024 * 1024 * 1024;
2364 max_chunk_size = 10 * max_stripe_size;
2365 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2366 max_stripe_size = 256 * 1024 * 1024;
2367 max_chunk_size = max_stripe_size;
2368 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2369 max_stripe_size = 8 * 1024 * 1024;
2370 max_chunk_size = 2 * max_stripe_size;
2371 } else {
2372 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2373 type);
2374 BUG_ON(1);
2375 }
2376
2377 /* we don't want a chunk larger than 10% of writeable space */
2378 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2379 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002380
2381 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2382 GFP_NOFS);
2383 if (!devices_info)
2384 return -ENOMEM;
2385
Arne Jansen73c5de02011-04-12 12:07:57 +02002386 cur = fs_devices->alloc_list.next;
2387
2388 /*
2389 * in the first pass through the devices list, we gather information
2390 * about the available holes on each device.
2391 */
2392 ndevs = 0;
2393 while (cur != &fs_devices->alloc_list) {
2394 struct btrfs_device *device;
2395 u64 max_avail;
2396 u64 dev_offset;
2397
2398 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2399
2400 cur = cur->next;
2401
2402 if (!device->writeable) {
2403 printk(KERN_ERR
2404 "btrfs: read-only device in alloc_list\n");
2405 WARN_ON(1);
2406 continue;
2407 }
2408
2409 if (!device->in_fs_metadata)
2410 continue;
2411
2412 if (device->total_bytes > device->bytes_used)
2413 total_avail = device->total_bytes - device->bytes_used;
2414 else
2415 total_avail = 0;
2416 /* avail is off by max(alloc_start, 1MB), but that is the same
2417 * for all devices, so it doesn't hurt the sorting later on
2418 */
2419
2420 ret = find_free_dev_extent(trans, device,
2421 max_stripe_size * dev_stripes,
2422 &dev_offset, &max_avail);
2423 if (ret && ret != -ENOSPC)
2424 goto error;
2425
2426 if (ret == 0)
2427 max_avail = max_stripe_size * dev_stripes;
2428
2429 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2430 continue;
2431
2432 devices_info[ndevs].dev_offset = dev_offset;
2433 devices_info[ndevs].max_avail = max_avail;
2434 devices_info[ndevs].total_avail = total_avail;
2435 devices_info[ndevs].dev = device;
2436 ++ndevs;
2437 }
2438
2439 /*
2440 * now sort the devices by hole size / available space
2441 */
2442 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2443 btrfs_cmp_device_info, NULL);
2444
2445 /* round down to number of usable stripes */
2446 ndevs -= ndevs % devs_increment;
2447
2448 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2449 ret = -ENOSPC;
2450 goto error;
2451 }
2452
2453 if (devs_max && ndevs > devs_max)
2454 ndevs = devs_max;
2455 /*
2456 * the primary goal is to maximize the number of stripes, so use as many
2457 * devices as possible, even if the stripes are not maximum sized.
2458 */
2459 stripe_size = devices_info[ndevs-1].max_avail;
2460 num_stripes = ndevs * dev_stripes;
2461
2462 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2463 stripe_size = max_chunk_size * ncopies;
2464 do_div(stripe_size, num_stripes);
2465 }
2466
2467 do_div(stripe_size, dev_stripes);
2468 do_div(stripe_size, BTRFS_STRIPE_LEN);
2469 stripe_size *= BTRFS_STRIPE_LEN;
2470
Miao Xieb2117a32011-01-05 10:07:28 +00002471 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2472 if (!map) {
2473 ret = -ENOMEM;
2474 goto error;
2475 }
2476 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002477
Arne Jansen73c5de02011-04-12 12:07:57 +02002478 for (i = 0; i < ndevs; ++i) {
2479 for (j = 0; j < dev_stripes; ++j) {
2480 int s = i * dev_stripes + j;
2481 map->stripes[s].dev = devices_info[i].dev;
2482 map->stripes[s].physical = devices_info[i].dev_offset +
2483 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002484 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002485 }
Chris Mason593060d2008-03-25 16:50:33 -04002486 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002487 map->stripe_len = BTRFS_STRIPE_LEN;
2488 map->io_align = BTRFS_STRIPE_LEN;
2489 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002490 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002491 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002492
Yan Zheng2b820322008-11-17 21:11:30 -05002493 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002494 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002495
Arne Jansen73c5de02011-04-12 12:07:57 +02002496 *stripe_size_out = stripe_size;
2497 *num_bytes_out = num_bytes;
2498
2499 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002500
David Sterba172ddd62011-04-21 00:48:27 +02002501 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002502 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002503 ret = -ENOMEM;
2504 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002505 }
Chris Mason0b86a832008-03-24 15:01:56 -04002506 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002507 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002508 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002509 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002510 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002511
Chris Mason0b86a832008-03-24 15:01:56 -04002512 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002513 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002514 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002515 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002516 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002517 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002518
2519 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2520 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002521 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002522 BUG_ON(ret);
2523
Arne Jansen73c5de02011-04-12 12:07:57 +02002524 for (i = 0; i < map->num_stripes; ++i) {
2525 struct btrfs_device *device;
2526 u64 dev_offset;
2527
2528 device = map->stripes[i].dev;
2529 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002530
2531 ret = btrfs_alloc_dev_extent(trans, device,
2532 info->chunk_root->root_key.objectid,
2533 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002534 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002535 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002536 }
2537
Miao Xieb2117a32011-01-05 10:07:28 +00002538 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002539 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002540
2541error:
2542 kfree(map);
2543 kfree(devices_info);
2544 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002545}
2546
2547static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2548 struct btrfs_root *extent_root,
2549 struct map_lookup *map, u64 chunk_offset,
2550 u64 chunk_size, u64 stripe_size)
2551{
2552 u64 dev_offset;
2553 struct btrfs_key key;
2554 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2555 struct btrfs_device *device;
2556 struct btrfs_chunk *chunk;
2557 struct btrfs_stripe *stripe;
2558 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2559 int index = 0;
2560 int ret;
2561
2562 chunk = kzalloc(item_size, GFP_NOFS);
2563 if (!chunk)
2564 return -ENOMEM;
2565
2566 index = 0;
2567 while (index < map->num_stripes) {
2568 device = map->stripes[index].dev;
2569 device->bytes_used += stripe_size;
2570 ret = btrfs_update_device(trans, device);
2571 BUG_ON(ret);
2572 index++;
2573 }
2574
2575 index = 0;
2576 stripe = &chunk->stripe;
2577 while (index < map->num_stripes) {
2578 device = map->stripes[index].dev;
2579 dev_offset = map->stripes[index].physical;
2580
2581 btrfs_set_stack_stripe_devid(stripe, device->devid);
2582 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2583 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2584 stripe++;
2585 index++;
2586 }
2587
2588 btrfs_set_stack_chunk_length(chunk, chunk_size);
2589 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2590 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2591 btrfs_set_stack_chunk_type(chunk, map->type);
2592 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2593 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2594 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2595 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2596 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2597
2598 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2599 key.type = BTRFS_CHUNK_ITEM_KEY;
2600 key.offset = chunk_offset;
2601
2602 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2603 BUG_ON(ret);
2604
2605 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2606 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2607 item_size);
2608 BUG_ON(ret);
2609 }
liubo1abe9b82011-03-24 11:18:59 +00002610
Yan Zheng2b820322008-11-17 21:11:30 -05002611 kfree(chunk);
2612 return 0;
2613}
2614
2615/*
2616 * Chunk allocation falls into two parts. The first part does works
2617 * that make the new allocated chunk useable, but not do any operation
2618 * that modifies the chunk tree. The second part does the works that
2619 * require modifying the chunk tree. This division is important for the
2620 * bootstrap process of adding storage to a seed btrfs.
2621 */
2622int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2623 struct btrfs_root *extent_root, u64 type)
2624{
2625 u64 chunk_offset;
2626 u64 chunk_size;
2627 u64 stripe_size;
2628 struct map_lookup *map;
2629 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2630 int ret;
2631
2632 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2633 &chunk_offset);
2634 if (ret)
2635 return ret;
2636
2637 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2638 &stripe_size, chunk_offset, type);
2639 if (ret)
2640 return ret;
2641
2642 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2643 chunk_size, stripe_size);
2644 BUG_ON(ret);
2645 return 0;
2646}
2647
Chris Masond3977122009-01-05 21:25:51 -05002648static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002649 struct btrfs_root *root,
2650 struct btrfs_device *device)
2651{
2652 u64 chunk_offset;
2653 u64 sys_chunk_offset;
2654 u64 chunk_size;
2655 u64 sys_chunk_size;
2656 u64 stripe_size;
2657 u64 sys_stripe_size;
2658 u64 alloc_profile;
2659 struct map_lookup *map;
2660 struct map_lookup *sys_map;
2661 struct btrfs_fs_info *fs_info = root->fs_info;
2662 struct btrfs_root *extent_root = fs_info->extent_root;
2663 int ret;
2664
2665 ret = find_next_chunk(fs_info->chunk_root,
2666 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07002667 if (ret)
2668 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002669
2670 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2671 (fs_info->metadata_alloc_profile &
2672 fs_info->avail_metadata_alloc_bits);
2673 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2674
2675 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2676 &stripe_size, chunk_offset, alloc_profile);
2677 BUG_ON(ret);
2678
2679 sys_chunk_offset = chunk_offset + chunk_size;
2680
2681 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2682 (fs_info->system_alloc_profile &
2683 fs_info->avail_system_alloc_bits);
2684 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2685
2686 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2687 &sys_chunk_size, &sys_stripe_size,
2688 sys_chunk_offset, alloc_profile);
2689 BUG_ON(ret);
2690
2691 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2692 BUG_ON(ret);
2693
2694 /*
2695 * Modifying chunk tree needs allocating new blocks from both
2696 * system block group and metadata block group. So we only can
2697 * do operations require modifying the chunk tree after both
2698 * block groups were created.
2699 */
2700 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2701 chunk_size, stripe_size);
2702 BUG_ON(ret);
2703
2704 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2705 sys_chunk_offset, sys_chunk_size,
2706 sys_stripe_size);
2707 BUG_ON(ret);
2708 return 0;
2709}
2710
2711int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2712{
2713 struct extent_map *em;
2714 struct map_lookup *map;
2715 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2716 int readonly = 0;
2717 int i;
2718
Chris Mason890871b2009-09-02 16:24:52 -04002719 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002720 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002721 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002722 if (!em)
2723 return 1;
2724
Josef Bacikf48b9072010-01-27 02:07:59 +00002725 if (btrfs_test_opt(root, DEGRADED)) {
2726 free_extent_map(em);
2727 return 0;
2728 }
2729
Yan Zheng2b820322008-11-17 21:11:30 -05002730 map = (struct map_lookup *)em->bdev;
2731 for (i = 0; i < map->num_stripes; i++) {
2732 if (!map->stripes[i].dev->writeable) {
2733 readonly = 1;
2734 break;
2735 }
2736 }
2737 free_extent_map(em);
2738 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002739}
2740
2741void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2742{
David Sterbaa8067e02011-04-21 00:34:43 +02002743 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002744}
2745
2746void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2747{
2748 struct extent_map *em;
2749
Chris Masond3977122009-01-05 21:25:51 -05002750 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002751 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002752 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2753 if (em)
2754 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002755 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002756 if (!em)
2757 break;
2758 kfree(em->bdev);
2759 /* once for us */
2760 free_extent_map(em);
2761 /* once for the tree */
2762 free_extent_map(em);
2763 }
2764}
2765
Chris Masonf1885912008-04-09 16:28:12 -04002766int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2767{
2768 struct extent_map *em;
2769 struct map_lookup *map;
2770 struct extent_map_tree *em_tree = &map_tree->map_tree;
2771 int ret;
2772
Chris Mason890871b2009-09-02 16:24:52 -04002773 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002774 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002775 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002776 BUG_ON(!em);
2777
2778 BUG_ON(em->start > logical || em->start + em->len < logical);
2779 map = (struct map_lookup *)em->bdev;
2780 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2781 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002782 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2783 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002784 else
2785 ret = 1;
2786 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002787 return ret;
2788}
2789
Chris Masondfe25022008-05-13 13:46:40 -04002790static int find_live_mirror(struct map_lookup *map, int first, int num,
2791 int optimal)
2792{
2793 int i;
2794 if (map->stripes[optimal].dev->bdev)
2795 return optimal;
2796 for (i = first; i < first + num; i++) {
2797 if (map->stripes[i].dev->bdev)
2798 return i;
2799 }
2800 /* we couldn't find one that doesn't fail. Just return something
2801 * and the io error handling code will clean up eventually
2802 */
2803 return optimal;
2804}
2805
Chris Masonf2d8d742008-04-21 10:03:05 -04002806static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2807 u64 logical, u64 *length,
2808 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002809 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002810{
2811 struct extent_map *em;
2812 struct map_lookup *map;
2813 struct extent_map_tree *em_tree = &map_tree->map_tree;
2814 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002815 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002816 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002817 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002818 u64 stripe_nr_orig;
2819 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002820 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002821 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002822 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002823 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002824 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002825 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002826 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002827
Li Dongyangfce3bb92011-03-24 10:24:26 +00002828 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002829 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002830again:
2831 if (multi_ret) {
2832 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2833 GFP_NOFS);
2834 if (!multi)
2835 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002836
2837 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002838 }
Chris Mason0b86a832008-03-24 15:01:56 -04002839
Chris Mason890871b2009-09-02 16:24:52 -04002840 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002841 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002842 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002843
Chris Mason3b951512008-04-17 11:29:12 -04002844 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002845 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2846 (unsigned long long)logical,
2847 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002848 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002849 }
Chris Mason0b86a832008-03-24 15:01:56 -04002850
2851 BUG_ON(em->start > logical || em->start + em->len < logical);
2852 map = (struct map_lookup *)em->bdev;
2853 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002854
Chris Masonf1885912008-04-09 16:28:12 -04002855 if (mirror_num > map->num_stripes)
2856 mirror_num = 0;
2857
Chris Masoncea9e442008-04-09 16:28:12 -04002858 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002859 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002860 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2861 BTRFS_BLOCK_GROUP_DUP)) {
2862 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002863 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002864 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2865 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002866 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002867 }
2868 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002869 if (rw & REQ_DISCARD) {
2870 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2871 BTRFS_BLOCK_GROUP_RAID1 |
2872 BTRFS_BLOCK_GROUP_DUP |
2873 BTRFS_BLOCK_GROUP_RAID10)) {
2874 stripes_required = map->num_stripes;
2875 }
2876 }
2877 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002878 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002879 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002880 free_extent_map(em);
2881 kfree(multi);
2882 goto again;
2883 }
Chris Mason593060d2008-03-25 16:50:33 -04002884 stripe_nr = offset;
2885 /*
2886 * stripe_nr counts the total number of stripes we have to stride
2887 * to get to this block
2888 */
2889 do_div(stripe_nr, map->stripe_len);
2890
2891 stripe_offset = stripe_nr * map->stripe_len;
2892 BUG_ON(offset < stripe_offset);
2893
2894 /* stripe_offset is the offset of this block in its stripe*/
2895 stripe_offset = offset - stripe_offset;
2896
Li Dongyangfce3bb92011-03-24 10:24:26 +00002897 if (rw & REQ_DISCARD)
2898 *length = min_t(u64, em->len - offset, *length);
2899 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2900 BTRFS_BLOCK_GROUP_RAID1 |
2901 BTRFS_BLOCK_GROUP_RAID10 |
2902 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002903 /* we limit the length of each bio to what fits in a stripe */
2904 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002905 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002906 } else {
2907 *length = em->len - offset;
2908 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002909
Jens Axboe7eaceac2011-03-10 08:52:07 +01002910 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002911 goto out;
2912
Chris Masonf2d8d742008-04-21 10:03:05 -04002913 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002914 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002915 stripe_nr_orig = stripe_nr;
2916 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2917 (~(map->stripe_len - 1));
2918 do_div(stripe_nr_end, map->stripe_len);
2919 stripe_end_offset = stripe_nr_end * map->stripe_len -
2920 (offset + *length);
2921 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2922 if (rw & REQ_DISCARD)
2923 num_stripes = min_t(u64, map->num_stripes,
2924 stripe_nr_end - stripe_nr_orig);
2925 stripe_index = do_div(stripe_nr, map->num_stripes);
2926 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07002927 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002928 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002929 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002930 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002931 else {
2932 stripe_index = find_live_mirror(map, 0,
2933 map->num_stripes,
2934 current->pid % map->num_stripes);
2935 }
Chris Mason2fff7342008-04-29 14:12:09 -04002936
Chris Mason611f0e02008-04-03 16:29:03 -04002937 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00002938 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002939 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002940 else if (mirror_num)
2941 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002942
Chris Mason321aecc2008-04-16 10:49:51 -04002943 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2944 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002945
2946 stripe_index = do_div(stripe_nr, factor);
2947 stripe_index *= map->sub_stripes;
2948
Jens Axboe7eaceac2011-03-10 08:52:07 +01002949 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04002950 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002951 else if (rw & REQ_DISCARD)
2952 num_stripes = min_t(u64, map->sub_stripes *
2953 (stripe_nr_end - stripe_nr_orig),
2954 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04002955 else if (mirror_num)
2956 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002957 else {
2958 stripe_index = find_live_mirror(map, stripe_index,
2959 map->sub_stripes, stripe_index +
2960 current->pid % map->sub_stripes);
2961 }
Chris Mason8790d502008-04-03 16:29:03 -04002962 } else {
2963 /*
2964 * after this do_div call, stripe_nr is the number of stripes
2965 * on this device we have to walk to find the data, and
2966 * stripe_index is the number of our device in the stripe array
2967 */
2968 stripe_index = do_div(stripe_nr, map->num_stripes);
2969 }
Chris Mason593060d2008-03-25 16:50:33 -04002970 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002971
Li Dongyangfce3bb92011-03-24 10:24:26 +00002972 if (rw & REQ_DISCARD) {
2973 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002974 multi->stripes[i].physical =
2975 map->stripes[stripe_index].physical +
2976 stripe_offset + stripe_nr * map->stripe_len;
2977 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002978
2979 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2980 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04002981 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002982 int j;
2983
Chris Masond9d04872011-03-27 21:23:21 -04002984 div_u64_rem(stripe_nr_end - 1,
2985 map->num_stripes,
2986 &last_stripe);
2987
Li Dongyangfce3bb92011-03-24 10:24:26 +00002988 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04002989 u32 test;
2990
2991 div_u64_rem(stripe_nr_end - 1 - j,
2992 map->num_stripes, &test);
2993 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00002994 break;
2995 }
2996 stripes = stripe_nr_end - 1 - j;
2997 do_div(stripes, map->num_stripes);
2998 multi->stripes[i].length = map->stripe_len *
2999 (stripes - stripe_nr + 1);
3000
3001 if (i == 0) {
3002 multi->stripes[i].length -=
3003 stripe_offset;
3004 stripe_offset = 0;
3005 }
3006 if (stripe_index == last_stripe)
3007 multi->stripes[i].length -=
3008 stripe_end_offset;
3009 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3010 u64 stripes;
3011 int j;
3012 int factor = map->num_stripes /
3013 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003014 u32 last_stripe = 0;
3015
3016 div_u64_rem(stripe_nr_end - 1,
3017 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003018 last_stripe *= map->sub_stripes;
3019
3020 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003021 u32 test;
3022
3023 div_u64_rem(stripe_nr_end - 1 - j,
3024 factor, &test);
3025
3026 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003027 stripe_index / map->sub_stripes)
3028 break;
3029 }
3030 stripes = stripe_nr_end - 1 - j;
3031 do_div(stripes, factor);
3032 multi->stripes[i].length = map->stripe_len *
3033 (stripes - stripe_nr + 1);
3034
3035 if (i < map->sub_stripes) {
3036 multi->stripes[i].length -=
3037 stripe_offset;
3038 if (i == map->sub_stripes - 1)
3039 stripe_offset = 0;
3040 }
3041 if (stripe_index >= last_stripe &&
3042 stripe_index <= (last_stripe +
3043 map->sub_stripes - 1)) {
3044 multi->stripes[i].length -=
3045 stripe_end_offset;
3046 }
3047 } else
3048 multi->stripes[i].length = *length;
3049
3050 stripe_index++;
3051 if (stripe_index == map->num_stripes) {
3052 /* This could only happen for RAID0/10 */
3053 stripe_index = 0;
3054 stripe_nr++;
3055 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003056 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003057 } else {
3058 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003059 multi->stripes[i].physical =
3060 map->stripes[stripe_index].physical +
3061 stripe_offset +
3062 stripe_nr * map->stripe_len;
3063 multi->stripes[i].dev =
3064 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003065 stripe_index++;
3066 }
Chris Mason593060d2008-03-25 16:50:33 -04003067 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003068 if (multi_ret) {
3069 *multi_ret = multi;
3070 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003071 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003072 }
Chris Masoncea9e442008-04-09 16:28:12 -04003073out:
Chris Mason0b86a832008-03-24 15:01:56 -04003074 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003075 return 0;
3076}
3077
Chris Masonf2d8d742008-04-21 10:03:05 -04003078int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3079 u64 logical, u64 *length,
3080 struct btrfs_multi_bio **multi_ret, int mirror_num)
3081{
3082 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003083 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003084}
3085
Yan Zhenga512bbf2008-12-08 16:46:26 -05003086int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3087 u64 chunk_start, u64 physical, u64 devid,
3088 u64 **logical, int *naddrs, int *stripe_len)
3089{
3090 struct extent_map_tree *em_tree = &map_tree->map_tree;
3091 struct extent_map *em;
3092 struct map_lookup *map;
3093 u64 *buf;
3094 u64 bytenr;
3095 u64 length;
3096 u64 stripe_nr;
3097 int i, j, nr = 0;
3098
Chris Mason890871b2009-09-02 16:24:52 -04003099 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003100 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003101 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003102
3103 BUG_ON(!em || em->start != chunk_start);
3104 map = (struct map_lookup *)em->bdev;
3105
3106 length = em->len;
3107 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3108 do_div(length, map->num_stripes / map->sub_stripes);
3109 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3110 do_div(length, map->num_stripes);
3111
3112 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3113 BUG_ON(!buf);
3114
3115 for (i = 0; i < map->num_stripes; i++) {
3116 if (devid && map->stripes[i].dev->devid != devid)
3117 continue;
3118 if (map->stripes[i].physical > physical ||
3119 map->stripes[i].physical + length <= physical)
3120 continue;
3121
3122 stripe_nr = physical - map->stripes[i].physical;
3123 do_div(stripe_nr, map->stripe_len);
3124
3125 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3126 stripe_nr = stripe_nr * map->num_stripes + i;
3127 do_div(stripe_nr, map->sub_stripes);
3128 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3129 stripe_nr = stripe_nr * map->num_stripes + i;
3130 }
3131 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003132 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003133 for (j = 0; j < nr; j++) {
3134 if (buf[j] == bytenr)
3135 break;
3136 }
Chris Mason934d3752008-12-08 16:43:10 -05003137 if (j == nr) {
3138 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003139 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003140 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003141 }
3142
Yan Zhenga512bbf2008-12-08 16:46:26 -05003143 *logical = buf;
3144 *naddrs = nr;
3145 *stripe_len = map->stripe_len;
3146
3147 free_extent_map(em);
3148 return 0;
3149}
3150
Chris Mason8790d502008-04-03 16:29:03 -04003151static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003152{
Chris Masoncea9e442008-04-09 16:28:12 -04003153 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003154 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003155
Chris Mason8790d502008-04-03 16:29:03 -04003156 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003157 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003158
Chris Mason7d2b4da2008-08-05 10:13:57 -04003159 if (bio == multi->orig_bio)
3160 is_orig_bio = 1;
3161
Chris Masoncea9e442008-04-09 16:28:12 -04003162 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003163 if (!is_orig_bio) {
3164 bio_put(bio);
3165 bio = multi->orig_bio;
3166 }
Chris Mason8790d502008-04-03 16:29:03 -04003167 bio->bi_private = multi->private;
3168 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003169 /* only send an error to the higher layers if it is
3170 * beyond the tolerance of the multi-bio
3171 */
Chris Mason1259ab72008-05-12 13:39:03 -04003172 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003173 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003174 } else if (err) {
3175 /*
3176 * this bio is actually up to date, we didn't
3177 * go over the max number of errors
3178 */
3179 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003180 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003181 }
Chris Mason8790d502008-04-03 16:29:03 -04003182 kfree(multi);
3183
3184 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003185 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003186 bio_put(bio);
3187 }
Chris Mason8790d502008-04-03 16:29:03 -04003188}
3189
Chris Mason8b712842008-06-11 16:50:36 -04003190struct async_sched {
3191 struct bio *bio;
3192 int rw;
3193 struct btrfs_fs_info *info;
3194 struct btrfs_work work;
3195};
3196
3197/*
3198 * see run_scheduled_bios for a description of why bios are collected for
3199 * async submit.
3200 *
3201 * This will add one bio to the pending list for a device and make sure
3202 * the work struct is scheduled.
3203 */
Chris Masond3977122009-01-05 21:25:51 -05003204static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003205 struct btrfs_device *device,
3206 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003207{
3208 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003209 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003210
3211 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003212 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003213 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003214 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003215 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003216 return 0;
3217 }
3218
3219 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003220 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003221 * higher layers. Otherwise, the async bio makes it appear we have
3222 * made progress against dirty pages when we've really just put it
3223 * on a queue for later
3224 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003225 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003226 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003227 bio->bi_next = NULL;
3228 bio->bi_rw |= rw;
3229
3230 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003231 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003232 pending_bios = &device->pending_sync_bios;
3233 else
3234 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003235
Chris Masonffbd5172009-04-20 15:50:09 -04003236 if (pending_bios->tail)
3237 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003238
Chris Masonffbd5172009-04-20 15:50:09 -04003239 pending_bios->tail = bio;
3240 if (!pending_bios->head)
3241 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003242 if (device->running_pending)
3243 should_queue = 0;
3244
3245 spin_unlock(&device->io_lock);
3246
3247 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003248 btrfs_queue_worker(&root->fs_info->submit_workers,
3249 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003250 return 0;
3251}
3252
Chris Masonf1885912008-04-09 16:28:12 -04003253int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003254 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003255{
3256 struct btrfs_mapping_tree *map_tree;
3257 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003258 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003259 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003260 u64 length = 0;
3261 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003262 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003263 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003264 int dev_nr = 0;
3265 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003266
Chris Masonf2d8d742008-04-21 10:03:05 -04003267 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003268 map_tree = &root->fs_info->mapping_tree;
3269 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003270
Chris Masonf1885912008-04-09 16:28:12 -04003271 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3272 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003273 BUG_ON(ret);
3274
3275 total_devs = multi->num_stripes;
3276 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003277 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3278 "len %llu\n", (unsigned long long)logical,
3279 (unsigned long long)length,
3280 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003281 BUG();
3282 }
3283 multi->end_io = first_bio->bi_end_io;
3284 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003285 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003286 atomic_set(&multi->stripes_pending, multi->num_stripes);
3287
Chris Masond3977122009-01-05 21:25:51 -05003288 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003289 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003290 if (dev_nr < total_devs - 1) {
3291 bio = bio_clone(first_bio, GFP_NOFS);
3292 BUG_ON(!bio);
3293 } else {
3294 bio = first_bio;
3295 }
3296 bio->bi_private = multi;
3297 bio->bi_end_io = end_bio_multi_stripe;
3298 }
Chris Masoncea9e442008-04-09 16:28:12 -04003299 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3300 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003301 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003302 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003303 if (async_submit)
3304 schedule_bio(root, dev, rw, bio);
3305 else
3306 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003307 } else {
3308 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3309 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003310 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003311 }
Chris Mason8790d502008-04-03 16:29:03 -04003312 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003313 }
Chris Masoncea9e442008-04-09 16:28:12 -04003314 if (total_devs == 1)
3315 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003316 return 0;
3317}
3318
Chris Masona4437552008-04-18 10:29:38 -04003319struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003320 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003321{
Yan Zheng2b820322008-11-17 21:11:30 -05003322 struct btrfs_device *device;
3323 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003324
Yan Zheng2b820322008-11-17 21:11:30 -05003325 cur_devices = root->fs_info->fs_devices;
3326 while (cur_devices) {
3327 if (!fsid ||
3328 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3329 device = __find_device(&cur_devices->devices,
3330 devid, uuid);
3331 if (device)
3332 return device;
3333 }
3334 cur_devices = cur_devices->seed;
3335 }
3336 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003337}
3338
Chris Masondfe25022008-05-13 13:46:40 -04003339static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3340 u64 devid, u8 *dev_uuid)
3341{
3342 struct btrfs_device *device;
3343 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3344
3345 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003346 if (!device)
3347 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003348 list_add(&device->dev_list,
3349 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003350 device->dev_root = root->fs_info->dev_root;
3351 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003352 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003353 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003354 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003355 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003356 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003357 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003358 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003359 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3360 return device;
3361}
3362
Chris Mason0b86a832008-03-24 15:01:56 -04003363static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3364 struct extent_buffer *leaf,
3365 struct btrfs_chunk *chunk)
3366{
3367 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3368 struct map_lookup *map;
3369 struct extent_map *em;
3370 u64 logical;
3371 u64 length;
3372 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003373 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003374 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003375 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003376 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003377
Chris Masone17cade2008-04-15 15:41:47 -04003378 logical = key->offset;
3379 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003380
Chris Mason890871b2009-09-02 16:24:52 -04003381 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003382 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003383 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003384
3385 /* already mapped? */
3386 if (em && em->start <= logical && em->start + em->len > logical) {
3387 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003388 return 0;
3389 } else if (em) {
3390 free_extent_map(em);
3391 }
Chris Mason0b86a832008-03-24 15:01:56 -04003392
David Sterba172ddd62011-04-21 00:48:27 +02003393 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003394 if (!em)
3395 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003396 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3397 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003398 if (!map) {
3399 free_extent_map(em);
3400 return -ENOMEM;
3401 }
3402
3403 em->bdev = (struct block_device *)map;
3404 em->start = logical;
3405 em->len = length;
3406 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003407 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003408
Chris Mason593060d2008-03-25 16:50:33 -04003409 map->num_stripes = num_stripes;
3410 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3411 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3412 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3413 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3414 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003415 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003416 for (i = 0; i < num_stripes; i++) {
3417 map->stripes[i].physical =
3418 btrfs_stripe_offset_nr(leaf, chunk, i);
3419 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003420 read_extent_buffer(leaf, uuid, (unsigned long)
3421 btrfs_stripe_dev_uuid_nr(chunk, i),
3422 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003423 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3424 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003425 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003426 kfree(map);
3427 free_extent_map(em);
3428 return -EIO;
3429 }
Chris Masondfe25022008-05-13 13:46:40 -04003430 if (!map->stripes[i].dev) {
3431 map->stripes[i].dev =
3432 add_missing_dev(root, devid, uuid);
3433 if (!map->stripes[i].dev) {
3434 kfree(map);
3435 free_extent_map(em);
3436 return -EIO;
3437 }
3438 }
3439 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003440 }
3441
Chris Mason890871b2009-09-02 16:24:52 -04003442 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003443 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003444 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003445 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003446 free_extent_map(em);
3447
3448 return 0;
3449}
3450
3451static int fill_device_from_item(struct extent_buffer *leaf,
3452 struct btrfs_dev_item *dev_item,
3453 struct btrfs_device *device)
3454{
3455 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003456
3457 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003458 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3459 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003460 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3461 device->type = btrfs_device_type(leaf, dev_item);
3462 device->io_align = btrfs_device_io_align(leaf, dev_item);
3463 device->io_width = btrfs_device_io_width(leaf, dev_item);
3464 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003465
3466 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003467 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003468
Chris Mason0b86a832008-03-24 15:01:56 -04003469 return 0;
3470}
3471
Yan Zheng2b820322008-11-17 21:11:30 -05003472static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3473{
3474 struct btrfs_fs_devices *fs_devices;
3475 int ret;
3476
3477 mutex_lock(&uuid_mutex);
3478
3479 fs_devices = root->fs_info->fs_devices->seed;
3480 while (fs_devices) {
3481 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3482 ret = 0;
3483 goto out;
3484 }
3485 fs_devices = fs_devices->seed;
3486 }
3487
3488 fs_devices = find_fsid(fsid);
3489 if (!fs_devices) {
3490 ret = -ENOENT;
3491 goto out;
3492 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003493
3494 fs_devices = clone_fs_devices(fs_devices);
3495 if (IS_ERR(fs_devices)) {
3496 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003497 goto out;
3498 }
3499
Christoph Hellwig97288f22008-12-02 06:36:09 -05003500 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003501 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003502 if (ret)
3503 goto out;
3504
3505 if (!fs_devices->seeding) {
3506 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003507 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003508 ret = -EINVAL;
3509 goto out;
3510 }
3511
3512 fs_devices->seed = root->fs_info->fs_devices->seed;
3513 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003514out:
3515 mutex_unlock(&uuid_mutex);
3516 return ret;
3517}
3518
Chris Mason0d81ba52008-03-24 15:02:07 -04003519static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003520 struct extent_buffer *leaf,
3521 struct btrfs_dev_item *dev_item)
3522{
3523 struct btrfs_device *device;
3524 u64 devid;
3525 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003526 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003527 u8 dev_uuid[BTRFS_UUID_SIZE];
3528
Chris Mason0b86a832008-03-24 15:01:56 -04003529 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003530 read_extent_buffer(leaf, dev_uuid,
3531 (unsigned long)btrfs_device_uuid(dev_item),
3532 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003533 read_extent_buffer(leaf, fs_uuid,
3534 (unsigned long)btrfs_device_fsid(dev_item),
3535 BTRFS_UUID_SIZE);
3536
3537 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3538 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003539 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003540 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003541 }
3542
3543 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3544 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003545 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003546 return -EIO;
3547
3548 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003549 printk(KERN_WARNING "warning devid %llu missing\n",
3550 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003551 device = add_missing_dev(root, devid, dev_uuid);
3552 if (!device)
3553 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003554 } else if (!device->missing) {
3555 /*
3556 * this happens when a device that was properly setup
3557 * in the device info lists suddenly goes bad.
3558 * device->bdev is NULL, and so we have to set
3559 * device->missing to one here
3560 */
3561 root->fs_info->fs_devices->missing_devices++;
3562 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003563 }
3564 }
3565
3566 if (device->fs_devices != root->fs_info->fs_devices) {
3567 BUG_ON(device->writeable);
3568 if (device->generation !=
3569 btrfs_device_generation(leaf, dev_item))
3570 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003571 }
Chris Mason0b86a832008-03-24 15:01:56 -04003572
3573 fill_device_from_item(leaf, dev_item, device);
3574 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003575 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003576 if (device->writeable)
3577 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003578 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003579 return ret;
3580}
3581
Yan Zhenge4404d62008-12-12 10:03:26 -05003582int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003583{
3584 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003585 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003586 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003587 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003588 u8 *ptr;
3589 unsigned long sb_ptr;
3590 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003591 u32 num_stripes;
3592 u32 array_size;
3593 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003594 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003595 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003596
Yan Zhenge4404d62008-12-12 10:03:26 -05003597 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003598 BTRFS_SUPER_INFO_SIZE);
3599 if (!sb)
3600 return -ENOMEM;
3601 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04003602 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05003603
Chris Masona061fc82008-05-07 11:43:44 -04003604 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003605 array_size = btrfs_super_sys_array_size(super_copy);
3606
Chris Mason0b86a832008-03-24 15:01:56 -04003607 ptr = super_copy->sys_chunk_array;
3608 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3609 cur = 0;
3610
3611 while (cur < array_size) {
3612 disk_key = (struct btrfs_disk_key *)ptr;
3613 btrfs_disk_key_to_cpu(&key, disk_key);
3614
Chris Masona061fc82008-05-07 11:43:44 -04003615 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003616 sb_ptr += len;
3617 cur += len;
3618
Chris Mason0d81ba52008-03-24 15:02:07 -04003619 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003620 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003621 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003622 if (ret)
3623 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003624 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3625 len = btrfs_chunk_item_size(num_stripes);
3626 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003627 ret = -EIO;
3628 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003629 }
3630 ptr += len;
3631 sb_ptr += len;
3632 cur += len;
3633 }
Chris Masona061fc82008-05-07 11:43:44 -04003634 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003635 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003636}
3637
3638int btrfs_read_chunk_tree(struct btrfs_root *root)
3639{
3640 struct btrfs_path *path;
3641 struct extent_buffer *leaf;
3642 struct btrfs_key key;
3643 struct btrfs_key found_key;
3644 int ret;
3645 int slot;
3646
3647 root = root->fs_info->chunk_root;
3648
3649 path = btrfs_alloc_path();
3650 if (!path)
3651 return -ENOMEM;
3652
3653 /* first we search for all of the device items, and then we
3654 * read in all of the chunk items. This way we can create chunk
3655 * mappings that reference all of the devices that are afound
3656 */
3657 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3658 key.offset = 0;
3659 key.type = 0;
3660again:
3661 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003662 if (ret < 0)
3663 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003664 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003665 leaf = path->nodes[0];
3666 slot = path->slots[0];
3667 if (slot >= btrfs_header_nritems(leaf)) {
3668 ret = btrfs_next_leaf(root, path);
3669 if (ret == 0)
3670 continue;
3671 if (ret < 0)
3672 goto error;
3673 break;
3674 }
3675 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3676 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3677 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3678 break;
3679 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3680 struct btrfs_dev_item *dev_item;
3681 dev_item = btrfs_item_ptr(leaf, slot,
3682 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003683 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003684 if (ret)
3685 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003686 }
3687 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3688 struct btrfs_chunk *chunk;
3689 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3690 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003691 if (ret)
3692 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003693 }
3694 path->slots[0]++;
3695 }
3696 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3697 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003698 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003699 goto again;
3700 }
Chris Mason0b86a832008-03-24 15:01:56 -04003701 ret = 0;
3702error:
Yan Zheng2b820322008-11-17 21:11:30 -05003703 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003704 return ret;
3705}