blob: e138af710de2d0bcb738eea7380c46940dde9f0a [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 Mason492bb6d2008-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 }
298 }
Chris Masonffbd5172009-04-20 15:50:09 -0400299
Chris Mason51684082010-03-10 15:33:32 -0500300 cond_resched();
301 if (again)
302 goto loop;
303
304 spin_lock(&device->io_lock);
305 if (device->pending_bios.head || device->pending_sync_bios.head)
306 goto loop_lock;
307 spin_unlock(&device->io_lock);
308
Chris Mason8b712842008-06-11 16:50:36 -0400309done:
Chris Mason211588a2011-04-19 20:12:40 -0400310 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400311 return 0;
312}
313
Christoph Hellwigb2950862008-12-02 09:54:17 -0500314static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400315{
316 struct btrfs_device *device;
317
318 device = container_of(work, struct btrfs_device, work);
319 run_scheduled_bios(device);
320}
321
Chris Masona1b32a52008-09-05 16:09:51 -0400322static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400323 struct btrfs_super_block *disk_super,
324 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
325{
326 struct btrfs_device *device;
327 struct btrfs_fs_devices *fs_devices;
328 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000329 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330
331 fs_devices = find_fsid(disk_super->fsid);
332 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400333 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400334 if (!fs_devices)
335 return -ENOMEM;
336 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400337 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338 list_add(&fs_devices->list, &fs_uuids);
339 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
340 fs_devices->latest_devid = devid;
341 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400342 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 device = NULL;
344 } else {
Chris Masona4437552008-04-18 10:29:38 -0400345 device = __find_device(&fs_devices->devices, devid,
346 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400347 }
348 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500349 if (fs_devices->opened)
350 return -EBUSY;
351
Chris Mason8a4b83c2008-03-24 15:02:07 -0400352 device = kzalloc(sizeof(*device), GFP_NOFS);
353 if (!device) {
354 /* we can safely leave the fs_devices entry around */
355 return -ENOMEM;
356 }
357 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400358 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400359 memcpy(device->uuid, disk_super->dev_item.uuid,
360 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400361 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400362 device->name = kstrdup(path, GFP_NOFS);
363 if (!device->name) {
364 kfree(device);
365 return -ENOMEM;
366 }
Yan Zheng2b820322008-11-17 21:11:30 -0500367 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400368
369 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000370 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400371 mutex_unlock(&fs_devices->device_list_mutex);
372
Yan Zheng2b820322008-11-17 21:11:30 -0500373 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400374 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500375 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000376 name = kstrdup(path, GFP_NOFS);
377 if (!name)
378 return -ENOMEM;
379 kfree(device->name);
380 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500381 if (device->missing) {
382 fs_devices->missing_devices--;
383 device->missing = 0;
384 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400385 }
386
387 if (found_transid > fs_devices->latest_trans) {
388 fs_devices->latest_devid = devid;
389 fs_devices->latest_trans = found_transid;
390 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400391 *fs_devices_ret = fs_devices;
392 return 0;
393}
394
Yan Zhenge4404d62008-12-12 10:03:26 -0500395static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
396{
397 struct btrfs_fs_devices *fs_devices;
398 struct btrfs_device *device;
399 struct btrfs_device *orig_dev;
400
401 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
402 if (!fs_devices)
403 return ERR_PTR(-ENOMEM);
404
405 INIT_LIST_HEAD(&fs_devices->devices);
406 INIT_LIST_HEAD(&fs_devices->alloc_list);
407 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400408 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500409 fs_devices->latest_devid = orig->latest_devid;
410 fs_devices->latest_trans = orig->latest_trans;
411 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
412
Xiao Guangrong46224702011-04-20 10:08:47 +0000413 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500414 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
415 device = kzalloc(sizeof(*device), GFP_NOFS);
416 if (!device)
417 goto error;
418
419 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400420 if (!device->name) {
421 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500422 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400423 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500424
425 device->devid = orig_dev->devid;
426 device->work.func = pending_bios_fn;
427 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500428 spin_lock_init(&device->io_lock);
429 INIT_LIST_HEAD(&device->dev_list);
430 INIT_LIST_HEAD(&device->dev_alloc_list);
431
432 list_add(&device->dev_list, &fs_devices->devices);
433 device->fs_devices = fs_devices;
434 fs_devices->num_devices++;
435 }
436 return fs_devices;
437error:
438 free_fs_devices(fs_devices);
439 return ERR_PTR(-ENOMEM);
440}
441
Chris Masondfe25022008-05-13 13:46:40 -0400442int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
443{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500444 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400445
446 mutex_lock(&uuid_mutex);
447again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000448 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500449 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500450 if (device->in_fs_metadata)
451 continue;
452
453 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100454 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500455 device->bdev = NULL;
456 fs_devices->open_devices--;
457 }
458 if (device->writeable) {
459 list_del_init(&device->dev_alloc_list);
460 device->writeable = 0;
461 fs_devices->rw_devices--;
462 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500463 list_del_init(&device->dev_list);
464 fs_devices->num_devices--;
465 kfree(device->name);
466 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400467 }
Yan Zheng2b820322008-11-17 21:11:30 -0500468
469 if (fs_devices->seed) {
470 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500471 goto again;
472 }
473
Chris Masondfe25022008-05-13 13:46:40 -0400474 mutex_unlock(&uuid_mutex);
475 return 0;
476}
Chris Masona0af4692008-05-13 16:03:06 -0400477
Xiao Guangrong1f781602011-04-20 10:09:16 +0000478static void __free_device(struct work_struct *work)
479{
480 struct btrfs_device *device;
481
482 device = container_of(work, struct btrfs_device, rcu_work);
483
484 if (device->bdev)
485 blkdev_put(device->bdev, device->mode);
486
487 kfree(device->name);
488 kfree(device);
489}
490
491static void free_device(struct rcu_head *head)
492{
493 struct btrfs_device *device;
494
495 device = container_of(head, struct btrfs_device, rcu);
496
497 INIT_WORK(&device->rcu_work, __free_device);
498 schedule_work(&device->rcu_work);
499}
500
Yan Zheng2b820322008-11-17 21:11:30 -0500501static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400502{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400503 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500504
Yan Zheng2b820322008-11-17 21:11:30 -0500505 if (--fs_devices->opened > 0)
506 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400507
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000508 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500509 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000510 struct btrfs_device *new_device;
511
512 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400513 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000514
Yan Zheng2b820322008-11-17 21:11:30 -0500515 if (device->writeable) {
516 list_del_init(&device->dev_alloc_list);
517 fs_devices->rw_devices--;
518 }
519
Josef Bacikd5e20032011-08-04 14:52:27 +0000520 if (device->can_discard)
521 fs_devices->num_can_discard--;
522
Xiao Guangrong1f781602011-04-20 10:09:16 +0000523 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
524 BUG_ON(!new_device);
525 memcpy(new_device, device, sizeof(*new_device));
526 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000527 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000528 new_device->bdev = NULL;
529 new_device->writeable = 0;
530 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000531 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000532 list_replace_rcu(&device->dev_list, &new_device->dev_list);
533
534 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000536 mutex_unlock(&fs_devices->device_list_mutex);
537
Yan Zhenge4404d62008-12-12 10:03:26 -0500538 WARN_ON(fs_devices->open_devices);
539 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500540 fs_devices->opened = 0;
541 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500542
Chris Mason8a4b83c2008-03-24 15:02:07 -0400543 return 0;
544}
545
Yan Zheng2b820322008-11-17 21:11:30 -0500546int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
547{
Yan Zhenge4404d62008-12-12 10:03:26 -0500548 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500549 int ret;
550
551 mutex_lock(&uuid_mutex);
552 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500553 if (!fs_devices->opened) {
554 seed_devices = fs_devices->seed;
555 fs_devices->seed = NULL;
556 }
Yan Zheng2b820322008-11-17 21:11:30 -0500557 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500558
559 while (seed_devices) {
560 fs_devices = seed_devices;
561 seed_devices = fs_devices->seed;
562 __btrfs_close_devices(fs_devices);
563 free_fs_devices(fs_devices);
564 }
Yan Zheng2b820322008-11-17 21:11:30 -0500565 return ret;
566}
567
Yan Zhenge4404d62008-12-12 10:03:26 -0500568static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
569 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400570{
Josef Bacikd5e20032011-08-04 14:52:27 +0000571 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400572 struct block_device *bdev;
573 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400575 struct block_device *latest_bdev = NULL;
576 struct buffer_head *bh;
577 struct btrfs_super_block *disk_super;
578 u64 latest_devid = 0;
579 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400580 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500581 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400582 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583
Tejun Heod4d77622010-11-13 11:55:18 +0100584 flags |= FMODE_EXCL;
585
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500586 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400587 if (device->bdev)
588 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400589 if (!device->name)
590 continue;
591
Tejun Heod4d77622010-11-13 11:55:18 +0100592 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400593 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500594 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400595 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400596 }
Chris Masona061fc82008-05-07 11:43:44 -0400597 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400598
Yan Zhenga512bbf2008-12-08 16:46:26 -0500599 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000600 if (!bh) {
601 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400602 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000603 }
Chris Masona0af4692008-05-13 16:03:06 -0400604
605 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000606 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400607 if (devid != device->devid)
608 goto error_brelse;
609
Yan Zheng2b820322008-11-17 21:11:30 -0500610 if (memcmp(device->uuid, disk_super->dev_item.uuid,
611 BTRFS_UUID_SIZE))
612 goto error_brelse;
613
614 device->generation = btrfs_super_generation(disk_super);
615 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400616 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500617 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400618 latest_bdev = bdev;
619 }
620
Yan Zheng2b820322008-11-17 21:11:30 -0500621 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
622 device->writeable = 0;
623 } else {
624 device->writeable = !bdev_read_only(bdev);
625 seeding = 0;
626 }
627
Josef Bacikd5e20032011-08-04 14:52:27 +0000628 q = bdev_get_queue(bdev);
629 if (blk_queue_discard(q)) {
630 device->can_discard = 1;
631 fs_devices->num_can_discard++;
632 }
633
Chris Mason8a4b83c2008-03-24 15:02:07 -0400634 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400635 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500636 device->mode = flags;
637
Chris Masonc2898112009-06-10 09:51:32 -0400638 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
639 fs_devices->rotating = 1;
640
Chris Masona0af4692008-05-13 16:03:06 -0400641 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500642 if (device->writeable) {
643 fs_devices->rw_devices++;
644 list_add(&device->dev_alloc_list,
645 &fs_devices->alloc_list);
646 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000647 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400648 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400649
Chris Masona0af4692008-05-13 16:03:06 -0400650error_brelse:
651 brelse(bh);
652error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100653 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400654error:
655 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400656 }
Chris Masona0af4692008-05-13 16:03:06 -0400657 if (fs_devices->open_devices == 0) {
658 ret = -EIO;
659 goto out;
660 }
Yan Zheng2b820322008-11-17 21:11:30 -0500661 fs_devices->seeding = seeding;
662 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400663 fs_devices->latest_bdev = latest_bdev;
664 fs_devices->latest_devid = latest_devid;
665 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500666 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400667out:
Yan Zheng2b820322008-11-17 21:11:30 -0500668 return ret;
669}
670
671int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500672 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500673{
674 int ret;
675
676 mutex_lock(&uuid_mutex);
677 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500678 fs_devices->opened++;
679 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500680 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500681 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500682 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400683 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400684 return ret;
685}
686
Christoph Hellwig97288f22008-12-02 06:36:09 -0500687int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400688 struct btrfs_fs_devices **fs_devices_ret)
689{
690 struct btrfs_super_block *disk_super;
691 struct block_device *bdev;
692 struct buffer_head *bh;
693 int ret;
694 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400695 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696
697 mutex_lock(&uuid_mutex);
698
Tejun Heod4d77622010-11-13 11:55:18 +0100699 flags |= FMODE_EXCL;
700 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701
702 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703 ret = PTR_ERR(bdev);
704 goto error;
705 }
706
707 ret = set_blocksize(bdev, 4096);
708 if (ret)
709 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500710 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400711 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000712 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 goto error_close;
714 }
715 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000716 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400717 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400718 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500719 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200720 else
721 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500722 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500723 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400724 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
725
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726 brelse(bh);
727error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100728 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400729error:
730 mutex_unlock(&uuid_mutex);
731 return ret;
732}
Chris Mason0b86a832008-03-24 15:01:56 -0400733
Miao Xie6d07bce2011-01-05 10:07:31 +0000734/* helper to account the used device space in the range */
735int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
736 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400737{
738 struct btrfs_key key;
739 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000740 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500741 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000742 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400743 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000744 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400745 struct extent_buffer *l;
746
Miao Xie6d07bce2011-01-05 10:07:31 +0000747 *length = 0;
748
749 if (start >= device->total_bytes)
750 return 0;
751
Yan Zheng2b820322008-11-17 21:11:30 -0500752 path = btrfs_alloc_path();
753 if (!path)
754 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400755 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400756
Chris Mason0b86a832008-03-24 15:01:56 -0400757 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000758 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400759 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000760
761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400762 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000763 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400764 if (ret > 0) {
765 ret = btrfs_previous_item(root, path, key.objectid, key.type);
766 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000767 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400768 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000769
Chris Mason0b86a832008-03-24 15:01:56 -0400770 while (1) {
771 l = path->nodes[0];
772 slot = path->slots[0];
773 if (slot >= btrfs_header_nritems(l)) {
774 ret = btrfs_next_leaf(root, path);
775 if (ret == 0)
776 continue;
777 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000778 goto out;
779
780 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400781 }
782 btrfs_item_key_to_cpu(l, &key, slot);
783
784 if (key.objectid < device->devid)
785 goto next;
786
787 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000788 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400789
Chris Masond3977122009-01-05 21:25:51 -0500790 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400791 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400792
Chris Mason0b86a832008-03-24 15:01:56 -0400793 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000794 extent_end = key.offset + btrfs_dev_extent_length(l,
795 dev_extent);
796 if (key.offset <= start && extent_end > end) {
797 *length = end - start + 1;
798 break;
799 } else if (key.offset <= start && extent_end > start)
800 *length += extent_end - start;
801 else if (key.offset > start && extent_end <= end)
802 *length += extent_end - key.offset;
803 else if (key.offset > start && key.offset <= end) {
804 *length += end - key.offset + 1;
805 break;
806 } else if (key.offset > end)
807 break;
808
809next:
810 path->slots[0]++;
811 }
812 ret = 0;
813out:
814 btrfs_free_path(path);
815 return ret;
816}
817
Chris Mason0b86a832008-03-24 15:01:56 -0400818/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000819 * find_free_dev_extent - find free space in the specified device
820 * @trans: transaction handler
821 * @device: the device which we search the free space in
822 * @num_bytes: the size of the free space that we need
823 * @start: store the start of the free space.
824 * @len: the size of the free space. that we find, or the size of the max
825 * free space if we don't find suitable free space
826 *
Chris Mason0b86a832008-03-24 15:01:56 -0400827 * this uses a pretty simple search, the expectation is that it is
828 * called very infrequently and that a given device has a small number
829 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000830 *
831 * @start is used to store the start of the free space if we find. But if we
832 * don't find suitable free space, it will be used to store the start position
833 * of the max free space.
834 *
835 * @len is used to store the size of the free space that we find.
836 * But if we don't find suitable free space, it is used to store the size of
837 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400838 */
839int find_free_dev_extent(struct btrfs_trans_handle *trans,
840 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000841 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400842{
843 struct btrfs_key key;
844 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000845 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400846 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 u64 hole_size;
848 u64 max_hole_start;
849 u64 max_hole_size;
850 u64 extent_end;
851 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400852 u64 search_end = device->total_bytes;
853 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400855 struct extent_buffer *l;
856
Chris Mason0b86a832008-03-24 15:01:56 -0400857 /* FIXME use last free of some kind */
858
859 /* we don't want to overwrite the superblock on the drive,
860 * so we make sure to start at an offset of at least 1MB
861 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200862 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400863
Miao Xie7bfc8372011-01-05 10:07:26 +0000864 max_hole_start = search_start;
865 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000866 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000867
868 if (search_start >= search_end) {
869 ret = -ENOSPC;
870 goto error;
871 }
872
873 path = btrfs_alloc_path();
874 if (!path) {
875 ret = -ENOMEM;
876 goto error;
877 }
878 path->reada = 2;
879
Chris Mason0b86a832008-03-24 15:01:56 -0400880 key.objectid = device->devid;
881 key.offset = search_start;
882 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000883
Chris Mason0b86a832008-03-24 15:01:56 -0400884 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
885 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000886 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400887 if (ret > 0) {
888 ret = btrfs_previous_item(root, path, key.objectid, key.type);
889 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000890 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400891 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000892
Chris Mason0b86a832008-03-24 15:01:56 -0400893 while (1) {
894 l = path->nodes[0];
895 slot = path->slots[0];
896 if (slot >= btrfs_header_nritems(l)) {
897 ret = btrfs_next_leaf(root, path);
898 if (ret == 0)
899 continue;
900 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000901 goto out;
902
903 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400904 }
905 btrfs_item_key_to_cpu(l, &key, slot);
906
907 if (key.objectid < device->devid)
908 goto next;
909
910 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400912
Chris Mason0b86a832008-03-24 15:01:56 -0400913 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
914 goto next;
915
Miao Xie7bfc8372011-01-05 10:07:26 +0000916 if (key.offset > search_start) {
917 hole_size = key.offset - search_start;
918
919 if (hole_size > max_hole_size) {
920 max_hole_start = search_start;
921 max_hole_size = hole_size;
922 }
923
924 /*
925 * If this free space is greater than which we need,
926 * it must be the max free space that we have found
927 * until now, so max_hole_start must point to the start
928 * of this free space and the length of this free space
929 * is stored in max_hole_size. Thus, we return
930 * max_hole_start and max_hole_size and go back to the
931 * caller.
932 */
933 if (hole_size >= num_bytes) {
934 ret = 0;
935 goto out;
936 }
937 }
938
Chris Mason0b86a832008-03-24 15:01:56 -0400939 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000940 extent_end = key.offset + btrfs_dev_extent_length(l,
941 dev_extent);
942 if (extent_end > search_start)
943 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400944next:
945 path->slots[0]++;
946 cond_resched();
947 }
Chris Mason0b86a832008-03-24 15:01:56 -0400948
liubo38c01b92011-08-02 02:39:03 +0000949 /*
950 * At this point, search_start should be the end of
951 * allocated dev extents, and when shrinking the device,
952 * search_end may be smaller than search_start.
953 */
954 if (search_end > search_start)
955 hole_size = search_end - search_start;
956
Miao Xie7bfc8372011-01-05 10:07:26 +0000957 if (hole_size > max_hole_size) {
958 max_hole_start = search_start;
959 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400960 }
Chris Mason0b86a832008-03-24 15:01:56 -0400961
Miao Xie7bfc8372011-01-05 10:07:26 +0000962 /* See above. */
963 if (hole_size < num_bytes)
964 ret = -ENOSPC;
965 else
966 ret = 0;
967
968out:
Yan Zheng2b820322008-11-17 21:11:30 -0500969 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000970error:
971 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000972 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000973 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400974 return ret;
975}
976
Christoph Hellwigb2950862008-12-02 09:54:17 -0500977static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400978 struct btrfs_device *device,
979 u64 start)
980{
981 int ret;
982 struct btrfs_path *path;
983 struct btrfs_root *root = device->dev_root;
984 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400985 struct btrfs_key found_key;
986 struct extent_buffer *leaf = NULL;
987 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400988
989 path = btrfs_alloc_path();
990 if (!path)
991 return -ENOMEM;
992
993 key.objectid = device->devid;
994 key.offset = start;
995 key.type = BTRFS_DEV_EXTENT_KEY;
996
997 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400998 if (ret > 0) {
999 ret = btrfs_previous_item(root, path, key.objectid,
1000 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001001 if (ret)
1002 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001003 leaf = path->nodes[0];
1004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1005 extent = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_dev_extent);
1007 BUG_ON(found_key.offset > start || found_key.offset +
1008 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -04001009 } else if (ret == 0) {
1010 leaf = path->nodes[0];
1011 extent = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_dev_extent);
1013 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001014 BUG_ON(ret);
1015
Josef Bacik2bf64752011-09-26 17:12:22 -04001016 if (device->bytes_used > 0) {
1017 u64 len = btrfs_dev_extent_length(leaf, extent);
1018 device->bytes_used -= len;
1019 spin_lock(&root->fs_info->free_chunk_lock);
1020 root->fs_info->free_chunk_space += len;
1021 spin_unlock(&root->fs_info->free_chunk_lock);
1022 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001023 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001024
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001025out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001026 btrfs_free_path(path);
1027 return ret;
1028}
1029
Yan Zheng2b820322008-11-17 21:11:30 -05001030int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001031 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001032 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001033 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001034{
1035 int ret;
1036 struct btrfs_path *path;
1037 struct btrfs_root *root = device->dev_root;
1038 struct btrfs_dev_extent *extent;
1039 struct extent_buffer *leaf;
1040 struct btrfs_key key;
1041
Chris Masondfe25022008-05-13 13:46:40 -04001042 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001043 path = btrfs_alloc_path();
1044 if (!path)
1045 return -ENOMEM;
1046
Chris Mason0b86a832008-03-24 15:01:56 -04001047 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001048 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001049 key.type = BTRFS_DEV_EXTENT_KEY;
1050 ret = btrfs_insert_empty_item(trans, root, path, &key,
1051 sizeof(*extent));
1052 BUG_ON(ret);
1053
1054 leaf = path->nodes[0];
1055 extent = btrfs_item_ptr(leaf, path->slots[0],
1056 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001057 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1058 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1059 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1060
1061 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1062 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1063 BTRFS_UUID_SIZE);
1064
Chris Mason0b86a832008-03-24 15:01:56 -04001065 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1066 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001067 btrfs_free_path(path);
1068 return ret;
1069}
1070
Chris Masona1b32a52008-09-05 16:09:51 -04001071static noinline int find_next_chunk(struct btrfs_root *root,
1072 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001073{
1074 struct btrfs_path *path;
1075 int ret;
1076 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001077 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001078 struct btrfs_key found_key;
1079
1080 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001081 if (!path)
1082 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001083
Chris Masone17cade2008-04-15 15:41:47 -04001084 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001085 key.offset = (u64)-1;
1086 key.type = BTRFS_CHUNK_ITEM_KEY;
1087
1088 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1089 if (ret < 0)
1090 goto error;
1091
1092 BUG_ON(ret == 0);
1093
1094 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1095 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001096 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001097 } else {
1098 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1099 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001100 if (found_key.objectid != objectid)
1101 *offset = 0;
1102 else {
1103 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1104 struct btrfs_chunk);
1105 *offset = found_key.offset +
1106 btrfs_chunk_length(path->nodes[0], chunk);
1107 }
Chris Mason0b86a832008-03-24 15:01:56 -04001108 }
1109 ret = 0;
1110error:
1111 btrfs_free_path(path);
1112 return ret;
1113}
1114
Yan Zheng2b820322008-11-17 21:11:30 -05001115static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001116{
1117 int ret;
1118 struct btrfs_key key;
1119 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001120 struct btrfs_path *path;
1121
1122 root = root->fs_info->chunk_root;
1123
1124 path = btrfs_alloc_path();
1125 if (!path)
1126 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001127
1128 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1129 key.type = BTRFS_DEV_ITEM_KEY;
1130 key.offset = (u64)-1;
1131
1132 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1133 if (ret < 0)
1134 goto error;
1135
1136 BUG_ON(ret == 0);
1137
1138 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1139 BTRFS_DEV_ITEM_KEY);
1140 if (ret) {
1141 *objectid = 1;
1142 } else {
1143 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1144 path->slots[0]);
1145 *objectid = found_key.offset + 1;
1146 }
1147 ret = 0;
1148error:
Yan Zheng2b820322008-11-17 21:11:30 -05001149 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001150 return ret;
1151}
1152
1153/*
1154 * the device information is stored in the chunk root
1155 * the btrfs_device struct should be fully filled in
1156 */
1157int btrfs_add_device(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root,
1159 struct btrfs_device *device)
1160{
1161 int ret;
1162 struct btrfs_path *path;
1163 struct btrfs_dev_item *dev_item;
1164 struct extent_buffer *leaf;
1165 struct btrfs_key key;
1166 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001167
1168 root = root->fs_info->chunk_root;
1169
1170 path = btrfs_alloc_path();
1171 if (!path)
1172 return -ENOMEM;
1173
Chris Mason0b86a832008-03-24 15:01:56 -04001174 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1175 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001176 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001177
1178 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001179 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001180 if (ret)
1181 goto out;
1182
1183 leaf = path->nodes[0];
1184 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1185
1186 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001187 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001188 btrfs_set_device_type(leaf, dev_item, device->type);
1189 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1190 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1191 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001192 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1193 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001194 btrfs_set_device_group(leaf, dev_item, 0);
1195 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1196 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001197 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001198
Chris Mason0b86a832008-03-24 15:01:56 -04001199 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001200 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001201 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1202 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001203 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001204
Yan Zheng2b820322008-11-17 21:11:30 -05001205 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001206out:
1207 btrfs_free_path(path);
1208 return ret;
1209}
Chris Mason8f18cf12008-04-25 16:53:30 -04001210
Chris Masona061fc82008-05-07 11:43:44 -04001211static int btrfs_rm_dev_item(struct btrfs_root *root,
1212 struct btrfs_device *device)
1213{
1214 int ret;
1215 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001216 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001217 struct btrfs_trans_handle *trans;
1218
1219 root = root->fs_info->chunk_root;
1220
1221 path = btrfs_alloc_path();
1222 if (!path)
1223 return -ENOMEM;
1224
Yan, Zhenga22285a2010-05-16 10:48:46 -04001225 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001226 if (IS_ERR(trans)) {
1227 btrfs_free_path(path);
1228 return PTR_ERR(trans);
1229 }
Chris Masona061fc82008-05-07 11:43:44 -04001230 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1231 key.type = BTRFS_DEV_ITEM_KEY;
1232 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001233 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001234
1235 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1236 if (ret < 0)
1237 goto out;
1238
1239 if (ret > 0) {
1240 ret = -ENOENT;
1241 goto out;
1242 }
1243
1244 ret = btrfs_del_item(trans, root, path);
1245 if (ret)
1246 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001247out:
1248 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001249 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001250 btrfs_commit_transaction(trans, root);
1251 return ret;
1252}
1253
1254int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1255{
1256 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001257 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001258 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001259 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001260 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001261 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001262 u64 all_avail;
1263 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001264 u64 num_devices;
1265 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001266 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001267 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001268
Chris Masona061fc82008-05-07 11:43:44 -04001269 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001270 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001271
1272 all_avail = root->fs_info->avail_data_alloc_bits |
1273 root->fs_info->avail_system_alloc_bits |
1274 root->fs_info->avail_metadata_alloc_bits;
1275
1276 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001277 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001278 printk(KERN_ERR "btrfs: unable to go below four devices "
1279 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001280 ret = -EINVAL;
1281 goto out;
1282 }
1283
1284 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001285 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001286 printk(KERN_ERR "btrfs: unable to go below two "
1287 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001288 ret = -EINVAL;
1289 goto out;
1290 }
1291
Chris Masondfe25022008-05-13 13:46:40 -04001292 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001293 struct list_head *devices;
1294 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001295
Chris Masondfe25022008-05-13 13:46:40 -04001296 device = NULL;
1297 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001298 /*
1299 * It is safe to read the devices since the volume_mutex
1300 * is held.
1301 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001302 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001303 if (tmp->in_fs_metadata && !tmp->bdev) {
1304 device = tmp;
1305 break;
1306 }
1307 }
1308 bdev = NULL;
1309 bh = NULL;
1310 disk_super = NULL;
1311 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001312 printk(KERN_ERR "btrfs: no missing devices found to "
1313 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001314 goto out;
1315 }
Chris Masondfe25022008-05-13 13:46:40 -04001316 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001317 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1318 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001319 if (IS_ERR(bdev)) {
1320 ret = PTR_ERR(bdev);
1321 goto out;
1322 }
1323
Yan Zheng2b820322008-11-17 21:11:30 -05001324 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001325 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001326 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001327 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001328 goto error_close;
1329 }
1330 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001331 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001332 dev_uuid = disk_super->dev_item.uuid;
1333 device = btrfs_find_device(root, devid, dev_uuid,
1334 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001335 if (!device) {
1336 ret = -ENOENT;
1337 goto error_brelse;
1338 }
Chris Masondfe25022008-05-13 13:46:40 -04001339 }
Yan Zheng2b820322008-11-17 21:11:30 -05001340
1341 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001342 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1343 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001344 ret = -EINVAL;
1345 goto error_brelse;
1346 }
1347
1348 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001349 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001350 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001351 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001352 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001353 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001354 }
Chris Masona061fc82008-05-07 11:43:44 -04001355
1356 ret = btrfs_shrink_device(device, 0);
1357 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001358 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001359
Chris Masona061fc82008-05-07 11:43:44 -04001360 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1361 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001362 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001363
Josef Bacik2bf64752011-09-26 17:12:22 -04001364 spin_lock(&root->fs_info->free_chunk_lock);
1365 root->fs_info->free_chunk_space = device->total_bytes -
1366 device->bytes_used;
1367 spin_unlock(&root->fs_info->free_chunk_lock);
1368
Yan Zheng2b820322008-11-17 21:11:30 -05001369 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001370 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001371
1372 /*
1373 * the device list mutex makes sure that we don't change
1374 * the device list while someone else is writing out all
1375 * the device supers.
1376 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001377
1378 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001379 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001380 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001381
Yan Zhenge4404d62008-12-12 10:03:26 -05001382 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001383
Chris Masoncd02dca2010-12-13 14:56:23 -05001384 if (device->missing)
1385 root->fs_info->fs_devices->missing_devices--;
1386
Yan Zheng2b820322008-11-17 21:11:30 -05001387 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1388 struct btrfs_device, dev_list);
1389 if (device->bdev == root->fs_info->sb->s_bdev)
1390 root->fs_info->sb->s_bdev = next_device->bdev;
1391 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1392 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1393
Xiao Guangrong1f781602011-04-20 10:09:16 +00001394 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001395 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001396
1397 call_rcu(&device->rcu, free_device);
1398 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001399
Yan Zheng2b820322008-11-17 21:11:30 -05001400 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1401 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1402
Xiao Guangrong1f781602011-04-20 10:09:16 +00001403 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001404 struct btrfs_fs_devices *fs_devices;
1405 fs_devices = root->fs_info->fs_devices;
1406 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001407 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001408 break;
1409 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001410 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001411 fs_devices->seed = cur_devices->seed;
1412 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001413 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001414 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001415 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001416 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001417 }
1418
1419 /*
1420 * at this point, the device is zero sized. We want to
1421 * remove it from the devices list and zero out the old super
1422 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001423 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001424 /* make sure this device isn't detected as part of
1425 * the FS anymore
1426 */
1427 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1428 set_buffer_dirty(bh);
1429 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001430 }
Chris Masona061fc82008-05-07 11:43:44 -04001431
Chris Masona061fc82008-05-07 11:43:44 -04001432 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001433
1434error_brelse:
1435 brelse(bh);
1436error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001437 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001438 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001439out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001440 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001441 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001442 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001443error_undo:
1444 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001445 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001446 list_add(&device->dev_alloc_list,
1447 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001448 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001449 root->fs_info->fs_devices->rw_devices++;
1450 }
1451 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001452}
1453
Yan Zheng2b820322008-11-17 21:11:30 -05001454/*
1455 * does all the dirty work required for changing file system's UUID.
1456 */
1457static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *root)
1459{
1460 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1461 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001462 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001463 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1464 struct btrfs_device *device;
1465 u64 super_flags;
1466
1467 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001468 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001469 return -EINVAL;
1470
Yan Zhenge4404d62008-12-12 10:03:26 -05001471 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1472 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001473 return -ENOMEM;
1474
Yan Zhenge4404d62008-12-12 10:03:26 -05001475 old_devices = clone_fs_devices(fs_devices);
1476 if (IS_ERR(old_devices)) {
1477 kfree(seed_devices);
1478 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001479 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001480
Yan Zheng2b820322008-11-17 21:11:30 -05001481 list_add(&old_devices->list, &fs_uuids);
1482
Yan Zhenge4404d62008-12-12 10:03:26 -05001483 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1484 seed_devices->opened = 1;
1485 INIT_LIST_HEAD(&seed_devices->devices);
1486 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001487 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001488
1489 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001490 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1491 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001492 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1493
Yan Zhenge4404d62008-12-12 10:03:26 -05001494 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1495 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1496 device->fs_devices = seed_devices;
1497 }
1498
Yan Zheng2b820322008-11-17 21:11:30 -05001499 fs_devices->seeding = 0;
1500 fs_devices->num_devices = 0;
1501 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001502 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001503
1504 generate_random_uuid(fs_devices->fsid);
1505 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1506 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1507 super_flags = btrfs_super_flags(disk_super) &
1508 ~BTRFS_SUPER_FLAG_SEEDING;
1509 btrfs_set_super_flags(disk_super, super_flags);
1510
1511 return 0;
1512}
1513
1514/*
1515 * strore the expected generation for seed devices in device items.
1516 */
1517static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1518 struct btrfs_root *root)
1519{
1520 struct btrfs_path *path;
1521 struct extent_buffer *leaf;
1522 struct btrfs_dev_item *dev_item;
1523 struct btrfs_device *device;
1524 struct btrfs_key key;
1525 u8 fs_uuid[BTRFS_UUID_SIZE];
1526 u8 dev_uuid[BTRFS_UUID_SIZE];
1527 u64 devid;
1528 int ret;
1529
1530 path = btrfs_alloc_path();
1531 if (!path)
1532 return -ENOMEM;
1533
1534 root = root->fs_info->chunk_root;
1535 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1536 key.offset = 0;
1537 key.type = BTRFS_DEV_ITEM_KEY;
1538
1539 while (1) {
1540 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1541 if (ret < 0)
1542 goto error;
1543
1544 leaf = path->nodes[0];
1545next_slot:
1546 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1547 ret = btrfs_next_leaf(root, path);
1548 if (ret > 0)
1549 break;
1550 if (ret < 0)
1551 goto error;
1552 leaf = path->nodes[0];
1553 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001554 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001555 continue;
1556 }
1557
1558 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1559 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1560 key.type != BTRFS_DEV_ITEM_KEY)
1561 break;
1562
1563 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1564 struct btrfs_dev_item);
1565 devid = btrfs_device_id(leaf, dev_item);
1566 read_extent_buffer(leaf, dev_uuid,
1567 (unsigned long)btrfs_device_uuid(dev_item),
1568 BTRFS_UUID_SIZE);
1569 read_extent_buffer(leaf, fs_uuid,
1570 (unsigned long)btrfs_device_fsid(dev_item),
1571 BTRFS_UUID_SIZE);
1572 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1573 BUG_ON(!device);
1574
1575 if (device->fs_devices->seeding) {
1576 btrfs_set_device_generation(leaf, dev_item,
1577 device->generation);
1578 btrfs_mark_buffer_dirty(leaf);
1579 }
1580
1581 path->slots[0]++;
1582 goto next_slot;
1583 }
1584 ret = 0;
1585error:
1586 btrfs_free_path(path);
1587 return ret;
1588}
1589
Chris Mason788f20e2008-04-28 15:29:42 -04001590int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1591{
Josef Bacikd5e20032011-08-04 14:52:27 +00001592 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001593 struct btrfs_trans_handle *trans;
1594 struct btrfs_device *device;
1595 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001596 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001597 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001598 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001599 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001600 int ret = 0;
1601
Yan Zheng2b820322008-11-17 21:11:30 -05001602 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1603 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001604
Tejun Heod4d77622010-11-13 11:55:18 +01001605 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1606 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001607 if (IS_ERR(bdev))
1608 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001609
Yan Zheng2b820322008-11-17 21:11:30 -05001610 if (root->fs_info->fs_devices->seeding) {
1611 seeding_dev = 1;
1612 down_write(&sb->s_umount);
1613 mutex_lock(&uuid_mutex);
1614 }
1615
Chris Mason8c8bee12008-09-29 11:19:10 -04001616 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001617 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001618
Chris Mason788f20e2008-04-28 15:29:42 -04001619 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001620 /*
1621 * we have the volume lock, so we don't need the extra
1622 * device list mutex while reading the list here.
1623 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001624 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001625 if (device->bdev == bdev) {
1626 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001627 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001628 }
1629 }
1630
1631 device = kzalloc(sizeof(*device), GFP_NOFS);
1632 if (!device) {
1633 /* we can safely leave the fs_devices entry around */
1634 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001635 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001636 }
1637
Chris Mason788f20e2008-04-28 15:29:42 -04001638 device->name = kstrdup(device_path, GFP_NOFS);
1639 if (!device->name) {
1640 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001641 ret = -ENOMEM;
1642 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001643 }
Yan Zheng2b820322008-11-17 21:11:30 -05001644
1645 ret = find_next_devid(root, &device->devid);
1646 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001647 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001648 kfree(device);
1649 goto error;
1650 }
1651
Yan, Zhenga22285a2010-05-16 10:48:46 -04001652 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001653 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001654 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001655 kfree(device);
1656 ret = PTR_ERR(trans);
1657 goto error;
1658 }
1659
Yan Zheng2b820322008-11-17 21:11:30 -05001660 lock_chunks(root);
1661
Josef Bacikd5e20032011-08-04 14:52:27 +00001662 q = bdev_get_queue(bdev);
1663 if (blk_queue_discard(q))
1664 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001665 device->writeable = 1;
1666 device->work.func = pending_bios_fn;
1667 generate_random_uuid(device->uuid);
1668 spin_lock_init(&device->io_lock);
1669 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001670 device->io_width = root->sectorsize;
1671 device->io_align = root->sectorsize;
1672 device->sector_size = root->sectorsize;
1673 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001674 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001675 device->dev_root = root->fs_info->dev_root;
1676 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001677 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001678 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001679 set_blocksize(device->bdev, 4096);
1680
Yan Zheng2b820322008-11-17 21:11:30 -05001681 if (seeding_dev) {
1682 sb->s_flags &= ~MS_RDONLY;
1683 ret = btrfs_prepare_sprout(trans, root);
1684 BUG_ON(ret);
1685 }
1686
1687 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001688
1689 /*
1690 * we don't want write_supers to jump in here with our device
1691 * half setup
1692 */
1693 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001694 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001695 list_add(&device->dev_alloc_list,
1696 &root->fs_info->fs_devices->alloc_list);
1697 root->fs_info->fs_devices->num_devices++;
1698 root->fs_info->fs_devices->open_devices++;
1699 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001700 if (device->can_discard)
1701 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001702 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1703
Josef Bacik2bf64752011-09-26 17:12:22 -04001704 spin_lock(&root->fs_info->free_chunk_lock);
1705 root->fs_info->free_chunk_space += device->total_bytes;
1706 spin_unlock(&root->fs_info->free_chunk_lock);
1707
Chris Masonc2898112009-06-10 09:51:32 -04001708 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1709 root->fs_info->fs_devices->rotating = 1;
1710
Chris Mason788f20e2008-04-28 15:29:42 -04001711 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1712 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1713 total_bytes + device->total_bytes);
1714
1715 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1716 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1717 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001718 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001719
Yan Zheng2b820322008-11-17 21:11:30 -05001720 if (seeding_dev) {
1721 ret = init_first_rw_device(trans, root, device);
1722 BUG_ON(ret);
1723 ret = btrfs_finish_sprout(trans, root);
1724 BUG_ON(ret);
1725 } else {
1726 ret = btrfs_add_device(trans, root, device);
1727 }
1728
Chris Mason913d9522009-03-10 13:17:18 -04001729 /*
1730 * we've got more storage, clear any full flags on the space
1731 * infos
1732 */
1733 btrfs_clear_space_info_full(root->fs_info);
1734
Chris Mason7d9eb122008-07-08 14:19:17 -04001735 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001736 btrfs_commit_transaction(trans, root);
1737
1738 if (seeding_dev) {
1739 mutex_unlock(&uuid_mutex);
1740 up_write(&sb->s_umount);
1741
1742 ret = btrfs_relocate_sys_chunks(root);
1743 BUG_ON(ret);
1744 }
1745out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001746 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001747 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001748error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001749 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001750 if (seeding_dev) {
1751 mutex_unlock(&uuid_mutex);
1752 up_write(&sb->s_umount);
1753 }
Chris Mason788f20e2008-04-28 15:29:42 -04001754 goto out;
1755}
1756
Chris Masond3977122009-01-05 21:25:51 -05001757static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1758 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001759{
1760 int ret;
1761 struct btrfs_path *path;
1762 struct btrfs_root *root;
1763 struct btrfs_dev_item *dev_item;
1764 struct extent_buffer *leaf;
1765 struct btrfs_key key;
1766
1767 root = device->dev_root->fs_info->chunk_root;
1768
1769 path = btrfs_alloc_path();
1770 if (!path)
1771 return -ENOMEM;
1772
1773 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1774 key.type = BTRFS_DEV_ITEM_KEY;
1775 key.offset = device->devid;
1776
1777 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1778 if (ret < 0)
1779 goto out;
1780
1781 if (ret > 0) {
1782 ret = -ENOENT;
1783 goto out;
1784 }
1785
1786 leaf = path->nodes[0];
1787 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1788
1789 btrfs_set_device_id(leaf, dev_item, device->devid);
1790 btrfs_set_device_type(leaf, dev_item, device->type);
1791 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1792 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1793 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001794 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001795 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1796 btrfs_mark_buffer_dirty(leaf);
1797
1798out:
1799 btrfs_free_path(path);
1800 return ret;
1801}
1802
Chris Mason7d9eb122008-07-08 14:19:17 -04001803static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001804 struct btrfs_device *device, u64 new_size)
1805{
1806 struct btrfs_super_block *super_copy =
1807 &device->dev_root->fs_info->super_copy;
1808 u64 old_total = btrfs_super_total_bytes(super_copy);
1809 u64 diff = new_size - device->total_bytes;
1810
Yan Zheng2b820322008-11-17 21:11:30 -05001811 if (!device->writeable)
1812 return -EACCES;
1813 if (new_size <= device->total_bytes)
1814 return -EINVAL;
1815
Chris Mason8f18cf12008-04-25 16:53:30 -04001816 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001817 device->fs_devices->total_rw_bytes += diff;
1818
1819 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001820 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001821 btrfs_clear_space_info_full(device->dev_root->fs_info);
1822
Chris Mason8f18cf12008-04-25 16:53:30 -04001823 return btrfs_update_device(trans, device);
1824}
1825
Chris Mason7d9eb122008-07-08 14:19:17 -04001826int btrfs_grow_device(struct btrfs_trans_handle *trans,
1827 struct btrfs_device *device, u64 new_size)
1828{
1829 int ret;
1830 lock_chunks(device->dev_root);
1831 ret = __btrfs_grow_device(trans, device, new_size);
1832 unlock_chunks(device->dev_root);
1833 return ret;
1834}
1835
Chris Mason8f18cf12008-04-25 16:53:30 -04001836static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1837 struct btrfs_root *root,
1838 u64 chunk_tree, u64 chunk_objectid,
1839 u64 chunk_offset)
1840{
1841 int ret;
1842 struct btrfs_path *path;
1843 struct btrfs_key key;
1844
1845 root = root->fs_info->chunk_root;
1846 path = btrfs_alloc_path();
1847 if (!path)
1848 return -ENOMEM;
1849
1850 key.objectid = chunk_objectid;
1851 key.offset = chunk_offset;
1852 key.type = BTRFS_CHUNK_ITEM_KEY;
1853
1854 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1855 BUG_ON(ret);
1856
1857 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001858
1859 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001860 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001861}
1862
Christoph Hellwigb2950862008-12-02 09:54:17 -05001863static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001864 chunk_offset)
1865{
1866 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1867 struct btrfs_disk_key *disk_key;
1868 struct btrfs_chunk *chunk;
1869 u8 *ptr;
1870 int ret = 0;
1871 u32 num_stripes;
1872 u32 array_size;
1873 u32 len = 0;
1874 u32 cur;
1875 struct btrfs_key key;
1876
1877 array_size = btrfs_super_sys_array_size(super_copy);
1878
1879 ptr = super_copy->sys_chunk_array;
1880 cur = 0;
1881
1882 while (cur < array_size) {
1883 disk_key = (struct btrfs_disk_key *)ptr;
1884 btrfs_disk_key_to_cpu(&key, disk_key);
1885
1886 len = sizeof(*disk_key);
1887
1888 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1889 chunk = (struct btrfs_chunk *)(ptr + len);
1890 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1891 len += btrfs_chunk_item_size(num_stripes);
1892 } else {
1893 ret = -EIO;
1894 break;
1895 }
1896 if (key.objectid == chunk_objectid &&
1897 key.offset == chunk_offset) {
1898 memmove(ptr, ptr + len, array_size - (cur + len));
1899 array_size -= len;
1900 btrfs_set_super_sys_array_size(super_copy, array_size);
1901 } else {
1902 ptr += len;
1903 cur += len;
1904 }
1905 }
1906 return ret;
1907}
1908
Christoph Hellwigb2950862008-12-02 09:54:17 -05001909static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001910 u64 chunk_tree, u64 chunk_objectid,
1911 u64 chunk_offset)
1912{
1913 struct extent_map_tree *em_tree;
1914 struct btrfs_root *extent_root;
1915 struct btrfs_trans_handle *trans;
1916 struct extent_map *em;
1917 struct map_lookup *map;
1918 int ret;
1919 int i;
1920
1921 root = root->fs_info->chunk_root;
1922 extent_root = root->fs_info->extent_root;
1923 em_tree = &root->fs_info->mapping_tree.map_tree;
1924
Josef Bacikba1bf482009-09-11 16:11:19 -04001925 ret = btrfs_can_relocate(extent_root, chunk_offset);
1926 if (ret)
1927 return -ENOSPC;
1928
Chris Mason8f18cf12008-04-25 16:53:30 -04001929 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001930 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001931 if (ret)
1932 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001933
Yan, Zhenga22285a2010-05-16 10:48:46 -04001934 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001935 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001936
Chris Mason7d9eb122008-07-08 14:19:17 -04001937 lock_chunks(root);
1938
Chris Mason8f18cf12008-04-25 16:53:30 -04001939 /*
1940 * step two, delete the device extents and the
1941 * chunk tree entries
1942 */
Chris Mason890871b2009-09-02 16:24:52 -04001943 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001944 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001945 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001946
Chris Masona061fc82008-05-07 11:43:44 -04001947 BUG_ON(em->start > chunk_offset ||
1948 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001949 map = (struct map_lookup *)em->bdev;
1950
1951 for (i = 0; i < map->num_stripes; i++) {
1952 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1953 map->stripes[i].physical);
1954 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001955
Chris Masondfe25022008-05-13 13:46:40 -04001956 if (map->stripes[i].dev) {
1957 ret = btrfs_update_device(trans, map->stripes[i].dev);
1958 BUG_ON(ret);
1959 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001960 }
1961 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1962 chunk_offset);
1963
1964 BUG_ON(ret);
1965
liubo1abe9b82011-03-24 11:18:59 +00001966 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1967
Chris Mason8f18cf12008-04-25 16:53:30 -04001968 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1969 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1970 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001971 }
1972
Zheng Yan1a40e232008-09-26 10:09:34 -04001973 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1974 BUG_ON(ret);
1975
Chris Mason890871b2009-09-02 16:24:52 -04001976 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001977 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001978 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001979
Chris Mason8f18cf12008-04-25 16:53:30 -04001980 kfree(map);
1981 em->bdev = NULL;
1982
1983 /* once for the tree */
1984 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001985 /* once for us */
1986 free_extent_map(em);
1987
Chris Mason7d9eb122008-07-08 14:19:17 -04001988 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 btrfs_end_transaction(trans, root);
1990 return 0;
1991}
1992
Yan Zheng2b820322008-11-17 21:11:30 -05001993static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1994{
1995 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1996 struct btrfs_path *path;
1997 struct extent_buffer *leaf;
1998 struct btrfs_chunk *chunk;
1999 struct btrfs_key key;
2000 struct btrfs_key found_key;
2001 u64 chunk_tree = chunk_root->root_key.objectid;
2002 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002003 bool retried = false;
2004 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002005 int ret;
2006
2007 path = btrfs_alloc_path();
2008 if (!path)
2009 return -ENOMEM;
2010
Josef Bacikba1bf482009-09-11 16:11:19 -04002011again:
Yan Zheng2b820322008-11-17 21:11:30 -05002012 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2013 key.offset = (u64)-1;
2014 key.type = BTRFS_CHUNK_ITEM_KEY;
2015
2016 while (1) {
2017 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2018 if (ret < 0)
2019 goto error;
2020 BUG_ON(ret == 0);
2021
2022 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2023 key.type);
2024 if (ret < 0)
2025 goto error;
2026 if (ret > 0)
2027 break;
2028
2029 leaf = path->nodes[0];
2030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2031
2032 chunk = btrfs_item_ptr(leaf, path->slots[0],
2033 struct btrfs_chunk);
2034 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002035 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002036
2037 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2038 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2039 found_key.objectid,
2040 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002041 if (ret == -ENOSPC)
2042 failed++;
2043 else if (ret)
2044 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002045 }
2046
2047 if (found_key.offset == 0)
2048 break;
2049 key.offset = found_key.offset - 1;
2050 }
2051 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002052 if (failed && !retried) {
2053 failed = 0;
2054 retried = true;
2055 goto again;
2056 } else if (failed && retried) {
2057 WARN_ON(1);
2058 ret = -ENOSPC;
2059 }
Yan Zheng2b820322008-11-17 21:11:30 -05002060error:
2061 btrfs_free_path(path);
2062 return ret;
2063}
2064
Chris Masonec44a352008-04-28 15:29:52 -04002065static u64 div_factor(u64 num, int factor)
2066{
2067 if (factor == 10)
2068 return num;
2069 num *= factor;
2070 do_div(num, 10);
2071 return num;
2072}
2073
Chris Masonec44a352008-04-28 15:29:52 -04002074int btrfs_balance(struct btrfs_root *dev_root)
2075{
2076 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002077 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2078 struct btrfs_device *device;
2079 u64 old_size;
2080 u64 size_to_free;
2081 struct btrfs_path *path;
2082 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002083 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2084 struct btrfs_trans_handle *trans;
2085 struct btrfs_key found_key;
2086
Yan Zheng2b820322008-11-17 21:11:30 -05002087 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2088 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002089
Ben Hutchings6f88a442010-12-29 14:55:03 +00002090 if (!capable(CAP_SYS_ADMIN))
2091 return -EPERM;
2092
Chris Mason7d9eb122008-07-08 14:19:17 -04002093 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002094 dev_root = dev_root->fs_info->dev_root;
2095
Chris Masonec44a352008-04-28 15:29:52 -04002096 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002097 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002098 old_size = device->total_bytes;
2099 size_to_free = div_factor(old_size, 1);
2100 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002101 if (!device->writeable ||
2102 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002103 continue;
2104
2105 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002106 if (ret == -ENOSPC)
2107 break;
Chris Masonec44a352008-04-28 15:29:52 -04002108 BUG_ON(ret);
2109
Yan, Zhenga22285a2010-05-16 10:48:46 -04002110 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002111 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002112
2113 ret = btrfs_grow_device(trans, device, old_size);
2114 BUG_ON(ret);
2115
2116 btrfs_end_transaction(trans, dev_root);
2117 }
2118
2119 /* step two, relocate all the chunks */
2120 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002121 if (!path) {
2122 ret = -ENOMEM;
2123 goto error;
2124 }
Chris Masonec44a352008-04-28 15:29:52 -04002125 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2126 key.offset = (u64)-1;
2127 key.type = BTRFS_CHUNK_ITEM_KEY;
2128
Chris Masond3977122009-01-05 21:25:51 -05002129 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002130 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2131 if (ret < 0)
2132 goto error;
2133
2134 /*
2135 * this shouldn't happen, it means the last relocate
2136 * failed
2137 */
2138 if (ret == 0)
2139 break;
2140
2141 ret = btrfs_previous_item(chunk_root, path, 0,
2142 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002143 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002144 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002145
Chris Masonec44a352008-04-28 15:29:52 -04002146 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2147 path->slots[0]);
2148 if (found_key.objectid != key.objectid)
2149 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002150
Chris Masonec44a352008-04-28 15:29:52 -04002151 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002152 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002153 break;
2154
David Sterbab3b4aa72011-04-21 01:20:15 +02002155 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002156 ret = btrfs_relocate_chunk(chunk_root,
2157 chunk_root->root_key.objectid,
2158 found_key.objectid,
2159 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002160 if (ret && ret != -ENOSPC)
2161 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002162 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002163 }
2164 ret = 0;
2165error:
2166 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002167 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002168 return ret;
2169}
2170
Chris Mason8f18cf12008-04-25 16:53:30 -04002171/*
2172 * shrinking a device means finding all of the device extents past
2173 * the new size, and then following the back refs to the chunks.
2174 * The chunk relocation code actually frees the device extent
2175 */
2176int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2177{
2178 struct btrfs_trans_handle *trans;
2179 struct btrfs_root *root = device->dev_root;
2180 struct btrfs_dev_extent *dev_extent = NULL;
2181 struct btrfs_path *path;
2182 u64 length;
2183 u64 chunk_tree;
2184 u64 chunk_objectid;
2185 u64 chunk_offset;
2186 int ret;
2187 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002188 int failed = 0;
2189 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002190 struct extent_buffer *l;
2191 struct btrfs_key key;
2192 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2193 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002194 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002195 u64 diff = device->total_bytes - new_size;
2196
Yan Zheng2b820322008-11-17 21:11:30 -05002197 if (new_size >= device->total_bytes)
2198 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002199
2200 path = btrfs_alloc_path();
2201 if (!path)
2202 return -ENOMEM;
2203
Chris Mason8f18cf12008-04-25 16:53:30 -04002204 path->reada = 2;
2205
Chris Mason7d9eb122008-07-08 14:19:17 -04002206 lock_chunks(root);
2207
Chris Mason8f18cf12008-04-25 16:53:30 -04002208 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002209 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002210 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002211 spin_lock(&root->fs_info->free_chunk_lock);
2212 root->fs_info->free_chunk_space -= diff;
2213 spin_unlock(&root->fs_info->free_chunk_lock);
2214 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002215 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002216
Josef Bacikba1bf482009-09-11 16:11:19 -04002217again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002218 key.objectid = device->devid;
2219 key.offset = (u64)-1;
2220 key.type = BTRFS_DEV_EXTENT_KEY;
2221
2222 while (1) {
2223 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2224 if (ret < 0)
2225 goto done;
2226
2227 ret = btrfs_previous_item(root, path, 0, key.type);
2228 if (ret < 0)
2229 goto done;
2230 if (ret) {
2231 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002232 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002233 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002234 }
2235
2236 l = path->nodes[0];
2237 slot = path->slots[0];
2238 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2239
Josef Bacikba1bf482009-09-11 16:11:19 -04002240 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002241 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002242 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002243 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002244
2245 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2246 length = btrfs_dev_extent_length(l, dev_extent);
2247
Josef Bacikba1bf482009-09-11 16:11:19 -04002248 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002249 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002250 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002251 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002252
2253 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2254 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2255 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002256 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002257
2258 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2259 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002260 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002261 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002262 if (ret == -ENOSPC)
2263 failed++;
2264 key.offset -= 1;
2265 }
2266
2267 if (failed && !retried) {
2268 failed = 0;
2269 retried = true;
2270 goto again;
2271 } else if (failed && retried) {
2272 ret = -ENOSPC;
2273 lock_chunks(root);
2274
2275 device->total_bytes = old_size;
2276 if (device->writeable)
2277 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002278 spin_lock(&root->fs_info->free_chunk_lock);
2279 root->fs_info->free_chunk_space += diff;
2280 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04002281 unlock_chunks(root);
2282 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002283 }
2284
Chris Balld6397ba2009-04-27 07:29:03 -04002285 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002286 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002287 if (IS_ERR(trans)) {
2288 ret = PTR_ERR(trans);
2289 goto done;
2290 }
2291
Chris Balld6397ba2009-04-27 07:29:03 -04002292 lock_chunks(root);
2293
2294 device->disk_total_bytes = new_size;
2295 /* Now btrfs_update_device() will change the on-disk size. */
2296 ret = btrfs_update_device(trans, device);
2297 if (ret) {
2298 unlock_chunks(root);
2299 btrfs_end_transaction(trans, root);
2300 goto done;
2301 }
2302 WARN_ON(diff > old_total);
2303 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2304 unlock_chunks(root);
2305 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002306done:
2307 btrfs_free_path(path);
2308 return ret;
2309}
2310
Christoph Hellwigb2950862008-12-02 09:54:17 -05002311static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002312 struct btrfs_root *root,
2313 struct btrfs_key *key,
2314 struct btrfs_chunk *chunk, int item_size)
2315{
2316 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2317 struct btrfs_disk_key disk_key;
2318 u32 array_size;
2319 u8 *ptr;
2320
2321 array_size = btrfs_super_sys_array_size(super_copy);
2322 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2323 return -EFBIG;
2324
2325 ptr = super_copy->sys_chunk_array + array_size;
2326 btrfs_cpu_key_to_disk(&disk_key, key);
2327 memcpy(ptr, &disk_key, sizeof(disk_key));
2328 ptr += sizeof(disk_key);
2329 memcpy(ptr, chunk, item_size);
2330 item_size += sizeof(disk_key);
2331 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2332 return 0;
2333}
2334
Miao Xieb2117a32011-01-05 10:07:28 +00002335/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002336 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002337 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002338static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002339{
Arne Jansen73c5de02011-04-12 12:07:57 +02002340 const struct btrfs_device_info *di_a = a;
2341 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002342
Arne Jansen73c5de02011-04-12 12:07:57 +02002343 if (di_a->max_avail > di_b->max_avail)
2344 return -1;
2345 if (di_a->max_avail < di_b->max_avail)
2346 return 1;
2347 if (di_a->total_avail > di_b->total_avail)
2348 return -1;
2349 if (di_a->total_avail < di_b->total_avail)
2350 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002351 return 0;
2352}
2353
2354static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2355 struct btrfs_root *extent_root,
2356 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002357 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002358 u64 start, u64 type)
2359{
2360 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002361 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2362 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002363 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002364 struct extent_map_tree *em_tree;
2365 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002366 struct btrfs_device_info *devices_info = NULL;
2367 u64 total_avail;
2368 int num_stripes; /* total number of stripes to allocate */
2369 int sub_stripes; /* sub_stripes info for map */
2370 int dev_stripes; /* stripes per dev */
2371 int devs_max; /* max devs to use */
2372 int devs_min; /* min devs needed */
2373 int devs_increment; /* ndevs has to be a multiple of this */
2374 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002375 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002376 u64 max_stripe_size;
2377 u64 max_chunk_size;
2378 u64 stripe_size;
2379 u64 num_bytes;
2380 int ndevs;
2381 int i;
2382 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002383
2384 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2385 (type & BTRFS_BLOCK_GROUP_DUP)) {
2386 WARN_ON(1);
2387 type &= ~BTRFS_BLOCK_GROUP_DUP;
2388 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002389
Miao Xieb2117a32011-01-05 10:07:28 +00002390 if (list_empty(&fs_devices->alloc_list))
2391 return -ENOSPC;
2392
Arne Jansen73c5de02011-04-12 12:07:57 +02002393 sub_stripes = 1;
2394 dev_stripes = 1;
2395 devs_increment = 1;
2396 ncopies = 1;
2397 devs_max = 0; /* 0 == as many as possible */
2398 devs_min = 1;
2399
2400 /*
2401 * define the properties of each RAID type.
2402 * FIXME: move this to a global table and use it in all RAID
2403 * calculation code
2404 */
2405 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2406 dev_stripes = 2;
2407 ncopies = 2;
2408 devs_max = 1;
2409 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2410 devs_min = 2;
2411 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2412 devs_increment = 2;
2413 ncopies = 2;
2414 devs_max = 2;
2415 devs_min = 2;
2416 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2417 sub_stripes = 2;
2418 devs_increment = 2;
2419 ncopies = 2;
2420 devs_min = 4;
2421 } else {
2422 devs_max = 1;
2423 }
2424
2425 if (type & BTRFS_BLOCK_GROUP_DATA) {
2426 max_stripe_size = 1024 * 1024 * 1024;
2427 max_chunk_size = 10 * max_stripe_size;
2428 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2429 max_stripe_size = 256 * 1024 * 1024;
2430 max_chunk_size = max_stripe_size;
2431 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2432 max_stripe_size = 8 * 1024 * 1024;
2433 max_chunk_size = 2 * max_stripe_size;
2434 } else {
2435 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2436 type);
2437 BUG_ON(1);
2438 }
2439
2440 /* we don't want a chunk larger than 10% of writeable space */
2441 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2442 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002443
2444 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2445 GFP_NOFS);
2446 if (!devices_info)
2447 return -ENOMEM;
2448
Arne Jansen73c5de02011-04-12 12:07:57 +02002449 cur = fs_devices->alloc_list.next;
2450
2451 /*
2452 * in the first pass through the devices list, we gather information
2453 * about the available holes on each device.
2454 */
2455 ndevs = 0;
2456 while (cur != &fs_devices->alloc_list) {
2457 struct btrfs_device *device;
2458 u64 max_avail;
2459 u64 dev_offset;
2460
2461 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2462
2463 cur = cur->next;
2464
2465 if (!device->writeable) {
2466 printk(KERN_ERR
2467 "btrfs: read-only device in alloc_list\n");
2468 WARN_ON(1);
2469 continue;
2470 }
2471
2472 if (!device->in_fs_metadata)
2473 continue;
2474
2475 if (device->total_bytes > device->bytes_used)
2476 total_avail = device->total_bytes - device->bytes_used;
2477 else
2478 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00002479
2480 /* If there is no space on this device, skip it. */
2481 if (total_avail == 0)
2482 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02002483
2484 ret = find_free_dev_extent(trans, device,
2485 max_stripe_size * dev_stripes,
2486 &dev_offset, &max_avail);
2487 if (ret && ret != -ENOSPC)
2488 goto error;
2489
2490 if (ret == 0)
2491 max_avail = max_stripe_size * dev_stripes;
2492
2493 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2494 continue;
2495
2496 devices_info[ndevs].dev_offset = dev_offset;
2497 devices_info[ndevs].max_avail = max_avail;
2498 devices_info[ndevs].total_avail = total_avail;
2499 devices_info[ndevs].dev = device;
2500 ++ndevs;
2501 }
2502
2503 /*
2504 * now sort the devices by hole size / available space
2505 */
2506 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2507 btrfs_cmp_device_info, NULL);
2508
2509 /* round down to number of usable stripes */
2510 ndevs -= ndevs % devs_increment;
2511
2512 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2513 ret = -ENOSPC;
2514 goto error;
2515 }
2516
2517 if (devs_max && ndevs > devs_max)
2518 ndevs = devs_max;
2519 /*
2520 * the primary goal is to maximize the number of stripes, so use as many
2521 * devices as possible, even if the stripes are not maximum sized.
2522 */
2523 stripe_size = devices_info[ndevs-1].max_avail;
2524 num_stripes = ndevs * dev_stripes;
2525
2526 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2527 stripe_size = max_chunk_size * ncopies;
2528 do_div(stripe_size, num_stripes);
2529 }
2530
2531 do_div(stripe_size, dev_stripes);
2532 do_div(stripe_size, BTRFS_STRIPE_LEN);
2533 stripe_size *= BTRFS_STRIPE_LEN;
2534
Miao Xieb2117a32011-01-05 10:07:28 +00002535 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2536 if (!map) {
2537 ret = -ENOMEM;
2538 goto error;
2539 }
2540 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002541
Arne Jansen73c5de02011-04-12 12:07:57 +02002542 for (i = 0; i < ndevs; ++i) {
2543 for (j = 0; j < dev_stripes; ++j) {
2544 int s = i * dev_stripes + j;
2545 map->stripes[s].dev = devices_info[i].dev;
2546 map->stripes[s].physical = devices_info[i].dev_offset +
2547 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002548 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002549 }
Chris Mason593060d2008-03-25 16:50:33 -04002550 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002551 map->stripe_len = BTRFS_STRIPE_LEN;
2552 map->io_align = BTRFS_STRIPE_LEN;
2553 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002554 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002555 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002556
Yan Zheng2b820322008-11-17 21:11:30 -05002557 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002558 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002559
Arne Jansen73c5de02011-04-12 12:07:57 +02002560 *stripe_size_out = stripe_size;
2561 *num_bytes_out = num_bytes;
2562
2563 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002564
David Sterba172ddd62011-04-21 00:48:27 +02002565 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002566 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002567 ret = -ENOMEM;
2568 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002569 }
Chris Mason0b86a832008-03-24 15:01:56 -04002570 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002571 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002572 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002573 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002574 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002575
Chris Mason0b86a832008-03-24 15:01:56 -04002576 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002577 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002578 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002579 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002580 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002581 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002582
2583 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2584 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002585 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002586 BUG_ON(ret);
2587
Arne Jansen73c5de02011-04-12 12:07:57 +02002588 for (i = 0; i < map->num_stripes; ++i) {
2589 struct btrfs_device *device;
2590 u64 dev_offset;
2591
2592 device = map->stripes[i].dev;
2593 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002594
2595 ret = btrfs_alloc_dev_extent(trans, device,
2596 info->chunk_root->root_key.objectid,
2597 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002598 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002599 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002600 }
2601
Miao Xieb2117a32011-01-05 10:07:28 +00002602 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002603 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002604
2605error:
2606 kfree(map);
2607 kfree(devices_info);
2608 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002609}
2610
2611static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2612 struct btrfs_root *extent_root,
2613 struct map_lookup *map, u64 chunk_offset,
2614 u64 chunk_size, u64 stripe_size)
2615{
2616 u64 dev_offset;
2617 struct btrfs_key key;
2618 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2619 struct btrfs_device *device;
2620 struct btrfs_chunk *chunk;
2621 struct btrfs_stripe *stripe;
2622 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2623 int index = 0;
2624 int ret;
2625
2626 chunk = kzalloc(item_size, GFP_NOFS);
2627 if (!chunk)
2628 return -ENOMEM;
2629
2630 index = 0;
2631 while (index < map->num_stripes) {
2632 device = map->stripes[index].dev;
2633 device->bytes_used += stripe_size;
2634 ret = btrfs_update_device(trans, device);
2635 BUG_ON(ret);
2636 index++;
2637 }
2638
Josef Bacik2bf64752011-09-26 17:12:22 -04002639 spin_lock(&extent_root->fs_info->free_chunk_lock);
2640 extent_root->fs_info->free_chunk_space -= (stripe_size *
2641 map->num_stripes);
2642 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2643
Yan Zheng2b820322008-11-17 21:11:30 -05002644 index = 0;
2645 stripe = &chunk->stripe;
2646 while (index < map->num_stripes) {
2647 device = map->stripes[index].dev;
2648 dev_offset = map->stripes[index].physical;
2649
2650 btrfs_set_stack_stripe_devid(stripe, device->devid);
2651 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2652 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2653 stripe++;
2654 index++;
2655 }
2656
2657 btrfs_set_stack_chunk_length(chunk, chunk_size);
2658 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2659 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2660 btrfs_set_stack_chunk_type(chunk, map->type);
2661 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2662 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2663 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2664 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2665 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2666
2667 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2668 key.type = BTRFS_CHUNK_ITEM_KEY;
2669 key.offset = chunk_offset;
2670
2671 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2672 BUG_ON(ret);
2673
2674 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2675 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2676 item_size);
2677 BUG_ON(ret);
2678 }
liubo1abe9b82011-03-24 11:18:59 +00002679
Yan Zheng2b820322008-11-17 21:11:30 -05002680 kfree(chunk);
2681 return 0;
2682}
2683
2684/*
2685 * Chunk allocation falls into two parts. The first part does works
2686 * that make the new allocated chunk useable, but not do any operation
2687 * that modifies the chunk tree. The second part does the works that
2688 * require modifying the chunk tree. This division is important for the
2689 * bootstrap process of adding storage to a seed btrfs.
2690 */
2691int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *extent_root, u64 type)
2693{
2694 u64 chunk_offset;
2695 u64 chunk_size;
2696 u64 stripe_size;
2697 struct map_lookup *map;
2698 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2699 int ret;
2700
2701 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2702 &chunk_offset);
2703 if (ret)
2704 return ret;
2705
2706 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2707 &stripe_size, chunk_offset, type);
2708 if (ret)
2709 return ret;
2710
2711 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2712 chunk_size, stripe_size);
2713 BUG_ON(ret);
2714 return 0;
2715}
2716
Chris Masond3977122009-01-05 21:25:51 -05002717static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002718 struct btrfs_root *root,
2719 struct btrfs_device *device)
2720{
2721 u64 chunk_offset;
2722 u64 sys_chunk_offset;
2723 u64 chunk_size;
2724 u64 sys_chunk_size;
2725 u64 stripe_size;
2726 u64 sys_stripe_size;
2727 u64 alloc_profile;
2728 struct map_lookup *map;
2729 struct map_lookup *sys_map;
2730 struct btrfs_fs_info *fs_info = root->fs_info;
2731 struct btrfs_root *extent_root = fs_info->extent_root;
2732 int ret;
2733
2734 ret = find_next_chunk(fs_info->chunk_root,
2735 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07002736 if (ret)
2737 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002738
2739 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2740 (fs_info->metadata_alloc_profile &
2741 fs_info->avail_metadata_alloc_bits);
2742 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2743
2744 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2745 &stripe_size, chunk_offset, alloc_profile);
2746 BUG_ON(ret);
2747
2748 sys_chunk_offset = chunk_offset + chunk_size;
2749
2750 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2751 (fs_info->system_alloc_profile &
2752 fs_info->avail_system_alloc_bits);
2753 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2754
2755 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2756 &sys_chunk_size, &sys_stripe_size,
2757 sys_chunk_offset, alloc_profile);
2758 BUG_ON(ret);
2759
2760 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2761 BUG_ON(ret);
2762
2763 /*
2764 * Modifying chunk tree needs allocating new blocks from both
2765 * system block group and metadata block group. So we only can
2766 * do operations require modifying the chunk tree after both
2767 * block groups were created.
2768 */
2769 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2770 chunk_size, stripe_size);
2771 BUG_ON(ret);
2772
2773 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2774 sys_chunk_offset, sys_chunk_size,
2775 sys_stripe_size);
2776 BUG_ON(ret);
2777 return 0;
2778}
2779
2780int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2781{
2782 struct extent_map *em;
2783 struct map_lookup *map;
2784 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2785 int readonly = 0;
2786 int i;
2787
Chris Mason890871b2009-09-02 16:24:52 -04002788 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002789 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002790 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002791 if (!em)
2792 return 1;
2793
Josef Bacikf48b9072010-01-27 02:07:59 +00002794 if (btrfs_test_opt(root, DEGRADED)) {
2795 free_extent_map(em);
2796 return 0;
2797 }
2798
Yan Zheng2b820322008-11-17 21:11:30 -05002799 map = (struct map_lookup *)em->bdev;
2800 for (i = 0; i < map->num_stripes; i++) {
2801 if (!map->stripes[i].dev->writeable) {
2802 readonly = 1;
2803 break;
2804 }
2805 }
2806 free_extent_map(em);
2807 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002808}
2809
2810void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2811{
David Sterbaa8067e02011-04-21 00:34:43 +02002812 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002813}
2814
2815void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2816{
2817 struct extent_map *em;
2818
Chris Masond3977122009-01-05 21:25:51 -05002819 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002820 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002821 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2822 if (em)
2823 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002824 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002825 if (!em)
2826 break;
2827 kfree(em->bdev);
2828 /* once for us */
2829 free_extent_map(em);
2830 /* once for the tree */
2831 free_extent_map(em);
2832 }
2833}
2834
Chris Masonf1885912008-04-09 16:28:12 -04002835int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2836{
2837 struct extent_map *em;
2838 struct map_lookup *map;
2839 struct extent_map_tree *em_tree = &map_tree->map_tree;
2840 int ret;
2841
Chris Mason890871b2009-09-02 16:24:52 -04002842 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002843 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002844 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002845 BUG_ON(!em);
2846
2847 BUG_ON(em->start > logical || em->start + em->len < logical);
2848 map = (struct map_lookup *)em->bdev;
2849 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2850 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002851 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2852 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002853 else
2854 ret = 1;
2855 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002856 return ret;
2857}
2858
Chris Masondfe25022008-05-13 13:46:40 -04002859static int find_live_mirror(struct map_lookup *map, int first, int num,
2860 int optimal)
2861{
2862 int i;
2863 if (map->stripes[optimal].dev->bdev)
2864 return optimal;
2865 for (i = first; i < first + num; i++) {
2866 if (map->stripes[i].dev->bdev)
2867 return i;
2868 }
2869 /* we couldn't find one that doesn't fail. Just return something
2870 * and the io error handling code will clean up eventually
2871 */
2872 return optimal;
2873}
2874
Chris Masonf2d8d742008-04-21 10:03:05 -04002875static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2876 u64 logical, u64 *length,
2877 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002878 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002879{
2880 struct extent_map *em;
2881 struct map_lookup *map;
2882 struct extent_map_tree *em_tree = &map_tree->map_tree;
2883 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002884 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002885 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002886 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002887 u64 stripe_nr_orig;
2888 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002889 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002890 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002891 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002892 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002893 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002894 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002895 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002896
Li Dongyangfce3bb92011-03-24 10:24:26 +00002897 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002898 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002899again:
2900 if (multi_ret) {
2901 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2902 GFP_NOFS);
2903 if (!multi)
2904 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002905
2906 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002907 }
Chris Mason0b86a832008-03-24 15:01:56 -04002908
Chris Mason890871b2009-09-02 16:24:52 -04002909 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002910 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002911 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002912
Chris Mason3b951512008-04-17 11:29:12 -04002913 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002914 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2915 (unsigned long long)logical,
2916 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002917 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002918 }
Chris Mason0b86a832008-03-24 15:01:56 -04002919
2920 BUG_ON(em->start > logical || em->start + em->len < logical);
2921 map = (struct map_lookup *)em->bdev;
2922 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002923
Chris Masonf1885912008-04-09 16:28:12 -04002924 if (mirror_num > map->num_stripes)
2925 mirror_num = 0;
2926
Chris Masoncea9e442008-04-09 16:28:12 -04002927 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002928 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002929 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2930 BTRFS_BLOCK_GROUP_DUP)) {
2931 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002932 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002933 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2934 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002935 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002936 }
2937 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002938 if (rw & REQ_DISCARD) {
2939 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2940 BTRFS_BLOCK_GROUP_RAID1 |
2941 BTRFS_BLOCK_GROUP_DUP |
2942 BTRFS_BLOCK_GROUP_RAID10)) {
2943 stripes_required = map->num_stripes;
2944 }
2945 }
2946 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002947 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002948 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002949 free_extent_map(em);
2950 kfree(multi);
2951 goto again;
2952 }
Chris Mason593060d2008-03-25 16:50:33 -04002953 stripe_nr = offset;
2954 /*
2955 * stripe_nr counts the total number of stripes we have to stride
2956 * to get to this block
2957 */
2958 do_div(stripe_nr, map->stripe_len);
2959
2960 stripe_offset = stripe_nr * map->stripe_len;
2961 BUG_ON(offset < stripe_offset);
2962
2963 /* stripe_offset is the offset of this block in its stripe*/
2964 stripe_offset = offset - stripe_offset;
2965
Li Dongyangfce3bb92011-03-24 10:24:26 +00002966 if (rw & REQ_DISCARD)
2967 *length = min_t(u64, em->len - offset, *length);
2968 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2969 BTRFS_BLOCK_GROUP_RAID1 |
2970 BTRFS_BLOCK_GROUP_RAID10 |
2971 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002972 /* we limit the length of each bio to what fits in a stripe */
2973 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002974 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002975 } else {
2976 *length = em->len - offset;
2977 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002978
Jens Axboe7eaceac2011-03-10 08:52:07 +01002979 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002980 goto out;
2981
Chris Masonf2d8d742008-04-21 10:03:05 -04002982 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002983 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002984 stripe_nr_orig = stripe_nr;
2985 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2986 (~(map->stripe_len - 1));
2987 do_div(stripe_nr_end, map->stripe_len);
2988 stripe_end_offset = stripe_nr_end * map->stripe_len -
2989 (offset + *length);
2990 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2991 if (rw & REQ_DISCARD)
2992 num_stripes = min_t(u64, map->num_stripes,
2993 stripe_nr_end - stripe_nr_orig);
2994 stripe_index = do_div(stripe_nr, map->num_stripes);
2995 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07002996 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002997 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002998 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002999 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003000 else {
3001 stripe_index = find_live_mirror(map, 0,
3002 map->num_stripes,
3003 current->pid % map->num_stripes);
3004 }
Chris Mason2fff7342008-04-29 14:12:09 -04003005
Chris Mason611f0e02008-04-03 16:29:03 -04003006 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00003007 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003008 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003009 else if (mirror_num)
3010 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003011
Chris Mason321aecc2008-04-16 10:49:51 -04003012 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3013 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003014
3015 stripe_index = do_div(stripe_nr, factor);
3016 stripe_index *= map->sub_stripes;
3017
Jens Axboe7eaceac2011-03-10 08:52:07 +01003018 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003019 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003020 else if (rw & REQ_DISCARD)
3021 num_stripes = min_t(u64, map->sub_stripes *
3022 (stripe_nr_end - stripe_nr_orig),
3023 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003024 else if (mirror_num)
3025 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003026 else {
3027 stripe_index = find_live_mirror(map, stripe_index,
3028 map->sub_stripes, stripe_index +
3029 current->pid % map->sub_stripes);
3030 }
Chris Mason8790d502008-04-03 16:29:03 -04003031 } else {
3032 /*
3033 * after this do_div call, stripe_nr is the number of stripes
3034 * on this device we have to walk to find the data, and
3035 * stripe_index is the number of our device in the stripe array
3036 */
3037 stripe_index = do_div(stripe_nr, map->num_stripes);
3038 }
Chris Mason593060d2008-03-25 16:50:33 -04003039 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003040
Li Dongyangfce3bb92011-03-24 10:24:26 +00003041 if (rw & REQ_DISCARD) {
3042 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003043 multi->stripes[i].physical =
3044 map->stripes[stripe_index].physical +
3045 stripe_offset + stripe_nr * map->stripe_len;
3046 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003047
3048 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3049 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003050 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003051 int j;
3052
Chris Masond9d04872011-03-27 21:23:21 -04003053 div_u64_rem(stripe_nr_end - 1,
3054 map->num_stripes,
3055 &last_stripe);
3056
Li Dongyangfce3bb92011-03-24 10:24:26 +00003057 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003058 u32 test;
3059
3060 div_u64_rem(stripe_nr_end - 1 - j,
3061 map->num_stripes, &test);
3062 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003063 break;
3064 }
3065 stripes = stripe_nr_end - 1 - j;
3066 do_div(stripes, map->num_stripes);
3067 multi->stripes[i].length = map->stripe_len *
3068 (stripes - stripe_nr + 1);
3069
3070 if (i == 0) {
3071 multi->stripes[i].length -=
3072 stripe_offset;
3073 stripe_offset = 0;
3074 }
3075 if (stripe_index == last_stripe)
3076 multi->stripes[i].length -=
3077 stripe_end_offset;
3078 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3079 u64 stripes;
3080 int j;
3081 int factor = map->num_stripes /
3082 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003083 u32 last_stripe = 0;
3084
3085 div_u64_rem(stripe_nr_end - 1,
3086 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003087 last_stripe *= map->sub_stripes;
3088
3089 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003090 u32 test;
3091
3092 div_u64_rem(stripe_nr_end - 1 - j,
3093 factor, &test);
3094
3095 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003096 stripe_index / map->sub_stripes)
3097 break;
3098 }
3099 stripes = stripe_nr_end - 1 - j;
3100 do_div(stripes, factor);
3101 multi->stripes[i].length = map->stripe_len *
3102 (stripes - stripe_nr + 1);
3103
3104 if (i < map->sub_stripes) {
3105 multi->stripes[i].length -=
3106 stripe_offset;
3107 if (i == map->sub_stripes - 1)
3108 stripe_offset = 0;
3109 }
3110 if (stripe_index >= last_stripe &&
3111 stripe_index <= (last_stripe +
3112 map->sub_stripes - 1)) {
3113 multi->stripes[i].length -=
3114 stripe_end_offset;
3115 }
3116 } else
3117 multi->stripes[i].length = *length;
3118
3119 stripe_index++;
3120 if (stripe_index == map->num_stripes) {
3121 /* This could only happen for RAID0/10 */
3122 stripe_index = 0;
3123 stripe_nr++;
3124 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003125 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003126 } else {
3127 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003128 multi->stripes[i].physical =
3129 map->stripes[stripe_index].physical +
3130 stripe_offset +
3131 stripe_nr * map->stripe_len;
3132 multi->stripes[i].dev =
3133 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003134 stripe_index++;
3135 }
Chris Mason593060d2008-03-25 16:50:33 -04003136 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003137 if (multi_ret) {
3138 *multi_ret = multi;
3139 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003140 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003141 }
Chris Masoncea9e442008-04-09 16:28:12 -04003142out:
Chris Mason0b86a832008-03-24 15:01:56 -04003143 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003144 return 0;
3145}
3146
Chris Masonf2d8d742008-04-21 10:03:05 -04003147int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3148 u64 logical, u64 *length,
3149 struct btrfs_multi_bio **multi_ret, int mirror_num)
3150{
3151 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003152 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003153}
3154
Yan Zhenga512bbf2008-12-08 16:46:26 -05003155int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3156 u64 chunk_start, u64 physical, u64 devid,
3157 u64 **logical, int *naddrs, int *stripe_len)
3158{
3159 struct extent_map_tree *em_tree = &map_tree->map_tree;
3160 struct extent_map *em;
3161 struct map_lookup *map;
3162 u64 *buf;
3163 u64 bytenr;
3164 u64 length;
3165 u64 stripe_nr;
3166 int i, j, nr = 0;
3167
Chris Mason890871b2009-09-02 16:24:52 -04003168 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003169 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003170 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003171
3172 BUG_ON(!em || em->start != chunk_start);
3173 map = (struct map_lookup *)em->bdev;
3174
3175 length = em->len;
3176 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3177 do_div(length, map->num_stripes / map->sub_stripes);
3178 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3179 do_div(length, map->num_stripes);
3180
3181 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3182 BUG_ON(!buf);
3183
3184 for (i = 0; i < map->num_stripes; i++) {
3185 if (devid && map->stripes[i].dev->devid != devid)
3186 continue;
3187 if (map->stripes[i].physical > physical ||
3188 map->stripes[i].physical + length <= physical)
3189 continue;
3190
3191 stripe_nr = physical - map->stripes[i].physical;
3192 do_div(stripe_nr, map->stripe_len);
3193
3194 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3195 stripe_nr = stripe_nr * map->num_stripes + i;
3196 do_div(stripe_nr, map->sub_stripes);
3197 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3198 stripe_nr = stripe_nr * map->num_stripes + i;
3199 }
3200 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003201 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003202 for (j = 0; j < nr; j++) {
3203 if (buf[j] == bytenr)
3204 break;
3205 }
Chris Mason934d3752008-12-08 16:43:10 -05003206 if (j == nr) {
3207 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003208 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003209 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003210 }
3211
Yan Zhenga512bbf2008-12-08 16:46:26 -05003212 *logical = buf;
3213 *naddrs = nr;
3214 *stripe_len = map->stripe_len;
3215
3216 free_extent_map(em);
3217 return 0;
3218}
3219
Chris Mason8790d502008-04-03 16:29:03 -04003220static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003221{
Chris Masoncea9e442008-04-09 16:28:12 -04003222 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003223 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003224
Chris Mason8790d502008-04-03 16:29:03 -04003225 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003226 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003227
Chris Mason7d2b4da2008-08-05 10:13:57 -04003228 if (bio == multi->orig_bio)
3229 is_orig_bio = 1;
3230
Chris Masoncea9e442008-04-09 16:28:12 -04003231 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003232 if (!is_orig_bio) {
3233 bio_put(bio);
3234 bio = multi->orig_bio;
3235 }
Chris Mason8790d502008-04-03 16:29:03 -04003236 bio->bi_private = multi->private;
3237 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003238 /* only send an error to the higher layers if it is
3239 * beyond the tolerance of the multi-bio
3240 */
Chris Mason1259ab72008-05-12 13:39:03 -04003241 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003242 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003243 } else if (err) {
3244 /*
3245 * this bio is actually up to date, we didn't
3246 * go over the max number of errors
3247 */
3248 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003249 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003250 }
Chris Mason8790d502008-04-03 16:29:03 -04003251 kfree(multi);
3252
3253 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003254 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003255 bio_put(bio);
3256 }
Chris Mason8790d502008-04-03 16:29:03 -04003257}
3258
Chris Mason8b712842008-06-11 16:50:36 -04003259struct async_sched {
3260 struct bio *bio;
3261 int rw;
3262 struct btrfs_fs_info *info;
3263 struct btrfs_work work;
3264};
3265
3266/*
3267 * see run_scheduled_bios for a description of why bios are collected for
3268 * async submit.
3269 *
3270 * This will add one bio to the pending list for a device and make sure
3271 * the work struct is scheduled.
3272 */
Chris Masond3977122009-01-05 21:25:51 -05003273static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003274 struct btrfs_device *device,
3275 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003276{
3277 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003278 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003279
3280 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003281 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04003282 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003283 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04003284 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003285 return 0;
3286 }
3287
3288 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003289 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003290 * higher layers. Otherwise, the async bio makes it appear we have
3291 * made progress against dirty pages when we've really just put it
3292 * on a queue for later
3293 */
Chris Mason0986fe92008-08-15 15:34:15 -04003294 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04003295 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003296 bio->bi_next = NULL;
3297 bio->bi_rw |= rw;
3298
3299 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003300 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003301 pending_bios = &device->pending_sync_bios;
3302 else
3303 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003304
Chris Masonffbd5172009-04-20 15:50:09 -04003305 if (pending_bios->tail)
3306 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003307
Chris Masonffbd5172009-04-20 15:50:09 -04003308 pending_bios->tail = bio;
3309 if (!pending_bios->head)
3310 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003311 if (device->running_pending)
3312 should_queue = 0;
3313
3314 spin_unlock(&device->io_lock);
3315
3316 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003317 btrfs_queue_worker(&root->fs_info->submit_workers,
3318 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003319 return 0;
3320}
3321
Chris Masonf1885912008-04-09 16:28:12 -04003322int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003323 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003324{
3325 struct btrfs_mapping_tree *map_tree;
3326 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003327 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003328 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003329 u64 length = 0;
3330 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003331 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003332 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003333 int dev_nr = 0;
3334 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003335
Chris Masonf2d8d742008-04-21 10:03:05 -04003336 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003337 map_tree = &root->fs_info->mapping_tree;
3338 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003339
Chris Masonf1885912008-04-09 16:28:12 -04003340 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3341 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003342 BUG_ON(ret);
3343
3344 total_devs = multi->num_stripes;
3345 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003346 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3347 "len %llu\n", (unsigned long long)logical,
3348 (unsigned long long)length,
3349 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003350 BUG();
3351 }
3352 multi->end_io = first_bio->bi_end_io;
3353 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003354 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003355 atomic_set(&multi->stripes_pending, multi->num_stripes);
3356
Chris Masond3977122009-01-05 21:25:51 -05003357 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003358 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003359 if (dev_nr < total_devs - 1) {
3360 bio = bio_clone(first_bio, GFP_NOFS);
3361 BUG_ON(!bio);
3362 } else {
3363 bio = first_bio;
3364 }
3365 bio->bi_private = multi;
3366 bio->bi_end_io = end_bio_multi_stripe;
3367 }
Chris Masoncea9e442008-04-09 16:28:12 -04003368 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3369 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003370 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003371 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003372 if (async_submit)
3373 schedule_bio(root, dev, rw, bio);
3374 else
3375 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003376 } else {
3377 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3378 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003379 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003380 }
Chris Mason8790d502008-04-03 16:29:03 -04003381 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003382 }
Chris Masoncea9e442008-04-09 16:28:12 -04003383 if (total_devs == 1)
3384 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003385 return 0;
3386}
3387
Chris Masona4437552008-04-18 10:29:38 -04003388struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003389 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003390{
Yan Zheng2b820322008-11-17 21:11:30 -05003391 struct btrfs_device *device;
3392 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003393
Yan Zheng2b820322008-11-17 21:11:30 -05003394 cur_devices = root->fs_info->fs_devices;
3395 while (cur_devices) {
3396 if (!fsid ||
3397 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3398 device = __find_device(&cur_devices->devices,
3399 devid, uuid);
3400 if (device)
3401 return device;
3402 }
3403 cur_devices = cur_devices->seed;
3404 }
3405 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003406}
3407
Chris Masondfe25022008-05-13 13:46:40 -04003408static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3409 u64 devid, u8 *dev_uuid)
3410{
3411 struct btrfs_device *device;
3412 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3413
3414 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003415 if (!device)
3416 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003417 list_add(&device->dev_list,
3418 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003419 device->dev_root = root->fs_info->dev_root;
3420 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003421 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003422 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003423 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003424 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003425 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003426 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003427 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003428 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3429 return device;
3430}
3431
Chris Mason0b86a832008-03-24 15:01:56 -04003432static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3433 struct extent_buffer *leaf,
3434 struct btrfs_chunk *chunk)
3435{
3436 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3437 struct map_lookup *map;
3438 struct extent_map *em;
3439 u64 logical;
3440 u64 length;
3441 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003442 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003443 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003444 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003445 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003446
Chris Masone17cade2008-04-15 15:41:47 -04003447 logical = key->offset;
3448 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003449
Chris Mason890871b2009-09-02 16:24:52 -04003450 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003451 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003452 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003453
3454 /* already mapped? */
3455 if (em && em->start <= logical && em->start + em->len > logical) {
3456 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003457 return 0;
3458 } else if (em) {
3459 free_extent_map(em);
3460 }
Chris Mason0b86a832008-03-24 15:01:56 -04003461
David Sterba172ddd62011-04-21 00:48:27 +02003462 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003463 if (!em)
3464 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003465 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3466 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003467 if (!map) {
3468 free_extent_map(em);
3469 return -ENOMEM;
3470 }
3471
3472 em->bdev = (struct block_device *)map;
3473 em->start = logical;
3474 em->len = length;
3475 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003476 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003477
Chris Mason593060d2008-03-25 16:50:33 -04003478 map->num_stripes = num_stripes;
3479 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3480 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3481 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3482 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3483 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003484 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003485 for (i = 0; i < num_stripes; i++) {
3486 map->stripes[i].physical =
3487 btrfs_stripe_offset_nr(leaf, chunk, i);
3488 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003489 read_extent_buffer(leaf, uuid, (unsigned long)
3490 btrfs_stripe_dev_uuid_nr(chunk, i),
3491 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003492 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3493 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003494 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003495 kfree(map);
3496 free_extent_map(em);
3497 return -EIO;
3498 }
Chris Masondfe25022008-05-13 13:46:40 -04003499 if (!map->stripes[i].dev) {
3500 map->stripes[i].dev =
3501 add_missing_dev(root, devid, uuid);
3502 if (!map->stripes[i].dev) {
3503 kfree(map);
3504 free_extent_map(em);
3505 return -EIO;
3506 }
3507 }
3508 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003509 }
3510
Chris Mason890871b2009-09-02 16:24:52 -04003511 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003512 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003513 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003514 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003515 free_extent_map(em);
3516
3517 return 0;
3518}
3519
3520static int fill_device_from_item(struct extent_buffer *leaf,
3521 struct btrfs_dev_item *dev_item,
3522 struct btrfs_device *device)
3523{
3524 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003525
3526 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003527 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3528 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003529 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3530 device->type = btrfs_device_type(leaf, dev_item);
3531 device->io_align = btrfs_device_io_align(leaf, dev_item);
3532 device->io_width = btrfs_device_io_width(leaf, dev_item);
3533 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003534
3535 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003536 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003537
Chris Mason0b86a832008-03-24 15:01:56 -04003538 return 0;
3539}
3540
Yan Zheng2b820322008-11-17 21:11:30 -05003541static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3542{
3543 struct btrfs_fs_devices *fs_devices;
3544 int ret;
3545
3546 mutex_lock(&uuid_mutex);
3547
3548 fs_devices = root->fs_info->fs_devices->seed;
3549 while (fs_devices) {
3550 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3551 ret = 0;
3552 goto out;
3553 }
3554 fs_devices = fs_devices->seed;
3555 }
3556
3557 fs_devices = find_fsid(fsid);
3558 if (!fs_devices) {
3559 ret = -ENOENT;
3560 goto out;
3561 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003562
3563 fs_devices = clone_fs_devices(fs_devices);
3564 if (IS_ERR(fs_devices)) {
3565 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003566 goto out;
3567 }
3568
Christoph Hellwig97288f22008-12-02 06:36:09 -05003569 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003570 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003571 if (ret)
3572 goto out;
3573
3574 if (!fs_devices->seeding) {
3575 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003576 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003577 ret = -EINVAL;
3578 goto out;
3579 }
3580
3581 fs_devices->seed = root->fs_info->fs_devices->seed;
3582 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003583out:
3584 mutex_unlock(&uuid_mutex);
3585 return ret;
3586}
3587
Chris Mason0d81ba52008-03-24 15:02:07 -04003588static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003589 struct extent_buffer *leaf,
3590 struct btrfs_dev_item *dev_item)
3591{
3592 struct btrfs_device *device;
3593 u64 devid;
3594 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003595 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003596 u8 dev_uuid[BTRFS_UUID_SIZE];
3597
Chris Mason0b86a832008-03-24 15:01:56 -04003598 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003599 read_extent_buffer(leaf, dev_uuid,
3600 (unsigned long)btrfs_device_uuid(dev_item),
3601 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003602 read_extent_buffer(leaf, fs_uuid,
3603 (unsigned long)btrfs_device_fsid(dev_item),
3604 BTRFS_UUID_SIZE);
3605
3606 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3607 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003608 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003609 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003610 }
3611
3612 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3613 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003614 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003615 return -EIO;
3616
3617 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003618 printk(KERN_WARNING "warning devid %llu missing\n",
3619 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003620 device = add_missing_dev(root, devid, dev_uuid);
3621 if (!device)
3622 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003623 } else if (!device->missing) {
3624 /*
3625 * this happens when a device that was properly setup
3626 * in the device info lists suddenly goes bad.
3627 * device->bdev is NULL, and so we have to set
3628 * device->missing to one here
3629 */
3630 root->fs_info->fs_devices->missing_devices++;
3631 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003632 }
3633 }
3634
3635 if (device->fs_devices != root->fs_info->fs_devices) {
3636 BUG_ON(device->writeable);
3637 if (device->generation !=
3638 btrfs_device_generation(leaf, dev_item))
3639 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003640 }
Chris Mason0b86a832008-03-24 15:01:56 -04003641
3642 fill_device_from_item(leaf, dev_item, device);
3643 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003644 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04003645 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003646 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04003647 spin_lock(&root->fs_info->free_chunk_lock);
3648 root->fs_info->free_chunk_space += device->total_bytes -
3649 device->bytes_used;
3650 spin_unlock(&root->fs_info->free_chunk_lock);
3651 }
Chris Mason0b86a832008-03-24 15:01:56 -04003652 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003653 return ret;
3654}
3655
Yan Zhenge4404d62008-12-12 10:03:26 -05003656int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003657{
3658 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003659 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003660 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003661 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003662 u8 *ptr;
3663 unsigned long sb_ptr;
3664 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003665 u32 num_stripes;
3666 u32 array_size;
3667 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003668 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003669 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003670
Yan Zhenge4404d62008-12-12 10:03:26 -05003671 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003672 BTRFS_SUPER_INFO_SIZE);
3673 if (!sb)
3674 return -ENOMEM;
3675 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04003676 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05003677
Chris Masona061fc82008-05-07 11:43:44 -04003678 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003679 array_size = btrfs_super_sys_array_size(super_copy);
3680
Chris Mason0b86a832008-03-24 15:01:56 -04003681 ptr = super_copy->sys_chunk_array;
3682 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3683 cur = 0;
3684
3685 while (cur < array_size) {
3686 disk_key = (struct btrfs_disk_key *)ptr;
3687 btrfs_disk_key_to_cpu(&key, disk_key);
3688
Chris Masona061fc82008-05-07 11:43:44 -04003689 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003690 sb_ptr += len;
3691 cur += len;
3692
Chris Mason0d81ba52008-03-24 15:02:07 -04003693 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003694 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003695 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003696 if (ret)
3697 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003698 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3699 len = btrfs_chunk_item_size(num_stripes);
3700 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003701 ret = -EIO;
3702 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003703 }
3704 ptr += len;
3705 sb_ptr += len;
3706 cur += len;
3707 }
Chris Masona061fc82008-05-07 11:43:44 -04003708 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003709 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003710}
3711
3712int btrfs_read_chunk_tree(struct btrfs_root *root)
3713{
3714 struct btrfs_path *path;
3715 struct extent_buffer *leaf;
3716 struct btrfs_key key;
3717 struct btrfs_key found_key;
3718 int ret;
3719 int slot;
3720
3721 root = root->fs_info->chunk_root;
3722
3723 path = btrfs_alloc_path();
3724 if (!path)
3725 return -ENOMEM;
3726
3727 /* first we search for all of the device items, and then we
3728 * read in all of the chunk items. This way we can create chunk
3729 * mappings that reference all of the devices that are afound
3730 */
3731 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3732 key.offset = 0;
3733 key.type = 0;
3734again:
3735 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003736 if (ret < 0)
3737 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003738 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003739 leaf = path->nodes[0];
3740 slot = path->slots[0];
3741 if (slot >= btrfs_header_nritems(leaf)) {
3742 ret = btrfs_next_leaf(root, path);
3743 if (ret == 0)
3744 continue;
3745 if (ret < 0)
3746 goto error;
3747 break;
3748 }
3749 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3750 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3751 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3752 break;
3753 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3754 struct btrfs_dev_item *dev_item;
3755 dev_item = btrfs_item_ptr(leaf, slot,
3756 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003757 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003758 if (ret)
3759 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003760 }
3761 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3762 struct btrfs_chunk *chunk;
3763 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3764 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003765 if (ret)
3766 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003767 }
3768 path->slots[0]++;
3769 }
3770 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3771 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003772 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003773 goto again;
3774 }
Chris Mason0b86a832008-03-24 15:01:56 -04003775 ret = 0;
3776error:
Yan Zheng2b820322008-11-17 21:11:30 -05003777 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003778 return ret;
3779}