blob: 73f673c8d8d82f22363a84858ee288cbddf60a73 [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
Miao Xie7bfc8372011-01-05 10:07:26 +0000832 * @device: the device which we search the free space in
833 * @num_bytes: the size of the free space that we need
834 * @start: store the start of the free space.
835 * @len: the size of the free space. that we find, or the size of the max
836 * free space if we don't find suitable free space
837 *
Chris Mason0b86a832008-03-24 15:01:56 -0400838 * this uses a pretty simple search, the expectation is that it is
839 * called very infrequently and that a given device has a small number
840 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000841 *
842 * @start is used to store the start of the free space if we find. But if we
843 * don't find suitable free space, it will be used to store the start position
844 * of the max free space.
845 *
846 * @len is used to store the size of the free space that we find.
847 * But if we don't find suitable free space, it is used to store the size of
848 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400849 */
Li Zefan125ccb02011-12-08 15:07:24 +0800850int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000851 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400852{
853 struct btrfs_key key;
854 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000855 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400856 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 u64 hole_size;
858 u64 max_hole_start;
859 u64 max_hole_size;
860 u64 extent_end;
861 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400862 u64 search_end = device->total_bytes;
863 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000864 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400865 struct extent_buffer *l;
866
Chris Mason0b86a832008-03-24 15:01:56 -0400867 /* FIXME use last free of some kind */
868
869 /* we don't want to overwrite the superblock on the drive,
870 * so we make sure to start at an offset of at least 1MB
871 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200872 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400873
Miao Xie7bfc8372011-01-05 10:07:26 +0000874 max_hole_start = search_start;
875 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000876 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000877
878 if (search_start >= search_end) {
879 ret = -ENOSPC;
880 goto error;
881 }
882
883 path = btrfs_alloc_path();
884 if (!path) {
885 ret = -ENOMEM;
886 goto error;
887 }
888 path->reada = 2;
889
Chris Mason0b86a832008-03-24 15:01:56 -0400890 key.objectid = device->devid;
891 key.offset = search_start;
892 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000893
Li Zefan125ccb02011-12-08 15:07:24 +0800894 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400895 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000896 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400897 if (ret > 0) {
898 ret = btrfs_previous_item(root, path, key.objectid, key.type);
899 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000900 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400901 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000902
Chris Mason0b86a832008-03-24 15:01:56 -0400903 while (1) {
904 l = path->nodes[0];
905 slot = path->slots[0];
906 if (slot >= btrfs_header_nritems(l)) {
907 ret = btrfs_next_leaf(root, path);
908 if (ret == 0)
909 continue;
910 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 goto out;
912
913 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400914 }
915 btrfs_item_key_to_cpu(l, &key, slot);
916
917 if (key.objectid < device->devid)
918 goto next;
919
920 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000921 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400922
Chris Mason0b86a832008-03-24 15:01:56 -0400923 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
924 goto next;
925
Miao Xie7bfc8372011-01-05 10:07:26 +0000926 if (key.offset > search_start) {
927 hole_size = key.offset - search_start;
928
929 if (hole_size > max_hole_size) {
930 max_hole_start = search_start;
931 max_hole_size = hole_size;
932 }
933
934 /*
935 * If this free space is greater than which we need,
936 * it must be the max free space that we have found
937 * until now, so max_hole_start must point to the start
938 * of this free space and the length of this free space
939 * is stored in max_hole_size. Thus, we return
940 * max_hole_start and max_hole_size and go back to the
941 * caller.
942 */
943 if (hole_size >= num_bytes) {
944 ret = 0;
945 goto out;
946 }
947 }
948
Chris Mason0b86a832008-03-24 15:01:56 -0400949 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000950 extent_end = key.offset + btrfs_dev_extent_length(l,
951 dev_extent);
952 if (extent_end > search_start)
953 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400954next:
955 path->slots[0]++;
956 cond_resched();
957 }
Chris Mason0b86a832008-03-24 15:01:56 -0400958
liubo38c01b92011-08-02 02:39:03 +0000959 /*
960 * At this point, search_start should be the end of
961 * allocated dev extents, and when shrinking the device,
962 * search_end may be smaller than search_start.
963 */
964 if (search_end > search_start)
965 hole_size = search_end - search_start;
966
Miao Xie7bfc8372011-01-05 10:07:26 +0000967 if (hole_size > max_hole_size) {
968 max_hole_start = search_start;
969 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400970 }
Chris Mason0b86a832008-03-24 15:01:56 -0400971
Miao Xie7bfc8372011-01-05 10:07:26 +0000972 /* See above. */
973 if (hole_size < num_bytes)
974 ret = -ENOSPC;
975 else
976 ret = 0;
977
978out:
Yan Zheng2b820322008-11-17 21:11:30 -0500979 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000980error:
981 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000982 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000983 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400984 return ret;
985}
986
Christoph Hellwigb2950862008-12-02 09:54:17 -0500987static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400988 struct btrfs_device *device,
989 u64 start)
990{
991 int ret;
992 struct btrfs_path *path;
993 struct btrfs_root *root = device->dev_root;
994 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400995 struct btrfs_key found_key;
996 struct extent_buffer *leaf = NULL;
997 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400998
999 path = btrfs_alloc_path();
1000 if (!path)
1001 return -ENOMEM;
1002
1003 key.objectid = device->devid;
1004 key.offset = start;
1005 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001006again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001007 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001008 if (ret > 0) {
1009 ret = btrfs_previous_item(root, path, key.objectid,
1010 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001011 if (ret)
1012 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001013 leaf = path->nodes[0];
1014 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1015 extent = btrfs_item_ptr(leaf, path->slots[0],
1016 struct btrfs_dev_extent);
1017 BUG_ON(found_key.offset > start || found_key.offset +
1018 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001019 key = found_key;
1020 btrfs_release_path(path);
1021 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001022 } else if (ret == 0) {
1023 leaf = path->nodes[0];
1024 extent = btrfs_item_ptr(leaf, path->slots[0],
1025 struct btrfs_dev_extent);
1026 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001027 BUG_ON(ret);
1028
Josef Bacik2bf64752011-09-26 17:12:22 -04001029 if (device->bytes_used > 0) {
1030 u64 len = btrfs_dev_extent_length(leaf, extent);
1031 device->bytes_used -= len;
1032 spin_lock(&root->fs_info->free_chunk_lock);
1033 root->fs_info->free_chunk_space += len;
1034 spin_unlock(&root->fs_info->free_chunk_lock);
1035 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001036 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001037
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001038out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001039 btrfs_free_path(path);
1040 return ret;
1041}
1042
Yan Zheng2b820322008-11-17 21:11:30 -05001043int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001044 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001045 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001046 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001047{
1048 int ret;
1049 struct btrfs_path *path;
1050 struct btrfs_root *root = device->dev_root;
1051 struct btrfs_dev_extent *extent;
1052 struct extent_buffer *leaf;
1053 struct btrfs_key key;
1054
Chris Masondfe25022008-05-13 13:46:40 -04001055 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001056 path = btrfs_alloc_path();
1057 if (!path)
1058 return -ENOMEM;
1059
Chris Mason0b86a832008-03-24 15:01:56 -04001060 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001061 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001062 key.type = BTRFS_DEV_EXTENT_KEY;
1063 ret = btrfs_insert_empty_item(trans, root, path, &key,
1064 sizeof(*extent));
1065 BUG_ON(ret);
1066
1067 leaf = path->nodes[0];
1068 extent = btrfs_item_ptr(leaf, path->slots[0],
1069 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001070 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1071 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1072 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1073
1074 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1075 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1076 BTRFS_UUID_SIZE);
1077
Chris Mason0b86a832008-03-24 15:01:56 -04001078 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1079 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001080 btrfs_free_path(path);
1081 return ret;
1082}
1083
Chris Masona1b32a52008-09-05 16:09:51 -04001084static noinline int find_next_chunk(struct btrfs_root *root,
1085 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001086{
1087 struct btrfs_path *path;
1088 int ret;
1089 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001090 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001091 struct btrfs_key found_key;
1092
1093 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001094 if (!path)
1095 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001096
Chris Masone17cade2008-04-15 15:41:47 -04001097 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001098 key.offset = (u64)-1;
1099 key.type = BTRFS_CHUNK_ITEM_KEY;
1100
1101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1102 if (ret < 0)
1103 goto error;
1104
1105 BUG_ON(ret == 0);
1106
1107 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1108 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001109 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001110 } else {
1111 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1112 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001113 if (found_key.objectid != objectid)
1114 *offset = 0;
1115 else {
1116 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1117 struct btrfs_chunk);
1118 *offset = found_key.offset +
1119 btrfs_chunk_length(path->nodes[0], chunk);
1120 }
Chris Mason0b86a832008-03-24 15:01:56 -04001121 }
1122 ret = 0;
1123error:
1124 btrfs_free_path(path);
1125 return ret;
1126}
1127
Yan Zheng2b820322008-11-17 21:11:30 -05001128static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001129{
1130 int ret;
1131 struct btrfs_key key;
1132 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001133 struct btrfs_path *path;
1134
1135 root = root->fs_info->chunk_root;
1136
1137 path = btrfs_alloc_path();
1138 if (!path)
1139 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001140
1141 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1142 key.type = BTRFS_DEV_ITEM_KEY;
1143 key.offset = (u64)-1;
1144
1145 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1146 if (ret < 0)
1147 goto error;
1148
1149 BUG_ON(ret == 0);
1150
1151 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1152 BTRFS_DEV_ITEM_KEY);
1153 if (ret) {
1154 *objectid = 1;
1155 } else {
1156 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1157 path->slots[0]);
1158 *objectid = found_key.offset + 1;
1159 }
1160 ret = 0;
1161error:
Yan Zheng2b820322008-11-17 21:11:30 -05001162 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001163 return ret;
1164}
1165
1166/*
1167 * the device information is stored in the chunk root
1168 * the btrfs_device struct should be fully filled in
1169 */
1170int btrfs_add_device(struct btrfs_trans_handle *trans,
1171 struct btrfs_root *root,
1172 struct btrfs_device *device)
1173{
1174 int ret;
1175 struct btrfs_path *path;
1176 struct btrfs_dev_item *dev_item;
1177 struct extent_buffer *leaf;
1178 struct btrfs_key key;
1179 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001180
1181 root = root->fs_info->chunk_root;
1182
1183 path = btrfs_alloc_path();
1184 if (!path)
1185 return -ENOMEM;
1186
Chris Mason0b86a832008-03-24 15:01:56 -04001187 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1188 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001189 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001190
1191 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001192 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001193 if (ret)
1194 goto out;
1195
1196 leaf = path->nodes[0];
1197 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1198
1199 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001200 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001201 btrfs_set_device_type(leaf, dev_item, device->type);
1202 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1203 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1204 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001205 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1206 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001207 btrfs_set_device_group(leaf, dev_item, 0);
1208 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1209 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001210 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001211
Chris Mason0b86a832008-03-24 15:01:56 -04001212 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001213 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001214 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1215 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001216 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001217
Yan Zheng2b820322008-11-17 21:11:30 -05001218 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001219out:
1220 btrfs_free_path(path);
1221 return ret;
1222}
Chris Mason8f18cf12008-04-25 16:53:30 -04001223
Chris Masona061fc82008-05-07 11:43:44 -04001224static int btrfs_rm_dev_item(struct btrfs_root *root,
1225 struct btrfs_device *device)
1226{
1227 int ret;
1228 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001229 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001230 struct btrfs_trans_handle *trans;
1231
1232 root = root->fs_info->chunk_root;
1233
1234 path = btrfs_alloc_path();
1235 if (!path)
1236 return -ENOMEM;
1237
Yan, Zhenga22285a2010-05-16 10:48:46 -04001238 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001239 if (IS_ERR(trans)) {
1240 btrfs_free_path(path);
1241 return PTR_ERR(trans);
1242 }
Chris Masona061fc82008-05-07 11:43:44 -04001243 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1244 key.type = BTRFS_DEV_ITEM_KEY;
1245 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001246 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001247
1248 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1249 if (ret < 0)
1250 goto out;
1251
1252 if (ret > 0) {
1253 ret = -ENOENT;
1254 goto out;
1255 }
1256
1257 ret = btrfs_del_item(trans, root, path);
1258 if (ret)
1259 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001260out:
1261 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001262 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001263 btrfs_commit_transaction(trans, root);
1264 return ret;
1265}
1266
1267int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1268{
1269 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001270 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001271 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001272 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001273 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001274 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001275 u64 all_avail;
1276 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001277 u64 num_devices;
1278 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001279 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001280 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001281
Chris Masona061fc82008-05-07 11:43:44 -04001282 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001283 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001284
1285 all_avail = root->fs_info->avail_data_alloc_bits |
1286 root->fs_info->avail_system_alloc_bits |
1287 root->fs_info->avail_metadata_alloc_bits;
1288
1289 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001290 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001291 printk(KERN_ERR "btrfs: unable to go below four devices "
1292 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001293 ret = -EINVAL;
1294 goto out;
1295 }
1296
1297 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001298 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001299 printk(KERN_ERR "btrfs: unable to go below two "
1300 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001301 ret = -EINVAL;
1302 goto out;
1303 }
1304
Chris Masondfe25022008-05-13 13:46:40 -04001305 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001306 struct list_head *devices;
1307 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001308
Chris Masondfe25022008-05-13 13:46:40 -04001309 device = NULL;
1310 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001311 /*
1312 * It is safe to read the devices since the volume_mutex
1313 * is held.
1314 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001315 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001316 if (tmp->in_fs_metadata && !tmp->bdev) {
1317 device = tmp;
1318 break;
1319 }
1320 }
1321 bdev = NULL;
1322 bh = NULL;
1323 disk_super = NULL;
1324 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001325 printk(KERN_ERR "btrfs: no missing devices found to "
1326 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001327 goto out;
1328 }
Chris Masondfe25022008-05-13 13:46:40 -04001329 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001330 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1331 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001332 if (IS_ERR(bdev)) {
1333 ret = PTR_ERR(bdev);
1334 goto out;
1335 }
1336
Yan Zheng2b820322008-11-17 21:11:30 -05001337 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001338 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001339 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001340 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001341 goto error_close;
1342 }
1343 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001344 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001345 dev_uuid = disk_super->dev_item.uuid;
1346 device = btrfs_find_device(root, devid, dev_uuid,
1347 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001348 if (!device) {
1349 ret = -ENOENT;
1350 goto error_brelse;
1351 }
Chris Masondfe25022008-05-13 13:46:40 -04001352 }
Yan Zheng2b820322008-11-17 21:11:30 -05001353
1354 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001355 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1356 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001357 ret = -EINVAL;
1358 goto error_brelse;
1359 }
1360
1361 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001362 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001363 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001364 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001365 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001366 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001367 }
Chris Masona061fc82008-05-07 11:43:44 -04001368
1369 ret = btrfs_shrink_device(device, 0);
1370 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001371 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001372
Chris Masona061fc82008-05-07 11:43:44 -04001373 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1374 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001375 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001376
Josef Bacik2bf64752011-09-26 17:12:22 -04001377 spin_lock(&root->fs_info->free_chunk_lock);
1378 root->fs_info->free_chunk_space = device->total_bytes -
1379 device->bytes_used;
1380 spin_unlock(&root->fs_info->free_chunk_lock);
1381
Yan Zheng2b820322008-11-17 21:11:30 -05001382 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001383 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001384
1385 /*
1386 * the device list mutex makes sure that we don't change
1387 * the device list while someone else is writing out all
1388 * the device supers.
1389 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001390
1391 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001392 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001393 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001394
Yan Zhenge4404d62008-12-12 10:03:26 -05001395 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001396
Chris Masoncd02dca2010-12-13 14:56:23 -05001397 if (device->missing)
1398 root->fs_info->fs_devices->missing_devices--;
1399
Yan Zheng2b820322008-11-17 21:11:30 -05001400 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1401 struct btrfs_device, dev_list);
1402 if (device->bdev == root->fs_info->sb->s_bdev)
1403 root->fs_info->sb->s_bdev = next_device->bdev;
1404 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1405 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1406
Xiao Guangrong1f781602011-04-20 10:09:16 +00001407 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001408 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409
1410 call_rcu(&device->rcu, free_device);
1411 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001412
David Sterba6c417612011-04-13 15:41:04 +02001413 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1414 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001415
Xiao Guangrong1f781602011-04-20 10:09:16 +00001416 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001417 struct btrfs_fs_devices *fs_devices;
1418 fs_devices = root->fs_info->fs_devices;
1419 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001420 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001421 break;
1422 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001423 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001424 fs_devices->seed = cur_devices->seed;
1425 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001426 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001427 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001428 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001429 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001430 }
1431
1432 /*
1433 * at this point, the device is zero sized. We want to
1434 * remove it from the devices list and zero out the old super
1435 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001437 /* make sure this device isn't detected as part of
1438 * the FS anymore
1439 */
1440 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1441 set_buffer_dirty(bh);
1442 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001443 }
Chris Masona061fc82008-05-07 11:43:44 -04001444
Chris Masona061fc82008-05-07 11:43:44 -04001445 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001446
1447error_brelse:
1448 brelse(bh);
1449error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001450 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001451 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001452out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001453 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001454 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 */
Li Zefan125ccb02011-12-08 15:07:24 +08001470static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001471{
1472 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1473 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001474 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001475 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001476 struct btrfs_device *device;
1477 u64 super_flags;
1478
1479 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001480 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001481 return -EINVAL;
1482
Yan Zhenge4404d62008-12-12 10:03:26 -05001483 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1484 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001485 return -ENOMEM;
1486
Yan Zhenge4404d62008-12-12 10:03:26 -05001487 old_devices = clone_fs_devices(fs_devices);
1488 if (IS_ERR(old_devices)) {
1489 kfree(seed_devices);
1490 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001491 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001492
Yan Zheng2b820322008-11-17 21:11:30 -05001493 list_add(&old_devices->list, &fs_uuids);
1494
Yan Zhenge4404d62008-12-12 10:03:26 -05001495 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1496 seed_devices->opened = 1;
1497 INIT_LIST_HEAD(&seed_devices->devices);
1498 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001499 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001500
1501 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001502 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1503 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001504 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1505
Yan Zhenge4404d62008-12-12 10:03:26 -05001506 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1507 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1508 device->fs_devices = seed_devices;
1509 }
1510
Yan Zheng2b820322008-11-17 21:11:30 -05001511 fs_devices->seeding = 0;
1512 fs_devices->num_devices = 0;
1513 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001514 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001515
1516 generate_random_uuid(fs_devices->fsid);
1517 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1518 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1519 super_flags = btrfs_super_flags(disk_super) &
1520 ~BTRFS_SUPER_FLAG_SEEDING;
1521 btrfs_set_super_flags(disk_super, super_flags);
1522
1523 return 0;
1524}
1525
1526/*
1527 * strore the expected generation for seed devices in device items.
1528 */
1529static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1530 struct btrfs_root *root)
1531{
1532 struct btrfs_path *path;
1533 struct extent_buffer *leaf;
1534 struct btrfs_dev_item *dev_item;
1535 struct btrfs_device *device;
1536 struct btrfs_key key;
1537 u8 fs_uuid[BTRFS_UUID_SIZE];
1538 u8 dev_uuid[BTRFS_UUID_SIZE];
1539 u64 devid;
1540 int ret;
1541
1542 path = btrfs_alloc_path();
1543 if (!path)
1544 return -ENOMEM;
1545
1546 root = root->fs_info->chunk_root;
1547 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1548 key.offset = 0;
1549 key.type = BTRFS_DEV_ITEM_KEY;
1550
1551 while (1) {
1552 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1553 if (ret < 0)
1554 goto error;
1555
1556 leaf = path->nodes[0];
1557next_slot:
1558 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1559 ret = btrfs_next_leaf(root, path);
1560 if (ret > 0)
1561 break;
1562 if (ret < 0)
1563 goto error;
1564 leaf = path->nodes[0];
1565 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001566 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001567 continue;
1568 }
1569
1570 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1571 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1572 key.type != BTRFS_DEV_ITEM_KEY)
1573 break;
1574
1575 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1576 struct btrfs_dev_item);
1577 devid = btrfs_device_id(leaf, dev_item);
1578 read_extent_buffer(leaf, dev_uuid,
1579 (unsigned long)btrfs_device_uuid(dev_item),
1580 BTRFS_UUID_SIZE);
1581 read_extent_buffer(leaf, fs_uuid,
1582 (unsigned long)btrfs_device_fsid(dev_item),
1583 BTRFS_UUID_SIZE);
1584 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1585 BUG_ON(!device);
1586
1587 if (device->fs_devices->seeding) {
1588 btrfs_set_device_generation(leaf, dev_item,
1589 device->generation);
1590 btrfs_mark_buffer_dirty(leaf);
1591 }
1592
1593 path->slots[0]++;
1594 goto next_slot;
1595 }
1596 ret = 0;
1597error:
1598 btrfs_free_path(path);
1599 return ret;
1600}
1601
Chris Mason788f20e2008-04-28 15:29:42 -04001602int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1603{
Josef Bacikd5e20032011-08-04 14:52:27 +00001604 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001605 struct btrfs_trans_handle *trans;
1606 struct btrfs_device *device;
1607 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001608 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001609 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001610 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001611 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001612 int ret = 0;
1613
Yan Zheng2b820322008-11-17 21:11:30 -05001614 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1615 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001616
Li Zefana5d16332011-12-07 20:08:40 -05001617 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001618 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001619 if (IS_ERR(bdev))
1620 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001621
Yan Zheng2b820322008-11-17 21:11:30 -05001622 if (root->fs_info->fs_devices->seeding) {
1623 seeding_dev = 1;
1624 down_write(&sb->s_umount);
1625 mutex_lock(&uuid_mutex);
1626 }
1627
Chris Mason8c8bee12008-09-29 11:19:10 -04001628 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001629 mutex_lock(&root->fs_info->volume_mutex);
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;
Li Zefan125ccb02011-12-08 15:07:24 +08001695 ret = btrfs_prepare_sprout(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001696 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 }
1757out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001758 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001759 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001760error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001761 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001762 if (seeding_dev) {
1763 mutex_unlock(&uuid_mutex);
1764 up_write(&sb->s_umount);
1765 }
Chris Mason788f20e2008-04-28 15:29:42 -04001766 goto out;
1767}
1768
Chris Masond3977122009-01-05 21:25:51 -05001769static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1770 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001771{
1772 int ret;
1773 struct btrfs_path *path;
1774 struct btrfs_root *root;
1775 struct btrfs_dev_item *dev_item;
1776 struct extent_buffer *leaf;
1777 struct btrfs_key key;
1778
1779 root = device->dev_root->fs_info->chunk_root;
1780
1781 path = btrfs_alloc_path();
1782 if (!path)
1783 return -ENOMEM;
1784
1785 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1786 key.type = BTRFS_DEV_ITEM_KEY;
1787 key.offset = device->devid;
1788
1789 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1790 if (ret < 0)
1791 goto out;
1792
1793 if (ret > 0) {
1794 ret = -ENOENT;
1795 goto out;
1796 }
1797
1798 leaf = path->nodes[0];
1799 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1800
1801 btrfs_set_device_id(leaf, dev_item, device->devid);
1802 btrfs_set_device_type(leaf, dev_item, device->type);
1803 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1804 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1805 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001806 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001807 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1808 btrfs_mark_buffer_dirty(leaf);
1809
1810out:
1811 btrfs_free_path(path);
1812 return ret;
1813}
1814
Chris Mason7d9eb122008-07-08 14:19:17 -04001815static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001816 struct btrfs_device *device, u64 new_size)
1817{
1818 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001819 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001820 u64 old_total = btrfs_super_total_bytes(super_copy);
1821 u64 diff = new_size - device->total_bytes;
1822
Yan Zheng2b820322008-11-17 21:11:30 -05001823 if (!device->writeable)
1824 return -EACCES;
1825 if (new_size <= device->total_bytes)
1826 return -EINVAL;
1827
Chris Mason8f18cf12008-04-25 16:53:30 -04001828 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001829 device->fs_devices->total_rw_bytes += diff;
1830
1831 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001832 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001833 btrfs_clear_space_info_full(device->dev_root->fs_info);
1834
Chris Mason8f18cf12008-04-25 16:53:30 -04001835 return btrfs_update_device(trans, device);
1836}
1837
Chris Mason7d9eb122008-07-08 14:19:17 -04001838int btrfs_grow_device(struct btrfs_trans_handle *trans,
1839 struct btrfs_device *device, u64 new_size)
1840{
1841 int ret;
1842 lock_chunks(device->dev_root);
1843 ret = __btrfs_grow_device(trans, device, new_size);
1844 unlock_chunks(device->dev_root);
1845 return ret;
1846}
1847
Chris Mason8f18cf12008-04-25 16:53:30 -04001848static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1849 struct btrfs_root *root,
1850 u64 chunk_tree, u64 chunk_objectid,
1851 u64 chunk_offset)
1852{
1853 int ret;
1854 struct btrfs_path *path;
1855 struct btrfs_key key;
1856
1857 root = root->fs_info->chunk_root;
1858 path = btrfs_alloc_path();
1859 if (!path)
1860 return -ENOMEM;
1861
1862 key.objectid = chunk_objectid;
1863 key.offset = chunk_offset;
1864 key.type = BTRFS_CHUNK_ITEM_KEY;
1865
1866 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1867 BUG_ON(ret);
1868
1869 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001870
1871 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001872 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001873}
1874
Christoph Hellwigb2950862008-12-02 09:54:17 -05001875static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001876 chunk_offset)
1877{
David Sterba6c417612011-04-13 15:41:04 +02001878 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001879 struct btrfs_disk_key *disk_key;
1880 struct btrfs_chunk *chunk;
1881 u8 *ptr;
1882 int ret = 0;
1883 u32 num_stripes;
1884 u32 array_size;
1885 u32 len = 0;
1886 u32 cur;
1887 struct btrfs_key key;
1888
1889 array_size = btrfs_super_sys_array_size(super_copy);
1890
1891 ptr = super_copy->sys_chunk_array;
1892 cur = 0;
1893
1894 while (cur < array_size) {
1895 disk_key = (struct btrfs_disk_key *)ptr;
1896 btrfs_disk_key_to_cpu(&key, disk_key);
1897
1898 len = sizeof(*disk_key);
1899
1900 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1901 chunk = (struct btrfs_chunk *)(ptr + len);
1902 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1903 len += btrfs_chunk_item_size(num_stripes);
1904 } else {
1905 ret = -EIO;
1906 break;
1907 }
1908 if (key.objectid == chunk_objectid &&
1909 key.offset == chunk_offset) {
1910 memmove(ptr, ptr + len, array_size - (cur + len));
1911 array_size -= len;
1912 btrfs_set_super_sys_array_size(super_copy, array_size);
1913 } else {
1914 ptr += len;
1915 cur += len;
1916 }
1917 }
1918 return ret;
1919}
1920
Christoph Hellwigb2950862008-12-02 09:54:17 -05001921static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001922 u64 chunk_tree, u64 chunk_objectid,
1923 u64 chunk_offset)
1924{
1925 struct extent_map_tree *em_tree;
1926 struct btrfs_root *extent_root;
1927 struct btrfs_trans_handle *trans;
1928 struct extent_map *em;
1929 struct map_lookup *map;
1930 int ret;
1931 int i;
1932
1933 root = root->fs_info->chunk_root;
1934 extent_root = root->fs_info->extent_root;
1935 em_tree = &root->fs_info->mapping_tree.map_tree;
1936
Josef Bacikba1bf482009-09-11 16:11:19 -04001937 ret = btrfs_can_relocate(extent_root, chunk_offset);
1938 if (ret)
1939 return -ENOSPC;
1940
Chris Mason8f18cf12008-04-25 16:53:30 -04001941 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001942 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001943 if (ret)
1944 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001945
Yan, Zhenga22285a2010-05-16 10:48:46 -04001946 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001947 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001948
Chris Mason7d9eb122008-07-08 14:19:17 -04001949 lock_chunks(root);
1950
Chris Mason8f18cf12008-04-25 16:53:30 -04001951 /*
1952 * step two, delete the device extents and the
1953 * chunk tree entries
1954 */
Chris Mason890871b2009-09-02 16:24:52 -04001955 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001956 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001957 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001958
Chris Masona061fc82008-05-07 11:43:44 -04001959 BUG_ON(em->start > chunk_offset ||
1960 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001961 map = (struct map_lookup *)em->bdev;
1962
1963 for (i = 0; i < map->num_stripes; i++) {
1964 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1965 map->stripes[i].physical);
1966 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001967
Chris Masondfe25022008-05-13 13:46:40 -04001968 if (map->stripes[i].dev) {
1969 ret = btrfs_update_device(trans, map->stripes[i].dev);
1970 BUG_ON(ret);
1971 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001972 }
1973 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1974 chunk_offset);
1975
1976 BUG_ON(ret);
1977
liubo1abe9b82011-03-24 11:18:59 +00001978 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1979
Chris Mason8f18cf12008-04-25 16:53:30 -04001980 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1981 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1982 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001983 }
1984
Zheng Yan1a40e232008-09-26 10:09:34 -04001985 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1986 BUG_ON(ret);
1987
Chris Mason890871b2009-09-02 16:24:52 -04001988 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001990 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001991
Chris Mason8f18cf12008-04-25 16:53:30 -04001992 kfree(map);
1993 em->bdev = NULL;
1994
1995 /* once for the tree */
1996 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001997 /* once for us */
1998 free_extent_map(em);
1999
Chris Mason7d9eb122008-07-08 14:19:17 -04002000 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002001 btrfs_end_transaction(trans, root);
2002 return 0;
2003}
2004
Yan Zheng2b820322008-11-17 21:11:30 -05002005static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2006{
2007 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2008 struct btrfs_path *path;
2009 struct extent_buffer *leaf;
2010 struct btrfs_chunk *chunk;
2011 struct btrfs_key key;
2012 struct btrfs_key found_key;
2013 u64 chunk_tree = chunk_root->root_key.objectid;
2014 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002015 bool retried = false;
2016 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002017 int ret;
2018
2019 path = btrfs_alloc_path();
2020 if (!path)
2021 return -ENOMEM;
2022
Josef Bacikba1bf482009-09-11 16:11:19 -04002023again:
Yan Zheng2b820322008-11-17 21:11:30 -05002024 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2025 key.offset = (u64)-1;
2026 key.type = BTRFS_CHUNK_ITEM_KEY;
2027
2028 while (1) {
2029 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2030 if (ret < 0)
2031 goto error;
2032 BUG_ON(ret == 0);
2033
2034 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2035 key.type);
2036 if (ret < 0)
2037 goto error;
2038 if (ret > 0)
2039 break;
2040
2041 leaf = path->nodes[0];
2042 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2043
2044 chunk = btrfs_item_ptr(leaf, path->slots[0],
2045 struct btrfs_chunk);
2046 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002047 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002048
2049 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2050 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2051 found_key.objectid,
2052 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002053 if (ret == -ENOSPC)
2054 failed++;
2055 else if (ret)
2056 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002057 }
2058
2059 if (found_key.offset == 0)
2060 break;
2061 key.offset = found_key.offset - 1;
2062 }
2063 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002064 if (failed && !retried) {
2065 failed = 0;
2066 retried = true;
2067 goto again;
2068 } else if (failed && retried) {
2069 WARN_ON(1);
2070 ret = -ENOSPC;
2071 }
Yan Zheng2b820322008-11-17 21:11:30 -05002072error:
2073 btrfs_free_path(path);
2074 return ret;
2075}
2076
Chris Masonec44a352008-04-28 15:29:52 -04002077static u64 div_factor(u64 num, int factor)
2078{
2079 if (factor == 10)
2080 return num;
2081 num *= factor;
2082 do_div(num, 10);
2083 return num;
2084}
2085
Chris Masonec44a352008-04-28 15:29:52 -04002086int btrfs_balance(struct btrfs_root *dev_root)
2087{
2088 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002089 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2090 struct btrfs_device *device;
2091 u64 old_size;
2092 u64 size_to_free;
2093 struct btrfs_path *path;
2094 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002095 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2096 struct btrfs_trans_handle *trans;
2097 struct btrfs_key found_key;
2098
Yan Zheng2b820322008-11-17 21:11:30 -05002099 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2100 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002101
Ben Hutchings6f88a442010-12-29 14:55:03 +00002102 if (!capable(CAP_SYS_ADMIN))
2103 return -EPERM;
2104
Chris Mason7d9eb122008-07-08 14:19:17 -04002105 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002106 dev_root = dev_root->fs_info->dev_root;
2107
Chris Masonec44a352008-04-28 15:29:52 -04002108 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002109 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002110 old_size = device->total_bytes;
2111 size_to_free = div_factor(old_size, 1);
2112 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002113 if (!device->writeable ||
2114 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002115 continue;
2116
2117 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002118 if (ret == -ENOSPC)
2119 break;
Chris Masonec44a352008-04-28 15:29:52 -04002120 BUG_ON(ret);
2121
Yan, Zhenga22285a2010-05-16 10:48:46 -04002122 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002123 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002124
2125 ret = btrfs_grow_device(trans, device, old_size);
2126 BUG_ON(ret);
2127
2128 btrfs_end_transaction(trans, dev_root);
2129 }
2130
2131 /* step two, relocate all the chunks */
2132 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002133 if (!path) {
2134 ret = -ENOMEM;
2135 goto error;
2136 }
Chris Masonec44a352008-04-28 15:29:52 -04002137 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2138 key.offset = (u64)-1;
2139 key.type = BTRFS_CHUNK_ITEM_KEY;
2140
Chris Masond3977122009-01-05 21:25:51 -05002141 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002142 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2143 if (ret < 0)
2144 goto error;
2145
2146 /*
2147 * this shouldn't happen, it means the last relocate
2148 * failed
2149 */
2150 if (ret == 0)
2151 break;
2152
2153 ret = btrfs_previous_item(chunk_root, path, 0,
2154 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002155 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002156 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002157
Chris Masonec44a352008-04-28 15:29:52 -04002158 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2159 path->slots[0]);
2160 if (found_key.objectid != key.objectid)
2161 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002162
Chris Masonec44a352008-04-28 15:29:52 -04002163 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002164 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002165 break;
2166
David Sterbab3b4aa72011-04-21 01:20:15 +02002167 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002168 ret = btrfs_relocate_chunk(chunk_root,
2169 chunk_root->root_key.objectid,
2170 found_key.objectid,
2171 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002172 if (ret && ret != -ENOSPC)
2173 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002174 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002175 }
2176 ret = 0;
2177error:
2178 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002179 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002180 return ret;
2181}
2182
Chris Mason8f18cf12008-04-25 16:53:30 -04002183/*
2184 * shrinking a device means finding all of the device extents past
2185 * the new size, and then following the back refs to the chunks.
2186 * The chunk relocation code actually frees the device extent
2187 */
2188int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2189{
2190 struct btrfs_trans_handle *trans;
2191 struct btrfs_root *root = device->dev_root;
2192 struct btrfs_dev_extent *dev_extent = NULL;
2193 struct btrfs_path *path;
2194 u64 length;
2195 u64 chunk_tree;
2196 u64 chunk_objectid;
2197 u64 chunk_offset;
2198 int ret;
2199 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002200 int failed = 0;
2201 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002202 struct extent_buffer *l;
2203 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002204 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002205 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002206 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002207 u64 diff = device->total_bytes - new_size;
2208
Yan Zheng2b820322008-11-17 21:11:30 -05002209 if (new_size >= device->total_bytes)
2210 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002211
2212 path = btrfs_alloc_path();
2213 if (!path)
2214 return -ENOMEM;
2215
Chris Mason8f18cf12008-04-25 16:53:30 -04002216 path->reada = 2;
2217
Chris Mason7d9eb122008-07-08 14:19:17 -04002218 lock_chunks(root);
2219
Chris Mason8f18cf12008-04-25 16:53:30 -04002220 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002221 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002222 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002223 spin_lock(&root->fs_info->free_chunk_lock);
2224 root->fs_info->free_chunk_space -= diff;
2225 spin_unlock(&root->fs_info->free_chunk_lock);
2226 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002227 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002228
Josef Bacikba1bf482009-09-11 16:11:19 -04002229again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002230 key.objectid = device->devid;
2231 key.offset = (u64)-1;
2232 key.type = BTRFS_DEV_EXTENT_KEY;
2233
2234 while (1) {
2235 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2236 if (ret < 0)
2237 goto done;
2238
2239 ret = btrfs_previous_item(root, path, 0, key.type);
2240 if (ret < 0)
2241 goto done;
2242 if (ret) {
2243 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002244 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002245 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002246 }
2247
2248 l = path->nodes[0];
2249 slot = path->slots[0];
2250 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2251
Josef Bacikba1bf482009-09-11 16:11:19 -04002252 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002253 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002254 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002255 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002256
2257 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2258 length = btrfs_dev_extent_length(l, dev_extent);
2259
Josef Bacikba1bf482009-09-11 16:11:19 -04002260 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002261 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002262 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002263 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002264
2265 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2266 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2267 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002268 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002269
2270 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2271 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002272 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002273 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002274 if (ret == -ENOSPC)
2275 failed++;
2276 key.offset -= 1;
2277 }
2278
2279 if (failed && !retried) {
2280 failed = 0;
2281 retried = true;
2282 goto again;
2283 } else if (failed && retried) {
2284 ret = -ENOSPC;
2285 lock_chunks(root);
2286
2287 device->total_bytes = old_size;
2288 if (device->writeable)
2289 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002290 spin_lock(&root->fs_info->free_chunk_lock);
2291 root->fs_info->free_chunk_space += diff;
2292 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04002293 unlock_chunks(root);
2294 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002295 }
2296
Chris Balld6397ba2009-04-27 07:29:03 -04002297 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002298 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002299 if (IS_ERR(trans)) {
2300 ret = PTR_ERR(trans);
2301 goto done;
2302 }
2303
Chris Balld6397ba2009-04-27 07:29:03 -04002304 lock_chunks(root);
2305
2306 device->disk_total_bytes = new_size;
2307 /* Now btrfs_update_device() will change the on-disk size. */
2308 ret = btrfs_update_device(trans, device);
2309 if (ret) {
2310 unlock_chunks(root);
2311 btrfs_end_transaction(trans, root);
2312 goto done;
2313 }
2314 WARN_ON(diff > old_total);
2315 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2316 unlock_chunks(root);
2317 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002318done:
2319 btrfs_free_path(path);
2320 return ret;
2321}
2322
Li Zefan125ccb02011-12-08 15:07:24 +08002323static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04002324 struct btrfs_key *key,
2325 struct btrfs_chunk *chunk, int item_size)
2326{
David Sterba6c417612011-04-13 15:41:04 +02002327 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04002328 struct btrfs_disk_key disk_key;
2329 u32 array_size;
2330 u8 *ptr;
2331
2332 array_size = btrfs_super_sys_array_size(super_copy);
2333 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2334 return -EFBIG;
2335
2336 ptr = super_copy->sys_chunk_array + array_size;
2337 btrfs_cpu_key_to_disk(&disk_key, key);
2338 memcpy(ptr, &disk_key, sizeof(disk_key));
2339 ptr += sizeof(disk_key);
2340 memcpy(ptr, chunk, item_size);
2341 item_size += sizeof(disk_key);
2342 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2343 return 0;
2344}
2345
Miao Xieb2117a32011-01-05 10:07:28 +00002346/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002347 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002348 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002349static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002350{
Arne Jansen73c5de02011-04-12 12:07:57 +02002351 const struct btrfs_device_info *di_a = a;
2352 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002353
Arne Jansen73c5de02011-04-12 12:07:57 +02002354 if (di_a->max_avail > di_b->max_avail)
2355 return -1;
2356 if (di_a->max_avail < di_b->max_avail)
2357 return 1;
2358 if (di_a->total_avail > di_b->total_avail)
2359 return -1;
2360 if (di_a->total_avail < di_b->total_avail)
2361 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002362 return 0;
2363}
2364
2365static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2366 struct btrfs_root *extent_root,
2367 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002368 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002369 u64 start, u64 type)
2370{
2371 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002372 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2373 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002374 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002375 struct extent_map_tree *em_tree;
2376 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002377 struct btrfs_device_info *devices_info = NULL;
2378 u64 total_avail;
2379 int num_stripes; /* total number of stripes to allocate */
2380 int sub_stripes; /* sub_stripes info for map */
2381 int dev_stripes; /* stripes per dev */
2382 int devs_max; /* max devs to use */
2383 int devs_min; /* min devs needed */
2384 int devs_increment; /* ndevs has to be a multiple of this */
2385 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002386 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002387 u64 max_stripe_size;
2388 u64 max_chunk_size;
2389 u64 stripe_size;
2390 u64 num_bytes;
2391 int ndevs;
2392 int i;
2393 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002394
2395 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2396 (type & BTRFS_BLOCK_GROUP_DUP)) {
2397 WARN_ON(1);
2398 type &= ~BTRFS_BLOCK_GROUP_DUP;
2399 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002400
Miao Xieb2117a32011-01-05 10:07:28 +00002401 if (list_empty(&fs_devices->alloc_list))
2402 return -ENOSPC;
2403
Arne Jansen73c5de02011-04-12 12:07:57 +02002404 sub_stripes = 1;
2405 dev_stripes = 1;
2406 devs_increment = 1;
2407 ncopies = 1;
2408 devs_max = 0; /* 0 == as many as possible */
2409 devs_min = 1;
2410
2411 /*
2412 * define the properties of each RAID type.
2413 * FIXME: move this to a global table and use it in all RAID
2414 * calculation code
2415 */
2416 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2417 dev_stripes = 2;
2418 ncopies = 2;
2419 devs_max = 1;
2420 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2421 devs_min = 2;
2422 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2423 devs_increment = 2;
2424 ncopies = 2;
2425 devs_max = 2;
2426 devs_min = 2;
2427 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2428 sub_stripes = 2;
2429 devs_increment = 2;
2430 ncopies = 2;
2431 devs_min = 4;
2432 } else {
2433 devs_max = 1;
2434 }
2435
2436 if (type & BTRFS_BLOCK_GROUP_DATA) {
2437 max_stripe_size = 1024 * 1024 * 1024;
2438 max_chunk_size = 10 * max_stripe_size;
2439 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2440 max_stripe_size = 256 * 1024 * 1024;
2441 max_chunk_size = max_stripe_size;
2442 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2443 max_stripe_size = 8 * 1024 * 1024;
2444 max_chunk_size = 2 * max_stripe_size;
2445 } else {
2446 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2447 type);
2448 BUG_ON(1);
2449 }
2450
2451 /* we don't want a chunk larger than 10% of writeable space */
2452 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2453 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002454
2455 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2456 GFP_NOFS);
2457 if (!devices_info)
2458 return -ENOMEM;
2459
Arne Jansen73c5de02011-04-12 12:07:57 +02002460 cur = fs_devices->alloc_list.next;
2461
2462 /*
2463 * in the first pass through the devices list, we gather information
2464 * about the available holes on each device.
2465 */
2466 ndevs = 0;
2467 while (cur != &fs_devices->alloc_list) {
2468 struct btrfs_device *device;
2469 u64 max_avail;
2470 u64 dev_offset;
2471
2472 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2473
2474 cur = cur->next;
2475
2476 if (!device->writeable) {
2477 printk(KERN_ERR
2478 "btrfs: read-only device in alloc_list\n");
2479 WARN_ON(1);
2480 continue;
2481 }
2482
2483 if (!device->in_fs_metadata)
2484 continue;
2485
2486 if (device->total_bytes > device->bytes_used)
2487 total_avail = device->total_bytes - device->bytes_used;
2488 else
2489 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00002490
2491 /* If there is no space on this device, skip it. */
2492 if (total_avail == 0)
2493 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02002494
Li Zefan125ccb02011-12-08 15:07:24 +08002495 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02002496 max_stripe_size * dev_stripes,
2497 &dev_offset, &max_avail);
2498 if (ret && ret != -ENOSPC)
2499 goto error;
2500
2501 if (ret == 0)
2502 max_avail = max_stripe_size * dev_stripes;
2503
2504 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2505 continue;
2506
2507 devices_info[ndevs].dev_offset = dev_offset;
2508 devices_info[ndevs].max_avail = max_avail;
2509 devices_info[ndevs].total_avail = total_avail;
2510 devices_info[ndevs].dev = device;
2511 ++ndevs;
2512 }
2513
2514 /*
2515 * now sort the devices by hole size / available space
2516 */
2517 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2518 btrfs_cmp_device_info, NULL);
2519
2520 /* round down to number of usable stripes */
2521 ndevs -= ndevs % devs_increment;
2522
2523 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2524 ret = -ENOSPC;
2525 goto error;
2526 }
2527
2528 if (devs_max && ndevs > devs_max)
2529 ndevs = devs_max;
2530 /*
2531 * the primary goal is to maximize the number of stripes, so use as many
2532 * devices as possible, even if the stripes are not maximum sized.
2533 */
2534 stripe_size = devices_info[ndevs-1].max_avail;
2535 num_stripes = ndevs * dev_stripes;
2536
2537 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2538 stripe_size = max_chunk_size * ncopies;
2539 do_div(stripe_size, num_stripes);
2540 }
2541
2542 do_div(stripe_size, dev_stripes);
2543 do_div(stripe_size, BTRFS_STRIPE_LEN);
2544 stripe_size *= BTRFS_STRIPE_LEN;
2545
Miao Xieb2117a32011-01-05 10:07:28 +00002546 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2547 if (!map) {
2548 ret = -ENOMEM;
2549 goto error;
2550 }
2551 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002552
Arne Jansen73c5de02011-04-12 12:07:57 +02002553 for (i = 0; i < ndevs; ++i) {
2554 for (j = 0; j < dev_stripes; ++j) {
2555 int s = i * dev_stripes + j;
2556 map->stripes[s].dev = devices_info[i].dev;
2557 map->stripes[s].physical = devices_info[i].dev_offset +
2558 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002559 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002560 }
Chris Mason593060d2008-03-25 16:50:33 -04002561 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002562 map->stripe_len = BTRFS_STRIPE_LEN;
2563 map->io_align = BTRFS_STRIPE_LEN;
2564 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002565 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002566 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002567
Yan Zheng2b820322008-11-17 21:11:30 -05002568 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002569 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002570
Arne Jansen73c5de02011-04-12 12:07:57 +02002571 *stripe_size_out = stripe_size;
2572 *num_bytes_out = num_bytes;
2573
2574 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002575
David Sterba172ddd62011-04-21 00:48:27 +02002576 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002577 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002578 ret = -ENOMEM;
2579 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002580 }
Chris Mason0b86a832008-03-24 15:01:56 -04002581 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002582 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002583 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002584 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002585 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002586
Chris Mason0b86a832008-03-24 15:01:56 -04002587 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002588 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002589 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002590 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002591 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002592 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002593
2594 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2595 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002596 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002597 BUG_ON(ret);
2598
Arne Jansen73c5de02011-04-12 12:07:57 +02002599 for (i = 0; i < map->num_stripes; ++i) {
2600 struct btrfs_device *device;
2601 u64 dev_offset;
2602
2603 device = map->stripes[i].dev;
2604 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002605
2606 ret = btrfs_alloc_dev_extent(trans, device,
2607 info->chunk_root->root_key.objectid,
2608 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002609 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002610 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002611 }
2612
Miao Xieb2117a32011-01-05 10:07:28 +00002613 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002614 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002615
2616error:
2617 kfree(map);
2618 kfree(devices_info);
2619 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002620}
2621
2622static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2623 struct btrfs_root *extent_root,
2624 struct map_lookup *map, u64 chunk_offset,
2625 u64 chunk_size, u64 stripe_size)
2626{
2627 u64 dev_offset;
2628 struct btrfs_key key;
2629 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2630 struct btrfs_device *device;
2631 struct btrfs_chunk *chunk;
2632 struct btrfs_stripe *stripe;
2633 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2634 int index = 0;
2635 int ret;
2636
2637 chunk = kzalloc(item_size, GFP_NOFS);
2638 if (!chunk)
2639 return -ENOMEM;
2640
2641 index = 0;
2642 while (index < map->num_stripes) {
2643 device = map->stripes[index].dev;
2644 device->bytes_used += stripe_size;
2645 ret = btrfs_update_device(trans, device);
2646 BUG_ON(ret);
2647 index++;
2648 }
2649
Josef Bacik2bf64752011-09-26 17:12:22 -04002650 spin_lock(&extent_root->fs_info->free_chunk_lock);
2651 extent_root->fs_info->free_chunk_space -= (stripe_size *
2652 map->num_stripes);
2653 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2654
Yan Zheng2b820322008-11-17 21:11:30 -05002655 index = 0;
2656 stripe = &chunk->stripe;
2657 while (index < map->num_stripes) {
2658 device = map->stripes[index].dev;
2659 dev_offset = map->stripes[index].physical;
2660
2661 btrfs_set_stack_stripe_devid(stripe, device->devid);
2662 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2663 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2664 stripe++;
2665 index++;
2666 }
2667
2668 btrfs_set_stack_chunk_length(chunk, chunk_size);
2669 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2670 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2671 btrfs_set_stack_chunk_type(chunk, map->type);
2672 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2673 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2674 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2675 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2676 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2677
2678 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2679 key.type = BTRFS_CHUNK_ITEM_KEY;
2680 key.offset = chunk_offset;
2681
2682 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2683 BUG_ON(ret);
2684
2685 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Li Zefan125ccb02011-12-08 15:07:24 +08002686 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05002687 item_size);
2688 BUG_ON(ret);
2689 }
liubo1abe9b82011-03-24 11:18:59 +00002690
Yan Zheng2b820322008-11-17 21:11:30 -05002691 kfree(chunk);
2692 return 0;
2693}
2694
2695/*
2696 * Chunk allocation falls into two parts. The first part does works
2697 * that make the new allocated chunk useable, but not do any operation
2698 * that modifies the chunk tree. The second part does the works that
2699 * require modifying the chunk tree. This division is important for the
2700 * bootstrap process of adding storage to a seed btrfs.
2701 */
2702int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2703 struct btrfs_root *extent_root, u64 type)
2704{
2705 u64 chunk_offset;
2706 u64 chunk_size;
2707 u64 stripe_size;
2708 struct map_lookup *map;
2709 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2710 int ret;
2711
2712 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2713 &chunk_offset);
2714 if (ret)
2715 return ret;
2716
2717 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2718 &stripe_size, chunk_offset, type);
2719 if (ret)
2720 return ret;
2721
2722 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2723 chunk_size, stripe_size);
2724 BUG_ON(ret);
2725 return 0;
2726}
2727
Chris Masond3977122009-01-05 21:25:51 -05002728static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002729 struct btrfs_root *root,
2730 struct btrfs_device *device)
2731{
2732 u64 chunk_offset;
2733 u64 sys_chunk_offset;
2734 u64 chunk_size;
2735 u64 sys_chunk_size;
2736 u64 stripe_size;
2737 u64 sys_stripe_size;
2738 u64 alloc_profile;
2739 struct map_lookup *map;
2740 struct map_lookup *sys_map;
2741 struct btrfs_fs_info *fs_info = root->fs_info;
2742 struct btrfs_root *extent_root = fs_info->extent_root;
2743 int ret;
2744
2745 ret = find_next_chunk(fs_info->chunk_root,
2746 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07002747 if (ret)
2748 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002749
2750 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2751 (fs_info->metadata_alloc_profile &
2752 fs_info->avail_metadata_alloc_bits);
2753 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2754
2755 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2756 &stripe_size, chunk_offset, alloc_profile);
2757 BUG_ON(ret);
2758
2759 sys_chunk_offset = chunk_offset + chunk_size;
2760
2761 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2762 (fs_info->system_alloc_profile &
2763 fs_info->avail_system_alloc_bits);
2764 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2765
2766 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2767 &sys_chunk_size, &sys_stripe_size,
2768 sys_chunk_offset, alloc_profile);
2769 BUG_ON(ret);
2770
2771 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2772 BUG_ON(ret);
2773
2774 /*
2775 * Modifying chunk tree needs allocating new blocks from both
2776 * system block group and metadata block group. So we only can
2777 * do operations require modifying the chunk tree after both
2778 * block groups were created.
2779 */
2780 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2781 chunk_size, stripe_size);
2782 BUG_ON(ret);
2783
2784 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2785 sys_chunk_offset, sys_chunk_size,
2786 sys_stripe_size);
2787 BUG_ON(ret);
2788 return 0;
2789}
2790
2791int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2792{
2793 struct extent_map *em;
2794 struct map_lookup *map;
2795 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2796 int readonly = 0;
2797 int i;
2798
Chris Mason890871b2009-09-02 16:24:52 -04002799 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002800 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002801 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002802 if (!em)
2803 return 1;
2804
Josef Bacikf48b9072010-01-27 02:07:59 +00002805 if (btrfs_test_opt(root, DEGRADED)) {
2806 free_extent_map(em);
2807 return 0;
2808 }
2809
Yan Zheng2b820322008-11-17 21:11:30 -05002810 map = (struct map_lookup *)em->bdev;
2811 for (i = 0; i < map->num_stripes; i++) {
2812 if (!map->stripes[i].dev->writeable) {
2813 readonly = 1;
2814 break;
2815 }
2816 }
2817 free_extent_map(em);
2818 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002819}
2820
2821void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2822{
David Sterbaa8067e02011-04-21 00:34:43 +02002823 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002824}
2825
2826void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2827{
2828 struct extent_map *em;
2829
Chris Masond3977122009-01-05 21:25:51 -05002830 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002831 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002832 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2833 if (em)
2834 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002835 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002836 if (!em)
2837 break;
2838 kfree(em->bdev);
2839 /* once for us */
2840 free_extent_map(em);
2841 /* once for the tree */
2842 free_extent_map(em);
2843 }
2844}
2845
Chris Masonf1885912008-04-09 16:28:12 -04002846int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2847{
2848 struct extent_map *em;
2849 struct map_lookup *map;
2850 struct extent_map_tree *em_tree = &map_tree->map_tree;
2851 int ret;
2852
Chris Mason890871b2009-09-02 16:24:52 -04002853 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002854 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002855 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002856 BUG_ON(!em);
2857
2858 BUG_ON(em->start > logical || em->start + em->len < logical);
2859 map = (struct map_lookup *)em->bdev;
2860 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2861 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002862 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2863 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002864 else
2865 ret = 1;
2866 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002867 return ret;
2868}
2869
Chris Masondfe25022008-05-13 13:46:40 -04002870static int find_live_mirror(struct map_lookup *map, int first, int num,
2871 int optimal)
2872{
2873 int i;
2874 if (map->stripes[optimal].dev->bdev)
2875 return optimal;
2876 for (i = first; i < first + num; i++) {
2877 if (map->stripes[i].dev->bdev)
2878 return i;
2879 }
2880 /* we couldn't find one that doesn't fail. Just return something
2881 * and the io error handling code will clean up eventually
2882 */
2883 return optimal;
2884}
2885
Chris Masonf2d8d742008-04-21 10:03:05 -04002886static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2887 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02002888 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002889 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002890{
2891 struct extent_map *em;
2892 struct map_lookup *map;
2893 struct extent_map_tree *em_tree = &map_tree->map_tree;
2894 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002895 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002896 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002897 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002898 u64 stripe_nr_orig;
2899 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002900 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002901 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002902 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002903 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002904 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002905 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002906 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002907
Jan Schmidta1d3c472011-08-04 17:15:33 +02002908 if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002909 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002910again:
Jan Schmidta1d3c472011-08-04 17:15:33 +02002911 if (bbio_ret) {
2912 bbio = kzalloc(btrfs_bio_size(stripes_allocated),
Chris Masoncea9e442008-04-09 16:28:12 -04002913 GFP_NOFS);
Jan Schmidta1d3c472011-08-04 17:15:33 +02002914 if (!bbio)
Chris Masoncea9e442008-04-09 16:28:12 -04002915 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002916
Jan Schmidta1d3c472011-08-04 17:15:33 +02002917 atomic_set(&bbio->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002918 }
Chris Mason0b86a832008-03-24 15:01:56 -04002919
Chris Mason890871b2009-09-02 16:24:52 -04002920 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002921 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002922 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002923
Chris Mason3b951512008-04-17 11:29:12 -04002924 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002925 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2926 (unsigned long long)logical,
2927 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002928 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002929 }
Chris Mason0b86a832008-03-24 15:01:56 -04002930
2931 BUG_ON(em->start > logical || em->start + em->len < logical);
2932 map = (struct map_lookup *)em->bdev;
2933 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002934
Chris Masonf1885912008-04-09 16:28:12 -04002935 if (mirror_num > map->num_stripes)
2936 mirror_num = 0;
2937
Jan Schmidta1d3c472011-08-04 17:15:33 +02002938 /* if our btrfs_bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002939 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002940 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2941 BTRFS_BLOCK_GROUP_DUP)) {
2942 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002943 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002944 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2945 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002946 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002947 }
2948 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002949 if (rw & REQ_DISCARD) {
2950 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2951 BTRFS_BLOCK_GROUP_RAID1 |
2952 BTRFS_BLOCK_GROUP_DUP |
2953 BTRFS_BLOCK_GROUP_RAID10)) {
2954 stripes_required = map->num_stripes;
2955 }
2956 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02002957 if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002958 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002959 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002960 free_extent_map(em);
Jan Schmidta1d3c472011-08-04 17:15:33 +02002961 kfree(bbio);
Chris Masoncea9e442008-04-09 16:28:12 -04002962 goto again;
2963 }
Chris Mason593060d2008-03-25 16:50:33 -04002964 stripe_nr = offset;
2965 /*
2966 * stripe_nr counts the total number of stripes we have to stride
2967 * to get to this block
2968 */
2969 do_div(stripe_nr, map->stripe_len);
2970
2971 stripe_offset = stripe_nr * map->stripe_len;
2972 BUG_ON(offset < stripe_offset);
2973
2974 /* stripe_offset is the offset of this block in its stripe*/
2975 stripe_offset = offset - stripe_offset;
2976
Li Dongyangfce3bb92011-03-24 10:24:26 +00002977 if (rw & REQ_DISCARD)
2978 *length = min_t(u64, em->len - offset, *length);
2979 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2980 BTRFS_BLOCK_GROUP_RAID1 |
2981 BTRFS_BLOCK_GROUP_RAID10 |
2982 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002983 /* we limit the length of each bio to what fits in a stripe */
2984 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002985 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002986 } else {
2987 *length = em->len - offset;
2988 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002989
Jan Schmidta1d3c472011-08-04 17:15:33 +02002990 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002991 goto out;
2992
Chris Masonf2d8d742008-04-21 10:03:05 -04002993 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002994 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002995 stripe_nr_orig = stripe_nr;
2996 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2997 (~(map->stripe_len - 1));
2998 do_div(stripe_nr_end, map->stripe_len);
2999 stripe_end_offset = stripe_nr_end * map->stripe_len -
3000 (offset + *length);
3001 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3002 if (rw & REQ_DISCARD)
3003 num_stripes = min_t(u64, map->num_stripes,
3004 stripe_nr_end - stripe_nr_orig);
3005 stripe_index = do_div(stripe_nr, map->num_stripes);
3006 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003007 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003008 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003009 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003010 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003011 else {
3012 stripe_index = find_live_mirror(map, 0,
3013 map->num_stripes,
3014 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003015 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003016 }
Chris Mason2fff7342008-04-29 14:12:09 -04003017
Chris Mason611f0e02008-04-03 16:29:03 -04003018 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003019 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003020 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003021 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003022 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003023 } else {
3024 mirror_num = 1;
3025 }
Chris Mason2fff7342008-04-29 14:12:09 -04003026
Chris Mason321aecc2008-04-16 10:49:51 -04003027 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3028 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003029
3030 stripe_index = do_div(stripe_nr, factor);
3031 stripe_index *= map->sub_stripes;
3032
Jens Axboe7eaceac2011-03-10 08:52:07 +01003033 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003034 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003035 else if (rw & REQ_DISCARD)
3036 num_stripes = min_t(u64, map->sub_stripes *
3037 (stripe_nr_end - stripe_nr_orig),
3038 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003039 else if (mirror_num)
3040 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003041 else {
3042 stripe_index = find_live_mirror(map, stripe_index,
3043 map->sub_stripes, stripe_index +
3044 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003045 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003046 }
Chris Mason8790d502008-04-03 16:29:03 -04003047 } else {
3048 /*
3049 * after this do_div call, stripe_nr is the number of stripes
3050 * on this device we have to walk to find the data, and
3051 * stripe_index is the number of our device in the stripe array
3052 */
3053 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003054 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003055 }
Chris Mason593060d2008-03-25 16:50:33 -04003056 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003057
Li Dongyangfce3bb92011-03-24 10:24:26 +00003058 if (rw & REQ_DISCARD) {
3059 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003060 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003061 map->stripes[stripe_index].physical +
3062 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003063 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003064
3065 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3066 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003067 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003068 int j;
3069
Chris Masond9d04872011-03-27 21:23:21 -04003070 div_u64_rem(stripe_nr_end - 1,
3071 map->num_stripes,
3072 &last_stripe);
3073
Li Dongyangfce3bb92011-03-24 10:24:26 +00003074 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003075 u32 test;
3076
3077 div_u64_rem(stripe_nr_end - 1 - j,
3078 map->num_stripes, &test);
3079 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003080 break;
3081 }
3082 stripes = stripe_nr_end - 1 - j;
3083 do_div(stripes, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003084 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003085 (stripes - stripe_nr + 1);
3086
3087 if (i == 0) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003088 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003089 stripe_offset;
3090 stripe_offset = 0;
3091 }
3092 if (stripe_index == last_stripe)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003093 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003094 stripe_end_offset;
3095 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3096 u64 stripes;
3097 int j;
3098 int factor = map->num_stripes /
3099 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003100 u32 last_stripe = 0;
3101
3102 div_u64_rem(stripe_nr_end - 1,
3103 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003104 last_stripe *= map->sub_stripes;
3105
3106 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003107 u32 test;
3108
3109 div_u64_rem(stripe_nr_end - 1 - j,
3110 factor, &test);
3111
3112 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003113 stripe_index / map->sub_stripes)
3114 break;
3115 }
3116 stripes = stripe_nr_end - 1 - j;
3117 do_div(stripes, factor);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003118 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003119 (stripes - stripe_nr + 1);
3120
3121 if (i < map->sub_stripes) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003122 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003123 stripe_offset;
3124 if (i == map->sub_stripes - 1)
3125 stripe_offset = 0;
3126 }
3127 if (stripe_index >= last_stripe &&
3128 stripe_index <= (last_stripe +
3129 map->sub_stripes - 1)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003130 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003131 stripe_end_offset;
3132 }
3133 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003134 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003135
3136 stripe_index++;
3137 if (stripe_index == map->num_stripes) {
3138 /* This could only happen for RAID0/10 */
3139 stripe_index = 0;
3140 stripe_nr++;
3141 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003142 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003143 } else {
3144 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003145 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003146 map->stripes[stripe_index].physical +
3147 stripe_offset +
3148 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003149 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003150 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003151 stripe_index++;
3152 }
Chris Mason593060d2008-03-25 16:50:33 -04003153 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003154 if (bbio_ret) {
3155 *bbio_ret = bbio;
3156 bbio->num_stripes = num_stripes;
3157 bbio->max_errors = max_errors;
3158 bbio->mirror_num = mirror_num;
Chris Masonf2d8d742008-04-21 10:03:05 -04003159 }
Chris Masoncea9e442008-04-09 16:28:12 -04003160out:
Chris Mason0b86a832008-03-24 15:01:56 -04003161 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003162 return 0;
3163}
3164
Chris Masonf2d8d742008-04-21 10:03:05 -04003165int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3166 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003167 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003168{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003169 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003170 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003171}
3172
Yan Zhenga512bbf2008-12-08 16:46:26 -05003173int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3174 u64 chunk_start, u64 physical, u64 devid,
3175 u64 **logical, int *naddrs, int *stripe_len)
3176{
3177 struct extent_map_tree *em_tree = &map_tree->map_tree;
3178 struct extent_map *em;
3179 struct map_lookup *map;
3180 u64 *buf;
3181 u64 bytenr;
3182 u64 length;
3183 u64 stripe_nr;
3184 int i, j, nr = 0;
3185
Chris Mason890871b2009-09-02 16:24:52 -04003186 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003187 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003188 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003189
3190 BUG_ON(!em || em->start != chunk_start);
3191 map = (struct map_lookup *)em->bdev;
3192
3193 length = em->len;
3194 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3195 do_div(length, map->num_stripes / map->sub_stripes);
3196 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3197 do_div(length, map->num_stripes);
3198
3199 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3200 BUG_ON(!buf);
3201
3202 for (i = 0; i < map->num_stripes; i++) {
3203 if (devid && map->stripes[i].dev->devid != devid)
3204 continue;
3205 if (map->stripes[i].physical > physical ||
3206 map->stripes[i].physical + length <= physical)
3207 continue;
3208
3209 stripe_nr = physical - map->stripes[i].physical;
3210 do_div(stripe_nr, map->stripe_len);
3211
3212 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3213 stripe_nr = stripe_nr * map->num_stripes + i;
3214 do_div(stripe_nr, map->sub_stripes);
3215 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3216 stripe_nr = stripe_nr * map->num_stripes + i;
3217 }
3218 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003219 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003220 for (j = 0; j < nr; j++) {
3221 if (buf[j] == bytenr)
3222 break;
3223 }
Chris Mason934d3752008-12-08 16:43:10 -05003224 if (j == nr) {
3225 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003226 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003227 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003228 }
3229
Yan Zhenga512bbf2008-12-08 16:46:26 -05003230 *logical = buf;
3231 *naddrs = nr;
3232 *stripe_len = map->stripe_len;
3233
3234 free_extent_map(em);
3235 return 0;
3236}
3237
Jan Schmidta1d3c472011-08-04 17:15:33 +02003238static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003239{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003240 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003241 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003242
Chris Mason8790d502008-04-03 16:29:03 -04003243 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003244 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003245
Jan Schmidta1d3c472011-08-04 17:15:33 +02003246 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003247 is_orig_bio = 1;
3248
Jan Schmidta1d3c472011-08-04 17:15:33 +02003249 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003250 if (!is_orig_bio) {
3251 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003252 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003253 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003254 bio->bi_private = bbio->private;
3255 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003256 bio->bi_bdev = (struct block_device *)
3257 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003258 /* only send an error to the higher layers if it is
3259 * beyond the tolerance of the multi-bio
3260 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003261 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003262 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003263 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003264 /*
3265 * this bio is actually up to date, we didn't
3266 * go over the max number of errors
3267 */
3268 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003269 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003270 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003271 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003272
3273 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003274 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003275 bio_put(bio);
3276 }
Chris Mason8790d502008-04-03 16:29:03 -04003277}
3278
Chris Mason8b712842008-06-11 16:50:36 -04003279struct async_sched {
3280 struct bio *bio;
3281 int rw;
3282 struct btrfs_fs_info *info;
3283 struct btrfs_work work;
3284};
3285
3286/*
3287 * see run_scheduled_bios for a description of why bios are collected for
3288 * async submit.
3289 *
3290 * This will add one bio to the pending list for a device and make sure
3291 * the work struct is scheduled.
3292 */
Chris Masond3977122009-01-05 21:25:51 -05003293static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003294 struct btrfs_device *device,
3295 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003296{
3297 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003298 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003299
3300 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003301 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003302 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003303 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003304 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003305 return 0;
3306 }
3307
3308 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003309 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003310 * higher layers. Otherwise, the async bio makes it appear we have
3311 * made progress against dirty pages when we've really just put it
3312 * on a queue for later
3313 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003314 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003315 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003316 bio->bi_next = NULL;
3317 bio->bi_rw |= rw;
3318
3319 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003320 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003321 pending_bios = &device->pending_sync_bios;
3322 else
3323 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003324
Chris Masonffbd5172009-04-20 15:50:09 -04003325 if (pending_bios->tail)
3326 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003327
Chris Masonffbd5172009-04-20 15:50:09 -04003328 pending_bios->tail = bio;
3329 if (!pending_bios->head)
3330 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003331 if (device->running_pending)
3332 should_queue = 0;
3333
3334 spin_unlock(&device->io_lock);
3335
3336 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003337 btrfs_queue_worker(&root->fs_info->submit_workers,
3338 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003339 return 0;
3340}
3341
Chris Masonf1885912008-04-09 16:28:12 -04003342int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003343 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003344{
3345 struct btrfs_mapping_tree *map_tree;
3346 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003347 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003348 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003349 u64 length = 0;
3350 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04003351 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003352 int dev_nr = 0;
3353 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003354 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003355
Chris Masonf2d8d742008-04-21 10:03:05 -04003356 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003357 map_tree = &root->fs_info->mapping_tree;
3358 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003359
Jan Schmidta1d3c472011-08-04 17:15:33 +02003360 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04003361 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003362 BUG_ON(ret);
3363
Jan Schmidta1d3c472011-08-04 17:15:33 +02003364 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003365 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003366 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3367 "len %llu\n", (unsigned long long)logical,
3368 (unsigned long long)length,
3369 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003370 BUG();
3371 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003372
3373 bbio->orig_bio = first_bio;
3374 bbio->private = first_bio->bi_private;
3375 bbio->end_io = first_bio->bi_end_io;
3376 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04003377
Chris Masond3977122009-01-05 21:25:51 -05003378 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003379 if (dev_nr < total_devs - 1) {
3380 bio = bio_clone(first_bio, GFP_NOFS);
3381 BUG_ON(!bio);
3382 } else {
3383 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04003384 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003385 bio->bi_private = bbio;
3386 bio->bi_end_io = btrfs_end_bio;
3387 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
3388 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003389 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003390 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3391 "(%s id %llu), size=%u\n", rw,
3392 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
3393 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04003394 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003395 if (async_submit)
3396 schedule_bio(root, dev, rw, bio);
3397 else
3398 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003399 } else {
3400 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3401 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003402 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003403 }
Chris Mason8790d502008-04-03 16:29:03 -04003404 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003405 }
Chris Mason0b86a832008-03-24 15:01:56 -04003406 return 0;
3407}
3408
Chris Masona4437552008-04-18 10:29:38 -04003409struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003410 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003411{
Yan Zheng2b820322008-11-17 21:11:30 -05003412 struct btrfs_device *device;
3413 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003414
Yan Zheng2b820322008-11-17 21:11:30 -05003415 cur_devices = root->fs_info->fs_devices;
3416 while (cur_devices) {
3417 if (!fsid ||
3418 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3419 device = __find_device(&cur_devices->devices,
3420 devid, uuid);
3421 if (device)
3422 return device;
3423 }
3424 cur_devices = cur_devices->seed;
3425 }
3426 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003427}
3428
Chris Masondfe25022008-05-13 13:46:40 -04003429static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3430 u64 devid, u8 *dev_uuid)
3431{
3432 struct btrfs_device *device;
3433 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3434
3435 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003436 if (!device)
3437 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003438 list_add(&device->dev_list,
3439 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003440 device->dev_root = root->fs_info->dev_root;
3441 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003442 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003443 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003444 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003445 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003446 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003447 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003448 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003449 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3450 return device;
3451}
3452
Chris Mason0b86a832008-03-24 15:01:56 -04003453static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3454 struct extent_buffer *leaf,
3455 struct btrfs_chunk *chunk)
3456{
3457 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3458 struct map_lookup *map;
3459 struct extent_map *em;
3460 u64 logical;
3461 u64 length;
3462 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003463 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003464 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003465 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003466 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003467
Chris Masone17cade2008-04-15 15:41:47 -04003468 logical = key->offset;
3469 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003470
Chris Mason890871b2009-09-02 16:24:52 -04003471 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003472 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003473 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003474
3475 /* already mapped? */
3476 if (em && em->start <= logical && em->start + em->len > logical) {
3477 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003478 return 0;
3479 } else if (em) {
3480 free_extent_map(em);
3481 }
Chris Mason0b86a832008-03-24 15:01:56 -04003482
David Sterba172ddd62011-04-21 00:48:27 +02003483 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003484 if (!em)
3485 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003486 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3487 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003488 if (!map) {
3489 free_extent_map(em);
3490 return -ENOMEM;
3491 }
3492
3493 em->bdev = (struct block_device *)map;
3494 em->start = logical;
3495 em->len = length;
3496 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003497 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003498
Chris Mason593060d2008-03-25 16:50:33 -04003499 map->num_stripes = num_stripes;
3500 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3501 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3502 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3503 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3504 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003505 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003506 for (i = 0; i < num_stripes; i++) {
3507 map->stripes[i].physical =
3508 btrfs_stripe_offset_nr(leaf, chunk, i);
3509 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003510 read_extent_buffer(leaf, uuid, (unsigned long)
3511 btrfs_stripe_dev_uuid_nr(chunk, i),
3512 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003513 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3514 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003515 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003516 kfree(map);
3517 free_extent_map(em);
3518 return -EIO;
3519 }
Chris Masondfe25022008-05-13 13:46:40 -04003520 if (!map->stripes[i].dev) {
3521 map->stripes[i].dev =
3522 add_missing_dev(root, devid, uuid);
3523 if (!map->stripes[i].dev) {
3524 kfree(map);
3525 free_extent_map(em);
3526 return -EIO;
3527 }
3528 }
3529 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003530 }
3531
Chris Mason890871b2009-09-02 16:24:52 -04003532 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003533 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003534 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003535 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003536 free_extent_map(em);
3537
3538 return 0;
3539}
3540
3541static int fill_device_from_item(struct extent_buffer *leaf,
3542 struct btrfs_dev_item *dev_item,
3543 struct btrfs_device *device)
3544{
3545 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003546
3547 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003548 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3549 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003550 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3551 device->type = btrfs_device_type(leaf, dev_item);
3552 device->io_align = btrfs_device_io_align(leaf, dev_item);
3553 device->io_width = btrfs_device_io_width(leaf, dev_item);
3554 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003555
3556 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003557 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003558
Chris Mason0b86a832008-03-24 15:01:56 -04003559 return 0;
3560}
3561
Yan Zheng2b820322008-11-17 21:11:30 -05003562static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3563{
3564 struct btrfs_fs_devices *fs_devices;
3565 int ret;
3566
3567 mutex_lock(&uuid_mutex);
3568
3569 fs_devices = root->fs_info->fs_devices->seed;
3570 while (fs_devices) {
3571 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3572 ret = 0;
3573 goto out;
3574 }
3575 fs_devices = fs_devices->seed;
3576 }
3577
3578 fs_devices = find_fsid(fsid);
3579 if (!fs_devices) {
3580 ret = -ENOENT;
3581 goto out;
3582 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003583
3584 fs_devices = clone_fs_devices(fs_devices);
3585 if (IS_ERR(fs_devices)) {
3586 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003587 goto out;
3588 }
3589
Christoph Hellwig97288f22008-12-02 06:36:09 -05003590 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003591 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003592 if (ret)
3593 goto out;
3594
3595 if (!fs_devices->seeding) {
3596 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003597 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003598 ret = -EINVAL;
3599 goto out;
3600 }
3601
3602 fs_devices->seed = root->fs_info->fs_devices->seed;
3603 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003604out:
3605 mutex_unlock(&uuid_mutex);
3606 return ret;
3607}
3608
Chris Mason0d81ba52008-03-24 15:02:07 -04003609static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003610 struct extent_buffer *leaf,
3611 struct btrfs_dev_item *dev_item)
3612{
3613 struct btrfs_device *device;
3614 u64 devid;
3615 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003616 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003617 u8 dev_uuid[BTRFS_UUID_SIZE];
3618
Chris Mason0b86a832008-03-24 15:01:56 -04003619 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003620 read_extent_buffer(leaf, dev_uuid,
3621 (unsigned long)btrfs_device_uuid(dev_item),
3622 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003623 read_extent_buffer(leaf, fs_uuid,
3624 (unsigned long)btrfs_device_fsid(dev_item),
3625 BTRFS_UUID_SIZE);
3626
3627 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3628 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003629 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003630 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003631 }
3632
3633 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3634 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003635 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003636 return -EIO;
3637
3638 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003639 printk(KERN_WARNING "warning devid %llu missing\n",
3640 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003641 device = add_missing_dev(root, devid, dev_uuid);
3642 if (!device)
3643 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003644 } else if (!device->missing) {
3645 /*
3646 * this happens when a device that was properly setup
3647 * in the device info lists suddenly goes bad.
3648 * device->bdev is NULL, and so we have to set
3649 * device->missing to one here
3650 */
3651 root->fs_info->fs_devices->missing_devices++;
3652 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003653 }
3654 }
3655
3656 if (device->fs_devices != root->fs_info->fs_devices) {
3657 BUG_ON(device->writeable);
3658 if (device->generation !=
3659 btrfs_device_generation(leaf, dev_item))
3660 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003661 }
Chris Mason0b86a832008-03-24 15:01:56 -04003662
3663 fill_device_from_item(leaf, dev_item, device);
3664 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003665 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04003666 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003667 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04003668 spin_lock(&root->fs_info->free_chunk_lock);
3669 root->fs_info->free_chunk_space += device->total_bytes -
3670 device->bytes_used;
3671 spin_unlock(&root->fs_info->free_chunk_lock);
3672 }
Chris Mason0b86a832008-03-24 15:01:56 -04003673 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003674 return ret;
3675}
3676
Yan Zhenge4404d62008-12-12 10:03:26 -05003677int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003678{
David Sterba6c417612011-04-13 15:41:04 +02003679 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003680 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003681 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003682 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003683 u8 *ptr;
3684 unsigned long sb_ptr;
3685 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003686 u32 num_stripes;
3687 u32 array_size;
3688 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003689 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003690 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003691
Yan Zhenge4404d62008-12-12 10:03:26 -05003692 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003693 BTRFS_SUPER_INFO_SIZE);
3694 if (!sb)
3695 return -ENOMEM;
3696 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04003697 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05003698
Chris Masona061fc82008-05-07 11:43:44 -04003699 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003700 array_size = btrfs_super_sys_array_size(super_copy);
3701
Chris Mason0b86a832008-03-24 15:01:56 -04003702 ptr = super_copy->sys_chunk_array;
3703 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3704 cur = 0;
3705
3706 while (cur < array_size) {
3707 disk_key = (struct btrfs_disk_key *)ptr;
3708 btrfs_disk_key_to_cpu(&key, disk_key);
3709
Chris Masona061fc82008-05-07 11:43:44 -04003710 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003711 sb_ptr += len;
3712 cur += len;
3713
Chris Mason0d81ba52008-03-24 15:02:07 -04003714 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003715 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003716 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003717 if (ret)
3718 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003719 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3720 len = btrfs_chunk_item_size(num_stripes);
3721 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003722 ret = -EIO;
3723 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003724 }
3725 ptr += len;
3726 sb_ptr += len;
3727 cur += len;
3728 }
Chris Masona061fc82008-05-07 11:43:44 -04003729 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003730 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003731}
3732
3733int btrfs_read_chunk_tree(struct btrfs_root *root)
3734{
3735 struct btrfs_path *path;
3736 struct extent_buffer *leaf;
3737 struct btrfs_key key;
3738 struct btrfs_key found_key;
3739 int ret;
3740 int slot;
3741
3742 root = root->fs_info->chunk_root;
3743
3744 path = btrfs_alloc_path();
3745 if (!path)
3746 return -ENOMEM;
3747
3748 /* first we search for all of the device items, and then we
3749 * read in all of the chunk items. This way we can create chunk
3750 * mappings that reference all of the devices that are afound
3751 */
3752 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3753 key.offset = 0;
3754 key.type = 0;
3755again:
3756 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003757 if (ret < 0)
3758 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003759 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003760 leaf = path->nodes[0];
3761 slot = path->slots[0];
3762 if (slot >= btrfs_header_nritems(leaf)) {
3763 ret = btrfs_next_leaf(root, path);
3764 if (ret == 0)
3765 continue;
3766 if (ret < 0)
3767 goto error;
3768 break;
3769 }
3770 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3771 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3772 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3773 break;
3774 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3775 struct btrfs_dev_item *dev_item;
3776 dev_item = btrfs_item_ptr(leaf, slot,
3777 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003778 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003779 if (ret)
3780 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003781 }
3782 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3783 struct btrfs_chunk *chunk;
3784 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3785 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003786 if (ret)
3787 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003788 }
3789 path->slots[0]++;
3790 }
3791 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3792 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003793 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003794 goto again;
3795 }
Chris Mason0b86a832008-03-24 15:01:56 -04003796 ret = 0;
3797error:
Yan Zheng2b820322008-11-17 21:11:30 -05003798 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003799 return ret;
3800}