blob: f8e2943101a11b43efeac758643d54e85abb9ee4 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Chris Mason593060d2008-03-25 16:50:33 -040026#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "ctree.h"
29#include "extent_map.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "print-tree.h"
33#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Yan Zheng2b820322008-11-17 21:11:30 -050036static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
40
Chris Mason8a4b83c2008-03-24 15:02:07 -040041static DEFINE_MUTEX(uuid_mutex);
42static LIST_HEAD(fs_uuids);
43
Chris Mason7d9eb122008-07-08 14:19:17 -040044static void lock_chunks(struct btrfs_root *root)
45{
Chris Mason7d9eb122008-07-08 14:19:17 -040046 mutex_lock(&root->fs_info->chunk_mutex);
47}
48
49static void unlock_chunks(struct btrfs_root *root)
50{
Chris Mason7d9eb122008-07-08 14:19:17 -040051 mutex_unlock(&root->fs_info->chunk_mutex);
52}
53
Yan Zhenge4404d62008-12-12 10:03:26 -050054static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
55{
56 struct btrfs_device *device;
57 WARN_ON(fs_devices->opened);
58 while (!list_empty(&fs_devices->devices)) {
59 device = list_entry(fs_devices->devices.next,
60 struct btrfs_device, dev_list);
61 list_del(&device->dev_list);
62 kfree(device->name);
63 kfree(device);
64 }
65 kfree(fs_devices);
66}
67
Chris Mason8a4b83c2008-03-24 15:02:07 -040068int btrfs_cleanup_fs_uuids(void)
69{
70 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040071
Yan Zheng2b820322008-11-17 21:11:30 -050072 while (!list_empty(&fs_uuids)) {
73 fs_devices = list_entry(fs_uuids.next,
74 struct btrfs_fs_devices, list);
75 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050076 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040077 }
78 return 0;
79}
80
Chris Masona1b32a52008-09-05 16:09:51 -040081static noinline struct btrfs_device *__find_device(struct list_head *head,
82 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040083{
84 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040085
Qinghuang Fengc6e30872009-01-21 10:59:08 -050086 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040087 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040088 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040089 return dev;
Chris Masona4437552008-04-18 10:29:38 -040090 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 }
92 return NULL;
93}
94
Chris Masona1b32a52008-09-05 16:09:51 -040095static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040096{
Chris Mason8a4b83c2008-03-24 15:02:07 -040097 struct btrfs_fs_devices *fs_devices;
98
Qinghuang Fengc6e30872009-01-21 10:59:08 -050099 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400100 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
101 return fs_devices;
102 }
103 return NULL;
104}
105
Chris Masonffbd5172009-04-20 15:50:09 -0400106static void requeue_list(struct btrfs_pending_bios *pending_bios,
107 struct bio *head, struct bio *tail)
108{
109
110 struct bio *old_head;
111
112 old_head = pending_bios->head;
113 pending_bios->head = head;
114 if (pending_bios->tail)
115 tail->bi_next = old_head;
116 else
117 pending_bios->tail = tail;
118}
119
Chris Mason8b712842008-06-11 16:50:36 -0400120/*
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
124 *
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
130 */
Chris Masond3977122009-01-05 21:25:51 -0500131static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400132{
133 struct bio *pending;
134 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400135 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400136 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400137 struct bio *tail;
138 struct bio *cur;
139 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400140 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400141 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400142 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400143 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400144 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000145 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400146 struct blk_plug plug;
147
148 /*
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
153 */
154 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400155
Chris Masonbedf7622009-04-03 10:32:58 -0400156 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400157 fs_info = device->dev_root->fs_info;
158 limit = btrfs_async_submit_limit(fs_info);
159 limit = limit * 2 / 3;
160
Chris Mason8b712842008-06-11 16:50:36 -0400161loop:
162 spin_lock(&device->io_lock);
163
Chris Masona6837052009-02-04 09:19:41 -0500164loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400165 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400166
Chris Mason8b712842008-06-11 16:50:36 -0400167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
171 */
Chris Masond84275c2009-06-09 15:39:08 -0400172 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400173 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400174 force_reg = 1;
175 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400176 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400177 force_reg = 0;
178 }
Chris Masonffbd5172009-04-20 15:50:09 -0400179
180 pending = pending_bios->head;
181 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400182 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400183
184 /*
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
188 *
189 * device->running_pending is used to synchronize with the
190 * schedule_bio code.
191 */
Chris Masonffbd5172009-04-20 15:50:09 -0400192 if (device->pending_sync_bios.head == NULL &&
193 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400194 again = 0;
195 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400196 } else {
197 again = 1;
198 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400199 }
Chris Masonffbd5172009-04-20 15:50:09 -0400200
201 pending_bios->head = NULL;
202 pending_bios->tail = NULL;
203
Chris Mason8b712842008-06-11 16:50:36 -0400204 spin_unlock(&device->io_lock);
205
Chris Masond3977122009-01-05 21:25:51 -0500206 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400207
208 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
211 */
212 if ((num_run > 32 &&
213 pending_bios != &device->pending_sync_bios &&
214 device->pending_sync_bios.head) ||
215 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
216 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400217 spin_lock(&device->io_lock);
218 requeue_list(pending_bios, pending, tail);
219 goto loop_lock;
220 }
221
Chris Mason8b712842008-06-11 16:50:36 -0400222 cur = pending;
223 pending = pending->bi_next;
224 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400225 atomic_dec(&fs_info->nr_async_bios);
226
227 if (atomic_read(&fs_info->nr_async_bios) < limit &&
228 waitqueue_active(&fs_info->async_submit_wait))
229 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400230
231 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400232
Chris Mason2ab1ba62011-08-04 14:28:36 -0400233 /*
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
236 *
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
239 * we add more
240 */
241 if (pending_bios == &device->pending_sync_bios) {
242 sync_pending = 1;
243 } else if (sync_pending) {
244 blk_finish_plug(&plug);
245 blk_start_plug(&plug);
246 sync_pending = 0;
247 }
248
Chris Mason5ff7ba32010-03-15 10:21:30 -0400249 submit_bio(cur->bi_rw, cur);
250 num_run++;
251 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100252 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400253 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400254
255 /*
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
258 * run instead
259 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400260 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500261 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400262 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400263
Chris Masonb765ead2009-04-03 10:27:10 -0400264 ioc = current->io_context;
265
266 /*
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
270 *
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
274 */
275 if (ioc && ioc->nr_batch_requests > 0 &&
276 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
277 (last_waited == 0 ||
278 ioc->last_waited == last_waited)) {
279 /*
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
284 */
285 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100286 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400287 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400288 continue;
289 }
Chris Mason8b712842008-06-11 16:50:36 -0400290 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400291 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500292 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400293
294 spin_unlock(&device->io_lock);
295 btrfs_requeue_work(&device->work);
296 goto done;
297 }
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
Arne Jansen90519d62011-05-23 14:30:00 +0200369 /* init readahead state */
370 spin_lock_init(&device->reada_lock);
371 device->reada_curr_zone = NULL;
372 atomic_set(&device->reada_in_flight, 0);
373 device->reada_next = 0;
374 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
375 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
376
Chris Masone5e9a522009-06-10 15:17:02 -0400377 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000378 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400379 mutex_unlock(&fs_devices->device_list_mutex);
380
Yan Zheng2b820322008-11-17 21:11:30 -0500381 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400382 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500383 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000384 name = kstrdup(path, GFP_NOFS);
385 if (!name)
386 return -ENOMEM;
387 kfree(device->name);
388 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500389 if (device->missing) {
390 fs_devices->missing_devices--;
391 device->missing = 0;
392 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400393 }
394
395 if (found_transid > fs_devices->latest_trans) {
396 fs_devices->latest_devid = devid;
397 fs_devices->latest_trans = found_transid;
398 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 *fs_devices_ret = fs_devices;
400 return 0;
401}
402
Yan Zhenge4404d62008-12-12 10:03:26 -0500403static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
404{
405 struct btrfs_fs_devices *fs_devices;
406 struct btrfs_device *device;
407 struct btrfs_device *orig_dev;
408
409 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
410 if (!fs_devices)
411 return ERR_PTR(-ENOMEM);
412
413 INIT_LIST_HEAD(&fs_devices->devices);
414 INIT_LIST_HEAD(&fs_devices->alloc_list);
415 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400416 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500417 fs_devices->latest_devid = orig->latest_devid;
418 fs_devices->latest_trans = orig->latest_trans;
419 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
420
Xiao Guangrong46224702011-04-20 10:08:47 +0000421 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500422 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
423 device = kzalloc(sizeof(*device), GFP_NOFS);
424 if (!device)
425 goto error;
426
427 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400428 if (!device->name) {
429 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500430 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400431 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500432
433 device->devid = orig_dev->devid;
434 device->work.func = pending_bios_fn;
435 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 spin_lock_init(&device->io_lock);
437 INIT_LIST_HEAD(&device->dev_list);
438 INIT_LIST_HEAD(&device->dev_alloc_list);
439
440 list_add(&device->dev_list, &fs_devices->devices);
441 device->fs_devices = fs_devices;
442 fs_devices->num_devices++;
443 }
444 return fs_devices;
445error:
446 free_fs_devices(fs_devices);
447 return ERR_PTR(-ENOMEM);
448}
449
Chris Masondfe25022008-05-13 13:46:40 -0400450int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
451{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500452 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400453
454 mutex_lock(&uuid_mutex);
455again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000456 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500457 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500458 if (device->in_fs_metadata)
459 continue;
460
461 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100462 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500463 device->bdev = NULL;
464 fs_devices->open_devices--;
465 }
466 if (device->writeable) {
467 list_del_init(&device->dev_alloc_list);
468 device->writeable = 0;
469 fs_devices->rw_devices--;
470 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500471 list_del_init(&device->dev_list);
472 fs_devices->num_devices--;
473 kfree(device->name);
474 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400475 }
Yan Zheng2b820322008-11-17 21:11:30 -0500476
477 if (fs_devices->seed) {
478 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500479 goto again;
480 }
481
Chris Masondfe25022008-05-13 13:46:40 -0400482 mutex_unlock(&uuid_mutex);
483 return 0;
484}
Chris Masona0af4692008-05-13 16:03:06 -0400485
Xiao Guangrong1f781602011-04-20 10:09:16 +0000486static void __free_device(struct work_struct *work)
487{
488 struct btrfs_device *device;
489
490 device = container_of(work, struct btrfs_device, rcu_work);
491
492 if (device->bdev)
493 blkdev_put(device->bdev, device->mode);
494
495 kfree(device->name);
496 kfree(device);
497}
498
499static void free_device(struct rcu_head *head)
500{
501 struct btrfs_device *device;
502
503 device = container_of(head, struct btrfs_device, rcu);
504
505 INIT_WORK(&device->rcu_work, __free_device);
506 schedule_work(&device->rcu_work);
507}
508
Yan Zheng2b820322008-11-17 21:11:30 -0500509static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400510{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400511 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500512
Yan Zheng2b820322008-11-17 21:11:30 -0500513 if (--fs_devices->opened > 0)
514 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400515
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000516 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500517 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000518 struct btrfs_device *new_device;
519
520 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400521 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000522
Yan Zheng2b820322008-11-17 21:11:30 -0500523 if (device->writeable) {
524 list_del_init(&device->dev_alloc_list);
525 fs_devices->rw_devices--;
526 }
527
Josef Bacikd5e20032011-08-04 14:52:27 +0000528 if (device->can_discard)
529 fs_devices->num_can_discard--;
530
Xiao Guangrong1f781602011-04-20 10:09:16 +0000531 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
532 BUG_ON(!new_device);
533 memcpy(new_device, device, sizeof(*new_device));
534 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000535 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000536 new_device->bdev = NULL;
537 new_device->writeable = 0;
538 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000539 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000540 list_replace_rcu(&device->dev_list, &new_device->dev_list);
541
542 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400543 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000544 mutex_unlock(&fs_devices->device_list_mutex);
545
Yan Zhenge4404d62008-12-12 10:03:26 -0500546 WARN_ON(fs_devices->open_devices);
547 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500548 fs_devices->opened = 0;
549 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500550
Chris Mason8a4b83c2008-03-24 15:02:07 -0400551 return 0;
552}
553
Yan Zheng2b820322008-11-17 21:11:30 -0500554int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
555{
Yan Zhenge4404d62008-12-12 10:03:26 -0500556 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500557 int ret;
558
559 mutex_lock(&uuid_mutex);
560 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500561 if (!fs_devices->opened) {
562 seed_devices = fs_devices->seed;
563 fs_devices->seed = NULL;
564 }
Yan Zheng2b820322008-11-17 21:11:30 -0500565 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500566
567 while (seed_devices) {
568 fs_devices = seed_devices;
569 seed_devices = fs_devices->seed;
570 __btrfs_close_devices(fs_devices);
571 free_fs_devices(fs_devices);
572 }
Yan Zheng2b820322008-11-17 21:11:30 -0500573 return ret;
574}
575
Yan Zhenge4404d62008-12-12 10:03:26 -0500576static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
577 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578{
Josef Bacikd5e20032011-08-04 14:52:27 +0000579 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400580 struct block_device *bdev;
581 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400582 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400583 struct block_device *latest_bdev = NULL;
584 struct buffer_head *bh;
585 struct btrfs_super_block *disk_super;
586 u64 latest_devid = 0;
587 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400588 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500589 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400590 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400591
Tejun Heod4d77622010-11-13 11:55:18 +0100592 flags |= FMODE_EXCL;
593
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500594 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400595 if (device->bdev)
596 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400597 if (!device->name)
598 continue;
599
Tejun Heod4d77622010-11-13 11:55:18 +0100600 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400601 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500602 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400603 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400604 }
Chris Masona061fc82008-05-07 11:43:44 -0400605 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400606
Yan Zhenga512bbf2008-12-08 16:46:26 -0500607 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300608 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400609 goto error_close;
610
611 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000612 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400613 if (devid != device->devid)
614 goto error_brelse;
615
Yan Zheng2b820322008-11-17 21:11:30 -0500616 if (memcmp(device->uuid, disk_super->dev_item.uuid,
617 BTRFS_UUID_SIZE))
618 goto error_brelse;
619
620 device->generation = btrfs_super_generation(disk_super);
621 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400622 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500623 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400624 latest_bdev = bdev;
625 }
626
Yan Zheng2b820322008-11-17 21:11:30 -0500627 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
628 device->writeable = 0;
629 } else {
630 device->writeable = !bdev_read_only(bdev);
631 seeding = 0;
632 }
633
Josef Bacikd5e20032011-08-04 14:52:27 +0000634 q = bdev_get_queue(bdev);
635 if (blk_queue_discard(q)) {
636 device->can_discard = 1;
637 fs_devices->num_can_discard++;
638 }
639
Chris Mason8a4b83c2008-03-24 15:02:07 -0400640 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400641 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500642 device->mode = flags;
643
Chris Masonc2898112009-06-10 09:51:32 -0400644 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
645 fs_devices->rotating = 1;
646
Chris Masona0af4692008-05-13 16:03:06 -0400647 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500648 if (device->writeable) {
649 fs_devices->rw_devices++;
650 list_add(&device->dev_alloc_list,
651 &fs_devices->alloc_list);
652 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000653 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400654 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400655
Chris Masona0af4692008-05-13 16:03:06 -0400656error_brelse:
657 brelse(bh);
658error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100659 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400660error:
661 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400662 }
Chris Masona0af4692008-05-13 16:03:06 -0400663 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300664 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400665 goto out;
666 }
Yan Zheng2b820322008-11-17 21:11:30 -0500667 fs_devices->seeding = seeding;
668 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400669 fs_devices->latest_bdev = latest_bdev;
670 fs_devices->latest_devid = latest_devid;
671 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500672 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400673out:
Yan Zheng2b820322008-11-17 21:11:30 -0500674 return ret;
675}
676
677int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500678 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500679{
680 int ret;
681
682 mutex_lock(&uuid_mutex);
683 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500684 fs_devices->opened++;
685 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500686 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500687 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500688 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400689 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400690 return ret;
691}
692
Christoph Hellwig97288f22008-12-02 06:36:09 -0500693int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400694 struct btrfs_fs_devices **fs_devices_ret)
695{
696 struct btrfs_super_block *disk_super;
697 struct block_device *bdev;
698 struct buffer_head *bh;
699 int ret;
700 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400701 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400702
703 mutex_lock(&uuid_mutex);
704
Tejun Heod4d77622010-11-13 11:55:18 +0100705 flags |= FMODE_EXCL;
706 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400707
708 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709 ret = PTR_ERR(bdev);
710 goto error;
711 }
712
713 ret = set_blocksize(bdev, 4096);
714 if (ret)
715 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500716 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400717 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000718 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400719 goto error_close;
720 }
721 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000722 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400723 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400724 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500725 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200726 else
727 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500728 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500729 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
731
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 brelse(bh);
733error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100734 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400735error:
736 mutex_unlock(&uuid_mutex);
737 return ret;
738}
Chris Mason0b86a832008-03-24 15:01:56 -0400739
Miao Xie6d07bce2011-01-05 10:07:31 +0000740/* helper to account the used device space in the range */
741int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
742 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400743{
744 struct btrfs_key key;
745 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000746 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500747 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000748 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400749 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000750 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400751 struct extent_buffer *l;
752
Miao Xie6d07bce2011-01-05 10:07:31 +0000753 *length = 0;
754
755 if (start >= device->total_bytes)
756 return 0;
757
Yan Zheng2b820322008-11-17 21:11:30 -0500758 path = btrfs_alloc_path();
759 if (!path)
760 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400761 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400762
Chris Mason0b86a832008-03-24 15:01:56 -0400763 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000764 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400765 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000766
767 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400768 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000769 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400770 if (ret > 0) {
771 ret = btrfs_previous_item(root, path, key.objectid, key.type);
772 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000773 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400774 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000775
Chris Mason0b86a832008-03-24 15:01:56 -0400776 while (1) {
777 l = path->nodes[0];
778 slot = path->slots[0];
779 if (slot >= btrfs_header_nritems(l)) {
780 ret = btrfs_next_leaf(root, path);
781 if (ret == 0)
782 continue;
783 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000784 goto out;
785
786 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400787 }
788 btrfs_item_key_to_cpu(l, &key, slot);
789
790 if (key.objectid < device->devid)
791 goto next;
792
793 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000794 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400795
Chris Masond3977122009-01-05 21:25:51 -0500796 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400797 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400798
Chris Mason0b86a832008-03-24 15:01:56 -0400799 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000800 extent_end = key.offset + btrfs_dev_extent_length(l,
801 dev_extent);
802 if (key.offset <= start && extent_end > end) {
803 *length = end - start + 1;
804 break;
805 } else if (key.offset <= start && extent_end > start)
806 *length += extent_end - start;
807 else if (key.offset > start && extent_end <= end)
808 *length += extent_end - key.offset;
809 else if (key.offset > start && key.offset <= end) {
810 *length += end - key.offset + 1;
811 break;
812 } else if (key.offset > end)
813 break;
814
815next:
816 path->slots[0]++;
817 }
818 ret = 0;
819out:
820 btrfs_free_path(path);
821 return ret;
822}
823
Chris Mason0b86a832008-03-24 15:01:56 -0400824/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000825 * find_free_dev_extent - find free space in the specified device
826 * @trans: transaction handler
827 * @device: the device which we search the free space in
828 * @num_bytes: the size of the free space that we need
829 * @start: store the start of the free space.
830 * @len: the size of the free space. that we find, or the size of the max
831 * free space if we don't find suitable free space
832 *
Chris Mason0b86a832008-03-24 15:01:56 -0400833 * this uses a pretty simple search, the expectation is that it is
834 * called very infrequently and that a given device has a small number
835 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000836 *
837 * @start is used to store the start of the free space if we find. But if we
838 * don't find suitable free space, it will be used to store the start position
839 * of the max free space.
840 *
841 * @len is used to store the size of the free space that we find.
842 * But if we don't find suitable free space, it is used to store the size of
843 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400844 */
845int find_free_dev_extent(struct btrfs_trans_handle *trans,
846 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400848{
849 struct btrfs_key key;
850 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000851 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400852 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000853 u64 hole_size;
854 u64 max_hole_start;
855 u64 max_hole_size;
856 u64 extent_end;
857 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400858 u64 search_end = device->total_bytes;
859 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000860 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400861 struct extent_buffer *l;
862
Chris Mason0b86a832008-03-24 15:01:56 -0400863 /* FIXME use last free of some kind */
864
865 /* we don't want to overwrite the superblock on the drive,
866 * so we make sure to start at an offset of at least 1MB
867 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200868 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400869
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 max_hole_start = search_start;
871 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000872 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000873
874 if (search_start >= search_end) {
875 ret = -ENOSPC;
876 goto error;
877 }
878
879 path = btrfs_alloc_path();
880 if (!path) {
881 ret = -ENOMEM;
882 goto error;
883 }
884 path->reada = 2;
885
Chris Mason0b86a832008-03-24 15:01:56 -0400886 key.objectid = device->devid;
887 key.offset = search_start;
888 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000889
Chris Mason0b86a832008-03-24 15:01:56 -0400890 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
891 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000892 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400893 if (ret > 0) {
894 ret = btrfs_previous_item(root, path, key.objectid, key.type);
895 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000896 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400897 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000898
Chris Mason0b86a832008-03-24 15:01:56 -0400899 while (1) {
900 l = path->nodes[0];
901 slot = path->slots[0];
902 if (slot >= btrfs_header_nritems(l)) {
903 ret = btrfs_next_leaf(root, path);
904 if (ret == 0)
905 continue;
906 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000907 goto out;
908
909 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400910 }
911 btrfs_item_key_to_cpu(l, &key, slot);
912
913 if (key.objectid < device->devid)
914 goto next;
915
916 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000917 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400918
Chris Mason0b86a832008-03-24 15:01:56 -0400919 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
920 goto next;
921
Miao Xie7bfc8372011-01-05 10:07:26 +0000922 if (key.offset > search_start) {
923 hole_size = key.offset - search_start;
924
925 if (hole_size > max_hole_size) {
926 max_hole_start = search_start;
927 max_hole_size = hole_size;
928 }
929
930 /*
931 * If this free space is greater than which we need,
932 * it must be the max free space that we have found
933 * until now, so max_hole_start must point to the start
934 * of this free space and the length of this free space
935 * is stored in max_hole_size. Thus, we return
936 * max_hole_start and max_hole_size and go back to the
937 * caller.
938 */
939 if (hole_size >= num_bytes) {
940 ret = 0;
941 goto out;
942 }
943 }
944
Chris Mason0b86a832008-03-24 15:01:56 -0400945 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000946 extent_end = key.offset + btrfs_dev_extent_length(l,
947 dev_extent);
948 if (extent_end > search_start)
949 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400950next:
951 path->slots[0]++;
952 cond_resched();
953 }
Chris Mason0b86a832008-03-24 15:01:56 -0400954
liubo38c01b92011-08-02 02:39:03 +0000955 /*
956 * At this point, search_start should be the end of
957 * allocated dev extents, and when shrinking the device,
958 * search_end may be smaller than search_start.
959 */
960 if (search_end > search_start)
961 hole_size = search_end - search_start;
962
Miao Xie7bfc8372011-01-05 10:07:26 +0000963 if (hole_size > max_hole_size) {
964 max_hole_start = search_start;
965 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400966 }
Chris Mason0b86a832008-03-24 15:01:56 -0400967
Miao Xie7bfc8372011-01-05 10:07:26 +0000968 /* See above. */
969 if (hole_size < num_bytes)
970 ret = -ENOSPC;
971 else
972 ret = 0;
973
974out:
Yan Zheng2b820322008-11-17 21:11:30 -0500975 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000976error:
977 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000978 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000979 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400980 return ret;
981}
982
Christoph Hellwigb2950862008-12-02 09:54:17 -0500983static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400984 struct btrfs_device *device,
985 u64 start)
986{
987 int ret;
988 struct btrfs_path *path;
989 struct btrfs_root *root = device->dev_root;
990 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400991 struct btrfs_key found_key;
992 struct extent_buffer *leaf = NULL;
993 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400994
995 path = btrfs_alloc_path();
996 if (!path)
997 return -ENOMEM;
998
999 key.objectid = device->devid;
1000 key.offset = start;
1001 key.type = BTRFS_DEV_EXTENT_KEY;
1002
1003 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001004 if (ret > 0) {
1005 ret = btrfs_previous_item(root, path, key.objectid,
1006 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001007 if (ret)
1008 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001009 leaf = path->nodes[0];
1010 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1011 extent = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_dev_extent);
1013 BUG_ON(found_key.offset > start || found_key.offset +
1014 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -04001015 } else if (ret == 0) {
1016 leaf = path->nodes[0];
1017 extent = btrfs_item_ptr(leaf, path->slots[0],
1018 struct btrfs_dev_extent);
1019 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001020 BUG_ON(ret);
1021
Josef Bacik2bf64752011-09-26 17:12:22 -04001022 if (device->bytes_used > 0) {
1023 u64 len = btrfs_dev_extent_length(leaf, extent);
1024 device->bytes_used -= len;
1025 spin_lock(&root->fs_info->free_chunk_lock);
1026 root->fs_info->free_chunk_space += len;
1027 spin_unlock(&root->fs_info->free_chunk_lock);
1028 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001029 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001030
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001031out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001032 btrfs_free_path(path);
1033 return ret;
1034}
1035
Yan Zheng2b820322008-11-17 21:11:30 -05001036int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001037 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001038 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001039 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001040{
1041 int ret;
1042 struct btrfs_path *path;
1043 struct btrfs_root *root = device->dev_root;
1044 struct btrfs_dev_extent *extent;
1045 struct extent_buffer *leaf;
1046 struct btrfs_key key;
1047
Chris Masondfe25022008-05-13 13:46:40 -04001048 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001049 path = btrfs_alloc_path();
1050 if (!path)
1051 return -ENOMEM;
1052
Chris Mason0b86a832008-03-24 15:01:56 -04001053 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001054 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001055 key.type = BTRFS_DEV_EXTENT_KEY;
1056 ret = btrfs_insert_empty_item(trans, root, path, &key,
1057 sizeof(*extent));
1058 BUG_ON(ret);
1059
1060 leaf = path->nodes[0];
1061 extent = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001063 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1064 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1065 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1066
1067 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1068 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1069 BTRFS_UUID_SIZE);
1070
Chris Mason0b86a832008-03-24 15:01:56 -04001071 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1072 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001073 btrfs_free_path(path);
1074 return ret;
1075}
1076
Chris Masona1b32a52008-09-05 16:09:51 -04001077static noinline int find_next_chunk(struct btrfs_root *root,
1078 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001079{
1080 struct btrfs_path *path;
1081 int ret;
1082 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001083 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001084 struct btrfs_key found_key;
1085
1086 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001087 if (!path)
1088 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001089
Chris Masone17cade2008-04-15 15:41:47 -04001090 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001091 key.offset = (u64)-1;
1092 key.type = BTRFS_CHUNK_ITEM_KEY;
1093
1094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1095 if (ret < 0)
1096 goto error;
1097
1098 BUG_ON(ret == 0);
1099
1100 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1101 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001102 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001103 } else {
1104 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1105 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001106 if (found_key.objectid != objectid)
1107 *offset = 0;
1108 else {
1109 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1110 struct btrfs_chunk);
1111 *offset = found_key.offset +
1112 btrfs_chunk_length(path->nodes[0], chunk);
1113 }
Chris Mason0b86a832008-03-24 15:01:56 -04001114 }
1115 ret = 0;
1116error:
1117 btrfs_free_path(path);
1118 return ret;
1119}
1120
Yan Zheng2b820322008-11-17 21:11:30 -05001121static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001122{
1123 int ret;
1124 struct btrfs_key key;
1125 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001126 struct btrfs_path *path;
1127
1128 root = root->fs_info->chunk_root;
1129
1130 path = btrfs_alloc_path();
1131 if (!path)
1132 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001133
1134 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1135 key.type = BTRFS_DEV_ITEM_KEY;
1136 key.offset = (u64)-1;
1137
1138 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1139 if (ret < 0)
1140 goto error;
1141
1142 BUG_ON(ret == 0);
1143
1144 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1145 BTRFS_DEV_ITEM_KEY);
1146 if (ret) {
1147 *objectid = 1;
1148 } else {
1149 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1150 path->slots[0]);
1151 *objectid = found_key.offset + 1;
1152 }
1153 ret = 0;
1154error:
Yan Zheng2b820322008-11-17 21:11:30 -05001155 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001156 return ret;
1157}
1158
1159/*
1160 * the device information is stored in the chunk root
1161 * the btrfs_device struct should be fully filled in
1162 */
1163int btrfs_add_device(struct btrfs_trans_handle *trans,
1164 struct btrfs_root *root,
1165 struct btrfs_device *device)
1166{
1167 int ret;
1168 struct btrfs_path *path;
1169 struct btrfs_dev_item *dev_item;
1170 struct extent_buffer *leaf;
1171 struct btrfs_key key;
1172 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001173
1174 root = root->fs_info->chunk_root;
1175
1176 path = btrfs_alloc_path();
1177 if (!path)
1178 return -ENOMEM;
1179
Chris Mason0b86a832008-03-24 15:01:56 -04001180 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1181 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001182 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001183
1184 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001185 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001186 if (ret)
1187 goto out;
1188
1189 leaf = path->nodes[0];
1190 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1191
1192 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001193 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001194 btrfs_set_device_type(leaf, dev_item, device->type);
1195 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1196 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1197 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001198 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1199 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001200 btrfs_set_device_group(leaf, dev_item, 0);
1201 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1202 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001203 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001204
Chris Mason0b86a832008-03-24 15:01:56 -04001205 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001206 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001207 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1208 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001209 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001210
Yan Zheng2b820322008-11-17 21:11:30 -05001211 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001212out:
1213 btrfs_free_path(path);
1214 return ret;
1215}
Chris Mason8f18cf12008-04-25 16:53:30 -04001216
Chris Masona061fc82008-05-07 11:43:44 -04001217static int btrfs_rm_dev_item(struct btrfs_root *root,
1218 struct btrfs_device *device)
1219{
1220 int ret;
1221 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001222 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001223 struct btrfs_trans_handle *trans;
1224
1225 root = root->fs_info->chunk_root;
1226
1227 path = btrfs_alloc_path();
1228 if (!path)
1229 return -ENOMEM;
1230
Yan, Zhenga22285a2010-05-16 10:48:46 -04001231 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001232 if (IS_ERR(trans)) {
1233 btrfs_free_path(path);
1234 return PTR_ERR(trans);
1235 }
Chris Masona061fc82008-05-07 11:43:44 -04001236 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1237 key.type = BTRFS_DEV_ITEM_KEY;
1238 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001239 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001240
1241 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1242 if (ret < 0)
1243 goto out;
1244
1245 if (ret > 0) {
1246 ret = -ENOENT;
1247 goto out;
1248 }
1249
1250 ret = btrfs_del_item(trans, root, path);
1251 if (ret)
1252 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001253out:
1254 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001255 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001256 btrfs_commit_transaction(trans, root);
1257 return ret;
1258}
1259
1260int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1261{
1262 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001263 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001264 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001265 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001266 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001267 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001268 u64 all_avail;
1269 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001270 u64 num_devices;
1271 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001272 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001273 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001274
Chris Masona061fc82008-05-07 11:43:44 -04001275 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001276 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001277
1278 all_avail = root->fs_info->avail_data_alloc_bits |
1279 root->fs_info->avail_system_alloc_bits |
1280 root->fs_info->avail_metadata_alloc_bits;
1281
1282 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001283 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001284 printk(KERN_ERR "btrfs: unable to go below four devices "
1285 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001286 ret = -EINVAL;
1287 goto out;
1288 }
1289
1290 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001291 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001292 printk(KERN_ERR "btrfs: unable to go below two "
1293 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001294 ret = -EINVAL;
1295 goto out;
1296 }
1297
Chris Masondfe25022008-05-13 13:46:40 -04001298 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001299 struct list_head *devices;
1300 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001301
Chris Masondfe25022008-05-13 13:46:40 -04001302 device = NULL;
1303 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001304 /*
1305 * It is safe to read the devices since the volume_mutex
1306 * is held.
1307 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001308 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001309 if (tmp->in_fs_metadata && !tmp->bdev) {
1310 device = tmp;
1311 break;
1312 }
1313 }
1314 bdev = NULL;
1315 bh = NULL;
1316 disk_super = NULL;
1317 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001318 printk(KERN_ERR "btrfs: no missing devices found to "
1319 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001320 goto out;
1321 }
Chris Masondfe25022008-05-13 13:46:40 -04001322 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001323 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1324 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001325 if (IS_ERR(bdev)) {
1326 ret = PTR_ERR(bdev);
1327 goto out;
1328 }
1329
Yan Zheng2b820322008-11-17 21:11:30 -05001330 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001331 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001332 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001333 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001334 goto error_close;
1335 }
1336 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001337 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001338 dev_uuid = disk_super->dev_item.uuid;
1339 device = btrfs_find_device(root, devid, dev_uuid,
1340 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001341 if (!device) {
1342 ret = -ENOENT;
1343 goto error_brelse;
1344 }
Chris Masondfe25022008-05-13 13:46:40 -04001345 }
Yan Zheng2b820322008-11-17 21:11:30 -05001346
1347 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001348 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1349 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001350 ret = -EINVAL;
1351 goto error_brelse;
1352 }
1353
1354 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001355 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001356 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001357 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001358 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001359 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001360 }
Chris Masona061fc82008-05-07 11:43:44 -04001361
1362 ret = btrfs_shrink_device(device, 0);
1363 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001364 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001365
Chris Masona061fc82008-05-07 11:43:44 -04001366 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1367 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001368 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001369
Josef Bacik2bf64752011-09-26 17:12:22 -04001370 spin_lock(&root->fs_info->free_chunk_lock);
1371 root->fs_info->free_chunk_space = device->total_bytes -
1372 device->bytes_used;
1373 spin_unlock(&root->fs_info->free_chunk_lock);
1374
Yan Zheng2b820322008-11-17 21:11:30 -05001375 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001376 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001377
1378 /*
1379 * the device list mutex makes sure that we don't change
1380 * the device list while someone else is writing out all
1381 * the device supers.
1382 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001383
1384 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001385 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001386 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001387
Yan Zhenge4404d62008-12-12 10:03:26 -05001388 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001389
Chris Masoncd02dca2010-12-13 14:56:23 -05001390 if (device->missing)
1391 root->fs_info->fs_devices->missing_devices--;
1392
Yan Zheng2b820322008-11-17 21:11:30 -05001393 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1394 struct btrfs_device, dev_list);
1395 if (device->bdev == root->fs_info->sb->s_bdev)
1396 root->fs_info->sb->s_bdev = next_device->bdev;
1397 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1398 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1399
Xiao Guangrong1f781602011-04-20 10:09:16 +00001400 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001401 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001402
1403 call_rcu(&device->rcu, free_device);
1404 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001405
David Sterba6c417612011-04-13 15:41:04 +02001406 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1407 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001408
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001410 struct btrfs_fs_devices *fs_devices;
1411 fs_devices = root->fs_info->fs_devices;
1412 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001413 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001414 break;
1415 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001416 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001417 fs_devices->seed = cur_devices->seed;
1418 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001419 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001420 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001421 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001422 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001423 }
1424
1425 /*
1426 * at this point, the device is zero sized. We want to
1427 * remove it from the devices list and zero out the old super
1428 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001429 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001430 /* make sure this device isn't detected as part of
1431 * the FS anymore
1432 */
1433 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1434 set_buffer_dirty(bh);
1435 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001436 }
Chris Masona061fc82008-05-07 11:43:44 -04001437
Chris Masona061fc82008-05-07 11:43:44 -04001438 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001439
1440error_brelse:
1441 brelse(bh);
1442error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001443 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001444 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001445out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001446 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001447 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001448 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001449error_undo:
1450 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001451 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001452 list_add(&device->dev_alloc_list,
1453 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001454 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001455 root->fs_info->fs_devices->rw_devices++;
1456 }
1457 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001458}
1459
Yan Zheng2b820322008-11-17 21:11:30 -05001460/*
1461 * does all the dirty work required for changing file system's UUID.
1462 */
1463static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1464 struct btrfs_root *root)
1465{
1466 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1467 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001468 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001469 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001470 struct btrfs_device *device;
1471 u64 super_flags;
1472
1473 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001474 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001475 return -EINVAL;
1476
Yan Zhenge4404d62008-12-12 10:03:26 -05001477 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1478 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001479 return -ENOMEM;
1480
Yan Zhenge4404d62008-12-12 10:03:26 -05001481 old_devices = clone_fs_devices(fs_devices);
1482 if (IS_ERR(old_devices)) {
1483 kfree(seed_devices);
1484 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001485 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001486
Yan Zheng2b820322008-11-17 21:11:30 -05001487 list_add(&old_devices->list, &fs_uuids);
1488
Yan Zhenge4404d62008-12-12 10:03:26 -05001489 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1490 seed_devices->opened = 1;
1491 INIT_LIST_HEAD(&seed_devices->devices);
1492 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001493 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001494
1495 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001496 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1497 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001498 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1499
Yan Zhenge4404d62008-12-12 10:03:26 -05001500 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1501 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1502 device->fs_devices = seed_devices;
1503 }
1504
Yan Zheng2b820322008-11-17 21:11:30 -05001505 fs_devices->seeding = 0;
1506 fs_devices->num_devices = 0;
1507 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001508 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001509
1510 generate_random_uuid(fs_devices->fsid);
1511 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1512 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1513 super_flags = btrfs_super_flags(disk_super) &
1514 ~BTRFS_SUPER_FLAG_SEEDING;
1515 btrfs_set_super_flags(disk_super, super_flags);
1516
1517 return 0;
1518}
1519
1520/*
1521 * strore the expected generation for seed devices in device items.
1522 */
1523static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1524 struct btrfs_root *root)
1525{
1526 struct btrfs_path *path;
1527 struct extent_buffer *leaf;
1528 struct btrfs_dev_item *dev_item;
1529 struct btrfs_device *device;
1530 struct btrfs_key key;
1531 u8 fs_uuid[BTRFS_UUID_SIZE];
1532 u8 dev_uuid[BTRFS_UUID_SIZE];
1533 u64 devid;
1534 int ret;
1535
1536 path = btrfs_alloc_path();
1537 if (!path)
1538 return -ENOMEM;
1539
1540 root = root->fs_info->chunk_root;
1541 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1542 key.offset = 0;
1543 key.type = BTRFS_DEV_ITEM_KEY;
1544
1545 while (1) {
1546 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1547 if (ret < 0)
1548 goto error;
1549
1550 leaf = path->nodes[0];
1551next_slot:
1552 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1553 ret = btrfs_next_leaf(root, path);
1554 if (ret > 0)
1555 break;
1556 if (ret < 0)
1557 goto error;
1558 leaf = path->nodes[0];
1559 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001560 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001561 continue;
1562 }
1563
1564 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1565 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1566 key.type != BTRFS_DEV_ITEM_KEY)
1567 break;
1568
1569 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1570 struct btrfs_dev_item);
1571 devid = btrfs_device_id(leaf, dev_item);
1572 read_extent_buffer(leaf, dev_uuid,
1573 (unsigned long)btrfs_device_uuid(dev_item),
1574 BTRFS_UUID_SIZE);
1575 read_extent_buffer(leaf, fs_uuid,
1576 (unsigned long)btrfs_device_fsid(dev_item),
1577 BTRFS_UUID_SIZE);
1578 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1579 BUG_ON(!device);
1580
1581 if (device->fs_devices->seeding) {
1582 btrfs_set_device_generation(leaf, dev_item,
1583 device->generation);
1584 btrfs_mark_buffer_dirty(leaf);
1585 }
1586
1587 path->slots[0]++;
1588 goto next_slot;
1589 }
1590 ret = 0;
1591error:
1592 btrfs_free_path(path);
1593 return ret;
1594}
1595
Chris Mason788f20e2008-04-28 15:29:42 -04001596int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1597{
Josef Bacikd5e20032011-08-04 14:52:27 +00001598 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001599 struct btrfs_trans_handle *trans;
1600 struct btrfs_device *device;
1601 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001602 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001603 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001604 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001605 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001606 int ret = 0;
1607
Yan Zheng2b820322008-11-17 21:11:30 -05001608 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1609 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001610
Tejun Heod4d77622010-11-13 11:55:18 +01001611 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1612 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001613 if (IS_ERR(bdev))
1614 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001615
Yan Zheng2b820322008-11-17 21:11:30 -05001616 if (root->fs_info->fs_devices->seeding) {
1617 seeding_dev = 1;
1618 down_write(&sb->s_umount);
1619 mutex_lock(&uuid_mutex);
1620 }
1621
Chris Mason8c8bee12008-09-29 11:19:10 -04001622 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001623 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001624
Chris Mason788f20e2008-04-28 15:29:42 -04001625 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001626 /*
1627 * we have the volume lock, so we don't need the extra
1628 * device list mutex while reading the list here.
1629 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001630 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001631 if (device->bdev == bdev) {
1632 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001633 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001634 }
1635 }
1636
1637 device = kzalloc(sizeof(*device), GFP_NOFS);
1638 if (!device) {
1639 /* we can safely leave the fs_devices entry around */
1640 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001641 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001642 }
1643
Chris Mason788f20e2008-04-28 15:29:42 -04001644 device->name = kstrdup(device_path, GFP_NOFS);
1645 if (!device->name) {
1646 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001647 ret = -ENOMEM;
1648 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001649 }
Yan Zheng2b820322008-11-17 21:11:30 -05001650
1651 ret = find_next_devid(root, &device->devid);
1652 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001653 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001654 kfree(device);
1655 goto error;
1656 }
1657
Yan, Zhenga22285a2010-05-16 10:48:46 -04001658 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001659 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001660 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001661 kfree(device);
1662 ret = PTR_ERR(trans);
1663 goto error;
1664 }
1665
Yan Zheng2b820322008-11-17 21:11:30 -05001666 lock_chunks(root);
1667
Josef Bacikd5e20032011-08-04 14:52:27 +00001668 q = bdev_get_queue(bdev);
1669 if (blk_queue_discard(q))
1670 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001671 device->writeable = 1;
1672 device->work.func = pending_bios_fn;
1673 generate_random_uuid(device->uuid);
1674 spin_lock_init(&device->io_lock);
1675 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001676 device->io_width = root->sectorsize;
1677 device->io_align = root->sectorsize;
1678 device->sector_size = root->sectorsize;
1679 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001680 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001681 device->dev_root = root->fs_info->dev_root;
1682 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001683 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001684 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001685 set_blocksize(device->bdev, 4096);
1686
Yan Zheng2b820322008-11-17 21:11:30 -05001687 if (seeding_dev) {
1688 sb->s_flags &= ~MS_RDONLY;
1689 ret = btrfs_prepare_sprout(trans, root);
1690 BUG_ON(ret);
1691 }
1692
1693 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001694
1695 /*
1696 * we don't want write_supers to jump in here with our device
1697 * half setup
1698 */
1699 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001700 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001701 list_add(&device->dev_alloc_list,
1702 &root->fs_info->fs_devices->alloc_list);
1703 root->fs_info->fs_devices->num_devices++;
1704 root->fs_info->fs_devices->open_devices++;
1705 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001706 if (device->can_discard)
1707 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001708 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1709
Josef Bacik2bf64752011-09-26 17:12:22 -04001710 spin_lock(&root->fs_info->free_chunk_lock);
1711 root->fs_info->free_chunk_space += device->total_bytes;
1712 spin_unlock(&root->fs_info->free_chunk_lock);
1713
Chris Masonc2898112009-06-10 09:51:32 -04001714 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1715 root->fs_info->fs_devices->rotating = 1;
1716
David Sterba6c417612011-04-13 15:41:04 +02001717 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1718 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001719 total_bytes + device->total_bytes);
1720
David Sterba6c417612011-04-13 15:41:04 +02001721 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1722 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001723 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001724 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001725
Yan Zheng2b820322008-11-17 21:11:30 -05001726 if (seeding_dev) {
1727 ret = init_first_rw_device(trans, root, device);
1728 BUG_ON(ret);
1729 ret = btrfs_finish_sprout(trans, root);
1730 BUG_ON(ret);
1731 } else {
1732 ret = btrfs_add_device(trans, root, device);
1733 }
1734
Chris Mason913d9522009-03-10 13:17:18 -04001735 /*
1736 * we've got more storage, clear any full flags on the space
1737 * infos
1738 */
1739 btrfs_clear_space_info_full(root->fs_info);
1740
Chris Mason7d9eb122008-07-08 14:19:17 -04001741 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001742 btrfs_commit_transaction(trans, root);
1743
1744 if (seeding_dev) {
1745 mutex_unlock(&uuid_mutex);
1746 up_write(&sb->s_umount);
1747
1748 ret = btrfs_relocate_sys_chunks(root);
1749 BUG_ON(ret);
1750 }
1751out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001752 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001753 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001754error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001755 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001756 if (seeding_dev) {
1757 mutex_unlock(&uuid_mutex);
1758 up_write(&sb->s_umount);
1759 }
Chris Mason788f20e2008-04-28 15:29:42 -04001760 goto out;
1761}
1762
Chris Masond3977122009-01-05 21:25:51 -05001763static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1764 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001765{
1766 int ret;
1767 struct btrfs_path *path;
1768 struct btrfs_root *root;
1769 struct btrfs_dev_item *dev_item;
1770 struct extent_buffer *leaf;
1771 struct btrfs_key key;
1772
1773 root = device->dev_root->fs_info->chunk_root;
1774
1775 path = btrfs_alloc_path();
1776 if (!path)
1777 return -ENOMEM;
1778
1779 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1780 key.type = BTRFS_DEV_ITEM_KEY;
1781 key.offset = device->devid;
1782
1783 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1784 if (ret < 0)
1785 goto out;
1786
1787 if (ret > 0) {
1788 ret = -ENOENT;
1789 goto out;
1790 }
1791
1792 leaf = path->nodes[0];
1793 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1794
1795 btrfs_set_device_id(leaf, dev_item, device->devid);
1796 btrfs_set_device_type(leaf, dev_item, device->type);
1797 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1798 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1799 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001800 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001801 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1802 btrfs_mark_buffer_dirty(leaf);
1803
1804out:
1805 btrfs_free_path(path);
1806 return ret;
1807}
1808
Chris Mason7d9eb122008-07-08 14:19:17 -04001809static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001810 struct btrfs_device *device, u64 new_size)
1811{
1812 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001813 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001814 u64 old_total = btrfs_super_total_bytes(super_copy);
1815 u64 diff = new_size - device->total_bytes;
1816
Yan Zheng2b820322008-11-17 21:11:30 -05001817 if (!device->writeable)
1818 return -EACCES;
1819 if (new_size <= device->total_bytes)
1820 return -EINVAL;
1821
Chris Mason8f18cf12008-04-25 16:53:30 -04001822 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001823 device->fs_devices->total_rw_bytes += diff;
1824
1825 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001826 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001827 btrfs_clear_space_info_full(device->dev_root->fs_info);
1828
Chris Mason8f18cf12008-04-25 16:53:30 -04001829 return btrfs_update_device(trans, device);
1830}
1831
Chris Mason7d9eb122008-07-08 14:19:17 -04001832int btrfs_grow_device(struct btrfs_trans_handle *trans,
1833 struct btrfs_device *device, u64 new_size)
1834{
1835 int ret;
1836 lock_chunks(device->dev_root);
1837 ret = __btrfs_grow_device(trans, device, new_size);
1838 unlock_chunks(device->dev_root);
1839 return ret;
1840}
1841
Chris Mason8f18cf12008-04-25 16:53:30 -04001842static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1843 struct btrfs_root *root,
1844 u64 chunk_tree, u64 chunk_objectid,
1845 u64 chunk_offset)
1846{
1847 int ret;
1848 struct btrfs_path *path;
1849 struct btrfs_key key;
1850
1851 root = root->fs_info->chunk_root;
1852 path = btrfs_alloc_path();
1853 if (!path)
1854 return -ENOMEM;
1855
1856 key.objectid = chunk_objectid;
1857 key.offset = chunk_offset;
1858 key.type = BTRFS_CHUNK_ITEM_KEY;
1859
1860 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1861 BUG_ON(ret);
1862
1863 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001864
1865 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001866 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001867}
1868
Christoph Hellwigb2950862008-12-02 09:54:17 -05001869static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001870 chunk_offset)
1871{
David Sterba6c417612011-04-13 15:41:04 +02001872 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001873 struct btrfs_disk_key *disk_key;
1874 struct btrfs_chunk *chunk;
1875 u8 *ptr;
1876 int ret = 0;
1877 u32 num_stripes;
1878 u32 array_size;
1879 u32 len = 0;
1880 u32 cur;
1881 struct btrfs_key key;
1882
1883 array_size = btrfs_super_sys_array_size(super_copy);
1884
1885 ptr = super_copy->sys_chunk_array;
1886 cur = 0;
1887
1888 while (cur < array_size) {
1889 disk_key = (struct btrfs_disk_key *)ptr;
1890 btrfs_disk_key_to_cpu(&key, disk_key);
1891
1892 len = sizeof(*disk_key);
1893
1894 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1895 chunk = (struct btrfs_chunk *)(ptr + len);
1896 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1897 len += btrfs_chunk_item_size(num_stripes);
1898 } else {
1899 ret = -EIO;
1900 break;
1901 }
1902 if (key.objectid == chunk_objectid &&
1903 key.offset == chunk_offset) {
1904 memmove(ptr, ptr + len, array_size - (cur + len));
1905 array_size -= len;
1906 btrfs_set_super_sys_array_size(super_copy, array_size);
1907 } else {
1908 ptr += len;
1909 cur += len;
1910 }
1911 }
1912 return ret;
1913}
1914
Christoph Hellwigb2950862008-12-02 09:54:17 -05001915static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001916 u64 chunk_tree, u64 chunk_objectid,
1917 u64 chunk_offset)
1918{
1919 struct extent_map_tree *em_tree;
1920 struct btrfs_root *extent_root;
1921 struct btrfs_trans_handle *trans;
1922 struct extent_map *em;
1923 struct map_lookup *map;
1924 int ret;
1925 int i;
1926
1927 root = root->fs_info->chunk_root;
1928 extent_root = root->fs_info->extent_root;
1929 em_tree = &root->fs_info->mapping_tree.map_tree;
1930
Josef Bacikba1bf482009-09-11 16:11:19 -04001931 ret = btrfs_can_relocate(extent_root, chunk_offset);
1932 if (ret)
1933 return -ENOSPC;
1934
Chris Mason8f18cf12008-04-25 16:53:30 -04001935 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001936 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001937 if (ret)
1938 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001939
Yan, Zhenga22285a2010-05-16 10:48:46 -04001940 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001941 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001942
Chris Mason7d9eb122008-07-08 14:19:17 -04001943 lock_chunks(root);
1944
Chris Mason8f18cf12008-04-25 16:53:30 -04001945 /*
1946 * step two, delete the device extents and the
1947 * chunk tree entries
1948 */
Chris Mason890871b2009-09-02 16:24:52 -04001949 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001950 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001951 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001952
Chris Masona061fc82008-05-07 11:43:44 -04001953 BUG_ON(em->start > chunk_offset ||
1954 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001955 map = (struct map_lookup *)em->bdev;
1956
1957 for (i = 0; i < map->num_stripes; i++) {
1958 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1959 map->stripes[i].physical);
1960 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001961
Chris Masondfe25022008-05-13 13:46:40 -04001962 if (map->stripes[i].dev) {
1963 ret = btrfs_update_device(trans, map->stripes[i].dev);
1964 BUG_ON(ret);
1965 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001966 }
1967 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1968 chunk_offset);
1969
1970 BUG_ON(ret);
1971
liubo1abe9b82011-03-24 11:18:59 +00001972 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1973
Chris Mason8f18cf12008-04-25 16:53:30 -04001974 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1975 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1976 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001977 }
1978
Zheng Yan1a40e232008-09-26 10:09:34 -04001979 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1980 BUG_ON(ret);
1981
Chris Mason890871b2009-09-02 16:24:52 -04001982 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001983 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001984 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001985
Chris Mason8f18cf12008-04-25 16:53:30 -04001986 kfree(map);
1987 em->bdev = NULL;
1988
1989 /* once for the tree */
1990 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001991 /* once for us */
1992 free_extent_map(em);
1993
Chris Mason7d9eb122008-07-08 14:19:17 -04001994 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001995 btrfs_end_transaction(trans, root);
1996 return 0;
1997}
1998
Yan Zheng2b820322008-11-17 21:11:30 -05001999static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2000{
2001 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2002 struct btrfs_path *path;
2003 struct extent_buffer *leaf;
2004 struct btrfs_chunk *chunk;
2005 struct btrfs_key key;
2006 struct btrfs_key found_key;
2007 u64 chunk_tree = chunk_root->root_key.objectid;
2008 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002009 bool retried = false;
2010 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002011 int ret;
2012
2013 path = btrfs_alloc_path();
2014 if (!path)
2015 return -ENOMEM;
2016
Josef Bacikba1bf482009-09-11 16:11:19 -04002017again:
Yan Zheng2b820322008-11-17 21:11:30 -05002018 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2019 key.offset = (u64)-1;
2020 key.type = BTRFS_CHUNK_ITEM_KEY;
2021
2022 while (1) {
2023 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2024 if (ret < 0)
2025 goto error;
2026 BUG_ON(ret == 0);
2027
2028 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2029 key.type);
2030 if (ret < 0)
2031 goto error;
2032 if (ret > 0)
2033 break;
2034
2035 leaf = path->nodes[0];
2036 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2037
2038 chunk = btrfs_item_ptr(leaf, path->slots[0],
2039 struct btrfs_chunk);
2040 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002041 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002042
2043 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2044 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2045 found_key.objectid,
2046 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002047 if (ret == -ENOSPC)
2048 failed++;
2049 else if (ret)
2050 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002051 }
2052
2053 if (found_key.offset == 0)
2054 break;
2055 key.offset = found_key.offset - 1;
2056 }
2057 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002058 if (failed && !retried) {
2059 failed = 0;
2060 retried = true;
2061 goto again;
2062 } else if (failed && retried) {
2063 WARN_ON(1);
2064 ret = -ENOSPC;
2065 }
Yan Zheng2b820322008-11-17 21:11:30 -05002066error:
2067 btrfs_free_path(path);
2068 return ret;
2069}
2070
Chris Masonec44a352008-04-28 15:29:52 -04002071static u64 div_factor(u64 num, int factor)
2072{
2073 if (factor == 10)
2074 return num;
2075 num *= factor;
2076 do_div(num, 10);
2077 return num;
2078}
2079
Chris Masonec44a352008-04-28 15:29:52 -04002080int btrfs_balance(struct btrfs_root *dev_root)
2081{
2082 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002083 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2084 struct btrfs_device *device;
2085 u64 old_size;
2086 u64 size_to_free;
2087 struct btrfs_path *path;
2088 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002089 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2090 struct btrfs_trans_handle *trans;
2091 struct btrfs_key found_key;
2092
Yan Zheng2b820322008-11-17 21:11:30 -05002093 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2094 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002095
Ben Hutchings6f88a442010-12-29 14:55:03 +00002096 if (!capable(CAP_SYS_ADMIN))
2097 return -EPERM;
2098
Chris Mason7d9eb122008-07-08 14:19:17 -04002099 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002100 dev_root = dev_root->fs_info->dev_root;
2101
Chris Masonec44a352008-04-28 15:29:52 -04002102 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002103 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002104 old_size = device->total_bytes;
2105 size_to_free = div_factor(old_size, 1);
2106 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002107 if (!device->writeable ||
2108 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002109 continue;
2110
2111 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002112 if (ret == -ENOSPC)
2113 break;
Chris Masonec44a352008-04-28 15:29:52 -04002114 BUG_ON(ret);
2115
Yan, Zhenga22285a2010-05-16 10:48:46 -04002116 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002117 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002118
2119 ret = btrfs_grow_device(trans, device, old_size);
2120 BUG_ON(ret);
2121
2122 btrfs_end_transaction(trans, dev_root);
2123 }
2124
2125 /* step two, relocate all the chunks */
2126 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002127 if (!path) {
2128 ret = -ENOMEM;
2129 goto error;
2130 }
Chris Masonec44a352008-04-28 15:29:52 -04002131 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2132 key.offset = (u64)-1;
2133 key.type = BTRFS_CHUNK_ITEM_KEY;
2134
Chris Masond3977122009-01-05 21:25:51 -05002135 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002136 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2137 if (ret < 0)
2138 goto error;
2139
2140 /*
2141 * this shouldn't happen, it means the last relocate
2142 * failed
2143 */
2144 if (ret == 0)
2145 break;
2146
2147 ret = btrfs_previous_item(chunk_root, path, 0,
2148 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002149 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002150 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002151
Chris Masonec44a352008-04-28 15:29:52 -04002152 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2153 path->slots[0]);
2154 if (found_key.objectid != key.objectid)
2155 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002156
Chris Masonec44a352008-04-28 15:29:52 -04002157 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002158 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002159 break;
2160
David Sterbab3b4aa72011-04-21 01:20:15 +02002161 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002162 ret = btrfs_relocate_chunk(chunk_root,
2163 chunk_root->root_key.objectid,
2164 found_key.objectid,
2165 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002166 if (ret && ret != -ENOSPC)
2167 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002168 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002169 }
2170 ret = 0;
2171error:
2172 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002173 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002174 return ret;
2175}
2176
Chris Mason8f18cf12008-04-25 16:53:30 -04002177/*
2178 * shrinking a device means finding all of the device extents past
2179 * the new size, and then following the back refs to the chunks.
2180 * The chunk relocation code actually frees the device extent
2181 */
2182int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2183{
2184 struct btrfs_trans_handle *trans;
2185 struct btrfs_root *root = device->dev_root;
2186 struct btrfs_dev_extent *dev_extent = NULL;
2187 struct btrfs_path *path;
2188 u64 length;
2189 u64 chunk_tree;
2190 u64 chunk_objectid;
2191 u64 chunk_offset;
2192 int ret;
2193 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002194 int failed = 0;
2195 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002196 struct extent_buffer *l;
2197 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002198 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002199 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002200 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002201 u64 diff = device->total_bytes - new_size;
2202
Yan Zheng2b820322008-11-17 21:11:30 -05002203 if (new_size >= device->total_bytes)
2204 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002205
2206 path = btrfs_alloc_path();
2207 if (!path)
2208 return -ENOMEM;
2209
Chris Mason8f18cf12008-04-25 16:53:30 -04002210 path->reada = 2;
2211
Chris Mason7d9eb122008-07-08 14:19:17 -04002212 lock_chunks(root);
2213
Chris Mason8f18cf12008-04-25 16:53:30 -04002214 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002215 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002216 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002217 spin_lock(&root->fs_info->free_chunk_lock);
2218 root->fs_info->free_chunk_space -= diff;
2219 spin_unlock(&root->fs_info->free_chunk_lock);
2220 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002221 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002222
Josef Bacikba1bf482009-09-11 16:11:19 -04002223again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002224 key.objectid = device->devid;
2225 key.offset = (u64)-1;
2226 key.type = BTRFS_DEV_EXTENT_KEY;
2227
2228 while (1) {
2229 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2230 if (ret < 0)
2231 goto done;
2232
2233 ret = btrfs_previous_item(root, path, 0, key.type);
2234 if (ret < 0)
2235 goto done;
2236 if (ret) {
2237 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002238 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002239 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002240 }
2241
2242 l = path->nodes[0];
2243 slot = path->slots[0];
2244 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2245
Josef Bacikba1bf482009-09-11 16:11:19 -04002246 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002247 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002248 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002249 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002250
2251 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2252 length = btrfs_dev_extent_length(l, dev_extent);
2253
Josef Bacikba1bf482009-09-11 16:11:19 -04002254 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002255 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002256 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002257 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002258
2259 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2260 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2261 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002262 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002263
2264 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2265 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002266 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002267 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002268 if (ret == -ENOSPC)
2269 failed++;
2270 key.offset -= 1;
2271 }
2272
2273 if (failed && !retried) {
2274 failed = 0;
2275 retried = true;
2276 goto again;
2277 } else if (failed && retried) {
2278 ret = -ENOSPC;
2279 lock_chunks(root);
2280
2281 device->total_bytes = old_size;
2282 if (device->writeable)
2283 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002284 spin_lock(&root->fs_info->free_chunk_lock);
2285 root->fs_info->free_chunk_space += diff;
2286 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04002287 unlock_chunks(root);
2288 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002289 }
2290
Chris Balld6397ba2009-04-27 07:29:03 -04002291 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002292 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002293 if (IS_ERR(trans)) {
2294 ret = PTR_ERR(trans);
2295 goto done;
2296 }
2297
Chris Balld6397ba2009-04-27 07:29:03 -04002298 lock_chunks(root);
2299
2300 device->disk_total_bytes = new_size;
2301 /* Now btrfs_update_device() will change the on-disk size. */
2302 ret = btrfs_update_device(trans, device);
2303 if (ret) {
2304 unlock_chunks(root);
2305 btrfs_end_transaction(trans, root);
2306 goto done;
2307 }
2308 WARN_ON(diff > old_total);
2309 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2310 unlock_chunks(root);
2311 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002312done:
2313 btrfs_free_path(path);
2314 return ret;
2315}
2316
Christoph Hellwigb2950862008-12-02 09:54:17 -05002317static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002318 struct btrfs_root *root,
2319 struct btrfs_key *key,
2320 struct btrfs_chunk *chunk, int item_size)
2321{
David Sterba6c417612011-04-13 15:41:04 +02002322 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04002323 struct btrfs_disk_key disk_key;
2324 u32 array_size;
2325 u8 *ptr;
2326
2327 array_size = btrfs_super_sys_array_size(super_copy);
2328 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2329 return -EFBIG;
2330
2331 ptr = super_copy->sys_chunk_array + array_size;
2332 btrfs_cpu_key_to_disk(&disk_key, key);
2333 memcpy(ptr, &disk_key, sizeof(disk_key));
2334 ptr += sizeof(disk_key);
2335 memcpy(ptr, chunk, item_size);
2336 item_size += sizeof(disk_key);
2337 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2338 return 0;
2339}
2340
Miao Xieb2117a32011-01-05 10:07:28 +00002341/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002342 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002343 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002344static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002345{
Arne Jansen73c5de02011-04-12 12:07:57 +02002346 const struct btrfs_device_info *di_a = a;
2347 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002348
Arne Jansen73c5de02011-04-12 12:07:57 +02002349 if (di_a->max_avail > di_b->max_avail)
2350 return -1;
2351 if (di_a->max_avail < di_b->max_avail)
2352 return 1;
2353 if (di_a->total_avail > di_b->total_avail)
2354 return -1;
2355 if (di_a->total_avail < di_b->total_avail)
2356 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002357 return 0;
2358}
2359
2360static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2361 struct btrfs_root *extent_root,
2362 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002363 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002364 u64 start, u64 type)
2365{
2366 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002367 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2368 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002369 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002370 struct extent_map_tree *em_tree;
2371 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002372 struct btrfs_device_info *devices_info = NULL;
2373 u64 total_avail;
2374 int num_stripes; /* total number of stripes to allocate */
2375 int sub_stripes; /* sub_stripes info for map */
2376 int dev_stripes; /* stripes per dev */
2377 int devs_max; /* max devs to use */
2378 int devs_min; /* min devs needed */
2379 int devs_increment; /* ndevs has to be a multiple of this */
2380 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002381 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002382 u64 max_stripe_size;
2383 u64 max_chunk_size;
2384 u64 stripe_size;
2385 u64 num_bytes;
2386 int ndevs;
2387 int i;
2388 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002389
2390 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2391 (type & BTRFS_BLOCK_GROUP_DUP)) {
2392 WARN_ON(1);
2393 type &= ~BTRFS_BLOCK_GROUP_DUP;
2394 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002395
Miao Xieb2117a32011-01-05 10:07:28 +00002396 if (list_empty(&fs_devices->alloc_list))
2397 return -ENOSPC;
2398
Arne Jansen73c5de02011-04-12 12:07:57 +02002399 sub_stripes = 1;
2400 dev_stripes = 1;
2401 devs_increment = 1;
2402 ncopies = 1;
2403 devs_max = 0; /* 0 == as many as possible */
2404 devs_min = 1;
2405
2406 /*
2407 * define the properties of each RAID type.
2408 * FIXME: move this to a global table and use it in all RAID
2409 * calculation code
2410 */
2411 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2412 dev_stripes = 2;
2413 ncopies = 2;
2414 devs_max = 1;
2415 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2416 devs_min = 2;
2417 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2418 devs_increment = 2;
2419 ncopies = 2;
2420 devs_max = 2;
2421 devs_min = 2;
2422 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2423 sub_stripes = 2;
2424 devs_increment = 2;
2425 ncopies = 2;
2426 devs_min = 4;
2427 } else {
2428 devs_max = 1;
2429 }
2430
2431 if (type & BTRFS_BLOCK_GROUP_DATA) {
2432 max_stripe_size = 1024 * 1024 * 1024;
2433 max_chunk_size = 10 * max_stripe_size;
2434 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2435 max_stripe_size = 256 * 1024 * 1024;
2436 max_chunk_size = max_stripe_size;
2437 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2438 max_stripe_size = 8 * 1024 * 1024;
2439 max_chunk_size = 2 * max_stripe_size;
2440 } else {
2441 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2442 type);
2443 BUG_ON(1);
2444 }
2445
2446 /* we don't want a chunk larger than 10% of writeable space */
2447 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2448 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002449
2450 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2451 GFP_NOFS);
2452 if (!devices_info)
2453 return -ENOMEM;
2454
Arne Jansen73c5de02011-04-12 12:07:57 +02002455 cur = fs_devices->alloc_list.next;
2456
2457 /*
2458 * in the first pass through the devices list, we gather information
2459 * about the available holes on each device.
2460 */
2461 ndevs = 0;
2462 while (cur != &fs_devices->alloc_list) {
2463 struct btrfs_device *device;
2464 u64 max_avail;
2465 u64 dev_offset;
2466
2467 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2468
2469 cur = cur->next;
2470
2471 if (!device->writeable) {
2472 printk(KERN_ERR
2473 "btrfs: read-only device in alloc_list\n");
2474 WARN_ON(1);
2475 continue;
2476 }
2477
2478 if (!device->in_fs_metadata)
2479 continue;
2480
2481 if (device->total_bytes > device->bytes_used)
2482 total_avail = device->total_bytes - device->bytes_used;
2483 else
2484 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00002485
2486 /* If there is no space on this device, skip it. */
2487 if (total_avail == 0)
2488 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02002489
2490 ret = find_free_dev_extent(trans, device,
2491 max_stripe_size * dev_stripes,
2492 &dev_offset, &max_avail);
2493 if (ret && ret != -ENOSPC)
2494 goto error;
2495
2496 if (ret == 0)
2497 max_avail = max_stripe_size * dev_stripes;
2498
2499 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2500 continue;
2501
2502 devices_info[ndevs].dev_offset = dev_offset;
2503 devices_info[ndevs].max_avail = max_avail;
2504 devices_info[ndevs].total_avail = total_avail;
2505 devices_info[ndevs].dev = device;
2506 ++ndevs;
2507 }
2508
2509 /*
2510 * now sort the devices by hole size / available space
2511 */
2512 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2513 btrfs_cmp_device_info, NULL);
2514
2515 /* round down to number of usable stripes */
2516 ndevs -= ndevs % devs_increment;
2517
2518 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2519 ret = -ENOSPC;
2520 goto error;
2521 }
2522
2523 if (devs_max && ndevs > devs_max)
2524 ndevs = devs_max;
2525 /*
2526 * the primary goal is to maximize the number of stripes, so use as many
2527 * devices as possible, even if the stripes are not maximum sized.
2528 */
2529 stripe_size = devices_info[ndevs-1].max_avail;
2530 num_stripes = ndevs * dev_stripes;
2531
2532 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2533 stripe_size = max_chunk_size * ncopies;
2534 do_div(stripe_size, num_stripes);
2535 }
2536
2537 do_div(stripe_size, dev_stripes);
2538 do_div(stripe_size, BTRFS_STRIPE_LEN);
2539 stripe_size *= BTRFS_STRIPE_LEN;
2540
Miao Xieb2117a32011-01-05 10:07:28 +00002541 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2542 if (!map) {
2543 ret = -ENOMEM;
2544 goto error;
2545 }
2546 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002547
Arne Jansen73c5de02011-04-12 12:07:57 +02002548 for (i = 0; i < ndevs; ++i) {
2549 for (j = 0; j < dev_stripes; ++j) {
2550 int s = i * dev_stripes + j;
2551 map->stripes[s].dev = devices_info[i].dev;
2552 map->stripes[s].physical = devices_info[i].dev_offset +
2553 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002554 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002555 }
Chris Mason593060d2008-03-25 16:50:33 -04002556 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002557 map->stripe_len = BTRFS_STRIPE_LEN;
2558 map->io_align = BTRFS_STRIPE_LEN;
2559 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002560 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002561 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002562
Yan Zheng2b820322008-11-17 21:11:30 -05002563 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002564 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002565
Arne Jansen73c5de02011-04-12 12:07:57 +02002566 *stripe_size_out = stripe_size;
2567 *num_bytes_out = num_bytes;
2568
2569 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002570
David Sterba172ddd62011-04-21 00:48:27 +02002571 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002572 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002573 ret = -ENOMEM;
2574 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002575 }
Chris Mason0b86a832008-03-24 15:01:56 -04002576 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002577 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002578 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002579 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002580 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002581
Chris Mason0b86a832008-03-24 15:01:56 -04002582 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002583 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002584 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002585 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002586 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002587 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002588
2589 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2590 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002591 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002592 BUG_ON(ret);
2593
Arne Jansen73c5de02011-04-12 12:07:57 +02002594 for (i = 0; i < map->num_stripes; ++i) {
2595 struct btrfs_device *device;
2596 u64 dev_offset;
2597
2598 device = map->stripes[i].dev;
2599 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002600
2601 ret = btrfs_alloc_dev_extent(trans, device,
2602 info->chunk_root->root_key.objectid,
2603 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002604 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002605 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002606 }
2607
Miao Xieb2117a32011-01-05 10:07:28 +00002608 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002609 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002610
2611error:
2612 kfree(map);
2613 kfree(devices_info);
2614 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002615}
2616
2617static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2618 struct btrfs_root *extent_root,
2619 struct map_lookup *map, u64 chunk_offset,
2620 u64 chunk_size, u64 stripe_size)
2621{
2622 u64 dev_offset;
2623 struct btrfs_key key;
2624 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2625 struct btrfs_device *device;
2626 struct btrfs_chunk *chunk;
2627 struct btrfs_stripe *stripe;
2628 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2629 int index = 0;
2630 int ret;
2631
2632 chunk = kzalloc(item_size, GFP_NOFS);
2633 if (!chunk)
2634 return -ENOMEM;
2635
2636 index = 0;
2637 while (index < map->num_stripes) {
2638 device = map->stripes[index].dev;
2639 device->bytes_used += stripe_size;
2640 ret = btrfs_update_device(trans, device);
2641 BUG_ON(ret);
2642 index++;
2643 }
2644
Josef Bacik2bf64752011-09-26 17:12:22 -04002645 spin_lock(&extent_root->fs_info->free_chunk_lock);
2646 extent_root->fs_info->free_chunk_space -= (stripe_size *
2647 map->num_stripes);
2648 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2649
Yan Zheng2b820322008-11-17 21:11:30 -05002650 index = 0;
2651 stripe = &chunk->stripe;
2652 while (index < map->num_stripes) {
2653 device = map->stripes[index].dev;
2654 dev_offset = map->stripes[index].physical;
2655
2656 btrfs_set_stack_stripe_devid(stripe, device->devid);
2657 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2658 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2659 stripe++;
2660 index++;
2661 }
2662
2663 btrfs_set_stack_chunk_length(chunk, chunk_size);
2664 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2665 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2666 btrfs_set_stack_chunk_type(chunk, map->type);
2667 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2668 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2669 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2670 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2671 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2672
2673 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2674 key.type = BTRFS_CHUNK_ITEM_KEY;
2675 key.offset = chunk_offset;
2676
2677 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2678 BUG_ON(ret);
2679
2680 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2681 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2682 item_size);
2683 BUG_ON(ret);
2684 }
liubo1abe9b82011-03-24 11:18:59 +00002685
Yan Zheng2b820322008-11-17 21:11:30 -05002686 kfree(chunk);
2687 return 0;
2688}
2689
2690/*
2691 * Chunk allocation falls into two parts. The first part does works
2692 * that make the new allocated chunk useable, but not do any operation
2693 * that modifies the chunk tree. The second part does the works that
2694 * require modifying the chunk tree. This division is important for the
2695 * bootstrap process of adding storage to a seed btrfs.
2696 */
2697int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2698 struct btrfs_root *extent_root, u64 type)
2699{
2700 u64 chunk_offset;
2701 u64 chunk_size;
2702 u64 stripe_size;
2703 struct map_lookup *map;
2704 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2705 int ret;
2706
2707 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2708 &chunk_offset);
2709 if (ret)
2710 return ret;
2711
2712 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2713 &stripe_size, chunk_offset, type);
2714 if (ret)
2715 return ret;
2716
2717 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2718 chunk_size, stripe_size);
2719 BUG_ON(ret);
2720 return 0;
2721}
2722
Chris Masond3977122009-01-05 21:25:51 -05002723static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002724 struct btrfs_root *root,
2725 struct btrfs_device *device)
2726{
2727 u64 chunk_offset;
2728 u64 sys_chunk_offset;
2729 u64 chunk_size;
2730 u64 sys_chunk_size;
2731 u64 stripe_size;
2732 u64 sys_stripe_size;
2733 u64 alloc_profile;
2734 struct map_lookup *map;
2735 struct map_lookup *sys_map;
2736 struct btrfs_fs_info *fs_info = root->fs_info;
2737 struct btrfs_root *extent_root = fs_info->extent_root;
2738 int ret;
2739
2740 ret = find_next_chunk(fs_info->chunk_root,
2741 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07002742 if (ret)
2743 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002744
2745 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2746 (fs_info->metadata_alloc_profile &
2747 fs_info->avail_metadata_alloc_bits);
2748 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2749
2750 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2751 &stripe_size, chunk_offset, alloc_profile);
2752 BUG_ON(ret);
2753
2754 sys_chunk_offset = chunk_offset + chunk_size;
2755
2756 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2757 (fs_info->system_alloc_profile &
2758 fs_info->avail_system_alloc_bits);
2759 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2760
2761 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2762 &sys_chunk_size, &sys_stripe_size,
2763 sys_chunk_offset, alloc_profile);
2764 BUG_ON(ret);
2765
2766 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2767 BUG_ON(ret);
2768
2769 /*
2770 * Modifying chunk tree needs allocating new blocks from both
2771 * system block group and metadata block group. So we only can
2772 * do operations require modifying the chunk tree after both
2773 * block groups were created.
2774 */
2775 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2776 chunk_size, stripe_size);
2777 BUG_ON(ret);
2778
2779 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2780 sys_chunk_offset, sys_chunk_size,
2781 sys_stripe_size);
2782 BUG_ON(ret);
2783 return 0;
2784}
2785
2786int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2787{
2788 struct extent_map *em;
2789 struct map_lookup *map;
2790 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2791 int readonly = 0;
2792 int i;
2793
Chris Mason890871b2009-09-02 16:24:52 -04002794 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002795 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002796 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002797 if (!em)
2798 return 1;
2799
Josef Bacikf48b9072010-01-27 02:07:59 +00002800 if (btrfs_test_opt(root, DEGRADED)) {
2801 free_extent_map(em);
2802 return 0;
2803 }
2804
Yan Zheng2b820322008-11-17 21:11:30 -05002805 map = (struct map_lookup *)em->bdev;
2806 for (i = 0; i < map->num_stripes; i++) {
2807 if (!map->stripes[i].dev->writeable) {
2808 readonly = 1;
2809 break;
2810 }
2811 }
2812 free_extent_map(em);
2813 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002814}
2815
2816void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2817{
David Sterbaa8067e02011-04-21 00:34:43 +02002818 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002819}
2820
2821void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2822{
2823 struct extent_map *em;
2824
Chris Masond3977122009-01-05 21:25:51 -05002825 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002826 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002827 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2828 if (em)
2829 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002830 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002831 if (!em)
2832 break;
2833 kfree(em->bdev);
2834 /* once for us */
2835 free_extent_map(em);
2836 /* once for the tree */
2837 free_extent_map(em);
2838 }
2839}
2840
Chris Masonf1885912008-04-09 16:28:12 -04002841int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2842{
2843 struct extent_map *em;
2844 struct map_lookup *map;
2845 struct extent_map_tree *em_tree = &map_tree->map_tree;
2846 int ret;
2847
Chris Mason890871b2009-09-02 16:24:52 -04002848 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002849 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002850 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002851 BUG_ON(!em);
2852
2853 BUG_ON(em->start > logical || em->start + em->len < logical);
2854 map = (struct map_lookup *)em->bdev;
2855 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2856 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002857 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2858 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002859 else
2860 ret = 1;
2861 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002862 return ret;
2863}
2864
Chris Masondfe25022008-05-13 13:46:40 -04002865static int find_live_mirror(struct map_lookup *map, int first, int num,
2866 int optimal)
2867{
2868 int i;
2869 if (map->stripes[optimal].dev->bdev)
2870 return optimal;
2871 for (i = first; i < first + num; i++) {
2872 if (map->stripes[i].dev->bdev)
2873 return i;
2874 }
2875 /* we couldn't find one that doesn't fail. Just return something
2876 * and the io error handling code will clean up eventually
2877 */
2878 return optimal;
2879}
2880
Chris Masonf2d8d742008-04-21 10:03:05 -04002881static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2882 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02002883 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002884 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002885{
2886 struct extent_map *em;
2887 struct map_lookup *map;
2888 struct extent_map_tree *em_tree = &map_tree->map_tree;
2889 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002890 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002891 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002892 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002893 u64 stripe_nr_orig;
2894 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002895 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002896 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002897 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002898 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002899 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002900 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002901 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002902
Jan Schmidta1d3c472011-08-04 17:15:33 +02002903 if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002904 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002905again:
Jan Schmidta1d3c472011-08-04 17:15:33 +02002906 if (bbio_ret) {
2907 bbio = kzalloc(btrfs_bio_size(stripes_allocated),
Chris Masoncea9e442008-04-09 16:28:12 -04002908 GFP_NOFS);
Jan Schmidta1d3c472011-08-04 17:15:33 +02002909 if (!bbio)
Chris Masoncea9e442008-04-09 16:28:12 -04002910 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002911
Jan Schmidta1d3c472011-08-04 17:15:33 +02002912 atomic_set(&bbio->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002913 }
Chris Mason0b86a832008-03-24 15:01:56 -04002914
Chris Mason890871b2009-09-02 16:24:52 -04002915 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002916 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002917 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002918
Chris Mason3b951512008-04-17 11:29:12 -04002919 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002920 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2921 (unsigned long long)logical,
2922 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002923 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002924 }
Chris Mason0b86a832008-03-24 15:01:56 -04002925
2926 BUG_ON(em->start > logical || em->start + em->len < logical);
2927 map = (struct map_lookup *)em->bdev;
2928 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002929
Chris Masonf1885912008-04-09 16:28:12 -04002930 if (mirror_num > map->num_stripes)
2931 mirror_num = 0;
2932
Jan Schmidta1d3c472011-08-04 17:15:33 +02002933 /* if our btrfs_bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002934 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002935 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2936 BTRFS_BLOCK_GROUP_DUP)) {
2937 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002938 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002939 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2940 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002941 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002942 }
2943 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002944 if (rw & REQ_DISCARD) {
2945 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2946 BTRFS_BLOCK_GROUP_RAID1 |
2947 BTRFS_BLOCK_GROUP_DUP |
2948 BTRFS_BLOCK_GROUP_RAID10)) {
2949 stripes_required = map->num_stripes;
2950 }
2951 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02002952 if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002953 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002954 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002955 free_extent_map(em);
Jan Schmidta1d3c472011-08-04 17:15:33 +02002956 kfree(bbio);
Chris Masoncea9e442008-04-09 16:28:12 -04002957 goto again;
2958 }
Chris Mason593060d2008-03-25 16:50:33 -04002959 stripe_nr = offset;
2960 /*
2961 * stripe_nr counts the total number of stripes we have to stride
2962 * to get to this block
2963 */
2964 do_div(stripe_nr, map->stripe_len);
2965
2966 stripe_offset = stripe_nr * map->stripe_len;
2967 BUG_ON(offset < stripe_offset);
2968
2969 /* stripe_offset is the offset of this block in its stripe*/
2970 stripe_offset = offset - stripe_offset;
2971
Li Dongyangfce3bb92011-03-24 10:24:26 +00002972 if (rw & REQ_DISCARD)
2973 *length = min_t(u64, em->len - offset, *length);
2974 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2975 BTRFS_BLOCK_GROUP_RAID1 |
2976 BTRFS_BLOCK_GROUP_RAID10 |
2977 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002978 /* we limit the length of each bio to what fits in a stripe */
2979 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002980 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002981 } else {
2982 *length = em->len - offset;
2983 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002984
Jan Schmidta1d3c472011-08-04 17:15:33 +02002985 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002986 goto out;
2987
Chris Masonf2d8d742008-04-21 10:03:05 -04002988 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002989 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002990 stripe_nr_orig = stripe_nr;
2991 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2992 (~(map->stripe_len - 1));
2993 do_div(stripe_nr_end, map->stripe_len);
2994 stripe_end_offset = stripe_nr_end * map->stripe_len -
2995 (offset + *length);
2996 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2997 if (rw & REQ_DISCARD)
2998 num_stripes = min_t(u64, map->num_stripes,
2999 stripe_nr_end - stripe_nr_orig);
3000 stripe_index = do_div(stripe_nr, map->num_stripes);
3001 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003002 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003003 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003004 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003005 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003006 else {
3007 stripe_index = find_live_mirror(map, 0,
3008 map->num_stripes,
3009 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003010 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003011 }
Chris Mason2fff7342008-04-29 14:12:09 -04003012
Chris Mason611f0e02008-04-03 16:29:03 -04003013 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003014 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003015 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003016 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003017 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003018 } else {
3019 mirror_num = 1;
3020 }
Chris Mason2fff7342008-04-29 14:12:09 -04003021
Chris Mason321aecc2008-04-16 10:49:51 -04003022 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3023 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003024
3025 stripe_index = do_div(stripe_nr, factor);
3026 stripe_index *= map->sub_stripes;
3027
Jens Axboe7eaceac2011-03-10 08:52:07 +01003028 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003029 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003030 else if (rw & REQ_DISCARD)
3031 num_stripes = min_t(u64, map->sub_stripes *
3032 (stripe_nr_end - stripe_nr_orig),
3033 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003034 else if (mirror_num)
3035 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003036 else {
3037 stripe_index = find_live_mirror(map, stripe_index,
3038 map->sub_stripes, stripe_index +
3039 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003040 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003041 }
Chris Mason8790d502008-04-03 16:29:03 -04003042 } else {
3043 /*
3044 * after this do_div call, stripe_nr is the number of stripes
3045 * on this device we have to walk to find the data, and
3046 * stripe_index is the number of our device in the stripe array
3047 */
3048 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003049 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003050 }
Chris Mason593060d2008-03-25 16:50:33 -04003051 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003052
Li Dongyangfce3bb92011-03-24 10:24:26 +00003053 if (rw & REQ_DISCARD) {
3054 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003055 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003056 map->stripes[stripe_index].physical +
3057 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003058 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003059
3060 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3061 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003062 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003063 int j;
3064
Chris Masond9d04872011-03-27 21:23:21 -04003065 div_u64_rem(stripe_nr_end - 1,
3066 map->num_stripes,
3067 &last_stripe);
3068
Li Dongyangfce3bb92011-03-24 10:24:26 +00003069 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003070 u32 test;
3071
3072 div_u64_rem(stripe_nr_end - 1 - j,
3073 map->num_stripes, &test);
3074 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003075 break;
3076 }
3077 stripes = stripe_nr_end - 1 - j;
3078 do_div(stripes, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003079 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003080 (stripes - stripe_nr + 1);
3081
3082 if (i == 0) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003083 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003084 stripe_offset;
3085 stripe_offset = 0;
3086 }
3087 if (stripe_index == last_stripe)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003088 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003089 stripe_end_offset;
3090 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3091 u64 stripes;
3092 int j;
3093 int factor = map->num_stripes /
3094 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003095 u32 last_stripe = 0;
3096
3097 div_u64_rem(stripe_nr_end - 1,
3098 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003099 last_stripe *= map->sub_stripes;
3100
3101 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003102 u32 test;
3103
3104 div_u64_rem(stripe_nr_end - 1 - j,
3105 factor, &test);
3106
3107 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003108 stripe_index / map->sub_stripes)
3109 break;
3110 }
3111 stripes = stripe_nr_end - 1 - j;
3112 do_div(stripes, factor);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003113 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003114 (stripes - stripe_nr + 1);
3115
3116 if (i < map->sub_stripes) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003117 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003118 stripe_offset;
3119 if (i == map->sub_stripes - 1)
3120 stripe_offset = 0;
3121 }
3122 if (stripe_index >= last_stripe &&
3123 stripe_index <= (last_stripe +
3124 map->sub_stripes - 1)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003125 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003126 stripe_end_offset;
3127 }
3128 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003129 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003130
3131 stripe_index++;
3132 if (stripe_index == map->num_stripes) {
3133 /* This could only happen for RAID0/10 */
3134 stripe_index = 0;
3135 stripe_nr++;
3136 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003137 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003138 } else {
3139 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003140 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003141 map->stripes[stripe_index].physical +
3142 stripe_offset +
3143 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003144 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003145 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003146 stripe_index++;
3147 }
Chris Mason593060d2008-03-25 16:50:33 -04003148 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003149 if (bbio_ret) {
3150 *bbio_ret = bbio;
3151 bbio->num_stripes = num_stripes;
3152 bbio->max_errors = max_errors;
3153 bbio->mirror_num = mirror_num;
Chris Masonf2d8d742008-04-21 10:03:05 -04003154 }
Chris Masoncea9e442008-04-09 16:28:12 -04003155out:
Chris Mason0b86a832008-03-24 15:01:56 -04003156 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003157 return 0;
3158}
3159
Chris Masonf2d8d742008-04-21 10:03:05 -04003160int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3161 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003162 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003163{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003164 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003165 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003166}
3167
Yan Zhenga512bbf2008-12-08 16:46:26 -05003168int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3169 u64 chunk_start, u64 physical, u64 devid,
3170 u64 **logical, int *naddrs, int *stripe_len)
3171{
3172 struct extent_map_tree *em_tree = &map_tree->map_tree;
3173 struct extent_map *em;
3174 struct map_lookup *map;
3175 u64 *buf;
3176 u64 bytenr;
3177 u64 length;
3178 u64 stripe_nr;
3179 int i, j, nr = 0;
3180
Chris Mason890871b2009-09-02 16:24:52 -04003181 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003182 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003183 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003184
3185 BUG_ON(!em || em->start != chunk_start);
3186 map = (struct map_lookup *)em->bdev;
3187
3188 length = em->len;
3189 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3190 do_div(length, map->num_stripes / map->sub_stripes);
3191 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3192 do_div(length, map->num_stripes);
3193
3194 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3195 BUG_ON(!buf);
3196
3197 for (i = 0; i < map->num_stripes; i++) {
3198 if (devid && map->stripes[i].dev->devid != devid)
3199 continue;
3200 if (map->stripes[i].physical > physical ||
3201 map->stripes[i].physical + length <= physical)
3202 continue;
3203
3204 stripe_nr = physical - map->stripes[i].physical;
3205 do_div(stripe_nr, map->stripe_len);
3206
3207 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3208 stripe_nr = stripe_nr * map->num_stripes + i;
3209 do_div(stripe_nr, map->sub_stripes);
3210 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3211 stripe_nr = stripe_nr * map->num_stripes + i;
3212 }
3213 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003214 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003215 for (j = 0; j < nr; j++) {
3216 if (buf[j] == bytenr)
3217 break;
3218 }
Chris Mason934d3752008-12-08 16:43:10 -05003219 if (j == nr) {
3220 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003221 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003222 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003223 }
3224
Yan Zhenga512bbf2008-12-08 16:46:26 -05003225 *logical = buf;
3226 *naddrs = nr;
3227 *stripe_len = map->stripe_len;
3228
3229 free_extent_map(em);
3230 return 0;
3231}
3232
Jan Schmidta1d3c472011-08-04 17:15:33 +02003233static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003234{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003235 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003236 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003237
Chris Mason8790d502008-04-03 16:29:03 -04003238 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003239 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003240
Jan Schmidta1d3c472011-08-04 17:15:33 +02003241 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003242 is_orig_bio = 1;
3243
Jan Schmidta1d3c472011-08-04 17:15:33 +02003244 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003245 if (!is_orig_bio) {
3246 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003247 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003248 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003249 bio->bi_private = bbio->private;
3250 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003251 bio->bi_bdev = (struct block_device *)
3252 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003253 /* only send an error to the higher layers if it is
3254 * beyond the tolerance of the multi-bio
3255 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003256 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003257 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003258 } else if (err) {
3259 /*
3260 * this bio is actually up to date, we didn't
3261 * go over the max number of errors
3262 */
3263 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003264 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003265 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003266 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003267
3268 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003269 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003270 bio_put(bio);
3271 }
Chris Mason8790d502008-04-03 16:29:03 -04003272}
3273
Chris Mason8b712842008-06-11 16:50:36 -04003274struct async_sched {
3275 struct bio *bio;
3276 int rw;
3277 struct btrfs_fs_info *info;
3278 struct btrfs_work work;
3279};
3280
3281/*
3282 * see run_scheduled_bios for a description of why bios are collected for
3283 * async submit.
3284 *
3285 * This will add one bio to the pending list for a device and make sure
3286 * the work struct is scheduled.
3287 */
Chris Masond3977122009-01-05 21:25:51 -05003288static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003289 struct btrfs_device *device,
3290 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003291{
3292 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003293 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003294
3295 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003296 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003297 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003298 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003299 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003300 return 0;
3301 }
3302
3303 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003304 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003305 * higher layers. Otherwise, the async bio makes it appear we have
3306 * made progress against dirty pages when we've really just put it
3307 * on a queue for later
3308 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003309 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003310 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003311 bio->bi_next = NULL;
3312 bio->bi_rw |= rw;
3313
3314 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003315 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003316 pending_bios = &device->pending_sync_bios;
3317 else
3318 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003319
Chris Masonffbd5172009-04-20 15:50:09 -04003320 if (pending_bios->tail)
3321 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003322
Chris Masonffbd5172009-04-20 15:50:09 -04003323 pending_bios->tail = bio;
3324 if (!pending_bios->head)
3325 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003326 if (device->running_pending)
3327 should_queue = 0;
3328
3329 spin_unlock(&device->io_lock);
3330
3331 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003332 btrfs_queue_worker(&root->fs_info->submit_workers,
3333 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003334 return 0;
3335}
3336
Chris Masonf1885912008-04-09 16:28:12 -04003337int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003338 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003339{
3340 struct btrfs_mapping_tree *map_tree;
3341 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003342 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003343 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003344 u64 length = 0;
3345 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04003346 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003347 int dev_nr = 0;
3348 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003349 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003350
Chris Masonf2d8d742008-04-21 10:03:05 -04003351 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003352 map_tree = &root->fs_info->mapping_tree;
3353 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003354
Jan Schmidta1d3c472011-08-04 17:15:33 +02003355 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04003356 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003357 BUG_ON(ret);
3358
Jan Schmidta1d3c472011-08-04 17:15:33 +02003359 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003360 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003361 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3362 "len %llu\n", (unsigned long long)logical,
3363 (unsigned long long)length,
3364 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003365 BUG();
3366 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003367
3368 bbio->orig_bio = first_bio;
3369 bbio->private = first_bio->bi_private;
3370 bbio->end_io = first_bio->bi_end_io;
3371 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04003372
Chris Masond3977122009-01-05 21:25:51 -05003373 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003374 if (dev_nr < total_devs - 1) {
3375 bio = bio_clone(first_bio, GFP_NOFS);
3376 BUG_ON(!bio);
3377 } else {
3378 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04003379 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003380 bio->bi_private = bbio;
3381 bio->bi_end_io = btrfs_end_bio;
3382 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
3383 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003384 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003385 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3386 "(%s id %llu), size=%u\n", rw,
3387 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
3388 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04003389 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003390 if (async_submit)
3391 schedule_bio(root, dev, rw, bio);
3392 else
3393 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003394 } else {
3395 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3396 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003397 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003398 }
Chris Mason8790d502008-04-03 16:29:03 -04003399 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003400 }
Chris Mason0b86a832008-03-24 15:01:56 -04003401 return 0;
3402}
3403
Chris Masona4437552008-04-18 10:29:38 -04003404struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003405 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003406{
Yan Zheng2b820322008-11-17 21:11:30 -05003407 struct btrfs_device *device;
3408 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003409
Yan Zheng2b820322008-11-17 21:11:30 -05003410 cur_devices = root->fs_info->fs_devices;
3411 while (cur_devices) {
3412 if (!fsid ||
3413 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3414 device = __find_device(&cur_devices->devices,
3415 devid, uuid);
3416 if (device)
3417 return device;
3418 }
3419 cur_devices = cur_devices->seed;
3420 }
3421 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003422}
3423
Chris Masondfe25022008-05-13 13:46:40 -04003424static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3425 u64 devid, u8 *dev_uuid)
3426{
3427 struct btrfs_device *device;
3428 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3429
3430 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003431 if (!device)
3432 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003433 list_add(&device->dev_list,
3434 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003435 device->dev_root = root->fs_info->dev_root;
3436 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003437 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003438 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003439 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003440 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003441 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003442 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003443 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003444 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3445 return device;
3446}
3447
Chris Mason0b86a832008-03-24 15:01:56 -04003448static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3449 struct extent_buffer *leaf,
3450 struct btrfs_chunk *chunk)
3451{
3452 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3453 struct map_lookup *map;
3454 struct extent_map *em;
3455 u64 logical;
3456 u64 length;
3457 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003458 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003459 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003460 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003461 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003462
Chris Masone17cade2008-04-15 15:41:47 -04003463 logical = key->offset;
3464 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003465
Chris Mason890871b2009-09-02 16:24:52 -04003466 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003467 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003468 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003469
3470 /* already mapped? */
3471 if (em && em->start <= logical && em->start + em->len > logical) {
3472 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003473 return 0;
3474 } else if (em) {
3475 free_extent_map(em);
3476 }
Chris Mason0b86a832008-03-24 15:01:56 -04003477
David Sterba172ddd62011-04-21 00:48:27 +02003478 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003479 if (!em)
3480 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003481 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3482 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003483 if (!map) {
3484 free_extent_map(em);
3485 return -ENOMEM;
3486 }
3487
3488 em->bdev = (struct block_device *)map;
3489 em->start = logical;
3490 em->len = length;
3491 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003492 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003493
Chris Mason593060d2008-03-25 16:50:33 -04003494 map->num_stripes = num_stripes;
3495 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3496 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3497 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3498 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3499 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003500 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003501 for (i = 0; i < num_stripes; i++) {
3502 map->stripes[i].physical =
3503 btrfs_stripe_offset_nr(leaf, chunk, i);
3504 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003505 read_extent_buffer(leaf, uuid, (unsigned long)
3506 btrfs_stripe_dev_uuid_nr(chunk, i),
3507 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003508 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3509 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003510 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003511 kfree(map);
3512 free_extent_map(em);
3513 return -EIO;
3514 }
Chris Masondfe25022008-05-13 13:46:40 -04003515 if (!map->stripes[i].dev) {
3516 map->stripes[i].dev =
3517 add_missing_dev(root, devid, uuid);
3518 if (!map->stripes[i].dev) {
3519 kfree(map);
3520 free_extent_map(em);
3521 return -EIO;
3522 }
3523 }
3524 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003525 }
3526
Chris Mason890871b2009-09-02 16:24:52 -04003527 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003528 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003529 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003530 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003531 free_extent_map(em);
3532
3533 return 0;
3534}
3535
3536static int fill_device_from_item(struct extent_buffer *leaf,
3537 struct btrfs_dev_item *dev_item,
3538 struct btrfs_device *device)
3539{
3540 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003541
3542 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003543 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3544 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003545 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3546 device->type = btrfs_device_type(leaf, dev_item);
3547 device->io_align = btrfs_device_io_align(leaf, dev_item);
3548 device->io_width = btrfs_device_io_width(leaf, dev_item);
3549 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003550
3551 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003552 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003553
Chris Mason0b86a832008-03-24 15:01:56 -04003554 return 0;
3555}
3556
Yan Zheng2b820322008-11-17 21:11:30 -05003557static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3558{
3559 struct btrfs_fs_devices *fs_devices;
3560 int ret;
3561
3562 mutex_lock(&uuid_mutex);
3563
3564 fs_devices = root->fs_info->fs_devices->seed;
3565 while (fs_devices) {
3566 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3567 ret = 0;
3568 goto out;
3569 }
3570 fs_devices = fs_devices->seed;
3571 }
3572
3573 fs_devices = find_fsid(fsid);
3574 if (!fs_devices) {
3575 ret = -ENOENT;
3576 goto out;
3577 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003578
3579 fs_devices = clone_fs_devices(fs_devices);
3580 if (IS_ERR(fs_devices)) {
3581 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003582 goto out;
3583 }
3584
Christoph Hellwig97288f22008-12-02 06:36:09 -05003585 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003586 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003587 if (ret)
3588 goto out;
3589
3590 if (!fs_devices->seeding) {
3591 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003592 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003593 ret = -EINVAL;
3594 goto out;
3595 }
3596
3597 fs_devices->seed = root->fs_info->fs_devices->seed;
3598 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003599out:
3600 mutex_unlock(&uuid_mutex);
3601 return ret;
3602}
3603
Chris Mason0d81ba52008-03-24 15:02:07 -04003604static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003605 struct extent_buffer *leaf,
3606 struct btrfs_dev_item *dev_item)
3607{
3608 struct btrfs_device *device;
3609 u64 devid;
3610 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003611 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003612 u8 dev_uuid[BTRFS_UUID_SIZE];
3613
Chris Mason0b86a832008-03-24 15:01:56 -04003614 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003615 read_extent_buffer(leaf, dev_uuid,
3616 (unsigned long)btrfs_device_uuid(dev_item),
3617 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003618 read_extent_buffer(leaf, fs_uuid,
3619 (unsigned long)btrfs_device_fsid(dev_item),
3620 BTRFS_UUID_SIZE);
3621
3622 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3623 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003624 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003625 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003626 }
3627
3628 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3629 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003630 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003631 return -EIO;
3632
3633 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003634 printk(KERN_WARNING "warning devid %llu missing\n",
3635 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003636 device = add_missing_dev(root, devid, dev_uuid);
3637 if (!device)
3638 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003639 } else if (!device->missing) {
3640 /*
3641 * this happens when a device that was properly setup
3642 * in the device info lists suddenly goes bad.
3643 * device->bdev is NULL, and so we have to set
3644 * device->missing to one here
3645 */
3646 root->fs_info->fs_devices->missing_devices++;
3647 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003648 }
3649 }
3650
3651 if (device->fs_devices != root->fs_info->fs_devices) {
3652 BUG_ON(device->writeable);
3653 if (device->generation !=
3654 btrfs_device_generation(leaf, dev_item))
3655 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003656 }
Chris Mason0b86a832008-03-24 15:01:56 -04003657
3658 fill_device_from_item(leaf, dev_item, device);
3659 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003660 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04003661 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003662 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04003663 spin_lock(&root->fs_info->free_chunk_lock);
3664 root->fs_info->free_chunk_space += device->total_bytes -
3665 device->bytes_used;
3666 spin_unlock(&root->fs_info->free_chunk_lock);
3667 }
Chris Mason0b86a832008-03-24 15:01:56 -04003668 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003669 return ret;
3670}
3671
Yan Zhenge4404d62008-12-12 10:03:26 -05003672int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003673{
David Sterba6c417612011-04-13 15:41:04 +02003674 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003675 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003676 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003677 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003678 u8 *ptr;
3679 unsigned long sb_ptr;
3680 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003681 u32 num_stripes;
3682 u32 array_size;
3683 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003684 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003685 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003686
Yan Zhenge4404d62008-12-12 10:03:26 -05003687 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003688 BTRFS_SUPER_INFO_SIZE);
3689 if (!sb)
3690 return -ENOMEM;
3691 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04003692 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05003693
Chris Masona061fc82008-05-07 11:43:44 -04003694 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003695 array_size = btrfs_super_sys_array_size(super_copy);
3696
Chris Mason0b86a832008-03-24 15:01:56 -04003697 ptr = super_copy->sys_chunk_array;
3698 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3699 cur = 0;
3700
3701 while (cur < array_size) {
3702 disk_key = (struct btrfs_disk_key *)ptr;
3703 btrfs_disk_key_to_cpu(&key, disk_key);
3704
Chris Masona061fc82008-05-07 11:43:44 -04003705 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003706 sb_ptr += len;
3707 cur += len;
3708
Chris Mason0d81ba52008-03-24 15:02:07 -04003709 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003710 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003711 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003712 if (ret)
3713 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003714 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3715 len = btrfs_chunk_item_size(num_stripes);
3716 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003717 ret = -EIO;
3718 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003719 }
3720 ptr += len;
3721 sb_ptr += len;
3722 cur += len;
3723 }
Chris Masona061fc82008-05-07 11:43:44 -04003724 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003725 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003726}
3727
3728int btrfs_read_chunk_tree(struct btrfs_root *root)
3729{
3730 struct btrfs_path *path;
3731 struct extent_buffer *leaf;
3732 struct btrfs_key key;
3733 struct btrfs_key found_key;
3734 int ret;
3735 int slot;
3736
3737 root = root->fs_info->chunk_root;
3738
3739 path = btrfs_alloc_path();
3740 if (!path)
3741 return -ENOMEM;
3742
3743 /* first we search for all of the device items, and then we
3744 * read in all of the chunk items. This way we can create chunk
3745 * mappings that reference all of the devices that are afound
3746 */
3747 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3748 key.offset = 0;
3749 key.type = 0;
3750again:
3751 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003752 if (ret < 0)
3753 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003754 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003755 leaf = path->nodes[0];
3756 slot = path->slots[0];
3757 if (slot >= btrfs_header_nritems(leaf)) {
3758 ret = btrfs_next_leaf(root, path);
3759 if (ret == 0)
3760 continue;
3761 if (ret < 0)
3762 goto error;
3763 break;
3764 }
3765 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3766 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3767 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3768 break;
3769 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3770 struct btrfs_dev_item *dev_item;
3771 dev_item = btrfs_item_ptr(leaf, slot,
3772 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003773 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003774 if (ret)
3775 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003776 }
3777 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3778 struct btrfs_chunk *chunk;
3779 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3780 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003781 if (ret)
3782 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003783 }
3784 path->slots[0]++;
3785 }
3786 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3787 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003788 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003789 goto again;
3790 }
Chris Mason0b86a832008-03-24 15:01:56 -04003791 ret = 0;
3792error:
Yan Zheng2b820322008-11-17 21:11:30 -05003793 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003794 return ret;
3795}