blob: 3c5f2fcd82c1c0e9216a46964f73b3fd1fb97cef [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Chris Mason593060d2008-03-25 16:50:33 -040026#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "ctree.h"
29#include "extent_map.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "print-tree.h"
33#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Yan Zheng2b820322008-11-17 21:11:30 -050036static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
40
Chris Mason8a4b83c2008-03-24 15:02:07 -040041static DEFINE_MUTEX(uuid_mutex);
42static LIST_HEAD(fs_uuids);
43
Chris Mason7d9eb122008-07-08 14:19:17 -040044static void lock_chunks(struct btrfs_root *root)
45{
Chris Mason7d9eb122008-07-08 14:19:17 -040046 mutex_lock(&root->fs_info->chunk_mutex);
47}
48
49static void unlock_chunks(struct btrfs_root *root)
50{
Chris Mason7d9eb122008-07-08 14:19:17 -040051 mutex_unlock(&root->fs_info->chunk_mutex);
52}
53
Yan Zhenge4404d62008-12-12 10:03:26 -050054static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
55{
56 struct btrfs_device *device;
57 WARN_ON(fs_devices->opened);
58 while (!list_empty(&fs_devices->devices)) {
59 device = list_entry(fs_devices->devices.next,
60 struct btrfs_device, dev_list);
61 list_del(&device->dev_list);
62 kfree(device->name);
63 kfree(device);
64 }
65 kfree(fs_devices);
66}
67
Chris Mason8a4b83c2008-03-24 15:02:07 -040068int btrfs_cleanup_fs_uuids(void)
69{
70 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040071
Yan Zheng2b820322008-11-17 21:11:30 -050072 while (!list_empty(&fs_uuids)) {
73 fs_devices = list_entry(fs_uuids.next,
74 struct btrfs_fs_devices, list);
75 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050076 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040077 }
78 return 0;
79}
80
Chris Masona1b32a52008-09-05 16:09:51 -040081static noinline struct btrfs_device *__find_device(struct list_head *head,
82 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040083{
84 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040085
Qinghuang Fengc6e30872009-01-21 10:59:08 -050086 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040087 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040088 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040089 return dev;
Chris Masona4437552008-04-18 10:29:38 -040090 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 }
92 return NULL;
93}
94
Chris Masona1b32a52008-09-05 16:09:51 -040095static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040096{
Chris Mason8a4b83c2008-03-24 15:02:07 -040097 struct btrfs_fs_devices *fs_devices;
98
Qinghuang Fengc6e30872009-01-21 10:59:08 -050099 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400100 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
101 return fs_devices;
102 }
103 return NULL;
104}
105
Chris Masonffbd5172009-04-20 15:50:09 -0400106static void requeue_list(struct btrfs_pending_bios *pending_bios,
107 struct bio *head, struct bio *tail)
108{
109
110 struct bio *old_head;
111
112 old_head = pending_bios->head;
113 pending_bios->head = head;
114 if (pending_bios->tail)
115 tail->bi_next = old_head;
116 else
117 pending_bios->tail = tail;
118}
119
Chris Mason8b712842008-06-11 16:50:36 -0400120/*
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
124 *
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
130 */
Chris Masond3977122009-01-05 21:25:51 -0500131static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400132{
133 struct bio *pending;
134 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400135 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400136 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400137 struct bio *tail;
138 struct bio *cur;
139 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400140 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400141 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400142 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400143 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400144 int force_reg = 0;
Chris Mason2ab1ba62011-08-04 14:28:36 -0400145 int sync_pending;
Chris Mason211588a2011-04-19 20:12:40 -0400146 struct blk_plug plug;
147
148 /*
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
153 */
154 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400155
Chris Masonbedf7622009-04-03 10:32:58 -0400156 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400157 fs_info = device->dev_root->fs_info;
158 limit = btrfs_async_submit_limit(fs_info);
159 limit = limit * 2 / 3;
160
Chris Mason8b712842008-06-11 16:50:36 -0400161loop:
162 spin_lock(&device->io_lock);
163
Chris Masona6837052009-02-04 09:19:41 -0500164loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400165 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400166
Chris Mason8b712842008-06-11 16:50:36 -0400167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
171 */
Chris Masond84275c2009-06-09 15:39:08 -0400172 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400173 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400174 force_reg = 1;
175 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400176 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400177 force_reg = 0;
178 }
Chris Masonffbd5172009-04-20 15:50:09 -0400179
180 pending = pending_bios->head;
181 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400182 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400183
184 /*
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
188 *
189 * device->running_pending is used to synchronize with the
190 * schedule_bio code.
191 */
Chris Masonffbd5172009-04-20 15:50:09 -0400192 if (device->pending_sync_bios.head == NULL &&
193 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400194 again = 0;
195 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400196 } else {
197 again = 1;
198 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400199 }
Chris Masonffbd5172009-04-20 15:50:09 -0400200
201 pending_bios->head = NULL;
202 pending_bios->tail = NULL;
203
Chris Mason8b712842008-06-11 16:50:36 -0400204 spin_unlock(&device->io_lock);
205
Chris Masond3977122009-01-05 21:25:51 -0500206 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400207
208 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
211 */
212 if ((num_run > 32 &&
213 pending_bios != &device->pending_sync_bios &&
214 device->pending_sync_bios.head) ||
215 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
216 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400217 spin_lock(&device->io_lock);
218 requeue_list(pending_bios, pending, tail);
219 goto loop_lock;
220 }
221
Chris Mason8b712842008-06-11 16:50:36 -0400222 cur = pending;
223 pending = pending->bi_next;
224 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400225 atomic_dec(&fs_info->nr_async_bios);
226
227 if (atomic_read(&fs_info->nr_async_bios) < limit &&
228 waitqueue_active(&fs_info->async_submit_wait))
229 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400230
231 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400232
Chris Mason2ab1ba62011-08-04 14:28:36 -0400233 /*
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
236 *
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
239 * we add more
240 */
241 if (pending_bios == &device->pending_sync_bios) {
242 sync_pending = 1;
243 } else if (sync_pending) {
244 blk_finish_plug(&plug);
245 blk_start_plug(&plug);
246 sync_pending = 0;
247 }
248
Chris Mason5ff7ba32010-03-15 10:21:30 -0400249 submit_bio(cur->bi_rw, cur);
250 num_run++;
251 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100252 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400253 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400254
255 /*
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
258 * run instead
259 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400260 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500261 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400262 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400263
Chris Masonb765ead2009-04-03 10:27:10 -0400264 ioc = current->io_context;
265
266 /*
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
270 *
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
274 */
275 if (ioc && ioc->nr_batch_requests > 0 &&
276 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
277 (last_waited == 0 ||
278 ioc->last_waited == last_waited)) {
279 /*
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
284 */
285 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100286 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400287 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400288 continue;
289 }
Chris Mason8b712842008-06-11 16:50:36 -0400290 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400291 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500292 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400293
294 spin_unlock(&device->io_lock);
295 btrfs_requeue_work(&device->work);
296 goto done;
297 }
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
Xiao Guangrong1f781602011-04-20 10:09:16 +0000520 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
521 BUG_ON(!new_device);
522 memcpy(new_device, device, sizeof(*new_device));
523 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000524 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000525 new_device->bdev = NULL;
526 new_device->writeable = 0;
527 new_device->in_fs_metadata = 0;
528 list_replace_rcu(&device->dev_list, &new_device->dev_list);
529
530 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000532 mutex_unlock(&fs_devices->device_list_mutex);
533
Yan Zhenge4404d62008-12-12 10:03:26 -0500534 WARN_ON(fs_devices->open_devices);
535 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500536 fs_devices->opened = 0;
537 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500538
Chris Mason8a4b83c2008-03-24 15:02:07 -0400539 return 0;
540}
541
Yan Zheng2b820322008-11-17 21:11:30 -0500542int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
543{
Yan Zhenge4404d62008-12-12 10:03:26 -0500544 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500545 int ret;
546
547 mutex_lock(&uuid_mutex);
548 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500549 if (!fs_devices->opened) {
550 seed_devices = fs_devices->seed;
551 fs_devices->seed = NULL;
552 }
Yan Zheng2b820322008-11-17 21:11:30 -0500553 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500554
555 while (seed_devices) {
556 fs_devices = seed_devices;
557 seed_devices = fs_devices->seed;
558 __btrfs_close_devices(fs_devices);
559 free_fs_devices(fs_devices);
560 }
Yan Zheng2b820322008-11-17 21:11:30 -0500561 return ret;
562}
563
Yan Zhenge4404d62008-12-12 10:03:26 -0500564static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
565 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400566{
567 struct block_device *bdev;
568 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400569 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400570 struct block_device *latest_bdev = NULL;
571 struct buffer_head *bh;
572 struct btrfs_super_block *disk_super;
573 u64 latest_devid = 0;
574 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400575 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500576 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400577 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578
Tejun Heod4d77622010-11-13 11:55:18 +0100579 flags |= FMODE_EXCL;
580
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500581 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400582 if (device->bdev)
583 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400584 if (!device->name)
585 continue;
586
Tejun Heod4d77622010-11-13 11:55:18 +0100587 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500589 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400590 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400591 }
Chris Masona061fc82008-05-07 11:43:44 -0400592 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400593
Yan Zhenga512bbf2008-12-08 16:46:26 -0500594 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000595 if (!bh) {
596 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400597 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000598 }
Chris Masona0af4692008-05-13 16:03:06 -0400599
600 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000601 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400602 if (devid != device->devid)
603 goto error_brelse;
604
Yan Zheng2b820322008-11-17 21:11:30 -0500605 if (memcmp(device->uuid, disk_super->dev_item.uuid,
606 BTRFS_UUID_SIZE))
607 goto error_brelse;
608
609 device->generation = btrfs_super_generation(disk_super);
610 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400611 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500612 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400613 latest_bdev = bdev;
614 }
615
Yan Zheng2b820322008-11-17 21:11:30 -0500616 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
617 device->writeable = 0;
618 } else {
619 device->writeable = !bdev_read_only(bdev);
620 seeding = 0;
621 }
622
Chris Mason8a4b83c2008-03-24 15:02:07 -0400623 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400624 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500625 device->mode = flags;
626
Chris Masonc2898112009-06-10 09:51:32 -0400627 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
628 fs_devices->rotating = 1;
629
Chris Masona0af4692008-05-13 16:03:06 -0400630 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500631 if (device->writeable) {
632 fs_devices->rw_devices++;
633 list_add(&device->dev_alloc_list,
634 &fs_devices->alloc_list);
635 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000636 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400637 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400638
Chris Masona0af4692008-05-13 16:03:06 -0400639error_brelse:
640 brelse(bh);
641error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100642 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400643error:
644 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400645 }
Chris Masona0af4692008-05-13 16:03:06 -0400646 if (fs_devices->open_devices == 0) {
647 ret = -EIO;
648 goto out;
649 }
Yan Zheng2b820322008-11-17 21:11:30 -0500650 fs_devices->seeding = seeding;
651 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400652 fs_devices->latest_bdev = latest_bdev;
653 fs_devices->latest_devid = latest_devid;
654 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500655 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400656out:
Yan Zheng2b820322008-11-17 21:11:30 -0500657 return ret;
658}
659
660int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500661 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500662{
663 int ret;
664
665 mutex_lock(&uuid_mutex);
666 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500667 fs_devices->opened++;
668 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500669 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500670 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500671 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400672 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400673 return ret;
674}
675
Christoph Hellwig97288f22008-12-02 06:36:09 -0500676int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400677 struct btrfs_fs_devices **fs_devices_ret)
678{
679 struct btrfs_super_block *disk_super;
680 struct block_device *bdev;
681 struct buffer_head *bh;
682 int ret;
683 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400684 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685
686 mutex_lock(&uuid_mutex);
687
Tejun Heod4d77622010-11-13 11:55:18 +0100688 flags |= FMODE_EXCL;
689 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400690
691 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400692 ret = PTR_ERR(bdev);
693 goto error;
694 }
695
696 ret = set_blocksize(bdev, 4096);
697 if (ret)
698 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500699 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400700 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000701 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400702 goto error_close;
703 }
704 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000705 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400706 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400707 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500708 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200709 else
710 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500711 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500712 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
714
Chris Mason8a4b83c2008-03-24 15:02:07 -0400715 brelse(bh);
716error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100717 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400718error:
719 mutex_unlock(&uuid_mutex);
720 return ret;
721}
Chris Mason0b86a832008-03-24 15:01:56 -0400722
Miao Xie6d07bce2011-01-05 10:07:31 +0000723/* helper to account the used device space in the range */
724int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
725 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400726{
727 struct btrfs_key key;
728 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000729 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500730 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000731 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400732 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000733 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400734 struct extent_buffer *l;
735
Miao Xie6d07bce2011-01-05 10:07:31 +0000736 *length = 0;
737
738 if (start >= device->total_bytes)
739 return 0;
740
Yan Zheng2b820322008-11-17 21:11:30 -0500741 path = btrfs_alloc_path();
742 if (!path)
743 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400744 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400745
Chris Mason0b86a832008-03-24 15:01:56 -0400746 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000747 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400748 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000749
750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400751 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000752 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400753 if (ret > 0) {
754 ret = btrfs_previous_item(root, path, key.objectid, key.type);
755 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000756 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400757 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000758
Chris Mason0b86a832008-03-24 15:01:56 -0400759 while (1) {
760 l = path->nodes[0];
761 slot = path->slots[0];
762 if (slot >= btrfs_header_nritems(l)) {
763 ret = btrfs_next_leaf(root, path);
764 if (ret == 0)
765 continue;
766 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000767 goto out;
768
769 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400770 }
771 btrfs_item_key_to_cpu(l, &key, slot);
772
773 if (key.objectid < device->devid)
774 goto next;
775
776 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000777 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400778
Chris Masond3977122009-01-05 21:25:51 -0500779 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400780 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400781
Chris Mason0b86a832008-03-24 15:01:56 -0400782 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000783 extent_end = key.offset + btrfs_dev_extent_length(l,
784 dev_extent);
785 if (key.offset <= start && extent_end > end) {
786 *length = end - start + 1;
787 break;
788 } else if (key.offset <= start && extent_end > start)
789 *length += extent_end - start;
790 else if (key.offset > start && extent_end <= end)
791 *length += extent_end - key.offset;
792 else if (key.offset > start && key.offset <= end) {
793 *length += end - key.offset + 1;
794 break;
795 } else if (key.offset > end)
796 break;
797
798next:
799 path->slots[0]++;
800 }
801 ret = 0;
802out:
803 btrfs_free_path(path);
804 return ret;
805}
806
Chris Mason0b86a832008-03-24 15:01:56 -0400807/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000808 * find_free_dev_extent - find free space in the specified device
809 * @trans: transaction handler
810 * @device: the device which we search the free space in
811 * @num_bytes: the size of the free space that we need
812 * @start: store the start of the free space.
813 * @len: the size of the free space. that we find, or the size of the max
814 * free space if we don't find suitable free space
815 *
Chris Mason0b86a832008-03-24 15:01:56 -0400816 * this uses a pretty simple search, the expectation is that it is
817 * called very infrequently and that a given device has a small number
818 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000819 *
820 * @start is used to store the start of the free space if we find. But if we
821 * don't find suitable free space, it will be used to store the start position
822 * of the max free space.
823 *
824 * @len is used to store the size of the free space that we find.
825 * But if we don't find suitable free space, it is used to store the size of
826 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400827 */
828int find_free_dev_extent(struct btrfs_trans_handle *trans,
829 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000830 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400831{
832 struct btrfs_key key;
833 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000834 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400835 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000836 u64 hole_size;
837 u64 max_hole_start;
838 u64 max_hole_size;
839 u64 extent_end;
840 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400841 u64 search_end = device->total_bytes;
842 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000843 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400844 struct extent_buffer *l;
845
Chris Mason0b86a832008-03-24 15:01:56 -0400846 /* FIXME use last free of some kind */
847
848 /* we don't want to overwrite the superblock on the drive,
849 * so we make sure to start at an offset of at least 1MB
850 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200851 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400852
Miao Xie7bfc8372011-01-05 10:07:26 +0000853 max_hole_start = search_start;
854 max_hole_size = 0;
855
856 if (search_start >= search_end) {
857 ret = -ENOSPC;
858 goto error;
859 }
860
861 path = btrfs_alloc_path();
862 if (!path) {
863 ret = -ENOMEM;
864 goto error;
865 }
866 path->reada = 2;
867
Chris Mason0b86a832008-03-24 15:01:56 -0400868 key.objectid = device->devid;
869 key.offset = search_start;
870 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000871
Chris Mason0b86a832008-03-24 15:01:56 -0400872 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
873 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000874 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400875 if (ret > 0) {
876 ret = btrfs_previous_item(root, path, key.objectid, key.type);
877 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000878 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400879 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000880
Chris Mason0b86a832008-03-24 15:01:56 -0400881 while (1) {
882 l = path->nodes[0];
883 slot = path->slots[0];
884 if (slot >= btrfs_header_nritems(l)) {
885 ret = btrfs_next_leaf(root, path);
886 if (ret == 0)
887 continue;
888 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000889 goto out;
890
891 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400892 }
893 btrfs_item_key_to_cpu(l, &key, slot);
894
895 if (key.objectid < device->devid)
896 goto next;
897
898 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000899 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400900
Chris Mason0b86a832008-03-24 15:01:56 -0400901 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
902 goto next;
903
Miao Xie7bfc8372011-01-05 10:07:26 +0000904 if (key.offset > search_start) {
905 hole_size = key.offset - search_start;
906
907 if (hole_size > max_hole_size) {
908 max_hole_start = search_start;
909 max_hole_size = hole_size;
910 }
911
912 /*
913 * If this free space is greater than which we need,
914 * it must be the max free space that we have found
915 * until now, so max_hole_start must point to the start
916 * of this free space and the length of this free space
917 * is stored in max_hole_size. Thus, we return
918 * max_hole_start and max_hole_size and go back to the
919 * caller.
920 */
921 if (hole_size >= num_bytes) {
922 ret = 0;
923 goto out;
924 }
925 }
926
Chris Mason0b86a832008-03-24 15:01:56 -0400927 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000928 extent_end = key.offset + btrfs_dev_extent_length(l,
929 dev_extent);
930 if (extent_end > search_start)
931 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400932next:
933 path->slots[0]++;
934 cond_resched();
935 }
Chris Mason0b86a832008-03-24 15:01:56 -0400936
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 hole_size = search_end- search_start;
938 if (hole_size > max_hole_size) {
939 max_hole_start = search_start;
940 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400941 }
Chris Mason0b86a832008-03-24 15:01:56 -0400942
Miao Xie7bfc8372011-01-05 10:07:26 +0000943 /* See above. */
944 if (hole_size < num_bytes)
945 ret = -ENOSPC;
946 else
947 ret = 0;
948
949out:
Yan Zheng2b820322008-11-17 21:11:30 -0500950 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000951error:
952 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000953 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000954 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400955 return ret;
956}
957
Christoph Hellwigb2950862008-12-02 09:54:17 -0500958static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400959 struct btrfs_device *device,
960 u64 start)
961{
962 int ret;
963 struct btrfs_path *path;
964 struct btrfs_root *root = device->dev_root;
965 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400966 struct btrfs_key found_key;
967 struct extent_buffer *leaf = NULL;
968 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400969
970 path = btrfs_alloc_path();
971 if (!path)
972 return -ENOMEM;
973
974 key.objectid = device->devid;
975 key.offset = start;
976 key.type = BTRFS_DEV_EXTENT_KEY;
977
978 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400979 if (ret > 0) {
980 ret = btrfs_previous_item(root, path, key.objectid,
981 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +0000982 if (ret)
983 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400984 leaf = path->nodes[0];
985 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
986 extent = btrfs_item_ptr(leaf, path->slots[0],
987 struct btrfs_dev_extent);
988 BUG_ON(found_key.offset > start || found_key.offset +
989 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -0400990 } else if (ret == 0) {
991 leaf = path->nodes[0];
992 extent = btrfs_item_ptr(leaf, path->slots[0],
993 struct btrfs_dev_extent);
994 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400995 BUG_ON(ret);
996
Chris Masondfe25022008-05-13 13:46:40 -0400997 if (device->bytes_used > 0)
998 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400999 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001000
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001001out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001002 btrfs_free_path(path);
1003 return ret;
1004}
1005
Yan Zheng2b820322008-11-17 21:11:30 -05001006int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001007 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001008 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001009 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001010{
1011 int ret;
1012 struct btrfs_path *path;
1013 struct btrfs_root *root = device->dev_root;
1014 struct btrfs_dev_extent *extent;
1015 struct extent_buffer *leaf;
1016 struct btrfs_key key;
1017
Chris Masondfe25022008-05-13 13:46:40 -04001018 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001019 path = btrfs_alloc_path();
1020 if (!path)
1021 return -ENOMEM;
1022
Chris Mason0b86a832008-03-24 15:01:56 -04001023 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001024 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001025 key.type = BTRFS_DEV_EXTENT_KEY;
1026 ret = btrfs_insert_empty_item(trans, root, path, &key,
1027 sizeof(*extent));
1028 BUG_ON(ret);
1029
1030 leaf = path->nodes[0];
1031 extent = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001033 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1034 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1035 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1036
1037 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1038 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1039 BTRFS_UUID_SIZE);
1040
Chris Mason0b86a832008-03-24 15:01:56 -04001041 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1042 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001043 btrfs_free_path(path);
1044 return ret;
1045}
1046
Chris Masona1b32a52008-09-05 16:09:51 -04001047static noinline int find_next_chunk(struct btrfs_root *root,
1048 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001049{
1050 struct btrfs_path *path;
1051 int ret;
1052 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001053 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001054 struct btrfs_key found_key;
1055
1056 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001057 if (!path)
1058 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001059
Chris Masone17cade2008-04-15 15:41:47 -04001060 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001061 key.offset = (u64)-1;
1062 key.type = BTRFS_CHUNK_ITEM_KEY;
1063
1064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1065 if (ret < 0)
1066 goto error;
1067
1068 BUG_ON(ret == 0);
1069
1070 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1071 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001072 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001073 } else {
1074 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1075 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001076 if (found_key.objectid != objectid)
1077 *offset = 0;
1078 else {
1079 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1080 struct btrfs_chunk);
1081 *offset = found_key.offset +
1082 btrfs_chunk_length(path->nodes[0], chunk);
1083 }
Chris Mason0b86a832008-03-24 15:01:56 -04001084 }
1085 ret = 0;
1086error:
1087 btrfs_free_path(path);
1088 return ret;
1089}
1090
Yan Zheng2b820322008-11-17 21:11:30 -05001091static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001092{
1093 int ret;
1094 struct btrfs_key key;
1095 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001096 struct btrfs_path *path;
1097
1098 root = root->fs_info->chunk_root;
1099
1100 path = btrfs_alloc_path();
1101 if (!path)
1102 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001103
1104 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1105 key.type = BTRFS_DEV_ITEM_KEY;
1106 key.offset = (u64)-1;
1107
1108 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1109 if (ret < 0)
1110 goto error;
1111
1112 BUG_ON(ret == 0);
1113
1114 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1115 BTRFS_DEV_ITEM_KEY);
1116 if (ret) {
1117 *objectid = 1;
1118 } else {
1119 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1120 path->slots[0]);
1121 *objectid = found_key.offset + 1;
1122 }
1123 ret = 0;
1124error:
Yan Zheng2b820322008-11-17 21:11:30 -05001125 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001126 return ret;
1127}
1128
1129/*
1130 * the device information is stored in the chunk root
1131 * the btrfs_device struct should be fully filled in
1132 */
1133int btrfs_add_device(struct btrfs_trans_handle *trans,
1134 struct btrfs_root *root,
1135 struct btrfs_device *device)
1136{
1137 int ret;
1138 struct btrfs_path *path;
1139 struct btrfs_dev_item *dev_item;
1140 struct extent_buffer *leaf;
1141 struct btrfs_key key;
1142 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001143
1144 root = root->fs_info->chunk_root;
1145
1146 path = btrfs_alloc_path();
1147 if (!path)
1148 return -ENOMEM;
1149
Chris Mason0b86a832008-03-24 15:01:56 -04001150 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1151 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001152 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001153
1154 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001155 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001156 if (ret)
1157 goto out;
1158
1159 leaf = path->nodes[0];
1160 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1161
1162 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001163 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001164 btrfs_set_device_type(leaf, dev_item, device->type);
1165 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1166 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1167 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001168 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1169 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001170 btrfs_set_device_group(leaf, dev_item, 0);
1171 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1172 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001173 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001174
Chris Mason0b86a832008-03-24 15:01:56 -04001175 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001176 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001177 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1178 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001179 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001180
Yan Zheng2b820322008-11-17 21:11:30 -05001181 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001182out:
1183 btrfs_free_path(path);
1184 return ret;
1185}
Chris Mason8f18cf12008-04-25 16:53:30 -04001186
Chris Masona061fc82008-05-07 11:43:44 -04001187static int btrfs_rm_dev_item(struct btrfs_root *root,
1188 struct btrfs_device *device)
1189{
1190 int ret;
1191 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001192 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001193 struct btrfs_trans_handle *trans;
1194
1195 root = root->fs_info->chunk_root;
1196
1197 path = btrfs_alloc_path();
1198 if (!path)
1199 return -ENOMEM;
1200
Yan, Zhenga22285a2010-05-16 10:48:46 -04001201 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001202 if (IS_ERR(trans)) {
1203 btrfs_free_path(path);
1204 return PTR_ERR(trans);
1205 }
Chris Masona061fc82008-05-07 11:43:44 -04001206 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1207 key.type = BTRFS_DEV_ITEM_KEY;
1208 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001209 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001210
1211 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1212 if (ret < 0)
1213 goto out;
1214
1215 if (ret > 0) {
1216 ret = -ENOENT;
1217 goto out;
1218 }
1219
1220 ret = btrfs_del_item(trans, root, path);
1221 if (ret)
1222 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001223out:
1224 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001225 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001226 btrfs_commit_transaction(trans, root);
1227 return ret;
1228}
1229
1230int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1231{
1232 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001233 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001234 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001235 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001236 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001237 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001238 u64 all_avail;
1239 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001240 u64 num_devices;
1241 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001242 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001243 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001244
Chris Masona061fc82008-05-07 11:43:44 -04001245 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001246 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001247
1248 all_avail = root->fs_info->avail_data_alloc_bits |
1249 root->fs_info->avail_system_alloc_bits |
1250 root->fs_info->avail_metadata_alloc_bits;
1251
1252 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001253 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001254 printk(KERN_ERR "btrfs: unable to go below four devices "
1255 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001256 ret = -EINVAL;
1257 goto out;
1258 }
1259
1260 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001261 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001262 printk(KERN_ERR "btrfs: unable to go below two "
1263 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001264 ret = -EINVAL;
1265 goto out;
1266 }
1267
Chris Masondfe25022008-05-13 13:46:40 -04001268 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001269 struct list_head *devices;
1270 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001271
Chris Masondfe25022008-05-13 13:46:40 -04001272 device = NULL;
1273 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001274 /*
1275 * It is safe to read the devices since the volume_mutex
1276 * is held.
1277 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001278 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001279 if (tmp->in_fs_metadata && !tmp->bdev) {
1280 device = tmp;
1281 break;
1282 }
1283 }
1284 bdev = NULL;
1285 bh = NULL;
1286 disk_super = NULL;
1287 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001288 printk(KERN_ERR "btrfs: no missing devices found to "
1289 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001290 goto out;
1291 }
Chris Masondfe25022008-05-13 13:46:40 -04001292 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001293 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1294 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001295 if (IS_ERR(bdev)) {
1296 ret = PTR_ERR(bdev);
1297 goto out;
1298 }
1299
Yan Zheng2b820322008-11-17 21:11:30 -05001300 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001301 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001302 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001303 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001304 goto error_close;
1305 }
1306 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001307 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001308 dev_uuid = disk_super->dev_item.uuid;
1309 device = btrfs_find_device(root, devid, dev_uuid,
1310 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001311 if (!device) {
1312 ret = -ENOENT;
1313 goto error_brelse;
1314 }
Chris Masondfe25022008-05-13 13:46:40 -04001315 }
Yan Zheng2b820322008-11-17 21:11:30 -05001316
1317 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001318 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1319 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001320 ret = -EINVAL;
1321 goto error_brelse;
1322 }
1323
1324 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001325 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001326 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001327 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001328 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001329 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001330 }
Chris Masona061fc82008-05-07 11:43:44 -04001331
1332 ret = btrfs_shrink_device(device, 0);
1333 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001334 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001335
Chris Masona061fc82008-05-07 11:43:44 -04001336 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1337 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001338 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001339
Yan Zheng2b820322008-11-17 21:11:30 -05001340 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001341 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001342
1343 /*
1344 * the device list mutex makes sure that we don't change
1345 * the device list while someone else is writing out all
1346 * the device supers.
1347 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001348
1349 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001350 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001351 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001352
Yan Zhenge4404d62008-12-12 10:03:26 -05001353 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001354
Chris Masoncd02dca2010-12-13 14:56:23 -05001355 if (device->missing)
1356 root->fs_info->fs_devices->missing_devices--;
1357
Yan Zheng2b820322008-11-17 21:11:30 -05001358 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1359 struct btrfs_device, dev_list);
1360 if (device->bdev == root->fs_info->sb->s_bdev)
1361 root->fs_info->sb->s_bdev = next_device->bdev;
1362 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1363 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1364
Xiao Guangrong1f781602011-04-20 10:09:16 +00001365 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001366 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001367
1368 call_rcu(&device->rcu, free_device);
1369 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001370
Yan Zheng2b820322008-11-17 21:11:30 -05001371 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1372 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1373
Xiao Guangrong1f781602011-04-20 10:09:16 +00001374 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001375 struct btrfs_fs_devices *fs_devices;
1376 fs_devices = root->fs_info->fs_devices;
1377 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001378 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001379 break;
1380 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001381 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001382 fs_devices->seed = cur_devices->seed;
1383 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001384 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001385 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001386 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001387 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001388 }
1389
1390 /*
1391 * at this point, the device is zero sized. We want to
1392 * remove it from the devices list and zero out the old super
1393 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001394 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001395 /* make sure this device isn't detected as part of
1396 * the FS anymore
1397 */
1398 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1399 set_buffer_dirty(bh);
1400 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001401 }
Chris Masona061fc82008-05-07 11:43:44 -04001402
Chris Masona061fc82008-05-07 11:43:44 -04001403 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001404
1405error_brelse:
1406 brelse(bh);
1407error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001408 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001409 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001410out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001411 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001412 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001413 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001414error_undo:
1415 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001416 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001417 list_add(&device->dev_alloc_list,
1418 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001419 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001420 root->fs_info->fs_devices->rw_devices++;
1421 }
1422 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001423}
1424
Yan Zheng2b820322008-11-17 21:11:30 -05001425/*
1426 * does all the dirty work required for changing file system's UUID.
1427 */
1428static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1429 struct btrfs_root *root)
1430{
1431 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1432 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001433 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001434 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1435 struct btrfs_device *device;
1436 u64 super_flags;
1437
1438 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001439 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001440 return -EINVAL;
1441
Yan Zhenge4404d62008-12-12 10:03:26 -05001442 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1443 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001444 return -ENOMEM;
1445
Yan Zhenge4404d62008-12-12 10:03:26 -05001446 old_devices = clone_fs_devices(fs_devices);
1447 if (IS_ERR(old_devices)) {
1448 kfree(seed_devices);
1449 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001450 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001451
Yan Zheng2b820322008-11-17 21:11:30 -05001452 list_add(&old_devices->list, &fs_uuids);
1453
Yan Zhenge4404d62008-12-12 10:03:26 -05001454 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1455 seed_devices->opened = 1;
1456 INIT_LIST_HEAD(&seed_devices->devices);
1457 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001458 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001459
1460 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001461 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1462 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001463 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1464
Yan Zhenge4404d62008-12-12 10:03:26 -05001465 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1466 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1467 device->fs_devices = seed_devices;
1468 }
1469
Yan Zheng2b820322008-11-17 21:11:30 -05001470 fs_devices->seeding = 0;
1471 fs_devices->num_devices = 0;
1472 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001473 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001474
1475 generate_random_uuid(fs_devices->fsid);
1476 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1477 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1478 super_flags = btrfs_super_flags(disk_super) &
1479 ~BTRFS_SUPER_FLAG_SEEDING;
1480 btrfs_set_super_flags(disk_super, super_flags);
1481
1482 return 0;
1483}
1484
1485/*
1486 * strore the expected generation for seed devices in device items.
1487 */
1488static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1489 struct btrfs_root *root)
1490{
1491 struct btrfs_path *path;
1492 struct extent_buffer *leaf;
1493 struct btrfs_dev_item *dev_item;
1494 struct btrfs_device *device;
1495 struct btrfs_key key;
1496 u8 fs_uuid[BTRFS_UUID_SIZE];
1497 u8 dev_uuid[BTRFS_UUID_SIZE];
1498 u64 devid;
1499 int ret;
1500
1501 path = btrfs_alloc_path();
1502 if (!path)
1503 return -ENOMEM;
1504
1505 root = root->fs_info->chunk_root;
1506 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1507 key.offset = 0;
1508 key.type = BTRFS_DEV_ITEM_KEY;
1509
1510 while (1) {
1511 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1512 if (ret < 0)
1513 goto error;
1514
1515 leaf = path->nodes[0];
1516next_slot:
1517 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1518 ret = btrfs_next_leaf(root, path);
1519 if (ret > 0)
1520 break;
1521 if (ret < 0)
1522 goto error;
1523 leaf = path->nodes[0];
1524 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001525 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001526 continue;
1527 }
1528
1529 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1530 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1531 key.type != BTRFS_DEV_ITEM_KEY)
1532 break;
1533
1534 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1535 struct btrfs_dev_item);
1536 devid = btrfs_device_id(leaf, dev_item);
1537 read_extent_buffer(leaf, dev_uuid,
1538 (unsigned long)btrfs_device_uuid(dev_item),
1539 BTRFS_UUID_SIZE);
1540 read_extent_buffer(leaf, fs_uuid,
1541 (unsigned long)btrfs_device_fsid(dev_item),
1542 BTRFS_UUID_SIZE);
1543 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1544 BUG_ON(!device);
1545
1546 if (device->fs_devices->seeding) {
1547 btrfs_set_device_generation(leaf, dev_item,
1548 device->generation);
1549 btrfs_mark_buffer_dirty(leaf);
1550 }
1551
1552 path->slots[0]++;
1553 goto next_slot;
1554 }
1555 ret = 0;
1556error:
1557 btrfs_free_path(path);
1558 return ret;
1559}
1560
Chris Mason788f20e2008-04-28 15:29:42 -04001561int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1562{
1563 struct btrfs_trans_handle *trans;
1564 struct btrfs_device *device;
1565 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001566 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001567 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001568 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001569 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001570 int ret = 0;
1571
Yan Zheng2b820322008-11-17 21:11:30 -05001572 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1573 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001574
Tejun Heod4d77622010-11-13 11:55:18 +01001575 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1576 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001577 if (IS_ERR(bdev))
1578 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001579
Yan Zheng2b820322008-11-17 21:11:30 -05001580 if (root->fs_info->fs_devices->seeding) {
1581 seeding_dev = 1;
1582 down_write(&sb->s_umount);
1583 mutex_lock(&uuid_mutex);
1584 }
1585
Chris Mason8c8bee12008-09-29 11:19:10 -04001586 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001587 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001588
Chris Mason788f20e2008-04-28 15:29:42 -04001589 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001590 /*
1591 * we have the volume lock, so we don't need the extra
1592 * device list mutex while reading the list here.
1593 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001594 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001595 if (device->bdev == bdev) {
1596 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001597 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001598 }
1599 }
1600
1601 device = kzalloc(sizeof(*device), GFP_NOFS);
1602 if (!device) {
1603 /* we can safely leave the fs_devices entry around */
1604 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001605 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001606 }
1607
Chris Mason788f20e2008-04-28 15:29:42 -04001608 device->name = kstrdup(device_path, GFP_NOFS);
1609 if (!device->name) {
1610 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001611 ret = -ENOMEM;
1612 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001613 }
Yan Zheng2b820322008-11-17 21:11:30 -05001614
1615 ret = find_next_devid(root, &device->devid);
1616 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001617 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001618 kfree(device);
1619 goto error;
1620 }
1621
Yan, Zhenga22285a2010-05-16 10:48:46 -04001622 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001623 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001624 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001625 kfree(device);
1626 ret = PTR_ERR(trans);
1627 goto error;
1628 }
1629
Yan Zheng2b820322008-11-17 21:11:30 -05001630 lock_chunks(root);
1631
Yan Zheng2b820322008-11-17 21:11:30 -05001632 device->writeable = 1;
1633 device->work.func = pending_bios_fn;
1634 generate_random_uuid(device->uuid);
1635 spin_lock_init(&device->io_lock);
1636 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001637 device->io_width = root->sectorsize;
1638 device->io_align = root->sectorsize;
1639 device->sector_size = root->sectorsize;
1640 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001641 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001642 device->dev_root = root->fs_info->dev_root;
1643 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001644 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001645 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001646 set_blocksize(device->bdev, 4096);
1647
Yan Zheng2b820322008-11-17 21:11:30 -05001648 if (seeding_dev) {
1649 sb->s_flags &= ~MS_RDONLY;
1650 ret = btrfs_prepare_sprout(trans, root);
1651 BUG_ON(ret);
1652 }
1653
1654 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001655
1656 /*
1657 * we don't want write_supers to jump in here with our device
1658 * half setup
1659 */
1660 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001661 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001662 list_add(&device->dev_alloc_list,
1663 &root->fs_info->fs_devices->alloc_list);
1664 root->fs_info->fs_devices->num_devices++;
1665 root->fs_info->fs_devices->open_devices++;
1666 root->fs_info->fs_devices->rw_devices++;
1667 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1668
Chris Masonc2898112009-06-10 09:51:32 -04001669 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1670 root->fs_info->fs_devices->rotating = 1;
1671
Chris Mason788f20e2008-04-28 15:29:42 -04001672 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1673 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1674 total_bytes + device->total_bytes);
1675
1676 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1677 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1678 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001679 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001680
Yan Zheng2b820322008-11-17 21:11:30 -05001681 if (seeding_dev) {
1682 ret = init_first_rw_device(trans, root, device);
1683 BUG_ON(ret);
1684 ret = btrfs_finish_sprout(trans, root);
1685 BUG_ON(ret);
1686 } else {
1687 ret = btrfs_add_device(trans, root, device);
1688 }
1689
Chris Mason913d9522009-03-10 13:17:18 -04001690 /*
1691 * we've got more storage, clear any full flags on the space
1692 * infos
1693 */
1694 btrfs_clear_space_info_full(root->fs_info);
1695
Chris Mason7d9eb122008-07-08 14:19:17 -04001696 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001697 btrfs_commit_transaction(trans, root);
1698
1699 if (seeding_dev) {
1700 mutex_unlock(&uuid_mutex);
1701 up_write(&sb->s_umount);
1702
1703 ret = btrfs_relocate_sys_chunks(root);
1704 BUG_ON(ret);
1705 }
1706out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001707 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001708 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001709error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001710 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001711 if (seeding_dev) {
1712 mutex_unlock(&uuid_mutex);
1713 up_write(&sb->s_umount);
1714 }
Chris Mason788f20e2008-04-28 15:29:42 -04001715 goto out;
1716}
1717
Chris Masond3977122009-01-05 21:25:51 -05001718static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1719 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001720{
1721 int ret;
1722 struct btrfs_path *path;
1723 struct btrfs_root *root;
1724 struct btrfs_dev_item *dev_item;
1725 struct extent_buffer *leaf;
1726 struct btrfs_key key;
1727
1728 root = device->dev_root->fs_info->chunk_root;
1729
1730 path = btrfs_alloc_path();
1731 if (!path)
1732 return -ENOMEM;
1733
1734 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1735 key.type = BTRFS_DEV_ITEM_KEY;
1736 key.offset = device->devid;
1737
1738 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1739 if (ret < 0)
1740 goto out;
1741
1742 if (ret > 0) {
1743 ret = -ENOENT;
1744 goto out;
1745 }
1746
1747 leaf = path->nodes[0];
1748 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1749
1750 btrfs_set_device_id(leaf, dev_item, device->devid);
1751 btrfs_set_device_type(leaf, dev_item, device->type);
1752 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1753 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1754 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001755 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001756 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1757 btrfs_mark_buffer_dirty(leaf);
1758
1759out:
1760 btrfs_free_path(path);
1761 return ret;
1762}
1763
Chris Mason7d9eb122008-07-08 14:19:17 -04001764static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001765 struct btrfs_device *device, u64 new_size)
1766{
1767 struct btrfs_super_block *super_copy =
1768 &device->dev_root->fs_info->super_copy;
1769 u64 old_total = btrfs_super_total_bytes(super_copy);
1770 u64 diff = new_size - device->total_bytes;
1771
Yan Zheng2b820322008-11-17 21:11:30 -05001772 if (!device->writeable)
1773 return -EACCES;
1774 if (new_size <= device->total_bytes)
1775 return -EINVAL;
1776
Chris Mason8f18cf12008-04-25 16:53:30 -04001777 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001778 device->fs_devices->total_rw_bytes += diff;
1779
1780 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001781 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001782 btrfs_clear_space_info_full(device->dev_root->fs_info);
1783
Chris Mason8f18cf12008-04-25 16:53:30 -04001784 return btrfs_update_device(trans, device);
1785}
1786
Chris Mason7d9eb122008-07-08 14:19:17 -04001787int btrfs_grow_device(struct btrfs_trans_handle *trans,
1788 struct btrfs_device *device, u64 new_size)
1789{
1790 int ret;
1791 lock_chunks(device->dev_root);
1792 ret = __btrfs_grow_device(trans, device, new_size);
1793 unlock_chunks(device->dev_root);
1794 return ret;
1795}
1796
Chris Mason8f18cf12008-04-25 16:53:30 -04001797static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1798 struct btrfs_root *root,
1799 u64 chunk_tree, u64 chunk_objectid,
1800 u64 chunk_offset)
1801{
1802 int ret;
1803 struct btrfs_path *path;
1804 struct btrfs_key key;
1805
1806 root = root->fs_info->chunk_root;
1807 path = btrfs_alloc_path();
1808 if (!path)
1809 return -ENOMEM;
1810
1811 key.objectid = chunk_objectid;
1812 key.offset = chunk_offset;
1813 key.type = BTRFS_CHUNK_ITEM_KEY;
1814
1815 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1816 BUG_ON(ret);
1817
1818 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001819
1820 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001821 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001822}
1823
Christoph Hellwigb2950862008-12-02 09:54:17 -05001824static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001825 chunk_offset)
1826{
1827 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1828 struct btrfs_disk_key *disk_key;
1829 struct btrfs_chunk *chunk;
1830 u8 *ptr;
1831 int ret = 0;
1832 u32 num_stripes;
1833 u32 array_size;
1834 u32 len = 0;
1835 u32 cur;
1836 struct btrfs_key key;
1837
1838 array_size = btrfs_super_sys_array_size(super_copy);
1839
1840 ptr = super_copy->sys_chunk_array;
1841 cur = 0;
1842
1843 while (cur < array_size) {
1844 disk_key = (struct btrfs_disk_key *)ptr;
1845 btrfs_disk_key_to_cpu(&key, disk_key);
1846
1847 len = sizeof(*disk_key);
1848
1849 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1850 chunk = (struct btrfs_chunk *)(ptr + len);
1851 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1852 len += btrfs_chunk_item_size(num_stripes);
1853 } else {
1854 ret = -EIO;
1855 break;
1856 }
1857 if (key.objectid == chunk_objectid &&
1858 key.offset == chunk_offset) {
1859 memmove(ptr, ptr + len, array_size - (cur + len));
1860 array_size -= len;
1861 btrfs_set_super_sys_array_size(super_copy, array_size);
1862 } else {
1863 ptr += len;
1864 cur += len;
1865 }
1866 }
1867 return ret;
1868}
1869
Christoph Hellwigb2950862008-12-02 09:54:17 -05001870static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001871 u64 chunk_tree, u64 chunk_objectid,
1872 u64 chunk_offset)
1873{
1874 struct extent_map_tree *em_tree;
1875 struct btrfs_root *extent_root;
1876 struct btrfs_trans_handle *trans;
1877 struct extent_map *em;
1878 struct map_lookup *map;
1879 int ret;
1880 int i;
1881
1882 root = root->fs_info->chunk_root;
1883 extent_root = root->fs_info->extent_root;
1884 em_tree = &root->fs_info->mapping_tree.map_tree;
1885
Josef Bacikba1bf482009-09-11 16:11:19 -04001886 ret = btrfs_can_relocate(extent_root, chunk_offset);
1887 if (ret)
1888 return -ENOSPC;
1889
Chris Mason8f18cf12008-04-25 16:53:30 -04001890 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001891 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001892 if (ret)
1893 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001894
Yan, Zhenga22285a2010-05-16 10:48:46 -04001895 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001896 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001897
Chris Mason7d9eb122008-07-08 14:19:17 -04001898 lock_chunks(root);
1899
Chris Mason8f18cf12008-04-25 16:53:30 -04001900 /*
1901 * step two, delete the device extents and the
1902 * chunk tree entries
1903 */
Chris Mason890871b2009-09-02 16:24:52 -04001904 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001905 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001906 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001907
Chris Masona061fc82008-05-07 11:43:44 -04001908 BUG_ON(em->start > chunk_offset ||
1909 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001910 map = (struct map_lookup *)em->bdev;
1911
1912 for (i = 0; i < map->num_stripes; i++) {
1913 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1914 map->stripes[i].physical);
1915 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001916
Chris Masondfe25022008-05-13 13:46:40 -04001917 if (map->stripes[i].dev) {
1918 ret = btrfs_update_device(trans, map->stripes[i].dev);
1919 BUG_ON(ret);
1920 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001921 }
1922 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1923 chunk_offset);
1924
1925 BUG_ON(ret);
1926
liubo1abe9b82011-03-24 11:18:59 +00001927 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1928
Chris Mason8f18cf12008-04-25 16:53:30 -04001929 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1930 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1931 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001932 }
1933
Zheng Yan1a40e232008-09-26 10:09:34 -04001934 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1935 BUG_ON(ret);
1936
Chris Mason890871b2009-09-02 16:24:52 -04001937 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001938 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001939 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001940
Chris Mason8f18cf12008-04-25 16:53:30 -04001941 kfree(map);
1942 em->bdev = NULL;
1943
1944 /* once for the tree */
1945 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001946 /* once for us */
1947 free_extent_map(em);
1948
Chris Mason7d9eb122008-07-08 14:19:17 -04001949 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001950 btrfs_end_transaction(trans, root);
1951 return 0;
1952}
1953
Yan Zheng2b820322008-11-17 21:11:30 -05001954static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1955{
1956 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1957 struct btrfs_path *path;
1958 struct extent_buffer *leaf;
1959 struct btrfs_chunk *chunk;
1960 struct btrfs_key key;
1961 struct btrfs_key found_key;
1962 u64 chunk_tree = chunk_root->root_key.objectid;
1963 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001964 bool retried = false;
1965 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001966 int ret;
1967
1968 path = btrfs_alloc_path();
1969 if (!path)
1970 return -ENOMEM;
1971
Josef Bacikba1bf482009-09-11 16:11:19 -04001972again:
Yan Zheng2b820322008-11-17 21:11:30 -05001973 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1974 key.offset = (u64)-1;
1975 key.type = BTRFS_CHUNK_ITEM_KEY;
1976
1977 while (1) {
1978 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1979 if (ret < 0)
1980 goto error;
1981 BUG_ON(ret == 0);
1982
1983 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1984 key.type);
1985 if (ret < 0)
1986 goto error;
1987 if (ret > 0)
1988 break;
1989
1990 leaf = path->nodes[0];
1991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1992
1993 chunk = btrfs_item_ptr(leaf, path->slots[0],
1994 struct btrfs_chunk);
1995 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02001996 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001997
1998 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1999 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2000 found_key.objectid,
2001 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002002 if (ret == -ENOSPC)
2003 failed++;
2004 else if (ret)
2005 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002006 }
2007
2008 if (found_key.offset == 0)
2009 break;
2010 key.offset = found_key.offset - 1;
2011 }
2012 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002013 if (failed && !retried) {
2014 failed = 0;
2015 retried = true;
2016 goto again;
2017 } else if (failed && retried) {
2018 WARN_ON(1);
2019 ret = -ENOSPC;
2020 }
Yan Zheng2b820322008-11-17 21:11:30 -05002021error:
2022 btrfs_free_path(path);
2023 return ret;
2024}
2025
Chris Masonec44a352008-04-28 15:29:52 -04002026static u64 div_factor(u64 num, int factor)
2027{
2028 if (factor == 10)
2029 return num;
2030 num *= factor;
2031 do_div(num, 10);
2032 return num;
2033}
2034
Chris Masonec44a352008-04-28 15:29:52 -04002035int btrfs_balance(struct btrfs_root *dev_root)
2036{
2037 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002038 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2039 struct btrfs_device *device;
2040 u64 old_size;
2041 u64 size_to_free;
2042 struct btrfs_path *path;
2043 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002044 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2045 struct btrfs_trans_handle *trans;
2046 struct btrfs_key found_key;
2047
Yan Zheng2b820322008-11-17 21:11:30 -05002048 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2049 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002050
Ben Hutchings6f88a442010-12-29 14:55:03 +00002051 if (!capable(CAP_SYS_ADMIN))
2052 return -EPERM;
2053
Chris Mason7d9eb122008-07-08 14:19:17 -04002054 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002055 dev_root = dev_root->fs_info->dev_root;
2056
Chris Masonec44a352008-04-28 15:29:52 -04002057 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002058 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002059 old_size = device->total_bytes;
2060 size_to_free = div_factor(old_size, 1);
2061 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002062 if (!device->writeable ||
2063 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002064 continue;
2065
2066 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002067 if (ret == -ENOSPC)
2068 break;
Chris Masonec44a352008-04-28 15:29:52 -04002069 BUG_ON(ret);
2070
Yan, Zhenga22285a2010-05-16 10:48:46 -04002071 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002072 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002073
2074 ret = btrfs_grow_device(trans, device, old_size);
2075 BUG_ON(ret);
2076
2077 btrfs_end_transaction(trans, dev_root);
2078 }
2079
2080 /* step two, relocate all the chunks */
2081 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002082 if (!path) {
2083 ret = -ENOMEM;
2084 goto error;
2085 }
Chris Masonec44a352008-04-28 15:29:52 -04002086 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2087 key.offset = (u64)-1;
2088 key.type = BTRFS_CHUNK_ITEM_KEY;
2089
Chris Masond3977122009-01-05 21:25:51 -05002090 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002091 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2092 if (ret < 0)
2093 goto error;
2094
2095 /*
2096 * this shouldn't happen, it means the last relocate
2097 * failed
2098 */
2099 if (ret == 0)
2100 break;
2101
2102 ret = btrfs_previous_item(chunk_root, path, 0,
2103 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002104 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002105 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002106
Chris Masonec44a352008-04-28 15:29:52 -04002107 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2108 path->slots[0]);
2109 if (found_key.objectid != key.objectid)
2110 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002111
Chris Masonec44a352008-04-28 15:29:52 -04002112 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002113 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002114 break;
2115
David Sterbab3b4aa72011-04-21 01:20:15 +02002116 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002117 ret = btrfs_relocate_chunk(chunk_root,
2118 chunk_root->root_key.objectid,
2119 found_key.objectid,
2120 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002121 if (ret && ret != -ENOSPC)
2122 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002123 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002124 }
2125 ret = 0;
2126error:
2127 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002128 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002129 return ret;
2130}
2131
Chris Mason8f18cf12008-04-25 16:53:30 -04002132/*
2133 * shrinking a device means finding all of the device extents past
2134 * the new size, and then following the back refs to the chunks.
2135 * The chunk relocation code actually frees the device extent
2136 */
2137int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2138{
2139 struct btrfs_trans_handle *trans;
2140 struct btrfs_root *root = device->dev_root;
2141 struct btrfs_dev_extent *dev_extent = NULL;
2142 struct btrfs_path *path;
2143 u64 length;
2144 u64 chunk_tree;
2145 u64 chunk_objectid;
2146 u64 chunk_offset;
2147 int ret;
2148 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002149 int failed = 0;
2150 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002151 struct extent_buffer *l;
2152 struct btrfs_key key;
2153 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2154 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002155 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002156 u64 diff = device->total_bytes - new_size;
2157
Yan Zheng2b820322008-11-17 21:11:30 -05002158 if (new_size >= device->total_bytes)
2159 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002160
2161 path = btrfs_alloc_path();
2162 if (!path)
2163 return -ENOMEM;
2164
Chris Mason8f18cf12008-04-25 16:53:30 -04002165 path->reada = 2;
2166
Chris Mason7d9eb122008-07-08 14:19:17 -04002167 lock_chunks(root);
2168
Chris Mason8f18cf12008-04-25 16:53:30 -04002169 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002170 if (device->writeable)
2171 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002172 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002173
Josef Bacikba1bf482009-09-11 16:11:19 -04002174again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002175 key.objectid = device->devid;
2176 key.offset = (u64)-1;
2177 key.type = BTRFS_DEV_EXTENT_KEY;
2178
2179 while (1) {
2180 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2181 if (ret < 0)
2182 goto done;
2183
2184 ret = btrfs_previous_item(root, path, 0, key.type);
2185 if (ret < 0)
2186 goto done;
2187 if (ret) {
2188 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002189 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002190 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002191 }
2192
2193 l = path->nodes[0];
2194 slot = path->slots[0];
2195 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2196
Josef Bacikba1bf482009-09-11 16:11:19 -04002197 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002198 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002199 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002200 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002201
2202 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2203 length = btrfs_dev_extent_length(l, dev_extent);
2204
Josef Bacikba1bf482009-09-11 16:11:19 -04002205 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002206 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002207 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002208 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002209
2210 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2211 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2212 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002213 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002214
2215 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2216 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002217 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002218 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002219 if (ret == -ENOSPC)
2220 failed++;
2221 key.offset -= 1;
2222 }
2223
2224 if (failed && !retried) {
2225 failed = 0;
2226 retried = true;
2227 goto again;
2228 } else if (failed && retried) {
2229 ret = -ENOSPC;
2230 lock_chunks(root);
2231
2232 device->total_bytes = old_size;
2233 if (device->writeable)
2234 device->fs_devices->total_rw_bytes += diff;
2235 unlock_chunks(root);
2236 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002237 }
2238
Chris Balld6397ba2009-04-27 07:29:03 -04002239 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002240 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002241 if (IS_ERR(trans)) {
2242 ret = PTR_ERR(trans);
2243 goto done;
2244 }
2245
Chris Balld6397ba2009-04-27 07:29:03 -04002246 lock_chunks(root);
2247
2248 device->disk_total_bytes = new_size;
2249 /* Now btrfs_update_device() will change the on-disk size. */
2250 ret = btrfs_update_device(trans, device);
2251 if (ret) {
2252 unlock_chunks(root);
2253 btrfs_end_transaction(trans, root);
2254 goto done;
2255 }
2256 WARN_ON(diff > old_total);
2257 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2258 unlock_chunks(root);
2259 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002260done:
2261 btrfs_free_path(path);
2262 return ret;
2263}
2264
Christoph Hellwigb2950862008-12-02 09:54:17 -05002265static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002266 struct btrfs_root *root,
2267 struct btrfs_key *key,
2268 struct btrfs_chunk *chunk, int item_size)
2269{
2270 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2271 struct btrfs_disk_key disk_key;
2272 u32 array_size;
2273 u8 *ptr;
2274
2275 array_size = btrfs_super_sys_array_size(super_copy);
2276 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2277 return -EFBIG;
2278
2279 ptr = super_copy->sys_chunk_array + array_size;
2280 btrfs_cpu_key_to_disk(&disk_key, key);
2281 memcpy(ptr, &disk_key, sizeof(disk_key));
2282 ptr += sizeof(disk_key);
2283 memcpy(ptr, chunk, item_size);
2284 item_size += sizeof(disk_key);
2285 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2286 return 0;
2287}
2288
Miao Xieb2117a32011-01-05 10:07:28 +00002289/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002290 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002291 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002292static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002293{
Arne Jansen73c5de02011-04-12 12:07:57 +02002294 const struct btrfs_device_info *di_a = a;
2295 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002296
Arne Jansen73c5de02011-04-12 12:07:57 +02002297 if (di_a->max_avail > di_b->max_avail)
2298 return -1;
2299 if (di_a->max_avail < di_b->max_avail)
2300 return 1;
2301 if (di_a->total_avail > di_b->total_avail)
2302 return -1;
2303 if (di_a->total_avail < di_b->total_avail)
2304 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002305 return 0;
2306}
2307
2308static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2309 struct btrfs_root *extent_root,
2310 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002311 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002312 u64 start, u64 type)
2313{
2314 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002315 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2316 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002317 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002318 struct extent_map_tree *em_tree;
2319 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002320 struct btrfs_device_info *devices_info = NULL;
2321 u64 total_avail;
2322 int num_stripes; /* total number of stripes to allocate */
2323 int sub_stripes; /* sub_stripes info for map */
2324 int dev_stripes; /* stripes per dev */
2325 int devs_max; /* max devs to use */
2326 int devs_min; /* min devs needed */
2327 int devs_increment; /* ndevs has to be a multiple of this */
2328 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002329 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002330 u64 max_stripe_size;
2331 u64 max_chunk_size;
2332 u64 stripe_size;
2333 u64 num_bytes;
2334 int ndevs;
2335 int i;
2336 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002337
2338 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2339 (type & BTRFS_BLOCK_GROUP_DUP)) {
2340 WARN_ON(1);
2341 type &= ~BTRFS_BLOCK_GROUP_DUP;
2342 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002343
Miao Xieb2117a32011-01-05 10:07:28 +00002344 if (list_empty(&fs_devices->alloc_list))
2345 return -ENOSPC;
2346
Arne Jansen73c5de02011-04-12 12:07:57 +02002347 sub_stripes = 1;
2348 dev_stripes = 1;
2349 devs_increment = 1;
2350 ncopies = 1;
2351 devs_max = 0; /* 0 == as many as possible */
2352 devs_min = 1;
2353
2354 /*
2355 * define the properties of each RAID type.
2356 * FIXME: move this to a global table and use it in all RAID
2357 * calculation code
2358 */
2359 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2360 dev_stripes = 2;
2361 ncopies = 2;
2362 devs_max = 1;
2363 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2364 devs_min = 2;
2365 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2366 devs_increment = 2;
2367 ncopies = 2;
2368 devs_max = 2;
2369 devs_min = 2;
2370 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2371 sub_stripes = 2;
2372 devs_increment = 2;
2373 ncopies = 2;
2374 devs_min = 4;
2375 } else {
2376 devs_max = 1;
2377 }
2378
2379 if (type & BTRFS_BLOCK_GROUP_DATA) {
2380 max_stripe_size = 1024 * 1024 * 1024;
2381 max_chunk_size = 10 * max_stripe_size;
2382 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2383 max_stripe_size = 256 * 1024 * 1024;
2384 max_chunk_size = max_stripe_size;
2385 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2386 max_stripe_size = 8 * 1024 * 1024;
2387 max_chunk_size = 2 * max_stripe_size;
2388 } else {
2389 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2390 type);
2391 BUG_ON(1);
2392 }
2393
2394 /* we don't want a chunk larger than 10% of writeable space */
2395 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2396 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002397
2398 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2399 GFP_NOFS);
2400 if (!devices_info)
2401 return -ENOMEM;
2402
Arne Jansen73c5de02011-04-12 12:07:57 +02002403 cur = fs_devices->alloc_list.next;
2404
2405 /*
2406 * in the first pass through the devices list, we gather information
2407 * about the available holes on each device.
2408 */
2409 ndevs = 0;
2410 while (cur != &fs_devices->alloc_list) {
2411 struct btrfs_device *device;
2412 u64 max_avail;
2413 u64 dev_offset;
2414
2415 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2416
2417 cur = cur->next;
2418
2419 if (!device->writeable) {
2420 printk(KERN_ERR
2421 "btrfs: read-only device in alloc_list\n");
2422 WARN_ON(1);
2423 continue;
2424 }
2425
2426 if (!device->in_fs_metadata)
2427 continue;
2428
2429 if (device->total_bytes > device->bytes_used)
2430 total_avail = device->total_bytes - device->bytes_used;
2431 else
2432 total_avail = 0;
2433 /* avail is off by max(alloc_start, 1MB), but that is the same
2434 * for all devices, so it doesn't hurt the sorting later on
2435 */
2436
2437 ret = find_free_dev_extent(trans, device,
2438 max_stripe_size * dev_stripes,
2439 &dev_offset, &max_avail);
2440 if (ret && ret != -ENOSPC)
2441 goto error;
2442
2443 if (ret == 0)
2444 max_avail = max_stripe_size * dev_stripes;
2445
2446 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2447 continue;
2448
2449 devices_info[ndevs].dev_offset = dev_offset;
2450 devices_info[ndevs].max_avail = max_avail;
2451 devices_info[ndevs].total_avail = total_avail;
2452 devices_info[ndevs].dev = device;
2453 ++ndevs;
2454 }
2455
2456 /*
2457 * now sort the devices by hole size / available space
2458 */
2459 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2460 btrfs_cmp_device_info, NULL);
2461
2462 /* round down to number of usable stripes */
2463 ndevs -= ndevs % devs_increment;
2464
2465 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2466 ret = -ENOSPC;
2467 goto error;
2468 }
2469
2470 if (devs_max && ndevs > devs_max)
2471 ndevs = devs_max;
2472 /*
2473 * the primary goal is to maximize the number of stripes, so use as many
2474 * devices as possible, even if the stripes are not maximum sized.
2475 */
2476 stripe_size = devices_info[ndevs-1].max_avail;
2477 num_stripes = ndevs * dev_stripes;
2478
2479 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2480 stripe_size = max_chunk_size * ncopies;
2481 do_div(stripe_size, num_stripes);
2482 }
2483
2484 do_div(stripe_size, dev_stripes);
2485 do_div(stripe_size, BTRFS_STRIPE_LEN);
2486 stripe_size *= BTRFS_STRIPE_LEN;
2487
Miao Xieb2117a32011-01-05 10:07:28 +00002488 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2489 if (!map) {
2490 ret = -ENOMEM;
2491 goto error;
2492 }
2493 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002494
Arne Jansen73c5de02011-04-12 12:07:57 +02002495 for (i = 0; i < ndevs; ++i) {
2496 for (j = 0; j < dev_stripes; ++j) {
2497 int s = i * dev_stripes + j;
2498 map->stripes[s].dev = devices_info[i].dev;
2499 map->stripes[s].physical = devices_info[i].dev_offset +
2500 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002501 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002502 }
Chris Mason593060d2008-03-25 16:50:33 -04002503 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002504 map->stripe_len = BTRFS_STRIPE_LEN;
2505 map->io_align = BTRFS_STRIPE_LEN;
2506 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002507 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002508 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002509
Yan Zheng2b820322008-11-17 21:11:30 -05002510 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002511 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002512
Arne Jansen73c5de02011-04-12 12:07:57 +02002513 *stripe_size_out = stripe_size;
2514 *num_bytes_out = num_bytes;
2515
2516 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002517
David Sterba172ddd62011-04-21 00:48:27 +02002518 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002519 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002520 ret = -ENOMEM;
2521 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002522 }
Chris Mason0b86a832008-03-24 15:01:56 -04002523 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002524 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002525 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002526 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002527 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002528
Chris Mason0b86a832008-03-24 15:01:56 -04002529 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002530 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002531 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002532 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002533 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002534 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002535
2536 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2537 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002538 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002539 BUG_ON(ret);
2540
Arne Jansen73c5de02011-04-12 12:07:57 +02002541 for (i = 0; i < map->num_stripes; ++i) {
2542 struct btrfs_device *device;
2543 u64 dev_offset;
2544
2545 device = map->stripes[i].dev;
2546 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002547
2548 ret = btrfs_alloc_dev_extent(trans, device,
2549 info->chunk_root->root_key.objectid,
2550 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002551 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002552 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002553 }
2554
Miao Xieb2117a32011-01-05 10:07:28 +00002555 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002556 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002557
2558error:
2559 kfree(map);
2560 kfree(devices_info);
2561 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002562}
2563
2564static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2565 struct btrfs_root *extent_root,
2566 struct map_lookup *map, u64 chunk_offset,
2567 u64 chunk_size, u64 stripe_size)
2568{
2569 u64 dev_offset;
2570 struct btrfs_key key;
2571 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2572 struct btrfs_device *device;
2573 struct btrfs_chunk *chunk;
2574 struct btrfs_stripe *stripe;
2575 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2576 int index = 0;
2577 int ret;
2578
2579 chunk = kzalloc(item_size, GFP_NOFS);
2580 if (!chunk)
2581 return -ENOMEM;
2582
2583 index = 0;
2584 while (index < map->num_stripes) {
2585 device = map->stripes[index].dev;
2586 device->bytes_used += stripe_size;
2587 ret = btrfs_update_device(trans, device);
2588 BUG_ON(ret);
2589 index++;
2590 }
2591
2592 index = 0;
2593 stripe = &chunk->stripe;
2594 while (index < map->num_stripes) {
2595 device = map->stripes[index].dev;
2596 dev_offset = map->stripes[index].physical;
2597
2598 btrfs_set_stack_stripe_devid(stripe, device->devid);
2599 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2600 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2601 stripe++;
2602 index++;
2603 }
2604
2605 btrfs_set_stack_chunk_length(chunk, chunk_size);
2606 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2607 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2608 btrfs_set_stack_chunk_type(chunk, map->type);
2609 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2610 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2611 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2612 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2613 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2614
2615 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2616 key.type = BTRFS_CHUNK_ITEM_KEY;
2617 key.offset = chunk_offset;
2618
2619 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2620 BUG_ON(ret);
2621
2622 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2623 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2624 item_size);
2625 BUG_ON(ret);
2626 }
liubo1abe9b82011-03-24 11:18:59 +00002627
Yan Zheng2b820322008-11-17 21:11:30 -05002628 kfree(chunk);
2629 return 0;
2630}
2631
2632/*
2633 * Chunk allocation falls into two parts. The first part does works
2634 * that make the new allocated chunk useable, but not do any operation
2635 * that modifies the chunk tree. The second part does the works that
2636 * require modifying the chunk tree. This division is important for the
2637 * bootstrap process of adding storage to a seed btrfs.
2638 */
2639int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2640 struct btrfs_root *extent_root, u64 type)
2641{
2642 u64 chunk_offset;
2643 u64 chunk_size;
2644 u64 stripe_size;
2645 struct map_lookup *map;
2646 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2647 int ret;
2648
2649 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2650 &chunk_offset);
2651 if (ret)
2652 return ret;
2653
2654 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2655 &stripe_size, chunk_offset, type);
2656 if (ret)
2657 return ret;
2658
2659 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2660 chunk_size, stripe_size);
2661 BUG_ON(ret);
2662 return 0;
2663}
2664
Chris Masond3977122009-01-05 21:25:51 -05002665static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002666 struct btrfs_root *root,
2667 struct btrfs_device *device)
2668{
2669 u64 chunk_offset;
2670 u64 sys_chunk_offset;
2671 u64 chunk_size;
2672 u64 sys_chunk_size;
2673 u64 stripe_size;
2674 u64 sys_stripe_size;
2675 u64 alloc_profile;
2676 struct map_lookup *map;
2677 struct map_lookup *sys_map;
2678 struct btrfs_fs_info *fs_info = root->fs_info;
2679 struct btrfs_root *extent_root = fs_info->extent_root;
2680 int ret;
2681
2682 ret = find_next_chunk(fs_info->chunk_root,
2683 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07002684 if (ret)
2685 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002686
2687 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2688 (fs_info->metadata_alloc_profile &
2689 fs_info->avail_metadata_alloc_bits);
2690 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2691
2692 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2693 &stripe_size, chunk_offset, alloc_profile);
2694 BUG_ON(ret);
2695
2696 sys_chunk_offset = chunk_offset + chunk_size;
2697
2698 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2699 (fs_info->system_alloc_profile &
2700 fs_info->avail_system_alloc_bits);
2701 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2702
2703 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2704 &sys_chunk_size, &sys_stripe_size,
2705 sys_chunk_offset, alloc_profile);
2706 BUG_ON(ret);
2707
2708 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2709 BUG_ON(ret);
2710
2711 /*
2712 * Modifying chunk tree needs allocating new blocks from both
2713 * system block group and metadata block group. So we only can
2714 * do operations require modifying the chunk tree after both
2715 * block groups were created.
2716 */
2717 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2718 chunk_size, stripe_size);
2719 BUG_ON(ret);
2720
2721 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2722 sys_chunk_offset, sys_chunk_size,
2723 sys_stripe_size);
2724 BUG_ON(ret);
2725 return 0;
2726}
2727
2728int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2729{
2730 struct extent_map *em;
2731 struct map_lookup *map;
2732 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2733 int readonly = 0;
2734 int i;
2735
Chris Mason890871b2009-09-02 16:24:52 -04002736 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002737 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002738 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002739 if (!em)
2740 return 1;
2741
Josef Bacikf48b9072010-01-27 02:07:59 +00002742 if (btrfs_test_opt(root, DEGRADED)) {
2743 free_extent_map(em);
2744 return 0;
2745 }
2746
Yan Zheng2b820322008-11-17 21:11:30 -05002747 map = (struct map_lookup *)em->bdev;
2748 for (i = 0; i < map->num_stripes; i++) {
2749 if (!map->stripes[i].dev->writeable) {
2750 readonly = 1;
2751 break;
2752 }
2753 }
2754 free_extent_map(em);
2755 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002756}
2757
2758void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2759{
David Sterbaa8067e02011-04-21 00:34:43 +02002760 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002761}
2762
2763void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2764{
2765 struct extent_map *em;
2766
Chris Masond3977122009-01-05 21:25:51 -05002767 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002768 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002769 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2770 if (em)
2771 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002772 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002773 if (!em)
2774 break;
2775 kfree(em->bdev);
2776 /* once for us */
2777 free_extent_map(em);
2778 /* once for the tree */
2779 free_extent_map(em);
2780 }
2781}
2782
Chris Masonf1885912008-04-09 16:28:12 -04002783int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2784{
2785 struct extent_map *em;
2786 struct map_lookup *map;
2787 struct extent_map_tree *em_tree = &map_tree->map_tree;
2788 int ret;
2789
Chris Mason890871b2009-09-02 16:24:52 -04002790 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002791 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002792 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002793 BUG_ON(!em);
2794
2795 BUG_ON(em->start > logical || em->start + em->len < logical);
2796 map = (struct map_lookup *)em->bdev;
2797 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2798 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002799 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2800 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002801 else
2802 ret = 1;
2803 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002804 return ret;
2805}
2806
Chris Masondfe25022008-05-13 13:46:40 -04002807static int find_live_mirror(struct map_lookup *map, int first, int num,
2808 int optimal)
2809{
2810 int i;
2811 if (map->stripes[optimal].dev->bdev)
2812 return optimal;
2813 for (i = first; i < first + num; i++) {
2814 if (map->stripes[i].dev->bdev)
2815 return i;
2816 }
2817 /* we couldn't find one that doesn't fail. Just return something
2818 * and the io error handling code will clean up eventually
2819 */
2820 return optimal;
2821}
2822
Chris Masonf2d8d742008-04-21 10:03:05 -04002823static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2824 u64 logical, u64 *length,
2825 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002826 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002827{
2828 struct extent_map *em;
2829 struct map_lookup *map;
2830 struct extent_map_tree *em_tree = &map_tree->map_tree;
2831 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002832 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002833 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002834 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002835 u64 stripe_nr_orig;
2836 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002837 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002838 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002839 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002840 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002841 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002842 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002843 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002844
Li Dongyangfce3bb92011-03-24 10:24:26 +00002845 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002846 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002847again:
2848 if (multi_ret) {
2849 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2850 GFP_NOFS);
2851 if (!multi)
2852 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002853
2854 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002855 }
Chris Mason0b86a832008-03-24 15:01:56 -04002856
Chris Mason890871b2009-09-02 16:24:52 -04002857 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002858 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002859 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002860
Chris Mason3b951512008-04-17 11:29:12 -04002861 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002862 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2863 (unsigned long long)logical,
2864 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002865 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002866 }
Chris Mason0b86a832008-03-24 15:01:56 -04002867
2868 BUG_ON(em->start > logical || em->start + em->len < logical);
2869 map = (struct map_lookup *)em->bdev;
2870 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002871
Chris Masonf1885912008-04-09 16:28:12 -04002872 if (mirror_num > map->num_stripes)
2873 mirror_num = 0;
2874
Chris Masoncea9e442008-04-09 16:28:12 -04002875 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002876 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002877 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2878 BTRFS_BLOCK_GROUP_DUP)) {
2879 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002880 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002881 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2882 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002883 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002884 }
2885 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002886 if (rw & REQ_DISCARD) {
2887 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2888 BTRFS_BLOCK_GROUP_RAID1 |
2889 BTRFS_BLOCK_GROUP_DUP |
2890 BTRFS_BLOCK_GROUP_RAID10)) {
2891 stripes_required = map->num_stripes;
2892 }
2893 }
2894 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002895 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002896 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002897 free_extent_map(em);
2898 kfree(multi);
2899 goto again;
2900 }
Chris Mason593060d2008-03-25 16:50:33 -04002901 stripe_nr = offset;
2902 /*
2903 * stripe_nr counts the total number of stripes we have to stride
2904 * to get to this block
2905 */
2906 do_div(stripe_nr, map->stripe_len);
2907
2908 stripe_offset = stripe_nr * map->stripe_len;
2909 BUG_ON(offset < stripe_offset);
2910
2911 /* stripe_offset is the offset of this block in its stripe*/
2912 stripe_offset = offset - stripe_offset;
2913
Li Dongyangfce3bb92011-03-24 10:24:26 +00002914 if (rw & REQ_DISCARD)
2915 *length = min_t(u64, em->len - offset, *length);
2916 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2917 BTRFS_BLOCK_GROUP_RAID1 |
2918 BTRFS_BLOCK_GROUP_RAID10 |
2919 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002920 /* we limit the length of each bio to what fits in a stripe */
2921 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002922 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002923 } else {
2924 *length = em->len - offset;
2925 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002926
Jens Axboe7eaceac2011-03-10 08:52:07 +01002927 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002928 goto out;
2929
Chris Masonf2d8d742008-04-21 10:03:05 -04002930 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002931 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002932 stripe_nr_orig = stripe_nr;
2933 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2934 (~(map->stripe_len - 1));
2935 do_div(stripe_nr_end, map->stripe_len);
2936 stripe_end_offset = stripe_nr_end * map->stripe_len -
2937 (offset + *length);
2938 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2939 if (rw & REQ_DISCARD)
2940 num_stripes = min_t(u64, map->num_stripes,
2941 stripe_nr_end - stripe_nr_orig);
2942 stripe_index = do_div(stripe_nr, map->num_stripes);
2943 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07002944 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002945 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002946 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002947 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002948 else {
2949 stripe_index = find_live_mirror(map, 0,
2950 map->num_stripes,
2951 current->pid % map->num_stripes);
2952 }
Chris Mason2fff7342008-04-29 14:12:09 -04002953
Chris Mason611f0e02008-04-03 16:29:03 -04002954 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00002955 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002956 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002957 else if (mirror_num)
2958 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002959
Chris Mason321aecc2008-04-16 10:49:51 -04002960 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2961 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002962
2963 stripe_index = do_div(stripe_nr, factor);
2964 stripe_index *= map->sub_stripes;
2965
Jens Axboe7eaceac2011-03-10 08:52:07 +01002966 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04002967 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002968 else if (rw & REQ_DISCARD)
2969 num_stripes = min_t(u64, map->sub_stripes *
2970 (stripe_nr_end - stripe_nr_orig),
2971 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04002972 else if (mirror_num)
2973 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002974 else {
2975 stripe_index = find_live_mirror(map, stripe_index,
2976 map->sub_stripes, stripe_index +
2977 current->pid % map->sub_stripes);
2978 }
Chris Mason8790d502008-04-03 16:29:03 -04002979 } else {
2980 /*
2981 * after this do_div call, stripe_nr is the number of stripes
2982 * on this device we have to walk to find the data, and
2983 * stripe_index is the number of our device in the stripe array
2984 */
2985 stripe_index = do_div(stripe_nr, map->num_stripes);
2986 }
Chris Mason593060d2008-03-25 16:50:33 -04002987 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002988
Li Dongyangfce3bb92011-03-24 10:24:26 +00002989 if (rw & REQ_DISCARD) {
2990 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002991 multi->stripes[i].physical =
2992 map->stripes[stripe_index].physical +
2993 stripe_offset + stripe_nr * map->stripe_len;
2994 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002995
2996 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2997 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04002998 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002999 int j;
3000
Chris Masond9d04872011-03-27 21:23:21 -04003001 div_u64_rem(stripe_nr_end - 1,
3002 map->num_stripes,
3003 &last_stripe);
3004
Li Dongyangfce3bb92011-03-24 10:24:26 +00003005 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003006 u32 test;
3007
3008 div_u64_rem(stripe_nr_end - 1 - j,
3009 map->num_stripes, &test);
3010 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003011 break;
3012 }
3013 stripes = stripe_nr_end - 1 - j;
3014 do_div(stripes, map->num_stripes);
3015 multi->stripes[i].length = map->stripe_len *
3016 (stripes - stripe_nr + 1);
3017
3018 if (i == 0) {
3019 multi->stripes[i].length -=
3020 stripe_offset;
3021 stripe_offset = 0;
3022 }
3023 if (stripe_index == last_stripe)
3024 multi->stripes[i].length -=
3025 stripe_end_offset;
3026 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3027 u64 stripes;
3028 int j;
3029 int factor = map->num_stripes /
3030 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003031 u32 last_stripe = 0;
3032
3033 div_u64_rem(stripe_nr_end - 1,
3034 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003035 last_stripe *= map->sub_stripes;
3036
3037 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003038 u32 test;
3039
3040 div_u64_rem(stripe_nr_end - 1 - j,
3041 factor, &test);
3042
3043 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003044 stripe_index / map->sub_stripes)
3045 break;
3046 }
3047 stripes = stripe_nr_end - 1 - j;
3048 do_div(stripes, factor);
3049 multi->stripes[i].length = map->stripe_len *
3050 (stripes - stripe_nr + 1);
3051
3052 if (i < map->sub_stripes) {
3053 multi->stripes[i].length -=
3054 stripe_offset;
3055 if (i == map->sub_stripes - 1)
3056 stripe_offset = 0;
3057 }
3058 if (stripe_index >= last_stripe &&
3059 stripe_index <= (last_stripe +
3060 map->sub_stripes - 1)) {
3061 multi->stripes[i].length -=
3062 stripe_end_offset;
3063 }
3064 } else
3065 multi->stripes[i].length = *length;
3066
3067 stripe_index++;
3068 if (stripe_index == map->num_stripes) {
3069 /* This could only happen for RAID0/10 */
3070 stripe_index = 0;
3071 stripe_nr++;
3072 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003073 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003074 } else {
3075 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003076 multi->stripes[i].physical =
3077 map->stripes[stripe_index].physical +
3078 stripe_offset +
3079 stripe_nr * map->stripe_len;
3080 multi->stripes[i].dev =
3081 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003082 stripe_index++;
3083 }
Chris Mason593060d2008-03-25 16:50:33 -04003084 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003085 if (multi_ret) {
3086 *multi_ret = multi;
3087 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003088 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003089 }
Chris Masoncea9e442008-04-09 16:28:12 -04003090out:
Chris Mason0b86a832008-03-24 15:01:56 -04003091 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003092 return 0;
3093}
3094
Chris Masonf2d8d742008-04-21 10:03:05 -04003095int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3096 u64 logical, u64 *length,
3097 struct btrfs_multi_bio **multi_ret, int mirror_num)
3098{
3099 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003100 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003101}
3102
Yan Zhenga512bbf2008-12-08 16:46:26 -05003103int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3104 u64 chunk_start, u64 physical, u64 devid,
3105 u64 **logical, int *naddrs, int *stripe_len)
3106{
3107 struct extent_map_tree *em_tree = &map_tree->map_tree;
3108 struct extent_map *em;
3109 struct map_lookup *map;
3110 u64 *buf;
3111 u64 bytenr;
3112 u64 length;
3113 u64 stripe_nr;
3114 int i, j, nr = 0;
3115
Chris Mason890871b2009-09-02 16:24:52 -04003116 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003117 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003118 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003119
3120 BUG_ON(!em || em->start != chunk_start);
3121 map = (struct map_lookup *)em->bdev;
3122
3123 length = em->len;
3124 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3125 do_div(length, map->num_stripes / map->sub_stripes);
3126 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3127 do_div(length, map->num_stripes);
3128
3129 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3130 BUG_ON(!buf);
3131
3132 for (i = 0; i < map->num_stripes; i++) {
3133 if (devid && map->stripes[i].dev->devid != devid)
3134 continue;
3135 if (map->stripes[i].physical > physical ||
3136 map->stripes[i].physical + length <= physical)
3137 continue;
3138
3139 stripe_nr = physical - map->stripes[i].physical;
3140 do_div(stripe_nr, map->stripe_len);
3141
3142 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3143 stripe_nr = stripe_nr * map->num_stripes + i;
3144 do_div(stripe_nr, map->sub_stripes);
3145 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3146 stripe_nr = stripe_nr * map->num_stripes + i;
3147 }
3148 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003149 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003150 for (j = 0; j < nr; j++) {
3151 if (buf[j] == bytenr)
3152 break;
3153 }
Chris Mason934d3752008-12-08 16:43:10 -05003154 if (j == nr) {
3155 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003156 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003157 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003158 }
3159
Yan Zhenga512bbf2008-12-08 16:46:26 -05003160 *logical = buf;
3161 *naddrs = nr;
3162 *stripe_len = map->stripe_len;
3163
3164 free_extent_map(em);
3165 return 0;
3166}
3167
Chris Mason8790d502008-04-03 16:29:03 -04003168static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003169{
Chris Masoncea9e442008-04-09 16:28:12 -04003170 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003171 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003172
Chris Mason8790d502008-04-03 16:29:03 -04003173 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003174 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003175
Chris Mason7d2b4da2008-08-05 10:13:57 -04003176 if (bio == multi->orig_bio)
3177 is_orig_bio = 1;
3178
Chris Masoncea9e442008-04-09 16:28:12 -04003179 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003180 if (!is_orig_bio) {
3181 bio_put(bio);
3182 bio = multi->orig_bio;
3183 }
Chris Mason8790d502008-04-03 16:29:03 -04003184 bio->bi_private = multi->private;
3185 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003186 /* only send an error to the higher layers if it is
3187 * beyond the tolerance of the multi-bio
3188 */
Chris Mason1259ab72008-05-12 13:39:03 -04003189 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003190 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003191 } else if (err) {
3192 /*
3193 * this bio is actually up to date, we didn't
3194 * go over the max number of errors
3195 */
3196 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003197 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003198 }
Chris Mason8790d502008-04-03 16:29:03 -04003199 kfree(multi);
3200
3201 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003202 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003203 bio_put(bio);
3204 }
Chris Mason8790d502008-04-03 16:29:03 -04003205}
3206
Chris Mason8b712842008-06-11 16:50:36 -04003207struct async_sched {
3208 struct bio *bio;
3209 int rw;
3210 struct btrfs_fs_info *info;
3211 struct btrfs_work work;
3212};
3213
3214/*
3215 * see run_scheduled_bios for a description of why bios are collected for
3216 * async submit.
3217 *
3218 * This will add one bio to the pending list for a device and make sure
3219 * the work struct is scheduled.
3220 */
Chris Masond3977122009-01-05 21:25:51 -05003221static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003222 struct btrfs_device *device,
3223 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003224{
3225 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003226 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003227
3228 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003229 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003230 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003231 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003232 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003233 return 0;
3234 }
3235
3236 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003237 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003238 * higher layers. Otherwise, the async bio makes it appear we have
3239 * made progress against dirty pages when we've really just put it
3240 * on a queue for later
3241 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003242 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003243 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003244 bio->bi_next = NULL;
3245 bio->bi_rw |= rw;
3246
3247 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003248 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003249 pending_bios = &device->pending_sync_bios;
3250 else
3251 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003252
Chris Masonffbd5172009-04-20 15:50:09 -04003253 if (pending_bios->tail)
3254 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003255
Chris Masonffbd5172009-04-20 15:50:09 -04003256 pending_bios->tail = bio;
3257 if (!pending_bios->head)
3258 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003259 if (device->running_pending)
3260 should_queue = 0;
3261
3262 spin_unlock(&device->io_lock);
3263
3264 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003265 btrfs_queue_worker(&root->fs_info->submit_workers,
3266 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003267 return 0;
3268}
3269
Chris Masonf1885912008-04-09 16:28:12 -04003270int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003271 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003272{
3273 struct btrfs_mapping_tree *map_tree;
3274 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003275 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003276 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003277 u64 length = 0;
3278 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003279 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003280 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003281 int dev_nr = 0;
3282 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003283
Chris Masonf2d8d742008-04-21 10:03:05 -04003284 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003285 map_tree = &root->fs_info->mapping_tree;
3286 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003287
Chris Masonf1885912008-04-09 16:28:12 -04003288 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3289 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003290 BUG_ON(ret);
3291
3292 total_devs = multi->num_stripes;
3293 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003294 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3295 "len %llu\n", (unsigned long long)logical,
3296 (unsigned long long)length,
3297 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003298 BUG();
3299 }
3300 multi->end_io = first_bio->bi_end_io;
3301 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003302 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003303 atomic_set(&multi->stripes_pending, multi->num_stripes);
3304
Chris Masond3977122009-01-05 21:25:51 -05003305 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003306 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003307 if (dev_nr < total_devs - 1) {
3308 bio = bio_clone(first_bio, GFP_NOFS);
3309 BUG_ON(!bio);
3310 } else {
3311 bio = first_bio;
3312 }
3313 bio->bi_private = multi;
3314 bio->bi_end_io = end_bio_multi_stripe;
3315 }
Chris Masoncea9e442008-04-09 16:28:12 -04003316 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3317 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003318 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003319 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003320 if (async_submit)
3321 schedule_bio(root, dev, rw, bio);
3322 else
3323 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003324 } else {
3325 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3326 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003327 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003328 }
Chris Mason8790d502008-04-03 16:29:03 -04003329 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003330 }
Chris Masoncea9e442008-04-09 16:28:12 -04003331 if (total_devs == 1)
3332 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003333 return 0;
3334}
3335
Chris Masona4437552008-04-18 10:29:38 -04003336struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003337 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003338{
Yan Zheng2b820322008-11-17 21:11:30 -05003339 struct btrfs_device *device;
3340 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003341
Yan Zheng2b820322008-11-17 21:11:30 -05003342 cur_devices = root->fs_info->fs_devices;
3343 while (cur_devices) {
3344 if (!fsid ||
3345 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3346 device = __find_device(&cur_devices->devices,
3347 devid, uuid);
3348 if (device)
3349 return device;
3350 }
3351 cur_devices = cur_devices->seed;
3352 }
3353 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003354}
3355
Chris Masondfe25022008-05-13 13:46:40 -04003356static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3357 u64 devid, u8 *dev_uuid)
3358{
3359 struct btrfs_device *device;
3360 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3361
3362 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003363 if (!device)
3364 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003365 list_add(&device->dev_list,
3366 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003367 device->dev_root = root->fs_info->dev_root;
3368 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003369 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003370 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003371 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003372 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003373 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003374 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003375 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003376 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3377 return device;
3378}
3379
Chris Mason0b86a832008-03-24 15:01:56 -04003380static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3381 struct extent_buffer *leaf,
3382 struct btrfs_chunk *chunk)
3383{
3384 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3385 struct map_lookup *map;
3386 struct extent_map *em;
3387 u64 logical;
3388 u64 length;
3389 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003390 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003391 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003392 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003393 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003394
Chris Masone17cade2008-04-15 15:41:47 -04003395 logical = key->offset;
3396 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003397
Chris Mason890871b2009-09-02 16:24:52 -04003398 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003399 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003400 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003401
3402 /* already mapped? */
3403 if (em && em->start <= logical && em->start + em->len > logical) {
3404 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003405 return 0;
3406 } else if (em) {
3407 free_extent_map(em);
3408 }
Chris Mason0b86a832008-03-24 15:01:56 -04003409
David Sterba172ddd62011-04-21 00:48:27 +02003410 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003411 if (!em)
3412 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003413 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3414 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003415 if (!map) {
3416 free_extent_map(em);
3417 return -ENOMEM;
3418 }
3419
3420 em->bdev = (struct block_device *)map;
3421 em->start = logical;
3422 em->len = length;
3423 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003424 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003425
Chris Mason593060d2008-03-25 16:50:33 -04003426 map->num_stripes = num_stripes;
3427 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3428 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3429 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3430 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3431 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003432 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003433 for (i = 0; i < num_stripes; i++) {
3434 map->stripes[i].physical =
3435 btrfs_stripe_offset_nr(leaf, chunk, i);
3436 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003437 read_extent_buffer(leaf, uuid, (unsigned long)
3438 btrfs_stripe_dev_uuid_nr(chunk, i),
3439 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003440 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3441 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003442 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003443 kfree(map);
3444 free_extent_map(em);
3445 return -EIO;
3446 }
Chris Masondfe25022008-05-13 13:46:40 -04003447 if (!map->stripes[i].dev) {
3448 map->stripes[i].dev =
3449 add_missing_dev(root, devid, uuid);
3450 if (!map->stripes[i].dev) {
3451 kfree(map);
3452 free_extent_map(em);
3453 return -EIO;
3454 }
3455 }
3456 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003457 }
3458
Chris Mason890871b2009-09-02 16:24:52 -04003459 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003460 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003461 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003462 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003463 free_extent_map(em);
3464
3465 return 0;
3466}
3467
3468static int fill_device_from_item(struct extent_buffer *leaf,
3469 struct btrfs_dev_item *dev_item,
3470 struct btrfs_device *device)
3471{
3472 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003473
3474 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003475 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3476 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003477 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3478 device->type = btrfs_device_type(leaf, dev_item);
3479 device->io_align = btrfs_device_io_align(leaf, dev_item);
3480 device->io_width = btrfs_device_io_width(leaf, dev_item);
3481 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003482
3483 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003484 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003485
Chris Mason0b86a832008-03-24 15:01:56 -04003486 return 0;
3487}
3488
Yan Zheng2b820322008-11-17 21:11:30 -05003489static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3490{
3491 struct btrfs_fs_devices *fs_devices;
3492 int ret;
3493
3494 mutex_lock(&uuid_mutex);
3495
3496 fs_devices = root->fs_info->fs_devices->seed;
3497 while (fs_devices) {
3498 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3499 ret = 0;
3500 goto out;
3501 }
3502 fs_devices = fs_devices->seed;
3503 }
3504
3505 fs_devices = find_fsid(fsid);
3506 if (!fs_devices) {
3507 ret = -ENOENT;
3508 goto out;
3509 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003510
3511 fs_devices = clone_fs_devices(fs_devices);
3512 if (IS_ERR(fs_devices)) {
3513 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003514 goto out;
3515 }
3516
Christoph Hellwig97288f22008-12-02 06:36:09 -05003517 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003518 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003519 if (ret)
3520 goto out;
3521
3522 if (!fs_devices->seeding) {
3523 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003524 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003525 ret = -EINVAL;
3526 goto out;
3527 }
3528
3529 fs_devices->seed = root->fs_info->fs_devices->seed;
3530 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003531out:
3532 mutex_unlock(&uuid_mutex);
3533 return ret;
3534}
3535
Chris Mason0d81ba52008-03-24 15:02:07 -04003536static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003537 struct extent_buffer *leaf,
3538 struct btrfs_dev_item *dev_item)
3539{
3540 struct btrfs_device *device;
3541 u64 devid;
3542 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003543 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003544 u8 dev_uuid[BTRFS_UUID_SIZE];
3545
Chris Mason0b86a832008-03-24 15:01:56 -04003546 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003547 read_extent_buffer(leaf, dev_uuid,
3548 (unsigned long)btrfs_device_uuid(dev_item),
3549 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003550 read_extent_buffer(leaf, fs_uuid,
3551 (unsigned long)btrfs_device_fsid(dev_item),
3552 BTRFS_UUID_SIZE);
3553
3554 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3555 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003556 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003557 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003558 }
3559
3560 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3561 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003562 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003563 return -EIO;
3564
3565 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003566 printk(KERN_WARNING "warning devid %llu missing\n",
3567 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003568 device = add_missing_dev(root, devid, dev_uuid);
3569 if (!device)
3570 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003571 } else if (!device->missing) {
3572 /*
3573 * this happens when a device that was properly setup
3574 * in the device info lists suddenly goes bad.
3575 * device->bdev is NULL, and so we have to set
3576 * device->missing to one here
3577 */
3578 root->fs_info->fs_devices->missing_devices++;
3579 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003580 }
3581 }
3582
3583 if (device->fs_devices != root->fs_info->fs_devices) {
3584 BUG_ON(device->writeable);
3585 if (device->generation !=
3586 btrfs_device_generation(leaf, dev_item))
3587 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003588 }
Chris Mason0b86a832008-03-24 15:01:56 -04003589
3590 fill_device_from_item(leaf, dev_item, device);
3591 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003592 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003593 if (device->writeable)
3594 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003595 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003596 return ret;
3597}
3598
Yan Zhenge4404d62008-12-12 10:03:26 -05003599int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003600{
3601 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003602 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003603 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003604 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003605 u8 *ptr;
3606 unsigned long sb_ptr;
3607 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003608 u32 num_stripes;
3609 u32 array_size;
3610 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003611 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003612 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003613
Yan Zhenge4404d62008-12-12 10:03:26 -05003614 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003615 BTRFS_SUPER_INFO_SIZE);
3616 if (!sb)
3617 return -ENOMEM;
3618 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04003619 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05003620
Chris Masona061fc82008-05-07 11:43:44 -04003621 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003622 array_size = btrfs_super_sys_array_size(super_copy);
3623
Chris Mason0b86a832008-03-24 15:01:56 -04003624 ptr = super_copy->sys_chunk_array;
3625 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3626 cur = 0;
3627
3628 while (cur < array_size) {
3629 disk_key = (struct btrfs_disk_key *)ptr;
3630 btrfs_disk_key_to_cpu(&key, disk_key);
3631
Chris Masona061fc82008-05-07 11:43:44 -04003632 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003633 sb_ptr += len;
3634 cur += len;
3635
Chris Mason0d81ba52008-03-24 15:02:07 -04003636 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003637 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003638 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003639 if (ret)
3640 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003641 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3642 len = btrfs_chunk_item_size(num_stripes);
3643 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003644 ret = -EIO;
3645 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003646 }
3647 ptr += len;
3648 sb_ptr += len;
3649 cur += len;
3650 }
Chris Masona061fc82008-05-07 11:43:44 -04003651 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003652 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003653}
3654
3655int btrfs_read_chunk_tree(struct btrfs_root *root)
3656{
3657 struct btrfs_path *path;
3658 struct extent_buffer *leaf;
3659 struct btrfs_key key;
3660 struct btrfs_key found_key;
3661 int ret;
3662 int slot;
3663
3664 root = root->fs_info->chunk_root;
3665
3666 path = btrfs_alloc_path();
3667 if (!path)
3668 return -ENOMEM;
3669
3670 /* first we search for all of the device items, and then we
3671 * read in all of the chunk items. This way we can create chunk
3672 * mappings that reference all of the devices that are afound
3673 */
3674 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3675 key.offset = 0;
3676 key.type = 0;
3677again:
3678 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003679 if (ret < 0)
3680 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003681 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003682 leaf = path->nodes[0];
3683 slot = path->slots[0];
3684 if (slot >= btrfs_header_nritems(leaf)) {
3685 ret = btrfs_next_leaf(root, path);
3686 if (ret == 0)
3687 continue;
3688 if (ret < 0)
3689 goto error;
3690 break;
3691 }
3692 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3693 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3694 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3695 break;
3696 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3697 struct btrfs_dev_item *dev_item;
3698 dev_item = btrfs_item_ptr(leaf, slot,
3699 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003700 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003701 if (ret)
3702 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003703 }
3704 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3705 struct btrfs_chunk *chunk;
3706 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3707 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003708 if (ret)
3709 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003710 }
3711 path->slots[0]++;
3712 }
3713 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3714 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003715 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003716 goto again;
3717 }
Chris Mason0b86a832008-03-24 15:01:56 -04003718 ret = 0;
3719error:
Yan Zheng2b820322008-11-17 21:11:30 -05003720 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003721 return ret;
3722}