blob: f08210e8333925f7a622c729d6052f8168324545 [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;
Miao Xie0e588852011-08-05 09:32:37 +0000145 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400146 struct blk_plug plug;
147
148 /*
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
153 */
154 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400155
Chris Masonbedf7622009-04-03 10:32:58 -0400156 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400157 fs_info = device->dev_root->fs_info;
158 limit = btrfs_async_submit_limit(fs_info);
159 limit = limit * 2 / 3;
160
Chris Mason8b712842008-06-11 16:50:36 -0400161loop:
162 spin_lock(&device->io_lock);
163
Chris Masona6837052009-02-04 09:19:41 -0500164loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400165 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400166
Chris Mason8b712842008-06-11 16:50:36 -0400167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
171 */
Chris Masond84275c2009-06-09 15:39:08 -0400172 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400173 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400174 force_reg = 1;
175 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400176 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400177 force_reg = 0;
178 }
Chris Masonffbd5172009-04-20 15:50:09 -0400179
180 pending = pending_bios->head;
181 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400182 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400183
184 /*
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
188 *
189 * device->running_pending is used to synchronize with the
190 * schedule_bio code.
191 */
Chris Masonffbd5172009-04-20 15:50:09 -0400192 if (device->pending_sync_bios.head == NULL &&
193 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400194 again = 0;
195 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400196 } else {
197 again = 1;
198 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400199 }
Chris Masonffbd5172009-04-20 15:50:09 -0400200
201 pending_bios->head = NULL;
202 pending_bios->tail = NULL;
203
Chris Mason8b712842008-06-11 16:50:36 -0400204 spin_unlock(&device->io_lock);
205
Chris Masond3977122009-01-05 21:25:51 -0500206 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400207
208 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
211 */
212 if ((num_run > 32 &&
213 pending_bios != &device->pending_sync_bios &&
214 device->pending_sync_bios.head) ||
215 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
216 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400217 spin_lock(&device->io_lock);
218 requeue_list(pending_bios, pending, tail);
219 goto loop_lock;
220 }
221
Chris Mason8b712842008-06-11 16:50:36 -0400222 cur = pending;
223 pending = pending->bi_next;
224 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400225 atomic_dec(&fs_info->nr_async_bios);
226
227 if (atomic_read(&fs_info->nr_async_bios) < limit &&
228 waitqueue_active(&fs_info->async_submit_wait))
229 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400230
231 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400232
Chris Mason2ab1ba62011-08-04 14:28:36 -0400233 /*
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
236 *
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
239 * we add more
240 */
241 if (pending_bios == &device->pending_sync_bios) {
242 sync_pending = 1;
243 } else if (sync_pending) {
244 blk_finish_plug(&plug);
245 blk_start_plug(&plug);
246 sync_pending = 0;
247 }
248
Chris Mason5ff7ba32010-03-15 10:21:30 -0400249 submit_bio(cur->bi_rw, cur);
250 num_run++;
251 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100252 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400253 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400254
255 /*
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
258 * run instead
259 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400260 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500261 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400262 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400263
Chris Masonb765ead2009-04-03 10:27:10 -0400264 ioc = current->io_context;
265
266 /*
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
270 *
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
274 */
275 if (ioc && ioc->nr_batch_requests > 0 &&
276 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
277 (last_waited == 0 ||
278 ioc->last_waited == last_waited)) {
279 /*
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
284 */
285 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100286 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400287 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400288 continue;
289 }
Chris Mason8b712842008-06-11 16:50:36 -0400290 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400291 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500292 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400293
294 spin_unlock(&device->io_lock);
295 btrfs_requeue_work(&device->work);
296 goto done;
297 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500298 /* unplug every 64 requests just for good measure */
299 if (batch_run % 64 == 0) {
300 blk_finish_plug(&plug);
301 blk_start_plug(&plug);
302 sync_pending = 0;
303 }
Chris Mason8b712842008-06-11 16:50:36 -0400304 }
Chris Masonffbd5172009-04-20 15:50:09 -0400305
Chris Mason51684082010-03-10 15:33:32 -0500306 cond_resched();
307 if (again)
308 goto loop;
309
310 spin_lock(&device->io_lock);
311 if (device->pending_bios.head || device->pending_sync_bios.head)
312 goto loop_lock;
313 spin_unlock(&device->io_lock);
314
Chris Mason8b712842008-06-11 16:50:36 -0400315done:
Chris Mason211588a2011-04-19 20:12:40 -0400316 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400317 return 0;
318}
319
Christoph Hellwigb2950862008-12-02 09:54:17 -0500320static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400321{
322 struct btrfs_device *device;
323
324 device = container_of(work, struct btrfs_device, work);
325 run_scheduled_bios(device);
326}
327
Chris Masona1b32a52008-09-05 16:09:51 -0400328static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400329 struct btrfs_super_block *disk_super,
330 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
331{
332 struct btrfs_device *device;
333 struct btrfs_fs_devices *fs_devices;
334 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000335 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336
337 fs_devices = find_fsid(disk_super->fsid);
338 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400339 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400340 if (!fs_devices)
341 return -ENOMEM;
342 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400343 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344 list_add(&fs_devices->list, &fs_uuids);
345 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
346 fs_devices->latest_devid = devid;
347 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400348 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400349 device = NULL;
350 } else {
Chris Masona4437552008-04-18 10:29:38 -0400351 device = __find_device(&fs_devices->devices, devid,
352 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400353 }
354 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500355 if (fs_devices->opened)
356 return -EBUSY;
357
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358 device = kzalloc(sizeof(*device), GFP_NOFS);
359 if (!device) {
360 /* we can safely leave the fs_devices entry around */
361 return -ENOMEM;
362 }
363 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400364 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400365 memcpy(device->uuid, disk_super->dev_item.uuid,
366 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400367 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 device->name = kstrdup(path, GFP_NOFS);
369 if (!device->name) {
370 kfree(device);
371 return -ENOMEM;
372 }
Yan Zheng2b820322008-11-17 21:11:30 -0500373 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400374
Arne Jansen90519d62011-05-23 14:30:00 +0200375 /* init readahead state */
376 spin_lock_init(&device->reada_lock);
377 device->reada_curr_zone = NULL;
378 atomic_set(&device->reada_in_flight, 0);
379 device->reada_next = 0;
380 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
381 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
382
Chris Masone5e9a522009-06-10 15:17:02 -0400383 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000384 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_unlock(&fs_devices->device_list_mutex);
386
Yan Zheng2b820322008-11-17 21:11:30 -0500387 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500389 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000390 name = kstrdup(path, GFP_NOFS);
391 if (!name)
392 return -ENOMEM;
393 kfree(device->name);
394 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500395 if (device->missing) {
396 fs_devices->missing_devices--;
397 device->missing = 0;
398 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 }
400
401 if (found_transid > fs_devices->latest_trans) {
402 fs_devices->latest_devid = devid;
403 fs_devices->latest_trans = found_transid;
404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 *fs_devices_ret = fs_devices;
406 return 0;
407}
408
Yan Zhenge4404d62008-12-12 10:03:26 -0500409static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
410{
411 struct btrfs_fs_devices *fs_devices;
412 struct btrfs_device *device;
413 struct btrfs_device *orig_dev;
414
415 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
416 if (!fs_devices)
417 return ERR_PTR(-ENOMEM);
418
419 INIT_LIST_HEAD(&fs_devices->devices);
420 INIT_LIST_HEAD(&fs_devices->alloc_list);
421 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400422 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500423 fs_devices->latest_devid = orig->latest_devid;
424 fs_devices->latest_trans = orig->latest_trans;
425 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
426
Xiao Guangrong46224702011-04-20 10:08:47 +0000427 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500428 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
429 device = kzalloc(sizeof(*device), GFP_NOFS);
430 if (!device)
431 goto error;
432
433 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400434 if (!device->name) {
435 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400437 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500438
439 device->devid = orig_dev->devid;
440 device->work.func = pending_bios_fn;
441 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500442 spin_lock_init(&device->io_lock);
443 INIT_LIST_HEAD(&device->dev_list);
444 INIT_LIST_HEAD(&device->dev_alloc_list);
445
446 list_add(&device->dev_list, &fs_devices->devices);
447 device->fs_devices = fs_devices;
448 fs_devices->num_devices++;
449 }
450 return fs_devices;
451error:
452 free_fs_devices(fs_devices);
453 return ERR_PTR(-ENOMEM);
454}
455
Chris Masondfe25022008-05-13 13:46:40 -0400456int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
457{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500458 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400459
460 mutex_lock(&uuid_mutex);
461again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000462 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500463 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500464 if (device->in_fs_metadata)
465 continue;
466
467 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100468 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500469 device->bdev = NULL;
470 fs_devices->open_devices--;
471 }
472 if (device->writeable) {
473 list_del_init(&device->dev_alloc_list);
474 device->writeable = 0;
475 fs_devices->rw_devices--;
476 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500477 list_del_init(&device->dev_list);
478 fs_devices->num_devices--;
479 kfree(device->name);
480 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400481 }
Yan Zheng2b820322008-11-17 21:11:30 -0500482
483 if (fs_devices->seed) {
484 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500485 goto again;
486 }
487
Chris Masondfe25022008-05-13 13:46:40 -0400488 mutex_unlock(&uuid_mutex);
489 return 0;
490}
Chris Masona0af4692008-05-13 16:03:06 -0400491
Xiao Guangrong1f781602011-04-20 10:09:16 +0000492static void __free_device(struct work_struct *work)
493{
494 struct btrfs_device *device;
495
496 device = container_of(work, struct btrfs_device, rcu_work);
497
498 if (device->bdev)
499 blkdev_put(device->bdev, device->mode);
500
501 kfree(device->name);
502 kfree(device);
503}
504
505static void free_device(struct rcu_head *head)
506{
507 struct btrfs_device *device;
508
509 device = container_of(head, struct btrfs_device, rcu);
510
511 INIT_WORK(&device->rcu_work, __free_device);
512 schedule_work(&device->rcu_work);
513}
514
Yan Zheng2b820322008-11-17 21:11:30 -0500515static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400516{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400517 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500518
Yan Zheng2b820322008-11-17 21:11:30 -0500519 if (--fs_devices->opened > 0)
520 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400521
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000522 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500523 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000524 struct btrfs_device *new_device;
525
526 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400527 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000528
Yan Zheng2b820322008-11-17 21:11:30 -0500529 if (device->writeable) {
530 list_del_init(&device->dev_alloc_list);
531 fs_devices->rw_devices--;
532 }
533
Josef Bacikd5e20032011-08-04 14:52:27 +0000534 if (device->can_discard)
535 fs_devices->num_can_discard--;
536
Xiao Guangrong1f781602011-04-20 10:09:16 +0000537 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
538 BUG_ON(!new_device);
539 memcpy(new_device, device, sizeof(*new_device));
540 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000541 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000542 new_device->bdev = NULL;
543 new_device->writeable = 0;
544 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000545 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000546 list_replace_rcu(&device->dev_list, &new_device->dev_list);
547
548 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000550 mutex_unlock(&fs_devices->device_list_mutex);
551
Yan Zhenge4404d62008-12-12 10:03:26 -0500552 WARN_ON(fs_devices->open_devices);
553 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500554 fs_devices->opened = 0;
555 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500556
Chris Mason8a4b83c2008-03-24 15:02:07 -0400557 return 0;
558}
559
Yan Zheng2b820322008-11-17 21:11:30 -0500560int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
561{
Yan Zhenge4404d62008-12-12 10:03:26 -0500562 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500563 int ret;
564
565 mutex_lock(&uuid_mutex);
566 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500567 if (!fs_devices->opened) {
568 seed_devices = fs_devices->seed;
569 fs_devices->seed = NULL;
570 }
Yan Zheng2b820322008-11-17 21:11:30 -0500571 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500572
573 while (seed_devices) {
574 fs_devices = seed_devices;
575 seed_devices = fs_devices->seed;
576 __btrfs_close_devices(fs_devices);
577 free_fs_devices(fs_devices);
578 }
Yan Zheng2b820322008-11-17 21:11:30 -0500579 return ret;
580}
581
Yan Zhenge4404d62008-12-12 10:03:26 -0500582static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
583 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400584{
Josef Bacikd5e20032011-08-04 14:52:27 +0000585 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400586 struct block_device *bdev;
587 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400589 struct block_device *latest_bdev = NULL;
590 struct buffer_head *bh;
591 struct btrfs_super_block *disk_super;
592 u64 latest_devid = 0;
593 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400594 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500595 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400596 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400597
Tejun Heod4d77622010-11-13 11:55:18 +0100598 flags |= FMODE_EXCL;
599
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500600 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400601 if (device->bdev)
602 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400603 if (!device->name)
604 continue;
605
Tejun Heod4d77622010-11-13 11:55:18 +0100606 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400607 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500608 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400609 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400610 }
Chris Masona061fc82008-05-07 11:43:44 -0400611 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400612
Yan Zhenga512bbf2008-12-08 16:46:26 -0500613 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300614 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400615 goto error_close;
616
617 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000618 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400619 if (devid != device->devid)
620 goto error_brelse;
621
Yan Zheng2b820322008-11-17 21:11:30 -0500622 if (memcmp(device->uuid, disk_super->dev_item.uuid,
623 BTRFS_UUID_SIZE))
624 goto error_brelse;
625
626 device->generation = btrfs_super_generation(disk_super);
627 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400628 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500629 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400630 latest_bdev = bdev;
631 }
632
Yan Zheng2b820322008-11-17 21:11:30 -0500633 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
634 device->writeable = 0;
635 } else {
636 device->writeable = !bdev_read_only(bdev);
637 seeding = 0;
638 }
639
Josef Bacikd5e20032011-08-04 14:52:27 +0000640 q = bdev_get_queue(bdev);
641 if (blk_queue_discard(q)) {
642 device->can_discard = 1;
643 fs_devices->num_can_discard++;
644 }
645
Chris Mason8a4b83c2008-03-24 15:02:07 -0400646 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400647 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500648 device->mode = flags;
649
Chris Masonc2898112009-06-10 09:51:32 -0400650 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
651 fs_devices->rotating = 1;
652
Chris Masona0af4692008-05-13 16:03:06 -0400653 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500654 if (device->writeable) {
655 fs_devices->rw_devices++;
656 list_add(&device->dev_alloc_list,
657 &fs_devices->alloc_list);
658 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000659 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400660 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400661
Chris Masona0af4692008-05-13 16:03:06 -0400662error_brelse:
663 brelse(bh);
664error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100665 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400666error:
667 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400668 }
Chris Masona0af4692008-05-13 16:03:06 -0400669 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300670 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400671 goto out;
672 }
Yan Zheng2b820322008-11-17 21:11:30 -0500673 fs_devices->seeding = seeding;
674 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400675 fs_devices->latest_bdev = latest_bdev;
676 fs_devices->latest_devid = latest_devid;
677 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500678 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400679out:
Yan Zheng2b820322008-11-17 21:11:30 -0500680 return ret;
681}
682
683int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500684 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500685{
686 int ret;
687
688 mutex_lock(&uuid_mutex);
689 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500690 fs_devices->opened++;
691 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500692 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500693 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500694 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400695 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 return ret;
697}
698
Christoph Hellwig97288f22008-12-02 06:36:09 -0500699int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400700 struct btrfs_fs_devices **fs_devices_ret)
701{
702 struct btrfs_super_block *disk_super;
703 struct block_device *bdev;
704 struct buffer_head *bh;
705 int ret;
706 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400707 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708
709 mutex_lock(&uuid_mutex);
710
Tejun Heod4d77622010-11-13 11:55:18 +0100711 flags |= FMODE_EXCL;
712 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713
714 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400715 ret = PTR_ERR(bdev);
716 goto error;
717 }
718
719 ret = set_blocksize(bdev, 4096);
720 if (ret)
721 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500722 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400723 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000724 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400725 goto error_close;
726 }
727 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000728 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400729 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400730 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500731 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200732 else
733 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500734 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500735 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
737
Chris Mason8a4b83c2008-03-24 15:02:07 -0400738 brelse(bh);
739error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100740 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741error:
742 mutex_unlock(&uuid_mutex);
743 return ret;
744}
Chris Mason0b86a832008-03-24 15:01:56 -0400745
Miao Xie6d07bce2011-01-05 10:07:31 +0000746/* helper to account the used device space in the range */
747int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
748 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400749{
750 struct btrfs_key key;
751 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000752 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500753 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000754 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400755 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000756 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400757 struct extent_buffer *l;
758
Miao Xie6d07bce2011-01-05 10:07:31 +0000759 *length = 0;
760
761 if (start >= device->total_bytes)
762 return 0;
763
Yan Zheng2b820322008-11-17 21:11:30 -0500764 path = btrfs_alloc_path();
765 if (!path)
766 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400767 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400768
Chris Mason0b86a832008-03-24 15:01:56 -0400769 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000770 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400771 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000772
773 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400774 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000775 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400776 if (ret > 0) {
777 ret = btrfs_previous_item(root, path, key.objectid, key.type);
778 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000779 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400780 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000781
Chris Mason0b86a832008-03-24 15:01:56 -0400782 while (1) {
783 l = path->nodes[0];
784 slot = path->slots[0];
785 if (slot >= btrfs_header_nritems(l)) {
786 ret = btrfs_next_leaf(root, path);
787 if (ret == 0)
788 continue;
789 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000790 goto out;
791
792 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400793 }
794 btrfs_item_key_to_cpu(l, &key, slot);
795
796 if (key.objectid < device->devid)
797 goto next;
798
799 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000800 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400801
Chris Masond3977122009-01-05 21:25:51 -0500802 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400803 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400804
Chris Mason0b86a832008-03-24 15:01:56 -0400805 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000806 extent_end = key.offset + btrfs_dev_extent_length(l,
807 dev_extent);
808 if (key.offset <= start && extent_end > end) {
809 *length = end - start + 1;
810 break;
811 } else if (key.offset <= start && extent_end > start)
812 *length += extent_end - start;
813 else if (key.offset > start && extent_end <= end)
814 *length += extent_end - key.offset;
815 else if (key.offset > start && key.offset <= end) {
816 *length += end - key.offset + 1;
817 break;
818 } else if (key.offset > end)
819 break;
820
821next:
822 path->slots[0]++;
823 }
824 ret = 0;
825out:
826 btrfs_free_path(path);
827 return ret;
828}
829
Chris Mason0b86a832008-03-24 15:01:56 -0400830/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000831 * find_free_dev_extent - find free space in the specified device
832 * @trans: transaction handler
833 * @device: the device which we search the free space in
834 * @num_bytes: the size of the free space that we need
835 * @start: store the start of the free space.
836 * @len: the size of the free space. that we find, or the size of the max
837 * free space if we don't find suitable free space
838 *
Chris Mason0b86a832008-03-24 15:01:56 -0400839 * this uses a pretty simple search, the expectation is that it is
840 * called very infrequently and that a given device has a small number
841 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000842 *
843 * @start is used to store the start of the free space if we find. But if we
844 * don't find suitable free space, it will be used to store the start position
845 * of the max free space.
846 *
847 * @len is used to store the size of the free space that we find.
848 * But if we don't find suitable free space, it is used to store the size of
849 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400850 */
851int find_free_dev_extent(struct btrfs_trans_handle *trans,
852 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000853 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400854{
855 struct btrfs_key key;
856 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400858 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000859 u64 hole_size;
860 u64 max_hole_start;
861 u64 max_hole_size;
862 u64 extent_end;
863 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400864 u64 search_end = device->total_bytes;
865 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000866 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400867 struct extent_buffer *l;
868
Chris Mason0b86a832008-03-24 15:01:56 -0400869 /* FIXME use last free of some kind */
870
871 /* we don't want to overwrite the superblock on the drive,
872 * so we make sure to start at an offset of at least 1MB
873 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200874 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400875
Miao Xie7bfc8372011-01-05 10:07:26 +0000876 max_hole_start = search_start;
877 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000878 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000879
880 if (search_start >= search_end) {
881 ret = -ENOSPC;
882 goto error;
883 }
884
885 path = btrfs_alloc_path();
886 if (!path) {
887 ret = -ENOMEM;
888 goto error;
889 }
890 path->reada = 2;
891
Chris Mason0b86a832008-03-24 15:01:56 -0400892 key.objectid = device->devid;
893 key.offset = search_start;
894 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000895
Chris Mason0b86a832008-03-24 15:01:56 -0400896 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
897 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000898 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400899 if (ret > 0) {
900 ret = btrfs_previous_item(root, path, key.objectid, key.type);
901 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000902 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400903 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000904
Chris Mason0b86a832008-03-24 15:01:56 -0400905 while (1) {
906 l = path->nodes[0];
907 slot = path->slots[0];
908 if (slot >= btrfs_header_nritems(l)) {
909 ret = btrfs_next_leaf(root, path);
910 if (ret == 0)
911 continue;
912 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 goto out;
914
915 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400916 }
917 btrfs_item_key_to_cpu(l, &key, slot);
918
919 if (key.objectid < device->devid)
920 goto next;
921
922 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000923 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400924
Chris Mason0b86a832008-03-24 15:01:56 -0400925 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
926 goto next;
927
Miao Xie7bfc8372011-01-05 10:07:26 +0000928 if (key.offset > search_start) {
929 hole_size = key.offset - search_start;
930
931 if (hole_size > max_hole_size) {
932 max_hole_start = search_start;
933 max_hole_size = hole_size;
934 }
935
936 /*
937 * If this free space is greater than which we need,
938 * it must be the max free space that we have found
939 * until now, so max_hole_start must point to the start
940 * of this free space and the length of this free space
941 * is stored in max_hole_size. Thus, we return
942 * max_hole_start and max_hole_size and go back to the
943 * caller.
944 */
945 if (hole_size >= num_bytes) {
946 ret = 0;
947 goto out;
948 }
949 }
950
Chris Mason0b86a832008-03-24 15:01:56 -0400951 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000952 extent_end = key.offset + btrfs_dev_extent_length(l,
953 dev_extent);
954 if (extent_end > search_start)
955 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400956next:
957 path->slots[0]++;
958 cond_resched();
959 }
Chris Mason0b86a832008-03-24 15:01:56 -0400960
liubo38c01b92011-08-02 02:39:03 +0000961 /*
962 * At this point, search_start should be the end of
963 * allocated dev extents, and when shrinking the device,
964 * search_end may be smaller than search_start.
965 */
966 if (search_end > search_start)
967 hole_size = search_end - search_start;
968
Miao Xie7bfc8372011-01-05 10:07:26 +0000969 if (hole_size > max_hole_size) {
970 max_hole_start = search_start;
971 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400972 }
Chris Mason0b86a832008-03-24 15:01:56 -0400973
Miao Xie7bfc8372011-01-05 10:07:26 +0000974 /* See above. */
975 if (hole_size < num_bytes)
976 ret = -ENOSPC;
977 else
978 ret = 0;
979
980out:
Yan Zheng2b820322008-11-17 21:11:30 -0500981 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000982error:
983 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000984 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000985 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400986 return ret;
987}
988
Christoph Hellwigb2950862008-12-02 09:54:17 -0500989static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400990 struct btrfs_device *device,
991 u64 start)
992{
993 int ret;
994 struct btrfs_path *path;
995 struct btrfs_root *root = device->dev_root;
996 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400997 struct btrfs_key found_key;
998 struct extent_buffer *leaf = NULL;
999 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001000
1001 path = btrfs_alloc_path();
1002 if (!path)
1003 return -ENOMEM;
1004
1005 key.objectid = device->devid;
1006 key.offset = start;
1007 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001008again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001009 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001010 if (ret > 0) {
1011 ret = btrfs_previous_item(root, path, key.objectid,
1012 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001013 if (ret)
1014 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001015 leaf = path->nodes[0];
1016 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1017 extent = btrfs_item_ptr(leaf, path->slots[0],
1018 struct btrfs_dev_extent);
1019 BUG_ON(found_key.offset > start || found_key.offset +
1020 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001021 key = found_key;
1022 btrfs_release_path(path);
1023 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001024 } else if (ret == 0) {
1025 leaf = path->nodes[0];
1026 extent = btrfs_item_ptr(leaf, path->slots[0],
1027 struct btrfs_dev_extent);
1028 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001029 BUG_ON(ret);
1030
Josef Bacik2bf64752011-09-26 17:12:22 -04001031 if (device->bytes_used > 0) {
1032 u64 len = btrfs_dev_extent_length(leaf, extent);
1033 device->bytes_used -= len;
1034 spin_lock(&root->fs_info->free_chunk_lock);
1035 root->fs_info->free_chunk_space += len;
1036 spin_unlock(&root->fs_info->free_chunk_lock);
1037 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001038 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001039
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001040out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001041 btrfs_free_path(path);
1042 return ret;
1043}
1044
Yan Zheng2b820322008-11-17 21:11:30 -05001045int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001046 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001047 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001048 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001049{
1050 int ret;
1051 struct btrfs_path *path;
1052 struct btrfs_root *root = device->dev_root;
1053 struct btrfs_dev_extent *extent;
1054 struct extent_buffer *leaf;
1055 struct btrfs_key key;
1056
Chris Masondfe25022008-05-13 13:46:40 -04001057 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001058 path = btrfs_alloc_path();
1059 if (!path)
1060 return -ENOMEM;
1061
Chris Mason0b86a832008-03-24 15:01:56 -04001062 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001063 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001064 key.type = BTRFS_DEV_EXTENT_KEY;
1065 ret = btrfs_insert_empty_item(trans, root, path, &key,
1066 sizeof(*extent));
1067 BUG_ON(ret);
1068
1069 leaf = path->nodes[0];
1070 extent = btrfs_item_ptr(leaf, path->slots[0],
1071 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001072 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1073 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1074 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1075
1076 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1077 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1078 BTRFS_UUID_SIZE);
1079
Chris Mason0b86a832008-03-24 15:01:56 -04001080 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1081 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001082 btrfs_free_path(path);
1083 return ret;
1084}
1085
Chris Masona1b32a52008-09-05 16:09:51 -04001086static noinline int find_next_chunk(struct btrfs_root *root,
1087 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001088{
1089 struct btrfs_path *path;
1090 int ret;
1091 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001092 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001093 struct btrfs_key found_key;
1094
1095 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001096 if (!path)
1097 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001098
Chris Masone17cade2008-04-15 15:41:47 -04001099 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001100 key.offset = (u64)-1;
1101 key.type = BTRFS_CHUNK_ITEM_KEY;
1102
1103 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1104 if (ret < 0)
1105 goto error;
1106
1107 BUG_ON(ret == 0);
1108
1109 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1110 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001111 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001112 } else {
1113 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1114 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001115 if (found_key.objectid != objectid)
1116 *offset = 0;
1117 else {
1118 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1119 struct btrfs_chunk);
1120 *offset = found_key.offset +
1121 btrfs_chunk_length(path->nodes[0], chunk);
1122 }
Chris Mason0b86a832008-03-24 15:01:56 -04001123 }
1124 ret = 0;
1125error:
1126 btrfs_free_path(path);
1127 return ret;
1128}
1129
Yan Zheng2b820322008-11-17 21:11:30 -05001130static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001131{
1132 int ret;
1133 struct btrfs_key key;
1134 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001135 struct btrfs_path *path;
1136
1137 root = root->fs_info->chunk_root;
1138
1139 path = btrfs_alloc_path();
1140 if (!path)
1141 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001142
1143 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1144 key.type = BTRFS_DEV_ITEM_KEY;
1145 key.offset = (u64)-1;
1146
1147 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1148 if (ret < 0)
1149 goto error;
1150
1151 BUG_ON(ret == 0);
1152
1153 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1154 BTRFS_DEV_ITEM_KEY);
1155 if (ret) {
1156 *objectid = 1;
1157 } else {
1158 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1159 path->slots[0]);
1160 *objectid = found_key.offset + 1;
1161 }
1162 ret = 0;
1163error:
Yan Zheng2b820322008-11-17 21:11:30 -05001164 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001165 return ret;
1166}
1167
1168/*
1169 * the device information is stored in the chunk root
1170 * the btrfs_device struct should be fully filled in
1171 */
1172int btrfs_add_device(struct btrfs_trans_handle *trans,
1173 struct btrfs_root *root,
1174 struct btrfs_device *device)
1175{
1176 int ret;
1177 struct btrfs_path *path;
1178 struct btrfs_dev_item *dev_item;
1179 struct extent_buffer *leaf;
1180 struct btrfs_key key;
1181 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001182
1183 root = root->fs_info->chunk_root;
1184
1185 path = btrfs_alloc_path();
1186 if (!path)
1187 return -ENOMEM;
1188
Chris Mason0b86a832008-03-24 15:01:56 -04001189 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1190 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001191 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001192
1193 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001194 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001195 if (ret)
1196 goto out;
1197
1198 leaf = path->nodes[0];
1199 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1200
1201 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001202 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001203 btrfs_set_device_type(leaf, dev_item, device->type);
1204 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1205 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1206 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001207 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1208 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001209 btrfs_set_device_group(leaf, dev_item, 0);
1210 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1211 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001212 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001213
Chris Mason0b86a832008-03-24 15:01:56 -04001214 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001215 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001216 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1217 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001218 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001219
Yan Zheng2b820322008-11-17 21:11:30 -05001220 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001221out:
1222 btrfs_free_path(path);
1223 return ret;
1224}
Chris Mason8f18cf12008-04-25 16:53:30 -04001225
Chris Masona061fc82008-05-07 11:43:44 -04001226static int btrfs_rm_dev_item(struct btrfs_root *root,
1227 struct btrfs_device *device)
1228{
1229 int ret;
1230 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001231 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001232 struct btrfs_trans_handle *trans;
1233
1234 root = root->fs_info->chunk_root;
1235
1236 path = btrfs_alloc_path();
1237 if (!path)
1238 return -ENOMEM;
1239
Yan, Zhenga22285a2010-05-16 10:48:46 -04001240 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001241 if (IS_ERR(trans)) {
1242 btrfs_free_path(path);
1243 return PTR_ERR(trans);
1244 }
Chris Masona061fc82008-05-07 11:43:44 -04001245 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1246 key.type = BTRFS_DEV_ITEM_KEY;
1247 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001248 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001249
1250 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1251 if (ret < 0)
1252 goto out;
1253
1254 if (ret > 0) {
1255 ret = -ENOENT;
1256 goto out;
1257 }
1258
1259 ret = btrfs_del_item(trans, root, path);
1260 if (ret)
1261 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001262out:
1263 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001264 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001265 btrfs_commit_transaction(trans, root);
1266 return ret;
1267}
1268
1269int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1270{
1271 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001272 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001273 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001274 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001276 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001277 u64 all_avail;
1278 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001279 u64 num_devices;
1280 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001281 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001282 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001283
Chris Masona061fc82008-05-07 11:43:44 -04001284 mutex_lock(&uuid_mutex);
1285
1286 all_avail = root->fs_info->avail_data_alloc_bits |
1287 root->fs_info->avail_system_alloc_bits |
1288 root->fs_info->avail_metadata_alloc_bits;
1289
1290 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001291 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001292 printk(KERN_ERR "btrfs: unable to go below four devices "
1293 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001294 ret = -EINVAL;
1295 goto out;
1296 }
1297
1298 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001299 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001300 printk(KERN_ERR "btrfs: unable to go below two "
1301 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001302 ret = -EINVAL;
1303 goto out;
1304 }
1305
Chris Masondfe25022008-05-13 13:46:40 -04001306 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001307 struct list_head *devices;
1308 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001309
Chris Masondfe25022008-05-13 13:46:40 -04001310 device = NULL;
1311 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001312 /*
1313 * It is safe to read the devices since the volume_mutex
1314 * is held.
1315 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001316 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001317 if (tmp->in_fs_metadata && !tmp->bdev) {
1318 device = tmp;
1319 break;
1320 }
1321 }
1322 bdev = NULL;
1323 bh = NULL;
1324 disk_super = NULL;
1325 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001326 printk(KERN_ERR "btrfs: no missing devices found to "
1327 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001328 goto out;
1329 }
Chris Masondfe25022008-05-13 13:46:40 -04001330 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001331 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1332 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001333 if (IS_ERR(bdev)) {
1334 ret = PTR_ERR(bdev);
1335 goto out;
1336 }
1337
Yan Zheng2b820322008-11-17 21:11:30 -05001338 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001339 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001340 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001341 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001342 goto error_close;
1343 }
1344 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001345 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001346 dev_uuid = disk_super->dev_item.uuid;
1347 device = btrfs_find_device(root, devid, dev_uuid,
1348 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001349 if (!device) {
1350 ret = -ENOENT;
1351 goto error_brelse;
1352 }
Chris Masondfe25022008-05-13 13:46:40 -04001353 }
Yan Zheng2b820322008-11-17 21:11:30 -05001354
1355 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001356 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1357 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001358 ret = -EINVAL;
1359 goto error_brelse;
1360 }
1361
1362 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001363 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001364 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001365 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001366 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001367 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001368 }
Chris Masona061fc82008-05-07 11:43:44 -04001369
1370 ret = btrfs_shrink_device(device, 0);
1371 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001372 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001373
Chris Masona061fc82008-05-07 11:43:44 -04001374 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1375 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001376 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001377
Josef Bacik2bf64752011-09-26 17:12:22 -04001378 spin_lock(&root->fs_info->free_chunk_lock);
1379 root->fs_info->free_chunk_space = device->total_bytes -
1380 device->bytes_used;
1381 spin_unlock(&root->fs_info->free_chunk_lock);
1382
Yan Zheng2b820322008-11-17 21:11:30 -05001383 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001384 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001385
1386 /*
1387 * the device list mutex makes sure that we don't change
1388 * the device list while someone else is writing out all
1389 * the device supers.
1390 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001391
1392 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001393 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001394 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001395
Yan Zhenge4404d62008-12-12 10:03:26 -05001396 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001397
Chris Masoncd02dca2010-12-13 14:56:23 -05001398 if (device->missing)
1399 root->fs_info->fs_devices->missing_devices--;
1400
Yan Zheng2b820322008-11-17 21:11:30 -05001401 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1402 struct btrfs_device, dev_list);
1403 if (device->bdev == root->fs_info->sb->s_bdev)
1404 root->fs_info->sb->s_bdev = next_device->bdev;
1405 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1406 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1407
Xiao Guangrong1f781602011-04-20 10:09:16 +00001408 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001409 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001410
1411 call_rcu(&device->rcu, free_device);
1412 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001413
David Sterba6c417612011-04-13 15:41:04 +02001414 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1415 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001416
Xiao Guangrong1f781602011-04-20 10:09:16 +00001417 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001418 struct btrfs_fs_devices *fs_devices;
1419 fs_devices = root->fs_info->fs_devices;
1420 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001421 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001422 break;
1423 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001424 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001425 fs_devices->seed = cur_devices->seed;
1426 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001427 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001428 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001429 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001430 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001431 }
1432
1433 /*
1434 * at this point, the device is zero sized. We want to
1435 * remove it from the devices list and zero out the old super
1436 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001437 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001438 /* make sure this device isn't detected as part of
1439 * the FS anymore
1440 */
1441 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1442 set_buffer_dirty(bh);
1443 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001444 }
Chris Masona061fc82008-05-07 11:43:44 -04001445
Chris Masona061fc82008-05-07 11:43:44 -04001446 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001447
1448error_brelse:
1449 brelse(bh);
1450error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001451 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001452 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001453out:
1454 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001455 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001456error_undo:
1457 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001458 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001459 list_add(&device->dev_alloc_list,
1460 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001461 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001462 root->fs_info->fs_devices->rw_devices++;
1463 }
1464 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001465}
1466
Yan Zheng2b820322008-11-17 21:11:30 -05001467/*
1468 * does all the dirty work required for changing file system's UUID.
1469 */
1470static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1471 struct btrfs_root *root)
1472{
1473 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1474 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001475 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001476 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001477 struct btrfs_device *device;
1478 u64 super_flags;
1479
1480 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001481 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001482 return -EINVAL;
1483
Yan Zhenge4404d62008-12-12 10:03:26 -05001484 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1485 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001486 return -ENOMEM;
1487
Yan Zhenge4404d62008-12-12 10:03:26 -05001488 old_devices = clone_fs_devices(fs_devices);
1489 if (IS_ERR(old_devices)) {
1490 kfree(seed_devices);
1491 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001492 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001493
Yan Zheng2b820322008-11-17 21:11:30 -05001494 list_add(&old_devices->list, &fs_uuids);
1495
Yan Zhenge4404d62008-12-12 10:03:26 -05001496 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1497 seed_devices->opened = 1;
1498 INIT_LIST_HEAD(&seed_devices->devices);
1499 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001500 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001501
1502 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001503 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1504 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001505 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1506
Yan Zhenge4404d62008-12-12 10:03:26 -05001507 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1508 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1509 device->fs_devices = seed_devices;
1510 }
1511
Yan Zheng2b820322008-11-17 21:11:30 -05001512 fs_devices->seeding = 0;
1513 fs_devices->num_devices = 0;
1514 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001515 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001516
1517 generate_random_uuid(fs_devices->fsid);
1518 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1519 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1520 super_flags = btrfs_super_flags(disk_super) &
1521 ~BTRFS_SUPER_FLAG_SEEDING;
1522 btrfs_set_super_flags(disk_super, super_flags);
1523
1524 return 0;
1525}
1526
1527/*
1528 * strore the expected generation for seed devices in device items.
1529 */
1530static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1531 struct btrfs_root *root)
1532{
1533 struct btrfs_path *path;
1534 struct extent_buffer *leaf;
1535 struct btrfs_dev_item *dev_item;
1536 struct btrfs_device *device;
1537 struct btrfs_key key;
1538 u8 fs_uuid[BTRFS_UUID_SIZE];
1539 u8 dev_uuid[BTRFS_UUID_SIZE];
1540 u64 devid;
1541 int ret;
1542
1543 path = btrfs_alloc_path();
1544 if (!path)
1545 return -ENOMEM;
1546
1547 root = root->fs_info->chunk_root;
1548 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1549 key.offset = 0;
1550 key.type = BTRFS_DEV_ITEM_KEY;
1551
1552 while (1) {
1553 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1554 if (ret < 0)
1555 goto error;
1556
1557 leaf = path->nodes[0];
1558next_slot:
1559 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1560 ret = btrfs_next_leaf(root, path);
1561 if (ret > 0)
1562 break;
1563 if (ret < 0)
1564 goto error;
1565 leaf = path->nodes[0];
1566 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001567 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001568 continue;
1569 }
1570
1571 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1572 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1573 key.type != BTRFS_DEV_ITEM_KEY)
1574 break;
1575
1576 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1577 struct btrfs_dev_item);
1578 devid = btrfs_device_id(leaf, dev_item);
1579 read_extent_buffer(leaf, dev_uuid,
1580 (unsigned long)btrfs_device_uuid(dev_item),
1581 BTRFS_UUID_SIZE);
1582 read_extent_buffer(leaf, fs_uuid,
1583 (unsigned long)btrfs_device_fsid(dev_item),
1584 BTRFS_UUID_SIZE);
1585 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1586 BUG_ON(!device);
1587
1588 if (device->fs_devices->seeding) {
1589 btrfs_set_device_generation(leaf, dev_item,
1590 device->generation);
1591 btrfs_mark_buffer_dirty(leaf);
1592 }
1593
1594 path->slots[0]++;
1595 goto next_slot;
1596 }
1597 ret = 0;
1598error:
1599 btrfs_free_path(path);
1600 return ret;
1601}
1602
Chris Mason788f20e2008-04-28 15:29:42 -04001603int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1604{
Josef Bacikd5e20032011-08-04 14:52:27 +00001605 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001606 struct btrfs_trans_handle *trans;
1607 struct btrfs_device *device;
1608 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001609 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001610 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001611 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001612 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001613 int ret = 0;
1614
Yan Zheng2b820322008-11-17 21:11:30 -05001615 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1616 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001617
Li Zefana5d16332011-12-07 20:08:40 -05001618 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001619 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001620 if (IS_ERR(bdev))
1621 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001622
Yan Zheng2b820322008-11-17 21:11:30 -05001623 if (root->fs_info->fs_devices->seeding) {
1624 seeding_dev = 1;
1625 down_write(&sb->s_umount);
1626 mutex_lock(&uuid_mutex);
1627 }
1628
Chris Mason8c8bee12008-09-29 11:19:10 -04001629 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001630
Chris Mason788f20e2008-04-28 15:29:42 -04001631 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001632 /*
1633 * we have the volume lock, so we don't need the extra
1634 * device list mutex while reading the list here.
1635 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001636 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001637 if (device->bdev == bdev) {
1638 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001639 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001640 }
1641 }
1642
1643 device = kzalloc(sizeof(*device), GFP_NOFS);
1644 if (!device) {
1645 /* we can safely leave the fs_devices entry around */
1646 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001647 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001648 }
1649
Chris Mason788f20e2008-04-28 15:29:42 -04001650 device->name = kstrdup(device_path, GFP_NOFS);
1651 if (!device->name) {
1652 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001653 ret = -ENOMEM;
1654 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001655 }
Yan Zheng2b820322008-11-17 21:11:30 -05001656
1657 ret = find_next_devid(root, &device->devid);
1658 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001659 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001660 kfree(device);
1661 goto error;
1662 }
1663
Yan, Zhenga22285a2010-05-16 10:48:46 -04001664 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001665 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001666 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001667 kfree(device);
1668 ret = PTR_ERR(trans);
1669 goto error;
1670 }
1671
Yan Zheng2b820322008-11-17 21:11:30 -05001672 lock_chunks(root);
1673
Josef Bacikd5e20032011-08-04 14:52:27 +00001674 q = bdev_get_queue(bdev);
1675 if (blk_queue_discard(q))
1676 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001677 device->writeable = 1;
1678 device->work.func = pending_bios_fn;
1679 generate_random_uuid(device->uuid);
1680 spin_lock_init(&device->io_lock);
1681 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001682 device->io_width = root->sectorsize;
1683 device->io_align = root->sectorsize;
1684 device->sector_size = root->sectorsize;
1685 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001686 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001687 device->dev_root = root->fs_info->dev_root;
1688 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001689 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001690 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001691 set_blocksize(device->bdev, 4096);
1692
Yan Zheng2b820322008-11-17 21:11:30 -05001693 if (seeding_dev) {
1694 sb->s_flags &= ~MS_RDONLY;
1695 ret = btrfs_prepare_sprout(trans, root);
1696 BUG_ON(ret);
1697 }
1698
1699 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001700
1701 /*
1702 * we don't want write_supers to jump in here with our device
1703 * half setup
1704 */
1705 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001706 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001707 list_add(&device->dev_alloc_list,
1708 &root->fs_info->fs_devices->alloc_list);
1709 root->fs_info->fs_devices->num_devices++;
1710 root->fs_info->fs_devices->open_devices++;
1711 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001712 if (device->can_discard)
1713 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001714 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1715
Josef Bacik2bf64752011-09-26 17:12:22 -04001716 spin_lock(&root->fs_info->free_chunk_lock);
1717 root->fs_info->free_chunk_space += device->total_bytes;
1718 spin_unlock(&root->fs_info->free_chunk_lock);
1719
Chris Masonc2898112009-06-10 09:51:32 -04001720 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1721 root->fs_info->fs_devices->rotating = 1;
1722
David Sterba6c417612011-04-13 15:41:04 +02001723 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1724 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001725 total_bytes + device->total_bytes);
1726
David Sterba6c417612011-04-13 15:41:04 +02001727 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1728 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001729 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001730 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001731
Yan Zheng2b820322008-11-17 21:11:30 -05001732 if (seeding_dev) {
1733 ret = init_first_rw_device(trans, root, device);
1734 BUG_ON(ret);
1735 ret = btrfs_finish_sprout(trans, root);
1736 BUG_ON(ret);
1737 } else {
1738 ret = btrfs_add_device(trans, root, device);
1739 }
1740
Chris Mason913d9522009-03-10 13:17:18 -04001741 /*
1742 * we've got more storage, clear any full flags on the space
1743 * infos
1744 */
1745 btrfs_clear_space_info_full(root->fs_info);
1746
Chris Mason7d9eb122008-07-08 14:19:17 -04001747 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001748 btrfs_commit_transaction(trans, root);
1749
1750 if (seeding_dev) {
1751 mutex_unlock(&uuid_mutex);
1752 up_write(&sb->s_umount);
1753
1754 ret = btrfs_relocate_sys_chunks(root);
1755 BUG_ON(ret);
1756 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001757
Chris Mason788f20e2008-04-28 15:29:42 -04001758 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001759error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001760 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001761 if (seeding_dev) {
1762 mutex_unlock(&uuid_mutex);
1763 up_write(&sb->s_umount);
1764 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001765 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001766}
1767
Chris Masond3977122009-01-05 21:25:51 -05001768static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1769 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001770{
1771 int ret;
1772 struct btrfs_path *path;
1773 struct btrfs_root *root;
1774 struct btrfs_dev_item *dev_item;
1775 struct extent_buffer *leaf;
1776 struct btrfs_key key;
1777
1778 root = device->dev_root->fs_info->chunk_root;
1779
1780 path = btrfs_alloc_path();
1781 if (!path)
1782 return -ENOMEM;
1783
1784 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1785 key.type = BTRFS_DEV_ITEM_KEY;
1786 key.offset = device->devid;
1787
1788 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1789 if (ret < 0)
1790 goto out;
1791
1792 if (ret > 0) {
1793 ret = -ENOENT;
1794 goto out;
1795 }
1796
1797 leaf = path->nodes[0];
1798 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1799
1800 btrfs_set_device_id(leaf, dev_item, device->devid);
1801 btrfs_set_device_type(leaf, dev_item, device->type);
1802 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1803 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1804 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001805 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001806 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1807 btrfs_mark_buffer_dirty(leaf);
1808
1809out:
1810 btrfs_free_path(path);
1811 return ret;
1812}
1813
Chris Mason7d9eb122008-07-08 14:19:17 -04001814static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001815 struct btrfs_device *device, u64 new_size)
1816{
1817 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001818 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001819 u64 old_total = btrfs_super_total_bytes(super_copy);
1820 u64 diff = new_size - device->total_bytes;
1821
Yan Zheng2b820322008-11-17 21:11:30 -05001822 if (!device->writeable)
1823 return -EACCES;
1824 if (new_size <= device->total_bytes)
1825 return -EINVAL;
1826
Chris Mason8f18cf12008-04-25 16:53:30 -04001827 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001828 device->fs_devices->total_rw_bytes += diff;
1829
1830 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001831 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001832 btrfs_clear_space_info_full(device->dev_root->fs_info);
1833
Chris Mason8f18cf12008-04-25 16:53:30 -04001834 return btrfs_update_device(trans, device);
1835}
1836
Chris Mason7d9eb122008-07-08 14:19:17 -04001837int btrfs_grow_device(struct btrfs_trans_handle *trans,
1838 struct btrfs_device *device, u64 new_size)
1839{
1840 int ret;
1841 lock_chunks(device->dev_root);
1842 ret = __btrfs_grow_device(trans, device, new_size);
1843 unlock_chunks(device->dev_root);
1844 return ret;
1845}
1846
Chris Mason8f18cf12008-04-25 16:53:30 -04001847static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1848 struct btrfs_root *root,
1849 u64 chunk_tree, u64 chunk_objectid,
1850 u64 chunk_offset)
1851{
1852 int ret;
1853 struct btrfs_path *path;
1854 struct btrfs_key key;
1855
1856 root = root->fs_info->chunk_root;
1857 path = btrfs_alloc_path();
1858 if (!path)
1859 return -ENOMEM;
1860
1861 key.objectid = chunk_objectid;
1862 key.offset = chunk_offset;
1863 key.type = BTRFS_CHUNK_ITEM_KEY;
1864
1865 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1866 BUG_ON(ret);
1867
1868 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001869
1870 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001871 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001872}
1873
Christoph Hellwigb2950862008-12-02 09:54:17 -05001874static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001875 chunk_offset)
1876{
David Sterba6c417612011-04-13 15:41:04 +02001877 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001878 struct btrfs_disk_key *disk_key;
1879 struct btrfs_chunk *chunk;
1880 u8 *ptr;
1881 int ret = 0;
1882 u32 num_stripes;
1883 u32 array_size;
1884 u32 len = 0;
1885 u32 cur;
1886 struct btrfs_key key;
1887
1888 array_size = btrfs_super_sys_array_size(super_copy);
1889
1890 ptr = super_copy->sys_chunk_array;
1891 cur = 0;
1892
1893 while (cur < array_size) {
1894 disk_key = (struct btrfs_disk_key *)ptr;
1895 btrfs_disk_key_to_cpu(&key, disk_key);
1896
1897 len = sizeof(*disk_key);
1898
1899 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1900 chunk = (struct btrfs_chunk *)(ptr + len);
1901 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1902 len += btrfs_chunk_item_size(num_stripes);
1903 } else {
1904 ret = -EIO;
1905 break;
1906 }
1907 if (key.objectid == chunk_objectid &&
1908 key.offset == chunk_offset) {
1909 memmove(ptr, ptr + len, array_size - (cur + len));
1910 array_size -= len;
1911 btrfs_set_super_sys_array_size(super_copy, array_size);
1912 } else {
1913 ptr += len;
1914 cur += len;
1915 }
1916 }
1917 return ret;
1918}
1919
Christoph Hellwigb2950862008-12-02 09:54:17 -05001920static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001921 u64 chunk_tree, u64 chunk_objectid,
1922 u64 chunk_offset)
1923{
1924 struct extent_map_tree *em_tree;
1925 struct btrfs_root *extent_root;
1926 struct btrfs_trans_handle *trans;
1927 struct extent_map *em;
1928 struct map_lookup *map;
1929 int ret;
1930 int i;
1931
1932 root = root->fs_info->chunk_root;
1933 extent_root = root->fs_info->extent_root;
1934 em_tree = &root->fs_info->mapping_tree.map_tree;
1935
Josef Bacikba1bf482009-09-11 16:11:19 -04001936 ret = btrfs_can_relocate(extent_root, chunk_offset);
1937 if (ret)
1938 return -ENOSPC;
1939
Chris Mason8f18cf12008-04-25 16:53:30 -04001940 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001941 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001942 if (ret)
1943 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001944
Yan, Zhenga22285a2010-05-16 10:48:46 -04001945 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001946 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001947
Chris Mason7d9eb122008-07-08 14:19:17 -04001948 lock_chunks(root);
1949
Chris Mason8f18cf12008-04-25 16:53:30 -04001950 /*
1951 * step two, delete the device extents and the
1952 * chunk tree entries
1953 */
Chris Mason890871b2009-09-02 16:24:52 -04001954 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001955 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001956 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001957
Chris Masona061fc82008-05-07 11:43:44 -04001958 BUG_ON(em->start > chunk_offset ||
1959 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001960 map = (struct map_lookup *)em->bdev;
1961
1962 for (i = 0; i < map->num_stripes; i++) {
1963 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1964 map->stripes[i].physical);
1965 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001966
Chris Masondfe25022008-05-13 13:46:40 -04001967 if (map->stripes[i].dev) {
1968 ret = btrfs_update_device(trans, map->stripes[i].dev);
1969 BUG_ON(ret);
1970 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001971 }
1972 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1973 chunk_offset);
1974
1975 BUG_ON(ret);
1976
liubo1abe9b82011-03-24 11:18:59 +00001977 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1978
Chris Mason8f18cf12008-04-25 16:53:30 -04001979 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1980 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1981 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001982 }
1983
Zheng Yan1a40e232008-09-26 10:09:34 -04001984 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1985 BUG_ON(ret);
1986
Chris Mason890871b2009-09-02 16:24:52 -04001987 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001988 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001989 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001990
Chris Mason8f18cf12008-04-25 16:53:30 -04001991 kfree(map);
1992 em->bdev = NULL;
1993
1994 /* once for the tree */
1995 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001996 /* once for us */
1997 free_extent_map(em);
1998
Chris Mason7d9eb122008-07-08 14:19:17 -04001999 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002000 btrfs_end_transaction(trans, root);
2001 return 0;
2002}
2003
Yan Zheng2b820322008-11-17 21:11:30 -05002004static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2005{
2006 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2007 struct btrfs_path *path;
2008 struct extent_buffer *leaf;
2009 struct btrfs_chunk *chunk;
2010 struct btrfs_key key;
2011 struct btrfs_key found_key;
2012 u64 chunk_tree = chunk_root->root_key.objectid;
2013 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002014 bool retried = false;
2015 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002016 int ret;
2017
2018 path = btrfs_alloc_path();
2019 if (!path)
2020 return -ENOMEM;
2021
Josef Bacikba1bf482009-09-11 16:11:19 -04002022again:
Yan Zheng2b820322008-11-17 21:11:30 -05002023 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2024 key.offset = (u64)-1;
2025 key.type = BTRFS_CHUNK_ITEM_KEY;
2026
2027 while (1) {
2028 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2029 if (ret < 0)
2030 goto error;
2031 BUG_ON(ret == 0);
2032
2033 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2034 key.type);
2035 if (ret < 0)
2036 goto error;
2037 if (ret > 0)
2038 break;
2039
2040 leaf = path->nodes[0];
2041 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2042
2043 chunk = btrfs_item_ptr(leaf, path->slots[0],
2044 struct btrfs_chunk);
2045 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002046 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002047
2048 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2049 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2050 found_key.objectid,
2051 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002052 if (ret == -ENOSPC)
2053 failed++;
2054 else if (ret)
2055 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002056 }
2057
2058 if (found_key.offset == 0)
2059 break;
2060 key.offset = found_key.offset - 1;
2061 }
2062 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002063 if (failed && !retried) {
2064 failed = 0;
2065 retried = true;
2066 goto again;
2067 } else if (failed && retried) {
2068 WARN_ON(1);
2069 ret = -ENOSPC;
2070 }
Yan Zheng2b820322008-11-17 21:11:30 -05002071error:
2072 btrfs_free_path(path);
2073 return ret;
2074}
2075
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002076/*
2077 * Should be called with both balance and volume mutexes held to
2078 * serialize other volume operations (add_dev/rm_dev/resize) with
2079 * restriper. Same goes for unset_balance_control.
2080 */
2081static void set_balance_control(struct btrfs_balance_control *bctl)
2082{
2083 struct btrfs_fs_info *fs_info = bctl->fs_info;
2084
2085 BUG_ON(fs_info->balance_ctl);
2086
2087 spin_lock(&fs_info->balance_lock);
2088 fs_info->balance_ctl = bctl;
2089 spin_unlock(&fs_info->balance_lock);
2090}
2091
2092static void unset_balance_control(struct btrfs_fs_info *fs_info)
2093{
2094 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2095
2096 BUG_ON(!fs_info->balance_ctl);
2097
2098 spin_lock(&fs_info->balance_lock);
2099 fs_info->balance_ctl = NULL;
2100 spin_unlock(&fs_info->balance_lock);
2101
2102 kfree(bctl);
2103}
2104
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002105/*
2106 * Balance filters. Return 1 if chunk should be filtered out
2107 * (should not be balanced).
2108 */
2109static int chunk_profiles_filter(u64 chunk_profile,
2110 struct btrfs_balance_args *bargs)
2111{
2112 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2113
2114 if (chunk_profile == 0)
2115 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2116
2117 if (bargs->profiles & chunk_profile)
2118 return 0;
2119
2120 return 1;
2121}
2122
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002123static u64 div_factor_fine(u64 num, int factor)
2124{
2125 if (factor <= 0)
2126 return 0;
2127 if (factor >= 100)
2128 return num;
2129
2130 num *= factor;
2131 do_div(num, 100);
2132 return num;
2133}
2134
2135static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2136 struct btrfs_balance_args *bargs)
2137{
2138 struct btrfs_block_group_cache *cache;
2139 u64 chunk_used, user_thresh;
2140 int ret = 1;
2141
2142 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2143 chunk_used = btrfs_block_group_used(&cache->item);
2144
2145 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2146 if (chunk_used < user_thresh)
2147 ret = 0;
2148
2149 btrfs_put_block_group(cache);
2150 return ret;
2151}
2152
Ilya Dryomov409d4042012-01-16 22:04:47 +02002153static int chunk_devid_filter(struct extent_buffer *leaf,
2154 struct btrfs_chunk *chunk,
2155 struct btrfs_balance_args *bargs)
2156{
2157 struct btrfs_stripe *stripe;
2158 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2159 int i;
2160
2161 for (i = 0; i < num_stripes; i++) {
2162 stripe = btrfs_stripe_nr(chunk, i);
2163 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2164 return 0;
2165 }
2166
2167 return 1;
2168}
2169
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002170/* [pstart, pend) */
2171static int chunk_drange_filter(struct extent_buffer *leaf,
2172 struct btrfs_chunk *chunk,
2173 u64 chunk_offset,
2174 struct btrfs_balance_args *bargs)
2175{
2176 struct btrfs_stripe *stripe;
2177 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2178 u64 stripe_offset;
2179 u64 stripe_length;
2180 int factor;
2181 int i;
2182
2183 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2184 return 0;
2185
2186 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2187 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2188 factor = 2;
2189 else
2190 factor = 1;
2191 factor = num_stripes / factor;
2192
2193 for (i = 0; i < num_stripes; i++) {
2194 stripe = btrfs_stripe_nr(chunk, i);
2195 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2196 continue;
2197
2198 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2199 stripe_length = btrfs_chunk_length(leaf, chunk);
2200 do_div(stripe_length, factor);
2201
2202 if (stripe_offset < bargs->pend &&
2203 stripe_offset + stripe_length > bargs->pstart)
2204 return 0;
2205 }
2206
2207 return 1;
2208}
2209
Ilya Dryomovea671762012-01-16 22:04:48 +02002210/* [vstart, vend) */
2211static int chunk_vrange_filter(struct extent_buffer *leaf,
2212 struct btrfs_chunk *chunk,
2213 u64 chunk_offset,
2214 struct btrfs_balance_args *bargs)
2215{
2216 if (chunk_offset < bargs->vend &&
2217 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2218 /* at least part of the chunk is inside this vrange */
2219 return 0;
2220
2221 return 1;
2222}
2223
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002224static int should_balance_chunk(struct btrfs_root *root,
2225 struct extent_buffer *leaf,
2226 struct btrfs_chunk *chunk, u64 chunk_offset)
2227{
2228 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2229 struct btrfs_balance_args *bargs = NULL;
2230 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2231
2232 /* type filter */
2233 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2234 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2235 return 0;
2236 }
2237
2238 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2239 bargs = &bctl->data;
2240 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2241 bargs = &bctl->sys;
2242 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2243 bargs = &bctl->meta;
2244
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002245 /* profiles filter */
2246 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2247 chunk_profiles_filter(chunk_type, bargs)) {
2248 return 0;
2249 }
2250
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002251 /* usage filter */
2252 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2253 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2254 return 0;
2255 }
2256
Ilya Dryomov409d4042012-01-16 22:04:47 +02002257 /* devid filter */
2258 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2259 chunk_devid_filter(leaf, chunk, bargs)) {
2260 return 0;
2261 }
2262
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002263 /* drange filter, makes sense only with devid filter */
2264 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2265 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2266 return 0;
2267 }
2268
Ilya Dryomovea671762012-01-16 22:04:48 +02002269 /* vrange filter */
2270 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2271 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2272 return 0;
2273 }
2274
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002275 return 1;
2276}
2277
Chris Masonec44a352008-04-28 15:29:52 -04002278static u64 div_factor(u64 num, int factor)
2279{
2280 if (factor == 10)
2281 return num;
2282 num *= factor;
2283 do_div(num, 10);
2284 return num;
2285}
2286
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002287static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002288{
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002289 struct btrfs_root *chunk_root = fs_info->chunk_root;
2290 struct btrfs_root *dev_root = fs_info->dev_root;
2291 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002292 struct btrfs_device *device;
2293 u64 old_size;
2294 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002295 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002296 struct btrfs_path *path;
2297 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002298 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002299 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002300 struct extent_buffer *leaf;
2301 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002302 int ret;
2303 int enospc_errors = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002304
Chris Masonec44a352008-04-28 15:29:52 -04002305 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002306 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002307 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002308 old_size = device->total_bytes;
2309 size_to_free = div_factor(old_size, 1);
2310 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002311 if (!device->writeable ||
2312 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002313 continue;
2314
2315 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002316 if (ret == -ENOSPC)
2317 break;
Chris Masonec44a352008-04-28 15:29:52 -04002318 BUG_ON(ret);
2319
Yan, Zhenga22285a2010-05-16 10:48:46 -04002320 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002321 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002322
2323 ret = btrfs_grow_device(trans, device, old_size);
2324 BUG_ON(ret);
2325
2326 btrfs_end_transaction(trans, dev_root);
2327 }
2328
2329 /* step two, relocate all the chunks */
2330 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002331 if (!path) {
2332 ret = -ENOMEM;
2333 goto error;
2334 }
Chris Masonec44a352008-04-28 15:29:52 -04002335 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2336 key.offset = (u64)-1;
2337 key.type = BTRFS_CHUNK_ITEM_KEY;
2338
Chris Masond3977122009-01-05 21:25:51 -05002339 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002340 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2341 if (ret < 0)
2342 goto error;
2343
2344 /*
2345 * this shouldn't happen, it means the last relocate
2346 * failed
2347 */
2348 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002349 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002350
2351 ret = btrfs_previous_item(chunk_root, path, 0,
2352 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002353 if (ret) {
2354 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002355 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002356 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002357
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002358 leaf = path->nodes[0];
2359 slot = path->slots[0];
2360 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2361
Chris Masonec44a352008-04-28 15:29:52 -04002362 if (found_key.objectid != key.objectid)
2363 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002364
Chris Masonec44a352008-04-28 15:29:52 -04002365 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002366 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002367 break;
2368
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002369 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2370
2371 ret = should_balance_chunk(chunk_root, leaf, chunk,
2372 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002373 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002374 if (!ret)
2375 goto loop;
2376
Chris Masonec44a352008-04-28 15:29:52 -04002377 ret = btrfs_relocate_chunk(chunk_root,
2378 chunk_root->root_key.objectid,
2379 found_key.objectid,
2380 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002381 if (ret && ret != -ENOSPC)
2382 goto error;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002383 if (ret == -ENOSPC)
2384 enospc_errors++;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002385loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002386 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002387 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002388
Chris Masonec44a352008-04-28 15:29:52 -04002389error:
2390 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002391 if (enospc_errors) {
2392 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2393 enospc_errors);
2394 if (!ret)
2395 ret = -ENOSPC;
2396 }
2397
2398 return ret;
2399}
2400
2401static void __cancel_balance(struct btrfs_fs_info *fs_info)
2402{
2403 unset_balance_control(fs_info);
2404}
2405
2406void update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
2407 struct btrfs_ioctl_balance_args *bargs);
2408
2409/*
2410 * Should be called with both balance and volume mutexes held
2411 */
2412int btrfs_balance(struct btrfs_balance_control *bctl,
2413 struct btrfs_ioctl_balance_args *bargs)
2414{
2415 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002416 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002417 int ret;
2418
2419 if (btrfs_fs_closing(fs_info)) {
2420 ret = -EINVAL;
2421 goto out;
2422 }
2423
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002424 /*
2425 * In case of mixed groups both data and meta should be picked,
2426 * and identical options should be given for both of them.
2427 */
2428 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2429 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2430 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2431 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2432 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2433 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2434 printk(KERN_ERR "btrfs: with mixed groups data and "
2435 "metadata balance options must be the same\n");
2436 ret = -EINVAL;
2437 goto out;
2438 }
2439 }
2440
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002441 /*
2442 * Profile changing sanity checks. Skip them if a simple
2443 * balance is requested.
2444 */
2445 if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
2446 BTRFS_BALANCE_ARGS_CONVERT))
2447 goto do_balance;
2448
2449 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2450 if (fs_info->fs_devices->num_devices == 1)
2451 allowed |= BTRFS_BLOCK_GROUP_DUP;
2452 else if (fs_info->fs_devices->num_devices < 4)
2453 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2454 else
2455 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2456 BTRFS_BLOCK_GROUP_RAID10);
2457
2458 if (!profile_is_valid(bctl->data.target, 1) ||
2459 bctl->data.target & ~allowed) {
2460 printk(KERN_ERR "btrfs: unable to start balance with target "
2461 "data profile %llu\n",
2462 (unsigned long long)bctl->data.target);
2463 ret = -EINVAL;
2464 goto out;
2465 }
2466 if (!profile_is_valid(bctl->meta.target, 1) ||
2467 bctl->meta.target & ~allowed) {
2468 printk(KERN_ERR "btrfs: unable to start balance with target "
2469 "metadata profile %llu\n",
2470 (unsigned long long)bctl->meta.target);
2471 ret = -EINVAL;
2472 goto out;
2473 }
2474 if (!profile_is_valid(bctl->sys.target, 1) ||
2475 bctl->sys.target & ~allowed) {
2476 printk(KERN_ERR "btrfs: unable to start balance with target "
2477 "system profile %llu\n",
2478 (unsigned long long)bctl->sys.target);
2479 ret = -EINVAL;
2480 goto out;
2481 }
2482
2483 if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
2484 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2485 ret = -EINVAL;
2486 goto out;
2487 }
2488
2489 /* allow to reduce meta or sys integrity only if force set */
2490 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2491 BTRFS_BLOCK_GROUP_RAID10;
2492 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2493 (fs_info->avail_system_alloc_bits & allowed) &&
2494 !(bctl->sys.target & allowed)) ||
2495 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2496 (fs_info->avail_metadata_alloc_bits & allowed) &&
2497 !(bctl->meta.target & allowed))) {
2498 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2499 printk(KERN_INFO "btrfs: force reducing metadata "
2500 "integrity\n");
2501 } else {
2502 printk(KERN_ERR "btrfs: balance will reduce metadata "
2503 "integrity, use force if you want this\n");
2504 ret = -EINVAL;
2505 goto out;
2506 }
2507 }
2508
2509do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002510 set_balance_control(bctl);
2511
2512 mutex_unlock(&fs_info->balance_mutex);
2513
2514 ret = __btrfs_balance(fs_info);
2515
2516 mutex_lock(&fs_info->balance_mutex);
2517
2518 if (bargs) {
2519 memset(bargs, 0, sizeof(*bargs));
2520 update_ioctl_balance_args(fs_info, bargs);
2521 }
2522
2523 __cancel_balance(fs_info);
2524
2525 return ret;
2526out:
2527 kfree(bctl);
Chris Masonec44a352008-04-28 15:29:52 -04002528 return ret;
2529}
2530
Chris Mason8f18cf12008-04-25 16:53:30 -04002531/*
2532 * shrinking a device means finding all of the device extents past
2533 * the new size, and then following the back refs to the chunks.
2534 * The chunk relocation code actually frees the device extent
2535 */
2536int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2537{
2538 struct btrfs_trans_handle *trans;
2539 struct btrfs_root *root = device->dev_root;
2540 struct btrfs_dev_extent *dev_extent = NULL;
2541 struct btrfs_path *path;
2542 u64 length;
2543 u64 chunk_tree;
2544 u64 chunk_objectid;
2545 u64 chunk_offset;
2546 int ret;
2547 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002548 int failed = 0;
2549 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002550 struct extent_buffer *l;
2551 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002552 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002553 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002554 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002555 u64 diff = device->total_bytes - new_size;
2556
Yan Zheng2b820322008-11-17 21:11:30 -05002557 if (new_size >= device->total_bytes)
2558 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002559
2560 path = btrfs_alloc_path();
2561 if (!path)
2562 return -ENOMEM;
2563
Chris Mason8f18cf12008-04-25 16:53:30 -04002564 path->reada = 2;
2565
Chris Mason7d9eb122008-07-08 14:19:17 -04002566 lock_chunks(root);
2567
Chris Mason8f18cf12008-04-25 16:53:30 -04002568 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002569 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002570 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002571 spin_lock(&root->fs_info->free_chunk_lock);
2572 root->fs_info->free_chunk_space -= diff;
2573 spin_unlock(&root->fs_info->free_chunk_lock);
2574 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002575 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002576
Josef Bacikba1bf482009-09-11 16:11:19 -04002577again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002578 key.objectid = device->devid;
2579 key.offset = (u64)-1;
2580 key.type = BTRFS_DEV_EXTENT_KEY;
2581
2582 while (1) {
2583 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2584 if (ret < 0)
2585 goto done;
2586
2587 ret = btrfs_previous_item(root, path, 0, key.type);
2588 if (ret < 0)
2589 goto done;
2590 if (ret) {
2591 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002592 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002593 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002594 }
2595
2596 l = path->nodes[0];
2597 slot = path->slots[0];
2598 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2599
Josef Bacikba1bf482009-09-11 16:11:19 -04002600 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002601 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002602 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002603 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002604
2605 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2606 length = btrfs_dev_extent_length(l, dev_extent);
2607
Josef Bacikba1bf482009-09-11 16:11:19 -04002608 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002609 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002610 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002611 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002612
2613 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2614 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2615 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002616 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002617
2618 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2619 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002620 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002621 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002622 if (ret == -ENOSPC)
2623 failed++;
2624 key.offset -= 1;
2625 }
2626
2627 if (failed && !retried) {
2628 failed = 0;
2629 retried = true;
2630 goto again;
2631 } else if (failed && retried) {
2632 ret = -ENOSPC;
2633 lock_chunks(root);
2634
2635 device->total_bytes = old_size;
2636 if (device->writeable)
2637 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002638 spin_lock(&root->fs_info->free_chunk_lock);
2639 root->fs_info->free_chunk_space += diff;
2640 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04002641 unlock_chunks(root);
2642 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002643 }
2644
Chris Balld6397ba2009-04-27 07:29:03 -04002645 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002646 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002647 if (IS_ERR(trans)) {
2648 ret = PTR_ERR(trans);
2649 goto done;
2650 }
2651
Chris Balld6397ba2009-04-27 07:29:03 -04002652 lock_chunks(root);
2653
2654 device->disk_total_bytes = new_size;
2655 /* Now btrfs_update_device() will change the on-disk size. */
2656 ret = btrfs_update_device(trans, device);
2657 if (ret) {
2658 unlock_chunks(root);
2659 btrfs_end_transaction(trans, root);
2660 goto done;
2661 }
2662 WARN_ON(diff > old_total);
2663 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2664 unlock_chunks(root);
2665 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002666done:
2667 btrfs_free_path(path);
2668 return ret;
2669}
2670
Christoph Hellwigb2950862008-12-02 09:54:17 -05002671static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002672 struct btrfs_root *root,
2673 struct btrfs_key *key,
2674 struct btrfs_chunk *chunk, int item_size)
2675{
David Sterba6c417612011-04-13 15:41:04 +02002676 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04002677 struct btrfs_disk_key disk_key;
2678 u32 array_size;
2679 u8 *ptr;
2680
2681 array_size = btrfs_super_sys_array_size(super_copy);
2682 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2683 return -EFBIG;
2684
2685 ptr = super_copy->sys_chunk_array + array_size;
2686 btrfs_cpu_key_to_disk(&disk_key, key);
2687 memcpy(ptr, &disk_key, sizeof(disk_key));
2688 ptr += sizeof(disk_key);
2689 memcpy(ptr, chunk, item_size);
2690 item_size += sizeof(disk_key);
2691 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2692 return 0;
2693}
2694
Miao Xieb2117a32011-01-05 10:07:28 +00002695/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002696 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002697 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002698static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002699{
Arne Jansen73c5de02011-04-12 12:07:57 +02002700 const struct btrfs_device_info *di_a = a;
2701 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002702
Arne Jansen73c5de02011-04-12 12:07:57 +02002703 if (di_a->max_avail > di_b->max_avail)
2704 return -1;
2705 if (di_a->max_avail < di_b->max_avail)
2706 return 1;
2707 if (di_a->total_avail > di_b->total_avail)
2708 return -1;
2709 if (di_a->total_avail < di_b->total_avail)
2710 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002711 return 0;
2712}
2713
2714static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2715 struct btrfs_root *extent_root,
2716 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002717 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002718 u64 start, u64 type)
2719{
2720 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002721 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2722 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002723 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002724 struct extent_map_tree *em_tree;
2725 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002726 struct btrfs_device_info *devices_info = NULL;
2727 u64 total_avail;
2728 int num_stripes; /* total number of stripes to allocate */
2729 int sub_stripes; /* sub_stripes info for map */
2730 int dev_stripes; /* stripes per dev */
2731 int devs_max; /* max devs to use */
2732 int devs_min; /* min devs needed */
2733 int devs_increment; /* ndevs has to be a multiple of this */
2734 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002735 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002736 u64 max_stripe_size;
2737 u64 max_chunk_size;
2738 u64 stripe_size;
2739 u64 num_bytes;
2740 int ndevs;
2741 int i;
2742 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002743
2744 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2745 (type & BTRFS_BLOCK_GROUP_DUP)) {
2746 WARN_ON(1);
2747 type &= ~BTRFS_BLOCK_GROUP_DUP;
2748 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002749
Miao Xieb2117a32011-01-05 10:07:28 +00002750 if (list_empty(&fs_devices->alloc_list))
2751 return -ENOSPC;
2752
Arne Jansen73c5de02011-04-12 12:07:57 +02002753 sub_stripes = 1;
2754 dev_stripes = 1;
2755 devs_increment = 1;
2756 ncopies = 1;
2757 devs_max = 0; /* 0 == as many as possible */
2758 devs_min = 1;
2759
2760 /*
2761 * define the properties of each RAID type.
2762 * FIXME: move this to a global table and use it in all RAID
2763 * calculation code
2764 */
2765 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2766 dev_stripes = 2;
2767 ncopies = 2;
2768 devs_max = 1;
2769 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2770 devs_min = 2;
2771 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2772 devs_increment = 2;
2773 ncopies = 2;
2774 devs_max = 2;
2775 devs_min = 2;
2776 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2777 sub_stripes = 2;
2778 devs_increment = 2;
2779 ncopies = 2;
2780 devs_min = 4;
2781 } else {
2782 devs_max = 1;
2783 }
2784
2785 if (type & BTRFS_BLOCK_GROUP_DATA) {
2786 max_stripe_size = 1024 * 1024 * 1024;
2787 max_chunk_size = 10 * max_stripe_size;
2788 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2789 max_stripe_size = 256 * 1024 * 1024;
2790 max_chunk_size = max_stripe_size;
2791 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2792 max_stripe_size = 8 * 1024 * 1024;
2793 max_chunk_size = 2 * max_stripe_size;
2794 } else {
2795 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2796 type);
2797 BUG_ON(1);
2798 }
2799
2800 /* we don't want a chunk larger than 10% of writeable space */
2801 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2802 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002803
2804 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2805 GFP_NOFS);
2806 if (!devices_info)
2807 return -ENOMEM;
2808
Arne Jansen73c5de02011-04-12 12:07:57 +02002809 cur = fs_devices->alloc_list.next;
2810
2811 /*
2812 * in the first pass through the devices list, we gather information
2813 * about the available holes on each device.
2814 */
2815 ndevs = 0;
2816 while (cur != &fs_devices->alloc_list) {
2817 struct btrfs_device *device;
2818 u64 max_avail;
2819 u64 dev_offset;
2820
2821 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2822
2823 cur = cur->next;
2824
2825 if (!device->writeable) {
2826 printk(KERN_ERR
2827 "btrfs: read-only device in alloc_list\n");
2828 WARN_ON(1);
2829 continue;
2830 }
2831
2832 if (!device->in_fs_metadata)
2833 continue;
2834
2835 if (device->total_bytes > device->bytes_used)
2836 total_avail = device->total_bytes - device->bytes_used;
2837 else
2838 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00002839
2840 /* If there is no space on this device, skip it. */
2841 if (total_avail == 0)
2842 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02002843
2844 ret = find_free_dev_extent(trans, device,
2845 max_stripe_size * dev_stripes,
2846 &dev_offset, &max_avail);
2847 if (ret && ret != -ENOSPC)
2848 goto error;
2849
2850 if (ret == 0)
2851 max_avail = max_stripe_size * dev_stripes;
2852
2853 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2854 continue;
2855
2856 devices_info[ndevs].dev_offset = dev_offset;
2857 devices_info[ndevs].max_avail = max_avail;
2858 devices_info[ndevs].total_avail = total_avail;
2859 devices_info[ndevs].dev = device;
2860 ++ndevs;
2861 }
2862
2863 /*
2864 * now sort the devices by hole size / available space
2865 */
2866 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2867 btrfs_cmp_device_info, NULL);
2868
2869 /* round down to number of usable stripes */
2870 ndevs -= ndevs % devs_increment;
2871
2872 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2873 ret = -ENOSPC;
2874 goto error;
2875 }
2876
2877 if (devs_max && ndevs > devs_max)
2878 ndevs = devs_max;
2879 /*
2880 * the primary goal is to maximize the number of stripes, so use as many
2881 * devices as possible, even if the stripes are not maximum sized.
2882 */
2883 stripe_size = devices_info[ndevs-1].max_avail;
2884 num_stripes = ndevs * dev_stripes;
2885
2886 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2887 stripe_size = max_chunk_size * ncopies;
2888 do_div(stripe_size, num_stripes);
2889 }
2890
2891 do_div(stripe_size, dev_stripes);
2892 do_div(stripe_size, BTRFS_STRIPE_LEN);
2893 stripe_size *= BTRFS_STRIPE_LEN;
2894
Miao Xieb2117a32011-01-05 10:07:28 +00002895 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2896 if (!map) {
2897 ret = -ENOMEM;
2898 goto error;
2899 }
2900 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002901
Arne Jansen73c5de02011-04-12 12:07:57 +02002902 for (i = 0; i < ndevs; ++i) {
2903 for (j = 0; j < dev_stripes; ++j) {
2904 int s = i * dev_stripes + j;
2905 map->stripes[s].dev = devices_info[i].dev;
2906 map->stripes[s].physical = devices_info[i].dev_offset +
2907 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002908 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002909 }
Chris Mason593060d2008-03-25 16:50:33 -04002910 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002911 map->stripe_len = BTRFS_STRIPE_LEN;
2912 map->io_align = BTRFS_STRIPE_LEN;
2913 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002914 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002915 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002916
Yan Zheng2b820322008-11-17 21:11:30 -05002917 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002918 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002919
Arne Jansen73c5de02011-04-12 12:07:57 +02002920 *stripe_size_out = stripe_size;
2921 *num_bytes_out = num_bytes;
2922
2923 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002924
David Sterba172ddd62011-04-21 00:48:27 +02002925 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002926 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002927 ret = -ENOMEM;
2928 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002929 }
Chris Mason0b86a832008-03-24 15:01:56 -04002930 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002931 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002932 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002933 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002934 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002935
Chris Mason0b86a832008-03-24 15:01:56 -04002936 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002937 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002938 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002939 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002940 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002941 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002942
2943 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2944 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002945 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002946 BUG_ON(ret);
2947
Arne Jansen73c5de02011-04-12 12:07:57 +02002948 for (i = 0; i < map->num_stripes; ++i) {
2949 struct btrfs_device *device;
2950 u64 dev_offset;
2951
2952 device = map->stripes[i].dev;
2953 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002954
2955 ret = btrfs_alloc_dev_extent(trans, device,
2956 info->chunk_root->root_key.objectid,
2957 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002958 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002959 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002960 }
2961
Miao Xieb2117a32011-01-05 10:07:28 +00002962 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002963 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002964
2965error:
2966 kfree(map);
2967 kfree(devices_info);
2968 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002969}
2970
2971static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2972 struct btrfs_root *extent_root,
2973 struct map_lookup *map, u64 chunk_offset,
2974 u64 chunk_size, u64 stripe_size)
2975{
2976 u64 dev_offset;
2977 struct btrfs_key key;
2978 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2979 struct btrfs_device *device;
2980 struct btrfs_chunk *chunk;
2981 struct btrfs_stripe *stripe;
2982 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2983 int index = 0;
2984 int ret;
2985
2986 chunk = kzalloc(item_size, GFP_NOFS);
2987 if (!chunk)
2988 return -ENOMEM;
2989
2990 index = 0;
2991 while (index < map->num_stripes) {
2992 device = map->stripes[index].dev;
2993 device->bytes_used += stripe_size;
2994 ret = btrfs_update_device(trans, device);
2995 BUG_ON(ret);
2996 index++;
2997 }
2998
Josef Bacik2bf64752011-09-26 17:12:22 -04002999 spin_lock(&extent_root->fs_info->free_chunk_lock);
3000 extent_root->fs_info->free_chunk_space -= (stripe_size *
3001 map->num_stripes);
3002 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3003
Yan Zheng2b820322008-11-17 21:11:30 -05003004 index = 0;
3005 stripe = &chunk->stripe;
3006 while (index < map->num_stripes) {
3007 device = map->stripes[index].dev;
3008 dev_offset = map->stripes[index].physical;
3009
3010 btrfs_set_stack_stripe_devid(stripe, device->devid);
3011 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3012 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3013 stripe++;
3014 index++;
3015 }
3016
3017 btrfs_set_stack_chunk_length(chunk, chunk_size);
3018 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3019 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3020 btrfs_set_stack_chunk_type(chunk, map->type);
3021 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3022 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3023 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3024 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3025 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3026
3027 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3028 key.type = BTRFS_CHUNK_ITEM_KEY;
3029 key.offset = chunk_offset;
3030
3031 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
3032 BUG_ON(ret);
3033
3034 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3035 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
3036 item_size);
3037 BUG_ON(ret);
3038 }
liubo1abe9b82011-03-24 11:18:59 +00003039
Yan Zheng2b820322008-11-17 21:11:30 -05003040 kfree(chunk);
3041 return 0;
3042}
3043
3044/*
3045 * Chunk allocation falls into two parts. The first part does works
3046 * that make the new allocated chunk useable, but not do any operation
3047 * that modifies the chunk tree. The second part does the works that
3048 * require modifying the chunk tree. This division is important for the
3049 * bootstrap process of adding storage to a seed btrfs.
3050 */
3051int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3052 struct btrfs_root *extent_root, u64 type)
3053{
3054 u64 chunk_offset;
3055 u64 chunk_size;
3056 u64 stripe_size;
3057 struct map_lookup *map;
3058 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3059 int ret;
3060
3061 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3062 &chunk_offset);
3063 if (ret)
3064 return ret;
3065
3066 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3067 &stripe_size, chunk_offset, type);
3068 if (ret)
3069 return ret;
3070
3071 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3072 chunk_size, stripe_size);
3073 BUG_ON(ret);
3074 return 0;
3075}
3076
Chris Masond3977122009-01-05 21:25:51 -05003077static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003078 struct btrfs_root *root,
3079 struct btrfs_device *device)
3080{
3081 u64 chunk_offset;
3082 u64 sys_chunk_offset;
3083 u64 chunk_size;
3084 u64 sys_chunk_size;
3085 u64 stripe_size;
3086 u64 sys_stripe_size;
3087 u64 alloc_profile;
3088 struct map_lookup *map;
3089 struct map_lookup *sys_map;
3090 struct btrfs_fs_info *fs_info = root->fs_info;
3091 struct btrfs_root *extent_root = fs_info->extent_root;
3092 int ret;
3093
3094 ret = find_next_chunk(fs_info->chunk_root,
3095 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003096 if (ret)
3097 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003098
3099 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003100 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003101 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3102
3103 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3104 &stripe_size, chunk_offset, alloc_profile);
3105 BUG_ON(ret);
3106
3107 sys_chunk_offset = chunk_offset + chunk_size;
3108
3109 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003110 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003111 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3112
3113 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3114 &sys_chunk_size, &sys_stripe_size,
3115 sys_chunk_offset, alloc_profile);
3116 BUG_ON(ret);
3117
3118 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3119 BUG_ON(ret);
3120
3121 /*
3122 * Modifying chunk tree needs allocating new blocks from both
3123 * system block group and metadata block group. So we only can
3124 * do operations require modifying the chunk tree after both
3125 * block groups were created.
3126 */
3127 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3128 chunk_size, stripe_size);
3129 BUG_ON(ret);
3130
3131 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3132 sys_chunk_offset, sys_chunk_size,
3133 sys_stripe_size);
3134 BUG_ON(ret);
3135 return 0;
3136}
3137
3138int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3139{
3140 struct extent_map *em;
3141 struct map_lookup *map;
3142 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3143 int readonly = 0;
3144 int i;
3145
Chris Mason890871b2009-09-02 16:24:52 -04003146 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003147 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003148 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003149 if (!em)
3150 return 1;
3151
Josef Bacikf48b9072010-01-27 02:07:59 +00003152 if (btrfs_test_opt(root, DEGRADED)) {
3153 free_extent_map(em);
3154 return 0;
3155 }
3156
Yan Zheng2b820322008-11-17 21:11:30 -05003157 map = (struct map_lookup *)em->bdev;
3158 for (i = 0; i < map->num_stripes; i++) {
3159 if (!map->stripes[i].dev->writeable) {
3160 readonly = 1;
3161 break;
3162 }
3163 }
3164 free_extent_map(em);
3165 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003166}
3167
3168void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3169{
David Sterbaa8067e02011-04-21 00:34:43 +02003170 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003171}
3172
3173void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3174{
3175 struct extent_map *em;
3176
Chris Masond3977122009-01-05 21:25:51 -05003177 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003178 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003179 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3180 if (em)
3181 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003182 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003183 if (!em)
3184 break;
3185 kfree(em->bdev);
3186 /* once for us */
3187 free_extent_map(em);
3188 /* once for the tree */
3189 free_extent_map(em);
3190 }
3191}
3192
Chris Masonf1885912008-04-09 16:28:12 -04003193int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3194{
3195 struct extent_map *em;
3196 struct map_lookup *map;
3197 struct extent_map_tree *em_tree = &map_tree->map_tree;
3198 int ret;
3199
Chris Mason890871b2009-09-02 16:24:52 -04003200 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003201 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003202 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003203 BUG_ON(!em);
3204
3205 BUG_ON(em->start > logical || em->start + em->len < logical);
3206 map = (struct map_lookup *)em->bdev;
3207 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3208 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003209 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3210 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003211 else
3212 ret = 1;
3213 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003214 return ret;
3215}
3216
Chris Masondfe25022008-05-13 13:46:40 -04003217static int find_live_mirror(struct map_lookup *map, int first, int num,
3218 int optimal)
3219{
3220 int i;
3221 if (map->stripes[optimal].dev->bdev)
3222 return optimal;
3223 for (i = first; i < first + num; i++) {
3224 if (map->stripes[i].dev->bdev)
3225 return i;
3226 }
3227 /* we couldn't find one that doesn't fail. Just return something
3228 * and the io error handling code will clean up eventually
3229 */
3230 return optimal;
3231}
3232
Chris Masonf2d8d742008-04-21 10:03:05 -04003233static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3234 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003235 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003236 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003237{
3238 struct extent_map *em;
3239 struct map_lookup *map;
3240 struct extent_map_tree *em_tree = &map_tree->map_tree;
3241 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003242 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003243 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003244 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003245 u64 stripe_nr_orig;
3246 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04003247 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04003248 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003249 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003250 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04003251 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003252 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003253 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003254
Jan Schmidta1d3c472011-08-04 17:15:33 +02003255 if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04003256 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003257again:
Jan Schmidta1d3c472011-08-04 17:15:33 +02003258 if (bbio_ret) {
3259 bbio = kzalloc(btrfs_bio_size(stripes_allocated),
Chris Masoncea9e442008-04-09 16:28:12 -04003260 GFP_NOFS);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003261 if (!bbio)
Chris Masoncea9e442008-04-09 16:28:12 -04003262 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04003263
Jan Schmidta1d3c472011-08-04 17:15:33 +02003264 atomic_set(&bbio->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04003265 }
Chris Mason0b86a832008-03-24 15:01:56 -04003266
Chris Mason890871b2009-09-02 16:24:52 -04003267 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003268 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003269 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003270
Chris Mason3b951512008-04-17 11:29:12 -04003271 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003272 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3273 (unsigned long long)logical,
3274 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003275 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003276 }
Chris Mason0b86a832008-03-24 15:01:56 -04003277
3278 BUG_ON(em->start > logical || em->start + em->len < logical);
3279 map = (struct map_lookup *)em->bdev;
3280 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003281
Chris Masonf1885912008-04-09 16:28:12 -04003282 if (mirror_num > map->num_stripes)
3283 mirror_num = 0;
3284
Jan Schmidta1d3c472011-08-04 17:15:33 +02003285 /* if our btrfs_bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003286 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04003287 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3288 BTRFS_BLOCK_GROUP_DUP)) {
3289 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003290 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003291 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3292 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003293 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003294 }
3295 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003296 if (rw & REQ_DISCARD) {
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003297 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003298 stripes_required = map->num_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003299 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003300 if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04003301 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04003302 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003303 free_extent_map(em);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003304 kfree(bbio);
Chris Masoncea9e442008-04-09 16:28:12 -04003305 goto again;
3306 }
Chris Mason593060d2008-03-25 16:50:33 -04003307 stripe_nr = offset;
3308 /*
3309 * stripe_nr counts the total number of stripes we have to stride
3310 * to get to this block
3311 */
3312 do_div(stripe_nr, map->stripe_len);
3313
3314 stripe_offset = stripe_nr * map->stripe_len;
3315 BUG_ON(offset < stripe_offset);
3316
3317 /* stripe_offset is the offset of this block in its stripe*/
3318 stripe_offset = offset - stripe_offset;
3319
Li Dongyangfce3bb92011-03-24 10:24:26 +00003320 if (rw & REQ_DISCARD)
3321 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003322 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003323 /* we limit the length of each bio to what fits in a stripe */
3324 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003325 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003326 } else {
3327 *length = em->len - offset;
3328 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003329
Jan Schmidta1d3c472011-08-04 17:15:33 +02003330 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003331 goto out;
3332
Chris Masonf2d8d742008-04-21 10:03:05 -04003333 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003334 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003335 stripe_nr_orig = stripe_nr;
3336 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3337 (~(map->stripe_len - 1));
3338 do_div(stripe_nr_end, map->stripe_len);
3339 stripe_end_offset = stripe_nr_end * map->stripe_len -
3340 (offset + *length);
3341 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3342 if (rw & REQ_DISCARD)
3343 num_stripes = min_t(u64, map->num_stripes,
3344 stripe_nr_end - stripe_nr_orig);
3345 stripe_index = do_div(stripe_nr, map->num_stripes);
3346 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003347 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003348 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003349 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003350 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003351 else {
3352 stripe_index = find_live_mirror(map, 0,
3353 map->num_stripes,
3354 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003355 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003356 }
Chris Mason2fff7342008-04-29 14:12:09 -04003357
Chris Mason611f0e02008-04-03 16:29:03 -04003358 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003359 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003360 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003361 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003362 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003363 } else {
3364 mirror_num = 1;
3365 }
Chris Mason2fff7342008-04-29 14:12:09 -04003366
Chris Mason321aecc2008-04-16 10:49:51 -04003367 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3368 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003369
3370 stripe_index = do_div(stripe_nr, factor);
3371 stripe_index *= map->sub_stripes;
3372
Jens Axboe7eaceac2011-03-10 08:52:07 +01003373 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003374 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003375 else if (rw & REQ_DISCARD)
3376 num_stripes = min_t(u64, map->sub_stripes *
3377 (stripe_nr_end - stripe_nr_orig),
3378 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003379 else if (mirror_num)
3380 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003381 else {
3382 stripe_index = find_live_mirror(map, stripe_index,
3383 map->sub_stripes, stripe_index +
3384 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003385 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003386 }
Chris Mason8790d502008-04-03 16:29:03 -04003387 } else {
3388 /*
3389 * after this do_div call, stripe_nr is the number of stripes
3390 * on this device we have to walk to find the data, and
3391 * stripe_index is the number of our device in the stripe array
3392 */
3393 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003394 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003395 }
Chris Mason593060d2008-03-25 16:50:33 -04003396 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003397
Li Dongyangfce3bb92011-03-24 10:24:26 +00003398 if (rw & REQ_DISCARD) {
3399 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003400 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003401 map->stripes[stripe_index].physical +
3402 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003403 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003404
3405 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3406 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003407 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003408 int j;
3409
Chris Masond9d04872011-03-27 21:23:21 -04003410 div_u64_rem(stripe_nr_end - 1,
3411 map->num_stripes,
3412 &last_stripe);
3413
Li Dongyangfce3bb92011-03-24 10:24:26 +00003414 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003415 u32 test;
3416
3417 div_u64_rem(stripe_nr_end - 1 - j,
3418 map->num_stripes, &test);
3419 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003420 break;
3421 }
3422 stripes = stripe_nr_end - 1 - j;
3423 do_div(stripes, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003424 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003425 (stripes - stripe_nr + 1);
3426
3427 if (i == 0) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003428 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003429 stripe_offset;
3430 stripe_offset = 0;
3431 }
3432 if (stripe_index == last_stripe)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003433 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003434 stripe_end_offset;
3435 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3436 u64 stripes;
3437 int j;
3438 int factor = map->num_stripes /
3439 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003440 u32 last_stripe = 0;
3441
3442 div_u64_rem(stripe_nr_end - 1,
3443 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003444 last_stripe *= map->sub_stripes;
3445
3446 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003447 u32 test;
3448
3449 div_u64_rem(stripe_nr_end - 1 - j,
3450 factor, &test);
3451
3452 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003453 stripe_index / map->sub_stripes)
3454 break;
3455 }
3456 stripes = stripe_nr_end - 1 - j;
3457 do_div(stripes, factor);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003458 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003459 (stripes - stripe_nr + 1);
3460
3461 if (i < map->sub_stripes) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003462 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003463 stripe_offset;
3464 if (i == map->sub_stripes - 1)
3465 stripe_offset = 0;
3466 }
3467 if (stripe_index >= last_stripe &&
3468 stripe_index <= (last_stripe +
3469 map->sub_stripes - 1)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003470 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003471 stripe_end_offset;
3472 }
3473 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003474 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003475
3476 stripe_index++;
3477 if (stripe_index == map->num_stripes) {
3478 /* This could only happen for RAID0/10 */
3479 stripe_index = 0;
3480 stripe_nr++;
3481 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003482 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003483 } else {
3484 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003485 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003486 map->stripes[stripe_index].physical +
3487 stripe_offset +
3488 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003489 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003490 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003491 stripe_index++;
3492 }
Chris Mason593060d2008-03-25 16:50:33 -04003493 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003494 if (bbio_ret) {
3495 *bbio_ret = bbio;
3496 bbio->num_stripes = num_stripes;
3497 bbio->max_errors = max_errors;
3498 bbio->mirror_num = mirror_num;
Chris Masonf2d8d742008-04-21 10:03:05 -04003499 }
Chris Masoncea9e442008-04-09 16:28:12 -04003500out:
Chris Mason0b86a832008-03-24 15:01:56 -04003501 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003502 return 0;
3503}
3504
Chris Masonf2d8d742008-04-21 10:03:05 -04003505int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3506 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003507 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003508{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003509 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003510 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003511}
3512
Yan Zhenga512bbf2008-12-08 16:46:26 -05003513int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3514 u64 chunk_start, u64 physical, u64 devid,
3515 u64 **logical, int *naddrs, int *stripe_len)
3516{
3517 struct extent_map_tree *em_tree = &map_tree->map_tree;
3518 struct extent_map *em;
3519 struct map_lookup *map;
3520 u64 *buf;
3521 u64 bytenr;
3522 u64 length;
3523 u64 stripe_nr;
3524 int i, j, nr = 0;
3525
Chris Mason890871b2009-09-02 16:24:52 -04003526 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003527 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003528 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003529
3530 BUG_ON(!em || em->start != chunk_start);
3531 map = (struct map_lookup *)em->bdev;
3532
3533 length = em->len;
3534 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3535 do_div(length, map->num_stripes / map->sub_stripes);
3536 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3537 do_div(length, map->num_stripes);
3538
3539 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3540 BUG_ON(!buf);
3541
3542 for (i = 0; i < map->num_stripes; i++) {
3543 if (devid && map->stripes[i].dev->devid != devid)
3544 continue;
3545 if (map->stripes[i].physical > physical ||
3546 map->stripes[i].physical + length <= physical)
3547 continue;
3548
3549 stripe_nr = physical - map->stripes[i].physical;
3550 do_div(stripe_nr, map->stripe_len);
3551
3552 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3553 stripe_nr = stripe_nr * map->num_stripes + i;
3554 do_div(stripe_nr, map->sub_stripes);
3555 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3556 stripe_nr = stripe_nr * map->num_stripes + i;
3557 }
3558 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003559 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003560 for (j = 0; j < nr; j++) {
3561 if (buf[j] == bytenr)
3562 break;
3563 }
Chris Mason934d3752008-12-08 16:43:10 -05003564 if (j == nr) {
3565 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003566 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003567 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003568 }
3569
Yan Zhenga512bbf2008-12-08 16:46:26 -05003570 *logical = buf;
3571 *naddrs = nr;
3572 *stripe_len = map->stripe_len;
3573
3574 free_extent_map(em);
3575 return 0;
3576}
3577
Jan Schmidta1d3c472011-08-04 17:15:33 +02003578static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003579{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003580 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003581 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003582
Chris Mason8790d502008-04-03 16:29:03 -04003583 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003584 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003585
Jan Schmidta1d3c472011-08-04 17:15:33 +02003586 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003587 is_orig_bio = 1;
3588
Jan Schmidta1d3c472011-08-04 17:15:33 +02003589 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003590 if (!is_orig_bio) {
3591 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003592 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003593 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003594 bio->bi_private = bbio->private;
3595 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003596 bio->bi_bdev = (struct block_device *)
3597 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003598 /* only send an error to the higher layers if it is
3599 * beyond the tolerance of the multi-bio
3600 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003601 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003602 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003603 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003604 /*
3605 * this bio is actually up to date, we didn't
3606 * go over the max number of errors
3607 */
3608 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003609 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003610 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003611 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003612
3613 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003614 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003615 bio_put(bio);
3616 }
Chris Mason8790d502008-04-03 16:29:03 -04003617}
3618
Chris Mason8b712842008-06-11 16:50:36 -04003619struct async_sched {
3620 struct bio *bio;
3621 int rw;
3622 struct btrfs_fs_info *info;
3623 struct btrfs_work work;
3624};
3625
3626/*
3627 * see run_scheduled_bios for a description of why bios are collected for
3628 * async submit.
3629 *
3630 * This will add one bio to the pending list for a device and make sure
3631 * the work struct is scheduled.
3632 */
Chris Masond3977122009-01-05 21:25:51 -05003633static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003634 struct btrfs_device *device,
3635 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003636{
3637 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003638 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003639
3640 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003641 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003642 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003643 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003644 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003645 return 0;
3646 }
3647
3648 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003649 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003650 * higher layers. Otherwise, the async bio makes it appear we have
3651 * made progress against dirty pages when we've really just put it
3652 * on a queue for later
3653 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003654 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003655 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003656 bio->bi_next = NULL;
3657 bio->bi_rw |= rw;
3658
3659 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003660 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003661 pending_bios = &device->pending_sync_bios;
3662 else
3663 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003664
Chris Masonffbd5172009-04-20 15:50:09 -04003665 if (pending_bios->tail)
3666 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003667
Chris Masonffbd5172009-04-20 15:50:09 -04003668 pending_bios->tail = bio;
3669 if (!pending_bios->head)
3670 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003671 if (device->running_pending)
3672 should_queue = 0;
3673
3674 spin_unlock(&device->io_lock);
3675
3676 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003677 btrfs_queue_worker(&root->fs_info->submit_workers,
3678 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003679 return 0;
3680}
3681
Chris Masonf1885912008-04-09 16:28:12 -04003682int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003683 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003684{
3685 struct btrfs_mapping_tree *map_tree;
3686 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003687 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003688 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003689 u64 length = 0;
3690 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04003691 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003692 int dev_nr = 0;
3693 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003694 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003695
Chris Masonf2d8d742008-04-21 10:03:05 -04003696 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003697 map_tree = &root->fs_info->mapping_tree;
3698 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003699
Jan Schmidta1d3c472011-08-04 17:15:33 +02003700 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04003701 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003702 BUG_ON(ret);
3703
Jan Schmidta1d3c472011-08-04 17:15:33 +02003704 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003705 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003706 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3707 "len %llu\n", (unsigned long long)logical,
3708 (unsigned long long)length,
3709 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003710 BUG();
3711 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003712
3713 bbio->orig_bio = first_bio;
3714 bbio->private = first_bio->bi_private;
3715 bbio->end_io = first_bio->bi_end_io;
3716 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04003717
Chris Masond3977122009-01-05 21:25:51 -05003718 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003719 if (dev_nr < total_devs - 1) {
3720 bio = bio_clone(first_bio, GFP_NOFS);
3721 BUG_ON(!bio);
3722 } else {
3723 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04003724 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003725 bio->bi_private = bbio;
3726 bio->bi_end_io = btrfs_end_bio;
3727 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
3728 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003729 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003730 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3731 "(%s id %llu), size=%u\n", rw,
3732 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
3733 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04003734 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003735 if (async_submit)
3736 schedule_bio(root, dev, rw, bio);
3737 else
3738 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003739 } else {
3740 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3741 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003742 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003743 }
Chris Mason8790d502008-04-03 16:29:03 -04003744 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003745 }
Chris Mason0b86a832008-03-24 15:01:56 -04003746 return 0;
3747}
3748
Chris Masona4437552008-04-18 10:29:38 -04003749struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003750 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003751{
Yan Zheng2b820322008-11-17 21:11:30 -05003752 struct btrfs_device *device;
3753 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003754
Yan Zheng2b820322008-11-17 21:11:30 -05003755 cur_devices = root->fs_info->fs_devices;
3756 while (cur_devices) {
3757 if (!fsid ||
3758 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3759 device = __find_device(&cur_devices->devices,
3760 devid, uuid);
3761 if (device)
3762 return device;
3763 }
3764 cur_devices = cur_devices->seed;
3765 }
3766 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003767}
3768
Chris Masondfe25022008-05-13 13:46:40 -04003769static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3770 u64 devid, u8 *dev_uuid)
3771{
3772 struct btrfs_device *device;
3773 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3774
3775 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003776 if (!device)
3777 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003778 list_add(&device->dev_list,
3779 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003780 device->dev_root = root->fs_info->dev_root;
3781 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003782 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003783 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003784 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003785 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003786 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003787 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003788 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003789 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3790 return device;
3791}
3792
Chris Mason0b86a832008-03-24 15:01:56 -04003793static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3794 struct extent_buffer *leaf,
3795 struct btrfs_chunk *chunk)
3796{
3797 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3798 struct map_lookup *map;
3799 struct extent_map *em;
3800 u64 logical;
3801 u64 length;
3802 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003803 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003804 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003805 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003806 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003807
Chris Masone17cade2008-04-15 15:41:47 -04003808 logical = key->offset;
3809 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003810
Chris Mason890871b2009-09-02 16:24:52 -04003811 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003812 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003813 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003814
3815 /* already mapped? */
3816 if (em && em->start <= logical && em->start + em->len > logical) {
3817 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003818 return 0;
3819 } else if (em) {
3820 free_extent_map(em);
3821 }
Chris Mason0b86a832008-03-24 15:01:56 -04003822
David Sterba172ddd62011-04-21 00:48:27 +02003823 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003824 if (!em)
3825 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003826 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3827 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003828 if (!map) {
3829 free_extent_map(em);
3830 return -ENOMEM;
3831 }
3832
3833 em->bdev = (struct block_device *)map;
3834 em->start = logical;
3835 em->len = length;
3836 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003837 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003838
Chris Mason593060d2008-03-25 16:50:33 -04003839 map->num_stripes = num_stripes;
3840 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3841 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3842 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3843 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3844 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003845 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003846 for (i = 0; i < num_stripes; i++) {
3847 map->stripes[i].physical =
3848 btrfs_stripe_offset_nr(leaf, chunk, i);
3849 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003850 read_extent_buffer(leaf, uuid, (unsigned long)
3851 btrfs_stripe_dev_uuid_nr(chunk, i),
3852 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003853 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3854 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003855 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003856 kfree(map);
3857 free_extent_map(em);
3858 return -EIO;
3859 }
Chris Masondfe25022008-05-13 13:46:40 -04003860 if (!map->stripes[i].dev) {
3861 map->stripes[i].dev =
3862 add_missing_dev(root, devid, uuid);
3863 if (!map->stripes[i].dev) {
3864 kfree(map);
3865 free_extent_map(em);
3866 return -EIO;
3867 }
3868 }
3869 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003870 }
3871
Chris Mason890871b2009-09-02 16:24:52 -04003872 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003873 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003874 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003875 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003876 free_extent_map(em);
3877
3878 return 0;
3879}
3880
3881static int fill_device_from_item(struct extent_buffer *leaf,
3882 struct btrfs_dev_item *dev_item,
3883 struct btrfs_device *device)
3884{
3885 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003886
3887 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003888 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3889 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003890 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3891 device->type = btrfs_device_type(leaf, dev_item);
3892 device->io_align = btrfs_device_io_align(leaf, dev_item);
3893 device->io_width = btrfs_device_io_width(leaf, dev_item);
3894 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003895
3896 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003897 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003898
Chris Mason0b86a832008-03-24 15:01:56 -04003899 return 0;
3900}
3901
Yan Zheng2b820322008-11-17 21:11:30 -05003902static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3903{
3904 struct btrfs_fs_devices *fs_devices;
3905 int ret;
3906
3907 mutex_lock(&uuid_mutex);
3908
3909 fs_devices = root->fs_info->fs_devices->seed;
3910 while (fs_devices) {
3911 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3912 ret = 0;
3913 goto out;
3914 }
3915 fs_devices = fs_devices->seed;
3916 }
3917
3918 fs_devices = find_fsid(fsid);
3919 if (!fs_devices) {
3920 ret = -ENOENT;
3921 goto out;
3922 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003923
3924 fs_devices = clone_fs_devices(fs_devices);
3925 if (IS_ERR(fs_devices)) {
3926 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003927 goto out;
3928 }
3929
Christoph Hellwig97288f22008-12-02 06:36:09 -05003930 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003931 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003932 if (ret)
3933 goto out;
3934
3935 if (!fs_devices->seeding) {
3936 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003937 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003938 ret = -EINVAL;
3939 goto out;
3940 }
3941
3942 fs_devices->seed = root->fs_info->fs_devices->seed;
3943 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003944out:
3945 mutex_unlock(&uuid_mutex);
3946 return ret;
3947}
3948
Chris Mason0d81ba52008-03-24 15:02:07 -04003949static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003950 struct extent_buffer *leaf,
3951 struct btrfs_dev_item *dev_item)
3952{
3953 struct btrfs_device *device;
3954 u64 devid;
3955 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003956 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003957 u8 dev_uuid[BTRFS_UUID_SIZE];
3958
Chris Mason0b86a832008-03-24 15:01:56 -04003959 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003960 read_extent_buffer(leaf, dev_uuid,
3961 (unsigned long)btrfs_device_uuid(dev_item),
3962 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003963 read_extent_buffer(leaf, fs_uuid,
3964 (unsigned long)btrfs_device_fsid(dev_item),
3965 BTRFS_UUID_SIZE);
3966
3967 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3968 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003969 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003970 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003971 }
3972
3973 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3974 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003975 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003976 return -EIO;
3977
3978 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003979 printk(KERN_WARNING "warning devid %llu missing\n",
3980 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003981 device = add_missing_dev(root, devid, dev_uuid);
3982 if (!device)
3983 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003984 } else if (!device->missing) {
3985 /*
3986 * this happens when a device that was properly setup
3987 * in the device info lists suddenly goes bad.
3988 * device->bdev is NULL, and so we have to set
3989 * device->missing to one here
3990 */
3991 root->fs_info->fs_devices->missing_devices++;
3992 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003993 }
3994 }
3995
3996 if (device->fs_devices != root->fs_info->fs_devices) {
3997 BUG_ON(device->writeable);
3998 if (device->generation !=
3999 btrfs_device_generation(leaf, dev_item))
4000 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004001 }
Chris Mason0b86a832008-03-24 15:01:56 -04004002
4003 fill_device_from_item(leaf, dev_item, device);
4004 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004005 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004006 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004007 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004008 spin_lock(&root->fs_info->free_chunk_lock);
4009 root->fs_info->free_chunk_space += device->total_bytes -
4010 device->bytes_used;
4011 spin_unlock(&root->fs_info->free_chunk_lock);
4012 }
Chris Mason0b86a832008-03-24 15:01:56 -04004013 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004014 return ret;
4015}
4016
Yan Zhenge4404d62008-12-12 10:03:26 -05004017int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004018{
David Sterba6c417612011-04-13 15:41:04 +02004019 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004020 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004021 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004022 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004023 u8 *ptr;
4024 unsigned long sb_ptr;
4025 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004026 u32 num_stripes;
4027 u32 array_size;
4028 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004029 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004030 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004031
Yan Zhenge4404d62008-12-12 10:03:26 -05004032 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004033 BTRFS_SUPER_INFO_SIZE);
4034 if (!sb)
4035 return -ENOMEM;
4036 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004037 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05004038
Chris Masona061fc82008-05-07 11:43:44 -04004039 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004040 array_size = btrfs_super_sys_array_size(super_copy);
4041
Chris Mason0b86a832008-03-24 15:01:56 -04004042 ptr = super_copy->sys_chunk_array;
4043 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4044 cur = 0;
4045
4046 while (cur < array_size) {
4047 disk_key = (struct btrfs_disk_key *)ptr;
4048 btrfs_disk_key_to_cpu(&key, disk_key);
4049
Chris Masona061fc82008-05-07 11:43:44 -04004050 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004051 sb_ptr += len;
4052 cur += len;
4053
Chris Mason0d81ba52008-03-24 15:02:07 -04004054 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004055 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004056 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004057 if (ret)
4058 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004059 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4060 len = btrfs_chunk_item_size(num_stripes);
4061 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004062 ret = -EIO;
4063 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004064 }
4065 ptr += len;
4066 sb_ptr += len;
4067 cur += len;
4068 }
Chris Masona061fc82008-05-07 11:43:44 -04004069 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004070 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004071}
4072
4073int btrfs_read_chunk_tree(struct btrfs_root *root)
4074{
4075 struct btrfs_path *path;
4076 struct extent_buffer *leaf;
4077 struct btrfs_key key;
4078 struct btrfs_key found_key;
4079 int ret;
4080 int slot;
4081
4082 root = root->fs_info->chunk_root;
4083
4084 path = btrfs_alloc_path();
4085 if (!path)
4086 return -ENOMEM;
4087
4088 /* first we search for all of the device items, and then we
4089 * read in all of the chunk items. This way we can create chunk
4090 * mappings that reference all of the devices that are afound
4091 */
4092 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4093 key.offset = 0;
4094 key.type = 0;
4095again:
4096 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004097 if (ret < 0)
4098 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004099 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004100 leaf = path->nodes[0];
4101 slot = path->slots[0];
4102 if (slot >= btrfs_header_nritems(leaf)) {
4103 ret = btrfs_next_leaf(root, path);
4104 if (ret == 0)
4105 continue;
4106 if (ret < 0)
4107 goto error;
4108 break;
4109 }
4110 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4111 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4112 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4113 break;
4114 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4115 struct btrfs_dev_item *dev_item;
4116 dev_item = btrfs_item_ptr(leaf, slot,
4117 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004118 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004119 if (ret)
4120 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004121 }
4122 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4123 struct btrfs_chunk *chunk;
4124 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4125 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004126 if (ret)
4127 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004128 }
4129 path->slots[0]++;
4130 }
4131 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4132 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004133 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004134 goto again;
4135 }
Chris Mason0b86a832008-03-24 15:01:56 -04004136 ret = 0;
4137error:
Yan Zheng2b820322008-11-17 21:11:30 -05004138 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004139 return ret;
4140}